From 9b573de1fb847c3c9e19602f3e758e7d54524806 Mon Sep 17 00:00:00 2001 From: Paul Bakaus Date: Mon, 13 Apr 2026 14:16:07 -0700 Subject: [PATCH] Add ADR for live variant mode architecture Comprehensive architecture decision record covering the live variant mode: context, key decisions (source modification over DOM patching, SSE over WebSocket, self-contained skill scripts, HTTP long-poll for agent), full architecture diagram with message flows, variant wrapper format, browser UI states, session persistence, security model, server resilience, performance optimizations, test coverage, known limitations, and future work. Also picks up improvements from parallel thread: poll timeout bumped to 10 min, SSE heartbeat every 30s, and other minor fixes. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../skills/impeccable/scripts/live-browser.js | 6 +- .../skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- .../skills/impeccable/scripts/live-browser.js | 6 +- .../skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- .../skills/impeccable/scripts/live-browser.js | 6 +- .../skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- .../skills/impeccable/scripts/live-browser.js | 6 +- .../skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- .../skills/impeccable/scripts/live-browser.js | 6 +- .../skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- .../skills/impeccable/scripts/live-browser.js | 6 +- .kiro/skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- .../skills/impeccable/scripts/live-browser.js | 6 +- .../skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- .pi/skills/impeccable/scripts/live-browser.js | 6 +- .pi/skills/impeccable/scripts/live-poll.mjs | 2 +- .pi/skills/impeccable/scripts/live-server.mjs | 9 +- .../skills/impeccable/scripts/live-browser.js | 6 +- .../skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- .../skills/impeccable/scripts/live-browser.js | 6 +- .../skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- .../skills/impeccable/scripts/live-browser.js | 6 +- .trae/skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- docs/adr-live-variant-mode.md | 269 ++++++++++++++++++ .../skills/impeccable/scripts/live-browser.js | 6 +- .../skills/impeccable/scripts/live-poll.mjs | 2 +- .../skills/impeccable/scripts/live-server.mjs | 9 +- 37 files changed, 437 insertions(+), 36 deletions(-) create mode 100644 docs/adr-live-variant-mode.md diff --git a/.agents/skills/impeccable/scripts/live-browser.js b/.agents/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.agents/skills/impeccable/scripts/live-browser.js +++ b/.agents/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.agents/skills/impeccable/scripts/live-poll.mjs b/.agents/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.agents/skills/impeccable/scripts/live-poll.mjs +++ b/.agents/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.agents/skills/impeccable/scripts/live-server.mjs b/.agents/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.agents/skills/impeccable/scripts/live-server.mjs +++ b/.agents/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/.claude/skills/impeccable/scripts/live-browser.js b/.claude/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.claude/skills/impeccable/scripts/live-browser.js +++ b/.claude/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.claude/skills/impeccable/scripts/live-poll.mjs b/.claude/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.claude/skills/impeccable/scripts/live-poll.mjs +++ b/.claude/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.claude/skills/impeccable/scripts/live-server.mjs b/.claude/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.claude/skills/impeccable/scripts/live-server.mjs +++ b/.claude/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/.cursor/skills/impeccable/scripts/live-browser.js b/.cursor/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.cursor/skills/impeccable/scripts/live-browser.js +++ b/.cursor/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.cursor/skills/impeccable/scripts/live-poll.mjs b/.cursor/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.cursor/skills/impeccable/scripts/live-poll.mjs +++ b/.cursor/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.cursor/skills/impeccable/scripts/live-server.mjs b/.cursor/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.cursor/skills/impeccable/scripts/live-server.mjs +++ b/.cursor/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/.gemini/skills/impeccable/scripts/live-browser.js b/.gemini/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.gemini/skills/impeccable/scripts/live-browser.js +++ b/.gemini/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.gemini/skills/impeccable/scripts/live-poll.mjs b/.gemini/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.gemini/skills/impeccable/scripts/live-poll.mjs +++ b/.gemini/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.gemini/skills/impeccable/scripts/live-server.mjs b/.gemini/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.gemini/skills/impeccable/scripts/live-server.mjs +++ b/.gemini/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/.github/skills/impeccable/scripts/live-browser.js b/.github/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.github/skills/impeccable/scripts/live-browser.js +++ b/.github/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.github/skills/impeccable/scripts/live-poll.mjs b/.github/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.github/skills/impeccable/scripts/live-poll.mjs +++ b/.github/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.github/skills/impeccable/scripts/live-server.mjs b/.github/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.github/skills/impeccable/scripts/live-server.mjs +++ b/.github/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/.kiro/skills/impeccable/scripts/live-browser.js b/.kiro/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.kiro/skills/impeccable/scripts/live-browser.js +++ b/.kiro/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.kiro/skills/impeccable/scripts/live-poll.mjs b/.kiro/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.kiro/skills/impeccable/scripts/live-poll.mjs +++ b/.kiro/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.kiro/skills/impeccable/scripts/live-server.mjs b/.kiro/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.kiro/skills/impeccable/scripts/live-server.mjs +++ b/.kiro/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/.opencode/skills/impeccable/scripts/live-browser.js b/.opencode/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.opencode/skills/impeccable/scripts/live-browser.js +++ b/.opencode/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.opencode/skills/impeccable/scripts/live-poll.mjs b/.opencode/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.opencode/skills/impeccable/scripts/live-poll.mjs +++ b/.opencode/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.opencode/skills/impeccable/scripts/live-server.mjs b/.opencode/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.opencode/skills/impeccable/scripts/live-server.mjs +++ b/.opencode/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/.pi/skills/impeccable/scripts/live-browser.js b/.pi/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.pi/skills/impeccable/scripts/live-browser.js +++ b/.pi/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.pi/skills/impeccable/scripts/live-poll.mjs b/.pi/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.pi/skills/impeccable/scripts/live-poll.mjs +++ b/.pi/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.pi/skills/impeccable/scripts/live-server.mjs b/.pi/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.pi/skills/impeccable/scripts/live-server.mjs +++ b/.pi/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/.rovodev/skills/impeccable/scripts/live-browser.js b/.rovodev/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.rovodev/skills/impeccable/scripts/live-browser.js +++ b/.rovodev/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.rovodev/skills/impeccable/scripts/live-poll.mjs b/.rovodev/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.rovodev/skills/impeccable/scripts/live-poll.mjs +++ b/.rovodev/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.rovodev/skills/impeccable/scripts/live-server.mjs b/.rovodev/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.rovodev/skills/impeccable/scripts/live-server.mjs +++ b/.rovodev/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/.trae-cn/skills/impeccable/scripts/live-browser.js b/.trae-cn/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.trae-cn/skills/impeccable/scripts/live-browser.js +++ b/.trae-cn/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.trae-cn/skills/impeccable/scripts/live-poll.mjs b/.trae-cn/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.trae-cn/skills/impeccable/scripts/live-poll.mjs +++ b/.trae-cn/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.trae-cn/skills/impeccable/scripts/live-server.mjs b/.trae-cn/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.trae-cn/skills/impeccable/scripts/live-server.mjs +++ b/.trae-cn/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/.trae/skills/impeccable/scripts/live-browser.js b/.trae/skills/impeccable/scripts/live-browser.js index eacda98de..6297eccfc 100644 --- a/.trae/skills/impeccable/scripts/live-browser.js +++ b/.trae/skills/impeccable/scripts/live-browser.js @@ -874,11 +874,15 @@ let evtSource = null; let sseRetries = 0; - const SSE_MAX_RETRIES = 5; + const SSE_MAX_RETRIES = 20; // generous: heartbeats keep the connection alive, so retries mean real trouble function connectSSE() { evtSource = new EventSource('http://localhost:' + PORT + '/events?token=' + TOKEN); + evtSource.onopen = () => { + sseRetries = 0; // reset on successful (re)connect + }; + evtSource.onmessage = (e) => { sseRetries = 0; // reset on any successful message let msg; try { msg = JSON.parse(e.data); } catch { return; } diff --git a/.trae/skills/impeccable/scripts/live-poll.mjs b/.trae/skills/impeccable/scripts/live-poll.mjs index be2c1dcc7..f868f1e43 100644 --- a/.trae/skills/impeccable/scripts/live-poll.mjs +++ b/.trae/skills/impeccable/scripts/live-poll.mjs @@ -93,7 +93,7 @@ Options: // Poll mode: block until browser event const timeoutArg = args.find(a => a.startsWith('--timeout=')); - const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 120000; + const timeout = timeoutArg ? parseInt(timeoutArg.split('=')[1], 10) : 600000; try { const res = await fetch(`${base}/poll?token=${info.token}&timeout=${timeout}`); diff --git a/.trae/skills/impeccable/scripts/live-server.mjs b/.trae/skills/impeccable/scripts/live-server.mjs index 6051cf40f..998651d85 100644 --- a/.trae/skills/impeccable/scripts/live-server.mjs +++ b/.trae/skills/impeccable/scripts/live-server.mjs @@ -24,7 +24,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // PID file in the project root so both the server and agent can find it // predictably (os.tmpdir() varies across platforms). const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json'); -const DEFAULT_POLL_TIMEOUT = 120_000; +const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway +const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s // --------------------------------------------------------------------------- // Port detection @@ -206,7 +207,13 @@ function createRequestHandler({ detectScript, liveScriptWithToken }) { state.sseClients.add(res); clearTimeout(state.exitTimer); + // Keepalive: SSE comment every 30s prevents silent connection drops. + const heartbeat = setInterval(() => { + try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); } + }, SSE_HEARTBEAT_INTERVAL); + req.on('close', () => { + clearInterval(heartbeat); state.sseClients.delete(res); if (state.sseClients.size === 0) { clearTimeout(state.exitTimer); diff --git a/docs/adr-live-variant-mode.md b/docs/adr-live-variant-mode.md new file mode 100644 index 000000000..82c978c01 --- /dev/null +++ b/docs/adr-live-variant-mode.md @@ -0,0 +1,269 @@ +# ADR: Live Variant Mode + +**Status:** Implemented (v3.1, feature branch `feature/single-skill-consolidation`) +**Date:** 2026-04-12 +**Author:** Paul Bakaus + Claude + +## Context + +Impeccable is a design skill for AI coding agents. It teaches AI harnesses (Claude Code, Cursor, Gemini CLI, Codex, etc.) how to produce better frontend design. The skill has 22 commands (bolder, quieter, polish, typeset, etc.) that the agent runs on source code. + +The missing piece: there was no way to visually iterate on a live page. The user could ask the agent to "make this bolder," but they had to read the code diff, reload the page, and decide if they liked it. If not, they'd ask again, wait, reload, repeat. Slow and disconnected. + +**Goal:** Let the user select an element directly in the browser, pick a design action, and see N real HTML+CSS variants hot-swapped in. Cycle through them visually, accept or discard, repeat. The agent generates the variants; the browser shows them. + +## Decision + +Build a self-contained live variant mode that ships as part of the impeccable skill (no separate npm install required). The system bridges three parties: the **browser** (where the user picks elements and cycles variants), the **server** (a localhost HTTP server that relays messages), and the **agent** (the AI that generates variants by modifying source files). + +### Key architectural decisions + +**1. Source modification, not DOM patching.** +Variants are written to the actual source file, not injected into the browser DOM. This means: +- Framework state (React, Vue, etc.) is preserved because the framework's own rendering pipeline handles the update via HMR. +- "Accept" is trivial: the winning variant is already in the source. Just remove the other variants. +- Variants are real code that the user can inspect in their editor, diff, and commit. + +**2. SSE + fetch, not WebSocket.** +Server-Sent Events (server to browser) + fetch POST (browser to server) instead of WebSocket. This eliminates the `ws` npm dependency entirely. The server is zero-dependency pure Node.js (http, crypto, fs, net, os). This matters because the scripts ship inside the skill directory and run in the user's project without any package installation. + +**3. Self-contained skill scripts.** +All live mode code lives in `source/skills/impeccable/scripts/`: +- `live-server.mjs` — HTTP server (SSE, poll, source file reader) +- `live-poll.mjs` — CLI client for the agent poll/reply loop +- `live-wrap.mjs` — CLI helper that finds elements in source and creates variant wrappers +- `live-browser.js` — Browser script (element picker, action panel, variant cycler, global bar) + +When a user installs the skill via `npx skills add pbakaus/impeccable`, they get the live mode without any additional setup. The agent runs the scripts via `node {{scripts_path}}/live-server.mjs`. + +**4. HTTP long-poll for the agent, not WebSocket or stdin.** +The agent communicates with the server via HTTP long-poll (`GET /poll` blocks until a browser event arrives). This works across all AI harnesses because every harness can run a shell command and read its stdout. No harness-specific integration needed. + +**5. `display: contents` variant wrapper.** +Variants are wrapped in a container with `display: contents`, which makes the wrapper invisible to CSS layout. The selected element's relationship with its parent (flex child, grid child, etc.) is preserved. The wrapper carries `data-impeccable-variants` and `data-impeccable-variant-count` attributes that the browser script uses to detect and cycle variants. + +**6. No-HMR fallback.** +For dev servers that don't support HMR (like Bun's static HTML import), the browser fetches the raw source file directly from the live server's `/source` endpoint and injects the variants into the DOM. This works universally, at the cost of losing framework state on that injection. + +## Architecture + +``` + ┌─────────────────────────────────────────────────────────────────┐ + │ BROWSER │ + │ │ + │ live-browser.js (injected via