fix(server): link thread PRs without an open client (#10101)

Co-authored-by: Claude Fable 5.1 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5.1 authored and GitHub committed 2026-09-06 04:06:15 -07:00
1 parent 5fa35d2116
commit 223ff4490f
50 files changed
+2308 -1485

No files matched your search

@@ -306,50 +306,65 @@ describe("applyThreadDetailEvent", () => {
}
});
it("sets and clears a linked pull request", () => {
const linkedPullRequest = {
projectId: ProjectId.make("project-1"),
repository: "pingdotgg/t3code",
number: 42,
url: "https://github.com/pingdotgg/t3code/pull/42",
};
const linked = applyThreadDetailEvent(baseThread, {
...baseEventFields,
sequence: 5,
occurredAt: "2026-04-01T05:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.meta-updated",
payload: {
threadId: ThreadId.make("thread-1"),
linkedPullRequest,
updatedAt: "2026-04-01T05:00:00.000Z",
},
});
it.each(["linkedPullRequest", "branchPullRequest"] as const)(
"sets and clears %s without changing the other link",
(field) => {
const linkedPullRequest = {
projectId: ProjectId.make("project-1"),
repository: "pingdotgg/t3code",
number: 42,
url: "https://github.com/pingdotgg/t3code/pull/42",
};
const otherField =
field === "linkedPullRequest" ? "branchPullRequest" : "linkedPullRequest";
const otherPullRequest = {
...linkedPullRequest,
number: 43,
url: "https://github.com/pingdotgg/t3code/pull/43",
};
const linked = applyThreadDetailEvent(
{ ...baseThread, [otherField]: otherPullRequest },
{
...baseEventFields,
sequence: 5,
occurredAt: "2026-04-01T05:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.meta-updated",
payload: {
threadId: ThreadId.make("thread-1"),
[field]: linkedPullRequest,
updatedAt: "2026-04-01T05:00:00.000Z",
},
},
);
expect(linked.kind).toBe("updated");
if (linked.kind !== "updated") return;
expect(linked.thread.linkedPullRequest).toEqual(linkedPullRequest);
expect(linked.kind).toBe("updated");
if (linked.kind !== "updated") return;
expect(linked.thread[field]).toEqual(linkedPullRequest);
expect(linked.thread[otherField]).toEqual(otherPullRequest);
const cleared = applyThreadDetailEvent(linked.thread, {
...baseEventFields,
sequence: 6,
occurredAt: "2026-04-01T06:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.meta-updated",
payload: {
threadId: ThreadId.make("thread-1"),
linkedPullRequest: null,
updatedAt: "2026-04-01T06:00:00.000Z",
},
});
const cleared = applyThreadDetailEvent(linked.thread, {
...baseEventFields,
sequence: 6,
occurredAt: "2026-04-01T06:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.meta-updated",
payload: {
threadId: ThreadId.make("thread-1"),
[field]: null,
updatedAt: "2026-04-01T06:00:00.000Z",
},
});
expect(cleared.kind).toBe("updated");
if (cleared.kind === "updated") {
expect(cleared.thread.linkedPullRequest).toBeNull();
}
});
expect(cleared.kind).toBe("updated");
if (cleared.kind === "updated") {
expect(cleared.thread[field]).toBeNull();
expect(cleared.thread[otherField]).toEqual(otherPullRequest);
}
},
);
});
describe("thread.message-sent", () => {
@@ -97,6 +97,7 @@ export function applyThreadDetailEvent(
interactionMode: event.payload.interactionMode,
branch: event.payload.branch,
worktreePath: event.payload.worktreePath,
branchPullRequest: null,
latestTurn: null,
createdAt: event.payload.createdAt,
updatedAt: event.payload.updatedAt,
@@ -240,6 +241,9 @@ export function applyThreadDetailEvent(
...(event.payload.linkedPullRequest !== undefined
? { linkedPullRequest: event.payload.linkedPullRequest }
: {}),
...(event.payload.branchPullRequest !== undefined
? { branchPullRequest: event.payload.branchPullRequest }
: {}),
updatedAt: event.payload.updatedAt,
},
};
@@ -2,6 +2,7 @@ import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Schema from "effect/Schema";
import { CommandId, ProjectId, ThreadId } from "./baseSchemas.ts";
import {
DEFAULT_PROVIDER_INTERACTION_MODE,
@@ -837,6 +838,47 @@ it.effect("accepts an internal title regeneration completion", () =>
}),
);
it.effect("accepts pull request synchronization only as an internal command", () =>
Effect.gen(function* () {
const pullRequest = {
projectId: ProjectId.make("project-1"),
repository: "pingdotgg/t3code",
number: 42,
url: "https://github.com/pingdotgg/t3code/pull/42",
};
const command = {
type: "thread.pull-request.sync" as const,
commandId: CommandId.make("cmd-pull-request-sync"),
threadId: ThreadId.make("thread-1"),
projectId: pullRequest.projectId,
snapshotSequence: 12,
expected: {
workspaceRoot: "/workspace/project",
branch: "feature",
worktreePath: null,
linkedPullRequest: null,
branchPullRequest: null,
},
branchPullRequest: pullRequest,
linkedPullRequest: pullRequest,
};
assert.deepStrictEqual(yield* decodeOrchestrationCommand(command), command);
assert.ok(yield* decodeClientOrchestrationCommand(command).pipe(Effect.flip));
const cleared = { ...command, branchPullRequest: null };
assert.deepStrictEqual(yield* decodeOrchestrationCommand(cleared), cleared);
const metadata = yield* decodeClientOrchestrationCommand({
type: "thread.meta.update",
commandId: "cmd-forged-branch-pull-request",
threadId: "thread-1",
branchPullRequest: pullRequest,
});
assert.isFalse("branchPullRequest" in metadata);
}),
);
it.effect("rejects an explicit title combined with title regeneration", () =>
Effect.gen(function* () {
const result = yield* Effect.exit(
+21
View File
@@ -494,6 +494,7 @@ export const OrchestrationThread = Schema.Struct({
branch: Schema.NullOr(TrimmedNonEmptyString),
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
linkedPullRequest: Schema.optional(Schema.NullOr(ThreadLinkedPullRequest)),
branchPullRequest: Schema.optional(Schema.NullOr(ThreadLinkedPullRequest)),
latestTurn: Schema.NullOr(OrchestrationLatestTurn),
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
@@ -572,6 +573,7 @@ export const OrchestrationThreadShell = Schema.Struct({
branch: Schema.NullOr(TrimmedNonEmptyString),
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
linkedPullRequest: Schema.optional(Schema.NullOr(ThreadLinkedPullRequest)),
branchPullRequest: Schema.optional(Schema.NullOr(ThreadLinkedPullRequest)),
latestTurn: Schema.NullOr(OrchestrationLatestTurn),
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
@@ -1183,6 +1185,23 @@ const ThreadTitleRegenerationCompleteCommand = Schema.Struct({
title: Schema.optional(TrimmedNonEmptyString),
});
const ThreadPullRequestSyncCommand = Schema.Struct({
type: Schema.Literal("thread.pull-request.sync"),
commandId: CommandId,
threadId: ThreadId,
projectId: ProjectId,
snapshotSequence: NonNegativeInt,
expected: Schema.Struct({
workspaceRoot: TrimmedNonEmptyString,
branch: Schema.NullOr(TrimmedNonEmptyString),
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
linkedPullRequest: Schema.NullOr(ThreadLinkedPullRequest),
branchPullRequest: Schema.NullOr(ThreadLinkedPullRequest),
}),
branchPullRequest: Schema.NullOr(ThreadLinkedPullRequest),
linkedPullRequest: Schema.optional(ThreadLinkedPullRequest),
});
const InternalOrchestrationCommand = Schema.Union([
ThreadAutoSettleCommand,
ThreadSessionSetCommand,
@@ -1194,6 +1213,7 @@ const InternalOrchestrationCommand = Schema.Union([
ThreadActivityAppendCommand,
ThreadRevertCompleteCommand,
ThreadTitleRegenerationCompleteCommand,
ThreadPullRequestSyncCommand,
]);
export type InternalOrchestrationCommand = typeof InternalOrchestrationCommand.Type;
@@ -1367,6 +1387,7 @@ export const ThreadMetaUpdatedPayload = Schema.Struct({
branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
linkedPullRequest: Schema.optional(Schema.NullOr(ThreadLinkedPullRequest)),
branchPullRequest: Schema.optional(Schema.NullOr(ThreadLinkedPullRequest)),
updatedAt: IsoDateTime,
});
+2 -17
View File
@@ -9,18 +9,16 @@ describe("resolveThreadReferenceCopyTarget", () => {
threadId: "thread-1",
openPanelPullRequestUrl: null,
linkedPullRequestUrl: "https://github.com/t3/pr/12",
detectedPullRequestUrl: "https://github.com/t3/pr/13",
}),
).toBeNull();
});
it("prefers the open panel pull request over linked and detected pull requests", () => {
it("prefers the open panel pull request over the thread pull request", () => {
expect(
resolveThreadReferenceCopyTarget({
threadId: "thread-1",
openPanelPullRequestUrl: "https://github.com/t3/pr/14",
linkedPullRequestUrl: "https://github.com/t3/pr/12",
detectedPullRequestUrl: "https://github.com/t3/pr/13",
}),
).toMatchObject({
kind: "pull-request",
@@ -29,12 +27,11 @@ describe("resolveThreadReferenceCopyTarget", () => {
});
});
it("prefers a durable linked pull request", () => {
it("uses the thread pull request when no panel is open", () => {
expect(
resolveThreadReferenceCopyTarget({
threadId: "thread-1",
linkedPullRequestUrl: "https://github.com/t3/pr/12",
detectedPullRequestUrl: "https://github.com/t3/pr/13",
}),
).toMatchObject({
kind: "pull-request",
@@ -43,18 +40,6 @@ describe("resolveThreadReferenceCopyTarget", () => {
});
});
it("uses a pull request detected from the active branch", () => {
expect(
resolveThreadReferenceCopyTarget({
threadId: "thread-1",
detectedPullRequestUrl: "https://github.com/t3/pr/13",
}),
).toMatchObject({
kind: "pull-request",
value: "https://github.com/t3/pr/13",
});
});
it("falls back to the thread ID", () => {
expect(resolveThreadReferenceCopyTarget({ threadId: "thread-1" })).toEqual({
kind: "thread",
+1 -3
View File
@@ -11,11 +11,9 @@ export function resolveThreadReferenceCopyTarget(input: {
/** Undefined means no PR panel; null means its URL is not available yet. */
readonly openPanelPullRequestUrl?: string | null | undefined;
readonly linkedPullRequestUrl?: string | null;
readonly detectedPullRequestUrl?: string | null;
}): ThreadReferenceCopyTarget | null {
if (input.openPanelPullRequestUrl === null) return null;
const pullRequestUrl =
input.openPanelPullRequestUrl ?? input.linkedPullRequestUrl ?? input.detectedPullRequestUrl;
const pullRequestUrl = input.openPanelPullRequestUrl ?? input.linkedPullRequestUrl;
return pullRequestUrl
? {
kind: "pull-request",