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

ProjectionSnapshotQuery.ts

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