Skip to content

Convert Claude Code or Codex into the most intelligent Deep Research Agent. Collect, search, and synthesize web research into a persistent, searchable wiki that builds on itself. Hosted API + MCP: hyperresearch.ai

fix: send the PreToolUse reminder as hookSpecificOutput JSON (#94) (#97)

Wu Shuwenfddb4d7e5a3b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0930177..b4e5611 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
## [Unreleased]
+- **The PreToolUse reminder now reaches the model (#94).** The installed hook wrote its reminder to stderr and exited 0, and for PreToolUse that channel is discarded — so the "check the research base before searching the web" nudge shipped since 0.x had never once been delivered. It is emitted as `hookSpecificOutput.additionalContext` now, which is the documented injection channel. The matcher is narrowed to `WebSearch|WebFetch`: on `Glob` and `Grep` the advice is noise, and delivering it there for the first time would have made every local vault operation pay for a reminder about the web.
+
### Scholarly discovery: eight sources through one client layer
Academic discovery used to be four URL templates rendered into the agent's instructions by `core/agent_docs.py`, which the model was trusted to assemble and call by hand. No retry, no rate limiting, no dedup, no offline tests — and one of those templates shipped `[email protected]`, a shared placeholder on every install, which is exactly the anti-pattern the open-access resolver refuses to commit for Unpaywall. It is now a real package.
diff --git a/src/hyperresearch/core/hooks.py b/src/hyperresearch/core/hooks.py
index 59ba264..03e618a 100644
--- a/src/hyperresearch/core/hooks.py
+++ b/src/hyperresearch/core/hooks.py
@@ -3540,7 +3540,14 @@ if (vault) {{
'',
'For multiple URLs, use subagents to fetch in parallel.',
].join('\\n');
- process.stderr.write(msg + '\\n');
+ // stderr reaches the model only on exit 2. On exit 0 it goes to the debug
+ // log, so the reminder has to leave as hookSpecificOutput JSON on stdout.
+ process.stdout.write(JSON.stringify({{
+ hookSpecificOutput: {{
+ hookEventName: 'PreToolUse',
+ additionalContext: msg
+ }}
+ }}) + '\\n');
}}
"""
@@ -3723,8 +3730,12 @@ def _install_claude_hook(vault_root: Path, hpr_path: str) -> str | None:
if "hyperresearch" in h.get("command", ""):
return None
+ # Web tools only. The reminder is "check the vault before you search the
+ # web"; on Glob and Grep it is noise, and now that the payload actually
+ # reaches the model (it was silently discarded before #94), every match
+ # costs context on every call.
pre_tool.append({
- "matcher": "Glob|Grep|WebSearch|WebFetch",
+ "matcher": "WebSearch|WebFetch",
"hooks": [{
"type": "command",
"command": f'node "{hook_path.as_posix()}"',
diff --git a/tests/test_core/test_hooks.py b/tests/test_core/test_hooks.py
index fca5134..45d8327 100644
--- a/tests/test_core/test_hooks.py
+++ b/tests/test_core/test_hooks.py
@@ -3,7 +3,12 @@
from __future__ import annotations
import json
+import os
import shlex
+import shutil
+import subprocess
+
+import pytest
from hyperresearch.core.hooks import (
_RETIRED_AGENT_FILES,
@@ -21,6 +26,7 @@ from hyperresearch.core.hooks import (
_install_source_analyst_agent,
_install_width_critic_agent,
_prune_retired_agents,
+ _write_hook_script,
install_hooks,
)
@@ -387,6 +393,45 @@ def test_prune_retired_agents_spares_a_dir_with_no_skill_file(tmp_vault):
assert (mine / "scratch.txt").exists()
+# ---------------------------------------------------------------------------
+# PreToolUse hook — the reminder must reach the model (#94)
+# ---------------------------------------------------------------------------
+
+
+def test_installed_hook_delivers_reminder_through_the_injection_channel(tmp_vault):
+ """Exit 0 + hookSpecificOutput JSON on stdout is the only channel that
+ reaches the model. stderr at exit 0 is written to the debug log, so a hook
+ that writes the reminder there has no effect at all."""
+ script = _write_hook_script(tmp_vault.root, "hyperresearch").read_text(encoding="utf-8")
+
+ assert "hookSpecificOutput" in script
+ assert "hookEventName: 'PreToolUse'" in script
+ assert "additionalContext" in script
+ assert "process.stderr.write" not in script
+
+
[email protected](shutil.which("node") is None, reason="node is not installed")
+def test_installed_hook_emits_parseable_injection_json(tmp_vault):
+ """Run the installed script the way Claude Code does, and read the channel
+ the model reads."""
+ hook_path = _write_hook_script(tmp_vault.root, "hyperresearch")
+
+ proc = subprocess.run(
+ ["node", str(hook_path)],
+ capture_output=True,
+ text=True,
+ env={**os.environ, "CLAUDE_PROJECT_DIR": str(tmp_vault.root)},
+ timeout=60,
+ )
+
+ assert proc.returncode == 0, proc.stderr
+ assert proc.stderr == ""
+ output = json.loads(proc.stdout)["hookSpecificOutput"]
+ assert output["hookEventName"] == "PreToolUse"
+ assert "HYPERRESEARCH" in output["additionalContext"]
+ assert "hyperresearch fetch" in output["additionalContext"]
+
+
# ---------------------------------------------------------------------------
# install_hooks — end-to-end integration
# ---------------------------------------------------------------------------