Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 228 additions & 3 deletions AGENTS.md

Large diffs are not rendered by default.

46 changes: 38 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,12 @@ renders perfectly. Nothing you can run locally reproduces that.
transport acks internally, which is why the SDK templates never touch raw
`postMessage`. The two **SDK-free** templates (`static`, `page-vite`) therefore
ship a small vendored emitter, **`civitai-host.js`**, loaded from the entry
point. Leave it in place.
point. Leave it in place — and if you are retrofitting it into an older app,
note that the file has to be *referenced* as well as copied: `static` loads it
with `<script src="./civitai-host.js"></script>` in `index.html`, `page-vite`
with `import './civitai-host.js';` as the first line of `src/main.jsx`.
`civitai app validate` checks that reference where it can resolve your entry
point, and tells you when it can't.

> ⚠️ **If you adopt `@civitai/blocks-react`, delete `civitai-host.js` in the
> same change.** This is the one situation where removing it is correct, and
Expand Down Expand Up @@ -671,14 +676,36 @@ never installs for them and they are never flagged.

Finally it emits one **advisory** about the
[host handshake](#the-host-handshake-block_ready): if your manifest declares a
`page` surface and **nothing in your source posts `BLOCK_READY`**, `validate`
`page` surface and **nothing your app loads posts `BLOCK_READY`**, `validate`
says so. That is the shape of an app scaffolded before the templates were fixed
(#206) — it renders perfectly everywhere you can look locally and is replaced by
a failure card in the real host. It is a **warning, never an error**: unlike the
lockfile rule (where the platform build provably dies), this one infers
*runtime* behaviour from *static text* and can be wrong, so it must not fail a
correct project. Three things follow:

correct project.

> ⚠️ **Copying `civitai-host.js` in is only half the fix.** A browser never
> fetches a file nothing references, so the emitter has to be *loaded* too — a
> `<script src="./civitai-host.js"></script>` in `index.html`, or an
> `import './civitai-host.js';` at the top of the entry module `index.html`
> loads. Earlier releases of this check looked only for the *text*
> `BLOCK_READY` anywhere in your tree, so an unreferenced copy silenced it and a
> still-broken app validated clean. It now resolves what your `index.html`
> actually loads.

Four things follow:

- **It checks REACHABILITY where it can, and says when it can't.** Starting at
`index.html` it follows every `<script src>`, inline module and `import` it can
resolve, and asks whether any of *those* files posts the message. When that
resolution is complete you get a precise finding — including "you have an
emitter, but nothing loads it". When it *isn't* — no `index.html` at your
project root, a bundler alias (`import '@/…'`), a reference to a file that
isn't there, an import chain deeper than it follows — it falls back to
scanning your whole tree for the text, and the
warning **says so in as many words**: *"it did NOT check that the file is
loaded"*. Read that sentence as it is written; in that mode, adding the emitter
without referencing it will silence the warning and leave the app broken.
- **A dependency that acks ends the check** — today that is
`@civitai/blocks-react`, and nothing else. Its iframe transport acks internally
and the literal never appears in your `src/`, so a `page-money` app is never
Expand All @@ -694,13 +721,16 @@ correct project. Three things follow:
that is a **symlink** into a shared package *is* followed.
- **It stays quiet when it cannot see the whole project.** An unreadable file, a
file over 2 MiB, a very large tree, or a directory holding only a manifest all
mean "we could not look" — reported as nothing, never as a finding.
mean "we could not look" — reported as nothing, never as a finding. Likewise a
project whose entry graph can't be resolved *and* which contains the literal
somewhere: quiet, deliberately, because warning at a correct project is the
more expensive mistake.

If it fires on a project you know is correct — your ack arrives from a bundled
dependency, or from a file type this scan doesn't open — it is a false alarm, and
it never blocks (exit 0) unless you pass `--strict`. What it proves is narrow:
that the message is *mentioned* in code. It cannot prove the ack ever **fires**;
only the real host can.
it never blocks (exit 0) unless you pass `--strict`. What it proves stays narrow
even at its strongest: that a file your `index.html` really loads *mentions* the
message. It cannot prove the ack ever **fires**; only the real host can.

`civitai app submit` prints the same warnings before it uploads, and likewise
does not block on them.
Expand Down
116 changes: 116 additions & 0 deletions internal/blockproto/comments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package blockproto

// comments.go is the ONE comment stripper in this repo.
//
// It had three copies — `internal/validate/readyack.go`,
// `internal/scaffold/ready_ack_contract_test.go`, and the entry-graph resolver
// next door — asking the identical question: "does this file MENTION the token
// outside a comment?" A predicate open-coded at N sites is typically wrong at
// N-1 of them, so it lives here once, with the two rules that were each earned
// by a false warning at a correct project.

import "strings"

// MarkupExts are the extensions whose comments are `<!-- -->`. HTML comment
// stripping is applied ONLY to these.
//
// 🔴 Applying it to `.js`/`.ts` was a false-warning source: StripHTMLComments
// has no string awareness, so a perfectly ordinary `var OPEN = '<!--';` in a
// sanitiser started a "comment" that ran to end of file and deleted the emitter
// below it. Reproduced live on a `static` scaffold. JS has no HTML comments
// worth stripping here — `//` and `/* */` cover it, and Annex B's HTML-like
// comment form is vanishingly rare next to the literal appearing in a string.
var MarkupExts = map[string]bool{
".html": true, ".htm": true, ".vue": true, ".svelte": true, ".astro": true,
}

// StripCommentsForExt removes comments from src, given its file extension. JS
// line/block comments are always stripped, leaving string and template literals
// intact (so `'https://x'` is not read as the start of a comment, and an emitter
// that posts from inside a string still counts). HTML comments are stripped only
// for markup files — see MarkupExts for why that gate exists.
func StripCommentsForExt(src, ext string) string {
if MarkupExts[strings.ToLower(ext)] {
src = StripHTMLComments(src)
}
return StripJSComments(src)
}

// StripHTMLComments removes `<!-- … -->` pairs.
func StripHTMLComments(src string) string {
var b strings.Builder
b.Grow(len(src))
for i := 0; i < len(src); {
if strings.HasPrefix(src[i:], "<!--") {
end := strings.Index(src[i+4:], "-->")
if end < 0 {
// 🔴 UNTERMINATED. Keep the remainder VERBATIM rather than
// discarding it. Dropping the tail is lossy in the expensive
// direction — everything below an unclosed `<!--` disappears,
// including an emitter, and the author gets a warning about a
// correct project. There is no upside to the lossy branch: text
// after an unclosed marker is not evidence of a comment, it is
// evidence the file is not what we assumed.
b.WriteString(src[i:])
return b.String()
}
i += 4 + end + 3
continue
}
b.WriteByte(src[i])
i++
}
return b.String()
}

// StripJSComments removes // and /* */ comments while respecting string and
// template literals, so a URL like 'https://x' is not mistaken for a comment
// and a comment mentioning a filename cannot satisfy a wiring check.
func StripJSComments(src string) string {
var b strings.Builder
b.Grow(len(src))
for i := 0; i < len(src); {
c := src[i]
switch {
case c == '/' && i+1 < len(src) && src[i+1] == '/':
for i < len(src) && src[i] != '\n' {
i++
}
case c == '/' && i+1 < len(src) && src[i+1] == '*':
// 🔴 UNTERMINATED — the same rule StripHTMLComments carries, for the
// same reason, which used to be documented only on that half. An
// unclosed `/*` swallowed the rest of the file: lossy in the
// EXPENSIVE direction, because everything below it disappears —
// including an emitter — and the author gets a warning about a
// correct project. Text after an unclosed marker is not evidence of
// a comment, it is evidence the file is not what we assumed.
end := strings.Index(src[i+2:], "*/")
if end < 0 {
b.WriteString(src[i:])
return b.String()
}
i += 2 + end + 2
case c == '\'' || c == '"' || c == '`':
quote := c
b.WriteByte(c)
i++
for i < len(src) {
if src[i] == '\\' && i+1 < len(src) {
b.WriteString(src[i : i+2])
i += 2
continue
}
b.WriteByte(src[i])
if src[i] == quote {
i++
break
}
i++
}
default:
b.WriteByte(c)
i++
}
}
return b.String()
}
158 changes: 158 additions & 0 deletions internal/blockproto/comments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package blockproto

import "testing"

// The comment stripper decides whether a mention of a token is EVIDENCE or just
// prose. Both directions are expensive:
//
// strip too little -> a comment naming BLOCK_READY silences the check at a
// genuinely broken app (false negative)
// strip too much -> real code disappears and a correct project is warned at
// (false positive, the one AGENTS.md item 10 exists for)
//
// These cases are built so a widening or narrowing of each internal branch
// changes the ANSWER, not just the whitespace. Several exist because the audit's
// sweep found the branch unpinned.

func TestStripHTMLComments(t *testing.T) {
cases := []struct{ name, src, want string }{
{
// 🔴 The TERMINATOR is `-->`, three characters. Widening it to `->`
// ends the comment early, so everything after the arrow becomes
// "code" — a commented-out mention turns into evidence and the check
// goes silently quiet at a broken app. An `->` inside a comment is
// ordinary prose (and an arrow function's `=>` is one keystroke away).
name: "an arrow inside a comment does not terminate it",
src: "<!-- the host -> block handshake posts BLOCK_READY -->after",
want: "after",
},
{
name: "a normal comment is removed",
src: "a<!-- b -->c",
want: "ac",
},
{
// 🔴 UNTERMINATED: keep the tail verbatim. Dropping it is lossy in
// the expensive direction — everything below an unclosed marker
// disappears, including an emitter.
name: "an unterminated comment keeps the rest of the file",
src: "a<!-- b\nBLOCK_READY",
want: "a<!-- b\nBLOCK_READY",
},
{
name: "two comments are both removed",
src: "a<!--x-->b<!--y-->c",
want: "abc",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := StripHTMLComments(c.src); got != c.want {
t.Fatalf("StripHTMLComments(%q) = %q, want %q", c.src, got, c.want)
}
})
}
}

func TestStripJSComments(t *testing.T) {
cases := []struct{ name, src, want string }{
{
name: "a line comment goes, the code after it stays",
src: "// BLOCK_READY\nkeep;",
want: "\nkeep;",
},
{
name: "a block comment goes",
src: "a;/* BLOCK_READY */b;",
want: "a;b;",
},
{
// 🔴 UNTERMINATED `/*` — the same rule the HTML half carries, which
// used to be documented and implemented only there. Swallowing the
// tail deletes an emitter and warns at a correct project.
name: "an unterminated block comment keeps the rest of the file",
src: "a;/* oops\nBLOCK_READY;",
want: "a;/* oops\nBLOCK_READY;",
},
{
// 🟢 The TERMINATOR is `*/`, two characters. Widening it to `*`
// ends the comment at the first star, so the tail of the comment
// becomes "code" — a commented-out mention turns into evidence and
// the check goes quiet at a broken app. Only a distant test in
// internal/validate caught this; the HTML half had a dedicated case
// and the JS half did not.
name: "a star inside a block comment does not terminate it",
src: "a;/* 2 * 3 BLOCK_READY */b;",
want: "a;b;",
},
{
// The string branch: a URL is not a comment. The ack is on the SAME
// LINE on purpose — with quote handling defeated the `//` in
// `https://` swallows everything after it.
name: "a URL in a string does not open a comment",
src: "var u = 'https://x'; BLOCK_READY;",
want: "var u = 'https://x'; BLOCK_READY;",
},
{
// The BACKTICK branch, which nothing pinned: a template literal is a
// string too, and a `//` inside one is not a comment.
name: "a template literal protects its contents",
src: "var u = `https://x`; BLOCK_READY;",
want: "var u = `https://x`; BLOCK_READY;",
},
{
// The BACKSLASH-ESCAPE branch, which nothing pinned. Without it the
// escaped quote is read as the string's END, so the rest of the line
// is parsed as code — and the `//` that follows becomes a comment
// that eats the ack.
name: "an escaped quote does not end the string early",
src: `var s = 'it\'s // not a comment'; BLOCK_READY;`,
want: `var s = 'it\'s // not a comment'; BLOCK_READY;`,
},
{
// The inverse control for the branch above: an escape must not make
// the stripper run past a string that really does end.
name: "an escaped backslash still ends the string",
src: `var s = 'x\\'; // gone` + "\nBLOCK_READY;",
want: `var s = 'x\\'; ` + "\nBLOCK_READY;",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := StripJSComments(c.src); got != c.want {
t.Fatalf("StripJSComments(%q) = %q, want %q", c.src, got, c.want)
}
})
}
}

