Mirror the server's turn-id guard when settling

Self-review of the settle path, which has been the source of every
regression in this branch. The server filters `turnId !== null &&
state === "running"` before settling; this filtered only on state.

No row is both null-id and running today — the sole null-id insert
writes 'pending' — so this changes no behavior. It is the invariant the
UPDATE already depends on, written down where it is relied upon, and it
keeps the two predicates directly comparable if a null-id running row
ever becomes possible.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5 committed 2026-07-26 20:38:49 -07:00
1 parent c92ad95fe3
commit dba56bafa3
1 file changed
+7
+7
View File
@@ -256,6 +256,12 @@ function settleRunningTurns(
const setCompletedAt = hasColumn(target, "projection_turns", "completed_at")
? `, completed_at = requested_at`
: "";
// `turn_id IS NOT NULL` mirrors the server's own settle filter
// (ProjectionPipeline, on the session leaving "running"). No writer produces
// a row that is both null-id and running today — the only null-id insert
// writes 'pending' — so this is guarding the invariant rather than a case,
// and it keeps the two predicates comparable if that ever changes.
//
// 'running' is inlined, not bound: `--threads` is capped at SQLite's variable
// ceiling, so every binding beyond one per thread puts the maximum over the
// limit. Safe to interpolate — an internal literal, never input.
@@ -263,6 +269,7 @@ function settleRunningTurns(
.prepare(
`UPDATE projection_turns SET state = 'interrupted'${setCompletedAt}
WHERE state = 'running'
AND turn_id IS NOT NULL
AND thread_id IN (${placeholders(threadIds.length)})`,
)
.run(...threadIds);