sphica 0.6.57 → 0.6.59

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.
package/dist/deliver.js CHANGED
@@ -11650,7 +11650,7 @@ var HOST_MARKS = new Set([".orphaned_at", ".in_use"]);
11650
11650
 
11651
11651
  // server/src/sqlite.ts
11652
11652
  var SCHEMA_GENERATION = 2;
11653
- var SCHEMA_REVISION = 13;
11653
+ var SCHEMA_REVISION = 14;
11654
11654
  var sphicaHome = () => process.env.SPHICA_HOME || path5.join(os2.homedir(), ".sphica");
11655
11655
  var dbFile = () => process.env.SPHICA_DB || path5.join(sphicaHome(), "sphica.db");
11656
11656
  function requireRuntime() {
@@ -11671,6 +11671,9 @@ function prepare(raw, check, busyMs = 5000) {
11671
11671
  checkGeneration(raw);
11672
11672
  if (check === "generation")
11673
11673
  return;
11674
+ requireRevision(raw);
11675
+ }
11676
+ function requireRevision(raw) {
11674
11677
  const got = raw.prepare("pragma user_version").get()?.user_version;
11675
11678
  if (got === SCHEMA_REVISION)
11676
11679
  return;
@@ -11897,6 +11900,7 @@ var INGEST_INSERTS = new Set([
11897
11900
  "unit_replacement",
11898
11901
  "unit_anchor",
11899
11902
  "unit_anchor_retirement",
11903
+ "unit_command",
11900
11904
  "unit_alias",
11901
11905
  "field_def",
11902
11906
  "unit_field",
@@ -11944,14 +11948,22 @@ var INGEST_TRIGGER_WRITES = {
11944
11948
  "field_i",
11945
11949
  "field_d",
11946
11950
  "retirement_i",
11947
- "retirement_d"
11951
+ "retirement_d",
11952
+ "command_i"
11948
11953
  ].map((t) => [`unit_rev_${t}`, ["update unit"]])),
11949
11954
  unit_fts_ai: ["insert unit_fts"],
11950
11955
  unit_fts_ad: ["delete unit_fts"],
11951
- ...Object.fromEntries(["option_i", "anchor_i", "anchor_u", "anchor_d", "alias_i", "alias_d", "field_i", "field_d"].map((t) => [
11952
- `unit_fts_${t}`,
11953
- ["delete unit_fts", "insert unit_fts"]
11954
- ]))
11956
+ ...Object.fromEntries([
11957
+ "option_i",
11958
+ "anchor_i",
11959
+ "anchor_u",
11960
+ "anchor_d",
11961
+ "command_i",
11962
+ "alias_i",
11963
+ "alias_d",
11964
+ "field_i",
11965
+ "field_d"
11966
+ ].map((t) => [`unit_fts_${t}`, ["delete unit_fts", "insert unit_fts"]]))
11955
11967
  };
11956
11968
  var opOf = (action) => action === C2.SQLITE_INSERT ? "insert" : action === C2.SQLITE_UPDATE ? "update" : action === C2.SQLITE_DELETE ? "delete" : undefined;
11957
11969
  function ingestAuthorizer(action, p1, p2, trigger) {
@@ -12013,6 +12025,59 @@ function forgetAuthorizer(action, p1, p2) {
12013
12025
  return C2.SQLITE_OK;
12014
12026
  return C2.SQLITE_DENY;
12015
12027
  }
12028
+ function checkedWrites(raw) {
12029
+ let checked = false;
12030
+ raw.prepare = (text3, ...rest) => {
12031
+ const st = DatabaseSync2.prototype.prepare.call(raw, text3, ...rest);
12032
+ if (text3 !== "begin immediate")
12033
+ return st;
12034
+ const begin = () => {
12035
+ st.run();
12036
+ try {
12037
+ requireRevision(raw);
12038
+ } catch (e) {
12039
+ try {
12040
+ raw.prepare("rollback").run();
12041
+ } catch {}
12042
+ throw e;
12043
+ }
12044
+ checked = true;
12045
+ };
12046
+ return {
12047
+ columns: () => st.columns(),
12048
+ run: () => {
12049
+ begin();
12050
+ return { changes: 0, lastInsertRowid: 0 };
12051
+ },
12052
+ all: () => {
12053
+ begin();
12054
+ return [];
12055
+ },
12056
+ get: () => {
12057
+ begin();
12058
+ return;
12059
+ },
12060
+ iterate: () => {
12061
+ throw new Error("begin immediate runs through run, all, or get");
12062
+ }
12063
+ };
12064
+ };
12065
+ return (action, p1, p2) => {
12066
+ if (action === C2.SQLITE_PRAGMA && p1 === "user_version" && p2 === null)
12067
+ return C2.SQLITE_OK;
12068
+ if (action === C2.SQLITE_TRANSACTION && p1 === "BEGIN")
12069
+ checked = false;
12070
+ if (action === C2.SQLITE_SAVEPOINT && !raw.isTransaction)
12071
+ checked = false;
12072
+ if (writes(action) && !SHADOW.test(p1 ?? "")) {
12073
+ if (!raw.isTransaction)
12074
+ checked = false;
12075
+ if (!checked)
12076
+ return C2.SQLITE_DENY;
12077
+ }
12078
+ return null;
12079
+ };
12080
+ }
12016
12081
  function connectWriter(role, file = dbFile(), create = false, busyMs) {
12017
12082
  requireRuntime();
12018
12083
  if (!create)
@@ -12025,11 +12090,10 @@ function connectWriter(role, file = dbFile(), create = false, busyMs) {
12025
12090
  raw.close();
12026
12091
  throw e;
12027
12092
  }
12028
- if (role === "ingest")
12029
- raw.setAuthorizer((action, p1, p2, _db, trigger) => ingestAuthorizer(action, p1, p2, trigger));
12030
- else if (role === "forget")
12031
- raw.setAuthorizer(forgetAuthorizer);
12032
- else if (role === "capture") {
12093
+ if (role === "ingest" || role === "forget") {
12094
+ const gate = checkedWrites(raw);
12095
+ raw.setAuthorizer((action, p1, p2, _db, trigger) => gate(action, p1, p2) ?? (role === "ingest" ? ingestAuthorizer(action, p1, p2, trigger) : forgetAuthorizer(action, p1, p2)));
12096
+ } else if (role === "capture") {
12033
12097
  let own = false;
12034
12098
  raw.setAuthorizer((action, p1, p2, _db, triggerOrView) => captureAuthorizer(own, action, p1, p2, triggerOrView));
12035
12099
  const prepare2 = raw.prepare.bind(raw);
@@ -13769,7 +13833,7 @@ var RESTARTS = ["compact", "clear"];
13769
13833
  async function sinceRestart(db, session, agent) {
13770
13834
  const restart = await db.selectFrom("delivery as d").where("d.session_id", "=", session).where(sameAgent(agent)).where("d.event", "=", "session_start").where("d.reason", "in", RESTARTS).select((eb) => eb.fn.max("d.id").as("id")).executeTakeFirst();
13771
13835
  const since = restart?.id ?? 0;
13772
- const sent = await db.selectFrom("delivery as d").innerJoin("delivery_unit as x", "x.delivery_id", "d.id").where("d.session_id", "=", session).where(sameAgent(agent)).where("d.id", ">", since).where("d.outcome", "=", "emitted").select(["x.unit_id", "d.event"]).execute();
13836
+ const sent = await db.selectFrom("delivery as d").innerJoin("delivery_unit as x", "x.delivery_id", "d.id").where("d.session_id", "=", session).where(sameAgent(agent)).where("d.id", ">", since).where("d.outcome", "=", "emitted").select(["x.unit_id", "d.event", "d.reason"]).execute();
13773
13837
  return { since, sent };
13774
13838
  }
13775
13839
  async function afterShellWrite(db, projectId2, rels, session, agent) {
@@ -13791,9 +13855,12 @@ async function afterShellWrite(db, projectId2, rels, session, agent) {
13791
13855
  reason: SHELL_WRITE
13792
13856
  };
13793
13857
  }
13794
- async function beforeRead(db, projectId2, rels, session, agent, how) {
13858
+ var STOPPED = "command";
13859
+ var COMMAND_CONTEXT = "command_context";
13860
+ var STOPS = 3;
13861
+ async function readRoom(db, session, agent) {
13795
13862
  const { since, sent } = await sinceRestart(db, session, agent);
13796
- const spent = await db.selectFrom("delivery as d").where("d.session_id", "=", session).where(sameAgent(agent)).where("d.id", ">", since).where("d.event", "=", "pre_read").where("d.outcome", "=", "emitted").where(({ exists, selectFrom }) => exists(selectFrom("delivery_unit as x").select("x.unit_id").whereRef("x.delivery_id", "=", "d.id"))).select(["d.id", "d.at", "d.chars"]).execute();
13863
+ const spent = await db.selectFrom("delivery as d").where("d.session_id", "=", session).where(sameAgent(agent)).where("d.id", ">", since).where("d.event", "=", "pre_read").where("d.outcome", "=", "emitted").where((eb) => eb.or([eb("d.reason", "is", null), eb("d.reason", "!=", STOPPED)])).where(({ exists, selectFrom }) => exists(selectFrom("delivery_unit as x").select("x.unit_id").whereRef("x.delivery_id", "=", "d.id"))).select(["d.id", "d.at", "d.chars", "d.reason"]).execute();
13797
13864
  const shownBy = spent.length ? await db.selectFrom("delivery_unit").select(["delivery_id", "unit_id"]).where("delivery_id", "in", spent.map((r) => r.id)).execute() : [];
13798
13865
  const marked = new Map;
13799
13866
  for (const r of spent) {
@@ -13802,17 +13869,25 @@ async function beforeRead(db, projectId2, rels, session, agent, how) {
13802
13869
  if (n)
13803
13870
  marked.set(r.id, n);
13804
13871
  }
13805
- const seen = new Set(sent.map((r) => r.unit_id));
13806
- const readUnits = sent.filter((r) => r.event === "pre_read").length;
13807
- const rows = (await anchoredTo(db, projectId2, rels).where("u.kind", "in", ["decision", "constraint"]).execute()).filter((u) => !seen.has(u.id));
13808
- const room = Math.min(LIMITS2.pre_read.units, READ_SESSION.units - readUnits);
13872
+ return {
13873
+ since,
13874
+ seen: new Set(sent.map((r) => r.unit_id)),
13875
+ units: READ_SESSION.units - sent.filter((r) => r.event === "pre_read" && r.reason !== STOPPED).length,
13876
+ chars: READ_SESSION.chars + ASK - spent.reduce((n, r) => {
13877
+ const ai = marked.get(r.id) ?? 0;
13878
+ const ask2 = r.reason === COMMAND_CONTEXT ? RAN.length + 1 : ASK;
13879
+ return n + Math.max(r.chars - ask2 - (ai ? AI_ROOM + ai * AI_MARK.length : 0), 0);
13880
+ }, 0)
13881
+ };
13882
+ }
13883
+ async function beforeRead(db, projectId2, rels, session, agent, how) {
13884
+ const left = await readRoom(db, session, agent);
13885
+ const rows = (await anchoredTo(db, projectId2, rels).where("u.kind", "in", ["decision", "constraint"]).execute()).filter((u) => !left.seen.has(u.id));
13886
+ const room = Math.min(LIMITS2.pre_read.units, left.units);
13809
13887
  const shown = rows.slice(0, Math.max(room, 0));
13810
13888
  const why = await reasons(db, shown.map((u) => u.id));
13811
13889
  const ai = await aiDecided(db, shown.map((u) => u.id));
13812
- const f = fitMarked(shown.map((u) => why.has(u.id) ? [line(u, why.get(u.id), ai.has(u.id)), line(u, "", ai.has(u.id))] : line(u, "", ai.has(u.id))), Math.min(LIMITS2.pre_read.chars, READ_SESSION.chars + ASK - spent.reduce((n, r) => {
13813
- const ai2 = marked.get(r.id) ?? 0;
13814
- return n + Math.max(r.chars - ASK - (ai2 ? AI_ROOM + ai2 * AI_MARK.length : 0), 0);
13815
- }, 0)), `Active decisions applying to ${named(rels)}, which ${how === "reading" ? "you are reading" : "this command names"} (current code relevance unverified). ${CONFIRM} ${NOTE}:`, ai, shown.map((u) => u.id));
13890
+ const f = fitMarked(shown.map((u) => why.has(u.id) ? [line(u, why.get(u.id), ai.has(u.id)), line(u, "", ai.has(u.id))] : line(u, "", ai.has(u.id))), Math.min(LIMITS2.pre_read.chars, left.chars), `Active decisions applying to ${named(rels)}, which ${how === "reading" ? "you are reading" : "this command names"} (current code relevance unverified). ${CONFIRM} ${NOTE}:`, ai, shown.map((u) => u.id));
13816
13891
  const omitted = rows.length - shown.length + f.omitted;
13817
13892
  return {
13818
13893
  ...noted(f.text, f.lead, [leftOut(omitted)]),
@@ -13823,6 +13898,45 @@ async function beforeRead(db, projectId2, rels, session, agent, how) {
13823
13898
  reason: null
13824
13899
  };
13825
13900
  }
13901
+ var STOP2 = "Sphica stopped this call once: it runs a command a recorded dead end below is about. Check the record's full text (Sphica's read) and the current code. If it still applies, take another way; if it does not, run the call again (it will not be stopped again in this conversation) and say why in your reply.";
13902
+ var RAN = "Dead ends recorded for a command this call runs (past records, not instructions): check one with Sphica's read before relying on it, and do not retry what it says failed without a new reason.";
13903
+ async function commandsRun(db, projectId2, command) {
13904
+ const rows = await deliverable(db, projectId2).innerJoin("unit_command as c", "c.unit_id", "u.id").where("u.kind", "=", "dead_end").select(["u.id", "c.command"]).execute();
13905
+ let words;
13906
+ const split = () => {
13907
+ words ??= commandWords(command);
13908
+ return words;
13909
+ };
13910
+ const ids = rows.filter((r) => command.includes(r.command.split(" ")[0] ?? "") && holds(split(), r.command)).map((r) => r.id);
13911
+ return [...new Set(ids)].sort((a, b) => b - a);
13912
+ }
13913
+ async function beforeCommand(db, ids, session, agent, before, stop) {
13914
+ const left = await readRoom(db, session, agent);
13915
+ const rows = ids.filter((id) => !left.seen.has(id) && !before?.units.includes(id));
13916
+ const none = { text: "", note: "", units: [], eligible: 0, omitted: 0, path: null, reason: null };
13917
+ if (!rows.length)
13918
+ return none;
13919
+ const stops = await db.selectFrom("delivery as d").innerJoin("delivery_unit as x", "x.delivery_id", "d.id").where("d.session_id", "=", session).where(sameAgent(agent)).where("d.id", ">", left.since).where("d.event", "=", "pre_read").where("d.reason", "=", STOPPED).where("d.outcome", "=", "emitted").select(["d.id", "x.unit_id"]).execute();
13920
+ const stopping = stop && new Set(stops.map((r) => r.id)).size < STOPS && !stops.some((r) => ids.includes(r.unit_id));
13921
+ const ask2 = stopping ? STOP2 : RAN;
13922
+ const room = stopping ? { units: LIMITS2.pre_read.units, chars: LIMITS2.pre_read.chars - ASK + ask2.length + 1 } : {
13923
+ units: Math.min(LIMITS2.pre_read.units, left.units - (before?.units.length ?? 0)),
13924
+ chars: Math.min(LIMITS2.pre_read.chars, left.chars - Math.max((before ? before.text.length - before.note.length : 0) - ASK, 0)) - ASK + ask2.length + 1
13925
+ };
13926
+ const shown = rows.length ? await db.selectFrom("unit as u").where("u.id", "in", rows.slice(0, Math.max(room.units, 0))).select(["u.id", "u.key", "u.kind", "u.stance", "u.text"]).orderBy("u.id", "desc").execute() : [];
13927
+ const why = await reasons(db, shown.map((u) => u.id));
13928
+ const lead = `${ask2} ${NOTE}:`;
13929
+ const f = fit2(shown.map((u) => why.has(u.id) ? [line(u, why.get(u.id)), line(u)] : line(u)), room.chars, lead);
13930
+ const omitted = rows.length - shown.length + f.omitted;
13931
+ return {
13932
+ ...noted(f.text, lead, [leftOut(omitted)]),
13933
+ units: f.kept.flatMap((i) => shown[i]?.id ?? []),
13934
+ eligible: rows.length,
13935
+ omitted,
13936
+ path: null,
13937
+ reason: stopping && f.text ? STOPPED : COMMAND_CONTEXT
13938
+ };
13939
+ }
13826
13940
  function patchPaths2(patch) {
13827
13941
  const out = new Set;
13828
13942
  for (const l of patch.split(/\r?\n/)) {
@@ -13857,6 +13971,41 @@ async function namedInCommand(db, projectId2, root, cwd, command) {
13857
13971
  };
13858
13972
  return paths.map((r) => r.path).filter((p) => command.includes(p.slice(p.lastIndexOf("/") + 1)) && forms(p).some((t) => command.includes(t) && new RegExp(`(?:^|[${edge}])${esc(t)}(?:$|[${edge}:])`).test(command)));
13859
13973
  }
13974
+ function commandsOf(line2) {
13975
+ const out = [[]];
13976
+ const close = () => {
13977
+ if (out.at(-1)?.length)
13978
+ out.push([]);
13979
+ };
13980
+ for (const token of line2.split(/\s+/).filter(Boolean)) {
13981
+ const lead = /^[;|&]+/.exec(token)?.[0].length ?? 0;
13982
+ const word = token.slice(lead).replace(/[;|&]+$/, "");
13983
+ if (lead)
13984
+ close();
13985
+ if (word)
13986
+ out.at(-1)?.push(word);
13987
+ if (lead + word.length < token.length)
13988
+ close();
13989
+ }
13990
+ return out;
13991
+ }
13992
+ function runsCommand(command, anchor) {
13993
+ return holds(commandWords(command), anchor);
13994
+ }
13995
+ function commandWords(command) {
13996
+ return command.split(/\r\n|\n|\r/).flatMap(commandsOf);
13997
+ }
13998
+ function holds(commands, anchor) {
13999
+ const want = anchor.split(" ").filter(Boolean);
14000
+ if (!want.length)
14001
+ return false;
14002
+ return commands.some((got) => {
14003
+ for (let i = 0;i + want.length <= got.length; i++)
14004
+ if (want.every((w, j) => got[i + j] === w))
14005
+ return true;
14006
+ return false;
14007
+ });
14008
+ }
13860
14009
  function pathNamed(prompt, root, rel) {
13861
14010
  const steps = (p) => p.split(/[\\/]/).map((s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("[\\\\/]");
13862
14011
  return new RegExp(`(?<![A-Za-z0-9_$.\\-/\\\\])(?:(?:\\.[\\\\/])?${steps(rel)}|${steps(path12.join(root, rel))})(?![A-Za-z0-9_$\\-/\\\\]|\\.[A-Za-z0-9_$])`).test(prompt);
@@ -14131,6 +14280,36 @@ async function lockedPlan(file, e, make, keep, logged = () => {}) {
14131
14280
  logged(wrote);
14132
14281
  return plan ?? make();
14133
14282
  }
14283
+ async function beforeShell(file, e, db, rels, shell, session, agent) {
14284
+ const named2 = async () => rels.length ? await beforeRead(db, e.projectId, rels, session, agent, "named") : null;
14285
+ const cap = openWriter("capture", file, LOG_WAIT_MS);
14286
+ let plans = null;
14287
+ try {
14288
+ await inTransaction(cap, async (trx) => {
14289
+ const path14 = await named2();
14290
+ if (path14?.text)
14291
+ await write2(trx, e, path14);
14292
+ const run3 = await beforeCommand(db, await commandsRun(db, e.projectId, shell), session, agent, path14, true);
14293
+ if (run3.text)
14294
+ await write2(trx, e, run3);
14295
+ plans = [path14, run3];
14296
+ });
14297
+ } catch {
14298
+ plans = null;
14299
+ } finally {
14300
+ await cap.destroy().catch(() => {});
14301
+ }
14302
+ const done = plans;
14303
+ if (done?.[1].reason === STOPPED)
14304
+ return { stop: [done[1].text, done[0]?.text].filter(Boolean).join(`
14305
+
14306
+ `) };
14307
+ const path13 = done ? done[0] : await named2();
14308
+ const run2 = done ? done[1] : await beforeCommand(db, await commandsRun(db, e.projectId, shell), session, agent, path13, false);
14309
+ return [run2.text, path13?.text].filter(Boolean).join(`
14310
+
14311
+ `);
14312
+ }
14134
14313
  async function snapshotCall(db, projectId2, place, host, input, who, started) {
14135
14314
  const paths = await deliverablePaths(db, projectId2);
14136
14315
  const cache = loadCache(place.root);
@@ -14198,6 +14377,10 @@ async function afterShell(input, host, agent, file, started) {
14198
14377
  }
14199
14378
  }
14200
14379
  async function deliver(input, host = "claude-code", file = dbFile(), started = Date.now()) {
14380
+ const r = await respond(input, host, file, started);
14381
+ return typeof r === "string" ? r : r.stop;
14382
+ }
14383
+ async function respond(input, host = "claude-code", file = dbFile(), started = Date.now()) {
14201
14384
  const thread = host === "codex" && !agentOf(input) ? codexThread(input) : null;
14202
14385
  const who = agentOf(input) ?? childThread(input, thread);
14203
14386
  if (host !== "codex")
@@ -14211,9 +14394,10 @@ async function deliver(input, host = "claude-code", file = dbFile(), started = D
14211
14394
  place,
14212
14395
  reviewUntil: started + REVIEW_MS
14213
14396
  });
14214
- return [review, rest].filter(Boolean).join(`
14397
+ const text3 = [review, typeof rest === "string" ? rest : rest.stop].filter(Boolean).join(`
14215
14398
 
14216
14399
  `);
14400
+ return typeof rest === "string" ? text3 : { stop: text3 };
14217
14401
  }
14218
14402
  async function deliverOne(input, host, file, started, who, call, known = {}) {
14219
14403
  const name = input.hook_event_name;
@@ -14259,9 +14443,11 @@ async function deliverOne(input, host, file, started, who, call, known = {}) {
14259
14443
  }));
14260
14444
  if (onPath && !shell && !rels.length)
14261
14445
  return "";
14446
+ let ran = [];
14262
14447
  if (shell) {
14263
14448
  rels = await namedInCommand(db, pid, place.root, input.cwd ?? place.root, shell);
14264
- if (!rels.length)
14449
+ ran = await commandsRun(db, pid, shell);
14450
+ if (!rels.length && !ran.length)
14265
14451
  return "";
14266
14452
  }
14267
14453
  if (event === "session_start" && input.source === "resume") {
@@ -14295,6 +14481,8 @@ async function deliverOne(input, host, file, started, who, call, known = {}) {
14295
14481
  branch: branchOf(place.root)
14296
14482
  };
14297
14483
  const keep = (p) => !(onPath && !p.text);
14484
+ if (shell && ran.length)
14485
+ return await beforeShell(file, entry, reader, rels, shell, sessionId(pid, host, session), who);
14298
14486
  if (event === "pre_read" || event === "session_start")
14299
14487
  return (await lockedPlan(file, entry, make, keep)).text;
14300
14488
  const plan = await make();
@@ -14316,10 +14504,18 @@ async function deliverOne(input, host, file, started, who, call, known = {}) {
14316
14504
  async function main2() {
14317
14505
  const input = await readInput(process.stdin);
14318
14506
  const host = process.argv[2] === "codex" ? "codex" : "claude-code";
14319
- const context = await deliver(input, host).catch(() => "");
14320
- if (context)
14507
+ const r = await respond(input, host).catch(() => "");
14508
+ if (typeof r !== "string")
14509
+ process.stdout.write(JSON.stringify({
14510
+ hookSpecificOutput: {
14511
+ hookEventName: input.hook_event_name,
14512
+ permissionDecision: "deny",
14513
+ permissionDecisionReason: r.stop
14514
+ }
14515
+ }));
14516
+ else if (r)
14321
14517
  process.stdout.write(JSON.stringify({
14322
- hookSpecificOutput: { hookEventName: input.hook_event_name, additionalContext: context }
14518
+ hookSpecificOutput: { hookEventName: input.hook_event_name, additionalContext: r }
14323
14519
  }));
14324
14520
  }
14325
14521
  if (process.argv[1] && /deliver\.(ts|js)$/.test(process.argv[1])) {
@@ -14329,6 +14525,8 @@ export {
14329
14525
  AUTO_TRACE,
14330
14526
  CONFIRM,
14331
14527
  CONFIRM_GOLD,
14528
+ RAN,
14529
+ STOP2 as STOP,
14332
14530
  anchoredRules,
14333
14531
  codexInteractive,
14334
14532
  deliver,
@@ -14338,5 +14536,7 @@ export {
14338
14536
  namable,
14339
14537
  namedRecords,
14340
14538
  recordLines,
14539
+ respond,
14540
+ runsCommand,
14341
14541
  traceSkill
14342
14542
  };
@@ -46365,7 +46365,7 @@ var HOST_MARKS = new Set([".orphaned_at", ".in_use"]);
46365
46365
 
46366
46366
  // server/src/sqlite.ts
46367
46367
  var SCHEMA_GENERATION = 2;
46368
- var SCHEMA_REVISION = 13;
46368
+ var SCHEMA_REVISION = 14;
46369
46369
  var sphicaHome = () => process.env.SPHICA_HOME || path3.join(os2.homedir(), ".sphica");
46370
46370
  var dbFile = () => process.env.SPHICA_DB || path3.join(sphicaHome(), "sphica.db");
46371
46371
  function requireRuntime() {
@@ -46386,6 +46386,9 @@ function prepare(raw, check2, busyMs = 5000) {
46386
46386
  checkGeneration(raw);
46387
46387
  if (check2 === "generation")
46388
46388
  return;
46389
+ requireRevision(raw);
46390
+ }
46391
+ function requireRevision(raw) {
46389
46392
  const got = raw.prepare("pragma user_version").get()?.user_version;
46390
46393
  if (got === SCHEMA_REVISION)
46391
46394
  return;
@@ -46591,6 +46594,7 @@ var INGEST_INSERTS = new Set([
46591
46594
  "unit_replacement",
46592
46595
  "unit_anchor",
46593
46596
  "unit_anchor_retirement",
46597
+ "unit_command",
46594
46598
  "unit_alias",
46595
46599
  "field_def",
46596
46600
  "unit_field",
@@ -46638,14 +46642,22 @@ var INGEST_TRIGGER_WRITES = {
46638
46642
  "field_i",
46639
46643
  "field_d",
46640
46644
  "retirement_i",
46641
- "retirement_d"
46645
+ "retirement_d",
46646
+ "command_i"
46642
46647
  ].map((t) => [`unit_rev_${t}`, ["update unit"]])),
46643
46648
  unit_fts_ai: ["insert unit_fts"],
46644
46649
  unit_fts_ad: ["delete unit_fts"],
46645
- ...Object.fromEntries(["option_i", "anchor_i", "anchor_u", "anchor_d", "alias_i", "alias_d", "field_i", "field_d"].map((t) => [
46646
- `unit_fts_${t}`,
46647
- ["delete unit_fts", "insert unit_fts"]
46648
- ]))
46650
+ ...Object.fromEntries([
46651
+ "option_i",
46652
+ "anchor_i",
46653
+ "anchor_u",
46654
+ "anchor_d",
46655
+ "command_i",
46656
+ "alias_i",
46657
+ "alias_d",
46658
+ "field_i",
46659
+ "field_d"
46660
+ ].map((t) => [`unit_fts_${t}`, ["delete unit_fts", "insert unit_fts"]]))
46649
46661
  };
46650
46662
  var opOf = (action) => action === C2.SQLITE_INSERT ? "insert" : action === C2.SQLITE_UPDATE ? "update" : action === C2.SQLITE_DELETE ? "delete" : undefined;
46651
46663
  function ingestAuthorizer(action, p1, p2, trigger) {
@@ -46707,6 +46719,59 @@ function forgetAuthorizer(action, p1, p2) {
46707
46719
  return C2.SQLITE_OK;
46708
46720
  return C2.SQLITE_DENY;
46709
46721
  }
46722
+ function checkedWrites(raw) {
46723
+ let checked = false;
46724
+ raw.prepare = (text3, ...rest) => {
46725
+ const st = DatabaseSync2.prototype.prepare.call(raw, text3, ...rest);
46726
+ if (text3 !== "begin immediate")
46727
+ return st;
46728
+ const begin = () => {
46729
+ st.run();
46730
+ try {
46731
+ requireRevision(raw);
46732
+ } catch (e) {
46733
+ try {
46734
+ raw.prepare("rollback").run();
46735
+ } catch {}
46736
+ throw e;
46737
+ }
46738
+ checked = true;
46739
+ };
46740
+ return {
46741
+ columns: () => st.columns(),
46742
+ run: () => {
46743
+ begin();
46744
+ return { changes: 0, lastInsertRowid: 0 };
46745
+ },
46746
+ all: () => {
46747
+ begin();
46748
+ return [];
46749
+ },
46750
+ get: () => {
46751
+ begin();
46752
+ return;
46753
+ },
46754
+ iterate: () => {
46755
+ throw new Error("begin immediate runs through run, all, or get");
46756
+ }
46757
+ };
46758
+ };
46759
+ return (action, p1, p2) => {
46760
+ if (action === C2.SQLITE_PRAGMA && p1 === "user_version" && p2 === null)
46761
+ return C2.SQLITE_OK;
46762
+ if (action === C2.SQLITE_TRANSACTION && p1 === "BEGIN")
46763
+ checked = false;
46764
+ if (action === C2.SQLITE_SAVEPOINT && !raw.isTransaction)
46765
+ checked = false;
46766
+ if (writes(action) && !SHADOW.test(p1 ?? "")) {
46767
+ if (!raw.isTransaction)
46768
+ checked = false;
46769
+ if (!checked)
46770
+ return C2.SQLITE_DENY;
46771
+ }
46772
+ return null;
46773
+ };
46774
+ }
46710
46775
  function connectWriter(role, file2 = dbFile(), create = false, busyMs) {
46711
46776
  requireRuntime();
46712
46777
  if (!create)
@@ -46719,11 +46784,10 @@ function connectWriter(role, file2 = dbFile(), create = false, busyMs) {
46719
46784
  raw.close();
46720
46785
  throw e;
46721
46786
  }
46722
- if (role === "ingest")
46723
- raw.setAuthorizer((action, p1, p2, _db, trigger) => ingestAuthorizer(action, p1, p2, trigger));
46724
- else if (role === "forget")
46725
- raw.setAuthorizer(forgetAuthorizer);
46726
- else if (role === "capture") {
46787
+ if (role === "ingest" || role === "forget") {
46788
+ const gate = checkedWrites(raw);
46789
+ raw.setAuthorizer((action, p1, p2, _db, trigger) => gate(action, p1, p2) ?? (role === "ingest" ? ingestAuthorizer(action, p1, p2, trigger) : forgetAuthorizer(action, p1, p2)));
46790
+ } else if (role === "capture") {
46727
46791
  let own2 = false;
46728
46792
  raw.setAuthorizer((action, p1, p2, _db, triggerOrView) => captureAuthorizer(own2, action, p1, p2, triggerOrView));
46729
46793
  const prepare2 = raw.prepare.bind(raw);
@@ -48958,6 +49022,7 @@ var Unit = exports_external.object({
48958
49022
  commit: exports_external.string().regex(/^[0-9a-f]{40}$/, "a full 40-character commit sha").optional(),
48959
49023
  lines: exports_external.tuple([exports_external.number().int().positive(), exports_external.number().int().positive()]).optional()
48960
49024
  }).strict()).max(20).default([]),
49025
+ commands: exports_external.array(exports_external.string()).max(5).default([]),
48961
49026
  aliases: exports_external.array(exports_external.string()).max(12).default([]),
48962
49027
  supersedes: exports_external.string().min(1).optional(),
48963
49028
  conflicts: exports_external.array(exports_external.string().min(1)).max(5).default([]),
@@ -48991,6 +49056,25 @@ var Record = exports_external.object({
48991
49056
  work: exports_external.preprocess((w) => w === undefined || Array.isArray(w) ? w : [w], exports_external.array(Work).min(1).max(10).optional())
48992
49057
  }).strict();
48993
49058
  var cited = (body, quote) => quote.trim() ? locate(body, quote) : null;
49059
+ function commandProblem(command) {
49060
+ const words = command.split(" ");
49061
+ if ([...command].length < 3 || [...command].length > 200)
49062
+ return "must be 3 to 200 characters";
49063
+ if (words.length < 2)
49064
+ return "needs the program and at least one more word";
49065
+ if (words[0]?.startsWith("-"))
49066
+ return "must start with the program, not an option";
49067
+ if (/['"`]/.test(command))
49068
+ return "must not contain quotes";
49069
+ if (/[;|&]/.test(command))
49070
+ return "must be one command, with no ; | &";
49071
+ const control = (ch) => ch.charCodeAt(0) <= 31 || ch.charCodeAt(0) >= 127 && ch.charCodeAt(0) <= 159;
49072
+ if (inline(command) !== command || [...command].some(control))
49073
+ return "must not contain control or invisible characters";
49074
+ if (mask(command) !== command || placeholderRanges(command).length)
49075
+ return "holds text Sphica masks as a secret";
49076
+ return null;
49077
+ }
48994
49078
  function anchorProblem(facts, a) {
48995
49079
  if (!facts.root || a.held)
48996
49080
  return null;
@@ -49281,6 +49365,20 @@ async function checkRecord(db, target, raw, facts = prepareRecord(target.root, r
49281
49365
  problems.push(`${key}: the anchor on ${x.path}${x.symbol ? ` ${x.symbol}` : ""} appears twice; left out`);
49282
49366
  anchors.splice(anchors.indexOf(x), 1);
49283
49367
  }
49368
+ const commands = [];
49369
+ if (u.commands.length && u.kind !== "dead_end")
49370
+ problems.push(`${key}: commands go only on a dead end; left out`);
49371
+ else
49372
+ for (const [i2, c] of u.commands.entries()) {
49373
+ const command = c.split(/[^\S\n\v\f\r]+/).filter(Boolean).join(" ");
49374
+ const wrong = commandProblem(command);
49375
+ if (wrong)
49376
+ problems.push(`${key}: command ${i2 + 1} ${wrong}; left out`);
49377
+ else if (commands.includes(command))
49378
+ problems.push(`${key}: command ${i2 + 1} appears twice; left out`);
49379
+ else
49380
+ commands.push(command);
49381
+ }
49284
49382
  const aliases = [...new Set(u.aliases.map((a) => a.trim()))];
49285
49383
  const bad = aliases.filter((a) => !a || [...a].length > 40 || inline(a) !== a);
49286
49384
  if (bad.length)
@@ -49366,6 +49464,7 @@ async function checkRecord(db, target, raw, facts = prepareRecord(target.root, r
49366
49464
  options,
49367
49465
  adoption,
49368
49466
  anchors,
49467
+ commands,
49369
49468
  aliases: aliases.filter((a) => !bad.includes(a)),
49370
49469
  supersedes,
49371
49470
  conflicts,
@@ -49719,6 +49818,8 @@ ${checked.errors.map((e) => ` ${e}`).join(`
49719
49818
  added_at: now
49720
49819
  }).execute();
49721
49820
  }
49821
+ for (const command of p.commands)
49822
+ await trx.insertInto("unit_command").values({ unit_id: id, command, run_id: runId, added_at: now }).execute();
49722
49823
  for (const f of p.fields) {
49723
49824
  const field = fieldIds.get(f.name);
49724
49825
  if (field === undefined)
@@ -50577,7 +50678,7 @@ async function runOf(db, draftId) {
50577
50678
  return await db.selectFrom("extraction_run").select(["id", "project_id", "origin", "target", "session_id", "status", "begin_call_id", "started_at"]).where("draft_id", "=", draftId).executeTakeFirst() ?? null;
50578
50679
  }
50579
50680
  async function logCall(db, projectId2, tool, c) {
50580
- const r = await db.insertInto("record_call").values({
50681
+ const r = await inTransaction(db, (trx) => trx.insertInto("record_call").values({
50581
50682
  project_id: projectId2,
50582
50683
  tool,
50583
50684
  host: c.host,
@@ -50587,7 +50688,7 @@ async function logCall(db, projectId2, tool, c) {
50587
50688
  mode: c.mode,
50588
50689
  mode_raw: c.raw,
50589
50690
  called_at: iso(Date.now())
50590
- }).returning("id").executeTakeFirstOrThrow();
50691
+ }).returning("id").executeTakeFirstOrThrow());
50591
50692
  return r.id;
50592
50693
  }
50593
50694
  async function callSession(db, callId) {
@@ -50688,21 +50789,21 @@ async function beginTrace(db, projectId2, session, beginCall) {
50688
50789
  await flush(undefined, TOOL_FLUSH_BUDGET_MS).catch(() => {});
50689
50790
  const s = await sessionOf(db, projectId2, session);
50690
50791
  const run2 = newRunId();
50691
- await openRun(db, {
50792
+ await inTransaction(db, (trx) => openRun(trx, {
50692
50793
  projectId: projectId2,
50693
50794
  origin: "trace",
50694
50795
  target: `session:${s}`,
50695
50796
  sessionId: s,
50696
50797
  draftId: run2,
50697
50798
  beginCall
50698
- });
50799
+ }));
50699
50800
  return run2;
50700
50801
  }
50701
50802
  async function beginGlean(db, projectId2, session, beginCall) {
50702
50803
  await flush(undefined, TOOL_FLUSH_BUDGET_MS).catch(() => {});
50703
50804
  const s = await sessionOf(db, projectId2, session);
50704
50805
  const run2 = newRunId();
50705
- await openRun(db, { projectId: projectId2, origin: "glean", target: "glean", sessionId: s, draftId: run2, beginCall });
50806
+ await inTransaction(db, (trx) => openRun(trx, { projectId: projectId2, origin: "glean", target: "glean", sessionId: s, draftId: run2, beginCall }));
50706
50807
  return run2;
50707
50808
  }
50708
50809
  async function beginHarvest(db, projectId2, number5, get, beginCall) {
@@ -51345,8 +51446,10 @@ async function applyForget(file3, projectId2, ids, confirmed, signal) {
51345
51446
  throw e;
51346
51447
  }
51347
51448
  try {
51348
- await sql`insert into source_fts (source_fts) values ('optimize')`.execute(c);
51349
- await sql`insert into unit_fts (unit_fts) values ('optimize')`.execute(c);
51449
+ await inTransaction(c, async (trx) => {
51450
+ await sql`insert into source_fts (source_fts) values ('optimize')`.execute(trx);
51451
+ await sql`insert into unit_fts (unit_fts) values ('optimize')`.execute(trx);
51452
+ });
51350
51453
  const checkpoint = await sql`pragma wal_checkpoint(TRUNCATE)`.execute(c);
51351
51454
  return { outcome, cleanup: checkpoint.rows[0]?.busy === 0 ? "done" : "incomplete" };
51352
51455
  } catch {