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 AgentSessionImportSource,
11 ApprovalRequestId,
12 CheckpointRef,
13 MessageId,
14 ProjectId,
15 ThreadId,
16} from "@t3tools/contracts";
17import type {
18 OrchestrationCheckpointSummary,
19 OrchestrationMessage,
20 OrchestrationProject,
21 OrchestrationProjectShell,
22 OrchestrationReadModel,
23 OrchestrationSearchThreadsInput,
24 OrchestrationSearchThreadsResult,
25 OrchestrationShellSnapshot,
26 OrchestrationThread,
27 OrchestrationThreadActivity,
28 OrchestrationThreadDetailSnapshot,
29 OrchestrationThreadDetailWindow,
30 OrchestrationThreadShell,
31} from "@t3tools/contracts/legacy-orchestration";
32import * as Context from "effect/Context";
33import type * as Option from "effect/Option";
34import type * as Effect from "effect/Effect";
35
36import type { ProjectionRepositoryError } from "../../persistence/Errors.ts";
37
38export interface ProjectionSnapshotCounts {
39 readonly projectCount: number;
40 readonly threadCount: number;
41}
42
43export interface ProjectionSnapshotSequence {
44 readonly snapshotSequence: number;
45}
46
47export interface ProjectionEventReplayStats {
48 readonly eventCount: number;
49 readonly payloadBytes: number;
50}
51
52export interface ProjectionThreadCheckpointContext {
53 readonly threadId: ThreadId;
54 readonly projectId: ProjectId;
55 readonly workspaceRoot: string;
56 readonly worktreePath: string | null;
57 readonly checkpoints: ReadonlyArray<OrchestrationCheckpointSummary>;
58}
59
60export interface ProjectionFullThreadDiffContext {
61 readonly threadId: ThreadId;
62 readonly projectId: ProjectId;
63 readonly workspaceRoot: string;
64 readonly worktreePath: string | null;
65 readonly latestCheckpointTurnCount: number;
66 readonly toCheckpointRef: CheckpointRef | null;
67}
68
69export interface ProjectionThreadDetailQuery {
70 /**
71 * Limit activities before SQLite returns and decodes their payloads.
72 * Any explicit filter omits pinned-request reads. An empty list also skips
73 * the activity query. Omit this option to preserve the full detail response.
74 */
75 readonly activityKinds?: ReadonlyArray<string>;
76}
77
78/**
79 * ProjectionSnapshotQueryShape - Service API for read-model snapshots.
80 */
81export interface ProjectionSnapshotQueryShape {
82 /** Read the latest request or resolution without loading the thread history. */
83 readonly getUserInputActivity: (input: {
84 readonly threadId: ThreadId;
85 readonly requestId: ApprovalRequestId;
86 }) => Effect.Effect<Option.Option<OrchestrationThreadActivity>, ProjectionRepositoryError>;
87
88 /**
89 * Read every activity of one kind across active (not deleted, not archived)
90 * threads, without hydrating the threads. Used at startup to find state a
91 * crashed process left behind.
92 */
93 readonly listActivitiesByKind: (
94 kind: string,
95 ) => Effect.Effect<ReadonlyArray<OrchestrationThreadActivity>, ProjectionRepositoryError>;
96
97 /**
98 * Read the lightweight command snapshot used to bootstrap the in-memory
99 * orchestration engine without hydrating message/activity/checkpoint bodies.
100 */
101 readonly getCommandReadModel: () => Effect.Effect<
102 OrchestrationReadModel,
103 ProjectionRepositoryError
104 >;
105
106 /**
107 * Read the latest orchestration projection snapshot.
108 *
109 * Rehydrates from projection tables and derives snapshot sequence from
110 * projector cursor state.
111 */
112 readonly getSnapshot: () => Effect.Effect<OrchestrationReadModel, ProjectionRepositoryError>;
113
114 /**
115 * Read the latest orchestration shell snapshot.
116 *
117 * Returns only projects and thread shell summaries so clients can bootstrap
118 * lightweight navigation state without hydrating every thread body.
119 */
120 readonly getShellSnapshot: () => Effect.Effect<
121 OrchestrationShellSnapshot,
122 ProjectionRepositoryError
123 >;
124
125 /**
126 * Read the shell snapshot with null optional repository metadata.
127 *
128 * Transactional callers use this method and enrich the returned projects
129 * only after their transaction has closed.
130 */
131 readonly getShellSnapshotWithoutEnrichment: () => Effect.Effect<
132 OrchestrationShellSnapshot,
133 ProjectionRepositoryError
134 >;
135
136 /**
137 * Read archived thread shell summaries for the archive page.
138 *
139 * This query is separate from the main shell snapshot so archived threads
140 * are never bootstrapped into normal navigation state.
141 */
142 readonly getArchivedShellSnapshot: () => Effect.Effect<
143 OrchestrationShellSnapshot,
144 ProjectionRepositoryError
145 >;
146
147 /** Durable worktree ownership retained after thread deletion, including across restarts. */
148 readonly getDeletedWorktreeThreads: () => Effect.Effect<
149 ReadonlyArray<{
150 readonly id: ThreadId;
151 readonly projectId: ProjectId;
152 readonly branch: string;
153 readonly worktreePath: string;
154 readonly workspaceRoot: string;
155 readonly deletedAt: string;
156 }>,
157 ProjectionRepositoryError
158 >;
159
160 /**
161 * Search active thread navigation metadata, user messages, and canonical
162 * assistant outputs without hydrating thread detail snapshots.
163 */
164 readonly searchThreads: (
165 input: OrchestrationSearchThreadsInput,
166 ) => Effect.Effect<OrchestrationSearchThreadsResult, ProjectionRepositoryError>;
167
168 /**
169 * Read the latest projection snapshot sequence without hydrating read-model
170 * entities.
171 */
172 readonly getSnapshotSequence: () => Effect.Effect<
173 ProjectionSnapshotSequence,
174 ProjectionRepositoryError
175 >;
176
177 /**
178 * Read aggregate projection counts without hydrating the full read model.
179 */
180 readonly getCounts: () => Effect.Effect<ProjectionSnapshotCounts, ProjectionRepositoryError>;
181
182 /**
183 * Measure a persisted event range without decoding its payload bodies.
184 */
185 readonly getEventReplayStats: (input: {
186 readonly fromSequenceExclusive: number;
187 readonly toSequenceInclusive: number;
188 }) => Effect.Effect<ProjectionEventReplayStats, ProjectionRepositoryError>;
189
190 /**
191 * Read the active project for an exact workspace root match.
192 */
193 readonly getActiveProjectByWorkspaceRoot: (
194 workspaceRoot: string,
195 ) => Effect.Effect<Option.Option<OrchestrationProject>, ProjectionRepositoryError>;
196
197 /**
198 * Read a single active project shell row by id.
199 */
200 readonly getProjectShellById: (
201 projectId: ProjectId,
202 ) => Effect.Effect<Option.Option<OrchestrationProjectShell>, ProjectionRepositoryError>;
203
204 /** Read every active project shell without hydrating thread rows or enrichment. */
205 readonly getProjectShellsWithoutEnrichment: () => Effect.Effect<
206 ReadonlyArray<OrchestrationProjectShell>,
207 ProjectionRepositoryError
208 >;
209 readonly getProjectShells: (
210 projectIds?: ReadonlyArray<ProjectId>,
211 ) => Effect.Effect<ReadonlyArray<OrchestrationProjectShell>, ProjectionRepositoryError>;
212
213 /**
214 * Read the earliest active thread for a project.
215 */
216 readonly getFirstActiveThreadIdByProjectId: (
217 projectId: ProjectId,
218 ) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;
219
220 /** Read completed import sources without loading thread history. */
221 readonly getImportedAgentSessionSources: (projectId: ProjectId) => Effect.Effect<
222 ReadonlyArray<{
223 readonly threadId: ThreadId;
224 readonly source: AgentSessionImportSource;
225 }>,
226 ProjectionRepositoryError
227 >;
228
229 /**
230 * Read the checkpoint context needed to resolve a single thread diff.
231 */
232 readonly getThreadCheckpointContext: (
233 threadId: ThreadId,
234 ) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
235
236 /**
237 * Read only the narrow context needed to compute a full-thread diff from
238 * checkpoint 0 to a specific turn count.
239 */
240 readonly getFullThreadDiffContext: (
241 threadId: ThreadId,
242 toTurnCount: number,
243 ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
244
245 /**
246 * Read a single active thread shell row by id.
247 */
248 readonly getThreadShellById: (
249 threadId: ThreadId,
250 ) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
251
252 /** Read the active thread and session facts used to ingest provider events. */
253 readonly getThreadRuntimeContext: (
254 threadId: ThreadId,
255 ) => Effect.Effect<
256 Option.Option<
257 Pick<OrchestrationThreadShell, "id" | "projectId" | "title" | "titleState" | "session">
258 >,
259 ProjectionRepositoryError
260 >;
261
262 /**
263 * Read one requested message and whether another non-compaction user message exists.
264 * Newer queued messages count too, preserving first-turn title eligibility.
265 */
266 readonly getTurnStartMessage: (input: {
267 readonly threadId: ThreadId;
268 readonly messageId: MessageId;
269 }) => Effect.Effect<
270 Option.Option<{
271 readonly message: OrchestrationMessage;
272 readonly hasOtherUserMessages: boolean;
273 }>,
274 ProjectionRepositoryError
275 >;
276
277 /**
278 * Read a single active thread detail snapshot by id.
279 */
280 readonly getThreadDetailById: (
281 threadId: ThreadId,
282 query?: ProjectionThreadDetailQuery,
283 ) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
284
285 /**
286 * Read a single active thread detail together with the projection snapshot
287 * sequence in one consistent transaction, so the returned `snapshotSequence`
288 * exactly matches the state reflected in `thread` (no interleaving projector
289 * update between the two reads).
290 *
291 * When `window` is provided, the thread's messages, activities, proposed
292 * plans, and checkpoints are bounded to a page of recent turns and the
293 * response carries `page` metadata (see `OrchestrationThreadDetailWindow`).
294 * Without a window the full thread is returned with no `page` field —
295 * pagination is strictly opt-in.
296 */
297 readonly getThreadDetailSnapshot: (
298 threadId: ThreadId,
299 window?: OrchestrationThreadDetailWindow,
300 ) => Effect.Effect<Option.Option<OrchestrationThreadDetailSnapshot>, ProjectionRepositoryError>;
301}
302
303/**
304 * ProjectionSnapshotQuery - Service tag for projection snapshot queries.
305 */
306export class ProjectionSnapshotQuery extends Context.Service<
307 ProjectionSnapshotQuery,
308 ProjectionSnapshotQueryShape
309>()("t3/orchestration/Services/ProjectionSnapshotQuery") {}