Commit graph

8 commits

Author SHA1 Message Date
9a9321adf7 feat(M8): migration runner + per-(share,id) snapshots 🏗️🔑
The v2 foundation (V2-SPEC §5.1). Two things land:

- PRAGMA user_version migration runner in internal/hub/meta: ordered,
  append-only, each step in its own txn with the version bump LAST so a
  crash rolls back cleanly. Backs up an existing DB to <path>.pre-v2.bak
  (VACUUM INTO) before migrating; refuses a DB written by a newer build.
  Every later v2 schema change rides this.

- Re-key snapshots from a GLOBAL id PK to per-(share, id). v1's global id
  meant identical content pushed to two shares collided on one row, so the
  second share never counted its chunk refcounts (undercount) and recorded
  no snapshot row. Per-share rows fix the refcount and let each share own
  its history. Wire id/Head stays the manifest hash — clients unchanged.
  Migration also backfills any share head left rowless by the v1 idempotent
  push, restoring the head-has-a-row invariant. GC reworked to prune per
  (share, id), decrementing each share's chunks and freeing a shared
  manifest blob at most once.

Tests: cross-share refcount, migration versioning + refuse-newer, and a
realistic populated-v1 -> v2 upgrade (data preserved, backup written,
head backfill). All 17 pkgs -race green; hub cross-compiles linux/amd64.

Deferred to M8-3 (conflict sidecar): restore/deploy conflict-copy — a naive
version breaks restore-reproduces-snapshot; it needs the base/ancestor
awareness the sidecar introduces. snapshot_chunks edge table deferred to
M9 (no consumer yet; the runner makes adding it a one-line migration).
2026-06-23 10:46:26 -05:00
04f94d7730 harden(auth): require proof-of-possession on join 🪪🔐
The device id is derived from the pubkey in JoinRequest, but the hub never
checked the joiner actually held that key — anyone with a token could enroll
claiming another device's identity. Now the client signs JoinChallenge(token,
pubkey) with its ed25519 private key and the hub verifies it (ed25519.Verify)
BEFORE redeeming the token — so a bad request can't burn a legitimate one-time
token either. proto gains a Signature field + the shared JoinChallenge helper.
Regression test: garbage/missing/wrong-key sigs all 401, token survives, valid
join enrolls.
2026-06-23 03:08:23 -05:00
fd361e811e harden(dos): bound request bodies + add hub server timeouts 🚪🛡️
The hub read request bodies with unbounded io.ReadAll / json.Decode and ran
http.ListenAndServe with zero timeouts — a single client could OOM it or hold
connections open (slowloris). Now:
- handleBlob caps the body at 256 MiB, decode() caps JSON at 8 MiB, both via
  http.MaxBytesReader -> 413 when exceeded.
- serve uses an http.Server with ReadHeaderTimeout/ReadTimeout/IdleTimeout +
  MaxHeaderBytes. WriteTimeout intentionally unset so the long-lived /v1/events
  SSE stream isn't killed. [fanned-out subagent: harden-dos]
2026-06-23 02:59:53 -05:00
9ad2a9da74 fix(dataloss): make hub GC safe against cross-share refcount undercount 🩸🗑️
Snapshots are content-addressed with a GLOBAL id, so two shares with identical
content collide on one snapshots row and the idempotent push skips the refcount
bump — a chunk two share heads both need ends up refcount=1. The old GC then
pruned the 'old' snapshot for one share and freed chunks/manifests the OTHER
share's live head still pointed at. Permanent cross-share data loss.

Rework runGC as mark-and-sweep from every share's live snapshots as roots:
nothing reachable from a kept snapshot is ever deleted, whatever the refcounts
say. Never prune a snapshot that is any share's head. Plus hardening:
- DeleteSnapshot floors refcount at 0 and only decrements if the row existed
  (no negative counts from a double-delete).
- DeleteChunkRow is now conditional (WHERE refcount<=0) and reports if it
  deleted, so a push that re-references a chunk mid-GC isn't corrupted.
Cross-share repro test + existing single-share GC test both green.

Deferred to v2 (documented): proper per-(share,id) refcount accounting — needs
a schema migration of the live hub; the ground-truth GC makes it safe meanwhile.
2026-06-23 02:35:43 -05:00
6d0453d8e3 feat(M6): versioning — log, restore, hub gc 🕰️
- devbox log <share>: hub GET /v1/log (meta.SnapshotLog newest-first), client.Log
- devbox restore <share> <snapshot> [path]: snapshot id == manifest blob hash, so
  syncer.Restore fetches the old manifest, rewrites the file(s) (+deletes extras for
  a whole-tree restore), and pushes -> itself a new reversible snapshot
- devbox-hub gc [--keep N]: prune snapshots beyond the newest N (never the head),
  decrement chunk refcounts, sweep refcount<=0 chunks + manifest blobs from the store
- meta: SnapshotLog, ShareNames, DeletableSnapshots (rowid tiebreaker), DeleteSnapshot
  (tx + refcount--), UnreferencedChunks, DeleteChunkRow
- tests: log order/limit, deletable keeps head+newest, refcount decrement, whole-tree
  + single-path restore, gc end-to-end (old unique chunks freed, head intact)
2026-06-23 01:01:54 -05:00
3cfef72a88 harden(M2->M3): parent-aware push + hashed bearers + blob download 🔐
From the M2 ponytail review (the load-bearing findings):
- hub push now enforces parent==head: re-push=idempotent, parent==head=fast-forward,
  else Conflict (never clobbers head) — the foundation M3 conflict resolution needs
- bearers stored as sha256 (like join tokens), no plaintext creds at rest / timing oracle
- GET /v1/blob/{hash} download + client.GetBlob — pull needs it
- randHex propagates crypto/rand errors (no silent low-entropy tokens)
- syncer/publish are head-aware and surface conflicts
- tests: conflict-when-behind (head not clobbered), manifest download+parse

Kept rw-by-default (intentional single-owner v1 per spec §6).
2026-06-22 23:54:55 -05:00
55175fa2ac feat(M2): hub server + transport client + push engine + CLI 🛰️
- internal/hub: HTTP server (join/publish/have/blob/push/head/metrics), bearer auth,
  blob hash-verify, hand-rolled Prometheus /metrics (no dep)
- internal/transport: device HTTP client for the full wire contract
- internal/syncer: device push engine (build manifest -> upload missing blobs ->
  commit snapshot) + .devignore loader; in-process e2e test (join->publish->push)
- meta: idempotent AddSnapshot (content-addressed re-push advances head, no double
  refcount) + busy_timeout for multi-process CLI access
- cmd/devbox-hub: serve/token/readonly/revoke; cmd/devbox: real join + publish/push
- config: persist bearer + device id

Verified end-to-end with real binaries: secret blocked, node_modules ignored,
re-publish dedups to 0 uploads.
2026-06-22 23:40:18 -05:00
79f91597ad feat(M0): skeleton — cobra CLI, Ed25519 identity, machine config 🦴
- cmd/devbox: join/status/version + stubbed start/stop/publish
- cmd/devbox-hub: version + stubbed serve/token
- internal/identity: Ed25519 keypair gen/save/load (0600), fingerprint
- internal/config: daemon.toml read/write (hub + mounts), XDG-aware dir
- tests: identity round-trip/idempotent, config round-trip

devbox join now generates a device identity and records the hub locally.
2026-06-22 23:01:00 -05:00