Skip to content

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

Closed

Description

@EricCheungAO

Bug type

Behavior bug (incorrect output/state without crash)

Beta release blocker

No

Summary

The OpenClaw runtime invokes installed AgentToolResultMiddleware handlers without available agent, session, or run identity, even though those fields are part of the public middleware context and the Codex runtime forwards them.

Steps to reproduce

  1. On OpenClaw 2026.7.1, enable an installed plugin whose manifest declares agentToolResultMiddleware: ["openclaw", "codex"].
  2. Register middleware that records only whether ctx.agentId, ctx.sessionId, ctx.sessionKey, and ctx.runId are present.
  3. Execute a harmless tool call through the OpenClaw runtime.
  4. Execute an equivalent harmless tool call through the Codex runtime with known agent/session/run context.
  5. Compare only the presence booleans.

Expected behavior

When the OpenClaw tool-result bridge has agent/session/run context, it should pass that context to createAgentToolResultMiddlewareRunner, matching the public AgentToolResultMiddlewareContext shape and the Codex integration.

Public references:

  • The context type declares optional agentId, sessionId, sessionKey, and runId:
    export type AgentToolResultMiddlewareContext = {
    runtime: AgentToolResultMiddlewareRuntime;
    /** @deprecated Use runtime. */
    harness?: AgentToolResultMiddlewareRuntime;
    agentId?: string;
    sessionId?: string;
    sessionKey?: string;
    runId?: string;
  • The Codex integration constructs its runner with runtime: "codex" plus its tool-result hook context:
    function readRouteToken(record: Record<string, unknown>, key: string): string | number | undefined {
    const value = record[key];
    return typeof value === "string" || typeof value === "number" ? value : undefined;
    }

Actual behavior

The OpenClaw integration constructs the runner with only { runtime: "openclaw" }, despite receiving runId in the surrounding factory and retaining a session manager:

function buildAgentToolResultMiddlewareFactory(
sessionManager: SessionManager,
runId?: string,
): ExtensionFactory {

As a result, middleware sees ctx.agentId, ctx.sessionId, ctx.sessionKey, and ctx.runId as absent on the OpenClaw path. The same middleware therefore cannot attribute or scope OpenClaw events at parity with Codex.

OpenClaw version

2026.7.1

Operating system

macOS

Install method

NOT_ENOUGH_INFO

Model

NOT_ENOUGH_INFO

Provider / routing chain

NOT_ENOUGH_INFO

Additional provider/model setup details

NOT_ENOUGH_INFO

Logs, screenshots, and evidence

No raw logs are included. The reproduction records presence booleans only. The public source links above show the context-construction difference directly.

Impact and severity

  • Affected: installed middleware that performs per-agent/per-session audit, telemetry, or policy decisions on the OpenClaw runtime.
  • Severity: medium; tool calls still complete, but middleware attribution and session scoping are unavailable.
  • Frequency: every observed OpenClaw middleware invocation on 2026.7.1.
  • Consequence: middleware must label OpenClaw events as unknown or fail closed; it cannot prove the expected agent/session identity for a denial canary.

Additional information

A focused regression test could construct the OpenClaw middleware runner from a known embedded run context and assert parity with the Codex path for agentId, sessionId, sessionKey, and runId.

Activity

  1. added
    P2
    impact:session-state
    no-stale
    issue-rating: 🦞 diamond lobster
    clawsweeper:source-repro
    clawsweeper:queueable-fix
    clawsweeper:fix-shape-clear
    on Jul 14, 2026
  2. clawsweeper commented on Jul 14, 2026

    @clawsweeper
    Contributor

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed July 13, 2026, 8:26 PM ET / July 14, 2026, 00:26 UTC.

    Summary
    Keep open: current main still constructs OpenClaw tool-result middleware without execution identity, while the public middleware contract and Codex sibling path support carrying that context. No merged fix or canonical related item was found.

    Likely related people

    • steipete — medium-confidence recent contributor to the embedded agent runtime.
    • shakkernerd — medium-confidence contributor to recent run/session-context refactors.

    Reproducibility: yes. at source level: current main visibly discards the available runId, and the public context plus sibling Codex implementation establish the expected path. A live plugin invocation was not executed during this read-only review.

    Next step
    The defect has a narrow owner boundary, an existing context contract, a clear sibling implementation, and a focused regression-test path with no product-direction decision.

    Review details

    Best possible solution:

    Forward canonical prepared agent, session, and run identifiers from the embedded-run attempt into the middleware runner, without request-time rediscovery, and add focused parity coverage for both OpenClaw and Codex contexts.

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

    Yes at source level: current main visibly discards the available runId, and the public context plus sibling Codex implementation establish the expected path. A live plugin invocation was not executed during this read-only review.

    Is this the best way to solve the issue?

    Yes, with one boundary constraint: pass the canonical prepared identifiers through the existing factory rather than querying the session manager during every tool result.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    • A repair must confirm the canonical meaning and availability of sessionId versus sessionKey; deriving either from mutable session-manager state inside the tool-result hot path could associate middleware events with the wrong session.

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

    Label changes

    Label justifications:

    • P2: This is a bounded public-middleware correctness bug affecting attribution and policy decisions without crashing the core runtime.
    • impact:session-state: Tool-result middleware cannot reliably associate OpenClaw events with their agent, session, or run context.
    Evidence reviewed

    Acceptance criteria:

    • pnpm test src/agents/embedded-agent-runner/extensions.test.ts
    • pnpm test extensions/codex/src/app-server/dynamic-tools.test.ts
    • pnpm check:changed -- src/agents/embedded-agent-runner/extensions.ts src/agents/embedded-agent-runner/extensions.test.ts

    What I checked:

    Likely related people:

    • steipete: Recent merged history shows substantial work internalizing and maintaining the embedded Pi agent runtime that owns the extension-factory call path. (role: recent area contributor; confidence: medium; commits: bb46b79; files: src/agents/pi-embedded-runner/run/attempt.ts, src/agents/embedded-agent-runner/extensions.ts)
    • shakkernerd: Recent merged work refactored prepared user turns and related run/session context flow in the embedded agent path, making this person relevant to safely forwarding canonical identity. (role: recent context-path contributor; confidence: medium; commits: 56e461b, 04f85b7; files: src/agents/pi-embedded-runner/run/attempt.ts)
    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.
  3. ruel225 commented on Jul 14, 2026

    @ruel225
    Contributor

    Looking into this — PR #107023 adds agentId, sessionId, sessionKey, and runId to the OpenClaw tool result middleware context.

  4. 3 remaining items

  5. ruel225 commented on Jul 16, 2026

    @ruel225
    Contributor

    Reopened as #108952 (previous PR #107023 was closed due to stuck ClawSweeper review).

  6. self-assigned this
    on Jul 29, 2026
  7. added a commit that references this issue on Jul 29, 2026
    bed7014
  8. added a commit that references this issue on Jul 30, 2026
    5111d4c
  9. locked as resolved and limited conversation to collaborators on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2clawsweeper:fix-shape-clearclawsweeper:queueable-fixclawsweeper:source-reproimpact:session-stateissue-rating: 🦞 diamond lobsterno-stale

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions