import dedent from "dedent"; /** * What this run continues, said as the three states a run can be in. A thread * round knows its history from the checkpoint: "Since your last round" is in * the task when there is one, so the prompt only names the state and the * reading order. A first thread round and a standalone run share the rule that * there is nothing to catch up on; the thread one adds that the thread starts * with this answer. */ export function thread(options: ThreadOptions): string { const parts = [dedent` The task is the request for this run. An optional is the user's latest instruction: answer it while keeping the task in scope. `]; if (options.threadMode) { parts.push(dedent` \`--thread\` names the conversation, not a live session. When configured, new PR commits or replies trigger separate one-shot messages in that conversation. Finish this run and exit; never poll or wait for future commits, replies or approval. `); } parts.push(threadState(options)); return parts.join("\n\n"); } function threadState(options: ThreadOptions): string { if (options.threadMode === "continuing") { return dedent` This is a later round in a thread. Catch up first: read what moved before anything else, and never repeat an answer that is still visible in the thread. If present, "Since your last round" below lists what moved since you last answered; read it before anything else. Otherwise read the earlier answer and current thread from the host. `; } if (options.threadMode === "first") { return dedent` This is the first message in this thread: no earlier answer of yours exists there, and there is no "Since your last round" to catch up on. Later rounds will continue from what you write now. There is nothing to catch up on: start the task directly. `; } return dedent` This is the first and only message of the run: there is no earlier round, no thread history, and no "Since your last round" to catch up on. There is nothing to catch up on: start the task directly. `; } export type ThreadMode = "first" | "continuing"; export interface ThreadOptions { /** * Where this run sits in its thread. `continuing` is a later round with * history to catch up on; `first` opens a thread; undefined is a standalone * run with no thread at all. */ threadMode?: ThreadMode; } /** * How a host collapses a comment. The round is the same everywhere, so a host * adds one line of mechanism, never a round of its own. * * The host comes from the environment, never from the remote: a remote says * where the code lives, CI variables say we have a thread to answer in. */ const HOSTS = { github: { env: ['GITHUB_ACTIONS'], collapse: dedent` Collapse with the \`minimizeComment\` mutation, classifier \`OUTDATED\`; \`isMinimized\` reports it. Read \`body\`, not \`body_text\` — that one strips the marker. Never \`--edit-last\` or \`--delete-last\`: in CI that is a bot account shared with every other tool here.`, }, gitlab: { env: ['GITLAB_CI'], collapse: dedent` Notes cannot collapse, only be rewritten, so keep one note: current answer on top, the previous one under it in \`
\`. One generation, never a chain. \`$GITLAB_TOKEN\` needs \`api\` scope — a blocker when empty. Notes, not discussions.`, }, bitbucket: { env: ['BITBUCKET_BUILD_NUMBER'], collapse: dedent` Collapse by resolving the comment thread; \`resolved\` reports it. Top-level comments only, a reply answers 403. \`$BITBUCKET_ACCESS_TOKEN\` — a blocker when empty. Deleted comments linger as tombstones, so filter \`deleted=false\`. The cap is 200 comments.`, }, generic: { env: [], collapse: dedent` Use whatever hides a whole comment here — hide, minimise, collapse, delete, resolve — and read back whatever field reports it. If the host can only edit, keep one comment and rewrite it with the previous answer folded inside. If it can do neither, say so in your output.`, }, } satisfies Record; /** * Which host this job belongs to. `generic` when it is CI and nothing claims it, * nothing at all when it is not CI — a developer's own checkout has no thread. */ export function threadHost(env: Environment): ThreadHost | null { for (const [name, host] of Object.entries(HOSTS)) { if (host.env.some((variable) => env[variable])) return name as ThreadHost; } if (env.CI) return 'generic'; return null; } /** What a comment of this thread opens with, and so what identifies it. */ export function threadMarker(thread: string): string { return `