( bus: ProgressBus, store: ProgressStore, runsDirProvider: () => string = getRunsDir, )
| 188 | * cannot monkey-patch getRunsDir itself). |
| 189 | */ |
| 190 | export function attachRunStatePersistence( |
| 191 | bus: ProgressBus, |
| 192 | store: ProgressStore, |
| 193 | runsDirProvider: () => string = getRunsDir, |
| 194 | ): () => void { |
| 195 | return bus.subscribe(event => { |
| 196 | if (event.type !== 'run_done') return |
| 197 | const run = store.get(event.runId) |
| 198 | if (!run) return |
| 199 | const dir = runsDirProvider() |
| 200 | void writeRunState(dir, run).then(() => { |
| 201 | // Sweep only after the new state lands on disk — avoids a race where the just-finished run |
| 202 | // itself gets pruned because its state.json wasn't counted yet. |
| 203 | void cleanupOldRuns(dir).catch(e => { |
| 204 | logForDebugging( |
| 205 | `[workflow warn] cleanupOldRuns after run_done threw: ${(e as Error).message}`, |
| 206 | ) |
| 207 | }) |
| 208 | }) |
| 209 | }) |
| 210 | } |
no test coverage detected