Repository navigation
fix(box): Grant the standard library of the Runtime Python on CodeZip, not its prefix, so box starts on Runtime V2 - #2903
Conversation
leo237
left a comment
There was a problem hiding this comment.
The fix looks right: granting sysconfig.get_path("stdlib") in place of sys.base_prefix gets rid of the /usr system-root refusal on V2. The live deploy and invoke you ran back that up. The pack keeps ABSOLUTE_PATH_PATTERN on the new input, so an empty value or / still can't widen the grant. And nothing in src/ still refers to inputs.python.prefix or BOX_PYTHON_PREFIX.
I checked out a08503c and ran the checks locally. bun run typecheck passes. bun test gives 6108 pass, 7 skip, 0 fail, so the boxProfileResolver failure from the PR description didn't happen here. The new start.py test and the CodeZip template test both run (the start.py test isn't skipped, since python3 is on PATH).
Comments, none blocking:
- Stale start.py in existing projects (inline on the pack input). This one is worth addressing, or at least tracking. It's the biggest behavior risk in the PR.
- lib-dynload is assumed to be under
stdlib(inline on start.py). It's true on the V2 image you measured, but CPython puts lib-dynload underplatstdlib, and nothing checks that the two are the same. - V1 and other Python versions. The narrowing was only tested live on V2 / PYTHON_3_14. On V1 (
/opt/aws/agentcore-runtime/python/versions/3.14.2), the old prefix grant also covered<prefix>/lib, where a shared-build Python keepslibpython3.X.so(Py_ENABLE_SHARED=1,LIBDIR=<prefix>/lib). The new grant drops that folder. That's only safe if V1's Python is static, or if #2879 (V2 pin) merges before or with this PR. Can you confirm which one holds? Also, were PYTHON_3_10 to 3_13 on V2 checked to have the same/usr/lib/python3.Xlayout? The template test hard-codes the 3.14 values. - nits on naming and the test fixtures (inline).
The squash plan doesn't cover this fix, so none of these are deferred to a later phase.
a08503c to
85e87ca
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## box-integration #2903 +/- ##
================================================
Coverage 96.85% 96.85%
================================================
Files 773 773
Lines 68326 68326
================================================
+ Hits 66177 66178 +1
+ Misses 2149 2148 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
leo237
left a comment
There was a problem hiding this comment.
Re-review at 85e87ca. Since my review at a08503c, the branch was rebased onto the current box-integration. The PR's own changes are line for line the same as before: I compared the changed lines of both versions. There are no replies on the threads yet, so the status below is the same as last time. None of it blocks.
I checked out 85e87ca and ran the checks locally. bun run typecheck passes. bun test gives 6333 pass, 7 skip, 0 fail.
Status of my earlier comments:
- Stale start.py in existing projects (boxProfile.ts): not fixed. One thing is new on the base. Since 3d7a347, an old Box project fails schema validation with a message that says only to set
"platformVersion": "V2". A user who follows it then deploys with the old start.py and hits the Runtime crash. Details are in the thread. - lib-dynload under
platstdlib, notstdlib(start.py:95): not fixed. - Question: V1, and PYTHON_3_10 to 3_13 on V2: the V1 part no longer matters. Since 3d7a347 on the base, every Box runtime must set
platformVersionto V2. The question about 3.10 to 3.13 on V2 is still open.e2eTest/project/templates.test.ts:116-118(from MQ-7) already deploys the CodeZip box template on each of those Pythons. One run of it on this branch would answer the question, because the stdlib path on V2 differs for each version. - nit: the doc of
runtimePythonStdlibon Container (context.ts): not fixed. - nit: the dead
inputs.python.prefixentry, and no source noted for the copied box lists (additions.test.ts): not fixed.
No new findings, because there is no new code.
…, not its prefix
A CodeZip box runtime does not start on AgentCore Runtime. box stops with this error:
`[agent] filesystem entry "/usr" is refused: ... a system root is never a grantable tree`
The Runtime profile moves the read grant and the exec grant of the uv Pythons of the local profile
to `${{ inputs.python.prefix }}`. start.py sets this input to `sys.base_prefix`. The Python of a
PYTHON_3_14 Runtime is `/usr/bin/python3.14`. Thus the value is `/usr`. Box Core refuses a grant of
`/usr`, and each Box Core release refuses it. Thus box did not start on a CodeZip Runtime before
this change. Before
[4700b62](4700b62),
start.sh started the agent with no box when box did not start. That commit and
[#2853](#2853) removed this fallback. At
this time, the Runtime stops the runtime, and the deploy stops with an error. The branch
box-integration-development has the same problem.
With this change, the uv Pythons of the local profile go to the standard library of the Runtime
Python, `${{ inputs.python.stdlib }}`, and not to its prefix.
- start.py exports `BOX_PYTHON_STDLIB`, the value of `sysconfig.get_path("stdlib")`, in place of
`BOX_PYTHON_PREFIX`. On a PYTHON_3_14 Runtime, the value is `/usr/lib/python3.14`.
- The `@box/agentcore-codezip` pack has the input `python.stdlib` in place of `python.prefix`, with
the same pattern.
- The paths rule moves `${{ home }}/.local/share/uv/python` to `${{ inputs.python.stdlib }}`.
After this change, the Runtime profile of each CodeZip template has these grants:
- `read = [code folder, standard library, "/usr/lib64", "/lib64"]`. The Python reads its modules.
`/usr/lib64` contains the libraries that its native modules use, for example `libssl.so.3`.
- `exec = [code folder, standard library]`. box mounts a folder that is not a loader folder with
no exec. Thus the Python can use the native modules in `lib-dynload` only with this grant.
- The Python itself is the command of the agent, and box gives it exec.
A read grant of the loader folder `/usr/lib` also lets the Python start. This change does not use
it, because it gives read and exec on all of `/usr/lib`, and not only on the standard library.
A CodeZip box runtime that `agentcore create` made before this change has a start.py that exports
the prefix. Copy the start.py of this change into its code folder.
- `start.py > gives box the standard library of the Runtime's Python, and not its base prefix`
(`boxStartPy.test.ts`) starts `main()` of start.py, and replaces `os.execve` with a function that
records the environment. It makes sure that `BOX_PYTHON_STDLIB` is the standard library, and that
no value is the prefix.
- `the Runtime profile of each CodeZip box template of create` (`additions.test.ts`) derives the
Runtime profile of the six CodeZip box templates. It makes sure that the read grant and the exec
grant of the standard library are in the profile. It also makes sure that no grant has a value
on the Runtime that Box Core refuses: a system root in a list, or a loader folder in a list that
is not `read`.
- The pack test makes sure that the pack has the input `python.stdlib`. The paths test and the
test of the recorded profiles in `old-renderer-tables.json` use the standard library. In that
file, only these 204 values change.
- On the code before this change, 115 of the new and changed tests show an error.
- Five mutations of the code, and the errors that each mutation causes:
- start.py exports the prefix again: the start.py test shows an error.
- The paths rule moves the uv Pythons to the prefix again: 111 tests show an error.
- The pack has `python.prefix` again: 2 pack tests show an error.
- `runtimeCodeZipRunner` gives no exec grant of the uv Pythons: 110 tests show an error.
- `runtimeCodeZipRunner` also gives a grant of the prefix: the six template tests show an error
at the check of the refused grants.
- `bun test`: `5835 pass`, `6 skip`, `2 fail`. This change does not cause the two errors.
`boxProfileResolver.test.ts` also shows the error on box-integration with the same box
(0.1.11-preview.10) on PATH. `remove/index.test.ts` stops at its 10 s timeout when all tests
operate together. When it operates with no other test, it shows no error.
- `bun run typecheck`, `bun run lint:check`, `bun run format:check`, `bun run build`,
`bun run audit`, and `bun run secrets:check`: no errors.
- A local test in an Amazon Linux 2023 arm64 container, with box 0.1.11-preview.10 and a Python
3.14.3 that has the prefix `/usr` and the standard library `/usr/lib/python3.14`. The Python of
the Runtime has the same paths. The results:
- The zip of box-integration stops with the `/usr` refusal.
- The zip of this change starts the agent, and `/ping` gives 200.
- The zip of box-integration-development stops with the `/usr` refusal.
- A live test on AgentCore Runtime in a dev account. In the test, we deploy a new CodeZip box
project of this branch, with no change. Three invocations get status 200, and the box records
are in the log group `-DEFAULT` of the runtime. The test removes all its resources.
We did not do `bun run test:e2e`. This change has no test on the CodeZip Runtimes PYTHON_3_10 to
PYTHON_3_13, and no test of an `[mcp]` server or a `[tool]` on the Runtime Python.
(cherry picked from commit 490c197)
createdRuntime on box-integration takes the otel flag. The box templates write enableOtel false, so the test passes false.
85e87ca to
855f3b9
Compare
leo237
left a comment
There was a problem hiding this comment.
Approving at 85e87ca. Thanks for the replies. Nothing blocking remains. Box is unreleased, and the live V2 deploy and invoke show the fix works.
On platstdlib == stdlib on V2: I could not check it directly, because no fixture in the repo records platstdlib. The live test points the same way: box reports exec /usr/lib/python3.14, and the agent's TLS calls to Bedrock work, which needs _ssl from lib-dynload. That's good enough for this PR.
Deferred follow-ups (please reply with the backlog ticket IDs so they can be tracked):
start.py: fail early whenplatstdlib!=stdlib, or whenstdlibis not a folder (the lib-dynload guard).context.ts: fix theruntimePythonStdlibdoc for Container, where the value is the image's Python prefix.additions.test.ts: drop the deadinputs.python.prefixentry, and name the box source/version thatBOX_SYSTEM_ROOTS/BOX_LOADER_FOLDERSwere copied from.- Before Box is released: detect a stale
start.py(noBOX_PYTHON_STDLIB) at build time, or at least mention it in the V2 schema message or the release notes. - The Python 3.10–3.13 e2e run is still not confirmed. The PR description says e2e was not run. Please run it, or note why it isn't needed, before Box is released.
leo237
left a comment
There was a problem hiding this comment.
Re-approving after the rebase onto box-integration (now including #2893). I compared the PR's two commits before and after: 855f3b9 is identical to a08503c, and 4a476cd matches a27a77b except for the conflict resolution in one doc comment, which drops the stale "no ffi shim" wording that #2893 removed. The follow-ups listed in my previous approval still apply.
Description
A CodeZip Box agent does not start on AgentCore Runtime V2. Box refuses the Runtime profile, and the Runtime create fails with "The runtime process exited unexpectedly". The Runtime log has this error:
Cause.
c40bee73made the CodeZip Runtime profile grant the base prefix of the Runtime's Python (sys.base_prefix), forreadandexec. Later, #2853 moved the value intostart.py(BOX_PYTHON_PREFIX), and #2866 kept the grant as${{ inputs.python.prefix }}./opt/aws/agentcore-runtime/python/versions/3.14.2. The grant is a narrow folder, and box accepts it./usr/bin/python3.14, so the prefix is/usr. Box refuses a system root as a grant. All box builds from0.1.11-linux-preview.5to0.1.11-preview.10have this rule.The V2 pin (aws/agentcore-l3-cdk-constructs#474 and #2879) does not cause the defect. It only makes the defect appear.
Fix. This is the change of #2882, which was closed unmerged. This PR applies it on the current
box-integration.start.pyexports the standard library of the Runtime's Python (sysconfig.get_path("stdlib")) asBOX_PYTHON_STDLIB, in place ofBOX_PYTHON_PREFIX.@box/agentcore-codezippack reads it asinputs.python.stdlib.${{ inputs.python.stdlib }}forreadandexec.execis necessary, because box cannot map the native modules inlib-dynloadfrom areadgrant. Box adds the interpreter itself as an implicitexec.The second commit changes one test from #2882. On
box-integration, the test helpercreatedRuntimealso takesotel.Effect on existing projects.
createwritesstart.pyinto the project, and build does not write it again. A CodeZip Box project from before this change keeps astart.pythat exports onlyBOX_PYTHON_PREFIX. With this change, box does not getinputs.python.stdlibfor such a project. Such a project needs the newstart.py, or a newcreate.Related Issue
There is no issue. This PR replaces #2882. It works with #2879, which pins Box runtimes to V2.
Documentation PR
Not applicable.
Type of Change
Testing
Live test in
us-east-1. The CLI wasbox-integrationwith #2879 and this change, the L3 wasbox-integrationb21cfbf, and box was0.1.11-preview.10. The project wascreate --template agent-python-strands-box, with no other change.PlatformVersion: V2. The Runtime create fails, and the Runtime log has the/usrrefusal above.invokereturns status 200 with the answer. The box log shows[agent] runs /usr/bin/python3.14,exec /usr/lib/python3.14, and no refusal. All the agent's calls to Bedrock and AgentCore Memory are permitted.Local test:
start.pyin an arm64 Amazon Linux 2023 container, where Python is in/usr. It shows the same refusal without this change, and box starts with this change.bun test. 6108 pass and 1 fails:boxProfileResolver.test.ts> "resolves a profile into the rules the tests fake" (ametadata_hostsrule). That test also fails onbox-integration2f9422d5without this change.bun run test:e2e, or explained why they are not applicable(CI does not run them for you). I did not run them. The live deploy and invoke above take their place for this change.
bun run typecheckbun run lint:checkbun run format:checkbun run auditbun run secrets:checkbun run buildsrc/assets/, I updated affected snapshots withbun test <test-file> --update-snapshotsand committed them. No snapshot changed.by this PR.
old-renderer-tables.jsonchanges onlyinputs.python.prefixtoinputs.python.stdlib.command.md(the release workflow regenerates it)Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the
terms of your choice.