Open History to see the last commit
1/**
2 * ProjectionSnapshotQuery - Read-model snapshot query service interface.
3 *
4 * Exposes the current orchestration projection snapshot for read-only API
5 * access.
6 *
7 * @module ProjectionSnapshotQuery
8 */
9import type {
10 CheckpointRef,
11 OrchestrationCheckpointSummary,
12 OrchestrationProject,
13 OrchestrationProjectShell,
14 OrchestrationReadModel,
15 OrchestrationSearchThreadsInput,
16 OrchestrationSearchThreadsResult,
17 OrchestrationShellSnapshot,
18 OrchestrationThread,
19 OrchestrationThreadDetailSnapshot,
20 OrchestrationThreadDetailWindow,
21 OrchestrationThreadShell,
22 ProjectId,
23 ThreadId,
24} from "@t3tools/contracts";
25import * as Context from "effect/Context";
26import type * as Option from "effect/Option";
27import type * as Effect from "effect/Effect";
28
29import type { ProjectionRepositoryError } from "../../persistence/Errors.ts";
30
31export interface ProjectionSnapshotCounts {
32 readonly projectCount: number;
33 readonly threadCount: number;
34}
35
36export interface ProjectionSnapshotSequence {
37 readonly snapshotSequence: number;
38}
39
40export interface ProjectionThreadCheckpointContext {
41 readonly threadId: ThreadId;
42 readonly projectId: ProjectId;
43 readonly workspaceRoot: string;
44 readonly worktreePath: string | null;
45 readonly checkpoints: ReadonlyArray<OrchestrationCheckpointSummary>;
46}
47
48export interface ProjectionFullThreadDiffContext {
49 readonly threadId: ThreadId;
50 readonly projectId: ProjectId;
51 readonly workspaceRoot: string;
52 readonly worktreePath: string | null;
53 readonly latestCheckpointTurnCount: number;
54 readonly toCheckpointRef: CheckpointRef | null;
55}
56
57export interface ProjectionThreadDetailQuery {
58 /**
59 * Limit activities before SQLite returns and decodes their payloads.
60 * Any explicit filter omits pinned-request reads. An empty list also skips
61 * the activity query. Omit this option to preserve the full detail response.
62 */
63 readonly activityKinds?: ReadonlyArray<string>;
64}
65
66/**
67 * ProjectionSnapshotQueryShape - Service API for read-model snapshots.
68 */
69export interface ProjectionSnapshotQueryShape {
70 /**
71 * Read the lightweight command snapshot used to bootstrap the in-memory
72 * orchestration engine without hydrating message/activity/checkpoint bodies.
73 */
74 readonly getCommandReadModel: () => Effect.Effect<
75 OrchestrationReadModel,
76 ProjectionRepositoryError
77 >;
78
79 /**
80 * Read the latest orchestration projection snapshot.
81 *
82 * Rehydrates from projection tables and derives snapshot sequence from
83 * projector cursor state.
84 */
85 readonly getSnapshot: () => Effect.Effect<OrchestrationReadModel, ProjectionRepositoryError>;
86
87 /**
88 * Read the latest orchestration shell snapshot.
89 *
90 * Returns only projects and thread shell summaries so clients can bootstrap
91 * lightweight navigation state without hydrating every thread body.
92 */
93 readonly getShellSnapshot: () => Effect.Effect<
94 OrchestrationShellSnapshot,
95 ProjectionRepositoryError
96 >;
97
98 /**
99 * Read archived thread shell summaries for the archive page.
100 *
101 * This query is separate from the main shell snapshot so archived threads
102 * are never bootstrapped into normal navigation state.
103 */
104 readonly getArchivedShellSnapshot: () => Effect.Effect<
105 OrchestrationShellSnapshot,
106 ProjectionRepositoryError
107 >;
108
109 /**
110 * Search active thread navigation metadata, user messages, and canonical
111 * assistant outputs without hydrating thread detail snapshots.
112 */
113 readonly searchThreads: (
114 input: OrchestrationSearchThreadsInput,
115 ) => Effect.Effect<OrchestrationSearchThreadsResult, ProjectionRepositoryError>;
116
117 /**
118 * Read the latest projection snapshot sequence without hydrating read-model
119 * entities.
120 */
121 readonly getSnapshotSequence: () => Effect.Effect<
122 ProjectionSnapshotSequence,
123 ProjectionRepositoryError
124 >;
125
126 /**
127 * Read aggregate projection counts without hydrating the full read model.
128 */
129 readonly getCounts: () => Effect.Effect<ProjectionSnapshotCounts, ProjectionRepositoryError>;
130
131 /**
132 * Read the active project for an exact workspace root match.
133 */
134 readonly getActiveProjectByWorkspaceRoot: (
135 workspaceRoot: string,
136 ) => Effect.Effect<Option.Option<OrchestrationProject>, ProjectionRepositoryError>;
137
138 /**
139 * Read a single active project shell row by id.
140 */
141 readonly getProjectShellById: (
142 projectId: ProjectId,
143 ) => Effect.Effect<Option.Option<OrchestrationProjectShell>, ProjectionRepositoryError>;
144
145 /**
146 * Read the earliest active thread for a project.
147 */
148 readonly getFirstActiveThreadIdByProjectId: (
149 projectId: ProjectId,
150 ) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;
151
152 /**
153 * Read the checkpoint context needed to resolve a single thread diff.
154 */
155 readonly getThreadCheckpointContext: (
156 threadId: ThreadId,
157 ) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
158
159 /**
160 * Read only the narrow context needed to compute a full-thread diff from
161 * checkpoint 0 to a specific turn count.
162 */
163 readonly getFullThreadDiffContext: (
164 threadId: ThreadId,
165 toTurnCount: number,
166 ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
167
168 /**
169 * Read a single active thread shell row by id.
170 */
171 readonly getThreadShellById: (
172 threadId: ThreadId,
173 ) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
174
175 /**
176 * Read a single active thread detail snapshot by id.
177 */
178 readonly getThreadDetailById: (
179 threadId: ThreadId,
180 query?: ProjectionThreadDetailQuery,
181 ) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
182
183 /**
184 * Read a single active thread detail together with the projection snapshot
185 * sequence in one consistent transaction, so the returned `snapshotSequence`
186 * exactly matches the state reflected in `thread` (no interleaving projector
187 * update between the two reads).
188 *
189 * When `window` is provided, the thread's messages, activities, proposed
190 * plans, and checkpoints are bounded to a page of recent turns and the
191 * response carries `page` metadata (see `OrchestrationThreadDetailWindow`).
192 * Without a window the full thread is returned with no `page` field —
193 * pagination is strictly opt-in.
194 *
195 * Activity payloads are projected for clients as they are read in small
196 * sequential batches. Callers still apply the full snapshot projector for
197 * collection-level activity pruning.
198 */
199 readonly getThreadDetailSnapshot: (
200 threadId: ThreadId,
201 window?: OrchestrationThreadDetailWindow,
202 ) => Effect.Effect<Option.Option<OrchestrationThreadDetailSnapshot>, ProjectionRepositoryError>;
203}
204
205/**
206 * ProjectionSnapshotQuery - Service tag for projection snapshot queries.
207 */
208export class ProjectionSnapshotQuery extends Context.Service<
209 ProjectionSnapshotQuery,
210 ProjectionSnapshotQueryShape
211>()("t3/orchestration/Services/ProjectionSnapshotQuery") {}