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
176 changes: 176 additions & 0 deletions packages/cli/src/commands/review/agent-prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3445,6 +3445,10 @@ describe('per-chunk retirement — cold territories stop costing a round', () =>
expect(out).toContain('2 auditors required this round');
expect(out).toContain('chunk 13 — retired: dry in rounds 1 and 2');
expect(out).toContain('certificate final');
// Pin the spelled cap number, not just the branch: a hardcoded `5-round
// cap leaves` in the note wording would otherwise ship silently and tell
// the orchestrator a false cap on exactly the huge-diff runs this targets.
expect(out).toContain('3-round cap leaves');
expect(out).not.toContain('next cold check round 4');
});

Expand Down Expand Up @@ -3472,6 +3476,19 @@ describe('per-chunk retirement — cold territories stop costing a round', () =>
.join('\n');
expect(msg).toContain('ROUND CAP');
expect(msg).toContain('round cap is 3');
// The load-bearing tail rules — the same verify-only / compose-floor
// contract the budget message's test pins and SKILL.md's round-cap
// bullet mirrors; a reword that drops any of these silently loosens
// the termination contract, so pin each.
expect(msg).toContain('agent-prompt --role verify');
expect(msg).toContain('never a hand-rolled agent');
expect(msg).toContain('compose floor');
expect(msg).toContain('Do NOT re-verify findings already');
Comment on lines +3483 to +3486

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Round-cap refusal message's wait-bound clauses not pinned — Failure scenario: A reword that drops either clause from the round-cap refusal message would pass the test suite, silently loosening the termination contract for the cap gate.

Suggested change
expect(msg).toContain('agent-prompt --role verify');
expect(msg).toContain('never a hand-rolled agent');
expect(msg).toContain('compose floor');
expect(msg).toContain('Do NOT re-verify findings already');
expect(msg).toContain('stop waiting on any verifier batch still out');
expect(msg).toContain('invent a fresh re-verification pass');
中文说明

round-cap 拒绝消息的 wait-bound 从句未被测试固定——失败场景:删除 round-cap 拒绝消息中任何一个从句的改写将不会导致测试失败,从而静默地放宽了 cap 门的终止契约。建议在 agent-prompt.test.ts 中为 round-cap 拒绝消息添加 stop waiting on any verifier batch still out 和 invent a fresh re-verification pass 的断言,与 deadline.test.ts 中的模式一致。

— deepseek-v4-flash via Qwen Code /review (v0.21.8)

// The wait-bound and no-fresh-pass clauses too — the budget message's
// test pins the same two for the sibling refusal; one bounded-tail
// protocol, both pin both.
expect(msg).toContain('stop waiting on any verifier batch still out');
expect(msg).toContain('invent a fresh re-verification pass');
// The marker is on disk so compose-review caps without the relay.
expect(readBudgetStop(plan)?.cause).toBe('round-cap');
expect(readBudgetStop(plan)?.cap).toBe(3);
Expand Down Expand Up @@ -3592,6 +3609,165 @@ describe('per-chunk retirement — cold territories stop costing a round', () =>
expect(readRoundStamps(plan)).toHaveLength(stampsBefore);
});

it('huge cap: a converged past-cap round exits 5, not the cap — convergence outranks it', () => {
// The ordering the PR documents four times (the convergence check runs
// BEFORE the round-cap gate) with no test pin: hoisting the cap check
// above it survives the whole suite. Round 5 is past the cap of 3, but
// its schedule has converged (every chunk twice-dry, odd round → all
// skipped), so it must exit 5 CONVERGED with NO marker — not exit 4 at
// the cap. History that lands convergence on an odd past-cap round: 13/14
// dry in rounds 1-2 (retire at 3), 15 whiffs round 1 then goes dry in
// 2-3, so round 3 (odd) builds only 15 and nothing converges before 5.
writeFileSync(
plan,
JSON.stringify({ ...PLAN, budget: { reverseAuditRounds: 3 } }),
);
const old = new Date(2020, 0, 1);
utimesSync(plan, old, old);
runRound(1);
auditorTranscript(recordOf(1, 13), DRY);
auditorTranscript(recordOf(1, 14), DRY);
auditorTranscript(recordOf(1, 15), WHIFF, { calls: 0 });
answerRound(2, { 13: DRY, 14: DRY, 15: DRY });
answerRound(3, { 15: DRY }); // 13,14 retired (odd → skipped); only 15 built
expect(keysOf(3)).toHaveLength(1);

const out = runRound(5); // 5 > cap 3, but the schedule has converged
expect(process.exitCode).toBe(5);
expect(out).toBe('');
const msg = (writeStderrLine as unknown as Mock).mock.calls
.map((c) => c[0])
.join('\n');
expect(msg).toContain('CONVERGED');
// Convergence outranks the cap: no round-cap refusal, no marker written.
expect(readBudgetStop(plan)).toBeNull();
});

it('huge cap: a CONVERGED exit clears a stale same-run round-cap marker', () => {
// Retry-after-refusal: round 4 (even) is refused at the cap — every
// retired chunk is DUE a cold check, so the schedule is not converged and
// 4 > 3 refuses, writing the marker. The orchestrator then asks for round
// 5, which converges. Nothing else unlinks budget-stop.json, so without
// the converged-branch clear the stale marker caps a verdict that
// legitimately converged.
writeFileSync(
plan,
JSON.stringify({ ...PLAN, budget: { reverseAuditRounds: 3 } }),
);
const old = new Date(2020, 0, 1);
utimesSync(plan, old, old);
runRound(1);
auditorTranscript(recordOf(1, 13), DRY);
auditorTranscript(recordOf(1, 14), DRY);
auditorTranscript(recordOf(1, 15), WHIFF, { calls: 0 });
answerRound(2, { 13: DRY, 14: DRY, 15: DRY });
answerRound(3, { 15: DRY });

runRound(4); // even → retired chunks due cold checks → not converged → cap refuses
expect(process.exitCode).toBe(4);
expect(readBudgetStop(plan)?.cause).toBe('round-cap');

process.exitCode = undefined;
const out = runRound(5); // odd → all skipped → converged
expect(process.exitCode).toBe(5);
expect(out).toBe('');
const msg = (writeStderrLine as unknown as Mock).mock.calls
.map((c) => c[0])
.join('\n');
expect(msg).toContain('CONVERGED');
// The marker channel is closed AND the relay channel is recalled: the
// refusal instructed the orchestrator to add the stop entry to
// unreviewedDimensions, and nothing but this sentence removes it once
// the marker (and with it compose-review's dedup splice) is gone.
expect(msg).toContain('remove it now — this convergence supersedes');
expect(readBudgetStop(plan)).toBeNull(); // the stale marker is cleared
});

it('huge cap: a CONVERGED exit clears a stale same-run time-budget marker too', () => {
// The clear is cause-blind, but both sibling clear tests produce their
// marker via the round-cap gate — a cause-conditional clear
// (`if (readBudgetStop(p)?.cause === 'round-cap') clearBudgetStop(p)`)
// passes them both and leaves a time-budget marker capping a verdict
// the audit legitimately converged. Cap 5 so even round 4 reaches the
// TIME gate instead of the cap gate: cold checks due → not converged →
// admitted at the cap, refused at the near deadline. Round 5 then
// converges and must clear the time-budget marker the same way.
writeFileSync(
plan,
JSON.stringify({ ...PLAN, budget: { reverseAuditRounds: 5 } }),
);
const old = new Date(2020, 0, 1);
utimesSync(plan, old, old);
runRound(1);
auditorTranscript(recordOf(1, 13), DRY);
auditorTranscript(recordOf(1, 14), DRY);
auditorTranscript(recordOf(1, 15), WHIFF, { calls: 0 });
answerRound(2, { 13: DRY, 14: DRY, 15: DRY });
answerRound(3, { 15: DRY });

process.env[DEADLINE_ENV] = String(Math.floor(Date.now() / 1000) + 60);
runRound(4); // even → not converged → 4 <= cap 5 → refused at the time gate
expect(process.exitCode).toBe(4);
expect(readBudgetStop(plan)?.entry).toBe(
'reverse audit — stopped before round 4 by the review time budget',
);

process.exitCode = undefined;
const out = runRound(5); // odd → all skipped → converged, before any gate
expect(process.exitCode).toBe(5);
expect(out).toBe('');
const msg = (writeStderrLine as unknown as Mock).mock.calls
.map((c) => c[0])
.join('\n');
expect(msg).toContain('CONVERGED');
expect(readBudgetStop(plan)).toBeNull(); // the stale time-budget marker is cleared
});

it('huge cap: a converged --chunk retry clears the stale cap marker too', () => {
// The --chunk gate threads the same convergence-first path with its own
// `args.plan`, but only the --all-chunks site's marker clear is pinned
// above: a converged per-chunk retry after a cap refusal must exit 5
// CONVERGED and clear the stale marker exactly like it, not exit 4 at
// the cap (the ordering) and not leave the marker capping a verdict the
// audit legitimately converged (the clear). Same retry-after-refusal
// history as the --all-chunks test.
writeFileSync(
plan,
JSON.stringify({ ...PLAN, budget: { reverseAuditRounds: 3 } }),
);
const old = new Date(2020, 0, 1);
utimesSync(plan, old, old);
runRound(1);
auditorTranscript(recordOf(1, 13), DRY);
auditorTranscript(recordOf(1, 14), DRY);
auditorTranscript(recordOf(1, 15), WHIFF, { calls: 0 });
answerRound(2, { 13: DRY, 14: DRY, 15: DRY });
answerRound(3, { 15: DRY });

runRound(4); // even → retired chunks due cold checks → cap refuses
expect(process.exitCode).toBe(4);
expect(readBudgetStop(plan)?.cause).toBe('round-cap');

process.exitCode = undefined;
(writeStdoutLine as unknown as Mock).mockClear();
(writeStderrLine as unknown as Mock).mockClear();
(agentPromptCommand.handler as (a: unknown) => void)({
plan,
role: 'reverse-audit',
findings,
chunk: 13,
round: 5,
});
expect(process.exitCode).toBe(5);
expect((writeStdoutLine as unknown as Mock).mock.calls).toHaveLength(0);
expect(keysOf(5)).toHaveLength(0);
const msg = (writeStderrLine as unknown as Mock).mock.calls
.map((c) => c[0])
.join('\n');
expect(msg).toContain('CONVERGED');
expect(readBudgetStop(plan)).toBeNull(); // the stale marker is cleared
});

