Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,15 @@ 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_<NAME>_URL` variables of gateways you add yourself: its calls go out through
its Box gateway.
its Box gateway. With its default Memory, its calls to the AgentCore host
(`bedrock-agentcore.<region>.amazonaws.com`) are the exception: box signs them with the execution
role and sends them to that host directly, so Memory can hold each call to the session of the
runtime. Box routes a connection by its host alone, and Code Interpreter, Browser, and the other
AgentCore APIs share that host, so their calls go there directly too. A `PRIVATE_VPC` runtime
reaches that host through the AgentCore endpoint of its private VPC, which only the runtime's role
may call through. Code in the box can make any AgentCore call on that host that the
execution role allows, so a `bedrock-agentcore` grant added through `additionalPolicies` is open
to it. A runtime in a VPC of your own needs a `bedrock-agentcore` VPC endpoint.

A Box project that an older CLI created still has `start.sh`, or a Dockerfile that runs it. Build
and deploy now render profiles that those files cannot start. Create the project again, or copy
Expand Down
9 changes: 7 additions & 2 deletions docs/box-local-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ fork of [strands-agents/box](https://github.com/strands-agents/box), then run
[aws/agentcore-l3-cdk-constructs#470](https://github.com/aws/agentcore-l3-cdk-constructs/pull/470),
as its `box-integration` branch does. The Box gateway that `create` writes has
`enableApplicationLogs`, and an older L3 refuses it at synth with a `ConfigValidationError`.
A boxed runtime with Memory also needs
[aws/agentcore-l3-cdk-constructs#481](https://github.com/aws/agentcore-l3-cdk-constructs/pull/481): it gives the
runtime's role its Memory grant, and a `PRIVATE_VPC` runtime the AgentCore endpoint of its private VPC. Without it,
the box cannot reach Memory.
- `box` `0.3.0` on your `PATH` (`0.3.0-preview.1` on macOS), the release the CLI pins. Use that
release: the box
features and the profiles that the CLI writes change together, so an older box can refuse the
profiles. The CLI does not check the version of the box on your `PATH` yet. Build, deploy and dev
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 AgentCore 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
Expand Down
168 changes: 22 additions & 146 deletions src/core/project/boxMemory.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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: [] });
Expand Down Expand Up @@ -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");
});
});
Loading
Loading