@boxxy/review-noslop 0.0.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,43 @@
1
- # @boxxy/review-noslop
1
+ <div align="center">
2
+ <h1>@boxxy/review-noslop</h1>
2
3
 
3
- This version is a placeholder. It holds no code. CI publishes the real versions.
4
+ <p>
5
+ <a href="https://www.npmjs.com/package/@boxxy/review-noslop"><img src="https://img.shields.io/npm/v/@boxxy/review-noslop" alt="npm version"></a>
6
+ <a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" alt="MIT license"></a>
7
+ </p>
8
+
9
+ <p>
10
+ <a href="../../docs/design/boxxy/README.md">Documentation</a>
11
+ </p>
12
+ </div>
13
+
14
+ boxxy's `ReviewGate` port on noslop: the command a review session runs, and the server's check and
15
+ post of its result.
16
+
17
+ ## Install
18
+
19
+ Install the package from [npm](https://www.npmjs.com/package/@boxxy/review-noslop) with Bun:
20
+
21
+ ```bash
22
+ bun add @boxxy/review-noslop
23
+ ```
24
+
25
+ The package runs on [Bun](https://bun.sh) 1.3 or newer.
26
+
27
+ ## Features
28
+
29
+ - `createNoslopReviewGate({ appEnv })` returns a `ReviewGate`. `appEnv` holds the noslop App's keys,
30
+ which only the server has.
31
+ - `buildCommand(round)` gives a command that writes `noslop-review.json` in the pull request's
32
+ checkout. A first round runs a full review. A later round runs `--fixes-only`, and runs a plain
33
+ review when that run judged no fix.
34
+ - `accept(round, result)` posts the result with `sendResult` from `@noslop/cli/post`, then reads the
35
+ findings still open with `check` from `@noslop/cli/check`.
36
+ - `accept` reads a result that it can't post as `not-covered` and posts nothing. That covers a
37
+ result for an old head and a fixes-only run that judged no fix.
38
+ - `accept` throws only when GitHub gives no answer, so the session sends the file again.
39
+
40
+ ## Documentation
41
+
42
+ [The review gate](../../docs/design/boxxy/review-and-merge.md#the-review-gate) describes each
43
+ reading and when the core sends a pull request back.
@@ -0,0 +1,3 @@
1
+ import type { GateCost } from '@boxxy/model';
2
+ import type { ReviewResult } from '@noslop/cli';
3
+ export declare function buildGateCost(review: ReviewResult): GateCost;
@@ -0,0 +1,12 @@
1
+ import type { CommitSha, GateReading, GateRound } from '@boxxy/model';
2
+ import type { FindingList, ReviewResult } from '@noslop/cli';
3
+ import type { PullRequestCheck } from '@noslop/cli/check';
4
+ export interface GateReadingInput {
5
+ readonly round: GateRound;
6
+ readonly head: CommitSha;
7
+ readonly review: ReviewResult;
8
+ readonly list: FindingList | null;
9
+ readonly listExitCode: number | null;
10
+ readonly outcome: PullRequestCheck;
11
+ }
12
+ export declare function buildGateReading(input: GateReadingInput): GateReading;
@@ -0,0 +1,3 @@
1
+ import type { GateRound } from '@boxxy/model';
2
+ export declare const reviewFileName = "noslop-review.json";
3
+ export declare function buildReviewCommand(round: GateRound): string;
@@ -0,0 +1,3 @@
1
+ import type { ReviewGate } from '@boxxy/model';
2
+ import type { NoslopReviewGateOptions } from './types.ts';
3
+ export declare function createNoslopReviewGate(options: NoslopReviewGateOptions): ReviewGate;
@@ -0,0 +1,3 @@
1
+ export { reviewFileName } from './build-review-command.ts';
2
+ export { createNoslopReviewGate } from './create-noslop-review-gate.ts';
3
+ export type { NoslopClient, NoslopReviewGateOptions } from './types.ts';
@@ -0,0 +1,14 @@
1
+ import type { CommitSha, GateCost, GateRound, JsonValue } from '@boxxy/model';
2
+ import type { FindingList, ReviewResult } from '@noslop/cli';
3
+ export type ParsedReviewBody = {
4
+ readonly kind: 'postable';
5
+ readonly head: CommitSha;
6
+ readonly review: ReviewResult;
7
+ readonly list: FindingList | null;
8
+ readonly listExitCode: number | null;
9
+ } | {
10
+ readonly kind: 'not-covered';
11
+ readonly head: CommitSha;
12
+ readonly cost: GateCost;
13
+ };
14
+ export declare function parseReviewBody(round: GateRound, body: JsonValue): ParsedReviewBody;
@@ -0,0 +1,22 @@
1
+ import type { ReviewResult } from '@noslop/cli';
2
+ type Finding = ReviewResult['findings'][number];
3
+ interface MockReviewResultInput {
4
+ readonly owner: string;
5
+ readonly repo: string;
6
+ readonly number: number;
7
+ readonly head?: string;
8
+ readonly findings?: readonly {
9
+ readonly id: string;
10
+ readonly severity: Finding['severity'];
11
+ readonly path: string;
12
+ }[];
13
+ readonly failedCalls?: number;
14
+ readonly fixesOnly?: {
15
+ readonly fixChecks: readonly {
16
+ readonly findingID: string;
17
+ readonly fixed: boolean;
18
+ }[];
19
+ };
20
+ }
21
+ export declare function buildMockReviewResult(input: MockReviewResultInput): ReviewResult;
22
+ export {};
@@ -0,0 +1,14 @@
1
+ import type { ReviewResult } from '@noslop/cli';
2
+ import type { PullRequestCheck } from '@noslop/cli/check';
3
+ import type { NoslopClient } from '../types.ts';
4
+ interface FakeNoslopClientInput {
5
+ readonly head: string;
6
+ readonly outcome: PullRequestCheck;
7
+ readonly down?: boolean;
8
+ }
9
+ export interface FakeNoslopClient extends NoslopClient {
10
+ readonly posted: ReviewResult[];
11
+ readonly checked: number;
12
+ }
13
+ export declare function createFakeNoslopClient(input: FakeNoslopClientInput): FakeNoslopClient;
14
+ export {};
@@ -0,0 +1,15 @@
1
+ export interface StubNoslopAnswers {
2
+ readonly full: string | null;
3
+ readonly fixesOnly: string | null;
4
+ readonly plain: string | null;
5
+ readonly findings: {
6
+ readonly stdout: string;
7
+ readonly exitCode: number;
8
+ };
9
+ readonly head: string;
10
+ }
11
+ export interface StubNoslop {
12
+ readonly binPath: string;
13
+ readCalls: () => Promise<string[]>;
14
+ }
15
+ export declare function createStubNoslop(root: string, answers: StubNoslopAnswers): Promise<StubNoslop>;
@@ -0,0 +1,10 @@
1
+ import type { check } from '@noslop/cli/check';
2
+ import type { sendResult } from '@noslop/cli/post';
3
+ export interface NoslopClient {
4
+ readonly sendResult: typeof sendResult;
5
+ readonly check: typeof check;
6
+ }
7
+ export interface NoslopReviewGateOptions {
8
+ readonly appEnv: Readonly<Record<string, string | undefined>>;
9
+ readonly client?: NoslopClient;
10
+ }
package/package.json CHANGED
@@ -1,13 +1,53 @@
1
1
  {
2
2
  "name": "@boxxy/review-noslop",
3
- "version": "0.0.1",
3
+ "version": "0.2.3",
4
+ "description": "boxxy's review gate on noslop: the review command, and the check and post of its result",
5
+ "homepage": "https://github.com/zgeoff/boxxy/tree/main/adapters/review-noslop#readme",
6
+ "bugs": "https://github.com/zgeoff/boxxy/issues",
4
7
  "license": "MIT",
5
8
  "repository": {
6
9
  "type": "git",
7
10
  "url": "git+https://github.com/zgeoff/boxxy.git",
8
11
  "directory": "adapters/review-noslop"
9
12
  },
13
+ "files": [
14
+ "src",
15
+ "!src/**/*.test.ts",
16
+ "dist"
17
+ ],
18
+ "type": "module",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./src/index.ts"
23
+ }
24
+ },
10
25
  "publishConfig": {
11
26
  "access": "public"
27
+ },
28
+ "scripts": {
29
+ "build": "rm -rf dist && tsrs -p tsconfig.build.json",
30
+ "typecheck": "tsrs --noEmit"
31
+ },
32
+ "dependencies": {
33
+ "@boxxy/model": "0.2.3",
34
+ "@noslop/cli": "0.4.29",
35
+ "ts-pattern": "5.9.2",
36
+ "zod": "4.6.7"
37
+ },
38
+ "devDependencies": {
39
+ "@faker-js/faker": "10.6.0"
40
+ },
41
+ "peerDependencies": {
42
+ "@faker-js/faker": "10.6.0"
43
+ },
44
+ "peerDependenciesMeta": {
45
+ "@faker-js/faker": {
46
+ "optional": true
47
+ }
48
+ },
49
+ "engines": {
50
+ "bun": ">=1.3",
51
+ "node": ">=24"
12
52
  }
13
53
  }
@@ -0,0 +1,16 @@
1
+ import type { GateCost } from '@boxxy/model';
2
+ import type { ReviewResult } from '@noslop/cli';
3
+
4
+ // A call with no model ran on the adapter's default model, so the adapter's name stands in.
5
+ export function buildGateCost(review: ReviewResult): GateCost {
6
+ return {
7
+ calls: review.calls.map((call) => ({
8
+ model: call.model ?? call.adapter,
9
+ input: call.tokens.input,
10
+ cachedInput: call.tokens.cachedInput,
11
+ cacheWrite: call.tokens.cacheWrite,
12
+ output: call.tokens.output,
13
+ durationMs: call.durationMs,
14
+ })),
15
+ };
16
+ }
@@ -0,0 +1,87 @@
1
+ import type { CommitSha, GateReading, GateRound, GateRun, SeverityCounts } from '@boxxy/model';
2
+ import type { FindingList, ReviewResult } from '@noslop/cli';
3
+ import type { PullRequestCheck } from '@noslop/cli/check';
4
+ import { buildGateCost } from './build-gate-cost.ts';
5
+
6
+ export interface GateReadingInput {
7
+ readonly round: GateRound;
8
+
9
+ // the result `sendResult` matched to the pull request's head, and posted
10
+ readonly head: CommitSha;
11
+ readonly review: ReviewResult;
12
+
13
+ // the `noslop findings` output from before the post, and its exit code
14
+ readonly list: FindingList | null;
15
+ readonly listExitCode: number | null;
16
+
17
+ // `check` after the post: the findings still open at the head
18
+ readonly outcome: PullRequestCheck;
19
+ }
20
+
21
+ interface KnownFinding {
22
+ readonly severity: 'P1' | 'P2' | 'P3';
23
+ readonly path: string;
24
+ }
25
+
26
+ const closedWithoutFixStates = new Set(['not-a-defect', 'deferred', 'accepted-risk']);
27
+
28
+ // The noslop check run is never read: it passes at a head that no review read, and after a failed
29
+ // call. Only `check`'s own answer, after the post, counts.
30
+ export function buildGateReading(input: GateReadingInput): GateReading {
31
+ const listed = input.list?.findings ?? [];
32
+
33
+ const known = new Map<string, KnownFinding>([
34
+ ...input.review.findings.map(
35
+ (finding) => [finding.id, { severity: finding.severity, path: finding.anchor.path }] as const,
36
+ ),
37
+ ...listed.map(
38
+ (finding) => [finding.id, { severity: finding.severity, path: finding.path }] as const,
39
+ ),
40
+ ]);
41
+
42
+ const openIDs = new Set(input.outcome.open.map((finding) => finding.id));
43
+
44
+ const open = [...openIDs].map((id) => known.get(id) ?? null);
45
+
46
+ const run: GateRun = {
47
+ head: input.head,
48
+ open: buildSeverityCounts(open),
49
+ closedWithoutFix: listed.filter(
50
+ (finding) => closedWithoutFixStates.has(finding.state) && !openIDs.has(finding.id),
51
+ ).length,
52
+ findingPaths: [
53
+ ...new Set(open.flatMap((finding) => (finding === null ? [] : [finding.path]))),
54
+ ].toSorted(),
55
+ cost: buildGateCost(input.review),
56
+ ref: input.review.reviewID,
57
+ answerHint: `Run \`noslop findings --pr ${input.round.pullRequest.number} --repo ${input.round.pullRequest.repo} --json\`, and reply to each open finding with a disposition: fixed, not-a-defect, deferred or accepted-risk.`,
58
+ };
59
+
60
+ if (input.listExitCode !== 0 || input.list === null) {
61
+ return { status: 'blocked', why: 'unreadable', ...run };
62
+ }
63
+
64
+ if (input.review.failedCalls.length > 0) {
65
+ return { status: 'blocked', why: 'failed-call', ...run };
66
+ }
67
+
68
+ if (open.length > 0) {
69
+ return { status: 'blocked', why: 'open-findings', ...run };
70
+ }
71
+
72
+ // a failure with no open finding: a finding's marker became unreadable
73
+ if (input.outcome.conclusion !== 'success') {
74
+ return { status: 'blocked', why: 'unreadable', ...run };
75
+ }
76
+
77
+ return { status: 'passed', ...run };
78
+ }
79
+
80
+ // An open finding that neither the result nor the list names counts as high, so it never lowers
81
+ // the bar.
82
+ function buildSeverityCounts(open: readonly (KnownFinding | null)[]): SeverityCounts {
83
+ const count = (severity: KnownFinding['severity']) =>
84
+ open.filter((finding) => (finding?.severity ?? 'P1') === severity).length;
85
+
86
+ return { high: count('P1'), medium: count('P2'), low: count('P3') };
87
+ }
@@ -0,0 +1,51 @@
1
+ import type { GateRound } from '@boxxy/model';
2
+ import { match } from 'ts-pattern';
3
+
4
+ // The file the command writes, which the session sends unchanged as its close-out.
5
+ export const reviewFileName = 'noslop-review.json';
6
+
7
+ // The script runs under Bun, which the agent host has for noslop, so the command needs no JSON
8
+ // tool of its own. `--report json` replaces the config's reporters, so the agent host posts
9
+ // nothing; `--adapter claude` uses the model sign-in the host's broker gives.
10
+ const script = `
11
+ const args = __ARGS__;
12
+ const run = (argv) => {
13
+ const child = Bun.spawnSync(argv, { stdout: 'pipe', stderr: 'inherit' });
14
+ return { exitCode: child.exitCode, text: child.stdout.toString() };
15
+ };
16
+ const parse = (text) => {
17
+ try { return JSON.parse(text); } catch { return null; }
18
+ };
19
+ const target = ['--pr', String(args.pr), '--repo', args.repo];
20
+ const options = [...target, '--adapter', 'claude', '--report', 'json', '--json'];
21
+ let review = parse(run(['noslop', 'review', args.mode, ...options]).text);
22
+ if (args.mode === '--fixes-only' && (review?.fixChecks ?? []).length === 0) {
23
+ review = parse(run(['noslop', 'review', ...options]).text);
24
+ }
25
+ const listed = run(['noslop', 'findings', ...target, '--json']);
26
+ const head = run(['git', 'rev-parse', 'HEAD']).text.trim();
27
+ await Bun.write(args.file, JSON.stringify({ head, review, findings: { exitCode: listed.exitCode, list: parse(listed.text) } }));
28
+ `;
29
+
30
+ // A first review reads the whole pull request. A later one checks the fixes, and falls back to a
31
+ // plain review when no `fixed` answer got a verdict, because that run read nothing at its head.
32
+ export function buildReviewCommand(round: GateRound): string {
33
+ const mode = match(round.cause)
34
+ .with('first', () => '--full')
35
+ .with('rework', 'recheck', () => '--fixes-only')
36
+ .exhaustive();
37
+
38
+ const args = JSON.stringify({
39
+ pr: round.pullRequest.number,
40
+ repo: round.pullRequest.repo,
41
+ mode,
42
+ file: reviewFileName,
43
+ });
44
+
45
+ const body = script
46
+ .replace('__ARGS__', () => args)
47
+ .trim()
48
+ .replaceAll("'", String.raw`'\''`);
49
+
50
+ return `bun -e '${body}'`;
51
+ }
@@ -0,0 +1,60 @@
1
+ import type { GateReading, GateRound, JsonValue, ReviewGate } from '@boxxy/model';
2
+ import { check } from '@noslop/cli/check';
3
+ import { sendResult } from '@noslop/cli/post';
4
+ import { buildGateCost } from './build-gate-cost.ts';
5
+ import { buildGateReading } from './build-gate-reading.ts';
6
+ import { buildReviewCommand } from './build-review-command.ts';
7
+ import { parseReviewBody } from './parse-review-body.ts';
8
+ import type { NoslopReviewGateOptions } from './types.ts';
9
+
10
+ // The server's side of a review: it checks and posts the result a review session sent, with the
11
+ // noslop App's keys, which no agent host gets.
12
+ export function createNoslopReviewGate(options: NoslopReviewGateOptions): ReviewGate {
13
+ const client = options.client ?? { sendResult, check };
14
+
15
+ return {
16
+ buildCommand: buildReviewCommand,
17
+ accept: async (round: GateRound, result: JsonValue): Promise<GateReading> => {
18
+ const parsed = parseReviewBody(round, result);
19
+
20
+ if (parsed.kind === 'not-covered') {
21
+ return { status: 'not-covered', head: parsed.head, cost: parsed.cost };
22
+ }
23
+
24
+ const posted = await client
25
+ .sendResult(parsed.review, { env: options.appEnv, verify: true })
26
+ .then(() => 'posted' as const)
27
+ .catch((error: unknown) => {
28
+ if (isRefusal(error)) {
29
+ return 'refused' as const;
30
+ }
31
+
32
+ throw error;
33
+ });
34
+
35
+ if (posted === 'refused') {
36
+ return { status: 'not-covered', head: parsed.head, cost: buildGateCost(parsed.review) };
37
+ }
38
+
39
+ const [owner = '', repo = ''] = round.pullRequest.repo.split('/');
40
+
41
+ const outcome = await client.check(
42
+ { owner, repo, number: round.pullRequest.number },
43
+ { env: options.appEnv, readOnly: true },
44
+ );
45
+
46
+ return buildGateReading({ round, ...parsed, outcome });
47
+ },
48
+ };
49
+ }
50
+
51
+ // noslop wraps every failure of its check in one error, a network failure included. Only a cause
52
+ // that says the result does not verify, such as an old head, is a refusal; any other failure
53
+ // throws, so the session sends again.
54
+ function isRefusal(error: unknown): boolean {
55
+ return (
56
+ error instanceof Error &&
57
+ error.cause instanceof Error &&
58
+ error.cause.message.startsWith('the result does not verify')
59
+ );
60
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { reviewFileName } from './build-review-command.ts';
2
+ export { createNoslopReviewGate } from './create-noslop-review-gate.ts';
3
+ export type { NoslopClient, NoslopReviewGateOptions } from './types.ts';
@@ -0,0 +1,81 @@
1
+ import type { CommitSha, GateCost, GateRound, JsonValue } from '@boxxy/model';
2
+ import { brandSchemas } from '@boxxy/model';
3
+ import type { FindingList, ReviewResult } from '@noslop/cli';
4
+ import { findingListSchema, reviewResultSchema } from '@noslop/cli';
5
+ import { z } from 'zod';
6
+ import { buildGateCost } from './build-gate-cost.ts';
7
+
8
+ // The file the review command writes: the git head of the checkout, the review's JSON, and the
9
+ // output of `noslop findings` with its exit code. A part the command could not read is null.
10
+ const reviewBodySchema = z.object({
11
+ head: z.string().nullable(),
12
+ review: z.unknown(),
13
+ findings: z.object({ exitCode: z.number().int().nullable(), list: z.unknown() }),
14
+ });
15
+
16
+ export type ParsedReviewBody =
17
+ | {
18
+ readonly kind: 'postable';
19
+ readonly head: CommitSha;
20
+ readonly review: ReviewResult;
21
+ readonly list: FindingList | null;
22
+ readonly listExitCode: number | null;
23
+ }
24
+ | { readonly kind: 'not-covered'; readonly head: CommitSha; readonly cost: GateCost };
25
+
26
+ const noHead = brandSchemas.commitSha.parse('0'.repeat(40));
27
+
28
+ // Picks what `accept` can post. A result it can't post reads not-covered: one that does not
29
+ // parse, one for another pull request, and a `--fixes-only` run that judged no fix, which read
30
+ // nothing at its head though its `head` names that head.
31
+ export function parseReviewBody(round: GateRound, body: JsonValue): ParsedReviewBody {
32
+ const envelope = reviewBodySchema.safeParse(body);
33
+
34
+ if (!envelope.success) {
35
+ return { kind: 'not-covered', head: noHead, cost: { calls: [] } };
36
+ }
37
+
38
+ const review = reviewResultSchema.safeParse(envelope.data.review);
39
+
40
+ if (!review.success) {
41
+ const checkoutHead = brandSchemas.commitSha.safeParse(envelope.data.head);
42
+
43
+ return {
44
+ kind: 'not-covered',
45
+ head: checkoutHead.success ? checkoutHead.data : noHead,
46
+ cost: { calls: [] },
47
+ };
48
+ }
49
+
50
+ const head = brandSchemas.commitSha.safeParse(review.data.head);
51
+
52
+ if (!head.success) {
53
+ return { kind: 'not-covered', head: noHead, cost: buildGateCost(review.data) };
54
+ }
55
+
56
+ if (!isRoundPullRequest(round, review.data) || isFixesOnlyWithNoVerdict(review.data)) {
57
+ return { kind: 'not-covered', head: head.data, cost: buildGateCost(review.data) };
58
+ }
59
+
60
+ const list = findingListSchema.safeParse(envelope.data.findings.list);
61
+
62
+ return {
63
+ kind: 'postable',
64
+ head: head.data,
65
+ review: review.data,
66
+ list: list.success ? list.data : null,
67
+ listExitCode: envelope.data.findings.exitCode,
68
+ };
69
+ }
70
+
71
+ function isRoundPullRequest(round: GateRound, review: ReviewResult): boolean {
72
+ return (
73
+ review.pullRequest !== null &&
74
+ `${review.pullRequest.owner}/${review.pullRequest.repo}` === round.pullRequest.repo &&
75
+ review.pullRequest.number === round.pullRequest.number
76
+ );
77
+ }
78
+
79
+ function isFixesOnlyWithNoVerdict(review: ReviewResult): boolean {
80
+ return review.fixesOnly === true && (review.fixChecks ?? []).length === 0;
81
+ }
@@ -0,0 +1,95 @@
1
+ import { faker } from '@faker-js/faker';
2
+ import type { ReviewResult } from '@noslop/cli';
3
+
4
+ type Finding = ReviewResult['findings'][number];
5
+
6
+ interface MockReviewResultInput {
7
+ readonly owner: string;
8
+ readonly repo: string;
9
+ readonly number: number;
10
+ readonly head?: string;
11
+ readonly findings?: readonly {
12
+ readonly id: string;
13
+ readonly severity: Finding['severity'];
14
+ readonly path: string;
15
+ }[];
16
+ readonly failedCalls?: number;
17
+ readonly fixesOnly?: {
18
+ readonly fixChecks: readonly { readonly findingID: string; readonly fixed: boolean }[];
19
+ };
20
+ }
21
+
22
+ // noslop sets each finding's severity from its criterion
23
+ const criterionBySeverity = {
24
+ P1: 'outage',
25
+ P2: 'untested-change',
26
+ P3: 'naming',
27
+ } as const satisfies Record<Finding['severity'], Finding['criterion']>;
28
+
29
+ // A noslop review result for one pull request, at a random head, with one model call. A finding
30
+ // ID is 16 hex characters.
31
+ export function buildMockReviewResult(input: MockReviewResultInput): ReviewResult {
32
+ const callID = `call-${faker.string.alphanumeric(6)}`;
33
+
34
+ return {
35
+ version: 7,
36
+ reviewID: `review-${faker.string.alphanumeric(8)}`,
37
+ repo: `${input.owner}/${input.repo}`,
38
+ pullRequest: { owner: input.owner, repo: input.repo, number: input.number },
39
+ base: faker.git.commitSha({ length: 40 }).toLowerCase(),
40
+ head: input.head ?? faker.git.commitSha({ length: 40 }).toLowerCase(),
41
+ title: faker.lorem.sentence(),
42
+ depth: 'standard',
43
+ special: null,
44
+ riskClasses: [],
45
+ lenses: ['correctness'],
46
+ calls: [
47
+ {
48
+ id: callID,
49
+ lenses: ['correctness'],
50
+ depth: 'standard',
51
+ adapter: 'claude',
52
+ model: 'claude-test-model',
53
+ effort: 'medium',
54
+ status: 'ok',
55
+ attempts: 1,
56
+ tokens: { input: 1200, cachedInput: 300, cacheWrite: 40, output: 210 },
57
+ durationMs: 4100,
58
+ },
59
+ ],
60
+ findings: (input.findings ?? []).map((finding) => ({
61
+ id: finding.id,
62
+ lens: 'correctness',
63
+ callID,
64
+
65
+ // noslop caps a finding title at 80 characters
66
+ title: faker.lorem.words(3),
67
+ body: faker.lorem.paragraph(),
68
+ anchor: { path: finding.path, startLine: 3, endLine: 5 },
69
+ severity: finding.severity,
70
+ criterion: criterionBySeverity[finding.severity],
71
+ verdict: 'CONFIRMED',
72
+ writtenByChange: true,
73
+ })),
74
+ dropped: [],
75
+ failedCalls: Array.from({ length: input.failedCalls ?? 0 }, (_, index) => ({
76
+ id: `failed-${index}`,
77
+ lenses: ['tests'],
78
+ reason: 'the model gave no answer',
79
+ })),
80
+ notReviewed: [],
81
+ notReviewedByLens: [],
82
+ configHash: faker.string.hexadecimal({ length: 16, casing: 'lower', prefix: '' }),
83
+ ...(input.fixesOnly === undefined
84
+ ? {}
85
+ : {
86
+ fixesOnly: true,
87
+ fixChecks: input.fixesOnly.fixChecks.map((fixCheck) => ({
88
+ ...fixCheck,
89
+ reason: faker.lorem.sentence(),
90
+ })),
91
+ }),
92
+ startedAt: '2026-10-01T10:00:00Z',
93
+ endedAt: '2026-10-01T10:04:00Z',
94
+ };
95
+ }
@@ -0,0 +1,68 @@
1
+ import type { ReviewResult } from '@noslop/cli';
2
+ import { reviewResultSchema } from '@noslop/cli';
3
+ import type { PullRequestCheck } from '@noslop/cli/check';
4
+ import type { NoslopClient } from '../types.ts';
5
+
6
+ interface FakeNoslopClientInput {
7
+ // the pull request's head on the forge
8
+ readonly head: string;
9
+
10
+ // what `check` answers after a post
11
+ readonly outcome: PullRequestCheck;
12
+
13
+ // true: GitHub gives no answer, so every call fails as a network failure does
14
+ readonly down?: boolean;
15
+ }
16
+
17
+ export interface FakeNoslopClient extends NoslopClient {
18
+ readonly posted: ReviewResult[];
19
+ readonly checked: number;
20
+ }
21
+
22
+ // noslop's two calls, against one pull request at `head`. A result for another head is refused
23
+ // with the error noslop throws.
24
+ export function createFakeNoslopClient(input: FakeNoslopClientInput): FakeNoslopClient {
25
+ const posted: ReviewResult[] = [];
26
+ let checked = 0;
27
+
28
+ return {
29
+ posted,
30
+ get checked() {
31
+ return checked;
32
+ },
33
+ sendResult: (result) => {
34
+ const review = reviewResultSchema.parse(result);
35
+
36
+ if (input.down === true) {
37
+ return Promise.reject(
38
+ new Error('noslop posts only a verified result, so it refuses this one: fetch failed', {
39
+ cause: new Error('fetch failed'),
40
+ }),
41
+ );
42
+ }
43
+
44
+ if (review.head !== input.head) {
45
+ const reason = `the result does not verify: it reviews ${review.head.slice(0, 12)}, but the head of the pull request is ${input.head.slice(0, 12)}`;
46
+
47
+ return Promise.reject(
48
+ new Error(`noslop posts only a verified result, so it refuses this one: ${reason}`, {
49
+ cause: new Error(reason),
50
+ }),
51
+ );
52
+ }
53
+
54
+ posted.push(review);
55
+
56
+ return Promise.resolve(review);
57
+ },
58
+ check: () => {
59
+ if (input.down === true) {
60
+ return Promise.reject(new Error('fetch failed'));
61
+ }
62
+
63
+ checked += 1;
64
+
65
+ return Promise.resolve(input.outcome);
66
+ },
67
+ };
68
+ }
@@ -0,0 +1,71 @@
1
+ import { chmod, mkdir, readFile, writeFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+
4
+ export interface StubNoslopAnswers {
5
+ // stdout of `noslop review --full`, `--fixes-only` and the plain `noslop review`; null prints
6
+ // nothing, as a run that failed before its JSON
7
+ readonly full: string | null;
8
+ readonly fixesOnly: string | null;
9
+ readonly plain: string | null;
10
+ readonly findings: { readonly stdout: string; readonly exitCode: number };
11
+
12
+ // what `git rev-parse HEAD` prints
13
+ readonly head: string;
14
+ }
15
+
16
+ export interface StubNoslop {
17
+ // the folder to put first on PATH
18
+ readonly binPath: string;
19
+
20
+ // each noslop call's arguments, in order
21
+ readCalls: () => Promise<string[]>;
22
+ }
23
+
24
+ // Stand-ins for the `noslop` and `git` commands that a review command runs, in `root`.
25
+ export async function createStubNoslop(
26
+ root: string,
27
+ answers: StubNoslopAnswers,
28
+ ): Promise<StubNoslop> {
29
+ const binPath = join(root, 'bin');
30
+ const callsPath = join(root, 'noslop-calls.log');
31
+
32
+ await mkdir(binPath, { recursive: true });
33
+ await writeFile(callsPath, '');
34
+
35
+ const answerPath = async (name: string, text: string | null) => {
36
+ const path = join(root, `${name}.out`);
37
+
38
+ await writeFile(path, text ?? '');
39
+
40
+ return path;
41
+ };
42
+
43
+ const noslop = [
44
+ '#!/bin/sh',
45
+ `printf '%s\\n' "$*" >> '${callsPath}'`,
46
+ 'case "$1 $2" in',
47
+ ` "review --full") cat '${await answerPath('full', answers.full)}' ;;`,
48
+ ` "review --fixes-only") cat '${await answerPath('fixes-only', answers.fixesOnly)}' ;;`,
49
+ ` "review --pr") cat '${await answerPath('plain', answers.plain)}' ;;`,
50
+ ` "findings --pr") cat '${await answerPath('findings', answers.findings.stdout)}'; exit ${answers.findings.exitCode} ;;`,
51
+ 'esac',
52
+ '',
53
+ ].join('\n');
54
+
55
+ await writeCommand(join(binPath, 'noslop'), noslop);
56
+ await writeCommand(join(binPath, 'git'), `#!/bin/sh\necho '${answers.head}'\n`);
57
+
58
+ return {
59
+ binPath,
60
+ readCalls: async () => {
61
+ const text = await readFile(callsPath, 'utf8');
62
+
63
+ return text.split('\n').filter((line) => line !== '');
64
+ },
65
+ };
66
+ }
67
+
68
+ async function writeCommand(path: string, text: string): Promise<void> {
69
+ await writeFile(path, text);
70
+ await chmod(path, 0o755);
71
+ }
package/src/types.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { check } from '@noslop/cli/check';
2
+ import type { sendResult } from '@noslop/cli/post';
3
+
4
+ // The two noslop calls the gate makes. The server passes the real ones; a test passes its own.
5
+ export interface NoslopClient {
6
+ readonly sendResult: typeof sendResult;
7
+ readonly check: typeof check;
8
+ }
9
+
10
+ export interface NoslopReviewGateOptions {
11
+ // the noslop App's keys: `NOSLOP_GITHUB_APP_ID`, `NOSLOP_GITHUB_APP_PRIVATE_KEY` and
12
+ // `NOSLOP_GITHUB_APP_INSTALLATION_ID`
13
+ readonly appEnv: Readonly<Record<string, string | undefined>>;
14
+ readonly client?: NoslopClient;
15
+ }