mirror of
https://github.com/agentclientprotocol/claude-agent-acp.git
synced 2026-12-01 21:52:06 +00:00
Compare commits
1 Commits
main
...
codex/acp-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6949773d76 |
@@ -6053,6 +6053,201 @@ describe("ACP v2 core", () => {
|
||||
return update.sessionUpdate === "session_info_update" && report ? [report] : [];
|
||||
});
|
||||
|
||||
it.each([
|
||||
["frame-first", "next result"],
|
||||
["response-first", "next result"],
|
||||
["frame-first", "next withdrawal"],
|
||||
["response-first", "next withdrawal"],
|
||||
["frame-first", "session close"],
|
||||
["frame-first", "next lifecycle ends"],
|
||||
])(
|
||||
"keeps a pending file report ordered and the session responsive (%s, %s)",
|
||||
async (order, followUp) => {
|
||||
const client = v2Client();
|
||||
await client.connect(async (agent) => {
|
||||
const { response, claude } = await openSession(agent, air("agentFileChangeReport"));
|
||||
const sessionId = response.sessionId;
|
||||
const canFinishReport = Promise.withResolvers<void>();
|
||||
const workspace = await realpath(cwd);
|
||||
const firstPath = path.join(workspace, "first.ts");
|
||||
const nextPath = path.join(workspace, "next.ts");
|
||||
claude.rewindFiles
|
||||
.mockImplementationOnce(async () => {
|
||||
await canFinishReport.promise;
|
||||
return { canRewind: true, filesChanged: [firstPath] };
|
||||
})
|
||||
.mockResolvedValueOnce({ canRewind: true, filesChanged: [nextPath] });
|
||||
|
||||
const first = agent.request(v2.methods.agent.session.prompt, {
|
||||
sessionId,
|
||||
prompt: PROMPT,
|
||||
_meta: requestMeta("first"),
|
||||
});
|
||||
const running = await claude.prompt(0);
|
||||
claude.send(lifecycle(running, "started"));
|
||||
await first;
|
||||
for (const event of textDelta("msg_first", "A's reply")) claude.send(event);
|
||||
|
||||
const controller = new AbortController();
|
||||
const second = agent
|
||||
.request(
|
||||
v2.methods.agent.session.prompt,
|
||||
{ sessionId, prompt: PROMPT },
|
||||
{ cancellationSignal: controller.signal },
|
||||
)
|
||||
.catch((error: unknown) => error);
|
||||
const waiting = await claude.prompt(1);
|
||||
expect(waiting.priority).toBe("now");
|
||||
claude.send(result({ user_message_uuids: [running.uuid] }));
|
||||
await vi.waitFor(() =>
|
||||
expect(client.sessionUpdates).toContainEqual(
|
||||
expect.objectContaining({
|
||||
sessionUpdate: "usage_update",
|
||||
cost: expect.any(Object),
|
||||
}),
|
||||
),
|
||||
);
|
||||
expect(client.states()).toEqual(["running"]);
|
||||
|
||||
const cancelAck = Promise.withResolvers<boolean>();
|
||||
claude.cancelAsyncMessage.mockReturnValueOnce(cancelAck.promise);
|
||||
controller.abort();
|
||||
await vi.waitFor(() =>
|
||||
expect(claude.cancelAsyncMessage).toHaveBeenCalledWith(waiting.uuid),
|
||||
);
|
||||
if (order === "frame-first") claude.send(lifecycle(waiting, "cancelled"));
|
||||
else cancelAck.resolve(true);
|
||||
expect(await second).toMatchObject({ code: -32800 });
|
||||
await vi.waitFor(() =>
|
||||
expect(claude.rewindFiles).toHaveBeenCalledWith(running.uuid, { dryRun: true }),
|
||||
);
|
||||
if (order === "frame-first") cancelAck.resolve(true);
|
||||
else claude.send(lifecycle(waiting, "cancelled"));
|
||||
expect(client.states()).toEqual(["running"]);
|
||||
expect(claude.interrupt).not.toHaveBeenCalled();
|
||||
|
||||
if (followUp === "session close") {
|
||||
await agent.request(v2.methods.agent.session.close, { sessionId });
|
||||
await vi.waitFor(() => expect(client.states()).toEqual(["running", "idle"]));
|
||||
expect(reports(client.sessionUpdates)).toEqual([
|
||||
expect.objectContaining({
|
||||
requestId: "first",
|
||||
status: "unavailable",
|
||||
reason: "cancelled",
|
||||
}),
|
||||
]);
|
||||
expect(client.sessionUpdates.at(-1)).toMatchObject({
|
||||
state: "idle",
|
||||
stopReason: "cancelled",
|
||||
});
|
||||
canFinishReport.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const nextController = new AbortController();
|
||||
const third = agent
|
||||
.request(
|
||||
v2.methods.agent.session.prompt,
|
||||
{
|
||||
sessionId,
|
||||
prompt: PROMPT,
|
||||
...(followUp === "next result" ? { _meta: requestMeta("next") } : {}),
|
||||
},
|
||||
{ cancellationSignal: nextController.signal },
|
||||
)
|
||||
.catch((error: unknown) => error);
|
||||
const next = await claude.prompt(2);
|
||||
expect(next.priority).toBe("now");
|
||||
if (followUp === "next withdrawal") {
|
||||
const nextAck = Promise.withResolvers<boolean>();
|
||||
claude.cancelAsyncMessage.mockReturnValueOnce(nextAck.promise);
|
||||
nextController.abort();
|
||||
await vi.waitFor(() =>
|
||||
expect(claude.cancelAsyncMessage).toHaveBeenCalledWith(next.uuid),
|
||||
);
|
||||
claude.send(lifecycle(next, "cancelled"));
|
||||
expect(await third).toMatchObject({ code: -32800 });
|
||||
expect(reports(client.sessionUpdates)).toHaveLength(0);
|
||||
expect(client.states()).toEqual(["running"]);
|
||||
expect(claude.interrupt).not.toHaveBeenCalled();
|
||||
nextAck.resolve(true);
|
||||
canFinishReport.resolve();
|
||||
await vi.waitFor(() => expect(client.states()).toEqual(["running", "idle"]));
|
||||
expect(reports(client.sessionUpdates)).toEqual([
|
||||
expect.objectContaining({
|
||||
requestId: "first",
|
||||
status: "reported",
|
||||
paths: [firstPath],
|
||||
}),
|
||||
]);
|
||||
return;
|
||||
}
|
||||
claude.send(lifecycle(next, "started"));
|
||||
if (followUp === "next lifecycle ends") {
|
||||
expect(await third).toEqual({ messageId: next.uuid });
|
||||
claude.send(lifecycle(next, "completed"));
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
expect(client.states()).toEqual(["running"]);
|
||||
canFinishReport.resolve();
|
||||
await vi.waitFor(() => expect(client.states()).toEqual(["running", "idle"]));
|
||||
expect(reports(client.sessionUpdates)).toEqual([
|
||||
expect.objectContaining({
|
||||
requestId: "first",
|
||||
status: "reported",
|
||||
paths: [firstPath],
|
||||
}),
|
||||
]);
|
||||
return;
|
||||
}
|
||||
for (const event of textDelta("msg_next", "C's reply")) claude.send(event);
|
||||
claude.send(result({ user_message_uuids: [next.uuid] }));
|
||||
expect(await third).toEqual({ messageId: next.uuid });
|
||||
await vi.waitFor(() =>
|
||||
expect(reports(client.sessionUpdates)).toEqual([
|
||||
expect.objectContaining({
|
||||
requestId: "next",
|
||||
status: "reported",
|
||||
paths: [nextPath],
|
||||
}),
|
||||
]),
|
||||
);
|
||||
canFinishReport.resolve();
|
||||
await vi.waitFor(() => {
|
||||
expect(client.states()).toEqual(["running", "idle"]);
|
||||
expect(reports(client.sessionUpdates)).toHaveLength(2);
|
||||
});
|
||||
|
||||
const events = client.sessionUpdates.flatMap((update) => {
|
||||
const [report] = reports([update]);
|
||||
if (report) return [`${report.requestId} report`];
|
||||
if (update.sessionUpdate === "state_update") return [update.state];
|
||||
if (update.sessionUpdate === "user_message" && update.messageId === next.uuid)
|
||||
return ["C accepted"];
|
||||
if (update.sessionUpdate === "agent_message_chunk" && update.messageId === "msg_next")
|
||||
return ["C output"];
|
||||
return [];
|
||||
});
|
||||
expect(events).toEqual([
|
||||
"running",
|
||||
"C accepted",
|
||||
"C output",
|
||||
"next report",
|
||||
"first report",
|
||||
"idle",
|
||||
]);
|
||||
expect(reports(client.sessionUpdates).at(-1)).toMatchObject({
|
||||
requestId: "first",
|
||||
status: "reported",
|
||||
paths: [firstPath],
|
||||
});
|
||||
expect(claude.rewindFiles.mock.calls).toEqual([
|
||||
[running.uuid, { dryRun: true }],
|
||||
[next.uuid, { dryRun: true }],
|
||||
]);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("lists the files of the prompt's checkpoint at its turn's end, before the idle", async () => {
|
||||
const client = v2Client();
|
||||
const changed = path.join(await realpath(cwd), "src", "App.ts");
|
||||
|
||||
@@ -28,6 +28,12 @@ export class TurnFileChanges {
|
||||
private readonly pending = new Map<string, NativeFileChangeReportTurn>();
|
||||
/** The reports of the prompts that the open turn serves. */
|
||||
private readonly turn: NativeFileChangeReportTurn[] = [];
|
||||
/** Reports already reading checkpoints, until they publish or the turn is stopped. */
|
||||
private readonly collecting = new Set<NativeFileChangeReportTurn>();
|
||||
|
||||
get reading(): boolean {
|
||||
return this.collecting.size > 0;
|
||||
}
|
||||
/** The tool uses of subagents in the open turn, for their file edits. */
|
||||
private subagentToolUses: ToolUses = {};
|
||||
|
||||
@@ -118,7 +124,12 @@ export class TurnFileChanges {
|
||||
}
|
||||
const reports = this.turn.splice(0);
|
||||
this.subagentToolUses = {};
|
||||
return Promise.all(reports.map((report) => this.reporter.report(report, query))).then(() => {});
|
||||
for (const report of reports) this.collecting.add(report);
|
||||
return Promise.all(reports.map((report) => this.reporter.report(report, query)))
|
||||
.then(() => {})
|
||||
.finally(() => {
|
||||
for (const report of reports) this.collecting.delete(report);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,9 +137,13 @@ export class TurnFileChanges {
|
||||
* such as of a turn that ended without a result, go out `unavailable`:
|
||||
* `cancelled` after a cancel, else `providerError`.
|
||||
*/
|
||||
turnClosed(cancelled: boolean): void {
|
||||
turnClosed(cancelled: boolean, stopReading = false): void {
|
||||
const reason: FileChangeReportUnavailableReason = cancelled ? "cancelled" : "providerError";
|
||||
for (const report of this.turn.splice(0)) this.reporter.finish(report.fileChangeReport, reason);
|
||||
if (stopReading) {
|
||||
for (const report of this.collecting) this.reporter.finish(report.fileChangeReport, reason);
|
||||
this.collecting.clear();
|
||||
}
|
||||
this.subagentToolUses = {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,6 +153,7 @@ export class V2Session implements RequestTarget {
|
||||
private readonly steeredPrompts = new Set<string>();
|
||||
/** Whether a turn of Claude Code is open: the session's foreground work. */
|
||||
private foreground = false;
|
||||
private turnNumber = 0;
|
||||
/** The prompt whose `started` frame came last in the open turn. */
|
||||
private turnCommand: string | undefined;
|
||||
/**
|
||||
@@ -1387,9 +1388,16 @@ export class V2Session implements RequestTarget {
|
||||
return;
|
||||
}
|
||||
const { stopReason } = idleOutcome(this.lastEnd, this.cancelledWork, this.turnReport);
|
||||
const reading = this.fileChanges?.reading;
|
||||
const reporting = this.fileChanges?.turnEnding(stopReason, this.query);
|
||||
if (!reporting) return this.closeTurn();
|
||||
return reporting.then(() => this.closeTurn());
|
||||
const turn = this.turnNumber;
|
||||
const close = reporting.then(() => {
|
||||
if (this.turnNumber === turn && !this.cycleOpen) this.closeTurn();
|
||||
});
|
||||
// A withdrawn prompt already started reading the turn's reports. Later
|
||||
// messages can make progress while idle waits for all the reports.
|
||||
if (!reading) return close;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1458,7 +1466,7 @@ export class V2Session implements RequestTarget {
|
||||
// Cancelled work that ended with the query was still cancelled.
|
||||
if (this.cancelledWork) this.output.closeUnfinished(true);
|
||||
else this.output.finishUnfinished("failed");
|
||||
this.fileChanges?.turnClosed(this.cancelledWork);
|
||||
this.fileChanges?.turnClosed(this.cancelledWork, true);
|
||||
// As `closeTurn`: a compaction still `in_progress` ends before the idle.
|
||||
this.reports.turnEnded();
|
||||
this.foreground = false;
|
||||
@@ -1507,6 +1515,7 @@ export class V2Session implements RequestTarget {
|
||||
}
|
||||
if (this.foreground) return;
|
||||
this.foreground = true;
|
||||
this.turnNumber++;
|
||||
this.lastEnd = undefined;
|
||||
this.reportState();
|
||||
}
|
||||
@@ -1518,6 +1527,13 @@ export class V2Session implements RequestTarget {
|
||||
*/
|
||||
private closeTurn(): void {
|
||||
if (!this.foreground || this.clearContext.holdsTurn() || this.steeredPrompts.size > 0) return;
|
||||
if (this.fileChanges?.reading && !this.cancelledWork) {
|
||||
// A lifecycle frame can end the cycle even while the turn must stay
|
||||
// open for its report. A later cycle can still start before it arrives.
|
||||
this.cycleOpen = false;
|
||||
this.cycleCommand = undefined;
|
||||
return;
|
||||
}
|
||||
// A subagent's end that still waited for its messages, before the turn's idle.
|
||||
this.tasks.turnClosed();
|
||||
// v2 asks for a final status of the turn's tool calls before its idle. A
|
||||
@@ -1535,7 +1551,7 @@ export class V2Session implements RequestTarget {
|
||||
this.reportApiError(this.lastEnd);
|
||||
this.reports.turnEnded();
|
||||
this.elicitations.turnEnded();
|
||||
this.fileChanges?.turnClosed(cancelled);
|
||||
this.fileChanges?.turnClosed(cancelled, cancelled);
|
||||
this.foreground = false;
|
||||
this.reportState();
|
||||
this.cancelledWork = false;
|
||||
|
||||
Reference in New Issue
Block a user