Skip to content

Commit 88bf713

Browse files
feat(state): keep engine state in state/<org>/
Everything the engine generates and tracks sat under dot-names in three places: .vapi-state.<org>.json, .vapi-state.<org>.snapshots/ and .vapi-state-hash/ at the repository root, and .vapi-org.json inside resources/<org>/. It now lives in state/<org>/: state.json and org.json are committed, local/baselines and local/snapshots stay on the machine. An older repository keeps working. Every command reads the old places. The next pull, push or apply on a person's machine moves the org, one rename per file, and prints one line. The move is held back in a CI run and while a workflow stages .vapi-state.*.json but not state/, because either would commit the old file's removal without the new file. The promotion workflow stages both places. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
1 parent a820577 commit 88bf713

56 files changed

Lines changed: 1516 additions & 197 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.conductor/setup.sh‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,15 @@ done
9898
shopt -u nullglob
9999

100100
# --- 4. Copy state files ----------------------------------------------------
101-
# .vapi-state.<org>.json is tracked in git (worktrees inherit it automatically),
102-
# but copy any untracked variants too. `npm run pull` rewrites these locally —
103-
# copying lets a worktree experiment without touching root.
101+
# state/<org>/ is tracked in git (worktrees inherit it automatically), but
102+
# copy any untracked variants too, and the root-level .vapi-state.<org>.json
103+
# of a repository that has not moved its state yet. `npm run pull` rewrites
104+
# these locally — copying lets a worktree experiment without touching root.
104105
log "Copying Vapi GitOps state files from root checkout"
105106
shopt -s nullglob
106-
for state_file in "$ROOT"/.vapi-state*.json; do
107+
for state_file in "$ROOT"/state/*/state.json "$ROOT"/state/*/org.json "$ROOT"/.vapi-state*.json; do
107108
[[ -e "$state_file" ]] || continue
108-
copy_if_present "$(basename "$state_file")"
109+
copy_if_present "${state_file#"$ROOT"/}"
109110
done
110111
shopt -u nullglob
111112

@@ -185,7 +186,7 @@ fi
185186
log "Setup complete"
186187
cat <<'INFO'
187188
Common commands in this workspace:
188-
npm run pull # sync state from Vapi (writes .vapi-state.<org>.json)
189+
npm run pull # sync state from Vapi (writes state/<org>/state.json)
189190
npm run push # push local resources to Vapi
190191
npm run call # interactive test call against an assistant or squad
191192
npm test # repo tests

‎.github/workflows/promotion.yml‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,18 @@ jobs:
102102
PROMOTION_OUTCOME: ${{ steps.promotion.outcome }}
103103
run: |
104104
set -euo pipefail
105-
git add -A -- ':(glob).vapi-state.*.json'
105+
# Engine state lives in state/ (state/<org>/local/ is gitignored).
106+
# A repository that has not moved its state there yet still has
107+
# .vapi-state.<org>.json at its root. Stage both places, removals
108+
# included, so a move commits the old file's removal and the new
109+
# file together. Each is skipped when it holds nothing, because
110+
# git add fails on a pathspec that matches no file.
111+
if [[ -n "$(git ls-files --cached --others --exclude-standard -- state)" ]]; then
112+
git add -A -- state
113+
fi
114+
if [[ -n "$(git ls-files --cached --others -- ':(glob).vapi-state.*.json')" ]]; then
115+
git add -A -- ':(glob).vapi-state.*.json'
116+
fi
106117
if [[ "$PROMOTION_OUTCOME" == "success" ]]; then
107118
git add -A -- resources
108119
elif [[ -s tmp/promotion-applied.txt ]]; then

‎.gitignore‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ Thumbs.db
2323

2424
tmp/
2525

26-
# Local snapshots written by `npm run push` for `npm run rollback` recovery.
27-
# Operator-local; not shared.
26+
# Engine state lives in state/<org>/ and is committed (see state/README.md),
27+
# except what each machine keeps for itself under state/<org>/local/:
28+
# baselines/ "the last platform state I saw" per resource, for drift
29+
# detection. Committing it would make one developer's baseline
30+
# everyone's.
31+
# snapshots/ copies written by `npm run push` for `npm run rollback`.
32+
state/*/local/
33+
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.
2836
.vapi-state.*.snapshots/
29-
30-
# Per-developer drift baseline store (.vapi-state-hash/<org>/<uuid>).
31-
# Holds "the last platform state I saw" per resource. Local, not shared —
32-
# committing it would make one developer's baseline everyone's.
3337
.vapi-state-hash/
3438

3539
# Dashboard-backup copies written by the push conflict prompt for manual

‎examples/cross-org-promotion/README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ This fixture shows the shape of a linear `example-dev` → `example-staging` →
1313

1414
The example state files intentionally assign different UUIDs to the same
1515
logical resources in each org. They are reference material only. Never copy
16-
them into the repository root or use them with a real Vapi organization.
16+
them into the repository's `state/` folder or use them with a real Vapi
17+
organization.
1718

1819
## What the promotion engine does
1920

2021
The engine resolves tools, structured outputs, assistants, and credentials
21-
through each org's own `.vapi-state.<org>.json`. `npm run promote` mirrors only
22+
through each org's own `state/<org>/state.json`. `npm run promote` mirrors only
2223
the resource patterns reviewed in `promotion.yml`, includes referenced managed
2324
dependencies, and lets the destination push mint or reuse that org's UUIDs.
2425
Source-org managed UUIDs are never copied as destination references.
@@ -85,7 +86,7 @@ Use real disposable orgs, not the fake IDs in this directory.
8586
After the first successful apply, compare the three generated state files:
8687

8788
```bash
88-
node -e 'for (const org of ["example-dev", "example-staging", "example-production"]) { const s = require(`./.vapi-state.${org}.json`); console.log(org, { credential: s.credentials["crm-api"].uuid, tool: s.tools["customer-lookup"].uuid, assistant: s.assistants["support-assistant"].uuid }); }'
89+
node -e 'for (const org of ["example-dev", "example-staging", "example-production"]) { const s = require(`./state/${org}/state.json`); console.log(org, { credential: s.credentials["crm-api"].uuid, tool: s.tools["customer-lookup"].uuid, assistant: s.assistants["support-assistant"].uuid }); }'
8990
```
9091

9192
The aliases should be identical while every org's physical IDs are different.

examples/cross-org-promotion/state/.vapi-state.example-dev.json.example renamed to examples/cross-org-promotion/state/example-dev/state.json.example

File renamed without changes.

examples/cross-org-promotion/state/.vapi-state.example-production.json.example renamed to examples/cross-org-promotion/state/example-production/state.json.example

File renamed without changes.

examples/cross-org-promotion/state/.vapi-state.example-staging.json.example renamed to examples/cross-org-promotion/state/example-staging/state.json.example

File renamed without changes.

‎src/api.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
VAPI_TOKEN,
77
VAPI_TOKEN_SOURCE,
88
} from "./config.ts";
9-
import { ORG_PIN_FILE, OrgPinMismatchError, orgPinCheck } from "./org-pin.ts";
9+
import { OrgPinMismatchError, orgPinCheck, orgPinLabel } from "./org-pin.ts";
1010
import type { VapiResponse } from "./types.ts";
1111
import { userAgentGet, vapiClientGet } from "./user-agent.ts";
1212
import {
@@ -33,7 +33,7 @@ const DRY_RUN_COUNTS = { POST: 0, PATCH: 0, DELETE: 0 };
3333
let orgPinChecked: Promise<void> | undefined;
3434

3535
/**
36-
* Check the key against resources/<org>/.vapi-org.json once per process,
36+
* Check the key against state/<org>/org.json once per process,
3737
* before the first request (see org-pin.ts). A mismatch exits instead of
3838
* throwing, because several callers catch request errors and retry or carry
3939
* on, and none of them should reach the wrong org.
@@ -54,11 +54,11 @@ export function orgPinGuard(): Promise<void> {
5454
});
5555
if (outcome.kind === "pinned")
5656
console.log(
57-
`📌 Pinned resources/${VAPI_ENV}/ to Vapi org ${outcome.orgId} (${ORG_PIN_FILE}). Commit it with the folder.`,
57+
`📌 Pinned resources/${VAPI_ENV}/ to Vapi org ${outcome.orgId} (${orgPinLabel(BASE_DIR, VAPI_ENV)}). Commit that file.`,
5858
);
5959
if (outcome.kind === "unverified")
6060
console.warn(
61-
`⚠️ Couldn't confirm which Vapi org the key belongs to, so it wasn't checked against resources/${VAPI_ENV}/${ORG_PIN_FILE}.`,
61+
`⚠️ Couldn't confirm which Vapi org the key belongs to, so it wasn't checked against ${orgPinLabel(BASE_DIR, VAPI_ENV)}.`,
6262
);
6363
} catch (error) {
6464
if (error instanceof OrgPinMismatchError) {

‎src/apply.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { execSync } from "child_process";
22
import { dirname, join, resolve } from "path";
33
import { fileURLToPath } from "url";
44
import { assertStateMigrated } from "./migrate-hash-store.ts";
5+
import { stateFileRead, stateLayoutPrepare } from "./state-paths.ts";
56
// Pins this command's User-Agent label for the pull and push it spawns.
67
import "./user-agent.ts";
78

@@ -32,10 +33,12 @@ export async function runApply(): Promise<void> {
3233
const allArgs = process.argv.slice(3);
3334
const hasForce = allArgs.includes("--force");
3435

35-
// Fail fast with one clear message if this org's state file is still legacy,
36-
// rather than letting the spawned pull/push subprocess surface it later.
36+
// Move an old-layout state file into state/<org>/ once, here, and fail fast
37+
// with one clear message if it is still in the legacy format, rather than
38+
// letting the spawned pull/push subprocess surface either later.
3739
if (env && SLUG_RE.test(env)) {
38-
assertStateMigrated(join(BASE_DIR, `.vapi-state.${env}.json`));
40+
for (const line of stateLayoutPrepare(BASE_DIR, env)) console.log(line);
41+
assertStateMigrated(stateFileRead(BASE_DIR, env));
3942
}
4043

4144
// Default drift resolution is "defer": pull preserves the local file AND

0 commit comments

Comments
 (0)