@workit-dev/pi 2.14.0 → 2.15.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
@@ -28592,11 +28592,12 @@ var inlineCallDeny = (fix) => `Run \`${fix}\` as its own call so the host can as
28592
28592
  var CLAUDE_CODE_DESCRIPTOR = {
28593
28593
  host: "claude_code",
28594
28594
  label: "Claude Code",
28595
- verifiedAgainst: "claude 2.1.288 (hook zod schemas in the binary)",
28595
+ verifiedAgainst: "claude 2.1.288 (hook zod schemas in the binary; SessionEnd: 2.1.327)",
28596
28596
  docs: ["https://code.claude.com/docs/en/hooks"],
28597
28597
  transport: "hook-process",
28598
28598
  events: {
28599
28599
  "session.start": { support: "native", native: "SessionStart" },
28600
+ "session.end": { support: "native", native: "SessionEnd" },
28600
28601
  "context.turn": { support: "native", native: "UserPromptSubmit" },
28601
28602
  "shell.pre": { support: "native", native: "PreToolUse" },
28602
28603
  "tool.pre": { support: "native", native: "PreToolUse" },
@@ -28706,6 +28707,7 @@ var CODEX_DESCRIPTOR = {
28706
28707
  transport: "hook-process",
28707
28708
  events: {
28708
28709
  "session.start": { support: "native", native: "SessionStart" },
28710
+ "session.end": { support: "none", native: null },
28709
28711
  "context.turn": { support: "native", native: "UserPromptSubmit" },
28710
28712
  "shell.pre": { support: "native", native: "PreToolUse" },
28711
28713
  "tool.pre": { support: "native", native: "PreToolUse" },
@@ -28816,6 +28818,7 @@ var CURSOR_DESCRIPTOR = {
28816
28818
  transport: "hook-process",
28817
28819
  events: {
28818
28820
  "session.start": { support: "native", native: "sessionStart" },
28821
+ "session.end": { support: "native", native: "sessionEnd" },
28819
28822
  "context.turn": none,
28820
28823
  "shell.pre": { support: "native", native: "beforeShellExecution" },
28821
28824
  "tool.pre": { support: "native", native: "preToolUse" },
@@ -28922,6 +28925,7 @@ var OPENCODE_DESCRIPTOR = {
28922
28925
  transport: "in-process-plugin",
28923
28926
  events: {
28924
28927
  "session.start": { support: "none", native: null },
28928
+ "session.end": { support: "partial", native: 'event("session.deleted")' },
28925
28929
  "context.turn": { support: "native", native: 'session.hook("context")' },
28926
28930
  "shell.pre": { support: "native", native: 'permission.hook("evaluate")' },
28927
28931
  "tool.pre": { support: "native", native: 'tool.hook("execute.before")' },
@@ -28991,6 +28995,7 @@ var PI_DESCRIPTOR = {
28991
28995
  transport: "in-process-plugin",
28992
28996
  events: {
28993
28997
  "session.start": { support: "native", native: "session_start" },
28998
+ "session.end": { support: "native", native: "session_shutdown" },
28994
28999
  "context.turn": { support: "native", native: "before_agent_start" },
28995
29000
  "shell.pre": { support: "native", native: "tool_call" },
28996
29001
  "tool.pre": { support: "native", native: "tool_call" },
@@ -30552,6 +30557,239 @@ function takeProcNotice(session, env = process.env) {
30552
30557
  }
30553
30558
  }
30554
30559
 
30560
+ // packages/workit-core/src/hooks/teardown.ts
30561
+ import { spawn } from "node:child_process";
30562
+ import fs32 from "node:fs";
30563
+ import path52 from "node:path";
30564
+
30565
+ // packages/workit-core/src/core/cli-locate.ts
30566
+ import { closeSync as closeSync5, existsSync as existsSync17, openSync as openSync5, readSync as readSync2 } from "node:fs";
30567
+ import path51 from "node:path";
30568
+ var LAUNCHER_MARKER = "workit-owned-launcher";
30569
+ var CLI_PACKAGE_PATH = ["node_modules", "@workit-dev", "cli"];
30570
+ var installRoot = (env, platform = process.platform) => {
30571
+ if (env.WORKIT_INSTALL_ROOT)
30572
+ return env.WORKIT_INSTALL_ROOT;
30573
+ const data = dataDir(env, platform);
30574
+ return platform === "win32" ? path51.join(data, "install") : data;
30575
+ };
30576
+ var ownedLauncherPath = (env, platform = process.platform) => path51.join(installRoot(env, platform), "bin", platform === "win32" ? "workit.cmd" : "workit");
30577
+ var ownedCurrentEntry = (env, platform = process.platform) => path51.join(installRoot(env, platform), "current", ...CLI_PACKAGE_PATH, "dist", "index.js");
30578
+ var head = (file) => {
30579
+ let fd;
30580
+ try {
30581
+ fd = openSync5(file, "r");
30582
+ const buffer = Buffer.alloc(256);
30583
+ const read = readSync2(fd, buffer, 0, buffer.length, 0);
30584
+ return buffer.subarray(0, read).toString("utf8");
30585
+ } catch {
30586
+ return "";
30587
+ } finally {
30588
+ if (fd !== undefined)
30589
+ closeSync5(fd);
30590
+ }
30591
+ };
30592
+ var locateWorkitCli = (options = {}) => {
30593
+ const env = options.env ?? process.env;
30594
+ const platform = options.platform ?? process.platform;
30595
+ const exists = options.exists ?? existsSync17;
30596
+ const read = options.read ?? head;
30597
+ const node = options.node ?? process.execPath;
30598
+ const launcher = ownedLauncherPath(env, platform);
30599
+ if (exists(launcher) && read(launcher).includes(LAUNCHER_MARKER))
30600
+ return {
30601
+ source: "owned-launcher",
30602
+ command: launcher,
30603
+ args: [],
30604
+ binDir: path51.dirname(launcher)
30605
+ };
30606
+ const current = ownedCurrentEntry(env, platform);
30607
+ if (exists(current))
30608
+ return {
30609
+ source: "owned-current",
30610
+ command: node,
30611
+ args: [current],
30612
+ binDir: null
30613
+ };
30614
+ if (options.bundled && exists(options.bundled))
30615
+ return {
30616
+ source: "bundled",
30617
+ command: node,
30618
+ args: [options.bundled],
30619
+ binDir: null
30620
+ };
30621
+ return { source: "path", command: "workit", args: [], binDir: null };
30622
+ };
30623
+ var pathKey = (env) => Object.keys(env).find((key) => key.toUpperCase() === "PATH") ?? "PATH";
30624
+ var workitCliEnv = (located, env = process.env, platform = process.platform) => {
30625
+ if (located.source === "path")
30626
+ return {};
30627
+ const patch = {
30628
+ WORKIT_CLI: [located.command, ...located.args].join(" ")
30629
+ };
30630
+ if (located.binDir) {
30631
+ const key = pathKey(env);
30632
+ const delimiter = platform === "win32" ? ";" : ":";
30633
+ 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);
30634
+ const rest = (env[key] ?? "").split(delimiter).filter((dir) => dir && !same(dir));
30635
+ patch[key] = [located.binDir, ...rest].join(delimiter);
30636
+ }
30637
+ return patch;
30638
+ };
30639
+ var useOwnedWorkitCli = (options = {}) => {
30640
+ const env = options.env ?? process.env;
30641
+ const located = locateWorkitCli({ ...options, env });
30642
+ Object.assign(env, workitCliEnv(located, env, options.platform));
30643
+ return located;
30644
+ };
30645
+
30646
+ // packages/workit-core/src/hooks/teardown.ts
30647
+ var REAP_INTERVAL_MS = 10 * 60000;
30648
+ var LOG_MAX_BYTES = 1 << 20;
30649
+ var ENDS = {
30650
+ claude_code: new Set(["logout", "prompt_input_exit", "other"]),
30651
+ cursor: new Set(["completed", "aborted", "error", "window_close", "user_close"]),
30652
+ pi: new Set(["quit"]),
30653
+ opencode: new Set(["deleted"])
30654
+ };
30655
+ var SAFE_SESSION = /^[\w.@+][\w.:@/+-]{0,127}$/u;
30656
+ var HOST_SESSION_VARS2 = ["OPENCODE_SESSION_ID", "PI_SESSION_ID", "CODEX_THREAD_ID"];
30657
+ var teardownDir = (env) => path52.join(stateDir(env), "procs");
30658
+ var teardownLog = (env) => path52.join(teardownDir(env), "teardown.log");
30659
+ var reapStamp = (env) => path52.join(teardownDir(env), "reap-at-start.stamp");
30660
+ function spawnDetached(request) {
30661
+ try {
30662
+ const shell = process.platform === "win32" && !/\.exe$/iu.test(request.command);
30663
+ const line = [`"${request.command}"`, ...request.args].join(" ");
30664
+ const child = spawn(shell ? line : request.command, shell ? [] : request.args, {
30665
+ cwd: request.cwd,
30666
+ env: request.env,
30667
+ detached: true,
30668
+ shell,
30669
+ stdio: ["ignore", request.log ?? "ignore", request.log ?? "ignore"],
30670
+ windowsHide: true
30671
+ });
30672
+ child.on("error", () => {});
30673
+ child.unref();
30674
+ return child.pid !== undefined;
30675
+ } catch {
30676
+ return false;
30677
+ }
30678
+ }
30679
+ var teardownDeps = {
30680
+ spawn: (request) => request.env.WORKIT_TEST_RUN === undefined && spawnDetached(request),
30681
+ now: () => Date.now()
30682
+ };
30683
+ var openLog = (env, args) => {
30684
+ try {
30685
+ const file = teardownLog(env);
30686
+ fs32.mkdirSync(path52.dirname(file), { recursive: true });
30687
+ let size = 0;
30688
+ try {
30689
+ size = fs32.statSync(file).size;
30690
+ } catch {}
30691
+ const fd = fs32.openSync(file, size > LOG_MAX_BYTES ? "w" : "a", 384);
30692
+ fs32.writeSync(fd, `${new Date(teardownDeps.now()).toISOString()} workit ${args.join(" ")}
30693
+ `);
30694
+ return fd;
30695
+ } catch {
30696
+ return null;
30697
+ }
30698
+ };
30699
+ function runCli(args, env, cwd) {
30700
+ let log = null;
30701
+ try {
30702
+ const cli = locateWorkitCli({ env });
30703
+ log = openLog(env, args);
30704
+ const started = teardownDeps.spawn({
30705
+ command: cli.command,
30706
+ args: [...cli.args, ...args],
30707
+ env,
30708
+ cwd: fs32.existsSync(cwd) ? cwd : teardownDir(env),
30709
+ log
30710
+ });
30711
+ if (!started && log !== null)
30712
+ fs32.writeSync(log, ` not started
30713
+ `);
30714
+ return started;
30715
+ } catch {
30716
+ return false;
30717
+ } finally {
30718
+ if (log !== null)
30719
+ try {
30720
+ fs32.closeSync(log);
30721
+ } catch {}
30722
+ }
30723
+ }
30724
+ function endSession(end, env = process.env) {
30725
+ const session = end.session?.trim() ?? "";
30726
+ if (!SAFE_SESSION.test(session) || !ENDS[end.host]?.has(end.reason ?? ""))
30727
+ return false;
30728
+ const outer = env.WORKIT_SESSION_ID?.trim();
30729
+ if (end.agentId || env.WORKIT_PI_WORKER_SESSION || leadOf(session) !== session || outer && leadOf(outer) !== outer)
30730
+ return false;
30731
+ const child = { ...env, WORKIT_SESSION_ID: session, WORKIT_HOST: end.host };
30732
+ for (const key of HOST_SESSION_VARS2)
30733
+ delete child[key];
30734
+ return runCli(["stop", "--session", session, "--reason", "session-end"], child, end.cwd);
30735
+ }
30736
+ var claim2 = (file, now) => {
30737
+ for (let attempt = 0;attempt < 2; attempt += 1)
30738
+ try {
30739
+ fs32.writeFileSync(file, String(now), { flag: "wx", mode: 384 });
30740
+ return true;
30741
+ } catch (error) {
30742
+ if (error.code !== "EEXIST")
30743
+ return false;
30744
+ let held = Number.NaN;
30745
+ try {
30746
+ held = Number(fs32.readFileSync(file, "utf8"));
30747
+ } catch {}
30748
+ if (now - held < REAP_INTERVAL_MS)
30749
+ return false;
30750
+ fs32.rmSync(file, { force: true });
30751
+ }
30752
+ return false;
30753
+ };
30754
+ var fresh = (stamp, now) => {
30755
+ try {
30756
+ const last = Number(fs32.readFileSync(stamp, "utf8"));
30757
+ return now - last < REAP_INTERVAL_MS && last <= now;
30758
+ } catch {
30759
+ return false;
30760
+ }
30761
+ };
30762
+ function reapAtStart(cwd, env = process.env) {
30763
+ const stamp = reapStamp(env);
30764
+ const lock = `${stamp}.claim`;
30765
+ const now = teardownDeps.now();
30766
+ try {
30767
+ if (fresh(stamp, now))
30768
+ return false;
30769
+ fs32.mkdirSync(path52.dirname(stamp), { recursive: true });
30770
+ if (!claim2(lock, now))
30771
+ return false;
30772
+ } catch {
30773
+ return false;
30774
+ }
30775
+ let started = false;
30776
+ try {
30777
+ if (fresh(stamp, now))
30778
+ return false;
30779
+ started = runCli(["stop", "--orphans", "--reason", "session-start"], env, cwd);
30780
+ if (started) {
30781
+ const temp = `${stamp}.${process.pid}`;
30782
+ fs32.writeFileSync(temp, String(now), { mode: 384 });
30783
+ fs32.renameSync(temp, stamp);
30784
+ }
30785
+ } catch {} finally {
30786
+ try {
30787
+ fs32.rmSync(lock, { force: true });
30788
+ } catch {}
30789
+ }
30790
+ return started;
30791
+ }
30792
+
30555
30793
  // packages/workit-core/src/hooks/ytrack-word.ts
30556
30794
  var YTRACK_WORD = /ytrack(?![\w/-]|\.(?!exe|cmd))/i;
30557
30795
  var namesYtrack = (text) => {
@@ -31114,6 +31352,7 @@ function decide(input, deps) {
31114
31352
  const event = input.event;
31115
31353
  switch (event.kind) {
31116
31354
  case "session.start": {
31355
+ reapAtStart(input.cwd);
31117
31356
  if (!usable2(descriptor.context.sessionStart))
31118
31357
  return NONE6;
31119
31358
  const key = input.session.id ? `${input.host}\x00${input.session.id}` : null;
@@ -31128,6 +31367,15 @@ function decide(input, deps) {
31128
31367
  seedTurnContext(input, descriptor);
31129
31368
  return { kind: "context", text };
31130
31369
  }
31370
+ case "session.end":
31371
+ endSession({
31372
+ host: input.host,
31373
+ session: input.session.id,
31374
+ agentId: input.session.agentId,
31375
+ reason: event.reason,
31376
+ cwd: input.cwd
31377
+ });
31378
+ return NONE6;
31131
31379
  case "context.turn": {
31132
31380
  if (!usable2(descriptor.context.perTurn))
31133
31381
  return NONE6;
@@ -31332,11 +31580,11 @@ var modelKey = (model) => {
31332
31580
 
31333
31581
  // packages/workit-core/src/hooks/budget-signal.ts
31334
31582
  import { createHash as createHash23 } from "node:crypto";
31335
- import fs32 from "node:fs";
31336
- import path51 from "node:path";
31583
+ import fs33 from "node:fs";
31584
+ import path53 from "node:path";
31337
31585
  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 ?? ""}`));
