@chatpanel/bridge 0.11.9 → 0.11.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatpanel/bridge",
3
- "version": "0.11.9",
3
+ "version": "0.11.13",
4
4
  "type": "module",
5
5
  "description": "Local bridge that exposes the AI coding agents installed on your machine — Claude Code (CLI), Codex (CLI), and Antigravity CLI (formerly Gemini CLI, which remains available for business/enterprise) — to the ChatPanel Chrome extension over a localhost SSE endpoint. Bring your own agent.",
6
6
  "keywords": [
package/src/env.js CHANGED
@@ -273,6 +273,52 @@ function stripBom(s) {
273
273
 
274
274
  // Version managers install CLIs under versioned bin dirs that a lazy-loaded
275
275
  // shell (nvm/fnm) doesn't export into a non-interactive service PATH. Add them.
276
+ /**
277
+ * The node version nvm would actually give you, and only that one.
278
+ *
279
+ * WHY THIS EXISTS. PATH discovery asks a LOGIN shell, which never reads .zshrc — and .zshrc
280
+ * is where nvm lives. So the learned PATH has no nvm in it, and the hardcoded fallbacks
281
+ * below take over. Those start with /opt/homebrew/bin, so a Homebrew copy of an agent CLI
282
+ * beat the one the user's terminal uses.
283
+ *
284
+ * One machine had Claude Code 2.1.268 under nvm and 2.1.175 under Homebrew. Every turn
285
+ * through the bridge answered "Claude Code 2.1.175 does not support this model" to a user
286
+ * who had just updated Claude Code — twice.
287
+ *
288
+ * `versionManagerBins` cannot fix that on its own: it lists EVERY installed version, in
289
+ * directory order, so preferring it wholesale would pick an arbitrary old node. This picks
290
+ * the one nvm itself would: the `default` alias if it names a version, else the highest
291
+ * installed. Pure filesystem reads — no shell, so nothing can block startup.
292
+ */
293
+ export function nvmCurrentBin(home = os.homedir(), env = process.env) {
294
+ const root = path.join(env.NVM_DIR || path.join(home, '.nvm'), 'versions', 'node');
295
+ let versions;
296
+ try {
297
+ versions = readdirSync(root).filter((v) => /^v\d+\./.test(v));
298
+ } catch {
299
+ return null; // no nvm on this machine
300
+ }
301
+ if (!versions.length) return null;
302
+
303
+ const byVersion = (a, b) => {
304
+ const pa = a.slice(1).split('.').map(Number);
305
+ const pb = b.slice(1).split('.').map(Number);
306
+ for (let i = 0; i < 3; i += 1) if ((pa[i] || 0) !== (pb[i] || 0)) return (pb[i] || 0) - (pa[i] || 0);
307
+ return 0;
308
+ };
309
+
310
+ // The `default` alias is what a new shell resolves to. It may name a version directly
311
+ // ("24.14.0"), or point at another alias ("lts/*"), which we do not chase — the highest
312
+ // installed is a better guess than a wrong one.
313
+ try {
314
+ const alias = readFileSync(path.join(env.NVM_DIR || path.join(home, '.nvm'), 'alias', 'default'), 'utf8').trim();
315
+ const want = alias.startsWith('v') ? alias : `v${alias}`;
316
+ if (versions.includes(want)) return path.join(root, want, 'bin');
317
+ } catch { /* no alias file — fall through */ }
318
+
319
+ return path.join(root, versions.sort(byVersion)[0], 'bin');
320
+ }
321
+
276
322
  function versionManagerBins(home) {
277
323
  const bins = [];
278
324
  const tryDir = (dir, sub) => {
@@ -501,7 +547,12 @@ export function enrichPath() {
501
547
  return;
502
548
  }
503
549
 
550
+ const nvmBin = nvmCurrentBin(home);
504
551
  const common = [
552
+ // BEFORE the package-manager dirs. A version manager PREPENDS itself to PATH in the
553
+ // user's shell, so its copy is the one their terminal runs — and the whole purpose of
554
+ // this list is to reproduce what the terminal would do.
555
+ ...(nvmBin ? [nvmBin] : []),
505
556
  '/opt/homebrew/bin',
506
557
  '/opt/homebrew/sbin',
507
558
  '/usr/local/bin',
@@ -523,6 +574,12 @@ export function enrichPath() {
523
574
  let shellPath = '';
524
575
  try {
525
576
  const shell = process.env.SHELL || '/bin/zsh';
577
+ // `-lc`, NOT `-ilc`, and deliberately: an INTERACTIVE shell can block on a prompt and
578
+ // hang the daemon's own startup. tests/env-startup.test.mjs holds that line.
579
+ //
580
+ // The cost is that a login shell does not read .zshrc/.bashrc, which is where nvm and
581
+ // friends install themselves — so this answer arrives WITHOUT the version manager, and
582
+ // the merge below has to supply it. See nvmCurrentBin().
526
583
  const r = spawnSync(shell, ['-lc', 'command -p echo "$PATH"'], {
527
584
  encoding: 'utf8',
528
585
  timeout: 4000,
@@ -534,6 +591,17 @@ export function enrichPath() {
534
591
 
535
592
  const seen = new Set();
536
593
  const merged = [
594
+ // THE VERSION MANAGER GOES FIRST, ahead even of the login shell's own answer.
595
+ //
596
+ // That looks aggressive until you see why the shell's answer is incomplete: `-lc` never
597
+ // reads .zshrc, which is where nvm installs itself, so the PATH we learn is the user's
598
+ // MINUS their version manager — while still containing /opt/homebrew/bin from .zprofile.
599
+ // Merging it first therefore hands Homebrew the win over a tool the user manages with
600
+ // nvm, which is the opposite of what their terminal does: nvm PREPENDS.
601
+ //
602
+ // So we put it back where nvm would have. `nvmCurrentBin` resolves the one version nvm
603
+ // itself would pick, from the filesystem, with no shell to block on.
604
+ ...(nvmBin ? [nvmBin] : []),
537
605
  ...(shellPath ? shellPath.split(':') : []),
538
606
  ...(process.env.PATH ? process.env.PATH.split(path.delimiter) : []),
539
607
  ...common,
package/src/server.js CHANGED
@@ -67,7 +67,7 @@ import {
67
67
  // Hardcoded (not read from package.json) so it survives Bun's single-file
68
68
  // --compile, where package.json isn't on a readable FS. CI fails the publish if
69
69
  // this drifts from package.json, so the two can't silently diverge.
70
- const VERSION = '0.11.9';
70
+ const VERSION = '0.11.13';
71
71
  const HOST = process.env.CHATPANEL_BRIDGE_HOST || '127.0.0.1';
72
72
  const PORT = Number(process.env.CHATPANEL_BRIDGE_PORT) || 4319;
73
73
 
@@ -445,6 +445,9 @@ async function handleHealth(res) {
445
445
  json(res, 200, {
446
446
  ok: true, version: VERSION, agents, update,
447
447
  workspace: DEFAULT_WORKSPACE,
448
+ // WHO STARTED THIS PROCESS — additive. ChatPanel Desktop sets CHATPANEL_MANAGED_BY=desktop on
449
+ // the login service it registers; a client can then say so instead of offering install.sh.
450
+ ...(process.env.CHATPANEL_MANAGED_BY ? { managedBy: String(process.env.CHATPANEL_MANAGED_BY).slice(0, 32) } : {}),
448
451
  ...(skills ? { skills } : {}),
449
452
  });
450
453
  }
package/src/update.js CHANGED
@@ -24,6 +24,18 @@ const LATEST_API = `https://api.github.com/repos/${REPO}/releases/latest`;
24
24
  const CHECK_EVERY_MS = 6 * 60 * 60 * 1000; // 6h
25
25
  const CACHE = path.join(os.homedir(), '.chatpanel', 'update-check.json');
26
26
  const UA = { 'User-Agent': 'chatpanel-bridge-updater' };
27
+ // THIS CHECK RUNS INSIDE /health. Every client heartbeat — the extension, the desktop's
28
+ // supervisor, the gateway's model list — waits on it. An un-timed fetch to GitHub therefore
29
+ // made every one of them wait for a VPN to give up on api.github.com, and the desktop gives a
30
+ // health answer 2.5s before calling the bridge dead. So: a short timeout, and after a failure
31
+ // no retry for a while — a failed check that was re-attempted on every call kept the stall
32
+ // going for as long as the network was bad.
33
+ const FETCH_TIMEOUT_MS = Math.max(500, Number(process.env.CHATPANEL_UPDATE_TIMEOUT_MS) || 5000);
34
+ const RETRY_AFTER_FAILURE_MS = 10 * 60 * 1000; // 10min
35
+ let lastFailure = { at: 0, error: '' };
36
+
37
+ /** Test seam: forget the last failure so the next unforced check reaches the network. */
38
+ export function resetUpdateBackoff() { lastFailure = { at: 0, error: '' }; }
27
39
 
28
40
  // Release asset name for THIS platform (matches release-binaries.yml outputs).
29
41
  // macOS publishes arm64 only; Intel Macs use `npx` (managed → no self-update).
@@ -95,21 +107,41 @@ export async function checkForUpdate(current, { force = false } = {}) {
95
107
  if (!force && cache && Date.now() - cache.checkedAt < CHECK_EVERY_MS) {
96
108
  latest = cache.latest;
97
109
  assetUrl = cache.assetUrl;
110
+ } else if (!force && lastFailure.at && Date.now() - lastFailure.at < RETRY_AFTER_FAILURE_MS) {
111
+ // The network said no a moment ago. Asking again on every health poll would make every
112
+ // poll as slow as the failure; the answer is what it was, and says so.
113
+ error = lastFailure.error;
114
+ fallBackToCache();
98
115
  } else {
116
+ // An explicit timer, not AbortSignal.timeout: that one's timer is unref'd, so in a
117
+ // process with nothing else to do (a test, a CLI one-shot) the loop can exit before it
118
+ // fires and the fetch is neither answered nor abandoned.
119
+ const ctl = new AbortController();
120
+ const timer = setTimeout(() => ctl.abort(), FETCH_TIMEOUT_MS);
99
121
  try {
100
- const res = await fetch(LATEST_API, { headers: { Accept: 'application/vnd.github+json', ...UA } });
122
+ const res = await fetch(LATEST_API, {
123
+ headers: { Accept: 'application/vnd.github+json', ...UA },
124
+ signal: ctl.signal,
125
+ });
101
126
  if (res.ok) {
102
127
  const data = await res.json();
103
128
  latest = parseVersion(data.tag_name) || parseVersion(data.name);
104
129
  assetUrl = want ? (data.assets || []).find((a) => a.name === want)?.browser_download_url || null : null;
105
130
  await writeCache({ checkedAt: Date.now(), latest, assetUrl });
131
+ lastFailure = { at: 0, error: '' };
106
132
  } else {
107
133
  error = res.status === 403 ? 'GitHub rate limit (60/hour per IP) — try again later' : `HTTP ${res.status}`;
134
+ lastFailure = { at: Date.now(), error };
108
135
  fallBackToCache();
109
136
  }
110
137
  } catch (e) {
111
- error = e?.message || String(e);
138
+ error = ctl.signal.aborted
139
+ ? `GitHub did not answer within ${Math.round(FETCH_TIMEOUT_MS / 1000)}s`
140
+ : e?.message || String(e);
141
+ lastFailure = { at: Date.now(), error };
112
142
  fallBackToCache();
143
+ } finally {
144
+ clearTimeout(timer);
113
145
  }
114
146
  }
115
147
  const updateAvailable = !!latest && cmp(latest, current) > 0;