Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/backups/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Reports

Dated outputs of work done in this repo: summaries, audits, analyses, backup ledgers. A report records what happened at a time; it is not maintained afterwards (corrections get a new dated report, or a note in the original).

If a report becomes standing reference material, it graduates to `library/` — reports are the inbox, the library is the shelf.
22 changes: 22 additions & 0 deletions doc/backups/backup-process-2026-10-21.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Backup process + ledger — 2026-10-21

## The process

Backups here are layered, by what the thing is:

1. **Source-of-truth text** (memory, docs, scripts, plans) → git: `dezren39/hatch` (public-safe) and `dezren39/hatch-personal` (everything private, including "not secret but private-guidance" material). Commits are the change record; the push is the backup completing.
2. **Secrets** → Bitwarden Secrets Manager, by name; repos and docs reference names and locations only. Files that are themselves secrets get uploaded as secret values (file-level) — audited 2026-10-21.
3. **Bulky/opaque data** (session transcripts, tarballs) → Google Drive folders `Muse Backups` and `Muse Avatar Backup`, each with a manifest (date, source, sha256, restore instructions). Git is the wrong tool for these (see the chunk-drawer research in the library build notes).
4. **Databases** → native dumps stored in the private repo when tiny (a SQLite dump is text and diffs beautifully) plus independent exports for anything that matters.
5. **The nix store / toolchains** → reproducible from docs + the snapshot tarball (`~/tools/nix-snapshot.sh`); binaries that are NOT reproducible that way are listed in `library/toolchains/binary-backup.md`.

Rule of thumb: every backup run leaves a record a future agent can verify — a commit, a manifest with hashes, or an entry in this ledger.

## Ledger (most recent first)

- 2026-10-21 — Private DB backup (SQLite dump, verified by restore into a scratch DB) filed in hatch-personal. Details intentionally private-only.
- 2026-10-21 — Secret-file → Bitwarden coverage audit + upload (see private records).
- 2026-10-21 — Small-files/scripts sweep into hatch `archive/` + hatch-personal `junk/` with hash manifests.
- 2026-10-21 — Session logs: `session-logs-2026-10-21.tar.zst` (~/agents, 1.1 GB → 103 MiB) + manifest → Drive `Muse Backups`; MD5-verified after upload.
- 2026-10-18 — Avatar backup to Drive `Muse Avatar Backup` (12 files, sizes verified; restore test passed; daily job `avatar-backup-daily` continues it).
- Ongoing — memory/personality/instructions sync to hatch-personal with normal work (private).
55 changes: 55 additions & 0 deletions doc/backups/binary-backup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Binary backup manifest

Generated 2026-10-21. Drew's rule (2026-10-21): the toolchain **backup set includes the binaries themselves**, not just docs and secrets — including the Bitwarden CLI (`bws`) in addition to the Bitwarden secrets it guards.

Most entries in `~/tools/bin` are symlinks/dispatcher stubs that resolve reproducibly from nix/omnibin — those are covered by the setup-cycle docs and do not need byte-level backup. The standalone real files below are the ones to copy as-is.

| file | bytes | sha256 (first 16) | note |
|---|---|---|---|
| ~/tools/bin/age-keygen | 266 | 6b060af7d8c038e8 | |
| ~/tools/bin/bws | 12359704 | e6982cfac1094d9b | Bitwarden Secrets Manager CLI |
| ~/tools/bin/cargo | 256 | a100950361d070f0 | |
| ~/tools/bin/codedb | 4427904 | af44d5f02a6c9a28 | |
| ~/tools/bin/cowsay | 258 | b7740b9809c9e427 | |
| ~/tools/bin/crane | 256 | be91577b8a1ac8e4 | |
| ~/tools/bin/deno | 254 | 4c7ada24e696fbdc | |
| ~/tools/bin/docker | 258 | e3681a0254f0d1b0 | |
| ~/tools/bin/fff | 252 | 3e167cda916b4f64 | |
| ~/tools/bin/fff-mcp | 13355488 | 20917f2c3e35612d | |
| ~/tools/bin/figlet | 258 | fe9c3942d930a249 | |
| ~/tools/bin/fish | 254 | 3d3b597485d150b3 | |
| ~/tools/bin/getfacl | 260 | c0968976ce93d357 | |
| ~/tools/bin/git | 252 | 049121e426d9b97b | |
| ~/tools/bin/go | 250 | 2c122df9bf24305a | |
| ~/tools/bin/gofmt | 256 | 9d8bfd8a1df15613 | |
| ~/tools/bin/hello | 256 | fe940ebd1ed3d9c5 | |
| ~/tools/bin/just | 254 | e4361fbe2890472f | |
| ~/tools/bin/nix-instantiate | 276 | 6e57018a7f535e4c | |
| ~/tools/bin/nix-store | 264 | dd65b1979f6c21f5 | |
| ~/tools/bin/oci | 252 | 380c4e135c86215f | |
| ~/tools/bin/or | 250 | 4b85ec244ca08ff3 | |
| ~/tools/bin/pg_dump | 260 | 37f2bbf8e11f0c9c | |
| ~/tools/bin/prettier | 262 | c9b4365c02c6f163 | |
| ~/tools/bin/profile | 260 | 78ff579ea1239248 | |
| ~/tools/bin/ruff | 254 | c3faa7cc358d6ef5 | |
| ~/tools/bin/rustc | 256 | 08c36859e3a9f1c6 | |
| ~/tools/bin/rustfmt | 260 | a4c0e7a9f12e7977 | |
| ~/tools/bin/rustup | 258 | e0753235a8cd1e4a | |
| ~/tools/bin/screen | 258 | f1f614f1cef5fe46 | |
| ~/tools/bin/shellcheck | 266 | 7a232ee2cc8806c4 | |
| ~/tools/bin/shfmt | 256 | e6d81acf6578b9d5 | |
| ~/tools/bin/skopeo | 258 | 1c0c1faae31fd5aa | |
| ~/tools/bin/sops | 254 | e87db8469016699d | |
| ~/tools/bin/supabase | 262 | 91af8ed5796cbf3e | |
| ~/tools/bin/taplo | 256 | 9c5fa27d54713afc | |
| ~/tools/bin/timeline | 262 | 193bbde90014c5a9 | |
| ~/tools/bin/turso | 256 | 24ad55d62ff4cff2 | |
| ~/tools/bin/uv | 250 | 060a1679f1359bbc | |
| ~/tools/bin/xxd | 252 | 96ccfd733eff6a9b | |

Toolchains installed as trees (back up the tree or reinstall via its doc): Go (~/tools/go), Rust (~/tools/cargo + ~/tools/rustup), wrangler (~/tools/wrangler), nix (persistent store — see nix.md, snapshot via ~/tools/nix-snapshot.sh).
Chromium + Playwright: install targets only, optional/headless — see chromium-playwright.md. Tailscale CLI: keep installed — see tailscale.md.

## Turso CLI provenance (resolved 2026-10-21)

The turso binary originally 'just existed' because it was a dispatcher/omnibin stub (Nix build of turso-cli 1.0.30), auto-created on first invocation — never a deliberate install. It has since been replaced with the official install (`get.tur.so/install.sh`, archived in `archive/install-scripts/`): real binary at `~/.turso/turso` (**v1.0.34**), `~/tools/bin/turso` now symlinks to it, existing `~/.config/turso/settings.json` worked unchanged (auth whoami = dezren39). Sandbox gotchas for any reinstall: the official script needs `TAR_OPTIONS=--no-same-owner` (sandbox denies chown, same quirk class as the Nix installer), it performs NO checksum verification, and it always installs 'latest' with no pinning — a reinstall silently upgrades.
27 changes: 27 additions & 0 deletions doc/backups/muse-backup-SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# muse-backup

Status: tentative, first draft 2026-10-21.

## Purpose

Back up Muse's durable state — memory and avatar — so it can be restored after a sandbox loss or an accidental edit, and keep the locations/rules in one place.

## What lives where

- **Memory (source of truth):** `dezren39/hatch-personal` (private git repo). On disk it is `~/hatch-personal/`, symlinked back to `~/MEMORY.md`, `~/memory/`, `~/USER.md`, etc. Backup = commit + push the private repo (surgical staging — memory files only when that is the task). The full symlink restore map is in `hatch-personal/library/inventory/symlink-map.md`.
- **Avatar:** source image + generated renders under `~/workspace/avatars/`, tracked in the public repo `dezren39/hatch`. Drew wants the backup to clearly include the avatar *and its animations*, with liked animations restorable if an accidental source-image edit produces renders he dislikes — i.e. keep prior render sets, don't just overwrite them; a new source image gets a new render set alongside the old.
- **Too big for git:** session transcripts (`~/agents/`, ~1 GB) and similar bulk go as tarballs to the dedicated Google Drive **"Muse Backups"** folder, never into either git repo.
- **Secrets are never part of a backup.** They live in Bitwarden; backups reference names only. Credential files are SECRET-SKIP (paths + sizes in the private inventory classification) — never copied into a backup set.

## Procedure

1. `git -C ~/hatch-personal status` — review carefully; stage only the intended memory/personal files; commit; push. Never sweep unrelated dirty files into a backup commit.
2. Avatar: confirm the current source image and all render/animation files for it are present under `~/workspace/avatars/` and tracked in `dezren39/hatch`; if renders were regenerated, keep the previous set until Drew confirms the new one.
3. Bulk (only when asked): tar the target (e.g. `~/agents/`) with a dated name, upload to Drive "Muse Backups", record name + size + date in the private repo.
4. Report what was backed up, where, and the commit SHA / Drive file name. State explicitly what was skipped and why.

## Pitfalls

- `/home/hatch` itself is a git repo distinct from `~/hatch-personal` — commit memory with `git -C ~/hatch-personal`, not from home.
- `~/workspace/goals` is a symlink into hatch-personal — files there are private-repo files even though the path looks public-workspace.
- Deletion inside read-only (555) directories fails even as root; `chmod u+w` first, or keep backups as single tarballs instead of live trees.
27 changes: 27 additions & 0 deletions doc/backups/muse-backup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# muse-backup

Status: tentative, first draft 2026-10-21.

## Purpose

Back up Muse's durable state — memory and avatar — so it can be restored after a sandbox loss or an accidental edit, and keep the locations/rules in one place.

## What lives where

- **Memory (source of truth):** `dezren39/hatch-personal` (private git repo). On disk it is `~/hatch-personal/`, symlinked back to `~/MEMORY.md`, `~/memory/`, `~/USER.md`, etc. Backup = commit + push the private repo (surgical staging — memory files only when that is the task). The full symlink restore map is in `hatch-personal/library/inventory/symlink-map.md`.
- **Avatar:** source image + generated renders under `~/workspace/avatars/`, tracked in the public repo `dezren39/hatch`. Drew wants the backup to clearly include the avatar *and its animations*, with liked animations restorable if an accidental source-image edit produces renders he dislikes — i.e. keep prior render sets, don't just overwrite them; a new source image gets a new render set alongside the old.
- **Too big for git:** session transcripts (`~/agents/`, ~1 GB) and similar bulk go as tarballs to the dedicated Google Drive **"Muse Backups"** folder, never into either git repo.
- **Secrets are never part of a backup.** They live in Bitwarden; backups reference names only. Credential files are SECRET-SKIP (paths + sizes in the private inventory classification) — never copied into a backup set.

## Procedure

1. `git -C ~/hatch-personal status` — review carefully; stage only the intended memory/personal files; commit; push. Never sweep unrelated dirty files into a backup commit.
2. Avatar: confirm the current source image and all render/animation files for it are present under `~/workspace/avatars/` and tracked in `dezren39/hatch`; if renders were regenerated, keep the previous set until Drew confirms the new one.
3. Bulk (only when asked): tar the target (e.g. `~/agents/`) with a dated name, upload to Drive "Muse Backups", record name + size + date in the private repo.
4. Report what was backed up, where, and the commit SHA / Drive file name. State explicitly what was skipped and why.

## Pitfalls

- `/home/hatch` itself is a git repo distinct from `~/hatch-personal` — commit memory with `git -C ~/hatch-personal`, not from home.
- `~/workspace/goals` is a symlink into hatch-personal — files there are private-repo files even though the path looks public-workspace.
- Deletion inside read-only (555) directories fails even as root; `chmod u+w` first, or keep backups as single tarballs instead of live trees.
142 changes: 142 additions & 0 deletions pkgs/agent-backup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# runtime-db-export

