Portable-Notes

PeaNote Architecture (3.11.0)

PeaNote is a local-first application with three layers and no build step:

Browser (static/index.html + static/app.js + static/style.css)
        │  fetch('/api/...')
        ▼
app.py — localhost HTTP host: routing, headers, static allowlist, CSRF guard
        │  imports                         ── contracts described in
        ▼                                    api/routes.py
core/vault.py — paths, front matter, SQLite index, history, recovery, integrity, migrations
core/tasks.py  — task syntax constants + parser model
core/version.py — single runtime version source
        │
        ▼
vault/*.md  (source of truth)      data/index.db (disposable projection)

The server binds to 127.0.0.1, preferring port 8765 and falling back to a free local port when it is busy. The active URL is published in data/server.json and used by start.bat / start.sh for status, stop, open, and capture. There is no telemetry, account, external API, or packaged runtime.


Runtime modules

app.py

core/vault.py — canonical vault layer

This is the single source of truth for filesystem and index behaviour; app.py imports it directly. It provides:

Concern Functions
Paths norm_rel(), md_path(), safe_name(), vault()
Front matter parse_front(), front(), boolv()
Index init_db(), index_note(), refresh(), rebuild(), prune_index(), serialize(), note_count()
Fast scans iter_note_entries() and iter_tree_entries() use os.scandir cached stats, skipping hidden/internal dirs
Search safety fts_query() tokenizes user input into a quoted FTS5 AND query
History snapshot(), note_history(), history_path(), history_dir(), save_history_label(), snapshot_diff()
Recovery front_matter_errors(), vault_integrity(), migration_status(), migration_plan(), apply_migrations(), rollback_migration()
Sync safety sync_ledger_path(), load_sync_ledger(), save_sync_ledger(), record_external_change(), sync_status()
Settings cfg_bool(), cfg_int() — bounded, allowlisted reads from data/config.json
Onboarding default_onboarding(), load_onboarding(), update_onboarding()
Maintenance iter_notes(), orphan_attachments(), seed()

Important invariants:

core/version.py

VERSION = "3.11.0" is the one runtime version source. core.vault re-exports it so existing imports keep working. scripts/check_version.py verifies every pinned runtime source (app header, UI assets, config, launcher, E2E pins) and fails on drift; historical release notes keep their old versions.

core/tasks.py

Defines the compiled regex constants for task syntax (completion, due, baseline, project, priority, estimate, task ID, dependencies, milestone), the Task model, and parse_task_line() / parse_tasks(). Route handlers currently import the constants and run their own parse loops; unifying those loops is planned for 3.6.

core/errors.py, services/task_repository.py

Typed error classes and a reusable task filter boundary. services/task_repository.py is currently exercised by tests only; it is not on the HTTP path.

api/

Only api/routes.py is on the HTTP path today. The other modules remain thin reference adapters so a future extraction cannot reintroduce a second vault implementation, which caused several 3.1 defects.

scripts/generate_api_docs.py renders docs/API_REFERENCE.md from the registry; --check fails in CI when the checked-in file is stale. tests/test_api_contracts.py also verifies registry uniqueness, that every exact /api/ path in app.py is registered (and vice versa), and that docs are current.

static/

Vanilla JS single-page UI. app.js owns state (st), rendering, autosave, dialogs, command palette, board/graph/tags/trash views, and global shortcuts. Note cards use one delegated handler on #list; the folder tree filters that list rather than duplicating note rows; open() uses a sequence guard so stale responses cannot overwrite a newer click. A local undo/redo stack records editor input and direct toolbar/restore mutations, while native undo stays active in other fields. style.css is a token-driven design system with light/dark/auto themes, a narrow-screen navigation toggle, and aligned side-by-side diff columns.


Data model

Markdown files are authoritative. SQLite (data/index.db) contains:

The index can be deleted at any time; startup or POST /api/rebuild recreates it. All task/project reports are read-only projections derived from indexed Markdown; they never write a competing task database.

Internal state lives in .portable-notes/ inside the vault (history snapshots with .labels.json, trash, and verified ZIP backups). It is excluded from indexing, graph/tree views, exports, and link rewrites. Application-local UI state lives in data/config.json (including the allowlisted launch_mode) and data/onboarding.json; migration backups live in data/migrations/. All stay on this machine and are not served over HTTP.


Security model

Known limitation: any local process can call the localhost API; this is inherent to an unauthenticated local app and is why the browser origin check exists.


Testing strategy

Layer Command Coverage
Unit python -m unittest discover tests -v 110 tests: front matter, path safety, vault/index invariants, onboarding state, background reindex, diagnostics, capture/drop-folder/Markdown bundle, recovery helpers, migrations, launch modes, sync safety, revamp regressions, task parser/repository, scroll layout, dispatch tables, local transcription
Live E2E python tests/e2e_ui_actions.py 132 checks against a temporary vault: API payload shapes, paging, onboarding persistence, reindex lifecycle, task endpoints, capture, drop folder, exports, attachments, history labels, trash recovery, backups, integrity, migrations, launch flags, sync conflict guard, security, local transcription wiring, and source-level UI wiring
Benchmark python scripts/benchmark.py 1k / 10k / 50k note runs with seeded vaults, real handler, median request timings
Contract python -m unittest discover tests -v (test_api_contracts.py) 86 registered routes, live-path parity, generated-doc freshness
Version python scripts/check_version.py 10 pinned runtime sources agree with core.version.VERSION
Syntax python -m compileall -q app.py core api services scripts tests and node --check static/app.js All Python modules and the browser script

The E2E suite starts app.py with a throwaway vault on port 8913 and restores the user’s real data/config.json and data/onboarding.json afterwards. It also sets PN_INDEX_DB inside that throwaway vault so deleting it cannot be mistaken for an external removal in the real vault. It writes UI_TEST_REPORT_v3.11.0.md. Because it is stdlib-only, dialog interactions are verified by source tokens rather than a browser driver; see AUDIT_REPORT.md for the remaining gap.


Planned evolution

ROADMAP 3.6 landed the route registry, generated API reference, core.version drift guard, contract tests, Make/CI gates, and runtime dispatch tables in app.py. Incremental/background indexing, paged lists, and browser-native list/tree virtualization are already in the runtime; a runtime plugin loader remains deferred. None of these is required to understand the current runtime.