Fieldnote
Case Study
Overview
An offline-first notes app and CRDT sync engine for people working without signal for days. Notes live on the device, and edits made on more than one device merge on reconnect without conflicts.
Designed so notes edited offline on more than one device merge cleanly on reconnect, without losing changes.
Where it started
The inspiration came when two of my crew members got lost on a hiking trip and had to call emergency services after being stuck for two nights.
I'd also been wanting a decent note-taking app, like the paid subscription one I was using. Fieldnote is my take on both: a proper notes app that keeps working when there's no signal at all.
Works with no signal
Notes live in SQLite on the device, so writing never waits on a network. Every edit goes into an outbox; whenever the relay can be reached, the sync client pushes the outbox and pulls anything new, and with no signal the status bar simply says “Working offline”.
app/src/sync/client.ts
while (more) {
const outgoing = db.outbox(BATCH);
const res = await fetch(`${this.state.server}/api/sync`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ replica: this.vault.replica, cursor: Number(db.get('cursor') ?? 0), ops: outgoing }),
signal: AbortSignal.timeout(15_000),
});
if (!res.ok) throw new Error(`Relay answered ${res.status}`);
const body: { cursor: number; more: boolean; ops: Op[]; conflicts: number } = await res.json();
db.clearOutbox(outgoing);
const fresh = this.vault.applyRemote(body.ops);
db.put('cursor', String(body.cursor));
more = body.more || db.outboxSize() > 0;
}
Conflict-free by design
Every character is its own insert operation, anchored after another one, and deletes are tombstones. Each operation has a globally unique id, so merging two devices is just a set union: it can happen in any order, any number of times, and conflicts can't occur. The same engine is written in Rust for the relay and in TypeScript for the app.
engine/src/sync/merge.rs
// sync/merge.rs: conflict-free on reconnect
impl Note {
pub fn merge(&mut self, remote: &Note) {
for (id, op) in &remote.ops {
self.ops.entry(*id).or_insert(op.clone());
}
self.clock.join(&remote.clock);
self.body = self.ops.render();
}
pub fn persist(&self, db: &Connection) -> Result<()> {
let tx = db.unchecked_transaction()?;
for op in self.ops.since(self.synced_at) {
tx.execute(
"INSERT OR IGNORE INTO ops VALUES (?1, ?2)",
params![op.id, op.to_bytes()],
)?;
}
tx.commit()
}
}
Tested for days offline
The engine's tests check that devices converge whatever order they reconnect in, that merging twice changes nothing, and that three days of offline edits come back with zero conflicts.
engine/src/sync/merge.rs
#[test]
fn three_days_offline_forty_one_edits_zero_conflicts() {
let base = note_with("Camp inventory\n");
let (mut field, mut camp) = (base.clone(), base.clone());
// 41 edits made with no signal, while camp keeps editing too.
for i in 0..41 {
let at = field.body.chars().count();
field.insert(A, at, &format!("- day {} fuel check\n", i / 14 + 1), i);
}
camp.insert(B, 15, "Fuel for 4 more days\n", 0);
camp.merge(&field);
field.merge(&camp);
assert_eq!(field.body, camp.body);
assert_eq!(camp.body.matches("fuel check").count(), 41);
}
Built for the field
Expedition mode goes offline on purpose and counts queued edits. Chronos scrubs a note through every version it has had, Palimpsest shows every character ever written tinted by device, and one keystroke stamps GPS coordinates, which work without signal. Around them sit the essentials of a full notes app: wikilinks, backlinks, a daily journal, tasks, and a 3D constellation of your notes.