Skip to content

Commit 1f89f52

Browse files
fix(server): keep the authoritative subagent model when snapshots race task_started
The agent panel sometimes labelled a spawned Claude subagent with the session's model instead of the one it ran on. task_started seeds the task's model from the launching Agent tool's parsed input and falls back to the session model, and the only correction was the subagent's own assistant snapshot, whose authoritative message.model was dropped whenever the snapshot arrived before the task was registered. Snapshots that cannot resolve their parent_tool_use_id to a registered task now park their model in a small bounded per-session map, and task_started prefers that buffered value over the launch-time seed. Later snapshots still refine the record in place, so the corrected model rides every subsequent linkage row.
1 parent 4347f14 commit 1f89f52

2 files changed

Lines changed: 136 additions & 6 deletions

File tree

‎apps/server/src/provider/Layers/ClaudeAdapter.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,84 @@ describe("ClaudeAdapterLive", () => {
18341834
);
18351835
});
18361836

1837+
it.effect("a subagent snapshot that beats task_started still wins over the seed", () => {
1838+
const harness = makeHarness();
1839+
return Effect.gen(function* () {
1840+
const adapter = yield* ClaudeAdapter;
1841+
1842+
const taskEventsFiber = yield* adapter.streamEvents.pipe(
1843+
Stream.filter((event) => event.type.startsWith("task.")),
1844+
Stream.take(2),
1845+
Stream.runCollect,
1846+
Effect.forkChild,
1847+
);
1848+
1849+
const session = yield* adapter.startSession({
1850+
threadId: THREAD_ID,
1851+
provider: ProviderDriverKind.make("claudeAgent"),
1852+
modelSelection: createModelSelection(
1853+
ProviderInstanceId.make("claudeAgent"),
1854+
"claude-opus-4-6",
1855+
[{ id: "effort", value: "max" }],
1856+
),
1857+
runtimeMode: "full-access",
1858+
});
1859+
yield* adapter.sendTurn({
1860+
threadId: session.threadId,
1861+
input: "spawn an agent",
1862+
attachments: [],
1863+
});
1864+
1865+
// The subagent streams its first assistant snapshot before the task is
1866+
// registered, so there is no agent to refine yet.
1867+
harness.query.emit({
1868+
type: "assistant",
1869+
parent_tool_use_id: "toolu_agent_early",
1870+
message: {
1871+
model: "claude-sonnet-5[1m]",
1872+
content: [],
1873+
},
1874+
uuid: "early-snapshot-uuid",
1875+
session_id: "sdk-session",
1876+
} as unknown as SDKMessage);
1877+
harness.query.emit({
1878+
type: "system",
1879+
subtype: "task_started",
1880+
task_id: "task-early",
1881+
description: "Agent E",
1882+
task_type: "local_agent",
1883+
tool_use_id: "toolu_agent_early",
1884+
uuid: "task-early-uuid",
1885+
session_id: "sdk-session",
1886+
} as unknown as SDKMessage);
1887+
harness.query.emit({
1888+
type: "system",
1889+
subtype: "task_progress",
1890+
task_id: "task-early",
1891+
description: "Agent E",
1892+
usage: { total_tokens: 100, tool_uses: 1, duration_ms: 10 },
1893+
uuid: "task-early-progress-uuid",
1894+
session_id: "sdk-session",
1895+
} as unknown as SDKMessage);
1896+
1897+
const taskEvents = Array.from(yield* Fiber.join(taskEventsFiber));
1898+
const started = taskEvents[0];
1899+
assert.equal(started?.type, "task.started");
1900+
if (started?.type === "task.started") {
1901+
assert.equal(started.payload.model, "claude-sonnet-5[1m]");
1902+
assert.equal(started.payload.effort, "max");
1903+
}
1904+
const progress = taskEvents[1];
1905+
assert.equal(progress?.type, "task.progress");
1906+
if (progress?.type === "task.progress") {
1907+
assert.equal(progress.payload.model, "claude-sonnet-5[1m]");
1908+
}
1909+
}).pipe(
1910+
Effect.provideService(Random.Random, makeDeterministicRandomService()),
1911+
Effect.provide(harness.layer),
1912+
);
1913+
});
1914+
18371915
it.effect("closes the session when the Claude stream aborts after a turn starts", () => {
18381916
const harness = makeHarness();
18391917
return Effect.gen(function* () {

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,32 @@ interface ClaudeTaskAgentState {
241241
effort: string | undefined;
242242
}
243243

244+
/**
245+
* How many racing snapshot models to buffer per session. A snapshot whose
246+
* task_started never arrives would otherwise pin its entry for the session's
247+
* lifetime; oldest entries evict first.
248+
*/
249+
const PENDING_TASK_MODEL_CAP = 64;
250+
251+
/**
252+
* Buffers a subagent snapshot's authoritative model under its
253+
* parent_tool_use_id, for snapshots that beat their task_started to the
254+
* stream. task_started consumes the entry when it registers the task.
255+
*/
256+
function rememberPendingTaskModel(
257+
pending: Map<string, string>,
258+
parentToolUseId: string,
259+
model: string,
260+
): void {
261+
pending.set(parentToolUseId, model);
262+
if (pending.size > PENDING_TASK_MODEL_CAP) {
263+
const oldest = pending.keys().next();
264+
if (!oldest.done) {
265+
pending.delete(oldest.value);
266+
}
267+
}
268+
}
269+
244270
interface ClaudeSessionContext {
245271
session: ProviderSession;
246272
readonly promptQueue: Queue.Queue<PromptQueueItem>;
@@ -262,6 +288,12 @@ interface ClaudeSessionContext {
262288
readonly inFlightTools: Map<number, ToolInFlight>;
263289
readonly claudeTasks: Map<string, ClaudeTaskState>;
264290
readonly taskAgents: Map<string, ClaudeTaskAgentState>;
291+
/**
292+
* Authoritative subagent models from assistant snapshots that arrived before
293+
* their task_started registered the task, keyed by parent_tool_use_id.
294+
* Written through `rememberPendingTaskModel`, consumed by task_started.
295+
*/
296+
readonly pendingTaskModels: Map<string, string>;
265297
/**
266298
* Last emitted workflow-member fingerprint per member slot. A coordinator
267299
* task_progress repeats the FULL member array every tick; without a
@@ -2878,8 +2910,18 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
28782910
const owningTaskId = agentIdForParentToolUse(context.taskAgents, assistantParentToolUseId);
28792911
const snapshotModel = trimmedString(message.message.model);
28802912
const owningAgent = owningTaskId ? context.taskAgents.get(owningTaskId) : undefined;
2881-
if (owningAgent && snapshotModel) {
2882-
owningAgent.model = snapshotModel;
2913+
if (snapshotModel) {
2914+
if (owningAgent) {
2915+
owningAgent.model = snapshotModel;
2916+
} else {
2917+
// The snapshot beat its task_started (or its tool_use_id was never
2918+
// recorded): hold the model until the task registers.
2919+
rememberPendingTaskModel(
2920+
context.pendingTaskModels,
2921+
assistantParentToolUseId,
2922+
snapshotModel,
2923+
);
2924+
}
28832925
}
28842926
context.lastAssistantUuid = message.uuid;
28852927
yield* updateResumeCursor(context);
@@ -3193,12 +3235,20 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
31933235
const owningAgentId = launchingTool?.agentId;
31943236
// Model/effort: the Agent tool's input carries explicit overrides;
31953237
// absent ones inherit the session's selection (SDK behavior).
3196-
// Subagent assistant snapshots later refine model with the
3197-
// authoritative API id. AgentInput.effort may be a named level or an
3198-
// integer.
3238+
// Subagent assistant snapshots refine model with the authoritative API
3239+
// id: one that already arrived is buffered and outranks the seed here,
3240+
// later ones refine the record in place. AgentInput.effort may be a
3241+
// named level or an integer.
31993242
const launchInput = launchingTool?.input;
3243+
const toolUseId = message.tool_use_id;
3244+
const bufferedModel = toolUseId ? context.pendingTaskModels.get(toolUseId) : undefined;
3245+
if (toolUseId) {
3246+
context.pendingTaskModels.delete(toolUseId);
3247+
}
32003248
const model =
3201-
trimmedString(launchInput?.model) ?? trimmedString(context.session.model ?? undefined);
3249+
bufferedModel ??
3250+
trimmedString(launchInput?.model) ??
3251+
trimmedString(context.session.model ?? undefined);
32023252
const rawLaunchEffort = launchInput?.effort;
32033253
const effort =
32043254
trimmedString(rawLaunchEffort) ??
@@ -3803,6 +3853,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
38033853
const inFlightTools = new Map<number, ToolInFlight>();
38043854
const claudeTasks = new Map<string, ClaudeTaskState>();
38053855
const taskAgents = new Map<string, ClaudeTaskAgentState>();
3856+
const pendingTaskModels = new Map<string, string>();
38063857
const workflowMemberFingerprints = new Map<string, string>();
38073858
const liveTaskIds = new Set<string>();
38083859

@@ -4266,6 +4317,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
42664317
inFlightTools,
42674318
claudeTasks,
42684319
taskAgents,
4320+
pendingTaskModels,
42694321
workflowMemberFingerprints,
42704322
liveTaskIds,
42714323
turnState: undefined,

0 commit comments

Comments
 (0)