@gr8ful/spf 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +15 -5
  2. package/assets/skill/references/config.md +9 -5
  3. package/assets/skill/references/observability.md +57 -12
  4. package/assets/templates/ts-opencode.spf.config.yaml +54 -0
  5. package/dist/chains/index.js +1 -1
  6. package/dist/chains/simple_sdlc.d.ts +2 -2
  7. package/dist/chains/simple_sdlc.js +13 -13
  8. package/dist/chains/steps.d.ts +2 -2
  9. package/dist/chains/steps.js +35 -19
  10. package/dist/cli/commands/abort.d.ts +1 -1
  11. package/dist/cli/commands/abort.js +30 -3
  12. package/dist/cli/commands/doctor.js +109 -8
  13. package/dist/cli/commands/estimate.js +3 -3
  14. package/dist/cli/commands/events.js +4 -4
  15. package/dist/cli/commands/fanout.js +93 -21
  16. package/dist/cli/commands/loop.js +31 -32
  17. package/dist/cli/commands/migrate.js +8 -1
  18. package/dist/cli/commands/phases.js +2 -2
  19. package/dist/cli/commands/sessions.js +2 -2
  20. package/dist/cli/commands/trace.d.ts +28 -8
  21. package/dist/cli/commands/trace.js +28 -15
  22. package/dist/cli/commands/ui.js +15 -5
  23. package/dist/cli/commands/watch.js +27 -27
  24. package/dist/cli/index.js +3 -1
  25. package/dist/cli/interview.d.ts +1 -0
  26. package/dist/cli/interview.js +86 -4
  27. package/dist/core/agent_opencode.d.ts +247 -0
  28. package/dist/core/agent_opencode.js +590 -0
  29. package/dist/core/agents.d.ts +12 -12
  30. package/dist/core/agents.js +113 -46
  31. package/dist/core/console.d.ts +12 -12
  32. package/dist/core/console.js +25 -25
  33. package/dist/core/data_types.d.ts +126 -12
  34. package/dist/core/data_types.js +101 -4
  35. package/dist/core/fanout.d.ts +1 -1
  36. package/dist/core/fanout.js +1 -1
  37. package/dist/core/gates.js +14 -1
  38. package/dist/core/paths.d.ts +41 -4
  39. package/dist/core/paths.js +32 -3
  40. package/dist/core/quality.d.ts +7 -7
  41. package/dist/core/quality.js +16 -10
  42. package/dist/core/runner.d.ts +9 -3
  43. package/dist/core/runner.js +39 -27
  44. package/dist/core/session.d.ts +2 -2
  45. package/dist/core/session.js +39 -18
  46. package/dist/core/sqlite.d.ts +14 -7
  47. package/dist/core/sqlite.js +14 -7
  48. package/dist/core/trace_db.d.ts +118 -0
  49. package/dist/core/trace_db.js +278 -0
  50. package/dist/core/tracer.d.ts +64 -34
  51. package/dist/core/tracer.js +141 -69
  52. package/dist/core/watch.d.ts +4 -4
  53. package/dist/core/watch.js +2 -2
  54. package/dist/ui/server/app.js +10 -10
  55. package/dist/ui/server/db.d.ts +89 -21
  56. package/dist/ui/server/db.js +235 -99
  57. package/dist/ui/server/serve.d.ts +5 -1
  58. package/dist/ui/server/serve.js +4 -5
  59. package/package.json +1 -1
  60. package/web/assets/index-CQ3k1Y1-.css +1 -0
  61. package/web/assets/index-CU8tom6S.js +21 -0
  62. package/web/index.html +2 -2
  63. package/web/assets/index-CRujNW-1.js +0 -11
  64. package/web/assets/index-Cto6nuQL.css +0 -1
@@ -1,13 +1,28 @@
1
1
  /**
2
- * Tracer: every event lands in JSONL and SQLite AS IT HAPPENS.
2
+ * Tracer: every event lands in JSONL and the trace db AS IT HAPPENS.
3
3
  *
4
- * Files are the raw record; spf.db is the queryable mirror the UI polls.
5
- * WAL mode so the UI can read while ADW processes write.
4
+ * Files are the raw record; the trace db (local sqlite, or remote D1 see
5
+ * `core/trace_db.ts`) is the queryable mirror the UI polls. For the LOCAL
6
+ * backend, WAL mode lets the UI read while ADW processes write, in the same
7
+ * instant, because both sides share one file on one filesystem.
8
+ *
9
+ * THE D1 BACKEND DOES NOT MAKE THAT SAME PROMISE — see `core/trace_db.ts`'s
10
+ * `D1TraceDb` doc comment for the consistency model it actually has and why
11
+ * this is a deliberate, documented trade-off (SPF #66) rather than a gap to
12
+ * close here.
6
13
  *
7
14
  * No push transport in the CONTROL flow — that is always, still, and only:
8
- * agents -> sqlite -> web ui. SQLite is the source of truth; nothing
15
+ * agents -> trace db -> web ui. The trace db is the source of truth; nothing
9
16
  * downstream of it can affect a phase, a gate, or a run outcome.
10
17
  *
18
+ * WHY EVERY WRITE METHOD BELOW IS ASYNC: the D1 backend's only way to reach
19
+ * a database from this plain Node CLI is its HTTP REST API — a network call.
20
+ * `core/trace_db.ts`'s `TraceDb` interface is async for that reason, and
21
+ * every write method here is a thin `await this.db...` over it — see that
22
+ * module's header. For the LOCAL backend this costs nothing but the syntax:
23
+ * `LocalTraceDb` wraps the exact same synchronous `Database` calls this file
24
+ * always made, immediately resolved.
25
+ *
11
26
  * The one amendment: when (and only when) `observability.otel` is configured,
12
27
  * each write method below ends with a single fan-out line to an optional
13
28
  * OtelExporter — a lossy, allowlisted PROJECTION of what was just written,
@@ -21,9 +36,9 @@
21
36
  * `processStart`/`processEnd` (pids) — have NO fan-out line on purpose. Do not
22
37
  * add one.
23
38
  */
