@workit-dev/pi 2.22.1 → 2.22.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/worker.js +419 -92
  2. package/dist/workit.js +1288 -983
  3. package/package.json +1 -1
package/dist/worker.js CHANGED
@@ -6707,6 +6707,8 @@ var ENVELOPE = new Set([
6707
6707
  ]);
6708
6708
  var NOT_FLAT = new Set(["schemaVersion", "action", "workspaceId"]);
6709
6709
  // packages/workit-core/src/core/workit-paths.mjs
6710
+ import { createHash as createHash2 } from "node:crypto";
6711
+ import { existsSync, lstatSync, mkdirSync, realpathSync, renameSync, writeFileSync } from "node:fs";
6710
6712
  import os from "node:os";
6711
6713
  import path from "node:path";
6712
6714
  var WORKIT_DIR_OVERRIDES = {
@@ -6757,6 +6759,17 @@ var workitDir = (kind, env = process.env, platform = process.platform) => {
6757
6759
  };
6758
6760
  var configDir = (env, platform) => workitDir("config", env, platform);
6759
6761
  var stateDir = (env, platform) => workitDir("state", env, platform);
6762
+ var folderSlug = (dir, platform = process.platform) => {
6763
+ let canonical = dir;
6764
+ try {
6765
+ canonical = realpathSync.native(dir);
6766
+ } catch {}
6767
+ if (platform === "win32")
6768
+ canonical = canonical.toLowerCase();
6769
+ const base = path.basename(canonical).replace(/[^A-Za-z0-9._-]/g, "_").slice(0, 40) || "root";
6770
+ return `${base}-${createHash2("sha256").update(canonical).digest("hex").slice(0, 12)}`;
6771
+ };
6772
+ var folderStoreDir = (dir, env, platform) => path.join(stateDir(env, platform), "folders", folderSlug(dir, platform));
6760
6773
 
6761
6774
  // packages/workit-core/src/core/config.ts
6762
6775
  var PRESETS = {
@@ -6792,11 +6805,11 @@ var COMMIT_PRESETS = [
6792
6805
 
6793
6806
  // packages/workit-core/src/core/workspaces.ts
6794
6807
  import { spawnSync as spawnSync4 } from "node:child_process";
6795
- import { existsSync as existsSync2, lstatSync as lstatSync2, readFileSync as readFileSync3, realpathSync as realpathSync3 } from "node:fs";
6808
+ import { existsSync as existsSync3, lstatSync as lstatSync3, readFileSync as readFileSync3, realpathSync as realpathSync4 } from "node:fs";
6796
6809
  import path6 from "node:path";
6797
6810
 
6798
6811
  // packages/workit-core/src/core/remotes.ts
6799
- import { lstatSync, readFileSync as readFileSync2, realpathSync as realpathSync2, statSync } from "node:fs";
6812
+ import { lstatSync as lstatSync2, readFileSync as readFileSync2, realpathSync as realpathSync3, statSync } from "node:fs";
6800
6813
  import path5 from "node:path";
6801
6814
 
6802
6815
  // packages/workit-core/src/git/rev.ts
@@ -7134,7 +7147,7 @@ function resolveSshHost(alias, sshConfig) {
7134
7147
 
7135
7148
  // packages/workit-core/src/core/account-home.ts
7136
7149
  import { spawnSync as spawnSync3 } from "node:child_process";
7137
- import { existsSync, readFileSync, readlinkSync, realpathSync } from "node:fs";
7150
+ import { existsSync as existsSync2, readFileSync, readlinkSync, realpathSync as realpathSync2 } from "node:fs";
7138
7151
  import os3 from "node:os";
7139
7152
  var WINDOWS = "C:\\Windows";
7140
7153
  var SYSTEM32 = `${WINDOWS}\\System32\\`;
@@ -7225,7 +7238,7 @@ var canonical2 = (real) => {
7225
7238
  };
7226
7239
  var isRepoDir = (dir) => ["HEAD", "objects", "refs"].every((name) => {
7227
7240
  try {
7228
- lstatSync(`${dir}/${name}`);
7241
+ lstatSync2(`${dir}/${name}`);
7229
7242
  return true;
7230
7243
  } catch {
7231
7244
  return false;
@@ -7234,7 +7247,7 @@ var isRepoDir = (dir) => ["HEAD", "objects", "refs"].every((name) => {
7234
7247
  var gitDirAt = (at, depth = 0) => {
7235
7248
  let real;
7236
7249
  try {
7237
- real = realpathSync2.native(at);
7250
+ real = realpathSync3.native(at);
7238
7251
  } catch {
7239
7252
  return null;
7240
7253
  }
@@ -7253,7 +7266,7 @@ var gitDirAt = (at, depth = 0) => {
7253
7266
  common = readFileSync2(`${real}/commondir`, "utf8").trim();
7254
7267
  } catch {}
7255
7268
  if (common !== undefined) {
7256
- lstatSync(`${real}/HEAD`);
7269
+ lstatSync2(`${real}/HEAD`);
7257
7270
  return gitDirAt(path5.isAbsolute(common) ? common : `${real}/${common}`, depth + 1);
7258
7271
  }
7259
7272
  if (!isRepoDir(real))
@@ -7262,11 +7275,11 @@ var gitDirAt = (at, depth = 0) => {
7262
7275
  for (const name of ["refs", "objects", "config", "packed-refs"]) {
7263
7276
  const item = `${real}/${name}`;
7264
7277
  try {
7265
- lstatSync(item);
7278
+ lstatSync2(item);
7266
7279
  } catch {
7267
7280
  continue;
7268
7281
  }
7269
- const resolved = realpathSync2.native(item);
7282
+ const resolved = realpathSync3.native(item);
7270
7283
  const rel = path5.relative(real, resolved);
7271
7284
  if (rel && !rel.startsWith("..") && !path5.isAbsolute(rel))
7272
7285
  continue;
@@ -7574,8 +7587,8 @@ var probeCheckout = (cwd) => {
7574
7587
  if (cached !== undefined)
7575
7588
  return cached;
7576
7589
  let dir = path6.resolve(cwd);
7577
- while (!existsSync2(path6.join(dir, ".git"))) {
7578
- if (["HEAD", "objects", "refs"].every((name) => existsSync2(path6.join(dir, name)))) {
7590
+ while (!existsSync3(path6.join(dir, ".git"))) {
7591
+ if (["HEAD", "objects", "refs"].every((name) => existsSync3(path6.join(dir, name)))) {
7579
7592
  const real = realpathOf(dir);
7580
7593
  const owner = ownInternals(real) === true ? gitDirOwner(real) : null;
7581
7594
  if (owner === null)
@@ -7642,7 +7655,7 @@ var probeCheckout = (cwd) => {
7642
7655
  };
7643
7656
  var ownGitDir = (dotGit, real) => {
7644
7657
  try {
7645
- return lstatSync2(dotGit).isDirectory() && realpathOf(dotGit) === real;
7658
+ return lstatSync3(dotGit).isDirectory() && realpathOf(dotGit) === real;
7646
7659
  } catch {
7647
7660
  return false;
7648
7661
  }
@@ -7653,13 +7666,13 @@ var ownInternals = (real) => {
7653
7666
  for (const name of INTERNALS) {
7654
7667
  const at = path6.join(real, name);
7655
7668
  try {
7656
- lstatSync2(at);
7669
+ lstatSync3(at);
7657
7670
  } catch {
7658
7671
  continue;
7659
7672
  }
7660
7673
  let resolved;
7661
7674
  try {
7662
- resolved = (realpathSync3.native ?? realpathSync3)(at);
7675
+ resolved = (realpathSync4.native ?? realpathSync4)(at);
7663
7676
  } catch {
7664
7677
  return false;
7665
7678
  }
@@ -7702,7 +7715,7 @@ var matchingEntries = (cwd, entries) => {
7702
7715
  return main ? match(main) : direct;
7703
7716
  };
7704
7717
  var realpathOf = (p) => {
7705
- const real = realpathSync3.native ?? realpathSync3;
7718
+ const real = realpathSync4.native ?? realpathSync4;
7706
7719
  try {
7707
7720
  return real(p);
7708
7721
  } catch {
@@ -7880,8 +7893,317 @@ var homePattern = new RegExp(`${escapeRegExp(os4.homedir())}(?=[/\\\\]|$)|\\$HOM
7880
7893
 
7881
7894
  // packages/workit-core/src/store/paths.ts
7882
7895
  import { spawnSync as spawnSync5 } from "node:child_process";
7896
+ import fs5 from "node:fs";
7897
+ import path8 from "node:path";
7898
+
7899
+ // packages/workit-core/src/store/folder-store.ts
7883
7900
  import fs4 from "node:fs";
7901
+ import os5 from "node:os";
7884
7902
  import path7 from "node:path";
7903
+ var FOLDER_CONFIG = new Set(["checks.json", "risk-paths"]);
7904
+ var MOVED_POINTER = "MOVED-TO-STATE.txt";
7905
+ var V2_LEFTOVERS = new Set(["recovery", "index.json"]);
7906
+ var isV2TaskStub = (rel) => /^tasks[/\\][^/\\]+\.json$/u.test(rel);
7907
+ var MARKER = "workspace.json";
7908
+ var v2Workspace = (workit) => {
7909
+ let bytes;
7910
+ try {
7911
+ bytes = fs4.readFileSync(path7.join(workit, MARKER), "utf8");
7912
+ } catch {
7913
+ return null;
7914
+ }
7915
+ try {
7916
+ const value = JSON.parse(bytes);
7917
+ return value?.store?.format !== "workit-store";
7918
+ } catch {
7919
+ return true;
7920
+ }
7921
+ };
7922
+ var generatedIgnore = (file) => {
7923
+ try {
7924
+ return /^\*\n(!risk-paths\n)?$/u.test(fs4.readFileSync(file, "utf8"));
7925
+ } catch {
7926
+ return false;
7927
+ }
7928
+ };
7929
+ var exists = (file) => {
7930
+ try {
7931
+ fs4.lstatSync(file);
7932
+ return true;
7933
+ } catch {
7934
+ return false;
7935
+ }
7936
+ };
7937
+ var isDir = (file) => {
7938
+ try {
7939
+ return fs4.lstatSync(file).isDirectory();
7940
+ } catch {
7941
+ return false;
7942
+ }
7943
+ };
7944
+ var onlyStubs = (tasks) => {
7945
+ try {
7946
+ return fs4.readdirSync(tasks).every((entry) => isV2TaskStub(`tasks/${entry}`));
7947
+ } catch {
7948
+ return false;
7949
+ }
7950
+ };
7951
+ var strayEntries = (workit) => {
7952
+ if (!isDir(workit))
7953
+ return [];
7954
+ let names;
7955
+ try {
7956
+ names = fs4.readdirSync(workit);
7957
+ } catch {
7958
+ return [];
7959
+ }
7960
+ return names.filter((name) => !FOLDER_CONFIG.has(name) && !V2_LEFTOVERS.has(name) && name !== MOVED_POINTER && name !== MARKER && !(name === ".gitignore" && generatedIgnore(path7.join(workit, name))) && !(name === "tasks" && onlyStubs(path7.join(workit, name))));
7961
+ };
7962
+ var folderStoreIo = {
7963
+ rename: (from, to) => fs4.renameSync(from, to),
7964
+ copyFile: (from, to) => fs4.copyFileSync(from, to, fs4.constants.COPYFILE_EXCL),
7965
+ judgedStale: () => {}
7966
+ };
7967
+ var tempSeq = 0;
7968
+ var tempBeside = (file) => path7.join(path7.dirname(file), `.${path7.basename(file)}.workit-move-${process.pid}-${tempSeq += 1}`);
7969
+ var copyInto = (from, to) => {
7970
+ const stat = fs4.lstatSync(from);
7971
+ if (stat.isDirectory()) {
7972
+ fs4.mkdirSync(to, { recursive: true });
7973
+ for (const name of fs4.readdirSync(from))
7974
+ copyInto(path7.join(from, name), path7.join(to, name));
7975
+ return;
7976
+ }
7977
+ if (exists(to))
7978
+ throw new Error(`${to} appeared during the move`);
7979
+ const temp = tempBeside(to);
7980
+ try {
7981
+ if (stat.isSymbolicLink())
7982
+ fs4.symlinkSync(fs4.readlinkSync(from), temp);
7983
+ else
7984
+ folderStoreIo.copyFile(from, temp);
7985
+ folderStoreIo.rename(temp, to);
7986
+ } catch (error) {
7987
+ fs4.rmSync(temp, { force: true });
7988
+ throw error;
7989
+ }
7990
+ };
7991
+ var sameFile = (left, right) => {
7992
+ try {
7993
+ const [a, b] = [fs4.lstatSync(left), fs4.lstatSync(right)];
7994
+ if (!a.isFile() || !b.isFile() || a.size !== b.size)
7995
+ return false;
7996
+ return fs4.readFileSync(left).equals(fs4.readFileSync(right));
7997
+ } catch {
7998
+ return false;
7999
+ }
8000
+ };
8001
+ var merge2 = (from, to, rel, moved) => {
8002
+ if (isV2TaskStub(rel))
8003
+ return [];
8004
+ if (!exists(to)) {
8005
+ try {
8006
+ folderStoreIo.rename(from, to);
8007
+ } catch {
8008
+ try {
8009
+ copyInto(from, to);
8010
+ fs4.rmSync(from, { recursive: true, force: true });
8011
+ } catch {
8012
+ return exists(from) ? [rel] : [];
8013
+ }
8014
+ }
8015
+ moved.push(rel);
8016
+ return [];
8017
+ }
8018
+ if (sameFile(from, to)) {
8019
+ fs4.rmSync(from, { force: true });
8020
+ moved.push(rel);
8021
+ return [];
8022
+ }
8023
+ if (!isDir(from) || !isDir(to))
8024
+ return [rel];
8025
+ const kept = fs4.readdirSync(from).flatMap((name) => merge2(path7.join(from, name), path7.join(to, name), `${rel}/${name}`, moved));
8026
+ try {
8027
+ if (fs4.readdirSync(from).length === 0)
8028
+ fs4.rmdirSync(from);
8029
+ } catch {}
8030
+ return kept;
8031
+ };
8032
+ var LOCK_STALE_MS = 1e4;
8033
+ var LOCK_WAIT_MS = 250;
8034
+ var pause = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
8035
+ var readLock = (lock) => fs4.readFileSync(lock, "utf8");
8036
+ var staleLock = (lock) => {
8037
+ try {
8038
+ const { mtimeMs } = fs4.statSync(lock);
8039
+ const text = readLock(lock);
8040
+ if (Date.now() - mtimeMs > LOCK_STALE_MS)
8041
+ return { text, mtimeMs };
8042
+ const pid = Number(text.trim().split(/\s+/u)[0]);
8043
+ if (!Number.isInteger(pid) || pid <= 0)
8044
+ return null;
8045
+ try {
8046
+ process.kill(pid, 0);
8047
+ return null;
8048
+ } catch (error) {
8049
+ return error.code === "ESRCH" ? { text, mtimeMs } : null;
8050
+ }
8051
+ } catch {
8052
+ return null;
8053
+ }
8054
+ };
8055
+ var removeStaleLock = (lock, judged) => {
8056
+ const aside = `${lock}.stale-${process.pid}-${crypto.randomUUID()}`;
8057
+ try {
8058
+ fs4.renameSync(lock, aside);
8059
+ } catch {
8060
+ return false;
8061
+ }
8062
+ let same = false;
8063
+ try {
8064
+ same = fs4.statSync(aside).mtimeMs === judged.mtimeMs && readLock(aside) === judged.text;
8065
+ } catch {}
8066
+ if (!same)
8067
+ try {
8068
+ fs4.linkSync(aside, lock);
8069
+ } catch {}
8070
+ fs4.rmSync(aside, { force: true });
8071
+ return same;
8072
+ };
8073
+ var takeMoveLock = (target) => {
8074
+ const lock = `${target}.lock`;
8075
+ const mine = `${process.pid} ${crypto.randomUUID()}
8076
+ `;
8077
+ const deadline = Date.now() + LOCK_WAIT_MS;
8078
+ for (;; ) {
8079
+ try {
8080
+ const fd = fs4.openSync(lock, "wx", 384);
8081
+ fs4.writeSync(fd, mine);
8082
+ fs4.closeSync(fd);
8083
+ return () => {
8084
+ try {
8085
+ if (readLock(lock) === mine)
8086
+ fs4.rmSync(lock, { force: true });
8087
+ } catch {}
8088
+ };
8089
+ } catch (error) {
8090
+ if (error.code !== "EEXIST")
8091
+ return null;
8092
+ }
8093
+ const stale = staleLock(lock);
8094
+ if (stale) {
8095
+ folderStoreIo.judgedStale();
8096
+ if (removeStaleLock(lock, stale))
8097
+ continue;
8098
+ }
8099
+ if (Date.now() >= deadline)
8100
+ return null;
8101
+ pause(25);
8102
+ }
8103
+ };
8104
+ var canonical3 = (dir) => {
8105
+ try {
8106
+ return fs4.realpathSync.native(dir);
8107
+ } catch {
8108
+ return path7.resolve(dir);
8109
+ }
8110
+ };
8111
+ var below = (parent, child) => {
8112
+ const rel = path7.relative(parent, child);
8113
+ return rel !== "" && !rel.startsWith("..") && !path7.isAbsolute(rel);
8114
+ };
8115
+ var TEST_RUN_REFUSALS = "folder-store-refusals.log";
8116
+ var testRunFolderRefusal = (folder, hasStray, env = process.env, tempDir = os5.tmpdir()) => {
8117
+ if (env.WORKIT_TEST_RUN === undefined || !hasStray)
8118
+ return null;
8119
+ const real = canonical3(folder);
8120
+ const tmp = canonical3(tempDir);
8121
+ if (below(tmp, real))
8122
+ return null;
8123
+ return `workit test run (WORKIT_TEST_RUN): refusing to move ${path7.join(real, ".workit")} into the run's throwaway state dir; a test must work in a fresh dir inside ${tmp}, never the temp dir itself, a parent of it or a folder outside it (#892)`;
8124
+ };
8125
+ function migrateFolderStore(folder, target, note = (line) => process.stderr.write(`${line}
8126
+ `)) {
8127
+ const workit = path7.join(folder, ".workit");
8128
+ const names = strayEntries(workit);
8129
+ const refusal = testRunFolderRefusal(folder, names.length > 0);
8130
+ if (refusal) {
8131
+ const run = process.env.WORKIT_TEST_RUN ?? "";
8132
+ if (path7.isAbsolute(run))
8133
+ try {
8134
+ fs4.appendFileSync(path7.join(run, TEST_RUN_REFUSALS), `${refusal}
8135
+ `);
8136
+ } catch {}
8137
+ throw new Error(refusal);
8138
+ }
8139
+ if (names.length === 0 || v2Workspace(workit) === true)
8140
+ return null;
8141
+ try {
8142
+ fs4.mkdirSync(path7.dirname(target), { recursive: true });
8143
+ } catch {
8144
+ return null;
8145
+ }
8146
+ const release = takeMoveLock(target);
8147
+ if (!release)
8148
+ return null;
8149
+ try {
8150
+ return moveLocked(workit, target, note);
8151
+ } finally {
8152
+ release();
8153
+ }
8154
+ }
8155
+ function moveLocked(workit, target, note) {
8156
+ const names = strayEntries(workit);
8157
+ if (names.length === 0 || v2Workspace(workit) === true)
8158
+ return null;
8159
+ try {
8160
+ fs4.mkdirSync(target, { recursive: true });
8161
+ } catch {
8162
+ return null;
8163
+ }
8164
+ const result = { from: workit, to: target, moved: [], kept: [] };
8165
+ for (const name of names) {
8166
+ result.kept.push(...merge2(path7.join(workit, name), path7.join(target, name), name, result.moved));
8167
+ }
8168
+ const left = (() => {
8169
+ try {
8170
+ return fs4.readdirSync(workit);
8171
+ } catch {
8172
+ return [];
8173
+ }
8174
+ })();
8175
+ const v2Left = left.some((name) => V2_LEFTOVERS.has(name) || name === "tasks" && isDir(path7.join(workit, name)));
8176
+ if (!v2Left && result.kept.length === 0) {
8177
+ fs4.rmSync(path7.join(workit, MARKER), { force: true });
8178
+ if (generatedIgnore(path7.join(workit, ".gitignore")))
8179
+ fs4.rmSync(path7.join(workit, ".gitignore"), { force: true });
8180
+ }
8181
+ const pointer = path7.join(workit, MOVED_POINTER);
8182
+ let wrote = false;
8183
+ if (result.kept.length > 0) {
8184
+ const text = `Workit keeps this folder's state in ${target}.
8185
+ Not moved, because the store there already has them or the move failed: ${result.kept.join(", ")}.
8186
+ Merge them by hand (cp -Rn ${workit}/. ${target}/), then delete them here.
8187
+ `;
8188
+ try {
8189
+ if (!exists(pointer) || fs4.readFileSync(pointer, "utf8") !== text) {
8190
+ fs4.writeFileSync(pointer, text);
8191
+ wrote = true;
8192
+ }
8193
+ } catch {}
8194
+ } else {
8195
+ fs4.rmSync(pointer, { force: true });
8196
+ try {
8197
+ if (fs4.readdirSync(workit).length === 0)
8198
+ fs4.rmdirSync(workit);
8199
+ } catch {}
8200
+ }
8201
+ if (result.moved.length > 0 || wrote)
8202
+ note(result.kept.length > 0 ? `workit: moved ${workit} to ${target}; ${result.kept.length} path${result.kept.length === 1 ? "" : "s"} left in place, see ${pointer}` : `workit: moved ${workit} to ${target} (outside a git repository Workit keeps its state there)`);
8203
+ return result;
8204
+ }
8205
+
8206
+ // packages/workit-core/src/store/paths.ts
7885
8207
  var NOT_A_REPO = /not a git repository \(or any (of the parent directories|parent up to)/u;
7886
8208
  var gitRun = (cwd, args) => {
7887
8209
  const run = spawnSync5("git", args, {
@@ -7908,7 +8230,7 @@ var gitRun = (cwd, args) => {
7908
8230
  };
7909
8231
  var real = (file) => {
7910
8232
  try {
7911
- return fs4.realpathSync.native(file);
8233
+ return fs5.realpathSync.native(file);
7912
8234
  } catch {
7913
8235
  return file;
7914
8236
  }
@@ -7922,14 +8244,14 @@ var DISCOVERY_ENV = [
7922
8244
  ];
7923
8245
  var isGitDir = (dir) => {
7924
8246
  try {
7925
- return fs4.statSync(path7.join(dir, "HEAD")).isFile() && fs4.statSync(path7.join(dir, "objects")).isDirectory() && fs4.statSync(path7.join(dir, "refs")).isDirectory();
8247
+ return fs5.statSync(path8.join(dir, "HEAD")).isFile() && fs5.statSync(path8.join(dir, "objects")).isDirectory() && fs5.statSync(path8.join(dir, "refs")).isDirectory();
7926
8248
  } catch {
7927
8249
  return false;
7928
8250
  }
7929
8251
  };
7930
8252
  var reftableRefs = (common) => {
7931
8253
  try {
7932
- return /^\s*refstorage\s*=\s*reftable\s*$/imu.test(fs4.readFileSync(path7.join(common, "config"), "utf8"));
8254
+ return /^\s*refstorage\s*=\s*reftable\s*$/imu.test(fs5.readFileSync(path8.join(common, "config"), "utf8"));
7933
8255
  } catch {
7934
8256
  return false;
7935
8257
  }
@@ -7942,17 +8264,17 @@ function fastGit(root) {
7942
8264
  for (;; ) {
7943
8265
  let stat;
7944
8266
  try {
7945
- stat = fs4.statSync(dir);
8267
+ stat = fs5.statSync(dir);
7946
8268
  } catch {
7947
8269
  return null;
7948
8270
  }
7949
8271
  if (device !== null && stat.dev !== device)
7950
8272
  return { kind: "none" };
7951
8273
  device = stat.dev;
7952
- const dotGit = path7.join(dir, ".git");
8274
+ const dotGit = path8.join(dir, ".git");
7953
8275
  let entry = null;
7954
8276
  try {
7955
- entry = fs4.statSync(dotGit);
8277
+ entry = fs5.statSync(dotGit);
7956
8278
  } catch {}
7957
8279
  let gitDir = null;
7958
8280
  let top = null;
@@ -7963,26 +8285,26 @@ function fastGit(root) {
7963
8285
  else if (entry.isFile()) {
7964
8286
  let text = "";
7965
8287
  try {
7966
- text = fs4.readFileSync(dotGit, "utf8");
8288
+ text = fs5.readFileSync(dotGit, "utf8");
7967
8289
  } catch {
7968
8290
  return null;
7969
8291
  }
7970
8292
  const match = /^gitdir: (.+)$/mu.exec(text);
7971
8293
  if (!match)
7972
8294
  return null;
7973
- gitDir = path7.resolve(dir, match[1].trim());
8295
+ gitDir = path8.resolve(dir, match[1].trim());
7974
8296
  } else
7975
8297
  return null;
7976
- if (!fs4.existsSync(path7.join(gitDir, "HEAD")))
8298
+ if (!fs5.existsSync(path8.join(gitDir, "HEAD")))
7977
8299
  return null;
7978
8300
  } else if (isGitDir(dir))
7979
8301
  gitDir = dir;
7980
8302
  if (gitDir) {
7981
8303
  let common = gitDir;
7982
8304
  try {
7983
- common = path7.resolve(gitDir, fs4.readFileSync(path7.join(gitDir, "commondir"), "utf8").trim());
8305
+ common = path8.resolve(gitDir, fs5.readFileSync(path8.join(gitDir, "commondir"), "utf8").trim());
7984
8306
  } catch {}
7985
- if (!fs4.existsSync(path7.join(common, "objects")))
8307
+ if (!fs5.existsSync(path8.join(common, "objects")))
7986
8308
  return null;
7987
8309
  return {
7988
8310
  kind: "repo",
@@ -7992,28 +8314,33 @@ function fastGit(root) {
7992
8314
  reftable: reftableRefs(common)
7993
8315
  };
7994
8316
  }
7995
- const parent = path7.dirname(dir);
8317
+ const parent = path8.dirname(dir);
7996
8318
  if (parent === dir)
7997
8319
  return { kind: "none" };
7998
8320
  dir = parent;
7999
8321
  }
8000
8322
  }
8323
+ var outsideGit = (root) => {
8324
+ const dir = folderStoreDir(root);
8325
+ migrateFolderStore(root, dir);
8326
+ return { dir, shared: false, top: null };
8327
+ };
8001
8328
  function resolveStore(root) {
8002
- root = path7.resolve(root);
8329
+ root = path8.resolve(root);
8003
8330
  let stat = null;
8004
8331
  try {
8005
- stat = fs4.statSync(root);
8332
+ stat = fs5.statSync(root);
8006
8333
  } catch {}
8007
8334
  if (!stat)
8008
8335
  return new Error(`cannot locate the workit store: ${root} does not exist (removed while workit ran?); nothing is recorded under a missing directory`);
8009
8336
  if (!stat.isDirectory())
8010
- return { dir: path7.join(root, ".workit"), shared: false, top: null };
8337
+ return { dir: path8.join(root, ".workit"), shared: false, top: null };
8011
8338
  const fast = fastGit(root);
8012
8339
  if (fast?.kind === "none")
8013
- return { dir: path7.join(root, ".workit"), shared: false, top: null };
8340
+ return outsideGit(root);
8014
8341
  if (fast?.kind === "repo")
8015
8342
  return {
8016
- dir: path7.join(fast.common, "workit"),
8343
+ dir: path8.join(fast.common, "workit"),
8017
8344
  shared: true,
8018
8345
  top: fast.top,
8019
8346
  git: { gitDir: fast.gitDir, top: fast.top, reftable: fast.reftable }
@@ -8023,13 +8350,13 @@ function resolveStore(root) {
8023
8350
  if (run.ok && common) {
8024
8351
  const top = gitRun(root, ["rev-parse", "--path-format=absolute", "--show-toplevel"]);
8025
8352
  return {
8026
- dir: path7.join(real(path7.resolve(root, common)), "workit"),
8353
+ dir: path8.join(real(path8.resolve(root, common)), "workit"),
8027
8354
  shared: true,
8028
8355
  top: top.ok && top.stdout.trim() ? real(top.stdout.trim()) : null
8029
8356
  };
8030
8357
  }
8031
8358
  if (run.missing || !run.ok && NOT_A_REPO.test(run.stderr))
8032
- return { dir: path7.join(root, ".workit"), shared: false, top: null };
8359
+ return outsideGit(root);
8033
8360
  return new Error(`cannot locate the workit store: git rev-parse failed (${run.stderr.trim().split(`
8034
8361
  `)[0] || "no output"}); fix the repository (git status should work here)`);
8035
8362
  }
@@ -8076,27 +8403,27 @@ var KNOWN_TYPES = new Set([
8076
8403
  var SLOT = Symbol.for("workit.migrationReporter");
8077
8404
 
8078
8405
  // packages/workit-core/src/core/package-root.ts
8079
- import { existsSync as existsSync3 } from "node:fs";
8080
- import path8 from "node:path";
8406
+ import { existsSync as existsSync4 } from "node:fs";
8407
+ import path9 from "node:path";
8081
8408
  import { fileURLToPath } from "node:url";
8082
8409
  var packageRoot = () => {
8083
- let dir = path8.dirname(fileURLToPath(import.meta.url));
8410
+ let dir = path9.dirname(fileURLToPath(import.meta.url));
8084
8411
  while (true) {
8085
- const parent = path8.dirname(dir);
8086
- if (existsSync3(path8.join(dir, "package.json")) || parent === dir)
8412
+ const parent = path9.dirname(dir);
8413
+ if (existsSync4(path9.join(dir, "package.json")) || parent === dir)
8087
8414
  return dir;
8088
8415
  dir = parent;
8089
8416
  }
8090
8417
  };
8091
8418
  var assetRoot = () => {
8092
8419
  const root = packageRoot();
8093
- const assets = path8.join(root, "assets");
8094
- return existsSync3(assets) ? assets : root;
8420
+ const assets = path9.join(root, "assets");
8421
+ return existsSync4(assets) ? assets : root;
8095
8422
  };
8096
8423
 
8097
8424
  // packages/workit-core/src/core/store-lock.ts
8098
8425
  import { spawnSync as spawnSync6 } from "node:child_process";
8099
- import fs5 from "node:fs";
8426
+ import fs6 from "node:fs";
8100
8427
  var metadataLockSchema = object({
8101
8428
  pid: number2().int().nonnegative().safe(),
8102
8429
  processStart: string2().nullable(),
@@ -8113,7 +8440,7 @@ var parseProcStatStart = (stat) => {
8113
8440
  };
8114
8441
  var readProcStat = (pid) => {
8115
8442
  try {
8116
- return fs5.readFileSync(`/proc/${pid}/stat`, "utf8");
8443
+ return fs6.readFileSync(`/proc/${pid}/stat`, "utf8");
8117
8444
  } catch {
8118
8445
  return null;
8119
8446
  }
@@ -8208,9 +8535,9 @@ var PYTHON_RUNNERS = new Set(["uv", "poetry", "pipenv", "pdm", "hatch", "rye"]);
8208
8535
 
8209
8536
  // packages/workit-core/src/ledger.ts
8210
8537
  import { spawnSync as spawnSync7 } from "node:child_process";
8211
- import { createHash as createHash3, randomBytes } from "node:crypto";
8212
- import fs7 from "node:fs";
8213
- import path10 from "node:path";
8538
+ import { createHash as createHash4, randomBytes } from "node:crypto";
8539
+ import fs8 from "node:fs";
8540
+ import path11 from "node:path";
8214
8541
 
8215
8542
  // packages/workit-core/src/core/policy/dep-bump.ts
8216
8543
  var MANIFESTS = new Set(["package.json", "go.mod", "Cargo.toml", "pyproject.toml"]);
@@ -8325,46 +8652,46 @@ var REGULAR = new Set(["100644", "100755"]);
8325
8652
  var TEST_ONLY_KINDS = new Set(["doc", "cosmetic", "generated"]);
8326
8653
 
8327
8654
  // packages/workit-core/src/hooks/standing-signal.ts
8328
- import { createHash as createHash2 } from "node:crypto";
8329
- import fs6 from "node:fs";
8330
- import path9 from "node:path";
8655
+ import { createHash as createHash3 } from "node:crypto";
8656
+ import fs7 from "node:fs";
8657
+ import path10 from "node:path";
8331
8658
  var gitStoreDir = (cwd) => {
8332
8659
  try {
8333
- for (let dir = path9.resolve(cwd);; dir = path9.dirname(dir)) {
8334
- const dotGit = path9.join(dir, ".git");
8335
- if (fs6.existsSync(dotGit)) {
8660
+ for (let dir = path10.resolve(cwd);; dir = path10.dirname(dir)) {
8661
+ const dotGit = path10.join(dir, ".git");
8662
+ if (fs7.existsSync(dotGit)) {
8336
8663
  let gitDir = dotGit;
8337
- if (fs6.statSync(dotGit).isFile()) {
8338
- const target = /^gitdir:\s*(.+)$/mu.exec(fs6.readFileSync(dotGit, "utf8"))?.[1]?.trim();
8664
+ if (fs7.statSync(dotGit).isFile()) {
8665
+ const target = /^gitdir:\s*(.+)$/mu.exec(fs7.readFileSync(dotGit, "utf8"))?.[1]?.trim();
8339
8666
  if (!target)
8340
8667
  return null;
8341
- gitDir = path9.resolve(dir, target);
8342
- const common = path9.join(gitDir, "commondir");
8343
- if (fs6.existsSync(common))
8344
- gitDir = path9.resolve(gitDir, fs6.readFileSync(common, "utf8").trim());
8668
+ gitDir = path10.resolve(dir, target);
8669
+ const common = path10.join(gitDir, "commondir");
8670
+ if (fs7.existsSync(common))
8671
+ gitDir = path10.resolve(gitDir, fs7.readFileSync(common, "utf8").trim());
8345
8672
  }
8346
- return path9.join(gitDir, "workit");
8673
+ return path10.join(gitDir, "workit");
8347
8674
  }
8348
- if (path9.dirname(dir) === dir)
8675
+ if (path10.dirname(dir) === dir)
8349
8676
  return null;
8350
8677
  }
8351
8678
  } catch {
8352
8679
  return null;
8353
8680
  }
8354
8681
  };
8355
- var standingRevFile = (store) => path9.join(store, "standing", "rev");
8356
- var standingSeenFile = (store, session, agentId) => path9.join(store, "standing", "seen", `${createHash2("sha256").update(`${session}\x00${agentId ?? ""}`).digest("hex").slice(0, 32)}.json`);
8682
+ var standingRevFile = (store) => path10.join(store, "standing", "rev");
8683
+ var standingSeenFile = (store, session, agentId) => path10.join(store, "standing", "seen", `${createHash3("sha256").update(`${session}\x00${agentId ?? ""}`).digest("hex").slice(0, 32)}.json`);
8357
8684
  var readStandingRev = (store) => {
8358
8685
  try {
8359
- return fs6.readFileSync(standingRevFile(store), "utf8").trim() || null;
8686
+ return fs7.readFileSync(standingRevFile(store), "utf8").trim() || null;
8360
8687
  } catch {
8361
8688
  return null;
8362
8689
  }
8363
8690
  };
8364
8691
  var writeStandingSeen = (file, seen) => {
8365
8692
  try {
8366
- fs6.mkdirSync(path9.dirname(file), { recursive: true });
8367
- fs6.writeFileSync(file, JSON.stringify(seen));
8693
+ fs7.mkdirSync(path10.dirname(file), { recursive: true });
8694
+ fs7.writeFileSync(file, JSON.stringify(seen));
8368
8695
  } catch {}
8369
8696
  };
8370
8697
 
@@ -8415,14 +8742,14 @@ var git2 = (cwd, args) => {
8415
8742
  };
8416
8743
  var safeRef = (value) => value.length > 0 && !value.startsWith("-");
8417
8744
  function storeRoot(cwd) {
8418
- const location = resolveStore(path10.resolve(cwd));
8745
+ const location = resolveStore(path11.resolve(cwd));
8419
8746
  if (location instanceof Error)
8420
8747
  return err("unavailable", location.message.replace(/; fix the repository.*$/u, ""), / does not exist /u.test(location.message) ? "run workit from a directory that exists" : "fix the repository (git status should work here) or run from a non-git directory");
8421
8748
  return { ok: true, value: { root: location.dir, shared: location.shared } };
8422
8749
  }
8423
8750
  function ledgerPath(cwd) {
8424
8751
  const root = storeRoot(cwd);
8425
- return root.ok ? { ok: true, value: path10.join(root.value.root, "ledger", "ledger.jsonl") } : root;
8752
+ return root.ok ? { ok: true, value: path11.join(root.value.root, "ledger", "ledger.jsonl") } : root;
8426
8753
  }
8427
8754
  var NETWORK_FS_TYPES = new Set([
8428
8755
  26985,
@@ -8436,7 +8763,7 @@ var NETWORK_FS_TYPES = new Set([
8436
8763
  1196443219,
8437
8764
  198183888
8438
8765
  ]);
8439
- var sha2562 = (text) => createHash3("sha256").update(text).digest("hex");
8766
+ var sha2562 = (text) => createHash4("sha256").update(text).digest("hex");
8440
8767
  var chainHash = (prevHash, body) => sha2562(`${prevHash ?? ""}
8441
8768
  ${body}`);
8442
8769
  var TAIL_BYTES = 64 * 1024;
@@ -8471,8 +8798,8 @@ function readLedgerAt(cwd, file, options) {
8471
8798
  let text = "";
8472
8799
  let stat = null;
8473
8800
  try {
8474
- stat = fs7.statSync(file);
8475
- text = fs7.readFileSync(file, "utf8");
8801
+ stat = fs8.statSync(file);
8802
+ text = fs8.readFileSync(file, "utf8");
8476
8803
  } catch (error) {
8477
8804
  if (error.code === "ENOENT")
8478
8805
  return {
@@ -8706,8 +9033,8 @@ var OBSERVED_ONLY = new Set([
8706
9033
  ]);
8707
9034
 
8708
9035
  // packages/workit-core/src/core/model-fallback.ts
8709
- import { mkdirSync, readFileSync as readFileSync4, renameSync, writeFileSync } from "node:fs";
8710
- import path11 from "node:path";
9036
+ import { mkdirSync as mkdirSync2, readFileSync as readFileSync4, renameSync as renameSync2, writeFileSync as writeFileSync2 } from "node:fs";
9037
+ import path12 from "node:path";
8711
9038
  var LIMITS_FILE = "model-limits.json";
8712
9039
  var DEFAULT_LIMIT_WINDOW_MS = 5 * 3600000;
8713
9040
  var KEEP_MS = 9 * 86400000;
@@ -8730,7 +9057,7 @@ var providerOf = (model, host) => {
8730
9057
  return null;
8731
9058
  };
8732
9059
  var empty = () => ({ exhausted: {}, seen: {}, redispatched: {} });
8733
- var storePath = (stateDir) => path11.join(stateDir ?? resolveStateDir(), LIMITS_FILE);
9060
+ var storePath = (stateDir) => path12.join(stateDir ?? resolveStateDir(), LIMITS_FILE);
8734
9061
  var readStore = (stateDir) => {
8735
9062
  try {
8736
9063
  const parsed = JSON.parse(readFileSync4(storePath(stateDir), "utf8"));
@@ -9196,12 +9523,12 @@ var contextReadSchema = object({
9196
9523
  cwd: string2().min(1).optional()
9197
9524
  }).strict();
9198
9525
  // packages/workit-core/src/hooks/budget-signal.ts
9199
- import { createHash as createHash4 } from "node:crypto";
9200
- import fs8 from "node:fs";
9201
- import path12 from "node:path";
9202
- var hash = (text) => createHash4("sha256").update(text).digest("hex").slice(0, 32);
9203
- var budgetDir = (store) => path12.join(store, "budget");
9204
- var agentBase = (store, session, agentId) => path12.join(budgetDir(store), hash(`${session}\x00${agentId ?? ""}`));
9526
+ import { createHash as createHash5 } from "node:crypto";
9527
+ import fs9 from "node:fs";
9528
+ import path13 from "node:path";
9529
+ var hash = (text) => createHash5("sha256").update(text).digest("hex").slice(0, 32);
9530
+ var budgetDir = (store) => path13.join(store, "budget");
9531
+ var agentBase = (store, session, agentId) => path13.join(budgetDir(store), hash(`${session}\x00${agentId ?? ""}`));
9205
9532
  var spawnBudget = (prompt) => {
9206
9533
  const found = /^TIMEBOX:[^\n]*?\b(\d+)\s+tool calls\b/mu.exec(prompt);
9207
9534
  const n = found ? Number(found[1]) : NaN;
@@ -9209,16 +9536,16 @@ var spawnBudget = (prompt) => {
9209
9536
  };
9210
9537
  var startBudget = (store, session, agentId, n) => {
9211
9538
  const dir = budgetDir(store);
9212
- fs8.mkdirSync(dir, { recursive: true });
9539
+ fs9.mkdirSync(dir, { recursive: true });
9213
9540
  const cutoff = Date.now() - 604800000;
9214
- for (const name of fs8.readdirSync(dir))
9541
+ for (const name of fs9.readdirSync(dir))
9215
9542
  try {
9216
- if (fs8.statSync(path12.join(dir, name)).mtimeMs < cutoff)
9217
- fs8.rmSync(path12.join(dir, name));
9543
+ if (fs9.statSync(path13.join(dir, name)).mtimeMs < cutoff)
9544
+ fs9.rmSync(path13.join(dir, name));
9218
9545
  } catch {}
9219
9546
  const base = agentBase(store, session, agentId);
9220
- fs8.writeFileSync(`${base}.json`, JSON.stringify({ limit: n, told: 0 }));
9221
- fs8.rmSync(`${base}.calls`, { force: true });
9547
+ fs9.writeFileSync(`${base}.json`, JSON.stringify({ limit: n, told: 0 }));
9548
+ fs9.rmSync(`${base}.calls`, { force: true });
9222
9549
  };
9223
9550
  var bindBudget = (cwd, session, agentId, prompt) => {
9224
9551
  try {
@@ -9235,8 +9562,8 @@ var bindBudget = (cwd, session, agentId, prompt) => {
9235
9562
  var CODEX_GATED = new Set(["bash", "unifiedexec", "shell", "applypatch", "edit", "write"]);
9236
9563
 
9237
9564
  // packages/workit-core/src/hooks/standing.ts
9238
- import fs9 from "node:fs";
9239
- import path14 from "node:path";
9565
+ import fs10 from "node:fs";
9566
+ import path15 from "node:path";
9240
9567
 
9241
9568
  // packages/workit-core/src/approvals/store.ts
9242
9569
  var REQUEST_TTL_MS = 30 * 60000;
@@ -9745,7 +10072,7 @@ var PI_DESCRIPTOR = {
9745
10072
  };
9746
10073
 
9747
10074
  // packages/workit-core/src/hooks/write-gate.ts
9748
- import path13 from "node:path";
10075
+ import path14 from "node:path";
9749
10076
 
9750
10077
  // packages/workit-core/src/core/live-session.ts
9751
10078
  import { spawnSync as spawnSync8 } from "node:child_process";
@@ -9787,7 +10114,7 @@ var PLACEHOLDERS = new WeakMap;
9787
10114
  var CD2 = new Set(["cd", "chdir", "set-location", "sl"]);
9788
10115
  var PUSHD2 = new Set(["pushd", "push-location"]);
9789
10116
  var POPD2 = new Set(["popd", "pop-location"]);
9790
- var UNKNOWN_DIR = path13.resolve(path13.parse(process.cwd()).root, ".workit-unknown-dir");
10117
+ var UNKNOWN_DIR = path14.resolve(path14.parse(process.cwd()).root, ".workit-unknown-dir");
9791
10118
  var DECLARERS = new Set(["export", "declare", "typeset", "local", "readonly"]);
9792
10119
  var REBINDS_ANY = new Set([
9793
10120
  "source",
@@ -9841,7 +10168,7 @@ var OPENS = new Set(["if", "for", "while", "until", "{"]);
9841
10168
  var CLOSES = new Set(["fi", "done", "}"]);
9842
10169
  var UNFOLLOWED = new Set(["case", "esac", "select", "function"]);
9843
10170
  var TRANSPARENT = new Set(["builtin", "command", "time", "!"]);
9844
- var UNKNOWN_VAR = path13.resolve(path13.parse(process.cwd()).root, ".workit-unknown-var");
10171
+ var UNKNOWN_VAR = path14.resolve(path14.parse(process.cwd()).root, ".workit-unknown-var");
9845
10172
 
9846
10173
  // packages/workit-core/src/hooks/worktree-confine.ts
9847
10174
  var CASE_INSENSITIVE2 = process.platform === "darwin" || process.platform === "win32";
@@ -9870,17 +10197,17 @@ var workitRole = (descriptor, agentType) => {
9870
10197
 
9871
10198
  // packages/workit-core/src/hooks/standing.ts
9872
10199
  var readPlans = (store) => {
9873
- const dir = path14.join(store, "fanouts");
10200
+ const dir = path15.join(store, "fanouts");
9874
10201
  let names;
9875
10202
  try {
9876
- names = fs9.readdirSync(dir).filter((name) => name.endsWith(".json"));
10203
+ names = fs10.readdirSync(dir).filter((name) => name.endsWith(".json"));
9877
10204
  } catch {
9878
10205
  return [];
9879
10206
  }
9880
10207
  const plans = [];
9881
10208
  for (const name of names) {
9882
10209
  try {
9883
- const raw = JSON.parse(fs9.readFileSync(path14.join(dir, name), "utf8"));
10210
+ const raw = JSON.parse(fs10.readFileSync(path15.join(dir, name), "utf8"));
9884
10211
  if (typeof raw.name !== "string" || !Array.isArray(raw.slices))
9885
10212
  continue;
9886
10213
  const texts = (value) => Array.isArray(value) ? value.filter((item) => typeof item === "string" && item !== "") : [];