Skip to content

feat(hooks): add sessionMode option for persistent hook sessions - #27193

Closed
ryanrhughes wants to merge 1 commit into
openclaw:mainfrom
ryanrhughes:fix/hook-session-reuse-v2
Closed

ryanrhughes wants to merge 1 commit into
openclaw:mainfrom
ryanrhughes:fix/hook-session-reuse-v2

Conversation

@ryanrhughes

@ryanrhughes ryanrhughes commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds sessionMode: "isolated" | "persistent" to hook mapping config, enabling multi-turn conversation history for webhook-dispatched sessions.

Problem

Every hook dispatch creates a fresh session transcript, losing all conversation history. The sessionKey preserves metadata but not the conversation itself. This causes orphaned .jsonl files and breaks use cases like project management integrations where multiple webhooks (card created, assigned, commented) should share context.

Solution

New optional sessionMode field on hook mappings:

hooks:
  mappings:
    - match: { path: "/fizzy" }
      sessionKey: "hook:fizzy:card-{{number}}"
      sessionMode: persistent  # reuse session across webhooks
  • isolated (default): fresh session per dispatch — current behavior, zero change for existing users
  • persistent: reuse existing sessionId when sessionKey matches

Transforms can also override sessionMode per-request.

Changes

  • src/config/types.hooks.ts — type definition
  • src/config/zod-schema.hooks.ts — schema validation
  • src/gateway/hooks-mapping.ts — threaded through resolve/build/merge
  • src/gateway/hooks.ts — payload normalization
  • src/gateway/server-http.ts — mapping dispatch path
  • src/gateway/server/hooks.ts — passed to isolated agent runner
  • src/cron/isolated-agent/run.ts — forceNew respects sessionMode

Testing

  • 6 new tests (schema validation + mapping threading + transform override)
  • All 262 cron tests pass, all gateway tests pass (1 pre-existing canvas-auth failure on main)
  • Live tested both modes on a real Fizzy webhook integration:
    • persistent: 2 webhooks → same sessionId, 2 turns in 1 file ✅
    • isolated: subsequent webhook → new sessionId, 1 turn ✅
  • Has been running in a production environment since 2026.02.12 and processed thousands of interactions

AI-Assisted

Built with Claude & ChatGPT (OpenClaw agents of course). Fully tested locally and live. We understand the full change.

Fixes #11665
Fixes #42332

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S labels Feb 26, 2026
@greptile-apps

greptile-apps Bot commented Feb 26, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds optional sessionMode field to hook mappings, enabling persistent session reuse across webhook invocations when set to "persistent". The implementation correctly threads the new field through all layers of the hooks system:

  • Type-safe throughout: Proper TypeScript types and Zod validation ensure only "isolated" or "persistent" values are accepted
  • Backward compatible: Defaults to undefined, preserving existing "isolated" behavior for all current hooks
  • Clean implementation: The field flows naturally through mapping resolution → action building → payload normalization → job execution
  • Correct session logic: In run.ts:229, forceNew is set based on both sessionTarget and sessionMode, ensuring persistent sessions reuse existing sessionIds when the sessionKey matches
  • Transform overrides supported: Hook transforms can dynamically override sessionMode per request
  • Well-tested: 6 new tests cover schema validation, mapping threading, default behavior, and transform overrides

Confidence Score: 5/5

  • This PR is safe to merge with no identified issues
  • The implementation is exceptionally clean with proper type safety throughout, comprehensive test coverage (including edge cases like transform overrides), correct logic that maintains backward compatibility, and clear documentation via JSDoc. The changes are purely additive and don't alter any existing behavior when the field is not set.
  • No files require special attention

Last reviewed commit: 7788c2d

@ryanrhughes
ryanrhughes force-pushed the fix/hook-session-reuse-v2 branch from 88fa45f to efd6af2 Compare March 1, 2026 05:17
@ryanrhughes
ryanrhughes force-pushed the fix/hook-session-reuse-v2 branch from efd6af2 to c6db22c Compare March 9, 2026 22:17
@ryanrhughes
ryanrhughes force-pushed the fix/hook-session-reuse-v2 branch from 98dc373 to a76dac5 Compare March 14, 2026 04:01
@ryanrhughes
ryanrhughes force-pushed the fix/hook-session-reuse-v2 branch from a76dac5 to 434e34a Compare March 14, 2026 04:01
…nclaw#11665)

Add sessionMode: 'isolated' | 'persistent' to hook mapping config.
When set to 'persistent', hook sessions reuse existing sessionId for
multi-turn conversation history. Default 'isolated' preserves current
behavior. Transforms can also override sessionMode per-request.

Fixes openclaw#11665
@ryanrhughes
ryanrhughes force-pushed the fix/hook-session-reuse-v2 branch from 434e34a to 632de9a Compare March 14, 2026 04:18
@luogao2333

Copy link
Copy Markdown

Hi OpenClaw team — just wanted to bump this PR for review.

We're hitting the exact same issue in production: routing per-group chat messages via /hooks/agent with consistent sessionKey values, but every dispatch creates a fresh session and all conversation context is lost. The session.reset.idleMinutes config has no effect on hook sessions.

The current docs say "Use a consistent key for multi-turn conversations in hook context", but the actual behavior doesn't match — each hook call starts a completely new session regardless of the key.

This PR's sessionMode: "persistent" solution is clean and minimal. The author has been running it in production since Feb 12 with thousands of interactions, and the Greptile review bot gave it a 5/5 confidence score. All tests pass.

Could this get a review and merge? Persistent hook sessions are a real need for any webhook integration that requires multi-turn context. Thanks!

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Mar 30, 2026
@openclaw-barnacle

Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #pr-thunderdome-dangerzone on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime size: S stale Marked as stale due to inactivity

Projects

None yet

2 participants