Skip to content

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

Closed

Description

@sydevs-bot

Summary

The embedded <sahaj-atlas> element ships the code that spends SahajCloud's live-preview credential, though no host page can ever open a session. applyRequestContext (src/config/api/client.ts:48-51) sets the token header and draft=true, and it sits in both entry graphs. #215 closed the session's writer list to boot.ts (src/Widget.standalone.test.ts:198-208), so the widget graph cannot open a session. This closes the other half: the widget graph should not carry the code that would use one.

Approach

Move the preview branch — and the header name — out of the shared graph, behind a one-slot seam.

  1. client.ts holds the slot, not the behaviour. Add type RequestDecorator = (url: URL, headers: Headers) => void, a module-scope slot holding one or none, and setPreviewRequestDecorator. applyRequestContext (client.ts:33) calls it last, after auth and locale, and loses its livePreview and LIVE_PREVIEW_HEADER imports (client.ts:8).
  2. A new standalone-only module holds the decorator — src/config/live-preview/request.ts. It carries the body of client.ts:48-51 verbatim, gate included: livePreview.active && livePreview.token stays inside the decorator, so the seam fails closed on its own rather than trusting where it was registered.
  3. LIVE_PREVIEW_HEADER moves there too, out of protocol.ts:44. This is the part that makes the change real: protocol.ts is in both graphs on purpose, so a constant left there keeps the wire name in the embed bundle. Move the ⚠ block at protocol.ts:38-42 with it, and correct the module docblock at protocol.ts:6-7, which still promises "header names".
  4. boot.ts registers it, after verification — inside activateLivePreview, on the line that flips active (boot.ts:150). boot.ts reaches only main.tsx (Widget.standalone.test.ts:145-158), and main.tsx:119 holds the render until activation settles, so no request can precede registration.

protocol.ts stays in both graphs, deliberately. App.tsx:433 and RegistrationForm.tsx:153 read the session to select restraints, and a restraint is not the credential. What leaves the embed graph is LIVE_PREVIEW_HEADER and every writer of it.

Two specs assert the credential, not one. fetch.test.ts:83-119 covers applyRequestContext directly. LivePreviewController.transport.test.tsx:158-161 covers the populate request, which reaches the same header through interceptFetch (LivePreviewController.tsx:72). Both must register the real decorator in setup. A spec that does not is asserting an absence, and stays green against a seam that never fires.

Out of scope: the per-read draft split #211 left open, and the controller gate's position, settled on #215.

Acceptance criteria

  • With no decorator registered, applyRequestContext produces the same URL and header set it does today.
  • config/live-preview/request.ts joins STANDALONE_ONLY (Widget.standalone.test.ts:97), and the graph walk proves Widget.tsx cannot reach it.
  • No module in the widget entry's import graph contains the literal x-sahajcloud-preview-secret, scanning comment-stripped source via that spec's own stripComments.
  • boot.ts is the only importer of the new module, asserted as a closed list like Widget.standalone.test.ts:145-158.
  • A verified standalone session still sends the header and draft=true on every read. fetch.test.ts:83-119 holds, retargeted at the seam.
  • The populate request still carries both. LivePreviewController.transport.test.tsx:158-161 holds, retargeted the same way.
  • A scoped sy-atlas-translations session is unchanged — it still sends both, per fix(preview): a translations session should not inert the widget it is previewing #215.
  • docs/rules/data-layer.md:37-42 and docs/architecture.md:89-90 describe the seam, not a branch inside applyRequestContext.

Verification checklist

  • pnpm lint && pnpm typecheck && pnpm test:run
  • pnpm build && pnpm size — record standalone / loader / embed against the budgets at scripts/check-bundle-size.mjs:115-118. Expect no meaningful byte change. Watch the under-budget arm as well as the over one. This buys a structural guarantee, not size.
  • Each new assertion driven red first, at the site that causes it (docs/testing.md). For the literal scan, that site is protocol.ts before the constant moves.
  • grep -rn "x-sahajcloud-preview-secret" src/ names only the new module and the specs.
  • /admin → Sahaj Atlas → Translations: an unpublished string still renders, proving the credential still rides the standalone read.
  • /admin → an Event → live preview: a title edit still lands on screen, proving the populate request still carries the header.

Open questions

  • Should an active session with no decorator registered say so? An unregistered decorator is a silent no-op — the reviewer previews published content and sees no error. A dev-only warning in applyRequestContext would catch it. A throw would not, since the widget must never break a host page.

Notes

Follow-up from #215, requested at #215 (comment).

A list of decorators was the first shape, and one named slot replaces it. Only one decorator will ever exist, a list makes iteration order an implicit contract, and applyRequestContext is meant to stay the one place cross-cutting context attaches (docs/rules/data-layer.md:37-42). A named setter also keeps "who can spend this credential" a one-hit grep.

The change trades an impossible failure for a silent one. Today the branch cannot fire in a host page, because nothing there writes the session. After it, 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 /admin checks stand in for the compiler here. The open question above is the same point.

Effort reads as Moderate: the code change is small, but it moves a seam two entry graphs share, needs a new graph assertion driven red, and retargets two specs and two docs.

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

Activity

  1. added theissue type on Sep 18, 2026
  2. added
    proposal
    bot:working
    and removed on Sep 18, 2026
  3. sydevs-bot commented on Sep 18, 2026

    @sydevs-bot
    ContributorAuthor

    🤖 no session running

    recent dispatches
    • 18:00Z implement · attempt 1 · session · done
    • 18:00Z implement · attempt 1 · session
    • 23:12Z revise · attempt 1 · session · done
    • 23:12Z revise · attempt 1 · session

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

  4. sydevs-bot commented on Sep 18, 2026

    @sydevs-bot
    ContributorAuthor

    Revised against the code. The shape holds. Four corrections.

    LIVE_PREVIEW_HEADER has to leave protocol.ts:44. The body did not name it as moving, and protocol.ts is in both graphs on purpose — so the wire name would stay in the embed bundle and the graph criterion could never pass.

    A second spec asserts the credential. LivePreviewController.transport.test.tsx:158-161 reaches the same header through interceptFetch. Only fetch.test.ts was named. Both must register the real decorator, or they assert an absence and stay green.

    One named slot, not a list of decorators. Only one will ever exist, and applyRequestContext is meant to stay one place. Rejected alternative is in Notes.

    Two docs describe today's branch — docs/rules/data-layer.md:37-42 and docs/architecture.md:89-90.

    Added one open question: an unregistered decorator is a silent no-op, and nothing catches it.

    Effort stays Moderate. Priority is the reviewer's. @sydevs-bot implement starts the work.

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


    Generated by Claude Code

  5. added and removed
    bot:working
    on Sep 18, 2026
  6. moved this from Proposed to Revising in SYDevelopers Technical Roadmapon Sep 18, 2026
  7. Ardnived commented on Sep 21, 2026

    @Ardnived
    Contributor

    @sydevs-bot implement

  8. added
    bot:working
    and removed
    proposal
    on Sep 21, 2026
  9. moved this from Revising to Approved in SYDevelopers Technical Roadmapon Sep 21, 2026
  10. removed
    bot:working
    on Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    Priority

    None yet

    Hold Until

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions