Skip to content

fix: agent_end hook context is missing jobId for scheduled agent runs - #167124

Merged
obviyus merged 3 commits into
openclaw:mainfrom
xyrolle:fix/agent-end-cron-job-id
Oct 9, 2026
Merged

obviyus merged 3 commits into
openclaw:mainfrom
xyrolle:fix/agent-end-cron-job-id

Conversation

@xyrolle

@xyrolle xyrolle commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Closes #167123

What Problem This Solves

Fixes: a plugin's agent_end hook context has no jobId when the run was started by a scheduled (cron) job.

User Impact

Plugin authors can tell which scheduled job a finished run belongs to from the typed jobId field, without parsing the session key. Runs the cron service did not start still have no jobId. No configuration, migration or public type change.

Why This Change Was Made

The cron executor already supplies jobId and the attempt dispatcher forwards it; the shared embedded hook projection did not select it, so every hook context built from that projection lost it. The change selects and returns that one optional field. Nothing is inferred from the session key.

Evidence

Running gateway, before and after (source build, loopback gateway, isolated state, env -i, a local scripted OpenAI-compatible provider, a recorder plugin loaded through plugins.load.paths; a client subscribed with sessions.subscribe before cron.add, jobs fired on their own schedule):

Build / case cron.add id cron.runs jobId agent_end context jobId History status
before / scheduled, succeeds 99fbe932-2b06-43e2-98d9-2f86c435fe19 same absent ok
before / scheduled, fails 1654bf0c-ddff-4899-8d74-4b44e1569cfc same absent error
before / ordinary chat.send n/a n/a absent final
after / scheduled, succeeds bf322ad9-b29e-4307-870e-b01f4f1240d1 same bf322ad9-b29e-4307-870e-b01f4f1240d1 ok
after / scheduled, fails 8c87061c-da7c-4583-bb56-1595e20e4ffa same 8c87061c-da7c-4583-bb56-1595e20e4ffa error
after / ordinary chat.send n/a n/a absent final

Absence was checked with Object.hasOwn(context, "jobId") in the recorder. The scripted provider makes the runs deterministic; this is not vendor-provider proof.

Captured contexts (trace fields and the workspace path omitted)

Before, scheduled run that succeeds:

{"runId":"ea2111bf-4650-4cd9-97a3-d72999fc3042","agentId":"main","sessionKey":"agent:main:cron:99fbe932-2b06-43e2-98d9-2f86c435fe19:run:37d1f345-fe3e-4169-bedc-663d037ec328","sessionId":"37d1f345-fe3e-4169-bedc-663d037ec328","sandboxed":false,"modelProviderId":"c1-scripted","modelId":"c1-model","trigger":"cron"}

After, scheduled run that succeeds:

{"runId":"95792de2-d9cd-4969-8a11-51a372b7e4e9","jobId":"bf322ad9-b29e-4307-870e-b01f4f1240d1","agentId":"main","sessionKey":"agent:main:cron:bf322ad9-b29e-4307-870e-b01f4f1240d1:run:eddab87d-263d-4e28-98bb-878fbc5dbc87","sessionId":"eddab87d-263d-4e28-98bb-878fbc5dbc87","sandboxed":false,"modelProviderId":"c1-scripted","modelId":"c1-model","trigger":"cron"}

Tests

  • src/agents/embedded-agent-runner/run/attempt-phase-lifecycle.test.ts: the completed-attempt test now runs the real end-context builder and hook adapter (its mock of agent-end-context.js hid this defect) and covers success, failure and abort, each with and without a job id. Without the fix: 3 failures, 15 passes. With it: 18 of 18. pnpm test <file> --maxWorkers=1: about 8 s in Vitest locally.
  • src/agents/harness/hook-context.test.ts and lifecycle-hook-helpers.test.ts: 10 of 10, unchanged.
  • Removing the mock drops one row from config/test-mock-exports-baseline.txt (pruned with the repository's command).

Checks

Changed-file lint, formatting and typechecking pass. pnpm check fails locally at the core lint max-lines rule on 12 files this PR does not touch; the same 12 fail with the patch removed.

Scope and risk

The projection is shared by the other embedded hook contexts, so they receive the same optional field when the scheduler supplied it. Validated on fc3e0973; the branch is based on current main.

Maintainer verification

Preserved @xyrolle's fix and regression coverage, and merged main because the lifecycle test and mock baseline changed there. No overlap with Pash/Sarah changes.

Built Gateway verification on baseline c9a0c00b8691bda5bd7a38ee494b5edd8ecb9254 and candidate 6a6303284761a8bf4e2cf60f214141b9827a88b1, using an isolated local recorder plugin and openclaw cron run:

  • Baseline job 97262cbc-dcf2-40b9-8a10-0570caaff5a3: successful callback omitted jobId.
  • Candidate job 3239dc08-7591-47d3-80e8-6a92c40e9e5a: successful callback supplied exactly that job ID.
  • Ordinary agent runs omitted jobId on both builds.
  • One real GPT-5-mini cron run (low reasoning, 8192-token output budget) succeeded and supplied job ID 5b3e722b-285d-469d-b4db-87a132139c5f to the hook.
  • Candidate pnpm build, real pnpm tsgo, and all 28 tests across the lifecycle and two harness suites passed. The lifecycle suite took 19.67 seconds; all three routed suites took 54.47 seconds including preparation. Both builds printed an existing advisory Control UI gzip-budget violation; the build commands completed successfully.

Merge risk: two-way door; reverting restores the previous projection without changing stored state or configuration. Blast radius is plugin consumers of shared embedded-agent hook contexts on scheduler-supplied runs.

The cron executor supplies jobId and the attempt dispatcher forwards it, but
the shared embedded hook projection did not select it, so plugin hook contexts
for cron-started runs had no jobId. Select and return the optional field.

The completed-attempt lifecycle test now exercises the real end-context
builder and hook adapter instead of mocking the builder, and covers success,
failure and abort with and without a job id.
@clawsweeper

clawsweeper Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Oct 8, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 8, 2026
@clawsweeper

clawsweeper Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

The branch forwards the scheduler-supplied job ID into shared agent hook contexts and extends lifecycle regression coverage.

Example: A plugin observes a completed scheduled agent run

  • Before: The agent_end context omits jobId for job bf322ad9-b29e-4307-870e-b01f4f1240d1.
  • After: The agent_end context contains jobId equal to bf322ad9-b29e-4307-870e-b01f4f1240d1.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A focused contract repair with relevant before/after Gateway proof, meaningful regression coverage, and no actionable findings.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): Recorded callbacks from a running isolated Gateway and loaded plugin show matching jobId after successful and failed scheduled runs, with no jobId for ordinary chat. Additional baseline-versus-pinned-head CLI verification confirms the repaired scheduler-to-hook path. The fix changes no stored-data contract.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Plugins cannot identify which scheduled job finished through the documented hook field.
Reason: The scheduler already supplies the identity and the public context already supports it. The small shared-owner repair restores the existing contract.

Merge readiness

✅ Ready for maintainer review

This PR addresses a documented defect still present on current main. No actionable patch findings remain; the large reported surface comes from merged main changes.

Priority: P2
Reviewed head: 6a6303284761a8bf4e2cf60f214141b9827a88b1

Before merge

None.

Findings

None.

Agent review details

How this fits together

The scheduler supplies job identity when starting an agent run. Shared context builders carry execution facts into the lifecycle callbacks that plugins receive.

flowchart TD
  A[Scheduled job] --> B[Agent execution parameters]
  C[Ordinary chat] --> B
  B --> D[Shared agent hook context]
  D --> E[Sparse plugin context]
  E --> F[Plugin lifecycle callback]
  A --> G[Scheduled run history]
Loading

Technical review

Best possible solution:

Keep scheduler-supplied identity in the existing shared projection and preserve sparse ordinary-run contexts at the plugin boundary.

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

Yes: current-main source proves that the scheduler supplies jobId and the shared projection drops it, corroborated by recorded Gateway callbacks; this review did not execute the reproduction.

Is this the best way to solve the issue?

Yes: forwarding the existing optional field at its shared owner repairs the cause without session-key parsing, another API, or configuration changes.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 3f76fe021990.

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Current main still omits job identity: The current-main input selection and returned hook context omit jobId, although scheduled execution supplies it. (src/agents/embedded-agent-runner/run/agent-hook-context.ts:14, 3f76fe021990)
  • Existing documented plugin contract: PluginHookAgentContext already declares jobId?: string; https://docs.openclaw.ai/plugins/hooks/prompt-and-session describes it as the originating cron job ID when supplied by the emitter. (src/plugins/hook-agent-context.types.ts:28, 6a6303284761)
  • Producer and consumer agree on the existing field: The cron executor passes params.job.id, the attempt dispatcher forwards params.jobId, and the harness projector already includes supplied IDs while omitting undefined IDs. (src/cron/isolated-agent/run-executor.ts:417, 6a6303284761)
  • Main-merge surface verified: The raw head records 811d442 as its second parent, and that commit is also the merge base with fetched main. Comparing that upstream revision with the head leaves only the hook projection, lifecycle test, and obsolete mock-baseline entry; the other reported paths are identical to that upstream revision. (6a6303284761)
  • Relevant files unchanged by later main commits: No later fetched-main commit changes the three fix-specific paths. The flagged credential and sandbox files are also identical between the merged upstream revision and fetched main. (3f76fe021990)
  • Recorded Gateway before/after behavior: The complete supplied body, source identity 8bbdaa90503982b6e10134cc21bd30fed454ff86d07dfd0193b1b79441d4aab5, records naturally scheduled successful and failed jobs through an isolated running Gateway and loaded recorder plugin. Both receive matching jobId after the fix; ordinary chat omits it. Additional verification pins baseline c9a0c00 and candidate 6a63032 and records the same improvement through openclaw cron run. (6a6303284761)

PR surface

PR surface statistics unavailable: the file list is incomplete.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #167123
Summary: This PR is the focused candidate fix for the canonical report's missing scheduled-job identity.

Members:

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

Labels

Label changes:

  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster. Replaced prior rating: 🐚 platinum hermit.
  • remove rating: 🐚 platinum hermit: Current PR rating is rating: 🦞 diamond lobster, so this older rating label is no longer current.

Label justifications:

  • P2: This bounded omission prevents plugins from correlating scheduled completions through the documented field without demonstrating a core workflow outage.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster. Replaced prior rating: 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only; repair and merge need explicit maintainer commands such as @clawsweeper autofix or @clawsweeper automerge.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-08T10:14:47.687Z sha dc74873 :: needs maintainer review before merge. :: none

Reviewed October 8, 2026, 9:13 PM ET / October 9, 2026, 01:13 UTC (Revision 2).

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Security-sensitive guard cleared

Current revision: 6a6303284761a8bf4e2cf60f214141b9827a88b1

This PR no longer changes files in the maintainer security-review tier.

Separate CODEOWNERS requirements still apply to security policy and enforcement files.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation channel: tlon Channel integration: tlon gateway Gateway runtime scripts Repository scripts size: XL and removed size: S labels Oct 9, 2026
@obviyus

obviyus commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

Maintainer override: the only red job is inherited and outside this diff.

ClawSweeper is ready on this exact head.

@obviyus
obviyus merged commit 78a91db into openclaw:main Oct 9, 2026
121 of 129 checks passed
@obviyus

obviyus commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

Landed in 78a91db. Thanks @xyrolle! Verified with a built Gateway and openclaw cron run: agent_end now receives the exact job id for cron runs (it was missing on main), and ordinary runs are unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling channel: tlon Channel integration: tlon docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts size: XL status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: agent_end hook context omits jobId for scheduled (cron) agent runs

2 participants