@devries-cameron20 / t3code Mirror · read-only / commits / 9fae3ee0e9
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 25, 17:15 UTC

look over effect services

Julius Marminge committed Feb 26, 2026 at 00:18 UTC 9fae3ee0e95b036388b597558ee58617b0035494
44 files changed +765 -280
.plans/16c-pr89-remediation-checklist.md
+6 -6
@@ -210,8 +210,8 @@ Counts: active `51` (`valid=33`, `partially-valid=18`), closed-invalid `6`
210 - Threads: PRRT_kwDORLtfbc5wiLhQ
211 - Audit note: Snapshot sequence can under-report due to extra projectors, but replay impact is lower now.
212
213 -- [ ] `C033` Three error classes defined but never instantiated anywhere
214 - - Status: `TODO`
213 +- [x] `C033` Three error classes defined but never instantiated anywhere
214 + - Status: `DONE`
215 - Verdict: `partially-valid`
216 - Severity: `Low`
217 - Area: `Checkpointing correctness`
@@ -219,8 +219,8 @@ Counts: active `51` (`valid=33`, `partially-valid=18`), closed-invalid `6`
219 - Threads: PRRT_kwDORLtfbc5wlYgo
220 - Audit note: Original claim overstated; some errors used, others appear unused.
221
222 -- [ ] `C034` Redundant `CheckpointInvariantError` in `CheckpointServiceError` union type
223 - - Status: `TODO`
222 +- [x] `C034` Redundant `CheckpointInvariantError` in `CheckpointServiceError` union type
223 + - Status: `DONE`
224 - Verdict: `valid`
225 - Severity: `Low`
226 - Area: `Checkpointing correctness`
@@ -228,8 +228,8 @@ Counts: active `51` (`valid=33`, `partially-valid=18`), closed-invalid `6`
228 - Threads: PRRT_kwDORLtfbc5wj5fn
229 - Audit note: CheckpointInvariantError remains redundantly included in service union.
230
231 -- [ ] `C035` Redundant error type in CheckpointServiceError union definition
232 - - Status: `TODO`
231 +- [x] `C035` Redundant error type in CheckpointServiceError union definition
232 + - Status: `DONE`
233 - Verdict: `valid`
234 - Severity: `Low`
235 - Area: `Checkpointing correctness`
apps/server/src/checkpointing/Errors.ts
+2 -38
@@ -19,37 +19,6 @@ export class CheckpointUnavailableError extends Schema.TaggedErrorClass<Checkpoi
19 }
20 }
21
22 -/**
23 - * CheckpointRepositoryError - Checkpointing unavailable for cwd/repository.
24 - */
25 -export class CheckpointRepositoryError extends Schema.TaggedErrorClass<CheckpointRepositoryError>()(
26 - "CheckpointRepositoryError",
27 - {
28 - cwd: Schema.String,
29 - detail: Schema.String,
30 - cause: Schema.optional(Schema.Defect),
31 - },
32 -) {
33 - override get message(): string {
34 - return `Checkpoint repository error for ${this.cwd}: ${this.detail}`;
35 - }
36 -}
37 -
38 -/**
39 - * CheckpointSessionNotFoundError - Session is missing in checkpoint workflow.
40 - */
41 -export class CheckpointSessionNotFoundError extends Schema.TaggedErrorClass<CheckpointSessionNotFoundError>()(
42 - "CheckpointSessionNotFoundError",
43 - {
44 - sessionId: Schema.String,
45 - cause: Schema.optional(Schema.Defect),
46 - },
47 -) {
48 - override get message(): string {
49 - return `Checkpoint session not found: ${this.sessionId}`;
50 - }
51 -}
52 -
22 /**
23 * CheckpointInvariantError - Inconsistent provider/filesystem/catalog state.
24 */
@@ -69,11 +38,6 @@ export class CheckpointInvariantError extends Schema.TaggedErrorClass<Checkpoint
38 export type CheckpointStoreError =
39 | GitCommandError
40 | CheckpointInvariantError
72 - | CheckpointUnavailableError
73 - | CheckpointRepositoryError;
41 + | CheckpointUnavailableError;
42
75 -export type CheckpointServiceError =
76 - | CheckpointStoreError
77 - | ProjectionRepositoryError
78 - | CheckpointSessionNotFoundError
79 - | CheckpointInvariantError;
43 +export type CheckpointServiceError = CheckpointStoreError | ProjectionRepositoryError;
apps/server/src/checkpointing/Layers/CheckpointDiffQuery.ts
-1
@@ -1,6 +1,5 @@
1 import {
2 OrchestrationGetTurnDiffResult,
3 - type ProjectId,
3 type OrchestrationGetFullThreadDiffInput,
4 type OrchestrationGetFullThreadDiffResult,
5 type OrchestrationGetTurnDiffResult as OrchestrationGetTurnDiffResultType,
apps/server/src/checkpointing/Services/CheckpointDiffQuery.ts
+25
@@ -1,3 +1,11 @@
1 +/**
2 + * CheckpointDiffQuery - Query interface for computed checkpoint diffs.
3 + *
4 + * Provides read-only diff operations across checkpoint snapshots used by
5 + * orchestration APIs.
6 + *
7 + * @module CheckpointDiffQuery
8 + */
9 import type {
10 OrchestrationGetFullThreadDiffInput,
11 OrchestrationGetFullThreadDiffResult,
@@ -9,15 +17,32 @@ import type { Effect } from "effect";
17
18 import type { CheckpointServiceError } from "../Errors.ts";
19
20 +/**
21 + * CheckpointDiffQueryShape - Service API for checkpoint diff queries.
22 + */
23 export interface CheckpointDiffQueryShape {
24 + /**
25 + * Read the patch diff for a single turn checkpoint transition.
26 + *
27 + * Verifies checkpoint availability in both projection state and filesystem.
28 + */
29 readonly getTurnDiff: (
30 input: OrchestrationGetTurnDiffInput,
31 ) => Effect.Effect<OrchestrationGetTurnDiffResult, CheckpointServiceError>;
32 +
33 + /**
34 + * Read the full patch diff across a thread range of checkpoints.
35 + *
36 + * Delegates to turn diff with `fromTurnCount = 0`.
37 + */
38 readonly getFullThreadDiff: (
39 input: OrchestrationGetFullThreadDiffInput,
40 ) => Effect.Effect<OrchestrationGetFullThreadDiffResult, CheckpointServiceError>;
41 }
42
43 +/**
44 + * CheckpointDiffQuery - Service tag for checkpoint diff queries.
45 + */
46 export class CheckpointDiffQuery extends ServiceMap.Service<
47 CheckpointDiffQuery,
48 CheckpointDiffQueryShape
apps/server/src/checkpointing/Services/CheckpointStore.ts
+11
@@ -39,6 +39,9 @@ export interface DeleteCheckpointRefsInput {
39 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
40 }
41
42 +/**
43 + * CheckpointStoreShape - Service API for checkpoint capture/restore and diff access.
44 + */
45 export interface CheckpointStoreShape {
46 /**
47 * Check whether cwd is inside a Git worktree.
@@ -47,6 +50,8 @@ export interface CheckpointStoreShape {
50
51 /**
52 * Capture a checkpoint commit and store it at the provided checkpoint ref.
53 + *
54 + * Uses an isolated temporary Git index and writes a hidden ref.
55 */
56 readonly captureCheckpoint: (
57 input: CaptureCheckpointInput,
@@ -61,6 +66,8 @@ export interface CheckpointStoreShape {
66
67 /**
68 * Restore workspace/staging state to a checkpoint.
69 + *
70 + * Optionally falls back to current `HEAD` when the checkpoint ref is missing.
71 */
72 readonly restoreCheckpoint: (
73 input: RestoreCheckpointInput,
@@ -68,6 +75,8 @@ export interface CheckpointStoreShape {
75
76 /**
77 * Compute patch diff between two checkpoint refs.
78 + *
79 + * Can optionally treat missing "from" ref as `HEAD`.
80 */
81 readonly diffCheckpoints: (
82 input: DiffCheckpointsInput,
@@ -75,6 +84,8 @@ export interface CheckpointStoreShape {
84
85 /**
86 * Delete the provided checkpoint refs.
87 + *
88 + * Best-effort delete: missing refs are tolerated.
89 */
90 readonly deleteCheckpointRefs: (
91 input: DeleteCheckpointRefsInput,
apps/server/src/config.ts
+25
@@ -1,3 +1,11 @@
1 +/**
2 + * ServerConfig and NetService - Runtime configuration services.
3 + *
4 + * Defines process-level server configuration and networking helpers used by
5 + * startup and runtime layers.
6 + *
7 + * @module ServerConfig
8 + */
9 import * as Net from "node:net";
10 import { Data, Effect, FileSystem, Layer, Path, ServiceMap } from "effect";
11
@@ -5,6 +13,9 @@ export const DEFAULT_PORT = 3773;
13
14 export type RuntimeMode = "web" | "desktop";
15
16 +/**
17 + * ServerConfigShape - Process/runtime configuration required by the server.
18 + */
19 export interface ServerConfigShape {
20 readonly mode: RuntimeMode;
21 readonly port: number;
@@ -20,6 +31,9 @@ export interface ServerConfigShape {
31 readonly logWebSocketEvents: boolean;
32 }
33
34 +/**
35 + * ServerConfig - Service tag for server runtime configuration.
36 + */
37 export class ServerConfig extends ServiceMap.Service<ServerConfig, ServerConfigShape>()(
38 "server/ServerConfig",
39 ) {}
@@ -31,10 +45,21 @@ export class NetError extends Data.TaggedError("NetError")<{
45 readonly cause?: unknown;
46 }> {}
47
48 +/**
49 + * NetServiceShape - Networking helper operations used during startup.
50 + */
51 export interface NetServiceShape {
52 + /**
53 + * Resolve an available listening port, preferring the provided port first.
54 + *
55 + * Falls back to an ephemeral OS-assigned port when the preferred port is in use.
56 + */
57 readonly findAvailablePort: (preferred: number) => Effect.Effect<number, NetError>;
58 }
59
60 +/**
61 + * NetService - Service tag for startup networking helpers.
62 + */
63 export class NetService extends ServiceMap.Service<NetService, NetServiceShape>()(
64 "server/NetService",
65 ) {
apps/server/src/git/Errors.ts
+3 -18
@@ -57,26 +57,11 @@ export class GitManagerError extends Schema.TaggedErrorClass<GitManagerError>()(
57 }
58 }
59
60 -/**
61 - * GitCoreError - Errors emitted by low-level Git repository operations.
62 - */
63 -export type GitCoreError = GitCommandError;
64 -
65 -/**
66 - * GitHubCliServiceError - Errors emitted by GitHub CLI service helpers.
67 - */
68 -export type GitHubCliServiceError = GitHubCliError;
69 -
70 -/**
71 - * TextGenerationServiceError - Errors emitted by commit/PR text generation.
72 - */
73 -export type TextGenerationServiceError = TextGenerationError;
74 -
60 /**
61 * GitManagerServiceError - Errors emitted by stacked Git workflow orchestration.
62 */
63 export type GitManagerServiceError =
64 | GitManagerError
80 - | GitCoreError
81 - | GitHubCliServiceError
82 - | TextGenerationServiceError;
65 + | GitCommandError
66 + | GitHubCliError
67 + | TextGenerationError;
apps/server/src/git/Services/GitService.ts
+3
@@ -27,6 +27,9 @@ export interface ExecuteGitResult {
27 readonly stderr: string;
28 }
29
30 +/**
31 + * GitServiceShape - Service API for Git command execution.
32 + */
33 export interface GitServiceShape {
34 /**
35 * Execute a Git command.
apps/server/src/keybindings.ts
+26
@@ -1,3 +1,11 @@
1 +/**
2 + * Keybindings - Keybinding configuration service definitions.
3 + *
4 + * Owns parsing, validation, merge, and persistence of user keybinding
5 + * configuration consumed by the server runtime.
6 + *
7 + * @module Keybindings
8 + */
9 import {
10 KeybindingRule,
11 KeybindingsConfig,
@@ -388,16 +396,34 @@ function mergeWithDefaultKeybindings(custom: ResolvedKeybindingsConfig): Resolve
396 return merged.slice(-MAX_KEYBINDINGS_COUNT);
397 }
398
399 +/**
400 + * KeybindingsShape - Service API for keybinding configuration operations.
401 + */
402 export interface KeybindingsShape {
403 + /**
404 + * Load and resolve keybindings from disk merged with defaults.
405 + *
406 + * Returns an in-memory cached result after the first successful load.
407 + */
408 readonly loadResolvedKeybindingsConfig: Effect.Effect<
409 readonly ResolvedKeybindingRule[],
410 KeybindingsConfigError
411 >;
412 +
413 + /**
414 + * Upsert a keybinding rule and persist the resulting configuration.
415 + *
416 + * Writes config atomically and enforces the max rule count by truncating
417 + * oldest entries when needed.
418 + */
419 readonly upsertKeybindingRule: (
420 rule: KeybindingRule,
421 ) => Effect.Effect<ResolvedKeybindingsConfig, KeybindingsConfigError>;
422 }
423
424 +/**
425 + * Keybindings - Service tag for keybinding configuration operations.
426 + */
427 export class Keybindings extends ServiceMap.Service<Keybindings, KeybindingsShape>()(
428 "server/Keybindings",
429 ) {}
apps/server/src/main.ts
+25
@@ -1,3 +1,11 @@
1 +/**
2 + * CliConfig - CLI/runtime bootstrap service definitions.
3 + *
4 + * Defines startup-only service contracts used while resolving process config
5 + * and constructing server runtime layers.
6 + *
7 + * @module CliConfig
8 + */
9 import { Config, Data, Effect, FileSystem, Layer, Option, Path, Schema, ServiceMap } from "effect";
10 import { Command, Flag } from "effect/unstable/cli";
11
@@ -32,12 +40,29 @@ interface CliInput {
40 readonly logWebSocketEvents: Option.Option<boolean>;
41 }
42
43 +/**
44 + * CliConfigShape - Startup helpers required while building server layers.
45 + */
46 export interface CliConfigShape {
47 + /**
48 + * Current process working directory.
49 + */
50 readonly cwd: string;
51 +
52 + /**
53 + * Apply OS-specific PATH normalization.
54 + */
55 readonly fixPath: Effect.Effect<void>;
56 +
57 + /**
58 + * Resolve static web asset directory for server mode.
59 + */
60 readonly resolveStaticDir: Effect.Effect<string | undefined>;
61 }
62
63 +/**
64 + * CliConfig - Service tag for startup CLI/runtime helpers.
65 + */
66 export class CliConfig extends ServiceMap.Service<CliConfig, CliConfigShape>()("server/CliConfig") {
67 static readonly layer = Layer.effect(
68 CliConfig,
apps/server/src/open.ts
+23
@@ -1,3 +1,11 @@
1 +/**
2 + * Open - Browser/editor launch service interface.
3 + *
4 + * Owns process launch helpers for opening URLs in a browser and workspace
5 + * paths in a configured editor.
6 + *
7 + * @module Open
8 + */
9 import { spawn } from "node:child_process";
10
11 import { EDITORS, type EditorId } from "@t3tools/contracts";
@@ -22,11 +30,26 @@ interface EditorLaunch {
30 readonly args: ReadonlyArray<string>;
31 }
32
33 +/**
34 + * OpenShape - Service API for browser and editor launch actions.
35 + */
36 export interface OpenShape {
37 + /**
38 + * Open a URL target in the default browser.
39 + */
40 readonly openBrowser: (target: string) => Effect.Effect<void, OpenError>;
41 +
42 + /**
43 + * Open a workspace path in a selected editor integration.
44 + *
45 + * Launches the editor as a detached process so server startup is not blocked.
46 + */
47 readonly openInEditor: (input: OpenInEditorInput) => Effect.Effect<void, OpenError>;
48 }
49
50 +/**
51 + * Open - Service tag for browser/editor launch operations.
52 + */
53 export class Open extends ServiceMap.Service<Open, OpenShape>()("server/Open") {}
54
55 // ==============================
apps/server/src/orchestration/Errors.ts
-4
@@ -1,8 +1,6 @@
1 import { SchemaIssue, Schema } from "effect";
2
3 import type {
4 - OrchestrationCommandReceiptRepositoryError,
5 - OrchestrationEventStoreError,
4 ProjectionRepositoryError,
5 } from "../persistence/Errors.ts";
6
@@ -83,8 +81,6 @@ export class OrchestrationListenerCallbackError extends Schema.TaggedErrorClass<
81 }
82
83 export type OrchestrationDispatchError =
86 - | OrchestrationEventStoreError
87 - | OrchestrationCommandReceiptRepositoryError
84 | ProjectionRepositoryError
85 | OrchestrationCommandInvariantError
86 | OrchestrationCommandPreviouslyRejectedError
apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
+7 -17
@@ -14,9 +14,7 @@ import * as SqlClient from "effect/unstable/sql/SqlClient";
14 import * as SqlSchema from "effect/unstable/sql/SqlSchema";
15
16 import {
17 - PersistenceDecodeError,
18 - PersistenceSerializationError,
19 - PersistenceSqlError,
17 + isPersistenceError,
18 toPersistenceDecodeError,
19 toPersistenceSqlError,
20 type ProjectionRepositoryError,
@@ -106,19 +104,6 @@ function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: st
104 : toPersistenceSqlError(sqlOperation)(cause);
105 }
106
109 -function toProjectionRepositoryError(operation: string) {
110 - return (cause: unknown): ProjectionRepositoryError => {
111 - if (
112 - cause instanceof PersistenceSqlError ||
113 - cause instanceof PersistenceDecodeError ||
114 - cause instanceof PersistenceSerializationError
115 - ) {
116 - return cause;
117 - }
118 - return toPersistenceSqlError(operation)(cause);
119 - };
120 -}
121 -
107 const makeProjectionSnapshotQuery = Effect.gen(function* () {
108 const sql = yield* SqlClient.SqlClient;
109
@@ -441,7 +426,12 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
426 }),
427 )
428 .pipe(
444 - Effect.mapError(toProjectionRepositoryError("ProjectionSnapshotQuery.getSnapshot:query")),
429 + Effect.mapError((error) => {
430 + if (isPersistenceError(error)) {
431 + return error;
432 + }
433 + return toPersistenceSqlError("ProjectionSnapshotQuery.getSnapshot:query")(error);
434 + }),
435 );
436
437 return {
apps/server/src/orchestration/Services/CheckpointReactor.ts
+17
@@ -1,16 +1,33 @@
1 +/**
2 + * CheckpointReactor - Checkpoint reaction service interface.
3 + *
4 + * Owns background workers that react to orchestration checkpoint lifecycle
5 + * events and apply checkpoint side effects.
6 + *
7 + * @module CheckpointReactor
8 + */
9 import { ServiceMap } from "effect";
10 import type { Effect, Scope } from "effect";
11
12 +/**
13 + * CheckpointReactorShape - Service API for checkpoint reactor lifecycle.
14 + */
15 export interface CheckpointReactorShape {
16 /**
17 * Start the checkpoint reactor.
18 *
19 * The returned effect must be run in a scope so all worker fibers can be
20 * finalized on shutdown.
21 + *
22 + * Consumes both orchestration-domain and provider-runtime events via an
23 + * internal queue.
24 */
25 readonly start: Effect.Effect<void, never, Scope.Scope>;
26 }
27
28 +/**
29 + * CheckpointReactor - Service tag for checkpoint reactor workers.
30 + */
31 export class CheckpointReactor extends ServiceMap.Service<
32 CheckpointReactor,
33 CheckpointReactorShape
apps/server/src/orchestration/Services/OrchestrationEngine.ts
+8
@@ -21,6 +21,9 @@ import type { Effect, Stream } from "effect";
21 import type { OrchestrationDispatchError } from "../Errors.ts";
22 import type { OrchestrationEventStoreError } from "../../persistence/Errors.ts";
23
24 +/**
25 + * OrchestrationEngineShape - Service API for orchestration command and event flow.
26 + */
27 export interface OrchestrationEngineShape {
28 /**
29 * Read the current in-memory orchestration read model.
@@ -44,6 +47,9 @@ export interface OrchestrationEngineShape {
47 *
48 * @param command - Valid orchestration command.
49 * @returns Effect containing the sequence of the persisted event.
50 + *
51 + * Dispatch is serialized through an internal queue and deduplicated via
52 + * command receipts.
53 */
54 readonly dispatch: (
55 command: OrchestrationCommand,
@@ -51,6 +57,8 @@ export interface OrchestrationEngineShape {
57
58 /**
59 * Stream persisted domain events in dispatch order.
60 + *
61 + * This is a hot runtime stream (new events only), not a historical replay.
62 */
63 readonly streamDomainEvents: Stream.Stream<OrchestrationEvent>;
64 }
apps/server/src/orchestration/Services/OrchestrationReactor.ts
+14
@@ -1,6 +1,17 @@
1 +/**
2 + * OrchestrationReactor - Composite orchestration reactor service interface.
3 + *
4 + * Coordinates startup of orchestration runtime reactors that translate domain
5 + * events into downstream side effects.
6 + *
7 + * @module OrchestrationReactor
8 + */
9 import { ServiceMap } from "effect";
10 import type { Effect, Scope } from "effect";
11
12 +/**
13 + * OrchestrationReactorShape - Service API for orchestration reactor lifecycle.
14 + */
15 export interface OrchestrationReactorShape {
16 /**
17 * Start orchestration-side reactors for provider/runtime/checkpoint flows.
@@ -11,6 +22,9 @@ export interface OrchestrationReactorShape {
22 readonly start: Effect.Effect<void, never, Scope.Scope>;
23 }
24
25 +/**
26 + * OrchestrationReactor - Service tag for orchestration reactor coordination.
27 + */
28 export class OrchestrationReactor extends ServiceMap.Service<
29 OrchestrationReactor,
30 OrchestrationReactorShape
apps/server/src/orchestration/Services/ProjectionPipeline.ts
+26 -9
@@ -1,24 +1,41 @@
1 +/**
2 + * OrchestrationProjectionPipeline - Event projection pipeline service interface.
3 + *
4 + * Coordinates projection bootstrap/replay and per-event projection updates for
5 + * orchestration read models.
6 + *
7 + * @module OrchestrationProjectionPipeline
8 + */
9 import type { OrchestrationEvent } from "@t3tools/contracts";
10 import { ServiceMap } from "effect";
11 import type { Effect } from "effect";
12
5 -import type {
6 - OrchestrationEventStoreError,
7 - ProjectionRepositoryError,
8 -} from "../../persistence/Errors.ts";
9 -
10 -export type OrchestrationProjectionPipelineError =
11 - | OrchestrationEventStoreError
12 - | ProjectionRepositoryError;
13 +import type { ProjectionRepositoryError } from "../../persistence/Errors.ts";
14
15 +/**
16 + * OrchestrationProjectionPipelineShape - Service API for projection execution.
17 + */
18 export interface OrchestrationProjectionPipelineShape {
15 - readonly bootstrap: Effect.Effect<void, OrchestrationProjectionPipelineError>;
19 + /**
20 + * Bootstrap projections by replaying persisted events.
21 + *
22 + * Resumes each projector from its stored projection-state cursor.
23 + */
24 + readonly bootstrap: Effect.Effect<void, ProjectionRepositoryError>;
25
26 + /**
27 + * Project a single orchestration event into projection repositories.
28 + *
29 + * Projectors are executed sequentially to preserve deterministic ordering.
30 + */
31 readonly projectEvent: (
32 event: OrchestrationEvent,
33 ) => Effect.Effect<void, ProjectionRepositoryError>;
34 }
35
36 +/**
37 + * OrchestrationProjectionPipeline - Service tag for orchestration projections.
38 + */
39 export class OrchestrationProjectionPipeline extends ServiceMap.Service<
40 OrchestrationProjectionPipeline,
41 OrchestrationProjectionPipelineShape
apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts
+20
@@ -1,13 +1,33 @@
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 + */
9 import type { OrchestrationReadModel } from "@t3tools/contracts";
10 import { ServiceMap } from "effect";
11 import type { Effect } from "effect";
12
13 import type { ProjectionRepositoryError } from "../../persistence/Errors.ts";
14
15 +/**
16 + * ProjectionSnapshotQueryShape - Service API for read-model snapshots.
17 + */
18 export interface ProjectionSnapshotQueryShape {
19 + /**
20 + * Read the latest orchestration projection snapshot.
21 + *
22 + * Rehydrates from projection tables and derives snapshot sequence from
23 + * projector cursor state.
24 + */
25 readonly getSnapshot: () => Effect.Effect<OrchestrationReadModel, ProjectionRepositoryError>;
26 }
27
28 +/**
29 + * ProjectionSnapshotQuery - Service tag for projection snapshot queries.
30 + */
31 export class ProjectionSnapshotQuery extends ServiceMap.Service<
32 ProjectionSnapshotQuery,
33 ProjectionSnapshotQueryShape
apps/server/src/orchestration/Services/ProviderCommandReactor.ts
+17
@@ -1,16 +1,33 @@
1 +/**
2 + * ProviderCommandReactor - Provider command reaction service interface.
3 + *
4 + * Owns background workers that react to orchestration intent events and
5 + * dispatch provider-side command execution.
6 + *
7 + * @module ProviderCommandReactor
8 + */
9 import { ServiceMap } from "effect";
10 import type { Effect, Scope } from "effect";
11
12 +/**
13 + * ProviderCommandReactorShape - Service API for provider command reactors.
14 + */
15 export interface ProviderCommandReactorShape {
16 /**
17 * Start reacting to provider-intent orchestration domain events.
18 *
19 * The returned effect must be run in a scope so all worker fibers can be
20 * finalized on shutdown.
21 + *
22 + * Filters orchestration domain events to provider-intent types before
23 + * processing.
24 */
25 readonly start: Effect.Effect<void, never, Scope.Scope>;
26 }
27
28 +/**
29 + * ProviderCommandReactor - Service tag for provider command reaction workers.
30 + */
31 export class ProviderCommandReactor extends ServiceMap.Service<
32 ProviderCommandReactor,
33 ProviderCommandReactorShape
apps/server/src/orchestration/Services/ProviderRuntimeIngestion.ts
+17
@@ -1,16 +1,33 @@
1 +/**
2 + * ProviderRuntimeIngestionService - Provider runtime ingestion service interface.
3 + *
4 + * Owns background workers that consume provider runtime streams and emit
5 + * orchestration commands/events.
6 + *
7 + * @module ProviderRuntimeIngestionService
8 + */
9 import { ServiceMap } from "effect";
10 import type { Effect, Scope } from "effect";
11
12 +/**
13 + * ProviderRuntimeIngestionShape - Service API for runtime ingestion lifecycle.
14 + */
15 export interface ProviderRuntimeIngestionShape {
16 /**
17 * Start ingesting provider runtime events into orchestration commands.
18 *
19 * The returned effect must be run in a scope so all worker fibers can be
20 * finalized on shutdown.
21 + *
22 + * Uses an internal queue and continues after non-interrupt failures by
23 + * logging warnings.
24 */
25 readonly start: Effect.Effect<void, never, Scope.Scope>;
26 }
27
28 +/**
29 + * ProviderRuntimeIngestionService - Service tag for runtime ingestion workers.
30 + */
31 export class ProviderRuntimeIngestionService extends ServiceMap.Service<
32 ProviderRuntimeIngestionService,
33 ProviderRuntimeIngestionShape
apps/server/src/persistence/Errors.ts
+5 -116
@@ -30,19 +30,6 @@ export class PersistenceDecodeError extends Schema.TaggedErrorClass<PersistenceD
30 }
31 }
32
33 -export class PersistenceSerializationError extends Schema.TaggedErrorClass<PersistenceSerializationError>()(
34 - "PersistenceSerializationError",
35 - {
36 - operation: Schema.String,
37 - detail: Schema.String,
38 - cause: Schema.optional(Schema.Defect),
39 - },
40 -) {
41 - override get message(): string {
42 - return `Serialization error in ${this.operation}: ${this.detail}`;
43 - }
44 -}
45 -
33 export function toPersistenceSqlError(operation: string) {
34 return (cause: unknown): PersistenceSqlError =>
35 new PersistenceSqlError({
@@ -70,85 +57,8 @@ export function toPersistenceDecodeCauseError(operation: string) {
57 });
58 }
59
73 -export function toPersistenceSerializationError(operation: string) {
74 - return (cause: unknown): PersistenceSerializationError =>
75 - new PersistenceSerializationError({
76 - operation,
77 - detail: `Failed to execute ${operation}`,
78 - cause,
79 - });
80 -}
81 -
82 -// ===============================
83 -// Project Repository Errors
84 -// ===============================
85 -
86 -export class ProjectValidationError extends Schema.TaggedErrorClass<ProjectValidationError>()(
87 - "ProjectValidationError",
88 - {
89 - operation: Schema.String,
90 - issue: Schema.String,
91 - cause: Schema.optional(Schema.Defect),
92 - },
93 -) {
94 - override get message(): string {
95 - return `Project validation failed in ${this.operation}: ${this.issue}`;
96 - }
97 -}
98 -
99 -export class ProjectPathMissingError extends Schema.TaggedErrorClass<ProjectPathMissingError>()(
100 - "ProjectPathMissingError",
101 - {
102 - cwd: Schema.String,
103 - cause: Schema.optional(Schema.Defect),
104 - },
105 -) {
106 - override get message(): string {
107 - return `Project path does not exist: ${this.cwd}`;
108 - }
109 -}
110 -
111 -export class ProjectNotFoundError extends Schema.TaggedErrorClass<ProjectNotFoundError>()(
112 - "ProjectNotFoundError",
113 - {
114 - projectId: Schema.String,
115 - cause: Schema.optional(Schema.Defect),
116 - },
117 -) {
118 - override get message(): string {
119 - return `Project not found: ${this.projectId}`;
120 - }
121 -}
122 -
123 -// ===============================
124 -// Checkpoint Repository Errors
125 -// ===============================
126 -
127 -export class CheckpointRepositoryValidationError extends Schema.TaggedErrorClass<CheckpointRepositoryValidationError>()(
128 - "CheckpointRepositoryValidationError",
129 - {
130 - operation: Schema.String,
131 - issue: Schema.String,
132 - cause: Schema.optional(Schema.Defect),
133 - },
134 -) {
135 - override get message(): string {
136 - return `Checkpoint repository validation failed in ${this.operation}: ${this.issue}`;
137 - }
138 -}
139 -
140 -export class CheckpointRepositoryPersistenceError extends Schema.TaggedErrorClass<CheckpointRepositoryPersistenceError>()(
141 - "CheckpointRepositoryPersistenceError",
142 - {
143 - operation: Schema.String,
144 - detail: Schema.String,
145 - cause: Schema.optional(Schema.Defect),
146 - },
147 -) {
148 - override get message(): string {
149 - return `Checkpoint repository persistence error in ${this.operation}: ${this.detail}`;
150 - }
151 -}
60 +export const isPersistenceError = (u: unknown) =>
61 + Schema.is(PersistenceSqlError)(u) || Schema.is(PersistenceDecodeError)(u);
62
63 // ===============================
64 // Provider Session Repository Errors
@@ -180,22 +90,7 @@ export class ProviderSessionRepositoryPersistenceError extends Schema.TaggedErro
90 }
91 }
92
183 -export type ProjectRepositoryError =
184 - | PersistenceSqlError
185 - | PersistenceDecodeError
186 - | PersistenceSerializationError
187 - | ProjectValidationError
188 - | ProjectPathMissingError
189 - | ProjectNotFoundError;
190 -
191 -export type OrchestrationEventStoreError =
192 - | PersistenceSqlError
193 - | PersistenceDecodeError
194 - | PersistenceSerializationError;
195 -
196 -export type CheckpointRepositoryError =
197 - | CheckpointRepositoryValidationError
198 - | CheckpointRepositoryPersistenceError;
93 +export type OrchestrationEventStoreError = PersistenceSqlError | PersistenceDecodeError;
94
95 export type ProviderSessionRepositoryError =
96 | ProviderSessionRepositoryValidationError
@@ -205,12 +100,6 @@ export type OrchestrationCommandReceiptRepositoryError =
100 | PersistenceSqlError
101 | PersistenceDecodeError;
102
208 -export type ProviderSessionRuntimeRepositoryError =
209 - | PersistenceSqlError
210 - | PersistenceDecodeError
211 - | PersistenceSerializationError;
103 +export type ProviderSessionRuntimeRepositoryError = PersistenceSqlError | PersistenceDecodeError;
104
213 -export type ProjectionRepositoryError =
214 - | PersistenceSqlError
215 - | PersistenceDecodeError
216 - | PersistenceSerializationError;
105 +export type ProjectionRepositoryError = PersistenceSqlError | PersistenceDecodeError;
apps/server/src/persistence/NodeSqliteClient.ts
+5
@@ -1,6 +1,8 @@
1 /**
2 * Port of `@effect/sql-sqlite-node` that uses the native `node:sqlite`
3 * bindings instead of `better-sqlite3`.
4 + *
5 + * @module SqliteClient
6 */
7 import { DatabaseSync, type StatementSync } from "node:sqlite";
8
@@ -27,6 +29,9 @@ export const TypeId: TypeId = "~local/sqlite-node/SqliteClient";
29
30 export type TypeId = "~local/sqlite-node/SqliteClient";
31
32 +/**
33 + * SqliteClient - Effect service tag for the sqlite SQL client.
34 + */
35 export const SqliteClient = ServiceMap.Service<Client.SqlClient>("@local/sqlite-node/SqliteClient");
36
37 export interface SqliteClientConfig {
apps/server/src/persistence/Services/OrchestrationCommandReceipts.ts
+22
@@ -1,3 +1,11 @@
1 +/**
2 + * OrchestrationCommandReceiptRepository - Repository interface for command receipts.
3 + *
4 + * Owns persistence operations for deduplication and status tracking of
5 + * orchestration command handling.
6 + *
7 + * @module OrchestrationCommandReceiptRepository
8 + */
9 import {
10 CommandId,
11 IsoDateTime,
@@ -28,11 +36,22 @@ export const GetByCommandIdInput = Schema.Struct({
36 });
37 export type GetByCommandIdInput = typeof GetByCommandIdInput.Type;
38
39 +/**
40 + * OrchestrationCommandReceiptRepositoryShape - Service API for command receipts.
41 + */
42 export interface OrchestrationCommandReceiptRepositoryShape {
43 + /**
44 + * Insert or replace a command receipt row.
45 + *
46 + * Upserts by `commandId` for idempotent command-result tracking.
47 + */
48 readonly upsert: (
49 receipt: OrchestrationCommandReceipt,
50 ) => Effect.Effect<void, OrchestrationCommandReceiptRepositoryError>;
51
52 + /**
53 + * Read a command receipt by command id.
54 + */
55 readonly getByCommandId: (
56 input: GetByCommandIdInput,
57 ) => Effect.Effect<
@@ -41,6 +60,9 @@ export interface OrchestrationCommandReceiptRepositoryShape {
60 >;
61 }
62
63 +/**
64 + * OrchestrationCommandReceiptRepository - Service tag for command receipt persistence.
65 + */
66 export class OrchestrationCommandReceiptRepository extends ServiceMap.Service<
67 OrchestrationCommandReceiptRepository,
68 OrchestrationCommandReceiptRepositoryShape
apps/server/src/persistence/Services/OrchestrationEventStore.ts
+7
@@ -15,12 +15,17 @@ import type { Effect, Stream } from "effect";
15
16 import type { OrchestrationEventStoreError } from "../Errors.ts";
17
18 +/**
19 + * OrchestrationEventStoreShape - Service API for orchestration event persistence.
20 + */
21 export interface OrchestrationEventStoreShape {
22 /**
23 * Persist a new orchestration event.
24 *
25 * @param event - Event payload without sequence (assigned by storage).
26 * @returns Effect containing the stored event with assigned sequence.
27 + *
28 + * Actor kind is inferred from command/metadata before persistence.
29 */
30 readonly append: (
31 event: Omit<OrchestrationEvent, "sequence">,
@@ -32,6 +37,8 @@ export interface OrchestrationEventStoreShape {
37 * @param sequenceExclusive - Sequence cursor (exclusive).
38 * @param limit - Maximum number of events to emit.
39 * @returns Stream containing ordered events.
40 + *
41 + * Reads in fixed-size pages and normalizes non-integer/negative limits.
42 */
43 readonly readFromSequence: (
44 sequenceExclusive: number,
apps/server/src/persistence/Services/ProjectionCheckpoints.ts
+30
@@ -1,3 +1,11 @@
1 +/**
2 + * ProjectionCheckpointRepository - Projection repository interface for checkpoints.
3 + *
4 + * Owns persistence operations for projected checkpoint summaries in thread
5 + * timelines.
6 + *
7 + * @module ProjectionCheckpointRepository
8 + */
9 import {
10 CheckpointRef,
11 IsoDateTime,
@@ -41,22 +49,44 @@ export const DeleteByThreadIdInput = Schema.Struct({
49 });
50 export type DeleteByThreadIdInput = typeof DeleteByThreadIdInput.Type;
51
52 +/**
53 + * ProjectionCheckpointRepositoryShape - Service API for projected checkpoints.
54 + */
55 export interface ProjectionCheckpointRepositoryShape {
56 + /**
57 + * Insert or replace a projected checkpoint row.
58 + *
59 + * Upserts by composite key `(threadId, checkpointTurnCount)`.
60 + */
61 readonly upsert: (row: ProjectionCheckpoint) => Effect.Effect<void, ProjectionRepositoryError>;
62
63 + /**
64 + * List projected checkpoints for a thread.
65 + *
66 + * Returned in ascending checkpoint turn-count order.
67 + */
68 readonly listByThreadId: (
69 input: ListByThreadIdInput,
70 ) => Effect.Effect<ReadonlyArray<ProjectionCheckpoint>, ProjectionRepositoryError>;
71
72 + /**
73 + * Read a projected checkpoint by thread and turn-count key.
74 + */
75 readonly getByThreadAndTurnCount: (
76 input: GetByThreadAndTurnCountInput,
77 ) => Effect.Effect<Option.Option<ProjectionCheckpoint>, ProjectionRepositoryError>;
78
79 + /**
80 + * Delete projected checkpoint rows by thread.
81 + */
82 readonly deleteByThreadId: (
83 input: DeleteByThreadIdInput,
84 ) => Effect.Effect<void, ProjectionRepositoryError>;
85 }
86
87 +/**
88 + * ProjectionCheckpointRepository - Service tag for checkpoint projection persistence.
89 + */
90 export class ProjectionCheckpointRepository extends ServiceMap.Service<
91 ProjectionCheckpointRepository,
92 ProjectionCheckpointRepositoryShape
apps/server/src/persistence/Services/ProjectionPendingApprovals.ts
+30
@@ -1,3 +1,11 @@
1 +/**
2 + * ProjectionPendingApprovalRepository - Repository interface for pending approvals.
3 + *
4 + * Owns persistence operations for projected approval requests awaiting user
5 + * decisions.
6 + *
7 + * @module ProjectionPendingApprovalRepository
8 + */
9 import {
10 ApprovalRequestId,
11 IsoDateTime,
@@ -37,24 +45,46 @@ export const DeleteProjectionPendingApprovalInput = Schema.Struct({
45 });
46 export type DeleteProjectionPendingApprovalInput = typeof DeleteProjectionPendingApprovalInput.Type;
47
48 +/**
49 + * ProjectionPendingApprovalRepositoryShape - Service API for pending approvals.
50 + */
51 export interface ProjectionPendingApprovalRepositoryShape {
52 + /**
53 + * Insert or replace a projected pending approval row.
54 + *
55 + * Upserts by `requestId`.
56 + */
57 readonly upsert: (
58 row: ProjectionPendingApproval,
59 ) => Effect.Effect<void, ProjectionRepositoryError>;
60
61 + /**
62 + * List pending approvals for a thread.
63 + *
64 + * Returned in ascending creation order.
65 + */
66 readonly listByThreadId: (
67 input: ListProjectionPendingApprovalsInput,
68 ) => Effect.Effect<ReadonlyArray<ProjectionPendingApproval>, ProjectionRepositoryError>;
69
70 + /**
71 + * Read a pending approval row by request id.
72 + */
73 readonly getByRequestId: (
74 input: GetProjectionPendingApprovalInput,
75 ) => Effect.Effect<Option.Option<ProjectionPendingApproval>, ProjectionRepositoryError>;
76
77 + /**
78 + * Delete a pending approval row by request id.
79 + */
80 readonly deleteByRequestId: (
81 input: DeleteProjectionPendingApprovalInput,
82 ) => Effect.Effect<void, ProjectionRepositoryError>;
83 }
84
85 +/**
86 + * ProjectionPendingApprovalRepository - Service tag for pending approval persistence.
87 + */
88 export class ProjectionPendingApprovalRepository extends ServiceMap.Service<
89 ProjectionPendingApprovalRepository,
90 ProjectionPendingApprovalRepositoryShape
apps/server/src/persistence/Services/ProjectionPendingTurnStarts.ts
+25
@@ -1,3 +1,11 @@
1 +/**
2 + * ProjectionPendingTurnStartRepository - Repository interface for pending turn starts.
3 + *
4 + * Owns persistence operations for projected "pending turn start" markers used
5 + * while creating new turns from queued messages.
6 + *
7 + * @module ProjectionPendingTurnStartRepository
8 + */
9 import { IsoDateTime, MessageId, ThreadId } from "@t3tools/contracts";
10 import { Option, Schema, ServiceMap } from "effect";
11 import type { Effect } from "effect";
@@ -21,20 +29,37 @@ export const DeleteProjectionPendingTurnStartInput = Schema.Struct({
29 });
30 export type DeleteProjectionPendingTurnStartInput = typeof DeleteProjectionPendingTurnStartInput.Type;
31
32 +/**
33 + * ProjectionPendingTurnStartRepositoryShape - Service API for pending turn-start markers.
34 + */
35 export interface ProjectionPendingTurnStartRepositoryShape {
36 + /**
37 + * Insert or replace a pending turn-start row.
38 + *
39 + * Maintains at most one pending marker per thread.
40 + */
41 readonly upsert: (
42 row: ProjectionPendingTurnStart,
43 ) => Effect.Effect<void, ProjectionRepositoryError>;
44
45 + /**
46 + * Read a pending turn-start marker by thread id.
47 + */
48 readonly getByThreadId: (
49 input: GetProjectionPendingTurnStartInput,
50 ) => Effect.Effect<Option.Option<ProjectionPendingTurnStart>, ProjectionRepositoryError>;
51
52 + /**
53 + * Delete a pending turn-start marker by thread id.
54 + */
55 readonly deleteByThreadId: (
56 input: DeleteProjectionPendingTurnStartInput,
57 ) => Effect.Effect<void, ProjectionRepositoryError>;
58 }
59
60 +/**
61 + * ProjectionPendingTurnStartRepository - Service tag for pending turn-start persistence.
62 + */
63 export class ProjectionPendingTurnStartRepository extends ServiceMap.Service<
64 ProjectionPendingTurnStartRepository,
65 ProjectionPendingTurnStartRepositoryShape
apps/server/src/persistence/Services/ProjectionProjects.ts
+30
@@ -1,3 +1,11 @@
1 +/**
2 + * ProjectionProjectRepository - Projection repository interface for projects.
3 + *
4 + * Owns persistence operations for project rows in the orchestration projection
5 + * read model.
6 + *
7 + * @module ProjectionProjectRepository
8 + */
9 import { IsoDateTime, ProjectId, ProjectScript } from "@t3tools/contracts";
10 import { Option, Schema, ServiceMap } from "effect";
11 import type { Effect } from "effect";
@@ -26,23 +34,45 @@ export const DeleteProjectionProjectInput = Schema.Struct({
34 });
35 export type DeleteProjectionProjectInput = typeof DeleteProjectionProjectInput.Type;
36
37 +/**
38 + * ProjectionProjectRepositoryShape - Service API for projected project records.
39 + */
40 export interface ProjectionProjectRepositoryShape {
41 + /**
42 + * Insert or replace a projected project row.
43 + *
44 + * Upserts by `projectId` and persists scripts through JSON encoding.
45 + */
46 readonly upsert: (row: ProjectionProject) => Effect.Effect<void, ProjectionRepositoryError>;
47
48 + /**
49 + * Read a projected project row by id.
50 + */
51 readonly getById: (
52 input: GetProjectionProjectInput,
53 ) => Effect.Effect<Option.Option<ProjectionProject>, ProjectionRepositoryError>;
54
55 + /**
56 + * List all projected project rows.
57 + *
58 + * Returned in deterministic creation order.
59 + */
60 readonly listAll: () => Effect.Effect<
61 ReadonlyArray<ProjectionProject>,
62 ProjectionRepositoryError
63 >;
64
65 + /**
66 + * Soft-delete a projected project row by id.
67 + */
68 readonly deleteById: (
69 input: DeleteProjectionProjectInput,
70 ) => Effect.Effect<void, ProjectionRepositoryError>;
71 }
72
73 +/**
74 + * ProjectionProjectRepository - Service tag for project projection persistence.
75 + */
76 export class ProjectionProjectRepository extends ServiceMap.Service<
77 ProjectionProjectRepository,
78 ProjectionProjectRepositoryShape
apps/server/src/persistence/Services/ProjectionState.ts
+30
@@ -1,3 +1,11 @@
1 +/**
2 + * ProjectionStateRepository - Projection repository interface for projector cursors.
3 + *
4 + * Owns persistence operations for projection cursor state used to resume
5 + * incremental event projection.
6 + *
7 + * @module ProjectionStateRepository
8 + */
9 import { IsoDateTime, NonNegativeInt } from "@t3tools/contracts";
10 import { Option, Schema, ServiceMap } from "effect";
11 import type { Effect } from "effect";
@@ -16,18 +24,40 @@ export const GetProjectionStateInput = Schema.Struct({
24 });
25 export type GetProjectionStateInput = typeof GetProjectionStateInput.Type;
26
27 +/**
28 + * ProjectionStateRepositoryShape - Service API for projector state records.
29 + */
30 export interface ProjectionStateRepositoryShape {
31 + /**
32 + * Insert or replace a projection cursor row.
33 + *
34 + * Upserts by projector name.
35 + */
36 readonly upsert: (row: ProjectionState) => Effect.Effect<void, ProjectionRepositoryError>;
37
38 + /**
39 + * Read projection cursor state for a projector key.
40 + */
41 readonly getByProjector: (
42 input: GetProjectionStateInput,
43 ) => Effect.Effect<Option.Option<ProjectionState>, ProjectionRepositoryError>;
44
45 + /**
46 + * List all projector cursor rows.
47 + */
48 readonly listAll: () => Effect.Effect<ReadonlyArray<ProjectionState>, ProjectionRepositoryError>;
49
50 + /**
51 + * Read the minimum applied sequence across all projectors.
52 + *
53 + * Returns `null` when no projector state rows exist.
54 + */
55 readonly minLastAppliedSequence: () => Effect.Effect<number | null, ProjectionRepositoryError>;
56 }
57
58 +/**
59 + * ProjectionStateRepository - Service tag for projection cursor persistence.
60 + */
61 export class ProjectionStateRepository extends ServiceMap.Service<
62 ProjectionStateRepository,
63 ProjectionStateRepositoryShape
apps/server/src/persistence/Services/ProjectionThreadActivities.ts
+27
@@ -1,3 +1,11 @@
1 +/**
2 + * ProjectionThreadActivityRepository - Projection repository interface for thread activity.
3 + *
4 + * Owns persistence operations for activity timeline entries projected from
5 + * orchestration events.
6 + *
7 + * @module ProjectionThreadActivityRepository
8 + */
9 import {
10 EventId,
11 IsoDateTime,
@@ -33,20 +41,39 @@ export const DeleteProjectionThreadActivitiesInput = Schema.Struct({
41 export type DeleteProjectionThreadActivitiesInput =
42 typeof DeleteProjectionThreadActivitiesInput.Type;
43
44 +/**
45 + * ProjectionThreadActivityRepositoryShape - Service API for projected thread activity.
46 + */
47 export interface ProjectionThreadActivityRepositoryShape {
48 + /**
49 + * Insert or replace a projected thread activity row.
50 + *
51 + * Upserts by `activityId` and JSON-encodes payload.
52 + */
53 readonly upsert: (
54 row: ProjectionThreadActivity,
55 ) => Effect.Effect<void, ProjectionRepositoryError>;
56
57 + /**
58 + * List projected thread activity rows for a thread.
59 + *
60 + * Returned in ascending creation order.
61 + */
62 readonly listByThreadId: (
63 input: ListProjectionThreadActivitiesInput,
64 ) => Effect.Effect<ReadonlyArray<ProjectionThreadActivity>, ProjectionRepositoryError>;
65
66 + /**
67 + * Delete projected thread activity rows by thread.
68 + */
69 readonly deleteByThreadId: (
70 input: DeleteProjectionThreadActivitiesInput,
71 ) => Effect.Effect<void, ProjectionRepositoryError>;
72 }
73
74 +/**
75 + * ProjectionThreadActivityRepository - Service tag for thread activity persistence.
76 + */
77 export class ProjectionThreadActivityRepository extends ServiceMap.Service<
78 ProjectionThreadActivityRepository,
79 ProjectionThreadActivityRepositoryShape
apps/server/src/persistence/Services/ProjectionThreadMessages.ts
+27
@@ -1,3 +1,11 @@
1 +/**
2 + * ProjectionThreadMessageRepository - Projection repository interface for messages.
3 + *
4 + * Owns persistence operations for projected thread messages rendered in the
5 + * orchestration read model.
6 + *
7 + * @module ProjectionThreadMessageRepository
8 + */
9 import {
10 OrchestrationMessageRole,
11 MessageId,
@@ -32,20 +40,39 @@ export const DeleteProjectionThreadMessagesInput = Schema.Struct({
40 });
41 export type DeleteProjectionThreadMessagesInput = typeof DeleteProjectionThreadMessagesInput.Type;
42
43 +/**
44 + * ProjectionThreadMessageRepositoryShape - Service API for projected thread messages.
45 + */
46 export interface ProjectionThreadMessageRepositoryShape {
47 + /**
48 + * Insert or replace a projected thread message row.
49 + *
50 + * Upserts by `messageId`.
51 + */
52 readonly upsert: (
53 message: ProjectionThreadMessage,
54 ) => Effect.Effect<void, ProjectionRepositoryError>;
55
56 + /**
57 + * List projected thread messages for a thread.
58 + *
59 + * Returned in ascending creation order.
60 + */
61 readonly listByThreadId: (
62 input: ListProjectionThreadMessagesInput,
63 ) => Effect.Effect<ReadonlyArray<ProjectionThreadMessage>, ProjectionRepositoryError>;
64
65 + /**
66 + * Delete projected thread messages by thread.
67 + */
68 readonly deleteByThreadId: (
69 input: DeleteProjectionThreadMessagesInput,
70 ) => Effect.Effect<void, ProjectionRepositoryError>;
71 }
72
73 +/**
74 + * ProjectionThreadMessageRepository - Service tag for message projection persistence.
75 + */
76 export class ProjectionThreadMessageRepository extends ServiceMap.Service<
77 ProjectionThreadMessageRepository,
78 ProjectionThreadMessageRepositoryShape
apps/server/src/persistence/Services/ProjectionThreadSessions.ts
+25
@@ -1,3 +1,11 @@
1 +/**
2 + * ProjectionThreadSessionRepository - Repository interface for thread sessions.
3 + *
4 + * Owns persistence operations for projected provider-session linkage and
5 + * runtime status for each thread.
6 + *
7 + * @module ProjectionThreadSessionRepository
8 + */
9 import {
10 IsoDateTime,
11 OrchestrationSessionStatus,
@@ -33,18 +41,35 @@ export const DeleteProjectionThreadSessionInput = Schema.Struct({
41 });
42 export type DeleteProjectionThreadSessionInput = typeof DeleteProjectionThreadSessionInput.Type;
43
44 +/**
45 + * ProjectionThreadSessionRepositoryShape - Service API for projected thread sessions.
46 + */
47 export interface ProjectionThreadSessionRepositoryShape {
48 + /**
49 + * Insert or replace a projected thread-session row.
50 + *
51 + * Upserts by `threadId`.
52 + */
53 readonly upsert: (row: ProjectionThreadSession) => Effect.Effect<void, ProjectionRepositoryError>;
54
55 + /**
56 + * Read projected thread-session state by thread id.
57 + */
58 readonly getByThreadId: (
59 input: GetProjectionThreadSessionInput,
60 ) => Effect.Effect<Option.Option<ProjectionThreadSession>, ProjectionRepositoryError>;
61
62 + /**
63 + * Delete projected thread-session state by thread id.
64 + */
65 readonly deleteByThreadId: (
66 input: DeleteProjectionThreadSessionInput,
67 ) => Effect.Effect<void, ProjectionRepositoryError>;
68 }
69
70 +/**
71 + * ProjectionThreadSessionRepository - Service tag for thread-session persistence.
72 + */
73 export class ProjectionThreadSessionRepository extends ServiceMap.Service<
74 ProjectionThreadSessionRepository,
75 ProjectionThreadSessionRepositoryShape
apps/server/src/persistence/Services/ProjectionThreadTurns.ts
+29
@@ -1,3 +1,10 @@
1 +/**
2 + * ProjectionThreadTurnRepository - Projection repository interface for turns.
3 + *
4 + * Owns persistence operations for projected turn records in thread timelines.
5 + *
6 + * @module ProjectionThreadTurnRepository
7 + */
8 import {
9 IsoDateTime,
10 MessageId,
@@ -38,22 +45,44 @@ export const DeleteProjectionThreadTurnsInput = Schema.Struct({
45 });
46 export type DeleteProjectionThreadTurnsInput = typeof DeleteProjectionThreadTurnsInput.Type;
47
48 +/**
49 + * ProjectionThreadTurnRepositoryShape - Service API for projected thread turns.
50 + */
51 export interface ProjectionThreadTurnRepositoryShape {
52 + /**
53 + * Insert or replace a projected turn row.
54 + *
55 + * Upserts by `turnId`.
56 + */
57 readonly upsert: (turn: ProjectionThreadTurn) => Effect.Effect<void, ProjectionRepositoryError>;
58
59 + /**
60 + * List projected turns for a thread.
61 + *
62 + * Returned in ascending `turnCount` order.
63 + */
64 readonly listByThreadId: (
65 input: ListProjectionThreadTurnsInput,
66 ) => Effect.Effect<ReadonlyArray<ProjectionThreadTurn>, ProjectionRepositoryError>;
67
68 + /**
69 + * Read a projected turn row by turn id.
70 + */
71 readonly getByTurnId: (
72 input: GetProjectionThreadTurnInput,
73 ) => Effect.Effect<Option.Option<ProjectionThreadTurn>, ProjectionRepositoryError>;
74
75 + /**
76 + * Delete projected turn rows by thread.
77 + */
78 readonly deleteByThreadId: (
79 input: DeleteProjectionThreadTurnsInput,
80 ) => Effect.Effect<void, ProjectionRepositoryError>;
81 }
82
83 +/**
84 + * ProjectionThreadTurnRepository - Service tag for turn projection persistence.
85 + */
86 export class ProjectionThreadTurnRepository extends ServiceMap.Service<
87 ProjectionThreadTurnRepository,
88 ProjectionThreadTurnRepositoryShape
apps/server/src/persistence/Services/ProjectionThreads.ts
+30
@@ -1,3 +1,11 @@
1 +/**
2 + * ProjectionThreadRepository - Projection repository interface for threads.
3 + *
4 + * Owns persistence operations for projected thread records in the
5 + * orchestration read model.
6 + *
7 + * @module ProjectionThreadRepository
8 + */
9 import { IsoDateTime, ProjectId, ThreadId, TurnId } from "@t3tools/contracts";
10 import { Option, Schema, ServiceMap } from "effect";
11 import type { Effect } from "effect";
@@ -33,22 +41,44 @@ export const ListProjectionThreadsByProjectInput = Schema.Struct({
41 });
42 export type ListProjectionThreadsByProjectInput = typeof ListProjectionThreadsByProjectInput.Type;
43
44 +/**
45 + * ProjectionThreadRepositoryShape - Service API for projected thread records.
46 + */
47 export interface ProjectionThreadRepositoryShape {
48 + /**
49 + * Insert or replace a projected thread row.
50 + *
51 + * Upserts by `threadId`.
52 + */
53 readonly upsert: (thread: ProjectionThread) => Effect.Effect<void, ProjectionRepositoryError>;
54
55 + /**
56 + * Read a projected thread row by id.
57 + */
58 readonly getById: (
59 input: GetProjectionThreadInput,
60 ) => Effect.Effect<Option.Option<ProjectionThread>, ProjectionRepositoryError>;
61
62 + /**
63 + * List projected threads for a project.
64 + *
65 + * Returned in deterministic creation order.
66 + */
67 readonly listByProjectId: (
68 input: ListProjectionThreadsByProjectInput,
69 ) => Effect.Effect<ReadonlyArray<ProjectionThread>, ProjectionRepositoryError>;
70
71 + /**
72 + * Soft-delete a projected thread row by id.
73 + */
74 readonly deleteById: (
75 input: DeleteProjectionThreadInput,
76 ) => Effect.Effect<void, ProjectionRepositoryError>;
77 }
78
79 +/**
80 + * ProjectionThreadRepository - Service tag for thread projection persistence.
81 + */
82 export class ProjectionThreadRepository extends ServiceMap.Service<
83 ProjectionThreadRepository,
84 ProjectionThreadRepositoryShape
apps/server/src/persistence/Services/ProviderSessionRuntime.ts
+29
@@ -1,3 +1,10 @@
1 +/**
2 + * ProviderSessionRuntimeRepository - Repository interface for provider runtime sessions.
3 + *
4 + * Owns persistence operations for provider runtime metadata and resume cursors.
5 + *
6 + * @module ProviderSessionRuntimeRepository
7 + */
8 import {
9 IsoDateTime,
10 ProviderSessionId,
@@ -33,25 +40,47 @@ export const DeleteProviderSessionRuntimeInput = Schema.Struct({
40 });
41 export type DeleteProviderSessionRuntimeInput = typeof DeleteProviderSessionRuntimeInput.Type;
42
43 +/**
44 + * ProviderSessionRuntimeRepositoryShape - Service API for provider runtime records.
45 + */
46 export interface ProviderSessionRuntimeRepositoryShape {
47 + /**
48 + * Insert or replace a provider runtime row.
49 + *
50 + * Upserts by `providerSessionId`, including JSON payload/cursor fields.
51 + */
52 readonly upsert: (
53 runtime: ProviderSessionRuntime,
54 ) => Effect.Effect<void, ProviderSessionRuntimeRepositoryError>;
55
56 + /**
57 + * Read provider runtime state by provider session id.
58 + */
59 readonly getBySessionId: (
60 input: GetProviderSessionRuntimeInput,
61 ) => Effect.Effect<Option.Option<ProviderSessionRuntime>, ProviderSessionRuntimeRepositoryError>;
62
63 + /**
64 + * List all provider runtime rows.
65 + *
66 + * Returned in ascending last-seen order.
67 + */
68 readonly list: () => Effect.Effect<
69 ReadonlyArray<ProviderSessionRuntime>,
70 ProviderSessionRuntimeRepositoryError
71 >;
72
73 + /**
74 + * Delete provider runtime state by provider session id.
75 + */
76 readonly deleteBySessionId: (
77 input: DeleteProviderSessionRuntimeInput,
78 ) => Effect.Effect<void, ProviderSessionRuntimeRepositoryError>;
79 }
80
81 +/**
82 + * ProviderSessionRuntimeRepository - Service tag for provider runtime persistence.
83 + */
84 export class ProviderSessionRuntimeRepository extends ServiceMap.Service<
85 ProviderSessionRuntimeRepository,
86 ProviderSessionRuntimeRepositoryShape
apps/server/src/persistence/Services/ProviderSessions.ts
+30
@@ -1,3 +1,11 @@
1 +/**
2 + * ProviderSessionRepository - Repository interface for provider session lookup.
3 + *
4 + * Owns persistence operations that map internal sessions to provider kinds and
5 + * optional thread ownership.
6 + *
7 + * @module ProviderSessionRepository
8 + */
9 import type { ProviderKind, ProviderSessionId, ThreadId } from "@t3tools/contracts";
10 import { Option, ServiceMap } from "effect";
11 import type { Effect } from "effect";
@@ -26,25 +34,47 @@ export interface GetProviderSessionInput {
34 readonly sessionId: ProviderSessionId;
35 }
36
37 +/**
38 + * ProviderSessionRepositoryShape - Service API for provider-session records.
39 + */
40 export interface ProviderSessionRepositoryShape {
41 + /**
42 + * Insert or replace a provider-session row.
43 + *
44 + * Upserts by `sessionId`.
45 + */
46 readonly upsertSession: (
47 input: UpsertProviderSessionInput,
48 ) => Effect.Effect<void, ProviderSessionRepositoryError>;
49
50 + /**
51 + * Read a provider-session row by session id.
52 + */
53 readonly getSession: (
54 input: GetProviderSessionInput,
55 ) => Effect.Effect<Option.Option<ProviderSessionEntry>, ProviderSessionRepositoryError>;
56
57 + /**
58 + * List all provider-session rows.
59 + *
60 + * Returned in deterministic creation order.
61 + */
62 readonly listSessions: () => Effect.Effect<
63 ReadonlyArray<ProviderSessionEntry>,
64 ProviderSessionRepositoryError
65 >;
66
67 + /**
68 + * Delete a provider-session row by session id.
69 + */
70 readonly deleteSession: (
71 input: DeleteProviderSessionInput,
72 ) => Effect.Effect<void, ProviderSessionRepositoryError>;
73 }
74
75 +/**
76 + * ProviderSessionRepository - Service tag for provider-session persistence.
77 + */
78 export class ProviderSessionRepository extends ServiceMap.Service<
79 ProviderSessionRepository,
80 ProviderSessionRepositoryShape
apps/server/src/provider/Errors.ts
-71
@@ -51,23 +51,6 @@ export class ProviderAdapterSessionClosedError extends Schema.TaggedErrorClass<P
51 }
52 }
53
54 -/**
55 - * ProviderAdapterProtocolError - Invalid/unexpected provider protocol payload.
56 - */
57 -export class ProviderAdapterProtocolError extends Schema.TaggedErrorClass<ProviderAdapterProtocolError>()(
58 - "ProviderAdapterProtocolError",
59 - {
60 - provider: Schema.String,
61 - operation: Schema.String,
62 - detail: Schema.String,
63 - cause: Schema.optional(Schema.Defect),
64 - },
65 -) {
66 - override get message(): string {
67 - return `Provider adapter protocol error (${this.provider}) in ${this.operation}: ${this.detail}`;
68 - }
69 -}
70 -
54 /**
55 * ProviderAdapterRequestError - Provider protocol request failed or timed out.
56 */
@@ -164,61 +147,10 @@ export class ProviderSessionDirectoryPersistenceError extends Schema.TaggedError
147 }
148 }
149
167 -/**
168 - * ProviderCheckpointUnavailableError - Checkpointing unavailable for this session.
169 - */
170 -export class ProviderCheckpointUnavailableError extends Schema.TaggedErrorClass<ProviderCheckpointUnavailableError>()(
171 - "ProviderCheckpointUnavailableError",
172 - {
173 - sessionId: Schema.String,
174 - detail: Schema.String,
175 - cause: Schema.optional(Schema.Defect),
176 - },
177 -) {
178 - override get message(): string {
179 - return `Provider checkpoint unavailable for session ${this.sessionId}: ${this.detail}`;
180 - }
181 -}
182 -
183 -/**
184 - * ProviderCheckpointRangeError - Requested checkpoint range is invalid.
185 - */
186 -export class ProviderCheckpointRangeError extends Schema.TaggedErrorClass<ProviderCheckpointRangeError>()(
187 - "ProviderCheckpointRangeError",
188 - {
189 - sessionId: Schema.String,
190 - fromTurnCount: Schema.Number,
191 - toTurnCount: Schema.Number,
192 - detail: Schema.String,
193 - cause: Schema.optional(Schema.Defect),
194 - },
195 -) {
196 - override get message(): string {
197 - return `Provider checkpoint range error for session ${this.sessionId}: ${this.fromTurnCount}..${this.toTurnCount} (${this.detail})`;
198 - }
199 -}
200 -
201 -/**
202 - * ProviderFilesystemError - Filesystem checkpoint capture/restore failure.
203 - */
204 -export class ProviderFilesystemError extends Schema.TaggedErrorClass<ProviderFilesystemError>()(
205 - "ProviderFilesystemError",
206 - {
207 - sessionId: Schema.String,
208 - detail: Schema.String,
209 - cause: Schema.optional(Schema.Defect),
210 - },
211 -) {
212 - override get message(): string {
213 - return `Provider filesystem checkpoint error for session ${this.sessionId}: ${this.detail}`;
214 - }
215 -}
216 -
150 export type ProviderAdapterError =
151 | ProviderAdapterValidationError
152 | ProviderAdapterSessionNotFoundError
153 | ProviderAdapterSessionClosedError
221 - | ProviderAdapterProtocolError
154 | ProviderAdapterRequestError
155 | ProviderAdapterProcessError;
156
@@ -227,8 +159,5 @@ export type ProviderServiceError =
159 | ProviderUnsupportedError
160 | ProviderSessionNotFoundError
161 | ProviderSessionDirectoryPersistenceError
230 - | ProviderCheckpointUnavailableError
231 - | ProviderCheckpointRangeError
232 - | ProviderFilesystemError
162 | ProviderAdapterError
163 | CheckpointServiceError;
apps/server/src/provider/Services/CodexAdapter.ts
+3
@@ -15,6 +15,9 @@ import { ServiceMap } from "effect";
15 import type { ProviderAdapterError } from "../Errors.ts";
16 import type { ProviderAdapterShape } from "./ProviderAdapter.ts";
17
18 +/**
19 + * CodexAdapterShape - Service API for the Codex provider adapter.
20 + */
21 export interface CodexAdapterShape extends ProviderAdapterShape<ProviderAdapterError> {
22 readonly provider: "codex";
23 }
apps/server/src/provider/Services/ProviderAdapterRegistry.ts
+3
@@ -14,6 +14,9 @@ import type { Effect } from "effect";
14 import type { ProviderAdapterError, ProviderUnsupportedError } from "../Errors.ts";
15 import type { ProviderAdapterShape } from "./ProviderAdapter.ts";
16
17 +/**
18 + * ProviderAdapterRegistryShape - Service API for adapter lookup by provider kind.
19 + */
20 export interface ProviderAdapterRegistryShape {
21 /**
22 * Resolve the adapter for a provider kind.
apps/server/src/provider/Services/ProviderService.ts
+7
@@ -28,6 +28,9 @@ import type { Effect, Stream } from "effect";
28
29 import type { ProviderServiceError } from "../Errors.ts";
30
31 +/**
32 + * ProviderServiceShape - Service API for provider session and turn orchestration.
33 + */
34 export interface ProviderServiceShape {
35 /**
36 * Start a provider session.
@@ -67,6 +70,8 @@ export interface ProviderServiceShape {
70
71 /**
72 * List active provider sessions.
73 + *
74 + * Aggregates runtime session lists from all registered adapters.
75 */
76 readonly listSessions: () => Effect.Effect<ReadonlyArray<ProviderSession>>;
77
@@ -83,6 +88,8 @@ export interface ProviderServiceShape {
88 *
89 * Persisted session bindings remain so stale sessions can be resumed after
90 * process restart.
91 + *
92 + * Runtime alias mappings are cleared as part of shutdown.
93 */
94 readonly stopAll: () => Effect.Effect<void, ProviderServiceError>;
95
apps/server/src/provider/Services/ProviderSessionDirectory.ts
+6
@@ -42,9 +42,15 @@ export type ProviderSessionDirectoryWriteError =
42 | ProviderValidationError
43 | ProviderSessionDirectoryPersistenceError;
44
45 +/**
46 + * ProviderSessionDirectoryShape - Service API for provider session ownership metadata.
47 + */
48 export interface ProviderSessionDirectoryShape {
49 /**
50 * Record or update ownership for one provider session.
51 + *
52 + * Preserves existing persisted fields when omitted and shallow-merges
53 + * runtime payload objects.
54 */
55 readonly upsert: (
56 binding: ProviderSessionBinding,
apps/server/src/terminal/Services/Manager.ts
+52
@@ -1,3 +1,11 @@
1 +/**
2 + * TerminalManager - Terminal session orchestration service interface.
3 + *
4 + * Owns terminal lifecycle operations, output fanout, and session state
5 + * transitions for thread-scoped terminals.
6 + *
7 + * @module TerminalManager
8 + */
9 import {
10 TerminalClearInput,
11 TerminalCloseInput,
@@ -40,17 +48,61 @@ export interface TerminalStartInput extends TerminalOpenInput {
48 rows: number;
49 }
50
51 +/**
52 + * TerminalManagerShape - Service API for terminal session lifecycle operations.
53 + */
54 export interface TerminalManagerShape {
55 + /**
56 + * Open or attach to a terminal session.
57 + *
58 + * Reuses an existing session for the same thread/terminal id and restores
59 + * persisted history on first open.
60 + */
61 readonly open: (input: TerminalOpenInput) => Effect.Effect<TerminalSessionSnapshot, unknown>;
62 +
63 + /**
64 + * Write input bytes to a terminal session.
65 + */
66 readonly write: (input: TerminalWriteInput) => Effect.Effect<void, unknown>;
67 +
68 + /**
69 + * Resize the PTY backing a terminal session.
70 + */
71 readonly resize: (input: TerminalResizeInput) => Effect.Effect<void, unknown>;
72 +
73 + /**
74 + * Clear terminal output history.
75 + */
76 readonly clear: (input: TerminalClearInput) => Effect.Effect<void, unknown>;
77 +
78 + /**
79 + * Restart a terminal session in place.
80 + *
81 + * Always resets history before spawning the new process.
82 + */
83 readonly restart: (input: TerminalOpenInput) => Effect.Effect<TerminalSessionSnapshot, unknown>;
84 +
85 + /**
86 + * Close an active terminal session.
87 + *
88 + * When `terminalId` is omitted, closes all sessions for the thread.
89 + */
90 readonly close: (input: TerminalCloseInput) => Effect.Effect<void, unknown>;
91 +
92 + /**
93 + * Subscribe to terminal runtime events.
94 + */
95 readonly subscribe: (listener: (event: TerminalEvent) => void) => Effect.Effect<() => void>;
96 +
97 + /**
98 + * Dispose all managed terminal resources.
99 + */
100 readonly dispose: () => Effect.Effect<void>;
101 }
102
103 +/**
104 + * TerminalManager - Service tag for terminal session orchestration.
105 + */
106 export class TerminalManager extends ServiceMap.Service<TerminalManager, TerminalManagerShape>()(
107 "terminal/TerminalManager",
108 ) {}
apps/server/src/terminal/Services/PTY.ts
+17
@@ -1,3 +1,11 @@
1 +/**
2 + * PtyAdapter - Terminal PTY adapter service contract.
3 + *
4 + * Defines the process primitives required by terminal session management
5 + * without binding to a specific PTY implementation.
6 + *
7 + * @module PtyAdapter
8 + */
9 import { ServiceMap } from "effect";
10
11 export interface PtyExitEvent {
@@ -23,10 +31,19 @@ export interface PtySpawnInput {
31 env: NodeJS.ProcessEnv;
32 }
33
34 +/**
35 + * PtyAdapterShape - Service API for spawning and controlling PTY processes.
36 + */
37 export interface PtyAdapterShape {
38 + /**
39 + * Spawn a PTY process for a terminal session.
40 + */
41 spawn(input: PtySpawnInput): PtyProcess;
42 }
43
44 +/**
45 + * PtyAdapter - Service tag for PTY process integration.
46 + */
47 export class PtyAdapter extends ServiceMap.Service<PtyAdapter, PtyAdapterShape>()(
48 "terminal/PtyAdapter",
49 ) {}
apps/server/src/wsServer.ts
+21
@@ -1,3 +1,11 @@
1 +/**
2 + * Server - HTTP/WebSocket server service interface.
3 + *
4 + * Owns startup and shutdown lifecycle of the HTTP server, static asset serving,
5 + * and WebSocket request routing.
6 + *
7 + * @module Server
8 + */
9 import fs from "node:fs";
10 import http from "node:http";
11 import path from "node:path";
@@ -49,15 +57,28 @@ const MIME_TYPES: Record<string, string> = {
57 ".map": "application/json",
58 };
59
60 +/**
61 + * ServerShape - Service API for server lifecycle control.
62 + */
63 export interface ServerShape {
64 + /**
65 + * Start HTTP and WebSocket listeners.
66 + */
67 readonly start: Effect.Effect<
68 http.Server,
69 unknown,
70 Scope.Scope | ServerRuntimeServices | ServerConfig
71 >;
72 +
73 + /**
74 + * Wait for process shutdown signals.
75 + */
76 readonly stopSignal: Effect.Effect<unknown, never>;
77 }
78
79 +/**
80 + * Server - Service tag for HTTP/WebSocket lifecycle management.
81 + */
82 export class Server extends ServiceMap.Service<Server, ServerShape>()("server/Server") {}
83
84 const isServerNotRunningError = (error: unknown): boolean => {