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((definition) => { const command = definition.changeRequestCreateCommand; return command === undefined ? [] : // The subcommand must end its shell word: `gh pr create#x` runs another one. [ { definition, 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