mirror of
https://github.com/pbakaus/impeccable.git
synced 2026-09-17 08:36:25 +03:00
Addresses every issue surfaced during hands-on live-mode testing. ## Injection across multi-page sites - Config schema: `file` → `files: string[]` so multi-page static sites can opt into script-tag injection across every HTML entry the browser loads. - `live-inject.mjs` loops the array, reports per-file results, and refuses silently with `config_invalid` if the schema is stale. - `insertBefore` switched from first-match to last-match (lastIndexOf) so the anchor lands at the true close of `</body>`, not the first one embedded inside a `<pre><code>` documentation sample. ## Source-vs-generated detection - New `is-generated.mjs` helper: gitignore check + generated-header markers. Edge-case `generatedFiles` config dropped — the two real signals cover every project shape we tested. - `live-wrap.mjs` excludes generated files from auto-search and returns clear fallback errors: `file_is_generated`, `element_not_in_source` (with `generatedMatch` path), and `element_not_found`. - `live-accept.mjs` refuses to persist into generated files; returns `mode: "fallback"` so the agent takes over via the Handle fallback flow. ## Accept correctness - `extractVariant` / `extractOriginal` now skip `<style>` regions when matching markers. Previous regex substring match treated `@scope ([data-impeccable-variant="N"])` in CSS as the target HTML div, capturing garbage and producing orphan CSS that rendered as prose on the page. - On accept, the chosen variant's content is wrapped in `<div data-impeccable-variant="N" style="display: contents">` so the carbonize block's `@scope` selectors keep matching. Users see the accepted design immediately; no pre-carbonize dead state. ## Browser-side UI - `positionBar` gains a third case: when the selected element is taller than the viewport, pin the bar to a stable viewport anchor instead of teleporting between top and bottom as the user scrolls. - No-HMR source-fetch path (`injectVariantsFromSource`) now calls `hideShaderOverlay()` on state transition to CYCLING. Previously the shader kept running after variants arrived via the fetch fallback. - `pickVariantContent` helper replaces fragile `> :first-child` selection for outline positioning. Skips non-visual tags (style, script, link, meta, template) and falls back to the variant div itself when a variant contains multiple visual children. - `resumeSession` re-captures and restarts the shader overlay when the page reloads mid-generation (Bun HTML HMR does a full reload and destroys the canvas). - MutationObserver re-anchors `selectedElement` when the original element is detached by HMR, preventing zero-rect highlight drift. ## Skill docs - `live.md` reframes `config.files` as "the HTML files the browser actually loads" and documents the regen-wipes-inject caveat for multi-page generator projects. - New Handle fallback section covers the three wrap error shapes and how the agent should manually wrap for preview and commit to real source on accept. - Handle accept documents the new `data-impeccable-variant` wrapper and the carbonize agent's duty to strip it. ## Prefetch feature (landed but disabled) A `prefetch` event fires from the browser on first CONFIGURING per route so the agent can pre-Read the source file before Go. Real latency win in the linger-before-Go case but costs a harness round trip when Go fires quickly. Disabled via a `PREFETCH_ENABLED = false` flag in `live-browser.js`; server validator and skill dispatch stay so re- enabling (with a browser-side debounce) is a one-line change. ## Harness guidance Earlier skill rewrite compressed two load-bearing instructions: - Restored prescriptive wording for "open the tab via Chrome MCP before the first poll" and the Claude Code background-poll policy. - Flag-mapping for `live-wrap` rewritten as explicit bullets so models don't collapse `--element-id`/`--classes`/`--tag` into a single `--query` argument. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
70 lines
2.2 KiB
JavaScript
70 lines
2.2 KiB
JavaScript
/**
|
|
* Decide whether a given file is "generated" (regenerated by a build step,
|
|
* unsafe to write variants into) or "source" (safe to edit, changes persist).
|
|
*
|
|
* Why this matters: when the user picks an element on a page whose underlying
|
|
* file is regenerated by a build step (e.g. `scripts/build-sub-pages.js`
|
|
* rewriting `public/docs/*.html`), writing variants or accepted changes into
|
|
* that file is silent data loss — the next build wipes them.
|
|
*
|
|
* Signals, in order of reliability:
|
|
* 1. Git check-ignore: gitignored files are assumed generated.
|
|
* 2. File-header markers ("GENERATED", "DO NOT EDIT", "AUTO-GENERATED")
|
|
* within the first ~300 characters — catches non-git projects.
|
|
*/
|
|
|
|
import { execSync } from 'node:child_process';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
const HEADER_SCAN_BYTES = 300;
|
|
const HEADER_MARKERS = [
|
|
/@generated\b/i,
|
|
/\bGENERATED\s+FILE\b/,
|
|
/\bAUTO-?GENERATED\b/i,
|
|
/\bDO\s+NOT\s+EDIT\b/i,
|
|
];
|
|
|
|
/**
|
|
* @param {string} filePath - absolute or cwd-relative path
|
|
* @param {object} [options]
|
|
* @param {string} [options.cwd] - project root (defaults to process.cwd())
|
|
*/
|
|
export function isGeneratedFile(filePath, options = {}) {
|
|
const cwd = options.cwd || process.cwd();
|
|
const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);
|
|
|
|
if (isGitIgnored(absPath, cwd)) return true;
|
|
if (hasGeneratedHeader(absPath)) return true;
|
|
return false;
|
|
}
|
|
|
|
function isGitIgnored(absPath, cwd) {
|
|
try {
|
|
execSync(`git check-ignore --quiet ${JSON.stringify(absPath)}`, {
|
|
cwd,
|
|
stdio: 'ignore',
|
|
});
|
|
return true; // exit 0 = ignored
|
|
} catch (err) {
|
|
// Exit code 1 = not ignored. Exit code 128 = not a git repo or other error.
|
|
// In both cases, treat as "not known to be ignored."
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function hasGeneratedHeader(absPath) {
|
|
let fd;
|
|
try {
|
|
fd = fs.openSync(absPath, 'r');
|
|
const buf = Buffer.alloc(HEADER_SCAN_BYTES);
|
|
const bytesRead = fs.readSync(fd, buf, 0, HEADER_SCAN_BYTES, 0);
|
|
const head = buf.slice(0, bytesRead).toString('utf-8');
|
|
return HEADER_MARKERS.some((re) => re.test(head));
|
|
} catch {
|
|
return false;
|
|
} finally {
|
|
if (fd !== undefined) { try { fs.closeSync(fd); } catch {} }
|
|
}
|
|
}
|