Skip to content

Commit c30aef6

Browse files
skyhong2002claude
andcommitted
fix(web): keep background activity reporting after an interrupted report
Each per-environment reportClientActivity request was wrapped in Effect.ignore, which swallows failures but not interruption. When an environment's connection is torn down while its report is in flight (for example across a sleep/wake), the interruption ended the report-consuming fiber, so the renderer never reported again. With no leases, provider-status work, including usage limit refreshes, stopped until the window was reloaded. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 4ee6bfd commit c30aef6

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

‎apps/web/src/lib/backgroundActivityReporter.test.ts‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
import { EnvironmentRegistry } from "@t3tools/client-runtime/connection";
12
import { EnvironmentId, WS_METHODS } from "@t3tools/contracts";
2-
import { describe, expect, it } from "@effect/vitest";
3+
import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest";
4+
import * as Cause from "effect/Cause";
35
import * as Effect from "effect/Effect";
6+
import * as Layer from "effect/Layer";
7+
import * as SubscriptionRef from "effect/SubscriptionRef";
8+
import * as TestClock from "effect/testing/TestClock";
9+
import { vi } from "vite-plus/test";
410

511
import {
12+
backgroundActivityReporterLayer,
613
observeBackgroundActivitySubscription,
714
retainedBackgroundScopes,
815
wasRecentlyInteracted,
@@ -61,3 +68,59 @@ describe("wasRecentlyInteracted", () => {
6168
}),
6269
);
6370
});
71+
72+
beforeEach(() => {
73+
const target = new EventTarget();
74+
vi.stubGlobal("window", {
75+
addEventListener: target.addEventListener.bind(target),
76+
removeEventListener: target.removeEventListener.bind(target),
77+
localStorage: { getItem: () => "client", setItem: () => undefined },
78+
});
79+
vi.stubGlobal("document", {
80+
visibilityState: "visible",
81+
hasFocus: () => true,
82+
addEventListener: target.addEventListener.bind(target),
83+
removeEventListener: target.removeEventListener.bind(target),
84+
});
85+
});
86+
87+
afterEach(() => {
88+
vi.unstubAllGlobals();
89+
});
90+
91+
describe("backgroundActivityReporterLayer", () => {
92+
const keepsReportingAfter = (firstReport: Effect.Effect<void, string>) =>
93+
Effect.gen(function* () {
94+
const entries = yield* SubscriptionRef.make(
95+
new Map([[EnvironmentId.make("environment-reporter-test"), {}]]) as never,
96+
);
97+
let calls = 0;
98+
const registry = EnvironmentRegistry.EnvironmentRegistry.of({
99+
entries,
100+
run: () => {
101+
calls += 1;
102+
return calls === 1 ? firstReport : Effect.void;
103+
},
104+
} as never);
105+
106+
yield* Layer.build(
107+
backgroundActivityReporterLayer.pipe(
108+
Layer.provide(Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, registry)),
109+
),
110+
);
111+
yield* TestClock.adjust("1 second");
112+
expect(calls).toBe(1);
113+
114+
yield* TestClock.adjust("60 seconds");
115+
expect(calls).toBeGreaterThan(1);
116+
}).pipe(Effect.scoped);
117+
118+
it.effect("keeps reporting after one report fails", () =>
119+
keepsReportingAfter(Effect.fail("unreachable")),
120+
);
121+
122+
// As when an environment's connection is torn down while its report is in flight.
123+
it.effect("keeps reporting after one report is interrupted", () =>
124+
keepsReportingAfter(Effect.failCause(Cause.interrupt())),
125+
);
126+
});

‎apps/web/src/lib/backgroundActivityReporter.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ export const backgroundActivityReporterLayer = Layer.effectDiscard(
206206
createActivityReport(environmentId, lastInteractionAtMs, observedAtMs),
207207
),
208208
)
209-
.pipe(Effect.ignore),
209+
// An environment torn down mid-request interrupts its report; that must not
210+
// end the reporting loop, or provider-status leases lapse until reload.
211+
.pipe(Effect.ignoreCause),
210212
{ concurrency: "unbounded", discard: true },
211213
);
212214
}).pipe(Effect.withSpan("web.backgroundActivity.report"));

0 commit comments

Comments
 (0)