Skip to content

fix(agents): pass agent/session context to OpenClaw tool result middleware (#106910) - #107023

Closed
ruel225 wants to merge 1 commit into
openclaw:mainfrom
ruel225:fix/agent-tool-result-middleware-context
Closed

ruel225 wants to merge 1 commit into
openclaw:mainfrom
ruel225:fix/agent-tool-result-middleware-context

Conversation

@ruel225

@ruel225 ruel225 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Pass agentId, sessionId, sessionKey, and runId to createAgentToolResultMiddlewareRunner on the OpenClaw runtime path.
  • Previously only { runtime: "openclaw" } was passed, while the Codex runtime passes the full toolResultHookContext.

What Problem This Solves

The OpenClaw runtime invoked AgentToolResultMiddleware handlers without agent, session, or run identity, even though those fields are part of the public middleware context (AgentToolResultMiddlewareContext). Middleware could not attribute or scope OpenClaw events.

The Codex runtime (dynamic-tools.ts:460-462) already passes the full context via toolResultHookContext.

User Impact

  • Why it matters: Installed middleware that performs per-agent/per-session audit, telemetry, or policy decisions can now attribute OpenClaw events at parity with Codex.
  • What did NOT change: The middleware API is unchanged. Only the context values passed to existing handlers are added.

Origin / follow-up

Fixes #106910.

Evidence

node scripts/run-vitest.mjs src/agents/embedded-agent-runner/extensions.test.ts src/agents/embedded-agent-runner/run/attempt-session.test.ts
# 7/7 tests passed

Fixes #106910

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Jul 14, 2026
@ruel225
ruel225 force-pushed the fix/agent-tool-result-middleware-context branch from be6e974 to 12b9749 Compare July 14, 2026 02:43
@ruel225

ruel225 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jul 14, 2026
@clawsweeper

clawsweeper Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 14, 2026, 6:11 AM ET / 10:11 UTC.

Summary
Adds optional middleware identity parameters and forwards agentId, sessionKey, and runId through the OpenClaw embedded-agent path, with fixture updates.

PR surface: Source +21, Tests +4. Total +25 across 3 files.

Reproducibility: yes. —the current-main runner construction and the issue's presence-boolean middleware path provide a high-confidence source reproduction, although this review did not execute a live installed-plugin run.

Review metrics: 1 noteworthy metric.

  • Middleware identity context: 3 forwarded, 1 omitted. The PR promises four identity fields, but sessionId remains explicitly absent at the runner call.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #106910
Summary: The linked issue is the canonical bug report, and this PR is its candidate implementation but does not yet fully resolve the session identity gap.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🦪 silver shellfish
Result: blocked until real behavior proof from a real setup is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Forward the authoritative active sessionId and add a regression assertion covering all four context fields.
  • Provide redacted installed-runtime terminal output or logs showing the corrected middleware context.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR supplies focused Vitest output only; add redacted terminal output or logs from an after-fix installed middleware invocation showing agentId, sessionId, sessionKey, and runId. Updating the PR body should trigger review; otherwise ask a maintainer to comment @clawsweeper re-review, and redact keys, phone numbers, endpoints, and other private data. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] Merging as written would let middleware distinguish an agent and session key while still receiving no active sessionId, allowing per-session audit or policy state to remain unknown or mis-associated.
  • [P1] The only submitted evidence is focused Vitest output; it does not show an installed middleware handler receiving the corrected context in a real OpenClaw run.

Maintainer options:

  1. Forward the authoritative session ID (recommended)
    Thread the active session ID from the session manager or attempt context, add a focused four-field regression assertion, and provide installed-runtime proof before merge.
  2. Pause the partial fix
    If the authoritative session ID cannot be established safely, keep the linked issue open and rework this branch rather than shipping incomplete identity parity.

Next step before merge

  • [P1] The contributor must restore the active session ID and provide after-fix installed-runtime proof; an automated repair worker cannot supply proof from the contributor's environment.

Security
Cleared: The focused TypeScript and fixture changes introduce no concrete security or supply-chain concern beyond the functional session-association defect.

Review findings

  • [P1] Forward the active session ID to the runner — src/agents/embedded-agent-runner/extensions.ts:227
Review details

Best possible solution:

Forward the authoritative active sessionId, assert all four optional identity fields at the middleware boundary, and provide redacted installed-plugin runtime output showing the after-fix context.

Do we have a high-confidence way to reproduce the issue?

Yes—the current-main runner construction and the issue's presence-boolean middleware path provide a high-confidence source reproduction, although this review did not execute a live installed-plugin run.

Is this the best way to solve the issue?

No—the general context-threading approach is appropriate, but explicitly dropping the active sessionId leaves the documented bug partially unresolved.

Full review comments:

  • [P1] Forward the active session ID to the runner — src/agents/embedded-agent-runner/extensions.ts:227
    The runner call still passes a literal undefined for sessionId, so installed middleware never receives the active session identity promised by this fix. Thread the authoritative session ID from the session manager or attempt context and assert it in the regression test; this is the same unresolved blocker from the previous review of this head.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.99

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against e052d122d6ac.

Label changes

Label justifications:

  • P2: This is a bounded plugin middleware attribution bug with limited blast radius, but the proposed fix remains incomplete.
  • merge-risk: 🚨 session-state: The patch can expose partial identity that still cannot reliably associate middleware policy or telemetry with the active session.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦪 silver shellfish.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR supplies focused Vitest output only; add redacted terminal output or logs from an after-fix installed middleware invocation showing agentId, sessionId, sessionKey, and runId. Updating the PR body should trigger review; otherwise ask a maintainer to comment @clawsweeper re-review, and redact keys, phone numbers, endpoints, and other private data. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +21, Tests +4. Total +25 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 23 2 +21
Tests 1 4 0 +4
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 27 2 +25

What I checked:

Likely related people:

  • joshavant: The behavior is likely related to the context-engine integration work that touched the embedded extension and attempt boundaries used by this middleware path. (github.com) (role: adjacent context-management owner; confidence: medium; commits: fee91fefceb4; files: src/agents/embedded-agent-runner/extensions.ts, src/agents/embedded-agent-runner/run/attempt-session.ts)
  • vincentkoc: Recent agent-scope and embedded-runtime refactors make this contributor a useful routing candidate for verifying which session identity is authoritative at the factory boundary. (role: recent agent-runtime contributor; confidence: low; commits: 74e7b8d47b18, a372e4a15240, 91ac48524610; files: src/agents/agent-scope-config.ts, src/agents/embedded-agent-runner/extensions.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
Review history (9 earlier review cycles; latest 8 shown)
  • reviewed 2026-07-14T05:16:54.409Z sha 5c0f293c2f8feba6fdaecdbf0d54a9bfda459e24 :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-14T05:54:15.066Z sha 5c0f293c2f8feba6fdaecdbf0d54a9bfda459e24 :: needs real behavior proof before merge. :: [P1] Update the active current-main attempt caller
  • reviewed 2026-07-14T06:24:17.650Z sha 122fc6f7a2943774eeccae3f2632d3786db69990 :: needs real behavior proof before merge. :: [P1] Pass the actual session ID to the middleware runner
  • reviewed 2026-07-14T07:12:29.679Z sha 122fc6f7a2943774eeccae3f2632d3786db69990 :: needs real behavior proof before merge. :: [P1] Pass the actual session ID to the middleware runner
  • reviewed 2026-07-14T08:04:22.995Z sha 122fc6f7a2943774eeccae3f2632d3786db69990 :: needs real behavior proof before merge. :: [P1] Pass the actual session ID to the middleware runner
  • reviewed 2026-07-14T08:47:31.553Z sha df1773ebec26d2c45954b6eb881c73957236a932 :: needs real behavior proof before merge. :: [P1] Forward the active session ID instead of dropping it
  • reviewed 2026-07-14T09:07:32.161Z sha 707c244f27daa18c5d8f8b74ab59f75d0cb474b8 :: needs real behavior proof before merge. :: [P1] Forward the active session ID instead of dropping it
  • reviewed 2026-07-14T09:50:24.960Z sha d30e4e2 :: needs real behavior proof before merge. :: [P1] Pass the active session ID to the runner

@ruel225
ruel225 force-pushed the fix/agent-tool-result-middleware-context branch from 12b9749 to 80f85b4 Compare July 14, 2026 03:17
@ruel225

ruel225 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Addressed ClawSweeper P1 finding: removed empty-string sentinels from identity fallbacks.

Before: sessionManager.sessionId ?? and attempt.sessionKey ?? created empty strings that middleware could misinterpret as valid scope keys.
After: Optional values are passed as-is (no ?? sentinel), matching the existing AgentToolResultMiddlewareContext optional semantics.

@ruel225

ruel225 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

@ruel225
ruel225 force-pushed the fix/agent-tool-result-middleware-context branch 2 times, most recently from 5c0f293 to 122fc6f Compare July 14, 2026 05:58
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jul 14, 2026
@ruel225
ruel225 force-pushed the fix/agent-tool-result-middleware-context branch from 122fc6f to df1773e Compare July 14, 2026 08:39
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jul 14, 2026
@ruel225
ruel225 force-pushed the fix/agent-tool-result-middleware-context branch 2 times, most recently from 707c244 to 90e71f1 Compare July 14, 2026 09:13
…eware

The OpenClaw runtime constructed the AgentToolResultMiddlewareRunner with
only { runtime: 'openclaw' }, omitting agentId, sessionId, sessionKey,
and runId. This differed from the Codex runtime which passes the full
toolResultHookContext.

Add the missing fields so middleware can attribute and scope OpenClaw
events at parity with Codex.

Fixes openclaw#106910
@ruel225
ruel225 force-pushed the fix/agent-tool-result-middleware-context branch from 90e71f1 to d30e4e2 Compare July 14, 2026 09:26
@ruel225

ruel225 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

@ruel225

ruel225 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Closing to re-trigger ClawSweeper review — the review appeared stuck after 'picked up' for 2+ days. Will reopen with the same code.

@ruel225 ruel225 closed this Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: XS status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: OpenClaw AgentToolResultMiddleware omits agent/session/run context

1 participant