Overview
T3 Code server uses SQLite with Effect SQL for durable persistence, implementing event sourcing with CQRS projections for orchestration state.Database Layer
SQLite Persistence
The server uses SQLite with WAL mode and foreign key constraints enabled. Source:apps/server/src/persistence/Layers/Sqlite.ts
Configuration
Database Setup
On initialization, the following pragmas are applied:PRAGMA journal_mode = WAL: Write-Ahead Logging for better concurrencyPRAGMA foreign_keys = ON: Enforce referential integrity
runMigrations.
makeSqlitePersistenceLive
Source:apps/server/src/persistence/Layers/Sqlite.ts:38
Create a SQLite persistence layer with the specified database path.
dbPath: Absolute path to SQLite database file
Layer<SqlClient.SqlClient>
layerConfig
Source:apps/server/src/persistence/Layers/Sqlite.ts:52
Production persistence layer that reads database path from ServerConfig.stateDir.
ServerConfig, Path.Path
Provides: SqlClient.SqlClient
SqlitePersistenceMemory
Source:apps/server/src/persistence/Layers/Sqlite.ts:47
In-memory SQLite layer for testing. Data is lost when the process exits.
SqlClient.SqlClient
Runtime Detection
The persistence layer automatically detects the runtime environment:- Bun: Uses
@effect/sql-sqlite-bun - Node.js: Uses custom Node SQLite client
Event Store
OrchestrationEventStore
Durable append-only event store for orchestration events. Service Tag:t3/persistence/Services/OrchestrationEventStore
Source: apps/server/src/persistence/Services/OrchestrationEventStore.ts:67
Methods
(event: Omit<OrchestrationEvent, 'sequence'>) => Effect<OrchestrationEvent, OrchestrationEventStoreError>
Persist a new orchestration eventActor kind is inferred from command/metadata before persistence. Sequence number is assigned by the storage layer.Parameters:
event: Event payload without sequence number
OrchestrationEventStoreError on database failures(sequenceExclusive: number, limit?: number) => Stream<OrchestrationEvent, OrchestrationEventStoreError>
Replay events after the provided sequenceReads in fixed-size pages and normalizes non-integer/negative limits.Parameters:
sequenceExclusive: Sequence cursor (exclusive start)limit: Maximum number of events to emit (optional)
OrchestrationEventStoreError on database failures() => Stream<OrchestrationEvent, OrchestrationEventStoreError>
Read all events from the beginning of the streamReturns: Stream of all stored events in orderErrors:
OrchestrationEventStoreError on database failuresExample Usage
Projection Repositories
Projection repositories maintain denormalized read models derived from orchestration events.ProjectionThreadRepository
Persistence for projected thread records. Service Tag:t3/persistence/Services/ProjectionThreads/ProjectionThreadRepository
Source: apps/server/src/persistence/Services/ProjectionThreads.ts:91
ProjectionThread Schema
ThreadId
required
Unique thread identifier
ProjectId
required
Parent project identifier
string
required
Thread title
string
required
Active model name (e.g.,
"o3-mini", "claude-3.5-sonnet")RuntimeMode
required
Runtime mode:
"editor" or "agent"ProviderInteractionMode
required
Provider interaction mode:
"blocking" or "streaming"string | null
Git branch name if applicable
string | null
Git worktree path if applicable
TurnId | null
ID of the most recent turn
IsoDateTime
required
Thread creation timestamp
IsoDateTime
required
Last update timestamp
IsoDateTime | null
Soft deletion timestamp
Methods
(thread: ProjectionThread) => Effect<void, ProjectionRepositoryError>
Insert or replace a projected thread rowUpserts by
threadId.(input: { threadId: ThreadId }) => Effect<Option<ProjectionThread>, ProjectionRepositoryError>
Read a projected thread row by IDReturns:
Option.Some(thread) if found, Option.None otherwise(input: { projectId: ProjectId }) => Effect<ReadonlyArray<ProjectionThread>, ProjectionRepositoryError>
List projected threads for a projectReturned in deterministic creation order.
(input: { threadId: ThreadId }) => Effect<void, ProjectionRepositoryError>
Soft-delete a projected thread row by IDSets
deletedAt timestamp without removing the row.ProjectionProjectRepository
Persistence for projected project records. Service Tag:t3/persistence/Services/ProjectionProjects/ProjectionProjectRepository
Source: apps/server/src/persistence/Services/ProjectionProjects.ts:76
ProjectionProject Schema
ProjectId
required
Unique project identifier
string
required
Project title
string
required
Absolute path to workspace root directory
string | null
Default model for new threads
Array<ProjectScript>
required
Project scripts for task automationPersisted as JSON.
IsoDateTime
required
Project creation timestamp
IsoDateTime
required
Last update timestamp
IsoDateTime | null
Soft deletion timestamp
Methods
(project: ProjectionProject) => Effect<void, ProjectionRepositoryError>
Insert or replace a projected project rowUpserts by
projectId and persists scripts through JSON encoding.(input: { projectId: ProjectId }) => Effect<Option<ProjectionProject>, ProjectionRepositoryError>
Read a projected project row by IDReturns:
Option.Some(project) if found, Option.None otherwise() => Effect<ReadonlyArray<ProjectionProject>, ProjectionRepositoryError>
List all projected project rowsReturned in deterministic creation order.
(input: { projectId: ProjectId }) => Effect<void, ProjectionRepositoryError>
Soft-delete a projected project row by IDSets
deletedAt timestamp without removing the row.Additional Projection Repositories
The persistence layer includes additional projection repositories:- ProjectionTurnRepository: Turn records with status and timestamps
- ProjectionThreadMessageRepository: Thread messages with attachments
- ProjectionThreadActivityRepository: Thread activity logs
- ProjectionThreadSessionRepository: Provider session records
- ProjectionPendingApprovalRepository: Pending approval requests
- ProjectionCheckpointRepository: Checkpoint snapshots
- ProjectionThreadProposedPlanRepository: Proposed plan records
- ProjectionStateRepository: Projection cursor state
apps/server/src/persistence/Services/ for detailed interfaces.
Command Receipts
OrchestrationCommandReceiptRepository
Persistence for command deduplication receipts. Service Tag:t3/persistence/Services/OrchestrationCommandReceipts
Source: apps/server/src/persistence/Services/OrchestrationCommandReceipts.ts
Used by the orchestration engine to deduplicate command dispatch through idempotency tracking.
Provider Session Runtime
ProviderSessionRuntimeRepository
Persistence for active provider session runtime state. Service Tag:t3/persistence/Services/ProviderSessionRuntime
Source: apps/server/src/persistence/Services/ProviderSessionRuntime.ts
Stores session metadata required for session resumption and directory lookups.
Error Types
OrchestrationEventStoreError
Database or decoding errors from event store operations. Source:apps/server/src/persistence/Errors.ts
ProjectionRepositoryError
Database or schema errors from projection repository operations. Source:apps/server/src/persistence/Errors.ts
Migrations
Database schema migrations are managed via Effect and executed automatically on startup. Source:apps/server/src/persistence/Migrations.ts
Migration files are located in apps/server/src/persistence/Migrations/ and numbered sequentially:
001_OrchestrationEvents.ts: Event store table002_OrchestrationCommandReceipts.ts: Command receipt tracking003_CheckpointDiffBlobs.ts: Checkpoint diff storage004_ProviderSessionRuntime.ts: Provider session metadata005_Projections.ts: Core projection tables- And more…
