@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 9,842 lines 439 KB
Raw
1 import type { ProjectThreadAwarenessV2Input } from "@t3tools/shared/agentAwareness";
2 import { applyOrchestrationV2ThreadVisit, pendingGuestMessageCount } from "@t3tools/contracts";
3 import { applyThreadPullRequestLinkUpdate } from "../sourceControl/threadPullRequests.ts";
4 import { projectTurnItemForWire } from "./WireProjection.ts";
5 import * as Stream from "effect/Stream";
6 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
7 import { makeThreadFind } from "./ThreadFind.ts";
8 import { unsentManualContinuationSource } from "@t3tools/shared/orchestrationV2ManualContinuation";
9 import type {
10 OrchestrationV2ThreadLinkMetadata,
11 OrchestrationV2SearchThreadInput,
12 OrchestrationV2SearchThreadResult,
13 OrchestrationV2ThreadHistoryPage,
14 } from "@t3tools/contracts";
15 import {
16 latestRootProviderFailure,
17 latestUnheldRun,
18 runRanAfter,
19 threadErrorSummary,
20 usageLimitRunPresentedAsLatest,
21 } from "@t3tools/shared/orchestrationV2ThreadError";
22 import { threadPullRequestsOf } from "../sourceControl/threadPullRequests.ts";
23 import {
24 anchorsModelSwitch,
25 isModelSwitchCandidate,
26 isShownUserMessageItem,
27 } from "./modelSwitch.ts";
28 import type {
29 OrchestrationV2AppThread,
30 OrchestrationV2Checkpoint,
31 OrchestrationV2CheckpointScope,
32 OrchestrationV2ExecutionNode,
33 OrchestrationV2PlanArtifact,
34 OrchestrationV2PendingRuntimeRequestSummary,
35 PlanId,
36 OrchestrationV2ConversationMessage,
37 OrchestrationV2ContextTransfer,
38 OrchestrationV2DomainEvent,
39 OrchestrationV2OlderSettledCount,
40 OrchestrationV2OlderSettledCursor,
41 OrchestrationV2ProjectedTurnItem,
42 OrchestrationV2ProviderThread,
43 OrchestrationV2ProviderTurn,
44 OrchestrationV2Run,
45 OrchestrationV2RuntimeRequest,
46 OrchestrationV2Subagent,
47 OrchestrationV2ThreadShellSnapshot,
48 OrchestrationV2ShellThreadStatus,
49 OrchestrationV2ThreadShell,
50 OrchestrationV2ThreadProjection,
51 OrchestrationV2TurnItem,
52 GuestShareId,
53 ProviderThreadId,
54 ProviderTurnId,
55 RunAttemptId,
56 } from "@t3tools/contracts";
57 import {
58 IDLE_COMPACTION_TEXT,
59 idleCompactionOf,
60 isQueuedProviderThreadPlaceholder,
61 OrchestrationV2AppThreadJson as OrchestrationV2AppThreadJsonSchema,
62 OrchestrationV2CheckpointJson as OrchestrationV2CheckpointJsonSchema,
63 OrchestrationV2CheckpointScopeJson as OrchestrationV2CheckpointScopeJsonSchema,
64 OrchestrationV2ContextHandoffJson as OrchestrationV2ContextHandoffJsonSchema,
65 OrchestrationV2ContextTransferJson as OrchestrationV2ContextTransferJsonSchema,
66 OrchestrationV2ConversationMessageJson as OrchestrationV2ConversationMessageJsonSchema,
67 OrchestrationV2ExecutionNodeJson as OrchestrationV2ExecutionNodeJsonSchema,
68 OrchestrationV2PlanArtifact as OrchestrationV2PlanArtifactSchema,
69 OrchestrationV2ProviderSessionJson as OrchestrationV2ProviderSessionJsonSchema,
70 OrchestrationV2ProviderThreadJson as OrchestrationV2ProviderThreadJsonSchema,
71 OrchestrationV2ProviderTurnJson as OrchestrationV2ProviderTurnJsonSchema,
72 OrchestrationV2RunAttemptJson as OrchestrationV2RunAttemptJsonSchema,
73 OrchestrationV2RunJson as OrchestrationV2RunJsonSchema,
74 OrchestrationV2RuntimeRequestJson as OrchestrationV2RuntimeRequestJsonSchema,
75 OrchestrationV2SubagentJson as OrchestrationV2SubagentJsonSchema,
76 OrchestrationV2TurnItemJson as OrchestrationV2TurnItemJsonSchema,
77 orchestrationV2RunWorkStartedAt,
78 RunId,
79 MessageId,
80 RuntimeRequestId,
81 CheckpointScopeId,
82 CheckpointId,
83 EnvironmentId,
84 ThreadId,
85 TurnItemId,
86 NodeId,
87 ProjectId,
88 ProviderInstanceId,
89 } from "@t3tools/contracts";
90 import { createOrchestrationV2TurnItemVisibility } from "@t3tools/shared/orchestrationV2Timeline";
91 import {
92 deriveBlockedOnThreadWait,
93 derivePendingBackgroundWork,
94 } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
95 import * as Context from "effect/Context";
96 import * as DateTime from "effect/DateTime";
97 import * as Effect from "effect/Effect";
98 import * as Layer from "effect/Layer";
99 import * as Result from "effect/Result";
100 import * as Schema from "effect/Schema";
101 import * as SqlClient from "effect/sql/SqlClient";
102 import type * as Statement from "effect/sql/Statement";
103
104 import { MCP_APP_OUTPUT_KEY } from "@t3tools/shared/mcpApp";
105 import { threadHtmlRenderAttachmentIds } from "../attachmentStore.ts";
106 import { DELEGATED_TASK_MESSAGE_CONDITION } from "./delegatedTaskMessage.ts";
107 import {
108 DEFERRED_COMMAND_MESSAGE_CONDITION,
109 JAVASCRIPT_TRIM_WHITESPACE,
110 } from "./deferredCommandMessage.ts";
111 import { readableSearchText } from "./messageSearchIndex.ts";
112 import {
113 isFoldableWorkItem,
114 isThreadHistoryUserTurn,
115 isConversationHistoryItem,
116 THREAD_HISTORY_MAX_RAW_TURNS,
117 missingInterruptRequests,
118 DEFERRED_WORK_ITEM_TYPES,
119 decodeThreadHistoryCursor,
120 deferWorkHistoryRows,
121 findCursorEndIndex,
122 selectDeferredWorkPage,
123 encodeThreadHistoryCursor,
124 olderHistoryPagePolicy,
125 selectHistoryPageFromCursor,
126 selectRecentTimelineWindow,
127 THREAD_HISTORY_PAGE_POLICY,
128 THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT,
129 THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
130 type SelectTimelinePageResult,
131 type ThreadHistoryPrefix,
132 } from "./threadHistoryPaging.ts";
133
134 export class ProjectionStoreApplyEventError extends Schema.TaggedError<ProjectionStoreApplyEventError>()(
135 "ProjectionStoreApplyEventError",
136 {
137 eventType: Schema.String,
138 cause: Schema.optional(Schema.Defect()),
139 },
140 ) {
141 override get message(): string {
142 return `Failed to apply orchestration projection event ${this.eventType}.`;
143 }
144 }
145
146 export class ProjectionStoreSetupError extends Schema.TaggedError<ProjectionStoreSetupError>()(
147 "ProjectionStoreSetupError",
148 {
149 cause: Schema.optional(Schema.Defect()),
150 },
151 ) {
152 override get message(): string {
153 return "Failed to initialize orchestration projection store.";
154 }
155 }
156
157 export class ProjectionStoreThreadNotFoundError extends Schema.TaggedError<ProjectionStoreThreadNotFoundError>()(
158 "ProjectionStoreThreadNotFoundError",
159 {
160 threadId: ThreadId,
161 },
162 ) {
163 override get message(): string {
164 return `No orchestration projection exists for thread ${this.threadId}.`;
165 }
166 }
167
168 export class ProjectionStoreReadError extends Schema.TaggedError<ProjectionStoreReadError>()(
169 "ProjectionStoreReadError",
170 {
171 threadId: ThreadId,
172 cause: Schema.optional(Schema.Defect()),
173 },
174 ) {
175 override get message(): string {
176 return `Failed to read orchestration projection for thread ${this.threadId}.`;
177 }
178 }
179
180 export const ProjectionStoreV2Error = Schema.Union([
181 ProjectionStoreSetupError,
182 ProjectionStoreApplyEventError,
183 ProjectionStoreThreadNotFoundError,
184 ProjectionStoreReadError,
185 ]);
186 export type ProjectionStoreV2Error = typeof ProjectionStoreV2Error.Type;
187
188 export type ProjectionRecoveryKind =
189 | "queued-runs"
190 | "runtime"
191 | "restart-continuations"
192 | "subagent-results"
193 | "delegated-completions"
194 | "unread-steers";
195
196 export type ProjectionLimitRecoveryCandidate = Pick<
197 OrchestrationV2ThreadShell,
198 | "id"
199 | "status"
200 | "lastErrorClass"
201 | "latestRunId"
202 | "usageLimitResetAt"
203 | "archivedAt"
204 | "settledOverride"
205 | "pendingRuntimeRequest"
206 | "latestRunCompletedAt"
207 | "updatedAt"
208 | "limitRecovery"
209 | "snoozedUntil"
210 >;
211
212 export type ProjectionThreadPullRequests = Pick<
213 OrchestrationV2AppThread,
214 "id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pullRequests"
215 >;
216
217 export type ProjectionSettlementCandidate = Pick<
218 OrchestrationV2ThreadShell,
219 | "id"
220 | "projectId"
221 | "branch"
222 | "worktreePath"
223 | "lineage"
224 | "pullRequests"
225 | "linkedPullRequest"
226 | "branchPullRequest"
227 | "createdAt"
228 | "updatedAt"
229 | "archivedAt"
230 | "settledOverride"
231 | "pinnedAt"
232 | "autoSettleDisabledAt"
233 | "snoozedUntil"
234 | "snoozedAt"
235 | "latestRunId"
236 | "latestRunRequestedAt"
237 | "latestRunStartedAt"
238 | "latestRunCompletedAt"
239 | "latestUserMessageAt"
240 | "status"
241 | "activityRunStartedAt"
242 | "activityRunStatus"
243 | "pendingRuntimeRequest"
244 | "pendingBackgroundTasks"
245 | "pendingGuestMessageCount"
246 > &
247 Pick<OrchestrationV2AppThread, "lastSnoozeWakeAt"> & {
248 readonly latestUserAuthoredMessageAt: DateTime.Utc | null;
249 };
250
251 const ProjectionCheckpointContext = Schema.Struct({
252 runs: Schema.Array(
253 OrchestrationV2RunJsonSchema.mapFields(({ id, ordinal, status }) => ({ id, ordinal, status })),
254 ),
255 checkpointScopes: Schema.Array(
256 OrchestrationV2CheckpointScopeJsonSchema.mapFields(({ id, runId, kind, cwd }) => ({
257 id,
258 runId,
259 kind,
260 cwd,
261 })),
262 ),
263 checkpoints: Schema.Array(
264 OrchestrationV2CheckpointJsonSchema.mapFields(
265 ({ scopeId, runId, ordinalWithinScope, appRunOrdinal, status, ref }) => ({
266 scopeId,
267 runId,
268 ordinalWithinScope,
269 appRunOrdinal,
270 status,
271 ref,
272 }),
273 ),
274 ),
275 });
276 export type ProjectionCheckpointContext = typeof ProjectionCheckpointContext.Type;
277 const decodeCheckpointContext = Schema.decodeUnknownEffect(ProjectionCheckpointContext);
278
279 export interface ProjectionCheckpointCaptureContext {
280 readonly run: OrchestrationV2Run | undefined;
281 readonly rootNode: OrchestrationV2ExecutionNode | undefined;
282 readonly scope: OrchestrationV2CheckpointScope | undefined;
283 readonly providerThread: OrchestrationV2ProviderThread | undefined;
284 /**
285 * Ordinals a runless baseline must not overwrite: ready snapshots, and missing checkpoints pinning
286 * a run. Without file snapshots that run link is what rewind uses. Stale rows are rebaselined.
287 */
288 readonly preservedCheckpointOrdinals: ReadonlyArray<number>;
289 /** Side chats share their parent's worktree and never snapshot it. */
290 readonly sideChat: boolean;
291 }
292
293 const RollbackTargetAttempts = Schema.Array(
294 OrchestrationV2RunAttemptJsonSchema.mapFields(({ id, providerTurnId }) => ({
295 id,
296 providerTurnId,
297 })),
298 );
299 const decodeRollbackTargetAttempts = Schema.decodeUnknownEffect(RollbackTargetAttempts);
300
301 export interface ProjectionCheckpointRollbackContext {
302 readonly thread: OrchestrationV2AppThread;
303 readonly providerThread: OrchestrationV2ProviderThread | undefined;
304 readonly providerSession: OrchestrationV2ThreadProjection["providerSessions"][number] | undefined;
305 readonly checkpoint: OrchestrationV2Checkpoint | undefined;
306 readonly scope: OrchestrationV2CheckpointScope | undefined;
307 readonly targetAttempt: (typeof RollbackTargetAttempts.Type)[number] | undefined;
308 readonly providerTurns: ReadonlyArray<OrchestrationV2ProviderTurn>;
309 readonly rolledBackAttemptIds: ReadonlyArray<RunAttemptId>;
310 readonly runsToRollback: ReadonlyArray<OrchestrationV2Run>;
311 readonly rootNodes: ReadonlyArray<OrchestrationV2ExecutionNode>;
312 readonly staleCheckpoints: ReadonlyArray<OrchestrationV2Checkpoint>;
313 }
314
315 export interface ProjectionProviderControlContext {
316 readonly providerThread: OrchestrationV2ThreadProjection["providerThreads"][number] | undefined;
317 readonly providerTurn: OrchestrationV2ProviderTurn | undefined;
318 readonly attempt: OrchestrationV2ThreadProjection["attempts"][number] | undefined;
319 readonly message: OrchestrationV2ConversationMessage | undefined;
320 readonly run: OrchestrationV2Run | undefined;
321 }
322
323 export interface ProjectionProviderControlTarget {
324 readonly providerThreadId: ProviderThreadId;
325 readonly providerTurnId: ProviderTurnId;
326 readonly attemptId?: RunAttemptId;
327 readonly messageId?: MessageId;
328 }
329
330 export type ProjectionRuntimeRecoveryState = Pick<
331 OrchestrationV2ThreadProjection,
332 | "thread"
333 | "runs"
334 | "attempts"
335 | "nodes"
336 | "subagents"
337 | "providerSessions"
338 | "providerThreads"
339 | "providerTurns"
340 | "runtimeRequests"
341 | "messages"
342 | "turnItems"
343 >;
344
345 export type ProjectionPendingUserInputs = Pick<
346 OrchestrationV2ThreadProjection,
347 "runtimeRequests" | "nodes" | "turnItems"
348 >;
349
350 export type ProjectionThreadProviderContext = Pick<
351 OrchestrationV2ThreadProjection,
352 "thread" | "providerSessions" | "providerThreads"
353 >;
354 export interface ProjectionRuntimeResponseContext {
355 readonly request: OrchestrationV2ThreadProjection["runtimeRequests"][number] | undefined;
356 readonly node: OrchestrationV2ThreadProjection["nodes"][number] | undefined;
357 readonly item: OrchestrationV2ThreadProjection["turnItems"][number] | undefined;
358 readonly session: OrchestrationV2ThreadProjection["providerSessions"][number] | undefined;
359 }
360
361 const PROMPT_NARROWING_BLOCKING_FILTERS = [
362 "runIds",
363 "runStatuses",
364 "latestRunLimit",
365 "turnItemRunId",
366 "turnItemRunIds",
367 "turnItemNodeIds",
368 "turnItemRequestIds",
369 "turnItemStatuses",
370 ] as const satisfies ReadonlyArray<keyof ProjectionRecordFilter>;
371
372 export interface ProjectionRecordFilter {
373 readonly providerThreadIds?: ReadonlyArray<ProviderThreadId>;
374 readonly providerTurnIds?: ReadonlyArray<ProviderTurnId>;
375 readonly providerTurnAttemptIds?: ReadonlyArray<RunAttemptId>;
376 readonly attemptIds?: ReadonlyArray<RunAttemptId>;
377 readonly messageRoles?: ReadonlyArray<OrchestrationV2ConversationMessage["role"]>;
378 readonly monitorNotificationsOnly?: boolean;
379 /** User messages that are monitor notifications or delegated-completion wakes, through their partial index. */
380 readonly delegatedTaskMessagesOnly?: true;
381 readonly turnItemRunId?: RunId;
382 readonly messageIds?: ReadonlyArray<MessageId>;
383 readonly messageRunIds?: ReadonlyArray<RunId>;
384 readonly turnItemRunIds?: ReadonlyArray<RunId | null>;
385 readonly turnItemNodeIds?: ReadonlyArray<NodeId>;
386 readonly runIds?: ReadonlyArray<RunId>;
387 /** Without `includeLatestRun`, reads through a status index and sorts the matches, so keep it to statuses few runs hold. */
388 readonly runStatuses?: ReadonlyArray<OrchestrationV2Run["status"]>;
389 /** Keeps the thread's highest-ordinal run even when `runStatuses` excludes it. */
390 readonly includeLatestRun?: true;
391 /** The thread's N highest-ordinal runs, plus any run `runStatuses` matches. Ignores `includeLatestRun`. */
392 readonly latestRunLimit?: number;
393 /** Keeps the run `latestExecutedRun` picks even when `runStatuses` excludes it. Ignores `includeLatestRun`. */
394 readonly includeLatestExecutedRun?: true;
395 readonly providerTurnThreadIds?: ReadonlyArray<ProviderThreadId>;
396 /** Only the provider turn whose token usage was updated last. */
397 readonly latestProviderTurnUsageOnly?: true;
398 readonly runtimeRequestStatuses?: ReadonlyArray<OrchestrationV2RuntimeRequest["status"]>;
399 readonly contextTransferStatuses?: ReadonlyArray<OrchestrationV2ContextTransfer["status"]>;
400 readonly subagentIds?: ReadonlyArray<NodeId>;
401 readonly subagentRunIds?: ReadonlyArray<RunId>;
402 readonly subagentChildThreadIds?: ReadonlyArray<ThreadId>;
403 readonly nodeIds?: ReadonlyArray<NodeId>;
404 readonly turnItemRequestIds?: ReadonlyArray<RuntimeRequestId>;
405 readonly turnItemTypes?: ReadonlyArray<OrchestrationV2TurnItem["type"]>;
406 readonly turnItemStatuses?: ReadonlyArray<OrchestrationV2TurnItem["status"]>;
407 readonly omitSettledLeafNodes?: boolean;
408 /**
409 * Skips ended subagents whose completion was acknowledged, delivered or disposed, unless their
410 * run is active or `except` names them.
411 */
412 readonly omitSettledSubagents?: true | { readonly except: ReadonlyArray<NodeId> };
413 /**
414 * Also skips the provider thread of each subagent that `omitSettledSubagents: true` skips, and
415 * with `omitSettledLeafNodes` its node. A provider thread that shares a provider session with
416 * the thread stays.
417 */
418 readonly omitSettledSubagentRecords?: boolean;
419 /**
420 * For a compacted thread, reads attempts, nodes, messages and provider turns of the recent runs
421 * and the runs named here. Turn items are the failed errors of every run and the prompts of the
422 * recent, fresh-context and model-switch runs. `turnItemTypes` does not apply. A read without `runs`, or with a run or turn-item
423 * filter, keeps its filtered items. Runs stay whole.
424 */
425 readonly sinceLastCompaction?: {
426 readonly keepRunIds: ReadonlyArray<RunId>;
427 /** Keeps the run that already holds this message, for a message that is sent again. */
428 readonly keepMessageId: MessageId;
429 };
430 }
431
432 // A fork inherits its source's history before it has messages of its own,
433 // unless a portable handoff's budget left none of that history to deliver.
434 const inheritsForkConversation = ({
435 thread,
436 contextTransfers,
437 contextHandoffs,
438 }: Pick<OrchestrationV2ThreadProjection, "thread" | "contextTransfers" | "contextHandoffs">) => {
439 if (thread.forkedFrom?.type !== "run") return false;
440 const resolution = contextTransfers.find(
441 (transfer) => transfer.type === "fork" && transfer.targetThreadId === thread.id,
442 )?.resolution;
443 return (
444 resolution?.strategy !== "portable_context" ||
445 contextHandoffs.some(
446 (handoff) =>
447 handoff.id === resolution.contextHandoffId && (handoff.history?.messages.length ?? 0) > 0,
448 )
449 );
450 };
451
452 export type ProjectionRecordField = Exclude<
453 keyof OrchestrationV2ThreadProjection,
454 "thread" | "updatedAt" | "visibleTurnItems"
455 >;
456 export type ProjectionRecords<K extends ProjectionRecordField> = Pick<
457 OrchestrationV2ThreadProjection,
458 "thread" | K
459 >;
460
461 /**
462 * A page holds the oldest matches after `afterPosition`, or with `beforePosition`,
463 * the newest matches before it. `itemId` ignores both bounds.
464 */
465 export interface ProjectionTimelinePageOptions {
466 readonly afterPosition?: number;
467 readonly beforePosition?: number;
468 readonly itemId?: TurnItemId;
469 readonly attachmentId?: string;
470 readonly view?: "messages" | "activity";
471 readonly limit: number;
472 }
473 export interface ProjectionTimelinePage {
474 readonly items: ReadonlyArray<OrchestrationV2ProjectedTurnItem>;
475 readonly totalItems: number;
476 /** Matches exist after the page. */
477 readonly hasMore: boolean;
478 /** Matches exist before the page. */
479 readonly hasEarlier: boolean;
480 }
481
482 export interface ShellSnapshotOptions {
483 readonly location?: "active" | "archive";
484 readonly projectId?: ProjectId;
485 readonly unsettledOnly?: boolean;
486 /**
487 * Only the threads sharing this thread's `lineage.rootThreadId`: the root, its forks, and their
488 * subagent trees. The thread may be deleted: forks keep a deleted source's root.
489 */
490 readonly familyOf?: ThreadId;
491 readonly worktreePath?: string;
492 readonly threadIds?: ReadonlyArray<ThreadId>;
493 }
494
495 /** What a shell subscriber's trims read from an active thread, without building its shell. */
496 export interface ActiveShellIndexEntry extends Pick<
497 OrchestrationV2ThreadShell,
498 | "id"
499 | "projectId"
500 | "lineage"
501 | "settledOverride"
502 | "settledAt"
503 | "pinnedAt"
504 | "pinOrderKey"
505 | "snoozedUntil"
506 > {
507 /** False only when no run, request, work item, or watch can make the shell live. */
508 readonly mayBeLive: boolean;
509 /** Whether it delegated a task to a linked environment, whose clients nest that task under it. */
510 readonly hasRemoteTasks: boolean;
511 }
512
513 /** What thread listings filter and order an active thread by, without building its shell. */
514 export type ActiveThreadListEntry = Pick<
515 OrchestrationV2ThreadShell,
516 | "id"
517 | "projectId"
518 | "title"
519 | "lineage"
520 | "delegatedFrom"
521 | "settledOverride"
522 | "settledAt"
523 | "snoozedUntil"
524 | "updatedAt"
525 >;
526
527 export interface OlderSettledThreadsQuery {
528 readonly before: OrchestrationV2OlderSettledCursor;
529 readonly projectIds?: ReadonlyArray<ProjectId> | undefined;
530 readonly limit: number;
531 }
532
533 export interface OlderSettledThreadsListing {
534 readonly threadIds: ReadonlyArray<ThreadId>;
535 /** Only for `limit: 0`. */
536 readonly counts: ReadonlyArray<OrchestrationV2OlderSettledCount>;
537 }
538
539 export interface ProjectionStoreV2Shape {
540 readonly searchThreadStream: (
541 input: OrchestrationV2SearchThreadInput,
542 ) => Stream.Stream<OrchestrationV2SearchThreadResult, ProjectionStoreV2Error>;
543 readonly searchThread: (
544 input: OrchestrationV2SearchThreadInput,
545 ) => Effect.Effect<OrchestrationV2SearchThreadResult, ProjectionStoreV2Error>;
546 /** Files thread deletion may remove; `includeShownByLiveFork` adds ones a live fork shows. */
547 readonly getThreadAttachmentIds: (
548 threadId: ThreadId,
549 options?: { readonly includeShownByLiveFork?: boolean },
550 ) => Effect.Effect<ReadonlyArray<string>, ProjectionStoreV2Error>;
551 readonly getThreadHistoryPage: (
552 threadId: ThreadId,
553 cursor: string,
554 throughEntryId?: string,
555 conversationOnly?: boolean,
556 workMode?: "deferred" | "work",
557 ) => Effect.Effect<OrchestrationV2ThreadHistoryPage, ProjectionStoreV2Error>;
558 readonly getTimelinePage: (
559 threadId: ThreadId,
560 options: ProjectionTimelinePageOptions,
561 ) => Effect.Effect<ProjectionTimelinePage, ProjectionStoreV2Error>;
562 readonly getMessageCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
563 /** Bytes of tool output a transcript export is sure to hold, or 0 when it can fit in `limit`. */
564 readonly getTranscriptByteFloor: (
565 threadId: ThreadId,
566 limit: number,
567 ) => Effect.Effect<number, ProjectionStoreV2Error>;
568 readonly getSubagentCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
569 readonly getRunCount: (threadId: ThreadId) => Effect.Effect<number, ProjectionStoreV2Error>;
570 /** Child threads of the thread's app-owned tasks, in task order, without decoding the tasks. */
571 readonly listAppOwnedChildThreadIds: (
572 threadId: ThreadId,
573 ) => Effect.Effect<ReadonlyArray<ThreadId>, ProjectionStoreV2Error>;
574 readonly hasPendingDelegatedCompletion: (
575 childThreadId: ThreadId,
576 ) => Effect.Effect<boolean, ProjectionStoreV2Error>;
577 readonly getNextTurnItemOrdinal: (
578 threadId: ThreadId,
579 ) => Effect.Effect<number, ProjectionStoreV2Error>;
580 readonly getTurnItem: (input: {
581 readonly threadId: ThreadId;
582 readonly itemId: TurnItemId;
583 }) => Effect.Effect<OrchestrationV2TurnItem | null, ProjectionStoreV2Error>;
584 readonly getMessagesById: (
585 threadId: ThreadId,
586 messageIds: ReadonlyArray<MessageId>,
587 ) => Effect.Effect<
588 ReadonlyMap<MessageId, OrchestrationV2ConversationMessage>,
589 ProjectionStoreV2Error
590 >;
591 readonly getThreadRecords: <K extends ProjectionRecordField>(
592 threadId: ThreadId,
593 fields: ReadonlyArray<K>,
594 filter?: ProjectionRecordFilter,
595 ) => Effect.Effect<ProjectionRecords<K>, ProjectionStoreV2Error>;
596
597 readonly apply: (
598 event: OrchestrationV2DomainEvent,
599 ) => Effect.Effect<void, ProjectionStoreV2Error>;
600 readonly getPullRequestSnapshot: (options: {
601 readonly threadId?: ThreadId;
602 readonly unsettledOnly?: boolean;
603 }) => Effect.Effect<
604 {
605 readonly snapshotSequence: number;
606 readonly threads: ReadonlyArray<typeof PullRequestThread.Type>;
607 },
608 ProjectionStoreV2Error
609 >;
610 readonly getAgentAwarenessSnapshot: (
611 threadId?: ThreadId,
612 ) => Effect.Effect<
613 { readonly snapshotSequence: number; readonly threads: ReadonlyArray<AgentAwarenessThread> },
614 ProjectionStoreV2Error
615 >;
616 readonly getShellSnapshot: (
617 options?: ShellSnapshotOptions,
618 ) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, ProjectionStoreV2Error>;
619 readonly listActiveSubagentThreadIds: () => Effect.Effect<
620 ReadonlyArray<ThreadId>,
621 ProjectionStoreV2Error
622 >;
623 /** Threads that are not deleted, archived ones included. */
624 readonly countThreads: () => Effect.Effect<number, ProjectionStoreV2Error>;
625 /** Every active thread, in shell order. */
626 readonly listActiveShellIndex: () => Effect.Effect<
627 ReadonlyArray<ActiveShellIndexEntry>,
628 ProjectionStoreV2Error
629 >;
630 /** Every active thread, or one project's. */
631 readonly listActiveThreads: (
632 projectId?: ProjectId,
633 ) => Effect.Effect<ReadonlyArray<ActiveThreadListEntry>, ProjectionStoreV2Error>;
634 /** Older settled thread ids below a cursor, newest settle first, and per-project counts. */
635 readonly listOlderSettledThreads: (
636 query: OlderSettledThreadsQuery,
637 ) => Effect.Effect<OlderSettledThreadsListing, ProjectionStoreV2Error>;
638 /**
639 * Callers that combine the shell with other reads run this inside their own
640 * transaction and the returned decode after it commits, so the shared
641 * connection is not held while thousands of rows are converted.
642 */
643 readonly readShellSnapshot: (
644 options?: ShellSnapshotOptions,
645 ) => Effect.Effect<
646 Effect.Effect<OrchestrationV2ThreadShellSnapshot, ProjectionStoreV2Error>,
647 ProjectionStoreV2Error
648 >;
649 readonly getThreadShell: (
650 threadId: ThreadId,
651 ) => Effect.Effect<OrchestrationV2ThreadShell | null, ProjectionStoreV2Error>;
652 /** The thread whose current guest share has this id. Scans thread rows; only redeem uses it. */
653 readonly findGuestShareThreadId: (
654 shareId: GuestShareId,
655 ) => Effect.Effect<ThreadId | null, ProjectionStoreV2Error>;
656 readonly getThread: (
657 threadId: ThreadId,
658 ) => Effect.Effect<OrchestrationV2AppThread, ProjectionStoreV2Error>;
659 /** Like getThread, but a missing thread is null, so an expected miss is not an error span. */
660 readonly findThread: (
661 threadId: ThreadId,
662 ) => Effect.Effect<OrchestrationV2AppThread | null, ProjectionStoreV2Error>;
663 readonly getLimitRecoveryCandidates: (options: {
664 readonly now: DateTime.Utc;
665 readonly autoResume: boolean;
666 readonly snooze: boolean;
667 }) => Effect.Effect<ReadonlyArray<ProjectionLimitRecoveryCandidate>, ProjectionStoreV2Error>;
668 /** Epoch ms of the next armed reset or snooze end after `now`, when a waiting thread becomes a candidate. */
669 readonly getLimitRecoveryWakeAt: (
670 now: DateTime.Utc,
671 ) => Effect.Effect<number | null, ProjectionStoreV2Error>;
672 readonly getSettlementCandidates: (
673 threadId?: ThreadId,
674 ) => Effect.Effect<ReadonlyArray<ProjectionSettlementCandidate>, ProjectionStoreV2Error>;
675 readonly getThreadsWithPullRequests: (
676 threadId?: ThreadId,
677 ) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequests>, ProjectionStoreV2Error>;
678 /** Titles of the given threads that exist, archived ones included. */
679 readonly getThreadLinkMetadata: (
680 threadIds: ReadonlyArray<ThreadId>,
681 ) => Effect.Effect<Record<ThreadId, OrchestrationV2ThreadLinkMetadata>, ProjectionStoreV2Error>;
682 readonly getThreadTitles: (
683 threadIds: ReadonlyArray<ThreadId>,
684 ) => Effect.Effect<Record<ThreadId, string>, ProjectionStoreV2Error>;
685 readonly getThreadWorktreePaths: (options?: {
686 readonly includeArchived?: boolean;
687 }) => Effect.Effect<
688 ReadonlyArray<{ readonly threadId: ThreadId; readonly worktreePath: string }>,
689 ProjectionStoreV2Error
690 >;
691 /**
692 * Delegated tasks in a linked environment with work left for this one: no
693 * result yet, or cancelled or stopped here without that stop reaching there,
694 * including a Stop after the task had its result.
695 */
696 readonly getOpenRemoteDelegatedTasks: Effect.Effect<
697 ReadonlyArray<{ readonly parentThreadId: ThreadId; readonly taskId: NodeId }>,
698 ProjectionStoreV2Error
699 >;
700 readonly getTurnStartContext: (
701 threadId: ThreadId,
702 runId: RunId,
703 ) => Effect.Effect<
704 Pick<
705 OrchestrationV2ThreadProjection,
706 | "thread"
707 | "runs"
708 | "attempts"
709 | "nodes"
710 | "subagents"
711 | "providerSessions"
712 | "providerThreads"
713 | "providerTurns"
714 | "messages"
715 | "checkpointScopes"
716 | "contextHandoffs"
717 | "contextTransfers"
718 | "turnItems"
719 > & { readonly hasConversation: boolean },
720 ProjectionStoreV2Error
721 >;
722 readonly getTurnStartHistory: (
723 threadId: ThreadId,
724 runIds?: ReadonlyArray<RunId>,
725 ) => Effect.Effect<ReadonlyArray<OrchestrationV2TurnItem>, ProjectionStoreV2Error>;
726 readonly getThreadProjection: (
727 threadId: ThreadId,
728 ) => Effect.Effect<OrchestrationV2ThreadProjection, ProjectionStoreV2Error>;
729 readonly getRuntimeRecoveryProjection: (
730 threadId: ThreadId,
731 ) => Effect.Effect<ProjectionRuntimeRecoveryState, ProjectionStoreV2Error>;
732 readonly getPendingNativeUserInputs: (
733 threadId: ThreadId,
734 providerTurnId: ProviderTurnId,
735 ) => Effect.Effect<ProjectionPendingUserInputs, ProjectionStoreV2Error>;
736 readonly getPlan: (
737 threadId: ThreadId,
738 planId: PlanId,
739 ) => Effect.Effect<OrchestrationV2PlanArtifact | undefined, ProjectionStoreV2Error>;
740 readonly hasUnpairedRunInterruptRequest: (
741 threadId: ThreadId,
742 requestId: TurnItemId,
743 resultId: TurnItemId,
744 ) => Effect.Effect<boolean, ProjectionStoreV2Error>;
745 /** Whether Stop reached the run. Reads one index entry, however long the thread is. */
746 readonly hasRunInterruptRequest: (
747 threadId: ThreadId,
748 runId: RunId,
749 ) => Effect.Effect<boolean, ProjectionStoreV2Error>;
750 /** One run and node with the run's provider thread, active attempt and provider turn, read together. */
751 readonly getRunProviderRecords: (
752 threadId: ThreadId,
753 runId: RunId,
754 nodeId: NodeId,
755 ) => Effect.Effect<
756 ProjectionRecords<"runs" | "nodes" | "providerThreads" | "providerTurns" | "attempts">,
757 ProjectionStoreV2Error
758 >;
759 /** `getRunProviderRecords` and the thread's cards of one type, read together. */
760 readonly getRunCardRecords: (
761 threadId: ThreadId,
762 runId: RunId,
763 nodeId: NodeId,
764 cardType: "secret_request" | "link_request",
765 ) => Effect.Effect<
766 ProjectionRecords<
767 "runs" | "nodes" | "providerThreads" | "providerTurns" | "attempts" | "turnItems"
768 >,
769 ProjectionStoreV2Error
770 >;
771 readonly isNodeStored: (
772 node: OrchestrationV2ExecutionNode,
773 ) => Effect.Effect<boolean, ProjectionStoreV2Error>;
774 readonly getRuntimeRequest: (
775 threadId: ThreadId,
776 requestId: RuntimeRequestId,
777 ) => Effect.Effect<
778 OrchestrationV2ThreadProjection["runtimeRequests"][number] | undefined,
779 ProjectionStoreV2Error
780 >;
781 readonly getProviderControlContext: (
782 threadId: ThreadId,
783 target: ProjectionProviderControlTarget,
784 ) => Effect.Effect<ProjectionProviderControlContext, ProjectionStoreV2Error>;
785 readonly getRunningTurnContext: (
786 threadId: ThreadId,
787 ) => Effect.Effect<
788 Pick<ProjectionProviderControlContext, "run" | "providerThread" | "providerTurn">,
789 ProjectionStoreV2Error
790 >;
791 readonly getThreadProviderContext: (
792 threadId: ThreadId,
793 targetInstanceId?: ProviderInstanceId,
794 ) => Effect.Effect<ProjectionThreadProviderContext, ProjectionStoreV2Error>;
795 readonly getRuntimeResponseContext: (
796 threadId: ThreadId,
797 requestId: RuntimeRequestId,
798 ) => Effect.Effect<ProjectionRuntimeResponseContext, ProjectionStoreV2Error>;
799 readonly getCheckpointContext: (
800 threadId: ThreadId,
801 ) => Effect.Effect<ProjectionCheckpointContext, ProjectionStoreV2Error>;
802 readonly getCheckpointCaptureContext: (
803 threadId: ThreadId,
804 target: { readonly runId: RunId; readonly scopeId: CheckpointScopeId },
805 ) => Effect.Effect<ProjectionCheckpointCaptureContext, ProjectionStoreV2Error>;
806 readonly getCheckpointRollbackContext: (
807 threadId: ThreadId,
808 target: {
809 readonly providerThreadId: ProviderThreadId;
810 readonly checkpointId: CheckpointId;
811 readonly scopeId: CheckpointScopeId;
812 },
813 ) => Effect.Effect<ProjectionCheckpointRollbackContext, ProjectionStoreV2Error>;
814 readonly getRunMessage: (
815 threadId: ThreadId,
816 runId: RunId,
817 ) => Effect.Effect<OrchestrationV2ConversationMessage | undefined, ProjectionStoreV2Error>;
818 readonly getRun: (
819 threadId: ThreadId,
820 runId: RunId,
821 ) => Effect.Effect<OrchestrationV2Run | undefined, ProjectionStoreV2Error>;
822 /**
823 * One snapshot: the `windowSize` runs that ended last, unfinished runs, the runs of `taskIds` and
824 * their wakes. `runsFloor` is set when older runs were left out; every run that ran after it is
825 * in `runs`.
826 */
827 readonly getDelegatedChildControls: (
828 threadId: ThreadId,
829 input: { readonly taskIds: ReadonlyArray<NodeId>; readonly windowSize: number },
830 ) => Effect.Effect<
831 ProjectionRecords<"runs" | "messages" | "subagents" | "providerThreads"> & {
832 readonly runsFloor: OrchestrationV2Run | undefined;
833 },
834 ProjectionStoreV2Error
835 >;
836 readonly canStartQueuedRun: (
837 threadId: ThreadId,
838 ) => Effect.Effect<boolean, ProjectionStoreV2Error>;
839 readonly getHeldQueuedRuns: (
840 threadId: ThreadId,
841 ) => Effect.Effect<ReadonlyArray<OrchestrationV2Run>, ProjectionStoreV2Error>;
842 readonly getRecoveryThreadIds: (
843 kind: ProjectionRecoveryKind,
844 projects?: {
845 readonly include?: ReadonlyArray<string>;
846 readonly exclude?: ReadonlyArray<string>;
847 },
848 ) => Effect.Effect<ReadonlyArray<ThreadId>, ProjectionStoreV2Error>;
849 readonly getUnreadableThreadIds: () => Effect.Effect<
850 ReadonlyArray<ThreadId>,
851 ProjectionStoreV2Error
852 >;
853 readonly getThreadSnapshot: (threadId: ThreadId) => Effect.Effect<
854 {
855 readonly schemaVersion: number;
856 readonly snapshotSequence: number;
857 readonly projection: OrchestrationV2ThreadProjection;
858 },
859 ProjectionStoreV2Error
860 >;
861 /** Context handoffs come without `history` and `delivery`; no window reader uses them. */
862 readonly getThreadSnapshotWindow: (
863 threadId: ThreadId,
864 options: {
865 readonly rowLimit: number;
866 readonly userTurnLimit?: number | undefined;
867 readonly deferWork?: boolean | undefined;
868 readonly anchorItemId?: TurnItemId | undefined;
869 readonly anchorThreadId?: ThreadId | undefined;
870 readonly requiredRunId?: RunId | undefined;
871 },
872 ) => Effect.Effect<
873 {
874 readonly schemaVersion: number;
875 readonly snapshotSequence: number;
876 readonly projection: OrchestrationV2ThreadProjection;
877 },
878 ProjectionStoreV2Error
879 >;
880 }
881
882 type ProjectionWindow = Omit<
883 Parameters<ProjectionStoreV2Shape["getThreadSnapshotWindow"]>[1],
884 "anchorThreadId"
885 > & {
886 readonly suppressLocal?: boolean | undefined;
887 readonly forkCreatedAt?: DateTime.Utc | undefined;
888 readonly historyAnchor?: { readonly threadId: ThreadId; readonly itemId: TurnItemId } | undefined;
889 };
890
891 export class ProjectionStoreV2 extends Context.Service<ProjectionStoreV2, ProjectionStoreV2Shape>()(
892 "t3/orchestration-v2/ProjectionStore/ProjectionStoreV2",
893 ) {}
894
895 // The provider batch owns the writer, so no other write can change these rows before it commits.
896 export interface ProviderBatchProjection {
897 readonly nodeReads: Map<NodeId, string | undefined>;
898 /** Thread `updated_at` bumps, written once by `writeThreadTouches` before the batch commits. */
899 readonly threadTouches: Map<ThreadId, string>;
900 }
901 export class ProviderBatchProjectionState extends Context.Reference<
902 ProviderBatchProjection | undefined
903 >("t3/orchestration-v2/ProjectionStore/ProviderBatchProjectionState", {
904 defaultValue: () => undefined,
905 }) {}
906
907 // Runs per projected event or once per batch; the projection or batch commit span traces it.
908 export const writeThreadTouches = Effect.fnUntraced(function* (
909 sql: SqlClient.SqlClient,
910 touches: ReadonlyMap<ThreadId, string>,
911 ) {
912 for (const [threadId, updatedAt] of touches) {
913 yield* sql`
914 UPDATE orchestration_v2_projection_threads
915 SET
916 updated_at = ${updatedAt},
917 payload_json = json_set(payload_json, '$.updatedAt', ${updatedAt})
918 WHERE thread_id = ${threadId}
919 `;
920 }
921 });
922
923 export const ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION = 2;
924
925 function upsertById<T extends { readonly id: string }>(items: ReadonlyArray<T>, next: T): Array<T> {
926 const index = items.findIndex((item) => item.id === next.id);
927 if (index === -1) {
928 return [...items, next];
929 }
930
931 const updated = [...items];
932 updated[index] = next;
933 return updated;
934 }
935
936 /** Adapters resend whole rosters without runs, so a task keeps the run it first appeared under. */
937 function attributePendingBackgroundTasks(
938 next: OrchestrationV2ProviderThread,
939 previous: Pick<OrchestrationV2ProviderThread, "pendingBackgroundTasks"> | undefined,
940 runId: OrchestrationV2DomainEvent["runId"],
941 ): OrchestrationV2ProviderThread {
942 const tasks = next.pendingBackgroundTasks ?? [];
943 if (tasks.every((task) => task.runId !== undefined)) return next;
944 const known = new Map(
945 (previous?.pendingBackgroundTasks ?? []).map((task) => [task.taskId, task.runId]),
946 );
947 return {
948 ...next,
949 pendingBackgroundTasks: tasks.map((task) => {
950 const owner = task.runId ?? (known.has(task.taskId) ? known.get(task.taskId) : runId);
951 return owner === undefined ? task : { ...task, runId: owner };
952 }),
953 };
954 }
955
956 function upsertProviderTurn(
957 turns: ReadonlyArray<OrchestrationV2ProviderTurn>,
958 next: OrchestrationV2ProviderTurn,
959 ): Array<OrchestrationV2ProviderTurn> {
960 const current = turns.find((turn) => turn.id === next.id);
961 return upsertById(turns, {
962 ...next,
963 ...((next.tokenUsage ?? current?.tokenUsage) === undefined
964 ? {}
965 : { tokenUsage: next.tokenUsage ?? current?.tokenUsage }),
966 });
967 }
968
969 function preserveRunRecordedFields(
970 current: OrchestrationV2Run | undefined,
971 next: OrchestrationV2Run,
972 ): OrchestrationV2Run {
973 if (current === undefined) return next;
974 return {
975 ...next,
976 ...(next.delegatedCompletion === undefined && current.delegatedCompletion !== undefined
977 ? { delegatedCompletion: current.delegatedCompletion }
978 : {}),
979 ...(next.restartCancelledBackgroundWork === undefined &&
980 current.restartCancelledBackgroundWork !== undefined
981 ? { restartCancelledBackgroundWork: current.restartCancelledBackgroundWork }
982 : {}),
983 // Only a submission sets the output; a lifecycle write carries it as it was when read.
984 ...(current.output !== undefined ? { output: current.output } : {}),
985 };
986 }
987
988 function preserveCompletionDelivery(
989 current: OrchestrationV2Subagent | undefined,
990 next: OrchestrationV2Subagent,
991 ): OrchestrationV2Subagent {
992 if (next.completionDelivery !== undefined || current?.completionDelivery === undefined) {
993 return next;
994 }
995 return { ...next, completionDelivery: current.completionDelivery };
996 }
997
998 export function emptyProjection(
999 event: Extract<OrchestrationV2DomainEvent, { readonly type: "thread.created" }>,
1000 ): OrchestrationV2ThreadProjection {
1001 return {
1002 thread: event.payload,
1003 runs: [],
1004 attempts: [],
1005 nodes: [],
1006 subagents: [],
1007 providerSessions: [],
1008 providerThreads: [],
1009 providerTurns: [],
1010 runtimeRequests: [],
1011 messages: [],
1012 plans: [],
1013 turnItems: [],
1014 checkpointScopes: [],
1015 checkpoints: [],
1016 contextHandoffs: [],
1017 contextTransfers: [],
1018 visibleTurnItems: [],
1019 updatedAt: event.occurredAt,
1020 };
1021 }
1022
1023 export function applyToProjection(
1024 projection: OrchestrationV2ThreadProjection,
1025 event: OrchestrationV2DomainEvent,
1026 ): OrchestrationV2ThreadProjection {
1027 const base = {
1028 ...projection,
1029 thread: {
1030 ...projection.thread,
1031 updatedAt: event.occurredAt,
1032 },
1033 updatedAt: event.occurredAt,
1034 };
1035
1036 switch (event.type) {
1037 case "thread.pull-request-link-updated":
1038 return {
1039 ...projection,
1040 thread: applyThreadPullRequestLinkUpdate(projection.thread, event.payload),
1041 };
1042 case "thread.created":
1043 case "thread.archived":
1044 case "thread.unarchived":
1045 case "thread.deleted":
1046 case "thread.settled":
1047 case "thread.unsettled":
1048 case "thread.snoozed":
1049 case "thread.unsnoozed":
1050 case "thread.auto-settle-set":
1051 case "thread.pinned":
1052 case "thread.unpinned":
1053 case "thread.pin-reordered":
1054 case "thread.metadata-updated":
1055 case "thread.runtime-mode-updated":
1056 case "thread.interaction-mode-updated":
1057 case "thread.model-selection-updated":
1058 case "thread.provider-switched":
1059 case "thread.guest-share-updated":
1060 return {
1061 ...base,
1062 thread: event.payload,
1063 };
1064 // Reordering and PR refreshes preserve the orchestrator's activity timestamp.
1065 case "thread.active-reordered":
1066 case "thread.tags-updated":
1067 case "thread.pull-request-synced":
1068 case "thread.issue-synced":
1069 return {
1070 ...base,
1071 thread: event.payload,
1072 updatedAt: event.payload.updatedAt,
1073 };
1074 case "thread.visited":
1075 case "thread.marked-unread":
1076 return {
1077 ...projection,
1078 thread: event.payload,
1079 };
1080 case "thread.visit-recorded":
1081 return {
1082 ...projection,
1083 thread: applyOrchestrationV2ThreadVisit(projection.thread, event.payload),
1084 };
1085 case "run.created":
1086 case "run.updated":
1087 return withLocalVisibleTurnItems({
1088 ...base,
1089 runs: upsertById(
1090 base.runs,
1091 preserveRunRecordedFields(
1092 base.runs.find((run) => run.id === event.payload.id),
1093 event.payload,
1094 ),
1095 ),
1096 });
1097 case "run.background-work-cancelled":
1098 return {
1099 ...base,
1100 runs: base.runs.map((run) =>
1101 run.id === event.payload.runId
1102 ? {
1103 ...run,
1104 restartCancelledBackgroundWork: event.payload.restartCancelledBackgroundWork,
1105 }
1106 : run,
1107 ),
1108 };
1109 case "run.output-submitted":
1110 return {
1111 ...base,
1112 runs: base.runs.map((run) =>
1113 run.id === event.payload.runId ? { ...run, output: event.payload.output } : run,
1114 ),
1115 };
1116 case "run-attempt.created":
1117 case "run-attempt.updated":
1118 return withLocalVisibleTurnItems({
1119 ...base,
1120 attempts: upsertById(base.attempts, event.payload),
1121 });
1122 case "node.updated":
1123 return {
1124 ...base,
1125 nodes: upsertById(base.nodes, event.payload),
1126 };
1127 case "subagent.updated":
1128 return {
1129 ...base,
1130 subagents: upsertById(
1131 base.subagents,
1132 preserveCompletionDelivery(
1133 base.subagents.find((task) => task.id === event.payload.id),
1134 event.payload,
1135 ),
1136 ),
1137 };
1138 case "provider-session.attached":
1139 case "provider-session.updated":
1140 return {
1141 ...base,
1142 providerSessions: upsertById(base.providerSessions, event.payload),
1143 };
1144 case "provider-session.detached":
1145 return {
1146 ...base,
1147 providerSessions: base.providerSessions.filter(
1148 (session) => session.id !== event.payload.providerSessionId,
1149 ),
1150 };
1151 case "provider-thread.updated":
1152 return {
1153 ...base,
1154 thread:
1155 event.payload.appThreadId === base.thread.id &&
1156 !isQueuedProviderThreadPlaceholder(event.payload)
1157 ? {
1158 ...base.thread,
1159 activeProviderThreadId: event.payload.id,
1160 }
1161 : base.thread,
1162 providerThreads: upsertById(
1163 base.providerThreads,
1164 attributePendingBackgroundTasks(
1165 event.payload,
1166 base.providerThreads.find((thread) => thread.id === event.payload.id),
1167 event.runId,
1168 ),
1169 ),
1170 };
1171 case "provider-turn.updated":
1172 return {
1173 ...base,
1174 providerTurns: upsertProviderTurn(base.providerTurns, event.payload),
1175 };
1176 case "runtime-request.updated":
1177 return {
1178 ...base,
1179 runtimeRequests: upsertById(base.runtimeRequests, event.payload),
1180 };
1181 case "message.updated":
1182 return {
1183 ...base,
1184 messages: upsertById(base.messages, event.payload),
1185 };
1186 case "turn-item.updated":
1187 return withLocalVisibleTurnItems({
1188 ...base,
1189 turnItems: upsertById(base.turnItems, event.payload),
1190 });
1191 case "plan.updated":
1192 return {
1193 ...base,
1194 plans: upsertById(base.plans, event.payload),
1195 };
1196 case "checkpoint-scope.created":
1197 return {
1198 ...base,
1199 checkpointScopes: upsertById(base.checkpointScopes, event.payload),
1200 };
1201 case "checkpoint.captured":
1202 return {
1203 ...base,
1204 checkpoints: upsertById(base.checkpoints, event.payload),
1205 };
1206 case "checkpoint.rollback-requested":
1207 return base;
1208 case "context-handoff.updated":
1209 return {
1210 ...base,
1211 contextHandoffs: upsertById(base.contextHandoffs, event.payload),
1212 };
1213 case "context-transfer.created":
1214 case "context-transfer.updated":
1215 return {
1216 ...base,
1217 contextTransfers: upsertById(base.contextTransfers, event.payload),
1218 };
1219 }
1220 }
1221
1222 type PayloadRow = {
1223 readonly payload_json: string;
1224 };
1225
1226 type ShellPendingTurnItemRow = PayloadRow & {
1227 readonly thread_id: string;
1228 readonly owning_provider_instance_id: string | null;
1229 };
1230
1231 export type AgentAwarenessThread = ProjectThreadAwarenessV2Input["thread"] &
1232 Pick<
1233 OrchestrationV2ThreadShell,
1234 | "projectId"
1235 | "creationSource"
1236 | "latestRunCompletedAt"
1237 | "archivedAt"
1238 | "snoozedUntil"
1239 | "snoozedAt"
1240 | "pendingGuestMessageCount"
1241 | "activeRunId"
1242 | "latestRunId"
1243 | "hasActionableProposedPlan"
1244 >;
1245
1246 type ShellThreadRow = {
1247 readonly thread_id: string;
1248 readonly payload_json: string;
1249 readonly forked_from_run_source_thread_id: string | null;
1250 readonly latest_run_id: string | null;
1251 readonly latest_run_status: string | null;
1252 readonly latest_run_requested_at: string | null;
1253 readonly latest_run_started_at: string | null;
1254 readonly latest_run_completed_at: string | null;
1255 readonly latest_run_stop_requested: number;
1256 readonly latest_run_background_wake: number;
1257 readonly active_run_id: string | null;
1258 readonly active_run_status: string | null;
1259 readonly activity_run_status: string | null;
1260 readonly activity_run_started_at: string | null;
1261 readonly last_error: string | null;
1262 readonly terminal_failure_payload_json: string | null;
1263 readonly blocking_run_id: string | null;
1264 readonly blocking_run_requested_at: string | null;
1265 readonly blocking_run_started_at: string | null;
1266 readonly blocking_run_completed_at: string | null;
1267 readonly blocking_run_stop_requested: number;
1268 readonly blocking_run_background_wake: number;
1269 readonly blocking_failure_payload_json: string | null;
1270 readonly pending_request_payload_json: string | null;
1271 readonly pending_secret_request_payload_json: string | null;
1272 readonly pending_link_request_payload_json: string | null;
1273 readonly latest_user_message_at: string | null;
1274 readonly latest_user_authored_message_at: string | null;
1275 readonly has_actionable_proposed_plan: number;
1276 };
1277
1278 type ShellRunRow = {
1279 readonly thread_id: string;
1280 readonly run_id: string;
1281 readonly ordinal: number;
1282 readonly rolled_back_at: string | null;
1283 };
1284
1285 type ShellRun = {
1286 readonly ordinal: number;
1287 readonly rolledBackAt: DateTime.Utc | null;
1288 };
1289
1290 type SettlementThreadRow = Pick<
1291 ShellThreadRow,
1292 | "thread_id"
1293 | "payload_json"
1294 | "latest_run_id"
1295 | "latest_run_status"
1296 | "latest_run_requested_at"
1297 | "latest_run_started_at"
1298 | "latest_run_completed_at"
1299 | "latest_user_message_at"
1300 | "latest_user_authored_message_at"
1301 >;
1302
1303 type ShellRunItemCountRow = {
1304 readonly thread_id: string;
1305 readonly run_id: string;
1306 readonly item_count: number;
1307 };
1308
1309 type ShellItemCountRow = {
1310 readonly thread_id: string;
1311 readonly item_count: number;
1312 readonly runless_item_count: number;
1313 };
1314
1315 const encodeTimelineSources = Schema.encodeEffect(
1316 Schema.fromJsonString(
1317 Schema.Array(Schema.Struct({ threadId: Schema.String, id: Schema.NullOr(Schema.String) })),
1318 ),
1319 );
1320 const shellIndexLineage = (row: {
1321 readonly relationship_to_parent: string | null;
1322 readonly parent_thread_id: string | null;
1323 readonly root_thread_id: string;
1324 readonly side_chat: number | null;
1325 }): OrchestrationV2ThreadShell["lineage"] => ({
1326 parentThreadId: row.parent_thread_id === null ? null : ThreadId.make(row.parent_thread_id),
1327 relationshipToParent:
1328 row.relationship_to_parent === "fork" || row.relationship_to_parent === "subagent"
1329 ? row.relationship_to_parent
1330 : null,
1331 rootThreadId: ThreadId.make(row.root_thread_id),
1332 ...(row.side_chat === null ? {} : { sideChat: row.side_chat === 1 }),
1333 });
1334
1335 const encodeIdList = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String)));
1336
1337 // Empty filters still scan SQLite rows; short-circuit them, including turnItemTypes: [].
1338 const matchesNothing = (...filters: ReadonlyArray<ReadonlyArray<unknown> | undefined>) =>
1339 filters.some((values) => values?.length === 0);
1340
1341 const encodeThreadPayload = Schema.encodeEffect(
1342 Schema.fromJsonString(OrchestrationV2AppThreadJsonSchema),
1343 );
1344 const encodeThreadUpdatedAt = Schema.encodeEffect(
1345 OrchestrationV2AppThreadJsonSchema.fields.updatedAt,
1346 );
1347 const encodeRunPayload = Schema.encodeEffect(Schema.fromJsonString(OrchestrationV2RunJsonSchema));
1348 const encodeRestartCancelledBackgroundWork = Schema.encodeEffect(
1349 Schema.fromJsonString(OrchestrationV2RunJsonSchema.fields.restartCancelledBackgroundWork),
1350 );
1351 const encodeRunOutput = Schema.encodeEffect(
1352 Schema.fromJsonString(OrchestrationV2RunJsonSchema.fields.output),
1353 );
1354 const encodeRunAttemptPayload = Schema.encodeEffect(
1355 Schema.fromJsonString(OrchestrationV2RunAttemptJsonSchema),
1356 );
1357 const encodeNodePayload = Schema.encodeEffect(
1358 Schema.fromJsonString(OrchestrationV2ExecutionNodeJsonSchema),
1359 );
1360 export const encodeSubagentPayload = Schema.encodeEffect(
1361 Schema.fromJsonString(OrchestrationV2SubagentJsonSchema),
1362 );
1363 const encodeProviderSessionPayload = Schema.encodeEffect(
1364 Schema.fromJsonString(OrchestrationV2ProviderSessionJsonSchema),
1365 );
1366 export const encodeProviderThreadPayload = Schema.encodeEffect(
1367 Schema.fromJsonString(OrchestrationV2ProviderThreadJsonSchema),
1368 );
1369 const encodeProviderTurnPayload = Schema.encodeEffect(
1370 Schema.fromJsonString(OrchestrationV2ProviderTurnJsonSchema),
1371 );
1372 const encodeRuntimeRequestPayload = Schema.encodeEffect(
1373 Schema.fromJsonString(OrchestrationV2RuntimeRequestJsonSchema),
1374 );
1375 const encodeMessagePayload = Schema.encodeEffect(
1376 Schema.fromJsonString(OrchestrationV2ConversationMessageJsonSchema),
1377 );
1378 const encodePlanPayload = Schema.encodeEffect(
1379 Schema.fromJsonString(OrchestrationV2PlanArtifactSchema),
1380 );
1381 export const encodeTurnItemPayload = Schema.encodeEffect(
1382 Schema.fromJsonString(OrchestrationV2TurnItemJsonSchema),
1383 );
1384 // The codec every *Json payload schema uses for timestamps, so index columns match payload_json.
1385 const encodePayloadTimestamp = Schema.encodeEffect(Schema.DateTimeUtcFromString);
1386 const encodeCheckpointScopePayload = Schema.encodeEffect(
1387 Schema.fromJsonString(OrchestrationV2CheckpointScopeJsonSchema),
1388 );
1389 const encodeCheckpointPayload = Schema.encodeEffect(
1390 Schema.fromJsonString(OrchestrationV2CheckpointJsonSchema),
1391 );
1392 const encodeContextHandoffPayload = Schema.encodeEffect(
1393 Schema.fromJsonString(OrchestrationV2ContextHandoffJsonSchema),
1394 );
1395 const encodeContextTransferPayload = Schema.encodeEffect(
1396 Schema.fromJsonString(OrchestrationV2ContextTransferJsonSchema),
1397 );
1398
1399 const PullRequestThread = OrchestrationV2AppThreadJsonSchema.mapFields(
1400 ({
1401 id,
1402 projectId,
1403 lineage,
1404 createdAt,
1405 updatedAt,
1406 branch,
1407 worktreePath,
1408 archivedAt,
1409 settledOverride,
1410 settledAt,
1411 linkedPullRequest,
1412 branchPullRequest,
1413 pullRequests,
1414 }) => ({
1415 id,
1416 projectId,
1417 lineage,
1418 createdAt,
1419 updatedAt,
1420 branch,
1421 worktreePath,
1422 archivedAt,
1423 settledOverride,
1424 settledAt,
1425 linkedPullRequest,
1426 branchPullRequest,
1427 pullRequests,
1428 }),
1429 );
1430 const decodePullRequestThread = Schema.decodeUnknownEffect(
1431 Schema.fromJsonString(PullRequestThread),
1432 );
1433
1434 const decodeThreadPayload = Schema.decodeUnknownEffect(
1435 Schema.fromJsonString(OrchestrationV2AppThreadJsonSchema),
1436 );
1437 const decodeRunPayload = Schema.decodeUnknownEffect(
1438 Schema.fromJsonString(OrchestrationV2RunJsonSchema),
1439 );
1440 const decodeRunAttemptPayload = Schema.decodeUnknownEffect(
1441 Schema.fromJsonString(OrchestrationV2RunAttemptJsonSchema),
1442 );
1443 const decodeNodePayload = Schema.decodeUnknownEffect(
1444 Schema.fromJsonString(OrchestrationV2ExecutionNodeJsonSchema),
1445 );
1446 const decodeSubagentPayload = Schema.decodeUnknownEffect(
1447 Schema.fromJsonString(OrchestrationV2SubagentJsonSchema),
1448 );
1449 const decodeProviderSessionPayload = Schema.decodeUnknownEffect(
1450 Schema.fromJsonString(OrchestrationV2ProviderSessionJsonSchema),
1451 );
1452 const decodeProviderThreadPayload = Schema.decodeUnknownEffect(
1453 Schema.fromJsonString(OrchestrationV2ProviderThreadJsonSchema),
1454 );
1455 const decodeProviderTurnPayload = Schema.decodeUnknownEffect(
1456 Schema.fromJsonString(OrchestrationV2ProviderTurnJsonSchema),
1457 );
1458 const decodeRuntimeRequestPayload = Schema.decodeUnknownEffect(
1459 Schema.fromJsonString(OrchestrationV2RuntimeRequestJsonSchema),
1460 );
1461 const decodeMessagePayload = Schema.decodeUnknownEffect(
1462 Schema.fromJsonString(OrchestrationV2ConversationMessageJsonSchema),
1463 );
1464 const decodePlanJson = Schema.decodeUnknownEffect(
1465 Schema.fromJsonString(OrchestrationV2PlanArtifactSchema),
1466 );
1467 const decodeTurnItemPayload = Schema.decodeUnknownEffect(
1468 Schema.fromJsonString(OrchestrationV2TurnItemJsonSchema),
1469 );
1470 const decodeCheckpointScopePayload = Schema.decodeUnknownEffect(
1471 Schema.fromJsonString(OrchestrationV2CheckpointScopeJsonSchema),
1472 );
1473 const decodeCheckpointPayload = Schema.decodeUnknownEffect(
1474 Schema.fromJsonString(OrchestrationV2CheckpointJsonSchema),
1475 );
1476 const decodeContextHandoffPayload = Schema.decodeUnknownEffect(
1477 Schema.fromJsonString(OrchestrationV2ContextHandoffJsonSchema),
1478 );
1479 const decodeContextTransferPayload = Schema.decodeUnknownEffect(
1480 Schema.fromJsonString(OrchestrationV2ContextTransferJsonSchema),
1481 );
1482
1483 const isProjectionStoreThreadNotFoundError = Schema.is(ProjectionStoreThreadNotFoundError);
1484 const isProjectionStoreReadError = Schema.is(ProjectionStoreReadError);
1485
1486 function parseEncodedPayload(json: string): Record<string, unknown> {
1487 return JSON.parse(json) as Record<string, unknown>;
1488 }
1489
1490 function stringField(payload: Record<string, unknown>, field: string): string {
1491 const value = payload[field];
1492 return typeof value === "string" ? value : String(value);
1493 }
1494
1495 function nullableStringField(payload: Record<string, unknown>, field: string): string | null {
1496 const value = payload[field];
1497 if (value === null || value === undefined) {
1498 return null;
1499 }
1500 return typeof value === "string" ? value : String(value);
1501 }
1502
1503 function booleanInt(value: boolean): 0 | 1 {
1504 return value ? 1 : 0;
1505 }
1506
1507 const decodeRows =
1508 <A, E>(decode: (json: string) => Effect.Effect<A, E>, threadId: ThreadId) =>
1509 (rows: ReadonlyArray<PayloadRow>): Effect.Effect<Array<A>, ProjectionStoreReadError> =>
1510 Effect.forEach(rows, (row) => decode(row.payload_json)).pipe(
1511 Effect.mapError(
1512 (cause) =>
1513 new ProjectionStoreReadError({
1514 threadId,
1515 cause,
1516 }),
1517 ),
1518 );
1519
1520 function messageIdForTurnItem(item: OrchestrationV2TurnItem): string | null {
1521 switch (item.type) {
1522 case "user_message":
1523 case "assistant_message":
1524 return item.messageId;
1525 default:
1526 return null;
1527 }
1528 }
1529
1530 function sortMessagesByTurnItemOrder(
1531 messages: ReadonlyArray<OrchestrationV2ConversationMessage>,
1532 turnItems: ReadonlyArray<OrchestrationV2TurnItem>,
1533 messageOrdinals = new Map<string, number>(),
1534 ): Array<OrchestrationV2ConversationMessage> {
1535 for (const turnItem of turnItems) {
1536 const messageId = messageIdForTurnItem(turnItem);
1537 if (messageId === null) {
1538 continue;
1539 }
1540 const existing = messageOrdinals.get(messageId);
1541 if (existing === undefined || turnItem.ordinal < existing) {
1542 messageOrdinals.set(messageId, turnItem.ordinal);
1543 }
1544 }
1545
1546 return messages.toSorted((left, right) => {
1547 const leftOrdinal = messageOrdinals.get(left.id) ?? Number.MAX_SAFE_INTEGER;
1548 const rightOrdinal = messageOrdinals.get(right.id) ?? Number.MAX_SAFE_INTEGER;
1549 if (leftOrdinal !== rightOrdinal) {
1550 return leftOrdinal - rightOrdinal;
1551 }
1552
1553 const leftCreatedAt = DateTime.toEpochMillis(left.createdAt);
1554 const rightCreatedAt = DateTime.toEpochMillis(right.createdAt);
1555 if (leftCreatedAt !== rightCreatedAt) {
1556 return leftCreatedAt - rightCreatedAt;
1557 }
1558
1559 return left.id.localeCompare(right.id);
1560 });
1561 }
1562
1563 function activeLocalTurnItems(
1564 projection: OrchestrationV2ThreadProjection,
1565 ): Array<OrchestrationV2ProjectedTurnItem> {
1566 const isVisible = createOrchestrationV2TurnItemVisibility({
1567 runs: projection.runs,
1568 attempts: projection.attempts,
1569 items: projection.turnItems,
1570 });
1571 return projection.turnItems.filter(isVisible).map((item, position) => ({
1572 position,
1573 visibility: "local" as const,
1574 sourceThreadId: item.threadId,
1575 sourceItemId: item.id,
1576 item,
1577 }));
1578 }
1579
1580 function localVisibleTurnItems(
1581 projection: OrchestrationV2ThreadProjection,
1582 ): Array<OrchestrationV2ProjectedTurnItem> {
1583 return activeLocalTurnItems(projection);
1584 }
1585
1586 function inheritedVisibleTurnItemsFromLocalItems(
1587 items: ReadonlyArray<OrchestrationV2TurnItem>,
1588 ): Array<Omit<OrchestrationV2ProjectedTurnItem, "position">> {
1589 return items.map((item) => ({
1590 visibility: "inherited" as const,
1591 sourceThreadId: item.threadId,
1592 sourceItemId: item.id,
1593 item,
1594 }));
1595 }
1596
1597 function withLocalVisibleTurnItems(
1598 projection: OrchestrationV2ThreadProjection,
1599 ): OrchestrationV2ThreadProjection {
1600 return {
1601 ...projection,
1602 visibleTurnItems: localVisibleTurnItems(projection),
1603 };
1604 }
1605
1606 function renumberVisibleTurnItems(
1607 rows: ReadonlyArray<Omit<OrchestrationV2ProjectedTurnItem, "position">>,
1608 offset = 0,
1609 ): Array<OrchestrationV2ProjectedTurnItem> {
1610 return rows.map((row, position) => ({ ...row, position: position + offset }));
1611 }
1612
1613 function makeForkMarkerTurnItem(input: {
1614 readonly targetProjection: Pick<OrchestrationV2ThreadProjection, "thread">;
1615 readonly sourceThreadId: ThreadId;
1616 readonly sourceRunId: NonNullable<OrchestrationV2TurnItem["runId"]>;
1617 }): Extract<OrchestrationV2TurnItem, { type: "fork" }> {
1618 const createdAt = input.targetProjection.thread.createdAt;
1619 return {
1620 id: TurnItemId.make(`turn-item:fork:${input.targetProjection.thread.id}`),
1621 threadId: input.targetProjection.thread.id,
1622 runId: null,
1623 nodeId: null,
1624 providerTurnId: null,
1625 nativeItemRef: null,
1626 parentItemId: null,
1627 ordinal: 0,
1628 status: "completed",
1629 title: "Forked from conversation",
1630 startedAt: null,
1631 completedAt: createdAt,
1632 updatedAt: createdAt,
1633 type: "fork",
1634 source: { type: "run", threadId: input.sourceThreadId, runId: input.sourceRunId },
1635 targetThreadId: input.targetProjection.thread.id,
1636 };
1637 }
1638
1639 function isTurnItemAtOrBeforeRun(input: {
1640 readonly historyOrigin: OrchestrationV2ThreadProjection["thread"]["historyOrigin"];
1641 readonly itemRunId: OrchestrationV2TurnItem["runId"];
1642 readonly runOrdinalById: ReadonlyMap<NonNullable<OrchestrationV2TurnItem["runId"]>, number>;
1643 readonly sourceRunOrdinal: number;
1644 }): boolean {
1645 if (input.itemRunId === null) {
1646 return input.historyOrigin === "v1_import";
1647 }
1648 const ordinal = input.runOrdinalById.get(input.itemRunId);
1649 return ordinal !== undefined && ordinal <= input.sourceRunOrdinal;
1650 }
1651
1652 // Run ordinals enforce the fork cutoff; this keeps later rollbacks from hiding
1653 // inherited history while retaining cancellation evidence for undelivered input.
1654 function createInheritedTurnItemVisibility(
1655 source: Parameters<typeof createOrchestrationV2TurnItemVisibility>[0],
1656 ) {
1657 const cancelledRunIds = new Set(
1658 source.attempts
1659 .filter((attempt) => attempt.status === "cancelled")
1660 .map((attempt) => attempt.runId),
1661 );
1662 return createOrchestrationV2TurnItemVisibility({
1663 ...source,
1664 runs: source.runs.flatMap((run) =>
1665 run.status !== "rolled_back"
1666 ? [run]
1667 : cancelledRunIds.has(run.id)
1668 ? [{ ...run, status: "cancelled" as const }]
1669 : [],
1670 ),
1671 });
1672 }
1673
1674 // Rollback stamps completedAt; a fork made after the rewind never received that run.
1675 function runRolledBackAt(run: Pick<OrchestrationV2Run, "status" | "completedAt">) {
1676 return run.status === "rolled_back" ? run.completedAt : null;
1677 }
1678
1679 function isRolledBackBeforeFork(
1680 rolledBackAt: DateTime.Utc | null,
1681 forkCreatedAt: DateTime.Utc,
1682 ): boolean {
1683 return rolledBackAt !== null && DateTime.isLessThan(rolledBackAt, forkCreatedAt);
1684 }
1685
1686 export function forkInheritedRunOrdinals(
1687 runs: ReadonlyArray<OrchestrationV2Run>,
1688 forkCreatedAt: DateTime.Utc,
1689 ): Map<RunId, number> {
1690 return new Map(
1691 runs
1692 .filter((run) => !isRolledBackBeforeFork(runRolledBackAt(run), forkCreatedAt))
1693 .map((run) => [run.id, run.ordinal]),
1694 );
1695 }
1696
1697 function visibleTurnItemsThroughRun(input: {
1698 readonly sourceProjection: OrchestrationV2ThreadProjection;
1699 readonly sourceRunId: NonNullable<OrchestrationV2TurnItem["runId"]>;
1700 readonly forkCreatedAt: DateTime.Utc;
1701 }): Array<Omit<OrchestrationV2ProjectedTurnItem, "position">> {
1702 const sourceRun = input.sourceProjection.runs.find((run) => run.id === input.sourceRunId);
1703 if (sourceRun === undefined) {
1704 return [];
1705 }
1706
1707 const runOrdinalById = forkInheritedRunOrdinals(input.sourceProjection.runs, input.forkCreatedAt);
1708 const isVisible = createInheritedTurnItemVisibility({
1709 runs: input.sourceProjection.runs,
1710 attempts: input.sourceProjection.attempts,
1711 items: input.sourceProjection.turnItems,
1712 });
1713 const inheritedPrefix = input.sourceProjection.visibleTurnItems
1714 .filter(
1715 (row) => row.item.threadId !== input.sourceProjection.thread.id || row.item.type === "fork",
1716 )
1717 .map((row) => ({
1718 visibility: "inherited" as const,
1719 sourceThreadId: row.sourceThreadId,
1720 sourceItemId: row.sourceItemId,
1721 item: row.item,
1722 }));
1723 const localPrefix = inheritedVisibleTurnItemsFromLocalItems(
1724 input.sourceProjection.turnItems.filter(
1725 (item) =>
1726 isVisible(item) &&
1727 isTurnItemAtOrBeforeRun({
1728 historyOrigin: input.sourceProjection.thread.historyOrigin,
1729 itemRunId: item.runId,
1730 runOrdinalById,
1731 sourceRunOrdinal: sourceRun.ordinal,
1732 }),
1733 ),
1734 );
1735
1736 return [...inheritedPrefix, ...localPrefix];
1737 }
1738
1739 // Side chats show only their own rows; the fork transfer carries the parent's context to the model.
1740 function inheritedRunFork(thread: Pick<OrchestrationV2AppThread, "forkedFrom" | "lineage">) {
1741 return thread.forkedFrom?.type === "run" && thread.lineage.sideChat !== true
1742 ? thread.forkedFrom
1743 : null;
1744 }
1745
1746 function buildVisibleTurnItems(input: {
1747 readonly projection: OrchestrationV2ThreadProjection;
1748 readonly sourceProjection: OrchestrationV2ThreadProjection | null;
1749 readonly inheritedRowCount?: number;
1750 }): Array<OrchestrationV2ProjectedTurnItem> {
1751 const forkedFrom = inheritedRunFork(input.projection.thread);
1752 if (
1753 forkedFrom === null ||
1754 (input.sourceProjection === null && input.inheritedRowCount === undefined)
1755 ) {
1756 return localVisibleTurnItems(input.projection);
1757 }
1758
1759 const inherited =
1760 input.sourceProjection === null
1761 ? []
1762 : visibleTurnItemsThroughRun({
1763 sourceProjection: input.sourceProjection,
1764 sourceRunId: forkedFrom.runId,
1765 forkCreatedAt: input.projection.thread.createdAt,
1766 });
1767 const markerItem = makeForkMarkerTurnItem({
1768 targetProjection: input.projection,
1769 sourceThreadId: forkedFrom.threadId,
1770 sourceRunId: forkedFrom.runId,
1771 });
1772 const local = activeLocalTurnItems(input.projection).map((row) => ({
1773 visibility: "local" as const,
1774 sourceThreadId: row.sourceThreadId,
1775 sourceItemId: row.sourceItemId,
1776 item: row.item,
1777 }));
1778
1779 return renumberVisibleTurnItems(
1780 [
1781 ...inherited,
1782 {
1783 visibility: "synthetic",
1784 sourceThreadId: forkedFrom.threadId,
1785 sourceItemId: markerItem.id,
1786 item: markerItem,
1787 },
1788 ...local,
1789 ],
1790 input.inheritedRowCount ?? 0,
1791 );
1792 }
1793
1794 function pendingRuntimeRequestSummary(
1795 request: OrchestrationV2ThreadProjection["runtimeRequests"][number] | null,
1796 ): OrchestrationV2PendingRuntimeRequestSummary | null {
1797 if (request === null) return null;
1798 return {
1799 id: request.id,
1800 kind: request.kind,
1801 createdAt: request.createdAt,
1802 blocking: request.responseCapability.type !== "message",
1803 };
1804 }
1805
1806 function secretRequestAsPendingInput(
1807 item: OrchestrationV2TurnItem | null,
1808 ): OrchestrationV2PendingRuntimeRequestSummary | null {
1809 if ((item?.type !== "secret_request" && item?.type !== "link_request") || item.nodeId === null)
1810 return null;
1811 return {
1812 id: RuntimeRequestId.make(item.id),
1813 kind: "user_input",
1814 createdAt: item.startedAt ?? item.updatedAt,
1815 blocking: true,
1816 userOnly: true,
1817 };
1818 }
1819
1820 /**
1821 * A secret card is answered while its agent waits in the run. A link card's
1822 * agent asked without waiting, so the card waits on the user after its run,
1823 * until the user writes in the thread again and so has moved past it.
1824 */
1825 const waitsOnUser = (
1826 item: OrchestrationV2TurnItem,
1827 liveRunIds: ReadonlySet<RunId>,
1828 latestUserAuthoredMessageAt: DateTime.Utc | null,
1829 ) =>
1830 (item.type === "link_request" && linkCardStillAsks(item, latestUserAuthoredMessageAt)) ||
1831 (item.type === "secret_request" && item.runId !== null && liveRunIds.has(item.runId));
1832
1833 const linkCardStillAsks = (
1834 item: OrchestrationV2TurnItem,
1835 latestUserAuthoredMessageAt: DateTime.Utc | null,
1836 ) =>
1837 latestUserAuthoredMessageAt === null ||
1838 !DateTime.isGreaterThan(latestUserAuthoredMessageAt, item.startedAt ?? item.updatedAt);
1839
1840 export function threadShellFromProjection(
1841 projection: OrchestrationV2ThreadProjection,
1842 ): OrchestrationV2ThreadShell {
1843 const providerSession =
1844 projection.providerSessions
1845 .filter((session) => session.providerInstanceId === projection.thread.providerInstanceId)
1846 .toSorted(
1847 (left, right) =>
1848 DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
1849 )[0] ?? null;
1850 // T3 Code's own idle compactions stay out of the shell unless they fail, so thread lists,
1851 // notifications and the relay keep presenting the user's last turn.
1852 const runs = projection.runs.filter((run) => !isSilentRunForShell(run, projection));
1853 const latestRun =
1854 usageLimitRunPresentedAsLatest(
1855 runs,
1856 projection.turnItems,
1857 providerSession?.lastError ?? null,
1858 ) ?? latestUnheldRun(runs);
1859 const activeRun =
1860 runs
1861 .filter(isInterruptibleRunForShell)
1862 .toSorted((left, right) => right.ordinal - left.ordinal)[0] ?? null;
1863 const activityRun =
1864 runs.filter(isActivityRunForShell).toSorted((left, right) => right.ordinal - left.ordinal)[0] ??
1865 null;
1866 const liveRunIds = new Set(runs.filter(isActivityRunForShell).map((run) => run.id));
1867 let pendingRuntimeRequest = pendingRuntimeRequestSummary(
1868 projection.runtimeRequests
1869 .filter((request) => request.status === "pending")
1870 .toSorted(
1871 (left, right) =>
1872 Number(left.kind === "user_input") - Number(right.kind === "user_input") ||
1873 DateTime.toEpochMillis(right.createdAt) - DateTime.toEpochMillis(left.createdAt),
1874 )[0] ?? null,
1875 );
1876
1877 const userMessages = projection.messages
1878 .filter((message) => message.role === "user" && idleCompactionOf(message) === undefined)
1879 .toSorted(
1880 (left, right) =>
1881 DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
1882 );
1883 const latestUserAuthoredMessageAt =
1884 userMessages.find((message) => message.createdBy === "user")?.updatedAt ?? null;
1885 if (pendingRuntimeRequest === null || pendingRuntimeRequest.kind === "user_input") {
1886 const waitingCard = secretRequestAsPendingInput(
1887 projection.turnItems
1888 .filter(
1889 (item) =>
1890 item.status === "waiting" && waitsOnUser(item, liveRunIds, latestUserAuthoredMessageAt),
1891 )
1892 .toSorted(
1893 (left, right) =>
1894 DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
1895 )[0] ?? null,
1896 );
1897 if (
1898 waitingCard !== null &&
1899 (pendingRuntimeRequest === null ||
1900 DateTime.toEpochMillis(waitingCard.createdAt) >=
1901 DateTime.toEpochMillis(pendingRuntimeRequest.createdAt))
1902 ) {
1903 pendingRuntimeRequest = waitingCard;
1904 }
1905 }
1906 const latestUserMessage = userMessages[0] ?? null;
1907 const pendingBackgroundTasks = derivePendingBackgroundWork({
1908 latestRun,
1909 providerThreads: projection.providerThreads,
1910 turnItems: projection.turnItems,
1911 activeProviderThreadId: projection.thread.activeProviderThreadId,
1912 runs,
1913 pullRequests: projection.thread.pullRequests,
1914 });
1915 return {
1916 createdBy: projection.thread.createdBy,
1917 creationSource: projection.thread.creationSource,
1918 id: projection.thread.id,
1919 projectId: projection.thread.projectId,
1920 title: projection.thread.title,
1921 providerInstanceId: projection.thread.providerInstanceId,
1922 modelSelection: projection.thread.modelSelection,
1923 runtimeMode: projection.thread.runtimeMode,
1924 interactionMode: projection.thread.interactionMode,
1925 branch: projection.thread.branch,
1926 worktreePath: projection.thread.worktreePath,
1927 pullRequests: threadPullRequestsOf(projection.thread),
1928 ...(projection.thread.linkedPullRequest === undefined
1929 ? {}
1930 : { linkedPullRequest: projection.thread.linkedPullRequest }),
1931 ...(projection.thread.branchPullRequest === undefined
1932 ? {}
1933 : { branchPullRequest: projection.thread.branchPullRequest }),
1934 ...(projection.thread.issues === undefined ? {} : { issues: projection.thread.issues }),
1935 ...(projection.thread.activeOrderKey === undefined
1936 ? {}
1937 : { activeOrderKey: projection.thread.activeOrderKey }),
1938 lineage: projection.thread.lineage,
1939 forkedFrom: projection.thread.forkedFrom,
1940 activeProviderThreadId: projection.thread.activeProviderThreadId,
1941 ...(projection.thread.historyOrigin === undefined
1942 ? {}
1943 : { historyOrigin: projection.thread.historyOrigin }),
1944 ...(projection.thread.linkOrigin === undefined
1945 ? {}
1946 : { linkOrigin: projection.thread.linkOrigin }),
1947 ...(projection.thread.delegatedFrom === undefined
1948 ? {}
1949 : { delegatedFrom: projection.thread.delegatedFrom }),
1950 latestRunId: latestRun?.id ?? null,
1951 latestRunRequestedAt: latestRun?.requestedAt ?? null,
1952 latestRunStartedAt: latestRun?.startedAt ?? null,
1953 latestRunCompletedAt: latestRun?.completedAt ?? null,
1954 latestRunStopRequested:
1955 latestRun != null &&
1956 projection.turnItems.some(
1957 (item) => item.runId === latestRun.id && item.type === "run_interrupt_request",
1958 ),
1959 latestRunBackgroundWake:
1960 latestRun != null && isBackgroundWakeRunForShell(latestRun, projection),
1961 activeRunId: activeRun?.id ?? null,
1962 blockedOnThreadWait: deriveBlockedOnThreadWait({ activeRun, turnItems: projection.turnItems }),
1963 activityRunStatus: activityRun?.status ?? null,
1964 activityRunStartedAt:
1965 activityRun === null ? null : orchestrationV2RunWorkStartedAt(activityRun),
1966 status: latestRun?.status ?? "idle",
1967 ...threadErrorSummary(
1968 latestRootProviderFailure(latestRun, projection.turnItems),
1969 providerSession?.lastError ?? null,
1970 ),
1971 pendingRuntimeRequest,
1972 pendingGuestMessageCount: pendingGuestMessageCount(projection.thread.guestShare),
1973 // Thread detail owns message bodies. Keeping them out of shell rows makes
1974 // initial hydration and streaming updates independent of transcript size.
1975 latestVisibleMessage: null,
1976 latestUserMessageAt: latestUserMessage?.updatedAt ?? null,
1977 latestUserAuthoredMessageAt,
1978 hasActionableProposedPlan: projection.plans.some(
1979 (plan) => plan.kind === "proposed_plan" && plan.status === "active",
1980 ),
1981 pendingBackgroundTasks: [...pendingBackgroundTasks],
1982 providerInstanceHistory: providerInstanceHistoryForShell({
1983 threadId: projection.thread.id,
1984 providerThreads: projection.providerThreads,
1985 }),
1986 goal: activeProviderGoalForShell(
1987 projection.providerThreads,
1988 projection.thread.activeProviderThreadId,
1989 ),
1990 itemCount: activeLocalTurnItems(projection).length,
1991 visibleItemCount: projection.visibleTurnItems.length,
1992 createdAt: projection.thread.createdAt,
1993 updatedAt: projection.thread.updatedAt,
1994 archivedAt: projection.thread.archivedAt,
1995 settledOverride: projection.thread.settledOverride,
1996 settledAt: projection.thread.settledAt,
1997 unsettledAt: projection.thread.unsettledAt ?? null,
1998 snoozedUntil: projection.thread.snoozedUntil ?? null,
1999 snoozedAt: projection.thread.snoozedAt ?? null,
2000 pinnedAt: projection.thread.pinnedAt ?? null,
2001
2002 autoSettleDisabledAt: projection.thread.autoSettleDisabledAt ?? null,
2003 pinOrderKey: projection.thread.pinOrderKey ?? null,
2004 ...(projection.thread.tags?.length ? { tags: projection.thread.tags } : {}),
2005 lastVisitedAt: projection.thread.lastVisitedAt,
2006 opens: projection.thread.opens ?? null,
2007 titleRegeneration: projection.thread.titleRegeneration ?? null,
2008 lastTitleRegeneration: projection.thread.lastTitleRegeneration ?? null,
2009 limitRecovery: projection.thread.limitRecovery ?? null,
2010 deletedAt: projection.thread.deletedAt,
2011 };
2012 }
2013
2014 function providerInstanceHistoryForShell(input: {
2015 readonly threadId: ThreadId;
2016 readonly providerThreads: ReadonlyArray<
2017 OrchestrationV2ThreadProjection["providerThreads"][number]
2018 >;
2019 }): ReadonlyArray<ProviderInstanceId> {
2020 const history: Array<ProviderInstanceId> = [];
2021 for (const providerThread of input.providerThreads
2022 .filter((thread) => thread.appThreadId === input.threadId && thread.ownerNodeId === null)
2023 .toSorted(
2024 (left, right) =>
2025 DateTime.toEpochMillis(left.createdAt) - DateTime.toEpochMillis(right.createdAt) ||
2026 left.id.localeCompare(right.id),
2027 )) {
2028 if (!history.includes(providerThread.providerInstanceId)) {
2029 history.push(providerThread.providerInstanceId);
2030 }
2031 }
2032 return history;
2033 }
2034
2035 function activeProviderGoalForShell(
2036 providerThreads: ReadonlyArray<OrchestrationV2ThreadProjection["providerThreads"][number]>,
2037 activeProviderThreadId: OrchestrationV2ThreadProjection["thread"]["activeProviderThreadId"],
2038 ): OrchestrationV2ThreadShell["goal"] {
2039 return providerThreads.find((thread) => thread.id === activeProviderThreadId)?.goal ?? null;
2040 }
2041
2042 function isSilentRunForShell(
2043 run: OrchestrationV2ThreadProjection["runs"][number],
2044 projection: Pick<OrchestrationV2ThreadProjection, "messages" | "turnItems">,
2045 ): boolean {
2046 return (
2047 run.status !== "failed" &&
2048 projection.messages.some(
2049 (message) => message.id === run.userMessageId && idleCompactionOf(message) !== undefined,
2050 ) &&
2051 !projection.turnItems.some(
2052 (item) => item.runId === run.id && item.type === "compaction" && item.status === "failed",
2053 )
2054 );
2055 }
2056
2057 const BACKGROUND_WAKE_SOURCE_KINDS = new Set(["background_task", "command", "monitor"]);
2058
2059 function isBackgroundWakeRunForShell(
2060 run: OrchestrationV2ThreadProjection["runs"][number],
2061 projection: Pick<OrchestrationV2ThreadProjection, "messages">,
2062 ): boolean {
2063 const message = projection.messages.find((candidate) => candidate.id === run.userMessageId);
2064 return (
2065 message?.delegatedCompletion === undefined &&
2066 // Only progress wakes; a terminal one ("Command … finished") is the work reporting its end.
2067 message?.notification?.outcome === "updated" &&
2068 BACKGROUND_WAKE_SOURCE_KINDS.has(message.notification.source.kind) &&
2069 // A follow-up the user steered into the wake makes the run theirs.
2070 !projection.messages.some(
2071 (candidate) => candidate.runId === run.id && candidate.createdBy === "user",
2072 )
2073 );
2074 }
2075
2076 function isInterruptibleRunForShell(run: OrchestrationV2ThreadProjection["runs"][number]): boolean {
2077 return run.status === "preparing" || run.status === "starting" || run.status === "running";
2078 }
2079
2080 type ShellActivityRunStatus = "preparing" | "running" | "starting" | "waiting";
2081
2082 function isActivityRunForShell(
2083 run: OrchestrationV2ThreadProjection["runs"][number],
2084 ): run is OrchestrationV2ThreadProjection["runs"][number] & {
2085 readonly status: ShellActivityRunStatus;
2086 } {
2087 return isInterruptibleRunForShell(run) || run.status === "waiting";
2088 }
2089
2090 type ShellThreadState = {
2091 readonly thread: OrchestrationV2ThreadProjection["thread"];
2092 readonly latestRunId: RunId | null;
2093 readonly latestRunStatus: OrchestrationV2ShellThreadStatus;
2094 readonly latestRunRequestedAt: DateTime.Utc | null;
2095 readonly latestRunStartedAt: DateTime.Utc | null;
2096 readonly latestRunCompletedAt: DateTime.Utc | null;
2097 readonly latestRunStopRequested: boolean;
2098 readonly latestRunBackgroundWake: boolean;
2099 readonly activeRunId: RunId | null;
2100 readonly blockedOnThreadWait: boolean;
2101 readonly activityRunStatus: ShellActivityRunStatus | null;
2102 readonly activityRunStartedAt: DateTime.Utc | null;
2103 readonly lastError: string | null;
2104 readonly lastErrorClass: OrchestrationV2ThreadShell["lastErrorClass"];
2105 readonly lastErrorCode: OrchestrationV2ThreadShell["lastErrorCode"];
2106 readonly usageLimitResetAt: OrchestrationV2ThreadShell["usageLimitResetAt"];
2107 readonly pendingRuntimeRequest: OrchestrationV2PendingRuntimeRequestSummary | null;
2108 readonly latestUserMessageAt: DateTime.Utc | null;
2109 readonly latestUserAuthoredMessageAt: DateTime.Utc | null;
2110 readonly hasActionableProposedPlan: boolean;
2111 readonly pendingBackgroundTasks: OrchestrationV2ThreadShell["pendingBackgroundTasks"];
2112 readonly providerInstanceHistory: OrchestrationV2ThreadShell["providerInstanceHistory"];
2113 readonly goal: OrchestrationV2ThreadShell["goal"];
2114 readonly itemCount: number;
2115 readonly runlessItemCount: number;
2116 readonly updatedAt: OrchestrationV2ThreadProjection["updatedAt"];
2117 readonly runsById: ReadonlyMap<RunId, ShellRun>;
2118 readonly itemCountByRunId: ReadonlyMap<RunId, number>;
2119 };
2120
2121 function shellStatusFromStoredRunStatus(status: string | null): OrchestrationV2ShellThreadStatus {
2122 switch (status) {
2123 case null:
2124 return "idle";
2125 case "preparing":
2126 case "queued":
2127 case "starting":
2128 case "running":
2129 case "waiting":
2130 case "completed":
2131 case "interrupted":
2132 case "failed":
2133 case "cancelled":
2134 case "rolled_back":
2135 return status;
2136 default:
2137 return "failed";
2138 }
2139 }
2140
2141 function itemCountThroughRun(input: {
2142 readonly state: ShellThreadState;
2143 readonly runId: RunId;
2144 readonly forkCreatedAt: DateTime.Utc;
2145 }): number {
2146 const runOrdinal = input.state.runsById.get(input.runId)?.ordinal;
2147 if (runOrdinal === undefined) {
2148 return 0;
2149 }
2150
2151 let count = input.state.thread.historyOrigin === "v1_import" ? input.state.runlessItemCount : 0;
2152 for (const [runId, itemCount] of input.state.itemCountByRunId) {
2153 const itemRun = input.state.runsById.get(runId);
2154 if (
2155 itemRun !== undefined &&
2156 itemRun.ordinal <= runOrdinal &&
2157 !isRolledBackBeforeFork(itemRun.rolledBackAt, input.forkCreatedAt)
2158 ) {
2159 count += itemCount;
2160 }
2161 }
2162 return count;
2163 }
2164
2165 function shellForkSourceIds(
2166 rows: ReadonlyArray<{ readonly forked_from_run_source_thread_id: string | null }>,
2167 ): ReadonlyArray<ThreadId> {
2168 return [
2169 ...new Set(
2170 rows.flatMap((row) =>
2171 row.forked_from_run_source_thread_id === null
2172 ? []
2173 : [ThreadId.make(row.forked_from_run_source_thread_id)],
2174 ),
2175 ),
2176 ];
2177 }
2178
2179 function visibleItemCountForShell(input: {
2180 readonly threadId: ThreadId;
2181 readonly statesByThreadId: ReadonlyMap<ThreadId, ShellThreadState>;
2182 readonly seenThreadIds?: ReadonlySet<ThreadId>;
2183 }): number {
2184 const state = input.statesByThreadId.get(input.threadId);
2185 if (state === undefined) {
2186 return 0;
2187 }
2188
2189 const forkedFrom = inheritedRunFork(state.thread);
2190 if (forkedFrom === null) {
2191 return state.itemCount;
2192 }
2193
2194 const seenThreadIds = input.seenThreadIds ?? new Set<ThreadId>();
2195 if (seenThreadIds.has(state.thread.id)) {
2196 return state.itemCount;
2197 }
2198
2199 const sourceState = input.statesByThreadId.get(forkedFrom.threadId);
2200 if (sourceState === undefined) {
2201 return state.itemCount;
2202 }
2203
2204 const inheritedPrefixCount =
2205 inheritedRunFork(sourceState.thread) !== null
2206 ? visibleItemCountForShell({
2207 threadId: sourceState.thread.id,
2208 statesByThreadId: input.statesByThreadId,
2209 seenThreadIds: new Set([...seenThreadIds, state.thread.id]),
2210 }) - sourceState.itemCount
2211 : 0;
2212
2213 return (
2214 inheritedPrefixCount +
2215 itemCountThroughRun({
2216 state: sourceState,
2217 runId: forkedFrom.runId,
2218 forkCreatedAt: state.thread.createdAt,
2219 }) +
2220 1 +
2221 state.itemCount
2222 );
2223 }
2224
2225 function shellFromState(input: {
2226 readonly state: ShellThreadState;
2227 readonly visibleItemCount: number;
2228 }): OrchestrationV2ThreadShell {
2229 return {
2230 createdBy: input.state.thread.createdBy,
2231 creationSource: input.state.thread.creationSource,
2232 id: input.state.thread.id,
2233 projectId: input.state.thread.projectId,
2234 title: input.state.thread.title,
2235 providerInstanceId: input.state.thread.providerInstanceId,
2236 modelSelection: input.state.thread.modelSelection,
2237 runtimeMode: input.state.thread.runtimeMode,
2238 interactionMode: input.state.thread.interactionMode,
2239 branch: input.state.thread.branch,
2240 worktreePath: input.state.thread.worktreePath,
2241 pullRequests: threadPullRequestsOf(input.state.thread),
2242 ...(input.state.thread.linkedPullRequest === undefined
2243 ? {}
2244 : { linkedPullRequest: input.state.thread.linkedPullRequest }),
2245 ...(input.state.thread.branchPullRequest === undefined
2246 ? {}
2247 : { branchPullRequest: input.state.thread.branchPullRequest }),
2248 ...(input.state.thread.issues === undefined ? {} : { issues: input.state.thread.issues }),
2249 ...(input.state.thread.activeOrderKey === undefined
2250 ? {}
2251 : { activeOrderKey: input.state.thread.activeOrderKey }),
2252 lineage: input.state.thread.lineage,
2253 forkedFrom: input.state.thread.forkedFrom,
2254 activeProviderThreadId: input.state.thread.activeProviderThreadId,
2255 ...(input.state.thread.historyOrigin === undefined
2256 ? {}
2257 : { historyOrigin: input.state.thread.historyOrigin }),
2258 ...(input.state.thread.linkOrigin === undefined
2259 ? {}
2260 : { linkOrigin: input.state.thread.linkOrigin }),
2261 ...(input.state.thread.delegatedFrom === undefined
2262 ? {}
2263 : { delegatedFrom: input.state.thread.delegatedFrom }),
2264 latestRunId: input.state.latestRunId,
2265 latestRunRequestedAt: input.state.latestRunRequestedAt,
2266 latestRunStartedAt: input.state.latestRunStartedAt,
2267 latestRunCompletedAt: input.state.latestRunCompletedAt,
2268 latestRunStopRequested: input.state.latestRunStopRequested,
2269 latestRunBackgroundWake: input.state.latestRunBackgroundWake,
2270 activeRunId: input.state.activeRunId,
2271 blockedOnThreadWait: input.state.blockedOnThreadWait,
2272 activityRunStatus: input.state.activityRunStatus,
2273 activityRunStartedAt: input.state.activityRunStartedAt,
2274 status: input.state.latestRunStatus,
2275 lastError: input.state.lastError,
2276 lastErrorClass: input.state.lastErrorClass,
2277 lastErrorCode: input.state.lastErrorCode,
2278 usageLimitResetAt: input.state.usageLimitResetAt,
2279 pendingRuntimeRequest: input.state.pendingRuntimeRequest,
2280 pendingGuestMessageCount: pendingGuestMessageCount(input.state.thread.guestShare),
2281 latestVisibleMessage: null,
2282 latestUserMessageAt: input.state.latestUserMessageAt,
2283 latestUserAuthoredMessageAt: input.state.latestUserAuthoredMessageAt,
2284 hasActionableProposedPlan: input.state.hasActionableProposedPlan,
2285 pendingBackgroundTasks: input.state.pendingBackgroundTasks,
2286 providerInstanceHistory: input.state.providerInstanceHistory,
2287 goal: input.state.goal,
2288 itemCount: input.state.itemCount,
2289 visibleItemCount: input.visibleItemCount,
2290 createdAt: input.state.thread.createdAt,
2291 updatedAt: input.state.updatedAt,
2292 archivedAt: input.state.thread.archivedAt,
2293 settledOverride: input.state.thread.settledOverride,
2294 settledAt: input.state.thread.settledAt,
2295 unsettledAt: input.state.thread.unsettledAt ?? null,
2296 snoozedUntil: input.state.thread.snoozedUntil ?? null,
2297 snoozedAt: input.state.thread.snoozedAt ?? null,
2298 pinnedAt: input.state.thread.pinnedAt ?? null,
2299
2300 autoSettleDisabledAt: input.state.thread.autoSettleDisabledAt ?? null,
2301 pinOrderKey: input.state.thread.pinOrderKey ?? null,
2302 ...(input.state.thread.tags?.length ? { tags: input.state.thread.tags } : {}),
2303 lastVisitedAt: input.state.thread.lastVisitedAt,
2304 opens: input.state.thread.opens ?? null,
2305 titleRegeneration: input.state.thread.titleRegeneration ?? null,
2306 lastTitleRegeneration: input.state.thread.lastTitleRegeneration ?? null,
2307 limitRecovery: input.state.thread.limitRecovery ?? null,
2308 deletedAt: input.state.thread.deletedAt,
2309 };
2310 }
2311
2312 // Dynamic tool output the client renders inline (HTML, MCP apps, viewed images, delegated
2313 // tasks, messages sent to the user), so its work group never defers. Mirrors the LIKE backfill
2314 // in migration 095, where a tool name counts anywhere after the first toolName. Searching from
2315 // that first one once avoids rescanning the payload for every nested toolName.
2316 const INLINE_WORK_OUTPUT = new RegExp(
2317 `htmlRender|${MCP_APP_OUTPUT_KEY}|"viewedImagePath"|sentMessage`,
2318 "i",
2319 );
2320 const TOOL_NAME_PREFIX = /"toolName":"/i;
2321 const INLINE_TOOL_NAME = /(?:delegate.task|send.to.user)"/is;
2322
2323 const isInlineWorkOutput = (payloadJson: string) => {
2324 if (INLINE_WORK_OUTPUT.test(payloadJson)) return true;
2325 const toolName = TOOL_NAME_PREFIX.exec(payloadJson);
2326 return (
2327 toolName !== null &&
2328 INLINE_TOOL_NAME.test(payloadJson.slice(toolName.index + toolName[0].length))
2329 );
2330 };
2331
2332 export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = Layer.effect(
2333 ProjectionStoreV2,
2334 Effect.gen(function* () {
2335 const sql = yield* SqlClient.SqlClient;
2336
2337 // SQLite uses the partial conversation index of migration 082 only for this exact predicate.
2338 const CONVERSATION_INDEX_EXCLUDED_TYPES = sql.literal(
2339 `(${DEFERRED_WORK_ITEM_TYPES.map((type) => `'${type}'`).join(", ")})`,
2340 );
2341
2342 // Other node indexes also start with thread_id. Without this hint SQLite reads a thread's live
2343 // nodes through one of them and fetches every node of the thread to check its status. The
2344 // query must filter on the same status list as the index of migration 060.
2345 const LIVE_NODES_INDEX = sql.literal("INDEXED BY orchestration_v2_projection_nodes_live_idx");
2346
2347 // An ended task whose completion was acknowledged, delivered or disposed, outside an active run.
2348 const settledSubagent = (row: Statement.Fragment) => sql`(
2349 ${row}.status IN ('completed', 'failed', 'cancelled', 'interrupted')
2350 AND COALESCE(${row}.completion_delivery_state, '') IN ('acknowledged', 'delivered', 'disposed')
2351 AND NOT EXISTS (
2352 SELECT 1 FROM orchestration_v2_projection_runs AS run
2353 WHERE run.run_id = ${row}.run_id
2354 AND run.status IN ('preparing', 'queued', 'starting', 'running', 'waiting')
2355 )
2356 )`;
2357
2358 // The settled index holds every column the check reads, so the settled tasks are dropped
2359 // there and their rows, each with a full prompt, stay unread.
2360 const unsettledSubagentRowids = (threadId: ThreadId) => sql`
2361 SELECT candidate.rowid
2362 FROM orchestration_v2_projection_subagents AS candidate
2363 INDEXED BY orchestration_v2_projection_subagents_settled_idx
2364 WHERE candidate.thread_id = ${threadId}
2365 AND NOT ${settledSubagent(sql`candidate`)}
2366 `;
2367
2368 // A todo list is never closed when its run ends, so a long thread holds an active one per
2369 // old run. Counting those as live work drags each run's nodes and subagents into every window.
2370 const todoListOfSettledRun = (row: Statement.Fragment) => sql`(
2371 ${row}.kind = 'todo_list' AND ${row}.run_id IS NOT NULL AND NOT EXISTS (
2372 SELECT 1 FROM orchestration_v2_projection_runs AS todo_run
2373 WHERE todo_run.run_id = ${row}.run_id
2374 AND todo_run.status IN ('queued','preparing','starting','running','waiting')
2375 ))`;
2376
2377 // Start from the few owned provider threads; a thread can have a large settled node history.
2378 const nodesOwningProviderThreads = (threadId: ThreadId) => sql`
2379 SELECT owner.node_id
2380 FROM orchestration_v2_projection_provider_threads AS owned
2381 INDEXED BY orchestration_v2_projection_provider_threads_owner_idx
2382 CROSS JOIN orchestration_v2_projection_nodes AS owner
2383 ON owner.node_id = owned.owner_node_id
2384 WHERE owned.owner_node_id IS NOT NULL AND owner.thread_id = ${threadId}
2385 `;
2386
2387 const keepRecordedRunField = (payload: Statement.Fragment, path: string) => sql`
2388 CASE
2389 WHEN json_type(excluded.payload_json, ${path}) IS NULL
2390 AND json_type(orchestration_v2_projection_runs.payload_json, ${path}) IS NOT NULL
2391 THEN json_set(
2392 ${payload},
2393 ${path},
2394 json_extract(orchestration_v2_projection_runs.payload_json, ${path})
2395 )
2396 ELSE ${payload}
2397 END
2398 `;
2399 // Only a submission sets the output; a lifecycle write carries it as it was when read.
2400 const keepRecordedRunOutput = (payload: Statement.Fragment) => sql`
2401 CASE
2402 WHEN json_type(orchestration_v2_projection_runs.payload_json, '$.output') IS NOT NULL
2403 THEN json_set(
2404 ${payload},
2405 '$.output',
2406 json_extract(orchestration_v2_projection_runs.payload_json, '$.output')
2407 )
2408 ELSE ${payload}
2409 END
2410 `;
2411
2412 type TimelineIndexItem = Pick<
2413 OrchestrationV2TurnItem,
2414 "id" | "threadId" | "runId" | "nodeId" | "parentItemId" | "type" | "status" | "ordinal"
2415 > & {
2416 readonly inputIntent?: Extract<
2417 OrchestrationV2TurnItem,
2418 { type: "user_message" }
2419 >["inputIntent"];
2420 readonly createdBy?: "user" | "agent" | "system";
2421 readonly messageId?: MessageId;
2422 readonly runStatus?: string | undefined;
2423 readonly attemptId?: string | null;
2424 readonly attemptStatus?: string | null;
2425 readonly persistentOutput?: boolean;
2426 readonly deferredWork?: OrchestrationV2TurnItem["deferredWork"];
2427 };
2428 type TimelineIndexRow = {
2429 readonly sourceThreadId: ThreadId;
2430 readonly sourceItemId: TurnItemId;
2431 readonly visibility: OrchestrationV2ProjectedTurnItem["visibility"];
2432 readonly item: TimelineIndexItem;
2433 readonly synthetic?: OrchestrationV2TurnItem;
2434 };
2435 type TimelineIndex = {
2436 readonly records: ProjectionRecords<"runs" | "attempts">;
2437 /** The thread and every fork source the index read. */
2438 readonly threadIds: ReadonlyArray<ThreadId>;
2439 readonly local: ReadonlyArray<TimelineIndexRow>;
2440 readonly visible: ReadonlyArray<TimelineIndexRow>;
2441 };
2442 // Least recently used first; a few cover clients scrolling side by side without pinning every thread.
2443 const TIMELINE_INDEX_CACHE_LIMIT = 8;
2444 const timelineIndexes = new Map<
2445 string,
2446 { readonly sequence: number; readonly index: TimelineIndex }
2447 >();
2448
2449 const apply: ProjectionStoreV2Shape["apply"] =
2450 /* This helper runs for individual orchestration events; the enclosing operation owns the span. */
2451 Effect.fnUntraced(
2452 function* (event) {
2453 for (const [key, cached] of timelineIndexes) {
2454 if (cached.index.threadIds.includes(event.threadId)) timelineIndexes.delete(key);
2455 }
2456 const batch = yield* ProviderBatchProjectionState;
2457 // These rewrite the thread row from what they read, so a deferred bump must land first.
2458 const rewrittenThreadId = event.type.startsWith("thread.")
2459 ? event.threadId
2460 : event.type === "provider-thread.updated"
2461 ? event.payload.appThreadId
2462 : null;
2463 const deferredUpdatedAt =
2464 rewrittenThreadId === null ? undefined : batch?.threadTouches.get(rewrittenThreadId);
2465 if (
2466 batch !== undefined &&
2467 rewrittenThreadId !== null &&
2468 deferredUpdatedAt !== undefined
2469 ) {
2470 batch.threadTouches.delete(rewrittenThreadId);
2471 yield* writeThreadTouches(sql, new Map([[rewrittenThreadId, deferredUpdatedAt]]));
2472 }
2473 switch (event.type) {
2474 case "thread.pull-request-link-updated": {
2475 const thread = yield* getThread(event.threadId);
2476 yield* apply({
2477 ...event,
2478 type: "thread.pull-request-synced",
2479 payload: applyThreadPullRequestLinkUpdate(thread, event.payload),
2480 });
2481 break;
2482 }
2483 case "thread.visit-recorded": {
2484 const thread = yield* getThread(event.threadId);
2485 yield* apply({
2486 ...event,
2487 type: "thread.visited",
2488 payload: applyOrchestrationV2ThreadVisit(thread, event.payload),
2489 });
2490 break;
2491 }
2492 case "thread.created":
2493 case "thread.archived":
2494 case "thread.unarchived":
2495 case "thread.deleted":
2496 case "thread.settled":
2497 case "thread.unsettled":
2498 case "thread.snoozed":
2499 case "thread.unsnoozed":
2500 case "thread.auto-settle-set":
2501 case "thread.pinned":
2502 case "thread.unpinned":
2503 case "thread.pin-reordered":
2504 case "thread.active-reordered":
2505 case "thread.tags-updated":
2506 case "thread.visited":
2507 case "thread.marked-unread":
2508 case "thread.metadata-updated":
2509 case "thread.pull-request-synced":
2510 case "thread.issue-synced":
2511 case "thread.runtime-mode-updated":
2512 case "thread.interaction-mode-updated":
2513 case "thread.model-selection-updated":
2514 case "thread.provider-switched":
2515 case "thread.guest-share-updated": {
2516 const payloadJson = yield* encodeThreadPayload(event.payload);
2517 const payload = parseEncodedPayload(payloadJson);
2518 yield* sql`
2519 INSERT INTO orchestration_v2_projection_threads (
2520 thread_id,
2521 project_id,
2522 title,
2523 default_provider,
2524 provider_instance_id,
2525 runtime_mode,
2526 interaction_mode,
2527 active_provider_thread_id,
2528 created_at,
2529 updated_at,
2530 archived_at,
2531 deleted_at,
2532 payload_json
2533 )
2534 VALUES (
2535 ${event.payload.id},
2536 ${event.payload.projectId},
2537 ${event.payload.title},
2538 ${event.payload.providerInstanceId},
2539 ${event.payload.providerInstanceId},
2540 ${event.payload.runtimeMode},
2541 ${event.payload.interactionMode},
2542 ${event.payload.activeProviderThreadId},
2543 ${stringField(payload, "createdAt")},
2544 ${stringField(payload, "updatedAt")},
2545 ${nullableStringField(payload, "archivedAt")},
2546 ${nullableStringField(payload, "deletedAt")},
2547 ${payloadJson}
2548 )
2549 ON CONFLICT(thread_id)
2550 DO UPDATE SET
2551 project_id = excluded.project_id,
2552 title = excluded.title,
2553 default_provider = excluded.default_provider,
2554 provider_instance_id = excluded.provider_instance_id,
2555 runtime_mode = excluded.runtime_mode,
2556 interaction_mode = excluded.interaction_mode,
2557 active_provider_thread_id = excluded.active_provider_thread_id,
2558 created_at = excluded.created_at,
2559 updated_at = excluded.updated_at,
2560 archived_at = excluded.archived_at,
2561 deleted_at = excluded.deleted_at,
2562 payload_json = excluded.payload_json
2563 `;
2564 break;
2565 }
2566 case "run.created":
2567 case "run.updated": {
2568 const payloadJson = yield* encodeRunPayload(event.payload);
2569 const payload = parseEncodedPayload(payloadJson);
2570 yield* sql`
2571 INSERT INTO orchestration_v2_projection_runs (
2572 run_id,
2573 thread_id,
2574 ordinal,
2575 provider,
2576 provider_instance_id,
2577 provider_thread_id,
2578 status,
2579 requested_at,
2580 completed_at,
2581 payload_json
2582 )
2583 VALUES (
2584 ${event.payload.id},
2585 ${event.payload.threadId},
2586 ${event.payload.ordinal},
2587 ${event.payload.providerInstanceId},
2588 ${event.payload.providerInstanceId},
2589 ${event.payload.providerThreadId},
2590 ${event.payload.status},
2591 ${stringField(payload, "requestedAt")},
2592 ${nullableStringField(payload, "completedAt")},
2593 ${payloadJson}
2594 )
2595 ON CONFLICT(run_id)
2596 DO UPDATE SET
2597 thread_id = excluded.thread_id,
2598 ordinal = excluded.ordinal,
2599 provider = excluded.provider,
2600 provider_instance_id = excluded.provider_instance_id,
2601 provider_thread_id = excluded.provider_thread_id,
2602 status = excluded.status,
2603 requested_at = excluded.requested_at,
2604 completed_at = excluded.completed_at,
2605 payload_json = ${keepRecordedRunOutput(
2606 keepRecordedRunField(
2607 keepRecordedRunField(sql`excluded.payload_json`, "$.delegatedCompletion"),
2608 "$.restartCancelledBackgroundWork",
2609 ),
2610 )}
2611 `;
2612 break;
2613 }
2614 case "run.background-work-cancelled": {
2615 // Only this field changes, so a concurrent lifecycle write is never regressed.
2616 const workJson = yield* encodeRestartCancelledBackgroundWork(
2617 event.payload.restartCancelledBackgroundWork,
2618 );
2619 yield* sql`
2620 UPDATE orchestration_v2_projection_runs
2621 SET payload_json = json_set(
2622 payload_json,
2623 '$.restartCancelledBackgroundWork',
2624 json(${workJson})
2625 )
2626 WHERE run_id = ${event.payload.runId} AND thread_id = ${event.threadId}
2627 `;
2628 break;
2629 }
2630 case "run.output-submitted": {
2631 // Only this field changes, so a concurrent lifecycle write is never regressed.
2632 const outputJson = yield* encodeRunOutput(event.payload.output);
2633 yield* sql`
2634 UPDATE orchestration_v2_projection_runs
2635 SET payload_json = json_set(payload_json, '$.output', json(${outputJson}))
2636 WHERE run_id = ${event.payload.runId} AND thread_id = ${event.threadId}
2637 `;
2638 break;
2639 }
2640 case "run-attempt.created":
2641 case "run-attempt.updated": {
2642 const payloadJson = yield* encodeRunAttemptPayload(event.payload);
2643 yield* sql`
2644 INSERT INTO orchestration_v2_projection_run_attempts (
2645 attempt_id,
2646 thread_id,
2647 run_id,
2648 attempt_ordinal,
2649 root_node_id,
2650 provider,
2651 provider_instance_id,
2652 provider_thread_id,
2653 provider_turn_id,
2654 status,
2655 payload_json
2656 )
2657 VALUES (
2658 ${event.payload.id},
2659 ${event.threadId},
2660 ${event.payload.runId},
2661 ${event.payload.attemptOrdinal},
2662 ${event.payload.rootNodeId},
2663 ${event.payload.providerInstanceId},
2664 ${event.payload.providerInstanceId},
2665 ${event.payload.providerThreadId},
2666 ${event.payload.providerTurnId},
2667 ${event.payload.status},
2668 ${payloadJson}
2669 )
2670 ON CONFLICT(attempt_id)
2671 DO UPDATE SET
2672 thread_id = excluded.thread_id,
2673 run_id = excluded.run_id,
2674 attempt_ordinal = excluded.attempt_ordinal,
2675 root_node_id = excluded.root_node_id,
2676 provider = excluded.provider,
2677 provider_instance_id = excluded.provider_instance_id,
2678 provider_thread_id = excluded.provider_thread_id,
2679 provider_turn_id = excluded.provider_turn_id,
2680 status = excluded.status,
2681 payload_json = excluded.payload_json
2682 `;
2683 break;
2684 }
2685 case "node.updated": {
2686 const payloadJson = yield* encodeNodePayload(event.payload);
2687 const payload = parseEncodedPayload(payloadJson);
2688 yield* sql`
2689 INSERT INTO orchestration_v2_projection_nodes (
2690 node_id,
2691 thread_id,
2692 run_id,
2693 parent_node_id,
2694 root_node_id,
2695 kind,
2696 status,
2697 provider_thread_id,
2698 provider_turn_id,
2699 started_at,
2700 completed_at,
2701 payload_json
2702 )
2703 VALUES (
2704 ${event.payload.id},
2705 ${event.payload.threadId},
2706 ${event.payload.runId},
2707 ${event.payload.parentNodeId},
2708 ${event.payload.rootNodeId},
2709 ${event.payload.kind},
2710 ${event.payload.status},
2711 ${event.payload.providerThreadId},
2712 ${event.payload.providerTurnId},
2713 ${nullableStringField(payload, "startedAt")},
2714 ${nullableStringField(payload, "completedAt")},
2715 ${payloadJson}
2716 )
2717 ON CONFLICT(node_id)
2718 DO UPDATE SET
2719 thread_id = excluded.thread_id,
2720 run_id = excluded.run_id,
2721 parent_node_id = excluded.parent_node_id,
2722 root_node_id = excluded.root_node_id,
2723 kind = excluded.kind,
2724 status = excluded.status,
2725 provider_thread_id = excluded.provider_thread_id,
2726 provider_turn_id = excluded.provider_turn_id,
2727 started_at = excluded.started_at,
2728 completed_at = excluded.completed_at,
2729 payload_json = excluded.payload_json
2730 `;
2731 batch?.nodeReads.set(event.payload.id, payloadJson);
2732 break;
2733 }
2734 case "subagent.updated": {
2735 const payloadJson = yield* encodeSubagentPayload(event.payload);
2736 const payload = parseEncodedPayload(payloadJson);
2737 yield* sql`
2738 INSERT INTO orchestration_v2_projection_subagents (
2739 subagent_id,
2740 thread_id,
2741 run_id,
2742 parent_node_id,
2743 provider,
2744 driver,
2745 provider_instance_id,
2746 provider_thread_id,
2747 child_thread_id,
2748 origin,
2749 status,
2750 started_at,
2751 completed_at,
2752 updated_at,
2753 completion_delivery_state,
2754 payload_json
2755 )
2756 VALUES (
2757 ${event.payload.id},
2758 ${event.payload.threadId},
2759 ${event.payload.runId},
2760 ${event.payload.parentNodeId},
2761 ${event.payload.providerInstanceId},
2762 ${event.payload.driver},
2763 ${event.payload.providerInstanceId},
2764 ${event.payload.providerThreadId},
2765 ${event.payload.childThreadId},
2766 ${event.payload.origin},
2767 ${event.payload.status},
2768 ${nullableStringField(payload, "startedAt")},
2769 ${nullableStringField(payload, "completedAt")},
2770 ${stringField(payload, "updatedAt")},
2771 ${event.payload.completionDelivery?.state ?? null},
2772 ${payloadJson}
2773 )
2774 ON CONFLICT(subagent_id)
2775 DO UPDATE SET
2776 thread_id = excluded.thread_id,
2777 run_id = excluded.run_id,
2778 parent_node_id = excluded.parent_node_id,
2779 provider = excluded.provider,
2780 driver = excluded.driver,
2781 provider_instance_id = excluded.provider_instance_id,
2782 provider_thread_id = excluded.provider_thread_id,
2783 child_thread_id = excluded.child_thread_id,
2784 origin = excluded.origin,
2785 status = excluded.status,
2786 started_at = excluded.started_at,
2787 completed_at = excluded.completed_at,
2788 updated_at = excluded.updated_at,
2789 completion_delivery_state = COALESCE(
2790 excluded.completion_delivery_state,
2791 orchestration_v2_projection_subagents.completion_delivery_state
2792 ),
2793 payload_json = CASE
2794 WHEN json_type(excluded.payload_json, '$.completionDelivery') IS NULL
2795 AND json_type(orchestration_v2_projection_subagents.payload_json, '$.completionDelivery') IS NOT NULL
2796 THEN json_set(
2797 excluded.payload_json,
2798 '$.completionDelivery',
2799 json_extract(
2800 orchestration_v2_projection_subagents.payload_json,
2801 '$.completionDelivery'
2802 )
2803 )
2804 ELSE excluded.payload_json
2805 END
2806 `;
2807 break;
2808 }
2809 case "provider-session.attached":
2810 case "provider-session.updated": {
2811 const payloadJson = yield* encodeProviderSessionPayload(event.payload);
2812 const payload = parseEncodedPayload(payloadJson);
2813 yield* sql`
2814 INSERT INTO orchestration_v2_projection_provider_sessions (
2815 provider_session_id,
2816 thread_id,
2817 provider,
2818 driver,
2819 provider_instance_id,
2820 status,
2821 updated_at,
2822 payload_json
2823 )
2824 VALUES (
2825 ${event.payload.id},
2826 ${event.threadId},
2827 ${event.payload.providerInstanceId},
2828 ${event.payload.driver},
2829 ${event.payload.providerInstanceId},
2830 ${event.payload.status},
2831 ${stringField(payload, "updatedAt")},
2832 ${payloadJson}
2833 )
2834 ON CONFLICT(provider_session_id)
2835 DO UPDATE SET
2836 thread_id = excluded.thread_id,
2837 provider = excluded.provider,
2838 driver = excluded.driver,
2839 provider_instance_id = excluded.provider_instance_id,
2840 status = excluded.status,
2841 updated_at = excluded.updated_at,
2842 payload_json = excluded.payload_json
2843 `;
2844 if (event.type === "provider-session.attached") {
2845 yield* sql`
2846 INSERT OR IGNORE INTO orchestration_v2_projection_provider_session_bindings (
2847 provider_session_id,
2848 thread_id
2849 )
2850 VALUES (${event.payload.id}, ${event.threadId})
2851 `;
2852 }
2853 break;
2854 }
2855 case "provider-session.detached": {
2856 yield* sql`
2857 DELETE FROM orchestration_v2_projection_provider_session_bindings
2858 WHERE provider_session_id = ${event.payload.providerSessionId}
2859 AND thread_id = ${event.threadId}
2860 `;
2861 break;
2862 }
2863 case "provider-thread.updated": {
2864 const existingRows = (event.payload.pendingBackgroundTasks ?? []).some(
2865 (task) => task.runId === undefined,
2866 )
2867 ? yield* sql<PayloadRow>`
2868 SELECT payload_json
2869 FROM orchestration_v2_projection_provider_threads
2870 WHERE provider_thread_id = ${event.payload.id}
2871 LIMIT 1
2872 `
2873 : [];
2874 const existing = existingRows[0];
2875 const payloadJson = yield* encodeProviderThreadPayload(
2876 attributePendingBackgroundTasks(
2877 event.payload,
2878 existing === undefined
2879 ? undefined
2880 : yield* decodeProviderThreadPayload(existing.payload_json),
2881 event.runId,
2882 ),
2883 );
2884 const payload = parseEncodedPayload(payloadJson);
2885 yield* sql`
2886 INSERT INTO orchestration_v2_projection_provider_threads (
2887 provider_thread_id,
2888 thread_id,
2889 owner_node_id,
2890 provider,
2891 driver,
2892 provider_instance_id,
2893 provider_session_id,
2894 status,
2895 first_run_ordinal,
2896 last_run_ordinal,
2897 updated_at,
2898 payload_json
2899 )
2900 VALUES (
2901 ${event.payload.id},
2902 ${event.payload.appThreadId},
2903 ${event.payload.ownerNodeId},
2904 ${event.payload.providerInstanceId},
2905 ${event.payload.driver},
2906 ${event.payload.providerInstanceId},
2907 ${event.payload.providerSessionId},
2908 ${event.payload.status},
2909 ${event.payload.firstRunOrdinal},
2910 ${event.payload.lastRunOrdinal},
2911 ${stringField(payload, "updatedAt")},
2912 ${payloadJson}
2913 )
2914 ON CONFLICT(provider_thread_id)
2915 DO UPDATE SET
2916 thread_id = excluded.thread_id,
2917 owner_node_id = excluded.owner_node_id,
2918 provider = excluded.provider,
2919 driver = excluded.driver,
2920 provider_instance_id = excluded.provider_instance_id,
2921 provider_session_id = excluded.provider_session_id,
2922 status = excluded.status,
2923 first_run_ordinal = excluded.first_run_ordinal,
2924 last_run_ordinal = excluded.last_run_ordinal,
2925 updated_at = excluded.updated_at,
2926 payload_json = excluded.payload_json
2927 `;
2928 if (
2929 event.payload.appThreadId !== null &&
2930 !isQueuedProviderThreadPlaceholder(event.payload)
2931 ) {
2932 const threadRows = yield* sql<PayloadRow>`
2933 SELECT payload_json
2934 FROM orchestration_v2_projection_threads
2935 WHERE thread_id = ${event.payload.appThreadId}
2936 LIMIT 1
2937 `;
2938 const threadRow = threadRows[0];
2939 if (threadRow !== undefined) {
2940 const thread = yield* decodeThreadPayload(threadRow.payload_json);
2941 const updatedThread = {
2942 ...thread,
2943 activeProviderThreadId: event.payload.id,
2944 updatedAt: event.payload.updatedAt,
2945 };
2946 const updatedThreadPayloadJson = yield* encodeThreadPayload(updatedThread);
2947 yield* sql`
2948 UPDATE orchestration_v2_projection_threads
2949 SET
2950 active_provider_thread_id = ${event.payload.id},
2951 updated_at = ${stringField(parseEncodedPayload(updatedThreadPayloadJson), "updatedAt")},
2952 payload_json = ${updatedThreadPayloadJson}
2953 WHERE thread_id = ${event.payload.appThreadId}
2954 `;
2955 }
2956 }
2957 break;
2958 }
2959 case "provider-turn.updated": {
2960 const existingRows =
2961 event.payload.tokenUsage === undefined
2962 ? yield* sql<PayloadRow>`
2963 SELECT payload_json
2964 FROM orchestration_v2_projection_provider_turns
2965 WHERE provider_turn_id = ${event.payload.id}
2966 LIMIT 1
2967 `
2968 : [];
2969 const existing = existingRows[0];
2970 const providerTurn =
2971 existing === undefined
2972 ? event.payload
2973 : upsertProviderTurn(
2974 [yield* decodeProviderTurnPayload(existing.payload_json)],
2975 event.payload,
2976 )[0]!;
2977 const payloadJson = yield* encodeProviderTurnPayload(providerTurn);
2978 const payload = parseEncodedPayload(payloadJson);
2979 yield* sql`
2980 INSERT INTO orchestration_v2_projection_provider_turns (
2981 provider_turn_id,
2982 thread_id,
2983 provider_thread_id,
2984 node_id,
2985 run_attempt_id,
2986 ordinal,
2987 status,
2988 started_at,
2989 completed_at,
2990 payload_json
2991 )
2992 VALUES (
2993 ${event.payload.id},
2994 ${event.threadId},
2995 ${providerTurn.providerThreadId},
2996 ${providerTurn.nodeId},
2997 ${providerTurn.runAttemptId},
2998 ${providerTurn.ordinal},
2999 ${providerTurn.status},
3000 ${nullableStringField(payload, "startedAt")},
3001 ${nullableStringField(payload, "completedAt")},
3002 ${payloadJson}
3003 )
3004 ON CONFLICT(provider_turn_id)
3005 DO UPDATE SET
3006 thread_id = excluded.thread_id,
3007 provider_thread_id = excluded.provider_thread_id,
3008 node_id = excluded.node_id,
3009 run_attempt_id = excluded.run_attempt_id,
3010 ordinal = excluded.ordinal,
3011 status = excluded.status,
3012 started_at = excluded.started_at,
3013 completed_at = excluded.completed_at,
3014 payload_json = excluded.payload_json
3015 `;
3016 break;
3017 }
3018 case "runtime-request.updated": {
3019 const payloadJson = yield* encodeRuntimeRequestPayload(event.payload);
3020 const payload = parseEncodedPayload(payloadJson);
3021 yield* sql`
3022 INSERT INTO orchestration_v2_projection_runtime_requests (
3023 runtime_request_id,
3024 thread_id,
3025 node_id,
3026 provider_turn_id,
3027 kind,
3028 status,
3029 created_at,
3030 resolved_at,
3031 payload_json
3032 )
3033 VALUES (
3034 ${event.payload.id},
3035 ${event.threadId},
3036 ${event.payload.nodeId},
3037 ${event.payload.providerTurnId},
3038 ${event.payload.kind},
3039 ${event.payload.status},
3040 ${stringField(payload, "createdAt")},
3041 ${nullableStringField(payload, "resolvedAt")},
3042 ${payloadJson}
3043 )
3044 ON CONFLICT(runtime_request_id)
3045 DO UPDATE SET
3046 thread_id = excluded.thread_id,
3047 node_id = excluded.node_id,
3048 provider_turn_id = excluded.provider_turn_id,
3049 kind = excluded.kind,
3050 status = excluded.status,
3051 created_at = excluded.created_at,
3052 resolved_at = excluded.resolved_at,
3053 payload_json = excluded.payload_json
3054 `;
3055 break;
3056 }
3057 case "message.updated": {
3058 const payloadJson = yield* encodeMessagePayload(event.payload);
3059 const createdAt = yield* encodePayloadTimestamp(event.payload.createdAt);
3060 const updatedAt = yield* encodePayloadTimestamp(event.payload.updatedAt);
3061 yield* sql`
3062 INSERT INTO orchestration_v2_projection_messages (
3063 message_id,
3064 thread_id,
3065 run_id,
3066 node_id,
3067 role,
3068 streaming,
3069 created_at,
3070 updated_at,
3071 payload_json,
3072 search_text
3073 )
3074 VALUES (
3075 ${event.payload.id},
3076 ${event.payload.threadId},
3077 ${event.payload.runId},
3078 ${event.payload.nodeId},
3079 ${event.payload.role},
3080 ${booleanInt(event.payload.streaming)},
3081 ${createdAt},
3082 ${updatedAt},
3083 ${payloadJson},
3084 ${readableSearchText(event.payload.text)}
3085 )
3086 ON CONFLICT(message_id)
3087 DO UPDATE SET
3088 thread_id = excluded.thread_id,
3089 run_id = excluded.run_id,
3090 node_id = excluded.node_id,
3091 role = excluded.role,
3092 streaming = excluded.streaming,
3093 created_at = excluded.created_at,
3094 updated_at = excluded.updated_at,
3095 payload_json = excluded.payload_json,
3096 search_text = excluded.search_text
3097 `;
3098 break;
3099 }
3100 case "plan.updated": {
3101 const payloadJson = yield* encodePlanPayload(event.payload);
3102 yield* sql`
3103 INSERT INTO orchestration_v2_projection_plans (
3104 plan_id,
3105 thread_id,
3106 run_id,
3107 node_id,
3108 kind,
3109 status,
3110 payload_json
3111 )
3112 VALUES (
3113 ${event.payload.id},
3114 ${event.payload.threadId},
3115 ${event.payload.runId},
3116 ${event.payload.nodeId},
3117 ${event.payload.kind},
3118 ${event.payload.status},
3119 ${payloadJson}
3120 )
3121 ON CONFLICT(plan_id)
3122 DO UPDATE SET
3123 thread_id = excluded.thread_id,
3124 run_id = excluded.run_id,
3125 node_id = excluded.node_id,
3126 kind = excluded.kind,
3127 status = excluded.status,
3128 payload_json = excluded.payload_json
3129 `;
3130 break;
3131 }
3132 case "turn-item.updated": {
3133 const payloadJson = yield* encodeTurnItemPayload(event.payload);
3134 const updatedAt = yield* encodePayloadTimestamp(event.payload.updatedAt);
3135 yield* sql`
3136 INSERT INTO orchestration_v2_projection_turn_items (
3137 turn_item_id,
3138 thread_id,
3139 run_id,
3140 node_id,
3141 provider_thread_id,
3142 provider_turn_id,
3143 parent_item_id,
3144 ordinal,
3145 type,
3146 status,
3147 updated_at,
3148 payload_json,
3149 inline_work_output
3150 )
3151 VALUES (
3152 ${event.payload.id},
3153 ${event.payload.threadId},
3154 ${event.payload.runId},
3155 ${event.payload.nodeId},
3156 ${event.payload.providerThreadId},
3157 ${event.payload.providerTurnId},
3158 ${event.payload.parentItemId},
3159 ${event.payload.ordinal},
3160 ${event.payload.type},
3161 ${event.payload.status},
3162 ${updatedAt},
3163 ${payloadJson},
3164 ${event.payload.type === "dynamic_tool" && isInlineWorkOutput(payloadJson) ? 1 : 0}
3165 )
3166 ON CONFLICT(turn_item_id)
3167 DO UPDATE SET
3168 thread_id = excluded.thread_id,
3169 run_id = excluded.run_id,
3170 node_id = excluded.node_id,
3171 provider_thread_id = excluded.provider_thread_id,
3172 provider_turn_id = excluded.provider_turn_id,
3173 parent_item_id = excluded.parent_item_id,
3174 ordinal = excluded.ordinal,
3175 type = excluded.type,
3176 status = excluded.status,
3177 updated_at = excluded.updated_at,
3178 payload_json = excluded.payload_json,
3179 inline_work_output = excluded.inline_work_output
3180 `;
3181 break;
3182 }
3183 case "checkpoint-scope.created": {
3184 const payloadJson = yield* encodeCheckpointScopePayload(event.payload);
3185 const payload = parseEncodedPayload(payloadJson);
3186 yield* sql`
3187 INSERT INTO orchestration_v2_projection_checkpoint_scopes (
3188 scope_id,
3189 thread_id,
3190 run_id,
3191 node_id,
3192 provider_thread_id,
3193 kind,
3194 ordinal_within_parent,
3195 advances_app_run_count,
3196 created_at,
3197 payload_json
3198 )
3199 VALUES (
3200 ${event.payload.id},
3201 ${event.payload.threadId},
3202 ${event.payload.runId},
3203 ${event.payload.nodeId},
3204 ${event.payload.providerThreadId},
3205 ${event.payload.kind},
3206 ${event.payload.ordinalWithinParent},
3207 ${booleanInt(event.payload.advancesAppRunCount)},
3208 ${stringField(payload, "createdAt")},
3209 ${payloadJson}
3210 )
3211 ON CONFLICT(scope_id)
3212 DO UPDATE SET
3213 thread_id = excluded.thread_id,
3214 run_id = excluded.run_id,
3215 node_id = excluded.node_id,
3216 provider_thread_id = excluded.provider_thread_id,
3217 kind = excluded.kind,
3218 ordinal_within_parent = excluded.ordinal_within_parent,
3219 advances_app_run_count = excluded.advances_app_run_count,
3220 created_at = excluded.created_at,
3221 payload_json = excluded.payload_json
3222 `;
3223 break;
3224 }
3225 case "checkpoint.captured": {
3226 const payloadJson = yield* encodeCheckpointPayload(event.payload);
3227 const payload = parseEncodedPayload(payloadJson);
3228 yield* sql`
3229 INSERT INTO orchestration_v2_projection_checkpoints (
3230 checkpoint_id,
3231 thread_id,
3232 scope_id,
3233 run_id,
3234 node_id,
3235 ordinal_within_scope,
3236 app_run_ordinal,
3237 status,
3238 captured_at,
3239 payload_json
3240 )
3241 VALUES (
3242 ${event.payload.id},
3243 ${event.payload.threadId},
3244 ${event.payload.scopeId},
3245 ${event.payload.runId},
3246 ${event.payload.nodeId},
3247 ${event.payload.ordinalWithinScope},
3248 ${event.payload.appRunOrdinal},
3249 ${event.payload.status},
3250 ${stringField(payload, "capturedAt")},
3251 ${payloadJson}
3252 )
3253 ON CONFLICT(checkpoint_id)
3254 DO UPDATE SET
3255 thread_id = excluded.thread_id,
3256 scope_id = excluded.scope_id,
3257 run_id = excluded.run_id,
3258 node_id = excluded.node_id,
3259 ordinal_within_scope = excluded.ordinal_within_scope,
3260 app_run_ordinal = excluded.app_run_ordinal,
3261 status = excluded.status,
3262 captured_at = excluded.captured_at,
3263 payload_json = excluded.payload_json
3264 `;
3265 break;
3266 }
3267 case "checkpoint.rollback-requested":
3268 break;
3269 case "context-handoff.updated": {
3270 const payloadJson = yield* encodeContextHandoffPayload(event.payload);
3271 const payload = parseEncodedPayload(payloadJson);
3272 yield* sql`
3273 INSERT INTO orchestration_v2_projection_context_handoffs (
3274 context_handoff_id,
3275 thread_id,
3276 target_run_id,
3277 to_provider_thread_id,
3278 strategy,
3279 status,
3280 updated_at,
3281 payload_json
3282 )
3283 VALUES (
3284 ${event.payload.id},
3285 ${event.payload.threadId},
3286 ${event.payload.targetRunId},
3287 ${event.payload.toProviderThreadId},
3288 ${event.payload.strategy},
3289 ${event.payload.status},
3290 ${stringField(payload, "updatedAt")},
3291 ${payloadJson}
3292 )
3293 ON CONFLICT(context_handoff_id)
3294 DO UPDATE SET
3295 thread_id = excluded.thread_id,
3296 target_run_id = excluded.target_run_id,
3297 to_provider_thread_id = excluded.to_provider_thread_id,
3298 strategy = excluded.strategy,
3299 status = excluded.status,
3300 updated_at = excluded.updated_at,
3301 payload_json = excluded.payload_json
3302 `;
3303 break;
3304 }
3305 case "context-transfer.created":
3306 case "context-transfer.updated": {
3307 const payloadJson = yield* encodeContextTransferPayload(event.payload);
3308 const payload = parseEncodedPayload(payloadJson);
3309 yield* sql`
3310 INSERT INTO orchestration_v2_projection_context_transfers (
3311 context_transfer_id,
3312 source_thread_id,
3313 target_thread_id,
3314 target_run_id,
3315 type,
3316 status,
3317 updated_at,
3318 payload_json
3319 )
3320 VALUES (
3321 ${event.payload.id},
3322 ${event.payload.sourceThreadId},
3323 ${event.payload.targetThreadId},
3324 ${event.payload.targetRunId},
3325 ${event.payload.type},
3326 ${event.payload.status},
3327 ${stringField(payload, "updatedAt")},
3328 ${payloadJson}
3329 )
3330 ON CONFLICT(context_transfer_id)
3331 DO UPDATE SET
3332 source_thread_id = excluded.source_thread_id,
3333 target_thread_id = excluded.target_thread_id,
3334 target_run_id = excluded.target_run_id,
3335 type = excluded.type,
3336 status = excluded.status,
3337 updated_at = excluded.updated_at,
3338 payload_json = excluded.payload_json
3339 `;
3340 break;
3341 }
3342 }
3343
3344 if (
3345 event.type !== "thread.created" &&
3346 event.type !== "thread.archived" &&
3347 event.type !== "thread.unarchived" &&
3348 event.type !== "thread.deleted" &&
3349 event.type !== "thread.settled" &&
3350 event.type !== "thread.unsettled" &&
3351 event.type !== "thread.snoozed" &&
3352 event.type !== "thread.unsnoozed" &&
3353 event.type !== "thread.auto-settle-set" &&
3354 event.type !== "thread.pinned" &&
3355 event.type !== "thread.unpinned" &&
3356 event.type !== "thread.pin-reordered" &&
3357 event.type !== "thread.active-reordered" &&
3358 event.type !== "thread.tags-updated" &&
3359 event.type !== "thread.visited" &&
3360 event.type !== "thread.visit-recorded" &&
3361 event.type !== "thread.marked-unread" &&
3362 event.type !== "thread.metadata-updated" &&
3363 event.type !== "thread.pull-request-synced" &&
3364 event.type !== "thread.issue-synced" &&
3365 event.type !== "thread.pull-request-link-updated" &&
3366 event.type !== "thread.runtime-mode-updated" &&
3367 event.type !== "thread.interaction-mode-updated" &&
3368 event.type !== "thread.model-selection-updated" &&
3369 event.type !== "thread.provider-switched" &&
3370 event.type !== "thread.guest-share-updated"
3371 ) {
3372 const updatedAt = yield* encodeThreadUpdatedAt(event.occurredAt);
3373 if (batch !== undefined) batch.threadTouches.set(event.threadId, updatedAt);
3374 else yield* writeThreadTouches(sql, new Map([[event.threadId, updatedAt]]));
3375 }
3376 },
3377 (effect, event) =>
3378 effect.pipe(
3379 Effect.mapError(
3380 (cause) =>
3381 new ProjectionStoreApplyEventError({
3382 eventType: event.type,
3383 cause,
3384 }),
3385 ),
3386 ),
3387 );
3388
3389 // Share selection so counting discarded ancestry preserves payload-read positions.
3390 const retainedWindowRows = Effect.fn("ProjectionStore.retainedWindowRows")(function* (
3391 threadId: ThreadId,
3392 window: ProjectionWindow,
3393 ) {
3394 const eligible = sql`
3395 WITH eligible AS NOT MATERIALIZED (
3396 SELECT item.rowid AS item_rowid, item.payload_json, item.ordinal, item.turn_item_id,
3397 item.run_id, item.node_id, item.type, item.status, item.inline_work_output,
3398 run.status AS run_status,
3399 attempt.attempt_id, attempt.status AS attempt_status
3400 FROM orchestration_v2_projection_turn_items AS item
3401 LEFT JOIN orchestration_v2_projection_runs AS run
3402 ON run.run_id = item.run_id
3403 LEFT JOIN orchestration_v2_projection_nodes AS node
3404 ON ${window.deferWork ? 1 : 0} = 1 AND node.node_id = item.node_id
3405 LEFT JOIN orchestration_v2_projection_run_attempts AS attempt
3406 ON ${window.deferWork ? 1 : 0} = 1 AND attempt.attempt_id = (
3407 SELECT attempt_id FROM orchestration_v2_projection_run_attempts
3408 WHERE run_id = item.run_id AND root_node_id = COALESCE(node.root_node_id, item.node_id)
3409 ORDER BY attempt_ordinal DESC LIMIT 1
3410 )
3411 WHERE item.thread_id = ${threadId}
3412 AND item.ordinal <= COALESCE(
3413 (
3414 SELECT ordinal
3415 FROM orchestration_v2_projection_turn_items
3416 WHERE thread_id = ${threadId}
3417 AND turn_item_id = ${window.anchorItemId ?? null}
3418 LIMIT 1
3419 ),
3420 9223372036854775807
3421 )
3422 -- The fork cutoff below filters rewound runs; undelivered queued input stays hidden.
3423 AND (
3424 ${window.requiredRunId ?? null} IS NOT NULL
3425 OR run.status IS NULL
3426 OR run.status <> 'rolled_back'
3427 )
3428 AND NOT (
3429 item.type = 'user_message'
3430 AND (
3431 COALESCE(run.status IN ('queued', 'cancelled'), 0)
3432 OR (
3433 run.status = 'rolled_back'
3434 AND EXISTS (
3435 SELECT 1
3436 FROM orchestration_v2_projection_run_attempts AS attempt
3437 WHERE attempt.run_id = run.run_id
3438 AND attempt.status = 'cancelled'
3439 )
3440 )
3441 )
3442 AND json_extract(item.payload_json, '$.inputIntent') = 'queued_turn'
3443 )
3444 AND (
3445 ${window.requiredRunId ?? null} IS NULL
3446 OR (
3447 item.run_id IS NULL
3448 AND EXISTS (
3449 SELECT 1
3450 FROM orchestration_v2_projection_threads AS source_thread
3451 WHERE source_thread.thread_id = item.thread_id
3452 AND json_extract(source_thread.payload_json, '$.historyOrigin') = 'v1_import'
3453 )
3454 )
3455 OR (
3456 run.ordinal <= (
3457 SELECT required_run.ordinal
3458 FROM orchestration_v2_projection_runs AS required_run
3459 WHERE required_run.thread_id = item.thread_id
3460 AND required_run.run_id = ${window.requiredRunId ?? null}
3461 LIMIT 1
3462 )
3463 AND NOT COALESCE(
3464 run.status = 'rolled_back'
3465 AND julianday(run.completed_at) < julianday(${
3466 window.forkCreatedAt === undefined
3467 ? null
3468 : DateTime.formatIso(window.forkCreatedAt)
3469 }),
3470 0
3471 )
3472 )
3473 )
3474 AND NOT (
3475 item.type = 'run_interrupt_result'
3476 AND EXISTS (
3477 SELECT 1
3478 FROM orchestration_v2_projection_run_attempts AS attempt
3479 WHERE attempt.run_id = item.run_id
3480 AND attempt.root_node_id = item.node_id
3481 AND attempt.status = 'superseded'
3482 )
3483 AND NOT EXISTS (
3484 SELECT 1
3485 FROM orchestration_v2_projection_turn_items AS request
3486 WHERE request.thread_id = item.thread_id
3487 AND request.run_id = item.run_id
3488 AND request.type = 'run_interrupt_request'
3489 )
3490 )
3491 )`;
3492 // Resolve anchors separately: repeated CTE expansion exceeds SQLite's subquery limit.
3493 const anchors = yield* sql<{ readonly ordinal: number; readonly latest: number | null }>`
3494 ${eligible}, run_starts AS (
3495 -- A wake (PR watch, task completion) starts its run with a
3496 -- notification instead of a user message. Read the first
3497 -- input of each run, newest run first, and add the
3498 -- notifications to the user-message turn starts. A run
3499 -- without an eligible first input (queued, rolled back) is
3500 -- skipped, so it cannot hide older turns. CROSS JOIN keeps
3501 -- the runs as the outer loop, and each probe reads only the
3502 -- first rows of a run.
3503 SELECT start.ordinal, start.type
3504 FROM orchestration_v2_projection_runs AS recent_run
3505 CROSS JOIN eligible AS start ON start.item_rowid = (
3506 SELECT first_input.rowid
3507 FROM orchestration_v2_projection_turn_items AS first_input
3508 WHERE first_input.run_id = recent_run.run_id
3509 AND first_input.type IN ('user_message', 'notification')
3510 ORDER BY first_input.ordinal
3511 LIMIT 1
3512 )
3513 WHERE recent_run.thread_id = ${threadId}
3514 AND recent_run.ordinal <= COALESCE(
3515 (
3516 SELECT anchor_run.ordinal
3517 FROM orchestration_v2_projection_turn_items AS anchor_item
3518 JOIN orchestration_v2_projection_runs AS anchor_run
3519 ON anchor_run.run_id = anchor_item.run_id
3520 WHERE anchor_item.thread_id = ${threadId}
3521 AND anchor_item.turn_item_id = ${window.anchorItemId ?? null}
3522 LIMIT 1
3523 ),
3524 9223372036854775807
3525 )
3526 AND ${window.userTurnLimit ?? null} IS NOT NULL
3527 ORDER BY recent_run.ordinal DESC
3528 LIMIT ${THREAD_HISTORY_MAX_RAW_TURNS + 2}
3529 ), turn_anchors AS (
3530 SELECT ordinal, payload_json
3531 FROM (
3532 SELECT ordinal, payload_json
3533 FROM eligible
3534 WHERE type = 'user_message'
3535 AND json_extract(payload_json, '$.inputIntent') IN ('turn_start', 'queued_turn')
3536 AND ${window.userTurnLimit ?? null} IS NOT NULL
3537 ORDER BY ordinal DESC
3538 LIMIT ${THREAD_HISTORY_MAX_RAW_TURNS + 2}
3539 )
3540 UNION ALL
3541 SELECT ordinal, NULL FROM run_starts WHERE type = 'notification'
3542 ORDER BY ordinal DESC
3543 LIMIT ${THREAD_HISTORY_MAX_RAW_TURNS + 2}
3544 ), user_anchors AS (
3545 SELECT ordinal
3546 FROM turn_anchors
3547 WHERE json_extract(payload_json, '$.createdBy') = 'user'
3548 ORDER BY ordinal DESC
3549 LIMIT ${(window.userTurnLimit ?? 0) + 2}
3550 ), boundary AS (
3551 SELECT CASE
3552 WHEN COUNT(*) >= ${(window.userTurnLimit ?? 0) + 2} THEN MIN(ordinal)
3553 WHEN (SELECT COUNT(*) FROM turn_anchors) >= ${THREAD_HISTORY_MAX_RAW_TURNS + 2}
3554 THEN (SELECT MIN(ordinal) FROM turn_anchors)
3555 ELSE 0
3556 END AS ordinal
3557 FROM user_anchors
3558 )
3559 SELECT ordinal, (SELECT MAX(ordinal) FROM user_anchors) AS latest FROM boundary
3560 `;
3561 const boundary = anchors[0]!;
3562 // Folding needs whole segments, so the window would walk every item since the boundary.
3563 // It stays above the newest `tailRows` rows that are sure to keep their own row. Start at
3564 // the latest segment start of each run that reaches that point, clamped to it so runless
3565 // rows below stay in the scan.
3566 const tailRows = Math.max(window.rowLimit, THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT);
3567 // The newest rows sure to keep their own row: conversation rows, and every row of the
3568 // running run, which folds nothing. More of them than a whole turn may hold means the
3569 // window pages from inside the newest turn with the row limit, so the tail shrinks to
3570 // that limit. A turn of thousands of rows is then not walked to return the newest few.
3571 const tailWorkStart = sql<{
3572 readonly ordinal: number | null;
3573 readonly long_turn: number;
3574 }>`
3575 ${eligible}, live_run AS (
3576 -- The oldest live run is the one at work; queued runs follow it.
3577 SELECT live.run_id FROM orchestration_v2_projection_runs AS live
3578 WHERE live.thread_id = ${threadId}
3579 AND live.status IN ('queued','preparing','starting','running','waiting')
3580 AND ${window.requiredRunId ?? null} IS NULL
3581 AND NOT EXISTS (
3582 SELECT 1 FROM orchestration_v2_projection_run_attempts
3583 WHERE run_id = live.run_id AND status = 'superseded'
3584 )
3585 ORDER BY +live.ordinal ASC LIMIT 1
3586 ), visible_tail AS (
3587 SELECT ordinal FROM (
3588 SELECT ordinal FROM eligible
3589 WHERE ordinal >= ${boundary.ordinal}
3590 AND type NOT IN ${CONVERSATION_INDEX_EXCLUDED_TYPES}
3591 ORDER BY ordinal DESC
3592 LIMIT ${tailRows}
3593 )
3594 UNION
3595 SELECT ordinal FROM (
3596 SELECT live_item.ordinal
3597 FROM orchestration_v2_projection_turn_items AS live_item
3598 -- The unary plus keeps this on the run index, whose ordinal order ends
3599 -- the walk at the limit.
3600 WHERE live_item.run_id = (SELECT run_id FROM live_run)
3601 AND +live_item.thread_id = ${threadId}
3602 AND live_item.ordinal >= ${boundary.ordinal}
3603 AND live_item.ordinal <= COALESCE(
3604 (
3605 SELECT ordinal FROM orchestration_v2_projection_turn_items
3606 WHERE thread_id = ${threadId}
3607 AND turn_item_id = ${window.anchorItemId ?? null}
3608 LIMIT 1
3609 ),
3610 9223372036854775807
3611 )
3612 ORDER BY live_item.ordinal DESC
3613 LIMIT ${tailRows}
3614 )
3615 ORDER BY ordinal DESC
3616 LIMIT ${tailRows}
3617 ), turn AS (
3618 SELECT (
3619 SELECT COUNT(*) FROM visible_tail WHERE ordinal >= ${boundary.latest}
3620 ) > ${THREAD_HISTORY_PAGE_POLICY.maxTurnItems} AS long_turn
3621 ), tail AS (
3622 SELECT visible_tail.ordinal FROM visible_tail
3623 ORDER BY visible_tail.ordinal DESC
3624 LIMIT 1 OFFSET (
3625 SELECT MAX(CASE WHEN long_turn THEN ${window.rowLimit} ELSE ${tailRows} END, 1) - 1
3626 FROM turn
3627 )
3628 ), tail_runs AS (
3629 SELECT DISTINCT item.run_id
3630 FROM tail CROSS JOIN eligible AS item
3631 WHERE item.ordinal >= tail.ordinal AND item.run_id IS NOT NULL
3632 )
3633 SELECT MIN(MIN(CASE
3634 -- Only a completed run or a superseded attempt folds work, and folding is
3635 -- what needs the whole segment. Any other run keeps every row.
3636 WHEN (
3637 SELECT status FROM orchestration_v2_projection_runs
3638 WHERE run_id = tail_runs.run_id
3639 ) IS NOT 'completed' AND NOT EXISTS (
3640 SELECT 1 FROM orchestration_v2_projection_run_attempts
3641 WHERE run_id = tail_runs.run_id AND status = 'superseded'
3642 ) THEN tail.ordinal
3643 ELSE COALESCE(
3644 (
3645 SELECT start.ordinal FROM eligible AS start
3646 WHERE start.run_id = tail_runs.run_id AND start.type = 'user_message'
3647 AND start.ordinal <= tail.ordinal
3648 ORDER BY start.ordinal DESC LIMIT 1
3649 ),
3650 (
3651 SELECT MIN(first.ordinal) FROM orchestration_v2_projection_turn_items AS first
3652 WHERE first.thread_id = ${threadId} AND first.run_id = tail_runs.run_id
3653 ),
3654 tail.ordinal
3655 )
3656 END, tail.ordinal)) AS ordinal, (SELECT long_turn FROM turn) AS long_turn
3657 FROM tail LEFT JOIN tail_runs
3658 `;
3659 // A negative or fractional limit makes SQLite's LIMIT disagree with `tailRows`; keep the old start.
3660 const tail =
3661 window.deferWork === true &&
3662 boundary.latest !== null &&
3663 Number.isSafeInteger(window.rowLimit) &&
3664 window.rowLimit >= 0
3665 ? (yield* tailWorkStart)[0]
3666 : undefined;
3667 const longTurn = tail?.long_turn === 1;
3668 const workStart = Math.max(boundary.ordinal, tail?.ordinal ?? 0);
3669 return sql`
3670 ${eligible}, blocked_runs AS MATERIALIZED (
3671 SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items
3672 WHERE thread_id = ${threadId} AND run_id IS NOT NULL
3673 AND ${window.deferWork ? 1 : 0} = 1
3674 -- Only a run that can fold asks whether it is blocked. A live run cannot,
3675 -- and this check would read every row it ever wrote.
3676 AND run_id IN (
3677 SELECT run_id FROM eligible
3678 WHERE ordinal >= ${workStart} AND run_id IS NOT NULL
3679 AND (run_status = 'completed' OR attempt_status = 'superseded')
3680 )
3681 AND (type IN ('run_interrupt_request', 'run_interrupt_result')
3682 OR (type = 'error' AND status = 'failed' AND parent_item_id IS NULL))
3683 ), work_segments AS (
3684 SELECT item_rowid, ordinal, turn_item_id, run_id, type, attempt_id,
3685 MAX(CASE WHEN type = 'user_message' THEN ordinal ELSE 0 END)
3686 OVER (PARTITION BY run_id ORDER BY ordinal) AS segment,
3687 CASE WHEN status = 'completed' AND (run_status = 'completed' OR attempt_status = 'superseded')
3688 AND type IN ${sql.in(DEFERRED_WORK_ITEM_TYPES)}
3689 AND inline_work_output = 0
3690 AND run_id NOT IN (SELECT run_id FROM blocked_runs)
3691 THEN 1 ELSE 0 END AS can_defer
3692 FROM eligible AS item
3693 WHERE ${window.deferWork ? 1 : 0} = 1
3694 AND ${boundary.latest === null ? 0 : 1} = 1
3695 AND ordinal >= ${workStart}
3696 ), deferred_work AS (
3697 SELECT run_id, segment, attempt_id, MIN(ordinal) AS first_ordinal,
3698 MAX(ordinal) AS last_ordinal, COUNT(*) AS item_count
3699 FROM work_segments WHERE can_defer = 1
3700 GROUP BY run_id, segment, attempt_id HAVING COUNT(*) > ${THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT}
3701 ), window_items AS NOT MATERIALIZED (
3702 SELECT item_rowid, ordinal, turn_item_id, run_id, type
3703 FROM eligible WHERE ${window.deferWork ? 1 : 0} = 0
3704 OR ${boundary.latest === null ? 1 : 0} = 1
3705 UNION ALL
3706 SELECT item.item_rowid, item.ordinal, item.turn_item_id, item.run_id, item.type
3707 FROM work_segments item LEFT JOIN deferred_work deferred
3708 ON deferred.run_id = item.run_id AND deferred.segment = item.segment
3709 AND deferred.attempt_id IS item.attempt_id
3710 WHERE item.can_defer = 0 OR deferred.run_id IS NULL OR item.ordinal = deferred.first_ordinal
3711 ), latest_user_turn AS (
3712 -- Mirrors selectOlderTimelinePage: whole turns while the
3713 -- newest user turn fits the turn row budget, otherwise the
3714 -- window pages from inside that turn with the row limit.
3715 SELECT COUNT(*) AS row_count
3716 FROM (
3717 SELECT 1
3718 FROM window_items
3719 WHERE ordinal >= ${boundary.latest}
3720 LIMIT ${THREAD_HISTORY_PAGE_POLICY.maxTurnItems + 1}
3721 )
3722 ), selected AS (
3723 -- Choose and sort rows by ID, then fetch payloads in that
3724 -- order. A turn window can hold megabytes of tool output, and
3725 -- carrying it through the union and sort cost about a second.
3726 SELECT item_rowid, ordinal, turn_item_id, run_id, type
3727 FROM window_items
3728 WHERE ordinal >= ${boundary.ordinal}
3729 ORDER BY ordinal DESC, turn_item_id DESC
3730 -- Every LIMIT here and in latest_user_turn must stay <= tailRows, or rows below workStart go missing.
3731 LIMIT CASE
3732 WHEN ${window.rowLimit} = 0 THEN 0
3733 WHEN ${longTurn ? 1 : 0} = 1 THEN ${window.rowLimit}
3734 WHEN (SELECT row_count FROM latest_user_turn)
3735 BETWEEN 1 AND ${THREAD_HISTORY_PAGE_POLICY.maxTurnItems}
3736 THEN ${THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT}
3737 ELSE ${window.rowLimit}
3738 END
3739 ), retained AS MATERIALIZED (
3740 SELECT item_rowid, ordinal, turn_item_id FROM selected
3741 UNION
3742 SELECT request.rowid AS item_rowid, request.ordinal, request.turn_item_id
3743 FROM orchestration_v2_projection_turn_items AS request
3744 WHERE request.run_id IN (
3745 SELECT run_id FROM selected
3746 WHERE type = 'run_interrupt_result' AND run_id IS NOT NULL
3747 )
3748 AND request.type = 'run_interrupt_request'
3749 UNION
3750 SELECT latest.item_rowid, latest.ordinal, latest.turn_item_id
3751 FROM (
3752 SELECT rowid AS item_rowid, ordinal, turn_item_id
3753 FROM orchestration_v2_projection_turn_items
3754 WHERE thread_id = ${threadId}
3755 AND ${window.anchorItemId ?? null} IS NULL
3756 AND ${window.requiredRunId ?? null} IS NULL
3757 ORDER BY ordinal DESC, turn_item_id DESC
3758 LIMIT 1
3759 ) AS latest
3760 ORDER BY ordinal ASC, turn_item_id ASC
3761 )
3762 `;
3763 });
3764
3765 const turnItemMatch = (threadId: ThreadId, filter: ProjectionRecordFilter | undefined) => sql`
3766 ${threadColumn(filter?.turnItemNodeIds)} = ${threadId}
3767 ${filter?.turnItemRequestIds === undefined ? sql`` : sql`AND json_extract(payload_json, '$.requestId') IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemRequestIds)}))`}
3768 ${filter?.turnItemTypes === undefined ? sql`` : sql`AND type IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemTypes)}))`}
3769 ${filter?.turnItemStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemStatuses)}))`}
3770 ${filter?.turnItemRunId === undefined ? sql`` : sql`AND run_id = ${filter.turnItemRunId}`}
3771 ${filter?.turnItemNodeIds === undefined ? sql`` : sql`AND node_id IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemNodeIds)}))`}
3772 ${filter?.turnItemRunIds === undefined ? sql`` : sql`AND (run_id IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemRunIds.filter((id): id is RunId => id !== null))})) OR (${filter.turnItemRunIds.includes(null) ? 1 : 0} = 1 AND run_id IS NULL))`}
3773 `;
3774
3775 // An id filter reads by primary key. `+` keeps SQLite off the thread index, which walks
3776 // every row of the thread to find the few ids.
3777 const threadColumn = (ids: ReadonlyArray<unknown> | undefined, column = "thread_id") =>
3778 sql.literal(ids === undefined ? column : `+${column}`);
3779
3780 /** The ids of the runs whose records a new message reads. Null when nothing was compacted. */
3781 const runIdsSinceLastCompaction = Effect.fn("ProjectionStore.runIdsSinceLastCompaction")(
3782 function* (
3783 threadId: ThreadId,
3784 keep: NonNullable<ProjectionRecordFilter["sinceLastCompaction"]>,
3785 ) {
3786 // A held queue can run out of ordinal order, so a run that ended after the compaction's
3787 // run was requested is recent too. Every run row is read anyway, so this scan reads no
3788 // extra pages.
3789 const recent = yield* sql<{ readonly run_id: RunId }>`
3790 WITH compaction AS (
3791 SELECT run.ordinal, run.requested_at
3792 FROM orchestration_v2_projection_turn_items AS item
3793 INDEXED BY orchestration_v2_projection_turn_items_conversation_idx
3794 INNER JOIN orchestration_v2_projection_runs AS run ON run.run_id = item.run_id
3795 WHERE item.thread_id = ${threadId}
3796 AND item.type = 'compaction' AND item.status = 'completed'
3797 AND item.type NOT IN ${CONVERSATION_INDEX_EXCLUDED_TYPES}
3798 AND run.status <> 'rolled_back'
3799 ORDER BY item.ordinal DESC
3800 LIMIT 1
3801 )
3802 SELECT run.run_id
3803 FROM compaction
3804 INNER JOIN orchestration_v2_projection_runs AS run ON run.thread_id = ${threadId}
3805 WHERE run.ordinal >= compaction.ordinal
3806 OR run.completed_at IS NULL
3807 OR run.completed_at >= compaction.requested_at
3808 OR run.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
3809 UNION
3810 SELECT message.run_id
3811 FROM compaction
3812 INNER JOIN orchestration_v2_projection_messages AS message
3813 ON message.message_id = ${keep.keepMessageId}
3814 WHERE message.thread_id = ${threadId} AND message.run_id IS NOT NULL
3815 `;
3816 // The compaction's own run is always recent, so no rows means no compaction.
3817 return recent.length === 0
3818 ? null
3819 : [...recent.map((row) => row.run_id), ...keep.keepRunIds];
3820 },
3821 );
3822
3823 /**
3824 * What a command reads of a compacted thread's prompts and errors: the failed errors of every
3825 * run, and the prompts of the recent runs, of fresh-context runs, and of the newest runs back
3826 * to the one `modelSwitchSince` measures from. That one can be older than the compaction.
3827 */
3828 const commandTurnItems = Effect.fn("ProjectionStore.commandTurnItems")(function* (
3829 threadId: ThreadId,
3830 runs: ReadonlyArray<OrchestrationV2Run>,
3831 recentRunIds: ReadonlyArray<RunId>,
3832 ) {
3833 const errors = yield* sql<PayloadRow>`
3834 SELECT payload_json
3835 FROM orchestration_v2_projection_turn_items
3836 INDEXED BY orchestration_v2_projection_turn_items_failed_error_idx
3837 WHERE thread_id = ${threadId} AND type = 'error' AND status = 'failed'
3838 `.pipe(Effect.flatMap(decodeRows(decodeTurnItemPayload, threadId)));
3839 const readPrompts = (runIds: ReadonlyArray<string>) =>
3840 sql<PayloadRow>`
3841 SELECT payload_json
3842 FROM orchestration_v2_projection_turn_items
3843 INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
3844 WHERE thread_id = ${threadId} AND type = 'user_message'
3845 AND run_id IN (SELECT value FROM json_each(${encodeIdList(runIds)}))
3846 `.pipe(Effect.flatMap(decodeRows(decodeTurnItemPayload, threadId)));
3847 const read = new Set<string>(recentRunIds);
3848 for (const run of runs) if (run.freshContext === true) read.add(run.id);
3849 const prompts = yield* readPrompts([...read]);
3850 const shownMessageIds = new Set<string>();
3851 const showPrompts = (items: ReadonlyArray<OrchestrationV2TurnItem>) => {
3852 for (const item of items) {
3853 if (item.type === "user_message" && isShownUserMessageItem(item)) {
3854 shownMessageIds.add(item.messageId);
3855 }
3856 }
3857 };
3858 showPrompts(prompts);
3859 const newestFirst = runs
3860 .filter(isModelSwitchCandidate)
3861 .toSorted((left, right) => (runRanAfter(left, right) ? -1 : 1));
3862 // The batches double, so a thread with no such run takes few queries.
3863 for (let start = 0, size = 1; start < newestFirst.length; start += size, size *= 2) {
3864 const batch = newestFirst.slice(start, start + size);
3865 const unread = batch.filter((run) => !read.has(run.id));
3866 if (unread.length > 0) {
3867 for (const run of unread) read.add(run.id);
3868 const more = yield* readPrompts(unread.map((run) => run.id));
3869 showPrompts(more);
3870 prompts.push(...more);
3871 }
3872 if (batch.some((run) => anchorsModelSwitch(run, shownMessageIds))) break;
3873 }
3874 return [...errors, ...prompts].toSorted(
3875 (left, right) =>
3876 left.ordinal - right.ordinal || (left.id < right.id ? -1 : left.id > right.id ? 1 : 0),
3877 );
3878 });
3879
3880 const readCanonicalProjection = Effect.fn("ProjectionStore.readCanonicalProjection")(
3881 function* (
3882 threadId: ThreadId,
3883 window?: ProjectionWindow,
3884 fields?: ReadonlyArray<ProjectionRecordField>,
3885 filter?: ProjectionRecordFilter,
3886 ) {
3887 const threadRows = yield* sql<PayloadRow>`
3888 SELECT payload_json
3889 FROM orchestration_v2_projection_threads
3890 WHERE thread_id = ${threadId}
3891 LIMIT 1
3892 `;
3893 const threadRow = threadRows[0];
3894 if (!threadRow) {
3895 return yield* new ProjectionStoreThreadNotFoundError({ threadId });
3896 }
3897 const recentRuns =
3898 window === undefined && filter?.sinceLastCompaction !== undefined
3899 ? yield* runIdsSinceLastCompaction(threadId, filter.sinceLastCompaction)
3900 : null;
3901 const recentRunIds = recentRuns === null ? null : encodeIdList(recentRuns);
3902 const readsWholeHistory =
3903 (fields === undefined || fields.includes("runs")) &&
3904 PROMPT_NARROWING_BLOCKING_FILTERS.every((key) => filter?.[key] === undefined);
3905 const promptRuns = readsWholeHistory ? recentRuns : null;
3906 const inRecentRuns = (column: string) =>
3907 recentRunIds === null
3908 ? sql``
3909 : sql`AND ${sql.literal(column)} IN (SELECT value FROM json_each(${recentRunIds}))`;
3910 // Without the hint SQLite reads every row of the thread to find the recent runs' rows.
3911 const recentRunIndex = (index: string) =>
3912 recentRunIds === null
3913 ? sql``
3914 : sql`INDEXED BY ${sql.literal(`orchestration_v2_projection_${index}`)}`;
3915
3916 type WindowPayloadRow = PayloadRow & {
3917 readonly work_last_id?: string | null;
3918 readonly work_last_ordinal?: number | null;
3919 readonly work_item_count?: number | null;
3920 };
3921 const boundedTurnItemRows: ReadonlyArray<WindowPayloadRow> =
3922 (fields !== undefined && !fields.includes("turnItems")) ||
3923 // A compacted thread reads its items once its runs are known.
3924 promptRuns !== null ||
3925 (window === undefined &&
3926 matchesNothing(
3927 filter?.turnItemRequestIds,
3928 filter?.turnItemTypes,
3929 filter?.turnItemStatuses,
3930 filter?.turnItemRunIds,
3931 filter?.turnItemNodeIds,
3932 ))
3933 ? []
3934 : window === undefined
3935 ? yield* sql<PayloadRow>`
3936 SELECT payload_json
3937 FROM orchestration_v2_projection_turn_items
3938 ${
3939 filter?.turnItemTypes === undefined
3940 ? sql`WHERE ${turnItemMatch(threadId, filter)}`
3941 : // Seeks the type index. Sorted, SQLite would walk the thread by ordinal instead.
3942 sql`WHERE rowid IN (
3943 SELECT rowid FROM orchestration_v2_projection_turn_items
3944 WHERE ${turnItemMatch(threadId, filter)}
3945 )`
3946 }
3947 ORDER BY ordinal ASC, turn_item_id ASC
3948 `
3949 : yield* sql<PayloadRow>`
3950 ${yield* retainedWindowRows(threadId, window)}
3951 -- CROSS JOIN keeps the sorted IDs as the outer loop, so SQLite
3952 -- skips sorting again once the payloads are attached. Carry the
3953 -- rowid through selection to avoid a second string-key index lookup.
3954 SELECT item.payload_json, tail.turn_item_id AS work_last_id,
3955 deferred.last_ordinal AS work_last_ordinal, deferred.item_count AS work_item_count
3956 FROM retained
3957 CROSS JOIN orchestration_v2_projection_turn_items AS item
3958 ON item.rowid = retained.item_rowid
3959 LEFT JOIN deferred_work deferred ON deferred.run_id = item.run_id
3960 AND deferred.first_ordinal = item.ordinal
3961 LEFT JOIN orchestration_v2_projection_turn_items tail
3962 ON tail.turn_item_id = (
3963 SELECT turn_item_id FROM orchestration_v2_projection_turn_items
3964 WHERE thread_id = item.thread_id AND ordinal = deferred.last_ordinal
3965 ORDER BY turn_item_id DESC LIMIT 1
3966 )
3967 ORDER BY retained.ordinal ASC, retained.turn_item_id ASC
3968 `;
3969 // Decode items once for cohort ids and projection; repeated parsing duplicates retained tool output.
3970 const decodedTurnItems = yield* decodeRows(
3971 decodeTurnItemPayload,
3972 threadId,
3973 )(boundedTurnItemRows);
3974 const storedTurnItems = decodedTurnItems.map((item, index) => {
3975 const row = boundedTurnItemRows[index]!;
3976 return row.work_last_id == null ||
3977 row.work_item_count == null ||
3978 row.work_last_ordinal == null
3979 ? item
3980 : {
3981 ...item,
3982 deferredWork: {
3983 cursor: encodeThreadHistoryCursor({
3984 snapshotSequence: 0,
3985 sourceThreadId: threadId,
3986 sourceItemId: TurnItemId.make(row.work_last_id),
3987 position: row.work_last_ordinal,
3988 inclusive: true,
3989 }),
3990 itemCount: row.work_item_count,
3991 lastOrdinal: row.work_last_ordinal,
3992 },
3993 };
3994 });
3995 const cohortJson = (field: string) =>
3996 JSON.stringify(
3997 storedTurnItems.flatMap((payload) => {
3998 const value = nullableStringField(payload, field);
3999 return value === null ? [] : [value];
4000 }),
4001 );
4002 const cohortRunIds = cohortJson("runId");
4003 const cohortNodeIds =
4004 window === undefined ||
4005 (fields !== undefined &&
4006 fields.every(
4007 (field) => field === "runs" || field === "attempts" || field === "turnItems",
4008 ))
4009 ? cohortJson("nodeId")
4010 : encodeIdList(
4011 (yield* sql<{ readonly node_id: string }>`
4012 WITH RECURSIVE retained(node_id) AS (
4013 SELECT value FROM json_each(${cohortJson("nodeId")})
4014 UNION
4015 SELECT node_id FROM orchestration_v2_projection_nodes AS live
4016 ${LIVE_NODES_INDEX}
4017 WHERE thread_id = ${threadId} AND status IN ('pending','starting','running','waiting')
4018 AND NOT ${todoListOfSettledRun(sql`live`)}
4019 UNION
4020 SELECT root_node_id FROM orchestration_v2_projection_run_attempts
4021 WHERE thread_id = ${threadId} AND (
4022 status IN ('pending','starting','running','waiting')
4023 OR run_id IN (SELECT value FROM json_each(${cohortRunIds}))
4024 OR run_id = ${window.requiredRunId ?? null})
4025 UNION
4026 SELECT json_extract(payload_json, '$.rootNodeId') FROM orchestration_v2_projection_runs
4027 WHERE thread_id = ${threadId} AND (
4028 status IN ('queued','preparing','starting','running','waiting')
4029 OR run_id IN (SELECT value FROM json_each(${cohortRunIds}))
4030 OR run_id = ${window.requiredRunId ?? null})
4031 UNION
4032 SELECT node_id FROM orchestration_v2_projection_runtime_requests
4033 WHERE thread_id = ${threadId} AND status IN ('pending','waiting')
4034 UNION
4035 SELECT parent_node_id FROM orchestration_v2_projection_subagents
4036 WHERE thread_id = ${threadId} AND status IN ('pending','starting','running','waiting')
4037 UNION
4038 SELECT node_id FROM orchestration_v2_projection_provider_turns
4039 WHERE thread_id = ${threadId} AND status IN ('starting','running','waiting')
4040 UNION
4041 SELECT parent.parent_node_id FROM orchestration_v2_projection_nodes AS parent
4042 INNER JOIN retained ON parent.node_id = retained.node_id
4043 WHERE parent.thread_id = ${threadId} AND parent.parent_node_id IS NOT NULL
4044 )
4045 -- Sort IDs before fetching bodies, so large payloads never enter
4046 -- SQLite's temporary sort. The node read restores this order by key.
4047 SELECT retained.node_id
4048 FROM retained
4049 LEFT JOIN orchestration_v2_projection_nodes AS node
4050 ON node.node_id = retained.node_id AND node.thread_id = ${threadId}
4051 WHERE retained.node_id IS NOT NULL
4052 ORDER BY COALESCE(node.started_at, ''), retained.node_id ASC
4053 `).map((row) => row.node_id),
4054 );
4055 const cohortProviderThreadIds = cohortJson("providerThreadId");
4056 const cohortProviderTurnIds = cohortJson("providerTurnId");
4057 const cohortMessageIds = cohortJson("messageId");
4058 const cohortPlanIds = cohortJson("planId");
4059 const cohortCheckpointIds = cohortJson("checkpointId");
4060 const cohortHandoffIds = cohortJson("contextHandoffId");
4061 // A newer completion must keep an older active list from resurfacing after paging.
4062 const [latestTodoList] =
4063 window === undefined ||
4064 (fields !== undefined &&
4065 !fields.some(
4066 (field) => field === "plans" || field === "runs" || field === "providerThreads",
4067 ))
4068 ? []
4069 : yield* sql<{
4070 readonly plan_id: string;
4071 readonly run_id: string;
4072 readonly provider_thread_id: string | null;
4073 readonly last_plan_id: string;
4074 }>`
4075 WITH latest_owner AS MATERIALIZED (
4076 SELECT plan.plan_id, owner.run_id, owner.provider_thread_id
4077 FROM orchestration_v2_projection_plans AS plan
4078 INNER JOIN orchestration_v2_projection_runs AS owner
4079 ON owner.thread_id = plan.thread_id AND owner.run_id = plan.run_id
4080 WHERE plan.thread_id = ${threadId}
4081 AND plan.kind = 'todo_list' AND plan.status <> 'superseded'
4082 ORDER BY owner.ordinal DESC, plan.plan_id DESC
4083 LIMIT 1
4084 )
4085 SELECT COALESCE(CASE WHEN EXISTS (
4086 SELECT 1 FROM orchestration_v2_projection_plans
4087 WHERE thread_id = ${threadId} AND run_id = owner.run_id
4088 AND kind = 'todo_list' AND status <> 'superseded'
4089 AND plan_id <> owner.plan_id
4090 ) THEN (
4091 -- Plan UUIDs cannot order replacements within a run.
4092 SELECT plan.plan_id
4093 FROM orchestration_v2_projection_turn_items AS item
4094 CROSS JOIN orchestration_v2_projection_plans AS plan
4095 ON plan.plan_id = json_extract(item.payload_json, '$.planId')
4096 WHERE item.run_id = owner.run_id AND item.thread_id = ${threadId}
4097 AND item.type = 'todo_list'
4098 AND plan.run_id = owner.run_id AND plan.thread_id = ${threadId}
4099 AND plan.kind = 'todo_list' AND plan.status <> 'superseded'
4100 ORDER BY item.ordinal DESC
4101 LIMIT 1
4102 ) END, owner.plan_id) AS plan_id, owner.run_id, owner.provider_thread_id,
4103 (
4104 SELECT MAX(plan_id) FROM orchestration_v2_projection_plans
4105 WHERE thread_id = ${threadId} AND run_id = owner.run_id
4106 ) AS last_plan_id
4107 FROM latest_owner AS owner
4108 `;
4109
4110 const [
4111 thread,
4112 runRows,
4113 attemptRows,
4114 nodeRows,
4115 subagentRows,
4116 providerSessionRows,
4117 providerThreadRows,
4118 providerTurnRows,
4119 runtimeRequestRows,
4120 messageRows,
4121 planRows,
4122 checkpointScopeRows,
4123 checkpointRows,
4124 contextHandoffRows,
4125 contextTransferRows,
4126 ] = yield* Effect.all([
4127 decodeThreadPayload(threadRow.payload_json),
4128 (fields !== undefined && !fields.includes("runs")) ||
4129 (window === undefined &&
4130 (matchesNothing(filter?.runIds) ||
4131 (matchesNothing(filter?.runStatuses) &&
4132 filter?.includeLatestRun !== true &&
4133 filter?.includeLatestExecutedRun !== true &&
4134 filter?.latestRunLimit === undefined)))
4135 ? Effect.succeed([])
4136 : window === undefined
4137 ? sql<PayloadRow>`
4138 SELECT payload_json
4139 FROM orchestration_v2_projection_runs
4140 WHERE ${threadColumn(
4141 filter?.latestRunLimit === undefined && filter?.includeLatestExecutedRun !== true
4142 ? filter?.runIds
4143 : [],
4144 )} = ${threadId}
4145 ${filter?.runIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.runIds)}))`}
4146 ${
4147 filter?.latestRunLimit !== undefined
4148 ? sql`AND run_id IN (
4149 SELECT run_id FROM (
4150 SELECT run_id FROM orchestration_v2_projection_runs
4151 WHERE thread_id = ${threadId}
4152 ORDER BY ordinal DESC
4153 LIMIT ${filter.latestRunLimit}
4154 )
4155 ${
4156 filter.runStatuses === undefined
4157 ? sql``
4158 : sql`UNION
4159 SELECT run_id FROM orchestration_v2_projection_runs
4160 WHERE thread_id = ${threadId}
4161 AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))`
4162 }
4163 )
4164 ORDER BY ordinal ASC`
4165 : filter?.runStatuses === undefined
4166 ? sql`ORDER BY ordinal ASC`
4167 : filter.includeLatestRun !== true && filter.includeLatestExecutedRun !== true
4168 ? // `+ordinal` stops SQLite from walking every run in the (thread_id, ordinal)
4169 // index to skip a sort; a status index reads only matching runs.
4170 sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
4171 ORDER BY +ordinal ASC`
4172 : filter.includeLatestExecutedRun === true
4173 ? sql`AND run_id IN (
4174 SELECT run_id FROM orchestration_v2_projection_runs
4175 WHERE thread_id = ${threadId}
4176 AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
4177 UNION
4178 SELECT run_id FROM (
4179 SELECT run_id FROM orchestration_v2_projection_runs
4180 WHERE thread_id = ${threadId}
4181 AND status <> 'queued'
4182 AND NOT (
4183 status = 'cancelled'
4184 AND json_extract(payload_json, '$.startedAt') IS NULL
4185 )
4186 -- latestExecutedRun: the run that ended last (runRanAfter).
4187 ORDER BY completed_at IS NULL DESC, completed_at DESC, ordinal DESC,
4188 run_id DESC
4189 LIMIT 1
4190 )
4191 )
4192 ORDER BY ordinal ASC`
4193 : sql`AND (status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
4194 OR run_id = (
4195 SELECT run_id FROM orchestration_v2_projection_runs
4196 WHERE thread_id = ${threadId}
4197 AND (status <> 'cancelled' OR json_extract(payload_json, '$.startedAt') IS NOT NULL)
4198 ORDER BY ordinal DESC
4199 LIMIT 1
4200 ))
4201 ORDER BY ordinal ASC`
4202 }
4203 `
4204 : sql<PayloadRow>`
4205 SELECT payload_json FROM orchestration_v2_projection_runs
4206 WHERE thread_id = ${threadId}
4207 AND (status IN ('queued','preparing','starting','running','waiting')
4208 OR run_id IN (SELECT value FROM json_each(${cohortRunIds}))
4209 OR run_id = ${window.requiredRunId ?? null}
4210 ${
4211 // Keep the shell's latest run even when its items are outside the newest window.
4212 window.anchorItemId === undefined && window.requiredRunId === undefined
4213 ? sql`OR run_id = ${presentedShellRunId(sql`${threadId}`)}`
4214 : sql``
4215 }
4216 -- Queued or cancelled turns must not hide the latest actual session.
4217 OR run_id = ${latestTodoList?.run_id ?? null}
4218 OR run_id = (
4219 SELECT run_id FROM orchestration_v2_projection_runs
4220 WHERE thread_id = ${threadId}
4221 AND provider_thread_id IS NOT NULL
4222 AND json_extract(payload_json, '$.startedAt') IS NOT NULL
4223 AND ${latestTodoList?.plan_id ?? null} IS NOT NULL
4224 ORDER BY ordinal DESC
4225 LIMIT 1
4226 ))
4227 ORDER BY ordinal ASC
4228 `,
4229 (fields !== undefined && !fields.includes("attempts")) ||
4230 (window === undefined && matchesNothing(filter?.attemptIds))
4231 ? Effect.succeed([])
4232 : window === undefined
4233 ? sql<PayloadRow>`
4234 SELECT payload_json
4235 FROM orchestration_v2_projection_run_attempts
4236 WHERE ${threadColumn(filter?.attemptIds)} = ${threadId}
4237 ${filter?.attemptIds === undefined ? sql`` : sql`AND attempt_id IN (SELECT value FROM json_each(${encodeIdList(filter.attemptIds)}))`}
4238 ${inRecentRuns("run_id")}
4239 ORDER BY run_id ASC, attempt_ordinal ASC
4240 `
4241 : sql<PayloadRow>`
4242 SELECT payload_json FROM orchestration_v2_projection_run_attempts
4243 WHERE thread_id = ${threadId}
4244 AND (status IN ('pending','starting','running','waiting')
4245 OR run_id IN (SELECT value FROM json_each(${cohortRunIds})))
4246 ORDER BY run_id ASC, attempt_ordinal ASC
4247 `,
4248 (fields !== undefined && !fields.includes("nodes")) ||
4249 (window === undefined && matchesNothing(filter?.nodeIds))
4250 ? Effect.succeed([])
4251 : window === undefined
4252 ? sql<PayloadRow>`
4253 SELECT payload_json
4254 FROM orchestration_v2_projection_nodes AS node
4255 WHERE ${
4256 filter?.omitSettledLeafNodes === true
4257 ? // Seeks the kept nodes. One filter over the thread reads every settled leaf to
4258 // drop it. The first predicate must match migration 083's index expression. A
4259 // node's run is always a run of its own thread, and an active run is recent.
4260 sql`node.rowid IN (
4261 SELECT rowid FROM orchestration_v2_projection_nodes
4262 ${recentRunIndex("nodes_thread_run_idx")}
4263 WHERE thread_id = ${threadId}
4264 AND (kind IN ('tool_call', 'assistant_message', 'reasoning')
4265 AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
4266 AND run_id IS NOT NULL) = 0
4267 ${inRecentRuns("run_id")}
4268 ${
4269 filter.omitSettledSubagentRecords === true
4270 ? // A subagent's node shares its id. Both indexes hold every column
4271 // read here, so no settled subagent or node row is fetched.
4272 sql`EXCEPT
4273 SELECT settled_node.rowid
4274 FROM orchestration_v2_projection_subagents AS settled
4275 INDEXED BY orchestration_v2_projection_subagents_thread_idx
4276 CROSS JOIN orchestration_v2_projection_nodes AS settled_node
4277 ON settled_node.node_id = settled.subagent_id
4278 WHERE settled.thread_id = ${threadId}
4279 AND settled.rowid NOT IN (${unsettledSubagentRowids(threadId)})`
4280 : sql``
4281 }
4282 UNION ALL
4283 SELECT rowid FROM orchestration_v2_projection_nodes
4284 WHERE thread_id = ${threadId}
4285 AND run_id IN (
4286 SELECT run_id FROM orchestration_v2_projection_runs
4287 WHERE thread_id = ${threadId}
4288 AND status IN ('preparing', 'queued', 'starting', 'running', 'waiting')
4289 )
4290 AND kind IN ('tool_call', 'assistant_message', 'reasoning')
4291 AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
4292 )`
4293 : sql`${threadColumn(filter?.nodeIds, "node.thread_id")} = ${threadId}
4294 ${inRecentRuns("node.run_id")}`
4295 }
4296 ${filter?.nodeIds === undefined ? sql`` : sql`AND node.node_id IN (SELECT value FROM json_each(${encodeIdList(filter.nodeIds)}))`}
4297 ORDER BY COALESCE(node.started_at, ''), node.node_id ASC
4298 `
4299 : sql<PayloadRow & { readonly position: number }>`
4300 -- Keep retained IDs outermost so SQLite looks up each node by its
4301 -- primary key instead of scanning every historical node in the thread.
4302 SELECT node.payload_json, retained.key AS position
4303 FROM json_each(${cohortNodeIds}) AS retained
4304 CROSS JOIN orchestration_v2_projection_nodes AS node
4305 ON node.node_id = retained.value
4306 WHERE node.thread_id = ${threadId}
4307 `.pipe(
4308 Effect.map((rows) =>
4309 rows.toSorted((left, right) => left.position - right.position),
4310 ),
4311 ),
4312 (fields !== undefined && !fields.includes("subagents")) ||
4313 (window === undefined &&
4314 matchesNothing(
4315 filter?.subagentIds,
4316 filter?.subagentRunIds,
4317 filter?.subagentChildThreadIds,
4318 ))
4319 ? Effect.succeed([])
4320 : window === undefined
4321 ? sql<PayloadRow>`
4322 SELECT payload_json
4323 FROM orchestration_v2_projection_subagents AS subagent
4324 WHERE ${threadColumn(filter?.subagentIds ?? filter?.subagentChildThreadIds)} = ${threadId}
4325 ${filter?.subagentIds === undefined ? sql`` : sql`AND subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentIds)}))`}
4326 ${filter?.subagentChildThreadIds === undefined ? sql`` : sql`AND child_thread_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentChildThreadIds)}))`}
4327 ${filter?.subagentRunIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentRunIds)}))`}
4328 ${
4329 filter?.omitSettledSubagents === true
4330 ? sql`AND subagent.rowid IN (${unsettledSubagentRowids(threadId)})`
4331 : filter?.omitSettledSubagents !== undefined
4332 ? sql`AND (
4333 subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.omitSettledSubagents.except)}))
4334 OR NOT ${settledSubagent(sql`subagent`)}
4335 )`
4336 : sql``
4337 }
4338 ORDER BY COALESCE(started_at, ''), subagent_id ASC
4339 `
4340 : sql<PayloadRow>`
4341 SELECT payload_json FROM orchestration_v2_projection_subagents
4342 WHERE thread_id = ${threadId}
4343 AND (status IN ('pending','starting','running','waiting')
4344 OR run_id IN (SELECT value FROM json_each(${cohortRunIds}))
4345 OR parent_node_id IN (SELECT value FROM json_each(${cohortNodeIds})))
4346 ORDER BY COALESCE(started_at, ''), subagent_id ASC
4347 `,
4348 fields !== undefined && !fields.includes("providerSessions")
4349 ? Effect.succeed([])
4350 : window === undefined
4351 ? sql<PayloadRow>`
4352 SELECT sessions.payload_json
4353 FROM orchestration_v2_projection_provider_sessions AS sessions
4354 INNER JOIN orchestration_v2_projection_provider_session_bindings AS bindings
4355 ON bindings.provider_session_id = sessions.provider_session_id
4356 WHERE bindings.thread_id = ${threadId}
4357 ORDER BY sessions.updated_at ASC, sessions.provider_session_id ASC
4358 `
4359 : sql<PayloadRow>`
4360 SELECT DISTINCT sessions.payload_json
4361 FROM orchestration_v2_projection_provider_sessions AS sessions
4362 INNER JOIN orchestration_v2_projection_provider_session_bindings AS bindings
4363 ON bindings.provider_session_id = sessions.provider_session_id
4364 LEFT JOIN orchestration_v2_projection_provider_threads AS threads
4365 ON threads.provider_session_id = sessions.provider_session_id
4366 WHERE bindings.thread_id = ${threadId}
4367 AND (sessions.status IN ('starting','running','waiting')
4368 OR threads.provider_thread_id IN (SELECT value FROM json_each(${cohortProviderThreadIds})))
4369 ORDER BY sessions.updated_at ASC, sessions.provider_session_id ASC
4370 `,
4371 (fields !== undefined && !fields.includes("providerThreads")) ||
4372 (window === undefined && matchesNothing(filter?.providerThreadIds))
4373 ? Effect.succeed([])
4374 : window === undefined
4375 ? sql<PayloadRow>`
4376 SELECT payload_json
4377 FROM orchestration_v2_projection_provider_threads
4378 WHERE (thread_id = ${threadId}
4379 OR owner_node_id IN (${nodesOwningProviderThreads(threadId)})
4380 OR ${
4381 filter?.omitSettledSubagentRecords === true
4382 ? // A dispatcher thread holds thousands of settled tasks. Their provider
4383 // threads belong to the child threads; only a session this thread also
4384 // uses is probed for background work when the thread detaches it.
4385 sql`provider_thread_id IN (
4386 SELECT provider_thread_id
4387 FROM orchestration_v2_projection_subagents
4388 WHERE rowid IN (${unsettledSubagentRowids(threadId)})
4389 AND provider_thread_id IS NOT NULL
4390 )
4391 OR (provider_session_id IN (
4392 SELECT provider_session_id
4393 FROM orchestration_v2_projection_provider_session_bindings
4394 WHERE thread_id = ${threadId}
4395 )
4396 AND EXISTS (
4397 SELECT 1 FROM orchestration_v2_projection_subagents AS owner
4398 WHERE owner.provider_thread_id =
4399 orchestration_v2_projection_provider_threads.provider_thread_id
4400 AND owner.thread_id = ${threadId}
4401 ))`
4402 : sql`provider_thread_id IN (
4403 SELECT provider_thread_id
4404 FROM orchestration_v2_projection_subagents
4405 WHERE thread_id = ${threadId}
4406 AND provider_thread_id IS NOT NULL
4407 )`
4408 })
4409 ${filter?.providerThreadIds === undefined ? sql`` : sql`AND provider_thread_id IN (SELECT value FROM json_each(${encodeIdList(filter.providerThreadIds)}))`}
4410 ORDER BY COALESCE(first_run_ordinal, 0), provider_thread_id ASC
4411 `
4412 : sql<PayloadRow>`
4413 SELECT payload_json FROM orchestration_v2_projection_provider_threads
4414 WHERE (thread_id = ${threadId} AND status = 'active')
4415 OR provider_thread_id IN (SELECT value FROM json_each(${cohortProviderThreadIds}))
4416 OR owner_node_id IN (SELECT value FROM json_each(${cohortNodeIds}))
4417 OR (thread_id = ${threadId}
4418 AND provider_thread_id = ${latestTodoList?.provider_thread_id ?? null})
4419 ORDER BY COALESCE(first_run_ordinal, 0), provider_thread_id ASC
4420 `,
4421 (fields !== undefined && !fields.includes("providerTurns")) ||
4422 (window === undefined &&
4423 matchesNothing(
4424 filter?.providerTurnIds,
4425 filter?.providerTurnAttemptIds,
4426 filter?.providerTurnThreadIds,
4427 ))
4428 ? Effect.succeed([])
4429 : window === undefined
4430 ? sql<PayloadRow>`
4431 SELECT payload_json
4432 FROM orchestration_v2_projection_provider_turns
4433 WHERE ${threadColumn(filter?.providerTurnIds ?? filter?.providerTurnAttemptIds)} = ${threadId}
4434 ${filter?.providerTurnIds === undefined ? sql`` : sql`AND provider_turn_id IN (SELECT value FROM json_each(${encodeIdList(filter.providerTurnIds)}))`}
4435 ${
4436 recentRunIds === null
4437 ? sql``
4438 : sql`AND run_attempt_id IN (
4439 SELECT attempt_id FROM orchestration_v2_projection_run_attempts
4440 WHERE thread_id = ${threadId} ${inRecentRuns("run_id")})`
4441 }
4442 ${filter?.providerTurnAttemptIds === undefined ? sql`` : sql`AND run_attempt_id IN (SELECT value FROM json_each(${encodeIdList(filter.providerTurnAttemptIds)}))`}
4443 ${filter?.providerTurnThreadIds === undefined ? sql`` : sql`AND provider_thread_id IN (SELECT value FROM json_each(${encodeIdList(filter.providerTurnThreadIds)}))`}
4444 ${
4445 filter?.latestProviderTurnUsageOnly === true
4446 ? sql`AND json_extract(payload_json, '$.tokenUsage') IS NOT NULL
4447 ORDER BY julianday(json_extract(payload_json, '$.tokenUsage.updatedAt')) DESC,
4448 provider_thread_id ASC, ordinal ASC
4449 LIMIT 1`
4450 : sql`ORDER BY provider_thread_id ASC, ordinal ASC`
4451 }
4452 `
4453 : sql<PayloadRow>`
4454 SELECT payload_json FROM orchestration_v2_projection_provider_turns
4455 WHERE thread_id = ${threadId}
4456 AND (status IN ('starting','running','waiting')
4457 OR provider_turn_id IN (SELECT value FROM json_each(${cohortProviderTurnIds}))
4458 OR node_id IN (SELECT value FROM json_each(${cohortNodeIds})))
4459 ORDER BY provider_thread_id ASC, ordinal ASC
4460 `,
4461 (fields !== undefined && !fields.includes("runtimeRequests")) ||
4462 (window === undefined && matchesNothing(filter?.runtimeRequestStatuses))
4463 ? Effect.succeed([])
4464 : window === undefined
4465 ? sql<PayloadRow>`
4466 SELECT payload_json
4467 FROM orchestration_v2_projection_runtime_requests
4468 WHERE thread_id = ${threadId}
4469 ${filter?.runtimeRequestStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runtimeRequestStatuses)}))`}
4470 ORDER BY created_at ASC, runtime_request_id ASC
4471 `
4472 : sql<PayloadRow>`
4473 SELECT payload_json FROM orchestration_v2_projection_runtime_requests
4474 WHERE thread_id = ${threadId}
4475 AND (status IN ('pending','waiting')
4476 OR node_id IN (SELECT value FROM json_each(${cohortNodeIds}))
4477 OR provider_turn_id IN (SELECT value FROM json_each(${cohortProviderTurnIds})))
4478 ORDER BY created_at ASC, runtime_request_id ASC
4479 `,
4480 (fields !== undefined && !fields.includes("messages")) ||
4481 (window === undefined &&
4482 matchesNothing(filter?.messageIds, filter?.messageRoles, filter?.messageRunIds))
4483 ? Effect.succeed([])
4484 : window === undefined
4485 ? sql<PayloadRow>`
4486 SELECT payload_json FROM orchestration_v2_projection_messages
4487 ${recentRunIndex("messages_run_idx")}
4488 WHERE ${recentRunIds === null ? threadColumn(filter?.messageIds) : sql.literal("thread_id")} = ${threadId}
4489 ${inRecentRuns("run_id")}
4490 ${filter?.messageIds === undefined ? sql`` : sql`AND message_id IN (SELECT value FROM json_each(${encodeIdList(filter.messageIds)}))`}
4491 ${filter?.messageRoles === undefined ? sql`` : sql`AND role IN (SELECT value FROM json_each(${encodeIdList(filter.messageRoles)}))`}
4492 ${filter?.monitorNotificationsOnly === true ? sql`AND json_extract(payload_json, '$.notification.source.kind') = 'monitor'` : sql``}
4493 ${filter?.delegatedTaskMessagesOnly === true ? sql`AND ${sql.literal(DELEGATED_TASK_MESSAGE_CONDITION)}` : sql``}
4494 ${filter?.messageRunIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.messageRunIds)}))`}
4495 ORDER BY created_at ASC, message_id ASC
4496 `
4497 : sql<PayloadRow>`
4498 -- Look the window's messages up by key and the live runs' by run. One filter
4499 -- over the thread reads every message the thread ever had.
4500 SELECT payload_json FROM (
4501 SELECT message.payload_json, message.created_at, message.message_id
4502 FROM json_each(${cohortMessageIds}) AS wanted
4503 CROSS JOIN orchestration_v2_projection_messages AS message
4504 ON message.message_id = wanted.value
4505 WHERE message.thread_id = ${threadId}
4506 UNION
4507 SELECT message.payload_json, message.created_at, message.message_id
4508 FROM orchestration_v2_projection_runs AS run
4509 CROSS JOIN orchestration_v2_projection_messages AS message
4510 ON message.run_id = run.run_id
4511 WHERE run.thread_id = ${threadId}
4512 AND run.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
4513 AND message.thread_id = ${threadId}
4514 )
4515 ORDER BY created_at ASC, message_id ASC
4516 `,
4517 fields !== undefined && !fields.includes("plans")
4518 ? Effect.succeed([])
4519 : window === undefined
4520 ? sql<PayloadRow>`
4521 SELECT payload_json FROM orchestration_v2_projection_plans
4522 WHERE thread_id = ${threadId} ORDER BY plan_id ASC
4523 `
4524 : sql<PayloadRow>`
4525 SELECT payload_json FROM orchestration_v2_projection_plans AS plan
4526 WHERE plan.thread_id = ${threadId}
4527 AND (
4528 (plan.status = 'active' AND NOT ${todoListOfSettledRun(sql`plan`)})
4529 OR plan.plan_id = ${latestTodoList?.plan_id ?? null}
4530 OR plan.plan_id IN (
4531 SELECT value FROM json_each(${cohortPlanIds})
4532 )
4533 )
4534 -- Keep the selected artifact last within its run for equal-run consumers.
4535 ORDER BY CASE WHEN plan_id = ${latestTodoList?.plan_id ?? null}
4536 THEN ${latestTodoList?.last_plan_id ?? null} ELSE plan_id END ASC,
4537 plan_id = ${latestTodoList?.plan_id ?? null} ASC
4538 `,
4539 fields !== undefined && !fields.includes("checkpointScopes")
4540 ? Effect.succeed([])
4541 : window === undefined
4542 ? sql<PayloadRow>`
4543 SELECT payload_json
4544 FROM orchestration_v2_projection_checkpoint_scopes
4545 WHERE thread_id = ${threadId}
4546 ORDER BY ordinal_within_parent ASC, scope_id ASC
4547 `
4548 : sql<PayloadRow>`
4549 SELECT payload_json FROM orchestration_v2_projection_checkpoint_scopes
4550 WHERE thread_id = ${threadId}
4551 AND (run_id IN (SELECT value FROM json_each(${cohortRunIds}))
4552 OR node_id IN (SELECT value FROM json_each(${cohortNodeIds})))
4553 ORDER BY ordinal_within_parent ASC, scope_id ASC
4554 `,
4555 fields !== undefined && !fields.includes("checkpoints")
4556 ? Effect.succeed([])
4557 : window === undefined
4558 ? sql<PayloadRow>`
4559 SELECT payload_json
4560 FROM orchestration_v2_projection_checkpoints
4561 WHERE thread_id = ${threadId}
4562 ORDER BY scope_id ASC, ordinal_within_scope ASC
4563 `
4564 : sql<PayloadRow>`
4565 SELECT payload_json FROM orchestration_v2_projection_checkpoints
4566 WHERE thread_id = ${threadId}
4567 AND (status IN ('pending','capturing')
4568 OR (run_id IS NULL AND ordinal_within_scope = 0 AND status = 'ready')
4569 -- The diff of the last finished turn stays in reach while a long run is at work.
4570 OR checkpoint_id = (
4571 SELECT checkpoint_id FROM orchestration_v2_projection_checkpoints
4572 WHERE thread_id = ${threadId} AND status = 'ready'
4573 AND app_run_ordinal IS NOT NULL
4574 ORDER BY app_run_ordinal DESC LIMIT 1
4575 )
4576 OR checkpoint_id IN (SELECT value FROM json_each(${cohortCheckpointIds}))
4577 OR run_id IN (SELECT value FROM json_each(${cohortRunIds}))
4578 OR node_id IN (SELECT value FROM json_each(${cohortNodeIds})))
4579 ORDER BY scope_id ASC, ordinal_within_scope ASC
4580 `,
4581 fields !== undefined && !fields.includes("contextHandoffs")
4582 ? Effect.succeed([])
4583 : window === undefined
4584 ? sql<PayloadRow>`
4585 SELECT payload_json FROM orchestration_v2_projection_context_handoffs
4586 WHERE thread_id = ${threadId} ORDER BY rowid ASC
4587 `
4588 : sql<PayloadRow>`
4589 -- Megabytes on a long thread, and projectContextHandoffForWire drops both.
4590 SELECT json_remove(payload_json, '$.history', '$.delivery') AS payload_json
4591 FROM orchestration_v2_projection_context_handoffs AS handoff
4592 WHERE handoff.thread_id = ${threadId}
4593 AND (
4594 handoff.status IN ('pending', 'ready')
4595 OR handoff.context_handoff_id IN (
4596 SELECT value FROM json_each(${cohortHandoffIds})
4597 )
4598 )
4599 ORDER BY rowid ASC
4600 `,
4601 (fields !== undefined && !fields.includes("contextTransfers")) ||
4602 (window === undefined && matchesNothing(filter?.contextTransferStatuses))
4603 ? Effect.succeed([])
4604 : window === undefined
4605 ? sql<PayloadRow>`
4606 SELECT payload_json
4607 FROM orchestration_v2_projection_context_transfers
4608 WHERE ${
4609 filter?.contextTransferStatuses === undefined
4610 ? sql`(source_thread_id = ${threadId} OR target_thread_id = ${threadId})`
4611 : // Each side seeks its (thread, status) index. One OR over both sides checks the
4612 // status on every row the thread ever sent or received.
4613 sql`rowid IN (
4614 SELECT rowid FROM orchestration_v2_projection_context_transfers
4615 WHERE source_thread_id = ${threadId}
4616 AND status IN (SELECT value FROM json_each(${encodeIdList(filter.contextTransferStatuses)}))
4617 UNION
4618 SELECT rowid FROM orchestration_v2_projection_context_transfers
4619 WHERE target_thread_id = ${threadId}
4620 AND status IN (SELECT value FROM json_each(${encodeIdList(filter.contextTransferStatuses)}))
4621 )`
4622 }
4623 ORDER BY rowid ASC
4624 `
4625 : sql<PayloadRow>`
4626 SELECT payload_json FROM orchestration_v2_projection_context_transfers
4627 WHERE (source_thread_id = ${threadId} OR target_thread_id = ${threadId})
4628 AND (status IN ('pending','running','waiting')
4629 OR target_run_id IN (SELECT value FROM json_each(${cohortRunIds}))
4630 OR json_extract(payload_json, '$.contextHandoffId') IN
4631 (SELECT value FROM json_each(${cohortHandoffIds})))
4632 ORDER BY rowid ASC
4633 `,
4634 ]);
4635
4636 const [
4637 runs,
4638 attempts,
4639 nodes,
4640 subagents,
4641 providerSessions,
4642 providerThreads,
4643 providerTurns,
4644 runtimeRequests,
4645 messages,
4646 plans,
4647 checkpointScopes,
4648 checkpoints,
4649 contextHandoffs,
4650 contextTransfers,
4651 ] = yield* Effect.all([
4652 decodeRows(decodeRunPayload, threadId)(runRows),
4653 decodeRows(decodeRunAttemptPayload, threadId)(attemptRows),
4654 decodeRows(decodeNodePayload, threadId)(nodeRows),
4655 decodeRows(decodeSubagentPayload, threadId)(subagentRows),
4656 decodeRows(decodeProviderSessionPayload, threadId)(providerSessionRows),
4657 decodeRows(decodeProviderThreadPayload, threadId)(providerThreadRows),
4658 decodeRows(decodeProviderTurnPayload, threadId)(providerTurnRows),
4659 decodeRows(decodeRuntimeRequestPayload, threadId)(runtimeRequestRows),
4660 decodeRows(decodeMessagePayload, threadId)(messageRows),
4661 decodeRows(decodePlanJson, threadId)(planRows),
4662 decodeRows(decodeCheckpointScopePayload, threadId)(checkpointScopeRows),
4663 decodeRows(decodeCheckpointPayload, threadId)(checkpointRows),
4664 decodeRows(decodeContextHandoffPayload, threadId)(contextHandoffRows),
4665 decodeRows(decodeContextTransferPayload, threadId)(contextTransferRows),
4666 ]);
4667 const turnItems =
4668 promptRuns === null || (fields !== undefined && !fields.includes("turnItems"))
4669 ? storedTurnItems
4670 : yield* commandTurnItems(threadId, runs, promptRuns);
4671 const messageOrdinals = new Map<string, number>();
4672 // Only user items can name a user message, so a user-only read needs no assistant items.
4673 const userOnly = messages.every((message) => message.role === "user");
4674 // The loaded items already order the messages when they hold every item the lookup reads.
4675 const loadsEveryItemOf = (type: OrchestrationV2TurnItem["type"]) =>
4676 window === undefined &&
4677 promptRuns === null &&
4678 (fields === undefined || fields.includes("turnItems")) &&
4679 filter?.turnItemRequestIds === undefined &&
4680 filter?.turnItemStatuses === undefined &&
4681 filter?.turnItemRunId === undefined &&
4682 filter?.turnItemRunIds === undefined &&
4683 filter?.turnItemNodeIds === undefined &&
4684 (filter?.turnItemTypes === undefined || filter.turnItemTypes.includes(type));
4685 if (
4686 fields !== undefined &&
4687 fields.includes("messages") &&
4688 messages.length > 1 &&
4689 !(loadsEveryItemOf("user_message") && (userOnly || loadsEveryItemOf("assistant_message")))
4690 ) {
4691 // Broad role reads keep the original query to avoid per-ID lookup overhead.
4692 const loadedMessageIds =
4693 filter?.messageIds === undefined &&
4694 filter?.messageRunIds === undefined &&
4695 (filter?.messageRoles === undefined ||
4696 (filter.messageRoles.includes("user") && filter.messageRoles.includes("assistant")))
4697 ? undefined
4698 : messages.map((message) => message.id);
4699 // Migration 94 indexes exactly this expression, type list and json_valid term: changing
4700 // any, even the IN order, silently drops the index. ProjectionStore.test.ts asserts the plan.
4701 const rows = yield* sql<{ message_id: string; ordinal: number }>`
4702 SELECT json_extract(payload_json, '$.messageId') AS message_id, MIN(ordinal) AS ordinal
4703 FROM orchestration_v2_projection_turn_items
4704 WHERE thread_id = ${threadId} AND type IN ('user_message', 'assistant_message')
4705 AND json_valid(payload_json)
4706 ${loadedMessageIds === undefined ? sql`` : sql`AND json_extract(payload_json, '$.messageId') IN (SELECT value FROM json_each(${encodeIdList(loadedMessageIds)}))`}
4707 GROUP BY json_extract(payload_json, '$.messageId')
4708 `;
4709 for (const row of rows) messageOrdinals.set(row.message_id, row.ordinal);
4710 }
4711 const orderedMessages = sortMessagesByTurnItemOrder(messages, turnItems, messageOrdinals);
4712 const projection = {
4713 thread,
4714 runs,
4715 attempts,
4716 nodes,
4717 subagents,
4718 providerSessions,
4719 providerThreads,
4720 providerTurns,
4721 runtimeRequests,
4722 messages: orderedMessages,
4723 plans,
4724 turnItems,
4725 checkpointScopes,
4726 checkpoints,
4727 contextHandoffs,
4728 contextTransfers,
4729 visibleTurnItems: [],
4730 updatedAt: thread.updatedAt,
4731 } satisfies OrchestrationV2ThreadProjection;
4732 return projection;
4733 },
4734 (effect, threadId, _window?, _fields?, _filter?) =>
4735 effect.pipe(
4736 Effect.mapError((cause) =>
4737 isProjectionStoreThreadNotFoundError(cause)
4738 ? cause
4739 : new ProjectionStoreReadError({
4740 threadId,
4741 cause,
4742 }),
4743 ),
4744 ),
4745 );
4746
4747 const readProjection: (
4748 threadId: ThreadId,
4749 seenThreadIds: ReadonlySet<ThreadId>,
4750 window?: ProjectionWindow,
4751 ) => Effect.Effect<OrchestrationV2ThreadProjection, ProjectionStoreV2Error> = Effect.fn(
4752 "ProjectionStore.readProjection",
4753 )(
4754 function* (
4755 threadId: ThreadId,
4756 seenThreadIds: ReadonlySet<ThreadId>,
4757 window?: ProjectionWindow,
4758 ) {
4759 const localWindow =
4760 window?.suppressLocal === true ||
4761 (window?.historyAnchor !== undefined && window.historyAnchor.threadId !== threadId)
4762 ? { ...window, rowLimit: 0, anchorItemId: undefined }
4763 : window;
4764 const projection = yield* readCanonicalProjection(
4765 threadId,
4766 localWindow,
4767 window !== undefined && seenThreadIds.size > 0
4768 ? ["runs", "attempts", "turnItems"]
4769 : undefined,
4770 );
4771 const forkedFrom = inheritedRunFork(projection.thread);
4772 if (forkedFrom === null || seenThreadIds.has(forkedFrom.threadId)) {
4773 return withLocalVisibleTurnItems(projection);
4774 }
4775
4776 // Page unanchored local segments fully before inherited turns affect the boundary.
4777 if (
4778 window?.userTurnLimit !== undefined &&
4779 localWindow !== undefined &&
4780 localWindow.rowLimit > 0 &&
4781 projection.turnItems.length >= localWindow.rowLimit &&
4782 !projection.turnItems.some(isThreadHistoryUserTurn)
4783 ) {
4784 return withLocalVisibleTurnItems(projection);
4785 }
4786
4787 const sourceWindow =
4788 window === undefined
4789 ? undefined
4790 : yield* Effect.gen(function* () {
4791 const historyAnchor = window.historyAnchor;
4792 const anchorBelongsToSource = historyAnchor?.threadId === forkedFrom.threadId;
4793 const rows = yield* sql<{ readonly turn_item_id: string }>`
4794 WITH fork_run AS (
4795 SELECT ordinal
4796 FROM orchestration_v2_projection_runs
4797 WHERE thread_id = ${forkedFrom.threadId}
4798 AND run_id = ${forkedFrom.runId}
4799 LIMIT 1
4800 ), fork_boundary AS (
4801 SELECT (
4802 SELECT item.ordinal
4803 FROM orchestration_v2_projection_turn_items AS item
4804 WHERE item.run_id = run.run_id
4805 ORDER BY item.ordinal DESC
4806 LIMIT 1
4807 ) AS ordinal
4808 FROM orchestration_v2_projection_runs AS run
4809 WHERE run.thread_id = ${forkedFrom.threadId}
4810 AND run.ordinal <= (SELECT ordinal FROM fork_run)
4811 AND EXISTS (
4812 SELECT 1
4813 FROM orchestration_v2_projection_turn_items AS item
4814 WHERE item.run_id = run.run_id
4815 LIMIT 1
4816 )
4817 ORDER BY run.ordinal DESC
4818 LIMIT 1
4819 ), effective_boundary AS (
4820 SELECT COALESCE(
4821 (SELECT ordinal FROM fork_boundary),
4822 (
4823 SELECT ordinal
4824 FROM orchestration_v2_projection_turn_items
4825 WHERE thread_id = ${forkedFrom.threadId}
4826 AND run_id IS NULL
4827 AND json_extract(payload_json, '$.historyOrigin') = 'v1_import'
4828 ORDER BY ordinal DESC, turn_item_id DESC
4829 LIMIT 1
4830 ),
4831 -1
4832 ) AS ordinal
4833 )
4834 SELECT turn_item_id
4835 FROM orchestration_v2_projection_turn_items
4836 WHERE thread_id = ${forkedFrom.threadId}
4837 AND ordinal <= COALESCE(
4838 (SELECT ordinal FROM effective_boundary),
4839 -1
4840 )
4841 AND (
4842 ${anchorBelongsToSource ? 1 : 0} = 0
4843 OR ordinal <= COALESCE(
4844 (SELECT ordinal FROM orchestration_v2_projection_turn_items
4845 WHERE thread_id = ${forkedFrom.threadId}
4846 AND turn_item_id = ${historyAnchor?.itemId ?? null}
4847 LIMIT 1),
4848 (SELECT ordinal FROM effective_boundary),
4849 -1
4850 )
4851 )
4852 ORDER BY ordinal DESC, turn_item_id DESC
4853 LIMIT 1
4854 `;
4855 const anchor = rows[0]?.turn_item_id;
4856 const anchorIsInDescendant =
4857 historyAnchor !== undefined &&
4858 historyAnchor.threadId !== threadId &&
4859 historyAnchor.threadId !== forkedFrom.threadId;
4860 return {
4861 rowLimit: window.rowLimit,
4862 userTurnLimit: window.userTurnLimit,
4863 deferWork: window.deferWork,
4864 requiredRunId: forkedFrom.runId,
4865 forkCreatedAt: projection.thread.createdAt,
4866 suppressLocal: anchor === undefined || anchorIsInDescendant,
4867 ...(anchor === undefined || anchorIsInDescendant
4868 ? {}
4869 : { anchorItemId: TurnItemId.make(anchor) }),
4870 ...(historyAnchor === undefined || historyAnchor.threadId === threadId
4871 ? {}
4872 : { historyAnchor }),
4873 };
4874 });
4875
4876 // Filled local pages need only the inherited count for cursor positions.
4877 // Run-forked parents still need the recursive read to include their ancestry.
4878 if (
4879 window?.userTurnLimit !== undefined &&
4880 window.historyAnchor === undefined &&
4881 seenThreadIds.size === 0 &&
4882 sourceWindow !== undefined &&
4883 activeLocalTurnItems(projection).filter((row) => isThreadHistoryUserTurn(row.item))
4884 .length >= Math.max(window.userTurnLimit, THREAD_HISTORY_PAGE_POLICY.maxUserTurns)
4885 ) {
4886 const counted = yield* sql<{ readonly inherited_rows: number | null }>`
4887 ${yield* retainedWindowRows(forkedFrom.threadId, {
4888 ...sourceWindow,
4889 rowLimit: sourceWindow.suppressLocal ? 0 : sourceWindow.rowLimit,
4890 })}
4891 SELECT CASE
4892 WHEN json_extract(source.payload_json, '$.forkedFrom.type') = 'run' THEN NULL
4893 WHEN NOT EXISTS (
4894 SELECT 1 FROM orchestration_v2_projection_runs
4895 WHERE thread_id = source.thread_id AND run_id = ${forkedFrom.runId}
4896 ) THEN 0
4897 ELSE (SELECT COUNT(*) FROM retained)
4898 END AS inherited_rows
4899 FROM orchestration_v2_projection_threads AS source
4900 WHERE source.thread_id = ${forkedFrom.threadId}
4901 `;
4902 const inheritedRowCount = counted[0]?.inherited_rows ?? null;
4903 if (inheritedRowCount !== null) {
4904 return {
4905 ...projection,
4906 visibleTurnItems: buildVisibleTurnItems({
4907 projection,
4908 sourceProjection: null,
4909 inheritedRowCount,
4910 }),
4911 };
4912 }
4913 }
4914
4915 const sourceProjection = yield* readProjection(
4916 forkedFrom.threadId,
4917 new Set([...seenThreadIds, threadId]),
4918 sourceWindow,
4919 );
4920 return {
4921 ...projection,
4922 visibleTurnItems: buildVisibleTurnItems({
4923 projection,
4924 sourceProjection,
4925 }),
4926 };
4927 },
4928 (effect, threadId, _seenThreadIds, _window?) =>
4929 effect.pipe(
4930 Effect.mapError((cause) =>
4931 isProjectionStoreThreadNotFoundError(cause) || isProjectionStoreReadError(cause)
4932 ? cause
4933 : new ProjectionStoreReadError({ threadId, cause }),
4934 ),
4935 ),
4936 );
4937
4938 const getLimitRecoveryCandidates = Effect.fn("ProjectionStore.getLimitRecoveryCandidates")(
4939 function* (options: Parameters<ProjectionStoreV2Shape["getLimitRecoveryCandidates"]>[0]) {
4940 const rows = yield* sql<{
4941 readonly payload_json: string;
4942 readonly run_id: string;
4943 readonly completed_at: string | null;
4944 readonly failure_payload_json: string;
4945 readonly last_error: string | null;
4946 }>`
4947 SELECT t.payload_json, r.run_id, r.completed_at,
4948 item.payload_json AS failure_payload_json,
4949 (
4950 SELECT json_extract(session.payload_json, '$.lastError')
4951 -- Keep bindings outermost: one provider instance can hold every session.
4952 FROM orchestration_v2_projection_provider_session_bindings binding
4953 CROSS JOIN orchestration_v2_projection_provider_sessions session
4954 ON session.provider_session_id = binding.provider_session_id
4955 WHERE binding.thread_id = t.thread_id
4956 AND session.provider_instance_id = t.provider_instance_id
4957 ORDER BY session.updated_at DESC, session.provider_session_id DESC
4958 LIMIT 1
4959 ) AS last_error
4960 FROM orchestration_v2_projection_threads t
4961 INNER JOIN orchestration_v2_projection_runs r ON r.run_id = (
4962 SELECT latest.run_id FROM orchestration_v2_projection_runs latest
4963 WHERE latest.thread_id = t.thread_id
4964 AND latest.status <> 'queued'
4965 AND NOT (
4966 latest.status = 'cancelled'
4967 AND json_extract(latest.payload_json, '$.startedAt') IS NULL
4968 )
4969 -- latestExecutedRun: the run that ended last (runRanAfter).
4970 ORDER BY latest.completed_at IS NULL DESC, latest.completed_at DESC,
4971 latest.ordinal DESC, latest.run_id DESC
4972 LIMIT 1
4973 ) AND r.status = 'failed'
4974 INNER JOIN orchestration_v2_projection_turn_items item ON item.turn_item_id = (
4975 SELECT error.turn_item_id FROM orchestration_v2_projection_turn_items error
4976 WHERE error.thread_id = t.thread_id AND error.run_id = r.run_id
4977 AND error.type = 'error' AND error.status = 'failed'
4978 AND error.node_id IS json_extract(r.payload_json, '$.rootNodeId')
4979 ORDER BY error.updated_at DESC, error.ordinal DESC, error.turn_item_id DESC
4980 LIMIT 1
4981 )
4982 WHERE t.deleted_at IS NULL
4983 -- The sweep runs every scheduler tick. Without this list the latest-run
4984 -- lookup above sorts every run of every thread, tens of milliseconds
4985 -- for one long thread; only threads with a usage-limit failure qualify.
4986 AND t.thread_id IN (
4987 SELECT failure.thread_id FROM orchestration_v2_projection_turn_items failure
4988 -- Spelled as in 099_BackgroundRecoveryIndexes so the partial index serves it.
4989 WHERE failure.type = 'error' AND failure.status = 'failed'
4990 AND CASE WHEN json_valid(failure.payload_json) THEN
4991 json_extract(failure.payload_json, '$.failure.class') = 'usage_limit'
4992 ELSE 0 END
4993 )
4994 AND t.archived_at IS NULL
4995 AND json_extract(t.payload_json, '$.settledOverride') IS NOT 'settled'
4996 AND json_valid(item.payload_json)
4997 AND json_extract(item.payload_json, '$.failure.class') = 'usage_limit'
4998 AND json_extract(item.payload_json, '$.failure.resetAt') IS NOT NULL
4999 AND julianday(json_extract(item.payload_json, '$.failure.resetAt')) > julianday(COALESCE(r.completed_at, json_extract(t.payload_json, '$.updatedAt')))
5000 AND (
Showing first 5,000 of 9,842 lines. View raw