You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor: state schema with per-resource content hashes (#19)
## ELI5
**Problem.** The state file (`.vapi-state.<env>.json`) used to map
*name → UUID* and nothing else. So when push went to update a resource,
the engine had no way to tell whether someone had edited the resource
on the dashboard since you last pulled — there was nothing to compare
to. This is the root cause of "drift detection isn't possible," "real
rollback isn't possible," and "scoped pushes can't be precise about
what they touched": the engine has no per-resource memory of *what
was there before*.
**What this fix does.** Widens each state entry from a bare `string`
(the UUID) to a `ResourceState` object carrying:
- `uuid` — the platform UUID (unchanged semantics)
- `lastPulledHash` — sha256 of the platform payload at last pull
- `lastPulledAt` — ISO timestamp
- `lastPushedHash` — sha256 of the last pushed payload
- `platformVersionId` — Stack I, populated when platform exposes one
Every state-reading and state-writing call site is updated. **No new
external behavior ships in this PR alone** — strictly plumbing.
Backwards compatible: legacy state files (the old `string` shape) load
fine, just without hashes until the next pull/push populates them. The
on-disk file isn't rewritten until the next `saveState`, so a "deploy
and immediately rollback" doesn't corrupt state.
**Outcome you'll notice.** This PR alone changes nothing visible. It's
the architectural foundation that **drift detection (Stack G), snapshot
rollback (Stack H), and scoped state writes (Stack J)** all depend on.
After it lands, your next pull populates `lastPulledHash` for every
resource, and the next three PRs unlock real safety guarantees.
---
Architectural pivot. State sections move from Record<string, string>
(name → UUID) to Record<string, ResourceState> carrying:
- uuid: string (the platform UUID, unchanged semantics)
- lastPulledHash?: string (sha256 of canonicalized platform payload)
- lastPulledAt?: string (ISO timestamp)
- lastPushedHash?: string (sha256 of last pushed payload)
- platformVersionId?: string (Stack I — populated when platform exposes one)
This is the architectural prerequisite for drift detection (Stack G),
snapshot rollback (Stack H), optimistic concurrency (Stack I), and scoped
state writes (Stack J). Every state-reading call site is updated, but
NO new external behavior ships in this PR — strictly plumbing.
Backwards compatibility:
- src/state.ts:loadState wraps any legacy bare-string value as
{ uuid: <string> } at load time. Existing customer state files keep
working until their next pull populates hashes. No flag-day migration.
- The on-disk file is NOT rewritten until the next saveState, so a
"deploy and immediately rollback" scenario does NOT corrupt state.
Files:
- src/types.ts: ResourceState type, StateFile sections retyped.
- src/state-serialize.ts: hashPayload (canonicalize + sha256),
asResourceState (legacy migration), upsertState (preserves un-touched
fields when patching).
- src/state.ts: stateUuid helper for the common case;
loadState wraps legacy string entries via migrateSection;
re-exports the helpers for ergonomics.
- src/pull.ts: each pull populates lastPulledHash + lastPulledAt;
credential entries preserve prior metadata when slug+uuid are stable.
- src/push.ts: each PATCH/POST populates lastPushedHash via upsertState.
All `state.X[id]` reads → `?.uuid`. State assignments → upsertState.
- src/cleanup.ts, src/credentials.ts, src/delete.ts, src/eval.ts,
src/resolver.ts, src/call.ts: mechanical updates for the new shape.
Verified by tsc — no leaks where a bare string is still expected.
- tests/state-migration.test.ts: legacy string entries load and
round-trip; mixed legacy + new entries; canonicalize stability;
hashPayload determinism; upsertState preservation semantics.
Closes improvements.md #4 (architectural prerequisite). G/H/I/J unblocked.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
0 commit comments