openclaw/docs/plugins
pandadev66 dfa1a11676 fix(gateway): fire typed session_end on shutdown/restart for active sessions (#57790)
`session_end` was only fired when a session was replaced, reset, deleted, or
compacted -- the gateway shutdown/restart paths closed the process without
enumerating active sessions, so downstream `session_end` plugins
(e.g. claude-mem) accumulated ghost rows in `active` state across restarts.
Issue reporter saw 11 orphaned sessions cause 63 timeouts/day from agent
pool exhaustion.

Add an in-memory active-session tracker
(`src/gateway/active-sessions-shutdown-tracker.ts`) populated by
`emitGatewaySessionStartPluginHook` and forgotten unconditionally by
`emitGatewaySessionEndPluginHook` (even when no plugin listens), so any
session that has already been finalized through the normal lifecycle is
never re-fired by the shutdown drain. The close handler then calls a new
`drainActiveSessionsForShutdown({ reason })` in `session-reset-service.ts`
between the `gateway:shutdown`/`gateway:pre-restart` lifecycle hooks and
the subsystem teardown steps; the drain races a bounded 2 s total timeout
so a slow plugin cannot block SIGTERM/SIGINT, surfacing the timeout as a
`session-end-drain` warning on the shutdown result.

Extend `PluginHookSessionEndReason` with `"shutdown"` and `"restart"` so
plugins can distinguish a graceful close from a planned restart; the close
handler picks `restart` when `restartExpectedMs` is set and `shutdown`
otherwise. Update `emitGatewaySessionStartPluginHook` to also accept
`storePath`, `sessionFile`, and `agentId` so the shutdown drain can build
the same `session_end` payload shape the normal lifecycle path emits, and
update the existing call sites in `session-reset-service.ts` and
`server-methods/sessions.ts` to pass those fields through.

Tests:

- `src/gateway/active-sessions-shutdown-tracker.test.ts` (new) -- tracker
  insert/forget/clear semantics, idempotent re-noting, empty-id guard,
  snapshot isolation.
- `src/gateway/drain-active-sessions-for-shutdown.test.ts` (new) -- drain
  fires `session_end` with the right reason for every tracked session,
  skips sessions already finalized via reset/delete/compaction, and still
  forgets sessions even when no `session_end` plugin is registered.
- `src/gateway/server-close.test.ts` -- four new cases covering the
  shutdown/restart drain wiring, the bounded timeout warning, and the
  drain-skipped-when-no-helper case.

Docs:

- `docs/plugins/hooks.md` documents the new `shutdown`/`restart` values
  on `PluginHookSessionEndReason`.
- `docs/automation/hooks.md` documents the post-`gateway:shutdown`
  `session_end` drain step and its bounded execution guarantee.

Fixes #57790.
2026-05-11 17:28:23 +01:00
..
reference docs(plugins): add clickclack reference 2026-05-10 06:13:38 +01:00
adding-capabilities.md docs: consolidate moved docs pages 2026-05-05 17:38:21 +01:00
agent-tools.md docs(plugins/agent-tools): remove duplicate H1 2026-05-06 09:00:20 -07:00
architecture-internals.md docs(plugin-sdk): document consolidated workflow seams 2026-05-11 03:24:08 +01:00
architecture.md
building-extensions.md
building-plugins.md feat: expose active model plugin context 2026-05-10 15:21:43 +01:00
bundles.md fix(mcp): keep numeric bundle tool names provider-safe 2026-05-10 06:29:08 +01:00
cli-backend-plugins.md fix(acpx): harden session lifecycle cleanup 2026-05-07 07:30:37 +01:00
codex-computer-use.md Keep OpenAI Codex migrations on automatic runtime routing (#79238) 2026-05-08 16:05:35 +09:00
codex-harness-reference.md fix(codex): remove dynamic tools profile option 2026-05-09 21:34:33 -07:00
codex-harness-runtime.md fix(codex): observe native tool completions 2026-05-11 14:23:46 +01:00
codex-harness.md fix(codex): rotate auth profiles inside harness 2026-05-11 12:41:21 +01:00
codex-native-plugins.md fix(codex): auto-approve plugin read tools (#80513) 2026-05-10 21:13:40 -07:00
community.md docs: add dedicated ClawHub docs tab (#79159) 2026-05-07 18:55:08 -07:00
compatibility.md build: refresh deps and route testbox through crabbox 2026-05-11 03:41:00 +01:00
dependency-resolution.md fix(plugins): avoid managed npm prefix on Windows 2026-05-09 07:51:49 +08:00
google-meet.md docs: sentence-case 4 stray Title Case headings 2026-05-06 09:22:35 -07:00
hooks.md fix(gateway): fire typed session_end on shutdown/restart for active sessions (#57790) 2026-05-11 17:28:23 +01:00
install-overrides.md feat: add plugin install overrides 2026-05-09 06:06:08 -04:00
manage-plugins.md docs: add dedicated ClawHub docs tab (#79159) 2026-05-07 18:55:08 -07:00
manifest.md feat: unify model catalog registration 2026-05-09 02:34:56 -04:00
memory-lancedb.md
memory-wiki.md
message-presentation.md feat(plugin-sdk): share presentation control helpers 2026-05-09 10:11:56 +01:00
oc-path.md docs(oc-path): expand usage examples 2026-05-09 01:48:35 -04:00
plugin-inventory.md chore(release): prepare 2026.5.10-beta.1 2026-05-10 06:13:38 +01:00
reference.md chore(release): prepare 2026.5.10-beta.1 2026-05-10 06:13:38 +01:00
sdk-agent-harness.md Keep OpenAI Codex migrations on automatic runtime routing (#79238) 2026-05-08 16:05:35 +09:00
sdk-channel-ingress.md refactor: centralize channel ingress access 2026-05-10 00:18:36 -04:00
sdk-channel-message.md refactor: consolidate message delivery API 2026-05-09 07:04:04 +01:00
sdk-channel-plugins.md refactor: move rare sdk seams into owners 2026-05-10 12:37:10 +01:00
sdk-channel-turn.md refactor: centralize channel ingress access 2026-05-10 00:18:36 -04:00
sdk-entrypoints.md refactor: move canvas to plugin surfaces 2026-05-07 09:07:18 +01:00
sdk-migration.md refactor: move rare sdk seams into owners 2026-05-10 12:37:10 +01:00
sdk-overview.md docs(plugin-sdk): document consolidated workflow seams 2026-05-11 03:24:08 +01:00
sdk-provider-plugins.md refactor: reduce plugin sdk surface 2026-05-10 12:37:10 +01:00
sdk-runtime.md docs(plugin-sdk): document consolidated workflow seams 2026-05-11 03:24:08 +01:00
sdk-setup.md refactor: move rare sdk seams into owners 2026-05-10 12:37:10 +01:00
sdk-subpaths.md docs(plugin-sdk): document consolidated workflow seams 2026-05-11 03:24:08 +01:00
sdk-testing.md refactor: reduce plugin sdk surface 2026-05-10 12:37:10 +01:00
skill-workshop.md fix(skill-workshop): honor pending approval for tool suggestions [AI] (#78516) 2026-05-07 13:27:32 +05:30
voice-call.md feat: add realtime consult overrides 2026-05-09 06:51:20 -04:00
webhooks.md docs: remove duplicate body H1s + sentence-case headings across 10 pages 2026-05-06 09:06:11 -07:00
zalouser.md docs: add dedicated ClawHub docs tab (#79159) 2026-05-07 18:55:08 -07:00