@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/bash.js CHANGED
@@ -699,6 +699,21 @@ function getProcessRegistry() {
699
699
  return _registry;
700
700
  }
701
701
  var REGISTRY_FILE = ".wrongstack/process-registry.json";
702
+ function toErrorMessage(err) {
703
+ return err instanceof Error ? err.message : String(err);
704
+ }
705
+ function emitStructuredLog(level, event, message, error) {
706
+ const payload = {
707
+ level,
708
+ event,
709
+ message,
710
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
711
+ };
712
+ if (error !== void 0) {
713
+ payload.error = toErrorMessage(error);
714
+ }
715
+ console.log(JSON.stringify(payload));
716
+ }
702
717
  var HEARTBEAT_INTERVAL_MS = 5e3;
703
718
  var STALE_THRESHOLD_MS = 3e4;
704
719
  var LOCKFILE = ".wrongstack/.process-registry.lock";
@@ -708,6 +723,9 @@ function generateInstanceId() {
708
723
  const random = Math.random().toString(36).slice(2, 8);
709
724
  return `${hostname2}:${pid}:${random}`;
710
725
  }
726
+ function isNodeError(err) {
727
+ return typeof err === "object" && err !== null && "code" in err;
728
+ }
711
729
  async function acquireLock(lockfilePath, timeoutMs = 5e3) {
712
730
  const start = Date.now();
713
731
  const pidStr = String(process.pid);
@@ -722,7 +740,7 @@ async function acquireLock(lockfilePath, timeoutMs = 5e3) {
722
740
  }
723
741
  };
724
742
  } catch (err) {
725
- if (err.code === "EEXIST") {
743
+ if (isNodeError(err) && err.code === "EEXIST") {
726
744
  try {
727
745
  const content = await fs.readFile(lockfilePath, "utf-8");
728
746
  const parts = content.split(":");
@@ -759,7 +777,7 @@ async function readRegistryFile(filePath) {
759
777
  }
760
778
  return parsed;
761
779
  } catch (err) {
762
- if (err.code === "ENOENT") {
780
+ if (isNodeError(err) && err.code === "ENOENT") {
763
781
  return {
764
782
  version: 1,
765
783
  instances: /* @__PURE__ */ new Map(),
@@ -795,7 +813,7 @@ var PersistentProcessRegistry = class {
795
813
  this.lockPath = path.join(homeDir, LOCKFILE);
796
814
  this.baseRegistry = baseRegistry ?? getProcessRegistry();
797
815
  this.ensureDirectory().catch((err) => {
798
- console.error("PersistentProcessRegistry: failed to create .wrongstack dir", err);
816
+ emitStructuredLog("warn", "process_registry.dir_create_failed", "PersistentProcessRegistry: failed to create .wrongstack directory", err);
799
817
  });
800
818
  }
801
819
  async ensureDirectory() {
@@ -803,7 +821,7 @@ var PersistentProcessRegistry = class {
803
821
  try {
804
822
  await fs.mkdir(dir, { recursive: true });
805
823
  } catch (err) {
806
- if (err.code !== "EEXIST") throw err;
824
+ if (!isNodeError(err) || err.code !== "EEXIST") throw err;
807
825
  }
808
826
  }
809
827
  /**
@@ -883,6 +901,7 @@ var PersistentProcessRegistry = class {
883
901
  try {
884
902
  const data = await readRegistryFile(this.registryPath);
885
903
  data.instances.set(String(entry.pid), entry);
904
+ const child = null;
886
905
  this.baseRegistry.register({
887
906
  pid: entry.pid,
888
907
  name: entry.name,
@@ -890,8 +909,7 @@ var PersistentProcessRegistry = class {
890
909
  startedAt: entry.startedAt,
891
910
  sessionId: entry.sessionId,
892
911
  protected: entry.protected,
893
- child: null
894
- // Main process has no child handle
912
+ child
895
913
  });
896
914
  await writeRegistryFile(this.registryPath, data);
897
915
  } finally {
@@ -939,7 +957,7 @@ var PersistentProcessRegistry = class {
939
957
  data.lastCleanup = now;
940
958
  await writeRegistryFile(this.registryPath, data);
941
959
  } catch (err) {
942
- console.error("PersistentProcessRegistry: sync failed", err);
960
+ emitStructuredLog("warn", "process_registry.sync_failed", "PersistentProcessRegistry: sync failed", err);
943
961
  } finally {
944
962
  await release();
945
963
  }
@@ -960,7 +978,11 @@ var PersistentProcessRegistry = class {
960
978
  if (process.platform !== "win32") {
961
979
  process.kill(entry.pid, 0);
962
980
  } else {
963
- console.log(`PersistentProcessRegistry: checking stale pid ${entry.pid} (${age}ms old)`);
981
+ emitStructuredLog(
982
+ "debug",
983
+ "process_registry.stale_pid_check",
984
+ `PersistentProcessRegistry: checking stale pid ${entry.pid} (${age}ms old)`
985
+ );
964
986
  }
965
987
  } catch {
966
988
  stalePids.push(_pidStr);
@@ -974,7 +996,7 @@ var PersistentProcessRegistry = class {
974
996
  await writeRegistryFile(this.registryPath, data);
975
997
  }
976
998
  } catch (err) {
977
- console.error("PersistentProcessRegistry: cleanup failed", err);
999
+ emitStructuredLog("warn", "process_registry.cleanup_failed", "PersistentProcessRegistry: cleanup failed", err);
978
1000
  } finally {
979
1001
  await release();
980
1002
  }
@@ -1197,7 +1219,7 @@ async function isKillProtected(kill) {
1197
1219
  const entries = await getProtectedEntries();
1198
1220
  const killNameLower = kill.name.toLowerCase();
1199
1221
  for (const entry of entries) {
1200
- if (entry.name && entry.name.toLowerCase().includes(killNameLower)) {
1222
+ if (entry.name?.toLowerCase().includes(killNameLower)) {
1201
1223
  return true;
1202
1224
  }
1203
1225
  }