@devries-cameron20 / t3code-1 / commits / 95414cf1ad
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): usage-limit auto-resume no longer arms children of settled parents (#27951)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 2, 2026 at 18:00 UTC 95414cf1ad5af17ddb6e710218579a23ceee4bfa
2 files changed +185
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+178
@@ -48,6 +48,7 @@ import {
48 THREAD_HISTORY_PAGE_POLICY,
49 THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
50 } from "./threadHistoryPaging.ts";
51 +import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
52
53 const layerTest = Layer.mergeAll(
54 ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
@@ -5457,6 +5458,183 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5458 }),
5459 );
5460
5461 + it.effect("does not freshly arm limit recovery for a subagent of a settled parent", () =>
5462 + Effect.gen(function* () {
5463 + const store = yield* ProjectionStore.ProjectionStoreV2;
5464 + const now = yield* DateTime.now;
5465 + const resetAt = DateTime.formatIso(DateTime.add(now, { hours: 1 }));
5466 + const threadPayload = (
5467 + threadId: ThreadId,
5468 + parentThreadId: ThreadId | null,
5469 + settledOverride: "settled" | null,
5470 + ) =>
5471 + ({
5472 + createdBy: "user",
5473 + creationSource: "web",
5474 + id: threadId,
5475 + projectId: ProjectId.make("project:settled-parent-limit"),
5476 + title: "Settled parent limit recovery",
5477 + providerInstanceId,
5478 + modelSelection,
5479 + runtimeMode: "full-access",
5480 + interactionMode: "default",
5481 + branch: null,
5482 + worktreePath: null,
5483 + activeProviderThreadId: null,
5484 + lineage: {
5485 + parentThreadId,
5486 + relationshipToParent: parentThreadId === null ? null : "subagent",
5487 + rootThreadId: parentThreadId ?? threadId,
5488 + },
5489 + forkedFrom: null,
5490 + createdAt: now,
5491 + updatedAt: now,
5492 + archivedAt: null,
5493 + settledOverride,
5494 + settledAt: settledOverride === null ? null : now,
5495 + lastVisitedAt: null,
5496 + deletedAt: null,
5497 + }) as const;
5498 + const createThread = (
5499 + threadId: ThreadId,
5500 + parentThreadId: ThreadId | null,
5501 + settledOverride: "settled" | null,
5502 + ) =>
5503 + store.apply({
5504 + id: EventId.make(`event:${threadId}:created`),
5505 + type: "thread.created",
5506 + threadId,
5507 + occurredAt: now,
5508 + payload: threadPayload(threadId, parentThreadId, settledOverride),
5509 + });
5510 + const addLimitedChild = Effect.fnUntraced(function* (name: string, parentSettled: boolean) {
5511 + const parentId = ThreadId.make(`thread:settled-parent-limit:${name}:parent`);
5512 + const childId = ThreadId.make(`thread:settled-parent-limit:${name}:child`);
5513 + const runId = RunId.make(`run:settled-parent-limit:${name}`);
5514 + const rootNodeId = NodeId.make(`node:settled-parent-limit:${name}`);
5515 + yield* createThread(parentId, null, parentSettled ? "settled" : null);
5516 + yield* createThread(childId, parentId, null);
5517 + const run = {
5518 + id: runId,
5519 + threadId: childId,
5520 + ordinal: 1,
5521 + providerInstanceId,
5522 + modelSelection,
5523 + providerThreadId: null,
5524 + userMessageId: MessageId.make(`message:settled-parent-limit:${name}`),
5525 + rootNodeId,
5526 + activeAttemptId: null,
5527 + status: "failed" as const,
5528 + requestedAt: now,
5529 + startedAt: now,
5530 + completedAt: now,
5531 + checkpointId: null,
5532 + contextHandoffId: null,
5533 + };
5534 + yield* store.apply({
5535 + id: EventId.make(`event:settled-parent-limit:${name}:run`),
5536 + type: "run.created",
5537 + threadId: childId,
5538 + runId,
5539 + nodeId: rootNodeId,
5540 + driver,
5541 + providerInstanceId,
5542 + occurredAt: now,
5543 + payload: run,
5544 + });
5545 + yield* store.apply({
5546 + id: EventId.make(`event:settled-parent-limit:${name}:error`),
5547 + type: "turn-item.updated",
5548 + threadId: childId,
5549 + occurredAt: now,
5550 + payload: {
5551 + id: TurnItemId.make(`item:settled-parent-limit:${name}:error`),
5552 + threadId: childId,
5553 + runId,
5554 + nodeId: rootNodeId,
5555 + providerThreadId: null,
5556 + providerTurnId: null,
5557 + nativeItemRef: null,
5558 + parentItemId: null,
5559 + ordinal: 1,
5560 + status: "failed",
5561 + title: "Usage limit reached",
5562 + startedAt: now,
5563 + completedAt: now,
5564 + updatedAt: now,
5565 + type: "error",
5566 + failure: {
5567 + class: "usage_limit",
5568 + message: "Plan limit reached.",
5569 + resetAt,
5570 + code: "usageLimitExceeded",
5571 + retryable: null,
5572 + },
5573 + },
5574 + });
5575 + return childId;
5576 + });
5577 + const settledParentChild = yield* addLimitedChild("settled", true);
5578 + const activeParentChild = yield* addLimitedChild("active", false);
5579 + for (const options of [
5580 + { now, autoResume: true, snooze: false },
5581 + { now, autoResume: false, snooze: true },
5582 + ]) {
5583 + const armed = (yield* store.getLimitRecoveryCandidates(options))
5584 + .filter((row) => row.id === settledParentChild || row.id === activeParentChild)
5585 + .filter(
5586 + (row) =>
5587 + limitRecoveryCommand(
5588 + row,
5589 + options.autoResume,
5590 + DateTime.toEpochMillis(now),
5591 + options.snooze,
5592 + )?.type === "thread.metadata.update",
5593 + )
5594 + .map((row) => row.id);
5595 + assert.deepEqual(
5596 + armed,
5597 + [activeParentChild],
5598 + `only the child of the unsettled parent is armed (${JSON.stringify(options)})`,
5599 + );
5600 + }
5601 + // A recovery armed before the parent settled still resumes at its reset.
5602 + yield* store.apply({
5603 + id: EventId.make("event:settled-parent-limit:settled:armed"),
5604 + type: "thread.metadata-updated",
5605 + threadId: settledParentChild,
5606 + occurredAt: now,
5607 + payload: {
5608 + ...threadPayload(
5609 + settledParentChild,
5610 + ThreadId.make("thread:settled-parent-limit:settled:parent"),
5611 + null,
5612 + ),
5613 + limitRecovery: {
5614 + runId: RunId.make("run:settled-parent-limit:settled"),
5615 + resetAt,
5616 + autoResume: true,
5617 + },
5618 + },
5619 + });
5620 + const afterReset = DateTime.add(now, { hours: 2 });
5621 + const resumed = (yield* store.getLimitRecoveryCandidates({
5622 + now: afterReset,
5623 + autoResume: true,
5624 + snooze: false,
5625 + })).find((row) => row.id === settledParentChild);
5626 + assert.isDefined(resumed);
5627 + assert.isTrue(
5628 + limitRecoveryCommand(
5629 + resumed,
5630 + true,
5631 + DateTime.toEpochMillis(afterReset),
5632 + false,
5633 + )?.commandId.startsWith("limit-resume:"),
5634 + );
5635 + }),
5636 + );
5637 +
5638 it.effect("projects one shared provider session into multiple thread bindings", () =>
5639 Effect.gen(function* () {
5640 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
apps/server/src/orchestration-v2/ProjectionStore.ts
+7
@@ -3974,6 +3974,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3974 ${booleanInt(options.autoResume)}
3975 OR (${booleanInt(options.snooze)} AND julianday(json_extract(item.payload_json, '$.failure.resetAt')) > julianday(${DateTime.formatIso(options.now)}))
3976 )
3977 + -- A subagent of a settled parent is abandoned work; resuming it would wake the parent.
3978 + AND NOT EXISTS (
3979 + SELECT 1 FROM orchestration_v2_projection_threads parent
3980 + WHERE json_extract(t.payload_json, '$.lineage.relationshipToParent') = 'subagent'
3981 + AND parent.thread_id = json_extract(t.payload_json, '$.lineage.parentThreadId')
3982 + AND json_extract(parent.payload_json, '$.settledOverride') = 'settled'
3983 + )
3984 )
3985 )
3986 AND NOT EXISTS (