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

ProjectionSnapshotQuery.ts

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