@devries-cameron20 / t3code-1 / commits / a98ebde990
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(client-runtime): refresh Git refs when the RPC session changes (#23314)

Yash Singh committed Oct 21, 2026 at 22:22 UTC a98ebde9905bb6f600ec75d006656e8fea4c74a7
2 files changed +136 -28
packages/client-runtime/src/state/vcs.test.ts
+130 -21
@@ -3,16 +3,21 @@ import {
3 EnvironmentId,
4 AuthSourceControlWriteScope,
5 WS_METHODS,
6 + VcsProcessExitError,
7 type VcsListRefsResult,
8 } from "@t3tools/contracts";
9 import { describe, expect, it } from "@effect/vitest";
10 +import * as Deferred from "effect/Deferred";
11 import * as Effect from "effect/Effect";
12 import * as Fiber from "effect/Fiber";
13 import * as Layer from "effect/Layer";
14 +import * as Logger from "effect/Logger";
15 import * as Option from "effect/Option";
16 +import * as Queue from "effect/Queue";
17 import * as Ref from "effect/Ref";
18 import * as Stream from "effect/Stream";
19 import * as SubscriptionRef from "effect/SubscriptionRef";
20 +import * as TestClock from "effect/testing/TestClock";
21 import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
22
23 import {
@@ -402,63 +407,167 @@ describe("cached VCS refs", () => {
407 ),
408 );
409
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", () =>
411 Effect.scoped(
412 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>();
458 const calls = yield* Ref.make(0);
459 const interruptions = yield* Ref.make(0);
410 - const connectionState = yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE);
460 const client = {
461 [WS_METHODS.vcsListRefs]: () =>
462 Ref.updateAndGet(calls, (count) => count + 1).pipe(
463 Effect.flatMap((count) =>
464 count === 1
416 - ? Effect.never.pipe(
465 + ? Deferred.succeed(started, undefined).pipe(
466 + Effect.andThen(Effect.never),
467 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 + ),
471 ),
472 )
473 : Effect.succeed(LIVE_REFS),
474 ),
475 ),
476 } as unknown as WsRpcProtocolClient;
477 + const liveSession = yield* SubscriptionRef.make(Option.some(session(client)));
478 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
479 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,
482 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
483 connect: Effect.void,
484 disconnect: Effect.void,
485 retryNow: Effect.void,
486 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
487
435 - const result = Stream.unwrap(
488 + const fiber = yield* Stream.unwrap(
489 makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
490 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
491 Effect.provideService(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())),
492 ),
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);
498 expect(yield* Ref.get(interruptions)).toBe(1);
499
452 - yield* SubscriptionRef.set(connectionState, {
453 - ...CONNECTED_CONNECTION_STATE,
454 - generation: 2,
455 - });
500 + yield* SubscriptionRef.set(liveSession, Option.some(session(client)));
501 expect(Option.getOrThrow(yield* Fiber.join(fiber))).toEqual(LIVE_REFS);
502 expect(yield* Ref.get(calls)).toBe(2);
503 }),
504 ),
505 );
506
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 +
571 it.effect("emits persisted refs before a live refresh", () =>
572 Effect.scoped(
573 Effect.gen(function* () {
packages/client-runtime/src/state/vcs.ts
+6 -7
@@ -179,17 +179,16 @@ export const makeCachedVcsRefsChanges = Effect.fn("CachedVcsRefsState.makeChange
179 }),
180 ),
181 );
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),
184 Stream.changes,
188 - Stream.switchMap((generation) =>
189 - generation === null
185 + Stream.switchMap((session) =>
186 + session === null
187 ? Stream.empty
188 : Stream.fromEffect(
189 refresh().pipe(
190 + // Session removal can precede switchMap cancelling this refresh.
191 + Effect.catchTags({ EnvironmentRpcUnavailableError: () => Effect.never }),
192 Effect.tapError((error) =>
193 Effect.logWarning("Could not refresh Git refs.").pipe(
194 Effect.annotateLogs({