Skip to content

Commit d9c8744

Browse files
dominikzabcikclaude
andcommitted
fix(server): keep-awake follows the thread shell for background work
Work the provider reports only as a turn item released the hold as soon as its run ended, and a deleted thread's roster kept holding. The check now reads the shells of threads with pending items or rosters, so the hold matches what the sidebar shows. Pull request watches do not hold. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 5b4a41e commit d9c8744

5 files changed

Lines changed: 126 additions & 22 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,16 @@ const makeHarness = Effect.fn("makeKeepAwakeHarness")(function* (options: {
129129
Effect.flatMap((next) => PubSub.publish(settingsChanges, next)),
130130
);
131131
const work = (
132-
type: "run.created" | "run.updated" | "provider-thread.updated",
132+
type: "run.created" | "run.updated" | "provider-thread.updated" | "turn-item.updated",
133133
live: boolean | "unreadable",
134+
payload?: { readonly type: "subagent"; readonly status: "running" | "completed" },
134135
) =>
135136
Ref.set(working, live).pipe(
136137
Effect.andThen(
137138
Effect.suspend(() => {
138139
const stored = {
139140
sequence: committed.length + 1,
140-
event: { type },
141+
event: { type, payload },
141142
} as OrchestrationV2StoredEvent;
142143
committed.push(stored);
143144
return PubSub.publish(events, stored);
@@ -177,8 +178,11 @@ it.effect("whileWorking holds from the first live work to the last", () =>
177178
yield* harness.work("run.updated", true);
178179
assert.equal(yield* harness.next, "query");
179180

180-
yield* harness.work("provider-thread.updated", false);
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" });
183+
yield* harness.work("turn-item.updated", false, { type: "subagent", status: "completed" });
181184
assert.deepEqual([yield* harness.next, yield* harness.next], ["query", "kill"]);
185+
assert.isTrue(yield* harness.idle);
182186
assert.lengthOf(harness.commands, 1);
183187
}),
184188
);

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
*
44
* @module KeepAwake
55
*/
6-
import type { KeepAwakeMode } from "@t3tools/contracts";
6+
import type { KeepAwakeMode, OrchestrationV2StoredEvent } from "@t3tools/contracts";
77
import * as HostProcess from "@t3tools/shared/HostProcess";
8+
import { turnItemUpdateCanEndBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
89
import * as Duration from "effect/Duration";
910
import * as Effect from "effect/Effect";
1011
import * as Layer from "effect/Layer";
@@ -40,7 +41,17 @@ const inhibitor = ChildProcess.make("/usr/bin/caffeinate", ["-i", "-w", String(p
4041
forceKillAfter: Duration.seconds(2),
4142
});
4243

43-
const WORK_EVENT_TYPES = ["run.created", "run.updated", "provider-thread.updated"] as const;
44+
const WORK_EVENT_TYPES = [
45+
"run.created",
46+
"run.updated",
47+
"provider-thread.updated",
48+
"turn-item.updated",
49+
"thread.deleted",
50+
] as const;
51+
52+
// Item updates stream with every tool call; only an ending background item can release the hold.
53+
const canChangeLiveWork = ({ event }: OrchestrationV2StoredEvent) =>
54+
event.type !== "turn-item.updated" || turnItemUpdateCanEndBackgroundWork(event.payload);
4455

4556
const restartSchedule = Schedule.exponential("1 second").pipe(
4657
Schedule.modifyDelay(({ duration }) =>
@@ -71,7 +82,10 @@ const make = Effect.gen(function* () {
7182
WORK_EVENT_TYPES.map((eventType) => eventSink.stream({ afterSequence, eventType })),
7283
{ concurrency: "unbounded" },
7384
);
74-
return Stream.concat(Stream.succeed(undefined), workEvents).pipe(
85+
return Stream.concat(
86+
Stream.succeed(undefined),
87+
Stream.filter(workEvents, canChangeLiveWork),
88+
).pipe(
7589
Stream.buffer({ capacity: 1, strategy: "sliding" }),
7690
Stream.mapEffect(() => projections.hasLiveAgentWork()),
7791
);

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

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8760,18 +8760,20 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
87608760
);
87618761
});
87628762

8763-
it.effect("reports live agent work for active runs and holding background rosters only", () =>
8763+
it.effect("reports live agent work for active runs and holding background work only", () =>
87648764
Effect.gen(function* () {
87658765
const store = yield* ProjectionStore.ProjectionStoreV2;
8766-
const now = yield* DateTime.now;
8767-
const threadId = ThreadId.make("thread:live-work");
8766+
const threadId = yield* addRolledBackRecoveryCandidate("live-work");
8767+
const initial = yield* store.getThreadProjection(threadId);
8768+
const now = initial.updatedAt;
87688769
const runId = RunId.make("run:live-work");
8770+
// The fixture's command is still running, but its run was rolled back.
87698771
assert.isFalse(yield* store.hasLiveAgentWork());
87708772

87718773
const run = {
87728774
id: runId,
87738775
threadId,
8774-
ordinal: 1,
8776+
ordinal: 2,
87758777
providerInstanceId,
87768778
modelSelection,
87778779
providerThreadId: null,
@@ -8856,5 +8858,66 @@ it.effect("reports live agent work for active runs and holding background roster
88568858
});
88578859
assert.equal(yield* store.hasLiveAgentWork(), expected, name);
88588860
}
8861+
8862+
// Work the provider reports only as a turn item, with the run over and the roster empty.
8863+
const command = {
8864+
id: TurnItemId.make("item:live-work:command"),
8865+
threadId,
8866+
runId,
8867+
nodeId: null,
8868+
providerThreadId: null,
8869+
providerTurnId: null,
8870+
nativeItemRef: null,
8871+
parentItemId: null,
8872+
ordinal: 2,
8873+
status: "running" as const,
8874+
title: null,
8875+
startedAt: now,
8876+
completedAt: null,
8877+
updatedAt: now,
8878+
type: "command_execution" as const,
8879+
input: "sleep 60",
8880+
};
8881+
const subagent = {
8882+
...command,
8883+
id: TurnItemId.make("item:live-work:subagent"),
8884+
type: "subagent" as const,
8885+
subagentId: NodeId.make("node:live-work:subagent"),
8886+
origin: "provider_native" as const,
8887+
driver,
8888+
providerInstanceId,
8889+
childThreadId: null,
8890+
prompt: "Review",
8891+
result: null,
8892+
};
8893+
const items = [
8894+
["bare command item", command, false],
8895+
["subagent item", subagent, true],
8896+
[
8897+
"ended subagent item",
8898+
{ ...subagent, status: "completed" as const, completedAt: now },
8899+
false,
8900+
],
8901+
["second subagent item", { ...subagent, id: TurnItemId.make("item:live-work:two") }, true],
8902+
] as const;
8903+
for (const [name, payload, expected] of items) {
8904+
yield* store.apply({
8905+
id: EventId.make(`event:live-work:item:${name}`),
8906+
type: "turn-item.updated",
8907+
threadId,
8908+
occurredAt: now,
8909+
payload,
8910+
});
8911+
assert.equal(yield* store.hasLiveAgentWork(), expected, name);
8912+
}
8913+
8914+
yield* store.apply({
8915+
id: EventId.make("event:live-work:deleted"),
8916+
type: "thread.deleted",
8917+
threadId,
8918+
occurredAt: now,
8919+
payload: { ...initial.thread, deletedAt: now },
8920+
});
8921+
assert.isFalse(yield* store.hasLiveAgentWork(), "deleted thread");
88598922
}).pipe(Effect.provide(layerTest)),
88608923
);

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import {
8686
backgroundWorkHoldsCompletion,
8787
deriveBlockedOnThreadWait,
8888
derivePendingBackgroundWork,
89+
isPullRequestWatchTask,
8990
} from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
9091
import * as Context from "effect/Context";
9192
import * as DateTime from "effect/DateTime";
@@ -520,7 +521,7 @@ export interface ProjectionStoreV2Shape {
520521
>;
521522
/** Threads that are not deleted, archived ones included. */
522523
readonly countThreads: () => Effect.Effect<number, ProjectionStoreV2Error>;
523-
/** An active run, or a background roster that holds its thread's completion, on any thread. */
524+
/** An active run, or pending background work that holds its thread's completion, on any thread. */
524525
readonly hasLiveAgentWork: () => Effect.Effect<boolean, ProjectionStoreV2Error>;
525526
/** Every active thread, in shell order. */
526527
readonly listActiveShellIndex: () => Effect.Effect<
@@ -7298,17 +7299,32 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
72987299
LIMIT 1
72997300
`;
73007301
if (activeRuns.length > 0) return true;
7301-
const rosters = yield* sql<PayloadRow>`
7302-
SELECT payload_json FROM orchestration_v2_projection_provider_threads
7303-
WHERE CASE WHEN json_valid(payload_json)
7304-
THEN json_array_length(payload_json, '$.pendingBackgroundTasks') > 0
7305-
ELSE 0 END
7302+
// Starts from the pending-item partial index, so its predicate matches the shell's read.
7303+
const candidates = yield* sql<{ readonly thread_id: string }>`
7304+
SELECT t.thread_id FROM orchestration_v2_projection_threads t
7305+
WHERE t.deleted_at IS NULL
7306+
AND t.thread_id IN (
7307+
SELECT item.thread_id FROM orchestration_v2_projection_turn_items item
7308+
WHERE item.type IN ('command_execution', 'dynamic_tool', 'subagent')
7309+
AND item.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled')
7310+
UNION
7311+
SELECT provider_thread.thread_id
7312+
FROM orchestration_v2_projection_provider_threads provider_thread
7313+
WHERE json_array_length(provider_thread.payload_json, '$.pendingBackgroundTasks') > 0
7314+
)
73067315
`;
7307-
for (const row of rosters) {
7308-
const providerThread = yield* decodeProviderThreadPayload(row.payload_json);
7309-
if (backgroundWorkHoldsCompletion(providerThread.pendingBackgroundTasks ?? [])) return true;
7310-
}
7311-
return false;
7316+
if (candidates.length === 0) return false;
7317+
// The shell decides what is pending, so a hold always has a thread that shows it.
7318+
const { threads } = yield* decodeShellSnapshot(
7319+
yield* readShellSnapshotRows({
7320+
threadIds: candidates.map((row) => ThreadId.make(row.thread_id)),
7321+
}),
7322+
);
7323+
return threads.some((shell) =>
7324+
backgroundWorkHoldsCompletion(
7325+
(shell.pendingBackgroundTasks ?? []).filter((task) => !isPullRequestWatchTask(task)),
7326+
),
7327+
);
73127328
}, Effect.mapError(shellSnapshotReadError));
73137329

73147330
const listActiveShellIndex = Effect.fn("ProjectionStore.listActiveShellIndex")(function* () {

‎packages/shared/src/orchestrationV2PendingBackgroundWork.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ export function derivePendingBackgroundWork(input: {
289289
return Array.from(byTaskId.values());
290290
}
291291

292+
const PULL_REQUEST_WATCH_TASK_PREFIX = "pull-request-watch:";
293+
294+
/** A watch waits on GitHub, not on work running on this machine. */
295+
export function isPullRequestWatchTask(task: Pick<PendingBackgroundWorkTask, "taskId">): boolean {
296+
return task.taskId.startsWith(PULL_REQUEST_WATCH_TASK_PREFIX);
297+
}
298+
292299
function pullRequestWatchTasks(
293300
pullRequests: ReadonlyArray<PendingBackgroundWorkPullRequest> | undefined,
294301
): Array<PendingBackgroundWorkTask> {
@@ -302,7 +309,7 @@ function pullRequestWatchTasks(
302309
{
303310
// Links store their normalized identity, and this id only has to be unique within the
304311
// thread, so it reads those fields instead of parsing the URL.
305-
taskId: `pull-request-watch:${link.host.toLowerCase()}/${link.repository.toLowerCase()}#${link.number}`,
312+
taskId: `${PULL_REQUEST_WATCH_TASK_PREFIX}${link.host.toLowerCase()}/${link.repository.toLowerCase()}#${link.number}`,
306313
description: `Watching pull request #${link.number}`,
307314
kind: "monitor" as const,
308315
awaited: true as const,

0 commit comments

Comments
 (0)