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