Skip to content

Commit f7482ce

Browse files
dominikzabcikclaude
andcommitted
fix(server): keep-awake rechecks when background items start, and counts archived threads
A subagent the provider reported after its run ended did not trigger a recheck, so the Mac could sleep while it worked. Archived threads with pending background work were left out of the check. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 978ab4f commit f7482ce

5 files changed

Lines changed: 24 additions & 8 deletions

File tree

‎apps/server/src/background/KeepAwake.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,14 @@ it.effect("whileWorking holds from the first live work to the last", () =>
178178
yield* harness.work("run.updated", true);
179179
assert.equal(yield* harness.next, "query");
180180

181-
// Only an item that ended can have changed the answer, so progress asks nothing.
182-
yield* harness.work("turn-item.updated", true, { type: "subagent", status: "running" });
183181
yield* harness.work("turn-item.updated", false, { type: "subagent", status: "completed" });
184182
assert.deepEqual([yield* harness.next, yield* harness.next], ["query", "kill"]);
183+
184+
// A provider can report a subagent after the run has ended.
185+
yield* harness.work("turn-item.updated", true, { type: "subagent", status: "running" });
186+
assert.deepEqual([yield* harness.next, yield* harness.next], ["query", "spawn"]);
185187
assert.isTrue(yield* harness.idle);
186-
assert.lengthOf(harness.commands, 1);
188+
assert.lengthOf(harness.commands, 2);
187189
}),
188190
);
189191

‎apps/server/src/background/KeepAwake.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import type { KeepAwakeMode, OrchestrationV2StoredEvent } from "@t3tools/contracts";
77
import * as HostProcess from "@t3tools/shared/HostProcess";
8-
import { turnItemUpdateCanEndBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
8+
import { isBackgroundTurnItem } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
99
import * as Duration from "effect/Duration";
1010
import * as Effect from "effect/Effect";
1111
import * as Layer from "effect/Layer";
@@ -49,9 +49,9 @@ const WORK_EVENT_TYPES = [
4949
"thread.deleted",
5050
] as const;
5151

52-
// Item updates stream with every tool call; only an ending background item can release the hold.
52+
// Messages and plans stream item updates too; only background items can start or end work.
5353
const canChangeLiveWork = ({ event }: OrchestrationV2StoredEvent) =>
54-
event.type !== "turn-item.updated" || turnItemUpdateCanEndBackgroundWork(event.payload);
54+
event.type !== "turn-item.updated" || isBackgroundTurnItem(event.payload);
5555

5656
const restartSchedule = Schedule.exponential("1 second").pipe(
5757
Schedule.modifyDelay(({ duration }) =>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9634,6 +9634,15 @@ it.effect("reports live agent work for active runs and holding background work o
96349634
assert.equal(yield* store.hasLiveAgentWork(), expected, name);
96359635
}
96369636

9637+
yield* store.apply({
9638+
id: EventId.make("event:live-work:archived"),
9639+
type: "thread.archived",
9640+
threadId,
9641+
occurredAt: now,
9642+
payload: { ...initial.thread, archivedAt: now },
9643+
});
9644+
assert.isTrue(yield* store.hasLiveAgentWork(), "archived thread");
9645+
96379646
yield* store.apply({
96389647
id: EventId.make("event:live-work:deleted"),
96399648
type: "thread.deleted",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7653,12 +7653,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
76537653
`;
76547654
if (candidates.length === 0) return false;
76557655
// The shell decides what is pending, so a hold always has a thread that shows it.
7656-
const { threads } = yield* decodeShellSnapshot(
7656+
const { threads, archivedThreads } = yield* decodeShellSnapshot(
76577657
yield* readShellSnapshotRows({
76587658
threadIds: candidates.map((row) => ThreadId.make(row.thread_id)),
76597659
}),
76607660
);
7661-
return threads.some((shell) =>
7661+
return [...threads, ...archivedThreads].some((shell) =>
76627662
backgroundWorkHoldsCompletion(
76637663
(shell.pendingBackgroundTasks ?? []).filter((task) => !isPullRequestWatchTask(task)),
76647664
),

‎packages/shared/src/orchestrationV2PendingBackgroundWork.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ void TERMINAL_RUN_STATUSES;
4848

4949
export type PendingBackgroundWorkTask = OrchestrationV2PendingBackgroundTask;
5050

51+
/** Starting or ending one of these items can change whether a thread has background work. */
52+
export function isBackgroundTurnItem(item: Pick<OrchestrationV2TurnItem, "type">): boolean {
53+
return BACKGROUND_TURN_ITEM_TYPES.has(item.type);
54+
}
55+
5156
export function turnItemUpdateCanEndBackgroundWork(
5257
item: Pick<OrchestrationV2TurnItem, "type" | "status">,
5358
): boolean {

0 commit comments

Comments
 (0)