Repository navigation
feat(box): Add a Box template for each Python agent, with its profile fixes #2857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e98ce7a
4ed1626
5591311
3579cfb
a190307
3df66a2
f1f55b8
d6eec45
68d2bbb
c1bc457
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,6 +99,14 @@ const RUNTIME_AWS_STAND_IN_LINES = [ | |
| 'AWS_SECRET_ACCESS_KEY = "box-stand-in"', | ||
| ].join("\n"); | ||
|
|
||
| /** | ||
| * The tools of box/base that get the project's write grant: box gives each one its own grant, so | ||
| * each lists the agent's `deny_write` folders as well. Taken from box/base in the box release that | ||
| * `boxRelease.json` pins (0.1.11-linux-preview.6). When the pin moves, check box/base's tools: box | ||
| * refuses a CodeZip box profile if a tool it misses gets the project write grant. | ||
| */ | ||
| const BOX_BASE_PROJECT_WRITE_TOOLS = ["uname"]; | ||
|
mquang-amazon marked this conversation as resolved.
Resolved
|
||
|
|
||
| type Protocol = NonNullable<ScaffoldRuntimeInput["protocol"]>; | ||
|
|
||
| /** | ||
|
|
@@ -443,6 +451,12 @@ export function renderBoxProfile( | |
| const workloadPort = boxWorkloadPort(protocol); | ||
| const onRuntime = target === "runtime"; | ||
| const container = isBoxedContainer(runtime); | ||
| // The image's agent reads /app and writes nothing there. Build resolves the profile on the host, | ||
| // where the image's /app/.venv does not exist, so a write grant would leave its Python unprotected | ||
| // and box would refuse the profile. A CodeZip agent writes in the folder box starts in. | ||
| const projectSection = container | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every local CodeZip profile now gets
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would do start box in app/runtime instead, since ever read can still have .env.local. THis is simpler
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed on |
||
| ? '# The agent, and the tools its packs add, can read the folder box starts in.\n[project]\naccess = "read"\n' | ||
| : '# The agent, and the tools its packs add, can read and write the folder box starts in.\n[project]\naccess = "write"\n'; | ||
| const codeZipOnRuntime = onRuntime && !container; | ||
| const runner = container | ||
| ? containerRunner(workloadPort, !onRuntime) | ||
|
|
@@ -479,6 +493,14 @@ export function renderBoxProfile( | |
| const execComment = runner.execComment ?? ""; | ||
| const denyWrite = | ||
| runner.denyWrite.length > 0 ? `deny_write = [${runner.denyWrite.join(", ")}]\n` : ""; | ||
| // box refuses to run a program from a folder that any process in the box can write, so each | ||
| // tool that gets the project's write grant lists the venv too. | ||
| const toolDenyWrite = | ||
| !container && runner.denyWrite.length > 0 | ||
| ? BOX_BASE_PROJECT_WRITE_TOOLS.map( | ||
| (tool) => `\n[tool.${tool}.filesystem]\ndeny_write = [${runner.denyWrite.join(", ")}]\n`, | ||
| ).join("") | ||
| : ""; | ||
| // Only the Runtime agent calls AgentCore: locally, LOCAL_DEV=1 skips Identity, and no memory ID | ||
| // reaches the box, so Memory is off. | ||
| const routes = onRuntime ? runtimeRoutes : ""; | ||
|
|
@@ -520,6 +542,7 @@ ${policies} | |
| [profile] | ||
| name = ${str(projectName)} | ||
|
|
||
| ${projectSection} | ||
| ${options}[agent] | ||
| command = ${list(runner.command)} | ||
|
|
||
|
|
@@ -533,28 +556,23 @@ ${denyWrite} | |
| [[agent.ingress]] | ||
| listen = ${str(`${listenHost}:${ingressPort}`)} | ||
| workload_port = ${workloadPort} | ||
| ${routes}${bedrockRoute}${keyedRoute}`; | ||
| ${toolDenyWrite}${routes}${bedrockRoute}${keyedRoute}`; | ||
| } | ||
|
|
||
| /** | ||
| * A route the agent's calls take on the Runtime: the box sends calls to `host` | ||
| * to the runtime's Box gateway target `target`, signing them for the gateway in | ||
| * `region` with the execution role; the gateway calls `host` with the target's | ||
| * the Runtime's region with the execution role; the gateway calls `host` with the target's | ||
| * own auth. Every `[egress.<name>]` on the Runtime is one of these, so every | ||
| * outbound call goes through the Box gateway. Starts with a blank line. | ||
| */ | ||
| function runtimeGatewayRouteSection( | ||
| gatewayName: string, | ||
| target: string, | ||
| host: string, | ||
| region: string, | ||
| ): string { | ||
| function runtimeGatewayRouteSection(gatewayName: string, target: string, host: string): string { | ||
| const key = tomlKey(target); | ||
| return ` | ||
| # Calls to ${host}, through the '${gatewayName}' gateway's '${target}' target. | ||
| [egress.${key}] | ||
| destinations = [${str(host)}] | ||
| ${runtimeGatewayBackend(`egress.${key}`, target, region)}`; | ||
| ${runtimeGatewayBackend(`egress.${key}`, target)}`; | ||
| } | ||
|
|
||
| /** An HTTP MCP server of the box profile that a Box gateway target serves. */ | ||
|
|
@@ -574,7 +592,6 @@ function runtimeMcpServerSection( | |
| server: RuntimeMcpServer, | ||
| target: string, | ||
| host: string, | ||
| region: string, | ||
| ): string { | ||
| const key = tomlKey(server.name); | ||
| const secret = server.secret?.ref.startsWith("env://") | ||
|
|
@@ -588,23 +605,23 @@ function runtimeMcpServerSection( | |
| [mcp.${key}] | ||
| type = "http" | ||
| destinations = [${str(host)}] | ||
| ${secret}${runtimeGatewayBackend(`mcp.${key}`, target, region)}`; | ||
| ${secret}${runtimeGatewayBackend(`mcp.${key}`, target)}`; | ||
| } | ||
|
|
||
| /** | ||
| * The backend of a Runtime route or MCP server: the runtime's Box gateway | ||
| * target, signed for the gateway in `region` with the execution role. Both | ||
| * Runtime profiles read the gateway URL from the environment through the | ||
| * box/agentcore pack's input, and sign through a bare `aws://`, so no start | ||
| * script fills the profile in. Starts with a blank line. | ||
| * target. Both Runtime profiles read the gateway URL and the region from the | ||
| * environment through the agentcore pack's inputs, and sign with the execution | ||
| * role through a bare `aws://`, so the backend names no region. Starts with a | ||
| * blank line. | ||
| */ | ||
| function runtimeGatewayBackend(table: string, target: string, region: string): string { | ||
| function runtimeGatewayBackend(table: string, target: string): string { | ||
| return ` | ||
| [[${table}.backends]] | ||
| url = ${str(`\${{ inputs.gateway.url }}/${target}`)} | ||
| auth.ref = ${str(RUNTIME_AWS_SIGNER)} | ||
| auth.aws.service = "bedrock-agentcore" | ||
| auth.aws.region = ${str(region)} | ||
| auth.aws.region = "\${{ inputs.aws.region }}" | ||
| `; | ||
| } | ||
|
|
||
|
|
@@ -619,7 +636,6 @@ auth.aws.region = ${str(region)} | |
| function runtimeGatewayRoutes( | ||
| spec: ProjectSpec, | ||
| runtime: ProjectRuntime, | ||
| region: string, | ||
| servers: ReadonlyMap<string, RuntimeMcpServer>, | ||
| ): string { | ||
| if (!isBoxProject(spec)) return ""; | ||
|
|
@@ -646,8 +662,8 @@ function runtimeGatewayRoutes( | |
| .map((target) => { | ||
| const server = servers.get(target.name); | ||
| return server | ||
| ? runtimeMcpServerSection(gateway.name, server, target.name, hostOf(target), region) | ||
| : runtimeGatewayRouteSection(gateway.name, target.name, hostOf(target), region); | ||
| ? runtimeMcpServerSection(gateway.name, server, target.name, hostOf(target)) | ||
| : runtimeGatewayRouteSection(gateway.name, target.name, hostOf(target)); | ||
| }) | ||
| .join(""); | ||
| } | ||
|
|
@@ -721,7 +737,7 @@ export function renderRuntimeBoxProfile( | |
| input, | ||
| region, | ||
| "runtime", | ||
| runtimeGatewayRoutes(spec, runtime, region, runtimeMcpServers(network?.hosts ?? [])), | ||
| runtimeGatewayRoutes(spec, runtime, runtimeMcpServers(network?.hosts ?? [])), | ||
| network?.policyFiles, | ||
| )!; | ||
| return profile + (network?.telemetry ?? []).map(runtimeTelemetrySection).join(""); | ||
|
|
@@ -812,7 +828,8 @@ description = "The runtime's Box gateway URL." | |
| from = ${str(`env:${BOX_GATEWAY_URL_ENV}`)} | ||
| pattern = ${str(GATEWAY_URL_PATTERN)} | ||
|
|
||
| # The Runtime's region. The Runtime sets AWS_REGION; the profile passes it to the agent. | ||
| # The Runtime's region, which box signs each Box gateway backend for and passes to the agent. The | ||
| # Runtime sets AWS_REGION. | ||
| [inputs.aws.region] | ||
| description = "The AWS region the runtime runs in." | ||
| from = "env:AWS_REGION" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small one: the empty
.venvcould be part of the template tree inwithBoxStartScript, next tostart.py, rather than a--skip-installbranch increate.uv syncfills an existing empty.venv, and it keeps the box layout in one place.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I have this in later commit, will include it herer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #2886 (08fb943): the empty
.venvis part ofboxCodeZipFileswithstart.pyintemplates/project.ts, which bothcreateandadd runtimewrite. The--skip-installbranch increateand the copy inadd runtimeare gone.