@workit-dev/pi 2.27.8 → 2.27.9

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/worker.js CHANGED
@@ -6947,11 +6947,6 @@ var escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
6947
6947
  var resolveStateDir = () => stateDir(process.env);
6948
6948
  var homePattern = new RegExp(`${escapeRegExp(os4.homedir())}(?=[/\\\\]|$)|\\$HOME(?=[/\\\\]|$)`, "g");
6949
6949
 
6950
- // packages/workit-core/src/store/paths.ts
6951
- import { spawnSync as spawnSync5 } from "node:child_process";
6952
- import fs5 from "node:fs";
6953
- import path8 from "node:path";
6954
-
6955
6950
  // packages/workit-core/src/store/folder-store.ts
6956
6951
  import fs4 from "node:fs";
6957
6952
  import os5 from "node:os";
@@ -7168,13 +7163,14 @@ var below = (parent, child) => {
7168
7163
  const rel = path7.relative(parent, child);
7169
7164
  return rel !== "" && !rel.startsWith("..") && !path7.isAbsolute(rel);
7170
7165
  };
7166
+ var insideTempDir = (dir, tempDir = os5.tmpdir()) => below(canonical3(tempDir), canonical3(dir));
7171
7167
  var TEST_RUN_REFUSALS = "folder-store-refusals.log";
