78 lines
6.1 KiB
Markdown
78 lines
6.1 KiB
Markdown
# PROJECT KNOWLEDGE BASE
|
|
|
|
**Generated:** 2026-11-17
|
|
**Commit:** bcec5ef
|
|
**Branch:** master
|
|
|
|
## OVERVIEW
|
|
THE COMMONS ("the-commons-hollow") - an installable, local-first PWA "super-app": one Ed25519 identity, one signed event log, and one replay-derived wallet ledger under a WeChat/Alipay-style mini-app launcher. Stack: React 19 + React Router 7 + TypeScript (strict) + Vite 8 + Dexie (IndexedDB) + `@noble/ed25519`, PWA via `vite-plugin-pwa`/Workbox. A self-hosted backend (`api/blob/*`, `api/sync/*`, `api/admin/*`) now exists for mini-app blob sharing, cross-device event sync, and read-only admin monitoring - but the signed event log's own append/read/replay path (`src/lib/eventlog.ts`) stays fully local-first, working unaffected whether or not that backend is reachable.
|
|
|
|
## STRUCTURE
|
|
```
|
|
patriothacks/
|
|
├── api/ # self-hosted backend: blob/* (persistent blob store), sync/* (signed-event relay), admin/* (read-only aggregate)
|
|
├── src/
|
|
│ ├── lib/ # domain core: identity, signed event log, ledger, Dexie schema, sync.ts (best-effort relay client)
|
|
│ ├── shell/ # mini-app launcher UI: home grid, wallet card, router outlet
|
|
│ ├── admin/ # /admin seed panel: seed.* config events + demo fixture + backend-aggregate monitoring view
|
|
│ ├── miniapps/ # 13 mini-app screens (fairshare, fairdraw, waterwatch, healthpost, tunnelsafety, sky, findfamily, assembly, farms, housing, lightclock, theclimb, mentor)
|
|
│ ├── App.tsx / AppRoutes.tsx / main.tsx # root, routes, PWA SW registration
|
|
├── fixtures/ # TEST/REFERENCE data only - never imported by app code (see ANTI-PATTERNS)
|
|
├── public/ # PWA icons, favicons
|
|
├── .omo/ .senpi/ # planning/research/evidence artifacts + generated HTML - NOT app code
|
|
└── vite.config.ts # PWA manifest + Workbox live here
|
|
```
|
|
|
|
## WHERE TO LOOK
|
|
| Task | Location | Notes |
|
|
|------|----------|-------|
|
|
| Add/verify a signed event | `src/lib/eventlog.ts` | `append()` is the sole LOCAL-signing write path; `appendEntry()` separately persists pre-verified foreign entries via `src/lib/sync.ts` |
|
|
| Balances / wallet math | `src/lib/ledger.ts` | pure reduction over the log; never stored |
|
|
| Identity / crypto | `src/lib/identity.ts` | Ed25519 via `@noble/ed25519`, non-custodial |
|
|
| DB schema / tables | `src/lib/db.ts` | Dexie, versioned `.stores()` |
|
|
| Home grid / mini-app tiles | `src/shell/miniappRegistry.ts` | single source of truth for tiles |
|
|
| Router -> mini-app mount | `src/shell/MiniappOutlet.tsx` + `miniappComponents.tsx` | id lookup, no per-app branches |
|
|
| Seed / demo data | `src/admin/seedEvents.ts` | starting config only, not outcomes |
|
|
| Routes | `src/AppRoutes.tsx` | `/admin` standalone; everything else under `ShellLayout` |
|
|
|
|
## CODE MAP
|
|
| Symbol | Type | Location | Refs | Role |
|
|
|--------|------|----------|------|------|
|
|
| `append` | fn | `lib/eventlog.ts` | ~50 | sign + append; sole LOCAL-signing write path (`appendEntry()` separately persists pre-verified foreign entries via `src/lib/sync.ts`) |
|
|
| `db` | const | `lib/db.ts` | ~34 | Dexie instance (`identity`, `eventLog` tables) |
|
|
| `computeBalances` / `balanceOf` | fn | `lib/ledger.ts` | ~35 | replay log -> per-account balance |
|
|
| `readAll` | fn | `lib/eventlog.ts` | ~19 | ordered read for replay/live queries |
|
|
| `sortForReplay` | fn | `lib/ledger.ts` | ~4 | deterministic order: lamportTs, pubkey, sig |
|
|
| `miniappRegistry` | const | `shell/miniappRegistry.ts` | ~11 | 13 tile definitions |
|
|
| `toAccountId` | fn | `shell/accountId.ts` | ~10 | hex(pubkey) -> ledger account id |
|
|
| `mergeReplicas` | fn | `lib/ledgerConflicts.ts` | ~6 | double-spend detect, earlier-Lamport-wins |
|
|
|
|
## CONVENTIONS
|
|
- Named exports only (no default exports except `App`); co-located `*.test.ts(x)` beside source.
|
|
- Imports use explicit `.ts`/`.tsx` extensions (bundler mode, `allowImportingTsExtensions`, `verbatimModuleSyntax`).
|
|
- CSS Modules (`*.module.css`) per component; theme = Alipay blue `#1677FF`.
|
|
- Crypto/DB are async (`@noble/ed25519` async API, Dexie promises); never wrap `await`ed signing inside a `db.transaction()`.
|
|
- Typed error classes (e.g. `IdentityMissingError`, `SignatureVerificationError`) over bare throws.
|
|
|
|
## ANTI-PATTERNS (THIS PROJECT)
|
|
- No network call in the event-log append/read/replay path (`src/lib/eventlog.ts`'s `append`/`appendEntry`/`readAll`/`readRange`) - this has always meant that path specifically, now stated precisely: a backend IS present and fine for mini-app sharing/sync/monitoring (`api/blob/*`, `api/sync/*`, `api/admin/*`), but it must never be able to block, slow, or corrupt a local write/read/replay - `src/lib/sync.ts`'s `trySyncWithBackend()` is the one sanctioned caller of that backend and is a silent, throw-free no-op whenever it is unreachable.
|
|
- No unsigned/bypass write to the Event Log; every event (incl. seed/demo) goes through `append()`.
|
|
- No update/delete of event-log entries - the log is append-only; no such API exists.
|
|
- Shell/registry must NOT import `src/miniapps/*` business logic (enforced by `src/shell/importGraph.test.ts`).
|
|
- No blockchain/mining/consensus for the wallet (Ed25519 + local deterministic replay only).
|
|
- Production code must NEVER import `fixtures/gecco2018-water-quality-sample.csv` (test-only dataset).
|
|
- No cross-platform mini-app runtime (Taro/uni-app) - this is a plain React SPA.
|
|
|
|
## COMMANDS
|
|
```bash
|
|
npm run dev # vite dev server
|
|
npm run build # tsc -b && vite build (type-check gate + production bundle)
|
|
npm run test # vitest run
|
|
npm run lint # oxlint
|
|
```
|
|
|
|
## NOTES
|
|
- Tests run in the `node` environment (`vitest.config.ts`); React/DOM tests opt in per-file with a line-1 `// @vitest-environment happy-dom` pragma. `src/lib/test-setup.ts` loads `fake-indexeddb/auto` globally.
|
|
- `src/miniapps/` now holds the shipped mini-app screens (fairshare, fairdraw, waterwatch, healthpost, tunnelsafety, sky, findfamily, assembly, farms, housing, lightclock, theclimb, mentor); `miniappComponents.tsx` still renders a "Coming soon" placeholder for any registry id without a mounted component.
|
|
- `.omo/` and `.senpi/` hold the work plan, research, evidence, and drafts referenced by commit messages; they are not part of the shipped app.
|