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

ProjectionSnapshotQuery.ts

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