Repository navigation
feat(policy): reload an authored policy over a live authority - #122
Conversation
|
I'd like to understand the devex that we're going after here that requires policy reload. It seems like this PR is adding another CLI verb on the box core binary, but, presumably this isn't the long term option right? I don't imagine the Box CLI will use it this way. |
The CLI verb isn't the long-term interface, and I don't think the Box CLI should expose it this way either. The surface for that is the library call, PolicyEngine::reload(&self): it's &self, callable in-process, so a harness (or Boxy via the SDK layer) handles the banner click by calling reload() directly. No CLI and no socket in that path. I can Gate it behind once we have the CLI part for reload. |
Went further than gating and removed the verb. Boxy embeds Box as a library, so the caller already holds the engine and the trigger is just the function call; the commit drops the verb, the control socket, and the That takes the interface-freeze crossing from twice to once, and the devex it buys is an agent session that survives a rule change instead of ending on one. |
05d603c to
3a81c05
Compare
|
The engine half looks great, What I'd propose is splitting this PR in two:
One more thing for context: I've been exploring remote policy sources (e.g. AVP) that Box Core would own. Your framing of the "caller owns the source" in the doc comments will probably not be the long term approach. Happy to walk you through it, it's still early. |
## 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.
5dc809e to
0e108f1
Compare
Split as proposed: #122 is the engine and its tests only, and the handle moved to #149 as a draft. You're right that the Both the process question and whether Box Core ends up owning the policy |
| .map_err(|error| PolicyStagingError::Durable(error.to_string()))?; | ||
| proposed.mark_all_ready(); | ||
| live.readiness = Readiness::Ready; | ||
| live.pending_tools = BTreeSet::new(); |
There was a problem hiding this comment.
Issue: reload unconditionally sets readiness = Ready and clears pending_tools, but leaves degraded_tools untouched, and there's no stated behaviour for a reload that arrives while the authority is still Discovering. As written, a reload during discovery just fails at validate_canonical_policy_set (the per-tool typed rules reference schemas that haven't staged), and a server degraded in a prior discovery keeps denying its tools fail-closed even if the new authored set would permit its tools/list.
Suggestion: The fail-closed direction is safe, so this isn't a blocker, but the Discovering/degraded_tools interaction is non-obvious. Please either add a short doc note stating that reload requires a fully-discovered schema set and that degraded_tools are intentionally sticky across a reload, or add a test pinning reload behaviour while Discovering.
| let Some(current) = proposal_guard.as_ref() else { | ||
| return Err(PolicyStagingError::Durable( | ||
| "the authority was not opened for runtime staging".to_string(), | ||
| )); |
There was a problem hiding this comment.
Issue: PolicyStagingError::Durable is documented as "The durable authority could not recover or commit" — a storage fault. Here it's returned for a caller/state error (the authority was opened via open_complete, not staged), which mislabels a programming mistake as a durability failure. stage_mcp_schema handles the same condition without an error (it returns SchemaStage::Rejected).
Suggestion: Consider a dedicated variant (e.g. a NotStaged/misuse case) so a caller can distinguish "you called reload on the wrong kind of authority" from "the history store failed." Minor, but it affects how a future caller handles the error.
| /// The box's decision history is kept, so temporal budgets and rate counters accumulated | ||
| /// before the reload still apply after it. A rule whose canonical text is unchanged keeps | ||
| /// its token; a rule whose text changed is a new rule and starts a new budget. | ||
| /// |
There was a problem hiding this comment.
Issue: The doc comment makes a load-bearing behavioural claim — "A rule whose canonical text is unchanged keeps its token; a rule whose text changed is a new rule and starts a new budget" — but neither added test exercises it. The root AGENTS.md is explicit: "A claim about behaviour needs a test named beside it, or it is not a claim." Budget/counter continuity across a reload is the whole point of keeping decision history, so this is the most important behaviour to pin.
Suggestion: Add a test that spends part of a temporal/rate budget, reloads a set whose rule text is byte-identical (plus one new rule), and asserts the spent budget carries over; ideally a companion showing a reworded rule resets its budget. The PR already references issue #59's gensym instability for the reformatted-text direction — a positive test for the unchanged-text direction would still pin the claim that matters here.
| })?; | ||
| live.engine | ||
| .commit_staged(&complete, effective_schema) | ||
| .map_err(|error| PolicyStagingError::Durable(error.to_string()))?; |
There was a problem hiding this comment.
Issue: stage_mcp_schema distinguishes Err(DurableError::Rejected(reason)) (a non-fatal rejection that leaves the previous authority serving) from other durable errors. Here every commit_staged error is flattened into PolicyStagingError::Durable. Since validation already ran before the live lock, a Rejected here should be rare, but if it does occur the caller sees a generic "history failure" string rather than the rejection reason.
Suggestion: Mirror stage_mcp_schema's match on DurableError::Rejected so a post-validation rejection surfaces its reason (the previous policy still serves either way thanks to the atomic batch, so behaviour is unchanged — this is about the error the caller gets).
|
|
||
| engine | ||
| .reload(&source("this is not a policy {{{")) | ||
| .expect_err("a policy that does not parse is refused"); |
There was a problem hiding this comment.
Issue: The only refusal test feeds text that fails at parse_policy_source ("this is not a policy {{{"), so it exercises the early ? on parsing and never reaches validate_canonical_policy_set. The invariant you describe ("a set that does not validate is refused in full") is strongest at the strict-validation stage, which this test skips.
Suggestion: Add a refusal test whose text parses but fails strict validation (e.g. references an unknown action), asserting the previously-installed policy still decides. A test for the open_complete/not-staged path returning an error would also be worth having, since that branch is currently uncovered.
|
Issue: This PR adds a new Suggestion: Add the |
|
Assessment: Comment The engine-only shape ( Review themes
Nice to see the serialisation and lock-ordering reasoning spelled out in the comments — it made the concurrency review straightforward. |
Description
This PR was split in two after review. It is now the engine half only. The public handle moved
to #149, which is parked for a design revisit — see "Split" below.
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.
PolicyEngine::reloadinstalls an authored policy set over the live authority:It takes
&self, so a caller reloads through theArcthe box already shares, and it serialisesagainst 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 installed policy keeps deciding every request.
reloadtakes the policy text, not a path, so the engine opens no file. It does not authenticateeither: 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 where a reload trigger belongs, are open
questions this PR does not settle — @ferdingler is exploring remote policy sources (AVP) that Box
Core would own, and this signature is compatible with that because the engine only ever receives
text.
Known upstream defect, not introduced here: canonical text is literal, so a reformatted rule counts
as a changed rule and restarts its temporal budget. Three tests in this crate are already ignored
against the
Dogwood1.0.0 gensym instability behind it (issue 59). This answers the policy-identityquestion raised in the 2026-10-20 sync: cosmetic edits do change rule identity, and the failure
direction is permissive.
This crosses the interface-frozen gate (one new
pub fnon the policy crate) and needs explicitapproval.
Split
Review found that the public handle in the previous revision could not survive Box Core moving into
its own process: it carried an
Arc<PolicyEngine>to the caller through a callback, which only workswhile Box Core runs inside the caller's address space. The engine is unaffected — it takes plain
data, so in a two-process topology the text arrives over IPC and the box process calls
reloadonits own engine.
So this PR keeps the engine, and the handle moved to #149 for the revisit. The branch was
force-pushed from
5dc809eto drop the handle, the control-socket, and theSIGUSR1commits;earlier review comments on this PR refer to code that now lives in #149.
Related Issues
None.
Type of Change
New feature
Testing
just check, orcargo test --workspace --all-features)cargo fmt --allandcargo clippy --workspace --all-targets --all-features -- -D warningspolicy.rs::reload_installs_a_new_authored_policy_on_a_live_engineopens an engine on a policy withno 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_forceshows 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 mid-run. That needs a
caller, which is #149. Everything ran on macOS, so the Linux assertions need CI.
Deferred:
reloadreturns(), so there is no version identifier a caller can record for an audittrail. The 2026-10-20 sync liked immutable policy versioning, which wants a return value; it is
easier to add before callers exist, but it is a separate decision.
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.