Skip to content

Commit c20b0e7

Browse files
agent-trace-db: Enable Turso sync for trace storage
Allow database specs to opt in to Turso Cloud sync and enable it for the Agent Trace DB. Disable empty remote bootstrapping for sync connections and use non-AUTOINCREMENT integer primary keys for compatibility with synced trace tables. Co-authored-by: SCE <sce@crocoder.dev>
1 parent 94d0ffd commit c20b0e7

10 files changed

Lines changed: 63 additions & 10 deletions

File tree

cli/migrations/agent-trace/001_create_diff_traces.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CREATE TABLE IF NOT EXISTS diff_traces (
2-
id INTEGER PRIMARY KEY AUTOINCREMENT,
2+
id INTEGER PRIMARY KEY,
33
time_ms INTEGER NOT NULL,
44
session_id TEXT NOT NULL,
55
patch TEXT NOT NULL,

cli/migrations/agent-trace/002_create_post_commit_patch_intersections.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CREATE TABLE IF NOT EXISTS post_commit_patch_intersections (
2-
id INTEGER PRIMARY KEY AUTOINCREMENT,
2+
id INTEGER PRIMARY KEY,
33
commit_id TEXT NOT NULL,
44
post_commit_time_ms INTEGER NOT NULL,
55
recent_window_cutoff_ms INTEGER NOT NULL,

cli/migrations/agent-trace/004_create_agent_traces.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CREATE TABLE IF NOT EXISTS agent_traces (
2-
id INTEGER PRIMARY KEY AUTOINCREMENT,
2+
id INTEGER PRIMARY KEY,
33
commit_id TEXT NOT NULL,
44
commit_time_ms INTEGER NOT NULL,
55
trace_json TEXT NOT NULL,

cli/src/services/agent_trace_db/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ impl DbSpec for AgentTraceDbSpec {
8888
fn migrations() -> &'static [(&'static str, &'static str)] {
8989
AGENT_TRACE_MIGRATIONS
9090
}
91+
92+
fn sync_enabled() -> bool {
93+
true
94+
}
9195
}
9296

9397
/// Agent trace Turso database adapter.

cli/src/services/db/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ pub trait DbSpec {
3434

3535
/// Ordered embedded migration SQL files as `(id, sql)` pairs.
3636
fn migrations() -> &'static [(&'static str, &'static str)];
37+
38+
/// Whether this database may use Turso Cloud sync mode when sync env vars
39+
/// are configured.
40+
fn sync_enabled() -> bool {
41+
false
42+
}
3743
}
3844

3945
/// Collect common filesystem health problems for a Turso database path.
@@ -186,8 +192,12 @@ impl<M: DbSpec> TursoDb<M> {
186192
format!("failed to create {db_name} tokio runtime. Try: rerun the command; if the issue persists, verify the local Tokio runtime environment.")
187193
})?;
188194

189-
let sync_url = std::env::var(crate::services::config::SYNC_URL_ENV_KEY).ok();
190-
let sync_token = std::env::var(crate::services::config::SYNC_TOKEN_ENV_KEY).ok();
195+
let sync_url = M::sync_enabled()
196+
.then(|| std::env::var(crate::services::config::SYNC_URL_ENV_KEY).ok())
197+
.flatten();
198+
let sync_token = M::sync_enabled()
199+
.then(|| std::env::var(crate::services::config::SYNC_TOKEN_ENV_KEY).ok())
200+
.flatten();
191201

192202
let path_str = db_path.to_str().ok_or_else(|| {
193203
anyhow::anyhow!("invalid UTF-8 in database path: {}", db_path.display())
@@ -198,6 +208,7 @@ impl<M: DbSpec> TursoDb<M> {
198208
let sync_db = turso::sync::Builder::new_remote(path_str)
199209
.with_remote_url(url)
200210
.with_auth_token(token)
211+
.bootstrap_if_empty(false)
201212
.build()
202213
.await
203214
.map_err(|e| {

cli/src/services/setup/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl RuntimeCommand for SetupCommand {
5454
for provider in &providers {
5555
let outcome = provider
5656
.setup(&ctx)
57-
.map_err(|error| ClassifiedError::runtime(error.to_string()))?;
57+
.map_err(|error| ClassifiedError::runtime(format!("{error:#}")))?;
5858

5959
if let Some(ref hooks_outcome) = outcome.required_hooks_install {
6060
sections.push(setup::format_required_hook_install_success_message(

cli/src/services/sync/command.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ impl RuntimeCommand for SyncCommand {
1515
}
1616

1717
fn execute(&self, _context: &AppContext) -> Result<String, ClassifiedError> {
18-
sync::run_sync(self.subcommand).map_err(|error| ClassifiedError::runtime(error.to_string()))
18+
sync::run_sync(self.subcommand)
19+
.map_err(|error| ClassifiedError::runtime(format!("{error:#}")))
1920
}
2021
}
2122

context/sce/agent-trace-db.md

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

flake.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,8 @@
10711071
typescript-language-server
10721072
vscode-json-languageserver
10731073
opencodePackage
1074+
openssl
1075+
pkg-config
10741076
rust-analyzer
10751077
scePackage
10761078
tursoPackage

manual-test-turso-sync.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Manual smoke test for Turso sync support.
5+
# Contains test credentials supplied for local manual testing only.
6+
# Do not commit this file if the token is sensitive or long-lived.
7+
8+
export DATABASE_URL=""
9+
export DATABASE_AUTH_TOKEN=""
10+
11+
export SCE_SYNC_URL="$DATABASE_URL"
12+
export SCE_SYNC_TOKEN="$DATABASE_AUTH_TOKEN"
13+
14+
STATE_DIR="$(mktemp -d)"
15+
export XDG_STATE_HOME="$STATE_DIR"
16+
17+
echo "Using isolated state dir: $XDG_STATE_HOME"
18+
19+
echo "1. Show sync command help"
20+
nix develop -c sh -c 'cd cli && cargo run -- sync --help'
21+
22+
echo "2. Run setup, including best-effort initial pull"
23+
nix develop -c sh -c 'cd cli && cargo run -- setup --opencode --non-interactive'
24+
25+
echo "3. Pull from Turso"
26+
nix develop -c sh -c 'cd cli && cargo run -- sync pull'
27+
28+
echo "4. Push to Turso"
29+
nix develop -c sh -c 'cd cli && cargo run -- sync push'
30+
31+
echo "5. Confirm local Agent Trace DB exists"
32+
test -f "$XDG_STATE_HOME/sce/agent-trace.db"
33+
ls -lh "$XDG_STATE_HOME/sce/agent-trace.db"
34+
35+
echo "Manual Turso sync smoke test passed."

0 commit comments

Comments
 (0)