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
13 changes: 8 additions & 5 deletions bin/design-code-proxy.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
import http from 'node:http';
import https from 'node:https';
import { parseProxyPort } from './proxy-port.mjs';
import { parseProxyMillis, parseProxyPort } from './proxy-port.mjs';
import { createRequestBodyCollector } from './request-body.mjs';
import { createStreamDecoder } from './stream-decode.mjs';

Expand Down Expand Up @@ -31,10 +31,13 @@ const httpsAgent = new https.Agent({ keepAlive: true, keepAliveMsecs: 1000, maxS
// slow-but-alive streams are never killed. Pinned at 60s — generous for a
// slow first byte from a local language-server backend — and overridable
// via DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS (tests pin it short).
const BACKEND_TIMEOUT_MS = (() => {
const raw = Number(process.env.DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS);
return Number.isFinite(raw) && raw > 0 ? Math.floor(raw) : 60_000;
})();
// Strict parse via the house helper (#4225): the previous hand-rolled
// Number() guard admitted values above 2^31-1, which Node's timer layer
// silently truncates to 2147483647 ms (~24.8 days) — defeating this
// timeout entirely — and non-decimal spellings like 0x10 (16 ms).
// parseProxyMillis refuses anything outside integer 1..2147483647 at
// startup, naming the variable; unset/empty keeps the 60s default.
const BACKEND_TIMEOUT_MS = parseProxyMillis('DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS', 60_000);

const DESIGN_SYSTEM_PROMPT = `You are an elite Code & Systems Design Architect.
Your core expertise is in software architecture, clean code design, UI/UX design engineering, design tokens, component abstractions, domain-driven design, and maintainable systems patterns. Provide elegant, production-grade architectures and idiomatic design implementations.`;
Expand Down
59 changes: 59 additions & 0 deletions bin/design-code-proxy.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,62 @@ test('a primary that stalls mid-stream has its client stream torn down by the ti
assert.equal(await Promise.race([read, bounded]), 'torn-down');
assert.equal(fallback.calls, 0);
});

// #4225: DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS goes through the strict
// parseProxyMillis helper. Overflow and non-decimal spellings must
// refuse to start (naming the variable) instead of silently truncating
// to ~24.8 days or 16 ms; a valid value is honoured (the #2097 tests
// above already pin short values through startProxy).
const spawnProxyRaw = async (extraEnv) => {
const port = await freePort();
const env = { ...process.env };
delete env.OPEN_DESIGN_API_KEY;
delete env.AMR_API_KEY;
const child = spawn(process.execPath, [path.join(binDir, 'design-code-proxy.mjs')], {
env: { ...env, DESIGN_CODE_PROXY_PORT: String(port), ...extraEnv },
stdio: ['ignore', 'pipe', 'pipe']
});
let log = '';
child.stdout.on('data', d => { log += d; });
child.stderr.on('data', d => { log += d; });
return { child, port, log: () => log };
};

const waitForExit = (child, ms = 5000) => new Promise((resolve, reject) => {
const timer = setTimeout(() => reject(new Error('proxy did not exit in time')), ms);
child.on('exit', code => { clearTimeout(timer); resolve(code); });
});

test('a backend timeout above 2^31-1 refuses to start, naming the variable (#4225)', async () => {
const { child, log } = await spawnProxyRaw({ DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS: '2147483648' });
const code = await waitForExit(child);
assert.notEqual(code, 0);
assert.match(log(), /DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS/);
assert.match(log(), /2147483648/);
});

test('non-decimal backend timeout spellings refuse to start (#4225)', async () => {
for (const bad of ['0x10', '1e5']) {
const { child, log } = await spawnProxyRaw({ DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS: bad });
const code = await waitForExit(child);
assert.notEqual(code, 0, `${bad} should refuse to start`);
assert.match(log(), /DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS/);
}
});

test('a valid backend timeout value is honoured (#4225)', async () => {
const { child, port } = await spawnProxyRaw({ DESIGN_CODE_PROXY_BACKEND_TIMEOUT_MS: '5000' });
try {
const deadline = Date.now() + 10000;
for (;;) {
try {
const res = await fetch(`http://127.0.0.1:${port}/health`);
if (res.ok) break;
} catch { /* not up yet */ }
if (Date.now() > deadline) throw new Error('proxy with a valid timeout did not start');
await new Promise(r => setTimeout(r, 50));
}
} finally {
child.kill('SIGKILL');
}
});