From 7adbcc5c0103ca54040f6b7a35baa8b60bc0c0ce Mon Sep 17 00:00:00 2001 From: dhruva Date: Sat, 21 Nov 2026 02:13:02 +0000 Subject: [PATCH] fix(push): stop scoped pushes before creating unlinked dependencies Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- AGENTS.md | 6 + README.md | 2 +- docs/guides/commands.md | 19 ++ docs/guides/how-it-works.md | 14 ++ improvements.md | 17 ++ src/link-preflight.ts | 63 +++++- src/push-dependencies.ts | 71 +++++++ src/push-scope.ts | 42 ++++ src/push.ts | 47 +---- tests/push-dependencies.test.ts | 169 ++++++++++++++++ tests/scoped-dependency-gate.test.ts | 285 +++++++++++++++++++++++++++ 11 files changed, 684 insertions(+), 51 deletions(-) create mode 100644 src/push-dependencies.ts create mode 100644 src/push-scope.ts create mode 100644 tests/push-dependencies.test.ts create mode 100644 tests/scoped-dependency-gate.test.ts diff --git a/AGENTS.md b/AGENTS.md index 9e9a322..4c12f16 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -172,6 +172,12 @@ an unlinked file creates a new resource. Show the report and ask the human to choose per file. Pass `--allow-new-files` only once they confirm every file is genuinely new. For a rename, see [Renaming](#naming-and-renaming); delete stale files. +Scoped pushes also check dependencies they would create, including squad +member assistants and those assistants' tools and structured outputs. Each +is marked "Needed by ", including transitive dependencies. +`--allow-new-files` approves creation throughout the selected scope and its +dependencies; the dependency paths need not also be listed. Ask for approval +for the whole reported creation plan, not just the selected file. Same-named org resources are NOT linked automatically. Preview with `npm run push -- --dry-run --allow-new-files` only after approval for those new files, and check the **Name collisions** list (read-only dashboard diff --git a/README.md b/README.md index e7a7d6f..7d91d1a 100644 --- a/README.md +++ b/README.md @@ -209,7 +209,7 @@ More detail: [How the engine works](docs/guides/how-it-works.md). | `npm run validate -- ` | Check resource files offline before deploying | | `npm run apply -- ` | Deploy: pull, merge, then push (the default) | | `npm run pull -- ` | Sync platform changes down without overwriting local edits; unknown UUIDs get separate files, never name-linked | -| `npm run push -- ` | Push without pulling first (prefer `apply`); review `--dry-run` Name collisions, and use `--adopt [=]` only with explicit approval to take over a resource; unchanged alias files are removed, edited/unbaselined copies need review. Push/apply support `--json` for one grouped link-preflight report (exit 1 on blockers); see [Running in CI](docs/guides/commands.md#running-in-ci). | +| `npm run push -- ` | Push without pulling first (prefer `apply`); review `--dry-run` Name collisions, and use `--adopt [=]` only with explicit approval to take over a resource; unchanged alias files are removed, edited/unbaselined copies need review. Scoped pushes also stop for unlinked dependencies; `--allow-new-files` approves creation in the selection and its dependencies. Push/apply support `--json` for one grouped link-preflight report (exit 1 on blockers); see [Running in CI](docs/guides/commands.md#running-in-ci). | | `npm run rollback -- ` | Restore a pre-deploy snapshot | | `npm run cleanup -- ` | Find, and optionally delete, platform resources with no file | | `npm run audit -- ` | Report drift between files, state and the platform | diff --git a/docs/guides/commands.md b/docs/guides/commands.md index fc64295..8411a1b 100644 --- a/docs/guides/commands.md +++ b/docs/guides/commands.md @@ -45,6 +45,25 @@ block and exits **1**, before API mutations, state writes or alias deletion. "Not linked to any resource in yet" means pushing would create a new resource; the engine cannot infer new versus renamed/stale intent. +The same check covers dependencies a scoped push would auto-create: tools, +structured outputs, and squad member assistants (including their dependencies). +They are marked "Needed by " in text and in JSON's `problem` +field. Already-linked dependencies and repo-owned mappings that can be safely +reused do not require creation approval. `--allow-new-files` authorizes new +resources across the selected scope **and its dependencies**; you do not need +to list the dependency paths separately. For example, after reviewing the +whole plan: + +```bash +npm run push -- assistants/receptionist.md --allow-new-files +# Or explicitly take over the existing tool instead of creating one: +npm run push -- assistants/receptionist.md --adopt my-end-call +``` + +Without either approval, an unlinked dependency stops the run with exit 1 +before API mutations or state writes. `--dry-run` reports the same blockers +and never creates resources. + Each file has exact commands: approve `--allow-new-files` for genuinely new resources, approve `--adopt [=]` for a takeover, or merge needed edits and remove a renamed/stale file. A single ready-to-run combined command appears diff --git a/docs/guides/how-it-works.md b/docs/guides/how-it-works.md index 83ebce1..1c399b1 100644 --- a/docs/guides/how-it-works.md +++ b/docs/guides/how-it-works.md @@ -52,6 +52,20 @@ block and exits **1**, before API mutations, state writes or alias deletion. "Not linked to any resource in yet" means pushing would create a new resource; the engine cannot infer new versus renamed/stale intent. +Before a scoped push writes anything, preflight also follows its auto-apply +paths: selected assistants' tools and structured outputs, and selected squads' +member assistants plus their tools/outputs. This includes dependencies of +already-linked member assistants. Any dependency that would need a POST is +included in the same grouped report, marked "Needed by " (also +in JSON's `problem` field). Linked dependencies and safe reuse of repo-owned +mappings behave as before. References without an auto-apply path do not expand +the creation plan. + +`--allow-new-files` approves the selected scope and all its new dependencies; +dependency paths do not need to be listed separately. Alternatively, explicitly +adopt the dependency with `--adopt [=]`. Without approval, preflight +stops with exit 1 before mutations, including during `--dry-run`. + Each file has exact commands: approve `--allow-new-files` for genuinely new resources, approve `--adopt [=]` for a takeover, or merge needed edits and remove a renamed/stale file. A single ready-to-run combined command appears diff --git a/improvements.md b/improvements.md index 51eac4e..1799bdb 100644 --- a/improvements.md +++ b/improvements.md @@ -2097,3 +2097,20 @@ Alias cleanup now checks canonical file content against the UUID-keyed baseline, removes unchanged copies, and requires approval for edited/unbaselined copies. Link preflight groups all stop reasons with per-file commands (exit 1); `--json` emits the machine-readable report. Apply runs this read-only check before pull. + +## Scoped dependency creation approval (2026-11-21) + +- **Problem:** a scoped assistant push could bypass the new-file check and + POST an unlinked tool or structured output without approval. +- **Current behavior:** link preflight follows the same scope selection and + auto-apply paths, including squad → assistant → tool/output, before writes. + All unlinked dependencies that would be created join the grouped stop report + with the selected file that needs them. +- **Risk:** unexpected new remote resources and changed assistant references. +- **Current mitigation:** review the whole plan, then explicitly approve + `--allow-new-files` or adopt the intended resource. Dependency paths need not + be listed separately; the flag applies throughout the selected scope. +- **Possible fix:** implemented in `src/push-dependencies.ts` and + `src/link-preflight.ts`, with shared selection in `src/push-scope.ts`. +- **Status:** resolved; local-HTTP tests cover blocked, approved, adopted, + linked, dry-run, JSON and transitive creation plans. diff --git a/src/link-preflight.ts b/src/link-preflight.ts index dd56858..1d9655d 100644 --- a/src/link-preflight.ts +++ b/src/link-preflight.ts @@ -12,6 +12,11 @@ import { } from "./config.ts"; import { extractResourceName, type RemoteResource } from "./dep-dedup.ts"; import { detectOrphanYamls } from "./new-file-gate.ts"; +import { collectUnlinkedDependencies } from "./push-dependencies.ts"; +import { + filterResourcesByPaths, + shouldApplyResourceType, +} from "./push-scope.ts"; import { extractBaseSlug, slugify } from "./slug-utils.ts"; import type { TouchedSets } from "./state-merge.ts"; import { type StopIssue, shellQuote } from "./stop-report.ts"; @@ -113,6 +118,37 @@ export async function prepareResourceLinks(opts: { state: preview, filePathFilter: APPLY_FILTER.filePaths, }); + const dependencies = collectUnlinkedDependencies( + opts.resources, + { + assistants: shouldApplyResourceType("assistants") + ? filterResourcesByPaths(opts.resources.assistants, "assistants") + : [], + squads: shouldApplyResourceType("squads") + ? filterResourcesByPaths(opts.resources.squads, "squads") + : [], + }, + preview, + ); + const neededBy = new Map( + dependencies.map((dependency) => [ + `${dependency.type}:${dependency.resource.resourceId}`, + [...dependency.neededBy].map((path) => relative(BASE_DIR, path)), + ]), + ); + for (const { type, resource } of dependencies) { + if ( + !report.orphans.some( + (entry) => + entry.type === type && entry.resourceId === resource.resourceId, + ) + ) + report.orphans.push({ + type, + resourceId: resource.resourceId, + relativePath: relative(BASE_DIR, resource.filePath), + }); + } for (const entry of report.orphans) { if (removed.has(`${entry.type}:${entry.resourceId}`)) continue; const file = opts.resources[entry.type].find( @@ -124,6 +160,15 @@ export async function prepareResourceLinks(opts: { ) continue; const path = relative(BASE_DIR, file.filePath); + const parents = neededBy.get(`${entry.type}:${entry.resourceId}`); + const commandPaths = parents + ? APPLY_FILTER.filePaths?.length + ? APPLY_FILTER.filePaths + : APPLY_FILTER.resourceTypes?.length + ? APPLY_FILTER.resourceTypes + : [path] + : [path]; + const scope = commandPaths.map(shellQuote).join(" "); const name = slugify(extractResourceName(file.data) ?? entry.resourceId); const matches = new Set( Object.entries(opts.state[entry.type]) @@ -141,24 +186,28 @@ export async function prepareResourceLinks(opts: { const adopt = matches.size === 1 && !lookupError; const args = [ `--adopt ${shellQuote(entry.resourceId)}`, - shellQuote(path), + ...commandPaths.map(shellQuote), ]; const commands = [...matches] .sort() .map( (uuid) => - `${base} --adopt ${shellQuote(entry.resourceId + (matches.size > 1 ? "=" + uuid : ""))} ${shellQuote(path)}`, + `${base} --adopt ${shellQuote(entry.resourceId + (matches.size > 1 ? "=" + uuid : ""))} ${scope}`, ); const issue: StopIssue = { file: path, resourceType: entry.type, - problem: `Not linked to any resource in ${VAPI_ENV} yet.${matches.size ? ` Same-named resource(s): ${[...matches].join(", ")}; NOT linked automatically.` : ""}${lookupError ? ` Inventory unavailable: ${lookupError}` : ""}`, - fix: matches.size - ? "To take over a same-named resource, explicitly approve adoption. If genuinely new, approve --allow-new-files instead. If renamed/stale, delete the file." - : "If new, approve --allow-new-files. If renamed/stale, delete the file (merge any needed edits first).", + problem: `Not linked to any resource in ${VAPI_ENV} yet.${parents ? ` Needed by ${parents.join(", ")}.` : ""}${matches.size ? ` Same-named resource(s): ${[...matches].join(", ")}; NOT linked automatically.` : ""}${lookupError ? ` Inventory unavailable: ${lookupError}` : ""}`, + fix: + (matches.size + ? "To take over a same-named resource, explicitly approve adoption. If genuinely new, approve --allow-new-files instead. If renamed/stale, delete the file." + : "If new, approve --allow-new-files. If renamed/stale, delete the file (merge any needed edits first).") + + (parents + ? " If deleting it, also remove or replace its references in the files that need it." + : ""), command: [ ...commands, - `${base} --allow-new-files ${shellQuote(path)}`, + `${base} --allow-new-files ${scope}`, `rm -- ${shellQuote(path)}`, ].join(" OR "), needsChoice: !adopt, diff --git a/src/push-dependencies.ts b/src/push-dependencies.ts new file mode 100644 index 0000000..a62cdd1 --- /dev/null +++ b/src/push-dependencies.ts @@ -0,0 +1,71 @@ +import { findExistingResourceByName } from "./dep-dedup.ts"; +import { extractReferencedIds } from "./resolver.ts"; +import type { + LoadedResources, + ResourceFile, + ResourceType, + StateFile, +} from "./types.ts"; + +export interface UnlinkedDependency { + type: ResourceType; + resource: ResourceFile; + neededBy: Set; +} + +export function collectUnlinkedDependencies( + resources: LoadedResources, + selected: Pick, + state: StateFile, +): UnlinkedDependency[] { + const dependencies = new Map(); + for (const rootType of ["assistants", "squads"] as const) { + for (const root of selected[rootType]) { + const visited = new Set(); + const visit = (type: ResourceType, resource: ResourceFile) => { + const key = `${type}:${resource.resourceId}`; + if (visited.has(key) || resource.data._platformDefault === true) return; + visited.add(key); + const refs = extractReferencedIds(resource.data); + const childTypes: ("assistants" | "tools" | "structuredOutputs")[] = + type === "squads" + ? ["assistants"] + : type === "assistants" + ? ["tools", "structuredOutputs"] + : []; + for (const childType of childTypes) { + for (const id of refs[childType]) { + if (/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(id)) + continue; + const child = resources[childType].find( + (file) => file.resourceId === id, + ); + if (!child || child.data._platformDefault === true) continue; + if ( + !state[childType][id] && + !findExistingResourceByName({ + localResourceId: id, + localPayload: child.data, + stateSection: state[childType], + }) + ) { + let dependency = dependencies.get(`${childType}:${id}`); + if (!dependency) { + dependency = { + type: childType, + resource: child, + neededBy: new Set(), + }; + dependencies.set(`${childType}:${id}`, dependency); + } + dependency.neededBy.add(root.filePath); + } + visit(childType, child); + } + } + }; + visit(rootType, root); + } + } + return [...dependencies.values()]; +} diff --git a/src/push-scope.ts b/src/push-scope.ts new file mode 100644 index 0000000..df950f7 --- /dev/null +++ b/src/push-scope.ts @@ -0,0 +1,42 @@ +import { resolve } from "path"; +import { APPLY_FILTER, BASE_DIR } from "./config.ts"; +import { FOLDER_MAP, pathMatchesFolder } from "./resources.ts"; +import type { ResourceFile, ResourceType } from "./types.ts"; + +export function shouldApplyResourceType(type: ResourceType): boolean { + if (APPLY_FILTER.filePaths?.length) { + const folder = FOLDER_MAP[type]; + return APPLY_FILTER.filePaths.some((fp) => pathMatchesFolder(fp, folder)); + } + if (APPLY_FILTER.resourceTypes?.length) { + return APPLY_FILTER.resourceTypes.includes(type); + } + return true; +} + +export function filterResourcesByPaths( + resources: ResourceFile[], + _type: ResourceType, +): ResourceFile[] { + if (!APPLY_FILTER.filePaths?.length) return resources; + + const matchingIds = new Set(); + for (const filePath of APPLY_FILTER.filePaths) { + const resolvedInput = resolve(BASE_DIR, filePath); + for (const resource of resources) { + if ( + resource.filePath === resolvedInput || + resource.filePath.endsWith(filePath) || + filePath.endsWith(resource.resourceId + ".yml") || + filePath.endsWith(resource.resourceId + ".yaml") || + filePath.endsWith(resource.resourceId + ".md") || + filePath.endsWith(resource.resourceId + ".ts") || + resource.filePath === filePath || + resource.resourceId === filePath.replace(/\.(yml|yaml|md|ts)$/, "") + ) { + matchingIds.add(resource.resourceId); + } + } + } + return resources.filter((resource) => matchingIds.has(resource.resourceId)); +} diff --git a/src/push.ts b/src/push.ts index 0067b27..821cbaf 100644 --- a/src/push.ts +++ b/src/push.ts @@ -27,6 +27,10 @@ import { deleteBaseline, writeBaseline } from "./hash-store.ts"; import { prepareResourceLinks } from "./link-preflight.ts"; import { assertStateMigrated } from "./migrate-hash-store.ts"; import { NameCollisionReport } from "./name-collisions.ts"; +import { + filterResourcesByPaths, + shouldApplyResourceType, +} from "./push-scope.ts"; import { reconcileStateKeyForResource } from "./reconcile-state-key.ts"; import { writeSnapshot } from "./snapshot.ts"; import { mergeScoped } from "./state-merge.ts"; @@ -75,11 +79,9 @@ import { resolveReferences, } from "./resolver.ts"; import { - FOLDER_MAP, loadResources, loadSingleResource, parseResourceFilePath, - pathMatchesFolder, } from "./resources.ts"; import { hashPayload, loadState, saveState, upsertState } from "./state.ts"; import type { @@ -1181,47 +1183,6 @@ function isPartialApply(): boolean { ); } -function shouldApplyResourceType(type: ResourceType): boolean { - if (APPLY_FILTER.filePaths?.length) { - const folder = FOLDER_MAP[type]; - return APPLY_FILTER.filePaths.some((fp) => pathMatchesFolder(fp, folder)); - } - if (APPLY_FILTER.resourceTypes?.length) { - return APPLY_FILTER.resourceTypes.includes(type); - } - return true; -} - -function filterResourcesByPaths( - resources: ResourceFile[], - type: ResourceType, -): ResourceFile[] { - if (!APPLY_FILTER.filePaths?.length) return resources; - - const matchingIds = new Set(); - - for (const filePath of APPLY_FILTER.filePaths) { - const resolvedInput = resolve(BASE_DIR, filePath); - - for (const resource of resources) { - if ( - resource.filePath === resolvedInput || - resource.filePath.endsWith(filePath) || - filePath.endsWith(resource.resourceId + ".yml") || - filePath.endsWith(resource.resourceId + ".yaml") || - filePath.endsWith(resource.resourceId + ".md") || - filePath.endsWith(resource.resourceId + ".ts") || - resource.filePath === filePath || - resource.resourceId === filePath.replace(/\.(yml|yaml|md|ts)$/, "") - ) { - matchingIds.add(resource.resourceId); - } - } - } - - return resources.filter((r) => matchingIds.has(r.resourceId)); -} - function scopeLoadedResourcesForApply( resources: LoadedResources, ): LoadedResources { diff --git a/tests/push-dependencies.test.ts b/tests/push-dependencies.test.ts new file mode 100644 index 0000000..43b3689 --- /dev/null +++ b/tests/push-dependencies.test.ts @@ -0,0 +1,169 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { collectUnlinkedDependencies } from "../src/push-dependencies.ts"; +import type { LoadedResources, ResourceFile, StateFile } from "../src/types.ts"; +import { VALID_RESOURCE_TYPES } from "../src/types.ts"; + +const resource = ( + id: string, + data: Record = {}, +): ResourceFile => ({ + resourceId: id, + filePath: `${id}.yml`, + data, +}); +function fixture() { + const resources = Object.fromEntries( + VALID_RESOURCE_TYPES.map((type) => [type, []]), + ) as unknown as LoadedResources; + const state = Object.fromEntries( + ["credentials", ...VALID_RESOURCE_TYPES].map((type) => [type, {}]), + ) as unknown as StateFile; + return { resources, state }; +} + +test("dependency planning follows squad members through linked assistants to tools and outputs", () => { + const { resources, state } = fixture(); + resources.squads = [ + resource("team", { members: [{ assistantId: "agent" }] }), + ]; + resources.assistants = [ + resource("agent", { + model: { toolIds: ["stop", "stop"] }, + artifactPlan: { structuredOutputIds: ["result"] }, + }), + ]; + resources.tools = [resource("stop")]; + resources.structuredOutputs = [resource("result")]; + state.assistants.agent = { uuid: "linked", origin: "pulled" }; + const found = collectUnlinkedDependencies( + resources, + { assistants: [], squads: resources.squads }, + state, + ); + assert.deepEqual( + found.map(({ type, resource, neededBy }) => [ + type, + resource.resourceId, + [...neededBy], + ]), + [ + ["tools", "stop", ["team.yml"]], + ["structuredOutputs", "result", ["team.yml"]], + ], + ); +}); + +test("each selected root is recorded once and unlinked squad members are included", () => { + const { resources, state } = fixture(); + resources.squads = [ + resource("team", { + members: [{ assistantId: "agent" }, { assistantId: "agent" }], + }), + ]; + resources.assistants = [ + resource("agent", { hooks: [{ do: [{ toolId: "stop" }] }] }), + ]; + resources.tools = [resource("stop")]; + const found = collectUnlinkedDependencies(resources, resources, state); + assert.deepEqual( + found.find(({ type }) => type === "tools")?.neededBy, + new Set(["agent.yml", "team.yml"]), + ); + assert.deepEqual( + found.find(({ type }) => type === "assistants")?.neededBy, + new Set(["team.yml"]), + ); +}); + +test("linked and repo-owned reusable dependencies do not plan POSTs", () => { + const { resources, state } = fixture(); + resources.assistants = [resource("agent", { toolIds: ["linked", "owned"] })]; + resources.tools = [resource("linked"), resource("owned", { name: "Owned" })]; + state.tools.linked = { uuid: "remote", origin: "pulled" }; + for (const origin of ["pushed", "adopted"] as const) { + state.tools["owned-abcd1234"] = { uuid: "abcd1234", origin }; + assert.deepEqual( + collectUnlinkedDependencies(resources, resources, state), + [], + ); + } + state.tools["owned-abcd1234"] = { uuid: "abcd1234", origin: "pulled" }; + assert.deepEqual( + collectUnlinkedDependencies(resources, resources, state).map( + ({ resource }) => resource.resourceId, + ), + ["owned"], + ); +}); + +test("empty, unresolved, UUID and read-only references are not auto-created", () => { + const { resources, state } = fixture(); + assert.deepEqual( + collectUnlinkedDependencies(resources, resources, state), + [], + ); + const uuid = "aaaaaaaa-1111-4111-8111-111111111111"; + resources.assistants = [ + resource("agent", { model: { toolIds: ["missing", uuid, "default"] } }), + ]; + resources.tools = [ + resource(uuid), + resource("default", { _platformDefault: true }), + ]; + assert.deepEqual( + collectUnlinkedDependencies(resources, resources, state), + [], + ); +}); + +test("squad assistant destinations are also auto-applied", () => { + const { resources, state } = fixture(); + resources.squads = [ + resource("team", { + members: [ + { + assistantId: "external", + assistantDestinations: [{ assistantId: "target" }], + }, + ], + }), + ]; + resources.assistants = [resource("target")]; + const found = collectUnlinkedDependencies( + resources, + { assistants: [], squads: resources.squads }, + state, + ); + assert.deepEqual( + found.map(({ resource }) => resource.resourceId), + ["target"], + ); +}); + +test("references without an engine auto-apply path do not expand the plan", () => { + const { resources, state } = fixture(); + resources.assistants = [ + resource("agent", { + toolIds: ["handoff"], + artifactPlan: { structuredOutputIds: ["result"] }, + }), + resource("other", { toolIds: ["unrelated"] }), + ]; + resources.tools = [ + resource("handoff", { destinations: [{ assistantId: "other" }] }), + resource("unrelated"), + ]; + resources.structuredOutputs = [ + resource("result", { assistant_ids: ["other"] }), + ]; + const found = collectUnlinkedDependencies( + resources, + { assistants: [resources.assistants[0]!], squads: [] }, + state, + ); + assert.deepEqual( + found.map(({ resource }) => resource.resourceId), + ["handoff", "result"], + ); +}); diff --git a/tests/scoped-dependency-gate.test.ts b/tests/scoped-dependency-gate.test.ts new file mode 100644 index 0000000..da1c244 --- /dev/null +++ b/tests/scoped-dependency-gate.test.ts @@ -0,0 +1,285 @@ +import assert from "node:assert/strict"; +import { spawn } from "node:child_process"; +import { + cpSync, + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + symlinkSync, + writeFileSync, +} from "node:fs"; +import { createServer } from "node:http"; +import type { AddressInfo } from "node:net"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; +import { fileURLToPath } from "node:url"; + +const REPO = fileURLToPath(new URL("..", import.meta.url)); +const ORG = "test-dep-gate"; +const TOOL = "67aea057-1111-4111-8111-111111111111"; +const ASSISTANT = "aaaaaaaa-1111-4111-8111-111111111111"; +const CREATED = "bbbbbbbb-2222-4222-8222-222222222222"; +const SQUAD = "cccccccc-3333-4333-8333-333333333333"; + +for (const mode of [ + "unscoped", + "scoped", + "scoped-short", + "dry-run", + "json", + "type", + "allow", + "allow-dry-run", + "adopt", + "linked", + "owned", + "transitive", + "inventory-error", +]) { + test(`scoped dependency gate: ${mode}`, async () => { + const dir = mkdtempSync(join(tmpdir(), "vapi-dep-gate-")); + cpSync(join(REPO, "src"), join(dir, "src"), { recursive: true }); + cpSync(join(REPO, "package.json"), join(dir, "package.json")); + symlinkSync(join(REPO, "node_modules"), join(dir, "node_modules"), "dir"); + const resources = join(dir, "resources", ORG); + for (const folder of ["assistants", "tools", "squads", "structuredOutputs"]) + mkdirSync(join(resources, folder), { recursive: true }); + const assistantPath = `resources/${ORG}/assistants/receptionist.md`; + const squadPath = `resources/${ORG}/squads/team.yml`; + const assistantFile = `--- +name: Receptionist +model: + provider: openai + model: gpt-4o + toolIds: [my-end-call] +voice: + provider: 11labs + voiceId: burt +${mode === "transitive" ? "artifactPlan:\n structuredOutputIds: [result]\n" : ""}--- + +You are the receptionist. +`; + writeFileSync(join(dir, assistantPath), assistantFile); + writeFileSync( + join(resources, "tools/my-end-call.yml"), + "name: End Call\ntype: endCall\n", + ); + if (mode === "transitive") { + writeFileSync( + join(resources, "squads/team.yml"), + "name: Team\nmembers:\n - assistantId: receptionist\n", + ); + writeFileSync( + join(resources, "structuredOutputs/result.yml"), + "name: Result\nschema:\n type: object\n", + ); + } + const statePath = join(dir, `.vapi-state.${ORG}.json`); + const initial = JSON.stringify({ + credentials: { fake: { uuid: "cred" } }, + assistants: + mode === "transitive" + ? {} + : { receptionist: { uuid: ASSISTANT, origin: "pulled" } }, + tools: + mode === "linked" + ? { "my-end-call": { uuid: TOOL, origin: "pulled" } } + : { + "end-call-67aea057": { + uuid: TOOL, + origin: mode === "owned" ? "pushed" : "pulled", + }, + }, + squads: + mode === "transitive" + ? { team: { uuid: SQUAD, origin: "pulled" } } + : {}, + }); + writeFileSync(statePath, initial); + const assistant = { + id: ASSISTANT, + name: "Receptionist", + model: { provider: "openai", model: "gpt-4o", toolIds: [TOOL] }, + voice: { provider: "11labs", voiceId: "burt" }, + }; + const tool = { id: TOOL, name: "End Call", type: "endCall" }; + const requests: { + method: string; + path: string; + body: Record; + }[] = []; + const server = createServer(async (req, res) => { + let body = ""; + for await (const chunk of req) body += chunk; + const path = req.url!.split("?")[0]!; + requests.push({ + method: req.method!, + path, + body: body ? JSON.parse(body) : {}, + }); + res.setHeader("Content-Type", "application/json"); + if (mode === "inventory-error" && path === "/tool") { + res.statusCode = 401; + res.end(JSON.stringify({ message: "Inventory denied" })); + return; + } + let result: unknown = []; + if (req.method === "GET" && path === "/tool") result = [tool]; + if (req.method === "GET" && path === "/assistant") result = [assistant]; + if (req.method === "GET" && path === `/assistant/${ASSISTANT}`) + result = assistant; + if (req.method === "GET" && path === `/tool/${TOOL}`) result = tool; + if (req.method === "POST" && path === "/tool") + result = { ...JSON.parse(body), id: CREATED }; + if (req.method === "PATCH") + result = { ...JSON.parse(body), id: path.split("/")[2] }; + res.end(JSON.stringify(result)); + }); + await new Promise((resolve) => + server.listen(0, "127.0.0.1", resolve), + ); + try { + const args = [ + "--import", + "tsx", + "src/push.ts", + ORG, + ...(mode === "unscoped" + ? [] + : mode === "type" + ? ["--type", "assistants"] + : [ + mode === "transitive" + ? squadPath + : mode === "scoped-short" + ? "assistants/receptionist.md" + : assistantPath, + ]), + ...(mode.startsWith("allow") ? ["--allow-new-files"] : []), + ...(mode.includes("dry-run") ? ["--dry-run"] : []), + ...(mode === "json" || mode === "transitive" ? ["--json"] : []), + ...(mode === "adopt" ? ["--adopt", "my-end-call"] : []), + ]; + const child = spawn(process.execPath, args, { + cwd: dir, + env: { + ...process.env, + VAPI_PRIVATE_API_KEY: "fake-token-not-used", + VAPI_BASE_URL: `http://127.0.0.1:${(server.address() as AddressInfo).port}`, + }, + stdio: ["ignore", "pipe", "pipe"], + }); + let stdout = "", + stderr = ""; + child.stdout.on("data", (data) => { + stdout += data; + }); + child.stderr.on("data", (data) => { + stderr += data; + }); + const timeout = setTimeout(() => child.kill("SIGKILL"), 30000); + const code = await new Promise((resolve) => + child.on("close", resolve), + ); + clearTimeout(timeout); + const blocked = ![ + "allow", + "allow-dry-run", + "adopt", + "linked", + "owned", + ].includes(mode); + assert.equal(code, blocked ? 1 : 0, stdout + stderr); + const mutations = requests.filter((request) => request.method !== "GET"); + if (blocked || mode === "allow-dry-run") { + assert.deepEqual(mutations, [], JSON.stringify(requests)); + assert.equal(readFileSync(statePath, "utf8"), initial); + assert.equal( + readFileSync(join(dir, assistantPath), "utf8"), + assistantFile, + ); + assert.equal(existsSync(join(dir, ".vapi-state-hash")), false); + assert.equal( + existsSync(join(dir, `.vapi-state.${ORG}.snapshots`)), + false, + ); + } + if (blocked) { + assert.match(stdout + stderr, /Not linked to any resource/); + assert.match( + stdout + stderr, + /Needed by .*\/(receptionist.md|team.yml)/, + ); + if (mode === "json" || mode === "transitive") { + const issues = JSON.parse(stdout) as { + file: string; + problem: string; + command: string; + }[]; + assert.equal(issues.length, mode === "transitive" ? 3 : 1, stdout); + assert.ok( + issues.every((issue) => + issue.problem.includes( + mode === "transitive" ? squadPath : assistantPath, + ), + ), + ); + if (mode === "transitive") + assert.deepEqual( + issues.map((issue) => issue.file.split("/").at(-1)).sort(), + ["my-end-call.yml", "receptionist.md", "result.yml"], + ); + } else assert.equal(stderr.match(/Push stopped:/g)?.length, 1, stderr); + if (mode === "inventory-error") + assert.match(stderr, /Inventory unavailable/); + if (mode === "scoped") { + assert.match( + stderr, + new RegExp(`--allow-new-files ${assistantPath}`), + ); + assert.match( + stderr, + new RegExp(`--adopt my-end-call ${assistantPath}`), + ); + } + } else if (mode === "allow-dry-run") { + assert.match(stdout, /would POST \/tool/); + } else { + const creates = mutations.filter( + (request) => request.method === "POST", + ); + assert.equal( + creates.length, + mode === "allow" ? 1 : 0, + JSON.stringify(mutations), + ); + if (creates.length) assert.equal(creates[0]!.path, "/tool"); + const patch = mutations.find( + (request) => request.path === `/assistant/${ASSISTANT}`, + )!; + assert.deepEqual((patch.body.model as { toolIds: string[] }).toolIds, [ + mode === "allow" ? CREATED : TOOL, + ]); + const saved = JSON.parse(readFileSync(statePath, "utf8")); + assert.equal( + saved.tools["my-end-call"].uuid, + mode === "allow" ? CREATED : TOOL, + ); + assert.equal( + saved.tools["my-end-call"].origin, + mode === "adopt" + ? "adopted" + : mode === "linked" + ? "pulled" + : "pushed", + ); + } + } finally { + await new Promise((resolve) => server.close(() => resolve())); + rmSync(dir, { recursive: true, force: true }); + } + }); +}