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