Skip to content

Commit 9359081

Browse files
fix(server): a pull request watch started after a pending self-settle survives the turn end (#34884)
1 parent 8024e50 commit 9359081

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/ThreadManagementService.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import * as CommandReceiptStore from "./CommandReceiptStore.ts";
6262
import * as Orchestrator from "./Orchestrator.ts";
6363
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
6464
import { projectedRowEncodedBytes } from "./threadHistoryPaging.ts";
65+
import { threadPullRequestsOf } from "../sourceControl/threadPullRequests.ts";
6566
import * as ProviderSessionManager from "./ProviderSessionManager.ts";
6667
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
6768
import { DispatchModeLimit, exceededDispatchModeLimit } from "./DispatchModeLimit.ts";
@@ -1066,12 +1067,22 @@ const make = Effect.gen(function* () {
10661067
const settleAfterRun: ThreadManagementServiceShape["settleAfterRun"] = Effect.fn(
10671068
"orchestrationV2.threadManagement.settleAfterRun",
10681069
)(function* (input) {
1070+
const requestedAt = yield* DateTime.now;
10691071
const { run } = yield* waitForThread({ ...input, timeoutMs: SETTLE_AFTER_RUN_WAIT_MS });
10701072
if (run?.status !== "completed") return;
10711073
const afterSequence = yield* orchestrator.getThreadEventSequence(input.threadId);
10721074
const trySettle = Effect.gen(function* () {
10731075
const sequence = yield* orchestrator.getThreadEventSequence(input.threadId);
10741076
const records = yield* getProjectThreadRecords(input, ["runs"]);
1077+
// Settling ends every watch; one the agent started after asking to settle must outlive it.
1078+
if (
1079+
threadPullRequestsOf(records.thread).some(
1080+
(link) =>
1081+
link.watch !== undefined &&
1082+
!DateTime.isLessThan(DateTime.makeUnsafe(link.watch.startedAt), requestedAt),
1083+
)
1084+
)
1085+
return true;
10751086
const following = records.runs.filter((candidate) => candidate.ordinal > run.ordinal);
10761087
if (following.length > 0) {
10771088
const { messages } = yield* getProjectThreadRecords(input, ["messages"], {

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5761,6 +5761,81 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
57615761
}),
57625762
);
57635763

5764+
it.effect("keeps a pull request watch its agent started after asking to settle", () =>
5765+
Effect.gen(function* () {
5766+
const orchestrator = yield* Orchestrator.OrchestratorV2;
5767+
const eventSink = yield* EventSink.EventSinkV2;
5768+
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
5769+
const projectId = ProjectId.make("runtime-layer-settle-then-watch-project");
5770+
const threadId = ThreadId.make("runtime-layer-settle-then-watch-thread");
5771+
5772+
yield* orchestrator.dispatch({
5773+
type: "thread.create",
5774+
createdBy: "user",
5775+
creationSource: "web",
5776+
commandId: CommandId.make("runtime-layer-settle-then-watch-create"),
5777+
threadId,
5778+
projectId,
5779+
title: "Settle then watch",
5780+
modelSelection,
5781+
runtimeMode: "full-access",
5782+
interactionMode: "default",
5783+
branch: null,
5784+
worktreePath: "/tmp/runtime-layer-settle-then-watch",
5785+
});
5786+
yield* orchestrator.dispatch({
5787+
type: "message.dispatch",
5788+
createdBy: "user",
5789+
creationSource: "web",
5790+
commandId: CommandId.make("runtime-layer-settle-then-watch-message"),
5791+
threadId,
5792+
messageId: MessageId.make("runtime-layer-settle-then-watch-message"),
5793+
text: "Settle this thread, then watch the pull request.",
5794+
attachments: [],
5795+
modelSelection,
5796+
dispatchMode: { type: "start_immediately" },
5797+
});
5798+
const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0];
5799+
if (run === undefined) return yield* Effect.die(new Error("Run missing."));
5800+
5801+
// The deferred settle `settleThread` forks for its own agent's active run.
5802+
const settleRequest = yield* threadManagement
5803+
.settleAfterRun({ projectId, threadId, runId: run.id })
5804+
.pipe(Effect.forkChild);
5805+
yield* orchestrator.dispatch({
5806+
type: "thread.pull-request.watch",
5807+
commandId: CommandId.make("runtime-layer-settle-then-watch-watch"),
5808+
threadId,
5809+
host: "github.com",
5810+
repository: "pingdotgg/t3code",
5811+
number: 7,
5812+
watching: true,
5813+
link: { url: "https://github.com/pingdotgg/t3code/pull/7", source: "agent" },
5814+
});
5815+
assert.isDefined((yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch);
5816+
5817+
const now = yield* DateTime.now;
5818+
yield* eventSink.write({
5819+
commandId: CommandId.make("runtime-layer-settle-then-watch-completed"),
5820+
events: [
5821+
{
5822+
id: EventId.make("runtime-layer-settle-then-watch-completed"),
5823+
type: "run.updated",
5824+
threadId,
5825+
runId: run.id,
5826+
occurredAt: now,
5827+
payload: { ...run, status: "completed", startedAt: now, completedAt: now },
5828+
},
5829+
],
5830+
});
5831+
yield* Fiber.join(settleRequest);
5832+
5833+
const shell = yield* orchestrator.getThreadShell(threadId);
5834+
assert.isDefined(shell?.pullRequests?.[0]?.watch);
5835+
assert.isNull(shell?.settledOverride ?? null);
5836+
}),
5837+
);
5838+
57645839
it.effect("settles past held automatic runs but not held user messages", () =>
57655840
Effect.gen(function* () {
57665841
const orchestrator = yield* Orchestrator.OrchestratorV2;

0 commit comments

Comments
 (0)