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

ProjectionSnapshotQuery.ts

315 lines10.5 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​
67/** The thread fields pull request sync reads, for a thread with at least one link. */
68export type ProjectionThreadPullRequests = Pick<
69 OrchestrationThreadShell,
70 "id" | "projectId" | "settledOverride" | "settledAt" | "pullRequests"
71>;
72​
73export interface ProjectionThreadDetailQuery {
74 /**
75 * Limit activities before SQLite returns and decodes their payloads.
76 * Any explicit filter omits pinned-request reads. An empty list also skips
77 * the activity query. Omit this option to preserve the full detail response.
78 */
79 readonly activityKinds?: ReadonlyArray<string>;
81​
82/**
83 * ProjectionSnapshotQueryShape - Service API for read-model snapshots.
84 */
85export interface ProjectionSnapshotQueryShape {
86 /** Read the latest request or resolution without loading the thread history. */
87 readonly getUserInputActivity: (input: {
88 readonly threadId: ThreadId;
89 readonly requestId: ApprovalRequestId;
90 }) => Effect.Effect<Option.Option<OrchestrationThreadActivity>, ProjectionRepositoryError>;
91​
92 /**
93 * Read every activity of one kind across active (not deleted, not archived)
94 * threads, without hydrating the threads. Used at startup to find state a
95 * crashed process left behind.
96 */
97 readonly listActivitiesByKind: (
98 kind: string,
99 ) => Effect.Effect<ReadonlyArray<OrchestrationThreadActivity>, ProjectionRepositoryError>;
100​
101 /**
102 * Read the lightweight command snapshot used to bootstrap the in-memory
103 * orchestration engine without hydrating message/activity/checkpoint bodies.
104 */
105 readonly getCommandReadModel: () => Effect.Effect<
106 OrchestrationReadModel,
107 ProjectionRepositoryError
108 >;
109​
110 /**
111 * Read the latest orchestration projection snapshot.
112 *
113 * Rehydrates from projection tables and derives snapshot sequence from
114 * projector cursor state.
115 */
116 readonly getSnapshot: () => Effect.Effect<OrchestrationReadModel, ProjectionRepositoryError>;
117​
118 /**
119 * Read the latest orchestration shell snapshot.
120 *
121 * Returns only projects and thread shell summaries so clients can bootstrap
122 * lightweight navigation state without hydrating every thread body.
123 *
124 * `unsettledOnly` is for background sweeps, not clients. It skips settled
125 * threads and their sessions, PR links, and turns, and its `updatedAt`
126 * ignores those rows. It still resolves every project, which keeps
127 * repository identities cached for client connects.
128 */
129 readonly getShellSnapshot: (options?: {
130 readonly unsettledOnly?: boolean;
131 }) => Effect.Effect<OrchestrationShellSnapshot, ProjectionRepositoryError>;
132​
133 /**
134 * Read archived thread shell summaries for the archive page.
135 *
136 * This query is separate from the main shell snapshot so archived threads
137 * are never bootstrapped into normal navigation state.
138 */
139 readonly getArchivedShellSnapshot: () => Effect.Effect<
140 OrchestrationShellSnapshot,
141 ProjectionRepositoryError
142 >;
143​
144 /**
145 * Read active (not deleted, not archived) threads that have at least one pull
146 * request link, in shell snapshot order. Skips repository identity, so no
147 * legacy `linkedPullRequest` is derived.
148 */
149 readonly listThreadsWithPullRequests: () => Effect.Effect<
150 ReadonlyArray<ProjectionThreadPullRequests>,
151 ProjectionRepositoryError
152 >;
153​
154 /** Durable worktree ownership retained after thread deletion, including across restarts. */
155 readonly getDeletedWorktreeThreads: () => Effect.Effect<
156 ReadonlyArray<{
157 readonly id: ThreadId;
158 readonly projectId: ProjectId;
159 readonly branch: string;
160 readonly worktreePath: string;
161 readonly workspaceRoot: string;
162 readonly deletedAt: string;
163 }>,
164 ProjectionRepositoryError
165 >;
166​
167 /**
168 * Search active thread navigation metadata, user messages, and canonical
169 * assistant outputs without hydrating thread detail snapshots.
170 */
171 readonly searchThreads: (
172 input: OrchestrationSearchThreadsInput,
173 ) => Effect.Effect<OrchestrationSearchThreadsResult, ProjectionRepositoryError>;
174​
175 /**
176 * Read the latest projection snapshot sequence without hydrating read-model
177 * entities.
178 */
179 readonly getSnapshotSequence: () => Effect.Effect<
180 ProjectionSnapshotSequence,
181 ProjectionRepositoryError
182 >;
183​
184 /**
185 * Read aggregate projection counts without hydrating the full read model.
186 */
187 readonly getCounts: () => Effect.Effect<ProjectionSnapshotCounts, ProjectionRepositoryError>;
188​
189 /**
190 * Measure a persisted event range without decoding its payload bodies.
191 */
192 readonly getEventReplayStats: (input: {
193 readonly fromSequenceExclusive: number;
194 readonly toSequenceInclusive: number;
195 }) => Effect.Effect<ProjectionEventReplayStats, ProjectionRepositoryError>;
196​
197 /**
198 * Read the active project for an exact workspace root match.
199 */
200 readonly getActiveProjectByWorkspaceRoot: (
201 workspaceRoot: string,
202 ) => Effect.Effect<Option.Option<OrchestrationProject>, ProjectionRepositoryError>;
203​
204 /**
205 * Read a single active project shell row by id.
206 */
207 readonly getProjectShellById: (
208 projectId: ProjectId,
209 ) => Effect.Effect<Option.Option<OrchestrationProjectShell>, ProjectionRepositoryError>;
210​
211 readonly getProjectShells: (
212 projectIds?: ReadonlyArray<ProjectId>,
213 ) => Effect.Effect<ReadonlyArray<OrchestrationProjectShell>, ProjectionRepositoryError>;
214​
215 /**
216 * Read the earliest active thread for a project.
217 */
218 readonly getFirstActiveThreadIdByProjectId: (
219 projectId: ProjectId,
220 ) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;
221​
222 /** Read completed import sources without loading thread history. */
223 readonly getImportedAgentSessionSources: (projectId: ProjectId) => Effect.Effect<
224 ReadonlyArray<{
225 readonly threadId: ThreadId;
226 readonly source: AgentSessionImportSource;
227 }>,
228 ProjectionRepositoryError
229 >;
230​
231 /**
232 * Read the checkpoint context needed to resolve a single thread diff.
233 */
234 readonly getThreadCheckpointContext: (
235 threadId: ThreadId,
236 ) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
237​
238 /**
239 * Read only the narrow context needed to compute a full-thread diff from
240 * checkpoint 0 to a specific turn count.
241 */
242 readonly getFullThreadDiffContext: (
243 threadId: ThreadId,
244 toTurnCount: number,
245 ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
246​
247 /**
248 * Read a single active thread shell row by id.
249 */
250 readonly getThreadShellById: (
251 threadId: ThreadId,
252 ) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
253​
254 /** Read the active thread and session facts used to ingest provider events. */
255 readonly getThreadRuntimeContext: (
256 threadId: ThreadId,
257 ) => Effect.Effect<
258 Option.Option<
259 Pick<OrchestrationThreadShell, "id" | "projectId" | "title" | "titleState" | "session">
260 >,
261 ProjectionRepositoryError
262 >;
263​
264 /**
265 * Read one requested message and whether another non-compaction user message exists.
266 * Newer queued messages count too, preserving first-turn title eligibility.
267 */
268 readonly getTurnStartMessage: (input: {
269 readonly threadId: ThreadId;
270 readonly messageId: MessageId;
271 }) => Effect.Effect<
272 Option.Option<{
273 readonly message: OrchestrationMessage;
274 readonly hasOtherUserMessages: boolean;
275 }>,
276 ProjectionRepositoryError
277 >;
278​
279 /**
280 * Read a single active thread detail snapshot by id.
281 */
282 readonly getThreadDetailById: (
283 threadId: ThreadId,
284 query?: ProjectionThreadDetailQuery,
285 ) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
286​
287 /**
288 * Read a single active thread detail together with the projection snapshot
289 * sequence in one consistent transaction, so the returned `snapshotSequence`
290 * exactly matches the state reflected in `thread` (no interleaving projector
291 * update between the two reads).
292 *
293 * When `window` is provided, the thread's messages, activities, proposed
294 * plans, and checkpoints are bounded to a page of recent turns and the
295 * response carries `page` metadata (see `OrchestrationThreadDetailWindow`).
296 * Without a window the full thread is returned with no `page` field —
297 * pagination is strictly opt-in.
298 *
299 * Activity payloads are projected for clients as they are read in small
300 * sequential batches. Callers still apply the full snapshot projector for
301 * collection-level activity pruning.
302 */
303 readonly getThreadDetailSnapshot: (
304 threadId: ThreadId,
305 window?: OrchestrationThreadDetailWindow,
306 ) => Effect.Effect<Option.Option<OrchestrationThreadDetailSnapshot>, ProjectionRepositoryError>;
308​
309/**
310 * ProjectionSnapshotQuery - Service tag for projection snapshot queries.
311 */
312export class ProjectionSnapshotQuery extends Context.Service<
313 ProjectionSnapshotQuery,
314 ProjectionSnapshotQueryShape
315>()("t3/orchestration/Services/ProjectionSnapshotQuery") {}