Skip to content

Commit a452a49

Browse files
fix(blocks): keep the blocks record where the org's commit step stages it
An org whose state is still in the layout from before state/ gets its record at resources/<org>/.vapi-blocks.json. A commit step written for that layout stages resources/, so the record is committed, and a promotion run with nothing to promote no longer plans a sync for every resource that uses a block. An org whose state is in state/<org>/ keeps state/<org>/blocks.json. npm run migrate moves the record with the state file and the pin. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
1 parent eb49f0e commit a452a49

7 files changed

Lines changed: 61 additions & 8 deletions

File tree

‎docs/guides/blocks.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ 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.
249249

250+
In a repository whose state is still in the layout from before `state/`, the
251+
record is `resources/<org>/.vapi-blocks.json` instead, which the commit step
252+
that repository already has stages with the folder. `npm run migrate` moves
253+
it with the rest: see
254+
[Upgrading from an older version](commands.md#upgrading-from-an-older-version).
255+
250256
### With promotion
251257

252258
When orgs are linked by a [promotion](promotion.md) pipeline (for example

‎docs/guides/commands.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ tell it. Nothing changes inside the files.
235235
| `.vapi-state.<org>.json` | `state/<org>/state.json` |
236236
| `resources/<org>/.vapi-org.json` | `state/<org>/org.json` |
237237
| `resources/<org>/.vapi-credential-providers.json` | `state/<org>/credential-providers.json` |
238+
| `resources/<org>/.vapi-blocks.json` | `state/<org>/blocks.json` |
238239
| `.vapi-state-hash/<org>/` | `state/<org>/local/baselines/` |
239240
| `.vapi-state.<org>.snapshots/` | `state/<org>/local/snapshots/` |
240241

@@ -383,6 +384,7 @@ for dir in state/*/; do
383384
if [ -f "state/$org/state.json" ]; then mv "state/$org/state.json" ".vapi-state.$org.json"; fi
384385
if [ -f "state/$org/org.json" ]; then mv "state/$org/org.json" "resources/$org/.vapi-org.json"; fi
385386
if [ -f "state/$org/credential-providers.json" ]; then mv "state/$org/credential-providers.json" "resources/$org/.vapi-credential-providers.json"; fi
387+
if [ -f "state/$org/blocks.json" ]; then mv "state/$org/blocks.json" "resources/$org/.vapi-blocks.json"; fi
386388
# This machine's baselines and snapshots. They are not in git, so everyone
387389
# who has pulled or pushed since the move runs these two on their machine.
388390
if [ -d "state/$org/local/baselines" ]; then mkdir -p .vapi-state-hash && mv "state/$org/local/baselines" ".vapi-state-hash/$org"; fi

‎improvements.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2300,7 +2300,8 @@ prompts and `$include` in mappings, looked up in `resources/<org>/templates/`
23002300
then `templates/`, expanded in the loader. Pull never writes a file that uses
23012301
blocks and saves the dashboard version beside it instead. Push records the
23022302
block content each resource went up with in
2303-
`state/<org>/blocks.json`; `npm run sync-blocks` and the promotion
2303+
`state/<org>/blocks.json` (`resources/<org>/.vapi-blocks.json` while the
2304+
org's state is in the older layout); `npm run sync-blocks` and the promotion
23042305
plan (`sync` entries) read it to find resources a block edit has not reached.
23052306

23062307
### Status

‎src/blocks.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ 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";
26+
import { stateRecordPath } from "./state-paths.ts";
2727

2828
export const TEMPLATES_FOLDER = "templates";
2929
export const INCLUDE_KEY = "$include";
@@ -294,14 +294,15 @@ export function blocksExpand<T>(
294294
//
295295
// state/<org>/blocks.json records, for each resource that uses
296296
// blocks, the block content it was last pushed with. Push writes it and it is
297-
// committed. A resource whose blocks no longer match its entry still runs the
297+
// committed. While the org's state is in the layout from before state/, the
298+
// file is resources/<org>/.vapi-blocks.json, which an older commit step stages. A resource whose blocks no longer match its entry still runs the
298299
// old block text on the platform: that is what "npm run sync-blocks" lists
299300
// and what promotion adds to its plan. The file text of such a resource has
300301
// not changed, so nothing else in git shows that it needs a push.
301302
// ─────────────────────────────────────────────────────────────────────────────
302303

303304
export function blocksRecordPath(rootDir: string, org: string): string {
304-
return join(stateOrgDir(rootDir, org), "blocks.json");
305+
return stateRecordPath(rootDir, org, "blocks");
305306
}
306307

307308
// "<type folder>/<resource id>" → the blocks it was pushed with.

‎src/state-paths.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export function orgPinFileRead(rootDir: string, org: string): string {
186186
export const orgPinFileWrite = orgPinFileRead;
187187

188188
/** The records the engine keeps beside an org's state file, by name. */
189-
export const STATE_RECORDS = ["credential-providers"] as const;
189+
export const STATE_RECORDS = ["credential-providers", "blocks"] as const;
190190
export type StateRecord = (typeof STATE_RECORDS)[number];
191191

192192
function recordPath(rootDir: string, org: string, name: StateRecord): string {

‎state/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ Everything under `resources/<org>/`, including `resources/<org>/.vapi-ignore` an
2929

3030
The same files used to sit elsewhere: `.vapi-state.<org>.json` and `.vapi-state.<org>.snapshots/` at the repository root, `.vapi-state-hash/<org>/`, and `resources/<org>/.vapi-org.json`. The engine keeps reading and writing them there, on your machine and in CI, for as long as you leave them.
3131

32-
While an org's state is there, the engine writes its credential providers record to `resources/<org>/.vapi-credential-providers.json`, where the commit step such a repository already has stages it.
32+
While an org's state is there, the engine writes its credential providers record to `resources/<org>/.vapi-credential-providers.json` and its blocks record to `resources/<org>/.vapi-blocks.json`, where the commit step such a repository already has stages them.
3333

3434
Only `npm run migrate` moves them here. Before it moves anything it says what the step that commits state after a run has to stage, and it moves nothing while it can't see that step stage this folder, unless you tell it yours does. Commit the removal of the old files together with the new folder. See [Upgrading from an older version](../docs/guides/commands.md#upgrading-from-an-older-version).

‎tests/blocks.test.ts‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "node:assert/strict";
2-
import { readFileSync, rmSync } from "node:fs";
2+
import { existsSync, readFileSync, rmSync } from "node:fs";
33
import { join } from "node:path";
44
import test from "node:test";
55
import {
@@ -16,6 +16,8 @@ import {
1616
parseResourceDataFromFile,
1717
resourceFileUsesBlocks,
1818
} from "../src/resource-parse.ts";
19+
import { migrateAll } from "../src/migrate-hash-store.ts";
20+
import { commitStepAcknowledge } from "../src/state-paths.ts";
1921
import { filesWrite, repoCreate } from "./blocks-helpers.ts";
2022

2123
// Blocks are looked up from an org's folder, so every test builds a small
@@ -469,8 +471,49 @@ test("resourceFileUsesBlocks reads the file as written", () => {
469471
);
470472
});
471473

474+
test("the blocks record is written where the org's state is committed, and migrate moves it", async () => {
475+
const current = new Map<string, Record<string, string>>([
476+
["assistants/triage", { "blocks/red-flags.md": "aaaa" }],
477+
]);
478+
const entry = { "assistants/triage": { "blocks/red-flags.md": "aaaa" } };
479+
withRepo({ "state/acme/state.json": "{}\n" }, (root) => {
480+
blocksRecordUpdate(root, "acme", current, ["assistants/triage"]);
481+
assert.equal(
482+
blocksRecordPath(root, "acme"),
483+
join(root, "state/acme/blocks.json"),
484+
);
485+
assert.equal(existsSync(join(root, "resources/acme")), false);
486+
assert.deepEqual(blocksRecordRead(root, "acme"), entry);
487+
});
488+
// The layout from before state/: beside the old org pin, which a commit
489+
// step written for that layout stages. Nothing goes under state/.
490+
withRepo({ ".vapi-state.acme.json": "{}\n" }, (root) => {
491+
blocksRecordUpdate(root, "acme", current, ["assistants/triage"]);
492+
assert.equal(
493+
blocksRecordPath(root, "acme"),
494+
join(root, "resources/acme/.vapi-blocks.json"),
495+
);
496+
assert.equal(existsSync(join(root, "state")), false);
497+
assert.deepEqual(blocksRecordRead(root, "acme"), entry);
498+
});
499+
const root = repoCreate({ ".vapi-state.acme.json": "{}\n" });
500+
try {
501+
blocksRecordUpdate(root, "acme", current, ["assistants/triage"]);
502+
commitStepAcknowledge(root);
503+
await migrateAll(root);
504+
assert.equal(existsSync(join(root, "resources/acme/.vapi-blocks.json")), false);
505+
assert.equal(
506+
blocksRecordPath(root, "acme"),
507+
join(root, "state/acme/blocks.json"),
508+
);
509+
assert.deepEqual(blocksRecordRead(root, "acme"), entry);
510+
} finally {
511+
rmSync(root, { recursive: true, force: true });
512+
}
513+
});
514+
472515
test("the blocks record marks only pushed resources as in sync", () => {
473-
withRepo({ "resources/acme/.keep": "" }, (root) => {
516+
withRepo({ "resources/acme/.keep": "", "state/acme/state.json": "{}\n" }, (root) => {
474517
const current = new Map<string, Record<string, string>>([
475518
["assistants/triage", { "blocks/red-flags.md": "aaaa" }],
476519
["assistants/pharmacy", { "blocks/red-flags.md": "aaaa" }],

0 commit comments

Comments
 (0)