Skip to content

refactor(preview): keep the preview credential out of the embed graph - #218

Merged
sydevs-bot merged 8 commits into
mainfrom
claude/refactor-217-preview-decorator-seam
Sep 21, 2026
Merged

sydevs-bot merged 8 commits into
mainfrom
claude/refactor-217-preview-decorator-seam

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • The embedded <sahaj-atlas> element no longer carries the code that spends SahajCloud's live-preview credential, nor the header name it spends it under.
  • applyRequestContext ends in one nullable call — livePreview.decorateRequest. config/live-preview/request.ts holds the decorator and is standalone-only.
  • No byte change in any graph. This buys a structural guarantee, not size.

Changes

  • src/config/api/client.ts — the preview branch becomes livePreview.decorateRequest?.(url, headers), called last. Nothing new is exported.
  • src/config/live-preview/request.ts — new. LIVE_PREVIEW_HEADER and the one decorator that sets it, gate included.
  • src/config/live-preview/protocol.ts — loses LIVE_PREVIEW_HEADER, gains the decorateRequest field and the erased type it is declared with. Stays in both graphs: App and RegistrationForm read the session to select restraints, and a restraint is not the credential.
  • src/config/live-preview/boot.ts — hangs the decorator off the session immediately before flipping active. Capture sets the field null, so it disarms.
  • src/config/live-preview/boot.test.ts — pins that registration, in both directions.
  • src/Widget.standalone.test.ts — the header scan of the widget graph, each standalone-only module's closed importer list, and the session's closed writer list.
  • docs/rules/data-layer.md, docs/architecture.md, AGENTS.md — the seam, step 6, the layout line.

Preview

Branch alias: https://claude-refactor-217-preview.sahajatlas.pages.dev (the latest commit is still deploying). Nothing here is visible: no UI, no route, and no host-observable behaviour changed, so there is nothing to deep-link. Ladle is unaffected.

Contract impact

None. The requests the embedded widget makes are byte-identical, no origin or CSP entry changed, and no attribute was added. Per CHANGELOG.md's own rule — it records only what a host can observe — this gets no entry and docs/embedding.md is unchanged.

Test results

  • Lean gate: ✓ pnpm lint && pnpm typecheck && pnpm test:run — 122 files, 1566 tests
  • Build: ✓ pnpm build (incl. assert:css, assert:maps)
  • pnpm size: ✓ standalone 295.1 / 308, loader 2.6 / 3.5, embed 296.9 / 308 KiB. Identical to a build of origin/main measured on this machine — all three graphs, to 0.1 KiB, and unmoved by either review pass. The under-budget arm is unmoved.
  • The shipped bytes were also grepped by hand on b7072f6: x-sahajcloud-preview-secret appears in exactly one emitted file, assets/main-*.js, which pnpm size scores under standalone and not under embed.
  • Every new assertion was driven red at its causing site before being left green. Details below.

Manual verification

Both need a real SahajCloud session — neither is reachable from a spec.

  1. /admin → Sahaj Atlas → Translations: an unpublished string still renders. This proves the credential still rides the standalone read.
  2. /admin → an Event → live preview: a title edit still lands on screen. This proves the populate request still carries the header.

Notes for reviewer

The change trades an impossible failure for a silent one, and that is the judgement worth checking. Today the branch cannot fire in a host page because nothing there writes the session. After this, the branch is not in that graph at all — but a standalone session whose registration is dropped previews published content and says nothing. The retargeted specs and the two checks above stand in for the compiler. The ticket's open question (a dev-only warning for an active session with no decorator) is not implemented — it asks for a decision nobody has made, and none of the acceptance criteria need it.

The simplification pass is ab09890 and b7072f6, and each is separately revertable.

  • ab09890 moves the decorator onto the session. livePreview was already a mutable singleton in both graphs with a closed writer list, asserted before this branch existed. Reusing it deleted a type export, a module binding, a setter, the setter's own closed caller list, and six teardown lines — and client.ts's diff against main is now one call line. One list guards opening a session and spending its credential, and every Object.assign(livePreview, LIVE_PREVIEW_INACTIVE) disarms the credential for free.
    The cost, stated plainly: client.ts names a live-preview field again, where the slot version was feature-neutral. It imported livePreview on main too, so no module enters the embed graph that was not already there — but if the generic slot is worth its extra parts, revert this commit and the rest of the branch stands.
  • b7072f6 deletes the pnpm size arm, which was the one change on this branch beyond the acceptance criteria. Criterion 3 asks for a source-level scan, and Widget.standalone.test.ts is it. The arm's one unique gap was a carrier arriving from a dependency; no dependency spells SahajCloud's private header, and 54 lines of script plus a positive control is a lot to hold that. The AGENTS.md paragraph describing it went with it.

Dismissed findings, each with a reason:

  • Register the decorator at boot.ts module scope instead of inside activateLivePreview. Simpler, and the decorator self-gates, so it would be safe. Declined: the ticket specifies registration after verification, and it is a second independent condition guarding a credential. Worth overruling me if you disagree — it would delete an ordering rule that exists only as prose.
  • Import LIVE_PREVIEW_HEADER in fetch.test.ts instead of spelling the literal. Declined: those four literals are the only place anything pins the wire name to its actual value. Widget.standalone.test.ts reads it off the constant, so importing it here would leave nothing asserting the string SahajCloud's CORS allowlist matches on.

Considered and not taken: merging request.ts into boot.ts, which would drop a module and inherit boot.ts's pre-existing exclusion from the widget graph. It costs more than it saves. request.ts is a leaf, so three specs and the header scan read the wire name off it without pulling in client.ts and i18next; via boot.ts each would need vi.mock lines it does not have today.

Out of scope, noticed on the way: docs/testing.md's "keep the lane under ~5s" is stale — the lane measures 20.1s on this branch, and 22.3s on main. Not filed as a ticket; it is one number in a doc, and it is here so it is not lost.

Detail

Driving each assertion red, at the site that caused it (docs/testing.md):

assertion reintroduction result
literal absent from the widget graph constant back in protocol.ts red, names protocol.ts
literal present in request.ts same red, two carriers
request.ts unreachable from Widget.tsx client.ts imports the decorator red
request.ts's closed importer list same red, names client.ts
same request with no decorator branch back inside applyRequestContext red
the four header assertions in the two specs registration dropped from setup all four red
the decorator is registered at all registration line cut from boot.ts red, names the missing header
the registration sits after the verify it moved above if (!verified) red

The last two were re-driven on ab09890's shape, against the session field rather than the slot. The first attempt at the old pnpm size leak arm did not fire: an unused export const in protocol.ts is tree-shaken, so it never reached the bytes. That was the reason both checks existed — the source scan catches a parked constant, the artifact scan catches a shipped one — and b7072f6 weighs that one remaining case against the arm's cost.

Why a field on the session and not a slot of its own. One decorator will ever exist, so a list would make iteration order an implicit contract for no gain, and applyRequestContext stays the one place cross-cutting context attaches. Given that, the field is smaller than a slot: it needs no setter and no second guard. A review pass also considered threading the decorator as an argument and splitting client.ts per entry; the first needs a factory plus a shared instance holder (module-level mutable state again, by a longer path), and the second duplicates auth and locale, the one thing docs/rules/data-layer.md insists exists once.

A security review found nothing at confidence ≥ 8 on the slot version. What ab09890 changes about its findings: the gate still fails closed (livePreview.active && livePreview.token is unmoved, and the one production write sits after activateLivePreview's if (!verified) early return), and capture now writes the field null, so a second token arriving mid-session cannot inherit the first one's decorator. The decorator re-reads the singleton rather than closing over a token, so a never-cleared field is inert. Reachability from the embedded surface is now a property of request.ts alone, since client.ts exports nothing to call.

No import cycle. request.ts takes both the session and the erased type from protocol.ts, and client.ts imports only the session, as it did on main.

Docs swept: docs/rules/data-layer.md (the seam, plus request.ts added to its paths: so the rule loads there), docs/architecture.md (step 6), the root AGENTS.md (the live-preview/ layout line). docs/testing.md, docs/environment.md and docs/rules/i18n-and-state.md mention applyRequestContext but say nothing this change makes stale.

Commit order. c9ec1ee is the change; 7b5bbe8 folds three near-identical importer scans into one helper and cuts the prose where one fact was stated in eleven places; cc5d10b, 8b171c5 and de3e43f answer the adversarial review one thread each; ab09890 and b7072f6 are the simplification pass above. 8b171c5's closed caller list is gone with the setter it guarded, and its finding — a widget-graph write could spell the 30-character header itself — is now carried by the session's writer list.

Closes #217

🤖 Written by the sydevs autonomous loop — see what this is

The embedded `<sahaj-atlas>` element shipped the code that spends SahajCloud's
live-preview credential, though no host page can open a session. #215 closed the
session's writer list to `boot.ts`; this closes the other half.

`applyRequestContext` now ends in one named decorator slot instead of a preview
branch. `config/live-preview/request.ts` holds the decorator, standalone-only,
and `boot.ts` registers it on the line that flips `active` — before the flip, in
the same synchronous step, so no request can see an open session with an empty
slot.

**Moving `LIVE_PREVIEW_HEADER` is the part that makes this real.** `protocol.ts`
is in both entry graphs on purpose, so a constant left there keeps the wire name
in every host's bundle, and a writer needs nothing else. It lives beside the one
decorator that sets it now.

A slot, not a list: only one decorator will ever exist, a list would make
iteration order an implicit contract, and a named setter keeps "who can spend
this credential" a one-hit grep.

`Widget.standalone.test.ts` gains three assertions — the graph walk over
`request.ts`, a closed importer list for it, and a scan of the widget entry's
graph for the header literal, read off the real constant so a rename cannot make
it vacuous. Each was driven red at its causing site: the constant back in
`protocol.ts`, and `client.ts` importing the decorator directly.

The two specs that assert the credential now register the real decorator in
setup. Without that they assert an absence and stay green against a seam that
never fires; dropping the registration turns all four header assertions red.

This trades an impossible failure for a silent one. A standalone session whose
registration is dropped previews published content and says nothing — the
retargeted specs and the two `/admin` checks stand in for the compiler.

Refs #217.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TecGGqBhAjFnCuoC3eevCH
Review-pass follow-ups on the same change.

`Widget.standalone.test.ts` had three near-identical importer scans and grew
a fourth. `importersOf` replaces two of them under one `describe.each`, and
the module paths are named constants, so `STANDALONE_ONLY` stops being half a
source of truth. A `read` helper matches the idiom the other scanning specs
already use. Net: one fewer full pass over `src/`.

The session-writer docblock still argued from the threat this change removed —
that a widget-graph write would send a CMS credential. It cannot any more. The
gate is still worth closing, because such a write opens a session on a host
page we do not own: inert links, snapped-back navigation, a refused
registration. That is now its whole justification, so it says so.

One fact — request.ts is standalone-only, and why — was stated in eleven
places. Kept where it is load-bearing, cut where it was restatement. No ⚠ line
and no breadcrumb was dropped.

Refs #217.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TecGGqBhAjFnCuoC3eevCH
`Widget.standalone.test.ts` proves the widget's `src/` graph does not reach
the credential header. This proves it of the bytes a host downloads, which is
the claim that actually matters.

Neither check subsumes the other, and both failure modes are real. A constant
parked in a shared module with no reader is tree-shaken, so it reaches the
spec and not the bundle. And the standalone and embed graphs DO share chunks —
unlike the loader/embed pair this script already guards — so a decorator moved
into one of them would ship while a source-level resolver saw nothing wrong.
Rebuilding the pre-#217 state puts the header in `Spinner-*.js`, a chunk both
entries import.

The literal is duplicated into the script, as `src/loader/literals.ts` is and
for the same reason: a gate that imported the string from `src/` would be
reading the thing it is checking. The positive arm keeps that honest — if the
name stops appearing in the standalone graph, the negative arm has gone
vacuous and says so rather than passing.

Refs #217.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TecGGqBhAjFnCuoC3eevCH
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Deploying sahajatlas with  Cloudflare Pages  Cloudflare Pages

Latest commit: b7072f6
Status: ✅  Deploy successful!
Preview URL: https://0869ae39.sahajatlas.pages.dev
Branch Preview URL: https://claude-refactor-217-preview.sahajatlas.pages.dev

View logs

@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 21, 2026
@sydevs-bot

sydevs-bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running

recent dispatches
  • 18:53Z address-review · attempt 1 · session · done
  • 18:53Z address-review · attempt 1 · session
  • 18:33Z address-review · attempt 1 · session · done
  • 18:33Z address-review · attempt 1 · session
  • 18:24Z adversarial-review · attempt 1 · session · done

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧐 Adversarial review

The diff is the size of its ticket and stays inside it. A one-slot seam is the smallest shape that takes the header name out of the embed graph, and the three alternatives the body dismisses are dismissed correctly. The wire literal is unchanged, so SahajCloud's CORS allowlist and Cache Rule still match, and no request changes — skipping CHANGELOG.md is right.

The body names the real judgement: an impossible failure traded for a silent one. The one line that prevents the silent one is asserted by nothing.

Findings

  • should-fix — boot.ts:156, the production wiring, is unpinned.
  • consider — check-bundle-size.mjs:373: its stated non-overlap is not the one that holds.
  • consider — Widget.standalone.test.ts:230 overclaims.

What was checked

Ticket #217 against all 11 files. Read whole: client.ts, request.ts, boot.ts, protocol.ts, Widget.standalone.test.ts, main(), activateLivePreview. Grepped for the literal and the setter: one definition, one production caller, one activation path.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Comment thread src/config/live-preview/boot.ts Resolved
Comment thread scripts/check-bundle-size.mjs Resolved
Comment thread src/Widget.standalone.test.ts Resolved
@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 21, 2026
`boot.ts` registers the preview decorator on the line before it flips
`active`, and nothing read that line. Five activation cases exercised the
function without touching the slot, and both retargeted specs register the
decorator by hand in setup — so deleting the registration left the whole lane
green while a verified reviewer silently previewed published content.

Two cases, because the line makes two claims. The first drives a verified
activation and then runs `applyRequestContext`: delete the registration and it
goes red naming the missing header. The second re-opens the session by hand
after a REFUSED token and expects no header, which pins the registration below
the `if (!verified)` early return — the decorator's own gate would answer that
case whether the slot was filled or not, so the gate is stepped around
deliberately.

The slot is module state, so it is reset in `beforeEach`. Without that, one
case's registration answers the next and both go vacuous.

Ordering within the synchronous step stays unasserted: the two statements are
adjacent and nothing can interleave between them in-process. What the cases
hold is that the registration happens, and that it happens after the verify.

Refs #217.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_0127SJVrtgNiRBJWyyH8Y7Rt
The session-writer docblock said a widget-graph write "can no longer send
anything" because `request.ts` sits outside that graph. That overclaimed.
`client.ts` exports `setPreviewRequestDecorator` and IS in that graph, and the
header is a 30-character string anyone can spell — so one module there could
fill the slot itself. The slot was empty by absence, not by structure.

A closed caller list makes it structural: within the widget graph, only the
module DEFINING the setter may name it. The docblock now argues from that list
rather than from a reachability the setter does not have.

Not covered by the header scan beside it. Driven red with a widget-graph
module re-attaching the credential through a header name assembled by `join`:
the header scan stays green, and this is the only assertion that fires.

Refs #217.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_0127SJVrtgNiRBJWyyH8Y7Rt
The comment justified the embed-graph header scan against
`Widget.standalone.test.ts` with "the standalone and embed graphs do share
chunks, so a move of the decorator into one of them is invisible to the spec".
That is not a gap. Rolldown puts a module in a chunk `embed.js` loads only
where `Widget.tsx` reaches it, which is exactly the reachability the spec
walks — so stated that way the arm looked redundant.

The real non-overlap runs both ways. The spec's resolver follows only our own
`.ts`/`.tsx` files, a package specifier resolving to nothing there by design,
so a carrier arriving from a dependency reaches the bytes unseen. And
`importClosure` here is static, so a carrier behind a dynamic import is absent
from these bytes and caught only by the spec, whose walk follows `import()`.
The tree-shaking leg was already right and is unchanged.

Comment and guide only — no arm changed. The wrong argument is kept as a ⚠
line so it is not re-derived.

Refs #217.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_0127SJVrtgNiRBJWyyH8Y7Rt
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Three adversarial-review threads, all adopted — one commit each, pushed as de3e43f.

  • Registration unpinned → cc5d10b. Two cases in boot.test.ts: the registration happens, and it happens after the verify.
  • The size arm's stated gap → de3e43f. Wrong argument: a shared chunk follows the reachability the spec already walks. The real gaps are a carrier from a dependency, and one behind a dynamic import. AGENTS.md carried the same wording.
  • Overclaiming docblock → 8b171c5. Corrected as well as adopted: client.ts defines the setter, so toEqual([]) fails on the definition. Closed caller list instead.

Gate green: lint, typecheck, 1567 tests. pnpm build and pnpm size re-run — 295.1 / 2.6 / 296.9 KiB, unmoved. Each new assertion was driven red at its causing site.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 21, 2026
@sydevs-bot
sydevs-bot marked this pull request as ready for review September 21, 2026 18:46
@sydevs-bot
sydevs-bot requested a review from Ardnived September 21, 2026 18:46

@Ardnived Ardnived left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a step back and look for ways to radically simplify our approach to this feature in a way that eliminates the amount of code. Look for simpler ways to also smooth out the implementation and make it simple, robust, and easy to understand

@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed awaiting labels Sep 21, 2026
…client.ts

The seam #217 needs is "the embed graph carries neither the header name nor
the code that spends it". A dedicated slot in `client.ts` bought that with a
type, a module-level binding, an exported setter, and a second closed list in
`Widget.standalone.test.ts` to keep the setter from being called.

`livePreview` is already a mutable singleton in both graphs with a closed
WRITER list, asserted since before this branch. Moving the decorator onto it
as `decorateRequest` reuses both: one list now guards opening a session and
spending its credential, and every `Object.assign(livePreview,
LIVE_PREVIEW_INACTIVE)` — in production's refusal path and in four specs'
teardown — disarms the credential for free instead of needing a paired
`setPreviewRequestDecorator(null)`.

What that deletes: the `RequestDecorator` export and slot from `client.ts`
(its diff against main is now one call line), the setter, the setter's
caller-list arm, and six teardown lines. `client.ts` exports nothing new.
`request.ts` keeps the header and the decorator, and stays out of the embed
graph.

`readLivePreviewParams` returns the field null, so capture disarms and only a
verified activation arms — the same fail-closed gap capture and activation
already had.

Refs #217.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Sp12Sh1UK6sPPdYS3QyyK2
This arm was the one change on this branch beyond #217's acceptance criteria,
and it was flagged as such when it landed. Criterion 3 asks for a source-level
scan, which `src/Widget.standalone.test.ts` does: it walks the real import
graph, static and dynamic, and names the module at fault.

What this arm caught that the spec cannot is one case — a carrier arriving
from a dependency, invisible to a resolver that follows only our own
`.ts`/`.tsx` files. No dependency spells SahajCloud's private header, and the
shape that would make one do so is not a shape a size gate should be the first
to notice. 54 lines of script plus its own positive control, for that.

The AGENTS.md paragraph describing it goes with it, so the gate's description
keeps matching the script.

Refs #217.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Sp12Sh1UK6sPPdYS3QyyK2
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Simplified in two commits for your review.

  • ab09890 — the decorator hangs off livePreview instead of a slot in client.ts. The session was already a singleton with a closed writer list, so reusing it deletes the type export, the setter, the setter's own caller-list arm and six teardown lines. client.ts's diff against main is now one call line.
  • b7072f6 — the pnpm size arm goes. It was the one change beyond the acceptance criteria, and criterion 3 is the source scan.

Net +324/−69 → +253/−70: one fewer file, gate arm and closed list.

Declined: merging request.ts into boot.ts. It is a leaf, so three specs read the wire name off it without pulling client.ts and i18next in.

Both commits revert independently. ab09890 costs one thing: client.ts names a preview field again, where the slot was feature-neutral.

Gate green: lint, typecheck, 1566 tests, build. Size unmoved at 295.1 / 2.6 / 296.9 KiB.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot added awaiting and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 21, 2026
@sydevs-bot
sydevs-bot added this pull request to the merge queue Sep 21, 2026
Merged via the queue into main with commit 76382f8 Sep 21, 2026
17 checks passed
@sydevs-bot
sydevs-bot deleted the claude/refactor-217-preview-decorator-seam branch September 21, 2026 19:39
@sydevs-bot sydevs-bot moved this from Revising to Done in SYDevelopers Technical Roadmap Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

refactor(preview): keep the preview credential out of the embed graph

3 participants