Skip to content

fix(sessions): gate command:new hook emission on emitCommandHooks param in sessions.create (#76957)#77376

Closed
hclsys wants to merge 3 commits intoopenclaw:mainfrom
hclsys:fix/76957-sessions-create-webchat-hooks-v2
Closed

fix(sessions): gate command:new hook emission on emitCommandHooks param in sessions.create (#76957)#77376
hclsys wants to merge 3 commits intoopenclaw:mainfrom
hclsys:fix/76957-sessions-create-webchat-hooks-v2

Conversation

@hclsys
Copy link
Copy Markdown
Contributor

@hclsys hclsys commented May 4, 2026

Summary

Fixes #76957. Closes PR #77004 (previous fix was incorrect).

Root cause: commit 37aebf6 changed the Control UI /new flow from sendChatMessageNow("/new") (which dispatched through the command handler and fired command:new hooks) to host.onSlashAction("new-session") -> sessions.create, which fires no lifecycle hooks.

Previous fix (#77004) gated hook emission on parentSessionKey !== undefined, but parentSessionKey is a public SDK param used for ordinary programmatic child-session creates -- those must NOT fire command:new with commandSource: "webchat".

This fix: Add emitCommandHooks?: boolean to SessionsCreateParams. The gateway only fires the full hook chain (command:new, before_reset, session lifecycle) when emitCommandHooks === true. The Control UI /new helper passes emitCommandHooks: true; external SDK callers do not, preserving existing behavior.

Changes

  • src/gateway/protocol/schema/sessions.ts -- add emitCommandHooks?: boolean to schema
  • src/gateway/server-methods/sessions.ts -- gate hook block on p.emitCommandHooks === true; export createInternalHookEvent import
  • src/gateway/session-reset-service.ts -- export emitGatewayBeforeResetPluginHook
  • src/gateway/server-methods/sessions.runtime.ts -- re-export emitGatewayBeforeResetPluginHook + emitGatewaySessionStartPluginHook
  • ui/src/ui/controllers/sessions.ts -- add emitCommandHooks?: boolean to CreateSessionParams
  • ui/src/ui/app-render.helpers.ts -- pass emitCommandHooks: true when parentSessionKey present

Tests

  • sessions.create with emitCommandHooks=true fires command:new hook against parent
  • sessions.create without emitCommandHooks does NOT fire command:new hook

Real behavior proof

  • Behavior or issue addressed: Control UI /new can explicitly request command/session lifecycle hooks via emitCommandHooks: true, while external SDK sessions.create calls continue to avoid webchat /new hook emission by default.
  • Real environment tested: Local OpenClaw checkout on branch cycle-repair-77376-260505_2018, rebased onto origin/main 0c977cd687; repository gateway/UI/protocol test shards.
  • Exact steps or command run after this patch:
    • pnpm test src/gateway/server.sessions.reset-hooks.test.ts ui/src/ui/app-render.helpers.node.test.ts
    • pnpm protocol:check
  • Evidence after fix: Terminal output from the after-patch runs:
Test Files  1 passed (1)
Tests  7 passed (7)
Test Files  1 passed (1)
Tests  61 passed (61)
[test] passed 2 Vitest shards in 8.32s

pnpm protocol:check
wrote dist/protocol.schema.json
wrote apps/macos/Sources/OpenClawProtocol/GatewayModels.swift
wrote apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift
  • Observed result after fix: Gateway reset-hook tests covered both hook-emitting and non-hook sessions.create paths; UI helper tests passed with emitCommandHooks: true; generated protocol and Swift mirrors stayed consistent.
  • What was not tested: Did not run a live browser Control UI session against a running gateway; verification used repository gateway/UI/protocol coverage.

Generated with Claude Code

@clawsweeper
Copy link
Copy Markdown
Contributor

clawsweeper Bot commented May 4, 2026

Codex review: needs real behavior proof before merge.

Summary
Adds an explicit emitCommandHooks flag to sessions.create, wires Control UI new-session creation to send it for parented creates, emits hook events for that opt-in path, and updates protocol mirrors, tests, and changelog.

Reproducibility: yes. source-level. Current main routes typed Control UI /new through onSlashAction("new-session") and sessions.create, while sessions.create has no hook emission path despite docs defining command:new for /new.

Real behavior proof
Needs real behavior proof before merge: The PR body includes after-patch test and protocol terminal output, but it explicitly lacks a live Control UI/Gateway run showing the changed hook behavior.

Next step before merge
A contributor or maintainer needs to add or accept real after-fix behavior proof; there is no narrow code repair for ClawSweeper to make.

Security
Cleared: Cleared: the diff touches gateway/UI/protocol hook dispatch, generated Swift mirrors, tests, and changelog only, with no dependency, CI permission, secret, package-resolution, or download/execute changes.

Review details

Best possible solution:

Land the explicit hook-emission opt-in after real Control UI/Gateway behavior proof or maintainer override and exact-head CI completion.

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

Yes, source-level. Current main routes typed Control UI /new through onSlashAction("new-session") and sessions.create, while sessions.create has no hook emission path despite docs defining command:new for /new.

Is this the best way to solve the issue?

Yes, functionally. The explicit opt-in is the narrow maintainable fix because it restores Control UI hook behavior without making ordinary SDK parentSessionKey creates emit webchat command hooks.

What I checked:

Likely related people:

  • BunsDev: Commit 37aebf6 rerouted typed Control UI /new, and recent history shows repeated Control UI session/chat work in the same files. (role: introduced behavior and recent Control UI maintainer; confidence: high; commits: 37aebf612b83, 05c9492bff0f, 8469a51326d7; files: ui/src/ui/app-chat.ts, ui/src/ui/app-render.helpers.ts)
  • steipete: Recent main history shows several gateway session store, transcript, reset-service, and session-method changes adjacent to the affected server path. (role: recent gateway sessions maintainer; confidence: high; commits: 43de6ae72508, b151694e0048, 4967bcb16bf7; files: src/gateway/server-methods/sessions.ts, src/gateway/session-reset-service.ts)
  • 100yenadmin: Commit 1adaa28 added generic plugin host-hook contracts in the reset-service area that this PR now reuses for before_reset emission. (role: adjacent plugin hook contract owner; confidence: medium; commits: 1adaa28dc86b; files: src/gateway/session-reset-service.ts)

Remaining risk / open question:

  • The PR body shows repository tests and protocol generation output, but no live Control UI/Gateway run proving /new fires hooks or writes session-memory after the patch.
  • Several exact-head check-runs were still in progress at review time, so final merge should wait for them to complete green.

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

@hclsys
Copy link
Copy Markdown
Contributor Author

hclsys commented May 4, 2026

Fixed clawsweeper P2:

Problem: app-render.helpers.node.test.ts had an exact toHaveBeenCalledWith matcher expecting only {agentId, parentSessionKey}, but createChatSession now passes emitCommandHooks: true when parentSessionKey is set.

Fix: Added emitCommandHooks: true to the test expectation at line 663.

61/61 UI tests + 13/13 gateway session tests pass, commit f2e7f4ae7f.

Re-review progress:

@hclsys
Copy link
Copy Markdown
Contributor Author

hclsys commented May 4, 2026

Fixed checks-fast-protocol CI failure (commit `29dfcfd89a`):

Problem: emitCommandHooks was added to the TypeScript sessions.create params schema but the Swift GatewayModels.swift protocol mirror was not updated, causing the protocol diff check to fail.

Fix: Added emitcommandhooks: Bool? field to SessionsCreateParams in apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift, including the property declaration, init parameter, assignment, and CodingKeys entry mapping to "emitCommandHooks". All other CI checks were passing.

@hclsys
Copy link
Copy Markdown
Contributor Author

hclsys commented May 4, 2026

Rebased onto current origin/main (SHA 86e4b9b5a4) to clear the CHANGELOG.md conflict. No code changes beyond conflict resolution.

Verification:

  • pnpm test src/gateway/server.sessions.reset-hooks.test.ts ui/src/ui/app-render.helpers.node.test.ts
  • pnpm exec oxlint src/gateway/protocol/schema/sessions.ts src/gateway/server-methods/sessions.runtime.ts src/gateway/server-methods/sessions.ts src/gateway/server.sessions.reset-hooks.test.ts src/gateway/session-reset-service.ts ui/src/ui/app-render.helpers.ts ui/src/ui/controllers/sessions.ts apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift
  • pnpm exec oxfmt --check --threads=1 CHANGELOG.md src/gateway/protocol/schema/sessions.ts src/gateway/server-methods/sessions.runtime.ts src/gateway/server-methods/sessions.ts src/gateway/server.sessions.reset-hooks.test.ts src/gateway/session-reset-service.ts ui/src/ui/app-render.helpers.ts ui/src/ui/controllers/sessions.ts apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift

@hclsys
Copy link
Copy Markdown
Contributor Author

hclsys commented May 4, 2026

Follow-up: checks-fast-protocol caught the missing generated Swift protocol mirrors after the rebase. I regenerated and committed both GatewayModels.swift mirrors for SessionsCreateParams.emitCommandHooks, then reran pnpm protocol:check locally and it now passes.

@openclaw-barnacle openclaw-barnacle Bot added the app: macos App: macos label May 4, 2026
hclsys and others added 3 commits May 5, 2026 20:18
…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
…penclaw#76957)

The createChatSession helper now passes emitCommandHooks: true when a
parentSessionKey is present. Update the exact toHaveBeenCalledWith
matcher to match the new call shape.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@hclsys hclsys force-pushed the fix/76957-sessions-create-webchat-hooks-v2 branch from 3f74298 to d9d9abe Compare May 5, 2026 12:19
@hclsys
Copy link
Copy Markdown
Contributor Author

hclsys commented May 5, 2026

Closing — stale past the empirical 20h ceiling with no maintainer signal. Happy to revisit if the underlying issue stays relevant.

@hclsys hclsys closed this May 5, 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

app: macos App: macos app: web-ui App: web-ui gateway Gateway runtime size: S

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