diff --git a/src/core/partition.test.ts b/src/core/partition.test.ts index 7275bf082..1470bec14 100644 --- a/src/core/partition.test.ts +++ b/src/core/partition.test.ts @@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test"; import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { isChinaContext, isChinaRegion } from "./partition"; +import { isChinaContext, isChinaRegion, supportsRuntimeV2 } from "./partition"; describe("isChinaRegion", () => { test.each(["cn-north-1", "cn-northwest-1"])("returns true for %s", (region) => { @@ -14,6 +14,30 @@ describe("isChinaRegion", () => { }); }); +describe("supportsRuntimeV2", () => { + // ap-east-9 is not a region: one this CLI does not know of counts as having V2, as the European + // Sovereign Cloud (eusc-de-east-1) does. + test.each(["us-west-2", "us-east-1", "eu-west-1", "cn-north-1", "ap-east-9", "eusc-de-east-1"])( + "returns true for %s", + (region) => { + expect(supportsRuntimeV2(region)).toBe(true); + }, + ); + + test.each([ + "ap-south-2", + "us-west-1", + "us-gov-west-1", + "us-gov-east-1", + "us-iso-east-1", + "us-isob-east-1", + "us-isof-south-1", + "eu-isoe-west-1", + ])("returns false for %s", (region) => { + expect(supportsRuntimeV2(region)).toBe(false); + }); +}); + describe("isChinaContext", () => { let savedRegion: string | undefined; let savedDefaultRegion: string | undefined; diff --git a/src/core/partition.ts b/src/core/partition.ts index e2657d26e..5a08b1e05 100644 --- a/src/core/partition.ts +++ b/src/core/partition.ts @@ -14,6 +14,34 @@ export function isChinaRegion(region: string): boolean { return partition(region).name === "aws-cn"; } +/** + * Regions where AgentCore Runtime offers no platformVersion V2. Box runs only on V2. The list is + * the AgentCore Runtime team's; remove a region here once V2 launches in it. + */ +const NO_RUNTIME_V2_REGIONS: ReadonlySet = new Set(["ap-south-2", "us-west-1"]); +/** + * Partitions with no V2 in any region: GovCloud, and the isolated (ADC) partitions. The European + * Sovereign Cloud (`aws-eusc`) is left out on purpose: like a region the CLI does not list, it + * counts as having V2, and CreateAgentRuntime refuses it there if it does not. + */ +const NO_RUNTIME_V2_PARTITIONS: ReadonlySet = new Set([ + "aws-us-gov", + "aws-iso", + "aws-iso-b", + "aws-iso-e", + "aws-iso-f", +]); + +/** + * Whether AgentCore Runtime offers platformVersion V2 in `region`. A region not listed counts as + * having V2, so a new region needs no CLI release; one without V2 fails at CreateAgentRuntime. + */ +export function supportsRuntimeV2(region: string): boolean { + return ( + !NO_RUNTIME_V2_REGIONS.has(region) && !NO_RUNTIME_V2_PARTITIONS.has(partition(region).name) + ); +} + /** * True when the CLI is operating in a China (aws-cn) context: the caller's * resolved region (when provided — telemetry resolves through the same chain diff --git a/src/core/project/boxValidation.ts b/src/core/project/boxValidation.ts index 633951403..7e4f35a4f 100644 --- a/src/core/project/boxValidation.ts +++ b/src/core/project/boxValidation.ts @@ -3,8 +3,9 @@ * agent with no box. The runtime sync asks the sync rules before it writes agentcore.json, and * build and deploy call {@link validateBoxRuntimes} before they read the box profiles. */ -import { ProjectStateError } from "../../errors/errors"; +import { ProjectStateError, RegionUnsupportedFeatureError } from "../../errors/errors"; import { isBoxProject, type ProjectSpec } from "../../projectSchemas/project"; +import { supportsRuntimeV2 } from "../partition"; import type { ProjectRuntime } from "../../projectSchemas/runtime"; import { BOX_RELEASE } from "./backends/boxRelease"; import { BOX_CODEZIP_ENTRYPOINT, runsInBox } from "./templates/boxProfile"; @@ -100,3 +101,34 @@ export function validateBoxRuntimes(spec: ProjectSpec): void { assertBoxRuntimesHaveBoxVersion(spec); assertBoxCodeZipRuntimesStartBox(spec); } + +/** + * Throws for a Box project deployed to a region where AgentCore Runtime has no platformVersion V2, + * when one of its runtimes runs in a box: Box runs only on V2, so that runtime could not be created + * there. A Box project with no runtime is let through, as deploying it is how its stack is removed. + * `target` is a configured target, or, with `provisioning`, the default target deploy is about to + * create in that region; `otherTargets` says whether aws-targets.json has targets to deploy to. + */ +export function assertBoxRegionSupported( + spec: ProjectSpec, + target: { name: string; region: string }, + { + provisioning = false, + otherTargets = false, + }: { provisioning?: boolean; otherTargets?: boolean } = {}, +): void { + if (supportsRuntimeV2(target.region)) return; + if (!spec.runtimes.some((runtime) => runsInBox(isBoxProject(spec), runtime))) return; + const wayOut = provisioning + ? otherTargets + ? "Deploy with --region set to a region that has it, or with --target set to a target in " + + "another region, then retry." + : "Deploy with --region set to a region that has it, or add a target in another region to " + + "agentcore/aws-targets.json, then retry." + : `Change the region of target '${target.name}' in agentcore/aws-targets.json, or deploy ` + + "with --target to a target in another region, then retry."; + throw new RegionUnsupportedFeatureError( + `Cannot deploy Box project '${spec.name}' to target '${target.name}' in ${target.region}: ` + + `Box runs on AgentCore Runtime V2, which is not available in ${target.region}. ${wayOut}`, + ); +} diff --git a/src/core/project/manager.test.ts b/src/core/project/manager.test.ts index f72d95162..263a2d3dd 100644 --- a/src/core/project/manager.test.ts +++ b/src/core/project/manager.test.ts @@ -1567,6 +1567,130 @@ describe("FsProjectManager.addResource", () => { expect(deployCalls).toEqual([]); }); + describe("Box projects in a region without Runtime V2", () => { + // partition.test.ts covers which regions have no Runtime V2; one is enough here. + test.each(["us-west-1"])( + "deploy of a Box project to %s fails before anything is written", + async (region) => { + await inTempDirectory(); + const { subject, project, deployCalls } = await projectWithTarget( + region, + undefined, + AGENT_PYTHON_STRANDS_BOX, + ); + const specPath = join(project.rootPath, "agentcore", "agentcore.json"); + const before = await readFile(specPath, "utf8"); + + const { error } = await deployOutcome(subject, project); + expect(error).toBeInstanceOf(RegionUnsupportedFeatureError); + expect(String(error)).toContain( + `Cannot deploy Box project 'example' to target 'primary' in ${region}: Box runs on ` + + `AgentCore Runtime V2, which is not available in ${region}. Change the region of ` + + "target 'primary' in agentcore/aws-targets.json, or deploy with --target to a target " + + "in another region, then retry.", + ); + expect(await readFile(specPath, "utf8")).toBe(before); + expect(deployCalls).toEqual([]); + }, + ); + + // Deploying a project with no runtime is how the CLI removes its stack, so a Box project that was + // deployed to such a region with an earlier build can still be torn down. + test("deploy of a Box project with no runtime to us-west-1 is not refused", async () => { + await inTempDirectory(); + const { subject, project } = await projectWithTarget( + "us-west-1", + undefined, + AGENT_PYTHON_STRANDS_BOX, + ); + const specPath = join(project.rootPath, "agentcore", "agentcore.json"); + const spec = JSON.parse(await readFile(specPath, "utf8")); + await writeFile( + specPath, + JSON.stringify({ ...spec, runtimes: [], agentCoreGateways: [], memories: [] }, null, 2), + ); + + const { error } = await deployOutcome(subject, project); + expect(error).not.toBeInstanceOf(RegionUnsupportedFeatureError); + }); + + test("deploy of a Box project with no target to us-west-1 fails before STS or aws-targets.json", async () => { + await inTempDirectory(); + const { project } = await projectWithTarget( + "us-east-1", + undefined, + AGENT_PYTHON_STRANDS_BOX, + ); + const targetsPath = join(project.rootPath, "agentcore", "aws-targets.json"); + await rm(targetsPath); + const accounts: string[] = []; + const subject = new FsProjectManager({ + logger: createSilentLogger(), + identity: new TestIdentityClient(), + enableTransactionSearch: async () => {}, + boxProfileResolver: new FakeBoxProfileResolver(), + runner: async () => {}, + checkTool: async () => {}, + resolveAccount: async (region) => { + accounts.push(region); + return "111122223333"; + }, + }); + const fresh = (await subject.resolve({ filePath: project.rootPath }))!; + + const deploying = (async () => { + for await (const _ of subject.deploy(fresh, { + target: "default", + region: "us-west-1", + confirmTeardown: async () => false, + })); + })(); + + await expect(deploying).rejects.toThrow( + new RegionUnsupportedFeatureError( + "Cannot deploy Box project 'example' to target 'default' in us-west-1: Box runs on " + + "AgentCore Runtime V2, which is not available in us-west-1. Deploy with --region set " + + "to a region that has it, or add a target in another region to " + + "agentcore/aws-targets.json, then retry.", + ), + ); + expect(accounts).toEqual([]); + expect(existsSync(targetsPath)).toBe(false); + }); + + test("deploy of a project that is not a Box project to us-west-1 is not refused", async () => { + await inTempDirectory(); + const { subject, project, deployCalls } = await projectWithTarget("us-west-1"); + + const { error } = await deployOutcome(subject, project); + expect(error).toBeUndefined(); + expect(deployCalls).toHaveLength(1); + }); + + test("names --target, not a new target, when aws-targets.json has targets", async () => { + await inTempDirectory(); + const { subject, project } = await projectWithTarget( + "us-west-2", + undefined, + AGENT_PYTHON_STRANDS_BOX, + ); + const fresh = (await subject.resolve({ filePath: project.rootPath }))!; + + const deploying = (async () => { + for await (const _ of subject.deploy(fresh, { + target: "default", + region: "us-west-1", + confirmTeardown: async () => false, + })); + })(); + + await expect(deploying).rejects.toThrow( + "Deploy with --region set to a region that has it, or with --target set to a target in " + + "another region, then retry.", + ); + }); + }); + test("deploy to a China target hard-fails a Bedrock Managed Agents runtime", async () => { await inTempDirectory(); const { subject, project, deployCalls } = await projectWithTarget( diff --git a/src/core/project/manager.tsx b/src/core/project/manager.tsx index 761570dd3..e8e8a4c2c 100644 --- a/src/core/project/manager.tsx +++ b/src/core/project/manager.tsx @@ -81,7 +81,7 @@ import { startsInBoxOnCodeZip, } from "./templates/boxProfile"; import { defaultLogTargetNotice } from "./runtimeProfile/rules/additions"; -import { validateBoxRuntimes } from "./boxValidation"; +import { assertBoxRegionSupported, validateBoxRuntimes } from "./boxValidation"; import { BOX_INSTALL_HINT, BOX_RELEASE } from "./backends/boxRelease"; import { getRuntimeTemplateResolver } from "./templates/runtime"; import { @@ -1817,6 +1817,12 @@ export class FsProjectManager implements ProjectManager { // `default` gets this treatment: inventing a *named* target would turn a // typo'd --target into a deployment somewhere unintended. if (!target && input.target === DEFAULT_TARGET_NAME) { + // Box runs only on AgentCore Runtime V2, so stop before STS or aws-targets.json. + assertBoxRegionSupported( + project.spec, + { name: DEFAULT_TARGET_NAME, region: input.region }, + { provisioning: true, otherTargets: targets.length > 0 }, + ); target = await this.provisionDefaultTarget(project, targetsPath, input.region); yield { type: "step", @@ -1845,6 +1851,9 @@ export class FsProjectManager implements ProjectManager { ); } + // Box runs only on AgentCore Runtime V2, so stop before the sync rewrites agentcore.json. + assertBoxRegionSupported(project.spec, target); + // Synth reads agentcore.json, so it is brought up to date with the code // first; only once the target is known, so a bad --target rewrites nothing. const synced = await this.syncRuntimeBuilds(project, { purpose: "deploy" }); diff --git a/src/handlers/project/create/create.screen.test.tsx b/src/handlers/project/create/create.screen.test.tsx index a0f742fed..76317f788 100644 --- a/src/handlers/project/create/create.screen.test.tsx +++ b/src/handlers/project/create/create.screen.test.tsx @@ -1077,6 +1077,43 @@ describe("project create wizard", () => { r.unmount(); }, 10000); + test("template flow: a box template in a region without Runtime V2 warns and still creates", async () => { + const { path: directory, cleanup } = await inTempDirectory(); + cleanups.push(cleanup); + const core = new TestCoreClient(); + spyOnCreate(core); + const r = renderScreen("/agentcore/create", { + core, + withContext: (ctx) => ctx.withValue(RegionKey, "us-west-1"), + }); + + await waitForText(r.lastFrame, "name your project"); + await r.write("SfoApp"); + await r.press("return"); + await waitForText(r.lastFrame, "what kind of agent to start with?"); + await r.press("down"); // code-based + await r.press("return"); + await waitForText(r.lastFrame, "choose a template"); + await r.press("down"); // agent-python-strands-box + await waitForText(r.lastFrame, "● agent-python-strands-box "); + await r.press("return"); + await waitForText(r.lastFrame, "choose a model provider"); + await r.press("return"); // focus the (Bedrock) model id + await waitForText(r.lastFrame, "model ID"); + await r.press("return"); // accept it + await waitForText(r.lastFrame, "how contained should the agent be?"); + await r.press("return"); // sandboxed + await waitForText(r.lastFrame, "this project will be created"); + await r.press("return"); + await waitForText(r.lastFrame, "✔ project created in ./SfoApp", 5000); + + expect(flatFrame(r.lastFrame)).toContain( + "Box runs on AgentCore Runtime V2, which is not available in us-west-1", + ); + expect(existsSync(join(directory, "SfoApp", "agentcore", "agentcore.json"))).toBe(true); + r.unmount(); + }, 10000); + test("template flow: the minimal template scaffolds without memory", async () => { const { path: directory, cleanup } = await inTempDirectory(); cleanups.push(cleanup); diff --git a/src/handlers/project/create/index.ts b/src/handlers/project/create/index.ts index 3d7785423..1fa89a7af 100644 --- a/src/handlers/project/create/index.ts +++ b/src/handlers/project/create/index.ts @@ -32,7 +32,11 @@ import { InputValidationError, RegionUnsupportedFeatureError } from "../../../er import { JsonKey, RegionKey } from "../../keys"; import { renderResult } from "../../utils"; import { projectReference, type ProjectMutationResult } from "../output"; -import { stripCreateRegionUnavailableDefaults, validateCreateRegionSupport } from "./region"; +import { + boxRegionWarning, + stripCreateRegionUnavailableDefaults, + validateCreateRegionSupport, +} from "./region"; import { chinaModelProviderRestriction } from "../../../core/project/manager"; import { isChinaRegion } from "../../../core/partition"; import { harnessApiKeyCredentialName } from "../../../core/project/templates/harness"; @@ -178,6 +182,8 @@ export const createCreateProjectHandler = (config: CreateProjectHandlerConfig) = } const stripped = stripCreateRegionUnavailableDefaults(createInput, region); if (stripped !== undefined) config.io.stderr.write(`${stripped}\n`); + const boxWarning = boxRegionWarning(createInput, region); + if (boxWarning !== undefined) config.io.stderr.write(`Warning: ${boxWarning}\n`); // Same driver as build and deploy: a live step list in a TTY, and the previous plain // line-per-step output when stderr is not a TTY or --json wants no ANSI on it. diff --git a/src/handlers/project/create/region.ts b/src/handlers/project/create/region.ts index a7def1fb1..5642a35ad 100644 --- a/src/handlers/project/create/region.ts +++ b/src/handlers/project/create/region.ts @@ -1,10 +1,11 @@ -import { isChinaRegion } from "../../../core/partition"; +import { isChinaRegion, supportsRuntimeV2 } from "../../../core/partition"; import { BMA_CN_MESSAGE, HARNESS_CN_MESSAGE, MEMORY_STRIPPED_CN_MESSAGE, chinaModelProviderRestriction, } from "../../../core/project/manager"; +import { hasBoxProfile } from "../../../core/project/templates/boxProfile"; import { RegionUnsupportedFeatureError } from "../../../errors"; import type { CreateProjectInput } from "../types"; @@ -42,3 +43,20 @@ export function stripCreateRegionUnavailableDefaults( input.scaffoldRuntimeInput.memory = undefined; return MEMORY_STRIPPED_CN_MESSAGE; } + +/** + * The warning create gives for a box template in a region where AgentCore Runtime has no V2, which + * Box needs. Create still makes the project: the region here is often only the one the CLI + * resolved, and the project can deploy to a target in another region. Deploy refuses the region. + * Shared by the CLI and the interactive wizard. Undefined when there is nothing to warn about. + */ +export function boxRegionWarning(input: CreateProjectInput, region: string): string | undefined { + const runtime = input.scaffoldRuntimeInput; + if (runtime === undefined || !hasBoxProfile(runtime) || supportsRuntimeV2(region)) + return undefined; + return ( + `Box runs on AgentCore Runtime V2, which is not available in ${region}, so this project ` + + "cannot be deployed there. Deploy it with --region set to a region that has V2, or to a " + + "target in another region." + ); +} diff --git a/src/handlers/project/create/screen.tsx b/src/handlers/project/create/screen.tsx index f8aa12c3f..896827b0f 100644 --- a/src/handlers/project/create/screen.tsx +++ b/src/handlers/project/create/screen.tsx @@ -56,7 +56,11 @@ import { } from "../../../components/wizard"; import { darkTheme } from "../../../components/ui/_core.js"; import { TuiExitMessageKey } from "../../../tui/exitMessage"; -import { stripCreateRegionUnavailableDefaults, validateCreateRegionSupport } from "./region"; +import { + boxRegionWarning, + stripCreateRegionUnavailableDefaults, + validateCreateRegionSupport, +} from "./region"; const theme = darkTheme; @@ -254,6 +258,8 @@ export function ProjectCreateScreen({ ctx, core }: ScreenProps) { validateCreateRegionSupport(input, resolvedRegion); const stripped = stripCreateRegionUnavailableDefaults(input, resolvedRegion); if (stripped !== undefined) yield { type: "step" as const, message: stripped }; + const boxWarning = boxRegionWarning(input, resolvedRegion); + if (boxWarning !== undefined) yield { type: "warning" as const, message: boxWarning }; return yield* core.projectManager.create({ ...input, region: resolvedRegion }); }} runningLabel={`creating ${values.name}…`} diff --git a/src/handlers/project/project.test.ts b/src/handlers/project/project.test.ts index e31d36b7a..68d923956 100644 --- a/src/handlers/project/project.test.ts +++ b/src/handlers/project/project.test.ts @@ -172,6 +172,53 @@ describe("project create", () => { expect(spec.runtimes[0].box).toEqual(box); }); + test.each(["us-west-1", "ap-south-2", "us-gov-west-1"])( + "create of a box template in %s warns that it cannot deploy there, and still creates it", + async (region) => { + const { path: directory, cleanup } = await inTempDirectory(); + cleanups.push(cleanup); + const { io } = await run([ + "create", + "--name", + "BoxApp", + "--template", + "agent-python-strands-box", + "--region", + region, + "--skip-install", + "--skip-git", + ]); + + expect(io.stderr()).toContain( + `Warning: Box runs on AgentCore Runtime V2, which is not available in ${region}, so this ` + + "project cannot be deployed there.", + ); + expect(existsSync(join(directory, "BoxApp", "agentcore", "agentcore.json"))).toBe(true); + }, + ); + + test.each([ + ["a box template in us-west-2", "agent-python-strands-box", "us-west-2"], + ["a template that is not a box template in us-west-1", "agent-python-strands", "us-west-1"], + ])("create of %s gives no Runtime V2 warning", async (_name, template, region) => { + const { path: directory, cleanup } = await inTempDirectory(); + cleanups.push(cleanup); + const { io } = await run([ + "create", + "--name", + "BoxApp", + "--template", + template, + "--region", + region, + "--skip-install", + "--skip-git", + ]); + + expect(io.stderr()).not.toContain("AgentCore Runtime V2"); + expect(existsSync(join(directory, "BoxApp", "agentcore", "agentcore.json"))).toBe(true); + }); + test.each(["agent-python-strands", "empty"])( "create --template %s --containment private-vpc is refused and writes nothing", async (template) => {