Skip to content

Commit 9ab75b7

Browse files
committed
fix(server): ignore background tasks of deleted threads when keeping the host awake
1 parent fe60aa0 commit 9ab75b7

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,12 +883,14 @@ it.effect("working runs exclude queued and finished runs", () =>
883883
});
884884
assert.isTrue(yield* store.hasWorkingRuns());
885885

886+
// Recovery skips deleted threads, so their leftover roster must not hold the host awake.
887+
const { thread } = yield* store.getThreadProjection(threadId);
886888
yield* store.apply({
887-
id: EventId.make("working-runs:background-cleared"),
888-
type: "provider-thread.updated",
889+
id: EventId.make("working-runs:thread-deleted"),
890+
type: "thread.deleted",
889891
threadId,
890892
occurredAt: now,
891-
payload: { ...providerThread, pendingBackgroundTasks: [] },
893+
payload: { ...thread, deletedAt: now },
892894
});
893895
assert.isFalse(yield* store.hasWorkingRuns());
894896
}).pipe(Effect.provide(layerTest)),

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7244,8 +7244,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
72447244
SELECT 1
72457245
FROM orchestration_v2_projection_provider_threads AS provider_thread
72467246
JOIN json_each(provider_thread.payload_json, '$.pendingBackgroundTasks') AS task
7247-
WHERE json_extract(task.value, '$.awaited') = 1
7248-
OR json_extract(task.value, '$.kind') IN ('subagent', 'background_task')
7247+
LEFT JOIN orchestration_v2_projection_threads AS thread
7248+
ON thread.thread_id = provider_thread.thread_id
7249+
WHERE thread.deleted_at IS NULL
7250+
AND (
7251+
json_extract(task.value, '$.awaited') = 1
7252+
OR json_extract(task.value, '$.kind') IN ('subagent', 'background_task')
7253+
)
72497254
)
72507255
) AS working
72517256
`;

0 commit comments

Comments
 (0)