Skip to content

fix(hermes): close the rewrite's stdin and make its timeout configurable - #3833

Merged
KuSh merged 7 commits into
rtk-ai:developfrom
Hubujiu:fix/hermes-modify-directive
Nov 1, 2026
Merged

KuSh merged 7 commits into
rtk-ai:developfrom
Hubujiu:fix/hermes-modify-directive

Conversation

@Hubujiu

@Hubujiu Hubujiu commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The Hermes plugin keeps rewriting the terminal payload in place and returns no modify directive. That reaches execution on every Hermes release tested, from v2026.7.30 to v0.22.5, and leaves other plugins' modify directives in charge of the keys they set, command included.

  • rtk rewrite runs with stdin closed (subprocess.DEVNULL) instead of inheriting the host's.
  • The rewrite timeout comes from RTK_HERMES_TIMEOUT (5 s default, previously a fixed 2 s). Missing, malformed, non-positive or non-finite values fall back to the default.
  • hooks/hermes/README.md and hooks/README.md describe the hook contract, why no directive is returned, and how the timeout relates to Hermes' own plugins.hook_callback_timeout.
  • The installed-flow test no longer reads RTK_HERMES_TIMEOUT from the caller's environment.

Test plan

  • python3 -m unittest discover -s hooks/hermes -v (19 tests, including the installed flow)
  • cargo fmt --all --check
  • cargo clippy --all-targets
  • cargo test --all

@CLAassistant

CLAassistant commented Sep 2, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@hsbsidd hsbsidd left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tested and approved on a live Hermes deployment.
Independently reviewed and validated this PR end-to-end.

Independently validated this PR end-to-end against a live Hermes deployment. The proposed fix works for me.

Environment

  • RTK v0.48.0
  • Hermes Agent v0.21.0 (2026.8.31)
  • Oracle Linux 9.8 / aarch64
  • Rust/Cargo 1.98.1
  • PR commit tested: f8f4c2d

Validation

  1. Checked out the PR commit directly and reviewed the diff against develop.
  2. Ran the Hermes test suite with Cargo available:

Ran 16 tests in 48.538s OK

This included test_cargo_init_installs_importable_plugin_that_rewrites_with_fake_rtk.
3. Copied the PR's hooks/hermes/rtk-rewrite/__init__.py into the live Hermes plugin directory and verified it was byte-for-byte identical to the PR version before restarting the gateway.
4. Restarted the Hermes gateway successfully.
5. From a live WhatsApp Hermes session, asked Hermes to run git status. The terminal invocation was rewritten to:

rtk git status

  1. rtk gain independently recorded the invocation. The tracked rtk git status count increased from 1 to 2 after the PR version was loaded.

Live Hermes terminal call was successfully rewritten through RTK. I had previously reproduced the underlying issue locally with the {"action": "modify", "args": {"command": rewritten}} workaround.

Also verified this PR's more complete {**args, "command": rewritten} implementation works in a live Hermes gateway while preserving other terminal arguments covered by the regression test. This provides live Linux/aarch64 validation in addition to the existing unit/contract testing.

Thanks for the fix!

@KuSh

KuSh commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Two small additions to the same subprocess.run call, coming from #4387 (a Hermes user who patched them locally after seeing rtk: hermes plugin warning: rtk rewrite timed out on a live gateway):

  1. stdin=subprocess.DEVNULL: the rtk rewrite child currently inherits the gateway's stdin. rtk rewrite does not read it, so nothing breaks today, but the hook has no reason to hand the host's stdin to a subprocess.
  2. The timeout: timeout=2 is hardcoded and the reporter hit it in practice. Reading it from an env var such as RTK_HERMES_TIMEOUT, with a 5s default and a fallback to that default on a missing or malformed value, matches what they run locally.

A run.call_args assertion in test_rewrite_returns_modify_directive_without_mutating_terminal_args (or a small new test) would pin both, plus one test for the env override and its fallback.

@Hubujiu

Hubujiu commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@KuSh I've addressed these in commit 6f91b82: added stdin=subprocess.DEVNULL, made RTK_HERMES_TIMEOUT configurable with a 5-second default and fallback for invalid values, and added the requested assertions and tests. The README is updated too. The changes are already pushed to this PR and ready for another look.

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Round 1 — CHANGES REQUESTED

