Skip to content

Commit 4d0bfdc

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 2495db8 commit 4d0bfdc

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
@@ -388,6 +388,51 @@ describe("ThreadPullRequestReactor", () => {
388388
),
389389
);
390390

391+
it.effect("refreshes the project identity when a turn adds the remote", () =>
392+
Effect.scoped(
393+
Effect.gen(function* () {
394+
const current = thread("new-remote");
395+
const fixture = yield* makeHarness({
396+
threads: [current],
397+
project: { ...project, repositoryIdentity: null },
398+
branchPullRequest: () => Effect.succeed(branchPullRequest()),
399+
resolveRepositoryIdentity: (_cwd, options) =>
400+
Effect.succeed(options?.refresh ? project.repositoryIdentity : null),
401+
});
402+
yield* Effect.gen(function* () {
403+
const reactor = yield* fixture.start();
404+
expect(yield* Ref.get(fixture.commands)).toHaveLength(0);
405+
406+
yield* fixture.publish({
407+
type: "thread.turn-diff-completed",
408+
sequence: 2,
409+
eventId: EventId.make("checkpoint-finished"),
410+
aggregateKind: "thread",
411+
aggregateId: current.id,
412+
occurredAt: NOW,
413+
commandId: null,
414+
causationEventId: null,
415+
correlationId: null,
416+
metadata: {},
417+
payload: {
418+
threadId: current.id,
419+
turnId: TurnId.make("turn"),
420+
checkpointTurnCount: 1,
421+
checkpointRef: CheckpointRef.make("checkpoint"),
422+
status: "ready",
423+
files: [],
424+
assistantMessageId: null,
425+
completedAt: NOW,
426+
},
427+
});
428+
yield* Queue.take(fixture.reads);
429+
yield* reactor.drain;
430+
expect((yield* Ref.get(fixture.commands))[0]?.branchPullRequest).toEqual(reference(42));
431+
}).pipe(Effect.provide(fixture.layer));
432+
}),
433+
),
434+
);
435+
391436
it.effect("uses live worktrees and falls back to the project for removed worktrees", () =>
392437
Effect.scoped(
393438
Effect.gen(function* () {

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,19 @@ export const make = Effect.gen(function* () {
131131
(group) =>
132132
Effect.gen(function* () {
133133
const first = group[0]!;
134-
const project = projects.get(first.projectId);
135-
if (project === undefined) return finishBackfill(group);
134+
const snapshotProject = projects.get(first.projectId);
135+
if (snapshotProject === undefined) return finishBackfill(group);
136+
// A finished turn may have added the remote this PR lives on. A failed
137+
// refresh resolves to null, so keep the snapshot's identity then.
138+
const project = request.refresh
139+
? {
140+
...snapshotProject,
141+
repositoryIdentity:
142+
(yield* repositoryIdentities.resolve(snapshotProject.workspaceRoot, {
143+
refresh: true,
144+
})) ?? snapshotProject.repositoryIdentity,
145+
}
146+
: snapshotProject;
136147
const repository = sourceControlRepositorySelector(project.repositoryIdentity);
137148
if (first.branch !== null && repository === null) return finishBackfill(group);
138149
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)