From 18f07c696ce46e042feb1f8d025e544e0aa12b4e Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Fri, 20 Nov 2026 17:41:47 -0800 Subject: [PATCH 01/11] feat(box): Send a boxed agent's Memory calls to Memory directly The Box gateway signed the agent's Memory calls with its own role. Memory then could not hold each call to the session of the runtime by its context keys, so one session could read or write the memory of another session. Build now adds no `agentcore` target to the Box gateway, and writes no `agentcore/box-memory-policy.json` for the gateway role. The Runtime profile gets a direct `[egress.agentcore]` route to the AgentCore host of the build region. Box signs that route with a bare `aws://`, the execution role, and the route has no gateway backend. The execution role already has the Memory data-plane grants, because the runtime binds its Memory. The route is added only for a runtime with its default Memory. A route or HTTP MCP server of the profile that already has the AgentCore host keeps it. A user route named `agentcore` for another host stops build. When AgentCore Gateway can get the session from the context keys, Memory calls can go through the gateway again. --- src/core/project/boxMemory.test.ts | 168 +++--------------- src/core/project/boxMemory.ts | 116 +----------- src/core/project/boxNetwork/network.test.ts | 67 ++++--- src/core/project/boxNetwork/network.ts | 30 ++-- src/core/project/boxNetworkSource.test.ts | 12 +- src/core/project/boxNetworkSource.ts | 11 +- src/core/project/boxNetworkWriters.test.ts | 62 ++----- src/core/project/boxNetworkWriters.ts | 22 +-- src/core/project/manager.boxNetwork.test.ts | 114 ++++++------ src/core/project/manager.test.ts | 50 +++--- src/core/project/manager.tsx | 4 +- .../project/runtimeProfile/derive.test.ts | 2 +- src/core/project/runtimeProfile/derive.ts | 2 +- .../runtimeProfile/rules/additions.test.ts | 101 +++++++++-- .../project/runtimeProfile/rules/additions.ts | 64 ++++++- .../runtimeProfile/rules/gateway.test.ts | 32 ++-- .../project/runtimeProfile/rules/gateway.ts | 3 +- .../runtimeProfile/rules/paths.test.ts | 2 +- .../runtimeProfile/rules/refusals.test.ts | 2 +- src/core/project/runtimeProfile/types.ts | 4 +- src/handlers/project/shortcuts.test.ts | 12 +- src/handlers/project/shortcuts.ts | 28 +-- 22 files changed, 355 insertions(+), 553 deletions(-) diff --git a/src/core/project/boxMemory.test.ts b/src/core/project/boxMemory.test.ts index 96dfbfb43..56b434a9f 100644 --- a/src/core/project/boxMemory.test.ts +++ b/src/core/project/boxMemory.test.ts @@ -1,18 +1,11 @@ import { describe, expect, test } from "bun:test"; -import { InputValidationError } from "../../errors/errors"; -import { - getBoxGatewayAgentCoreTargetSpec, - getBoxGatewayBedrockTargetSpec, -} from "../../handlers/project/shortcuts"; +import { getBoxGatewayBedrockTargetSpec } from "../../handlers/project/shortcuts"; import { boxGatewaySpec, type ProjectSpec } from "../../projectSchemas/project"; import { addBoxBedrockTarget, addBoxGatewayTargets, - addBoxMemoryTarget, BOX_BEDROCK_POLICY_FILE, - BOX_MEMORY_POLICY_FILE, boxBedrockPolicy, - boxMemoryPolicy, } from "./boxMemory"; function spec(options: { entrypoint?: string; memories?: string[] } = {}): ProjectSpec { @@ -45,132 +38,6 @@ const gateway = (project: ProjectSpec) => const targets = (project: ProjectSpec) => gateway(project).targets.map(({ name }) => name); -describe("addBoxMemoryTarget", () => { - test("adds the agentcore target to the runtime's Box gateway for its default Memory", () => { - const project = spec(); - - expect(addBoxMemoryTarget(project)).toEqual([ - `adding target 'agentcore' to gateway '${GATEWAY}' in agentcore.json, for the boxed agent's Memory calls`, - `adding 'agentcore/box-memory-policy.json' to gateway '${GATEWAY}' additionalPolicies in agentcore.json, so its role can call Memory`, - ]); - expect(gateway(project).additionalPolicies).toEqual([BOX_MEMORY_POLICY_FILE]); - expect(gateway(project).targets).toEqual([getBoxGatewayAgentCoreTargetSpec()]); - }); - - test("changes nothing when the target is already there", () => { - const project = spec(); - addBoxMemoryTarget(project); - - expect(addBoxMemoryTarget(project)).toEqual([]); - expect(targets(project)).toEqual(["agentcore"]); - }); - - test("changes nothing for another Memory, or for a runtime that does not run in a box", () => { - const otherMemory = spec({ memories: ["ordersMemory"] }); - const unboxed = spec({ entrypoint: "main.py" }); - - expect(addBoxMemoryTarget(otherMemory)).toEqual([]); - expect(addBoxMemoryTarget(unboxed)).toEqual([]); - expect(targets(otherMemory)).toEqual([]); - expect(targets(unboxed)).toEqual([]); - }); - - test("changes nothing in a project that is not a Box project", () => { - const { projectType: _omitted, ...rest } = spec(); - const project = { ...rest, agentCoreGateways: [] } as ProjectSpec; - - expect(addBoxMemoryTarget(project)).toEqual([]); - expect(project.agentCoreGateways).toEqual([]); - expect(project.projectType).toBeUndefined(); - }); - - test("adds no agentcore target where another target serves its endpoint", () => { - const project = spec(); - gateway(project).targets = [{ ...getBoxGatewayAgentCoreTargetSpec(), name: "mem" }]; - - addBoxMemoryTarget(project); - - expect(targets(project)).toEqual(["mem"]); - expect(gateway(project).additionalPolicies).toEqual([BOX_MEMORY_POLICY_FILE]); - }); - - test("refuses the target name on a gateway that is not a Box gateway", () => { - const project = spec(); - project.agentCoreGateways.push({ - name: "tools", - authorizerType: "AWS_IAM", - targets: [getBoxGatewayAgentCoreTargetSpec()], - } as ProjectSpec["agentCoreGateways"][number]); - - expect(() => addBoxMemoryTarget(project)).toThrow(InputValidationError); - }); - - test("lists the policy file on a gateway that already has the target", () => { - const project = spec(); - gateway(project).targets.push(getBoxGatewayAgentCoreTargetSpec()); - - expect(addBoxMemoryTarget(project)).toEqual([ - `adding 'agentcore/box-memory-policy.json' to gateway '${GATEWAY}' additionalPolicies in agentcore.json, so its role can call Memory`, - ]); - expect(targets(project)).toEqual(["agentcore"]); - }); - - test("leaves the policies of a gateway that imports its role", () => { - const project = spec(); - gateway(project).executionRoleArn = "arn:aws:iam::123456789012:role/mine"; - - addBoxMemoryTarget(project); - - expect(gateway(project).additionalPolicies).toBeUndefined(); - }); -}); - -describe("boxMemoryPolicy", () => { - type Policy = { - Statement: Array<{ Action: string[]; Resource: string[]; Condition: unknown }>; - }; - const SAME_ACCOUNT = { StringEquals: { "aws:ResourceAccount": "${aws:PrincipalAccount}" } }; - - test("allows the Memory data plane on the default Memory in any target, in the gateway role's account", () => { - const policy = boxMemoryPolicy(spec(), "us-west-2") as Policy; - - expect(policy.Statement[0]!.Resource).toEqual([ - "arn:aws:bedrock-agentcore:us-west-2:*:memory/demo_*agentMemory-*", - ]); - expect(policy.Statement[0]!.Condition).toEqual(SAME_ACCOUNT); - expect(policy.Statement[0]!.Action).toContain("bedrock-agentcore:ListEvents"); - expect(policy.Statement[0]!.Action).toContain("bedrock-agentcore:CreateEvent"); - }); - - test("names the region it is given, and no other", () => { - const policy = boxMemoryPolicy(spec(), "eu-central-1") as Policy; - - expect(policy.Statement).toEqual([ - expect.objectContaining({ - Resource: ["arn:aws:bedrock-agentcore:eu-central-1:*:memory/demo_*agentMemory-*"], - Condition: SAME_ACCOUNT, - }), - ]); - }); - - test("names the partition of the region it is given", () => { - const resources = (region: string) => - (boxMemoryPolicy(spec(), region) as Policy).Statement[0]!.Resource; - - expect(resources("cn-north-1")).toEqual([ - "arn:aws-cn:bedrock-agentcore:cn-north-1:*:memory/demo_*agentMemory-*", - ]); - expect(resources("us-gov-west-1")).toEqual([ - "arn:aws-us-gov:bedrock-agentcore:us-gov-west-1:*:memory/demo_*agentMemory-*", - ]); - }); - - test("is undefined when no boxed runtime has its default Memory", () => { - expect(boxMemoryPolicy(spec({ memories: ["ordersMemory"] }), "us-west-2")).toBeUndefined(); - expect(boxMemoryPolicy(spec({ entrypoint: "main.py" }), "us-west-2")).toBeUndefined(); - }); -}); - describe("addBoxBedrockTarget", () => { test("adds the bedrock target and its policy for a boxed agent that calls Bedrock", () => { const project = spec({ memories: [] }); @@ -269,30 +136,39 @@ describe("boxBedrockPolicy", () => { }); describe("addBoxGatewayTargets", () => { - test("adds the bedrock and agentcore targets a boxed agent's calls take, in the region given", () => { + test("adds the bedrock target a boxed agent's model calls take, in the region given", () => { const project = spec(); addBoxGatewayTargets(project, "eu-central-1"); - expect(gateway(project).targets).toEqual([ - getBoxGatewayBedrockTargetSpec("eu-central-1"), - getBoxGatewayAgentCoreTargetSpec("eu-central-1"), - ]); + expect(gateway(project).targets).toEqual([getBoxGatewayBedrockTargetSpec("eu-central-1")]); + }); + + test("adds no target and no policy for the default Memory: its calls go to Memory directly", () => { + const project = spec(); + project.runtimes[0]!.modelProvider = "OpenAI"; + + expect(addBoxGatewayTargets(project)).toEqual([]); + expect(targets(project)).toEqual([]); + expect(gateway(project).additionalPolicies).toBeUndefined(); }); test("moves a target it wrote for another region, and keeps one the user changed", () => { const project = spec(); addBoxGatewayTargets(project, "us-west-2"); - const memory = gateway(project).targets[1]!; - gateway(project).targets[1] = { - ...memory, - outboundAuth: { ...memory.outboundAuth!, region: "eu-west-1" }, - }; expect(addBoxGatewayTargets(project, "us-east-1")).toEqual([ `updating target 'bedrock' of gateway '${GATEWAY}' in agentcore.json from us-west-2 to us-east-1`, ]); - expect(gateway(project).targets[0]).toEqual(getBoxGatewayBedrockTargetSpec("us-east-1")); - expect(gateway(project).targets[1]!.outboundAuth?.region).toBe("eu-west-1"); + expect(gateway(project).targets).toEqual([getBoxGatewayBedrockTargetSpec("us-east-1")]); + + const bedrock = gateway(project).targets[0]!; + // The user signs for another service, so the target is no longer the one build wrote. + gateway(project).targets[0] = { + ...bedrock, + outboundAuth: { ...bedrock.outboundAuth!, service: "bedrock-runtime" }, + }; + expect(addBoxGatewayTargets(project, "eu-west-1")).toEqual([]); + expect(gateway(project).targets[0]!.outboundAuth?.region).toBe("us-east-1"); }); }); diff --git a/src/core/project/boxMemory.ts b/src/core/project/boxMemory.ts index fe0533d70..bd332c408 100644 --- a/src/core/project/boxMemory.ts +++ b/src/core/project/boxMemory.ts @@ -1,28 +1,16 @@ import { isDeepStrictEqual } from "node:util"; import { partition } from "@aws-sdk/util-endpoints"; -import { InputValidationError } from "../../errors/errors"; import { - BOX_GATEWAY_AGENTCORE_TARGET_NAME, DEFAULT_EGRESS_BEDROCK_REGION, callsBedrockModel, - getBoxGatewayAgentCoreTargetSpec, getBoxGatewayBedrockTargetSpec, } from "../../handlers/project/shortcuts"; import type { AgentCoreGateway, AgentCoreGatewayTarget } from "../../projectSchemas/gateway"; -import { isBoundGateway, type ProjectSpec } from "../../projectSchemas/project"; +import type { ProjectSpec } from "../../projectSchemas/project"; import { defaultMemoryName } from "../../projectSchemas/memory"; import type { ProjectRuntime } from "../../projectSchemas/runtime"; import { runtimeRunsInBox } from "./templates/boxProfile"; -/** The Box gateway's target, and the box route, that the agent's AgentCore Memory calls take. */ -export const BOX_MEMORY_TARGET = BOX_GATEWAY_AGENTCORE_TARGET_NAME; - -/** - * The policy file, relative to the project root, that lets the Box gateway's - * role call AgentCore Memory for the boxed agents. The CLI rewrites it on build. - */ -export const BOX_MEMORY_POLICY_FILE = "agentcore/box-memory-policy.json"; - /** * The policy file, relative to the project root, that lets the Box gateway's role call the Bedrock * model for the boxed agents: the gateway's `bedrock` target signs the model calls with that role, @@ -36,21 +24,6 @@ const BEDROCK_INVOKE_ACTIONS = ["bedrock:InvokeModel", "bedrock:InvokeModelWithR /** The IAM policy variable for the account of the gateway role. */ const PRINCIPAL_ACCOUNT = "${aws:PrincipalAccount}"; -/** The Memory data-plane actions the agent's session manager calls, as the L3 grants a runtime. */ -const MEMORY_DATA_PLANE_ACTIONS = [ - "bedrock-agentcore:ListMemoryRecords", - "bedrock-agentcore:RetrieveMemoryRecords", - "bedrock-agentcore:GetEvent", - "bedrock-agentcore:GetMemory", - "bedrock-agentcore:GetMemoryRecord", - "bedrock-agentcore:ListActors", - "bedrock-agentcore:ListEvents", - "bedrock-agentcore:ListSessions", - "bedrock-agentcore:CreateEvent", - "bedrock-agentcore:DeleteEvent", - "bedrock-agentcore:DeleteMemoryRecord", -]; - /** Whether the project has the default Memory create scaffolds for `runtime`. */ export function hasDefaultMemory(spec: ProjectSpec, runtime: ProjectRuntime): boolean { return spec.memories.some((memory) => memory.name === defaultMemoryName(runtime.name)); @@ -64,11 +37,6 @@ function boxedRuntimes(spec: ProjectSpec): ProjectRuntime[] { return spec.runtimes.filter((runtime) => runtimeRunsInBox(spec, runtime)); } -/** The runtimes of a Box project that run their agent in a box and have their default Memory. */ -function boxedRuntimesWithMemory(spec: ProjectSpec): ProjectRuntime[] { - return boxedRuntimes(spec).filter((runtime) => hasDefaultMemory(spec, runtime)); -} - /** * Whether a runtime's agent calls a Bedrock model, as its box profile routes it: a protocol other * than MCP, and the Bedrock provider (the default) or a `bedrock/` LiteLLM model. @@ -85,38 +53,6 @@ function boxedRuntimesCallingBedrock(spec: ProjectSpec): ProjectRuntime[] { return boxedRuntimes(spec).filter(callsBedrock); } -/** - * The IAM policy document for {@link BOX_MEMORY_POLICY_FILE}, or undefined when - * no boxed runtime has its default Memory. The Memory ARN is known only after - * deploy, so each resource matches the physical name the L3 gives the Memory, - * `__-`, in any target, in `region`, the region of - * the `agentcore` target that build writes, and in the partition of `region`. One file - * serves every deploy target, so the ARN names no account, and the condition holds the - * Memory to the gateway role's own account. - */ -export function boxMemoryPolicy( - spec: ProjectSpec, - region: string, -): Record | undefined { - const memories = boxedRuntimesWithMemory(spec).map(({ name }) => defaultMemoryName(name)); - if (memories.length === 0) return undefined; - return { - Version: "2012-10-17", - Statement: [ - { - Sid: "BoxedAgentMemoryThroughGateway", - Effect: "Allow", - Action: MEMORY_DATA_PLANE_ACTIONS, - Resource: memories.map( - (memory) => - `arn:${partition(region).name}:bedrock-agentcore:${region}:*:memory/${spec.name}_*${memory}-*`, - ), - Condition: { StringEquals: { "aws:ResourceAccount": "${aws:PrincipalAccount}" } }, - }, - ], - }; -} - /** * The IAM policy document for {@link BOX_BEDROCK_POLICY_FILE}, or undefined when no boxed runtime * calls a Bedrock model. Each ARN is in the partition of `region`, the region of the `bedrock` @@ -163,22 +99,6 @@ export function boxBedrockPolicy( }; } -/** - * Refuses a target named `name` on a gateway that is not a Box gateway: the Box gateways need the - * name for `calls`. - */ -function refuseTargetNameOutsideBoxGateways(spec: ProjectSpec, name: string, calls: string): void { - const owner = spec.agentCoreGateways.find( - (gateway) => !isBoundGateway(gateway) && gateway.targets.some((target) => target.name === name), - ); - if (owner) { - throw new InputValidationError( - `gateway '${owner.name}' has a target named '${name}', the name the Box gateways need for ` + - `${calls}. Rename that target.`, - ); - } -} - /** * Lists `policyFile` in `gateway`'s `additionalPolicies`, unless the gateway imports its role * (`executionRoleArn`), which is the user's to grant; the L3 warns about it at synth. @@ -197,32 +117,6 @@ function grantPolicyFile( ); } -/** - * Adds the `agentcore` passthrough target, in `region`, to the Box gateway (the gateway bound to - * the runtime) of each runtime that runs its agent in a box and has its default Memory, so the box - * can send the agent's Memory calls through the gateway, and lists {@link BOX_MEMORY_POLICY_FILE} - * in that gateway's `additionalPolicies` so its role may make them. Changes nothing outside a Box - * project. Changes `spec` in place, and returns the step messages; none when nothing changed. The - * target name on a gateway that is not a Box gateway is an error. - */ -export function addBoxMemoryTarget( - spec: ProjectSpec, - region: string = DEFAULT_EGRESS_BEDROCK_REGION, -): string[] { - const runtimes = boxedRuntimesWithMemory(spec); - if (runtimes.length === 0) return []; - refuseTargetNameOutsideBoxGateways(spec, BOX_MEMORY_TARGET, "the agent's Memory calls"); - - const messages: string[] = []; - for (const runtime of runtimes) { - const gateway = boxGatewayFor(spec, runtime); - if (!gateway) continue; - placeTarget(gateway, getBoxGatewayAgentCoreTargetSpec, region, messages, "Memory calls"); - grantPolicyFile(gateway, BOX_MEMORY_POLICY_FILE, messages, "Memory"); - } - return messages; -} - /** * Adds the `bedrock` passthrough target, in `region`, to the Box gateway of each runtime that runs * its agent in a box and calls a Bedrock model, so the Runtime profile has a route for the model @@ -246,14 +140,16 @@ export function addBoxBedrockTarget( } /** - * Adds the Box gateway targets that the boxed agents' calls need, the Bedrock model and the default - * Memory, in `region`, before build renders the Runtime profiles from those targets. + * Adds the Box gateway targets that the boxed agents' calls need, in `region`, before build renders + * the Runtime profiles from those targets: the Bedrock model's. The agent's Memory calls take no + * target: box signs them with the execution role and sends them to Memory itself, so Memory can + * hold each call to the session of the runtime (runtimeProfile/rules/additions.ts). */ export function addBoxGatewayTargets( spec: ProjectSpec, region: string = DEFAULT_EGRESS_BEDROCK_REGION, ): string[] { - return [...addBoxBedrockTarget(spec, region), ...addBoxMemoryTarget(spec, region)]; + return addBoxBedrockTarget(spec, region); } /** diff --git a/src/core/project/boxNetwork/network.test.ts b/src/core/project/boxNetwork/network.test.ts index 04e2b6dc9..c5c32a98e 100644 --- a/src/core/project/boxNetwork/network.test.ts +++ b/src/core/project/boxNetwork/network.test.ts @@ -1,8 +1,5 @@ import { describe, expect, test } from "bun:test"; -import { - getBoxGatewayAgentCoreTargetSpec, - getBoxGatewayBedrockTargetSpec, -} from "../../../handlers/project/shortcuts"; +import { getBoxGatewayBedrockTargetSpec } from "../../../handlers/project/shortcuts"; import type { AgentCoreGatewayTarget } from "../../../projectSchemas/gateway"; import { boxGatewaySpec, type ProjectSpec } from "../../../projectSchemas/project"; import type { ProjectRuntime } from "../../../projectSchemas/runtime"; @@ -903,18 +900,14 @@ describe("HTTP MCP servers", () => { }); describe("the default targets", () => { - test("gives the Memory endpoint to the agentcore target build adds, and no egress- target", () => { - const r = model( - "", - { - memory: { - destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], - secret: { ref: "aws://default" }, - }, + test("gives the Memory endpoint of an entry an egress- target: build adds no target for Memory", () => { + const r = model("", { + memory: { + destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], + secret: { ref: "aws://default" }, }, - { targets: [getBoxGatewayAgentCoreTargetSpec()] }, - ); - expect(r.targets()).toEqual(["bedrock-agentcore.us-west-2.amazonaws.com -> agentcore"]); + }); + expect(r.targets()).toEqual(["bedrock-agentcore.us-west-2.amazonaws.com -> egress-memory"]); expect(r.failures()).toEqual([]); }); @@ -939,14 +932,14 @@ describe("the default targets", () => { const r = model( "", { - memory: { - destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], + models: { + destinations: ["bedrock-runtime.us-west-2.amazonaws.com"], secret: { ref: "env://KEY" }, }, }, - { targets: [getBoxGatewayAgentCoreTargetSpec()] }, + { targets: [getBoxGatewayBedrockTargetSpec()] }, ); - expect(r.failures()).toEqual([["EXISTING_TARGET_CONFLICT", "memory"]]); + expect(r.failures()).toEqual([["EXISTING_TARGET_CONFLICT", "models"]]); }); }); @@ -1029,9 +1022,11 @@ describe("warnings", () => { const control = passthrough("egress-control", "https://bedrock.us-west-2.amazonaws.com", { outboundAuth: { type: "GATEWAY_IAM_ROLE", service: "bedrock", region: "us-west-2" }, }); - const memory = getBoxGatewayAgentCoreTargetSpec(); - const r = model("", egress, { targets: [control, memory] }); - r.spec.agentCoreGateways[0]!.additionalPolicies = ["agentcore/box-memory-policy.json"]; + // A Memory target of the user: build writes no policy file for one. + const memory = passthrough("agentcore", "https://bedrock-agentcore.us-west-2.amazonaws.com", { + outboundAuth: { type: "GATEWAY_IAM_ROLE", service: "bedrock-agentcore", region: "us-west-2" }, + }); + const r = model("", egress, { targets: [control] }); expect(boxGatewayIamWarnings(r.spec, r.network)).toEqual([ "Target 'egress-control' calls bedrock with the gateway's role, which has no bedrock " + "permissions. Add an IAM policy file to additionalPolicies of the 'agent-box-egress' gateway.", @@ -1064,6 +1059,7 @@ describe("warnings", () => { describe("the grant each endpoint of a target needs", () => { const BEDROCK_FILE = "agentcore/box-bedrock-policy.json"; + /** The Memory policy file that build wrote before Memory calls went to Memory directly. */ const MEMORY_FILE = "agentcore/box-memory-policy.json"; const signed = (name: string, host: string, service: string) => passthrough(name, `https://${host}`, { @@ -1151,21 +1147,23 @@ describe("warnings", () => { expect(warningsFor(functionUrl, ["bedrock:*"])).toEqual([forService("egress-fn", "lambda")]); }); - test("an AgentCore target needs an AgentCore grant, and the Memory file covers only Memory", () => { - expect(warningsFor(getBoxGatewayAgentCoreTargetSpec(), [], [MEMORY_FILE])).toEqual([]); - expect(warningsFor(agentcoreControl, [], [MEMORY_FILE])).toEqual([ - forHost("egress-agentcore-control", AGENTCORE_CONTROL_HOST, "bedrock-agentcore"), - ]); - expect(warningsFor(agentcoreControl, ["bedrock-agentcore:*"], [MEMORY_FILE])).toEqual([]); - expect(warningsFor(getBoxGatewayAgentCoreTargetSpec("us-east-1"), [], [MEMORY_FILE])).toEqual( - [forHost("agentcore", "bedrock-agentcore.us-east-1.amazonaws.com", "bedrock-agentcore")], + test("an AgentCore target needs an AgentCore grant, and a Memory file of an earlier build grants none", () => { + const memory = signed( + "agentcore", + "bedrock-agentcore.us-west-2.amazonaws.com", + "bedrock-agentcore", ); + expect(warningsFor(memory, [], [MEMORY_FILE])).toEqual([ + forService("agentcore", "bedrock-agentcore"), + ]); + expect(warningsFor(memory, ["bedrock-agentcore:*"])).toEqual([]); + expect(warningsFor(agentcoreControl, ["bedrock-agentcore:*"])).toEqual([]); expect(warningsFor(agentcoreControl)).toEqual([ forService("egress-agentcore-control", "bedrock-agentcore"), ]); }); - test("the Bedrock and Memory files grant the amazonaws.com.cn hosts of a build for China", () => { + test("the Bedrock file grants the amazonaws.com.cn host of a build for China", () => { const china = (name: string, host: string, service: string) => passthrough(name, `https://${host}`, { outboundAuth: { type: "GATEWAY_IAM_ROLE", service, region: "cn-north-1" }, @@ -1176,15 +1174,10 @@ describe("warnings", () => { { targets: [ china("egress-models", "bedrock-runtime.cn-north-1.amazonaws.com.cn", "bedrock"), - china( - "egress-memory", - "bedrock-agentcore.cn-north-1.amazonaws.com.cn", - "bedrock-agentcore", - ), ], }, ); - r.spec.agentCoreGateways[0]!.additionalPolicies = [BEDROCK_FILE, MEMORY_FILE]; + r.spec.agentCoreGateways[0]!.additionalPolicies = [BEDROCK_FILE]; expect(boxGatewayIamWarnings(r.spec, { ...r.network, region: "cn-north-1" })).toEqual([]); }); diff --git a/src/core/project/boxNetwork/network.ts b/src/core/project/boxNetwork/network.ts index 49d72b050..710d55a50 100644 --- a/src/core/project/boxNetwork/network.ts +++ b/src/core/project/boxNetwork/network.ts @@ -4,14 +4,11 @@ * the policy files to stage, and every warning and failure. Build, deploy, and staging write from * it. */ -import { - boxGatewayAgentCoreHost, - boxGatewayBedrockHost, -} from "../../../handlers/project/shortcuts"; +import { boxGatewayBedrockHost } from "../../../handlers/project/shortcuts"; import type { AgentCoreGateway, AgentCoreGatewayTarget } from "../../../projectSchemas/gateway"; import type { ProjectSpec } from "../../../projectSchemas/project"; import type { ProjectRuntime } from "../../../projectSchemas/runtime"; -import { BOX_BEDROCK_POLICY_FILE, BOX_MEMORY_POLICY_FILE } from "../boxMemory"; +import { BOX_BEDROCK_POLICY_FILE } from "../boxMemory"; import { boxEgressCredentialName } from "../boxProfileCredentials"; import type { BoxEnvRoute } from "../boxProfileEgress"; import { buildHostMap, entryLabel, entryRef, type MappedHost } from "./hostMap"; @@ -108,7 +105,7 @@ export interface BoxNetwork { readonly runtime: string; /** The runtime's Box gateway, as agentcore.json names it. */ readonly gateway: string; - /** The region build builds for: of the Box gateway, its `bedrock` and `agentcore` targets. */ + /** The region build builds for: of the Box gateway and its `bedrock` target. */ readonly region: string; /** * The `[egress]` entries' hosts in profile order, then the HTTP `[mcp]` servers' hosts, then the @@ -151,8 +148,8 @@ export interface BoxNetworkProject { /** The region build builds for. */ readonly region: string; /** - * The gateway's targets, with the `bedrock` and `agentcore` targets that build adds for the - * runtime's model and Memory calls (`addBoxGatewayTargets`). + * The gateway's targets, with the `bedrock` target that build adds for the runtime's model calls + * (`addBoxGatewayTargets`). */ readonly targets: readonly AgentCoreGatewayTarget[]; /** The IAM actions that the gateway's `additionalPolicies` files grant, except build's own. */ @@ -603,9 +600,8 @@ function sourceLabel(source: EntrySource): string { /** * A warning for each target of the network's Box gateway in `spec`, as build writes it, that the * gateway signs with its own role for an endpoint that no policy of that role allows. The Bedrock - * policy file that build writes allows the Bedrock runtime host of the build region, and the Memory - * policy file the Memory host of that region; a gateway that imports its role is the user's to - * grant. + * policy file that build writes allows the Bedrock runtime host of the build region; a gateway that + * imports its role is the user's to grant. */ export function boxGatewayIamWarnings( spec: ProjectSpec, @@ -613,18 +609,15 @@ export function boxGatewayIamWarnings( ): string[] { const gateway = boxGatewayOf(spec, network.runtime); if (!gateway || gateway.executionRoleArn) return []; - // Build writes these two files, so each one counts when it is listed, not by its actions. + // Build writes this file, so it counts when it is listed, not by its actions. const listed = gateway.additionalPolicies ?? []; - const memoryListed = listed.includes(BOX_MEMORY_POLICY_FILE); const bedrockListed = listed.includes(BOX_BEDROCK_POLICY_FILE); const granted = network.grantedActions; const grantsService = (service: string) => granted.some((action) => action === "*" || action.toLowerCase().startsWith(`${service}:`)); /** Whether the role has a permission for `service`, whatever its endpoint. */ const holds = (service: string) => - (service === "bedrock" && bedrockListed) || - (service === "bedrock-agentcore" && memoryListed) || - grantsService(service); + (service === "bedrock" && bedrockListed) || grantsService(service); const grants = (action: string) => granted.some((pattern) => actionMatches(pattern, action)); const allows = (api: AwsEndpointApi, host: string): boolean => { switch (api) { @@ -636,10 +629,7 @@ export function boxGatewayIamWarnings( case "bedrock-control": return granted.some(grantsBedrockControlPlane); case "bedrock-agentcore": - return ( - (memoryListed && host === boxGatewayAgentCoreHost(network.region)) || - grantsService("bedrock-agentcore") - ); + return grantsService("bedrock-agentcore"); case "lambda-url": return grants("lambda:InvokeFunctionUrl"); } diff --git a/src/core/project/boxNetworkSource.test.ts b/src/core/project/boxNetworkSource.test.ts index 85151893e..771072052 100644 --- a/src/core/project/boxNetworkSource.test.ts +++ b/src/core/project/boxNetworkSource.test.ts @@ -503,12 +503,12 @@ describe("the IAM policy files of the Box gateway", () => { ]); }); - test("gives no warning for the Memory policy file that build writes before it writes it", async () => { + test("gives no warning for the Bedrock policy file that build writes before it writes it", async () => { const root = await project([]); const spec = { ...SPEC, agentCoreGateways: [ - { ...boxGatewaySpec("agent"), additionalPolicies: ["agentcore/box-memory-policy.json"] }, + { ...boxGatewaySpec("agent"), additionalPolicies: ["agentcore/box-bedrock-policy.json"] }, ], } as unknown as ProjectSpec; @@ -523,15 +523,9 @@ describe("the IAM policy files of the Box gateway", () => { await mkdir(join(root, "agentcore")); const grant = (action: string) => JSON.stringify({ Statement: [{ Effect: "Allow", Action: [action] }] }); - await writeFile( - join(root, "agentcore", "box-memory-policy.json"), - grant("bedrock-agentcore:GetEvent"), - ); await writeFile(join(root, "agentcore", "box-bedrock-policy.json"), "{ not json"); await writeFile(join(root, "agentcore", "grants.json"), grant("lambda:InvokeFunctionUrl")); - const listed = ["box-memory-policy", "box-bedrock-policy", "grants"].map( - (name) => `agentcore/${name}.json`, - ); + const listed = ["box-bedrock-policy", "grants"].map((name) => `agentcore/${name}.json`); const spec = { ...SPEC, agentCoreGateways: [{ ...boxGatewaySpec("agent"), additionalPolicies: listed }], diff --git a/src/core/project/boxNetworkSource.ts b/src/core/project/boxNetworkSource.ts index 0b18cb0c2..a7817002a 100644 --- a/src/core/project/boxNetworkSource.ts +++ b/src/core/project/boxNetworkSource.ts @@ -13,7 +13,7 @@ import type { ProjectRuntime } from "../../projectSchemas/runtime"; import { InvalidEnvironmentError, ProjectStateError } from "../../errors/errors"; import type { PolicyFileCopy } from "./backends/boxContainer"; import { DEFAULT_EGRESS_BEDROCK_REGION } from "../../handlers/project/shortcuts"; -import { addBoxGatewayTargets, BOX_BEDROCK_POLICY_FILE, BOX_MEMORY_POLICY_FILE } from "./boxMemory"; +import { addBoxGatewayTargets, BOX_BEDROCK_POLICY_FILE } from "./boxMemory"; import { BOX_BEDROCK_PACK, boxLocalDir, @@ -59,8 +59,8 @@ export async function readBoxNetworks( const runtimes = spec.runtimes.filter((runtime) => runtimeRunsInBox(spec, runtime)); if (runtimes.length === 0) return []; assertBoxRuntimesHaveProfiles(projectRoot, runtimes); - // The model sees the targets build adds for the model and Memory calls, so no host takes a - // second target for their endpoints. + // The model sees the target build adds for the model calls, so no host takes a second target for + // its endpoint. const withDefaults = structuredClone(spec); addBoxGatewayTargets(withDefaults, region); const networks: BoxNetwork[] = []; @@ -420,10 +420,7 @@ function policyFileProblem( } /** The policy files that build writes for the gateway's role. */ -const BUILD_POLICY_FILES: ReadonlySet = new Set([ - BOX_BEDROCK_POLICY_FILE, - BOX_MEMORY_POLICY_FILE, -]); +const BUILD_POLICY_FILES: ReadonlySet = new Set([BOX_BEDROCK_POLICY_FILE]); /** * The actions that the Allow statements of the `.json` policy files of `policies` grant, and a diff --git a/src/core/project/boxNetworkWriters.test.ts b/src/core/project/boxNetworkWriters.test.ts index c1d957ba7..f4ef78e7f 100644 --- a/src/core/project/boxNetworkWriters.test.ts +++ b/src/core/project/boxNetworkWriters.test.ts @@ -1,9 +1,8 @@ import { describe, expect, test } from "bun:test"; -import { getBoxGatewayAgentCoreTargetSpec } from "../../handlers/project/shortcuts"; import type { AgentCoreGatewayTarget } from "../../projectSchemas/gateway"; import { boxGatewaySpec, type ProjectSpec } from "../../projectSchemas/project"; import type { BoxNetwork, BoxNetworkHost } from "./boxNetwork"; -import { BOX_BEDROCK_POLICY_FILE, BOX_MEMORY_POLICY_FILE } from "./boxMemory"; +import { BOX_BEDROCK_POLICY_FILE } from "./boxMemory"; import { BOX_NETWORK_WRITERS, boxNetworkEdits, @@ -273,7 +272,7 @@ describe("credentialsWriter", () => { }); describe("defaultTargetsWriter", () => { - test("adds the model and Memory targets and writes the role policies, as addBoxGatewayTargets does", () => { + test("adds the model target and writes its role policy, and none for the default Memory, as addBoxGatewayTargets does", () => { const project = spec(); project.memories.push({ name: "agentMemory", @@ -283,34 +282,9 @@ describe("defaultTargetsWriter", () => { const edits = run([defaultTargetsWriter], [network()], project); - expect(targetNames(edits.spec)).toEqual(["mine", "bedrock", "agentcore"]); - expect(edits.files.map(({ path }) => path)).toEqual([ - BOX_BEDROCK_POLICY_FILE, - BOX_MEMORY_POLICY_FILE, - ]); - }); - - test("writes the Memory policy for the region of the agentcore target it writes", () => { - const project = spec(); - project.memories.push({ - name: "agentMemory", - eventExpiryDuration: 30, - strategies: [], - } as never); - - const edits = run([defaultTargetsWriter], [network({ region: "eu-central-1" })], project); - - const agentcore = edits.spec.agentCoreGateways[0]!.targets.find( - ({ name }) => name === "agentcore", - ); - expect(agentcore?.outboundAuth?.region).toBe("eu-central-1"); - const memory = edits.files.find(({ path }) => path === BOX_MEMORY_POLICY_FILE); - expect(JSON.parse((memory as { text: string }).text).Statement).toEqual([ - expect.objectContaining({ - Resource: ["arn:aws:bedrock-agentcore:eu-central-1:*:memory/demo_*agentMemory-*"], - Condition: { StringEquals: { "aws:ResourceAccount": "${aws:PrincipalAccount}" } }, - }), - ]); + // The agent's Memory calls go to Memory directly, so they take no target and no gateway policy. + expect(targetNames(edits.spec)).toEqual(["mine", "bedrock"]); + expect(edits.files.map(({ path }) => path)).toEqual([BOX_BEDROCK_POLICY_FILE]); }); test("writes the Bedrock policy for the region of the bedrock target it writes", () => { @@ -329,40 +303,24 @@ describe("defaultTargetsWriter", () => { ]); }); - test("writes the model and Memory targets for the region of the build", () => { - const project = spec(); - project.memories.push({ - name: "agentMemory", - eventExpiryDuration: 30, - strategies: [], - } as never); - - const edits = run([defaultTargetsWriter], [network({ region: "eu-central-1" })], project); + test("writes the model target for the region of the build", () => { + const edits = run([defaultTargetsWriter], [network({ region: "eu-central-1" })]); const regions = edits.spec.agentCoreGateways[0]!.targets.map( ({ name, outboundAuth }) => `${name}:${outboundAuth?.region}`, ); - expect(regions).toEqual(["mine:undefined", "bedrock:eu-central-1", "agentcore:eu-central-1"]); + expect(regions).toEqual(["mine:undefined", "bedrock:eu-central-1"]); }); test("adds no default target for an endpoint a target serves already", () => { const project = spec([ - { ...getBoxGatewayAgentCoreTargetSpec(), name: "mem" }, passthrough("egress-models", "https://bedrock-runtime.us-west-2.amazonaws.com"), ]); - project.memories.push({ - name: "agentMemory", - eventExpiryDuration: 30, - strategies: [], - } as never); const edits = run([defaultTargetsWriter], [network()], project); - expect(targetNames(edits.spec)).toEqual(["mem", "egress-models"]); - expect(edits.spec.agentCoreGateways[0]!.additionalPolicies).toEqual([ - BOX_BEDROCK_POLICY_FILE, - BOX_MEMORY_POLICY_FILE, - ]); + expect(targetNames(edits.spec)).toEqual(["egress-models"]); + expect(edits.spec.agentCoreGateways[0]!.additionalPolicies).toEqual([BOX_BEDROCK_POLICY_FILE]); }); }); diff --git a/src/core/project/boxNetworkWriters.ts b/src/core/project/boxNetworkWriters.ts index 88bdbbe73..e47b0288c 100644 --- a/src/core/project/boxNetworkWriters.ts +++ b/src/core/project/boxNetworkWriters.ts @@ -19,13 +19,7 @@ import { type BoxNetwork, type BoxNetworkHost, } from "./boxNetwork"; -import { - addBoxGatewayTargets, - BOX_BEDROCK_POLICY_FILE, - BOX_MEMORY_POLICY_FILE, - boxBedrockPolicy, - boxMemoryPolicy, -} from "./boxMemory"; +import { addBoxGatewayTargets, BOX_BEDROCK_POLICY_FILE, boxBedrockPolicy } from "./boxMemory"; import { planBoxProfileCredentials, type BoxKeySources } from "./boxProfileCredentials"; import type { BoxEnvRoute } from "./boxProfileEgress"; import { @@ -284,16 +278,12 @@ function undeclaredKey( } /** - * The `bedrock` and `agentcore` targets that each box runtime's model and Memory calls take, and - * the Bedrock and Memory policies of the gateways' role, as `addBoxGatewayTargets` adds them. + * The `bedrock` target that each box runtime's model calls take, and the Bedrock policy of the + * gateways' role, as `addBoxGatewayTargets` adds them. */ export const defaultTargetsWriter: BoxNetworkWriter = { name: "default targets", - writes: () => [ - { kind: "defaultTargets" }, - { kind: "file", path: BOX_BEDROCK_POLICY_FILE }, - { kind: "file", path: BOX_MEMORY_POLICY_FILE }, - ], + writes: () => [{ kind: "defaultTargets" }, { kind: "file", path: BOX_BEDROCK_POLICY_FILE }], edits: (networks, { spec }) => { // Every network of one build has the region build builds for. const region = networks[0]?.region; @@ -304,10 +294,6 @@ export const defaultTargetsWriter: BoxNetworkWriter = { path: BOX_BEDROCK_POLICY_FILE, policy: region === undefined ? undefined : boxBedrockPolicy(spec, region), }, - { - path: BOX_MEMORY_POLICY_FILE, - policy: region === undefined ? undefined : boxMemoryPolicy(spec, region), - }, ].flatMap(({ path, policy }) => policy ? [{ path, text: `${JSON.stringify(policy, undefined, 2)}\n` }] : [], ), diff --git a/src/core/project/manager.boxNetwork.test.ts b/src/core/project/manager.boxNetwork.test.ts index 91fa06fb8..3a89f3044 100644 --- a/src/core/project/manager.boxNetwork.test.ts +++ b/src/core/project/manager.boxNetwork.test.ts @@ -10,6 +10,7 @@ import type { Project } from "../../handlers/project/types"; import { FsReadWriteJson, type ReadWriteJson } from "../../io"; import { createSilentLogger, matchGolden, TestIdentityClient } from "../../testing"; import type { DeployBackendInput, ProjectBackend } from "./backends/types"; +import type { BoxNetwork } from "./boxNetwork"; import { boxShowOf, FakeBoxProfileResolver } from "./boxProfileResolver-test-support"; import type { ResolvedBoxProfile } from "./boxProfileResolver"; import { seedBoxCache } from "./backends/box-release-test-support"; @@ -255,13 +256,9 @@ describe("build writes the Box network of the box profile", () => { const { steps } = await build(manager, root); expect(counter.specWrites).toBe(1); - // create declares no target, so build adds the egress targets first, then bedrock and Memory. - expect(await targetNames(root)).toEqual([ - "egress-github", - "egress-httpbin-org", - "bedrock", - "agentcore", - ]); + // create declares no target, so build adds the egress targets first, then bedrock. The agent's + // Memory calls take no target: they go to Memory directly. + expect(await targetNames(root)).toEqual(["egress-github", "egress-httpbin-org", "bedrock"]); const { targets } = await boxGateway(root); expect(targets.slice(0, 2)).toEqual([ userTarget("egress-github", "https://api.github.com", { outboundAuth: GITHUB_KEY }), @@ -289,6 +286,11 @@ describe("build writes the Box network of the box profile", () => { ]); expect(egress.github).toEqual(gatewayRoute("egress-github", "api.github.com")); expect(egress["egress-httpbin-org"]).toEqual(gatewayRoute("egress-httpbin-org", "httpbin.org")); + // The Memory route has no backend: box signs it with the execution role, for Memory itself. + expect(egress.agentcore).toEqual({ + destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], + secret: { ref: "aws://" }, + }); expect(egress.bedrock).toEqual( gatewayRoute("bedrock", "bedrock-runtime.us-west-2.amazonaws.com"), ); @@ -348,7 +350,7 @@ describe("build writes the Box network of the box profile", () => { const { steps } = await build(manager, root); - expect(await targetNames(root)).toEqual(["bedrock", "agentcore"]); + expect(await targetNames(root)).toEqual(["bedrock"]); expect(JSON.parse(await text(root, SPEC)).credentials ?? []).toEqual([]); expect(steps).toContain( `not adding gateway target 'egress-github' of '${GATEWAY}' for api.github.com: it sends ` + @@ -398,7 +400,7 @@ describe("build writes the Box network of the box profile", () => { const { steps, warnings } = await build(manager, root); - expect(await targetNames(root)).toEqual(["egress-tools", "bedrock", "agentcore"]); + expect(await targetNames(root)).toEqual(["egress-tools", "bedrock"]); const { targets } = await boxGateway(root); // A CUSTOM passthrough target, so Box alone decides each mcp:call. expect(targets[0]).toEqual( @@ -438,7 +440,7 @@ describe("build writes the Box network of the box profile", () => { const { steps } = await build(manager, root); - expect(await targetNames(root)).toEqual(["bedrock", "agentcore"]); + expect(await targetNames(root)).toEqual(["bedrock"]); expect(steps).toContain( `not adding gateway target 'egress-tools' of '${GATEWAY}' for tools.example.com: it sends ` + "the key of env://TOOLS_TOKEN, and agentcore.json has no API key credential 'tools' " + @@ -461,7 +463,7 @@ describe("build writes the Box network of the box profile", () => { await build(manager, root); - expect(await targetNames(root)).toEqual(["gh", "weather", "bedrock", "agentcore"]); + expect(await targetNames(root)).toEqual(["gh", "weather", "bedrock"]); const egress = (await runtimeProfile(root)).egress as Record; expect(egress.github).toEqual(gatewayRoute("gh", "api.github.com")); }); @@ -477,14 +479,14 @@ describe("build writes the Box network of the box profile", () => { const { steps } = await build(manager, root); - expect(await targetNames(root)).toEqual(["bedrock", "agentcore"]); + expect(await targetNames(root)).toEqual(["bedrock"]); expect(steps).toContain( `removing gateway target 'egress-github' from '${GATEWAY}': the Box profile no longer ` + "allows api.github.com", ); }); - test("gives the Memory endpoint to the agentcore target, and adds no second target for it", async () => { + test("routes an entry of the profile for the Memory host through the gateway, and adds no Memory route", async () => { const { manager, root, box } = await strandsProject(); await editProfile(root, { tables: @@ -500,12 +502,12 @@ describe("build writes the Box network of the box profile", () => { await build(manager, root); - expect(await targetNames(root)).toEqual(["bedrock", "agentcore"]); + expect(await targetNames(root)).toEqual(["egress-memory", "bedrock"]); const egress = (await runtimeProfile(root)).egress as Record; - // The route of [egress.memory] keeps the name of its entry, and goes to the agentcore target. + // The user's [egress.memory] keeps the host, so the box has one route to it: to its target. expect(Object.keys(egress)).toEqual(["bedrock", "memory"]); expect(egress.memory).toEqual( - gatewayRoute("agentcore", "bedrock-agentcore.us-west-2.amazonaws.com"), + gatewayRoute("egress-memory", "bedrock-agentcore.us-west-2.amazonaws.com"), ); }); @@ -890,7 +892,7 @@ describe("build reads the box profile as box resolves it", () => { }); }); -describe("build writes the Bedrock and Memory targets for the region it builds for", () => { +describe("build writes the Bedrock target and the Memory route for the region it builds for", () => { const bedrockTarget = (region: string) => ({ name: "bedrock", targetType: "passthrough", @@ -908,10 +910,7 @@ describe("build writes the Bedrock and Memory targets for the region it builds f const { targets } = await boxGateway(root); expect(targets.find(({ name }) => name === "bedrock")).toEqual(bedrockTarget("eu-central-1")); - expect(targets.find(({ name }) => name === "agentcore")).toMatchObject({ - passthrough: { endpoint: "https://bedrock-agentcore.eu-central-1.amazonaws.com" }, - outboundAuth: { region: "eu-central-1" }, - }); + expect(targets.find(({ name }) => name === "agentcore")).toBeUndefined(); const profile = await runtimeProfile(root); expect(profile.options).toEqual({ bedrock: { region: "eu-central-1" } }); const egress = profile.egress as Record; @@ -920,6 +919,11 @@ describe("build writes the Bedrock and Memory targets for the region it builds f expect(JSON.stringify(egress.bedrock!.backends)).toContain( '"region":"${{ inputs.aws.region }}"', ); + // The Memory route goes to the Memory host of that region, with no backend. + expect(egress.agentcore as unknown).toEqual({ + destinations: ["bedrock-agentcore.eu-central-1.amazonaws.com"], + secret: { ref: "aws://" }, + }); }); test("has box resolve the profile for that region, from a copy it then removes", async () => { @@ -931,7 +935,7 @@ describe("build writes the Bedrock and Memory targets for the region it builds f // @box/bedrock's route is for eu-central-1, so the bedrock target serves it, and no // egress-bedrock target is added for the us-west-2 host that `agentcore dev` calls. - expect(await targetNames(root)).toEqual(["bedrock", "agentcore"]); + expect(await targetNames(root)).toEqual(["bedrock"]); const [call] = box.calls; expect(isAbsolute(call!.profilePath)).toBe(true); expect(existsSync(call!.profilePath)).toBe(false); @@ -978,26 +982,13 @@ describe("build writes the Bedrock and Memory targets for the region it builds f expect(targets.find(({ name }) => name === "bedrock")).toEqual(bedrockTarget("eu-west-2")); }); - test("moves a target it wrote for another region, and keeps one the user changed", async () => { + test("moves a target it wrote for another region", async () => { const { manager, root } = await strandsProject(); await build(manager, root); await editGateway(root, (gateway) => { - gateway.targets = gateway.targets.map((target) => { - const { name } = target as { name: string }; - if (name === "bedrock") return bedrockTarget("us-east-1"); - if (name === "agentcore") { - return { - ...(target as object), - passthrough: { endpoint: "https://memory.example.com", protocolType: "CUSTOM" }, - outboundAuth: { - type: "GATEWAY_IAM_ROLE", - service: "bedrock-agentcore", - region: "eu-west-1", - }, - }; - } - return target; - }); + gateway.targets = gateway.targets.map((target) => + (target as { name: string }).name === "bedrock" ? bedrockTarget("us-east-1") : target, + ); }); const { steps } = await build(manager, root); @@ -1007,9 +998,6 @@ describe("build writes the Bedrock and Memory targets for the region it builds f expect(steps).toContain( `updating target 'bedrock' of gateway '${GATEWAY}' in agentcore.json from us-east-1 to us-west-2`, ); - expect(targets.find(({ name }) => name === "agentcore")).toMatchObject({ - passthrough: { endpoint: "https://memory.example.com" }, - }); }); }); @@ -1053,11 +1041,7 @@ describe("deploy writes the Box network before the backend deploys", () => { expect(calls).toHaveLength(1); const { spec } = calls[0]!.project; const gateway = spec.agentCoreGateways.find(({ name }) => name === GATEWAY); - expect(gateway?.targets.map(({ name }) => name)).toEqual([ - "egress-github", - "bedrock", - "agentcore", - ]); + expect(gateway?.targets.map(({ name }) => name)).toEqual(["egress-github", "bedrock"]); // Deploy writes the targets for its target's region, not the AWS_REGION build would take. expect(gateway?.targets.find(({ name }) => name === "bedrock")?.outboundAuth?.region).toBe( "eu-west-1", @@ -1071,10 +1055,16 @@ describe("deploy writes the Box network before the backend deploys", () => { expect(await text(root, "agentcore/.env.local")).toMatch(/^AGENTCORE_CREDENTIAL_GITHUB=./m); }); - test("moves the Memory and Bedrock policies with their targets to the region of the targets", async () => { + test("moves the Bedrock policy with its target, and the Memory route, to the region of the targets", async () => { + // The networks build and deploy hand the backend, which this one does not stage. + const networks: (readonly BoxNetwork[])[] = []; const backend: ProjectBackend = { - async *build() {}, - async *deploy() { + async *build(_project, input) { + networks.push(input?.boxNetworks ?? []); + yield { type: "step", message: "building" }; + }, + async *deploy(_project, input) { + networks.push(input.boxNetworks); yield { type: "step", message: "deploying" }; return { outputs: {} }; }, @@ -1086,31 +1076,28 @@ describe("deploy writes the Box network before the backend deploys", () => { }, }; const { manager, root } = await strandsProject({ backends: { CDK: backend } }); - const memoryStatements = async () => - JSON.parse(await text(root, "agentcore/box-memory-policy.json")).Statement as unknown[]; + const memoryRoute = () => + (networks.at(-1)![0]!.runtimeProfile!.egress as Record).agentcore; const bedrockResources = async () => JSON.parse(await text(root, "agentcore/box-bedrock-policy.json")).Statement.map( ({ Resource }: { Resource: string[] }) => Resource, ); const authOf = async (target: string) => (await boxGateway(root)).targets.find(({ name }) => name === target)?.outboundAuth; - const statementsFor = (region: string) => [ - expect.objectContaining({ - Resource: [ - `arn:aws:bedrock-agentcore:${region}:*:memory/demo_*agent_python_strandsMemory-*`, - ], - Condition: { StringEquals: { "aws:ResourceAccount": "${aws:PrincipalAccount}" } }, - }), - ]; + const routeFor = (region: string) => ({ + destinations: [`bedrock-agentcore.${region}.amazonaws.com`], + secret: { ref: "aws://" }, + }); const bedrockFor = (region: string) => [ [`arn:aws:bedrock:${region}:*:inference-profile/*`], [`arn:aws:bedrock:${region}::foundation-model/*`], ["arn:aws:bedrock:*::foundation-model/*"], ]; await build(manager, root); - expect(await authOf("agentcore")).toMatchObject({ region: "us-west-2" }); + expect(await authOf("agentcore")).toBeUndefined(); expect(await authOf("bedrock")).toMatchObject({ region: "us-west-2" }); - expect(await memoryStatements()).toEqual(statementsFor("us-west-2")); + expect(existsSync(path(root, "agentcore/box-memory-policy.json"))).toBe(false); + expect(memoryRoute()).toEqual(routeFor("us-west-2")); expect(await bedrockResources()).toEqual(bedrockFor("us-west-2")); await writeFile( path(root, "agentcore/aws-targets.json"), @@ -1126,9 +1113,8 @@ describe("deploy writes the Box network before the backend deploys", () => { }), ); - expect(await authOf("agentcore")).toMatchObject({ region: "eu-west-1" }); expect(await authOf("bedrock")).toMatchObject({ region: "eu-west-1" }); - expect(await memoryStatements()).toEqual(statementsFor("eu-west-1")); + expect(memoryRoute()).toEqual(routeFor("eu-west-1")); expect(await bedrockResources()).toEqual(bedrockFor("eu-west-1")); }); }); diff --git a/src/core/project/manager.test.ts b/src/core/project/manager.test.ts index 99f66dc18..2960ab1c3 100644 --- a/src/core/project/manager.test.ts +++ b/src/core/project/manager.test.ts @@ -1,4 +1,5 @@ import { afterEach, describe, expect, test } from "bun:test"; +import { parse } from "smol-toml"; import { existsSync, readdirSync } from "node:fs"; import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises"; import { writeMigrationState } from "./migrate/journal"; @@ -2105,8 +2106,9 @@ describe("FsProjectManager.build", () => { cwd: join(directory, "example", "agentcore", "cdk"), }, ]); - // Build adds the Bedrock and Memory targets, and grants the Box gateway's role each, since the - // gateway signs the agent's calls with that role. + // Build adds the Bedrock target, and grants it to the Box gateway's role, since the gateway signs + // the agent's model calls with that role. The Memory calls take no target: they go to Memory + // directly, signed with the execution role. expect(events).toEqual([ { type: "step", @@ -2118,18 +2120,7 @@ describe("FsProjectManager.build", () => { message: "adding 'agentcore/box-bedrock-policy.json' to gateway 'agent-python-strands-box-box-egress' additionalPolicies in agentcore.json, so its role can call the Bedrock model", }, - { - type: "step", - message: - "adding target 'agentcore' to gateway 'agent-python-strands-box-box-egress' in agentcore.json, for the boxed agent's Memory calls", - }, - { - type: "step", - message: - "adding 'agentcore/box-memory-policy.json' to gateway 'agent-python-strands-box-box-egress' additionalPolicies in agentcore.json, so its role can call Memory", - }, { type: "step", message: "writing agentcore/box-bedrock-policy.json" }, - { type: "step", message: "writing agentcore/box-memory-policy.json" }, { type: "step", message: "synthesizing CloudFormation templates" }, ]); // The zip carries box and the Runtime profile in the code folder's .box-dist. @@ -2184,7 +2175,7 @@ describe("FsProjectManager.build", () => { expect(commands).toEqual([]); }); - test("routes a boxed agent's default Memory through the Box gateway before synth", async () => { + test("sends a boxed agent's default Memory calls to Memory directly, not through the Box gateway", async () => { const directory = await inTempDirectory(); const { manager: subject } = manager(await boxHome()); const { project } = await runCreate(subject, { @@ -2205,22 +2196,27 @@ describe("FsProjectManager.build", () => { const box = spec.agentCoreGateways.find( ({ name }: { name: string }) => name === boxGatewayName, ); - expect(box.targets.map(({ name }: { name: string }) => name)).toEqual(["bedrock", "agentcore"]); - expect(box.additionalPolicies).toEqual([ - "agentcore/box-bedrock-policy.json", - "agentcore/box-memory-policy.json", - ]); - expect(existsSync(join(root, "agentcore", "box-memory-policy.json"))).toBe(true); - expect(events).toContainEqual({ + // The gateway gets no Memory target, and its role no Memory policy. + expect(box.targets.map(({ name }: { name: string }) => name)).toEqual(["bedrock"]); + expect(box.additionalPolicies).toEqual(["agentcore/box-bedrock-policy.json"]); + expect(existsSync(join(root, "agentcore", "box-memory-policy.json"))).toBe(false); + expect(events).not.toContainEqual({ type: "step", message: "writing agentcore/box-memory-policy.json", }); - // The Runtime profile routes the Memory calls to that target. - const profile = await readFile( - join(root, "app", "agent_python_strands_box", ".box-dist", "profile.toml"), - "utf8", - ); - expect(profile).toContain('url = "${{ inputs.gateway.url }}/agentcore"'); + // The Runtime profile sends the Memory calls to Memory, signed with the execution role. + const profile = parse( + await readFile( + join(root, "app", "agent_python_strands_box", ".box-dist", "profile.toml"), + "utf8", + ), + ) as { egress: Record }; + // Build writes both for the region it builds for, which the environment of the test sets. + const region = box.targets[0].outboundAuth.region as string; + expect(profile.egress.agentcore).toEqual({ + destinations: [`bedrock-agentcore.${region}.amazonaws.com`], + secret: { ref: "aws://" }, + }); // A second build, of the project as it now reads, changes nothing in agentcore.json. const reread = await subject.resolve({ filePath: root }); diff --git a/src/core/project/manager.tsx b/src/core/project/manager.tsx index d7c3b9bbe..6394ab099 100644 --- a/src/core/project/manager.tsx +++ b/src/core/project/manager.tsx @@ -1641,8 +1641,8 @@ export class FsProjectManager implements ProjectManager { } /** - * The region `agentcore build` writes the Box gateway's `bedrock` and `agentcore` targets and the - * AgentCore Runtime profile for, as the first deploy picks the default target's: AWS_REGION, + * The region `agentcore build` writes the Box gateway's `bedrock` target and the AgentCore Runtime + * profile for, as the first deploy picks the default target's: AWS_REGION, * then AWS_DEFAULT_REGION, then the active profile's region in the shared AWS config file, then * DEFAULT_REGION. Deploy uses its target's region instead. */ diff --git a/src/core/project/runtimeProfile/derive.test.ts b/src/core/project/runtimeProfile/derive.test.ts index dff787616..226acbf4c 100644 --- a/src/core/project/runtimeProfile/derive.test.ts +++ b/src/core/project/runtimeProfile/derive.test.ts @@ -18,7 +18,7 @@ const CODEZIP = { box: { version: "1.0.0" }, } as ProjectRuntime; const CONTAINER = { ...CODEZIP, build: "Container", entrypoint: "main.py" } as ProjectRuntime; -const SPEC = { name: "demo", runtimes: [CODEZIP] } as unknown as ProjectSpec; +const SPEC = { name: "demo", runtimes: [CODEZIP], memories: [] } as unknown as ProjectSpec; const context = (runtime: ProjectRuntime, places = new Map()) => runtimeProfileContext({ diff --git a/src/core/project/runtimeProfile/derive.ts b/src/core/project/runtimeProfile/derive.ts index d0d4581c1..bc3bdb7e9 100644 --- a/src/core/project/runtimeProfile/derive.ts +++ b/src/core/project/runtimeProfile/derive.ts @@ -8,7 +8,7 @@ * | --- | --- | * | `refusals` | stops for a key that cannot work on the Runtime whatever its paths are | * | `gateway` | sends each call of the box through the runtime's Box gateway | - * | `additions` | adds what only the Runtime needs, and replaces each value that works only under dev | + * | `additions` | adds what only the Runtime needs, the direct Memory route included, and replaces each value that works only under dev | * | `paths` | moves each path of the project to where the code is on the Runtime | */ import * as additions from "./rules/additions"; diff --git a/src/core/project/runtimeProfile/rules/additions.test.ts b/src/core/project/runtimeProfile/rules/additions.test.ts index 1b3dc8202..ae1b1a81e 100644 --- a/src/core/project/runtimeProfile/rules/additions.test.ts +++ b/src/core/project/runtimeProfile/rules/additions.test.ts @@ -17,13 +17,20 @@ import { runtimeProfileContext } from "../context"; import { deriveRuntimeProfile } from "../derive"; import { serializeRuntimeProfile } from "../serialize"; import type { TomlTable } from "../types"; -import { apply, DEFAULT_TELEMETRY_TARGET, runtimeProfileHeader } from "./additions"; +import { defaultMemoryName } from "../../../../projectSchemas/memory"; +import { apply, DEFAULT_TELEMETRY_TARGET, MEMORY_ROUTE, runtimeProfileHeader } from "./additions"; const PROFILE_FILE = ".box/agent/local/profile.toml"; /** The region create resolved, which the local profile names: not the build region. */ const CREATE_REGION = "us-east-1"; const BUILD_REGION = "us-west-2"; +/** The route of the agent's Memory calls: to Memory directly, signed with the execution role. */ +const DIRECT_MEMORY_ROUTE = { + destinations: [`bedrock-agentcore.${BUILD_REGION}.amazonaws.com`], + secret: { ref: "aws://" }, +}; + type Build = "CodeZip" | "Container"; type Protocol = NonNullable; @@ -48,14 +55,12 @@ function passthrough(name: string, endpoint: string): AgentCoreGatewayTarget { } as AgentCoreGatewayTarget; } -/** A Box project with `runtime`, its Box gateway with the targets build adds, and `memories`. */ +/** + * A Box project with `runtime`, its Box gateway with the target build adds, and `memories`. Build + * adds no target for Memory: the agent's Memory calls go to Memory directly. + */ function specOf(runtime: ProjectRuntime, memories: readonly string[] = []) { - const targets = [ - passthrough("bedrock", `https://bedrock-runtime.${BUILD_REGION}.amazonaws.com`), - ...(memories.length > 0 - ? [passthrough("agentcore", `https://bedrock-agentcore.${BUILD_REGION}.amazonaws.com`)] - : []), - ]; + const targets = [passthrough("bedrock", `https://bedrock-runtime.${BUILD_REGION}.amazonaws.com`)]; return { name: "demo", runtimes: [runtime], @@ -285,15 +290,22 @@ describe("deriveRuntimeProfile on each box template of create", () => { const { profile, problems } = deriveRuntimeProfile( local, - contextOf(runtime, { memories, telemetry }), + contextOf(runtime, { + memories: memories.length > 0 ? [defaultMemoryName(runtime.name)] : [], + telemetry, + }), ); - expect(problems).toEqual([]); - expect(profile).toEqual( + // The old renderer sent the Memory calls through the gateway's `agentcore` target. They now + // go to Memory directly, under the same route name, in the same place. + const expected = structuredClone( OLD_RENDERER.boxTemplates[ `${template} provider=${provider} memories=${memories.length} telemetry=${telemetryCase}` ]!, ); + if (memories.length > 0) (expected.egress as TomlTable)[MEMORY_ROUTE] = DIRECT_MEMORY_ROUTE; + expect(problems).toEqual([]); + expect(profile).toEqual(expected); }, ); }); @@ -920,6 +932,72 @@ describe("the additions rule", () => { }); }); + describe("the Memory route", () => { + const MEMORY = defaultMemoryName("agent"); + + test("sends the Memory calls of a runtime with its default Memory to Memory directly, signed with the execution role", () => { + const { table, problems } = apply( + createdProfile(CODEZIP), + contextOf(CODEZIP, { memories: [MEMORY] }), + ); + + expect(problems).toEqual([]); + const route = (table.egress as TomlTable)[MEMORY_ROUTE]; + expect(route).toEqual(DIRECT_MEMORY_ROUTE); + // No backend: the Box gateway would sign the calls with its own role. + expect(route).not.toHaveProperty("backends"); + }); + + test("keeps each route of the profile, and adds the Memory route after them", () => { + const local = { + ...createdProfile(CONTAINER), + egress: { github: { destinations: ["api.github.com"] } }, + }; + + const egress = apply(local, contextOf(CONTAINER, { memories: [MEMORY] })).table + .egress as TomlTable; + + expect(Object.keys(egress)).toEqual(["github", MEMORY_ROUTE]); + expect(egress.github).toEqual({ destinations: ["api.github.com"] }); + }); + + test("adds no Memory route for a runtime with no default Memory", () => { + const none = apply(createdProfile(CODEZIP), contextOf(CODEZIP)).table; + const other = apply(createdProfile(CODEZIP), contextOf(CODEZIP, { memories: ["shared"] })); + expect(none.egress).not.toHaveProperty(MEMORY_ROUTE); + expect(other.table.egress).not.toHaveProperty(MEMORY_ROUTE); + }); + + test("adds no second route to the Memory host when a route or HTTP MCP server of the profile has it", () => { + const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; + const context = contextOf(CODEZIP, { memories: [MEMORY] }); + const routed = { egress: { memory: { destinations: [host] } } }; + const served = { mcp: { memory: { type: "http", destinations: [host] } } }; + + expect(apply(routed, context).table.egress).toEqual(routed.egress); + expect(apply(served, context).table.egress).toBeUndefined(); + }); + + test("stops build for a route of the user under the name of the Memory route", () => { + const local = { egress: { [MEMORY_ROUTE]: { destinations: ["api.example.com"] } } }; + + const { table, problems } = apply(local, contextOf(CODEZIP, { memories: [MEMORY] })); + + expect(table.egress).toEqual(local.egress); + expect(problems).toEqual([ + { + code: "RUNTIME_ROUTE_NAME_TAKEN", + key: ["egress", MEMORY_ROUTE], + place: PROFILE_FILE, + reason: + `[egress.${MEMORY_ROUTE}] is the name of the route that the agent's Memory calls ` + + `take on AgentCore Runtime, to bedrock-agentcore.${BUILD_REGION}.amazonaws.com. ` + + "Rename that entry.", + }, + ]); + }); + }); + test("keeps the policies list as the local profile names it", () => { const local = { policies: ["./policy.dw", "rules/extra.dw"] }; @@ -936,6 +1014,7 @@ describe("the additions rule", () => { expect(derived).toContain("@box/agentcore and @box/agentcore-codezip packs"); expect(runtimeProfileHeader(contextOf(CONTAINER))[1]).not.toContain("@box/agentcore-codezip"); expect(standIns).toContain("AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are stand-ins"); + expect(standIns).toContain(`except [egress.${MEMORY_ROUTE}], which goes to AgentCore Memory`); const text = serializeRuntimeProfile( apply(createdProfile(CODEZIP), contextOf(CODEZIP)).table, runtimeProfileHeader(contextOf(CODEZIP)), diff --git a/src/core/project/runtimeProfile/rules/additions.ts b/src/core/project/runtimeProfile/rules/additions.ts index 085f5042b..6b9bc8867 100644 --- a/src/core/project/runtimeProfile/rules/additions.ts +++ b/src/core/project/runtimeProfile/rules/additions.ts @@ -14,9 +14,12 @@ * | `[tool.]` | a table that the template writes, as it writes it, goes; a table of the user stays | * | `[telemetry.]` | each CloudWatch target that box takes is signed with a bare `aws://`, in the deploy region when it names none; one that box refuses goes; a `file` target goes, with a warning | * | `[telemetry.agentcore]` | added when no target is `cloudwatch_logs`: the runtime's own log group and log stream | + * | `[egress.agentcore]` | added when the runtime has its default Memory and no route of the profile has the AgentCore host: box signs the Memory calls with a bare `aws://` and sends them to Memory directly, not through the Box gateway | * * It moves no path: the paths rule runs after it and moves each path of the user. */ +import { agentCoreDataPlaneHost } from "../../../../handlers/project/shortcuts"; +import { hasDefaultMemory } from "../../boxMemory"; import { AGENTCORE_CODEZIP_PACK_NAME, AGENTCORE_PACK_NAME, @@ -33,9 +36,17 @@ import { RUNTIME_INPUTS, type RuleResult, type RuntimeProfileContext, + type RuntimeProfileProblem, type TomlTable, } from "../types"; +/** + * The `[egress]` route of the agent's Memory calls. Box signs them with the execution role and + * sends them to AgentCore Memory directly. The Box gateway would sign them with its own role, and + * Memory could then not hold each call to the session of the runtime by its context keys. + */ +export const MEMORY_ROUTE = "agentcore"; + /** The variables of the region of the agent, which get the region of the Runtime. */ const REGION_ENV_NAMES: ReadonlySet = new Set(["AWS_REGION", "AWS_DEFAULT_REGION"]); @@ -49,8 +60,8 @@ const LOCAL_DEV = "LOCAL_DEV"; const CLOUDWATCH_KINDS: ReadonlySet = new Set(["cloudwatch_logs", "cloudwatch_traces"]); /** - * Stand-in AWS keys for boto3 in the box on AgentCore Runtime. They authenticate nothing: each AWS - * route goes to the Box gateway, and box signs each call for it with the execution role. + * Stand-in AWS keys for boto3 in the box on AgentCore Runtime. They authenticate nothing: box signs + * each AWS call with the execution role, for the Box gateway or, on the Memory route, for Memory. */ const RUNTIME_AWS_STAND_INS: Readonly> = { AWS_ACCESS_KEY_ID: "box-stand-in", @@ -129,8 +140,9 @@ export function runtimeProfileHeader( `Runs the agent of the AgentCore project '${context.spec.name}' in a box.`, `Derived by agentcore build and deploy from ${context.profileFile}, for AgentCore Runtime. ${packs}`, `${Object.keys(RUNTIME_AWS_STAND_INS).join(" and ")} are stand-ins so boto3 in the box can ` + - "build its requests. They authenticate nothing: every AWS route goes to the Box gateway, " + - "and box signs each request for it with the execution role.", + "build its requests. They authenticate nothing: box signs each request with the execution " + + "role. Every AWS route goes to the Box gateway, except " + + `[egress.${MEMORY_ROUTE}], which goes to AgentCore Memory directly.`, ...Object.entries(tableOf(profile.telemetry)) .filter(([, target]) => sameValue(target, DEFAULT_TELEMETRY_TARGET)) .map( @@ -159,7 +171,49 @@ export function apply(table: Readonly, context: RuntimeProfileContext else result.tool = tool; } result.telemetry = runtimeTelemetry(result.telemetry, context, warnings); - return { table: result, problems: [], warnings }; + const problems: RuntimeProfileProblem[] = []; + const egress = runtimeEgress(result.egress, result.mcp, context, problems); + if (egress !== undefined) result.egress = egress; + return { table: result, problems, warnings }; +} + +/** + * `[egress]` with the {@link MEMORY_ROUTE} route when the runtime has its default Memory: one + * destination, the AgentCore host of the build region, signed with a bare `aws://`, the execution + * role, and no backend. A route or HTTP MCP server of the profile that already has that host keeps + * it, so the box has one route to the host. A route of the user named {@link MEMORY_ROUTE} for + * another host is a problem. + */ +function runtimeEgress( + egress: unknown, + mcp: unknown, + context: RuntimeProfileContext, + problems: RuntimeProfileProblem[], +): unknown { + if (!hasDefaultMemory(context.spec, context.runtime)) return egress; + // box refuses `[egress]` that is not a table before build gets the profile. + if (egress !== undefined && !isTable(egress)) return egress; + const host = agentCoreDataPlaneHost(context.region); + const routes = [...Object.values(egress ?? {}), ...Object.values(tableOf(mcp))]; + const hasHost = (entry: unknown) => + isTable(entry) && Array.isArray(entry.destinations) && entry.destinations.includes(host); + if (routes.some(hasHost)) return egress; + if (egress !== undefined && Object.hasOwn(egress, MEMORY_ROUTE)) { + const key = ["egress", MEMORY_ROUTE]; + problems.push({ + code: "RUNTIME_ROUTE_NAME_TAKEN", + key, + place: context.placeOf(key), + reason: + `[egress.${MEMORY_ROUTE}] is the name of the route that the agent's Memory calls take ` + + `on AgentCore Runtime, to ${host}. Rename that entry.`, + }); + return egress; + } + return { + ...egress, + [MEMORY_ROUTE]: { destinations: [host], secret: { ref: RUNTIME_AWS_SIGNER } }, + }; } /** The user's packs in their order, then each Runtime pack that they do not import. */ diff --git a/src/core/project/runtimeProfile/rules/gateway.test.ts b/src/core/project/runtimeProfile/rules/gateway.test.ts index 585b2e30d..5ddbce001 100644 --- a/src/core/project/runtimeProfile/rules/gateway.test.ts +++ b/src/core/project/runtimeProfile/rules/gateway.test.ts @@ -1,10 +1,7 @@ import { describe, expect, test } from "bun:test"; import { parse } from "smol-toml"; import { InputValidationError } from "../../../../errors/errors"; -import { - getBoxGatewayAgentCoreTargetSpec, - getBoxGatewayBedrockTargetSpec, -} from "../../../../handlers/project/shortcuts"; +import { getBoxGatewayBedrockTargetSpec } from "../../../../handlers/project/shortcuts"; import type { AgentCoreGatewayTarget } from "../../../../projectSchemas/gateway"; import { boxGatewaySpec, type ProjectSpec } from "../../../../projectSchemas/project"; import type { ProjectRuntime } from "../../../../projectSchemas/runtime"; @@ -301,7 +298,7 @@ describe("the gateway rule", () => { const targets = [ passthrough("orders", "https://orders.example.com/v2"), getBoxGatewayBedrockTargetSpec(), - getBoxGatewayAgentCoreTargetSpec(), + passthrough("tickets", "https://tickets.example.com"), // An earlier build wrote this target, and no host of the profile takes it now. passthrough("egress-old", "https://old.example.com"), { name: "lambda", targetType: "lambda" } as unknown as AgentCoreGatewayTarget, @@ -324,8 +321,8 @@ describe("the gateway rule", () => { backendText("egress.bedrock", "bedrock") + '[egress.orders]\ndestinations = ["orders.example.com"]\n' + backendText("egress.orders", "orders") + - '[egress.agentcore]\ndestinations = ["bedrock-agentcore.us-west-2.amazonaws.com"]\n' + - backendText("egress.agentcore", "agentcore") + + '[egress.tickets]\ndestinations = ["tickets.example.com"]\n' + + backendText("egress.tickets", "tickets") + '[egress.egress-httpbin-org]\ndestinations = ["httpbin.org"]\n' + backendText("egress.egress-httpbin-org", "egress-httpbin-org"), ).egress, @@ -494,6 +491,21 @@ const withEntryKeys = (egress: unknown) => ]), ); +/** + * `withEntryKeys(egress)`, with the Memory route that the old renderer sent through the gateway's + * `agentcore` target as the additions rule now writes it: to Memory directly, with no backend. + */ +const withDirectMemory = (egress: unknown) => { + const result = withEntryKeys(egress); + if (Object.hasOwn(result, "agentcore")) { + result.agentcore = { + destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], + secret: { ref: "aws://" }, + }; + } + return result; +}; + describe("the gateway rule and the Runtime renderer it replaced", () => { const TEXT = [ 'packs = ["@box/base", "@box/bedrock"]', @@ -644,10 +656,10 @@ describe("the gateway rule and the Runtime renderer it replaced", () => { "orders", ]); expect(Object.keys(http(old.mcp))).toEqual(["docs"]); - expect(derived.profile.egress).toEqual(withEntryKeys(old.egress)); + expect(derived.profile.egress).toEqual(withDirectMemory(old.egress)); expect(http(derived.profile.mcp)).toEqual(http(old.mcp)); // The model that build keeps holds the same derived profile. - expect(network.runtimeProfile?.egress).toEqual(withEntryKeys(old.egress)); + expect(network.runtimeProfile?.egress).toEqual(withDirectMemory(old.egress)); expect(http(network.runtimeProfile?.mcp)).toEqual(http(old.mcp)); }); @@ -658,7 +670,7 @@ describe("the gateway rule and the Runtime renderer it replaced", () => { expect(network.failures).toEqual([]); expect(first.agentCoreGateways[0]!.targets.map(({ name }) => name)).toContain("egress-github"); - expect(derived.profile.egress).toEqual(withEntryKeys(old.egress)); + expect(derived.profile.egress).toEqual(withDirectMemory(old.egress)); expect(http(derived.profile.mcp)).toEqual(http(old.mcp)); }); diff --git a/src/core/project/runtimeProfile/rules/gateway.ts b/src/core/project/runtimeProfile/rules/gateway.ts index 321e267b6..a302d3200 100644 --- a/src/core/project/runtimeProfile/rules/gateway.ts +++ b/src/core/project/runtimeProfile/rules/gateway.ts @@ -3,7 +3,8 @@ * Box gateway. Each passthrough target of that gateway, as build writes the gateway, gets one * route: the box sends the calls to the target's host to `${{ inputs.gateway.url }}/`, and * signs them for the gateway in the Runtime's own region, `${{ inputs.aws.region }}`, with a bare - * `aws://`, the execution role. The gateway then calls the host with the target's own auth. + * `aws://`, the execution role. The gateway then calls the host with the target's own auth. The + * agent's Memory calls are the one exception: the additions rule sends them to Memory directly. * * | Local profile | Runtime profile | * | --- | --- | diff --git a/src/core/project/runtimeProfile/rules/paths.test.ts b/src/core/project/runtimeProfile/rules/paths.test.ts index 075d6e1c3..3a828bf69 100644 --- a/src/core/project/runtimeProfile/rules/paths.test.ts +++ b/src/core/project/runtimeProfile/rules/paths.test.ts @@ -25,7 +25,7 @@ const PROFILE = ".box/agent/local/profile.toml"; */ const contextOf = (text: string, runtime: ProjectRuntime = CODEZIP) => runtimeProfileContext({ - spec: { name: "demo", runtimes: [runtime] } as unknown as ProjectSpec, + spec: { name: "demo", runtimes: [runtime], memories: [] } as unknown as ProjectSpec, runtime, profileFile: PROFILE, region: "us-west-2", diff --git a/src/core/project/runtimeProfile/rules/refusals.test.ts b/src/core/project/runtimeProfile/rules/refusals.test.ts index ed1720f94..91b7e3865 100644 --- a/src/core/project/runtimeProfile/rules/refusals.test.ts +++ b/src/core/project/runtimeProfile/rules/refusals.test.ts @@ -26,7 +26,7 @@ const PROFILE = ".box/agent/local/profile.toml"; */ const contextOf = (text: string, runtime: ProjectRuntime = CODEZIP) => runtimeProfileContext({ - spec: { name: "demo", runtimes: [runtime] } as unknown as ProjectSpec, + spec: { name: "demo", runtimes: [runtime], memories: [] } as unknown as ProjectSpec, runtime, profileFile: PROFILE, region: "us-west-2", diff --git a/src/core/project/runtimeProfile/types.ts b/src/core/project/runtimeProfile/types.ts index 628d1520f..514cd9cb0 100644 --- a/src/core/project/runtimeProfile/types.ts +++ b/src/core/project/runtimeProfile/types.ts @@ -60,7 +60,7 @@ export interface RuntimeProfileContext { readonly runtime: ProjectRuntime; /** The local profile's path from the project root: `.box//local/profile.toml`. */ readonly profileFile: string; - /** The region build builds for: of the Box gateway and its `bedrock` and `agentcore` targets. */ + /** The region build builds for: of the Box gateway, its `bedrock` target, and the Memory route. */ readonly region: string; /** The runtime's Box gateway: its name, and its targets with the ones build adds. */ readonly gateway: { readonly name: string; readonly targets: readonly AgentCoreGatewayTarget[] }; @@ -92,6 +92,8 @@ export const RUNTIME_PROFILE_PROBLEM_CODES = [ "RUNTIME_TELEMETRY_NOT_CLOUDWATCH", /** A CloudWatch telemetry target that box refuses when a run starts. */ "RUNTIME_TELEMETRY_REFUSED", + /** A route of the user under the name of a route that only the Runtime profile has. */ + "RUNTIME_ROUTE_NAME_TAKEN", ] as const; export type RuntimeProfileProblemCode = (typeof RUNTIME_PROFILE_PROBLEM_CODES)[number]; diff --git a/src/handlers/project/shortcuts.test.ts b/src/handlers/project/shortcuts.test.ts index f22c389bb..3337bea63 100644 --- a/src/handlers/project/shortcuts.test.ts +++ b/src/handlers/project/shortcuts.test.ts @@ -2,8 +2,8 @@ import { describe, expect, test } from "bun:test"; import { EMPTY_TEMPLATE_NAME, formatTemplateParameterHelp, - getBoxGatewayAgentCoreTargetSpec, getBoxGatewayBedrockTargetSpec, + agentCoreDataPlaneHost, getDefaultMemorySpec, PROJECT_TEMPLATE_NAMES, RUNTIME_TEMPLATE_SHORTCUT_NAMES, @@ -87,12 +87,12 @@ test("default memory names fit the service limit for long runtime names", () => expect(memory.name).toHaveLength(48); }); -describe("Box gateway targets", () => { +describe("the Box gateway target and the Memory host", () => { test("take the DNS suffix of the partition of the region", () => { - const endpoints = (region: string) => - [getBoxGatewayBedrockTargetSpec(region), getBoxGatewayAgentCoreTargetSpec(region)].map( - ({ passthrough }) => passthrough?.endpoint, - ); + const endpoints = (region: string) => [ + getBoxGatewayBedrockTargetSpec(region).passthrough?.endpoint, + `https://${agentCoreDataPlaneHost(region)}`, + ]; expect(endpoints("cn-north-1")).toEqual([ "https://bedrock-runtime.cn-north-1.amazonaws.com.cn", diff --git a/src/handlers/project/shortcuts.ts b/src/handlers/project/shortcuts.ts index a1140608c..81b34e9b6 100644 --- a/src/handlers/project/shortcuts.ts +++ b/src/handlers/project/shortcuts.ts @@ -31,8 +31,8 @@ export function getDefaultMemorySpec(runtimeName: string): Memory { /** * The Bedrock region of the box profile `agentcore create` writes when the caller passes no * region. Both create paths pass the region they resolved. Build and deploy write the Box - * gateway's `bedrock` and `agentcore` targets, and the AgentCore Runtime profile, for the region - * they build for instead (see `boxBuildRegion`). + * gateway's `bedrock` target, and the AgentCore Runtime profile, for the region they build for + * instead (see `boxBuildRegion`). */ export const DEFAULT_EGRESS_BEDROCK_REGION = "us-west-2"; @@ -57,31 +57,13 @@ export function getBoxGatewayBedrockTargetSpec( } /** - * The name of the Box gateway's AgentCore target, which build and deploy add - * for a boxed agent's default Memory; also the name of the box route to it. + * The AgentCore data-plane host of `region`. A boxed agent's Memory calls go to it directly, signed + * with the execution role, and not through the Box gateway. */ -export const BOX_GATEWAY_AGENTCORE_TARGET_NAME = "agentcore"; - -/** The AgentCore data-plane host of `region`, which the Box gateway's AgentCore target fronts. */ -export function boxGatewayAgentCoreHost(region: string = DEFAULT_EGRESS_BEDROCK_REGION): string { +export function agentCoreDataPlaneHost(region: string = DEFAULT_EGRESS_BEDROCK_REGION): string { return `bedrock-agentcore.${region}.${partition(region).dnsSuffix}`; } -/** - * The Box gateway's passthrough target for the AgentCore data plane (Memory), - * signed with the gateway's role. - */ -export function getBoxGatewayAgentCoreTargetSpec( - region: string = DEFAULT_EGRESS_BEDROCK_REGION, -): AgentCoreGatewayTarget { - return { - name: BOX_GATEWAY_AGENTCORE_TARGET_NAME, - targetType: "passthrough", - passthrough: { endpoint: `https://${boxGatewayAgentCoreHost(region)}`, protocolType: "CUSTOM" }, - outboundAuth: { type: "GATEWAY_IAM_ROLE", service: "bedrock-agentcore", region }, - }; -} - /** * Whether the scaffolded agent calls Bedrock for its model: the Bedrock * provider, or LiteLLM with a `bedrock/` model id (its default). From fafa189f25f8bc176f14410a05ce548008d19e68 Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Fri, 20 Nov 2026 19:05:54 -0800 Subject: [PATCH 02/11] fix(box): Remove the Memory gateway access that an earlier build wrote A project that an earlier build wrote kept the `agentcore` target on its Box gateway and `agentcore/box-memory-policy.json` in the gateway's additionalPolicies and on disk. The gateway rule kept that target as the user's, so the Memory calls still went through the gateway role, and the IAM check read the old file as a user policy and gave no warning. Build now removes the `agentcore` target when it is as the earlier build wrote it for some region, removes the policy listing, and removes the policy file. A target of that name that the user changed stays, and build then stops until the user renames it, because its route would take the name of the Memory route. The Memory route rule now defers only to a direct route or HTTP MCP server to the Memory host. A route through the Box gateway stops build with RUNTIME_MEMORY_THROUGH_GATEWAY. The rule matches the host with `:443`, in any case, and as a `*.` wildcard. The README and the local testing guide now say that Memory calls go to Memory directly, and that a runtime in VPC network mode needs a `bedrock-agentcore` VPC endpoint. --- README.md | 4 +- docs/box-local-testing.md | 6 +- src/core/project/boxMemory.test.ts | 76 ++++++++++++++++ src/core/project/boxMemory.ts | 68 +++++++++++++- src/core/project/boxNetworkWriters.test.ts | 6 +- src/core/project/boxNetworkWriters.ts | 24 +++-- src/core/project/manager.boxNetwork.test.ts | 90 +++++++++++++++++-- .../runtimeProfile/rules/additions.test.ts | 79 ++++++++++++++++ .../project/runtimeProfile/rules/additions.ts | 55 ++++++++++-- src/core/project/runtimeProfile/types.ts | 2 + 10 files changed, 380 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 65dab78c2..feaa93916 100644 --- a/README.md +++ b/README.md @@ -261,7 +261,9 @@ second answer should say teal, and the runtime log shows `running profile ...` w A boxed agent gets the deployed Memory IDs and the Runtime's region inside the box. It does not get the `AGENTCORE_GATEWAY__URL` variables of gateways you add yourself: its calls go out through -its Box gateway. +its Box gateway. Its calls to its default Memory are the exception. Box signs them with the +execution role and sends them to Memory directly, so Memory can hold each call to the session of +the runtime. A runtime in VPC network mode needs a `bedrock-agentcore` VPC endpoint for them. A Box project that an older CLI created still has `start.sh`, or a Dockerfile that runs it. Build and deploy now render profiles that those files cannot start. Create the project again, or copy diff --git a/docs/box-local-testing.md b/docs/box-local-testing.md index 5b147d5fb..9f4bd91a7 100644 --- a/docs/box-local-testing.md +++ b/docs/box-local-testing.md @@ -47,9 +47,9 @@ to it. the default telemetry target of the AgentCore Runtime profile needs. Build, deploy and dev run it to read the box profile with `box profile show --format json --full`, and CodeZip dev runs the agent with it. See [Install box](#install-box). -- AWS credentials for a **dev or test** account. Build writes the Box gateway's Bedrock and - Memory targets for `AWS_REGION` (else your AWS profile's region), and deploy for its target's - region. +- AWS credentials for a **dev or test** account. Build writes the Box gateway's Bedrock target and + the Runtime profile's direct Memory route for `AWS_REGION` (else your AWS profile's region), and + deploy for its target's region. - `bun` for the CLI, `npm` for the generated CDK app. ## Install box diff --git a/src/core/project/boxMemory.test.ts b/src/core/project/boxMemory.test.ts index 56b434a9f..ba5c7aae4 100644 --- a/src/core/project/boxMemory.test.ts +++ b/src/core/project/boxMemory.test.ts @@ -6,6 +6,8 @@ import { addBoxGatewayTargets, BOX_BEDROCK_POLICY_FILE, boxBedrockPolicy, + LEGACY_BOX_MEMORY_POLICY_FILE, + removeLegacyMemoryAccess, } from "./boxMemory"; function spec(options: { entrypoint?: string; memories?: string[] } = {}): ProjectSpec { @@ -172,3 +174,77 @@ describe("addBoxGatewayTargets", () => { expect(gateway(project).targets[0]!.outboundAuth?.region).toBe("us-east-1"); }); }); + +describe("removeLegacyMemoryAccess", () => { + /** The `agentcore` target, in `region`, as a build before Memory went direct wrote it. */ + const legacy = (region: string, dnsSuffix = "amazonaws.com") => ({ + name: "agentcore", + targetType: "passthrough" as const, + passthrough: { + endpoint: `https://bedrock-agentcore.${region}.${dnsSuffix}`, + protocolType: "CUSTOM" as const, + }, + outboundAuth: { + type: "GATEWAY_IAM_ROLE" as const, + service: "bedrock-agentcore", + region, + }, + }); + + test("removes the agentcore target of an earlier build, in any region, and the policy listing", () => { + for (const [region, suffix] of [ + ["us-west-2", "amazonaws.com"], + ["eu-central-1", "amazonaws.com"], + ["cn-north-1", "amazonaws.com.cn"], + ] as const) { + const project = spec(); + gateway(project).targets = [getBoxGatewayBedrockTargetSpec(), legacy(region, suffix)]; + gateway(project).additionalPolicies = [ + BOX_BEDROCK_POLICY_FILE, + LEGACY_BOX_MEMORY_POLICY_FILE, + ]; + + expect(removeLegacyMemoryAccess(project)).toEqual([ + `removing target 'agentcore' from gateway '${GATEWAY}' in agentcore.json: the boxed agent's Memory calls go to Memory directly`, + `removing '${LEGACY_BOX_MEMORY_POLICY_FILE}' from gateway '${GATEWAY}' additionalPolicies in agentcore.json: its role no longer calls Memory`, + ]); + expect(targets(project)).toEqual(["bedrock"]); + expect(gateway(project).additionalPolicies).toEqual([BOX_BEDROCK_POLICY_FILE]); + } + }); + + test("drops additionalPolicies when the Memory file was its only entry", () => { + const project = spec(); + gateway(project).additionalPolicies = [LEGACY_BOX_MEMORY_POLICY_FILE]; + + removeLegacyMemoryAccess(project); + + expect(gateway(project).additionalPolicies).toBeUndefined(); + }); + + test("keeps an agentcore target that the user changed, and one on a gateway that is not a Box gateway", () => { + const project = spec(); + const changed = { ...legacy("us-west-2"), description: "mine" } as never; + gateway(project).targets = [changed]; + const tools = { + name: "tools", + authorizerType: "AWS_IAM", + targets: [legacy("us-west-2")], + } as ProjectSpec["agentCoreGateways"][number]; + project.agentCoreGateways.push(tools); + + expect(removeLegacyMemoryAccess(project)).toEqual([]); + expect(gateway(project).targets).toEqual([changed]); + expect(tools.targets).toHaveLength(1); + }); + + test("runs first in addBoxGatewayTargets", () => { + const project = spec(); + gateway(project).targets = [legacy("us-west-2")]; + + expect(addBoxGatewayTargets(project, "us-west-2")[0]).toStartWith( + "removing target 'agentcore'", + ); + expect(targets(project)).toEqual(["bedrock"]); + }); +}); diff --git a/src/core/project/boxMemory.ts b/src/core/project/boxMemory.ts index bd332c408..9f3bc2719 100644 --- a/src/core/project/boxMemory.ts +++ b/src/core/project/boxMemory.ts @@ -2,11 +2,12 @@ import { isDeepStrictEqual } from "node:util"; import { partition } from "@aws-sdk/util-endpoints"; import { DEFAULT_EGRESS_BEDROCK_REGION, + agentCoreDataPlaneHost, callsBedrockModel, getBoxGatewayBedrockTargetSpec, } from "../../handlers/project/shortcuts"; import type { AgentCoreGateway, AgentCoreGatewayTarget } from "../../projectSchemas/gateway"; -import type { ProjectSpec } from "../../projectSchemas/project"; +import { isBoundGateway, type ProjectSpec } from "../../projectSchemas/project"; import { defaultMemoryName } from "../../projectSchemas/memory"; import type { ProjectRuntime } from "../../projectSchemas/runtime"; import { runtimeRunsInBox } from "./templates/boxProfile"; @@ -18,6 +19,25 @@ import { runtimeRunsInBox } from "./templates/boxProfile"; */ export const BOX_BEDROCK_POLICY_FILE = "agentcore/box-bedrock-policy.json"; +/** + * The policy file, relative to the project root, that an earlier build wrote so the Box gateway's + * role could call Memory. The agent's Memory calls now go to Memory directly, so build removes it. + */ +export const LEGACY_BOX_MEMORY_POLICY_FILE = "agentcore/box-memory-policy.json"; + +/** The name of the Box gateway target that an earlier build wrote for the agent's Memory calls. */ +const LEGACY_MEMORY_TARGET_NAME = "agentcore"; + +/** The `agentcore` target, in `region`, as an earlier build wrote it for the Memory calls. */ +function legacyMemoryTarget(region: string): AgentCoreGatewayTarget { + return { + name: LEGACY_MEMORY_TARGET_NAME, + targetType: "passthrough", + passthrough: { endpoint: `https://${agentCoreDataPlaneHost(region)}`, protocolType: "CUSTOM" }, + outboundAuth: { type: "GATEWAY_IAM_ROLE", service: "bedrock-agentcore", region }, + }; +} + /** The Bedrock actions behind Converse and ConverseStream, which the agent's model calls make. */ const BEDROCK_INVOKE_ACTIONS = ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"]; @@ -139,17 +159,59 @@ export function addBoxBedrockTarget( return messages; } +/** + * Removes from each Box gateway what an earlier build wrote for the agent's Memory calls: the + * `agentcore` target, when it is as that build wrote it for some region, and + * {@link LEGACY_BOX_MEMORY_POLICY_FILE} in `additionalPolicies`. Without this, the gateway rule + * would keep that target as the user's and route the Memory calls through the gateway role again. + * A target of that name that the user changed stays. Changes `spec` in place, and returns the step + * messages; none when nothing changed. + */ +export function removeLegacyMemoryAccess(spec: ProjectSpec): string[] { + const messages: string[] = []; + for (const gateway of spec.agentCoreGateways.filter(isBoundGateway)) { + const legacy = gateway.targets.findIndex((target) => { + const region = target.outboundAuth?.region; + return ( + target.name === LEGACY_MEMORY_TARGET_NAME && + region !== undefined && + isDeepStrictEqual(target, legacyMemoryTarget(region)) + ); + }); + if (legacy !== -1) { + gateway.targets.splice(legacy, 1); + messages.push( + `removing target '${LEGACY_MEMORY_TARGET_NAME}' from gateway '${gateway.name}' in ` + + "agentcore.json: the boxed agent's Memory calls go to Memory directly", + ); + } + if (gateway.additionalPolicies?.includes(LEGACY_BOX_MEMORY_POLICY_FILE)) { + const kept = gateway.additionalPolicies.filter( + (file) => file !== LEGACY_BOX_MEMORY_POLICY_FILE, + ); + if (kept.length > 0) gateway.additionalPolicies = kept; + else delete gateway.additionalPolicies; + messages.push( + `removing '${LEGACY_BOX_MEMORY_POLICY_FILE}' from gateway '${gateway.name}' ` + + "additionalPolicies in agentcore.json: its role no longer calls Memory", + ); + } + } + return messages; +} + /** * Adds the Box gateway targets that the boxed agents' calls need, in `region`, before build renders * the Runtime profiles from those targets: the Bedrock model's. The agent's Memory calls take no * target: box signs them with the execution role and sends them to Memory itself, so Memory can - * hold each call to the session of the runtime (runtimeProfile/rules/additions.ts). + * hold each call to the session of the runtime (runtimeProfile/rules/additions.ts). It first + * removes the Memory target and policy that an earlier build wrote. */ export function addBoxGatewayTargets( spec: ProjectSpec, region: string = DEFAULT_EGRESS_BEDROCK_REGION, ): string[] { - return addBoxBedrockTarget(spec, region); + return [...removeLegacyMemoryAccess(spec), ...addBoxBedrockTarget(spec, region)]; } /** diff --git a/src/core/project/boxNetworkWriters.test.ts b/src/core/project/boxNetworkWriters.test.ts index f4ef78e7f..fbb7e8ce8 100644 --- a/src/core/project/boxNetworkWriters.test.ts +++ b/src/core/project/boxNetworkWriters.test.ts @@ -283,8 +283,12 @@ describe("defaultTargetsWriter", () => { const edits = run([defaultTargetsWriter], [network()], project); // The agent's Memory calls go to Memory directly, so they take no target and no gateway policy. + // The writer removes the Memory policy file that an earlier build wrote. expect(targetNames(edits.spec)).toEqual(["mine", "bedrock"]); - expect(edits.files.map(({ path }) => path)).toEqual([BOX_BEDROCK_POLICY_FILE]); + expect(edits.files).toEqual([ + expect.objectContaining({ path: BOX_BEDROCK_POLICY_FILE }), + { path: "agentcore/box-memory-policy.json", remove: true }, + ]); }); test("writes the Bedrock policy for the region of the bedrock target it writes", () => { diff --git a/src/core/project/boxNetworkWriters.ts b/src/core/project/boxNetworkWriters.ts index 5f315be81..de931f4c9 100644 --- a/src/core/project/boxNetworkWriters.ts +++ b/src/core/project/boxNetworkWriters.ts @@ -19,7 +19,12 @@ import { type BoxNetwork, type BoxNetworkHost, } from "./boxNetwork"; -import { addBoxGatewayTargets, BOX_BEDROCK_POLICY_FILE, boxBedrockPolicy } from "./boxMemory"; +import { + addBoxGatewayTargets, + BOX_BEDROCK_POLICY_FILE, + boxBedrockPolicy, + LEGACY_BOX_MEMORY_POLICY_FILE, +} from "./boxMemory"; import { planBoxProfileCredentials, type BoxKeySources } from "./boxProfileCredentials"; import type { BoxEnvRoute } from "./boxProfileEgress"; import { @@ -279,11 +284,16 @@ function undeclaredKey( /** * The `bedrock` target that each box runtime's model calls take, and the Bedrock policy of the - * gateways' role, as `addBoxGatewayTargets` adds them. + * gateways' role, as `addBoxGatewayTargets` adds them. It removes the Memory policy file that an + * earlier build wrote, as `addBoxGatewayTargets` removes its listing. */ export const defaultTargetsWriter: BoxNetworkWriter = { name: "default targets", - writes: () => [{ kind: "defaultTargets" }, { kind: "file", path: BOX_BEDROCK_POLICY_FILE }], + writes: () => [ + { kind: "defaultTargets" }, + { kind: "file", path: BOX_BEDROCK_POLICY_FILE }, + { kind: "file", path: LEGACY_BOX_MEMORY_POLICY_FILE }, + ], edits: (networks, { spec }) => { // Every network of one build has the region build builds for. const region = networks[0]?.region; @@ -294,9 +304,11 @@ export const defaultTargetsWriter: BoxNetworkWriter = { path: BOX_BEDROCK_POLICY_FILE, policy: region === undefined ? undefined : boxBedrockPolicy(spec, region), }, - ].flatMap(({ path, policy }) => - policy ? [{ path, text: `${JSON.stringify(policy, undefined, 2)}\n` }] : [], - ), + ] + .flatMap(({ path, policy }): BoxNetworkFile[] => + policy ? [{ path, text: `${JSON.stringify(policy, undefined, 2)}\n` }] : [], + ) + .concat({ path: LEGACY_BOX_MEMORY_POLICY_FILE, remove: true }), }; }, }; diff --git a/src/core/project/manager.boxNetwork.test.ts b/src/core/project/manager.boxNetwork.test.ts index 3ca6f0625..87b736359 100644 --- a/src/core/project/manager.boxNetwork.test.ts +++ b/src/core/project/manager.boxNetwork.test.ts @@ -221,6 +221,7 @@ async function boxGateway(root: string) { const spec = JSON.parse(await text(root, SPEC)); return spec.agentCoreGateways.find(({ name }: { name: string }) => name === GATEWAY) as { targets: Record[]; + additionalPolicies?: string[]; enableApplicationLogs?: boolean; }; } @@ -500,7 +501,7 @@ describe("build writes the Box network of the box profile", () => { ); }); - test("routes an entry of the profile for the Memory host through the gateway, and adds no Memory route", async () => { + test("stops for an entry of the profile that would send the Memory calls through the gateway", async () => { const { manager, root, box } = await strandsProject(); await editProfile(root, { tables: @@ -514,15 +515,90 @@ describe("build writes the Box network of the box profile", () => { }, }); - await build(manager, root); + const error = await build(manager, root).then( + () => undefined, + (caught: unknown) => caught, + ); + + // On the Runtime the entry gets a gateway target, which would sign the Memory calls with the + // gateway's role, and Memory could not hold each call to the session of the runtime. + expect(error).toBeInstanceOf(InputValidationError); + expect((error as Error).message).toContain( + "[egress.memory] sends the calls to bedrock-agentcore.us-west-2.amazonaws.com through the " + + "Box gateway on AgentCore Runtime", + ); + expect(await targetNames(root)).not.toContain("egress-memory"); + }); + + test("removes the Memory target, policy listing, and policy file of an earlier build, and sends Memory directly", async () => { + const { manager, root } = await strandsProject(); + const MEMORY_FILE = "agentcore/box-memory-policy.json"; + // What a build before Memory went direct wrote for the agent's Memory calls. + await editGateway(root, (gateway) => { + gateway.targets.push({ + name: "agentcore", + targetType: "passthrough", + passthrough: { + endpoint: "https://bedrock-agentcore.us-west-2.amazonaws.com", + protocolType: "CUSTOM", + }, + outboundAuth: { + type: "GATEWAY_IAM_ROLE", + service: "bedrock-agentcore", + region: "us-west-2", + }, + }); + (gateway as { additionalPolicies?: string[] }).additionalPolicies = [MEMORY_FILE]; + }); + await writeFile(path(root, MEMORY_FILE), '{ "Version": "2012-10-17", "Statement": [] }\n'); + + const { steps } = await build(manager, root); - expect(await targetNames(root)).toEqual(["egress-memory", "bedrock"]); + expect(await targetNames(root)).toEqual(["bedrock"]); + expect((await boxGateway(root)).additionalPolicies).toEqual([ + "agentcore/box-bedrock-policy.json", + ]); + expect(existsSync(path(root, MEMORY_FILE))).toBe(false); + expect(steps).toEqual( + expect.arrayContaining([ + `removing target 'agentcore' from gateway '${GATEWAY}' in agentcore.json: the boxed ` + + "agent's Memory calls go to Memory directly", + `removing '${MEMORY_FILE}' from gateway '${GATEWAY}' additionalPolicies in ` + + "agentcore.json: its role no longer calls Memory", + `removing ${MEMORY_FILE}`, + ]), + ); const egress = (await runtimeProfile(root)).egress as Record; - // The user's [egress.memory] keeps the host, so the box has one route to it: to its target. - expect(Object.keys(egress)).toEqual(["bedrock", "memory"]); - expect(egress.memory).toEqual( - gatewayRoute("egress-memory", "bedrock-agentcore.us-west-2.amazonaws.com"), + expect(egress.agentcore).toEqual({ + destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], + secret: { ref: "aws://" }, + }); + }); + + test("keeps an agentcore target that the user changed, and stops until the user renames it", async () => { + const { manager, root, box } = await strandsProject(); + const mine = { + name: "agentcore", + targetType: "passthrough", + passthrough: { endpoint: "https://memory.example.com", protocolType: "CUSTOM" }, + outboundAuth: { type: "NONE" }, + }; + await editGateway(root, (gateway) => gateway.targets.push(mine)); + box.answer = shows({}); + + const error = await build(manager, root).then( + () => undefined, + (caught: unknown) => caught, + ); + + // Build does not remove a target it did not write, and its route would take the Memory route's name. + expect(error).toBeInstanceOf(InputValidationError); + expect((error as Error).message).toContain( + `agentcore/agentcore.json [egress.agentcore] is the name of the route that the agent's ` + + "Memory calls take on AgentCore Runtime, to bedrock-agentcore.us-west-2.amazonaws.com. " + + `Rename the 'agentcore' target of gateway '${GATEWAY}'.`, ); + expect((await boxGateway(root)).targets).toContainEqual(mine); }); test("on a first build, stops with every problem, where it is, and writes nothing", async () => { diff --git a/src/core/project/runtimeProfile/rules/additions.test.ts b/src/core/project/runtimeProfile/rules/additions.test.ts index 1a90d3e7c..b30290615 100644 --- a/src/core/project/runtimeProfile/rules/additions.test.ts +++ b/src/core/project/runtimeProfile/rules/additions.test.ts @@ -1024,6 +1024,85 @@ describe("the additions rule", () => { expect(apply(served, context).table.egress).toBeUndefined(); }); + test("counts a direct route to the Memory host with :443, in another case, or as a wildcard", () => { + const context = contextOf(CODEZIP, { memories: [MEMORY] }); + for (const destination of [ + `bedrock-agentcore.${BUILD_REGION}.amazonaws.com:443`, + `Bedrock-AgentCore.${BUILD_REGION}.amazonaws.com`, + "*.amazonaws.com", + ]) { + const local = { egress: { memory: { destinations: [destination] } } }; + const { table, problems } = apply(local, context); + + expect([destination, problems]).toEqual([destination, []]); + expect(table.egress).toEqual(local.egress); + } + // Another port, or a wildcard of another domain, is not the Memory host. + const other = { + egress: { + memory: { + destinations: ["*.example.com", "bedrock-agentcore.us-west-2.amazonaws.com:8443"], + }, + }, + }; + expect(Object.keys(apply(other, context).table.egress as TomlTable)).toEqual([ + "memory", + MEMORY_ROUTE, + ]); + }); + + test("stops build for a route that sends the Memory calls through the Box gateway", () => { + const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; + const backends = [{ url: "${{ inputs.gateway.url }}/egress-memory" }]; + const local = { + egress: { memory: { destinations: [host], backends } }, + mcp: { recall: { type: "http", destinations: [host], backends } }, + }; + + const { table, problems } = apply(local, contextOf(CODEZIP, { memories: [MEMORY] })); + + expect(problems.map(({ code, key }) => [code, key])).toEqual([ + ["RUNTIME_MEMORY_THROUGH_GATEWAY", ["egress", "memory"]], + ["RUNTIME_MEMORY_THROUGH_GATEWAY", ["mcp", "recall"]], + ]); + expect(problems[0]!.reason).toBe( + `[egress.memory] sends the calls to ${host} through the Box gateway on AgentCore Runtime, ` + + "and the gateway would sign the agent's Memory calls with its own role. Then Memory " + + "cannot hold each call to the session of the runtime. Remove that entry: build sends " + + "the Memory calls to Memory directly, signed with the execution role.", + ); + expect(table.egress).not.toHaveProperty(MEMORY_ROUTE); + }); + + test("leaves a gateway route to the Memory host alone for a runtime with no default Memory", () => { + const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; + const local = { egress: { memory: { destinations: [host], backends: [{ url: "x" }] } } }; + + expect(apply(local, contextOf(CODEZIP)).problems).toEqual([]); + }); + + test("names the gateway target when a target of the user takes the Memory route's name", () => { + const context = contextOf(CODEZIP, { memories: [MEMORY] }); + const withTarget = { + ...context, + gateway: { + ...context.gateway, + targets: [ + ...context.gateway.targets, + passthrough("agentcore", "https://memory.example.com"), + ], + }, + }; + const local = { egress: { [MEMORY_ROUTE]: { destinations: ["memory.example.com"] } } }; + + const [problem] = apply(local, withTarget).problems; + + expect(problem?.place).toBe("agentcore/agentcore.json"); + expect(problem?.reason).toEndWith( + `Rename the 'agentcore' target of gateway '${context.gateway.name}'.`, + ); + }); + test("stops build for a route of the user under the name of the Memory route", () => { const local = { egress: { [MEMORY_ROUTE]: { destinations: ["api.example.com"] } } }; diff --git a/src/core/project/runtimeProfile/rules/additions.ts b/src/core/project/runtimeProfile/rules/additions.ts index 1b4874b30..bbeea34aa 100644 --- a/src/core/project/runtimeProfile/rules/additions.ts +++ b/src/core/project/runtimeProfile/rules/additions.ts @@ -22,6 +22,7 @@ import { posix } from "node:path"; import { agentCoreDataPlaneHost } from "../../../../handlers/project/shortcuts"; import { hasDefaultMemory } from "../../boxMemory"; +import { parseDestination } from "../../boxNetwork/egress"; import { AGENTCORE_CODEZIP_PACK_NAME, AGENTCORE_PACK_NAME, @@ -232,9 +233,10 @@ export function apply(table: Readonly, context: RuntimeProfileContext /** * `[egress]` with the {@link MEMORY_ROUTE} route when the runtime has its default Memory: one * destination, the AgentCore host of the build region, signed with a bare `aws://`, the execution - * role, and no backend. A route or HTTP MCP server of the profile that already has that host keeps - * it, so the box has one route to the host. A route of the user named {@link MEMORY_ROUTE} for - * another host is a problem. + * role, and no backend. A direct route or HTTP MCP server of the profile, one with no backend, that + * already has that host keeps it, so the box has one route to the host. One that sends the host's + * calls through the Box gateway is a problem: the gateway would sign the Memory calls with its own + * role. A route of the user named {@link MEMORY_ROUTE} for another host is a problem too. */ function runtimeEgress( egress: unknown, @@ -246,19 +248,38 @@ function runtimeEgress( // box refuses `[egress]` that is not a table before build gets the profile. if (egress !== undefined && !isTable(egress)) return egress; const host = agentCoreDataPlaneHost(context.region); - const routes = [...Object.values(egress ?? {}), ...Object.values(tableOf(mcp))]; - const hasHost = (entry: unknown) => - isTable(entry) && Array.isArray(entry.destinations) && entry.destinations.includes(host); - if (routes.some(hasHost)) return egress; + const routes = [ + ...Object.entries(egress ?? {}).map(([name, entry]) => ({ key: ["egress", name], entry })), + ...Object.entries(tableOf(mcp)).map(([name, entry]) => ({ key: ["mcp", name], entry })), + ].filter(({ entry }) => isTable(entry) && hasDestination(entry.destinations, host)); + const throughGateway = routes.filter(({ entry }) => (entry as TomlTable).backends !== undefined); + for (const { key } of throughGateway) { + problems.push({ + code: "RUNTIME_MEMORY_THROUGH_GATEWAY", + key, + place: context.placeOf(key), + reason: + `[${key.join(".")}] sends the calls to ${host} through the Box gateway on AgentCore ` + + "Runtime, and the gateway would sign the agent's Memory calls with its own role. Then " + + "Memory cannot hold each call to the session of the runtime. Remove that entry: build " + + "sends the Memory calls to Memory directly, signed with the execution role.", + }); + } + if (routes.length > 0) return egress; if (egress !== undefined && Object.hasOwn(egress, MEMORY_ROUTE)) { const key = ["egress", MEMORY_ROUTE]; + // The gateway rule names a route for a target that no entry of the profile names after it. + const target = context.gateway.targets.some(({ name }) => name === MEMORY_ROUTE); problems.push({ code: "RUNTIME_ROUTE_NAME_TAKEN", key, - place: context.placeOf(key), + place: target ? "agentcore/agentcore.json" : context.placeOf(key), reason: `[egress.${MEMORY_ROUTE}] is the name of the route that the agent's Memory calls take ` + - `on AgentCore Runtime, to ${host}. Rename that entry.`, + `on AgentCore Runtime, to ${host}. ` + + (target + ? `Rename the '${MEMORY_ROUTE}' target of gateway '${context.gateway.name}'.` + : "Rename that entry."), }); return egress; } @@ -268,6 +289,22 @@ function runtimeEgress( }; } +/** + * Whether `destinations` has one that box matches to `host` on port 443: the host itself, in any + * case, with or without `:443`, or a `*.` wildcard of its parent domains. + */ +function hasDestination(destinations: unknown, host: string): boolean { + if (!Array.isArray(destinations)) return false; + return destinations.some((text) => { + const destination = typeof text === "string" ? parseDestination(text) : undefined; + if (destination === undefined || destination.port !== 443) return false; + const name = destination.host.toLowerCase(); + return destination.wildcard + ? name.startsWith("*.") && host.endsWith(name.slice(1)) + : name === host; + }); +} + /** The user's packs in their order, then each Runtime pack that they do not import. */ function runtimePacks(packs: unknown, context: RuntimeProfileContext): unknown { // box refuses `packs` that is not a list before build gets the profile. diff --git a/src/core/project/runtimeProfile/types.ts b/src/core/project/runtimeProfile/types.ts index cbb4aa716..a839b9a15 100644 --- a/src/core/project/runtimeProfile/types.ts +++ b/src/core/project/runtimeProfile/types.ts @@ -100,6 +100,8 @@ export const RUNTIME_PROFILE_PROBLEM_CODES = [ "RUNTIME_TELEMETRY_REFUSED", /** A route of the user under the name of a route that only the Runtime profile has. */ "RUNTIME_ROUTE_NAME_TAKEN", + /** A route that would send the agent's Memory calls through the Box gateway, not directly. */ + "RUNTIME_MEMORY_THROUGH_GATEWAY", ] as const; export type RuntimeProfileProblemCode = (typeof RUNTIME_PROFILE_PROBLEM_CODES)[number]; From 78801c2106246d25f9ee008a986fc5df6d9d0c6d Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Mon, 23 Nov 2026 12:21:02 -0800 Subject: [PATCH 03/11] fix(box): Sign only the /memories path on the direct Memory route, and send a profile route to the host directly The direct Memory route opened every API of the AgentCore data-plane host to the execution role, such as the identity and API key calls. It now names `bedrock-agentcore..amazonaws.com/memories`, so box signs only the Memory calls. A call to another API of the host goes unsigned, and AgentCore refuses it. A route of the profile to the AgentCore host that the gateway rule sends through the Box gateway stopped build with RUNTIME_MEMORY_THROUGH_GATEWAY. That broke profiles that need the host under `agentcore dev`, for Code Interpreter or Browser. On the Runtime such a route now goes to the host directly, signed with a bare `aws://`, with its name and other keys, and build warns that it signs every API of the host. Box refuses the Memory route beside a signed route that covers its path, so build adds none there. A direct route to the host that box does not sign with `aws://` gets a warning, as the Memory calls then fail. Build keeps the Memory policy file of an earlier build while a gateway that is not a Box gateway lists it. The upgrade test now builds twice. The reserved Box gateway target names keep `agentcore`, now as the name of the Memory route. The README and the local testing guide say that box signs only `/memories`, and that a PRIVATE_VPC runtime needs the secure VPC's AgentCore endpoint. --- README.md | 4 +- docs/box-local-testing.md | 2 + src/core/project/boxNetworkWriters.test.ts | 17 +++ src/core/project/boxNetworkWriters.ts | 17 ++- src/core/project/manager.boxNetwork.test.ts | 48 ++++---- src/core/project/manager.test.ts | 2 +- .../runtimeProfile/rules/additions.test.ts | 85 ++++++++++---- .../project/runtimeProfile/rules/additions.ts | 109 ++++++++++++------ .../runtimeProfile/rules/gateway.test.ts | 2 +- src/core/project/runtimeProfile/types.ts | 2 - 10 files changed, 197 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index c3eb765b1..d291341cb 100644 --- a/README.md +++ b/README.md @@ -264,7 +264,9 @@ A boxed agent gets the deployed Memory IDs and the Runtime's region inside the b the `AGENTCORE_GATEWAY__URL` variables of gateways you add yourself: its calls go out through its Box gateway. Its calls to its default Memory are the exception. Box signs them with the execution role and sends them to Memory directly, so Memory can hold each call to the session of -the runtime. A runtime in VPC network mode needs a `bedrock-agentcore` VPC endpoint for them. +the runtime. Box signs only the `/memories` path of the AgentCore host on that route. A +`PRIVATE_VPC` runtime reaches Memory through the AgentCore endpoint of its private VPC. A runtime +in a VPC of your own needs a `bedrock-agentcore` VPC endpoint for these calls. A Box project that an older CLI created still has `start.sh`, or a Dockerfile that runs it. Build and deploy now render profiles that those files cannot start. Create the project again, or copy diff --git a/docs/box-local-testing.md b/docs/box-local-testing.md index 9f4bd91a7..3bd8f5f85 100644 --- a/docs/box-local-testing.md +++ b/docs/box-local-testing.md @@ -41,6 +41,8 @@ to it. [aws/agentcore-l3-cdk-constructs#470](https://github.com/aws/agentcore-l3-cdk-constructs/pull/470), as its `box-integration` branch does. The Box gateway that `create` writes has `enableApplicationLogs`, and an older L3 refuses it at synth with a `ConfigValidationError`. + For a `PRIVATE_VPC` runtime with Memory, the L3 checkout also needs the `fix/secure-vpc-memory-endpoint` + change, which gives the private VPC its AgentCore endpoint. Without it, the box cannot reach Memory. - `box` `0.1.11-preview.10` on your `PATH`, the release the CLI pins. The oldest box that works is `0.1.11-linux-preview.7`, the first release with [strands-agents/staging-boxy#625](https://github.com/strands-agents/staging-boxy/pull/625), which diff --git a/src/core/project/boxNetworkWriters.test.ts b/src/core/project/boxNetworkWriters.test.ts index fbb7e8ce8..3670d94d9 100644 --- a/src/core/project/boxNetworkWriters.test.ts +++ b/src/core/project/boxNetworkWriters.test.ts @@ -307,6 +307,23 @@ describe("defaultTargetsWriter", () => { ]); }); + test("keeps the Memory policy file of an earlier build while a gateway that is not a Box gateway lists it", () => { + const project = spec(); + project.agentCoreGateways.push({ + name: "tools", + authorizerType: "AWS_IAM", + targets: [], + additionalPolicies: ["agentcore/box-memory-policy.json"], + } as never); + + const edits = run([defaultTargetsWriter], [network()], project); + + expect(edits.files.map(({ path }) => path)).toEqual([BOX_BEDROCK_POLICY_FILE]); + expect( + edits.spec.agentCoreGateways.find(({ name }) => name === "tools")?.additionalPolicies, + ).toEqual(["agentcore/box-memory-policy.json"]); + }); + test("writes the model target for the region of the build", () => { const edits = run([defaultTargetsWriter], [network({ region: "eu-central-1" })]); diff --git a/src/core/project/boxNetworkWriters.ts b/src/core/project/boxNetworkWriters.ts index 451a1a279..9a1a98f81 100644 --- a/src/core/project/boxNetworkWriters.ts +++ b/src/core/project/boxNetworkWriters.ts @@ -9,7 +9,7 @@ import { dirname, join } from "node:path"; import { isDeepStrictEqual } from "node:util"; import type { EnvLocalEntry } from "../../handlers/project/types"; import type { AgentCoreGatewayTarget } from "../../projectSchemas/gateway"; -import type { ProjectSpec } from "../../projectSchemas/project"; +import { isBoundGateway, type ProjectSpec } from "../../projectSchemas/project"; import { boxGatewayOf, gatewayTargetFor, @@ -317,7 +317,8 @@ function undeclaredKey( /** * The `bedrock` target that each box runtime's model calls take, and the Bedrock policy of the * gateways' role, as `addBoxGatewayTargets` adds them. It removes the Memory policy file that an - * earlier build wrote, as `addBoxGatewayTargets` removes its listing. + * earlier build wrote, as `addBoxGatewayTargets` removes its listing, unless a gateway that is not + * a Box gateway still lists it. */ export const defaultTargetsWriter: BoxNetworkWriter = { name: "default targets", @@ -340,7 +341,17 @@ export const defaultTargetsWriter: BoxNetworkWriter = { .flatMap(({ path, policy }): BoxNetworkFile[] => policy ? [{ path, text: `${JSON.stringify(policy, undefined, 2)}\n` }] : [], ) - .concat({ path: LEGACY_BOX_MEMORY_POLICY_FILE, remove: true }), + .concat( + // removeLegacyMemoryAccess drops the listing only from the Box gateways. A gateway that + // is not a Box gateway lists the file only when the user listed it there. + spec.agentCoreGateways.some( + (gateway) => + !isBoundGateway(gateway) && + gateway.additionalPolicies?.includes(LEGACY_BOX_MEMORY_POLICY_FILE), + ) + ? [] + : [{ path: LEGACY_BOX_MEMORY_POLICY_FILE, remove: true }], + ), }; }, }; diff --git a/src/core/project/manager.boxNetwork.test.ts b/src/core/project/manager.boxNetwork.test.ts index 87b736359..3a09591b8 100644 --- a/src/core/project/manager.boxNetwork.test.ts +++ b/src/core/project/manager.boxNetwork.test.ts @@ -303,7 +303,7 @@ describe("build writes the Box network of the box profile", () => { expect(egress["egress-httpbin-org"]).toEqual(gatewayRoute("egress-httpbin-org", "httpbin.org")); // The Memory route has no backend: box signs it with the execution role, for Memory itself. expect(egress.agentcore).toEqual({ - destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], + destinations: ["bedrock-agentcore.us-west-2.amazonaws.com/memories"], secret: { ref: "aws://" }, }); expect(egress.bedrock).toEqual( @@ -501,33 +501,25 @@ describe("build writes the Box network of the box profile", () => { ); }); - test("stops for an entry of the profile that would send the Memory calls through the gateway", async () => { + test("sends an entry of the profile for the Memory host directly on the Runtime, with its name, and warns", async () => { const { manager, root, box } = await strandsProject(); + const host = "bedrock-agentcore.us-west-2.amazonaws.com"; await editProfile(root, { - tables: - '\n[egress.memory]\ndestinations = ["bedrock-agentcore.us-west-2.amazonaws.com"]\n' + - 'secret.ref = "aws://default"\n', - }); - box.answer = shows({ - memory: { - destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], - secret: { ref: "aws://default" }, - }, + tables: `\n[egress.tools]\ndestinations = ["${host}"]\nsecret.ref = "aws://default"\n`, }); + box.answer = shows({ tools: { destinations: [host], secret: { ref: "aws://default" } } }); - const error = await build(manager, root).then( - () => undefined, - (caught: unknown) => caught, - ); + const { warnings } = await build(manager, root); - // On the Runtime the entry gets a gateway target, which would sign the Memory calls with the - // gateway's role, and Memory could not hold each call to the session of the runtime. - expect(error).toBeInstanceOf(InputValidationError); - expect((error as Error).message).toContain( - "[egress.memory] sends the calls to bedrock-agentcore.us-west-2.amazonaws.com through the " + - "Box gateway on AgentCore Runtime", + // `agentcore dev` keeps the entry, for Code Interpreter or Browser. On the Runtime it goes to + // the host directly, signed with the execution role, so Memory can hold each call to the + // session; box refuses the Memory route beside it. + const egress = (await runtimeProfile(root)).egress as Record; + expect(Object.keys(egress)).toEqual(["bedrock", "tools"]); + expect(egress.tools).toEqual({ destinations: [host], secret: { ref: "aws://" } }); + expect(warnings).toContainEqual( + expect.stringContaining(`[egress.tools] goes to ${host} directly on AgentCore Runtime`), ); - expect(await targetNames(root)).not.toContain("egress-memory"); }); test("removes the Memory target, policy listing, and policy file of an earlier build, and sends Memory directly", async () => { @@ -570,9 +562,15 @@ describe("build writes the Box network of the box profile", () => { ); const egress = (await runtimeProfile(root)).egress as Record; expect(egress.agentcore).toEqual({ - destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], + destinations: ["bedrock-agentcore.us-west-2.amazonaws.com/memories"], secret: { ref: "aws://" }, }); + + // A second build finds nothing of the earlier build, and changes nothing. + const spec = await text(root, SPEC); + const again = await build(manager, root); + expect(again.steps.filter((step) => step.startsWith("removing"))).toEqual([]); + expect(await text(root, SPEC)).toBe(spec); }); test("keeps an agentcore target that the user changed, and stops until the user renames it", async () => { @@ -1093,7 +1091,7 @@ describe("build writes the Bedrock target and the Memory route for the region it ); // The Memory route goes to the Memory host of that region, with no backend. expect(egress.agentcore as unknown).toEqual({ - destinations: ["bedrock-agentcore.eu-central-1.amazonaws.com"], + destinations: ["bedrock-agentcore.eu-central-1.amazonaws.com/memories"], secret: { ref: "aws://" }, }); }); @@ -1257,7 +1255,7 @@ describe("deploy writes the Box network before the backend deploys", () => { const authOf = async (target: string) => (await boxGateway(root)).targets.find(({ name }) => name === target)?.outboundAuth; const routeFor = (region: string) => ({ - destinations: [`bedrock-agentcore.${region}.amazonaws.com`], + destinations: [`bedrock-agentcore.${region}.amazonaws.com/memories`], secret: { ref: "aws://" }, }); const bedrockFor = (region: string) => [ diff --git a/src/core/project/manager.test.ts b/src/core/project/manager.test.ts index 47295845d..f0496291e 100644 --- a/src/core/project/manager.test.ts +++ b/src/core/project/manager.test.ts @@ -2224,7 +2224,7 @@ describe("FsProjectManager.build", () => { // Build writes both for the region it builds for, which the environment of the test sets. const region = box.targets[0].outboundAuth.region as string; expect(profile.egress.agentcore).toEqual({ - destinations: [`bedrock-agentcore.${region}.amazonaws.com`], + destinations: [`bedrock-agentcore.${region}.amazonaws.com/memories`], secret: { ref: "aws://" }, }); diff --git a/src/core/project/runtimeProfile/rules/additions.test.ts b/src/core/project/runtimeProfile/rules/additions.test.ts index b30290615..5b5a0fcf6 100644 --- a/src/core/project/runtimeProfile/rules/additions.test.ts +++ b/src/core/project/runtimeProfile/rules/additions.test.ts @@ -35,7 +35,7 @@ const BUILD_REGION = "us-west-2"; /** The route of the agent's Memory calls: to Memory directly, signed with the execution role. */ const DIRECT_MEMORY_ROUTE = { - destinations: [`bedrock-agentcore.${BUILD_REGION}.amazonaws.com`], + destinations: [`bedrock-agentcore.${BUILD_REGION}.amazonaws.com/memories`], secret: { ref: "aws://" }, }; @@ -990,8 +990,11 @@ describe("the additions rule", () => { expect(problems).toEqual([]); const route = (table.egress as TomlTable)[MEMORY_ROUTE]; expect(route).toEqual(DIRECT_MEMORY_ROUTE); - // No backend: the Box gateway would sign the calls with its own role. + // No backend: the Box gateway would sign the calls with its own role. The /memories path + // limits what box signs: a call to another API of the host, such as an API key call, goes + // unsigned, and AgentCore refuses it. expect(route).not.toHaveProperty("backends"); + expect((route as { destinations: string[] }).destinations[0]).toEndWith("/memories"); }); test("keeps each route of the profile, and adds the Memory route after them", () => { @@ -1014,14 +1017,35 @@ describe("the additions rule", () => { expect(other.table.egress).not.toHaveProperty(MEMORY_ROUTE); }); - test("adds no second route to the Memory host when a route or HTTP MCP server of the profile has it", () => { + test("adds no second route to the Memory host when a direct route or HTTP MCP server of the profile has it", () => { const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; const context = contextOf(CODEZIP, { memories: [MEMORY] }); - const routed = { egress: { memory: { destinations: [host] } } }; - const served = { mcp: { memory: { type: "http", destinations: [host] } } }; + const signed = { secret: { ref: "aws://" } }; + const routed = { egress: { memory: { destinations: [host], ...signed } } }; + const served = { mcp: { memory: { type: "http", destinations: [host], ...signed } } }; - expect(apply(routed, context).table.egress).toEqual(routed.egress); - expect(apply(served, context).table.egress).toBeUndefined(); + expect(apply(routed, context)).toMatchObject({ + table: { egress: routed.egress }, + warnings: [], + }); + const mcp = apply(served, context); + expect(mcp.table.egress).toBeUndefined(); + expect(mcp.table.mcp).toEqual(served.mcp); + expect(mcp.warnings).toEqual([]); + }); + + test("warns for a direct route to the Memory host that box does not sign with the execution role", () => { + const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; + const local = { egress: { memory: { destinations: [host], secret: { ref: "env://KEY" } } } }; + + const { table, warnings } = apply(local, contextOf(CODEZIP, { memories: [MEMORY] })); + + expect(table.egress).toEqual(local.egress); + expect(warnings).toEqual([ + `[egress.memory] goes to ${host} directly on AgentCore Runtime, and box does not sign it ` + + "with the execution role (aws://), so the agent's Memory calls fail. Sign it with " + + `aws://, or remove it: build then adds [egress.${MEMORY_ROUTE}].`, + ]); }); test("counts a direct route to the Memory host with :443, in another case, or as a wildcard", () => { @@ -1031,7 +1055,9 @@ describe("the additions rule", () => { `Bedrock-AgentCore.${BUILD_REGION}.amazonaws.com`, "*.amazonaws.com", ]) { - const local = { egress: { memory: { destinations: [destination] } } }; + const local = { + egress: { memory: { destinations: [destination], secret: { ref: "aws://" } } }, + }; const { table, problems } = apply(local, context); expect([destination, problems]).toEqual([destination, []]); @@ -1051,34 +1077,47 @@ describe("the additions rule", () => { ]); }); - test("stops build for a route that sends the Memory calls through the Box gateway", () => { + test("sends a route of the profile to the Memory host directly, not through the Box gateway, and warns", () => { const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; - const backends = [{ url: "${{ inputs.gateway.url }}/egress-memory" }]; + const backends = [{ url: "${{ inputs.gateway.url }}/egress-tools" }]; const local = { - egress: { memory: { destinations: [host], backends } }, + egress: { tools: { destinations: [host], backends, when: "true" } }, mcp: { recall: { type: "http", destinations: [host], backends } }, }; - const { table, problems } = apply(local, contextOf(CODEZIP, { memories: [MEMORY] })); + const { table, problems, warnings } = apply( + local, + contextOf(CODEZIP, { memories: [MEMORY] }), + ); - expect(problems.map(({ code, key }) => [code, key])).toEqual([ - ["RUNTIME_MEMORY_THROUGH_GATEWAY", ["egress", "memory"]], - ["RUNTIME_MEMORY_THROUGH_GATEWAY", ["mcp", "recall"]], - ]); - expect(problems[0]!.reason).toBe( - `[egress.memory] sends the calls to ${host} through the Box gateway on AgentCore Runtime, ` + - "and the gateway would sign the agent's Memory calls with its own role. Then Memory " + - "cannot hold each call to the session of the runtime. Remove that entry: build sends " + - "the Memory calls to Memory directly, signed with the execution role.", + // Each keeps its name and other keys. Box refuses the Memory route beside a signed route that + // covers its path, so build adds none. + expect(problems).toEqual([]); + expect(table.egress).toEqual({ + tools: { destinations: [host], when: "true", secret: { ref: "aws://" } }, + }); + expect(table.mcp).toEqual({ + recall: { type: "http", destinations: [host], secret: { ref: "aws://" } }, + }); + expect(warnings).toEqual( + ["[egress.tools]", "[mcp.recall]"].map( + (label) => + `${label} goes to ${host} directly on AgentCore Runtime, signed with the execution ` + + "role, and not through the Box gateway, so Memory can hold each call to the session of " + + "the runtime. Box signs every API of that host on this route, not only Memory. Remove " + + `${label} if the agent calls only Memory there: build then adds [egress.${MEMORY_ROUTE}], ` + + "which box signs only for Memory.", + ), ); - expect(table.egress).not.toHaveProperty(MEMORY_ROUTE); }); test("leaves a gateway route to the Memory host alone for a runtime with no default Memory", () => { const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; const local = { egress: { memory: { destinations: [host], backends: [{ url: "x" }] } } }; - expect(apply(local, contextOf(CODEZIP)).problems).toEqual([]); + const { table, problems, warnings } = apply(local, contextOf(CODEZIP)); + expect([problems, warnings]).toEqual([[], []]); + expect(table.egress).toEqual(local.egress); }); test("names the gateway target when a target of the user takes the Memory route's name", () => { diff --git a/src/core/project/runtimeProfile/rules/additions.ts b/src/core/project/runtimeProfile/rules/additions.ts index 8e8cfe1fb..fbf79d623 100644 --- a/src/core/project/runtimeProfile/rules/additions.ts +++ b/src/core/project/runtimeProfile/rules/additions.ts @@ -15,7 +15,8 @@ * | `[telemetry.]` | each CloudWatch target that box takes is signed with a bare `aws://`, in the deploy region when it names none; one that box refuses goes; a `file` target goes, with a warning | * | `[telemetry.agentcore]` | added when no target is `cloudwatch_logs`: the runtime's own log group and log stream | * | `[telemetry.agentcore_traces]` | added with OTEL on when no target is `cloudwatch_traces`: X-Ray in the Runtime's region | - * | `[egress.agentcore]` | added when the runtime has its default Memory and no route of the profile has the AgentCore host: box signs the Memory calls with a bare `aws://` and sends them to Memory directly, not through the Box gateway | + * | `[egress.agentcore]` | added when the runtime has its default Memory and no route of the profile has the AgentCore host: the host's `/memories` path, which box signs with a bare `aws://` and sends to Memory directly, not through the Box gateway | + * | a route to the AgentCore host | with a default Memory, one through the Box gateway goes to the host directly, signed with a bare `aws://`, with a warning; one that is direct and not signed so gets a warning | * * It moves no path: the paths rule runs after it and moves each path of the user. */ @@ -54,6 +55,12 @@ import { */ export const MEMORY_ROUTE = BOX_MEMORY_ROUTE_NAME; +/** + * The path of the AgentCore data-plane host that the Memory calls take: `/memories` and each path + * under it. Box signs only those calls on the Memory route. + */ +export const MEMORY_PATH = "/memories"; + /** The variables of the region of the agent, which get the region of the Runtime. */ const REGION_ENV_NAMES: ReadonlySet = new Set(["AWS_REGION", "AWS_DEFAULT_REGION"]); @@ -225,48 +232,74 @@ export function apply(table: Readonly, context: RuntimeProfileContext } result.telemetry = runtimeTelemetry(result.telemetry, context, warnings); const problems: RuntimeProfileProblem[] = []; - const egress = runtimeEgress(result.egress, result.mcp, context, problems); - if (egress !== undefined) result.egress = egress; + const routes = memoryRoutes(result.egress, result.mcp, context, problems, warnings); + if (routes.egress !== undefined) result.egress = routes.egress; + if (routes.mcp !== undefined) result.mcp = routes.mcp; return { table: result, problems, warnings }; } /** - * `[egress]` with the {@link MEMORY_ROUTE} route when the runtime has its default Memory: one - * destination, the AgentCore host of the build region, signed with a bare `aws://`, the execution - * role, and no backend. A direct route or HTTP MCP server of the profile, one with no backend, that - * already has that host keeps it, so the box has one route to the host. One that sends the host's - * calls through the Box gateway is a problem: the gateway would sign the Memory calls with its own - * role. A route of the user named {@link MEMORY_ROUTE} for another host is a problem too. + * `[egress]` and `[mcp]` for the agent's Memory calls when the runtime has its default Memory. They + * go to Memory directly, signed with the execution role, not through the Box gateway, whose role + * would sign them and keep Memory from holding each call to the session of the runtime. + * + * - With no route of the profile to the AgentCore host, build adds {@link MEMORY_ROUTE}: the + * `/memories` path of that host, signed with a bare `aws://`. Box signs only the calls under that + * path. A call to another API of the host, such as an identity or API key call, goes unsigned, + * and AgentCore refuses it. + * - A route or HTTP MCP server of the profile that the gateway rule sends to that host through the + * gateway goes to the host directly, signed with a bare `aws://`, with its name and other keys. + * Box refuses the Memory route beside a signed route that covers its path. So build adds none, + * and warns that the route signs every API of the host with the execution role. + * - A direct route of the profile to that host stays. Build adds none beside it, and warns when + * box does not sign it with `aws://`, as the Memory calls then fail. + * - A route of the user named {@link MEMORY_ROUTE} for another host is a problem. */ -function runtimeEgress( +function memoryRoutes( egress: unknown, mcp: unknown, context: RuntimeProfileContext, problems: RuntimeProfileProblem[], -): unknown { - if (!hasDefaultMemory(context.spec, context.runtime)) return egress; - // box refuses `[egress]` that is not a table before build gets the profile. - if (egress !== undefined && !isTable(egress)) return egress; + warnings: string[], +): { egress: unknown; mcp: unknown } { + if (!hasDefaultMemory(context.spec, context.runtime)) return { egress, mcp }; + // box refuses `[egress]` or `[mcp]` that is not a table before build gets the profile. + if ((egress !== undefined && !isTable(egress)) || (mcp !== undefined && !isTable(mcp))) { + return { egress, mcp }; + } const host = agentCoreDataPlaneHost(context.region); - const routes = [ - ...Object.entries(egress ?? {}).map(([name, entry]) => ({ key: ["egress", name], entry })), - ...Object.entries(tableOf(mcp)).map(([name, entry]) => ({ key: ["mcp", name], entry })), - ].filter(({ entry }) => isTable(entry) && hasDestination(entry.destinations, host)); - const throughGateway = routes.filter(({ entry }) => (entry as TomlTable).backends !== undefined); - for (const { key } of throughGateway) { - problems.push({ - code: "RUNTIME_MEMORY_THROUGH_GATEWAY", - key, - place: context.placeOf(key), - reason: - `[${key.join(".")}] sends the calls to ${host} through the Box gateway on AgentCore ` + - "Runtime, and the gateway would sign the agent's Memory calls with its own role. Then " + - "Memory cannot hold each call to the session of the runtime. Remove that entry: build " + - "sends the Memory calls to Memory directly, signed with the execution role.", - }); + const tables = { egress: { ...egress }, mcp: { ...mcp } } as Record<"egress" | "mcp", TomlTable>; + let routed = false; + for (const kind of ["egress", "mcp"] as const) { + for (const [name, entry] of Object.entries(tables[kind])) { + if (!isTable(entry) || !hasDestination(entry.destinations, host)) continue; + routed = true; + const label = `[${kind}.${name}]`; + if (entry.backends !== undefined) { + const { backends: _backends, secret: _secret, ...kept } = entry; + tables[kind][name] = { ...kept, secret: { ref: RUNTIME_AWS_SIGNER } }; + warnings.push( + `${label} goes to ${host} directly on AgentCore Runtime, signed with the execution ` + + "role, and not through the Box gateway, so Memory can hold each call to the session " + + "of the runtime. Box signs every API of that host on this route, not only Memory. " + + `Remove ${label} if the agent calls only Memory there: build then adds ` + + `[egress.${MEMORY_ROUTE}], which box signs only for Memory.`, + ); + } else if (!isTable(entry.secret) || entry.secret.ref !== RUNTIME_AWS_SIGNER) { + warnings.push( + `${label} goes to ${host} directly on AgentCore Runtime, and box does not sign it with ` + + `the execution role (${RUNTIME_AWS_SIGNER}), so the agent's Memory calls fail. Sign it ` + + `with ${RUNTIME_AWS_SIGNER}, or remove it: build then adds [egress.${MEMORY_ROUTE}].`, + ); + } + } } - if (routes.length > 0) return egress; - if (egress !== undefined && Object.hasOwn(egress, MEMORY_ROUTE)) { + const result = { + egress: Object.keys(tables.egress).length > 0 ? tables.egress : egress, + mcp: Object.keys(tables.mcp).length > 0 ? tables.mcp : mcp, + }; + if (routed) return result; + if (Object.hasOwn(tables.egress, MEMORY_ROUTE)) { const key = ["egress", MEMORY_ROUTE]; // The gateway rule names a route for a target that no entry of the profile names after it. const target = context.gateway.targets.some(({ name }) => name === MEMORY_ROUTE); @@ -281,11 +314,17 @@ function runtimeEgress( ? `Rename the '${MEMORY_ROUTE}' target of gateway '${context.gateway.name}'.` : "Rename that entry."), }); - return egress; + return result; } return { - ...egress, - [MEMORY_ROUTE]: { destinations: [host], secret: { ref: RUNTIME_AWS_SIGNER } }, + ...result, + egress: { + ...tables.egress, + [MEMORY_ROUTE]: { + destinations: [`${host}${MEMORY_PATH}`], + secret: { ref: RUNTIME_AWS_SIGNER }, + }, + }, }; } diff --git a/src/core/project/runtimeProfile/rules/gateway.test.ts b/src/core/project/runtimeProfile/rules/gateway.test.ts index 5ddbce001..ab72c2a20 100644 --- a/src/core/project/runtimeProfile/rules/gateway.test.ts +++ b/src/core/project/runtimeProfile/rules/gateway.test.ts @@ -499,7 +499,7 @@ const withDirectMemory = (egress: unknown) => { const result = withEntryKeys(egress); if (Object.hasOwn(result, "agentcore")) { result.agentcore = { - destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], + destinations: ["bedrock-agentcore.us-west-2.amazonaws.com/memories"], secret: { ref: "aws://" }, }; } diff --git a/src/core/project/runtimeProfile/types.ts b/src/core/project/runtimeProfile/types.ts index a839b9a15..cbb4aa716 100644 --- a/src/core/project/runtimeProfile/types.ts +++ b/src/core/project/runtimeProfile/types.ts @@ -100,8 +100,6 @@ export const RUNTIME_PROFILE_PROBLEM_CODES = [ "RUNTIME_TELEMETRY_REFUSED", /** A route of the user under the name of a route that only the Runtime profile has. */ "RUNTIME_ROUTE_NAME_TAKEN", - /** A route that would send the agent's Memory calls through the Box gateway, not directly. */ - "RUNTIME_MEMORY_THROUGH_GATEWAY", ] as const; export type RuntimeProfileProblemCode = (typeof RUNTIME_PROFILE_PROBLEM_CODES)[number]; From c411bae48dae552ff25288a470fec1c195cfaa3a Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Mon, 23 Nov 2026 12:49:51 -0800 Subject: [PATCH 04/11] fix(box): Write no gateway target for a host that the Runtime profile sends directly A route of the profile to the AgentCore host goes to the host directly on AgentCore Runtime when the runtime has its default Memory. Build still wrote the host's `egress-` gateway target, which the Runtime did not use, and the gateway IAM check then warned about the gateway role's grants for it. The gateway targets writer now writes a managed target only when a route or MCP server of the derived Runtime profile sends calls to it through a backend. A target that an earlier build wrote for a host that now goes directly is removed, with a step message that says why. --- src/core/project/boxNetworkWriters.test.ts | 61 ++++++++++++++++++++- src/core/project/boxNetworkWriters.ts | 40 +++++++++++++- src/core/project/manager.boxNetwork.test.ts | 10 ++++ 3 files changed, 108 insertions(+), 3 deletions(-) diff --git a/src/core/project/boxNetworkWriters.test.ts b/src/core/project/boxNetworkWriters.test.ts index 3670d94d9..6b3777862 100644 --- a/src/core/project/boxNetworkWriters.test.ts +++ b/src/core/project/boxNetworkWriters.test.ts @@ -17,7 +17,24 @@ import { AGENTCORE_CODEZIP_PACK_PATH, AGENTCORE_PACK_PATH } from "./templates/bo import { serializeRuntimeProfile } from "./runtimeProfile/serialize"; /** A hand-built model of runtime `agent`, which each test changes as it needs. */ +/** + * A network of the runtime `agent`. Its Runtime profile routes each host with a target through the + * gateway, as the gateway rule writes it, unless `overrides` gives another profile. + */ function network(overrides: Partial = {}): BoxNetwork { + const routes = (overrides.hosts ?? []).flatMap((each) => + each.target === undefined + ? [] + : [ + [ + each.target, + { + destinations: [each.authority], + backends: [{ url: `\${{ inputs.gateway.url }}/${each.target}` }], + }, + ], + ], + ); return { runtime: "agent", gateway: "agent-box-egress", @@ -25,7 +42,11 @@ function network(overrides: Partial = {}): BoxNetwork { hosts: [], envRoutes: [], policyFiles: [], - runtimeProfile: { packs: ["@box/base", "@box/agentcore"], profile: { name: "demo" } }, + runtimeProfile: { + packs: ["@box/base", "@box/agentcore"], + profile: { name: "demo" }, + ...(routes.length > 0 && { egress: Object.fromEntries(routes) }), + }, runtimeProfileHeader: ["Derived from .box/agent/local/profile.toml."], grantedActions: [], warnings: [], @@ -142,6 +163,44 @@ describe("boxNetworkEdits", () => { }); describe("gatewayTargetsWriter", () => { + test("writes no target for a host that the Runtime profile sends directly, and removes the one an earlier build wrote", () => { + const MEMORY_HOST = "bedrock-agentcore.us-west-2.amazonaws.com"; + // The additions rule sends a route to the AgentCore host directly when the runtime has its + // default Memory, so the route has no backend to the host's target. + const direct = (targets: AgentCoreGatewayTarget[]) => + run( + [gatewayTargetsWriter], + [ + network({ + hosts: [host(MEMORY_HOST, "egress-tools"), host("httpbin.org", "egress-httpbin-org")], + runtimeProfile: { + egress: { + tools: { destinations: [MEMORY_HOST], secret: { ref: "aws://" } }, + "egress-httpbin-org": { + destinations: ["httpbin.org"], + backends: [{ url: "${{ inputs.gateway.url }}/egress-httpbin-org" }], + }, + }, + }, + }), + ], + spec(targets), + ); + + const first = direct([MINE]); + expect(targetNames(first.spec)).toEqual(["mine", "egress-httpbin-org"]); + expect(first.messages).toEqual([ + "adding gateway target 'egress-httpbin-org' to 'agent-box-egress' for httpbin.org", + ]); + + const earlier = direct([MINE, passthrough("egress-tools", `https://${MEMORY_HOST}`)]); + expect(targetNames(earlier.spec)).toEqual(["mine", "egress-httpbin-org"]); + expect(earlier.messages).toContain( + "removing gateway target 'egress-tools' from 'agent-box-egress': on AgentCore Runtime the " + + `box sends the calls to ${MEMORY_HOST} directly, not through the gateway`, + ); + }); + test("adds a host's target, rewrites a managed one, removes a stale one, and keeps the user's", () => { const project = spec([ passthrough("ours", "https://ours.example.com"), diff --git a/src/core/project/boxNetworkWriters.ts b/src/core/project/boxNetworkWriters.ts index 9a1a98f81..c033503a9 100644 --- a/src/core/project/boxNetworkWriters.ts +++ b/src/core/project/boxNetworkWriters.ts @@ -253,8 +253,13 @@ function writeGatewayTargets(spec: ProjectSpec, network: BoxNetwork): string[] { ? [] : [{ ...host, target: host.target }], ); - const sent = served.filter((host) => undeclaredKey(spec, host) === undefined); - const unsent = served.filter((host) => undeclaredKey(spec, host) !== undefined); + // A host whose route the Runtime profile sends directly, not to its target, takes no target: the + // agent's Memory calls go to the AgentCore host directly (runtimeProfile/rules/additions.ts). + const routed = gatewayRoutedTargets(network.runtimeProfile); + const direct = served.filter((host) => routed !== undefined && !routed.has(host.target)); + const used = served.filter((host) => !direct.includes(host)); + const sent = used.filter((host) => undeclaredKey(spec, host) === undefined); + const unsent = used.filter((host) => undeclaredKey(spec, host) !== undefined); const existing = gateway.targets; const messages: string[] = []; const targets: AgentCoreGatewayTarget[] = []; @@ -267,6 +272,14 @@ function writeGatewayTargets(spec: ProjectSpec, network: BoxNetwork): string[] { const host = sent.find((candidate) => candidate.target === target.name); if (!host) { if (unsent.some((candidate) => candidate.target === target.name)) continue; + const directHost = direct.find((candidate) => candidate.target === target.name); + if (directHost) { + messages.push( + `removing gateway target '${target.name}' from ${gatewayName}: on AgentCore Runtime the ` + + `box sends the calls to ${directHost.authority} directly, not through the gateway`, + ); + continue; + } const at = target.passthrough && parseEndpoint(target.passthrough.endpoint); messages.push( `removing gateway target '${target.name}' from ${gatewayName}: the Box profile no ` + @@ -301,6 +314,29 @@ function writeGatewayTargets(spec: ProjectSpec, network: BoxNetwork): string[] { return messages; } +/** + * The names of the gateway targets that a route or MCP server of `profile` sends calls to, through + * a backend at `${{ inputs.gateway.url }}/`; `undefined` when build has no Runtime profile + * for the network. + */ +function gatewayRoutedTargets(profile: Readonly | undefined): Set | undefined { + if (profile === undefined) return undefined; + const prefix = `${RUNTIME_INPUTS.gatewayUrl}/`; + const names = new Set(); + for (const key of ["egress", "mcp"]) { + const table = profile[key]; + if (!isTable(table)) continue; + for (const entry of Object.values(table)) { + if (!isTable(entry) || !Array.isArray(entry.backends)) continue; + for (const backend of entry.backends) { + const url = isTable(backend) ? backend.url : undefined; + if (typeof url === "string" && url.startsWith(prefix)) names.add(url.slice(prefix.length)); + } + } + } + return names; +} + /** The API key that a host's target sends, when agentcore.json declares no credential for it. */ function undeclaredKey( spec: ProjectSpec, diff --git a/src/core/project/manager.boxNetwork.test.ts b/src/core/project/manager.boxNetwork.test.ts index 3a09591b8..2678c9d3b 100644 --- a/src/core/project/manager.boxNetwork.test.ts +++ b/src/core/project/manager.boxNetwork.test.ts @@ -520,6 +520,16 @@ describe("build writes the Box network of the box profile", () => { expect(warnings).toContainEqual( expect.stringContaining(`[egress.tools] goes to ${host} directly on AgentCore Runtime`), ); + // The Runtime does not use a gateway target for the host, so build writes none, and gives no + // warning about the gateway role's grants for one. + expect(await targetNames(root)).toEqual(["bedrock"]); + expect(warnings).not.toContainEqual(expect.stringContaining("Target 'egress-")); + + // A second build changes nothing. + const spec = await text(root, SPEC); + const again = await build(manager, root); + expect(again.steps.filter((step) => step.includes("gateway target"))).toEqual([]); + expect(await text(root, SPEC)).toBe(spec); }); test("removes the Memory target, policy listing, and policy file of an earlier build, and sends Memory directly", async () => { From ff7bd08b43019ef3717d1b43af63c36de47ef691 Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Mon, 23 Nov 2026 13:06:34 -0800 Subject: [PATCH 05/11] refactor(box): Remove the cleanup of what an earlier build wrote for Memory Box on AgentCore Runtime is not launched yet, and a Box project built before this change can be discarded. So build no longer removes the `agentcore` gateway target, the `agentcore/box-memory-policy.json` listing, or that file from an earlier build. A user target named `agentcore` still stops build, because its route would take the name of the Memory route. --- src/core/project/boxMemory.test.ts | 76 --------------------- src/core/project/boxMemory.ts | 70 ++----------------- src/core/project/boxNetwork/network.test.ts | 8 +-- src/core/project/boxNetworkWriters.test.ts | 31 ++------- src/core/project/boxNetworkWriters.ts | 37 ++-------- src/core/project/manager.boxNetwork.test.ts | 55 +-------------- 6 files changed, 20 insertions(+), 257 deletions(-) diff --git a/src/core/project/boxMemory.test.ts b/src/core/project/boxMemory.test.ts index ba5c7aae4..56b434a9f 100644 --- a/src/core/project/boxMemory.test.ts +++ b/src/core/project/boxMemory.test.ts @@ -6,8 +6,6 @@ import { addBoxGatewayTargets, BOX_BEDROCK_POLICY_FILE, boxBedrockPolicy, - LEGACY_BOX_MEMORY_POLICY_FILE, - removeLegacyMemoryAccess, } from "./boxMemory"; function spec(options: { entrypoint?: string; memories?: string[] } = {}): ProjectSpec { @@ -174,77 +172,3 @@ describe("addBoxGatewayTargets", () => { expect(gateway(project).targets[0]!.outboundAuth?.region).toBe("us-east-1"); }); }); - -describe("removeLegacyMemoryAccess", () => { - /** The `agentcore` target, in `region`, as a build before Memory went direct wrote it. */ - const legacy = (region: string, dnsSuffix = "amazonaws.com") => ({ - name: "agentcore", - targetType: "passthrough" as const, - passthrough: { - endpoint: `https://bedrock-agentcore.${region}.${dnsSuffix}`, - protocolType: "CUSTOM" as const, - }, - outboundAuth: { - type: "GATEWAY_IAM_ROLE" as const, - service: "bedrock-agentcore", - region, - }, - }); - - test("removes the agentcore target of an earlier build, in any region, and the policy listing", () => { - for (const [region, suffix] of [ - ["us-west-2", "amazonaws.com"], - ["eu-central-1", "amazonaws.com"], - ["cn-north-1", "amazonaws.com.cn"], - ] as const) { - const project = spec(); - gateway(project).targets = [getBoxGatewayBedrockTargetSpec(), legacy(region, suffix)]; - gateway(project).additionalPolicies = [ - BOX_BEDROCK_POLICY_FILE, - LEGACY_BOX_MEMORY_POLICY_FILE, - ]; - - expect(removeLegacyMemoryAccess(project)).toEqual([ - `removing target 'agentcore' from gateway '${GATEWAY}' in agentcore.json: the boxed agent's Memory calls go to Memory directly`, - `removing '${LEGACY_BOX_MEMORY_POLICY_FILE}' from gateway '${GATEWAY}' additionalPolicies in agentcore.json: its role no longer calls Memory`, - ]); - expect(targets(project)).toEqual(["bedrock"]); - expect(gateway(project).additionalPolicies).toEqual([BOX_BEDROCK_POLICY_FILE]); - } - }); - - test("drops additionalPolicies when the Memory file was its only entry", () => { - const project = spec(); - gateway(project).additionalPolicies = [LEGACY_BOX_MEMORY_POLICY_FILE]; - - removeLegacyMemoryAccess(project); - - expect(gateway(project).additionalPolicies).toBeUndefined(); - }); - - test("keeps an agentcore target that the user changed, and one on a gateway that is not a Box gateway", () => { - const project = spec(); - const changed = { ...legacy("us-west-2"), description: "mine" } as never; - gateway(project).targets = [changed]; - const tools = { - name: "tools", - authorizerType: "AWS_IAM", - targets: [legacy("us-west-2")], - } as ProjectSpec["agentCoreGateways"][number]; - project.agentCoreGateways.push(tools); - - expect(removeLegacyMemoryAccess(project)).toEqual([]); - expect(gateway(project).targets).toEqual([changed]); - expect(tools.targets).toHaveLength(1); - }); - - test("runs first in addBoxGatewayTargets", () => { - const project = spec(); - gateway(project).targets = [legacy("us-west-2")]; - - expect(addBoxGatewayTargets(project, "us-west-2")[0]).toStartWith( - "removing target 'agentcore'", - ); - expect(targets(project)).toEqual(["bedrock"]); - }); -}); diff --git a/src/core/project/boxMemory.ts b/src/core/project/boxMemory.ts index f80d16c33..efebe7425 100644 --- a/src/core/project/boxMemory.ts +++ b/src/core/project/boxMemory.ts @@ -3,7 +3,6 @@ import { partition } from "@aws-sdk/util-endpoints"; import { BOX_GATEWAY_BEDROCK_TARGET_NAME, DEFAULT_EGRESS_BEDROCK_REGION, - agentCoreDataPlaneHost, callsBedrockModel, getBoxGatewayBedrockTargetSpec, } from "../../handlers/project/shortcuts"; @@ -22,30 +21,11 @@ import { runtimeRunsInBox } from "./templates/boxProfile"; export const BOX_BEDROCK_POLICY_FILE = "agentcore/box-bedrock-policy.json"; /** - * The policy file, relative to the project root, that an earlier build wrote so the Box gateway's - * role could call Memory. The agent's Memory calls now go to Memory directly, so build removes it. - */ -export const LEGACY_BOX_MEMORY_POLICY_FILE = "agentcore/box-memory-policy.json"; - -/** - * The name of the Runtime profile route of the agent's Memory calls, which go to Memory directly. - * An earlier build gave the Box gateway target of those calls the same name. + * The name of the Runtime profile route of the agent's Memory calls, which go to Memory directly + * (runtimeProfile/rules/additions.ts). */ export const BOX_MEMORY_ROUTE_NAME = "agentcore"; -/** The name of the Box gateway target that an earlier build wrote for the agent's Memory calls. */ -const LEGACY_MEMORY_TARGET_NAME = BOX_MEMORY_ROUTE_NAME; - -/** The `agentcore` target, in `region`, as an earlier build wrote it for the Memory calls. */ -function legacyMemoryTarget(region: string): AgentCoreGatewayTarget { - return { - name: LEGACY_MEMORY_TARGET_NAME, - targetType: "passthrough", - passthrough: { endpoint: `https://${agentCoreDataPlaneHost(region)}`, protocolType: "CUSTOM" }, - outboundAuth: { type: "GATEWAY_IAM_ROLE", service: "bedrock-agentcore", region }, - }; -} - /** The Bedrock actions behind Converse and ConverseStream, which the agent's model calls make. */ const BEDROCK_INVOKE_ACTIONS = ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"]; @@ -205,59 +185,17 @@ export function addBoxBedrockTarget( return messages; } -/** - * Removes from each Box gateway what an earlier build wrote for the agent's Memory calls: the - * `agentcore` target, when it is as that build wrote it for some region, and - * {@link LEGACY_BOX_MEMORY_POLICY_FILE} in `additionalPolicies`. Without this, the gateway rule - * would keep that target as the user's and route the Memory calls through the gateway role again. - * A target of that name that the user changed stays. Changes `spec` in place, and returns the step - * messages; none when nothing changed. - */ -export function removeLegacyMemoryAccess(spec: ProjectSpec): string[] { - const messages: string[] = []; - for (const gateway of spec.agentCoreGateways.filter(isBoundGateway)) { - const legacy = gateway.targets.findIndex((target) => { - const region = target.outboundAuth?.region; - return ( - target.name === LEGACY_MEMORY_TARGET_NAME && - region !== undefined && - isDeepStrictEqual(target, legacyMemoryTarget(region)) - ); - }); - if (legacy !== -1) { - gateway.targets.splice(legacy, 1); - messages.push( - `removing target '${LEGACY_MEMORY_TARGET_NAME}' from gateway '${gateway.name}' in ` + - "agentcore.json: the boxed agent's Memory calls go to Memory directly", - ); - } - if (gateway.additionalPolicies?.includes(LEGACY_BOX_MEMORY_POLICY_FILE)) { - const kept = gateway.additionalPolicies.filter( - (file) => file !== LEGACY_BOX_MEMORY_POLICY_FILE, - ); - if (kept.length > 0) gateway.additionalPolicies = kept; - else delete gateway.additionalPolicies; - messages.push( - `removing '${LEGACY_BOX_MEMORY_POLICY_FILE}' from gateway '${gateway.name}' ` + - "additionalPolicies in agentcore.json: its role no longer calls Memory", - ); - } - } - return messages; -} - /** * Adds the Box gateway targets that the boxed agents' calls need, in `region`, before build renders * the Runtime profiles from those targets: the Bedrock model's. The agent's Memory calls take no * target: box signs them with the execution role and sends them to Memory itself, so Memory can - * hold each call to the session of the runtime (runtimeProfile/rules/additions.ts). It first - * removes the Memory target and policy that an earlier build wrote. + * hold each call to the session of the runtime (runtimeProfile/rules/additions.ts). */ export function addBoxGatewayTargets( spec: ProjectSpec, region: string = DEFAULT_EGRESS_BEDROCK_REGION, ): string[] { - return [...removeLegacyMemoryAccess(spec), ...addBoxBedrockTarget(spec, region)]; + return addBoxBedrockTarget(spec, region); } /** diff --git a/src/core/project/boxNetwork/network.test.ts b/src/core/project/boxNetwork/network.test.ts index c5c32a98e..8d92dec12 100644 --- a/src/core/project/boxNetwork/network.test.ts +++ b/src/core/project/boxNetwork/network.test.ts @@ -1059,8 +1059,6 @@ describe("warnings", () => { describe("the grant each endpoint of a target needs", () => { const BEDROCK_FILE = "agentcore/box-bedrock-policy.json"; - /** The Memory policy file that build wrote before Memory calls went to Memory directly. */ - const MEMORY_FILE = "agentcore/box-memory-policy.json"; const signed = (name: string, host: string, service: string) => passthrough(name, `https://${host}`, { outboundAuth: { type: "GATEWAY_IAM_ROLE", service, region: "us-west-2" }, @@ -1147,15 +1145,13 @@ describe("warnings", () => { expect(warningsFor(functionUrl, ["bedrock:*"])).toEqual([forService("egress-fn", "lambda")]); }); - test("an AgentCore target needs an AgentCore grant, and a Memory file of an earlier build grants none", () => { + test("an AgentCore target needs an AgentCore grant", () => { const memory = signed( "agentcore", "bedrock-agentcore.us-west-2.amazonaws.com", "bedrock-agentcore", ); - expect(warningsFor(memory, [], [MEMORY_FILE])).toEqual([ - forService("agentcore", "bedrock-agentcore"), - ]); + expect(warningsFor(memory)).toEqual([forService("agentcore", "bedrock-agentcore")]); expect(warningsFor(memory, ["bedrock-agentcore:*"])).toEqual([]); expect(warningsFor(agentcoreControl, ["bedrock-agentcore:*"])).toEqual([]); expect(warningsFor(agentcoreControl)).toEqual([ diff --git a/src/core/project/boxNetworkWriters.test.ts b/src/core/project/boxNetworkWriters.test.ts index 6b3777862..2914b875f 100644 --- a/src/core/project/boxNetworkWriters.test.ts +++ b/src/core/project/boxNetworkWriters.test.ts @@ -163,7 +163,7 @@ describe("boxNetworkEdits", () => { }); describe("gatewayTargetsWriter", () => { - test("writes no target for a host that the Runtime profile sends directly, and removes the one an earlier build wrote", () => { + test("writes no target for a host that the Runtime profile sends directly, and removes a managed one it holds", () => { const MEMORY_HOST = "bedrock-agentcore.us-west-2.amazonaws.com"; // The additions rule sends a route to the AgentCore host directly when the runtime has its // default Memory, so the route has no backend to the host's target. @@ -193,9 +193,9 @@ describe("gatewayTargetsWriter", () => { "adding gateway target 'egress-httpbin-org' to 'agent-box-egress' for httpbin.org", ]); - const earlier = direct([MINE, passthrough("egress-tools", `https://${MEMORY_HOST}`)]); - expect(targetNames(earlier.spec)).toEqual(["mine", "egress-httpbin-org"]); - expect(earlier.messages).toContain( + const held = direct([MINE, passthrough("egress-tools", `https://${MEMORY_HOST}`)]); + expect(targetNames(held.spec)).toEqual(["mine", "egress-httpbin-org"]); + expect(held.messages).toContain( "removing gateway target 'egress-tools' from 'agent-box-egress': on AgentCore Runtime the " + `box sends the calls to ${MEMORY_HOST} directly, not through the gateway`, ); @@ -342,12 +342,8 @@ describe("defaultTargetsWriter", () => { const edits = run([defaultTargetsWriter], [network()], project); // The agent's Memory calls go to Memory directly, so they take no target and no gateway policy. - // The writer removes the Memory policy file that an earlier build wrote. expect(targetNames(edits.spec)).toEqual(["mine", "bedrock"]); - expect(edits.files).toEqual([ - expect.objectContaining({ path: BOX_BEDROCK_POLICY_FILE }), - { path: "agentcore/box-memory-policy.json", remove: true }, - ]); + expect(edits.files.map(({ path }) => path)).toEqual([BOX_BEDROCK_POLICY_FILE]); }); test("writes the Bedrock policy for the region of the bedrock target it writes", () => { @@ -366,23 +362,6 @@ describe("defaultTargetsWriter", () => { ]); }); - test("keeps the Memory policy file of an earlier build while a gateway that is not a Box gateway lists it", () => { - const project = spec(); - project.agentCoreGateways.push({ - name: "tools", - authorizerType: "AWS_IAM", - targets: [], - additionalPolicies: ["agentcore/box-memory-policy.json"], - } as never); - - const edits = run([defaultTargetsWriter], [network()], project); - - expect(edits.files.map(({ path }) => path)).toEqual([BOX_BEDROCK_POLICY_FILE]); - expect( - edits.spec.agentCoreGateways.find(({ name }) => name === "tools")?.additionalPolicies, - ).toEqual(["agentcore/box-memory-policy.json"]); - }); - test("writes the model target for the region of the build", () => { const edits = run([defaultTargetsWriter], [network({ region: "eu-central-1" })]); diff --git a/src/core/project/boxNetworkWriters.ts b/src/core/project/boxNetworkWriters.ts index c033503a9..824f92de4 100644 --- a/src/core/project/boxNetworkWriters.ts +++ b/src/core/project/boxNetworkWriters.ts @@ -9,7 +9,7 @@ import { dirname, join } from "node:path"; import { isDeepStrictEqual } from "node:util"; import type { EnvLocalEntry } from "../../handlers/project/types"; import type { AgentCoreGatewayTarget } from "../../projectSchemas/gateway"; -import { isBoundGateway, type ProjectSpec } from "../../projectSchemas/project"; +import type { ProjectSpec } from "../../projectSchemas/project"; import { boxGatewayOf, gatewayTargetFor, @@ -19,12 +19,7 @@ import { type BoxNetwork, type BoxNetworkHost, } from "./boxNetwork"; -import { - addBoxGatewayTargets, - BOX_BEDROCK_POLICY_FILE, - boxBedrockPolicy, - LEGACY_BOX_MEMORY_POLICY_FILE, -} from "./boxMemory"; +import { addBoxGatewayTargets, BOX_BEDROCK_POLICY_FILE, boxBedrockPolicy } from "./boxMemory"; import type { ReadEnvFileOptions } from "../../io/envFile"; import { boxKeyVariables, @@ -352,17 +347,11 @@ function undeclaredKey( /** * The `bedrock` target that each box runtime's model calls take, and the Bedrock policy of the - * gateways' role, as `addBoxGatewayTargets` adds them. It removes the Memory policy file that an - * earlier build wrote, as `addBoxGatewayTargets` removes its listing, unless a gateway that is not - * a Box gateway still lists it. + * gateways' role, as `addBoxGatewayTargets` adds them. */ export const defaultTargetsWriter: BoxNetworkWriter = { name: "default targets", - writes: () => [ - { kind: "defaultTargets" }, - { kind: "file", path: BOX_BEDROCK_POLICY_FILE }, - { kind: "file", path: LEGACY_BOX_MEMORY_POLICY_FILE }, - ], + writes: () => [{ kind: "defaultTargets" }, { kind: "file", path: BOX_BEDROCK_POLICY_FILE }], edits: (networks, { spec }) => { // Every network of one build has the region build builds for. const region = networks[0]?.region; @@ -373,21 +362,9 @@ export const defaultTargetsWriter: BoxNetworkWriter = { path: BOX_BEDROCK_POLICY_FILE, policy: region === undefined ? undefined : boxBedrockPolicy(spec, region), }, - ] - .flatMap(({ path, policy }): BoxNetworkFile[] => - policy ? [{ path, text: `${JSON.stringify(policy, undefined, 2)}\n` }] : [], - ) - .concat( - // removeLegacyMemoryAccess drops the listing only from the Box gateways. A gateway that - // is not a Box gateway lists the file only when the user listed it there. - spec.agentCoreGateways.some( - (gateway) => - !isBoundGateway(gateway) && - gateway.additionalPolicies?.includes(LEGACY_BOX_MEMORY_POLICY_FILE), - ) - ? [] - : [{ path: LEGACY_BOX_MEMORY_POLICY_FILE, remove: true }], - ), + ].flatMap(({ path, policy }): BoxNetworkFile[] => + policy ? [{ path, text: `${JSON.stringify(policy, undefined, 2)}\n` }] : [], + ), }; }, }; diff --git a/src/core/project/manager.boxNetwork.test.ts b/src/core/project/manager.boxNetwork.test.ts index 2678c9d3b..f07ab67c8 100644 --- a/src/core/project/manager.boxNetwork.test.ts +++ b/src/core/project/manager.boxNetwork.test.ts @@ -532,58 +532,7 @@ describe("build writes the Box network of the box profile", () => { expect(await text(root, SPEC)).toBe(spec); }); - test("removes the Memory target, policy listing, and policy file of an earlier build, and sends Memory directly", async () => { - const { manager, root } = await strandsProject(); - const MEMORY_FILE = "agentcore/box-memory-policy.json"; - // What a build before Memory went direct wrote for the agent's Memory calls. - await editGateway(root, (gateway) => { - gateway.targets.push({ - name: "agentcore", - targetType: "passthrough", - passthrough: { - endpoint: "https://bedrock-agentcore.us-west-2.amazonaws.com", - protocolType: "CUSTOM", - }, - outboundAuth: { - type: "GATEWAY_IAM_ROLE", - service: "bedrock-agentcore", - region: "us-west-2", - }, - }); - (gateway as { additionalPolicies?: string[] }).additionalPolicies = [MEMORY_FILE]; - }); - await writeFile(path(root, MEMORY_FILE), '{ "Version": "2012-10-17", "Statement": [] }\n'); - - const { steps } = await build(manager, root); - - expect(await targetNames(root)).toEqual(["bedrock"]); - expect((await boxGateway(root)).additionalPolicies).toEqual([ - "agentcore/box-bedrock-policy.json", - ]); - expect(existsSync(path(root, MEMORY_FILE))).toBe(false); - expect(steps).toEqual( - expect.arrayContaining([ - `removing target 'agentcore' from gateway '${GATEWAY}' in agentcore.json: the boxed ` + - "agent's Memory calls go to Memory directly", - `removing '${MEMORY_FILE}' from gateway '${GATEWAY}' additionalPolicies in ` + - "agentcore.json: its role no longer calls Memory", - `removing ${MEMORY_FILE}`, - ]), - ); - const egress = (await runtimeProfile(root)).egress as Record; - expect(egress.agentcore).toEqual({ - destinations: ["bedrock-agentcore.us-west-2.amazonaws.com/memories"], - secret: { ref: "aws://" }, - }); - - // A second build finds nothing of the earlier build, and changes nothing. - const spec = await text(root, SPEC); - const again = await build(manager, root); - expect(again.steps.filter((step) => step.startsWith("removing"))).toEqual([]); - expect(await text(root, SPEC)).toBe(spec); - }); - - test("keeps an agentcore target that the user changed, and stops until the user renames it", async () => { + test("stops for a user target named agentcore, the name of the Memory route, and keeps it", async () => { const { manager, root, box } = await strandsProject(); const mine = { name: "agentcore", @@ -599,7 +548,7 @@ describe("build writes the Box network of the box profile", () => { (caught: unknown) => caught, ); - // Build does not remove a target it did not write, and its route would take the Memory route's name. + // Build does not remove a target of the user, and its route would take the Memory route's name. expect(error).toBeInstanceOf(InputValidationError); expect((error as Error).message).toContain( `agentcore/agentcore.json [egress.agentcore] is the name of the route that the agent's ` + From 48d495e83b49250c17e8ff30bbae7c036c69e201 Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Mon, 23 Nov 2026 13:39:32 -0800 Subject: [PATCH 06/11] feat(box): Keep the box of a runtime with its default Memory to /memories on the AgentCore host On AgentCore Runtime the box of a runtime with its default Memory now reaches the AgentCore data-plane host only for Memory. Code Interpreter, Browser, identity, and API key calls on that host are blocked inside the box. - Build writes agentcore-memory-paths.dw beside the Runtime profile and lists it last in `policies`. Its first rule denies each request to the host whose path is not /memories or under it. Its second denies a path that could leave /memories once AWS normalizes it: a `..` or `.` segment, an encoded dot, or an encoded backslash. No Memory, actor, session, or record ID holds a dot or a backslash. - A route or HTTP MCP server of the profile to the AgentCore host, and a permit that names it, now stop build with RUNTIME_AGENTCORE_HOST_ROUTED. Box refuses a gateway route for a host that has another route, and a route of its own would reach the other APIs of the host. The problem names the entry or the permit of the local profile. Build no longer sends such a route to the host directly. - A user policy file of the same name stops build. The profile header names the policy file only when the profile lists it. --- README.md | 9 +- src/core/project/boxNetworkWriters.ts | 14 + src/core/project/manager.boxNetwork.test.ts | 39 ++- .../runtimeProfile/rules/additions.test.ts | 243 ++++++++++++------ .../project/runtimeProfile/rules/additions.ts | 193 +++++++++----- src/core/project/runtimeProfile/types.ts | 2 + 6 files changed, 341 insertions(+), 159 deletions(-) diff --git a/README.md b/README.md index d291341cb..dc1ee5773 100644 --- a/README.md +++ b/README.md @@ -264,9 +264,12 @@ A boxed agent gets the deployed Memory IDs and the Runtime's region inside the b the `AGENTCORE_GATEWAY__URL` variables of gateways you add yourself: its calls go out through its Box gateway. Its calls to its default Memory are the exception. Box signs them with the execution role and sends them to Memory directly, so Memory can hold each call to the session of -the runtime. Box signs only the `/memories` path of the AgentCore host on that route. A -`PRIVATE_VPC` runtime reaches Memory through the AgentCore endpoint of its private VPC. A runtime -in a VPC of your own needs a `bedrock-agentcore` VPC endpoint for these calls. +the runtime. The box reaches the AgentCore host only at `/memories`: build adds a box policy, +`agentcore-memory-paths.dw`, that blocks every other path of that host, such as Code Interpreter, +Browser, identity, and API key calls. So a runtime with its default Memory cannot also route the +AgentCore host in its box profile, and build stops when it does. A `PRIVATE_VPC` runtime reaches +Memory through the AgentCore endpoint of its private VPC. A runtime in a VPC of your own needs a +`bedrock-agentcore` VPC endpoint for these calls. A Box project that an older CLI created still has `start.sh`, or a Dockerfile that runs it. Build and deploy now render profiles that those files cannot start. Create the project again, or copy diff --git a/src/core/project/boxNetworkWriters.ts b/src/core/project/boxNetworkWriters.ts index 824f92de4..161ec17eb 100644 --- a/src/core/project/boxNetworkWriters.ts +++ b/src/core/project/boxNetworkWriters.ts @@ -43,6 +43,7 @@ import { } from "./templates/boxProfile"; import { isTable } from "./runtimeProfile/projectPaths"; import { serializeRuntimeProfile } from "./runtimeProfile/serialize"; +import { MEMORY_POLICY_FILE, memoryHostPolicy } from "./runtimeProfile/rules/additions"; import { RUNTIME_INPUTS, type TomlTable } from "./runtimeProfile/types"; import { ProjectStateError } from "../../errors/errors"; @@ -455,6 +456,9 @@ export function runtimeProfileWriter(staged: readonly StagedRuntime[]): BoxNetwo kind: "file" as const, path: `${dist}/${file}`, })), + ...(listsMemoryPolicy(networkOf(networks, runtime)) + ? [{ kind: "file" as const, path: `${dist}/${MEMORY_POLICY_FILE}` }] + : []), ]), edits: (networks, { spec }) => ({ files: staged.flatMap(({ runtime, dist, kind }) => { @@ -475,12 +479,22 @@ export function runtimeProfileWriter(staged: readonly StagedRuntime[]): BoxNetwo path: `${dist}/${file}`, copyOf: `${boxLocalDir(runtime)}/${file}`, })), + // The box of a runtime with its default Memory reaches the AgentCore host only for Memory. + ...(network && listsMemoryPolicy(network) + ? [{ path: `${dist}/${MEMORY_POLICY_FILE}`, text: memoryHostPolicy(network.region) }] + : []), ]; }), }), }; } +/** Whether the Runtime profile of `network` lists the policy file of the Memory host. */ +function listsMemoryPolicy(network: BoxNetwork | undefined): boolean { + const policies = network?.runtimeProfile?.policies; + return Array.isArray(policies) && policies.includes(MEMORY_POLICY_FILE); +} + /** * The text of the AgentCore Runtime profile of `runtime`, which build derived from its local box * profile into the Box network model (`BoxNetwork.runtimeProfile`). diff --git a/src/core/project/manager.boxNetwork.test.ts b/src/core/project/manager.boxNetwork.test.ts index f07ab67c8..49da1837d 100644 --- a/src/core/project/manager.boxNetwork.test.ts +++ b/src/core/project/manager.boxNetwork.test.ts @@ -12,6 +12,7 @@ import { createSilentLogger, matchGolden, TestIdentityClient } from "../../testi import type { DeployBackendInput, ProjectBackend } from "./backends/types"; import type { BoxNetwork } from "./boxNetwork"; import { boxShowOf, FakeBoxProfileResolver } from "./boxProfileResolver-test-support"; +import { MEMORY_POLICY_FILE, memoryHostPolicy } from "./runtimeProfile/rules/additions"; import type { ResolvedBoxProfile } from "./boxProfileResolver"; import { seedBoxCache } from "./backends/box-release-test-support"; import { boxGatewayLogsNotice, FsProjectManager } from "./manager"; @@ -289,8 +290,12 @@ describe("build writes the Box network of the box profile", () => { // The Runtime profile routes each target of the Box gateway, the model's included. const profile = await runtimeProfile(root); - expect(profile.policies).toEqual(["policy.dw"]); + // The runtime has its default Memory, so the box reaches the AgentCore host only for Memory. + expect(profile.policies).toEqual(["policy.dw", MEMORY_POLICY_FILE]); expect(await text(root, "app/agent_python_strands/.box-dist/policy.dw")).toBe(POLICY); + expect(await text(root, `app/agent_python_strands/.box-dist/${MEMORY_POLICY_FILE}`)).toBe( + memoryHostPolicy("us-west-2"), + ); const egress = profile.egress as Record; // The route of [egress.github] keeps the name of its entry, and goes to its target. expect(Object.keys(egress).sort()).toEqual([ @@ -501,34 +506,28 @@ describe("build writes the Box network of the box profile", () => { ); }); - test("sends an entry of the profile for the Memory host directly on the Runtime, with its name, and warns", async () => { + test("stops for an entry of the profile to the AgentCore host, as the box reaches it only for Memory", async () => { const { manager, root, box } = await strandsProject(); const host = "bedrock-agentcore.us-west-2.amazonaws.com"; await editProfile(root, { tables: `\n[egress.tools]\ndestinations = ["${host}"]\nsecret.ref = "aws://default"\n`, }); box.answer = shows({ tools: { destinations: [host], secret: { ref: "aws://default" } } }); + const spec = await text(root, SPEC); - const { warnings } = await build(manager, root); - - // `agentcore dev` keeps the entry, for Code Interpreter or Browser. On the Runtime it goes to - // the host directly, signed with the execution role, so Memory can hold each call to the - // session; box refuses the Memory route beside it. - const egress = (await runtimeProfile(root)).egress as Record; - expect(Object.keys(egress)).toEqual(["bedrock", "tools"]); - expect(egress.tools).toEqual({ destinations: [host], secret: { ref: "aws://" } }); - expect(warnings).toContainEqual( - expect.stringContaining(`[egress.tools] goes to ${host} directly on AgentCore Runtime`), + const error = await build(manager, root).then( + () => undefined, + (caught: unknown) => caught, ); - // The Runtime does not use a gateway target for the host, so build writes none, and gives no - // warning about the gateway role's grants for one. - expect(await targetNames(root)).toEqual(["bedrock"]); - expect(warnings).not.toContainEqual(expect.stringContaining("Target 'egress-")); - // A second build changes nothing. - const spec = await text(root, SPEC); - const again = await build(manager, root); - expect(again.steps.filter((step) => step.includes("gateway target"))).toEqual([]); + // Code Interpreter and Browser are on that host. On the Runtime the box reaches it only at + // /memories, directly, and blocks every other path, so build stops and writes nothing. + expect(error).toBeInstanceOf(InputValidationError); + expect((error as Error).message).toContain( + `[egress.tools] reaches ${host}. On AgentCore Runtime, a runtime with its default Memory ` + + `reaches that host only for Memory, at ${host}/memories, and box blocks every other path ` + + "of it, such as Code Interpreter and Browser.", + ); expect(await text(root, SPEC)).toBe(spec); }); diff --git a/src/core/project/runtimeProfile/rules/additions.test.ts b/src/core/project/runtimeProfile/rules/additions.test.ts index 5b5a0fcf6..ab108f8fc 100644 --- a/src/core/project/runtimeProfile/rules/additions.test.ts +++ b/src/core/project/runtimeProfile/rules/additions.test.ts @@ -18,13 +18,16 @@ import { deriveRuntimeProfile } from "../derive"; import { serializeRuntimeProfile } from "../serialize"; import type { TomlTable } from "../types"; import { defaultMemoryName } from "../../../../projectSchemas/memory"; +import { parsePolicy } from "../../boxNetwork/dogwood"; import { agentStartsUnderOtel, apply, DEFAULT_TELEMETRY_TARGET, DEFAULT_TRACES_TARGET, defaultTracesTelemetry, + MEMORY_POLICY_FILE, MEMORY_ROUTE, + memoryHostPolicy, runtimeProfileHeader, } from "./additions"; @@ -33,6 +36,21 @@ const PROFILE_FILE = ".box/agent/local/profile.toml"; const CREATE_REGION = "us-east-1"; const BUILD_REGION = "us-west-2"; +/** + * The Runtime form of `recorded`, a profile the old renderer wrote for a runtime with its default + * Memory: the Memory route goes to Memory directly, under the same name, in the same place, and the + * policy file that keeps the box to `/memories` on the AgentCore host comes last in `policies`. + */ +function withDirectMemory(recorded: TomlTable): TomlTable { + const result = structuredClone(recorded); + if (isTableValue(result.egress)) result.egress[MEMORY_ROUTE] = DIRECT_MEMORY_ROUTE; + result.policies = [...((recorded.policies as string[] | undefined) ?? []), MEMORY_POLICY_FILE]; + return result; +} + +const isTableValue = (value: unknown): value is TomlTable => + typeof value === "object" && value !== null && !Array.isArray(value); + /** The route of the agent's Memory calls: to Memory directly, signed with the execution role. */ const DIRECT_MEMORY_ROUTE = { destinations: [`bedrock-agentcore.${BUILD_REGION}.amazonaws.com/memories`], @@ -266,9 +284,10 @@ describe("deriveRuntimeProfile on the created templates", () => { contextOf(runtime, { memories, telemetry }), ); - const recorded = OLD_RENDERER.templates[ + const old = OLD_RENDERER.templates[ `${build} ${protocol} ${provider} memories=${memories.length} telemetry=${telemetryCase}` ] as TomlTable; + const recorded = memories.length > 0 ? withDirectMemory(old) : old; const expected = otel ? withOtel(recorded, build) : recorded; expect(problems).toEqual([]); for (const key of OWNED) @@ -342,14 +361,12 @@ describe("deriveRuntimeProfile on each box template of create", () => { }), ); - // The old renderer sent the Memory calls through the gateway's `agentcore` target. They now - // go to Memory directly, under the same route name, in the same place. - const recorded = structuredClone( + // The old renderer sent the Memory calls through the gateway's `agentcore` target. + const old = OLD_RENDERER.boxTemplates[ `${template} provider=${provider} memories=${memories.length} telemetry=${telemetryCase}` - ]!, - ); - if (memories.length > 0) (recorded.egress as TomlTable)[MEMORY_ROUTE] = DIRECT_MEMORY_ROUTE; + ]!; + const recorded = memories.length > 0 ? withDirectMemory(old) : old; expect(problems).toEqual([]); expect(profile).toEqual(otel ? withOtel(recorded, input.build as Build) : recorded); }, @@ -978,6 +995,66 @@ describe("the additions rule", () => { }); }); + describe("the Memory host policy", () => { + const HOST = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; + + test("parses, and names its two rules", () => { + const parsed = parsePolicy(memoryHostPolicy(BUILD_REGION)); + + expect(parsed.errors).toEqual([]); + expect(parsed.statements.map(({ id, effect }) => [id, effect])).toEqual([ + ["agentcore_host_memory_only", "forbid"], + ["agentcore_host_no_dot_segments", "forbid"], + ]); + }); + + test("names the AgentCore host of the region, and in its partition", () => { + expect(memoryHostPolicy(BUILD_REGION)).toContain(`context.input.host == "${HOST}"`); + expect(memoryHostPolicy("cn-north-1")).toContain( + 'context.input.host == "bedrock-agentcore.cn-north-1.amazonaws.com.cn"', + ); + }); + + test("allows /memories and the paths under it, and blocks each other path and each dot segment", () => { + const patterns = parsePolicy(memoryHostPolicy(BUILD_REGION)).statements.flatMap(({ text }) => + [...text.matchAll(/like "([^"]+)"/g)].map(([, p]) => p!), + ); + const like = (pattern: string, path: string) => + new RegExp( + `^${pattern + .split("*") + .map((part) => part.replace(/[.?+^$|()[\]{}\\]/g, "\\$&")) + .join(".*")}$`, + ).test(path); + const blocked = (path: string) => + !(path === "/memories" || like("/memories/*", path)) || + patterns.slice(1).some((pattern) => like(pattern, path)); + + for (const path of [ + "/memories", + "/memories/mem-abc1234567/events", + "/memories/mem-abc1234567/actor/user%2F1%3Aa/sessions/s-1", + "/memories/mem-abc1234567/retrieve", + ]) { + expect([path, blocked(path)]).toEqual([path, false]); + } + for (const path of [ + "/code-interpreters/aws.codeinterpreter.v1/tools/invoke", + "/browsers/aws.browser.v1/sessions/start", + "/identities/api-key", + "/memoriesX", + "/memories/../identities/api-key", + "/memories/x/./y", + "/memories/x/..", + "/memories/%2e%2e/identities", + "/memories/%2E%2E/identities", + "/memories/..%5cidentities", + ]) { + expect([path, blocked(path)]).toEqual([path, true]); + } + }); + }); + describe("the Memory route", () => { const MEMORY = defaultMemoryName("agent"); @@ -1017,98 +1094,111 @@ describe("the additions rule", () => { expect(other.table.egress).not.toHaveProperty(MEMORY_ROUTE); }); - test("adds no second route to the Memory host when a direct route or HTTP MCP server of the profile has it", () => { - const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; + test("lists the policy file that keeps the box to /memories on the AgentCore host last in policies", () => { const context = contextOf(CODEZIP, { memories: [MEMORY] }); - const signed = { secret: { ref: "aws://" } }; - const routed = { egress: { memory: { destinations: [host], ...signed } } }; - const served = { mcp: { memory: { type: "http", destinations: [host], ...signed } } }; - expect(apply(routed, context)).toMatchObject({ - table: { egress: routed.egress }, - warnings: [], - }); - const mcp = apply(served, context); - expect(mcp.table.egress).toBeUndefined(); - expect(mcp.table.mcp).toEqual(served.mcp); - expect(mcp.warnings).toEqual([]); + expect(apply({}, context).table.policies).toEqual([MEMORY_POLICY_FILE]); + expect(apply({ policies: ["policy.dw"] }, context).table.policies).toEqual([ + "policy.dw", + MEMORY_POLICY_FILE, + ]); + expect(apply({ policies: ["policy.dw"] }, contextOf(CODEZIP)).table.policies).toEqual([ + "policy.dw", + ]); }); - test("warns for a direct route to the Memory host that box does not sign with the execution role", () => { + test("stops build for a route or HTTP MCP server of the profile to the AgentCore host, through the gateway or not", () => { const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; - const local = { egress: { memory: { destinations: [host], secret: { ref: "env://KEY" } } } }; + const backends = [{ url: "${{ inputs.gateway.url }}/egress-tools" }]; + const local = { + egress: { + tools: { destinations: [host], backends }, + direct: { destinations: [host], secret: { ref: "aws://" } }, + }, + mcp: { recall: { type: "http", destinations: [host], backends } }, + }; - const { table, warnings } = apply(local, contextOf(CODEZIP, { memories: [MEMORY] })); + const { problems } = apply(local, contextOf(CODEZIP, { memories: [MEMORY] })); - expect(table.egress).toEqual(local.egress); - expect(warnings).toEqual([ - `[egress.memory] goes to ${host} directly on AgentCore Runtime, and box does not sign it ` + - "with the execution role (aws://), so the agent's Memory calls fail. Sign it with " + - `aws://, or remove it: build then adds [egress.${MEMORY_ROUTE}].`, + expect(problems.map(({ code, key }) => [code, key])).toEqual([ + ["RUNTIME_AGENTCORE_HOST_ROUTED", ["egress", "tools"]], + ["RUNTIME_AGENTCORE_HOST_ROUTED", ["egress", "direct"]], + ["RUNTIME_AGENTCORE_HOST_ROUTED", ["mcp", "recall"]], ]); + expect(problems[0]!.reason).toBe( + `[egress.tools] reaches ${host}. On AgentCore Runtime, a runtime with its default Memory ` + + `reaches that host only for Memory, at ${host}/memories, and box blocks every other ` + + "path of it, such as Code Interpreter and Browser. Remove [egress.tools], or remove the " + + "default Memory of runtime 'agent' from agentcore.json.", + ); + }); + + test("names the entry and the permit of the local profile that name the AgentCore host", () => { + const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; + const context = { + ...contextOf(CODEZIP, { memories: [MEMORY] }), + hosts: [ + { + host, + authority: host, + target: "egress-bedrock-agentcore", + sources: [ + { kind: "entry", egressEntry: "tools", destinations: [host] }, + { kind: "rule", ruleId: "ci_calls" }, + ], + }, + ], + } as unknown as Parameters[1]; + // The gateway rule names the route of a permit-only host after its target. + const local = { + egress: { + "egress-bedrock-agentcore": { destinations: [host], backends: [{ url: "x" }] }, + }, + }; + + const labels = apply(local, context).problems.map( + ({ reason }) => reason.split(" reaches")[0], + ); + + expect(labels).toEqual(["[egress.tools]", 'The permit "ci_calls"']); }); - test("counts a direct route to the Memory host with :443, in another case, or as a wildcard", () => { + test("counts a route to the AgentCore host with :443, in another case, or as a wildcard", () => { const context = contextOf(CODEZIP, { memories: [MEMORY] }); for (const destination of [ `bedrock-agentcore.${BUILD_REGION}.amazonaws.com:443`, `Bedrock-AgentCore.${BUILD_REGION}.amazonaws.com`, "*.amazonaws.com", ]) { - const local = { - egress: { memory: { destinations: [destination], secret: { ref: "aws://" } } }, - }; - const { table, problems } = apply(local, context); + const local = { egress: { tools: { destinations: [destination] } } }; - expect([destination, problems]).toEqual([destination, []]); - expect(table.egress).toEqual(local.egress); + expect([destination, apply(local, context).problems.map(({ code }) => code)]).toEqual([ + destination, + ["RUNTIME_AGENTCORE_HOST_ROUTED"], + ]); } - // Another port, or a wildcard of another domain, is not the Memory host. + // Another port, or a wildcard of another domain, is not the AgentCore host. const other = { egress: { - memory: { - destinations: ["*.example.com", "bedrock-agentcore.us-west-2.amazonaws.com:8443"], + tools: { + destinations: ["*.example.com", `bedrock-agentcore.${BUILD_REGION}.amazonaws.com:8443`], }, }, }; - expect(Object.keys(apply(other, context).table.egress as TomlTable)).toEqual([ - "memory", - MEMORY_ROUTE, - ]); + const { table, problems } = apply(other, context); + expect(problems).toEqual([]); + expect(Object.keys(table.egress as TomlTable)).toEqual(["tools", MEMORY_ROUTE]); }); - test("sends a route of the profile to the Memory host directly, not through the Box gateway, and warns", () => { - const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; - const backends = [{ url: "${{ inputs.gateway.url }}/egress-tools" }]; - const local = { - egress: { tools: { destinations: [host], backends, when: "true" } }, - mcp: { recall: { type: "http", destinations: [host], backends } }, - }; - - const { table, problems, warnings } = apply( - local, + test("stops build for a policy file of the user under the name of the Memory policy file", () => { + const { problems } = apply( + { policies: [`rules/${MEMORY_POLICY_FILE}`] }, contextOf(CODEZIP, { memories: [MEMORY] }), ); - // Each keeps its name and other keys. Box refuses the Memory route beside a signed route that - // covers its path, so build adds none. - expect(problems).toEqual([]); - expect(table.egress).toEqual({ - tools: { destinations: [host], when: "true", secret: { ref: "aws://" } }, - }); - expect(table.mcp).toEqual({ - recall: { type: "http", destinations: [host], secret: { ref: "aws://" } }, - }); - expect(warnings).toEqual( - ["[egress.tools]", "[mcp.recall]"].map( - (label) => - `${label} goes to ${host} directly on AgentCore Runtime, signed with the execution ` + - "role, and not through the Box gateway, so Memory can hold each call to the session of " + - "the runtime. Box signs every API of that host on this route, not only Memory. Remove " + - `${label} if the agent calls only Memory there: build then adds [egress.${MEMORY_ROUTE}], ` + - "which box signs only for Memory.", - ), - ); + expect(problems.map(({ code, key }) => [code, key])).toEqual([ + ["RUNTIME_ROUTE_NAME_TAKEN", ["policies"]], + ]); }); test("leaves a gateway route to the Memory host alone for a runtime with no default Memory", () => { @@ -1145,9 +1235,8 @@ describe("the additions rule", () => { test("stops build for a route of the user under the name of the Memory route", () => { const local = { egress: { [MEMORY_ROUTE]: { destinations: ["api.example.com"] } } }; - const { table, problems } = apply(local, contextOf(CODEZIP, { memories: [MEMORY] })); + const { problems } = apply(local, contextOf(CODEZIP, { memories: [MEMORY] })); - expect(table.egress).toEqual(local.egress); expect(problems).toEqual([ { code: "RUNTIME_ROUTE_NAME_TAKEN", @@ -1395,7 +1484,13 @@ describe("the additions rule", () => { expect(derived).toContain("@box/agentcore and @box/agentcore-codezip packs"); expect(runtimeProfileHeader(contextOf(CONTAINER))[1]).not.toContain("@box/agentcore-codezip"); expect(standIns).toContain("AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are stand-ins"); - expect(standIns).toContain(`except [egress.${MEMORY_ROUTE}], which goes to AgentCore Memory`); + expect(standIns).toEndWith("Every AWS route goes to the Box gateway."); + const context = contextOf(CODEZIP, { memories: [defaultMemoryName("agent")] }); + const withMemory = runtimeProfileHeader(context, apply(createdProfile(CODEZIP), context).table); + expect(withMemory[2]).toEndWith( + `except [egress.${MEMORY_ROUTE}], which goes to AgentCore Memory directly. ` + + `${MEMORY_POLICY_FILE} keeps the box to /memories on the AgentCore host.`, + ); const text = serializeRuntimeProfile( apply(createdProfile(CODEZIP), contextOf(CODEZIP)).table, runtimeProfileHeader(contextOf(CODEZIP)), diff --git a/src/core/project/runtimeProfile/rules/additions.ts b/src/core/project/runtimeProfile/rules/additions.ts index fbf79d623..070cb1957 100644 --- a/src/core/project/runtimeProfile/rules/additions.ts +++ b/src/core/project/runtimeProfile/rules/additions.ts @@ -15,8 +15,9 @@ * | `[telemetry.]` | each CloudWatch target that box takes is signed with a bare `aws://`, in the deploy region when it names none; one that box refuses goes; a `file` target goes, with a warning | * | `[telemetry.agentcore]` | added when no target is `cloudwatch_logs`: the runtime's own log group and log stream | * | `[telemetry.agentcore_traces]` | added with OTEL on when no target is `cloudwatch_traces`: X-Ray in the Runtime's region | - * | `[egress.agentcore]` | added when the runtime has its default Memory and no route of the profile has the AgentCore host: the host's `/memories` path, which box signs with a bare `aws://` and sends to Memory directly, not through the Box gateway | - * | a route to the AgentCore host | with a default Memory, one through the Box gateway goes to the host directly, signed with a bare `aws://`, with a warning; one that is direct and not signed so gets a warning | + * | `[egress.agentcore]` | added when the runtime has its default Memory: the AgentCore host's `/memories` path, which box signs with a bare `aws://` and sends to Memory directly, not through the Box gateway | + * | `policies` | with a default Memory, {@link MEMORY_POLICY_FILE} comes last: box blocks every other path of the AgentCore host | + * | a route to the AgentCore host | with a default Memory, a problem: that host is for Memory only | * * It moves no path: the paths rule runs after it and moves each path of the user. */ @@ -61,6 +62,40 @@ export const MEMORY_ROUTE = BOX_MEMORY_ROUTE_NAME; */ export const MEMORY_PATH = "/memories"; +/** + * The policy file that build writes beside the Runtime profile of a runtime with its default + * Memory, and lists last in its `policies`: {@link memoryHostPolicy}. + */ +export const MEMORY_POLICY_FILE = "agentcore-memory-paths.dw"; + +/** + * The box policy of {@link MEMORY_POLICY_FILE}, for the AgentCore host of `region`. On AgentCore + * Runtime the box reaches that host only for Memory: the first rule blocks each request to it + * whose path is not `/memories` or under it, such as a Code Interpreter, Browser, identity, or API + * key call. The second blocks a path that could leave `/memories` once AWS normalizes it: a `..` + * or `.` segment, an encoded dot, or an encoded backslash. No Memory, actor, session, or record ID + * holds a dot or a backslash, so no Memory call has one. The path holds no query string. + */ +export function memoryHostPolicy(region: string): string { + const host = JSON.stringify(agentCoreDataPlaneHost(region)); + const anyOf = (patterns: readonly string[]) => + patterns + .map((pattern) => `context.input.path like ${JSON.stringify(pattern)}`) + .join(" ||\n "); + return [ + "// Written by agentcore build and deploy. On AgentCore Runtime the box reaches the AgentCore", + "// host only for the agent's Memory calls.", + '@id("agentcore_host_memory_only")', + 'forbid (principal, action == Box::Action::"http:request", resource)', + `when { context.input.host == ${host} && !(context.input.path == ${JSON.stringify(MEMORY_PATH)} || context.input.path like ${JSON.stringify(`${MEMORY_PATH}/*`)}) };`, + "", + '@id("agentcore_host_no_dot_segments")', + 'forbid (principal, action == Box::Action::"http:request", resource)', + `when { context.input.host == ${host} && (\n ${anyOf(["*..*", "*/./*", "*/.", "*%2e*", "*%2E*", "*%5c*", "*%5C*"])}) };`, + "", + ].join("\n"); +} + /** The variables of the region of the agent, which get the region of the Runtime. */ const REGION_ENV_NAMES: ReadonlySet = new Set(["AWS_REGION", "AWS_DEFAULT_REGION"]); @@ -167,8 +202,11 @@ export function runtimeProfileHeader( `Derived by agentcore build and deploy from ${context.profileFile}, for AgentCore Runtime. ${packs}`, `${Object.keys(RUNTIME_AWS_STAND_INS).join(" and ")} are stand-ins so boto3 in the box can ` + "build its requests. They authenticate nothing: box signs each request with the execution " + - "role. Every AWS route goes to the Box gateway, except " + - `[egress.${MEMORY_ROUTE}], which goes to AgentCore Memory directly.`, + "role. Every AWS route goes to the Box gateway" + + (Array.isArray(profile.policies) && profile.policies.includes(MEMORY_POLICY_FILE) + ? `, except [egress.${MEMORY_ROUTE}], which goes to AgentCore Memory directly. ` + + `${MEMORY_POLICY_FILE} keeps the box to ${MEMORY_PATH} on the AgentCore host.` + : "."), ...Object.entries(tableOf(profile.telemetry)) .filter(([, target]) => sameValue(target, DEFAULT_TELEMETRY_TARGET)) .map( @@ -232,74 +270,72 @@ export function apply(table: Readonly, context: RuntimeProfileContext } result.telemetry = runtimeTelemetry(result.telemetry, context, warnings); const problems: RuntimeProfileProblem[] = []; - const routes = memoryRoutes(result.egress, result.mcp, context, problems, warnings); + const routes = memoryRoutes(result, context, problems); if (routes.egress !== undefined) result.egress = routes.egress; - if (routes.mcp !== undefined) result.mcp = routes.mcp; + if (routes.policies !== undefined) result.policies = routes.policies; return { table: result, problems, warnings }; } /** - * `[egress]` and `[mcp]` for the agent's Memory calls when the runtime has its default Memory. They - * go to Memory directly, signed with the execution role, not through the Box gateway, whose role - * would sign them and keep Memory from holding each call to the session of the runtime. + * `[egress]` and `policies` for the agent's Memory calls when the runtime has its default Memory. + * They go to Memory directly, signed with the execution role, not through the Box gateway, whose + * role would sign them and keep Memory from holding each call to the session of the runtime. * - * - With no route of the profile to the AgentCore host, build adds {@link MEMORY_ROUTE}: the - * `/memories` path of that host, signed with a bare `aws://`. Box signs only the calls under that - * path. A call to another API of the host, such as an identity or API key call, goes unsigned, - * and AgentCore refuses it. - * - A route or HTTP MCP server of the profile that the gateway rule sends to that host through the - * gateway goes to the host directly, signed with a bare `aws://`, with its name and other keys. - * Box refuses the Memory route beside a signed route that covers its path. So build adds none, - * and warns that the route signs every API of the host with the execution role. - * - A direct route of the profile to that host stays. Build adds none beside it, and warns when - * box does not sign it with `aws://`, as the Memory calls then fail. - * - A route of the user named {@link MEMORY_ROUTE} for another host is a problem. + * - Build adds {@link MEMORY_ROUTE}: the `/memories` path of the AgentCore host, signed with a bare + * `aws://`. Box signs only the calls under that path. + * - Build lists {@link MEMORY_POLICY_FILE} last in `policies`, so box blocks every other path of + * the host, for Code Interpreter, Browser, identity, and API keys among them. + * - A route or HTTP MCP server of the profile to the AgentCore host is a problem. Box refuses a + * gateway route for a host that has another route, and a route of its own would reach the other + * APIs of that host. + * - A route of the user named {@link MEMORY_ROUTE}, and a policy file of the user named + * {@link MEMORY_POLICY_FILE}, are problems. */ function memoryRoutes( - egress: unknown, - mcp: unknown, + table: Readonly, context: RuntimeProfileContext, problems: RuntimeProfileProblem[], - warnings: string[], -): { egress: unknown; mcp: unknown } { - if (!hasDefaultMemory(context.spec, context.runtime)) return { egress, mcp }; - // box refuses `[egress]` or `[mcp]` that is not a table before build gets the profile. - if ((egress !== undefined && !isTable(egress)) || (mcp !== undefined && !isTable(mcp))) { - return { egress, mcp }; +): Pick { + const { egress, mcp, policies } = table; + if (!hasDefaultMemory(context.spec, context.runtime)) return { egress, policies }; + // box refuses these keys in another form before build gets the profile. + if ( + (egress !== undefined && !isTable(egress)) || + (mcp !== undefined && !isTable(mcp)) || + (policies !== undefined && !Array.isArray(policies)) + ) { + return { egress, policies }; } const host = agentCoreDataPlaneHost(context.region); - const tables = { egress: { ...egress }, mcp: { ...mcp } } as Record<"egress" | "mcp", TomlTable>; - let routed = false; - for (const kind of ["egress", "mcp"] as const) { - for (const [name, entry] of Object.entries(tables[kind])) { - if (!isTable(entry) || !hasDestination(entry.destinations, host)) continue; - routed = true; - const label = `[${kind}.${name}]`; - if (entry.backends !== undefined) { - const { backends: _backends, secret: _secret, ...kept } = entry; - tables[kind][name] = { ...kept, secret: { ref: RUNTIME_AWS_SIGNER } }; - warnings.push( - `${label} goes to ${host} directly on AgentCore Runtime, signed with the execution ` + - "role, and not through the Box gateway, so Memory can hold each call to the session " + - "of the runtime. Box signs every API of that host on this route, not only Memory. " + - `Remove ${label} if the agent calls only Memory there: build then adds ` + - `[egress.${MEMORY_ROUTE}], which box signs only for Memory.`, - ); - } else if (!isTable(entry.secret) || entry.secret.ref !== RUNTIME_AWS_SIGNER) { - warnings.push( - `${label} goes to ${host} directly on AgentCore Runtime, and box does not sign it with ` + - `the execution role (${RUNTIME_AWS_SIGNER}), so the agent's Memory calls fail. Sign it ` + - `with ${RUNTIME_AWS_SIGNER}, or remove it: build then adds [egress.${MEMORY_ROUTE}].`, - ); - } - } + const routed = [ + ...Object.entries(egress ?? {}).map(([name, entry]) => ({ key: ["egress", name], entry })), + ...Object.entries(mcp ?? {}).map(([name, entry]) => ({ key: ["mcp", name], entry })), + ].filter(({ entry }) => isTable(entry) && hasDestination(entry.destinations, host)); + for (const named of namesOfHost(host, routed, context)) { + problems.push({ + code: "RUNTIME_AGENTCORE_HOST_ROUTED", + key: named.key, + place: named.place, + reason: + `${named.label} reaches ${host}. On AgentCore Runtime, a runtime with its default Memory ` + + `reaches that host only for Memory, at ${host}${MEMORY_PATH}, and box blocks every other ` + + "path of it, such as Code Interpreter and Browser. Remove " + + `${named.label}, or remove the default Memory of runtime '${context.runtime.name}' from ` + + "agentcore.json.", + }); } - const result = { - egress: Object.keys(tables.egress).length > 0 ? tables.egress : egress, - mcp: Object.keys(tables.mcp).length > 0 ? tables.mcp : mcp, - }; - if (routed) return result; - if (Object.hasOwn(tables.egress, MEMORY_ROUTE)) { + const userPolicies = (policies ?? []) as unknown[]; + if (userPolicies.some((file) => typeof file === "string" && file.endsWith(MEMORY_POLICY_FILE))) { + problems.push({ + code: "RUNTIME_ROUTE_NAME_TAKEN", + key: ["policies"], + place: context.placeOf(["policies"]), + reason: + `policies lists ${MEMORY_POLICY_FILE}, the name of the policy file that build writes for ` + + "the agent's Memory calls on AgentCore Runtime. Rename that file.", + }); + } + if (egress !== undefined && Object.hasOwn(egress, MEMORY_ROUTE) && routed.length === 0) { const key = ["egress", MEMORY_ROUTE]; // The gateway rule names a route for a target that no entry of the profile names after it. const target = context.gateway.targets.some(({ name }) => name === MEMORY_ROUTE); @@ -314,20 +350,53 @@ function memoryRoutes( ? `Rename the '${MEMORY_ROUTE}' target of gateway '${context.gateway.name}'.` : "Rename that entry."), }); - return result; } return { - ...result, egress: { - ...tables.egress, + ...egress, [MEMORY_ROUTE]: { destinations: [`${host}${MEMORY_PATH}`], secret: { ref: RUNTIME_AWS_SIGNER }, }, }, + policies: [...userPolicies, MEMORY_POLICY_FILE], }; } +/** + * What names the AgentCore host in the local profile, for a problem: each `[egress]` entry and + * HTTP `[mcp]` server, and each permit when no entry names it, as the Box network model records + * them. With no record of the host, as in a rule test, the Runtime routes to it. + */ +function namesOfHost( + host: string, + routed: readonly { key: string[] }[], + context: RuntimeProfileContext, +): { key: string[]; label: string; place: string }[] { + const sources = context.hosts + .filter((each) => each.host === host) + .flatMap((each) => each.sources); + if (sources.length === 0) { + return routed.map(({ key }) => ({ + key, + label: `[${key.join(".")}]`, + place: context.placeOf(key), + })); + } + const named = new Map(); + for (const source of sources) { + const key = + source.kind === "entry" + ? ["egress", source.egressEntry] + : source.kind === "mcp" + ? ["mcp", source.mcpEntry] + : ["policies"]; + const label = source.kind === "rule" ? `The permit "${source.ruleId}"` : `[${key.join(".")}]`; + named.set(label, { key, label, place: context.placeOf(key) }); + } + return [...named.values()]; +} + /** * Whether `destinations` has one that box matches to `host` on port 443: the host itself, in any * case, with or without `:443`, or a `*.` wildcard of its parent domains. diff --git a/src/core/project/runtimeProfile/types.ts b/src/core/project/runtimeProfile/types.ts index cbb4aa716..ef67c526f 100644 --- a/src/core/project/runtimeProfile/types.ts +++ b/src/core/project/runtimeProfile/types.ts @@ -100,6 +100,8 @@ export const RUNTIME_PROFILE_PROBLEM_CODES = [ "RUNTIME_TELEMETRY_REFUSED", /** A route of the user under the name of a route that only the Runtime profile has. */ "RUNTIME_ROUTE_NAME_TAKEN", + /** A route to the AgentCore host in a runtime whose box reaches that host only for Memory. */ + "RUNTIME_AGENTCORE_HOST_ROUTED", ] as const; export type RuntimeProfileProblemCode = (typeof RUNTIME_PROFILE_PROBLEM_CODES)[number]; From 84e0efae83f4868aaa177b67535141df2f47788a Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Mon, 23 Nov 2026 13:43:19 -0800 Subject: [PATCH 07/11] fix(box): Also block an encoded percent on the AgentCore host Box decodes %2e once before the policy, so a double-encoded %252e reaches the policy as %2e and passes it. No Memory, actor, session, or record ID holds a %, so the dot-segment rule now also blocks %25 on the AgentCore host. --- .../project/runtimeProfile/rules/additions.test.ts | 1 + src/core/project/runtimeProfile/rules/additions.ts | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/core/project/runtimeProfile/rules/additions.test.ts b/src/core/project/runtimeProfile/rules/additions.test.ts index ab108f8fc..0465aebb0 100644 --- a/src/core/project/runtimeProfile/rules/additions.test.ts +++ b/src/core/project/runtimeProfile/rules/additions.test.ts @@ -1049,6 +1049,7 @@ describe("the additions rule", () => { "/memories/%2e%2e/identities", "/memories/%2E%2E/identities", "/memories/..%5cidentities", + "/memories/%252e%252e/identities", ]) { expect([path, blocked(path)]).toEqual([path, true]); } diff --git a/src/core/project/runtimeProfile/rules/additions.ts b/src/core/project/runtimeProfile/rules/additions.ts index 070cb1957..c145af66e 100644 --- a/src/core/project/runtimeProfile/rules/additions.ts +++ b/src/core/project/runtimeProfile/rules/additions.ts @@ -72,9 +72,11 @@ export const MEMORY_POLICY_FILE = "agentcore-memory-paths.dw"; * The box policy of {@link MEMORY_POLICY_FILE}, for the AgentCore host of `region`. On AgentCore * Runtime the box reaches that host only for Memory: the first rule blocks each request to it * whose path is not `/memories` or under it, such as a Code Interpreter, Browser, identity, or API - * key call. The second blocks a path that could leave `/memories` once AWS normalizes it: a `..` - * or `.` segment, an encoded dot, or an encoded backslash. No Memory, actor, session, or record ID - * holds a dot or a backslash, so no Memory call has one. The path holds no query string. + * key call. Box resolves `.` and `..` segments and decodes `%2e` before a rule sees the path, so + * the first rule judges the path that box sends. The second blocks what that leaves and AWS could + * still read as another path: a `..` or `.` segment, an encoded dot or backslash, and an encoded + * `%`, as in a double-encoded `%252e`. No Memory, actor, session, or record ID holds a dot, a + * backslash, or a `%`, so no Memory call has one. The path holds no query string. */ export function memoryHostPolicy(region: string): string { const host = JSON.stringify(agentCoreDataPlaneHost(region)); @@ -91,7 +93,7 @@ export function memoryHostPolicy(region: string): string { "", '@id("agentcore_host_no_dot_segments")', 'forbid (principal, action == Box::Action::"http:request", resource)', - `when { context.input.host == ${host} && (\n ${anyOf(["*..*", "*/./*", "*/.", "*%2e*", "*%2E*", "*%5c*", "*%5C*"])}) };`, + `when { context.input.host == ${host} && (\n ${anyOf(["*..*", "*/./*", "*/.", "*%2e*", "*%2E*", "*%5c*", "*%5C*", "*%25*"])}) };`, "", ].join("\n"); } From 5732c3cc2f250ac2d39be519f8da37c6a16a9b4a Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Mon, 23 Nov 2026 15:15:54 -0800 Subject: [PATCH 08/11] feat(box): Send every call of the box to the AgentCore host directly, with no path limit Box chooses where a connection goes from its host and port alone, before it sees the path, so it cannot send /memories directly and Code Interpreter or Browser through the Box gateway on one host. Code Interpreter, Browser, and the other AgentCore primitives are egress destinations on the same host as Memory, so for a runtime with its default Memory they all go to that host directly, signed with the execution role. - [egress.agentcore] now names the whole AgentCore data-plane host, not only /memories. - Build no longer writes agentcore-memory-paths.dw or lists it in policies. - A route or HTTP MCP server of the profile to the AgentCore host no longer stops build. It goes to the host directly, signed with a bare aws://, with its name and other keys, and build adds no second route beside it. What the execution role may call on that host depends on its grants. The README and the profile header say that these calls go to the host directly. --- README.md | 15 +- src/core/project/boxNetworkWriters.ts | 14 -- src/core/project/manager.boxNetwork.test.ts | 44 ++--- src/core/project/manager.test.ts | 2 +- .../runtimeProfile/rules/additions.test.ts | 174 +++-------------- .../project/runtimeProfile/rules/additions.ts | 183 +++++------------- .../runtimeProfile/rules/gateway.test.ts | 2 +- src/core/project/runtimeProfile/types.ts | 2 - 8 files changed, 100 insertions(+), 336 deletions(-) diff --git a/README.md b/README.md index 38b11bb85..f2c123612 100644 --- a/README.md +++ b/README.md @@ -261,14 +261,13 @@ second answer should say teal, and the runtime log shows `running profile ...` w A boxed agent gets the deployed Memory IDs and the Runtime's region inside the box. It does not get the `AGENTCORE_GATEWAY__URL` variables of gateways you add yourself: its calls go out through -its Box gateway. Its calls to its default Memory are the exception. Box signs them with the -execution role and sends them to Memory directly, so Memory can hold each call to the session of -the runtime. The box reaches the AgentCore host only at `/memories`: build adds a box policy, -`agentcore-memory-paths.dw`, that blocks every other path of that host, such as Code Interpreter, -Browser, identity, and API key calls. So a runtime with its default Memory cannot also route the -AgentCore host in its box profile, and build stops when it does. A `PRIVATE_VPC` runtime reaches -Memory through the AgentCore endpoint of its private VPC. A runtime in a VPC of your own needs a -`bedrock-agentcore` VPC endpoint for these calls. +its Box gateway. With its default Memory, its calls to the AgentCore host +(`bedrock-agentcore..amazonaws.com`) are the exception: box signs them with the execution +role and sends them to that host directly, so Memory can hold each call to the session of the +runtime. Box routes a connection by its host alone, and Code Interpreter, Browser, and the other +AgentCore APIs share that host, so their calls go there directly too. A `PRIVATE_VPC` runtime +reaches Memory through the AgentCore endpoint of its private VPC, whose policy allows only Memory +calls. A runtime in a VPC of your own needs a `bedrock-agentcore` VPC endpoint. A Box project that an older CLI created still has `start.sh`, or a Dockerfile that runs it. Build and deploy now render profiles that those files cannot start. Create the project again, or copy diff --git a/src/core/project/boxNetworkWriters.ts b/src/core/project/boxNetworkWriters.ts index bd1240497..55404c6de 100644 --- a/src/core/project/boxNetworkWriters.ts +++ b/src/core/project/boxNetworkWriters.ts @@ -43,7 +43,6 @@ import { } from "./templates/boxProfile"; import { isTable } from "./runtimeProfile/projectPaths"; import { serializeRuntimeProfile } from "./runtimeProfile/serialize"; -import { MEMORY_POLICY_FILE, memoryHostPolicy } from "./runtimeProfile/rules/additions"; import { RUNTIME_INPUTS, type TomlTable } from "./runtimeProfile/types"; import { ProjectStateError } from "../../errors/errors"; import { BOX_RELEASE } from "./backends/boxRelease"; @@ -458,9 +457,6 @@ export function runtimeProfileWriter(staged: readonly StagedRuntime[]): BoxNetwo kind: "file" as const, path: `${dist}/${file}`, })), - ...(listsMemoryPolicy(networkOf(networks, runtime)) - ? [{ kind: "file" as const, path: `${dist}/${MEMORY_POLICY_FILE}` }] - : []), ]), edits: (networks, { spec }) => ({ files: staged.flatMap(({ runtime, dist, kind }) => { @@ -481,22 +477,12 @@ export function runtimeProfileWriter(staged: readonly StagedRuntime[]): BoxNetwo path: `${dist}/${file}`, copyOf: `${boxLocalDir(runtime)}/${file}`, })), - // The box of a runtime with its default Memory reaches the AgentCore host only for Memory. - ...(network && listsMemoryPolicy(network) - ? [{ path: `${dist}/${MEMORY_POLICY_FILE}`, text: memoryHostPolicy(network.region) }] - : []), ]; }), }), }; } -/** Whether the Runtime profile of `network` lists the policy file of the Memory host. */ -function listsMemoryPolicy(network: BoxNetwork | undefined): boolean { - const policies = network?.runtimeProfile?.policies; - return Array.isArray(policies) && policies.includes(MEMORY_POLICY_FILE); -} - /** * The text of the AgentCore Runtime profile of `runtime`, which build derived from its local box * profile into the Box network model (`BoxNetwork.runtimeProfile`). diff --git a/src/core/project/manager.boxNetwork.test.ts b/src/core/project/manager.boxNetwork.test.ts index af8754f4f..7b9b0e384 100644 --- a/src/core/project/manager.boxNetwork.test.ts +++ b/src/core/project/manager.boxNetwork.test.ts @@ -12,7 +12,6 @@ import { createSilentLogger, matchGolden, TestIdentityClient } from "../../testi import type { DeployBackendInput, ProjectBackend } from "./backends/types"; import type { BoxNetwork } from "./boxNetwork"; import { boxShowOf, FakeBoxProfileResolver } from "./boxProfileResolver-test-support"; -import { MEMORY_POLICY_FILE, memoryHostPolicy } from "./runtimeProfile/rules/additions"; import type { ResolvedBoxProfile } from "./boxProfileResolver"; import { seedBoxCache } from "./backends/box-release-test-support"; import { BOX_RELEASE } from "./backends/boxRelease"; @@ -293,12 +292,8 @@ describe("build writes the Box network of the box profile", () => { // The Runtime profile routes each target of the Box gateway, the model's included. const profile = await runtimeProfile(root); - // The runtime has its default Memory, so the box reaches the AgentCore host only for Memory. - expect(profile.policies).toEqual(["policy.dw", MEMORY_POLICY_FILE]); + expect(profile.policies).toEqual(["policy.dw"]); expect(await text(root, "app/agent_python_strands/.box-dist/policy.dw")).toBe(POLICY); - expect(await text(root, `app/agent_python_strands/.box-dist/${MEMORY_POLICY_FILE}`)).toBe( - memoryHostPolicy("us-west-2"), - ); const egress = profile.egress as Record; // The route of [egress.github] keeps the name of its entry, and goes to its target. expect(Object.keys(egress).sort()).toEqual([ @@ -311,7 +306,7 @@ describe("build writes the Box network of the box profile", () => { expect(egress["egress-httpbin-org"]).toEqual(gatewayRoute("egress-httpbin-org", "httpbin.org")); // The Memory route has no backend: box signs it with the execution role, for Memory itself. expect(egress.agentcore).toEqual({ - destinations: ["bedrock-agentcore.us-west-2.amazonaws.com/memories"], + destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], secret: { ref: "aws://" }, }); expect(egress.bedrock).toEqual( @@ -509,28 +504,31 @@ describe("build writes the Box network of the box profile", () => { ); }); - test("stops for an entry of the profile to the AgentCore host, as the box reaches it only for Memory", async () => { + test("sends an entry of the profile for the AgentCore host directly on the Runtime, with its name", async () => { const { manager, root, box } = await strandsProject(); const host = "bedrock-agentcore.us-west-2.amazonaws.com"; await editProfile(root, { tables: `\n[egress.tools]\ndestinations = ["${host}"]\nsecret.ref = "aws://default"\n`, }); box.answer = shows({ tools: { destinations: [host], secret: { ref: "aws://default" } } }); - const spec = await text(root, SPEC); - const error = await build(manager, root).then( - () => undefined, - (caught: unknown) => caught, - ); + const { warnings } = await build(manager, root); - // Code Interpreter and Browser are on that host. On the Runtime the box reaches it only at - // /memories, directly, and blocks every other path, so build stops and writes nothing. - expect(error).toBeInstanceOf(InputValidationError); - expect((error as Error).message).toContain( - `[egress.tools] reaches ${host}. On AgentCore Runtime, a runtime with its default Memory ` + - `reaches that host only for Memory, at ${host}/memories, and box blocks every other path ` + - "of it, such as Code Interpreter and Browser.", - ); + // Code Interpreter, Browser, and Memory share that host, and box routes a connection by host + // alone. So on the Runtime the entry goes to the host directly, signed with the execution + // role, and it carries the Memory calls too. + const egress = (await runtimeProfile(root)).egress as Record; + expect(Object.keys(egress)).toEqual(["bedrock", "tools"]); + expect(egress.tools).toEqual({ destinations: [host], secret: { ref: "aws://" } }); + // The Runtime does not use a gateway target for the host, so build writes none, and gives no + // warning about the gateway role's grants for one. + expect(await targetNames(root)).toEqual(["bedrock"]); + expect(warnings).not.toContainEqual(expect.stringContaining("Target 'egress-")); + + // A second build changes nothing. + const spec = await text(root, SPEC); + const again = await build(manager, root); + expect(again.steps.filter((step) => step.includes("gateway target"))).toEqual([]); expect(await text(root, SPEC)).toBe(spec); }); @@ -1084,7 +1082,7 @@ describe("build writes the Bedrock target and the Memory route for the region it ); // The Memory route goes to the Memory host of that region, with no backend. expect(egress.agentcore as unknown).toEqual({ - destinations: ["bedrock-agentcore.eu-central-1.amazonaws.com/memories"], + destinations: ["bedrock-agentcore.eu-central-1.amazonaws.com"], secret: { ref: "aws://" }, }); }); @@ -1259,7 +1257,7 @@ describe("deploy writes the Box network before the backend deploys", () => { const authOf = async (target: string) => (await boxGateway(root)).targets.find(({ name }) => name === target)?.outboundAuth; const routeFor = (region: string) => ({ - destinations: [`bedrock-agentcore.${region}.amazonaws.com/memories`], + destinations: [`bedrock-agentcore.${region}.amazonaws.com`], secret: { ref: "aws://" }, }); const bedrockFor = (region: string) => [ diff --git a/src/core/project/manager.test.ts b/src/core/project/manager.test.ts index 6657b281c..1aafee10d 100644 --- a/src/core/project/manager.test.ts +++ b/src/core/project/manager.test.ts @@ -2341,7 +2341,7 @@ describe("FsProjectManager.build", () => { // Build writes both for the region it builds for, which the environment of the test sets. const region = box.targets[0].outboundAuth.region as string; expect(profile.egress.agentcore).toEqual({ - destinations: [`bedrock-agentcore.${region}.amazonaws.com/memories`], + destinations: [`bedrock-agentcore.${region}.amazonaws.com`], secret: { ref: "aws://" }, }); diff --git a/src/core/project/runtimeProfile/rules/additions.test.ts b/src/core/project/runtimeProfile/rules/additions.test.ts index 0465aebb0..f1fc2395a 100644 --- a/src/core/project/runtimeProfile/rules/additions.test.ts +++ b/src/core/project/runtimeProfile/rules/additions.test.ts @@ -18,16 +18,13 @@ import { deriveRuntimeProfile } from "../derive"; import { serializeRuntimeProfile } from "../serialize"; import type { TomlTable } from "../types"; import { defaultMemoryName } from "../../../../projectSchemas/memory"; -import { parsePolicy } from "../../boxNetwork/dogwood"; import { agentStartsUnderOtel, apply, DEFAULT_TELEMETRY_TARGET, DEFAULT_TRACES_TARGET, defaultTracesTelemetry, - MEMORY_POLICY_FILE, MEMORY_ROUTE, - memoryHostPolicy, runtimeProfileHeader, } from "./additions"; @@ -38,13 +35,12 @@ const BUILD_REGION = "us-west-2"; /** * The Runtime form of `recorded`, a profile the old renderer wrote for a runtime with its default - * Memory: the Memory route goes to Memory directly, under the same name, in the same place, and the - * policy file that keeps the box to `/memories` on the AgentCore host comes last in `policies`. + * Memory: the Memory route goes to the AgentCore host directly, under the same name, in the same + * place. */ function withDirectMemory(recorded: TomlTable): TomlTable { const result = structuredClone(recorded); if (isTableValue(result.egress)) result.egress[MEMORY_ROUTE] = DIRECT_MEMORY_ROUTE; - result.policies = [...((recorded.policies as string[] | undefined) ?? []), MEMORY_POLICY_FILE]; return result; } @@ -53,7 +49,7 @@ const isTableValue = (value: unknown): value is TomlTable => /** The route of the agent's Memory calls: to Memory directly, signed with the execution role. */ const DIRECT_MEMORY_ROUTE = { - destinations: [`bedrock-agentcore.${BUILD_REGION}.amazonaws.com/memories`], + destinations: [`bedrock-agentcore.${BUILD_REGION}.amazonaws.com`], secret: { ref: "aws://" }, }; @@ -995,67 +991,6 @@ describe("the additions rule", () => { }); }); - describe("the Memory host policy", () => { - const HOST = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; - - test("parses, and names its two rules", () => { - const parsed = parsePolicy(memoryHostPolicy(BUILD_REGION)); - - expect(parsed.errors).toEqual([]); - expect(parsed.statements.map(({ id, effect }) => [id, effect])).toEqual([ - ["agentcore_host_memory_only", "forbid"], - ["agentcore_host_no_dot_segments", "forbid"], - ]); - }); - - test("names the AgentCore host of the region, and in its partition", () => { - expect(memoryHostPolicy(BUILD_REGION)).toContain(`context.input.host == "${HOST}"`); - expect(memoryHostPolicy("cn-north-1")).toContain( - 'context.input.host == "bedrock-agentcore.cn-north-1.amazonaws.com.cn"', - ); - }); - - test("allows /memories and the paths under it, and blocks each other path and each dot segment", () => { - const patterns = parsePolicy(memoryHostPolicy(BUILD_REGION)).statements.flatMap(({ text }) => - [...text.matchAll(/like "([^"]+)"/g)].map(([, p]) => p!), - ); - const like = (pattern: string, path: string) => - new RegExp( - `^${pattern - .split("*") - .map((part) => part.replace(/[.?+^$|()[\]{}\\]/g, "\\$&")) - .join(".*")}$`, - ).test(path); - const blocked = (path: string) => - !(path === "/memories" || like("/memories/*", path)) || - patterns.slice(1).some((pattern) => like(pattern, path)); - - for (const path of [ - "/memories", - "/memories/mem-abc1234567/events", - "/memories/mem-abc1234567/actor/user%2F1%3Aa/sessions/s-1", - "/memories/mem-abc1234567/retrieve", - ]) { - expect([path, blocked(path)]).toEqual([path, false]); - } - for (const path of [ - "/code-interpreters/aws.codeinterpreter.v1/tools/invoke", - "/browsers/aws.browser.v1/sessions/start", - "/identities/api-key", - "/memoriesX", - "/memories/../identities/api-key", - "/memories/x/./y", - "/memories/x/..", - "/memories/%2e%2e/identities", - "/memories/%2E%2E/identities", - "/memories/..%5cidentities", - "/memories/%252e%252e/identities", - ]) { - expect([path, blocked(path)]).toEqual([path, true]); - } - }); - }); - describe("the Memory route", () => { const MEMORY = defaultMemoryName("agent"); @@ -1068,11 +1003,8 @@ describe("the additions rule", () => { expect(problems).toEqual([]); const route = (table.egress as TomlTable)[MEMORY_ROUTE]; expect(route).toEqual(DIRECT_MEMORY_ROUTE); - // No backend: the Box gateway would sign the calls with its own role. The /memories path - // limits what box signs: a call to another API of the host, such as an API key call, goes - // unsigned, and AgentCore refuses it. + // No backend: the Box gateway would sign the calls with its own role. expect(route).not.toHaveProperty("backends"); - expect((route as { destinations: string[] }).destinations[0]).toEndWith("/memories"); }); test("keeps each route of the profile, and adds the Memory route after them", () => { @@ -1095,73 +1027,28 @@ describe("the additions rule", () => { expect(other.table.egress).not.toHaveProperty(MEMORY_ROUTE); }); - test("lists the policy file that keeps the box to /memories on the AgentCore host last in policies", () => { - const context = contextOf(CODEZIP, { memories: [MEMORY] }); - - expect(apply({}, context).table.policies).toEqual([MEMORY_POLICY_FILE]); - expect(apply({ policies: ["policy.dw"] }, context).table.policies).toEqual([ - "policy.dw", - MEMORY_POLICY_FILE, - ]); - expect(apply({ policies: ["policy.dw"] }, contextOf(CODEZIP)).table.policies).toEqual([ - "policy.dw", - ]); - }); - - test("stops build for a route or HTTP MCP server of the profile to the AgentCore host, through the gateway or not", () => { + test("sends a route or HTTP MCP server of the profile to the AgentCore host directly, with its name, and adds no second route", () => { const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; const backends = [{ url: "${{ inputs.gateway.url }}/egress-tools" }]; const local = { - egress: { - tools: { destinations: [host], backends }, - direct: { destinations: [host], secret: { ref: "aws://" } }, - }, + egress: { tools: { destinations: [host], backends, when: "true" } }, mcp: { recall: { type: "http", destinations: [host], backends } }, }; - const { problems } = apply(local, contextOf(CODEZIP, { memories: [MEMORY] })); - - expect(problems.map(({ code, key }) => [code, key])).toEqual([ - ["RUNTIME_AGENTCORE_HOST_ROUTED", ["egress", "tools"]], - ["RUNTIME_AGENTCORE_HOST_ROUTED", ["egress", "direct"]], - ["RUNTIME_AGENTCORE_HOST_ROUTED", ["mcp", "recall"]], - ]); - expect(problems[0]!.reason).toBe( - `[egress.tools] reaches ${host}. On AgentCore Runtime, a runtime with its default Memory ` + - `reaches that host only for Memory, at ${host}/memories, and box blocks every other ` + - "path of it, such as Code Interpreter and Browser. Remove [egress.tools], or remove the " + - "default Memory of runtime 'agent' from agentcore.json.", - ); - }); - - test("names the entry and the permit of the local profile that name the AgentCore host", () => { - const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; - const context = { - ...contextOf(CODEZIP, { memories: [MEMORY] }), - hosts: [ - { - host, - authority: host, - target: "egress-bedrock-agentcore", - sources: [ - { kind: "entry", egressEntry: "tools", destinations: [host] }, - { kind: "rule", ruleId: "ci_calls" }, - ], - }, - ], - } as unknown as Parameters[1]; - // The gateway rule names the route of a permit-only host after its target. - const local = { - egress: { - "egress-bedrock-agentcore": { destinations: [host], backends: [{ url: "x" }] }, - }, - }; - - const labels = apply(local, context).problems.map( - ({ reason }) => reason.split(" reaches")[0], + const { table, problems, warnings } = apply( + local, + contextOf(CODEZIP, { memories: [MEMORY] }), ); - expect(labels).toEqual(["[egress.tools]", 'The permit "ci_calls"']); + // Box chooses where a connection goes from its host alone, so every call to the AgentCore + // host goes there directly. Box refuses two signed routes for one host. + expect([problems, warnings]).toEqual([[], []]); + expect(table.egress).toEqual({ + tools: { destinations: [host], when: "true", secret: { ref: "aws://" } }, + }); + expect(table.mcp).toEqual({ + recall: { type: "http", destinations: [host], secret: { ref: "aws://" } }, + }); }); test("counts a route to the AgentCore host with :443, in another case, or as a wildcard", () => { @@ -1173,9 +1060,9 @@ describe("the additions rule", () => { ]) { const local = { egress: { tools: { destinations: [destination] } } }; - expect([destination, apply(local, context).problems.map(({ code }) => code)]).toEqual([ + expect([destination, apply(local, context).table.egress]).toEqual([ destination, - ["RUNTIME_AGENTCORE_HOST_ROUTED"], + { tools: { destinations: [destination], secret: { ref: "aws://" } } }, ]); } // Another port, or a wildcard of another domain, is not the AgentCore host. @@ -1186,19 +1073,9 @@ describe("the additions rule", () => { }, }, }; - const { table, problems } = apply(other, context); - expect(problems).toEqual([]); - expect(Object.keys(table.egress as TomlTable)).toEqual(["tools", MEMORY_ROUTE]); - }); - - test("stops build for a policy file of the user under the name of the Memory policy file", () => { - const { problems } = apply( - { policies: [`rules/${MEMORY_POLICY_FILE}`] }, - contextOf(CODEZIP, { memories: [MEMORY] }), - ); - - expect(problems.map(({ code, key }) => [code, key])).toEqual([ - ["RUNTIME_ROUTE_NAME_TAKEN", ["policies"]], + expect(Object.keys(apply(other, context).table.egress as TomlTable)).toEqual([ + "tools", + MEMORY_ROUTE, ]); }); @@ -1489,8 +1366,9 @@ describe("the additions rule", () => { const context = contextOf(CODEZIP, { memories: [defaultMemoryName("agent")] }); const withMemory = runtimeProfileHeader(context, apply(createdProfile(CODEZIP), context).table); expect(withMemory[2]).toEndWith( - `except [egress.${MEMORY_ROUTE}], which goes to AgentCore Memory directly. ` + - `${MEMORY_POLICY_FILE} keeps the box to /memories on the AgentCore host.`, + `except each route to bedrock-agentcore.${BUILD_REGION}.amazonaws.com, such as ` + + `[egress.${MEMORY_ROUTE}]: box sends the calls to that host directly, so Memory can hold ` + + "each call to the session of the runtime.", ); const text = serializeRuntimeProfile( apply(createdProfile(CODEZIP), contextOf(CODEZIP)).table, diff --git a/src/core/project/runtimeProfile/rules/additions.ts b/src/core/project/runtimeProfile/rules/additions.ts index c145af66e..17a790f9f 100644 --- a/src/core/project/runtimeProfile/rules/additions.ts +++ b/src/core/project/runtimeProfile/rules/additions.ts @@ -15,9 +15,8 @@ * | `[telemetry.]` | each CloudWatch target that box takes is signed with a bare `aws://`, in the deploy region when it names none; one that box refuses goes; a `file` target goes, with a warning | * | `[telemetry.agentcore]` | added when no target is `cloudwatch_logs`: the runtime's own log group and log stream | * | `[telemetry.agentcore_traces]` | added with OTEL on when no target is `cloudwatch_traces`: X-Ray in the Runtime's region | - * | `[egress.agentcore]` | added when the runtime has its default Memory: the AgentCore host's `/memories` path, which box signs with a bare `aws://` and sends to Memory directly, not through the Box gateway | - * | `policies` | with a default Memory, {@link MEMORY_POLICY_FILE} comes last: box blocks every other path of the AgentCore host | - * | a route to the AgentCore host | with a default Memory, a problem: that host is for Memory only | + * | `[egress.agentcore]` | added when the runtime has its default Memory and no route of the profile has the AgentCore host: that host, which box signs with a bare `aws://` and sends to directly, not through the Box gateway | + * | a route to the AgentCore host | with a default Memory, it goes to the host directly, signed with a bare `aws://`, with its name and other keys | * * It moves no path: the paths rule runs after it and moves each path of the user. */ @@ -56,48 +55,6 @@ import { */ export const MEMORY_ROUTE = BOX_MEMORY_ROUTE_NAME; -/** - * The path of the AgentCore data-plane host that the Memory calls take: `/memories` and each path - * under it. Box signs only those calls on the Memory route. - */ -export const MEMORY_PATH = "/memories"; - -/** - * The policy file that build writes beside the Runtime profile of a runtime with its default - * Memory, and lists last in its `policies`: {@link memoryHostPolicy}. - */ -export const MEMORY_POLICY_FILE = "agentcore-memory-paths.dw"; - -/** - * The box policy of {@link MEMORY_POLICY_FILE}, for the AgentCore host of `region`. On AgentCore - * Runtime the box reaches that host only for Memory: the first rule blocks each request to it - * whose path is not `/memories` or under it, such as a Code Interpreter, Browser, identity, or API - * key call. Box resolves `.` and `..` segments and decodes `%2e` before a rule sees the path, so - * the first rule judges the path that box sends. The second blocks what that leaves and AWS could - * still read as another path: a `..` or `.` segment, an encoded dot or backslash, and an encoded - * `%`, as in a double-encoded `%252e`. No Memory, actor, session, or record ID holds a dot, a - * backslash, or a `%`, so no Memory call has one. The path holds no query string. - */ -export function memoryHostPolicy(region: string): string { - const host = JSON.stringify(agentCoreDataPlaneHost(region)); - const anyOf = (patterns: readonly string[]) => - patterns - .map((pattern) => `context.input.path like ${JSON.stringify(pattern)}`) - .join(" ||\n "); - return [ - "// Written by agentcore build and deploy. On AgentCore Runtime the box reaches the AgentCore", - "// host only for the agent's Memory calls.", - '@id("agentcore_host_memory_only")', - 'forbid (principal, action == Box::Action::"http:request", resource)', - `when { context.input.host == ${host} && !(context.input.path == ${JSON.stringify(MEMORY_PATH)} || context.input.path like ${JSON.stringify(`${MEMORY_PATH}/*`)}) };`, - "", - '@id("agentcore_host_no_dot_segments")', - 'forbid (principal, action == Box::Action::"http:request", resource)', - `when { context.input.host == ${host} && (\n ${anyOf(["*..*", "*/./*", "*/.", "*%2e*", "*%2E*", "*%5c*", "*%5C*", "*%25*"])}) };`, - "", - ].join("\n"); -} - /** The variables of the region of the agent, which get the region of the Runtime. */ const REGION_ENV_NAMES: ReadonlySet = new Set(["AWS_REGION", "AWS_DEFAULT_REGION"]); @@ -205,9 +162,10 @@ export function runtimeProfileHeader( `${Object.keys(RUNTIME_AWS_STAND_INS).join(" and ")} are stand-ins so boto3 in the box can ` + "build its requests. They authenticate nothing: box signs each request with the execution " + "role. Every AWS route goes to the Box gateway" + - (Array.isArray(profile.policies) && profile.policies.includes(MEMORY_POLICY_FILE) - ? `, except [egress.${MEMORY_ROUTE}], which goes to AgentCore Memory directly. ` + - `${MEMORY_POLICY_FILE} keeps the box to ${MEMORY_PATH} on the AgentCore host.` + (hasDefaultMemory(context.spec, context.runtime) + ? `, except each route to ${agentCoreDataPlaneHost(context.region)}, such as ` + + `[egress.${MEMORY_ROUTE}]: box sends the calls to that host directly, so Memory can hold ` + + "each call to the session of the runtime." : "."), ...Object.entries(tableOf(profile.telemetry)) .filter(([, target]) => sameValue(target, DEFAULT_TELEMETRY_TARGET)) @@ -274,70 +232,53 @@ export function apply(table: Readonly, context: RuntimeProfileContext const problems: RuntimeProfileProblem[] = []; const routes = memoryRoutes(result, context, problems); if (routes.egress !== undefined) result.egress = routes.egress; - if (routes.policies !== undefined) result.policies = routes.policies; + if (routes.mcp !== undefined) result.mcp = routes.mcp; return { table: result, problems, warnings }; } /** - * `[egress]` and `policies` for the agent's Memory calls when the runtime has its default Memory. - * They go to Memory directly, signed with the execution role, not through the Box gateway, whose - * role would sign them and keep Memory from holding each call to the session of the runtime. + * `[egress]` and `[mcp]` for the agent's Memory calls when the runtime has its default Memory. They + * go to the AgentCore data-plane host directly, signed with the execution role, not through the Box + * gateway, whose role would sign them and keep Memory from holding each call to the session of the + * runtime. Box chooses where a connection goes from its host alone, before it sees a path, and the + * other AgentCore APIs (Code Interpreter, Browser, and more) share that host. So every call of the + * box to the host goes there directly. * - * - Build adds {@link MEMORY_ROUTE}: the `/memories` path of the AgentCore host, signed with a bare - * `aws://`. Box signs only the calls under that path. - * - Build lists {@link MEMORY_POLICY_FILE} last in `policies`, so box blocks every other path of - * the host, for Code Interpreter, Browser, identity, and API keys among them. - * - A route or HTTP MCP server of the profile to the AgentCore host is a problem. Box refuses a - * gateway route for a host that has another route, and a route of its own would reach the other - * APIs of that host. - * - A route of the user named {@link MEMORY_ROUTE}, and a policy file of the user named - * {@link MEMORY_POLICY_FILE}, are problems. + * - With no route of the profile to the AgentCore host, build adds {@link MEMORY_ROUTE}: that host, + * signed with a bare `aws://`. + * - A route or HTTP MCP server of the profile to that host, which the gateway rule sends through the + * gateway, goes to the host directly, signed with a bare `aws://`, with its name and other keys. + * Build adds no {@link MEMORY_ROUTE} beside it: box refuses two signed routes for one host. + * - A route of the user named {@link MEMORY_ROUTE} for another host is a problem. */ function memoryRoutes( table: Readonly, context: RuntimeProfileContext, problems: RuntimeProfileProblem[], -): Pick { - const { egress, mcp, policies } = table; - if (!hasDefaultMemory(context.spec, context.runtime)) return { egress, policies }; - // box refuses these keys in another form before build gets the profile. - if ( - (egress !== undefined && !isTable(egress)) || - (mcp !== undefined && !isTable(mcp)) || - (policies !== undefined && !Array.isArray(policies)) - ) { - return { egress, policies }; +): Pick { + const { egress, mcp } = table; + if (!hasDefaultMemory(context.spec, context.runtime)) return { egress, mcp }; + // box refuses `[egress]` or `[mcp]` that is not a table before build gets the profile. + if ((egress !== undefined && !isTable(egress)) || (mcp !== undefined && !isTable(mcp))) { + return { egress, mcp }; } const host = agentCoreDataPlaneHost(context.region); - const routed = [ - ...Object.entries(egress ?? {}).map(([name, entry]) => ({ key: ["egress", name], entry })), - ...Object.entries(mcp ?? {}).map(([name, entry]) => ({ key: ["mcp", name], entry })), - ].filter(({ entry }) => isTable(entry) && hasDestination(entry.destinations, host)); - for (const named of namesOfHost(host, routed, context)) { - problems.push({ - code: "RUNTIME_AGENTCORE_HOST_ROUTED", - key: named.key, - place: named.place, - reason: - `${named.label} reaches ${host}. On AgentCore Runtime, a runtime with its default Memory ` + - `reaches that host only for Memory, at ${host}${MEMORY_PATH}, and box blocks every other ` + - "path of it, such as Code Interpreter and Browser. Remove " + - `${named.label}, or remove the default Memory of runtime '${context.runtime.name}' from ` + - "agentcore.json.", - }); - } - const userPolicies = (policies ?? []) as unknown[]; - if (userPolicies.some((file) => typeof file === "string" && file.endsWith(MEMORY_POLICY_FILE))) { - problems.push({ - code: "RUNTIME_ROUTE_NAME_TAKEN", - key: ["policies"], - place: context.placeOf(["policies"]), - reason: - `policies lists ${MEMORY_POLICY_FILE}, the name of the policy file that build writes for ` + - "the agent's Memory calls on AgentCore Runtime. Rename that file.", - }); + const tables = { egress: { ...egress }, mcp: { ...mcp } } as Record<"egress" | "mcp", TomlTable>; + let routed = false; + for (const kind of ["egress", "mcp"] as const) { + for (const [name, entry] of Object.entries(tables[kind])) { + if (!isTable(entry) || !hasDestination(entry.destinations, host)) continue; + routed = true; + const { backends: _backends, secret: _secret, ...kept } = entry; + tables[kind][name] = { ...kept, secret: { ref: RUNTIME_AWS_SIGNER } }; + } } - if (egress !== undefined && Object.hasOwn(egress, MEMORY_ROUTE) && routed.length === 0) { + const result = { + egress: Object.keys(tables.egress).length > 0 ? tables.egress : egress, + mcp: Object.keys(tables.mcp).length > 0 ? tables.mcp : mcp, + }; + if (routed) return result; + if (Object.hasOwn(tables.egress, MEMORY_ROUTE)) { const key = ["egress", MEMORY_ROUTE]; // The gateway rule names a route for a target that no entry of the profile names after it. const target = context.gateway.targets.some(({ name }) => name === MEMORY_ROUTE); @@ -352,53 +293,17 @@ function memoryRoutes( ? `Rename the '${MEMORY_ROUTE}' target of gateway '${context.gateway.name}'.` : "Rename that entry."), }); + return result; } return { + ...result, egress: { - ...egress, - [MEMORY_ROUTE]: { - destinations: [`${host}${MEMORY_PATH}`], - secret: { ref: RUNTIME_AWS_SIGNER }, - }, + ...tables.egress, + [MEMORY_ROUTE]: { destinations: [host], secret: { ref: RUNTIME_AWS_SIGNER } }, }, - policies: [...userPolicies, MEMORY_POLICY_FILE], }; } -/** - * What names the AgentCore host in the local profile, for a problem: each `[egress]` entry and - * HTTP `[mcp]` server, and each permit when no entry names it, as the Box network model records - * them. With no record of the host, as in a rule test, the Runtime routes to it. - */ -function namesOfHost( - host: string, - routed: readonly { key: string[] }[], - context: RuntimeProfileContext, -): { key: string[]; label: string; place: string }[] { - const sources = context.hosts - .filter((each) => each.host === host) - .flatMap((each) => each.sources); - if (sources.length === 0) { - return routed.map(({ key }) => ({ - key, - label: `[${key.join(".")}]`, - place: context.placeOf(key), - })); - } - const named = new Map(); - for (const source of sources) { - const key = - source.kind === "entry" - ? ["egress", source.egressEntry] - : source.kind === "mcp" - ? ["mcp", source.mcpEntry] - : ["policies"]; - const label = source.kind === "rule" ? `The permit "${source.ruleId}"` : `[${key.join(".")}]`; - named.set(label, { key, label, place: context.placeOf(key) }); - } - return [...named.values()]; -} - /** * Whether `destinations` has one that box matches to `host` on port 443: the host itself, in any * case, with or without `:443`, or a `*.` wildcard of its parent domains. diff --git a/src/core/project/runtimeProfile/rules/gateway.test.ts b/src/core/project/runtimeProfile/rules/gateway.test.ts index ab72c2a20..5ddbce001 100644 --- a/src/core/project/runtimeProfile/rules/gateway.test.ts +++ b/src/core/project/runtimeProfile/rules/gateway.test.ts @@ -499,7 +499,7 @@ const withDirectMemory = (egress: unknown) => { const result = withEntryKeys(egress); if (Object.hasOwn(result, "agentcore")) { result.agentcore = { - destinations: ["bedrock-agentcore.us-west-2.amazonaws.com/memories"], + destinations: ["bedrock-agentcore.us-west-2.amazonaws.com"], secret: { ref: "aws://" }, }; } diff --git a/src/core/project/runtimeProfile/types.ts b/src/core/project/runtimeProfile/types.ts index ef67c526f..cbb4aa716 100644 --- a/src/core/project/runtimeProfile/types.ts +++ b/src/core/project/runtimeProfile/types.ts @@ -100,8 +100,6 @@ export const RUNTIME_PROFILE_PROBLEM_CODES = [ "RUNTIME_TELEMETRY_REFUSED", /** A route of the user under the name of a route that only the Runtime profile has. */ "RUNTIME_ROUTE_NAME_TAKEN", - /** A route to the AgentCore host in a runtime whose box reaches that host only for Memory. */ - "RUNTIME_AGENTCORE_HOST_ROUTED", ] as const; export type RuntimeProfileProblemCode = (typeof RUNTIME_PROFILE_PROBLEM_CODES)[number]; From 3c9ac0350a2acbdd23544973f63ea04cca0a9e4f Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Mon, 23 Nov 2026 15:34:15 -0800 Subject: [PATCH 09/11] docs(box): Say that a boxed runtime with Memory needs the L3 Memory grant and data-plane endpoint --- README.md | 4 ++-- docs/box-local-testing.md | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f2c123612..8edb6b4c8 100644 --- a/README.md +++ b/README.md @@ -266,8 +266,8 @@ its Box gateway. With its default Memory, its calls to the AgentCore host role and sends them to that host directly, so Memory can hold each call to the session of the runtime. Box routes a connection by its host alone, and Code Interpreter, Browser, and the other AgentCore APIs share that host, so their calls go there directly too. A `PRIVATE_VPC` runtime -reaches Memory through the AgentCore endpoint of its private VPC, whose policy allows only Memory -calls. A runtime in a VPC of your own needs a `bedrock-agentcore` VPC endpoint. +reaches that host through the AgentCore endpoint of its private VPC, which only the runtime's role +may call through. A runtime in a VPC of your own needs a `bedrock-agentcore` VPC endpoint. A Box project that an older CLI created still has `start.sh`, or a Dockerfile that runs it. Build and deploy now render profiles that those files cannot start. Create the project again, or copy diff --git a/docs/box-local-testing.md b/docs/box-local-testing.md index eb0707009..2afe3456e 100644 --- a/docs/box-local-testing.md +++ b/docs/box-local-testing.md @@ -43,8 +43,9 @@ as a GitHub pre-release with the release's file names (the `aarch64-apple-darwin [aws/agentcore-l3-cdk-constructs#470](https://github.com/aws/agentcore-l3-cdk-constructs/pull/470), as its `box-integration` branch does. The Box gateway that `create` writes has `enableApplicationLogs`, and an older L3 refuses it at synth with a `ConfigValidationError`. - For a `PRIVATE_VPC` runtime with Memory, the L3 checkout also needs the `fix/secure-vpc-memory-endpoint` - change, which gives the private VPC its AgentCore endpoint. Without it, the box cannot reach Memory. + A boxed runtime with Memory also needs the `fix/secure-vpc-memory-endpoint` change of the L3: it gives the + runtime's role its Memory grant, and a `PRIVATE_VPC` runtime the AgentCore endpoint of its private VPC. Without it, + the box cannot reach Memory. - `box` `0.1.11-preview.10` on your `PATH`, the release the CLI pins. Use that release: the box features and the profiles that the CLI writes change together, so an older box can refuse the profiles. The CLI does not check the version of the box on your `PATH` yet. Build, deploy and dev From 967e82f3e266aadda5cddb28dc6c7a6f09e0d0a7 Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Mon, 23 Nov 2026 15:34:50 -0800 Subject: [PATCH 10/11] docs(box): Link the L3 PR that a boxed runtime with Memory needs --- docs/box-local-testing.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/box-local-testing.md b/docs/box-local-testing.md index 2afe3456e..6538dfae9 100644 --- a/docs/box-local-testing.md +++ b/docs/box-local-testing.md @@ -43,7 +43,8 @@ as a GitHub pre-release with the release's file names (the `aarch64-apple-darwin [aws/agentcore-l3-cdk-constructs#470](https://github.com/aws/agentcore-l3-cdk-constructs/pull/470), as its `box-integration` branch does. The Box gateway that `create` writes has `enableApplicationLogs`, and an older L3 refuses it at synth with a `ConfigValidationError`. - A boxed runtime with Memory also needs the `fix/secure-vpc-memory-endpoint` change of the L3: it gives the + A boxed runtime with Memory also needs + [aws/agentcore-l3-cdk-constructs#481](https://github.com/aws/agentcore-l3-cdk-constructs/pull/481): it gives the runtime's role its Memory grant, and a `PRIVATE_VPC` runtime the AgentCore endpoint of its private VPC. Without it, the box cannot reach Memory. - `box` `0.1.11-preview.10` on your `PATH`, the release the CLI pins. Use that release: the box From 833bc69aa0ab9ee0dead98eb7dbebf626f9846db Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Tue, 24 Nov 2026 10:04:04 -0800 Subject: [PATCH 11/11] fix(box): Stop build for a route that sends its own key to the AgentCore host With the default Memory, box sends every call to the AgentCore host directly, signed with the execution role, and box cannot route one host both ways. A route to that host with an env:// key, such as an HTTP MCP server on another AgentCore Runtime with bearer auth, lost its key with no message: the server got SigV4 instead of the token, and deploy still provisioned the credential. Build now stops with RUNTIME_AGENTCORE_HOST_CREDENTIAL and names the [egress] entry or [mcp] server and its variable, from the Box network model's record of the host's API key, or from a secret other than aws:// that the entry still holds. That also covers an entry with the AgentCore host and another host. Also: - The profile header names the routes that reach the AgentCore host, not always [egress.agentcore]. - The gateway targets writer treats only the AgentCore host's target as direct, so a rule that drops another route never removes its target. - The README says that code in the box can make any AgentCore call that the execution role allows, including a grant added through additionalPolicies. --- README.md | 4 +- src/core/project/boxNetworkWriters.ts | 11 ++- src/core/project/manager.boxNetwork.test.ts | 68 +++++++++++++++ .../runtimeProfile/rules/additions.test.ts | 84 ++++++++++++++++++- .../project/runtimeProfile/rules/additions.ts | 76 ++++++++++++++++- src/core/project/runtimeProfile/types.ts | 2 + 6 files changed, 235 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 396a44867..ee3fc2697 100644 --- a/README.md +++ b/README.md @@ -274,7 +274,9 @@ role and sends them to that host directly, so Memory can hold each call to the s runtime. Box routes a connection by its host alone, and Code Interpreter, Browser, and the other AgentCore APIs share that host, so their calls go there directly too. A `PRIVATE_VPC` runtime reaches that host through the AgentCore endpoint of its private VPC, which only the runtime's role -may call through. A runtime in a VPC of your own needs a `bedrock-agentcore` VPC endpoint. +may call through. Code in the box can make any AgentCore call on that host that the +execution role allows, so a `bedrock-agentcore` grant added through `additionalPolicies` is open +to it. A runtime in a VPC of your own needs a `bedrock-agentcore` VPC endpoint. A Box project that an older CLI created still has `start.sh`, or a Dockerfile that runs it. Build and deploy now render profiles that those files cannot start. Create the project again, or copy diff --git a/src/core/project/boxNetworkWriters.ts b/src/core/project/boxNetworkWriters.ts index fc7e94a9e..9051b257c 100644 --- a/src/core/project/boxNetworkWriters.ts +++ b/src/core/project/boxNetworkWriters.ts @@ -19,6 +19,7 @@ import { type BoxNetwork, type BoxNetworkHost, } from "./boxNetwork"; +import { agentCoreDataPlaneHost } from "../../handlers/project/shortcuts"; import { projectGatewayTargetFor } from "./boxNetwork/projectGateways"; import { addBoxGatewayTargets, BOX_BEDROCK_POLICY_FILE, boxBedrockPolicy } from "./boxMemory"; import type { ReadEnvFileOptions } from "../../io/envFile"; @@ -251,10 +252,14 @@ function writeGatewayTargets(spec: ProjectSpec, network: BoxNetwork): string[] { ? [] : [{ ...host, target: host.target }], ); - // A host whose route the Runtime profile sends directly, not to its target, takes no target: the - // agent's Memory calls go to the AgentCore host directly (runtimeProfile/rules/additions.ts). + // The AgentCore host takes no target when the Runtime profile sends its calls directly: with the + // default Memory, every call of the box to that host goes there (runtimeProfile/rules/additions.ts). + // Only that host, so a rule that drops another route never removes its target with this message. const routed = gatewayRoutedTargets(network.runtimeProfile); - const direct = served.filter((host) => routed !== undefined && !routed.has(host.target)); + const agentCoreHost = agentCoreDataPlaneHost(network.region); + const direct = served.filter( + (host) => host.host === agentCoreHost && routed !== undefined && !routed.has(host.target), + ); const used = served.filter((host) => !direct.includes(host)); const sent = used.filter((host) => undeclaredKey(spec, host) === undefined); const unsent = used.filter((host) => undeclaredKey(spec, host) !== undefined); diff --git a/src/core/project/manager.boxNetwork.test.ts b/src/core/project/manager.boxNetwork.test.ts index 7e99eb6b2..c838c7931 100644 --- a/src/core/project/manager.boxNetwork.test.ts +++ b/src/core/project/manager.boxNetwork.test.ts @@ -578,6 +578,74 @@ describe("build writes the Box network of the box profile", () => { expect(await text(root, SPEC)).toBe(spec); }); + const AGENTCORE_HOST = "bedrock-agentcore.us-west-2.amazonaws.com"; + test.each([ + { + form: "an HTTP [mcp] server", + label: "[mcp.tools]", + tables: `\n[mcp.tools]\ntype = "http"\ndestinations = ["${AGENTCORE_HOST}"]\nsecret.ref = "env://TOOLS_TOKEN"\n`, + answer: { + egress: { bedrock: BEDROCK }, + mcp: { + tools: { + type: "http", + destinations: [AGENTCORE_HOST], + secret: { ref: "env://TOOLS_TOKEN" }, + }, + }, + }, + }, + { + form: "an [egress] entry", + label: "[egress.tools]", + tables: `\n[egress.tools]\ndestinations = ["${AGENTCORE_HOST}"]\nsecret.ref = "env://TOOLS_TOKEN"\n`, + answer: { + egress: { + bedrock: BEDROCK, + tools: { destinations: [AGENTCORE_HOST], secret: { ref: "env://TOOLS_TOKEN" } }, + }, + }, + }, + { + form: "an [egress] entry with another host too", + label: "[egress.tools]", + tables: `\n[egress.tools]\ndestinations = ["${AGENTCORE_HOST}", "api.github.com"]\nsecret.ref = "env://TOOLS_TOKEN"\n`, + answer: { + egress: { + bedrock: BEDROCK, + tools: { + destinations: [AGENTCORE_HOST, "api.github.com"], + secret: { ref: "env://TOOLS_TOKEN" }, + }, + }, + }, + }, + ])( + "stops for $form that sends an env:// key to the AgentCore host, and writes nothing", + async ({ label, tables, answer }) => { + const { manager, root, box } = await strandsProject({ env: { TOOLS_TOKEN: "tools-123" } }); + await editProfile(root, { tables }); + box.answer = boxShowOf(answer); + const spec = await text(root, SPEC); + + const error = await build(manager, root).then( + () => undefined, + (caught: unknown) => caught, + ); + + // With the default Memory, box sends every call to the AgentCore host directly, signed with + // the execution role, so the key would never reach the server. + expect(error).toBeInstanceOf(InputValidationError); + expect((error as Error).message).toContain( + `${label} sends env://TOOLS_TOKEN to ${AGENTCORE_HOST}. On AgentCore Runtime, runtime ` + + "'agent_python_strands' has its default Memory, so box sends every call to that host " + + "directly, signed with the execution role", + ); + // Build provisions no credential that nothing would send. + expect(await text(root, SPEC)).toBe(spec); + }, + ); + test("stops for a user target named agentcore, the name of the Memory route, and keeps it", async () => { const { manager, root, box } = await strandsProject(); const mine = { diff --git a/src/core/project/runtimeProfile/rules/additions.test.ts b/src/core/project/runtimeProfile/rules/additions.test.ts index 25c81ed5f..16f5d3320 100644 --- a/src/core/project/runtimeProfile/rules/additions.test.ts +++ b/src/core/project/runtimeProfile/rules/additions.test.ts @@ -1184,6 +1184,79 @@ describe("the additions rule", () => { }); }); + test.each([ + { kind: "egress", entry: {} }, + { kind: "mcp", entry: { type: "http" } }, + ] as const)( + "stops build for an [$kind] entry that sends an env:// key to the AgentCore host", + ({ kind, entry }) => { + const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; + const context = { + ...contextOf(CODEZIP, { memories: [MEMORY] }), + hosts: [ + { + host, + authority: host, + target: "egress-tools", + outboundAuth: { + kind: "apiKey", + credentialName: "tools", + variable: "TOOLS_TOKEN", + header: "Authorization", + }, + sources: [ + kind === "egress" + ? { kind: "entry", egressEntry: "tools", destinations: [host] } + : { kind: "mcp", mcpEntry: "tools", destinations: [host] }, + ], + }, + ], + } as unknown as Parameters[1]; + // As the gateway rule writes it: a backend to the target that sends the key. + const local = { + [kind]: { + tools: { + ...entry, + destinations: [host], + backends: [{ url: "${{ inputs.gateway.url }}/egress-tools" }], + }, + }, + }; + + const { problems } = apply(local, context); + + expect(problems).toEqual([ + { + code: "RUNTIME_AGENTCORE_HOST_CREDENTIAL", + key: [kind, "tools"], + place: PROFILE_FILE, + reason: + `[${kind}.tools] sends env://TOOLS_TOKEN to ${host}. On AgentCore Runtime, runtime ` + + "'agent' has its default Memory, so box sends every call to that host directly, " + + "signed with the execution role, and not through the Box gateway. Box cannot route " + + "one host both ways, so the key would not be sent. Remove the secret from " + + `[${kind}.tools] if the service takes the execution role's signature, or remove the ` + + "default Memory of runtime 'agent' from agentcore.json.", + }, + ]); + }, + ); + + test("stops build for a route to the AgentCore host that still holds a secret other than aws://", () => { + const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; + const local = { + mcp: { + tools: { type: "http", destinations: [host], secret: { ref: "env://TOOLS_TOKEN" } }, + }, + }; + + const { problems } = apply(local, contextOf(CODEZIP, { memories: [MEMORY] })); + + expect(problems.map(({ code, key }) => [code, key])).toEqual([ + ["RUNTIME_AGENTCORE_HOST_CREDENTIAL", ["mcp", "tools"]], + ]); + }); + test("counts a route to the AgentCore host with :443, in another case, or as a wildcard", () => { const context = contextOf(CODEZIP, { memories: [MEMORY] }); for (const destination of [ @@ -1498,10 +1571,15 @@ describe("the additions rule", () => { expect(standIns).toEndWith("Every AWS route goes to the Box gateway."); const context = contextOf(CODEZIP, { memories: [defaultMemoryName("agent")] }); const withMemory = runtimeProfileHeader(context, apply(createdProfile(CODEZIP), context).table); + const host = `bedrock-agentcore.${BUILD_REGION}.amazonaws.com`; expect(withMemory[2]).toEndWith( - `except each route to bedrock-agentcore.${BUILD_REGION}.amazonaws.com, such as ` + - `[egress.${MEMORY_ROUTE}]: box sends the calls to that host directly, so Memory can hold ` + - "each call to the session of the runtime.", + `except [egress.${MEMORY_ROUTE}], to ${host}: box sends the calls to that host directly, so ` + + "Memory can hold each call to the session of the runtime.", + ); + // A route of the user that replaces [egress.agentcore] is the one the header names. + const routed = apply({ egress: { tools: { destinations: [host] } } }, context).table; + expect(runtimeProfileHeader(context, routed)[2]).toContain( + `except [egress.tools], to ${host}:`, ); const text = serializeRuntimeProfile( apply(createdProfile(CODEZIP), contextOf(CODEZIP)).table, diff --git a/src/core/project/runtimeProfile/rules/additions.ts b/src/core/project/runtimeProfile/rules/additions.ts index fadc18d55..b1b0294f9 100644 --- a/src/core/project/runtimeProfile/rules/additions.ts +++ b/src/core/project/runtimeProfile/rules/additions.ts @@ -165,9 +165,9 @@ export function runtimeProfileHeader( "build its requests. They authenticate nothing: box signs each request with the execution " + "role. Every AWS route goes to the Box gateway" + (hasDefaultMemory(context.spec, context.runtime) - ? `, except each route to ${agentCoreDataPlaneHost(context.region)}, such as ` + - `[egress.${MEMORY_ROUTE}]: box sends the calls to that host directly, so Memory can hold ` + - "each call to the session of the runtime." + ? `, except ${routesToHost(profile, agentCoreDataPlaneHost(context.region))}: box sends ` + + "the calls to that host directly, so Memory can hold each call to the session of the " + + "runtime." : "."), ...Object.entries(tableOf(profile.telemetry)) .filter(([, target]) => sameValue(target, DEFAULT_TELEMETRY_TARGET)) @@ -265,6 +265,7 @@ function memoryRoutes( return { egress, mcp }; } const host = agentCoreDataPlaneHost(context.region); + credentialProblems(host, egress, mcp, context, problems); const tables = { egress: { ...egress }, mcp: { ...mcp } } as Record<"egress" | "mcp", TomlTable>; let routed = false; for (const kind of ["egress", "mcp"] as const) { @@ -306,6 +307,75 @@ function memoryRoutes( }; } +/** + * A problem for each entry of the profile that sends a credential of its own to the AgentCore host, + * such as the `env://` API key of an MCP server on another AgentCore Runtime. With the default + * Memory, every call of the box to that host goes there directly, signed with the execution role, + * and box cannot route one host both ways. So the key would never be sent, and the server would + * refuse the call. The Box network model records the API key of the host; an entry that still + * holds a secret other than `aws://` names one too. + */ +function credentialProblems( + host: string, + egress: TomlTable | undefined, + mcp: TomlTable | undefined, + context: RuntimeProfileContext, + problems: RuntimeProfileProblem[], +): void { + const named = new Map(); + for (const record of context.hosts) { + if (record.host !== host || record.outboundAuth.kind !== "apiKey") continue; + const secret = `env://${record.outboundAuth.variable}`; + for (const source of record.sources) { + if (source.kind === "entry") { + named.set(`egress.${source.egressEntry}`, { key: ["egress", source.egressEntry], secret }); + } else if (source.kind === "mcp") { + named.set(`mcp.${source.mcpEntry}`, { key: ["mcp", source.mcpEntry], secret }); + } + } + } + for (const [kind, table] of [ + ["egress", egress], + ["mcp", mcp], + ] as const) { + for (const [name, entry] of Object.entries(table ?? {})) { + if (!isTable(entry) || !hasDestination(entry.destinations, host)) continue; + const ref = isTable(entry.secret) ? entry.secret.ref : undefined; + if (typeof ref !== "string" || ref.startsWith("aws://")) continue; + if (!named.has(`${kind}.${name}`)) + named.set(`${kind}.${name}`, { key: [kind, name], secret: ref }); + } + } + for (const { key, secret } of named.values()) { + const label = `[${key.join(".")}]`; + problems.push({ + code: "RUNTIME_AGENTCORE_HOST_CREDENTIAL", + key, + place: context.placeOf(key), + reason: + `${label} sends ${secret} to ${host}. On AgentCore Runtime, runtime ` + + `'${context.runtime.name}' has its default Memory, so box sends every call to that host ` + + "directly, signed with the execution role, and not through the Box gateway. Box cannot " + + "route one host both ways, so the key would not be sent. Remove the secret from " + + `${label} if the service takes the execution role's signature, or remove the default ` + + `Memory of runtime '${context.runtime.name}' from agentcore.json.`, + }); + } +} + +/** + * The routes and HTTP MCP servers of `profile` to `host`, as the header names them: `[egress.agentcore]`, + * or the user's routes when one replaces it. With none, as before the rule runs, the host itself. + */ +function routesToHost(profile: Readonly, host: string): string { + const labels = (["egress", "mcp"] as const).flatMap((kind) => + Object.entries(tableOf(profile[kind])) + .filter(([, entry]) => isTable(entry) && hasDestination(entry.destinations, host)) + .map(([name]) => `[${kind}.${name}]`), + ); + return labels.length > 0 ? `${labels.join(", ")}, to ${host}` : `each route to ${host}`; +} + /** * Whether `destinations` has one that box matches to `host` on port 443: the host itself, in any * case, with or without `:443`, or a `*.` wildcard of its parent domains. diff --git a/src/core/project/runtimeProfile/types.ts b/src/core/project/runtimeProfile/types.ts index 661c20095..a54dcd89a 100644 --- a/src/core/project/runtimeProfile/types.ts +++ b/src/core/project/runtimeProfile/types.ts @@ -106,6 +106,8 @@ export const RUNTIME_PROFILE_PROBLEM_CODES = [ "RUNTIME_TELEMETRY_REFUSED", /** A route of the user under the name of a route that only the Runtime profile has. */ "RUNTIME_ROUTE_NAME_TAKEN", + /** A route to the AgentCore host with a credential of its own, which the direct route cannot send. */ + "RUNTIME_AGENTCORE_HOST_CREDENTIAL", /** An `[mcp]` server with the name of a gateway of the project, which build adds on the Runtime. */ "RUNTIME_GATEWAY_SERVER_NAME_TAKEN", ] as const;