@devries-cameron20 / t3code Mirror · read-only / commits / da33a53d18
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

fix(server): wake when CI fails during the PR watch baseline read (#39116)

Co-authored-by: Tibor Casteleijn <tibbor.casteleijn@easytrip.eu> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Tibor committed Nov 19, 2026 at 16:18 UTC da33a53d1896848eca237c6722d1447132f54eb3
7 files changed +271 -17
apps/server/src/orchestration-v2/Orchestrator.ts
+8 -2
@@ -5291,8 +5291,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
5291 ...(existing.endedWatch?.approvalAnnounced !== undefined && {
5292 approvalAnnounced: existing.endedWatch.approvalAnnounced,
5293 }),
5294 - // A finished-checks answer may be an older head's; a fresh read decides.
5295 - ...(existing.endedWatch?.checksPending === true && { checksPending: true }),
5294 + // CI pending with no failure yet at the start may fail before the first
5295 + // read. A caller's fresh read beats the link's last sync; a finished-checks
5296 + // answer kept from the last watch may be an older head's.
5297 + ...((command.type === "thread.pull-request.watch" &&
5298 + command.checksPending !== undefined
5299 + ? command.checksPending
5300 + : existing.snapshot?.checksState === "pending" ||
5301 + existing.endedWatch?.checksPending === true) && { checksPending: true }),
5302 ...(existing.endedWatch?.checksAwaitedSince !== undefined && {
5303 checksAwaitedSince: existing.endedWatch.checksAwaitedSince,
5304 }),
apps/server/src/orchestration-v2/pullRequestWatch.test.ts
+45
@@ -190,6 +190,51 @@ describe("evaluatePullRequestWatch", () => {
190 ]);
191 });
192
193 + it("reports failures on the first read only when CI was pending at the start", () => {
194 + const red = detail({ checks: [check("lint", "failure"), check("test", "pending")] });
195 + const pending = evaluatePullRequestWatch(
196 + watch({ awaitingBaseline: "all", checksPending: true }),
197 + red,
198 + noRemarks,
199 + );
200 + assert.deepEqual(pending.changes, [
201 + { kind: "checks-failed", failed: [check("lint", "failure")] },
202 + ]);
203 + assert.deepEqual(
204 + evaluatePullRequestWatch(
205 + pending.next,
206 + detail({ checks: [check("lint", "failure"), check("test", "success")] }),
207 + noRemarks,
208 + ).changes,
209 + [],
210 + );
211 + assert.deepEqual(
212 + evaluatePullRequestWatch(
213 + watch({ awaitingBaseline: "all", checksPending: false }),
214 + red,
215 + noRemarks,
216 + ).changes,
217 + [],
218 + );
219 +
220 + // A read the host could not finish keeps the baseline and must keep the seed with it.
221 + const placeholder = { ...check("Workflow approval status", "pending"), placeholder: true };
222 + const incomplete = evaluatePullRequestWatch(
223 + watch({ awaitingBaseline: "all", checksPending: true }),
224 + detail({ checks: [check("lint", "success"), placeholder] }),
225 + noRemarks,
226 + );
227 + assert.deepEqual(incomplete.changes, []);
228 + assert.deepEqual(
229 + evaluatePullRequestWatch(
230 + incomplete.next,
231 + detail({ checks: [check("lint", "success"), check("gated", "failure")] }),
232 + noRemarks,
233 + ).changes,
234 + [{ kind: "checks-failed", failed: [check("gated", "failure")] }],
235 + );
236 + });
237 +
238 it("baselines checks already awaiting approval", () => {
239 const gated = detail({ checks: [{ ...check("CI", "action-required"), url: run(1) }] });
240 const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), gated, noRemarks);
apps/server/src/orchestration-v2/pullRequestWatch.ts
+7 -2
@@ -76,9 +76,14 @@ export function evaluatePullRequestWatch(
76 const awaiting = checks.filter((check) => check.status === "action-required");
77 if (checks.length > 0) {
78 // A run awaiting approval waits on a person, like a review, so only running checks count.
79 - checksPending = checks.some((check) => check.status === "pending");
79 + // An unfinished baseline read keeps the seed below for the next one.
80 + checksPending =
81 + checks.some((check) => check.status === "pending") ||
82 + (checksBaseline && unread && watch.checksPending === true);
83 const newlyFailed = failed.filter((check) => !failedChecks.includes(check.name));
81 - if (!checksBaseline && newlyFailed.length > 0) {
84 + // The baseline hides failures from before the watch, but CI running at the start can fail
85 + // before the first read.
86 + if (newlyFailed.length > 0 && (!checksBaseline || watch.checksPending === true)) {
87 changes.push({ kind: "checks-failed", failed: newlyFailed });
88 }
89 if (!approvalAnnounced && awaiting.length > 0) {
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+175
@@ -4278,6 +4278,33 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
4278 assert.isUndefined(yield* watchOf);
4279 const { messages } = yield* orchestrator.getThreadRecords(threadId, ["messages"]);
4280 assert.deepEqual(messages, []);
4281 +
4282 + // A client start sends no CI state, so the link's last sync seeds it.
4283 + yield* orchestrator.dispatch({
4284 + type: "thread.pull-request-link.sync",
4285 + commandId: CommandId.make("pr-watch-ci-running"),
4286 + threadId,
4287 + ...key,
4288 + snapshot: {
4289 + state: "open",
4290 + title: "Watched pull request",
4291 + headBranch: "feature",
4292 + baseBranch: "main",
4293 + isDraft: false,
4294 + updatedAt: null,
4295 + syncedAt: "2026-10-02T12:30:00.000Z",
4296 + checksState: "pending",
4297 + },
4298 + stack: null,
4299 + });
4300 + yield* orchestrator.dispatch({
4301 + type: "thread.pull-request.watch",
4302 + commandId: CommandId.make("pr-watch-client-start"),
4303 + threadId,
4304 + ...key,
4305 + watching: true,
4306 + });
4307 + assert.isTrue((yield* watchOf)?.checksPending);
4308 }),
4309 );
4310
@@ -5219,6 +5246,154 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
5246 }),
5247 );
5248
5249 + it.effect.each([
5250 + // Failures from before the watch stay baseline, even with checks still running.
5251 + {
5252 + label: "finished failing",
5253 + atStart: "failing",
5254 + reads: [
5255 + { lint: "failure", test: "success" },
5256 + { lint: "failure", test: "success" },
5257 + ],
5258 + told: [[], []],
5259 + },
5260 + {
5261 + label: "failing and still running",
5262 + atStart: "failing",
5263 + reads: [
5264 + { lint: "failure", test: "pending" },
5265 + { lint: "failure", test: "failure" },
5266 + ],
5267 + told: [[], ["test"]],
5268 + },
5269 + // Pending CI had no failure, so one the first read finds landed after the start.
5270 + {
5271 + label: "pending",
5272 + atStart: "pending",
5273 + reads: [
5274 + { lint: "failure", test: "pending" },
5275 + { lint: "failure", test: "failure" },
5276 + ],
5277 + told: [["lint"], ["test"]],
5278 + },
5279 + ] as const)(
5280 + "reports only failures after a watch that starts on $label CI",
5281 + ({ label, atStart, reads, told }) =>
5282 + Effect.gen(function* () {
5283 + const orchestrator = yield* Orchestrator.OrchestratorV2;
5284 + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
5285 + const threadId = ThreadId.make(`runtime-pull-request-watch-start-${label}`);
5286 + const projectId = ProjectId.make("pr-watch-start-project");
5287 + yield* seedProject({
5288 + projectId,
5289 + title: "Watch start",
5290 + workspaceRoot: "/workspace/watch-start",
5291 + defaultModelSelection: null,
5292 + createdAt: "2026-10-01T00:00:00.000Z",
5293 + });
5294 + yield* orchestrator.dispatch({
5295 + type: "thread.create",
5296 + createdBy: "user",
5297 + creationSource: "web",
5298 + commandId: CommandId.make(`pr-watch-start-create-${label}`),
5299 + threadId,
5300 + projectId,
5301 + title: "Watch start",
5302 + modelSelection,
5303 + runtimeMode: "full-access",
5304 + interactionMode: "default",
5305 + branch: null,
5306 + worktreePath: null,
5307 + });
5308 + const key = { host: "github.com", repository: "pingdotgg/t3code", number: 13 };
5309 + yield* orchestrator.dispatch({
5310 + type: "thread.pull-request.link",
5311 + commandId: CommandId.make(`pr-watch-start-link-${label}`),
5312 + threadId,
5313 + ...key,
5314 + url: "https://github.com/pingdotgg/t3code/pull/13",
5315 + source: "manual",
5316 + });
5317 + yield* orchestrator.dispatch({
5318 + type: "thread.pull-request-link.sync",
5319 + commandId: CommandId.make(`pr-watch-start-sync-${label}`),
5320 + threadId,
5321 + ...key,
5322 + snapshot: {
5323 + state: "open",
5324 + title: "Watched pull request",
5325 + headBranch: "feature",
5326 + baseBranch: "main",
5327 + isDraft: false,
5328 + updatedAt: null,
5329 + syncedAt: "2026-10-02T12:00:00.000Z",
5330 + checksState: atStart,
5331 + },
5332 + stack: null,
5333 + });
5334 + // A client start, which carries no CI state of its own.
5335 + yield* orchestrator.dispatch({
5336 + type: "thread.pull-request.watch",
5337 + commandId: CommandId.make(`pr-watch-start-watch-${label}`),
5338 + threadId,
5339 + ...key,
5340 + watching: true,
5341 + });
5342 +
5343 + let read = 0;
5344 + const host = Layer.mergeAll(
5345 + NodeServices.layer,
5346 + NodeCrypto.layer,
5347 + Layer.mock(PullRequestService.PullRequestService)({
5348 + detail: () =>
5349 + Effect.sync(() => ({
5350 + ...watchedPullRequestDetail({
5351 + projectId,
5352 + number: 13,
5353 + at: "2026-10-02T12:00:00.000Z",
5354 + }),
5355 + headSha: "ad70160aaa",
5356 + checks: Object.entries(reads[read]!).map(([name, status]) => ({
5357 + name,
5358 + status,
5359 + description: null,
5360 + url: null,
5361 + })),
5362 + })),
5363 + activity: () =>
5364 + Effect.succeed({
5365 + comments: [],
5366 + commentCount: 0,
5367 + commentsTruncated: false,
5368 + reviewThreads: [],
5369 + commits: [],
5370 + }),
5371 + }),
5372 + );
5373 + const texts = Effect.map(
5374 + orchestrator.getThreadRecords(threadId, ["messages"]),
5375 + ({ messages }) => messages.map((message) => message.text),
5376 + );
5377 + const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
5378 + for (const [index, names] of told.entries()) {
5379 + read = index;
5380 + const before = (yield* texts).length;
5381 + if (index > 0) yield* TestClock.adjust("10 minutes");
5382 + yield* reactor.sweep;
5383 + yield* worker.drain();
5384 + const wakes = (yield* texts).slice(before);
5385 + assert.equal(wakes.length, names.length === 0 ? 0 : 1);
5386 + for (const name of ["lint", "test"]) {
5387 + assert.equal(
5388 + wakes.some((text) => text.includes(` - ${name}`)),
5389 + names.includes(name as never),
5390 + `read ${index} told ${name}`,
5391 + );
5392 + }
5393 + }
5394 + }),
5395 + );
5396 +
5397 it.effect("baselines a new watch across restarts and reports later changes", () =>
5398 Effect.gen(function* () {
5399 const orchestrator = yield* Orchestrator.OrchestratorV2;
apps/server/src/pullRequest/PullRequestWatchService.test.ts
+25 -10
@@ -114,6 +114,7 @@ interface HarnessOptions {
114 readonly project?: OrchestrationProjectShell | null;
115 readonly readFailure?: PullRequestService.PullRequestError;
116 readonly readState?: PullRequestSummary["state"];
117 + readonly readChecksState?: PullRequestSummary["checksState"];
118 }
119
120 const makeHarness = Effect.fn("makePullRequestWatchHarness")(function* (
@@ -144,6 +145,9 @@ const makeHarness = Effect.fn("makePullRequestWatchHarness")(function* (
145 title: "Pull request",
146 url: `https://github.com/${input.repository}/pull/${input.number}`,
147 state: options.readState ?? "open",
148 + ...(options.readChecksState !== undefined && {
149 + checksState: options.readChecksState,
150 + }),
151 headBranch: "feature",
152 baseBranch: "main",
153 updatedAt: "2026-08-20T00:00:00.000Z",
@@ -164,21 +168,28 @@ const makeHarness = Effect.fn("makePullRequestWatchHarness")(function* (
168 describe("PullRequestWatchService", () => {
169 it.effect("links an unlinked watch even while the host has a transient read failure", () =>
170 Effect.gen(function* () {
167 - for (const options of [
168 - {},
169 - {
170 - readFailure: new PullRequestOperationError({
171 - operation: "summary",
172 - detail: "The host is temporarily unavailable.",
173 - }),
174 - },
175 - ]) {
171 + const cases: ReadonlyArray<readonly [HarnessOptions, boolean | undefined]> = [
172 + [{ readChecksState: "pending" }, true],
173 + [{ readChecksState: "failing" }, false],
174 + [{}, false],
175 + [
176 + {
177 + readFailure: new PullRequestOperationError({
178 + operation: "summary",
179 + detail: "The host is temporarily unavailable.",
180 + }),
181 + },
182 + undefined,
183 + ],
184 + ];
185 + for (const [options, checksPending] of cases) {
186 const harness = yield* makeHarness(options);
187 const result = yield* harness.setWatching(true, {
188 url: "https://github.com/t3tools/t3code/pull/9",
189 });
190 expect(result).toMatchObject({ number: 9, wasWatching: false });
181 - expect(yield* Ref.get(harness.commands)).toMatchObject([
191 + const commands = yield* Ref.get(harness.commands);
192 + expect(commands).toMatchObject([
193 {
194 type: "thread.pull-request.watch",
195 number: 9,
@@ -186,6 +197,10 @@ describe("PullRequestWatchService", () => {
197 link: { url: "https://github.com/t3tools/t3code/pull/9", source: "agent" },
198 },
199 ]);
200 + const [command] = commands;
201 + expect(command?.type === "thread.pull-request.watch" ? command.checksPending : null).toBe(
202 + checksPending,
203 + );
204 }
205 }),
206 );
apps/server/src/pullRequest/PullRequestWatchService.ts
+9 -3
@@ -126,8 +126,10 @@ const make = Effect.gen(function* () {
126 if (watching && before?.snapshot?.state === "merged") {
127 return yield* new PullRequestNotOpenError({ state: "merged" });
128 }
129 + // A pending rollup has no failure yet, so any failure the first read finds is news.
130 + let checksPendingAtStart: boolean | undefined;
131 if (watching && before?.watch === undefined) {
130 - yield* pullRequests
132 + checksPendingAtStart = yield* pullRequests
133 .summary(
134 {
135 projectId: thread.projectId,
@@ -142,11 +144,12 @@ const make = Effect.gen(function* () {
144 Effect.flatMap((summary) =>
145 summary.state === "merged"
146 ? Effect.fail(new PullRequestNotOpenError({ state: "merged" }))
145 - : Effect.void,
147 + : Effect.succeed(summary.checksState === "pending"),
148 ),
149 Effect.catchTags({
150 // Host and transport errors can recover while the watch is active.
149 - PullRequestOperationError: () => Effect.void,
151 + // A failed read leaves the seed to the link's last sync.
152 + PullRequestOperationError: () => Effect.undefined,
153 }),
154 );
155 }
@@ -162,6 +165,9 @@ const make = Effect.gen(function* () {
165 watching,
166 ...(watching ? { link: { url: target.url, source: "agent" as const } } : {}),
167 ...(watching && input.until !== undefined && { until: input.until }),
168 + ...(watching && checksPendingAtStart !== undefined
169 + ? { checksPending: checksPendingAtStart }
170 + : {}),
171 })
172 .pipe(
173 Effect.catchCause((cause) =>
packages/contracts/src/orchestrationV2.ts
+2
@@ -3323,6 +3323,8 @@ export const OrchestrationV2Command = Schema.Union([
3323 watching: Schema.Boolean,
3324 /** With `watching`, sets how long the watch holds its thread; absent keeps the current one. */
3325 until: Schema.optional(PullRequestWatchUntil),
3326 + /** Whether CI was pending with no failure yet when the watch started, so any failure is news. */
3327 + checksPending: Schema.optional(Schema.Boolean),
3328 link: Schema.optional(
3329 Schema.Struct({
3330 url: TrimmedNonEmptyString,