@bastani/atomic 0.6.5 → 0.6.6-1

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.
Files changed (148) hide show
  1. package/.agents/skills/ado-commit/SKILL.md +2 -0
  2. package/.agents/skills/ado-create-pr/SKILL.md +2 -0
  3. package/.agents/skills/advanced-evaluation/SKILL.md +2 -0
  4. package/.agents/skills/ast-grep/SKILL.md +2 -0
  5. package/.agents/skills/bdi-mental-states/SKILL.md +2 -0
  6. package/.agents/skills/bun/SKILL.md +156 -122
  7. package/.agents/skills/context-compression/SKILL.md +2 -0
  8. package/.agents/skills/context-degradation/SKILL.md +2 -0
  9. package/.agents/skills/context-fundamentals/SKILL.md +2 -0
  10. package/.agents/skills/context-optimization/SKILL.md +2 -0
  11. package/.agents/skills/create-spec/SKILL.md +2 -0
  12. package/.agents/skills/docx/SKILL.md +2 -0
  13. package/.agents/skills/evaluation/SKILL.md +2 -0
  14. package/.agents/skills/explain-code/SKILL.md +2 -0
  15. package/.agents/skills/filesystem-context/SKILL.md +2 -0
  16. package/.agents/skills/find-skills/SKILL.md +2 -0
  17. package/.agents/skills/gh-commit/SKILL.md +2 -0
  18. package/.agents/skills/gh-create-pr/SKILL.md +2 -0
  19. package/.agents/skills/hosted-agents/SKILL.md +2 -0
  20. package/.agents/skills/impeccable/SKILL.md +117 -304
  21. package/.agents/skills/impeccable/agents/openai.yaml +4 -0
  22. package/.agents/skills/{adapt/SKILL.md → impeccable/reference/adapt.md} +2 -11
  23. package/.agents/skills/{animate/SKILL.md → impeccable/reference/animate.md} +15 -15
  24. package/.agents/skills/{audit/SKILL.md → impeccable/reference/audit.md} +8 -22
  25. package/.agents/skills/{bolder/SKILL.md → impeccable/reference/bolder.md} +9 -13
  26. package/.agents/skills/impeccable/reference/brand.md +114 -0
  27. package/.agents/skills/{clarify/SKILL.md → impeccable/reference/clarify.md} +2 -11
  28. package/.agents/skills/{colorize/SKILL.md → impeccable/reference/colorize.md} +23 -12
  29. package/.agents/skills/impeccable/reference/craft.md +152 -29
  30. package/.agents/skills/{critique/SKILL.md → impeccable/reference/critique.md} +25 -37
  31. package/.agents/skills/{delight/SKILL.md → impeccable/reference/delight.md} +9 -11
  32. package/.agents/skills/{distill/SKILL.md → impeccable/reference/distill.md} +2 -13
  33. package/.agents/skills/impeccable/reference/document.md +427 -0
  34. package/.agents/skills/impeccable/reference/extract.md +1 -1
  35. package/.agents/skills/{harden/SKILL.md → impeccable/reference/harden.md} +1 -43
  36. package/.agents/skills/{layout/SKILL.md → impeccable/reference/layout.md} +27 -11
  37. package/.agents/skills/impeccable/reference/live.md +594 -0
  38. package/.agents/skills/impeccable/reference/motion-design.md +12 -2
  39. package/.agents/skills/impeccable/reference/onboard.md +234 -0
  40. package/.agents/skills/{optimize/SKILL.md → impeccable/reference/optimize.md} +4 -12
  41. package/.agents/skills/{overdrive/SKILL.md → impeccable/reference/overdrive.md} +9 -21
  42. package/.agents/skills/{critique → impeccable}/reference/personas.md +1 -1
  43. package/.agents/skills/{polish/SKILL.md → impeccable/reference/polish.md} +31 -23
  44. package/.agents/skills/impeccable/reference/product.md +62 -0
  45. package/.agents/skills/{quieter/SKILL.md → impeccable/reference/quieter.md} +7 -11
  46. package/.agents/skills/impeccable/reference/shape.md +151 -0
  47. package/.agents/skills/impeccable/reference/teach.md +156 -0
  48. package/.agents/skills/{typeset/SKILL.md → impeccable/reference/typeset.md} +19 -11
  49. package/.agents/skills/impeccable/reference/typography.md +31 -14
  50. package/.agents/skills/impeccable/scripts/cleanup-deprecated.mjs +87 -17
  51. package/.agents/skills/impeccable/scripts/command-metadata.json +94 -0
  52. package/.agents/skills/impeccable/scripts/design-parser.mjs +820 -0
  53. package/.agents/skills/impeccable/scripts/detect-csp.mjs +198 -0
  54. package/.agents/skills/impeccable/scripts/is-generated.mjs +69 -0
  55. package/.agents/skills/impeccable/scripts/live-accept.mjs +595 -0
  56. package/.agents/skills/impeccable/scripts/live-browser.js +4781 -0
  57. package/.agents/skills/impeccable/scripts/live-inject.mjs +445 -0
  58. package/.agents/skills/impeccable/scripts/live-poll.mjs +186 -0
  59. package/.agents/skills/impeccable/scripts/live-server.mjs +694 -0
  60. package/.agents/skills/impeccable/scripts/live-wrap.mjs +571 -0
  61. package/.agents/skills/impeccable/scripts/live.mjs +247 -0
  62. package/.agents/skills/impeccable/scripts/load-context.mjs +141 -0
  63. package/.agents/skills/impeccable/scripts/modern-screenshot.umd.js +14 -0
  64. package/.agents/skills/impeccable/scripts/pin.mjs +214 -0
  65. package/.agents/skills/init/SKILL.md +2 -0
  66. package/.agents/skills/liteparse/SKILL.md +1 -0
  67. package/.agents/skills/memory-systems/SKILL.md +2 -0
  68. package/.agents/skills/multi-agent-patterns/SKILL.md +2 -0
  69. package/.agents/skills/opentui/SKILL.md +1 -0
  70. package/.agents/skills/pdf/SKILL.md +2 -0
  71. package/.agents/skills/playwright-cli/SKILL.md +51 -5
  72. package/.agents/skills/playwright-cli/references/playwright-tests.md +1 -1
  73. package/.agents/skills/playwright-cli/references/running-code.md +10 -0
  74. package/.agents/skills/playwright-cli/references/session-management.md +56 -0
  75. package/.agents/skills/playwright-cli/references/spec-driven-testing.md +305 -0
  76. package/.agents/skills/playwright-cli/references/test-generation.md +49 -3
  77. package/.agents/skills/pptx/SKILL.md +2 -0
  78. package/.agents/skills/project-development/SKILL.md +2 -0
  79. package/.agents/skills/prompt-engineer/SKILL.md +2 -0
  80. package/.agents/skills/research-codebase/SKILL.md +2 -0
  81. package/.agents/skills/ripgrep/SKILL.md +2 -0
  82. package/.agents/skills/skill-creator/LICENSE.txt +1 -1
  83. package/.agents/skills/skill-creator/SKILL.md +2 -0
  84. package/.agents/skills/sl-commit/SKILL.md +2 -0
  85. package/.agents/skills/sl-submit-diff/SKILL.md +2 -0
  86. package/.agents/skills/tdd/SKILL.md +4 -0
  87. package/.agents/skills/tool-design/SKILL.md +2 -0
  88. package/.agents/skills/typescript-advanced-types/SKILL.md +2 -1
  89. package/.agents/skills/typescript-expert/SKILL.md +7 -1
  90. package/.agents/skills/typescript-react-reviewer/SKILL.md +2 -1
  91. package/.agents/skills/workflow-creator/SKILL.md +75 -72
  92. package/.agents/skills/workflow-creator/references/session-config.md +48 -1
  93. package/.agents/skills/xlsx/SKILL.md +2 -0
  94. package/.opencode/opencode.json +6 -2
  95. package/README.md +39 -38
  96. package/dist/lib/atomic-temp.d.ts +8 -0
  97. package/dist/lib/atomic-temp.d.ts.map +1 -0
  98. package/dist/lib/terminal-env.d.ts +9 -0
  99. package/dist/lib/terminal-env.d.ts.map +1 -0
  100. package/dist/sdk/providers/claude.d.ts.map +1 -1
  101. package/dist/sdk/providers/copilot.d.ts +24 -14
  102. package/dist/sdk/providers/copilot.d.ts.map +1 -1
  103. package/dist/sdk/runtime/executor.d.ts +8 -0
  104. package/dist/sdk/runtime/executor.d.ts.map +1 -1
  105. package/dist/sdk/runtime/port-discovery.d.ts +71 -0
  106. package/dist/sdk/runtime/port-discovery.d.ts.map +1 -0
  107. package/dist/sdk/runtime/tmux.d.ts +10 -0
  108. package/dist/sdk/runtime/tmux.d.ts.map +1 -1
  109. package/dist/sdk/types.d.ts +1 -0
  110. package/dist/sdk/types.d.ts.map +1 -1
  111. package/dist/sdk/workflows/builtin/deep-research-codebase/opencode/index.d.ts.map +1 -1
  112. package/dist/sdk/workflows/builtin/open-claude-design/opencode/index.d.ts.map +1 -1
  113. package/dist/sdk/workflows/builtin/ralph/claude/index.d.ts.map +1 -1
  114. package/dist/sdk/workflows/builtin/ralph/copilot/index.d.ts.map +1 -1
  115. package/dist/sdk/workflows/builtin/ralph/helpers/prompts.d.ts +15 -0
  116. package/dist/sdk/workflows/builtin/ralph/helpers/prompts.d.ts.map +1 -1
  117. package/dist/sdk/workflows/builtin/ralph/opencode/index.d.ts.map +1 -1
  118. package/package.json +10 -10
  119. package/src/commands/cli/chat/index.test.ts +194 -2
  120. package/src/commands/cli/chat/index.ts +83 -28
  121. package/src/lib/atomic-temp.test.ts +86 -0
  122. package/src/lib/atomic-temp.ts +62 -0
  123. package/src/lib/terminal-env.test.ts +343 -0
  124. package/src/lib/terminal-env.ts +100 -0
  125. package/src/scripts/clean-dist.test.ts +53 -0
  126. package/src/scripts/clean-dist.ts +37 -0
  127. package/src/sdk/providers/claude.ts +42 -20
  128. package/src/sdk/providers/copilot.test.ts +365 -0
  129. package/src/sdk/providers/copilot.ts +117 -15
  130. package/src/sdk/runtime/cc-debounce.ts +2 -2
  131. package/src/sdk/runtime/executor.test.ts +322 -1
  132. package/src/sdk/runtime/executor.ts +159 -96
  133. package/src/sdk/runtime/port-discovery.test.ts +573 -0
  134. package/src/sdk/runtime/port-discovery.ts +496 -0
  135. package/src/sdk/runtime/tmux.ts +22 -2
  136. package/src/sdk/types.ts +1 -0
  137. package/src/sdk/workflows/builtin/deep-research-codebase/opencode/index.ts +24 -6
  138. package/src/sdk/workflows/builtin/open-claude-design/opencode/index.ts +52 -13
  139. package/src/sdk/workflows/builtin/ralph/claude/index.ts +31 -3
  140. package/src/sdk/workflows/builtin/ralph/copilot/index.ts +16 -0
  141. package/src/sdk/workflows/builtin/ralph/helpers/prompts.ts +70 -3
  142. package/src/sdk/workflows/builtin/ralph/opencode/index.ts +50 -6
  143. package/src/services/system/auth.test.ts +53 -0
  144. package/src/services/system/auth.ts +31 -28
  145. package/src/services/system/detect.ts +1 -1
  146. package/.agents/skills/shape/SKILL.md +0 -96
  147. /package/.agents/skills/{critique → impeccable}/reference/cognitive-load.md +0 -0
  148. /package/.agents/skills/{critique → impeccable}/reference/heuristics-scoring.md +0 -0
@@ -0,0 +1,694 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Live variant mode server (self-contained, zero dependencies).
4
+ *
5
+ * Serves the browser script (/live.js), the detection overlay (/detect.js),
6
+ * uses Server-Sent Events (SSE) for server→browser push, and HTTP POST for
7
+ * browser→server events. Agent communicates via HTTP long-poll (/poll).
8
+ *
9
+ * Usage:
10
+ * node <scripts_path>/live-server.mjs # start
11
+ * node <scripts_path>/live-server.mjs stop # stop + remove injected live.js tag
12
+ * node <scripts_path>/live-server.mjs stop --keep-inject # stop only
13
+ * node <scripts_path>/live-server.mjs --help
14
+ */
15
+
16
+ import http from 'node:http';
17
+ import { randomUUID } from 'node:crypto';
18
+ import { spawn, execFileSync } from 'node:child_process';
19
+ import fs from 'node:fs';
20
+ import path from 'node:path';
21
+ import net from 'node:net';
22
+ import { fileURLToPath } from 'node:url';
23
+ import { parseDesignMd } from './design-parser.mjs';
24
+ import { resolveContextDir } from './load-context.mjs';
25
+
26
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
27
+ // PID file in the project root so both the server and agent can find it
28
+ // predictably (os.tmpdir() varies across platforms).
29
+ const LIVE_PID_FILE = path.join(process.cwd(), '.impeccable-live.json');
30
+ // PRODUCT.md / DESIGN.md / DESIGN.json live wherever load-context.mjs resolves.
31
+ // Keeps live-server in sync with the loader when users keep the docs in
32
+ // .agents/context/, docs/, or a path set via IMPECCABLE_CONTEXT_DIR.
33
+ const CONTEXT_DIR = resolveContextDir(process.cwd());
34
+ const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway
35
+ const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s
36
+
37
+ // ---------------------------------------------------------------------------
38
+ // Port detection
39
+ // ---------------------------------------------------------------------------
40
+
41
+ async function findOpenPort(start = 8400) {
42
+ return new Promise((resolve) => {
43
+ const srv = net.createServer();
44
+ srv.listen(start, '127.0.0.1', () => {
45
+ const port = srv.address().port;
46
+ srv.close(() => resolve(port));
47
+ });
48
+ srv.on('error', () => resolve(findOpenPort(start + 1)));
49
+ });
50
+ }
51
+
52
+ // ---------------------------------------------------------------------------
53
+ // Session state
54
+ // ---------------------------------------------------------------------------
55
+
56
+ const state = {
57
+ token: null,
58
+ port: null,
59
+ sseClients: new Set(), // SSE response objects (server→browser push)
60
+ pendingEvents: [], // browser events waiting for agent poll
61
+ pendingPolls: [], // agent poll callbacks waiting for browser events
62
+ exitTimer: null,
63
+ sessionDir: null, // per-session tmp dir for annotation screenshots
64
+ };
65
+
66
+ // Cap per-annotation upload size. A full 1920×1080 PNG is typically <1 MB;
67
+ // cap at 10 MB to guard against runaway writes from a misbehaving client.
68
+ const MAX_ANNOTATION_BYTES = 10 * 1024 * 1024;
69
+
70
+ function enqueueEvent(event) {
71
+ if (state.pendingPolls.length > 0) {
72
+ state.pendingPolls.shift()(event);
73
+ } else {
74
+ state.pendingEvents.push(event);
75
+ }
76
+ }
77
+
78
+ /** Push a message to all connected SSE clients. */
79
+ function broadcast(msg) {
80
+ const data = 'data: ' + JSON.stringify(msg) + '\n\n';
81
+ for (const res of state.sseClients) {
82
+ try { res.write(data); } catch { /* client gone */ }
83
+ }
84
+ }
85
+
86
+ // ---------------------------------------------------------------------------
87
+ // Load scripts
88
+ // ---------------------------------------------------------------------------
89
+
90
+ function loadBrowserScripts() {
91
+ // Detection script: look relative to the skill scripts dir, then fall back
92
+ // to the npm package location (src/detect-antipatterns-browser.js).
93
+ // This one IS cached — detect.js rarely changes during a session.
94
+ const detectPaths = [
95
+ path.join(__dirname, '..', '..', '..', '..', 'src', 'detect-antipatterns-browser.js'),
96
+ path.join(process.cwd(), 'node_modules', 'impeccable', 'src', 'detect-antipatterns-browser.js'),
97
+ ];
98
+ let detectScript = '';
99
+ for (const p of detectPaths) {
100
+ try { detectScript = fs.readFileSync(p, 'utf-8'); break; } catch { /* try next */ }
101
+ }
102
+
103
+ // live-browser.js: DO NOT cache. Return the path so the /live.js handler
104
+ // can re-read on every request. Editing the browser script during iteration
105
+ // should land on the next tab reload, not require a server restart.
106
+ const livePath = path.join(__dirname, 'live-browser.js');
107
+ if (!fs.existsSync(livePath)) {
108
+ process.stderr.write('Error: live-browser.js not found at ' + livePath + '\n');
109
+ process.exit(1);
110
+ }
111
+
112
+ return { detectScript, livePath };
113
+ }
114
+
115
+ function hasProjectContext() {
116
+ // PRODUCT.md carries brand voice / anti-references — that's what determines
117
+ // whether variants are brand-aware. DESIGN.md (visual tokens) is a separate
118
+ // concern, surfaced by the design panel's own empty state. Legacy
119
+ // .impeccable.md is auto-migrated to PRODUCT.md by load-context.mjs.
120
+ try {
121
+ fs.accessSync(path.join(CONTEXT_DIR, 'PRODUCT.md'), fs.constants.R_OK);
122
+ return true;
123
+ } catch { return false; }
124
+ }
125
+
126
+ function statOrNull(filePath) {
127
+ try { return fs.statSync(filePath); } catch { return null; }
128
+ }
129
+
130
+ // ---------------------------------------------------------------------------
131
+ // Validation (inline — no external import needed for self-contained script)
132
+ // ---------------------------------------------------------------------------
133
+
134
+ const VISUAL_ACTIONS = [
135
+ 'impeccable', 'bolder', 'quieter', 'distill', 'polish', 'typeset',
136
+ 'colorize', 'layout', 'adapt', 'animate', 'delight', 'overdrive',
137
+ ];
138
+
139
+ // Browser generates ids via crypto.randomUUID().slice(0, 8) (8 hex chars)
140
+ // and variantIds via String(small integer). Restrict to those shapes so
141
+ // any value that reaches a downstream child_process or DOM selector is
142
+ // inert by construction.
143
+ const ID_PATTERN = /^[0-9a-f]{8}$/;
144
+ const VARIANT_ID_PATTERN = /^[0-9]{1,3}$/;
145
+
146
+ function isValidId(v) { return typeof v === 'string' && ID_PATTERN.test(v); }
147
+ function isValidVariantId(v) { return typeof v === 'string' && VARIANT_ID_PATTERN.test(v); }
148
+
149
+ function validateEvent(msg) {
150
+ if (!msg || typeof msg !== 'object' || !msg.type) return 'Missing or invalid message';
151
+ switch (msg.type) {
152
+ case 'generate':
153
+ if (!isValidId(msg.id)) return 'generate: missing or malformed id';
154
+ if (!msg.action || !VISUAL_ACTIONS.includes(msg.action)) return 'generate: invalid action';
155
+ if (!Number.isInteger(msg.count) || msg.count < 1 || msg.count > 8) return 'generate: count must be 1-8';
156
+ if (!msg.element || !msg.element.outerHTML) return 'generate: missing element context';
157
+ // Optional annotation fields (all-or-nothing: if any present, all must be well-formed).
158
+ if (msg.screenshotPath !== undefined && typeof msg.screenshotPath !== 'string') return 'generate: screenshotPath must be string';
159
+ if (msg.comments !== undefined && !Array.isArray(msg.comments)) return 'generate: comments must be array';
160
+ if (msg.strokes !== undefined && !Array.isArray(msg.strokes)) return 'generate: strokes must be array';
161
+ return null;
162
+ case 'accept':
163
+ if (!isValidId(msg.id)) return 'accept: missing or malformed id';
164
+ if (!isValidVariantId(msg.variantId)) return 'accept: missing or malformed variantId';
165
+ if (msg.paramValues !== undefined) {
166
+ if (typeof msg.paramValues !== 'object' || msg.paramValues === null || Array.isArray(msg.paramValues)) {
167
+ return 'accept: paramValues must be an object';
168
+ }
169
+ }
170
+ return null;
171
+ case 'discard':
172
+ return isValidId(msg.id) ? null : 'discard: missing or malformed id';
173
+ case 'exit':
174
+ return null;
175
+ case 'prefetch':
176
+ if (!msg.pageUrl || typeof msg.pageUrl !== 'string') return 'prefetch: missing pageUrl';
177
+ return null;
178
+ default:
179
+ return 'Unknown event type: ' + msg.type;
180
+ }
181
+ }
182
+
183
+ // ---------------------------------------------------------------------------
184
+ // HTTP request handler
185
+ // ---------------------------------------------------------------------------
186
+
187
+ function createRequestHandler({ detectScript, livePath }) {
188
+ return (req, res) => {
189
+ const url = new URL(req.url, `http://localhost:${state.port}`);
190
+ res.setHeader('Access-Control-Allow-Origin', '*');
191
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
192
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
193
+ if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; }
194
+
195
+ const p = url.pathname;
196
+
197
+ // --- Scripts ---
198
+ if (p === '/live.js') {
199
+ // Re-read from disk each request so edits to live-browser.js land on
200
+ // the next tab reload. No-store headers prevent browser caching across
201
+ // sessions — during iteration, a cached old script silently breaks
202
+ // every subsequent session.
203
+ let liveScript;
204
+ try {
205
+ liveScript = fs.readFileSync(livePath, 'utf-8');
206
+ } catch (err) {
207
+ res.writeHead(500, { 'Content-Type': 'text/plain' });
208
+ res.end('Error reading live-browser.js: ' + err.message);
209
+ return;
210
+ }
211
+ const body =
212
+ `window.__IMPECCABLE_TOKEN__ = '${state.token}';\n` +
213
+ `window.__IMPECCABLE_PORT__ = ${state.port};\n` +
214
+ liveScript;
215
+ res.writeHead(200, {
216
+ 'Content-Type': 'application/javascript',
217
+ 'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
218
+ 'Pragma': 'no-cache',
219
+ });
220
+ res.end(body);
221
+ return;
222
+ }
223
+ if (p === '/detect.js' || p === '/') {
224
+ if (!detectScript) { res.writeHead(404); res.end('Not available'); return; }
225
+ res.writeHead(200, { 'Content-Type': 'application/javascript' });
226
+ res.end(detectScript);
227
+ return;
228
+ }
229
+
230
+ // --- Vendored modern-screenshot (UMD build) ---
231
+ // Lazy-loaded by live.js when the user clicks Go; exposes
232
+ // window.modernScreenshot.domToBlob(...) for capture.
233
+ if (p === '/modern-screenshot.js') {
234
+ const vendorPath = path.join(__dirname, 'modern-screenshot.umd.js');
235
+ try {
236
+ res.writeHead(200, {
237
+ 'Content-Type': 'application/javascript',
238
+ 'Cache-Control': 'public, max-age=31536000, immutable',
239
+ });
240
+ res.end(fs.readFileSync(vendorPath));
241
+ } catch {
242
+ res.writeHead(404); res.end('Vendor script not found');
243
+ }
244
+ return;
245
+ }
246
+
247
+ // --- Annotation upload (browser → server, raw PNG body) ---
248
+ // Client generates the eventId, POSTs the PNG, then POSTs the generate
249
+ // event with screenshotPath already set. Keeps bytes out of the SSE/poll
250
+ // bridge and preserves the "one shot from the user's POV" UX.
251
+ if (p === '/annotation' && req.method === 'POST') {
252
+ const token = url.searchParams.get('token');
253
+ if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
254
+ const eventId = url.searchParams.get('eventId');
255
+ if (!eventId || !/^[A-Za-z0-9_-]{1,64}$/.test(eventId)) {
256
+ res.writeHead(400, { 'Content-Type': 'application/json' });
257
+ res.end(JSON.stringify({ error: 'Invalid eventId' }));
258
+ return;
259
+ }
260
+ if ((req.headers['content-type'] || '').toLowerCase() !== 'image/png') {
261
+ res.writeHead(415, { 'Content-Type': 'application/json' });
262
+ res.end(JSON.stringify({ error: 'Content-Type must be image/png' }));
263
+ return;
264
+ }
265
+ if (!state.sessionDir) {
266
+ res.writeHead(500, { 'Content-Type': 'application/json' });
267
+ res.end(JSON.stringify({ error: 'Session dir unavailable' }));
268
+ return;
269
+ }
270
+ const chunks = [];
271
+ let total = 0;
272
+ let aborted = false;
273
+ req.on('data', (c) => {
274
+ if (aborted) return;
275
+ total += c.length;
276
+ if (total > MAX_ANNOTATION_BYTES) {
277
+ aborted = true;
278
+ res.writeHead(413, { 'Content-Type': 'application/json' });
279
+ res.end(JSON.stringify({ error: 'Payload too large' }));
280
+ req.destroy();
281
+ return;
282
+ }
283
+ chunks.push(c);
284
+ });
285
+ req.on('end', () => {
286
+ if (aborted) return;
287
+ const absPath = path.join(state.sessionDir, eventId + '.png');
288
+ try {
289
+ fs.writeFileSync(absPath, Buffer.concat(chunks));
290
+ } catch (err) {
291
+ res.writeHead(500, { 'Content-Type': 'application/json' });
292
+ res.end(JSON.stringify({ error: 'Write failed: ' + err.message }));
293
+ return;
294
+ }
295
+ res.writeHead(200, { 'Content-Type': 'application/json' });
296
+ res.end(JSON.stringify({ ok: true, path: absPath }));
297
+ });
298
+ req.on('error', () => {
299
+ if (!aborted) {
300
+ res.writeHead(500, { 'Content-Type': 'application/json' });
301
+ res.end(JSON.stringify({ error: 'Upload failed' }));
302
+ }
303
+ });
304
+ return;
305
+ }
306
+
307
+ // --- Health ---
308
+ if (p === '/health') {
309
+ res.writeHead(200, { 'Content-Type': 'application/json' });
310
+ res.end(JSON.stringify({
311
+ status: 'ok', port: state.port, mode: 'variant',
312
+ hasProjectContext: hasProjectContext(),
313
+ connectedClients: state.sseClients.size,
314
+ }));
315
+ return;
316
+ }
317
+
318
+ // --- Design system (unified v2 response) + raw ---
319
+ // /design-system.json returns both parsed DESIGN.md and DESIGN.json
320
+ // sidecar when present. Panel merges them:
321
+ // { present, parsed, sidecar, hasMd, hasSidecar,
322
+ // mdNewerThanJson, parseError?, sidecarError? }
323
+ // - parsed: output of parseDesignMd (frontmatter
324
+ // + six canonical sections) when DESIGN.md exists.
325
+ // - sidecar: DESIGN.json contents when present.
326
+ // Expected shape: schemaVersion 2, carrying
327
+ // extensions + components + narrative.
328
+ // /design-system/raw returns DESIGN.md markdown verbatim
329
+ if (p === '/design-system.json' || p === '/design-system/raw') {
330
+ const token = url.searchParams.get('token');
331
+ if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
332
+
333
+ const mdPath = path.join(CONTEXT_DIR, 'DESIGN.md');
334
+ const jsonPath = path.join(CONTEXT_DIR, 'DESIGN.json');
335
+ const mdStat = statOrNull(mdPath);
336
+ const jsonStat = statOrNull(jsonPath);
337
+
338
+ if (p === '/design-system/raw') {
339
+ if (!mdStat) { res.writeHead(404); res.end('Not found'); return; }
340
+ res.writeHead(200, { 'Content-Type': 'text/markdown; charset=utf-8' });
341
+ res.end(fs.readFileSync(mdPath, 'utf-8'));
342
+ return;
343
+ }
344
+
345
+ if (!mdStat && !jsonStat) {
346
+ res.writeHead(404, { 'Content-Type': 'application/json' });
347
+ res.end(JSON.stringify({ present: false }));
348
+ return;
349
+ }
350
+
351
+ const response = {
352
+ present: true,
353
+ hasMd: !!mdStat,
354
+ hasSidecar: !!jsonStat,
355
+ mdNewerThanJson: !!(mdStat && jsonStat && mdStat.mtimeMs > jsonStat.mtimeMs + 1000),
356
+ };
357
+
358
+ if (mdStat) {
359
+ try {
360
+ response.parsed = parseDesignMd(fs.readFileSync(mdPath, 'utf-8'));
361
+ } catch (err) {
362
+ response.parseError = err.message;
363
+ }
364
+ }
365
+
366
+ if (jsonStat) {
367
+ try {
368
+ response.sidecar = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));
369
+ } catch (err) {
370
+ response.sidecarError = 'Failed to parse DESIGN.json: ' + err.message;
371
+ }
372
+ }
373
+
374
+ res.writeHead(200, { 'Content-Type': 'application/json' });
375
+ res.end(JSON.stringify(response));
376
+ return;
377
+ }
378
+
379
+ // --- Source file (no-HMR fallback) ---
380
+ if (p === '/source') {
381
+ const token = url.searchParams.get('token');
382
+ if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
383
+ const filePath = url.searchParams.get('path');
384
+ if (!filePath || filePath.includes('..')) { res.writeHead(400); res.end('Bad path'); return; }
385
+ const absPath = path.resolve(process.cwd(), filePath);
386
+ if (!absPath.startsWith(process.cwd())) { res.writeHead(403); res.end('Forbidden'); return; }
387
+ let content;
388
+ try { content = fs.readFileSync(absPath, 'utf-8'); }
389
+ catch { res.writeHead(404); res.end('File not found'); return; }
390
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
391
+ res.end(content);
392
+ return;
393
+ }
394
+
395
+ // --- SSE: server→browser push (replaces WebSocket) ---
396
+ if (p === '/events' && req.method === 'GET') {
397
+ const token = url.searchParams.get('token');
398
+ if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
399
+ res.writeHead(200, {
400
+ 'Content-Type': 'text/event-stream',
401
+ 'Cache-Control': 'no-cache',
402
+ 'Connection': 'keep-alive',
403
+ });
404
+ res.write('data: ' + JSON.stringify({
405
+ type: 'connected',
406
+ hasProjectContext: hasProjectContext(),
407
+ }) + '\n\n');
408
+
409
+ state.sseClients.add(res);
410
+ clearTimeout(state.exitTimer);
411
+
412
+ // Keepalive: SSE comment every 30s prevents silent connection drops.
413
+ const heartbeat = setInterval(() => {
414
+ try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); }
415
+ }, SSE_HEARTBEAT_INTERVAL);
416
+
417
+ req.on('close', () => {
418
+ clearInterval(heartbeat);
419
+ state.sseClients.delete(res);
420
+ if (state.sseClients.size === 0) {
421
+ clearTimeout(state.exitTimer);
422
+ state.exitTimer = setTimeout(() => {
423
+ if (state.sseClients.size === 0) enqueueEvent({ type: 'exit' });
424
+ }, 8000);
425
+ }
426
+ });
427
+ return;
428
+ }
429
+
430
+ // --- Browser→server events (replaces WebSocket messages) ---
431
+ if (p === '/events' && req.method === 'POST') {
432
+ let body = '';
433
+ req.on('data', (c) => { body += c; });
434
+ req.on('end', () => {
435
+ let msg;
436
+ try { msg = JSON.parse(body); } catch {
437
+ res.writeHead(400, { 'Content-Type': 'application/json' });
438
+ res.end(JSON.stringify({ error: 'Invalid JSON' }));
439
+ return;
440
+ }
441
+ if (msg.token !== state.token) {
442
+ res.writeHead(401, { 'Content-Type': 'application/json' });
443
+ res.end(JSON.stringify({ error: 'Unauthorized' }));
444
+ return;
445
+ }
446
+ const error = validateEvent(msg);
447
+ if (error) {
448
+ res.writeHead(400, { 'Content-Type': 'application/json' });
449
+ res.end(JSON.stringify({ error }));
450
+ return;
451
+ }
452
+ enqueueEvent(msg);
453
+ res.writeHead(200, { 'Content-Type': 'application/json' });
454
+ res.end(JSON.stringify({ ok: true }));
455
+ });
456
+ return;
457
+ }
458
+
459
+ // --- Stop ---
460
+ if (p === '/stop') {
461
+ const token = url.searchParams.get('token');
462
+ if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
463
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
464
+ res.end('stopping');
465
+ shutdown();
466
+ return;
467
+ }
468
+
469
+ // --- Agent poll ---
470
+ if (p === '/poll' && req.method === 'GET') {
471
+ handlePollGet(req, res, url);
472
+ return;
473
+ }
474
+ if (p === '/poll' && req.method === 'POST') {
475
+ handlePollPost(req, res);
476
+ return;
477
+ }
478
+
479
+ res.writeHead(404); res.end('Not found');
480
+ };
481
+ }
482
+
483
+ // ---------------------------------------------------------------------------
484
+ // Agent poll endpoints (unchanged from WS version)
485
+ // ---------------------------------------------------------------------------
486
+
487
+ function handlePollGet(req, res, url) {
488
+ const token = url.searchParams.get('token');
489
+ if (token !== state.token) {
490
+ res.writeHead(401, { 'Content-Type': 'application/json' });
491
+ res.end(JSON.stringify({ error: 'Unauthorized' }));
492
+ return;
493
+ }
494
+ const timeout = parseInt(url.searchParams.get('timeout') || DEFAULT_POLL_TIMEOUT, 10);
495
+ if (state.pendingEvents.length > 0) {
496
+ res.writeHead(200, { 'Content-Type': 'application/json' });
497
+ res.end(JSON.stringify(state.pendingEvents.shift()));
498
+ return;
499
+ }
500
+ const timer = setTimeout(() => {
501
+ const idx = state.pendingPolls.indexOf(resolve);
502
+ if (idx !== -1) state.pendingPolls.splice(idx, 1);
503
+ res.writeHead(200, { 'Content-Type': 'application/json' });
504
+ res.end(JSON.stringify({ type: 'timeout' }));
505
+ }, timeout);
506
+ function resolve(event) {
507
+ clearTimeout(timer);
508
+ res.writeHead(200, { 'Content-Type': 'application/json' });
509
+ res.end(JSON.stringify(event));
510
+ }
511
+ state.pendingPolls.push(resolve);
512
+ req.on('close', () => {
513
+ clearTimeout(timer);
514
+ const idx = state.pendingPolls.indexOf(resolve);
515
+ if (idx !== -1) state.pendingPolls.splice(idx, 1);
516
+ });
517
+ }
518
+
519
+ function handlePollPost(req, res) {
520
+ let body = '';
521
+ req.on('data', (c) => { body += c; });
522
+ req.on('end', () => {
523
+ let msg;
524
+ try { msg = JSON.parse(body); } catch {
525
+ res.writeHead(400, { 'Content-Type': 'application/json' });
526
+ res.end(JSON.stringify({ error: 'Invalid JSON' }));
527
+ return;
528
+ }
529
+ if (msg.token !== state.token) {
530
+ res.writeHead(401, { 'Content-Type': 'application/json' });
531
+ res.end(JSON.stringify({ error: 'Unauthorized' }));
532
+ return;
533
+ }
534
+ // Forward the reply to the browser via SSE
535
+ broadcast({ type: msg.type || 'done', id: msg.id, message: msg.message, file: msg.file, data: msg.data });
536
+ res.writeHead(200, { 'Content-Type': 'application/json' });
537
+ res.end(JSON.stringify({ ok: true }));
538
+ });
539
+ }
540
+
541
+ // ---------------------------------------------------------------------------
542
+ // Lifecycle
543
+ // ---------------------------------------------------------------------------
544
+
545
+ let httpServer = null;
546
+
547
+ function shutdown() {
548
+ try { fs.unlinkSync(LIVE_PID_FILE); } catch {}
549
+ if (state.sessionDir) {
550
+ try { fs.rmSync(state.sessionDir, { recursive: true, force: true }); } catch {}
551
+ }
552
+ for (const res of state.sseClients) { try { res.end(); } catch {} }
553
+ state.sseClients.clear();
554
+ for (const resolve of state.pendingPolls) resolve({ type: 'exit' });
555
+ state.pendingPolls.length = 0;
556
+ if (httpServer) httpServer.close();
557
+ process.exit(0);
558
+ }
559
+
560
+ // ---------------------------------------------------------------------------
561
+ // Main
562
+ // ---------------------------------------------------------------------------
563
+
564
+ const args = process.argv.slice(2);
565
+
566
+ if (args.includes('--help') || args.includes('-h')) {
567
+ console.log(`Usage: node live-server.mjs [options]
568
+
569
+ Start the live variant mode server (zero dependencies).
570
+
571
+ Commands:
572
+ (default) Start the server (foreground)
573
+ stop Stop the server and remove the injected live.js script tag
574
+ stop --keep-inject Stop the server only (leave the script tag in the HTML entry)
575
+
576
+ Options:
577
+ --background Start detached, print connection JSON to stdout, then exit
578
+ --port=PORT Use a specific port (default: auto-detect starting at 8400)
579
+ --keep-inject Only with stop: skip live-inject.mjs --remove
580
+ --help Show this help
581
+
582
+ Endpoints:
583
+ /live.js Browser script (element picker + variant cycling)
584
+ /detect.js Detection overlay (backwards compatible)
585
+ /modern-screenshot.js Vendored modern-screenshot UMD build (lazy-loaded by live.js)
586
+ /annotation POST raw image/png to stage a variant screenshot
587
+ /events SSE stream (server→browser) + POST (browser→server)
588
+ /poll Long-poll for agent CLI
589
+ /source Raw source file reader (no-HMR fallback)
590
+ /health Health check`);
591
+ process.exit(0);
592
+ }
593
+
594
+ if (args.includes('stop')) {
595
+ const keepInject = args.includes('--keep-inject');
596
+ try {
597
+ const info = JSON.parse(fs.readFileSync(LIVE_PID_FILE, 'utf-8'));
598
+ const res = await fetch(`http://localhost:${info.port}/stop?token=${info.token}`);
599
+ if (res.ok) console.log(`Stopped live server on port ${info.port}.`);
600
+ } catch {
601
+ console.log('No running live server found.');
602
+ }
603
+ if (!keepInject) {
604
+ const injectPath = path.join(__dirname, 'live-inject.mjs');
605
+ try {
606
+ const out = execFileSync(process.execPath, [injectPath, '--remove'], {
607
+ encoding: 'utf-8',
608
+ cwd: process.cwd(),
609
+ });
610
+ const line = out.trim().split('\n').filter(Boolean).pop();
611
+ if (line) {
612
+ try {
613
+ const j = JSON.parse(line);
614
+ if (j.removed === true) {
615
+ console.log(`Removed live script tag from ${j.file}.`);
616
+ }
617
+ } catch {
618
+ /* ignore non-JSON lines */
619
+ }
620
+ }
621
+ } catch (err) {
622
+ const detail = err.stderr?.toString?.().trim?.()
623
+ || err.stdout?.toString?.().trim?.()
624
+ || err.message
625
+ || String(err);
626
+ console.warn(`Note: could not remove live script tag (${detail.split('\n')[0]})`);
627
+ }
628
+ }
629
+ process.exit(0);
630
+ }
631
+
632
+ // --background: spawn a detached child server, wait for it to be ready,
633
+ // print the connection JSON, then exit. This keeps the startup command
634
+ // simple (no shell backgrounding or chained commands).
635
+ if (args.includes('--background')) {
636
+ const childArgs = args.filter(a => a !== '--background');
637
+ const child = spawn(process.execPath, [fileURLToPath(import.meta.url), ...childArgs], {
638
+ detached: true,
639
+ stdio: 'ignore',
640
+ cwd: process.cwd(),
641
+ });
642
+ child.unref();
643
+
644
+ // Poll for the PID file (the child writes it once the HTTP server is listening).
645
+ const deadline = Date.now() + 10_000;
646
+ while (Date.now() < deadline) {
647
+ try {
648
+ const info = JSON.parse(fs.readFileSync(LIVE_PID_FILE, 'utf-8'));
649
+ if (info.pid !== process.pid) {
650
+ // Output JSON so the agent can read port + token from stdout.
651
+ console.log(JSON.stringify(info));
652
+ process.exit(0);
653
+ }
654
+ } catch { /* not ready yet */ }
655
+ await new Promise(r => setTimeout(r, 200));
656
+ }
657
+ console.error('Timed out waiting for live server to start.');
658
+ process.exit(1);
659
+ }
660
+
661
+ // Check for existing session
662
+ try {
663
+ const existing = JSON.parse(fs.readFileSync(LIVE_PID_FILE, 'utf-8'));
664
+ try { process.kill(existing.pid, 0);
665
+ console.error(`Live server already running on port ${existing.port} (pid ${existing.pid}).`);
666
+ console.error('Stop it first with: node ' + path.basename(fileURLToPath(import.meta.url)) + ' stop');
667
+ process.exit(1);
668
+ } catch { fs.unlinkSync(LIVE_PID_FILE); }
669
+ } catch {}
670
+
671
+ state.token = randomUUID();
672
+ const portArg = args.find(a => a.startsWith('--port='));
673
+ state.port = portArg ? parseInt(portArg.split('=')[1], 10) : await findOpenPort();
674
+ // Annotation screenshots live in the project root so the agent's Read tool
675
+ // doesn't trip a per-file permission prompt. Sessioned by token so concurrent
676
+ // projects (or quick restarts) don't collide.
677
+ const annotRoot = path.join(process.cwd(), '.impeccable-live', 'annotations');
678
+ fs.mkdirSync(annotRoot, { recursive: true });
679
+ state.sessionDir = fs.mkdtempSync(path.join(annotRoot, 'session-'));
680
+
681
+ const { detectScript, livePath } = loadBrowserScripts();
682
+ httpServer = http.createServer(createRequestHandler({ detectScript, livePath }));
683
+
684
+ httpServer.listen(state.port, '127.0.0.1', () => {
685
+ fs.writeFileSync(LIVE_PID_FILE, JSON.stringify({ pid: process.pid, port: state.port, token: state.token }));
686
+ const url = `http://localhost:${state.port}`;
687
+ console.log(`\nImpeccable live server running on ${url}`);
688
+ console.log(`Token: ${state.token}\n`);
689
+ console.log(`Inject: <script src="${url}/live.js"><\/script>`);
690
+ console.log(`Stop: node ${path.basename(fileURLToPath(import.meta.url))} stop`);
691
+ });
692
+
693
+ process.on('SIGINT', shutdown);
694
+ process.on('SIGTERM', shutdown);