|
1 | 1 | import { assert, describe, it } from "@effect/vitest"; |
2 | 2 | import { |
3 | 3 | AuthSessionId, |
| 4 | + type BackgroundScope, |
4 | 5 | DEFAULT_SERVER_SETTINGS, |
5 | 6 | defaultInstanceIdForDriver, |
6 | 7 | type HostPowerSnapshot, |
@@ -86,18 +87,22 @@ const fakeTarget = ( |
86 | 87 | initial: ServerProvider, |
87 | 88 | options?: { |
88 | 89 | readonly awaitingFirstCheck?: () => boolean; |
| 90 | + readonly checksOnInterval?: () => boolean; |
89 | 91 | readonly enabled?: boolean; |
90 | 92 | readonly noRefreshUsage?: boolean; |
91 | 93 | }, |
92 | 94 | ) => { |
93 | 95 | let snapshot = initial; |
94 | 96 | let calls = 0; |
| 97 | + let interrupts = 0; |
95 | 98 | let outcome: Outcome = "ok"; |
96 | 99 | let gate: Deferred.Deferred<void> | undefined; |
97 | 100 | const refreshUsage = Effect.gen(function* () { |
98 | 101 | calls += 1; |
99 | 102 | if (gate !== undefined) yield* Deferred.await(gate); |
100 | | - if (outcome === "hang") return yield* Effect.never; |
| 103 | + if (outcome === "hang") { |
| 104 | + return yield* Effect.never.pipe(Effect.onInterrupt(() => Effect.sync(() => interrupts++))); |
| 105 | + } |
101 | 106 | if (outcome === "error") { |
102 | 107 | return yield* new ServerSettingsError({ |
103 | 108 | settingsPath: "settings.json", |
@@ -131,12 +136,16 @@ const fakeTarget = ( |
131 | 136 | snapshot: { |
132 | 137 | getSnapshot: Effect.sync(() => snapshot), |
133 | 138 | isAwaitingFirstCheck: Effect.sync(() => options?.awaitingFirstCheck?.() ?? false), |
| 139 | + ...(options?.checksOnInterval |
| 140 | + ? { checksOnInterval: Effect.sync(options.checksOnInterval) } |
| 141 | + : {}), |
134 | 142 | ...(options?.noRefreshUsage ? {} : { refreshUsage }), |
135 | 143 | }, |
136 | 144 | }; |
137 | 145 | return { |
138 | 146 | target, |
139 | 147 | calls: () => calls, |
| 148 | + interrupts: () => interrupts, |
140 | 149 | setOutcome: (next: Outcome) => { |
141 | 150 | outcome = next; |
142 | 151 | }, |
@@ -328,6 +337,26 @@ describe("HeadlessUsageRefresher", () => { |
328 | 337 | }), |
329 | 338 | ); |
330 | 339 |
|
| 340 | + it.effect("skips an instance upstream's own loop skips, such as a healthy OpenCode", () => |
| 341 | + Effect.gen(function* () { |
| 342 | + yield* startAt(NOW_MS); |
| 343 | + const old = NOW_MS - HOUR; |
| 344 | + const opencode = { driver: ProviderDriverKind.make("opencode") }; |
| 345 | + let healthy = true; |
| 346 | + // A check of a healthy OpenCode instance starts an OpenCode server; upstream skips it. |
| 347 | + const skipped = fakeTarget(provider(old, opencode), { checksOnInterval: () => !healthy }); |
| 348 | + const otherDriver = fakeTarget(provider(old, {}, SECOND)); |
| 349 | + const { sweep } = yield* make(deps([skipped.target, otherDriver.target])); |
| 350 | + yield* sweep; |
| 351 | + assert.equal(skipped.calls(), 0); |
| 352 | + assert.equal(otherDriver.calls(), 1); |
| 353 | + // Unhealthy (for example its usage probe failed): upstream checks it, and so does this. |
| 354 | + healthy = false; |
| 355 | + yield* sweep; |
| 356 | + assert.equal(skipped.calls(), 1); |
| 357 | + }), |
| 358 | + ); |
| 359 | + |
331 | 360 | it.effect( |
332 | 361 | "leaves the refresh to upstream while a client lease holds, and pauses on a constrained host", |
333 | 362 | () => |
@@ -438,6 +467,7 @@ describe("HeadlessUsageRefresher", () => { |
438 | 467 | yield* TestClock.adjust(REFRESH_TIMEOUT_MS); |
439 | 468 | yield* Fiber.join(fiber); |
440 | 469 | assert.equal(fake.calls(), 1); |
| 470 | + assert.equal(fake.interrupts(), 1); |
441 | 471 | assert.equal(log.warnings.length, 1); |
442 | 472 | // The next pass is not stuck behind the hung one; it waits out the backoff. |
443 | 473 | fake.setOutcome("ok"); |
@@ -529,69 +559,112 @@ describe("HeadlessUsageRefresher", () => { |
529 | 559 | "the server layer refreshes through the Scheduler only while no client lease holds", |
530 | 560 | () => |
531 | 561 | Effect.gen(function* () { |
532 | | - yield* startAt(NOW_MS); |
533 | | - const fake = fakeTarget(provider(NOW_MS - HOUR)); |
534 | | - const refreshed = yield* Queue.unbounded<void>(); |
535 | | - const target = fake.target; |
536 | | - const instance = { |
537 | | - ...target, |
538 | | - snapshot: { |
539 | | - ...target.snapshot, |
540 | | - refreshUsage: target.snapshot.refreshUsage!.pipe( |
541 | | - Effect.andThen(Queue.offer(refreshed, undefined)), |
542 | | - ), |
543 | | - }, |
544 | | - } as unknown as ProviderInstance; |
545 | | - const registry = Layer.succeed( |
546 | | - ProviderInstanceRegistry.ProviderInstanceRegistry, |
547 | | - ProviderInstanceRegistry.ProviderInstanceRegistry.of({ |
548 | | - getInstance: () => Effect.succeed(instance), |
549 | | - listInstances: Effect.succeed([instance]), |
550 | | - listUnavailable: Effect.succeed([]), |
551 | | - streamChanges: Stream.empty, |
552 | | - subscribeChanges: Effect.die("unused"), |
553 | | - }), |
554 | | - ); |
555 | | - const policyLayer = BackgroundPolicy.layer.pipe( |
556 | | - Layer.provide(Layer.merge(hostPowerLayer, ServerSettingsModule.layerTest())), |
557 | | - ); |
558 | | - yield* Effect.gen(function* () { |
559 | | - const policy = yield* BackgroundPolicy.BackgroundPolicy; |
560 | | - // A focused client watches provider status: upstream's loop refreshes, not this job. |
561 | | - yield* policy.reportClientActivity(AuthSessionId.make("s"), RpcClientId.make(1), { |
562 | | - clientId: "c", |
563 | | - clientKind: "web", |
564 | | - visible: true, |
565 | | - focused: true, |
566 | | - recentlyInteracted: true, |
567 | | - scopes: [{ type: "provider-status" }], |
568 | | - ttlMs: 60_000, |
569 | | - observedAt: DateTime.makeUnsafe(NOW_MS), |
570 | | - }); |
571 | | - yield* Effect.scoped( |
572 | | - Layer.build( |
573 | | - HeadlessUsageRefresher.layer.pipe( |
574 | | - Layer.provide(Layer.mergeAll(registry, ServerSettingsModule.layerTest())), |
575 | | - ), |
576 | | - ).pipe( |
577 | | - Effect.andThen( |
578 | | - Effect.gen(function* () { |
579 | | - yield* TestClock.adjust("10 seconds"); |
580 | | - assert.equal(fake.calls(), 0); |
581 | | - // The client goes away: its lease expires and the job takes over. |
582 | | - yield* policy.removeRpcClient(AuthSessionId.make("s"), RpcClientId.make(1)); |
583 | | - yield* TestClock.adjust("5 seconds"); |
584 | | - yield* Queue.take(refreshed); |
585 | | - assert.equal(fake.calls(), 1); |
586 | | - }), |
587 | | - ), |
588 | | - ), |
| 562 | + // A client may watch all providers or one instance; either way upstream's loop refreshes. |
| 563 | + const scopes: ReadonlyArray<BackgroundScope> = [ |
| 564 | + { type: "provider-status" }, |
| 565 | + { type: "provider-status", instanceId: INSTANCE }, |
| 566 | + ]; |
| 567 | + for (const scope of scopes) { |
| 568 | + yield* withServerLayer(({ fake, policy, refreshed }) => |
| 569 | + Effect.gen(function* () { |
| 570 | + // A focused client watches provider status: upstream's loop refreshes, not this job. |
| 571 | + yield* policy.reportClientActivity(AuthSessionId.make("s"), RpcClientId.make(1), { |
| 572 | + clientId: "c", |
| 573 | + clientKind: "web", |
| 574 | + visible: true, |
| 575 | + focused: true, |
| 576 | + recentlyInteracted: true, |
| 577 | + scopes: [scope], |
| 578 | + ttlMs: 60_000, |
| 579 | + observedAt: DateTime.makeUnsafe(NOW_MS), |
| 580 | + }); |
| 581 | + yield* TestClock.adjust("10 seconds"); |
| 582 | + assert.equal(fake.calls(), 0, JSON.stringify(scope)); |
| 583 | + // The client goes away: its lease expires and the job takes over. |
| 584 | + yield* policy.removeRpcClient(AuthSessionId.make("s"), RpcClientId.make(1)); |
| 585 | + yield* TestClock.adjust("5 seconds"); |
| 586 | + yield* Queue.take(refreshed); |
| 587 | + assert.equal(fake.calls(), 1); |
| 588 | + }), |
589 | 589 | ); |
590 | | - }).pipe(Effect.provide(policyLayer)); |
| 590 | + } |
591 | 591 | }), |
592 | 592 | ); |
| 593 | + |
| 594 | + it.effect("the server layer pauses while the host is locked or in low power", () => |
| 595 | + Effect.gen(function* () { |
| 596 | + const constrained: ReadonlyArray<Partial<HostPowerSnapshot>> = [ |
| 597 | + // Default (balanced) settings pause on these; battery pauses only in battery-saver. |
| 598 | + { locked: "true" }, |
| 599 | + { lowPowerMode: "true" }, |
| 600 | + ]; |
| 601 | + for (const power of constrained) { |
| 602 | + let hostPower: HostPowerSnapshot = { ...nominalHostPower, stale: false, ...power }; |
| 603 | + yield* withServerLayer( |
| 604 | + ({ fake, refreshed }) => |
| 605 | + Effect.gen(function* () { |
| 606 | + yield* TestClock.adjust("10 seconds"); |
| 607 | + assert.equal(fake.calls(), 0, JSON.stringify(power)); |
| 608 | + hostPower = { ...nominalHostPower, stale: false }; |
| 609 | + yield* TestClock.adjust("5 seconds"); |
| 610 | + yield* Queue.take(refreshed); |
| 611 | + assert.equal(fake.calls(), 1); |
| 612 | + }), |
| 613 | + Effect.sync(() => hostPower), |
| 614 | + ); |
| 615 | + } |
| 616 | + }), |
| 617 | + ); |
593 | 618 | }); |
594 | 619 |
|
| 620 | +/** Runs `body` with the refresher's server layer over one stale signed-in instance. */ |
| 621 | +const withServerLayer = <A, E>( |
| 622 | + body: (ctx: { |
| 623 | + readonly fake: ReturnType<typeof fakeTarget>; |
| 624 | + readonly policy: BackgroundPolicy.BackgroundPolicy["Service"]; |
| 625 | + readonly refreshed: Queue.Queue<void>; |
| 626 | + }) => Effect.Effect<A, E>, |
| 627 | + hostPower: Effect.Effect<HostPowerSnapshot> = Effect.succeed(nominalHostPower), |
| 628 | +) => |
| 629 | + Effect.gen(function* () { |
| 630 | + yield* startAt(NOW_MS); |
| 631 | + const fake = fakeTarget(provider(NOW_MS - HOUR)); |
| 632 | + const refreshed = yield* Queue.unbounded<void>(); |
| 633 | + const target = fake.target; |
| 634 | + const instance = { |
| 635 | + ...target, |
| 636 | + snapshot: { |
| 637 | + ...target.snapshot, |
| 638 | + refreshUsage: target.snapshot.refreshUsage!.pipe( |
| 639 | + Effect.andThen(Queue.offer(refreshed, undefined)), |
| 640 | + ), |
| 641 | + }, |
| 642 | + } as unknown as ProviderInstance; |
| 643 | + const registry = Layer.succeed( |
| 644 | + ProviderInstanceRegistry.ProviderInstanceRegistry, |
| 645 | + ProviderInstanceRegistry.ProviderInstanceRegistry.of({ |
| 646 | + getInstance: () => Effect.succeed(instance), |
| 647 | + listInstances: Effect.succeed([instance]), |
| 648 | + listUnavailable: Effect.succeed([]), |
| 649 | + streamChanges: Stream.empty, |
| 650 | + subscribeChanges: Effect.die("unused"), |
| 651 | + }), |
| 652 | + ); |
| 653 | + const policyLayer = BackgroundPolicy.layer.pipe( |
| 654 | + Layer.provide(Layer.merge(hostPowerLayer(hostPower), ServerSettingsModule.layerTest())), |
| 655 | + ); |
| 656 | + return yield* Effect.gen(function* () { |
| 657 | + const policy = yield* BackgroundPolicy.BackgroundPolicy; |
| 658 | + return yield* Effect.scoped( |
| 659 | + Layer.build( |
| 660 | + HeadlessUsageRefresher.layer.pipe( |
| 661 | + Layer.provide(Layer.mergeAll(registry, ServerSettingsModule.layerTest())), |
| 662 | + ), |
| 663 | + ).pipe(Effect.andThen(body({ fake, policy, refreshed }))), |
| 664 | + ); |
| 665 | + }).pipe(Effect.provide(policyLayer)); |
| 666 | + }); |
| 667 | + |
595 | 668 | const nominalHostPower: HostPowerSnapshot = { |
596 | 669 | source: "unknown", |
597 | 670 | idle: "unknown", |
@@ -605,14 +678,15 @@ const nominalHostPower: HostPowerSnapshot = { |
605 | 678 | updatedAt: DateTime.makeUnsafe(NOW_MS), |
606 | 679 | }; |
607 | 680 |
|
608 | | -const hostPowerLayer = Layer.effect( |
609 | | - HostPowerMonitor.HostPowerMonitor, |
610 | | - Effect.gen(function* () { |
611 | | - const changes = yield* PubSub.sliding<HostPowerSnapshot>(1); |
612 | | - return HostPowerMonitor.HostPowerMonitor.of({ |
613 | | - snapshot: Effect.succeed(nominalHostPower), |
614 | | - report: () => Effect.void, |
615 | | - streamChanges: Stream.fromPubSub(changes), |
616 | | - }); |
617 | | - }), |
618 | | -); |
| 681 | +const hostPowerLayer = (snapshot: Effect.Effect<HostPowerSnapshot>) => |
| 682 | + Layer.effect( |
| 683 | + HostPowerMonitor.HostPowerMonitor, |
| 684 | + Effect.gen(function* () { |
| 685 | + const changes = yield* PubSub.sliding<HostPowerSnapshot>(1); |
| 686 | + return HostPowerMonitor.HostPowerMonitor.of({ |
| 687 | + snapshot, |
| 688 | + report: () => Effect.void, |
| 689 | + streamChanges: Stream.fromPubSub(changes), |
| 690 | + }); |
| 691 | + }), |
| 692 | + ); |