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

ProjectionSnapshotQuery.ts

197 lines6.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 ProjectionThreadCheckpointContext {
41 readonly threadId: ThreadId;
42 readonly projectId: ProjectId;
43 readonly workspaceRoot: string;
44 readonly worktreePath: string | null;
45 readonly checkpoints: ReadonlyArray<OrchestrationCheckpointSummary>;
47​
48export interface ProjectionFullThreadDiffContext {
49 readonly threadId: ThreadId;
50 readonly projectId: ProjectId;
51 readonly workspaceRoot: string;
52 readonly worktreePath: string | null;
53 readonly latestCheckpointTurnCount: number;
54 readonly toCheckpointRef: CheckpointRef | null;
56​
57/**
58 * ProjectionSnapshotQueryShape - Service API for read-model snapshots.
59 */
60export interface ProjectionSnapshotQueryShape {
61 /**
62 * Read the lightweight command snapshot used to bootstrap the in-memory
63 * orchestration engine without hydrating message/activity/checkpoint bodies.
64 */
65 readonly getCommandReadModel: () => Effect.Effect<
66 OrchestrationReadModel,
67 ProjectionRepositoryError
68 >;
69​
70 /**
71 * Read the latest orchestration projection snapshot.
72 *
73 * Rehydrates from projection tables and derives snapshot sequence from
74 * projector cursor state.
75 */
76 readonly getSnapshot: () => Effect.Effect<OrchestrationReadModel, ProjectionRepositoryError>;
77​
78 /**
79 * Read the latest orchestration shell snapshot.
80 *
81 * Returns only projects and thread shell summaries so clients can bootstrap
82 * lightweight navigation state without hydrating every thread body.
83 */
84 readonly getShellSnapshot: () => Effect.Effect<
85 OrchestrationShellSnapshot,
86 ProjectionRepositoryError
87 >;
88​
89 /**
90 * Read archived thread shell summaries for the archive page.
91 *
92 * This query is separate from the main shell snapshot so archived threads
93 * are never bootstrapped into normal navigation state.
94 */
95 readonly getArchivedShellSnapshot: () => Effect.Effect<
96 OrchestrationShellSnapshot,
97 ProjectionRepositoryError
98 >;
99​
100 /**
101 * Search active thread navigation metadata, user messages, and canonical
102 * assistant outputs without hydrating thread detail snapshots.
103 */
104 readonly searchThreads: (
105 input: OrchestrationSearchThreadsInput,
106 ) => Effect.Effect<OrchestrationSearchThreadsResult, ProjectionRepositoryError>;
107​
108 /**
109 * Read the latest projection snapshot sequence without hydrating read-model
110 * entities.
111 */
112 readonly getSnapshotSequence: () => Effect.Effect<
113 ProjectionSnapshotSequence,
114 ProjectionRepositoryError
115 >;
116​
117 /**
118 * Read aggregate projection counts without hydrating the full read model.
119 */
120 readonly getCounts: () => Effect.Effect<ProjectionSnapshotCounts, ProjectionRepositoryError>;
121​
122 /**
123 * Read the active project for an exact workspace root match.
124 */
125 readonly getActiveProjectByWorkspaceRoot: (
126 workspaceRoot: string,
127 ) => Effect.Effect<Option.Option<OrchestrationProject>, ProjectionRepositoryError>;
128​
129 /**
130 * Read a single active project shell row by id.
131 */
132 readonly getProjectShellById: (
133 projectId: ProjectId,
134 ) => Effect.Effect<Option.Option<OrchestrationProjectShell>, ProjectionRepositoryError>;
135​
136 /**
137 * Read the earliest active thread for a project.
138 */
139 readonly getFirstActiveThreadIdByProjectId: (
140 projectId: ProjectId,
141 ) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;
142​
143 /**
144 * Read the checkpoint context needed to resolve a single thread diff.
145 */
146 readonly getThreadCheckpointContext: (
147 threadId: ThreadId,
148 ) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
149​
150 /**
151 * Read only the narrow context needed to compute a full-thread diff from
152 * checkpoint 0 to a specific turn count.
153 */
154 readonly getFullThreadDiffContext: (
155 threadId: ThreadId,
156 toTurnCount: number,
157 ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
158​
159 /**
160 * Read a single active thread shell row by id.
161 */
162 readonly getThreadShellById: (
163 threadId: ThreadId,
164 ) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
165​
166 /**
167 * Read a single active thread detail snapshot by id.
168 */
169 readonly getThreadDetailById: (
170 threadId: ThreadId,
171 ) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
172​
173 /**
174 * Read a single active thread detail together with the projection snapshot
175 * sequence in one consistent transaction, so the returned `snapshotSequence`
176 * exactly matches the state reflected in `thread` (no interleaving projector
177 * update between the two reads).
178 *
179 * When `window` is provided, the thread's messages, activities, proposed
180 * plans, and checkpoints are bounded to a page of recent turns and the
181 * response carries `page` metadata (see `OrchestrationThreadDetailWindow`).
182 * Without a window the full thread is returned with no `page` field —
183 * pagination is strictly opt-in.
184 */
185 readonly getThreadDetailSnapshot: (
186 threadId: ThreadId,
187 window?: OrchestrationThreadDetailWindow,
188 ) => Effect.Effect<Option.Option<OrchestrationThreadDetailSnapshot>, ProjectionRepositoryError>;
190​
191/**
192 * ProjectionSnapshotQuery - Service tag for projection snapshot queries.
193 */
194export class ProjectionSnapshotQuery extends Context.Service<
195 ProjectionSnapshotQuery,
196 ProjectionSnapshotQueryShape
197>()("t3/orchestration/Services/ProjectionSnapshotQuery") {}