@caveman-ai/cli 1.2.4 → 1.2.5

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
@@ -11039,7 +11039,13 @@ async function nativeHook(argv) {
11039
11039
  entry.tool_name = toolName;
11040
11040
  if (cwd)
11041
11041
  entry.cwd_sha256 = `sha256:${createHash("sha256").update(cwd).digest("hex")}`;
11042
- if (normalizedEvent === "SessionStart") {
11042
+ // SessionStart revives a missing local proxy, but native routing points every
11043
+ // LATER turn of the session at that proxy too, and a wrap-owned instance
11044
+ // idle-exits ~30m after its wrap dies. A plain (unwrapped) session then
11045
+ // hard-fails with ConnectionRefused on its next prompt, with nothing left to
11046
+ // restart the proxy. The port check below makes the revive idempotent, so run
11047
+ // it for the mid-session events that reach the full CLI as well.
11048
+ if (normalizedEvent === "SessionStart" || normalizedEvent === "UserPromptSubmit" || normalizedEvent === "PostCompact") {
11043
11049
  try {
11044
11050
  const opts = defaultWrapOptions();
11045
11051
  const gw = gatewayURL();
@@ -19,6 +19,45 @@ const TASK_STOPWORDS = new Set(["about", "after", "again", "agent", "before", "b
19
19
  function caveHome() {
20
20
  return process.env.CAVEMAN_HOME ?? join(homedir(), ".caveman");
21
21
  }
22
+ // localGatewayEndpoint resolves the gateway target and returns its host/port
23
+ // only when it is a loopback proxy — the only case where a dead port means
24
+ // "the local proxy needs restarting". Managed gateways (CAVE_GATEWAY_URL /
25
+ // persisted gatewayUrl off-loopback) return undefined and never pay a
26
+ // per-prompt delegate spawn. Default mirrors the full CLI's PROXY_ADDR.
27
+ function localGatewayEndpoint() {
28
+ let gw = process.env.CAVE_GATEWAY_URL;
29
+ if (typeof gw !== "string" || !gw.trim()) {
30
+ try {
31
+ gw = object(JSON.parse(readFileSync(join(homedir(), ".caveman-cloud", "config.json"), "utf8"))).gatewayUrl;
32
+ }
33
+ catch { /* local default */ }
34
+ }
35
+ if (typeof gw !== "string" || !gw.trim())
36
+ return { host: "127.0.0.1", port: 8787 };
37
+ try {
38
+ const url = new URL(gw);
39
+ const host = url.hostname;
40
+ if (host !== "127.0.0.1" && host !== "localhost" && host !== "::1" && host !== "[::1]")
41
+ return undefined;
42
+ return { host: host === "[::1]" ? "::1" : host, port: url.port ? Number(url.port) : 8787 };
43
+ }
44
+ catch {
45
+ return { host: "127.0.0.1", port: 8787 };
46
+ }
47
+ }
48
+ // gatewayListening is the same signal the full CLI's revive block keys on
49
+ // (portListening): a loopback TCP probe, ~1ms either way. Gating the delegate
50
+ // on the PORT rather than the runtime socket matters — record pass-through,
51
+ // noProxy, or a slow runtime all leave the socket dead while the proxy is up,
52
+ // and must not pay a full-CLI spawn on every prompt.
53
+ function gatewayListening(endpoint) {
54
+ return new Promise((settle) => {
55
+ const socket = netConnect({ host: endpoint.host, port: endpoint.port, timeout: 250 });
56
+ socket.on("connect", () => { socket.destroy(); settle(true); });
57
+ socket.on("error", () => settle(false));
58
+ socket.on("timeout", () => { socket.destroy(); settle(false); });
59
+ });
60
+ }
22
61
  function object(value) {
23
62
  return value && typeof value === "object" && !Array.isArray(value) ? value : {};
24
63
  }
@@ -407,7 +446,9 @@ function delegateToFullCLI(raw, agent) {
407
446
  process.stdout.write(result.stdout);
408
447
  if (result.stderr?.length)
409
448
  process.stderr.write(result.stderr);
449
+ return true;
410
450
  }
451
+ return false;
411
452
  }
412
453
  async function main() {
413
454
  const agentArg = process.argv[2] === "native-hook" ? process.argv[3] : process.argv[2];
@@ -452,8 +493,24 @@ async function main() {
452
493
  entry.cwd_sha256 = `sha256:${createHash("sha256").update(cwd).digest("hex")}`;
453
494
  const runtimeRequest = sessionID && eventName !== "Unknown" ? request(agent, eventName, sessionID, event) : undefined;
454
495
  const response = runtimeRequest ? await callRuntime(runtimeRequest) : undefined;
455
- if (!response)
496
+ if (!response) {
497
+ // A dead loopback gateway PORT at prompt time is the SessionStart revive
498
+ // signal arriving mid-session: native routing still points this session's
499
+ // next request at the local proxy, but a wrap-owned proxy idle-exits after
500
+ // its wrap dies and a plain session has no other way to restart it — the
501
+ // turn hard-fails with ConnectionRefused. Delegate this one event to the
502
+ // full CLI, which revives the proxy exactly like SessionStart (and records
503
+ // the event itself). The port probe — not the dead runtime socket — is the
504
+ // trigger, so proxy-up/socket-down states (record pass-through, noProxy,
505
+ // slow runtime) never pay the spawn; a failed delegate falls through so
506
+ // the event is still recorded.
507
+ if (eventName === "UserPromptSubmit") {
508
+ const endpoint = localGatewayEndpoint();
509
+ if (endpoint && !(await gatewayListening(endpoint)) && delegateToFullCLI(raw, agent))
510
+ return;
511
+ }
456
512
  fallback(entry);
513
+ }
457
514
  if (eventName === "SessionEnd" && response?.message)
458
515
  process.stderr.write(`${response.message}\n`);
459
516
  const mode = policyMode();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caveman-ai/cli",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "description": "Caveman CLI — wrap coding agents with recoverable local context compression and truthful metering (`caveman claude`), then drive Caveman Cloud from terminal. Compression and metering run in companion Go binaries: run `caveman setup` to see install state. Local savings stay inferred until verified.",
5
5
  "license": "MIT",
6
6
  "type": "module",