@hmharness/cli 0.14.6 → 0.14.9

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.
@@ -1,27 +1,44 @@
1
1
  export declare const DEFAULT_WEB_PORT = 7788;
2
2
  export declare function readWebPid(): number;
3
- /** cheap probe: does OUR server answer on the port (not just any listener)?
4
- * Accepts ANY hmh /api/state shape - a daemon from an older build still
5
- * serves the UI, and rejecting it made the TUI auto-link spawn a fresh
6
- * daemon that died on EADDRINUSE every startup (the "lost setting" bug). */
3
+ /** Probe the SERVED web server: is it up, and what version does the RUNNING
4
+ * process report? The daemon self-reports its own code version via
5
+ * /api/state.daemonVersion — this is the truth the staleness check uses.
6
+ * An older daemon without the field reports version '' (file-based fallback
7
+ * then applies). Accepts ANY hmh /api/state shape — a daemon from an older
8
+ * build still serves the UI, and rejecting it made the TUI auto-link spawn
9
+ * a fresh daemon that died on EADDRINUSE every startup ("lost setting"). */
10
+ export interface WebProbe {
11
+ up: boolean;
12
+ version: string;
13
+ }
14
+ export declare function probeWeb(port: number): Promise<WebProbe>;
15
+ /** cheap probe: does OUR server answer on the port (not just any listener)? */
7
16
  export declare function hmhWebUp(port: number): Promise<boolean>;
8
- export declare function stopWebDaemon(): boolean;
17
+ /** Wait (bounded) until nothing answers on the port — used after evicting a
18
+ * stale listener so the respawn does not die on EADDRINUSE. */
19
+ export declare function waitPortFree(port: number, maxMs?: number): Promise<boolean>;
20
+ export declare function stopWebDaemon(port?: number): boolean;
9
21
  /**
10
22
  * Idempotent: if our web UI is already up (pid file alive, or the port
11
- * answers as hmh), keep it; otherwise spawn it and wait up to ~6s for
12
- * readiness. Returns true when the web UI is usable.
23
+ * answers as hmh) at the CURRENT CLI version, keep it; otherwise evict and
24
+ * respawn, waiting up to ~6s for readiness. Returns true when the web UI is
25
+ * usable.
13
26
  *
14
- * Version check: a daemon spawned by an older CLI keeps serving old code
15
- * (the user sees missing features for days). We compare the recorded daemon
16
- * version with the current CLI and restart on mismatch.
27
+ * Staleness = what the SERVED process reports (/api/state.daemonVersion),
28
+ * not the web.version file: a file is overwritten by every spawn (even one
29
+ * that then died on EADDRINUSE) while the OLD listener keeps the port, so a
30
+ * file-based comparison can silently bless a stale daemon. Old daemons
31
+ * without the field report '' -> file-based comparison as the fallback.
17
32
  */
18
33
  export declare function ensureWebDaemon(port?: number, entry?: string): Promise<boolean>;
19
- /** Report whether the running daemon matches the current CLI version. */
20
- export declare function webDaemonStale(): {
34
+ /** Report whether the running daemon matches the current CLI version.
35
+ * Prefers what the SERVED process reports; falls back to the recorded file
36
+ * version for daemons that predate the self-reporting field. */
37
+ export declare function webDaemonStale(port?: number): Promise<{
21
38
  stale: boolean;
22
39
  daemon: string;
23
40
  cli: string;
24
- };
41
+ }>;
25
42
  export declare function startWebDaemon(port?: number, entry?: string): {
26
43
  already: boolean;
27
44
  pid: number;
@@ -35,22 +35,22 @@ function alive(pid) {
35
35
  return false;
36
36
  }
37
37
  }
38
- /** cheap probe: does OUR server answer on the port (not just any listener)?
39
- * Accepts ANY hmh /api/state shape - a daemon from an older build still
40
- * serves the UI, and rejecting it made the TUI auto-link spawn a fresh
41
- * daemon that died on EADDRINUSE every startup (the "lost setting" bug). */
42
- export async function hmhWebUp(port) {
38
+ export async function probeWeb(port) {
43
39
  try {
44
40
  const r = await fetch(`http://127.0.0.1:${port}/api/state`, { signal: AbortSignal.timeout(1500) });
45
41
  if (!r.ok)
46
- return false;
42
+ return { up: false, version: '' };
47
43
  const d = await r.json();
48
- return !!d && typeof d === 'object';
44
+ return { up: !!d && typeof d === 'object', version: typeof d.daemonVersion === 'string' ? d.daemonVersion : '' };
49
45
  }
50
46
  catch {
51
- return false;
47
+ return { up: false, version: '' };
52
48
  }
53
49
  }
50
+ /** cheap probe: does OUR server answer on the port (not just any listener)? */
51
+ export async function hmhWebUp(port) {
52
+ return (await probeWeb(port)).up;
53
+ }
54
54
  /** Windows-first: find the PID LISTENING on 127.0.0.1:<port> via netstat.
55
55
  * Used to reclaim a port held by an orphaned/old daemon whose pid file is
56
56
  * stale - `hmh web stop` must be able to evict it, or restarts never heal. */
@@ -68,7 +68,18 @@ function portOwnerPid(port) {
68
68
  catch { /* netstat unavailable - give up quietly */ }
69
69
  return 0;
70
70
  }
71
- export function stopWebDaemon() {
71
+ /** Wait (bounded) until nothing answers on the port — used after evicting a
72
+ * stale listener so the respawn does not die on EADDRINUSE. */
73
+ export async function waitPortFree(port, maxMs = 4000) {
74
+ const t0 = Date.now();
75
+ while (Date.now() - t0 < maxMs) {
76
+ if (portOwnerPid(port) === 0)
77
+ return true;
78
+ await new Promise((r) => setTimeout(r, 300));
79
+ }
80
+ return portOwnerPid(port) === 0;
81
+ }
82
+ export function stopWebDaemon(port = DEFAULT_WEB_PORT) {
72
83
  const pid = readWebPid();
73
84
  let killed = false;
74
85
  if (pid && alive(pid)) {
@@ -87,7 +98,7 @@ export function stopWebDaemon() {
87
98
  }
88
99
  catch { /* absent */ }
89
100
  // stale pid file but the port is still held (orphaned/old daemon): evict
90
- const owner = portOwnerPid(DEFAULT_WEB_PORT);
101
+ const owner = portOwnerPid(port);
91
102
  if (owner && owner !== pid) {
92
103
  try {
93
104
  spawn('taskkill', ['/PID', String(owner), '/T', '/F'], { windowsHide: true });
@@ -138,28 +149,35 @@ function cliVersion() {
138
149
  }
139
150
  /**
140
151
  * Idempotent: if our web UI is already up (pid file alive, or the port
141
- * answers as hmh), keep it; otherwise spawn it and wait up to ~6s for
142
- * readiness. Returns true when the web UI is usable.
152
+ * answers as hmh) at the CURRENT CLI version, keep it; otherwise evict and
153
+ * respawn, waiting up to ~6s for readiness. Returns true when the web UI is
154
+ * usable.
143
155
  *
144
- * Version check: a daemon spawned by an older CLI keeps serving old code
145
- * (the user sees missing features for days). We compare the recorded daemon
146
- * version with the current CLI and restart on mismatch.
156
+ * Staleness = what the SERVED process reports (/api/state.daemonVersion),
157
+ * not the web.version file: a file is overwritten by every spawn (even one
158
+ * that then died on EADDRINUSE) while the OLD listener keeps the port, so a
159
+ * file-based comparison can silently bless a stale daemon. Old daemons
160
+ * without the field report '' -> file-based comparison as the fallback.
147
161
  */
148
162
  export async function ensureWebDaemon(port = DEFAULT_WEB_PORT, entry = process.argv[1]) {
149
163
  const pid = readWebPid();
150
- const daemonV = daemonVersion();
151
164
  const myV = cliVersion();
152
- const stale = daemonV !== '' && myV !== '' && daemonV !== myV;
165
+ const served = await probeWeb(port);
166
+ // served version wins; '' means an old daemon without the field — fall back
167
+ // to the recorded file version for the comparison
168
+ const daemonV = served.version || daemonVersion();
169
+ const stale = myV !== '' && daemonV !== '' && daemonV !== myV;
153
170
  if (!stale) {
154
171
  if (pid && alive(pid))
155
172
  return true;
156
- if (await hmhWebUp(port))
173
+ if (served.up)
157
174
  return true;
158
175
  }
159
176
  else {
160
- // stale daemon: kill it (it may hold the pid OR just the port)
161
- stopWebDaemon();
162
- await new Promise((r) => setTimeout(r, 600));
177
+ // stale daemon: kill it (it may hold the pid OR just the port) and wait
178
+ // for the port to free so the respawn cannot die on EADDRINUSE
179
+ stopWebDaemon(port);
180
+ await waitPortFree(port);
163
181
  }
164
182
  spawnWebDaemon(port, entry);
165
183
  for (let i = 0; i < 12; i++) {
@@ -169,10 +187,13 @@ export async function ensureWebDaemon(port = DEFAULT_WEB_PORT, entry = process.a
169
187
  }
170
188
  return false;
171
189
  }
172
- /** Report whether the running daemon matches the current CLI version. */
173
- export function webDaemonStale() {
174
- const d = daemonVersion();
190
+ /** Report whether the running daemon matches the current CLI version.
191
+ * Prefers what the SERVED process reports; falls back to the recorded file
192
+ * version for daemons that predate the self-reporting field. */
193
+ export async function webDaemonStale(port = DEFAULT_WEB_PORT) {
175
194
  const c = cliVersion();
195
+ const served = await probeWeb(port);
196
+ const d = served.version || daemonVersion();
176
197
  return { stale: d !== '' && c !== '' && d !== c, daemon: d, cli: c };
177
198
  }
178
199
  export function startWebDaemon(port = DEFAULT_WEB_PORT, entry = process.argv[1]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/cli",
3
- "version": "0.14.6",
3
+ "version": "0.14.9",
4
4
  "description": "hmharness command line: one-shot tasks, an interactive REPL, a fullscreen TUI, the web frontend, and direct tool invocation.",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",
@@ -43,13 +43,13 @@
43
43
  "build": "tsc -p tsconfig.build.json"
44
44
  },
45
45
  "dependencies": {
46
- "@hmharness/kernel": "0.9.0",
46
+ "@hmharness/kernel": "0.9.1",
47
47
  "@hmharness/observability": "0.7.0",
48
- "@hmharness/evolution": "0.14.5",
48
+ "@hmharness/evolution": "0.14.9",
49
49
  "@hmharness/domain-harmony": "0.13.5",
50
50
  "@hmharness/domain-ops": "0.8.0",
51
- "@hmharness/agent": "0.14.6",
51
+ "@hmharness/agent": "0.14.9",
52
52
  "@hmharness/evaluation": "0.8.0",
53
- "@hmharness/web": "0.8.2"
53
+ "@hmharness/web": "0.9.1"
54
54
  }
55
55
  }