openclaw icon indicating copy to clipboard operation
openclaw copied to clipboard

Cron isolated sessions use wrong Matrix accountId (inherits from session history instead of agent bindings)

Open saxyguy81 opened this issue 10 months ago • 1 comments

Description

When a cron job runs with agentId: "max" in a multi-agent/multi-account Matrix setup, the isolated cron session sends messages through the wrong Matrix account. Instead of using the account bound to the agent (e.g., "max"), it uses whatever account was last active in the main session (e.g., "yessica").

Root Cause

The issue is in the outbound account resolution chain for cron isolated sessions:

1. delivery-target.js resolves accountId from session history, not agent bindings

In dist/cron/isolated-agent/delivery-target.js, resolveDeliveryTarget() calls resolveSessionDeliveryTarget() which uses deliveryContextFromSession() to extract lastAccountId from the session store entry.

2. session.js copies lastAccountId from main session

In dist/cron/isolated-agent/session.js, resolveCronSession() copies lastAccountId from the main session's store entry:

const sessionEntry = {
    // ...
    lastAccountId: entry?.lastAccountId,  // ← Inherited from main session
    // ...
};

3. targets.js uses lastAccountId without considering agent bindings

In dist/infra/outbound/targets.js line 38:

const accountId = channel && channel === lastChannel ? lastAccountId : undefined;

This means: if a different agent (e.g., "yessica") was the last to interact on the Matrix channel, the cron session for "max" will inherit yessica's lastAccountId and send messages through yessica's Matrix account.

4. The message tool falls back to this wrong accountId

In dist/agents/tools/message-tool.js, line 275:

const accountId = readStringParam(params, "accountId") ?? agentAccountId;

Where agentAccountId comes from resolvedDelivery.accountId (the wrong one from step 3), and the LLM typically doesn't explicitly specify accountId in tool calls.

Expected Behavior

A cron job with agentId: "max" should resolve the correct Matrix account from agent bindings:

{
  "bindings": [
    { "agentId": "max", "match": { "channel": "matrix", "accountId": "max" } }
  ]
}

The message tool should use accountId: "max" regardless of which agent was last active on the channel.

Actual Behavior

The cron session inherits lastAccountId from the main session store, which may belong to a completely different agent/account. Messages are sent through the wrong Matrix account.

Steps to Reproduce

  1. Configure 4 Matrix accounts (max, sam, yessica, dave) with corresponding agents
  2. Configure bindings mapping each agentId to its accountId
  3. Create a cron job with agentId: "max", sessionTarget: "isolated", kind: "agentTurn"
  4. Have agent "yessica" send a message on Matrix (making yessica the lastAccountId)
  5. Wait for the cron job to fire

Expected: Cron sends message via max's Matrix account Actual: Cron sends message via yessica's Matrix account

Config Snippet

{
  "channels": {
    "matrix": {
      "accounts": {
        "max": { "enabled": true, "userId": "@max:server", "accessToken": "..." },
        "sam": { "enabled": true },
        "yessica": { "enabled": true },
        "dave": { "enabled": true }
      }
    }
  },
  "bindings": [
    { "agentId": "max", "match": { "channel": "matrix", "accountId": "max" } },
    { "agentId": "sam", "match": { "channel": "matrix", "accountId": "sam" } },
    { "agentId": "yessica", "match": { "channel": "matrix", "accountId": "yessica" } },
    { "agentId": "dave", "match": { "channel": "matrix", "accountId": "dave" } }
  ]
}

Cron job (ba940a90-324c-4b2d-bfd9-41228ba6a5d0):

{
  "agentId": "max",
  "payload": {
    "kind": "agentTurn",
    "deliver": true,
    "channel": "matrix"
  }
}

Proposed Fix

In delivery-target.js, after resolving the delivery target from session history, check the agent's bindings to determine the correct accountId:

// After resolving from session context, override accountId from agent bindings
const agentBinding = cfg.bindings?.find(b => 
    b.agentId === agentId && b.match?.channel === channel
);
if (agentBinding?.match?.accountId) {
    resolved.accountId = agentBinding.match.accountId;
}

Alternatively, the runCronIsolatedAgentTurn function in run.js could resolve the correct accountId from bindings before passing it to runEmbeddedPiAgent.

Workaround

Disabled the cron job to prevent messages going through the wrong account.

Related Issues

  • #3165 — Matrix plugin: Multiple accounts don't work (shared client singleton + missing accountId in routing) — covers inbound routing; this issue covers outbound account resolution in cron sessions
  • #3520 — Cron delivery routing picks up wrong target from isolated session context — similar session-context-inheritance problem but for delivery target, not accountId
  • #6602 — Multi-agent routing bindings are ignored — related binding resolution issue

Environment

  • OpenClaw version: 2026.1.29
  • Node.js: v25.3.0
  • macOS (arm64)
  • 4 Matrix accounts, 4 agents with bindings

saxyguy81 avatar Feb 02 '26 19:02 saxyguy81

Duplicate of #3520

tyler6204 avatar Feb 04 '26 22:02 tyler6204