Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectionSnapshotQuery.ts

189 lines5.9 KB

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 OrchestrationSearchThreadsInput,
16 OrchestrationSearchThreadsResult,
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;
34​
35export interface ProjectionSnapshotSequence {
36 readonly snapshotSequence: number;
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>;
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;
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 * Search active thread navigation metadata, user messages, and canonical
101 * assistant outputs without hydrating thread detail snapshots.
102 */
103 readonly searchThreads: (
104 input: OrchestrationSearchThreadsInput,
105 ) => Effect.Effect<OrchestrationSearchThreadsResult, ProjectionRepositoryError>;
106​
107 /**
108 * Read the latest projection snapshot sequence without hydrating read-model
109 * entities.
110 */
111 readonly getSnapshotSequence: () => Effect.Effect<
112 ProjectionSnapshotSequence,
113 ProjectionRepositoryError
114 >;
115​
116 /**
117 * Read aggregate projection counts without hydrating the full read model.
118 */
119 readonly getCounts: () => Effect.Effect<ProjectionSnapshotCounts, ProjectionRepositoryError>;
120​
121 /**
122 * Read the active project for an exact workspace root match.
123 */
124 readonly getActiveProjectByWorkspaceRoot: (
125 workspaceRoot: string,
126 ) => Effect.Effect<Option.Option<OrchestrationProject>, ProjectionRepositoryError>;
127​
128 /**
129 * Read a single active project shell row by id.
130 */
131 readonly getProjectShellById: (
132 projectId: ProjectId,
133 ) => Effect.Effect<Option.Option<OrchestrationProjectShell>, ProjectionRepositoryError>;
134​
135 /**
136 * Read the earliest active thread for a project.
137 */
138 readonly getFirstActiveThreadIdByProjectId: (
139 projectId: ProjectId,
140 ) => Effect.Effect<Option.Option<ThreadId>, ProjectionRepositoryError>;
141​
142 /**
143 * Read the checkpoint context needed to resolve a single thread diff.
144 */
145 readonly getThreadCheckpointContext: (
146 threadId: ThreadId,
147 ) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
148​
149 /**
150 * Read only the narrow context needed to compute a full-thread diff from
151 * checkpoint 0 to a specific turn count.
152 */
153 readonly getFullThreadDiffContext: (
154 threadId: ThreadId,
155 toTurnCount: number,
156 ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
157​
158 /**
159 * Read a single active thread shell row by id.
160 */
161 readonly getThreadShellById: (
162 threadId: ThreadId,
163 ) => Effect.Effect<Option.Option<OrchestrationThreadShell>, ProjectionRepositoryError>;
164​
165 /**
166 * Read a single active thread detail snapshot by id.
167 */
168 readonly getThreadDetailById: (
169 threadId: ThreadId,
170 ) => Effect.Effect<Option.Option<OrchestrationThread>, ProjectionRepositoryError>;
171​
172 /**
173 * Read a single active thread detail together with the projection snapshot
174 * sequence in one consistent transaction, so the returned `snapshotSequence`
175 * exactly matches the state reflected in `thread` (no interleaving projector
176 * update between the two reads).
177 */
178 readonly getThreadDetailSnapshot: (
179 threadId: ThreadId,
180 ) => Effect.Effect<Option.Option<OrchestrationThreadDetailSnapshot>, ProjectionRepositoryError>;
182​
183/**
184 * ProjectionSnapshotQuery - Service tag for projection snapshot queries.
185 */
186export class ProjectionSnapshotQuery extends Context.Service<
187 ProjectionSnapshotQuery,
188 ProjectionSnapshotQueryShape
189>()("t3/orchestration/Services/ProjectionSnapshotQuery") {}