Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/agy-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import http from 'node:http';
import { spawn } from 'node:child_process';
import crypto from 'node:crypto';
import fs from 'node:fs';
import { parseProxyPort } from './proxy-port.mjs';

const PORT = parseInt(process.env.AGY_PROXY_PORT || '18743', 10);
const PORT = parseProxyPort('AGY_PROXY_PORT', 18743);
const HOST = process.env.AGY_PROXY_HOST || '127.0.0.1';

const MODELS = [
Expand Down
3 changes: 2 additions & 1 deletion bin/antigravity-ls-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import http from 'node:http';
import { spawn, execSync } from 'node:child_process';
import crypto from 'node:crypto';
import fs from 'node:fs';
import { parseProxyPort } from './proxy-port.mjs';

const PORT = parseInt(process.env.ANTIGRAVITY_LS_PROXY_PORT || '18745', 10);
const PORT = parseProxyPort('ANTIGRAVITY_LS_PROXY_PORT', 18745);
const HOST = process.env.ANTIGRAVITY_LS_PROXY_HOST || '127.0.0.1';

const DEFAULT_MANAGED_HTTP_PORT = 48999;
Expand Down
3 changes: 2 additions & 1 deletion bin/clef-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { parseProxyPort } from './proxy-port.mjs';

const PORT = parseInt(process.env.CLEF_PROXY_PORT || '18742', 10);
const PORT = parseProxyPort('CLEF_PROXY_PORT', 18742);
const HOST = process.env.CLEF_PROXY_HOST || '127.0.0.1';

const readConfigFileSync = (filename, fallback = '') => {
Expand Down
3 changes: 2 additions & 1 deletion bin/codex-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import http from 'node:http';
import { spawn, execSync } from 'node:child_process';
import crypto from 'node:crypto';
import fs from 'node:fs';
import { parseProxyPort } from './proxy-port.mjs';

const PORT = parseInt(process.env.CODEX_PROXY_PORT || '18744', 10);
const PORT = parseProxyPort('CODEX_PROXY_PORT', 18744);
const HOST = process.env.CODEX_PROXY_HOST || '127.0.0.1';

// Synchronize freshly refreshed ChatGPT OAuth tokens from Codex to OpenCode SQLite DB
Expand Down
7 changes: 4 additions & 3 deletions bin/design-code-proxy.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env node
import http from 'node:http';
import https from 'node:https';
import { parseProxyPort } from './proxy-port.mjs';

const PORT = parseInt(process.env.DESIGN_CODE_PROXY_PORT || '18746', 10);
const PORT = parseProxyPort('DESIGN_CODE_PROXY_PORT', 18746);
const HOST = process.env.DESIGN_CODE_PROXY_HOST || '127.0.0.1';

const ANTIGRAVITY_LS_PORT = parseInt(process.env.ANTIGRAVITY_LS_PROXY_PORT || '18745', 10);
const CODEX_PROXY_PORT = parseInt(process.env.CODEX_PROXY_PORT || '18744', 10);
const ANTIGRAVITY_LS_PORT = parseProxyPort('ANTIGRAVITY_LS_PROXY_PORT', 18745);
const CODEX_PROXY_PORT = parseProxyPort('CODEX_PROXY_PORT', 18744);
const AMR_UPSTREAM_URL = process.env.AMR_UPSTREAM_URL || 'https://amr-link.open-design.ai/v1';

const DESIGN_SYSTEM_PROMPT = `You are an elite Code & Systems Design Architect.
Expand Down
33 changes: 33 additions & 0 deletions bin/proxy-port.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Strict parsing for the bin/* proxies' listen ports (#483).
//
// A bare parseInt is not a validator: 'abc' becomes NaN (an opaque
// ERR_SOCKET_BAD_PORT crash at listen time), '18744abc' and '18744.9'
// silently become 18744, and '0x4a38' becomes 0, which binds a random
// ephemeral port while every client keeps dialling the configured one.
// Every proxy parses its *_PROXY_PORT through here instead, so a bad
// value fails at startup with the env var and its value named.
//
// These scripts are launched by home-manager as
// `node ${../../bin}/<name>.mjs` (the whole bin/ directory is copied to
// the store), so this sibling import resolves both from a checkout and
// from the store path. Do not inline-copy this helper into a proxy.

export const parseProxyPortValue = (name, raw, fallback) => {
if (raw === undefined || raw === '') return fallback;
if (/^[0-9]+$/.test(raw)) {
const port = Number(raw);
if (port >= 1 && port <= 65535) return port;
}
throw new Error(
`${name}="${raw}" is not a valid port (expected an integer 1-65535)`
);
};

export const parseProxyPort = (name, fallback) => {
try {
return parseProxyPortValue(name, process.env[name], fallback);
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
160 changes: 160 additions & 0 deletions bin/proxy-port.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Tests for the shared proxy port parser (#483).
// Run: node --test bin/proxy-port.test.mjs
import assert from 'node:assert/strict';
import { spawn, spawnSync } from 'node:child_process';
import fs from 'node:fs';
import net from 'node:net';
import path from 'node:path';
import test from 'node:test';
import { fileURLToPath } from 'node:url';
import { parseProxyPortValue } from './proxy-port.mjs';

const binDir = path.dirname(fileURLToPath(import.meta.url));

test('unset and empty values fall back to the default', () => {
assert.equal(parseProxyPortValue('X_PROXY_PORT', undefined, 18744), 18744);
assert.equal(parseProxyPortValue('X_PROXY_PORT', '', 18744), 18744);
});

test('valid decimal ports are accepted exactly', () => {
assert.equal(parseProxyPortValue('X_PROXY_PORT', '1', 18744), 1);
assert.equal(parseProxyPortValue('X_PROXY_PORT', '18744', 1), 18744);
assert.equal(parseProxyPortValue('X_PROXY_PORT', '65535', 1), 65535);
});

test('invalid values throw an error naming the env var and the value', () => {
const invalid = [
'abc',
'0x4a38',
'99999',
'65536',
'0',
'-1',
'18744.9',
'18744abc',
' 18744',
'+18744'
];
for (const raw of invalid) {
assert.throws(
() => parseProxyPortValue('CODEX_PROXY_PORT', raw, 18744),
(err) => {
assert.match(err.message, /CODEX_PROXY_PORT/);
assert.ok(
err.message.includes(`"${raw}"`),
`message should quote the value: ${err.message}`
);
assert.match(err.message, /integer 1-65535/);
return true;
},
`expected ${JSON.stringify(raw)} to be rejected`
);
}
});

const runClef = (value) =>
spawnSync(process.execPath, [path.join(binDir, 'clef-proxy.mjs')], {
env: { ...process.env, CLEF_PROXY_PORT: value },
encoding: 'utf8',
timeout: 10000
});

test('clef-proxy exits at startup naming the var for abc / 0x… / 99999', () => {
for (const raw of ['abc', '0x4a38', '99999']) {
const result = runClef(raw);
assert.equal(result.status, 1, `value ${raw}: ${result.stderr}`);
assert.match(result.stderr, /CLEF_PROXY_PORT/);
assert.ok(result.stderr.includes(`"${raw}"`), result.stderr);
assert.doesNotMatch(result.stdout, /listening/);
}
});

test('clef-proxy binds exactly the requested valid port', async (t) => {
const probe = net.createServer();
await new Promise((resolve) => probe.listen(0, '127.0.0.1', resolve));
const port = probe.address().port;
await new Promise((resolve) => probe.close(resolve));

const child = spawn(process.execPath, [path.join(binDir, 'clef-proxy.mjs')], {
env: { ...process.env, CLEF_PROXY_PORT: String(port) },
stdio: ['ignore', 'pipe', 'pipe']
});
t.after(() => child.kill());
const listening = await new Promise((resolve, reject) => {
let out = '';
const timer = setTimeout(
() => reject(new Error(`no listening line: ${out}`)),
10000
);
child.stdout.on('data', (chunk) => {
out += chunk;
if (out.includes('listening')) {
clearTimeout(timer);
resolve(out);
}
});
child.on('exit', () => {
clearTimeout(timer);
reject(new Error(`proxy exited early: ${out}`));
});
});
assert.ok(
listening.includes(`http://127.0.0.1:${port}`),
`expected the requested port in: ${listening}`
);
const res = await fetch(`http://127.0.0.1:${port}/health`);
assert.equal(res.status, 200);
assert.equal((await res.json()).port, port);
});

