fix(agent-os): refuse run_tool before executing when post_tool_call is unbound #4428
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #4428"
general
weight: 0.35
prayagupa
Related Issue
Fixes #4424
Problem & Solution
Problem: with
pre_tool_callbound andpost_tool_callnot,AgentControl.run_toolran the tool and only then raisedAgentControlBlockedforpost_tool_callwithruntime_error:intervention_point_unknown. The engine's fail-closed rule for an unbound point is right, but the check came after the side effect. The caller was told the call was blocked while the tool had already run, and a retry could run it again.protect_tooland the SDK adapters that callrun_tool(MCP, LangChain, Semantic Kernel, Foundry, the genericguard_tool) had the same order.Solution: the fail-closed rule stays. In enforce mode
run_toolnow reads the bound points from the runtime client'spolicy_labelsmap (which covers parents of anextendschain) and, once the pre-check permits the call, raises the same block with the same verdict and reason before calling the tool. Therun_toolandprotect_tooldocstrings, the Python SDK README, the quickstart, the tutorial and the policy-engine README manifest now say both tool points must be bound.Behaviour change, in full. Only in enforce mode, only through
AgentControl.run_tool(and soprotect_tooland the SDK adapters that call it: MCP, LangChain, Semantic Kernel, Foundry, genericguard_tool), and only when the runtime client reports bound points (every native-backed constructor:from_native,from_path,from_url,from_manifest_chain): if the merged manifest does not bindpost_tool_call, the tool callable is no longer invoked. The caller still gets the sameAgentControlBlockedforpost_tool_callwith the same verdict (deny, reasonruntime_error:intervention_point_unknown, message "Request blocked by Agent Control Specification.", no policy input, no identities), the samestr(exc), and the same telemetry events (pre_tool_callthenpost_tool_call). Differences for such callers:executeis not called; aSnapshotBuildertool-call slot reserved for the call is released instead of staying charged; thepost_tool_calltelemetry event has a near-zero duration.Everything else is unchanged: a
pre_tool_calldeny still wins and is reported aspre_tool_call; manifests binding both points behave exactly as before; a manifest binding neither still denies atpre_tool_callbefore the tool runs;evaluate_onlymode still runs the tool and returns the engine's unknown-point result; a customRuntimeClientwithoutpolicy_labels(or one that returns an empty map) keeps the old order (tool runs, then the engine's post-check deny);run()with an unboundoutputpoint andevaluate_intervention_pointare untouched; agent-osNativeAdapterRuntimeand its adapters are untouched.Impact on Your Work
A manifest that binds only
pre_tool_callis an easy mistake to make, and until now it let a blocked tool call run anyway. That breaks the promise that a deny stops the action, and a payment, write or send could repeat on retry. This change makes the block happen before the tool runs, so the fail-closed rule holds in practice and not only in the returned verdict.Timeline
None
Alternatives Considered
Docs only: state that both tool points must be bound and leave the order alone. Rejected because the side effect still happens before the block; the docs change is included, but on its own it does not close the gap.
Type of Change
Package(s) Affected
Core & runtime:
Governance & security:
Platform & tooling:
CLI plugins:
Shared / other:
Testing
Unit Testing
New file
policy-engine/sdk/python/tests/test_run_tool_unbound_post.py, 13 tests.Environment: native SDK built with maturin 1.8.7 (
uv venv --python 3.12,maturin develop --releaseinpolicy-engine/sdk/python),ACS_OPA_PATH=/home/mhabuomar/.local/bin/opa.On this branch:
Same file against main's
_orchestration.py(the one file stashed, and again copied into a throwaway worktree oforigin/mainwith the native.socopied over, run withPYTHONPATH=.so main's package loaded, confirmed via__file__):The 4 that fail on main are the bug:
test_pre_only_manifest_blocks_before_the_tool_runs(Lists differ: [{'amount': 100}] != [], the spy recorded the tool call)test_pre_only_manifest_releases_the_tool_call_slot(1 != 0)test_pre_only_manifest_through_protect_tool([{'amount': 5}] != [])CustomClientTests.test_client_reporting_bound_points_blocks_before_the_tool_runs(AgentControlBlocked not raised)The other 9 guard unchanged behaviour and pass on both trees by design: both bound allow, both bound deny at post, pre deny wins, neither bound, chain parent binds post,
extendsparent binds post,evaluate_only, early block equals the engine's own result, custom client withoutpolicy_labelskeeps the old order.Full Python SDK suite:
(Excluding
test_real_packages.py: 318 passed, 3 skipped, 50 subtests passed.)agent-os suite, run because its adapters sit on the same SDK (
agent-governance-python/agent-os,PYTHONPATH=src:../agent-sre/src; agent-sre is needed fortests/test_spec_mcp_gateway_conformance.pyto collect):The 3 failures are the same on both trees and unrelated:
tests/test_new_features_batch2.py::TestSandboxProvider::test_subprocess_echo,test_subprocess_timeout,test_subprocess_failure. The focused agent-os files (tests/test_native_adapter_runtime.py tests/test_adapter_mediation_contract.py tests/test_adapter_enforcement.py tests/test_transform_never_dropped.py): 204 passed, 1 skipped.Manual Testing
A probe script run against both trees (branch package and
origin/mainpackage), outputs diffed:1. Manifest chain,
post_tool_callbound only in the child: tool runs, value returned, both verdicts allow. Same on both.2. Bound only in the parent (
from_manifest_chainand anextendsfile): tool runs. Same on both.3. Bound in neither (parent binds
input, child bindspre): branch blocks atpost_tool_callwithtool.calls == []; main raised the same exception aftertool.calls == [{'amount': 100}].4. No tool points at all: blocked at
pre_tool_callwithruntime_error:intervention_point_unknown, tool never runs. Same on both.5.
post_tool_callbound to an always-deny policy: tool runs, result withheld,AgentControlBlocked(post_tool_call, reason 'nope')with fullpolicy_input. Same on both.6. Tool raises with both bound:
RuntimeErrorpropagates,SnapshotBuildertool_call_countstays 1. Same on both.7. Tool raises with pre-only: branch never calls it (blocked first); main let it raise after running.
8. Pre deny: tool not run, blocked at
pre_tool_callreasonpre_no. Same on both.9.
EVALUATE_ONLY(enum and the string'evaluate_only'): tool runs, post result deny with the unknown reason returned for inspection. Same on both.10. Mode
'enforce'as a string: early block works.11. Pre escalate approved by the resolver, then pre-only: blocked before the tool on branch; after on main.
12. Byte identity: the early block's result equals the engine's own
evaluate_intervention_pointresult for the unbound point (dataclass eq and repr eq both true); exception typeagent_control_specification._types.AgentControlBlocked, MRO[AgentControlBlocked, AgentControlInterruption, RuntimeError],str'Agent Control Specification blocked post_tool_call (runtime_error:intervention_point_unknown).', message'Request blocked by Agent Control Specification.',policy_input None. The only differing field across main and branch in every blocked line istool.calls.13. Telemetry with a sink, pre-only enforce: two events (
pre_tool_callallow withpolicy_id 'tools';post_tool_calldeny,error_class runtime_error,reason_code runtime_error:intervention_point_unknown,policy_id None), identical in every field except duration on both trees.14.
protect_toolwithSnapshotBuilder, pre-only: branch count 0 after the block; main 1.Source review: the engine decides "unknown" by
manifest.intervention_points.get(point)(agent-control-spec 0.4.0-alpha.4runtime.rs:294-301) andpolicy_labelsiterates the same map (sdk/rust/src/host/mod.rs:346-358), so "key present inpolicy_labels" is exactly "bound"; no drift path._bound_points_from_clientreturnsNoneon a missing accessor, an exception, a non-mapping or an empty map, so a custom client keeps the old order. No new imports. The raise sits inside thetrythat releases the builder slot.Static checks:
ruff check src/ --select E,F,W --ignore E501clean in agent-os and on the two changed SDK files; license headers present (new test file has the MIT header);python3 scripts/check_v4_ratchet.pyOK (grand total 4); cspell on added lines 0 issues.Checklist
Attribution & Prior Art
Prior art / related projects (if any):
The analysis of the ordering bug and the pre-only reproduction come from @ricjhill's report in #4424.
AI Assistance
If AI tools materially shaped this change, briefly note what was used:
Claude Code drafted the fix and tests under my direction; an independent verification pass re-ran the tests on main and on the branch and probed the chain and deny cases, and I reviewed the result.
IP, Patents, and Licensing