Skip to content

fix(agent-hooks): treat Kimi <hook_result> envelopes as harness-injected - #14807

Open
VXNCXNX wants to merge 1 commit into
stablyai:mainfrom
VXNCXNX:fix/branch-name-kimi-hook-result-envelope
Open

VXNCXNX wants to merge 1 commit into
stablyai:mainfrom
VXNCXNX:fix/branch-name-kimi-hook-result-envelope

Conversation

@VXNCXNX

@VXNCXNX VXNCXNX commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

ELI5

When you type a request to Kimi, its hook sends Orca a second, robot-written message right after yours: <hook_result hook_event="UserPromptSubmit">{}</hook_result>. Orca already knows how to ignore robot messages like this, but it was only recognising the ones written with dashes (<system-reminder>), not the ones with underscores. So the robot message overwrote what you actually typed, and the branch got named after the robot message instead of your task.

What Changed

src/shared/harness-injected-user-turns.ts:

  • the leading-tag matcher now allows _ as well as -, so <hook_result ...> is seen as a tag at all
  • hook_result added to KNOWN_HARNESS_TAG_NAMES

Both are needed: the regex alone would not classify it, and the allowlist entry alone is unreachable because the regex never matches the name.

Why

resolvePrompt in agent-hook-listener.ts keeps the cached prompt when isKnownHarnessInjectedUserTurnText returns true. That classifier's tag matcher was /^<([a-z][a-z0-9-]*)(?:[\s>]|$)/, which cannot match hook_result, so the envelope was treated as a genuine user turn and replaced the real prompt. buildBranchNamePrompt then received the envelope as firstPrompt.

I kept the allowlist approach rather than matching underscore tags generically, because the file's own comment warns that Grok wraps real typed prompts in <user_query>. Treating the underscore shape itself as machinery would misclassify those. Only the name that was actually observed is added, and there is a test asserting <user_query> and <my_widget> still count as user turns.

Linked Issue

Fixes #14789

Visual Proof

N/A - no UI or interaction change. The only visible difference is the generated branch name, which is model output and therefore not stable enough to screenshot meaningfully. The behavioural change is shown by the test below instead.

Testing

Reproduced through the listener before fixing, using the existing Kimi test harness. A typed prompt followed by the envelope:

before: prompt = '<hook_result hook_event="UserPromptSubmit">{}</hook_result>'
after:  prompt = 'debug the CI config'

Two tests added:

  • agent-hook-listener-claude-compatible-vendors.test.ts - the end-to-end one, next to the existing Kimi harness-injection test. Asserts the cached prompt survives the envelope.
  • harness-injected-user-turns.test.ts - classifier level, including the <user_query> / <my_widget> non-regression.

Each half of the fix was reverted independently; both mutations fail both tests:

AssertionError: expected '<hook_result hook_event="UserPromptSubmit">{}</hook_result>' to be 'debug the CI config'
AssertionError: expected false to be true

vitest run src/shared/ is 4772 passed, 2 failed. Both failures are pre-existing and unrelated (automation-schedules expects Sundays and gets Sonntags on a non-English locale; external-automation-jobs-file expects a different error string). Both fail identically with my change stashed. All three typecheck projects and oxlint src/shared/ are clean.

Platforms: pure string classification, no platform, path, shell, or network behaviour. Tested on Linux.

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

AI Disclosure

Written with AI assistance (Claude Code, Opus 5). I reproduced the bug through the listener, confirmed the two pre-existing test failures are unrelated by stashing, and ran the mutation checks.

Review

  • Security: none. No new input is trusted; the change makes Orca trust less transcript content.
  • Cross-platform: no platform-dependent code paths touched. Pure string matching.
  • Remote SSH: harness-injected-user-turns.ts is in src/shared/, so local and SSH targets get identical classification, which is the point of it living there.
  • Mobile: not touched.
  • Backwards compatibility: no wire format, RPC param, or persisted shape changes. A previously-misclassified turn is now correctly dropped from prompt caching; nothing that was cached before becomes unreadable.
  • Agent/provider neutrality: hook_result is added to the same observed-shapes allowlist every other harness uses, not behind a Kimi-specific branch, so any harness emitting the same envelope benefits. Grok's <user_query> is explicitly protected by test.
  • Performance: unchanged. Same single regex against the same 256-char bounded head; one more entry in a Set lookup.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Author

  • X / Twitter: N/A

The tag matcher did not allow underscores, so the Kimi UserPromptSubmit
envelope was never classified as machinery and replaced the user's real
prompt. Branch auto-naming then anchored on the envelope instead of the task.

Fixes stablyai#14789
@coderabbitai

coderabbitai Bot commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: fcab482b-fb1a-406b-9863-3ce2e75902d5

📥 Commits

Reviewing files that changed from the base of the PR and between 35b308f and 758e880.

📒 Files selected for processing (3)
  • src/shared/agent-hook-listener-claude-compatible-vendors.test.ts
  • src/shared/harness-injected-user-turns.test.ts
  • src/shared/harness-injected-user-turns.ts

📝 Walkthrough

Walkthrough

The change updates leading harness tag detection to allow underscores and adds hook_result to the recognized injected-harness tags. Tests cover attributed and unattributed Kimi hook_result envelopes, reject unrelated underscore-prefixed content, and verify that a later hook result does not replace the captured typed prompt.

Merge Risk: ⚪ Minimal · up to 758e8

This localized change correctly preserves the user’s prompt when processing Kimi hook envelopes without changing supported user-tag behavior. No actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the main change: treating Kimi hook_result envelopes as harness-injected content.
Description check ✅ Passed The description includes all required sections, explains the change and rationale, documents testing, and records known unrelated test failures.
Linked Issues check ✅ Passed The changes satisfy issue #14789 by ignoring Kimi hook_result envelopes while preserving legitimate underscore-wrapped user prompts.
Out of Scope Changes check ✅ Passed All changes are limited to harness-turn classification, prompt preservation, and focused regression tests for the linked issue.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

Branch auto-name ingests agent hook envelopes (<hook_result>, system wrappers) — names anchor on noise

4 participants