Skip to content
Open
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
25 changes: 24 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,25 @@ 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.
* Box runs only on V2, so the runtimes could not be created there. `target` is a configured target,
* or, with `provisioning`, the default target deploy is about to create in that region.
*/
export function assertBoxRegionSupported(
spec: ProjectSpec,
target: { name: string; region: string },
{ provisioning = false }: { provisioning?: boolean } = {},
): void {
if (!isBoxProject(spec) || supportsRuntimeV2(target.region)) return;
const wayOut = provisioning
? "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}`,
);
}
74 changes: 74 additions & 0 deletions src/core/project/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,80 @@ describe("FsProjectManager.addResource", () => {
expect(deployCalls).toEqual([]);
});

test.each(["ap-south-2", "us-west-1", "us-gov-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([]);
},
);

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("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 @@ -74,7 +74,7 @@ import {
startsInBoxOnCodeZip,
} from "./templates/boxProfile";
import { defaultLogTargetNotice, observabilityOff } 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 @@ -1795,6 +1795,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 },
);
target = await this.provisionDefaultTarget(project, targetsPath, input.region);
yield {
type: "step",
Expand Down Expand Up @@ -1823,6 +1829,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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: this check is in the right place for a configured target. But for a fresh project, provisionDefaultTarget (above) has already called STS and written aws-targets.json by the time we get here. You could check input.region for a Box project before provisioning, or reword "before any AWS call" in the description. Either way, a test for the default-target path would pin it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 6f78b4f: a Box project is checked against input.region before provisionDefaultTarget, so an unsupported region makes no STS call and writes no aws-targets.json. A test covers that path (no aws-targets.json, Box project, us-west-1): resolveAccount is never called and the file is not written.


// 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
Loading