@workit-dev/pi 2.14.0 → 2.16.0

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
@@ -9219,11 +9219,12 @@ var WRITE_TOOLS = new Set([
9219
9219
  var CLAUDE_CODE_DESCRIPTOR = {
9220
9220
  host: "claude_code",
9221
9221
  label: "Claude Code",
9222
- verifiedAgainst: "claude 2.1.288 (hook zod schemas in the binary)",
9222
+ verifiedAgainst: "claude 2.1.288 (hook zod schemas in the binary; SessionEnd: 2.1.327)",
9223
9223
  docs: ["https://code.claude.com/docs/en/hooks"],
9224
9224
  transport: "hook-process",
9225
9225
  events: {
9226
9226
  "session.start": { support: "native", native: "SessionStart" },
9227
+ "session.end": { support: "native", native: "SessionEnd" },
9227
9228
  "context.turn": { support: "native", native: "UserPromptSubmit" },
9228
9229
  "shell.pre": { support: "native", native: "PreToolUse" },
9229
9230
  "tool.pre": { support: "native", native: "PreToolUse" },
@@ -9333,6 +9334,7 @@ var CODEX_DESCRIPTOR = {
9333
9334
  transport: "hook-process",
9334
9335
  events: {
9335
9336
  "session.start": { support: "native", native: "SessionStart" },
9337
+ "session.end": { support: "none", native: null },
9336
9338
  "context.turn": { support: "native", native: "UserPromptSubmit" },
9337
9339
  "shell.pre": { support: "native", native: "PreToolUse" },
9338
9340
  "tool.pre": { support: "native", native: "PreToolUse" },
@@ -9443,6 +9445,7 @@ var CURSOR_DESCRIPTOR = {
9443
9445
  transport: "hook-process",
9444
9446
  events: {
9445
9447
  "session.start": { support: "native", native: "sessionStart" },
9448
+ "session.end": { support: "native", native: "sessionEnd" },
9446
9449
  "context.turn": none,
9447
9450
  "shell.pre": { support: "native", native: "beforeShellExecution" },
9448
9451
  "tool.pre": { support: "native", native: "preToolUse" },
@@ -9549,6 +9552,7 @@ var OPENCODE_DESCRIPTOR = {
9549
9552
  transport: "in-process-plugin",
9550
9553
  events: {
9551
9554
  "session.start": { support: "none", native: null },
9555
+ "session.end": { support: "partial", native: 'event("session.deleted")' },
9552
9556
  "context.turn": { support: "native", native: 'session.hook("context")' },
9553
9557
  "shell.pre": { support: "native", native: 'permission.hook("evaluate")' },
9554
9558
  "tool.pre": { support: "native", native: 'tool.hook("execute.before")' },
@@ -9618,6 +9622,7 @@ var PI_DESCRIPTOR = {
9618
9622
  transport: "in-process-plugin",
9619
9623
  events: {
9620
9624
  "session.start": { support: "native", native: "session_start" },
9625
+ "session.end": { support: "native", native: "session_shutdown" },
9621
9626
  "context.turn": { support: "native", native: "before_agent_start" },
9622
9627
  "shell.pre": { support: "native", native: "tool_call" },
9623
9628
  "tool.pre": { support: "native", native: "tool_call" },
package/dist/workit.js CHANGED
@@ -24189,6 +24189,10 @@ function findExecutable(bin, env) {
24189
24189
  var TIMEOUT_MS = 60000;
24190
24190
  var YTRACK_EXIT = { ok: 0, failed: 1, cancelled: 2, auth: 4 };
24191
24191
  var findYtrack = (env) => env.WORKIT_NO_YTRACK === "1" ? null : findExecutable("ytrack", env);
24192
+ var YTRACK_QUIET_ENV = {
24193
+ YTRACK_NO_UPDATE_NOTIFIER: "1",
24194
+ YTRACK_NO_AUTO_UPGRADE: "1"
24195
+ };
24192
24196
  function ytrackRunner(env, host, timeoutMs = TIMEOUT_MS) {
24193
24197
  return (args, input) => {
24194
24198
  const exe = findYtrack(env);
@@ -24196,7 +24200,12 @@ function ytrackRunner(env, host, timeoutMs = TIMEOUT_MS) {
24196
24200
  return { status: null, stdout: "", stderr: "", missing: true };
24197
24201
  const result = spawnSync21(exe, args, {
24198
24202
  encoding: "utf8",
24199
- env: { ...env, NO_COLOR: "1", ...host ? { YTRACK_HOST: host } : {} },
24203
+ env: {
24204
+ ...env,
24205
+ NO_COLOR: "1",
24206
+ ...YTRACK_QUIET_ENV,
24207
+ ...host ? { YTRACK_HOST: host } : {}
24208
+ },
24200
24209
  timeout: timeoutMs,
24201
24210
  killSignal: "SIGKILL",
24202
24211
  windowsHide: true,
@@ -28592,11 +28601,12 @@ var inlineCallDeny = (fix) => `Run \`${fix}\` as its own call so the host can as
28592
28601
  var CLAUDE_CODE_DESCRIPTOR = {
28593
28602
  host: "claude_code",
28594
28603
  label: "Claude Code",
28595
- verifiedAgainst: "claude 2.1.288 (hook zod schemas in the binary)",
28604
+ verifiedAgainst: "claude 2.1.288 (hook zod schemas in the binary; SessionEnd: 2.1.327)",
28596
28605
  docs: ["https://code.claude.com/docs/en/hooks"],
28597
28606
  transport: "hook-process",
28598
28607
  events: {
28599
28608
  "session.start": { support: "native", native: "SessionStart" },
28609
+ "session.end": { support: "native", native: "SessionEnd" },
28600
28610
  "context.turn": { support: "native", native: "UserPromptSubmit" },
28601
28611
  "shell.pre": { support: "native", native: "PreToolUse" },
28602
28612
  "tool.pre": { support: "native", native: "PreToolUse" },
@@ -28706,6 +28716,7 @@ var CODEX_DESCRIPTOR = {
28706
28716
  transport: "hook-process",
28707
28717
  events: {
28708
28718
  "session.start": { support: "native", native: "SessionStart" },
28719
+ "session.end": { support: "none", native: null },
28709
28720
  "context.turn": { support: "native", native: "UserPromptSubmit" },
28710
28721
  "shell.pre": { support: "native", native: "PreToolUse" },
28711
28722
  "tool.pre": { support: "native", native: "PreToolUse" },
@@ -28816,6 +28827,7 @@ var CURSOR_DESCRIPTOR = {
28816
28827
  transport: "hook-process",
28817
28828
  events: {
28818
28829
  "session.start": { support: "native", native: "sessionStart" },
28830
+ "session.end": { support: "native", native: "sessionEnd" },
28819
28831
  "context.turn": none,
28820
28832
  "shell.pre": { support: "native", native: "beforeShellExecution" },
28821
28833
  "tool.pre": { support: "native", native: "preToolUse" },
@@ -28922,6 +28934,7 @@ var OPENCODE_DESCRIPTOR = {
28922
28934
  transport: "in-process-plugin",
28923
28935
  events: {
28924
28936
  "session.start": { support: "none", native: null },
28937
+ "session.end": { support: "partial", native: 'event("session.deleted")' },
28925
28938
  "context.turn": { support: "native", native: 'session.hook("context")' },
28926
28939
  "shell.pre": { support: "native", native: 'permission.hook("evaluate")' },
28927
28940
  "tool.pre": { support: "native", native: 'tool.hook("execute.before")' },
@@ -28991,6 +29004,7 @@ var PI_DESCRIPTOR = {
28991
29004
  transport: "in-process-plugin",
28992
29005
  events: {
28993
29006
  "session.start": { support: "native", native: "session_start" },
29007
+ "session.end": { support: "native", native: "session_shutdown" },
28994
29008
  "context.turn": { support: "native", native: "before_agent_start" },
28995
29009
  "shell.pre": { support: "native", native: "tool_call" },
28996
29010
  "tool.pre": { support: "native", native: "tool_call" },
@@ -30552,6 +30566,239 @@ function takeProcNotice(session, env = process.env) {
30552
30566
  }
30553
30567
  }
30554
30568
 
30569
+ // packages/workit-core/src/hooks/teardown.ts
30570
+ import { spawn } from "node:child_process";
30571
+ import fs32 from "node:fs";
30572
+ import path52 from "node:path";
30573
+
30574
+ // packages/workit-core/src/core/cli-locate.ts
30575
+ import { closeSync as closeSync5, existsSync as existsSync17, openSync as openSync5, readSync as readSync2 } from "node:fs";
30576
+ import path51 from "node:path";
30577
+ var LAUNCHER_MARKER = "workit-owned-launcher";
30578
+ var CLI_PACKAGE_PATH = ["node_modules", "@workit-dev", "cli"];
30579
+ var installRoot = (env, platform = process.platform) => {
30580
+ if (env.WORKIT_INSTALL_ROOT)
30581
+ return env.WORKIT_INSTALL_ROOT;
30582
+ const data = dataDir(env, platform);
30583
+ return platform === "win32" ? path51.join(data, "install") : data;
30584
+ };
30585
+ var ownedLauncherPath = (env, platform = process.platform) => path51.join(installRoot(env, platform), "bin", platform === "win32" ? "workit.cmd" : "workit");
30586
+ var ownedCurrentEntry = (env, platform = process.platform) => path51.join(installRoot(env, platform), "current", ...CLI_PACKAGE_PATH, "dist", "index.js");
30587
+ var head = (file) => {
30588
+ let fd;
30589
+ try {
30590
+ fd = openSync5(file, "r");
30591
+ const buffer = Buffer.alloc(256);
30592
+ const read = readSync2(fd, buffer, 0, buffer.length, 0);
30593
+ return buffer.subarray(0, read).toString("utf8");
30594
+ } catch {
30595
+ return "";
30596
+ } finally {
30597
+ if (fd !== undefined)
30598
+ closeSync5(fd);
30599
+ }
30600
+ };
30601
+ var locateWorkitCli = (options = {}) => {
30602
+ const env = options.env ?? process.env;
30603
+ const platform = options.platform ?? process.platform;
30604
+ const exists = options.exists ?? existsSync17;
30605
+ const read = options.read ?? head;
30606
+ const node = options.node ?? process.execPath;
30607
+ const launcher = ownedLauncherPath(env, platform);
30608
+ if (exists(launcher) && read(launcher).includes(LAUNCHER_MARKER))
30609
+ return {
30610
+ source: "owned-launcher",
30611
+ command: launcher,
30612
+ args: [],
30613
+ binDir: path51.dirname(launcher)
30614
+ };
30615
+ const current = ownedCurrentEntry(env, platform);
30616
+ if (exists(current))
30617
+ return {
30618
+ source: "owned-current",
30619
+ command: node,
30620
+ args: [current],
30621
+ binDir: null
30622
+ };
30623
+ if (options.bundled && exists(options.bundled))
30624
+ return {
30625
+ source: "bundled",
30626
+ command: node,
30627
+ args: [options.bundled],
30628
+ binDir: null
30629
+ };
30630
+ return { source: "path", command: "workit", args: [], binDir: null };
30631
+ };
30632
+ var pathKey = (env) => Object.keys(env).find((key) => key.toUpperCase() === "PATH") ?? "PATH";
30633
+ var workitCliEnv = (located, env = process.env, platform = process.platform) => {
30634
+ if (located.source === "path")
30635
+ return {};
30636
+ const patch = {
30637
+ WORKIT_CLI: [located.command, ...located.args].join(" ")
30638
+ };
30639
+ if (located.binDir) {
30640
+ const key = pathKey(env);
30641
+ const delimiter = platform === "win32" ? ";" : ":";
30642
+ const same = (dir) => platform === "win32" ? path51.win32.normalize(dir).toLowerCase() === path51.win32.normalize(located.binDir).toLowerCase() : path51.posix.normalize(dir) === path51.posix.normalize(located.binDir);
30643
+ const rest = (env[key] ?? "").split(delimiter).filter((dir) => dir && !same(dir));
30644
+ patch[key] = [located.binDir, ...rest].join(delimiter);
30645
+ }
30646
+ return patch;
30647
+ };
30648
+ var useOwnedWorkitCli = (options = {}) => {
30649
+ const env = options.env ?? process.env;
30650
+ const located = locateWorkitCli({ ...options, env });
30651
+ Object.assign(env, workitCliEnv(located, env, options.platform));
30652
+ return located;
30653
+ };
30654
+
30655
+ // packages/workit-core/src/hooks/teardown.ts
30656
+ var REAP_INTERVAL_MS = 10 * 60000;
30657
+ var LOG_MAX_BYTES = 1 << 20;
30658
+ var ENDS = {
30659
+ claude_code: new Set(["logout", "prompt_input_exit", "other"]),
30660
+ cursor: new Set(["completed", "aborted", "error", "window_close", "user_close"]),
30661
+ pi: new Set(["quit"]),
30662
+ opencode: new Set(["deleted"])
30663
+ };
30664
+ var SAFE_SESSION = /^[\w.@+][\w.:@/+-]{0,127}$/u;
30665
+ var HOST_SESSION_VARS2 = ["OPENCODE_SESSION_ID", "PI_SESSION_ID", "CODEX_THREAD_ID"];
30666
+ var teardownDir = (env) => path52.join(stateDir(env), "procs");
30667
+ var teardownLog = (env) => path52.join(teardownDir(env), "teardown.log");
30668
+ var reapStamp = (env) => path52.join(teardownDir(env), "reap-at-start.stamp");
30669
+ function spawnDetached(request) {
30670
+ try {
30671
+ const shell = process.platform === "win32" && !/\.exe$/iu.test(request.command);
30672
+ const line = [`"${request.command}"`, ...request.args].join(" ");
30673
+ const child = spawn(shell ? line : request.command, shell ? [] : request.args, {
30674
+ cwd: request.cwd,
30675
+ env: request.env,
30676
+ detached: true,
30677
+ shell,
30678
+ stdio: ["ignore", request.log ?? "ignore", request.log ?? "ignore"],
30679
+ windowsHide: true
30680
+ });
30681
+ child.on("error", () => {});
30682
+ child.unref();
30683
+ return child.pid !== undefined;
30684
+ } catch {
30685
+ return false;
30686
+ }
30687
+ }
30688
+ var teardownDeps = {
30689
+ spawn: (request) => request.env.WORKIT_TEST_RUN === undefined && spawnDetached(request),
30690
+ now: () => Date.now()
30691
+ };
30692
+ var openLog = (env, args) => {
30693
+ try {
30694
+ const file = teardownLog(env);
30695
+ fs32.mkdirSync(path52.dirname(file), { recursive: true });
30696
+ let size = 0;
30697
+ try {
30698
+ size = fs32.statSync(file).size;
30699
+ } catch {}
30700
+ const fd = fs32.openSync(file, size > LOG_MAX_BYTES ? "w" : "a", 384);
30701
+ fs32.writeSync(fd, `${new Date(teardownDeps.now()).toISOString()} workit ${args.join(" ")}
30702
+ `);
30703
+ return fd;
30704
+ } catch {
30705
+ return null;
30706
+ }
30707
+ };
30708
+ function runCli(args, env, cwd) {
30709
+ let log = null;
30710
+ try {
30711
+ const cli = locateWorkitCli({ env });
30712
+ log = openLog(env, args);
30713
+ const started = teardownDeps.spawn({
30714
+ command: cli.command,
30715
+ args: [...cli.args, ...args],
30716
+ env,
30717
+ cwd: fs32.existsSync(cwd) ? cwd : teardownDir(env),
30718
+ log
30719
+ });
30720
+ if (!started && log !== null)
30721
+ fs32.writeSync(log, ` not started
30722
+ `);
30723
+ return started;
30724
+ } catch {
30725
+ return false;
30726
+ } finally {
30727
+ if (log !== null)
30728
+ try {
30729
+ fs32.closeSync(log);
30730
+ } catch {}
30731
+ }
30732
+ }
30733
+ function endSession(end, env = process.env) {
30734
+ const session = end.session?.trim() ?? "";
30735
+ if (!SAFE_SESSION.test(session) || !ENDS[end.host]?.has(end.reason ?? ""))
30736
+ return false;
30737
+ const outer = env.WORKIT_SESSION_ID?.trim();
30738
+ if (end.agentId || env.WORKIT_PI_WORKER_SESSION || leadOf(session) !== session || outer && leadOf(outer) !== outer)
30739
+ return false;
30740
+ const child = { ...env, WORKIT_SESSION_ID: session, WORKIT_HOST: end.host };
30741
+ for (const key of HOST_SESSION_VARS2)
30742
+ delete child[key];
30743
+ return runCli(["stop", "--session", session, "--reason", "session-end"], child, end.cwd);
30744
+ }
30745
+ var claim2 = (file, now) => {
30746
+ for (let attempt = 0;attempt < 2; attempt += 1)
30747
+ try {
30748
+ fs32.writeFileSync(file, String(now), { flag: "wx", mode: 384 });
30749
+ return true;
30750
+ } catch (error) {
30751
+ if (error.code !== "EEXIST")
30752
+ return false;
30753
+ let held = Number.NaN;
30754
+ try {
30755
+ held = Number(fs32.readFileSync(file, "utf8"));
30756
+ } catch {}
30757
+ if (now - held < REAP_INTERVAL_MS)
30758
+ return false;
30759
+ fs32.rmSync(file, { force: true });
30760
+ }
30761
+ return false;
30762
+ };
30763
+ var fresh = (stamp, now) => {
30764
+ try {
30765
+ const last = Number(fs32.readFileSync(stamp, "utf8"));
30766
+ return now - last < REAP_INTERVAL_MS && last <= now;
30767
+ } catch {
30768
+ return false;
30769
+ }
30770
+ };
30771
+ function reapAtStart(cwd, env = process.env) {
30772
+ const stamp = reapStamp(env);
30773
+ const lock = `${stamp}.claim`;
30774
+ const now = teardownDeps.now();
30775
+ try {
30776
+ if (fresh(stamp, now))
30777
+ return false;
30778
+ fs32.mkdirSync(path52.dirname(stamp), { recursive: true });
30779
+ if (!claim2(lock, now))
30780
+ return false;
30781
+ } catch {
30782
+ return false;
30783
+ }
30784
+ let started = false;
30785
+ try {
30786
+ if (fresh(stamp, now))
30787
+ return false;
30788
+ started = runCli(["stop", "--orphans", "--reason", "session-start"], env, cwd);
30789
+ if (started) {
30790
+ const temp = `${stamp}.${process.pid}`;
30791
+ fs32.writeFileSync(temp, String(now), { mode: 384 });
30792
+ fs32.renameSync(temp, stamp);
30793
+ }
30794
+ } catch {} finally {
30795
+ try {
30796
+ fs32.rmSync(lock, { force: true });
30797
+ } catch {}
30798
+ }
30799
+ return started;
30800
+ }
30801
+
30555
30802
  // packages/workit-core/src/hooks/ytrack-word.ts
30556
30803
  var YTRACK_WORD = /ytrack(?![\w/-]|\.(?!exe|cmd))/i;
30557
30804
  var namesYtrack = (text) => {
@@ -31114,6 +31361,7 @@ function decide(input, deps) {
31114
31361
  const event = input.event;
31115
31362
  switch (event.kind) {
31116
31363
  case "session.start": {
31364
+ reapAtStart(input.cwd);
31117
31365
  if (!usable2(descriptor.context.sessionStart))
31118
31366
  return NONE6;
31119
31367
  const key = input.session.id ? `${input.host}\x00${input.session.id}` : null;
@@ -31128,6 +31376,15 @@ function decide(input, deps) {
31128
31376
  seedTurnContext(input, descriptor);
31129
31377
  return { kind: "context", text };
31130
31378
  }
31379
+ case "session.end":
31380
+ endSession({
31381
+ host: input.host,
31382
+ session: input.session.id,
31383
+ agentId: input.session.agentId,
31384
+ reason: event.reason,
31385
+ cwd: input.cwd
31386
+ });
31387
+ return NONE6;
31131
31388
  case "context.turn": {
31132
31389
  if (!usable2(descriptor.context.perTurn))
31133
31390
  return NONE6;
@@ -31332,11 +31589,11 @@ var modelKey = (model) => {
31332
31589
 
31333
31590
  // packages/workit-core/src/hooks/budget-signal.ts
31334
31591
  import { createHash as createHash23 } from "node:crypto";
31335
- import fs32 from "node:fs";
31336
- import path51 from "node:path";
31592
+ import fs33 from "node:fs";
31593
+ import path53 from "node:path";
31337
31594
  var hash2 = (text) => createHash23("sha256").update(text).digest("hex").slice(0, 32);
31338
- var budgetDir = (store) => path51.join(store, "budget");
31339
- var agentBase = (store, session, agentId) => path51.join(budgetDir(store), hash2(`${session}\x00${agentId ?? ""}`));
31595
+ var budgetDir = (store) => path53.join(store, "budget");
31596
+ var agentBase = (store, session, agentId) => path53.join(budgetDir(store), hash2(`${session}\x00${agentId ?? ""}`));
31340
31597
  var spawnBudget = (prompt) => {
31341
31598
  const found = /^TIMEBOX:[^\n]*?\b(\d+)\s+tool calls\b/mu.exec(prompt);
31342
31599
  const n = found ? Number(found[1]) : NaN;
@@ -31344,7 +31601,7 @@ var spawnBudget = (prompt) => {
31344
31601
  };
31345
31602
  var readBudget = (file) => {
31346
31603
  try {
31347
- const { limit, told } = JSON.parse(fs32.readFileSync(file, "utf8"));
31604
+ const { limit, told } = JSON.parse(fs33.readFileSync(file, "utf8"));
31348
31605
  return typeof limit === "number" && limit > 0 ? { limit, told: Number(told) || 0 } : null;
31349
31606
  } catch {
31350
31607
  return null;
@@ -31352,16 +31609,16 @@ var readBudget = (file) => {
31352
31609
  };
31353
31610
  var startBudget = (store, session, agentId, n) => {
31354
31611
  const dir = budgetDir(store);
31355
- fs32.mkdirSync(dir, { recursive: true });
31612
+ fs33.mkdirSync(dir, { recursive: true });
31356
31613
  const cutoff = Date.now() - 604800000;
31357
- for (const name of fs32.readdirSync(dir))
31614
+ for (const name of fs33.readdirSync(dir))
31358
31615
  try {
31359
- if (fs32.statSync(path51.join(dir, name)).mtimeMs < cutoff)
31360
- fs32.rmSync(path51.join(dir, name));
31616
+ if (fs33.statSync(path53.join(dir, name)).mtimeMs < cutoff)
31617
+ fs33.rmSync(path53.join(dir, name));
31361
31618
  } catch {}
31362
31619
  const base = agentBase(store, session, agentId);
31363
- fs32.writeFileSync(`${base}.json`, JSON.stringify({ limit: n, told: 0 }));
31364
- fs32.rmSync(`${base}.calls`, { force: true });
31620
+ fs33.writeFileSync(`${base}.json`, JSON.stringify({ limit: n, told: 0 }));
31621
+ fs33.rmSync(`${base}.calls`, { force: true });
31365
31622
  };
31366
31623
  var bindBudget = (cwd, session, agentId, prompt) => {
31367
31624
  try {
@@ -31384,8 +31641,8 @@ var tickBudget = (cwd, session, agentId) => {
31384
31641
  const budget = readBudget(`${base}.json`);
31385
31642
  if (!budget)
31386
31643
  return false;
31387
- fs32.appendFileSync(`${base}.calls`, ".");
31388
- return fs32.statSync(`${base}.calls`).size >= budget.limit * (budget.told + 1);
31644
+ fs33.appendFileSync(`${base}.calls`, ".");
31645
+ return fs33.statSync(`${base}.calls`).size >= budget.limit * (budget.told + 1);
31389
31646
  } catch {
31390
31647
  return false;
31391
31648
  }
@@ -31397,14 +31654,14 @@ var reachedOf = (cwd, session, agentId) => {
31397
31654
  const budget = base && readBudget(`${base}.json`);
31398
31655
  if (!budget)
31399
31656
  return null;
31400
- return { base, budget, reached: Math.floor(fs32.statSync(`${base}.calls`).size / budget.limit) };
31657
+ return { base, budget, reached: Math.floor(fs33.statSync(`${base}.calls`).size / budget.limit) };
31401
31658
  };
31402
31659
  var budgetLine = (cwd, session, agentId) => {
31403
31660
  try {
31404
31661
  const at = reachedOf(cwd, session, agentId);
31405
31662
  if (!at || at.reached <= at.budget.told)
31406
31663
  return null;
31407
- fs32.writeFileSync(`${at.base}.json`, JSON.stringify({ ...at.budget, told: at.reached }));
31664
+ fs33.writeFileSync(`${at.base}.json`, JSON.stringify({ ...at.budget, told: at.reached }));
31408
31665
  return lineFor(at.budget.limit * at.reached);
31409
31666
  } catch {
31410
31667
  return null;
@@ -31812,12 +32069,12 @@ var skillPromptNudge = (prompt, ctx) => process.env.WORKIT_PI_WORKER_SESSION ? n
31812
32069
  // packages/workit-pi/src/launches.ts
31813
32070
  import { mkdirSync as mkdirSync7, readFileSync as readFileSync16, readdirSync as readdirSync8, rmSync as rmSync4, writeFileSync as writeFileSync6 } from "node:fs";
31814
32071
  import os12 from "node:os";
31815
- import path52 from "node:path";
32072
+ import path54 from "node:path";
31816
32073
  var launchDir = (root) => {
31817
32074
  const location = resolveStore(root);
31818
- return location instanceof Error ? null : path52.join(location.dir, "pi-launches");
32075
+ return location instanceof Error ? null : path54.join(location.dir, "pi-launches");
31819
32076
  };
31820
- var fileFor = (dir, workerId) => path52.join(dir, `${workerId.replace(/[^A-Za-z0-9-]/g, "_")}.json`);
32077
+ var fileFor = (dir, workerId) => path54.join(dir, `${workerId.replace(/[^A-Za-z0-9-]/g, "_")}.json`);
31821
32078
  var recordLaunch = (root, attempt) => {
31822
32079
  const dir = launchDir(root);
31823
32080
  if (!dir)
@@ -31863,7 +32120,7 @@ var abandonedLaunches = (root) => {
31863
32120
  const found = [];
31864
32121
  for (const name of names) {
31865
32122
  try {
31866
- const attempt = JSON.parse(readFileSync16(path52.join(dir, name), "utf8"));
32123
+ const attempt = JSON.parse(readFileSync16(path54.join(dir, name), "utf8"));
31867
32124
  if (typeof attempt.workerId === "string" && typeof attempt.session === "string") {
31868
32125
  if (!alive(attempt))
31869
32126
  found.push(attempt);
@@ -31874,87 +32131,6 @@ var abandonedLaunches = (root) => {
31874
32131
  };
31875
32132
  var cancelHint = (workerId) => `cancel it first: workit_worker {"action":"cancel","workerId":"${workerId}","reason":"launch outcome unknown"} (CLI: workit worker cancel --payload '{"workerId":"${workerId}","reason":"launch outcome unknown"}')`;
31876
32133
 
31877
- // packages/workit-core/src/core/cli-locate.ts
31878
- import { closeSync as closeSync5, existsSync as existsSync17, openSync as openSync5, readSync as readSync2 } from "node:fs";
31879
- import path53 from "node:path";
31880
- var LAUNCHER_MARKER = "workit-owned-launcher";
31881
- var CLI_PACKAGE_PATH = ["node_modules", "@workit-dev", "cli"];
31882
- var installRoot = (env, platform = process.platform) => {
31883
- if (env.WORKIT_INSTALL_ROOT)
31884
- return env.WORKIT_INSTALL_ROOT;
31885
- const data = dataDir(env, platform);
31886
- return platform === "win32" ? path53.join(data, "install") : data;
31887
- };
31888
- var ownedLauncherPath = (env, platform = process.platform) => path53.join(installRoot(env, platform), "bin", platform === "win32" ? "workit.cmd" : "workit");
31889
- var ownedCurrentEntry = (env, platform = process.platform) => path53.join(installRoot(env, platform), "current", ...CLI_PACKAGE_PATH, "dist", "index.js");
31890
- var head = (file) => {
31891
- let fd;
31892
- try {
31893
- fd = openSync5(file, "r");
31894
- const buffer = Buffer.alloc(256);
31895
- const read = readSync2(fd, buffer, 0, buffer.length, 0);
31896
- return buffer.subarray(0, read).toString("utf8");
31897
- } catch {
31898
- return "";
31899
- } finally {
31900
- if (fd !== undefined)
31901
- closeSync5(fd);
31902
- }
31903
- };
31904
- var locateWorkitCli = (options = {}) => {
31905
- const env = options.env ?? process.env;
31906
- const platform = options.platform ?? process.platform;
31907
- const exists = options.exists ?? existsSync17;
31908
- const read = options.read ?? head;
31909
- const node = options.node ?? process.execPath;
31910
- const launcher = ownedLauncherPath(env, platform);
31911
- if (exists(launcher) && read(launcher).includes(LAUNCHER_MARKER))
31912
- return {
31913
- source: "owned-launcher",
31914
- command: launcher,
31915
- args: [],
31916
- binDir: path53.dirname(launcher)
31917
- };
31918
- const current = ownedCurrentEntry(env, platform);
31919
- if (exists(current))
31920
- return {
31921
- source: "owned-current",
31922
- command: node,
31923
- args: [current],
31924
- binDir: null
31925
- };
31926
- if (options.bundled && exists(options.bundled))
31927
- return {
31928
- source: "bundled",
31929
- command: node,
31930
- args: [options.bundled],
31931
- binDir: null
31932
- };
31933
- return { source: "path", command: "workit", args: [], binDir: null };
31934
- };
31935
- var pathKey = (env) => Object.keys(env).find((key) => key.toUpperCase() === "PATH") ?? "PATH";
31936
- var workitCliEnv = (located, env = process.env, platform = process.platform) => {
31937
- if (located.source === "path")
31938
- return {};
31939
- const patch = {
31940
- WORKIT_CLI: [located.command, ...located.args].join(" ")
31941
- };
31942
- if (located.binDir) {
31943
- const key = pathKey(env);
31944
- const delimiter = platform === "win32" ? ";" : ":";
31945
- const same = (dir) => platform === "win32" ? path53.win32.normalize(dir).toLowerCase() === path53.win32.normalize(located.binDir).toLowerCase() : path53.posix.normalize(dir) === path53.posix.normalize(located.binDir);
31946
- const rest = (env[key] ?? "").split(delimiter).filter((dir) => dir && !same(dir));
31947
- patch[key] = [located.binDir, ...rest].join(delimiter);
31948
- }
31949
- return patch;
31950
- };
31951
- var useOwnedWorkitCli = (options = {}) => {
31952
- const env = options.env ?? process.env;
31953
- const located = locateWorkitCli({ ...options, env });
31954
- Object.assign(env, workitCliEnv(located, env, options.platform));
31955
- return located;
31956
- };
31957
-
31958
32134
  // packages/workit-pi/src/cli-path.ts
31959
32135
  var useOwnedCliInPi = (env = process.env) => useOwnedWorkitCli({ env });
31960
32136
 
@@ -32811,6 +32987,7 @@ function extension(pi) {
32811
32987
  }) + `
32812
32988
  `);
32813
32989
  sessions.delete(ctx.sessionManager.getSessionId());
32990
+ reapAtStart(ctx.cwd);
32814
32991
  reconcileLostWorkers(ctx);
32815
32992
  });
32816
32993
  const procNotice = (ctx) => {
@@ -32933,8 +33110,15 @@ ${nudge}` : content) };
32933
33110
  });
32934
33111
  return observeToolResult(event, ctx);
32935
33112
  };
32936
- pi.on("session_shutdown", (_event, ctx) => {
33113
+ pi.on("session_shutdown", (event, ctx) => {
32937
33114
  sessions.delete(ctx.sessionManager.getSessionId());
33115
+ endSession({
33116
+ host: "pi",
33117
+ session: ctx.sessionManager.getSessionId(),
33118
+ agentId: null,
33119
+ reason: typeof event?.reason === "string" ? event.reason : null,
33120
+ cwd: ctx.cwd
33121
+ });
32938
33122
  });
32939
33123
  }
32940
33124
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workit-dev/pi",
3
- "version": "2.14.0",
3
+ "version": "2.16.0",
4
4
  "description": "Workit native extension for Pi — tools, method skills, and session continuity",
5
5
  "keywords": [
6
6
  "agentic-coding",