Periodic export of the Muse runtime records DB into the Turso DB
`muse-mirror` (`agent_mirror_*` tables). Built 2026-10-29, approved by
Drew 2026-10-28. Runs as an agent cron job `runtime-db-export` every 6 hours
(America/Chicago) — it must be an agent job because scripts cannot reach the
runtime DB (per-exec mount namespaces hide the socket); the only source
route is the `muse.db` tool inside an agent run.

**Repointed 2026-11-06** (code#5691): the landing side moved from muse-dev2
Supabase (`agent.mirror_*`) to the dedicated Turso DB `muse-mirror`
(`agent_mirror_*`), whose initial contents are the migration copy of the PG
tables (219,105 rows, parity-verified). The job was paused for the migration
and is **safe to unpause** — see the status banner at the top of JOB-BODY.md
for the first-run checks. Nothing in this directory writes to Postgres
anymore; `load_rows_pg.py` / `run_sql_pg.py` are the retired PG versions,
kept for reference/rollback only.

## Files

- `JOB-BODY.md` — the exact cron job body (self-contained: every query,
loader spec, watermark rule). The stored cron body is character-identical
to this file (verified by diff against the cron projection).
- `load_rows.py` — the landing loader (spec JSON on stdin; upserts, history
archiving, epoch→date/hour conversion pinned to UTC; drops `_`-prefixed
columns). Writes to Turso via `turso_client.py`.
- `turso_client.py` — the Turso transport (libsql HTTP pipeline) + the
SQLite DDL for all 28 landing tables (created on first use; the 20
migration-copied tables already exist). Token by BWS name only
(`turso.muse-mirror-rw-token`), resolved via `fleet secrets get` with an
automatic direct-BWS fallback when the broker's cached value is rejected.
Also provides the staging mode: `RDE_TURSO_URL=file:/path.db` runs the
identical SQL path against a local SQLite file.
- `load_from_output.py` / `page_load.py` — feed saved oversized muse.db
outputs into the loader without rows passing through agent generation;
page_load prints the next keyset cursor.
- `run_sql.py` — raw SQL files → Turso muse-mirror (prunes, reconciliation
counts). Translates the PG dialect the job body uses (`agent.mirror_*`
names, `now()`, `DATE 'YYYY-MM-DD'`) to SQLite/libsql.
- `set_state.py` — records a unit's outcome in BOTH `state.json` and
`agent_mirror_export_state`.
- `spec_m1..m8.json`, `spec_r1.json`, `spec_r2.json`, `spec_s5.json` —
loader specs for the saved-output flow.
- `schema.sql`, `schema2.sql` — the original PG DDL for all landing tables
(historical; the live SQLite DDL lives in `turso_client.py`).
- `state.json` — per-unit watermarks/status (mirrored in muse-mirror).
- `progress/` — backfill logs per unit (build record).
- `merge_s5.py` — S5 merge helper from the build (superseded by the JOB-BODY
inline merge; kept for reference).

## Landing tables (28, all in Turso `muse-mirror` as `agent_mirror_*`)

Mirrors: mirror_scheduler_jobs (162), mirror_scheduler_job_runs,
mirror_agent_agents, mirror_goals_goals (49), mirror_goals_updates,
mirror_scheduler_job_definitions (metadata only), mirror_agent_sessions
(source empty), mirror_sessions (derived per-session summaries).
History: mirror_history_{scheduler_jobs, agent_agents, goals_goals,
goals_updates, agent_sessions}. Rollups: mirror_token_usage_daily,
mirror_token_usage_hourly, mirror_session_activity_hourly.
Stats: mirror_message_token_stats_daily, mirror_session_stats_daily,
mirror_run_stats_daily, mirror_histograms_daily, mirror_concurrency_hourly,
mirror_distincts_daily, mirror_top_daily, mirror_tool_stats_daily,
mirror_compaction_stats_daily, mirror_subagent_stats_daily,
mirror_conversation_stats_daily. State: mirror_export_state.
(Counts as of the inaugural backfill are in the build report; they grow
every run.)

## Design decisions and adaptations

- **History granularity = run cadence.** mirror_history_* rows are written
when a mutable row's payload_hash changes between runs; changes made and
reverted between two runs collapse into one step. job_runs has NO history
(immutable-ish; status transitions are refreshed in place by M2 REFRESH).
- **agent.agents lands a REDUCED payload.** spawn_metadata_json averages
~29KB/row (max 2.4MB — beyond muse.db's 64KB row cap) and
last_assistant_message ~4KB; the payload carries all scalar/identity
columns, the extracted scheduler_cron_job_id / worker phase, and the BYTE
LENGTHS of the four big text columns. Full-text export of agents is
impossible through muse.db.
- **scheduler.job_definitions is metadata-only** (task_text byte length +
md5): bodies are job prompts; length+hash detect changes. Source table
was empty at build anyway.
- **job_runs payload excludes** the raw device_read_state_json /
pending_terminal_result_json / onboarding_tour_body /
worker_approval_wait_json contents (NULL or approval blobs at build);
the approval blob's byte length is kept.
- **agent.sessions is EMPTY at source**, so mirror_agent_sessions is empty
and per-session summaries are DERIVED from agent.agents
(mirror_sessions): session key = coalesce(root_session_id, session_id),
with chat_id via chat.chats on the root agent's session_id.
- **Percentiles are nearest-rank** (row_number + rank
greatest(1, ceil(p*n))) — the muse.db vocabulary has no percentile_cont.
The "median" is the single middle rank ((n+1)/2 integer division): for
even n it is the lower-middle value, not the average of two middles.
- **Histogram bucket edges are fixed** (listed in JOB-BODY S4; do not
change). Zero-count buckets are skipped at load, so a recompute cannot
remove a bucket that dropped to zero (documented caveat).
- **Concurrency is an exact event-sweep** (single-pass peaks + carry-in
form; per-bucket correlated subqueries blow the 3s statement limit).
Runs with finished_at set but started_at NULL (never started) contribute
NO end event. Hour spine = hours with token usage (runs variant adds
run-scheduled hours). Run-concurrency is only meaningful inside the
job_runs retention window (~7 days at build); earlier hours read 0.
- **Rollups are upsert-only**: a recompute overwrites values for keys that
still exist but cannot retract a key that vanished from the source
grouping (e.g. an agent re-attributed to another job). Windows (last 4
days daily, last 2 hours hourly) bound the staleness.
- **M3 mirror is append-only**: agents deleted at source are not pruned
(at final build check the mirror held 12,726 rows vs 12,680 live source
rows — source-side churn during the scan). M1/M4 snapshot tables DO
prune (with history archiving).
- **S8 tool stats**: runtime.tool_calls / tool_outputs were EMPTY at
build; the unit is landed-empty and its error rule (output error_text
NOT NULL OR status outside ('success','completed','ok')) is PROVISIONAL
until real data exists to validate against.
- **Anchors are UTC epochs**: first token day_epoch = 20724 (2026-09-28),
first hour_epoch = 497390. (An early build note used 20728 as the first
day — that was a wrong anchor, caught and repaired by a gap backfill;
coverage now spans 2026-09-28 onward for every daily unit.)
- **Pad technique**: short-row aggregate pages return inline from muse.db
and would have to be retyped; appending a 600-char `_pad` literal forces
the runtime to save the page to a file, and the loader drops `_` columns.
Page data then never passes through agent generation.

## Operations

- Inspect health: `agent_mirror_export_state` in muse-mirror
(`turso db shell muse-mirror`) or `state.json` (last_status / last_error
per unit; failures are isolated per unit and named in the run's report
line).
- Manual run: `cron.run runtime-db-export`. Cadence changes need Drew.
- Steady-state cost (measured on the inaugural run, 2026-10-29):
50,772,671 input tokens / 199,890 output across the run's 6-agent tree
(worker root + live worker + 4 subagents), 3,067s wall. Usage rows post
only at run completion. NOTE: subagents do NOT inherit the
scheduler_cron_job_id spawn-metadata key, so the per-job rollup (R1/R2)
attributes only the root agents' ~25.6M input to this job — the true
all-in cost is the tree sum above. This is NOT a cheap job: at 6h
cadence it projects to ~200M input tokens/day. If that burn is
unwanted, levers are: longer cadence, narrower recompute windows, or a
leaner run agent (fewer inline pages in context). Cadence changes need
Drew.
Loading
Loading