Skip to content

Commit c6262a8

Browse files
fix(state): only migrate moves committed state
An org is in one layout, decided by where its committed state file is. Pull, push, apply and promote read and write the layout they find, on a person's machine and in CI, and print one line saying the newer one exists. npm run migrate is the only command that moves state, after saying what the commit step has to stage. The commit-step reader fails closed: a git add counts only as a plain command at the repository root that names state or stages everything. With nothing seen, migrate asks for --commit-step-stages-state. Pull no longer reads "edited since the last sync" off the state file's timestamp, which in a fresh clone is the order git wrote the files in. It compares against the moment this machine's engine last saved state, and a clone with no such save keeps every file. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
1 parent 2866322 commit c6262a8

22 files changed

Lines changed: 2389 additions & 1096 deletions

‎.gitignore‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ tmp/
3131
# snapshots/ copies written by `npm run push` for `npm run rollback`.
3232
state/*/local/
3333

34-
# The same two stores where they sat before state/ existed. The engine moves
35-
# them on the next pull, push or apply; until then they stay out of git.
34+
# The same two stores where they sit while a repository's state is still in
35+
# the layout from before state/ existed. For an org whose state is in state/
36+
# the engine moves them on the next pull, push or apply; either way they
37+
# stay out of git.
3638
.vapi-state.*.snapshots/
3739
.vapi-state-hash/
3840

‎docs/guides/commands.md‎

Lines changed: 95 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,13 @@ The test-call CLI cleans its terminal output for the developer loop:
193193

194194
## Upgrading from an older version
195195

196-
`npm run migrate` brings an older repository up to date. It needs no org and
196+
Upgrading the engine does not change where your state is kept. A repository
197+
with `.vapi-state.<org>.json` at its root keeps it there: `pull`, `push`,
198+
`apply` and `promote` read and write it in place, on your machine and in CI,
199+
for as long as you like. Each of those commands prints one line saying the
200+
newer layout exists.
201+
202+
`npm run migrate` is the only command that moves state. It needs no org and
197203
no API key, and it's safe to run more than once:
198204

199205
```bash
@@ -202,10 +208,25 @@ git add -A -- state ':(glob).vapi-state.*.json' resources
202208
git commit -m "chore: move engine state into state/"
203209
```
204210

205-
It does two things.
211+
It does three things.
212+
213+
**Says what your commit step has to stage, before it moves anything.** A
214+
promotion or apply run in CI writes the state file and leaves committing it
215+
to a later step. If that step stages only the old file, moved state drops out
216+
of git: every run still passes, and the next one creates every resource
217+
again. Once state is in `state/`, whatever commits state after a run has to
218+
stage both of these:
206219

207-
**Moves engine files into `state/<org>/`.** They used to sit under dot-names
208-
in three places. Nothing changes inside the files.
220+
```bash
221+
git add -A -- state # the new folder
222+
git add -A -- ':(glob).vapi-state.*.json' # the old files, so their removal is committed
223+
```
224+
225+
A commit action or a script needs the same two paths. Upstream's
226+
`.github/workflows/promotion.yml` has both.
227+
228+
**Moves engine files into `state/<org>/`,** when it can see that step or you
229+
tell it. Nothing changes inside the files.
209230

210231
| Before | Now |
211232
| --- | --- |
@@ -214,38 +235,17 @@ in three places. Nothing changes inside the files.
214235
| `.vapi-state-hash/<org>/` | `state/<org>/local/baselines/` |
215236
| `.vapi-state.<org>.snapshots/` | `state/<org>/local/snapshots/` |
216237

217-
You don't have to run it for this: the engine keeps reading the old places,
218-
and the next `pull`, `push` or `apply` on your machine moves that org and
219-
says so in one line. Either way, commit the removal of the old file together
220-
with the new folder.
221-
222-
**State moves only when the engine can see it will still be committed.** A
223-
promotion run in CI writes the state file and leaves committing it to a later
224-
step. If that step stages only the old file, a move takes the UUID map out of
225-
git: every run still passes, and the next one creates every resource again.
226-
So the move is held back in three cases, and the command tells you which:
227-
228-
- **In a CI run.** State stays where it is and is updated in place. Run
229-
`npm run migrate` on your machine and commit the result.
230-
- **When a workflow under `.github/workflows/` stages `.vapi-state.*.json` but
231-
not `state/`.** That workflow would commit the old file's removal without
232-
the new one. Take upstream's `promotion.yml` workflow, or add
233-
`git add -A -- state` to your own commit step, then run `npm run migrate`.
234-
- **When something may commit state and the engine can't see it stage
235-
`state/`.** That is a workflow that can write to the repository and has no
236-
`git add` line for `state` (it stages through a script, a commit action or
237-
a variable), or a root `promotion.yml` with no workflow in the repository
238-
that stages `state/` (the pipeline runs somewhere the engine can't read).
239-
`npm run migrate` then moves nothing and prints what the commit step has to
240-
stage:
241-
242-
```bash
243-
git add -A -- state # the new folder
244-
git add -A -- ':(glob).vapi-state.*.json' # the old files, so their removal is committed
245-
```
238+
What it does depends on what it reads in `.github/workflows/`:
246239

247-
A commit action or a script needs the same two paths. When your step
248-
stages both, say so:
240+
- **Every commit step there stages `state/`.** It moves every org.
241+
- **A step stages `.vapi-state.*.json` and nothing that could be `state/`.**
242+
It moves nothing and names the workflow. Take upstream's `promotion.yml`
243+
workflow, or add `git add -A -- state` to your own commit step, then run
244+
`npm run migrate` again.
245+
- **Anything else.** A step that stages through a script, a commit action or
246+
a variable, a repository with no workflow, or a pipeline in another CI
247+
system. It moves nothing and asks you. When your step stages both paths,
248+
say so:
249249

250250
```bash
251251
npm run migrate -- --commit-step-stages-state
@@ -254,49 +254,87 @@ So the move is held back in three cases, and the command tells you which:
254254
That writes `state/commit-step.json` and moves every org. Commit it with
255255
the move.
256256

257-
What counts as seen: a `git add` line in a workflow file that names `state`
258-
(or `state/`, `state/**`), or one that stages everything (`git add -A`,
259-
`git add .`). A workflow whose `permissions` keep `contents` read-only, and
260-
that never runs `git commit` or `git push`, is not a commit step, so
261-
upstream's `ci.yml` and `vapi-checks.yml` hold nothing back.
262-
263-
A CI run also stops, before it sends any request, when it would write state
264-
in a place its commit step isn't seen to stage: state already in `state/`
265-
under a step that stages only the old file or can't be read, or state still
266-
at the root under a step that stages only `state/`. In GitHub Actions the
267-
engine checks the workflow that is running it. Anywhere else it checks every
268-
workflow in the repository.
269-
270257
> **`--commit-step-stages-state` is your word, and the engine can't check
271258
> it.** If you pass it and your commit step does not stage `state/`,
272259
> promotion runs pass while `state/<org>/state.json` is never committed, and
273260
> a later run creates the same resources again. The flag is refused only when
274261
> a workflow is plainly seen to stage the old file alone. To take it back,
275262
> delete `state/commit-step.json`.
276263
277-
If both the old and the new file exist for an org and they disagree, every
278-
writing command stops before it changes anything and names the two files.
279-
280264
**Rewrites a legacy-format state file.** Repositories created before the
281265
state-file format changed carry per-resource hashes in the state file, and
282266
`pull`, `push` and `apply` refuse to run until they're gone. `migrate`
283267
rewrites each file to the current `{ "name": { "uuid": … } }` format and
284268
seeds this machine's drift baselines from the old file.
285269

270+
### What the engine reads as a commit step
271+
272+
The engine reads the workflow files in `.github/workflows/` and nothing else.
273+
It counts a `git add` as staging `state/` only where it can tell what the
274+
command stages:
275+
276+
- It is a command of its own in a `run:` step that runs `bash` or `sh` at
277+
the repository root.
278+
- It names `state` (also `state/` or `state/**`), or it stages everything:
279+
`git add -A`, `git add --all`, `git add .`.
280+
- An `if` around it is fine when it names `state`, because that is how a step
281+
guards a folder that may not exist yet.
282+
283+
Everything else counts for nothing: a `git add` after a `cd`, in a step with
284+
a `working-directory`, after `||`, inside a loop, a function or a quoted
285+
string, with `-u`, with a variable or an exclusion in its paths, or a
286+
`git add -A` inside an `if`. A step like that may well work. The engine just
287+
can't tell, so `migrate` asks for the flag.
288+
289+
Every job that shows a commit step has to pass, in every workflow file. A job
290+
shows none when its `permissions` keep `contents` read-only and it runs no
291+
`git add`, `git commit` or `git push`, so upstream's `ci.yml` and
292+
`vapi-checks.yml` don't get in the way.
293+
294+
A pipeline the engine has no file for is never read: another CI system, or a
295+
script that commits with its own token. Check it yourself against the two
296+
lines above before you run `migrate`.
297+
298+
### When a CI run stops
299+
300+
A CI run stops, before it sends any request, when the workflow running it is
301+
seen not to commit what it would write:
302+
303+
- State is in `state/`, and the commit step stages only the old file.
304+
- State is in `state/`, the commit step can't be read, and there is no
305+
`state/commit-step.json`.
306+
- State is still at the root, and the commit step stages only `state/`.
307+
308+
In GitHub Actions the engine checks the workflow that is running it. Anywhere
309+
else it checks every workflow in the repository.
310+
311+
If both the old and the new file exist for an org and they disagree, every
312+
writing command stops before it changes anything and names the two files. If
313+
they hold the same content, the engine reads the one in `state/`, keeps the
314+
old copy identical, and says that `npm run migrate` removes it.
315+
286316
### What you'll see after upgrading
287317

288-
- **`📦 Moved … into state/<org>/`**, once per org, on the first `pull`,
289-
`push` or `apply` on your machine.
290-
- **`📦 1 locally modified file(s) will be preserved`** on the first `pull`
291-
after the move. The file is the old org pin,
318+
- **`ℹ️ Engine state here is in the older layout …`**, one line per writing
319+
command, until you run `npm run migrate`. Nothing else changes.
320+
321+
After `npm run migrate`:
322+
323+
- **`📦 Moved … into state/<org>/`**, once per org, from `migrate` itself.
324+
- **`📦 1 locally modified file(s) will be preserved`** on a `pull` before you
325+
commit the move. The file is the old org pin,
292326
`resources/<org>/.vapi-org.json`. It moved to `state/<org>/org.json`, so git
293327
shows it as deleted until you commit. Pull does nothing differently because
294328
of it.
295-
- **`… stays where it is: …`** on every writing command while the move is
296-
held back. The line says why.
329+
- **`📦 Moved .vapi-state-hash/<org>/ … into state/<org>/local/`**, once, on
330+
each teammate's machine after they pull your commit. Those are that
331+
machine's own baselines and snapshots, which are not in git.
297332

298333
### Going back to an older engine
299334

335+
A repository that never ran `npm run migrate` has nothing to move back: its
336+
state is where an older engine looks for it.
337+
300338
An engine from before `state/` doesn't read the folder. If you revert the
301339
engine after state has moved, it finds no state file: `pull` adopts every
302340
resource again under a new file name, and promotion creates each resource a

‎docs/guides/workflows.md‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,13 @@ fresh-clone workflows:
169169
2. **Git-tracked changes** — when no baseline exists yet, files that show up
170170
in `git status` (modified, deleted, or individually untracked) are
171171
preserved.
172-
3. **mtime fallback** — if git can't help (no commits yet, the resource tree
173-
isn't tracked at all, or git just had nothing to say), files that are
174-
newer than `state/<org>/state.json` are still preserved. This is the safety
175-
net for the "fresh clone, edit a file, run pull again" case.
172+
3. **Last-save fallback** — if git can't help (no commits yet, the resource
173+
tree isn't tracked at all, or git just had nothing to say), a file is
174+
still preserved unless the engine has saved this org's state on this
175+
machine since the file was last written. A fresh clone has no such save
176+
yet, so its first pull keeps every file: a change merged in git and not
177+
yet on the platform is not overwritten, whatever order the checkout wrote
178+
the files in.
176179

177180
Interactive `npm run pull` is local-first too: it asks
178181
`Overwrite locally modified files?` (default `No`). Pass `--force` (or answer

‎docs/learnings/sync-behavior.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ sweep, which consume the resources today without consuming the verdict.
194194

195195
| Command | Behavior |
196196
|---|---|
197-
| `pull` | direction unknown (`no-baseline`) → falls back to git/mtime modified-detection: modified files ✏️ preserved; unmodified files 📝 rewritten — and the baseline is seeded. **Run a plain pull first on any fresh clone.** |
197+
| `pull` | direction unknown (`no-baseline`) → falls back to git modified-detection, then to the last-save rule: a file is ✏️ preserved unless the engine has saved this org's state on this machine since the file was written. A fresh clone has no save yet, so its first pull preserves every file; after that, an untouched file is 📝 rewritten and its baseline seeded. **Run a plain pull first on any fresh clone.** |
198198
| `push` (without pulling first) | drift check skipped with a warning, PATCH proceeds, baseline seeded from the response. Risk window: this one push cannot detect dashboard drift. |
199199
| `audit` | `no-baseline` info findings (do not block CI) |
200200

‎improvements.md‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,23 +2123,40 @@ snapshots. `state/README.md` says the engine writes it and what breaks when a
21232123
file goes missing. `resources/<org>/` now holds hand-written files only.
21242124
`src/state-paths.ts` is the one module that knows both layouts.
21252125

2126-
An older repository keeps working: every command reads the old places, and
2127-
the next `pull`, `push` or `apply` on a person's machine moves the org with
2128-
one rename per file and prints one line. The move fails closed: it happens
2129-
only when a workflow is seen to stage `state/`, or when nothing in the
2130-
repository commits state after a run. It is held back in a CI run, while a
2131-
workflow stages `.vapi-state.*.json` but not `state/`, and while a commit
2132-
step can't be read (a script, a commit action, a pipeline outside GitHub
2133-
Actions) until a person says it stages `state/` with
2134-
`npm run migrate -- --commit-step-stages-state`. A CI run stops before any
2135-
request when it would write state where its commit step isn't seen to look.
2136-
The promotion workflow stages both places. `tests/state-layout.test.ts`,
2137-
`tests/state-layout-unseen-commit.test.ts` and
2126+
An older repository keeps working exactly as it did: an org is in one layout,
2127+
decided by where its committed state file is, and every command reads and
2128+
writes the layout it finds, on a person's machine and in CI. No command moves
2129+
committed state as a side effect. Only `npm run migrate` does, with one
2130+
rename per file, after saying what the step that commits state after a run
2131+
has to stage. It moves state when every commit step in `.github/workflows/`
2132+
is seen to stage `state/`. It moves nothing while a workflow stages
2133+
`.vapi-state.*.json` and nothing that could be `state/`, or while no commit
2134+
step can be read (a script, a commit action, no workflow, a pipeline outside
2135+
GitHub Actions), until a person says theirs stages `state/` with
2136+
`npm run migrate -- --commit-step-stages-state`.
2137+
2138+
The reader of commit steps (`src/commit-step.ts`) fails closed: a `git add`
2139+
counts only as a plain command at the repository root that names `state` or
2140+
stages everything. A CI run stops before any request when the workflow
2141+
running it is seen not to commit what it would write. The promotion workflow
2142+
stages both places. `tests/state-layout.test.ts`,
2143+
`tests/state-layout-unseen-commit.test.ts`,
2144+
`tests/state-move-second-round.test.ts` and
21382145
`tests/promotion-workflow-commit.test.ts` cover each path.
21392146

2140-
One limit remains. The flag is a person's word. If it is given and the commit
2147+
Pull had one rule that depended on where the state file sat. With no baseline
2148+
and nothing changed in git it kept a file only when the file was newer than
2149+
the state file, and in a fresh clone that is the order git wrote the files
2150+
in: every file was kept with state at the root, and every file would have
2151+
been overwritten with state in `state/`. Pull now compares against the moment
2152+
this machine's engine last saved state, kept beside the baselines, and a
2153+
clone with no such save keeps every file.
2154+
2155+
Two limits remain. The flag is a person's word: if it is given and the commit
21412156
step does not stage `state/`, promotion runs pass while state is never
2142-
committed. The engine can't see a step it can't read.
2157+
committed. And the engine reads `.github/workflows/` only: a pipeline it has
2158+
no file for is not read, which is why `migrate` always prints the two lines
2159+
first.
21432160

21442161
Two things stay where they were, on purpose. Dashboard backups
21452162
(`<name>.<timestamp>.bkp.md`) stay beside the resource they shadow: a person

‎src/apply.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { dirname, join, resolve } from "path";
33
import { fileURLToPath } from "url";
44
import { parseAdoptArgs } from "./adopt.ts";
55
import { assertStateMigrated } from "./migrate-hash-store.ts";
6-
import { stateFileRead, stateLayoutPrepare } from "./state-paths.ts";
6+
import {
7+
stateFileRead,
8+
stateLayoutPrepare,
9+
stateNoticesPrint,
10+
} from "./state-paths.ts";
711
// Pins this command's User-Agent label for the pull and push it spawns.
812
import "./user-agent.ts";
913

@@ -42,11 +46,12 @@ export async function runApply(): Promise<void> {
4246
const hasForce = allArgs.includes("--force");
4347
const dryRun = allArgs.includes("--dry-run");
4448

45-
// Move an old-layout state file into state/<org>/ once, here, and fail fast
46-
// with one clear message if it is still in the legacy format, rather than
47-
// letting the spawned pull/push subprocess surface either later.
49+
// Check the org's state layout once, here, and fail fast with one clear
50+
// message if the state file is still in the legacy format, rather than
51+
// letting the spawned pull/push subprocess surface either later. What is
52+
// printed here is not printed again by the pull and push below.
4853
if (env && SLUG_RE.test(env)) {
49-
for (const line of stateLayoutPrepare(BASE_DIR, env)) console.log(line);
54+
stateNoticesPrint(stateLayoutPrepare(BASE_DIR, env));
5055
assertStateMigrated(stateFileRead(BASE_DIR, env));
5156
}
5257

0 commit comments

Comments
 (0)