Repository navigation
fix(tools): run middleware layers exactly once and honour pre-delegation vetoes [K-01] - #4
Merged
Conversation
…ailures as vetoes [K-01]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The tool middleware pipeline (
tools/middleware.py) now calls the tool handler exactly once per tool call, and a middleware that rejects a call before delegating is treated as a veto instead of being swallowed.Why
MiddlewareChain.execute()wrapped each layer intry/except Exceptionand, on error, fell through tocall_at(index + 1, ...)— i.e. it re-ran the rest of the chain, including the terminal handler:Because a middleware sits around
next_call, the handler's own exception bubbles back up into every enclosing layer'sexcept, and each one replays the downstream chain:2 ** len(middlewares)handler invocations. With the default 4-middleware chain (coerce_arguments_middleware,ssrf_guard_middleware,logging_middleware,special_tools_middleware,core.py:588-589) a single raising tool runs 16 times — side effects (messages sent, files written, commands run, money spent) happen 16 times, andlogging_middlewarerecords 15 extra "failed" rows in working memory.Measured on unfixed code with the default chain:
The same fallback also inverted the guards:
ssrf_guard_middlewareraisesValueError("SSRF violation: ...")before delegating, which the fallback logged and then continued past, so the blocked call executed anyway:What changed
tools/middleware.py—call_atnow tracks whether the layer delegated vianext_call:Middleware <name> vetoed tool <tool>: ...) and propagates to the caller. Nothing downstream runs, sossrf_guard_middlewareandcoerce_arguments_middlewareactually gate the call.core.pyis untouched:Agent._execute_toolalready catches the exception and returnsTool error (<name>): <reason>to the model, so a veto now surfaces as a tool error instead of a silent execution.tests/test_tool_middleware.py— 11 new tests (parametrised chain depths included).Provenance
Mechanism and edge cases reviewed against
hermes:agent/tool_executor.py(upstream middleware runner: at-most-once layer execution, veto-vs-handler-failure distinction). Re-implemented in koza's own style — koza names, koza log messages, no upstream import, no foreign identifiers.Verification
(8 pre-existing tests + 11 new; all green, none skipped.)
Before/after probe, default 4-middleware chain, handler raising:
Real
Agent._execute_toolpath (probe script, stub agent + realDEFAULT_MIDDLEWARESand realALL_HANDLERSentry whose handler raises):ruff check tools/middleware.py tests/test_tool_middleware.py: 10 findings, down from 11 onmain— all pre-existing style rules (UP035/RUF013/UP006/PERF102/SIM102/PIE810/S110/BLE001), none introduced. Repo-wideruff check .reports 1997 pre-existing findings and is unchanged by this PR.Risk / rollback
Behaviour change: a middleware failure that previously let the tool run now surfaces as
Tool error (...)(fail-closed). For the shipped middlewares this is the intended semantics —is_safe_url()inskills/web.pyalready catches its own exceptions and returnsFalse, so the SSRF guard only raises on a genuine violation;coerce_tool_args()is defensive and returnsargsunchanged when it cannot coerce;logging_middlewarefails after the handler has run and only re-raises. Error-handling middleware that intentionally catches a downstream failure and returns a fallback (a supported pattern, covered by a test) is unaffected. Rollback: revert this single commit — no config, schema, storage or interface change.Note for the owner:
tests/is listed in.gitignore(line 63) whiletests/test_custom_base_url.pyandtests/test_telegram_auto_delegation.pyare tracked, so this test file had to be added withgit add -f. Worth removing that ignore line separately (K-19 / K-52 territory) so future tests are not silently dropped.Roadmap: K-01