Skip to content
Merged
10 changes: 8 additions & 2 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5254,8 +5254,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
...(existing.endedWatch?.approvalAnnounced !== undefined && {
approvalAnnounced: existing.endedWatch.approvalAnnounced,
}),
// A finished-checks answer may be an older head's; a fresh read decides.
...(existing.endedWatch?.checksPending === true && { checksPending: true }),
// CI pending with no failure yet at the start may fail before the first
// read. A caller's fresh read beats the link's last sync; a finished-checks
// answer kept from the last watch may be an older head's.
...((command.type === "thread.pull-request.watch" &&
command.checksPending !== undefined
? command.checksPending
: existing.snapshot?.checksState === "pending" ||
existing.endedWatch?.checksPending === true) && { checksPending: true }),
...(existing.endedWatch?.checksAwaitedSince !== undefined && {
checksAwaitedSince: existing.endedWatch.checksAwaitedSince,
}),
Expand Down
45 changes: 45 additions & 0 deletions apps/server/src/orchestration-v2/pullRequestWatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,51 @@ describe("evaluatePullRequestWatch", () => {
]);
});

it("reports failures on the first read only when CI was pending at the start", () => {
const red = detail({ checks: [check("lint", "failure"), check("test", "pending")] });
const pending = evaluatePullRequestWatch(
watch({ awaitingBaseline: "all", checksPending: true }),
red,
noRemarks,
);
assert.deepEqual(pending.changes, [
{ kind: "checks-failed", failed: [check("lint", "failure")] },
]);
assert.deepEqual(
evaluatePullRequestWatch(
pending.next,
detail({ checks: [check("lint", "failure"), check("test", "success")] }),
noRemarks,
).changes,
[],
);
assert.deepEqual(
evaluatePullRequestWatch(
watch({ awaitingBaseline: "all", checksPending: false }),
red,
noRemarks,
).changes,
[],
);

// A read the host could not finish keeps the baseline and must keep the seed with it.
const placeholder = { ...check("Workflow approval status", "pending"), placeholder: true };
const incomplete = evaluatePullRequestWatch(
watch({ awaitingBaseline: "all", checksPending: true }),
detail({ checks: [check("lint", "success"), placeholder] }),
noRemarks,
);
assert.deepEqual(incomplete.changes, []);
assert.deepEqual(
evaluatePullRequestWatch(
incomplete.next,
detail({ checks: [check("lint", "success"), check("gated", "failure")] }),
noRemarks,
).changes,
[{ kind: "checks-failed", failed: [check("gated", "failure")] }],
);
});

it("baselines checks already awaiting approval", () => {
const gated = detail({ checks: [{ ...check("CI", "action-required"), url: run(1) }] });
const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), gated, noRemarks);
Expand Down
9 changes: 7 additions & 2 deletions apps/server/src/orchestration-v2/pullRequestWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ export function evaluatePullRequestWatch(
const awaiting = checks.filter((check) => check.status === "action-required");
if (checks.length > 0) {
// A run awaiting approval waits on a person, like a review, so only running checks count.
checksPending = checks.some((check) => check.status === "pending");
// An unfinished baseline read keeps the seed below for the next one.
checksPending =
checks.some((check) => check.status === "pending") ||
(checksBaseline && unread && watch.checksPending === true);
const newlyFailed = failed.filter((check) => !failedChecks.includes(check.name));
if (!checksBaseline && newlyFailed.length > 0) {
// The baseline hides failures from before the watch, but CI running at the start can fail
// before the first read.
if (newlyFailed.length > 0 && (!checksBaseline || watch.checksPending === true)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
changes.push({ kind: "checks-failed", failed: newlyFailed });
}
if (!approvalAnnounced && awaiting.length > 0) {
Expand Down
175 changes: 175 additions & 0 deletions apps/server/src/orchestration-v2/runtimeLayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4278,6 +4278,33 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
assert.isUndefined(yield* watchOf);
const { messages } = yield* orchestrator.getThreadRecords(threadId, ["messages"]);
assert.deepEqual(messages, []);

// A client start sends no CI state, so the link's last sync seeds it.
yield* orchestrator.dispatch({
type: "thread.pull-request-link.sync",
commandId: CommandId.make("pr-watch-ci-running"),
threadId,
...key,
snapshot: {
state: "open",
title: "Watched pull request",
headBranch: "feature",
baseBranch: "main",
isDraft: false,
updatedAt: null,
syncedAt: "2026-10-02T12:30:00.000Z",
checksState: "pending",
},
stack: null,
});
yield* orchestrator.dispatch({
type: "thread.pull-request.watch",
commandId: CommandId.make("pr-watch-client-start"),
threadId,
...key,
watching: true,
});
assert.isTrue((yield* watchOf)?.checksPending);
}),
);

Expand Down Expand Up @@ -5219,6 +5246,154 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
}),
);

