@cat-factory/executor-harness 1.58.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 +24 -2
- package/dist/agent.js +28 -14
- package/dist/captured-command.js +112 -0
- package/dist/coding-agent.js +90 -9
- package/dist/git.js +108 -319
- package/dist/host-markdown.js +142 -0
- package/dist/job.js +5 -46
- package/dist/pr-description.js +157 -0
- package/dist/reproduction-proof.js +614 -0
- package/dist/runner.js +3 -0
- package/dist/validation-checks.js +70 -82
- package/dist/vcs-api.js +402 -0
- package/package.json +4 -3
- package/src/agent.ts +28 -14
- package/src/captured-command.ts +144 -0
- package/src/coding-agent.ts +133 -6
- package/src/git.ts +134 -385
- package/src/host-markdown.ts +155 -0
- package/src/job.ts +32 -46
- package/src/pr-description.ts +171 -0
- package/src/reproduction-proof.ts +806 -0
- package/src/runner.ts +20 -0
- package/src/validation-checks.ts +71 -81
- package/src/vcs-api.ts +512 -0
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:
|
|
@@ -509,6 +475,40 @@ export async function branchAheadOfBase(dir, baseBranch, ghToken, signal) {
|
|
|
509
475
|
return undefined;
|
|
510
476
|
}
|
|
511
477
|
}
|
|
478
|
+
/**
|
|
479
|
+
* The files `commitish` changes relative to its merge base with the PR base branch — i.e.
|
|
480
|
+
* everything the work branch has added on top of base, `git diff --name-only <base>...<commitish>`.
|
|
481
|
+
*
|
|
482
|
+
* The BUGFIX REPRODUCTION PROOF uses this to answer the one question that decides whether a GREEN
|
|
483
|
+
* pre-fix tree means anything: does that tree ALREADY carry non-test work committed on this
|
|
484
|
+
* branch? A resumed run's `baseSha` is whatever the branch tip was when this pass started, which
|
|
485
|
+
* in the designed flow is the reproduction step's test commit — but after an eviction it is this
|
|
486
|
+
* same coder step's own interrupted work, fix included. Reporting "the check passed before your
|
|
487
|
+
* change, so it does not demonstrate the defect" in that case is simply false.
|
|
488
|
+
*
|
|
489
|
+
* `undefined` means "could not determine" (a shallow clone with no reachable merge base, a fetch
|
|
490
|
+
* failure, an unknown ref), never an empty list: the caller must degrade to its prior behaviour
|
|
491
|
+
* rather than read a failed probe as "the tree is clean".
|
|
492
|
+
*
|
|
493
|
+
* NUL-delimited so a path containing a newline (legal in git) cannot split into two entries.
|
|
494
|
+
*/
|
|
495
|
+
export async function changedFilesSinceBase(dir, baseBranch, ghToken, commitish, signal) {
|
|
496
|
+
try {
|
|
497
|
+
await git(['fetch', 'origin', `+refs/heads/${baseBranch}:refs/cat-factory/base`], {
|
|
498
|
+
cwd: dir,
|
|
499
|
+
signal,
|
|
500
|
+
env: await authEnv(ghToken),
|
|
501
|
+
});
|
|
502
|
+
const out = await git(['diff', '--name-only', '-z', `refs/cat-factory/base...${commitish}`], {
|
|
503
|
+
cwd: dir,
|
|
504
|
+
signal,
|
|
505
|
+
});
|
|
506
|
+
return out.split('\0').filter((p) => p !== '');
|
|
507
|
+
}
|
|
508
|
+
catch {
|
|
509
|
+
return undefined;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
512
|
/**
|
|
513
513
|
* Whether the checked-out branch has a real, examinable diff against
|
|
514
514
|
* `origin/<baseBranch>` — i.e. the base branch's remote-tracking ref exists (so the
|
|
@@ -565,6 +565,80 @@ export async function hasAgentChanges(dir, signal) {
|
|
|
565
565
|
export async function headCommit(dir, signal) {
|
|
566
566
|
return (await git(['rev-parse', 'HEAD'], { cwd: dir, signal })).trim();
|
|
567
567
|
}
|
|
568
|
+
/**
|
|
569
|
+
* Add a DETACHED worktree of `commitish` at `worktreePath`, sharing `dir`'s object database.
|
|
570
|
+
*
|
|
571
|
+
* The bugfix reproduction proof runs the declared check against two trees of the SAME clone (the
|
|
572
|
+
* pre-fix tree and the final tree), so a worktree is the only mechanism that gets both without a
|
|
573
|
+
* second clone, a second fetch, or disturbing the agent's own checkout — which must stay exactly
|
|
574
|
+
* as the agent left it, since the push and the PR come off it.
|
|
575
|
+
*
|
|
576
|
+
* `--detach` (rather than a branch) is deliberate: a worktree that claimed a branch would collide
|
|
577
|
+
* with the work branch checked out in `dir`, and nothing here ever commits.
|
|
578
|
+
*
|
|
579
|
+
* `worktreePath` is expected to live OUTSIDE the checkout (a per-job temp root), so the worktree's
|
|
580
|
+
* `.git` pointer file can never be swept into the agent's commit by a broad `git add -A`.
|
|
581
|
+
*/
|
|
582
|
+
export async function addWorktree(dir, worktreePath, commitish, signal) {
|
|
583
|
+
await git(['worktree', 'add', '--detach', worktreePath, commitish], { cwd: dir, signal });
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Remove a worktree previously added by {@link addWorktree} and prune the stale administrative
|
|
587
|
+
* entry, never throwing: teardown is bookkeeping, and a run whose PROOF succeeded must not fail
|
|
588
|
+
* because a temp directory could not be cleaned up. The caller still deletes the temp root, so a
|
|
589
|
+
* failure here leaks only a `.git/worktrees/<name>` record inside a container that is about to be
|
|
590
|
+
* destroyed anyway.
|
|
591
|
+
*/
|
|
592
|
+
export async function removeWorktree(dir, worktreePath, signal) {
|
|
593
|
+
try {
|
|
594
|
+
await git(['worktree', 'remove', '--force', worktreePath], { cwd: dir, signal });
|
|
595
|
+
}
|
|
596
|
+
catch {
|
|
597
|
+
// Fall through to the prune, which cleans up the record even when the directory is gone.
|
|
598
|
+
}
|
|
599
|
+
try {
|
|
600
|
+
await git(['worktree', 'prune'], { cwd: dir, signal });
|
|
601
|
+
}
|
|
602
|
+
catch {
|
|
603
|
+
// Best-effort by design (see the doc comment).
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Which of `paths` actually exist in `commitish`'s tree. Used by the reproduction proof to tell a
|
|
608
|
+
* DECLARED test file that was committed from one that only ever existed as an untracked working-
|
|
609
|
+
* tree file: the proof runs against committed trees, so an unadded test is invisible to it — and
|
|
610
|
+
* equally invisible to the push, which is the point worth telling the agent about rather than
|
|
611
|
+
* reporting a verdict computed without the reproduction in it.
|
|
612
|
+
*
|
|
613
|
+
* Returns the input order/spelling of the paths that matched, so the caller can diff against its
|
|
614
|
+
* declared list to name the missing ones verbatim.
|
|
615
|
+
*/
|
|
616
|
+
export async function pathsPresentAtCommit(dir, commitish, paths, signal) {
|
|
617
|
+
if (paths.length === 0)
|
|
618
|
+
return [];
|
|
619
|
+
const out = await git(['ls-tree', '-r', '--name-only', '-z', commitish, '--', ...paths], {
|
|
620
|
+
cwd: dir,
|
|
621
|
+
signal,
|
|
622
|
+
});
|
|
623
|
+
// NUL-delimited so a path containing a newline (legal in git) can't split into two entries.
|
|
624
|
+
const present = new Set(out.split('\0').filter((p) => p !== ''));
|
|
625
|
+
return paths.filter((p) => present.has(p));
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Check `paths` out of `commitish` into `dir`'s working tree (and index), leaving every other file
|
|
629
|
+
* untouched.
|
|
630
|
+
*
|
|
631
|
+
* This is how the reproduction's declared TEST files are placed onto the pre-fix worktree, and the
|
|
632
|
+
* narrowness is the whole safety property: a whole-tree checkout would drag the FIX across too and
|
|
633
|
+
* green the base, manufacturing a "the test does not capture the defect" verdict out of a
|
|
634
|
+
* perfectly good reproduction. Only the paths the caller has already sanitized are passed, and
|
|
635
|
+
* `--` stops any of them being read as a revision.
|
|
636
|
+
*/
|
|
637
|
+
export async function checkoutPathsFrom(dir, commitish, paths, signal) {
|
|
638
|
+
if (paths.length === 0)
|
|
639
|
+
return;
|
|
640
|
+
await git(['checkout', commitish, '--', ...paths], { cwd: dir, signal });
|
|
641
|
+
}
|
|
568
642
|
/** Stage everything and commit; returns false when there was nothing to commit. */
|
|
569
643
|
export async function commitAll(dir, message, signal) {
|
|
570
644
|
await git(['add', '-A'], { cwd: dir, signal });
|
|
@@ -770,288 +844,3 @@ export async function reinitAndPush(opts) {
|
|
|
770
844
|
env: await authEnv(opts.ghToken),
|
|
771
845
|
});
|
|
772
846
|
}
|
|
773
|
-
/**
|
|
774
|
-
* The VCS host a clone URL points at. The harness is otherwise provider-agnostic (its git
|
|
775
|
-
* auth is a host-neutral GIT_ASKPASS credential), but the "open the PR/MR" REST call is not:
|
|
776
|
-
* GitHub and GitLab have different endpoints, so infer which to call from the host. GitHub is
|
|
777
|
-
* the default; a host of `gitlab.com` or one in the `gitlab.*` / `*.gitlab.*` family (covering
|
|
778
|
-
* self-managed instances named that way) is treated as GitLab.
|
|
779
|
-
*/
|
|
780
|
-
export function inferVcsProvider(cloneUrl) {
|
|
781
|
-
let host = '';
|
|
782
|
-
try {
|
|
783
|
-
host = new URL(cloneUrl).host.toLowerCase();
|
|
784
|
-
}
|
|
785
|
-
catch {
|
|
786
|
-
return 'github';
|
|
787
|
-
}
|
|
788
|
-
if (host === 'gitlab.com' || host.startsWith('gitlab.') || host.includes('.gitlab.')) {
|
|
789
|
-
return 'gitlab';
|
|
790
|
-
}
|
|
791
|
-
return 'github';
|
|
792
|
-
}
|
|
793
|
-
/** The GitLab REST v4 base for a clone URL's host, e.g. `https://gitlab.com/api/v4`. */
|
|
794
|
-
export function gitlabApiBaseFromCloneUrl(cloneUrl) {
|
|
795
|
-
const u = new URL(cloneUrl);
|
|
796
|
-
return `${u.protocol}//${u.host}/api/v4`;
|
|
797
|
-
}
|
|
798
|
-
/**
|
|
799
|
-
* The URL-encoded GitLab project path from a clone URL — the full namespace path (so subgroups
|
|
800
|
-
* survive), with the trailing `.git` stripped, e.g.
|
|
801
|
-
* `https://gitlab.com/group/sub/proj.git` → `group%2Fsub%2Fproj`.
|
|
802
|
-
*/
|
|
803
|
-
export function gitlabProjectPath(cloneUrl) {
|
|
804
|
-
const path = new URL(cloneUrl).pathname.replace(/^\/+/, '').replace(/\.git$/, '');
|
|
805
|
-
return encodeURIComponent(path);
|
|
806
|
-
}
|
|
807
|
-
/** The abort reason as an Error (the watchdog aborts with one), or a generic fallback. */
|
|
808
|
-
function abortError(signal) {
|
|
809
|
-
return signal.reason instanceof Error ? signal.reason : new Error('aborted');
|
|
810
|
-
}
|
|
811
|
-
/** Whether a thrown fetch error is an AbortError (caller-initiated, never retried). */
|
|
812
|
-
function isAbortError(err) {
|
|
813
|
-
return err instanceof Error && err.name === 'AbortError';
|
|
814
|
-
}
|
|
815
|
-
/**
|
|
816
|
-
* Parse a `Retry-After` header into ms, bounded so it can't stall the job. Accepts BOTH
|
|
817
|
-
* forms the spec allows: integer delay-seconds (`120`) and an HTTP-date (`Wed, 21 Oct 2026
|
|
818
|
-
* 07:28:00 GMT`); the latter is turned into a delay from now. A past/zero/unparseable value
|
|
819
|
-
* yields undefined so the caller falls back to exponential backoff.
|
|
820
|
-
*/
|
|
821
|
-
function retryAfterMs(res) {
|
|
822
|
-
const raw = res.headers.get('retry-after');
|
|
823
|
-
if (!raw)
|
|
824
|
-
return undefined;
|
|
825
|
-
const secs = Number(raw);
|
|
826
|
-
if (Number.isFinite(secs)) {
|
|
827
|
-
return secs > 0 ? Math.min(secs * 1000, MAX_RETRY_AFTER_MS) : undefined;
|
|
828
|
-
}
|
|
829
|
-
const at = Date.parse(raw);
|
|
830
|
-
if (Number.isNaN(at))
|
|
831
|
-
return undefined;
|
|
832
|
-
const ms = at - Date.now();
|
|
833
|
-
return ms > 0 ? Math.min(ms, MAX_RETRY_AFTER_MS) : undefined;
|
|
834
|
-
}
|
|
835
|
-
/** Sleep `ms`, rejecting immediately (with the abort reason) if `signal` aborts meanwhile. */
|
|
836
|
-
function abortableDelay(ms, signal) {
|
|
837
|
-
return new Promise((resolve, reject) => {
|
|
838
|
-
if (signal?.aborted)
|
|
839
|
-
return reject(abortError(signal));
|
|
840
|
-
const onAbort = () => {
|
|
841
|
-
clearTimeout(timer);
|
|
842
|
-
reject(abortError(signal));
|
|
843
|
-
};
|
|
844
|
-
const timer = setTimeout(() => {
|
|
845
|
-
signal?.removeEventListener('abort', onAbort);
|
|
846
|
-
resolve();
|
|
847
|
-
}, ms);
|
|
848
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
849
|
-
});
|
|
850
|
-
}
|
|
851
|
-
const MAX_RETRY_AFTER_MS = 8_000;
|
|
852
|
-
const RETRY_BASE_MS = 500;
|
|
853
|
-
const RETRY_MAX_DELAY_MS = 4_000;
|
|
854
|
-
/**
|
|
855
|
-
* Run a single HTTP request with bounded retry for TRANSIENT failures, so a momentary
|
|
856
|
-
* upstream blip (a 5xx, a 429 rate-limit, or a dropped connection) no longer fails an
|
|
857
|
-
* otherwise-complete run on its very last step (opening the PR/MR). Up to 3 attempts
|
|
858
|
-
* (2 retries) with exponential backoff + jitter (honoring a `Retry-After` on a 429),
|
|
859
|
-
* every wait abort-aware so the inactivity/max-duration watchdog still cancels promptly.
|
|
860
|
-
*
|
|
861
|
-
* ONLY transient failures retry: a `>=500`/`429` response, or a network-level fetch
|
|
862
|
-
* rejection. A 4xx (incl. the 422/409 "already exists" the callers treat as success) is
|
|
863
|
-
* returned to the caller unretried, and a caller abort is rethrown at once. The response
|
|
864
|
-
* body is never read here, so the caller's existing status handling is unchanged.
|
|
865
|
-
*/
|
|
866
|
-
async function withApiRetry(fn, opts = {}) {
|
|
867
|
-
const maxAttempts = opts.attempts ?? 3;
|
|
868
|
-
let lastError;
|
|
869
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
870
|
-
if (opts.signal?.aborted)
|
|
871
|
-
throw abortError(opts.signal);
|
|
872
|
-
let res;
|
|
873
|
-
try {
|
|
874
|
-
res = await fn();
|
|
875
|
-
}
|
|
876
|
-
catch (err) {
|
|
877
|
-
// A caller/watchdog abort is terminal; a network error is transient → retry.
|
|
878
|
-
if (isAbortError(err) || opts.signal?.aborted)
|
|
879
|
-
throw err;
|
|
880
|
-
lastError = err;
|
|
881
|
-
}
|
|
882
|
-
if (res) {
|
|
883
|
-
const transient = res.status >= 500 || res.status === 429;
|
|
884
|
-
if (!transient || attempt >= maxAttempts)
|
|
885
|
-
return res;
|
|
886
|
-
const after = retryAfterMs(res);
|
|
887
|
-
// Discard the unread body before retrying so the connection can be reused.
|
|
888
|
-
await res.body?.cancel().catch(() => { });
|
|
889
|
-
await abortableDelay(after ?? backoffMs(attempt), opts.signal);
|
|
890
|
-
continue;
|
|
891
|
-
}
|
|
892
|
-
if (attempt >= maxAttempts)
|
|
893
|
-
break;
|
|
894
|
-
await abortableDelay(backoffMs(attempt), opts.signal);
|
|
895
|
-
}
|
|
896
|
-
// Exhausted on a network-level rejection (no HTTP response): an upstream API failure.
|
|
897
|
-
const message = lastError instanceof Error ? lastError.message : 'API request failed after retries';
|
|
898
|
-
throw new HarnessFailure('api', redactSecrets(message));
|
|
899
|
-
}
|
|
900
|
-
/** Exponential backoff (base 500ms, capped 4s) with up to 25% positive jitter. */
|
|
901
|
-
function backoffMs(attempt) {
|
|
902
|
-
const base = Math.min(RETRY_MAX_DELAY_MS, RETRY_BASE_MS * 2 ** (attempt - 1));
|
|
903
|
-
return base + Math.floor(base * 0.25 * Math.random());
|
|
904
|
-
}
|
|
905
|
-
/**
|
|
906
|
-
* Open a PR (GitHub) or merge request (GitLab) for the pushed branch; returns its web URL.
|
|
907
|
-
* The provider is chosen from the EXPLICIT `opts.provider` when the dispatcher set it,
|
|
908
|
-
* falling back to host inference from the clone URL only when it didn't — so a self-managed
|
|
909
|
-
* GitLab whose host isn't named `gitlab.*` still opens an MR instead of being misrouted to
|
|
910
|
-
* GitHub's API. The GitHub path is unchanged.
|
|
911
|
-
*/
|
|
912
|
-
export async function openPullRequest(opts) {
|
|
913
|
-
const provider = opts.provider ?? (opts.cloneUrl ? inferVcsProvider(opts.cloneUrl) : 'github');
|
|
914
|
-
if (provider === 'gitlab') {
|
|
915
|
-
if (!opts.cloneUrl) {
|
|
916
|
-
throw new Error('Cannot open a GitLab merge request without the repo clone URL');
|
|
917
|
-
}
|
|
918
|
-
return openGitLabMergeRequest({ ...opts, cloneUrl: opts.cloneUrl });
|
|
919
|
-
}
|
|
920
|
-
const apiBase = opts.apiBase ?? 'https://api.github.com';
|
|
921
|
-
const path = `${encodeURIComponent(opts.owner)}/${encodeURIComponent(opts.name)}`;
|
|
922
|
-
const res = await withApiRetry(() => fetch(`${apiBase}/repos/${path}/pulls`, {
|
|
923
|
-
method: 'POST',
|
|
924
|
-
headers: {
|
|
925
|
-
authorization: `Bearer ${opts.ghToken}`,
|
|
926
|
-
accept: 'application/vnd.github+json',
|
|
927
|
-
'user-agent': 'cat-factory-executor',
|
|
928
|
-
'x-github-api-version': '2022-11-28',
|
|
929
|
-
'content-type': 'application/json',
|
|
930
|
-
},
|
|
931
|
-
body: JSON.stringify({
|
|
932
|
-
title: opts.pr.title,
|
|
933
|
-
head: opts.head,
|
|
934
|
-
base: opts.base,
|
|
935
|
-
body: opts.pr.body,
|
|
936
|
-
}),
|
|
937
|
-
// Bound on the watchdog so a hung GitHub call can't stall the job.
|
|
938
|
-
...(opts.signal ? { signal: opts.signal } : {}),
|
|
939
|
-
}), { signal: opts.signal });
|
|
940
|
-
if (!res.ok) {
|
|
941
|
-
const detail = await res.text().catch(() => '');
|
|
942
|
-
// A resumed run pushes to a branch that already has an open PR; GitHub answers
|
|
943
|
-
// 422 "A pull request already exists". That's success for us — return the
|
|
944
|
-
// existing PR's url rather than failing the resumed run.
|
|
945
|
-
if (res.status === 422 && /pull request already exists/i.test(detail)) {
|
|
946
|
-
const existing = await findOpenPullRequestUrl(opts);
|
|
947
|
-
if (existing)
|
|
948
|
-
return existing;
|
|
949
|
-
}
|
|
950
|
-
// The head branch has nothing ahead of base ("No commits between <base> and <head>").
|
|
951
|
-
// That is not an API failure — there is simply nothing to open a PR for (e.g. a resumed
|
|
952
|
-
// branch whose earlier PR was merged with a merge commit, leaving the branch reachable
|
|
953
|
-
// from base). Signal it with null so the caller records a clean no-op instead of failing
|
|
954
|
-
// the run with GitHub's opaque 422.
|
|
955
|
-
if (res.status === 422 && /no commits between/i.test(detail))
|
|
956
|
-
return null;
|
|
957
|
-
const remedy = describePrOpenFailure(res.status, 'github');
|
|
958
|
-
const base = redactSecrets(`Failed to open PR (HTTP ${res.status}): ${detail.slice(0, 300)}`);
|
|
959
|
-
throw new HarnessFailure('api', remedy ? `${base}\n${remedy}` : base);
|
|
960
|
-
}
|
|
961
|
-
const body = (await res.json());
|
|
962
|
-
if (!body.html_url)
|
|
963
|
-
throw new HarnessFailure('api', 'GitHub did not return a PR url');
|
|
964
|
-
return body.html_url;
|
|
965
|
-
}
|
|
966
|
-
/** GitLab API headers for the PAT (the `PRIVATE-TOKEN` auth GitLab uses). */
|
|
967
|
-
function gitlabHeaders(token) {
|
|
968
|
-
return {
|
|
969
|
-
'private-token': token,
|
|
970
|
-
accept: 'application/json',
|
|
971
|
-
'user-agent': 'cat-factory-executor',
|
|
972
|
-
'content-type': 'application/json',
|
|
973
|
-
};
|
|
974
|
-
}
|
|
975
|
-
/**
|
|
976
|
-
* Open a GitLab merge request (the analogue of {@link openPullRequest} for GitLab). The REST
|
|
977
|
-
* base + project path are derived from the clone URL's host, so it works for gitlab.com and a
|
|
978
|
-
* self-managed instance alike. `head`→`source_branch`, `base`→`target_branch`. On a duplicate
|
|
979
|
-
* (a resumed run whose branch already has an open MR — GitLab answers 409) the existing MR's
|
|
980
|
-
* web URL is returned instead of failing the run, mirroring the GitHub 422 handling.
|
|
981
|
-
*/
|
|
982
|
-
async function openGitLabMergeRequest(opts) {
|
|
983
|
-
const apiBase = gitlabApiBaseFromCloneUrl(opts.cloneUrl);
|
|
984
|
-
const project = gitlabProjectPath(opts.cloneUrl);
|
|
985
|
-
const res = await withApiRetry(() => fetch(`${apiBase}/projects/${project}/merge_requests`, {
|
|
986
|
-
method: 'POST',
|
|
987
|
-
headers: gitlabHeaders(opts.ghToken),
|
|
988
|
-
body: JSON.stringify({
|
|
989
|
-
source_branch: opts.head,
|
|
990
|
-
target_branch: opts.base,
|
|
991
|
-
title: opts.pr.title,
|
|
992
|
-
description: opts.pr.body,
|
|
993
|
-
}),
|
|
994
|
-
...(opts.signal ? { signal: opts.signal } : {}),
|
|
995
|
-
}), { signal: opts.signal });
|
|
996
|
-
if (!res.ok) {
|
|
997
|
-
const detail = await res.text().catch(() => '');
|
|
998
|
-
// GitLab returns 409 (sometimes 400) when an open MR already exists for this source
|
|
999
|
-
// branch; that is success for a resumed run — return the existing MR's url.
|
|
1000
|
-
if ((res.status === 409 || res.status === 400) &&
|
|
1001
|
-
/already exists|open merge request/i.test(detail)) {
|
|
1002
|
-
const existing = await findOpenMergeRequestUrl(apiBase, project, opts);
|
|
1003
|
-
if (existing)
|
|
1004
|
-
return existing;
|
|
1005
|
-
}
|
|
1006
|
-
const remedy = describePrOpenFailure(res.status, 'gitlab');
|
|
1007
|
-
const base = redactSecrets(`Failed to open merge request (HTTP ${res.status}): ${detail.slice(0, 300)}`);
|
|
1008
|
-
throw new HarnessFailure('api', remedy ? `${base}\n${remedy}` : base);
|
|
1009
|
-
}
|
|
1010
|
-
const body = (await res.json());
|
|
1011
|
-
if (!body.web_url)
|
|
1012
|
-
throw new HarnessFailure('api', 'GitLab did not return a merge request url');
|
|
1013
|
-
return body.web_url;
|
|
1014
|
-
}
|
|
1015
|
-
/** Find the open GitLab MR for `opts.head`→`opts.base`, returning its web_url or undefined. */
|
|
1016
|
-
async function findOpenMergeRequestUrl(apiBase, project, opts) {
|
|
1017
|
-
// Filter by BOTH branches: a source branch can have open MRs to several targets, so the
|
|
1018
|
-
// source alone could match an MR against a different base than the one we just tried to open.
|
|
1019
|
-
const query = new URLSearchParams({
|
|
1020
|
-
source_branch: opts.head,
|
|
1021
|
-
target_branch: opts.base,
|
|
1022
|
-
state: 'opened',
|
|
1023
|
-
});
|
|
1024
|
-
const res = await fetch(`${apiBase}/projects/${project}/merge_requests?${query}`, {
|
|
1025
|
-
headers: gitlabHeaders(opts.ghToken),
|
|
1026
|
-
...(opts.signal ? { signal: opts.signal } : {}),
|
|
1027
|
-
});
|
|
1028
|
-
if (!res.ok)
|
|
1029
|
-
return undefined;
|
|
1030
|
-
const list = (await res.json().catch(() => []));
|
|
1031
|
-
return Array.isArray(list) && list[0]?.web_url ? list[0].web_url : undefined;
|
|
1032
|
-
}
|
|
1033
|
-
/** Find the open PR for `opts.head` on `opts.base`, returning its html_url or undefined. */
|
|
1034
|
-
async function findOpenPullRequestUrl(opts) {
|
|
1035
|
-
const apiBase = opts.apiBase ?? 'https://api.github.com';
|
|
1036
|
-
// Encode the ref-derived query params: a branch/owner containing `&` or `#` would
|
|
1037
|
-
// otherwise split the query string or inject an unintended parameter.
|
|
1038
|
-
const query = new URLSearchParams({
|
|
1039
|
-
head: `${opts.owner}:${opts.head}`,
|
|
1040
|
-
base: opts.base,
|
|
1041
|
-
state: 'open',
|
|
1042
|
-
});
|
|
1043
|
-
const path = `${encodeURIComponent(opts.owner)}/${encodeURIComponent(opts.name)}`;
|
|
1044
|
-
const res = await fetch(`${apiBase}/repos/${path}/pulls?${query}`, {
|
|
1045
|
-
headers: {
|
|
1046
|
-
authorization: `Bearer ${opts.ghToken}`,
|
|
1047
|
-
accept: 'application/vnd.github+json',
|
|
1048
|
-
'user-agent': 'cat-factory-executor',
|
|
1049
|
-
'x-github-api-version': '2022-11-28',
|
|
1050
|
-
},
|
|
1051
|
-
...(opts.signal ? { signal: opts.signal } : {}),
|
|
1052
|
-
});
|
|
1053
|
-
if (!res.ok)
|
|
1054
|
-
return undefined;
|
|
1055
|
-
const list = (await res.json().catch(() => []));
|
|
1056
|
-
return Array.isArray(list) && list[0]?.html_url ? list[0].html_url : undefined;
|
|
1057
|
-
}
|
|
@@ -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
|
+
}
|