Skip to content

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

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#76967
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 3, 2026

Problem

Typing /new in the Control UI no longer fires command:new internal hooks, breaking the bundled session-memory hook and any custom hooks listening on command:new.

Root cause: Commit 37aebf612b (fix(control-ui): create sessions for typed /new, fixing #69599) rerouted the /new slash-command dispatch from:

// Before — dispatched through Gateway command handler → fires command:new hooks
await sendChatMessageNow(host, "/new", { refreshSessions: true, ... });

to:

// After — calls sessions.create directly → fires no lifecycle hooks
await host.onSlashAction("new-session");
// → createChatSessionInternal() → createSessionAndRefresh() → sessions.create RPC

sessions.create creates the session entry but never emits command:new (or before_reset) hooks against the parent session. The gateway logs confirm: [ws] ⇄ res ✓ sessions.create 93ms with zero hook activity — compare with /reset which produces [hooks/session-memory] Hook triggered for reset/new command.

TUI and CLI /new paths go through performGatewaySessionReset and are unaffected.

Fix

In sessions.create, when a parentSessionKey is provided (this is the "new session from existing" = /new flow), emit the command:new internal hook against the parent session before creating the new session:

if (canonicalParentSessionKey && hasInternalHookListeners("command", "new")) {
  const { entry: parentEntry } = loadSessionEntry(canonicalParentSessionKey);
  const parentAgentId = normalizeAgentId(
    resolveAgentIdFromSessionKey(canonicalParentSessionKey) ?? resolveDefaultAgentId(cfg),
  );
  const hookEvent = createInternalHookEvent("command", "new", canonicalParentSessionKey, {
    sessionEntry: parentEntry,
    previousSessionEntry: parentEntry,
    commandSource: "webchat",
    cfg,
    workspaceDir: resolveAgentWorkspaceDir(cfg, parentAgentId),
  });
  await triggerInternalHook(hookEvent);
}

This mirrors the hook emission in performGatewaySessionReset (which handles TUI/CLI /new). The hasInternalHookListeners guard keeps this path zero-cost when no hooks are registered.

Files changed: src/gateway/server-methods/sessions.ts (1 import added, 17 lines added in sessions.create)

Fixes #76957.

…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.
@openclaw-barnacle
Copy link
Copy Markdown

Closing this PR because the author has more than 20 active PRs in this repo. Please reduce the active PR queue and reopen or resubmit once it is back under the limit. You can close your own PRs to get back under the limit.

@openclaw-barnacle openclaw-barnacle Bot closed this May 3, 2026
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 r: too-many-prs Auto-close: author has more than twenty active PRs. size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Control UI typed /new no longer fires command:new hooks after 37aebf612b — session-memory and custom hooks broken

1 participant