Skip to content

Commit 2a5b348

Browse files
fix(server): post-turn identity refresh runs in the background, only for missing identities
The gate now reads through the enrichment cache instead of peeking it, so a known identity past its 1-minute TTL no longer forces a resolver refresh. The refresh runs in a per-project FiberMap so forge probes cannot delay the next queued turn, and a forced resolve that still finds no remote publishes nothing. The thread shell is read after the workspace and VCS refreshes again, so its failure cannot interrupt them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bc55321 commit 2a5b348

3 files changed

Lines changed: 164 additions & 22 deletions

File tree

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

Lines changed: 147 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ThreadId,
88
type OrchestrationV2ThreadShell,
99
type Project,
10+
type RepositoryIdentity,
1011
} from "@t3tools/contracts";
1112
import * as Deferred from "effect/Deferred";
1213
import * as Effect from "effect/Effect";
@@ -335,16 +336,156 @@ it.effect("resolves the identity of a project whose remote was added during the
335336
const observer = yield* RunFinalization.RunFinalizationObserver;
336337
yield* observer.refresh({ cwd: workspaceRoot, threadId, runId });
337338

338-
let available = yield* enrichment.getAvailable(workspaceRoot);
339-
while (!available.repositoryIdentityResolved) {
340-
yield* PubSub.take(changes);
341-
available = yield* enrichment.getAvailable(workspaceRoot);
342-
}
339+
// The refresh runs in the background; until it publishes, the cached null still reads as resolved.
340+
const change = yield* PubSub.take(changes);
343341
assert.strictEqual(
344-
available.repositoryIdentity?.locator.remoteUrl,
342+
change.enrichment.repositoryIdentity?.locator.remoteUrl,
345343
"https://github.com/t3-triage/example-29929.git",
346344
"the project's repository identity is still null after the turn added a remote",
347345
);
348346
}).pipe(Effect.scoped, Effect.provide(layer));
349347
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
350348
);
349+
350+
const stubbedIdentityProject = {
351+
projectId: ProjectId.make("project-stubbed-identity"),
352+
threadId: ThreadId.make("thread-stubbed-identity"),
353+
runId: RunId.make("completed-run"),
354+
workspaceRoot: "/repo-stubbed-identity",
355+
};
356+
const stubbedIdentity: RepositoryIdentity = {
357+
canonicalKey: "example.test/stubbed-identity",
358+
locator: {
359+
source: "git-remote",
360+
remoteName: "origin",
361+
remoteUrl: "https://example.test/stubbed-identity.git",
362+
},
363+
};
364+
365+
const layerStubbedIdentityResolver = (
366+
resolve: RepositoryIdentityResolver.RepositoryIdentityResolver["Service"]["resolve"],
367+
) =>
368+
RunFinalization.layerObserver.pipe(
369+
Layer.provideMerge(
370+
Layer.mergeAll(
371+
Layer.effect(ProjectEnrichment.ProjectEnrichmentService, ProjectEnrichment.make()).pipe(
372+
Layer.provide(
373+
Layer.mergeAll(
374+
Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, { resolve }),
375+
Layer.succeed(ProjectFaviconResolver.ProjectFaviconResolver, {
376+
invalidate: () => Effect.void,
377+
resolvePath: () => Effect.succeed(null),
378+
}),
379+
),
380+
),
381+
),
382+
Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
383+
Layer.mock(PullRequestService.PullRequestService)({ refreshAfterTurn: () => Effect.void }),
384+
Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
385+
refreshLocalStatus: () =>
386+
Effect.succeed({
387+
isRepo: true,
388+
hasPrimaryRemote: true,
389+
isDefaultRef: true,
390+
refName: "main",
391+
hasWorkingTreeChanges: false,
392+
workingTree: { files: [], insertions: 0, deletions: 0 },
393+
}),
394+
}),
395+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
396+
getThreadShell: () =>
397+
Effect.succeed({
398+
id: stubbedIdentityProject.threadId,
399+
projectId: stubbedIdentityProject.projectId,
400+
branch: "main",
401+
activeRunId: null,
402+
} as OrchestrationV2ThreadShell),
403+
}),
404+
Layer.mock(ProjectService.ProjectService)({
405+
getById: () =>
406+
Effect.succeedSome({
407+
id: stubbedIdentityProject.projectId,
408+
workspaceRoot: stubbedIdentityProject.workspaceRoot,
409+
} as Project),
410+
}),
411+
),
412+
),
413+
);
414+
415+
it.effect(
416+
"keeps a known identity's resolver caches when a turn outlasts the enrichment TTL",
417+
() => {
418+
const { workspaceRoot, threadId, runId } = stubbedIdentityProject;
419+
const refreshFlags: boolean[] = [];
420+
const resolvedAfterTurn = Deferred.makeUnsafe<void>();
421+
return Effect.gen(function* () {
422+
const enrichment = yield* ProjectEnrichment.ProjectEnrichmentService;
423+
assert.isNotNull(yield* enrichment.readRepositoryIdentity(workspaceRoot));
424+
yield* TestClock.adjust("61 seconds");
425+
const observer = yield* RunFinalization.RunFinalizationObserver;
426+
yield* observer.refresh({ cwd: workspaceRoot, threadId, runId });
427+
yield* Deferred.await(resolvedAfterTurn);
428+
assert.deepEqual(
429+
refreshFlags,
430+
[false, false],
431+
"a project with a known identity had its resolver caches forced after the turn",
432+
);
433+
}).pipe(
434+
Effect.provide(
435+
layerStubbedIdentityResolver((_cwd, options) =>
436+
Effect.sync(() => {
437+
refreshFlags.push(options?.refresh === true);
438+
return stubbedIdentity;
439+
}).pipe(
440+
Effect.tap(() =>
441+
refreshFlags.length > 1
442+
? Deferred.succeed(resolvedAfterTurn, undefined)
443+
: Effect.void,
444+
),
445+
),
446+
),
447+
),
448+
);
449+
},
450+
);
451+
452+
it.effect("returns from the post-turn refresh while the identity resolver is still running", () => {
453+
const { workspaceRoot, threadId, runId } = stubbedIdentityProject;
454+
const releaseResolver = Deferred.makeUnsafe<void>();
455+
let remoteAdded = false;
456+
return Effect.gen(function* () {
457+
const enrichment = yield* ProjectEnrichment.ProjectEnrichmentService;
458+
assert.isNull(yield* enrichment.readRepositoryIdentity(workspaceRoot));
459+
const changes = yield* enrichment.subscribeChanges;
460+
const observer = yield* RunFinalization.RunFinalizationObserver;
461+
const refresh = yield* observer
462+
.refresh({ cwd: workspaceRoot, threadId, runId })
463+
.pipe(Effect.timeoutOption("1 minute"), Effect.forkChild({ startImmediately: true }));
464+
yield* TestClock.adjust("1 minute");
465+
assert.isTrue(
466+
Option.isSome(yield* Fiber.join(refresh)),
467+
"turn finalization waited on the identity resolver, blocking the thread's next turn",
468+
);
469+
470+
yield* Deferred.succeed(releaseResolver, undefined);
471+
const change = yield* PubSub.take(changes);
472+
assert.strictEqual(
473+
change.enrichment.repositoryIdentity?.canonicalKey,
474+
stubbedIdentity.canonicalKey,
475+
);
476+
}).pipe(
477+
Effect.scoped,
478+
Effect.provide(
479+
layerStubbedIdentityResolver((_cwd, options) =>
480+
options?.refresh
481+
? Deferred.await(releaseResolver).pipe(
482+
Effect.map(() => {
483+
remoteAdded = true;
484+
return stubbedIdentity;
485+
}),
486+
)
487+
: Effect.sync(() => (remoteAdded ? stubbedIdentity : null)),
488+
),
489+
),
490+
);
491+
});

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export const layerObserver = Layer.effect(
103103
const projects = yield* ProjectService.ProjectService;
104104
const enrichment = yield* ProjectEnrichment.ProjectEnrichmentService;
105105
const pullRequestLookups = yield* FiberMap.make<string>();
106+
const identityRefreshes = yield* FiberMap.make<ProjectId>();
106107
// A turn can add the first remote (`git remote add`, `gh repo create --push`), which the
107108
// enrichment and resolver caches would otherwise keep reporting as missing for a minute.
108109
const refreshMissingRepositoryIdentity = Effect.fn(
@@ -112,7 +113,7 @@ export const layerObserver = Layer.effect(
112113
const project = yield* projects.getById(projectId);
113114
if (Option.isNone(project)) return;
114115
const { workspaceRoot } = project.value;
115-
if ((yield* enrichment.peek(workspaceRoot)).repositoryIdentity !== null) return;
116+
if ((yield* enrichment.readRepositoryIdentity(workspaceRoot)) !== null) return;
116117
yield* enrichment.refreshRepositoryIdentity(workspaceRoot);
117118
},
118119
(effect, projectId) =>
@@ -129,22 +130,18 @@ export const layerObserver = Layer.effect(
129130
refreshAfterTurn: pullRequests.refreshAfterTurn,
130131
refresh: Effect.fn("RunFinalizationService.refresh")(
131132
function* ({ cwd, threadId, runId }) {
132-
const [, local, thread] = yield* Effect.all(
133-
[
134-
workspaceEntries.refresh(cwd),
135-
vcsStatus.refreshLocalStatus(cwd),
136-
projections
137-
.getThreadShell(threadId)
138-
.pipe(
139-
Effect.tap((thread) =>
140-
thread ? refreshMissingRepositoryIdentity(thread.projectId) : Effect.void,
141-
),
142-
),
143-
],
133+
const [, local] = yield* Effect.all(
134+
[workspaceEntries.refresh(cwd), vcsStatus.refreshLocalStatus(cwd)],
144135
{ concurrency: "unbounded" },
145136
);
137+
const thread = yield* projections.getThreadShell(threadId);
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+
);
146143
if (local.refName === null || local.isDefaultRef) return;
147-
if (!thread || thread.branch !== local.refName) return;
144+
if (thread.branch !== local.refName) return;
148145
if (thread.activeRunId !== null && thread.activeRunId !== runId) return;
149146
// The forge CLI can take minutes, so the next turn must not wait for it. A newer
150147
// turn's lookup replaces a running one instead of queueing behind the cwd's lock.

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ 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 the result, leaving the favicon alone. */
54+
/** Re-resolves past both identity caches and publishes a found identity, leaving the favicon alone. */
5555
readonly refreshRepositoryIdentity: (workspaceRoot: string) => Effect.Effect<void>;
5656
readonly subscribeChanges: Effect.Effect<
5757
PubSub.Subscription<ProjectEnrichmentChange>,
@@ -301,7 +301,11 @@ export const make = Effect.fn("ProjectEnrichmentService.make")(function* (
301301

302302
const refreshRepositoryIdentity: ProjectEnrichmentService["Service"]["refreshRepositoryIdentity"] =
303303
Effect.fn("ProjectEnrichmentService.refreshRepositoryIdentity")(function* (workspaceRoot) {
304-
yield* repositoryIdentityResolver.resolve(workspaceRoot, { refresh: true });
304+
// Remote-less folders hit this after every turn; publishing their unchanged null would
305+
// push a shell frame to every subscriber.
306+
if ((yield* repositoryIdentityResolver.resolve(workspaceRoot, { refresh: true })) === null) {
307+
return;
308+
}
305309
yield* Cache.invalidate(repositoryIdentityCache, workspaceRoot);
306310
yield* resolveRepositoryIdentity(workspaceRoot);
307311
});

0 commit comments

Comments
 (0)