@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 1,338 lines 52.8 KB
Raw
1 import {
2 CommandId,
3 type OrchestrationV2Run,
4 OrchestrationV2DomainEvent,
5 type OrchestrationV2ProviderThread,
6 type OrchestrationV2Subagent,
7 type OrchestrationV2TurnItem,
8 OrchestrationV2StoredEvent,
9 ProviderThreadId,
10 RunAttemptId,
11 RunId,
12 RuntimeRequestId,
13 NodeId,
14 type ProjectId,
15 ThreadId,
16 } from "@t3tools/contracts";
17 import * as ProviderCommitBatch from "./ProviderCommitBatch.ts";
18 import * as ThreadViewers from "./ThreadViewers.ts";
19 import * as Context from "effect/Context";
20 import * as DateTime from "effect/DateTime";
21 import * as Effect from "effect/Effect";
22 import * as Layer from "effect/Layer";
23 import * as Option from "effect/Option";
24 import * as PubSub from "effect/PubSub";
25 import * as Semaphore from "effect/Semaphore";
26 import * as Schema from "effect/Schema";
27 import * as Stream from "effect/Stream";
28 import * as SqlClient from "effect/sql/SqlClient";
29 import * as SqlError from "effect/sql/SqlError";
30
31 import { replayAndBufferProjectedLiveEvents } from "./LiveStreamBudget.ts";
32 import type { UnsequencedProjectEvent } from "../persistence/OrchestrationEventStore.ts";
33 import { projectStoredEventForWire } from "./WireProjection.ts";
34 import { makeRunOutcomeRecorder, type RunOutcome } from "./RunMetrics.ts";
35
36 import * as CommandReceiptStore from "./CommandReceiptStore.ts";
37 import * as EffectOutbox from "./EffectOutbox.ts";
38 import * as EventStore from "./EventStore.ts";
39 import * as ProjectionStore from "./ProjectionStore.ts";
40 import * as ProjectStore from "./ProjectStore.ts";
41 import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
42
43 export class EventSinkWriteError extends Schema.TaggedError<EventSinkWriteError>()(
44 "EventSinkWriteError",
45 {
46 eventCount: Schema.Number,
47 commandId: Schema.optional(CommandId),
48 cause: Schema.optional(Schema.Defect()),
49 },
50 ) {
51 override get message(): string {
52 return `Failed to write ${this.eventCount} orchestration V2 event(s).`;
53 }
54 }
55
56 export class EventSinkStreamError extends Schema.TaggedError<EventSinkStreamError>()(
57 "EventSinkStreamError",
58 {
59 threadId: Schema.optional(ThreadId),
60 afterSequence: Schema.optional(Schema.Number),
61 cause: Schema.optional(Schema.Defect()),
62 },
63 ) {
64 override get message(): string {
65 return this.threadId === undefined
66 ? "Failed to stream orchestration V2 events."
67 : `Failed to stream orchestration V2 events for thread ${this.threadId}.`;
68 }
69 }
70
71 export const EventSinkV2Error = Schema.Union([EventSinkWriteError, EventSinkStreamError]);
72 export type EventSinkV2Error = typeof EventSinkV2Error.Type;
73
74 interface ProviderBatchPublication {
75 readonly publish: Array<Effect.Effect<void>>;
76 readonly projection: ProjectionStore.ProviderBatchProjection;
77 /** The projection high-water mark, recorded once before the batch commits. */
78 projectedSequence: number | undefined;
79 eventCount: number;
80 }
81 const CurrentProviderBatch = Context.Reference<ProviderBatchPublication | undefined>(
82 "t3/orchestration-v2/EventSink/CurrentProviderBatch",
83 { defaultValue: () => undefined },
84 );
85 // Projecting `provider-thread.updated` also rewrites its app thread's row.
86 const projectedThreadIds = (events: ReadonlyArray<OrchestrationV2DomainEvent>) =>
87 events.flatMap((event) =>
88 event.type === "provider-thread.updated" && event.payload.appThreadId !== null
89 ? [event.threadId, event.payload.appThreadId]
90 : [event.threadId],
91 );
92
93 export interface EventSinkV2Shape {
94 /** Items that are not provider snapshots, such as a consumer's own control items, never batch. */
95 readonly consumeProviderEvents: <A extends { readonly type: string }, E, R, E2, R2>(
96 source: Stream.Stream<A, E, R>,
97 process: (event: A) => Effect.Effect<void, E2, R2>,
98 after: Effect.Effect<boolean, E2, R2>,
99 canBatch: Effect.Effect<boolean, never, R2>,
100 priority?: ProviderCommitBatch.StreamPriority,
101 ) => Effect.Effect<
102 void,
103 E | E2 | ProviderCommitBatch.CommitBatchError | Schema.SchemaError,
104 R | R2
105 >;
106
107 /** Normalize and persist an ordered group against one transaction; publish after its commit. */
108 readonly withProviderBatch: <A, E, R>(
109 effect: Effect.Effect<A, E, R>,
110 ) => Effect.Effect<A, E | EventSinkV2Error, R>;
111
112 readonly write: (input: {
113 /** Return no events if another thread owns this native session at commit time. */
114 readonly unlessNativeThreadOwned?: Pick<
115 OrchestrationV2ProviderThread,
116 "driver" | "providerInstanceId" | "appThreadId" | "nativeThreadRef"
117 >;
118 readonly guardPendingUserInputCancellations?: boolean;
119 readonly commandId?: CommandId;
120 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
121 }) => Effect.Effect<ReadonlyArray<OrchestrationV2StoredEvent>, EventSinkV2Error>;
122 readonly writeWithEffects: (input: {
123 /**
124 * Rebuilds each run.updated from the stored row with only its delegatedCompletion replaced.
125 * The batch must not also change that run's lifecycle.
126 */
127 readonly updateDelegatedCompletionOnly?: boolean;
128 readonly unlessNativeThreadOwned?: Pick<
129 OrchestrationV2ProviderThread,
130 "driver" | "providerInstanceId" | "appThreadId" | "nativeThreadRef"
131 >;
132 readonly guardPendingUserInputCancellations?: boolean;
133 readonly commandId?: CommandId;
134 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
135 readonly effects: ReadonlyArray<EffectOutbox.PendingOrchestrationEffectV2>;
136 }) => Effect.Effect<ReadonlyArray<OrchestrationV2StoredEvent>, EventSinkV2Error>;
137 readonly writeIfRunCurrent: (input: {
138 readonly guardPendingUserInputCancellations?: boolean;
139 readonly commandId?: CommandId;
140 readonly threadId: ThreadId;
141 readonly runId: RunId;
142 readonly activeAttemptId: RunAttemptId;
143 readonly expectedStatus: OrchestrationV2Run["status"];
144 /** Also refuse while this run interrupt request is stored without its result. */
145 readonly unlessInterruptRequested?: {
146 readonly requestId: OrchestrationV2TurnItem["id"];
147 readonly resultId: OrchestrationV2TurnItem["id"];
148 };
149 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
150 readonly effects?: ReadonlyArray<EffectOutbox.PendingOrchestrationEffectV2>;
151 }) => Effect.Effect<
152 {
153 readonly committed: boolean;
154 readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>;
155 },
156 EventSinkV2Error
157 >;
158 /** Commits only if the expected attempt and ordinal still own the provider thread, protecting newer attempts. */
159 readonly writeIfProviderThreadOwner: (input: {
160 readonly guardPendingUserInputCancellations?: boolean;
161 readonly commandId?: CommandId;
162 readonly providerThreadId: ProviderThreadId;
163 readonly runId: RunId;
164 readonly activeAttemptId: RunAttemptId;
165 readonly expectedLastRunOrdinal: number;
166 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
167 }) => Effect.Effect<
168 {
169 readonly committed: boolean;
170 readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>;
171 },
172 EventSinkV2Error
173 >;
174 /** Commits only if the full provider row equals expected; some writers do not advance updatedAt. */
175 readonly writeIfProviderThreadUnchanged: (input: {
176 readonly expected: OrchestrationV2ProviderThread;
177 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
178 }) => Effect.Effect<
179 {
180 readonly committed: boolean;
181 readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>;
182 },
183 EventSinkV2Error
184 >;
185 /** Commits only if the stored turn item still equals expected. */
186 readonly writeIfTurnItemUnchanged: (input: {
187 readonly expected: OrchestrationV2TurnItem;
188 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
189 }) => Effect.Effect<
190 {
191 readonly committed: boolean;
192 readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>;
193 },
194 EventSinkV2Error
195 >;
196 /** Commits only if the stored subagent still equals expected. */
197 readonly writeIfSubagentUnchanged: (input: {
198 readonly expected: OrchestrationV2Subagent;
199 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
200 }) => Effect.Effect<
201 {
202 readonly committed: boolean;
203 readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>;
204 },
205 EventSinkV2Error
206 >;
207 readonly commitCommand: (input: {
208 readonly commandId: CommandId;
209 readonly threadId: ThreadId;
210 readonly commandType: string;
211 readonly acceptedAt: DateTime.Utc;
212 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
213 readonly effects: ReadonlyArray<EffectOutbox.PendingOrchestrationEffectV2>;
214 readonly cancelUnsettledEffects?: {
215 readonly effectTypes: ReadonlyArray<EffectOutbox.OrchestrationEffectRequestV2["type"]>;
216 readonly reason: string;
217 };
218 /** Like writeWithEffects' updateDelegatedCompletionOnly, for this run's run.updated only. */
219 readonly updateDelegatedCompletionOnlyRunId?: RunId;
220 }) => Effect.Effect<
221 {
222 readonly receipt: CommandReceiptStore.CommandReceiptV2;
223 readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>;
224 readonly committed: boolean;
225 readonly cancelledEffectCount: number;
226 },
227 EventSinkV2Error
228 >;
229 readonly commitRejectedCommand: (input: {
230 readonly commandId: CommandId;
231 readonly threadId: ThreadId;
232 readonly commandType: string;
233 readonly rejectedAt: DateTime.Utc;
234 readonly error: string;
235 }) => Effect.Effect<CommandReceiptStore.CommandReceiptV2, EventSinkV2Error>;
236 readonly commitProjectCommand: (input: {
237 readonly commandId: CommandId;
238 readonly projectId: ProjectId;
239 readonly commandType: string;
240 readonly acceptedAt: DateTime.Utc;
241 readonly event: UnsequencedProjectEvent;
242 }) => Effect.Effect<
243 { readonly receipt: CommandReceiptStore.ProjectCommandReceiptV2; readonly committed: boolean },
244 EventSinkV2Error
245 >;
246 readonly commitRejectedProjectCommand: (input: {
247 readonly commandId: CommandId;
248 readonly projectId: ProjectId;
249 readonly commandType: string;
250 readonly rejectedAt: DateTime.Utc;
251 readonly error: string;
252 }) => Effect.Effect<CommandReceiptStore.ProjectCommandReceiptV2, EventSinkV2Error>;
253 readonly stream: (input?: {
254 readonly threadId?: ThreadId;
255 readonly afterSequence?: number;
256 readonly eventType?: OrchestrationV2DomainEvent["type"];
257 /** Bound RPC subscribers; internal workers must not drop their subscription under load. */
258 readonly bounded?: boolean;
259 }) => Stream.Stream<OrchestrationV2StoredEvent, EventSinkV2Error>;
260 readonly latestSequence: (input?: {
261 readonly threadId?: ThreadId;
262 }) => Effect.Effect<number, EventSinkV2Error>;
263 readonly readByCommandId: (input: {
264 readonly commandId: CommandId;
265 }) => Stream.Stream<OrchestrationV2StoredEvent, EventSinkV2Error>;
266 }
267
268 export class EventSinkV2 extends Context.Service<EventSinkV2, EventSinkV2Shape>()(
269 "t3/orchestration-v2/EventSink/EventSinkV2",
270 ) {}
271
272 const layerBase: Layer.Layer<
273 EventSinkV2,
274 never,
275 | CommandReceiptStore.CommandReceiptStoreV2
276 | EffectOutbox.EffectOutboxV2
277 | EventStore.EventStoreV2
278 | ProjectionStore.ProjectionStoreV2
279 | ProjectStore.ProjectStoreV2
280 | SqlClient.SqlClient
281 | TurnItemPositionStore.TurnItemPositionStoreV2
282 | ThreadViewers.ThreadViewers
283 > = Layer.effect(
284 EventSinkV2,
285 Effect.gen(function* () {
286 const sql = yield* SqlClient.SqlClient;
287 const threadViewers = yield* ThreadViewers.ThreadViewers;
288 const commandReceipts = yield* CommandReceiptStore.CommandReceiptStoreV2;
289 const effectOutbox = yield* EffectOutbox.EffectOutboxV2;
290 const eventStore = yield* EventStore.EventStoreV2;
291 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
292 const projectStore = yield* ProjectStore.ProjectStoreV2;
293 const turnItemPositions = yield* TurnItemPositionStore.TurnItemPositionStoreV2;
294 const liveEvents = yield* PubSub.unbounded<OrchestrationV2StoredEvent>();
295 const liveEventsByType = new Map<
296 OrchestrationV2DomainEvent["type"],
297 PubSub.PubSub<OrchestrationV2StoredEvent>
298 >();
299 const publishLiveEvents =
300 /* This helper runs for individual orchestration events; the enclosing operation owns the span. */
301 Effect.fnUntraced(function* (events: ReadonlyArray<OrchestrationV2StoredEvent>) {
302 yield* PubSub.publishAll(liveEvents, events);
303 for (const [type, pubsub] of liveEventsByType) {
304 yield* PubSub.publishAll(
305 pubsub,
306 events.filter((stored) => stored.event.type === type),
307 );
308 }
309 });
310 const runOutcomes = makeRunOutcomeRecorder((threadId) =>
311 // One payload row; unlike the shell, it still resolves a deleted child thread.
312 projectionStore
313 .getThread(threadId)
314 .pipe(Effect.map((thread) => thread.lineage.relationshipToParent === "subagent")),
315 );
316 // Recorded before publication, so an interrupted publish cannot lose a committed outcome.
317 const publishStoredEvents = (
318 events: ReadonlyArray<OrchestrationV2StoredEvent>,
319 outcomes: ReadonlyArray<RunOutcome>,
320 ) =>
321 runOutcomes
322 .record(outcomes)
323 .pipe(
324 Effect.andThen(eventStore.publishCommitted(events)),
325 Effect.andThen(publishLiveEvents(events)),
326 );
327
328 // Serialize commit and publish; reordered publication makes clients drop earlier sequences.
329 const publishLane = yield* Semaphore.make(1);
330 const commitThenPublishNow = <A, E, R>(
331 transaction: Effect.Effect<A, E, R>,
332 publish: (committed: A) => Effect.Effect<void>,
333 ) =>
334 Effect.uninterruptibleMask((restore) => {
335 let holdsLane = false;
336 const takeLane = publishLane.take(1).pipe(
337 Effect.andThen(
338 Effect.sync(() => {
339 holdsLane = true;
340 }),
341 ),
342 Effect.uninterruptible,
343 );
344 // Cancellation may roll back the transaction's work. Once it commits,
345 // every subscriber must hear about it before this writer can exit.
346 // Otherwise a failed child run can leave its parent's task running.
347 return sql
348 .withTransaction(Effect.tap(restore(transaction), () => takeLane))
349 .pipe(
350 Effect.tap(publish),
351 Effect.ensuring(
352 Effect.suspend(() => (holdsLane ? publishLane.release(1) : Effect.void)),
353 ),
354 );
355 });
356
357 // A user can answer after terminal normalization reads the pending request.
358 // Recheck inside the write transaction so stale cleanup cannot erase answers.
359 const guardUserInputCancellations =
360 /* This helper runs for individual orchestration events; the enclosing operation owns the span. */
361 Effect.fnUntraced(function* (events: ReadonlyArray<OrchestrationV2DomainEvent>) {
362 const staleRequests = new Set<RuntimeRequestId>();
363 const staleNodes = new Set<NodeId>();
364 for (const event of events) {
365 if (
366 event.type !== "runtime-request.updated" ||
367 event.payload.kind !== "user_input" ||
368 event.payload.status !== "cancelled"
369 )
370 continue;
371 const current = yield* projectionStore.getRuntimeRequest(
372 event.threadId,
373 event.payload.id,
374 );
375 if (
376 current?.status !== "pending" ||
377 current.kind !== "user_input" ||
378 current.providerTurnId !== event.payload.providerTurnId ||
379 current.responseCapability.type === "message"
380 ) {
381 staleRequests.add(event.payload.id);
382 staleNodes.add(event.payload.nodeId);
383 }
384 }
385 return events.filter((event) => {
386 switch (event.type) {
387 case "runtime-request.updated":
388 return event.payload.status !== "cancelled" || !staleRequests.has(event.payload.id);
389 case "node.updated":
390 return event.payload.status !== "cancelled" || !staleNodes.has(event.payload.id);
391 case "turn-item.updated":
392 return (
393 event.payload.type !== "user_input_request" ||
394 event.payload.status !== "cancelled" ||
395 !staleRequests.has(event.payload.requestId)
396 );
397 default:
398 return true;
399 }
400 });
401 });
402
403 const normalizeEvents = (events: ReadonlyArray<OrchestrationV2DomainEvent>) => {
404 const runOrdinals = new Map(
405 events.flatMap((event) =>
406 event.type === "run.created" || event.type === "run.updated"
407 ? [[event.payload.id, event.payload.ordinal] as const]
408 : [],
409 ),
410 );
411 return Effect.forEach(
412 events,
413 (event): Effect.Effect<OrchestrationV2DomainEvent, unknown> =>
414 event.type === "turn-item.updated"
415 ? turnItemPositions
416 .normalize(
417 event.payload,
418 event.payload.runId === null ? undefined : runOrdinals.get(event.payload.runId),
419 )
420 .pipe(Effect.map((payload) => ({ ...event, payload })))
421 : Effect.succeed(event),
422 { concurrency: 1 },
423 );
424 };
425
426 // The enclosing write or commit span already traces this projection bookkeeping.
427 const recordProjectedSequence = Effect.fnUntraced(function* (sequence: number) {
428 const now = DateTime.formatIso(yield* DateTime.now);
429 yield* sql`
430 INSERT INTO orchestration_v2_projection_metadata (
431 projection_name,
432 schema_version,
433 last_sequence,
434 updated_at
435 )
436 VALUES (
437 'thread-projections',
438 ${ProjectionStore.ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION},
439 ${sequence},
440 ${now}
441 )
442 ON CONFLICT(projection_name)
443 DO UPDATE SET
444 schema_version = excluded.schema_version,
445 last_sequence = excluded.last_sequence,
446 updated_at = excluded.updated_at
447 `;
448 });
449
450 const applyStoredEvents =
451 /* This helper runs for individual orchestration events; the enclosing operation owns the span. */
452 Effect.fnUntraced(function* (storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>) {
453 yield* Effect.forEach(storedEvents, (stored) => projectionStore.apply(stored.event), {
454 concurrency: 1,
455 });
456 const sequence = storedEvents.at(-1)?.sequence;
457 if (sequence !== undefined) {
458 const batch = yield* CurrentProviderBatch;
459 if (batch !== undefined) batch.projectedSequence = sequence;
460 else yield* recordProjectedSequence(sequence);
461 }
462 // Read lineage here, on the connection this transaction holds; see makeRunOutcomeRecorder.
463 return yield* runOutcomes.prepare(storedEvents);
464 });
465
466 // The enclosing write or commit span already traces this per-event projection merge.
467 const updateDelegatedCompletionOnly = Effect.fnUntraced(function* (
468 events: ReadonlyArray<OrchestrationV2DomainEvent>,
469 includesRun: (runId: RunId) => boolean,
470 ) {
471 return yield* Effect.forEach(events, (event) =>
472 Effect.gen(function* () {
473 if (event.type !== "run.updated" || !includesRun(event.payload.id)) return event;
474 const { runs } = yield* projectionStore.getThreadRecords(event.threadId, ["runs"], {
475 runIds: [event.payload.id],
476 });
477 const current = runs[0];
478 return current === undefined
479 ? event
480 : {
481 ...event,
482 payload: { ...current, delegatedCompletion: event.payload.delegatedCompletion },
483 };
484 }),
485 );
486 });
487
488 const writeEffect = Effect.fn("orchestrationV2.EventSink.write")(function* (
489 input: Parameters<EventSinkV2Shape["writeWithEffects"]>[0],
490 ) {
491 yield* Effect.annotateCurrentSpan({
492 "orchestration_v2.command_id": input.commandId ?? null,
493 "orchestration_v2.event_count": input.events.length,
494 "orchestration_v2.thread_id": input.events[0]?.threadId ?? null,
495 });
496
497 const batch = yield* CurrentProviderBatch;
498 if (batch !== undefined) batch.eventCount += input.events.length;
499 const { committed } = yield* commitThenPublish(
500 projectedThreadIds(input.events),
501 Effect.gen(function* () {
502 // Check ownership in the same transaction that claims the native session.
503 const imported = input.unlessNativeThreadOwned;
504 if (imported?.nativeThreadRef != null) {
505 const owners = yield* sql`
506 SELECT provider_thread_id
507 FROM orchestration_v2_projection_provider_threads
508 WHERE driver = ${imported.driver}
509 AND provider_instance_id = ${imported.providerInstanceId}
510 AND thread_id IS NOT ${imported.appThreadId}
511 AND json_extract(payload_json, '$.nativeThreadRef.nativeId') = ${imported.nativeThreadRef.nativeId}
512 LIMIT 1
513 `;
514 if (owners.length > 0) return { committed: [], outcomes: [] };
515 }
516 // Checkpoint capture can finish after a cohort writer reads its parent run.
517 // Merge in this transaction so the event log and projection keep that completion.
518 const events = input.updateDelegatedCompletionOnly
519 ? yield* updateDelegatedCompletionOnly(input.events, () => true)
520 : input.events;
521 const normalized = yield* normalizeEvents(
522 input.guardPendingUserInputCancellations === true
523 ? yield* guardUserInputCancellations(events)
524 : events,
525 );
526 const committed = yield* eventStore.append({
527 ...(input.commandId === undefined ? {} : { commandId: input.commandId }),
528 events: normalized,
529 });
530 const outcomes = yield* applyStoredEvents(committed);
531 yield* effectOutbox.enqueue(input.effects);
532 return { committed, outcomes };
533 }).pipe(Effect.withTracerEnabled(false)),
534 ({ committed, outcomes }) =>
535 Effect.gen(function* () {
536 if (input.effects.length > 0) {
537 yield* effectOutbox.notifyAvailable(input.effects.length);
538 }
539 yield* publishStoredEvents(committed, outcomes);
540 }),
541 );
542 return committed;
543 });
544
545 const writeIfRunCurrentEffect = Effect.fn("orchestrationV2.EventSink.writeIfRunCurrent")(
546 function* (input: Parameters<EventSinkV2Shape["writeIfRunCurrent"]>[0]) {
547 yield* Effect.annotateCurrentSpan({
548 "orchestration_v2.command_id": input.commandId ?? null,
549 "orchestration_v2.event_count": input.events.length,
550 "orchestration_v2.run_id": input.runId,
551 "orchestration_v2.thread_id": input.threadId,
552 });
553
554 return yield* commitThenPublish(
555 [input.threadId, ...projectedThreadIds(input.events)],
556 Effect.gen(function* () {
557 const rows = yield* sql<{
558 readonly status: string;
559 readonly active_attempt_id: string | null;
560 }>`
561 SELECT
562 status,
563 json_extract(payload_json, '$.activeAttemptId') AS active_attempt_id
564 FROM orchestration_v2_projection_runs
565 WHERE run_id = ${input.runId}
566 AND thread_id = ${input.threadId}
567 LIMIT 1
568 `;
569 const current = rows[0];
570 if (
571 current === undefined ||
572 current.status !== input.expectedStatus ||
573 current.active_attempt_id !== input.activeAttemptId
574 ) {
575 return {
576 committed: false as const,
577 storedEvents: [] as ReadonlyArray<OrchestrationV2StoredEvent>,
578 };
579 }
580 if (input.unlessInterruptRequested !== undefined) {
581 const { requestId, resultId } = input.unlessInterruptRequested;
582 const pending = yield* sql<{ readonly pending: number }>`
583 SELECT EXISTS (
584 SELECT 1 FROM orchestration_v2_projection_turn_items
585 WHERE thread_id = ${input.threadId} AND turn_item_id = ${requestId}
586 ) AND NOT EXISTS (
587 SELECT 1 FROM orchestration_v2_projection_turn_items
588 WHERE thread_id = ${input.threadId} AND turn_item_id = ${resultId}
589 ) AS pending
590 `;
591 if (pending[0]?.pending === 1) {
592 return {
593 committed: false as const,
594 storedEvents: [] as ReadonlyArray<OrchestrationV2StoredEvent>,
595 };
596 }
597 }
598
599 const normalized = yield* normalizeEvents(
600 input.guardPendingUserInputCancellations === true
601 ? yield* guardUserInputCancellations(input.events)
602 : input.events,
603 );
604 const storedEvents = yield* eventStore.append({
605 ...(input.commandId === undefined ? {} : { commandId: input.commandId }),
606 events: normalized,
607 });
608 const outcomes = yield* applyStoredEvents(storedEvents);
609 yield* effectOutbox.enqueue(input.effects ?? []);
610 return { committed: true as const, storedEvents, outcomes };
611 }),
612 (result) =>
613 Effect.gen(function* () {
614 if (!result.committed) return;
615 if (input.effects !== undefined && input.effects.length > 0) {
616 yield* effectOutbox.notifyAvailable(input.effects.length);
617 }
618 yield* publishStoredEvents(result.storedEvents, result.outcomes);
619 }),
620 );
621 },
622 );
623
624 const writeIfProviderThreadOwnerEffect = Effect.fn(
625 "orchestrationV2.EventSink.writeIfProviderThreadOwner",
626 )(function* (input: Parameters<EventSinkV2Shape["writeIfProviderThreadOwner"]>[0]) {
627 yield* Effect.annotateCurrentSpan({
628 "orchestration_v2.command_id": input.commandId ?? null,
629 "orchestration_v2.event_count": input.events.length,
630 "orchestration_v2.provider_thread_id": input.providerThreadId,
631 "orchestration_v2.run_id": input.runId,
632 "orchestration_v2.active_attempt_id": input.activeAttemptId,
633 "orchestration_v2.expected_last_run_ordinal": input.expectedLastRunOrdinal,
634 });
635
636 return yield* commitThenPublish(
637 projectedThreadIds(input.events),
638 Effect.gen(function* () {
639 const rows = yield* sql<{
640 readonly active_attempt_id: string | null;
641 readonly last_run_ordinal: number | null;
642 }>`
643 SELECT
644 json_extract(r.payload_json, '$.activeAttemptId') AS active_attempt_id,
645 p.last_run_ordinal
646 FROM orchestration_v2_projection_provider_threads p
647 JOIN orchestration_v2_projection_runs r
648 ON r.run_id = ${input.runId}
649 AND r.thread_id = p.thread_id
650 WHERE p.provider_thread_id = ${input.providerThreadId}
651 LIMIT 1
652 `;
653 const current = rows[0];
654 if (
655 current === undefined ||
656 current.active_attempt_id !== input.activeAttemptId ||
657 current.last_run_ordinal !== input.expectedLastRunOrdinal
658 ) {
659 return {
660 committed: false as const,
661 storedEvents: [] as ReadonlyArray<OrchestrationV2StoredEvent>,
662 };
663 }
664
665 const normalized = yield* normalizeEvents(
666 input.guardPendingUserInputCancellations === true
667 ? yield* guardUserInputCancellations(input.events)
668 : input.events,
669 );
670 const storedEvents = yield* eventStore.append({
671 ...(input.commandId === undefined ? {} : { commandId: input.commandId }),
672 events: normalized,
673 });
674 const outcomes = yield* applyStoredEvents(storedEvents);
675 return { committed: true as const, storedEvents, outcomes };
676 }),
677 (result) =>
678 result.committed
679 ? publishStoredEvents(result.storedEvents, result.outcomes)
680 : Effect.void,
681 );
682 });
683
684 // Appends the events only if the stored payload still matches, in one transaction.
685 const writeIfStoredPayloadMatches = <E>(
686 readStoredPayload: Effect.Effect<ReadonlyArray<{ readonly payload_json: string }>, E>,
687 expectedJson: string,
688 events: ReadonlyArray<OrchestrationV2DomainEvent>,
689 ) =>
690 commitThenPublish(
691 projectedThreadIds(events),
692 Effect.gen(function* () {
693 const rows = yield* readStoredPayload;
694 if (rows[0]?.payload_json !== expectedJson) {
695 return {
696 committed: false as const,
697 storedEvents: [] as ReadonlyArray<OrchestrationV2StoredEvent>,
698 };
699 }
700
701 const normalized = yield* normalizeEvents(events);
702 const storedEvents = yield* eventStore.append({ events: normalized });
703 const outcomes = yield* applyStoredEvents(storedEvents);
704 return { committed: true as const, storedEvents, outcomes };
705 }),
706 (result) =>
707 result.committed
708 ? publishStoredEvents(result.storedEvents, result.outcomes)
709 : Effect.void,
710 );
711
712 const writeIfProviderThreadUnchangedEffect = Effect.fn(
713 "orchestrationV2.EventSink.writeIfProviderThreadUnchanged",
714 )(function* (input: Parameters<EventSinkV2Shape["writeIfProviderThreadUnchanged"]>[0]) {
715 yield* Effect.annotateCurrentSpan({
716 "orchestration_v2.event_count": input.events.length,
717 "orchestration_v2.provider_thread_id": input.expected.id,
718 });
719 const expectedJson = yield* ProjectionStore.encodeProviderThreadPayload(input.expected);
720 return yield* writeIfStoredPayloadMatches(
721 sql<{ readonly payload_json: string }>`
722 SELECT payload_json
723 FROM orchestration_v2_projection_provider_threads
724 WHERE provider_thread_id = ${input.expected.id}
725 LIMIT 1
726 `,
727 expectedJson,
728 input.events,
729 );
730 });
731
732 const writeIfTurnItemUnchangedEffect = Effect.fn(
733 "orchestrationV2.EventSink.writeIfTurnItemUnchanged",
734 )(function* (input: Parameters<EventSinkV2Shape["writeIfTurnItemUnchanged"]>[0]) {
735 yield* Effect.annotateCurrentSpan({
736 "orchestration_v2.event_count": input.events.length,
737 "orchestration_v2.turn_item_id": input.expected.id,
738 });
739 const expectedJson = yield* ProjectionStore.encodeTurnItemPayload(input.expected);
740 return yield* writeIfStoredPayloadMatches(
741 sql<{ readonly payload_json: string }>`
742 SELECT payload_json
743 FROM orchestration_v2_projection_turn_items
744 WHERE turn_item_id = ${input.expected.id}
745 LIMIT 1
746 `,
747 expectedJson,
748 input.events,
749 );
750 });
751
752 const writeIfSubagentUnchangedEffect = Effect.fn(
753 "orchestrationV2.EventSink.writeIfSubagentUnchanged",
754 )(function* (input: Parameters<EventSinkV2Shape["writeIfSubagentUnchanged"]>[0]) {
755 yield* Effect.annotateCurrentSpan({
756 "orchestration_v2.event_count": input.events.length,
757 "orchestration_v2.subagent_id": input.expected.id,
758 });
759 const expectedJson = yield* ProjectionStore.encodeSubagentPayload(input.expected);
760 return yield* writeIfStoredPayloadMatches(
761 sql<{ readonly payload_json: string }>`
762 SELECT payload_json
763 FROM orchestration_v2_projection_subagents
764 WHERE subagent_id = ${input.expected.id}
765 LIMIT 1
766 `,
767 expectedJson,
768 input.events,
769 );
770 });
771
772 const existingCommandResult =
773 /* This helper runs for individual orchestration events; the enclosing operation owns the span. */
774 Effect.fnUntraced(function* (commandId: CommandId) {
775 const existing = yield* commandReceipts.getByCommandId(commandId);
776 if (Option.isNone(existing)) {
777 return yield* Effect.die(
778 new Error(`Command receipt ${commandId} disappeared during its transaction.`),
779 );
780 }
781 const storedEvents = yield* eventStore.readByCommandId({ commandId }).pipe(
782 Stream.runCollect,
783 Effect.map((events): ReadonlyArray<OrchestrationV2StoredEvent> => Array.from(events)),
784 );
785 return { receipt: existing.value, storedEvents };
786 });
787
788 const commitCommandEffect = Effect.fn("orchestrationV2.EventSink.commitCommand")(function* (
789 input: Parameters<EventSinkV2Shape["commitCommand"]>[0],
790 ) {
791 const result = yield* commitThenPublish(
792 [input.threadId, ...projectedThreadIds(input.events)],
793 Effect.gen(function* () {
794 const reserved = yield* commandReceipts.insertIfAbsent({
795 commandId: input.commandId,
796 threadId: input.threadId,
797 commandType: input.commandType,
798 acceptedAt: input.acceptedAt,
799 resultSequence: 0,
800 status: "accepted",
801 error: null,
802 });
803 if (!reserved) {
804 const existing = yield* existingCommandResult(input.commandId);
805 return { ...existing, committed: false as const, cancelledEffectIds: [] };
806 }
807
808 // Planning does not lock the source. Once deletion commits, maintenance
809 // can remove its checkpoints, so a new fork must not revive that history.
810 if (input.commandType === "thread.fork") {
811 for (const event of input.events) {
812 if (event.type !== "thread.created" || event.payload.forkedFrom?.type !== "run")
813 continue;
814 const sourceId = event.payload.forkedFrom.threadId;
815 const source = yield* sql`
816 SELECT 1 FROM orchestration_v2_projection_threads
817 WHERE thread_id = ${sourceId} AND deleted_at IS NULL
818 `;
819 if (source.length === 0) {
820 const receipt: CommandReceiptStore.CommandReceiptV2 = {
821 commandId: input.commandId,
822 threadId: input.threadId,
823 commandType: input.commandType,
824 acceptedAt: input.acceptedAt,
825 resultSequence: yield* eventStore.latestSequence({ threadId: input.threadId }),
826 status: "rejected",
827 error: "Cannot fork a missing or deleted thread.",
828 };
829 yield* commandReceipts.upsert(receipt);
830 return {
831 receipt,
832 storedEvents: [],
833 committed: false as const,
834 cancelledEffectIds: [],
835 };
836 }
837 }
838 }
839
840 const runId = input.updateDelegatedCompletionOnlyRunId;
841 const normalized = yield* normalizeEvents(
842 runId === undefined
843 ? input.events
844 : yield* updateDelegatedCompletionOnly(input.events, (id) => id === runId),
845 );
846 const storedEvents = yield* eventStore.append({
847 commandId: input.commandId,
848 events: normalized,
849 });
850 // A Stop that changes nothing can still queue the stop of its delegated tasks.
851 const sequence =
852 storedEvents.at(-1)?.sequence ??
853 (input.effects.length === 0
854 ? undefined
855 : yield* eventStore.latestSequence({ threadId: input.threadId }));
856 if (sequence === undefined) {
857 return yield* Effect.die(
858 new Error(`Command ${input.commandId} produced no orchestration events.`),
859 );
860 }
861 const outcomes = yield* applyStoredEvents(storedEvents);
862 yield* effectOutbox.enqueue(input.effects);
863 const receipt: CommandReceiptStore.CommandReceiptV2 = {
864 commandId: input.commandId,
865 threadId: input.threadId,
866 commandType: input.commandType,
867 acceptedAt: input.acceptedAt,
868 resultSequence: sequence,
869 status: "accepted",
870 error: null,
871 };
872 yield* commandReceipts.upsert(receipt);
873 const cancelledEffectIds =
874 input.cancelUnsettledEffects === undefined
875 ? []
876 : yield* effectOutbox.cancelUnsettled({
877 threadId: input.threadId,
878 ...input.cancelUnsettledEffects,
879 });
880 return { receipt, storedEvents, outcomes, committed: true as const, cancelledEffectIds };
881 }),
882 (result) =>
883 Effect.gen(function* () {
884 yield* effectOutbox.signalCancellations(result.cancelledEffectIds);
885 if (result.committed && input.effects.length > 0) {
886 yield* effectOutbox.notifyAvailable(input.effects.length);
887 }
888 if (result.committed) yield* publishStoredEvents(result.storedEvents, result.outcomes);
889 }),
890 );
891 return {
892 receipt: result.receipt,
893 storedEvents: result.storedEvents,
894 committed: result.committed,
895 cancelledEffectCount: result.cancelledEffectIds.length,
896 };
897 });
898
899 const commitRejectedCommandEffect = Effect.fn(
900 "orchestrationV2.EventSink.commitRejectedCommand",
901 )(function* (input: Parameters<EventSinkV2Shape["commitRejectedCommand"]>[0]) {
902 return yield* sql.withTransaction(
903 Effect.gen(function* () {
904 const sequence = yield* eventStore.latestSequence({ threadId: input.threadId });
905 const receipt: CommandReceiptStore.CommandReceiptV2 = {
906 commandId: input.commandId,
907 threadId: input.threadId,
908 commandType: input.commandType,
909 acceptedAt: input.rejectedAt,
910 resultSequence: sequence,
911 status: "rejected",
912 error: input.error,
913 };
914 const inserted = yield* commandReceipts.insertIfAbsent(receipt);
915 if (inserted) {
916 return receipt;
917 }
918 const existing = yield* commandReceipts.getByCommandId(input.commandId);
919 return Option.getOrElse(existing, () => receipt);
920 }),
921 );
922 });
923
924 const existingProjectReceipt = (commandId: CommandId) =>
925 commandReceipts.getProjectByCommandId(commandId).pipe(
926 Effect.flatMap(
927 Option.match({
928 onNone: () =>
929 Effect.fail(`Command ${commandId} was already used by a thread command.` as const),
930 onSome: Effect.succeed,
931 }),
932 ),
933 );
934
935 const commitProjectCommandEffect = Effect.fn("orchestrationV2.EventSink.commitProjectCommand")(
936 function* (input: Parameters<EventSinkV2Shape["commitProjectCommand"]>[0]) {
937 const result = yield* commitThenPublish(
938 [],
939 Effect.gen(function* () {
940 const reserved: CommandReceiptStore.ProjectCommandReceiptV2 = {
941 commandId: input.commandId,
942 projectId: input.projectId,
943 commandType: input.commandType,
944 acceptedAt: input.acceptedAt,
945 resultSequence: 0,
946 status: "accepted",
947 error: null,
948 };
949 if (!(yield* commandReceipts.insertIfAbsent(reserved))) {
950 return { receipt: yield* existingProjectReceipt(input.commandId), event: undefined };
951 }
952 const event = yield* eventStore.appendProjectEvent(input.event);
953 yield* projectStore.apply(event);
954 const receipt = { ...reserved, resultSequence: event.sequence };
955 yield* commandReceipts.upsert(receipt);
956 return { receipt, event };
957 }),
958 (result) =>
959 result.event === undefined ? Effect.void : eventStore.publishCommitted([result.event]),
960 );
961 return { receipt: result.receipt, committed: result.event !== undefined };
962 },
963 );
964
965 const commitRejectedProjectCommandEffect = Effect.fn(
966 "orchestrationV2.EventSink.commitRejectedProjectCommand",
967 )(function* (input: Parameters<EventSinkV2Shape["commitRejectedProjectCommand"]>[0]) {
968 return yield* sql.withTransaction(
969 Effect.gen(function* () {
970 const receipt: CommandReceiptStore.ProjectCommandReceiptV2 = {
971 commandId: input.commandId,
972 projectId: input.projectId,
973 commandType: input.commandType,
974 acceptedAt: input.rejectedAt,
975 resultSequence: yield* eventStore.latestApplicationSequence,
976 status: "rejected",
977 error: input.error,
978 };
979 return (yield* commandReceipts.insertIfAbsent(receipt))
980 ? receipt
981 : yield* existingProjectReceipt(input.commandId);
982 }),
983 );
984 });
985
986 const catchUp = (input: {
987 readonly afterSequence: number;
988 readonly throughSequence: number;
989 readonly threadId?: ThreadId;
990 readonly eventType?: OrchestrationV2DomainEvent["type"];
991 }): Stream.Stream<OrchestrationV2StoredEvent, unknown> => {
992 const pageSize = 256;
993 const loop = (afterSequence: number): Stream.Stream<OrchestrationV2StoredEvent, unknown> =>
994 Stream.unwrap(
995 eventStore
996 .read({
997 afterSequence,
998 throughSequence: input.throughSequence,
999 ...(input.threadId === undefined ? {} : { threadId: input.threadId }),
1000 ...(input.eventType === undefined ? {} : { eventType: input.eventType }),
1001 limit: pageSize,
1002 })
1003 .pipe(
1004 Stream.runCollect,
1005 Effect.map((chunk) => Array.from(chunk)),
1006 Effect.map((events) => {
1007 if (events.length === 0) {
1008 return Stream.empty;
1009 }
1010 const current = Stream.fromIterable(events);
1011 const last = events.at(-1)?.sequence ?? input.throughSequence;
1012 return events.length < pageSize || last >= input.throughSequence
1013 ? current
1014 : Stream.concat(current, loop(last));
1015 }),
1016 ),
1017 );
1018 return loop(input.afterSequence);
1019 };
1020
1021 const stream = (input?: Parameters<EventSinkV2Shape["stream"]>[0]) => {
1022 const afterSequence = input?.afterSequence ?? 0;
1023 const matches = (stored: OrchestrationV2StoredEvent) =>
1024 (input?.threadId === undefined || stored.event.threadId === input.threadId) &&
1025 (input?.eventType === undefined || stored.event.type === input.eventType);
1026 const replay = (throughSequence: number) =>
1027 catchUp({
1028 afterSequence,
1029 throughSequence,
1030 ...(input?.threadId === undefined ? {} : { threadId: input.threadId }),
1031 ...(input?.eventType === undefined ? {} : { eventType: input.eventType }),
1032 }).pipe(Stream.filter(matches));
1033 return Stream.unwrap(
1034 Effect.gen(function* () {
1035 let pubsub = liveEvents;
1036 if (input?.eventType !== undefined) {
1037 const existing = liveEventsByType.get(input.eventType);
1038 if (existing !== undefined) {
1039 pubsub = existing;
1040 } else {
1041 const created = yield* PubSub.unbounded<OrchestrationV2StoredEvent>();
1042 pubsub = liveEventsByType.get(input.eventType) ?? created;
1043 liveEventsByType.set(input.eventType, pubsub);
1044 }
1045 }
1046 if (input?.bounded === true) {
1047 return replayAndBufferProjectedLiveEvents({
1048 subscribe: PubSub.subscribe(pubsub),
1049 latestSequence: eventStore.latestSequence(),
1050 afterSequence,
1051 filter: matches,
1052 replay,
1053 project: projectStoredEventForWire,
1054 });
1055 }
1056 const subscription = yield* PubSub.subscribe(pubsub);
1057 const highWater = yield* eventStore.latestSequence();
1058 const live = Stream.fromSubscription(subscription).pipe(
1059 Stream.filter((stored) => stored.sequence > Math.max(highWater, afterSequence)),
1060 Stream.filter(matches),
1061 );
1062 return Stream.concat(replay(highWater), live);
1063 }),
1064 );
1065 };
1066
1067 // Waits only for queued provider snapshots of `threadIds`, the threads this write touches.
1068 const commitThenPublish = <A, E, R>(
1069 threadIds: ReadonlyArray<ThreadId>,
1070 transaction: Effect.Effect<A, E, R>,
1071 publish: (committed: A) => Effect.Effect<void>,
1072 ) =>
1073 Effect.flatMap(CurrentProviderBatch, (batch) =>
1074 batch === undefined
1075 ? Effect.flatMap(Effect.serviceOption(sql.transactionService), (existing) =>
1076 Option.isSome(existing)
1077 ? commitThenPublishNow(transaction, publish)
1078 : coordinator.barrier(commitThenPublishNow(transaction, publish), threadIds),
1079 )
1080 : transaction.pipe(
1081 Effect.tap((committed) =>
1082 Effect.sync(() => {
1083 batch.publish.push(publish(committed));
1084 }),
1085 ),
1086 ),
1087 );
1088
1089 const withProviderBatch: EventSinkV2Shape["withProviderBatch"] = Effect.fn(
1090 "orchestrationV2.EventSink.providerBatch",
1091 )(function* <A, E, R>(effect: Effect.Effect<A, E, R>) {
1092 if ((yield* CurrentProviderBatch) !== undefined) return yield* effect;
1093 const batch: ProviderBatchPublication = {
1094 publish: [],
1095 eventCount: 0,
1096 projection: { nodeReads: new Map(), threadTouches: new Map() },
1097 projectedSequence: undefined,
1098 };
1099 let started = false;
1100 const result = yield* commitThenPublishNow(
1101 Effect.sync(() => {
1102 started = true;
1103 }).pipe(
1104 Effect.andThen(
1105 effect.pipe(
1106 Effect.provideService(CurrentProviderBatch, batch),
1107 Effect.provideService(ProjectionStore.ProviderBatchProjectionState, batch.projection),
1108 Effect.tap(() =>
1109 ProjectionStore.writeThreadTouches(sql, batch.projection.threadTouches).pipe(
1110 Effect.andThen(
1111 batch.projectedSequence === undefined
1112 ? Effect.void
1113 : recordProjectedSequence(batch.projectedSequence),
1114 ),
1115 ),
1116 ),
1117 ),
1118 ),
1119 ),
1120 () => Effect.forEach(batch.publish, (publish) => publish, { discard: true }),
1121 ).pipe(
1122 // Normalization mutates run-local state, so only retry before its transaction body starts.
1123 Effect.retry({
1124 while: (cause) =>
1125 !started && SqlError.isSqlError(cause) && cause.reason._tag === "LockTimeoutError",
1126 times: 2,
1127 }),
1128 Effect.mapError((cause) =>
1129 SqlError.isSqlError(cause)
1130 ? new EventSinkWriteError({ eventCount: batch.eventCount, cause })
1131 : cause,
1132 ),
1133 );
1134 yield* Effect.annotateCurrentSpan({
1135 "orchestration_v2.event_count": batch.eventCount,
1136 "orchestration_v2.batch_writes": batch.publish.length,
1137 });
1138 return result;
1139 });
1140
1141 const coordinator = yield* ProviderCommitBatch.make(
1142 (work) =>
1143 withProviderBatch(work).pipe(
1144 Effect.mapError((cause) =>
1145 ProviderCommitBatch.isCommitBatchError(cause)
1146 ? cause
1147 : new ProviderCommitBatch.CommitBatchError({ cause }),
1148 ),
1149 ),
1150 yield* ProviderCommitBatch.enabled,
1151 // Each snapshot owns a savepoint and its deferred publications within the shared commit.
1152 Effect.fnUntraced(function* (
1153 work: Effect.Effect<void, ProviderCommitBatch.CommitBatchError>,
1154 ) {
1155 const batch = yield* CurrentProviderBatch;
1156 const publicationCount = batch?.publish.length ?? 0;
1157 const eventCount = batch?.eventCount ?? 0;
1158 const projectedSequence = batch?.projectedSequence;
1159 const threadTouches = new Map(batch?.projection.threadTouches);
1160 return yield* sql.withTransaction(work).pipe(
1161 Effect.onError(() =>
1162 Effect.sync(() => {
1163 if (batch !== undefined) {
1164 batch.publish.length = publicationCount;
1165 batch.eventCount = eventCount;
1166 batch.projectedSequence = projectedSequence;
1167 batch.projection.nodeReads.clear();
1168 batch.projection.threadTouches.clear();
1169 for (const [threadId, updatedAt] of threadTouches)
1170 batch.projection.threadTouches.set(threadId, updatedAt);
1171 }
1172 }),
1173 ),
1174 Effect.mapError((cause) =>
1175 ProviderCommitBatch.isCommitBatchError(cause)
1176 ? cause
1177 : new ProviderCommitBatch.CommitBatchError({ cause }),
1178 ),
1179 );
1180 }),
1181 threadViewers.isViewed,
1182 );
1183
1184 return EventSinkV2.of({
1185 consumeProviderEvents: coordinator.consume,
1186 withProviderBatch,
1187 write: (input) =>
1188 writeEffect({ ...input, effects: [] }).pipe(
1189 Effect.mapError(
1190 (cause) =>
1191 new EventSinkWriteError({
1192 eventCount: input.events.length,
1193 ...(input.commandId === undefined ? {} : { commandId: input.commandId }),
1194 cause,
1195 }),
1196 ),
1197 ),
1198 writeWithEffects: (input) =>
1199 writeEffect(input).pipe(
1200 Effect.mapError(
1201 (cause) =>
1202 new EventSinkWriteError({
1203 eventCount: input.events.length,
1204 ...(input.commandId === undefined ? {} : { commandId: input.commandId }),
1205 cause,
1206 }),
1207 ),
1208 ),
1209 writeIfRunCurrent: (input) =>
1210 writeIfRunCurrentEffect(input).pipe(
1211 Effect.mapError(
1212 (cause) =>
1213 new EventSinkWriteError({
1214 eventCount: input.events.length,
1215 ...(input.commandId === undefined ? {} : { commandId: input.commandId }),
1216 cause,
1217 }),
1218 ),
1219 ),
1220 writeIfProviderThreadOwner: (input) =>
1221 writeIfProviderThreadOwnerEffect(input).pipe(
1222 Effect.mapError(
1223 (cause) =>
1224 new EventSinkWriteError({
1225 eventCount: input.events.length,
1226 ...(input.commandId === undefined ? {} : { commandId: input.commandId }),
1227 cause,
1228 }),
1229 ),
1230 ),
1231 writeIfProviderThreadUnchanged: (input) =>
1232 writeIfProviderThreadUnchangedEffect(input).pipe(
1233 Effect.mapError(
1234 (cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }),
1235 ),
1236 ),
1237 writeIfTurnItemUnchanged: (input) =>
1238 writeIfTurnItemUnchangedEffect(input).pipe(
1239 Effect.mapError(
1240 (cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }),
1241 ),
1242 ),
1243 writeIfSubagentUnchanged: (input) =>
1244 writeIfSubagentUnchangedEffect(input).pipe(
1245 Effect.mapError(
1246 (cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }),
1247 ),
1248 ),
1249 commitCommand: (input) =>
1250 commitCommandEffect(input).pipe(
1251 Effect.mapError(
1252 (cause) =>
1253 new EventSinkWriteError({
1254 commandId: input.commandId,
1255 eventCount: input.events.length,
1256 cause,
1257 }),
1258 ),
1259 ),
1260 commitRejectedCommand: (input) =>
1261 commitRejectedCommandEffect(input).pipe(
1262 Effect.mapError(
1263 (cause) =>
1264 new EventSinkWriteError({
1265 commandId: input.commandId,
1266 eventCount: 0,
1267 cause,
1268 }),
1269 ),
1270 ),
1271 commitProjectCommand: (input) =>
1272 commitProjectCommandEffect(input).pipe(
1273 Effect.mapError(
1274 (cause) =>
1275 new EventSinkWriteError({ commandId: input.commandId, eventCount: 1, cause }),
1276 ),
1277 ),
1278 commitRejectedProjectCommand: (input) =>
1279 commitRejectedProjectCommandEffect(input).pipe(
1280 Effect.mapError(
1281 (cause) =>
1282 new EventSinkWriteError({ commandId: input.commandId, eventCount: 0, cause }),
1283 ),
1284 ),
1285 stream: (input) =>
1286 stream(input).pipe(
1287 Stream.mapError(
1288 (cause) =>
1289 new EventSinkStreamError({
1290 ...(input?.threadId === undefined ? {} : { threadId: input.threadId }),
1291 ...(input?.afterSequence === undefined
1292 ? {}
1293 : { afterSequence: input.afterSequence }),
1294 cause,
1295 }),
1296 ),
1297 ),
1298 latestSequence: (input) =>
1299 eventStore.latestSequence(input).pipe(
1300 Effect.mapError(
1301 (cause) =>
1302 new EventSinkStreamError({
1303 ...(input?.threadId === undefined ? {} : { threadId: input.threadId }),
1304 cause,
1305 }),
1306 ),
1307 ),
1308 readByCommandId: (input) =>
1309 eventStore.readByCommandId(input).pipe(
1310 Stream.mapError(
1311 (cause) =>
1312 new EventSinkStreamError({
1313 cause,
1314 }),
1315 ),
1316 ),
1317 } satisfies EventSinkV2Shape);
1318 }),
1319 );
1320
1321 /** Share the worker's outbox instance: enqueue wakeups are in-memory. */
1322 export const layerFromStores = layerBase;
1323
1324 export const layer: Layer.Layer<
1325 EventSinkV2,
1326 never,
1327 EventStore.EventStoreV2 | ProjectionStore.ProjectionStoreV2 | SqlClient.SqlClient
1328 > = layerBase.pipe(
1329 Layer.provide(
1330 Layer.mergeAll(
1331 CommandReceiptStore.layer,
1332 EffectOutbox.layer,
1333 ProjectStore.layer,
1334 TurnItemPositionStore.layer,
1335 ThreadViewers.layer,
1336 ),
1337 ),
1338 );