Skip to content

Commit 58d08de

Browse files
committed
fix(acp): capture caller lifecycle before setup
1 parent bb7aff1 commit 58d08de

2 files changed

Lines changed: 91 additions & 3 deletions

File tree

‎src/auto-reply/reply/dispatch-acp.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,73 @@ describe("tryDispatchAcpReply", () => {
788788
clearAgentRunContext(runId, replacementGeneration);
789789
});
790790

791+
it("captures caller-owned ACP lifecycle before lazy dispatch setup yields", async () => {
792+
setReadyAcpResolution();
793+
const runId = "caller-reused-during-setup";
794+
const admittedGeneration = getAgentEventLifecycleGeneration();
795+
claimAgentRunContext(runId, {
796+
agentId: "caller",
797+
lifecycleGeneration: admittedGeneration,
798+
sessionKey,
799+
});
800+
const admittedToken = getAgentRunContextLifecycleToken(runId, admittedGeneration);
801+
let startGeneration: string | undefined;
802+
let startToken: object | undefined;
803+
let callbackGeneration: string | undefined;
804+
let callbackToken: object | undefined;
805+
let terminalGeneration: string | undefined;
806+
let terminalToken: object | undefined;
807+
808+
auditMocks.emitAcpLifecycleStart.mockImplementationOnce(
809+
(params: { lifecycleGeneration?: string }) => {
810+
startGeneration = params.lifecycleGeneration;
811+
startToken = getAgentRunExecutionContextLifecycleToken(runId);
812+
},
813+
);
814+
auditMocks.emitAcpRuntimeEvent.mockImplementationOnce(() => {
815+
callbackGeneration = getAgentRunExecutionLifecycleGeneration();
816+
callbackToken = getAgentRunExecutionContextLifecycleToken(runId);
817+
});
818+
auditMocks.emitAcpLifecycleEnd.mockImplementationOnce(() => {
819+
terminalGeneration = getAgentRunExecutionLifecycleGeneration();
820+
terminalToken = getAgentRunExecutionContextLifecycleToken(runId);
821+
});
822+
managerMocks.runTurn.mockImplementationOnce(
823+
async ({ onEvent }: { onEvent?: (event: unknown) => Promise<void> }) => {
824+
if (onEvent) {
825+
await emitToolLifecycleEvents(onEvent, "tool-after-setup-reuse");
826+
}
827+
},
828+
);
829+
830+
const dispatch = runDispatch({ bodyForAgent: "audit setup race", runId });
831+
const replacementGeneration = rotateAgentEventLifecycleGeneration();
832+
claimAgentRunContext(runId, {
833+
agentId: "replacement",
834+
lifecycleGeneration: replacementGeneration,
835+
sessionKey: "agent:replacement:main",
836+
});
837+
const replacementToken = getAgentRunContextLifecycleToken(runId, replacementGeneration);
838+
839+
await dispatch;
840+
841+
expect(admittedToken).toBeTypeOf("object");
842+
expect(replacementToken).toBeTypeOf("object");
843+
expect(replacementToken).not.toBe(admittedToken);
844+
expect(startGeneration).toBe(admittedGeneration);
845+
expect(startToken).toBe(admittedToken);
846+
expect(callbackGeneration).toBe(admittedGeneration);
847+
expect(callbackToken).toBe(admittedToken);
848+
expect(terminalGeneration).toBe(admittedGeneration);
849+
expect(terminalToken).toBe(admittedToken);
850+
expect(getAgentRunContext(runId)).toMatchObject({
851+
agentId: "replacement",
852+
lifecycleGeneration: replacementGeneration,
853+
sessionKey: "agent:replacement:main",
854+
});
855+
clearAgentRunContext(runId, replacementGeneration);
856+
});
857+
791858
it("keeps audit run ids unique when channel message ids repeat", async () => {
792859
setReadyAcpResolution();
793860

‎src/auto-reply/reply/dispatch-acp.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ import {
2222
captureAgentRunLifecycleGeneration,
2323
withAgentRunLifecycleGeneration,
2424
} from "../../infra/agent-events.js";
25+
import { captureAgentRunExecutionContextLifecycleToken } from "../../infra/agent-run-execution-context.js";
2526
import {
2627
claimAgentRunContext,
28+
getAgentRunContextLifecycleToken,
2729
releaseAgentRunContext,
2830
retainActiveAgentRunContext,
2931
} from "../../infra/agent-run-registry.js";
@@ -455,6 +457,17 @@ export async function tryDispatchAcpReply(params: {
455457
return null;
456458
}
457459

460+
const existingRunId = normalizeOptionalString(params.runId);
461+
// Lazy ACP setup can yield while a caller-owned run id is rotated and reused.
462+
// Snapshot both ownership identities now so later audit callbacks cannot rebound.
463+
const admittedAuditLifecycleGeneration = existingRunId
464+
? captureAgentRunLifecycleGeneration(existingRunId)
465+
: undefined;
466+
const admittedAuditContextLifecycleToken =
467+
existingRunId && admittedAuditLifecycleGeneration
468+
? getAgentRunContextLifecycleToken(existingRunId, admittedAuditLifecycleGeneration)
469+
: undefined;
470+
458471
const { getAcpSessionManager } = await loadDispatchAcpManagerRuntime();
459472
const acpManager = getAcpSessionManager();
460473
const acpResolution = acpManager.resolveSession({
@@ -581,14 +594,13 @@ export async function tryDispatchAcpReply(params: {
581594
markIdle: params.markIdle,
582595
});
583596
const requestId = resolveAcpRequestId(params.ctx);
584-
const existingRunId = normalizeOptionalString(params.runId);
585597
const auditOnly = existingRunId === undefined;
586598
const auditRunId = existingRunId ?? generateSecureUuid();
587599
const auditRuntime = await loadDispatchAcpAuditRuntime();
588600
const auditToolTracker = auditRuntime.createAcpToolLifecycleTracker();
589601
let auditStarted = false;
590602
let auditFinished = false;
591-
let auditLifecycleGeneration: string | undefined;
603+
let auditLifecycleGeneration = admittedAuditLifecycleGeneration;
592604
let auditContextOwnerToken: string | undefined;
593605
let releaseAuditContextLease: (() => void) | undefined;
594606
let auditTerminalOutcome: "blocked" | undefined;
@@ -639,7 +651,16 @@ export async function tryDispatchAcpReply(params: {
639651
const runWithAuditLifecycle = <T>(run: () => T): T => {
640652
claimAuditContext();
641653
return auditLifecycleGeneration
642-
? withAgentRunLifecycleGeneration(auditLifecycleGeneration, run)
654+
? withAgentRunLifecycleGeneration(auditLifecycleGeneration, () => {
655+
if (admittedAuditContextLifecycleToken) {
656+
captureAgentRunExecutionContextLifecycleToken(
657+
auditRunId,
658+
auditLifecycleGeneration,
659+
admittedAuditContextLifecycleToken,
660+
);
661+
}
662+
return run();
663+
})
643664
: run();
644665
};
645666
const releaseAuditContext = () => {

0 commit comments

Comments
 (0)