Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
668 changes: 660 additions & 8 deletions packages/cli/src/commands/review/agent-prompt.test.ts

Large diffs are not rendered by default.

283 changes: 240 additions & 43 deletions packages/cli/src/commands/review/agent-prompt.ts

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion packages/cli/src/commands/review/lib/agent-briefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ export interface Brief {
brief: string;
}

/**
* The model receipt the reverse-audit brief hands every auditor as its
* example. Exported so the retirement classifier can refuse a clause that
* parrots it — measured: agents repeat what they are handed, and a receipt
* the prompt wrote is not evidence of a walk.
*/
export const REVERSE_AUDIT_EXAMPLE_RECEIPT =
"No issues found — re-walked the reconnect state machine and the two changed exports' call sites; every gap I checked was already in the list";

export const BRIEFS: Record<RoleId, Brief> = {
'0': {
label: 'Agent 0: Issue fidelity & root-cause ownership',
Expand Down Expand Up @@ -644,7 +653,7 @@ The asymmetry cuts both ways: confirming also requires the trace, and a finding
- **Report only Critical or Suggestion.** Do not report Nice to have.
- A found gap uses the standard finding format (with \`Source: [review]\`), including its failure scenario — your findings go through the same verification as any other, so they must carry the evidence a verifier can trace.

If you find no new gap in your scope, say so **and name what you re-examined** — \`No issues found — re-walked the reconnect state machine and the two changed exports' call sites; every gap I checked was already in the list\`. A bare "No issues found." is indistinguishable from an agent that did nothing, and it is treated as one: it ends nothing, and it earns your scope a relaunch.`,
If you find no new gap in your scope, say so **and name what you re-examined** — \`${REVERSE_AUDIT_EXAMPLE_RECEIPT}\`. A bare "No issues found." is indistinguishable from an agent that did nothing, and it is treated as one: it ends nothing, and it earns your scope a relaunch.`,
},
};

Expand Down
40 changes: 22 additions & 18 deletions packages/cli/src/commands/review/lib/deadline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,27 @@ export const RESERVE_ENV = 'QWEN_REVIEW_DEADLINE_RESERVE_SECONDS';
* verification of that round's findings, compose-review, anchor resolution
* and the submission itself.
*
* The measured round estimate ALSO contains one verification pass — a round
* is admitted only after the previous round's findings were verified and
* merged (SKILL.md Step 5), so an admission-to-admission span includes the
* verification between them — which means the gate holds back roughly one
* verification more than the terminal round strictly needs. That overlap is
* deliberate margin, not double-entry bookkeeping that slipped: round costs
* trend UP (each round re-reads the diff against a longer findings list, and
* repair relaunches land mid-loop), so the previous round's measurement
* under-predicts the next in exactly the runs that end near the boundary —
* and the two error directions are not symmetric. Over-reserving ends the
* loop at most one round early, disclosed as a budget stop; under-reserving
* is #8368 — killed mid-verification, holding every confirmed finding.
* Under the pipelined loop (SKILL.md Step 5), a round's verification
* launches WITH the next round's auditors instead of sitting between
* admissions — so the admission-to-admission span the gate measures
* contains no verification pass, and the terminal round's verification has
* exactly one cover: this reserve. That makes the reserve's sizing the
* whole margin, not a top-up on an overlap the measurement already
* carried, and the two error directions are still not symmetric: round
* costs trend UP (each round re-reads the diff against a longer findings
* list, and repair relaunches land mid-loop), so the previous round's
* measurement under-predicts the next in exactly the runs that end near
* the boundary. Over-reserving ends the loop at most one round early,
* disclosed as a budget stop; under-reserving is #8368 — killed
* mid-verification, holding every confirmed finding.
*
* This is only the fallback: the budget itself is
* chosen outside the CLI (a repository variable, a workflow input, a
* `/review --timeout=N` comment), so the review workflow passes a reserve
* scaled to the budget it resolved rather than trusting this constant to fit
* an arbitrary one. A local run has no deadline and no reserve at all.
* an arbitrary one. The workflow caps that scaled reserve at this same
* number (`.github/workflows/qwen-code-pr-review.yml`) — keep the two in
* sync. A local run has no deadline and no reserve at all.
*/
export const DEFAULT_RESERVE_SECONDS = 3600;

Expand Down Expand Up @@ -176,11 +179,12 @@ export function stampRound(

/**
* What the round about to be admitted is expected to cost, in seconds: the
* observed cost of the previous round (admission-to-admission — its agents,
* their verification, the orchestration between) when a stamp exists, else
* the conservative constant. The span deliberately overlaps the tail
* reserve by one verification pass — see `DEFAULT_RESERVE_SECONDS` for why
* that margin is kept rather than netted out. A stamp of the SAME round is
* observed cost of the previous round (admission-to-admission — its audit
* fan-out and the orchestration around it; under the pipelined loop a
* round's verification overlaps the NEXT round instead of sitting between
* admissions, so it is not in this measure, and the terminal round's
* verification is exactly what the deadline's reserve covers) when a stamp
* exists, else the conservative constant. A stamp of the SAME round is
* ignored — that is a rebuild, and measuring it would report a round as
* cheap because its prompts were built twice quickly.
*/
Expand Down
60 changes: 51 additions & 9 deletions packages/cli/src/commands/review/lib/prompt-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@
// agent's actual launch prompt. The two artifacts have different authors, and
// neither is the orchestrator.

import { mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
import {
mkdirSync,
readFileSync,
readdirSync,
statSync,
writeFileSync,
} from 'node:fs';
import { dirname, join, basename, resolve } from 'node:path';

/**
Expand Down Expand Up @@ -128,8 +134,24 @@ export function recordPrompt(
}
}

/** Every prompt this plan's builder emitted, keyed as it was recorded. */
export function readRecordedPrompts(planPath: string): Map<string, string> {
/**
* Every prompt this plan's builder emitted, keyed as it was recorded.
*
* `sinceMs` is the same fence every other reader of this directory applies —
* the plan's mtime. Nothing clears the record dir, and a run that dies
* mid-review leaves its records beside the retry's (the CI retry re-runs the
* review at the SAME plan path, under a freshly-captured plan): a record file
* older than the plan belongs to that dead attempt. A caller that reads
* records as OBLIGATIONS (coverage: "an agent was owed for this key") passes
* nothing — an obligation is not less owed for being stale, and dropping one
* would excuse the agent it demands. A caller that reads them as HISTORY
* (retirement) must pass the fence, or the dead attempt's records shadow the
* live ones.
*/
export function readRecordedPrompts(
planPath: string,
sinceMs?: number,
): Map<string, string> {
const out = new Map<string, string>();
const dir = promptRecordDir(planPath);
let names: string[];
Expand All @@ -150,7 +172,9 @@ export function readRecordedPrompts(planPath: string): Map<string, string> {
} catch {
continue; // Not a name this module wrote.
}
out.set(key, readFileSync(join(dir, name), 'utf8'));
const file = join(dir, name);
if (sinceMs !== undefined && statSync(file).mtimeMs < sinceMs) continue;
out.set(key, readFileSync(file, 'utf8'));
} catch {
/* raced with a cleanup */
}
Expand Down Expand Up @@ -186,6 +210,22 @@ export function readRecordedPrompts(planPath: string): Map<string, string> {
export function wasDeliveredVerbatim(
launchPrompt: string,
built: string,
): boolean {
return deliveredVerbatim(flattenPrompt(launchPrompt), built);
}

/**
* `wasDeliveredVerbatim` with the launch prompt already put through
* `flattenPrompt`. The pair exists for the one caller that pairs MANY records
* against MANY transcripts (the retirement scheduler): flattening is the
* expensive half of the check, and a caller that flattens each launch once
* pays it per transcript instead of per (record, transcript) pair — a few
* thousand full-prompt passes on the run the scheduler was built for, all
* before the round is admitted. Same contract, same failure modes.
*/
export function deliveredVerbatim(
flattenedLaunch: string,
built: string,
): boolean {
// A zero-byte record is not a prompt, and the loop below would be vacuously true
// for it — the check would pass every agent, and the roster would credit a role
Expand All @@ -194,25 +234,27 @@ export function wasDeliveredVerbatim(
// *built*), so an empty file is exactly what a partial write leaves behind. It is
// the one input that must fail closed.
if (built.trim().length === 0) return false;
const delivered = flatten(launchPrompt);
let at = 0;
for (const line of lines(built)) {
const i = delivered.indexOf(line, at);
const i = flattenedLaunch.indexOf(line, at);
if (i === -1) return false;
at = i + line.length;
}
return true;
}

/** Whitespace collapsed to single spaces: a re-wrap is not an edit. */
function flatten(s: string): string {
/**
* Whitespace collapsed to single spaces: a re-wrap is not an edit. What
* `deliveredVerbatim` expects its launch side to have been put through.
*/
export function flattenPrompt(s: string): string {
return s.replace(/\s+/g, ' ').trim();
}

/** The built prompt's lines, whitespace-normalized, blanks dropped. */
function lines(built: string): string[] {
return built
.split('\n')
.map((l) => flatten(l))
.map((l) => flattenPrompt(l))
.filter((l) => l.length > 0);
}
Loading
Loading