Repository navigation
Description
id(web/collab): INIT frame mode shape-trusted — a non-string or unknown mode is adopted verbatim, and a falsy mode silently opens a rich doc in the raw schema as an empty editor
Summary
handleMessage in pkgs/id/web/src/collab.ts validates the INIT frame's
version and doc (#2824: version must be a safe non-negative integer, doc must
be a doc object) — but the frame's fourth field, the content mode, is still
read with an unchecked cast (collab.ts:316 on origin/main df780d3c3b):
const mode = ((msg[3] as string) || "raw") as ContentMode;
documentMode = mode;
...
editorInstance = initEditor(container, docRecord, version, mode, sendCursor, filename);Any value is adopted as the editor's ContentMode ("rich" | "markdown" | "plain" | "raw" | "media" | "binary", editor.ts:41) and drives schema
selection (getSchema: only "raw" gets the raw schema, editor.ts:476-478),
toolbar/editable decisions, and the mode stored on the editor instance. Two
failure classes result:
- Truthy wrong shapes are adopted verbatim. A numeric, object, or
unknown-string mode becomes the session's mode:initEditoris called with
it,instance.modereturns it, and every mode comparison downstream
silently takes the "not raw / no toolbar" branch for a document that may
be raw text. - Falsy wrong shapes coerce to
"raw". A mode of0/false(deploy
skew, a future server contract change) hits the|| "raw"fallback, so a
rich/markdown document is parsed with the raw schema (doc =code_block+
only).Node.fromJSONthrows (RangeError: Unknown node type: paragraph),initEditorcatches it and falls back to an empty doc —
the editor opens showing nothing for a non-empty file, one
save/autosave away from overwriting the real content with the empty state.
This is the INIT sibling of #7854 (AUTH_OK / NEW_VERSION frame fields):
that filing covers the token and hash fields; the INIT mode field was not
covered by #2824 (version/doc only) or by #7854.
Verified behavior (this run)
Probe: ~/git/fleet/probe-initmode (laptop) — a detached worktree at
origin/main df780d3c3b (collab.ts md5 d4ecdd5034a0219d3a88424940a73b05,
identical to git show origin/main:pkgs/id/web/src/collab.ts) plus
src/probe-initmode.test.ts, which drives the real initCollab
through the same FakeWebSocket harness as collab.test.ts, frames packed
with the repo's MessagePack config. All 6 probe tests pass; the editor
instance is captured via onEditorReady:
| INIT frame | Observed |
|---|---|
[0, 0, rawDoc(code_block "hello raw"), "raw"] (control) |
instance.mode === "raw", doc text "hello raw" ✅ |
[0, 0, richDoc(paragraph "hello rich"), "markdown"] (control) |
instance.mode === "markdown", doc text "hello rich" ✅ |
[0, 0, rawDoc, 123] |
instance.mode === 123 (number, adopted verbatim); schema selection takes the non-raw branch |
[0, 0, richDoc, {type:"doc"}] |
instance.mode === {"type":"doc"} (object, adopted verbatim) |
[0, 0, rawDoc, "bogus"] |
instance.mode === "bogus"; hasToolbar false, non-raw schema for a raw doc |
[0, 0, richDoc, 0] |
mode coerced to "raw"; Node.fromJSON throws RangeError: Unknown node type: paragraph; editor initialized with an empty doc (doc(code_block), text "") — the sent content silently vanishes |
Why it matters
- The empty-editor case is a data-loss footgun: the user sees an empty
document for a file that has content, and any edit + save from that
state posts the empty/wrong-schema doc back. - Wrong-schema initialization (bogus/number/object modes) silently changes
how the document is parsed, edited, and serialized for the whole session,
with no error anywhere — the same silent-corruption shape as id(web/collab): AUTH_OK and NEW_VERSION frame fields shape-trusted — a non-string refreshed token is persisted as the session identity, a non-string hash becomes '[object Object]' #7854. - The fix pattern is already established in this function: id(web): collab handleMessage trusts MessagePack frame shape — a null frame throws out of onmessage, and a field-less INIT silently initializes an empty editor at an undefined version #2824 drops INIT
frames with an invalid version or doc; the mode field simply never got
the same treatment.
Acceptance criteria
- The INIT mode is validated at the frame boundary in
collab.ts: it must be one of theContentModeunion values ("rich","markdown","plain","raw","media","binary"). A missing mode may keep the"raw"default only when the field is absent/undefined. - An INIT frame with a present-but-invalid mode (non-string, or a string outside the union) is dropped with a diagnosable
console.error, like the invalid version/doc cases — the editor is never initialized with an adopted-verbatim or coerced mode. - A rich/markdown document can never be silently opened as an empty raw-schema editor because of a wrong-typed mode field.
- Regression tests cover mode
123, mode{...}, mode"bogus", and mode0/falsewith a rich doc, plus the"raw"and"markdown"controls (a test incollab.test.tsstyle, drivinginitCollabthrough the fake-socket harness). - pkgs/id web vitest suite + typecheck green.
Scope notes
- Fix belongs at the INIT boundary in
pkgs/id/web/src/collab.ts(a smallisContentModeguard next to the existing version/doc validation is fine); server contract unchanged. - Out of scope: AUTH_OK / NEW_VERSION fields (id(web/collab): AUTH_OK and NEW_VERSION frame fields shape-trusted — a non-string refreshed token is persisted as the session identity, a non-string hash becomes '[object Object]' #7854), the version/doc INIT validation (id(web): collab handleMessage trusts MessagePack frame shape — a null frame throws out of onmessage, and a field-less INIT silently initializes an empty editor at an undefined version #2824, done), UPDATE/CURSOR field tolerance (noted in id(web): collab handleMessage trusts MessagePack frame shape — a null frame throws out of onmessage, and a field-less INIT silently initializes an empty editor at an undefined version #2824's probe as tolerated — cursor positions are clamped in
cursors.ts).
Activity
developing-today commentedon Nov 8, 2026
CODE-2335
dezren39 commentedon Nov 8, 2026
🤖 Agent note · filed
dezren39 commentedon Nov 10, 2026
🤖 Agent note · parked — awaiting triage
status/needs-triage. Per Queue/lane design rules: no by-id blocking by default, optional platform selector, triage separate from fixes #8097 Rule 3, fix work starts only on triaged items and triage belongs to the dedicated triage step — parking task 14128 back for 25 minutes with stage awaiting_triage.6 remaining items
dezren39 commentedon Nov 11, 2026
agent-note:v1
state: pr_opened (parked CI wait)
task: 14128
lane: tmp-code-f
Fix implemented in this claim: PR #8534 adds the isContentMode guard at the INIT frame boundary in pkgs/id/web/src/collab.ts (absent mode keeps the raw default; present-but-invalid drops the frame with a diagnosable console.error) plus 3 regression tests in collab.test.ts. collab suite 27/27; oracle verified (malformed-mode test fails pre-fix); full web suite 727 pass with 1 pre-existing search-panel failure that reproduces on clean main; tsc clean. Parked as broker CI wait on PR #8534. Resume: checks green + Codex disposition -> merge -> verify #7859 closes -> complete fixed_merged.
dezren39 commentedon Nov 11, 2026
agent-note:v1
state: parked
worker: repo-worker-code
lane: any-b
task: 14128
note: Resumed and re-verified PR #8534 (head 7f3f271): Codex review completed with no findings, no inline review comments. Blocked only on CI: 11 basic checks QUEUED in the fleet-wide backlog, 0 failed. Parked as CI wait. Resume: checks green -> merge -> verify issue closes -> complete fixed_merged.
Merge pull request #8534 from developing-today/queue/14128-7859-init-…
dezren39 commentedon Nov 11, 2026
🤖 Agent note · done — PR merged
bunx vitest run src/collab.test.ts— 27/27 passed;bun run typecheck(tsc --noEmit) — clean. (Caution recorded: running this suite underbun testinstead of vitest fails 13 tests on missingvi.*globals — a runner artifact, not a code failure; the package's own test script isvitest run.)