@@ -69,7 +69,6 @@ import {
6969 isOrchestrationV2SupersededInterrupt ,
7070} from "@t3tools/shared/orchestrationV2Timeline" ;
7171import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork" ;
72- import { isBufferedProviderNotification } from "./NotificationMailbox.ts" ;
7372import * as Context from "effect/Context" ;
7473import * as DateTime from "effect/DateTime" ;
7574import * as Effect from "effect/Effect" ;
@@ -593,12 +592,7 @@ function needsRecovery(
593592 projection . runs . some (
594593 ( run ) =>
595594 [ "preparing" , "starting" , "running" , "waiting" ] . includes ( run . status ) ||
596- ( run . status === "queued" &&
597- ( run . queueHeld !== true ||
598- projection . messages . some (
599- ( message ) =>
600- message . id === run . userMessageId && isBufferedProviderNotification ( message ) ,
601- ) ) ) ,
595+ ( run . status === "queued" && run . queueHeld !== true ) ,
602596 ) ||
603597 projection . runtimeRequests . some ( ( request ) => request . status === "pending" ) ||
604598 projection . subagents . some (
@@ -3645,23 +3639,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
36453639 SELECT thread_id FROM orchestration_v2_projection_runs
36463640 WHERE status IN ('preparing', 'starting', 'running', 'waiting')
36473641 UNION
3648- -- Held durable queues need no reread. Old buffered wakes still
3649- -- need cancellation even if earlier recovery held them.
3650- SELECT queued.thread_id FROM orchestration_v2_projection_runs AS queued
3651- WHERE queued.status = 'queued'
3652- AND (CASE WHEN json_valid(queued.payload_json)
3653- THEN json_extract(queued.payload_json, '$.queueHeld') IS NOT 1
3654- ELSE 1 END OR EXISTS (
3655- SELECT 1 FROM orchestration_v2_projection_messages AS message
3656- WHERE message.message_id = CASE WHEN json_valid(queued.payload_json)
3657- THEN json_extract(queued.payload_json, '$.userMessageId') END
3658- AND CASE WHEN json_valid(message.payload_json) THEN
3659- json_extract(message.payload_json, '$.createdBy') = 'agent'
3660- AND json_extract(message.payload_json, '$.creationSource') = 'provider'
3661- AND json_type(message.payload_json, '$.notification') = 'object'
3662- AND json_type(message.payload_json, '$.delegatedCompletion') IS NULL
3663- ELSE 0 END
3664- ))
3642+ -- A held queue already went through recovery; rereading it on
3643+ -- every boot costs a projection read per held thread.
3644+ SELECT thread_id FROM orchestration_v2_projection_runs
3645+ WHERE status = 'queued'
3646+ AND CASE WHEN json_valid(payload_json)
3647+ THEN json_extract(payload_json, '$.queueHeld') IS NOT 1
3648+ ELSE 1 END
36653649 UNION
36663650 SELECT thread_id FROM orchestration_v2_projection_runtime_requests
36673651 WHERE status = 'pending'
@@ -4207,18 +4191,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
42074191 ` ,
42084192 sql < PayloadRow > `
42094193 SELECT message.payload_json FROM orchestration_v2_projection_messages AS message
4210- WHERE message.thread_id = ${ threadId } AND (
4211- (message.streaming = 1 AND message.run_id IN (
4212- SELECT run_id FROM orchestration_v2_projection_runs
4213- WHERE thread_id = ${ threadId }
4214- AND status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
4215- ))
4216- OR message.message_id IN (
4217- SELECT json_extract(payload_json, '$.userMessageId')
4218- FROM orchestration_v2_projection_runs
4219- WHERE thread_id = ${ threadId } AND status = 'queued'
4194+ WHERE message.thread_id = ${ threadId } AND message.streaming = 1
4195+ AND message.run_id IN (
4196+ SELECT run_id FROM orchestration_v2_projection_runs
4197+ WHERE thread_id = ${ threadId }
4198+ AND status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
42204199 )
4221- )
42224200 ORDER BY message.created_at ASC, message.message_id ASC
42234201 ` ,
42244202 sql < PayloadRow > `
0 commit comments