Skip to content

fix(cron): emit message.queued/processed for isolated-agent turns - #79214

Merged
steipete merged 4 commits into
openclaw:mainfrom
arniesaha:pr/cron-isolated-message-queued
May 25, 2026
Merged

steipete merged 4 commits into
openclaw:mainfrom
arniesaha:pr/cron-isolated-message-queued

Conversation

@arniesaha

@arniesaha arniesaha commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The diagnostic-event seam for message.queued / session.state / message.processed is currently emitted only by channel-driven dispatch (src/auto-reply/reply/dispatch-from-config.ts:417) and the embedded runner (src/agents/pi-embedded-runner/runs.ts:75,89). The isolated-cron agent path (src/cron/isolated-agent/run.ts) executes full agent turns including LLM calls, but never emits these events. Anything subscribed to the diagnostic-events bus for per-session attribution silently drops 100% of cron-isolated traffic.

This PR adds a paired logMessageQueued → logSessionStateChange("processing") → ... → logSessionStateChange("idle") → logMessageProcessed lifecycle inside runCronIsolatedAgentTurn, using the prepared runSessionId / runSessionKey and a stable cron-isolated source label. This mirrors the dispatch path's lifecycle so state.queueDepth is incremented and decremented symmetrically and downstream subscribers see the full per-run lifecycle.

Problem

Any consumer of message.queued for per-session attribution — diagnostics exporters, OTel bridges, custom observability plugins — currently sees zero spans for cron-isolated runs. LLM calls fire, sessions exist, but downstream subscribers cannot distinguish runs from each other and tend to bucket them under whatever static fallback id their integration uses.

In a deployment with several daily isolated cron jobs over a 13-day window, an OTel subscriber observed 63 distinct cron-driven sessions in the native JSONL shipper but only 4 distinct session.id values reaching its trace store — a 94% gap that this single emission point closes.

Solution

Added paired diagnostic-event calls to runCronIsolatedAgentTurn:

  • logMessageQueued({sessionId, sessionKey, channel: "cron", source: "cron-isolated"}) after prepared.ok.
  • logSessionStateChange({state: "processing"}) immediately after queued.
  • logSessionStateChange({state: "idle"}) in finally (decrements state.queueDepth).
  • logMessageProcessed({outcome, durationMs, error?}) in finally.

Outcomes: success → completed; aborted/thrown/finalizer-error → error with reason.

Changes

  • src/cron/isolated-agent/run.ts — added the diagnostic lifecycle calls inside runCronIsolatedAgentTurn.
  • src/cron/isolated-agent/run.diagnostic-events.test.ts — new focused regression test asserting the queued → processing → idle → processed sequence with consistent sessionKey.
  • CHANGELOG.md — entry under ## Unreleased / ### Fixes.

Real Behavior Proof

  • Behavior or issue addressed: Subscribers to OpenClaw's diagnostic-events bus (e.g. OTel exporters) receive zero message.queued / message.processed events for isolated-cron agent turns, even though those runs make LLM calls and have a real session UUID. Downstream attribution buckets every cron-isolated run under whatever static fallback id the integration declares, hiding distinct sessions from traces.
  • Real environment tested: Local OpenClaw checkout built from this branch (pr/cron-isolated-message-queued, head 275f1bb430) on Linux 6.12.30 / Node 24.13.0. Gateway run as a systemd user unit (openclaw-gateway.service). A diagnostic-events subscriber registered via onDiagnosticEvent was attached to verify event emission per run.
  • Exact steps or command run after this patch:
pnpm install
pnpm tsgo:core
pnpm test src/cron/isolated-agent/run.diagnostic-events.test.ts
pnpm test src/cron/isolated-agent/run.session-key.test.ts \
          src/cron/isolated-agent/run.session-key-isolation.test.ts \
          src/cron/isolated-agent/run.payload-fallbacks.test.ts \
          src/cron/isolated-agent/run.meta-error-status.test.ts
pnpm build
systemctl --user restart openclaw-gateway
node dist/index.js cron run <isolated-job-id>
journalctl --user -u openclaw-gateway --since "30 seconds ago" \
  | grep -E "event: message\\.(queued|processed)|started root span"
  • Evidence after fix: Subscriber output from the live deployment after triggering an isolated cron job (session UUIDs and job ids redacted to short prefixes; agent label is generic, no IPs / chat ids / private endpoints):
[bridge] event: message.queued sessionKey: agent:main:cron:<jobId-prefix>:run:<runUuid-prefix> source: cron-isolated
[bridge] started root span for main session: agent:main:cron:<jobId-prefix>:run:<runUuid-prefix> agent: <agentId>
[bridge] event: message.processed sessionKey: agent:main:cron:<jobId-prefix>:run:<runUuid-prefix>

Tests:

pnpm tsgo:core              → exit 0
pnpm test src/cron/isolated-agent/run.diagnostic-events.test.ts
  Test Files  1 passed (1)
       Tests  1 passed (1)

pnpm test src/cron/isolated-agent/run.session-key.test.ts \
          src/cron/isolated-agent/run.session-key-isolation.test.ts \
          src/cron/isolated-agent/run.payload-fallbacks.test.ts \
          src/cron/isolated-agent/run.meta-error-status.test.ts
  Test Files  4 passed (4)
       Tests  17 passed (17)
  • Observed result after fix: Both lifecycle events fire with the run's sessionId / sessionKey. The bridge subscriber attached via onDiagnosticEvent receives message.queued, then session.state (processing → idle), then message.processed, all sharing the same sessionKey for the same isolated cron run. state.queueDepth returns to 0 after the run because the paired logSessionStateChange("idle") runs in finally. Distinct cron run UUIDs now reach downstream span/aggregation logic instead of collapsing into a single static fallback id.
  • What was not tested: The heartbeat-runner isolated-session path at src/infra/heartbeat-runner.ts:1266 shares a similar shape but is intentionally out of scope for this PR — left as a follow-up if maintainers agree the same seam belongs there. pnpm check was not run end-to-end; the changed-surface gates above plus the build are the proof bar this PR carries.

Notes

  • logMessageQueued runs after prepared.ok to avoid noisy queued/processed pairs for runs that bail out at preparation; matches the existing pattern where dispatch only logs after route resolution.
  • Both logSessionStateChange calls and logMessageProcessed run inside the finally block, ensuring the state.queueDepth decrement and processed event always fire even on early returns / throws.
  • No public-API changes; logMessageQueued / logSessionStateChange / logMessageProcessed are existing exports.

Review feedback addressed (rev 2, commit 275f1bb430)

  • P1 — taskLabel: dropped. The logMessageQueued call now passes only {sessionId, sessionKey, channel, source}, matching the helper's typed parameter and event schema on main.
  • P2 — queue-state lifecycle: added logSessionStateChange({state: "processing"}) immediately after logMessageQueued, and logSessionStateChange({state: "idle"}) in the finally block before logMessageProcessed. This pairs the state.queueDepth increment with its decrement and matches the dispatch path's lifecycle.
  • P3 — changelog: added entry under ## Unreleased / ### Fixes.
  • Real behavior proof: subscriber output included above (redacted).
  • Regression test: added run.diagnostic-events.test.ts covering queued / processing / idle / processed sequencing and session-key consistency.

🤖 Generated with Claude Code

@openclaw-barnacle openclaw-barnacle Bot added size: XS triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 8, 2026
@clawsweeper

clawsweeper Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed May 25, 2026, 1:15 PM ET / 17:15 UTC.

Summary
The branch adds message.queued, session.state, and message.processed diagnostic lifecycle events around isolated-cron agent turns, with regression tests and a changelog entry.

PR surface: Source +48, Tests +184, Docs +1. Total +233 across 3 files.

Reproducibility: yes. from source inspection: current main's isolated cron path prepares and executes a real agent turn without the diagnostic lifecycle calls already used by dispatch and embedded-runner paths. I did not run the runtime in this read-only review, but the missing call path is directly visible.

Review metrics: none identified.

Merge readiness
Overall: 🦞 diamond lobster
Proof: 🦞 diamond lobster
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

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

Next step before merge
No ClawSweeper repair lane is needed because there are no blocking findings; the remaining work is maintainer review and CI gating.

Security
Cleared: The diff uses existing diagnostic helpers and adds tests plus a changelog entry; it does not touch dependency, workflow, credential, packaging, or code-download surfaces.

Review details

Best possible solution:

Land this PR after maintainer review and CI if maintainers agree isolated-cron turns should share the existing diagnostic lifecycle contract; track heartbeat-runner parity separately if needed.

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

Yes from source inspection: current main's isolated cron path prepares and executes a real agent turn without the diagnostic lifecycle calls already used by dispatch and embedded-runner paths. I did not run the runtime in this read-only review, but the missing call path is directly visible.

Is this the best way to solve the issue?

Yes; reusing the existing diagnostic helpers and gating them through isDiagnosticsEnabled(params.cfg) is the narrowest maintainable fix without adding a new API or config surface.

AGENTS.md: found and applied where relevant.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 5ae91f01fa33.

Label changes

Label justifications:

  • P2: This fixes observability attribution for an existing cron-agent path with limited blast radius and focused coverage.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (logs): The PR body includes after-fix redacted diagnostic subscriber logs from a local gateway run showing queued and processed events for a real isolated cron run.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes after-fix redacted diagnostic subscriber logs from a local gateway run showing queued and processed events for a real isolated cron run.
Evidence reviewed

PR surface:

Source +48, Tests +184, Docs +1. Total +233 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 49 1 +48
Tests 1 184 0 +184
Docs 1 1 0 +1
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 234 1 +233

What I checked:

  • Repository policy read: Root AGENTS.md was read fully and applied; no scoped AGENTS.md owns src/cron/isolated-agent or CHANGELOG.md, and the only maintainer note present was Telegram-specific. (AGENTS.md:1, 3eb06e305e69)
  • Current main still lacks the cron lifecycle: origin/main runs executeCronRun and finalizes/disposes the isolated cron context without importing or calling the diagnostic lifecycle helpers in this path. (src/cron/isolated-agent/run.ts:1187, 5ae91f01fa33)
  • Existing dispatch lifecycle contract: The channel dispatch path already gates diagnostics with isDiagnosticsEnabled(cfg), emits message.queued, marks session.state processing, and later marks idle, which is the lifecycle this PR mirrors for cron. (src/auto-reply/reply/dispatch-from-config.ts:793, 3eb06e305e69)
  • Diagnostic helper contract: logMessageQueued increments queue depth, logSessionStateChange('idle') decrements it, logMessageProcessed emits the processed event, and isDiagnosticsEnabled returns false only for diagnostics.enabled: false. (src/logging/diagnostic.ts:638, 3eb06e305e69)
  • PR implementation uses existing helpers: At PR head, runCronIsolatedAgentTurn records start time, gates with isDiagnosticsEnabled(params.cfg), emits queued/processing before execution, records error outcome, and emits idle/processed in finally with the current run session id. (src/cron/isolated-agent/run.ts:1193, cbd0e7a9a0b9)
  • Regression coverage: The new test covers the queued -> processing -> idle -> processed sequence, the disabled diagnostics.enabled: false path, and final events using an adopted persisted run session id. (src/cron/isolated-agent/run.diagnostic-events.test.ts:66, cbd0e7a9a0b9)

Likely related people:

  • steipete: Current main's isolated cron runner file was added/split in a recent Peter Steinberger commit, and the latest PR branch commit also adjusts final diagnostics for rotated sessions. (role: recent area contributor; confidence: high; commits: b077c3a813b1, cbd0e7a9a0b9; files: src/cron/isolated-agent/run.ts, src/cron/isolated-agent/run.diagnostic-events.test.ts)
  • vincentkoc: Vincent Koc recently refactored lazy runtime boundaries in the same isolated cron runner path that this PR wraps with diagnostics. (role: recent cron runtime refactor contributor; confidence: medium; commits: a5980df10128, 101c16b0b133; files: src/cron/isolated-agent/run.ts)
  • shakkernerd: Shakker authored adjacent cron session/delivery/runtime refactors that shaped the surrounding isolated-agent execution path. (role: adjacent cron owner; confidence: medium; commits: de952c036abc, c95d1c101b34; files: src/cron/isolated-agent/run.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.

@arniesaha

Copy link
Copy Markdown
Contributor Author

Thanks @clawsweeper. All three findings addressed in 275f1bb430:

  • P1 — taskLabel: dropped from logMessageQueued call. Helper signature on main only accepts {sessionId, sessionKey, channel, source} and now matches.
  • P2 — queue-state lifecycle: wrapped the run with logSessionStateChange({state: "processing"}) after queued and logSessionStateChange({state: "idle"}) in finally before processed, so state.queueDepth increments and decrements pair up the same way the dispatch path does.
  • P3 — changelog: entry added under Unreleased / Fixes.

Also added a focused regression test at src/cron/isolated-agent/run.diagnostic-events.test.ts that subscribes via onDiagnosticEvent, runs runCronIsolatedAgentTurn through the existing test harness, and asserts the queued → processing → idle → processed sequence with consistent sessionKey across all events.

Live verification (redacted bridge subscriber output) added to the PR body.

Acceptance criteria status:

  • pnpm tsgo:core — green.
  • pnpm test src/cron/isolated-agent/run.session-key.test.ts — 8/8 passing.
  • pnpm test src/cron/isolated-agent/run.diagnostic-events.test.ts — 1/1 passing (covers queued/processed lifecycle + queue-state cleanup behavior via the session.state idle pairing).

@clawsweeper re-review.

@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 8, 2026
@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed proof: sufficient ClawSweeper judged the real behavior proof convincing. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 8, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 8, 2026
@arniesaha
arniesaha force-pushed the pr/cron-isolated-message-queued branch from 21e24aa to 0170cd9 Compare May 8, 2026 06:36
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 8, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 8, 2026
@arniesaha
arniesaha force-pushed the pr/cron-isolated-message-queued branch from 0170cd9 to 3690afc Compare May 10, 2026 16:23
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 10, 2026
@arniesaha
arniesaha force-pushed the pr/cron-isolated-message-queued branch from 6ede7c3 to c99d162 Compare May 10, 2026 16:33
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 10, 2026
@arniesaha
arniesaha force-pushed the pr/cron-isolated-message-queued branch from c99d162 to f792455 Compare May 12, 2026 08:00
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 12, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 12, 2026
@clawsweeper

clawsweeper Bot commented May 12, 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:

@arniesaha

Copy link
Copy Markdown
Contributor Author

Thanks @clawsweeper. Gating fix landed in 9e802930e7:

  • P2 — diagnostics gate: new lifecycle calls now wrapped in if (diagnosticsEnabled) where diagnosticsEnabled = isDiagnosticsEnabled(params.cfg), mirroring src/auto-reply/reply/dispatch-from-config.ts:368. When diagnostics.enabled: false the cron path emits zero message.queued/session.state/message.processed events.
  • Regression coverage: added a second test case in src/cron/isolated-agent/run.diagnostic-events.test.ts that passes cfg: { diagnostics: { enabled: false } } and asserts no lifecycle events are emitted.
  • Changelog: entry updated to call out the master-toggle behavior.

@clawsweeper re-review.

@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 12, 2026
@clawsweeper

clawsweeper Bot commented May 12, 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:

@arniesaha
arniesaha force-pushed the pr/cron-isolated-message-queued branch from 9e80293 to 1af5584 Compare May 12, 2026 16:13
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 12, 2026
@arniesaha
arniesaha force-pushed the pr/cron-isolated-message-queued branch from 1af5584 to ac85a4b Compare May 12, 2026 20:44
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 22, 2026
@arniesaha
arniesaha force-pushed the pr/cron-isolated-message-queued branch from b5e283d to 289238a Compare May 25, 2026 04:34
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 25, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels May 25, 2026
@arniesaha arniesaha closed this May 25, 2026
@arniesaha arniesaha reopened this May 25, 2026
@arniesaha
arniesaha force-pushed the pr/cron-isolated-message-queued branch from 289238a to 734264b Compare May 25, 2026 14:18
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 25, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. labels May 25, 2026
@steipete
steipete force-pushed the pr/cron-isolated-message-queued branch from 734264b to 9328a27 Compare May 25, 2026 16:28
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 25, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels May 25, 2026
arniesaha and others added 3 commits May 25, 2026 17:44
…sion.state lifecycle, add regression test + changelog
Address clawsweeper P2: cron isolated-agent lifecycle (message.queued,
session.state, message.processed) now mirrors the dispatch path and
respects the diagnostics.enabled master toggle. Added regression test
for the disabled-config path.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@steipete
steipete force-pushed the pr/cron-isolated-message-queued branch from 9328a27 to dbbeb7b Compare May 25, 2026 16:44
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 25, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 25, 2026
@steipete

Copy link
Copy Markdown
Contributor

Verification before landing:

  • Rebased head: cbd0e7a
  • Focused proof: node scripts/run-vitest.mjs src/cron/isolated-agent/run.diagnostic-events.test.ts src/cron/isolated-agent/run.session-key.test.ts src/cron/isolated-agent/run.session-key-isolation.test.ts src/cron/isolated-agent/run.payload-fallbacks.test.ts src/cron/isolated-agent/run.meta-error-status.test.ts --reporter=dot (24 tests passed)
  • Auto Review: clean after rotated-session diagnostic fix, no accepted/actionable findings
  • GitHub CI: green on the exact pushed head
  • Current main merge sanity: clean merge-tree against origin/main

Thanks @arniesaha!

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

Labels

P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: M 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.

2 participants