test('all seven parse sites in the five proxies use the shared helper', () => {
const files = [
'codex-proxy.mjs',
'clef-proxy.mjs',
'agy-proxy.mjs',
'antigravity-ls-proxy.mjs',
'design-code-proxy.mjs'
];
let sites = 0;
for (const file of files) {
const source = fs.readFileSync(path.join(binDir, file), 'utf8');
assert.match(
source,
/import \{ parseProxyPort \} from '\.\/proxy-port\.mjs';/,
`${file} should import the shared helper`
);
assert.doesNotMatch(
source,
/parseInt\(process\.env\.[A-Z_]*PROXY_PORT/,
`${file} still parses a proxy port with bare parseInt`
);
sites += source.match(/parseProxyPort\('[A-Z_]+_PROXY_PORT', \d+\)/g).length;
}
assert.equal(sites, 7);
});

test('home-manager launches the proxies from the bin directory', () => {
// default.nix used to interpolate each script as a single store file
// (${../../bin/<name>.mjs}); a sibling import like ./proxy-port.mjs
// cannot resolve from there. The directory form must stay.
const nix = fs.readFileSync(
path.join(binDir, '..', 'home', 'common', 'default.nix'),
'utf8'
);
for (const file of [
'codex-proxy.mjs',
'clef-proxy.mjs',
'agy-proxy.mjs',
'antigravity-ls-proxy.mjs',
'design-code-proxy.mjs'
]) {
assert.ok(
nix.includes('${../../bin}/' + file),
`default.nix should launch ${file} via the bin directory`
);
assert.ok(
!nix.includes('${../../bin/' + file + '}'),
`default.nix still launches ${file} as a single store file`
);
}
});
20 changes: 10 additions & 10 deletions home/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ in
Service = {
ExecStart = toString (
pkgs.writeShellScript "clef-proxy-start" ''
exec ${pkgs.nodejs}/bin/node ${../../bin/clef-proxy.mjs}
exec ${pkgs.nodejs}/bin/node ${../../bin}/clef-proxy.mjs
''
);
Restart = "always";
Expand All @@ -486,7 +486,7 @@ in
ExecStart = toString (
pkgs.writeShellScript "agy-proxy-start" ''
export PATH="$HOME/.gemini/bin:$HOME/.local/bin:$PATH"
exec ${pkgs.nodejs}/bin/node ${../../bin/agy-proxy.mjs}
exec ${pkgs.nodejs}/bin/node ${../../bin}/agy-proxy.mjs
''
);
Restart = "no";
Expand All @@ -507,7 +507,7 @@ in
ExecStart = toString (
pkgs.writeShellScript "codex-proxy-start" ''
export PATH="/run/current-system/sw/bin:$HOME/.local/bin:$PATH"
exec ${pkgs.nodejs}/bin/node ${../../bin/codex-proxy.mjs}
exec ${pkgs.nodejs}/bin/node ${../../bin}/codex-proxy.mjs
''
);
Restart = "always";
Expand All @@ -530,7 +530,7 @@ in
ExecStart = toString (
pkgs.writeShellScript "antigravity-ls-proxy-start" ''
export PATH="/run/current-system/sw/bin:$HOME/.gemini/bin:$HOME/.local/bin:$PATH"
exec ${pkgs.nodejs}/bin/node ${../../bin/antigravity-ls-proxy.mjs}
exec ${pkgs.nodejs}/bin/node ${../../bin}/antigravity-ls-proxy.mjs
''
);
Restart = "always";
Expand Down Expand Up @@ -562,7 +562,7 @@ in
ExecStart = toString (
pkgs.writeShellScript "design-code-proxy-start" ''
export PATH="/run/current-system/sw/bin:$HOME/.local/bin:$PATH"
exec ${pkgs.nodejs}/bin/node ${../../bin/design-code-proxy.mjs}
exec ${pkgs.nodejs}/bin/node ${../../bin}/design-code-proxy.mjs
''
);
Restart = "always";
Expand Down Expand Up @@ -1242,23 +1242,23 @@ in
[
libnotify
(pkgs.writeShellScriptBin "clef-proxy" ''
exec ${pkgs.nodejs}/bin/node ${../../bin/clef-proxy.mjs} "$@"
exec ${pkgs.nodejs}/bin/node ${../../bin}/clef-proxy.mjs "$@"
'')
(pkgs.writeShellScriptBin "agy-proxy" ''
export PATH="$HOME/.gemini/bin:$HOME/.local/bin:$PATH"
exec ${pkgs.nodejs}/bin/node ${../../bin/agy-proxy.mjs} "$@"
exec ${pkgs.nodejs}/bin/node ${../../bin}/agy-proxy.mjs "$@"
'')
(pkgs.writeShellScriptBin "codex-proxy" ''
export PATH="/run/current-system/sw/bin:$HOME/.local/bin:$PATH"
exec ${pkgs.nodejs}/bin/node ${../../bin/codex-proxy.mjs} "$@"
exec ${pkgs.nodejs}/bin/node ${../../bin}/codex-proxy.mjs "$@"
'')
(pkgs.writeShellScriptBin "antigravity-ls-proxy" ''
export PATH="/run/current-system/sw/bin:$HOME/.gemini/bin:$HOME/.local/bin:$PATH"
exec ${pkgs.nodejs}/bin/node ${../../bin/antigravity-ls-proxy.mjs} "$@"
exec ${pkgs.nodejs}/bin/node ${../../bin}/antigravity-ls-proxy.mjs "$@"
'')
(pkgs.writeShellScriptBin "design-code-proxy" ''
export PATH="/run/current-system/sw/bin:$HOME/.local/bin:$PATH"
exec ${pkgs.nodejs}/bin/node ${../../bin/design-code-proxy.mjs} "$@"
exec ${pkgs.nodejs}/bin/node ${../../bin}/design-code-proxy.mjs "$@"
'')
(pkgs.writeShellScriptBin "sync-models" ''
export PATH="/run/current-system/sw/bin:$HOME/.gemini/bin:$HOME/.local/bin:$PATH"
Expand Down
Loading