fix(server): keep the authoritative subagent model when snapshots race task_started (#7583)
This commit is contained in:
1 parent
a00218741f
commit
6a2608292d
2 files changed
+136
-6
No files matched your search
@@ -1997,6 +1997,84 @@ describe("ClaudeAdapterLive", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("a subagent snapshot that beats task_started still wins over the seed", () => {
|
||||
const harness = makeHarness();
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* ClaudeAdapter;
|
||||
|
||||
const taskEventsFiber = yield* adapter.streamEvents.pipe(
|
||||
Stream.filter((event) => event.type.startsWith("task.")),
|
||||
Stream.take(2),
|
||||
Stream.runCollect,
|
||||
Effect.forkChild,
|
||||
);
|
||||
|
||||
const session = yield* adapter.startSession({
|
||||
threadId: THREAD_ID,
|
||||
provider: ProviderDriverKind.make("claudeAgent"),
|
||||
modelSelection: createModelSelection(
|
||||
ProviderInstanceId.make("claudeAgent"),
|
||||
"claude-opus-4-6",
|
||||
[{ id: "effort", value: "max" }],
|
||||
),
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
yield* adapter.sendTurn({
|
||||
threadId: session.threadId,
|
||||
input: "spawn an agent",
|
||||
attachments: [],
|
||||
});
|
||||
|
||||
// The subagent streams its first assistant snapshot before the task is
|
||||
// registered, so there is no agent to refine yet.
|
||||
harness.query.emit({
|
||||
type: "assistant",
|
||||
parent_tool_use_id: "toolu_agent_early",
|
||||
message: {
|
||||
model: "claude-sonnet-5[1m]",
|
||||
content: [],
|
||||
},
|
||||
uuid: "early-snapshot-uuid",
|
||||
session_id: "sdk-session",
|
||||
} as unknown as SDKMessage);
|
||||
harness.query.emit({
|
||||
type: "system",
|
||||
subtype: "task_started",
|
||||
task_id: "task-early",
|
||||
description: "Agent E",
|
||||
task_type: "local_agent",
|
||||
tool_use_id: "toolu_agent_early",
|
||||
uuid: "task-early-uuid",
|
||||
session_id: "sdk-session",
|
||||
} as unknown as SDKMessage);
|
||||
harness.query.emit({
|
||||
type: "system",
|
||||
subtype: "task_progress",
|
||||
task_id: "task-early",
|
||||
description: "Agent E",
|
||||
usage: { total_tokens: 100, tool_uses: 1, duration_ms: 10 },
|
||||
uuid: "task-early-progress-uuid",
|
||||
session_id: "sdk-session",
|
||||
} as unknown as SDKMessage);
|
||||
|
||||
const taskEvents = Array.from(yield* Fiber.join(taskEventsFiber));
|
||||
const started = taskEvents[0];
|
||||
assert.equal(started?.type, "task.started");
|
||||
if (started?.type === "task.started") {
|
||||
assert.equal(started.payload.model, "claude-sonnet-5[1m]");
|
||||
assert.equal(started.payload.effort, "max");
|
||||
}
|
||||
const progress = taskEvents[1];
|
||||
assert.equal(progress?.type, "task.progress");
|
||||
if (progress?.type === "task.progress") {
|
||||
assert.equal(progress.payload.model, "claude-sonnet-5[1m]");
|
||||
}
|
||||
}).pipe(
|
||||
Effect.provideService(Random.Random, makeDeterministicRandomService()),
|
||||
Effect.provide(harness.layer),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("closes the session when the Claude stream aborts after a turn starts", () => {
|
||||
const harness = makeHarness();
|
||||
return Effect.gen(function* () {
|
||||
|
||||
@@ -241,6 +241,32 @@ interface ClaudeTaskAgentState {
|
||||
effort: string | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* How many racing snapshot models to buffer per session. A snapshot whose
|
||||
* task_started never arrives would otherwise pin its entry for the session's
|
||||
* lifetime; oldest entries evict first.
|
||||
*/
|
||||
const PENDING_TASK_MODEL_CAP = 64;
|
||||
|
||||
/**
|
||||
* Buffers a subagent snapshot's authoritative model under its
|
||||
* parent_tool_use_id, for snapshots that beat their task_started to the
|
||||
* stream. task_started consumes the entry when it registers the task.
|
||||
*/
|
||||
function rememberPendingTaskModel(
|
||||
pending: Map<string, string>,
|
||||
parentToolUseId: string,
|
||||
model: string,
|
||||
): void {
|
||||
pending.set(parentToolUseId, model);
|
||||
if (pending.size > PENDING_TASK_MODEL_CAP) {
|
||||
const oldest = pending.keys().next();
|
||||
if (!oldest.done) {
|
||||
pending.delete(oldest.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface ClaudeSessionContext {
|
||||
session: ProviderSession;
|
||||
readonly promptQueue: Queue.Queue<PromptQueueItem>;
|
||||
@@ -262,6 +288,12 @@ interface ClaudeSessionContext {
|
||||
readonly inFlightTools: Map<number, ToolInFlight>;
|
||||
readonly claudeTasks: Map<string, ClaudeTaskState>;
|
||||
readonly taskAgents: Map<string, ClaudeTaskAgentState>;
|
||||
/**
|
||||
* Authoritative subagent models from assistant snapshots that arrived before
|
||||
* their task_started registered the task, keyed by parent_tool_use_id.
|
||||
* Written through `rememberPendingTaskModel`, consumed by task_started.
|
||||
*/
|
||||
readonly pendingTaskModels: Map<string, string>;
|
||||
/**
|
||||
* Last emitted workflow-member fingerprint per member slot. A coordinator
|
||||
* task_progress repeats the FULL member array every tick; without a
|
||||
@@ -2875,8 +2907,18 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
const owningTaskId = agentIdForParentToolUse(context.taskAgents, assistantParentToolUseId);
|
||||
const snapshotModel = trimmedString(message.message.model);
|
||||
const owningAgent = owningTaskId ? context.taskAgents.get(owningTaskId) : undefined;
|
||||
if (owningAgent && snapshotModel) {
|
||||
owningAgent.model = snapshotModel;
|
||||
if (snapshotModel) {
|
||||
if (owningAgent) {
|
||||
owningAgent.model = snapshotModel;
|
||||
} else {
|
||||
// The snapshot beat its task_started (or its tool_use_id was never
|
||||
// recorded): hold the model until the task registers.
|
||||
rememberPendingTaskModel(
|
||||
context.pendingTaskModels,
|
||||
assistantParentToolUseId,
|
||||
snapshotModel,
|
||||
);
|
||||
}
|
||||
}
|
||||
context.lastAssistantUuid = message.uuid;
|
||||
yield* updateResumeCursor(context);
|
||||
@@ -3190,12 +3232,20 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
const owningAgentId = launchingTool?.agentId;
|
||||
// Model/effort: the Agent tool's input carries explicit overrides;
|
||||
// absent ones inherit the session's selection (SDK behavior).
|
||||
// Subagent assistant snapshots later refine model with the
|
||||
// authoritative API id. AgentInput.effort may be a named level or an
|
||||
// integer.
|
||||
// Subagent assistant snapshots refine model with the authoritative API
|
||||
// id: one that already arrived is buffered and outranks the seed here,
|
||||
// later ones refine the record in place. AgentInput.effort may be a
|
||||
// named level or an integer.
|
||||
const launchInput = launchingTool?.input;
|
||||
const toolUseId = message.tool_use_id;
|
||||
const bufferedModel = toolUseId ? context.pendingTaskModels.get(toolUseId) : undefined;
|
||||
if (toolUseId) {
|
||||
context.pendingTaskModels.delete(toolUseId);
|
||||
}
|
||||
const model =
|
||||
trimmedString(launchInput?.model) ?? trimmedString(context.session.model ?? undefined);
|
||||
bufferedModel ??
|
||||
trimmedString(launchInput?.model) ??
|
||||
trimmedString(context.session.model ?? undefined);
|
||||
const rawLaunchEffort = launchInput?.effort;
|
||||
const effort =
|
||||
trimmedString(rawLaunchEffort) ??
|
||||
@@ -3807,6 +3857,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
const inFlightTools = new Map<number, ToolInFlight>();
|
||||
const claudeTasks = new Map<string, ClaudeTaskState>();
|
||||
const taskAgents = new Map<string, ClaudeTaskAgentState>();
|
||||
const pendingTaskModels = new Map<string, string>();
|
||||
const workflowMemberFingerprints = new Map<string, string>();
|
||||
const liveTaskIds = new Set<string>();
|
||||
|
||||
@@ -4270,6 +4321,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
inFlightTools,
|
||||
claudeTasks,
|
||||
taskAgents,
|
||||
pendingTaskModels,
|
||||
workflowMemberFingerprints,
|
||||
liveTaskIds,
|
||||
turnState: undefined,
|
||||
|
||||
Reference in new issue
Block a user