sailfork / t3code-github / apps/ server/ src/ orchestration-v2/ PullRequestSyncReactor.ts

import {
  CommandId,
  isOrchestrationV2WorkActive,
  type OrchestrationV2TurnItem,
  type PullRequestSummary,
  type ThreadId,
  type ThreadPullRequestKey,
  type ThreadPullRequestLink,
  type ThreadPullRequestSnapshot,
  type ThreadPullRequestStack,
} from "@t3tools/contracts";
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
import {
  threadPullRequestKeyOf,
  normalizeThreadPullRequestKey,
  threadPullRequestKeysEqual,
  visibleThreadPullRequests,
} from "../sourceControl/threadPullRequests.ts";
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
import * as Cause from "effect/Cause";
import * as Clock from "effect/Clock";
import * as Context from "effect/Context";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import * as Schedule from "effect/Schedule";
import type * as Scope from "effect/Scope";
import * as Semaphore from "effect/Semaphore";
import * as Stream from "effect/Stream";

import * as GitManager from "../git/GitManager.ts";
import { PullRequestProviderError } from "@t3tools/source-control-core/server/PullRequestProvider";
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";

const SLOW_SYNC_INTERVAL_MS = 15 * 60 * 1_000;
// Every server polls every open linked pull request, and most of them sit untouched for days.
// One nobody has updated for QUIET_AFTER_MS is read on the quiet cadence instead of each sweep;
// requested refreshes, watches and a host CLI's merge command still read it at once.
const QUIET_AFTER_MS = 30 * 60 * 1_000;
const QUIET_SYNC_INTERVAL_MS = 5 * 60 * 1_000;
// A pull request closed this long ago is rarely reopened, so the sweep stops reading it, as it
// does merged ones. A reader that sees it open, or a requested refresh, still reads it at once.
const CLOSED_FINAL_AFTER_MS = 24 * 60 * 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 =
  "(?:^|[;&|({`\\n]|\\b(?:do|then|else)\\s)\\s*(?:(?:\\w+=\\S*|env|time|nohup|timeout\\s+\\S+)\\s+)*";
const CREATE_COMMANDS = sourceControlDefinitions.definitions.flatMap(
  ({ kind, changeRequestCreateCommand: command }) =>
    command === undefined
      ? []
      : // The subcommand must end its shell word: `gh pr create#x` runs another one.
        [
          {
            kind,
            command,
            run: new RegExp(`${COMMAND_START}${command.source}(?=$|[\\s;&|)])`, "gu"),
          },
        ],
);
const createCommandCount = (code: string) =>
  CREATE_COMMANDS.reduce((count, { run }) => count + [...code.matchAll(run)].length, 0);

// Codex runs each command as `/bin/zsh -lc <script>`, quoting the script in pieces such as
// `"a "'`b`'" c"`.
const SHELL_WRAPPER = /^\s*(?:\S*\/)?(?:ba|z|da|k)?sh\s+-l?c\s+(?=["'])([\s\S]*)$/u;
const SHELL_WORD_PIECE = /"((?:\\[\s\S]|[^"\\])*)"|'([^']*)'|\\([\s\S])|(\S)/gu;
// Heredoc bodies, quoted strings and comments are text a command carries, not commands it runs.
const HEREDOC_BODY =
  /<<-?[ \t]*(['"]?)(\w+)\1[^\n]*\n(?:(?:[\s\S]*?\n)?[ \t]*\2[ \t]*(?=\n|$)|[\s\S]*$)/gu;
const QUOTED_OR_COMMENT = /'[^']*'|"(?:\\[\s\S]|[^"\\])*"|(?<=^|[\s;&|(])#[^\n]*/gmu;
const COMMAND_SEPARATOR = /&&|\|\||[;|\n]/u;
// Commands that print other change requests' URLs, so output beside them proves nothing.
const LISTS_CHANGE_REQUESTS =
  /\b(?:gh\s+(?:pr\s+(?:list|status)|search\s+prs|api)|glab\s+(?:mr\s+list|api))\b/u;
const URL_LINE = /^[ \t]*(https?:\/\/\S+)[ \t]*$/gmu;

/** The script a shell input runs, unwrapped from Codex's `sh -lc`. */
function shellScript(input: string): string {
  const [, wrapped] = SHELL_WRAPPER.exec(input) ?? [];
  return wrapped === undefined
    ? input
    : Array.from(
        wrapped.matchAll(SHELL_WORD_PIECE),
        ([, double, single, escaped, bare]) =>
          double?.replace(/\\(["\\

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	apps/server/src/orchestration-v2/PullRequestSyncReactor.ts at main · sailfork/t3code-github · Sailfork
	
		
])/gu, "$1") ?? single ?? escaped ?? bare, ).join(""); } /** A script split at command separators that sit outside quotes. */ function shellSegments(script: string): ReadonlyArray<string> { const segments: Array<string> = []; let current = ""; let quote: string | null = null; for (let index = 0; index < script.length; index++) { const char = script[index] ?? ""; if (quote !== null) { current += char; if (char === "\\" && quote === '"') current += script[++index] ?? ""; else if (char === quote) quote = null; continue; } if (char === "'" || char === '"') quote = char; if (char === "\\") { current += char + (script[++index] ?? ""); continue; } const pair = script.slice(index, index + 2); if (pair === "&&" || pair === "||" || char === ";" || char === "|" || char === "\n") { segments.push(current); current = ""; if (pair === "&&" || pair === "||") index++; continue; } current += char; } segments.push(current); return segments; } /** The commands a script runs, without the text it carries. */ const shellCode = (script: string) => script.replace(HEREDOC_BODY, "<<").replace(QUOTED_OR_COMMENT, "''"); type CommandExecution = Extract<OrchestrationV2TurnItem, { readonly type: "command_execution" }>; /** * The change requests a shell command opened: URLs on lines of their own in the output of a * command that runs a host's create command. One the command also names, as `gh pr view 12` * does, existed before it ran. More URLs than create commands means something else printed * some, such as `gh pr list`, so none are taken. */ export function changeRequestsCreatedBy(input: string, output: string) { if (!CREATE_COMMANDS.some(({ command }) => command.test(input))) return []; const script = shellScript(input); const code = shellCode(script); const creates = createCommandCount(code); if (creates === 0) return []; const otherCommands = code .split(COMMAND_SEPARATOR) .filter((part) => createCommandCount(part) === 0) .join("\n"); if (LISTS_CHANGE_REQUESTS.test(otherCommands)) return []; const otherScript = shellSegments(script) .filter((segment) => createCommandCount(shellCode(segment)) === 0) .join("\n"); const creators = new Set( CREATE_COMMANDS.filter(({ run }) => code.match(run) !== null).map(({ kind }) => kind), ); const created = new Map<string, ThreadPullRequestKey & { readonly url: string }>(); for (const [, url = ""] of output.matchAll(URL_LINE)) { const parsed = sourceControlDefinitions.parseChangeRequestUrl(url); // Only the host whose create command ran can have printed a new change request. if (parsed === null || !creators.has(parsed.definition.kind)) continue; const { link } = parsed; const named = new RegExp(`(?:^|(?<=[\\s#/=("']))${link.number}(?=$|[\\s);&|/"'])`, "u"); // Quoted text in the other commands counts, as in `printf '.../pull/12'`. if (named.test(otherScript)) continue; const key = normalizeThreadPullRequestKey(link); created.set(threadPullRequestKeyOf(key), { ...key, url }); } return created.size > creates ? [] : [...created.values()]; } const isPullRequestProviderError = Schema.is(PullRequestProviderError); type SnapshotFields = Omit<ThreadPullRequestSnapshot, "syncedAt">; interface LinkEntry { readonly thread: ProjectionStore.ProjectionThreadPullRequests; readonly link: ThreadPullRequestLink; } /** Another layer of the stack `url` belongs to, in its host's canonical shape. */ function siblingChangeRequestUrl(url: string, number: number): string | null { if (!Number.isSafeInteger(number) || number < 1) return null; const parsed = sourceControlDefinitions.parseChangeRequestUrl(url); if (parsed === null) return null; const { definition, link } = parsed; return definition.changeRequestUrl({ host: link.host, repository: link.repository, number, webUrl: link.repositoryUrl, }); } function snapshotFieldsOf(summary: PullRequestSummary): SnapshotFields { return { state: summary.state, title: summary.title, headBranch: summary.headBranch, baseBranch: summary.baseBranch, isDraft: summary.isDraft ?? false, updatedAt: summary.updatedAt, closedAt: summary.closedAt ?? null, mergedAt: summary.mergedAt ?? null, ...(summary.author === undefined ? {} : { author: summary.author }), ...(summary.additions === undefined ? {} : { additions: summary.additions }), ...(summary.deletions === undefined ? {} : { deletions: summary.deletions }), ...(summary.changedFiles === undefined ? {} : { changedFiles: summary.changedFiles }), ...(summary.reviewDecision === undefined ? {} : { reviewDecision: summary.reviewDecision }), ...(summary.checksState === undefined ? {} : { checksState: summary.checksState }), ...(summary.mergeability === undefined ? {} : { mergeability: summary.mergeability }), }; } // GitHub briefly returns unknown after base pushes; other hosts may mean a lasting condition. function withKnownMergeability( previous: ThreadPullRequestSnapshot | null, fields: SnapshotFields, provider: PullRequestSummary["provider"], ): SnapshotFields { if ( provider !== "github" || fields.state !== "open" || fields.mergeability !== "unknown" || previous?.updatedAt !== fields.updatedAt ) return fields; const known = previous?.state === "open" ? previous.mergeability : undefined; return known === undefined || known === "unknown" ? fields : { ...fields, mergeability: known }; } // Rotating ghe.com avatar tokens should not make otherwise identical snapshots differ. function avatarIdentity(avatarUrl: string | null | undefined): string | null { if (avatarUrl === null || avatarUrl === undefined) return null; const url = URL.parse(avatarUrl); if (url === null || (url.hostname !== "ghe.com" && !url.hostname.endsWith(".ghe.com"))) { return avatarUrl; } url.searchParams.delete("token"); return url.href; } function snapshotFieldsEqual(left: SnapshotFields, right: SnapshotFields): boolean { return ( left.state === right.state && left.title === right.title && left.headBranch === right.headBranch && left.baseBranch === right.baseBranch && left.isDraft === right.isDraft && left.updatedAt === right.updatedAt && (left.closedAt ?? null) === (right.closedAt ?? null) && (left.mergedAt ?? null) === (right.mergedAt ?? null) && (left.author?.login ?? null) === (right.author?.login ?? null) && avatarIdentity(left.author?.avatarUrl) === avatarIdentity(right.author?.avatarUrl) && left.additions === right.additions && left.deletions === right.deletions && left.changedFiles === right.changedFiles && (left.reviewDecision ?? null) === (right.reviewDecision ?? null) && (left.checksState ?? null) === (right.checksState ?? null) && left.mergeability === right.mergeability ); } function stacksEqual( left: ThreadPullRequestStack | null, right: ThreadPullRequestStack | null, ): boolean { if (left === null || right === null) return left === right; return ( left.kind === right.kind && left.id === right.id && left.number === right.number && left.url === right.url && left.base === right.base && left.layers.length === right.layers.length && left.layers.every((layer, index) => { const other = right.layers[index]!; return ( layer.number === other.number && layer.headBranch === other.headBranch && layer.state === other.state ); }) ); } /** Stack ids are unique only within a repository, e.g. GitHub's fallback to the stack number. */ function nativeStackKeyOf(link: ThreadPullRequestLink, stack: ThreadPullRequestStack): string { const key = normalizeThreadPullRequestKey(link); return `${key.host}/${key.repository}#stack:${stack.id}`; } function sameLayerNumbers(left: ThreadPullRequestStack, right: ThreadPullRequestStack): boolean { return ( left.layers.length === right.layers.length && left.layers.every((layer, index) => layer.number === right.layers[index]!.number) ); } function skipReason(cause: Cause.Cause<unknown>): string { const error = Cause.squash(cause); return error instanceof Error ? error.message : String(error); } function rateLimitRetryAt(cause: Cause.Cause<unknown>): number | undefined { let error: unknown = Cause.squash(cause); while (error instanceof Error) { if (isPullRequestProviderError(error) && error.reason === "rate-limited") return error.retryAt; error = error.cause; } return undefined; } function isUnsettled(thread: ProjectionStore.ProjectionThreadPullRequests): boolean { return thread.settledOverride !== "settled" && thread.settledAt === null; } export class PullRequestSyncReactor extends Context.Service< PullRequestSyncReactor, { readonly start: () => Effect.Effect<void, never, Scope.Scope>; readonly drain: Effect.Effect<void>; /** Forces reread on the next sweep, including terminal snapshots; rate-limited hosts wait until the pause ends. */ readonly requestSync: (key: ThreadPullRequestKey) => Effect.Effect<void>; } >()("t3/orchestration-v2/PullRequestSyncReactor") {} /** @public */ export const make = Effect.gen(function* () { const engine = yield* Orchestrator.OrchestratorV2; const projections = yield* ProjectionStore.ProjectionStoreV2; const pullRequests = yield* PullRequestService.PullRequestService; const git = yield* GitManager.GitManager; const random = yield* SecureRandom.SecureRandom; const lastSyncedAt = new Map<string, number>(); const requested = new Set<string>(); // Requests arriving during a host read wait for the next sweep. const pendingRequests = new Set<string>(); // PR keys wait in `pendingRequests` for one queued sweep, so a burst of links (an agent // linking dozens of pull requests) is read together and shares the summary batches. let requestedSweepQueued = false; const retryStacks = new Set<string>(); // Pause per project and host because credentials are project-specific; leave paused PRs due until reset. const pausedUntil = new Map<string, number>(); const isDue = (key: string, entries: ReadonlyArray<LinkEntry>, nowMs: number): boolean => { if (requested.has(key) || retryStacks.has(key)) return true; if (entries.some((entry) => entry.link.snapshot === null)) return true; const active = entries.filter((entry) => isUnsettled(entry.thread)); // Merged snapshots without mergedAt cannot settle threads; reread slowly until supplied. const isFinal = ({ link: { snapshot } }: LinkEntry) => snapshot?.state === "merged" ? snapshot.mergedAt != null : snapshot?.state === "closed" && nowMs - Date.parse(snapshot.closedAt ?? "") >= CLOSED_FINAL_AFTER_MS; if (active.every(isFinal)) return false; const last = lastSyncedAt.get(key); const sinceLastMs = last === undefined ? Number.POSITIVE_INFINITY : nowMs - last; const open = active.filter((entry) => entry.link.snapshot?.state === "open"); if (open.length > 0) { const quiet = open.every((entry) => { const updatedAt = entry.link.snapshot?.updatedAt; return updatedAt != null && nowMs - Date.parse(updatedAt) >= QUIET_AFTER_MS; }); return !quiet || sinceLastMs >= QUIET_SYNC_INTERVAL_MS; } return sinceLastMs >= SLOW_SYNC_INTERVAL_MS; }; const logSkipped = (message: string, fields: Record<string, unknown>) => <E>(cause: Cause.Cause<E>): Effect.Effect<void, E> => Cause.hasInterruptsOnly(cause) ? Effect.failCause(cause) : Effect.logWarning(message, fields); const sweep = Effect.fn("PullRequestSyncReactor.sweep")(function* (scope: "all" | "requested") { const threads = yield* projections.getThreadsWithPullRequests(); const now = yield* DateTime.now; const nowMs = DateTime.toEpochMillis(now); const nowIso = DateTime.formatIso(now); const groups = new Map<string, Array<LinkEntry>>(); for (const thread of threads) { for (const link of visibleThreadPullRequests(thread.pullRequests ?? [])) { // Reads depend on the project's checkout and credentials, including their failures. const key = `${thread.projectId}\0${threadPullRequestKeyOf(link)}`; const entries = groups.get(key) ?? []; entries.push({ thread, link }); groups.set(key, entries); } } // A host-level refresh reaches every project, each retaining its own pending retry. for (const [key, entries] of groups) { if (pendingRequests.has(threadPullRequestKeyOf(entries[0]!.link))) requested.add(key); } pendingRequests.clear(); for (const key of lastSyncedAt.keys()) if (!groups.has(key)) lastSyncedAt.delete(key); for (const key of retryStacks) if (!groups.has(key)) retryStacks.delete(key); for (const key of requested.keys()) if (!groups.has(key)) requested.delete(key); const linkedThisSweep = new Set<string>(); // Fresh stack reads by group, so a member flagged by a sibling is compared against what the // host answered this sweep, not the saved layers that sibling is about to replace. const stacksReadThisSweep = new Map<string, ThreadPullRequestStack | null>(); const persistence = yield* Semaphore.make(1); // Effect.fn's stack frames close over these sweep-scoped bodies and their arguments, and a // cache lookup forked inside keeps those frames, so a plain span leaves the sweep collectable. const syncEntry = Effect.fnUntraced(function* ( entry: LinkEntry, hostFields: SnapshotFields, fetchedStack: { readonly stack: ThreadPullRequestStack | null } | null, provider: PullRequestSummary["provider"], ) { const { thread, link } = entry; const fields = withKnownMergeability(link.snapshot, hostFields, provider); const nextStack = fetchedStack === null ? link.stack : fetchedStack.stack; const changed = link.snapshot === null || !snapshotFieldsEqual(link.snapshot, fields) || !stacksEqual(link.stack, nextStack); // Persist siblings before terminal snapshots trigger settlement; settled threads get snapshots without new links. for (const layer of isUnsettled(thread) ? (fetchedStack?.stack?.layers ?? []) : []) { const layerKey = { host: normalizeThreadPullRequestKey(link).host, repository: link.repository, number: layer.number, }; const dedupeKey = `${thread.id}:${threadPullRequestKeyOf(layerKey)}`; if (linkedThisSweep.has(dedupeKey)) continue; // Tombstones count as present: a dismissed layer is never re-added. if ( (thread.pullRequests ?? []).some((existing) => threadPullRequestKeysEqual(existing, layerKey), ) ) { continue; } const url = siblingChangeRequestUrl(link.url, layer.number); if (url === null) continue; const uuid = yield* random.uuidV4(); yield* engine.dispatch({ type: "thread.pull-request.link", commandId: CommandId.make(`server:pr-stack-link:${thread.id}:${uuid}`), threadId: thread.id, ...layerKey, url, source: "stack", }); linkedThisSweep.add(dedupeKey); } if (changed) { const uuid = yield* random.uuidV4(); yield* engine.dispatch({ type: "thread.pull-request-link.sync", commandId: CommandId.make(`server:pr-sync:${thread.id}:${uuid}`), threadId: thread.id, host: normalizeThreadPullRequestKey(link).host, repository: link.repository, number: link.number, snapshot: { ...fields, syncedAt: nowIso }, stack: nextStack, }); } }, Effect.withSpan("PullRequestSyncReactor.syncEntry")); const syncGroup = Effect.fnUntraced(function* (key: string, entries: ReadonlyArray<LinkEntry>) { const first = entries[0]!; const ref = { projectId: first.thread.projectId, host: normalizeThreadPullRequestKey(first.link).host, repository: first.link.repository, number: first.link.number, }; const refreshRequested = requested.has(key); // A held merged summary is served without asking the host, so a snapshot still missing its // merge time has to drop it or the slow-cadence re-read would hand back the same gap. const missingMergedAt = entries.some( (entry) => entry.link.snapshot?.state === "merged" && entry.link.snapshot.mergedAt == null, ); // A stack retry reads past the cache, or it could get back the layer list it is retrying. const invalidated = refreshRequested || missingMergedAt || retryStacks.has(key); if (invalidated) yield* pullRequests.invalidate({ reference: ref }); const summary = yield* pullRequests.summary(ref, { recoverTransientFailure: false }); const fields = snapshotFieldsOf(summary); const needsStack = refreshRequested || retryStacks.has(key) || entries.some( (entry) => entry.link.snapshot === null || !snapshotFieldsEqual( entry.link.snapshot, withKnownMergeability(entry.link.snapshot, fields, summary.provider), ) || (summary.stack !== undefined && (entry.link.stack?.number ?? null) !== (summary.stack?.number ?? null)), ); const knownUnstacked = summary.stack === null && entries.every((entry) => entry.link.stack === null); // A sibling can flag this group while its summary read is in flight. if (!invalidated && retryStacks.has(key) && !knownUnstacked) { yield* pullRequests.invalidate({ reference: ref }); } // The retry this read answers is consumed before the read, not after it: a sibling may // flag this group again while the read is in flight, and that flag must survive to the // next sweep because the answer below may predate the sibling's layers. if (needsStack) retryStacks.delete(key); const fetchedStack = !needsStack ? null : knownUnstacked ? { stack: null } : yield* pullRequests.stack(ref, { includeDetails: false }).pipe( Effect.map((stack) => ({ stack: stack === null ? null : ({ kind: "native", ...stack } as const), })), Effect.catchCauseIf( (cause) => !Cause.hasInterruptsOnly(cause), (cause) => rateLimitRetryAt(cause) !== undefined ? Effect.sync(() => retryStacks.add(key)).pipe( Effect.andThen(Effect.failCause(cause)), ) : Effect.logWarning("pull request stack lookup failed", { key, }).pipe(Effect.as(null)), ), ); if (needsStack) { if (fetchedStack === null) { retryStacks.add(key); return; } const fetched = fetchedStack.stack; stacksReadThisSweep.set(key, fetched); // New upper layers leave older members' fields unchanged; differing layer lists need a stack read. // Only flag siblings in the same project: different credentials can yield lists that never agree. if (fetched !== null) { const fetchedKey = nativeStackKeyOf(first.link, fetched); for (const [siblingKey, siblingEntries] of groups) { if (siblingKey === key) continue; if (siblingEntries[0]!.thread.projectId !== first.thread.projectId) continue; const readThisSweep = stacksReadThisSweep.get(siblingKey); const stale = siblingEntries.some(({ link }) => { const stack = readThisSweep === undefined ? link.stack : readThisSweep; return ( stack !== null && nativeStackKeyOf(link, stack) === fetchedKey && !sameLayerNumbers(stack, fetched) ); }); if (stale) retryStacks.add(siblingKey); } } } // The host answered, so the cadence clock ticks even if a dispatch below is rejected. lastSyncedAt.set(key, nowMs); requested.delete(key); yield* Effect.forEach( entries, (entry) => syncEntry(entry, fields, fetchedStack, summary.provider).pipe( persistence.withPermits(1), Effect.catchCause((cause) => { if (!Cause.hasInterruptsOnly(cause)) retryStacks.add(key); return logSkipped("pull request sync skipped", { threadId: entry.thread.id, key })( cause, ); }), ), { discard: true }, ); }, Effect.withSpan("PullRequestSyncReactor.syncGroup")); const skips = new Map<string, { count: number; readonly key: string }>(); const readGroup = (key: string, entries: ReadonlyArray<LinkEntry>, pauseKey: string) => syncGroup(key, entries).pipe( Effect.catchCause((cause) => { if (Cause.hasInterruptsOnly(cause)) return Effect.failCause(cause); const retryAt = rateLimitRetryAt(cause); if (retryAt !== undefined) { pausedUntil.set(pauseKey, Math.max(retryAt, pausedUntil.get(pauseKey) ?? 0)); } const reason = skipReason(cause); const skip = skips.get(reason); if (skip === undefined) skips.set(reason, { count: 1, key }); else skip.count += 1; return Effect.void; }), ); yield* Effect.forEach( groups, ([key, entries]) => { if (!((scope === "all" || requested.has(key)) && isDue(key, entries, nowMs))) { return Effect.void; } const first = entries[0]!; const pauseKey = `${first.thread.projectId}\0${normalizeThreadPullRequestKey(first.link).host}`; // Check pauses at each read so changes during the sweep apply immediately. return Clock.currentTimeMillis.pipe( Effect.flatMap((startedAtMs) => (pausedUntil.get(pauseKey) ?? 0) > startedAtMs ? Effect.void : readGroup(key, entries, pauseKey), ), ); }, // Match summary batch width so GitHub reads coalesce into one request. { concurrency: 25, discard: true }, ); for (const [reason, { count, key }] of skips) { yield* Effect.logWarning("pull request sync skipped", { count, key, reason }); } }); const worker = yield* makeDrainableWorker((scope: "all" | "requested") => Effect.suspend(() => { if (scope === "requested") requestedSweepQueued = false; return pullRequests.withSharedProjectReads(sweep(scope)); }).pipe(Effect.catchCause(logSkipped("pull request sync sweep failed", {}))), ); // Threads that ran a close command this run, with the commands already read for. const closeCommands = new Map<ThreadId, Set<string>>(); const refreshUnmergedLinks = (threadId: ThreadId) => projections.getThreadsWithPullRequests(threadId).pipe( Effect.flatMap((threads) => Effect.forEach( threads.flatMap((thread) => visibleThreadPullRequests(thread.pullRequests ?? []).filter( (link) => link.snapshot?.state === "open" || link.snapshot?.state === "closed", ), ), requestSync, { discard: true }, ), ), Effect.catchCause(logSkipped("pull request refresh after run skipped", { threadId })), ); // An agent that opens a pull request from a shell gets it linked as link_pull_request would. const linkCreatedChangeRequests = Effect.fn("PullRequestSyncReactor.linkCreatedChangeRequests")( function* (threadId: ThreadId, item: CommandExecution) { if (isOrchestrationV2WorkActive(item.status)) return; yield* Effect.forEach( changeRequestsCreatedBy(item.input, item.output ?? ""), (created) => engine .dispatch({ type: "thread.pull-request.link", // Stable per command, so a repeated settlement cannot relink one the user unlinked. commandId: CommandId.make( `server:pr-created-link:${threadId}:${item.id}:${threadPullRequestKeyOf(created)}`, ), threadId, ...created, source: "created", }) .pipe( Effect.catchCause( logSkipped("linking a pull request a command created failed", { threadId }), ), ), { discard: true }, ); }, ); const start: PullRequestSyncReactor["Service"]["start"] = Effect.fn( "PullRequestSyncReactor.start", )(function* () { const events = engine.streamDomainEvents; const stateChanges = Stream.merge( yield* pullRequests.subscribeStateChanges, yield* git.subscribePullRequestStateChanges, ); yield* forkParked( Stream.runForEach(stateChanges, requestSync).pipe( Effect.catchCause(logSkipped("pull request state change stream failed", {})), ), ); yield* forkParked( Stream.runForEach(events, (event) => { switch (event.type) { case "thread.pull-request-link-updated": return event.payload.snapshot === null ? requestSync(event.payload) : Effect.void; case "thread.pull-request-synced": return Effect.forEach( visibleThreadPullRequests(event.payload.pullRequests ?? []).filter( (link) => link.snapshot === null, ), requestSync, { discard: true }, ); // An agent can merge or close its pull request from a shell with its host's CLI, which // sends no merge notification. Read the thread's unmerged links fresh once that command // ends, while the agent usually still replies, so settlement can land before the run // ends. Read again when the run ends, since `merge --auto` returns before it merges. case "turn-item.updated": { const item = event.payload; if (item.type !== "command_execution") return Effect.void; const linked = linkCreatedChangeRequests(event.threadId, item); if (!isChangeRequestCloseCommand(item.input)) return linked; const ended = closeCommands.get(event.threadId) ?? new Set<string>(); closeCommands.set(event.threadId, ended); if (isOrchestrationV2WorkActive(item.status) || ended.has(item.id)) return linked; ended.add(item.id); return Effect.andThen(linked, refreshUnmergedLinks(event.threadId)); } case "run.updated": return isTerminalRunStatus(event.payload.status) && closeCommands.delete(event.threadId) ? refreshUnmergedLinks(event.threadId) : Effect.void; default: return Effect.void; } }).pipe(Effect.catchCause(logSkipped("pull request sync event stream failed", {}))), ); yield* forkParked( Effect.gen(function* () { yield* worker.enqueue("all"); yield* worker.drain; }).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.asVoid), ); }); const requestSync: PullRequestSyncReactor["Service"]["requestSync"] = (key) => Effect.suspend(() => { pendingRequests.add(threadPullRequestKeyOf(key)); if (requestedSweepQueued) return Effect.void; requestedSweepQueued = true; return worker.enqueue("requested"); }); return { start, drain: worker.drain, requestSync } satisfies PullRequestSyncReactor["Service"]; }); export const layer = Layer.effect(PullRequestSyncReactor, make);
apps/server/src/orchestration-v2/PullRequestSyncReactor.ts at main · sailfork/t3code-github · Sailfork
])/gu, \"$1\") ?? single ?? escaped ?? bare,\n ).join(\"\");\n}\n\n/** A script split at command separators that sit outside quotes. */\nfunction shellSegments(script: string): ReadonlyArray\u003cstring> {\n const segments: Array\u003cstring> = [];\n let current = \"\";\n let quote: string | null = null;\n for (let index = 0; index \u003c script.length; index++) {\n const char = script[index] ?? \"\";\n if (quote !== null) {\n current += char;\n if (char === \"\\\\\" && quote === '\"') current += script[++index] ?? \"\";\n else if (char === quote) quote = null;\n continue;\n }\n if (char === \"'\" || char === '\"') quote = char;\n if (char === \"\\\\\") {\n current += char + (script[++index] ?? \"\");\n continue;\n }\n const pair = script.slice(index, index + 2);\n if (pair === \"&&\" || pair === \"||\" || char === \";\" || char === \"|\" || char === \"\\n\") {\n segments.push(current);\n current = \"\";\n if (pair === \"&&\" || pair === \"||\") index++;\n continue;\n }\n current += char;\n }\n segments.push(current);\n return segments;\n}\n\n/** The commands a script runs, without the text it carries. */\nconst shellCode = (script: string) =>\n script.replace(HEREDOC_BODY, \"\u003c\u003c\").replace(QUOTED_OR_COMMENT, \"''\");\n\ntype CommandExecution = Extract\u003cOrchestrationV2TurnItem, { readonly type: \"command_execution\" }>;\n\n/**\n * The change requests a shell command opened: URLs on lines of their own in the output of a\n * command that runs a host's create command. One the command also names, as `gh pr view 12`\n * does, existed before it ran. More URLs than create commands means something else printed\n * some, such as `gh pr list`, so none are taken.\n */\nexport function changeRequestsCreatedBy(input: string, output: string) {\n if (!CREATE_COMMANDS.some(({ command }) => command.test(input))) return [];\n const script = shellScript(input);\n const code = shellCode(script);\n const creates = createCommandCount(code);\n if (creates === 0) return [];\n const otherCommands = code\n .split(COMMAND_SEPARATOR)\n .filter((part) => createCommandCount(part) === 0)\n .join(\"\\n\");\n if (LISTS_CHANGE_REQUESTS.test(otherCommands)) return [];\n const otherScript = shellSegments(script)\n .filter((segment) => createCommandCount(shellCode(segment)) === 0)\n .join(\"\\n\");\n const creators = new Set(\n CREATE_COMMANDS.filter(({ run }) => code.match(run) !== null).map(({ kind }) => kind),\n );\n const created = new Map\u003cstring, ThreadPullRequestKey & { readonly url: string }>();\n for (const [, url = \"\"] of output.matchAll(URL_LINE)) {\n const parsed = sourceControlDefinitions.parseChangeRequestUrl(url);\n // Only the host whose create command ran can have printed a new change request.\n if (parsed === null || !creators.has(parsed.definition.kind)) continue;\n const { link } = parsed;\n const named = new RegExp(`(?:^|(?\u003c=[\\\\s#/=(\"']))${link.number}(?=$|[\\\\s);&|/\"'])`, \"u\");\n // Quoted text in the other commands counts, as in `printf '.../pull/12'`.\n if (named.test(otherScript)) continue;\n const key = normalizeThreadPullRequestKey(link);\n created.set(threadPullRequestKeyOf(key), { ...key, url });\n }\n return created.size > creates ? [] : [...created.values()];\n}\n\nconst isPullRequestProviderError = Schema.is(PullRequestProviderError);\n\ntype SnapshotFields = Omit\u003cThreadPullRequestSnapshot, \"syncedAt\">;\n\ninterface LinkEntry {\n readonly thread: ProjectionStore.ProjectionThreadPullRequests;\n readonly link: ThreadPullRequestLink;\n}\n\n/** Another layer of the stack `url` belongs to, in its host's canonical shape. */\nfunction siblingChangeRequestUrl(url: string, number: number): string | null {\n if (!Number.isSafeInteger(number) || number \u003c 1) return null;\n const parsed = sourceControlDefinitions.parseChangeRequestUrl(url);\n if (parsed === null) return null;\n const { definition, link } = parsed;\n return definition.changeRequestUrl({\n host: link.host,\n repository: link.repository,\n number,\n webUrl: link.repositoryUrl,\n });\n}\n\nfunction snapshotFieldsOf(summary: PullRequestSummary): SnapshotFields {\n return {\n state: summary.state,\n title: summary.title,\n headBranch: summary.headBranch,\n baseBranch: summary.baseBranch,\n isDraft: summary.isDraft ?? false,\n updatedAt: summary.updatedAt,\n closedAt: summary.closedAt ?? null,\n mergedAt: summary.mergedAt ?? null,\n ...(summary.author === undefined ? {} : { author: summary.author }),\n ...(summary.additions === undefined ? {} : { additions: summary.additions }),\n ...(summary.deletions === undefined ? {} : { deletions: summary.deletions }),\n ...(summary.changedFiles === undefined ? {} : { changedFiles: summary.changedFiles }),\n ...(summary.reviewDecision === undefined ? {} : { reviewDecision: summary.reviewDecision }),\n ...(summary.checksState === undefined ? {} : { checksState: summary.checksState }),\n ...(summary.mergeability === undefined ? {} : { mergeability: summary.mergeability }),\n };\n}\n\n// GitHub briefly returns unknown after base pushes; other hosts may mean a lasting condition.\nfunction withKnownMergeability(\n previous: ThreadPullRequestSnapshot | null,\n fields: SnapshotFields,\n provider: PullRequestSummary[\"provider\"],\n): SnapshotFields {\n if (\n provider !== \"github\" ||\n fields.state !== \"open\" ||\n fields.mergeability !== \"unknown\" ||\n previous?.updatedAt !== fields.updatedAt\n )\n return fields;\n const known = previous?.state === \"open\" ? previous.mergeability : undefined;\n return known === undefined || known === \"unknown\" ? fields : { ...fields, mergeability: known };\n}\n\n// Rotating ghe.com avatar tokens should not make otherwise identical snapshots differ.\nfunction avatarIdentity(avatarUrl: string | null | undefined): string | null {\n if (avatarUrl === null || avatarUrl === undefined) return null;\n const url = URL.parse(avatarUrl);\n if (url === null || (url.hostname !== \"ghe.com\" && !url.hostname.endsWith(\".ghe.com\"))) {\n return avatarUrl;\n }\n url.searchParams.delete(\"token\");\n return url.href;\n}\n\nfunction snapshotFieldsEqual(left: SnapshotFields, right: SnapshotFields): boolean {\n return (\n left.state === right.state &&\n left.title === right.title &&\n left.headBranch === right.headBranch &&\n left.baseBranch === right.baseBranch &&\n left.isDraft === right.isDraft &&\n left.updatedAt === right.updatedAt &&\n (left.closedAt ?? null) === (right.closedAt ?? null) &&\n (left.mergedAt ?? null) === (right.mergedAt ?? null) &&\n (left.author?.login ?? null) === (right.author?.login ?? null) &&\n avatarIdentity(left.author?.avatarUrl) === avatarIdentity(right.author?.avatarUrl) &&\n left.additions === right.additions &&\n left.deletions === right.deletions &&\n left.changedFiles === right.changedFiles &&\n (left.reviewDecision ?? null) === (right.reviewDecision ?? null) &&\n (left.checksState ?? null) === (right.checksState ?? null) &&\n left.mergeability === right.mergeability\n );\n}\n\nfunction stacksEqual(\n left: ThreadPullRequestStack | null,\n right: ThreadPullRequestStack | null,\n): boolean {\n if (left === null || right === null) return left === right;\n return (\n left.kind === right.kind &&\n left.id === right.id &&\n left.number === right.number &&\n left.url === right.url &&\n left.base === right.base &&\n left.layers.length === right.layers.length &&\n left.layers.every((layer, index) => {\n const other = right.layers[index]!;\n return (\n layer.number === other.number &&\n layer.headBranch === other.headBranch &&\n layer.state === other.state\n );\n })\n );\n}\n\n/** Stack ids are unique only within a repository, e.g. GitHub's fallback to the stack number. */\nfunction nativeStackKeyOf(link: ThreadPullRequestLink, stack: ThreadPullRequestStack): string {\n const key = normalizeThreadPullRequestKey(link);\n return `${key.host}/${key.repository}#stack:${stack.id}`;\n}\n\nfunction sameLayerNumbers(left: ThreadPullRequestStack, right: ThreadPullRequestStack): boolean {\n return (\n left.layers.length === right.layers.length &&\n left.layers.every((layer, index) => layer.number === right.layers[index]!.number)\n );\n}\n\nfunction skipReason(cause: Cause.Cause\u003cunknown>): string {\n const error = Cause.squash(cause);\n return error instanceof Error ? error.message : String(error);\n}\n\nfunction rateLimitRetryAt(cause: Cause.Cause\u003cunknown>): number | undefined {\n let error: unknown = Cause.squash(cause);\n while (error instanceof Error) {\n if (isPullRequestProviderError(error) && error.reason === \"rate-limited\") return error.retryAt;\n error = error.cause;\n }\n return undefined;\n}\n\nfunction isUnsettled(thread: ProjectionStore.ProjectionThreadPullRequests): boolean {\n return thread.settledOverride !== \"settled\" && thread.settledAt === null;\n}\n\nexport class PullRequestSyncReactor extends Context.Service\u003c\n PullRequestSyncReactor,\n {\n readonly start: () => Effect.Effect\u003cvoid, never, Scope.Scope>;\n readonly drain: Effect.Effect\u003cvoid>;\n /** Forces reread on the next sweep, including terminal snapshots; rate-limited hosts wait until the pause ends. */\n readonly requestSync: (key: ThreadPullRequestKey) => Effect.Effect\u003cvoid>;\n }\n>()(\"t3/orchestration-v2/PullRequestSyncReactor\") {}\n\n/** @public */\nexport const make = Effect.gen(function* () {\n const engine = yield* Orchestrator.OrchestratorV2;\n const projections = yield* ProjectionStore.ProjectionStoreV2;\n const pullRequests = yield* PullRequestService.PullRequestService;\n const git = yield* GitManager.GitManager;\n const random = yield* SecureRandom.SecureRandom;\n\n const lastSyncedAt = new Map\u003cstring, number>();\n const requested = new Set\u003cstring>();\n // Requests arriving during a host read wait for the next sweep.\n const pendingRequests = new Set\u003cstring>();\n // PR keys wait in `pendingRequests` for one queued sweep, so a burst of links (an agent\n // linking dozens of pull requests) is read together and shares the summary batches.\n let requestedSweepQueued = false;\n const retryStacks = new Set\u003cstring>();\n // Pause per project and host because credentials are project-specific; leave paused PRs due until reset.\n const pausedUntil = new Map\u003cstring, number>();\n\n const isDue = (key: string, entries: ReadonlyArray\u003cLinkEntry>, nowMs: number): boolean => {\n if (requested.has(key) || retryStacks.has(key)) return true;\n if (entries.some((entry) => entry.link.snapshot === null)) return true;\n const active = entries.filter((entry) => isUnsettled(entry.thread));\n // Merged snapshots without mergedAt cannot settle threads; reread slowly until supplied.\n const isFinal = ({ link: { snapshot } }: LinkEntry) =>\n snapshot?.state === \"merged\"\n ? snapshot.mergedAt != null\n : snapshot?.state === \"closed\" &&\n nowMs - Date.parse(snapshot.closedAt ?? \"\") >= CLOSED_FINAL_AFTER_MS;\n if (active.every(isFinal)) return false;\n const last = lastSyncedAt.get(key);\n const sinceLastMs = last === undefined ? Number.POSITIVE_INFINITY : nowMs - last;\n const open = active.filter((entry) => entry.link.snapshot?.state === \"open\");\n if (open.length > 0) {\n const quiet = open.every((entry) => {\n const updatedAt = entry.link.snapshot?.updatedAt;\n return updatedAt != null && nowMs - Date.parse(updatedAt) >= QUIET_AFTER_MS;\n });\n return !quiet || sinceLastMs >= QUIET_SYNC_INTERVAL_MS;\n }\n return sinceLastMs >= SLOW_SYNC_INTERVAL_MS;\n };\n\n const logSkipped =\n (message: string, fields: Record\u003cstring, unknown>) =>\n \u003cE>(cause: Cause.Cause\u003cE>): Effect.Effect\u003cvoid, E> =>\n Cause.hasInterruptsOnly(cause) ? Effect.failCause(cause) : Effect.logWarning(message, fields);\n\n const sweep = Effect.fn(\"PullRequestSyncReactor.sweep\")(function* (scope: \"all\" | \"requested\") {\n const threads = yield* projections.getThreadsWithPullRequests();\n const now = yield* DateTime.now;\n const nowMs = DateTime.toEpochMillis(now);\n const nowIso = DateTime.formatIso(now);\n\n const groups = new Map\u003cstring, Array\u003cLinkEntry>>();\n for (const thread of threads) {\n for (const link of visibleThreadPullRequests(thread.pullRequests ?? [])) {\n // Reads depend on the project's checkout and credentials, including their failures.\n const key = `${thread.projectId}\\0${threadPullRequestKeyOf(link)}`;\n const entries = groups.get(key) ?? [];\n entries.push({ thread, link });\n groups.set(key, entries);\n }\n }\n\n // A host-level refresh reaches every project, each retaining its own pending retry.\n for (const [key, entries] of groups) {\n if (pendingRequests.has(threadPullRequestKeyOf(entries[0]!.link))) requested.add(key);\n }\n pendingRequests.clear();\n\n for (const key of lastSyncedAt.keys()) if (!groups.has(key)) lastSyncedAt.delete(key);\n for (const key of retryStacks) if (!groups.has(key)) retryStacks.delete(key);\n for (const key of requested.keys()) if (!groups.has(key)) requested.delete(key);\n\n const linkedThisSweep = new Set\u003cstring>();\n // Fresh stack reads by group, so a member flagged by a sibling is compared against what the\n // host answered this sweep, not the saved layers that sibling is about to replace.\n const stacksReadThisSweep = new Map\u003cstring, ThreadPullRequestStack | null>();\n const persistence = yield* Semaphore.make(1);\n\n // Effect.fn's stack frames close over these sweep-scoped bodies and their arguments, and a\n // cache lookup forked inside keeps those frames, so a plain span leaves the sweep collectable.\n const syncEntry = Effect.fnUntraced(function* (\n entry: LinkEntry,\n hostFields: SnapshotFields,\n fetchedStack: { readonly stack: ThreadPullRequestStack | null } | null,\n provider: PullRequestSummary[\"provider\"],\n ) {\n const { thread, link } = entry;\n const fields = withKnownMergeability(link.snapshot, hostFields, provider);\n const nextStack = fetchedStack === null ? link.stack : fetchedStack.stack;\n const changed =\n link.snapshot === null ||\n !snapshotFieldsEqual(link.snapshot, fields) ||\n !stacksEqual(link.stack, nextStack);\n // Persist siblings before terminal snapshots trigger settlement; settled threads get snapshots without new links.\n for (const layer of isUnsettled(thread) ? (fetchedStack?.stack?.layers ?? []) : []) {\n const layerKey = {\n host: normalizeThreadPullRequestKey(link).host,\n repository: link.repository,\n number: layer.number,\n };\n const dedupeKey = `${thread.id}:${threadPullRequestKeyOf(layerKey)}`;\n if (linkedThisSweep.has(dedupeKey)) continue;\n // Tombstones count as present: a dismissed layer is never re-added.\n if (\n (thread.pullRequests ?? []).some((existing) =>\n threadPullRequestKeysEqual(existing, layerKey),\n )\n ) {\n continue;\n }\n const url = siblingChangeRequestUrl(link.url, layer.number);\n if (url === null) continue;\n const uuid = yield* random.uuidV4();\n yield* engine.dispatch({\n type: \"thread.pull-request.link\",\n commandId: CommandId.make(`server:pr-stack-link:${thread.id}:${uuid}`),\n threadId: thread.id,\n ...layerKey,\n url,\n source: \"stack\",\n });\n linkedThisSweep.add(dedupeKey);\n }\n if (changed) {\n const uuid = yield* random.uuidV4();\n yield* engine.dispatch({\n type: \"thread.pull-request-link.sync\",\n commandId: CommandId.make(`server:pr-sync:${thread.id}:${uuid}`),\n threadId: thread.id,\n host: normalizeThreadPullRequestKey(link).host,\n repository: link.repository,\n number: link.number,\n snapshot: { ...fields, syncedAt: nowIso },\n stack: nextStack,\n });\n }\n }, Effect.withSpan(\"PullRequestSyncReactor.syncEntry\"));\n\n const syncGroup = Effect.fnUntraced(function* (key: string, entries: ReadonlyArray\u003cLinkEntry>) {\n const first = entries[0]!;\n const ref = {\n projectId: first.thread.projectId,\n host: normalizeThreadPullRequestKey(first.link).host,\n repository: first.link.repository,\n number: first.link.number,\n };\n const refreshRequested = requested.has(key);\n // A held merged summary is served without asking the host, so a snapshot still missing its\n // merge time has to drop it or the slow-cadence re-read would hand back the same gap.\n const missingMergedAt = entries.some(\n (entry) => entry.link.snapshot?.state === \"merged\" && entry.link.snapshot.mergedAt == null,\n );\n // A stack retry reads past the cache, or it could get back the layer list it is retrying.\n const invalidated = refreshRequested || missingMergedAt || retryStacks.has(key);\n if (invalidated) yield* pullRequests.invalidate({ reference: ref });\n const summary = yield* pullRequests.summary(ref, { recoverTransientFailure: false });\n const fields = snapshotFieldsOf(summary);\n const needsStack =\n refreshRequested ||\n retryStacks.has(key) ||\n entries.some(\n (entry) =>\n entry.link.snapshot === null ||\n !snapshotFieldsEqual(\n entry.link.snapshot,\n withKnownMergeability(entry.link.snapshot, fields, summary.provider),\n ) ||\n (summary.stack !== undefined &&\n (entry.link.stack?.number ?? null) !== (summary.stack?.number ?? null)),\n );\n const knownUnstacked =\n summary.stack === null && entries.every((entry) => entry.link.stack === null);\n // A sibling can flag this group while its summary read is in flight.\n if (!invalidated && retryStacks.has(key) && !knownUnstacked) {\n yield* pullRequests.invalidate({ reference: ref });\n }\n // The retry this read answers is consumed before the read, not after it: a sibling may\n // flag this group again while the read is in flight, and that flag must survive to the\n // next sweep because the answer below may predate the sibling's layers.\n if (needsStack) retryStacks.delete(key);\n const fetchedStack = !needsStack\n ? null\n : knownUnstacked\n ? { stack: null }\n : yield* pullRequests.stack(ref, { includeDetails: false }).pipe(\n Effect.map((stack) => ({\n stack: stack === null ? null : ({ kind: \"native\", ...stack } as const),\n })),\n Effect.catchCauseIf(\n (cause) => !Cause.hasInterruptsOnly(cause),\n (cause) =>\n rateLimitRetryAt(cause) !== undefined\n ? Effect.sync(() => retryStacks.add(key)).pipe(\n Effect.andThen(Effect.failCause(cause)),\n )\n : Effect.logWarning(\"pull request stack lookup failed\", {\n key,\n }).pipe(Effect.as(null)),\n ),\n );\n if (needsStack) {\n if (fetchedStack === null) {\n retryStacks.add(key);\n return;\n }\n const fetched = fetchedStack.stack;\n stacksReadThisSweep.set(key, fetched);\n // New upper layers leave older members' fields unchanged; differing layer lists need a stack read.\n // Only flag siblings in the same project: different credentials can yield lists that never agree.\n if (fetched !== null) {\n const fetchedKey = nativeStackKeyOf(first.link, fetched);\n for (const [siblingKey, siblingEntries] of groups) {\n if (siblingKey === key) continue;\n if (siblingEntries[0]!.thread.projectId !== first.thread.projectId) continue;\n const readThisSweep = stacksReadThisSweep.get(siblingKey);\n const stale = siblingEntries.some(({ link }) => {\n const stack = readThisSweep === undefined ? link.stack : readThisSweep;\n return (\n stack !== null &&\n nativeStackKeyOf(link, stack) === fetchedKey &&\n !sameLayerNumbers(stack, fetched)\n );\n });\n if (stale) retryStacks.add(siblingKey);\n }\n }\n }\n // The host answered, so the cadence clock ticks even if a dispatch below is rejected.\n lastSyncedAt.set(key, nowMs);\n requested.delete(key);\n yield* Effect.forEach(\n entries,\n (entry) =>\n syncEntry(entry, fields, fetchedStack, summary.provider).pipe(\n persistence.withPermits(1),\n Effect.catchCause((cause) => {\n if (!Cause.hasInterruptsOnly(cause)) retryStacks.add(key);\n return logSkipped(\"pull request sync skipped\", { threadId: entry.thread.id, key })(\n cause,\n );\n }),\n ),\n { discard: true },\n );\n }, Effect.withSpan(\"PullRequestSyncReactor.syncGroup\"));\n\n const skips = new Map\u003cstring, { count: number; readonly key: string }>();\n const readGroup = (key: string, entries: ReadonlyArray\u003cLinkEntry>, pauseKey: string) =>\n syncGroup(key, entries).pipe(\n Effect.catchCause((cause) => {\n if (Cause.hasInterruptsOnly(cause)) return Effect.failCause(cause);\n const retryAt = rateLimitRetryAt(cause);\n if (retryAt !== undefined) {\n pausedUntil.set(pauseKey, Math.max(retryAt, pausedUntil.get(pauseKey) ?? 0));\n }\n const reason = skipReason(cause);\n const skip = skips.get(reason);\n if (skip === undefined) skips.set(reason, { count: 1, key });\n else skip.count += 1;\n return Effect.void;\n }),\n );\n yield* Effect.forEach(\n groups,\n ([key, entries]) => {\n if (!((scope === \"all\" || requested.has(key)) && isDue(key, entries, nowMs))) {\n return Effect.void;\n }\n const first = entries[0]!;\n const pauseKey = `${first.thread.projectId}\\0${normalizeThreadPullRequestKey(first.link).host}`;\n // Check pauses at each read so changes during the sweep apply immediately.\n return Clock.currentTimeMillis.pipe(\n Effect.flatMap((startedAtMs) =>\n (pausedUntil.get(pauseKey) ?? 0) > startedAtMs\n ? Effect.void\n : readGroup(key, entries, pauseKey),\n ),\n );\n },\n // Match summary batch width so GitHub reads coalesce into one request.\n { concurrency: 25, discard: true },\n );\n for (const [reason, { count, key }] of skips) {\n yield* Effect.logWarning(\"pull request sync skipped\", { count, key, reason });\n }\n });\n\n const worker = yield* makeDrainableWorker((scope: \"all\" | \"requested\") =>\n Effect.suspend(() => {\n if (scope === \"requested\") requestedSweepQueued = false;\n return pullRequests.withSharedProjectReads(sweep(scope));\n }).pipe(Effect.catchCause(logSkipped(\"pull request sync sweep failed\", {}))),\n );\n\n // Threads that ran a close command this run, with the commands already read for.\n const closeCommands = new Map\u003cThreadId, Set\u003cstring>>();\n const refreshUnmergedLinks = (threadId: ThreadId) =>\n projections.getThreadsWithPullRequests(threadId).pipe(\n Effect.flatMap((threads) =>\n Effect.forEach(\n threads.flatMap((thread) =>\n visibleThreadPullRequests(thread.pullRequests ?? []).filter(\n (link) => link.snapshot?.state === \"open\" || link.snapshot?.state === \"closed\",\n ),\n ),\n requestSync,\n { discard: true },\n ),\n ),\n Effect.catchCause(logSkipped(\"pull request refresh after run skipped\", { threadId })),\n );\n\n // An agent that opens a pull request from a shell gets it linked as link_pull_request would.\n const linkCreatedChangeRequests = Effect.fn(\"PullRequestSyncReactor.linkCreatedChangeRequests\")(\n function* (threadId: ThreadId, item: CommandExecution) {\n if (isOrchestrationV2WorkActive(item.status)) return;\n yield* Effect.forEach(\n changeRequestsCreatedBy(item.input, item.output ?? \"\"),\n (created) =>\n engine\n .dispatch({\n type: \"thread.pull-request.link\",\n // Stable per command, so a repeated settlement cannot relink one the user unlinked.\n commandId: CommandId.make(\n `server:pr-created-link:${threadId}:${item.id}:${threadPullRequestKeyOf(created)}`,\n ),\n threadId,\n ...created,\n source: \"created\",\n })\n .pipe(\n Effect.catchCause(\n logSkipped(\"linking a pull request a command created failed\", { threadId }),\n ),\n ),\n { discard: true },\n );\n },\n );\n\n const start: PullRequestSyncReactor[\"Service\"][\"start\"] = Effect.fn(\n \"PullRequestSyncReactor.start\",\n )(function* () {\n const events = engine.streamDomainEvents;\n const stateChanges = Stream.merge(\n yield* pullRequests.subscribeStateChanges,\n yield* git.subscribePullRequestStateChanges,\n );\n yield* forkParked(\n Stream.runForEach(stateChanges, requestSync).pipe(\n Effect.catchCause(logSkipped(\"pull request state change stream failed\", {})),\n ),\n );\n yield* forkParked(\n Stream.runForEach(events, (event) => {\n switch (event.type) {\n case \"thread.pull-request-link-updated\":\n return event.payload.snapshot === null ? requestSync(event.payload) : Effect.void;\n case \"thread.pull-request-synced\":\n return Effect.forEach(\n visibleThreadPullRequests(event.payload.pullRequests ?? []).filter(\n (link) => link.snapshot === null,\n ),\n requestSync,\n { discard: true },\n );\n // An agent can merge or close its pull request from a shell with its host's CLI, which\n // sends no merge notification. Read the thread's unmerged links fresh once that command\n // ends, while the agent usually still replies, so settlement can land before the run\n // ends. Read again when the run ends, since `merge --auto` returns before it merges.\n case \"turn-item.updated\": {\n const item = event.payload;\n if (item.type !== \"command_execution\") return Effect.void;\n const linked = linkCreatedChangeRequests(event.threadId, item);\n if (!isChangeRequestCloseCommand(item.input)) return linked;\n const ended = closeCommands.get(event.threadId) ?? new Set\u003cstring>();\n closeCommands.set(event.threadId, ended);\n if (isOrchestrationV2WorkActive(item.status) || ended.has(item.id)) return linked;\n ended.add(item.id);\n return Effect.andThen(linked, refreshUnmergedLinks(event.threadId));\n }\n case \"run.updated\":\n return isTerminalRunStatus(event.payload.status) && closeCommands.delete(event.threadId)\n ? refreshUnmergedLinks(event.threadId)\n : Effect.void;\n default:\n return Effect.void;\n }\n }).pipe(Effect.catchCause(logSkipped(\"pull request sync event stream failed\", {}))),\n );\n yield* forkParked(\n Effect.gen(function* () {\n yield* worker.enqueue(\"all\");\n yield* worker.drain;\n }).pipe(Effect.repeat(Schedule.spaced(\"1 minute\")), Effect.asVoid),\n );\n });\n\n const requestSync: PullRequestSyncReactor[\"Service\"][\"requestSync\"] = (key) =>\n Effect.suspend(() => {\n pendingRequests.add(threadPullRequestKeyOf(key));\n if (requestedSweepQueued) return Effect.void;\n requestedSweepQueued = true;\n return worker.enqueue(\"requested\");\n });\n\n return { start, drain: worker.drain, requestSync } satisfies PullRequestSyncReactor[\"Service\"];\n});\n\nexport const layer = Layer.effect(PullRequestSyncReactor, make);\n","preview":null,"history":null,"version":"9dffe4babb42402292908347ac692a1fd7d97017"},"/_session?shell=1":null}