Skip to content

Commit 5cde06d

Browse files
committed
fix(client-runtime): refresh Git refs when the RPC session changes
- Retry Git failures and cancel pending refreshes when the session is removed
1 parent defb7e6 commit 5cde06d

2 files changed

Lines changed: 136 additions & 28 deletions

File tree

‎packages/client-runtime/src/state/vcs.test.ts‎

Lines changed: 130 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ import {
33
EnvironmentId,
44
AuthSourceControlWriteScope,
55
WS_METHODS,
6+
VcsProcessExitError,
67
type VcsListRefsResult,
78
} from "@t3tools/contracts";
89
import { describe, expect, it } from "@effect/vitest";
10+
import * as Deferred from "effect/Deferred";
911
import * as Effect from "effect/Effect";
1012
import * as Fiber from "effect/Fiber";
1113
import * as Layer from "effect/Layer";
14+
import * as Logger from "effect/Logger";
1215
import * as Option from "effect/Option";
16+
import * as Queue from "effect/Queue";
1317
import * as Ref from "effect/Ref";
1418
import * as Stream from "effect/Stream";
1519
import * as SubscriptionRef from "effect/SubscriptionRef";
20+
import * as TestClock from "effect/testing/TestClock";
1621
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
1722

1823
import {
@@ -402,63 +407,167 @@ describe("cached VCS refs", () => {
402407
),
403408
);
404409

405-
it.effect("cancels an in-flight refresh when the connection generation changes", () =>
410+
it.effect("waits silently for a session while the connection phase is still connected", () =>
406411
Effect.scoped(
407412
Effect.gen(function* () {
413+
const logs: unknown[] = [];
414+
const logger = Logger.make(({ message }) => logs.push(message));
415+
const liveSession = yield* SubscriptionRef.make(Option.none<RpcSession>());
416+
const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
417+
target: TARGET,
418+
state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),
419+
session: liveSession,
420+
prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
421+
connect: Effect.void,
422+
disconnect: Effect.void,
423+
retryNow: Effect.void,
424+
} satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
425+
const refs = yield* Stream.unwrap(
426+
makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
427+
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
428+
Effect.provideService(
429+
Persistence.EnvironmentCacheStore,
430+
cacheWithRefs(Option.some(CACHED_REFS)),
431+
),
432+
),
433+
).pipe(
434+
Stream.take(2),
435+
Stream.runCollect,
436+
Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
437+
Effect.forkChild({ startImmediately: true }),
438+
);
439+
440+
yield* TestClock.adjust("1 minute");
441+
expect(logs).toEqual([]);
442+
443+
const client = {
444+
[WS_METHODS.vcsListRefs]: () => Effect.succeed(LIVE_REFS),
445+
} as unknown as WsRpcProtocolClient;
446+
yield* SubscriptionRef.set(liveSession, Option.some(session(client)));
447+
expect(yield* Fiber.join(refs)).toEqual([CACHED_REFS, LIVE_REFS]);
448+
expect(logs).toEqual([]);
449+
}),
450+
),
451+
);
452+
453+
it.effect("cancels an in-flight refresh and restarts when the RPC session changes", () =>
454+
Effect.scoped(
455+
Effect.gen(function* () {
456+
const started = yield* Deferred.make<void>();
457+
const interrupted = yield* Deferred.make<void>();
408458
const calls = yield* Ref.make(0);
409459
const interruptions = yield* Ref.make(0);
410-
const connectionState = yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE);
411460
const client = {
412461
[WS_METHODS.vcsListRefs]: () =>
413462
Ref.updateAndGet(calls, (count) => count + 1).pipe(
414463
Effect.flatMap((count) =>
415464
count === 1
416-
? Effect.never.pipe(
465+
? Deferred.succeed(started, undefined).pipe(
466+
Effect.andThen(Effect.never),
417467
Effect.onInterrupt(() =>
418-
Ref.update(interruptions, (interruptions) => interruptions + 1),
468+
Ref.update(interruptions, (interruptions) => interruptions + 1).pipe(
469+
Effect.andThen(Deferred.succeed(interrupted, undefined)),
470+
),
419471
),
420472
)
421473
: Effect.succeed(LIVE_REFS),
422474
),
423475
),
424476
} as unknown as WsRpcProtocolClient;
477+
const liveSession = yield* SubscriptionRef.make(Option.some(session(client)));
425478
const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
426479
target: TARGET,
427-
state: connectionState,
428-
session: yield* SubscriptionRef.make(Option.some(session(client))),
480+
state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),
481+
session: liveSession,
429482
prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
430483
connect: Effect.void,
431484
disconnect: Effect.void,
432485
retryNow: Effect.void,
433486
} satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
434487

435-
const result = Stream.unwrap(
488+
const fiber = yield* Stream.unwrap(
436489
makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
437490
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
438491
Effect.provideService(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())),
439492
),
440-
).pipe(Stream.runHead);
441-
const fiber = yield* Effect.forkChild(result);
442-
443-
for (let attempt = 0; attempt < 100 && (yield* Ref.get(calls)) < 1; attempt += 1) {
444-
yield* Effect.yieldNow;
445-
}
446-
yield* SubscriptionRef.set(connectionState, AVAILABLE_CONNECTION_STATE);
447-
for (let attempt = 0; attempt < 100 && (yield* Ref.get(interruptions)) < 1; attempt += 1) {
448-
yield* Effect.yieldNow;
449-
}
493+
).pipe(Stream.runHead, Effect.forkChild);
494+
495+
yield* Deferred.await(started);
496+
yield* SubscriptionRef.set(liveSession, Option.none());
497+
yield* Deferred.await(interrupted);
450498
expect(yield* Ref.get(interruptions)).toBe(1);
451499

