Skip to content
Closed
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
40 changes: 40 additions & 0 deletions .github/workflows/bump-scaffold-pins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,46 @@ permissions:
pull-requests: write

jobs:
# Runtime half of the ready-ack contract (issue #206). The offline guard in
# `internal/scaffold/ready_ack_contract_test.go` proves each SDK-free page
# template ships a byte-identical copy of `internal/blockproto/ready-ack.js`
# and that the entry point loads it — it CANNOT prove the ack fires, because
# no Go static check can prove JavaScript executes. This job runs the emitter
# in node against a fake host and asserts a single correctly-addressed
# `{type:'BLOCK_READY',payload:{height:0}}` goes out.
#
# 🔴 This is the DRIFT runner, NOT the gate. The gate is the identically-named
# job in ci.yml, which runs on every pull request. A daily job cannot block a
# merge — a regression would land green and surface up to 24h later, on a
# workflow with no dependent job — so this copy alone would be a guard that
# gates nothing. It exists to catch drift on `main` between PRs (a merged-tree
# interaction, a node bump, an npm-side change), and is deliberately
# independent of the `bump` job below so it runs on every sweep, including the
# usual no-op ones where the pins are already current.
ready-ack-runtime:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: "1.25"
cache: true

- uses: actions/setup-node@v4
with:
node-version: "22"

# -v so the run REPORTS its controls (an inert emitter and a naive ack are
# both driven through the same assertions and must be rejected) rather than
# just an exit code.
- name: scaffolded ready-ack actually fires
env:
CIVITAI_CHECK_SCAFFOLD_RUNTIME: "1"
run: go test ./internal/scaffold -run TestScaffoldedReadyAckActuallyFires -count=1 -v

bump:
runs-on: ubuntu-latest
steps:
Expand Down
110 changes: 110 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,116 @@ jobs:
CIVITAI_CHECK_PUBLISHED_PINS: "1"
run: go test ./internal/scaffold -run TestScaffoldPinsSatisfyPublished -v

# Guard B for the block->host ready-ack (issue #206), and the half that GATES.
#
# `internal/scaffold/ready_ack_contract_test.go` runs in the build-test job
# above and proves each SDK-free page template ships a byte-identical copy of
# `internal/blockproto/ready-ack.js` that the entry point RESOLVES to. It
# cannot prove the ack FIRES — no Go static check can prove JavaScript
# executes, and an emitter with an inverted `event.source` check passes every
# static assertion while being completely inert.
#
# This job executes the emitter in node against a fake host. It is env-gated
# so `make ci` stays Go-only and offline. The daily `bump-scaffold-pins` sweep
# runs it too, but a DAILY runner gates nothing: a regression would merge green
# and surface up to 24h later, on a workflow with no dependent job. A PR check
# is what makes it a guard.
#
# -v so the run REPORTS its controls (an inert emitter and a naive ack are
# driven through the same assertions and must be rejected) rather than just an
# exit code.
ready-ack-runtime:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: "1.25"
cache: true

- uses: actions/setup-node@v4
with:
node-version: "22"

- name: scaffolded ready-ack actually fires
env:
CIVITAI_CHECK_SCAFFOLD_RUNTIME: "1"
run: go test ./internal/scaffold -run TestScaffoldedReadyAckActuallyFires -count=1 -v

# Rot-guard: build the page-vite template. It is the only SDK-free template
# with a build step, and nothing else in CI builds it — `template-page-money`
# and `scaffold-currency` both scaffold page-money.
#
# Since #206 its entry module has a HARD RESOLUTION DEPENDENCY on
# `./civitai-host.js`, so a wrong import path is a build failure rather than a
# subtle one. Measured: `import '../nonexistent/civitai-host.js'` survived the
# entire Go suite, and would have shipped a template that cannot build at all.
#
# The bundle assertion is separate from Guard A on purpose. Guard A pins the
# SOURCE tree; Vite output is what the platform actually serves, and a
# tree-shaken or mis-externalised side-effect import would leave a bundle with
# no handshake in it. Control pair, both measured locally: with the import the
# bundle carries BLOCK_INIT/BLOCK_READY, with it commented out it carries
# neither — while the positive control (the app's display name) matches in
# both cases, proving the grep reads the bundle either way.
template-page-vite:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: "1.25"
cache: true

- uses: actions/setup-node@v4
with:
node-version: "22"

- name: scaffold page-vite
run: go run ./cmd/civitai app init "CI Vite Block" --dir ./_ci-page-vite --template page-vite

- name: install
working-directory: ./_ci-page-vite
run: npm install --no-audit --no-fund

- name: build
working-directory: ./_ci-page-vite
run: npm run build

- name: the ready-ack survives bundling
working-directory: ./_ci-page-vite
run: |
set -euo pipefail
bundles=$(find dist -name '*.js' -type f | wc -l)
echo "JS files emitted into dist/: $bundles"
if [ "$bundles" -lt 1 ]; then
echo "::error::the build emitted no JS into dist/ — nothing to check, so a pass here would be meaningless"
exit 1
fi
# POSITIVE CONTROL for the grep itself, FIRST: a string that must be in
# the bundle whatever the handshake does. If this misses, the path or
# the pattern is wrong and the two assertions below prove nothing.
control=$(grep -rlF 'CI Vite Block' dist/ | wc -l)
echo "positive control (app display name): $control file(s)"
if [ "$control" -lt 1 ]; then
echo "::error::positive control failed — this step is not reading the built bundle"
exit 1
fi
for needle in BLOCK_INIT BLOCK_READY; do
n=$(grep -rlF "$needle" dist/ | wc -l)
echo "$needle: $n file(s)"
if [ "$n" -lt 1 ]; then
echo "::error::$needle is absent from the built bundle — the ready-ack did not survive the build, so the shipped app never says hello (issue #206)"
exit 1
fi
done

- name: validate
working-directory: ./_ci-page-vite
run: go run ../cmd/civitai app validate

