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

ProjectionSnapshotQuery.ts

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