UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

206 lines • 7.22 kB
/** * Bounded probe-back scheduler for tier-2 demoted selectors. * * Owns ONE armed probe plan per session: at most two probes (first at half-hint, * then at the absolute deadline), never two in-flight simultaneously. Arming * while armed silently supersedes the previous plan. cancel() aborts any * in-flight probe and clears pending timers — no further emits after cancel. * * All timers go through the injected `setTimeout`/`clearTimeout` pair so tests * can drive them deterministically with fake timers. */ export class ProbeBackScheduler { constructor(opts) { this._armed = false; this._generation = 0; this._secondProbeAnnounced = false; this._now = opts.now; this._setTimeout = opts.setTimeout ?? ((cb, d) => setTimeout(cb, d)); // boundary narrowing: injected handle type this._clearTimeout = opts.clearTimeout ?? ((h) => clearTimeout(h)); } get active() { return this._armed; } arm(input) { const gen = ++this._generation; // Supersede any existing plan silently. if (this._armed) { this._clearTimers(); this._abortInFlight("superseded"); this._disarm(); } this._armed = true; this._firstTimer = { handle: undefined }; this._deadlineTimer = { handle: undefined }; this._activeProbeIndex = undefined; this._secondProbeAnnounced = false; // Emit scheduled event for probe 1. if (gen !== this._generation) return; input.emit({ type: "retry_probe_scheduled", selector: input.selector, atMs: input.firstAtMs, probeIndex: 1, }); if (gen !== this._generation) return; const firstDelay = Math.max(0, input.firstAtMs - this._now()); if (this._firstTimer) { this._firstTimer.handle = this._setTimeout(() => { void this._runProbe(input, 1, gen); }, firstDelay); } const deadlineDelay = Math.max(0, input.deadlineMs - this._now()); if (gen !== this._generation) return; if (this._deadlineTimer) { this._deadlineTimer.handle = this._setTimeout(() => { this._runDeadlineProbe(input, gen); }, deadlineDelay); } } cancel(reason) { this._generation++; this._clearTimers(); this._abortInFlight(reason); this._disarm(); } _clearTimers() { if (this._firstTimer?.handle !== undefined) { this._clearTimeout(this._firstTimer.handle); this._firstTimer.handle = undefined; } if (this._deadlineTimer?.handle !== undefined) { this._clearTimeout(this._deadlineTimer.handle); this._deadlineTimer.handle = undefined; } } _abortInFlight(_reason) { if (this._abortController) { this._abortController.abort(); this._abortController = undefined; } this._activeProbeIndex = undefined; } _disarm() { this._armed = false; this._firstTimer = undefined; this._deadlineTimer = undefined; this._abortController = undefined; this._activeProbeIndex = undefined; this._secondProbeAnnounced = false; } _finish() { this._clearTimers(); this._disarm(); } _announceSecondProbe(input, generation) { if (generation !== this._generation || this._secondProbeAnnounced) return; this._secondProbeAnnounced = true; input.emit({ type: "retry_probe_scheduled", selector: input.selector, atMs: input.deadlineMs, probeIndex: 2, }); } _runDeadlineProbe(input, generation) { if (generation !== this._generation || !this._armed) return; if (this._deadlineTimer) this._deadlineTimer.handle = undefined; if (this._firstTimer?.handle !== undefined) { this._clearTimeout(this._firstTimer.handle); this._firstTimer.handle = undefined; } if (this._activeProbeIndex === 1) this._abortInFlight("superseded"); this._announceSecondProbe(input, generation); if (generation !== this._generation) return; void this._runProbe(input, 2, generation); } async _runProbe(input, probeIndex, generation) { const gen = generation; if (gen !== this._generation || !this._armed) return; // Clear the timer that just fired so cancel doesn't try to clear a stale handle. if (probeIndex === 1 && this._firstTimer) { this._firstTimer.handle = undefined; } else if (this._deadlineTimer) { this._deadlineTimer.handle = undefined; } // Guard: auth unavailable -> fail immediately, disarm. if (!input.authAvailable()) { if (gen !== this._generation) return; input.emit({ type: "retry_probe_result", selector: input.selector, ok: false, errorMessage: "auth-unavailable", }); if (gen !== this._generation) return; this._finish(); return; } // Set up abort controller for the in-flight probe. if (gen !== this._generation || this._activeProbeIndex !== undefined) return; const abortController = new AbortController(); this._abortController = abortController; this._activeProbeIndex = probeIndex; let success; try { success = await input.runProbe(abortController.signal); } catch { success = false; } if (gen !== this._generation || this._activeProbeIndex !== probeIndex || this._abortController !== abortController) { return; } this._abortController = undefined; this._activeProbeIndex = undefined; if (success) { if (gen !== this._generation) return; input.onCleared(input.selector); if (gen !== this._generation) return; input.emit({ type: "retry_probe_result", selector: input.selector, ok: true, }); if (gen !== this._generation) return; this._finish(); return; } // First probe failed — the already-armed deadline timer owns the final attempt. if (probeIndex === 1) { this._announceSecondProbe(input, gen); return; } // Second probe failed — final result, disarm. if (gen !== this._generation) return; input.emit({ type: "retry_probe_result", selector: input.selector, ok: false, }); if (gen !== this._generation) return; this._finish(); } } //# sourceMappingURL=probe-scheduler.js.map