Skip to content

fix(agent-core): append PreToolUse output to context - #2502

Draft
xushun007 wants to merge 1 commit into
MoonshotAI:mainfrom
xushun007:agent/fix-pretool-hook-context
Draft

fix(agent-core): append PreToolUse output to context#2502
xushun007 wants to merge 1 commit into
MoonshotAI:mainfrom
xushun007:agent/fix-pretool-hook-context

Conversation

@xushun007

Copy link
Copy Markdown

Related Issue

Resolve #2107

Problem

Successful PreToolUse hooks can print additional context to stdout, but that output is currently discarded after the hook allows the tool call. As a result, the model never sees context provided by the hook.

What changed

  • Preserve successful PreToolUse hook results in the permission flow.
  • Append non-empty hook stdout to the model context as a <hook_result hook_event="PreToolUse"> user message after the corresponding tool result.
  • Emit the existing hook.result event for the preserved result.
  • Reuse the hook-output renderer shared with UserPromptSubmit.
  • Add regression coverage for the context message and its ordering, and update permission-policy tests for the full hook result path.

The hook message is deferred until turn finalization so the assistant tool call remains immediately followed by its tool result, preserving valid tool-call history ordering.

Verification

  • pnpm --filter @moonshot-ai/agent-core test -- test/agent/tool.test.ts test/agent/permission.test.ts test/session/hooks/engine.test.ts (221 passed)
  • Turn/hooks integration suite (97 passed)
  • pnpm --filter @moonshot-ai/agent-core typecheck
  • Targeted lint (0 errors; 3 existing warnings)
  • git diff --check

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

The existing English and Chinese hook documentation already describes successful stdout being appended to context, so no documentation update is needed.

@changeset-bot

changeset-bot Bot commented Aug 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 089d59a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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.

PreToolUse hook stdout is not appended to context (0.29.0)

1 participant