sphica 0.6.56 → 0.6.58

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://anthropic.com/claude-code/plugin.schema.json",
3
3
  "name": "sphica",
4
- "version": "0.6.56",
4
+ "version": "0.6.58",
5
5
  "description": "Records Claude Code and Codex sessions on your machine and keeps past implementation and decisions, with their sources, for your agent to find.",
6
6
  "author": {
7
7
  "name": "iroha924",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sphica",
3
- "version": "0.6.56",
3
+ "version": "0.6.58",
4
4
  "description": "Records Claude Code and Codex sessions on your machine and keeps past implementation and decisions, with their sources, for your agent to find.",
5
5
  "author": {
6
6
  "name": "iroha924",
@@ -0,0 +1,61 @@
1
+ -- Copyright (c) 2026 iroha924 and contributors
2
+ -- SPDX-License-Identifier: MIT
3
+
4
+ -- Revision 12 → 13 of generation 2: a withdrawal an answer caused keeps the record that answered (unit_answer). Earlier withdrawals name
5
+ -- the answer only in their reason, so each is copied when exactly one record its run saved has the key the reason names; the others are
6
+ -- listed and left without a row. Every statement that stays is the same as in the schema at revision 13.
7
+
8
+ create temp table sphica_migration_note (rule text, item text, action text);
9
+
10
+ create table unit_answer (
11
+ state_id integer primary key not null references unit_state (id) on delete cascade,
12
+ answer_unit integer not null references unit (id) on delete cascade
13
+ ) strict;
14
+ create index unit_answer_unit on unit_answer (answer_unit);
15
+ create trigger unit_answer_check before insert on unit_answer begin
16
+ select raise(abort, 'an answer is written for the withdrawal of a question')
17
+ where not exists (select 1 from unit_state t join unit q on q.id = t.unit_id
18
+ where t.id = new.state_id and t.to_state = 'withdrawn' and q.kind = 'question');
19
+ select raise(abort, 'a question does not answer another')
20
+ where (select kind from unit where id = new.answer_unit) is 'question';
21
+ select raise(abort, 'answer and question belong to different projects')
22
+ where (select project_id from unit where id = new.answer_unit)
23
+ is not (select q.project_id from unit_state t join unit q on q.id = t.unit_id where t.id = new.state_id);
24
+ select raise(abort, 'a question is closed by a record the withdrawal''s own run saved')
25
+ where (select run_id from unit where id = new.answer_unit) is not (select run_id from unit_state where id = new.state_id);
26
+ end;
27
+ create trigger unit_answer_frozen before update on unit_answer begin
28
+ select raise(abort, 'an answer is never changed');
29
+ end;
30
+ create trigger unit_answer_no_delete before delete on unit_answer
31
+ when exists (select 1 from unit_state where id = old.state_id) and exists (select 1 from unit where id = old.answer_unit) begin
32
+ select raise(abort, 'an answer goes only with its withdrawal or its record');
33
+ end;
34
+ -- Made before the copy below, so each copied row raises the revision of its question and of its answer as a saved one does
35
+ create trigger unit_rev_answer_i after insert on unit_answer begin
36
+ update unit set revision = revision + 1
37
+ where id in (new.answer_unit, (select unit_id from unit_state where id = new.state_id));
38
+ end;
39
+
40
+ -- The reason is compared with whole keys, not cut at a separator: a key's own characters cannot end it early. substr, not like, since
41
+ -- a key's _ would match any character there. Only pairs the insert trigger accepts are picked, so the copy never stops the migration
42
+ insert into unit_answer (state_id, answer_unit)
43
+ select t.id, min(a.id)
44
+ from unit_state t
45
+ join unit q on q.id = t.unit_id
46
+ join unit a on a.project_id = q.project_id and a.run_id = t.run_id and a.kind <> 'question'
47
+ and (t.reason = 'answered by ' || a.key
48
+ or substr(t.reason, 1, length('answered by ' || a.key || ' (through ')) = 'answered by ' || a.key || ' (through ')
49
+ where t.to_state = 'withdrawn' and q.kind = 'question'
50
+ group by t.id
51
+ having count(*) = 1;
52
+
53
+ insert into sphica_migration_note
54
+ select 'a withdrawal whose reason names an answer, with no single record of its save to match', q.key || ' (state ' || t.id || ')',
55
+ 'left without its answer; a timeline of the answering record does not tell this withdrawal'
56
+ from unit_state t join unit q on q.id = t.unit_id
57
+ where t.to_state = 'withdrawn' and substr(t.reason, 1, 12) = 'answered by '
58
+ and not exists (select 1 from unit_answer w where w.state_id = t.id)
59
+ order by t.id;
60
+
61
+ pragma user_version = 13;
package/db/schema.sql CHANGED
@@ -8,7 +8,7 @@
8
8
  -- Four boundaries:
9
9
  -- captured sources session, source, artifact_link, edit_observation: what was said or written, never rewritten
10
10
  -- (the owner can forget chosen sources: forget_batch and source_forgotten keep what was removed, without its text)
11
- -- extracted units unit and its option, evidence, adoption, link, state, anchor, alias tables: what was decided or implemented
11
+ -- extracted units unit and its option, evidence, adoption, link, state, answer, anchor, alias tables: what was decided or implemented
12
12
  -- processing extraction_run, source_processing, harvest_run_source: what has been looked at and saved, so gaps are counted
13
13
  -- work and delivery work, delivery, delivery_unit: the current work status and what the hooks injected
14
14
  -- Every table is STRICT and every primary key is not null. Times are ISO 8601 UTC (`Date#toISOString()`); `strftime(...) is column` rejects others.
@@ -660,6 +660,33 @@ create trigger unit_state_apply after insert on unit_state begin
660
660
  update unit set lifecycle = new.to_state, revision = revision + 1 where id = new.unit_id;
661
661
  end;
662
662
 
663
+ -- Why a question was withdrawn: the record whose answer closed it, written with the withdrawal's state row. A withdrawal the owner
664
+ -- asked for has no row. The answering record is one the withdrawal's own run saved, so a save closes questions only with what it wrote.
665
+ create table unit_answer (
666
+ state_id integer primary key not null references unit_state (id) on delete cascade,
667
+ answer_unit integer not null references unit (id) on delete cascade
668
+ ) strict;
669
+ create index unit_answer_unit on unit_answer (answer_unit);
670
+ create trigger unit_answer_check before insert on unit_answer begin
671
+ select raise(abort, 'an answer is written for the withdrawal of a question')
672
+ where not exists (select 1 from unit_state t join unit q on q.id = t.unit_id
673
+ where t.id = new.state_id and t.to_state = 'withdrawn' and q.kind = 'question');
674
+ select raise(abort, 'a question does not answer another')
675
+ where (select kind from unit where id = new.answer_unit) is 'question';
676
+ select raise(abort, 'answer and question belong to different projects')
677
+ where (select project_id from unit where id = new.answer_unit)
678
+ is not (select q.project_id from unit_state t join unit q on q.id = t.unit_id where t.id = new.state_id);
679
+ select raise(abort, 'a question is closed by a record the withdrawal''s own run saved')
680
+ where (select run_id from unit where id = new.answer_unit) is not (select run_id from unit_state where id = new.state_id);
681
+ end;
682
+ create trigger unit_answer_frozen before update on unit_answer begin
683
+ select raise(abort, 'an answer is never changed');
684
+ end;
685
+ create trigger unit_answer_no_delete before delete on unit_answer
686
+ when exists (select 1 from unit_state where id = old.state_id) and exists (select 1 from unit where id = old.answer_unit) begin
687
+ select raise(abort, 'an answer goes only with its withdrawal or its record');
688
+ end;
689
+
663
690
  -- Where a unit applies in code, or code cited as evidence. Validated against the working tree when served, never cached here.
664
691
  create table unit_anchor (
665
692
  id integer primary key autoincrement not null,
@@ -1052,6 +1079,10 @@ end;
1052
1079
  create trigger unit_rev_link_u after update on unit_link begin
1053
1080
  update unit set revision = revision + 1 where id in (new.from_unit, new.to_unit);
1054
1081
  end;
1082
+ create trigger unit_rev_answer_i after insert on unit_answer begin
1083
+ update unit set revision = revision + 1
1084
+ where id in (new.answer_unit, (select unit_id from unit_state where id = new.state_id));
1085
+ end;
1055
1086
  create trigger unit_rev_anchor_i after insert on unit_anchor begin update unit set revision = revision + 1 where id = new.unit_id; end;
1056
1087
  create trigger unit_rev_anchor_u after update on unit_anchor begin update unit set revision = revision + 1 where id = new.unit_id; end;
1057
1088
  create trigger unit_rev_retirement_i after insert on unit_anchor_retirement begin
@@ -1270,4 +1301,4 @@ create trigger capture_delivery_prune_insert instead of insert on capture_delive
1270
1301
  order by d.at, d.id limit 200);
1271
1302
  end;
1272
1303
 
1273
- pragma user_version = 12;
1304
+ pragma user_version = 13;
package/dist/capture.js CHANGED
@@ -11587,7 +11587,7 @@ var HOST_MARKS = new Set([".orphaned_at", ".in_use"]);
11587
11587
 
11588
11588
  // server/src/sqlite.ts
11589
11589
  var SCHEMA_GENERATION = 2;
11590
- var SCHEMA_REVISION = 12;
11590
+ var SCHEMA_REVISION = 13;
11591
11591
  var sphicaHome = () => process.env.SPHICA_HOME || path4.join(os2.homedir(), ".sphica");
11592
11592
  var dbFile = () => process.env.SPHICA_DB || path4.join(sphicaHome(), "sphica.db");
11593
11593
  function requireRuntime() {
@@ -11608,6 +11608,9 @@ function prepare(raw, check, busyMs = 5000) {
11608
11608
  checkGeneration(raw);
11609
11609
  if (check === "generation")
11610
11610
  return;
11611
+ requireRevision(raw);
11612
+ }
11613
+ function requireRevision(raw) {
11611
11614
  const got = raw.prepare("pragma user_version").get()?.user_version;
11612
11615
  if (got === SCHEMA_REVISION)
11613
11616
  return;
@@ -11795,6 +11798,7 @@ var INGEST_INSERTS = new Set([
11795
11798
  "unit_adoption",
11796
11799
  "unit_link",
11797
11800
  "unit_state",
11801
+ "unit_answer",
11798
11802
  "unit_replacement",
11799
11803
  "unit_anchor",
11800
11804
  "unit_anchor_retirement",
@@ -11836,6 +11840,7 @@ var INGEST_TRIGGER_WRITES = {
11836
11840
  "adoption_d",
11837
11841
  "link_i",
11838
11842
  "link_u",
11843
+ "answer_i",
11839
11844
  "replacement_i",
11840
11845
  "replacement_u",
11841
11846
  "anchor_i",
@@ -11913,6 +11918,59 @@ function forgetAuthorizer(action, p1, p2) {
11913
11918
  return C2.SQLITE_OK;
11914
11919
  return C2.SQLITE_DENY;
11915
11920
  }
11921
+ function checkedWrites(raw) {
11922
+ let checked = false;
11923
+ raw.prepare = (text3, ...rest) => {
11924
+ const st = DatabaseSync2.prototype.prepare.call(raw, text3, ...rest);
11925
+ if (text3 !== "begin immediate")
11926
+ return st;
11927
+ const begin = () => {
11928
+ st.run();
11929
+ try {
11930
+ requireRevision(raw);
11931
+ } catch (e) {
11932
+ try {
11933
+ raw.prepare("rollback").run();
11934
+ } catch {}
11935
+ throw e;
11936
+ }
11937
+ checked = true;
11938
+ };
11939
+ return {
11940
+ columns: () => st.columns(),
11941
+ run: () => {
11942
+ begin();
11943
+ return { changes: 0, lastInsertRowid: 0 };
11944
+ },
11945
+ all: () => {
11946
+ begin();
11947
+ return [];
11948
+ },
11949
+ get: () => {
11950
+ begin();
11951
+ return;
11952
+ },
11953
+ iterate: () => {
11954
+ throw new Error("begin immediate runs through run, all, or get");
11955
+ }
11956
+ };
11957
+ };
11958
+ return (action, p1, p2) => {
11959
+ if (action === C2.SQLITE_PRAGMA && p1 === "user_version" && p2 === null)
11960
+ return C2.SQLITE_OK;
11961
+ if (action === C2.SQLITE_TRANSACTION && p1 === "BEGIN")
11962
+ checked = false;
11963
+ if (action === C2.SQLITE_SAVEPOINT && !raw.isTransaction)
11964
+ checked = false;
11965
+ if (writes(action) && !SHADOW.test(p1 ?? "")) {
11966
+ if (!raw.isTransaction)
11967
+ checked = false;
11968
+ if (!checked)
11969
+ return C2.SQLITE_DENY;
11970
+ }
11971
+ return null;
11972
+ };
11973
+ }
11916
11974
  function connectWriter(role, file = dbFile(), create = false, busyMs) {
11917
11975
  requireRuntime();
11918
11976
  if (!create)
@@ -11925,11 +11983,10 @@ function connectWriter(role, file = dbFile(), create = false, busyMs) {
11925
11983
  raw.close();
11926
11984
  throw e;
11927
11985
  }
11928
- if (role === "ingest")
11929
- raw.setAuthorizer((action, p1, p2, _db, trigger) => ingestAuthorizer(action, p1, p2, trigger));
11930
- else if (role === "forget")
11931
- raw.setAuthorizer(forgetAuthorizer);
11932
- else if (role === "capture") {
11986
+ if (role === "ingest" || role === "forget") {
11987
+ const gate = checkedWrites(raw);
11988
+ raw.setAuthorizer((action, p1, p2, _db, trigger) => gate(action, p1, p2) ?? (role === "ingest" ? ingestAuthorizer(action, p1, p2, trigger) : forgetAuthorizer(action, p1, p2)));
11989
+ } else if (role === "capture") {
11933
11990
  let own = false;
11934
11991
  raw.setAuthorizer((action, p1, p2, _db, triggerOrView) => captureAuthorizer(own, action, p1, p2, triggerOrView));
11935
11992
  const prepare2 = raw.prepare.bind(raw);
package/dist/cli.js CHANGED
@@ -5082,7 +5082,7 @@ ${indent}${caution(note)}` : ""}`);
5082
5082
 
5083
5083
  // server/src/sqlite.ts
5084
5084
  var SCHEMA_GENERATION = 2;
5085
- var SCHEMA_REVISION = 12;
5085
+ var SCHEMA_REVISION = 13;
5086
5086
  var sphicaHome = () => process.env.SPHICA_HOME || path4.join(os3.homedir(), ".sphica");
5087
5087
  var dbFile = () => process.env.SPHICA_DB || path4.join(sphicaHome(), "sphica.db");
5088
5088
  function requireRuntime() {
@@ -5103,6 +5103,9 @@ function prepare(raw, check, busyMs = 5000) {
5103
5103
  checkGeneration(raw);
5104
5104
  if (check === "generation")
5105
5105
  return;
5106
+ requireRevision(raw);
5107
+ }
5108
+ function requireRevision(raw) {
5106
5109
  const got = raw.prepare("pragma user_version").get()?.user_version;
5107
5110
  if (got === SCHEMA_REVISION)
5108
5111
  return;
@@ -16175,6 +16178,7 @@ var INGEST_INSERTS = new Set([
16175
16178
  "unit_adoption",
16176
16179
  "unit_link",
16177
16180
  "unit_state",
16181
+ "unit_answer",
16178
16182
  "unit_replacement",
16179
16183
  "unit_anchor",
16180
16184
  "unit_anchor_retirement",
@@ -16216,6 +16220,7 @@ var INGEST_TRIGGER_WRITES = {
16216
16220
  "adoption_d",
16217
16221
  "link_i",
16218
16222
  "link_u",
16223
+ "answer_i",
16219
16224
  "replacement_i",
16220
16225
  "replacement_u",
16221
16226
  "anchor_i",
@@ -16293,6 +16298,59 @@ function forgetAuthorizer(action, p1, p2) {
16293
16298
  return C2.SQLITE_OK;
16294
16299
  return C2.SQLITE_DENY;
16295
16300
  }
16301
+ function checkedWrites(raw) {
16302
+ let checked = false;
16303
+ raw.prepare = (text3, ...rest) => {
16304
+ const st = DatabaseSync2.prototype.prepare.call(raw, text3, ...rest);
16305
+ if (text3 !== "begin immediate")
16306
+ return st;
16307
+ const begin = () => {
16308
+ st.run();
16309
+ try {
16310
+ requireRevision(raw);
16311
+ } catch (e) {
16312
+ try {
16313
+ raw.prepare("rollback").run();
16314
+ } catch {}
16315
+ throw e;
16316
+ }
16317
+ checked = true;
16318
+ };
16319
+ return {
16320
+ columns: () => st.columns(),
16321
+ run: () => {
16322
+ begin();
16323
+ return { changes: 0, lastInsertRowid: 0 };
16324
+ },
16325
+ all: () => {
16326
+ begin();
16327
+ return [];
16328
+ },
16329
+ get: () => {
16330
+ begin();
16331
+ return;
16332
+ },
16333
+ iterate: () => {
16334
+ throw new Error("begin immediate runs through run, all, or get");
16335
+ }
16336
+ };
16337
+ };
16338
+ return (action, p1, p2) => {
16339
+ if (action === C2.SQLITE_PRAGMA && p1 === "user_version" && p2 === null)
16340
+ return C2.SQLITE_OK;
16341
+ if (action === C2.SQLITE_TRANSACTION && p1 === "BEGIN")
16342
+ checked = false;
16343
+ if (action === C2.SQLITE_SAVEPOINT && !raw.isTransaction)
16344
+ checked = false;
16345
+ if (writes(action) && !SHADOW.test(p1 ?? "")) {
16346
+ if (!raw.isTransaction)
16347
+ checked = false;
16348
+ if (!checked)
16349
+ return C2.SQLITE_DENY;
16350
+ }
16351
+ return null;
16352
+ };
16353
+ }
16296
16354
  function connectWriter(role, file = dbFile(), create = false, busyMs) {
16297
16355
  requireRuntime();
16298
16356
  if (!create)
@@ -16305,11 +16363,10 @@ function connectWriter(role, file = dbFile(), create = false, busyMs) {
16305
16363
  raw.close();
16306
16364
  throw e;
16307
16365
  }
16308
- if (role === "ingest")
16309
- raw.setAuthorizer((action, p1, p2, _db, trigger) => ingestAuthorizer(action, p1, p2, trigger));
16310
- else if (role === "forget")
16311
- raw.setAuthorizer(forgetAuthorizer);
16312
- else if (role === "capture") {
16366
+ if (role === "ingest" || role === "forget") {
16367
+ const gate = checkedWrites(raw);
16368
+ raw.setAuthorizer((action, p1, p2, _db, trigger) => gate(action, p1, p2) ?? (role === "ingest" ? ingestAuthorizer(action, p1, p2, trigger) : forgetAuthorizer(action, p1, p2)));
16369
+ } else if (role === "capture") {
16313
16370
  let own = false;
16314
16371
  raw.setAuthorizer((action, p1, p2, _db, triggerOrView) => captureAuthorizer(own, action, p1, p2, triggerOrView));
16315
16372
  const prepare2 = raw.prepare.bind(raw);
@@ -17900,7 +17957,8 @@ function planOf(rows, asked) {
17900
17957
  const gone = ended.get(t2.unit);
17901
17958
  const reason2 = t2.from === null ? "first judged" : t2.to === "withdrawn" ? withdraw.get(t2.unit)?.reason ?? "withdrawn" : t2.to === "superseded" ? `superseded by ${keys.get(holder)}` : t2.from === "superseded" ? `${gone !== undefined ? keys.get(gone) : "its successor"} no longer replaces it` : t2.to === "active" ? hint?.reason ?? "support complete" : `${asked.because ?? "support lost"}: ${waitText(judged, t2.unit, loaded, intentOf.get(t2.unit))}`;
17902
17959
  const source = t2.to === "withdrawn" ? withdraw.get(t2.unit)?.source ?? null : t2.to === "superseded" ? asked.hints?.get(holder)?.source ?? null : hint?.source ?? null;
17903
- writes2.push({ op: "state", unit: t2.unit, from: t2.from, to: t2.to, reason: reason2, source });
17960
+ const answer = t2.to === "withdrawn" ? withdraw.get(t2.unit)?.answer ?? null : null;
17961
+ writes2.push({ op: "state", unit: t2.unit, from: t2.from, to: t2.to, reason: reason2, source, answer });
17904
17962
  }
17905
17963
  return { judged, loaded, writes: writes2, redundant, intentOf };
17906
17964
  }
@@ -19433,7 +19491,7 @@ async function init(flags) {
19433
19491
  return;
19434
19492
  }
19435
19493
  await withDb("ingest", async (db) => {
19436
- const added = await db.insertInto("project").values({ key: place.key, name: place.name }).onConflict((oc) => oc.column("key").doNothing()).returning("id").executeTakeFirst();
19494
+ const added = await inTransaction(db, (trx) => trx.insertInto("project").values({ key: place.key, name: place.name }).onConflict((oc) => oc.column("key").doNothing()).returning("id").executeTakeFirst());
19437
19495
  console.log(indent(`${mark(added ? "ok" : "none")} ${inline(place.name)} ${added ? "registered" : "already registered"} (${inline(place.key)}, ${inline(place.root)})`));
19438
19496
  });
19439
19497
  });
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 = 12;
11653
+ var SCHEMA_REVISION = 13;
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;
@@ -11893,6 +11896,7 @@ var INGEST_INSERTS = new Set([
11893
11896
  "unit_adoption",
11894
11897
  "unit_link",
11895
11898
  "unit_state",
11899
+ "unit_answer",
11896
11900
  "unit_replacement",
11897
11901
  "unit_anchor",
11898
11902
  "unit_anchor_retirement",
@@ -11934,6 +11938,7 @@ var INGEST_TRIGGER_WRITES = {
11934
11938
  "adoption_d",
11935
11939
  "link_i",
11936
11940
  "link_u",
11941
+ "answer_i",
11937
11942
  "replacement_i",
11938
11943
  "replacement_u",
11939
11944
  "anchor_i",
@@ -12011,6 +12016,59 @@ function forgetAuthorizer(action, p1, p2) {
12011
12016
  return C2.SQLITE_OK;
12012
12017
  return C2.SQLITE_DENY;
12013
12018
  }
12019
+ function checkedWrites(raw) {
12020
+ let checked = false;
12021
+ raw.prepare = (text3, ...rest) => {
12022
+ const st = DatabaseSync2.prototype.prepare.call(raw, text3, ...rest);
12023
+ if (text3 !== "begin immediate")
12024
+ return st;
12025
+ const begin = () => {
12026
+ st.run();
12027
+ try {
12028
+ requireRevision(raw);
12029
+ } catch (e) {
12030
+ try {
12031
+ raw.prepare("rollback").run();
12032
+ } catch {}
12033
+ throw e;
12034
+ }
12035
+ checked = true;
12036
+ };
12037
+ return {
12038
+ columns: () => st.columns(),
12039
+ run: () => {
12040
+ begin();
12041
+ return { changes: 0, lastInsertRowid: 0 };
12042
+ },
12043
+ all: () => {
12044
+ begin();
12045
+ return [];
12046
+ },
12047
+ get: () => {
12048
+ begin();
12049
+ return;
12050
+ },
12051
+ iterate: () => {
12052
+ throw new Error("begin immediate runs through run, all, or get");
12053
+ }
12054
+ };
12055
+ };
12056
+ return (action, p1, p2) => {
12057
+ if (action === C2.SQLITE_PRAGMA && p1 === "user_version" && p2 === null)
12058
+ return C2.SQLITE_OK;
12059
+ if (action === C2.SQLITE_TRANSACTION && p1 === "BEGIN")
12060
+ checked = false;
12061
+ if (action === C2.SQLITE_SAVEPOINT && !raw.isTransaction)
12062
+ checked = false;
12063
+ if (writes(action) && !SHADOW.test(p1 ?? "")) {
12064
+ if (!raw.isTransaction)
12065
+ checked = false;
12066
+ if (!checked)
12067
+ return C2.SQLITE_DENY;
12068
+ }
12069
+ return null;
12070
+ };
12071
+ }
12014
12072
  function connectWriter(role, file = dbFile(), create = false, busyMs) {
12015
12073
  requireRuntime();
12016
12074
  if (!create)
@@ -12023,11 +12081,10 @@ function connectWriter(role, file = dbFile(), create = false, busyMs) {
12023
12081
  raw.close();
12024
12082
  throw e;
12025
12083
  }
12026
- if (role === "ingest")
12027
- raw.setAuthorizer((action, p1, p2, _db, trigger) => ingestAuthorizer(action, p1, p2, trigger));
12028
- else if (role === "forget")
12029
- raw.setAuthorizer(forgetAuthorizer);
12030
- else if (role === "capture") {
12084
+ if (role === "ingest" || role === "forget") {
12085
+ const gate = checkedWrites(raw);
12086
+ raw.setAuthorizer((action, p1, p2, _db, trigger) => gate(action, p1, p2) ?? (role === "ingest" ? ingestAuthorizer(action, p1, p2, trigger) : forgetAuthorizer(action, p1, p2)));
12087
+ } else if (role === "capture") {
12031
12088
  let own = false;
12032
12089
  raw.setAuthorizer((action, p1, p2, _db, triggerOrView) => captureAuthorizer(own, action, p1, p2, triggerOrView));
12033
12090
  const prepare2 = raw.prepare.bind(raw);
@@ -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 = 12;
46368
+ var SCHEMA_REVISION = 13;
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;
@@ -46587,6 +46590,7 @@ var INGEST_INSERTS = new Set([
46587
46590
  "unit_adoption",
46588
46591
  "unit_link",
46589
46592
  "unit_state",
46593
+ "unit_answer",
46590
46594
  "unit_replacement",
46591
46595
  "unit_anchor",
46592
46596
  "unit_anchor_retirement",
@@ -46628,6 +46632,7 @@ var INGEST_TRIGGER_WRITES = {
46628
46632
  "adoption_d",
46629
46633
  "link_i",
46630
46634
  "link_u",
46635
+ "answer_i",
46631
46636
  "replacement_i",
46632
46637
  "replacement_u",
46633
46638
  "anchor_i",
@@ -46705,6 +46710,59 @@ function forgetAuthorizer(action, p1, p2) {
46705
46710
  return C2.SQLITE_OK;
46706
46711
  return C2.SQLITE_DENY;
46707
46712
  }
46713
+ function checkedWrites(raw) {
46714
+ let checked = false;
46715
+ raw.prepare = (text3, ...rest) => {
46716
+ const st = DatabaseSync2.prototype.prepare.call(raw, text3, ...rest);
46717
+ if (text3 !== "begin immediate")
46718
+ return st;
46719
+ const begin = () => {
46720
+ st.run();
46721
+ try {
46722
+ requireRevision(raw);
46723
+ } catch (e) {
46724
+ try {
46725
+ raw.prepare("rollback").run();
46726
+ } catch {}
46727
+ throw e;
46728
+ }
46729
+ checked = true;
46730
+ };
46731
+ return {
46732
+ columns: () => st.columns(),
46733
+ run: () => {
46734
+ begin();
46735
+ return { changes: 0, lastInsertRowid: 0 };
46736
+ },
46737
+ all: () => {
46738
+ begin();
46739
+ return [];
46740
+ },
46741
+ get: () => {
46742
+ begin();
46743
+ return;
46744
+ },
46745
+ iterate: () => {
46746
+ throw new Error("begin immediate runs through run, all, or get");
46747
+ }
46748
+ };
46749
+ };
46750
+ return (action, p1, p2) => {
46751
+ if (action === C2.SQLITE_PRAGMA && p1 === "user_version" && p2 === null)
46752
+ return C2.SQLITE_OK;
46753
+ if (action === C2.SQLITE_TRANSACTION && p1 === "BEGIN")
46754
+ checked = false;
46755
+ if (action === C2.SQLITE_SAVEPOINT && !raw.isTransaction)
46756
+ checked = false;
46757
+ if (writes(action) && !SHADOW.test(p1 ?? "")) {
46758
+ if (!raw.isTransaction)
46759
+ checked = false;
46760
+ if (!checked)
46761
+ return C2.SQLITE_DENY;
46762
+ }
46763
+ return null;
46764
+ };
46765
+ }
46708
46766
  function connectWriter(role, file2 = dbFile(), create = false, busyMs) {
46709
46767
  requireRuntime();
46710
46768
  if (!create)
@@ -46717,11 +46775,10 @@ function connectWriter(role, file2 = dbFile(), create = false, busyMs) {
46717
46775
  raw.close();
46718
46776
  throw e;
46719
46777
  }
46720
- if (role === "ingest")
46721
- raw.setAuthorizer((action, p1, p2, _db, trigger) => ingestAuthorizer(action, p1, p2, trigger));
46722
- else if (role === "forget")
46723
- raw.setAuthorizer(forgetAuthorizer);
46724
- else if (role === "capture") {
46778
+ if (role === "ingest" || role === "forget") {
46779
+ const gate = checkedWrites(raw);
46780
+ raw.setAuthorizer((action, p1, p2, _db, trigger) => gate(action, p1, p2) ?? (role === "ingest" ? ingestAuthorizer(action, p1, p2, trigger) : forgetAuthorizer(action, p1, p2)));
46781
+ } else if (role === "capture") {
46725
46782
  let own2 = false;
46726
46783
  raw.setAuthorizer((action, p1, p2, _db, triggerOrView) => captureAuthorizer(own2, action, p1, p2, triggerOrView));
46727
46784
  const prepare2 = raw.prepare.bind(raw);
@@ -48641,7 +48698,8 @@ function planOf(rows, asked) {
48641
48698
  const gone = ended.get(t.unit);
48642
48699
  const reason2 = t.from === null ? "first judged" : t.to === "withdrawn" ? withdraw.get(t.unit)?.reason ?? "withdrawn" : t.to === "superseded" ? `superseded by ${keys.get(holder)}` : t.from === "superseded" ? `${gone !== undefined ? keys.get(gone) : "its successor"} no longer replaces it` : t.to === "active" ? hint?.reason ?? "support complete" : `${asked.because ?? "support lost"}: ${waitText(judged, t.unit, loaded, intentOf.get(t.unit))}`;
48643
48700
  const source = t.to === "withdrawn" ? withdraw.get(t.unit)?.source ?? null : t.to === "superseded" ? asked.hints?.get(holder)?.source ?? null : hint?.source ?? null;
48644
- writes2.push({ op: "state", unit: t.unit, from: t.from, to: t.to, reason: reason2, source });
48701
+ const answer = t.to === "withdrawn" ? withdraw.get(t.unit)?.answer ?? null : null;
48702
+ writes2.push({ op: "state", unit: t.unit, from: t.from, to: t.to, reason: reason2, source, answer });
48645
48703
  }
48646
48704
  return { judged, loaded, writes: writes2, redundant, intentOf };
48647
48705
  }
@@ -48724,8 +48782,8 @@ async function reconcile(trx, seeds, cause, asked = {}) {
48724
48782
  await trx.updateTable("unit_replacement").set({ ended_at: sql`max(${at}, started_at)`, end_reason: w.reason, ...endBy }).where("from_unit", "=", w.from).where("to_unit", "=", w.to).where("ended_at", "is", null).execute();
48725
48783
  else if (w.op === "open")
48726
48784
  await trx.insertInto("unit_replacement").values({ from_unit: w.from, to_unit: w.to, ...by, started_at: at }).execute();
48727
- else
48728
- await trx.insertInto("unit_state").values({
48785
+ else {
48786
+ const state = await trx.insertInto("unit_state").values({
48729
48787
  unit_id: w.unit,
48730
48788
  from_state: w.from,
48731
48789
  to_state: w.to,
@@ -48733,7 +48791,10 @@ async function reconcile(trx, seeds, cause, asked = {}) {
48733
48791
  reason: w.reason,
48734
48792
  source_id: w.source,
48735
48793
  ...by
48736
- }).execute();
48794
+ }).returning("id").executeTakeFirstOrThrow();
48795
+ if (w.answer !== null)
48796
+ await trx.insertInto("unit_answer").values({ state_id: state.id, answer_unit: w.answer }).execute();
48797
+ }
48737
48798
  }
48738
48799
  proveSettled(await rowsOf(trx, ids));
48739
48800
  return settled(planned);
@@ -49482,6 +49543,7 @@ async function closeAnswered(trx, runId, units, saved) {
49482
49543
  const questions = new Map((await trx.selectFrom("unit").select(["id", "key", "lifecycle"]).where("id", "in", [...new Set(answering.flatMap((p) => p.answers))]).execute()).map((q) => [q.id, q]));
49483
49544
  const withdraw = new Map;
49484
49545
  const by = new Map;
49546
+ const written = new Map(saved.written.map((w) => [w.key, w.id]));
49485
49547
  const root = new Map;
49486
49548
  const left = new Map;
49487
49549
  for (const p of answering) {
@@ -49495,7 +49557,7 @@ async function closeAnswered(trx, runId, units, saved) {
49495
49557
  else if (why !== null || p.grounds === null)
49496
49558
  left.set(id, left.get(id) ?? `${q.key}: left open: ${why}`);
49497
49559
  else {
49498
- withdraw.set(id, { reason: `answered by ${p.key}`, source: p.grounds });
49560
+ withdraw.set(id, { reason: `answered by ${p.key}`, source: p.grounds, answer: written.get(p.key) });
49499
49561
  by.set(id, p.key);
49500
49562
  root.set(id, id);
49501
49563
  }
@@ -49520,7 +49582,8 @@ async function closeAnswered(trx, runId, units, saved) {
49520
49582
  const answer = by.get(l.from_unit);
49521
49583
  next.set(l.to_unit, {
49522
49584
  reason: `answered by ${answer} (through ${settled2.keys.get(l.from_unit)})`,
49523
- source: from.source
49585
+ source: from.source,
49586
+ answer: from.answer
49524
49587
  });
49525
49588
  by.set(l.to_unit, answer);
49526
49589
  through.set(l.to_unit, settled2.keys.get(l.from_unit) ?? `u${l.from_unit}`);
@@ -50569,7 +50632,7 @@ async function runOf(db, draftId) {
50569
50632
  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;
50570
50633
  }
50571
50634
  async function logCall(db, projectId2, tool, c) {
50572
- const r = await db.insertInto("record_call").values({
50635
+ const r = await inTransaction(db, (trx) => trx.insertInto("record_call").values({
50573
50636
  project_id: projectId2,
50574
50637
  tool,
50575
50638
  host: c.host,
@@ -50579,7 +50642,7 @@ async function logCall(db, projectId2, tool, c) {
50579
50642
  mode: c.mode,
50580
50643
  mode_raw: c.raw,
50581
50644
  called_at: iso(Date.now())
50582
- }).returning("id").executeTakeFirstOrThrow();
50645
+ }).returning("id").executeTakeFirstOrThrow());
50583
50646
  return r.id;
50584
50647
  }
50585
50648
  async function callSession(db, callId) {
@@ -50680,21 +50743,21 @@ async function beginTrace(db, projectId2, session, beginCall) {
50680
50743
  await flush(undefined, TOOL_FLUSH_BUDGET_MS).catch(() => {});
50681
50744
  const s = await sessionOf(db, projectId2, session);
50682
50745
  const run2 = newRunId();
50683
- await openRun(db, {
50746
+ await inTransaction(db, (trx) => openRun(trx, {
50684
50747
  projectId: projectId2,
50685
50748
  origin: "trace",
50686
50749
  target: `session:${s}`,
50687
50750
  sessionId: s,
50688
50751
  draftId: run2,
50689
50752
  beginCall
50690
- });
50753
+ }));
50691
50754
  return run2;
50692
50755
  }
50693
50756
  async function beginGlean(db, projectId2, session, beginCall) {
50694
50757
  await flush(undefined, TOOL_FLUSH_BUDGET_MS).catch(() => {});
50695
50758
  const s = await sessionOf(db, projectId2, session);
50696
50759
  const run2 = newRunId();
50697
- await openRun(db, { projectId: projectId2, origin: "glean", target: "glean", sessionId: s, draftId: run2, beginCall });
50760
+ await inTransaction(db, (trx) => openRun(trx, { projectId: projectId2, origin: "glean", target: "glean", sessionId: s, draftId: run2, beginCall }));
50698
50761
  return run2;
50699
50762
  }
50700
50763
  async function beginHarvest(db, projectId2, number5, get, beginCall) {
@@ -51337,8 +51400,10 @@ async function applyForget(file3, projectId2, ids, confirmed, signal) {
51337
51400
  throw e;
51338
51401
  }
51339
51402
  try {
51340
- await sql`insert into source_fts (source_fts) values ('optimize')`.execute(c);
51341
- await sql`insert into unit_fts (unit_fts) values ('optimize')`.execute(c);
51403
+ await inTransaction(c, async (trx) => {
51404
+ await sql`insert into source_fts (source_fts) values ('optimize')`.execute(trx);
51405
+ await sql`insert into unit_fts (unit_fts) values ('optimize')`.execute(trx);
51406
+ });
51342
51407
  const checkpoint = await sql`pragma wal_checkpoint(TRUNCATE)`.execute(c);
51343
51408
  return { outcome, cleanup: checkpoint.rows[0]?.busy === 0 ? "done" : "incomplete" };
51344
51409
  } catch {
package/dist/mcp.js CHANGED
@@ -46269,7 +46269,7 @@ var HOST_MARKS = new Set([".orphaned_at", ".in_use"]);
46269
46269
 
46270
46270
  // server/src/sqlite.ts
46271
46271
  var SCHEMA_GENERATION = 2;
46272
- var SCHEMA_REVISION = 12;
46272
+ var SCHEMA_REVISION = 13;
46273
46273
  var sphicaHome = () => process.env.SPHICA_HOME || path3.join(os2.homedir(), ".sphica");
46274
46274
  var dbFile = () => process.env.SPHICA_DB || path3.join(sphicaHome(), "sphica.db");
46275
46275
  function requireRuntime() {
@@ -46290,6 +46290,9 @@ function prepare(raw, check2, busyMs = 5000) {
46290
46290
  checkGeneration(raw);
46291
46291
  if (check2 === "generation")
46292
46292
  return;
46293
+ requireRevision(raw);
46294
+ }
46295
+ function requireRevision(raw) {
46293
46296
  const got = raw.prepare("pragma user_version").get()?.user_version;
46294
46297
  if (got === SCHEMA_REVISION)
46295
46298
  return;
@@ -48993,7 +48996,7 @@ async function linesOf(db, projectId2, entry) {
48993
48996
  const units = await db.selectFrom("unit").select(["id", "key", "kind", "stance", "text", "why", "created_at", "run_id"]).where("project_id", "=", projectId2).where("id", "in", ids).execute();
48994
48997
  const unit = new Map(units.map((u) => [u.id, u]));
48995
48998
  const runs = [...new Set(units.map((u) => u.run_id))];
48996
- const [intents, rows, gaps, states, evidence, adoption, anchors, conflicts, sameRun, outside] = await Promise.all([
48999
+ const [intents, rows, gaps, states, evidence, adoption, anchors, conflicts, sameRun, answered] = await Promise.all([
48997
49000
  db.selectFrom("unit_link").select(["from_unit", "to_unit", "added_at"]).where("kind", "=", "supersedes").where("from_unit", "in", ids).where("to_unit", "in", ids).execute(),
48998
49001
  db.selectFrom("unit_replacement").select(["id", "from_unit", "to_unit", "started_at", "ended_at", "end_reason"]).where("from_unit", "in", ids).where("to_unit", "in", ids).execute(),
48999
49002
  db.selectFrom("unit_replacement_gap").select(["from_unit", "to_unit"]).where("from_unit", "in", ids).where("to_unit", "in", ids).execute(),
@@ -49046,10 +49049,7 @@ async function linesOf(db, projectId2, entry) {
49046
49049
  "b.key as to_key"
49047
49050
  ]).where("l.kind", "=", "conflicts").where((eb) => eb.or([eb("l.from_unit", "in", ids), eb("l.to_unit", "in", ids)])).execute(),
49048
49051
  db.selectFrom("unit").select(["id", "key", "kind", "text", "created_at"]).where("project_id", "=", projectId2).where("extraction", "=", "supported").where("kind", "in", ["finding", "dead_end", "question"]).where("run_id", "in", runs).where("id", "not in", ids).orderBy("created_at", "desc").orderBy("id", "desc").limit(RELATED_READ + 1).execute(),
49049
- db.selectFrom("unit_state as t").innerJoin("unit as o", "o.id", "t.unit_id").select(["t.id", "t.at", "t.to_state", "t.reason", "o.id as unit", "o.key"]).where("o.project_id", "=", projectId2).where("t.run_id", "in", runs).where("o.id", "not in", ids).where((eb) => eb.or([
49050
- eb("o.run_id", "<>", eb.ref("t.run_id")),
49051
- eb("t.to_state", "in", ["superseded", "withdrawn"])
49052
- ])).execute()
49052
+ db.selectFrom("unit_answer as w").innerJoin("unit_state as t", "t.id", "w.state_id").innerJoin("unit as o", "o.id", "t.unit_id").select(["t.id", "t.at", "t.to_state", "t.reason", "o.id as unit", "o.key"]).where("o.project_id", "=", projectId2).where("w.answer_unit", "in", ids).where("o.id", "not in", ids).execute()
49053
49053
  ]);
49054
49054
  const everyPlace = [
49055
49055
  ...new Map(anchors.filter((a) => a.retired_at === null).sort((a, b) => a.id - b.id).map((a) => [JSON.stringify([a.path, a.symbol]), a])).values()
@@ -49223,7 +49223,7 @@ async function linesOf(db, projectId2, entry) {
49223
49223
  text: `${pair}: resolved, ${cut2(c.resolution)}`
49224
49224
  });
49225
49225
  }
49226
- for (const o of outside)
49226
+ for (const o of answered)
49227
49227
  lines2.push({
49228
49228
  at: o.at,
49229
49229
  order: ORDER.state,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sphica",
3
- "version": "0.6.56",
3
+ "version": "0.6.58",
4
4
  "description": "Records Claude Code and Codex sessions on your machine and keeps past implementation and decisions, with their sources, for your agent to find.",
5
5
  "license": "MIT",
6
6
  "type": "module",