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

ProjectionSnapshotQuery.ts

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