Skip to content

fix(agents): resolve session-scoped tools to the live run session, not a sandbox policy key - #154

Merged
convyusboy merged 3 commits into
boonfrom
ridho/eng-session-key-cron-sessions-send
Aug 27, 2026
Merged

convyusboy merged 3 commits into
boonfrom
ridho/eng-session-key-cron-sessions-send

Conversation

@convyusboy

@convyusboy convyusboy commented Aug 27, 2026 •

Copy link
Copy Markdown
Collaborator

What Problem This Solves

Under session.dmScope: "main" (the default), all direct-message turns to
an agent collapse into one shared transcript session (e.g. agent:main:main).
Separately, resolveRuntimePolicySessionKey() computes a distinct synthetic
per-DM-peer key used only to scope permission/visibility checks (e.g.
agent:main:msteams:default:direct:<peerId>) -- that key is never itself
persisted as a transcript session.

Several tools and one plugin SDK context field were constructed with only
that synthetic policy key, with no way to reach the real live run session.
session_status and the goal tools already receive a separate
runSessionKey for exactly this reason (openclaw#82669, openclaw#76708) -- everything else
that needs to address "the caller's own session" did not.

Why This Change Was Made

Live-debugged during a Teams DM demo of the browser-login-handoff flow
(ENG-17466): the agent scheduled a recurring sessionTarget: "current" cron
job to recheck sign-in status, and sent inter-session status updates via
sessions_send. Both bound to the synthetic policy key instead of the real
transcript session. Every triggered cron run spun up a disposable,
context-free session instead of resuming the real conversation -- the agent
had no memory of anything from one scheduled check to the next.

An adversarial review pass before landing, plus tracing every other consumer
of the same mechanism, found the identical gap in three more places:
cron's wake action, the gateway tool's post-restart continuationMessage
binding, sessions_send/sessions_history's sessionKey: "current"
resolution, and -- most importantly -- the plugin SDK's own
OpenClawPluginToolContext.sessionKey, which is exactly what the
browser-handoff plugin's own durable-resume mechanism
(api.session.workflow.scheduleSessionTurn) used to schedule its recheck
jobs. The feature built specifically to fix this "agent forgets everything"
bug class was itself vulnerable to it.

User Impact

Any agent turn running under a DM-collapsed main session (dmScope: "main",
the default) that uses cron (add with sessionTarget: "current", or
wake), gateway restart with a continuationMessage, sessions_send/
sessions_history with sessionKey: "current", or a plugin that schedules a
durable resume via scheduleSessionTurn (currently only browser-handoff) to
address "its own session" will now correctly resume the real conversation
instead of silently starting over / losing the event. No config or API
surface changes; a new runSessionKey field was added to the plugin SDK's
tool context (purely additive, existing sessionKey is untouched since
several other bundled plugins correctly use it for tenant/DM-scoped
isolation, e.g. per-conversation memory scoping).

Evidence

  • runSessionKey threaded from openclaw-tools.ts into createCronTool,
    createSessionsSendTool, createSessionsHistoryTool, and
    createGatewayTool; preferred over the sandbox agentSessionKey/
    requesterInternalKey wherever a job/message/continuation needs to
    address the caller's own live session. Visibility/A2A permission checks,
    cron's agentId resolution, and delivery-context/channel routing are
    deliberately left on the policy key.
  • New runSessionKey field added to OpenClawPluginToolContext
    (src/plugins/tool-types.ts) and threaded through to browser-handoff's
    two scheduleSessionTurn/unscheduleSessionTurnsByTag call sites.
  • Tests reproduce each failure mode and pass only with the fix:
    src/agents/tools/cron-tool.test.ts (sessionTarget: "current"
    resolution, sessionKey auto-fill, wake routing),
    src/agents/tools/sessions.test.ts (inter-session provenance),
    src/agents/tools/sessions-resolution.test.ts (shared "current"
    resolution used by sessions_send/sessions_history),
    src/agents/openclaw-tools.plugin-context.test.ts (plugin context
    forwarding), extensions/browser-handoff/src/tool.test.ts (durable
    resume scheduling).
  • node scripts/run-vitest.mjs across all touched files: 397/397 passed,
    no regressions.
  • Independent adversarial code-review pass run before landing; every gap it
    found (the wake action) was fixed and covered by an added test. A
    second, automated review pass (cubic) on the pushed PR found two more real
    gaps (sessions_send/sessions_history "current" resolution, and a
    stray issue-ID reference in a comment) -- both addressed in the follow-up
    commit.

…not a sandbox policy key

Under session.dmScope: "main" (the default), all direct-message turns to an
agent collapse into one shared transcript session. Separately,
resolveRuntimePolicySessionKey() computes a distinct synthetic per-peer key
used only to scope permission/visibility checks -- that key is never itself
persisted as a transcript session.

The cron and sessions_send tools were constructed with only that synthetic
key as agentSessionKey, unlike session_status and the goal tools, which
already receive a separate runSessionKey for exactly this reason (openclaw#82669,
openclaw#76708). As a result, a scheduled sessionTarget: "current" cron job, a
wake, or an inter-session sessions_send bound to that key addressed a
session that was never resumable -- each triggered run spun up a
disposable, context-free session instead of continuing the real
conversation. Reproduced live in a Teams DM demo: every scheduled recheck
lost all prior context.

Threads runSessionKey through to createCronTool and createSessionsSendTool
and prefers it over agentSessionKey wherever a job/message needs to address
the caller's own live session (cron add/wake, sessions_send provenance),
while leaving policy/visibility checks and agentId resolution on the
sandbox key so DM-scoping isolation is unaffected.
@claude

claude Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

Claude encountered an error —— View job


I'll analyze this and get back to you.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 5 files

Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread src/agents/tools/sessions-send-tool.ts Resolved
Comment thread src/agents/tools/sessions-send-tool.ts Resolved
…gin scheduleSessionTurn to the live run session

Closes two more instances of the same bug fixed for cron/sessions_send in
the prior commit, found via an adversarial review pass on that PR and by
tracing every other consumer of the underlying session-key mechanism.

- resolveSessionReference() (shared by sessions_send, sessions_history, and
  session_status) resolved a literal sessionKey: "current" target using the
  sandbox/policy requesterInternalKey once gateway lookups miss, not the
  live run session -- session_status was already immune via its own
  pre-substitution, but sessions_send and sessions_history were not.
  Threads runSessionKey through and prefers it for "current" resolution
  only, leaving requesterInternalKey untouched for visibility/spawned-
  ownership checks.

- The plugin SDK's OpenClawPluginToolContext.sessionKey (exposed to every
  plugin's registerTool factory) was populated only from the sandbox/policy
  agentSessionKey, with no live-run-session alternative at all. This is
  exactly what the browser-handoff plugin's own durable-resume mechanism
  (api.session.workflow.scheduleSessionTurn) used to schedule its recheck
  and completion-drop jobs -- the same durable-resume feature built to fix
  this exact "agent forgets everything" bug class was itself vulnerable to
  it. Adds a new, purely additive runSessionKey context field (existing
  sessionKey is untouched, since several other bundled plugins correctly
  use it for tenant/DM-scoped isolation, e.g. per-conversation memory
  scoping) and wires browser-handoff's two scheduling call sites to prefer
  it.

No other bundled plugin currently calls scheduleSessionTurn or
unscheduleSessionTurnsByTag, so this closes the gap for the one plugin that
has it today while leaving the door open for future plugins to opt in the
same way.
@convyusboy convyusboy changed the title fix(agents): resolve cron and sessions_send to the live run session, not a sandbox policy key fix(agents): resolve session-scoped tools to the live run session, not a sandbox policy key Aug 27, 2026
@convyusboy

Copy link
Copy Markdown
Collaborator Author

Both findings from the automated review are valid and fixed in 77b4d03:

  1. P3 (comment issue-ID references): removed #82669/#76708 from all four new comments I'd added; kept the invariant explanation.
  2. P1 (sessions_send/sessions_history "current" resolution): confirmed — resolveSessionReference() (the shared helper both tools call) resolved a literal sessionKey: "current" target via the sandbox requesterInternalKey once gateway lookups miss, not the live run session. session_status was already immune because it pre-substitutes before ever calling into shared resolution; sessions_send/sessions_history didn't. Added runSessionKey to resolveSessionReference (preferred only for "current" resolution, requesterInternalKey untouched for visibility/spawned-ownership checks) and threaded it through both tools. New regression test in sessions-resolution.test.ts.

While tracing every other consumer of the same underlying mechanism I found two more real instances of the same bug: cron's wake action, and — most significantly — the plugin SDK's own OpenClawPluginToolContext.sessionKey, which is exactly what browser-handoff's scheduleSessionTurn-based durable resume (the feature built specifically to fix this bug class) used to schedule its recheck jobs. All fixed with tests; see the updated PR description for the full list.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 13 files (changes from recent commits).

Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread src/agents/tools/sessions-resolution.ts Resolved
Comment thread extensions/browser-handoff/src/tool.ts Resolved
…y is known; clear legacy scheduled rechecks too

Addresses two more findings from cubic's automated review on PR #154:

- resolveSessionReference() tried resolving a literal sessionKey: "current"
  as if it were a real gateway session/sessionId before ever considering
  runSessionKey. If a session or sessionId genuinely named "current"
  existed, that probe could win outright, pre-empting the live-session
  answer with no ambiguity left to resolve for. Skip the probe entirely
  once runSessionKey is known -- there's nothing left to disambiguate.

- browser-handoff's clearScheduledRecheck only cleared jobs tagged under
  the current sessionKey (now runSessionKey-preferring). A recheck job
  scheduled before this fix deployed is tagged under the old sandbox key,
  so cleanup would miss it and it could fire once more, overlapping the
  newly scheduled live-session check. Clear both keys during the
  transition when they differ.

@gandalfboon gandalfboon Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approve.

Clean extension of the existing runSessionKey pattern (already applied to session_status + goal-tools) to the remaining consumers that need a resumable transcript address: cron add/current + wake, sessions_send provenance, sessions_history + sessions-resolution current, gateway continuationMessage, and the plugin SDK's OpenClawPluginToolContext feeding browser-handoff's scheduleSessionTurn / unscheduleSessionTurnsByTag. Priority is consistently runSessionKey ?? sandbox key at the points that need a persisted transcript; the sandbox/policy key stays authoritative where it should (visibility filter, delivery-context extraction, agentId inference). Nice touch: clearScheduledRecheck double-clears legacy + new tags so a stale pre-fix job can't outlive the transition.

End-to-end wiring already existed (heartbeat-runner / run-executor → agent-tools → createOpenClawTools); this PR just plugs the leaf tools in. Change-specific tests reproduce each failure mode. Ran the five touched suites locally on the branch — 228/228 pass, no regressions. CI green on everything completed, remaining checks in progress with none failing.

No security / migration / breaking-change concerns: purely additive optional field on the plugin SDK context type; existing sessionKey semantics preserved everywhere they were correct.

@convyusboy
convyusboy merged commit a3ef09a into boon Aug 27, 2026
148 of 149 checks passed
@convyusboy
convyusboy deleted the ridho/eng-session-key-cron-sessions-send branch August 27, 2026 06:19
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.

1 participant