Skip to content

fix(policy): refuse history stamped ahead of the clock - #410

Merged
priyasingh2111 merged 5 commits into
mainfrom
feature/history-clock-skew-refusal
Nov 12, 2026
Merged

priyasingh2111 merged 5 commits into
mainfrom
feature/history-clock-skew-refusal

Conversation

@priyasingh2111

@priyasingh2111 priyasingh2111 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

Human readable section

Problem: Box keeps a history of what the agent did, with a timestamp on each event, so time-based rules such as "at most 2 writes per hour" still apply after a restart. The policy engine normally refuses a history whose newest timestamp is more than five minutes ahead of the system clock, because that means the clock or the file is wrong. Box had set that limit to unlimited. So if the history file held one event stamped years ahead, the box accepted it at the next start and stamped every later event just after it. All real history then looked years old, time-based limits stopped applying, and nothing warned about it.

Solution: Box now uses the engine's own five-minute limit on every path that opens history. A history stamped further ahead is refused at startup, and the error names the skew. During a run, a request is denied while the gap exceeds five minutes, and decisions return once the clock is within five minutes again. If an action that already ran can't be recorded, Box keeps the record, saves it before the next decision or when the box shuts down, and denies every request until it is saved. So a refused record can't free up a budget, during the run or after a restart. The design pages and the user policy page describe the limit and how to recover.

Trade-off: If the system clock is moved back more than five minutes, the box refuses to start until the clock catches up, or until the operator removes the box directory. Time-zone changes and daylight saving don't move the system clock, which counts UTC seconds, so they don't trigger this. A forged event less than five minutes ahead is still accepted, and it can keep timestamps ahead for up to five minutes.

What we tested:

Test Setup Result
history_stamped_beyond_the_skew_bound_is_refused_by_every_open_path History stamped 1 hour ahead, opened through each open path Refused on every path with the skew error ✅
wall_clock_rollback_within_the_skew_bound_preserves_history_across_every_open_path History stamped 60 s ahead (clock rolled back 60 s) Opens; the spent budget is kept ✅
a_clock_behind_history_beyond_the_skew_bound_denies_until_it_returns_within_it During a run, clock moved 200 s, then 400 s, then 200 s behind history Decided, then denied, then decided again ✅
a_skew_refused_outcome_is_recorded_before_the_next_decision_and_survives_a_restart Request permitted; clock steps 400 s back before its outcome is recorded, then recovers; box restarted Denied while the record is refused; once recorded, the spent budget denies, also after the restart ✅
a_kept_outcome_is_recorded_when_the_engine_shuts_down Same refused outcome; clock corrected, then the box restarts with no request in between The outcome is saved at shutdown; after the restart, the spent budget denies ✅
End to end, built strands-box binary on macOS Budget of 2 commands per hour spent with TZ=America/Chicago 2 permitted, 1 denied ✅
End to end, same box restarted TZ set to America/New_York, Asia/Kolkata, Pacific/Auckland, and UTC Opens each time; budget still spent (denied) ✅
End to end, same box restarted Real macOS time-zone change to New York in System Settings Opens; budget still spent (denied) ✅

Details

durable_config() set with_max_future_skew(Duration::MAX). This disabled the guard in dogwood-local-engine that refuses a durable high-water mark more than five minutes ahead of the wall clock. No decision records a reason for the override.

If private/dogwood.redb holds an event stamped far in the future, for example after an edit while the box is stopped, the next start accepts that timestamp. Each real event then gets that timestamp plus one nanosecond, so the event clock stops. Real history moves out of every within window, and history-gated denials stop with no refusal, warning, or record.

This PR removes the override, so the box uses the engine default of five minutes on every open path. During a run, while the clock is more than five minutes behind the last event, the engine refuses to timestamp the event and the request is denied. Decisions continue once the clock is within five minutes again. An outcome the history refuses is kept on the engine. Each later decision records the kept events first and denies with InternalFault until they append, and Drop for DogwoodEngine records them at shutdown. An event that still can't append at shutdown, or a killed process, loses it; the docs state this.

Decision this depends on. This changes recorded behaviour: before, a box opened after any backward clock step. The PR uses the engine's five-minute default rather than a box-specific bound. If maintainers want more tolerance for clock corrections, a modest named constant is the alternative, never an unbounded one. The entry History is durable per box, docs/design/policy.md, and docs/user/policy.md now state the bound, the refusal, and the remedy.

Upgrade effect. An existing box whose clock moved back more than five minutes in an earlier run already has a high-water mark ahead of wall time. After this change, it refuses to open until the clock catches up or the box directory is wiped.

