claudecode-omc 5.6.8 → 5.11.0
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.
- package/.local/settings/settings.json +8 -0
- package/.local/skills/prompt-optimizer/SKILL.md +262 -19
- package/.omc-curation/ecc-selection.json +80 -0
- package/.omc-curation/governance.json +116 -0
- package/.omc-curation/sources.lock.json +30 -0
- package/README.md +78 -4
- package/bundled/manifest.json +6 -5
- package/bundled/upstream/anthropic-skills/.omc-source/bundle.json +18 -0
- package/bundled/upstream/anthropic-skills/.omc-source/provenance.json +399 -0
- package/bundled/upstream/anthropic-skills/skills/claude-api/SKILL.md +18 -17
- package/bundled/upstream/anthropic-skills/skills/claude-api/curl/examples.md +9 -9
- package/bundled/upstream/anthropic-skills/skills/claude-api/curl/managed-agents.md +4 -4
- package/bundled/upstream/anthropic-skills/skills/claude-api/go/managed-agents/README.md +2 -2
- package/bundled/upstream/anthropic-skills/skills/claude-api/java/claude-api.md +2 -2
- package/bundled/upstream/anthropic-skills/skills/claude-api/java/managed-agents/README.md +2 -2
- package/bundled/upstream/anthropic-skills/skills/claude-api/php/claude-api.md +10 -10
- package/bundled/upstream/anthropic-skills/skills/claude-api/php/managed-agents/README.md +2 -2
- package/bundled/upstream/anthropic-skills/skills/claude-api/python/claude-api/README.md +16 -16
- package/bundled/upstream/anthropic-skills/skills/claude-api/python/claude-api/batches.md +3 -3
- package/bundled/upstream/anthropic-skills/skills/claude-api/python/claude-api/files-api.md +3 -3
- package/bundled/upstream/anthropic-skills/skills/claude-api/python/claude-api/streaming.md +7 -7
- package/bundled/upstream/anthropic-skills/skills/claude-api/python/claude-api/tool-use.md +19 -19
- package/bundled/upstream/anthropic-skills/skills/claude-api/python/managed-agents/README.md +3 -3
- package/bundled/upstream/anthropic-skills/skills/claude-api/ruby/claude-api.md +4 -4
- package/bundled/upstream/anthropic-skills/skills/claude-api/ruby/managed-agents/README.md +2 -2
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/error-codes.md +5 -5
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/live-sources.md +3 -1
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/managed-agents-api-reference.md +10 -4
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/managed-agents-core.md +19 -1
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/managed-agents-environments.md +6 -2
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/managed-agents-multiagent.md +1 -1
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/managed-agents-onboarding.md +3 -3
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/managed-agents-overview.md +3 -2
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/managed-agents-self-hosted-sandboxes.md +173 -0
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/managed-agents-tools.md +10 -4
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/model-migration.md +113 -13
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/models.md +14 -11
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/prompt-caching.md +2 -2
- package/bundled/upstream/anthropic-skills/skills/claude-api/shared/tool-use-concepts.md +4 -4
- package/bundled/upstream/anthropic-skills/skills/claude-api/typescript/claude-api/README.md +15 -15
- package/bundled/upstream/anthropic-skills/skills/claude-api/typescript/claude-api/batches.md +2 -2
- package/bundled/upstream/anthropic-skills/skills/claude-api/typescript/claude-api/files-api.md +1 -1
- package/bundled/upstream/anthropic-skills/skills/claude-api/typescript/claude-api/streaming.md +5 -5
- package/bundled/upstream/anthropic-skills/skills/claude-api/typescript/claude-api/tool-use.md +15 -15
- package/bundled/upstream/anthropic-skills/skills/claude-api/typescript/managed-agents/README.md +3 -3
- package/bundled/upstream/ecc/.omc-source/bundle.json +2 -1
- package/bundled/upstream/ecc/.omc-source/last-plan-apply.json +108 -24
- package/bundled/upstream/ecc/.omc-source/manifests/.claude-plugin/marketplace.json +3 -3
- package/bundled/upstream/ecc/.omc-source/provenance.json +563 -0
- package/bundled/upstream/ecc/agents/marketing-agent.md +159 -0
- package/bundled/upstream/ecc/agents/react-build-resolver.md +215 -0
- package/bundled/upstream/ecc/agents/react-reviewer.md +167 -0
- package/bundled/upstream/ecc/agents/typescript-reviewer.md +3 -0
- package/bundled/upstream/ecc/commands/harness-audit.md +17 -10
- package/bundled/upstream/ecc/commands/marketing-campaign.md +129 -0
- package/bundled/upstream/ecc/commands/react-build.md +187 -0
- package/bundled/upstream/ecc/commands/react-review.md +170 -0
- package/bundled/upstream/ecc/commands/react-test.md +265 -0
- package/bundled/upstream/ecc/skills/benchmark-optimization-loop/SKILL.md +69 -0
- package/bundled/upstream/ecc/skills/blender-motion-state-inspection/SKILL.md +164 -0
- package/bundled/upstream/ecc/skills/canary-watch/SKILL.md +9 -1
- package/bundled/upstream/ecc/skills/continuous-learning-v2/hooks/observe.sh +31 -9
- package/bundled/upstream/ecc/skills/continuous-learning-v2/scripts/detect-project.sh +38 -4
- package/bundled/upstream/ecc/skills/continuous-learning-v2/scripts/instinct-cli.py +319 -12
- package/bundled/upstream/ecc/skills/data-throughput-accelerator/SKILL.md +72 -0
- package/bundled/upstream/ecc/skills/dynamic-workflow-mode/SKILL.md +123 -0
- package/bundled/upstream/ecc/skills/frontend-a11y/SKILL.md +446 -0
- package/bundled/upstream/ecc/skills/ito-basket-compare/SKILL.md +63 -0
- package/bundled/upstream/ecc/skills/ito-data-atlas-agent/SKILL.md +63 -0
- package/bundled/upstream/ecc/skills/ito-market-intelligence/SKILL.md +60 -0
- package/bundled/upstream/ecc/skills/ito-trade-planner/SKILL.md +67 -0
- package/bundled/upstream/ecc/skills/latency-critical-systems/SKILL.md +73 -0
- package/bundled/upstream/ecc/skills/marketing-campaign/SKILL.md +113 -0
- package/bundled/upstream/ecc/skills/nextjs-turbopack/SKILL.md +13 -0
- package/bundled/upstream/ecc/skills/parallel-execution-optimizer/SKILL.md +72 -0
- package/bundled/upstream/ecc/skills/prediction-market-oracle-research/SKILL.md +63 -0
- package/bundled/upstream/ecc/skills/prediction-market-risk-review/SKILL.md +60 -0
- package/bundled/upstream/ecc/skills/react-patterns/SKILL.md +341 -0
- package/bundled/upstream/ecc/skills/react-performance/SKILL.md +574 -0
- package/bundled/upstream/ecc/skills/react-testing/SKILL.md +423 -0
- package/bundled/upstream/ecc/skills/recsys-pipeline-architect/SKILL.md +114 -0
- package/bundled/upstream/ecc/skills/recursive-decision-ledger/SKILL.md +79 -0
- package/bundled/upstream/ecc/skills/social-publisher/SKILL.md +115 -0
- package/bundled/upstream/ecc/skills/team-agent-orchestration/SKILL.md +110 -0
- package/bundled/upstream/ecc/skills/uncloud/SKILL.md +343 -0
- package/bundled/upstream/ecc/skills/windows-desktop-e2e/SKILL.md +99 -0
- package/bundled/upstream/impeccable/.omc-source/bundle.json +20 -0
- package/bundled/upstream/impeccable/.omc-source/provenance.json +105 -0
- package/bundled/upstream/impeccable/agents/impeccable-manual-edit-applier.md +97 -0
- package/bundled/upstream/impeccable/skills/impeccable/SKILL.md +168 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/adapt.md +311 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/animate.md +201 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/audit.md +133 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/bolder.md +113 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/brand.md +108 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/clarify.md +288 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/codex.md +105 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/colorize.md +257 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/craft.md +123 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/critique.md +767 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/delight.md +302 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/distill.md +111 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/document.md +429 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/extract.md +69 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/harden.md +347 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/hooks.md +88 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/init.md +172 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/interaction-design.md +189 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/layout.md +161 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/live.md +718 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/onboard.md +234 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/optimize.md +258 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/overdrive.md +130 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/polish.md +241 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/product.md +60 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/quieter.md +99 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/shape.md +165 -0
- package/bundled/upstream/impeccable/skills/impeccable/reference/typeset.md +279 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/command-metadata.json +94 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/context-signals.mjs +225 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/context.mjs +280 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/critique-storage.mjs +242 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detect-csp.mjs +198 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detect.mjs +21 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/browser/injected/index.mjs +1735 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/cli/main.mjs +244 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/detect-antipatterns-browser.js +4907 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/detect-antipatterns.mjs +43 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/engines/browser/detect-url.mjs +252 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/engines/regex/detect-text.mjs +552 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/engines/static-html/css-cascade.mjs +1013 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/engines/static-html/detect-html.mjs +208 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/engines/visual/screenshot-contrast.mjs +189 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/findings.mjs +12 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/node/file-system.mjs +198 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/profile/profiler.mjs +166 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/registry/antipatterns.mjs +419 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/rules/checks.mjs +2671 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/shared/color.mjs +124 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/shared/constants.mjs +101 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/detector/shared/page.mjs +7 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/hook-admin.mjs +574 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/hook-before-edit.mjs +473 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/hook-lib.mjs +1286 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/hook.mjs +61 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/lib/design-parser.mjs +835 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/lib/impeccable-paths.mjs +126 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/lib/is-generated.mjs +69 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/browser-script-parts.mjs +49 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/completion.mjs +19 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/event-validation.mjs +137 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/insert-ui.mjs +458 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/manual-apply.mjs +939 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/manual-edit-routes.mjs +357 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/manual-edits-buffer.mjs +152 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/session-store.mjs +289 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/svelte-component.mjs +826 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/sveltekit-adapter.mjs +274 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/ui-core.mjs +180 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live/vocabulary.mjs +36 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-accept.mjs +812 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-browser-dom.js +146 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-browser-session.js +123 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-browser.js +11086 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-commit-manual-edits.mjs +1241 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-complete.mjs +75 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-copy-edit-agent.mjs +683 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-discard-manual-edits.mjs +51 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-inject.mjs +583 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-insert.mjs +272 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-manual-edit-evidence.mjs +363 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-poll.mjs +379 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-resume.mjs +94 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-server.mjs +1134 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-status.mjs +61 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live-wrap.mjs +894 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/live.mjs +246 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/modern-screenshot.umd.js +14 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/palette.mjs +633 -0
- package/bundled/upstream/impeccable/skills/impeccable/scripts/pin.mjs +214 -0
- package/bundled/upstream/oh-my-claudecode/.omc-source/bundle.json +2 -1
- package/bundled/upstream/oh-my-claudecode/.omc-source/provenance.json +116 -0
- package/bundled/upstream/oh-my-claudecode/skills/autopilot/SKILL.md +7 -0
- package/bundled/upstream/oh-my-claudecode/skills/cancel/SKILL.md +1 -0
- package/bundled/upstream/oh-my-claudecode/skills/deep-interview/SKILL.md +39 -5
- package/bundled/upstream/oh-my-claudecode/skills/hud/SKILL.md +1 -0
- package/bundled/upstream/oh-my-claudecode/skills/local-build-reminder/SKILL.md +78 -0
- package/bundled/upstream/oh-my-claudecode/skills/omc-doctor/SKILL.md +1 -1
- package/bundled/upstream/oh-my-claudecode/skills/omc-setup/SKILL.md +26 -10
- package/bundled/upstream/oh-my-claudecode/skills/omc-setup/phases/01-install-claude-md.md +3 -3
- package/bundled/upstream/oh-my-claudecode/skills/omc-setup/phases/02-configure.md +6 -4
- package/bundled/upstream/oh-my-claudecode/skills/omc-setup/phases/03-integrations.md +1 -1
- package/bundled/upstream/oh-my-claudecode/skills/omc-setup/phases/04-welcome.md +2 -2
- package/bundled/upstream/oh-my-claudecode/skills/omc-teams/SKILL.md +6 -6
- package/bundled/upstream/oh-my-claudecode/skills/plan/SKILL.md +44 -32
- package/bundled/upstream/oh-my-claudecode/skills/ralph/SKILL.md +45 -21
- package/bundled/upstream/oh-my-claudecode/skills/ralplan/SKILL.md +1 -1
- package/bundled/upstream/oh-my-claudecode/skills/self-improve/SKILL.md +7 -0
- package/bundled/upstream/oh-my-claudecode/skills/self-improve/scripts/resolve-paths.mjs +39 -15
- package/bundled/upstream/oh-my-claudecode/skills/team/SKILL.md +132 -90
- package/bundled/upstream/oh-my-claudecode/skills/ultragoal/SKILL.md +93 -0
- package/bundled/upstream/oh-my-claudecode/skills/ultraqa/SKILL.md +28 -13
- package/bundled/upstream/oh-my-claudecode/skills/ultrawork/SKILL.md +7 -0
- package/bundled/upstream/superpowers/.omc-source/bundle.json +2 -1
- package/bundled/upstream/superpowers/.omc-source/provenance.json +63 -0
- package/package.json +2 -1
- package/src/catalog/source-catalog.js +10 -4
- package/src/cli/index.js +4 -0
- package/src/cli/plan.js +14 -2
- package/src/cli/setup.js +52 -13
- package/src/cli/skill.js +1 -1
- package/src/cli/source.js +271 -14
- package/src/config/sources.js +82 -1
- package/src/merge/content-patch.js +88 -0
- package/templates/merge-config.json +1 -8
- package/bundled/upstream/ecc/skills/strategic-compact/suggest-compact.sh +0 -54
|
@@ -0,0 +1,1134 @@
|
|
|
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 './lib/design-parser.mjs';
|
|
24
|
+
import { resolveContextDir } from './context.mjs';
|
|
25
|
+
import {
|
|
26
|
+
assembleLiveBrowserScript,
|
|
27
|
+
assertLiveBrowserScriptParts,
|
|
28
|
+
readLiveBrowserScriptParts,
|
|
29
|
+
resolveLiveBrowserScriptParts,
|
|
30
|
+
} from './live/browser-script-parts.mjs';
|
|
31
|
+
import { createLiveSessionStore } from './live/session-store.mjs';
|
|
32
|
+
import { validateEvent } from './live/event-validation.mjs';
|
|
33
|
+
import { createManualEditRoutes } from './live/manual-edit-routes.mjs';
|
|
34
|
+
import { LIVE_COMMANDS } from './live/vocabulary.mjs';
|
|
35
|
+
import {
|
|
36
|
+
getDesignSidecarPath,
|
|
37
|
+
getLiveDir,
|
|
38
|
+
getLiveAnnotationsDir,
|
|
39
|
+
readLiveServerInfo,
|
|
40
|
+
removeLiveServerInfo,
|
|
41
|
+
resolveDesignSidecarPath,
|
|
42
|
+
writeLiveServerInfo,
|
|
43
|
+
} from './lib/impeccable-paths.mjs';
|
|
44
|
+
import { countByPage as countPendingByPage } from './live/manual-edits-buffer.mjs';
|
|
45
|
+
import {
|
|
46
|
+
createManualApplyController,
|
|
47
|
+
summarizeManualApplyFailures,
|
|
48
|
+
} from './live/manual-apply.mjs';
|
|
49
|
+
import {
|
|
50
|
+
applyDeferredSvelteComponentAccepts,
|
|
51
|
+
removeAllSvelteComponentSessions,
|
|
52
|
+
} from './live/svelte-component.mjs';
|
|
53
|
+
|
|
54
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
55
|
+
// PRODUCT.md / DESIGN.md live wherever context.mjs resolves. The generated
|
|
56
|
+
// DESIGN sidecar is project-local at .impeccable/design.json, with legacy
|
|
57
|
+
// DESIGN.json fallback for existing projects.
|
|
58
|
+
const CONTEXT_DIR = resolveContextDir(process.cwd());
|
|
59
|
+
const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway
|
|
60
|
+
const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s
|
|
61
|
+
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// Port detection
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
async function findOpenPort(start = 8400) {
|
|
67
|
+
return new Promise((resolve) => {
|
|
68
|
+
const srv = net.createServer();
|
|
69
|
+
srv.listen(start, '127.0.0.1', () => {
|
|
70
|
+
const port = srv.address().port;
|
|
71
|
+
srv.close(() => resolve(port));
|
|
72
|
+
});
|
|
73
|
+
srv.on('error', () => resolve(findOpenPort(start + 1)));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ---------------------------------------------------------------------------
|
|
78
|
+
// Session state
|
|
79
|
+
// ---------------------------------------------------------------------------
|
|
80
|
+
|
|
81
|
+
const state = {
|
|
82
|
+
token: null,
|
|
83
|
+
port: null,
|
|
84
|
+
sseClients: new Set(), // SSE response objects (server→browser push)
|
|
85
|
+
pendingEvents: [], // browser events waiting for agent ack ({ event, leaseUntil })
|
|
86
|
+
pendingPolls: [], // agent poll callbacks waiting for browser events
|
|
87
|
+
nextEventSeq: 1,
|
|
88
|
+
lastAgentPollingBroadcast: null,
|
|
89
|
+
exitTimer: null,
|
|
90
|
+
sessionDir: null, // per-session tmp dir for annotation screenshots
|
|
91
|
+
sessionStore: null,
|
|
92
|
+
leaseTimer: null,
|
|
93
|
+
manualEditActivity: null,
|
|
94
|
+
nextManualEditSeq: 1,
|
|
95
|
+
// Deferreds for in-flight chat-routed Apply events. Keyed by event id; each
|
|
96
|
+
// entry is resolved when the chat agent POSTs an ack carrying the batch
|
|
97
|
+
// result, or rejected when the hard timeout fires.
|
|
98
|
+
pendingApplyDeferreds: new Map(),
|
|
99
|
+
// Updated whenever a /poll long-poll request arrives or is resolved with an
|
|
100
|
+
// event. Used to detect "a chat agent is likely attached" without requiring
|
|
101
|
+
// a poll to be parked at the exact moment we dispatch.
|
|
102
|
+
lastPollAt: 0,
|
|
103
|
+
timedOutApplyIds: new Map(),
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const CHAT_POLL_FRESHNESS_MS = 60_000;
|
|
107
|
+
const POLL_LEASE_EXPIRY_TIMER_GRACE_MS = 2;
|
|
108
|
+
const DEBUG_MANUAL_EDIT_EVENTS = /^(1|true|yes)$/i.test(process.env.IMPECCABLE_LIVE_DEBUG_EVENTS || '');
|
|
109
|
+
|
|
110
|
+
const manualApply = createManualApplyController({
|
|
111
|
+
pendingEvents: state.pendingEvents,
|
|
112
|
+
pendingApplyDeferreds: state.pendingApplyDeferreds,
|
|
113
|
+
timedOutApplyIds: state.timedOutApplyIds,
|
|
114
|
+
enqueueEvent,
|
|
115
|
+
acknowledgePendingEvent,
|
|
116
|
+
flushPendingPolls,
|
|
117
|
+
recordManualEditActivity,
|
|
118
|
+
cwd: () => process.cwd(),
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const manualEditRoutes = createManualEditRoutes({
|
|
122
|
+
getToken: () => state.token,
|
|
123
|
+
manualApply,
|
|
124
|
+
recordManualEditActivity,
|
|
125
|
+
getManualEditStatus,
|
|
126
|
+
chatAgentLikelyActive,
|
|
127
|
+
cwd: () => process.cwd(),
|
|
128
|
+
env: () => process.env,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
function chatAgentLikelyActive() {
|
|
132
|
+
if (state.pendingPolls.length > 0) return true;
|
|
133
|
+
if (!state.lastPollAt) return false;
|
|
134
|
+
return Date.now() - state.lastPollAt < CHAT_POLL_FRESHNESS_MS;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Cap per-annotation upload size. A full 1920×1080 PNG is typically <1 MB;
|
|
138
|
+
// cap at 10 MB to guard against runaway writes from a misbehaving client.
|
|
139
|
+
const MAX_ANNOTATION_BYTES = 10 * 1024 * 1024;
|
|
140
|
+
|
|
141
|
+
function enqueueEvent(event) {
|
|
142
|
+
if (!event || (event.id && state.pendingEvents.some((entry) => entry.event?.id === event.id && entry.event?.type === event.type))) return;
|
|
143
|
+
state.pendingEvents.push({ event, leaseUntil: 0, seq: state.nextEventSeq++ });
|
|
144
|
+
flushPendingPolls();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function restorePendingEventsFromStore() {
|
|
148
|
+
if (!state.sessionStore) return;
|
|
149
|
+
for (const snapshot of state.sessionStore.listActiveSessions()) {
|
|
150
|
+
if (snapshot.pendingEvent) enqueueEvent(snapshot.pendingEvent);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function findAvailablePendingEvent(now = Date.now()) {
|
|
155
|
+
for (const entry of state.pendingEvents) {
|
|
156
|
+
if (entry.leaseUntil && entry.leaseUntil > now) continue;
|
|
157
|
+
return entry;
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function leaseEvent(entry, leaseMs) {
|
|
163
|
+
if (!entry.event?.id) {
|
|
164
|
+
const idx = state.pendingEvents.indexOf(entry);
|
|
165
|
+
if (idx !== -1) state.pendingEvents.splice(idx, 1);
|
|
166
|
+
return entry.event;
|
|
167
|
+
}
|
|
168
|
+
entry.leaseUntil = Date.now() + leaseMs;
|
|
169
|
+
scheduleLeaseFlush();
|
|
170
|
+
broadcastAgentPollingIfChanged();
|
|
171
|
+
return entry.event;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function acknowledgePendingEvent(id) {
|
|
175
|
+
if (!id) return false;
|
|
176
|
+
const idx = state.pendingEvents.findIndex((entry) => entry.event?.id === id);
|
|
177
|
+
if (idx === -1) return false;
|
|
178
|
+
const acknowledged = state.pendingEvents[idx].event;
|
|
179
|
+
state.pendingEvents.splice(idx, 1);
|
|
180
|
+
scheduleLeaseFlush();
|
|
181
|
+
broadcastAgentPollingIfChanged();
|
|
182
|
+
return acknowledged;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function findPendingEventById(id) {
|
|
186
|
+
if (!id) return null;
|
|
187
|
+
const entry = state.pendingEvents.find((item) => item.event?.id === id);
|
|
188
|
+
return entry?.event || null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function summarizePendingEventForStatus(entry) {
|
|
192
|
+
const event = entry.event || {};
|
|
193
|
+
const summary = {
|
|
194
|
+
id: event.id,
|
|
195
|
+
type: event.type,
|
|
196
|
+
leased: !!(entry.leaseUntil && entry.leaseUntil > Date.now()),
|
|
197
|
+
leaseUntil: entry.leaseUntil || null,
|
|
198
|
+
};
|
|
199
|
+
if (event.type === 'manual_edit_apply') {
|
|
200
|
+
summary.pageUrl = event.pageUrl || null;
|
|
201
|
+
summary.chunk = event.chunk || null;
|
|
202
|
+
summary.repair = event.repair || null;
|
|
203
|
+
summary.evidencePath = event.evidencePath || null;
|
|
204
|
+
summary.agentAction = event.agentAction || manualApply.buildAgentAction(event);
|
|
205
|
+
summary.manualApplySummary = manualApply.summarizeEvent(event, manualApply.getDeferred(event.id)?.batch || event.batch);
|
|
206
|
+
}
|
|
207
|
+
return summary;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function summarizeActiveSessionForClient(snapshot = {}) {
|
|
211
|
+
return {
|
|
212
|
+
id: snapshot.id,
|
|
213
|
+
phase: snapshot.phase,
|
|
214
|
+
pageUrl: snapshot.pageUrl ?? null,
|
|
215
|
+
sourceFile: snapshot.sourceFile ?? null,
|
|
216
|
+
previewFile: snapshot.previewFile ?? null,
|
|
217
|
+
previewMode: snapshot.previewMode ?? null,
|
|
218
|
+
expectedVariants: snapshot.expectedVariants ?? 0,
|
|
219
|
+
arrivedVariants: snapshot.arrivedVariants ?? 0,
|
|
220
|
+
visibleVariant: snapshot.visibleVariant ?? null,
|
|
221
|
+
checkpointRevision: snapshot.checkpointRevision ?? 0,
|
|
222
|
+
paramValues: snapshot.paramValues || {},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function activeSessionSummaries() {
|
|
227
|
+
if (!state.sessionStore) return [];
|
|
228
|
+
return state.sessionStore.listActiveSessions().map((snapshot) => summarizeActiveSessionForClient(snapshot));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function cancelQueuedAnonymousExitEvents() {
|
|
232
|
+
let removed = 0;
|
|
233
|
+
for (let i = state.pendingEvents.length - 1; i >= 0; i -= 1) {
|
|
234
|
+
const event = state.pendingEvents[i]?.event;
|
|
235
|
+
if (event?.type !== 'exit' || event.id) continue;
|
|
236
|
+
state.pendingEvents.splice(i, 1);
|
|
237
|
+
removed += 1;
|
|
238
|
+
}
|
|
239
|
+
if (removed > 0) {
|
|
240
|
+
scheduleLeaseFlush();
|
|
241
|
+
broadcastAgentPollingIfChanged();
|
|
242
|
+
}
|
|
243
|
+
return removed;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function scheduleLeaseFlush() {
|
|
247
|
+
if (state.leaseTimer) {
|
|
248
|
+
clearTimeout(state.leaseTimer);
|
|
249
|
+
state.leaseTimer = null;
|
|
250
|
+
}
|
|
251
|
+
const now = Date.now();
|
|
252
|
+
const nextLeaseUntil = state.pendingEvents
|
|
253
|
+
.map((entry) => entry.leaseUntil || 0)
|
|
254
|
+
.filter((leaseUntil) => leaseUntil > now)
|
|
255
|
+
.sort((a, b) => a - b)[0];
|
|
256
|
+
if (!nextLeaseUntil) return;
|
|
257
|
+
state.leaseTimer = setTimeout(() => {
|
|
258
|
+
state.leaseTimer = null;
|
|
259
|
+
flushPendingPolls();
|
|
260
|
+
broadcastAgentPollingIfChanged();
|
|
261
|
+
}, Math.max(0, nextLeaseUntil - now + POLL_LEASE_EXPIRY_TIMER_GRACE_MS));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function flushPendingPolls() {
|
|
265
|
+
let changed = false;
|
|
266
|
+
while (state.pendingPolls.length > 0) {
|
|
267
|
+
const entry = findAvailablePendingEvent();
|
|
268
|
+
if (!entry) {
|
|
269
|
+
scheduleLeaseFlush();
|
|
270
|
+
broadcastAgentPollingIfChanged();
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const poll = state.pendingPolls.shift();
|
|
274
|
+
poll.resolve(leaseEvent(entry, poll.leaseMs));
|
|
275
|
+
changed = true;
|
|
276
|
+
}
|
|
277
|
+
scheduleLeaseFlush();
|
|
278
|
+
if (changed) broadcastAgentPollingIfChanged();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function agentPollingConnected() {
|
|
282
|
+
const now = Date.now();
|
|
283
|
+
return state.pendingPolls.length > 0
|
|
284
|
+
|| state.pendingEvents.some((entry) => entry.leaseUntil && entry.leaseUntil > now);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function broadcastAgentPollingIfChanged() {
|
|
288
|
+
const connected = agentPollingConnected();
|
|
289
|
+
if (state.lastAgentPollingBroadcast === connected) return;
|
|
290
|
+
state.lastAgentPollingBroadcast = connected;
|
|
291
|
+
broadcast({ type: 'agent_polling', connected });
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Push a message to all connected SSE clients. */
|
|
295
|
+
function broadcast(msg) {
|
|
296
|
+
const data = 'data: ' + JSON.stringify(msg) + '\n\n';
|
|
297
|
+
for (const res of state.sseClients) {
|
|
298
|
+
try { res.write(data); } catch { /* client gone */ }
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function recordManualEditActivity(type, details = {}) {
|
|
303
|
+
const entry = {
|
|
304
|
+
seq: state.nextManualEditSeq++,
|
|
305
|
+
type,
|
|
306
|
+
ts: new Date().toISOString(),
|
|
307
|
+
...details,
|
|
308
|
+
};
|
|
309
|
+
state.manualEditActivity = entry;
|
|
310
|
+
if (DEBUG_MANUAL_EDIT_EVENTS) {
|
|
311
|
+
try {
|
|
312
|
+
const filePath = path.join(getLiveDir(process.cwd()), 'manual-edit-events.jsonl');
|
|
313
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
314
|
+
fs.appendFileSync(filePath, JSON.stringify(entry) + '\n');
|
|
315
|
+
} catch {
|
|
316
|
+
/* diagnostics are best-effort; never block live mode on observability */
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
broadcast(entry);
|
|
320
|
+
return entry;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function getManualEditStatus() {
|
|
324
|
+
try {
|
|
325
|
+
const { totalCount, perPage } = countPendingByPage(process.cwd());
|
|
326
|
+
return { totalCount, perPage, lastActivity: state.manualEditActivity };
|
|
327
|
+
} catch (err) {
|
|
328
|
+
return {
|
|
329
|
+
totalCount: null,
|
|
330
|
+
perPage: {},
|
|
331
|
+
lastActivity: state.manualEditActivity,
|
|
332
|
+
error: err.message,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// ---------------------------------------------------------------------------
|
|
338
|
+
// Load scripts
|
|
339
|
+
// ---------------------------------------------------------------------------
|
|
340
|
+
|
|
341
|
+
function loadBrowserScripts() {
|
|
342
|
+
// Detection script: prefer the skill-bundled detector, then fall back to
|
|
343
|
+
// source/npm package locations for local development and older installs.
|
|
344
|
+
// This one IS cached — detect.js rarely changes during a session.
|
|
345
|
+
const detectPaths = [
|
|
346
|
+
path.join(__dirname, 'detector', 'detect-antipatterns-browser.js'),
|
|
347
|
+
path.join(__dirname, '..', '..', 'cli', 'engine', 'detect-antipatterns-browser.js'),
|
|
348
|
+
path.join(__dirname, '..', '..', '..', '..', 'cli', 'engine', 'detect-antipatterns-browser.js'),
|
|
349
|
+
path.join(process.cwd(), 'node_modules', 'impeccable', 'cli', 'engine', 'detect-antipatterns-browser.js'),
|
|
350
|
+
];
|
|
351
|
+
let detectScript = '';
|
|
352
|
+
for (const p of detectPaths) {
|
|
353
|
+
try { detectScript = fs.readFileSync(p, 'utf-8'); break; } catch { /* try next */ }
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Browser script parts: DO NOT cache. Return paths so the /live.js handler
|
|
357
|
+
// can re-read every part on each request. Editing browser code during
|
|
358
|
+
// iteration should land on the next tab reload, not require a server restart.
|
|
359
|
+
const liveScriptParts = resolveLiveBrowserScriptParts(__dirname);
|
|
360
|
+
try {
|
|
361
|
+
assertLiveBrowserScriptParts(liveScriptParts);
|
|
362
|
+
} catch (err) {
|
|
363
|
+
process.stderr.write('Error: ' + err.message + '\n');
|
|
364
|
+
process.exit(1);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return { detectScript, liveScriptParts };
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function hasProjectContext() {
|
|
371
|
+
// PRODUCT.md carries brand voice / anti-references — that's what determines
|
|
372
|
+
// whether variants are brand-aware. DESIGN.md (visual tokens) is a separate
|
|
373
|
+
// concern, surfaced by the design panel's own empty state.
|
|
374
|
+
try {
|
|
375
|
+
fs.accessSync(path.join(CONTEXT_DIR, 'PRODUCT.md'), fs.constants.R_OK);
|
|
376
|
+
return true;
|
|
377
|
+
} catch { return false; }
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function statOrNull(filePath) {
|
|
381
|
+
try { return fs.statSync(filePath); } catch { return null; }
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// HTTP request handler
|
|
385
|
+
// ---------------------------------------------------------------------------
|
|
386
|
+
|
|
387
|
+
function createRequestHandler({ detectScript, liveScriptParts }) {
|
|
388
|
+
return (req, res) => {
|
|
389
|
+
const url = new URL(req.url, `http://localhost:${state.port}`);
|
|
390
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
391
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
392
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
393
|
+
if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; }
|
|
394
|
+
|
|
395
|
+
const p = url.pathname;
|
|
396
|
+
|
|
397
|
+
// --- Scripts ---
|
|
398
|
+
if (p === '/live.js') {
|
|
399
|
+
// Re-read from disk each request so edits to live-browser.js land on
|
|
400
|
+
// the next tab reload. No-store headers prevent browser caching across
|
|
401
|
+
// sessions — during iteration, a cached old script silently breaks
|
|
402
|
+
// every subsequent session.
|
|
403
|
+
let parts;
|
|
404
|
+
try {
|
|
405
|
+
parts = readLiveBrowserScriptParts(liveScriptParts);
|
|
406
|
+
} catch (err) {
|
|
407
|
+
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
408
|
+
res.end('Error reading live browser scripts: ' + err.message);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
const body = assembleLiveBrowserScript({
|
|
412
|
+
token: state.token,
|
|
413
|
+
port: state.port,
|
|
414
|
+
vocabulary: LIVE_COMMANDS,
|
|
415
|
+
parts,
|
|
416
|
+
});
|
|
417
|
+
res.writeHead(200, {
|
|
418
|
+
'Content-Type': 'application/javascript',
|
|
419
|
+
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
|
|
420
|
+
'Pragma': 'no-cache',
|
|
421
|
+
});
|
|
422
|
+
res.end(body);
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
if (p === '/detect.js' || p === '/') {
|
|
426
|
+
if (!detectScript) { res.writeHead(404); res.end('Not available'); return; }
|
|
427
|
+
res.writeHead(200, { 'Content-Type': 'application/javascript' });
|
|
428
|
+
res.end(detectScript);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// --- Vendored modern-screenshot (UMD build) ---
|
|
433
|
+
// Lazy-loaded by live.js when the user clicks Go; exposes
|
|
434
|
+
// window.modernScreenshot.domToBlob(...) for capture.
|
|
435
|
+
if (p === '/modern-screenshot.js') {
|
|
436
|
+
const vendorPath = path.join(__dirname, 'modern-screenshot.umd.js');
|
|
437
|
+
try {
|
|
438
|
+
res.writeHead(200, {
|
|
439
|
+
'Content-Type': 'application/javascript',
|
|
440
|
+
'Cache-Control': 'public, max-age=31536000, immutable',
|
|
441
|
+
});
|
|
442
|
+
res.end(fs.readFileSync(vendorPath));
|
|
443
|
+
} catch {
|
|
444
|
+
res.writeHead(404); res.end('Vendor script not found');
|
|
445
|
+
}
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// --- Annotation upload (browser → server, raw PNG body) ---
|
|
450
|
+
// Client generates the eventId, POSTs the PNG, then POSTs the generate
|
|
451
|
+
// event with screenshotPath already set. Keeps bytes out of the SSE/poll
|
|
452
|
+
// bridge and preserves the "one shot from the user's POV" UX.
|
|
453
|
+
if (p === '/annotation' && req.method === 'POST') {
|
|
454
|
+
const token = url.searchParams.get('token');
|
|
455
|
+
if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
|
|
456
|
+
const eventId = url.searchParams.get('eventId');
|
|
457
|
+
if (!eventId || !/^[A-Za-z0-9_-]{1,64}$/.test(eventId)) {
|
|
458
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
459
|
+
res.end(JSON.stringify({ error: 'Invalid eventId' }));
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
if ((req.headers['content-type'] || '').toLowerCase() !== 'image/png') {
|
|
463
|
+
res.writeHead(415, { 'Content-Type': 'application/json' });
|
|
464
|
+
res.end(JSON.stringify({ error: 'Content-Type must be image/png' }));
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if (!state.sessionDir) {
|
|
468
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
469
|
+
res.end(JSON.stringify({ error: 'Session dir unavailable' }));
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
const chunks = [];
|
|
473
|
+
let total = 0;
|
|
474
|
+
let aborted = false;
|
|
475
|
+
req.on('data', (c) => {
|
|
476
|
+
if (aborted) return;
|
|
477
|
+
total += c.length;
|
|
478
|
+
if (total > MAX_ANNOTATION_BYTES) {
|
|
479
|
+
aborted = true;
|
|
480
|
+
res.writeHead(413, { 'Content-Type': 'application/json' });
|
|
481
|
+
res.end(JSON.stringify({ error: 'Payload too large' }));
|
|
482
|
+
req.destroy();
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
chunks.push(c);
|
|
486
|
+
});
|
|
487
|
+
req.on('end', () => {
|
|
488
|
+
if (aborted) return;
|
|
489
|
+
const absPath = path.join(state.sessionDir, eventId + '.png');
|
|
490
|
+
try {
|
|
491
|
+
fs.writeFileSync(absPath, Buffer.concat(chunks));
|
|
492
|
+
} catch (err) {
|
|
493
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
494
|
+
res.end(JSON.stringify({ error: 'Write failed: ' + err.message }));
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
498
|
+
res.end(JSON.stringify({ ok: true, path: absPath }));
|
|
499
|
+
});
|
|
500
|
+
req.on('error', () => {
|
|
501
|
+
if (!aborted) {
|
|
502
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
503
|
+
res.end(JSON.stringify({ error: 'Upload failed' }));
|
|
504
|
+
}
|
|
505
|
+
});
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// --- Health ---
|
|
510
|
+
if (p === '/status') {
|
|
511
|
+
const token = url.searchParams.get('token');
|
|
512
|
+
if (token !== state.token) { res.writeHead(401, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Unauthorized' })); return; }
|
|
513
|
+
const sessions = activeSessionSummaries();
|
|
514
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
515
|
+
res.end(JSON.stringify({
|
|
516
|
+
status: 'ok',
|
|
517
|
+
port: state.port,
|
|
518
|
+
connectedClients: state.sseClients.size,
|
|
519
|
+
pendingEvents: state.pendingEvents.map((entry) => summarizePendingEventForStatus(entry)),
|
|
520
|
+
agentPolling: agentPollingConnected(),
|
|
521
|
+
activeSessions: sessions,
|
|
522
|
+
manualEdits: getManualEditStatus(),
|
|
523
|
+
}));
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (p === '/health') {
|
|
528
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
529
|
+
res.end(JSON.stringify({
|
|
530
|
+
status: 'ok', port: state.port, mode: 'variant',
|
|
531
|
+
hasProjectContext: hasProjectContext(),
|
|
532
|
+
connectedClients: state.sseClients.size,
|
|
533
|
+
}));
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// --- Design system (unified v2 response) + raw ---
|
|
538
|
+
// /design-system.json returns both parsed DESIGN.md and .impeccable/design.json
|
|
539
|
+
// sidecar when present. Panel merges them:
|
|
540
|
+
// { present, parsed, sidecar, hasMd, hasSidecar,
|
|
541
|
+
// mdNewerThanJson, parseError?, sidecarError? }
|
|
542
|
+
// - parsed: output of parseDesignMd (frontmatter
|
|
543
|
+
// + six canonical sections) when DESIGN.md exists.
|
|
544
|
+
// - sidecar: .impeccable/design.json contents when present.
|
|
545
|
+
// Expected shape: schemaVersion 2, carrying
|
|
546
|
+
// extensions + components + narrative.
|
|
547
|
+
// /design-system/raw returns DESIGN.md markdown verbatim
|
|
548
|
+
if (p === '/design-system.json' || p === '/design-system/raw') {
|
|
549
|
+
const token = url.searchParams.get('token');
|
|
550
|
+
if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
|
|
551
|
+
|
|
552
|
+
const mdPath = path.join(CONTEXT_DIR, 'DESIGN.md');
|
|
553
|
+
const jsonPath = resolveDesignSidecarPath(process.cwd(), CONTEXT_DIR) || getDesignSidecarPath(process.cwd());
|
|
554
|
+
const mdStat = statOrNull(mdPath);
|
|
555
|
+
const jsonStat = statOrNull(jsonPath);
|
|
556
|
+
|
|
557
|
+
if (p === '/design-system/raw') {
|
|
558
|
+
if (!mdStat) { res.writeHead(404); res.end('Not found'); return; }
|
|
559
|
+
res.writeHead(200, { 'Content-Type': 'text/markdown; charset=utf-8' });
|
|
560
|
+
res.end(fs.readFileSync(mdPath, 'utf-8'));
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (!mdStat && !jsonStat) {
|
|
565
|
+
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
566
|
+
res.end(JSON.stringify({ present: false }));
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const response = {
|
|
571
|
+
present: true,
|
|
572
|
+
hasMd: !!mdStat,
|
|
573
|
+
hasSidecar: !!jsonStat,
|
|
574
|
+
mdNewerThanJson: !!(mdStat && jsonStat && mdStat.mtimeMs > jsonStat.mtimeMs + 1000),
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
if (mdStat) {
|
|
578
|
+
try {
|
|
579
|
+
response.parsed = parseDesignMd(fs.readFileSync(mdPath, 'utf-8'));
|
|
580
|
+
} catch (err) {
|
|
581
|
+
response.parseError = err.message;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
if (jsonStat) {
|
|
586
|
+
try {
|
|
587
|
+
response.sidecar = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));
|
|
588
|
+
} catch (err) {
|
|
589
|
+
response.sidecarError = 'Failed to parse .impeccable/design.json: ' + err.message;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
594
|
+
res.end(JSON.stringify(response));
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
// --- Source file (no-HMR fallback) ---
|
|
599
|
+
if (p === '/source') {
|
|
600
|
+
const token = url.searchParams.get('token');
|
|
601
|
+
if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
|
|
602
|
+
const filePath = url.searchParams.get('path');
|
|
603
|
+
if (!filePath || filePath.includes('..')) { res.writeHead(400); res.end('Bad path'); return; }
|
|
604
|
+
const absPath = path.resolve(process.cwd(), filePath);
|
|
605
|
+
if (!absPath.startsWith(process.cwd())) { res.writeHead(403); res.end('Forbidden'); return; }
|
|
606
|
+
let content;
|
|
607
|
+
try { content = fs.readFileSync(absPath, 'utf-8'); }
|
|
608
|
+
catch { res.writeHead(404); res.end('File not found'); return; }
|
|
609
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
610
|
+
res.end(content);
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// --- SSE: server→browser push (replaces WebSocket) ---
|
|
615
|
+
if (p === '/events' && req.method === 'GET') {
|
|
616
|
+
const token = url.searchParams.get('token');
|
|
617
|
+
if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
|
|
618
|
+
clearTimeout(state.exitTimer);
|
|
619
|
+
state.exitTimer = null;
|
|
620
|
+
cancelQueuedAnonymousExitEvents();
|
|
621
|
+
res.writeHead(200, {
|
|
622
|
+
'Content-Type': 'text/event-stream',
|
|
623
|
+
'Cache-Control': 'no-cache',
|
|
624
|
+
'Connection': 'keep-alive',
|
|
625
|
+
});
|
|
626
|
+
res.write('data: ' + JSON.stringify({
|
|
627
|
+
type: 'connected',
|
|
628
|
+
hasProjectContext: hasProjectContext(),
|
|
629
|
+
agentPolling: agentPollingConnected(),
|
|
630
|
+
activeSessions: activeSessionSummaries(),
|
|
631
|
+
}) + '\n\n');
|
|
632
|
+
|
|
633
|
+
state.sseClients.add(res);
|
|
634
|
+
|
|
635
|
+
// Keepalive: SSE comment every 30s prevents silent connection drops.
|
|
636
|
+
const heartbeat = setInterval(() => {
|
|
637
|
+
try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); }
|
|
638
|
+
}, SSE_HEARTBEAT_INTERVAL);
|
|
639
|
+
|
|
640
|
+
req.on('close', () => {
|
|
641
|
+
clearInterval(heartbeat);
|
|
642
|
+
state.sseClients.delete(res);
|
|
643
|
+
if (state.sseClients.size === 0) {
|
|
644
|
+
clearTimeout(state.exitTimer);
|
|
645
|
+
state.exitTimer = setTimeout(() => {
|
|
646
|
+
if (state.sseClients.size === 0) enqueueEvent({ type: 'exit' });
|
|
647
|
+
}, 8000);
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
if (manualEditRoutes(req, res, url)) return;
|
|
654
|
+
|
|
655
|
+
// --- Browser→server events (replaces WebSocket messages) ---
|
|
656
|
+
if (p === '/events' && req.method === 'POST') {
|
|
657
|
+
let body = '';
|
|
658
|
+
req.on('data', (c) => { body += c; });
|
|
659
|
+
req.on('end', () => {
|
|
660
|
+
let msg;
|
|
661
|
+
try { msg = JSON.parse(body); } catch {
|
|
662
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
663
|
+
res.end(JSON.stringify({ error: 'Invalid JSON' }));
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
if (msg.token !== state.token) {
|
|
667
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
668
|
+
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
// Defense in depth: manual copy edits must use the staged stash/apply
|
|
672
|
+
// endpoints. The direct Save event path is disabled in the browser.
|
|
673
|
+
if (msg.type === 'manual_edits') {
|
|
674
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
675
|
+
res.end(JSON.stringify({ error: 'manual_edits must POST to /manual-edit-stash, not /events' }));
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
if (msg.type === 'manual_edit_apply') {
|
|
679
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
680
|
+
res.end(JSON.stringify({ error: 'manual_edit_apply is disabled; use /manual-edit-stash then /manual-edit-commit' }));
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
const error = validateEvent(msg);
|
|
684
|
+
if (error) {
|
|
685
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
686
|
+
res.end(JSON.stringify({ error }));
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
if (state.sessionStore && msg.id) {
|
|
690
|
+
try {
|
|
691
|
+
state.sessionStore.appendEvent(msg);
|
|
692
|
+
} catch (err) {
|
|
693
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
694
|
+
res.end(JSON.stringify({ error: 'session_store_append_failed', message: err.message }));
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
if (msg.type === 'exit') {
|
|
699
|
+
cleanupSvelteComponentSessionsBeforeExit();
|
|
700
|
+
}
|
|
701
|
+
if (msg.type !== 'checkpoint') {
|
|
702
|
+
enqueueEvent(msg);
|
|
703
|
+
}
|
|
704
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
705
|
+
res.end(JSON.stringify({ ok: true }));
|
|
706
|
+
});
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// --- Stop ---
|
|
711
|
+
if (p === '/stop') {
|
|
712
|
+
const token = url.searchParams.get('token');
|
|
713
|
+
if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
|
|
714
|
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
715
|
+
res.end('stopping');
|
|
716
|
+
shutdown();
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
// --- Agent poll ---
|
|
721
|
+
if (p === '/poll' && req.method === 'GET') {
|
|
722
|
+
handlePollGet(req, res, url);
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
if (p === '/poll' && req.method === 'POST') {
|
|
726
|
+
handlePollPost(req, res);
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
res.writeHead(404); res.end('Not found');
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// ---------------------------------------------------------------------------
|
|
735
|
+
// Agent poll endpoints (unchanged from WS version)
|
|
736
|
+
// ---------------------------------------------------------------------------
|
|
737
|
+
|
|
738
|
+
function handlePollGet(req, res, url) {
|
|
739
|
+
const token = url.searchParams.get('token');
|
|
740
|
+
if (token !== state.token) {
|
|
741
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
742
|
+
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
state.lastPollAt = Date.now();
|
|
746
|
+
const timeout = parseInt(url.searchParams.get('timeout') || DEFAULT_POLL_TIMEOUT, 10);
|
|
747
|
+
const leaseMs = parseInt(url.searchParams.get('leaseMs') || '30000', 10);
|
|
748
|
+
const available = findAvailablePendingEvent();
|
|
749
|
+
if (available) {
|
|
750
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
751
|
+
res.end(JSON.stringify(leaseEvent(available, leaseMs)));
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
const poll = { resolve, leaseMs };
|
|
755
|
+
const timer = setTimeout(() => {
|
|
756
|
+
const idx = state.pendingPolls.indexOf(poll);
|
|
757
|
+
if (idx !== -1) state.pendingPolls.splice(idx, 1);
|
|
758
|
+
broadcastAgentPollingIfChanged();
|
|
759
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
760
|
+
res.end(JSON.stringify({ type: 'timeout' }));
|
|
761
|
+
}, timeout);
|
|
762
|
+
function resolve(event) {
|
|
763
|
+
clearTimeout(timer);
|
|
764
|
+
state.lastPollAt = Date.now();
|
|
765
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
766
|
+
res.end(JSON.stringify(event));
|
|
767
|
+
}
|
|
768
|
+
state.pendingPolls.push(poll);
|
|
769
|
+
broadcastAgentPollingIfChanged();
|
|
770
|
+
scheduleLeaseFlush();
|
|
771
|
+
req.on('close', () => {
|
|
772
|
+
clearTimeout(timer);
|
|
773
|
+
const idx = state.pendingPolls.indexOf(poll);
|
|
774
|
+
if (idx !== -1) state.pendingPolls.splice(idx, 1);
|
|
775
|
+
broadcastAgentPollingIfChanged();
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
function sessionFileMetadataFromPollReply(file) {
|
|
780
|
+
if (!file || typeof file !== 'string') return { file };
|
|
781
|
+
const normalized = file.split(path.sep).join('/');
|
|
782
|
+
const base = { file: normalized };
|
|
783
|
+
if (!normalized.endsWith('/manifest.json') && normalized !== 'manifest.json') return base;
|
|
784
|
+
if (!normalized.includes('node_modules/.impeccable-live/') && !normalized.includes('src/lib/impeccable/')) return base;
|
|
785
|
+
|
|
786
|
+
let full;
|
|
787
|
+
try {
|
|
788
|
+
full = path.resolve(process.cwd(), normalized);
|
|
789
|
+
const rel = path.relative(process.cwd(), full);
|
|
790
|
+
if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) return base;
|
|
791
|
+
} catch {
|
|
792
|
+
return base;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
try {
|
|
796
|
+
const manifest = JSON.parse(fs.readFileSync(full, 'utf-8'));
|
|
797
|
+
if (manifest?.previewMode !== 'svelte-component' || !manifest.sourceFile) return base;
|
|
798
|
+
return {
|
|
799
|
+
file: String(manifest.sourceFile).split(path.sep).join('/'),
|
|
800
|
+
sourceFile: String(manifest.sourceFile).split(path.sep).join('/'),
|
|
801
|
+
previewFile: normalized,
|
|
802
|
+
previewMode: 'svelte-component',
|
|
803
|
+
};
|
|
804
|
+
} catch {
|
|
805
|
+
return base;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
function handlePollPost(req, res) {
|
|
810
|
+
let body = '';
|
|
811
|
+
req.on('data', (c) => { body += c; });
|
|
812
|
+
req.on('end', () => {
|
|
813
|
+
let msg;
|
|
814
|
+
try { msg = JSON.parse(body); } catch {
|
|
815
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
816
|
+
res.end(JSON.stringify({ error: 'Invalid JSON' }));
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
if (msg.token !== state.token) {
|
|
820
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
821
|
+
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
const pendingApplyDeferred = manualApply.getDeferred(msg.id);
|
|
825
|
+
if (pendingApplyDeferred) {
|
|
826
|
+
const validation = manualApply.validateResultMessage(msg, pendingApplyDeferred);
|
|
827
|
+
if (!validation.ok) {
|
|
828
|
+
recordManualEditActivity('manual_edit_apply_reply_invalid', {
|
|
829
|
+
id: msg.id,
|
|
830
|
+
pageUrl: pendingApplyDeferred.pageUrl,
|
|
831
|
+
chunk: pendingApplyDeferred.event?.chunk || null,
|
|
832
|
+
repair: pendingApplyDeferred.event?.repair || null,
|
|
833
|
+
reason: validation.body?.reason || validation.body?.error || 'invalid_manual_apply_result',
|
|
834
|
+
status: msg.data?.status || null,
|
|
835
|
+
});
|
|
836
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
837
|
+
res.end(JSON.stringify(validation.body));
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
recordManualEditActivity('manual_edit_apply_reply_received', {
|
|
841
|
+
id: msg.id,
|
|
842
|
+
pageUrl: pendingApplyDeferred.pageUrl,
|
|
843
|
+
chunk: pendingApplyDeferred.event?.chunk || null,
|
|
844
|
+
repair: pendingApplyDeferred.event?.repair || null,
|
|
845
|
+
status: validation.result.status,
|
|
846
|
+
appliedCount: validation.result.appliedEntryIds.length,
|
|
847
|
+
failed: summarizeManualApplyFailures(validation.result.failed),
|
|
848
|
+
fileCount: validation.result.files.length,
|
|
849
|
+
noteCount: validation.result.notes.length,
|
|
850
|
+
});
|
|
851
|
+
manualApply.resolveDeferred(msg.id, validation.result);
|
|
852
|
+
acknowledgePendingEvent(msg.id);
|
|
853
|
+
flushPendingPolls();
|
|
854
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
855
|
+
res.end(JSON.stringify({ ok: true }));
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
if (manualApply.hasTimedOutId(msg.id)) {
|
|
859
|
+
const rollback = manualApply.rollbackTimedOutReply(msg);
|
|
860
|
+
recordManualEditActivity('manual_edit_apply_stale_reply_rejected', {
|
|
861
|
+
id: msg.id,
|
|
862
|
+
rolledBackFileCount: rollback.rolledBackFiles?.length || 0,
|
|
863
|
+
rollbackFailureCount: rollback.rollbackFailures?.length || 0,
|
|
864
|
+
});
|
|
865
|
+
res.writeHead(409, { 'Content-Type': 'application/json' });
|
|
866
|
+
res.end(JSON.stringify({ error: 'stale_manual_edit_apply_reply', ...rollback }));
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
const pendingEventBeforeAck = findPendingEventById(msg.id);
|
|
870
|
+
if (pendingEventBeforeAck?.type === 'steer' && msg.type === 'steer_done'
|
|
871
|
+
&& !msg.file && !(typeof msg.message === 'string' && msg.message.trim())) {
|
|
872
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
873
|
+
res.end(JSON.stringify({
|
|
874
|
+
error: 'steer_done_requires_file_or_message',
|
|
875
|
+
hint: 'Reply with --file after writing source, or include a message explaining an intentional no-op.',
|
|
876
|
+
}));
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
const acknowledgedEvent = acknowledgePendingEvent(msg.id);
|
|
880
|
+
let skipJournalReply = false;
|
|
881
|
+
let existingSession = null;
|
|
882
|
+
if (!acknowledgedEvent && state.sessionStore && msg.id) {
|
|
883
|
+
try {
|
|
884
|
+
existingSession = state.sessionStore.getSnapshot(msg.id, { includeCompleted: true });
|
|
885
|
+
if (!existingSession?.updatedAt) existingSession = null;
|
|
886
|
+
skipJournalReply = existingSession?.phase === 'completed' || existingSession?.phase === 'discarded';
|
|
887
|
+
} catch { /* fall through and record the reply normally */ }
|
|
888
|
+
}
|
|
889
|
+
if (!acknowledgedEvent && !existingSession) {
|
|
890
|
+
recordManualEditActivity('manual_edit_poll_reply_unknown', {
|
|
891
|
+
id: msg.id || null,
|
|
892
|
+
type: msg.type || null,
|
|
893
|
+
});
|
|
894
|
+
res.writeHead(msg.id ? 404 : 400, { 'Content-Type': 'application/json' });
|
|
895
|
+
res.end(JSON.stringify({
|
|
896
|
+
error: msg.id ? 'unknown_poll_reply_id' : 'missing_poll_reply_id',
|
|
897
|
+
id: msg.id,
|
|
898
|
+
}));
|
|
899
|
+
return;
|
|
900
|
+
}
|
|
901
|
+
const replyFileMeta = sessionFileMetadataFromPollReply(msg.file);
|
|
902
|
+
if (state.sessionStore && msg.id && !skipJournalReply) {
|
|
903
|
+
try {
|
|
904
|
+
const eventType = msg.type === 'steer_done'
|
|
905
|
+
? 'steer_done'
|
|
906
|
+
: msg.type === 'discard' || msg.type === 'discarded'
|
|
907
|
+
? 'discarded'
|
|
908
|
+
: msg.type === 'complete'
|
|
909
|
+
? 'complete'
|
|
910
|
+
: msg.type === 'error'
|
|
911
|
+
? 'agent_error'
|
|
912
|
+
: 'agent_done';
|
|
913
|
+
state.sessionStore.appendEvent({
|
|
914
|
+
type: eventType,
|
|
915
|
+
id: msg.id,
|
|
916
|
+
file: replyFileMeta.file,
|
|
917
|
+
sourceFile: replyFileMeta.sourceFile,
|
|
918
|
+
previewFile: replyFileMeta.previewFile,
|
|
919
|
+
previewMode: replyFileMeta.previewMode,
|
|
920
|
+
message: msg.message,
|
|
921
|
+
sourceEventType: acknowledgedEvent?.type,
|
|
922
|
+
carbonize: msg.data?.carbonize === true,
|
|
923
|
+
});
|
|
924
|
+
} catch { /* keep reply path best-effort; browser still needs SSE */ }
|
|
925
|
+
}
|
|
926
|
+
flushPendingPolls();
|
|
927
|
+
// Forward the reply to the browser via SSE
|
|
928
|
+
broadcast({
|
|
929
|
+
type: msg.type || 'done',
|
|
930
|
+
id: msg.id,
|
|
931
|
+
message: msg.message,
|
|
932
|
+
file: msg.file,
|
|
933
|
+
sourceFile: replyFileMeta.sourceFile,
|
|
934
|
+
previewFile: replyFileMeta.previewFile,
|
|
935
|
+
previewMode: replyFileMeta.previewMode,
|
|
936
|
+
data: msg.data,
|
|
937
|
+
});
|
|
938
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
939
|
+
res.end(JSON.stringify({ ok: true }));
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
// ---------------------------------------------------------------------------
|
|
944
|
+
// Lifecycle
|
|
945
|
+
// ---------------------------------------------------------------------------
|
|
946
|
+
|
|
947
|
+
let httpServer = null;
|
|
948
|
+
|
|
949
|
+
function shutdown() {
|
|
950
|
+
cleanupSvelteComponentSessionsBeforeExit();
|
|
951
|
+
removeLiveServerInfo(process.cwd());
|
|
952
|
+
if (state.leaseTimer) clearTimeout(state.leaseTimer);
|
|
953
|
+
state.leaseTimer = null;
|
|
954
|
+
if (state.sessionDir) {
|
|
955
|
+
try { fs.rmSync(state.sessionDir, { recursive: true, force: true }); } catch {}
|
|
956
|
+
}
|
|
957
|
+
for (const res of state.sseClients) { try { res.end(); } catch {} }
|
|
958
|
+
state.sseClients.clear();
|
|
959
|
+
for (const poll of state.pendingPolls) poll.resolve({ type: 'exit' });
|
|
960
|
+
state.pendingPolls.length = 0;
|
|
961
|
+
if (httpServer) httpServer.close();
|
|
962
|
+
process.exit(0);
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
function cleanupSvelteComponentSessionsBeforeExit() {
|
|
966
|
+
try {
|
|
967
|
+
removeAllSvelteComponentSessions(process.cwd());
|
|
968
|
+
} catch (err) {
|
|
969
|
+
console.warn('[impeccable] Svelte component session cleanup failed:', err.message);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
function applyLegacyDeferredAcceptsOnStartup() {
|
|
974
|
+
try {
|
|
975
|
+
const result = applyDeferredSvelteComponentAccepts(process.cwd());
|
|
976
|
+
if (result.applied > 0 || result.failed > 0) {
|
|
977
|
+
console.log('[impeccable] applied legacy deferred Svelte component accepts:', JSON.stringify(result));
|
|
978
|
+
}
|
|
979
|
+
} catch (err) {
|
|
980
|
+
console.warn('[impeccable] legacy deferred Svelte component accept apply failed:', err.message);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
// ---------------------------------------------------------------------------
|
|
985
|
+
// Main
|
|
986
|
+
// ---------------------------------------------------------------------------
|
|
987
|
+
|
|
988
|
+
const args = process.argv.slice(2);
|
|
989
|
+
|
|
990
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
991
|
+
console.log(`Usage: node live-server.mjs [options]
|
|
992
|
+
|
|
993
|
+
Start the live variant mode server (zero dependencies).
|
|
994
|
+
|
|
995
|
+
Commands:
|
|
996
|
+
(default) Start the server (foreground)
|
|
997
|
+
stop Stop the server and remove the injected live.js script tag
|
|
998
|
+
stop --keep-inject Stop the server only (leave the script tag in the HTML entry)
|
|
999
|
+
|
|
1000
|
+
Options:
|
|
1001
|
+
--background Start detached, print connection JSON to stdout, then exit
|
|
1002
|
+
--port=PORT Use a specific port (default: auto-detect starting at 8400)
|
|
1003
|
+
--keep-inject Only with stop: skip live-inject.mjs --remove
|
|
1004
|
+
--help Show this help
|
|
1005
|
+
|
|
1006
|
+
Endpoints:
|
|
1007
|
+
/live.js Browser script (element picker + variant cycling)
|
|
1008
|
+
/detect.js Detection overlay (backwards compatible)
|
|
1009
|
+
/modern-screenshot.js Vendored modern-screenshot UMD build (lazy-loaded by live.js)
|
|
1010
|
+
/annotation POST raw image/png to stage a variant screenshot
|
|
1011
|
+
/events SSE stream (server→browser) + POST (browser→server)
|
|
1012
|
+
/poll Long-poll for agent CLI
|
|
1013
|
+
/manual-edit-stash Stage browser copy edits
|
|
1014
|
+
/manual-edit-commit Apply staged browser copy edits
|
|
1015
|
+
/manual-edit-discard Discard staged browser copy edits
|
|
1016
|
+
/source Raw source file reader (no-HMR fallback)
|
|
1017
|
+
/status Durable recovery status (token-protected)
|
|
1018
|
+
/health Health check`);
|
|
1019
|
+
process.exit(0);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
if (args.includes('stop')) {
|
|
1023
|
+
const keepInject = args.includes('--keep-inject');
|
|
1024
|
+
try {
|
|
1025
|
+
const { info } = readLiveServerInfo(process.cwd()) || {};
|
|
1026
|
+
const res = await fetch(`http://localhost:${info.port}/stop?token=${info.token}`);
|
|
1027
|
+
if (res.ok) console.log(`Stopped live server on port ${info.port}.`);
|
|
1028
|
+
} catch {
|
|
1029
|
+
console.log('No running live server found.');
|
|
1030
|
+
}
|
|
1031
|
+
if (!keepInject) {
|
|
1032
|
+
const injectPath = path.join(__dirname, 'live-inject.mjs');
|
|
1033
|
+
try {
|
|
1034
|
+
const out = execFileSync(process.execPath, [injectPath, '--remove'], {
|
|
1035
|
+
encoding: 'utf-8',
|
|
1036
|
+
cwd: process.cwd(),
|
|
1037
|
+
});
|
|
1038
|
+
const line = out.trim().split('\n').filter(Boolean).pop();
|
|
1039
|
+
if (line) {
|
|
1040
|
+
try {
|
|
1041
|
+
const j = JSON.parse(line);
|
|
1042
|
+
if (j.removed === true) {
|
|
1043
|
+
console.log(`Removed live script tag from ${j.file}.`);
|
|
1044
|
+
}
|
|
1045
|
+
} catch {
|
|
1046
|
+
/* ignore non-JSON lines */
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
} catch (err) {
|
|
1050
|
+
const detail = err.stderr?.toString?.().trim?.()
|
|
1051
|
+
|| err.stdout?.toString?.().trim?.()
|
|
1052
|
+
|| err.message
|
|
1053
|
+
|| String(err);
|
|
1054
|
+
console.warn(`Note: could not remove live script tag (${detail.split('\n')[0]})`);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
process.exit(0);
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
// --background: spawn a detached child server, wait for it to be ready,
|
|
1061
|
+
// print the connection JSON, then exit. This keeps the startup command
|
|
1062
|
+
// simple (no shell backgrounding or chained commands).
|
|
1063
|
+
if (args.includes('--background')) {
|
|
1064
|
+
const childArgs = args.filter(a => a !== '--background');
|
|
1065
|
+
const child = spawn(process.execPath, [fileURLToPath(import.meta.url), ...childArgs], {
|
|
1066
|
+
detached: true,
|
|
1067
|
+
stdio: 'ignore',
|
|
1068
|
+
cwd: process.cwd(),
|
|
1069
|
+
});
|
|
1070
|
+
child.unref();
|
|
1071
|
+
|
|
1072
|
+
// Poll for the PID file (the child writes it once the HTTP server is listening).
|
|
1073
|
+
const deadline = Date.now() + 10_000;
|
|
1074
|
+
while (Date.now() < deadline) {
|
|
1075
|
+
try {
|
|
1076
|
+
const { info } = readLiveServerInfo(process.cwd()) || {};
|
|
1077
|
+
if (info.pid !== process.pid) {
|
|
1078
|
+
// Output JSON so the agent can read port + token from stdout.
|
|
1079
|
+
console.log(JSON.stringify(info));
|
|
1080
|
+
process.exit(0);
|
|
1081
|
+
}
|
|
1082
|
+
} catch { /* not ready yet */ }
|
|
1083
|
+
await new Promise(r => setTimeout(r, 200));
|
|
1084
|
+
}
|
|
1085
|
+
console.error('Timed out waiting for live server to start.');
|
|
1086
|
+
process.exit(1);
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
// Check for existing session
|
|
1090
|
+
const existingRecord = readLiveServerInfo(process.cwd());
|
|
1091
|
+
if (existingRecord?.info) {
|
|
1092
|
+
const existing = existingRecord.info;
|
|
1093
|
+
try {
|
|
1094
|
+
process.kill(existing.pid, 0);
|
|
1095
|
+
console.error(`Live server already running on port ${existing.port} (pid ${existing.pid}).`);
|
|
1096
|
+
console.error('Stop it first with: node ' + path.basename(fileURLToPath(import.meta.url)) + ' stop');
|
|
1097
|
+
process.exit(1);
|
|
1098
|
+
} catch {
|
|
1099
|
+
try { fs.unlinkSync(existingRecord.path); } catch {}
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
state.token = randomUUID();
|
|
1104
|
+
state.sessionStore = createLiveSessionStore({ cwd: process.cwd() });
|
|
1105
|
+
manualApply.rollbackTransaction({
|
|
1106
|
+
reason: 'manual_edit_server_start_recovered_abandoned_transaction',
|
|
1107
|
+
});
|
|
1108
|
+
applyLegacyDeferredAcceptsOnStartup();
|
|
1109
|
+
restorePendingEventsFromStore();
|
|
1110
|
+
manualApply.pruneStaleEvidence();
|
|
1111
|
+
const portArg = args.find(a => a.startsWith('--port='));
|
|
1112
|
+
state.port = portArg ? parseInt(portArg.split('=')[1], 10) : await findOpenPort();
|
|
1113
|
+
// Annotation screenshots live in the project root so the agent's Read tool
|
|
1114
|
+
// doesn't trip a per-file permission prompt. Sessioned by token so concurrent
|
|
1115
|
+
// projects (or quick restarts) don't collide.
|
|
1116
|
+
const annotRoot = getLiveAnnotationsDir(process.cwd());
|
|
1117
|
+
fs.mkdirSync(annotRoot, { recursive: true });
|
|
1118
|
+
state.sessionDir = fs.mkdtempSync(path.join(annotRoot, 'session-'));
|
|
1119
|
+
|
|
1120
|
+
const { detectScript, liveScriptParts } = loadBrowserScripts();
|
|
1121
|
+
httpServer = http.createServer(createRequestHandler({ detectScript, liveScriptParts }));
|
|
1122
|
+
|
|
1123
|
+
httpServer.listen(state.port, '127.0.0.1', () => {
|
|
1124
|
+
writeLiveServerInfo(process.cwd(), { pid: process.pid, port: state.port, token: state.token });
|
|
1125
|
+
const url = `http://localhost:${state.port}`;
|
|
1126
|
+
console.log(`\nImpeccable live server running on ${url}`);
|
|
1127
|
+
console.log(`Token: ${state.token}\n`);
|
|
1128
|
+
console.log(`Script: ${url}/live.js`);
|
|
1129
|
+
console.log('Inject: managed by live-inject.mjs; Astro source tags use is:inline automatically.');
|
|
1130
|
+
console.log(`Stop: node ${path.basename(fileURLToPath(import.meta.url))} stop`);
|
|
1131
|
+
});
|
|
1132
|
+
|
|
1133
|
+
process.on('SIGINT', shutdown);
|
|
1134
|
+
process.on('SIGTERM', shutdown);
|