24
- import { Database } from "./sqlite.js";
25
39
  import { appendFileSync, mkdirSync } from "node:fs";
26
40
  import path from "node:path";
41
+ import { createTraceDb } from "./trace_db.js";
27
42
  import { newId, nowIso } from "./utils.js";
28
43
  const SCHEMA = `
29
44
  CREATE TABLE IF NOT EXISTS sessions (
@@ -38,7 +53,7 @@ CREATE TABLE IF NOT EXISTS sessions (
38
53
  );
39
54
  CREATE TABLE IF NOT EXISTS phases (
40
55
  phase_id TEXT PRIMARY KEY,
41
- adw_id TEXT REFERENCES sessions,
56
+ adw_id TEXT,
42
57
  seq INTEGER,
43
58
  name TEXT, kind TEXT, owner TEXT, description TEXT,
44
59
  status TEXT DEFAULT 'fail',
@@ -48,8 +63,8 @@ CREATE TABLE IF NOT EXISTS phases (
48
63
  );
49
64
  CREATE TABLE IF NOT EXISTS events (
50
65
  event_id TEXT PRIMARY KEY,
51
- adw_id TEXT REFERENCES sessions,
52
- phase_id TEXT REFERENCES phases,
66
+ adw_id TEXT,
67
+ phase_id TEXT,
53
68
  parent_id TEXT,
54
69
  type TEXT,
55
70
  name TEXT,
@@ -59,8 +74,8 @@ CREATE TABLE IF NOT EXISTS events (
59
74
  );
60
75
  CREATE TABLE IF NOT EXISTS envelopes (
61
76
  envelope_id TEXT PRIMARY KEY,
62
- adw_id TEXT REFERENCES sessions,
63
- phase_id TEXT REFERENCES phases,
77
+ adw_id TEXT,
78
+ phase_id TEXT,
64
79
  agent TEXT,
65
80
  output_type TEXT,
66
81
  payload_json TEXT,
@@ -70,8 +85,8 @@ CREATE TABLE IF NOT EXISTS envelopes (
70
85
  );
71
86
  CREATE TABLE IF NOT EXISTS gate_results (
72
87
  id INTEGER PRIMARY KEY AUTOINCREMENT,
73
- adw_id TEXT REFERENCES sessions,
74
- phase_id TEXT REFERENCES phases,
88
+ adw_id TEXT,
89
+ phase_id TEXT,
75
90
  attempt INTEGER,
76
91
  gate TEXT,
77
92
  passed INTEGER,
@@ -81,7 +96,7 @@ CREATE TABLE IF NOT EXISTS gate_results (
81
96
  );
82
97
  CREATE TABLE IF NOT EXISTS processes (
83
98
  id INTEGER PRIMARY KEY AUTOINCREMENT,
84
- adw_id TEXT REFERENCES sessions,
99
+ adw_id TEXT,
85
100
  kind TEXT, -- 'adw' (the workflow process) | 'agent' (a coding-agent child)
86
101
  name TEXT, -- '' for the adw, the agent name for a child
87
102
  pid INTEGER,
@@ -89,7 +104,7 @@ CREATE TABLE IF NOT EXISTS processes (
89
104
  started_at TEXT, ended_at TEXT -- ended_at NULL = believed alive
90
105
  );
91
106
  CREATE TABLE IF NOT EXISTS agent_sessions (
92
- adw_id TEXT REFERENCES sessions,
107
+ adw_id TEXT,
93
108
  agent TEXT,
94
109
  coding_agent TEXT, model TEXT, color TEXT,
95
110
  session_id TEXT,
@@ -111,50 +126,72 @@ const MIGRATIONS = [
111
126
  ];
112
127
  export class Tracer {
113
128
  db;
129
+ /** Absolute local sqlite path — `null` for a `kind:"d1"` config (`Console.sessionFinished`'s "db" line falls back to `runner.ts`'s `describeObservabilityDb` for a human label in that case). */
114
130
  dbPath;
115
131
  eventsJsonl;
116
132
  /** `null` unless `observability.otel` is configured — see the header. */
117
133
  otel;
118
- constructor(dbPath, eventsJsonl, otel) {
119
- this.otel = otel ?? null;
120
- mkdirSync(path.dirname(dbPath), { recursive: true });
134
+ constructor(db, dbPath, eventsJsonl, otel) {
135
+ this.db = db;
121
136
  this.dbPath = dbPath;
122
137
  this.eventsJsonl = eventsJsonl;
138
+ this.otel = otel;
123
139
  mkdirSync(path.dirname(eventsJsonl), { recursive: true });
124
- this.db = new Database(this.dbPath);
125
- this.db.exec("PRAGMA journal_mode=WAL;");
126
- this.db.exec("PRAGMA synchronous=NORMAL;");
127
- this.db.exec("PRAGMA busy_timeout=5000;");
128
- this.db.exec(SCHEMA);
129
- this.migrate();
130
140
  }
131
141
  /**
132
- * Close the sqlite handle. A one-shot CLI process never needs this it
142
+ * Open (or create) the trace db, run schema + migrations, and return a
143
+ * ready-to-use Tracer. The one constructor path — a plain `new Tracer(...)`
144
+ * cannot do this work itself because opening a D1-backed db means awaited
145
+ * HTTP calls before the schema exists, and a constructor cannot be async.
146
+ *
147
+ * `dbConfig` accepts either a bare local sqlite path (sugar for
148
+ * `{kind:"sqlite",path}` — every test's shorthand, and what every caller
149
+ * before SPF #66 passed) or a normalized `observability.db` descriptor
150
+ * (`paths.resolveDataPaths(...).db` — sqlite or d1).
151
+ */
152
+ static async open(dbConfig, eventsJsonl, otel, traceDbOptions) {
153
+ const normalized = typeof dbConfig === "string" ? { kind: "sqlite", path: dbConfig } : dbConfig;
154
+ const db = createTraceDb(normalized, { fetchImpl: traceDbOptions?.fetchImpl });
155
+ if (normalized.kind === "sqlite") {
156
+ // WAL + the busy/synchronous pragmas below are meaningless over D1's
157
+ // HTTP API (there is no local journal file to configure), so this
158
+ // block only ever runs for the local backend.
159
+ await db.exec("PRAGMA journal_mode=WAL;");
160
+ await db.exec("PRAGMA synchronous=NORMAL;");
161
+ await db.exec("PRAGMA busy_timeout=5000;");
162
+ }
163
+ await db.exec(SCHEMA);
164
+ const tracer = new Tracer(db, normalized.kind === "sqlite" ? normalized.path : null, eventsJsonl, otel ?? null);
165
+ await tracer.migrate();
166
+ return tracer;
167
+ }
168
+ /**
169
+ * Close the trace db handle. A one-shot CLI process never needs this — it
133
170
  * exits right after its one Tracer anyway — but `spf watch`'s daemon loop
134
- * builds a fresh Tracer (and a fresh `new Database(dbPath)`) per claimed
135
- * issue, in-process, for the life of the daemon; without this, every
136
- * issue's handle stayed open forever. Called from `session.ts`'s
137
- * `finalize()`, once a run's own dispatch has fully settled — see its
138
- * comment for why that timing is safe.
171
+ * builds a fresh Tracer (and a fresh local db connection, or a fresh
172
+ * stateless D1 client) per claimed issue, in-process, for the life of the
173
+ * daemon; without this, every issue's local handle stayed open forever.
174
+ * Called from `session.ts`'s `finalize()`, once a run's own dispatch has
175
+ * fully settled — see its comment for why that timing is safe. A no-op for
176
+ * `D1TraceDb` (stateless HTTP — nothing to release).
139
177
  */
140
- close() {
141
- this.db.close();
178
+ async close() {
179
+ await this.db.close();
142
180
  }
143
181
  /** Additive column migrations, so a db from an older SPF still opens. */
144
- migrate() {
182
+ async migrate() {
145
183
  for (const [table, column, decl] of MIGRATIONS) {
146
- const columns = new Set(this.db.query(`PRAGMA table_info(${table})`).all().map((row) => row.name));
184
+ const columns = new Set((await this.db.query(`PRAGMA table_info(${table})`).all()).map((row) => row.name));
147
185
  if (!columns.has(column)) {
148
- this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${decl}`);
186
+ await this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${decl}`);
149
187
  }
150
188
  }
151
189
  }
152
190
  /**
153
- * The ONE door to the optional otel projection, and the only reason a fan-out
154
- * line is safe to put at the end of a synchronous write method: it is a
155
- * no-op when unconfigured, and it swallows everything. An exporter bug, a
156
- * malformed span, an exhausted queue none of it may ever surface as a
157
- * failed phase, because export is not allowed to dispose of anything. The
191
+ * The ONE door to the optional otel projection. It is a no-op when
192
+ * unconfigured, and it swallows everything: an exporter bug, a malformed
193
+ * span, an exhausted queue — none of it may ever surface as a failed
194
+ * phase, because export is not allowed to dispose of anything. The
158
195
  * exporter's own methods are synchronous enqueues; the network happens later,
159
196
  * on an unref'd timer.
160
197
  */
@@ -171,12 +208,12 @@ export class Tracer {
171
208
  }
172
209
  }
173
210
  // ── events ──────────────────────────────────────────────────────────────
174
- event(record) {
211
+ async event(record) {
175
212
  const eventId = `evt_${newId(12)}`;
176
213
  const ts = nowIso();
177
214
  const line = { event_id: eventId, ts, ...record };
178
215
  appendFileSync(this.eventsJsonl, JSON.stringify(line) + "\n");
179
- this.db
216
+ await this.db
180
217
  .query(`INSERT INTO events (event_id, adw_id, phase_id, parent_id, type, name,
181
218
  payload_json, tokens, started_at, ended_at) VALUES (?,?,?,?,?,?,?,?,?,?)`)
182
219
  .run(eventId, record.adw_id, record.phase_id, record.parent_id, record.type, record.name, JSON.stringify(record.payload), record.tokens ?? null, record.started_at || ts, record.ended_at ?? null);
@@ -184,37 +221,72 @@ export class Tracer {
184
221
  return eventId;
185
222
  }
186
223
  // ── sessions ────────────────────────────────────────────────────────────
187
- sessionStart(adwId, engineer, adwName) {
224
+ async sessionStart(adwId, engineer, adwName) {
188
225
  const startedAt = nowIso();
189
- this.db
226
+ await this.db
190
227
  .query(`INSERT INTO sessions (adw_id, status, engineer, started_at) VALUES (?,?,?,?)
