Skip to content

Commit 3ffd240

Browse files
fix(server): a turn that adds a Git remote shows "Add to another machine" right away (#29947)
1 parent ea3e20a commit 3ffd240

6 files changed

Lines changed: 211 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ const layerTest = Layer.mergeAll(
143143
repositoryIdentityResolved: false,
144144
}),
145145
invalidate: () => Effect.void,
146+
refreshRepositoryIdentity: () => Effect.void,
146147
subscribeChanges: Effect.never,
147148
}),
148149
),

‎apps/server/src/orchestration-v2/RunFinalizationQueue.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as Layer from "effect/Layer";
55
import * as Option from "effect/Option";
66

77
import * as SqlitePersistence from "../persistence/Sqlite.ts";
8+
import * as ProjectEnrichment from "../project/ProjectEnrichmentService.ts";
9+
import * as ProjectService from "../project/ProjectService.ts";
810
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
911
import * as ServerSettings from "../serverSettings.ts";
1012
import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
@@ -43,11 +45,14 @@ it.effect.each([
4345
checkpointScopes: [{ id: scopeId, runId, kind: "root_run" as const, cwd: "/repo" }],
4446
checkpoints: [],
4547
}),
48+
getThreadShell: () => Effect.succeed(null),
4649
});
4750
const observer = RunFinalization.layerObserver.pipe(
4851
Layer.provide(
4952
Layer.mergeAll(
5053
projections,
54+
Layer.mock(ProjectService.ProjectService)({}),
55+
Layer.mock(ProjectEnrichment.ProjectEnrichmentService)({}),
5156
Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
5257
Layer.mock(PullRequestService.PullRequestService)({ refreshAfterTurn: () => Effect.void }),
5358
Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({

‎apps/server/src/orchestration-v2/RunFinalizationService.test.ts‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
12
import { assert, it } from "@effect/vitest";
23
import {
34
CheckpointScopeId,
5+
ProjectId,
46
RunId,
57
ThreadId,
68
type OrchestrationV2ThreadShell,
9+
type Project,
10+
type RepositoryIdentity,
711
} from "@t3tools/contracts";
812
import * as Deferred from "effect/Deferred";
913
import * as Effect from "effect/Effect";
1014
import * as Fiber from "effect/Fiber";
15+
import * as FileSystem from "effect/FileSystem";
1116
import * as Layer from "effect/Layer";
1217
import * as Option from "effect/Option";
18+
import * as PubSub from "effect/PubSub";
1319
import * as TestClock from "effect/testing/TestClock";
1420

21+
import * as ProcessRunner from "../processRunner.ts";
22+
import * as ProjectEnrichment from "../project/ProjectEnrichmentService.ts";
23+
import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
24+
import * as ProjectService from "../project/ProjectService.ts";
25+
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
1526
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
1627
import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
1728
import * as WorkspaceEntries from "../workspace/WorkspaceEntries.ts";
1829
import * as CheckpointCapture from "./CheckpointCaptureService.ts";
1930
import * as ProjectionStore from "./ProjectionStore.ts";
2031
import * as RunFinalization from "./RunFinalizationService.ts";
2132

33+
// Without a project the observer never touches enrichment, so its methods stay unimplemented.
34+
const layerNoProject = Layer.mergeAll(
35+
Layer.mock(ProjectService.ProjectService)({ getById: () => Effect.succeedNone }),
36+
Layer.mock(ProjectEnrichment.ProjectEnrichmentService)({}),
37+
);
38+
2239
it.effect("completes finalization when the post-checkpoint workspace refresh fails", () => {
2340
const threadId = ThreadId.make("thread_refresh_failure");
2441
const runId = RunId.make("run_refresh_failure");
@@ -103,6 +120,7 @@ it.effect.each(
103120
const layer = RunFinalization.layerObserver.pipe(
104121
Layer.provide(
105122
Layer.mergeAll(
123+
layerNoProject,
106124
Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
107125
Layer.mock(PullRequestService.PullRequestService)({
108126
refreshAfterTurn: () => Effect.void,
@@ -150,6 +168,7 @@ it.effect("returns from the post-turn refresh without waiting for the PR lookup"
150168
const layer = RunFinalization.layerObserver.pipe(
151169
Layer.provide(
152170
Layer.mergeAll(
171+
layerNoProject,
153172
Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
154173
Layer.mock(PullRequestService.PullRequestService)({
155174
refreshAfterTurn: () => Effect.void,
@@ -200,6 +219,7 @@ it.effect("replaces a still-running PR lookup when a newer turn finishes", () =>
200219
const layer = RunFinalization.layerObserver.pipe(
201220
Layer.provide(
202221
Layer.mergeAll(
222+
layerNoProject,
203223
Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
204224
Layer.mock(PullRequestService.PullRequestService)({
205225
refreshAfterTurn: () => Effect.void,
@@ -241,3 +261,138 @@ it.effect("replaces a still-running PR lookup when a newer turn finishes", () =>
241261
assert.strictEqual(lookups, 2);
242262
}).pipe(Effect.provide(layer));
243263
});
264+
265+
const identityProjectId = ProjectId.make("project-identity-refresh");
266+
const identityThreadId = ThreadId.make("thread-identity-refresh");
267+
const identityRunId = RunId.make("completed-run");
268+
269+
const layerIdentityRefresh = <R>(
270+
workspaceRoot: string,
271+
resolver: Layer.Layer<RepositoryIdentityResolver.RepositoryIdentityResolver, never, R>,
272+
) =>
273+
RunFinalization.layerObserver.pipe(
274+
Layer.provideMerge(
275+
Layer.mergeAll(
276+
Layer.effect(ProjectEnrichment.ProjectEnrichmentService, ProjectEnrichment.make()).pipe(
277+
Layer.provide(
278+
Layer.mergeAll(
279+
resolver,
280+
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
281+
invalidate: () => Effect.void,
282+
resolvePath: () => Effect.succeed(null),
283+
}),
284+
),
285+
),
286+
),
287+
Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
288+
Layer.mock(PullRequestService.PullRequestService)({ refreshAfterTurn: () => Effect.void }),
289+
Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
290+
refreshLocalStatus: () =>
291+
Effect.succeed({
292+
isRepo: true,
293+
hasPrimaryRemote: true,
294+
isDefaultRef: true,
295+
refName: "main",
296+
hasWorkingTreeChanges: false,
297+
workingTree: { files: [], insertions: 0, deletions: 0 },
298+
}),
299+
}),
300+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
301+
getThreadShell: () =>
302+
Effect.succeed({
303+
id: identityThreadId,
304+
projectId: identityProjectId,
305+
branch: "main",
306+
activeRunId: null,
307+
} as OrchestrationV2ThreadShell),
308+
}),
309+
Layer.mock(ProjectService.ProjectService)({
310+
getById: () => Effect.succeedSome({ id: identityProjectId, workspaceRoot } as Project),
311+
}),
312+
),
313+
),
314+
);
315+
316+
it.effect("resolves the identity of a project whose remote was added during the turn", () =>
317+
Effect.gen(function* () {
318+
const fileSystem = yield* FileSystem.FileSystem;
319+
const processRunner = yield* ProcessRunner.ProcessRunner;
320+
const workspaceRoot = yield* fileSystem.makeTempDirectoryScoped({
321+
prefix: "t3-run-finalization-identity-test-",
322+
});
323+
const git = (args: ReadonlyArray<string>) =>
324+
processRunner.run({ command: "git", args: ["-C", workspaceRoot, ...args] });
325+
yield* git(["init"]);
326+
327+
yield* Effect.gen(function* () {
328+
const enrichment = yield* ProjectEnrichment.ProjectEnrichmentService;
329+
// A shell read before the turn caches the folder as having no repository.
330+
assert.isNull(yield* enrichment.readRepositoryIdentity(workspaceRoot));
331+
332+
yield* git(["remote", "add", "origin", "https://github.com/t3-triage/example-29929.git"]);
333+
const changes = yield* enrichment.subscribeChanges;
334+
const observer = yield* RunFinalization.RunFinalizationObserver;
335+
yield* observer.refresh({
336+
cwd: workspaceRoot,
337+
threadId: identityThreadId,
338+
runId: identityRunId,
339+
});
340+
341+
const change = yield* PubSub.take(changes);
342+
assert.strictEqual(
343+
change.enrichment.repositoryIdentity?.locator.remoteUrl,
344+
"https://github.com/t3-triage/example-29929.git",
345+
"the project's repository identity is still null after the turn added a remote",
346+
);
347+
}).pipe(
348+
Effect.scoped,
349+
Effect.provide(layerIdentityRefresh(workspaceRoot, RepositoryIdentityResolver.layer)),
350+
);
351+
}).pipe(
352+
Effect.scoped,
353+
Effect.provide(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer))),
354+
),
355+
);
356+
357+
it.effect("returns from the post-turn refresh while the identity resolver is still running", () => {
358+
const workspaceRoot = "/repo-stubbed-identity";
359+
const identity: RepositoryIdentity = {
360+
canonicalKey: "example.test/stubbed-identity",
361+
locator: {
362+
source: "git-remote",
363+
remoteName: "origin",
364+
remoteUrl: "https://example.test/stubbed-identity.git",
365+
},
366+
};
367+
const releaseResolver = Deferred.makeUnsafe<void>();
368+
let remoteAdded = false;
369+
const resolver = Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, {
370+
resolve: (_cwd, options) =>
371+
options?.refresh
372+
? Deferred.await(releaseResolver).pipe(
373+
Effect.map(() => {
374+
remoteAdded = true;
375+
return identity;
376+
}),
377+
)
378+
: Effect.sync(() => (remoteAdded ? identity : null)),
379+
});
380+
return Effect.gen(function* () {
381+
const enrichment = yield* ProjectEnrichment.ProjectEnrichmentService;
382+
assert.isNull(yield* enrichment.readRepositoryIdentity(workspaceRoot));
383+
const changes = yield* enrichment.subscribeChanges;
384+
const observer = yield* RunFinalization.RunFinalizationObserver;
385+
const refresh = yield* observer
386+
.refresh({ cwd: workspaceRoot, threadId: identityThreadId, runId: identityRunId })
387+
.pipe(Effect.timeoutOption("1 minute"), Effect.forkChild({ startImmediately: true }));
388+
yield* TestClock.adjust("1 minute");
389+
assert.isTrue(
390+
Option.isSome(yield* Fiber.join(refresh)),
391+
"turn finalization waited on the identity resolver, blocking the thread's next turn",
392+
);
393+
394+
yield* Deferred.succeed(releaseResolver, undefined);
395+
const change = yield* PubSub.take(changes);
396+
assert.strictEqual(change.enrichment.repositoryIdentity?.canonicalKey, identity.canonicalKey);
397+
}).pipe(Effect.scoped, Effect.provide(layerIdentityRefresh(workspaceRoot, resolver)));
398+
});

‎apps/server/src/orchestration-v2/RunFinalizationService.ts‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import * as Context from "effect/Context";
33
import * as Effect from "effect/Effect";
44
import * as FiberMap from "effect/FiberMap";
55
import * as Layer from "effect/Layer";
6+
import * as Option from "effect/Option";
67
import * as Schema from "effect/Schema";
78

9+
import * as ProjectEnrichment from "../project/ProjectEnrichmentService.ts";
10+
import * as ProjectService from "../project/ProjectService.ts";
811
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
912
import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
1013
import * as WorkspaceEntries from "../workspace/WorkspaceEntries.ts";
@@ -97,7 +100,32 @@ export const layerObserver = Layer.effect(
97100
const vcsStatus = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
98101
const projections = yield* ProjectionStore.ProjectionStoreV2;
99102
const pullRequests = yield* PullRequestService.PullRequestService;
103+
const projects = yield* ProjectService.ProjectService;
104+
const enrichment = yield* ProjectEnrichment.ProjectEnrichmentService;
100105
const pullRequestLookups = yield* FiberMap.make<string>();
106+
const identityRefreshes = yield* FiberMap.make<ProjectId>();
107+
// A turn can add the first remote (`git remote add`, `gh repo create --push`), which the
108+
// enrichment and resolver caches would otherwise keep reporting as missing for a minute.
109+
const refreshMissingRepositoryIdentity = Effect.fn(
110+
"RunFinalizationService.refreshMissingRepositoryIdentity",
111+
)(
112+
function* (projectId: ProjectId) {
113+
const project = yield* projects.getById(projectId);
114+
if (Option.isNone(project)) return;
115+
const { workspaceRoot } = project.value;
116+
if ((yield* enrichment.readRepositoryIdentity(workspaceRoot)) !== null) return;
117+
yield* enrichment.refreshRepositoryIdentity(workspaceRoot);
118+
},
119+
(effect, projectId) =>
120+
effect.pipe(
121+
Effect.catch((error) =>
122+
Effect.logWarning("failed to refresh repository identity after run completion", {
123+
projectId,
124+
detail: error.message,
125+
}),
126+
),
127+
),
128+
);
101129
return {
102130
refreshAfterTurn: pullRequests.refreshAfterTurn,
103131
refresh: Effect.fn("RunFinalizationService.refresh")(
@@ -106,9 +134,14 @@ export const layerObserver = Layer.effect(
106134
[workspaceEntries.refresh(cwd), vcsStatus.refreshLocalStatus(cwd)],
107135
{ concurrency: "unbounded" },
108136
);
109-
if (local.refName === null || local.isDefaultRef) return;
110137
const thread = yield* projections.getThreadShell(threadId);
111-
if (!thread || thread.branch !== local.refName) return;
138+
if (!thread) return;
139+
// Forge discovery probes can take seconds; like the PR lookup, the next turn must not wait.
140+
yield* refreshMissingRepositoryIdentity(thread.projectId).pipe(
141+
FiberMap.run(identityRefreshes, thread.projectId),
142+
);
143+
if (local.refName === null || local.isDefaultRef) return;
144+
if (thread.branch !== local.refName) return;
112145
if (thread.activeRunId !== null && thread.activeRunId !== runId) return;
113146
// The forge CLI can take minutes, so the next turn must not wait for it. A newer
114147
// turn's lookup replaces a running one instead of queueing behind the cwd's lock.

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ const layerSharedApplicationDataPlaneTest = Layer.mergeAll(
999999
repositoryIdentityResolved: false,
10001000
}),
10011001
invalidate: () => Effect.void,
1002+
refreshRepositoryIdentity: () => Effect.void,
10021003
subscribeChanges: Effect.never,
10031004
}),
10041005
),

‎apps/server/src/project/ProjectEnrichmentService.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export class ProjectEnrichmentService extends Context.Service<
5151
workspaceRoot: string,
5252
) => Effect.Effect<RepositoryIdentity | null>;
5353
readonly invalidate: (workspaceRoots: Iterable<string>) => Effect.Effect<void>;
54+
/** Re-resolves past both identity caches and publishes a found identity, leaving the favicon alone. */
55+
readonly refreshRepositoryIdentity: (workspaceRoot: string) => Effect.Effect<void>;
5456
readonly subscribeChanges: Effect.Effect<
5557
PubSub.Subscription<ProjectEnrichmentChange>,
5658
never,
@@ -340,12 +342,24 @@ export const make = Effect.fn("ProjectEnrichmentService.make")(function* (
340342
);
341343
});
342344

345+
const refreshRepositoryIdentity: ProjectEnrichmentService["Service"]["refreshRepositoryIdentity"] =
346+
Effect.fn("ProjectEnrichmentService.refreshRepositoryIdentity")(function* (workspaceRoot) {
347+
// Remote-less folders hit this after every turn; publishing their unchanged null would
348+
// push a shell frame to every subscriber.
349+
if ((yield* repositoryIdentityResolver.resolve(workspaceRoot, { refresh: true })) === null) {
350+
return;
351+
}
352+
yield* Cache.invalidate(repositoryIdentityCache, workspaceRoot);
353+
yield* resolveRepositoryIdentity(workspaceRoot);
354+
});
355+
343356
return ProjectEnrichmentService.of({
344357
peek,
345358
request,
346359
getAvailable,
347360
readRepositoryIdentity,
348361
invalidate,
362+
refreshRepositoryIdentity,
349363
subscribeChanges: PubSub.subscribe(changes),
350364
});
351365
});

0 commit comments

Comments
 (0)