Skip to content

Humanize tool names in agent fallback responses (#1092) - #1127

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1092-humanize-tool-names
May 17, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1092-humanize-tool-names

Conversation

@malibio

@malibio malibio commented May 17, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Adds humanize_tool_name() in packages/agent/src/local_agent/agent_loop.rs mapping every supported tool (create_schema, update_node, search_semantic, etc.) to user-facing prose.
  • Routes both agent fallback paths through it: the post-tool empty-response synthesis (line ~298) and the max-iterations bullet summary (line ~471).
  • Deduplicates bullets in the max-iterations fallback so repeated calls to the same tool produce one line, and unknown identifiers fall back to a generic "the requested action" so a raw name can never reach the UI.

Test plan

  • cargo test -p nodespace-agent --lib agent_loop:: — 23/23 pass, including the updated max_iteration_limit test that now asserts the fallback contains no raw tool identifier
  • bun run test — 128 files / 3918 tests pass
  • bun run quality:fix — clean (eslint, svelte-check, cargo fmt, clippy)

Test status vs baseline

Test files Tests
Baseline (main) 128 passed 3918 passed
This branch 128 passed 3918 passed

Closes #1092

🤖 Generated with Claude Code

When the model fails to produce text alongside tool calls — either at
max iterations or after a final tool round — the agent loop synthesizes
a fallback message so the chat UI never goes blank. Previously these
messages embedded raw internal identifiers like `create_schema` and
`update_node`, surfacing implementation detail to end users.

Map known tool names to user-facing phrases (e.g. "schema creation",
"node update") and route unknowns through a generic "the requested
action" so no raw identifier can leak. The max-iterations branch also
deduplicates the bullet list since repeated calls to the same tool
produced repeated bullets.

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

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

malibio commented May 17, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This PR cleanly addresses issue #1092: raw tool identifiers no longer leak into either fallback message path. The implementation is small, well-contained, and well-tested. The humanizer table covers all 13 tools currently dispatched by GraphToolExecutor::execute (verified against packages/agent/src/local_agent/tools.rs:1392-1408), and the unknown-tool fallback ("the requested action") is a sensible safety net.

The change is a net positive and meets the acceptance criteria. I have one structural concern about long-term drift between the dispatch table and the humanizer, and a few smaller observations below — none are blockers.

Verdict: Approve with one improvement suggestion (drift-prevention test) and a couple of nits.


Findings

🟢 Critical Issues

None.

🟡 Suggested Improvements

  • 📁 packages/agent/src/local_agent/agent_loop.rs:46-66 — Drift risk between humanizer and dispatcher (DRY / single source of truth).
    The match in humanize_tool_name and the match name in GraphToolExecutor::execute (tools.rs:1392-1408) must be kept in lockstep. If a new tool is added to the executor and not to the humanizer, the chat UI silently degrades to "the requested action" for that tool — no compile error, no test failure. Consider one of:

    1. A unit test in agent_loop.rs that iterates over all_tool_definitions() (already exposed) and asserts humanize_tool_name(def.name) does not return the generic fallback. This is a 5-line drift detector and the cheapest fix.
    2. Move the mapping closer to the tool definitions in tools.rs — e.g., add a display_label: &'static str field on ToolDefinition so the label lives next to the schema. Heavier change, but eliminates the dual-source-of-truth entirely.

    Option 1 is the pragmatic minimum and I'd recommend it before merge. Underlying principle: a _ wildcard arm masks omissions; protect against that with a structural test.

  • 📁 packages/agent/src/local_agent/agent_loop.rs:1244-1254 — Test couples to the exact humanized string ("node search") (test brittleness).
    Asserting on the literal "node search" couples this test to the specific phrasing in the humanizer table. A future rephrase ("searching nodes", "node lookup", etc.) would break this test even though behavior is correct. The invariant the test should encode is "the raw identifier is gone", not "this specific phrase is present". Consider tightening to two regex-style checks:

    assert!(!result.response.contains('_'), ...);            // no snake_case leakage
    assert!(!result.response.contains("search_nodes"), ...); // no raw identifier

    The humanizer's own unit tests (humanize_tool_name_known_tools) already pin the specific phrases — that's the right place for phrase-level assertions. Underlying principle: test the invariant, not the implementation detail.

  • 📁 packages/agent/src/local_agent/agent_loop.rs:471-487 — Dedup is a behavior change worth confirming (correctness / information loss).
    Previously the max-iterations fallback emitted one bullet per execution (e.g., 5 search_nodes calls → 5 bullets). Now it emits one bullet per unique humanized label. The new behavior is almost certainly nicer for the user, but it does drop the signal that the agent retried the same tool repeatedly — which is exactly the diagnostic signal that this code path (max-iterations exhausted) is trying to surface. Worth confirming the trade-off is intentional. If you want both, a count suffix is cheap:

    format!("• {} completed ({}×)", label, count)

    when count > 1. Underlying principle: fallback messages exist precisely for the unhappy path; that's where diagnostic detail matters most.

  • 📁 packages/agent/src/local_agent/agent_loop.rs:474 — Vec<&'static str> for dedup is O(n²) (minor).
    Negligible at n=5 (the iteration cap), and Vec::contains on small &'static str is fine. Flagging only because if MAX_TOOL_ITERATIONS ever rises, a HashSet<&'static str> or IndexSet would preserve insertion order with O(1) lookup. Not worth changing now — KISS wins at this size. Nit-adjacent, not a request.

🟢 Nits

  • Nit: 📁 packages/core/src/mcp/handlers/schema.rs:1021 — Unrelated blank-line removal. It's a clear cargo fmt cleanup and harmless, but it widens the diff for a file unrelated to issue Humanize tool names in max-iterations fallback response #1092. Going forward, prefer keeping incidental formatting fixes in a separate housekeeping commit so the PR diff stays focused on the issue. Accept as-is for this PR.

  • Nit: 📁 packages/agent/src/local_agent/agent_loop.rs:55 — "related-node lookup" is the only hyphenated label and reads slightly awkwardly in a bullet ("• related-node lookup completed"). "related node lookup" (no hyphen) or "related nodes lookup" would scan more naturally. Minor copy polish.

  • Nit: 📁 packages/agent/src/local_agent/agent_loop.rs:60 — "task status update" is grammatically fine but verbose next to its peers ("node update", "schema update"). Consider just "task update" for consistency. Either way is defensible.

  • Nit: 📁 packages/agent/src/local_agent/agent_loop.rs:42-46 — The doc comment is already great. Optional addition: a single line noting "Keep in sync with GraphToolExecutor::execute in tools.rs" would give future maintainers a breadcrumb. Even better paired with the drift-detector test suggested above.


Scope check

The author extended the fix to the post-tool empty-response synthesis path at agent_loop.rs:296-302 in addition to the max-iterations path named in the issue. This is the right call — the same class of leak existed in both paths, and splitting it would have left a known-broken path in the codebase. Pragmatic Quality: a net improvement, atomic in purpose (humanize tool names in fallback responses), not scope creep.


Recommendation

Approve with one suggested follow-up: add the drift-detector test (Improvement #1) before merge. The remaining improvements and nits are optional polish.

@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.

Net positive change that meets issue #1092's acceptance criteria. See review comment for one suggested follow-up (drift-detector test) and minor polish nits — none are blockers. Full report: #1127 (comment)

- Add `humanize_tool_name_covers_all_registered_tools` drift detector that
  iterates `all_tool_definitions()` and asserts every registered tool has a
  non-generic humanized mapping. Without it, adding a new tool would silently
  degrade the chat UI to "the requested action". Made `all_tool_definitions`
  `pub(crate)` so the test can reach it.

- Replace the brittle "node search" assertion in `max_iteration_limit` with
  invariant checks (no underscores, no raw `search_nodes` identifier). The
  phrase-level assertions live in `humanize_tool_name_known_tools` where they
  belong.

- Preserve the retry diagnostic signal in the max-iterations fallback: repeated
  calls to the same tool now collapse to one bullet with a "(N×)" count
  suffix, so the message stays terse while still showing the agent looped on
  the same operation. The test now asserts the count is present.

- Add a doc-comment breadcrumb pointing at `tools.rs` and the drift test.

- Copy polish: "task status update" → "task update" (consistent with peers),
  "related-node lookup" → "related node lookup" (drops the awkward hyphen).

Skipped per reviewer guidance:
- O(n²) `Vec::contains` dedup — reviewer flagged as not worth changing at n=5.
- The unrelated `cargo fmt` blank-line cleanup in `schema.rs` — already in
  the prior commit; reviewer said accept as-is.

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

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

malibio commented May 17, 2026

Copy link
Copy Markdown
Collaborator Author

Review feedback addressed in 8b87050

✅ Addressed (4 substantive + 3 nits)

🟡 Drift detector test — Added humanize_tool_name_covers_all_registered_tools (packages/agent/src/local_agent/agent_loop.rs:1303-1316). Iterates all_tool_definitions() and asserts every registered tool maps to a non-generic phrase. Made all_tool_definitions pub(crate) so the test can reach it. New tool added without a humanizer arm → compile-passing test failure.

🟡 Test brittleness — max_iteration_limit now asserts the invariant (no underscores, no raw search_nodes identifier) instead of the exact phrase "node search". Phrase-level assertions stay in humanize_tool_name_known_tools.

🟡 Dedup loses retry signal — Repeated calls to the same tool now collapse to one bullet with a (N×) count suffix. The test asserts the count is present (MAX_TOOL_ITERATIONS × in the response). Diagnostic signal preserved, message stays terse.

🟢 Doc breadcrumb — Added "Keep arms in sync with GraphToolExecutor in tools.rs. The humanize_tool_name_covers_all_registered_tools test asserts that every registered tool has a non-generic mapping."

🟢 Copy polish — "task status update" → "task update" (consistent with "node update", "schema update"). "related-node lookup" → "related node lookup" (drops the awkward hyphen).

⏭️ Skipped (2)

🟡 O(n²) Vec::contains dedup — Reviewer explicitly flagged as "not worth changing now — KISS wins at this size". At MAX_TOOL_ITERATIONS = 5 the constant factors swamp the asymptotics. Would revisit if the iteration cap rises into the dozens.

🟢 Unrelated blank-line cleanup in schema.rs — Already in the prior commit; reviewer said "Accept as-is for this PR". Will keep incidental formatting in separate housekeeping commits going forward.

Verification

  • cargo test -p nodespace-agent --lib agent_loop:: — 24/24 pass (including the new drift detector and updated max_iteration_limit)
  • bun run test — 128 files / 3918 tests pass
  • bun run quality:fix — clean (eslint, svelte-check, cargo fmt, clippy)

🤖 Generated with Claude Code

@malibio
malibio merged commit faa235f into main May 17, 2026
@malibio
malibio deleted the worktree-issue-1092-humanize-tool-names branch May 17, 2026 14:26
malibio added a commit that referenced this pull request Jun 22, 2026
* Humanize tool names in agent fallback responses (#1092)

When the model fails to produce text alongside tool calls — either at
max iterations or after a final tool round — the agent loop synthesizes
a fallback message so the chat UI never goes blank. Previously these
messages embedded raw internal identifiers like `create_schema` and
`update_node`, surfacing implementation detail to end users.

Map known tool names to user-facing phrases (e.g. "schema creation",
"node update") and route unknowns through a generic "the requested
action" so no raw identifier can leak. The max-iterations branch also
deduplicates the bullet list since repeated calls to the same tool
produced repeated bullets.

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

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

* Address review feedback on #1127

- Add `humanize_tool_name_covers_all_registered_tools` drift detector that
  iterates `all_tool_definitions()` and asserts every registered tool has a
  non-generic humanized mapping. Without it, adding a new tool would silently
  degrade the chat UI to "the requested action". Made `all_tool_definitions`
  `pub(crate)` so the test can reach it.

- Replace the brittle "node search" assertion in `max_iteration_limit` with
  invariant checks (no underscores, no raw `search_nodes` identifier). The
  phrase-level assertions live in `humanize_tool_name_known_tools` where they
  belong.

- Preserve the retry diagnostic signal in the max-iterations fallback: repeated
  calls to the same tool now collapse to one bullet with a "(N×)" count
  suffix, so the message stays terse while still showing the agent looped on
  the same operation. The test now asserts the count is present.

- Add a doc-comment breadcrumb pointing at `tools.rs` and the drift test.

- Copy polish: "task status update" → "task update" (consistent with peers),
  "related-node lookup" → "related node lookup" (drops the awkward hyphen).

Skipped per reviewer guidance:
- O(n²) `Vec::contains` dedup — reviewer flagged as not worth changing at n=5.
- The unrelated `cargo fmt` blank-line cleanup in `schema.rs` — already in
  the prior commit; reviewer said accept as-is.

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

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

---------

Co-authored-by: Michael Libio <[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.

Humanize tool names in max-iterations fallback response

1 participant