Skip to content

feat: add lastRun() introspection getter#557

Merged
merencia merged 2 commits into
mainfrom
feat/last-run-getter
Jun 21, 2026
Merged

feat: add lastRun() introspection getter#557
merencia merged 2 commits into
mainfrom
feat/last-run-getter

Conversation

@merencia

Copy link
Copy Markdown
Member

Summary

Adds a lastRun() introspection getter to the public ScheduledTask interface. It returns information about the last actual execution of a task, or null if the task has not run yet.

Return type (LastRun, exported from the package):

{ date: Date, result?: unknown, error?: Error } | null
  • On a successful run: { date, result }.
  • On a failed run: { date, error }.
  • Before the first execution: null.

Key semantics

date reflects when the execution actually ran (the execution's own finish time), not when a scheduler tick was merely checked. This avoids the well-known competitor bug of reporting the tick-check time instead of the real execution time. The value is taken from the runner's per-execution finishedAt (falling back to startedAt), which is stamped the moment the task settles.

Implementation

  • Inline tasks: recorded from the runner's onFinished / onError hooks, which already carry the Execution object with its real timestamps, result, and error.
  • Background tasks: mirrored in the parent process from the daemon's forwarded execution:finished / execution:failed events, coercing IPC-serialized timestamps back to Date.

LastRun is exported from the package entry point and appears in the generated .d.ts.

Tests

Added coverage for both task types:

  • returns null before the first execution
  • returns the real execution date and result after a successful run
  • returns the date and the error after a failing run
  • the returned date reflects the actual execution time, not a tick check (inline)
  • coerces an ISO-string timestamp from IPC back to a Date (background)
  • updates to the most recent execution on each run

npm run check (lint + vitest with coverage) passes fully: 384 tests, src/tasks inline/interface files at 100% coverage.

Docs

README updated with a short runtime-introspection example covering getNextRun() and lastRun().

merencia added 2 commits June 19, 2026 23:58
Add a lastRun() method to the ScheduledTask interface returning info about
the last actual execution, or null if the task has not run yet. The return
type is { date, result } on success or { date, error } on failure, where
date reflects when the execution really ran (the execution's own finish
time), not when a scheduler tick was merely checked.

Implemented for both inline and background tasks. The inline task records
it from the runner's finished/error hooks; the background task mirrors it
in the parent from the daemon's forwarded events, coercing IPC-serialized
timestamps back to Date.
The defensive timestamp fallbacks in recordLastRun were uncovered, which
dropped coverage. The runner always sets finishedAt before the
finished/failed hooks (scheduler/runner.ts), so the inline path now reads
it directly instead of branching through startedAt/new Date(). The
background path keeps the IPC string coercion and now has tests for the
startedAt fallback, the no-timestamp fallback, and the no-execution guard.
@merencia
merencia merged commit 5d858ca into main Jun 21, 2026
6 checks passed
@merencia
merencia deleted the feat/last-run-getter branch June 21, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant