diff --git a/.github/scripts/check-proxy-port-fallbacks.sh b/.github/scripts/check-proxy-port-fallbacks.sh new file mode 100755 index 0000000000..6a8a83401f --- /dev/null +++ b/.github/scripts/check-proxy-port-fallbacks.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# Guard the local-proxy ports against silent drift (#434, #435). +# +# Each proxy's port is defined once, in the let block of +# home/common/default.nix, and flows to the systemd service environment +# and the shell session variables. Two places keep a literal copy BY +# DESIGN, as the documented fallback for launches that never see those +# variables: +# +# - bin/-proxy.mjs: parseProxyPort('', ) (or the older +# process.env. || '' form) +# - .opencode/opencode.jsonc: {env::-} in the provider +# baseURL (opencode's {env:VAR} substitution has no fallback of its +# own; the :- form is what keeps a non-shell launch — systemd +# service, desktop, VS Code — from resolving to an empty port). +# +# This script fails, naming the offender, when any copy disagrees with +# the let-block definition, when a baseURL loses its :- fallback, or +# when a bare {env:} or a literal proxy port reappears in +# opencode.jsonc. Clef has no opencode.jsonc consumer (its models ride +# the Cloudflare gateway), so only its script fallback is checked. +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +nix_file="$repo_root/home/common/default.nix" +config_file="$repo_root/.opencode/opencode.jsonc" + +# var | let binding | proxy script | has an opencode.jsonc baseURL +proxies=( + "CLEF_PROXY_PORT|clefProxyPort|bin/clef-proxy.mjs|no" + "AGY_PROXY_PORT|agyProxyPort|bin/agy-proxy.mjs|yes" + "CODEX_PROXY_PORT|codexProxyPort|bin/codex-proxy.mjs|yes" + "ANTIGRAVITY_LS_PROXY_PORT|antigravityLsProxyPort|bin/antigravity-ls-proxy.mjs|yes" + "DESIGN_CODE_PROXY_PORT|designCodeProxyPort|bin/design-code-proxy.mjs|yes" +) + +failed=0 +for entry in "${proxies[@]}"; do + IFS='|' read -r var let_name script has_config <<<"$entry" + + let_port="$(grep -E "^[[:space:]]*${let_name} = [0-9]+;" "$nix_file" | grep -oE '[0-9]+' | head -1 || true)" + if [[ -z $let_port ]]; then + echo "::error::${let_name} definition not found in home/common/default.nix." + failed=1 + continue + fi + + if ! grep -qF "parseProxyPort('${var}', ${let_port})" "$repo_root/$script" && + ! grep -qF "process.env.${var} || '${let_port}'" "$repo_root/$script"; then + echo "::error::$script fallback for ${var} does not match the ${let_name} definition (${let_port}) in home/common/default.nix." + failed=1 + fi + + if [[ $has_config == yes ]] && ! grep -qF "{env:${var}:-${let_port}}" "$config_file"; then + echo "::error::.opencode/opencode.jsonc has no {env:${var}:-${let_port}} baseURL — the fallback must match the ${let_name} definition (${let_port}); a bare {env:${var}} resolves to an empty port outside a home-manager shell (#434)." + failed=1 + fi + if grep -qF "{env:${var}}" "$config_file"; then + echo "::error::.opencode/opencode.jsonc uses a bare {env:${var}} with no :- fallback (#434)." + failed=1 + fi +done + +if grep -qE '127\.0\.0\.1:1874[0-9]' "$config_file"; then + echo "::error::.opencode/opencode.jsonc hardcodes a literal proxy port — use {env::-} so the copies cannot drift (#435)." + failed=1 +fi + +if [[ $failed -ne 0 ]]; then + exit 1 +fi + +echo "proxy port fallbacks match the home/common/default.nix definitions (${#proxies[@]} proxies)." diff --git a/.github/workflows/nix-ci.yml b/.github/workflows/nix-ci.yml index b9abe85d21..fbc44888eb 100644 --- a/.github/workflows/nix-ci.yml +++ b/.github/workflows/nix-ci.yml @@ -29,6 +29,13 @@ jobs: - uses: actions/checkout@v7 with: fetch-depth: 0 + # The proxy ports' fallback copies (bin/*.mjs script defaults and + # the {env:VAR:-port} defaults in .opencode/opencode.jsonc) must + # match the single definitions in home/common/default.nix, or a + # non-shell launch silently dials the wrong — or an empty — port + # (#434, #435). Pure bash/grep: no Nix needed. + - name: Check proxy port fallbacks match their definitions + run: .github/scripts/check-proxy-port-fallbacks.sh - uses: DeterminateSystems/nix-installer-action@3138316df39ed29be04236d7ffc686fa525866aa # v23 - name: Check formatting for changed files env: diff --git a/.opencode/opencode.jsonc b/.opencode/opencode.jsonc index 94fa9e7bf4..ab6c216000 100644 --- a/.opencode/opencode.jsonc +++ b/.opencode/opencode.jsonc @@ -1893,7 +1893,10 @@ "npm": "@ai-sdk/openai-compatible", "name": "Antigravity CLI (via agy)", "options": { - "baseURL": "http://127.0.0.1:18743/v1", + // Port comes from AGY_PROXY_PORT (set by home config from the + // same single definition the agy-proxy service binds) — never + // hardcode a port here; see the openai-codex provider note. + "baseURL": "http://127.0.0.1:{env:AGY_PROXY_PORT:-18743}/v1", "apiKey": "agy-local" }, "models": { @@ -1975,7 +1978,11 @@ "npm": "@ai-sdk/openai-compatible", "name": "Antigravity Language Server (Direct gRPC/ConnectRPC)", "options": { - "baseURL": "http://127.0.0.1:18745/v1", + // Port comes from ANTIGRAVITY_LS_PROXY_PORT (set by home config + // from the same single definition the antigravity-ls-proxy + // service binds) — never hardcode a port here; see the + // openai-codex provider note. + "baseURL": "http://127.0.0.1:{env:ANTIGRAVITY_LS_PROXY_PORT:-18745}/v1", "apiKey": "antigravity-local" }, "models": { @@ -2715,7 +2722,7 @@ // same single definition the codex-proxy service binds) — never // hardcode a port here; a hardcoded one silently breaks on any // device where the proxy isn't on that exact port. - "baseURL": "http://127.0.0.1:{env:CODEX_PROXY_PORT}/v1", + "baseURL": "http://127.0.0.1:{env:CODEX_PROXY_PORT:-18744}/v1", "apiKey": "codex-local" }, "models": { @@ -3085,7 +3092,7 @@ "npm": "@ai-sdk/openai-compatible", "name": "ChatGPT (via Codex)", "options": { - "baseURL": "http://127.0.0.1:18744/v1", + "baseURL": "http://127.0.0.1:{env:CODEX_PROXY_PORT:-18744}/v1", "apiKey": "codex-local" }, "models": { @@ -3387,7 +3394,7 @@ "npm": "@ai-sdk/openai-compatible", "name": "Design Code", "options": { - "baseURL": "http://127.0.0.1:18746/v1", + "baseURL": "http://127.0.0.1:{env:DESIGN_CODE_PROXY_PORT:-18746}/v1", "apiKey": "design-local" }, "models": { @@ -3474,7 +3481,7 @@ "npm": "@ai-sdk/openai-compatible", "name": "Code Design", "options": { - "baseURL": "http://127.0.0.1:18746/v1", + "baseURL": "http://127.0.0.1:{env:DESIGN_CODE_PROXY_PORT:-18746}/v1", "apiKey": "design-local" }, "models": { @@ -3561,7 +3568,7 @@ "npm": "@ai-sdk/openai-compatible", "name": "Open Design", "options": { - "baseURL": "http://127.0.0.1:18746/v1", + "baseURL": "http://127.0.0.1:{env:DESIGN_CODE_PROXY_PORT:-18746}/v1", "apiKey": "design-local" }, "models": { @@ -3648,7 +3655,7 @@ "npm": "@ai-sdk/openai-compatible", "name": "OpenDesign", "options": { - "baseURL": "http://127.0.0.1:18746/v1", + "baseURL": "http://127.0.0.1:{env:DESIGN_CODE_PROXY_PORT:-18746}/v1", "apiKey": "design-local" }, "models": { diff --git a/bin/agy-proxy.mjs b/bin/agy-proxy.mjs index 267ca8fd96..d49e95f818 100755 --- a/bin/agy-proxy.mjs +++ b/bin/agy-proxy.mjs @@ -3,11 +3,46 @@ import http from 'node:http'; import { spawn } from 'node:child_process'; import crypto from 'node:crypto'; import fs from 'node:fs'; +import path from 'node:path'; +import os from 'node:os'; import { parseProxyPort } from './proxy-port.mjs'; const PORT = parseProxyPort('AGY_PROXY_PORT', 18743); const HOST = process.env.AGY_PROXY_HOST || '127.0.0.1'; +// Publish the bound endpoint so other tools can find this proxy without +// knowing its port in advance: $XDG_STATE_HOME//endpoint.json +// (default ~/.local/state//endpoint.json), written on listen, +// removed on exit. +const publishEndpoint = () => { + try { + const stateHome = + process.env.XDG_STATE_HOME || path.join(os.homedir(), '.local', 'state'); + const dir = path.join(stateHome, 'agy-proxy'); + fs.mkdirSync(dir, { recursive: true }); + const file = path.join(dir, 'endpoint.json'); + fs.writeFileSync( + `${file}.tmp`, + JSON.stringify({ host: HOST, port: PORT, pid: process.pid, url: `http://${HOST}:${PORT}` }) + '\n', + ); + fs.renameSync(`${file}.tmp`, file); + const cleanup = () => { + try { + fs.unlinkSync(file); + } catch {} + }; + process.on('exit', cleanup); + process.on('SIGTERM', () => { + cleanup(); + process.exit(0); + }); + process.on('SIGINT', () => { + cleanup(); + process.exit(0); + }); + } catch {} +}; + const MODELS = [ { id: 'gemini-3.8-flash', name: 'Gemini 3.8 Flash (agy)', efforts: ['low', 'medium', 'high'], defaultEffort: 'medium' }, { id: 'gemini-3.7-flash', name: 'Gemini 3.7 Flash (agy)', efforts: ['low', 'medium', 'high'], defaultEffort: 'medium' }, @@ -582,6 +617,7 @@ const server = http.createServer((req, res) => { }); server.listen(PORT, HOST, () => { + publishEndpoint(); console.log(`Antigravity agy Proxy listening on http://${HOST}:${PORT}`); console.log(`Endpoints:`); console.log(` - GET http://${HOST}:${PORT}/health`); diff --git a/bin/antigravity-ls-proxy.mjs b/bin/antigravity-ls-proxy.mjs index d397e64fbf..1d7b3d8f1e 100755 --- a/bin/antigravity-ls-proxy.mjs +++ b/bin/antigravity-ls-proxy.mjs @@ -3,11 +3,41 @@ import http from 'node:http'; import { spawn, execSync } from 'node:child_process'; import crypto from 'node:crypto'; import fs from 'node:fs'; +import path from 'node:path'; +import os from 'node:os'; import { parseProxyPort, parseAddressPortValue } from './proxy-port.mjs'; const PORT = parseProxyPort('ANTIGRAVITY_LS_PROXY_PORT', 18745); const HOST = process.env.ANTIGRAVITY_LS_PROXY_HOST || '127.0.0.1'; +// Publish the bound endpoint so other tools can find this proxy without +// knowing its port in advance: $XDG_STATE_HOME//endpoint.json +// (default ~/.local/state//endpoint.json), written on listen, +// removed on exit. This file already registers SIGTERM/SIGINT handlers +// (see below, calling lsManager.cleanup() then process.exit(0)), so this +// helper does not register duplicate signal handlers; the 'exit' cleanup +// it registers runs on that existing exit path. +const publishEndpoint = () => { + try { + const stateHome = + process.env.XDG_STATE_HOME || path.join(os.homedir(), '.local', 'state'); + const dir = path.join(stateHome, 'antigravity-ls-proxy'); + fs.mkdirSync(dir, { recursive: true }); + const file = path.join(dir, 'endpoint.json'); + fs.writeFileSync( + `${file}.tmp`, + JSON.stringify({ host: HOST, port: PORT, pid: process.pid, url: `http://${HOST}:${PORT}` }) + '\n', + ); + fs.renameSync(`${file}.tmp`, file); + const cleanup = () => { + try { + fs.unlinkSync(file); + } catch {} + }; + process.on('exit', cleanup); + } catch {} +}; + const DEFAULT_MANAGED_HTTP_PORT = 48999; const DEFAULT_MANAGED_HTTPS_PORT = 48998; const MANAGED_CSRF_TOKEN = 'antigravity-ls-local-token-' + crypto.randomUUID().slice(0, 8); @@ -860,6 +890,7 @@ const server = http.createServer(async (req, res) => { }); server.listen(PORT, HOST, () => { + publishEndpoint(); console.log(`Antigravity Language Server Proxy listening on http://${HOST}:${PORT}`); console.log(`Endpoints:`); console.log(` - GET http://${HOST}:${PORT}/health`); diff --git a/bin/clef-proxy.mjs b/bin/clef-proxy.mjs index 3a4ef8360a..f90570a46d 100755 --- a/bin/clef-proxy.mjs +++ b/bin/clef-proxy.mjs @@ -8,6 +8,39 @@ import { parseProxyPort } from './proxy-port.mjs'; const PORT = parseProxyPort('CLEF_PROXY_PORT', 18742); const HOST = process.env.CLEF_PROXY_HOST || '127.0.0.1'; +// Publish the bound endpoint so other tools can find this proxy without +// knowing its port in advance: $XDG_STATE_HOME//endpoint.json +// (default ~/.local/state//endpoint.json), written on listen, +// removed on exit. +const publishEndpoint = () => { + try { + const stateHome = + process.env.XDG_STATE_HOME || path.join(os.homedir(), '.local', 'state'); + const dir = path.join(stateHome, 'clef-proxy'); + fs.mkdirSync(dir, { recursive: true }); + const file = path.join(dir, 'endpoint.json'); + fs.writeFileSync( + `${file}.tmp`, + JSON.stringify({ host: HOST, port: PORT, pid: process.pid, url: `http://${HOST}:${PORT}` }) + '\n', + ); + fs.renameSync(`${file}.tmp`, file); + const cleanup = () => { + try { + fs.unlinkSync(file); + } catch {} + }; + process.on('exit', cleanup); + process.on('SIGTERM', () => { + cleanup(); + process.exit(0); + }); + process.on('SIGINT', () => { + cleanup(); + process.exit(0); + }); + } catch {} +}; + const readConfigFileSync = (filename, fallback = '') => { try { const fullPath = path.join(os.homedir(), '.config', 'cloudflare', filename); @@ -130,6 +163,7 @@ const server = http.createServer(async (req, res) => { }); server.listen(PORT, HOST, () => { + publishEndpoint(); console.log(`Clef System One Proxy listening on http://${HOST}:${PORT}`); console.log(`Endpoint: http://${HOST}:${PORT}/v1/systemone`); }); diff --git a/bin/codex-proxy.mjs b/bin/codex-proxy.mjs index c276413f34..c129953a2d 100755 --- a/bin/codex-proxy.mjs +++ b/bin/codex-proxy.mjs @@ -3,11 +3,46 @@ import http from 'node:http'; import { spawn, execSync } from 'node:child_process'; import crypto from 'node:crypto'; import fs from 'node:fs'; +import path from 'node:path'; +import os from 'node:os'; import { parseProxyPort } from './proxy-port.mjs'; const PORT = parseProxyPort('CODEX_PROXY_PORT', 18744); const HOST = process.env.CODEX_PROXY_HOST || '127.0.0.1'; +// Publish the bound endpoint so other tools can find this proxy without +// knowing its port in advance: $XDG_STATE_HOME//endpoint.json +// (default ~/.local/state//endpoint.json), written on listen, +// removed on exit. +const publishEndpoint = () => { + try { + const stateHome = + process.env.XDG_STATE_HOME || path.join(os.homedir(), '.local', 'state'); + const dir = path.join(stateHome, 'codex-proxy'); + fs.mkdirSync(dir, { recursive: true }); + const file = path.join(dir, 'endpoint.json'); + fs.writeFileSync( + `${file}.tmp`, + JSON.stringify({ host: HOST, port: PORT, pid: process.pid, url: `http://${HOST}:${PORT}` }) + '\n', + ); + fs.renameSync(`${file}.tmp`, file); + const cleanup = () => { + try { + fs.unlinkSync(file); + } catch {} + }; + process.on('exit', cleanup); + process.on('SIGTERM', () => { + cleanup(); + process.exit(0); + }); + process.on('SIGINT', () => { + cleanup(); + process.exit(0); + }); + } catch {} +}; + // Synchronize freshly refreshed ChatGPT OAuth tokens from Codex to OpenCode SQLite DB const syncCodexTokensToOpenCode = () => { try { @@ -434,6 +469,7 @@ const server = http.createServer((req, res) => { }); server.listen(PORT, HOST, () => { + publishEndpoint(); console.log(`[codex-proxy] OpenAI-compatible bridge listening on http://${HOST}:${PORT}`); console.log(`[codex-proxy] Resolved codex binary: ${CODEX_BIN}`); syncCodexTokensToOpenCode(); diff --git a/home/common/default.nix b/home/common/default.nix index 5cc1344457..29c2027851 100644 --- a/home/common/default.nix +++ b/home/common/default.nix @@ -7,12 +7,18 @@ ... }: let - # The codex proxy's port, defined ONCE. It flows to the systemd user - # service (which binds it), to the shell session (CODEX_PROXY_PORT), and - # into opencode.jsonc's openai-codex baseURL via {env:CODEX_PROXY_PORT} - # substitution — so the three can never drift apart, and a new device - # that applies this config gets a consistent port automatically. + # Local proxy ports, each defined ONCE. Each value flows to the systemd + # user service that binds it, to the shell session (_PROXY_PORT), + # and — for the proxies opencode talks to — into opencode.jsonc's + # baseURL via {env:...} substitution, so the copies can never drift + # apart and a new device that applies this config gets consistent ports + # automatically. The bin/*.mjs proxies keep the same numbers as fallback + # defaults when the env var is absent. + clefProxyPort = 18742; + agyProxyPort = 18743; codexProxyPort = 18744; + antigravityLsProxyPort = 18745; + designCodeProxyPort = 18746; # Upstream prebuilt release binaries for tools where nixpkgs trails upstream. # See pkgs/latest-cli/default.nix for the rationale, the tradeoffs, and the # list of things deliberately NOT bumped. @@ -474,7 +480,7 @@ in Restart = "always"; RestartSec = 3; Environment = [ - "CLEF_PROXY_PORT=18742" + "CLEF_PROXY_PORT=${toString clefProxyPort}" "CLEF_PROXY_HOST=127.0.0.1" ]; }; @@ -497,7 +503,7 @@ in ); Restart = "no"; Environment = [ - "AGY_PROXY_PORT=18743" + "AGY_PROXY_PORT=${toString agyProxyPort}" "AGY_PROXY_HOST=127.0.0.1" ]; }; @@ -542,7 +548,7 @@ in Restart = "always"; RestartSec = 3; Environment = [ - "ANTIGRAVITY_LS_PROXY_PORT=18745" + "ANTIGRAVITY_LS_PROXY_PORT=${toString antigravityLsProxyPort}" "ANTIGRAVITY_LS_PROXY_HOST=127.0.0.1" "ANTIGRAVITY_LS_BIN=${antigravity-hub}/share/antigravity/resources/bin/language_server" ]; @@ -574,7 +580,7 @@ in Restart = "always"; RestartSec = 3; Environment = [ - "DESIGN_CODE_PROXY_PORT=18746" + "DESIGN_CODE_PROXY_PORT=${toString designCodeProxyPort}" "DESIGN_CODE_PROXY_HOST=127.0.0.1" ]; }; @@ -1236,9 +1242,13 @@ in OPENCODE_EXPERIMENTAL_OXFMT = "1"; OPENCODE_EXPERIMENTAL_EXA = "1"; OPENCODE_EXPERIMENTAL_EVENT_SYSTEM = "1"; - # Single source for the codex proxy port (see the let block): - # opencode.jsonc reads it via {env:CODEX_PROXY_PORT}. + # Single source for the local proxy ports (see the let block): + # opencode.jsonc reads them via {env:...} substitution. + CLEF_PROXY_PORT = toString clefProxyPort; + AGY_PROXY_PORT = toString agyProxyPort; CODEX_PROXY_PORT = toString codexProxyPort; + ANTIGRAVITY_LS_PROXY_PORT = toString antigravityLsProxyPort; + DESIGN_CODE_PROXY_PORT = toString designCodeProxyPort; OPENCODE_EXPERIMENTAL_ICON_DISCOVERY = "1"; OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT = "1"; # File watcher off by default (Drew, 2026-10-20): the working trees