From 18f07c696ce46e042feb1f8d025e544e0aa12b4e Mon Sep 17 00:00:00 2001 From: Varun Gunda Date: Fri, 20 Nov 2026 17:41:47 -0800 Subject: [PATCH 1/2] 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 2/2] 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];