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.
package/dist/deliver.js CHANGED
@@ -11474,16 +11474,32 @@ function listFiles(root, kind, { pathspecs = [], ...limits } = {}) {
11474
11474
  var originUrl = (root, limits) => text(root, ["remote", "get-url", "origin"], limits).trim();
11475
11475
  async function worktreeStatus(root, deadline) {
11476
11476
  const out = await inIsolation(root, [{ kind: "status" }], { deadline, max: 16 * 1024 * 1024 });
11477
- return out?.[0] ?? null;
11477
+ return Array.isArray(out) ? out[0] ?? null : null;
11478
11478
  }
11479
11479
  async function worktreeDiff(root, from, { max, deadline }) {
11480
11480
  const out = await inIsolation(root, [{ kind: "diff", from: objectId(from) }], { deadline, max });
11481
- return out && out.length === 2 ? { patch: out[0] ?? "", names: list(out[1] ?? "") } : null;
11481
+ if (!Array.isArray(out))
11482
+ return out;
11483
+ return out.length === 2 ? { patch: out[0] ?? "", names: list(out[1] ?? "") } : { unread: "failed" };
11482
11484
  }
11483
11485
  function workerFile() {
11484
11486
  const js = path2.join(import.meta.dirname, "git-worker.js");
11485
11487
  return fs.existsSync(js) ? js : path2.join(import.meta.dirname, "git-worker.ts");
11486
11488
  }
11489
+ function unreadOf(e) {
11490
+ const code = e?.code;
11491
+ return { unread: code === "ETIMEDOUT" ? "timeout" : code === "ENOBUFS" ? "too_large" : "failed" };
11492
+ }
11493
+ function refusalText(code) {
11494
+ const folder = "Sphica's git folder (.sphica/git under your home directory)";
11495
+ return {
11496
+ hooks_not_empty: `the hooks folder inside ${folder}, which Sphica keeps empty, holds files`,
11497
+ not_plain_dir: "one of Sphica's folders under your home directory (.sphica, .sphica/git, or .sphica/git/hooks) is a link or not a directory",
11498
+ outside_home: `${folder} leads out of your home directory`,
11499
+ overlaps_worktree: `${folder} overlaps this work tree, which an agent can write`,
11500
+ overlaps_tmp: `${folder} overlaps the temp directory, which an agent can write`
11501
+ }[code];
11502
+ }
11487
11503
  function inIsolation(root, ops, { deadline, max }) {
11488
11504
  return new Promise((resolve) => {
11489
11505
  let child;
@@ -11495,7 +11511,7 @@ function inIsolation(root, ops, { deadline, max }) {
11495
11511
  detached: process.platform !== "win32"
11496
11512
  });
11497
11513
  } catch {
11498
- resolve(null);
11514
+ resolve({ unread: "failed" });
11499
11515
  return;
11500
11516
  }
11501
11517
  const chunks = [];
@@ -11506,7 +11522,7 @@ function inIsolation(root, ops, { deadline, max }) {
11506
11522
  return;
11507
11523
  done = true;
11508
11524
  clearTimeout(timer);
11509
- if (out === null) {
11525
+ if (!Array.isArray(out)) {
11510
11526
  try {
11511
11527
  if (process.platform !== "win32" && child.pid)
11512
11528
  process.kill(-child.pid, "SIGKILL");
@@ -11521,25 +11537,30 @@ function inIsolation(root, ops, { deadline, max }) {
11521
11537
  }
11522
11538
  resolve(out);
11523
11539
  };
11524
- const timer = setTimeout(() => finish(null), deadline);
11540
+ const timer = setTimeout(() => finish({ unread: "timeout" }), deadline);
11525
11541
  const cap = 6 * max * Math.max(1, ops.length * 2) + 64 * 1024;
11526
11542
  child.stdout?.on("data", (c) => {
11527
11543
  size += c.length;
11528
11544
  if (size > cap)
11529
- finish(null);
11545
+ finish({ unread: "too_large" });
11530
11546
  else
11531
11547
  chunks.push(c);
11532
11548
  });
11533
- child.on("error", () => finish(null));
11549
+ child.on("error", () => finish({ unread: "failed" }));
11534
11550
  child.on("close", () => {
11535
11551
  try {
11536
11552
  const result = JSON.parse(Buffer.concat(chunks).toString("utf8"));
11537
- finish(result.ok ? result.out : null);
11553
+ if (result.ok)
11554
+ finish(result.out);
11555
+ else if ("refused" in result)
11556
+ finish({ unread: "refused", code: result.refused });
11557
+ else
11558
+ finish({ unread: result.failed });
11538
11559
  } catch {
11539
- finish(null);
11560
+ finish({ unread: "failed" });
11540
11561
  }
11541
11562
  });
11542
- child.stdin?.on("error", () => finish(null));
11563
+ child.stdin?.on("error", () => finish({ unread: "failed" }));
11543
11564
  child.stdin?.end(JSON.stringify({
11544
11565
  root,
11545
11566
  ops,
@@ -13144,6 +13165,13 @@ function reviewCall(input) {
13144
13165
  return null;
13145
13166
  return { name, args: typeof args === "string" ? args.slice(0, 1000) : "" };
13146
13167
  }
13168
+ function unreadText(why) {
13169
+ if (why.unread === "refused")
13170
+ return `Sphica's git worker would not start, because ${refusalText(why.code)} (run sphica doctor in this work tree)`;
13171
+ if (why.unread === "failed")
13172
+ return "the change could not be read (git failed or could not be started)";
13173
+ return "the change could not be read in time, or is too large to read (over 4 MB of diff or file names)";
13174
+ }
13147
13175
  async function localChange(root, args) {
13148
13176
  if (/^\s*#?\d+\s*$|(^|\s)#\d+\b|\/pull\/\d+/.test(args))
13149
13177
  return { problem: "it names a pull request, and Sphica sees only the local change" };
@@ -13166,15 +13194,13 @@ async function localChange(root, args) {
13166
13194
  return { problem: `HEAD shares no history with ${base}` };
13167
13195
  }
13168
13196
  const got = await worktreeDiff(root, from, { max: MAX_DIFF, deadline: left() });
13197
+ if ("unread" in got)
13198
+ return { problem: unreadText(got) };
13169
13199
  let untracked;
13170
13200
  try {
13171
- if (!got)
13172
- throw new Error("no diff");
13173
13201
  untracked = listFiles(root, "untracked", { max: MAX_DIFF, timeout: left() });
13174
- } catch {
13175
- return {
13176
- problem: "the change could not be read in time, or is too large to read (over 4 MB of diff or file names)"
13177
- };
13202
+ } catch (e) {
13203
+ return { problem: unreadText(unreadOf(e)) };
13178
13204
  }
13179
13205
  const { patch: diff, names } = got;
13180
13206
  const files = parseDiff(diff);
@@ -13486,14 +13512,17 @@ var noted = (text2, lead, notes) => {
13486
13512
  ${l}`).join("");
13487
13513
  return { text: note ? `${text2 || lead}${note}` : text2, note };
13488
13514
  };
13515
+ var sound = (unit) => sql`exists (select 1 from unit o where o.id = ${sql.ref(unit)} and o.extraction = 'supported' and o.unsourced = 0)`;
13489
13516
  var deliverable = (db, projectId2, asOf) => db.selectFrom("unit as u").where("u.project_id", "=", projectId2).$call((q) => asOf === undefined ? q.where("u.lifecycle", "=", "active") : q.where(sql`(select s.to_state from unit_state s where s.unit_id = u.id and s.at <= ${asOf}
13490
13517
  order by s.at desc, s.id desc limit 1) = 'active'`)).where("u.extraction", "=", "supported").where("u.unsourced", "=", 0).where(({ not, exists, selectFrom }) => not(exists(selectFrom("unit_link as l").select("l.from_unit").where("l.kind", "=", "conflicts").$call((q) => asOf === undefined ? q.where("l.resolved_at", "is", null) : q.where("l.added_at", "<=", asOf).where((eb) => eb.or([eb("l.resolved_at", "is", null), eb("l.resolved_at", ">", asOf)]))).where((eb) => eb.or([
13491
13518
  eb.and([
13492
13519
  eb("l.from_unit", "=", eb.ref("u.id")),
13520
+ sound("l.to_unit"),
13493
13521
  eb.or([eb.not(ownerAdopted("u.id", asOf)), ownerAdopted("l.to_unit", asOf)])
13494
13522
  ]),
13495
13523
  eb.and([
13496
13524
  eb("l.to_unit", "=", eb.ref("u.id")),
13525
+ sound("l.from_unit"),
13497
13526
  eb.or([eb.not(ownerAdopted("u.id", asOf)), ownerAdopted("l.from_unit", asOf)])
13498
13527
  ])
13499
13528
  ])))));
@@ -104,6 +104,13 @@ function configGet(root, key, limits) {
104
104
  }
105
105
 
106
106
  // server/src/git-worker.ts
107
+ class Refused extends Error {
108
+ code;
109
+ constructor(code) {
110
+ super(code);
111
+ this.code = code;
112
+ }
113
+ }
107
114
  var INDEX_LIMIT = 256 * 1024 * 1024;
108
115
  var FILE_LIMIT = 1024 * 1024;
109
116
  var STALE_MS = 60 * 60 * 1000;
@@ -114,11 +121,13 @@ var inside = (root, p) => {
114
121
  var overlap = (a, b) => inside(a, b) || inside(b, a);
115
122
  function ownDir(parent, name) {
116
123
  const dir = path2.join(parent, name);
117
- if (!fs2.existsSync(dir))
124
+ let st = fs2.lstatSync(dir, { throwIfNoEntry: false });
125
+ if (!st) {
118
126
  fs2.mkdirSync(dir, { mode: 448 });
119
- const st = fs2.lstatSync(dir);
127
+ st = fs2.lstatSync(dir);
128
+ }
120
129
  if (!st.isDirectory() || st.isSymbolicLink())
121
- throw new Error(`${dir} is not a plain directory`);
130
+ throw new Refused("not_plain_dir");
122
131
  return dir;
123
132
  }
124
133
  function isolatedHome(root) {
@@ -126,18 +135,26 @@ function isolatedHome(root) {
126
135
  const git = ownDir(ownDir(home, ".sphica"), "git");
127
136
  const hooks = ownDir(git, "hooks");
128
137
  if (fs2.readdirSync(hooks).length)
129
- throw new Error(`${hooks} is not empty`);
138
+ throw new Refused("hooks_not_empty");
130
139
  const iso = fs2.mkdtempSync(path2.join(git, "iso-"));
131
- const st = fs2.lstatSync(iso);
132
- if (!st.isDirectory() || st.isSymbolicLink())
133
- throw new Error(`${iso} is not a plain directory`);
134
- const real = fs2.realpathSync(iso);
135
- if (!inside(home, real) || real !== iso)
136
- throw new Error(`${iso} leads out of HOME`);
137
- for (const other of [root, os2.tmpdir()]) {
138
- const otherReal = fs2.realpathSync(other);
139
- if (overlap(real, otherReal) || overlap(git, otherReal))
140
- throw new Error(`${git} overlaps ${otherReal}, which an agent can write`);
140
+ try {
141
+ const st = fs2.lstatSync(iso);
142
+ if (!st.isDirectory() || st.isSymbolicLink())
143
+ throw new Refused("not_plain_dir");
144
+ const real = fs2.realpathSync(iso);
145
+ if (!inside(home, real) || real !== iso)
146
+ throw new Refused("outside_home");
147
+ for (const [other, code] of [
148
+ [root, "overlaps_worktree"],
149
+ [os2.tmpdir(), "overlaps_tmp"]
150
+ ]) {
151
+ const otherReal = fs2.realpathSync(other);
152
+ if (overlap(real, otherReal) || overlap(git, otherReal))
153
+ throw new Refused(code);
154
+ }
155
+ } catch (e) {
156
+ fs2.rmSync(iso, { recursive: true, force: true });
157
+ throw e;
141
158
  }
142
159
  return { git, iso, hooks };
143
160
  }
@@ -317,8 +334,12 @@ function work(request) {
317
334
  sweep(git);
318
335
  const remaining = () => ({ timeout: Math.max(1, Math.floor(request.until - Date.now())) });
319
336
  try {
337
+ if (request.ops.every((op) => op.kind === "place"))
338
+ return { ok: true, out: [] };
320
339
  const { env, options } = prepare(request.root, iso, hooks, remaining);
321
340
  const out = request.ops.flatMap((op) => {
341
+ if (op.kind === "place")
342
+ return [];
322
343
  const run2 = (args) => execFileSync2("git", ["-C", request.root, ...options, ...args], {
323
344
  cwd: startDir(),
324
345
  env,
@@ -340,7 +361,13 @@ function work(request) {
340
361
  fs2.rmSync(iso, { recursive: true, force: true });
341
362
  }
342
363
  } catch (e) {
343
- return { ok: false, error: e.message };
364
+ if (e instanceof Refused)
365
+ return { ok: false, refused: e.code };
366
+ const code = e.code;
367
+ return {
368
+ ok: false,
369
+ failed: code === "ETIMEDOUT" ? "timeout" : code === "ENOBUFS" ? "too_large" : "failed"
370
+ };
344
371
  }
345
372
  }
346
373
  if (process.argv[1] && path2.resolve(process.argv[1]) === path2.resolve(import.meta.filename ?? "")) {
@@ -351,7 +378,7 @@ if (process.argv[1] && path2.resolve(process.argv[1]) === path2.resolve(import.m
351
378
  try {
352
379
  request = JSON.parse(Buffer.concat(chunks).toString("utf8"));
353
380
  } catch {
354
- process.stdout.write(JSON.stringify({ ok: false, error: "the request is not JSON" }));
381
+ process.stdout.write(JSON.stringify({ ok: false, failed: "failed" }));
355
382
  return;
356
383
  }
357
384
  process.stdout.write(JSON.stringify(work(request)));