Skip to content

refactor(cron): split main and detached dispatch#57482

Merged
vincentkoc merged 6 commits intomainfrom
pr5/cron-target-split
Mar 30, 2026
Merged

refactor(cron): split main and detached dispatch#57482
vincentkoc merged 6 commits intomainfrom
pr5/cron-target-split

Conversation

@vincentkoc
Copy link
Copy Markdown
Member

Summary

  • split cron execution into explicit main-session and detached dispatch helpers
  • route cron task-run lifecycle writes through executor helpers instead of the task registry
  • update cron seam coverage to target the executor-backed failure paths

Testing

  • pnpm test -- src/cron/service/ops.test.ts src/cron/service/timer.test.ts src/tasks/task-executor.test.ts src/tasks/task-registry.test.ts
  • pnpm build

@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Mar 30, 2026
@vincentkoc vincentkoc self-assigned this Mar 30, 2026
Base automatically changed from pr4/subagent-executor to main March 30, 2026 04:59
@vincentkoc vincentkoc marked this pull request as ready for review March 30, 2026 04:59
@vincentkoc vincentkoc merged commit 1c90538 into main Mar 30, 2026
9 checks passed
@vincentkoc vincentkoc deleted the pr5/cron-target-split branch March 30, 2026 04:59
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 30, 2026

Greptile Summary

This PR refactors the cron and agent task-lifecycle wiring in two complementary directions: (1) it introduces a task-executor.ts façade that wraps task-registry.ts with intent-revealing helpers (createRunningTaskRun, completeTaskRunByRunId, failTaskRunByRunId, …), forcing callers to encode success/failure semantics at the call site rather than passing raw status strings; and (2) it extracts message-formatting and delivery-policy predicates into a standalone task-executor-policy.ts that can be unit-tested independently. The executeJobCore function in timer.ts is also decomposed into executeMainSessionCronJob and executeDetachedCronJob to make the two dispatch paths explicit.

