import type { WorkspaceMergeResult } from "agent-relay-sdk"; // #638 — settle a `workspace.merge` command on whether it made progress, not on whether // the merge function threw. Every no-progress outcome (origin moved ahead, an unpredicted // replay conflict, a push race) returns merged:false with an `error` set; every genuine // land / recycle / noop-resolve / PR-opened leaves `error` undefined. Reporting `succeeded` // for an unlanded branch is what let the auto-merge job believe it was progressing and // busy-loop the repo's merge lease. Mirrors the pr-merge / pr-arm settle contract. export function mergeCommandStatus(result: WorkspaceMergeResult): "succeeded" | "failed" { return result.error ? "failed" : "succeeded"; } // Command PATCH bodies are capped at 64 KiB by the relay. A failing full land gate // may retain 256 KiB of output for artifact upload, so forwarding it verbatim can // reject the *terminal* update and leave expiry to misreport a red gate as a timeout. // Keep ordinary output intact; when it will not fit, retain the bounded tail that is // already intended for the worker/coordinator notification. const MAX_COMMAND_REPORT_CHARS = 60 * 1024; export function prepareWorkspaceMergeResultForReport(result: WorkspaceMergeResult): WorkspaceMergeResult { const gateFailure = result.gateFailure; if (!gateFailure?.output) return result; const body = JSON.stringify({ status: mergeCommandStatus(result), result, error: result.error }); if (body.length <= MAX_COMMAND_REPORT_CHARS) return result; return { ...result, gateFailure: { ...gateFailure, output: gateFailure.outputTail, }, }; }