@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.
@@ -0,0 +1,157 @@
1
+ import { readFile, rm } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { inertInline, inertMarkdown, walkFences } from './host-markdown.js';
4
+ import { redactSecrets } from './redact.js';
5
+ // ---------------------------------------------------------------------------
6
+ // The agent-authored pull-request description side channel. A coding agent whose
7
+ // dispatch opens a PR is asked (via the backend-composed system prompt) to end its
8
+ // run by writing a reviewer briefing — the problem, the decisions made, what to
9
+ // look out for — to a sentinel file at the root of the checkout the PR belongs to.
10
+ // The harness reads it after the agent settles, removes it (so it never lands in a
11
+ // commit), and uses it as the PR body in place of the generic dispatch-time text
12
+ // the job body carries. Absent or unusable ⇒ the dispatch-time fallback, unchanged.
13
+ //
14
+ // The briefing is MODEL-AUTHORED text landing verbatim on a host-parsed surface, so
15
+ // it crosses `host-markdown.ts` (auto-link triggers defused, open code fences closed)
16
+ // on the way out — see that module for why a PR body is not an inert string sink.
17
+ //
18
+ // The filename is kept in sync with `PR_DESCRIPTION_FILE` in `@cat-factory/agents`
19
+ // (the executor-harness has no dependency on that package), exactly like the
20
+ // effort-report and follow-ups sentinels.
21
+ // ---------------------------------------------------------------------------
22
+ /** The sentinel file the agent writes its PR description to (relative to the checkout root). */
23
+ export const PR_DESCRIPTION_FILE = '.cat-pr-description.md';
24
+ /**
25
+ * Ceiling on the agent-authored body.
26
+ *
27
+ * The engine appends its verification report to the SAME body later, and that section carries
28
+ * its own 50,000-character ceiling (`MAX_SECTION_CHARS` in kernel's `hostMarkdown`). GitHub
29
+ * rejects a body over 65,536 with a 422, and the report publisher swallows its own failures —
30
+ * so a briefing budget that does not leave the report room would surface as a report that
31
+ * silently never publishes. 15,000 + 50,000 stays under the limit with room to join them.
32
+ */
33
+ const MAX_PR_BODY_CHARS = 15_000;
34
+ /** Ceiling on an agent-supplied title (GitHub truncates around 256; a title should be short). */
35
+ const MAX_PR_TITLE_CHARS = 160;
36
+ /** Opens the engine-managed region of a PR body (kept in sync with `kernel/domain/pr-report.ts`). */
37
+ export const PR_REPORT_MARKER_START = '<!-- cat-factory:verification-report:start -->';
38
+ /** Closes the engine-managed region of a PR body. */
39
+ export const PR_REPORT_MARKER_END = '<!-- cat-factory:verification-report:end -->';
40
+ /**
41
+ * A marker inside the agent-authored briefing would make the engine's splice treat part of the
42
+ * briefing as its own managed region and rewrite it, so any occurrence is stripped up front.
43
+ * Deliberately laxer than the exact constants above (whitespace-tolerant), so a near-miss the
44
+ * splice itself would not match cannot survive here either.
45
+ */
46
+ const MANAGED_SECTION_MARKER = /<!--\s*cat-factory:verification-report:(?:start|end)\s*-->/g;
47
+ /**
48
+ * Read + parse + REMOVE the agent's PR-description sentinel from `dir`. Lenient: returns
49
+ * undefined when the file is absent (the agent wrote none) or carries nothing usable. Never
50
+ * throws — a bad description must never fail an otherwise-good run; the caller falls back to
51
+ * the dispatch-time text.
52
+ *
53
+ * A SINGLE `# <title>` heading on the first line sets the PR title; everything after it is the
54
+ * body (see {@link splitTitle} for why a LONE heading is required). The whole text is
55
+ * secret-scrubbed, an over-budget body is truncated WITH a visible note (a silent cut would
56
+ * read as the complete briefing), and both halves are made inert for the host.
57
+ *
58
+ * On scrubbing: `redactSecrets`'s credential-assignment rule is deliberately eager, so a
59
+ * briefing sentence like "the token: handling changed" loses its next word. That is the right
60
+ * trade for a surface this public — the rule is shared with every other redaction path, and
61
+ * narrowing it so prose reads better would weaken all of them.
62
+ */
63
+ export async function readPrDescription(dir) {
64
+ const path = join(dir, PR_DESCRIPTION_FILE);
65
+ let raw;
66
+ try {
67
+ raw = await readFile(path, 'utf8');
68
+ }
69
+ catch {
70
+ return undefined; // no description written — the fallback body applies
71
+ }
72
+ // Remove it so it never lands in a commit (defence in depth; the checkout also excludes it).
73
+ await rm(path, { force: true }).catch(() => { });
74
+ const text = redactSecrets(raw).replace(MANAGED_SECTION_MARKER, '').trim();
75
+ if (!text)
76
+ return undefined;
77
+ const split = splitTitle(text);
78
+ // Cap BEFORE the escapes on both halves, so a numeric entity can never be sliced in half.
79
+ const title = split.title ? inertInline(capTitle(split.title)) : undefined;
80
+ const body = split.body ? inertMarkdown(capBody(split.body)) : undefined;
81
+ if (!title && !body)
82
+ return undefined;
83
+ return { ...(title ? { title } : {}), ...(body ? { body } : {}) };
84
+ }
85
+ /**
86
+ * Split a leading `# <title>` heading off the briefing.
87
+ *
88
+ * The heading becomes the title ONLY when it is the single level-1 heading in the whole file,
89
+ * which is exactly what the prompt asks for ("a single `# <title>` heading line"). An agent
90
+ * that instead uses `#` for its section headings — `# Problem`, `# Decisions`, entirely
91
+ * idiomatic for the briefing the prompt describes — would otherwise have its first section
92
+ * silently become the pull request's title, replacing `<block> (<pipeline>)` with the word
93
+ * "Problem". Headings inside fenced code are not headings and are skipped, or a briefing
94
+ * quoting a shell snippet (`# rebuild the image`) would lose its title to the snippet.
95
+ */
96
+ function splitTitle(text) {
97
+ const lines = text.split('\n');
98
+ const headings = [];
99
+ let index = 0;
100
+ walkFences(lines, (line, insideFence) => {
101
+ if (!insideFence && /^#\s+\S/.test(line))
102
+ headings.push(index);
103
+ index += 1;
104
+ });
105
+ if (headings.length !== 1 || headings[0] !== 0)
106
+ return { body: text };
107
+ const title = lines[0].replace(/^#\s+/, '').trim();
108
+ if (!title)
109
+ return { body: text };
110
+ return { title, body: lines.slice(1).join('\n').trim() };
111
+ }
112
+ /** Cut an over-long title at a word boundary when one is near, marking the cut. */
113
+ function capTitle(value) {
114
+ const collapsed = value.trim();
115
+ if (collapsed.length <= MAX_PR_TITLE_CHARS)
116
+ return collapsed;
117
+ const head = collapsed.slice(0, MAX_PR_TITLE_CHARS - 1);
118
+ const space = head.lastIndexOf(' ');
119
+ const kept = space > MAX_PR_TITLE_CHARS * 0.6 ? head.slice(0, space) : head;
120
+ return `${kept.trimEnd()}…`;
121
+ }
122
+ /** Cut an over-budget body, marking the cut so it is never read as the whole briefing. */
123
+ function capBody(value) {
124
+ if (value.length <= MAX_PR_BODY_CHARS)
125
+ return value;
126
+ return (value.slice(0, MAX_PR_BODY_CHARS).trimEnd() +
127
+ '\n\n_Truncated by the platform: the description exceeded the size budget._');
128
+ }
129
+ /**
130
+ * Fold an agent-authored description over the dispatch-time fallback the job body carries.
131
+ * Field-wise: the agent's title/body each win when present, so a body-only briefing keeps the
132
+ * backend-composed title and vice versa.
133
+ */
134
+ export function applyPrDescription(fallback, agent) {
135
+ if (!agent)
136
+ return fallback;
137
+ return { title: agent.title ?? fallback.title, body: agent.body ?? fallback.body };
138
+ }
139
+ /**
140
+ * The body to PATCH onto an ALREADY-OPEN pull request when a resumed run produced a fresh
141
+ * briefing: the new description followed by whatever the engine's managed verification-report
142
+ * region currently holds.
143
+ *
144
+ * Carrying the region across is what makes the refresh safe. The engine re-publishes the report
145
+ * on every step settlement, so dropping it here would usually self-heal — but "usually" is not
146
+ * a property to rest the one artefact a reviewer reads on, and a run that settles no further
147
+ * step (the work is already merged, the run failed after its push) would never restore it.
148
+ */
149
+ export function preserveManagedSection(currentBody, nextBody) {
150
+ const existing = currentBody ?? '';
151
+ const start = existing.indexOf(PR_REPORT_MARKER_START);
152
+ const end = existing.indexOf(PR_REPORT_MARKER_END);
153
+ if (start === -1 || end <= start)
154
+ return nextBody;
155
+ const region = existing.slice(start, end + PR_REPORT_MARKER_END.length);
156
+ return `${nextBody.trim()}\n\n${region}\n`;
157
+ }
@@ -0,0 +1,402 @@
1
+ // ---------------------------------------------------------------------------
2
+ // The VCS HOST REST surface: opening a pull request / merge request, finding the one that is
3
+ // already open, and refreshing its title + description.
4
+ //
5
+ // Split out of `git.ts`, which is otherwise entirely the git CLI. The seam is real rather than
6
+ // arithmetic: nothing here shells out to git, nothing in `git.ts` speaks HTTP, and the two
7
+ // halves fail in completely different ways (a rejected credential and a 403 from an App
8
+ // permission want different remedies). `test/git-pr.test.ts` already covered exactly this
9
+ // surface before it had a file of its own.
10
+ //
11
+ // Provider-agnostic by construction: GitHub and GitLab each get their own request shapes behind
12
+ // one `openPullRequest` entry point, and every capability added to one is added to the other.
13
+ // ---------------------------------------------------------------------------
14
+ import { HarnessFailure } from './failure.js';
15
+ import { preserveManagedSection } from './pr-description.js';
16
+ import { redactSecrets } from './redact.js';
17
+ /**
18
+ * Classify a PR/MR-open REST failure by its HTTP status into an actionable remedy, else
19
+ * undefined (an unmapped status keeps just the raw `Failed to open … (HTTP n)` line). Like
20
+ * {@link describeGitFailure} this only APPENDS a cause + fix — the raw status line is
21
+ * load-bearing detail and stays. `provider` tailors the scope/permission wording (GitHub App
22
+ * Pull-requests permission / `repo` PAT scope vs GitLab `api` scope) and the noun (pull
23
+ * request vs merge request). Pure, so it is unit-tested per status.
24
+ */
25
+ export function describePrOpenFailure(status, provider) {
26
+ const noun = provider === 'gitlab' ? 'merge request' : 'pull request';
27
+ if (status === 401) {
28
+ return (`The credential was rejected while opening the ${noun}. The GitHub App installation token ` +
29
+ '(or, in local mode, the GITHUB_PAT) is most likely expired, rotated, or revoked — reconnect ' +
30
+ 'the GitHub App for the workspace (or regenerate the PAT), then retry.');
31
+ }
32
+ if (status === 403) {
33
+ const scope = provider === 'gitlab'
34
+ ? 'the GitLab token needs the `api` scope and Developer+ access to the project'
35
+ : 'the GitHub App needs the "Pull requests: write" permission (or the PAT the `repo` scope) and write access to the repository';
36
+ return `The credential lacks permission to open a ${noun}: ${scope}. Grant it, then retry.`;
37
+ }
38
+ if (status === 404) {
39
+ return (`The repository could not be found while opening the ${noun} — it may have been deleted, ` +
40
+ 'renamed, or made private, or the credential can no longer see it. Confirm the repo and the ' +
41
+ "credential's access to it, then retry.");
42
+ }
43
+ if (status === 422 || status === 400) {
44
+ return (`GitHub/GitLab rejected the ${noun} as invalid. Usually the head or base branch does not ` +
45
+ 'exist, the two branches are identical (nothing to compare), or the base branch is protected ' +
46
+ 'against direct PRs. Check the branch names and that the head has commits ahead of the base, ' +
47
+ 'then retry.');
48
+ }
49
+ return undefined;
50
+ }
51
+ /**
52
+ * The VCS host a clone URL points at. The harness is otherwise provider-agnostic (its git
53
+ * auth is a host-neutral GIT_ASKPASS credential), but the "open the PR/MR" REST call is not:
54
+ * GitHub and GitLab have different endpoints, so infer which to call from the host. GitHub is
55
+ * the default; a host of `gitlab.com` or one in the `gitlab.*` / `*.gitlab.*` family (covering
56
+ * self-managed instances named that way) is treated as GitLab.
57
+ */
58
+ export function inferVcsProvider(cloneUrl) {
59
+ let host = '';
60
+ try {
61
+ host = new URL(cloneUrl).host.toLowerCase();
62
+ }
63
+ catch {
64
+ return 'github';
65
+ }
66
+ if (host === 'gitlab.com' || host.startsWith('gitlab.') || host.includes('.gitlab.')) {
67
+ return 'gitlab';
68
+ }
69
+ return 'github';
70
+ }
71
+ /** The GitLab REST v4 base for a clone URL's host, e.g. `https://gitlab.com/api/v4`. */
72
+ export function gitlabApiBaseFromCloneUrl(cloneUrl) {
73
+ const u = new URL(cloneUrl);
74
+ return `${u.protocol}//${u.host}/api/v4`;
75
+ }
76
+ /**
77
+ * The URL-encoded GitLab project path from a clone URL — the full namespace path (so subgroups
78
+ * survive), with the trailing `.git` stripped, e.g.
79
+ * `https://gitlab.com/group/sub/proj.git` → `group%2Fsub%2Fproj`.
80
+ */
81
+ export function gitlabProjectPath(cloneUrl) {
82
+ const path = new URL(cloneUrl).pathname.replace(/^\/+/, '').replace(/\.git$/, '');
83
+ return encodeURIComponent(path);
84
+ }
85
+ /** The abort reason as an Error (the watchdog aborts with one), or a generic fallback. */
86
+ function abortError(signal) {
87
+ return signal.reason instanceof Error ? signal.reason : new Error('aborted');
88
+ }
89
+ /** Whether a thrown fetch error is an AbortError (caller-initiated, never retried). */
90
+ function isAbortError(err) {
91
+ return err instanceof Error && err.name === 'AbortError';
92
+ }
93
+ /**
94
+ * Parse a `Retry-After` header into ms, bounded so it can't stall the job. Accepts BOTH
95
+ * forms the spec allows: integer delay-seconds (`120`) and an HTTP-date (`Wed, 21 Oct 2026
96
+ * 07:28:00 GMT`); the latter is turned into a delay from now. A past/zero/unparseable value
97
+ * yields undefined so the caller falls back to exponential backoff.
98
+ */
99
+ function retryAfterMs(res) {
100
+ const raw = res.headers.get('retry-after');
101
+ if (!raw)
102
+ return undefined;
103
+ const secs = Number(raw);
104
+ if (Number.isFinite(secs)) {
105
+ return secs > 0 ? Math.min(secs * 1000, MAX_RETRY_AFTER_MS) : undefined;
106
+ }
107
+ const at = Date.parse(raw);
108
+ if (Number.isNaN(at))
109
+ return undefined;
110
+ const ms = at - Date.now();
111
+ return ms > 0 ? Math.min(ms, MAX_RETRY_AFTER_MS) : undefined;
112
+ }
113
+ /** Sleep `ms`, rejecting immediately (with the abort reason) if `signal` aborts meanwhile. */
114
+ function abortableDelay(ms, signal) {
115
+ return new Promise((resolve, reject) => {
116
+ if (signal?.aborted)
117
+ return reject(abortError(signal));
118
+ const onAbort = () => {
119
+ clearTimeout(timer);
120
+ reject(abortError(signal));
121
+ };
122
+ const timer = setTimeout(() => {
123
+ signal?.removeEventListener('abort', onAbort);
124
+ resolve();
125
+ }, ms);
126
+ signal?.addEventListener('abort', onAbort, { once: true });
127
+ });
128
+ }
129
+ const MAX_RETRY_AFTER_MS = 8_000;
130
+ const RETRY_BASE_MS = 500;
131
+ const RETRY_MAX_DELAY_MS = 4_000;
132
+ /**
133
+ * Run a single HTTP request with bounded retry for TRANSIENT failures, so a momentary
134
+ * upstream blip (a 5xx, a 429 rate-limit, or a dropped connection) no longer fails an
135
+ * otherwise-complete run on its very last step (opening the PR/MR). Up to 3 attempts
136
+ * (2 retries) with exponential backoff + jitter (honoring a `Retry-After` on a 429),
137
+ * every wait abort-aware so the inactivity/max-duration watchdog still cancels promptly.
138
+ *
139
+ * ONLY transient failures retry: a `>=500`/`429` response, or a network-level fetch
140
+ * rejection. A 4xx (incl. the 422/409 "already exists" the callers treat as success) is
141
+ * returned to the caller unretried, and a caller abort is rethrown at once. The response
142
+ * body is never read here, so the caller's existing status handling is unchanged.
143
+ */
144
+ async function withApiRetry(fn, opts = {}) {
145
+ const maxAttempts = opts.attempts ?? 3;
146
+ let lastError;
147
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
148
+ if (opts.signal?.aborted)
149
+ throw abortError(opts.signal);
150
+ let res;
151
+ try {
152
+ res = await fn();
153
+ }
154
+ catch (err) {
155
+ // A caller/watchdog abort is terminal; a network error is transient → retry.
156
+ if (isAbortError(err) || opts.signal?.aborted)
157
+ throw err;
158
+ lastError = err;
159
+ }
160
+ if (res) {
161
+ const transient = res.status >= 500 || res.status === 429;
162
+ if (!transient || attempt >= maxAttempts)
163
+ return res;
164
+ const after = retryAfterMs(res);
165
+ // Discard the unread body before retrying so the connection can be reused.
166
+ await res.body?.cancel().catch(() => { });
167
+ await abortableDelay(after ?? backoffMs(attempt), opts.signal);
168
+ continue;
169
+ }
170
+ if (attempt >= maxAttempts)
171
+ break;
172
+ await abortableDelay(backoffMs(attempt), opts.signal);
173
+ }
174
+ // Exhausted on a network-level rejection (no HTTP response): an upstream API failure.
175
+ const message = lastError instanceof Error ? lastError.message : 'API request failed after retries';
176
+ throw new HarnessFailure('api', redactSecrets(message));
177
+ }
178
+ /** Exponential backoff (base 500ms, capped 4s) with up to 25% positive jitter. */
179
+ function backoffMs(attempt) {
180
+ const base = Math.min(RETRY_MAX_DELAY_MS, RETRY_BASE_MS * 2 ** (attempt - 1));
181
+ return base + Math.floor(base * 0.25 * Math.random());
182
+ }
183
+ /**
184
+ * Open a PR (GitHub) or merge request (GitLab) for the pushed branch; returns its web URL.
185
+ * The provider is chosen from the EXPLICIT `opts.provider` when the dispatcher set it,
186
+ * falling back to host inference from the clone URL only when it didn't — so a self-managed
187
+ * GitLab whose host isn't named `gitlab.*` still opens an MR instead of being misrouted to
188
+ * GitHub's API. The GitHub path is unchanged.
189
+ */
190
+ export async function openPullRequest(opts) {
191
+ const provider = opts.provider ?? (opts.cloneUrl ? inferVcsProvider(opts.cloneUrl) : 'github');
192
+ if (provider === 'gitlab') {
193
+ if (!opts.cloneUrl) {
194
+ throw new Error('Cannot open a GitLab merge request without the repo clone URL');
195
+ }
196
+ return openGitLabMergeRequest({ ...opts, cloneUrl: opts.cloneUrl });
197
+ }
198
+ const apiBase = opts.apiBase ?? 'https://api.github.com';
199
+ const path = `${encodeURIComponent(opts.owner)}/${encodeURIComponent(opts.name)}`;
200
+ const res = await withApiRetry(() => fetch(`${apiBase}/repos/${path}/pulls`, {
201
+ method: 'POST',
202
+ headers: {
203
+ authorization: `Bearer ${opts.ghToken}`,
204
+ accept: 'application/vnd.github+json',
205
+ 'user-agent': 'cat-factory-executor',
206
+ 'x-github-api-version': '2022-11-28',
207
+ 'content-type': 'application/json',
208
+ },
209
+ body: JSON.stringify({
210
+ title: opts.pr.title,
211
+ head: opts.head,
212
+ base: opts.base,
213
+ body: opts.pr.body,
214
+ }),
215
+ // Bound on the watchdog so a hung GitHub call can't stall the job.
216
+ ...(opts.signal ? { signal: opts.signal } : {}),
217
+ }), { signal: opts.signal });
218
+ if (!res.ok) {
219
+ const detail = await res.text().catch(() => '');
220
+ // A resumed run pushes to a branch that already has an open PR; GitHub answers
221
+ // 422 "A pull request already exists". That's success for us — return the
222
+ // existing PR's url rather than failing the resumed run.
223
+ if (res.status === 422 && /pull request already exists/i.test(detail)) {
224
+ const existing = await findOpenPullRequest(opts);
225
+ if (existing) {
226
+ if (opts.refreshExisting)
227
+ await refreshPullRequest(opts, existing);
228
+ return existing.url;
229
+ }
230
+ }
231
+ // The head branch has nothing ahead of base ("No commits between <base> and <head>").
232
+ // That is not an API failure — there is simply nothing to open a PR for (e.g. a resumed
233
+ // branch whose earlier PR was merged with a merge commit, leaving the branch reachable
234
+ // from base). Signal it with null so the caller records a clean no-op instead of failing
235
+ // the run with GitHub's opaque 422.
236
+ if (res.status === 422 && /no commits between/i.test(detail))
237
+ return null;
238
+ const remedy = describePrOpenFailure(res.status, 'github');
239
+ const base = redactSecrets(`Failed to open PR (HTTP ${res.status}): ${detail.slice(0, 300)}`);
240
+ throw new HarnessFailure('api', remedy ? `${base}\n${remedy}` : base);
241
+ }
242
+ const body = (await res.json());
243
+ if (!body.html_url)
244
+ throw new HarnessFailure('api', 'GitHub did not return a PR url');
245
+ return body.html_url;
246
+ }
247
+ /** GitLab API headers for the PAT (the `PRIVATE-TOKEN` auth GitLab uses). */
248
+ function gitlabHeaders(token) {
249
+ return {
250
+ 'private-token': token,
251
+ accept: 'application/json',
252
+ 'user-agent': 'cat-factory-executor',
253
+ 'content-type': 'application/json',
254
+ };
255
+ }
256
+ /**
257
+ * Open a GitLab merge request (the analogue of {@link openPullRequest} for GitLab). The REST
258
+ * base + project path are derived from the clone URL's host, so it works for gitlab.com and a
259
+ * self-managed instance alike. `head`→`source_branch`, `base`→`target_branch`. On a duplicate
260
+ * (a resumed run whose branch already has an open MR — GitLab answers 409) the existing MR's
261
+ * web URL is returned instead of failing the run, mirroring the GitHub 422 handling.
262
+ */
263
+ async function openGitLabMergeRequest(opts) {
264
+ const apiBase = gitlabApiBaseFromCloneUrl(opts.cloneUrl);
265
+ const project = gitlabProjectPath(opts.cloneUrl);
266
+ const res = await withApiRetry(() => fetch(`${apiBase}/projects/${project}/merge_requests`, {
267
+ method: 'POST',
268
+ headers: gitlabHeaders(opts.ghToken),
269
+ body: JSON.stringify({
270
+ source_branch: opts.head,
271
+ target_branch: opts.base,
272
+ title: opts.pr.title,
273
+ description: opts.pr.body,
274
+ }),
275
+ ...(opts.signal ? { signal: opts.signal } : {}),
276
+ }), { signal: opts.signal });
277
+ if (!res.ok) {
278
+ const detail = await res.text().catch(() => '');
279
+ // GitLab returns 409 (sometimes 400) when an open MR already exists for this source
280
+ // branch; that is success for a resumed run — return the existing MR's url.
281
+ if ((res.status === 409 || res.status === 400) &&
282
+ /already exists|open merge request/i.test(detail)) {
283
+ const existing = await findOpenMergeRequest(apiBase, project, opts);
284
+ if (existing) {
285
+ if (opts.refreshExisting)
286
+ await refreshMergeRequest(apiBase, project, opts, existing);
287
+ return existing.url;
288
+ }
289
+ }
290
+ const remedy = describePrOpenFailure(res.status, 'gitlab');
291
+ const base = redactSecrets(`Failed to open merge request (HTTP ${res.status}): ${detail.slice(0, 300)}`);
292
+ throw new HarnessFailure('api', remedy ? `${base}\n${remedy}` : base);
293
+ }
294
+ const body = (await res.json());
295
+ if (!body.web_url)
296
+ throw new HarnessFailure('api', 'GitLab did not return a merge request url');
297
+ return body.web_url;
298
+ }
299
+ /**
300
+ * Rewrite an already-open MR's title and description — the GitLab half of
301
+ * {@link refreshPullRequest}, so a resumed run's agent briefing reaches both hosts alike.
302
+ * Best-effort for the same reason.
303
+ */
304
+ async function refreshMergeRequest(apiBase, project, opts, existing) {
305
+ if (existing.number === undefined)
306
+ return;
307
+ await fetch(`${apiBase}/projects/${project}/merge_requests/${existing.number}`, {
308
+ method: 'PUT',
309
+ headers: gitlabHeaders(opts.ghToken),
310
+ body: JSON.stringify({
311
+ title: opts.pr.title,
312
+ description: preserveManagedSection(existing.body, opts.pr.body),
313
+ }),
314
+ ...(opts.signal ? { signal: opts.signal } : {}),
315
+ }).catch(() => undefined);
316
+ }
317
+ /** Find the open GitLab MR for `opts.head`→`opts.base`, or undefined when there is none. */
318
+ async function findOpenMergeRequest(apiBase, project, opts) {
319
+ // Filter by BOTH branches: a source branch can have open MRs to several targets, so the
320
+ // source alone could match an MR against a different base than the one we just tried to open.
321
+ const query = new URLSearchParams({
322
+ source_branch: opts.head,
323
+ target_branch: opts.base,
324
+ state: 'opened',
325
+ });
326
+ const res = await fetch(`${apiBase}/projects/${project}/merge_requests?${query}`, {
327
+ headers: gitlabHeaders(opts.ghToken),
328
+ ...(opts.signal ? { signal: opts.signal } : {}),
329
+ });
330
+ if (!res.ok)
331
+ return undefined;
332
+ const list = (await res.json().catch(() => []));
333
+ const found = Array.isArray(list) ? list[0] : undefined;
334
+ if (!found?.web_url)
335
+ return undefined;
336
+ return {
337
+ url: found.web_url,
338
+ // `iid` (project-scoped) is what the update endpoint addresses, not the global `id`.
339
+ ...(typeof found.iid === 'number' ? { number: found.iid } : {}),
340
+ body: found.description ?? undefined,
341
+ };
342
+ }
343
+ /**
344
+ * Rewrite an already-open PR's title and description from `opts.pr` (a resumed run whose agent
345
+ * wrote a fresh reviewer briefing — see {@link OpenPullRequestOptions.refreshExisting}).
346
+ *
347
+ * Best-effort by construction: the work is already pushed and the PR already exists, so a failed
348
+ * refresh must degrade to the stale description rather than fail the run.
349
+ */
350
+ async function refreshPullRequest(opts, existing) {
351
+ if (existing.number === undefined)
352
+ return;
353
+ const apiBase = opts.apiBase ?? 'https://api.github.com';
354
+ const path = `${encodeURIComponent(opts.owner)}/${encodeURIComponent(opts.name)}`;
355
+ await fetch(`${apiBase}/repos/${path}/pulls/${existing.number}`, {
356
+ method: 'PATCH',
357
+ headers: {
358
+ authorization: `Bearer ${opts.ghToken}`,
359
+ accept: 'application/vnd.github+json',
360
+ 'user-agent': 'cat-factory-executor',
361
+ 'x-github-api-version': '2022-11-28',
362
+ 'content-type': 'application/json',
363
+ },
364
+ body: JSON.stringify({
365
+ title: opts.pr.title,
366
+ body: preserveManagedSection(existing.body, opts.pr.body),
367
+ }),
368
+ ...(opts.signal ? { signal: opts.signal } : {}),
369
+ }).catch(() => undefined);
370
+ }
371
+ /** Find the open PR for `opts.head` on `opts.base`, or undefined when there is none. */
372
+ async function findOpenPullRequest(opts) {
373
+ const apiBase = opts.apiBase ?? 'https://api.github.com';
374
+ // Encode the ref-derived query params: a branch/owner containing `&` or `#` would
375
+ // otherwise split the query string or inject an unintended parameter.
376
+ const query = new URLSearchParams({
377
+ head: `${opts.owner}:${opts.head}`,
378
+ base: opts.base,
379
+ state: 'open',
380
+ });
381
+ const path = `${encodeURIComponent(opts.owner)}/${encodeURIComponent(opts.name)}`;
382
+ const res = await fetch(`${apiBase}/repos/${path}/pulls?${query}`, {
383
+ headers: {
384
+ authorization: `Bearer ${opts.ghToken}`,
385
+ accept: 'application/vnd.github+json',
386
+ 'user-agent': 'cat-factory-executor',
387
+ 'x-github-api-version': '2022-11-28',
388
+ },
389
+ ...(opts.signal ? { signal: opts.signal } : {}),
390
+ });
391
+ if (!res.ok)
392
+ return undefined;
393
+ const list = (await res.json().catch(() => []));
394
+ const found = Array.isArray(list) ? list[0] : undefined;
395
+ if (!found?.html_url)
396
+ return undefined;
397
+ return {
398
+ url: found.html_url,
399
+ ...(typeof found.number === 'number' ? { number: found.number } : {}),
400
+ body: found.body ?? undefined,
401
+ };
402
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cat-factory/executor-harness",
3
- "version": "1.60.0",
3
+ "version": "1.62.0",
4
4
  "description": "Container payload: a thin TypeScript wrapper that runs the Pi coding agent against a cloned repo and opens a PR. Runs in the Cloudflare Container (and, in local native mode, as a host process); carries no secrets.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,8 +26,9 @@
26
26
  "hono": "^4.12.32",
27
27
  "typescript": "7.0.2",
28
28
  "vitest": "^4.1.10",
29
- "@cat-factory/server": "0.154.0",
30
- "@cat-factory/spend": "0.12.90"
29
+ "@cat-factory/kernel": "0.168.0",
30
+ "@cat-factory/spend": "0.12.97",
31
+ "@cat-factory/server": "0.158.0"
31
32
  },
32
33
  "scripts": {
33
34
  "build": "tsc -p tsconfig.json",
package/src/agent.ts CHANGED
@@ -22,16 +22,16 @@ import {
22
22
  fetchReferenceBranches,
23
23
  hasAgentChanges,
24
24
  headCommit,
25
- inferVcsProvider,
26
25
  mergeBranch,
27
- openPullRequest,
28
26
  prepareExistingCheckout,
29
27
  pushBranch,
30
28
  reinitAndPush,
31
29
  unmergedPaths,
32
30
  } from './git.js'
31
+ import { inferVcsProvider, openPullRequest } from './vcs-api.js'
33
32
  import type { PiRunStats, RunDiagnostics } from './pi.js'
34
33
  import type { EffortReport } from './effort.js'
34
+ import { applyPrDescription } from './pr-description.js'
35
35
  import {
36
36
  makeDirClaimer,
37
37
  noChangesReason,
@@ -998,6 +998,7 @@ async function runSingleRepoCoding(job: AgentJob, opts: RunOptions): Promise<Age
998
998
  validationReport,
999
999
  reproductionReport,
1000
1000
  effortReport,
1001
+ prDescription,
1001
1002
  } = await runCodingAgent(buildSingleRepoCodingSpec(job, pushBranch), opts)
1002
1003
  // Ralph loop: the harness-computed validation verdict, forwarded onto the coding result as
1003
1004
  // `ralphVerdict` so the backend's `toRunResult` lifts it onto `AgentRunResult.ralphVerdict`.
@@ -1073,7 +1074,11 @@ async function runSingleRepoCoding(job: AgentJob, opts: RunOptions): Promise<Age
1073
1074
  ghToken: job.ghToken,
1074
1075
  head: pushBranch,
1075
1076
  base: job.repo.baseBranch,
1076
- pr: job.pr,
1077
+ // The agent-authored briefing (title/body) wins field-wise over the dispatch-time text.
1078
+ pr: applyPrDescription(job.pr, prDescription),
1079
+ // A resumed run's PR is already open, so refresh it rather than lose the briefing to the
1080
+ // duplicate-PR 422 — only from a REAL briefing (see `refreshExisting` for why).
1081
+ ...(prDescription ? { refreshExisting: true } : {}),
1077
1082
  apiBase: job.githubApiBase,
1078
1083
  // The provider (set by the server from the configured backend) selects GitHub-PR vs
1079
1084
  // GitLab-MR authoritatively; the clone URL supplies the GitLab REST base + project path.