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

ProjectionTurns.ts

270 lines10.2 KB

1/**
2 * ProjectionTurnRepository - Projection repository interface for unified turn state.
3 *
4 * Owns persistence operations for pending starts, running/completed turn lifecycle,
5 * and checkpoint metadata in a single projection table.
6 *
7 * @module ProjectionTurnRepository
8 */
9import {
10 CheckpointRef,
11 IsoDateTime,
12 MessageId,
13 NonNegativeInt,
14 OrchestrationProposedPlanId,
15 OrchestrationCheckpointFile,
16 OrchestrationCheckpointStatus,
17 ThreadId,
18 TurnId,
19} from "@t3tools/contracts";
20import * as Option from "effect/Option";
21import * as Schema from "effect/Schema";
22import * as Context from "effect/Context";
23import type * as Effect from "effect/Effect";
24​
25import type { ProjectionRepositoryError } from "../Errors.ts";
26​
27export const ProjectionTurnState = Schema.Literals([
28 "pending",
29 "submitted",
30 "running",
31 "interrupted",
32 "completed",
33 "error",
34]);
35export type ProjectionTurnState = typeof ProjectionTurnState.Type;
36​
37export const ProjectionTurn = Schema.Struct({
38 threadId: ThreadId,
39 turnId: Schema.NullOr(TurnId),
40 submittedTurnId: Schema.NullOr(TurnId),
41 pendingMessageId: Schema.NullOr(MessageId),
42 sourceProposedPlanThreadId: Schema.NullOr(ThreadId),
43 sourceProposedPlanId: Schema.NullOr(OrchestrationProposedPlanId),
44 assistantMessageId: Schema.NullOr(MessageId),
45 state: ProjectionTurnState,
46 requestedAt: IsoDateTime,
47 startedAt: Schema.NullOr(IsoDateTime),
48 completedAt: Schema.NullOr(IsoDateTime),
49 checkpointTurnCount: Schema.NullOr(NonNegativeInt),
50 checkpointRef: Schema.NullOr(CheckpointRef),
51 checkpointStatus: Schema.NullOr(OrchestrationCheckpointStatus),
52 checkpointFiles: Schema.Array(OrchestrationCheckpointFile),
53});
54export type ProjectionTurn = typeof ProjectionTurn.Type;
55​
56export const ProjectionTurnById = Schema.Struct({
57 threadId: ThreadId,
58 turnId: TurnId,
59 pendingMessageId: Schema.NullOr(MessageId),
60 sourceProposedPlanThreadId: Schema.NullOr(ThreadId),
61 sourceProposedPlanId: Schema.NullOr(OrchestrationProposedPlanId),
62 assistantMessageId: Schema.NullOr(MessageId),
63 state: ProjectionTurnState,
64 requestedAt: IsoDateTime,
65 startedAt: Schema.NullOr(IsoDateTime),
66 completedAt: Schema.NullOr(IsoDateTime),
67 checkpointTurnCount: Schema.NullOr(NonNegativeInt),
68 checkpointRef: Schema.NullOr(CheckpointRef),
69 checkpointStatus: Schema.NullOr(OrchestrationCheckpointStatus),
70 checkpointFiles: Schema.Array(OrchestrationCheckpointFile),
71});
72export type ProjectionTurnById = typeof ProjectionTurnById.Type;
73​
74export const ProjectionPendingTurnStart = Schema.Struct({
75 threadId: ThreadId,
76 messageId: MessageId,
77 sourceProposedPlanThreadId: Schema.NullOr(ThreadId),
78 sourceProposedPlanId: Schema.NullOr(OrchestrationProposedPlanId),
79 requestedAt: IsoDateTime,
80});
81export type ProjectionPendingTurnStart = typeof ProjectionPendingTurnStart.Type;
82​
83export const ProjectionSubmittedTurnStart = Schema.Struct({
84 ...ProjectionPendingTurnStart.fields,
85 turnId: TurnId,
86});
87export type ProjectionSubmittedTurnStart = typeof ProjectionSubmittedTurnStart.Type;
88​
89export const ListProjectionTurnsByThreadInput = Schema.Struct({
90 threadId: ThreadId,
91});
92export type ListProjectionTurnsByThreadInput = typeof ListProjectionTurnsByThreadInput.Type;
93​
94export const GetProjectionTurnByTurnIdInput = Schema.Struct({
95 threadId: ThreadId,
96 turnId: TurnId,
97});
98export type GetProjectionTurnByTurnIdInput = typeof GetProjectionTurnByTurnIdInput.Type;
99​
100export const GetProjectionPendingTurnStartInput = Schema.Struct({
101 threadId: ThreadId,
102});
103export type GetProjectionPendingTurnStartInput = typeof GetProjectionPendingTurnStartInput.Type;
104​
105export const GetProjectionAdoptableTurnStartInput = Schema.Struct({
106 threadId: ThreadId,
107 turnId: TurnId,
108});
109export type GetProjectionAdoptableTurnStartInput = typeof GetProjectionAdoptableTurnStartInput.Type;
110​
111export const GetProjectionSubmittedTurnStartInput = GetProjectionAdoptableTurnStartInput;
112export type GetProjectionSubmittedTurnStartInput = typeof GetProjectionSubmittedTurnStartInput.Type;
113​
114export const DeleteProjectionPendingTurnStartInput = Schema.Struct({
115 threadId: ThreadId,
116 messageId: MessageId,
117});
118export type DeleteProjectionPendingTurnStartInput =
119 typeof DeleteProjectionPendingTurnStartInput.Type;
120​
121export const AcknowledgeProjectionPendingTurnStartInput = Schema.Struct({
122 threadId: ThreadId,
123 messageId: MessageId,
124 turnId: TurnId,
125});
126export type AcknowledgeProjectionPendingTurnStartInput =
127 typeof AcknowledgeProjectionPendingTurnStartInput.Type;
128​
129export const DeleteProjectionSubmittedTurnStartsInput = Schema.Struct({
130 threadId: ThreadId,
131 turnId: TurnId,
132});
133export type DeleteProjectionSubmittedTurnStartsInput =
134 typeof DeleteProjectionSubmittedTurnStartsInput.Type;
135​
136export const DeleteProjectionTurnsByThreadInput = Schema.Struct({
137 threadId: ThreadId,
138});
139export type DeleteProjectionTurnsByThreadInput = typeof DeleteProjectionTurnsByThreadInput.Type;
140​
141export const DeleteProjectionSubmittedTurnStartsByThreadInput = DeleteProjectionTurnsByThreadInput;
142export type DeleteProjectionSubmittedTurnStartsByThreadInput =
143 typeof DeleteProjectionSubmittedTurnStartsByThreadInput.Type;
144​
145export const DeleteProjectionTurnsAfterCheckpointInput = Schema.Struct({
146 threadId: ThreadId,
147 turnCount: NonNegativeInt,
148});
149export type DeleteProjectionTurnsAfterCheckpointInput =
150 typeof DeleteProjectionTurnsAfterCheckpointInput.Type;
151​
152export const ClearCheckpointTurnConflictInput = Schema.Struct({
153 threadId: ThreadId,
154 turnId: TurnId,
155 checkpointTurnCount: NonNegativeInt,
156});
157export type ClearCheckpointTurnConflictInput = typeof ClearCheckpointTurnConflictInput.Type;
158​
159export interface ProjectionTurnRepositoryShape {
160 /**
161 * Inserts or updates the canonical row for a concrete `{threadId, turnId}` turn lifecycle state.
162 */
163 readonly upsertByTurnId: (
164 row: ProjectionTurnById,
165 ) => Effect.Effect<void, ProjectionRepositoryError>;
166​
167 /**
168 * Inserts one pending-start placeholder. Multiple accepted starts may wait
169 * behind serialized checkpoint work and must remain independently durable.
170 */
171 readonly insertPendingTurnStart: (
172 row: ProjectionPendingTurnStart,
173 ) => Effect.Effect<void, ProjectionRepositoryError>;
174​
175 /** Restores an accepted placeholder without making it eligible for startup resubmission. */
176 readonly insertSubmittedTurnStart: (
177 row: ProjectionSubmittedTurnStart,
178 ) => Effect.Effect<void, ProjectionRepositoryError>;
179​
180 /** Marks one correlated start as accepted by the provider so startup does not replay it. */
181 readonly markPendingTurnStartSubmitted: (
182 input: AcknowledgeProjectionPendingTurnStartInput,
183 ) => Effect.Effect<void, ProjectionRepositoryError>;
184​
185 /** Clears accepted steering placeholders correlated to a provider turn after it settles. */
186 readonly deleteSubmittedTurnStartsByTurnId: (
187 input: DeleteProjectionSubmittedTurnStartsInput,
188 ) => Effect.Effect<void, ProjectionRepositoryError>;
189​
190 /**
191 * Clears every accepted-start placeholder on a thread once the session
192 * settles, including submissions whose turn never surfaced as running.
193 */
194 readonly deleteSubmittedTurnStartsByThreadId: (
195 input: DeleteProjectionSubmittedTurnStartsByThreadInput,
196 ) => Effect.Effect<void, ProjectionRepositoryError>;
197​
198 /**
199 * Returns the oldest start that has not yet been accepted by a provider.
200 * Startup reconciliation uses this to exclude acknowledged submissions.
201 */
202 readonly getPendingTurnStartByThreadId: (
203 input: GetProjectionPendingTurnStartInput,
204 ) => Effect.Effect<Option.Option<ProjectionPendingTurnStart>, ProjectionRepositoryError>;
205​
206 /**
207 * Returns the oldest start placeholder the provider has not yet consumed,
208 * pending or submitted. Lifecycle presence checks use this so a queued
209 * second start still counts while it cannot be correlated yet.
210 */
211 readonly getUnresolvedTurnStartByThreadId: (
212 input: GetProjectionPendingTurnStartInput,
213 ) => Effect.Effect<Option.Option<ProjectionPendingTurnStart>, ProjectionRepositoryError>;
214​
215 /** Returns the oldest pending start or acknowledgement belonging to this provider turn. */
216 readonly getAdoptableTurnStartByThreadId: (
217 input: GetProjectionAdoptableTurnStartInput,
218 ) => Effect.Effect<Option.Option<ProjectionPendingTurnStart>, ProjectionRepositoryError>;
219​
220 /** Returns only the accepted start correlated to this provider turn. */
221 readonly getSubmittedTurnStartByTurnId: (
222 input: GetProjectionSubmittedTurnStartInput,
223 ) => Effect.Effect<Option.Option<ProjectionPendingTurnStart>, ProjectionRepositoryError>;
224​
225 /**
226 * Deletes one correlated pending-start placeholder and leaves other queued
227 * starts and concrete turn rows untouched.
228 */
229 readonly deletePendingTurnStart: (
230 input: DeleteProjectionPendingTurnStartInput,
231 ) => Effect.Effect<void, ProjectionRepositoryError>;
232​
233 /** Deletes concrete turns beyond a revert boundary without disturbing queued start order. */
234 readonly deleteTurnsAfterCheckpoint: (
235 input: DeleteProjectionTurnsAfterCheckpointInput,
236 ) => Effect.Effect<void, ProjectionRepositoryError>;
237​
238 /**
239 * Lists all projection rows for a thread, including pending placeholders, with checkpoint rows ordered before non-checkpoint rows.
240 */
241 readonly listByThreadId: (
242 input: ListProjectionTurnsByThreadInput,
243 ) => Effect.Effect<ReadonlyArray<ProjectionTurn>, ProjectionRepositoryError>;
244​
245 /**
246 * Looks up a concrete turn row by `{threadId, turnId}` and never returns pending placeholder rows.
247 */
248 readonly getByTurnId: (
249 input: GetProjectionTurnByTurnIdInput,
250 ) => Effect.Effect<Option.Option<ProjectionTurnById>, ProjectionRepositoryError>;
251​
252 /**
253 * Clears checkpoint fields on conflicting rows that reuse the same checkpoint turn count in a thread, excluding the provided turn.
254 */
255 readonly clearCheckpointTurnConflict: (
256 input: ClearCheckpointTurnConflictInput,
257 ) => Effect.Effect<void, ProjectionRepositoryError>;
258​
259 /**
260 * Hard-deletes all projection rows for a thread, including pending-start placeholders and checkpoint metadata rows.
261 */
262 readonly deleteByThreadId: (
263 input: DeleteProjectionTurnsByThreadInput,
264 ) => Effect.Effect<void, ProjectionRepositoryError>;
266​
267export class ProjectionTurnRepository extends Context.Service<
268 ProjectionTurnRepository,
269 ProjectionTurnRepositoryShape
270>()("t3/persistence/Services/ProjectionTurns/ProjectionTurnRepository") {}