-
Notifications
You must be signed in to change notification settings - Fork 99
feat: add China (aws-cn) region support with feature gates #2426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0016e43
feat: add China (aws-cn) regions to supported region enum
shawnxli cf49abe
feat: gate unavailable features in China (aws-cn) regions
shawnxli e2f7cb7
test: restructure isChinaContext walk for full line coverage
shawnxli a8eb63c
fix: address review feedback on the China (aws-cn) gates
shawnxli f279116
chore(deps): fix fast-uri audit advisories via bun audit fix
shawnxli fc8f4f0
fix: drop default memory from China scaffolds; address second review …
shawnxli 8884652
fix: allowlist China-supported resource families; classify imports; r…
shawnxli 37d67e4
Merge remote-tracking branch 'origin/refactor' into cn-region-support…
shawnxli a1fb82e
fix: classify LiteLLM runtimes by model id for the China deploy gate
shawnxli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| 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"; | ||
|
|
||
| describe("isChinaRegion", () => { | ||
| test.each(["cn-north-1", "cn-northwest-1"])("returns true for %s", (region) => { | ||
| expect(isChinaRegion(region)).toBe(true); | ||
| }); | ||
|
|
||
| test.each(["us-east-1", "eu-west-1", "us-gov-west-1", ""])("returns false for %j", (region) => { | ||
| expect(isChinaRegion(region)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isChinaContext", () => { | ||
| let savedRegion: string | undefined; | ||
| let savedDefaultRegion: string | undefined; | ||
| let directory: string; | ||
|
|
||
| beforeEach(async () => { | ||
| savedRegion = process.env.AWS_REGION; | ||
| savedDefaultRegion = process.env.AWS_DEFAULT_REGION; | ||
| delete process.env.AWS_REGION; | ||
| delete process.env.AWS_DEFAULT_REGION; | ||
| directory = await mkdtemp(join(tmpdir(), "agentcore-partition-")); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| if (savedRegion === undefined) delete process.env.AWS_REGION; | ||
| else process.env.AWS_REGION = savedRegion; | ||
| if (savedDefaultRegion === undefined) delete process.env.AWS_DEFAULT_REGION; | ||
| else process.env.AWS_DEFAULT_REGION = savedDefaultRegion; | ||
| await rm(directory, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| async function projectWithTargets(targets: unknown): Promise<string> { | ||
| const root = join(directory, "project"); | ||
| await mkdir(join(root, "agentcore"), { recursive: true }); | ||
| await writeFile(join(root, "agentcore", "agentcore.json"), "{}"); | ||
| if (targets !== undefined) { | ||
| await writeFile(join(root, "agentcore", "aws-targets.json"), JSON.stringify(targets)); | ||
| } | ||
| return root; | ||
| } | ||
|
|
||
| test("true when the ambient region env var is a China region", async () => { | ||
| process.env.AWS_REGION = "cn-northwest-1"; | ||
| expect(await isChinaContext({ cwd: directory })).toBe(true); | ||
| }); | ||
|
|
||
| test("true when the caller passes a resolved China region", async () => { | ||
| expect(await isChinaContext({ region: "cn-north-1", cwd: directory })).toBe(true); | ||
| expect(await isChinaContext({ region: "us-west-2", cwd: directory })).toBe(false); | ||
| }); | ||
|
|
||
| test("true when the enclosing project declares a China deployment target", async () => { | ||
| const root = await projectWithTargets([ | ||
| { name: "primary", account: "111122223333", region: "us-west-2" }, | ||
| { name: "china", account: "111122223333", region: "cn-north-1" }, | ||
| ]); | ||
| expect(await isChinaContext({ cwd: root })).toBe(true); | ||
| // The walk finds the project from a nested path as well. | ||
| expect(await isChinaContext({ cwd: join(root, "app", "nested") })).toBe(true); | ||
| }); | ||
|
|
||
| test("false for a project with only commercial targets", async () => { | ||
| const root = await projectWithTargets([ | ||
| { name: "primary", account: "111122223333", region: "us-west-2" }, | ||
| ]); | ||
| expect(await isChinaContext({ cwd: root })).toBe(false); | ||
| }); | ||
|
|
||
| test("false when no project encloses the directory", async () => { | ||
| expect(await isChinaContext({ cwd: directory })).toBe(false); | ||
| }); | ||
|
|
||
| test("false when the targets file is missing or malformed", async () => { | ||
| const noTargets = await projectWithTargets(undefined); | ||
| expect(await isChinaContext({ cwd: noTargets })).toBe(false); | ||
|
|
||
| await writeFile(join(noTargets, "agentcore", "aws-targets.json"), "not json"); | ||
| expect(await isChinaContext({ cwd: noTargets })).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { readFile } from "node:fs/promises"; | ||
| import { existsSync } from "node:fs"; | ||
| import { dirname, join } from "node:path"; | ||
| import { partition } from "@aws-sdk/util-endpoints"; | ||
|
|
||
| /** | ||
| * True when the region belongs to the aws-cn partition (cn-north-1, | ||
| * cn-northwest-1), per the AWS SDK's partition data rather than a name | ||
| * prefix. Features whose backing services are not reachable from that | ||
| * partition (template model providers, Bedrock Agent import, the telemetry | ||
| * collector) gate on this. | ||
| */ | ||
| export function isChinaRegion(region: string): boolean { | ||
| return partition(region).name === "aws-cn"; | ||
| } | ||
|
|
||
| /** | ||
| * 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 | ||
| * as withRegion) or the ambient AWS region env vars point at a China region, | ||
| * or the AgentCore project enclosing `cwd` declares a China-region deployment | ||
| * target in | ||
| * agentcore/aws-targets.json. | ||
| * | ||
| * Best-effort by design — a missing project or an unreadable/malformed targets | ||
| * file counts as non-China — so callers that must never fail (telemetry) can | ||
| * rely on it unconditionally. | ||
| */ | ||
| export async function isChinaContext( | ||
| options: { region?: string; cwd?: string } = {}, | ||
| ): Promise<boolean> { | ||
| const cwd = options.cwd ?? process.cwd(); | ||
| if (options.region !== undefined && isChinaRegion(options.region)) return true; | ||
| if (isChinaRegion(process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION ?? "")) return true; | ||
|
|
||
| // Mirror project discovery: walk up to the first directory containing | ||
| // agentcore/agentcore.json, then inspect its aws-targets.json. | ||
| let dir = cwd; | ||
| while (!existsSync(join(dir, "agentcore", "agentcore.json"))) { | ||
| const parent = dirname(dir); | ||
| if (parent === dir) return false; | ||
| dir = parent; | ||
| } | ||
| try { | ||
| const raw = await readFile(join(dir, "agentcore", "aws-targets.json"), "utf8"); | ||
| const targets: unknown = JSON.parse(raw); | ||
| return ( | ||
| Array.isArray(targets) && | ||
| targets.some( | ||
| (target) => | ||
| typeof target === "object" && | ||
| target !== null && | ||
| "region" in target && | ||
| typeof target.region === "string" && | ||
| isChinaRegion(target.region), | ||
| ) | ||
| ); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.