Claim: the Hermes plugin returns a pre_tool_call modify directive for terminal rewrites instead of only mutating the payload, closes the rewrite subprocess's stdin, and reads its timeout from RTK_HERMES_TIMEOUT (5 s default).
Scope: accept. #4093 carries the same Hermes change (opened later, alongside its OpenClaw work) and #4527 edits the same subprocess.run call; this one lands first.
Ran: real Hermes v2026.7.30, v2026.8.13, v2026.8.16, v2026.8.31 and v0.22.2 (run from source in Docker) × rtk develop f16becacd vs this PR merged onto it. Each run did rtk init --agent hermes, then a terminal git status through model_tools.handle_function_call and through the AIAgent loop against a stub model (quiet, verbose, two parallel calls). Also: a second plugin returning modify × 2 releases; RTK_HERMES_TIMEOUT × 6 values through Hermes; Hermes hook_callback_timeout below the rewrite time. Invariants: every decision, under the real host dispatch, non-default config. Savings n/a (no filter change).
Pins: 6 decision areas, 14 mutations, 0 survived the suite.
Matrix: executed this round; pin check: executed this round.
CI @6f91b82: all green (11/11).

Blocks merge (1, frozen at round 1)

  1. hooks/hermes/rtk-rewrite/__init__.py:85: the directive's shape does not match how Hermes applies a pre_tool_call result. It has two consequences, both regressions against develop (develop mutates args["command"] in place):

    • Hermes before v2026.8.16 ignores modify. Those releases only read block/approve from the return value and run the payload they passed in. On v2026.7.30 and v2026.8.13, develop's plugin rewrites: the tracking DB records git status → rtk git status and the model sees * master / ?? f1.txt …. This PR's plugin runs raw git status (On branch master / Untracked files: …) with no warning, on every path and mode above. From v2026.8.16 on, both rewrite.
    • {**args, "command": rewritten} resends the original value of every other key. Hermes merges modify directives in hook order, so this undoes an earlier plugin's change. With a plugin that returns {"action": "modify", "args": {"workdir": "/work/repo2"}} and sorts before rtk-rewrite, on a call {"command": "git status", "workdir": "/work/repo"}: develop runs in repo2, as that plugin asked. This PR runs back in repo (v2026.8.16 and v0.22.2).

    A correct fix passes these rows: the rewrite applies (rtk git status tracked) on all five releases above, in handle_function_call and the agent loop; and the other plugin's workdir survives on v2026.8.16 and v0.22.2. Tests: test_rewrite_returns_modify_directive_without_mutating_terminal_args and the installed-flow test currently pin the payload as untouched and the directive as carrying every key. They need to fail when the payload is not updated and when the directive carries keys other than command. The README sentence "The original payload stays unchanged" changes with it.

Optional — will not hold merge

  • The installed-flow test reads RTK_HERMES_TIMEOUT from the contributor's environment. RTK_HERMES_TIMEOUT=1e-9 python3 -m unittest discover -s hooks/hermes fails test_cargo_init_installs_importable_plugin_that_rewrites_with_fake_rtk with {'action': 'modify', …} != None. The same setUp as RtkRewritePluginTest fixes it.

  • The README accepts "any positive, finite" RTK_HERMES_TIMEOUT, but two ranges do not behave that way:

    • At or above Hermes' plugins.hook_callback_timeout (30 s by default), Hermes fails closed. With its cap below the rewrite time, the terminal call returns {'error': 'pre_tool_call plugin callback timed out or is still running'}, on develop and on this PR alike.
    • Very large values: RTK_HERMES_TIMEOUT=31536000 makes subprocess.run raise timeout is too large on every call (warning, no rewrite).

    Clamping below the cap, or saying so in the README, would cover both.

  • hooks/README.md:309-316 still shows timeout=2 and the in-place assignment as the whole mechanism.

Filed as follow-ups

  • CI runs none of the plugin test suites that live outside cargo (hooks/hermes unittest, hooks/opencode/rtk.test.mjs, hooks/claude/test-rtk-rewrite.sh, hooks/copilot/test-rtk-rewrite.sh). That is pre-existing, so the pins above only bind when someone runs them by hand. Filed as #4588.