31586
+ var budgetDir = (store) => path53.join(store, "budget");
31587
+ var agentBase = (store, session, agentId) => path53.join(budgetDir(store), hash2(`${session}\x00${agentId ?? ""}`));
31340
31588
  var spawnBudget = (prompt) => {
31341
31589
  const found = /^TIMEBOX:[^\n]*?\b(\d+)\s+tool calls\b/mu.exec(prompt);
31342
31590
  const n = found ? Number(found[1]) : NaN;
@@ -31344,7 +31592,7 @@ var spawnBudget = (prompt) => {
31344
31592
  };
31345
31593
  var readBudget = (file) => {
31346
31594
  try {
31347
- const { limit, told } = JSON.parse(fs32.readFileSync(file, "utf8"));
31595
+ const { limit, told } = JSON.parse(fs33.readFileSync(file, "utf8"));
31348
31596
  return typeof limit === "number" && limit > 0 ? { limit, told: Number(told) || 0 } : null;
31349
31597
  } catch {
31350
31598
  return null;
@@ -31352,16 +31600,16 @@ var readBudget = (file) => {
31352
31600
  };
31353
31601
  var startBudget = (store, session, agentId, n) => {
31354
31602
  const dir = budgetDir(store);
31355
- fs32.mkdirSync(dir, { recursive: true });
31603
+ fs33.mkdirSync(dir, { recursive: true });
31356
31604
  const cutoff = Date.now() - 604800000;
31357
- for (const name of fs32.readdirSync(dir))
31605
+ for (const name of fs33.readdirSync(dir))
31358
31606
  try {
31359
- if (fs32.statSync(path51.join(dir, name)).mtimeMs < cutoff)
31360
- fs32.rmSync(path51.join(dir, name));
31607
+ if (fs33.statSync(path53.join(dir, name)).mtimeMs < cutoff)
31608
+ fs33.rmSync(path53.join(dir, name));
31361
31609
  } catch {}
31362
31610
  const base = agentBase(store, session, agentId);
31363
- fs32.writeFileSync(`${base}.json`, JSON.stringify({ limit: n, told: 0 }));
31364
- fs32.rmSync(`${base}.calls`, { force: true });
31611
+ fs33.writeFileSync(`${base}.json`, JSON.stringify({ limit: n, told: 0 }));
31612
+ fs33.rmSync(`${base}.calls`, { force: true });
31365
31613
  };
31366
31614
  var bindBudget = (cwd, session, agentId, prompt) => {
31367
31615
  try {
@@ -31384,8 +31632,8 @@ var tickBudget = (cwd, session, agentId) => {
31384
31632
  const budget = readBudget(`${base}.json`);
31385
31633
  if (!budget)
31386
31634
  return false;
31387
- fs32.appendFileSync(`${base}.calls`, ".");
31388
- return fs32.statSync(`${base}.calls`).size >= budget.limit * (budget.told + 1);
31635
+ fs33.appendFileSync(`${base}.calls`, ".");
31636
+ return fs33.statSync(`${base}.calls`).size >= budget.limit * (budget.told + 1);
31389
31637
  } catch {
31390
31638
  return false;
31391
31639
  }
@@ -31397,14 +31645,14 @@ var reachedOf = (cwd, session, agentId) => {
31397
31645
  const budget = base && readBudget(`${base}.json`);
31398
31646
  if (!budget)
31399
31647
  return null;
31400
- return { base, budget, reached: Math.floor(fs32.statSync(`${base}.calls`).size / budget.limit) };
31648
+ return { base, budget, reached: Math.floor(fs33.statSync(`${base}.calls`).size / budget.limit) };
31401
31649
  };
31402
31650
  var budgetLine = (cwd, session, agentId) => {
31403
31651
  try {
31404
31652
  const at = reachedOf(cwd, session, agentId);
31405
31653
  if (!at || at.reached <= at.budget.told)
31406
31654
  return null;
31407
- fs32.writeFileSync(`${at.base}.json`, JSON.stringify({ ...at.budget, told: at.reached }));
31655
+ fs33.writeFileSync(`${at.base}.json`, JSON.stringify({ ...at.budget, told: at.reached }));
31408
31656
  return lineFor(at.budget.limit * at.reached);
31409
31657
  } catch {
31410
31658
  return null;
@@ -31812,12 +32060,12 @@ var skillPromptNudge = (prompt, ctx) => process.env.WORKIT_PI_WORKER_SESSION ? n
31812
32060
  // packages/workit-pi/src/launches.ts
31813
32061
  import { mkdirSync as mkdirSync7, readFileSync as readFileSync16, readdirSync as readdirSync8, rmSync as rmSync4, writeFileSync as writeFileSync6 } from "node:fs";
31814
32062
  import os12 from "node:os";
31815
- import path52 from "node:path";
32063
+ import path54 from "node:path";
31816
32064
  var launchDir = (root) => {
31817
32065
  const location = resolveStore(root);
31818
- return location instanceof Error ? null : path52.join(location.dir, "pi-launches");
32066
+ return location instanceof Error ? null : path54.join(location.dir, "pi-launches");
31819
32067
  };
31820
- var fileFor = (dir, workerId) => path52.join(dir, `${workerId.replace(/[^A-Za-z0-9-]/g, "_")}.json`);
32068
+ var fileFor = (dir, workerId) => path54.join(dir, `${workerId.replace(/[^A-Za-z0-9-]/g, "_")}.json`);
31821
32069
  var recordLaunch = (root, attempt) => {
31822
32070
  const dir = launchDir(root);
31823
32071
  if (!dir)
@@ -31863,7 +32111,7 @@ var abandonedLaunches = (root) => {
31863
32111
  const found = [];
31864
32112
  for (const name of names) {
31865
32113
  try {
31866
- const attempt = JSON.parse(readFileSync16(path52.join(dir, name), "utf8"));
32114
+ const attempt = JSON.parse(readFileSync16(path54.join(dir, name), "utf8"));
31867
32115
  if (typeof attempt.workerId === "string" && typeof attempt.session === "string") {
31868
32116
  if (!alive(attempt))
31869
32117
  found.push(attempt);
@@ -31874,87 +32122,6 @@ var abandonedLaunches = (root) => {
31874
32122
  };
31875
32123
  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
32124
 
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
32125
  // packages/workit-pi/src/cli-path.ts
31959
32126
  var useOwnedCliInPi = (env = process.env) => useOwnedWorkitCli({ env });
31960
32127
 
@@ -32811,6 +32978,7 @@ function extension(pi) {
32811
32978
  }) + `
32812
32979
  `);
32813
32980
  sessions.delete(ctx.sessionManager.getSessionId());
32981
+ reapAtStart(ctx.cwd);
32814
32982
  reconcileLostWorkers(ctx);
32815
32983
  });
32816
32984
  const procNotice = (ctx) => {
@@ -32933,8 +33101,15 @@ ${nudge}` : content) };
32933
33101
  });
32934
33102
  return observeToolResult(event, ctx);
32935
33103
  };
32936
- pi.on("session_shutdown", (_event, ctx) => {
33104
+ pi.on("session_shutdown", (event, ctx) => {
32937
33105
  sessions.delete(ctx.sessionManager.getSessionId());
33106
+ endSession({
33107
+ host: "pi",
33108
+ session: ctx.sessionManager.getSessionId(),
33109
+ agentId: null,
33110
+ reason: typeof event?.reason === "string" ? event.reason : null,
33111
+ cwd: ctx.cwd
33112
+ });
32938
33113
  });
32939
33114
  }
32940
33115
  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.15.0",
4
4
  "description": "Workit native extension for Pi — tools, method skills, and session continuity",
5
5
  "keywords": [
6
6
  "agentic-coding",