Skip to content

fix(cron): reject add/update when job name collides with existing job (#76160) - #76180

Closed
hclsys wants to merge 3 commits into
openclaw:mainfrom
hclsys:fix/76160-cron-duplicate-name-collision
Closed

hclsys wants to merge 3 commits into
openclaw:mainfrom
hclsys:fix/76160-cron-duplicate-name-collision

Conversation

@hclsys

@hclsys hclsys commented May 2, 2026

Copy link
Copy Markdown

Problem

`cron add` and `cron edit` silently accepted new jobs whose `name` collided with an existing job's `name`. Two distinct rows with identical names landed in `jobs.json`, each with its own `id`, `schedule`, and `enabled` state. If both rows were enabled they fired twice per cycle with divergent prompts; if one was disabled, the duplicate was invisible in the default `cron list` output (disabled rows filtered unless `--all`). `cron edit --name ` could also silently rename onto an existing job through the same gap in `ops.update`.

Fixes #76160.

Root cause

`ops.add` called `state.store.jobs.push(job)` with no name uniqueness check.
`ops.update` applied the name patch and replaced the job by ID without checking sibling names.

Changes

`src/cron/service/jobs.ts`

  • `findJobByName(state, name)`: finds an existing job by normalized name (same pattern as `findJobOrThrow` for IDs).

`src/cron/service/ops.ts`

  • `add`: throws before `push` when `findJobByName` returns a match. Error message includes the colliding `id` so operators know what to remove.
  • `update`: throws after `applyJobPatch` when the patched name collides with a different job. Same-job rename (keeping the current name) is allowed.

Test result

`11 passed (11)` in `src/cron/service/ops.test.ts`. 4 new cases:

  • refuses `add` when a job with the same name already exists
  • refuses `add` even when the existing job is disabled
  • refuses `update` rename onto an existing name (different ID)
  • allows `update` to keep the same name on the same job

`oxlint`: 0 warnings, 0 errors on all changed files.

@clawsweeper

clawsweeper Bot commented May 2, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

Summary
The branch adds normalized cron job-name collision checks to service add/update, service and Gateway regression tests, and an Unreleased changelog entry.

Reproducibility: yes. Current main gives a high-confidence source-level reproduction: add pushes a second normalized name without a sibling check, and update can patch a job name before replacement without checking sibling names; the linked issue also provides CLI steps.

Next step before merge
No automated repair job is needed; the remaining action is maintainer review and final exact-head CI, not a discrete file-level fix.

Security
Cleared: The diff only changes cron service validation, cron/Gateway tests, and changelog text; it does not touch dependencies, workflows, permissions, secrets, package metadata, or artifact execution.

Review details

Best possible solution:

Land the service-level duplicate-name guard after exact-head CI passes, and track --replace or legacy duplicate-list warnings separately if maintainers want that UX.

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

Yes. Current main gives a high-confidence source-level reproduction: add pushes a second normalized name without a sibling check, and update can patch a job name before replacement without checking sibling names; the linked issue also provides CLI steps.

Is this the best way to solve the issue?

Yes for the default rejection fix. A service-level RangeError guard after name normalization covers CLI, agent-tool, Gateway, and future callers while preserving the existing INVALID_REQUEST mapping.

What I checked:

  • Current-main add gap: cron add creates a normalized job and immediately pushes it into state.store.jobs without checking existing job names. (src/cron/service/ops.ts:319, 10448a0ad14c)
  • Current-main update gap: cron update applies a name patch to a cloned job before replacement and does not compare the patched name with sibling jobs. (src/cron/service/ops.ts:357, 10448a0ad14c)
  • Normalization contract: createJob and applyJobPatch both pass cron names through normalizeRequiredName, which trims and rejects blank names; checking after these calls matches current normalization behavior. (src/cron/service/jobs.ts:593, 10448a0ad14c)
  • Gateway error contract: Gateway cron.add and cron.update already map TypeError and RangeError from the cron service to INVALID_REQUEST, so the PR's RangeError choice fits the existing RPC contract. (src/gateway/server-methods/cron.ts:295, 10448a0ad14c)
  • PR implementation: The PR patch adds findJobByName, rejects add collisions before push, and rejects update name collisions when the matching job has a different id. (src/cron/service/ops.ts:318, ad51168650bf)
  • Regression coverage: The PR patch adds service tests for duplicate add, disabled duplicate add, rename collision, same-name self-update, and Gateway INVALID_REQUEST handling for duplicate-name RangeError responses. (src/cron/service/ops.test.ts:354, ad51168650bf)

Likely related people:

  • steipete: Recent path history shows repeated cron service, Gateway cron, validation-test, and changelog maintenance near the touched surface. (role: recent maintainer; confidence: medium; commits: c06739d773da, 43121fb0968a, d87f8cc1420f; files: src/cron/service/jobs.ts, src/gateway/server-methods/cron.ts, src/gateway/server-methods/cron.validation.test.ts)
  • tyler6204: Earlier merged cron scheduler, store hardening, isolated delivery, and Gateway cron work touched the core add/update behavior and Gateway cron paths. (role: introduced behavior / major cron hardening; confidence: medium; commits: d90cac990c8f, 3a03e38378e2, 38543d819690; files: src/cron/service/ops.ts, src/cron/service/jobs.ts, src/gateway/server-methods/cron.ts)
  • yelog: Recent Gateway cron validation work established the TypeError/RangeError INVALID_REQUEST mapping that this PR relies on. (role: adjacent owner; confidence: medium; commits: d2db67e6931b; files: src/gateway/server-methods/cron.ts, src/gateway/server-methods/cron.validation.test.ts)
  • vincentkoc: Recent cron service hardening commits touched interrupted startup runs and malformed legacy jobs around the same service files. (role: recent adjacent maintainer; confidence: low; commits: 924271385ba9, 2896107153e5; files: src/cron/service/ops.ts, src/cron/service/jobs.ts)

Remaining risk / open question:

  • Exact-head CI was still in progress during review, so the final merge gate was not fully proven.
  • The linked issue's optional --replace and duplicate-list-warning ideas remain outside this PR, but they are separable product follow-ups.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 10448a0ad14c.

@openclaw-barnacle openclaw-barnacle Bot added the gateway Gateway runtime label May 2, 2026
HCL and others added 3 commits May 3, 2026 04:18
…openclaw#76160)

Two cron.add calls with identical names pushed both rows into jobs.json with no
validation. If both were enabled they fired twice per cycle with divergent
prompts; if one was disabled the duplicate was invisible in the default cron list
view (disabled rows filtered). cron.update could also silently rename onto an
existing name through the same gap.

Add findJobByName helper in jobs.ts and guard both paths in ops.ts:
- add: throw before push if any existing job has the same normalized name
- update: throw after applyJobPatch if the patched name collides with a
  different job (same-job rename onto itself is allowed)

Error messages include the colliding job's id so operators can decide whether
to remove the old job or choose a different name.

Tests (4 new in ops.test.ts): refuse on collision, refuse when existing job is
disabled, refuse rename onto existing name, allow rename onto same name. 11/11.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…claw#76160)

Address clawsweeper P2: plain Error from duplicate-name guards was
rethrown as UNAVAILABLE. Change to RangeError so the gateway
cron.add/cron.update handlers map it to INVALID_REQUEST.

Add gateway regression tests for the duplicate-name INVALID_REQUEST
path in cron.validation.test.ts.

Tests: 41/41 (ops.test.ts + cron.validation.test.ts). oxlint: 0/0.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@hclsys

This comment was marked as low quality.

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

Labels

gateway Gateway runtime size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cron: cron add/edit silently allows duplicate name collisions

1 participant