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

ProjectionSnapshotQuery.ts

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