@chatpanel/gateway 0.6.101 → 0.6.102

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/gateway",
3
- "version": "0.6.101",
3
+ "version": "0.6.102",
4
4
  "description": "Local privacy gateway — redacts PII out of OpenAI/Anthropic API traffic before it reaches a model, then restores it in the reply. Point opencode, codex, aider, Claude Code, etc. at it.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -112,6 +112,10 @@ export async function ensureBridge(cfg, {
112
112
  waitForSiblingMs = 2500,
113
113
  now = Date.now,
114
114
  setTimer = setTimeout,
115
+ // The watch on an ADOPTED bridge: its own timer seam (a test's immediate `setTimer` would
116
+ // spin it), unref'd so a pending watch never keeps the process alive.
117
+ setWatch = (fn, ms) => { const t = setTimeout(fn, ms); t.unref?.(); return t; },
118
+ watchEveryMs = 10_000,
115
119
  } = {}) {
116
120
  const url = String(cfg?.bridge?.url || DEFAULT_BRIDGE_URL).replace(/\/$/, '');
117
121
  const state = { mode: 'off', version: null, pid: null, restarts: 0, why: '', child: null, stopping: false, backoff: 0 };
@@ -131,12 +135,43 @@ export async function ensureBridge(cfg, {
131
135
  const plan = planBridge({ managed, cfgUrl: url, healthy, standalone, embeddedVersion });
132
136
 
133
137
  if (plan.action === 'off') { settle('off', plan.why); return controller(); }
134
- if (plan.action === 'adopt') { settle('adopted', plan.why, { version: healthy.version }); return controller(); }
135
138
 
136
- const spec = plan.action === 'spawn-embedded'
137
- ? { ...emb, env: { CHATPANEL_BRIDGE_EMBEDDED: '1' }, mode: 'embedded', version: embeddedVersion }
138
- : { program: plan.path, args: [], env: {}, mode: 'standalone', version: standalone?.version || null };
139
+ // What WE would run, decided once and kept: an adopted bridge that goes away is replaced
140
+ // by this without re-planning from scratch.
141
+ const specFor = (p) => (p.action === 'spawn-embedded'
142
+ ? { ...emb, env: { CHATPANEL_BRIDGE_EMBEDDED: '1' }, mode: 'embedded', version: p.version || embeddedVersion }
143
+ : { program: p.path, args: [], env: {}, mode: 'standalone', version: standalone?.version || null });
144
+ let spec = plan.action === 'adopt' ? null : specFor(plan);
139
145
  let startedAt = 0;
146
+ let watch = null;
147
+ let misses = 0;
148
+
149
+ // AN ADOPTED BRIDGE IS WATCHED, NOT TRUSTED FOREVER. The gateway carries the bridge: when
150
+ // the one it adopted goes away — the desktop app that ran it quit, an old login unit was
151
+ // removed, a standalone was stopped — the port is quiet and every agent turn fails with
152
+ // "bridge unreachable" until someone restarts the gateway. Two missed probes (a refused
153
+ // port counts at once) and ours starts.
154
+ const adopt = (why, ver) => {
155
+ settle('adopted', why, { version: ver || null });
156
+ misses = 0;
157
+ const tick = async () => {
158
+ if (state.stopping || state.mode !== 'adopted') return;
159
+ const h = await probe(url);
160
+ if (h) { misses = 0; state.version = h.version || state.version; watch = setWatch(tick, watchEveryMs); return; }
161
+ misses += 1;
162
+ if (misses < 2) { watch = setWatch(tick, Math.min(watchEveryMs, 3000)); return; }
163
+ if (!spec) {
164
+ const ev = embeddedVersion || version(emb.program, emb.args);
165
+ const p = planBridge({ managed, cfgUrl: url, healthy: null, standalone, embeddedVersion: ev });
166
+ if (p.action === 'off') { settle('off', `the adopted bridge went away and ${p.why}`); return; }
167
+ spec = specFor({ ...p, version: ev });
168
+ }
169
+ log(` bridge : the adopted bridge at ${url} went away — ${spec.mode === 'embedded' ? `starting the embedded bridge (v${spec.version || '?'})` : `starting the installed bridge (v${spec.version || '?'})`}`);
170
+ settle(spec.mode, `the adopted bridge went away; ${spec.mode} bridge started in its place`, { version: spec.version });
171
+ start();
172
+ };
173
+ watch = setWatch(tick, watchEveryMs);
174
+ };
140
175
 
141
176
  const start = () => {
142
177
  if (state.stopping) return;
@@ -155,7 +190,7 @@ export async function ensureBridge(cfg, {
155
190
  // The port may have been taken by a bridge someone else started while ours came up —
156
191
  // that is not a failure to restart from, it is a bridge to adopt.
157
192
  const other = await probe(url);
158
- if (other) { settle('adopted', `a bridge already answers at ${url} (v${other.version || '?'}) — ours stepped aside`, { version: other.version }); return; }
193
+ if (other) { adopt(`a bridge already answers at ${url} (v${other.version || '?'}${other.managedBy ? `, run by ${other.managedBy}` : ''}) — ours stepped aside`, other.version); return; }
159
194
  if (now() - startedAt > STABLE_MS) state.backoff = 0;
160
195
  const delay = BACKOFF_MS[Math.min(state.backoff, BACKOFF_MS.length - 1)];
161
196
  state.backoff += 1; state.restarts += 1;
@@ -163,6 +198,7 @@ export async function ensureBridge(cfg, {
163
198
  setTimer(start, delay);
164
199
  });
165
200
  };
201
+ if (plan.action === 'adopt') { adopt(plan.why, healthy.version); return controller(); }
166
202
  settle(spec.mode, plan.why, { version: spec.version });
167
203
  start();
168
204
  return controller();
@@ -170,7 +206,7 @@ export async function ensureBridge(cfg, {
170
206
  function controller() {
171
207
  return {
172
208
  status: () => ({ mode: state.mode, version: state.version, pid: state.pid, restarts: state.restarts, why: state.why }),
173
- stop: () => { state.stopping = true; if (state.child && !state.child.killed) { try { state.child.kill('SIGTERM'); } catch { /* gone */ } } },
209
+ stop: () => { state.stopping = true; if (watch) { clearTimeout(watch); watch = null; } if (state.child && !state.child.killed) { try { state.child.kill('SIGTERM'); } catch { /* gone */ } } },
174
210
  };
175
211
  }
176
212
  }
package/src/server.js CHANGED
@@ -63,7 +63,7 @@ import * as openai from './openai.js';
63
63
  import * as responses from './responses.js';
64
64
  import * as anthropic from './anthropic.js';
65
65
 
66
- export const VERSION = '0.6.101';
66
+ export const VERSION = '0.6.102';
67
67
 
68
68
  // WARM search tier — SQLite + FTS5 record store (falls back to an encrypted-JSON
69
69
  // store if SQLite can't load), fed by the extension's ingest sync + backup-ingest.