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