Checked and correct — no need to re-verify

  • On v2026.8.16, v2026.8.31 and v0.22.2 the directive applies: rtk git status is tracked and the output is compact, in handle_function_call and in the agent loop (quiet, verbose, parallel).
  • stdin=subprocess.DEVNULL is passed, and the call assertion pins it.
  • RTK_HERMES_TIMEOUT through Hermes v0.22.2:
    • unset, abc and -3 fall back to 5 s and the rewrite applies;
    • 10 rewrites;
    • 0.000001 prints rtk: hermes plugin warning: rtk rewrite timed out and the original command runs.
  • The parsing rules (nan, inf, 1e999, 0, negatives, whitespace, read per call, not truncated) each have a test that fails when the rule is removed.
  • Passthrough and error paths still return None; asserted, and a mutation that makes them return a directive is caught.
  • Merge onto develop f16becacd is clean. cargo fmt --check, cargo clippy --all-targets and cargo test --all (49 suites) exit 0, and the Hermes unittest suite passes 18/18, including the installed flow.
  • One data point on #3797: develop's in-place rewrite does apply on v2026.8.31 (the release named there) in both dispatch paths above. If the failure came from another entry point (a gateway platform adapter, for instance), naming it would let a test cover that path.

Your questions

  • None open; the stdin and timeout asks from the earlier comment are in and verified above.

Next

Make the rewrite reach both the payload that Hermes before v2026.8.16 executes and the modify merge of later releases, with the directive carrying only command. The rows under "Blocks merge" pass, with the two tests updated to pin it. After that, this merges.
Threads: 1 open (the line comment below), 0 resolved this round.

Comment thread hooks/hermes/rtk-rewrite/__init__.py Resolved
Resolve the hooks README table conflict by retaining Oh My Pi and the corrected Hermes contract. No upstream workflow or Rust changes beyond the merge.

Validation: fmt, workspace clippy and workspace tests pass (4,954 tests, 8 existing ignored); Hermes Python suite passes 18/18 both normally and with inherited RTK_HERMES_TIMEOUT=1e-9.
@Hubujiu

Hubujiu commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

@KuSh I've addressed the Hermes compatibility feedback: the hook now updates args["command"] for older Hermes and returns a command-only modify directive for newer versions, so it won't overwrite another plugin's workdir. The tests and hook documentation have been updated accordingly.
I also reproduced the Git 2.56 failure in a_clustered_h_is_answered_by_git_once, then merged your fix from #4578 into a separate validation branch in my fork. All 14 CI jobs now pass, including both Windows targets on Git 2.56. The MSVC job needed one rerun after a crates.io download error.
Fork CI: https://github.com/Hubujiu/rtk/actions/runs/38843489295
The #4578 fix is only in the fork validation branch; I haven't added it to this PR. This confirms that the remaining test failure is resolved by #4578.

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Round 2 — CHANGES REQUESTED

Claim: the Hermes plugin returns a pre_tool_call modify directive for terminal rewrites, closes the rewrite subprocess's stdin, and reads its timeout from RTK_HERMES_TIMEOUT (5 s default).
Scope: accept (round 1, frozen).
Ran: real Hermes v2026.7.30, v2026.8.13, v2026.8.16, v2026.8.31 and v0.22.2 (0.19.1 to 0.22.2, run from source in Docker) × rtk develop 5538d72b1 vs this PR merged onto it. Each run did rtk init --agent hermes, then a terminal git status through the AIAgent loop against a stub model (quiet, verbose, two parallel calls). Also: a second plugin, sorting before rtk-rewrite, that returns modify for workdir and for command, on 4 releases; RTK_HERMES_TIMEOUT × 6 values. Invariants: every decision, under the real host dispatch, non-default config. Savings n/a (no filter change).
Pins: 6 decision areas, 15 mutations, 0 survived the suite.
Matrix: executed this round; pin check: executed this round.
CI @b2e7ce8: all green (15/15).

Blocks merge (1, frozen at round 1; +1 regression found this round)

  1. The round-1 item is fixed. The rewrite applies on all five releases in every mode (rtk git status tracked, compact output), and an earlier plugin's workdir change survives on v2026.8.16 and v0.22.2. Thread resolved.

  2. hooks/hermes/rtk-rewrite/__init__.py:88: the modify directive replaces a command another plugin chose. Round 1's code had this too and my round-1 matrix only varied workdir, so this one is mine to own. Hermes hands every pre_tool_call hook the original payload and merges their modify directives in hook order. A directive carrying command therefore wins over an earlier plugin's command, and it carries rtk's rewrite of the original command. With a plugin that returns {"action": "modify", "args": {"command": "git status --porcelain"}} for git status and sorts before rtk-rewrite:

    • Develop: that plugin's command runs (?? in_repo.txt) on v2026.8.16, v2026.8.31 and v0.22.2.
    • This PR: it runs rtk git status instead (* master / ?? in_repo.txt).

    On v2026.8.13 the two behave the same, since modify is ignored there. Develop loses rtk's rewrite in that setup, which is a safe failure; this PR drops the other plugin's change, which can be a sandbox or safety rewrite. The plugin cannot see the earlier directive, so no shape of a command directive avoids this.

    A correct fix passes these rows:

    • the rewrite applies on all five releases above, in every mode;
    • the earlier plugin's workdir survives on v2026.8.16 and v0.22.2;
    • the earlier plugin's command runs on v2026.8.16 and v0.22.2, as on develop;
    • the stdin and timeout rows below are unchanged.

    The in-place update this push added already passes the first row on every release I can run, including v2026.8.31, the release #3797 names. Without the directive it passes all four, and the tests then need to fail if a directive carrying command comes back. If #3797 came from an entry point where the in-place update does not reach the terminal tool, that is the input this row set is missing; round 1 asked which one it was, and naming it would settle whether a directive is needed at all.

Optional — will not hold merge

  • hooks/hermes/README.md: plugins.hook_callback_timeout only exists from v2026.8.31 (Hermes 0.21.0); v2026.7.30 to v2026.8.16 have no callback limit. The "keep it below" sentence and the fail-open line could say so.
  • The version boundary is given as a date tag only. hermes --version reports 0.20.1 for v2026.8.13 and 0.20.2 for v2026.8.16, so naming both would let a user place their install.

