Skip to content

fix(sessions): fire command:new hook against parent session on sessions.create (#76957)#77004

Closed
hclsys wants to merge 1 commit intoopenclaw:mainfrom
hclsys:fix/76957-sessions-create-command-new-hooks
Closed

fix(sessions): fire command:new hook against parent session on sessions.create (#76957)#77004
hclsys wants to merge 1 commit intoopenclaw:mainfrom
hclsys:fix/76957-sessions-create-command-new-hooks

Conversation

@hclsys
Copy link
Copy Markdown
Contributor

@hclsys hclsys commented May 4, 2026

Summary

Commit 37aebf612b rerouted the Control UI typed /new path from sendChatMessageNow(host, "/new") (which passed through Gateway command dispatch → command:new hooks) to host.onSlashAction("new-session")sessions.create, which creates a session entry but fires zero internal hooks. This broke:

  • The bundled session-memory hook (no memory file written on /new)
  • Any custom managed/workspace hooks listening on command:new
  • The before_reset plugin hook chain

Fix

In sessions.create, after a successful session creation, if canonicalParentSessionKey is set (the Control UI /new flow always passes the current session as parentSessionKey), fire createInternalHookEvent("command", "new", parentSessionKey, ...)triggerInternalHook. This restores command:new hook delivery for the transition from the parent session.

The guard hasInternalHookListeners("command", "new") ensures zero overhead when no hooks are registered.

Files changed

  • src/gateway/server-methods/sessions.ts — hook emission in sessions.create
  • CHANGELOG.md — fix entry

Test

  • 544/544 gateway method tests pass (1 pre-existing failure in unrelated web-tree-sitter import)
  • pnpm exec oxfmt --check passes on changed files

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS labels May 4, 2026
@clawsweeper
Copy link
Copy Markdown
Contributor

clawsweeper Bot commented May 4, 2026

Codex review: needs changes before merge.

Summary
The PR adds parent-session hook emission to sessions.create, exports the before-reset helper, adds gateway regression tests, and records a changelog fix for the Control UI /new hook regression.

Reproducibility: yes. source-level. Current main routes Control UI typed /new into sessions.create with a parentSessionKey, and that handler records the parent but does not emit the documented command or reset/lifecycle hooks.

Next step before merge
A narrow PR-branch repair can keep the valid Control UI hook restoration while preventing SDK/programmatic parent creates from being mislabeled as webchat /new commands.

Security
Cleared: The diff only changes gateway hook dispatch, tests, and changelog text; it does not alter dependencies, CI, package resolution, secrets, permissions, or downloaded-code paths.

Review findings

  • [P2] Gate hook emission on the actual webchat /new path — src/gateway/server-methods/sessions.ts:1011-1021
Review details

Best possible solution:

Constrain the restored hook emission to the actual Control UI new-session transition, while leaving generic SDK/programmatic parent-session creation as normal session creation unless it explicitly opts into command/reset semantics.

Do we have a high-confidence way to reproduce the issue?

Yes, source-level. Current main routes Control UI typed /new into sessions.create with a parentSessionKey, and that handler records the parent but does not emit the documented command or reset/lifecycle hooks.

Is this the best way to solve the issue?

No, not as currently patched. Emitting the reset-style hooks from the gateway is the right boundary, but parentSessionKey alone is too broad because the SDK exposes it for ordinary session creation.

Full review comments:

  • [P2] Gate hook emission on the actual webchat /new path — src/gateway/server-methods/sessions.ts:1011-1021
    This block runs for any sessions.create request that supplies parentSessionKey, but parentSessionKey is exposed on the SDK create params and is not itself proof that the caller issued a webchat /new command. As written, programmatic child-session creates will fire command:new, before_reset, and session lifecycle hooks with commandSource: "webchat", causing session-memory/custom hooks to run for non-command creates. Please gate this on the Control UI new-session source, or add an explicit create reason/emit-hooks signal, instead of using parentSessionKey alone.
    Confidence: 0.86

Overall correctness: patch is incorrect
Overall confidence: 0.86

Acceptance criteria:

  • pnpm test src/gateway/server.sessions.reset-hooks.test.ts src/gateway/server.sessions.create.test.ts
  • pnpm exec oxfmt --check --threads=1 src/gateway/server-methods/sessions.ts src/gateway/server.sessions.reset-hooks.test.ts src/gateway/server.sessions.create.test.ts
  • pnpm check:changed

What I checked:

Likely related people:

  • BunsDev: Commit 37aebf6 changed typed Control UI /new from sendChatMessageNow("/new") to the new-session action that reaches sessions.create, which is the regression trigger discussed in the PR. (role: introduced related Control UI routing; confidence: high; commits: 37aebf612b83; files: ui/src/ui/app-chat.ts, ui/src/ui/app.ts, ui/src/ui/app-render.helpers.ts)
  • VACInc: Merged PR fix(gateway): emit before_reset on session reset #53872/commit 711c9e7 added the gateway before_reset reset-hook behavior that this PR exports and reuses for parent-session creates. (role: introduced adjacent gateway hook behavior; confidence: high; commits: 711c9e724939; files: src/gateway/session-reset-service.ts, src/gateway/server.sessions.reset-hooks.test.ts)
  • jalehman: Commit 711c9e7 records jalehman as co-author/reviewer for the prior gateway reset hook work, and the same release history shows adjacent sessions.create maintenance. (role: related gateway hook reviewer and sessions maintainer; confidence: medium; commits: 711c9e724939; files: src/gateway/session-reset-service.ts, src/gateway/server.sessions.reset-hooks.test.ts, src/gateway/server-methods/sessions.ts)

Remaining risk / open question:

  • Validation is source-only in this read-only pass; the PR-reported targeted tests were not rerun here.

Codex review notes: model gpt-5.5, reasoning high; reviewed against c59c20e9fd48.

@hclsys hclsys force-pushed the fix/76957-sessions-create-command-new-hooks branch from 6fe2387 to 84ed87d Compare May 4, 2026 01:02
@hclsys
Copy link
Copy Markdown
Contributor Author

hclsys commented May 4, 2026

Fixed clawsweeper P2:

Problem: The initial patch only emitted the internal command:new hook, but the full reset hook contract also includes the plugin before_reset hook and session_end/session_start lifecycle hooks. Those remained broken for the Control UI /new path.

Fix:

  1. Exported emitGatewayBeforeResetPluginHook from session-reset-service.ts and added it to sessions.runtime.ts re-exports alongside emitGatewaySessionStartPluginHook
  2. In sessions.create, before session creation: fire command:new internal hook (existing) + before_reset plugin hook against the parent session
  3. After session creation: fire session_end (on parent, with new session as nextSessionId) + session_start (on new session, with parent as resumedFrom)

Tests added (3 new tests in server.sessions.reset-hooks.test.ts):

  • sessions.create with parentSessionKey emits command:new internal hook (#76957)
  • sessions.create with parentSessionKey emits before_reset plugin hook (#76957)
  • sessions.create with parentSessionKey emits session_end and session_start lifecycle hooks (#76957)

14/14 pass, oxfmt clean.

…ns.create

Control UI /new typed command was rerouted by 37aebf6 from
sendChatMessageNow("/new") — which dispatched through the Gateway command
handler and fired command:new internal hooks — to host.onSlashAction("new-session")
→ sessions.create, which only creates a session entry and fires no lifecycle hooks.

This broke the bundled session-memory hook (and any custom command:new hook) for
the Control UI /new path. The TUI and CLI paths were unaffected.

Fix: in sessions.create, when a parentSessionKey is provided (meaning this is a
"new session from existing" = /new flow), emit the command:new internal hook
event against the parent session before creating the new session. This mirrors the
hook emission in performGatewaySessionReset and restores hook delivery without
altering the session creation logic.

The guard on hasInternalHookListeners("command", "new") keeps this path free of
any I/O when no hooks are registered.

Fixes openclaw#76957.
@hclsys hclsys force-pushed the fix/76957-sessions-create-command-new-hooks branch from 84ed87d to a5f868d Compare May 4, 2026 12:03
@hclsys
Copy link
Copy Markdown
Contributor Author

hclsys commented May 4, 2026

Rebased onto current main — pre-existing check-test-types failure was caused by commit 24ec2aebe8 updating model auth fixture shape after I branched. Conflict in CHANGELOG.md resolved by keeping HEAD entries + appending my Gateway/sessions entry. 8/8 gateway hook tests pass locally. Head is now a5f868d577.

@hclsys
Copy link
Copy Markdown
Contributor Author

hclsys commented May 4, 2026

Superseded by #77376 which narrows hook emission to an explicit emitCommandHooks: true param (per clawsweeper P2 — parentSessionKey alone is too broad for SDK programmatic creates).

@hclsys hclsys closed this May 4, 2026
hclsys added a commit to hclsys/moltbot that referenced this pull request May 4, 2026
…am in sessions.create (openclaw#76957)

Control UI /new typed command was rerouted by 37aebf6 from
sendChatMessageNow("/new") to host.onSlashAction("new-session") →
sessions.create, which fires no lifecycle hooks. This broke session-memory
and other command:new hooks for the Control UI /new path.

Previous fix (openclaw#77004) gated on parentSessionKey alone, which is too broad
(SDK programmatic creates also supply parentSessionKey but must not fire
webchat command:new hooks per the public docs).

Fix: add emitCommandHooks: boolean to SessionsCreateParams. The Control UI
/new flow sets emitCommandHooks: true; SDK callers do not. Gateway only
emits command:new + before_reset + session lifecycle hooks when this flag
is explicitly true.

Tests:
- sessions.create with emitCommandHooks=true fires command:new against parent
- sessions.create with parentSessionKey but no emitCommandHooks fires nothing
hclsys added a commit to hclsys/moltbot that referenced this pull request May 5, 2026
…am in sessions.create (openclaw#76957)

Control UI /new typed command was rerouted by 37aebf6 from
sendChatMessageNow("/new") to host.onSlashAction("new-session") →
sessions.create, which fires no lifecycle hooks. This broke session-memory
and other command:new hooks for the Control UI /new path.

Previous fix (openclaw#77004) gated on parentSessionKey alone, which is too broad
(SDK programmatic creates also supply parentSessionKey but must not fire
webchat command:new hooks per the public docs).

Fix: add emitCommandHooks: boolean to SessionsCreateParams. The Control UI
/new flow sets emitCommandHooks: true; SDK callers do not. Gateway only
emits command:new + before_reset + session lifecycle hooks when this flag
is explicitly true.

Tests:
- sessions.create with emitCommandHooks=true fires command:new against parent
- sessions.create with parentSessionKey but no emitCommandHooks fires nothing
BunsDev added a commit that referenced this pull request May 6, 2026
Fixes #76957.

Restores the Control UI /new hook lifecycle through an explicit sessions.create emitCommandHooks opt-in, preserving hook-free defaults for programmatic parent-session creates.

Validation:
- pnpm protocol:check
- pnpm test src/gateway/server.sessions.reset-hooks.test.ts ui/src/ui/app-render.helpers.node.test.ts
- pnpm exec oxlint on touched TS files
- pnpm exec oxfmt --check --threads=1 on touched files
- git diff --check
- OPENCLAW_LOCAL_CHECK=1 OPENCLAW_LOCAL_CHECK_MODE=throttled env NODE_OPTIONS=--max-old-space-size=4096 pnpm check:changed
- GitHub PR checks green on 3a446ec
- ClawSweeper re-review completed with no blocking findings and security cleared

Duplicate triage:
- #77376, #77004, and #76967 were superseded closed attempts for #76957
- #77562 is a closed duplicate issue
- #77880 mentions #76957 but is not a duplicate of this hook fix
steipete pushed a commit that referenced this pull request May 6, 2026
Fixes #76957.

Restores the Control UI /new hook lifecycle through an explicit sessions.create emitCommandHooks opt-in, preserving hook-free defaults for programmatic parent-session creates.

Validation:
- pnpm protocol:check
- pnpm test src/gateway/server.sessions.reset-hooks.test.ts ui/src/ui/app-render.helpers.node.test.ts
- pnpm exec oxlint on touched TS files
- pnpm exec oxfmt --check --threads=1 on touched files
- git diff --check
- OPENCLAW_LOCAL_CHECK=1 OPENCLAW_LOCAL_CHECK_MODE=throttled env NODE_OPTIONS=--max-old-space-size=4096 pnpm check:changed
- GitHub PR checks green on 3a446ec
- ClawSweeper re-review completed with no blocking findings and security cleared

Duplicate triage:
- #77376, #77004, and #76967 were superseded closed attempts for #76957
- #77562 is a closed duplicate issue
- #77880 mentions #76957 but is not a duplicate of this hook fix

(cherry picked from commit 49c4a13)
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
Fixes openclaw#76957.

Restores the Control UI /new hook lifecycle through an explicit sessions.create emitCommandHooks opt-in, preserving hook-free defaults for programmatic parent-session creates.

Validation:
- pnpm protocol:check
- pnpm test src/gateway/server.sessions.reset-hooks.test.ts ui/src/ui/app-render.helpers.node.test.ts
- pnpm exec oxlint on touched TS files
- pnpm exec oxfmt --check --threads=1 on touched files
- git diff --check
- OPENCLAW_LOCAL_CHECK=1 OPENCLAW_LOCAL_CHECK_MODE=throttled env NODE_OPTIONS=--max-old-space-size=4096 pnpm check:changed
- GitHub PR checks green on 3a446ec
- ClawSweeper re-review completed with no blocking findings and security cleared

Duplicate triage:
- openclaw#77376, openclaw#77004, and openclaw#76967 were superseded closed attempts for openclaw#76957
- openclaw#77562 is a closed duplicate issue
- openclaw#77880 mentions openclaw#76957 but is not a duplicate of this hook fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant