SelfscribeDownloadOpen the app

Design document

Diagrams written as text, so a review sees every change line by line.

Document · Advanced · uses diagram, table

Edit it in the playgroundRead the file

What it shows

Write a design document whose diagrams are text you can review line by line.

What you learn

An architecture in Graphviz, flow, sequence and class diagrams in Mermaid, a contents list made from the headings, a CSV table, callout boxes that share one look, review badges and mentions as rules of your own.

Try this

  • Change link in the palette, and the RFC number, the mentions and the context box all follow.
  • Add a line D->>A: bye at the end of the handshake, and Figure 3 grows an arrow.
examples/design-doc.self · 215 lines
~~~ selfscribe
# Lesson: Write a design document whose diagrams are text you can review line by line.
# You learn: an architecture in Graphviz, flow, sequence and class diagrams in Mermaid, a contents list made from the headings, a CSV table, callout boxes that share one look, review badges and mentions as rules of your own.
# Try this: change `link` in the palette, and the RFC number, the mentions and the context box all follow.
# Try this: add a line `D->>A: bye` at the end of the handshake, and Figure 3 grows an arrow.
# summary: Diagrams written as text, so a review sees every change line by line.
# category: diagrams
# tags: document, advanced, work
# uses: diagram, table
use markdown
set title Design document
set style sans
set heading-font Space Grotesk

# ---- The palette: each colour named once, with its shade for dark pages --------------
# link is the blue of the header and mentions; good, risk and review colour the callouts and badges.
palette
  link      #1c64d8  dark #74a7ff
  good      #2b8a3e  dark #69db7c
  risk      #d9480f  dark #ff8a4c
  review    #6741d9  dark #b197fc

# ---- "RFC 014" and "meta a | b | c" around the title: the number and a row of pills ----
# keep leaves the word "RFC" on the page; the meta line is cut at each "|" into pills.
line   RFC _         -> div.rfc-no   keep { font: 700 0.75em/1 var(--doc-font-mono); letter-spacing: 0.14em; color: var(--link); margin-bottom: -2.6em }  as rfc-no
line   meta _        -> div.rfc-meta split "|" span.rfc-pill  { display: flex; flex-wrap: wrap; gap: 0.45em; margin: 0 0 1.4em; font: 0.8em/1.5 var(--doc-font-ui) }  as rfc-meta
class rfc-pill  { padding: 0.2em 0.75em; border-radius: 999px; border: 1px solid color-mix(in srgb, currentColor 18%, transparent); background: color-mix(in srgb, currentColor 5%, transparent) }
# "Summary: …" under the pills: the whole proposal in one larger sentence.
line   Summary: _    -> p.rfc-summary { font-size: 1.14em; line-height: 1.5; opacity: 0.85 }  as rfc-summary

# ---- "::: decision", "::: risk", "::: context" ... ":::": a tinted callout box -------
# The three share the class callout, which reads its colour from --tone. Each rule only sets --tone.
class callout  { margin: 1.2em 0; padding: 0.8em 1.1em; border-radius: 10px; border: 1px solid color-mix(in srgb, var(--tone) 35%, transparent); background: color-mix(in srgb, var(--tone) 8%, transparent) }
block  ::: decision ... :::  -> aside.callout  { --tone: var(--good) }  as rfc-decision
block  ::: risk ... :::      -> aside.callout  { --tone: var(--risk) }  as rfc-risk
block  ::: context ... :::   -> aside.callout  { --tone: var(--link); border-width: 0 0 0 4px }  as rfc-context
# "! Word" on its own line: the small capital heading at the top of a callout.
line   ! _                   -> div.callout-title { font: 700 0.75em/1.2 var(--doc-font-ui); letter-spacing: 0.1em; text-transform: uppercase; margin-bottom: 0.4em; opacity: 0.8 }  as rfc-callout-title

# ---- TODO, OPEN and DONE anywhere in the text: a review badge, one shape in three colours
class badge  { font: 700 0.7em/1 var(--doc-font-ui); padding: 0.25em 0.5em; border-radius: 4px; vertical-align: 0.15em }
word   TODO          -> span.badge  { background: var(--risk); color: var(--on-risk) }  as kw-todo
word   OPEN          -> span.badge  { background: var(--review); color: var(--on-review) }  as kw-open
word   DONE          -> span.badge  { background: var(--good); color: var(--on-good) }  as kw-done

# ---- "@Name@" and "~figure~" in a sentence: a person, and a number worth checking -----
inline @_@           -> span.mention   { font-weight: 600; color: var(--link) }  as rfc-mention
inline ~_~           -> span.metric    { font: 600 0.92em var(--doc-font-mono); padding: 0 0.3em; border-radius: 4px; background: color-mix(in srgb, var(--review) 12%, transparent) }  as rfc-metric

# ---- Fenced blocks drawn by plugins: write the diagram as text, see it drawn ----------
# verbatim hands the lines to the plugin untouched, so no rule of this page changes them.
block  ``` mermaid ... ```   -> mermaid   verbatim
block  ``` dot ... ```       -> graphviz  verbatim
block  ``` csv ... ```       -> csv       verbatim
# A toc block lists the headings of the page, and follows them as you add or rename one.
block  ``` toc ... ```       -> toc       verbatim
# "Figure N. text" on the line after a diagram: a small centred caption.
line   Figure _              -> div.caption keep { text-align: center; font: 0.82em/1.4 var(--doc-font-ui); opacity: 0.65; margin: -0.4em 0 1.6em }  as rfc-caption
~~~
RFC 014
# Offline sync for Field Notes

meta Status: In review | Owner: @Priya Raman@ | Reviewers: Tomás Ortega, Wen Li | Updated 2 September 2026

Summary: Every edit is written to the device first and sent in the background, so nothing typed in the field is lost to a closed tab, a crash or a flat battery.

```dot
digraph fieldnotes {
  rankdir=LR;
  graph [fontsize=13, labeljust=l, nodesep=0.3, ranksep=0.28];
  node [shape=box, style="rounded,filled", fillcolor="#3b5bdb", color="#3b5bdb", fontcolor="#fafbff", fontsize=16, height=0.55, margin="0.22,0.1"];
  edge [color="#868e96", fontcolor="#868e96", fontsize=13, arrowsize=0.7];
  subgraph cluster_device {
    label="ON THE DEVICE"; style="rounded,dashed"; color="#adb5bd"; fontcolor="#868e96";
    ui [label="Editor"];
    journal [label="Journal", shape=cylinder, fillcolor="#e8590c", color="#e8590c"];
    worker [label="Sync worker"];
  }
  subgraph cluster_cloud {
    label="IN THE CLOUD"; style="rounded,dashed"; color="#adb5bd"; fontcolor="#868e96";
    api [label="Sync API", fillcolor="#0c8599", color="#0c8599"];
    log [label="Change log", shape=cylinder, fillcolor="#e8590c", color="#e8590c"];
    store [label="Note store", shape=cylinder, fillcolor="#e8590c", color="#e8590c"];
  }
  ui -> journal;
  journal -> worker;
  worker -> api [label="batches"];
  api -> log;
  log -> store;
}
```
Figure 1. The sync worker is the only part of the app that talks to the network.

```toc
title: In this RFC
```

::: context
! Context
Field Notes is used by survey teams who spend whole days without signal. Today an edit made offline is kept in the browser and silently lost if the tab is closed before the device reconnects. Support traces ~31%~ of "lost work" tickets to this path.
:::

## Goals

- Every edit survives a closed tab, a crash or a flat battery.
- Sync resumes on its own when the network returns, with no user action.
- Two people editing the same note never lose each other's words.
- The first sync after a week offline finishes in under ~5 s~ for 2 000 notes.

Non-goals: real-time cursors and presence. They stay on the roadmap, OPEN for a later RFC.

## How a change travels

```mermaid
flowchart LR
  E([Edit]) --> L[(Local journal)]
  L --> Q{Online?}
  Q -- no --> W[Wait for network] --> Q
  Q -- yes --> P[Push batch]
  P --> S[(Server log)]
  S --> F[Fan out to devices]
```
Figure 2. Every edit is written to the local journal first, then pushed in batches.

## Sync handshake

```mermaid
sequenceDiagram
  participant D as Device
  participant A as API
  participant L as Change log
  D->>A: hello(since = cursor 812)
  A->>L: read changes after 812
  L-->>A: 14 changes
  A-->>D: changes + new cursor 826
  D->>A: push(local batch of 6)
  A->>L: append, merge conflicts
  A-->>D: ack(cursor 832)
```
Figure 3. The device pulls before it pushes, so merges happen against fresh data.

## Data model

```mermaid
classDiagram
  class Note {
    +id: UUID
    +title: string
    +body: Text
    +version: int
  }
  class Change {
    +noteId: UUID
    +author: DeviceId
    +ops: Op[]
    +clock: Lamport
  }
  class Journal {
    +append(change)
    +pending() Change[]
    +compact()
  }
  Note "1" --> "*" Change : history
  Journal o-- Change : holds
```
Figure 4. Notes keep their history as changes; the journal holds the ones not yet acknowledged.

::: decision
! Decision
Use an operation-based CRDT for note bodies and last-writer-wins for titles. Titles are short and conflicts there are rare; bodies are where people lose words. DONE in the prototype, see @Wen Li@'s branch.
:::

## Measurements

The prototype was run against three recorded field days. TODO add the fourth day once the Andes team uploads it.

```csv
Scenario, Notes, Changes offline, First sync (s), Conflicts, Lost edits
Coastal survey, 412, 1 830, 1.4, 3, 0
Forest plots, 1 208, 6 402, 3.9, 11, 0
Week in the hills, 2 050, 9 977, 4.6, 27, 0
Today (baseline), 2 050, 9 977, n/a, n/a, 212
```

::: risk
! Risk
Journals can grow without bound on devices that never reconnect. Compaction after ~30 days~ keeps them under 20 MB, but OPEN question: do we warn the user before that?
:::

## Rollout

- [x] Prototype behind a flag
- [x] Conflict tests on recorded field days
- [ ] Beta with two survey teams
- [ ] Remove the old "save when online" path

Questions and comments to @Priya Raman@ before 16 September.

---

## How it's made

- **Every diagram is a fenced block of text.** Figure 1 is a `dot` block that Graphviz draws, and the other three are `mermaid` blocks. A change to a diagram is a change to a few lines, so a review shows exactly what moved.
- **The header** is three rules of this page: `RFC` for the number, `meta` for the row of pills, cut at each `|`, and `Summary:` for the opening sentence.
- **The contents list** is a `toc` block, and the measurements table is a `csv` block.
- **The coloured boxes** are `::: decision`, `::: risk` and `::: context`, three rules that share one class. The words TODO, OPEN and DONE turn into badges wherever you type them, and `@Name@` is a mention.

## Try it yourself

1. In Figure 1, add `ui -> worker [style=dotted, label="wake"];` before the last `}`. The diagram grows an arrow from the editor to the worker.
2. Add a heading `## Open questions` above Rollout. It appears in the contents list at the top.
3. Add a row `Desert transect, 96, 340, 0.3, 0, 0` to the measurements. The table grows by one line.
4. Change the risk's `OPEN question` to `DONE: we warn at 25 days`. The purple badge turns green.

Next: *Data model* draws a whole model from text; *On-call runbook* is what a team writes once the design ships.