Skip to content

Commit eb08085

Browse files
fix(server): keep subagent model and effort when a Claude agent resumes
Reviving a finished subagent with SendMessage re-registers the same task under the SendMessage call, which names no model or effort, so the row fell back to the session's model and effort. Re-registration now keeps what is already known for the task. Subagent effort also defaulted to the session's effort, which is wrong whenever an agent definition sets its own. The SDK reports no per-agent effort, so effort is shown only when the launch input sets it.
1 parent d2c9281 commit eb08085

2 files changed

Lines changed: 96 additions & 25 deletions

File tree

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

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3945,8 +3945,8 @@ describe("ClaudeAdapterLive", () => {
39453945
attachments: [],
39463946
});
39473947

3948-
// No explicit model/effort on the launch input: the task inherits the
3949-
// session's selection.
3948+
// No explicit model/effort on the launch input: the model starts from the
3949+
// session's selection, and effort stays unknown.
39503950
harness.query.emit({
39513951
type: "system",
39523952
subtype: "task_started",
@@ -3984,13 +3984,13 @@ describe("ClaudeAdapterLive", () => {
39843984
assert.equal(started?.type, "task.started");
39853985
if (started?.type === "task.started") {
39863986
assert.equal(started.payload.model, SYNTHETIC_CLAUDE_CAPABLE_MODEL);
3987-
assert.equal(started.payload.effort, "max");
3987+
assert.equal(started.payload.effort, undefined);
39883988
}
39893989
const progress = taskEvents[1];
39903990
assert.equal(progress?.type, "task.progress");
39913991
if (progress?.type === "task.progress") {
39923992
assert.equal(progress.payload.model, SYNTHETIC_SUBAGENT_MODEL);
3993-
assert.equal(progress.payload.effort, "max");
3993+
assert.equal(progress.payload.effort, undefined);
39943994
}
39953995
}).pipe(
39963996
Effect.provideService(Random.Random, makeDeterministicRandomService()),
@@ -4063,7 +4063,7 @@ describe("ClaudeAdapterLive", () => {
40634063
assert.equal(started?.type, "task.started");
40644064
if (started?.type === "task.started") {
40654065
assert.equal(started.payload.model, SYNTHETIC_SUBAGENT_MODEL);
4066-
assert.equal(started.payload.effort, "max");
4066+
assert.equal(started.payload.effort, undefined);
40674067
}
40684068
const progress = taskEvents[1];
40694069
assert.equal(progress?.type, "task.progress");
@@ -4076,6 +4076,84 @@ describe("ClaudeAdapterLive", () => {
40764076
);
40774077
});
40784078

4079+
it.effect("a subagent resumed by SendMessage keeps its model and effort", () => {
4080+
const harness = makeHarness();
4081+
return Effect.gen(function* () {
4082+
const adapter = yield* ClaudeAdapter;
4083+
4084+
const startedFiber = yield* adapter.streamEvents.pipe(
4085+
Stream.filter((event) => event.type === "task.started"),
4086+
Stream.take(2),
4087+
Stream.runCollect,
4088+
Effect.forkChild,
4089+
);
4090+
4091+
const session = yield* adapter.startSession({
4092+
threadId: THREAD_ID,
4093+
provider: ProviderDriverKind.make("claudeAgent"),
4094+
modelSelection: createModelSelection(
4095+
ProviderInstanceId.make("claudeAgent"),
4096+
SYNTHETIC_CLAUDE_CAPABLE_MODEL,
4097+
[{ id: "effort", value: "max" }],
4098+
),
4099+
runtimeMode: "full-access",
4100+
});
4101+
yield* adapter.sendTurn({
4102+
threadId: session.threadId,
4103+
input: "spawn an agent",
4104+
attachments: [],
4105+
});
4106+
4107+
const toolUse = (index: number, id: string, name: string, input: object) =>
4108+
harness.query.emit({
4109+
type: "stream_event",
4110+
session_id: "sdk-session",
4111+
uuid: `stream-${id}`,
4112+
parent_tool_use_id: null,
4113+
event: {
4114+
type: "content_block_start",
4115+
index,
4116+
content_block: { type: "tool_use", id, name, input },
4117+
},
4118+
} as unknown as SDKMessage);
4119+
const taskStarted = (toolUseId: string) =>
4120+
harness.query.emit({
4121+
type: "system",
4122+
subtype: "task_started",
4123+
task_id: "task-resumed",
4124+
description: "Agent R",
4125+
task_type: "local_agent",
4126+
tool_use_id: toolUseId,
4127+
uuid: `task-resumed-${toolUseId}`,
4128+
session_id: "sdk-session",
4129+
} as unknown as SDKMessage);
4130+
4131+
toolUse(0, "toolu_agent_r", "Agent", { description: "Agent R", effort: "high" });
4132+
taskStarted("toolu_agent_r");
4133+
harness.query.emit({
4134+
type: "assistant",
4135+
parent_tool_use_id: "toolu_agent_r",
4136+
message: { model: SYNTHETIC_SUBAGENT_MODEL, content: [] },
4137+
uuid: "resumed-snapshot-uuid",
4138+
session_id: "sdk-session",
4139+
} as unknown as SDKMessage);
4140+
// Reviving a finished agent re-registers the same task under the
4141+
// SendMessage call, whose input names neither model nor effort.
4142+
toolUse(1, "toolu_send_r", "SendMessage", { to: "task-resumed", message: "continue" });
4143+
taskStarted("toolu_send_r");
4144+
4145+
const resumed = Array.from(yield* Fiber.join(startedFiber))[1];
4146+
assert.equal(resumed?.type, "task.started");
4147+
if (resumed?.type === "task.started") {
4148+
assert.equal(resumed.payload.model, SYNTHETIC_SUBAGENT_MODEL);
4149+
assert.equal(resumed.payload.effort, "high");
4150+
}
4151+
}).pipe(
4152+
Effect.provideService(Random.Random, makeDeterministicRandomService()),
4153+
Effect.provide(harness.layer),
4154+
);
4155+
});
4156+
40794157
it.effect("closes the session when the Claude stream aborts after a turn starts", () => {
40804158
const harness = makeHarness();
40814159
return Effect.gen(function* () {

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,6 @@ interface ClaudeSessionContext {
417417
readonly startedAt: string;
418418
readonly basePermissionMode: PermissionMode | undefined;
419419
currentApiModelId: string | undefined;
420-
/** Effective effort for the session's turns; subagents without an explicit
421-
* effort override inherit this. */
422-
currentEffort: string | undefined;
423420
resumeSessionId: string | undefined;
424421
readonly pendingApprovals: Map<ApprovalRequestId, PendingApproval>;
425422
readonly pendingUserInputs: Map<ApprovalRequestId, PendingUserInput>;
@@ -3720,28 +3717,33 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
37203717
) {
37213718
context.turnState.hasSubagents = true;
37223719
}
3723-
// Model/effort: the Agent tool's input carries explicit overrides;
3724-
// absent ones inherit the session's selection (SDK behavior).
3725-
// Subagent assistant snapshots refine model with the authoritative API
3726-
// id: one that already arrived is buffered and outranks the seed here,
3727-
// later ones refine the record in place. AgentInput.effort may be a
3728-
// named level or an integer.
3720+
// Model/effort: the Agent tool's input carries explicit overrides.
3721+
// A resumed agent re-registers under the same task_id, launched by a
3722+
// SendMessage call that carries neither, so it keeps what is already
3723+
// known. A fresh agent's model falls back to the session's, and
3724+
// subagent assistant snapshots refine it with the authoritative API id:
3725+
// one that already arrived is buffered and outranks the seed here,
3726+
// later ones refine the record in place. Effort has no such source (an
3727+
// agent definition may set its own), so it is only reported when
3728+
// explicit. AgentInput.effort may be a named level or an integer.
37293729
const launchInput = launchingTool?.input;
37303730
const toolUseId = message.tool_use_id;
37313731
const bufferedModel = toolUseId ? context.pendingTaskModels.get(toolUseId) : undefined;
37323732
if (toolUseId) {
37333733
context.pendingTaskModels.delete(toolUseId);
37343734
}
3735+
const known = context.taskAgents.get(message.task_id);
37353736
const model =
37363737
bufferedModel ??
37373738
trimmedString(launchInput?.model) ??
3739+
known?.model ??
37383740
trimmedString(context.session.model ?? undefined);
37393741
const rawLaunchEffort = launchInput?.effort;
37403742
const effort =
37413743
trimmedString(rawLaunchEffort) ??
37423744
(typeof rawLaunchEffort === "number" && Number.isFinite(rawLaunchEffort)
37433745
? String(rawLaunchEffort)
3744-
: context.currentEffort);
3746+
: known?.effort);
37453747
// Remember the agent identity so every later task.* payload for this
37463748
// taskId is self-describing (identity must survive activity retention).
37473749
context.taskAgents.set(message.task_id, {
@@ -3752,7 +3754,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
37523754
taskType: message.task_type,
37533755
workflowName: message.workflow_name,
37543756
skipTranscript: message.skip_transcript === true,
3755-
runHandles: context.taskAgents.get(message.task_id)?.runHandles,
3757+
runHandles: known?.runHandles,
37563758
owningAgentId,
37573759
model,
37583760
effort,
@@ -5036,7 +5038,6 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
50365038
startedAt,
50375039
basePermissionMode: permissionMode,
50385040
currentApiModelId: apiModelId,
5039-
currentEffort: effectiveEffort ?? undefined,
50405041
resumeSessionId: sessionId,
50415042
pendingApprovals,
50425043
pendingUserInputs,
@@ -5172,14 +5173,6 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
51725173
...context.session,
51735174
model: modelSelection.model,
51745175
};
5175-
const turnEffort = resolveClaudeCatalogEffort(
5176-
modelCatalog,
5177-
modelSelection.model,
5178-
getModelSelectionStringOptionValue(modelSelection, "effort"),
5179-
);
5180-
context.currentEffort =
5181-
getEffectiveClaudeAgentEffort(modelCatalog, turnEffort ?? null, modelSelection.model) ??
5182-
undefined;
51835176
}
51845177

51855178
// Apply interaction mode by switching the SDK's permission mode.

0 commit comments

Comments
 (0)