Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/server/src/vcs/VcsStatusBroadcaster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,10 @@ describe("VcsStatusBroadcaster", () => {
return Effect.gen(function* () {
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
const initial = yield* broadcaster.getStatus({ cwd: "/repo" });
const refreshed = yield* broadcaster.refreshStatus("/repo", { localOnly: true });
const { refsDigest, ...refreshed } = yield* broadcaster.refreshStatus("/repo", {
localOnly: true,
});
assert.isString(refsDigest);
assert.deepStrictEqual(refreshed, initial);
assert.deepStrictEqual(yield* broadcaster.getStatus({ cwd: "/repo" }), refreshed);
assert.equal(state.localInvalidationCalls, 1);
Expand Down
26 changes: 16 additions & 10 deletions apps/server/src/vcs/VcsStatusBroadcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,21 +736,27 @@ export const make = Effect.gen(function* () {
)(function* (rawCwd, options) {
const cwd = yield* withFileSystem(normalizeCwd(rawCwd));
yield* acquireBranchChangesDemand(cwd);
const refsBeforeLocalRead = options?.localOnly ? null : yield* workflow.readRefSnapshot(cwd);
yield* refreshLocalStatusCore(cwd);
// Lets the client keep its cached branch list when no ref moved since its last refresh.
const withRefsDigest = (status: VcsStatusResult, refs: string | null) =>
refs === null
? status
: {
...status,
refsDigest: NodeCrypto.createHash("sha256").update(refs).digest("hex").slice(0, 32),
};
if (options?.localOnly) {
const [refs] = yield* Effect.all(
[workflow.readRefSnapshot(cwd), refreshLocalStatusCore(cwd)],
{ concurrency: "unbounded" },
);
// Never cache a remote here: readers take `value: null` as "read and found no PR".
const parts = yield* getStatusParts(cwd);
return mergeGitStatusParts(parts.local, parts.remote);
return withRefsDigest(mergeGitStatusParts(parts.local, parts.remote), refs);
}
const refsBeforeLocalRead = yield* workflow.readRefSnapshot(cwd);
yield* refreshLocalStatusCore(cwd);
const { status, refs } = yield* refreshRemoteAndLocalTotals(cwd, rawCwd, refsBeforeLocalRead);
// Lets the client keep its cached branch list when no ref moved since its last refresh.
return refs === null
? status
: {
...status,
refsDigest: NodeCrypto.createHash("sha256").update(refs).digest("hex").slice(0, 32),
};
return withRefsDigest(status, refs);
}, Effect.scoped);

const refreshAfterCheckout: VcsStatusBroadcaster["Service"]["refreshAfterCheckout"] = Effect.fn(
Expand Down
8 changes: 8 additions & 0 deletions packages/client-runtime/src/state/vcs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,13 @@ describe("cached VCS refs", () => {
expect(yield* Ref.get(clears)).toBe(3);
expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(3);

yield* atoms.refreshLocalStatusForPicker.run(registry, {
environmentId: TARGET.environmentId,
input: { cwd: "/repo", localOnly: true },
});
expect(yield* Ref.get(clears)).toBe(3);
expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(3);

const prepareResult = yield* Effect.exit(
createGitEnvironmentAtoms(runtime).preparePullRequestThread.run(registry, {
environmentId: TARGET.environmentId,
Expand All @@ -485,6 +492,7 @@ describe("cached VCS refs", () => {
[WS_METHODS.vcsRefreshStatus]: () =>
Deferred.succeed(refreshStarted, undefined).pipe(
Effect.andThen(Deferred.await(releaseRefresh)),
Effect.as({}),
),
[WS_METHODS.vcsSwitchRef]: () => Effect.succeed({ refName: "feature" }),
} as unknown as WsRpcProtocolClient;
Expand Down
21 changes: 14 additions & 7 deletions packages/client-runtime/src/state/vcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type EnvironmentId,
type VcsListRefsInput,
type VcsListRefsResult,
type VcsRefreshStatusResult,
type VcsStatusResult,
WS_METHODS,
} from "@t3tools/contracts";
Expand Down Expand Up @@ -294,6 +295,17 @@ export function createVcsEnvironmentAtoms<R, E>(
cwd: target.input.cwd,
});

const invalidateRefsIfChanged = (
target: { readonly environmentId: EnvironmentId; readonly input: { readonly cwd: string } },
registry: AtomRegistry.AtomRegistry,
exit: Exit.Exit<VcsRefreshStatusResult, unknown>,
) =>
invalidateCachedVcsRefsIfChanged(
registry,
{ environmentId: target.environmentId, cwd: target.input.cwd },
Exit.isSuccess(exit) ? exit.value.refsDigest : undefined,
);

return {
listRefs,
// An uncached read for decisions that must match the repository now, not
Expand Down Expand Up @@ -341,19 +353,14 @@ export function createVcsEnvironmentAtoms<R, E>(
tag: WS_METHODS.vcsRefreshStatus,
scheduler: vcsCommandScheduler,
concurrency: vcsCommandConcurrency,
onSettled: (target, registry, exit) =>
invalidateCachedVcsRefsIfChanged(
registry,
{ environmentId: target.environmentId, cwd: target.input.cwd },
Exit.isSuccess(exit) ? exit.value.refsDigest : undefined,
),
onSettled: invalidateRefsIfChanged,
}),
// Outside the serial queue: it only reads status, and a checkout picked right after opening
// the picker must not wait behind that read.
refreshLocalStatusForPicker: createEnvironmentRpcCommand(runtime, {
label: "environment-data:vcs:refresh-local-status-for-picker",
tag: WS_METHODS.vcsRefreshStatus,
onSettled: invalidateRefs,
onSettled: invalidateRefsIfChanged,
}),
createWorktree: createEnvironmentRpcCommand(runtime, {
label: "environment-data:vcs:create-worktree",
Expand Down
Loading