Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions packages/cli/src/commands/review/agent-prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3688,6 +3688,48 @@ describe('per-chunk retirement — cold territories stop costing a round', () =>
expect(out).not.toContain('retirement:');
});

it('certification failures are diagnosed on stderr, chunk by chunk (#9206)', () => {
// The silent half of the reported run: chunks audited twice that are
// neither retired nor hot failed CERTIFICATION, and the round said
// nothing about it. The builder must name the bar each chunk fell at —
// on stderr; stdout stays the deliverable the orchestrator pastes.
answerRound(1, { 13: DRY, 14: DRY, 15: YIELD });
runRound(2);
auditorTranscript(recordOf(2, 13), WHIFF, { calls: 0 });
// 14's round-2 auditor left no transcript at all.
auditorTranscript(recordOf(2, 15), YIELD);

runRound(3);

const err = (writeStderrLine as unknown as Mock).mock.calls
.map((c) => c[0])
.join('\n');
expect(err).toContain('reverse-audit retirement certified nothing');
Comment thread
wenshao marked this conversation as resolved.
Resolved
expect(err).toContain('chunk 13 — round 2: no successful tool calls');
expect(err).toContain('chunk 14 — round 2: no matching transcript');
// A yielded chunk explains its own heat — no diagnostic for it.
expect(err).not.toContain('chunk 15');
});

it('a schedule with no readable transcripts names itself (#9206)', () => {
// The scheduler's catch used to swallow every exception without a word;
// a transcript-less round then retired nothing for the rest of the run,
// invisibly. The degradation direction stands — every chunk audited —
// but the round must say why nothing can retire.
answerRound(1, { 13: DRY, 14: DRY, 15: YIELD });
answerRound(2, { 13: DRY, 14: DRY, 15: YIELD });
delete process.env['QWEN_CODE_SESSION_ID'];

const out = runRound(3);

expect(out).toContain('3 auditors required this round — one per chunk.');
const err = (writeStderrLine as unknown as Mock).mock.calls
.map((c) => c[0])
.join('\n');
expect(err).toContain('reverse-audit retirement unavailable this round');
expect(err).toContain('auditing every chunk');
});

it('huge cap: a chunk dry in rounds 1 and 2 retires with a final certificate', () => {
// Under the reduced 3-round cap, chunk 13's next cold check (round 4) is
// past the cap, so the retirement note must read `certificate final`, not
Expand Down
38 changes: 34 additions & 4 deletions packages/cli/src/commands/review/agent-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
type DiffChunk,
} from './lib/diff-plan.js';
import {
promptRecordDir,
recordPrompt,
writeBrief,
writeFindingsFile,
Expand Down Expand Up @@ -2028,12 +2029,18 @@ function runAllChunks(
? report.diffPathAbsolute
: undefined,
);
} catch {
} catch (err) {
// Transcripts unavailable, an unreadable plan stat, anything: the
// schedule is an optimization, and a broken optimizer must degrade to
// today's behaviour — every territory audited — never to fewer
// auditors. `null` below means "everything is due".
// auditors. `null` below means "everything is due". But not SILENTLY
// (#9206): a schedule that dies here retires nothing for the rest of
// the run, and the round's own output is where the reader can see it.
schedule = null;
writeStderrLine(
`NOTE: reverse-audit retirement unavailable this round — ` +
Comment thread
wenshao marked this conversation as resolved.
Resolved
Comment thread
wenshao marked this conversation as resolved.
Resolved
`${(err as Error).message ?? String(err)} — auditing every chunk.`,
Comment thread
wenshao marked this conversation as resolved.
Resolved
);
Comment thread
wenshao marked this conversation as resolved.
Resolved
Comment thread
wenshao marked this conversation as resolved.
Resolved
}
}

Expand All @@ -2042,6 +2049,24 @@ function runAllChunks(
return;
}

