# mapa

The Oficina suite's team knowledge base. The design is
`docs/superpowers/specs/2026-11-12-mapa-design.md`, approved by Paul on
2026-11-12; read it before changing behaviour here. Its §15 records every
choice the build made where the spec was silent or ambiguous. A change that
departs from the spec needs the same review it had.

A [rastrillo](https://rastrillo.org) app on the current
`amadan.net/rastrillo/rastrillo` path (never the frozen
`github.com/carlosframework/rastrillo` one; `TestNoFrozenRastrilloImport`
forbids it), styled with estilo. Read rastrillo's SKILL.md
(`go list -m -f '{{.Dir}}' amadan.net/rastrillo/rastrillo`) before writing
app code. Modelled on Fichas (the in-memory index fed by the event log, the
seal, the plaintext gate) and Portero (the pins, `.amadan/ci`, the
Makefile, estilo's files served straight from the module).

## Layout

- `cmd/mapa/` — `rastrillo.Resolve` + `Serve`, after `LoadConfig`.
- `internal/mapa/`
  - `config.go` — every refusal to boot, tested.
  - `auth.go`, `auth_navigation.go`, `door.go` — magic-link sign-in with
    the `MAPA_OWNERS` admission list, Docs' keymail continuation, estilo's
    door.
  - `models.go`, `migrations/` — spec §3.1. `seal/` — the only encryption.
  - `store.go` — `Store.write`, the one way to write: it refuses to commit
    a transaction that logged no event (gate 8). Draft writes
    (`drafts.go`) and retention (`retention.go`) are the exemptions, by
    name (§5).
  - `pages.go`, `verbs.go`, `aliases.go`, `daily.go`, `share.go`,
    `markers.go`, `commit.go`, `history.go` — the §3.4 and §5 verbs.
  - `markup/` — Mapa's Markdown subset: §3.2's blocks, links, the safe
    renderer and the three-way block merge.
  - `locale/` — dates, times and numbers in the reader's locale (level-up
    build note 85): `dates.json`, CLDR's date patterns for the offered
    locales (`make locale-dates` writes it from Node's ICU), and
    `format.go`, their interpreter; numbers are rastrillo's. `locales.go`
    picks the reader's locale through rastrillo's middleware and clones
    the templates per locale. Write every date or count a person reads
    through it (`{{longDate}}`, `{{stamp}}`, `{{number L n}}`,
    `viewer.Locale`, `scope.Locale`); store and link ISO dates only.
    The reader's locale is their own (Settings, `settings_locale.go`,
    sealed by `members_locale.go`), else their browser's Accept-Language
    alone, else `MAPA_LOCALE`.
  - `index.go`, `graph.go`, `views.go`, `mentions.go`, `postings.go`,
    `search.go`, `rank.go`, `suggest.go` — the in-memory index, fed by
    the event log and partitioned by scope (§6.2).
  - `readrule.go`, `s3.go` — S2's rule and S3's layout, item 6's seams.
  - The Notion import (`docs/superpowers/specs/2026-11-17-notion-import.md`,
    phase 1): `import_*.go`, the only files that import
    `amadan.net/oficina/notion.git`. `import_connect.go` (NotionAccess,
    the fake in tests), `import_credential.go` (connect, disconnect,
    finish, forget), `import_book.go` (bookkeeping, fenced to notion_*),
    `import_run.go` and `import_job.go` (leases, Stop, resume, the
    goroutine), `import_survey.go`, `import_set.go`, `import_titles.go`,
    `import_write.go` (Survey and Write), `import_store.go`
    (`ImportCreate`, `ImportUpdate`), `import_images.go`,
    `import_images_store.go` and `import_images_place.go` (the image
    step over Mapa images, and notion_images), `import_history.go`
    (History's and the kicker's words), `import_handlers.go`,
    `import_view.go`,
    `import_report.go` and `templates/import*.html` (the screens).
  - `notionmd/` — the import's mapper, Notion's block tree to Mapa's
    Markdown, pure, checking its own output against `markup`. It never
    imports the notion module: its tree is Mapa's own copy.
  - `handlers_*.go`, `today.go`, `private.go`, `settings.go`,
    `render.go`, `shell.go`, `templates/`, `static/` — the screens, and
    estilo's files taken straight from the pinned module.
  - The level-up's shell (`docs/superpowers/specs/2026-11-14-mapa-level-up.md`):
    `frame.go`, `menus.go`, `side.go` and `templates/frame.html` build
    estilo's stacked bar, the menus and the sidebar with its rail;
    `around.go` (the one scoped accessor every new read path uses),
    `mapview.go`, `calendar.go` and `static/links.*` are the Links panel,
    its map, the week and the month; `pins.go` and `recent.go` are the
    sidebar's own lists; `lastedit.go` is the kicker's "Edited by".
    `mapa.js` is the editor's and ⌘K's script layer, `links.js` the
    panel's and `history.js` the History panel's; `frame.css`, `links.css`,
    `history.css` and `palette.css` style them. `versions.go`, `diff.go`,
    `diff_words.go` and `history_view.go` are the History panel's list,
    summaries and word diff (level-up §4.5); `typeddate.go` and
    `snippet.go` are ⌘K's typed dates and marked snippets.
    Phase 2's server half: `autosave.go` and `handlers_autosave.go` are
    the editor's autosave, its merge reply, `rebase` and `commit=1`;
    `firstscratch.go` a new scratch page's single-flight first commit
    (D12); `island.go` the editor's JSON island and `/links/resolve`;
    `markup/settle.go` the byte-preserving `Settle`.
- `editor/` — the page editor (level-up spec §6): `markup.mjs`, a
  line-for-line port of `markup/`'s parser; `schema.mjs`, whose `toDOM`
  is the server's HTML; and the bundle build, whose output
  (`internal/mapa/static/editor.<hash>.js`) is committed. Gate L4
  (`parity.test.mjs`) holds both to `markup/testdata/golden.json`, which
  Go writes from the corpus beside it. Change the renderer, the parser
  or the schema together, then `make golden` and commit the result.
  `serialize.mjs` writes the document back as Markdown, re-emitting
  untouched blocks and seams from `fidelity.mjs`'s side table (§6.4),
  which also holds the settle plugin (reparse, marker and list rules),
  rebuild and `prepareSave`; `clipboard.mjs` is §6.6's copy and paste.
  Gates L1–L3 (`fidelity.test.mjs`) run over the same corpus; L3 writes
  `markup/testdata/editor.json` (`make editor-golden`), which Go parses
  in `editor_test.go`.
  The editor on the page: `page.mjs` reads the island and mounts on the
  `<article>`; `saver.mjs` is §6.5's autosave, commit, rebase, leaving
  and closing (`adapter.mjs` is its view of the editor); `conflict.mjs`
  the inline choice; `resolutions.mjs` and `chipview.mjs` the chips'
  states; `keys.mjs`, `commands.mjs` and `inputrules.mjs` the keys,
  blocks and Markdown shortcuts; `suggest.mjs`, `slash.mjs` and
  `combobox.mjs` the `[[` and `/` lists; `datepicker.mjs` the date
  picker `/` → Date and Insert → Date… open, reading typed dates with
  `typeddate.mjs`, a port of `typeddate.go` held to it by
  `internal/mapa/testdata/typed_dates.json` (build note 83);
  `dates.mjs` writes dates from the island's patterns, held to Go by
  `locale/testdata/date_labels.json` and L4's `golden_locales.json`;
  `bubble.mjs` the selection's
  toolbar; `pagelink.mjs` its Page link and ⌘L (build note 81);
  `anchors.mjs` the ¶; `tasks.mjs` the task boxes; `writearea.mjs` a
  click on the page's empty area (build note 84); `maketeam.mjs` Make
  team page from a selection; `accessory.mjs` the
  phone's row; `notices.mjs` the bars and the lead's save status;
  `title.mjs` the title renamed in place, shared with the header's
  field (build note 78).
  `saver.test.mjs` is L9's client half; in `internal/mapatest/`,
  `editor_browser_test.go` holds L5 and L9 end to end,
  `editor_typing_browser_test.go` the typing, and
  `editor_access_browser_test.go` L10, with axe-core vendored in
  `testdata/axe/`.
  - Images (`docs/superpowers/specs/2026-11-17-mapa-images.md`, its §12
    the build notes): `imagefile/` sniffs, limits and strips each format
    without decoding pixels; `images.go` (types, limits, the blob
    semaphore), `images_store.go` (upload, sealing, Put),
    `images_read.go` (the read rule, serving bytes, copies, the
    allowances), `images_attach.go` (`tx.commit`'s explicit attaching
    and the `unreferenced_at` stamp), `images_copies.go` (Make team
    page's and `draftToScratch`'s copies), `images_remove.go`,
    `images_sweep.go`, `images_render.go` (drawing on page P),
    `images_list.go`, `images_bucket.go` (the bucket's own Put),
    `config_images.go`, `handlers_upload_read.go` (uploads read into
    bounded memory, never to a temporary file), `bodies.go` (every body
    bounded; multipart for upload routes only) and `handlers_images*.go`.
    In `editor/`: `imagedom.mjs`, `imageview.mjs`, `upload.mjs` (its
    queue gated by `upload.test.mjs`), `imagebubble.mjs`, `altinset.mjs`
    (the ALT badge and the description field over the image, build
    note I22).
  - A team page's look (`docs/superpowers/specs/2026-11-22-mapa-columns-icons.md`,
    §2–§4, phase 1): `looks.go` (the record's `icon` and `banner`, the
    `styled` verb, `SetLook`), `looks_images.go` (which images a look
    may name, and a removal taking its image out of the look),
    `looks_render.go` (drawing icons wherever a title is, the band),
    `looks_history.go` (History's words), `handlers_look.go` and
    `templates/look.html` (POST and the scriptless page),
    `emoji/` (Unicode's RGI list, vendored at 16.0, and the picker's
    JSON, `emoji_route.go`), `images_cache.go` (the small-image cache,
    served only after the read rule) and `static/look.js` (the picker,
    uploads and Reposition). A record's references are its body's and
    its look's images (`Record.images`); chips draw icons from their
    resolution (`markup.ChipIcon`), never from a node attribute.
  - Column layouts (the same spec, §1, phase 2): `markup/columns.go` (the
    strict grammar of §1.1, fences as blocks with `Block.Column`,
    `FenceLike`, `Grammar`, `renderLayout`) and `markup/merge_columns.go`
    (`ValidLayouts`, the merge's layout rules, `alignKeys`,
    `addedColumns`, `WholeOrInside`); `grammar.go` (`countedHere`, the
    409 for a post counted by another grammar); `versions_columns.go`
    (History's words). In `editor/`: `columns.mjs` (the commands),
    `markup.mjs`'s port of the grammar, the schema's `columns` and
    `column` nodes, fence pseudo-blocks in `serialize.mjs` (their
    origins on the containers: `originAttr`), and `fidelity.mjs`'s
    `fenceRule` (D16). `make pandoc-fixture` records Pandoc's reading
    (`markup/testdata/pandoc.json`, COL12).
  - Pages inside (the same spec, §5, §6.3, phase 3): `inside.go` (the
    "In: [[P]]" relation, derived in the index's `within` map, and the
    scoped `Inside`), `side_inside.go` (the Pinned rows' arrows, the
    `mapa-open` cookie, `POST /view/open` and `GET /p/{id}/inside`),
    `import_pins.go` (the import pins its roots once, marked in the
    sealed coordinate). Pins hold up to 20 (`pins.go` still reads a
    list sealed at 12).
  - The importer's looks (the same spec, §6.1, §6.4, phase 4):
    `import_looks.go` (a page's icon and cover prepared before its
    write, under notion_images' pseudo-blocks `icon:` and `cover:`;
    covers fetched only from Notion's file store and its cover gallery,
    the one external path the notion module pins), and `ImportUpdate`'s
    styled revision between the renamed and the edited.
- `internal/mapatest/` — the gates of spec §11, from the outside.
  `browser_test.go` is behind the `browser` tag.
- `scripts/preview/`, `scripts/mutate/` — `make preview`, `make mutate`.

## Rules Mapa holds hard

- **Sealed at rest.** Titles, aliases, dates, bodies, drafts, addresses and
  time zones are sealed. The plaintext columns are exactly §4's list:
  `plaintext_test.go` reads the real schema, and the file scan signs in
  for real and reads every byte on disk, WAL included (gate 4). The one
  named exception is rastrillo's: a signed-in address in its own
  `auth_links` and `auth_sessions` rows (spec §4), never in Mapa's tables.
- **Every write logs an event**, in the same transaction (gate 8). Mapa is
  never an actor: every event names a person.
- **A GET never writes.** Not a member row, not a draft, not a daily note
  (gate 10). A person's member row is created by their first write.
- **Scope before traversal.** Every read of the in-memory index is team
  pages plus the viewer's own private pages, and nothing anywhere counts
  what the viewer cannot see (§3.3, §6.3, gate 1).
- **A row you may not see is a 404**, never a 403.
- **Mapa reaches the network in two places only:** Notion, through
  `amadan.net/oficina/notion.git`, only from `import_*.go`, only during a
  run (and the one verify call when a token is saved); and CARLOS's own
  bucket, for images, from `images_bucket.go` and `config_images.go`. No
  client, transport, dialler or resolver anywhere else in `internal/mapa`
  or `cmd` (gate I8 names the two image files). Rastrillo's sign-in mail
  lives in rastrillo.
- **Screens work with JavaScript off**; destructive actions get their own
  confirm page.
- The gate is `make ci` (vet, gofmt, rastrillo's code budget, Fiesta's
  spacing check, tests, migration check, and `make editor`: npm ci, the
  bundle rebuilt with no drift, `node --test`), the same definition CI
  runs. It needs `node` and `npm` on PATH. `make spacing` holds every
  margin, padding and gap in `internal/mapa/static` to a
  `var(--rst-sp-N)` step or a `/* spacing-allow: why */`, and `make
  browser`'s rhythm audit holds the signed-in screens to the scale
  (level-up build note 89). `CGO_ENABLED=0`
  throughout. Use `make`, not bare `go test`: the Makefile pins the
  toolchain. `make browser` is gate 7 and `make mutate` proves gates 1, 4
  and 8 can fail, that a rename carries only drafts on its own base
  (L9, level-up build note 79), that Recently edited filters team
  pages inside its query (build note 80), the images' named risks
  (`scripts/mutate/images.go`), and that the Notion import's gates I1
  to I25 can (`MUTATE_GATES=I3,I13` runs a few).

## Serving

    MAPA_DEK=<base64 32 bytes> MAPA_INSTANCE_KEY=<secret> \
    MAPA_OWNERS=ana@example.com MAPA_ALLOW_LOGGED_MAIL=1 \
    ./mapa -addr 127.0.0.1:8834

`MAPA_TIME_ZONE` is the team's default time zone (UTC if unset).
`MAPA_LOCALE` is the team's locale, how dates and numbers are written
for a browser that asks for none Mapa offers (en-IE if unset; one of
`locale.Offered()`, spelt as CLDR does).
`MAPA_IMPORTERS` names who may import from Notion, each also an owner;
unset, the import does not exist.
`HOME_DIRECTORY_ORIGIN` adds "All apps" to the account menu. `GET /healthz`
and `GET /api/version` are the framework's. `make preview` serves the real
app over synthetic pages, signed in, on loopback.

## Known follow-ups

- **estilo's `chrome` imports the frozen rastrillo path**, so `go.mod`
  carries it indirectly. Mapa imports nothing from it.
