Skip to content

Commit 2866322

Browse files
fix(state): move state only under a commit step seen to stage state/
The guard that holds the move back looked for one known-bad pattern: a "git add" line naming .vapi-state. with none naming state. A commit step it couldn't read (a helper script, a commit action with a file pattern, a pipeline outside GitHub Actions) passed as safe. A command on a person's machine then moved state, and every later promotion run passed while state/<org>/state.json was never committed, so resources were created again as duplicates. The move now fails closed. State moves by itself only when a workflow is seen to stage state/, or when nothing in the repository commits state after a run: no workflow that can write to the repository, and no root promotion.yml. A CI run stops before any request when it would write state in a place its commit step isn't seen to stage. In GitHub Actions it reads the workflow that is running it. npm run migrate, where nothing is seen to stage state/, prints the two paths the commit step has to stage and moves nothing. A person who has updated their step says so with --commit-step-stages-state, which is recorded in state/commit-step.json. The engine can't check that word. Docs: what counts as seen, the limit of the flag, what a customer sees after upgrading, and how to go back to an older engine without creating duplicates. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
1 parent b0352d2 commit 2866322

8 files changed

Lines changed: 1075 additions & 45 deletions

File tree

‎docs/guides/commands.md‎

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,60 @@ and the next `pull`, `push` or `apply` on your machine moves that org and
219219
says so in one line. Either way, commit the removal of the old file together
220220
with the new folder.
221221

222-
The move is held back in two cases, and the command tells you which:
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:
223227

224-
- **In a CI run.** State stays where it is and is updated in place. Only a
225-
workflow that commits both places can record a move, so run
228+
- **In a CI run.** State stays where it is and is updated in place. Run
226229
`npm run migrate` on your machine and commit the result.
227230
- **When a workflow under `.github/workflows/` stages `.vapi-state.*.json` but
228231
not `state/`.** That workflow would commit the old file's removal without
229232
the new one. Take upstream's `promotion.yml` workflow, or add
230233
`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+
```
246+
247+
A commit action or a script needs the same two paths. When your step
248+
stages both, say so:
249+
250+
```bash
251+
npm run migrate -- --commit-step-stages-state
252+
```
253+
254+
That writes `state/commit-step.json` and moves every org. Commit it with
255+
the move.
256+
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+
270+
> **`--commit-step-stages-state` is your word, and the engine can't check
271+
> it.** If you pass it and your commit step does not stage `state/`,
272+
> promotion runs pass while `state/<org>/state.json` is never committed, and
273+
> a later run creates the same resources again. The flag is refused only when
274+
> a workflow is plainly seen to stage the old file alone. To take it back,
275+
> delete `state/commit-step.json`.
231276
232277
If both the old and the new file exist for an org and they disagree, every
233278
writing command stops before it changes anything and names the two files.
@@ -237,3 +282,40 @@ state-file format changed carry per-resource hashes in the state file, and
237282
`pull`, `push` and `apply` refuse to run until they're gone. `migrate`
238283
rewrites each file to the current `{ "name": { "uuid": … } }` format and
239284
seeds this machine's drift baselines from the old file.
285+
286+
### What you'll see after upgrading
287+
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,
292+
`resources/<org>/.vapi-org.json`. It moved to `state/<org>/org.json`, so git
293+
shows it as deleted until you commit. Pull does nothing differently because
294+
of it.
295+
- **`… stays where it is: …`** on every writing command while the move is
296+
held back. The line says why.
297+
298+
### Going back to an older engine
299+
300+
An engine from before `state/` doesn't read the folder. If you revert the
301+
engine after state has moved, it finds no state file: `pull` adopts every
302+
resource again under a new file name, and promotion creates each resource a
303+
second time. Move the state files back first, in the same commit as the
304+
revert:
305+
306+
```bash
307+
for dir in state/*/; do
308+
org=$(basename "$dir")
309+
if [ -f "state/$org/state.json" ]; then mv "state/$org/state.json" ".vapi-state.$org.json"; fi
310+
if [ -f "state/$org/org.json" ]; then mv "state/$org/org.json" "resources/$org/.vapi-org.json"; fi
311+
# This machine's baselines and snapshots. They are not in git, so everyone
312+
# who has pulled or pushed since the move runs these two on their machine.
313+
if [ -d "state/$org/local/baselines" ]; then mkdir -p .vapi-state-hash && mv "state/$org/local/baselines" ".vapi-state-hash/$org"; fi
314+
if [ -d "state/$org/local/snapshots" ]; then mv "state/$org/local/snapshots" ".vapi-state.$org.snapshots"; fi
315+
done
316+
git add -A -- state ':(glob).vapi-state.*.json' resources
317+
```
318+
319+
Then revert the engine and commit. The older engine ignores whatever is left
320+
under `state/`. A commit step that was changed to stage only `state/` has to
321+
stage `.vapi-state.*.json` again.

‎improvements.md‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,12 +2125,22 @@ file goes missing. `resources/<org>/` now holds hand-written files only.
21252125

