From bd8bcfdcbfb78d88d40dbe76434cb4f815b8f04d Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:13:56 +0100 Subject: [PATCH] usage refresher: clear the failure streak on the healthy skip (#322) A healthy OpenCode instance is skipped indefinitely, so a failure streak from before it recovered stayed: the next outage was not logged and its retry waited the long backoff. The healthy skip now ends the streak. The host lock is now read before each refresh instead of once per sweep, so a sweep running when the host locks stops there. Co-Authored-By: Claude Opus 5.5 --- .../src/usage/HeadlessUsageRefresher.test.ts | 66 +++++++++++++++++++ .../src/usage/HeadlessUsageRefresher.ts | 27 +++++--- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/apps/server/src/usage/HeadlessUsageRefresher.test.ts b/apps/server/src/usage/HeadlessUsageRefresher.test.ts index 62aff7a1e91eb..ad9f182794e6a 100644 --- a/apps/server/src/usage/HeadlessUsageRefresher.test.ts +++ b/apps/server/src/usage/HeadlessUsageRefresher.test.ts @@ -357,6 +357,50 @@ describe("HeadlessUsageRefresher", () => { }), ); + it.effect("a healthy skip ends a failure streak: the next outage is logged, short retry", () => + Effect.gen(function* () { + yield* startAt(NOW_MS); + const opencode = { driver: ProviderDriverKind.make("opencode") }; + let healthy = false; + const fake = fakeTarget(provider(NOW_MS - HOUR, opencode), { + checksOnInterval: () => !healthy, + }); + fake.setOutcome("noReading"); + const log = logCollector(); + const { sweep } = yield* make(deps([fake.target])); + const tick = (ms: number) => + TestClock.adjust(ms).pipe(Effect.andThen(sweep), Effect.provide(log.layer)); + // Three failures in a row: one warning. + yield* tick(0); + yield* tick(2 * INTERVAL); + yield* tick(4 * INTERVAL); + assert.equal(fake.calls(), 3); + assert.equal(log.warnings.length, 1); + // A client recovers usage; the instance is healthy, so it is skipped for days. + healthy = true; + const recoveredAt = NOW_MS + 6 * INTERVAL + MINUTE; + yield* TestClock.setTime(recoveredAt); + fake.setSnapshot(provider(recoveredAt, opencode)); + yield* tick(0); + assert.equal(fake.calls(), 3); + assert.deepEqual(log.infos, ["headless usage refresh works again"]); + yield* tick(3 * 24 * HOUR); + assert.equal(fake.calls(), 3); + // A new outage: retried at once, logged as a new streak, retried after 2 intervals. + healthy = false; + yield* tick(0); + assert.equal(fake.calls(), 4); + assert.equal(log.warnings.length, 2); + yield* tick(2 * INTERVAL); + assert.equal(fake.calls(), 5); + // Skipped again while healthy: the logged end is not repeated. + healthy = true; + yield* tick(0); + yield* tick(0); + assert.equal(log.infos.length, 2); + }), + ); + it.effect( "leaves the refresh to upstream while a client lease holds, and pauses on a constrained host", () => @@ -383,6 +427,28 @@ describe("HeadlessUsageRefresher", () => { }), ); + it.effect("a host that locks during a sweep stops the rest of that sweep", () => + Effect.gen(function* () { + yield* startAt(NOW_MS); + const first = fakeTarget(provider(NOW_MS - HOUR)); + const second = fakeTarget(provider(NOW_MS - HOUR, {}, SECOND)); + // The host locks while the first refresh runs, and unlocks later. + let locked = () => first.calls() > 0; + const { sweep } = yield* make( + deps([first.target, second.target], { + hostConstrained: () => Effect.sync(() => locked()), + }), + ); + yield* sweep; + assert.equal(first.calls(), 1); + assert.equal(second.calls(), 0); + locked = () => false; + yield* sweep; + assert.equal(first.calls(), 1); + assert.equal(second.calls(), 1); + }), + ); + it.effect("off switch: interval 0 refreshes nothing", () => Effect.gen(function* () { yield* startAt(NOW_MS); diff --git a/apps/server/src/usage/HeadlessUsageRefresher.ts b/apps/server/src/usage/HeadlessUsageRefresher.ts index 9e9aa6846837c..8f0e8449045d1 100644 --- a/apps/server/src/usage/HeadlessUsageRefresher.ts +++ b/apps/server/src/usage/HeadlessUsageRefresher.ts @@ -115,6 +115,18 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re const failing = new Map(); const permit = yield* Semaphore.make(1); + const endStreak = Effect.fn("HeadlessUsageRefresher.endStreak")(function* ( + instanceId: ProviderInstanceId, + ) { + const previous = failing.get(instanceId); + if (previous === undefined) return; + failing.delete(instanceId); + yield* Effect.logInfo("headless usage refresh works again", { + instanceId, + failures: previous.failures, + }); + }); + const refreshOne = Effect.fn("HeadlessUsageRefresher.refreshOne")(function* ( target: RefreshTarget, refreshUsage: NonNullable, @@ -131,13 +143,7 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re const ok = Exit.isSuccess(exit) && !timedOut && gotNewReading(before, after); const previous = failing.get(target.instanceId); if (ok) { - if (previous !== undefined) { - failing.delete(target.instanceId); - yield* Effect.logInfo("headless usage refresh works again", { - instanceId: target.instanceId, - failures: previous.failures, - }); - } + yield* endStreak(target.instanceId); return; } const failures = (previous?.failures ?? 0) + 1; @@ -162,7 +168,6 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re if (Option.isNone(settings)) return; const intervalMs = effectiveIntervalMs(settings.value); if (intervalMs === null) return; - if (yield* deps.hostConstrained(settings.value)) return; const targets = yield* deps.listTargets; const live = new Set(targets.map((target) => target.instanceId)); for (const id of failing.keys()) if (!live.has(id)) failing.delete(id); @@ -173,8 +178,10 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re continue; } // Upstream's loop skips some healthy instances: a check of a healthy OpenCode one - // starts an OpenCode server. Skip them too. + // starts an OpenCode server. Skip them too; healthy also ends a failure streak, so the + // next outage is logged and retried as a new one. if (target.snapshot.checksOnInterval && !(yield* target.snapshot.checksOnInterval)) { + yield* endStreak(target.instanceId); continue; } const before = yield* target.snapshot.getSnapshot; @@ -190,6 +197,8 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re ) { continue; } + // Read per refresh: the host may lock while an earlier refresh of this sweep runs. + if (yield* deps.hostConstrained(settings.value)) return; if (yield* deps.clientRefreshes(target.instanceId)) continue; yield* refreshOne(target, refreshUsage, before, intervalMs, nowMs); }