it.effect.each([
// Failures from before the watch stay baseline, even with checks still running.
{
label: "finished failing",
atStart: "failing",
reads: [
{ lint: "failure", test: "success" },
{ lint: "failure", test: "success" },
],
told: [[], []],
},
{
label: "failing and still running",
atStart: "failing",
reads: [
{ lint: "failure", test: "pending" },
{ lint: "failure", test: "failure" },
],
told: [[], ["test"]],
},
// Pending CI had no failure, so one the first read finds landed after the start.
{
label: "pending",
atStart: "pending",
reads: [
{ lint: "failure", test: "pending" },
{ lint: "failure", test: "failure" },
],
told: [["lint"], ["test"]],
},
] as const)(
"reports only failures after a watch that starts on $label CI",
({ label, atStart, reads, told }) =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
const threadId = ThreadId.make(`runtime-pull-request-watch-start-${label}`);
const projectId = ProjectId.make("pr-watch-start-project");
yield* seedProject({
projectId,
title: "Watch start",
workspaceRoot: "/workspace/watch-start",
defaultModelSelection: null,
createdAt: "2026-10-01T00:00:00.000Z",
});
yield* orchestrator.dispatch({
type: "thread.create",
createdBy: "user",
creationSource: "web",
commandId: CommandId.make(`pr-watch-start-create-${label}`),
threadId,
projectId,
title: "Watch start",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
});
const key = { host: "github.com", repository: "pingdotgg/t3code", number: 13 };
yield* orchestrator.dispatch({
type: "thread.pull-request.link",
commandId: CommandId.make(`pr-watch-start-link-${label}`),
threadId,
...key,
url: "https://github.com/pingdotgg/t3code/pull/13",
source: "manual",
});
yield* orchestrator.dispatch({
type: "thread.pull-request-link.sync",
commandId: CommandId.make(`pr-watch-start-sync-${label}`),
threadId,
...key,
snapshot: {
state: "open",
title: "Watched pull request",
headBranch: "feature",
baseBranch: "main",
isDraft: false,
updatedAt: null,
syncedAt: "2026-10-02T12:00:00.000Z",
checksState: atStart,
},
stack: null,
});
// A client start, which carries no CI state of its own.
yield* orchestrator.dispatch({
type: "thread.pull-request.watch",
commandId: CommandId.make(`pr-watch-start-watch-${label}`),
threadId,
...key,
watching: true,
});

let read = 0;
const host = Layer.mergeAll(
NodeServices.layer,
NodeCrypto.layer,
Layer.mock(PullRequestService.PullRequestService)({
detail: () =>
Effect.sync(() => ({
...watchedPullRequestDetail({
projectId,
number: 13,
at: "2026-10-02T12:00:00.000Z",
}),
headSha: "ad70160aaa",
checks: Object.entries(reads[read]!).map(([name, status]) => ({
name,
status,
description: null,
url: null,
})),
})),
activity: () =>
Effect.succeed({
comments: [],
commentCount: 0,
commentsTruncated: false,
reviewThreads: [],
commits: [],
}),
}),
);
const texts = Effect.map(
orchestrator.getThreadRecords(threadId, ["messages"]),
({ messages }) => messages.map((message) => message.text),
);
const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
for (const [index, names] of told.entries()) {
read = index;
const before = (yield* texts).length;
if (index > 0) yield* TestClock.adjust("10 minutes");
yield* reactor.sweep;
yield* worker.drain();
const wakes = (yield* texts).slice(before);
assert.equal(wakes.length, names.length === 0 ? 0 : 1);
for (const name of ["lint", "test"]) {
assert.equal(
wakes.some((text) => text.includes(` - ${name}`)),
names.includes(name as never),
`read ${index} told ${name}`,
);
}
}
}),
);

