The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | const RECHECK_MIN_MS = 30 * 60 * 1_000; |
| 2 | const RECHECK_MAX_MS = 24 * 60 * 60 * 1_000; |
| 3 | const RECHECK_IDLE_DIVISOR = 12; |
| 4 | const RECHECKS_PER_SWEEP = 4; |
| 5 | |
| 6 | /** |
| 7 | * Schedules a sweep's branch lookups, so their git work does not grow with the number of idle |
| 8 | * threads. GitManager's caches cannot: they expire in minutes and evict past a few thousand branches. |
| 9 | */ |
| 10 | export function makeNoPullRequestRechecks() { |
| 11 | const reads = new Map<string, { readonly readAtMs: number; readonly found: boolean }>(); |
| 12 | return { |
| 13 | /** |
| 14 | * The branches a scheduled sweep looks up now: every one that last had a pull request, and |
| 15 | * the few longest unread that had none and are due. One idle a day is due every two hours. |
| 16 | */ |
| 17 | select: ( |
| 18 | nowMs: number, |
| 19 | branches: ReadonlyArray<{ readonly key: string; readonly activityAtMs: number }>, |
| 20 | ): ReadonlySet<string> => { |
| 21 | const selected = new Set<string>(); |
| 22 | const due: Array<{ key: string; activityAtMs: number; readAtMs: number }> = []; |
| 23 | for (const branch of branches) { |
| 24 | const read = reads.get(branch.key); |
| 25 | if (read?.found === true) { |
| 26 | selected.add(branch.key); |
| 27 | continue; |
| 28 | } |
| 29 | const delayMs = Math.min( |
| 30 | Math.max((nowMs - branch.activityAtMs) / RECHECK_IDLE_DIVISOR, RECHECK_MIN_MS), |
| 31 | RECHECK_MAX_MS, |
| 32 | ); |
| 33 | if (read !== undefined && nowMs - read.readAtMs < delayMs) continue; |
| 34 | due.push({ ...branch, readAtMs: read?.readAtMs ?? Number.NEGATIVE_INFINITY }); |
| 35 | } |
| 36 | due.sort((left, right) => |
| 37 | left.readAtMs === right.readAtMs |
| 38 | ? right.activityAtMs - left.activityAtMs |
| 39 | : left.readAtMs - right.readAtMs, |
| 40 | ); |
| 41 | // Stamped before the lookup, so one that keeps failing cannot hold a place in every sweep. |
| 42 | for (const { key } of due.slice(0, RECHECKS_PER_SWEEP)) { |
| 43 | selected.add(key); |
| 44 | reads.set(key, { readAtMs: nowMs, found: false }); |
| 45 | } |
| 46 | return selected; |
| 47 | }, |
| 48 | record: (key: string, nowMs: number, found: boolean): void => { |
| 49 | reads.set(key, { readAtMs: nowMs, found }); |
| 50 | }, |
| 51 | retain: (keys: { readonly has: (key: string) => boolean }): void => { |
| 52 | for (const key of reads.keys()) { |
| 53 | if (!keys.has(key)) reads.delete(key); |
| 54 | } |
| 55 | }, |
| 56 | }; |
| 57 | } |