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