Skip to content

fix(cli): a lease renewal the backend never answered no longer stops the run's monitor - #119

Merged
ashot merged 5 commits into
codecast-sh:mainfrom
heyankitpatel:fix/lease-renew-unreachable
Nov 23, 2026
Merged

ashot merged 5 commits into
codecast-sh:mainfrom
heyankitpatel:fix/lease-renew-unreachable

Conversation

@heyankitpatel

Copy link
Copy Markdown
Contributor

Bug

A trigger run holds a 5-minute lease that the daemon renews every 60 s. SyncService.renewTaskLease turned every exception into false, and the run's heartbeat read false as "the lease is gone": it logged Lease renewal failed for task …, stopping monitor and cleared the run's timer for good. A network timeout was treated as a lost lease.

This happens most after a laptop wakes: the first calls time out, the one renewal in that window fails, and nothing renews the lease again. Within 5 minutes reclaimStaleTasks sees a lapsed lease for a run that is still alive and fires the trigger a second time. In one case two runs of a daily trigger started 32 minutes apart, and the second messaged the first about the duplicate. The log line appeared 58 times in 10 days on one machine.

Fix

  • renewTaskLease returns true (renewed), false (the backend answered that this daemon no longer holds the lease) or null (no answer: timeout, offline, no token).
  • The heartbeat stops the monitor only on false. On null it logs a warning and tries again on the next beat.
  • The timer body moves into heartbeat(taskId) (first commit, no behaviour change) so a test can drive one beat.

Trade-off

A run whose backend never comes back keeps beating until its max runtime, which already ends it. A run whose lease really was lost still stops at the next beat that gets an answer.

Alternative (not taken)

Make the reclaim sweep wait longer before re-firing. That delays real recoveries (a daemon that died mid-run) and leaves the dropped monitor in place.

Tests

Four new tests, written first and failing before the fix:

  • renewTaskLease answers null, not false, when the mutation throws.
  • It passes the backend's true and false through.
  • The scheduler keeps the run and renews on the next beat after a null.
  • The scheduler still stops on false.

They pass, as do taskScheduler.test.ts, syncService.timeout.test.ts and daemon.trigger-run.test.ts; cast check cli shows 0 errors.

Full CLI suite (bun test --parallel, 1071 files) on a bare Linux machine with bun 1.4.4:

  • main: 13,811 pass, 133 skip, 22 fail.
  • this branch: 13,814 pass, 133 skip, 23 fail.

The same 22 fail on both: they need real claude/codex clients or tmux behaviour that machine lacks. The extra failure is short-message delivery into Claude > hi submits exactly once from a composer that is empty, which is flaky: three reruns on each of main and the branch gave mixed results on both.

…the run's monitor

renewTaskLease turned every exception into false, and the heartbeat read false
as "the lease is gone": it cleared the run's timer for good. After a laptop
wake the first calls time out, so one failed beat left a live run with no
renewals. The 5 minute lease lapsed and reclaimStaleTasks fired the trigger a
second time while the first run was still working.

renewTaskLease now answers null when it got no answer. Only false, the
backend's own "you no longer hold it", stops the monitor; null logs and tries
again on the next beat.
…or the lease fix

- The heartbeat tests move to taskScheduler.heartbeat.test.ts: adding them to
  taskScheduler.test.ts took it over 800 lines (850), which the max-lines
  ratchet refuses for a new oversized file.
- index.ts's static graph sat at 4851 KB, the pin, and the lease fix's comment
  in syncService.ts pushed it to 4852. The explanation now sits on the
  scheduler's heartbeat, which is off that graph.
- Re-stamp the daemon build id: the lease fix changes the daemon closure.
@ashot
ashot merged commit a91b400 into codecast-sh:main Nov 23, 2026
15 of 16 checks passed
@ashot

ashot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Merged as a91b400, thanks. A clear write-up and tests that pin both outcomes. It ships with the next CLI release.

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.

2 participants