Skip to content

Commit 38c6c75

Browse files
fix(server): a remote task's thread wakes its parent for a usage-limit auto-resume
A forwarded wait with afterRunId reads the stored message that started each later run, but the usage-limit continuation marker lived only on the message.dispatch command, so an automatic resume after a usage limit never counted as follow-up work and the parent missed the resumed run's result. The stored message now carries usageLimitContinuationOfRunId (an optional field in its JSON payload, so no migration); messages stored before this change lack it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ab4588b commit 38c6c75

3 files changed

Lines changed: 126 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6316,6 +6316,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
63166316
: { scheduledTaskId: command.scheduledTaskId }),
63176317
...(command.senderThreadId === undefined ? {} : { senderThreadId: command.senderThreadId }),
63186318
...(command.idleCompaction === undefined ? {} : { idleCompaction: command.idleCompaction }),
6319+
...(command.usageLimitContinuationOfRunId === undefined
6320+
? {}
6321+
: { usageLimitContinuationOfRunId: command.usageLimitContinuationOfRunId }),
63196322
id: command.messageId,
63206323
threadId: command.threadId,
63216324
runId,
@@ -6700,6 +6703,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
67006703
: { scheduledTaskId: command.scheduledTaskId }),
67016704
...(command.senderThreadId === undefined ? {} : { senderThreadId: command.senderThreadId }),
67026705
...(command.idleCompaction === undefined ? {} : { idleCompaction: command.idleCompaction }),
6706+
...(command.usageLimitContinuationOfRunId === undefined
6707+
? {}
6708+
: { usageLimitContinuationOfRunId: command.usageLimitContinuationOfRunId }),
67036709
id: command.messageId,
67046710
threadId: command.threadId,
67056711
runId,
@@ -7482,6 +7488,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
74827488
: { scheduledTaskId: command.scheduledTaskId }),
74837489
...(command.senderThreadId === undefined ? {} : { senderThreadId: command.senderThreadId }),
74847490
...(command.idleCompaction === undefined ? {} : { idleCompaction: command.idleCompaction }),
7491+
...(command.usageLimitContinuationOfRunId === undefined
7492+
? {}
7493+
: { usageLimitContinuationOfRunId: command.usageLimitContinuationOfRunId }),
74857494
id: command.messageId,
74867495
threadId: command.threadId,
74877496
runId,

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8039,6 +8039,121 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
80398039
}),
80408040
);
80418041

