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
11 changes: 6 additions & 5 deletions src/assets/templates/box-codezip/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
# zip into a folder this user owns (box runs only a profile its user owns), exports the Python and
# code paths that only the microVM knows, and hands over to `box run`.
#
# The box profile reads the deployed resources, the gateway URL, the Python interpreter and the code
# directory from the environment through the @box/agentcore and @box/agentcore-codezip packs, and
# signs AWS routes with the execution role through a bare `aws://`. So there is nothing to fill in,
# no credentials to write, and no start.sh.
# The box profile reads the deployed resources, the gateway URL, the Python interpreter, its
# standard library and the code directory from the environment through the @box/agentcore and
# @box/agentcore-codezip packs, and signs AWS routes with the execution role through a bare
# `aws://`. So there is nothing to fill in, no credentials to write, and no start.sh.
import json
import os
import re
import shutil
import sys
import sysconfig

try:
import tomllib
Expand Down Expand Up @@ -91,7 +92,7 @@ def main():
"HOME": HOME,
"BOX_CODE": CODE,
"BOX_PYTHON": box_python(sys.executable, sys.version_info),
"BOX_PYTHON_PREFIX": sys.base_prefix,
"BOX_PYTHON_STDLIB": sysconfig.get_path("stdlib"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Non-blocking: this is right for the measured PYTHON_3_14 layout (/usr/lib/python3.14). On a lib64-layout Python (Fedora/RHEL style), stdlib becomes /usr/lib64/python3.X, which sits inside the /usr/lib64 loader-folder read grant and also lands in exec. Has a Runtime version other than 3.14 been probed, or do we know Box Core accepts exec on a subfolder of a loader folder? If not, a sentence in the PR body or a comment would set expectations.

},
)

Expand Down
46 changes: 45 additions & 1 deletion src/core/project/backends/boxStartPy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, test } from "bun:test";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { serializeRuntimeProfile } from "../runtimeProfile/serialize";
Expand Down Expand Up @@ -92,3 +92,47 @@ describe.skipIf(PYTHON === undefined)("start.py with no tomllib, as on Python 3.
expect(result.stderr).toContain("has a `policies` line start.py cannot read");
});
});

/**
* Runs start.py's main with its folders in a test folder and os.execve replaced, and prints what main
* hands box: the BOX_ variables, and the standard library and base prefix of this Python.
*/
const MAIN_HARNESS = `
import json, os, sys, sysconfig, types
start = types.ModuleType("start")
start.__file__ = sys.argv[1]
exec(compile(open(sys.argv[1]).read(), "start.py", "exec"), start.__dict__)
folder = sys.argv[2]
start.DIST = os.path.join(folder, "dist")
start.BOX = os.path.join(start.DIST, "box")
start.HOME = os.path.join(folder, "home")
start.WORKSPACE = os.path.join(folder, "workspace")
start.PROFILE_DIR = os.path.join(folder, "profile")
start.PROFILE = os.path.join(start.PROFILE_DIR, "profile.toml")
def execve(path, args, env):
box = {name: value for name, value in env.items() if name.startswith("BOX_")}
print(json.dumps({"box": box, "stdlib": sysconfig.get_path("stdlib"), "prefix": sys.base_prefix}))
sys.exit(0)
os.execve = execve
start.main()
`;

describe.skipIf(PYTHON === undefined)("start.py", () => {
test("gives box the standard library of the Runtime's Python, and not its base prefix, which box refuses as a system root (/usr on the Runtime)", async () => {
const folder = await mkdtemp(join(tmpdir(), "box-start-py-"));
folders.push(folder);
await mkdir(join(folder, "dist"));
await writeFile(join(folder, "dist", "profile.toml"), runtimeProfile());

const result = Bun.spawnSync([PYTHON!, "-c", MAIN_HARNESS, START_PY, folder]);

expect(result.stderr.toString()).toBe("");
const { box, stdlib, prefix } = JSON.parse(result.stdout.toString()) as {
box: Record<string, string>;
stdlib: string;
prefix: string;
};
expect(box.BOX_PYTHON_STDLIB).toBe(stdlib);
expect(Object.values(box)).not.toContain(prefix);
});
});
Loading
Loading