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