7172
7168
  var testRunFolderRefusal = (folder, hasStray, env = process.env, tempDir = os5.tmpdir()) => {
7173
7169
  if (env.WORKIT_TEST_RUN === undefined || !hasStray)
7174
7170
  return null;
7175
7171
  const real = canonical3(folder);
7176
7172
  const tmp = canonical3(tempDir);
7177
- if (below(tmp, real))
7173
+ if (insideTempDir(folder, tempDir))
7178
7174
  return null;
7179
7175
  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)`;
7180
7176
  };
@@ -7260,6 +7256,9 @@ Merge them by hand (cp -Rn ${workit}/. ${target}/), then delete them here.
7260
7256
  }
7261
7257
 
7262
7258
  // packages/workit-core/src/store/paths.ts
7259
+ import { spawnSync as spawnSync5 } from "node:child_process";
7260
+ import fs5 from "node:fs";
7261
+ import path8 from "node:path";
7263
7262
  var NOT_A_REPO = /not a git repository \(or any (of the parent directories|parent up to)/u;
7264
7263
  var gitRun = (cwd, args) => {
7265
7264
  const run = spawnSync5("git", args, {
@@ -7423,6 +7422,7 @@ var STAMPING_HOSTS = new Set([
7423
7422
  "codex_cli",
7424
7423
  "codex_desktop"
7425
7424
  ]);
7425
+ var pinFiles = new Map;
7426
7426
  var OWN_CHILD_SHELL = new Set(["opencode", "pi"]);
7427
7427
  var SESSION_UNSAFE = /[^A-Za-z0-9_.:@/+-]/g;
7428
7428
  function childSessionId(lead, child) {
package/dist/workit.js CHANGED
@@ -10040,12 +10040,6 @@ var escapeRegExp2 = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
10040
10040
  var resolveStateDir = () => stateDir(process.env);
10041
10041
  var homePattern = new RegExp(`${escapeRegExp2(os4.homedir())}(?=[/\\\\]|$)|\\$HOME(?=[/\\\\]|$)`, "g");
10042
10042
 
10043
- // packages/workit-core/src/store/paths.ts
10044
- import { spawnSync as spawnSync9 } from "node:child_process";
10045
- import { createHash as createHash7 } from "node:crypto";
10046
- import fs6 from "node:fs";
10047
- import path10 from "node:path";
10048
-
10049
10043
  // packages/workit-core/src/store/folder-store.ts
10050
10044
  import fs5 from "node:fs";
10051
10045
  import os5 from "node:os";
@@ -10262,13 +10256,14 @@ var below = (parent, child) => {
10262
10256
  const rel = path9.relative(parent, child);
10263
10257
  return rel !== "" && !rel.startsWith("..") && !path9.isAbsolute(rel);
10264
10258
  };
10259
+ var insideTempDir = (dir, tempDir = os5.tmpdir()) => below(canonical3(tempDir), canonical3(dir));
10265
10260
  var TEST_RUN_REFUSALS = "folder-store-refusals.log";
10266
10261
  var testRunFolderRefusal = (folder, hasStray, env = process.env, tempDir = os5.tmpdir()) => {
10267
10262
  if (env.WORKIT_TEST_RUN === undefined || !hasStray)
10268
10263
  return null;
10269
10264
  const real = canonical3(folder);
10270
10265
  const tmp = canonical3(tempDir);
10271
- if (below(tmp, real))
10266
+ if (insideTempDir(folder, tempDir))
10272
10267
  return null;
10273
10268
  return `workit test run (WORKIT_TEST_RUN): refusing to move ${path9.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)`;
10274
10269
  };
@@ -10354,6 +10349,10 @@ Merge them by hand (cp -Rn ${workit}/. ${target}/), then delete them here.
10354
10349
  }
10355
10350
 
10356
10351
  // packages/workit-core/src/store/paths.ts
10352
+ import { spawnSync as spawnSync9 } from "node:child_process";
10353
+ import { createHash as createHash7 } from "node:crypto";
10354
+ import fs6 from "node:fs";
10355
+ import path10 from "node:path";
10357
10356
  var NOT_A_REPO = /not a git repository \(or any (of the parent directories|parent up to)/u;
10358
10357
  var gitRun = (cwd, args) => {
10359
10358
  const run = spawnSync9("git", args, {
@@ -10666,8 +10665,58 @@ function hostSessionFromEnv(env) {
10666
10665
  var ISSUED_PER_LEAD = 64;
10667
10666
  var ISSUED_SESSIONS_MARKER = "workit-issued-sessions/3";
10668
10667
  var hash = (value) => createHash8("sha256").update(value).digest("hex").slice(0, 32);
10669
- var userIssuedRoot = () => path11.join(resolveStateDir(), "issued");
10670
- var userTokenRoot = () => path11.join(resolveStateDir(), "issued-tokens");
10668
+ var PIN_FILE = "issued-registry";
10669
+ var pinFiles = new Map;
10670
+ var pinFile = (cwd) => {
10671
+ if (cwd === undefined)
10672
+ return null;
10673
+ const key = path11.resolve(cwd);
10674
+ const known = pinFiles.get(key);
10675
+ if (known !== undefined)
10676
+ return known;
10677
+ try {
10678
+ const location = resolveStore(key);
10679
+ if (location instanceof Error)
10680
+ return null;
10681
+ const file = location.shared ? path11.join(location.dir, "ledger", PIN_FILE) : null;
10682
+ pinFiles.set(key, file);
10683
+ return file;
10684
+ } catch {
10685
+ return null;
10686
+ }
10687
+ };
10688
+ var pinnedStateDir = (cwd) => {
10689
+ const file = pinFile(cwd);
10690
+ if (file === null)
10691
+ return null;
10692
+ try {
10693
+ const pinned = fs7.readFileSync(file, "utf8").trim();
10694
+ return path11.isAbsolute(pinned) ? pinned : null;
10695
+ } catch {
10696
+ return null;
10697
+ }
10698
+ };
10699
+ var registryDir = (cwd) => pinnedStateDir(cwd) ?? resolveStateDir();
10700
+ var testRunSkipsPin = (file) => process.env.WORKIT_TEST_RUN !== undefined && !insideTempDir(path11.dirname(path11.dirname(path11.dirname(file))));
10701
+ function pinIssuedRegistry(cwd) {
10702
+ const file = pinFile(cwd);
10703
+ if (file === null)
10704
+ return;
10705
+ const state = path11.resolve(resolveStateDir());
10706
+ if (pinnedStateDir(cwd) === state)
10707
+ return;
10708
+ if (testRunSkipsPin(file))
10709
+ return;
10710
+ try {
10711
+ fs7.mkdirSync(path11.dirname(file), { recursive: true });
10712
+ const temp = `${file}.${process.pid}.tmp`;
10713
+ fs7.writeFileSync(temp, `${state}
10714
+ `, { mode: 420 });
10715
+ fs7.renameSync(temp, file);
10716
+ } catch {}
10717
+ }
10718
+ var userIssuedRoot = (cwd) => path11.join(registryDir(cwd), "issued");
10719
+ var userTokenRoot = (cwd) => path11.join(registryDir(cwd), "issued-tokens");
10671
10720
  function migrateRepoIssued(cwd) {
10672
10721
  if (cwd === undefined)
10673
10722
  return;
@@ -10687,7 +10736,7 @@ function migrateRepoIssued(cwd) {
10687
10736
  const from = path11.join(old, lead);
10688
10737
  if (!fs7.statSync(from).isDirectory())
10689
10738
  continue;
10690
- const to = path11.join(userIssuedRoot(), lead);
10739
+ const to = path11.join(userIssuedRoot(cwd), lead);
10691
10740
  fs7.mkdirSync(to, { recursive: true, mode: 448 });
10692
10741
  for (const id of fs7.readdirSync(from)) {
10693
10742
  try {
@@ -10719,13 +10768,15 @@ var tokenHash = (token) => createHash8("sha256").update(token).digest("hex");
10719
10768
  function recordIssuedSession(cwd, lead, issued) {
10720
10769
  const token = randomBytes(18).toString("hex");
10721
10770
  try {
10771
+ pinIssuedRegistry(cwd);
10722
10772
  migrateRepoIssued(cwd);
10723
10773
  const own = hash(issued);
10724
- const tokens = path11.join(userTokenRoot(), hash(lead));
10774
+ const state = resolveStateDir();
10775
+ const tokens = path11.join(state, "issued-tokens", hash(lead));
10725
10776
  fs7.mkdirSync(tokens, { recursive: true, mode: 448 });
10726
10777
  fs7.writeFileSync(path11.join(tokens, own), `${tokenHash(token)}
