feat(sessions): Auto-inject From:/To: identity headers in agent-to-agent messages#7516
feat(sessions): Auto-inject From:/To: identity headers in agent-to-agent messages#7516RusDyn wants to merge 2 commits intoopenclaw:mainfrom
Conversation
…ages Agents now automatically identify themselves when using sessions_send. Headers include display name from config (e.g., "From: Adam (supervisor)"). Fixes the "anonymous message" problem where recipients couldn't tell who sent the message in multi-agent setups.
- Remove unreachable "unknown" branch from buildAgentLabel (agentId is
always resolved to a string by resolveAgentIdFromSessionKey)
- Sanitize agent names: collapse newlines to spaces, strip parentheses
to prevent header formatting issues and prompt injection
- Gate requesterLabel computation on requesterAgentId being defined
Additional Comments (1)
Scenario: (Also appears to be inconsistent with earlier code that uses Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agents/tools/sessions-send-tool.ts
Line: 227:229
Comment:
`requesterInternalKey` can be `undefined` (it’s derived from `opts?.agentSessionKey` and you guard it earlier), but `resolveAgentIdFromSessionKey(requesterInternalKey)` is called unconditionally here. If `resolveAgentIdFromSessionKey` doesn’t accept `undefined`, this will throw at runtime for non-agent callers of `sessions_send` (e.g. when invoked from main/CLI context without `opts.agentSessionKey`). Consider guarding and treating “unknown requester” as a non-cross-agent send, or defaulting explicitly.
Scenario: `sessions_send` invoked without `opts.agentSessionKey`.
(Also appears to be inconsistent with earlier code that uses `requesterInternalKey ? resolveAgentIdFromSessionKey(...) : undefined`.)
How can I resolve this? If you propose a fix, please make it concise. |
| function buildAgentLabel(agentId: string, config: OpenClawConfig | undefined): string { | ||
| if (!config) { | ||
| return agentId; | ||
| } | ||
| const agentConfig = resolveAgentConfig(config, agentId); | ||
| const rawName = agentConfig?.name; | ||
| if (rawName) { | ||
| // Sanitize: collapse whitespace/newlines to single spaces, strip parentheses | ||
| const name = rawName | ||
| .replace(/[\r\n]+/g, " ") | ||
| .replace(/[()]/g, "") | ||
| .trim(); | ||
| if (name) { | ||
| return `${name} (${agentId})`; | ||
| } | ||
| } | ||
| return agentId; | ||
| } |
There was a problem hiding this comment.
[P2] buildAgentLabel() strips all parentheses from the configured display name (.replace(/[()]/g, "")). This can unexpectedly mangle common real-world names/labels (e.g., "Alex (On-call)" -> "Alex On-call") and is broader than what’s needed to keep the From:/To: header readable. Collapsing newlines is sufficient for the stated goal; if you want to prevent delimiter confusion, consider a narrower/specific sanitization (or escaping) rather than removing all ( and ) characters.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/tools/sessions-send-helpers.ts
Line: 75:92
Comment:
[P2] `buildAgentLabel()` strips all parentheses from the configured display name (`.replace(/[()]/g, "")`). This can unexpectedly mangle common real-world names/labels (e.g., "Alex (On-call)" -> "Alex On-call") and is broader than what’s needed to keep the `From:/To:` header readable. Collapsing newlines is sufficient for the stated goal; if you want to prevent delimiter confusion, consider a narrower/specific sanitization (or escaping) rather than removing all `(` and `)` characters.
How can I resolve this? If you propose a fix, please make it concise.|
Great timing on this PR — just ran into exactly this issue tonight. Real-world reproductionI was running as the same agent (
From Session A, OpenClaw called: What happened
The security concernWithout provenance headers, the receiving agent can't distinguish between:
This creates both false positives (rejecting legitimate requests, like I did) and potential false negatives (trusting malicious messages that look like user input). Suggestion for this PRThe
|
Extends inputProvenance to support agent-to-agent tool invocations: - Add 'tool_invocation' to InputProvenanceKind enum - Add skill and mode fields to InputProvenance type - Update normalizeInputProvenance to handle new fields - Add isToolInvocationProvenance and isCrossSessionProvenance helpers - Update agent_call and debate_call tools to use tool_invocation kind This enables agents to receive structured provenance when called via agent_call/debate_call, allowing skill routing and mode tracking. Related: openclaw#15154, openclaw#10486, openclaw#7516
bfc1ccb to
f92900f
Compare
Extends inputProvenance to support agent-to-agent tool invocations: - Add 'tool_invocation' to InputProvenanceKind enum - Add skill and mode fields to InputProvenance type - Update normalizeInputProvenance to handle new fields - Add isToolInvocationProvenance and isCrossSessionProvenance helpers - Update agent_call and debate_call tools to use tool_invocation kind This enables agents to receive structured provenance when called via agent_call/debate_call, allowing skill routing and mode tracking. Related: openclaw#15154, openclaw#10486, openclaw#7516
Summary
When agents communicate via
sessions_send, recipient agents currently see anonymous messages with no indication of who sent them. This PR automatically injectsFrom:andTo:headers using agent display names from config.Before:
Agent-to-agent message context:
Agent 1 (requester) session: agent:landing.
Agent 1 (requester) channel: #landing-builder.
Agent 2 (target) session: agent:openclaw.
The Stripe webhook is deployed! Here's what you need to configure...
After:
Agent-to-agent message context:
From: Tessa (landing), session: agent:landing.
From channel: #landing-builder.
To: Leo (openclaw), session: agent:openclaw.
The Stripe webhook is deployed! Here's what you need to configure...
Motivation
In multi-agent setups, agents frequently message each other via
sessions_send. Without identity headers, recipients can't tell if a message came from the supervisor, a peer agent, or a subagent — leading to confusion about who said what.This came up in my 9-agent team where an agent (Tessa) contacted another agent (Leo), but Leo couldn't identify the sender and assumed it was the supervisor (Adam).
Related: #15778 (expose subagent ID)
Changes
buildAgentLabel()helper and updatedbuildAgentToAgentMessageContext()to include From:/To: labelsTesting
AI Disclosure 🤖
Greptile Overview
Greptile Summary
This PR enhances
sessions_sendagent-to-agent messaging by auto-injecting identity context into the recipient system prompt. It derives requester/target agent IDs from session keys, resolves optional display names from config, and formats the injected context asFrom:/To:plus channel/session information. The send tool now passes the loaded config into the helper so labels can be resolved consistently.Confidence Score: 4/5