Skip to content

fix: honor --to when agent is specified#42009

Closed
TomDjerry wants to merge 3 commits into
openclaw:mainfrom
TomDjerry:fix-agent-to-session-routing
Closed

fix: honor --to when agent is specified#42009
TomDjerry wants to merge 3 commits into
openclaw:mainfrom
TomDjerry:fix-agent-to-session-routing

Conversation

@TomDjerry

Copy link
Copy Markdown

Summary

  • fix openclaw agent --agent --to so --to derives an agent-scoped session key instead of collapsing to the agent main session
  • keep explicit --session-key precedence unchanged
  • add regression coverage for --agent + --to and the explicit session-key guardrail

Root Cause

esolveSessionKeyForRequest() eagerly fell back to the agent main-session alias when --agent was present, so custom --to values like cw_111 were ignored and all runs reused �gent::main.

Testing

  • fully tested
  • corepack pnpm exec vitest run src/commands/agent/session.test.ts src/commands/agent/session-store.test.ts
  • temporarily reverted the fix and reran src/commands/agent/session.test.ts to confirm the new regression test fails with �gent:mybot:main before restoring the fix

Transparency

  • AI-assisted: implemented with Codex

Copilot AI review requested due to automatic review settings March 10, 2026 09:46
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation commands Command implementations size: S labels Mar 10, 2026
@greptile-apps

greptile-apps Bot commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes resolveSessionKeyForRequest to correctly honor --to when --agent is also specified, instead of always collapsing to the agent's main-session key. The change refactors the session-key resolution into three clearly distinct paths: explicit --session-key (highest precedence), a new derivedToSessionKey for the --agent + --to case, and the existing agent-main-session fallback — and adds targeted regression tests for both new paths.

  • Fix is logically correct: All four input combinations (explicit key, --to-only, --agent-only, --agent + --to) now route to the expected session key, verified by tracing through toAgentStoreSessionKey.
  • storeAgentId derivation improved: Using normalizedAgentId directly as the primary source (instead of reversing it from an agent-main alias) is cleaner and avoids an unnecessary indirection.
  • Minor readability concern (lines 55–61 in session.ts): The ternary explicitSessionKey || opts.to?.trim() ? undefined : resolveExplicitAgentSessionKey(...) relies on implicit || / ?: operator precedence. The logic is correct, but wrapping the OR sub-expression in parentheses would make the intent immediately obvious to future readers.
  • Plan document committed: docs/plans/2026-03-10-agent-to-session-routing.md is an AI-assisted implementation plan including a "For Claude" directive. Depending on team conventions, this type of ephemeral planning artifact might be better kept out of the repository's permanent history.

Confidence Score: 4/5

  • Safe to merge — the bug fix is correct and well-tested with no regressions in existing behavior.
  • The core logic change is sound: all four input combinations route to the correct session key, the new derivedToSessionKey path properly delegates to toAgentStoreSessionKey for agent-scoped keys, and the original --to-only and --agent-only flows are preserved. The only deduction is for a minor readability risk from implicit operator precedence on lines 55–61, which is a style concern rather than a functional defect.
  • Pay light attention to src/commands/agent/session.ts lines 55–61 for the ternary precedence style note; no files have critical issues.

Last reviewed commit: 48d19c2

Comment thread src/commands/agent/session.ts Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where openclaw agent --agent <id> --to <target> would ignore the --to flag and always collapse to the agent's main session key (agent:<id>:main). The root cause was that resolveSessionKeyForRequest() eagerly fell back to the agent main-session alias when --agent was present, before considering --to.

Changes:

  • Refactored session key resolution in resolveSessionKeyForRequest() to derive an agent-scoped session key from --to when both --agent and --to are provided, while preserving explicit --session-key precedence
  • Added two regression tests covering the --agent + --to derivation and the explicit --session-key guardrail
  • Added an implementation plan document

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/commands/agent/session.ts Refactored session key resolution to properly derive agent-scoped keys from --to instead of always collapsing to the main session
src/commands/agent/session.test.ts Added two regression tests for --agent + --to and explicit --session-key precedence
docs/plans/2026-03-10-agent-to-session-routing.md New implementation plan document describing the fix approach

You can also share your feedback on Copilot code review. Take the survey.

