Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions apps/server/src/orchestration-v2/PullRequestWatchReactor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { assert, describe, it } from "@effect/vitest";
import * as Crypto from "effect/Crypto";
import * as Effect from "effect/Effect";

import * as Orchestrator from "./Orchestrator.ts";
import * as ProjectionStore from "./ProjectionStore.ts";
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
import * as PullRequestWatchReactor from "./PullRequestWatchReactor.ts";

const STARTED = "2026-10-02T12:00:00.000Z";

const testCrypto = Crypto.make({
randomBytes: (size) => new Uint8Array(size).fill(1),
digest: (_algorithm, data) => Effect.succeed(data),
});

describe("PullRequestWatchReactor", () => {
it.effect("asks the host again before reading when there is no fingerprint to compare", () =>
Effect.gen(function* () {
const calls: Array<string> = [];
const link = {
host: "github.com",
repository: "o/r",
number: 1,
url: "https://github.com/o/r/pull/1",
snapshot: null,
watch: {
startedAt: STARTED,
headSha: "aaaaaaaaaa",
failedChecks: [],
passed: false,
passedChecks: [],
remarksThrough: STARTED,
remarkIds: [],
conflicting: false,
wakes: 0,
},
};
const thread = {
id: "thread-1",
projectId: "project-1",
lineage: { rootThreadId: "thread-1", parentThreadId: null, relationshipToParent: null },
settledOverride: null,
settledAt: null,
pullRequests: [link],
};
const reactor = yield* PullRequestWatchReactor.make.pipe(
Effect.provideService(Orchestrator.OrchestratorV2, {
dispatch: () => Effect.void,
} as never),
Effect.provideService(ProjectionStore.ProjectionStoreV2, {
getThreadsWithPullRequests: () => Effect.succeed([thread]),
} as never),
Effect.provideService(PullRequestService.PullRequestService, {
watchFingerprint: () => Effect.succeed(null),
invalidate: () => Effect.sync(() => void calls.push("invalidate")),
detail: () =>
Effect.sync(() => {
calls.push("detail");
return {
state: "open",
headSha: "aaaaaaaaaa",
baseBranch: "main",
checks: [],
mergeability: "mergeable",
capabilities: { checksAlwaysReport: false },
};
}),
activity: () =>
Effect.succeed({ comments: [], reviewThreads: [], commentsTruncated: false }),
} as never),
Effect.provideService(Crypto.Crypto, testCrypto),
);
yield* reactor.sweep;
assert.deepEqual(calls, ["invalidate", "detail"]);
}),
);
});
4 changes: 2 additions & 2 deletions apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,8 @@ export const make = Effect.gen(function* () {
threadTails.delete(group.key);
}
const reference = referenceOf(group);
// Invalidate cached reads before using the newer fingerprint; clients receive no notification.
if (fingerprint !== null) yield* pullRequests.invalidate({ reference });
// A cached read can predate a push, and a viewer's read fills the cache too.
yield* pullRequests.invalidate({ reference });
const read = yield* Effect.exit(
Effect.all(
[
Expand Down
48 changes: 48 additions & 0 deletions apps/server/src/orchestration-v2/pullRequestWatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,35 @@ describe("evaluatePullRequestWatch", () => {
]);
});

it("reports a conflict on a new head even if the replaced head conflicted too", () => {
const first = evaluatePullRequestWatch(
watch(),
detail({ mergeability: "conflicting" }),
noRemarks,
);
const pushed = evaluatePullRequestWatch(
first.next,
detail({ headSha: "bbbbbbbbbb", mergeability: "unknown" }),
noRemarks,
);
assert.deepEqual(
evaluatePullRequestWatch(
pushed.next,
detail({ headSha: "bbbbbbbbbb", mergeability: "conflicting" }),
noRemarks,
).changes,
[{ kind: "conflicting" }],
);
assert.deepEqual(
evaluatePullRequestWatch(
first.next,
detail({ headSha: "bbbbbbbbbb", mergeability: "conflicting" }),
noRemarks,
).changes,
[{ kind: "conflicting" }],
);
});

it("does not spend the comment wake limit on check results", () => {
const tired = watch({ headSha: "aaaaaaaaaa", wakes: PULL_REQUEST_WATCH_WAKE_LIMIT - 1 });
const result = evaluatePullRequestWatch(
Expand Down Expand Up @@ -420,4 +449,23 @@ describe("evaluatePullRequestWatch", () => {
assert.include(pullRequestWatchMessage({ ...input, instructions }).text, instructions);
assert.notInclude(pullRequestWatchMessage(input).text, "standing instructions");
});

it("names the head its findings are about and tells the agent to confirm it", () => {
const report = evaluatePullRequestWatch(
watch(),
detail({ mergeability: "conflicting" }),
noRemarks,
);
const input = {
number: 7,
url: "https://github.com/pingdotgg/t3code/pull/7",
baseBranch: "main",
report,
};
assert.include(
pullRequestWatchMessage({ ...input, headSha: "aaaaaaaaaa" }).text,
"These findings are about aaaaaaa. Before acting, confirm the pull request head is still that commit",
);
assert.notInclude(pullRequestWatchMessage({ ...input, headSha: null }).text, "head is still");
});
});
15 changes: 12 additions & 3 deletions apps/server/src/orchestration-v2/pullRequestWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@ export function evaluatePullRequestWatch(
...atLatest.map((remark) => remark.id),
];

if (detail.mergeability === "conflicting" && !watch.conflicting) {
if (detail.mergeability === "conflicting" && (!watch.conflicting || headMoved)) {
changes.push({ kind: "conflicting" });
}
// "unknown" is a host still computing after a push; only a clean answer clears a conflict.
// "unknown" is a host still computing; only a clean answer clears a conflict. A conflict seen on
// a replaced head says nothing about the new one, which must be reported if it conflicts too.
const conflicting =
detail.mergeability === "unknown" ? watch.conflicting : detail.mergeability === "conflicting";
detail.mergeability === "unknown"
? watch.conflicting && !headMoved
: detail.mergeability === "conflicting";
Comment thread
yordis marked this conversation as resolved.
Resolved

const commentsOnly = changes.length > 0 && changes.every((change) => change.kind === "remarks");
const progress = headMoved || (changes.length > 0 && !commentsOnly);
Expand Down Expand Up @@ -232,6 +235,12 @@ export function pullRequestWatchMessage(input: {
`Update on pull request #${input.number} (${input.url}), which T3 Code is watching for you:`,
...changes.flatMap((change) => changeLines(change, context)),
"",
...(input.headSha === null
? []
: [
`These findings are about ${input.headSha.slice(0, 7)}. Before acting, confirm the pull request head is still that commit, and ignore findings about a commit that is no longer the head.`,
"",
]),
...(input.instructions === undefined
? []
: [
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/orchestration-v2/runtimeLayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2984,6 +2984,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
Layer.mergeAll(
NodeServices.layer,
Layer.mock(PullRequestService.PullRequestService)({
invalidate: () => Effect.void,
detail: () => Effect.die("host unreachable"),
activity: () => Effect.die("host unreachable"),
}),
Expand Down Expand Up @@ -3045,6 +3046,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
const host = Layer.mergeAll(
NodeServices.layer,
Layer.mock(PullRequestService.PullRequestService)({
invalidate: () => Effect.void,
detail: () =>
Effect.sync(() => ({
...watchedPullRequestDetail({
Expand Down Expand Up @@ -3143,6 +3145,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
const host = Layer.mergeAll(
NodeServices.layer,
Layer.mock(PullRequestService.PullRequestService)({
invalidate: () => Effect.void,
detail: () =>
Effect.sync(() => ({
...watchedPullRequestDetail({
Expand Down Expand Up @@ -3287,6 +3290,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
const host = Layer.mergeAll(
NodeServices.layer,
Layer.mock(PullRequestService.PullRequestService)({
invalidate: () => Effect.void,
detail: () =>
Effect.sync(() => ({
...watchedPullRequestDetail({ projectId, number: 13, at: commentAt }),
Expand Down Expand Up @@ -3385,6 +3389,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
const host = Layer.mergeAll(
NodeServices.layer,
Layer.mock(PullRequestService.PullRequestService)({
invalidate: () => Effect.void,
detail: () =>
Effect.sync(() => ({
...watchedPullRequestDetail({
Expand Down Expand Up @@ -3488,6 +3493,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
const host = Layer.mergeAll(
NodeServices.layer,
Layer.mock(PullRequestService.PullRequestService)({
invalidate: () => Effect.void,
detail: () =>
Effect.sync(() => ({
...watchedPullRequestDetail({
Expand Down Expand Up @@ -3718,6 +3724,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
Layer.mergeAll(
NodeServices.layer,
Layer.mock(PullRequestService.PullRequestService)({
invalidate: () => Effect.void,
detail: () => Effect.succeed(detail),
activity: () =>
Effect.succeed({
Expand Down
Loading