Skip to content

Commit 3b79a6b

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 9f57e71 commit 3b79a6b

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
@@ -135,15 +135,60 @@ and the next `pull`, `push` or `apply` on your machine moves that org and
135135
says so in one line. Either way, commit the removal of the old file together
136136
with the new folder.
137137

138-
The move is held back in two cases, and the command tells you which:
138+
**State moves only when the engine can see it will still be committed.** A
139+
promotion run in CI writes the state file and leaves committing it to a later
140+
step. If that step stages only the old file, a move takes the UUID map out of
141+
git: every run still passes, and the next one creates every resource again.
142+
So the move is held back in three cases, and the command tells you which:
139143

140-
- **In a CI run.** State stays where it is and is updated in place. Only a
141-
workflow that commits both places can record a move, so run
144+
- **In a CI run.** State stays where it is and is updated in place. Run
142145
`npm run migrate` on your machine and commit the result.
143146
- **When a workflow under `.github/workflows/` stages `.vapi-state.*.json` but
144147
not `state/`.** That workflow would commit the old file's removal without
145148
the new one. Take upstream's `promotion.yml` workflow, or add
146149
`git add -A -- state` to your own commit step, then run `npm run migrate`.
150+
- **When something may commit state and the engine can't see it stage
151+
`state/`.** That is a workflow that can write to the repository and has no
152+
`git add` line for `state` (it stages through a script, a commit action or
153+
a variable), or a root `promotion.yml` with no workflow in the repository
154+
that stages `state/` (the pipeline runs somewhere the engine can't read).
155+
`npm run migrate` then moves nothing and prints what the commit step has to
156+
stage:
157+
158+
```bash
159+
git add -A -- state # the new folder
160+
git add -A -- ':(glob).vapi-state.*.json' # the old files, so their removal is committed
161+
```
162+
163+
A commit action or a script needs the same two paths. When your step
164+
stages both, say so:
165+
166+
```bash
167+
npm run migrate -- --commit-step-stages-state
168+
```
169+
170+
That writes `state/commit-step.json` and moves every org. Commit it with
171+
the move.
172+
173+
What counts as seen: a `git add` line in a workflow file that names `state`
174+
(or `state/`, `state/**`), or one that stages everything (`git add -A`,
175+
`git add .`). A workflow whose `permissions` keep `contents` read-only, and
176+
that never runs `git commit` or `git push`, is not a commit step, so
177+
upstream's `ci.yml` and `vapi-checks.yml` hold nothing back.
178+
179+
A CI run also stops, before it sends any request, when it would write state
180+
in a place its commit step isn't seen to stage: state already in `state/`
181+
under a step that stages only the old file or can't be read, or state still
182+
at the root under a step that stages only `state/`. In GitHub Actions the
183+
engine checks the workflow that is running it. Anywhere else it checks every
184+
workflow in the repository.
185+
186+
> **`--commit-step-stages-state` is your word, and the engine can't check
187+
> it.** If you pass it and your commit step does not stage `state/`,
188+
> promotion runs pass while `state/<org>/state.json` is never committed, and
189+
> a later run creates the same resources again. The flag is refused only when
190+
> a workflow is plainly seen to stage the old file alone. To take it back,
191+
> delete `state/commit-step.json`.
147192
148193
If both the old and the new file exist for an org and they disagree, every
149194
writing command stops before it changes anything and names the two files.
@@ -153,3 +198,40 @@ state-file format changed carry per-resource hashes in the state file, and
153198
`pull`, `push` and `apply` refuse to run until they're gone. `migrate`
154199
rewrites each file to the current `{ "name": { "uuid": … } }` format and
155200
seeds this machine's drift baselines from the old file.
201+
202+
### What you'll see after upgrading
203+
204+
- **`📦 Moved … into state/<org>/`**, once per org, on the first `pull`,
205+
`push` or `apply` on your machine.
206+
- **`📦 1 locally modified file(s) will be preserved`** on the first `pull`
207+
after the move. The file is the old org pin,
208+
`resources/<org>/.vapi-org.json`. It moved to `state/<org>/org.json`, so git
209+
shows it as deleted until you commit. Pull does nothing differently because
210+
of it.
211+
- **`… stays where it is: …`** on every writing command while the move is
212+
held back. The line says why.
213+
214+
### Going back to an older engine
215+
216+
An engine from before `state/` doesn't read the folder. If you revert the
217+
engine after state has moved, it finds no state file: `pull` adopts every
218+
resource again under a new file name, and promotion creates each resource a
219+
second time. Move the state files back first, in the same commit as the
220+
revert:
221+
222+
```bash
223+
for dir in state/*/; do
224+
org=$(basename "$dir")
225+
if [ -f "state/$org/state.json" ]; then mv "state/$org/state.json" ".vapi-state.$org.json"; fi
226+
if [ -f "state/$org/org.json" ]; then mv "state/$org/org.json" "resources/$org/.vapi-org.json"; fi
227+
# This machine's baselines and snapshots. They are not in git, so everyone
228+
# who has pulled or pushed since the move runs these two on their machine.
229+
if [ -d "state/$org/local/baselines" ]; then mkdir -p .vapi-state-hash && mv "state/$org/local/baselines" ".vapi-state-hash/$org"; fi
230+
if [ -d "state/$org/local/snapshots" ]; then mv "state/$org/local/snapshots" ".vapi-state.$org.snapshots"; fi
231+
done
232+
git add -A -- state ':(glob).vapi-state.*.json' resources
233+
```
234+
235+
Then revert the engine and commit. The older engine ignores whatever is left
236+
under `state/`. A commit step that was changed to stage only `state/` has to
237+
stage `.vapi-state.*.json` again.

‎improvements.md‎

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

21072107
An older repository keeps working: every command reads the old places, and
21082108
the next `pull`, `push` or `apply` on a person's machine moves the org with
2109-
one rename per file and prints one line. The move is held back in a CI run
2110-
and while a workflow stages `.vapi-state.*.json` but not `state/`, because
2111-
either would commit the old file's removal without the new file. The
2112-
promotion workflow stages both places. `tests/state-layout.test.ts` and
2109+
one rename per file and prints one line. The move fails closed: it happens
2110+
only when a workflow is seen to stage `state/`, or when nothing in the
2111+
repository commits state after a run. It is held back in a CI run, while a
2112+
workflow stages `.vapi-state.*.json` but not `state/`, and while a commit
2113+
step can't be read (a script, a commit action, a pipeline outside GitHub
2114+
Actions) until a person says it stages `state/` with
2115+
`npm run migrate -- --commit-step-stages-state`. A CI run stops before any
2116+
request when it would write state where its commit step isn't seen to look.
2117+
The promotion workflow stages both places. `tests/state-layout.test.ts`,
2118+
`tests/state-layout-unseen-commit.test.ts` and
21132119
`tests/promotion-workflow-commit.test.ts` cover each path.
21142120

2121+
One limit remains. The flag is a person's word. If it is given and the commit
2122+
step does not stage `state/`, promotion runs pass while state is never
2123+
committed. The engine can't see a step it can't read.
2124+
21152125
Two things stay where they were, on purpose. Dashboard backups
21162126
(`<name>.<timestamp>.bkp.md`) stay beside the resource they shadow: a person
21172127
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,
@@ -39,6 +41,15 @@ export interface OrgMigrationResult {
3941

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

4455
// A legacy entry is anything that isn't already exactly `{ uuid: string }`:
@@ -133,8 +144,8 @@ async function migrateOne(
133144

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

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

0 commit comments

Comments
 (0)