llm-switcher 1.1.1 → 1.1.2

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/README.md CHANGED
@@ -191,7 +191,15 @@ flowchart LR
191
191
 
192
192
  ---
193
193
 
194
- ## Changes in this update
194
+ ## Changes in 1.1.2
195
+
196
+ - **npm package.** Install with `npm install -g llm-switcher` and run `switch`. An npm install keeps its data in `~/.llm-switcher`, so an upgrade does not erase your configuration. A git checkout keeps its data next to the code, as before.
197
+ - **Contract lab.** The gateway can send a small sample of complete exchanges to an [intact](https://github.com/louisphamdev/intact) server, which finds fields that the converter loses. It is off by default. See "Contract lab" below.
198
+ - **macOS.** `blindfold/make-certs.sh` now runs with LibreSSL, the default `openssl` on macOS.
199
+ - **Upgrade from 1.1.0 or older.** A running gateway older than 1.1.1 cannot prove its identity. `switch` now names it and does not stop it. Stop it by hand once, then run `switch on`.
200
+ - **Tests.** `npm test` runs only `tests/**/*.test.mjs`, also on Node.js 18 and 20.
201
+
202
+ ### Earlier changes
195
203
 
196
204
  - The desktop dashboard now uses a compact developer-tool layout. It has clearer route controls, keyboard-accessible tabs, labeled model fields, and no decorative emoji.
197
205
  - Codex profiles now use three documented roles: `main`, `review`, and `subagent`.
@@ -222,6 +230,8 @@ cp "$(npm root -g)/llm-switcher/config.example.json" ~/.llm-switcher/config.json
222
230
 
223
231
  An npm install keeps `config.json`, `admin.token` and the launch files in `~/.llm-switcher`. An upgrade replaces the package folder only, so your configuration stays.
224
232
 
233
+ If you upgrade from 1.1.0 or older, stop the running gateway before you run `switch`. An older gateway cannot prove its identity, so `switch` does not stop it for you.
234
+
225
235
  **Option B: git clone**
226
236
  ```bash
227
237
  git clone https://github.com/louisphamdev/llm-switcher.git
package/README.vi.md CHANGED
@@ -191,7 +191,15 @@ flowchart LR
191
191
 
192
192
  ---
193
193
 
194
- ## Thay đổi trong lần cập nhật này
194
+ ## Thay đổi trong bản 1.1.2
195
+
196
+ - **Gói npm.** Cài bằng `npm install -g llm-switcher` rồi chạy `switch`. Bản cài bằng npm lưu dữ liệu trong `~/.llm-switcher`, nên nâng cấp không xoá cấu hình. Bản git checkout vẫn lưu dữ liệu cạnh mã nguồn như trước.
197
+ - **Contract lab.** Gateway có thể gửi một phần nhỏ các lượt trao đổi hoàn chỉnh lên server [intact](https://github.com/louisphamdev/intact) để tìm field mà converter làm mất. Mặc định tính năng này tắt. Xem mục "Contract lab" bên dưới.
198
+ - **macOS.** `blindfold/make-certs.sh` giờ chạy được với LibreSSL, là `openssl` mặc định trên macOS.
199
+ - **Nâng cấp từ 1.1.0 trở xuống.** Gateway cũ hơn 1.1.1 không chứng minh được danh tính. `switch` giờ gọi đúng tên nó và không tự dừng nó. Dừng nó bằng tay một lần, rồi chạy `switch on`.
200
+ - **Test.** `npm test` chỉ chạy `tests/**/*.test.mjs`, kể cả trên Node.js 18 và 20.
201
+
202
+ ### Các thay đổi trước đó
195
203
 
196
204
  - Dashboard cho máy tính nay có bố cục gọn như một công cụ dành cho lập trình viên. Các điều khiển route rõ hơn, tab dùng được bằng bàn phím, trường model có nhãn đầy đủ và không còn emoji trang trí.
197
205
  - Profile Codex dùng ba vai trò theo tài liệu chính thức: `main`, `review` và `subagent`.
@@ -222,6 +230,8 @@ cp "$(npm root -g)/llm-switcher/config.example.json" ~/.llm-switcher/config.json
222
230
 
223
231
  Bản cài bằng npm lưu `config.json`, `admin.token` và các file khởi chạy trong `~/.llm-switcher`. Khi nâng cấp, npm chỉ thay thư mục package, nên cấu hình của bạn vẫn còn.
224
232
 
233
+ Nếu bạn nâng cấp từ 1.1.0 trở xuống, hãy dừng gateway đang chạy trước khi chạy `switch`. Gateway cũ không chứng minh được danh tính, nên `switch` không tự dừng nó.
234
+
225
235
  **Cách B: git clone**
226
236
  ```bash
227
237
  git clone https://github.com/louisphamdev/llm-switcher.git
@@ -2,6 +2,7 @@
2
2
  # Build the private CA and the leaf certificate that blindfold.mjs presents.
3
3
  #
4
4
  # Windows: run this from Git Bash. The openssl that ships with Git for Windows works.
5
+ # macOS: /usr/bin/openssl is LibreSSL. Use only options that LibreSSL also has (it has no `x509 -ext`).
5
6
  # The earlier attempt used PowerShell New-SelfSignedCertificate, whose leaf was refused
6
7
  # with "unsuitable certificate purpose" because it carried no serverAuth extended key
7
8
  # usage. The extension files below are what fix that, so do not drop them.
@@ -92,4 +93,4 @@ mv -f "$WORK/ca.key" "$WORK/ca.pem" "$WORK/leaf.key" "$WORK/leaf.pem" "$OUT_DIR/
92
93
 
93
94
  echo "[blindfold] CA : $OUT_DIR/ca.pem"
94
95
  echo "[blindfold] leaf : $OUT_DIR/leaf.pem"
95
- openssl x509 -in "$OUT_DIR/leaf.pem" -noout -ext extendedKeyUsage,subjectAltName
96
+ openssl x509 -in "$OUT_DIR/leaf.pem" -noout -text | grep -A1 -E "Extended Key Usage|Subject Alternative Name"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "llm-switcher",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Zero-dependency multi-protocol edge gateway & provider switcher for Claude Code, Codex, OpenAI and Gemini clients",
5
5
  "keywords": [
6
6
  "llm",
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "scripts": {
32
32
  "start": "node proxy.mjs",
33
- "test": "node --test",
33
+ "test": "node scripts/run-tests.mjs",
34
34
  "test:live": "node tests/live-optimizer-interop.mjs"
35
35
  }
36
36
  }
@@ -0,0 +1,19 @@
1
+ // Runs only tests/**/*.test.mjs. A bare `node --test` also collects test files from git-ignored
2
+ // folders (an old copy in temp/), and Node 18 and 20 do not expand a glob argument.
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+ import { spawnSync } from 'node:child_process';
6
+ import { fileURLToPath } from 'node:url';
7
+
8
+ const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
9
+ const files = [];
10
+ (function walk(dir) {
11
+ for (const e of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
12
+ const p = path.join(dir, e.name);
13
+ if (e.isDirectory()) walk(p);
14
+ else if (e.name.endsWith('.test.mjs')) files.push(path.relative(root, p));
15
+ }
16
+ })(path.join(root, 'tests'));
17
+
18
+ const r = spawnSync(process.execPath, ['--test', ...process.argv.slice(2), ...files], { cwd: root, stdio: 'inherit' });
19
+ process.exit(r.status ?? 1);
package/state.mjs CHANGED
@@ -792,13 +792,15 @@ function getJson(port, pathname, timeoutMs = 3000) {
792
792
 
793
793
  const newNonce = () => crypto.randomBytes(16).toString('hex');
794
794
 
795
- /** 'ours' | 'foreign' | 'silent' | 'free'. Treat 'silent' like 'foreign' in every decision. */
795
+ /** 'ours' | 'legacy' | 'foreign' | 'silent' | 'free'. Treat 'legacy' and 'silent' like 'foreign' in every decision. */
796
796
  export async function probeGateway(port) {
797
797
  const nonce = newNonce();
798
798
  const r = await getJson(port, `/health?challenge=${nonce}`);
799
799
  if (r.state !== 'answered') return r.state;
800
800
  const b = r.body;
801
801
  if (b?.proxy !== 'llm-switcher' || b.port !== port) return 'foreign';
802
+ // Gateways before 1.1.1 answer without a proof: name them, but never trust them enough to stop them.
803
+ if (!('proof' in b)) return 'legacy';
802
804
  const proof = identityProof(nonce, { role: 'gateway', port, pid: b.pid });
803
805
  return proof && b.proof === proof ? 'ours' : 'foreign';
804
806
  }
package/switch.mjs CHANGED
@@ -82,9 +82,14 @@ function startProxyBackground(port) {
82
82
  }
83
83
 
84
84
  // 'silent' is a listener that never answered: a hung gateway of ours, or another program.
85
- const isHeld = (state) => state === 'foreign' || state === 'silent';
85
+ const isHeld = (state) => state === 'foreign' || state === 'silent' || state === 'legacy';
86
86
 
87
87
  function refuseForeignPort(port, owner, state = 'foreign') {
88
+ if (state === 'legacy') {
89
+ console.error(`[Error] An llm-switcher gateway older than 1.1.1 runs on port ${port}. Stop it, then run \`switch on\` again.`);
90
+ console.error(' It cannot prove its identity, so this switcher does not stop it. Nothing was changed.');
91
+ process.exit(1);
92
+ }
88
93
  console.error(state === 'silent'
89
94
  ? `[Error] Port ${port} accepts connections but does not answer. A hung ${owner} or another program holds it.`
90
95
  : `[Error] Port ${port} is held by another process, not by ${owner}.`);
@@ -204,7 +209,7 @@ function listeningPids(port) {
204
209
  return [...pids];
205
210
  }
206
211
 
207
- // Returns 'stopped', 'not-running', 'not-ours', 'silent' or 'still-running'. The kill targets the process that
212
+ // Returns 'stopped', 'not-running', 'not-ours', 'legacy', 'silent' or 'still-running'. The kill targets the process that
208
213
  // listens on the port, right after the identity probe confirmed that listener is this switcher.
209
214
  async function stopProxy(port) {
210
215
  const state = await probeGateway(port);
@@ -226,6 +231,7 @@ async function stopProxy(port) {
226
231
  return 'stopped';
227
232
  }
228
233
  if (state === 'foreign') return 'not-ours';
234
+ if (state === 'legacy') return 'legacy';
229
235
  if (state === 'silent') {
230
236
  if (ownUnit) {
231
237
  serviceStop(svc);
@@ -465,6 +471,10 @@ async function turnOff(targetArg) {
465
471
  console.error(`[Error] The gateway on port ${port} is still running. Stop it by hand; the launcher files are already cleared.`);
466
472
  process.exit(1);
467
473
  }
474
+ if (result === 'legacy') {
475
+ console.error(`[Error] An llm-switcher gateway older than 1.1.1 runs on port ${port}. Stop it, then run \`switch on\` again.`);
476
+ process.exit(1);
477
+ }
468
478
  if (result === 'not-ours' || result === 'silent') {
469
479
  console.error(result === 'silent'
470
480
  ? `[Error] Port ${port} accepts connections but does not answer, so it is not proven to be this switcher. It was not stopped.`
@@ -502,7 +512,7 @@ async function showStatus() {
502
512
  const flagged = fs.existsSync(paths.activeFlag);
503
513
 
504
514
  console.log('=== LLM Switcher Status ===');
505
- const held = { foreign: `PORT ${port} HELD BY ANOTHER PROCESS`, silent: `PORT ${port} DOES NOT ANSWER (hung gateway or another program)` };
515
+ const held = { legacy: `OLD GATEWAY (< 1.1.1) ON PORT ${port}: stop it, then run \`switch on\``, foreign: `PORT ${port} HELD BY ANOTHER PROCESS`, silent: `PORT ${port} DOES NOT ANSWER (hung gateway or another program)` };
506
516
  console.log(`Proxy Service: ${isRunning ? `RUNNING (port ${port})` : held[gateway] || 'STOPPED'}`);
507
517
  console.log(`Web UI: http://127.0.0.1:${port}/ui`);
508
518
  console.log(`Launcher Flag: ${flagged ? 'active.flag present' : 'absent (launchers use official endpoints)'}`);
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
3
3
  import fs from 'node:fs';
4
4
  import os from 'node:os';
5
5
  import path from 'node:path';
6
+ import { execFileSync } from 'node:child_process';
6
7
  import { resolveDataDir } from '../state.mjs';
7
8
 
8
9
  const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'llm-sw-datadir-'));
@@ -35,3 +36,16 @@ test('package.json is publishable and exposes the switch command', () => {
35
36
  assert.equal(pkg.bin?.switch, 'switch.mjs');
36
37
  assert.ok(fs.existsSync(new URL('../LICENSE', import.meta.url)), 'LICENSE file exists');
37
38
  });
39
+
40
+ // macOS ships LibreSSL as /usr/bin/openssl. It has no `x509 -ext`; the script must not depend on it.
41
+ test('make-certs.sh runs with an openssl that lacks LibreSSL-missing options', { skip: process.platform === 'win32' }, () => {
42
+ const dir = tmp();
43
+ const real = execFileSync('sh', ['-c', 'command -v openssl'], { encoding: 'utf8' }).trim();
44
+ const bin = path.join(dir, 'bin');
45
+ fs.mkdirSync(bin);
46
+ fs.writeFileSync(path.join(bin, 'openssl'), `#!/bin/sh\nfor a in "$@"; do [ "$a" = "-ext" ] && { echo "unknown option -ext" >&2; exit 1; }; done\nexec "${real}" "$@"\n`, { mode: 0o755 });
47
+ const out = path.join(dir, 'certs');
48
+ const script = new URL('../blindfold/make-certs.sh', import.meta.url).pathname;
49
+ execFileSync('bash', [script, 'chatgpt.com', out], { env: { ...process.env, PATH: `${bin}:${process.env.PATH}` }, stdio: 'pipe' });
50
+ for (const f of ['ca.pem', 'leaf.pem', 'leaf.key']) assert.ok(fs.existsSync(path.join(out, f)), f);
51
+ });
@@ -514,7 +514,9 @@ test('make-certs.sh builds a CA that can sign only for its host', (t) => {
514
514
  fs.writeFileSync(path.join(dir, 'evil.ext'), 'subjectAltName = DNS:evil.test\n');
515
515
  ossl('x509', '-req', '-in', 'evil.csr', '-CA', path.join(certs, 'ca.pem'), '-CAkey', path.join(certs, 'ca.key'),
516
516
  '-CAcreateserial', '-days', '1', '-extfile', 'evil.ext', '-out', 'evil.pem');
517
- assert.throws(() => ossl('verify', '-CAfile', path.join(certs, 'ca.pem'), 'evil.pem'), /permitted subtree violation/);
517
+ // LibreSSL prints the verify error on stdout, OpenSSL 3 on stderr.
518
+ assert.throws(() => ossl('verify', '-CAfile', path.join(certs, 'ca.pem'), 'evil.pem'),
519
+ (e) => /permitted subtree violation/.test(`${e.stdout}${e.stderr}`));
518
520
  });
519
521
 
520
522
  // ---- Launch state, config cache, logs, probes (audit F28, F31, F42, M5, racer "blocked gateway") ----
@@ -595,6 +597,30 @@ test('a port that accepts but never answers probes as silent, not as foreign', a
595
597
  assert.equal(await probeGateway(server.address().port), 'silent');
596
598
  });
597
599
 
600
+ // A gateway from before 1.1.1 answers /health without an identity proof. It is ours in spirit but
601
+ // cannot be proven, so it is never stopped; the CLI names it instead of calling it a foreign process.
602
+ test('a pre-1.1.1 gateway answers without a proof and probes as legacy, not as foreign', async (t) => {
603
+ const http = await import('node:http');
604
+ const server = http.createServer((req, res) => {
605
+ res.writeHead(200, { 'content-type': 'application/json' });
606
+ res.end(JSON.stringify({ status: 'ok', proxy: 'llm-switcher', port: server.address().port, configLoaded: true }));
607
+ });
608
+ await new Promise(r => server.listen(0, '127.0.0.1', r));
609
+ t.after(() => server.close());
610
+ assert.equal(await probeGateway(server.address().port), 'legacy');
611
+ });
612
+
613
+ test('an llm-switcher answer with a wrong proof is still foreign', async (t) => {
614
+ const http = await import('node:http');
615
+ const server = http.createServer((req, res) => {
616
+ res.writeHead(200, { 'content-type': 'application/json' });
617
+ res.end(JSON.stringify({ status: 'ok', proxy: 'llm-switcher', port: server.address().port, pid: 1, proof: 'forged' }));
618
+ });
619
+ await new Promise(r => server.listen(0, '127.0.0.1', r));
620
+ t.after(() => server.close());
621
+ assert.equal(await probeGateway(server.address().port), 'foreign');
622
+ });
623
+
598
624
  // Codex parses an unquoted --config value as TOML first. The Windows shim passes names unquoted, so a
599
625
  // name that TOML reads as a number, a boolean or a date would change type (audit N-3 residual).
600
626
  test('isSafeModelName refuses names that TOML reads as something other than a string', () => {