191
228
  ON CONFLICT(adw_id) DO UPDATE SET status='running'`)
192
229
  .run(adwId, "running", engineer, startedAt);
193
230
  if (adwName) {
194
231
  // A joined session chains ADWs — record each distinct one, in run order.
195
- const row = this.db.query("SELECT adw_name FROM sessions WHERE adw_id=?").get(adwId);
232
+ const row = (await this.db.query("SELECT adw_name FROM sessions WHERE adw_id=?").get(adwId));
196
233
  const names = row?.adw_name ? row.adw_name.split(" + ") : [];
197
234
  if (!names.includes(adwName)) {
198
235
  names.push(adwName);
199
- this.db.query("UPDATE sessions SET adw_name=? WHERE adw_id=?").run(names.join(" + "), adwId);
236
+ await this.db.query("UPDATE sessions SET adw_name=? WHERE adw_id=?").run(names.join(" + "), adwId);
200
237
  }
201
238
  }
202
239
  // otel projection: the run's clock only. `engineer` is a person's name —
203
240
  // outside the allowlist, and not a measure of anything.
204
241
  this.fanOut((otel) => otel.recordSessionStart(startedAt));
205
242
  }
206
- sessionRequest(adwId, request) {
207
- this.db.query("UPDATE sessions SET request=? WHERE adw_id=?").run(request.slice(0, 500), adwId);
243
+ async sessionRequest(adwId, request) {
244
+ await this.db.query("UPDATE sessions SET request=? WHERE adw_id=?").run(request.slice(0, 500), adwId);
208
245
  }
209
- sessionFinish(adwId, ok) {
210
- this.db
246
+ async sessionFinish(adwId, ok) {
247
+ // otel projection FIRST — synchronously, before ANY `await` in this
248
+ // method, including the ones below. `session.ts`'s `handleSignal` fires
249
+ // this method un-awaited and then calls `otel.flushAll()` -> `drain()`
250
+ // in that SAME synchronous turn (no microtask has run yet). `drain()`
251
+ // emits a placeholder "incomplete" root span the instant `rootEmitted`
252
+ // is still false, and `OtelExporter.emitRootSpan` latches on that flag —
253
+ // so once the placeholder fires, the real verdict below is silently
254
+ // dropped forever. `recordSessionFinish` -> `emitRootSpan` is a pure,
255
+ // synchronous, in-memory queue push (see otel.ts): safe to fire before
256
+ // either write below has actually landed, and it must fire here, not
257
+ // after them, to win that race unconditionally on both backends.
258
+ this.fanOut((otel) => otel.recordSessionFinish(ok)); // otel projection — see fanOut
259
+ // `processesEndAll` is CALLED (not awaited) before the sessions-row
260
+ // update below, on purpose: `session.ts`'s SIGINT/SIGTERM handler fires
261
+ // this method un-awaited and, on the local backend with no otel/notify
262
+ // configured, calls `process.exit()` in the very same synchronous turn —
263
+ // which never drains the microtask queue. `LocalTraceDb`'s writes happen
264
+ // synchronously the moment `.run(...)` is CALLED (only the `await` after
265
+ // it is deferred), so calling this first guarantees its write has
266
+ // already landed by the time this function reaches its own first
267
+ // `await`, exactly like the sessions-row write below. Awaiting it only
268
+ // after starting the sessions-row update would put it entirely after
269
+ // that update's `await`, i.e. in a microtask `process.exit()` never
270
+ // reaches — see session.ts's `handleSignal` for the full trace of why.
271
+ const processesDone = this.processesEndAll(adwId); // nothing of this run is alive any more
272
+ // Attach a handler to `processesDone` IMMEDIATELY — not after the
273
+ // sessions-row write below — so it can never become an unhandled
274
+ // rejection (fatal on Node 22). If the sessions-row write throws first,
275
+ // execution never reaches the `await processesDone` further down; without
276
+ // this line, `processesDone`'s own eventual rejection would then have no
277
+ // attached handler at all. `processesResult` resolves to the error
278
+ // (never rethrows here) so both writes stay independently handled; it is
279
+ // re-thrown below only once the sessions-row write has itself succeeded.
280
+ const processesResult = processesDone.then(() => null, (err) => err);
281
+ await this.db
211
282
  .query("UPDATE sessions SET status=?, ended_at=? WHERE adw_id=?")
212
283
  .run(ok ? "success" : "fail", nowIso(), adwId);
213
- this.processesEndAll(adwId); // nothing of this run is alive any more
214
- this.fanOut((otel) => otel.recordSessionFinish(ok)); // otel projection: emits the root run span, once
284
+ const processesError = await processesResult;
285
+ if (processesError)
286
+ throw processesError;
215
287
  }
216
- sessionAddUsage(adwId, tokens, cost) {
217
- this.db
288
+ async sessionAddUsage(adwId, tokens, cost) {
289
+ await this.db
218
290
  .query("UPDATE sessions SET total_tokens=total_tokens+?, total_cost=total_cost+? WHERE adw_id=?")
219
291
  .run(tokens, cost, adwId);
220
292
  }
@@ -227,22 +299,22 @@ export class Tracer {
227
299
  * belongs to. Writing it here makes the trace the answer to "what is this
228
300
  * run running, and how do I stop it".
229
301
  */
230
- processStart(adwId, kind, name, pid, command) {
231
- this.db
302
+ async processStart(adwId, kind, name, pid, command) {
303
+ await this.db
232
304
  .query(`INSERT INTO processes (adw_id, kind, name, pid, command, started_at) VALUES (?,?,?,?,?,?)`)
233
305
  .run(adwId, kind, name, pid, command.slice(0, 500), nowIso());
234
306
  }
235
307
  /** Mark the newest live row for this pid as finished. */
236
- processEnd(adwId, pid) {
237
- this.db
308
+ async processEnd(adwId, pid) {
309
+ await this.db
238
310
  .query(`UPDATE processes SET ended_at=? WHERE id = (
239
311
  SELECT id FROM processes WHERE adw_id=? AND pid=? AND ended_at IS NULL
240
312
  ORDER BY id DESC LIMIT 1)`)
241
313
  .run(nowIso(), adwId, pid);
242
314
  }
243
315
  /** Close out every live row for a run — called when the session ends. */
244
- processesEndAll(adwId) {
245
- this.db.query("UPDATE processes SET ended_at=? WHERE adw_id=? AND ended_at IS NULL").run(nowIso(), adwId);
316
+ async processesEndAll(adwId) {
317
+ await this.db.query("UPDATE processes SET ended_at=? WHERE adw_id=? AND ended_at IS NULL").run(nowIso(), adwId);
246
318
  }
247
319
  // ── phases ──────────────────────────────────────────────────────────────
248
320
  /**
@@ -253,13 +325,13 @@ export class Tracer {
253
325
  * ordering) and `phase_id` (silently overwriting a row through the
254
326
  * phase_upsert conflict clause).
255
327
  */
256
- maxPhaseSeq(adwId) {
257
- const row = this.db.query("SELECT MAX(seq) as m FROM phases WHERE adw_id = ?").get(adwId);
328
+ async maxPhaseSeq(adwId) {
329
+ const row = (await this.db.query("SELECT MAX(seq) as m FROM phases WHERE adw_id = ?").get(adwId));
258
330
  return row?.m ?? 0;
259
331
  }
260
- phaseUpsert(phase) {
332
+ async phaseUpsert(phase) {
261
333
  const p = phase.params;
262
- this.db
334
+ await this.db
263
335
  .query(`INSERT INTO phases (phase_id, adw_id, seq, name, kind, owner, description,
264
336
  status, attempt, retries, error, started_at, ended_at)
