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
11 changes: 7 additions & 4 deletions apps/server/src/mcp/toolkits/pullRequests/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
PullRequestsToolkit,
type ThreadPullRequestEntry,
alreadyEndedWatchNext,
checksAtStartWatchNext,
CHILD_PULL_REQUEST_REPORT_NEXT,
} from "./tools.ts";

Expand Down Expand Up @@ -332,10 +333,12 @@ const make = Effect.gen(function* () {
return yield* watches
.setWatching({ threadId, target: input, watching, until: input.until })
.pipe(
Effect.map((result) =>
result.state === undefined
? result
: { ...result, next: alreadyEndedWatchNext(result.state) },
Effect.map(({ checksAtStart, ...result }) =>
result.state !== undefined
? { ...result, next: alreadyEndedWatchNext(result.state) }
: checksAtStart !== undefined
? { ...result, next: checksAtStartWatchNext(checksAtStart) }
: result,
),
Effect.catchTags({
PullRequestWatchThreadNotFoundError: (error) =>
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/mcp/toolkits/pullRequests/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ export type WatchPullRequestResult = typeof WatchPullRequestResult.Type;
export const alreadyEndedWatchNext = (state: "merged" | "closed") =>
`This PR is already ${state}, so there is nothing to watch. Report that and end your turn.`;

// The rollup can miss checks GitHub holds for approval, so the agent checks before it acts.
export const checksAtStartWatchNext = (checks: "passing" | "failing") =>
`The checks rollup already showed ${checks} when this watch started, and no wake will report that. Check the PR's readiness now and act on it. The watch still wakes you on later changes.`;

export const ThreadPullRequestEntry = Schema.Struct({
...PullRequestIdentity,
source: ThreadPullRequestLinkSource,
Expand Down
19 changes: 18 additions & 1 deletion apps/server/src/orchestration-v2/pullRequestWatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe("evaluatePullRequestWatch", () => {
]);
});

it("reports failures on the first read only when CI was pending at the start", () => {
it("reports failures and passes 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 }),
Expand All @@ -221,6 +221,23 @@ describe("evaluatePullRequestWatch", () => {
).changes,
[],
);
const green = detail({ checks: [check("lint", "success"), check("test", "success")] });
assert.deepEqual(
evaluatePullRequestWatch(
watch({ awaitingBaseline: "all", checksPending: true }),
green,
noRemarks,
).changes,
[{ kind: "checks-passed", count: 2, required: false }],
);
assert.deepEqual(
evaluatePullRequestWatch(
watch({ awaitingBaseline: "all", checksPending: false }),
green,
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 };
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/orchestration-v2/pullRequestWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export function evaluatePullRequestWatch(
checks.every((check) => check.status !== "pending");
owedChecks = owed;
if (passedNow && (!passed || gateGrew)) {
if (!checksBaseline) {
// Like a failure, a pass the first read finds is news when CI was pending at the start.
if (!checksBaseline || watch.checksPending === true) {
changes.push({ kind: "checks-passed", count: gate.length, required: required.length > 0 });
}
owedChecks =
Expand Down
34 changes: 31 additions & 3 deletions apps/server/src/pullRequest/PullRequestWatchService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,12 @@ 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* () {
const cases: ReadonlyArray<readonly [HarnessOptions, boolean | undefined]> = [
const cases: ReadonlyArray<
readonly [HarnessOptions, boolean | undefined, ("passing" | "failing")?]
> = [
[{ readChecksState: "pending" }, true],
[{ readChecksState: "failing" }, false],
[{ readChecksState: "failing" }, false, "failing"],
[{ readChecksState: "passing" }, false, "passing"],
[{}, false],
[
{
Expand All @@ -182,12 +185,13 @@ describe("PullRequestWatchService", () => {
undefined,
],
];
for (const [options, checksPending] of cases) {
for (const [options, checksPending, checksAtStart] 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(result.checksAtStart).toBe(checksAtStart);
const commands = yield* Ref.get(harness.commands);
expect(commands).toMatchObject([
{
Expand All @@ -202,6 +206,30 @@ describe("PullRequestWatchService", () => {
checksPending,
);
}

const rewatched = yield* makeHarness({
readChecksState: "passing",
thread: makeThread([
makeLink(9, {
endedWatch: {
startedAt: "2026-08-20T00:00:00.000Z",
headSha: "aaaaaaaaaa",
failedChecks: [],
passed: false,
passedChecks: [],
conflicting: false,
checksPending: true,
remarksThrough: "2026-08-20T00:00:00.000Z",
remarkIds: [],
wakes: 0,
},
}),
]),
});
const again = yield* rewatched.setWatching(true, {
url: "https://github.com/t3tools/t3code/pull/9",
});
expect(again.checksAtStart).toBeUndefined();
}),
);

Expand Down
11 changes: 11 additions & 0 deletions apps/server/src/pullRequest/PullRequestWatchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export class PullRequestWatchService extends Context.Service<
readonly wasWatching: boolean;
/** Set when the PR already ended, so no watch was registered. */
readonly state?: "merged" | "closed";
/** A first watch's finished rollup, which its baseline never reports. */
readonly checksAtStart?: "passing" | "failing";
},
| PullRequestWatchFailedError
| PullRequestWatchFromSubagentError
Expand Down Expand Up @@ -130,6 +132,7 @@ const make = Effect.gen(function* () {
if (watching && before?.snapshot?.state === "merged") return alreadyEnded("merged");
// A pending rollup has no failure yet, so any failure the first read finds is news.
let checksPendingAtStart: boolean | undefined;
let checksAtStart: "passing" | "failing" | undefined;
if (watching && !wasWatching) {
const read = yield* pullRequests
.summary(
Expand All @@ -151,6 +154,13 @@ const make = Effect.gen(function* () {
);
if (read?.state === "merged" || read?.state === "closed") return alreadyEnded(read.state);
checksPendingAtStart = read === undefined ? undefined : read.checksState === "pending";
// A watch again keeps its last watch's news, so it reports an untold result itself.
const watchedBefore = threadPullRequestsOf(thread).some(
(link) => threadPullRequestKeysEqual(link, target) && link.endedWatch !== undefined,
);
if (!watchedBefore && (read?.checksState === "passing" || read?.checksState === "failing")) {
checksAtStart = read.checksState;
}
}
const uuid = yield* random.uuidV4().pipe(Effect.orDie);
yield* engine
Expand Down Expand Up @@ -180,6 +190,7 @@ const make = Effect.gen(function* () {
...target,
watching: watchedLink(after)?.watch !== undefined,
wasWatching,
...(checksAtStart !== undefined && { checksAtStart }),
};
});

Expand Down
Loading