| 1 | # Building and checking native Docs |
| 2 | |
| 3 | The native editor uses SwiftUI with AppKit/UIKit text controls and a bounded |
| 4 | WASM interpreter for the shared Yjs-compatible CRDT. It does not load a web editor. |
| 5 | |
| 6 | Requirements: Xcode 26, XcodeGen, Swift 6.2+, Node/npm, and Rust with the |
| 7 | `wasm32-unknown-unknown`, `aarch64-apple-darwin`, `x86_64-apple-darwin`, |
| 8 | `aarch64-apple-ios`, `aarch64-apple-ios-sim`, and `x86_64-apple-ios` targets. |
| 9 | The server gate additionally requires the Go version in `go.mod`. |
| 10 | |
| 11 | Run the native gate from the repository root: |
| 12 | |
| 13 | ```sh |
| 14 | native/scripts/check-native.sh |
| 15 | ``` |
| 16 | |
| 17 | It builds the CRDT WASM, runs Rust editor and bounded-host tests (including |
| 18 | opening and typing in a generated 200-page edited document on the host's |
| 19 | production fuel budget; see `fuel_budget` in `host/src/lib.rs`), checks native |
| 20 | and browser updates in both directions (including every list-rule reversal delta |
| 21 | and its authoritative snapshot, the undo-kept garbage-collection repro of |
| 22 | discussion #145, whose collected units must match Yjs's, and that each command's |
| 23 | update carries only its own change, as the browser's per-transaction updates |
| 24 | do), prepares the Apple XCFramework, and runs Swift adapter/actor tests. |
| 25 | `--core-only` runs the Rust and wire checks on |
| 26 | Linux. CI invokes this through `.amadan/ci.d/20-native`, alongside the existing |
| 27 | server/web gate. `--mutations` additionally proves representative faulty changes |
| 28 | are caught, restoring every changed source and saving logs under |
| 29 | `native/build/mutation-logs`. Do not run mutations concurrently with builds. |
| 30 | |
| 31 | `native/scripts/test-iphone.sh` runs the tests that need UIKit's text view |
| 32 | (DocsPhoneTests in `ios/project.yml`: the editor render-loop regression of |
| 33 | TestFlight 0.1.10 (12), and the redesign's S1a tests of `DocumentSurface`: |
| 34 | the flush triggers, delta safety, the block index, block render |
| 35 | equivalence, render isolation with §7's counters; their Mac half runs in |
| 36 | `swift test`) in the iOS 27.0 simulator. It is not in CI, which has no |
| 37 | simulator: run it before changing `NativeDocumentTextView`, |
| 38 | `DocumentSurface` or the render. |
| 39 | |
| 40 | §7's counters (`DocsKit/DocsProbes.swift`) compile under `DOCS_PROBES`: |
| 41 | Package.swift defines it for debug builds, so `swift test` and |
| 42 | test-iphone.sh hold the hard counters as gates (`RenderIsolationTests`); |
| 43 | a release build, the one that ships, carries none of it. The timings are |
| 44 | measured by `native/benchmarks/realtime` (its README), and the numbers are |
| 45 | in `native/benchmarks/README.md`. |
| 46 | |
| 47 | The apps bundle every font in Docs' shared list |
| 48 | (`internal/fonts/fonts.json`): `native/apple/Sources/DocsKit/Fonts` is a |
| 49 | copy of the manifest, each family's TTFs and its licence, made by |
| 50 | `native/scripts/sync-fonts.py`. Rerun it whenever `internal/fonts` changes; |
| 51 | the native gate runs it with `--check`, on Linux too, and fails on any |
| 52 | drift. Git stores the identical files once. The apps register every face |
| 53 | synchronously before their first window and draw each run from its bundled |
| 54 | file, never from a copy of the same name installed on the machine. |
| 55 | |
| 56 | `CARGO` can select a non-default Cargo executable; put its matching Rust toolchain |
| 57 | on `PATH`. If that toolchain needs an explicit LLVM runtime lookup path, set |
| 58 | `DOCS_LLVM_LIBRARY_PATH`. No generated native framework is committed. |
| 59 | |
| 60 | Build individual applications: |
| 61 | |
| 62 | ```sh |
| 63 | native/scripts/build-mac.sh |
| 64 | native/scripts/build-iphone.sh simulator |
| 65 | OFICINA_DEVELOPMENT_TEAM=YOUR_TEAM native/scripts/build-iphone.sh device |
| 66 | ``` |
| 67 | |
| 68 | The suite's `build-native-apps.py` prepares the native framework and Xcode projects |
| 69 | once, then runs Docs and Sheets builds concurrently with separate platform logs |
| 70 | and a combined `results.json`. Its default targets are Mac and iOS Simulator; |
| 71 | `--platforms mac simulator device` also builds signed device applications when |
| 72 | `OFICINA_DEVELOPMENT_TEAM` is set. It does not install or publish applications. |
| 73 | |
| 74 | Native UI tests use fresh local credentials and isolated databases. See |
| 75 | [REVIEW.md](REVIEW.md) for their runner, exact evidence, and untested behavior. |
| 76 | Performance measurements and their limits are in |
| 77 | [benchmarks/README.md](benchmarks/README.md). |
| 78 | |
| 79 | ## Oficina ID: entitlements, profiles and the launch check |
| 80 | |
| 81 | Docs signs in with Oficina ID first (Home's |
| 82 | `docs/superpowers/specs/2026-11-14-oficina-id-native.md`, slice A), through |
| 83 | rastrillo/native's `RastrilloApple/OficinaID` kit. "Other ways to sign in…" |
| 84 | keeps the server address and browser approval. The kit needs these grants on |
| 85 | every build: |
| 86 | |
| 87 | | Build | File | App group | |
| 88 | | --- | --- | --- | |
| 89 | | Mac, Sparkle (Developer ID) | `macos/DeveloperID.entitlements`, named by `mac-updates.json` | `9U4A84C4WK.works.oficina` | |
| 90 | | Mac App Store | `macos/AppStore.entitlements` | `9U4A84C4WK.works.oficina` | |
| 91 | | iPhone and iPad | `ios/DocsPhone.entitlements` | `group.works.oficina` | |
| 92 | |
| 93 | All three have `keychain-access-groups` = `9U4A84C4WK.works.oficina.docs` |
| 94 | **first**, then `9U4A84C4WK.works.oficina.id` (a query that names no group |
| 95 | lands in the first, and Docs' own items must never land in the group every |
| 96 | Oficina app reads), and `webcredentials:id.oficina.works`. |
| 97 | |
| 98 | A Developer ID Mac app that claims these without an embedded provisioning |
| 99 | profile is killed at launch. `mac-updates.json` names the profile by its local |
| 100 | path, `~/private_keys/oficina/profiles/Oficina-Docs-macOS-Developer-ID.provisionprofile`; |
| 101 | the profile itself is never committed. Elsewhere, pass |
| 102 | `--provisioning-profile <path>`, which wins over the configuration, to |
| 103 | `scripts/build-mac.sh` and to rastrillo's `release_mac.py`. A signed build: |
| 104 | |
| 105 | ```sh |
| 106 | native/scripts/build-mac.sh --updates-config mac-updates.json \ |
| 107 | --sign-identity 'Developer ID Application: Team Tito Limited (9U4A84C4WK)' |
| 108 | ``` |
| 109 | |
| 110 | An ad hoc build (no `--sign-identity`) drops those grants and says so on |
| 111 | stderr. It still runs, but Oficina ID cannot read the keychain there |
| 112 | (-34018), so Welcome says it couldn't be read. Simulator builds |
| 113 | (`CODE_SIGNING_ALLOWED=NO`) are the same. |
| 114 | |
| 115 | **The launch check** is the spec's launch gate (§13): before any Sparkle |
| 116 | publish, run the final signed, notarised artifact's executable with |
| 117 | `--oficina-id-launch-check`. |
| 118 | |
| 119 | ```sh |
| 120 | "native/build/Oficina Docs.app/Contents/MacOS/DocsMac" --oficina-id-launch-check |
| 121 | ``` |
| 122 | |
| 123 | macOS checks the embedded profile as the process starts. The check then reads |
| 124 | the signed entitlements (the keychain groups in order, the App Group, the |
| 125 | associated domain) and both keychain groups, prints counts (never an identity |
| 126 | or a session) and exits 0 only if all of them work. An ad hoc build, or the |
| 127 | unsigned test runner (`OficinaIDAdoptionTests`), fails every line. |
| 128 | |
| 129 | With one Oficina ID on the device Docs goes straight in, with no screen |
| 130 | (Paul, 2026-11-19; spec §9 "Straight in", rule 0); several ask which. While |
| 131 | "Other ways to sign in…" is the route, nothing goes straight in: the home |
| 132 | window suspends it at launch and on every route change (`routeChanged`, from |
| 133 | `.onChange(of: route)`), and resumes it when the route is Oficina ID again. A |
| 134 | signed-in session is never disturbed by either. The account menu (the Teams |
| 135 | sheet, and Docs ▸ in the Mac menu bar) offers "Use another Oficina ID" only |
| 136 | while signed in; the kit confirms (`oficinaReplaceConfirmation`, on the |
| 137 | signed-in screen only), and Docs refuses, and says why, while a document |
| 138 | window of the identity is open or its changes are unsent. Those are checked |
| 139 | before the kit asks, and a document window of the identity opened while it |
| 140 | asks withdraws the question. |
| 141 | |
| 142 | Docs' session guards answer a write whose session has ended with 403 |
| 143 | `signed out` (rastrillo's `sessions.Require`, and the identity guard for a |
| 144 | native session past its seven days). `DocsSessionWatch` reads exactly those |
| 145 | bytes as an ended session, re-mints it and surfaces a 401; every other 403 is |
| 146 | a refusal of the request. `internal/docs/native_contract_test.go` pins the |
| 147 | bytes on the server. |
| 148 | |
| 149 | An Oficina ID team's offline store key is filed under one fixed origin, not the |
| 150 | team's, so a team whose address changes (a label moved, a rename, self-hosting |
| 151 | by DNS) keeps its store and unsent outbox. Docs refreshes the team list at |
| 152 | launch, when the team picker opens (at most every five minutes) and on a 403, |
| 153 | through the kit's `refreshTeams`. Signing out, switching team or replacing the |
| 154 | Oficina ID waits for every team's outbox to drain, those a crash left on disk |
| 155 | included, and on the Mac for every document window of the identity to close; |
| 156 | the picker and Settings say which. |
| 157 | |
| 158 | Sign-in itself needs Home's native endpoints at `id.oficina.works` and the |
| 159 | directory at `oficina.works`; passkeys also need Home's association file. The |
| 160 | kit's in-memory `FakeOficinaIDService` stands in for all of them in |
| 161 | `OficinaIDAdoptionTests` and the screenshot gate. |
| 162 | |
| 163 | ## The screenshot gate |
| 164 | |
| 165 | `native/scripts/snapshots.sh [compare|record] [mac|iphone|all]` draws Docs' key |
| 166 | screens (library, Oficina ID onboarding, Other ways to sign in, the team |
| 167 | picker, Mac Settings) in light and dark, and on iPhone at AX3 too, and compares |
| 168 | them with `apple/Tests/DocsSnapshots/__Snapshots__/docs`. The contact sheet is |
| 169 | `apple/.build/snapshots/docs/index.html`. |
| 170 | |
| 171 | The same run draws the editor's page from fixed fixtures |
| 172 | (`apple/Tests/DocsSnapshots/EditorSnapshots.swift`, baselines in |
| 173 | `__Snapshots__/docs-editor` and, for iPhone at AX3, `docs-editor-ax3`; sheets |
| 174 | `apple/.build/snapshots/docs-editor*/index.html`). Each screen opens its |
| 175 | document through `DocsModel` and `NativeDocumentTextView` on an in-memory |
| 176 | server, and stays visibly unsettled until it is open, rendered, laid out and |
| 177 | scrolled, so the kit never records it half drawn. Rendering work (the native |
| 178 | redesign's S1) must leave these pictures unchanged; a screen that changes is |
| 179 | re-recorded only with a reason in the branch's discussion. |
| 180 | |
| 181 | It is not in `.amadan/ci.d`: |
| 182 | baselines are exact only on the OS and simulator runtime that drew them |
| 183 | (`ENVIRONMENT` beside them; today macOS 27.0.0, Xcode 27.2, and the iOS |
| 184 | 27.0 runtime 24A434 on an iPhone 18 Pro). Hub CI builds the snapshot tests |
| 185 | but skips them, since they need `RASTRILLO_SNAPSHOTS`. After an intended |
| 186 | change, `record`, read the contact sheet, and push the pictures one commit |
| 187 | per push with `git -c http.postBuffer=209715200 push`: without it the hub |
| 188 | drops a pack over about 1 MB ("unexpected disconnect while reading sideband |
| 189 | packet"), and pushes back to back trip its per-IP limit. Batch |
| 190 | re-recordings: every one stays in the hub's history. |
| 191 | |
| 192 | A screen that hangs the main thread never reaches the kit's own 3 s limit, |
| 193 | so the script stops the Mac test run after `SNAPSHOT_MAC_LIMIT` seconds |
| 194 | (default 300) and gives each iPhone test XCTest's 120 s allowance. |
| 195 | |
| 196 | ### Enforcing it |
| 197 | |
| 198 | Nothing but this procedure holds a branch to the gate, so: |
| 199 | |
| 200 | - **Every branch that can change rendering** (the redesign's S1a, S1b and |
| 201 | later, and any fix to the renderer) pastes the whole output of |
| 202 | `native/scripts/snapshots.sh compare all` into its discussion before it |
| 203 | asks for review. The script prints the environment each half drew on. |
| 204 | - **The reviewer re-runs it** on their own checkout (about a minute a |
| 205 | platform once built) and says so in the review. |
| 206 | - **When the environment no longer matches the baselines** (the script says |
| 207 | "ENVIRONMENT DIFFERS": a macOS, Xcode or simulator runtime update), the |
| 208 | failures are drift, not a regression, and can't be told apart from one. |
| 209 | Re-record from `main`, on a branch of its own whose only change is the |
| 210 | baselines, have the contact sheet reviewed, and land it first. Never |
| 211 | re-record on a rendering branch: that would bless whatever the branch |
| 212 | changed along with the drift. |
| 213 | - A rendering fix re-records only the screens it means to change, and its |
| 214 | discussion names them. |
| 215 | |
| 216 | ### Known rendering bugs in the baselines |
| 217 | |
| 218 | The editor baselines are today's pixels, bugs included, so that rendering |
| 219 | work can't fix (or break) them by accident. Each fix is its own branch and |
| 220 | re-records only the screens it changes: |
| 221 | |
| 222 | - Lists draw no bullet or number, on the Mac or iOS: `NSTextList` is set |
| 223 | and the items are indented, but no marker is drawn. |
| 224 | - Arabic paragraphs are left-aligned: `NativeTextProjection` sets `.left`, |
| 225 | not `.natural`. |
| 226 | - iPhone and iPad width, paged: the text sits about 72 pt above where the |
| 227 | page's top margin puts it, so it starts at the paper's top edge and stops |
| 228 | well above the page's foot. A paragraph's lines can split across pages |
| 229 | (iOS paginates by line). |
| 230 | - The page label differs: the Mac draws "Page 2" at the first break, iOS |
| 231 | "Page 1" at the foot of page 1. |
| 232 | - iOS draws no bar beside a blockquote; the Mac does. |
| 233 | - In Mac light appearance the desk around the paper is as white as the |
| 234 | paper, so only the dark pictures show the paper's edges and the bands |
| 235 | between pages. |
| 236 | |