@wrongstack/tools 0.272.1 → 0.273.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/index.js CHANGED
@@ -626,7 +626,7 @@ var replaceTool = {
626
626
  if (err.code === "ENOENT") return null;
627
627
  throw err;
628
628
  });
629
- if (!lstat2 || !lstat2.isFile()) continue;
629
+ if (!lstat2?.isFile()) continue;
630
630
  if (lstat2.isSymbolicLink()) continue;
631
631
  let realPath;
632
632
  try {
@@ -637,7 +637,7 @@ var replaceTool = {
637
637
  const rel = path.relative(realRoot, realPath);
638
638
  if (rel.startsWith("..") || path.isAbsolute(rel)) continue;
639
639
  const stat11 = await fs7.stat(realPath).catch(() => null);
640
- if (!stat11 || !stat11.isFile()) continue;
640
+ if (!stat11?.isFile()) continue;
641
641
  let content;
642
642
  try {
643
643
  const buf = await fs7.readFile(realPath);
@@ -1798,6 +1798,21 @@ function _resetProcessRegistry() {
1798
1798
  _registry = void 0;
1799
1799
  }
1800
1800
  var REGISTRY_FILE = ".wrongstack/process-registry.json";
1801
+ function toErrorMessage2(err) {
1802
+ return err instanceof Error ? err.message : String(err);
1803
+ }
1804
+ function emitStructuredLog(level, event, message, error) {
1805
+ const payload = {
1806
+ level,
1807
+ event,
1808
+ message,
1809
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1810
+ };
1811
+ if (error !== void 0) {
1812
+ payload.error = toErrorMessage2(error);
1813
+ }
1814
+ console.log(JSON.stringify(payload));
1815
+ }
1801
1816
  var HEARTBEAT_INTERVAL_MS = 5e3;
1802
1817
  var STALE_THRESHOLD_MS = 3e4;
1803
1818
  var LOCKFILE = ".wrongstack/.process-registry.lock";
@@ -1807,6 +1822,9 @@ function generateInstanceId() {
1807
1822
  const random = Math.random().toString(36).slice(2, 8);
1808
1823
  return `${hostname4}:${pid}:${random}`;
1809
1824
  }
1825
+ function isNodeError(err) {
1826
+ return typeof err === "object" && err !== null && "code" in err;
1827
+ }
1810
1828
  async function acquireLock(lockfilePath, timeoutMs = 5e3) {
1811
1829
  const start = Date.now();
1812
1830
  const pidStr = String(process.pid);
@@ -1821,7 +1839,7 @@ async function acquireLock(lockfilePath, timeoutMs = 5e3) {
1821
1839
  }
1822
1840
  };
1823
1841
  } catch (err) {
1824
- if (err.code === "EEXIST") {
1842
+ if (isNodeError(err) && err.code === "EEXIST") {
1825
1843
  try {
1826
1844
  const content = await fs7.readFile(lockfilePath, "utf-8");
1827
1845
  const parts = content.split(":");
@@ -1858,7 +1876,7 @@ async function readRegistryFile(filePath) {
1858
1876
  }
1859
1877
  return parsed;
1860
1878
  } catch (err) {
1861
- if (err.code === "ENOENT") {
1879
+ if (isNodeError(err) && err.code === "ENOENT") {
1862
1880
  return {
1863
1881
  version: 1,
1864
1882
  instances: /* @__PURE__ */ new Map(),
@@ -1894,7 +1912,7 @@ var PersistentProcessRegistry = class {
1894
1912
  this.lockPath = path.join(homeDir, LOCKFILE);
1895
1913
  this.baseRegistry = baseRegistry ?? getProcessRegistry();
1896
1914
  this.ensureDirectory().catch((err) => {
1897
- console.error("PersistentProcessRegistry: failed to create .wrongstack dir", err);
1915
+ emitStructuredLog("warn", "process_registry.dir_create_failed", "PersistentProcessRegistry: failed to create .wrongstack directory", err);
1898
1916
  });
1899
1917
  }
1900
1918
  async ensureDirectory() {
@@ -1902,7 +1920,7 @@ var PersistentProcessRegistry = class {
1902
1920
  try {
1903
1921
  await fs7.mkdir(dir, { recursive: true });
1904
1922
  } catch (err) {
1905
- if (err.code !== "EEXIST") throw err;
1923
+ if (!isNodeError(err) || err.code !== "EEXIST") throw err;
1906
1924
  }
1907
1925
  }
1908
1926
  /**
@@ -1982,6 +2000,7 @@ var PersistentProcessRegistry = class {
1982
2000
  try {
1983
2001
  const data = await readRegistryFile(this.registryPath);
1984
2002
  data.instances.set(String(entry.pid), entry);
2003
+ const child = null;
1985
2004
  this.baseRegistry.register({
1986
2005
  pid: entry.pid,
1987
2006
  name: entry.name,
@@ -1989,8 +2008,7 @@ var PersistentProcessRegistry = class {
1989
2008
  startedAt: entry.startedAt,
1990
2009
  sessionId: entry.sessionId,
1991
2010
  protected: entry.protected,
1992
- child: null
1993
- // Main process has no child handle
2011
+ child
1994
2012
  });
1995
2013
  await writeRegistryFile(this.registryPath, data);
1996
2014
  } finally {
@@ -2038,7 +2056,7 @@ var PersistentProcessRegistry = class {
2038
2056
  data.lastCleanup = now2;
2039
2057
  await writeRegistryFile(this.registryPath, data);
2040
2058
  } catch (err) {
2041
- console.error("PersistentProcessRegistry: sync failed", err);
2059
+ emitStructuredLog("warn", "process_registry.sync_failed", "PersistentProcessRegistry: sync failed", err);
2042
2060
  } finally {
2043
2061
  await release();
2044
2062
  }
@@ -2059,7 +2077,11 @@ var PersistentProcessRegistry = class {
2059
2077
  if (process.platform !== "win32") {
2060
2078
  process.kill(entry.pid, 0);
2061
2079
  } else {
2062
- console.log(`PersistentProcessRegistry: checking stale pid ${entry.pid} (${age}ms old)`);
2080
+ emitStructuredLog(
2081
+ "debug",
2082
+ "process_registry.stale_pid_check",
2083
+ `PersistentProcessRegistry: checking stale pid ${entry.pid} (${age}ms old)`
2084
+ );
2063
2085
  }
2064
2086
  } catch {
2065
2087
  stalePids.push(_pidStr);
@@ -2073,7 +2095,7 @@ var PersistentProcessRegistry = class {
2073
2095
  await writeRegistryFile(this.registryPath, data);
2074
2096
  }
2075
2097
  } catch (err) {
2076
- console.error("PersistentProcessRegistry: cleanup failed", err);
2098
+ emitStructuredLog("warn", "process_registry.cleanup_failed", "PersistentProcessRegistry: cleanup failed", err);
2077
2099
  } finally {
2078
2100
  await release();
2079
2101
  }
@@ -2302,7 +2324,7 @@ async function isKillProtected(kill) {
2302
2324
  const entries = await getProtectedEntries();
2303
2325
  const killNameLower = kill.name.toLowerCase();
2304
2326
  for (const entry of entries) {
2305
- if (entry.name && entry.name.toLowerCase().includes(killNameLower)) {
2327
+ if (entry.name?.toLowerCase().includes(killNameLower)) {
2306
2328
  return true;
2307
2329
  }
2308
2330
  }
@@ -8982,11 +9004,15 @@ async function tryNativeParse(file, content) {
8982
9004
  const crateDir = path.join(toolsDir, "syn-parser");
8983
9005
  const tmpFile = path.join(crateDir, "src", "input.rs");
8984
9006
  await fs7.writeFile(tmpFile, content, "utf8");
8985
- const proc = spawn("cargo", ["run", "--manifest-path", path.join(toolsDir, "Cargo.toml")], {
8986
- cwd: process.cwd(),
8987
- stdio: ["pipe", "pipe", "pipe"],
8988
- windowsHide: true
8989
- });
9007
+ const proc = spawn(
9008
+ "cargo",
9009
+ ["run", "--manifest-path", path.join(toolsDir, "Cargo.toml")],
9010
+ {
9011
+ cwd: process.cwd(),
9012
+ stdio: ["pipe", "pipe", "pipe"],
9013
+ windowsHide: true
9014
+ }
9015
+ );
8990
9016
  let stdout = "";
8991
9017
  proc.stdout?.on("data", (chunk) => {
8992
9018
  stdout += chunk.toString();
@@ -10482,7 +10508,11 @@ var taskTool = {
10482
10508
  const newIds = new Set(input.tasks.map((t) => t.id));
10483
10509
  if (newIds.size !== input.tasks.length) {
10484
10510
  const seen = /* @__PURE__ */ new Set();
10485
- const dupes = [...new Set(input.tasks.map((t) => t.id).filter((id) => seen.has(id) ? true : (seen.add(id), false)))];
10511
+ const dupes = [...new Set(input.tasks.map((t) => t.id).filter((id) => {
10512
+ if (seen.has(id)) return true;
10513
+ seen.add(id);
10514
+ return false;
10515
+ }))];
10486
10516
  early = {
10487
10517
  ok: false,
10488
10518
  message: `action=replace has duplicate task IDs: ${dupes.join(", ")}. Each task id must be unique.`,
@@ -10517,7 +10547,7 @@ var taskTool = {
10517
10547
  }
10518
10548
  case "add": {
10519
10549
  const t = input.task;
10520
- if (!t || !t.title) {
10550
+ if (!t?.title) {
10521
10551
  early = { ok: false, message: "action=add requires `task` with at least `title`.", count: 0, completed: 0, inProgress: 0 };
10522
10552
  return f;
10523
10553
  }