Filed as follow-ups

  • None this round (#4588 from round 1 still covers the plugin suites missing from CI).

Checked and correct — no need to re-verify

  • The in-place update and the command-only directive are each pinned. Dropping the assignment, resending all keys, returning no directive, updating a copy, or leaving command out of the directive each fail test_rewrite_updates_payload_and_returns_command_only_directive.
  • Behaviour that matches develop, since develop also mutates the payload in place:
    • later hooks see the rewritten command;
    • a worker that Hermes abandons after its callback timeout can still write the payload.
  • The installed-flow test no longer reads RTK_HERMES_TIMEOUT from the environment.
  • hooks/README.md now shows the current timeout, stdin and success path.
  • RTK_HERMES_TIMEOUT through Hermes v0.22.2:
    • unset, abc and -3 fall back to 5 s and the rewrite applies;
    • 10 rewrites;
    • 0.000001 warns rtk rewrite timed out and runs the original command;
    • 31536000 warns timeout is too large, as the README now says.
  • Merge onto develop 5538d72b1 is clean. cargo fmt --check, cargo clippy --all-targets and cargo test --all (50 suites) exit 0, and the Hermes unittest suite passes.

Your questions

  • No question in the last comment. Thanks for the Git 2.56 check: #4578 is on develop now, and CI is green on this head.

Next

Keep the rewrite reaching the payload on every release without overriding another plugin's command: the four rows under item 2. On what I could run, that is the in-place update alone, with stdin and timeout as they are. If #3797's entry point needs the directive, naming it is what decides between the two.
Threads: 1 open (item 2), 1 resolved this round (round 1's item, fixed).

Comment thread hooks/hermes/rtk-rewrite/__init__.py Resolved
@KuSh KuSh self-assigned this Oct 20, 2026
@levinkin

Copy link
Copy Markdown

Confirmed on Hermes Agent v0.22.5 (macOS and Ubuntu) with rtk 1.0.1 and 1.1.0. The plugin installed by rtk init -g --agent hermes only mutates args in place, so Hermes runs the original command and no rewrite applies (see #3797). Returning {"action": "modify", "args": {"command": rewritten}}, as this PR does, fixes it: after that one-line change, git status comes back as rtk git status output in a live session.

The agent table still listed a command-only modify directive the plugin no
longer returns. The README also spelled out how Hermes orders and merges
hook results, which is Hermes behaviour; it now states only what the plugin
does, and that from Hermes v2026.8.16 another plugin's modify directive
setting `command` replaces the rewrite. Hermes enforces
`plugins.hook_callback_timeout` from v2026.8.27 (0.20.6), not v2026.8.31.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KuSh KuSh changed the title fix(hermes): return modify directives for terminal rewrites fix(hermes): close the rewrite's stdin and make its timeout configurable Nov 1, 2026

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Round 3 — APPROVED, follow-up filed

Claim: the Hermes plugin closes the rtk rewrite subprocess's stdin and reads its timeout from RTK_HERMES_TIMEOUT (5 s default), and keeps rewriting the terminal payload in place without returning a directive.
Scope: accept (round 1, frozen). The modify directive the original claim was about is gone from the head. #3797 is closed: the in-place rewrite reaches execution on current Hermes. I retitled the PR and rewrote its description to match the head. Maintainer PRs: checked against #4458, #4402 and #4269; each merges clean, and both test sets pass on each merge.
Ran: real Hermes v2026.7.30, v2026.8.13, v2026.8.16, v2026.8.31, v0.22.2 and v0.22.5 (0.19.1 to 0.22.5, run from source in Docker) × rtk develop 550a5c13d vs this PR merged onto it. Each run did rtk init --agent hermes, then a terminal git status through the AIAgent loop against a stub model (quiet, verbose, two parallel calls). Also: a second plugin, sorting before rtk-rewrite, that returns modify for workdir and for command (v2026.8.16, v0.22.5); RTK_HERMES_TIMEOUT × 6 values on v0.22.5. Invariants: every decision, under the real host dispatch, non-default config. Savings n/a (no filter change).
Pins: 6 decision areas, 14 mutations, 0 survived the suite.
Matrix: executed this round; pin check: executed this round.
CI @19339fd: all green (15/15)

Blocks merge (1 from round 1, 1 from round 2, both fixed)

  1. Round 1, older Hermes: fixed. The rewrite applies on all six releases in every mode (rtk git status tracked, compact output).
  2. Round 2, command override: fixed in f72f7c8. An earlier plugin's workdir and command directives both win, as on develop (?? in_repo.txt for the command case on v2026.8.16 and v0.22.5). Thread resolved.

Filed as follow-ups

  • #4912 llm-proxy identity probe can panic inside ureq when the peer drops the connection: outside this PR's hunks. It is the source of the one red macOS job on f72f7c8 (probe_rejects_a_stranger_on_the_port, ureq 2.12.1's expect at response.rs:403).

Checked and correct — no need to re-verify

  • The in-place-only shape is pinned. Each of these fails test_rewrite_updates_payload_without_returning_a_directive and the new composition test:
    • dropping the assignment;
    • returning a command directive or a full-args directive;
    • updating a copy.
  • The round-1 stdin and timeout mutations are still caught.
  • RTK_HERMES_TIMEOUT through Hermes v0.22.5:
    • unset, abc and -3 fall back to 5 s and the rewrite applies;
    • 10 rewrites;
    • 0.000001 warns rtk rewrite timed out and runs the original command;
    • 31536000 warns timeout is too large.
  • Merge onto develop 550a5c13d is clean. cargo fmt --check, cargo clippy --all-targets and cargo test --all (61 suites) exit 0. The Hermes unittest suite passes 19/19, including the installed flow, and stays green with RTK_HERMES_TIMEOUT exported.

Your questions

  • None open since round 2.

Next

Fixup 19339fd pushed, docs only and code-reviewed:

  • hooks/README.md: the agent table row is back to "In-place mutation" (it still listed the directive).
  • hooks/hermes/README.md states what the plugin does and that another plugin's modify setting command replaces the rewrite (v2026.8.16 and later), rather than describing Hermes' merge internals.
  • The host callback timeout is dated from v2026.8.27 (Hermes 0.20.6), not v2026.8.31. That date came from my round-2 note, which was wrong.

Approving. Thank you for carrying this through three rounds.
Threads: 0 open, 1 resolved this round (round 2's, fixed in f72f7c8).

@KuSh
KuSh merged commit 4b4bc40 into rtk-ai:develop Nov 1, 2026
15 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Nov 1, 2026
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.

5 participants