# Markdown streaming follow-up

Branch `native/markdown-streaming`, based on main at `3589c220`. This report
includes the follow-up to Fable's review of `b533757b`.

## Message lifetime and large replies

A session owns a `MarkdownTranscript`, keyed by message identity. Each message
has one background parsing worker. Appended deltas replace the pending request
without cancelling the in-flight parse. Every finished prefix publishes before
the worker parses the latest pending revision. Intermediate revisions do not
queue. Small replies still render synchronously; cached revisions include the
account origin, message ID, exact text and draft/completed state.

SwiftUI hosts borrow the message's renderer. Disappearance, scrolling and
re-hosting do not stop it. Removing a message from the feed or a snapshot stops
its worker, as does leaving the session. Cancellation and a generation check
reject late results. A completed message settles its final parse, then the
worker exits. Replacing a message's source starts fresh.

The original 64 KiB test streams 67 updates at 5 ms intervals. The old
cancel-on-delta implementation showed only one distinct height before the
stream ended. The regression requires at least six. A delayed-parser test checks
that prefixes publish, only one parse runs, appends cause no cancellations and
the final completed tree is exact. A deterministic held-parse test sends 67
pending revisions plus completion and requires exactly two parses: the first
prefix and the latest completed revision.

## Mac transcript rows

`LiveTranscript` retains each row and reply host. A row revision includes its
feed item, width, task details, answer/aside feedback and stopping state.
Unchanged revisions receive no content update or intrinsic-size measurement.
Changed replies update their existing host in place; new and removed IDs alone
attach or detach rows. Ordering changes sort existing views without re-hosting.
Height callbacks resize the changed reply and move later rows without rebuilding
their contents. A width change resizes the existing rows.

Fable's reproduction is covered two ways: a SwiftUI host is repeatedly detached
and reattached across 67 large deltas, and the actual `EquipoMac.LiveTranscript`
runs 49 historical replies plus one 17–25 KB streaming reply. The first test
failed at one height before the fix. The actual transcript now grows throughout
the stream; a focused run saw 10 growing heights (64 with other layout tests
running concurrently). Completion keeps the same row, and removal cancels its
message parser.

The performance test uses the production Mac transcript inside an `NSWindow`,
with 50 realistic replies. Before the fix, its median update was 723 ms. After
the fix, a debug run measured 3.44 ms median and 5.50 ms maximum. The 50 ms median
budget allows debug/CI variation while rejecting the original full rebuild by
more than an order of magnitude. Timing is paired with deterministic assertions:
exactly one content revision per changed reply, the same row and host identities,
and no window detach/attach notifications. Removal, reinsertion, ordering and
width changes are checked too.

The Release test (`swift test --package-path native/apple -c release --filter
MacTranscriptTests`) passed: 2.71 ms median, 4.83 ms maximum for the 50-reply
update; the large reply showed 29 growing heights during 67 deltas. This
uses the real transcript class and window layout, with in-memory test accounts;
it is not a signed-in production-app timing session. No personal Keychain or
signing settings were accessed for the harness.

## Open code spans

The first follow-up matched Goldmark's merging of unmatched emphasis delimiters
into watched fallback backtick text, including adjacent closers. This revision
also ends that watched text at a link/image opener. Bracket labels remain
visible while the code span and destination are still open.

Every streaming cut of the supplied underscore/asterisk examples and
`[text](url)` / `![alt](src)` inside backticks is checked. The fixed-seed grammar
adds links and images inside open code spans, with quote/list prefixes, optional
closers, destinations and random cuts. The expansion found 322 mismatches before
the bracket fix. There are now 3,586 checked-in cases and 32,000 unique generated
inputs: 35,586 comparisons with zero mismatches. Go Render/RenderDraft output
is unchanged.

## Verification

Tests preceded the fixes. Focused checks use:

```sh
EQUIPO_MARKDOWN_DIFFERENTIAL="$PWD/native/build/markdown-differential.json" \
  swift test --package-path native/apple \
  --filter 'MacTranscriptTests|MarkdownParsingStreamTests|MarkdownLayoutTests|ChatMarkdownTests'
native/scripts/check-native.sh
```

The earlier commit's nine guards were broken and restored in eleven mutation
runs. All ten re-review guards were also broken and caught: detaching existing
rows, ignoring row revisions, replacing reply hosts, stopping on disappearance,
losing owner identity, omitting cancellation for removed messages, omitting
session-departure cancellation, hiding open-code link labels, omitting snapshot
removal cleanup and omitting feed removal cleanup. Each focused regression
failed, then the guard was restored; none failed to compile or timed out.

The revision-guard mutation initially stayed within the broad performance
budget; adding the exact content-update count made that unnecessary work
observable independently of machine speed.

`native/scripts/check-native.sh` passed: fixture freshness and the generated
corpus, strings/parity, reference scan, 167 Swift tests in 20 suites, Mac build
and unsigned iOS Simulator build. The suite retains the existing known Oficina
ID session issue; no new known issue was introduced. The full-suite Mac stream
also grew through 64 sampled heights during 67 deltas.

Go tests, vet, staticcheck v0.7.0 for `internal/chatmarkdown`, `make fmt-check` and
`go mod tidy -diff` passed with `GOTOOLCHAIN=go1.26.11 CGO_ENABLED=0`, with that
toolchain on PATH. Local evidence is under `native/build/logs/streaming-review-*`.

## Remaining scope

Fable's random-token finished-reply cases involving lone CR, reference
definitions and nested `<` in angle destinations remain deferred. The current
corpus matches, which does not claim those independent counterexamples match.
No product strings or dependency pins changed. Projects awaits its brief.
