Skip to content

feat(hooks): expose session_dir and inject SessionStart hook output into main agent context#847

Open
AzazelSensei wants to merge 1 commit into
MoonshotAI:mainfrom
AzazelSensei:feat/persistent-memory-hooks
Open

feat(hooks): expose session_dir and inject SessionStart hook output into main agent context#847
AzazelSensei wants to merge 1 commit into
MoonshotAI:mainfrom
AzazelSensei:feat/persistent-memory-hooks

Conversation

@AzazelSensei

Copy link
Copy Markdown

This PR enables external persistent-memory hooks for Kimi Code CLI by:

  • Adding session_dir to every hook payload so hooks can locate the session wire log.
  • Consuming SessionStart hook 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.

…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).
@changeset-bot

changeset-bot Bot commented Jun 17, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 74ba296

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment on lines +695 to +696
if (result.action !== 'allow') continue;
const text = (result.message ?? result.stdout ?? '').trim();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 |

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Comment on lines +66 to +71
"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`).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

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.

1 participant