452-
yield* SubscriptionRef.set(connectionState, {
453-
...CONNECTED_CONNECTION_STATE,
454-
generation: 2,
455-
});
500+
yield* SubscriptionRef.set(liveSession, Option.some(session(client)));
456501
expect(Option.getOrThrow(yield* Fiber.join(fiber))).toEqual(LIVE_REFS);
457502
expect(yield* Ref.get(calls)).toBe(2);
458503
}),
459504
),
460505
);
461506

507+
it.effect("retries Git failures and cancels pending retries when the session is removed", () =>
508+
Effect.scoped(
509+
Effect.gen(function* () {
510+
const calls = yield* Ref.make(0);
511+
const warnings = yield* Queue.unbounded<unknown>();
512+
const logger = Logger.make(({ message }) => Queue.offerUnsafe(warnings, message));
513+
const failure = new VcsProcessExitError({
514+
operation: "listRefs",
515+
command: "git",
516+
cwd: "/repo",
517+
exitCode: 128,
518+
detail: "Could not read refs.",
519+
});
520+
const client = {
521+
[WS_METHODS.vcsListRefs]: () =>
522+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
523+
Effect.flatMap((count) =>
524+
count < 3 ? Effect.fail(failure) : Effect.succeed(LIVE_REFS),
525+
),
526+
),
527+
} as unknown as WsRpcProtocolClient;
528+
const liveSession = yield* SubscriptionRef.make(Option.some(session(client)));
529+
const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
530+
target: TARGET,
531+
state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),
532+
session: liveSession,
533+
prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
534+
connect: Effect.void,
535+
disconnect: Effect.void,
536+
retryNow: Effect.void,
537+
} satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
538+
const refs = yield* Stream.unwrap(
539+
makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
540+
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
541+
Effect.provideService(
542+
Persistence.EnvironmentCacheStore,
543+
cacheWithRefs(Option.some(CACHED_REFS)),
544+
),
545+
),
546+
).pipe(
547+
Stream.take(2),
548+
Stream.runCollect,
549+
Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
550+
Effect.forkChild({ startImmediately: true }),
551+
);
552+
553+
yield* Queue.take(warnings);
554+
expect(yield* Ref.get(calls)).toBe(1);
555+
yield* TestClock.adjust("1 second");
556+
expect(yield* Ref.get(calls)).toBe(2);
557+
expect(yield* Queue.size(warnings)).toBe(1);
558+
559+
yield* SubscriptionRef.set(liveSession, Option.none());
560+
yield* TestClock.adjust("1 minute");
561+
expect(yield* Ref.get(calls)).toBe(2);
562+
expect(yield* Queue.size(warnings)).toBe(1);
563+
564+
yield* SubscriptionRef.set(liveSession, Option.some(session(client)));
565+
expect(yield* Fiber.join(refs)).toEqual([CACHED_REFS, LIVE_REFS]);
566+
expect(yield* Ref.get(calls)).toBe(3);
567+
}),
568+
),
569+
);
570+
462571
it.effect("emits persisted refs before a live refresh", () =>
463572
Effect.scoped(
464573
Effect.gen(function* () {

‎packages/client-runtime/src/state/vcs.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,16 @@ export const makeCachedVcsRefsChanges = Effect.fn("CachedVcsRefsState.makeChange
179179
}),
180180
),
181181
);
182-
const refreshedRefs = Stream.concat(
183-
Stream.fromEffect(SubscriptionRef.get(supervisor.state)),
184-
SubscriptionRef.changes(supervisor.state),
185-
).pipe(
186-
Stream.map((connection) => (connection.phase === "connected" ? connection.generation : null)),
182+
const refreshedRefs = SubscriptionRef.changes(supervisor.session).pipe(
183+
Stream.map(Option.getOrNull),
187184
Stream.changes,
188-
Stream.switchMap((generation) =>
189-
generation === null
185+
Stream.switchMap((session) =>
186+
session === null
190187
? Stream.empty
191188
: Stream.fromEffect(
192189
refresh().pipe(
190+
// Session removal can precede switchMap cancelling this refresh.
191+
Effect.catchTags({ EnvironmentRpcUnavailableError: () => Effect.never }),
193192
Effect.tapError((error) =>
194193
Effect.logWarning("Could not refresh Git refs.").pipe(
195194
Effect.annotateLogs({

0 commit comments

Comments
 (0)