fix(server): persist only changed pull request links (#19248)

This commit is contained in:
Julius Marminge authored and GitHub committed 2026-10-13 22:46:11 -07:00
1 parent d765d03cb7
commit 4d73e1d5cd
10 files changed
+186 -2

No files matched your search

@@ -3251,6 +3251,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
};
}
})();
if (
command.type === "thread.pull-request-link.sync" ||
command.type === "thread.pull-request-watch.sync"
) {
if (updatedThread === thread) return;
const link = threadPullRequestsOf(updatedThread).find((candidate) =>
threadPullRequestKeysEqual(candidate, normalizeThreadPullRequestKey(command)),
);
if (link === undefined) return;
yield* emit(
events,
command,
)({
type: "thread.pull-request-link-updated",
threadId: command.threadId,
providerInstanceId: updatedThread.providerInstanceId,
occurredAt: now,
payload: link,
});
return;
}
const eventType = (() => {
switch (command.type) {
case "thread.archive":
@@ -3282,9 +3303,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
return "thread.metadata-updated" as const;
case "thread.pull-request.link":
case "thread.pull-request.unlink":
case "thread.pull-request-link.sync":
case "thread.pull-request.watch":
case "thread.pull-request-watch.sync":
case "thread.pull-request.sync":
return "thread.pull-request-synced" as const;
case "thread.runtime-mode.set":
@@ -1,3 +1,4 @@
import { applyThreadPullRequestLinkUpdate } from "@t3tools/shared/threadPullRequests";
import { projectTurnItemForWire } from "./WireProjection.ts";
import * as Stream from "effect/Stream";
import { makeThreadFind, findProjectedThreadItems } from "./ThreadFind.ts";
@@ -718,6 +719,11 @@ export function applyToProjection(
};
switch (event.type) {
case "thread.pull-request-link-updated":
return {
...base,
thread: applyThreadPullRequestLinkUpdate(projection.thread, event.payload),
};
case "thread.created":
case "thread.archived":
case "thread.unarchived":
@@ -1823,6 +1829,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
const apply: ProjectionStoreV2Shape["apply"] = (event) =>
Effect.gen(function* () {
switch (event.type) {
case "thread.pull-request-link-updated": {
const thread = yield* getThread(event.threadId);
yield* apply({
...event,
type: "thread.pull-request-synced",
payload: applyThreadPullRequestLinkUpdate(thread, event.payload),
});
break;
}
case "thread.created":
case "thread.archived":
case "thread.unarchived":
@@ -440,6 +440,8 @@ export const make = Effect.gen(function* () {
yield* forkParked(
Stream.runForEach(events, (event) => {
switch (event.type) {
case "thread.pull-request-link-updated":
return event.payload.snapshot === null ? requestSync(event.payload) : Effect.void;
case "thread.pull-request-synced":
return Effect.forEach(
visibleThreadPullRequests(event.payload.pullRequests ?? []).filter(
@@ -606,6 +606,7 @@ export const make = Effect.gen(function* () {
switch (event.type) {
case "thread.settled":
return cleanUpSettledThread(event.threadId);
case "thread.pull-request-link-updated":
case "thread.pull-request-synced":
case "provider-session.detached":
return worker.enqueue(event.threadId);
@@ -4860,6 +4860,93 @@ it.layer(layerSharedApplicationDataPlaneTest)("visited projection", (it) => {
);
});
it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("compact PR events", (it) => {
it.effect("persists only the changed PR link and replays it without losing other links", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
const sql = yield* SqlClient.SqlClient;
const threadId = ThreadId.make("compact-pr-sync");
yield* orchestrator.dispatch({
type: "thread.create",
createdBy: "user",
creationSource: "web",
commandId: CommandId.make("compact-pr-create"),
threadId,
projectId: ProjectId.make("compact-pr-project"),
title: "PR sync",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
});
const key = { host: "github.com", repository: "pingdotgg/t3code" };
for (const number of [1, 2, 3]) {
yield* orchestrator.dispatch({
type: "thread.pull-request.link",
commandId: CommandId.make(`compact-pr-link-${number}`),
threadId,
...key,
number,
url: `https://github.com/pingdotgg/t3code/pull/${number}`,
source: "manual",
});
}
const before = yield* orchestrator.getThreadShell(threadId);
yield* orchestrator.dispatch({
type: "thread.pull-request-link.sync",
commandId: CommandId.make("compact-pr-sync"),
threadId,
...key,
number: 2,
snapshot: {
state: "open",
title: "Updated PR",
headBranch: "feature/pr",
baseBranch: "main",
isDraft: false,
updatedAt: null,
syncedAt: "2026-10-13T12:00:00.000Z",
},
stack: {
kind: "native",
id: "updated",
number: 2,
url: "https://github.com/pingdotgg/t3code/pull/2",
base: "main",
layers: [],
},
});
const rows = yield* sql<{ event_type: string; payload_json: string }>`
SELECT event_type, payload_json FROM orchestration_events
WHERE command_id = ${"compact-pr-sync"}
`;
assert.equal(rows.length, 1);
assert.equal(rows[0]?.event_type, "thread.pull-request-link-updated");
const payload = JSON.parse(rows[0]!.payload_json);
assert.equal(payload.number, 2);
assert.isUndefined(payload.pullRequests);
const after = yield* orchestrator.getThreadShell(threadId);
assert.equal(after?.pullRequests?.length, 3);
assert.deepEqual(after?.pullRequests?.[0], before?.pullRequests?.[0]);
assert.deepEqual(after?.pullRequests?.[2], before?.pullRequests?.[2]);
assert.equal(after?.pullRequests?.[1]?.stack?.id, "updated");
assert.isTrue((yield* maintenance.rebuild).valid);
assert.deepEqual(
(yield* orchestrator.getThreadShell(threadId))?.pullRequests,
after?.pullRequests,
);
yield* maintenance.compactEventStore;
assert.isTrue((yield* maintenance.rebuild).valid);
assert.deepEqual(
(yield* orchestrator.getThreadShell(threadId))?.pullRequests,
after?.pullRequests,
);
}),
);
});
it.layer(layerSharedApplicationDataPlaneTest)("shared application data plane", (it) => {
it.effect("orders retained project transactions and V2 thread transactions in one source", () =>
Effect.gen(function* () {
@@ -97,6 +97,7 @@ export function shouldPublishAgentAwarenessEvent(
case "thread.unarchived":
case "thread.deleted":
case "thread.metadata-updated":
case "thread.pull-request-link-updated":
case "thread.pull-request-synced":
case "thread.model-selection-updated":
case "thread.provider-switched":
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vite-plus/test";
import {
type ThreadPullRequestLink,
type OrchestrationV2DomainEvent,
type OrchestrationV2Run,
type OrchestrationV2ThreadProjection,
@@ -105,6 +106,34 @@ const emptyProjection = {
} as OrchestrationV2ThreadProjection;
describe("applyOrchestrationV2ProjectionEvent", () => {
it("applies a compact PR sync without replacing other links or thread metadata", () => {
const link = (number: number): ThreadPullRequestLink => ({
host: "github.com",
repository: "pingdotgg/t3code",
number,
url: `https://github.com/pingdotgg/t3code/pull/${number}`,
source: "manual",
linkedAt: DateTime.formatIso(now),
snapshot: null,
stack: null,
});
const links = [link(1), link(2)];
const projection = {
...emptyProjection,
thread: { ...emptyProjection.thread, pullRequests: links },
};
const updated = { ...links[1]!, source: "agent" as const };
const next = applyOrchestrationV2ProjectionEvent(projection, {
id: "compact-sync" as OrchestrationV2DomainEvent["id"],
type: "thread.pull-request-link-updated",
threadId,
occurredAt: now,
payload: updated,
});
expect(next?.thread).toEqual({ ...projection.thread, pullRequests: [links[0], updated] });
expect(next?.thread.pullRequests?.[0]).toBe(links[0]);
});
it("keeps live token usage when the terminal provider turn omits it", () => {
const providerTurnId = ProviderTurnId.make("provider-turn-reducer");
const running = {
@@ -1,3 +1,4 @@
import { applyThreadPullRequestLinkUpdate } from "@t3tools/shared/threadPullRequests";
import type {
OrchestrationV2DomainEvent,
OrchestrationV2Run,
@@ -201,6 +202,11 @@ export function applyOrchestrationV2ProjectionEvent(
const latestLocalTurnOrdinal = options?.latestLocalTurnOrdinal;
const base = { ...projection, updatedAt: event.occurredAt };
switch (event.type) {
case "thread.pull-request-link-updated":
return {
...base,
thread: applyThreadPullRequestLinkUpdate(projection.thread, event.payload),
};
case "thread.created":
case "thread.archived":
case "thread.unarchived":
+10
View File
@@ -1765,6 +1765,11 @@ export const OrchestrationV2DomainEvent = Schema.Union([
]),
payload: OrchestrationV2AppThread,
}),
Schema.Struct({
...OrchestrationV2EventBase.fields,
type: Schema.Literal("thread.pull-request-link-updated"),
payload: ThreadPullRequestLink,
}),
Schema.Struct({
...OrchestrationV2EventBase.fields,
type: Schema.Literal("run.created"),
@@ -2585,6 +2590,11 @@ export const OrchestrationV2DomainEventJson = Schema.Union([
]),
payload: OrchestrationV2AppThreadJson,
}),
Schema.Struct({
...OrchestrationV2JsonEventBaseFields,
type: Schema.Literal("thread.pull-request-link-updated"),
payload: ThreadPullRequestLink,
}),
Schema.Struct({
...OrchestrationV2JsonEventBaseFields,
type: Schema.Literal("run.created"),
+14
View File
@@ -1,4 +1,5 @@
import type {
OrchestrationV2AppThread,
RepositoryIdentity,
SourceControlProviderKind,
ThreadLinkedPullRequest,
@@ -354,3 +355,16 @@ export function threadPullRequestsOf(thread: {
},
];
}
/** Apply a single-link sync without copying unrelated PRs into the event history. */
export function applyThreadPullRequestLinkUpdate(
thread: OrchestrationV2AppThread,
updated: ThreadPullRequestLink,
): OrchestrationV2AppThread {
return {
...thread,
pullRequests: threadPullRequestsOf(thread).map((link) =>
threadPullRequestKeysEqual(link, updated) ? updated : link,
),
};
}