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 /**
148 * Search active thread navigation metadata, user messages, and canonical
149 * assistant outputs without hydrating thread detail snapshots.
150 */
151 readonly searchThreads: (
152 input: OrchestrationSearchThreadsInput,
153 ) => Effect.Effect<OrchestrationSearchThreadsResult, ProjectionRepositoryError>;
154
155 /**
156 * Read the latest projection snapshot sequence without hydrating read-model
157 * entities.
158 */
159 readonly getSnapshotSequence: () => Effect.Effect<
160 ProjectionSnapshotSequence,
161 ProjectionRepositoryError
162 >;
163
164 /**
165 * Read aggregate projection counts without hydrating the full read model.
166 */
167 readonly getCounts: () => Effect.Effect<ProjectionSnapshotCounts, ProjectionRepositoryError>;
168
169 /**
170 * Measure a persisted event range without decoding its payload bodies.
171 */
172 readonly getEventReplayStats: (input: {
173 readonly fromSequenceExclusive: number;
174 readonly toSequenceInclusive: number;
175 }) => Effect.Effect<ProjectionEventReplayStats, ProjectionRepositoryError>;
176
177 /**
178 * Read the active project for an exact workspace root match.
179 */
180 readonly getActiveProjectByWorkspaceRoot: (
181 workspaceRoot: string,
182 ) => Effect.Effect<Option.Option<OrchestrationProject>, ProjectionRepositoryError>;
183
184 /**
185 * Read a single active project shell row by id.
186 */
187 readonly getProjectShellById: (
188 projectId: ProjectId,
189 ) => Effect.Effect<Option.Option<OrchestrationProjectShell>, ProjectionRepositoryError>;
190
191 /** Read every active project shell without hydrating thread rows or enrichment. */
192 readonly getProjectShellsWithoutEnrichment: () => Effect.Effect<
193 ReadonlyArray<OrchestrationProjectShell>,
194 ProjectionRepositoryError
195 >;
196 readonly getProjectShells: (
197 projectIds?: ReadonlyArray<ProjectId>,
198 ) => Effect.Effect<ReadonlyArray<OrchestrationProjectShell>, ProjectionRepositoryError>;
199
200 /**
201 * Read the earliest active thread for a project.
202 */
203 readonly getFirstActiveThreadIdByProjectId: (
204 projectId: ProjectId,
205 ) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;
206
207 /** Read completed import sources without loading thread history. */
208 readonly getImportedAgentSessionSources: (projectId: ProjectId) => Effect.Effect<
209 ReadonlyArray<{
210 readonly threadId: ThreadId;
211 readonly source: AgentSessionImportSource;
212 }>,
213 ProjectionRepositoryError
214 >;
215
216 /**
217 * Read the checkpoint context needed to resolve a single thread diff.
218 */
219 readonly getThreadCheckpointContext: (
220 threadId: ThreadId,
221 ) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
222
223 /**
224 * Read only the narrow context needed to compute a full-thread diff from
225 * checkpoint 0 to a specific turn count.
226 */
227 readonly getFullThreadDiffContext: (
228 threadId: ThreadId,
229 toTurnCount: number,
230 ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
231
232 /**
233 * Read a single active thread shell row by id.
234 */
235 readonly getThreadShellById: (
236 threadId: ThreadId,
237 ) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
238
239 /** Read the active thread and session facts used to ingest provider events. */
240 readonly getThreadRuntimeContext: (
241 threadId: ThreadId,
242 ) => Effect.Effect<
243 Option.Option<
244 Pick<OrchestrationThreadShell, "id" | "projectId" | "title" | "titleState" | "session">
245 >,
246 ProjectionRepositoryError
247 >;
248
249 /**
250 * Read one requested message and whether another non-compaction user message exists.
251 * Newer queued messages count too, preserving first-turn title eligibility.
252 */
253 readonly getTurnStartMessage: (input: {
254 readonly threadId: ThreadId;
255 readonly messageId: MessageId;
256 }) => Effect.Effect<
257 Option.Option<{
258 readonly message: OrchestrationMessage;
259 readonly hasOtherUserMessages: boolean;
260 }>,
261 ProjectionRepositoryError
262 >;
263
264 /**
265 * Read a single active thread detail snapshot by id.
266 */
267 readonly getThreadDetailById: (
268 threadId: ThreadId,
269 query?: ProjectionThreadDetailQuery,
270 ) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
271
272 /**
273 * Read a single active thread detail together with the projection snapshot
274 * sequence in one consistent transaction, so the returned `snapshotSequence`
275 * exactly matches the state reflected in `thread` (no interleaving projector
276 * update between the two reads).
277 *
278 * When `window` is provided, the thread's messages, activities, proposed
279 * plans, and checkpoints are bounded to a page of recent turns and the
280 * response carries `page` metadata (see `OrchestrationThreadDetailWindow`).
281 * Without a window the full thread is returned with no `page` field —
282 * pagination is strictly opt-in.
283 */
284 readonly getThreadDetailSnapshot: (
285 threadId: ThreadId,
286 window?: OrchestrationThreadDetailWindow,
287 ) => Effect.Effect<Option.Option<OrchestrationThreadDetailSnapshot>, ProjectionRepositoryError>;
288}
289
290/**
291 * ProjectionSnapshotQuery - Service tag for projection snapshot queries.
292 */
293export class ProjectionSnapshotQuery extends Context.Service<
294 ProjectionSnapshotQuery,
295 ProjectionSnapshotQueryShape
296>()("t3/orchestration/Services/ProjectionSnapshotQuery") {}