sphica 0.6.47 → 0.6.49

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.
@@ -45929,7 +45929,7 @@ function repoFiles(root) {
45929
45929
  }
45930
45930
  async function worktreeStatus(root, deadline) {
45931
45931
  const out = await inIsolation(root, [{ kind: "status" }], { deadline, max: 16 * 1024 * 1024 });
45932
- return out?.[0] ?? null;
45932
+ return Array.isArray(out) ? out[0] ?? null : null;
45933
45933
  }
45934
45934
  function workerFile() {
45935
45935
  const js = path.join(import.meta.dirname, "git-worker.js");
@@ -45946,7 +45946,7 @@ function inIsolation(root, ops, { deadline, max }) {
45946
45946
  detached: process.platform !== "win32"
45947
45947
  });
45948
45948
  } catch {
45949
- resolve(null);
45949
+ resolve({ unread: "failed" });
45950
45950
  return;
45951
45951
  }
45952
45952
  const chunks = [];
@@ -45957,7 +45957,7 @@ function inIsolation(root, ops, { deadline, max }) {
45957
45957
  return;
45958
45958
  done = true;
45959
45959
  clearTimeout(timer);
45960
- if (out === null) {
45960
+ if (!Array.isArray(out)) {
45961
45961
  try {
45962
45962
  if (process.platform !== "win32" && child.pid)
45963
45963
  process.kill(-child.pid, "SIGKILL");
@@ -45972,25 +45972,30 @@ function inIsolation(root, ops, { deadline, max }) {
45972
45972
  }
45973
45973
  resolve(out);
45974
45974
  };
45975
- const timer = setTimeout(() => finish(null), deadline);
45975
+ const timer = setTimeout(() => finish({ unread: "timeout" }), deadline);
45976
45976
  const cap = 6 * max * Math.max(1, ops.length * 2) + 64 * 1024;
45977
45977
  child.stdout?.on("data", (c) => {
45978
45978
  size += c.length;
45979
45979
  if (size > cap)
45980
- finish(null);
45980
+ finish({ unread: "too_large" });
45981
45981
  else
45982
45982
  chunks.push(c);
45983
45983
  });
45984
- child.on("error", () => finish(null));
45984
+ child.on("error", () => finish({ unread: "failed" }));
45985
45985
  child.on("close", () => {
45986
45986
  try {
45987
45987
  const result = JSON.parse(Buffer.concat(chunks).toString("utf8"));
45988
- finish(result.ok ? result.out : null);
45988
+ if (result.ok)
45989
+ finish(result.out);
45990
+ else if ("refused" in result)
45991
+ finish({ unread: "refused", code: result.refused });
45992
+ else
45993
+ finish({ unread: result.failed });
45989
45994
  } catch {
45990
- finish(null);
45995
+ finish({ unread: "failed" });
45991
45996
  }
45992
45997
  });
45993
- child.stdin?.on("error", () => finish(null));
45998
+ child.stdin?.on("error", () => finish({ unread: "failed" }));
45994
45999
  child.stdin?.end(JSON.stringify({
45995
46000
  root,
45996
46001
  ops,
@@ -48223,6 +48228,163 @@ async function runSources(db, runId) {
48223
48228
  ]).select((eb) => eb.exists(eb.selectFrom("source_processing as p").whereRef("p.source_id", "=", "s.id").select(sql`1`.as("x"))).as("looked")).orderBy("s.created_at").orderBy("s.id").execute();
48224
48229
  }
48225
48230
 
48231
+ // server/src/own-words.ts
48232
+ var MAINTAINERS = new Set(["OWNER", "MEMBER", "COLLABORATOR"]);
48233
+ var refused = (words) => ("refused" in words);
48234
+ function locate(body, quote) {
48235
+ const at = Buffer.from(body, "utf8").indexOf(Buffer.from(quote, "utf8"));
48236
+ return at < 0 ? null : [at, at + Buffer.byteLength(quote, "utf8")];
48237
+ }
48238
+ function ownWords(s, quote, policy, agent = false) {
48239
+ if (policy === "maintainer" && s.kind === "pr_event")
48240
+ return { refused: "event" };
48241
+ const route = s.author_kind === "owner" ? "owner_statement" : policy !== "owner" && MAINTAINERS.has(s.author_association ?? "") ? "explicit" : policy === "maintainer" && agent && s.author_kind === "assistant" && s.kind === "session_message" ? "agent" : policy === "trusted" && s.kind === "session_message" ? "session" : null;
48242
+ if (!route)
48243
+ return { refused: "author" };
48244
+ const at = quote.trim() ? locate(s.text, quote) : null;
48245
+ if (!at)
48246
+ return { refused: "missing" };
48247
+ const quoted = notOwnWords(s.text, at[0], at[1]);
48248
+ if (quoted)
48249
+ return { refused: quoted };
48250
+ return { source: s.id, start: at[0], end: at[1], route };
48251
+ }
48252
+ var PAIRS = { '"': '"', "“": "”", "‘": "’", "「": "」", "『": "』" };
48253
+ var WORD2 = /[\p{Script=Latin}\p{N}]/u;
48254
+ var charAt = (line, at) => at < line.length ? String.fromCodePoint(line.codePointAt(at)) : "";
48255
+ function endsWord(line, end) {
48256
+ for (let i = end;i > 0; ) {
48257
+ const low = line.charCodeAt(i - 1);
48258
+ const from = low >= 56320 && low <= 57343 && i > 1 ? i - 2 : i - 1;
48259
+ const c = charAt(line, from);
48260
+ if (!/\p{M}/u.test(c))
48261
+ return WORD2.test(c);
48262
+ i = from;
48263
+ }
48264
+ return false;
48265
+ }
48266
+ var startsWord = (line, at) => WORD2.test(charAt(line, at + 1));
48267
+ function inlineRanges(line) {
48268
+ const found = [];
48269
+ const closing = new Map;
48270
+ const last = new Map;
48271
+ for (let end = line.length;end > 0; ) {
48272
+ if (line[end - 1] !== "`") {
48273
+ end--;
48274
+ continue;
48275
+ }
48276
+ let from = end - 1;
48277
+ while (from > 0 && line[from - 1] === "`")
48278
+ from--;
48279
+ const partner = last.get(end - from);
48280
+ if (partner !== undefined)
48281
+ closing.set(from, partner);
48282
+ last.set(end - from, from);
48283
+ end = from;
48284
+ }
48285
+ const unpaired = new Set;
48286
+ for (let i = 0;i < line.length; ) {
48287
+ const c = line[i];
48288
+ let close = -1;
48289
+ let width2 = 1;
48290
+ if (c === "`")
48291
+ while (line[i + width2] === "`")
48292
+ width2++;
48293
+ const mark2 = c === "`" ? c.repeat(width2) : c;
48294
+ if (unpaired.has(mark2)) {
48295
+ i += width2;
48296
+ continue;
48297
+ }
48298
+ if (c === "`")
48299
+ close = closing.get(i) ?? -1;
48300
+ else if (c === "'") {
48301
+ if (endsWord(line, i)) {
48302
+ i++;
48303
+ continue;
48304
+ }
48305
+ for (let j = i + 1;j < line.length; j++)
48306
+ if (line[j] === "'" && !startsWord(line, j)) {
48307
+ close = j;
48308
+ break;
48309
+ }
48310
+ } else if (PAIRS[c]) {
48311
+ close = line.indexOf(PAIRS[c], i + 1);
48312
+ while (c === "‘" && close >= 0 && startsWord(line, close))
48313
+ close = line.indexOf(PAIRS[c], close + 1);
48314
+ } else {
48315
+ i++;
48316
+ continue;
48317
+ }
48318
+ if (close < 0) {
48319
+ unpaired.add(mark2);
48320
+ i += width2;
48321
+ continue;
48322
+ }
48323
+ found.push([i, close + width2]);
48324
+ i = close + width2;
48325
+ }
48326
+ return found;
48327
+ }
48328
+ var OPENS = /["“'‘`「『]/;
48329
+ var CLOSES2 = /["”'’`」』]/;
48330
+ function notOwnWords(body, start, end) {
48331
+ const bytes2 = Buffer.from(body, "utf8");
48332
+ let fence = null;
48333
+ let own2 = "";
48334
+ let quoting = false;
48335
+ let before = "";
48336
+ let after = "";
48337
+ for (let at = 0;at <= bytes2.length; ) {
48338
+ const nl = bytes2.indexOf(10, at);
48339
+ const stop = nl < 0 ? bytes2.length : nl;
48340
+ const raw = bytes2.subarray(at, stop).toString("utf8").replace(/\r$/, "");
48341
+ const line = raw.trimStart();
48342
+ let block = fence !== null || line.startsWith(">");
48343
+ if (fence) {
48344
+ const closing = /^(`+|~+)\s*$/.exec(line)?.[1];
48345
+ if (closing && closing[0] === fence.mark && closing.length >= fence.length)
48346
+ fence = null;
48347
+ } else {
48348
+ const opening = /^(`{3,}|~{3,})(.*)$/.exec(line);
48349
+ if (opening && !(opening[1]?.startsWith("`") && opening[2]?.includes("`"))) {
48350
+ fence = { mark: opening[1][0], length: opening[1].length };
48351
+ block = true;
48352
+ }
48353
+ }
48354
+ if (at < end && stop > start) {
48355
+ if (block)
48356
+ return "block";
48357
+ const from = Buffer.from(raw, "utf8").subarray(0, Math.max(start - at, 0)).toString("utf8").length;
48358
+ const to = Buffer.from(raw, "utf8").subarray(0, Math.min(end, stop) - at).toString("utf8").length;
48359
+ if (start >= at)
48360
+ before = raw.slice(0, from).trimEnd().slice(-1);
48361
+ if (end <= stop)
48362
+ after = raw.slice(to).trimStart().slice(0, 1);
48363
+ let cursor = from;
48364
+ for (const [a, b] of inlineRanges(raw)) {
48365
+ if (b <= cursor || a >= to)
48366
+ continue;
48367
+ quoting = true;
48368
+ own2 += raw.slice(cursor, Math.max(a, cursor));
48369
+ cursor = Math.max(cursor, b);
48370
+ }
48371
+ own2 += `${raw.slice(cursor, Math.max(to, cursor))}
48372
+ `;
48373
+ }
48374
+ if (nl < 0)
48375
+ break;
48376
+ at = nl + 1;
48377
+ }
48378
+ if (OPENS.test(before) && CLOSES2.test(after))
48379
+ return "inline";
48380
+ const span = bytes2.subarray(start, end).toString("utf8").trim();
48381
+ const opens = span.slice(0, 1);
48382
+ const closes = PAIRS[opens] ?? (opens === "'" || opens === "`" ? opens : "");
48383
+ if (closes && span.length > 1 && span.endsWith(closes) && !span.slice(1, -1).includes(closes))
48384
+ return "inline";
48385
+ return !quoting || /\p{L}/u.test(own2) ? null : "inline";
48386
+ }
48387
+
48226
48388
  // server/src/judge.ts
48227
48389
  var replaceable = (successor, old) => successor === old || ["decision", "constraint"].includes(successor) && ["decision", "constraint"].includes(old);
48228
48390
  var owned = (kind) => kind === "decision" || kind === "constraint";
@@ -48615,7 +48777,6 @@ var instructionFile = (relative) => {
48615
48777
  var KEY = /^[a-z0-9][a-z0-9._/-]{0,63}$/;
48616
48778
  var FIELD_NAME2 = /^[a-z][a-z0-9_]{0,39}$/;
48617
48779
  var SOURCE_REF = /^s[1-9][0-9]{0,15}$/;
48618
- var MAINTAINERS = new Set(["OWNER", "MEMBER", "COLLABORATOR"]);
48619
48780
  var text3 = (max) => exports_external.string().trim().min(1).max(max);
48620
48781
  var Quote = exports_external.object({ source: exports_external.string().regex(SOURCE_REF, "cite a source ref such as s12"), quote: exports_external.string() }).strict();
48621
48782
  var Evidence = Quote.extend({
@@ -48679,40 +48840,7 @@ var Record = exports_external.object({
48679
48840
  units: exports_external.array(Unit).max(50),
48680
48841
  work: Work.optional()
48681
48842
  }).strict();
48682
- function quotedSpan(body, start, end) {
48683
- const bytes2 = Buffer.from(body, "utf8");
48684
- let fenced = false;
48685
- let touched = false;
48686
- for (let at = 0;at <= bytes2.length; ) {
48687
- const nl = bytes2.indexOf(10, at);
48688
- const stop = nl < 0 ? bytes2.length : nl;
48689
- const line = bytes2.subarray(at, stop).toString("utf8").trimStart();
48690
- const fence = /^(```|~~~)/.test(line);
48691
- if (stop >= start && at < end) {
48692
- touched = true;
48693
- if (!(fenced || fence || line.startsWith(">")))
48694
- return false;
48695
- }
48696
- if (fence)
48697
- fenced = !fenced;
48698
- if (nl < 0)
48699
- break;
48700
- at = nl + 1;
48701
- }
48702
- return touched;
48703
- }
48704
- function inlineQuoted(body, start, end) {
48705
- const bytes2 = Buffer.from(body, "utf8");
48706
- const before = bytes2.subarray(0, start).toString("utf8").trimEnd().at(-1) ?? "";
48707
- const after = bytes2.subarray(end).toString("utf8").trimStart()[0] ?? "";
48708
- return /["“'‘`「『]/.test(before) && /["”'’`」』]/.test(after);
48709
- }
48710
- function locate(body, quote) {
48711
- if (!quote.trim())
48712
- return null;
48713
- const at = Buffer.from(body, "utf8").indexOf(Buffer.from(quote, "utf8"));
48714
- return at < 0 ? null : [at, at + Buffer.byteLength(quote, "utf8")];
48715
- }
48843
+ var cited = (body, quote) => quote.trim() ? locate(body, quote) : null;
48716
48844
  function anchorProblem(facts, a) {
48717
48845
  if (!facts.root || a.held)
48718
48846
  return null;
@@ -48846,18 +48974,14 @@ async function checkRecord(db, target, raw, facts = prepareRecord(target.root, r
48846
48974
  const s = sources.get(Number(d.quote.source.slice(1)));
48847
48975
  if (!s)
48848
48976
  continue;
48849
- if (s.author_kind !== "owner") {
48850
- errors3.push(`${at}: the quote must be the owner's; ${d.quote.source} is by ${s.author_login ?? s.author_kind}`);
48851
- continue;
48852
- }
48853
- const span = locate(s.text, d.quote.quote);
48854
- if (!span) {
48855
- errors3.push(`${at}: quote not found in ${d.quote.source}: "${head(d.quote.quote, 80)}"`);
48977
+ const said2 = ownWords(s, d.quote.quote, "owner");
48978
+ if (refused(said2)) {
48979
+ errors3.push(said2.refused === "author" ? `${at}: the quote must be the owner's; ${d.quote.source} is by ${s.author_login ?? s.author_kind}` : said2.refused === "missing" ? `${at}: quote not found in ${d.quote.source}: "${head(d.quote.quote, 80)}"` : `${at}: the quote in ${d.quote.source} is quoted or in a code block, so it is someone else's words; quote the owner's own`);
48856
48980
  continue;
48857
48981
  }
48858
48982
  defined.set(d.name, { type: d.type, enum: d.enum ?? null, kinds: d.kinds });
48859
48983
  const { quote: _, ...rest } = d;
48860
- fieldDefs.push({ ...rest, source: s.id, start: span[0], end: span[1] });
48984
+ fieldDefs.push({ ...rest, ...said2 });
48861
48985
  }
48862
48986
  const names = record4.field_defs.map((d) => d.name);
48863
48987
  for (const n of new Set(names.filter((n2, i) => names.indexOf(n2) !== i)))
@@ -48880,7 +49004,7 @@ async function checkRecord(db, target, raw, facts = prepareRecord(target.root, r
48880
49004
  errors3.push(`${key}: decides quotes the AI choosing in its own reply, never a question it asked, someone else's words, or an option, and only a trace records it`);
48881
49005
  return [];
48882
49006
  }
48883
- const span = locate(s.text, e.quote);
49007
+ const span = cited(s.text, e.quote);
48884
49008
  if (!span) {
48885
49009
  quarantine.push(`quote not found in ${e.source}: "${head(e.quote, 80)}"`);
48886
49010
  return [];
@@ -48900,16 +49024,12 @@ async function checkRecord(db, target, raw, facts = prepareRecord(target.root, r
48900
49024
  if (!q || !s || o.reconsider_when === undefined)
48901
49025
  return null;
48902
49026
  cites.add(s.id);
48903
- if (s.author_kind !== "owner") {
48904
- errors3.push(`${key}: option "${head(o.text, 60)}": reconsider_quote must quote the owner; ${q.source} is by ${s.author_login ?? s.author_kind}`);
48905
- return null;
48906
- }
48907
- const span = locate(s.text, q.quote);
48908
- if (!span) {
48909
- errors3.push(`${key}: option "${head(o.text, 60)}": reconsider_quote not found in ${q.source}: "${head(q.quote, 80)}"`);
48910
- return null;
48911
- }
48912
- return { source: s.id, start: span[0], end: span[1] };
49027
+ const said2 = ownWords(s, q.quote, "owner");
49028
+ if (!refused(said2))
49029
+ return said2;
49030
+ const at = `${key}: option "${head(o.text, 60)}": reconsider_quote`;
49031
+ errors3.push(said2.refused === "author" ? `${at} must quote the owner; ${q.source} is by ${s.author_login ?? s.author_kind}` : said2.refused === "missing" ? `${at} not found in ${q.source}: "${head(q.quote, 80)}"` : `${at} in ${q.source} is quoted or in a code block, so it is someone else's words; quote the owner's own`);
49032
+ return null;
48913
49033
  };
48914
49034
  const options = u.options.map((o) => ({
48915
49035
  input: o,
@@ -48924,10 +49044,10 @@ async function checkRecord(db, target, raw, facts = prepareRecord(target.root, r
48924
49044
  if (target.origin !== "trace" && (u.supersedes || u.conflicts.length)) {
48925
49045
  const trusted = [...u.evidence, ...u.adoption].some((q) => {
48926
49046
  const s = sources.get(Number(q.source.slice(1)));
48927
- return s && (s.kind === "session_message" || s.author_kind === "owner" || MAINTAINERS.has(s.author_association ?? ""));
49047
+ return s !== undefined && !refused(ownWords(s, q.quote, "trusted"));
48928
49048
  });
48929
49049
  if (!trusted)
48930
- errors3.push(`${key}: supersedes and conflicts from ${target.origin} need the owner's or a maintainer's words, or the owner's session`);
49050
+ errors3.push(`${key}: supersedes and conflicts from ${target.origin} need the owner's or a maintainer's words, or the owner's session: words that are in the source, and not only a quotation there`);
48931
49051
  }
48932
49052
  const adoption = [];
48933
49053
  for (const a of u.adoption) {
@@ -48940,25 +49060,19 @@ async function checkRecord(db, target, raw, facts = prepareRecord(target.root, r
48940
49060
  problems.push(`${key}: adoption applies to decisions and constraints; ${a.source} left out`);
48941
49061
  continue;
48942
49062
  }
48943
- if (s.kind === "pr_event") {
48944
- problems.push(`${key}: the merge does not adopt a proposal (it only shows the code went in); cite the owner's or a maintainer's words`);
48945
- continue;
48946
- }
48947
- const route = s.author_kind === "owner" ? "owner_statement" : MAINTAINERS.has(s.author_association ?? "") ? "explicit" : s.author_kind === "assistant" && s.kind === "session_message" && target.agent ? "agent" : null;
48948
- if (!route) {
48949
- problems.push(`${key}: ${a.source} is by ${s.author_login ?? s.author_kind} (${s.author_association ?? "no association"}); only the owner or a maintainer can adopt${s.author_kind === "assistant" ? ", and an AI's own decision only in a trace an interactive session runs" : ""}`);
48950
- continue;
48951
- }
48952
- const span = locate(s.text, a.quote);
48953
- if (!span) {
48954
- quarantine.push(`adoption quote not found in ${a.source}: "${head(a.quote, 80)}"`);
48955
- continue;
48956
- }
48957
- if (quotedSpan(s.text, span[0], span[1]) || inlineQuoted(s.text, span[0], span[1])) {
48958
- problems.push(`${key}: the adoption in ${a.source} is quoted or in a code block, so it is someone else's words pasted in; left out, so it stays a candidate unless other words adopt it`);
49063
+ const words = ownWords(s, a.quote, "maintainer", target.agent);
49064
+ if (refused(words)) {
49065
+ if (words.refused === "event")
49066
+ problems.push(`${key}: the merge does not adopt a proposal (it only shows the code went in); cite the owner's or a maintainer's words`);
49067
+ else if (words.refused === "author")
49068
+ problems.push(`${key}: ${a.source} is by ${s.author_login ?? s.author_kind} (${s.author_association ?? "no association"}); only the owner or a maintainer can adopt${s.author_kind === "assistant" ? ", and an AI's own decision only in a trace an interactive session runs" : ""}`);
49069
+ else if (words.refused === "missing")
49070
+ quarantine.push(`adoption quote not found in ${a.source}: "${head(a.quote, 80)}"`);
49071
+ else
49072
+ problems.push(`${key}: the adoption in ${a.source} is quoted or in a code block, so it is someone else's words pasted in; left out, so it stays a candidate unless other words adopt it`);
48959
49073
  continue;
48960
49074
  }
48961
- adoption.push({ source: s.id, start: span[0], end: span[1], route });
49075
+ adoption.push(words);
48962
49076
  }
48963
49077
  const anchors = [];
48964
49078
  const fallbacks = new Set;
@@ -49071,7 +49185,7 @@ async function checkRecord(db, target, raw, facts = prepareRecord(target.root, r
49071
49185
  if (!s)
49072
49186
  continue;
49073
49187
  cites.add(s.id);
49074
- const span = locate(s.text, f.quote.quote);
49188
+ const span = cited(s.text, f.quote.quote);
49075
49189
  if (!span) {
49076
49190
  errors3.push(`${at}: quote not found in ${f.quote.source}: "${head(f.quote.quote, 80)}"`);
49077
49191
  continue;
@@ -49114,13 +49228,13 @@ var isoDate = (v) => {
49114
49228
  const d = new Date(`${v}T00:00:00Z`);
49115
49229
  return !Number.isNaN(d.getTime()) && d.toISOString().slice(0, 10) === v;
49116
49230
  };
49117
- var WORD2 = /[\p{Script=Latin}\p{Script=Greek}\p{Script=Cyrillic}\p{M}\p{N}_]/u;
49231
+ var WORD3 = /[\p{Script=Latin}\p{Script=Greek}\p{Script=Cyrillic}\p{M}\p{N}_]/u;
49118
49232
  var NUMBER = /(?<![\p{Script=Latin}\p{Script=Greek}\p{Script=Cyrillic}\p{M}\p{N}\p{Pd}_.+\uFF0B\u2212])[+-]?(?:[0-9]{1,3}(?:,[0-9]{3})+(?![0-9])|[0-9]+)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/gu;
49119
49233
  function standsAlone(quote, value) {
49120
49234
  for (let at = quote.indexOf(value);at >= 0; at = quote.indexOf(value, at + 1)) {
49121
49235
  const before = [...quote.slice(Math.max(0, at - 2), at)].at(-1) ?? "";
49122
49236
  const after = String.fromCodePoint(quote.codePointAt(at + value.length) ?? 32);
49123
- if (!WORD2.test(before) && !WORD2.test(after))
49237
+ if (!WORD3.test(before) && !WORD3.test(after))
49124
49238
  return true;
49125
49239
  }
49126
49240
  return false;
@@ -49207,9 +49321,9 @@ ${checked.errors.map((e) => ` ${e}`).join(`
49207
49321
  anchorProblems: [],
49208
49322
  written: []
49209
49323
  };
49210
- const cited = new Set;
49324
+ const cited2 = new Set;
49211
49325
  for (const d of checked.fieldDefs) {
49212
- cited.add(d.source);
49326
+ cited2.add(d.source);
49213
49327
  await trx.insertInto("field_def").values({
49214
49328
  project_id: target.projectId,
49215
49329
  name: d.name,
@@ -49228,7 +49342,7 @@ ${checked.errors.map((e) => ` ${e}`).join(`
49228
49342
  const fieldIds = new Map((checked.units.some((p) => p.fields.length) ? await trx.selectFrom("field_def").select(["id", "name"]).where("project_id", "=", target.projectId).execute() : []).map((d) => [d.name, d.id]));
49229
49343
  for (const p of checked.units) {
49230
49344
  for (const c of p.cites)
49231
- cited.add(c);
49345
+ cited2.add(c);
49232
49346
  const u = p.input;
49233
49347
  const hash2 = contentHash(u);
49234
49348
  const { id } = await trx.insertInto("unit").values({
@@ -49250,7 +49364,7 @@ ${checked.errors.map((e) => ` ${e}`).join(`
49250
49364
  }).returning("id").executeTakeFirstOrThrow();
49251
49365
  const evidence = async (list2, option) => {
49252
49366
  for (const e of list2) {
49253
- cited.add(e.source);
49367
+ cited2.add(e.source);
49254
49368
  await trx.insertInto("unit_evidence").values({
49255
49369
  unit_id: id,
49256
49370
  option_id: option,
@@ -49279,7 +49393,7 @@ ${checked.errors.map((e) => ` ${e}`).join(`
49279
49393
  }
49280
49394
  await evidence(p.evidence, null);
49281
49395
  for (const a of p.adoption) {
49282
- cited.add(a.source);
49396
+ cited2.add(a.source);
49283
49397
  await trx.insertInto("unit_adoption").values({
49284
49398
  unit_id: id,
49285
49399
  route: a.route,
@@ -49378,7 +49492,7 @@ ${checked.errors.map((e) => ` ${e}`).join(`
49378
49492
  await trx.insertInto("work").values({ project_id: target.projectId, key: w.key, ...row }).onConflict((oc) => oc.columns(["project_id", "key"]).doUpdateSet(row)).execute();
49379
49493
  }
49380
49494
  for (const s of new Set(looked))
49381
- await trx.insertInto("source_processing").values({ source_id: s, run_id: runId, outcome: cited.has(s) ? "units" : "no_unit" }).onConflict((oc) => oc.doNothing()).execute();
49495
+ await trx.insertInto("source_processing").values({ source_id: s, run_id: runId, outcome: cited2.has(s) ? "units" : "no_unit" }).onConflict((oc) => oc.doNothing()).execute();
49382
49496
  return saved;
49383
49497
  }
49384
49498
  async function finishRun(trx, runId) {
@@ -49390,7 +49504,6 @@ async function finishRun(trx, runId) {
49390
49504
 
49391
49505
  // server/src/glean.ts
49392
49506
  var MAX_FILE = 1024 * 1024;
49393
- var MAINTAINERS2 = new Set(["OWNER", "MEMBER", "COLLABORATOR"]);
49394
49507
  var SOURCE_REF2 = exports_external.string().regex(/^s[1-9][0-9]{0,15}$/, "cite a source ref such as s12");
49395
49508
  var unit = exports_external.string().min(1).max(200);
49396
49509
  var revision2 = exports_external.number().int().positive();
@@ -49519,10 +49632,6 @@ function readExcerpt(root, file3) {
49519
49632
  throw new Error(`${p} lines ${a}-${b} cut through text Sphica masks; cite more lines around it`);
49520
49633
  return { path: p, commit, blob, size, lines: [a, b], raw, text: text4 };
49521
49634
  }
49522
- var locate2 = (body, q) => {
49523
- const at = Buffer.from(body, "utf8").indexOf(Buffer.from(q, "utf8"));
49524
- return at < 0 ? null : [at, at + Buffer.byteLength(q, "utf8")];
49525
- };
49526
49635
  var excerptKey = (file3) => JSON.stringify([file3.path, file3.commit, file3.lines]);
49527
49636
  function excerptOf(facts, file3) {
49528
49637
  const key = excerptKey(file3);
@@ -49594,13 +49703,25 @@ async function checkGlean(db, target, raw, facts = prepareGlean(target.root, raw
49594
49703
  errors3.push(`${what}: ${ref} is not a source of this project`);
49595
49704
  return null;
49596
49705
  }
49597
- const at = locate2(s.text, q);
49706
+ const at = locate(s.text, q);
49598
49707
  if (!at) {
49599
49708
  errors3.push(`${what}: quote not found in ${ref}: "${head(q, 80)}"`);
49600
49709
  return null;
49601
49710
  }
49602
49711
  return { s, at };
49603
49712
  };
49713
+ const own2 = async (ref, q, policy, what, who) => {
49714
+ const s = await source(ref);
49715
+ if (!s) {
49716
+ errors3.push(`${what}: ${ref} is not a source of this project`);
49717
+ return null;
49718
+ }
49719
+ const words = policy === "owner" ? ownWords(s, q, "owner") : ownWords(s, q, "maintainer");
49720
+ if (!refused(words))
49721
+ return words;
49722
+ errors3.push(words.refused === "missing" ? `${what}: quote not found in ${ref}: "${head(q, 80)}"` : words.refused === "event" ? `${what}: the merge does not adopt a proposal` : words.refused === "author" ? `${what}: ${who}` : `${what}: the quote in ${ref} is quoted or in a code block, so it is someone else's words; quote the writer's own`);
49723
+ return null;
49724
+ };
49604
49725
  const replaced = new Map;
49605
49726
  const retired = new Set;
49606
49727
  const anchored = new Set;
@@ -49622,6 +49743,7 @@ async function checkGlean(db, target, raw, facts = prepareGlean(target.root, raw
49622
49743
  continue;
49623
49744
  }
49624
49745
  let excerpt = null;
49746
+ let said2 = null;
49625
49747
  if (op.op === "add_evidence") {
49626
49748
  if (!op.source === !op.file)
49627
49749
  errors3.push(`${what}: cite either a source or a file`);
@@ -49633,7 +49755,7 @@ async function checkGlean(db, target, raw, facts = prepareGlean(target.root, raw
49633
49755
  excerpt = read;
49634
49756
  if ((await forgottenExcerpt(db, target.projectId, excerpt)).same)
49635
49757
  errors3.push(`${what}: the owner forgot ${excerpt.path} lines ${op.file.lines.join("-")}; cite something else`);
49636
- else if (!locate2(excerpt.raw, op.quote))
49758
+ else if (!locate(excerpt.raw, op.quote))
49637
49759
  errors3.push(`${what}: quote not found in ${excerpt.path} lines ${op.file.lines.join("-")}`);
49638
49760
  else if (!quoteSpan(excerpt.raw, excerpt.text, op.quote))
49639
49761
  errors3.push(`${what}: the quote also appears in text Sphica masks; quote a longer or different part`);
@@ -49649,11 +49771,7 @@ async function checkGlean(db, target, raw, facts = prepareGlean(target.root, raw
49649
49771
  if (op.op === "adopt") {
49650
49772
  if (!["decision", "constraint"].includes(u.kind))
49651
49773
  errors3.push(`${what}: adoption applies to decisions and constraints`);
49652
- const got = await span(op.source, op.quote, what);
49653
- if (got && got.s.kind === "pr_event")
49654
- errors3.push(`${what}: the merge does not adopt a proposal`);
49655
- else if (got && got.s.author_kind !== "owner" && !MAINTAINERS2.has(got.s.author_association ?? ""))
49656
- errors3.push(`${what}: only the owner or a maintainer can adopt`);
49774
+ said2 = await own2(op.source, op.quote, "maintainer", what, "only the owner or a maintainer can adopt");
49657
49775
  }
49658
49776
  if (op.op === "anchor" && !repoPath(op.path))
49659
49777
  errors3.push(`${what}: the path is not inside the repository`);
@@ -49671,9 +49789,7 @@ async function checkGlean(db, target, raw, facts = prepareGlean(target.root, raw
49671
49789
  if (op.op === "replace_anchor" || op.op === "retire_anchor") {
49672
49790
  if (op.op === "replace_anchor" && !repoPath(op.to.path))
49673
49791
  errors3.push(`${what}: the path is not inside the repository`);
49674
- const said2 = await span(op.source, op.quote, what);
49675
- if (said2 && said2.s.author_kind !== "owner")
49676
- errors3.push(`${what}: only the owner's words can ${op.op === "retire_anchor" ? "retire" : "move"} an anchor`);
49792
+ said2 = await own2(op.source, op.quote, "owner", what, `only the owner's words can ${op.op === "retire_anchor" ? "retire" : "move"} an anchor`);
49677
49793
  const from = repoPath(op.from.path);
49678
49794
  if (!from)
49679
49795
  errors3.push(`${what}: the from path is not inside the repository`);
@@ -49738,9 +49854,7 @@ async function checkGlean(db, target, raw, facts = prepareGlean(target.root, raw
49738
49854
  });
49739
49855
  }
49740
49856
  if (op.op === "retract_evidence" || op.op === "retract_adoption" || op.op === "withdraw" || op.op === "resolve_conflict") {
49741
- const got = await span(op.reason_source, op.reason_quote, what);
49742
- if (got && got.s.author_kind !== "owner")
49743
- errors3.push(`${what}: only the owner's words can retract, withdraw, or resolve`);
49857
+ said2 = await own2(op.reason_source, op.reason_quote, "owner", what, "only the owner's words can retract, withdraw, or resolve");
49744
49858
  }
49745
49859
  if (op.op === "withdraw") {
49746
49860
  if (u.lifecycle === "superseded")
@@ -49785,6 +49899,7 @@ async function checkGlean(db, target, raw, facts = prepareGlean(target.root, raw
49785
49899
  unitId: u.id,
49786
49900
  lifecycle: u.lifecycle,
49787
49901
  excerpt,
49902
+ said: said2,
49788
49903
  retracts,
49789
49904
  replaces,
49790
49905
  retires,
@@ -49856,10 +49971,10 @@ ${c.errors.map((e) => ` ${e}`).join(`
49856
49971
  const units = await saveRecord(trx, target, runId, c.units, [], { settle: false });
49857
49972
  const now = iso(Date.now());
49858
49973
  const changed2 = [];
49859
- const spanOf = async (ref, q) => {
49860
- const s = await trx.selectFrom("source").select(["id", "text"]).where("id", "=", Number(ref.slice(1))).executeTakeFirstOrThrow();
49861
- const at = locate2(s.text, q) ?? [0, 0];
49862
- return { id: s.id, start: at[0], end: at[1] };
49974
+ const saidOf = (p) => {
49975
+ if (!p.said)
49976
+ throw new Error(`${p.input.op} ${p.input.unit}: saved without checked words`);
49977
+ return p.said;
49863
49978
  };
49864
49979
  const touched = new Map;
49865
49980
  const withdraw = new Map;
@@ -49870,12 +49985,11 @@ ${c.errors.map((e) => ` ${e}`).join(`
49870
49985
  ...c.ops.filter((p) => p.input.op === "replace_anchor"),
49871
49986
  ...c.ops.filter((p) => !moves.includes(p.input.op))
49872
49987
  ];
49873
- const reason2 = async (anchorId, ref, q) => {
49874
- const s = await spanOf(ref, q);
49988
+ const reason2 = async (anchorId, s) => {
49875
49989
  await trx.insertInto("unit_anchor_retirement").values({
49876
49990
  anchor_id: anchorId ?? -1,
49877
49991
  run_id: runId,
49878
- source_id: s.id,
49992
+ source_id: s.source,
49879
49993
  span_start: s.start,
49880
49994
  span_end: s.end,
49881
49995
  added_at: now
@@ -49886,7 +50000,7 @@ ${c.errors.map((e) => ` ${e}`).join(`
49886
50000
  touched.set(p.unitId, op.unit);
49887
50001
  if (op.op === "add_evidence") {
49888
50002
  const s = p.excerpt ? { id: await excerptSource(trx, target.projectId, p.excerpt), text: p.excerpt.text } : await trx.selectFrom("source").select(["id", "text"]).where("id", "=", Number(op.source?.slice(1))).executeTakeFirstOrThrow();
49889
- const at = (p.excerpt ? quoteSpan(p.excerpt.raw, s.text, op.quote) : locate2(s.text, op.quote)) ?? [
50003
+ const at = (p.excerpt ? quoteSpan(p.excerpt.raw, s.text, op.quote) : locate(s.text, op.quote)) ?? [
49890
50004
  0,
49891
50005
  0
49892
50006
  ];
@@ -49902,12 +50016,11 @@ ${c.errors.map((e) => ` ${e}`).join(`
49902
50016
  }).onConflict((oc) => oc.doNothing()).execute();
49903
50017
  changed2.push(`${op.unit}: evidence added`);
49904
50018
  } else if (op.op === "adopt") {
49905
- const s = await spanOf(op.source, op.quote);
49906
- const author = await trx.selectFrom("source").select("author_kind").where("id", "=", s.id).executeTakeFirstOrThrow();
50019
+ const s = saidOf(p);
49907
50020
  await trx.insertInto("unit_adoption").values({
49908
50021
  unit_id: p.unitId,
49909
- route: author.author_kind === "owner" ? "owner_statement" : "explicit",
49910
- source_id: s.id,
50022
+ route: s.route,
50023
+ source_id: s.source,
49911
50024
  span_start: s.start,
49912
50025
  span_end: s.end,
49913
50026
  run_id: runId,
@@ -49940,12 +50053,12 @@ ${c.errors.map((e) => ` ${e}`).join(`
49940
50053
  }).returning("id").executeTakeFirstOrThrow();
49941
50054
  if (op.op === "replace_anchor") {
49942
50055
  await trx.updateTable("unit_anchor").set({ retired_at: now, replaced_by: added.id }).where("id", "=", p.replaces ?? -1).where("retired_at", "is", null).execute();
49943
- await reason2(p.replaces, op.source, op.quote);
50056
+ await reason2(p.replaces, saidOf(p));
49944
50057
  }
49945
50058
  changed2.push(`${op.unit}: anchor ${op.op === "anchor" ? "added" : "replaced"}`);
49946
50059
  } else if (op.op === "retire_anchor") {
49947
50060
  await trx.updateTable("unit_anchor").set({ retired_at: now }).where("id", "=", p.retires ?? -1).where("retired_at", "is", null).execute();
49948
- await reason2(p.retires, op.source, op.quote);
50061
+ await reason2(p.retires, saidOf(p));
49949
50062
  changed2.push(`${op.unit}: anchor retired`);
49950
50063
  } else if (op.op === "replace_aliases") {
49951
50064
  const { content_hash } = await trx.selectFrom("unit").select("content_hash").where("id", "=", p.unitId).executeTakeFirstOrThrow();
@@ -49958,17 +50071,17 @@ ${c.errors.map((e) => ` ${e}`).join(`
49958
50071
  }).execute();
49959
50072
  changed2.push(`${op.unit}: aliases ${p.aliases?.length ? "replaced" : "cleared"}`);
49960
50073
  } else {
49961
- const reason3 = await spanOf(op.reason_source, op.reason_quote);
50074
+ const reason3 = saidOf(p);
49962
50075
  const retraction = {
49963
50076
  retracted_at: now,
49964
50077
  retraction_reason: op.reason_quote,
49965
- retraction_source_id: reason3.id,
50078
+ retraction_source_id: reason3.source,
49966
50079
  retraction_span_start: reason3.start,
49967
50080
  retraction_span_end: reason3.end
49968
50081
  };
49969
50082
  if (op.op === "resolve_conflict") {
49970
50083
  const other = await trx.selectFrom("unit").select("id").where("project_id", "=", target.projectId).where("key", "=", op.with).executeTakeFirstOrThrow();
49971
- await trx.updateTable("unit_link").set({ resolved_at: now, resolution: `${head(op.reason_quote, 400)} (s${reason3.id})` }).where("kind", "=", "conflicts").where("resolved_at", "is", null).where((eb) => eb.or([
50084
+ await trx.updateTable("unit_link").set({ resolved_at: now, resolution: `${head(op.reason_quote, 400)} (s${reason3.source})` }).where("kind", "=", "conflicts").where("resolved_at", "is", null).where((eb) => eb.or([
49972
50085
  eb.and([eb("from_unit", "=", p.unitId), eb("to_unit", "=", other.id)]),
49973
50086
  eb.and([eb("from_unit", "=", other.id), eb("to_unit", "=", p.unitId)])
49974
50087
  ])).execute();
@@ -49976,7 +50089,7 @@ ${c.errors.map((e) => ` ${e}`).join(`
49976
50089
  continue;
49977
50090
  }
49978
50091
  if (op.op === "withdraw") {
49979
- withdraw.set(p.unitId, { reason: `withdrawn: ${head(op.reason_quote, 200)}`, source: reason3.id });
50092
+ withdraw.set(p.unitId, { reason: `withdrawn: ${head(op.reason_quote, 200)}`, source: reason3.source });
49980
50093
  continue;
49981
50094
  }
49982
50095
  const table = op.op === "retract_evidence" ? "unit_evidence" : "unit_adoption";
@@ -50490,7 +50603,7 @@ async function validate2(trx, id, projectId2, root, record4, prepared, call) {
50490
50603
  ops: null,
50491
50604
  write: async (w) => {
50492
50605
  const shown = shownTo.get(id)?.sources ?? new Set;
50493
- const cited = new Set([
50606
+ const cited2 = new Set([
50494
50607
  ...checked.units.flatMap((u) => [
50495
50608
  ...u.evidence,
50496
50609
  ...u.adoption,
@@ -50499,14 +50612,14 @@ async function validate2(trx, id, projectId2, root, record4, prepared, call) {
50499
50612
  ]),
50500
50613
  ...checked.fieldDefs
50501
50614
  ].map((q) => q.source));
50502
- const saved = await saveRecord(w, scope.target, run2.id, checked, scope.looked.filter((s) => shown.has(s) || cited.has(s)));
50615
+ const saved = await saveRecord(w, scope.target, run2.id, checked, scope.looked.filter((s) => shown.has(s) || cited2.has(s)));
50503
50616
  return { saved, changed: [] };
50504
50617
  }
50505
50618
  };
50506
50619
  }
50507
50620
  async function checkText(db, id, projectId2, root, record4, call, probe) {
50508
50621
  const prepared = await prepare2(db, id, projectId2, root, record4, probe, call);
50509
- const { v, done, refused } = await inRolledBack(db, async (trx) => {
50622
+ const { v, done, refused: refused2 } = await inRolledBack(db, async (trx) => {
50510
50623
  const v2 = await validate2(trx, id, projectId2, root, record4, prepared, call);
50511
50624
  if (v2.errors.length)
50512
50625
  return { v: v2, done: null, refused: null };
@@ -50520,7 +50633,7 @@ async function checkText(db, id, projectId2, root, record4, call, probe) {
50520
50633
  return { v: v2, done: null, refused: e.message };
50521
50634
  }
50522
50635
  });
50523
- const errors3 = [...v.errors, ...refused === null ? [] : [refused]];
50636
+ const errors3 = [...v.errors, ...refused2 === null ? [] : [refused2]];
50524
50637
  const lines = [
50525
50638
  ...errors3.map((e) => `✗ ${e}`),
50526
50639
  ...[...new Set(v.problems)].map((p) => `△ ${p}`),
@@ -50901,9 +51014,9 @@ server.registerTool("forget_apply", {
50901
51014
  const seen = await previewForget(dbFile(), p.projectId, ids);
50902
51015
  const n = seen.sources.length;
50903
51016
  if (n > 0) {
50904
- const refused = (why) => new Error(`${why}, so nothing was forgotten`);
51017
+ const refused2 = (why) => new Error(`${why}, so nothing was forgotten`);
50905
51018
  if (!server.server.getClientCapabilities()?.elicitation)
50906
- throw refused("This host cannot ask you directly (run /sphica:forget in Claude Code)");
51019
+ throw refused2("This host cannot ask you directly (run /sphica:forget in Claude Code)");
50907
51020
  let answer;
50908
51021
  try {
50909
51022
  answer = await server.server.elicitInput({
@@ -50923,14 +51036,14 @@ ${forgetText(seen, dbFile())}`,
50923
51036
  }
50924
51037
  }, { timeout: ANSWER_MS, signal: extra.signal });
50925
51038
  } catch (e) {
50926
- throw refused(`The confirmation did not come back (${head(reason(e), 200)})`);
51039
+ throw refused2(`The confirmation did not come back (${head(reason(e), 200)})`);
50927
51040
  }
50928
51041
  if (extra.signal.aborted)
50929
- throw refused("The call was cancelled");
51042
+ throw refused2("The call was cancelled");
50930
51043
  if (answer.action !== "accept")
50931
- throw refused(`You ${answer.action === "decline" ? "declined" : "cancelled"}`);
51044
+ throw refused2(`You ${answer.action === "decline" ? "declined" : "cancelled"}`);
50932
51045
  if (String(answer.content?.confirm ?? "").trim() !== String(n))
50933
- throw refused(`The number typed does not match ${n}`);
51046
+ throw refused2(`The number typed does not match ${n}`);
50934
51047
  }
50935
51048
  const done = await applyForget(dbFile(), p.projectId, ids, seen, extra.signal);
50936
51049
  return [