# Rot-guard: scaffold the page-money template and run its JS toolchain against
# the PUBLISHED SDK (@civitai/blocks-react). Catches a template that renders
# but no longer installs / typechecks / tests / builds after an SDK bump — the
Expand Down
134 changes: 129 additions & 5 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ func newWhoAmICmd() *cobra.Command {

## Intentional decisions that look wrong (read before "fixing")

Items 1–3 are deliberate mirrors of the platform (items 4 and 8 are deliberate
*non*-mirrors); items 5–8 cover `civitai app metrics`, the CLI's only analytics
read path. The durable fix for the mirroring is a server-side
Items 1–3, 10 and 11 are deliberate mirrors of the platform (items 4 and 8 are
deliberate *non*-mirrors); items 5–9 cover `civitai app metrics`, the CLI's only
analytics read path. The durable fix for the mirroring is a server-side
`civitai app validate` endpoint that calls the real `BlockManifestValidator` —
until that exists, vendoring is on purpose.

Expand Down Expand Up @@ -290,8 +290,9 @@ until that exists, vendoring is on purpose.
`Origin: null` through the same `DialLocalDevServer` the proxy uses, and
reads real response headers. `CheckParentOrigins` is a **heuristic** —
`VITE_BLOCK_ALLOWED_PARENT_ORIGINS` is inlined into the bundle at transform
time and cannot be observed over HTTP, so it is a **third vendored mirror**
(alongside `schema/` and the slot registry): `resolveViteEnv` reproduces
time and cannot be observed over HTTP, so it is a **vendored mirror**
(one of four — `schema/`, the slot registry, this, and the ready-ack
emitter of item 11): `resolveViteEnv` reproduces
Vite's dev env resolution, where `.env.<mode>` beats `.env` and a REAL
process env var beats every file (dotenv does not overwrite existing vars).
Getting that last rule backwards warns at authors who exported the value in
Expand Down Expand Up @@ -355,6 +356,129 @@ until that exists, vendoring is on purpose.
absolute redirect, which silently turns the tunnel-Host probe into an ordinary
loopback request unless it is re-applied.

11. **The scaffold VENDORS the block→host ready-ack (`internal/blockproto/`),
and it is the FOURTH vendored mirror.** (Provenance: read against
`civitai@35a9598dc9`. The contract lives in
`src/components/AppBlocks/PageBlockHost.tsx` and
`src/components/AppBlocks/usePostMessage.ts` — note `usePostMessage.ts` is
under `AppBlocks/`, **not** `src/hooks/`, where it has never lived.) A
`page` app is not shown by the host until it posts `BLOCK_READY`; that
handler in `PageBlockHost.tsx` is the only transition into `ready`. `page-money` gets this free —
`@civitai/blocks-react`'s `IframeTransport` acks from its validated-
`BLOCK_INIT` branch — but `static` and `page-vite` are deliberately
SDK-free, so they have to say hello themselves. They didn't, and issue #206
was the result: measured against the real `PageBlockHost` in Chromium, both
templates rendered fine and NEVER reached ready, ending at a visible
failure card after ~37s of bounded auto-retry (`MAX_AUTO_RETRIES = 2`,
backoff `[2000, 5000]`). This was an original omission, not a regression.
- **One authority, copied — not templated.** `ready-ack.js` is `go:embed`ed
in `internal/blockproto` and written VERBATIM by `scaffold.Render` (no
`text/template` pass) into the path `Template.ReadyAckPath()` names. Do
NOT add a per-template `.tmpl` copy: two hand-maintained copies drift, and
drift is invisible locally — the app renders and dies only inside the
real host.
- **Ack on `BLOCK_INIT`, never on load** — and on INIT *only*. Not because
on-load is broken: an on-load poster was **measured working** (ready in
178–265 ms) in the same harness. On-INIT is chosen for robustness. The
host registers its `BLOCK_READY` subscriber inside an effect and
**silently drops** a message whose type has no subscriber yet, with no
retry (`usePostMessage.ts`), while it re-posts `BLOCK_INIT` every ~400 ms
until acked (`iframeInitController.ts`) — so answering INIT removes the
race. It also hands over `event.origin`, so nothing posts to `'*'`; it is
late enough not to reveal an empty interactive frame (the host fades its
launch overlay and enables `pointerEvents` on ready); and it is what the
SDK's own transport does. Repeats must stay a no-op: the host's inbound
channel is rate-limited across ALL types, so re-acking every retry can
starve `BLOCK_ERROR`.
- **The envelope is `{ type, payload }`.** The host dispatches
`data.payload` to subscribers, so top-level fields arrive as
`payload: undefined`. The host happens to ignore the `BLOCK_READY`
payload, so a wrong envelope would not break *this* message — it teaches
the wrong shape for every message the author adds next. That is why the
guards assert the envelope even though the ack would work without it.
- 🔴 **It pins the sender WINDOW, not the sender ORIGIN — and that is a
deliberate boundary, not an oversight.** The emitter answers
`window.parent` and replies to whatever origin that window sent from. It
establishes "our embedder", NOT "Civitai". Sound for this one message
(the ack carries `{height: 0}` and discloses nothing a page that chose to
frame us doesn't already know) and **insufficient for anything carrying
data**. We deliberately do NOT vendor an origin allowlist here: the real
set (production, preview subdomains, `dev-*.civit.ai` tunnels) is platform
state that moves without notice, so it would become a FIFTH mirror needing
lockstep, and getting it wrong silently breaks the dev tunnel — while
`@civitai/blocks-react` already maintains exactly that list from
`VITE_BLOCK_ALLOWED_PARENT_ORIGINS`. So the decision is: keep the emitter
minimal, and say loudly in both scaffold READMEs and the emitter header
that adopting the SDK is the prerequisite for handling inbound data. If
you ever do add an allowlist here, it is a new vendored mirror and needs a
drift check.
- 🔴 **Adopting the SDK means DELETING the emitter in the same change, and
the docs must keep saying so.** Whichever handshake answers the host's
first `BLOCK_INIT` calls `notifyReady()` → `stop()`, which clears the
retry interval **and** the readiness timeout
(`iframeInitController.ts`). If the vendored emitter wins that race
against a freshly-added `IframeTransport`, the SDK's `waitForInit` rejects
at its own `INIT_TIMEOUT_MS = 10_000` and the host sits in `ready` showing
an app that never started — no retry, no failure card. That is strictly
worse than #206 and it is silent. Narrow window, but the upgrade path is
the one place the "don't delete it" instruction reverses, so it is called
out with the mechanism rather than as a footnote.
- **The `acked` latch is set AFTER the post, not before.** `postMessage`
throws `SyntaxError` on a `targetOrigin` it cannot parse as a URL —
measured in Chromium 1228, `postMessage(msg, 'null')` — and `event.origin`
is the string `"null"` whenever the sender is itself at an opaque origin.
Latching first would make one throw permanently silent while the host
keeps retrying at a listener that has given up. Not reachable through
today's `PageBlockHost`; pinned anyway by Guard B's
`--throw-first-post` mode.
- **`RESIZE_IFRAME` is deliberately ABSENT from the page templates.** Both
raw templates used to demo it; `hostHandlerParity.ts` marks it **N/A for
`PageBlockHost`** (a page block fills the surface and does not size to
content), so it was inert advice that also modelled the wrong envelope.
Don't reintroduce it as a "minimal example". Note `iframe.resizable` in
the vendored `schema/` still describes itself in size-to-content terms;
that is a schema-side wording issue, not a licence to re-add the message.
- **Three guards, and none subsumes the others.**
`ready_ack_contract_test.go` (Guard A, runs in `make ci`) enumerates
`AllTemplates()`, decides subject-hood from the RENDERED manifest and
package.json — never a hardcoded list — and asserts byte-equality with
`blockproto.ReadyAckSource()` plus that the entry point RESOLVES to it,
with a count floor so a guard wired to nothing can't report a serene pass.
🔴 **"Resolves" is load-bearing and was earned the hard way**: the first
version matched a BASENAME, and two mutants shipped a broken app past the
entire suite — `src="./vendor/civitai-host.js"` (a 404) and
`import '../nonexistent/civitai-host.js'` (a build failure). Never
reintroduce a basename or `strings.Contains` match here; every reference
must resolve to a path and be compared with where the emitter actually is.
Guard A still **cannot prove the ack fires**: an inverted `event.source`
check passes every static assertion.
`ready_ack_runtime_test.go` (Guard B) executes the emitter in node against
a fake host and reads the outbound message. It is env-gated
(`CIVITAI_CHECK_SCAFFOLD_RUNTIME=1`) and has TWO runners, of which only
the first gates: the `ready-ack-runtime` job in **`ci.yml`** (every PR)
and the same-named job in `bump-scaffold-pins.yml` (daily drift). Shipping
only the daily one — as the first version did — means the very failure
Guard B exists to catch merges green and is reported up to 24h later, on a
workflow nothing depends on.
The `template-page-vite` job in `ci.yml` is the third: it is the only
thing that BUILDS an SDK-free template, and it asserts the ack survives
bundling (Guard A pins the source tree; Vite output is what the platform
serves).
If you add a template, add nothing — Guard A picks it up automatically and
fails until `ReadyAckPath()` is set.
- **`BLOCK_HELLO` now exists host-side, and the emitter deliberately does
not send it.** When this landed the host had zero references to it; as of
`civitai@35a9598dc9` it has real ones (`iframeInitController.notifyHello`,
`hostHandlerParity.ts`, host browser tests). Re-verified: it is an
**accelerator, not a gate** — `notifyHello()` leaves the retry interval
and the readiness timeout armed by `start()` untouched, and its own
comment forbids it ever becoming a precondition for sending init. So a
block that never announces is served by the unchanged retry loop, which is
exactly what our emitter relies on. Not sending it costs only the latency
of one retry tick, and sending it would add a second vendored message for
no correctness gain. If you reconsider, note the init-fragment fast path
ships gated off and refuses `surface === 'dev-tunnel'` by construction.

**When you change a validation rule, keep both vendored mirrors (`schema/` + the
ported Go checks in `internal/validate/`, including the slot registry) in sync
with the server, and update `examples_test.go` (asserts shipped examples
Expand Down
Loading
Loading