fix: inject available cache_uri hints into MCP tool error envelopes - #46
Merged
Conversation
When an MCP tool returns an `{error: "..."}` envelope, the engine now
scans the last 10 role=tool messages backward, harvests `_cache_uri` per
upstream tool name (deduped, most-recent-first), and appends
`available_cache_uris: [{tool_name, cache_uri}, ...]` to the LLM-visible
envelope before pushing into history.
Fixes the observed underperformance pattern: NRDS query → create_plotly_chart
fail → re-query NRDS → retry chart. Small/quantized models reliably drop
`_cache_uri` values from attention by the time they see a chart-tool
failure, so they defend by re-querying instead of reusing the cached
pointer. With the new field, the URI lives in immediate error-envelope
context — no scrollback required.
Wire-up:
- Inline-list-cap site (engine/index.js:934): enriched.
- Dispatch path (:1270): enriched BEFORE the truncation block so
oversized error envelopes preserve the field through summary build.
- Cache-miss site (:957): NOT enriched (`_missing_uris` already names
the failing URIs verbatim; peer-row enrichment would add no signal).
- Short-circuits when cacheOptions.enabled !== true (URI cannot exist).
Plan: docs/plans/2026-05-28-003-fix-error-envelope-cache-uri-hint-plan.md
Tests: 19 new in engine/cacheUriHint.test.js (11 helper + 7 integration +
1 invariant). Full suite: 698/698 across 43 files.
Bumps to 0.16.1-beta.0 on the beta dist-tag.
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
Closes a real underperformance pattern observed against the workshop stack: NRDS query →
create_plotly_chartfail → re-query NRDS → retry chart. The wasted upstream round-trip costs 5–30s on local Ollama models.Root cause (from /ce:debug):
create_plotly_chart(engine-side cap, MCP-server Pydantic validation, or any{error: "..."}shape) tells the LLM abstractly to usedata_uri, but does NOT include the URI value._cache_uri.Fix: Engine-side enrichment. When a tool returns
{error: "..."}, scan the last 10role: "tool"messages backward, harvest_cache_uriper upstream tool name (deduped, most-recent-first), and appendavailable_cache_uris: [{tool_name, cache_uri}, ...]to the LLM-visible envelope. The URI now lives in immediate error-envelope context — no scrollback required.Wire-up
engine/index.js:934:1270:957_missing_urisalready names the failing URIs verbatimCritical ordering: the dispatch-site enrichment runs BEFORE the truncation block at
:1179. The truncation summary at:1257-1264then explicitly copiesavailable_cache_urisfromresultForLlmalongside_cache_uri. Without this ordering, oversized error envelopes would silently drop the field — exactly the case where the LLM most needs the hint (HIGH-severity feasibility finding from document-review on the plan).Short-circuits:
cacheOptions.enabled !== true→ URI cannot exist; zero JSON.parse work.typeof envelope.error !== "string"→ only enrich error envelopes.Test plan
engine/cacheUriHint.test.js(happy paths, malformed JSON, scan window, dedup, short-circuits, defensive overwrite, mutation invariant).processToolCalls(inline-cap wire-up, dispatch error wire-up, truncation + error for HIGH feasibility finding, success path unchanged, first-turn empty history,enableResultCache=falsehost).cache.test.js).Release
Version bump:
0.16.0-beta.0→0.16.1-beta.0(patch bump on beta line, additive engine behavior, zero API surface change for hosts).After merge:
npm publish --tag betafrom a fresh checkout.Plan
docs/plans/2026-05-28-003-fix-error-envelope-cache-uri-hint-plan.md— reviewed via headlessdocument-review(1 HIGH feasibility, 1 HIGH coherence, 4 MODERATE + 2 LOW auto-fixed before code).