8042+
it.effect("a wait after a usage-limited run answers with its automatic resume", () =>
8043+
Effect.gen(function* () {
8044+
const orchestrator = yield* Orchestrator.OrchestratorV2;
8045+
const events = yield* EventSink.EventSinkV2;
8046+
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
8047+
const threadId = ThreadId.make("auto-resume-wait:thread");
8048+
const projectId = ProjectId.make("auto-resume-wait:project");
8049+
const now = yield* DateTime.now;
8050+
yield* seedProject({
8051+
projectId,
8052+
title: "Resume project",
8053+
workspaceRoot: process.cwd(),
8054+
defaultModelSelection: modelSelection,
8055+
createdAt: DateTime.formatIso(now),
8056+
});
8057+
yield* orchestrator.dispatch({
8058+
type: "thread.create",
8059+
commandId: CommandId.make("auto-resume-wait:create"),
8060+
threadId,
8061+
projectId,
8062+
title: "Limited thread",
8063+
modelSelection,
8064+
runtimeMode: "full-access",
8065+
interactionMode: "default",
8066+
branch: null,
8067+
worktreePath: null,
8068+
createdBy: "user",
8069+
creationSource: "web",
8070+
});
8071+
yield* orchestrator.dispatch({
8072+
type: "message.dispatch",
8073+
commandId: CommandId.make("auto-resume-wait:start"),
8074+
threadId,
8075+
messageId: MessageId.make("auto-resume-wait:start"),
8076+
text: "Start work.",
8077+
attachments: [],
8078+
dispatchMode: { type: "defer_start" },
8079+
createdBy: "user",
8080+
creationSource: "web",
8081+
});
8082+
const source = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
8083+
const resetAt = DateTime.formatIso(DateTime.add(now, { minutes: 1 }));
8084+
yield* events.write({
8085+
events: [
8086+
{
8087+
id: EventId.make("auto-resume-wait:failed"),
8088+
type: "run.updated",
8089+
threadId,
8090+
occurredAt: now,
8091+
payload: { ...source, status: "failed", completedAt: now },
8092+
},
8093+
{
8094+
id: EventId.make("auto-resume-wait:error"),
8095+
type: "turn-item.updated",
8096+
threadId,
8097+
occurredAt: now,
8098+
payload: {
8099+
id: TurnItemId.make("auto-resume-wait:error"),
8100+
type: "error",
8101+
threadId,
8102+
runId: source.id,
8103+
nodeId: source.rootNodeId,
8104+
providerThreadId: null,
8105+
providerTurnId: null,
8106+
nativeItemRef: null,
8107+
parentItemId: null,
8108+
ordinal: 2,
8109+
status: "failed",
8110+
title: "Usage limit reached",
8111+
startedAt: now,
8112+
completedAt: now,
8113+
updatedAt: now,
8114+
failure: {
8115+
class: "usage_limit",
8116+
message: "Plan limit reached.",
8117+
code: "usageLimitExceeded",
8118+
retryable: null,
8119+
resetAt,
8120+
},
8121+
},
8122+
},
8123+
],
8124+
});
8125+
const shell = (yield* orchestrator.getShellSnapshot()).threads.find(
8126+
(thread) => thread.id === threadId,
8127+
)!;
8128+
yield* orchestrator.dispatch(limitRecoveryCommand(shell, true, DateTime.toEpochMillis(now))!);
8129+
const armed = (yield* orchestrator.getShellSnapshot()).threads.find(
8130+
(thread) => thread.id === threadId,
8131+
)!;
8132+
yield* TestClock.adjust("1 minute");
8133+
yield* orchestrator.dispatch(limitRecoveryCommand(armed, true, Date.parse(resetAt))!);
8134+
const resumed = (yield* orchestrator.getThreadProjection(threadId)).runs[1]!;
8135+
const resumedAt = yield* DateTime.now;
8136+
yield* events.write({
8137+
events: [
8138+
{
8139+
id: EventId.make("auto-resume-wait:running"),
8140+
type: "run.updated",
8141+
threadId,
8142+
occurredAt: resumedAt,
8143+
payload: { ...resumed, status: "running", startedAt: resumedAt },
8144+
},
8145+
],
8146+
});
8147+
8148+
const wait = yield* threadManagement
8149+
.waitForThread({ projectId, threadId, afterRunId: source.id, timeoutMs: 1_000 })
8150+
.pipe(Effect.forkChild);
8151+
yield* TestClock.adjust("1 second");
8152+
const result = yield* Fiber.join(wait);
8153+
assert.deepEqual([result.timedOut, result.run?.id], [false, resumed.id]);
8154+
}),
8155+
);
8156+
80428157
it.effect.each([
80438158
"resume",
80448159
"queued-resume",

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,8 @@ export const OrchestrationV2ConversationMessage = Schema.Struct({
13041304
scheduledTaskId: Schema.optional(ScheduledTaskId),
13051305
senderThreadId: Schema.optional(ThreadId),
13061306
idleCompaction: Schema.optional(OrchestrationV2IdleCompaction),
1307+
/** The usage-limited run this message automatically resumes. */
1308+
usageLimitContinuationOfRunId: Schema.optional(RunId),
13071309
/**
13081310
* The message is the script a provider-native workflow run executes, not prose. Clients show
13091311
* the workflow's opening instead and keep the script behind a disclosure.

0 commit comments

Comments
 (0)