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 /**
170 * Read the earliest active thread for a project.
171 */
172 readonly getFirstActiveThreadIdByProjectId: (
173 projectId: ProjectId,
174 ) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;
175
176 /** Read completed import sources without loading thread history. */
177 readonly getImportedAgentSessionSources: (projectId: ProjectId) => Effect.Effect<
178 ReadonlyArray<{
179 readonly threadId: ThreadId;
180 readonly source: AgentSessionImportSource;
181 }>,
182 ProjectionRepositoryError
183 >;
184
185 /**
186 * Read the checkpoint context needed to resolve a single thread diff.
187 */
188 readonly getThreadCheckpointContext: (
189 threadId: ThreadId,
190 ) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
191
192 /**
193 * Read only the narrow context needed to compute a full-thread diff from
194 * checkpoint 0 to a specific turn count.
195 */
196 readonly getFullThreadDiffContext: (
197 threadId: ThreadId,
198 toTurnCount: number,
199 ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
200
201 /**
202 * Read a single active thread shell row by id.
203 */
204 readonly getThreadShellById: (
205 threadId: ThreadId,
206 ) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
207
208 /** Read the active thread and session facts used to ingest provider events. */
209 readonly getThreadRuntimeContext: (
210 threadId: ThreadId,
211 ) => Effect.Effect<
212 Option.Option<Pick<OrchestrationThreadShell, "id" | "projectId" | "title" | "session">>,
213 ProjectionRepositoryError
214 >;
215
216 /**
217 * Read one requested message and whether another non-compaction user message exists.
218 * Newer queued messages count too, preserving first-turn title eligibility.
219 */
220 readonly getTurnStartMessage: (input: {
221 readonly threadId: ThreadId;
222 readonly messageId: MessageId;
223 }) => Effect.Effect<
224 Option.Option<{
225 readonly message: OrchestrationMessage;
226 readonly hasOtherUserMessages: boolean;
227 }>,
228 ProjectionRepositoryError
229 >;
230
231 /**
232 * Read a single active thread detail snapshot by id.
233 */
234 readonly getThreadDetailById: (
235 threadId: ThreadId,
236 query?: ProjectionThreadDetailQuery,
237 ) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
238
239 /**
240 * Read a single active thread detail together with the projection snapshot
241 * sequence in one consistent transaction, so the returned `snapshotSequence`
242 * exactly matches the state reflected in `thread` (no interleaving projector
243 * update between the two reads).
244 *
245 * When `window` is provided, the thread's messages, activities, proposed
246 * plans, and checkpoints are bounded to a page of recent turns and the
247 * response carries `page` metadata (see `OrchestrationThreadDetailWindow`).
248 * Without a window the full thread is returned with no `page` field —
249 * pagination is strictly opt-in.
250 *
251 * Activity payloads are projected for clients as they are read in small
252 * sequential batches. Callers still apply the full snapshot projector for
253 * collection-level activity pruning.
254 */
255 readonly getThreadDetailSnapshot: (
256 threadId: ThreadId,
257 window?: OrchestrationThreadDetailWindow,
258 ) => Effect.Effect<Option.Option<OrchestrationThreadDetailSnapshot>, ProjectionRepositoryError>;
259}
260
261/**
262 * ProjectionSnapshotQuery - Service tag for projection snapshot queries.
263 */
264export class ProjectionSnapshotQuery extends Context.Service<
265 ProjectionSnapshotQuery,
266 ProjectionSnapshotQueryShape
267>()("t3/orchestration/Services/ProjectionSnapshotQuery") {}