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 ProjectId,
28 ThreadId,
29} from "@t3tools/contracts";
30import * as Context from "effect/Context";
31import type * as Option from "effect/Option";
32import type * as Effect from "effect/Effect";
33
34import type { ProjectionRepositoryError } from "../../persistence/Errors.ts";
35
36export interface ProjectionSnapshotCounts {
37 readonly projectCount: number;
38 readonly threadCount: number;
39}
40
41export interface ProjectionSnapshotSequence {
42 readonly snapshotSequence: number;
43}
44
45export interface ProjectionEventReplayStats {
46 readonly eventCount: number;
47 readonly payloadBytes: number;
48}
49
50export interface ProjectionThreadCheckpointContext {
51 readonly threadId: ThreadId;
52 readonly projectId: ProjectId;
53 readonly workspaceRoot: string;
54 readonly worktreePath: string | null;
55 readonly checkpoints: ReadonlyArray<OrchestrationCheckpointSummary>;
56}
57
58export interface ProjectionFullThreadDiffContext {
59 readonly threadId: ThreadId;
60 readonly projectId: ProjectId;
61 readonly workspaceRoot: string;
62 readonly worktreePath: string | null;
63 readonly latestCheckpointTurnCount: number;
64 readonly toCheckpointRef: CheckpointRef | null;
65}
66
67export interface ProjectionThreadDetailQuery {
68 /**
69 * Limit activities before SQLite returns and decodes their payloads.
70 * Any explicit filter omits pinned-request reads. An empty list also skips
71 * the activity query. Omit this option to preserve the full detail response.
72 */
73 readonly activityKinds?: ReadonlyArray<string>;
74}
75
76/**
77 * ProjectionSnapshotQueryShape - Service API for read-model snapshots.
78 */
79export interface ProjectionSnapshotQueryShape {
80 /** Read the latest request or resolution without loading the thread history. */
81 readonly getUserInputActivity: (input: {
82 readonly threadId: ThreadId;
83 readonly requestId: ApprovalRequestId;
84 }) => Effect.Effect<Option.Option<OrchestrationThreadActivity>, ProjectionRepositoryError>;
85
86 /**
87 * Read every activity of one kind across active (not deleted, not archived)
88 * threads, without hydrating the threads. Used at startup to find state a
89 * crashed process left behind.
90 */
91 readonly listActivitiesByKind: (
92 kind: string,
93 ) => Effect.Effect<ReadonlyArray<OrchestrationThreadActivity>, ProjectionRepositoryError>;
94
95 /**
96 * Read the lightweight command snapshot used to bootstrap the in-memory
97 * orchestration engine without hydrating message/activity/checkpoint bodies.
98 */
99 readonly getCommandReadModel: () => Effect.Effect<
100 OrchestrationReadModel,
101 ProjectionRepositoryError
102 >;
103
104 /**
105 * Read the latest orchestration projection snapshot.
106 *
107 * Rehydrates from projection tables and derives snapshot sequence from
108 * projector cursor state.
109 */
110 readonly getSnapshot: () => Effect.Effect<OrchestrationReadModel, ProjectionRepositoryError>;
111
112 /**
113 * Read the latest orchestration shell snapshot.
114 *
115 * Returns only projects and thread shell summaries so clients can bootstrap
116 * lightweight navigation state without hydrating every thread body.
117 */
118 readonly getShellSnapshot: () => Effect.Effect<
119 OrchestrationShellSnapshot,
120 ProjectionRepositoryError
121 >;
122
123 /**
124 * Read archived thread shell summaries for the archive page.
125 *
126 * This query is separate from the main shell snapshot so archived threads
127 * are never bootstrapped into normal navigation state.
128 */
129 readonly getArchivedShellSnapshot: () => Effect.Effect<
130 OrchestrationShellSnapshot,
131 ProjectionRepositoryError
132 >;
133
134 /**
135 * Search active thread navigation metadata, user messages, and canonical
136 * assistant outputs without hydrating thread detail snapshots.
137 */
138 readonly searchThreads: (
139 input: OrchestrationSearchThreadsInput,
140 ) => Effect.Effect<OrchestrationSearchThreadsResult, ProjectionRepositoryError>;
141
142 /**
143 * Read the latest projection snapshot sequence without hydrating read-model
144 * entities.
145 */
146 readonly getSnapshotSequence: () => Effect.Effect<
147 ProjectionSnapshotSequence,
148 ProjectionRepositoryError
149 >;
150
151 /**
152 * Read aggregate projection counts without hydrating the full read model.
153 */
154 readonly getCounts: () => Effect.Effect<ProjectionSnapshotCounts, ProjectionRepositoryError>;
155
156 /**
157 * Measure a persisted event range without decoding its payload bodies.
158 */
159 readonly getEventReplayStats: (input: {
160 readonly fromSequenceExclusive: number;
161 readonly toSequenceInclusive: number;
162 }) => Effect.Effect<ProjectionEventReplayStats, ProjectionRepositoryError>;
163
164 /**
165 * Read the active project for an exact workspace root match.
166 */
167 readonly getActiveProjectByWorkspaceRoot: (
168 workspaceRoot: string,
169 ) => Effect.Effect<Option.Option<OrchestrationProject>, ProjectionRepositoryError>;
170
171 /**
172 * Read a single active project shell row by id.
173 */
174 readonly getProjectShellById: (
175 projectId: ProjectId,
176 ) => Effect.Effect<Option.Option<OrchestrationProjectShell>, ProjectionRepositoryError>;
177
178 readonly getProjectShells: (
179 projectIds?: ReadonlyArray<ProjectId>,
180 ) => Effect.Effect<ReadonlyArray<OrchestrationProjectShell>, ProjectionRepositoryError>;
181
182 /**
183 * Read the earliest active thread for a project.
184 */
185 readonly getFirstActiveThreadIdByProjectId: (
186 projectId: ProjectId,
187 ) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;
188
189 /** Read completed import sources without loading thread history. */
190 readonly getImportedAgentSessionSources: (projectId: ProjectId) => Effect.Effect<
191 ReadonlyArray<{
192 readonly threadId: ThreadId;
193 readonly source: AgentSessionImportSource;
194 }>,
195 ProjectionRepositoryError
196 >;
197
198 /**
199 * Read the checkpoint context needed to resolve a single thread diff.
200 */
201 readonly getThreadCheckpointContext: (
202 threadId: ThreadId,
203 ) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
204
205 /**
206 * Read only the narrow context needed to compute a full-thread diff from
207 * checkpoint 0 to a specific turn count.
208 */
209 readonly getFullThreadDiffContext: (
210 threadId: ThreadId,
211 toTurnCount: number,
212 ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
213
214 /**
215 * Read a single active thread shell row by id.
216 */
217 readonly getThreadShellById: (
218 threadId: ThreadId,
219 ) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
220
221 /** Read the active thread and session facts used to ingest provider events. */
222 readonly getThreadRuntimeContext: (
223 threadId: ThreadId,
224 ) => Effect.Effect<
225 Option.Option<
226 Pick<OrchestrationThreadShell, "id" | "projectId" | "title" | "titleState" | "session">
227 >,
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") {}