Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 110 additions & 1 deletion bin/contributor-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ const PR_CLAIM_FOLLOWUP_ANCHOR = 'Your verdict says complete, but no PR for this
// follow-up: a bare `complete` with no PR is booked evidence-less by the hub
// (same issue), but it is not a contradiction the relay can put to the agent.
const PR_CLAIM_PATTERN = /\bPRs?\b|pull[ -]request|\bopened\b/i;
// A PR the agent opened inside its own fork (see detectForkPRURLs). The anchor
// is the message's fixed opening, so followUpAnswered() can find the echo.
const FORK_PR_FOLLOWUP_ANCHOR = 'That PR is in your fork, not in ';
function buildForkPRFollowUpMessage(url, repo) {
const owner = (/^https:\/\/github\.com\/([^/]+)\//.exec(url) || [])[1] || '<your-login>';
return `${FORK_PR_FOLLOWUP_ANCHOR}${repo}, so the hive cannot see it: ${url}. ` +
`Close it, then open it again against the upstream repository with 'gh pr create --repo ${repo} --head ${owner}:<your-branch>' ` +
`(keep the --base the task gave you), and print the HIVE_VERDICT line again on its own line.`;
}

const BYTES_PER_MIB = 1024 * 1024;
const DEFAULT_HEADLESS_MAX_OUTPUT_MIB = 16;
Expand Down Expand Up @@ -6557,6 +6566,40 @@ function detectPRURL(lines, repo) {
return urls.length > 0 ? urls[0] : '';
}

// detectForkPRURLs finds this task's PR opened in the wrong place: a URL in
// <someone>/<same-name> rather than in the task repo. detectPRURLs() is right
// to never credit it (a PR in another repository is never this task's PR), but
// it dropped it without a word: kylerankin/chairlift#3 was the agent's fix for
// projectbluefin/chairlift#798, opened with base and head both in the fork, and
// the issue sat at 010 for nine hours. Only the agent's own is counted — the
// fork owner is the contributor's login, or the URL is on a verdict line — so
// a researched PR in some other fork of the repo is still ignored.
function detectForkPRURLs(lines, repo, login) {
if (!Array.isArray(lines) || typeof repo !== 'string') return [];
const [owner, name] = repo.split('/');
if (!owner || !name) return [];
const me = typeof login === 'string' ? login.trim().toLowerCase() : '';
const URL_RE = /https:\/\/github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\/pull\/\d+/g;
const seen = new Set();
const out = [];
for (let i = lines.length - 1; i >= 0; i--) {
const line = lines[i];
if (typeof line !== 'string') continue;
const verdictLine = isHiveVerdictLine(line);
let m;
URL_RE.lastIndex = 0;
while ((m = URL_RE.exec(line)) !== null) {
const urlOwner = m[1].toLowerCase();
if (m[2].toLowerCase() !== name.toLowerCase() || urlOwner === owner.toLowerCase()) continue;
if (!verdictLine && (!me || urlOwner !== me)) continue;
if (seen.has(m[0])) continue;
seen.add(m[0]);
out.push(m[0]);
}
}
return out;
}

// ── Did THIS task open that PR? (kubestellar/hive#6662) ──────────────────────
//
// The precedence at the completion site reads "a visible PR contradicts
Expand Down Expand Up @@ -7313,6 +7356,10 @@ function prClaimFollowUpAnswered(lines) {
return followUpAnswered(lines, PR_CLAIM_FOLLOWUP_ANCHOR);
}

function forkPRFollowUpAnswered(lines) {
return followUpAnswered(lines, FORK_PR_FOLLOWUP_ANCHOR);
}

function followUpAnswered(lines, anchor) {
if (!Array.isArray(lines)) return true;
let echoAt = -1;
Expand Down Expand Up @@ -8010,6 +8057,8 @@ let postVerdictReviewRequested = false;
let lastTickConcernLines = new Set();
// #7862: whether this task's one PR-less-complete follow-up has been spent.
let prClaimFollowUpRequested = false;
// Whether this task's one "that PR is in your fork" follow-up has been spent.
let forkPRFollowUpRequested = false;
// #7907: the verdict LINE currently on the pane and when a tick first saw it.
// A different line (the second verdict after a #7759 follow-up) starts a new
// sighting; the same line ages toward VERDICT_SETTLE_MS.
Expand All @@ -8020,6 +8069,7 @@ function resetPostVerdictReviewState() {
postVerdictReviewRequested = false;
lastTickConcernLines = new Set();
prClaimFollowUpRequested = false;
forkPRFollowUpRequested = false;
verdictFirstSeen = null;
}

Expand Down Expand Up @@ -9325,6 +9375,8 @@ function maybeRequestPRForClaimedComplete(tmuxLines, verdict, prFinding) {
if (verdict.verdict !== HIVE_VERDICT_COMPLETE) return false;
if (!PR_CLAIM_PATTERN.test(verdict.reason || '')) return false;
if (prClaimFollowUpRequested) return false;
// The fork follow-up already told the agent exactly where its PR went.
if (forkPRFollowUpRequested) return false;
if (prFinding && prFinding.url) return false;

prClaimFollowUpRequested = true;
Expand All @@ -9349,6 +9401,49 @@ function maybeRequestPRForClaimedComplete(tmuxLines, verdict, prFinding) {
return true;
}

// ── A PR opened in the contributor's own fork ────────────────────────────────
//
// `gh pr create` without --repo takes its base repo from the clone, and a
// clone reused across tasks can carry a default that points at the fork. The
// prompt now names --repo; this is the relay's half for an agent that still
// lands one there: the PR is never booked as the task's (detectPRURLs keeps
// that rule), but the agent is told once, while it can still act, instead of
// the task ending as if nothing shipped. Called where the agent is not
// mid-turn: as the task would finalize (verdict or idle chrome), and as the
// stall backstop is about to give up on a pane that has sat unchanged — the
// shape of the chairlift case, where the agent opened the fork PR and the
// task failed 25 minutes later on "no pane activity". Same bounds as #7862:
// once per task, budget spent before typing, finalization held until the
// agent re-prints a verdict or goes idle.
function maybeRequestUpstreamPRForForkPR(paneScanLines, tmuxLines, prFinding) {
if (!currentTask || currentTask.kind !== 'issue') return false;
if (forkPRFollowUpRequested) return false;
if (prFinding && prFinding.url) return false;
const urls = detectForkPRURLs(paneScanLines, currentTask.repo, CONTRIBUTOR_LOGIN);
if (urls.length === 0) return false;

forkPRFollowUpRequested = true;
console.warn(`Task ${currentTask.task_id}: the agent's PR ${urls[0]} is in its fork, not in ${currentTask.repo} — ` +
`not booking it as this task's PR; asking the agent once to reopen it upstream`);
try {
tmuxSendNudge(buildForkPRFollowUpMessage(urls[0], currentTask.repo));
} catch (e) {
console.error('Failed to send the fork-PR follow-up; continuing without it:', e.message);
return false;
}
send({
type: 'task_progress',
seq: nextSeq(),
task_id: currentTask.task_id,
task_gen: currentTask.task_gen,
status: 'working',
summary: `Agent opened its PR in its fork (${urls[0]}), not in ${currentTask.repo}; asked it once to reopen it upstream`,
tmux_output: tmuxLines,
...progressModelFields(),
});
return true;
}

function progressTick() {
lastProgressTick = Date.now();
if (!currentTask) return;
Expand Down Expand Up @@ -9544,7 +9639,8 @@ function progressTick() {
lastTickConcernLines = new Set(reviewMarkers ? paneScanLines.filter(l => reviewMarkers.concern.test(l)) : []);
const secondVerdictPending = !!completionVerdict && (
(postVerdictReviewRequested && !postVerdictReviewAnswered(paneScanLines)) ||
(prClaimFollowUpRequested && !prClaimFollowUpAnswered(paneScanLines)));
(prClaimFollowUpRequested && !prClaimFollowUpAnswered(paneScanLines)) ||
(forkPRFollowUpRequested && !forkPRFollowUpAnswered(paneScanLines)));

// Chrome-idle grace (#5376). classifyTmuxPane() saying IDLE_COMPLETE is now
// only a hint; it must repeat across CHROME_IDLE_GRACE_TICKS ticks before it
Expand Down Expand Up @@ -9679,6 +9775,11 @@ function progressTick() {
: null;

// #7862: and its one chance to back a `complete` with the PR it implies.
// A PR opened in the agent's own fork: its one chance to reopen it upstream.
// Ahead of #7862, whose "no PR exists" would be the vaguer of the two.
if (finalizing && maybeRequestUpstreamPRForForkPR(paneScanLines, tmuxLines, prFinding)) {
return;
}
if (verdictCompletes && maybeRequestPRForClaimedComplete(tmuxLines, completionVerdict, prFinding)) {
return;
}
Expand Down Expand Up @@ -9852,6 +9953,10 @@ function progressTick() {
// immediately — see the comment above PANE_STALL_CONFIRM_TICKS for why a
// single instant cannot distinguish "stuck" from "about to finish".
if (paneStallConfirmed(tmuxLines)) {
// A pane frozen after the agent opened its PR in its fork is an agent
// waiting at its prompt, not a dead one: tell it before giving up. The
// typed follow-up changes the pane, which restarts the stall clock.
if (maybeRequestUpstreamPRForForkPR(paneScanLines, tmuxLines, null)) return;
// The CLI may still be mid-turn on the task we are about to give up on
// (observed live: a slow `gh pr create` returned, with a real PR link,
// seconds after the stall verdict). Relaunch it so the NEXT task starts
Expand Down Expand Up @@ -10852,6 +10957,10 @@ if (process.env.HIVE_RELAY_TEST_MODE === '1') {
getPRClaimFollowUpRequested: () => prClaimFollowUpRequested,
PR_CLAIM_FOLLOWUP_MESSAGE,
PR_CLAIM_FOLLOWUP_ANCHOR,
detectForkPRURLs,
buildForkPRFollowUpMessage,
getForkPRFollowUpRequested: () => forkPRFollowUpRequested,
FORK_PR_FOLLOWUP_ANCHOR,
PR_CLAIM_PATTERN,
tmuxSessionHasAttachedClient,
tmuxSessionHumanPresence,
Expand Down
92 changes: 92 additions & 0 deletions bin/contributor-relay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16248,6 +16248,98 @@ test('#7862 detectPRURLs synthesizes a candidate from "PR #N" on the verdict lin
} finally { teardown(relay); }
});

// ── A PR opened in the contributor's own fork ────────────────────────────────
//
// projectbluefin/chairlift#798: the agent opened kylerankin/chairlift#3, head
// and base both in its fork. detectPRURLs() rightly never credited it, but
// nothing told the agent, and the task failed 25 minutes later on a frozen
// pane while the issue sat at 010 for nine hours.

test('detectForkPRURLs finds the agent\'s own fork PR, and only that', () => {
const relay = loadRelay({ backend: 'copilot' });
try {
const fork = 'https://github.com/kylerankin/chairlift/pull/3';
assert.deepStrictEqual(relay.detectForkPRURLs([`Created ${fork}`], 'projectbluefin/chairlift', 'kylerankin'), [fork],
'a PR in <login>/<same-name> is the agent\'s, in the wrong place');
assert.deepStrictEqual(relay.detectForkPRURLs([`Created ${fork}`], 'projectbluefin/chairlift', 'KyleRankin'), [fork],
'the login compares case-insensitively');
assert.deepStrictEqual(relay.detectForkPRURLs([`HIVE_VERDICT: complete — PR ${fork}`], 'projectbluefin/chairlift', ''), [fork],
'on a verdict line it is the agent\'s own even when the relay does not know its login');
assert.deepStrictEqual(relay.detectForkPRURLs(['see https://github.com/someone/chairlift/pull/9'], 'projectbluefin/chairlift', 'kylerankin'), [],
'another fork\'s PR seen while researching is not the agent\'s');
assert.deepStrictEqual(relay.detectForkPRURLs(['see https://github.com/kylerankin/other/pull/9'], 'projectbluefin/chairlift', 'kylerankin'), [],
'a PR in an unrelated repository is research, as before');
assert.deepStrictEqual(relay.detectForkPRURLs(['https://github.com/projectbluefin/chairlift/pull/808'], 'projectbluefin/chairlift', 'kylerankin'), [],
'a PR in the task repo is detectPRURLs\' business, not this one\'s');
assert.deepStrictEqual(relay.detectForkPRURLs([`Created ${fork}`], 'projectbluefin/chairlift', 'kylerankin'),
relay.detectForkPRURLs([`Created ${fork}`, `again ${fork}`], 'projectbluefin/chairlift', 'kylerankin'), 'deduplicated');
assert.deepStrictEqual(relay.detectPRURLs([`HIVE_VERDICT: complete — PR ${fork}`], 'projectbluefin/chairlift'), [],
'and it is still never a candidate for the task\'s own PR');
assert.match(relay.buildForkPRFollowUpMessage(fork, 'projectbluefin/chairlift'),
/gh pr create --repo projectbluefin\/chairlift --head kylerankin:<your-branch>/);
} finally { teardown(relay); }
});

test('a complete whose PR is in the agent\'s fork gets one follow-up and is never booked with that PR', () => {
const fork = 'https://github.com/test-contributor/bar/pull/3';
let pane = 'Working…';
const relay = loadRelay({ backend: 'copilot', paneText: () => pane, prMeta: new Error('gh: offline'), env: { HIVE_CONTRIBUTOR_USERNAME: 'test-contributor' } });
const log = console.log; const warn = console.warn; console.log = () => {}; console.warn = () => {};
try {
dispatchTask(relay, 'ct-fork-pr', 140);
const before = relay.__tmuxSends().length;
pane = `Created ${fork}\nHIVE_VERDICT: complete — PR opened\n${IDLE_PANE}`;
relay.__crashTick();
assert.strictEqual(relay.__sent.filter(m => m.type === 'task_complete').length, 0, 'held for the agent to move the PR');
const sends = relay.__tmuxSends().slice(before);
assert.strictEqual(sends.filter(c => c.includes(relay.FORK_PR_FOLLOWUP_ANCHOR)).length, 1, `one fork follow-up: ${JSON.stringify(sends)}`);
assert.ok(!sends.some(c => c.includes(relay.PR_CLAIM_FOLLOWUP_ANCHOR)), 'the vaguer #7862 follow-up does not also fire');
assert.ok(relay.__sent.some(m => m.type === 'task_progress' && /in its fork/.test(m.summary || '')));
assert.strictEqual(relay.getForkPRFollowUpRequested(), true);

// Mid-turn under the echoed follow-up: nothing finalizes, nothing repeats.
const msg = relay.buildForkPRFollowUpMessage(fork, 'foo/bar');
pane = `Created ${fork}\nHIVE_VERDICT: complete — PR opened\n> ${msg}\nWorking…`;
relay.__crashTick();
assert.strictEqual(relay.__sent.filter(m => m.type === 'task_complete').length, 0);
assert.strictEqual(relay.__tmuxSends().filter(c => c.includes(relay.FORK_PR_FOLLOWUP_ANCHOR)).length, 1, 'no second follow-up');

// The agent reopened it upstream and re-printed the verdict.
pane = `Created ${fork}\nHIVE_VERDICT: complete — PR opened\n> ${msg}\nCreated https://github.com/foo/bar/pull/205\nHIVE_VERDICT: complete — PR opened\n${IDLE_PANE}`;
relay.__crashTick();
const completed = relay.__sent.filter(m => m.type === 'task_complete');
assert.strictEqual(completed.length, 1);
assert.strictEqual(completed[0].pr_url, 'https://github.com/foo/bar/pull/205', 'the upstream PR is the task\'s, never the fork one');
} finally { console.log = log; console.warn = warn; teardown(relay); }
});

test('a pane frozen after a fork PR gets the follow-up instead of the stall failure', () => {
const fork = 'https://github.com/test-contributor/bar/pull/3';
let capture = `Created ${fork}\nwaiting`;
const relay = loadRelay({ backend: 'agy', paneText: () => capture, env: { HIVE_CONTRIBUTOR_USERNAME: 'test-contributor' } });
const warn = console.warn; console.warn = () => {};
try {
relay.setCliReady(true);
assignTask(relay, 't-fork-stall');
relay.__stallTick();
relay.__agePaneStallClock(relay.PANE_STALL_TIMEOUT_MS + 1);
relay.__stallTick();
relay.__agePaneStallClock(relay.PANE_STALL_TIMEOUT_MS + 1);
const before = relay.__tmuxSends().length;
relay.__stallTick(); // would confirm the stall and fail the task
assert.strictEqual(relay.__sent.filter(m => m.type === 'task_failed').length, 0, 'the waiting agent is told, not failed');
assert.strictEqual(relay.__tmuxSends().slice(before).filter(c => c.includes(relay.FORK_PR_FOLLOWUP_ANCHOR)).length, 1);
assert.strictEqual(relay.getCurrentTask() && relay.getCurrentTask().task_id, 't-fork-stall');

// Spent once: a pane that freezes again is failed as before.
relay.__agePaneStallClock(relay.PANE_STALL_TIMEOUT_MS + 1);
relay.__stallTick();
relay.__agePaneStallClock(relay.PANE_STALL_TIMEOUT_MS + 1);
relay.__stallTick();
assert.strictEqual(relay.__sent.filter(m => m.type === 'task_failed').length, 1, 'one follow-up per task, then the backstop stands');
} finally { console.warn = warn; teardown(relay); }
});

// ── hivecommons/hive#13146: the `wrong_base` verdict ─────────────────────────

test('#13146 wrong_base carries a release branch without becoming already-done', () => {
Expand Down
1 change: 1 addition & 0 deletions changelog.d/fixed-contributor-pr-in-fork.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A contributor agent working from a fork could open its fix PR inside its own fork (head and base both in the fork), because the task prompt never told `gh pr create` which repository to target and a clone reused across tasks can default to the fork. The relay rightly never credited that PR, but dropped it without a word, so the issue stayed actionable with the fix invisible (projectbluefin/chairlift#798 waited nine hours behind kylerankin/chairlift#3). The prompt now names `--repo <upstream>` (and `--head <login>:<branch>` from a fork) and asks the agent to confirm the PR URL is under the upstream repo; the relay tells the agent once to reopen a PR it finds in the agent's fork, when the task would finalize or before the stall backstop fails a pane frozen after it, and logs it as a warning.
2 changes: 1 addition & 1 deletion src/docs/contributor-relay.md
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ What the GraphQL read does not cover:

The hub logs `PR verification: REST read was rate limited; pull request read through GraphQL instead` (info) when the GraphQL read settled the lookup, and `PR verification: REST read was rate limited and the GraphQL fallback did not settle it; result is the REST failure` (warning, with both errors) when it did not. `/api/gh-rate-limits` lists the read under caller `hive:pullrequest_verify` on endpoint `/graphql`.

The relay's side of the contract is to make the claim *checkable* before it is sent, which is what the fixes below do: it holds a `complete` that claims a PR none can be attributed to and asks for the PR or a `no_work_needed` ([#7862](https://github.com/hivecommons/hive/issues/7862)); it treats `no_work_needed` as authoritative when both sentinels appear ([#7861](https://github.com/hivecommons/hive/issues/7861)); the prompt fixes the sentinel's *timing* — opening the PR is finishing, do not wait on CI ([#7858](https://github.com/hivecommons/hive/issues/7858)); and review notes the advisor leaves under the final verdict are recorded on the PR and in the summary rather than lost ([#7759](https://github.com/hivecommons/hive/issues/7759), [#7879](https://github.com/hivecommons/hive/issues/7879)). Whether verdict handling joins the `src/formal/contribute-lease/` model's scope is an open architect/maintainer decision tracked on #7864; the table above is the informal spec such a model would have to agree with.
The relay's side of the contract is to make the claim *checkable* before it is sent, which is what the fixes below do: it holds a `complete` that claims a PR none can be attributed to and asks for the PR or a `no_work_needed` ([#7862](https://github.com/hivecommons/hive/issues/7862)); when the agent opened its PR in its own fork instead of the task repo (`<login>/<same-name>`, or such a URL on the verdict line), it never books that PR as the task's but tells the agent once to reopen it with `gh pr create --repo <upstream>`, both as the task would finalize and before the stall backstop gives up on a pane frozen after it (projectbluefin/chairlift#798); the task prompt itself names `--repo <upstream>` for the PR, and `--head <login>:<branch>` from a fork; it treats `no_work_needed` as authoritative when both sentinels appear ([#7861](https://github.com/hivecommons/hive/issues/7861)); the prompt fixes the sentinel's *timing* — opening the PR is finishing, do not wait on CI ([#7858](https://github.com/hivecommons/hive/issues/7858)); and review notes the advisor leaves under the final verdict are recorded on the PR and in the summary rather than lost ([#7759](https://github.com/hivecommons/hive/issues/7759), [#7879](https://github.com/hivecommons/hive/issues/7879)). Whether verdict handling joins the `src/formal/contribute-lease/` model's scope is an open architect/maintainer decision tracked on #7864; the table above is the informal spec such a model would have to agree with.

### A wrong base is routing evidence, not already-done evidence

Expand Down
Loading
Loading