Skip to content

fix(box): record an answered Python metadata check as a response - #268

Merged
priyasingh2111 merged 2 commits into
mainfrom
feature/monty-record-answered-metadata
Nov 3, 2026
Merged

priyasingh2111 merged 2 commits into
mainfrom
feature/monty-record-answered-metadata

Conversation

@priyasingh2111

@priyasingh2111 priyasingh2111 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Problem: This is the Python follow-up to #144. #144 made the Shell record a file check that got an answer as fs:read::response, and a check that Box refuses as fs:read::error. Python did not follow that rule. os.stat() of a missing file was recorded as an error, so a probe cap did not count it, and the agent could probe missing files without limit through python3. And Path.exists() and its siblings recorded a symlink-swap refusal as completed, so a refused check could satisfy a ::response rule.

Fix: Python now records an answered check as a response and a refused one as an error, the same way the Shell does.

Description

Python call Example main This PR
Missing file os.stat("missing.txt") error response
Parent is a regular file os.stat("notes.txt/child") error response
Folder the host will not search Path("locked/f.txt").exists() response response
Symlink swapped into the path after approval Path("dir/f.txt").exists() response error (still returns False)
Leaf swapped for a symlink after approval os.stat("f.txt") response error
Reach floor refuses any check error error

How.

  • HeldPath::reach returns why a walk stopped. A missing component, a permission refusal, or a regular file is an answer. A symlink is a refusal. fstatat tells the two apart, because a symlink and a regular file both fail the walk with ENOTDIR. HeldPath::of does not change for other callers.
  • os.stat() and the four predicates record the outcome from that. What the script sees does not change.
  • A host permission refusal counts as an answer, as in the Shell.

Interface change, approved by the operator. This changes what when temporal observes for Python read_metadata. docs/user/policy/actions.md, docs/design/policy.md, crates/policy/README.md, and docs/design/decisions.md describe it.

Related Issues

Follow-up to #144, which made the same change for the Shell and left Python as a separate change.

Type of Change

Bug fix

Testing

  • Real box: MO-PROBE-BUDGET (3 missing-file stat() calls trip a cap of 3, and 2 do not), SH-PROBE-SHARED (a Shell test -e and a Python stat() share one budget), SH-PROBE-PERMISSION (a permission refusal is an answer through both routes), and MO-PROBE-PREDICATES. MO-PROBE-BUDGET and SH-PROBE-SHARED fail on main.
  • Unit tests for each walk outcome in held.rs, and for stat() and the predicates in effects.rs, through the reach floor where it applies (admit_and_perform). With the symlink rule removed, the swap tests fail, including an_ancestor_swapped_for_a_link_after_approval_is_never_followed.
  • I ran the relevant suites: cargo test --workspace --all-features (4,376 passed, 0 failed), and test-integ/run.sh on macOS (GREEN, 8 Linux-only skips).
  • If I touched Rust, I ran cargo fmt --all --check and the CI clippy gate. No new warnings.

Not run locally: Linux. CI covers it.

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

… a refused one as an error

## Problem

The hosted Shell records an answered `stat`, `lstat`, or `access` as
`fs:read::response`, also when the answer is "no", and records a check that
Box refuses as `fs:read::error`. Python (Monty) did not follow this rule, in
two directions:

- `os.stat()` of a missing file recorded `fs:read::error`. The script got its
  answer (`OSError`), but a budget keyed on
  `fs:read::response{read_metadata}` did not count it. So a workload could
  probe any number of missing files through the `python3` alias.
- `Path.exists()`, `is_file()`, `is_dir()`, and `is_symlink()` recorded
  `Completed` for every failure to reach the parent, also when the swap
  guard refused a symbolic link that was swapped into the path after
  approval. So a refused check could satisfy a `::response` precondition.

## Solution

- `crates/box/src/run/broker/held.rs`: `HeldPath::reach` holds the parent of
  an approved path, or returns `Unreached`. A step of the walk that fails is
  an answer when the component is missing, when the host refuses it for
  permission, or when the component is not a directory and not a symbolic
  link. It is a refusal when the component is a symbolic link, or when the
  path is not canonical. `fstatat` with `AT_SYMLINK_NOFOLLOW` on the held
  parent tells the kinds apart, because a symbolic link and a regular file
  both fail the walk with `ENOTDIR`. `HeldPath::of` keeps its behaviour for
  every other caller.
- `crates/box/src/run/broker/python/effects.rs`: `os.stat()` and the four
  predicates record `Completed` for an answer and `Failed` for a refusal.
  A `stat` of the leaf that fails because the leaf is missing, or for
  permission, records `Completed`. The script sees the same return values
  and exceptions as before.
- Docs: `docs/user/policy/actions.md`, `docs/design/policy.md`,
  `crates/policy/README.md`, and the `docs/design/decisions.md` entry
  `temporal-rules-are-enforced-against-recorded-history` state the rule for
  Python too.

This changes what `when temporal` observes for Python `read_metadata`. The
operator approved it. A host permission refusal is an answer, as in the
Shell.

## Tests

- `held.rs`: `a_missing_component_is_an_answer`,
  `a_regular_file_component_is_an_answer`,
  `a_directory_the_host_will_not_search_is_an_answer`,
  `a_symbolic_link_component_is_a_refusal`,
  `a_dangling_symbolic_link_component_is_a_refusal`,
  `a_relative_path_is_a_refusal`, `of_refuses_every_unreached_path`, and
  `a_walk_to_an_existing_parent_holds_it`.
- `effects.rs`: `a_stat_of_a_missing_path_records_completed`,
  `a_stat_the_host_refuses_for_permission_records_completed`,
  `a_predicate_refused_by_an_ancestor_swap_records_failed`,
  `a_predicate_of_a_missing_path_records_completed`, and
  `a_read_metadata_budget_counts_stats_of_missing_paths`. The existing
  `an_ancestor_swapped_for_a_link_after_approval_is_never_followed` still
  holds.
- `test-integ`: `MO-PROBE-BUDGET`, `MO-PROBE-PREDICATES`, `SH-PROBE-SHARED`,
  and `SH-PROBE-PERMISSION` run a real box. `MO-PROBE-BUDGET` and
  `SH-PROBE-SHARED` fail with the `effects.rs` and `held.rs` of `main`.
- With the symbolic-link rule removed, the two symbolic-link tests, the
  predicate swap test, and `an_ancestor_swapped_for_a_link_after_approval_is_never_followed`
  fail. The two permission tests skip as root.

Ran on macOS, on `main` at `a916050`: `cargo test --workspace
--all-features` (4,373 passed, 0 failed), `cargo fmt --all --check`, the CI
clippy gate, and `test-integ/run.sh` (125 passed, 0 failed, 8 skipped
Linux-only cases).

Not run: the Linux runtime. The `fstatat` failure branch is reached only on
Linux. Still different from the Shell: the reach floor refuses a Python path
whose parent folder does not exist, before this code runs, so that check
records `fs:read::error`.
@priyasingh2111
priyasingh2111 marked this pull request as ready for review November 2, 2026 19:31
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Assessment: Request Changes

The walk classification in HeldPath::reach is correct for every path that gets past the reach floor. But the floor runs first, and it still turns the most common "no" answer (a folder that does not exist) into an ::error that the script can read. So the read_metadata budget bypass that the PR names is still open, and three unit tests skip the floor and check the opposite of what users see. Details are in the inline comments.

Checked and held
  • The walk classifies ENOENT, EACCES, and a non-symlink ENOTDIR as answers, and a symlink as a refusal (ELOOP on macOS goes to fstatat, which sees S_IFLNK). This is correct for missing.txt, notes.txt/child, locked/f.txt, and an ancestor swapped for a link.
  • Any other errno on a directory component (EMFILE, EIO) counts as an answer. This matches the Shell, where only guard_resolution refuses and an error from stat_effect becomes the default answer.
  • A race between openat and fstatat (symlink swapped for a file) can only misclassify paths that the workload can already write, so the workload learns nothing about the host.
  • HeldPath::of behaves as before for its other callers.
  • Resolve and Absolute (also read_metadata) stay refused and do not change.
  • The interface change is disclosed as operator-approved, and all four docs describe it the same way. No premise or other frozen surface is touched.

The Unreached split is small and easy to follow, and the two shared-budget integ cases are useful.

ferdingler
ferdingler previously approved these changes Nov 2, 2026
…the probe tests to what users see

- A Python metadata read whose leaf turned into a symbolic link after
  approval now records `Failed`, as the Shell's identity guard does.
  Approval admits no link at the leaf, so a link there proves a swap.
  `a_metadata_read_of_a_leaf_swapped_for_a_link_records_failed` pins it.
- The direct `perform` tests keep only paths the reach floor admits. New
  guard tests drive `admit_and_perform`: a path under this box's own state
  and a path outside the reachable set spend no metadata budget.
- Docs and the decisions entry state the rule per route and with the cases
  it covers: an answered check of a missing file in an existing folder, or
  of a file in a folder the host will not search, is a response, and a
  check that Box refuses is an error.

Ran on macOS: `cargo test --workspace --all-features` (4,376 passed,
0 failed), the CI clippy gate, `cargo fmt --check`, and `test-integ/run.sh`
(125 passed, 0 failed, 8 skipped Linux-only cases).
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Assessment: Request Changes

Re-reviewed at 9c27de9. The test fix holds: the direct perform() rows now use only paths the floor admits, and the new admit_and_perform tests check the outcome users actually get. The one open issue is still the missing-folder probe, and I replied on that thread. I ran the same probe through both routes: the Shell records it as a ::response and Python records it as a ::error. That contradicts the "out of scope" reasoning, and this commit also removed the user-doc sentence that disclosed the gap.

Checked and held
  • leaf_answered assumes that every symlink leaf seen in perform was swapped in after approval. This holds because approve() only goes through approve_host. A real link at the leaf gets NotCanonical, and a dangling link gets "leaf name already taken". Both are refused before perform, the same as on main.
  • A race between the two fstatat calls can only misclassify a leaf in a folder the workload can write, so the workload learns nothing about the host.
  • The new a_stat_beneath_the_box_state_spends_nothing and a_stat_outside_the_reachable_set_spends_nothing correctly pin floor refusals as non-spending.
  • The decisions.md text that says a cap on attempts must key on ::request matches the code.

@priyasingh2111
priyasingh2111 merged commit 917e71d into main Nov 3, 2026
23 of 25 checks passed
@priyasingh2111
priyasingh2111 deleted the feature/monty-record-answered-metadata branch November 3, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area - Shell/Monty bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants