TypeScript
fix(vcs): passive sidebar rows stop retaining remote pollers (#15666)
maria5055de3ae483
diff --git a/apps/mobile/src/connection/background-activity-scopes.ts b/apps/mobile/src/connection/background-activity-scopes.tsindex da013a57219..2e2a343ff0d 100644--- a/apps/mobile/src/connection/background-activity-scopes.ts+++ b/apps/mobile/src/connection/background-activity-scopes.ts@@ -45,7 +45,8 @@ function scopeForSubscription(if (observation.method !== WS_METHODS.subscribeVcsStatus) {return null;}- const input = observation.input as { readonly cwd?: unknown };+ const input = observation.input as { readonly cwd?: unknown; readonly includeRemote?: unknown };+ if (input.includeRemote === false) return null;return typeof input.cwd === "string" ? { type: "vcs-status", cwd: input.cwd } : null;}diff --git a/apps/mobile/src/connection/background-activity.test.ts b/apps/mobile/src/connection/background-activity.test.tsindex 7a2d902557e..170ba4efa9c 100644--- a/apps/mobile/src/connection/background-activity.test.ts+++ b/apps/mobile/src/connection/background-activity.test.ts@@ -12,6 +12,12 @@ describe("mobile background activity", () => {it.effect("retains VCS demand only while the mobile subscription is active", () =>Effect.gen(function* () {const environmentId = EnvironmentId.make("mobile-environment");+ const releasePassive = yield* observeMobileBackgroundActivitySubscription({+ environmentId,+ method: WS_METHODS.subscribeVcsStatus,+ input: { cwd: "/workspace", includeRemote: false },+ });+ expect(retainedMobileBackgroundScopes(environmentId)).toEqual([]);const release = yield* observeMobileBackgroundActivitySubscription({environmentId,method: WS_METHODS.subscribeVcsStatus,@@ -22,6 +28,11 @@ describe("mobile background activity", () => {{ type: "vcs-status", cwd: "/workspace" },]);+ yield* releasePassive;+ expect(retainedMobileBackgroundScopes(environmentId)).toEqual([+ { type: "vcs-status", cwd: "/workspace" },+ ]);+yield* release;expect(retainedMobileBackgroundScopes(environmentId)).toEqual([]);}),diff --git a/apps/server/src/git/GitManager.test.ts b/apps/server/src/git/GitManager.test.tsindex 95b99fb2c43..adef6ab42af 100644--- a/apps/server/src/git/GitManager.test.ts+++ b/apps/server/src/git/GitManager.test.ts@@ -6,7 +6,10 @@ import * as NodeChildProcess from "node:child_process";import * as NodeServices from "@effect/platform-node/NodeServices";import { it } from "@effect/vitest";import * as Duration from "effect/Duration";+import * as Context from "effect/Context";import * as Effect from "effect/Effect";+import * as Deferred from "effect/Deferred";+import * as Exit from "effect/Exit";import * as FileSystem from "effect/FileSystem";import * as Layer from "effect/Layer";import * as Logger from "effect/Logger";@@ -16,11 +19,13 @@ import * as References from "effect/References";import * as Result from "effect/Result";import * as Schema from "effect/Schema";import * as Scope from "effect/Scope";+import * as Stream from "effect/Stream";import { TestClock } from "effect/testing";import { ChildProcessSpawner } from "effect/process";import { expect } from "vite-plus/test";import type {GitActionProgressEvent,+ GitManagerServiceError,GitPreparePullRequestThreadInput,ModelSelection,} from "@t3tools/contracts";@@ -43,6 +48,11 @@ import * as GitLabCli from "../sourceControl/GitLabCli.ts";import * as TextGeneration from "../textGeneration/TextGeneration.ts";import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";import * as VcsProcess from "../vcs/VcsProcess.ts";+import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";+import * as VcsProjectConfig from "../vcs/VcsProjectConfig.ts";+import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";+import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";+import * as GitWorkflowService from "./GitWorkflowService.ts";import * as GitHubSourceControlProvider from "../sourceControl/GitHubSourceControlProvider.ts";import * as GitLabSourceControlProvider from "../sourceControl/GitLabSourceControlProvider.ts";import {@@ -778,6 +788,120 @@ const layerGitManagerTest = GitVcsDriver.layer.pipe();it.layer(layerGitManagerTest)("GitManager", (it) => {+ it.effect("passive worktree status streams do not start remote refreshes", () =>+ Effect.gen(function* () {+ const repoDir = yield* makeTempDir("t3code-passive-vcs-");+ yield* initRepo(repoDir);+ const remoteDir = yield* createBareRemote();+ yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);+ yield* runGit(repoDir, ["push", "-u", "origin", "main"]);+ const worktreeDir = NodePath.join(repoDir, "worktree");+ yield* runGit(repoDir, ["worktree", "add", "-b", "feature/passive", worktreeDir]);+ yield* runGit(worktreeDir, ["push", "-u", "origin", "feature/passive"]);++ const { manager } = yield* makeManager();+ let remoteReads = 0;+ const workflowContext = yield* Layer.build(+ GitWorkflowService.layer.pipe(+ Layer.provide(+ Layer.succeed(GitManager.GitManager, {+ ...manager,+ remoteStatus: (input, options) =>+ manager.remoteStatus(input, options).pipe(+ Effect.tap(() =>+ Effect.sync(() => {+ remoteReads += 1;+ }),+ ),+ ),+ }),+ ),+ Layer.provide(+ VcsDriverRegistry.layer.pipe(+ Layer.provide(VcsProjectConfig.layer),+ Layer.provide(VcsProcess.layer),+ ),+ ),+ ),+ );+ const broadcasterContext = yield* Layer.build(+ VcsStatusBroadcaster.layer.pipe(+ Layer.provide(+ Layer.succeed(+ GitWorkflowService.GitWorkflowService,+ Context.get(workflowContext, GitWorkflowService.GitWorkflowService),+ ),+ ),+ Layer.provide(+ Layer.mock(BackgroundPolicy.BackgroundPolicy)({+ hasDemand: () => Effect.succeed(true),+ shouldRunScopeWork: () => Effect.succeed(true),+ }),+ ),+ ),+ );+ const broadcaster = Context.get(+ broadcasterContext,+ VcsStatusBroadcaster.VcsStatusBroadcaster,+ );+ const passiveScope = yield* Scope.make();+ const snapshots = yield* Deferred.make<void, GitManagerServiceError>();+ const localUpdated = yield* Deferred.make<void>();+ const remoteUpdated = yield* Deferred.make<void>();+ let snapshotCount = 0;+ for (const cwd of [repoDir, worktreeDir]) {+ yield* Stream.runForEach(+ broadcaster.streamStatus(+ { cwd, includeRemote: false },+ { automaticRemoteRefreshInterval: Effect.succeed(Duration.seconds(1)) },+ ),+ (event) => {+ if (+ cwd === worktreeDir &&+ event._tag === "localUpdated" &&+ event.local.hasWorkingTreeChanges+ ) {+ return Deferred.succeed(localUpdated, undefined);+ }+ if (cwd === worktreeDir && event._tag === "remoteUpdated") {+ expect(event.remote?.hasUpstream).toBe(true);+ return Deferred.succeed(remoteUpdated, undefined);+ }+ if (event._tag !== "snapshot") return Effect.void;+ expect(event.local.isRepo).toBe(true);+ expect(event.local.refName).toBe(cwd === repoDir ? "main" : "feature/passive");+ snapshotCount += 1;+ return snapshotCount === 2 ? Deferred.succeed(snapshots, undefined) : Effect.void;+ },+ ).pipe(+ Effect.catchCause((cause) => Deferred.failCause(snapshots, cause)),+ Effect.forkIn(passiveScope),+ );+ }+ yield* Deferred.await(snapshots);+ expect(remoteReads).toBe(0);+ yield* TestClock.adjust("1 minute");+ expect(remoteReads).toBe(0);++ const fs = yield* FileSystem.FileSystem;+ yield* fs.writeFileString(NodePath.join(worktreeDir, "README.md"), "changed\n");+ yield* broadcaster.refreshLocalStatus(worktreeDir);+ yield* Deferred.await(localUpdated);+ expect(remoteReads).toBe(0);++ const activeScope = yield* Scope.make();+ yield* Stream.runDrain(broadcaster.streamStatus({ cwd: worktreeDir })).pipe(+ Effect.forkIn(activeScope),+ );+ yield* Deferred.await(remoteUpdated);+ expect(remoteReads).toBe(1);+ yield* Scope.close(activeScope, Exit.void);+ yield* TestClock.adjust("1 minute");+ expect(remoteReads).toBe(1);+ yield* Scope.close(passiveScope, Exit.void);+ }),+ );+it.effect("status includes draft PR metadata when branch already has a draft PR", () =>Effect.gen(function* () {const repoDir = yield* makeTempDir("t3code-git-manager-");diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.tsindex 480ed8458fe..35d013433c4 100644--- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts+++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts@@ -594,7 +594,7 @@ describe("VcsStatusBroadcaster", () => {},);- it.effect("streams a local snapshot first and remote updates later", () => {+ it.effect("passive streams retain cached remote status", () => {const state = {currentLocalStatus: baseLocalStatus,currentRemoteStatus: baseRemoteStatus,@@ -608,18 +608,25 @@ describe("VcsStatusBroadcaster", () => {const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;const snapshotDeferred = yield* Deferred.make<VcsStatusStreamEvent>();const remoteUpdatedDeferred = yield* Deferred.make<VcsStatusStreamEvent>();- yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => {- if (event._tag === "snapshot") {- return Deferred.succeed(snapshotDeferred, event).pipe(Effect.ignore);- }- if (event._tag === "remoteUpdated") {- return Deferred.succeed(remoteUpdatedDeferred, event).pipe(Effect.ignore);- }- return Effect.void;- }).pipe(Effect.forkScoped);+ yield* Stream.runForEach(+ broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }),+ (event) => {+ if (event._tag === "snapshot") {+ return Deferred.succeed(snapshotDeferred, event).pipe(Effect.ignore);+ }+ if (event._tag === "remoteUpdated") {+ return Deferred.succeed(remoteUpdatedDeferred, event).pipe(Effect.ignore);+ }+ return Effect.void;+ },+ ).pipe(Effect.forkScoped);const snapshot = yield* Deferred.await(snapshotDeferred);- yield* broadcaster.refreshStatus("/repo");+ yield* TestClock.adjust("1 minute");+ assert.equal(state.remoteStatusCalls, 0);+ yield* broadcaster.getStatus({ cwd: "/repo" });+ state.currentRemoteStatus = remoteStatusWithPr;+ yield* broadcaster.refreshPullRequestStatus("/repo");const remoteUpdated = yield* Deferred.await(remoteUpdatedDeferred);assert.deepStrictEqual(snapshot, {@@ -629,8 +636,16 @@ describe("VcsStatusBroadcaster", () => {} satisfies VcsStatusStreamEvent);assert.deepStrictEqual(remoteUpdated, {_tag: "remoteUpdated",- remote: baseRemoteStatus,+ remote: remoteStatusWithPr,} satisfies VcsStatusStreamEvent);+ const cachedSnapshot = yield* Stream.runHead(+ broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }),+ );+ assert.deepStrictEqual(Option.getOrThrow(cachedSnapshot), {+ _tag: "snapshot",+ local: baseLocalStatus,+ remote: remoteStatusWithPr,+ });}).pipe(Effect.provide(layerTestFor(state)));});@@ -1032,7 +1047,7 @@ describe("VcsStatusBroadcaster", () => {}).pipe(Effect.provide(layerTest));});- it.effect("stops the remote poller after the last stream subscriber disconnects", () => {+ it.effect("releases remote demand while passive observers remain", () => {const state = {currentLocalStatus: baseLocalStatus,currentRemoteStatus: baseRemoteStatus,@@ -1092,6 +1107,16 @@ describe("VcsStatusBroadcaster", () => {const secondSnapshot = yield* Deferred.make<VcsStatusStreamEvent>();const firstScope = yield* Scope.make();const secondScope = yield* Scope.make();+ const passiveSnapshot = yield* Deferred.make<VcsStatusStreamEvent>();+ yield* Stream.runForEach(+ broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }),+ (event) =>+ event._tag === "snapshot"+ ? Deferred.succeed(passiveSnapshot, event).pipe(Effect.ignore)+ : Effect.void,+ ).pipe(Effect.forkScoped);+ yield* Deferred.await(passiveSnapshot);+ assert.equal(state.remoteStatusCalls, 0);yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>event._tag === "snapshot"? Deferred.succeed(firstSnapshot, event).pipe(Effect.ignore)diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.tsindex 1f43ade5a28..468bc3301bf 100644--- a/apps/server/src/vcs/VcsStatusBroadcaster.ts+++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts@@ -19,6 +19,7 @@ import type {VcsStatusRemoteResult,VcsStatusResult,VcsStatusStreamEvent,+ VcsStatusSubscriptionInput,} from "@t3tools/contracts";import { mergeGitStatusParts } from "@t3tools/shared/git";import * as KeyedLock from "@t3tools/shared/KeyedLock";@@ -201,7 +202,7 @@ export class VcsStatusBroadcaster extends Context.Service<cwd: string,) => Effect.Effect<VcsStatusRemoteResult | null, GitManagerServiceError>;readonly streamStatus: (- input: VcsStatusInput,+ input: VcsStatusSubscriptionInput,options?: StreamStatusOptions,) => Stream.Stream<VcsStatusStreamEvent, GitManagerServiceError>;}@@ -708,15 +709,19 @@ export const make = Effect.gen(function* () {const initialLocal = yield* getOrLoadLocalStatus(cwd);const cachedStatus = yield* getCachedStatus(cwd);const initialRemote = cachedStatus?.remote?.value ?? null;- yield* retainRemotePoller(- cwd,- input.cwd,- options?.automaticRemoteRefreshInterval ??- Effect.succeed(DEFAULT_VCS_STATUS_REFRESH_INTERVAL),- cachedStatus?.remote === null || cachedStatus?.remote === undefined,- );-- const release = releaseRemotePoller(cwd, input.cwd).pipe(Effect.ignore, Effect.asVoid);+ if (input.includeRemote !== false) {+ yield* retainRemotePoller(+ cwd,+ input.cwd,+ options?.automaticRemoteRefreshInterval ??+ Effect.succeed(DEFAULT_VCS_STATUS_REFRESH_INTERVAL),+ cachedStatus?.remote === null || cachedStatus?.remote === undefined,+ );+ }+ const release =+ input.includeRemote === false+ ? Effect.void+ : releaseRemotePoller(cwd, input.cwd).pipe(Effect.ignore, Effect.asVoid);return Stream.concat(Stream.make({diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsxindex 667f87b8df9..370d4f335e8 100644--- a/apps/web/src/components/Sidebar.tsx+++ b/apps/web/src/components/Sidebar.tsx@@ -1209,7 +1209,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {leaseLiveStatus && (thread.branch != null || thread.worktreePath !== null) && gitCwd !== null? vcsEnvironment.status({environmentId: thread.environmentId,- input: { cwd: gitCwd },+ input: { cwd: gitCwd, includeRemote: false },}): null,);@@ -2204,7 +2204,7 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {leaseLiveStatus && (thread.branch != null || thread.worktreePath !== null) && gitCwd !== null? vcsEnvironment.status({environmentId: thread.environmentId,- input: { cwd: gitCwd },+ input: { cwd: gitCwd, includeRemote: false },}): null,);diff --git a/apps/web/src/components/ThreadStatusIndicators.tsx b/apps/web/src/components/ThreadStatusIndicators.tsxindex b17d8f324c6..ad0c9c99a29 100644--- a/apps/web/src/components/ThreadStatusIndicators.tsx+++ b/apps/web/src/components/ThreadStatusIndicators.tsx@@ -910,7 +910,7 @@ export function ThreadRowLeadingStatus({gitCwd !== null? vcsEnvironment.status({environmentId: thread.environmentId,- input: { cwd: gitCwd },+ input: { cwd: gitCwd, includeRemote: false },}): null,);diff --git a/apps/web/src/lib/backgroundActivityReporter.test.ts b/apps/web/src/lib/backgroundActivityReporter.test.tsindex e32d991f9f5..23e90448332 100644--- a/apps/web/src/lib/backgroundActivityReporter.test.ts+++ b/apps/web/src/lib/backgroundActivityReporter.test.ts@@ -22,6 +22,12 @@ describe("wasRecentlyInteracted", () => {Effect.gen(function* () {const environmentId = EnvironmentId.make("environment-observation-test");const scope = { type: "vcs-status" as const, cwd: "/repo" };+ const releasePassive = yield* observeBackgroundActivitySubscription({+ environmentId,+ method: WS_METHODS.subscribeVcsStatus,+ input: { cwd: scope.cwd, includeRemote: false },+ });+ expect(retainedBackgroundScopes(environmentId)).toEqual([]);const release = yield* observeBackgroundActivitySubscription({environmentId,method: WS_METHODS.subscribeVcsStatus,@@ -30,6 +36,9 @@ describe("wasRecentlyInteracted", () => {expect(retainedBackgroundScopes(environmentId)).toEqual([scope]);+ yield* releasePassive;+ expect(retainedBackgroundScopes(environmentId)).toEqual([scope]);+yield* release;expect(retainedBackgroundScopes(environmentId)).toEqual([]);}),diff --git a/apps/web/src/lib/backgroundActivityReporter.ts b/apps/web/src/lib/backgroundActivityReporter.tsindex bf0e7def102..5a65dab3809 100644--- a/apps/web/src/lib/backgroundActivityReporter.ts+++ b/apps/web/src/lib/backgroundActivityReporter.ts@@ -118,7 +118,8 @@ function scopeForSubscription(if (observation.method !== WS_METHODS.subscribeVcsStatus) {return null;}- const input = observation.input as { readonly cwd?: unknown };+ const input = observation.input as { readonly cwd?: unknown; readonly includeRemote?: unknown };+ if (input.includeRemote === false) return null;return typeof input.cwd === "string" ? { type: "vcs-status", cwd: input.cwd } : null;}diff --git a/packages/contracts/src/git.ts b/packages/contracts/src/git.tsindex 6e2efd0404b..b2cb2e902d8 100644--- a/packages/contracts/src/git.ts+++ b/packages/contracts/src/git.ts@@ -111,6 +111,13 @@ export const VcsStatusInput = Schema.Struct({});export type VcsStatusInput = typeof VcsStatusInput.Type;+export const VcsStatusSubscriptionInput = Schema.Struct({+ ...VcsStatusInput.fields,+ /** Passive observers receive cached remote status without retaining its refresh loop. */+ includeRemote: Schema.optional(Schema.Boolean),+});+export type VcsStatusSubscriptionInput = typeof VcsStatusSubscriptionInput.Type;+export const VcsPullInput = Schema.Struct({cwd: TrimmedNonEmptyStringSchema,});diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.tsindex 4d6ec6e4722..f0593530923 100644--- a/packages/contracts/src/rpc.ts+++ b/packages/contracts/src/rpc.ts@@ -121,6 +121,7 @@ import {GitResolvePullRequestResult,GitRunStackedActionInput,VcsStatusInput,+ VcsStatusSubscriptionInput,VcsStatusResult,VcsStatusStreamEvent,} from "./git.ts";@@ -1236,7 +1237,7 @@ const WsProviderUploadFeedbackRpc = Rpc.make(WS_METHODS.providerUploadFeedback,});const WsSubscribeVcsStatusRpc = Rpc.make(WS_METHODS.subscribeVcsStatus, {- payload: VcsStatusInput,+ payload: VcsStatusSubscriptionInput,success: VcsStatusStreamEvent,error: Schema.Union([GitManagerServiceError, EnvironmentAuthorizationError]),stream: true,