10727
10778
  `, { mode: 384, flag: "wx" });
10728
- const dir = path11.join(userIssuedRoot(), hash(lead));
10779
+ const dir = path11.join(state, "issued", hash(lead));
10729
10780
  fs7.mkdirSync(dir, { recursive: true, mode: 448 });
10730
10781
  fs7.writeFileSync(path11.join(dir, own), `${ISSUED_SESSIONS_MARKER}
10731
10782
  `, { mode: 384 });
@@ -10770,17 +10821,17 @@ var issuedRecord = (cwd, claimed) => {
10770
10821
  return null;
10771
10822
  migrateRepoIssued(cwd);
10772
10823
  try {
10773
- return fs7.readFileSync(path11.join(userIssuedRoot(), hash(claimed.slice(0, cut)), hash(claimed)), "utf8");
10824
+ return fs7.readFileSync(path11.join(userIssuedRoot(cwd), hash(claimed.slice(0, cut)), hash(claimed)), "utf8");
10774
10825
  } catch {
10775
10826
  return null;
10776
10827
  }
10777
10828
  };
10778
10829
  var TOKEN_HASH = /^([0-9a-f]{64})$/mu;
10779
- var issuedToken = (claimed) => {
10830
+ var issuedToken = (cwd, claimed) => {
10780
10831
  const cut = claimed.lastIndexOf(":");
10781
10832
  if (cut <= 0)
10782
10833
  return { stored: null, leadBound: false };
10783
- const dir = path11.join(userTokenRoot(), hash(claimed.slice(0, cut)));
10834
+ const dir = path11.join(userTokenRoot(cwd), hash(claimed.slice(0, cut)));
10784
10835
  try {
10785
10836
  const text = fs7.readFileSync(path11.join(dir, hash(claimed)), "utf8");
10786
10837
  return { stored: TOKEN_HASH.exec(text)?.[1] ?? null, leadBound: true };
@@ -10790,7 +10841,7 @@ var issuedToken = (claimed) => {
10790
10841
  };
10791
10842
  function issuedBinding(cwd, claimed) {
10792
10843
  const record = issuedRecord(cwd, claimed);
10793
- const { stored, leadBound } = issuedToken(claimed);
10844
+ const { stored, leadBound } = issuedToken(cwd, claimed);
10794
10845
  if (stored)
10795
10846
  return "token";
10796
10847
  if (record === null)
@@ -34322,13 +34373,36 @@ var hookEntryRun = (cwd, command, dialect = "posix") => {
34322
34373
  var hookEntryDeny = (entry) => `workit hook guard: this command runs Workit's hook entry ${entry}. Only the host runs Workit's hooks, with the payload it builds; a payload written in a shell would act as a session, subagent or agent that never existed. Use the workit CLI for what you need (\`workit doctor\` probes the installed hooks).`;
34323
34374
 
34324
34375
  // packages/workit-core/src/hooks/identity-env.ts
34376
+ import fs36 from "node:fs";
34377
+ import path59 from "node:path";
34325
34378
  var NAMES = "WORKIT_(?:AGENT_ID|HOST|SESSION_ID|SESSION_TOKEN)";
34326
- var HOST_NAMES = "(?:OPENCODE_SESSION_ID|PI_SESSION_ID|CODEX_THREAD_ID|CLAUDECODE|CLAUDE_CODE_ENTRYPOINT)";
34327
34379
  var IDENTITY = new RegExp(`^${NAMES}$`, "u");
34380
+ var HOST_NAMES = "(?:OPENCODE_SESSION_ID|PI_SESSION_ID|CODEX_THREAD_ID|CLAUDECODE|CLAUDE_CODE_ENTRYPOINT)";
34328
34381
  var HOST_VARIABLE = new RegExp(`^${HOST_NAMES}$`, "u");
34329
- var powershell = (names) => new RegExp(`\\$env:${names}\\b|env:[\\\\/]?${names}\\b|EnvironmentVariable\\(\\s*['"]${names}\\b`, "iu");
34382
+ var IDENTITY_ANY = new RegExp(NAMES, "iu");
34383
+ var STATE_NAMES = "(?:WORKIT_(?:STATE_DIR|CONFIG(?:_DIR)?)|XDG_(?:STATE|CONFIG|DATA)_HOME|HOME|USERPROFILE|(?:LOCAL)?APPDATA)";
34384
+ var STATE2 = new RegExp(`^${STATE_NAMES}$`, "u");
34385
+ var STATE_ANY = new RegExp(STATE_NAMES, "iu");
34386
+ var powershell = (names) => new RegExp([
34387
+ `\\$\\{?env:${names}\\}?\\s*(?:[-+*/]|\\?\\?)?=(?!=)`,
34388
+ `${PS_ITEM}[^;|\\n]*?(?<![$\\w{])env:[\\\\/]?${names}\\b`,
34389
+ `SetEnvironmentVariable\\(\\s*['"]${names}\\b`
34390
+ ].join("|"), "iu");
34391
+ var PS_ITEM = "\\b(?:(?:Set|New|Remove|Clear|Rename|Move|Copy)-Item(?:Property)?|(?:Set|Add|Clear)-Content|si|ni|ri|rm|del|erase|rd|rmdir|cli|sc|ac|clc|rni|mi|move|mv|cpi|copy|cp|ren)\\b";
34330
34392
  var POWERSHELL = powershell(NAMES);
34393
+ var POWERSHELL_STATE = powershell(STATE_NAMES);
34331
34394
  var POWERSHELL_HOST = powershell(HOST_NAMES);
34395
+ var POWERSHELL_OPAQUE = new RegExp([
34396
+ `SetEnvironmentVariable\\(\\s*(?:[^'"\\s]|"[^"]*\\$)`,
34397
+ `${PS_ITEM}[^;|\\n]*?(?<![$\\w{])env:[\\\\/]?["']?[$(]`,
34398
+ `(?:^|[;&|{(\\n])\\s*\\.\\s+[^\\s.]`,
34399
+ `\\b(?:iex|Invoke-Expression)\\b`
34400
+ ].join("|"), "iu");
34401
+ var cmdSet = (names) => new RegExp(`(?:^|[&|(\\s"])set\\s+(?:/[ap]\\s+)?"?(${names})\\s*\\+?=`, "iu");
34402
+ var CMD_IDENTITY = cmdSet(NAMES);
34403
+ var CMD_STATE = cmdSet(STATE_NAMES);
34404
+ var CMD_HOST = cmdSet(HOST_NAMES);
34405
+ var RUNS_TEXT = /\bworkit(?:-cli)?\b|@workit-dev[\\/]cli/iu;
34332
34406
  var DECLARES = new Set(["export", "unset", "declare", "typeset", "local", "readonly"]);
34333
34407
  var OPAQUE = new Set([
34334
34408
  "watch",
@@ -34346,22 +34420,41 @@ var ENV_FLAGS = new Set(["i", "0", "v"]);
34346
34420
  var ENV_LONG_VALUED = new Set(["--unset", "--chdir", "--split-string", "--argv0"]);
34347
34421
  var ENV_LONG_FLAGS = /^--(?:ignore-environment|null|debug|(?:block|default|ignore)-signal(?:=.*)?)$/u;
34348
34422
  var base3 = (word) => (word?.split(/[\\/]/u).at(-1) ?? "").replace(/\.exe$/iu, "");
34349
- var nameOf = (word) => word?.split("=")[0] ?? "";
34350
- var named = (word) => IDENTITY.test(nameOf(word)) || HOST_VARIABLE.test(nameOf(word));
34351
- var isWorkit = (word) => /^workit(?:-cli)?$/u.test(base3(word));
34423
+ var ASSIGN = /^[A-Za-z_]\w*\+?=/u;
34424
+ var nameOf = (word) => (word?.split("=")[0] ?? "").replace(/\+$/u, "");
34425
+ var named = (word) => IDENTITY.test(nameOf(word));
34426
+ var stateNamed = (word) => STATE2.test(nameOf(word));
34427
+ var isWorkit = (word) => /^workit(?:-cli)?(?:\.[cm]?js)?$/u.test(base3(word)) || /@workit-dev[\\/]cli\b|workit-cli[\\/](?:dist|src)[\\/]/u.test(word ?? "");
34428
+ var linksToWorkit = (word, cwd) => {
34429
+ const places = /[\\/]/u.test(word) ? [path59.resolve(cwd ?? process.cwd(), word)] : (process.env.PATH ?? "").split(path59.delimiter).map((dir) => path59.join(dir, word));
34430
+ for (const place of places) {
34431
+ try {
34432
+ const real = fs36.realpathSync(place);
34433
+ return isWorkit(real);
34434
+ } catch {}
34435
+ }
34436
+ return false;
34437
+ };
34438
+ var newSeg = () => ({ identity: null, scoped: null, runs: false, words: [] });
34352
34439
  var UNREADABLE = "env/unset arguments Workit cannot read";
34353
- var mark = (scan, word) => {
34354
- const name = nameOf(word);
34355
- if (IDENTITY.test(name))
34356
- scan.hit ??= name;
34357
- else if (HOST_VARIABLE.test(name))
34358
- scan.hostHit ??= name;
34359
- };
34360
- var scanEnv = (words, at, expands, scan, depth) => {
34440
+ var note = (scan, seg, word) => {
34441
+ if (scan.identity && named(word)) {
34442
+ if (seg)
34443
+ seg.identity ??= nameOf(word);
34444
+ else
34445
+ scan.hit ??= nameOf(word);
34446
+ }
34447
+ if (!stateNamed(word) && !(scan.identity && HOST_VARIABLE.test(nameOf(word))))
34448
+ return;
34449
+ if (seg)
34450
+ seg.scoped ??= nameOf(word);
34451
+ else
34452
+ scan.shell ??= nameOf(word);
34453
+ };
34454
+ var mayBeWorkit = (words, cwd) => words.some(({ word, built }) => built || linksToWorkit(word, cwd));
34455
+ var scanEnv = (words, at, expands, scan, seg, depth) => {
34361
34456
  const risky = (what) => void (scan.risky ??= what);
34362
- const unset = (name) => {
34363
- mark(scan, name);
34364
- };
34457
+ const unset = (name) => note(scan, seg, name);
34365
34458
  const split = (line, rest, built) => {
34366
34459
  if (line === undefined || depth >= 4)
34367
34460
  return risky(UNREADABLE);
@@ -34378,7 +34471,7 @@ var scanEnv = (words, at, expands, scan, depth) => {
34378
34471
  } catch {
34379
34472
  return risky(UNREADABLE);
34380
34473
  }
34381
- scanEnv([...tokens, ...words.slice(rest)], 0, expanded, scan, depth + 1);
34474
+ scanEnv([...tokens, ...words.slice(rest)], 0, expanded, scan, seg, depth + 1);
34382
34475
  };
34383
34476
  let options = true;
34384
34477
  for (let index = at;index < words.length; index += 1) {
@@ -34436,25 +34529,25 @@ var scanEnv = (words, at, expands, scan, depth) => {
34436
34529
  }
34437
34530
  continue;
34438
34531
  }
34439
- if (/^[A-Za-z_]\w*=/u.test(word)) {
34532
+ if (ASSIGN.test(word)) {
34440
34533
  unset(word);
34441
34534
  continue;
34442
34535
  }
34443
- return commandAt(words.slice(index), scan);
34536
+ return commandAt(words.slice(index), scan, seg, new Set([...expands].filter((at2) => at2 >= index).map((at2) => at2 - index)));
34444
34537
  }
34445
34538
  };
34446
- var commandAt = (words, scan, expands = new Set, from = 0) => {
34539
+ var commandAt = (words, scan, seg, expands = new Set, from = 0) => {
34447
34540
  let index = from;
34448
34541
  while (index < words.length) {
34449
- while (/^[A-Za-z_]\w*=/u.test(words[index] ?? "")) {
34450
- mark(scan, words[index]);
34542
+ while (ASSIGN.test(words[index] ?? "")) {
34543
+ note(scan, seg, words[index]);
34451
34544
  index += 1;
34452
34545
  }
34453
34546
  const name = base3(words[index]).toLowerCase();
34454
34547
  if (name === "env")
34455
- return scanEnv(words, index + 1, expands, scan, 0);
34548
+ return scanEnv(words, index + 1, expands, scan, seg, 0);
34456
34549
  if (OPAQUE.has(name))
34457
- return opaque(words.slice(index + 1), scan, true);
34550
+ return opaque(words.slice(index + 1), scan, seg, true);
34458
34551
  if (name === "busybox" || name === "toybox") {
34459
34552
  index += 1;
34460
34553
  continue;
@@ -34464,21 +34557,37 @@ var commandAt = (words, scan, expands = new Set, from = 0) => {
34464
34557
  break;
34465
34558
  index = pastOptions(words, index + 1, takes);
34466
34559
  }
34467
- if (isWorkit(words[index]))
34468
- scan.runs = true;
34469
- else if (words.slice(index + 1).some((word) => base3(word).toLowerCase() === "env"))
34470
- opaque(words.slice(index + 1), scan, false);
34471
- };
34472
- var opaque = (rest, scan, assigns) => {
34560
+ const word = words[index];
34561
+ if (word === undefined)
34562
+ return;
34563
+ seg.words.push({ word, built: expands.has(index) });
34564
+ const rest = words.slice(index + 1);
34565
+ if (isWorkit(word) || rest.some(isWorkit))
34566
+ seg.runs = true;
34567
+ if (!isWorkit(word) && rest.some((next) => base3(next).toLowerCase() === "env"))
34568
+ opaque(rest, scan, seg, false);
34569
+ const program = base3(word).toLowerCase();
34570
+ const shell = program === "pwsh" || program === "powershell";
34571
+ if (program === "cmd" || shell) {
34572
+ const at = rest.findIndex((next) => shell ? /^-(?:c|command)$/iu.test(next) : /^\/[ck]$/iu.test(next));
34573
+ if (at >= 0) {
34574
+ const script = rest.slice(at + 1).join(" ");
34575
+ const change = shell ? powershellChange(script, scan.identity) : cmdChange(script, scan);
34576
+ if (change)
34577
+ scan.hit ??= change;
34578
+ }
34579
+ }
34580
+ };
34581
+ var opaque = (rest, scan, seg, assigns) => {
34473
34582
  if (!rest.some(isWorkit))
34474
34583
  return;
34475
- scan.runs = true;
34476
- const changes = (word) => base3(word).toLowerCase() === "env" || DECLARES.has(word) || assigns && /^[A-Za-z_]\w*=/u.test(word);
34584
+ seg.runs = true;
34585
+ const changes = (word) => base3(word).toLowerCase() === "env" || DECLARES.has(word) || assigns && ASSIGN.test(word);
34477
34586
  if (rest.some(changes))
34478
34587
  scan.risky ??= UNREADABLE;
34479
- const hit = rest.find(named);
34480
- if (hit && rest.some((word) => base3(word).toLowerCase() === "env"))
34481
- mark(scan, hit);
34588
+ if (rest.some((word) => base3(word).toLowerCase() === "env"))
34589
+ for (const word of rest)
34590
+ note(scan, seg, word);
34482
34591
  };
34483
34592
  var LOGIN_VALUED = {
34484
34593
  su: /[sgGwc]/u,
@@ -34513,38 +34622,83 @@ var scanCommand2 = (command, dialect, depth, scan) => {
34513
34622
  }
34514
34623
  for (const { words, expands = [] } of segments) {
34515
34624
  const expanded = new Set(expands);
34625
+ const seg = newSeg();
34516
34626
  let at = 0;
34517
- while (/^[A-Za-z_]\w*=/u.test(words[at] ?? "")) {
34518
- mark(scan, words[at]);
34627
+ while (ASSIGN.test(words[at] ?? ""))
34519
34628
  at += 1;
34520
- }
34629
+ for (const word of words.slice(0, at))
34630
+ note(scan, at === words.length ? null : seg, word);
34521
34631
  let declare = at;
34522
34632
  if (["builtin", "command"].includes(words[declare] ?? ""))
34523
34633
  declare += 1;
34524
34634
  if (DECLARES.has(words[declare] ?? "")) {
34525
34635
  for (let index = declare + 1;index < words.length; index += 1) {
34526
- mark(scan, words[index]);
34636
+ note(scan, null, words[index]);
34527
34637
  if (expanded.has(index))
34528
34638
  scan.risky ??= UNREADABLE;
34529
34639
  }
34530
34640
  continue;
34531
34641
  }
34532
- commandAt(words, scan, expanded, at);
34642
+ commandAt(words, scan, seg, expanded, at);
34533
34643
  const script = depth < 4 ? shellScript2(words) ?? loginScript(words) : null;
34534
- if (script)
34644
+ if (script) {
34645
+ const before = scan.runs;
34646
+ scan.runs = false;
34535
34647
  scanCommand2(script, dialect, depth + 1, scan);
34536
- }
34537
- };
34538
- var identityEnvChange = (command, dialect = "posix") => {
34539
- if (!/WORKIT_|OPENCODE_SESSION_ID|PI_SESSION_ID|CODEX_THREAD_ID|CLAUDE|\b(?:env|unset|export|declare|typeset|local|readonly)\b/u.test(command))
34648
+ seg.runs ||= scan.runs;
34649
+ scan.runs ||= before;
34650
+ }
34651
+ scan.words.push(...seg.words);
34652
+ const reaches = () => seg.runs || mayBeWorkit(seg.words, scan.cwd);
34653
+ if (seg.identity && reaches())
34654
+ scan.hit ??= seg.identity;
34655
+ if (seg.scoped && reaches())
34656
+ scan.state ??= seg.scoped;
34657
+ scan.runs ||= seg.runs;
34658
+ }
34659
+ };
34660
+ var cmdChange = (script, scan) => (scan.identity ? CMD_IDENTITY.exec(script)?.[1] : null) ?? (RUNS_TEXT.test(script) ? CMD_STATE.exec(script)?.[1] ?? (scan.identity ? CMD_HOST.exec(script)?.[1] : null) ?? null : null);
34661
+ var powershellChange = (command, identity) => {
34662
+ const hit = identity ? POWERSHELL.exec(command)?.[0] : null;
34663
+ if (hit)
34664
+ return hit;
34665
+ if (!RUNS_TEXT.test(command) && !/&\s*[$(]/u.test(command))
34540
34666
  return null;
34667
+ const state = POWERSHELL_STATE.exec(command)?.[0] ?? (identity ? POWERSHELL_HOST.exec(command)?.[0] : null) ?? cmdChange(command, { identity });
34668
+ if (state)
34669
+ return state;
34670
+ return POWERSHELL_OPAQUE.test(command) ? UNREADABLE : null;
34671
+ };
34672
+ var identityEnvChange = (command, dialect = "posix", options = {}) => {
34673
+ const identity = options.identity ?? true;
34541
34674
  if (dialect === "powershell")
34542
- return POWERSHELL.exec(command)?.[0] ?? (/\bworkit\b/iu.test(command) ? POWERSHELL_HOST.exec(command)?.[0] ?? null : null);
34543
- const scan = { hit: null, hostHit: null, risky: null, runs: false };
34675
+ return powershellChange(command, identity);
34676
+ if (!/WORKIT_|XDG_|HOME|USERPROFILE|APPDATA|OPENCODE_SESSION_ID|PI_SESSION_ID|CODEX_THREAD_ID|CLAUDE|EnvironmentVariable|\b(?:env|unset|export|declare|typeset|local|readonly)\b/iu.test(command))
34677
+ return null;
34678
+ const scan = {
34679
+ identity,
34680
+ cwd: options.cwd,
34681
+ hit: null,
34682
+ state: null,
34683
+ shell: null,
34684
+ risky: null,
34685
+ runs: false,
34686
+ words: []
34687
+ };
34544
34688
  scanCommand2(command, dialect, 0, scan);
34545
- return scan.hit ?? (scan.runs ? scan.hostHit ?? scan.risky : null);
34689
+ if (scan.hit)
34690
+ return scan.hit;
34691
+ if (scan.state)
34692
+ return scan.state;
34693
+ if (scan.shell && (scan.runs || mayBeWorkit(scan.words, scan.cwd)))
34694
+ return scan.shell;
34695
+ return scan.runs ? scan.risky : null;
34696
+ };
34697
+ var identityEnvDeny = (change) => {
34698
+ if (change === UNREADABLE || IDENTITY_ANY.test(change) || !STATE_ANY.test(change))
34699
+ return `workit identity: ${change === UNREADABLE ? `this command runs workit with ${UNREADABLE}` : `this command changes ${change}`}. The host sets WORKIT_HOST, WORKIT_SESSION_ID, WORKIT_AGENT_ID and its own session variables, and Workit acts as who they name; run it without changing them (a verifier or reviewer passes the --session and --token its start text names).`;
34700
+ return `workit identity: this command runs workit with ${change} changed. Workit reads issued sessions, grants and its ledger trust from the state and config dirs the host's environment names, so a workit call may not move them (#980); run it without setting, unsetting or emptying ${change}.`;
34546
34701
  };
34547
- var identityEnvDeny = (change) => `workit identity: ${change === UNREADABLE ? `this command runs workit with ${UNREADABLE}` : `this command changes ${change}`}. The host sets WORKIT_HOST, WORKIT_SESSION_ID, WORKIT_AGENT_ID and its own session variables, and Workit acts as who they name; run it without changing them (a verifier or reviewer passes the --session and --token its start text names).`;
34548
34702
 
34549
34703
  // packages/workit-core/src/hooks/handle.ts
34550
34704
  var usable2 = (value) => value === "native" || value === "partial";
@@ -34621,6 +34775,7 @@ function decide(input, deps) {
34621
34775
  switch (event.kind) {
34622
34776
  case "session.start": {
34623
34777
  reapAtStart(input.cwd);
34778
+ pinIssuedRegistry(input.cwd);
34624
34779
  if (!usable2(descriptor.context.sessionStart))
34625
34780
  return NONE6;
34626
34781
  const key = input.session.id ? `${input.host}\x00${input.session.id}` : null;
@@ -34744,7 +34899,10 @@ function decide(input, deps) {
34744
34899
  const gate = usable2(descriptor.events["write.pre"].support) ? shellGate(input.cwd, event.command, owner, event.dialect) : NONE6;
34745
34900
  if (gate.kind === "deny")
34746
34901
  return gate;
34747
- const identity = canDeny && stampsAgents(descriptor) ? identityEnvChange(event.command, event.dialect) : null;
34902
+ const identity = canDeny ? identityEnvChange(event.command, event.dialect, {
34903
+ identity: stampsAgents(descriptor),
34904
+ cwd: input.cwd
34905
+ }) : null;
34748
34906
  if (identity)
34749
34907
  return { kind: "deny", reason: identityEnvDeny(identity), unblock: null };
34750
34908
  const plan = heavyGate(input.cwd, event.command, {
@@ -34881,11 +35039,11 @@ var modelKey = (model) => {
34881
35039
 
34882
35040
  // packages/workit-core/src/hooks/budget-signal.ts
34883
35041
  import { createHash as createHash25 } from "node:crypto";
34884
- import fs36 from "node:fs";
34885
- import path59 from "node:path";
35042
+ import fs37 from "node:fs";
35043
+ import path60 from "node:path";
34886
35044
  var hash3 = (text) => createHash25("sha256").update(text).digest("hex").slice(0, 32);
34887
- var budgetDir = (store) => path59.join(store, "budget");
34888
- var agentBase = (store, session, agentId) => path59.join(budgetDir(store), hash3(`${session}\x00${agentId ?? ""}`));
35045
+ var budgetDir = (store) => path60.join(store, "budget");
35046
+ var agentBase = (store, session, agentId) => path60.join(budgetDir(store), hash3(`${session}\x00${agentId ?? ""}`));
34889
35047
  var spawnBudget = (prompt) => {
34890
35048
  const found = /^TIMEBOX:[^\n]*?\b(\d+)\s+tool calls\b/mu.exec(prompt);
34891
35049
  const n = found ? Number(found[1]) : NaN;
@@ -34893,7 +35051,7 @@ var spawnBudget = (prompt) => {
34893
35051
  };
34894
35052
  var readBudget = (file) => {
34895
35053
  try {
34896
- const { limit, told } = JSON.parse(fs36.readFileSync(file, "utf8"));
35054
+ const { limit, told } = JSON.parse(fs37.readFileSync(file, "utf8"));
34897
35055
  return typeof limit === "number" && limit > 0 ? { limit, told: Number(told) || 0 } : null;
34898
35056
  } catch {
34899
35057
  return null;
@@ -34901,16 +35059,16 @@ var readBudget = (file) => {
34901
35059
  };
34902
35060
  var startBudget = (store, session, agentId, n) => {
34903
35061
  const dir = budgetDir(store);
34904
- fs36.mkdirSync(dir, { recursive: true });
35062
+ fs37.mkdirSync(dir, { recursive: true });
34905
35063
  const cutoff = Date.now() - 604800000;
34906
- for (const name of fs36.readdirSync(dir))
35064
+ for (const name of fs37.readdirSync(dir))
34907
35065
  try {
34908
- if (fs36.statSync(path59.join(dir, name)).mtimeMs < cutoff)
34909
- fs36.rmSync(path59.join(dir, name));
35066
+ if (fs37.statSync(path60.join(dir, name)).mtimeMs < cutoff)
35067
+ fs37.rmSync(path60.join(dir, name));
34910
35068
  } catch {}
34911
35069
  const base = agentBase(store, session, agentId);
34912
- fs36.writeFileSync(`${base}.json`, JSON.stringify({ limit: n, told: 0 }));
34913
- fs36.rmSync(`${base}.calls`, { force: true });
35070
+ fs37.writeFileSync(`${base}.json`, JSON.stringify({ limit: n, told: 0 }));
35071
+ fs37.rmSync(`${base}.calls`, { force: true });
34914
35072
  };
34915
35073
  var bindBudget = (cwd, session, agentId, prompt) => {
34916
35074
  try {
@@ -34933,8 +35091,8 @@ var tickBudget = (cwd, session, agentId) => {
34933
35091
  const budget = readBudget(`${base}.json`);
34934
35092
  if (!budget)
34935
35093
  return false;
34936
- fs36.appendFileSync(`${base}.calls`, ".");
34937
- return fs36.statSync(`${base}.calls`).size >= budget.limit * (budget.told + 1);
35094
+ fs37.appendFileSync(`${base}.calls`, ".");
35095
+ return fs37.statSync(`${base}.calls`).size >= budget.limit * (budget.told + 1);
34938
35096
  } catch {
34939
35097
  return false;
34940
35098
  }
@@ -34946,14 +35104,14 @@ var reachedOf = (cwd, session, agentId) => {
34946
35104
  const budget = base && readBudget(`${base}.json`);
34947
35105
  if (!budget)
34948
35106
  return null;
34949
- return { base, budget, reached: Math.floor(fs36.statSync(`${base}.calls`).size / budget.limit) };
35107
+ return { base, budget, reached: Math.floor(fs37.statSync(`${base}.calls`).size / budget.limit) };
34950
35108
  };
34951
35109
  var budgetLine = (cwd, session, agentId) => {
34952
35110
  try {
34953
35111
  const at = reachedOf(cwd, session, agentId);
34954
35112
  if (!at || at.reached <= at.budget.told)
34955
35113
  return null;
34956
- fs36.writeFileSync(`${at.base}.json`, JSON.stringify({ ...at.budget, told: at.reached }));
35114
+ fs37.writeFileSync(`${at.base}.json`, JSON.stringify({ ...at.budget, told: at.reached }));
34957
35115
  return lineFor(at.budget.limit * at.reached);
34958
35116
  } catch {
34959
35117
  return null;
@@ -35361,12 +35519,12 @@ var skillPromptNudge = (prompt, ctx) => process.env.WORKIT_PI_WORKER_SESSION ? n
35361
35519
  // packages/workit-pi/src/launches.ts
35362
35520
  import { mkdirSync as mkdirSync8, readFileSync as readFileSync17, readdirSync as readdirSync9, rmSync as rmSync4, writeFileSync as writeFileSync7 } from "node:fs";
35363
35521
  import os13 from "node:os";
35364
- import path60 from "node:path";
35522
+ import path61 from "node:path";
35365
35523
  var launchDir = (root) => {
35366
35524
  const location = resolveStore(root);
35367
- return location instanceof Error ? null : path60.join(location.dir, "pi-launches");
35525
+ return location instanceof Error ? null : path61.join(location.dir, "pi-launches");
35368
35526
  };
35369
- var fileFor = (dir, workerId) => path60.join(dir, `${workerId.replace(/[^A-Za-z0-9-]/g, "_")}.json`);
35527
+ var fileFor = (dir, workerId) => path61.join(dir, `${workerId.replace(/[^A-Za-z0-9-]/g, "_")}.json`);
35370
35528
  var recordLaunch = (root, attempt) => {
35371
35529
  const dir = launchDir(root);
35372
35530
  if (!dir)
@@ -35412,7 +35570,7 @@ var abandonedLaunches = (root) => {
35412
35570
  const found = [];
35413
35571
  for (const name of names) {
35414
35572
  try {
35415
- const attempt = JSON.parse(readFileSync17(path60.join(dir, name), "utf8"));
35573
+ const attempt = JSON.parse(readFileSync17(path61.join(dir, name), "utf8"));
35416
35574
  if (typeof attempt.workerId === "string" && typeof attempt.session === "string") {
35417
35575
  if (!alive(attempt))
35418
35576
  found.push(attempt);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workit-dev/pi",
3
- "version": "2.27.8",
3
+ "version": "2.27.9",
4
4
  "description": "Workit native extension for Pi — tools, method skills, and session continuity",
5
5
  "keywords": [
6
6
  "agentic-coding",