Skip to content
Closed
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
19 changes: 18 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, supportsRuntimeV2 } from "./partition";
import { isChinaContext, isChinaRegion, supportsMemory, supportsRuntimeV2 } from "./partition";

describe("isChinaRegion", () => {
test.each(["cn-north-1", "cn-northwest-1"])("returns true for %s", (region) => {
Expand Down Expand Up @@ -38,6 +38,23 @@ describe("supportsRuntimeV2", () => {
});
});

describe("supportsMemory", () => {
// ap-east-9 is not a region: one this CLI does not know of counts as having Memory.
test.each(["us-west-2", "us-east-1", "eu-west-1", "eu-south-2", "ap-southeast-1", "ap-east-9"])(
"returns true for %s",
(region) => {
expect(supportsMemory(region)).toBe(true);
},
);

test.each(["eu-south-1", "ap-southeast-5", "ap-southeast-7", "cn-north-1", "cn-northwest-1"])(
"returns false for %s",
(region) => {
expect(supportsMemory(region)).toBe(false);
},
);
});

describe("isChinaContext", () => {
let savedRegion: string | undefined;
let savedDefaultRegion: string | undefined;
Expand Down
18 changes: 18 additions & 0 deletions src/core/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ export function supportsRuntimeV2(region: string): boolean {
);
}

/**
* Commercial regions where AgentCore Memory is not available. Remove a region here once Memory
* launches in it.
*/
export const NO_MEMORY_REGIONS: ReadonlySet<string> = new Set([
"eu-south-1",
"ap-southeast-5",
"ap-southeast-7",
]);

/**
* Whether AgentCore Memory is available in `region`. It is not in the China (aws-cn) partition
* or in NO_MEMORY_REGIONS. A region not listed counts as having Memory.
*/
export function supportsMemory(region: string): boolean {
return !isChinaRegion(region) && !NO_MEMORY_REGIONS.has(region);
}

/**
* 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
56 changes: 56 additions & 0 deletions src/core/project/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
LITELLM_MODEL_ID_REQUIRED_CN_MESSAGE,
MEMORY_STRIPPED_CN_MESSAGE,
MODEL_PROVIDER_RUNTIMES_CN_MESSAGE,
memoryStrippedMessage,
chinaModelProviderRestriction,
modelProviderOverridableCnMessage,
} from "./manager";
Expand Down Expand Up @@ -1967,6 +1968,61 @@ describe("FsProjectManager.addResource", () => {
expect(String(error)).toContain("uv is missing");
});

// A new runtime with the template's default memory. The tests above set the memory of the
// shared LITELLM_RUNTIME to undefined, so these tests do not use it.
function strandsRuntimeWithMemory() {
const scaffoldRuntimeInput = resolveRuntimeTemplateShortcut("agent-python-strands", {
runtimeName: "mem",
});
expect(scaffoldRuntimeInput.memory).toBeDefined();
return {
resourceType: "runtime" as const,
resourceConfig: { name: "mem", scaffoldRuntimeInput },
};
}

test.each(["eu-south-1", "ap-southeast-5", "ap-southeast-7"])(
"a %s target drops the default memory",
async (region) => {
await inTempDirectory();
const { subject, project } = await projectWithTarget(region, "uv");
const input = strandsRuntimeWithMemory();

const { steps, error } = await collectSteps(subject.addResource(project, input));

expect(memoryStrippedMessage(region)).toContain(region);
expect(steps.join("\n")).toContain(memoryStrippedMessage(region));
expect(input.resourceConfig.scaffoldRuntimeInput.memory).toBeUndefined();
expect(String(error)).toContain("uv is missing");
},
);

test("with no target yet, a resolved region with no Memory drops the default memory", async () => {
await inTempDirectory();
const { subject, project } = await projectWithoutTarget();
const input = strandsRuntimeWithMemory();

const { steps, error } = await collectSteps(
subject.addResource(project, input, { region: "ap-southeast-5" }),
);

expect(steps.join("\n")).toContain(memoryStrippedMessage("ap-southeast-5"));
expect(input.resourceConfig.scaffoldRuntimeInput.memory).toBeUndefined();
expect(String(error)).toContain("uv is missing");
});

test("a target with Memory keeps the default memory", async () => {
await inTempDirectory();
const { subject, project } = await projectWithTarget("us-east-1", "uv");
const input = strandsRuntimeWithMemory();

const { steps, error } = await collectSteps(subject.addResource(project, input));

expect(steps.join("\n")).not.toContain("scaffolding without the default memory");
expect(input.resourceConfig.scaffoldRuntimeInput.memory).toBeDefined();
expect(String(error)).toContain("uv is missing");
});

test("with no target yet, a China resolved region refuses a blocked model provider", async () => {
await inTempDirectory();
const { subject, project } = await projectWithoutTarget();
Expand Down
48 changes: 42 additions & 6 deletions src/core/project/manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ import {
RegionUnsupportedFeatureError,
ResourceNotFoundError,
} from "../../errors/errors";
import { isChinaRegion } from "../partition";
import { isChinaRegion, supportsMemory } from "../partition";
import z from "zod";
import { CdkBackend, type TransactionSearchEnabler } from "./backends/cdk";
import {
Expand Down Expand Up @@ -363,6 +363,18 @@ export const MEMORY_STRIPPED_CN_MESSAGE =
"AgentCore Memory is not available in China regions (cn-north-1, cn-northwest-1); " +
"scaffolding without the default memory";

/**
* The note shown when the default memory is dropped from a scaffold because AgentCore Memory is
* not available in `region`. A China region gets MEMORY_STRIPPED_CN_MESSAGE. Like that message,
* the rendered code keeps the memory module, so a memory can be added later without code changes.
*/
export function memoryStrippedMessage(region: string): string {
if (isChinaRegion(region)) return MEMORY_STRIPPED_CN_MESSAGE;
return (
`AgentCore Memory is not available in ${region}; ` + "scaffolding without the default memory"
);
}

/**
* Shown when a LiteLLM runtime template targets a China (aws-cn) region without
* an explicit model id: the LiteLLM default routes to Amazon Bedrock, which is
Expand Down Expand Up @@ -680,6 +692,20 @@ export class FsProjectManager implements ProjectManager {
return region !== undefined && isChinaRegion(region);
}

/**
* The first region of the project where AgentCore Memory is not available, or undefined when
* Memory is available in all of them. The regions are those of the deployment targets, or the
* CLI's resolved region when no target exists yet, as in {@link isChinaProject}.
*/
private async memoryUnavailableRegion(
project: Project,
region: string | undefined,
): Promise<string | undefined> {
const targets = await this.listTargets(project);
const regions = targets.length > 0 ? targets.map((target) => target.region) : [region];
return regions.find((each) => each !== undefined && !supportsMemory(each));
}

public async *addResource(
project: Project,
input: AddResourceInput,
Expand Down Expand Up @@ -762,7 +788,7 @@ export class FsProjectManager implements ProjectManager {
// kinds in CN_SUPPORTED_ADD_KINDS (Runtime, Gateway, credential). Runtimes additionally gate on the model provider
// — Bedrock/Anthropic/OpenAI/Gemini are unreachable, LiteLLM needs an
// explicit model id (its default routes to Bedrock) — and the default
// memory is dropped from the scaffold (see MEMORY_STRIPPED_CN_MESSAGE);
// memory is dropped from the scaffold (see the memory check below);
// provider-free scaffolds (framework "none": minimal, MCP) stay available
// as the bring-your-own-implementation path.
//
Expand Down Expand Up @@ -793,10 +819,20 @@ export class FsProjectManager implements ProjectManager {
if (restriction !== undefined) {
throw new RegionUnsupportedFeatureError(restriction);
}
if (scaffold.memory !== undefined) {
input.resourceConfig.scaffoldRuntimeInput.memory = undefined;
yield { type: "step", message: MEMORY_STRIPPED_CN_MESSAGE };
}
}
}

// A runtime template's default memory is dropped when a project region has no AgentCore
// Memory: China, and the regions in NO_MEMORY_REGIONS.
if (
input.resourceType === "runtime" &&
!isRuntimeDocsConfig(input.resourceConfig) &&
input.resourceConfig.scaffoldRuntimeInput.memory !== undefined
) {
const noMemoryRegion = await this.memoryUnavailableRegion(project, options.region);
if (noMemoryRegion !== undefined) {
input.resourceConfig.scaffoldRuntimeInput.memory = undefined;
yield { type: "step", message: memoryStrippedMessage(noMemoryRegion) };
}
}

Expand Down
51 changes: 51 additions & 0 deletions src/handlers/project/create/region.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, test } from "bun:test";
import { MEMORY_STRIPPED_CN_MESSAGE, memoryStrippedMessage } from "../../../core/project/manager";
import { resolveRuntimeTemplateShortcut } from "../shortcuts";
import type { CreateProjectInput } from "../types";
import { stripCreateRegionUnavailableDefaults } from "./region";

function createInput(template: "agent-python-strands-box" | "agent-python-minimal-box") {
return {
name: "proj",
skipInstall: true,
skipGit: true,
scaffoldRuntimeInput: resolveRuntimeTemplateShortcut(template),
} satisfies CreateProjectInput;
}

describe("stripCreateRegionUnavailableDefaults", () => {
test.each(["eu-south-1", "ap-southeast-5", "ap-southeast-7"])(
"drops the default memory in %s",
(region) => {
const input = createInput("agent-python-strands-box");
expect(input.scaffoldRuntimeInput.memory).toBeDefined();

expect(stripCreateRegionUnavailableDefaults(input, region)).toBe(
memoryStrippedMessage(region),
);
expect(input.scaffoldRuntimeInput.memory).toBeUndefined();
},
);

test("drops the default memory in a China region with the China message", () => {
const input = createInput("agent-python-strands-box");

expect(stripCreateRegionUnavailableDefaults(input, "cn-north-1")).toBe(
MEMORY_STRIPPED_CN_MESSAGE,
);
expect(input.scaffoldRuntimeInput.memory).toBeUndefined();
});

test("keeps the default memory in a region with Memory", () => {
const input = createInput("agent-python-strands-box");

expect(stripCreateRegionUnavailableDefaults(input, "us-east-1")).toBeUndefined();
expect(input.scaffoldRuntimeInput.memory).toBeDefined();
});

test("gives no notice for a template with no memory", () => {
const input = createInput("agent-python-minimal-box");

expect(stripCreateRegionUnavailableDefaults(input, "eu-south-1")).toBeUndefined();
});
});
18 changes: 9 additions & 9 deletions src/handlers/project/create/region.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isChinaRegion, supportsRuntimeV2 } from "../../../core/partition";
import { isChinaRegion, supportsMemory, supportsRuntimeV2 } from "../../../core/partition";
import {
BMA_CN_MESSAGE,
HARNESS_CN_MESSAGE,
MEMORY_STRIPPED_CN_MESSAGE,
chinaModelProviderRestriction,
memoryStrippedMessage,
} from "../../../core/project/manager";
import { hasBoxProfile } from "../../../core/project/templates/boxProfile";
import { RegionUnsupportedFeatureError } from "../../../errors";
Expand All @@ -29,19 +29,19 @@ export function validateCreateRegionSupport(input: CreateProjectInput, region: s
}

/**
* Drops the defaults a China (aws-cn) region cannot provision — today the
* template's default memory, since AgentCore Memory is not available there.
* Shared by the CLI and the interactive wizard, and run only after
* validateCreateRegionSupport has passed the hard restrictions. Returns the
* notice to show the user, or undefined when nothing was dropped.
* Drops the defaults `region` cannot provision — today the template's default
* memory, where AgentCore Memory is not available (China, and the regions in
* NO_MEMORY_REGIONS). Shared by the CLI and the interactive wizard, and run
* only after validateCreateRegionSupport has passed the hard restrictions.
* Returns the notice to show the user, or undefined when nothing was dropped.
*/
export function stripCreateRegionUnavailableDefaults(
input: CreateProjectInput,
region: string,
): string | undefined {
if (!isChinaRegion(region) || input.scaffoldRuntimeInput?.memory === undefined) return undefined;
if (supportsMemory(region) || input.scaffoldRuntimeInput?.memory === undefined) return undefined;
input.scaffoldRuntimeInput.memory = undefined;
return MEMORY_STRIPPED_CN_MESSAGE;
return memoryStrippedMessage(region);
}

/**
Expand Down
Loading