it.effect("baselines a new watch across restarts and reports later changes", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
Expand Down
35 changes: 25 additions & 10 deletions apps/server/src/pullRequest/PullRequestWatchService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ interface HarnessOptions {
readonly project?: OrchestrationProjectShell | null;
readonly readFailure?: PullRequestService.PullRequestError;
readonly readState?: PullRequestSummary["state"];
readonly readChecksState?: PullRequestSummary["checksState"];
}

const makeHarness = Effect.fn("makePullRequestWatchHarness")(function* (
Expand Down Expand Up @@ -144,6 +145,9 @@ const makeHarness = Effect.fn("makePullRequestWatchHarness")(function* (
title: "Pull request",
url: `https://github.com/${input.repository}/pull/${input.number}`,
state: options.readState ?? "open",
...(options.readChecksState !== undefined && {
checksState: options.readChecksState,
}),
headBranch: "feature",
baseBranch: "main",
updatedAt: "2026-08-20T00:00:00.000Z",
Expand All @@ -164,28 +168,39 @@ const makeHarness = Effect.fn("makePullRequestWatchHarness")(function* (
describe("PullRequestWatchService", () => {
it.effect("links an unlinked watch even while the host has a transient read failure", () =>
Effect.gen(function* () {
for (const options of [
{},
{
readFailure: new PullRequestOperationError({
operation: "summary",
detail: "The host is temporarily unavailable.",
}),
},
]) {
const cases: ReadonlyArray<readonly [HarnessOptions, boolean | undefined]> = [
[{ readChecksState: "pending" }, true],
[{ readChecksState: "failing" }, false],
[{}, false],
[
{
readFailure: new PullRequestOperationError({
operation: "summary",
detail: "The host is temporarily unavailable.",
}),
},
undefined,
],
];
for (const [options, checksPending] of cases) {
const harness = yield* makeHarness(options);
const result = yield* harness.setWatching(true, {
url: "https://github.com/t3tools/t3code/pull/9",
});
expect(result).toMatchObject({ number: 9, wasWatching: false });
expect(yield* Ref.get(harness.commands)).toMatchObject([
const commands = yield* Ref.get(harness.commands);
expect(commands).toMatchObject([
{
type: "thread.pull-request.watch",
number: 9,
watching: true,
link: { url: "https://github.com/t3tools/t3code/pull/9", source: "agent" },
},
]);
const [command] = commands;
expect(command?.type === "thread.pull-request.watch" ? command.checksPending : null).toBe(
checksPending,
);
}
}),
);
Expand Down
12 changes: 9 additions & 3 deletions apps/server/src/pullRequest/PullRequestWatchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ const make = Effect.gen(function* () {
if (watching && before?.snapshot?.state === "merged") {
return yield* new PullRequestNotOpenError({ state: "merged" });
}
// A pending rollup has no failure yet, so any failure the first read finds is news.
let checksPendingAtStart: boolean | undefined;
if (watching && before?.watch === undefined) {
yield* pullRequests
checksPendingAtStart = yield* pullRequests
.summary(
{
projectId: thread.projectId,
Expand All @@ -142,11 +144,12 @@ const make = Effect.gen(function* () {
Effect.flatMap((summary) =>
summary.state === "merged"
? Effect.fail(new PullRequestNotOpenError({ state: "merged" }))
: Effect.void,
: Effect.succeed(summary.checksState === "pending"),
),
Effect.catchTags({
// Host and transport errors can recover while the watch is active.
PullRequestOperationError: () => Effect.void,
// A failed read leaves the seed to the link's last sync.
PullRequestOperationError: () => Effect.undefined,
}),
);
}
Expand All @@ -162,6 +165,9 @@ const make = Effect.gen(function* () {
watching,
...(watching ? { link: { url: target.url, source: "agent" as const } } : {}),
...(watching && input.until !== undefined && { until: input.until }),
...(watching && checksPendingAtStart !== undefined
? { checksPending: checksPendingAtStart }
: {}),
})
.pipe(
Effect.catchCause((cause) =>
Expand Down
2 changes: 2 additions & 0 deletions packages/contracts/src/orchestrationV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3323,6 +3323,8 @@ export const OrchestrationV2Command = Schema.Union([
watching: Schema.Boolean,
/** With `watching`, sets how long the watch holds its thread; absent keeps the current one. */
until: Schema.optional(PullRequestWatchUntil),
/** Whether CI was pending with no failure yet when the watch started, so any failure is news. */
checksPending: Schema.optional(Schema.Boolean),
link: Schema.optional(
Schema.Struct({
url: TrimmedNonEmptyString,
Expand Down
Loading