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