Skip to content

feat(box): reach a running box's policy from a library caller - #149

Closed
hanaduy wants to merge 4 commits into
mainfrom
feature/policy-reload-handle
Closed

hanaduy wants to merge 4 commits into
mainfrom
feature/policy-reload-handle

Conversation

@hanaduy

@hanaduy hanaduy commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Description

Draft, and parked on purpose. This is the public handle split out of #122 after review. The
design in it is known to be insufficient, and it is open so the code and the constraint have an
anchor — not as a merge candidate. Depends on #122 (engine half); this PR is stacked on that branch,
so its diff is the handle only.

The engine's PolicyEngine::reload is unreachable from outside the crate. #116 gave the library
run and generate_policy_schema, but the policy authority a run decides against never leaves the
crate, so an embedding caller applies an edited policy only by stopping the box and starting a new
one — which discards the agent's session, the thing reload exists to avoid.

What this adds:

pub fn run_with_handle<F>(config: PathBuf, workload: Vec<OsString>, on_open: F) -> ExitCode
where F: FnOnce(BoxHandle) + Send + 'static;

impl BoxHandle {
    pub fn reload(&self, sources: &[Policy]) -> Result<(), ReloadRefused>;
}

run_with_handle calls on_open once, after the box opens and before the workload starts, with a
BoxHandle over that run's live authority. It is additive: run keeps its signature and main.rs
is untouched. Both run paths share one private guard, so asking for a handle cannot start a second
box.

The known constraint

BoxHandle holds an Arc<PolicyEngine>. That is an in-memory pointer, so this shape only works
while Box Core runs inside the caller's address space
, and it cannot survive run moving into its
own process. No adjustment to the signature changes that — what crosses to the caller is an address
in this process's heap.

A cross-process shape has to carry a request rather than a pointer: the caller sends the authored
text over a channel, and the box process calls PolicyEngine::reload on its own engine. The engine
signature already suits that, because it takes Policy { origin, text } and never opens a file.
Open questions before this is worth designing:

  1. Does run move into its own process, and on what horizon? The 2026-10-20 sync recorded
    "exposed as a library method, not as an SDK or separate process", which reads as in-process, while
    review says a separate process is agreed and soon. These are different axes — how reload is
    exposed versus where run executes — and the answer decides whether a handle is a pointer or
    a protocol.
  2. Who owns an authored policy source? @ferdingler is exploring remote policy sources (AVP) that
    Box Core would own. If Box Core owns the source, the caller may not need to pass text at all, and
    the trigger may belong inside Box Core rather than above it. That inverts this PR's shape.
  3. Does a reload need a receipt? The sync liked immutable policy versioning. reload returns
    (), and a cross-process caller needs an acknowledgement far more than an in-process one does.

An earlier revision of #122 did carry a cross-process trigger — a control socket in the box's private
directory with an ack — removed on the premise that the caller always holds the engine in-process. If
that premise is changing, the plumbing is in that branch's history as reference, though its shape
(a side socket plus a CLI verb) is probably not what a two-process Box Core wants.

I have not guessed at a design here. Hot reload is not blocking until milestone 4, so the cost of
waiting for the topology answer is zero, and guessing is what produced three rounds of rework on
#122.

Related Issues

Split from #122.

Type of Change

New feature

Testing

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)
  • If I touched Rust, I ran cargo fmt --all and cargo clippy --workspace --all-targets --all-features -- -D warnings
cargo test -p strands-box --test box_library_reload   # ~20s, silent on success

box_library_reload.rs::a_handle_reloads_the_running_boxs_own_authority opens a real read-only box
through the library, refuses a reload naming an action the schema does not declare, applies one
written against the box's own schema, and ends on the workload's zero exit. That refusal is the
liveness evidence: only a box carrying its own compiled schema can reject the rule, so the handle
cannot be standing over a fresh engine. a_box_that_fails_to_open_hands_out_no_handle shows a
missing configuration calls on_open for no one.

The suite sets harness = false and owns its main, because the one-run guard is process-wide and
each case re-executes the test binary. It skips the live-handle case where the namespace backend
cannot open a box, which is every Linux host that is not aarch64.

Verified in a way CI cannot show: a mutation that removes the on_open call makes the first case
fail at the handle assertion. Each case runs from a copy of the test binary placed beside
target/debug/strands-box, because a box resolves its sibling binaries from the running image's
directory and a test binary lives in deps/, where they are not.

Not run: no test shows a contained workload observing a changed decision mid-run, because this handle
applies the reload before the workload starts.

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.

## Problem

A box decides every request against the policy it loaded when it opened. An
operator who edits the authored policy must stop the box and start a new one for
the edit to take effect, which ends the agent's session and discards its history.
The engine has no way to install a new authored policy over a running authority.

## Solution

`PolicyEngine::reload` takes an authored policy set and installs it over the live
authority. It takes `&self`, so a caller reloads through the `Arc` the box already
shares, and it serialises against in-flight decisions on the authority's own lock.
The box keeps its decision history, so a rule whose canonical text is unchanged
keeps the budget it has already spent. A set that does not validate is refused in
full, and the policy already installed keeps deciding every request.

`reload` takes the policy text, not a path, so the engine opens no file. Nothing
here authenticates a request either: the contained agent must never reach a
reload, and what keeps it out is where the trigger lives. Which component owns an
authored policy source, and which owns the trigger, are open questions this change
does not settle.

Canonical text is literal: a reformatted rule counts as a changed rule and starts
a new budget. `Dogwood` issue 59 tracks the gensym instability behind it, with
three tests already ignored in this crate against it.

## Tests

`policy.rs::reload_installs_a_new_authored_policy_on_a_live_engine` opens an
engine on a policy with no write rule, shows the write denied, reloads a policy
that adds one, and shows the same engine permitting the same write.
`a_reload_that_does_not_validate_leaves_the_previous_policy_in_force` shows a
refused set leaving the installed policy deciding. Both run in-crate, so they
reach the engine directly rather than through a caller.

Not run: no test shows a contained workload observing a changed decision in the
middle of a run. That needs a caller, and the public surface that reaches this
from outside the crate is a separate change.
## Problem

The library surface starts a box and returns the workload's exit status, and
nothing else leaves the crate. The policy authority a run decides against stays
inside, so a caller that embeds Box cannot install an edited policy on a box that
already runs. The only way to apply one is to stop the box and start a new one,
which discards the agent's session.

## Solution

`run_with_handle` runs a box and calls `on_open` once, after the box opens and
before the workload starts, with a `BoxHandle` over that run's live authority.
`BoxHandle::reload` installs an authored policy set over it, and `ReloadRefused`
reports a set the engine does not take. `run` keeps its signature, and both run
paths share one private guard, so a request for a handle cannot start a second
box. The surface stays narrow: `Policy` is re-exported, so a caller writes the
input without a name from an internal crate, and no other type becomes public.

## Tests

`box_library_reload.rs::a_handle_reloads_the_running_boxs_own_authority` opens a
real read-only box, refuses a reload that names an undeclared action, applies one
written against the box's own schema, and then ends with the workload's zero exit.
`a_box_that_fails_to_open_hands_out_no_handle` shows that a missing configuration
calls `on_open` for no one. A mutation that removes the `on_open` call makes the
first case fail. `policy.rs::reload_installs_a_new_authored_policy_on_a_live_engine`
pins the deny-reload-permit behaviour of the engine itself. The workspace suite
passes at 4196 tests, with 17 ignored; three of those are the Dogwood gensym tests
that issue 59 tracks. Not run: no test shows the contained workload as it observes
a changed decision in the middle of a run, because this handle applies the reload
before the workload starts.
Base automatically changed from feature/policy-reload-signal-demo to main October 22, 2026 17:51
@gautamsirdeshmukh gautamsirdeshmukh mentioned this pull request Oct 29, 2026
7 of 8 tasks
@hanaduy

hanaduy commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

#217 has cleaner integration, closing this.

@hanaduy hanaduy closed this Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant