User: In vaultdb I want to refactor the database layer so we can swap between Postgres and SQLite for tests without touching core logic. Right now the core modules import from src/adapters/postgres.ts directly.

Agent: [Bash: grep -rn "from.*postgres" src/core/]

```
src/core/session-store.ts:3: import { pgPool } from "../adapters/postgres.js";
src/core/fact-store.ts:2: import { pgPool } from "../adapters/postgres.js";
src/core/search.ts:5: import { pgPool } from "../adapters/postgres.js";
```

Three core modules import Postgres directly. The fix is to define a DbAdapter interface in the ports layer and have the core modules depend on that interface rather than the concrete Postgres adapter. The Postgres and SQLite implementations both implement the interface.

User: Yes. Let's extract the interface to src/ports/db-adapter.ts.

Agent: I'll first enumerate what operations the core modules actually call on pgPool, then define only those in the interface.

[Read: src/core/session-store.ts]
[Read: src/core/fact-store.ts]
[Read: src/core/search.ts]

Operations used: `query(sql, params)` returning rows, `transaction(fn)` for multi-statement writes. That's it.

[Write: src/ports/db-adapter.ts]

```typescript
export interface QueryResult<T = Record<string, unknown>> {
  readonly rows: ReadonlyArray<T>;
}

export interface DbAdapter {
  query<T = Record<string, unknown>>(
    sql: string,
    params?: ReadonlyArray<unknown>,
  ): Promise<QueryResult<T>>;
  transaction<T>(fn: (db: DbAdapter) => Promise<T>): Promise<T>;
}
```

[Edit: src/adapters/postgres.ts]

Implemented DbAdapter on the existing PgAdapter class. Added `transaction()` using `BEGIN` / `COMMIT` / `ROLLBACK` calls.

[Write: src/adapters/sqlite.ts]

New SQLite adapter implementing DbAdapter for use in tests using better-sqlite3.

[Edit: src/core/session-store.ts]
[Edit: src/core/fact-store.ts]
[Edit: src/core/search.ts]

Replaced `import { pgPool }` with `import type { DbAdapter } from "../ports/db-adapter.js"` in each. Each module now receives the adapter through its constructor or init function rather than importing it directly.

User: Where do we wire the concrete adapter?

Agent: In src/app.ts, which is the composition root. That's the only place that imports PgAdapter. In tests, SqliteAdapter is passed instead.

User: Can you verify no core module imports from adapters directly after this refactor?

Agent: [Bash: grep -rn "from.*adapters" src/core/]

No output. Core is clean.

User: Good. Run the full test suite.

Agent: [Bash: npm test]

```
Test Suites: 8 passed, 8 total
Tests:       61 passed, 61 total
```

All green. The DbAdapter interface is in the ports layer; SQL stays in adapters, not in core.