21262126
An older repository keeps working: every command reads the old places, and
21272127
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 is held back in a CI run
2129-
and while a workflow stages `.vapi-state.*.json` but not `state/`, because
2130-
either would commit the old file's removal without the new file. The
2131-
promotion workflow stages both places. `tests/state-layout.test.ts` and
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
21322138
`tests/promotion-workflow-commit.test.ts` cover each path.
21332139

2140+
One limit remains. The flag is a person's word. If it is given and the commit
2141+
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.
2143+
21342144
Two things stay where they were, on purpose. Dashboard backups
21352145
(`<name>.<timestamp>.bkp.md`) stay beside the resource they shadow: a person
21362146
opens them next to the file to merge by hand, nothing reads them back, and

‎src/migrate-cmd.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
// does not import `config.ts` (which would force an org + token at load).
88
//
99
// Idempotent: re-running after migration seeds nothing and rewrites nothing.
10+
//
11+
// `--commit-step-stages-state` is a person's word that whatever commits state
12+
// after a run stages state/. It is only needed where the engine can't see
13+
// that for itself, and without it nothing moves there.
1014

1115
import { resolve } from "path";
1216
import { fileURLToPath } from "url";
1317
import { migrateAll } from "./migrate-hash-store.ts";
18+
import { COMMIT_STEP_FLAG } from "./state-paths.ts";
1419

1520
async function main(): Promise<void> {
1621
console.log(
@@ -21,7 +26,17 @@ async function main(): Promise<void> {
2126
"═══════════════════════════════════════════════════════════════\n",
2227
);
2328

24-
const result = await migrateAll();
29+
const args = process.argv.slice(2);
30+
const unknown = args.filter((arg) => arg !== COMMIT_STEP_FLAG);
31+
if (unknown.length > 0)
32+
throw new Error(
33+
`Unrecognized argument: ${unknown.join(" ")}. The only option is ${COMMIT_STEP_FLAG}.`,
34+
);
35+
36+
const result = await migrateAll(undefined, {
37+
commitStepStagesState: args.includes(COMMIT_STEP_FLAG),
38+
});
39+
if (result.acknowledged) console.log(` ${result.acknowledged}\n`);
2540

2641
if (result.orgs.length === 0) {
2742
console.log(" No state files found — nothing to migrate.");
@@ -46,6 +61,7 @@ async function main(): Promise<void> {
4661
`\n✅ Migration complete: ${result.orgs.length} org(s), ` +
4762
`${totalSlimmed} state file(s) slimmed, ${totalSeeded} baseline(s) seeded.`,
4863
);
64+
if (result.commitStepAsk) console.log(`\n${result.commitStepAsk.join("\n")}`);
4965
}
5066

5167
const isMainModule =

‎src/migrate-hash-store.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
writeBaseline,
2020
} from "./hash-store.ts";
2121
import {
22+
commitStepAcknowledge,
23+
commitStepAsk,
2224
stateFileRead,
2325
stateLayoutPrepare,
2426
stateOrgsList,
@@ -40,6 +42,15 @@ export interface OrgMigrationResult {
4042

4143
export interface MigrationResult {
4244
orgs: OrgMigrationResult[];
45+
/** What was written down when the person said their commit step stages state/. */
46+
acknowledged?: string;
47+
/** Set when nothing moved: what the commit step has to stage first. */
48+
commitStepAsk?: string[];
49+
}
50+
51+
export interface MigrationOptions {
52+
/** The person says the step that commits state stages state/ (`--commit-step-stages-state`). */
53+
commitStepStagesState?: boolean;
4354
}
4455

4556
// A legacy entry is anything that isn't already exactly `{ uuid: string }`:
@@ -134,8 +145,8 @@ async function migrateOne(
134145

135146
// Slim first, in place, then move: each step leaves one complete state
136147
// file. An explicit migrate moves state even in a CI run, because whoever
137-
// runs it there commits the result; a workflow that would drop state/ from
138-
// its commit still stops the move.
148+
// runs it there commits the result; a commit step that would drop state/,
149+
// or isn't seen to stage it, still stops the move.
139150
const notices = stateLayoutPrepare(baseDir, org, {
140151
...process.env,
141152
CI: "",
@@ -155,14 +166,21 @@ async function migrateOne(
155166
// from legacy hashes, rewrite the file to slim form, then move the org's
156167
// engine files into `state/<org>/`. Idempotent — re-running on a migrated
157168
// repo seeds nothing, rewrites nothing and moves nothing.
169+
//
170+
// Where nothing is seen to stage state/ after a run, files are slimmed in
171+
// place and nothing moves until the person says their commit step stages it.
158172
export async function migrateAll(
159173
baseDir: string = BASE_DIR,
174+
options: MigrationOptions = {},
160175
): Promise<MigrationResult> {
176+
const acknowledged = options.commitStepStagesState
177+
? commitStepAcknowledge(baseDir)
178+
: undefined;
161179
const orgs: OrgMigrationResult[] = [];
162180
for (const org of stateOrgsList(baseDir)) {
163181
orgs.push(await migrateOne(baseDir, org));
164182
}
165-
return { orgs };
183+
return { orgs, acknowledged, commitStepAsk: commitStepAsk(baseDir) };
166184
}
167185

168186
// Guard called at the entry of pull/push/apply. Throws if the given state file

0 commit comments

Comments
 (0)