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
10 changes: 4 additions & 6 deletions apps/server/src/orchestration-v2/PullRequestSyncReactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
threadPullRequestKeysEqual,
visibleThreadPullRequests,
} from "../sourceControl/threadPullRequests.ts";
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
import {
isChangeRequestCloseCommand,
sourceControlDefinitions,
} from "../sourceControl/sourceControlDefinitions.ts";
import * as Cause from "effect/Cause";
import * as Clock from "effect/Clock";
import * as Context from "effect/Context";
Expand Down Expand Up @@ -51,11 +54,6 @@ const CLOSED_FINAL_AFTER_MS = 24 * 60 * 60 * 1_000;
// A link whose host read failed waits a sweep, then twice as long after each further failure.
const FAILED_READ_BACKOFF_MS = 60 * 1_000;

const isChangeRequestCloseCommand = (command: string) =>
sourceControlDefinitions.definitions.some(
(definition) => definition.changeRequestCloseCommand?.test(command) === true,
);

// Where a shell starts a command, past `NAME=value` assignments and wrappers like `timeout 60`.
// A create command anywhere else, as in `echo "gh pr create"`, is text, not a command that runs.
const COMMAND_START =
Expand Down
162 changes: 132 additions & 30 deletions apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import {
CommandId,
isOrchestrationV2WorkActive,
MessageId,
type OrchestrationV2Notification,
type PullRequestActivity,
type PullRequestComment,
type PullRequestDetail,
type PullRequestRef,
type PullRequestThreadCommentsResult,
type ThreadPullRequestLink,
type ThreadId,
type ThreadPullRequestWatch,
} from "@t3tools/contracts";
import { commandIndicatesFailure } from "@t3tools/shared/toolOutput";
import {
changeRequestsClosedBy,
isChangeRequestCloseCommand,
} from "../sourceControl/sourceControlDefinitions.ts";
import {
normalizeThreadPullRequestKey,
threadPullRequestKeyOf,
Expand Down Expand Up @@ -37,10 +45,12 @@ import * as PullRequestService from "../pullRequest/PullRequestService.ts";
import { forkParked } from "../serverActivation.ts";
import * as Orchestrator from "./Orchestrator.ts";
import * as ProjectionStore from "./ProjectionStore.ts";
import { isTerminalRunStatus } from "./ThreadManagementService.ts";
import {
combinePullRequestWatchWakes,
evaluatePullRequestWatch,
PULL_REQUEST_WATCH_NO_CHECKS_MS,
pullRequestWatchEndMessage,
pullRequestWatchMessage,
} from "./pullRequestWatch.ts";

Expand Down Expand Up @@ -292,6 +302,17 @@ export const make = Effect.gen(function* () {
// A pass reads watches and records new ones, so passes must not overlap.
const passes = Semaphore.makeUnsafe(1);
const lastReads = new Map<string, LastRead>();
/**
* Each thread's merge and close commands, by item, with the pull requests each names. One counts
* while it runs and, if it succeeded, until the read at its end is done.
*/
const agentEnds = new Map<ThreadId, Map<string, ReadonlySet<number> | "any">>();
// The agent needs no wake for a merge or close it made itself. Anything ending later, as a
// `--auto` merge does, is news.
const endedByAgent = (target: WatchTarget) =>
[...(agentEnds.get(target.thread.id)?.values() ?? [])].some(
(named) => named === "any" || named.has(target.link.number),
);
const threadTails = new Map<
string,
{
Expand Down Expand Up @@ -524,30 +545,34 @@ export const make = Effect.gen(function* () {
);
});

const closed = (groupKey: string, target: WatchTarget) =>
hold({
groupKey,
target,
next: null,
wake: {
text: `Pull request #${target.link.number} (${target.link.url}) was closed, so T3 Code stopped watching it. Call watch_pull_request if it reopens.`,
notification: {
source: { kind: "monitor" },
outcome: "updated",
summary: `#${target.link.number}: closed, stopped watching`,
},
},
reason: "closed",
});
const finished = (
groupKey: string,
target: WatchTarget,
detail: Pick<PullRequestDetail, "state" | "baseBranch" | "mergeCommitSha" | "mergedBy">,
) => {
const reason = detail.state === "merged" ? "merged" : "closed";
return endedByAgent(target)
? record(target, null).pipe(Effect.tap(() => ended(target, reason)))
: hold({
groupKey,
target,
next: null,
wake: pullRequestWatchEndMessage({
number: target.link.number,
url: target.link.url,
detail,
}),
reason,
});
};

const endsWithoutRead = ({ thread, link }: WatchTarget): WatchEndReason | undefined =>
link.snapshot?.state === "merged"
? "merged"
: thread.settledOverride === "settled" || thread.settledAt !== null
? "settled"
: thread.lineage.relationshipToParent === "subagent"
? "subagent"
: undefined;
// A merged snapshot still gets one read, so the wake can name the merge commit.
const endsWithoutRead = ({ thread }: WatchTarget): WatchEndReason | undefined =>
thread.settledOverride === "settled" || thread.settledAt !== null
? "settled"
: thread.lineage.relationshipToParent === "subagent"
? "subagent"
: undefined;

const eachTarget = <E>(
group: WatchGroup,
Expand All @@ -566,6 +591,16 @@ export const make = Effect.gen(function* () {
),
).pipe(Effect.map((landed) => landed.every(Boolean)));

// A merged PR's watch holds its thread from settling, so it must not wait out the host.
const endMergedSnapshots = (group: WatchGroup) => {
const merged = group.targets.filter((target) => target.link.snapshot?.state === "merged");
return eachTarget({ ...group, targets: merged }, (target) =>
target.link.snapshot ? finished(group.key, target, target.link.snapshot) : Effect.void,
).pipe(
Effect.as({ ...group, targets: group.targets.filter((target) => !merged.includes(target)) }),
);
};

const referenceOf = (group: WatchGroup) => {
const first = group.targets[0]!;
return { projectId: first.thread.projectId, ...identityOf(first.link) };
Expand Down Expand Up @@ -620,6 +655,11 @@ export const make = Effect.gen(function* () {
if (Cause.hasInterruptsOnly(detailRead.cause))
return yield* Effect.failCause(detailRead.cause);
lastReads.delete(group.key);
const unended = yield* endMergedSnapshots(group);
if (unended.targets.length === 0) {
readFailures.delete(group.key);
return;
}
const reason = providerFailureReason(detailRead.cause);
if (reason === "rate-limited") return;
// Transport and host failures can outlast the failure limit; keep the watch for recovery.
Expand All @@ -629,7 +669,7 @@ export const make = Effect.gen(function* () {
}
const failures = (readFailures.get(group.key) ?? 0) + 1;
readFailures.set(group.key, failures);
if (failures >= READ_FAILURE_LIMIT && (yield* eachTarget(group, giveUp))) {
if (failures >= READ_FAILURE_LIMIT && (yield* eachTarget(unended, giveUp))) {
readFailures.delete(group.key);
}
return yield* Effect.failCause(detailRead.cause);
Expand All @@ -651,11 +691,7 @@ export const make = Effect.gen(function* () {
}
if (detail.state !== "open") {
lastReads.delete(group.key);
return yield* eachTarget(group, (target) =>
detail.state === "closed"
? closed(group.key, target)
: record(target, null).pipe(Effect.tap(() => ended(target, "merged"))),
);
return yield* eachTarget(group, (target) => finished(group.key, target, detail));
}

// A failed comments read leaves remarks incomplete, so the next pass reads them again;
Expand Down Expand Up @@ -812,7 +848,7 @@ export const make = Effect.gen(function* () {
}),
Effect.flatMap((fingerprint) =>
fingerprint === "paused"
? Effect.void
? endMergedSnapshots(group).pipe(Effect.asVoid)
: reads.withPermit(
readGroup(group, fingerprint).pipe(
Effect.catchCause(
Expand Down Expand Up @@ -840,9 +876,75 @@ export const make = Effect.gen(function* () {
Effect.withSpan("PullRequestWatchReactor.readOnChange"),
);

// A full read, since an unchanged fingerprint or snapshot would otherwise skip the detail.
const readThreadWatches = (threadId: ThreadId) =>
projections.getThreadsWithPullRequests(threadId).pipe(
Effect.flatMap((threads) =>
Effect.forEach(
threads.flatMap((thread) =>
visibleThreadPullRequests(thread.pullRequests ?? [])
.filter((link) => link.watch !== undefined)
.map((link) => [thread.projectId, threadPullRequestKeyOf(link)] as const),
),
([projectId, pullRequest]) =>
// After any pass in flight, which could record a read from before the merge.
passes
.withPermit(Effect.sync(() => lastReads.delete(`${projectId} ${pullRequest}`)))
.pipe(Effect.andThen(readOnChange(pullRequest))),
{ discard: true },
),
),
Effect.catchCause(logFailure("pull request watch read after a merge failed", { threadId })),
);

const start: PullRequestWatchReactor["Service"]["start"] = Effect.fn(
"PullRequestWatchReactor.start",
)(function* () {
// Reading a thread's watches as its agent's merge or close ends lets them end at once.
const agentReads = yield* FiberMap.make<string>();
const finished = new Set<string>();
yield* forkParked(
Stream.runForEach(engine.streamDomainEvents, (event) => {
if (event.type === "run.updated") {
// A run can stop before its command reports that it ended.
const commands = agentEnds.get(event.threadId);
if (commands === undefined || !isTerminalRunStatus(event.payload.status)) {
return Effect.void;
}
for (const id of commands.keys()) if (!finished.has(id)) commands.delete(id);
if (commands.size === 0) agentEnds.delete(event.threadId);
return Effect.void;
}
if (event.type !== "turn-item.updated") return Effect.void;
const item = event.payload;
if (
item.type !== "command_execution" ||
finished.has(item.id) ||
!isChangeRequestCloseCommand(item.input)
) {
return Effect.void;
}
const commands = agentEnds.get(event.threadId) ?? new Map();
agentEnds.set(event.threadId, commands);
commands.set(item.id, changeRequestsClosedBy(item.input));
if (isOrchestrationV2WorkActive(item.status)) return Effect.void;
finished.add(item.id);
const done = Effect.sync(() => {
commands.delete(item.id);
if (commands.size === 0 && agentEnds.get(event.threadId) === commands) {
agentEnds.delete(event.threadId);
}
});
// Some providers report a command that failed as completed.
return Effect.andThen(
item.status === "completed" && !commandIndicatesFailure(item) ? Effect.void : done,
FiberMap.run(
agentReads,
item.id,
)(readThreadWatches(event.threadId).pipe(Effect.ensuring(done))),
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}).pipe(Effect.catchCause(logFailure("pull request watch command stream failed", {}))),
);
// One feed per pull request: the host keeps one subscription per pull request and account.
const feeds = yield* FiberMap.make<string>();
const follow = Effect.gen(function* () {
Expand Down
9 changes: 2 additions & 7 deletions apps/server/src/orchestration-v2/WireProjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
} from "@t3tools/shared/orchestrationV2DedupedBodies";
import { MCP_APP_OUTPUT_KEY, readMcpAppReference } from "@t3tools/shared/mcpApp";
import {
commandIndicatesFailure,
compactDynamicToolOutput,
omitToolOutputImageData,
toolOutputImages,
toolOutputIndicatesFailure,
} from "@t3tools/shared/toolOutput";

// A checkpoint over vendored or generated trees can list 15k+ files; the card needs a prefix.
Expand Down Expand Up @@ -167,12 +167,7 @@ export function projectTurnItemForWire(item: OrchestrationV2TurnItem): Orchestra
const { output, ...projected } = item;
// A successful search can print "ENOENT" or "exit code 2", so output text
// decides only when the provider reported no exit code.
const failed =
item.exitCode === undefined
? item.outputIndicatesFailure === true ||
(output !== undefined &&
toolOutputIndicatesFailure(output.slice(0, MAX_DETAIL_STRING_BYTES)))
: item.exitCode !== 0;
const failed = commandIndicatesFailure(item);
return {
...projected,
...(failed ? { outputIndicatesFailure: true } : {}),
Expand Down
40 changes: 40 additions & 0 deletions apps/server/src/orchestration-v2/pullRequestWatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { assert, describe, it } from "@effect/vitest";
import {
PULL_REQUEST_WATCH_WAKE_LIMIT,
evaluatePullRequestWatch as evaluate,
pullRequestWatchEndMessage,
pullRequestWatchMessage,
} from "./pullRequestWatch.ts";

Expand Down Expand Up @@ -1008,3 +1009,42 @@ describe("evaluatePullRequestWatch", () => {
assert.isTrue(unanswered(STARTED));
});
});

describe("pullRequestWatchEndMessage", () => {
const url = "https://git.cafe/pingdotgg/t3code/pull/7";
const end = (overrides: Partial<Parameters<typeof pullRequestWatchEndMessage>[0]["detail"]>) =>
pullRequestWatchEndMessage({
number: 7,
url,
detail: { state: "merged", baseBranch: "main", ...overrides },
});
const mergeCommitSha = "3b2f958603a51d8c42c4ede02eee7b8cb310b2d9";

it("says how the pull request ended, with the merge commit and merger the host reports", () => {
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
const merged = end({
mergeCommitSha,
mergedBy: { login: "juliusmarminge", name: null, avatarUrl: null },
});
assert.equal(
merged.text,
`Pull request #7 (${url}) was merged into main by juliusmarminge as 3b2f958, so T3 Code stopped watching it.`,
);
assert.deepEqual(merged.notification, {
source: { kind: "monitor" },
outcome: "completed",
summary: "#7: merged, stopped watching",
});
// GitCafe reports a merge made on GitHub without who made it.
assert.equal(
end({ mergeCommitSha }).text,
`Pull request #7 (${url}) was merged into main as 3b2f958, so T3 Code stopped watching it.`,
);

const closed = end({ state: "closed" });
assert.equal(
closed.text,
`Pull request #7 (${url}) was closed without merging, so T3 Code stopped watching it. If it is reopened, call watch_pull_request to watch it again.`,
);
assert.equal(closed.notification.summary, "#7: closed, stopped watching");
});
});
27 changes: 27 additions & 0 deletions apps/server/src/orchestration-v2/pullRequestWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,33 @@ export function combinePullRequestWatchWakes(
};
}

export function pullRequestWatchEndMessage(input: {
readonly number: number;
readonly url: string;
readonly detail: Pick<PullRequestDetail, "state" | "baseBranch" | "mergeCommitSha" | "mergedBy">;
}): { readonly text: string; readonly notification: OrchestrationV2Notification } {
const { detail } = input;
const merged = detail.state === "merged";
const how = merged
? [
`was merged into ${detail.baseBranch}`,
detail.mergedBy ? ` by ${detail.mergedBy.login}` : "",
detail.mergeCommitSha ? ` as ${detail.mergeCommitSha.slice(0, 7)}` : "",
].join("")
: "was closed without merging";
return {
text: [
`Pull request #${input.number} (${input.url}) ${how}, so T3 Code stopped watching it.`,
...(merged ? [] : ["If it is reopened, call watch_pull_request to watch it again."]),
].join(" "),
notification: {
source: { kind: "monitor" },
outcome: merged ? "completed" : "updated",
summary: `#${input.number}: ${merged ? "merged" : "closed"}, stopped watching`,
},
};
}

export function pullRequestWatchMessage(input: {
readonly number: number;
readonly url: string;
Expand Down
Loading
Loading