Skip to content

fix(cron): infer agentId from workspace when adding jobs - #54307

Closed
velamints2 wants to merge 1 commit into
openclaw:mainfrom
velamints2:fix/cron-infer-agent-from-workspace
Closed

velamints2 wants to merge 1 commit into
openclaw:mainfrom
velamints2:fix/cron-infer-agent-from-workspace

Conversation

@velamints2

Copy link
Copy Markdown
Contributor

PR Draft: fix(cron): inherit current agentId when creating jobs from routed agent context

Summary

Fixes a multi-agent isolation bug where cron jobs created without explicit --agent can be persisted with no agentId, then executed under default agent (main).

In routed environments (e.g., Feishu group -> sales), this causes cross-agent leakage: job outputs appear in sales channel while execution context is main.

Behavior before

  • Cron job persisted with agentId = null
  • Scheduler resolved to default agent (main)
  • Run history session key looked like agent:main:cron:<id>:run:<sessionId>

Behavior after

  • If cron add is called from an active agent/session context and --agent is absent:
    • auto-set job.agentId = currentAgentId
  • If no context is available:
    • keep existing default behavior (or optionally warn)
  • cron list and cron runs show explicit agentId

Proposed code touch points (indicative)

  1. src/cron/add.ts (or equivalent add command handler)
    • inject agentId from invocation context when omitted
  2. src/cron/store.ts
    • ensure agentId persisted and typed
  3. src/cron/scheduler.ts
    • prefer explicit job.agentId and avoid silent fallback when in multi-agent mode
  4. src/cron/runs.ts
    • include agentId field in run entries/output for observability
  5. tests
    • add unit/integration case: routed sales context + no --agent -> persisted agentId=sales

Acceptance criteria

  • Creating cron from sales routed context without --agent yields Agent ID = sales
  • New run history uses sessionKey with agent:sales:cron:...
  • Existing behavior with explicit --agent unchanged

Notes

Current user-level workaround:

openclaw cron edit <id> --agent sales --session isolated --announce --channel feishu --to chat:oc_xxx

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes size: S labels Mar 25, 2026
@clawsweeper

clawsweeper Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

Closing this as implemented after Codex automated review.

Current main already handles the routed-session leakage described by the PR: agent-created cron jobs inherit the active session agentId when the job omits agentId, cron storage persists it, and isolated cron execution uses that agentId for the cron session key and workspace. The draft PR's CWD-based CLI inference is no longer needed for the routed agent/session context bug it describes.

Best possible solution:

Close this draft PR as already implemented for the routed agent/session context bug. Keep the current session-derived agentId behavior for agent-created cron jobs and the explicit --agent CLI path; if maintainers want shell CWD-based CLI inference later, track it as a separate CLI UX request.

What I checked:

  • Routed tools receive active session context: Gateway tool resolution creates OpenClaw tools with the active session key, and createOpenClawTools passes that key into createCronTool as agentSessionKey. (src/gateway/tool-resolution.ts:83, 06d409dc2738)
  • Cron tool fills missing agentId from session: For cron add, the cron tool normalizes the job with sessionContext, then when agentId is absent resolves the session agent via resolveSessionAgentId({ sessionKey: opts.agentSessionKey, config: cfg }) and assigns it to job.agentId before calling cron.add. (src/agents/tools/cron-tool.ts:588, 06d409dc2738)
  • Cron jobs persist explicit agentId: createJob stores normalizeOptionalAgentId(input.agentId) on the persisted CronJob. (src/cron/service/jobs.ts:587, 06d409dc2738)
  • Execution prefers job.agentId over default agent: prepareCronRunContext chooses input.agentId or job.agentId before falling back to the default agent, then builds the agent-scoped cron session key and resolves that agent's workspace. (src/cron/isolated-agent/run.ts:453, 06d409dc2738)
  • Regression coverage for isolated cron identity: The isolated cron identity test asserts a job with agentId ops runs under sessionKey agent:ops:cron:job-ops and uses the ops workspace/session path. (src/cron/isolated-agent.session-identity.test.ts:62, 06d409dc2738)
  • Release contains the implementation: The latest release tag v2026.4.24 contains the same cron-tool session-agent stamping path. (src/agents/tools/cron-tool.ts:540, cbcfdf62c729)

So I’m closing this as already implemented rather than keeping a duplicate issue open.

Codex Review notes: model gpt-5.5, reasoning high; reviewed against 06d409dc2738; fix evidence: release v2026.4.24, commit cbcfdf62c729.

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

Labels

cli CLI command changes size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant