Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,21 @@ each runtime from its local profile. box comes from the public GitHub release `v
is needed to download it. Use that release's build for your machine for `agentcore dev` and the
profile checks of `build`: an older box cannot read the profiles that this CLI writes.

`src/core/project/backends/boxRelease.json` pins the Linux build of box that ships to AgentCore
Runtime, with its repository, version and SHA-256. It also pins the macOS arm64 build, which the e2e
workflow installs on its macOS runner. `bun scripts/bump-box-release.ts --box <version>`
(with `--box-repo` to move repository) rewrites the file from the release's `SHA256SUMS.txt`.
`src/core/project/backends/boxRelease.json` pins the box releases this CLI supports: `box`, the
default, and `supported`, the others. For each it pins the Linux build that ships to AgentCore
Runtime and the macOS arm64 build that the e2e workflow installs, with their repository, version and
SHA-256. `bun scripts/bump-box-release.ts --box <version>` makes a release the default (add
`--keep-previous` to keep the old one supported), `--add <version>` and `--remove <version>` change
the other supported releases, and `--box-repo` moves repository. The script reads each release's
`SHA256SUMS.txt`, and the checksums reach the CLI only through a reviewed change of the file.

How each command uses it:

- A CodeZip agent's zip carries box. `build` and `deploy` download box once with `curl` into
`~/.agentcore/box/`, check it against its pinned SHA-256, and stage box and its packs into the
runtime's `.box-dist/`.
- A container agent's image downloads box while it builds. `dev`, `build` and `deploy` stage the
release that `box.version` names, with its checksum, as `box-release.env` beside the Dockerfile
release that `box.version` names, with its pinned checksum, as `box-release.env` beside the Dockerfile
(`.box-dist/`, or `.box-dev/` under `dev`), and the Dockerfile downloads that release with `curl`,
checks its SHA-256 and unpacks it into `/opt/box`. The CLI downloads nothing for a container, so
the build machine, or AWS CodeBuild on `deploy`, needs network to github.com. `agentcore dev`
Expand All @@ -235,8 +238,10 @@ How each command uses it:
with that instruction when `box` is not on your PATH. Container agents need no local box.

Each box runtime names its release in `agentcore.json` with `"box": { "version": "<version>" }`,
which `agentcore create` writes. Every box runtime runs the pinned version, whose checksums
`boxRelease.json` keeps: build and deploy set `box.version` to it, and say so when it named another.
which `agentcore create` writes with the default. To move a runtime to another supported release,
change its `box.version`; with no version, it runs the default. Build, deploy and container `dev`
stop on a version that `boxRelease.json` does not list, and name the supported ones, before they
write anything. Build never rewrites `box.version`.

On AgentCore Runtime, box signs the agent's Bedrock and Memory calls with the execution role: the
profile's bare `aws://` resolves the AWS default credential chain, which box reads and refreshes
Expand Down
16 changes: 9 additions & 7 deletions docs/box-local-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ gateway, bound to it by `boundRuntime` and named after it: the runtime `agent` g

A Box project ships box inside its deploy artifact, so the agent runs in a box on AgentCore Runtime.
Each box runtime names its box release in `agentcore.json` with `"box": { "version": "<version>" }`,
which `agentcore create` writes and build keeps at the pinned release. `agentcore build` and
`agentcore deploy` download the pinned release's Linux build from
which `agentcore create` writes with the default release. `agentcore build` and `agentcore deploy`
download the Linux build of the release it names from
[mquang-amazon/box-releases](https://github.com/mquang-amazon/box-releases) once into
`~/.agentcore/box/`, check its SHA-256s, and stage it into the zip, for a CodeZip runtime. For a
container they write the release and its checksum into `.box-dist/box-release.env` instead. A
Expand All @@ -29,13 +29,15 @@ only for the box templates. The runtime's entry point and Dockerfile never decid
`start.py` runs the usual way. A runtime with `box.version` in a project without
`"projectType": "box"` stops dev with `agentcore.json has no "projectType": "box"`.

Every box runtime runs the box version the CLI pins in `src/core/project/backends/boxRelease.json`.
Build and deploy set each runtime's `box.version` to it, and say so when it named another; container
`dev` stages the pinned box whatever `box.version` says. To try an unreleased box build, publish it
as a GitHub pre-release with the release's file names (the `aarch64-apple-darwin` and
A runtime's `box.version` must be one of the releases that
`src/core/project/backends/boxRelease.json` supports: `box`, the default, or one of `supported`.
With no version, the runtime runs the default. Build, deploy and container `dev` stop on any other
version and name the supported ones; build never rewrites the field. To try an unreleased box build,
publish it as a GitHub pre-release with the release's file names (the `aarch64-apple-darwin` and
`aarch64-unknown-linux-gnu` archives and `SHA256SUMS.txt`), for example in a fork of
[mquang-amazon/box-releases](https://github.com/mquang-amazon/box-releases), then run
`bun scripts/bump-box-release.ts --box <version> --box-repo <owner/repo>` in your CLI checkout.
`bun scripts/bump-box-release.ts --add <version> --box-repo <owner/repo>` in your CLI checkout and
set the runtime's `box.version` to it, or `--box <version>` to make it the default.

## Prerequisites

Expand Down
78 changes: 59 additions & 19 deletions scripts/bump-box-release.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#!/usr/bin/env bun

/**
Pins box to a published GitHub release, in src/core/project/backends/boxRelease.json: the Linux
arm64 build that ships to AgentCore Runtime, and the macOS arm64 build that the e2e workflow
installs on its macOS runner. It reads the release's SHA256SUMS.txt, so the file changes only to a
version whose archives are published. It imports nothing from src/.
Pins the box releases this CLI supports, in src/core/project/backends/boxRelease.json: for each,
the Linux arm64 build that ships to AgentCore Runtime and the macOS arm64 build that the e2e
workflow installs on its macOS runner. `box` is the default release, which `agentcore create`
writes; `supported` lists the other releases a runtime's box.version can name. It reads each
release's SHA256SUMS.txt, so the file changes only to versions whose archives are published, and
the checksums it pins reach the CLI only through a reviewed change of the file. It imports nothing
from src/.

bun scripts/bump-box-release.ts --box <version> [--box-repo <owner/repo>]
bun scripts/bump-box-release.ts --box <version> [--keep-previous] [--box-repo <owner/repo>]
bun scripts/bump-box-release.ts --add <version> [--box-repo <owner/repo>]
bun scripts/bump-box-release.ts --remove <version>

--box makes <version> the default; --keep-previous keeps the old default as a supported release.
--add supports <version> beside the default, and --remove stops supporting it.
**/

import { join, resolve } from "node:path";
Expand All @@ -27,11 +35,14 @@ const { values } = parseArgs({
args: Bun.argv.slice(2),
options: {
box: { type: "string" },
add: { type: "string" },
remove: { type: "string" },
"keep-previous": { type: "boolean" },
"box-repo": { type: "string" },
},
});
if (!values.box) {
console.error("Name a version: --box <version>.");
if ([values.box, values.add, values.remove].filter(Boolean).length !== 1) {
console.error("Name one change: --box <version>, --add <version>, or --remove <version>.");
process.exit(2);
}

Expand All @@ -48,18 +59,47 @@ async function checksums(repo: string, version: string): Promise<Map<string, str
return sums;
}

const pinned = await Bun.file(FILE).json();
type Release = {
repo: string;
version: string;
linux: { target: string; asset: string; sha256: string };
darwin: { target: string; asset: string; sha256: string };
};

/** `version` of `repo`, with the pinned archive of each target, which the release must list. */
async function release(repo: string, version: string): Promise<Release> {
const sums = await checksums(repo, version);
const pin = (target: string) => {
const asset = `box-v${version}-${target}.tar.gz`;
const sha256 = sums.get(asset);
if (!sha256) throw new Error(`${repo} v${version} lists no ${asset}`);
return { target, asset, sha256 };
};
return { repo, version, linux: pin(LINUX_TARGET), darwin: pin(DARWIN_TARGET) };
}

const pinned = (await Bun.file(FILE).json()) as { box: Release; supported?: Release[] };
const repo = values["box-repo"] ?? pinned.box.repo;
const version = values.box;
const sums = await checksums(repo, version);
/** The pinned archive of `target`, which the release must list. */
function pin(target: string): { target: string; asset: string; sha256: string } {
const asset = `box-v${version}-${target}.tar.gz`;
const sha256 = sums.get(asset);
if (!sha256) throw new Error(`${repo} v${version} lists no ${asset}`);
return { target, asset, sha256 };
let box = pinned.box;
let supported = (pinned.supported ?? []).filter(({ version }) => version !== box.version);
if (values.box) {
const next = await release(repo, values.box);
console.log(`box: default ${box.repo} ${box.version} → ${next.repo} ${next.version}`);
if (values["keep-previous"]) supported = [box, ...supported];
box = next;
} else if (values.add) {
if (values.add === box.version) throw new Error(`${values.add} is the default already`);
console.log(`box: supporting ${repo} ${values.add} beside the default ${box.version}`);
supported = [
...supported.filter(({ version }) => version !== values.add),
await release(repo, values.add),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI vs #2893: release() hard-codes box-v${version}-${target}.tar.gz and builds darwin without the optional version that #2893 adds (its assets are box-cli-v...). Whichever PR lands second needs to reconcile this, or --add 0.3.x will throw "lists no box-v...". Also, --keep-previous is silently ignored with --add/--remove. You could reject that combination.

];
} else if (values.remove) {
if (values.remove === box.version)
throw new Error(`${values.remove} is the default: move --box first`);
console.log(`box: no longer supporting ${values.remove}`);
supported = supported.filter(({ version }) => version !== values.remove);
}
console.log(`box: ${pinned.box.repo} ${pinned.box.version} → ${repo} ${version}`);
const box = { repo, version, linux: pin(LINUX_TARGET), darwin: pin(DARWIN_TARGET) };
supported = supported.filter(({ version }) => version !== box.version);

await Bun.write(FILE, `${JSON.stringify({ box }, null, 2)}\n`);
await Bun.write(FILE, `${JSON.stringify({ box, supported }, null, 2)}\n`);
20 changes: 13 additions & 7 deletions src/core/dev/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
type StreamProcessOptions,
} from "../../io";
import type { ProjectRuntime } from "../../projectSchemas/runtime";
import { BOX_RELEASE } from "../project/backends/boxRelease";
import { BOX_RELEASE, type BoxRelease } from "../project/backends/boxRelease";
import { OTHER_BOX_RELEASE, TEST_BOX_RELEASES } from "../project/backends/box-release-test-support";
import type { ResolveAwsCredentials } from "./boxAwsCredentials";
import { ContainerDevRunner } from "./container";

Expand Down Expand Up @@ -76,6 +77,7 @@ function harness(
processEnv?: NodeJS.ProcessEnv;
resolveAwsCredentials?: ResolveAwsCredentials;
platform?: NodeJS.Platform;
releases?: readonly BoxRelease[];
} = {},
) {
const calls: ProcessCall[] = [];
Expand Down Expand Up @@ -125,6 +127,7 @@ function harness(
// Never the real chain in a test: none unless the test gives some.
resolveAwsCredentials: config.resolveAwsCredentials ?? (async () => undefined),
...(config.platform && { platform: config.platform }),
...(config.releases && { releases: config.releases }),
}),
};
}
Expand Down Expand Up @@ -812,7 +815,9 @@ describe("ContainerDevRunner", () => {
box?: boolean;
} = {},
) {
const projectRuntime = runtime(options.box === false ? {} : { box: { version: "1.0.0" } });
const projectRuntime = runtime(
options.box === false ? {} : { box: { version: BOX_RELEASE.version } },
);
const root = await projectRoot(projectRuntime);
const context = join(root, projectRuntime.codeLocation);
await writeFile(join(context, "Dockerfile"), options.dockerfile ?? BOX_DOCKERFILE);
Expand Down Expand Up @@ -1074,15 +1079,16 @@ describe("ContainerDevRunner", () => {
expect(JSON.stringify(events)).not.toContain("reads GITHUB_TOKEN for [egress.github]");
});

test("stages the pinned box, whatever box.version the runtime names", async () => {
test("stages the box release that the runtime's box.version names", async () => {
const { projectRuntime, root, context } = await boxedScene();
const { runner } = harness({ processEnv: credentials });
const { runner } = harness({ processEnv: credentials, releases: TEST_BOX_RELEASES });

await collect(runner.run(input(root, { ...projectRuntime, box: { version: "2.0.0" } })));
await collect(
runner.run(input(root, { ...projectRuntime, box: { version: OTHER_BOX_RELEASE.version } })),
);

const env = await readFile(join(context, ".box-dist", "box-release.env"), "utf8");
expect(env).toContain(BOX_RELEASE.version);
expect(env).not.toContain("2.0.0");
expect(env).toContain(`BOX_VERSION=${OTHER_BOX_RELEASE.version}`);
});

test("runs a runtime with no box.version without box's flags, even with a box profile", async () => {
Expand Down
6 changes: 6 additions & 0 deletions src/core/dev/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
imageTakesBoxDistArg,
stageBoxImage,
} from "../project/backends/boxContainer";
import { BOX_RELEASES, boxReleaseOfRuntime, type BoxRelease } from "../project/backends/boxRelease";
import { boxPolicyFileCopies } from "../project/boxNetworkSource";
import {
boxLocalProfileFile,
Expand Down Expand Up @@ -118,6 +119,8 @@ type ContainerDevRunnerConfig = {
resolveAwsCredentials?: ResolveAwsCredentials;
/** The platform, whose rules for variable names apply to `processEnv`; tests inject one. */
platform?: NodeJS.Platform;
/** The box releases a runtime's box.version can name; boxRelease.json's by default. */
releases?: readonly BoxRelease[];
};

export class ContainerDevRunner implements DevRunner {
Expand All @@ -127,6 +130,7 @@ export class ContainerDevRunner implements DevRunner {
private readonly processEnv: NodeJS.ProcessEnv;
private readonly resolveAwsCredentials: ResolveAwsCredentials;
private readonly platform: NodeJS.Platform;
private readonly releases: readonly BoxRelease[];

constructor(config: ContainerDevRunnerConfig = {}) {
this.streamProcess = config.streamProcess ?? streamProcess;
Expand All @@ -135,6 +139,7 @@ export class ContainerDevRunner implements DevRunner {
this.processEnv = config.processEnv ?? process.env;
this.resolveAwsCredentials = config.resolveAwsCredentials ?? resolveDefaultAwsCredentials;
this.platform = config.platform ?? process.platform;
this.releases = config.releases ?? BOX_RELEASES;
}

public async *run(input: DevServerInput): AsyncGenerator<DevEvent, void> {
Expand Down Expand Up @@ -390,6 +395,7 @@ export class ContainerDevRunner implements DevRunner {
context,
profileText,
boxPolicyFileCopies(input.projectRoot, input.runtime.name),
boxReleaseOfRuntime(input.runtime.name, input.runtime.box?.version, this.releases),
folder,
);
return { folder, profileText };
Expand Down
16 changes: 15 additions & 1 deletion src/core/project/backends/box-release-test-support.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { BOX_BINARIES, BOX_RELEASE, boxCachePath } from "./boxRelease";
import { BOX_BINARIES, BOX_RELEASE, boxCachePath, type BoxRelease } from "./boxRelease";

/**
* Fills the cache under `home` with a stand-in of the pinned box release, so build, deploy and
Expand All @@ -13,3 +13,17 @@ export async function seedBoxCache(home: string): Promise<void> {
for (const binary of BOX_BINARIES) await writeFile(join(root, binary), `#!${binary}\n`);
await writeFile(join(root, "release.json"), JSON.stringify({ box_version: BOX_RELEASE.version }));
}

/** A second supported box release, for tests of a runtime whose box.version is not the default. */
export const OTHER_BOX_RELEASE: BoxRelease = {
...BOX_RELEASE,
version: "9.9.9-test.1",
linux: {
...BOX_RELEASE.linux,
asset: "box-v9.9.9-test.1-aarch64-unknown-linux-gnu.tar.gz",
sha256: "1".repeat(64),
},
};

/** The supported releases of those tests: the default, then {@link OTHER_BOX_RELEASE}. */
export const TEST_BOX_RELEASES: readonly BoxRelease[] = [BOX_RELEASE, OTHER_BOX_RELEASE];
41 changes: 30 additions & 11 deletions src/core/project/backends/boxCodeZip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
import { serializeRuntimeProfile } from "../runtimeProfile/serialize";
import type { TomlTable } from "../runtimeProfile/types";
import { BOX_DIST_GITIGNORE, stageBoxCodeZip } from "./boxCodeZip";
import type { BoxLinuxBuild } from "./boxRelease";
import { BOX_RELEASE, type BoxLinuxBuild, type BoxRelease } from "./boxRelease";
import { OTHER_BOX_RELEASE, TEST_BOX_RELEASES } from "./box-release-test-support";

const directories: string[] = [];

Expand Down Expand Up @@ -66,7 +67,7 @@ async function project(
runtimeVersion,
modelProvider: "Bedrock",
// create writes box.version only for a box template, whose entry point is start.py.
...(entrypoint === "start.py" && { box: { version: "1.0.0" } }),
...(entrypoint === "start.py" && { box: { version: BOX_RELEASE.version } }),
})),
memories: [],
projectType: "box",
Expand Down Expand Up @@ -120,18 +121,36 @@ describe("stageBoxCodeZip", () => {
expect(await readFile(join(dist, ".gitignore"), "utf8")).toBe(BOX_DIST_GITIGNORE);
});

test("asks for the pinned box once, for every runtime", async () => {
test("asks once for each supported release the runtimes name, the default for no version", async () => {
const build = await linuxBuild();
const demo = await project("start.py", "PYTHON_3_14", ["agent", "second"]);
demo.spec.runtimes[1]!.box = { version: "2.0.0" };
let asked = 0;
const demo = await project("start.py", "PYTHON_3_14", ["agent", "second", "third"]);
demo.spec.runtimes[1]!.box = { version: OTHER_BOX_RELEASE.version };
demo.spec.runtimes[2]!.box = {};
const asked: string[] = [];

await stageBoxCodeZip(demo, [model([]), model([], "second")], async () => {
asked++;
return build;
});
await stageBoxCodeZip(
demo,
[model([]), model([], "second"), model([], "third")],
async (release: BoxRelease) => {
asked.push(release.version);
return build;
},
TEST_BOX_RELEASES,
);

expect(asked).toBe(1);
expect(asked).toEqual([BOX_RELEASE.version, OTHER_BOX_RELEASE.version]);
});

test("refuses a box.version this CLI does not support, and names the supported ones", async () => {
const demo = await project("start.py");
demo.spec.runtimes[0]!.box = { version: "0.0.1" };

await expect(
stageBoxCodeZip(demo, [model([])], async () => linuxBuild(), TEST_BOX_RELEASES),
).rejects.toThrow(
'runtime "agent" names box 0.0.1 under box.version in agentcore.json, but this CLI ' +
`supports box ${BOX_RELEASE.version}, ${OTHER_BOX_RELEASE.version}.`,
);
});

test.each([
Expand Down
Loading
Loading