// TestStripCommentsForExtGate pins the extension gate in BOTH directions.
//
// 🔴 Running StripHTMLComments over `.js` made an ordinary `var OPEN = '<!--';`
// open a "comment" that ran to EOF and deleted the emitter below it — a false
// warning at a correct project, reproduced live. The inverse control matters as
// much: without it, "don't strip HTML in .js" is indistinguishable from "don't
// strip HTML at all", which would let a real HTML comment count as evidence.
func TestStripCommentsForExtGate(t *testing.T) {
const js = "var OPEN = '<!--';\nBLOCK_READY;\nvar CLOSE = '-->';\n"
if got := StripCommentsForExt(js, ".js"); !contains(got, "BLOCK_READY") {
t.Fatalf(".js lost code between two HTML markers in strings: %q", got)
}
const html = "<!-- BLOCK_READY -->\n<script>keep;</script>"
if got := StripCommentsForExt(html, ".html"); contains(got, "BLOCK_READY") {
t.Fatalf(".html kept a real HTML comment: %q", got)
}
// The extension match is case-insensitive — an `.HTML` file is markup.
if got := StripCommentsForExt(html, ".HTML"); contains(got, "BLOCK_READY") {
t.Fatalf(".HTML was not treated as markup: %q", got)
}
}

func contains(s, sub string) bool {
for i := 0; i+len(sub) <= len(s); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
}
Loading
Loading