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
12 changes: 10 additions & 2 deletions packages/cli/src/commands/review/lib/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,19 @@ export function reviewWriteAuthorization(req: WriteAuthorizationRequest): {
} catch {
// No args file means no arguments — which means no `--comment`. Fail
// closed: a missing authorisation record is not an absent objection.
// The wording must not send a setting-driven operator to type a flag
// they never needed: with `review.comment` on, the real blocker is that
// no recorded invocation names a pull request to bind the write to, and
// a plain re-run of the review fixes that — typing `--comment` does not.
return {
ok: false,
why:
`no review arguments were recorded at ${path}, so this run cannot ` +
'show that `--comment` was requested',
req.defaultComment === true
? `no review arguments were recorded at ${path}, so no recorded ` +
'invocation names a pull request to bind this write to — re-run ' +
'the review naming the pull request'
Comment thread
wenshao marked this conversation as resolved.
Resolved
: `no review arguments were recorded at ${path}, so this run ` +
'cannot show that `--comment` was requested',
};
}

Expand Down
18 changes: 18 additions & 0 deletions packages/cli/src/commands/review/parse-args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,20 @@ describe('parseReviewArgs — settings-provided defaults', () => {
expect(got.warnings.some((w) => w.includes('review.comment'))).toBe(true);
});

it('a flag-forced high names the flag in the forcing warning, not the setting', () => {
// The flag branch of the same ternary: with no setting enabled, the
// warning must not send the operator hunting a setting that is off.
const got = parseReviewArgs('6711 --comment --effort low');
expect(got.effort).toBe('high');
expect(got.effortSource).toBe('forced-by-comment');
expect(got.warnings).toContain(
'`--comment` requires a verified review; running at high effort.',
);
expect(
got.warnings.some((w) => w.includes('`review.comment` is enabled')),
).toBe(false);
});

