diff --git a/src/core/partition.test.ts b/src/core/partition.test.ts index 1470bec14..55ebd5fb4 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, 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) => { @@ -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; diff --git a/src/core/partition.ts b/src/core/partition.ts index 5a08b1e05..8ead698b0 100644 --- a/src/core/partition.ts +++ b/src/core/partition.ts @@ -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 = 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 diff --git a/src/core/project/manager.test.ts b/src/core/project/manager.test.ts index 15959e228..c23bf93b9 100644 --- a/src/core/project/manager.test.ts +++ b/src/core/project/manager.test.ts @@ -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"; @@ -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(); diff --git a/src/core/project/manager.tsx b/src/core/project/manager.tsx index 75ef58d74..719c0dc2f 100644 --- a/src/core/project/manager.tsx +++ b/src/core/project/manager.tsx @@ -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 { @@ -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 @@ -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 { + 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, @@ -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. // @@ -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) }; } } diff --git a/src/handlers/project/create/region.test.ts b/src/handlers/project/create/region.test.ts new file mode 100644 index 000000000..51420cbab --- /dev/null +++ b/src/handlers/project/create/region.test.ts @@ -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(); + }); +}); diff --git a/src/handlers/project/create/region.ts b/src/handlers/project/create/region.ts index 5642a35ad..71bebd694 100644 --- a/src/handlers/project/create/region.ts +++ b/src/handlers/project/create/region.ts @@ -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"; @@ -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); } /**