Skip to content

Commit 00285a1

Browse files
devin-ai-integration[bot]dhruva-vapicapy-ai[bot]
authored
feat(state): keep engine state in state/<org>/ (#109)
* 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> * docs: describe the state/ folder and how an older repository moves into it Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs(improvements): point entry 39 at its pull request Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: leave state/ as it was after a test run Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * 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> * 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> * fix: keep the test suite out of the repository it runs in, and engine files out of pull's local edits Running the test suite in a repository with a state file at its root removed the repository: two tests cleaned up two folders above the engine's baselines folder, and in the older layout that is the repository root. The engine's directory now comes from one place, src/base-dir.ts. The test preload points it at a temporary directory, makes every node:fs change inside the repository throw, and the engine refuses a test started without the preload. A new test runs the whole suite inside a copy of a customer-shaped repository in the older layout, in state/, and with no state, and compares every tracked, untracked and ignored file before and after. That run found one more writer, tests/org-connection.test.ts, which created tmp/ in the repository. Pull's keep rule for a file with no baseline is off whenever git shows a change under resources/<org>/. The engine's own files there (the org pin in the older layout, also shown as deleted between migrate and its commit) are no longer counted. A repository with no state whose commit step can't be read still gets its first state file at the root, and the command now says where the file goes, why, and what has to stage it. The upgrade guide gains one line: on a machine with no baselines, the first pull after upgrading can bring a dashboard edit down one pull late. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: the test sandbox travels with each engine test, and the engine ignores the test runner The engine refused to start when NODE_TEST_CONTEXT was in its environment without a sandbox, and the preload npm test adds sandboxed every test file. Node's test runner sets that variable for every process it starts, so an engine command started from a repository's own test exited 1, and such a test could no longer run the engine on its repository or write a file into it. Both worked before the sandbox existed. The sandbox is now a module, tests/engine-sandbox.ts, that every engine test file imports first: the temporary directory for the engine and the node:fs guard on the repository. It holds however the file is started, by hand without the preload included. tests/no-vapi-api.ts is what it was before the sandbox, and src/base-dir.ts never looks at the test runner's environment. A test file that doesn't import the module is left alone: the commands it starts and the engine modules it imports work in its repository, and it writes where it likes. In the engine's own repository (a runner whose GITHUB_REPOSITORY is VapiAI/gitops, or a clone whose origin is) a test file without the import fails the suite. In any other repository that check is skipped. tests/base-dir.test.ts fails when a module under src/ works out the repository's directory for itself. npm test no longer starts a test runner inside a test. The suite run inside copies of a customer-shaped repository, and the other tests that start a runner of their own, move to tests/repository/ and run with npm run test:repository, one file at a time, and as a CI job in the engine's own repository. A suite inside a suite tripled the time npm test took and reached the twenty-second limits some tests give a command sooner on a slow machine. Each copy now holds a test of the customer's own and runs the engine's test files only. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * test: ask for the sandbox import only in CI for the engine's own repository The check that every test file imports tests/engine-sandbox.ts first was also asked on a person's machine, wherever origin pointed at VapiAI/gitops. A repository cloned straight from the engine's keeps that origin, so a test of its owner's under tests/ made one engine test fail in npm test. Before the sandbox existed it passed. The check is now asked only where GITHUB_REPOSITORY is VapiAI/gitops, which is CI for the engine's own repository. A contributor who leaves the import out finds out on the pull request. GITHUB_REPOSITORY=VapiAI/gitops npm test asks it on a machine. Tests and docs only. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix(state): integrate rebased state layout with main Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Dhruva Reddy <dhruva@vapi.ai> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 4f41c23 commit 00285a1

137 files changed

Lines changed: 6375 additions & 584 deletions

File tree

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/ci.yml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ jobs:
3939

4040
- run: npm test
4141

42+
repository:
43+
# The tests that start a test runner of their own: the suite run inside
44+
# copies of a repository, with every file compared before and after. Only
45+
# in the engine's own repository; a repository built from this one has no
46+
# use for it.
47+
name: The suite leaves its repository alone
48+
if: github.repository == 'VapiAI/gitops'
49+
runs-on: ubuntu-latest
50+
timeout-minutes: 30
51+
52+
steps:
53+
- uses: actions/checkout@v4
54+
55+
- uses: actions/setup-node@v4
56+
with:
57+
node-version: 22
58+
cache: npm
59+
60+
- run: npm ci
61+
62+
- run: npm run test:repository
63+
4264
actionlint:
4365
name: Lint workflows (actionlint)
4466
runs-on: ubuntu-latest

‎.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: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ 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 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.
2838
.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.
3339
.vapi-state-hash/
3440

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

‎AGENTS.md‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,23 @@ wizard needs a terminal. Instead:
110110
- Authoring from scratch → `npm run setup -- <org> --resources none` (state only, no files).
111111
Add `--region eu` for EU orgs if auto-detection picks the wrong one.
112112
4. Verify: `npm run validate -- <org>` passes, and `resources/<org>/` plus
113-
`.vapi-state.<org>.json` exist.
114-
5. Commit `resources/<org>/` (including `.vapi-org.json`) and `.vapi-state.<org>.json`. Never commit `.env.<org>`.
113+
`state/<org>/state.json` exist.
114+
5. Commit `resources/<org>/` and `state/<org>/`. Never commit `.env.<org>`.
115+
116+
`resources/<org>/` is hand-written: edit it. `state/<org>/` is written by the
117+
engine: never edit or delete anything in it (see [state/README.md](state/README.md)).
115118

116119
If a command stops because the key "belongs to Vapi org …, but resources/<org>/
117120
is pinned to org …", the key is for another org. Stop and tell the human.
118-
Never delete or edit `.vapi-org.json` to get past it: that file is the only
121+
Never delete or edit `state/<org>/org.json` to get past it: that file is the only
119122
thing tying the folder to its org.
120123

121124
If setup says the org is "already set up locally", don't delete anything to
122125
work around it — run `npm run pull -- <org>`, or ask the human.
123126

124127
**Working in an existing repository for the first time?** Run a plain
125128
`npm run pull -- <org>` before your first deploy. It seeds your local drift
126-
baselines (`.vapi-state-hash/`, which is per-developer and gitignored);
129+
baselines (`state/<org>/local/baselines/`, which is per-developer and gitignored);
127130
until then, the engine can't tell your edits from dashboard edits as
128131
precisely.
129132

@@ -145,7 +148,7 @@ precisely.
145148
`.env.<org>`, run `VAPI_PRIVATE_API_KEY=validate-only npm run validate -- <org>`;
146149
never ask for a real key just to validate. Don't weaken the check or the
147150
workflow to get past it, and never edit the state file, `.vapi-ignore` or
148-
`.vapi-org.json` to make a reference resolve: fix the name, or pull.
151+
`state/<org>/org.json` to make a reference resolve: fix the name, or pull.
149152
4. **Build PR checks offline** if `vapi-checks.yml` exists:
150153
`npm run check -- --all --dry-run`. Fix anything it reports.
151154
5. **Review the dry-run Name collisions list**, then **deploy only with a yes**
@@ -155,7 +158,7 @@ precisely.
155158
scoped to files: `npm run apply -- <org> resources/<org>/assistants/my-agent.md`.
156159
6. **Verify** with the human's agreement: `npm run call -- <org> -a <name>` or
157160
`npm run sim -- <org> --suite <name> --target <name>`.
158-
7. **Commit** the resource files and `.vapi-state.<org>.json`.
161+
7. **Commit** the resource files and `state/<org>/`.
159162

160163
**Why `apply`, not `push`:** `apply` pulls the platform's current state, merges
161164
your local edits, then pushes, so it never silently overwrites changes made in
@@ -372,6 +375,14 @@ For changes under `src/`, `tests/` or `.github/`:
372375
use a local HTTP stub, as the existing tests do.
373376
- Run `npx biome check <changed files>` before you finish. It uses the pinned
374377
local Biome version and the config in `biome.json`.
378+
- An engine test file's first import is `import "./engine-sandbox.ts";`. It
379+
makes the engine work in a temporary directory and stops the test changing
380+
files in the repository. A test of your own, for your own repository,
381+
leaves it out and works in the repository as before. The suite asks for
382+
the import only in CI for `VapiAI/gitops` itself, so a clone on your
383+
machine is never asked.
384+
- A test that starts a test runner of its own goes in `tests/repository/`
385+
and runs with `npm run test:repository`, not with `npm test`.
375386
- Changing a command's flags or behaviour? Update
376387
[`docs/guides/commands.md`](docs/guides/commands.md) and the README command
377388
table in the same change.

‎CONTRIBUTING.md‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ learnings are all welcome.
2626

2727
3. Make your change, with tests for any behaviour change. Tests use
2828
`node:test` and live in `tests/`; they never call the real Vapi API.
29-
4. Run `npx biome check <changed files>`, `npm run build` and `npm test`, then
30-
open a pull request.
29+
Start every new test file with `import "./engine-sandbox.ts";`, before
30+
any other import. It gives the engine a temporary directory to work in
31+
and stops the test changing files in the repository. CI on your pull
32+
request fails when a test file is missing it; `npm test` on your machine
33+
doesn't ask, so that a test of your own in a clone is left alone. To ask
34+
locally, run `GITHUB_REPOSITORY=VapiAI/gitops npm test`.
35+
4. Run `npx biome check <changed files>`, `npm run build` and `npm test`, then open a pull request.
36+
If you changed a test, or code that writes or removes files, also run
37+
`npm run test:repository`. It runs the suite inside copies of a
38+
repository and checks that no file in them changed. It takes a few
39+
minutes, so it is not part of `npm test`; CI runs it on every pull
40+
request here.
3141

3242
Commit messages and PR titles follow [Conventional Commits](https://www.conventionalcommits.org/)
3343
(`fix(pull): …`, `feat(check): …`, `docs: …`).

‎README.md‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ org can keep using `VAPI_PRIVATE_API_KEY`.
137137
| Option | Default | Meaning |
138138
| --- | --- | --- |
139139
| `--region us\|eu` | `VAPI_BASE_URL` if set, else auto-detect (US, then EU) | Which Vapi API to use |
140-
| `--resources all\|none` | `all` | `all` downloads every resource into `resources/<org>/`; `none` only seeds `.vapi-state.<org>.json` (use when you'll author from scratch) |
140+
| `--resources all\|none` | `all` | `all` downloads every resource into `resources/<org>/`; `none` only seeds `state/<org>/state.json` (use when you'll author from scratch) |
141141

142142
The private API key is never accepted as a command-line flag (it would leak into shell history and
143143
agent transcripts). Non-interactive setup also refuses to run if `resources/<org>/` or
144-
`.vapi-state.<org>.json` already exists — use `npm run pull -- <org>` to refresh an existing org.
144+
`state/<org>/state.json` already exists — use `npm run pull -- <org>` to refresh an existing org.
145145

146146
### 4. Make a change and deploy
147147

@@ -153,7 +153,7 @@ npm run validate -- my-org # schema check, no network
153153
npm run apply -- my-org # pull the latest, merge, push
154154
```
155155

156-
Commit the changed files and `.vapi-state.my-org.json` so your team shares the
156+
Commit the changed files and `state/my-org/state.json` so your team shares the
157157
same name → UUID mappings. Every pull request runs the same validator for
158158
every org in CI (the **Validate resources** check), so a config `apply` would
159159
refuse fails before it merges.
@@ -176,7 +176,7 @@ To test every pull request automatically, set up [PR checks](docs/guides/pr-chec
176176
- **Resources refer to each other by ID.** A resource's ID is its path under
177177
the type folder, without the extension: `tools/lookup-patient.yml` is
178178
`lookup-patient`. Never paste UUIDs into resource files.
179-
- **The state file maps IDs to UUIDs.** `.vapi-state.<org>.json` records which
179+
- **The state file maps IDs to UUIDs.** `state/<org>/state.json` records which
180180
platform resource each file is, per org. It is committed and holds no
181181
secrets.
182182
- **Secrets stay out of git.** API keys live in `.env.<org>` (gitignored) or
@@ -194,7 +194,7 @@ To test every pull request automatically, set up [PR checks](docs/guides/pr-chec
194194
resource; an unlinked file otherwise creates a new one. `apply` uses the same
195195
folder behavior in its push phase.
196196
- **Each folder is pinned to its org.** The first command that can see which
197-
org a key belongs to records it in `resources/<org>/.vapi-org.json`. After
197+
org a key belongs to records it in `state/<org>/org.json`. After
198198
that, every command checks the key against it before any request, and stops
199199
if the key belongs to a different org, so a wrong key can't pull another
200200
org's resources into the folder or push the folder into another org.
@@ -204,11 +204,15 @@ To test every pull request automatically, set up [PR checks](docs/guides/pr-chec
204204

205205
| Path | Committed? | What it is |
206206
| --- | --- | --- |
207-
| `resources/<org>/` | Yes | Your resources, and `.vapi-org.json`, the org the folder belongs to |
208-
| `.vapi-state.<org>.json` | Yes | Name → UUID mappings for the org |
207+
| `resources/<org>/` | Yes | Your resources. Hand-written: this is what you edit |
208+
| `state/<org>/state.json` | Yes | Name → UUID mappings for the org |
209+
| `state/<org>/org.json` | Yes | The Vapi org the folder belongs to |
210+
| `state/<org>/local/` | No | This machine's baselines for detecting dashboard edits, and pre-deploy snapshots for `npm run rollback` |
209211
| `.env.<org>` | **No** | API key and generated binding IDs |
210-
| `.vapi-state-hash/` | No | Your local baseline for detecting dashboard edits |
211-
| `.vapi-state.<org>.snapshots/` | No | Pre-deploy snapshots for `npm run rollback` |
212+
213+
Everything under `state/` is written by the engine. Don't edit or delete it by
214+
hand: without `state.json` the next push creates every resource again as a
215+
duplicate. [state/README.md](state/README.md) says what each file is.
212216

213217
More detail: [How the engine works](docs/guides/how-it-works.md).
214218

@@ -286,7 +290,7 @@ object. API references: [Assistants](https://docs.vapi.ai/api-reference/assistan
286290
## Staying up to date
287291

288292
The engine (`src/`, `tests/`, `.github/workflows/`, `package*.json`) comes from
289-
upstream. Your configuration (`resources/`, `.vapi-state.*.json`,
293+
upstream. Your configuration (`resources/`, `state/`,
290294
`promotion.yml`, `vapi-checks.yml`) is yours; upstream only ships `*.example`
291295
files there. Avoid editing engine files, so updates merge cleanly.
292296

@@ -300,6 +304,9 @@ If you started from **Use this template**, add the remote first with
300304
`git remote add upstream https://github.com/VapiAI/gitops.git`; the first merge
301305
also needs `--allow-unrelated-histories`, and you'll resolve conflicts once.
302306

307+
If your repository still has `.vapi-state.<org>.json` files at its root, see
308+
[Upgrading from an older version](docs/guides/commands.md#upgrading-from-an-older-version).
309+
303310
## Security
304311

305312
Never commit API keys: `.env.*` files are gitignored, and the CLI never accepts

‎SECURITY.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ acknowledge the report and keep you updated as we investigate.
1515
- **Never commit API keys.** Keys live in `.env.<org>` files, which are
1616
gitignored, or in CI secrets. The tools never accept a key as a command-line
1717
flag, so it can't leak into shell history.
18-
- **Committed state contains IDs, not secrets.** `.vapi-state.<org>.json` maps
18+
- **Committed state contains IDs, not secrets.** `state/<org>/state.json` maps
1919
resource names to Vapi UUIDs. Credential secrets and phone-number
2020
provisioning are never written to the repository.
2121
- **`.ts` resource files run code.** Loading a TypeScript resource executes its

0 commit comments

Comments
 (0)