Skip to content

Commit 575b3af

Browse files
t3dotggclaude
andcommitted
fix(server): refresh the project identity when a turn ends
With a 15-minute identity TTL, an agent that adds or changes a remote during a turn could leave its PR unlinked for up to 15 minutes. Turn-end PR discovery now resolves the project identity with refresh: true and keeps the snapshot identity if the refresh finds nothing. Ported from #13722. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 64ac741 commit 575b3af

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

‎apps/server/src/orchestration/ThreadPullRequestReactor.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,51 @@ describe("ThreadPullRequestReactor", () => {
406406
),
407407
);
408408

409+
it.effect("refreshes the project identity when a turn adds the remote", () =>
410+
Effect.scoped(
411+
Effect.gen(function* () {
412+
const current = thread("new-remote");
413+
const fixture = yield* makeHarness({
414+
threads: [current],
415+
project: { ...project, repositoryIdentity: null },
416+
branchPullRequest: () => Effect.succeed(branchPullRequest()),
417+
resolveRepositoryIdentity: (_cwd, options) =>
418+
Effect.succeed(options?.refresh ? project.repositoryIdentity : null),
419+
});
420+
yield* Effect.gen(function* () {
421+
const reactor = yield* fixture.start();
422+
expect(yield* Ref.get(fixture.commands)).toHaveLength(0);
423+
424+
yield* fixture.publish({
425+
type: "thread.turn-diff-completed",
426+
sequence: 2,
427+
eventId: EventId.make("checkpoint-finished"),
428+
aggregateKind: "thread",
429+
aggregateId: current.id,
430+
occurredAt: NOW,
431+
commandId: null,
432+
causationEventId: null,
433+
correlationId: null,
434+
metadata: {},
435+
payload: {
436+
threadId: current.id,
437+
turnId: TurnId.make("turn"),
438+
checkpointTurnCount: 1,
439+
checkpointRef: CheckpointRef.make("checkpoint"),
440+
status: "ready",
441+
files: [],
442+
assistantMessageId: null,
443+
completedAt: NOW,
444+
},
445+
});
446+
yield* Queue.take(fixture.reads);
447+
yield* reactor.drain;
448+
expect((yield* Ref.get(fixture.commands))[0]?.branchPullRequest).toEqual(reference(42));
449+
}).pipe(Effect.provide(fixture.layer));
450+
}),
451+
),
452+
);
453+
409454
it.effect("uses live worktrees and falls back to the project for removed worktrees", () =>
410455
Effect.scoped(
411456
Effect.gen(function* () {

‎apps/server/src/orchestration/ThreadPullRequestReactor.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,19 @@ export const make = Effect.gen(function* () {
164164
(group) =>
165165
Effect.gen(function* () {
166166
const first = group[0]!;
167-
const project = projects.get(first.projectId);
168-
if (project === undefined) return finishBackfill(group);
167+
const snapshotProject = projects.get(first.projectId);
168+
if (snapshotProject === undefined) return finishBackfill(group);
169+
// A finished turn may have added the remote this PR lives on. A failed
170+
// refresh resolves to null, so keep the snapshot's identity then.
171+
const project = request.refresh
172+
? {
173+
...snapshotProject,
174+
repositoryIdentity:
175+
(yield* repositoryIdentities.resolve(snapshotProject.workspaceRoot, {
176+
refresh: true,
177+
})) ?? snapshotProject.repositoryIdentity,
178+
}
179+
: snapshotProject;
169180
const repository = sourceControlRepositorySelector(project.repositoryIdentity);
170181
if (first.branch !== null && repository === null) return finishBackfill(group);
171182
const worktreeExists =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import * as ProcessRunner from "../processRunner.ts";
1414

1515
const DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY = 512;
1616
// Background sweeps resolve every project each minute. A long TTL keeps them
17-
// from spawning git each time. Clone, publish, and PR discovery (before it
18-
// saves links) resolve with `refresh: true`.
17+
// from spawning git each time. Clone, publish, and PR discovery (after a turn
18+
// and before it saves links) resolve with `refresh: true`.
1919
const DEFAULT_POSITIVE_CACHE_TTL = Duration.minutes(15);
2020
// Short, so a folder that gains a repository or a remote shows up quickly.
2121
const DEFAULT_NEGATIVE_CACHE_TTL = Duration.minutes(1);

0 commit comments

Comments
 (0)