// A chunk audited twice that is neither retired nor hot failed
// CERTIFICATION somewhere; the schedule names the bar per round (#9206 —
// the silent version of this ran a 12-chunk loop five rounds to the cap
// with no word of why nothing retired). stderr, never stdout: the round
// blocks below are the deliverable the orchestrator pastes.
if (schedule !== null && schedule.diagnostics.length > 0) {
writeStderrLine(
`NOTE: reverse-audit retirement certified nothing for ` +
`${schedule.diagnostics.length} twice-audited chunk(s) — they stay ` +
`under audit (the safe direction), but a chunk that looks dry and ` +
`never retires is the cost this schedule exists to stop paying. ` +
`The bar each round fell at:\n` +
schedule.diagnostics.join('\n') +
`\nCompare the recorded prompts in ${promptRecordDir(planPath)} ` +
`against this session's subagent transcripts to see the mismatch.`,
);
}
Comment thread
wenshao marked this conversation as resolved.
Resolved

// The budget gate, deferred here from the single-build path for
// --all-chunks rounds so the convergence check above runs FIRST: a
// converged audit is done — it owes no round, and refusing it would cap a
Expand Down Expand Up @@ -2541,10 +2566,15 @@ function runAgentPrompt(args: AgentPromptArgs): void {
? report.diffPathAbsolute
: undefined,
);
} catch {
} catch (err) {
// Same degradation as the round builder: an unreadable history must
// fall back to building the auditor, never to refusing it.
// fall back to building the auditor, never to refusing it — named,
// as the round builder names it (#9206).
schedule = null;
writeStderrLine(
`NOTE: reverse-audit retirement unavailable this round — ` +
`${(err as Error).message ?? String(err)} — auditing the chunk.`,
Comment thread
wenshao marked this conversation as resolved.
Resolved
);
Comment thread
wenshao marked this conversation as resolved.
Resolved
}
if (schedule !== null && schedule.converged) {
refuseConverged(args.plan);
Expand Down
68 changes: 67 additions & 1 deletion packages/cli/src/commands/review/cleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
execFileSync: vi.fn(),
existsSync: vi.fn(() => false),
readdirSync: vi.fn(() => []),
// The return type is declared so `mockReturnValue` can take string arrays —
// the sweep-retention tests hand it the tmp-dir listing.
readdirSync: vi.fn((): string[] => []),
Comment thread
wenshao marked this conversation as resolved.
Resolved
readFileSync: vi.fn((_path: string): string => {
throw Object.assign(new Error('ENOENT'), { code: 'ENOENT' });
}),
Expand Down Expand Up @@ -167,6 +169,70 @@ describe('runCleanup', () => {
{ recursive: true, force: true },
);
});

it('keeps the record directory of a NON-CONVERGED reverse audit (#9206)', () => {
// The loop writes its stop marker inside the record directory when it
// runs to the round cap (or the budget) without converging, and clears
// it on a clean convergence — so a marker on disk is exactly the run
// whose certification history must survive the sweep for diagnosis.
mocks.execFileSync.mockReturnValue(Buffer.from(''));
mocks.existsSync.mockReturnValue(true);
mocks.readdirSync.mockReturnValue([
'qwen-review-pr-123-fetch.json',
'qwen-review-pr-123-fetch-prompts',
'qwen-review-pr-123-diff.txt',
]);
mocks.readFileSync.mockImplementation((path: string): string => {
if (path.endsWith('budget-stop.json')) {
return JSON.stringify({
cause: 'round-cap',
cap: 5,
entry: 'reverse audit — did not converge within the 5-round cap of 5',
entryZh: '反向审计——在 5 轮的反审轮数上限内未收敛',
round: 6,
remainingSeconds: 0,
reserveSeconds: 0,
atMs: Date.now(),
});
}
// The fetch report without `fetchedAt`: the bypass audit skips itself.
return JSON.stringify({});
});

runCleanup('pr-123');

const removed = mocks.rmSync.mock.calls.map((c) => c[0]);
expect(removed).toContain('/repo/.qwen/tmp/qwen-review-pr-123-fetch.json');
expect(removed).toContain('/repo/.qwen/tmp/qwen-review-pr-123-diff.txt');
expect(removed).not.toContain(
'/repo/.qwen/tmp/qwen-review-pr-123-fetch-prompts',
);
expect(mocks.writeStdoutLine).toHaveBeenCalledWith(
expect.stringContaining(
'Kept /repo/.qwen/tmp/qwen-review-pr-123-fetch-prompts',
),
);
});

