Skip to content

feat(containment): add opt-in file watching on macOS - #430

Merged
pgrayy merged 8 commits into
mainfrom
feature/pgrayy/macos-native-file-watch
Nov 16, 2026
Merged

pgrayy merged 8 commits into
mainfrom
feature/pgrayy/macos-native-file-watch

Conversation

@pgrayy

@pgrayy pgrayy commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Description

Dev servers and build tools watch files so they can reload on each edit. On macOS they use FSEvents, which Box blocks. So inside Box, next dev misses edits and new routes, and Node reports a misleading EMFILE error. The same server works outside Box. The only workaround today is framework-specific polling, such as WATCHPACK_POLLING=1000.

This PR adds an opt-in file-watching = true on [agent], [tool.<name>], or stdio [mcp.<name>]:

[tool.dev]
command = ["npx", "next", "dev"]
file-watching = true
  • Defaults to false. Each table opts in on its own; the agent's choice does not reach its tools or servers.
  • On macOS the process gains only the com.apple.FSEvents Mach lookup. Content and write restrictions still apply.
  • FSEvents can disclose denied names and event metadata, including outside the workspace and from past events. Box prints that on stderr before the agent starts, and again when a stdio server starts.
  • No effect on Linux, which already allows inotify. Remote HTTP MCP entries refuse the key.
  • Selected configs need record version 35, so older builds refuse them; rolling back means removing the key from every table. Absent or false leaves stored records and wire shapes unchanged.
  • ContainmentConfig::allow_file_watching() is the new crate API.

The disclosure is an explicit exception to the four-inputs authority test, recorded in docs/design/decisions.md. Polling stays the answer when names must stay private.

Precedent

  • file-attributes is the closest match. It is a macOS-only switch that defaults to false, has no effect on Linux (which accepts the key), adds a file attributes: line to each process's startup disclosure, and stamps a newer record version. file-watching follows the same shape and file- naming.
  • network.contain_egress = false is the scope precedent. It grants reach outside policy, so each tool or stdio server opts in on its own table and gets a native egress: disclosure line. file-watching is per table for the same reason, unlike the box-wide file-attributes, which widens only the process's own write grants.

Related Issues

Refs #419. The strict FSEvents metadata isolation requirement remains unresolved.

Type of Change

New feature

Testing

  • Ran the relevant workspace suite via scripts/test-all.sh.
  • Ran cargo fmt --check and scoped clippy for both changed crates.

macOS follow-up validation passed:

  • All-feature builds for Box and containment, the no-default build, and workspace formatting passed. Scoped clippy reported zero warnings in the changed crates and six unchanged vendored warnings. The no-default build retained the existing unused render_profile warning.
  • scripts/test-all.sh: 5,107 passed, zero failed, and 19 ignored. The locked site_manifest suite also passed all 27 tests.
  • test-integ/run.sh: GREEN, with 129 passes, 12 declared Linux-only skips, and zero failures or errors across all 141 expected results. Coverage was full, with no quarantine or integrity problems.

Host-dependent coverage gaps were missing credsd, home-directory Node/Python, Homebrew Python, and filesystem support for non-UTF-8 filenames. No scripted examples remained; the live SDK tutorial was not run.

The native macOS launcher tests cover create, modify, rename, delete, default denial, agent/tool selection, and direct stat/list/read/write/unlink refusals with successful allowed-access controls. A profile comparison verifies that only the exact service lookup changes. Configuration tests cover all three process types, version precedence, serialization, remote-MCP refusal, and macOS-only disclosure. A startup regression also checks that an unused MCP server's warning precedes the agent, covers omitted/false/true settings and independent process selections, and expects no warning on Linux. Removing only the startup warning makes that regression fail. Independent mutation review checked each new test.

After merging main, the full workspace suite ran on Linux aarch64: 170 of 173 targets pass, including every file_watching test, runtime_mcp_start, config_model, and profile_conformance. The 3 failures are in files this PR does not touch (box_telemetry OTLP content type, runtime_mcp_policy_staging catalog page limit timeout, ingress PID 1 readiness timing). cargo fmt --check and CI's Clippy invocation pass.

