Skip to content

feat: add scriptable tool result transform hook#6291

Merged
houko merged 1 commit into
librefang:mainfrom
pavver:feat/scriptable-transform-tool-result
Jun 23, 2026
Merged

feat: add scriptable tool result transform hook#6291
houko merged 1 commit into
librefang:mainfrom
pavver:feat/scriptable-transform-tool-result

Conversation

@pavver

@pavver pavver commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add a transform_tool_result hook to ContextEngine so context engines can rewrite tool output before it is written into session history.
  • Wire the hook into the agent tool-call path after the existing HookEvent::TransformToolResult hook and before artifact spill, sanitization, injection guarding, and session commit.
  • Expose the hook through ContextEngineHooks for scriptable/manual/plugin configuration.
  • Include transform_tool_result in scriptable hook validation, metrics, traces, plugin hook path checks, and plugin integrity checks.
  • Make build_context_engine create a scriptable layer when any declared scriptable hook exists, including transform_tool_result, assemble, compact, subagent hooks, and on_event.

Hook Contract

The script receives:

{
  "type": "transform_tool_result",
  "agent_id": "...",
  "tool_name": "...",
  "tool_use_id": "...",
  "input": {},
  "content": "...",
  "is_error": false,
  "status": "completed"
}

To rewrite the model-visible tool result content, it returns:

{
  "content": "rewritten tool output"
}

If content is missing or not a string, the original tool result is kept.

If the hook fails under the default warning policy, the original tool result is kept.

Why

This gives per-agent context engines a deterministic pre-ingest cleanup point for noisy tool outputs.

For example, a Rust coding agent can trim cargo check or cargo test output with Rust-specific rules before that output enters conversation history, while an Android/Kotlin agent can use different Gradle/logcat cleanup rules. Agents that do not configure the hook keep the existing behavior.

Verification

  • cargo test -p librefang-runtime transform_tool_result
  • cargo test -p librefang-runtime hook_integrity_all_hook_fields_checked
  • cargo check -p librefang-types -p librefang-runtime
  • cargo clippy -p librefang-types -p librefang-runtime --all-targets -- -D warnings
  • git diff --check

Out Of Scope

  • No changes to the existing Rust HookEvent::TransformToolResult contract.
  • No LLM summary request protocol changes.
  • No built-in language-specific output cleaners.

@github-actions github-actions Bot added size/L 250-999 lines changed area/docs Documentation and guides area/runtime Agent loop, LLM drivers, WASM sandbox labels Jun 23, 2026
@pavver
pavver force-pushed the feat/scriptable-transform-tool-result branch from af96864 to afe34c8 Compare June 23, 2026 20:07
@houko
houko merged commit 8589d49 into librefang:main Jun 23, 2026
31 checks passed
houko added a commit that referenced this pull request Jun 24, 2026
…nbreak Windows CI (#6306)

* fix(test): gate scriptable transform-hook tests behind cfg(unix) to unbreak Windows CI

The #6291 transform_tool_result hook tests execute a real #!/bin/sh script through the native script runtime. Windows has no /bin/sh, so the spawn fails and the metrics assertions don't hold, failing both Windows test shards and turning main red (#6304) — every open PR then inherits the failure on its next run.

Gate the four shell-script tests and their make_transform_script / make_transform_engine helpers (plus the now-test-only MemorySubstrate / ToolExecutionStatus imports) behind #[cfg(unix)]. The transform-hook feature is unchanged and still covered on Linux and macOS.

* style(test): remove redundant import comment; collapse multi-line comment to one line

---------

Co-authored-by: Evan <[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

area/docs Documentation and guides area/runtime Agent loop, LLM drivers, WASM sandbox size/L 250-999 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants