Skip to content

feat: cross-gateway sessions_send and sessions_spawn via gateway.peers#494

Open
BingqingLyu wants to merge 2 commits into
mainfrom
fork-pr-43656-feat-43605-cross-gateway-sessions
Open

feat: cross-gateway sessions_send and sessions_spawn via gateway.peers#494
BingqingLyu wants to merge 2 commits into
mainfrom
fork-pr-43656-feat-43605-cross-gateway-sessions

Conversation

@BingqingLyu

@BingqingLyu BingqingLyu commented Apr 27, 2026

Copy link
Copy Markdown
Owner

Problem

sessions_send and sessions_spawn only work within a single gateway. In multi-machine setups (agents split across MacBook, iMac, Mac Studio — each running their own OpenClaw gateway), there's no native way for agents to communicate cross-gateway. The current workaround is SSH scripts (msg-jarvis), which are fragile and bypass all gateway auth/routing.

Solution

Add cross-gateway routing to session tools via three complementary mechanisms:

1. gateway.peers config map (new)

Named peer gateways so agents can target remote gateways by name instead of raw URLs:

gateway:
  peers:
    imac:
      url: wss://imac.local:18789
      token: \${env:IMAC_GATEWAY_TOKEN}
    studio:
      url: wss://studio.local:18789

Peer tokens support SecretInput (env refs, secret stores, plaintext).

2. sessions_send: gateway / gatewayUrl / gatewayToken params

sessions_send(sessionKey="agent:jarvis:main", message="hello", gateway="imac")
sessions_send(sessionKey="agent:pepper:main", message="hi", gatewayUrl="wss://custom:18789", gatewayToken="tok")

When cross-gateway params are present:

  • Label resolution happens on the remote gateway
  • Message is sent via remote agent method
  • Wait + history fetch all route to remote
  • Response includes remote: true flag
  • Local session resolution, visibility checks, and A2A flow are bypassed (the remote gateway owns those)

3. sessions_spawn: same params

sessions_spawn(task="run tests", agentId="pepper", gateway="studio")

Forwards spawn as an agent message to the remote gateway.

Shared helper: gateway-peer.ts

Resolves cross-gateway targeting from tool params:

  1. gateway: "peerName" → looks up gateway.peers[peerName]
  2. gatewayUrl + optional gatewayToken → explicit URL override
  3. Neither → local gateway (returns undefined, no behavior change)

Peer names take precedence when both gateway and gatewayUrl are provided.

Design notes

  • Zero breaking changes — local path is completely unchanged
  • Pattern already existsmessage, cron, gateway, nodes, canvas tools already accept gatewayUrl/gatewayToken; this extends the pattern to session tools
  • Security — peer URLs are validated through existing resolveGatewayOptions() which enforces URL format and token resolution rules

Changes

File Change
config/zod-schema.ts gateway.peers record schema
config/types.gateway.ts GatewayPeerConfig type
config/schema.labels.ts Label for gateway.peers
config/schema.help.ts Help text for gateway.peers
agents/tools/gateway-peer.ts New — shared peer resolution helper
agents/tools/sessions-send-tool.ts Cross-gateway fast path + 3 new schema params
agents/tools/sessions-spawn-tool.ts Cross-gateway fast path + 3 new schema params
agents/tools/gateway-peer.test.ts New — 9 tests for peer resolution

Tests

  • 9 new peer resolution tests (named peer, missing peer, explicit URL, precedence, error messages)
  • 17 existing sessions tests pass
  • 32 existing config schema tests pass

Closes openclaw#43605

Chance Robinson added 2 commits March 11, 2026 23:58
Add cross-gateway messaging support so agents on different machines
(each running their own OpenClaw gateway) can communicate natively
without SSH script workarounds.

Changes:

1. gateway.peers config map (name -> {url, token})
   - Zod schema, TypeScript type, schema label + help
   - Peer tokens support SecretInput (env refs, secret stores)

2. sessions_send: gateway/gatewayUrl/gatewayToken params
   - Named peer: gateway="imac" resolves from config
   - Explicit: gatewayUrl + gatewayToken for ad-hoc targets
   - Cross-gateway path: resolve label, send message, wait for
     reply, fetch history — all via remote callGateway()
   - Local path unchanged (zero breaking changes)

3. sessions_spawn: same gateway/gatewayUrl/gatewayToken params
   - Forwards spawn as agent message to remote gateway
   - Returns {status, runId, remote: true}

4. gateway-peer.ts: shared peer resolution helper
   - Resolves named peers from config with token resolution
   - Falls back to explicit gatewayUrl
   - Clear error messages with available peer names

5. Tests: 9 new tests for peer resolution (all passing)
   - Named peer with/without token
   - Missing peer, no peers configured
   - Explicit URL override, precedence rules
   - Existing 17 sessions tests + 32 config tests still pass

Config example:
  gateway:
    peers:
      imac:
        url: wss://imac.local:18789
        token: ${env:IMAC_GATEWAY_TOKEN}
      studio:
        url: wss://studio.local:18789
        token: ${env:STUDIO_GATEWAY_TOKEN}

Usage:
  sessions_send(sessionKey="agent:jarvis:main", message="hello", gateway="imac")
  sessions_spawn(task="run tests", agentId="pepper", gateway="studio")

Closes openclaw#43605
1. Bypass resolveGatewayOptions for peer URLs — it only allows
   loopback/remote URLs, rejecting valid peer addresses like
   wss://imac.local:18789. Both sessions-send-tool and
   sessions-spawn-tool now pass peer URL/token directly to callGateway.

2. Throw on token resolution failure in gateway-peer.ts — previously
   silently swallowed, which would cause auth failures on the remote
   gateway with no indication of misconfiguration.

3. Reject unsupported spawn parameters for cross-gateway spawns —
   runtime, model, thinking, cwd, timeouts, mode, cleanup, sandbox,
   streamTo, thread, attachments, resumeSessionId are local-only and
   were silently dropped. Now returns explicit error listing which
   params are unsupported.

4. Add ws:///wss:// URL format validation on gateway.peers URL in
   zod-schema via .refine() check.

Addresses greptile review on PR openclaw#43656.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Cross-gateway messaging (sessions_send between machines)

1 participant