Key points:

  • All behavioral logic in the extracted helpers is identical to what was previously inlined in task-registry.ts — this is a pure restructuring.
  • The success/failure branch split in markBackgroundTaskTerminal (manager.core.ts) and subagent-registry-lifecycle.ts is correct; terminalOutcome is forwarded only on the success branch where it is semantically meaningful.
  • The abort-signal handling in executeMainSessionCronJob inlines { status: \"error\", error: timeoutErrorMessage() } directly instead of delegating to resolveAbortError() — the two are identical in value.
  • createCronTaskRunId is defined identically in both timer.ts (line 127) and ops.ts (line 389) — a duplication that should be extracted to a shared location to avoid future divergence.
  • The startedAt field that was previously forwarded through markTaskTerminalByRunId in subagent-registry-lifecycle.ts is not exposed by completeTaskRunByRunId/failTaskRunByRunId; in practice this is a no-op since startedAt is set correctly at creation time, but the API narrowing is worth documenting.

Confidence Score: 5/5

Safe to merge — all findings are P2 style/observability concerns with no impact on runtime correctness.

All detected issues are P2: one duplicated helper function and one silently-dropped field that is a no-op in practice. No logic regressions, security issues, or data-integrity problems were found. The refactoring is well-structured and backed by new unit and integration tests.

src/cron/service/timer.ts and src/cron/service/ops.ts share a duplicated createCronTaskRunId helper that should be extracted.

Important Files Changed

Filename Overview
src/tasks/task-executor.ts New thin façade over task-registry: provides named, intent-revealing helpers and bakes in the success/failure status split so callers never pass raw status strings.
src/tasks/task-executor-policy.ts Pure extraction of policy/formatting functions from task-registry.ts; function bodies are identical to what was removed.
src/cron/service/timer.ts Switches to task-executor helpers and extracts executeMainSessionCronJob/executeDetachedCronJob. createCronTaskRunId is duplicated with ops.ts.
src/cron/service/ops.ts Parallel refactoring of the manual-run path with correct ok/skipped→complete and error/timeout→fail routing. createCronTaskRunId duplicated from timer.ts.
src/agents/subagent-registry-lifecycle.ts Terminal update split correctly into completeTaskRunByRunId/failTaskRunByRunId; startedAt: entry.startedAt silently dropped (currently a no-op).

Comments Outside Diff (1)

  1. src/agents/subagent-registry-lifecycle.ts, line 226-244 (link)

    P2 startedAt silently dropped on terminal update

    The previous call to markTaskTerminalByRunId included startedAt: entry.startedAt, which allowed the terminal write to backfill the startedAt field if it differed from what was stored at creation time. Neither completeTaskRunByRunId nor failTaskRunByRunId exposes a startedAt parameter, so this field is now silently dropped.

    In practice the task record is created with the same startedAt value in subagent-registry-run-manager.ts, so there should be no observable delta today. But this is a silent narrowing of the API: callers that previously relied on terminal-time correction of startedAt can no longer do so through the new helpers. Worth noting in a comment or ensuring neither helper needs to accept startedAt for future use cases.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: src/agents/subagent-registry-lifecycle.ts
    Line: 226-244
    
    Comment:
    **`startedAt` silently dropped on terminal update**
    
    The previous call to `markTaskTerminalByRunId` included `startedAt: entry.startedAt`, which allowed the terminal write to backfill the `startedAt` field if it differed from what was stored at creation time. Neither `completeTaskRunByRunId` nor `failTaskRunByRunId` exposes a `startedAt` parameter, so this field is now silently dropped.
    
    In practice the task record is created with the same `startedAt` value in `subagent-registry-run-manager.ts`, so there should be no observable delta today. But this is a silent narrowing of the API: callers that previously relied on terminal-time correction of `startedAt` can no longer do so through the new helpers. Worth noting in a comment or ensuring neither helper needs to accept `startedAt` for future use cases.
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/cron/service/timer.ts
Line: 127-129

Comment:
**Duplicated helper in `ops.ts`**

`createCronTaskRunId` is defined identically in both `src/cron/service/timer.ts` and `src/cron/service/ops.ts` (line 389). If the run-ID format ever needs to change (e.g. adding a counter for jobs that fire more than once per millisecond), both copies must be updated in lockstep, creating a silent divergence risk.

Consider extracting this to a shared module — e.g. a `cron-run-id.ts` utility or placing it in `jobs.ts` — and importing it in both files.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/subagent-registry-lifecycle.ts
Line: 226-244

Comment:
**`startedAt` silently dropped on terminal update**

The previous call to `markTaskTerminalByRunId` included `startedAt: entry.startedAt`, which allowed the terminal write to backfill the `startedAt` field if it differed from what was stored at creation time. Neither `completeTaskRunByRunId` nor `failTaskRunByRunId` exposes a `startedAt` parameter, so this field is now silently dropped.

In practice the task record is created with the same `startedAt` value in `subagent-registry-run-manager.ts`, so there should be no observable delta today. But this is a silent narrowing of the API: callers that previously relied on terminal-time correction of `startedAt` can no longer do so through the new helpers. Worth noting in a comment or ensuring neither helper needs to accept `startedAt` for future use cases.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "Merge branch 'main' into pr5/cron-target..." | Re-trigger Greptile

Comment thread src/cron/service/timer.ts
Comment on lines +127 to +129
function createCronTaskRunId(jobId: string, startedAt: number): string {
return `cron:${jobId}:${startedAt}`;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Duplicated helper in ops.ts

createCronTaskRunId is defined identically in both src/cron/service/timer.ts and src/cron/service/ops.ts (line 389). If the run-ID format ever needs to change (e.g. adding a counter for jobs that fire more than once per millisecond), both copies must be updated in lockstep, creating a silent divergence risk.

Consider extracting this to a shared module — e.g. a cron-run-id.ts utility or placing it in jobs.ts — and importing it in both files.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cron/service/timer.ts
Line: 127-129

Comment:
**Duplicated helper in `ops.ts`**

`createCronTaskRunId` is defined identically in both `src/cron/service/timer.ts` and `src/cron/service/ops.ts` (line 389). If the run-ID format ever needs to change (e.g. adding a counter for jobs that fire more than once per millisecond), both copies must be updated in lockstep, creating a silent divergence risk.

Consider extracting this to a shared module — e.g. a `cron-run-id.ts` utility or placing it in `jobs.ts` — and importing it in both files.

How can I resolve this? If you propose a fix, please make it concise.

Alix-007 pushed a commit to Alix-007/openclaw that referenced this pull request Mar 30, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy

* refactor(tasks): route acp through executor

* refactor(tasks): route subagents through executor

* refactor(cron): split main and detached dispatch
alexjiang1 pushed a commit to alexjiang1/openclaw that referenced this pull request Mar 31, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy

* refactor(tasks): route acp through executor

* refactor(tasks): route subagents through executor

* refactor(cron): split main and detached dispatch
pgondhi987 pushed a commit to pgondhi987/openclaw that referenced this pull request Mar 31, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy

* refactor(tasks): route acp through executor

* refactor(tasks): route subagents through executor

* refactor(cron): split main and detached dispatch
pgondhi987 pushed a commit to pgondhi987/openclaw that referenced this pull request Mar 31, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy

* refactor(tasks): route acp through executor

* refactor(tasks): route subagents through executor

* refactor(cron): split main and detached dispatch
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy

* refactor(tasks): route acp through executor

* refactor(tasks): route subagents through executor

* refactor(cron): split main and detached dispatch
Tardisyuan pushed a commit to Tardisyuan/openclaw that referenced this pull request Apr 30, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy

* refactor(tasks): route acp through executor

* refactor(tasks): route subagents through executor

* refactor(cron): split main and detached dispatch
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy

* refactor(tasks): route acp through executor

* refactor(tasks): route subagents through executor

* refactor(cron): split main and detached dispatch
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy

* refactor(tasks): route acp through executor

* refactor(tasks): route subagents through executor

* refactor(cron): split main and detached dispatch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant