@usepipa/pipa 0.5.5 → 0.6.0-beta.1

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/src/state.mjs CHANGED
@@ -3,6 +3,8 @@ import { access, chmod, link, mkdir, readFile, realpath, rename, rm, stat, write
3
3
  import os from "node:os";
4
4
  import path from "node:path";
5
5
  import { randomUUID } from "node:crypto";
6
+ import { spawnSync } from "node:child_process";
7
+ import { setTimeout as delay } from "node:timers/promises";
6
8
 
7
9
  const MANIFEST = {
8
10
  display_information: {
@@ -60,6 +62,7 @@ export function pipaPaths(home = process.env.PIPA_HOME || os.homedir()) {
60
62
  routines: path.join(directory, "routines.json"),
61
63
  routinesLock: path.join(directory, "routines.lock"),
62
64
  sessions: path.join(directory, "sessions.json"),
65
+ openCodeRecovery: path.join(directory, "opencode-recovery.json"),
63
66
  };
64
67
  }
65
68
 
@@ -84,10 +87,26 @@ export async function acquireInstanceLock(file = pipaPaths().lock) {
84
87
  return release;
85
88
  }
86
89
 
87
- export async function stopInstance(file = pipaPaths().lock, kill = process.kill) {
90
+ export async function stopInstance(file = pipaPaths().lock, kill = process.kill, { platform = process.platform, terminate = spawnSync, running = isRunning } = {}) {
88
91
  const identity = await readInstanceLock(file);
89
92
  const pid = identity?.pid;
90
93
  if (!pid) return null;
94
+ if (platform === "win32" && kill === process.kill) {
95
+ if (!running(pid)) { removeInstanceLock(file, identity); return null; }
96
+ // Windows SIGTERM does not run Node's shutdown handler. Stop the owned tree
97
+ // and release only the unchanged lock after the original process has exited.
98
+ const result = terminate(process.env.SystemRoot ? path.join(process.env.SystemRoot, "System32", "taskkill.exe") : "taskkill", ["/PID", String(pid), "/T", "/F"], {
99
+ timeout: 5000, windowsHide: true, stdio: "ignore", shell: false,
100
+ });
101
+ if (result.error || result.status !== 0) throw new Error("Could not stop Pipa's Windows process tree.");
102
+ const deadline = Date.now() + 5000;
103
+ while (running(pid)) {
104
+ if (Date.now() >= deadline) throw new Error("Could not confirm Pipa's Windows process tree stopped.");
105
+ await delay(25);
106
+ }
107
+ removeInstanceLock(file, identity);
108
+ return pid;
109
+ }
91
110
  try {
92
111
  kill(pid, "SIGTERM");
93
112
  return pid;
@@ -183,6 +202,9 @@ export async function loadConfig(file = pipaPaths().config) {
183
202
  throw new Error(`Invalid Pipa config: ${key} must be a list of non-empty Slack IDs.`);
184
203
  }
185
204
  }
205
+ if (config.mentionOnly !== undefined && typeof config.mentionOnly !== "boolean") {
206
+ throw new Error("Invalid Pipa config: mentionOnly must be a boolean.");
207
+ }
186
208
  normalizeModel(config.defaultModel);
187
209
  return { ...config, slackMode };
188
210
  }