265
337
  VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)
@@ -271,15 +343,15 @@ export class Tracer {
271
343
  this.fanOut((otel) => otel.recordPhase(phase));
272
344
  }
273
345
  // ── envelopes / gates / agent sessions ──────────────────────────────────
274
- envelopeRow(phase, agent, outputType, payloadJson, valid, attempt) {
275
- this.db
346
+ async envelopeRow(phase, agent, outputType, payloadJson, valid, attempt) {
347
+ await this.db
276
348
  .query(`INSERT INTO envelopes (envelope_id, adw_id, phase_id, agent, output_type,
277
349
  payload_json, valid, attempt, created_at) VALUES (?,?,?,?,?,?,?,?,?)`)
278
350
  .run(`env_${newId(12)}`, phase.adw_id, phase.phase_id, agent, outputType, payloadJson, valid ? 1 : 0, attempt, nowIso());
279
351
  }
280
352
  /** The report carries both the verdict and the evidence behind it. */
281
- gateRow(phase, gate, report, attempt) {
282
- this.db
353
+ async gateRow(phase, gate, report, attempt) {
354
+ await this.db
283
355
  .query(`INSERT INTO gate_results (adw_id, phase_id, attempt, gate, passed,
284
356
  violations_json, checks_json, created_at) VALUES (?,?,?,?,?,?,?,?)`)
285
357
  .run(phase.adw_id, phase.phase_id, attempt, gate, report.passed ? 1 : 0, JSON.stringify(report.violations), JSON.stringify(report.checks), nowIso());
@@ -295,9 +367,9 @@ export class Tracer {
295
367
  * wants one number per agent — the latest — and a session that runs the
296
368
  * same agent twice overwrites it, exactly like model and session_id.
297
369
  */
298
- agentSessionRow(adwId, agent, sessionId, contextTokens = 0, contextWindow = 0) {
370
+ async agentSessionRow(adwId, agent, sessionId, contextTokens = 0, contextWindow = 0) {
299
371
  const ts = nowIso();
300
- this.db
372
+ await this.db
301
373
  .query(`INSERT INTO agent_sessions (adw_id, agent, coding_agent, model, color,
302
374
  session_id, context_tokens, context_window, created_at, last_used_at)
303
375
  VALUES (?,?,?,?,?,?,?,?,?,?)
@@ -87,7 +87,7 @@ export interface WatchFanoutDeps {
87
87
  /** One attempt's chain, in that attempt's own worktree — the same ctx `runChain` builds, per attempt. */
88
88
  runAttempt: (dispatch: AttemptDispatch) => Promise<number>;
89
89
  /** Gate/usage rows for one attempt's adw_id, from the SHARED db. Must not throw. */
90
- readMetrics: (adwId: string) => AttemptMetrics;
90
+ readMetrics: (adwId: string) => Promise<AttemptMetrics>;
91
91
  /**
92
92
  * True when NOT ONE of these adw_ids has a session row yet — the same db,
93
93
  * the same predicate and the same reasoning as `spf fanout`'s reuse
@@ -103,16 +103,16 @@ export interface WatchFanoutDeps {
103
103
  * rather than reporting "free" and letting a previous run's rows decide
104
104
  * this run's winner.
105
105
  */
106
- adwIdsFree: (adwIds: string[]) => boolean;
106
+ adwIdsFree: (adwIds: string[]) => Promise<boolean>;
107
107
  /** The winner's review posture, read back post-hoc — the same two fields `runChain` returns, keyed on a WINNER instead of the sole attempt. */
108
108
  reviewFor: (opts: {
109
109
  cwd: string;
110
110
  adwId: string;
111
111
  chainOptions: Record<string, string>;
112
- }) => {
112
+ }) => Promise<{
113
113
  reviewRequired: boolean;
114
114
  reviewSummary?: string;
115
- };
115
+ }>;
116
116
  }
117
117
  export interface WatchDeps {
118
118
  provider: IssueProvider;
@@ -878,7 +878,7 @@ async function runIssueFanout(deps, issue, fanout) {
878
878
  let salt = 0;
879
879
  for (; salt <= MAX_FANOUT_SALT; salt++) {
880
880
  const ids = Array.from({ length: n }, (_, i) => attemptAdwId(baseFor(issue, salt), i + 1));
881
- if (fanout.adwIdsFree(ids))
881
+ if (await fanout.adwIdsFree(ids))
882
882
  break;
883
883
  }
884
884
  const baseAdwId = salt <= MAX_FANOUT_SALT ? baseFor(issue, salt) : `issue-${issue.id}-x${newId(4)}`;
@@ -994,7 +994,7 @@ async function runIssueFanout(deps, issue, fanout) {
994
994
  deps.git.deleteLocalBranch(winner.branch);
995
995
  won = { worktree: winner.worktree, branch };
996
996
  await deps.provider.writeMarker(issue, { worktree: won.worktree, branch: won.branch, attempt: 0 });
997
- const review = fanout.reviewFor({ cwd: winner.worktree, adwId: winner.adw_id, chainOptions: deps.chainOptions });
997
+ const review = await fanout.reviewFor({ cwd: winner.worktree, adwId: winner.adw_id, chainOptions: deps.chainOptions });
998
998
  // `won` stays set from here on — openPrForWinner catches nothing, so
999
999
  // this function's own catch (below) is the only cleanup for a throw
1000
1000
  // from push/openPr/writeMarker/transition/notify inside it, exactly as
@@ -39,11 +39,11 @@ export function createApp(db, webDir) {
39
39
  await next();
40
40
  c.header("cache-control", "no-store");
41
41
  });
42
- app.get("/api/health", (c) => c.json({ ok: true, db: db.path, journal_mode: db.journalMode, sessions: db.sessionCount() }));
43
- app.get("/api/sessions", (c) => c.json(db.sessions(intQueryParam(c.req.query("limit"), 200))));
44
- app.get("/api/sessions/:adw_id", (c) => {
42
+ app.get("/api/health", async (c) => c.json({ ok: true, db: db.label, journal_mode: db.journalMode, sessions: await db.sessionCount() }));
43
+ app.get("/api/sessions", async (c) => c.json(await db.sessions(intQueryParam(c.req.query("limit"), 200))));
44
+ app.get("/api/sessions/:adw_id", async (c) => {
45
45
  const adwId = c.req.param("adw_id");
46
- const detail = db.sessionDetail(adwId);
46
+ const detail = await db.sessionDetail(adwId);
47
47
  if (!detail)
48
48
  return c.json({ error: `no session ${adwId}` }, 404);
49
49
  return c.json(detail);
@@ -56,22 +56,22 @@ export function createApp(db, webDir) {
56
56
  return c.json({ error: "invalid adw_id" }, 400);
57
57
  const body = (await c.req.json().catch(() => ({})));
58
58
  const archived = body.archived === undefined ? true : Boolean(body.archived);
59
- if (!db.setArchived(adwId, archived))
59
+ if (!(await db.setArchived(adwId, archived)))
60
60
  return c.json({ error: `no session ${adwId}` }, 404);
61
61
  return c.json({ adw_id: adwId, archived });
62
62
  });
63
- app.get("/api/sessions/:adw_id/events", (c) => c.json(db.events(c.req.param("adw_id"), intQueryParam(c.req.query("after"), 0), intQueryParam(c.req.query("limit"), 500))));
64
- app.get("/api/sessions/:adw_id/envelopes", (c) => c.json(db.envelopes(c.req.param("adw_id"))));
65
- app.get("/api/sessions/:adw_id/gates", (c) => c.json(db.gates(c.req.param("adw_id"))));
63
+ app.get("/api/sessions/:adw_id/events", async (c) => c.json(await db.events(c.req.param("adw_id"), intQueryParam(c.req.query("after"), 0), intQueryParam(c.req.query("limit"), 500))));
64
+ app.get("/api/sessions/:adw_id/envelopes", async (c) => c.json(await db.envelopes(c.req.param("adw_id"))));
65
+ app.get("/api/sessions/:adw_id/gates", async (c) => c.json(await db.gates(c.req.param("adw_id"))));
66
66
  // The exact prompts an agent was sent, read from the session dir. Files are
67
67
  // the raw record; the db has no copy of them.
68
- app.get("/api/sessions/:adw_id/agents/:agent/prompts", (c) => {
68
+ app.get("/api/sessions/:adw_id/agents/:agent/prompts", async (c) => {
69
69
  const adwId = c.req.param("adw_id");
70
70
  const agent = c.req.param("agent");
71
71
  if (!isSafeSegment(adwId) || !isSafeSegment(agent)) {
72
72
  return c.json({ error: "invalid adw_id or agent" }, 400);
73
73
  }
74
- if (!db.session(adwId))
74
+ if (!(await db.session(adwId)))
75
75
  return c.json({ error: `no session ${adwId}` }, 404);
76
76
  const dir = resolve(db.sessionsDir, adwId, agent, "prompts");
77
77
  // Defense in depth: the segment check already forbids traversal.
@@ -1,21 +1,75 @@
1
+ import type { NormalizedObservabilityDb } from "../../core/data_types.ts";
2
+ import type { DataPaths } from "../../core/paths.ts";
1
3
  import type { AgentSession, Envelope, EventsPage, GateResult, Phase, Session, SessionDetail, SessionSummary, SessionUsage } from "../shared/types.ts";
2
4
  export declare class SfDb {
3
- readonly path: string;
5
+ /** Absolute local sqlite path — `null` for a `kind:"d1"` config. */
6
+ readonly path: string | null;
7
+ /** Human label for API/console display: the sqlite path, or `d1:<database_id>`. */
8
+ readonly label: string;
4
9
  /**
5
10
  * Where the ADW session dirs live: `{data_dir}/sessions/{adw_id}/{agent}/`.
6
- * The db sits in the same data_dir (config's `observability.db` defaults to
7
- * `adws/adw_data/spf.db`), so deriving it as a sibling of the db file keeps
8
- * working when the whole data_dir is relocated.
11
+ * Passed in explicitly (from `paths.resolveDataPaths(...).sessions_dir`)
12
+ * rather than derived from the db path session JSONL/envelope artifact
13
+ * files always live on the local filesystem regardless of where the
14
+ * queryable trace mirror (`db` below) lives; see `paths.ts`'s `DataPaths`.
9
15
  */
10
16
  readonly sessionsDir: string;
17
+ /** `"n/a (d1)"` for a remote backend — journal mode is a local-file concept. */
11
18
  readonly journalMode: string;
12
19
  private readonly db;
13
- /** Opened on first archive and kept; null until then. */
20
+ private readonly dbConfig;
21
+ /** D1-only; carried from `open()`'s `options.fetchImpl` so a lazily-opened writer (`setArchived`) uses the same injected fetch a test gave the reader. */
22
+ private readonly fetchImpl;
23
+ /** Opened on first archive and kept; `null` until then. */
14
24
  private writer;
15
25
  /** Cache for optionalColumn(), keyed "table.column". Only ever false → true. */
16
26
  private readonly columnCache;
17
- /** `path` is always absolute — resolved upstream by the ui command via paths.resolveAnchor/resolveDataPaths. */
18
- constructor(path: string);
27
+ private constructor();
28
+ /**
29
+ * Open a trace db for reading. `dbConfig` accepts either a bare local
30
+ * sqlite path (sugar for `{kind:"sqlite",path}` — `--db <path>`/`SF_DB`
31
+ * CLI overrides, and every test's shorthand) or a normalized
32
+ * `observability.db` descriptor (`paths.resolveDataPaths(...).db`).
33
+ *
34
+ * `sessionsDir` should be `paths.resolveDataPaths(...).sessions_dir`
35
+ * whenever the caller has a `DataPaths` in hand; omitted, it falls back to
36
+ * the historical "sibling of the sqlite file" derivation — only valid for
37
+ * the LOCAL backend (a bare-path caller has no other sessions dir to
38
+ * offer), so it is required when `dbConfig` names a `kind:"d1"` config.
39
+ *
40
+ * `options.fetchImpl` is D1-only and exists purely so tests can drive this
41
+ * end-to-end against a mocked D1 HTTP response shape — every real caller
42
+ * omits it and gets the global `fetch`, same as `createTraceDb` itself.
43
+ */
44
+ static open(dbConfig: NormalizedObservabilityDb | string, sessionsDir?: string, options?: {
45
+ fetchImpl?: typeof fetch;
46
+ }): Promise<SfDb>;
47
+ /**
48
+ * Cheap "is there anything to read yet" check — the async equivalent of
49
+ * the `existsSync(dataPaths.db_path)` fast path every sqlite-only caller
50
+ * used before D1 existed, extended to cover the d1 case those callers
51
+ * never accounted for.
52
+ *
53
+ * `open()` deliberately THROWS a friendly, actionable error on a
54
+ * never-written db (see above) — the right behavior for `spf ui`, where
55
+ * "nothing here yet" is a misconfiguration to surface loudly. Callers that
56
+ * instead want to treat a never-written db as "empty, skip this check" —
57
+ * `spf fanout`'s collision preflight and lazy metrics reader, `spf loop`'s
58
+ * per-iteration readback, `spf estimate`'s cold-start path, `spf watch`'s
59
+ * salt-collision short-circuit — use this instead, so they behave
60
+ * identically whether the backend is local sqlite or a fresh D1 database
61
+ * that has never been written to.
62
+ *
63
+ * For a `kind:"sqlite"` db: `existsSync(dataPaths.db_path)`, unchanged.
64
+ * For a `kind:"d1"` db: the same `sqlite_master` probe `open()`'s
65
+ * friendly-error path already runs, but returning `false` instead of
66
+ * throwing when the `sessions` table isn't there yet. Any OTHER failure
67
+ * (network, auth, a malformed database_id) still throws — only "nothing
68
+ * written yet" collapses to `false`.
69
+ */
70
+ static exists(dataPaths: Pick<DataPaths, "db" | "db_path">, options?: {
71
+ fetchImpl?: typeof fetch;
72
+ }): Promise<boolean>;
19
73
  /**
20
74
  * A SELECT fragment for a column the tracer adds by migration.
21
75
  *
@@ -32,7 +86,21 @@ export declare class SfDb {
32
86
  */
33
87
  private hasColumn;
34
88
  private optionalColumn;
35
- close(): void;
89
+ /**
90
+ * D1 caps bound parameters at 100 per statement; local sqlite allows tens
91
+ * of thousands. Any query that binds a whole id list into an `IN (...)`
92
+ * clause must go through this instead of a single unbounded bind, or it
93
+ * throws past ~100 ids on the D1 backend. Chunking is harmless on the
94
+ * local backend too (just slightly less efficient), so this is used
95
+ * unconditionally rather than special-cased per backend.
96
+ *
97
+ * Each id belongs to exactly one chunk, so per-id ordering from `fn`'s own
98
+ * ORDER BY is preserved — only the relative order of rows belonging to
99
+ * *different* chunks can interleave, which none of this class's callers
100
+ * depend on (they always regroup by id afterward).
101
+ */
102
+ private chunked;
103
+ close(): Promise<void>;
36
104
  /**
37
105
  * Archive or restore a session — the only write in this process.
38
106
  *
@@ -40,12 +108,12 @@ export declare class SfDb {
40
108
  * click should wait its turn rather than fail. Returns false when the id
41
109
  * does not exist, so the route can 404 instead of silently succeeding.
42
110
  */
43
- setArchived(adwId: string, archived: boolean): boolean;
111
+ setArchived(adwId: string, archived: boolean): Promise<boolean>;
44
112
  /** Sessions, most recent first, each with its phase statuses for the progress dots. */
45
- sessions(limit?: number): SessionSummary[];
46
- session(adwId: string): Session | null;
47
- phases(adwId: string): Phase[];
48
- agentSessions(adwId: string): AgentSession[];
113
+ sessions(limit?: number): Promise<SessionSummary[]>;
114
+ session(adwId: string): Promise<Session | null>;
115
+ phases(adwId: string): Promise<Phase[]>;
116
+ agentSessions(adwId: string): Promise<AgentSession[]>;
49
117
  /**
50
118
  * Agents per session, for a set of ids at once: the agent_sessions rows plus
51
119
  * anything that has started but not finished.
@@ -57,7 +125,7 @@ export declare class SfDb {
57
125
  */
58
126
  private agentsFor;
59
127
  /** Session + phases + agents in one shot — L2 needs all three to draw lanes. */
60
- sessionDetail(adwId: string): SessionDetail | null;
128
+ sessionDetail(adwId: string): Promise<SessionDetail | null>;
61
129
  /**
62
130
  * Raw tokens read and written, beside the billed headline.
63
131
  *
@@ -70,15 +138,15 @@ export declare class SfDb {
70
138
  * material generated. The gap between them and the headline is cached
71
139
  * re-reads, which is usually most of it.
72
140
  */
73
- usage(adwId: string): SessionUsage;
141
+ usage(adwId: string): Promise<SessionUsage>;
74
142
  /**
75
143
  * The polling query. Rowid cursor, insertion order, bounded page — the same
76
144
  * mechanism serves the live tail and lazy-paged history.
77
145
  */
78
- events(adwId: string, after?: number, limit?: number): EventsPage;
79
- envelopes(adwId: string): Envelope[];
80
- gates(adwId: string): GateResult[];
81
- sessionCount(): number;
146
+ events(adwId: string, after?: number, limit?: number): Promise<EventsPage>;
147
+ envelopes(adwId: string): Promise<Envelope[]>;
148
+ gates(adwId: string): Promise<GateResult[]>;
149
+ sessionCount(): Promise<number>;
82
150
  /**
83
151
  * `spf estimate`'s one read: every session that ran EXACTLY `chainName`
84
152
  * (never a joined one — see below), each with its per-phase token totals,
@@ -100,10 +168,10 @@ export declare class SfDb {
100
168
  * first (`spf estimate`'s own sample-selection order), which is a decision
101
169
  * `estimate.ts` makes, not this method.
102
170
  */
103
- chainPhaseHistory(chainName: string): {
171
+ chainPhaseHistory(chainName: string): Promise<{
104
172
  sessions: ChainHistorySession[];
105
173
  joinedExcluded: number;
106
- };
174
+ }>;
107
175
  }
108
176
  /** One EXACT-match session's history, as `chainPhaseHistory` returns it — `spf estimate`'s raw material. */
109
177
  export interface ChainHistorySession {