Repository navigation
perf(agent): offer tools in admission order so the prompt cache survives a skill load - #4036
Conversation
Verdict: Approve with suggestions — after the tool-selection eval runsTools are now offered to the model in the order they were admitted instead of being re-sorted alphabetically, so pulling in one bundle mid-answer appends to the end of the list rather than shuffling everything after it. That is the right fix for the cache-loss problem the description measures, and the selection logic itself is untouched — the same tools get picked, they just keep their place. One thing to settle before merge: this changes the order the tool list is sent to the model, and model behaviour is sensitive to ordering. The test plan already lists the agent eval as the outstanding item, unchecked. That run is the gate here, not optional — the unit tests pin the ordering invariant, but nothing yet shows the model still picks the same tools when they arrive in a different sequence. Worth also capturing the after-number for the cache hit rate on the same task the description measures before: that is the proof the fix worked, and right now only the before-number exists. Everything else is small. The escape hatch now deliberately lets the tool count run over its cap for the rest of a turn, which is a reasonable trade — trimming mid-turn is exactly the cost this PR removes — and the next turn puts it back. The behaviour is documented in the plan and the changelog, and the affected tests were updated rather than deleted. Real-world evidenceN/A — no evidence bundle was produced for this PR, and this change touches no CLI, API, MCP or Agent-UI surface directly; its observable surface is the agent's prompt and cost. My verdict rests on static review of the diff plus reading the new and updated tests against the current loader source. I did not execute the test suite (no pytest in this review environment), so the test-plan checkmarks are the author's evidence, not mine. The one piece of evidence I would want and that does not exist yet is the 🔍 Technical details🟡 ImportantRequired eval not yet run for an LLM-affecting change. gaia eval agent --category tool_selection --agent-type doc
gaia eval agent --compare tests/fixtures/eval_baselines/<model>/scorecard_tool_selection.json <run-dir>/scorecard.jsonThe description's cache measurement (4,276 / 12,545 prompt tokens reused) is a before-number; the matching after-number on the same task would close the loop on the perf claim too. 🟢 Minor
"Bounded (one bundle per call)" is a per-call bound, not a per-turn one ( Module-level Strengths
|
The turn's tool subset is picked from the user's message before the first
model call. A skill the model loaded during that turn joined the prompt but
not the tool list, so the tool its recipe names stayed missing until the next
turn - and a one-turn task has no next turn. After loading github-triage
("run every GitHub command through run_shell_command") the agent had no shell
tool and ran `gh` through run_python instead: 3 of 3 GitHub tasks that loaded
the skill in a 14-task benchmark run. For a user that is an approval prompt
per call, where the skill's grant needs none.
load_skill now adds the skill's tools_required and its own tools to an
active tool subset, the same way the load_tools escape hatch already widens
it mid-turn. Agents without a subset are unchanged, and a required tool the
agent does not have is not invented.
…offered list Re-sorting moved every tool after the new ones, and on a template that renders tools first that re-prefilled the whole prompt: 4,276 of 12,545 tokens cached on the step after a skill load, measured on the benchmark.
…ves a skill load One mid-turn `load_tools` used to cost a near-full re-prefill of the prompt. The offered tool list was re-sorted alphabetically on every admission, so adding one tool moved every tool after it — and several chat templates render the tool list *before* the system prompt, which makes it the cached prefix. On a measured GitHub task only 4,276 of 12,545 prompt tokens came back from cache on the step after a skill load; fresh input tokens are 53% of a run's cost. Tools are now offered in admission order — the always-on core in registry order, then each later admission appended where it happened — and never re-sorted, so a new tool only adds bytes at the end. Eviction moved to the turn boundary: load_bundle is add-only and may briefly exceed dynamic_tools_max, which the next select trims LRU-first, survivors keeping their relative order. Which tools get selected is unchanged; only their order, and when the list shrinks. test_load_bundle_is_cap_aware_and_protects_just_loaded became test_load_bundle_overshoots_the_cap_then_the_next_turn_trims — it pinned the mid-turn eviction this replaces.
e9ccb83 to
e1b8e39
Compare
# Conflicts: # src/gaia/agents/base/agent.py
# Conflicts: # src/gaia/agents/base/agent.py
|
Merged One conflict, and it was the interesting kind: 🔍 Technical detailsConflict was a single hunk in the class-attribute block of Tests:
|
|
Confirmed the token-budget failures are not yours, and #4038 is the fix. Nothing to do here. Ran the file on 🔍 Technical detailsBisected the tool-count change to |
# Conflicts: # hub/agents/gaia/npm/CHANGELOG.md
…literal The load_tools log line hardcoded "evicted": [] and "skipped_at_cap": []. That is accurate while the path is add-only, which is the point -- it looks right, so if eviction ever comes back here the log would keep reporting that none happened, and the eval's recall parser reads these fields. Reads them off the scratch object instead. The new test drives eviction onto the path and asserts the payload carries it; reverting the two fields to literals fails it.
|
Unblocked: this is mergeable and clean against main, and #4021 is mergeable too, so the stack is no longer stuck — merge order is the only remaining constraint. The Fixed the one real defect: the escape-hatch log claimed nothing was ever evicted by writing that in as a constant rather than reading it. Harmless today, because that path genuinely evicts nothing — which is exactly why it would survive review if eviction ever came back, and the eval's recall parser reads those fields. The eval is still the open gate, and I could not close it here. No inference backend is reachable on this machine, so 🔍 Technical detailsThe log fix ( New test Stack state: #4021 Tests: 193 passed across On the eval. Nothing reachable on 8000, 13305 or 4200, and no other eval is running (checked, per the one-eval-per-backend rule). Two
Worth noting before anyone runs it: these predate the flagship becoming the default Not addressed (the review's remaining 🟢 items): the |
Loading a skill mid-answer used to throw away the model's prompt cache. The tool list the model is sent was re-sorted alphabetically every time a tool was admitted, so pulling in one tool moved every tool after it — and the model's cache is a prefix match, with several chat templates putting the tool list ahead of the system prompt. On a measured GitHub task, only 4,276 of 12,545 prompt tokens came back from cache on the step after a skill load. Fresh input tokens are 53% of a run's cost, so that is real money and real latency on the exact turns where the agent is working hardest. Tools are now offered in the order they were admitted and appended, never re-shuffled, so a mid-turn load only adds bytes at the end and everything already sent stays cached. Which tools get selected is unchanged.
Test plan
python -m pytest tests/unit/test_tool_admission_order.py tests/unit/test_tool_loader_selection.py tests/unit/test_chat_dynamic_tools.py tests/unit/test_dynamic_tool_filtering.py tests/unit/test_chat_tool_bundles.py tests/unit/test_tool_recall.py tests/unit/test_adaptive_skills.py hub/agents/chat/python/tests -qpython -m pytest tests/unit -q— no new failures against this machine's known-failing set (profilespec goldens, tool-loader token budget, hub installer, diarize, coding starter skill, connectors/registry import).python util/lint.py --black --isort --flake8— black clean; the two isort failures (src/gaia/connectors/api.py, two integration tests) are pre-existing and untouched here.gaia eval agent --category tool_selectionshows no regression against the baseline. This changes the order tools appear in the prompt, so it needs a run on a machine with a backend.🔍 Technical details
What sorted, and what it does now
ToolLoader.selectreturnsorted(self._loaded)list(self._loaded)— dict insertion order is admission orderToolLoader.selectCORE tiersorted(self._core)ToolLoader.load_bundlesorted(members), returnedsorted(self._loaded)list(self._loaded)Left alone on purpose: the semantic tier's
(-score, name)tie-break (changing it would change which tool wins the last slot at the cap, not just the order),bundle_names()(a menu in the prompt's stable prefix), theTOOL_LOADERlog's diagnostic set keys, andtool_recall.py's union (offline scoring, order-irrelevant).Eviction moved to the turn boundary.
load_bundleis now add-only and may exceeddynamic_tools_maxfor the rest of the turn — bounded by one bundle per call, logged with anover_capcount.selecttrims the overshoot LRU-first before admitting, and adictdelete leaves survivors in order, so the list only loses the evicted entries. Mid-turn eviction was the worst case of the original bug: it dropped a tool from the middle of the offered list.Renderers were already fine.
_build_openai_tool_schemas(filter_to=…)and_format_tools_for_prompt(filter_to=…)both iteratefilter_toin the given order; only their docstrings claimed the list was pre-sorted._refresh_active_tool_filter's!=was already an ordered list comparison, so an unchanged set with the same order still recomputes nothing — there is now a test pinning both halves of that.Changed assertion.
test_load_bundle_is_cap_aware_and_protects_just_loaded→test_load_bundle_overshoots_the_cap_then_the_next_turn_trims. It pinned the mid-turn eviction this PR replaces; the new test asserts the add-only list and that the nextselectrestores the cap.Adjacent, not fixed here: #4021 adds
_offer_skill_tools, which returnssorted({*current, *missing})— the same alphabetical re-shuffle, on the mid-turn skill path. Whichever of the two lands second should switch it to append.