Skip to content

Commit fcc16d1

Browse files
committed
feat(create): Write a box profile for each Python template
`agentcore create` writes a box profile to <project>/.box/profile.toml, named after the project in [profile] name, and keeps one that already exists. Run it from the project folder with `box run .box/profile.toml`. box keeps the agent out of the project's .box folder, so the agent cannot change its own profile. - Python CodeZip (Strands, Minimal, LangChain, AG-UI, A2A, MCP): the agent runs from its venv in app/<runtime>, on its dev port with the agent one port above. The venv is in [agent.filesystem] exec and deny_write, both optional, so the agent can run it, compiled modules included, but cannot change it. Bedrock agents sign model calls with the default AWS profile; OpenAI, Anthropic, Gemini, OpenAI-compatible and LiteLLM agents get an API key route instead. The profile has no AgentCore route: locally LOCAL_DEV=1 skips Identity and no memory ID reaches the box. - Strands container: the agent runs from /app with the image's Python, and box listens on 0.0.0.0 so Docker can forward the port. The image does not run box yet. - TypeScript templates and Bedrock Managed Agents get no profile. The profile needs a profile file run by its path (strands-agents/staging-boxy#419), [[agent.ingress]] (strands-agents/staging-boxy#418), and deny_write (strands-agents/staging-boxy#433, strands-agents/box#429).
1 parent 306b6f6 commit fcc16d1

6 files changed

Lines changed: 631 additions & 1 deletion

File tree

‎src/core/project/__snapshots__/manager.test.ts.snap‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
exports[`FsProjectManager.create scaffolds the expected file tree into a fresh directory 1`] = `
44
[
5+
".box/profile.toml",
56
".gitignore",
67
"agentcore/.env.local",
78
"agentcore/agentcore.json",
@@ -22,6 +23,7 @@ exports[`FsProjectManager.create scaffolds the expected file tree into a fresh d
2223
exports[`FsProjectManager.create snapshots the Strands project manifest and runtime spec 1`] = `
2324
{
2425
"manifest": [
26+
".box/profile.toml",
2527
".gitignore",
2628
"agentcore/.env.local",
2729
"agentcore/agentcore.json",
@@ -142,6 +144,7 @@ exports[`FsProjectManager.create snapshots the Strands TypeScript project manife
142144
exports[`FsProjectManager.create snapshots the Strands A2A project manifest and runtime spec 1`] = `
143145
{
144146
"manifest": [
147+
".box/profile.toml",
145148
".gitignore",
146149
"agentcore/.env.local",
147150
"agentcore/agentcore.json",
@@ -202,6 +205,7 @@ exports[`FsProjectManager.create snapshots the Strands A2A project manifest and
202205
exports[`FsProjectManager.create snapshots the LangChain project manifest and runtime spec 1`] = `
203206
{
204207
"manifest": [
208+
".box/profile.toml",
205209
".gitignore",
206210
"agentcore/.env.local",
207211
"agentcore/agentcore.json",

‎src/core/project/manager.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
type AddResourceOptions,
4747
} from "../../handlers/project/types";
4848
import { createSilentLogger, TestIdentityClient } from "../../testing";
49+
import { BOX_PROFILE_FILE, renderBoxProfile } from "./templates/boxProfile";
4950
import type { DeployBackendInput, ProjectBackend } from "./backends/types";
5051
import { MINIMUM_COMPATIBLE_CDK_VERSION } from "./backends/cdk/compatibility";
5152

@@ -607,6 +608,7 @@ describe("FsProjectManager.create", () => {
607608
"creating project tree",
608609
"installing CDK dependencies with npm",
609610
"syncing Python dependencies with uv",
611+
expect.stringMatching(/^writing box profile .*[\\/]example[\\/]\.box[\\/]profile\.toml$/),
610612
"initializing git repository",
611613
]);
612614
expect(project.name).toBe("example");
@@ -651,6 +653,60 @@ describe("FsProjectManager.create", () => {
651653
});
652654
});
653655

656+
describe("FsProjectManager.create box profile", () => {
657+
test.each([
658+
["after installing", false],
659+
["with --skip-install", true],
660+
])("writes the box profile into the project's .box folder %s", async (_label, skipInstall) => {
661+
const directory = await inTempDirectory();
662+
const { events } = await runCreate(manager().manager, {
663+
name: "example",
664+
scaffoldRuntimeInput: AGENT_PYTHON_STRANDS,
665+
skipInstall,
666+
skipGit: true,
667+
});
668+
669+
const path = join(directory, "example", BOX_PROFILE_FILE);
670+
expect(await readFile(path, "utf8")).toBe(renderBoxProfile("example", AGENT_PYTHON_STRANDS)!);
671+
expect(events).toContainEqual({ type: "step", message: `writing box profile ${path}` });
672+
});
673+
674+
test("leaves an existing box profile unchanged", async () => {
675+
const directory = await inTempDirectory();
676+
const path = join(directory, "example", BOX_PROFILE_FILE);
677+
await mkdir(join(directory, "example", ".box"), { recursive: true });
678+
await writeFile(path, "# mine\n");
679+
680+
const { events } = await runCreate(manager().manager, {
681+
name: "example",
682+
scaffoldRuntimeInput: AGENT_PYTHON_STRANDS,
683+
skipInstall: true,
684+
skipGit: true,
685+
});
686+
687+
expect(await readFile(path, "utf8")).toBe("# mine\n");
688+
expect(events).toContainEqual({
689+
type: "warning",
690+
message: `box profile ${path} already exists; left unchanged`,
691+
});
692+
});
693+
694+
test.each([
695+
["a template with no box profile", AGENT_TYPESCRIPT_STRANDS],
696+
["an empty project", undefined],
697+
])("writes no box profile for %s", async (_label, runtime) => {
698+
const directory = await inTempDirectory();
699+
await runCreate(manager().manager, {
700+
name: "example",
701+
...(runtime && { scaffoldRuntimeInput: runtime }),
702+
skipInstall: true,
703+
skipGit: true,
704+
} as CreateProjectInput);
705+
706+
expect(existsSync(join(directory, "example", ".box"))).toBe(false);
707+
});
708+
});
709+
654710
describe("FsProjectManager.addResource", () => {
655711
test("applies a runtime template profile and defers its dependency setup", async () => {
656712
await inTempDirectory();

‎src/core/project/manager.tsx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync } from "node:fs";
2-
import { readFile, rm, writeFile } from "node:fs/promises";
2+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
33
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
44
import semver from "semver";
55
import type {
@@ -28,6 +28,7 @@ import type {
2828
RemoveResourceInput,
2929
RemoveResourceResult,
3030
RemoveResourcesResult,
31+
ScaffoldRuntimeInput,
3132
} from "../../handlers/project/types";
3233
import { RESOURCE_LABELS } from "../../handlers/project/types";
3334
import type { Logger } from "../../logging";
@@ -51,6 +52,7 @@ import {
5152
validateHarnessTemplateSource,
5253
} from "./templates/harness";
5354
import { createProjectTree } from "./templates/project";
55+
import { boxProfilePath, renderBoxProfile } from "./templates/boxProfile";
5456
import { getRuntimeTemplateResolver } from "./templates/runtime";
5557
import {
5658
EXPORT_NOTES_FILENAME,
@@ -555,6 +557,10 @@ export class FsProjectManager implements ProjectManager {
555557
yield* this.ensureLockFileExists(appDir);
556558
}
557559

560+
if (scaffoldRuntimeInput) {
561+
yield* this.writeBoxProfile(destination, input.name, scaffoldRuntimeInput);
562+
}
563+
558564
if (!input.skipGit) {
559565
yield { type: "step", message: "initializing git repository" };
560566
yield* this.run(["git", "init"], destination);
@@ -1864,6 +1870,28 @@ export class FsProjectManager implements ProjectManager {
18641870
}
18651871
}
18661872

1873+
/**
1874+
* Writes the boxy profile that runs the scaffolded agent into the project's
1875+
* `.box` folder, named after the project. An existing profile is the
1876+
* user's, so it is never overwritten.
1877+
*/
1878+
private async *writeBoxProfile(
1879+
projectRoot: string,
1880+
projectName: string,
1881+
runtime: ScaffoldRuntimeInput,
1882+
): AsyncGenerator<ProjectEvent, void> {
1883+
const text = renderBoxProfile(projectName, runtime);
1884+
if (text === undefined) return;
1885+
const path = boxProfilePath(projectRoot);
1886+
if (existsSync(path)) {
1887+
yield { type: "warning", message: `box profile ${path} already exists; left unchanged` };
1888+
return;
1889+
}
1890+
yield { type: "step", message: `writing box profile ${path}` };
1891+
await mkdir(dirname(path), { recursive: true });
1892+
await writeFile(path, text, { flag: "wx" });
1893+
}
1894+
18671895
/**
18681896
* Installs dependencies for a scaffolded runtime directory (e.g. `uv sync`
18691897
* for Python). No-ops if the runtime has no recognized dependency manifest.
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { parse } from "smol-toml";
3+
import {
4+
resolveRuntimeTemplateShortcut,
5+
type RuntimeTemplateShortcutName,
6+
} from "../../../handlers/project/shortcuts";
7+
import type { ModelProvider } from "../../../handlers/project/types";
8+
import { hasBoxProfile, renderBoxProfile } from "./boxProfile";
9+
10+
function template(name: RuntimeTemplateShortcutName, overrides: Record<string, string> = {}) {
11+
return resolveRuntimeTemplateShortcut(name, { runtimeName: "agent", ...overrides });
12+
}
13+
14+
function profile(name: RuntimeTemplateShortcutName, overrides: Record<string, string> = {}) {
15+
return parse(renderBoxProfile("V3Demo", template(name, overrides))!) as Record<string, any>;
16+
}
17+
18+
const STRANDS = template("agent-python-strands");
19+
20+
const VENV_GRANTS = {
21+
read: [
22+
"${{ workspace }}/app/agent",
23+
"${{ workspace }}/app/agent/.venv",
24+
{ path: "${{ home }}/.local/share/uv/python", optional: true },
25+
],
26+
exec: [
27+
{ path: "${{ workspace }}/app/agent/.venv", optional: true },
28+
{ path: "${{ home }}/.local/share/uv/python", optional: true },
29+
],
30+
deny_write: [{ path: "${{ workspace }}/app/agent/.venv", optional: true }],
31+
};
32+
33+
const UVICORN = [
34+
"${{ workspace }}/app/agent/.venv/bin/python",
35+
"-m",
36+
"uvicorn",
37+
"main:app",
38+
"--app-dir",
39+
"${{ workspace }}/app/agent",
40+
"--reload",
41+
"--reload-dir",
42+
"${{ workspace }}/app/agent",
43+
"--host",
44+
"127.0.0.1",
45+
"--port",
46+
"8081",
47+
];
48+
49+
describe("renderBoxProfile", () => {
50+
test("runs the Strands agent from the project's venv, reached through ingress", () => {
51+
expect(parse(renderBoxProfile("V3Demo", STRANDS)!) as unknown).toEqual({
52+
packs: ["box/base", "box/bedrock"],
53+
profile: { name: "V3Demo" },
54+
options: { bedrock: { region: "us-west-2" } },
55+
agent: {
56+
command: UVICORN,
57+
env: { LOCAL_DEV: "1", PORT: "8081", PYTHONUNBUFFERED: "1", PYTHONUTF8: "1" },
58+
filesystem: VENV_GRANTS,
59+
ingress: [{ listen: "127.0.0.1:8080", workload_port: 8081 }],
60+
},
61+
egress: { bedrock: { secret: { ref: "aws://default" } } },
62+
});
63+
});
64+
65+
test("names itself after the project, so `box run .box/profile.toml` can load it by its path", () => {
66+
expect(profile("agent-python-minimal").profile).toEqual({ name: "V3Demo" });
67+
expect(renderBoxProfile("V3Demo", STRANDS)).toContain("Run `box run .box/profile.toml`");
68+
});
69+
70+
test("names no host path, so the profile works on any machine and after a move", () => {
71+
const text = renderBoxProfile("V3Demo", STRANDS)!;
72+
expect(text).not.toContain(process.cwd());
73+
expect(text).not.toMatch(/"\/(Users|home)\//);
74+
});
75+
76+
test.each(["agent-python-minimal", "agent-python-langchain"] as const)(
77+
"runs %s with uvicorn on Bedrock, like the Strands agent",
78+
(name) => {
79+
const { packs, agent, egress } = profile(name);
80+
expect(packs).toEqual(["box/base", "box/bedrock"]);
81+
expect(agent.command).toEqual(UVICORN);
82+
expect(agent.filesystem).toEqual(VENV_GRANTS);
83+
expect(egress.bedrock).toEqual({ secret: { ref: "aws://default" } });
84+
},
85+
);
86+
87+
test.each([
88+
["agui-python-strands", "PORT", 8080],
89+
["a2a-python-strands", "A2A_PORT", 9000],
90+
["mcp-python-fastmcp", "FASTMCP_PORT", 8000],
91+
] as const)("runs %s as a module on its own port, one above dev's", (name, variable, devPort) => {
92+
const { agent } = profile(name);
93+
expect(agent.command).toEqual(["${{ workspace }}/app/agent/.venv/bin/python", "-m", "main"]);
94+
expect(agent.env).toMatchObject({
95+
PYTHONPATH: "${{ workspace }}/app/agent",
96+
[variable]: String(devPort + 1),
97+
});
98+
expect(agent.ingress).toEqual([{ listen: `127.0.0.1:${devPort}`, workload_port: devPort + 1 }]);
99+
expect(agent.filesystem).toEqual(VENV_GRANTS);
100+
});
101+
102+
test("gives the MCP server no route, because it calls no model", () => {
103+
const mcp = profile("mcp-python-fastmcp");
104+
expect(mcp.packs).toEqual(["box/base"]);
105+
expect(mcp.options).toBeUndefined();
106+
expect(mcp.egress).toBeUndefined();
107+
});
108+
109+
test.each([
110+
[
111+
"OpenAI",
112+
{},
113+
"openai",
114+
{
115+
destinations: ["api.openai.com"],
116+
secret: { ref: "env://AGENTCORE_CREDENTIAL_AGENTOPENAIAPIKEY" },
117+
},
118+
],
119+
[
120+
"Anthropic",
121+
{},
122+
"anthropic",
123+
{
124+
destinations: ["api.anthropic.com"],
125+
secret: {
126+
ref: "env://AGENTCORE_CREDENTIAL_AGENTANTHROPICAPIKEY",
127+
header: "x-api-key",
128+
phantom_prefix: "sk-ant-",
129+
},
130+
},
131+
],
132+
[
133+
"Gemini",
134+
{},
135+
"gemini",
136+
{
137+
destinations: ["generativelanguage.googleapis.com"],
138+
secret: { ref: "env://AGENTCORE_CREDENTIAL_AGENTGEMINIAPIKEY", header: "x-goog-api-key" },
139+
},
140+
],
141+
[
142+
"OpenAICompatible",
143+
{ apiBase: "https://models.example.com/v1", modelId: "my-model" },
144+
"openai-compatible",
145+
{
146+
destinations: ["models.example.com"],
147+
secret: { ref: "env://AGENTCORE_CREDENTIAL_AGENTOPENAICOMPATIBLEAPIKEY" },
148+
},
149+
],
150+
[
151+
"LiteLLM",
152+
{ modelId: "openai/gpt-4o" },
153+
"openai",
154+
{
155+
destinations: ["api.openai.com"],
156+
secret: { ref: "env://AGENTCORE_CREDENTIAL_AGENTLITELLMAPIKEY" },
157+
},
158+
],
159+
] as const)(
160+
"routes %s model calls with the API key from the environment, and not through Bedrock",
161+
(provider, overrides, route, expected) => {
162+
const { packs, options, egress } = profile("agent-python-strands", {
163+
modelProvider: provider as ModelProvider,
164+
apiKey: "sk-test",
165+
...overrides,
166+
});
167+
expect(packs).toEqual(["box/base"]);
168+
expect(options).toBeUndefined();
169+
expect(egress).toEqual({ [route]: expected });
170+
},
171+
);
172+
173+
test("routes a LiteLLM agent on a bedrock/ model through Bedrock", () => {
174+
const { packs, egress } = profile("agent-python-strands", { modelProvider: "LiteLLM" });
175+
expect(packs).toEqual(["box/base", "box/bedrock"]);
176+
expect(egress).toEqual({ bedrock: { secret: { ref: "aws://default" } } });
177+
});
178+
179+
test("runs the Strands container image's agent from /app, where box starts in the image", () => {
180+
const container = profile("agent-python-strands-container");
181+
expect(container.packs).toEqual(["box/base", "box/bedrock"]);
182+
expect(container.agent).toEqual({
183+
command: [
184+
"${{ workspace }}/.venv/bin/python",
185+
"-m",
186+
"uvicorn",
187+
"main:app",
188+
"--app-dir",
189+
"${{ workspace }}",
190+
"--reload",
191+
"--reload-dir",
192+
"${{ workspace }}",
193+
"--host",
194+
"127.0.0.1",
195+
"--port",
196+
"8081",
197+
],
198+
env: { LOCAL_DEV: "1", PORT: "8081", PYTHONUNBUFFERED: "1", PYTHONUTF8: "1" },
199+
filesystem: {
200+
read: ["${{ workspace }}", "/usr/local/lib", "/usr/lib/aarch64-linux-gnu"],
201+
exec: [
202+
{ path: "${{ workspace }}/.venv", optional: true },
203+
"/usr/local/bin",
204+
"/usr/local/lib",
205+
"/usr/lib/aarch64-linux-gnu",
206+
],
207+
deny_write: [{ path: "${{ workspace }}/.venv", optional: true }],
208+
},
209+
ingress: [{ listen: "0.0.0.0:8080", workload_port: 8081 }],
210+
});
211+
expect(container.egress.bedrock).toEqual({ secret: { ref: "aws://default" } });
212+
});
213+
214+
test.each([
215+
"environment-python-bma",
216+
"agent-typescript-strands",
217+
"agent-typescript-vercel",
218+
] as const)("writes no profile for %s yet", (name) => {
219+
const runtime = resolveRuntimeTemplateShortcut(name);
220+
expect(hasBoxProfile(runtime)).toBe(false);
221+
expect(renderBoxProfile("V3Demo", runtime)).toBeUndefined();
222+
});
223+
});

0 commit comments

Comments
 (0)