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