The earlier follow-up compiled successfully on Linux ARM64 with cargo check -p strands-box --all-targets --all-features --locked in Finch, including the new test. Linux runtime execution of the follow-up remains for CI. No Apple report was submitted.

Checklist

  • I have read the CONTRIBUTING document.
  • I have reviewed and understand every line of code in this PR, including generated code.
  • The change is focused on explicit file watching opt-in.
  • Tests cover the new behavior and preserve default denials.
  • The documentation is updated in the same commit.
  • The change generates 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.

## Problem

macOS directory watchers that use FSEvents cannot start inside Box. Node can
report EMFILE for this service denial. Permitting the service does not give
Linux-equivalent filename privacy: events can disclose denied names and
activity, including outside the workspace and from past events.

## Solution

Add native-file-watching, false by default, to agent, tool, and stdio MCP
process tables. Select it independently for each process and its native
descendants. Add ContainmentConfig::allow_native_file_watching() and one exact
com.apple.FSEvents Mach lookup grant on macOS. Keep automatic runtime-service
grants unchanged. Linux accepts the setting without changing enforcement or
disclosure; remote HTTP MCP entries refuse it.

On macOS, disclose the metadata limitation for each selected process. Keep
existing direct content and write restrictions. Document the accepted risk,
polling alternative, process selection, and rollback. Selected configurations
require record version 33; absent or false settings retain their prior record
and containment wire shapes. Broker and live record formats do not change.

## Tests

native_file_watching_requires_an_explicit_boolean and
native_file_watching_is_off_in_old_wire_and_preserved_when_selected cover the
default, type checks, and wire compatibility.
native_file_watching_is_versioned_and_selected_per_process and
native_file_watching_version_survives_other_feature_selections cover version 33,
independent selections, round trips, and version precedence.
a_remote_mcp_server_refuses_native_file_watching covers both boolean values.
native_file_watching_follows_only_the_selected_process covers propagation and
macOS-only disclosure for agents, tools, and local MCP servers.
native_file_watching_adds_only_the_exact_fsevents_lookup compares the full
agent and leaf profiles with only the selected lookup removed.

The shipped-launcher tests an_agent_must_opt_in_before_fsevents_starts,
an_opted_in_agent_watches_changes_and_keeps_direct_denials,
an_opted_in_tool_watches_without_enabling_its_agent, and
an_agent_opt_in_does_not_enable_a_tool cover create, modify, rename, and delete
events, strict defaults, process independence, and direct stat/list/read/write/
unlink denials with allowed-access controls. Each new test was checked against
a relevant mutation in an isolated scratch checkout.

On macOS 26.7.3, both-package all-feature build, scoped all-target/all-feature
clippy, root format check, and both-package no-default build passed. Clippy
reported zero owned warnings and six existing vendored Shell warnings; the
no-default build retains the existing unused render_profile warning.

The single scripts/test-all.sh sweep reported 5,105 passed, 1 failed, and
19 ignored. Its only failure was a_record_from_another_format_is_refused,
whose old highest-version sentinel collided with the new version 33. The
corrected final-source test passed on rerun, as did the final precedence test.
All other workspace suites passed. site_manifest passed all 27 tests.
The deterministic harness was GREEN: 129 PASS, zero FAIL/ERROR, and 12 declared
Linux-only SKIP, with coverage_gate=full.

Reported host gaps: no Homebrew Python, no Python or Node within the operator
home, no credsd daemon, and a filesystem that refuses non-UTF-8 pathnames.
No scripted examples remain; the live SDK tutorial was not run.

Linux ARM64 all-target/all-feature check and full build passed in Finch; five
Box configuration/boundary tests and the containment wire test passed there.
The full Linux native enforcement suite remains for CI. FSEvents metadata
privacy remains an explicit accepted limitation. No current Apple report was
submitted as part of this change.
Comment thread docs/user/config.md Resolved
Comment thread docs/design/decisions.md Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Comment

The mechanism is narrow and correct. It adds one exact com.apple.FSEvents lookup, selected per process, with versioned records. The two open points are about disclosure: when stdio MCP servers are disclosed, and how the decision record squares this key with the box contract's four-inputs test.

