Skip to content

Commit 5ab85bc

Browse files
refactor(blocks): keep the blocks record in state/<org>/
Push writes the record, so it belongs with the rest of the engine's files. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
1 parent 1e56843 commit 5ab85bc

12 files changed

Lines changed: 44 additions & 39 deletions

‎AGENTS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ every org). The engine expands it before validate, push and PR checks.
260260
`npm run sync-blocks` and show the human the list before any deploy.
261261
- Don't use Liquid `{% include %}` / `{% render %}` or YAML merge keys (`<<`)
262262
to share content: Vapi and the engine drop them silently.
263-
- Commit `resources/<org>/.vapi-blocks.json` when push changes it; never
264-
edit or delete it.
263+
- Commit `state/<org>/blocks.json` when push changes it; never edit or
264+
delete it.
265265

266266
Syntax, merge rules and dashboard-edit handling: [Blocks](docs/guides/blocks.md).
267267

‎docs/guides/blocks.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ Plain `npm run apply -- <org>` also pushes them: to the engine a block edit
242242
looks like a local edit to every file that includes the block.
243243

244244
**3. Commit the record.** The engine knows what each resource was last pushed
245-
with from `resources/<org>/.vapi-blocks.json`. Push updates it; commit it
245+
with from `state/<org>/blocks.json`. Push updates it; commit it
246246
with your other changes, as you do the state file. Don't edit it by hand. If
247247
it is missing, every resource that uses a block is listed until it is pushed
248248
once.

‎improvements.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ you which stack PR closes the row.**
9292
| 38 | Promotion can't carry a simulation that uses a stock personality by UUID | Gated orgs, and any promoted tests, need local personality files | None | Open |
9393
| 39 | Engine state looked like temp files and sat in three places | Deleting the state file makes the next push duplicate every resource | None | RESOLVED 2026-11-17 (#97) |
9494
| 40 | Nothing checked a `credentialIds` list against the keys an org has saved | A list that hides the org's own key runs the call on Vapi's key, silently | None | RESOLVED 2026-11-17 (#96) |
95+
| 41 | Shared prompt text and config had no home, and pull would erase any include marker | Repeated paragraphs drift apart across assistants and orgs | None | RESOLVED 2026-11-16 (#94) |
96+
| 42 | An org with no credentials is re-bootstrapped on every push, which disables the drift check | A dashboard edit is overwritten without a warning in such an org | None | Open |
9597

9698
**Active backlog after cleanup:** `#2`, `#6`, `#8`, `#12`, `#20`, `#24–#26`, `#31`, and the open remainder of `#27` (wiring the listing-completeness verdict into push/delete/audit, and moving `cleanup.ts` onto the shared pager). Resolved entries stay in this file as historical incident notes per the maintenance directive; stale superseded backlog rows are not duplicated.
9799

@@ -2279,7 +2281,7 @@ prompts and `$include` in mappings, looked up in `resources/<org>/templates/`
22792281
then `templates/`, expanded in the loader. Pull never writes a file that uses
22802282
blocks and saves the dashboard version beside it instead. Push records the
22812283
block content each resource went up with in
2282-
`resources/<org>/.vapi-blocks.json`; `npm run sync-blocks` and the promotion
2284+
`state/<org>/blocks.json`; `npm run sync-blocks` and the promotion
22832285
plan (`sync` entries) read it to find resources a block edit has not reached.
22842286

22852287
### Status

‎src/blocks.ts‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
// Config-free, like resource-parse.ts, so every org can be read in one run.
2020

2121
import { createHash } from "node:crypto";
22-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
22+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2323
import { dirname, join, relative, resolve } from "node:path";
2424
import { isDeepStrictEqual } from "node:util";
2525
import { parse as parseYaml } from "yaml";
26+
import { stateOrgDir } from "./state-paths.ts";
2627

2728
export const TEMPLATES_FOLDER = "templates";
2829
export const INCLUDE_KEY = "$include";
@@ -291,21 +292,23 @@ export function blocksExpand<T>(
291292
// ─────────────────────────────────────────────────────────────────────────────
292293
// Sync record
293294
//
294-
// resources/<org>/.vapi-blocks.json records, for each resource that uses
295+
// state/<org>/blocks.json records, for each resource that uses
295296
// blocks, the block content it was last pushed with. Push writes it and it is
296297
// committed. A resource whose blocks no longer match its entry still runs the
297298
// old block text on the platform: that is what "npm run sync-blocks" lists
298299
// and what promotion adds to its plan. The file text of such a resource has
299300
// not changed, so nothing else in git shows that it needs a push.
300301
// ─────────────────────────────────────────────────────────────────────────────
301302

302-
export const BLOCKS_RECORD_FILE = ".vapi-blocks.json";
303+
export function blocksRecordPath(rootDir: string, org: string): string {
304+
return join(stateOrgDir(rootDir, org), "blocks.json");
305+
}
303306

304307
// "<type folder>/<resource id>" → the blocks it was pushed with.
305308
export type BlocksRecord = Record<string, BlockUsage>;
306309

307-
export function blocksRecordRead(resourcesDir: string): BlocksRecord {
308-
const path = join(resourcesDir, BLOCKS_RECORD_FILE);
310+
export function blocksRecordRead(rootDir: string, org: string): BlocksRecord {
311+
const path = blocksRecordPath(rootDir, org);
309312
if (!existsSync(path)) return {};
310313
const parsed: unknown = JSON.parse(readFileSync(path, "utf8"));
311314
if (!isMapping(parsed)) throw new Error(`${path} must hold a JSON object`);
@@ -326,11 +329,12 @@ export function blocksChanged(
326329
// resources that no longer exist or no longer use blocks. Entries for
327330
// resources this push did not touch are kept: they may still be out of date.
328331
export function blocksRecordUpdate(
329-
resourcesDir: string,
332+
rootDir: string,
333+
org: string,
330334
current: Map<string, BlockUsage>,
331335
pushed: Iterable<string>,
332336
): void {
333-
const before = blocksRecordRead(resourcesDir);
337+
const before = blocksRecordRead(rootDir, org);
334338
const after: BlocksRecord = { ...before };
335339
for (const key of pushed) {
336340
const usage = current.get(key);
@@ -343,8 +347,7 @@ export function blocksRecordUpdate(
343347
if (isDeepStrictEqual(before, after)) return;
344348
const sorted: BlocksRecord = {};
345349
for (const key of Object.keys(after).sort()) sorted[key] = after[key]!;
346-
writeFileSync(
347-
join(resourcesDir, BLOCKS_RECORD_FILE),
348-
`${JSON.stringify(sorted, null, 2)}\n`,
349-
);
350+
const path = blocksRecordPath(rootDir, org);
351+
mkdirSync(dirname(path), { recursive: true });
352+
writeFileSync(path, `${JSON.stringify(sorted, null, 2)}\n`);
350353
}

‎src/promotion.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ export async function promotionPlanBuild(
623623
const targetByPath = new Map(target.map((file) => [file.path, file]));
624624
const targetDir = join(options.rootDir, "resources", options.target);
625625
const targetLayers = blockLayers(targetDir);
626-
const targetRecord = blocksRecordRead(targetDir);
626+
const targetRecord = blocksRecordRead(options.rootDir, options.target);
627627
const sourcePaths = new Set([...wanted.values()].map((file) => file.path));
628628
const changes: PromotionChange[] = [];
629629
const warnings: string[] = [];

‎src/push.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,7 @@ async function main(): Promise<void> {
23452345
// edit has not reached yet.
23462346
try {
23472347
if (blocksUsed)
2348-
blocksRecordUpdate(RESOURCES_DIR, blocksUsed, pushedResources);
2348+
blocksRecordUpdate(BASE_DIR, VAPI_ENV, blocksUsed, pushedResources);
23492349
} catch (recordError) {
23502350
console.error(
23512351
"\n⚠️ Failed to update the blocks record after apply:",

‎src/sync-blocks-cmd.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// A block edit changes every resource that includes the block, in every org,
44
// without changing any of their files. This command finds those resources:
55
// the ones whose blocks differ from what the blocks record
6-
// (resources/<org>/.vapi-blocks.json) says they were last pushed with.
6+
// (state/<org>/blocks.json) says they were last pushed with.
77
//
88
// Without --apply it only lists them, reading files and nothing else. With
99
// --apply it runs `apply` for each org, scoped to those files, so the usual
@@ -50,8 +50,7 @@ export async function blocksStaleRead(
5050
rootDir: string,
5151
org: string,
5252
): Promise<BlocksStaleResource[]> {
53-
const resourcesDir = join(rootDir, "resources", org);
54-
const record = blocksRecordRead(resourcesDir);
53+
const record = blocksRecordRead(rootDir, org);
5554
const stale: BlocksStaleResource[] = [];
5655
for (const resource of (await orgResourcesRead(rootDir, org)).values()) {
5756
if (Object.keys(resource.blocks).length === 0) continue;

‎state/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The engine writes everything in this folder. Don't edit or delete these files by
66
| --- | --- | --- |
77
| `state/<org>/state.json` | Which Vapi resource (UUID) each file under `resources/<org>/` is. | Yes |
88
| `state/<org>/org.json` | The Vapi org that `resources/<org>/` belongs to. A command run with another org's key stops before it reads or changes anything. | Yes |
9+
| `state/<org>/blocks.json` | The [block](../docs/guides/blocks.md) content each resource was last pushed with, so `npm run sync-blocks` and promotion can list resources that still run old block text. | Yes |
910
| `state/<org>/credential-providers.json` | The provider of each saved credential, by name, with no secret. The [credential-pin check](../docs/guides/credential-pins.md) reads it. | Yes |
1011
| `state/<org>/local/baselines/` | The last version of each resource this machine saw on the platform, used to notice dashboard edits. | No |
1112
| `state/<org>/local/snapshots/` | A copy of each resource taken before a push, for `npm run rollback`. | No |
@@ -15,6 +16,7 @@ The engine writes everything in this folder. Don't edit or delete these files by
1516

1617
- **`state.json`**: the engine no longer knows which resources already exist, so the next push creates every one of them again as a duplicate. Restore the file from git (`git checkout -- state/<org>/state.json`). Never rebuild it by hand.
1718
- **`org.json`**: the next command pins the folder again to whichever org its key belongs to, so the protection against a wrong key is gone until then. Restore it from git.
19+
- **`blocks.json`**: every resource that uses a block is listed as out of date until it is pushed once. Restore it from git.
1820
- **`credential-providers.json`**: `validate` can't check `credentialIds` lists and says so. The next pull writes the file again.
1921
- **`commit-step.json`**: nothing is lost. A CI run under that pipeline stops before it changes anything and says how to put the file back.
2022
- **`local/`**: nothing breaks. The next pull records new baselines. Snapshots taken so far are gone, so there is nothing to roll back to until the next push.

‎tests/blocks-promotion.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async function recordSeed(org: string): Promise<void> {
3030
const usage = new Map<string, BlockUsage>();
3131
for (const resource of (await orgResourcesRead(ROOT, org)).values())
3232
usage.set(`${FOLDER_MAP[resource.type]}/${resource.id}`, resource.blocks);
33-
blocksRecordUpdate(join(ROOT, "resources", org), usage, usage.keys());
33+
blocksRecordUpdate(ROOT, org, usage, usage.keys());
3434
}
3535

3636
// dev and prod hold the same file, and prod was last pushed with the block
@@ -45,8 +45,8 @@ async function fixtureReset(): Promise<void> {
4545
"resources/dev/assistants/plain.md": assistantFile("Plain", "Hello."),
4646
"resources/prod/assistants/triage.md": TRIAGE,
4747
"resources/prod/assistants/plain.md": assistantFile("Plain", "Hello."),
48-
".vapi-state.dev.json": "{}\n",
49-
".vapi-state.prod.json": "{}\n",
48+
"state/dev/state.json": "{}\n",
49+
"state/prod/state.json": "{}\n",
5050
".gitignore": "tmp/\n.env.*\n",
5151
});
5252
await recordSeed("prod");

‎tests/blocks-pull.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function repo(): string {
3838
"templates/blocks/red-flags.md": `${BLOCK}\n`,
3939
[`${ASSISTANTS}/triage.md`]: TRIAGE_FILE,
4040
[`${ASSISTANTS}/plain.md`]: PLAIN_FILE,
41-
[`.vapi-state.${ORG}.json`]: stateFile({ triage: TRIAGE, plain: PLAIN }),
41+
[`state/${ORG}/state.json`]: stateFile({ triage: TRIAGE, plain: PLAIN }),
4242
});
4343
}
4444

@@ -72,7 +72,7 @@ function read(root: string, path: string): string {
7272
}
7373

7474
function baseline(root: string, uuid: string): string | undefined {
75-
const path = join(root, ".vapi-state-hash", ORG, uuid);
75+
const path = join(root, "state", ORG, "local", "baselines", uuid);
7676
return existsSync(path) ? readFileSync(path, "utf8") : undefined;
7777
}
7878

0 commit comments

Comments
 (0)