Skip to content

Commit e151f62

Browse files
shivamhwpclaude
andcommitted
fix(orchestration-v2): ignore settled subagent noise
Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent e51a097 commit e151f62

2 files changed

Lines changed: 134 additions & 20 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,7 +3577,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
35773577
assert.equal(firstCompletion?.subagent.status, "idle");
35783578
assert.equal(firstCompletion?.subagent.result, "CODEX_FIRST_DONE");
35793579
assert.equal(firstCompletion?.subagent.activationCount, 1);
3580-
assert.equal(firstCompletion?.subagent.usage?.totalTokens, 100);
3580+
assert.isNull(firstCompletion?.subagent.usage);
35813581
assert.isNull(firstCompletion?.subagent.currentActivationId);
35823582
const firstActivationUpdates = harness
35833583
.subagentActivationUpdates()
@@ -3627,7 +3627,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
36273627
}, "resumed subagent completion");
36283628
const finalSubagent = harness.subagentUpdates().at(-1)?.subagent;
36293629
assert.equal(finalSubagent?.activationCount, 2);
3630-
assert.equal(finalSubagent?.usage?.totalTokens, 180);
3630+
assert.isNull(finalSubagent?.usage);
36313631
assert.isNull(finalSubagent?.currentActivationId);
36323632
const activations = harness.subagentActivationUpdates();
36333633
assert.lengthOf(new Set(activations.map((event) => event.activation.id)), 2);
@@ -3777,6 +3777,91 @@ describe("CodexAdapterV2 post-settle continuation", () => {
37773777
],
37783778
});
37793779