it('the standing comment setting stays inert on a local target', () => {
const got = parseReviewArgs('', { comment: true });
expect(got.comment.effective).toBe(false);
Expand Down Expand Up @@ -569,6 +583,10 @@ describe('parseReviewArgs — repeated --effort warnings state what is actually
const invalidWarning = got.warnings.find((w) => w.includes('"typo"'));
expect(invalidWarning).toContain('forces high effort');
expect(invalidWarning).not.toContain('default');
// The flag drove the forcing — the resolution must name the flag, not a
// setting that is off (both branches contain 'forces high effort').
expect(invalidWarning).toContain('`--comment` forces high effort');
expect(invalidWarning).not.toContain('review.comment');
});

it('with no valid occurrence anywhere the warning still says the default applies', () => {
Expand Down
57 changes: 43 additions & 14 deletions packages/cli/src/commands/review/publish-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ vi.mock('../../utils/stdioHelpers.js', () => ({

// The handler resolves `review.comment` through `operatorReviewSettings` —
// pin the view it reads so the wiring leg below does not depend on the
// running developer's settings.json. The refusal assertions call
// runPublishAssets directly and never touch this mock.
// running developer's settings.json. The direct refusal assertions call
// runPublishAssets and never touch this mock; the handler-path tests below
// do.
const reviewSettingsMock = vi.hoisted(() =>
vi.fn((): Record<string, unknown> => ({})),
);
Expand All @@ -61,9 +62,10 @@ vi.mock('../../config/settings.js', async (importOriginal) => {
// The production call carries `{ skipWorkspaceSettings: true }` — the
// authorisation default resolves from operator scopes only. A caller that
// forgets the flag reads the workspace-polluted view instead; the guards
// that redden are submit.test.ts's handler-level refusal test and
// review-settings.test.ts's direct assertion. This file's own refusals
// bypass the handler, so only the wiring leg below exercises the mock.
// that redden are submit.test.ts's handler-level refusal test,
// review-settings.test.ts's direct assertion, and this file's
// handler-level refusal test. The direct runPublishAssets refusals
// bypass the handler.
loadSettings: vi.fn((...callArgs: unknown[]) => {
const opts = callArgs[1] as
| { skipWorkspaceSettings?: boolean }
Expand Down Expand Up @@ -164,6 +166,25 @@ describe('publish-assets', () => {
} as never);
}

// The handler-path counterpart of run(): every handler-level leg calls
// this, so the yargs-facing argument shape lives in exactly one place and
// a one-leg-only edit (a renamed key silenced by the `as never` cast) is
// structurally impossible.
async function runHandler(
overrides: Record<string, unknown> = {},
): Promise<void> {
await publishAssetsCommand.handler?.({
_: [],
$0: 'qwen',
pr: 8346,
files: [pngFile('a.png')],
out: join(dir, 'manifest.json'),
'user-authorized': false,
'skill-args': argsFile,
...overrides,
} as never);
}

it('refuses without a designated repo — exit 3, nothing written', () => {
delete process.env['QWEN_REVIEW_ASSETS_REPO'];
run({ files: [pngFile('a.png')] });
Expand Down Expand Up @@ -217,19 +238,27 @@ describe('publish-assets', () => {
writeFileSync(argsFile, '8346\n'); // no --comment
reviewSettingsMock.mockReturnValue({ comment: true });
happyGh();
await publishAssetsCommand.handler?.({
_: [],
$0: 'qwen',
pr: 8346,
files: [pngFile('wired.png')],
out: join(dir, 'manifest.json'),
'user-authorized': false,
'skill-args': argsFile,
} as never);
await runHandler({ files: [pngFile('wired.png')] });
expect(process.exitCode).toBeUndefined();
expect(ghWithInputMock).toHaveBeenCalled();
});

it('the handler refuses when neither flag nor setting authorises — the polluted view must not decide', async () => {
Comment thread
wenshao marked this conversation as resolved.
Resolved
// The refusal counterpart of the wiring leg above: setting off, no
// `--comment` in the recorded arguments. If the handler's loadSettings
// call drops `skipWorkspaceSettings`, the workspace-polluted mock view
// answers comment:true and this refusal becomes a publish — the exact
// regression review-settings.ts documents (a repository-controlled
// .qwen/settings.json deciding to publish for every reviewer).
writeFileSync(argsFile, '8346\n'); // no --comment
reviewSettingsMock.mockReturnValue({}); // setting off
happyGh();
await runHandler({ files: [pngFile('refused.png')] });
expect(process.exitCode).toBe(3);
expect(ghWithInputMock).not.toHaveBeenCalled();
expect(ghMock).not.toHaveBeenCalled();
});

it('publishes, writes a manifest with commit-pinned URLs', () => {
happyGh();
const f = pngFile('evidence.png');
Expand Down
53 changes: 53 additions & 0 deletions packages/cli/src/commands/review/submit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,47 @@ describe('authorization — URL-shaped host and repo binding at the submit call
'`--comment` was not in the review arguments',
);
});

it('a missing args file names the missing invocation, not a missing flag, when the setting authorises', () => {
// With `review.comment` on, telling the operator to re-run with
// `--comment` misdirects: the blocker is that no recorded invocation
// names a PR, and a plain re-run fixes it. Flag-driven operators keep
// the flag wording — for them the flag IS the missing piece.
const missing = join(dir, 'no-such-args.txt');
const base = {
userAuthorized: false,
skillArgs: missing,
pr: 123,
repo: 'o/r',
};

const bySetting = reviewWriteAuthorization({
...base,
defaultComment: true,
});
expect(bySetting.ok).toBe(false);
expect(bySetting.why).toContain(
'no recorded invocation names a pull request',
);
expect(bySetting.why).not.toContain('`--comment`');

const byFlag = reviewWriteAuthorization(base);
expect(byFlag.ok).toBe(false);
expect(byFlag.why).toContain('cannot show that `--comment` was requested');
Comment thread
wenshao marked this conversation as resolved.
Resolved

// Both production callers pass a strict boolean (destructured default /
// the resolved setting), so pin the flag branch with the explicit false
// they actually send — a presence-check mutation of the ternary must not
// survive.
const byFlagExplicit = reviewWriteAuthorization({
...base,
defaultComment: false,
});
expect(byFlagExplicit.ok).toBe(false);
expect(byFlagExplicit.why).toContain(
'cannot show that `--comment` was requested',
);
});
});

describe('the posting gate', () => {
Expand Down Expand Up @@ -359,6 +400,7 @@ describe('the posting gate', () => {
);
expect(advice()).not.toContain('Re-run with `--comment`');
expect(advice()).toContain('invoked naming it');
expect(advice()).toContain('Nothing recorded authorises binding');
writeStderrSpy.mockClear();

// ...or a different PR than this submission targets.
Expand All @@ -369,6 +411,17 @@ describe('the posting gate', () => {
);
expect(advice()).not.toContain('Re-run with `--comment`');
expect(advice()).toContain('invoked naming it');
writeStderrSpy.mockClear();

// Nothing recorded at all, with the setting authorising: the refusal
// names the missing invocation, and the advice preamble must not
// contradict it by presupposing recorded arguments exist.
runSubmit(args({ skillArgs: join(dir, 'advice-missing.txt') }), 'unknown', {
defaultComment: true,
});
expect(advice()).toContain('no recorded invocation names a pull request');
expect(advice()).toContain('Nothing recorded authorises binding');
expect(advice()).not.toContain('The recorded arguments');
expect(ghMock).not.toHaveBeenCalled();
expect(process.exitCode).toBe(3);
});
Expand Down
18 changes: 11 additions & 7 deletions packages/cli/src/commands/review/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,18 +432,22 @@ export function runSubmit(
// report, and the user can ask for them to be posted.
// The advice must match the refusal class, or it misdirects the retry:
// the gate refuses either because comment was never requested (its `why`
// carries `` `--comment` was ``) or because the recorded arguments do not
// bind this target. `--comment` cannot fix the second class — the flag
// stands in for nothing a target binding needs, and the `review.comment`
// setting already stood in for the flag on exactly those refusals — so
// advising it there buys the futile retry loop authorization.ts's refusal
// wording exists to prevent.
// carries `` `--comment` was ``) or because nothing recorded authorises
// this target — a binding miss, or no recorded arguments at all. The
// second arm's preamble stays neutral ("Nothing recorded…") because a
// setting-driven missing-args refusal lands here too, and "The recorded
// arguments do not bind" would contradict its `why` ("no review
// arguments were recorded"). `--comment` cannot fix the second class —
// the flag stands in for nothing a target binding needs, and the
// `review.comment` setting already stood in for the flag on exactly
// those refusals — so advising it there buys the futile retry loop
// authorization.ts's refusal wording exists to prevent.
const advice = auth.why.includes('`--comment` was')
? `This is the correct outcome of a review the user did not ask to ` +
`publish — report the findings in the terminal and stop. Re-run with ` +
`\`--comment\`, or pass --user-authorized only after the user has ` +
`asked, in a message they typed, for this review to be published.`
: `The recorded arguments do not bind this target — report the ` +
: `Nothing recorded authorises binding this target — report the ` +
`findings in the terminal and stop. Posting to this pull request ` +
`needs a review invoked naming it, or --user-authorized after the ` +
`user has asked, in a message they typed, for this review to be ` +
Expand Down
Loading