| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464 |
- import { describe, expect, test } from "bun:test"
- import { $ } from "bun"
- import { fileURLToPath } from "url"
- import path from "path"
- import { SqliteClient } from "@effect/sql-sqlite-bun"
- import { EffectDrizzleSqlite } from "@opencode-ai/effect-drizzle-sqlite"
- import { Effect, Layer, Schema } from "effect"
- import { eq, inArray, sql } from "drizzle-orm"
- import { DatabaseMigration } from "@opencode-ai/core/database/migration"
- import { migrations } from "@opencode-ai/core/database/migration.gen"
- import sessionUsageMigration from "@opencode-ai/core/database/migration/20260510033149_session_usage"
- import normalizeStoragePathsMigration from "@opencode-ai/core/database/migration/20260601010001_normalize_storage_paths"
- import sessionMessageProjectionOrderMigration from "@opencode-ai/core/database/migration/20260603040000_session_message_projection_order"
- import eventSourcedSessionPendingMigration from "@opencode-ai/core/database/migration/20260604172448_event_sourced_session_input"
- import contextEpochAgentMigration from "@opencode-ai/core/database/migration/20260605042240_add_context_epoch_agent"
- import simplifyIntegrationCredentialsMigration from "@opencode-ai/core/database/migration/20260611192811_lush_chimera"
- import simplifySessionPendingMigration from "@opencode-ai/core/database/migration/20260622202450_simplify_session_input"
- import resetSessionEventsMigration from "@opencode-ai/core/database/migration/20260703200000_reset_v2_session_events"
- import durableSessionInboxMigration from "@opencode-ai/core/database/migration/20260707010146_durable_session_inbox"
- import migratePrelaunchV2StateMigration from "@opencode-ai/core/database/migration/20260707120000_migrate_prelaunch_v2_state"
- import genericSessionPendingMigration from "@opencode-ai/core/database/migration/20260709013000_generic_session_input"
- import sessionPendingTableMigration from "@opencode-ai/core/database/migration/20260709190621_session_pending_table"
- import renameInstructionsMigration from "@opencode-ai/core/database/migration/20260705180000_rename_instructions"
- import addSessionForkMigration from "@opencode-ai/core/database/migration/20260706223930_add-session-fork"
- import timeSuspendedMigration from "@opencode-ai/core/database/migration/20260709163752_time_suspended"
- import instructionSyncMigration from "@opencode-ai/core/database/migration/20260710025429_instruction_sync"
- import deleteToolProgressEventsMigration from "@opencode-ai/core/database/migration/20260722011141_delete_tool_progress_events"
- import canonicalToolResultsMigration from "@opencode-ai/core/database/migration/20260722170000_canonical_tool_results"
- import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
- import { LayerNode } from "@opencode-ai/util/effect/layer-node"
- import { Bus } from "@opencode-ai/core/bus"
- import { Project } from "@opencode-ai/core/project"
- import { ProjectTable } from "@opencode-ai/core/project/sql"
- import { AbsolutePath } from "@opencode-ai/core/schema"
- import { SessionSchema } from "@opencode-ai/core/session/schema"
- import { SessionMessage } from "@opencode-ai/core/session/message"
- import { SessionTable } from "@opencode-ai/core/session/sql"
- import sessionMetadataMigration from "@opencode-ai/core/database/migration/20260511173437_session-metadata"
- import type { SqlClient as SqlClientService } from "effect/unstable/sql/SqlClient"
- import { Database } from "@opencode-ai/core/database/database"
- import { SessionProjector } from "@opencode-ai/core/session/projector"
- import { SessionV1 } from "@opencode-ai/core/v1/session"
- import { tmpdir } from "./fixture/tmpdir"
- const run = <A, E>(effect: Effect.Effect<A, E, SqlClientService>) =>
- Effect.runPromise(
- effect.pipe(Effect.provide(SqliteClient.layer({ filename: ":memory:", disableWAL: true })), Effect.scoped),
- )
- const makeDb = EffectDrizzleSqlite.makeWithDefaults()
- describe("DatabaseMigration", () => {
- test("migrates pre-launch V2 state in place", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(
- sql`CREATE TABLE session_message (id text PRIMARY KEY, session_id text NOT NULL, type text NOT NULL, seq integer NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(
- sql`CREATE TABLE session_input (id text PRIMARY KEY, session_id text NOT NULL, type text NOT NULL, prompt text, delivery text, admitted_seq integer NOT NULL, promoted_seq integer, time_created integer NOT NULL)`,
- )
- yield* db.run(
- sql`CREATE TABLE event (id text PRIMARY KEY, aggregate_id text NOT NULL, seq integer NOT NULL, created integer NOT NULL, type text NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(
- sql`CREATE TABLE event_sequence (aggregate_id text PRIMARY KEY, seq integer NOT NULL, owner_id text)`,
- )
- yield* db.run(
- sql`CREATE TABLE instruction_checkpoint (session_id text PRIMARY KEY, baseline text NOT NULL, snapshot text NOT NULL, baseline_seq integer NOT NULL)`,
- )
- const messages = [
- ["msg_skill", "skill", { name: "effect", text: "Use Effect", time: { created: 1 } }],
- [
- "msg_shell",
- "shell",
- {
- shell: { id: "sh_old", command: "pwd", status: "exited", exit: 0, cwd: "/tmp" },
- output: { output: "/tmp", cursor: 4, size: 4, truncated: false },
- time: { created: 2, completed: 3 },
- },
- ],
- [
- "msg_assistant",
- "assistant",
- {
- agent: "build",
- model: { id: "model", providerID: "provider" },
- content: [
- {
- type: "tool",
- id: "call_old",
- name: "read",
- provider: "removed",
- state: { status: "pending", input: '{"path":"README.md"}', title: "removed" },
- time: { created: 3 },
- },
- ],
- time: { created: 3 },
- },
- ],
- [
- "msg_failed",
- "compaction",
- {
- status: "failed",
- reason: "manual",
- summary: "removed",
- recent: "removed",
- time: { created: 4 },
- },
- ],
- [
- "msg_queued",
- "compaction",
- { status: "queued", reason: "manual", summary: "", recent: "", time: { created: 5 } },
- ],
- [
- "msg_synthetic",
- "synthetic",
- { sessionID: "ses_test", text: "context", description: "source", time: { created: 6 } },
- ],
- [
- "msg_running",
- "compaction",
- { status: "running", reason: "auto", summary: "partial", recent: "recent", time: { created: 7 } },
- ],
- [
- "msg_completed",
- "compaction",
- { status: "completed", reason: "auto", summary: "summary", recent: "recent", time: { created: 8 } },
- ],
- ] as const
- for (const [id, type, data] of messages)
- yield* db.run(
- sql`INSERT INTO session_message VALUES (${id}, 'ses_test', ${type}, 1, 10, 11, ${JSON.stringify(data)})`,
- )
- yield* db.run(
- sql`INSERT INTO session_input VALUES ('msg_queued', 'ses_test', 'compaction', NULL, NULL, 4, NULL, 5)`,
- )
- yield* db.run(sql`INSERT INTO event_sequence VALUES ('ses_test', 9, 'owner')`)
- yield* db.run(sql`INSERT INTO instruction_checkpoint VALUES ('ses_test', 'baseline', '{"source":"value"}', 7)`)
- const events = [
- ["evt_skill", 1, 101, "session.skill.activated.1", { sessionID: "ses_test", name: "effect", text: "Use" }],
- ["evt_started", 2, 102, "session.compaction.started.1", { sessionID: "ses_test", reason: "auto" }],
- ["evt_delta", 3, 103, "session.compaction.delta.1", { sessionID: "ses_test", text: "partial" }],
- ["evt_failed", 4, 104, "session.compaction.failed.1", { sessionID: "ses_test" }],
- [
- "evt_revert",
- 5,
- 105,
- "session.revert.staged.1",
- {
- sessionID: "ses_test",
- revert: {
- messageID: "msg_skill",
- snapshot: "tree",
- diff: "removed",
- files: [{ path: "src/a.ts", patch: "@@", additions: 1, deletions: 0, status: "modified" }],
- },
- },
- ],
- [
- "evt_skill_current",
- 6,
- 106,
- "session.skill.activated.2",
- { sessionID: "ses_test", id: "effect-id", name: "Effect", text: "Use" },
- ],
- ] as const
- for (const [id, seq, created, type, data] of events)
- yield* db.run(
- sql`INSERT INTO event VALUES (${id}, 'ses_test', ${seq}, ${created}, ${type}, ${JSON.stringify(data)})`,
- )
- yield* DatabaseMigration.applyOnly(db, [migratePrelaunchV2StateMigration])
- const rows = yield* db.all<{
- id: string
- type: string
- seq: number
- time_created: number
- time_updated: number
- data: string
- }>(sql`SELECT id, type, seq, time_created, time_updated, data FROM session_message ORDER BY id`)
- for (const row of rows)
- Schema.decodeUnknownSync(SessionMessage.Info)({ ...JSON.parse(row.data), id: row.id, type: row.type })
- expect(rows.every((row) => row.seq === 1 && row.time_created === 10 && row.time_updated === 11)).toBe(true)
- expect(rows.map((row) => [row.id, JSON.parse(row.data)])).toEqual([
- [
- "msg_assistant",
- expect.objectContaining({
- content: [expect.objectContaining({ state: { status: "streaming", input: '{"path":"README.md"}' } })],
- }),
- ],
- ["msg_completed", expect.objectContaining({ status: "completed", summary: "summary", recent: "recent" })],
- [
- "msg_failed",
- {
- time: { created: 4 },
- status: "failed",
- reason: "manual",
- error: {
- type: "compaction.failed",
- message: "Compaction failed before recording an error",
- },
- },
- ],
- ["msg_running", expect.objectContaining({ status: "running", summary: "partial", recent: "recent" })],
- ["msg_shell", expect.objectContaining({ shellID: "sh_old", command: "pwd", status: "exited", exit: 0 })],
- ["msg_skill", { time: { created: 1 }, skill: "effect", name: "effect", text: "Use Effect" }],
- ["msg_synthetic", { time: { created: 6 }, text: "context", description: "source" }],
- ])
- expect(yield* db.get(sql`SELECT * FROM session_input`)).toEqual({
- id: "msg_queued",
- session_id: "ses_test",
- type: "compaction",
- prompt: null,
- delivery: null,
- admitted_seq: 4,
- promoted_seq: null,
- time_created: 5,
- })
- const migratedEvents = yield* db.all<{
- id: string
- aggregate_id: string
- seq: number
- created: number
- type: string
- data: string
- }>(sql`SELECT * FROM event ORDER BY seq`)
- expect(migratedEvents.map((event) => ({ ...event, data: JSON.parse(event.data) }))).toEqual([
- {
- id: "evt_skill",
- aggregate_id: "ses_test",
- seq: 1,
- created: 101,
- type: "session.skill.activated.1",
- data: { sessionID: "ses_test", id: "effect", name: "effect", text: "Use" },
- },
- {
- id: "evt_started",
- aggregate_id: "ses_test",
- seq: 2,
- created: 102,
- type: "session.compaction.started.1",
- data: { sessionID: "ses_test", reason: "auto", recent: "" },
- },
- {
- id: "evt_failed",
- aggregate_id: "ses_test",
- seq: 4,
- created: 104,
- type: "session.compaction.failed.1",
- data: {
- sessionID: "ses_test",
- reason: "auto",
- error: {
- type: "compaction.failed",
- message: "Compaction failed before recording an error",
- },
- },
- },
- {
- id: "evt_revert",
- aggregate_id: "ses_test",
- seq: 5,
- created: 105,
- type: "session.revert.staged.1",
- data: {
- sessionID: "ses_test",
- revert: {
- messageID: "msg_skill",
- snapshot: "tree",
- files: [{ file: "src/a.ts", patch: "@@", additions: 1, deletions: 0, status: "modified" }],
- },
- },
- },
- {
- id: "evt_skill_current",
- aggregate_id: "ses_test",
- seq: 6,
- created: 106,
- type: "session.skill.activated.1",
- data: { sessionID: "ses_test", id: "effect-id", name: "Effect", text: "Use" },
- },
- ])
- expect(yield* db.get(sql`SELECT * FROM event_sequence`)).toEqual({
- aggregate_id: "ses_test",
- seq: 9,
- owner_id: "owner",
- })
- expect(yield* db.get(sql`SELECT * FROM instruction_checkpoint`)).toEqual({
- session_id: "ses_test",
- baseline: "baseline",
- snapshot: '{"source":"value"}',
- baseline_seq: 7,
- })
- }),
- )
- })
- test("resets incompatible V2 Session event history", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session_input (id text PRIMARY KEY)`)
- yield* db.run(sql`CREATE TABLE session_message (id text PRIMARY KEY)`)
- yield* db.run(sql`CREATE TABLE event (id text PRIMARY KEY)`)
- yield* db.run(sql`CREATE TABLE event_sequence (aggregate_id text PRIMARY KEY, seq integer NOT NULL)`)
- yield* db.run(sql`INSERT INTO session_input (id) VALUES ('input')`)
- yield* db.run(sql`INSERT INTO session_message (id) VALUES ('message')`)
- yield* db.run(sql`INSERT INTO event (id) VALUES ('event')`)
- yield* db.run(sql`INSERT INTO event_sequence (aggregate_id, seq) VALUES ('session', 1)`)
- yield* DatabaseMigration.applyOnly(db, [resetSessionEventsMigration])
- expect(yield* db.get(sql`SELECT id FROM session_input`)).toBeUndefined()
- expect(yield* db.get(sql`SELECT id FROM session_message`)).toBeUndefined()
- expect(yield* db.get(sql`SELECT id FROM event`)).toBeUndefined()
- expect(yield* db.get(sql`SELECT aggregate_id FROM event_sequence`)).toBeUndefined()
- }),
- )
- })
- test("serializes concurrent embedded initialization for one database path", async () => {
- await using tmp = await tmpdir()
- const filename = path.join(tmp.path, "embedded.sqlite")
- const layers = [Database.layer({ path: filename }), Database.layer({ path: filename })]
- await Effect.runPromise(
- Effect.all(
- layers.map((layer) => Effect.scoped(Layer.build(layer))),
- { concurrency: "unbounded" },
- ),
- )
- })
- if (process.platform === "linux") {
- test("declared schema has no ungenerated migrations", async () => {
- const result = await $`bun ${fileURLToPath(new URL("../script/migration.ts", import.meta.url))} --check`
- .quiet()
- .nothrow()
- expect(result.exitCode, result.stderr.toString()).toBe(0)
- expect(result.stdout.toString()).toContain("No schema changes, nothing to migrate")
- }, 30_000)
- }
- test("applies tracked migrations to an empty database", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* DatabaseMigration.apply(db)
- expect(yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'session'`)).toEqual({
- name: "session",
- })
- expect(
- yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'session_input'`),
- ).toBeUndefined()
- expect(
- yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'session_pending'`),
- ).toEqual({ name: "session_pending" })
- expect(
- yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'instruction_checkpoint'`),
- ).toBeUndefined()
- expect(
- yield* db.all(
- sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name IN ('instruction_blob', 'instruction_state') ORDER BY name`,
- ),
- ).toEqual([{ name: "instruction_blob" }, { name: "instruction_state" }])
- expect(yield* db.get(sql`SELECT count(*) as count FROM migration`)).toEqual({ count: migrations.length })
- expect(
- yield* db.all(
- sql`SELECT name FROM sqlite_master WHERE type = 'index' AND name IN ('event_aggregate_seq_idx', 'event_aggregate_type_seq_idx', 'session_input_session_pending_seq_idx', 'session_input_session_pending_delivery_seq_idx', 'session_input_session_pending_type_delivery_seq_idx', 'session_input_session_pending_compaction_idx', 'session_input_session_admitted_seq_idx', 'session_input_session_promoted_seq_idx', 'session_pending_session_delivery_seq_idx', 'session_pending_session_compaction_idx', 'session_pending_session_admitted_seq_idx', 'session_message_session_idx', 'session_message_session_type_idx', 'session_message_session_seq_idx', 'session_message_session_type_seq_idx', 'session_message_session_time_created_id_idx') ORDER BY name`,
- ),
- ).toEqual([
- { name: "event_aggregate_seq_idx" },
- { name: "event_aggregate_type_seq_idx" },
- { name: "session_message_session_seq_idx" },
- { name: "session_message_session_time_created_id_idx" },
- { name: "session_message_session_type_seq_idx" },
- { name: "session_pending_session_admitted_seq_idx" },
- { name: "session_pending_session_compaction_idx" },
- { name: "session_pending_session_delivery_seq_idx" },
- ])
- }),
- )
- })
- test("rejects a non-empty database without a session table", async () => {
- await expect(
- run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE unrelated (id text PRIMARY KEY)`)
- yield* DatabaseMigration.apply(db)
- }),
- ),
- ).rejects.toThrow("Database is not empty and has no session table")
- })
- test("backfills existing Context Epoch rows to the build agent", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(
- sql`CREATE TABLE session_context_epoch (session_id text PRIMARY KEY, baseline text NOT NULL, snapshot text NOT NULL, baseline_seq integer NOT NULL, replacement_seq integer, revision integer DEFAULT 0 NOT NULL)`,
- )
- yield* db.run(
- sql`INSERT INTO session_context_epoch (session_id, baseline, snapshot, baseline_seq) VALUES ('ses_existing', 'baseline', '{}', 0)`,
- )
- yield* DatabaseMigration.applyOnly(db, [contextEpochAgentMigration])
- expect(yield* db.get(sql`SELECT agent FROM session_context_epoch WHERE session_id = 'ses_existing'`)).toEqual({
- agent: "build",
- })
- }),
- )
- })
- test("separates existing fork provenance from subagent hierarchy", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, parent_id text)`)
- yield* db.run(
- sql`CREATE TABLE event (aggregate_id text NOT NULL, seq integer NOT NULL, type text NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(sql`INSERT INTO session VALUES ('ses_source', NULL), ('ses_fork', 'ses_source')`)
- yield* db.run(
- sql`INSERT INTO event VALUES ('ses_fork', 0, 'session.forked', '{"sessionID":"ses_fork","parentID":"ses_source","from":"msg_boundary"}')`,
- )
- yield* DatabaseMigration.applyOnly(db, [addSessionForkMigration])
- expect(
- yield* db.get(sql`SELECT parent_id, fork_session_id, fork_message_id FROM session WHERE id = 'ses_fork'`),
- ).toEqual({
- parent_id: null,
- fork_session_id: "ses_source",
- fork_message_id: "msg_boundary",
- })
- expect(
- yield* db.get(sql`SELECT parent_id, fork_session_id, fork_message_id FROM session WHERE id = 'ses_source'`),
- ).toEqual({
- parent_id: null,
- fork_session_id: null,
- fork_message_id: null,
- })
- }),
- )
- })
- test("does not infer restart continuation from historical shutdown events", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY)`)
- yield* db.run(
- sql`CREATE TABLE event (aggregate_id text NOT NULL, seq integer NOT NULL, type text NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(sql`INSERT INTO session VALUES ('ses_shutdown')`)
- yield* db.run(
- sql`INSERT INTO event VALUES ('ses_shutdown', 0, 'session.execution.interrupted.1', '{"reason":"shutdown"}')`,
- )
- yield* DatabaseMigration.applyOnly(db, [timeSuspendedMigration])
- expect(yield* db.get(sql`SELECT time_suspended FROM session WHERE id = 'ses_shutdown'`)).toEqual({
- time_suspended: null,
- })
- }),
- )
- })
- test("renames instruction state without losing rows or durable updates", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY)`)
- yield* db.run(
- sql`CREATE TABLE session_context_entry (session_id text NOT NULL, key text NOT NULL, value text NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, PRIMARY KEY(session_id, key))`,
- )
- yield* db.run(
- sql`CREATE TABLE session_context_epoch (session_id text PRIMARY KEY, baseline text NOT NULL, snapshot text NOT NULL, baseline_seq integer NOT NULL)`,
- )
- yield* db.run(sql`CREATE TABLE event (type text NOT NULL)`)
- yield* db.run(sql`INSERT INTO session_context_entry VALUES ('ses_test', 'plan', '"ready"', 1, 2)`)
- yield* db.run(sql`INSERT INTO session_context_epoch VALUES ('ses_test', 'baseline', '{}', 7)`)
- yield* db.run(sql`INSERT INTO event VALUES ('session.context.updated.1')`)
- yield* DatabaseMigration.applyOnly(db, [renameInstructionsMigration])
- expect(yield* db.get(sql`SELECT * FROM instruction_entry`)).toEqual({
- session_id: "ses_test",
- key: "plan",
- value: '"ready"',
- time_created: 1,
- time_updated: 2,
- })
- expect(yield* db.get(sql`SELECT * FROM instruction_checkpoint`)).toEqual({
- session_id: "ses_test",
- baseline: "baseline",
- snapshot: "{}",
- baseline_seq: 7,
- })
- expect(yield* db.get(sql`SELECT type FROM event`)).toEqual({ type: "session.instructions.updated.1" })
- }),
- )
- })
- test("deletes pre-beta instruction events and projected System messages", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, fork_session_id text)`)
- yield* db.run(
- sql`CREATE TABLE instruction_entry (session_id text NOT NULL, key text NOT NULL, value text NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, PRIMARY KEY(session_id, key))`,
- )
- yield* db.run(sql`CREATE TABLE instruction_checkpoint (session_id text PRIMARY KEY)`)
- yield* db.run(sql`CREATE TABLE event_sequence (aggregate_id text PRIMARY KEY, seq integer NOT NULL)`)
- yield* db.run(
- sql`CREATE TABLE event (id text PRIMARY KEY, aggregate_id text NOT NULL, seq integer NOT NULL, type text NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(sql`CREATE TABLE session_message (id text PRIMARY KEY, type text NOT NULL)`)
- yield* db.run(sql`INSERT INTO session VALUES ('ses_test', NULL)`)
- yield* db.run(
- sql`INSERT INTO event VALUES ('evt_instruction', 'ses_test', 0, 'session.instructions.updated.1', '{"sessionID":"ses_test","text":"changed"}')`,
- )
- yield* db.run(
- sql`INSERT INTO event VALUES ('evt_other', 'ses_test', 1, 'session.synthetic.1', '{"sessionID":"ses_test","text":"keep"}')`,
- )
- yield* db.run(
- sql`INSERT INTO session_message VALUES ('msg_instruction', 'system'), ('msg_other', 'system'), ('msg_user', 'user')`,
- )
- yield* db.run(sql`INSERT INTO instruction_entry VALUES ('ses_test', 'plan', '"ready"', 1, 2)`)
- yield* DatabaseMigration.applyOnly(db, [instructionSyncMigration])
- expect(yield* db.all(sql`SELECT id, type FROM event`)).toEqual([
- { id: "evt_other", type: "session.synthetic.1" },
- ])
- expect(yield* db.all(sql`SELECT id, type FROM session_message ORDER BY id`)).toEqual([
- { id: "msg_user", type: "user" },
- ])
- expect(yield* db.get(sql`SELECT * FROM instruction_entry`)).toEqual({
- session_id: "ses_test",
- key: "plan",
- value: '"ready"',
- removed: 0,
- time_created: 1,
- time_updated: 2,
- })
- expect(
- yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'instruction_checkpoint'`),
- ).toBeUndefined()
- }),
- )
- })
- test("deletes durable tool progress without changing aggregate sequence watermarks", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE event_sequence (aggregate_id text PRIMARY KEY, seq integer NOT NULL)`)
- yield* db.run(
- sql`CREATE TABLE event (id text PRIMARY KEY, aggregate_id text NOT NULL, seq integer NOT NULL, type text NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(sql`INSERT INTO event_sequence VALUES ('ses_test', 5)`)
- yield* db.run(sql`INSERT INTO event VALUES ('evt_success', 'ses_test', 4, 'session.tool.success.1', '{}')`)
- yield* db.run(sql`INSERT INTO event VALUES ('evt_progress', 'ses_test', 5, 'session.tool.progress.1', '{}')`)
- yield* DatabaseMigration.applyOnly(db, [deleteToolProgressEventsMigration])
- expect(yield* db.all(sql`SELECT id, seq, type, data FROM event ORDER BY seq`)).toEqual([
- { id: "evt_success", seq: 4, type: "session.tool.success.1", data: "{}" },
- ])
- expect(yield* db.get(sql`SELECT aggregate_id, seq FROM event_sequence`)).toEqual({
- aggregate_id: "ses_test",
- seq: 5,
- })
- }),
- )
- })
- test("rewrites projected tool rows into the canonical result shape", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(
- sql`CREATE TABLE session_message (id text PRIMARY KEY, session_id text NOT NULL, type text NOT NULL, seq integer NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(sql`CREATE TABLE event (id text PRIMARY KEY, type text NOT NULL, data text NOT NULL)`)
- const assistant = {
- agent: "build",
- model: { id: "model", providerID: "provider" },
- content: [
- { type: "text", text: "before" },
- {
- type: "tool",
- id: "call_content",
- name: "grep",
- state: {
- status: "completed",
- input: { pattern: "TODO" },
- content: [{ type: "text", text: "src/a.ts:1: TODO" }],
- structured: { value: [{ file: "src/a.ts", line: 1 }] },
- },
- time: { created: 1, completed: 2 },
- },
- {
- type: "tool",
- id: "call_structured_only",
- name: "read",
- state: {
- status: "completed",
- input: { path: "README.md" },
- content: [],
- structured: { text: "hello" },
- },
- time: { created: 1, completed: 2 },
- },
- {
- type: "tool",
- id: "call_hosted",
- name: "web_search",
- executed: true,
- providerResultState: { blockType: "web_search_tool_result" },
- state: {
- status: "completed",
- input: { query: "effect" },
- content: [],
- structured: {},
- result: { type: "json", value: [{ url: "https://example.com" }] },
- },
- time: { created: 1, completed: 2 },
- },
- {
- type: "tool",
- id: "call_failed",
- name: "shell",
- state: {
- status: "error",
- input: { command: "sleep 99" },
- error: { type: "tool.execution", message: "timed out" },
- content: [{ type: "text", text: "partial output" }],
- structured: { truncated: false },
- result: { type: "error", value: "timed out" },
- },
- time: { created: 1, completed: 2 },
- },
- {
- type: "tool",
- id: "call_running",
- name: "shell",
- state: {
- status: "running",
- input: { command: "sleep 1" },
- structured: { truncated: false },
- content: [{ type: "text", text: "tick" }],
- },
- time: { created: 1, ran: 2 },
- },
- ],
- time: { created: 1 },
- }
- yield* db.run(
- sql`INSERT INTO session_message VALUES ('msg_tools', 'ses_test', 'assistant', 1, 10, 11, ${JSON.stringify(assistant)})`,
- )
- yield* db.run(
- sql`INSERT INTO session_message VALUES ('msg_user', 'ses_test', 'user', 2, 12, 13, '{"text":"hi","time":{"created":1}}')`,
- )
- // A row that never decoded must be skipped, not fail the migration.
- yield* db.run(
- sql`INSERT INTO session_message VALUES ('msg_corrupt', 'ses_test', 'assistant', 3, 14, 15, 'not json')`,
- )
- yield* db.run(
- sql`INSERT INTO event VALUES ('evt_success', 'session.tool.success.1', ${JSON.stringify({
- sessionID: "ses_test",
- assistantMessageID: "msg_tools",
- callID: "call_hosted",
- structured: {},
- content: [],
- result: { type: "json", value: [{ url: "https://example.com" }] },
- executed: true,
- })})`,
- )
- yield* db.run(
- sql`INSERT INTO event VALUES ('evt_failed', 'session.tool.failed.1', ${JSON.stringify({
- sessionID: "ses_test",
- assistantMessageID: "msg_tools",
- callID: "call_failed",
- error: { type: "tool.execution", message: "timed out" },
- metadata: { truncated: false },
- executed: false,
- })})`,
- )
- yield* DatabaseMigration.applyOnly(db, [canonicalToolResultsMigration])
- const row = yield* db.get<{ data: string }>(sql`SELECT data FROM session_message WHERE id = 'msg_tools'`)
- const migrated = JSON.parse(row!.data)
- // Every migrated row must decode with the current schema; reload hard-fails otherwise.
- Schema.decodeUnknownSync(SessionMessage.Info)({ ...migrated, id: "msg_tools", type: "assistant" })
- const states = new Map(
- migrated.content.flatMap((part: { type: string; id?: string }) =>
- part.type === "tool" ? [[part.id, part]] : [],
- ),
- )
- expect(states.get("call_content")).toMatchObject({
- state: {
- status: "completed",
- input: { pattern: "TODO" },
- content: [{ type: "text", text: "src/a.ts:1: TODO" }],
- // Old generic structured payloads survive as canonical metadata.
- metadata: { value: [{ file: "src/a.ts", line: 1 }] },
- },
- })
- expect((states.get("call_content") as { state: Record<string, unknown> }).state).not.toHaveProperty(
- "structured",
- )
- expect(states.get("call_structured_only")).toMatchObject({
- state: {
- status: "completed",
- content: [{ type: "text", text: JSON.stringify({ text: "hello" }, null, 2) }],
- metadata: { text: "hello" },
- },
- })
- expect(states.get("call_hosted")).toMatchObject({
- executed: true,
- providerResultState: {
- blockType: "web_search_tool_result",
- result: [{ url: "https://example.com" }],
- },
- state: {
- status: "completed",
- content: [{ type: "text", text: JSON.stringify([{ url: "https://example.com" }], null, 2) }],
- },
- })
- expect(states.get("call_failed")).toMatchObject({
- state: {
- status: "error",
- error: { type: "tool.execution", message: "timed out" },
- content: [{ type: "text", text: "partial output" }],
- metadata: { truncated: false },
- },
- })
- const failedState = (states.get("call_failed") as { state: Record<string, unknown> }).state
- expect(failedState).not.toHaveProperty("result")
- expect(failedState).not.toHaveProperty("structured")
- expect(states.get("call_running")).toMatchObject({
- state: {
- status: "running",
- metadata: { truncated: false },
- },
- })
- const event = yield* db.get<{ type: string; data: string }>(sql`SELECT type, data FROM event WHERE id = 'evt_success'`)
- expect(event!.type).toBe("session.tool.success.1")
- expect(JSON.parse(event!.data)).toEqual({
- sessionID: "ses_test",
- assistantMessageID: "msg_tools",
- callID: "call_hosted",
- structured: {},
- content: [],
- result: { type: "json", value: [{ url: "https://example.com" }] },
- executed: true,
- })
- const failedEvent = yield* db.get<{ type: string; data: string }>(sql`SELECT type, data FROM event WHERE id = 'evt_failed'`)
- expect(failedEvent!.type).toBe("session.tool.failed.1")
- expect(JSON.parse(failedEvent!.data)).toEqual({
- sessionID: "ses_test",
- assistantMessageID: "msg_tools",
- callID: "call_failed",
- error: { type: "tool.execution", message: "timed out" },
- metadata: { truncated: false },
- executed: false,
- })
- expect(yield* db.get(sql`SELECT data FROM session_message WHERE id = 'msg_user'`)).toEqual({
- data: '{"text":"hi","time":{"created":1}}',
- })
- expect(yield* db.get(sql`SELECT data FROM session_message WHERE id = 'msg_corrupt'`)).toEqual({
- data: "not json",
- })
- }),
- )
- })
- test("records the authoritative parent sequence on existing forks", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, fork_session_id text)`)
- yield* db.run(
- sql`CREATE TABLE instruction_entry (session_id text NOT NULL, key text NOT NULL, value text NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, PRIMARY KEY(session_id, key))`,
- )
- yield* db.run(sql`CREATE TABLE instruction_checkpoint (session_id text PRIMARY KEY)`)
- yield* db.run(sql`CREATE TABLE session_message (id text PRIMARY KEY, type text NOT NULL)`)
- yield* db.run(sql`CREATE TABLE event_sequence (aggregate_id text PRIMARY KEY, seq integer NOT NULL)`)
- yield* db.run(
- sql`CREATE TABLE event (id text PRIMARY KEY, aggregate_id text NOT NULL, seq integer NOT NULL, type text NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(sql`INSERT INTO session VALUES ('ses_child', 'ses_parent')`)
- yield* db.run(sql`INSERT INTO event_sequence VALUES ('ses_child', 8)`)
- yield* db.run(
- sql`INSERT INTO event VALUES ('evt_fork', 'ses_child', 0, 'session.forked.1', '{"sessionID":"ses_child","parentID":"ses_parent"}')`,
- )
- yield* db.run(
- sql`INSERT INTO event VALUES ('evt_instruction', 'ses_child', 5, 'session.instructions.updated.1', '{"sessionID":"ses_child","text":"changed"}')`,
- )
- yield* db.run(sql`INSERT INTO event VALUES ('evt_input', 'ses_child', 6, 'session.input.admitted.1', '{}')`)
- yield* DatabaseMigration.applyOnly(db, [instructionSyncMigration])
- expect(yield* db.get(sql`SELECT fork_seq FROM session`)).toEqual({ fork_seq: 4 })
- expect(yield* db.get(sql`SELECT type, data FROM event WHERE seq = 0`)).toEqual({
- type: "session.forked.2",
- data: '{"sessionID":"ses_child","parentID":"ses_parent","parentSeq":4}',
- })
- expect(yield* db.get(sql`SELECT id FROM event WHERE id = 'evt_instruction'`)).toBeUndefined()
- }),
- )
- })
- test("keeps legacy credential fields nullable", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(
- sql`CREATE TABLE credential (id text PRIMARY KEY, connector_id text NOT NULL, method_id text NOT NULL, label text NOT NULL, value text NOT NULL, active integer DEFAULT false NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL)`,
- )
- yield* db.run(
- sql`CREATE UNIQUE INDEX credential_connector_active_idx ON credential (connector_id) WHERE active = 1`,
- )
- yield* DatabaseMigration.applyOnly(db, [simplifyIntegrationCredentialsMigration])
- yield* db.run(
- sql`INSERT INTO credential (id, connector_id, method_id, label, value, active, time_created, time_updated) VALUES ('legacy', 'openai', 'oauth', 'Legacy', '{}', 1, 1, 1)`,
- )
- yield* db.run(
- sql`INSERT INTO credential (id, integration_id, label, value, time_created, time_updated) VALUES ('current', 'anthropic', 'Current', '{}', 2, 2)`,
- )
- expect(yield* db.get(sql`SELECT connector_id, method_id, active FROM credential WHERE id = 'current'`)).toEqual(
- { connector_id: null, method_id: null, active: null },
- )
- }),
- )
- })
- test("resets beta history and rebuilds event-sourced Session input storage", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, workspace_id text)`)
- yield* db.run(sql`CREATE TABLE workspace (id text PRIMARY KEY)`)
- yield* db.run(sql`CREATE TABLE message (id text PRIMARY KEY)`)
- yield* db.run(sql`CREATE TABLE part (id text PRIMARY KEY)`)
- yield* db.run(sql`CREATE TABLE event_sequence (aggregate_id text PRIMARY KEY, seq integer NOT NULL)`)
- yield* db.run(
- sql`CREATE TABLE event (id text PRIMARY KEY, aggregate_id text NOT NULL, seq integer NOT NULL, type text NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(sql`CREATE INDEX event_aggregate_seq_idx ON event (aggregate_id, seq)`)
- yield* db.run(sql`CREATE INDEX event_aggregate_type_seq_idx ON event (aggregate_id, type, seq)`)
- yield* db.run(
- sql`CREATE TABLE session_message (id text PRIMARY KEY, session_id text NOT NULL, type text NOT NULL, seq integer NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(sql`CREATE INDEX session_message_session_seq_idx ON session_message (session_id, seq)`)
- yield* db.run(
- sql`CREATE TABLE session_input (seq integer PRIMARY KEY AUTOINCREMENT, id text NOT NULL UNIQUE, session_id text NOT NULL, prompt text NOT NULL, delivery text NOT NULL, promoted_seq integer, time_created integer NOT NULL)`,
- )
- yield* db.run(
- sql`CREATE INDEX session_input_session_pending_delivery_seq_idx ON session_input (session_id, promoted_seq, delivery, seq)`,
- )
- yield* db.run(sql`INSERT INTO session (id, workspace_id) VALUES ('session', 'wrk_old')`)
- yield* db.run(sql`INSERT INTO workspace (id) VALUES ('wrk_old')`)
- yield* db.run(sql`INSERT INTO message (id) VALUES ('message')`)
- yield* db.run(sql`INSERT INTO part (id) VALUES ('part')`)
- yield* db.run(sql`INSERT INTO event_sequence (aggregate_id, seq) VALUES ('session', 0)`)
- yield* db.run(
- sql`INSERT INTO event (id, aggregate_id, seq, type, data) VALUES ('evt_old', 'session', 0, 'old.1', '{}')`,
- )
- yield* db.run(
- sql`INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES ('msg_old', 'session', 'user', 0, 1, 1, '{}')`,
- )
- yield* db.run(
- sql`INSERT INTO session_input (id, session_id, prompt, delivery, time_created) VALUES ('msg_pending', 'session', '{}', 'steer', 1)`,
- )
- yield* DatabaseMigration.applyOnly(db, [eventSourcedSessionPendingMigration])
- expect(yield* db.all(sql`SELECT id, workspace_id FROM session`)).toEqual([
- { id: "session", workspace_id: null },
- ])
- expect(yield* db.all(sql`SELECT id FROM workspace`)).toEqual([])
- expect(yield* db.all(sql`SELECT id FROM message`)).toEqual([{ id: "message" }])
- expect(yield* db.all(sql`SELECT id FROM part`)).toEqual([{ id: "part" }])
- expect(yield* db.all(sql`SELECT id FROM event`)).toEqual([])
- expect(yield* db.all(sql`SELECT aggregate_id FROM event_sequence`)).toEqual([])
- expect(yield* db.all(sql`SELECT id FROM session_message`)).toEqual([])
- expect(yield* db.all(sql`SELECT id FROM session_input`)).toEqual([])
- expect(
- (yield* db.all<{ name: string }>(sql`PRAGMA table_info(session_input)`)).map((column) => column.name),
- ).toEqual(["id", "session_id", "prompt", "delivery", "admitted_seq", "promoted_seq", "time_created"])
- expect(
- (yield* db.all<{ name: string; unique: number }>(sql`PRAGMA index_list(session_message)`)).find(
- (index) => index.name === "session_message_session_seq_idx",
- ),
- ).toMatchObject({ unique: 1 })
- expect(
- (yield* db.all<{ name: string; unique: number }>(sql`PRAGMA index_list(event)`)).find(
- (index) => index.name === "event_aggregate_seq_idx",
- ),
- ).toMatchObject({ unique: 1 })
- expect(
- (yield* db.all<{ name: string; unique: number }>(sql`PRAGMA index_list(session_input)`)).filter((index) =>
- ["session_input_session_admitted_seq_idx", "session_input_session_promoted_seq_idx"].includes(index.name),
- ),
- ).toEqual([
- expect.objectContaining({ name: "session_input_session_promoted_seq_idx", unique: 1 }),
- expect.objectContaining({ name: "session_input_session_admitted_seq_idx", unique: 1 }),
- ])
- }),
- )
- })
- test("preserves canonical V1 state and restarts its event stream", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`PRAGMA foreign_keys = ON`)
- yield* DatabaseMigration.apply(db)
- yield* db.run(
- sql`INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('global', '/project', 1, 1, '[]')`,
- )
- yield* db.run(
- sql`INSERT INTO workspace (id, type, project_id, time_used) VALUES ('workspace', 'local', 'global', 1)`,
- )
- yield* db.run(
- sql`INSERT INTO session (id, project_id, workspace_id, slug, directory, title, version, time_created, time_updated) VALUES ('session', 'global', 'workspace', 'session', '/project', 'Before', 'test', 1, 1)`,
- )
- yield* db.run(
- sql`INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES ('message', 'session', 1, 1, '{}')`,
- )
- yield* db.run(
- sql`INSERT INTO part (id, message_id, session_id, time_created, time_updated, data) VALUES ('part', 'message', 'session', 1, 1, '{}')`,
- )
- yield* db.run(sql`INSERT INTO event_sequence (aggregate_id, seq) VALUES ('session', 9)`)
- yield* db.run(
- sql`INSERT INTO event (id, aggregate_id, seq, type, data, created) VALUES ('event', 'session', 9, 'session.updated.1', '{}', 1)`,
- )
- yield* db.run(
- sql`INSERT INTO session_pending (id, session_id, type, data, delivery, admitted_seq, time_created) VALUES ('input', 'session', 'user', '{}', 'steer', 9, 1)`,
- )
- yield* db.run(
- sql`INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES ('projected', 'session', 'user', 9, 1, 1, '{}')`,
- )
- yield* db.run(sql`CREATE TABLE session_context_epoch (session_id text PRIMARY KEY)`)
- // The partial compaction index embeds the qualified table name, so it
- // must drop before the historical rename dance and recreate after.
- yield* db.run(sql`DROP INDEX session_pending_session_compaction_idx`)
- yield* db.run(sql`ALTER TABLE session_pending RENAME TO session_input`)
- yield* db.run(sql`DELETE FROM migration WHERE id = ${simplifySessionPendingMigration.id}`)
- yield* DatabaseMigration.applyOnly(db, [simplifySessionPendingMigration])
- yield* db.run(sql`DROP TABLE session_context_epoch`)
- yield* db.run(sql`ALTER TABLE session_input RENAME TO session_pending`)
- yield* db.run(
- sql`CREATE UNIQUE INDEX session_pending_session_compaction_idx ON session_pending (session_id) WHERE "session_pending"."type" = 'compaction'`,
- )
- const database = Layer.succeed(Database.Service, { db })
- yield* Bus.Service.use((service) =>
- service.publish(SessionV1.Event.Updated, {
- sessionID: SessionSchema.ID.make("session"),
- info: {
- id: SessionSchema.ID.make("session"),
- slug: "session",
- projectID: Project.ID.global,
- directory: "/project",
- title: "After",
- version: "test",
- time: { created: 1, updated: 2 },
- },
- }),
- ).pipe(
- Effect.provide(
- AppNodeBuilder.build(LayerNode.group([Bus.node, SessionProjector.node]), [[Database.node, database]]),
- ),
- )
- expect(
- yield* db.get(sql`
- SELECT
- (SELECT title FROM session WHERE id = 'session') AS title,
- (SELECT workspace_id FROM session WHERE id = 'session') AS workspaceID,
- (SELECT COUNT(*) FROM message WHERE id = 'message') AS messages,
- (SELECT COUNT(*) FROM part WHERE id = 'part') AS parts,
- (SELECT COUNT(*) FROM workspace) AS workspaces,
- (SELECT COUNT(*) FROM session_pending) AS sessionInputs,
- (SELECT COUNT(*) FROM session_message) AS sessionMessages,
- (SELECT COUNT(*) FROM instruction_state) AS instructionStates,
- (SELECT seq FROM event_sequence WHERE aggregate_id = 'session') AS seq,
- (SELECT type FROM event WHERE aggregate_id = 'session') AS eventType
- `),
- ).toEqual({
- title: "After",
- workspaceID: null,
- messages: 1,
- parts: 1,
- workspaces: 0,
- sessionInputs: 0,
- sessionMessages: 0,
- instructionStates: 0,
- seq: 0,
- eventType: "session.updated.1",
- })
- }),
- )
- })
- test("preserves admitted prompts while generalizing the durable inbox", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(
- sql`CREATE TABLE session_input (id text PRIMARY KEY, session_id text NOT NULL, prompt text NOT NULL, delivery text NOT NULL, admitted_seq integer NOT NULL, promoted_seq integer, time_created integer NOT NULL)`,
- )
- yield* db.run(
- sql`INSERT INTO session_input (id, session_id, prompt, delivery, admitted_seq, promoted_seq, time_created) VALUES ('input', 'session', '{"text":"hello"}', 'steer', 4, NULL, 1)`,
- )
- yield* DatabaseMigration.applyOnly(db, [durableSessionInboxMigration])
- expect(
- yield* db.all(
- sql`SELECT id, type, prompt, delivery, admitted_seq, promoted_seq FROM session_input ORDER BY admitted_seq`,
- ),
- ).toEqual([
- {
- id: "input",
- type: "prompt",
- prompt: '{"text":"hello"}',
- delivery: "steer",
- admitted_seq: 4,
- promoted_seq: null,
- },
- ])
- }),
- )
- })
- test("migrates prompt inbox rows and lifecycle events to generic user input", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY)`)
- yield* db.run(sql`INSERT INTO session (id) VALUES ('session')`)
- yield* db.run(
- sql`CREATE TABLE session_input (id text PRIMARY KEY, session_id text NOT NULL, type text NOT NULL, prompt text, delivery text, admitted_seq integer NOT NULL, promoted_seq integer, time_created integer NOT NULL)`,
- )
- yield* db.run(
- sql`INSERT INTO session_input (id, session_id, type, prompt, delivery, admitted_seq, promoted_seq, time_created) VALUES ('input', 'session', 'prompt', '{"text":"hello"}', 'queue', 4, NULL, 1)`,
- )
- yield* db.run(
- sql`INSERT INTO session_input (id, session_id, type, prompt, delivery, admitted_seq, promoted_seq, time_created) VALUES ('empty', 'session', 'prompt', NULL, 'steer', 6, NULL, 2)`,
- )
- yield* db.run(
- sql`CREATE TABLE event (id text PRIMARY KEY, aggregate_id text NOT NULL, seq integer NOT NULL, created integer NOT NULL, type text NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(
- sql`INSERT INTO event (id, aggregate_id, seq, created, type, data) VALUES ('admitted', 'session', 4, 1, 'session.prompt.admitted.1', '{"sessionID":"session","inputID":"input","prompt":{"text":"hello"},"delivery":"queue"}')`,
- )
- yield* db.run(
- sql`INSERT INTO event (id, aggregate_id, seq, created, type, data) VALUES ('promoted', 'session', 5, 2, 'session.prompt.promoted.1', '{"sessionID":"session","inputID":"input"}')`,
- )
- yield* db.run(
- sql`INSERT INTO event (id, aggregate_id, seq, created, type, data) VALUES ('empty-admitted', 'session', 6, 2, 'session.prompt.admitted.1', '{"sessionID":"session","inputID":"empty","prompt":null,"delivery":"steer"}')`,
- )
- yield* db.run(
- sql`INSERT INTO event (id, aggregate_id, seq, created, type, data) VALUES ('empty-promoted', 'session', 7, 2, 'session.prompt.promoted.1', '{"sessionID":"session","inputID":"empty"}')`,
- )
- yield* DatabaseMigration.applyOnly(db, [genericSessionPendingMigration])
- expect(yield* db.all(sql`SELECT id, type, data, delivery FROM session_input ORDER BY admitted_seq`)).toEqual([
- { id: "input", type: "user", data: '{"text":"hello"}', delivery: "queue" },
- ])
- expect(yield* db.all(sql`SELECT type, data FROM event ORDER BY seq`)).toEqual([
- {
- type: "session.input.admitted.1",
- data: '{"sessionID":"session","inputID":"input","input":{"type":"user","data":{"text":"hello"},"delivery":"queue"}}',
- },
- {
- type: "session.input.promoted.1",
- data: '{"sessionID":"session","inputID":"input"}',
- },
- ])
- }),
- )
- })
- test("replaces the durable inbox with the empty session_pending table", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY)`)
- yield* db.run(sql`INSERT INTO session (id) VALUES ('session')`)
- yield* db.run(
- sql`CREATE TABLE session_input (id text PRIMARY KEY, session_id text NOT NULL REFERENCES session(id) ON DELETE CASCADE, type text NOT NULL, data text NOT NULL, delivery text, admitted_seq integer NOT NULL, promoted_seq integer, time_created integer NOT NULL)`,
- )
- // Interim v2 builds shipped differing index sets on real databases;
- // dropping the table removes whatever variant exists.
- yield* db.run(
- sql`CREATE INDEX session_input_session_pending_type_delivery_seq_idx ON session_input (session_id, promoted_seq, type, delivery, admitted_seq)`,
- )
- yield* db.run(
- sql`INSERT INTO session_input (id, session_id, type, data, delivery, admitted_seq, promoted_seq, time_created) VALUES ('pending', 'session', 'user', '{"text":"hello"}', 'steer', 4, NULL, 1)`,
- )
- yield* DatabaseMigration.applyOnly(db, [sessionPendingTableMigration])
- expect(
- yield* db.get(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'session_input'`),
- ).toBeUndefined()
- expect(yield* db.all(sql`SELECT id FROM session_pending`)).toEqual([])
- expect(
- (yield* db.all<{ name: string }>(sql`PRAGMA table_info(session_pending)`)).map((column) => column.name),
- ).toEqual(["id", "session_id", "type", "data", "delivery", "admitted_seq", "time_created"])
- expect(
- (yield* db.all<{ name: string; unique: number }>(sql`PRAGMA index_list(session_pending)`))
- .filter((index) => index.name.startsWith("session_"))
- .map((index) => ({ name: index.name, unique: index.unique }))
- .sort((a, b) => a.name.localeCompare(b.name)),
- ).toEqual([
- { name: "session_pending_session_admitted_seq_idx", unique: 1 },
- { name: "session_pending_session_compaction_idx", unique: 1 },
- { name: "session_pending_session_delivery_seq_idx", unique: 0 },
- ])
- }),
- )
- })
- test("resets incompatible projected Session messages before adding sequence order", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY)`)
- yield* db.run(
- sql`CREATE TABLE message (id text PRIMARY KEY, session_id text NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(
- sql`CREATE TABLE part (id text PRIMARY KEY, message_id text NOT NULL, session_id text NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(sql`CREATE TABLE event (id text PRIMARY KEY, seq integer NOT NULL)`)
- yield* db.run(
- sql`CREATE TABLE session_message (id text PRIMARY KEY, session_id text NOT NULL, type text NOT NULL, time_created integer NOT NULL, time_updated integer NOT NULL, data text NOT NULL)`,
- )
- yield* db.run(
- sql`CREATE INDEX session_message_session_time_created_id_idx ON session_message (session_id, time_created, id)`,
- )
- yield* db.run(
- sql`CREATE INDEX session_message_session_type_time_created_id_idx ON session_message (session_id, type, time_created, id)`,
- )
- yield* db.run(sql`INSERT INTO session (id) VALUES ('session')`)
- yield* db.run(
- sql`INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES ('legacy_message', 'session', 1, 1, '{"role":"user"}')`,
- )
- yield* db.run(
- sql`INSERT INTO part (id, message_id, session_id, time_created, time_updated, data) VALUES ('legacy_part', 'legacy_message', 'session', 1, 1, '{"type":"text","text":"hello"}')`,
- )
- yield* db.run(
- sql`INSERT INTO session_message (id, session_id, type, time_created, time_updated, data) VALUES ('stale_projection', 'session', 'user', 1, 1, '{}')`,
- )
- yield* DatabaseMigration.applyOnly(db, [sessionMessageProjectionOrderMigration])
- expect(yield* db.all(sql`SELECT id, session_id, data FROM message`)).toEqual([
- { id: "legacy_message", session_id: "session", data: '{"role":"user"}' },
- ])
- expect(yield* db.all(sql`SELECT id, message_id, session_id, data FROM part`)).toEqual([
- {
- id: "legacy_part",
- message_id: "legacy_message",
- session_id: "session",
- data: '{"type":"text","text":"hello"}',
- },
- ])
- expect(yield* db.all(sql`SELECT id FROM session_message`)).toEqual([])
- yield* db.run(
- sql`INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES ('fresh_projection', 'session', 'user', 7, 2, 2, '{}')`,
- )
- expect(yield* db.get(sql`SELECT id, seq FROM session_message`)).toEqual({ id: "fresh_projection", seq: 7 })
- }),
- )
- })
- test("runs session usage backfill in order with schema changes", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, time_updated integer NOT NULL)`)
- yield* db.run(sql`CREATE TABLE message (id text PRIMARY KEY, session_id text NOT NULL, data text NOT NULL)`)
- yield* db.run(sql`INSERT INTO session (id, time_updated) VALUES ('session_1', 1)`)
- yield* db.run(
- sql`INSERT INTO message (id, session_id, data) VALUES ('message_1', 'session_1', '{"role":"assistant","cost":1.25,"tokens":{"input":2,"output":3,"reasoning":4,"cache":{"read":5,"write":6}}}')`,
- )
- yield* DatabaseMigration.applyOnly(db, [sessionUsageMigration])
- expect(
- yield* db.get(
- sql`SELECT cost, tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write FROM session WHERE id = 'session_1'`,
- ),
- ).toEqual({
- cost: 1.25,
- tokens_input: 2,
- tokens_output: 3,
- tokens_reasoning: 4,
- tokens_cache_read: 5,
- tokens_cache_write: 6,
- })
- }),
- )
- })
- test("normalizes Windows storage paths and leaves POSIX paths untouched", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE project (id text PRIMARY KEY, worktree text NOT NULL, sandboxes text NOT NULL)`)
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, directory text NOT NULL, path text)`)
- // Windows-shaped rows (drive + backslash) must be normalized.
- yield* db.run(
- sql`INSERT INTO project (id, worktree, sandboxes) VALUES (${"win"}, ${"C:\\Repo\\Thing"}, ${JSON.stringify([
- "C:\\Repo\\Thing\\sandbox",
- ])})`,
- )
- yield* db.run(
- sql`INSERT INTO session (id, directory, path) VALUES (${"win"}, ${"C:\\Repo\\Thing\\packages\\api"}, ${"packages\\api"})`,
- )
- // UNC worktrees and their sandboxes must normalize too (not just drive paths).
- yield* db.run(
- sql`INSERT INTO project (id, worktree, sandboxes) VALUES (${"unc"}, ${"\\\\server\\share"}, ${JSON.stringify([
- "\\\\server\\share\\sandbox",
- ])})`,
- )
- // The "/" worktree sentinel and POSIX paths (including a pathological
- // backslash in a POSIX filename) must survive byte-for-byte.
- yield* db.run(sql`INSERT INTO project (id, worktree, sandboxes) VALUES (${"global"}, ${"/"}, ${"[]"})`)
- yield* db.run(
- sql`INSERT INTO session (id, directory, path) VALUES (${"posix"}, ${"/home/me/we\\ird"}, ${"src\\weird"})`,
- )
- yield* DatabaseMigration.applyOnly(db, [normalizeStoragePathsMigration])
- expect(yield* db.get(sql`SELECT worktree, sandboxes FROM project WHERE id = 'win'`)).toEqual({
- worktree: "C:/Repo/Thing",
- sandboxes: JSON.stringify(["C:/Repo/Thing/sandbox"]),
- })
- expect(yield* db.get(sql`SELECT directory, path FROM session WHERE id = 'win'`)).toEqual({
- directory: "C:/Repo/Thing/packages/api",
- path: "packages/api",
- })
- expect(yield* db.get(sql`SELECT worktree, sandboxes FROM project WHERE id = 'unc'`)).toEqual({
- worktree: "//server/share",
- sandboxes: JSON.stringify(["//server/share/sandbox"]),
- })
- expect(yield* db.get(sql`SELECT worktree FROM project WHERE id = 'global'`)).toEqual({ worktree: "/" })
- expect(yield* db.get(sql`SELECT directory, path FROM session WHERE id = 'posix'`)).toEqual({
- directory: "/home/me/we\\ird",
- path: "src\\weird",
- })
- }),
- )
- })
- test("maps native Windows paths through database columns", async () => {
- if (process.platform !== "win32") return
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* DatabaseMigration.apply(db)
- const projectID = Project.ID.make("codec_project")
- const worktree = AbsolutePath.make("C:\\Repo\\Thing")
- const sandbox = AbsolutePath.make("C:\\Repo\\Thing\\sandbox")
- const directory = "C:\\Repo\\Thing\\packages\\api"
- const sessionID = SessionSchema.ID.make("ses_codec")
- expect(() =>
- Effect.runSync(
- db
- .insert(ProjectTable)
- .values({
- id: Project.ID.make("invalid_path"),
- worktree: AbsolutePath.make("not-absolute"),
- sandboxes: [],
- time_created: 1,
- time_updated: 1,
- })
- .run(),
- ),
- ).toThrow()
- yield* db
- .insert(ProjectTable)
- .values({
- id: projectID,
- worktree,
- sandboxes: [sandbox],
- time_created: 1,
- time_updated: 1,
- })
- .run()
- yield* db
- .insert(SessionTable)
- .values({
- id: sessionID,
- project_id: projectID,
- slug: "codec",
- directory,
- path: "packages\\api",
- title: "Codec",
- version: "test",
- time_created: 1,
- time_updated: 1,
- })
- .run()
- expect(
- yield* db.get<{ worktree: string; sandboxes: string }>(
- sql`SELECT worktree, sandboxes FROM project WHERE id = ${projectID}`,
- ),
- ).toEqual({
- worktree: "C:/Repo/Thing",
- sandboxes: JSON.stringify(["C:/Repo/Thing/sandbox"]),
- })
- expect(
- yield* db.get<{ directory: string; path: string }>(
- sql`SELECT directory, path FROM session WHERE id = ${sessionID}`,
- ),
- ).toEqual({
- directory: "C:/Repo/Thing/packages/api",
- path: "packages/api",
- })
- const project = yield* db.select().from(ProjectTable).where(eq(ProjectTable.worktree, worktree)).get()
- const session = yield* db.select().from(SessionTable).where(eq(SessionTable.directory, directory)).get()
- expect(project?.worktree).toBe(worktree)
- expect(project?.sandboxes).toEqual([sandbox])
- expect(session?.directory).toBe(directory)
- expect(session?.path).toBe("packages/api")
- expect((yield* db.select().from(SessionTable).where(eq(SessionTable.path, "packages\\api")).get())?.id).toBe(
- sessionID,
- )
- const moved = AbsolutePath.make("D:\\Moved\\Thing")
- const updated = yield* db
- .update(ProjectTable)
- .set({ worktree: moved, sandboxes: [moved] })
- .where(eq(ProjectTable.id, projectID))
- .returning()
- .get()
- expect(updated?.worktree).toBe(moved)
- expect(updated?.sandboxes).toEqual([moved])
- expect(
- yield* db.get<{ worktree: string; sandboxes: string }>(
- sql`SELECT worktree, sandboxes FROM project WHERE id = ${projectID}`,
- ),
- ).toEqual({ worktree: "D:/Moved/Thing", sandboxes: JSON.stringify(["D:/Moved/Thing"]) })
- expect(
- (yield* db
- .select()
- .from(ProjectTable)
- .where(inArray(ProjectTable.worktree, [moved]))
- .get())?.id,
- ).toBe(projectID)
- yield* db.run(sql`UPDATE project SET worktree = ${"not-absolute"} WHERE id = ${projectID}`)
- expect(() =>
- Effect.runSync(db.select().from(ProjectTable).where(eq(ProjectTable.id, projectID)).get()),
- ).toThrow()
- }),
- )
- })
- test("imports existing drizzle migration state", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(
- sql`CREATE TABLE __drizzle_migrations (id INTEGER PRIMARY KEY, hash text NOT NULL, created_at numeric, name text, applied_at TEXT)`,
- )
- yield* db.run(sql`
- INSERT INTO __drizzle_migrations (hash, created_at, name, applied_at)
- VALUES ('hash', 1, '20260127222353_familiar_lady_ursula', ${new Date().toISOString()})
- `)
- yield* DatabaseMigration.applyOnly(db, [])
- expect(yield* db.get(sql`SELECT id FROM migration`)).toEqual({ id: "20260127222353_familiar_lady_ursula" })
- }),
- )
- })
- test("does not replay a migrated session metadata column", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, metadata text)`)
- yield* db.run(
- sql`CREATE TABLE __drizzle_migrations (id INTEGER PRIMARY KEY, hash text NOT NULL, created_at numeric, name text, applied_at TEXT)`,
- )
- yield* db.run(sql`
- INSERT INTO __drizzle_migrations (hash, created_at, name, applied_at)
- VALUES ('hash', 1, '20260511173437_session-metadata', ${new Date().toISOString()})
- `)
- yield* DatabaseMigration.applyOnly(db, [sessionMetadataMigration])
- expect(yield* db.all(sql`SELECT id FROM migration`)).toEqual([{ id: "20260511173437_session-metadata" }])
- }),
- )
- })
- test("accepts the temporary replacement session metadata migration id", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE session (id text PRIMARY KEY, metadata text)`)
- yield* db.run(sql`CREATE TABLE migration (id TEXT PRIMARY KEY, time_completed INTEGER NOT NULL)`)
- yield* db.run(sql`INSERT INTO migration (id, time_completed) VALUES ('20260530232709_lovely_romulus', 1)`)
- yield* DatabaseMigration.applyOnly(db, [sessionMetadataMigration])
- expect(yield* db.all(sql`SELECT id FROM migration ORDER BY id`)).toEqual([
- { id: "20260511173437_session-metadata" },
- { id: "20260530232709_lovely_romulus" },
- ])
- }),
- )
- })
- test("skips drizzle import when migration table already has state", async () => {
- await run(
- Effect.gen(function* () {
- const db = yield* makeDb
- yield* db.run(sql`CREATE TABLE migration (id TEXT PRIMARY KEY, time_completed INTEGER NOT NULL)`)
- yield* db.run(sql`INSERT INTO migration (id, time_completed) VALUES ('existing', 1)`)
- yield* db.run(
- sql`CREATE TABLE __drizzle_migrations (id INTEGER PRIMARY KEY, hash text NOT NULL, created_at numeric, name text, applied_at TEXT)`,
- )
- yield* db.run(sql`
- INSERT INTO __drizzle_migrations (hash, created_at, name, applied_at)
- VALUES ('hash', 1, '20260127222353_familiar_lady_ursula', ${new Date().toISOString()})
- `)
- yield* DatabaseMigration.applyOnly(db, [])
- expect(yield* db.all(sql`SELECT id FROM migration ORDER BY id`)).toEqual([{ id: "existing" }])
- }),
- )
- })
- })
|