it('still sweeps the record directory once the loop converged (#9206)', () => {
// A converged run cleared its marker (`refuseConverged` removes it): the
// certification history earned nothing, and the sweep takes it like any
// other side file. Same entries as the retention test, no marker.
mocks.execFileSync.mockReturnValue(Buffer.from(''));
mocks.existsSync.mockReturnValue(true);
mocks.readdirSync.mockReturnValue([
'qwen-review-pr-123-fetch.json',
'qwen-review-pr-123-fetch-prompts',
]);
mocks.readFileSync.mockReturnValue(JSON.stringify({}));

runCleanup('pr-123');

expect(mocks.rmSync).toHaveBeenCalledWith(
'/repo/.qwen/tmp/qwen-review-pr-123-fetch-prompts',
{ recursive: true, force: true },
);
});
});

describe('findUnsanctionedIssueComments', () => {
Expand Down
31 changes: 29 additions & 2 deletions packages/cli/src/commands/review/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { clearReviewWorktreeLease } from '../../services/review-worktree-lease.j
import { currentUser, getGhHost, ghApiAll, setGhHost } from './lib/gh.js';
import { parseReceiptIds } from './lib/receipt.js';
import { refExists, releaseWorktree } from './lib/git.js';
import { readBudgetStop } from './lib/deadline.js';
import {
worktreePath,
probeWorktreePath,
Expand Down Expand Up @@ -462,9 +463,35 @@ export function runCleanup(target: string): void {
);
}

// #9206: a prompt-record directory whose loop STOPPED WITHOUT CONVERGING
// is the only certification history there is — the evidence a
// never-retiring reverse-audit loop needs to diagnose itself, which the
// sweep would otherwise destroy unread. The loop writes its stop marker
// (round-cap or budget) inside that directory, and a clean convergence
// clears it, so a marker on disk names the non-converged stop exactly.
// The decision is made BEFORE the sweep runs: the plan file the marker's
// epoch fence reads is itself one of the swept entries.
const preserved = new Set<string>();
for (const file of tmpEntries) {
if (!file.startsWith(prefix) || !file.endsWith('-prompts')) continue;
const planCandidate = join(
REVIEW_TMP_DIR,
`${file.slice(0, -'-prompts'.length)}.json`,
);
if (readBudgetStop(planCandidate) !== null) preserved.add(file);
Comment thread
wenshao marked this conversation as resolved.
Resolved
Comment thread
wenshao marked this conversation as resolved.
Resolved
}

for (const file of tmpEntries) {
if (!file.startsWith(prefix)) continue;
const full = join(REVIEW_TMP_DIR, file);
if (preserved.has(file)) {
writeStdoutLine(
`Kept ${full}: this review's reverse audit stopped without ` +
`converging (stop marker on disk) — the record directory is the ` +
`evidence for diagnosing it; remove it manually once done.`,
);
continue;
}
try {
// Not every side file is a file. `agent-prompt` records what it handed each
// agent in `<plan>-prompts/`, a directory under this same prefix, and
Expand All @@ -485,8 +512,8 @@ export function runCleanup(target: string): void {

// "Nothing to clean" is a claim about the tree, not about this run's luck. It
// is only true when there was nothing there — not when there was and we could
// not get rid of it.
if (!removedAny && !failedAny) {
// not get rid of it, and not when an entry was deliberately kept.
if (!removedAny && !failedAny && preserved.size === 0) {
Comment thread
wenshao marked this conversation as resolved.
Resolved
writeStdoutLine(`Nothing to clean for target "${target}".`);
}
}
Expand Down
Loading
Loading