Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Parallelize VCS status refresh and status reads (#3112)

Julius Marmingea5efb7cb6b9e
diff --git a/apps/server/src/git/GitManager.ts b/apps/server/src/git/GitManager.ts
index 92ee9007b..991211827 100644
--- a/apps/server/src/git/GitManager.ts
+++ b/apps/server/src/git/GitManager.ts
@@ -1361,7 +1361,9 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
},
);
const status: GitManagerShape["status"] = Effect.fn("status")(function* (input) {
- const [local, remote] = yield* Effect.all([localStatus(input), remoteStatus(input)]);
+ const [local, remote] = yield* Effect.all([localStatus(input), remoteStatus(input)], {
+ concurrency: "unbounded",
+ });
return mergeGitStatusParts(local, remote);
});
const invalidateLocalStatus: GitManagerShape["invalidateLocalStatus"] = Effect.fn(
diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts
index e92ab374f..7c5768162 100644
--- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts
+++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts
@@ -17,6 +17,7 @@ import type {
VcsStatusResult,
VcsStatusStreamEvent,
} from "@t3tools/contracts";
+import { GitManagerError } from "@t3tools/contracts";
import * as VcsStatusBroadcaster from "./VcsStatusBroadcaster.ts";
import * as GitWorkflowService from "../git/GitWorkflowService.ts";
@@ -149,6 +150,71 @@ describe("VcsStatusBroadcaster", () => {
}).pipe(Effect.provide(makeTestLayer(state)));
});
+ it.effect("keeps the cached snapshot unchanged when a refresh branch fails", () => {
+ const state = {
+ currentLocalStatus: baseLocalStatus,
+ currentRemoteStatus: baseRemoteStatus,
+ localStatusCalls: 0,
+ remoteStatusCalls: 0,
+ localInvalidationCalls: 0,
+ remoteInvalidationCalls: 0,
+ failRemoteStatus: false,
+ };
+ const testLayer = VcsStatusBroadcaster.layer.pipe(
+ Layer.provideMerge(NodeServices.layer),
+ Layer.provide(
+ Layer.mock(GitWorkflowService.GitWorkflowService)({
+ localStatus: () =>
+ Effect.sync(() => {
+ state.localStatusCalls += 1;
+ return state.currentLocalStatus;
+ }),
+ remoteStatus: () =>
+ Effect.suspend(() => {
+ state.remoteStatusCalls += 1;
+ return state.failRemoteStatus
+ ? Effect.fail(
+ new GitManagerError({
+ operation: "VcsStatusBroadcaster.test",
+ detail: "remote status failed",
+ }),
+ )
+ : Effect.succeed(state.currentRemoteStatus);
+ }),
+ invalidateLocalStatus: () =>
+ Effect.sync(() => {
+ state.localInvalidationCalls += 1;
+ }),
+ invalidateRemoteStatus: () =>
+ Effect.sync(() => {
+ state.remoteInvalidationCalls += 1;
+ }),
+ }),
+ ),
+ );
+
+ return Effect.gen(function* () {
+ const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
+ yield* broadcaster.getStatus({ cwd: "/repo" });
+
+ state.currentLocalStatus = {
+ ...baseLocalStatus,
+ refName: "feature/partial-refresh",
+ };
+ state.currentRemoteStatus = {
+ ...baseRemoteStatus,
+ aheadCount: 3,
+ };
+ state.failRemoteStatus = true;
+
+ const refreshExit = yield* broadcaster.refreshStatus("/repo").pipe(Effect.exit);
+ const cached = yield* broadcaster.getStatus({ cwd: "/repo" });
+
+ assert.isTrue(Exit.isFailure(refreshExit));
+ assert.deepStrictEqual(cached, baseStatus);
+ }).pipe(Effect.provide(testLayer));
+ });
+
it.effect("refreshes only the cached local snapshot when requested", () => {
const state = {
currentLocalStatus: baseLocalStatus,
diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts
index 9cd5a6c33..d83dc26fb 100644
--- a/apps/server/src/vcs/VcsStatusBroadcaster.ts
+++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts
@@ -179,18 +179,53 @@ export const layer = Layer.effect(
},
);
- const loadLocalStatus = Effect.fn("VcsStatusBroadcaster.loadLocalStatus")(function* (
+ const updateCachedStatus = Effect.fn("VcsStatusBroadcaster.updateCachedStatus")(function* (
cwd: string,
+ local: VcsStatusLocalResult,
+ remote: VcsStatusRemoteResult | null,
+ options?: { publish?: boolean },
) {
- const local = yield* workflow.localStatus({ cwd });
- return yield* updateCachedLocalStatus(cwd, local);
+ const nextLocal = {
+ fingerprint: fingerprintStatusPart(local),
+ value: local,
+ } satisfies CachedValue<VcsStatusLocalResult>;
+ const nextRemote = {
+ fingerprint: fingerprintStatusPart(remote),
+ value: remote,
+ } satisfies CachedValue<VcsStatusRemoteResult | null>;
+ const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
+ const previous = cache.get(cwd) ?? { local: null, remote: null };
+ const nextCache = new Map(cache);
+ nextCache.set(cwd, {
+ local: nextLocal,
+ remote: nextRemote,
+ });
+ return [
+ previous.local?.fingerprint !== nextLocal.fingerprint ||
+ previous.remote?.fingerprint !== nextRemote.fingerprint,
+ nextCache,
+ ] as const;
+ });
+
+ if (options?.publish && shouldPublish) {
+ yield* PubSub.publish(changesPubSub, {
+ cwd,
+ event: {
+ _tag: "snapshot",
+ local,
+ remote,
+ },
+ });
+ }
+
+ return mergeGitStatusParts(local, remote);
});
- const loadRemoteStatus = Effect.fn("VcsStatusBroadcaster.loadRemoteStatus")(function* (
+ const loadLocalStatus = Effect.fn("VcsStatusBroadcaster.loadLocalStatus")(function* (
cwd: string,
) {
- const remote = yield* workflow.remoteStatus({ cwd });
- return yield* updateCachedRemoteStatus(cwd, remote);
+ const local = yield* workflow.localStatus({ cwd });
+ return yield* updateCachedLocalStatus(cwd, local);
});
const getOrLoadLocalStatus = Effect.fn("VcsStatusBroadcaster.getOrLoadLocalStatus")(function* (
@@ -203,36 +238,39 @@ export const layer = Layer.effect(
return yield* loadLocalStatus(cwd);
});
- const getOrLoadRemoteStatus = Effect.fn("VcsStatusBroadcaster.getOrLoadRemoteStatus")(
- function* (cwd: string) {
- const cached = yield* getCachedStatus(cwd);
- if (cached?.remote) {
- return cached.remote.value;
- }
- return yield* loadRemoteStatus(cwd);
- },
- );
-
const withFileSystem = Effect.provideService(FileSystem.FileSystem, fs);
const getStatus: VcsStatusBroadcasterShape["getStatus"] = Effect.fn(
"VcsStatusBroadcaster.getStatus",
)(function* (input) {
const cwd = yield* withFileSystem(normalizeCwd(input.cwd));
- const [local, remote] = yield* Effect.all([
- getOrLoadLocalStatus(cwd),
- getOrLoadRemoteStatus(cwd),
- ]);
- return mergeGitStatusParts(local, remote);
+ const cached = yield* getCachedStatus(cwd);
+ if (cached?.local && cached.remote) {
+ return mergeGitStatusParts(cached.local.value, cached.remote.value);
+ }
+ const [local, remote] = yield* Effect.all(
+ [
+ cached?.local ? Effect.succeed(cached.local.value) : workflow.localStatus({ cwd }),
+ cached?.remote ? Effect.succeed(cached.remote.value) : workflow.remoteStatus({ cwd }),
+ ],
+ { concurrency: "unbounded" },
+ );
+ return yield* updateCachedStatus(cwd, local, remote);
});
+ const refreshLocalStatusCore = Effect.fn("VcsStatusBroadcaster.refreshLocalStatusCore")(
+ function* (cwd: string) {
+ yield* workflow.invalidateLocalStatus(cwd);
+ const local = yield* workflow.localStatus({ cwd });
+ return yield* updateCachedLocalStatus(cwd, local, { publish: true });
+ },
+ );
+
const refreshLocalStatus: VcsStatusBroadcasterShape["refreshLocalStatus"] = Effect.fn(
"VcsStatusBroadcaster.refreshLocalStatus",
)(function* (rawCwd) {
const cwd = yield* withFileSystem(normalizeCwd(rawCwd));
- yield* workflow.invalidateLocalStatus(cwd);
- const local = yield* workflow.localStatus({ cwd });
- return yield* updateCachedLocalStatus(cwd, local, { publish: true });
+ return yield* refreshLocalStatusCore(cwd);
});
const refreshRemoteStatus = Effect.fn("VcsStatusBroadcaster.refreshRemoteStatus")(function* (
@@ -247,11 +285,15 @@ export const layer = Layer.effect(
"VcsStatusBroadcaster.refreshStatus",
)(function* (rawCwd) {
const cwd = yield* withFileSystem(normalizeCwd(rawCwd));
- const [local, remote] = yield* Effect.all([
- refreshLocalStatus(cwd),
- refreshRemoteStatus(cwd),
- ]);
- return mergeGitStatusParts(local, remote);
+ yield* Effect.all(
+ [workflow.invalidateLocalStatus(cwd), workflow.invalidateRemoteStatus(cwd)],
+ { concurrency: "unbounded", discard: true },
+ );
+ const [local, remote] = yield* Effect.all(
+ [workflow.localStatus({ cwd }), workflow.remoteStatus({ cwd })],
+ { concurrency: "unbounded" },
+ );
+ return yield* updateCachedStatus(cwd, local, remote, { publish: true });
});
const makeRemoteRefreshLoop = (