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