Skip to content

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

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 16, 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 16, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. labels Jul 16, 2026
@clawsweeper

clawsweeper Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 27, 2026, 4:50 AM ET / 08:50 UTC.

ClawSweeper review

What this changes

Forward the embedded OpenClaw run’s agent, session, session-key, and run identifiers to existing tool-result middleware callbacks.

Merge readiness

⛔ Blocked until real behavior proof from a real setup is added - 4 items remain

The earlier missing-session finding is resolved: this PR now carries all four available embedded-run identity values into the existing middleware runner. The patch appears technically sound, but the external-PR gate still needs redacted after-fix proof from a real installed middleware invocation rather than focused test output alone.

Priority: P2
Reviewed head: f6511f7d4f52158cb49377586c09a948d70f89e8

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The implementation is focused and resolves the prior sessionId gap, but its merge-readiness is capped by missing real behavior proof.
Proof confidence 🦪 silver shellfish (2/6) Needs real behavior proof before merge: Focused tests are useful supplemental validation, but this external PR still lacks redacted after-fix evidence from a real installed middleware receiving the four callback fields. 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.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Needs proof Needs real behavior proof before merge: Focused tests are useful supplemental validation, but this external PR still lacks redacted after-fix evidence from a real installed middleware receiving the four callback fields. 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 4 items Patch forwards the complete available identity set: The proposed extension factory accepts agentId, sessionId, sessionKey, and runId, and the embedded attempt session supplies all four from the active attempt before creating the tool-result middleware runner.
Session context follow-up is addressed: The current head passes attempt.sessionId through prepareEmbeddedAttemptAgentSession, resolving the prior review cycles that identified sessionId as dropped.
The linked bug remains the canonical unresolved report: The linked issue documents a concrete OpenClaw-versus-Codex middleware-context mismatch and remains open pending a mergeable, proven fix.
Findings None None.
Security None None.

How this fits together

Embedded agent attempts assemble runtime extensions before executing tools. Tool-result middleware then delivers post-tool-call events to installed plugins for auditing, telemetry, or policy handling, so the callback context determines whether those plugins can associate an event with its active agent session and run.

flowchart LR
  A[Embedded agent attempt] --> B[Run identity context]
  B --> C[Extension factory setup]
  C --> D[Tool-result middleware]
  D --> E[Installed plugin handler]
  E --> F[Audit telemetry or policy action]
Loading

Before merge

  • Add real behavior proof - Needs real behavior proof before merge: Focused tests are useful supplemental validation, but this external PR still lacks redacted after-fix evidence from a real installed middleware receiving the four callback fields. 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.
  • Resolve merge risk (P1) - Before merge, the four forwarded identifiers need redacted real-runtime evidence from an installed middleware; focused tests do not establish that the OpenClaw callback receives the values in an actual run.
  • Resolve merge risk (P1) - The branch is behind current main, so final merge review should refresh the exact head against the current agent-runtime surface after proof is supplied.
  • Complete next step (P2) - The remaining action is contributor-supplied real behavior proof, not a concrete code repair suitable for an automated fix lane.
Agent review details

Security

None.

PR surface

Source +23, Tests +4. Total +27 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 25 2 +23
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 29 2 +27

Review metrics

Metric Value Why it matters
Middleware identity propagation 4 identifiers forwarded The patch aligns the OpenClaw callback with the existing context shape for agent, session, session-key, and run attribution.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #106910
Summary: This PR is the candidate implementation for the linked middleware-context bug; the issue remains canonical until the fix is merged and proven.

Members:

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

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep the existing middleware API and land this narrow parity fix after a redacted real OpenClaw run shows an installed middleware receiving agentId, sessionId, sessionKey, and runId.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep the existing middleware API and land this narrow parity fix after a redacted real OpenClaw run shows an installed middleware receiving agentId, sessionId, sessionKey, and runId.

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

No high-confidence live reproduction has been observed in this review. The linked report provides a clear source-level and installed-middleware path, but the supplied evidence does not show that path running after the fix.

Is this the best way to solve the issue?

Yes. Passing the already-available attempt context through the existing middleware factory is the narrowest parity repair and avoids a new middleware API; it still needs runtime proof before merge.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • remove rating: 🧂 unranked krab: Current PR rating is rating: 🦪 silver shellfish, so this older rating label is no longer current.

Label justifications:

  • P2: This is a bounded agent-runtime correctness fix affecting installed middleware attribution and policy behavior without evidence of core-runtime outage or data loss.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: Focused tests are useful supplemental validation, but this external PR still lacks redacted after-fix evidence from a real installed middleware receiving the four callback fields. 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

What I checked:

Likely related people:

  • ruel225: Authored the current head and the follow-up that corrected the previously missing sessionId propagation; broader current-main ownership could not be established from the supplied history evidence. (role: current patch author and follow-up contributor; confidence: low; commits: f6511f7d4f52; files: src/agents/embedded-agent-runner/extensions.ts, src/agents/embedded-agent-runner/run/attempt-session.ts)
  • EricCheungAO: Provided the linked report’s specific OpenClaw/Codex parity reproduction and identified the public middleware context contract affected by this change. (role: canonical bug reporter and reproduction author; confidence: medium; files: src/plugins/agent-tool-result-middleware-types.ts, extensions/codex/src/app-server/dynamic-tools.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Add a redacted terminal capture, runtime log, or live output from an installed middleware showing agentId, sessionId, sessionKey, and runId on an OpenClaw tool result.
  • Refresh the branch against current main after adding proof, then request the normal final merge review.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
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.

Workflow

  • 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.

History

Review history (3 earlier review cycles)
  • reviewed 2026-07-16T11:41:12.918Z sha 5570652a29aac8ada7d1935e7721c13ffe364d43 :: needs real behavior proof before merge. :: [P2] Pass the available session ID into the middleware context
  • reviewed 2026-07-17T06:51:47.057Z sha ef770e1863a6faeefbcc9091b8a8b82a30b4bddf :: needs real behavior proof before merge. :: [P2] Forward the available session ID to middleware
  • reviewed 2026-07-27T03:13:28.086Z sha 0eedff71bcc332438a36a94ff7740f02bdd15591 :: needs real behavior proof before merge. :: none

@ruel225
ruel225 force-pushed the fix/agent-tool-result-middleware-context branch from 5570652 to ef770e1 Compare July 17, 2026 06:33
@ruel225
ruel225 force-pushed the fix/agent-tool-result-middleware-context branch from ef770e1 to 0eedff7 Compare July 27, 2026 03:08
@ruel225

ruel225 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Addressed ClawSweeper P1: sessionId is now forwarded to the middleware.

Before: sessionId was dropped (passed as undefined) because SessionManager.sessionId is private.

After: The attempt-session.ts caller has attempt.sessionId available, so it's now passed through buildEmbeddedExtensionFactories → buildAgentToolResultMiddlewareFactory → createAgentToolResultMiddlewareRunner.

All four identity fields are now propagated: agentId, sessionId, sessionKey, runId.

{
  "proof": "middleware-identity-propagation",
  "fields": ["agentId", "sessionId", "sessionKey", "runId"],
  "source": "attempt-session.ts → extensions.ts → createAgentToolResultMiddlewareRunner",
  "tests": "7/7 passed"
}

@ruel225

ruel225 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 27, 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: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

…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 0eedff7 to f6511f7 Compare July 27, 2026 08:47
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jul 27, 2026
@ruel225

ruel225 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Closing — #107000 provides a more complete fix with SessionManager.getSessionId() fallback, compact.ts coverage, and runtime proof. Learning from their approach for future contributions.

@ruel225 ruel225 closed this Jul 28, 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 P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. 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