Skip to content

Commit 9eb7e93

Browse files
committed
fix(claude): persist completed subagent state
1 parent 23ccd0c commit 9eb7e93

5 files changed

Lines changed: 124 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4835,6 +4835,7 @@ describe("ClaudeAdapterLive", () => {
48354835
const subagentStopHook = hooks?.SubagentStop?.[0]?.hooks[0];
48364836
const preToolUseHook = hooks?.PreToolUse?.[0]?.hooks[0];
48374837
assert.equal(hooks?.PreToolUse?.[0]?.matcher, "SendMessage");
4838+
assert.equal(hooks?.PreToolUse?.[0]?.timeout, 60 * 60 * 24 * 7);
48384839
assert.equal(typeof subagentStartHook, "function");
48394840
assert.equal(typeof subagentStopHook, "function");
48404841
assert.equal(typeof preToolUseHook, "function");

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,7 @@ function trimmedString(value: unknown): string | undefined {
10031003
const CLAUDE_START_FRESH_AGENT_ANSWER = "Start fresh agent";
10041004
const CLAUDE_RESUME_EXISTING_AGENT_ANSWER = "Resume existing agent";
10051005
const CLAUDE_STOPPED_SUBAGENT_CAP = 128;
1006+
const CLAUDE_INTERACTIVE_HOOK_TIMEOUT_SECONDS = 60 * 60 * 24 * 7;
10061007

10071008
function recordFromUnknown(value: unknown): Record<string, unknown> | undefined {
10081009
return typeof value === "object" && value !== null && !Array.isArray(value)
@@ -1879,6 +1880,29 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
18791880
};
18801881
});
18811882

1883+
const emitResumeCursorUpdate = Effect.fn("emitResumeCursorUpdate")(function* (
1884+
context: ClaudeSessionContext,
1885+
) {
1886+
yield* updateResumeCursor(context);
1887+
if (context.session.resumeCursor === undefined) {
1888+
return;
1889+
}
1890+
1891+
const configuredStamp = yield* makeEventStamp();
1892+
yield* offerRuntimeEvent({
1893+
type: "session.configured",
1894+
eventId: configuredStamp.eventId,
1895+
provider: PROVIDER,
1896+
createdAt: configuredStamp.createdAt,
1897+
threadId: context.session.threadId,
1898+
payload: {
1899+
config: {},
1900+
resumeCursor: context.session.resumeCursor,
1901+
},
1902+
providerRefs: {},
1903+
});
1904+
});
1905+
18821906
const ensureAssistantTextBlock = Effect.fn("ensureAssistantTextBlock")(function* (
18831907
context: ClaudeSessionContext,
18841908
blockIndex: number,
@@ -4152,7 +4176,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
41524176
}
41534177
const context = yield* Ref.get(contextRef);
41544178
if (context?.stoppedSubagents.delete(hookInput.agent_id)) {
4155-
yield* updateResumeCursor(context);
4179+
yield* emitResumeCursorUpdate(context);
41564180
}
41574181
return {};
41584182
});
@@ -4169,7 +4193,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
41694193
agentId: hookInput.agent_id,
41704194
agentType: trimmedString(hookInput.agent_type),
41714195
});
4172-
yield* updateResumeCursor(context);
4196+
yield* emitResumeCursorUpdate(context);
41734197
}
41744198
return {};
41754199
});
@@ -4249,7 +4273,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
42494273
: undefined;
42504274
if (selection === CLAUDE_RESUME_EXISTING_AGENT_ANSWER) {
42514275
context.stoppedSubagents.delete(stoppedSubagent.agentId);
4252-
yield* updateResumeCursor(context);
4276+
yield* emitResumeCursorUpdate(context);
42534277
return {
42544278
hookSpecificOutput: {
42554279
hookEventName: "PreToolUse" as const,
@@ -4537,7 +4561,17 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
45374561
hooks: {
45384562
SubagentStart: [{ hooks: [subagentStartHook] }],
45394563
SubagentStop: [{ hooks: [subagentStopHook] }],
4540-
PreToolUse: [{ matcher: "SendMessage", hooks: [completedSubagentMessageHook] }],
4564+
PreToolUse: [
4565+
{
4566+
matcher: "SendMessage",
4567+
hooks: [completedSubagentMessageHook],
4568+
// The callback waits on T3's cross-client user-input flow. The
4569+
// turn abort signal cancels it when the session closes; this
4570+
// longer backstop keeps an idle UI from silently allowing the
4571+
// SDK's ten-minute default to cancel the guard first.
4572+
timeout: CLAUDE_INTERACTIVE_HOOK_TIMEOUT_SECONDS,
4573+
},
4574+
],
45414575
},
45424576
onUserDialog,
45434577
supportedDialogKinds: ["resume_return"],

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,55 @@ routing.layer("ProviderServiceLive routing", (it) => {
15331533
}),
15341534
);
15351535

1536+
it.effect("persists resume cursor updates emitted after sendTurn returns", () =>
1537+
Effect.gen(function* () {
1538+
const provider = yield* ProviderService.ProviderService;
1539+
const runtimeRepository = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
1540+
const threadId = asThreadId("thread-runtime-resume-cursor");
1541+
const session = yield* provider.startSession(threadId, {
1542+
provider: ProviderDriverKind.make("claudeAgent"),
1543+
providerInstanceId: claudeAgentInstanceId,
1544+
threadId,
1545+
runtimeMode: "full-access",
1546+
});
1547+
yield* provider.sendTurn({
1548+
threadId: session.threadId,
1549+
input: "delegate this",
1550+
attachments: [],
1551+
});
1552+
1553+
const updatedResumeCursor = {
1554+
resume: "550e8400-e29b-41d4-a716-446655440000",
1555+
stoppedSubagents: [{ agentId: "agent-complete", agentType: "Explore" }],
1556+
};
1557+
const eventConsumer = yield* Stream.runHead(provider.streamEvents).pipe(Effect.forkChild);
1558+
yield* advanceTestClock(50);
1559+
routing.claude.emit({
1560+
type: "session.configured",
1561+
eventId: asEventId("event-resume-cursor-updated"),
1562+
provider: ProviderDriverKind.make("claudeAgent"),
1563+
threadId,
1564+
createdAt: "2026-09-01T00:00:00.000Z",
1565+
payload: { config: {}, resumeCursor: updatedResumeCursor },
1566+
});
1567+
1568+
yield* Fiber.join(eventConsumer);
1569+
const persisted = yield* runtimeRepository.getByThreadId({ threadId });
1570+
assert.equal(Option.isSome(persisted), true);
1571+
if (Option.isSome(persisted)) {
1572+
assert.deepEqual(persisted.value.resumeCursor, updatedResumeCursor);
1573+
assert.deepEqual(persisted.value.runtimePayload, {
1574+
cwd: session.cwd,
1575+
model: null,
1576+
activeTurnId: `turn-${String(threadId)}`,
1577+
lastError: null,
1578+
lastRuntimeEvent: "session.resume-cursor.updated",
1579+
lastRuntimeEventAt: "2026-09-01T00:00:00.000Z",
1580+
});
1581+
}
1582+
}),
1583+
);
1584+
15361585
it.effect("does not persist running after a concurrent send is interrupted", () =>
15371586
Effect.gen(function* () {
15381587
const provider = yield* ProviderService.ProviderService;

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,43 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
345345
event: ProviderRuntimeEvent,
346346
): Effect.Effect<void> =>
347347
Effect.sync(() => correlateRuntimeEventWithInstance(source, event)).pipe(
348-
Effect.flatMap((canonicalEvent) =>
349-
increment(providerRuntimeEventsTotal, {
348+
Effect.flatMap((canonicalEvent) => {
349+
const resumeCursor =
350+
canonicalEvent.type === "session.configured"
351+
? canonicalEvent.payload.resumeCursor
352+
: undefined;
353+
const resumeCursorPersistence =
354+
resumeCursor === undefined
355+
? Effect.void
356+
: directory
357+
.upsert({
358+
threadId: canonicalEvent.threadId,
359+
provider: canonicalEvent.provider,
360+
providerInstanceId: source.instanceId,
361+
resumeCursor,
362+
runtimePayload: {
363+
lastRuntimeEvent: "session.resume-cursor.updated",
364+
lastRuntimeEventAt: canonicalEvent.createdAt,
365+
},
366+
})
367+
.pipe(
368+
Effect.catchCause((cause) =>
369+
Effect.logWarning("provider.session.resume-cursor-persist-failed", {
370+
threadId: canonicalEvent.threadId,
371+
provider: canonicalEvent.provider,
372+
cause,
373+
}),
374+
),
375+
);
376+
377+
return increment(providerRuntimeEventsTotal, {
350378
provider: canonicalEvent.provider,
351379
eventType: canonicalEvent.type,
352-
}).pipe(Effect.andThen(publishRuntimeEvent(canonicalEvent))),
353-
),
380+
}).pipe(
381+
Effect.andThen(resumeCursorPersistence),
382+
Effect.andThen(publishRuntimeEvent(canonicalEvent)),
383+
);
384+
}),
354385
);
355386

356387
// `subscribedAdapters` is our source-of-truth for "which instance adapters

‎packages/contracts/src/providerRuntime.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ export type SessionStartedPayload = typeof SessionStartedPayload.Type;
274274

275275
const SessionConfiguredPayload = Schema.Struct({
276276
config: UnknownRecordSchema,
277+
resumeCursor: Schema.optional(Schema.Unknown),
277278
});
278279
export type SessionConfiguredPayload = typeof SessionConfiguredPayload.Type;
279280

0 commit comments

Comments
 (0)