Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectionSnapshotQuery.ts

295 lines9.6 KB

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;
40​
41export interface ProjectionSnapshotSequence {
42 readonly snapshotSequence: number;
44​
45export interface ProjectionEventReplayStats {
46 readonly eventCount: number;
47 readonly payloadBytes: number;
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>;
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;
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>;
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 /** Durable worktree ownership retained after thread deletion, including across restarts. */
135 readonly getDeletedWorktreeThreads: () => Effect.Effect<
136 ReadonlyArray<{
137 readonly id: ThreadId;
138 readonly projectId: ProjectId;
139 readonly branch: string;
140 readonly worktreePath: string;
141 readonly workspaceRoot: string;
142 readonly deletedAt: string;
143 }>,
144 ProjectionRepositoryError
145 >;
146​
147 /**
148 * Search active thread navigation metadata, user messages, and canonical
149 * assistant outputs without hydrating thread detail snapshots.
150 */
151 readonly searchThreads: (
152 input: OrchestrationSearchThreadsInput,
153 ) => Effect.Effect<OrchestrationSearchThreadsResult, ProjectionRepositoryError>;
154​
155 /**
156 * Read the latest projection snapshot sequence without hydrating read-model
157 * entities.
158 */
159 readonly getSnapshotSequence: () => Effect.Effect<
160 ProjectionSnapshotSequence,
161 ProjectionRepositoryError
162 >;
163​
164 /**
165 * Read aggregate projection counts without hydrating the full read model.
166 */
167 readonly getCounts: () => Effect.Effect<ProjectionSnapshotCounts, ProjectionRepositoryError>;
168​
169 /**
170 * Measure a persisted event range without decoding its payload bodies.
171 */
172 readonly getEventReplayStats: (input: {
173 readonly fromSequenceExclusive: number;
174 readonly toSequenceInclusive: number;
175 }) => Effect.Effect<ProjectionEventReplayStats, ProjectionRepositoryError>;
176​
177 /**
178 * Read the active project for an exact workspace root match.
179 */
180 readonly getActiveProjectByWorkspaceRoot: (
181 workspaceRoot: string,
182 ) => Effect.Effect<Option.Option<OrchestrationProject>, ProjectionRepositoryError>;
183​
184 /**
185 * Read a single active project shell row by id.
186 */
187 readonly getProjectShellById: (
188 projectId: ProjectId,
189 ) => Effect.Effect<Option.Option<OrchestrationProjectShell>, ProjectionRepositoryError>;
190​
191 readonly getProjectShells: (
192 projectIds?: ReadonlyArray<ProjectId>,
193 ) => Effect.Effect<ReadonlyArray<OrchestrationProjectShell>, ProjectionRepositoryError>;
194​
195 /**
196 * Read the earliest active thread for a project.
197 */
198 readonly getFirstActiveThreadIdByProjectId: (
199 projectId: ProjectId,
200 ) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;
201​
202 /** Read completed import sources without loading thread history. */
203 readonly getImportedAgentSessionSources: (projectId: ProjectId) => Effect.Effect<
204 ReadonlyArray<{
205 readonly threadId: ThreadId;
206 readonly source: AgentSessionImportSource;
207 }>,
208 ProjectionRepositoryError
209 >;
210​
211 /**
212 * Read the checkpoint context needed to resolve a single thread diff.
213 */
214 readonly getThreadCheckpointContext: (
215 threadId: ThreadId,
216 ) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
217​
218 /**
219 * Read only the narrow context needed to compute a full-thread diff from
220 * checkpoint 0 to a specific turn count.
221 */
222 readonly getFullThreadDiffContext: (
223 threadId: ThreadId,
224 toTurnCount: number,
225 ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
226​
227 /**
228 * Read a single active thread shell row by id.
229 */
230 readonly getThreadShellById: (
231 threadId: ThreadId,
232 ) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
233​
234 /** Read the active thread and session facts used to ingest provider events. */
235 readonly getThreadRuntimeContext: (
236 threadId: ThreadId,
237 ) => Effect.Effect<
238 Option.Option<
239 Pick<OrchestrationThreadShell, "id" | "projectId" | "title" | "titleState" | "session">
240 >,
241 ProjectionRepositoryError
242 >;
243​
244 /**
245 * Read one requested message and whether another non-compaction user message exists.
246 * Newer queued messages count too, preserving first-turn title eligibility.
247 */
248 readonly getTurnStartMessage: (input: {
249 readonly threadId: ThreadId;
250 readonly messageId: MessageId;
251 }) => Effect.Effect<
252 Option.Option<{
253 readonly message: OrchestrationMessage;
254 readonly hasOtherUserMessages: boolean;
255 }>,
256 ProjectionRepositoryError
257 >;
258​
259 /**
260 * Read a single active thread detail snapshot by id.
261 */
262 readonly getThreadDetailById: (
263 threadId: ThreadId,
264 query?: ProjectionThreadDetailQuery,
265 ) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
266​
267 /**
268 * Read a single active thread detail together with the projection snapshot
269 * sequence in one consistent transaction, so the returned `snapshotSequence`
270 * exactly matches the state reflected in `thread` (no interleaving projector
271 * update between the two reads).
272 *
273 * When `window` is provided, the thread's messages, activities, proposed
274 * plans, and checkpoints are bounded to a page of recent turns and the
275 * response carries `page` metadata (see `OrchestrationThreadDetailWindow`).
276 * Without a window the full thread is returned with no `page` field —
277 * pagination is strictly opt-in.
278 *
279 * Activity payloads are projected for clients as they are read in small
280 * sequential batches. Callers still apply the full snapshot projector for
281 * collection-level activity pruning.
282 */
283 readonly getThreadDetailSnapshot: (
284 threadId: ThreadId,
285 window?: OrchestrationThreadDetailWindow,
286 ) => Effect.Effect<Option.Option<OrchestrationThreadDetailSnapshot>, ProjectionRepositoryError>;
288​
289/**
290 * ProjectionSnapshotQuery - Service tag for projection snapshot queries.
291 */
292export class ProjectionSnapshotQuery extends Context.Service<
293 ProjectionSnapshotQuery,
294 ProjectionSnapshotQueryShape
295>()("t3/orchestration/Services/ProjectionSnapshotQuery") {}