3780+
const restartSettledNoiseTranscript = makeCodexReplayTranscript({
3781+
scenario: `${RESTART_SCENARIO}-settled-noise`,
3782+
entries: [
3783+
...codexReplayPreamble({
3784+
nativeThreadId: RESTART_NATIVE_THREAD,
3785+
nativeTurnId: RESTART_NATIVE_TURN,
3786+
prompt: RESTART_PROMPT,
3787+
}),
3788+
restartChildFrame({
3789+
label: "thread/tokenUsage/updated/settled-child",
3790+
method: "thread/tokenUsage/updated",
3791+
params: {
3792+
threadId: RESTART_CHILD_THREAD,
3793+
turnId: "native-codex-restart-settled-turn",
3794+
tokenUsage: {
3795+
total: {
3796+
totalTokens: 250,
3797+
inputTokens: 200,
3798+
cachedInputTokens: 0,
3799+
outputTokens: 50,
3800+
reasoningOutputTokens: 0,
3801+
},
3802+
last: {
3803+
totalTokens: 150,
3804+
inputTokens: 120,
3805+
cachedInputTokens: 0,
3806+
outputTokens: 30,
3807+
reasoningOutputTokens: 0,
3808+
},
3809+
},
3810+
},
3811+
}),
3812+
restartChildFrame({
3813+
label: "item/completed/settled-agent-state",
3814+
method: "item/completed",
3815+
params: {
3816+
item: {
3817+
type: "collabAgentToolCall",
3818+
id: "call-codex-restart-settled-state",
3819+
tool: "wait",
3820+
status: "completed",
3821+
senderThreadId: RESTART_NATIVE_THREAD,
3822+
receiverThreadIds: [RESTART_CHILD_THREAD],
3823+
prompt: null,
3824+
model: null,
3825+
reasoningEffort: null,
3826+
agentsStates: {
3827+
[RESTART_CHILD_THREAD]: {
3828+
status: "completed",
3829+
message: "TRAILING_SETTLED_RESULT",
3830+
},
3831+
},
3832+
},
3833+
threadId: RESTART_NATIVE_THREAD,
3834+
turnId: RESTART_NATIVE_TURN,
3835+
completedAtMs: 1782622482000,
3836+
},
3837+
}),
3838+
restartChildFrame({
3839+
label: "item/completed/root-answer",
3840+
method: "item/completed",
3841+
params: {
3842+
item: {
3843+
type: "agentMessage",
3844+
id: "root-answer-restart-settled-noise",
3845+
text: "NO_CHANGE",
3846+
phase: "final_answer",
3847+
memoryCitation: null,
3848+
},
3849+
threadId: RESTART_NATIVE_THREAD,
3850+
turnId: RESTART_NATIVE_TURN,
3851+
completedAtMs: 1782622483000,
3852+
},
3853+
}),
3854+
restartChildFrame({
3855+
label: "turn/completed/root",
3856+
method: "turn/completed",
3857+
params: {
3858+
threadId: RESTART_NATIVE_THREAD,
3859+
turn: makeCodexReplayTurn({ id: RESTART_NATIVE_TURN, status: "completed" }),
3860+
},
3861+
}),
3862+
],
3863+
});
3864+
37803865
/**
37813866
* The subagent as the projection holds it after a restart: settled at idle,
37823867
* still bound to its child thread and native item, one activation recorded.
@@ -3906,10 +3991,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
39063991
assert.equal(latest?.childThreadId, existing.childThread.id);
39073992
// Continues the prior activation rather than restarting the count.
39083993
assert.equal(latest?.activationCount, 2);
3909-
// Usage carries across the restart instead of resetting: the seeded
3910-
// 100 is not lost, and the thread-cumulative 250 is not double-counted
3911-
// on top of it.
3912-
assert.equal(latest?.usage?.totalTokens, 250);
3994+
assert.equal(latest?.usage?.totalTokens, 100);
39133995
// The new activation takes the next ordinal, not a colliding ordinal 1.
39143996
const activations = harness.subagentActivationUpdates();
39153997
assert.deepStrictEqual(
@@ -3920,6 +4002,36 @@ describe("CodexAdapterV2 post-settle continuation", () => {
39204002
[...new Set(activations.map((event) => event.activation.subagentId))],
39214003
[RESTART_SUBAGENT_ID],
39224004
);
4005+
assert.equal(activations.at(-1)?.activation.usage?.totalTokens, 150);
4006+
}).pipe(Effect.provide(Layer.merge(idAllocatorLayer, NodeServices.layer))),
4007+
),
4008+
);
4009+
4010+
it.effect("drops settled identity updates until a new activation starts", () =>
4011+
Effect.scoped(
4012+
Effect.gen(function* () {
4013+
const harness = yield* makeCodexReplayHarness(restartSettledNoiseTranscript);
4014+
const now = yield* DateTime.now;
4015+
const existing = makeRestartExistingSubagent({
4016+
parentThreadId: harness.threadId,
4017+
parentProviderThread: harness.providerThread,
4018+
now,
4019+
});
4020+
4021+
yield* harness.runtime.startTurn({
4022+
...makeCodexTestTurnInput({
4023+
threadId: harness.threadId,
4024+
providerThread: harness.providerThread,
4025+
now,
4026+
attemptId: RunAttemptId.make("attempt-codex-restart-settled-noise"),
4027+
text: RESTART_PROMPT,
4028+
}),
4029+
existingSubagents: [existing],
4030+
});
4031+
4032+
yield* awaitUntil(() => harness.terminalEvents().length === 1, "root turn terminal");
4033+
assert.lengthOf(harness.subagentUpdates(), 0);
4034+
assert.lengthOf(harness.subagentActivationUpdates(), 0);
39234035
}).pipe(Effect.provide(Layer.merge(idAllocatorLayer, NodeServices.layer))),
39244036
),
39254037
);

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,7 +2476,7 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
24762476
const subagents = yield* Ref.get(subagentThreads);
24772477
for (const [nativeThreadId, state] of Object.entries(input.item.agentsStates)) {
24782478
const subagent = subagents.get(nativeThreadId);
2479-
if (subagent === undefined) {
2479+
if (subagent === undefined || subagent.task.currentActivationId === null) {
24802480
continue;
24812481
}
24822482
yield* emitSubagentTaskUpdate({
@@ -3488,19 +3488,21 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
34883488
if (subagent === undefined || payload.tokenUsage.total.totalTokens <= 0) {
34893489
return;
34903490
}
3491-
const total = payload.tokenUsage.total;
3492-
const usage = {
3493-
totalTokens: total.totalTokens,
3494-
inputTokens: total.inputTokens,
3495-
cachedInputTokens: total.cachedInputTokens,
3496-
outputTokens: total.outputTokens,
3497-
reasoningOutputTokens: total.reasoningOutputTokens,
3498-
} satisfies OrchestrationV2SubagentUsage;
3499-
yield* emitSubagentTaskUpdate({
3500-
subagent,
3501-
status: subagent.task.status,
3502-
usage,
3503-
});
3491+
if (subagent.task.currentActivationId !== null) {
3492+
const total = payload.tokenUsage.total;
3493+
const usage = {
3494+
totalTokens: total.totalTokens,
3495+
inputTokens: total.inputTokens,
3496+
cachedInputTokens: total.cachedInputTokens,
3497+
outputTokens: total.outputTokens,
3498+
reasoningOutputTokens: total.reasoningOutputTokens,
3499+
} satisfies OrchestrationV2SubagentUsage;
3500+
yield* emitSubagentTaskUpdate({
3501+
subagent,
3502+
status: subagent.task.status,
3503+
usage,
3504+
});
3505+
}
35043506

35053507
const activeContext = (yield* Ref.get(activeTurns)).get(payload.turnId);
35063508
const activation =

0 commit comments

Comments
 (0)