Comment thread docs/plans/2026-03-10-agent-to-session-routing.md Outdated
Comment thread docs/plans/2026-03-10-agent-to-session-routing.md Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 48d19c2907

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/commands/agent/session.ts Outdated
Comment thread src/commands/agent/session.ts Outdated
@openclaw-barnacle openclaw-barnacle Bot removed the docs Improvements or additions to documentation label Mar 10, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 388eb52283

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/commands/agent/session.ts Outdated
@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 Apr 27, 2026
@clawsweeper

clawsweeper Bot commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close as superseded: this PR targets a real session-routing bug, but the remaining work is now tracked by the open canonical issue and maintainer-owned implementation path, while this branch is conflicting and would regress current session-key contracts if merged as-is.

Canonical path: Keep #73403 as the canonical implementation path and close this conflicting branch while preserving its discussion as context.

So I’m closing this here and keeping the remaining discussion on #73403 and #41483.

Review details

Best possible solution:

Keep #73403 as the canonical implementation path and close this conflicting branch while preserving its discussion as context.

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

Yes. Source inspection on current main shows --agent can select the agent main session key before --to participates, so the reported collapse to agent:<id>:main is reproducible from the resolver path without running the CLI.

Is this the best way to solve the issue?

No. The branch points at a valid bug, but the better solution is the canonical channel-aware resolver fix in #73403 because it covers gateway and embedded paths while preserving current default-agent and session-id contracts.

Security review:

Security review needs attention: The diff has no dependency or workflow changes, but its direct-target session-key path could persist raw recipient identifiers and should not be merged in this superseded form.

  • [medium] Avoid raw direct-target session keys — src/agents/command/session.ts:176
    The PR would turn arbitrary direct --to values into persisted agent session-key suffixes via toAgentStoreSessionKey, which can expose customer or phone identifiers and conflicts with the current direct-chat main-session contract.
    Confidence: 0.74

What I checked:

Likely related people:

  • vincentkoc: Authored the merged default-agent session-routing compatibility work and the open canonical PR that fixes the same remaining channel-target routing problem. (role: recent resolver contributor and canonical follow-up owner; confidence: high; commits: 61a18e5596c8, 765765a972cc; files: src/agents/command/session.ts, src/commands/agent-via-gateway.ts, src/commands/agent/session.test.ts)
  • steipete: Recent path history shows repeated central work on session-key normalization and explicit session-id/session routing behavior adjacent to the direct/group key contract. (role: session-key normalization contributor; confidence: medium; commits: ad7399b6e605, cf1c2cc208ff, cd36ff748369; files: src/config/sessions/session-key.ts, src/config/sessions/session-key.test.ts, src/agents/command/session.ts)
  • frankekn: Authored a recent merged fix in the same resolver family covering agent-scoped session-id routing and gateway validation behavior. (role: adjacent session-id resolver contributor; confidence: medium; commits: 934dd5b3a738; files: src/agents/command/session.ts, src/commands/agent/session.test.ts, src/gateway/server-methods/agent.ts)

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

@TomDjerry
TomDjerry force-pushed the fix-agent-to-session-routing branch from 8ed941b to 09f09a2 Compare April 27, 2026 06:27
@openclaw-barnacle openclaw-barnacle Bot added the agents Agent runtime and tooling label Apr 27, 2026
@TomDjerry

Copy link
Copy Markdown
Author

Rebased and updated this PR against current main.

Ported the routing fix to src/agents/command/session.ts and kept src/commands/agent/session.ts as the re-export. The regression coverage in src/commands/agent/session.test.ts now covers --agent + --to, explicit --session-key precedence, blank --session-key, and qualified --to rewriting.

Verification run:

vitest run --config test/vitest/vitest.commands.config.ts src/commands/agent/session.test.ts --reporter=dot
1 passed, 15 tests passed

@TomDjerry
TomDjerry force-pushed the fix-agent-to-session-routing branch from 09f09a2 to 99e5422 Compare April 27, 2026 06:35
@openclaw-barnacle openclaw-barnacle Bot removed the stale Marked as stale due to inactivity label Apr 28, 2026
@clawsweeper clawsweeper Bot added the P2 Normal backlog priority with limited blast radius. label May 16, 2026
@openclaw-barnacle openclaw-barnacle Bot added the triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. label May 16, 2026
@clawsweeper clawsweeper Bot added impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. impact:security Security boundary, credential, authz, sandbox, or sensitive-data risk. labels May 17, 2026
@clawsweeper clawsweeper Bot closed this May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling commands Command implementations impact:security Security boundary, credential, authz, sandbox, or sensitive-data risk. impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. P2 Normal backlog priority with limited blast radius. size: S triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants