Model-check the Downy download queue (class Manager in app/src/main/manager.js) with TLA+ and TLC. Use for any change to Manager status handling, schedule(), start(), run(), shutdown(), cleanup(), the cookies map, pause, resume, cancel, remove or clearFinished, or batches (startBatch, commitBatch, dismissBatch), for the download schedule (job.onSchedule, releaseScheduled, parkScheduled, pendingScheduled, and what tick() in app/src/main/scheduler.js does with them), and for changes to the sche...
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Verify Job Lifecycle?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/konik-yt-verify-job-lifecycle)
---
name: verify-job-lifecycle
description: Model-check the Downy download queue (class Manager in app/src/main/manager.js) with TLA+ and TLC. Use for any change to Manager status handling, schedule(), start(), run(), shutdown(), cleanup(), the cookies map, pause, resume, cancel, remove or clearFinished, or batches (startBatch, commitBatch, dismissBatch), for the download schedule (job.onSchedule, releaseScheduled, parkScheduled, pendingScheduled, and what tick() in app/src/main/scheduler.js does with them), and for changes to the schedule() and shutdown() calls in app/src/main/main.js.
---
# Verify the download job lifecycle
`formal/JobLifecycle.tla` is a TLA+ model of the download queue that `Manager` runs in `app/src/main/manager.js`. TLC checks every order in which user calls, finished downloads, the download schedule's timers and quitting can interleave for 3 downloads, and it checks the properties below in each reachable state. Tests check the orders someone wrote down. This check covers the races between an await in `start()`, `run()` or `shutdown()` and a user call or a schedule tick, which tests rarely reach.
## Does the change touch the model?
Compare the change with the coverage record below. It touches the model when it changes any method in the table, adds a place that writes `job.status`, `job.onSchedule`, `job.pausing`, `job.cancelling`, `job.batch`, `this.runtime`, `this.cookies` or `this.stopping`, adds or moves an `await` inside one of those methods, adds a caller of `schedule()`, `start()` or `shutdown()`, or changes when `tick()` in `app/src/main/scheduler.js` calls `releaseScheduled()` or `parkScheduled()`.
To see what changed in the covered code since the model was last confirmed:
```bash
git diff cdf49de -- app/src/main/manager.js app/src/main/main.js app/src/main/segmented.js app/src/main/scheduler.js
git diff cdf49de -- app/src/renderer/app.js | grep -n -i -E "dismiss|b\.state"
```
Changes to formats, file names, progress, `tick()`, folders or yt-dlp arguments do not touch the model.
## Run the check
```bash
bash formal/check.sh
```
It needs Java 11 or later (CI uses Temurin 21) and curl. The first run downloads `tla2tools.jar` 1.7.4 to `~/.cache/downy-tla` and checks its SHA-256. A run takes about 40 seconds on 24 cores. The `model` job in `.github/workflows/check.yml` runs the same script.
Each config prints one line. All of them must say `pass`.
- `faithful-max1.cfg` and `faithful-max2.cfg` are the model as the code is, with "Downloads at the same time" at 1 and at 2. They pass when TLC finds no error.
- Each `bug-*.cfg` turns on one `BUG_*` switch, which puts a past or likely mistake back into the model. It passes only when TLC reports the invariant on its `INVARIANT` line as violated. This shows that the check can still fail.
To read a counterexample, run one config. The script then prints everything TLC says:
```bash
bash formal/check.sh faithful-max1.cfg
```
## Properties
Each property is an invariant at the end of `formal/JobLifecycle.tla`, with the same sentence in a comment above it.
| Invariant | Promise | Harm if it breaks | Fails in |
| --- | --- | --- | --- |
| `NoStartAfterStop` | Once `shutdown()` has begun, no job enters downloading. | A waiting download starts while Downy quits, is cut off, and comes back paused instead of queued. | `bug-no-stopping-guard.cfg`, the bug that c6868a3 fixed |
| `ActiveWithinLimit` | No more files are ACTIVE at once than maxActive. | Downy runs more downloads at once than the user set. | `bug-no-active-limit.cfg` |
| `OneAtATime` | A one-at-a-time batch never has two files ACTIVE at once. | A batch the user asked to run one by one downloads files side by side. | `bug-no-busy-check.cfg` |
| `NoCookieAfterEnd` | A job that is done, cancelled, removed or cleared holds no browser cookie. | Downy keeps the cookie of the browser session after the download ended. | `bug-keep-cookie.cfg`, the bug that 29d0a6d and 9f23943 fixed |
| `NoPartLeftBehind` | A job that is done, cancelled, removed or cleared leaves no part file. | Unused part files pile up in Downy's data folder. | `bug-remove-skips-finished.cfg`, the bug that 3ee9309 fixed |
| `FailedKeepsPart` | A queued, scheduled, running, paused or failed job keeps its part file. | A retry, or the next start of a scheduled download, starts it again from zero. | `bug-cleanup-on-error.cfg` |
| `RunningStaysListed` | A job with a runtime entry stays in the list until its run ended. | A removed download keeps running with no row to stop it. | `bug-remove-running.cfg` |
| `NoIdleSlot` | While Downy waits for the next event and is not quitting, no waiting file could take a free slot. | A download waits in the queue with a slot free. | `bug-no-schedule-on-resume.cfg` |
| `ScheduledWaits` | A job with the status "scheduled" has no run in progress. | A download that should wait for the schedule's next start is still using the network. | `bug-park-sets-scheduled.cfg` |
| `ClosedWindowHolds` | While the schedule's window is closed, no job on the schedule waits in line, and each one that downloads is already being stopped. | Downloads of the schedule start or go on after the stop time. | `bug-park-skips-running.cfg`, `bug-queue-takes-scheduled.cfg` |
| `OnScheduleShape` | A job on the schedule is scheduled, waiting in line or downloading. | A paused or finished job still counts as on the schedule, so the scheduler never sees its run end and Windows never goes to sleep. | `bug-pause-keeps-schedule.cfg` |
## Coverage record
The sync commit is `cdf49de`. At that commit the model matches the code in the table. Each action in the spec is one synchronous stretch of code, from where a method starts or an await returns to the next await or the end.
| Model | Code |
| --- | --- |
| `Schedule` operator, `Starts`, `Units`, `Slots`, `Fits`, `Busy`, `Waiting` | `schedule()`, `queue()`, `activeCount()`, and the synchronous part of `start()` in manager.js. `queue()` holds only "queued" jobs, which the `Waiting` operator says |
| `CookiesAfter`, `PartsAfter` | `cleanup()` in manager.js |
| `Commit` | `commit()` and `prepare()`, which sets the status "scheduled" and `job.onSchedule` for `custom.schedule` |
| `CommitBatch` | `commitBatch()` with mode "one" and one `custom.schedule` for the whole batch |
| `Pause` | `pause()`, also `pauseAll()` and `pauseBatch()`, which loop over it. It deletes `job.onSchedule` first |
| `Resume` | `resume()`, also `resumeAll()`, `resumeBatch()` and `retryBatch()`. Only `resume()` takes a "scheduled" job ("Start now") and deletes `job.onSchedule` |
| `Cancel` | `cancel()`, also `cancelBatch()`. It deletes `job.onSchedule` first |
| `Remove` | `remove()`, one id per step |
| `ClearFinished` | `clearFinished()` with its three scopes |
| `StartBatch` | `startBatch()` with mode "one" |
| `DismissBatch` | `dismissBatch()`, called only for a batch whose `b.state` is "finished" in `buildBatches()` in app/src/renderer/app.js |
| `MainSchedules` | the `manager.schedule()` calls in app/src/main/main.js |
| `ShutdownBegin` | `shutdown()` up to `await Promise.allSettled(...)`, called from before-quit in main.js |
| `ShutdownEnd` | `shutdown()` after that await, then `app.exit(0)` in before-quit |
| `WritePart` | `downloadStream()` in app/src/main/segmented.js and `runYtdlp()` writing part files |
| `RunEnds` | `run()` after its last await, through its try, catch and finally blocks. A pause ends as "scheduled" when `job.onSchedule` is set, and done and error delete it |
| `RunFinally` | the `.finally` callback that `start()` hangs on `run()` |
| `Releasing`, `OpenWindow`, `Release` | `releaseScheduled()` in manager.js, as `_tick()` in scheduler.js calls it once it has opened a run (`OpenWindow`) and on each later tick of an open run (`Release`). `releaseScheduled()` calls `schedule()` only when it moved a job |
| `CloseWindow` | `parkScheduled(keep)` in manager.js, as `_tick()` calls it at the stop time with `repeat === "daily"` as `keep`. It calls `pause()` on each job on the schedule that is queued or "downloading", and with `keep` it puts `job.onSchedule` back and sets a waiting job to "scheduled". Without `keep` it also pauses "scheduled" jobs |
| `FinishWindow` | `_tick()` in scheduler.js closing the run once none of the jobs it released is still on the schedule, as `pendingScheduled()` tells |
| `UserCanAct` | Node runs every queued promise callback before the next IPC call, HTTP request or timer. The scheduler's `setInterval` and the "update" event that calls `tick()` are timers, so the four window actions wait for it too |
## What the model leaves out
A pass covers the model at its bounds, not all of Downy.
- Bounds are 3 downloads, 3 batch ids, and maxActive 1 or 2 as a fixed setting. The two faithful configs reach 908,047 and 1,887,617 distinct states, and the whole script takes about 40 seconds on 24 cores. Before the schedule joined the model, 4 downloads and 4 batch ids at maxActive 2 also found no error, in 10,203,270 distinct states, in 9 minutes. The schedule makes the state space about 5 times as big, so that run was not repeated. To try it, copy `formal/faithful-max2.cfg` to `formal/faithful-big.cfg`, add `j4` to `Jobs` and `b4` to `BatchIds`, run `bash formal/check.sh faithful-big.cfg`, and delete the copy afterwards.
- Queue order is not modeled. `move()` lets the user put waiting files in any order, so `schedule()` in the model may start any waiting file, and a batch may start any of its waiting files instead of the lowest `batchIndex`. `releaseScheduled()` puts the released files at the end of the line in their old order, and the model leaves that order out too.
- "merging" and "converting" count as "downloading". `pause()` does nothing during them in the code, and the model lets it set `pausing`, which only adds behaviors. `parkScheduled()` also skips them in the code, so a merging file finishes after the stop time, while the model parks it with `pausing`. That removes a state, so `ClosedWindowHolds` is checked for downloading files only. In the code a merging file on the schedule can sit in a closed window without `pausing`, and it ends as done, which is meant.
- The clock, missed starts, the stop time, `run.ids`, the countdown and the power calls live in `scheduler.js`, and its unit tests cover them. The model has only a window that opens, takes part in `releaseScheduled()` and `parkScheduled()`, and closes. `FinishWindow` needs that no job at all is on the schedule. The code also closes a run when none of the files it released is still on the schedule, and at once when its first release finds nothing. By reading `_tick()`, that close finds no job waiting in line, so the states it leaves are ones that a stop time with `keep` also reaches, which `CloseWindow(TRUE)` covers.
- `repeat === "once"` leaves nothing on the schedule after a stop time, and no invariant says so, because it is a promise about one call and not about every state. The tests for `parkScheduled(false)` in app/test/manager.test.js cover it.
- `DismissBatch` stays allowed for a batch with a "scheduled" file, which is more than `buildBatches()` in app/src/renderer/app.js offers once it counts scheduled files as unfinished. All invariants hold either way, because a scheduled file is not waiting in line.
- Batches that run "all" together schedule like files outside a batch, so the model has no separate mode for them.
- Each job has one part file of its own. yt-dlp names temp files after the output name, so two jobs with the same name share them, and the model does not cover that.
- `run()` can throw before its first await when ffmpeg is missing. The model treats that as a run that ends later.
- `load()`, `sweepParts()`, restarts, `save()` and what downloads.json holds are not modeled. That includes `load()` turning a saved queued or running job with `onSchedule` back into "scheduled". The tests for a restart in app/test/manager.test.js cover it.
- A lower maxActive in Settings leaves running downloads alone, which the model does not cover.
## Is it a real bug or a modeling error?
When a faithful config fails, read the trace from `bash formal/check.sh <config>`. Each `State N: <Action line ...>` names the action that led to that state. Translate every step into a call with the coverage record, then ask whether the code can take those steps in that order.
- Check each step against the code it stands for. The guard of the action must match the `if` checks in the method, and the state after the step must match what the method writes.
- Check the order against the event loop. A user call cannot land while a `.finally` callback of `start()` is queued, or between the last such callback and the rest of `shutdown()`. `UserCanAct` encodes this. A run can end at any time.
- If every step is possible in the code, the code breaks the property. Report it with the steps in the code's words, such as "commit() starts A, then shutdown() aborts A, then A's .finally calls schedule(), which starts B". Do not weaken the invariant or the model until it passes, and do not change product code unless the user asks.
- If a step is impossible in the code, the model is wrong. Fix the action to match the code and run the check again.
When a `bug-*.cfg` stops failing, the model no longer reaches the mistake it replays. That usually means an action changed so that the switch has no effect any more. Make the switch put the mistake back again.
## Update the model for an intended change
1. Find the changed method in the coverage record and edit its action in `formal/JobLifecycle.tla` so it does what the new code does. One synchronous stretch of code is one action. A new await splits an action in two.
2. A new method that writes statuses, flags, the runtime map, cookies, part files or batches gets a new action, a row in the coverage record, and a place in `Next`.
3. Run `bash formal/check.sh`. If a faithful config fails, use the section above to tell a real bug from a modeling error.
4. If the change means a promise itself changes, change the invariant and its sentence only when the user agrees, and update the properties table here.
5. Every invariant needs a `bug-*.cfg` that fails on it. For a new property, add a `BUG_*` constant to the spec, set it to FALSE in every config, and add a config that sets it to TRUE and names the new invariant on its `INVARIANT` line.
6. Once the check passes and you have confirmed that every row of the coverage record still describes the code, set the sync commit above to the commit you checked. Move it forward only after that confirmation.