Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/core/partition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/core/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = 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<string> = 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
Expand Down
34 changes: 33 additions & 1 deletion src/core/project/boxValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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}`,
);
}
124 changes: 124 additions & 0 deletions src/core/project/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 10 additions & 1 deletion src/core/project/manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Comment thread
mquang-amazon marked this conversation as resolved.
Resolved

// 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" });
Expand Down
37 changes: 37 additions & 0 deletions src/handlers/project/create/create.screen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/handlers/project/create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 19 additions & 1 deletion src/handlers/project/create/region.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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."
);
}
Loading
Loading