Repository navigation
fix(agents): resolve session-scoped tools to the live run session, not a sandbox policy key - #154
Conversation
…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 encountered an error —— View job I'll analyze this and get back to you. |
There was a problem hiding this comment.
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
…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.
|
Both findings from the automated review are valid and fixed in 77b4d03:
While tracing every other consumer of the same underlying mechanism I found two more real instances of the same bug: |
There was a problem hiding this comment.
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
…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.
There was a problem hiding this comment.
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.
What Problem This Solves
Under
session.dmScope: "main"(the default), all direct-message turns toan agent collapse into one shared transcript session (e.g.
agent:main:main).Separately,
resolveRuntimePolicySessionKey()computes a distinct syntheticper-DM-peer key used only to scope permission/visibility checks (e.g.
agent:main:msteams:default:direct:<peerId>) -- that key is never itselfpersisted 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_statusand the goal tools already receive a separaterunSessionKeyfor exactly this reason (openclaw#82669, openclaw#76708) -- everything elsethat 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"cronjob to recheck sign-in status, and sent inter-session status updates via
sessions_send. Both bound to the synthetic policy key instead of the realtranscript 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'swakeaction, thegatewaytool's post-restartcontinuationMessagebinding,
sessions_send/sessions_history'ssessionKey: "current"resolution, and -- most importantly -- the plugin SDK's own
OpenClawPluginToolContext.sessionKey, which is exactly what thebrowser-handoff plugin's own durable-resume mechanism
(
api.session.workflow.scheduleSessionTurn) used to schedule its recheckjobs. 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(addwithsessionTarget: "current", orwake),gatewayrestart with acontinuationMessage,sessions_send/sessions_historywithsessionKey: "current", or a plugin that schedules adurable resume via
scheduleSessionTurn(currently only browser-handoff) toaddress "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
runSessionKeyfield was added to the plugin SDK'stool context (purely additive, existing
sessionKeyis untouched sinceseveral other bundled plugins correctly use it for tenant/DM-scoped
isolation, e.g. per-conversation memory scoping).
Evidence
runSessionKeythreaded fromopenclaw-tools.tsintocreateCronTool,createSessionsSendTool,createSessionsHistoryTool, andcreateGatewayTool; preferred over the sandboxagentSessionKey/requesterInternalKeywherever a job/message/continuation needs toaddress the caller's own live session. Visibility/A2A permission checks,
cron's
agentIdresolution, and delivery-context/channel routing aredeliberately left on the policy key.
runSessionKeyfield added toOpenClawPluginToolContext(
src/plugins/tool-types.ts) and threaded through to browser-handoff'stwo
scheduleSessionTurn/unscheduleSessionTurnsByTagcall sites.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 contextforwarding),
extensions/browser-handoff/src/tool.test.ts(durableresume scheduling).
node scripts/run-vitest.mjsacross all touched files: 397/397 passed,no regressions.
found (the
wakeaction) was fixed and covered by an added test. Asecond, automated review pass (cubic) on the pushed PR found two more real
gaps (
sessions_send/sessions_history"current"resolution, and astray issue-ID reference in a comment) -- both addressed in the follow-up
commit.