Skip to content

Commit 33f7100

Browse files
committed
Fix ACP canonical session key handling
1 parent 1ef3049 commit 33f7100

4 files changed

Lines changed: 117 additions & 1 deletion

File tree

‎src/acp/event-ledger.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,43 @@ describe("ACP event ledger", () => {
138138
]);
139139
});
140140

141+
it("preserves prompt history when a provisional ACP key becomes a canonical Gateway key", async () => {
142+
const ledger = createInMemoryAcpEventLedger({ now: () => 1000 });
143+
await ledger.startSession({
144+
sessionId: "acp-session-1",
145+
sessionKey: "acp:gateway-session-1",
146+
cwd: "/work",
147+
complete: true,
148+
});
149+
await ledger.recordUserPrompt({
150+
sessionId: "acp-session-1",
151+
sessionKey: "acp:gateway-session-1",
152+
runId: "run-1",
153+
prompt: [{ type: "text", text: "Question" }],
154+
});
155+
await ledger.recordUpdate({
156+
sessionId: "acp-session-1",
157+
sessionKey: "agent:main:acp:gateway-session-1",
158+
runId: "run-1",
159+
update: {
160+
sessionUpdate: "agent_message_chunk",
161+
content: { type: "text", text: "Answer" },
162+
},
163+
});
164+
165+
const replay = await ledger.readReplayBySessionKey({
166+
sessionKey: "agent:main:acp:gateway-session-1",
167+
});
168+
169+
expect(replay.complete).toBe(true);
170+
expect(replay.sessionId).toBe("acp-session-1");
171+
expect(replay.sessionKey).toBe("agent:main:acp:gateway-session-1");
172+
expect(replay.events.map((event) => event.update.sessionUpdate)).toEqual([
173+
"user_message_chunk",
174+
"agent_message_chunk",
175+
]);
176+
});
177+
141178
it("can replay multi-block prompt history by ACP session id", async () => {
142179
const ledger = createInMemoryAcpEventLedger({ now: () => 1000 });
143180
await ledger.startSession({

‎src/acp/event-ledger.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ function getOrCreateSession(
233233
): LedgerSession {
234234
const now = state.now();
235235
const existing = state.store.sessions[params.sessionId];
236-
if (!params.reset && existing && existing.sessionKey === params.sessionKey) {
236+
if (!params.reset && existing) {
237+
existing.sessionKey = params.sessionKey;
237238
if (params.cwd) {
238239
existing.cwd = params.cwd;
239240
}

‎src/acp/translator.stop-reason.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,63 @@ describe("acp translator stop reason mapping", () => {
5959
await expect(promptPromise).resolves.toEqual({ stopReason: "cancelled" });
6060
});
6161

62+
it("reconciles provisional ACP session keys to canonical Gateway keys by run id", async () => {
63+
const sentRunIds: string[] = [];
64+
const request = vi.fn(async (method: string, params?: Record<string, unknown>) => {
65+
if (method === "chat.send") {
66+
const runId = params?.idempotencyKey;
67+
if (typeof runId === "string") {
68+
sentRunIds.push(runId);
69+
}
70+
}
71+
return {};
72+
}) as GatewayClient["request"];
73+
const { agent, sessionId, sessionStore } = createSessionAgentHarness(request, {
74+
sessionKey: "acp:session-1",
75+
});
76+
77+
const firstPrompt = promptAgent(agent, sessionId);
78+
await vi.waitFor(() => {
79+
expect(sentRunIds).toHaveLength(1);
80+
});
81+
await agent.handleGatewayEvent(
82+
createChatEvent({
83+
runId: sentRunIds[0],
84+
sessionKey: "agent:main:acp:session-1",
85+
seq: 1,
86+
state: "final",
87+
message: {
88+
content: [{ type: "text", text: "first" }],
89+
},
90+
}),
91+
);
92+
93+
await expect(firstPrompt).resolves.toEqual({ stopReason: "end_turn" });
94+
expect(sessionStore.getSession(sessionId)?.sessionKey).toBe("agent:main:acp:session-1");
95+
96+
const secondPrompt = promptAgent(agent, sessionId, "again");
97+
await vi.waitFor(() => {
98+
expect(sentRunIds).toHaveLength(2);
99+
});
100+
expect(request).toHaveBeenLastCalledWith(
101+
"chat.send",
102+
expect.objectContaining({
103+
sessionKey: "agent:main:acp:session-1",
104+
}),
105+
{ timeoutMs: null },
106+
);
107+
await agent.handleGatewayEvent(
108+
createChatEvent({
109+
runId: sentRunIds[1],
110+
sessionKey: "agent:main:acp:session-1",
111+
seq: 2,
112+
state: "final",
113+
}),
114+
);
115+
116+
await expect(secondPrompt).resolves.toEqual({ stopReason: "end_turn" });
117+
});
118+
62119
it("keeps in-flight prompts pending across transient gateway disconnects", async () => {
63120
const { agent, promptPromise, runId } = await createPendingPromptHarness();
64121
const settleSpy = observeSettlement(promptPromise);

‎src/acp/translator.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,9 +1286,30 @@ export class AcpGatewayAgent implements Agent {
12861286
}
12871287
return pending;
12881288
}
1289+
if (runId) {
1290+
for (const pending of this.pendingPrompts.values()) {
1291+
if (pending.idempotencyKey !== runId) {
1292+
continue;
1293+
}
1294+
this.reconcilePendingSessionKey(pending, sessionKey);
1295+
return pending;
1296+
}
1297+
}
12891298
return undefined;
12901299
}
12911300

1301+
private reconcilePendingSessionKey(pending: PendingPrompt, sessionKey: string): void {
1302+
if (pending.sessionKey === sessionKey) {
1303+
return;
1304+
}
1305+
this.log(`session key reconciled: ${pending.sessionKey} -> ${sessionKey}`);
1306+
pending.sessionKey = sessionKey;
1307+
const session = this.sessionStore.getSession(pending.sessionId);
1308+
if (session?.activeRunId === pending.idempotencyKey) {
1309+
session.sessionKey = sessionKey;
1310+
}
1311+
}
1312+
12921313
private clearDisconnectTimer(): void {
12931314
if (!this.disconnectTimer) {
12941315
return;

0 commit comments

Comments
 (0)