Assumptions and rules checked that held
  • Only the selected table gets the grant. I checked agent, tool, and stdio MCP, the agent-on/tool-off and tool-on/agent-off cases, and Shell-spawned caller-exec programs, which inherit the agent's flag under [agent] the same way native-filesystem-policy does.
  • Remote HTTP MCP entries refuse the key through deny_unknown_fields.
  • The profile difference is exactly one line, for both the agent and a broad-exec leaf. I ran native_file_watching_adds_only_the_exact_fsevents_lookup and the wire round-trip test on Linux, and both pass.
  • Record versions: watching alone, or watching combined with gateway, limits, process policy, or file attributes, stamps 33. Records that were valid before keep their versions (the fold's max(22, 23) still gives 23). false or an absent key is not serialized.
  • Linux carries the flag on the containment wire, ignores it in the backend, and prints no disclosure.
  • Agent and tool startup disclosures print through run.rs.

The kernel-level native tests, with probes for both granted and refused access, are a strong regression guard.

Print the existing native file-watching warning for each selected stdio MCP
server before the agent starts, even when that server is never invoked. Share
the warning text with the launch-time boundary disclosure.

The startup regression covers omitted, false, and true selections, independent
agent and server settings, warning order, zero server invocations, and Linux
silence. Clarify the previously approved exception to the four-input authority
test and document when MCP warnings are printed.

Validation: scripts/test-all.sh passed (5,107 passed, 0 failed, 19 ignored).
The new startup regression failed when only the startup warning was removed.
All-feature and no-default builds, workspace fmt, scoped clippy, and the locked
site_manifest suite (27 tests) passed. Clippy reported only six unchanged
vendored warnings; no-default retained the existing render_profile warning.
The deterministic harness was GREEN (129 passed, 12 declared Linux-only skips,
0 failed/errors; full coverage). Linux ARM64 all-target/all-feature locked
cargo check passed, including the new test; Linux runtime remains for CI.

Host-dependent coverage gaps: no credsd, home-directory Node/Python, Homebrew
Python, or non-UTF-8 filename support. No scripted examples remained; the live
SDK tutorial was not run.
@github-actions

Copy link
Copy Markdown

Assessment: Approve

710eb58 resolves both earlier findings: stdio MCP servers now get the warning at startup, and the decision entry records the four-inputs exception. I found no new issues in the 85-line delta.

Assumptions and rules checked that held (delta since 9f0e78d)
  • New startup branch, case by case: selected stdio MCP on macOS prints the warning; false or absent prints nothing; Linux prints nothing; remote HTTP servers are not in contained_mcp; a never-started server is still warned about before AGENT_STARTED.
  • MCP disclosure lives only in the hosted path, so no other run path is missing it.
  • The warning text has one owner (native_file_watching_disclosure), shared by startup and spawn-time output.
  • runtime_mcp_start.rs is cfg(unix) and its launcher gate returns true on macOS, so the new regression runs where the warning exists.
  • Docs (config.md, mcp.md, architecture.md, decisions.md) now describe the startup and repeated-at-start behavior the code implements.
  • Everything from the earlier review still holds: per-table selection, the one-line profile delta, record version 33 gating, and HTTP refusal.

EC2 Default User added 2 commits November 13, 2026 17:11
…native-file-watch

# Conflicts:
#	crates/box/src/record/config/mod.rs
Version the watching record in the same chain as every other feature, drop a test that rechecked serde and the config tests, match the boundary and wire tests to their file-attributes siblings, and place the config guide section after the process prose so no existing text changes.
@github-actions

Copy link
Copy Markdown

Assessment: Approve

Since 710eb58, the branch has merged main (#397 took record version 33 for limits.shell.max_stdin) and added a tidy-up commit, 603f672. I found nothing new to fix.

Assumptions and rules checked that held (delta 710eb58 → 603f672)
  • Version renumber: a record with watching alone stamps 34. Watching plus max_stdin, gateway, limits, file attributes, or a process policy also stamps 34. A stdin limit alone still stamps 33. A disabled or absent key keeps the earlier versions. A watching record stamped 33 is refused with expected: 34, and the unknown-version test now probes NATIVE_FILE_WATCHING_RECORD_VERSION + 1.
  • Version chain: moving watching from the process_record_version fold to the top of record_version's chain keeps "highest wins" for every combination.
  • Removed tests: native_file_watching_is_versioned_and_selected_per_process still covers absent/false/true per table, plus serialization and round trips. Serde's bool type covers the non-boolean refusals, and the new the_native_file_watching_switch_round_trips_and_defaults_off covers the wire.
  • Disclosure assertions: the boundary test now compares against native_file_watching_disclosure(table), so the warning text has one owner and the test checks the full line.
  • Startup disclosure and decision record: both fixes from the last round survived the merge. The config.md section is moved without content changes.
  • Ran on Linux: strands-box --lib native_file_watching (4), record:: (315), and containment config_model native_file_watching (1) all pass.

…native-file-watch

# Conflicts:
#	crates/box/src/record/config/mod.rs
#	docs/user/native-filesystem-policy.md
Restore the process-field wording the README and containment design had, point the MCP guide and architecture note at the config reference instead of repeating it, fold the remote-MCP refusal into the per-process test, and drop the precedence test the version chain now makes redundant.
@github-actions

Copy link
Copy Markdown

Assessment: Approve

Since 603f672, the branch has one change: a merge of main. Main's #437 took record version 34 for box IDs that library callers supply, so the watching record correctly moves to 35. The merge resolution is correct. I have one small follow-up.

Suggestion (non-blocking): add NATIVE_FILE_WATCHING_RECORD_VERSION to the frozen-surface list in crates/box/AGENTS.md.
Example: lines 8–9 freeze "Record at RECORD_VERSION and each later record version through SUPPLIED_IDENTITY_RECORD_VERSION". #437 updated that bound when it added 34, but this PR adds 35 and leaves the bound where it was. That leaves the newest record shape outside the surface the crate says is frozen.
Suggestion: change the bound to "through NATIVE_FILE_WATCHING_RECORD_VERSION".

Assumptions checked that held (delta 603f672 → e18c648)

@github-actions

Copy link
Copy Markdown

Assessment: Approve

Since 603f672, the branch has merged main and added one commit, d2b9fef, which cuts back the docs and folds the tests together. I found nothing to fix.

Assumptions and rules checked that held (603f672 → d2b9fef)
  • Renumber after feat(box): accept an optional box ID from library callers, and generate one as today when absent #437: a library-supplied box ID now uses version 34, and file watching moves to 35, first in record_version's chain. A record with watching alone or with a supplied ID stamps 35. A supplied ID alone stamps 34, and records without the setting are unchanged. Record::parse accepts both versions, the unknown-version test probes 36, and native-filesystem-policy.md lists 34 and 35.
  • Removed combination test: if a later merge put watching lower in the chain, a build at the lower version would still refuse the record. ProcessSpec is deny_unknown_fields, so the refusal would come from the unknown key rather than a version mismatch. No record loads without its watching selection.
  • Remote MCP refusal fold: the key is refused before its value is read, so testing only false covers true as well.
  • Library callers (feat(box): accept an optional box ID from library callers, and generate one as today when absent #437): lib::run goes through command::execute, so library launches print the same startup disclosure, including for unstarted stdio MCP servers.
  • Shortened docs: mcp.md, architecture.md, and the README still state per-process selection, that remote servers refuse the key, and the disclosure timing, and they link to config.md#native-file-watching.
  • Ran on Linux: strands-box --lib record:: (319) and native_file_watching (2) pass.

The switch only gates macOS FSEvents, and Linux ignores it. The shorter key matches file-attributes and the docs' own wording.
@pgrayy pgrayy changed the title feat(containment): add opt-in native file watching on macOS feat(containment): add opt-in file watching on macOS Nov 16, 2026
@pgrayy
pgrayy enabled auto-merge (squash) November 16, 2026 17:40
Comment thread docs/user/config.md Resolved
@pgrayy
pgrayy merged commit bd38809 into main Nov 16, 2026
21 checks passed
@pgrayy
pgrayy deleted the feature/pgrayy/macos-native-file-watch branch November 16, 2026 18:45
@kevin-orellana

Copy link
Copy Markdown
Collaborator

Reviewed at ba24a091de3c76fc69923d1ffebd87eb69af65c3 on macOS 27.2.1 arm64.
That is the merged head, so these are post-merge findings against main rather
than change requests on this pull request. I ran the measurements after the merge.

The mechanism looks right to me, and I found no escape. The setting is opt-in, the
grant names one service instead of a broad mach-lookup, and the sandbox still
bounds what the service hands back. I pointed the service at the operator's home
from inside a box with the setting on, and it returned no path. The same program
outside a box replayed 4,973,153 events and named keychain and address-book files.
File contents stayed refused throughout.

Two findings are about the startup disclosure rather than the mechanism. One is a
test suggestion.


The startup disclosure does not name a host-wide activity counter

Type: Security | Interpreted Severity: P0 (warrants review)

crates/box/src/run/contain/boundary.rs, lines 927-936

Human-readable explanation

An operator sets file-watching = true so their agent learns about file changes
instead of checking files repeatedly. Box prints a warning at startup. It says the
service can reveal names of denied files and event metadata, including names
outside the workspace, and it says file contents and write restrictions still
apply.

The same grant discloses a third thing, and the warning does not name it. The
service answers a global counter of filesystem changes for the whole machine. The
call takes no path argument and needs no privilege. A workload reads the counter
twice and learns how busy the host is. That includes activity in directories the
box holds no grant on, and activity from other users and other boxes on the same
machine. FSEventsCopyUUIDForDevice answers in the same way, and it discloses the
volume's history database identity. With the setting absent, both calls fail.

The counter leaks no name and no file content, so I read the channel itself as low
severity. What makes it worth a change is the premise rather than the bandwidth: a
direct grant in Box states what it costs, and this one states less than it costs.
I would add both the counter and the volume identifier to
file_watching_disclosure.

One consequence I did not test. Two boxes on one host could signal each other
through the counter, because box A writes in its own workspace and box B reads the
rate. That crosses an isolation line, and I am recording it as an inference from
the measurement below rather than as a result.

Reproduction details

Tested: ba24a091de3c76fc69923d1ffebd87eb69af65c3, macOS 27.2.1 arm64,
build 26B131, as uid 503. Source comparison over 1,130 files confirmed the
checkout matched the reviewed head.

Write a temporary probe that links CoreServices and calls
FSEventsGetCurrentEventId() three times, one second apart, printing each value
and the difference. Have it refuse to run as root. Build it with
cc -framework CoreServices.

  1. Create two directories outside the workspace. Grant the box read and write
    on its workspace only, and no grant of any kind on the second directory.
  2. Write a box.toml whose [agent] table sets command to the probe,
    workspace to the first directory, and file-watching = true. Place box_dir
    beside the workspace rather than inside it, and set its mode to 0700.
  3. Start a background helper that creates 400 files in the ungranted directory.
  4. Run strands-box run --config box.toml.
  5. Repeat steps 2 to 4 with file-watching absent.

Expected: With the setting absent, the counter should not answer, because the
sandbox refuses the service connection.

Observed with the setting on: The counter read 121867601, then advanced 1462,
then advanced 1770. FSEventsCopyUUIDForDevice returned
32D17658-C33B-43FF-9730-5B814D882D62 for the boot volume.

Observed with the setting absent: The counter returned 0 and the volume
identifier was unavailable.

Control: The same probe outside any box read the counter and the identifier,
which attributes the refusal to the sandbox rather than to an absent service.

Limits: The two-box signaling case did not run. The counter's resolution and
its noise floor under concurrent host activity were not measured. The probe
reads; it never writes.


A root workload reaches the history delete, and the disclosure does not say so

Type: Security | Interpreted Severity: P0 (warrants review)

crates/containment/src/backend/macos/seatbelt.rs, lines 834-841

Human-readable explanation

The grant authorizes a workload to open a connection to one named service. Box
supports root launches, so a workload can run as root.

That service does more than report file changes. It holds seven operations. One of
them, FSEventsPurgeEventsForDeviceUpToEventId, deletes the stored change history
for a volume. Apple's header says only root may call it, and the service enforces
exactly that: it checks whether the sender is root and refuses anyone else.

Seatbelt decides one thing here, which is whether the workload may open the
connection. The rendered profile carries no message filter and no per-operation
condition, so a workload holding the connection can invoke any of the seven. A
root workload therefore satisfies both gates: Box permits the connection, and the
service accepts the sender. The startup warning describes disclosure of names and
metadata, so an operator reading it would not expect a workload to be able to
destroy host state.

Two options look reasonable to me. Name the delete in file_watching_disclosure,
alongside the condition that it needs a root workload. Or refuse the setting when
the workload runs as root, which removes the reach instead of disclosing it. I
lean toward the second, because the warning is already long and this cost is
conditional, but the choice is yours.

To be clear about what this is and is not: I am not reporting an escape from the
sandbox. The grant does what it says. The finding is that its stated cost is
narrower than its actual reach.

Evidence details

No runtime reproduction ran. I did not call the delete operation, and I never ran
anything as root. The reach below is established from one measurement plus source
inspection; the effect of the delete is not established.

Tested: ba24a091de3c76fc69923d1ffebd87eb69af65c3, macOS 27.2.1 arm64.

Observed, with a probe inside a real box:

  1. With file-watching absent, bootstrap_look_up for com.apple.FSEvents
    returned 1100, which is BOOTSTRAP_NOT_PRIVILEGED.
  2. With the setting on, the same call returned 0. A message carrying an operation
    number the service does not define then reached the service, which discarded it
    and released the reply right. That returned notification id 71,
    MACH_NOTIFY_SEND_ONCE.
  3. Outside any box, the same call returned 0. The control matters, because a
    retired service answers 1102 and a refusal answers 1100, so both are nonzero.

Read from the rendered profile, taken from the box's own trampoline configuration:
the only service rule added is
(allow mach-lookup (global-name "com.apple.FSEvents")). A search of the rendered
profile for a message-id condition, a filter-msg rule, or any per-operation
condition returned nothing.

Source-confirmed, from Apple's installed SDK header for this release: the delete
routine is declared as root-only and returns only a Boolean.

Inference, which is the step I did not measure: because Seatbelt authorizes the
connection by service name and filters no operation behind it, a root workload's
delete request reaches the service, and the service's own check passes for a root
sender.

Limits: Whether the delete then removes history is Apple's behavior and was not
tested. Confirming it needs root and a disposable volume. I verified the fixture is
buildable without root: a 1 GiB APFS image attached as uid 503, with a device
number distinct from the boot volume, carrying its own .fseventsd directory.


No test obtains the service inside a real box

Type: Testing | Interpreted Severity: P1 (non-blocking)

crates/containment/tests/profile_conformance.rs, lines 1534-1552

Human-readable explanation

file_watching_adds_only_the_exact_fsevents_lookup and
file_watching_is_versioned_and_selected_per_process read the generated rules as
text. They establish that the grant renders once and changes no other rule, which
is the right property for them to hold.

No test asks the operating system whether the grant works. So a change that
stopped the permission taking effect, or one that granted it with the setting off,
would leave the suite green. The crate already has the pattern for this in
account_lookup_is_allowed_but_preferences_services_remain_denied,
which drives a real box and keeps an uncontained control for each service.
com.apple.FSEvents is absent from its table, and containment-test-probe
already carries the mach-lookup verb.

Two further gaps sit beside it. No golden profile renders a file-watching box. And
the_rendered_profile_grants_exactly_these_operations_and_no_others cannot see
this change, because mach-lookup was already granted for the directory service,
so the operation count does not move when a second service name appears.

Would it make sense to add the two probe rows before the next change to this
grant? The measured values below are ready to use.

Reproduction details

Tested: ba24a091de3c76fc69923d1ffebd87eb69af65c3, macOS 27.2.1 arm64.

Extend the existing table with com.apple.FSEvents, run once with the setting on
and once with it absent, and keep the uncontained control that the surrounding
tests already use.

Measured values for those rows:

Arm bootstrap_look_up
file-watching absent 1100 (BOOTSTRAP_NOT_PRIVILEGED)
file-watching = true 0
uncontained control 0

Control: Keep the uncontained arm. A retired service answers 1102 and a
refusal answers 1100, so an assertion on "nonzero" goes vacuous without it. The
surrounding tests record this reason already.

Limits: I ran these arms through a hand-written probe and a local box.toml,
not through the crate's test harness. The numbers are the expected assertions, not
a passing test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants