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