feat(tools): add "scheduler" toolset to run instructions on a time or recurring interval #3632
@Sayt-0 @aheritier
Hi @Sayt-0, the linting problems are now fixed. Could you please review the PR when you have a chance? Thanks!
gofmt flags this file: the const block became misaligned when loopMaxWait was added.
category = "scheduler"
loopMaxWait = time.Minute
task lint runs golangci-lint with the gofmt/gofumpt formatters, so this fails as soon as the Go CI runs.
perfsprint (enabled in .golangci.yml) rejects fmt.Errorf without format arguments; errors.New is the repo pattern (see e.g. pkg/tools/builtin/shell/askpass.go).
return time.Time{}, 0, errors.New("empty schedule spec")
Requires adding errors to the imports.
.golangci.yml has an explicit forbidigo rule scoped to _test.go files: "do not use context.Background() in tests, use t.Context()" (usetesting is enabled as well). Every test in this package uses context.Background(), so lint will fail once the Go CI runs.
Applies to all occurrences in scheduler_test.go, including TestStartStopIsClean. backgroundjobs_test.go shows the expected pattern.
Fired schedules can be lost silently here, in two ways:
popDue mutates the store before delivery is attempted: one-shots are removed and recurring schedules re-armed. When rt == nil the popped one-shot is dropped without a trace; when Recall returns an error the function returns and the rest of the popped batch is dropped as well. No log is emitted in either case, whereas backgroundjobs logs a slog.WarnContext when Recall fails (backgroundjobs.go:302).createSchedule: t.store.add(...) runs before t.setRuntime(rt) (L71-L76), so a near-immediate schedule (e.g. in:1ns) can in theory be popped while rt is still nil.Suggested shape:
createSchedule, set the runtime before adding to the store;rt == nil before calling popDue;Recall error, log a warning and continue with the remaining schedules (or re-queue the undelivered ones) instead of returning.A test covering the failing-Recall path would lock the behavior in.
No lower bound is enforced on recurring intervals: every:100ms is accepted. Each fire injects a message into the agent loop and typically triggers an LLM turn, so a typo like every:1s instead of every:1h becomes an accidental token-burning loop. The fire loop follows untilNext exactly, so nothing else throttles it.
Options:
The first option seems safer for a v1; if the second seems more appropriate, a short note in docs/tools/scheduler/index.md would do.
Thanks for the contribution. The overall design is sound: reusing Runtime.Recall keeps every scheduled action under the agent's normal tools and permissions, the parse/store/toolset split is clean, and the parser/store test coverage is solid. A few issues need to be addressed before merge.
Note: the Go CI jobs (lint/test) have not run on this PR yet, only the docs checks are visible, so the lint findings below do not show on the checks tab. They were reproduced locally on this branch. The PR description states gofmt -l is clean, but gofmt -l pkg/tools/builtin/scheduler/ reports scheduler.go.
| # | Finding | Severity | Where |
| --- | --- | --- | --- |
| 1 | gofmt fails (const block alignment) | blocks CI | pkg/tools/builtin/scheduler/scheduler.go |
| 2 | context.Background() in tests, forbidden by the repo's forbidigo rule (t.Context() required) | blocks CI | all new _test.go files |
| 3 | fmt.Errorf without format verbs, rejected by perfsprint | blocks CI | pkg/tools/builtin/scheduler/schedule.go |
| 4 | Fired schedules are silently lost when Recall fails or the runtime is nil | correctness | pkg/tools/builtin/scheduler/scheduler.go |
| 5 | No minimum recurring interval (every:100ms accepted), recall-storm footgun | design | pkg/tools/builtin/scheduler/schedule.go |
Details in the inline comments. Points 1 to 3 are mechanical; point 4 is the main correctness concern; point 5 may be a deliberate choice but deserves either a floor or an explicit note in the docs.
Done.
Done.
Done.
Done. Fixed.
This is also done.
@Sayt-0 Thank you for the review - it was very precise and the reproduction notes made it easy to follow. I have pushed an update addressing all five points:
1. gofmt (const block alignment): Aligned the category / loopMaxWait const block in scheduler.go. gofmt -l pkg/tools/builtin/scheduler/ is now clean.
2. forbidigo (context.Background() in tests): Replaced every occurrence in the package with t.Context(), following the backgroundjobs_test.go pattern (including TestStartStopIsClean). The remaining context.Background() in scheduler.go is production code, which the rule excludes via path-except: _test\.go, so it is intentionally left as is — the fire loop needs a context that outlives Start.
3. perfsprint (fmt.Errorf without verbs): Switched to errors.New("empty schedule spec") and added the errors import. That was the only verb-less call; the others carry %q/%w and are unchanged.
4. Fired schedules silently lost: Fixed all three parts you identified:
- createSchedule now publishes the runtime with setRuntime(rt) before store.add(...), closing the ordering race where a near-immediate schedule could be popped while rt was still nil.
- fireDue now returns early when rt == nil before calling popDue, so schedules stay armed instead of being popped into the void.
- A failing Recall now logs slog.WarnContext (mirroring backgroundjobs.go:302) and continues with the rest of the batch instead of returning and dropping it.
- Locked in with two tests: TestFireDueContinuesAfterRecallError (both due schedules are still attempted when the first Recall fails) and TestFireDueWithoutRuntimeKeepsSchedules (a due one-shot survives a fire with no runtime).
5. Minimum recurring interval: Went with your first option and enforced a floor — minRecurringInterval = 1m, rejected with a clear error below that. It applies to recurring schedules only (every: and the presets); one-shot in: / at: are deliberately not floored since they fire once and cannot storm. 1m matches the smallest preset (minutely). Documented in docs/tools/scheduler/index.md, and covered by parser tests for below-floor, just-below-floor, at-floor and short one-shot.
Do let me know if any change is required please.
LGTM !
Approved these changes.
Sayt-0
dwin-gharibi
Closes #3631
---
Summary
Adds a new built-in toolset,
scheduler, that lets an agent schedule an instruction to run once at a time, after a delay, or on a recurring interval (every:1h, orminutely/hourly/daily/weekly). When a schedule is due, the instruction is injected back into the agent loop via the runtime's recall mechanism and the agent carries out the action with its normal tools.Tools:
create_schedule,list_schedules,cancel_schedule.Motivation
background_jobscan run work now and recall on completion, but nothing lets an agent make something happen later or repeatedly - e.g. "every 15 minutes, check if origin/main moved," or "at 09:00, post the standup summary." The scheduler fills that gap and lets docker-agent drive timed / recurring workflows during a live session.Design - execution model
The scheduler does not execute shell/api itself. When a schedule fires it calls
Runtime.Recall(ctx, <instruction>)and the agent performs the action with its own tools. This was a deliberate choice:Recallexactly as designed ("background work completed → inject a message"), the same primitivebackground_jobsuses;shelltool instead of adding a second, unattended command runner.A deterministic-execution variant (the scheduler runs a shell command itself) is noted as a possible follow-up but is intentionally out of scope here.
Lifecycle: the toolset implements
tools.Startable;Stopcancels the fire loop so no goroutine outlives the session. Schedules live in memory (not persisted across restarts). Scheduling requires host recall support (Runtime.Supports(CapabilityRecall)); otherwisecreate_schedulereturns a clear error.What changed
| File | Change |
| --- | --- |
|
pkg/tools/builtin/scheduler/schedule.go| New.parseWhen— spec → first fire time + interval. ||
pkg/tools/builtin/scheduler/store.go| New. In-memory schedule store: add / list / cancel /popDue(re-arm recurring, skip missed slots) /untilNext. Clock-injected, pure. ||
pkg/tools/builtin/scheduler/scheduler.go| New. TheToolSet: 3 tools,Recallfiring,Startablefire loop, instructions. ||
pkg/tools/builtin/scheduler/*_test.go| New. Parser, store, tool-handler, firing, and lifecycle tests. ||
pkg/teamloader/toolsets/toolsets.go| Register theschedulercreator. ||
pkg/teamloader/toolsets/catalog.go| Add theschedulercatalog entry (required by the drift-guard test). ||
docs/configuration/tools/index.md| Addschedulerto the built-in toolsets table. ||
docs/tools/scheduler/index.md| New. Dedicated docs page. |The existing catalog drift-guard test (
TestBuiltinToolsetsCatalogMatchesRegistry) forces the catalog entry: registering the type without documenting it fails the build.Example
Testing
parseWhen: all spec forms + rejections (empty, garbage, pastat:, non-positiveevery:).store: add/list ordering, cancel, one-shot removal, recurring re-arm, missed-slot skipping,untilNextclamping.Recall,Start/Stopclean shutdown.TestBuiltinToolsetsCatalogMatchesRegistrypasses withschedulerin both registry and catalog.All pass;
gofmt -landgo vetclean. (CI runstask lint/task test.)Scope / non-goals