No frozen interface changes. The history file format does not change.

Related Issues

None public.

Type of Change

Bug fix

Testing

  • history_stamped_beyond_the_skew_bound_is_refused_by_every_open_path (new): a history stamped one hour ahead is refused by every open path (three production paths and the fault-injection variant) with the engine's skew reason. On main, the previous test opened this same history on all four paths, so the new test fails without the fix.

  • a_clock_behind_history_beyond_the_skew_bound_denies_until_it_returns_within_it (new): during a run, a clock 200 s behind history is still decided, 400 s behind denies as an internal fault, and decisions return once the clock is within the bound again. The refusal does not persist.

  • wall_clock_rollback_within_the_skew_bound_preserves_history_across_every_open_path (renamed from wall_clock_rollback_preserves_history_across_every_open_path): a 60-second rollback still keeps the consumed budget on all four open paths.

  • cargo build / test / clippy --all-targets / fmt --check for -p strands-box-policy, with and without --all-features, plus RUSTDOCFLAGS="-D warnings" cargo doc: pass. Clippy shows only the six existing vendored strands-shell warnings.

  • cargo test -p strands-box --test site_manifest --locked --all-features: pass.

  • cargo build -p strands-box --all-features and the containment suite on macOS: pass (129 pass, 12 Linux-only skips).

  • End to end on macOS with the built binary: a box that spent a two-run budget restarted under TZ set to America/New_York, Asia/Kolkata, Pacific/Auckland, and UTC. Each restart opened and kept the budget, so a time-zone change does not trigger the refusal.

  • Not run: Linux targets (no local cross compiler) and the full workspace suite.

  • 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

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

`durable_config()` set `with_max_future_skew(Duration::MAX)`. This disabled the guard in
`dogwood-local-engine` that refuses a durable high-water mark more than five minutes ahead of the
wall clock. No decision records a reason for the override.

If `private/dogwood.redb` holds an event stamped far in the future, for example after an edit
while the box is stopped, the next start accepts that timestamp. Each real event then gets that
timestamp plus one nanosecond, so the event clock stops. All real history moves out of every
`within` window, and history-gated denials stop with no refusal, warning, or record.

## Solution

Remove the override, so the box uses the engine default of five minutes. Every open path uses
`durable_config()`. Thus `open`, the staged opens, and the test-clock open now refuse a history
stamped more than five minutes ahead of the clock. During a run, an append fails while the clock
is more than five minutes behind the last event, so the request is denied. Decisions continue when
the clock catches up.

This changes recorded behaviour: before, a box opened after any backward clock step. The decision
entry "History is durable per box", `docs/design/policy.md`, and `docs/user/policy.md` now state
the bound, the refusal text, and the remedy. No frozen interface changes. The history file format
does not change.

## Tests

- `history_stamped_beyond_the_skew_bound_is_refused_by_every_open_path` (new): a history stamped
  one hour ahead is refused by all four production open paths with the engine's skew reason. On
  `main`, the previous test opened this same history on all four paths, so this test fails without
  the fix.
- `wall_clock_rollback_within_the_skew_bound_preserves_history_across_every_open_path` (was
  `wall_clock_rollback_preserves_history_across_every_open_path`): a 60-second rollback still keeps
  the consumed budget on all four open paths. The previous one-hour case is now the refusal above.
- `cargo build`, `test`, `clippy --all-targets`, and `fmt --check` for `-p strands-box-policy`, with
  and without `--all-features`, and `RUSTDOCFLAGS="-D warnings" cargo doc`: pass. Clippy shows only
  the six existing `strands-shell` vendored warnings.
- `cargo test -p strands-box --test site_manifest --locked --all-features`: pass.
- feature-critic: no blockers. It ran `cargo build -p strands-box --all-features` and the
  containment suite (129 pass, 12 Linux-only skips on macOS).
- Not run: Linux targets, and a test that steps the clock back more than five minutes during a run.
  That behaviour comes from the engine.
@github-actions github-actions Bot added Area - policy-engine bug Something isn't working labels Nov 11, 2026
@priyasingh2111
priyasingh2111 marked this pull request as ready for review November 12, 2026 01:55
Comment thread crates/policy/src/dogwood.rs Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Request Changes

Removing the Duration::MAX override closes the forged future timestamp gap at open. But the in-run refusal also drops outcome records, which turns a host clock step during an in-flight effect into a budget bypass (see the inline comment, reproduced with a test).

Checks that held
  • Every open path (normal, staged, descriptor, fault-injection) refuses history more than 5 min ahead, and the error carries the engine's reason. The quoted policy staging failed: policy staging history failure: … prefix matches error.rs.
  • Within the bound, a rollback still keeps the consumed budget (hwm + 1ns), and a time-zone change does not move the clock.
  • A request refused for skew denies as InternalFault (deny-ward), and decisions resume once the clock is back within the bound.
  • No frozen surface changes: the pub use façade, the history file format, dogwood-local-engine = "=1.0.0", and the trusted system wall clock in production are untouched.
  • crates/policy/AGENTS.md "Never discard a record error": held. Callers warn, but see the inline comment for the consequence.

The upgrade effect and the bound are clearly written down in the decision entry and the user docs.

Comment thread crates/policy/src/dogwood.rs Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Comment

e34dff2 closes the in-run budget bypass: a lost outcome now makes the engine deny every later request in that run. One gap remains. The flag doesn't survive a restart, and the docs point operators to a restart as the remedy (inline on outcome_lost).

Checks that held
  • All three outcome paths (observe_outcome, observe_response, observe_tool_response) set the latch on any submit error: skew, I/O, poisoned lock, or exhausted offset.
  • Every request decision goes through decide_recording, including refine_tool_call, which passes InternalFault through rather than turning it into the coarse allow.
  • commit_staged_under changes the engine in place, so the latch survives MCP restaging. Production never replaces the engine; only tests do.
  • docs/design/policy.md now separates a refused request (not persistent) from a lost outcome (denies the rest of the run). The earlier contradiction is gone.
  • From the previous review, still true: every open path refuses history more than 5 min ahead; a rollback within the bound keeps the budget; no frozen surface or version pin changed.

Comment thread docs/user/policy.md Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Comment

201d73a fixes the restart gap from my last review. A refused outcome is now kept and recorded before the next decision, so it counts after a restart once it has been saved. One ordering is still open: the operator corrects the clock and then restarts before any request arrives (inline on docs/user/policy.md).

Checks that held
  • Outcome events can't be permanently refused, so the queue can't block the run forever. BadEvent only applies to decision kinds; outcome kinds return Recorded. The only errors left are clock skew and log I/O, and both clear up.
  • The retried event is stamped at its append time, which is no earlier than the real effect, so the result stays deny-ward.
  • Locks are always taken in the same order (pending outcomes, then engine), and nothing holds the engine lock while taking the pending lock, so this can't deadlock.
  • Every outcome path (observe_outcome, observe_response, observe_tool_response) goes through record_outcome, and every request goes through the flush in decide_recording.
  • The design docs (policy.md, decisions.md) and the new test match the in-run and restart-after-save behaviour.

@github-actions

Copy link
Copy Markdown

Assessment: Approve

4908777 fixes the ordering I flagged last time: the operator corrects the clock, then restarts with no request in between. impl Drop for DogwoodEngine now records any kept outcomes. a_kept_outcome_is_recorded_when_the_engine_shuts_down covers this exact order and passes locally. The user doc and design doc now say what the code does. I have no new findings.

Checks that held
  • The flush in Drop runs before the engine's fields drop, so the durable engine is still open when kept events append.
  • The real box shutdown reaches Drop. BrokerHost::drop joins the broker thread, the broker joins its client threads, and join_jobs joins the discovery threads. So the last Arc<PolicyEngine> drops when the run ends, including after SIGTERM or SIGHUP.
  • A poisoned lock, or an event that still can't append at shutdown, only prints a warning and doesn't panic in Drop. The docs list it as lost, along with a killed run.
  • Staging engines and transient engines have an empty queue, so their drop does nothing.
  • The earlier checks still hold. Outcome kinds can't hit BadEvent, so the queue can't block the run forever. A retried event is stamped at append time, which errs toward denying. Locks are always taken in the order pending outcomes, then engine.

@priyasingh2111
priyasingh2111 merged commit c846ed0 into main Nov 12, 2026
26 of 28 checks passed
@priyasingh2111
priyasingh2111 deleted the feature/history-clock-skew-refusal branch November 12, 2026 19:41
ferdingler added a commit that referenced this pull request Nov 13, 2026
…against the upstream Shell

The vendored Shell files that #397 changed are deleted here. #397's Shell tests
move to crates/shell/tests/main_shell_semantics.rs and the broker tests, and its
two Lua stdin tests move to the broker. The tests for #397's Shell fixes fail
until the upstream Shell fixes land.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area - policy-engine bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants