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 the lightweight command snapshot used to bootstrap the in-memory
90 * orchestration engine without hydrating message/activity/checkpoint bodies.
91 */
92 readonly getCommandReadModel: () => Effect.Effect<
93 OrchestrationReadModel,
94 ProjectionRepositoryError
95 >;
96
97 /**
98 * Read the latest orchestration projection snapshot.
99 *
100 * Rehydrates from projection tables and derives snapshot sequence from
101 * projector cursor state.
102 */
103 readonly getSnapshot: () => Effect.Effect<OrchestrationReadModel, ProjectionRepositoryError>;
104
105 /**
106 * Read the latest orchestration shell snapshot.
107 *
108 * Returns only projects and thread shell summaries so clients can bootstrap
109 * lightweight navigation state without hydrating every thread body.
110 */
111 readonly getShellSnapshot: () => Effect.Effect<
112 OrchestrationShellSnapshot,
113 ProjectionRepositoryError
114 >;
115
116 /**
117 * Read the shell snapshot with null optional repository metadata.
118 *
119 * Transactional callers use this method and enrich the returned projects
120 * only after their transaction has closed.
121 */
122 readonly getShellSnapshotWithoutEnrichment: () => Effect.Effect<
123 OrchestrationShellSnapshot,
124 ProjectionRepositoryError
125 >;
126
127 /**
128 * Read archived thread shell summaries for the archive page.
129 *
130 * This query is separate from the main shell snapshot so archived threads
131 * are never bootstrapped into normal navigation state.
132 */
133 readonly getArchivedShellSnapshot: () => Effect.Effect<
134 OrchestrationShellSnapshot,
135 ProjectionRepositoryError
136 >;
137
138 /**
139 * Search active thread navigation metadata, user messages, and canonical
140 * assistant outputs without hydrating thread detail snapshots.
141 */
142 readonly searchThreads: (
143 input: OrchestrationSearchThreadsInput,
144 ) => Effect.Effect<OrchestrationSearchThreadsResult, ProjectionRepositoryError>;
145
146 /**
147 * Read the latest projection snapshot sequence without hydrating read-model
148 * entities.
149 */
150 readonly getSnapshotSequence: () => Effect.Effect<
151 ProjectionSnapshotSequence,
152 ProjectionRepositoryError
153 >;
154
155 /**
156 * Read aggregate projection counts without hydrating the full read model.
157 */
158 readonly getCounts: () => Effect.Effect<ProjectionSnapshotCounts, ProjectionRepositoryError>;
159
160 /**
161 * Measure a persisted event range without decoding its payload bodies.
162 */
163 readonly getEventReplayStats: (input: {
164 readonly fromSequenceExclusive: number;
165 readonly toSequenceInclusive: number;
166 }) => Effect.Effect<ProjectionEventReplayStats, ProjectionRepositoryError>;
167
168 /**
169 * Read the active project for an exact workspace root match.
170 */
171 readonly getActiveProjectByWorkspaceRoot: (
172 workspaceRoot: string,
173 ) => Effect.Effect<Option.Option<OrchestrationProject>, ProjectionRepositoryError>;
174
175 /**
176 * Read a single active project shell row by id.
177 */
178 readonly getProjectShellById: (
179 projectId: ProjectId,
180 ) => Effect.Effect<Option.Option<OrchestrationProjectShell>, ProjectionRepositoryError>;
181
182 /** Read every active project shell without hydrating thread rows or enrichment. */
183 readonly getProjectShellsWithoutEnrichment: () => Effect.Effect<
184 ReadonlyArray<OrchestrationProjectShell>,
185 ProjectionRepositoryError
186 >;
187
188 /**
189 * Read the earliest active thread for a project.
190 */
191 readonly getFirstActiveThreadIdByProjectId: (
192 projectId: ProjectId,
193 ) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;
194
195 /** Read completed import sources without loading thread history. */
196 readonly getImportedAgentSessionSources: (projectId: ProjectId) => Effect.Effect<
197 ReadonlyArray<{
198 readonly threadId: ThreadId;
199 readonly source: AgentSessionImportSource;
200 }>,
201 ProjectionRepositoryError
202 >;
203
204 /**
205 * Read the checkpoint context needed to resolve a single thread diff.
206 */
207 readonly getThreadCheckpointContext: (
208 threadId: ThreadId,
209 ) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
210
211 /**
212 * Read only the narrow context needed to compute a full-thread diff from
213 * checkpoint 0 to a specific turn count.
214 */
215 readonly getFullThreadDiffContext: (
216 threadId: ThreadId,
217 toTurnCount: number,
218 ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
219
220 /**
221 * Read a single active thread shell row by id.
222 */
223 readonly getThreadShellById: (
224 threadId: ThreadId,
225 ) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
226
227 /** Read the active thread and session facts used to ingest provider events. */
228 readonly getThreadRuntimeContext: (
229 threadId: ThreadId,
230 ) => Effect.Effect<
231 Option.Option<Pick<OrchestrationThreadShell, "id" | "title" | "session">>,
232 ProjectionRepositoryError
233 >;
234
235 /**
236 * Read one requested message and whether another non-compaction user message exists.
237 * Newer queued messages count too, preserving first-turn title eligibility.
238 */
239 readonly getTurnStartMessage: (input: {
240 readonly threadId: ThreadId;
241 readonly messageId: MessageId;
242 }) => Effect.Effect<
243 Option.Option<{
244 readonly message: OrchestrationMessage;
245 readonly hasOtherUserMessages: boolean;
246 }>,
247 ProjectionRepositoryError
248 >;
249
250 /**
251 * Read a single active thread detail snapshot by id.
252 */
253 readonly getThreadDetailById: (
254 threadId: ThreadId,
255 query?: ProjectionThreadDetailQuery,
256 ) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
257
258 /**
259 * Read a single active thread detail together with the projection snapshot
260 * sequence in one consistent transaction, so the returned `snapshotSequence`
261 * exactly matches the state reflected in `thread` (no interleaving projector
262 * update between the two reads).
263 *
264 * When `window` is provided, the thread's messages, activities, proposed
265 * plans, and checkpoints are bounded to a page of recent turns and the
266 * response carries `page` metadata (see `OrchestrationThreadDetailWindow`).
267 * Without a window the full thread is returned with no `page` field —
268 * pagination is strictly opt-in.
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") {}