feat(hooks): expose session_dir and inject SessionStart hook output into main agent context#847
Conversation
…n payload - Append non-empty SessionStart hook stdout/message to the main agent context as a hook_result message, mirroring UserPromptSubmit behavior. - Add session_dir to the base hook payload so external integrations can locate session artifacts (e.g. wire.jsonl).
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 74ba296e87
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (result.action !== 'allow') continue; | ||
| const text = (result.message ?? result.stdout ?? '').trim(); |
There was a problem hiding this comment.
Ignore failed SessionStart hook output
For a SessionStart hook that writes stdout and then exits 1 (or times out after partial output), runHook still returns action: 'allow' with that stdout, and this code appends it because it only filters block actions. That differs from the existing UserPromptSubmit path, which excludes timed-out/non-zero results before adding hook text, so startup/resume hook diagnostics or partial output can be injected into the model context as recall data.
Useful? React with 👍 / 👎.
| | `PermissionRequest` | Tool name | — | Triggered just before waiting for user approval (observation only) | | ||
| | `PermissionResult` | Tool name | — | Triggered after approval completes (observation only) | | ||
| | `SessionStart` | `startup` or `resume` | — | Triggered after a new session starts or a previous session resumes | | ||
| | `SessionStart` | `startup` or `resume` | ✓ (since v0.14.0) | Triggered after a new session starts or a previous session resumes; non-empty stdout or `message` is appended to the main agent context | |
There was a problem hiding this comment.
Do not mark SessionStart as blockable
SessionStart is now listed as blockable, but triggerSessionStart calls hookEngine.trigger(...) and only skips block results instead of using triggerBlock(...); an exit-code-2 or structured deny hook will not stop startup/resume. Users following this table would rely on a safety check that is silently ignored, so either keep this event observation-only or implement the blocking behavior.
Useful? React with 👍 / 👎.
| "session_dir": "/path/to/kimi-code/home/sessions/..." | ||
| } | ||
| ``` | ||
|
|
||
| - `hook_event_name`, `session_id`, and `cwd` are always present. | ||
| - `session_dir` is the session's persistent directory under `KIMI_CODE_HOME`; it is present for all events and can be used by integrations that need to read session artifacts (for example, the active agent's wire log at `<session_dir>/agents/main/wire.jsonl`). |
There was a problem hiding this comment.
Keep Chinese hook docs in sync
This English-only addition is not mirrored in docs/zh/customization/hooks.md. docs/AGENTS.md says "docs/en/ and docs/zh/ are mirrored pairs", but the Chinese hook page still lacks the new session_dir payload field and SessionStart-output behavior, leaving one locale with stale instructions for this feature.
Useful? React with 👍 / 👎.
This PR enables external persistent-memory hooks for Kimi Code CLI by:
session_dirto every hook payload so hooks can locate the session wire log.SessionStarthook results and appending non-empty stdout/message text to the main agent context, matching the hook contract used by Claude/Codex.Motivation: third-party memory layers need to inject a recall block at session start and know where the transcript lives.
Tests:
npm_config_engine_strict=false pnpm --filter @moonshot-ai/agent-core test→ 2605 passed.