Skip to content

Commit cf17809

Browse files
committed
feat(rust): Phase 2 — DI wiring, migration-journal verify, session tables
PR #17: wire the event store into the request path and lay the persistence groundwork the session/event route cutovers (PR #18+) depend on. - DI: `AppContext` now carries `Arc<dyn EventStore>`, assembled in a single composition-root `build_app_context()` (opencode-bin) over ONE shared `sqlx::SqlitePool`. PRAGMAs mirror the TS server (WAL, synchronous=NORMAL, busy_timeout, 64 MiB cache, foreign_keys) applied per-connection via SqliteConnectOptions. `AppContext::in_memory()` for tests/ephemeral runs. - `opencode-db::Database` owns the shared pool and mints the event store + session repos from it; `SqlxEventStore::from_pool` shares the pool. - Migration-journal verification ("TS migrates, Rust verifies"): boot health check reads the `migration` journal (+ read-only `__drizzle_migrations` bridge) and diffs against EXPECTED_MIGRATIONS (mirrors migration.gen.ts). Behind+journal-present is fatal with a clear message; ahead/no-journal warns. - Session-store models + repositories: `session_input` (event-sourced steering inbox) and `session_context_epoch` (per-session baseline/agent), DDL mirroring the final TS schema. Tables stay TS-owned (not auto-created). - effect now depends on db; db re-exports the event contract types and adds serde. opencode-server test uses `AppContext::in_memory()`. Verified: cargo build/clippy -D warnings/fmt/test (db 15, bin 2, server 16) and openapi-diff all green. Owner decision on migration strictness recorded. https://claude.ai/code/session_017Lo2jkGdTgRvsPG6tcwEqz
1 parent 3bf6fce commit cf17809

12 files changed

Lines changed: 952 additions & 58 deletions

File tree

‎Cargo.lock‎

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎OWNER_DECISIONS.md‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Rust migration — pendências e decisões para o dono (@danzeroum)
22

33
> GitHub Issues está desabilitado no repo, então este arquivo serve como o documento de
4-
> **pendências/decisões** para você revisar quando voltar. Trabalho autônomo em andamento no
5-
> **PR #14** (branch `claude/affectionate-davinci-05ifmg`). Roadmap: [`RUST_MIGRATION.md`](./RUST_MIGRATION.md). Prévia do EventStore (Fase 2): [`EVENTSTORE_PLAN.md`](./EVENTSTORE_PLAN.md).
4+
> **pendências/decisões** para você revisar quando voltar. Branch de trabalho `claude/affectionate-davinci-05ifmg`; PRs de cutover entram na branch de integração **`rust-migration`** (PR #14/#15/#16 já mergeados lá; **PR #17** em andamento). Roadmap: [`RUST_MIGRATION.md`](./RUST_MIGRATION.md). Prévia do EventStore (Fase 2): [`EVENTSTORE_PLAN.md`](./EVENTSTORE_PLAN.md).
65
76
## Estado atual (CI verde ✅)
87
- **Fase 0** completa: Cargo workspace (`crates/`), seam strangler-fig (proxy reverso gated por `OPENCODE_RUST_ROUTES`), OpenAPI code-first (utoipa), `xtask`, workflow `rust.yml`.
9-
- **Fase 1** em andamento: `opencode-config` (JSONC + `Config` tipado), `opencode-tools` (`read`/`glob`/`grep` nas libs do ripgrep + `process::run_command`), e o **gate de contrato `openapi-diff`** real.
10-
- Cada push verificado localmente (check / clippy -D warnings / fmt / testes) e verde no `rust` CI.
8+
- **Fase 1**: `opencode-config` (JSONC + `Config` tipado), `opencode-tools` (ripgrep libs + `process`), o **gate `openapi-diff`** estrutural, e cutovers reais `global.health`/`path.get`/`find.files`/`find.text`/`app.log`.
9+
- **Fase 2** em andamento: `opencode-db` com `SqlxEventStore` (SQLite, WAL, concorrência otimista), **injeção de dependência** (`AppContext` carrega `Arc<dyn EventStore>`, montado num `build_app_context()` com pool SQLite único compartilhado), **verificação do journal de migrations no boot** (lê `migration` + bridge read-only de `__drizzle_migrations`), e **modelos+repos** de `session_input`/`session_context_epoch`.
10+
- Cada push verificado localmente (check / clippy -D warnings / fmt / testes / openapi-diff) e verde no `rust` CI.
1111

1212
## Pendências / decisões que precisam de você
1313

@@ -31,5 +31,11 @@
3131

3232
10. **Esforço.** ~2–3× a estimativa inicial. Centro de gravidade: Fase 3 (roteador LLM, ~6 protocolos, ~3.959 L) e Fase 4 (runner; `die`/`catchDefect`/`FiberSet` → enum `TurnOutcome` + `ToolExecutor`). Risco técnico nº 1: o fluxo de controle do runner.
3333

34+
11. **Política de verificação de migrations no boot (PR #17) — confirmar.** Adotei "**TS migra, Rust verifica**": no boot, o `opencode-bin` lê o journal `migration` (com bridge *read-only* de `__drizzle_migrations`) e compara com a lista `EXPECTED_MIGRATIONS` embutida no binário (espelha `migration.gen.ts`). Política atual:
35+
- **DB atrás** do binário (faltam migrations que o Rust espera) **e** journal presente → **falha o boot** com mensagem clara (suba o server TS para migrar).
36+
- **DB à frente** (tem migrations que o Rust não conhece) → apenas **warn** (durante a coexistência o TS costuma estar à frente; falhar aqui recusaria o boot toda vez que o TS publicasse uma migration nova).
37+
- **Sem journal** (DB novo/não inicializado) → **warn** e segue (o TS é dono da criação do schema; o Rust só cria as tabelas `event`/`event_sequence` que usa).
38+
**Decisão sua:** manter "à frente = warn" (recomendado), ou ser estrito e falhar também quando à frente? E manter "atrás = fatal" só quando há journal, ou sempre? Hoje o Rust só serve rotas que não tocam o DB (health/path/find/log), então o gate é preparatório para os cutovers de sessão/evento (PR #18+).
39+
3440
## Como estou tocando
3541
Continuo a implementação autonomamente o máximo possível, mantendo o `rust` CI verde a cada push, atualizando o checklist em `RUST_MIGRATION.md`. Novas pendências/ambiguidades entram **neste arquivo**. Não mergeio o PR sem você.

‎RUST_MIGRATION.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ cargo-dist · cargo-deny. **Out of scope:** tree-sitter (TUI-only).
7272
- 🟡 Cutovers (batch PR into `rust-migration`), all contract-enforced: **`/path`** (`path.get`), **`/find/file`** (`find.files`), **`/find`** (`find.text`, reuses `grep_detailed` w/ submatches+offsets) — GET, 400 `BadRequestError`; and **`POST /log`** (`app.log`, first POST/request-body route; 400 is the mutation-route union `anyOf[effect_HttpApiError_BadRequest, InvalidRequestError]`, modeled once as `RequestError` and reusable). The gate now treats `oneOf`≡`anyOf` (order-independent unions). **Finding:** GET routes use a `BadRequestError` 400 while mutation routes use that union. Next: `config.get` (model `Config`), `app.agents`/`command.list` (enumeration logic).
7373

7474
### Phase 2 — Persistence + event core 🟡
75-
- 🟡 Event store: `opencode-events` reconciled to the real `event` columns (`id`/`aggregate_id`/`seq`/`type`/`data`); `EventStore` trait + `MemoryEventStore` + **`SqlxEventStore`** (sqlx + SQLite, reuses the exact DDL, WAL + foreign keys, optimistic concurrency, persists on disk across reopen) + `opencode-core::Projector`/`project` — landed. Next: migration-journal verify (read the TS `migration` table + `__drizzle_migrations` bridge), `session_context_epoch`/`session_input`, then wire into `AppContext` and cut over `event`/`session` routes.
75+
- ✅ Event store: `opencode-events` reconciled to the real `event` columns (`id`/`aggregate_id`/`seq`/`type`/`data`); `EventStore` trait + `MemoryEventStore` + **`SqlxEventStore`** (sqlx + SQLite, reuses the exact DDL, WAL + foreign keys, optimistic concurrency, persists on disk across reopen) + `opencode-core::Projector`/`project`.
76+
- ✅ **Dependency injection** (PR #17): `AppContext` carries `Arc<dyn EventStore>`, assembled in a single composition-root `build_app_context()` over **one shared `sqlx::SqlitePool`** (TS PRAGMAs: WAL + `synchronous=NORMAL` + busy-timeout + 64 MiB cache + foreign keys, applied per-connection); `AppContext::in_memory()` for tests. `Database` mints the event store + session repos from the shared pool.
77+
- ✅ **Migration-journal verification** (PR #17, "TS migrates / Rust verifies"): boot health check reads the `migration` journal (+ **read-only** `__drizzle_migrations` bridge) and diffs against `EXPECTED_MIGRATIONS` (mirrors `migration.gen.ts`). Behind+journal-present → fatal with a clear message; ahead/no-journal → warn. Strictness recorded as an owner decision.
78+
- ✅ **Session-store tables** (PR #17): `session_input` (event-sourced steering inbox) + `session_context_epoch` (per-session baseline/agent) models + repositories in `opencode-db`, DDL mirroring the final TS schema. Next: first session/event **read** route cutover (e.g. `session.list`) wiring a repo through `AppContext`.
7679
- ⬜ Channels (`watch`/`async-broadcast`), SSE `/event`
7780
- ⬜ Cutover: `agent`, `project`, `project-copy`, `credential`, `model`, `provider`, then `event`
7881

‎crates/opencode-bin/Cargo.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ path = "src/main.rs"
1313
[dependencies]
1414
opencode-server.workspace = true
1515
opencode-effect.workspace = true
16+
opencode-db.workspace = true
1617

1718
tokio.workspace = true
1819
clap.workspace = true
1920
tracing.workspace = true
2021
anyhow.workspace = true
22+
23+
[dev-dependencies]
24+
tempfile.workspace = true
25+
serde_json.workspace = true

‎crates/opencode-bin/src/main.rs‎

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
//! `opencode` binary (Rust). Boots the axum server that fronts the strangler-fig reverse proxy:
22
//! native routes (per `OPENCODE_RUST_ROUTES`) are served by Rust, the rest proxied to the TS server.
3+
//!
4+
//! [`build_app_context`] is the composition root (the Rust analog of Effect's `Layer.provide`): it
5+
//! opens the single shared SQLite pool, verifies the migration journal, and wires the services into
6+
//! the [`AppContext`] threaded through axum.
37
8+
use std::path::{Path, PathBuf};
49
use std::sync::Arc;
510

611
use clap::Parser;
12+
use opencode_db::Database;
713
use opencode_effect::{init_tracing, AppContext};
814
use opencode_server::{proxy::Upstream, RouteTable, ServerState};
915

@@ -21,24 +27,151 @@ struct Cli {
2127
default_value = "http://127.0.0.1:4097"
2228
)]
2329
upstream: String,
30+
31+
/// SQLite database. `:memory:` for ephemeral; an absolute path is used as-is; a relative path is
32+
/// resolved under the opencode data dir. Defaults to `<data-dir>/opencode.db` (mirrors the TS
33+
/// `OPENCODE_DB` flag).
34+
#[arg(long, env = "OPENCODE_DB")]
35+
db: Option<String>,
36+
}
37+
38+
/// The opencode data directory (`$XDG_DATA_HOME/opencode`, falling back to `~/.local/share/opencode`),
39+
/// mirroring `packages/core/src/global.ts`.
40+
fn data_dir() -> PathBuf {
41+
if let Ok(xdg) = std::env::var("XDG_DATA_HOME") {
42+
if !xdg.is_empty() {
43+
return PathBuf::from(xdg).join("opencode");
44+
}
45+
}
46+
let home = std::env::var("HOME")
47+
.or_else(|_| std::env::var("USERPROFILE"))
48+
.unwrap_or_else(|_| ".".to_string());
49+
PathBuf::from(home).join(".local/share/opencode")
50+
}
51+
52+
/// Resolve the database path from the `--db`/`OPENCODE_DB` flag, mirroring TS `Database.path()`:
53+
/// `:memory:` and absolute paths pass through; a relative value is joined under `data_dir`; an absent
54+
/// flag defaults to `<data_dir>/opencode.db`.
55+
fn resolve_db_path(flag: Option<&str>, data_dir: &Path) -> PathBuf {
56+
match flag {
57+
Some(":memory:") => PathBuf::from(":memory:"),
58+
Some(f) if Path::new(f).is_absolute() => PathBuf::from(f),
59+
Some(f) => data_dir.join(f),
60+
None => data_dir.join("opencode.db"),
61+
}
62+
}
63+
64+
/// Composition root: open the shared pool, verify the migration journal, and build the [`AppContext`].
65+
///
66+
/// Migration policy ("TS migrates, Rust verifies"): a database that is **behind** this build (missing
67+
/// expected migrations) is fatal — the TypeScript server must apply migrations first. A database
68+
/// **ahead** of this build, or one with no journal yet, only warns.
69+
async fn build_app_context(db_path: &Path) -> anyhow::Result<AppContext> {
70+
if let Some(parent) = db_path.parent() {
71+
if !parent.as_os_str().is_empty() && db_path.as_os_str() != ":memory:" {
72+
std::fs::create_dir_all(parent).ok();
73+
}
74+
}
75+
let db = Database::connect(db_path).await?;
76+
77+
let report = db.verify_migrations().await?;
78+
if report.journal_present && report.is_behind() {
79+
anyhow::bail!(
80+
"database at {} is behind this opencode build: missing {} migration(s), starting with `{}`. \
81+
Start the TypeScript opencode server to apply migrations (it owns the schema during the Rust migration).",
82+
db_path.display(),
83+
report.missing.len(),
84+
report.missing.first().map(String::as_str).unwrap_or("?"),
85+
);
86+
}
87+
if report.is_ahead() {
88+
tracing::warn!(
89+
newer = report.unknown.len(),
90+
first = report.unknown.first().map(String::as_str).unwrap_or(""),
91+
"database has migrations newer than this opencode build; native routes assume this build's schema",
92+
);
93+
}
94+
if !report.journal_present {
95+
tracing::warn!(
96+
db = %db_path.display(),
97+
"database has no migration journal (fresh/uninitialized); the TypeScript server owns schema creation",
98+
);
99+
} else if report.in_sync() {
100+
tracing::info!(
101+
applied = report.applied.len(),
102+
"database migrations verified"
103+
);
104+
}
105+
106+
Ok(AppContext::new(db.event_store()))
24107
}
25108

26109
#[tokio::main]
27110
async fn main() -> anyhow::Result<()> {
28111
init_tracing();
29112
let cli = Cli::parse();
30113

114+
let db_path = resolve_db_path(cli.db.as_deref(), &data_dir());
115+
let ctx = build_app_context(&db_path).await?;
116+
31117
let state = ServerState {
32-
ctx: AppContext::new(),
118+
ctx,
33119
routes: RouteTable::from_env(),
34120
proxy: Arc::new(Upstream::new(cli.upstream.clone())),
35121
};
36122
tracing::info!(
37123
bind = %cli.bind,
38124
upstream = %cli.upstream,
125+
db = %db_path.display(),
39126
native_routes = state.routes.len(),
40127
"starting opencode (rust)"
41128
);
42129

43130
opencode_server::serve(state, &cli.bind).await
44131
}
132+
133+
#[cfg(test)]
134+
mod tests {
135+
use super::*;
136+
137+
#[test]
138+
fn resolve_db_path_handles_memory_absolute_and_relative() {
139+
let data = Path::new("/data/opencode");
140+
assert_eq!(
141+
resolve_db_path(Some(":memory:"), data),
142+
PathBuf::from(":memory:")
143+
);
144+
assert_eq!(
145+
resolve_db_path(Some("/abs/custom.db"), data),
146+
PathBuf::from("/abs/custom.db")
147+
);
148+
assert_eq!(
149+
resolve_db_path(Some("custom.db"), data),
150+
PathBuf::from("/data/opencode/custom.db")
151+
);
152+
assert_eq!(
153+
resolve_db_path(None, data),
154+
PathBuf::from("/data/opencode/opencode.db")
155+
);
156+
}
157+
158+
#[tokio::test]
159+
async fn build_app_context_wires_a_working_event_store() {
160+
// A fresh file DB has no journal → warns (not fatal) and yields a usable event store.
161+
let dir = tempfile::tempdir().unwrap();
162+
let ctx = build_app_context(&dir.path().join("opencode.db"))
163+
.await
164+
.unwrap();
165+
let store = ctx.event_store();
166+
let head = store
167+
.append(
168+
"ses_1",
169+
0,
170+
vec![opencode_db::EventInput::new("x", serde_json::Value::Null)],
171+
)
172+
.await
173+
.unwrap();
174+
assert_eq!(head, 1);
175+
assert_eq!(store.head_seq("ses_1").await.unwrap(), 1);
176+
}
177+
}

‎crates/opencode-db/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ description = "opencode persistence: sqlx pool, migration runner, event-store/se
88

99
[dependencies]
1010
opencode-events.workspace = true
11+
serde.workspace = true
1112
serde_json.workspace = true
1213
thiserror.workspace = true
1314
anyhow.workspace = true

0 commit comments

Comments
 (0)