it('a cold-check-only round is still built, admitted and stamped', () => {
answerRound(1, { 13: DRY, 14: DRY, 15: DRY });
answerRound(2, { 13: DRY, 14: DRY, 15: DRY });
Expand Down
33 changes: 27 additions & 6 deletions packages/cli/src/commands/review/agent-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { dirname, join, resolve } from 'node:path';
import { writeStdoutLine, writeStderrLine } from '../../utils/stdioHelpers.js';
import { launchToolBudget, reverseAuditRoundCap } from './lib/budget.js';
import {
clearBudgetStop,
expectedRoundSeconds,
readRoundStamps,
reverseAuditBudgetExhausted,
Expand Down Expand Up @@ -1885,8 +1886,14 @@ function admitReverseAuditRound(
`not rebuild or retry. A marker has been recorded and compose-review ` +
`will cap the verdict; still add \`${roundCapStopEntry(cap)}\` to ` +
`unreviewedDimensions so the terminal report agrees. If the cap ` +
`round reported findings whose verdicts have not landed, launch ` +
`their verifiers alone and wait, then proceed to Step 6.`,
`round reported findings whose verdicts have not landed, verify them ` +
`ONLY through \`agent-prompt --role verify\` (never a hand-rolled ` +
`agent) — it is gated on the compose floor and will refuse once too ` +
`little time remains; when the deadline is within that floor, stop ` +
`waiting on any verifier batch still out and compose with the tags ` +
`in hand. Do NOT re-verify findings already confirmed in earlier ` +
`rounds, and do NOT invent a fresh re-verification pass. Then ` +
`proceed to Step 6.`,
);
process.exitCode = 4;
return false;
Expand All @@ -1909,13 +1916,27 @@ function admitReverseAuditRound(
* twice over, so another round would audit nothing the history has not
* already answered. Not an error and not a gap — no record, no stamp, no
* disclosure owed; a round that builds nothing was never admitted.
*
* Clears any same-run stop marker first: a converged exit can follow an
* over-cap round the gate already refused (round 4 refused under cap 3,
* then round 5's schedule is converged — the convergence check runs before
* the cap gate), and that stale round-cap marker would otherwise cap a
* verdict the audit legitimately converged. The message also recalls the
* relay channel: the earlier refusal told the orchestrator to add its stop
* entry to unreviewedDimensions, and once the marker is gone the
* compose-review splice that dedups it no longer runs — only this
* instruction removes it.
*/
function refuseConverged(): void {
function refuseConverged(planPath: string): void {
clearBudgetStop(planPath);
writeStderrLine(
'CONVERGED: every chunk holds two consecutive substantive dry audits; ' +
'the reverse audit has converged — stop the loop and proceed to ' +
'Step 6. This is a clean convergence, not a gap: no ' +
'unreviewedDimensions entry is owed.',
'unreviewedDimensions entry is owed. If an earlier round-cap or ' +
'budget refusal told you to add its stop entry to ' +
'unreviewedDimensions, remove it now — this convergence supersedes ' +
'it.',
);
process.exitCode = 5;
}
Expand Down Expand Up @@ -1968,7 +1989,7 @@ function runAllChunks(
}

if (schedule !== null && schedule.converged) {
refuseConverged();
refuseConverged(planPath);
return;
}

Expand Down Expand Up @@ -2470,7 +2491,7 @@ function runAgentPrompt(args: AgentPromptArgs): void {
schedule = null;
}
if (schedule !== null && schedule.converged) {
refuseConverged();
refuseConverged(args.plan);
return;
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/cli/src/commands/review/compose-review.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,24 @@ describe('composeReview — event caps (round-7 Critical #2: caps must reach eve
expect(r.remediation.join(' ')).not.toContain('reverse audit:');
});

it('a round-cap stop does NOT suppress the not-built gap — its rebuild is admitted', () => {
// R4-9: the reverseByDesign exemption is time-budget-ONLY. A round-cap
// marker with zero reverse-audit records must not suppress the not-built
// gap the way a time-budget stop does: the cap gate refuses only
// `round > cap`, so the gap's FIX (rebuild `--round 1`) is admitted, and
// a local run has no deadline to refuse it at all. Reading the marker
// cause-blind would silently drop both the gap and its rebuild
// remediation for a run that audited nothing.
const plan = coveredPlan([]); // no reverse-audit ran — the not-built shape
writeRoundCapStop(plan, 3, 4);
const r = composeReview({ planPath: plan, env: ENV, modelId: MODEL });
// The round-cap marker still discloses and caps the verdict…
expect(r.event).toBe('COMMENT');
expect(r.body).toContain('reverse-audit round cap of 3');
// …but the not-built gap and its rebuild remediation are still owed.
expect(r.remediation.join(' ')).toContain('reverse audit:');
});

it('renders the budget stop bilingually on a Han-description PR', () => {
// Every sibling structural disclosure carries a zh pair; the budget stop
// used to ride the caller-prose path and posted English into both halves.
Expand Down
13 changes: 8 additions & 5 deletions packages/cli/src/commands/review/lib/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const SWEEP_FLOOR = 25;
/**
* The reverse-audit loop's full round cap (SKILL.md Step 5's "stop at the
* plan's `reverseAuditRounds` cap"). The normal value; a huge diff gets
* `HUGE_REVERSE_AUDIT_ROUNDS` instead. `retirement.ts` re-exports it.
* `HUGE_REVERSE_AUDIT_ROUNDS` instead. `compose-review` imports it directly.
*/
export const MAX_REVERSE_AUDIT_ROUNDS = 5;

Expand Down Expand Up @@ -232,10 +232,13 @@ export function reviewBudget(input: BudgetInput): ReviewBudget {

/**
* The reverse-audit round cap a plan's budget carries, for every reader
* that enforces or narrates it (the admission gate, the retirement
* scheduler, the cold-check note). A plan without the field — an older
* CLI — or a garbled value reads as the full cap: an old plan errs toward
* more auditing, never less, exactly like every other budget fallback.
* that enforces or narrates it (the admission gate and the cold-check
* note, both in `agent-prompt`; the retirement scheduler deliberately
* ignores the cap — whether a scheduled cold check is allowed is the note
* composer's question, not the schedule's). A plan without the field — an
* older CLI — or a garbled value reads as the full cap: an old plan errs
* toward more auditing, never less, exactly like every other budget
* fallback.
*
* The accepted range is floored at `HUGE_REVERSE_AUDIT_ROUNDS`, the
* smallest cap the CLI ever writes. A value of one or two is out of band
Expand Down
12 changes: 10 additions & 2 deletions packages/cli/src/commands/review/lib/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ export function verificationGaps(
(k) => k === 'reverse-audit' || k.startsWith('reverse-audit--'),
);
const reverse = bestDelivery(reverseKeys);
// A budget-stop marker means the round builder itself refused the reverse
// A TIME-budget stop marker means the round builder refused the reverse
// audit on the run's time budget. Exactly ONE gap shape is then by design:
// `not-built` — the refusal writes no record, so an audit with no records
// is the audit the gate stopped, and the gap's FIX (rebuild the round)
Expand All @@ -1441,7 +1441,15 @@ export function verificationGaps(
// hand-written round-1 launch is exactly as undelivered when round 3 later
// hits the budget, and suppressing it would let "stopped before round 3"
// imply the rounds that did run were faithful.
const budgetStopped = readBudgetStop(planPath) !== null;
//
// Only the time-budget cause earns this exemption. A ROUND-CAP stop does
// NOT: the cap gate refuses only `round > cap`, so the not-built gap's FIX
// (rebuild `--round 1`) is admitted, and a local run has no deadline to
// refuse it at all — the monotone-refusal premise fails twice. So a
// round-cap marker leaves the not-built gap and its rebuild remediation
// owed, exactly as if no marker were present.
const stop = readBudgetStop(planPath);
const budgetStopped = stop !== null && stop.cause !== 'round-cap';
const reverseByDesign = budgetStopped && reverse === 'not-built';
// A repairable reverse-audit gap only at high: medium is complete without it.
const reverseGap = !balancedMedium && !reverseByDesign && reverse !== 'ok';
Expand Down
Loading
Loading