( db: WorkerDb, owner: SecurityReviewOwner )
| 275 | |
| 276 | function toSyncRunProgress( |
| 277 | runId: string, |
| 278 | result: SyncResult, |
| 279 | completedRepos: string[], |
| 280 | chunkIndex: number, |
| 281 | noProgressChunks: number |
| 282 | ): SyncRunProgressWrite { |
| 283 | return { |
| 284 | runId, |
| 285 | completedRepos, |
| 286 | staleRepos: result.staleRepos, |
| 287 | authInvalidRepos: result.authInvalidRepos, |
| 288 | synced: result.synced, |
| 289 | errors: result.errors, |
| 290 | skipped: result.skipped, |
| 291 | authInvalid: result.authInvalid, |
| 292 | reauthRequired: result.reauthRequired, |
| 293 | noProgressChunks, |
| 294 | chunkIndex, |
| 295 | }; |
| 296 | } |
| 297 | |
| 298 | function readLastCompletedRunId(runtimeState: Record<string, unknown>): string | null { |
| 299 | const value = runtimeState.last_completed_run_id; |
| 300 | return typeof value === 'string' ? value : null; |
| 301 | } |
| 302 | |
| 303 | function readSyncRunRunId(runtimeState: Record<string, unknown>): string | null { |
| 304 | const value = runtimeState.sync_run; |
| 305 | if ( |
| 306 | value && |
| 307 | typeof value === 'object' && |
| 308 | typeof (value as { runId?: unknown }).runId === 'string' |
| 309 | ) { |
| 310 | return (value as { runId: string }).runId; |
| 311 | } |
| 312 | return null; |
| 313 | } |
| 314 | |
| 315 | type SyncLeaseReadOutcome = |
| 316 | | { kind: 'completed' } |
| 317 | | { kind: 'stale_chunk'; holderRunId: string | null; holderChunkIndex: number | null } |
| 318 | | { kind: 'superseded'; holderRunId: string | null; holderChunkIndex: number | null }; |
| 319 | |
| 320 | /** |
| 321 | * Classifies a runtime_state read that did not acquire the lease. `completed` |
| 322 | * means this run already finished. `stale_chunk` means a newer chunk of this |
| 323 | * same run holds the lease, or no different run id is visible at all (the lease |
| 324 | * vanished in a millisecond race). `superseded` means a different run id is |
| 325 | * actually visible on the lease or the cursor. |
| 326 | */ |
| 327 | function classifySyncLeaseRead( |
| 328 | runtimeState: Record<string, unknown>, |
| 329 | runId: string, |
| 330 | chunkIndex: number |
| 331 | ): SyncLeaseReadOutcome { |
| 332 | if (readLastCompletedRunId(runtimeState) === runId) return { kind: 'completed' }; |
| 333 | const parsedLease = SyncLeaseSchema.safeParse(runtimeState.sync_lease); |
| 334 | const lease = parsedLease.success ? parsedLease.data : null; |
no test coverage detected