@cat-factory/executor-harness 1.60.0 → 1.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -1
- package/dist/agent.js +9 -3
- package/dist/coding-agent.js +32 -4
- package/dist/git.js +0 -319
- package/dist/host-markdown.js +142 -0
- package/dist/pr-description.js +157 -0
- package/dist/vcs-api.js +402 -0
- package/package.json +4 -3
- package/src/agent.ts +8 -3
- package/src/coding-agent.ts +44 -2
- package/src/git.ts +1 -385
- package/src/host-markdown.ts +155 -0
- package/src/pr-description.ts +171 -0
- package/src/vcs-api.ts +512 -0
package/README.md
CHANGED
|
@@ -73,7 +73,16 @@ The implementation job (`POST /run`) is the canonical sequence:
|
|
|
73
73
|
6. **commit, push** a branch and **open a PR**, returning `{ prUrl, branch, summary }` — but
|
|
74
74
|
ONLY if step 4 ended green. A spent budget returns an error result with the validation report
|
|
75
75
|
and opens no PR. Absent `validationChecks` / `reproduction`, steps 4 and 5 do not happen at
|
|
76
|
-
all.
|
|
76
|
+
all. The PR's description prefers the agent-authored reviewer briefing over the generic
|
|
77
|
+
dispatch-time text the job body carries: a PR-opening agent is prompted to write one to the
|
|
78
|
+
`.cat-pr-description.md` sentinel at the checkout root (one per sibling repo in a multi-repo
|
|
79
|
+
run; an optional leading `# <title>` line, when it is the file's only `#` heading, sets the PR
|
|
80
|
+
title), and `src/pr-description.ts` lifts it — secret-scrubbed, size-capped with a visible
|
|
81
|
+
note, made inert for the host by `src/host-markdown.ts`, kept out of the commit like the
|
|
82
|
+
effort/follow-ups sentinels — onto `openPullRequest`. Absent or unusable ⇒ the fallback text,
|
|
83
|
+
unchanged. On a RESUMED run the PR already exists, so an agent briefing additionally refreshes
|
|
84
|
+
its title/description in place (carrying the engine's managed report region across); the
|
|
85
|
+
generic fallback never does, so a human's edit is safe.
|
|
77
86
|
|
|
78
87
|
Bootstrap differs at the ends — it may start from an empty dir, and **resets
|
|
79
88
|
history to one commit and force-pushes** the default branch instead of opening a
|
package/dist/agent.js
CHANGED
|
@@ -6,7 +6,9 @@ import { promisify } from 'node:util';
|
|
|
6
6
|
import { standUpFrontend, tearDownFrontend } from './frontend-infra.js';
|
|
7
7
|
import { configurePackageRegistries } from './package-registries.js';
|
|
8
8
|
import { captureRedactedOutput, redactSecrets, registerKnownSecrets } from './redact.js';
|
|
9
|
-
import { cloneRepo, commitAll, conflictDiff, fetchPullRequestHead, fetchReferenceBranches, hasAgentChanges, headCommit,
|
|
9
|
+
import { cloneRepo, commitAll, conflictDiff, fetchPullRequestHead, fetchReferenceBranches, hasAgentChanges, headCommit, mergeBranch, prepareExistingCheckout, pushBranch, reinitAndPush, unmergedPaths, } from './git.js';
|
|
10
|
+
import { inferVcsProvider, openPullRequest } from './vcs-api.js';
|
|
11
|
+
import { applyPrDescription } from './pr-description.js';
|
|
10
12
|
import { makeDirClaimer, noChangesReason, runCodingAgent, runMultiRepoCoding, } from './coding-agent.js';
|
|
11
13
|
import { validationFailureMessage } from './validation-checks.js';
|
|
12
14
|
import { acquireRepoCheckout, agentNeverActed, agentOutputTail, NEVER_ACTED_CAUSE, runAgentInWorkspace, unusableFinalAnswerCause, withWorkspace, } from './pi-workspace.js';
|
|
@@ -822,7 +824,7 @@ function buildSingleRepoCodingSpec(job, pushBranch) {
|
|
|
822
824
|
*/
|
|
823
825
|
async function runSingleRepoCoding(job, opts) {
|
|
824
826
|
const pushBranch = job.pushBranch ?? job.newBranch ?? job.branch;
|
|
825
|
-
const { summary, stats, stderrTail, pushed, usage, callMetrics, validation, validationReport, reproductionReport, effortReport, } = await runCodingAgent(buildSingleRepoCodingSpec(job, pushBranch), opts);
|
|
827
|
+
const { summary, stats, stderrTail, pushed, usage, callMetrics, validation, validationReport, reproductionReport, effortReport, prDescription, } = await runCodingAgent(buildSingleRepoCodingSpec(job, pushBranch), opts);
|
|
826
828
|
// Ralph loop: the harness-computed validation verdict, forwarded onto the coding result as
|
|
827
829
|
// `ralphVerdict` so the backend's `toRunResult` lifts it onto `AgentRunResult.ralphVerdict`.
|
|
828
830
|
const ralphVerdict = validation ? { ralphVerdict: validation } : {};
|
|
@@ -894,7 +896,11 @@ async function runSingleRepoCoding(job, opts) {
|
|
|
894
896
|
ghToken: job.ghToken,
|
|
895
897
|
head: pushBranch,
|
|
896
898
|
base: job.repo.baseBranch,
|
|
897
|
-
|
|
899
|
+
// The agent-authored briefing (title/body) wins field-wise over the dispatch-time text.
|
|
900
|
+
pr: applyPrDescription(job.pr, prDescription),
|
|
901
|
+
// A resumed run's PR is already open, so refresh it rather than lose the briefing to the
|
|
902
|
+
// duplicate-PR 422 — only from a REAL briefing (see `refreshExisting` for why).
|
|
903
|
+
...(prDescription ? { refreshExisting: true } : {}),
|
|
898
904
|
apiBase: job.githubApiBase,
|
|
899
905
|
// The provider (set by the server from the configured backend) selects GitHub-PR vs
|
|
900
906
|
// GitLab-MR authoritatively; the clone URL supplies the GitLab REST base + project path.
|
package/dist/coding-agent.js
CHANGED
|
@@ -3,9 +3,11 @@ import { join } from 'node:path';
|
|
|
3
3
|
import { spawn } from 'node:child_process';
|
|
4
4
|
import { killChildProcess, spawnDetached } from './process.js';
|
|
5
5
|
import { MAX_CAPTURED_OUTPUT_CHARS, redactSecrets } from './redact.js';
|
|
6
|
-
import { branchAheadOfBase, changedFilesSinceBase, branchHasCommitsSince, cloneExistingBranch, cloneRepo, commitTrackedEdits, createBranch, excludeFromGit, fetchReferenceBranches, headCommit, listUntrackedFiles,
|
|
6
|
+
import { branchAheadOfBase, changedFilesSinceBase, branchHasCommitsSince, cloneExistingBranch, cloneRepo, commitTrackedEdits, createBranch, excludeFromGit, fetchReferenceBranches, headCommit, listUntrackedFiles, prepareExistingCheckout, pushBranch, refreshFromBaseIfClean, remoteBranchExists, } from './git.js';
|
|
7
|
+
import { openPullRequest } from './vcs-api.js';
|
|
7
8
|
import { FOLLOW_UPS_FILENAME, FollowUpTailer } from './follow-ups.js';
|
|
8
9
|
import { EFFORT_REPORT_FILE } from './effort.js';
|
|
10
|
+
import { applyPrDescription, PR_DESCRIPTION_FILE, readPrDescription, } from './pr-description.js';
|
|
9
11
|
import { acquireRepoCheckout, agentNeverActed, agentOutputTail, runAgentInWorkspace, withWorkspace, } from './pi-workspace.js';
|
|
10
12
|
import { log } from './logger.js';
|
|
11
13
|
import { runValidationLoop, } from './validation-checks.js';
|
|
@@ -115,6 +117,9 @@ export async function runCodingAgent(spec, opts = {}) {
|
|
|
115
117
|
// but that cannot un-stage a mid-run commit; the per-clone exclude is what prevents it. A bare
|
|
116
118
|
// filename pattern matches the file in any subdirectory, so it covers a monorepo `workDir` too.
|
|
117
119
|
await excludeFromGit(dir, EFFORT_REPORT_FILE, signal);
|
|
120
|
+
// Same treatment for the agent-authored PR-description sentinel: excluded locally so the
|
|
121
|
+
// agent's own `git add` can never stage the briefing into the PR it describes.
|
|
122
|
+
await excludeFromGit(dir, PR_DESCRIPTION_FILE, signal);
|
|
118
123
|
// Follow-up companion: tail the Coder's sentinel file and stream new items out on the
|
|
119
124
|
// job view. Locally exclude it from git first so the agent's own `git add` can never
|
|
120
125
|
// stage it and it never surfaces as an untracked leftover or in the PR. The sentinel
|
|
@@ -376,6 +381,12 @@ async function finalizeCodingRun(args) {
|
|
|
376
381
|
// Safety net for forgotten edits: commit changes to TRACKED files only (never
|
|
377
382
|
// untracked scratch files/artifacts — the agent owns committing new files).
|
|
378
383
|
await commitTrackedEdits(dir, spec.commitMessage, signal);
|
|
384
|
+
// The agent-authored PR description, read AFTER the validation loop (a repair round may have
|
|
385
|
+
// changed what the briefing should say) and removed so it never lingers in the checkout. The
|
|
386
|
+
// prompt asks for it at the top level of the checkout; a monorepo agent working in a service
|
|
387
|
+
// subdirectory may drop it in its cwd instead, so probe the checkout root first, then the cwd.
|
|
388
|
+
const prDescription = (await readPrDescription(dir)) ??
|
|
389
|
+
(workDir !== dir ? await readPrDescription(workDir) : undefined);
|
|
379
390
|
// Stop periodic checkpoints and let any in-flight one settle BEFORE the final
|
|
380
391
|
// push, so the two never run a concurrent `git push` to the same branch (the
|
|
381
392
|
// final push below is then a fresh attempt whose failure is the real signal).
|
|
@@ -438,6 +449,7 @@ async function finalizeCodingRun(args) {
|
|
|
438
449
|
...(usage ? { usage } : {}),
|
|
439
450
|
...(callMetrics ? { callMetrics } : {}),
|
|
440
451
|
...(effortReport ? { effortReport } : {}),
|
|
452
|
+
...(prDescription ? { prDescription } : {}),
|
|
441
453
|
};
|
|
442
454
|
}
|
|
443
455
|
// Ralph loop: run the programmatic completion command against the pushed/committed
|
|
@@ -698,7 +710,7 @@ export async function runMultiRepoCoding(job, opts = {}) {
|
|
|
698
710
|
multiRepo: true,
|
|
699
711
|
}, opts);
|
|
700
712
|
// Commit forgotten tracked edits, then push + open a PR for each repo the run actually changed.
|
|
701
|
-
const { primaryPushed, primaryPrUrl, peerPullRequests } = await pushMultiRepoLegs(legs, job, logger, opts);
|
|
713
|
+
const { primaryPushed, primaryPrUrl, peerPullRequests } = await pushMultiRepoLegs(legs, job, logger, opts, root);
|
|
702
714
|
const anyWork = primaryPushed || peerPullRequests.length > 0;
|
|
703
715
|
if (!anyWork) {
|
|
704
716
|
// Nothing changed in ANY repo. For the implementer this is a failure (as in the
|
|
@@ -799,6 +811,9 @@ async function prepareMultiRepoCheckouts(root, legs, job, logger, opts) {
|
|
|
799
811
|
await createBranch(dir, leg.workBranch, signal);
|
|
800
812
|
}
|
|
801
813
|
leg.dir = dir;
|
|
814
|
+
// Exclude the agent-authored PR-description sentinel locally (as the single-repo path does)
|
|
815
|
+
// so the agent's own `git add` can never stage the briefing into the PR it describes.
|
|
816
|
+
await excludeFromGit(dir, PR_DESCRIPTION_FILE, signal);
|
|
802
817
|
// The branch tip before the agent runs. Captured BEFORE the resume base refresh below so
|
|
803
818
|
// that refresh's merge commit counts as advancement and is pushed (as in the single-repo
|
|
804
819
|
// path). A fresh leg produced work iff its branch advances past this; a resumed leg already
|
|
@@ -844,7 +859,9 @@ async function prepareMultiRepoCheckouts(root, legs, job, logger, opts) {
|
|
|
844
859
|
* no PR; a read-only reference leg is never committed or pushed). Extracted so the multi-repo body
|
|
845
860
|
* stays small; returns the primary's push/PR state plus the peer PRs.
|
|
846
861
|
*/
|
|
847
|
-
async function pushMultiRepoLegs(legs, job, logger, opts
|
|
862
|
+
async function pushMultiRepoLegs(legs, job, logger, opts,
|
|
863
|
+
/** The workspace root the agent ran in — the fallback probe for the primary's briefing. */
|
|
864
|
+
root) {
|
|
848
865
|
const { signal } = opts;
|
|
849
866
|
opts.onPhase?.('push');
|
|
850
867
|
let primaryPushed = false;
|
|
@@ -855,6 +872,14 @@ async function pushMultiRepoLegs(legs, job, logger, opts) {
|
|
|
855
872
|
// guarantee (the spec carries no branch/PR, and the clone phase gave it no work branch).
|
|
856
873
|
if (leg.readOnly)
|
|
857
874
|
continue;
|
|
875
|
+
// Lift (and remove) the agent-authored PR description for THIS repo's PR before anything
|
|
876
|
+
// else touches the checkout — each sibling checkout carries its own briefing for its own PR.
|
|
877
|
+
// The agent's cwd here is the WORKSPACE ROOT rather than any one checkout, so an agent that
|
|
878
|
+
// read the prompt loosely may well have written a single briefing there instead. Fall back
|
|
879
|
+
// to it for the PRIMARY leg only: at the root there is nothing to say which repo it
|
|
880
|
+
// describes, and the primary is the one the run is actually about.
|
|
881
|
+
const agentPrDescription = (await readPrDescription(leg.dir)) ??
|
|
882
|
+
(leg.primary ? await readPrDescription(root) : undefined);
|
|
858
883
|
await commitTrackedEdits(leg.dir, job.commitMessage ?? leg.pr?.title ?? 'Agent changes', signal);
|
|
859
884
|
const advanced = await branchHasCommitsSince(leg.dir, leg.baseSha, signal);
|
|
860
885
|
let hasWork = advanced || leg.resumed;
|
|
@@ -884,7 +909,10 @@ async function pushMultiRepoLegs(legs, job, logger, opts) {
|
|
|
884
909
|
ghToken: leg.ghToken,
|
|
885
910
|
head: leg.workBranch,
|
|
886
911
|
base: leg.repo.baseBranch,
|
|
887
|
-
pr: leg.pr,
|
|
912
|
+
pr: applyPrDescription(leg.pr, agentPrDescription),
|
|
913
|
+
// See the single-repo call site: refresh a resumed leg's already-open PR, but only
|
|
914
|
+
// when the text is the agent's own briefing rather than the dispatch-time fallback.
|
|
915
|
+
...(agentPrDescription ? { refreshExisting: true } : {}),
|
|
888
916
|
apiBase: job.githubApiBase,
|
|
889
917
|
cloneUrl: leg.repo.cloneUrl,
|
|
890
918
|
...(leg.repo.provider ? { provider: leg.repo.provider } : {}),
|
package/dist/git.js
CHANGED
|
@@ -132,40 +132,6 @@ export function describeGitFailure(stderr) {
|
|
|
132
132
|
}
|
|
133
133
|
return undefined;
|
|
134
134
|
}
|
|
135
|
-
/**
|
|
136
|
-
* Classify a PR/MR-open REST failure by its HTTP status into an actionable remedy, else
|
|
137
|
-
* undefined (an unmapped status keeps just the raw `Failed to open … (HTTP n)` line). Like
|
|
138
|
-
* {@link describeGitFailure} this only APPENDS a cause + fix — the raw status line is
|
|
139
|
-
* load-bearing detail and stays. `provider` tailors the scope/permission wording (GitHub App
|
|
140
|
-
* Pull-requests permission / `repo` PAT scope vs GitLab `api` scope) and the noun (pull
|
|
141
|
-
* request vs merge request). Pure, so it is unit-tested per status.
|
|
142
|
-
*/
|
|
143
|
-
export function describePrOpenFailure(status, provider) {
|
|
144
|
-
const noun = provider === 'gitlab' ? 'merge request' : 'pull request';
|
|
145
|
-
if (status === 401) {
|
|
146
|
-
return (`The credential was rejected while opening the ${noun}. The GitHub App installation token ` +
|
|
147
|
-
'(or, in local mode, the GITHUB_PAT) is most likely expired, rotated, or revoked — reconnect ' +
|
|
148
|
-
'the GitHub App for the workspace (or regenerate the PAT), then retry.');
|
|
149
|
-
}
|
|
150
|
-
if (status === 403) {
|
|
151
|
-
const scope = provider === 'gitlab'
|
|
152
|
-
? 'the GitLab token needs the `api` scope and Developer+ access to the project'
|
|
153
|
-
: 'the GitHub App needs the "Pull requests: write" permission (or the PAT the `repo` scope) and write access to the repository';
|
|
154
|
-
return `The credential lacks permission to open a ${noun}: ${scope}. Grant it, then retry.`;
|
|
155
|
-
}
|
|
156
|
-
if (status === 404) {
|
|
157
|
-
return (`The repository could not be found while opening the ${noun} — it may have been deleted, ` +
|
|
158
|
-
'renamed, or made private, or the credential can no longer see it. Confirm the repo and the ' +
|
|
159
|
-
"credential's access to it, then retry.");
|
|
160
|
-
}
|
|
161
|
-
if (status === 422 || status === 400) {
|
|
162
|
-
return (`GitHub/GitLab rejected the ${noun} as invalid. Usually the head or base branch does not ` +
|
|
163
|
-
'exist, the two branches are identical (nothing to compare), or the base branch is protected ' +
|
|
164
|
-
'against direct PRs. Check the branch names and that the head has commits ahead of the base, ' +
|
|
165
|
-
'then retry.');
|
|
166
|
-
}
|
|
167
|
-
return undefined;
|
|
168
|
-
}
|
|
169
135
|
/**
|
|
170
136
|
* Wrap a git failure into a credential-scrubbed {@link HarnessFailure}('git') with an ACCURATE
|
|
171
137
|
* message. Three cases the old bare "Command failed: git …" collapsed together:
|
|
@@ -878,288 +844,3 @@ export async function reinitAndPush(opts) {
|
|
|
878
844
|
env: await authEnv(opts.ghToken),
|
|
879
845
|
});
|
|
880
846
|
}
|
|
881
|
-
/**
|
|
882
|
-
* The VCS host a clone URL points at. The harness is otherwise provider-agnostic (its git
|
|
883
|
-
* auth is a host-neutral GIT_ASKPASS credential), but the "open the PR/MR" REST call is not:
|
|
884
|
-
* GitHub and GitLab have different endpoints, so infer which to call from the host. GitHub is
|
|
885
|
-
* the default; a host of `gitlab.com` or one in the `gitlab.*` / `*.gitlab.*` family (covering
|
|
886
|
-
* self-managed instances named that way) is treated as GitLab.
|
|
887
|
-
*/
|
|
888
|
-
export function inferVcsProvider(cloneUrl) {
|
|
889
|
-
let host = '';
|
|
890
|
-
try {
|
|
891
|
-
host = new URL(cloneUrl).host.toLowerCase();
|
|
892
|
-
}
|
|
893
|
-
catch {
|
|
894
|
-
return 'github';
|
|
895
|
-
}
|
|
896
|
-
if (host === 'gitlab.com' || host.startsWith('gitlab.') || host.includes('.gitlab.')) {
|
|
897
|
-
return 'gitlab';
|
|
898
|
-
}
|
|
899
|
-
return 'github';
|
|
900
|
-
}
|
|
901
|
-
/** The GitLab REST v4 base for a clone URL's host, e.g. `https://gitlab.com/api/v4`. */
|
|
902
|
-
export function gitlabApiBaseFromCloneUrl(cloneUrl) {
|
|
903
|
-
const u = new URL(cloneUrl);
|
|
904
|
-
return `${u.protocol}//${u.host}/api/v4`;
|
|
905
|
-
}
|
|
906
|
-
/**
|
|
907
|
-
* The URL-encoded GitLab project path from a clone URL — the full namespace path (so subgroups
|
|
908
|
-
* survive), with the trailing `.git` stripped, e.g.
|
|
909
|
-
* `https://gitlab.com/group/sub/proj.git` → `group%2Fsub%2Fproj`.
|
|
910
|
-
*/
|
|
911
|
-
export function gitlabProjectPath(cloneUrl) {
|
|
912
|
-
const path = new URL(cloneUrl).pathname.replace(/^\/+/, '').replace(/\.git$/, '');
|
|
913
|
-
return encodeURIComponent(path);
|
|
914
|
-
}
|
|
915
|
-
/** The abort reason as an Error (the watchdog aborts with one), or a generic fallback. */
|
|
916
|
-
function abortError(signal) {
|
|
917
|
-
return signal.reason instanceof Error ? signal.reason : new Error('aborted');
|
|
918
|
-
}
|
|
919
|
-
/** Whether a thrown fetch error is an AbortError (caller-initiated, never retried). */
|
|
920
|
-
function isAbortError(err) {
|
|
921
|
-
return err instanceof Error && err.name === 'AbortError';
|
|
922
|
-
}
|
|
923
|
-
/**
|
|
924
|
-
* Parse a `Retry-After` header into ms, bounded so it can't stall the job. Accepts BOTH
|
|
925
|
-
* forms the spec allows: integer delay-seconds (`120`) and an HTTP-date (`Wed, 21 Oct 2026
|
|
926
|
-
* 07:28:00 GMT`); the latter is turned into a delay from now. A past/zero/unparseable value
|
|
927
|
-
* yields undefined so the caller falls back to exponential backoff.
|
|
928
|
-
*/
|
|
929
|
-
function retryAfterMs(res) {
|
|
930
|
-
const raw = res.headers.get('retry-after');
|
|
931
|
-
if (!raw)
|
|
932
|
-
return undefined;
|
|
933
|
-
const secs = Number(raw);
|
|
934
|
-
if (Number.isFinite(secs)) {
|
|
935
|
-
return secs > 0 ? Math.min(secs * 1000, MAX_RETRY_AFTER_MS) : undefined;
|
|
936
|
-
}
|
|
937
|
-
const at = Date.parse(raw);
|
|
938
|
-
if (Number.isNaN(at))
|
|
939
|
-
return undefined;
|
|
940
|
-
const ms = at - Date.now();
|
|
941
|
-
return ms > 0 ? Math.min(ms, MAX_RETRY_AFTER_MS) : undefined;
|
|
942
|
-
}
|
|
943
|
-
/** Sleep `ms`, rejecting immediately (with the abort reason) if `signal` aborts meanwhile. */
|
|
944
|
-
function abortableDelay(ms, signal) {
|
|
945
|
-
return new Promise((resolve, reject) => {
|
|
946
|
-
if (signal?.aborted)
|
|
947
|
-
return reject(abortError(signal));
|
|
948
|
-
const onAbort = () => {
|
|
949
|
-
clearTimeout(timer);
|
|
950
|
-
reject(abortError(signal));
|
|
951
|
-
};
|
|
952
|
-
const timer = setTimeout(() => {
|
|
953
|
-
signal?.removeEventListener('abort', onAbort);
|
|
954
|
-
resolve();
|
|
955
|
-
}, ms);
|
|
956
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
957
|
-
});
|
|
958
|
-
}
|
|
959
|
-
const MAX_RETRY_AFTER_MS = 8_000;
|
|
960
|
-
const RETRY_BASE_MS = 500;
|
|
961
|
-
const RETRY_MAX_DELAY_MS = 4_000;
|
|
962
|
-
/**
|
|
963
|
-
* Run a single HTTP request with bounded retry for TRANSIENT failures, so a momentary
|
|
964
|
-
* upstream blip (a 5xx, a 429 rate-limit, or a dropped connection) no longer fails an
|
|
965
|
-
* otherwise-complete run on its very last step (opening the PR/MR). Up to 3 attempts
|
|
966
|
-
* (2 retries) with exponential backoff + jitter (honoring a `Retry-After` on a 429),
|
|
967
|
-
* every wait abort-aware so the inactivity/max-duration watchdog still cancels promptly.
|
|
968
|
-
*
|
|
969
|
-
* ONLY transient failures retry: a `>=500`/`429` response, or a network-level fetch
|
|
970
|
-
* rejection. A 4xx (incl. the 422/409 "already exists" the callers treat as success) is
|
|
971
|
-
* returned to the caller unretried, and a caller abort is rethrown at once. The response
|
|
972
|
-
* body is never read here, so the caller's existing status handling is unchanged.
|
|
973
|
-
*/
|
|
974
|
-
async function withApiRetry(fn, opts = {}) {
|
|
975
|
-
const maxAttempts = opts.attempts ?? 3;
|
|
976
|
-
let lastError;
|
|
977
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
978
|
-
if (opts.signal?.aborted)
|
|
979
|
-
throw abortError(opts.signal);
|
|
980
|
-
let res;
|
|
981
|
-
try {
|
|
982
|
-
res = await fn();
|
|
983
|
-
}
|
|
984
|
-
catch (err) {
|
|
985
|
-
// A caller/watchdog abort is terminal; a network error is transient → retry.
|
|
986
|
-
if (isAbortError(err) || opts.signal?.aborted)
|
|
987
|
-
throw err;
|
|
988
|
-
lastError = err;
|
|
989
|
-
}
|
|
990
|
-
if (res) {
|
|
991
|
-
const transient = res.status >= 500 || res.status === 429;
|
|
992
|
-
if (!transient || attempt >= maxAttempts)
|
|
993
|
-
return res;
|
|
994
|
-
const after = retryAfterMs(res);
|
|
995
|
-
// Discard the unread body before retrying so the connection can be reused.
|
|
996
|
-
await res.body?.cancel().catch(() => { });
|
|
997
|
-
await abortableDelay(after ?? backoffMs(attempt), opts.signal);
|
|
998
|
-
continue;
|
|
999
|
-
}
|
|
1000
|
-
if (attempt >= maxAttempts)
|
|
1001
|
-
break;
|
|
1002
|
-
await abortableDelay(backoffMs(attempt), opts.signal);
|
|
1003
|
-
}
|
|
1004
|
-
// Exhausted on a network-level rejection (no HTTP response): an upstream API failure.
|
|
1005
|
-
const message = lastError instanceof Error ? lastError.message : 'API request failed after retries';
|
|
1006
|
-
throw new HarnessFailure('api', redactSecrets(message));
|
|
1007
|
-
}
|
|
1008
|
-
/** Exponential backoff (base 500ms, capped 4s) with up to 25% positive jitter. */
|
|
1009
|
-
function backoffMs(attempt) {
|
|
1010
|
-
const base = Math.min(RETRY_MAX_DELAY_MS, RETRY_BASE_MS * 2 ** (attempt - 1));
|
|
1011
|
-
return base + Math.floor(base * 0.25 * Math.random());
|
|
1012
|
-
}
|
|
1013
|
-
/**
|
|
1014
|
-
* Open a PR (GitHub) or merge request (GitLab) for the pushed branch; returns its web URL.
|
|
1015
|
-
* The provider is chosen from the EXPLICIT `opts.provider` when the dispatcher set it,
|
|
1016
|
-
* falling back to host inference from the clone URL only when it didn't — so a self-managed
|
|
1017
|
-
* GitLab whose host isn't named `gitlab.*` still opens an MR instead of being misrouted to
|
|
1018
|
-
* GitHub's API. The GitHub path is unchanged.
|
|
1019
|
-
*/
|
|
1020
|
-
export async function openPullRequest(opts) {
|
|
1021
|
-
const provider = opts.provider ?? (opts.cloneUrl ? inferVcsProvider(opts.cloneUrl) : 'github');
|
|
1022
|
-
if (provider === 'gitlab') {
|
|
1023
|
-
if (!opts.cloneUrl) {
|
|
1024
|
-
throw new Error('Cannot open a GitLab merge request without the repo clone URL');
|
|
1025
|
-
}
|
|
1026
|
-
return openGitLabMergeRequest({ ...opts, cloneUrl: opts.cloneUrl });
|
|
1027
|
-
}
|
|
1028
|
-
const apiBase = opts.apiBase ?? 'https://api.github.com';
|
|
1029
|
-
const path = `${encodeURIComponent(opts.owner)}/${encodeURIComponent(opts.name)}`;
|
|
1030
|
-
const res = await withApiRetry(() => fetch(`${apiBase}/repos/${path}/pulls`, {
|
|
1031
|
-
method: 'POST',
|
|
1032
|
-
headers: {
|
|
1033
|
-
authorization: `Bearer ${opts.ghToken}`,
|
|
1034
|
-
accept: 'application/vnd.github+json',
|
|
1035
|
-
'user-agent': 'cat-factory-executor',
|
|
1036
|
-
'x-github-api-version': '2022-11-28',
|
|
1037
|
-
'content-type': 'application/json',
|
|
1038
|
-
},
|
|
1039
|
-
body: JSON.stringify({
|
|
1040
|
-
title: opts.pr.title,
|
|
1041
|
-
head: opts.head,
|
|
1042
|
-
base: opts.base,
|
|
1043
|
-
body: opts.pr.body,
|
|
1044
|
-
}),
|
|
1045
|
-
// Bound on the watchdog so a hung GitHub call can't stall the job.
|
|
1046
|
-
...(opts.signal ? { signal: opts.signal } : {}),
|
|
1047
|
-
}), { signal: opts.signal });
|
|
1048
|
-
if (!res.ok) {
|
|
1049
|
-
const detail = await res.text().catch(() => '');
|
|
1050
|
-
// A resumed run pushes to a branch that already has an open PR; GitHub answers
|
|
1051
|
-
// 422 "A pull request already exists". That's success for us — return the
|
|
1052
|
-
// existing PR's url rather than failing the resumed run.
|
|
1053
|
-
if (res.status === 422 && /pull request already exists/i.test(detail)) {
|
|
1054
|
-
const existing = await findOpenPullRequestUrl(opts);
|
|
1055
|
-
if (existing)
|
|
1056
|
-
return existing;
|
|
1057
|
-
}
|
|
1058
|
-
// The head branch has nothing ahead of base ("No commits between <base> and <head>").
|
|
1059
|
-
// That is not an API failure — there is simply nothing to open a PR for (e.g. a resumed
|
|
1060
|
-
// branch whose earlier PR was merged with a merge commit, leaving the branch reachable
|
|
1061
|
-
// from base). Signal it with null so the caller records a clean no-op instead of failing
|
|
1062
|
-
// the run with GitHub's opaque 422.
|
|
1063
|
-
if (res.status === 422 && /no commits between/i.test(detail))
|
|
1064
|
-
return null;
|
|
1065
|
-
const remedy = describePrOpenFailure(res.status, 'github');
|
|
1066
|
-
const base = redactSecrets(`Failed to open PR (HTTP ${res.status}): ${detail.slice(0, 300)}`);
|
|
1067
|
-
throw new HarnessFailure('api', remedy ? `${base}\n${remedy}` : base);
|
|
1068
|
-
}
|
|
1069
|
-
const body = (await res.json());
|
|
1070
|
-
if (!body.html_url)
|
|
1071
|
-
throw new HarnessFailure('api', 'GitHub did not return a PR url');
|
|
1072
|
-
return body.html_url;
|
|
1073
|
-
}
|
|
1074
|
-
/** GitLab API headers for the PAT (the `PRIVATE-TOKEN` auth GitLab uses). */
|
|
1075
|
-
function gitlabHeaders(token) {
|
|
1076
|
-
return {
|
|
1077
|
-
'private-token': token,
|
|
1078
|
-
accept: 'application/json',
|
|
1079
|
-
'user-agent': 'cat-factory-executor',
|
|
1080
|
-
'content-type': 'application/json',
|
|
1081
|
-
};
|
|
1082
|
-
}
|
|
1083
|
-
/**
|
|
1084
|
-
* Open a GitLab merge request (the analogue of {@link openPullRequest} for GitLab). The REST
|
|
1085
|
-
* base + project path are derived from the clone URL's host, so it works for gitlab.com and a
|
|
1086
|
-
* self-managed instance alike. `head`→`source_branch`, `base`→`target_branch`. On a duplicate
|
|
1087
|
-
* (a resumed run whose branch already has an open MR — GitLab answers 409) the existing MR's
|
|
1088
|
-
* web URL is returned instead of failing the run, mirroring the GitHub 422 handling.
|
|
1089
|
-
*/
|
|
1090
|
-
async function openGitLabMergeRequest(opts) {
|
|
1091
|
-
const apiBase = gitlabApiBaseFromCloneUrl(opts.cloneUrl);
|
|
1092
|
-
const project = gitlabProjectPath(opts.cloneUrl);
|
|
1093
|
-
const res = await withApiRetry(() => fetch(`${apiBase}/projects/${project}/merge_requests`, {
|
|
1094
|
-
method: 'POST',
|
|
1095
|
-
headers: gitlabHeaders(opts.ghToken),
|
|
1096
|
-
body: JSON.stringify({
|
|
1097
|
-
source_branch: opts.head,
|
|
1098
|
-
target_branch: opts.base,
|
|
1099
|
-
title: opts.pr.title,
|
|
1100
|
-
description: opts.pr.body,
|
|
1101
|
-
}),
|
|
1102
|
-
...(opts.signal ? { signal: opts.signal } : {}),
|
|
1103
|
-
}), { signal: opts.signal });
|
|
1104
|
-
if (!res.ok) {
|
|
1105
|
-
const detail = await res.text().catch(() => '');
|
|
1106
|
-
// GitLab returns 409 (sometimes 400) when an open MR already exists for this source
|
|
1107
|
-
// branch; that is success for a resumed run — return the existing MR's url.
|
|
1108
|
-
if ((res.status === 409 || res.status === 400) &&
|
|
1109
|
-
/already exists|open merge request/i.test(detail)) {
|
|
1110
|
-
const existing = await findOpenMergeRequestUrl(apiBase, project, opts);
|
|
1111
|
-
if (existing)
|
|
1112
|
-
return existing;
|
|
1113
|
-
}
|
|
1114
|
-
const remedy = describePrOpenFailure(res.status, 'gitlab');
|
|
1115
|
-
const base = redactSecrets(`Failed to open merge request (HTTP ${res.status}): ${detail.slice(0, 300)}`);
|
|
1116
|
-
throw new HarnessFailure('api', remedy ? `${base}\n${remedy}` : base);
|
|
1117
|
-
}
|
|
1118
|
-
const body = (await res.json());
|
|
1119
|
-
if (!body.web_url)
|
|
1120
|
-
throw new HarnessFailure('api', 'GitLab did not return a merge request url');
|
|
1121
|
-
return body.web_url;
|
|
1122
|
-
}
|
|
1123
|
-
/** Find the open GitLab MR for `opts.head`→`opts.base`, returning its web_url or undefined. */
|
|
1124
|
-
async function findOpenMergeRequestUrl(apiBase, project, opts) {
|
|
1125
|
-
// Filter by BOTH branches: a source branch can have open MRs to several targets, so the
|
|
1126
|
-
// source alone could match an MR against a different base than the one we just tried to open.
|
|
1127
|
-
const query = new URLSearchParams({
|
|
1128
|
-
source_branch: opts.head,
|
|
1129
|
-
target_branch: opts.base,
|
|
1130
|
-
state: 'opened',
|
|
1131
|
-
});
|
|
1132
|
-
const res = await fetch(`${apiBase}/projects/${project}/merge_requests?${query}`, {
|
|
1133
|
-
headers: gitlabHeaders(opts.ghToken),
|
|
1134
|
-
...(opts.signal ? { signal: opts.signal } : {}),
|
|
1135
|
-
});
|
|
1136
|
-
if (!res.ok)
|
|
1137
|
-
return undefined;
|
|
1138
|
-
const list = (await res.json().catch(() => []));
|
|
1139
|
-
return Array.isArray(list) && list[0]?.web_url ? list[0].web_url : undefined;
|
|
1140
|
-
}
|
|
1141
|
-
/** Find the open PR for `opts.head` on `opts.base`, returning its html_url or undefined. */
|
|
1142
|
-
async function findOpenPullRequestUrl(opts) {
|
|
1143
|
-
const apiBase = opts.apiBase ?? 'https://api.github.com';
|
|
1144
|
-
// Encode the ref-derived query params: a branch/owner containing `&` or `#` would
|
|
1145
|
-
// otherwise split the query string or inject an unintended parameter.
|
|
1146
|
-
const query = new URLSearchParams({
|
|
1147
|
-
head: `${opts.owner}:${opts.head}`,
|
|
1148
|
-
base: opts.base,
|
|
1149
|
-
state: 'open',
|
|
1150
|
-
});
|
|
1151
|
-
const path = `${encodeURIComponent(opts.owner)}/${encodeURIComponent(opts.name)}`;
|
|
1152
|
-
const res = await fetch(`${apiBase}/repos/${path}/pulls?${query}`, {
|
|
1153
|
-
headers: {
|
|
1154
|
-
authorization: `Bearer ${opts.ghToken}`,
|
|
1155
|
-
accept: 'application/vnd.github+json',
|
|
1156
|
-
'user-agent': 'cat-factory-executor',
|
|
1157
|
-
'x-github-api-version': '2022-11-28',
|
|
1158
|
-
},
|
|
1159
|
-
...(opts.signal ? { signal: opts.signal } : {}),
|
|
1160
|
-
});
|
|
1161
|
-
if (!res.ok)
|
|
1162
|
-
return undefined;
|
|
1163
|
-
const list = (await res.json().catch(() => []));
|
|
1164
|
-
return Array.isArray(list) && list[0]?.html_url ? list[0].html_url : undefined;
|
|
1165
|
-
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// The TEXT BOUNDARY for agent-authored text the harness writes onto a VCS host.
|
|
3
|
+
//
|
|
4
|
+
// A pull-request description is NOT an inert string sink. The host parses it: `#123` becomes
|
|
5
|
+
// an issue link, `@name` notifies a real person, a closing keyword in front of an issue
|
|
6
|
+
// reference CLOSES that issue when the PR merges, and an unbalanced code fence swallows
|
|
7
|
+
// everything rendered after it — including the fenced JSON block the engine later appends as
|
|
8
|
+
// the verification report's machine-readable contract.
|
|
9
|
+
//
|
|
10
|
+
// The agent's reviewer briefing (`pr-description.ts`) is model-authored prose that lands
|
|
11
|
+
// verbatim on that surface, so it crosses this boundary first. "This closes #42" is idiomatic
|
|
12
|
+
// for a briefing to emit and must not close issue 42; "@alice owns the rounding rule" is
|
|
13
|
+
// idiomatic and must not page whoever holds that handle.
|
|
14
|
+
//
|
|
15
|
+
// This is a deliberate COPY of `hostMarkdown` in `@cat-factory/kernel`
|
|
16
|
+
// (`src/shared/host-markdown.logic.ts`), for the same reason `isSafeTestPath` is copied: the
|
|
17
|
+
// container image is built from `src/` plus typescript alone (the Dockerfile cannot resolve a
|
|
18
|
+
// `workspace:*` dependency), so the harness carries no runtime dependency on any package here.
|
|
19
|
+
// `test/host-markdown.conformity.test.ts` pins the two implementations to byte-identical
|
|
20
|
+
// output over a shared corpus, so the copy cannot drift — change one, change the other.
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
/**
|
|
23
|
+
* The host's closing keywords. A PR body carrying one of these in front of an issue reference
|
|
24
|
+
* closes that issue on merge — a side effect the harness must never trigger on the agent's
|
|
25
|
+
* behalf. Same list on GitHub and GitLab.
|
|
26
|
+
*/
|
|
27
|
+
const CLOSING_KEYWORDS = 'close[sd]?|closing|fix|fixe[sd]|fixing|resolve[sd]?|resolving|implement(?:s|ed)?|implementing';
|
|
28
|
+
/** An issue/MR URL on either host, in the form a closing keyword can reference. */
|
|
29
|
+
const ISSUE_URL = String.raw `https?://\S+?/(?:issues|-/issues|merge_requests|pull)/\d+`;
|
|
30
|
+
/**
|
|
31
|
+
* Every auto-linking trigger, in ONE alternation.
|
|
32
|
+
*
|
|
33
|
+
* Deliberately a single pass rather than chained `.replace()` calls: each escape EMITS a `#`,
|
|
34
|
+
* so a later rule would re-escape the output of an earlier one (`@` → `@` → `&#64;`).
|
|
35
|
+
* One regex means the replacement text is never rescanned.
|
|
36
|
+
*/
|
|
37
|
+
const AUTO_LINK_TRIGGERS = new RegExp([
|
|
38
|
+
// A closing keyword in front of an issue/MR URL. The URL form survives the character
|
|
39
|
+
// escapes below (nothing in it is a trigger), so the KEYWORD is what gets defused.
|
|
40
|
+
String.raw `(?<keyword>\b(?:${CLOSING_KEYWORDS}))(?=\s*:?\s+${ISSUE_URL})`,
|
|
41
|
+
// `@name` / `@org/team` — a mention notifies a real account.
|
|
42
|
+
String.raw `(?<at>@(?=[A-Za-z0-9]))`,
|
|
43
|
+
// `#123` and `owner/repo#123` — an issue/PR cross-reference.
|
|
44
|
+
String.raw `(?<hash>#(?=\d))`,
|
|
45
|
+
// `!123` — GitLab's merge-request reference.
|
|
46
|
+
String.raw `(?<bang>!(?=\d))`,
|
|
47
|
+
].join('|'), 'gi');
|
|
48
|
+
/**
|
|
49
|
+
* Neutralise the host's auto-linking triggers in ONE line of untrusted text, leaving inline
|
|
50
|
+
* code spans alone (the host does not auto-link inside them, so escaping there would only
|
|
51
|
+
* show the reader a literal `#`).
|
|
52
|
+
*
|
|
53
|
+
* The escapes are numeric HTML entities, which render as the original character but are
|
|
54
|
+
* invisible to the reference parser — so the reader sees exactly what the agent wrote while
|
|
55
|
+
* the mention/close side effects are defused.
|
|
56
|
+
*/
|
|
57
|
+
function inertLine(line) {
|
|
58
|
+
return mapOutsideCodeSpans(line, (text) => text.replace(AUTO_LINK_TRIGGERS, (match, ...args) => {
|
|
59
|
+
const groups = args[args.length - 1];
|
|
60
|
+
// Entity-escaping the FIRST character is enough to break the parser's match while
|
|
61
|
+
// rendering identically — which matters most for the keyword, whose remaining letters
|
|
62
|
+
// are ordinary prose the reader should still see.
|
|
63
|
+
return `&#${match.charCodeAt(0)};${groups.keyword ? match.slice(1) : ''}`;
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Apply `fn` to the parts of `line` that are NOT inline code spans. Code spans are matched by
|
|
68
|
+
* a backtick run and its matching closer, which is CommonMark's rule and — more to the point
|
|
69
|
+
* — the rule the host renderer applies when deciding where to auto-link.
|
|
70
|
+
*/
|
|
71
|
+
function mapOutsideCodeSpans(line, fn) {
|
|
72
|
+
const out = [];
|
|
73
|
+
let index = 0;
|
|
74
|
+
const span = /(`+)[\s\S]*?\1/g;
|
|
75
|
+
let match;
|
|
76
|
+
while ((match = span.exec(line)) !== null) {
|
|
77
|
+
out.push(fn(line.slice(index, match.index)), match[0]);
|
|
78
|
+
index = match.index + match[0].length;
|
|
79
|
+
}
|
|
80
|
+
return out.join('') + fn(line.slice(index));
|
|
81
|
+
}
|
|
82
|
+
/** A line that opens or closes a fenced code block, with the fence it uses. */
|
|
83
|
+
function fenceAt(line) {
|
|
84
|
+
const match = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
|
|
85
|
+
if (!match)
|
|
86
|
+
return null;
|
|
87
|
+
const fence = match[1];
|
|
88
|
+
// A ``` fence's info string may not contain a backtick (CommonMark), which is what stops an
|
|
89
|
+
// inline span from being read as a fence.
|
|
90
|
+
if (fence.startsWith('`') && match[2].includes('`'))
|
|
91
|
+
return null;
|
|
92
|
+
return { char: fence[0], length: fence.length, info: match[2].trim().length > 0 };
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Walk `lines`, tracking fenced-code state, and hand each line to `visit` together with
|
|
96
|
+
* whether it sits INSIDE a fenced block. Returns the fence still open at the end, if any.
|
|
97
|
+
*
|
|
98
|
+
* One shared walker so the three things that care about fences — leaving code untouched,
|
|
99
|
+
* closing what the text left open, and finding the briefing's title heading — can never
|
|
100
|
+
* disagree about where a block starts and ends.
|
|
101
|
+
*/
|
|
102
|
+
export function walkFences(lines, visit) {
|
|
103
|
+
let open = null;
|
|
104
|
+
for (const line of lines) {
|
|
105
|
+
const fence = fenceAt(line);
|
|
106
|
+
// The fence line itself belongs to the code block, so it is never rewritten.
|
|
107
|
+
visit(line, open !== null || fence !== null);
|
|
108
|
+
if (!fence)
|
|
109
|
+
continue;
|
|
110
|
+
if (!open)
|
|
111
|
+
open = { char: fence.char, length: fence.length };
|
|
112
|
+
else if (fence.char === open.char && fence.length >= open.length && !fence.info)
|
|
113
|
+
open = null;
|
|
114
|
+
}
|
|
115
|
+
return open;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Render untrusted multi-line markdown safe to send to a host: auto-link triggers defused
|
|
119
|
+
* outside fenced code, and any fence the text leaves open closed again.
|
|
120
|
+
*
|
|
121
|
+
* Unlike kernel's `hostMarkdown.prose` this does NOT cap the length — the caller
|
|
122
|
+
* ({@link import('./pr-description.js')}) applies its own budget with its own visible note
|
|
123
|
+
* BEFORE calling here, so an escape entity can never be sliced in half. With that one
|
|
124
|
+
* difference the output is identical, which the conformity test pins.
|
|
125
|
+
*/
|
|
126
|
+
export function inertMarkdown(text) {
|
|
127
|
+
const normalised = text.replace(/\r\n?/g, '\n');
|
|
128
|
+
const rewritten = [];
|
|
129
|
+
const open = walkFences(normalised.split('\n'), (line, insideFence) => {
|
|
130
|
+
rewritten.push(insideFence ? line : inertLine(line));
|
|
131
|
+
});
|
|
132
|
+
const joined = rewritten.join('\n');
|
|
133
|
+
return open ? `${joined}\n${open.char.repeat(open.length)}` : joined;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Render untrusted text INLINE (a pull-request title): newlines folded to spaces because the
|
|
137
|
+
* surrounding line has its own meaning, and auto-link triggers defused. The caller caps the
|
|
138
|
+
* length first, for the same reason as {@link inertMarkdown}.
|
|
139
|
+
*/
|
|
140
|
+
export function inertInline(text) {
|
|
141
|
+
return inertLine(text.replace(/\s+/g, ' '));
|
|
142
|
+
}
|