| 1 | # Native chat Markdown |
| 2 | |
| 3 | Branch: `native/markdown`. This includes the 2026-11-23 implementation and the |
| 4 | review follow-ups to `506fafca` and `5a1fe0d1`. The web's Render and RenderDraft remain the |
| 5 | contract, including absolute HTTP/HTTPS and mailto links. |
| 6 | |
| 7 | ## What a person sees |
| 8 | |
| 9 | On iPhone and Mac, Paul's complete reply has three paragraphs, a bold |
| 10 | “Why they look different:” lead-in, and a blue task title which opens the |
| 11 | native task screen. Lists, emphasis, code spans and hard breaks follow the |
| 12 | web. Headings have its paragraph treatment and rules have no output. HTML is |
| 13 | text. Unsupported links keep their words. Relative project links open at the |
| 14 | account's origin because there is no native project detail screen. Absolute |
| 15 | web links use the browser; mailto uses the system mail handler. |
| 16 | |
| 17 | Draft markers wait, partial links show their words, and unmatched delimiters |
| 18 | follow RenderDraft. An open fence has its final paste-box layout with Copy |
| 19 | disabled. Closing it enables Copy without changing its anchor or size. Prompt |
| 20 | and Text wrap; other code scrolls horizontally in a system monospaced font. |
| 21 | Copy removes exactly one final newline. Paste boxes have VoiceOver labels. |
| 22 | |
| 23 | Replies up to 16 KiB, including small drafts, parse synchronously before the |
| 24 | first layout. A bounded cache holds trees by account, message ID and revision |
| 25 | (the exact source plus draft/completed state), so scrolling does not reparse. |
| 26 | Larger inputs parse on a concurrent executor while the previous revision or a |
| 27 | multiline plain-text fallback stays visible. A cancelled task cannot publish |
| 28 | its old result. Mac height changes resize only that reply and shift following |
| 29 | rows, preserving host identity and scroll position. Mac reply text remains 14 points, |
| 30 | matching the existing `MacType.line`; iPhone text uses Dynamic Type's body font. |
| 31 | Deep lists use flat layout rows with indentation capped at four levels. |
| 32 | |
| 33 | ## Parser choice and parity |
| 34 | |
| 35 | Equipo still uses swift-cmark's core CommonMark parser, without GFM extensions |
| 36 | or smart punctuation, pinned by revision to |
| 37 | `08ddb528923cc1a6527e02b7a1aee9e516ca749a` in both package files. The core exposes |
| 38 | list tightness, block buffers and parser transitions needed here. The higher |
| 39 | level swift-markdown wrapper does not expose all of these. A separate hand |
| 40 | parser would duplicate the block and reference grammar. |
| 41 | |
| 42 | The projection walks iteratively. Inline coordinates are mapped through the |
| 43 | retained original block buffer and cached line offsets, so continuation |
| 44 | indentation does not select another span or URL. Unicode classification is |
| 45 | adapted only in the parser's private buffer; displayed text and destinations |
| 46 | come from the retained original. Text and link entities have separate decoders |
| 47 | because the web uses different rules for them. Reference definitions are |
| 48 | compiled and scanned once per message. Explicit fence transitions, including |
| 49 | longer-than-255-character fences, determine Copy availability. |
| 50 | |
| 51 | `internal/chatmarkdown/native_test.go` projects the actual Render/RenderDraft |
| 52 | HTML into canonical JSON, only in tests. There are **3,510 checked-in fixtures**, |
| 53 | plus **32,000 distinct fixed-seed generated inputs** written at check time. The native |
| 54 | gate gives Swift that same generated file. Every block, inline, target, label |
| 55 | and Copy state is compared. The corpus includes Paul's full reply, streaming |
| 56 | cuts, whitespace and lazy continuations, nested formatting, raw HTML, entities, |
| 57 | Unicode symbols, CRLF and CR-only input, 300-character fences and code spans, |
| 58 | rejected URL schemes, angle destinations with edge whitespace, EOF labels, |
| 59 | soft-break whitespace, HTML after paragraphs, reference definitions, and every |
| 60 | character cut through escaped and open spans. |
| 61 | |
| 62 | The one production web change fixes destination entity decoding. A complete, |
| 63 | known entity decodes once; `×tamp`, `®ion`, and unknown names remain |
| 64 | literal. This preserves query parameters instead of corrupting them. The |
| 65 | `golang.org/x/net` test import is now a direct module dependency. |
| 66 | |
| 67 | The complete swift-cmark copyright and licence notices remain bundled and are |
| 68 | now reachable from the Account screen on both platforms, under swift-cmark. |
| 69 | |
| 70 | ## First review findings and regression evidence |
| 71 | |
| 72 | | Finding | Change and regression | |
| 73 | | --- | --- | |
| 74 | | P1.1, stack overflow | Iterative projection; 200, 1,000 and 5,000 quotes run on a 1 MB stack. | |
| 75 | | P1.2, draft regex | Linear marker scanner; 800, 1,600 and 3,200 spaces stay below one second. Large inputs parse off the main actor. | |
| 76 | | P1.3, continuation ranges | Retained block buffers and cached coordinate mapping; code spans and good/evil URL continuation fixtures. | |
| 77 | | P2.4, open-fence Copy | Parser transitions distinguish an explicit closer from EOF or leaving a list/quote. Indented false closers remain content. | |
| 78 | | P2.5, schemes | Fixtures and action tests reject file, ftp, tel, sms, blob, vbscript, custom schemes and protocol-relative URLs. | |
| 79 | | P2.6, URL rules | ASCII destination trimming and Go-compatible host/userinfo/escape validation. Odd web-allowed hosts remain actionable through validated original targets. | |
| 80 | | P2.7, web entities | Complete entities only, decoded once; query-name, unknown-name and nested-entity Go tests. | |
| 81 | | P2.8, list layout | Flat layout with bounded indentation; a 60-level list renders at 390 points within three seconds. | |
| 82 | | P3, Unicode emphasis | Symbol and whitespace classification matches Goldmark, including emoji, currency, NEL and line separators. | |
| 83 | | P3, small tree differences | HTML EOF newlines, EOF fence labels, escaped entities, CRLF, code spans and angle destinations are in the goldens. | |
| 84 | | P3, reference cost | One reference scan and one coordinate map per block; 2,000 references parse within two seconds. | |
| 85 | | P3, dependency pin | Both package files use the immutable cmark revision. | |
| 86 | | P3, licence | A source guard keeps the bundled notice reachable from Account. | |
| 87 | | P3, module tidy | `x/net` is direct; `check-fixtures.py` runs `go mod tidy -diff`, including in the native gate. | |
| 88 | | P3, accessibility/copy | Paste labels are reused for VoiceOver; the unused coordinator spoken-format key is removed. | |
| 89 | | P3, Mac typography | A regression fixes the reply font at the previous 14-point size. | |
| 90 | |
| 91 | Tests were added before each implementation. The original small-stack test |
| 92 | crashed. The original marker, reference and layout tests exceeded their bounds |
| 93 | and were terminated by the harness. The generated corpus exposed additional |
| 94 | entity and fence-label differences, which were retained and fixed. |
| 95 | |
| 96 | Focused commands: |
| 97 | |
| 98 | ```sh |
| 99 | EQUIPO_MARKDOWN_DIFFERENTIAL="$PWD/native/build/markdown-differential.json" \ |
| 100 | swift test --package-path native/apple --filter 'ChatMarkdownTests|MarkdownLayoutTests' |
| 101 | GOTOOLCHAIN=go1.26.11 CGO_ENABLED=0 go test ./internal/chatmarkdown -count=1 |
| 102 | native/scripts/check-native.sh |
| 103 | ``` |
| 104 | |
| 105 | All **28 deliberate mutations were caught**, then restored. This includes |
| 106 | restoring recursive walking/layout and the backtracking regex, moving parsing |
| 107 | onto the main actor, breaking source mapping, enabling open-fence Copy, adding |
| 108 | ftp, loosening URL/userinfo/zone checks, restoring legacy web entities, breaking |
| 109 | Unicode/CRLF/HTML/EOF/escape handling, restoring per-link reference scanning, |
| 110 | removing the revision pin/licence/accessibility wiring, restoring the unused |
| 111 | spoken key, changing Mac text to 13 points, truncating long fence lengths, |
| 112 | breaking odd-host actions, changing entity-pass order, and trusting a source |
| 113 | candidate without checking the parser-selected target. |
| 114 | |
| 115 | The first review follow-up passed with **zero mismatches across 26,724 trees**. The |
| 116 | 2,000-reference case takes about 0.14 seconds; the 60-level phone-width list |
| 117 | renders in under 0.5 seconds. The marker scan takes about a millisecond and |
| 118 | 5,000 quotes take under 0.05 seconds on the 1 MB stack. Timings are local Mac |
| 119 | measurements; the tests enforce wider explicit bounds. |
| 120 | |
| 121 | That revision’s full native gate passed: **152 Swift tests** (one existing marked Oficina |
| 122 | ID issue), 2,724 checked-in trees plus 24,000 distinct generated inputs with |
| 123 | zero mismatches, 173 strings, 28 web/native copy pairs, reference inventory, |
| 124 | Mac build and unsigned iOS Simulator build. |
| 125 | |
| 126 | Go gates passed with Go 1.26.11 and CGO disabled: all chatmarkdown tests, |
| 127 | `go vet ./internal/chatmarkdown`, staticcheck v0.7.0 for that package, |
| 128 | `make fmt-check` with the pinned GOROOT on PATH, and `go mod tidy -diff`. |
| 129 | The post-commit reference scan and branch CI verdict are recorded in the |
| 130 | controller handoff. |
| 131 | Local evidence is under `native/build/logs/review-*` and `mutation-*`. |
| 132 | |
| 133 | ## Re-review at 5a1fe0d1 |
| 134 | |
| 135 | | Finding | Fix and regression | |
| 136 | | --- | --- | |
| 137 | | P2.1, byte leak and restoration cost | Each physical LF line, including an empty one, maps to its original line. Restoration never searches later blocks. A 3,000-block test checks every copied body and a two-second bound (about 0.2 seconds locally). Every golden and generated tree also checks that introduced control bytes do not leak; legitimate entity-decoded controls must equal the web. | |
| 138 | | P2.2, blank first frame | Small replies parse before layout. A live host asserts nonzero height immediately. Cache tests verify exact revision reuse, draft/completion identity, previous-tree retention and a nonempty multiline fallback for large replies. The Mac callback resizes the affected row instead of rebuilding the transcript. | |
| 139 | | P3.1, angle whitespace | Retain the original angle destination even when cmark trims it. Apply the web URL policy to that original, including its fragment rules. | |
| 140 | | P3.2, EOF labels | The web drops a single byte immediately after the EOF fence, before whitespace trimming; surrounding whitespace and longer labels survive. A 120-case whitespace/label cross-product pins this distinction. | |
| 141 | | P3.3, soft breaks | Trim trailing spaces and tabs before soft breaks as well as hard breaks. | |
| 142 | | P3.4, HTML separator | The test-only Go projection removes the formatting newline after a block element. Render/RenderDraft HTML is unchanged. | |
| 143 | | P3.5, scheme property | The allowed set is exactly http, https and mailto. Property checks apply case variants and generated valid scheme names to opaque, hierarchical and mail-style destinations. Adding webcal fails. | |
| 144 | | P3.6, draft spans | A parsed delimiter ends the watched fallback backtick span. A backslash escapes one delimiter byte, not its whole run. Cross-product streaming cuts cover quotes, list prefixes, escapes, backticks, emphasis and links. | |
| 145 | | P3.7, tidy gate | The fixture gate now executes `go mod tidy -diff` with Go 1.26.11 and CGO disabled. Marking the direct x/net import indirect makes that gate fail. | |
| 146 | |
| 147 | The deterministic differential corpus now has **32,000 unique sources**, plus |
| 148 | **3,510 checked-in cases**, with **zero mismatches across all 35,510 trees**. |
| 149 | CR-only generation also exposed a parser line-count crash, now prevented by |
| 150 | width-preserving CR adaptation in both block and inline buffers. Long inline |
| 151 | backtick runs are adapted to cmark’s 80-byte limit while preserving original |
| 152 | code bytes and coordinates. These cases stay in the corpus. |
| 153 | |
| 154 | Tests were added before the fixes. The first-frame test failed with zero |
| 155 | height; the original byte-restoration test failed both content and time checks; |
| 156 | the expanded parity run initially failed and crashed on CR-only input. Extra |
| 157 | escaped-span cuts then caught five more draft mismatches before their fix. |
| 158 | Commands and outcomes are under `native/build/logs/review2-*`. |
| 159 | |
| 160 | All **19 new mutations were caught and restored**. They broke blank-line |
| 161 | mapping, first-frame content, revision caching, large-input background parsing, |
| 162 | large-input fallback content and line layout, completion revision identity, |
| 163 | Mac row resizing, angle whitespace, EOF labels (both length rules), soft-break |
| 164 | trimming, HTML separators, the scheme allowlist, backtick fallback boundaries, |
| 165 | escaped delimiter runs, CR-only adaptation, long code spans and module tidiness. |
| 166 | The harness log records each command, failure and restoration in |
| 167 | `native/build/logs/review2-mutations.json`. |
| 168 | |
| 169 | Final gates passed: **157 Swift tests in 18 suites** (one existing marked |
| 170 | Oficina ID issue), **35,510 comparisons with zero mismatches**, 173 strings, |
| 171 | 28 web/native copy pairs, the reference inventory, the Mac build and unsigned |
| 172 | iOS Simulator build. Go tests, vet and staticcheck for chatmarkdown, repository |
| 173 | fmt-check and module tidy all passed with Go 1.26.11 and CGO disabled. |
| 174 | The post-commit reference scan and branch CI verdict are in the handoff. |
| 175 | |
| 176 | ## Visual and platform checks |
| 177 | |
| 178 | Layout tests render the screenshot reply, a wrapping prompt, nested lists and |
| 179 | long scrollable commands at 390 and 760 points. Open and closed code boxes |
| 180 | have the same measured size and source anchor. A live NSHostingView test |
| 181 | verifies the first layout already contains content and subsequent revisions report their new height. |
| 182 | The new captures were inspected: the full task title wraps cleanly, the bold |
| 183 | lead-in has no delimiter marks, the prompt wraps inside its box, and the long |
| 184 | command clips horizontally without widening the transcript. Captures are in |
| 185 | `native/build/markdown-screenshots-review2/`. |
| 186 | These are Mac renders at phone and desktop widths, not physical iPhone |
| 187 | screenshots. The native gate builds the unsigned iOS Simulator app. |
| 188 | |
| 189 | The existing marked Oficina ID test issue remains outside this change: a |
| 190 | permission refusal during a Home outage can discard the kit session. No |
| 191 | signed build, production account, merge, deployment or upload is involved. |
| 192 | |
| 193 | ## Strings for Luna |
| 194 | |
| 195 | No new product strings in this follow-up. The five existing native keys still |
| 196 | reuse web copy exactly: `chat.paste.copy` (Copy), `chat.paste.copied` (Copied), |
| 197 | `chat.paste.prompt` (Prompt), `chat.paste.text` (Text), and |
| 198 | `chat.paste.command` (Command). These also supply paste-box accessibility labels. |
| 199 | |
| 200 | Removed the unused native key `message.coordinator_spoken %@ %@` from the |
| 201 | source strings and both catalogs. The dependency's proper name and verbatim |
| 202 | copyright/licence text are displayed in its disclosure. |
| 203 | |