Skip to content

feat(policy): reload an authored policy over a live authority - #122

Merged
hanaduy merged 1 commit into
mainfrom
feature/policy-reload-signal-demo
Oct 22, 2026
Merged

hanaduy merged 1 commit into
mainfrom
feature/policy-reload-signal-demo

Conversation

@hanaduy

@hanaduy hanaduy commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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::reload installs an authored policy set over the live authority:

pub fn reload(&self, sources: &[Policy]) -> Result<(), PolicyStagingError>
pub struct Policy { pub origin: PathBuf, pub text: String }

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 installed policy keeps deciding every request.

reload takes the policy text, not a path, so the engine opens no file. It does not authenticate
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 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 Dogwood 1.0.0 gensym instability behind it (issue 59). This answers the policy-identity
question 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 fn on the policy crate) and needs explicit
approval.

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 works
while 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 reload on
its own engine.

So this PR keeps the engine, and the handle moved to #149 for the revisit. The branch was
force-pushed from 5dc809e to drop the handle, the control-socket, and the SIGUSR1 commits;
earlier review comments on this PR refer to code that now lives in #149.

Related Issues

None.

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-policy reload   # the two tests below, <1s
just check                                # the full gate

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 mid-run. That needs a
caller, which is #149. Everything ran on macOS, so the Linux assertions need CI.

Deferred: reload returns (), so there is no version identifier a caller can record for an audit
trail. 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

  • 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.

@hanaduy
hanaduy requested a review from zastrowm October 19, 2026 18:56
@ferdingler

Copy link
Copy Markdown
Collaborator

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.

@hanaduy

hanaduy commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

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.

@hanaduy

hanaduy commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

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.

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 SIGUSR1 handler, leaving only PolicyEngine::reload(&self).

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.

@hanaduy
hanaduy force-pushed the feature/policy-reload-signal-demo branch from 05d603c to 3a81c05 Compare October 21, 2026 22:35
@hanaduy hanaduy changed the title feat(policy): reload an authored policy over a live authority [box] Reload a running box's policy through a library handle Oct 21, 2026
@hanaduy hanaduy changed the title [box] Reload a running box's policy through a library handle feat(box): reload a running box's policy through a library handle Oct 21, 2026
@ferdingler

Copy link
Copy Markdown
Collaborator

The engine half looks great, PolicyEngine::reload is what we need to support future use cases. However, the way you've written run_with_handle makes it so that we cannot move to Box Core running on a separate process. The caller gets an Arc<PolicyEngine> through a callback, so it only works while Box Core runs inside the caller's own process. And I believe we've agreed we need to move run into its own process sometime soon.

What I'd propose is splitting this PR in two:

  1. One PR for the PolicyEngine::reload and its tests.
  2. Revisit the public handle approach to ensure it works for when run lives in a separate process.

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.
@hanaduy
hanaduy force-pushed the feature/policy-reload-signal-demo branch from 5dc809e to 0e108f1 Compare October 22, 2026 03:37
@hanaduy hanaduy changed the title feat(box): reload a running box's policy through a library handle feat(policy): reload an authored policy over a live authority Oct 22, 2026
@hanaduy

hanaduy commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

The engine half looks great, PolicyEngine::reload is what we need to support future use cases. However, the way you've written run_with_handle makes it so that we cannot move to Box Core running on a separate process. The caller gets an Arc<PolicyEngine> through a callback, so it only works while Box Core runs inside the caller's own process. And I believe we've agreed we need to move run into its own process sometime soon.

What I'd propose is splitting this PR in two:

1. One PR for the `PolicyEngine::reload` and its tests.

2. Revisit the public handle approach to ensure it works for when `run` lives in a separate process.

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.

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 Arc<PolicyEngine> can't survive run moving out of process, so I've parked
#149 rather than guess at a cross-process shape

Both the process question and whether Box Core ends up owning the policy
source change what the handle should be, so I'd rather design it after seeing that than before.

@hanaduy
hanaduy marked this pull request as ready for review October 22, 2026 17:19
@hanaduy
hanaduy enabled auto-merge (squash) October 22, 2026 17:37
@hanaduy
hanaduy merged commit 8c46dac into main Oct 22, 2026
29 of 30 checks passed
@hanaduy
hanaduy deleted the feature/policy-reload-signal-demo branch October 22, 2026 17:51
.map_err(|error| PolicyStagingError::Durable(error.to_string()))?;
proposed.mark_all_ready();
live.readiness = Readiness::Ready;
live.pending_tools = BTreeSet::new();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(),
));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
///

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()))?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

Copy link
Copy Markdown

Issue: This PR adds a new pub fn to the policy crate's frozen surface, and the description itself says it "crosses the interface-frozen gate ... and needs explicit approval." Per the SDK API bar-raising process this needs a designated reviewer, but the only label on the PR is strands-running — there's no api/needs-review (or api/review-complete) label.

Suggestion: Add the api/needs-review label so the interface addition gets the explicit API sign-off the freeze requires before merge.

@github-actions

Copy link
Copy Markdown

Assessment: Comment

The engine-only shape (reload takes plain &[Policy] text, serialises on the authority's own live lock in the same proposal→live order as stage_mcp_schema) is sound and the two-process concern from the earlier review is addressed. My feedback is mostly about tests pinning the claims and a few edge cases, not the core design.

Review themes
  • Interface freeze: New pub fn on the frozen policy surface lacks the api/needs-review label for explicit sign-off.
  • Untested claims: The central budget-continuity behaviour and the strict-validation refusal path aren't pinned by tests; the one refusal test stops at the parse stage.
  • Edge cases: Discovering/degraded_tools interaction with a reload is undefined, and the not-opened-for-staging case reuses the Durable error variant for a caller-state error.
  • Nit (non-blocking): reload takes &[Policy] while open/open_staged take Vec<Policy>.

Nice to see the serialisation and lock-ordering reasoning spelled out in the comments — it made the concurrency review straightforward.

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.

2 participants