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