Files
plugins/packages/agent-console/test/v1/acp-resume.test.ts
T
qingnian108 a431798fe1 agent-console 1.0.0: a page plugin that drives the local Agent CLIs (#535)
Adds Agent Console (@magpie-community/page-agent-console 1.0.0), a community page plugin that drives the Agent CLIs on this computer (Claude Code, Codex, ACP agents) through a local daemon on 127.0.0.1, with its source in packages/agent-console and a registry entry. Claude Code is the user's own `claude` CLI driven over stream-json (daemon/src/providers/claude/cli.ts); no Anthropic SDK is installed, bundled or in the branch's history. Cold install ~45 MB (node-pty ~26 MB), stated in the README and registry summary in en and zh.

By qingnian108 (#535).

What was run at 8e8f910 (mote, macOS, throwaway HOME, no real accounts):
- packages/agent-console: `npm ci --ignore-scripts --workspaces=false`; `node scripts/verify-build.mjs`: all five bundles byte-identical (daemon/dist/main.js 08c95aa3…, app/app.js 9a6911f3…); `vitest run`: 45 files, 254 passed, 17 skipped; `npm run check` clean.
- Real Claude Code 2.1.316 (npm @anthropic-ai/claude-code, sandbox HOME/CLAUDE_CONFIG_DIR, local fake Messages API): `AC_REAL_CLAUDE=… vitest run --config vitest.real.config.ts test/real/claude-cli.real.test.ts`: 4 of 4 (initialize lists 51 slash commands, streaming turn, Bash permission approved and denied, interrupt, --resume with set_model).
- Probe: the driver's exact argv with `--permission-mode bypassPermissions --allow-dangerously-skip-permissions`: the tool runs with no can_use_tool request.
- Repo root: `HOME=$(mktemp -d) bun test`: 4006 tests, 0 fail; `bun scripts/check.mjs` exit 0.
- Install as magpie does: `npm pack --ignore-scripts` (552 KB) then `bun add --ignore-scripts`: 45 MB node_modules, no @anthropic-ai/*, index.mjs imports.
- Read every changed line since 41c7a94: argv values limited to SAFE_ARG tokens (no cmd.exe metacharacters reach the shim path), no new network hosts, no install scripts, no eval; no commit in main..8e8f910 contains claude-agent-sdk.

Not tried: magpie's desktop window loading the page; a Windows .cmd/.ps1 claude shim end to end.
2026-10-31 20:11:22 +08:00

139 lines
6.3 KiB
TypeScript

// Reopening an old ACP conversation: use the method the Agent's initialize advertises.
// - loadSession → session/load (Cursor, Copilot, OpenCode, Pi, Devin CLI)
// - sessionCapabilities.resume only → session/resume (dsh)
// - both → prefer load (OpenCode)
// - neither → new session + a notice that prior context wasn't restored
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { createAcpProvider } from "../../daemon/src/providers/acp/provider.js";
import { pickReconnect, sessionOpenFromInitialize } from "../../daemon/src/providers/acp/sessionCaps.js";
import type { ProviderSession } from "../../daemon/src/providers/types.js";
import type { ProviderEvent } from "../../daemon/src/timeline/types.js";
const fixture = resolve(__dirname, "fixtures", "fake-acp-agent.mjs");
const launch = { cmd: process.execPath, prefixArgs: [fixture] };
let dir: string;
const env = { session: process.env.FAKE_ACP_SESSION, slog: process.env.FAKE_ACP_SESSION_LOG };
beforeAll(() => {
dir = mkdtempSync(join(tmpdir(), "acp-resume-"));
});
afterAll(() => rmSync(dir, { recursive: true, force: true }));
afterEach(() => {
if (env.session === undefined) delete process.env.FAKE_ACP_SESSION;
else process.env.FAKE_ACP_SESSION = env.session;
if (env.slog === undefined) delete process.env.FAKE_ACP_SESSION_LOG;
else process.env.FAKE_ACP_SESSION_LOG = env.slog;
});
function provider() {
return createAcpProvider({ id: "fake-resume", name: "Fake ACP", binName: "fake-acp", acpArgs: [], installHint: "" });
}
type Run = { session: ProviderSession; events: ProviderEvent[]; slog: string; done: Promise<void> };
async function open(sessionCaps: { loadSession?: boolean; resume?: boolean } | null, resumeId: string | null): Promise<Run> {
const slog = join(dir, `s-${Math.random().toString(36).slice(2)}.jsonl`);
if (sessionCaps) process.env.FAKE_ACP_SESSION = JSON.stringify(sessionCaps);
else delete process.env.FAKE_ACP_SESSION;
process.env.FAKE_ACP_SESSION_LOG = slog;
const session = await provider().createSession({
threadId: "t1", cwd: dir, model: null, mode: "default", effort: null, resumeId, launch,
});
const events: ProviderEvent[] = [];
const done = (async () => { for await (const ev of session.events) events.push(ev); })();
return { session, events, slog, done };
}
async function until(check: () => boolean, ms = 10000) {
const end = Date.now() + ms;
while (!check()) {
if (Date.now() > end) throw new Error("timed out");
await new Promise((r) => setTimeout(r, 20));
}
}
const sessions = (r: Run) =>
existsSync(r.slog) ? readFileSync(r.slog, "utf8").trim().split("\n").filter(Boolean).map((l) => JSON.parse(l)) : [];
const notices = (r: Run) => r.events.filter((e): e is Extract<ProviderEvent, { type: "notice" }> => e.type === "notice");
const sid = (r: Run) => r.events.find((e) => e.type === "provider_session_id") as Extract<ProviderEvent, { type: "provider_session_id" }> | undefined;
async function close(r: Run) { await r.session.close(); await r.done; }
describe("sessionOpenFromInitialize / pickReconnect", () => {
it("reads loadSession and sessionCapabilities.resume the way ACP defines them", () => {
expect(sessionOpenFromInitialize({ agentCapabilities: { loadSession: true } })).toEqual({ load: true, resume: false });
expect(sessionOpenFromInitialize({ agentCapabilities: { sessionCapabilities: { resume: {} } } })).toEqual({ load: false, resume: true });
expect(sessionOpenFromInitialize({ agentCapabilities: { loadSession: true, sessionCapabilities: { resume: {}, list: {} } } })).toEqual({ load: true, resume: true });
expect(sessionOpenFromInitialize({ agentCapabilities: { sessionCapabilities: { resume: null } } })).toEqual({ load: false, resume: false });
expect(sessionOpenFromInitialize({})).toEqual({ load: false, resume: false });
});
it("prefers load, then resume, then none", () => {
expect(pickReconnect({ load: true, resume: true })).toBe("load");
expect(pickReconnect({ load: false, resume: true })).toBe("resume");
expect(pickReconnect({ load: true, resume: false })).toBe("load");
expect(pickReconnect({ load: false, resume: false })).toBe("none");
});
});
describe("reopening an ACP conversation", () => {
it("calls session/load when the Agent advertises loadSession (and keeps the stored id)", async () => {
const r = await open({ loadSession: true }, "old-load-id");
try {
await until(() => !!sid(r));
expect(sessions(r)).toEqual([{ method: "session/load", sessionId: "old-load-id" }]);
expect(sid(r)?.id).toBe("old-load-id");
expect(notices(r)).toEqual([]);
} finally {
await close(r);
}
});
it("calls session/resume when the Agent only advertises resume (dsh)", async () => {
const r = await open({ resume: true }, "old-resume-id");
try {
await until(() => !!sid(r));
expect(sessions(r)).toEqual([{ method: "session/resume", sessionId: "old-resume-id" }]);
expect(sid(r)?.id).toBe("old-resume-id");
expect(notices(r)).toEqual([]);
} finally {
await close(r);
}
});
it("prefers load when both are advertised", async () => {
const r = await open({ loadSession: true, resume: true }, "both-id");
try {
await until(() => !!sid(r));
expect(sessions(r)).toEqual([{ method: "session/load", sessionId: "both-id" }]);
} finally {
await close(r);
}
});
it("starts a new session and notices when neither load nor resume is advertised", async () => {
const r = await open({}, "orphaned-id");
try {
await until(() => !!sid(r) && notices(r).length >= 1);
expect(sessions(r)).toEqual([{ method: "session/new", sessionId: "fake-session-1" }]);
expect(sid(r)?.id).toBe("fake-session-1");
expect(notices(r)[0].message).toMatch(/session\/load|session\/resume|prior context|先前/);
} finally {
await close(r);
}
});
it("a fresh conversation still uses session/new", async () => {
const r = await open({ loadSession: true, resume: true }, null);
try {
await until(() => !!sid(r));
expect(sessions(r)).toEqual([{ method: "session/new", sessionId: "fake-session-1" }]);
expect(sid(r)?.id).toBe("fake-session-1");
} finally {
await close(r);
}
});
});