Skip to content

Commit 374e05f

Browse files
committed
fix(agents): reject CLI attribution collisions
1 parent 94381ba commit 374e05f

4 files changed

Lines changed: 81 additions & 19 deletions

File tree

‎src/agents/embedded-agent-runner/run/lane-controller.ts‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
withAgentRunLifecycleGeneration,
55
} from "../../../infra/agent-events.js";
66
import {
7+
assertAgentRunAttributionCompatible,
78
claimAgentRunContext,
89
getAgentRunContext,
910
retainQueuedAgentRunContext,
@@ -184,22 +185,7 @@ export function createEmbeddedRunLaneController<TParams extends LaneParams>(opti
184185
existingContext?.lifecycleGeneration === lifecycleGeneration
185186
? existingContext.attribution
186187
: undefined;
187-
if (existingAttribution && !params.attribution) {
188-
throw new TypeError(
189-
"Agent run ID is already bound to host-owned execution attribution.",
190-
);
191-
}
192-
if (
193-
existingAttribution &&
194-
params.attribution &&
195-
(existingAttribution.contextId !== params.attribution.contextId ||
196-
existingAttribution.executionId !== params.attribution.executionId ||
197-
existingAttribution.createdAt !== params.attribution.createdAt)
198-
) {
199-
throw new TypeError(
200-
"Agent run ID is already bound to different execution attribution.",
201-
);
202-
}
188+
assertAgentRunAttributionCompatible(existingAttribution, params.attribution);
203189
const { attribution: _existingAttribution, ...existingContextFields } =
204190
existingContext ?? {};
205191
const admittedAt = Date.now();

‎src/auto-reply/reply/agent-runner-cli-candidate.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import {
1919
assertAgentRunLifecycleGenerationCurrent,
2020
getAgentEventLifecycleGeneration,
2121
} from "../../infra/agent-events.js";
22-
import { claimAgentRunContext, getAgentRunContext } from "../../infra/agent-run-registry.js";
22+
import {
23+
assertAgentRunAttributionCompatible,
24+
claimAgentRunContext,
25+
getAgentRunContext,
26+
} from "../../infra/agent-run-registry.js";
2327
import {
2428
getGeneratedMediaTaskIdsForSessionKey,
2529
hasNewGeneratedMediaTaskForSessionKey,
@@ -193,11 +197,20 @@ export async function runCliFallbackCandidate(params: {
193197
if (lifecycleGeneration !== params.getLifecycleGeneration()) {
194198
params.onLifecycleGeneration(lifecycleGeneration);
195199
}
200+
const existingContext = getAgentRunContext(params.runId);
201+
const existingAttribution =
202+
existingContext?.lifecycleGeneration === lifecycleGeneration
203+
? existingContext.attribution
204+
: undefined;
205+
assertAgentRunAttributionCompatible(existingAttribution, attribution);
196206
if (attribution !== turn.attribution) {
197207
turn.attribution = attribution;
198208
}
199-
const { registeredAt: _registeredAt, ...reboundRunContext } =
200-
getAgentRunContext(params.runId) ?? {};
209+
const {
210+
attribution: _existingAttribution,
211+
registeredAt: _registeredAt,
212+
...reboundRunContext
213+
} = existingContext ?? {};
201214
claimAgentRunContext(params.runId, {
202215
// Re-admission starts a new TTL window after placement waits or rotation.
203216
...reboundRunContext,

‎src/auto-reply/reply/agent-runner-execution-runtime.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,50 @@ describe("executeAgentTurn: runtime selection", () => {
284284
expect(cliAttribution).not.toHaveProperty("agentId");
285285
});
286286

287+
it("rejects a CLI run id already bound to different execution attribution", async () => {
288+
const agentRunRegistry = await import("../../infra/agent-run-registry.js");
289+
const lifecycleGeneration = getAgentEventLifecycleGeneration();
290+
const runId = "cli-attribution-collision";
291+
const existingAttribution = createAgentExecutionAttribution({
292+
runId,
293+
lifecycleGeneration,
294+
});
295+
agentRunRegistry.claimAgentRunContext(runId, {
296+
attribution: existingAttribution,
297+
lifecycleGeneration,
298+
});
299+
state.isCliProviderMock.mockReturnValue(true);
300+
state.runWithModelFallbackMock.mockImplementationOnce(async (params: FallbackRunnerParams) => ({
301+
result: await params.run("codex-cli", "gpt-5.4"),
302+
provider: "codex-cli",
303+
model: "gpt-5.4",
304+
attempts: [],
305+
}));
306+
const followupRun = createFollowupRun();
307+
followupRun.run.provider = "codex-cli";
308+
followupRun.run.model = "gpt-5.4";
309+
const executeAgentTurn = await getExecuteAgentTurnForTest();
310+
311+
await expect(
312+
executeAgentTurn({
313+
...createMinimalRunAgentTurnParams({ followupRun, opts: { runId } }),
314+
attribution: createAgentExecutionAttribution({
315+
runId,
316+
lifecycleGeneration,
317+
}),
318+
}),
319+
).resolves.toEqual({
320+
kind: "final",
321+
payload: {
322+
isError: true,
323+
text: "⚠️ Something went wrong while processing your request. Please try again, or use /new to start a fresh session.",
324+
},
325+
});
326+
327+
expect(state.runCliAgentMock).not.toHaveBeenCalled();
328+
expect(agentRunRegistry.getAgentRunContext(runId)?.attribution).toBe(existingAttribution);
329+
});
330+
287331
it("rejects queued heartbeat CLI fallback after placement crosses a lifecycle rotation", async () => {
288332
state.isCliProviderMock.mockReturnValue(true);
289333
state.runWithModelFallbackMock.mockImplementationOnce(async (params: FallbackRunnerParams) => ({

‎src/infra/agent-run-registry.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ function attachAgentExecutionAttribution(
8181
});
8282
}
8383

84+
/** Rejects a same-generation run id that is already bound to different private identity. */
85+
export function assertAgentRunAttributionCompatible(
86+
existingAttribution: AgentExecutionAttribution | undefined,
87+
attribution: AgentExecutionAttribution | undefined,
88+
): void {
89+
if (existingAttribution && !attribution) {
90+
throw new TypeError("Agent run ID is already bound to host-owned execution attribution.");
91+
}
92+
if (
93+
existingAttribution &&
94+
attribution &&
95+
(existingAttribution.contextId !== attribution.contextId ||
96+
existingAttribution.executionId !== attribution.executionId ||
97+
existingAttribution.createdAt !== attribution.createdAt)
98+
) {
99+
throw new TypeError("Agent run ID is already bound to different execution attribution.");
100+
}
101+
}
102+
84103
function createAgentRunContext(
85104
context: AgentRunContext,
86105
lifecycleGeneration: string,

0 commit comments

Comments
 (0)