Skip to content

Commit 003df33

Browse files
fix(server): usage-limit auto-resume no longer arms children of settled parents
The limit-recovery sweep only checked a thread's own settlement, so turning on "Resume after usage limits" armed and resumed abandoned subagent threads of settled parents in a burst, and their completion wakes reactivated those parents. The fresh-arm branch of the candidate query now skips a subagent whose direct parent is settled. A recovery already armed for the child's latest run still resumes at its reset, and unsettling the parent makes the child eligible again on the next sweep. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8f558db commit 003df33

2 files changed

Lines changed: 74 additions & 27 deletions

File tree

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

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4677,6 +4677,38 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
46774677
const store = yield* ProjectionStore.ProjectionStoreV2;
46784678
const now = yield* DateTime.now;
46794679
const resetAt = DateTime.formatIso(DateTime.add(now, { hours: 1 }));
4680+
const threadPayload = (
4681+
threadId: ThreadId,
4682+
parentThreadId: ThreadId | null,
4683+
settledOverride: "settled" | null,
4684+
) =>
4685+
({
4686+
createdBy: "user",
4687+
creationSource: "web",
4688+
id: threadId,
4689+
projectId: ProjectId.make("project:settled-parent-limit"),
4690+
title: "Settled parent limit recovery",
4691+
providerInstanceId,
4692+
modelSelection,
4693+
runtimeMode: "full-access",
4694+
interactionMode: "default",
4695+
branch: null,
4696+
worktreePath: null,
4697+
activeProviderThreadId: null,
4698+
lineage: {
4699+
parentThreadId,
4700+
relationshipToParent: parentThreadId === null ? null : "subagent",
4701+
rootThreadId: parentThreadId ?? threadId,
4702+
},
4703+
forkedFrom: null,
4704+
createdAt: now,
4705+
updatedAt: now,
4706+
archivedAt: null,
4707+
settledOverride,
4708+
settledAt: settledOverride === null ? null : now,
4709+
lastVisitedAt: null,
4710+
deletedAt: null,
4711+
}) as const;
46804712
const createThread = (
46814713
threadId: ThreadId,
46824714
parentThreadId: ThreadId | null,
@@ -4687,33 +4719,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
46874719
type: "thread.created",
46884720
threadId,
46894721
occurredAt: now,
4690-
payload: {
4691-
createdBy: "user",
4692-
creationSource: "web",
4693-
id: threadId,
4694-
projectId: ProjectId.make("project:settled-parent-limit"),
4695-
title: "Settled parent limit recovery",
4696-
providerInstanceId,
4697-
modelSelection,
4698-
runtimeMode: "full-access",
4699-
interactionMode: "default",
4700-
branch: null,
4701-
worktreePath: null,
4702-
activeProviderThreadId: null,
4703-
lineage: {
4704-
parentThreadId,
4705-
relationshipToParent: parentThreadId === null ? null : "subagent",
4706-
rootThreadId: parentThreadId ?? threadId,
4707-
},
4708-
forkedFrom: null,
4709-
createdAt: now,
4710-
updatedAt: now,
4711-
archivedAt: null,
4712-
settledOverride,
4713-
settledAt: settledOverride === null ? null : now,
4714-
lastVisitedAt: null,
4715-
deletedAt: null,
4716-
},
4722+
payload: threadPayload(threadId, parentThreadId, settledOverride),
47174723
});
47184724
const addLimitedChild = Effect.fnUntraced(function* (name: string, parentSettled: boolean) {
47194725
const parentId = ThreadId.make(`thread:settled-parent-limit:${name}:parent`);
@@ -4806,6 +4812,40 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
48064812
`only the child of the unsettled parent is armed (${JSON.stringify(options)})`,
48074813
);
48084814
}
4815+
// A recovery armed before the parent settled still resumes at its reset.
4816+
yield* store.apply({
4817+
id: EventId.make("event:settled-parent-limit:settled:armed"),
4818+
type: "thread.metadata-updated",
4819+
threadId: settledParentChild,
4820+
occurredAt: now,
4821+
payload: {
4822+
...threadPayload(
4823+
settledParentChild,
4824+
ThreadId.make("thread:settled-parent-limit:settled:parent"),
4825+
null,
4826+
),
4827+
limitRecovery: {
4828+
runId: RunId.make("run:settled-parent-limit:settled"),
4829+
resetAt,
4830+
autoResume: true,
4831+
},
4832+
},
4833+
});
4834+
const afterReset = DateTime.add(now, { hours: 2 });
4835+
const resumed = (yield* store.getLimitRecoveryCandidates({
4836+
now: afterReset,
4837+
autoResume: true,
4838+
snooze: false,
4839+
})).find((row) => row.id === settledParentChild);
4840+
assert.isDefined(resumed);
4841+
assert.isTrue(
4842+
limitRecoveryCommand(
4843+
resumed,
4844+
true,
4845+
DateTime.toEpochMillis(afterReset),
4846+
false,
4847+
)?.commandId.startsWith("limit-resume:"),
4848+
);
48094849
}),
48104850
);
48114851

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3872,6 +3872,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
38723872
${booleanInt(options.autoResume)}
38733873
OR (${booleanInt(options.snooze)} AND julianday(json_extract(item.payload_json, '$.failure.resetAt')) > julianday(${DateTime.formatIso(options.now)}))
38743874
)
3875+
-- A subagent of a settled parent is abandoned work; resuming it would wake the parent.
3876+
AND NOT EXISTS (
3877+
SELECT 1 FROM orchestration_v2_projection_threads parent
3878+
WHERE json_extract(t.payload_json, '$.lineage.relationshipToParent') = 'subagent'
3879+
AND parent.thread_id = json_extract(t.payload_json, '$.lineage.parentThreadId')
3880+
AND json_extract(parent.payload_json, '$.settledOverride') = 'settled'
3881+
)
38753882
)
38763883
)
38773884
AND NOT EXISTS (

0 commit comments

Comments
 (0)