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
21 changes: 15 additions & 6 deletions apps/server/src/fork/reviewRules/findings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const FINDINGS_NONE_RE = re(
`^(?:findings|issues|problems)${S}*:${S}*(?:none|nothing|no|0|n/?a)${S}*\\.?${S}*$`,
);
const NOT_SAFE_RE = re(`${B}not${S}+safe${B}`);
/** readFindings(unmarked): `not safe to merge` on any line (review_judge.NOT_SAFE_TO_MERGE_RE). */
const NOT_SAFE_TO_MERGE_RE = re(`${B}not${S}+safe${S}+to${S}+merge${B}`);
const VERDICT_NOT_SAFE_RE = re(`^verdict${S}*[:${EM}${EN}-]?${S}*not${S}+safe${B}`);
const RULE_LINE_RE = new RegExp(`^${S}*(?:[-*_]${S}*){3,}$`, "u");
const HEADING_RE = new RegExp(`^ {0,3}#{1,6}(?:${S}|$)`, "u");
Expand Down Expand Up @@ -183,11 +185,14 @@ function read(lines: ReadonlyArray<string>, narrow: boolean): Read {
}

/**
* [the report's blocker and major findings as [kind, line] in line order, whether it has a findings section]. Read
* both ways a lens line is read (narrow or not): a line either way reads as a finding is one. A first line (the header,
* or the title under it) or a `verdict:` line that says NOT SAFE is a blocker whatever the labels say.
* review_judge.read_findings: [[kind, line] of each blocker and major in line order, has a findings section]. A first
* line (the header or its title line) or a `verdict:` line saying NOT SAFE is a blocker. unmarked (no lens marker): the
* first two lines are read as heading and title, and `not safe to merge` outside a fence is a blocker on any line.
*/
export function readFindings(body: string): [Array<[FindingKind, string]>, boolean] {
export function readFindings(
body: string,
unmarked = false,
): [Array<[FindingKind, string]>, boolean] {
pyUnicodeCheck(body);
const lines = splitlines(body);
const found = new Map<number, [FindingKind, string]>();
Expand All @@ -199,14 +204,18 @@ export function readFindings(body: string): [Array<[FindingKind, string]>, boole
if (!found.has(i)) found.set(i, [kind, cut(text, SHOW_FINDING)]);
}
const top = [...lines.keys()].filter((i) => !isBlank(lines[i]!)).slice(0, 2);
const first = new Set(lensHeader(body) ? top : top.slice(0, 1));
const first = new Set(unmarked || lensHeader(body) ? top : top.slice(0, 1));
let fence: string | null = null;
for (const [i, raw] of lines.entries()) {
let fenced: boolean;
[fence, fenced] = fenceStep(fence, raw);
if (fenced || found.has(i)) continue;
const line = plain(raw, false);
if ((first.has(i) && NOT_SAFE_RE.test(line)) || VERDICT_NOT_SAFE_RE.test(line)) {
if (
(first.has(i) && NOT_SAFE_RE.test(line)) ||
VERDICT_NOT_SAFE_RE.test(line) ||
(unmarked && NOT_SAFE_TO_MERGE_RE.test(line))
) {
found.set(i, ["blocker", cut(`the report says NOT SAFE: ${line}`, SHOW_FINDING)]);
}
}
Expand Down
114 changes: 99 additions & 15 deletions apps/server/src/fork/reviewRules/lensComment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/**
* Port of lens.py's header rules and lens.Review.lens_problem/lens_scan (fork-ci/actions/pr-queue), held to the shared
* vectors and the gate's lens corpus. A comment that only looks like a report, or holds a second header, is a
* NO_HEADER entry that reads nothing, as in the gate. The caller picks the posts that count, in the gate's order.
* Port of lens.py's lens rules (fork-ci/actions/pr-queue), held to the shared vectors and the gate's corpus. A lens
* report (one with the lens marker) that is not one valid read is a NO_HEADER entry, and so is a look-alike without
* the marker that holds an open finding; one with no open finding is no report (notLens). The caller picks the posts
* that count, in the gate's order.
*/
import * as Schema from "effect/Schema";

import { readFindings } from "./findings.ts";

import {
ALNUM,
B,
Expand Down Expand Up @@ -63,6 +66,19 @@ export const HIDDEN = "hidden: post a new comment instead";
const FLAGS_UNREAD =
"a review whose edit and hidden flags were not read from GitHub: re-run, or post a new comment";
export const EMBEDDED = "holds a lens header line";
export const NO_MARKER =
"no lens marker (no line starts with `lens:`, and no `read at` or lens field names a sha)";
/** How a lens fixes a report without the marker (lens.REPOST): every reason that names a look-alike says it. */
export const REPOST =
"post a NEW comment whose first line is `lens: <name> · reviewer: <who> · read at: <sha>` (editing the " +
"marker into an old comment does not count)";
/** The one way out of a held comment (lens.HELD_WAY_OUT): delete it or opt it out. */
const HELD_WAY_OUT = "delete the old one or edit it to start with `not a lens` and a colon";
/**
* reportProblem's kinds (lens.BROKEN, HELD, NOT_LENS): a lens report (with the marker) that is not one valid read; a
* comment without the marker that holds an open finding (both block); one without either (no report, never blocks).
*/
export type ReportKind = "broken" | "held" | "notLens";
const OPT_OUT_READ =
"says on its first line that it is no lens report, but holds a hex read-at sha outside a `>` quote";

Expand Down Expand Up @@ -203,6 +219,19 @@ function looseHeader(line: string): boolean {
return !!m && HEX_RE.test(m[1]!);
}

/** lens.marker_line: the line starts with the lens field, names a read (`read at <hex>`) or a lens field with a sha. */
const markerLine = (line: string) =>
FIELD_START_RE.test(line) || READ_AT_RE.test(line) || looseHeader(line);

/**
* lens.has_marker: a line of the comment, in a code block or quote too, read either way, carries the lens marker.
* Only such a comment is a lens report (COMMENTS.md, "The lens marker").
*/
export function hasMarker(body: string): boolean {
const lines = splitlines(body);
return [false, true].some((narrow) => lines.some((line) => markerLine(normLine(line, narrow))));
}

const starts = (first: string) => `starts with ${repr(cut(strip(first), 60))}`;

/** lens.Review.read_problem: lensProblem's rules with one way of reading tags and links. */
Expand Down Expand Up @@ -246,11 +275,52 @@ function readProblem(body: string, narrow: boolean): string | null {
}

/**
* lens.Review.lens_problem: why a comment that looks like a lens report is not one valid read, or null. Every line
* counts, in fences and quotes too; read both ways (narrow or not). An override comment (`<prefix> <40-hex sha>:`)
* never counts.
* lens.Review.report_problem: [why, kind] for a look-alike (looksProblem), null otherwise. Blocks with the marker
* (broken), or without it when readFindings sees an open finding (held: an older clean read must not win). Otherwise
* no lens report (notLens): named, never blocking.
*/
export function reportProblem(
body: string,
overridePrefix?: string,
): [why: string, kind: ReportKind] | null {
const problem = looksProblem(body, overridePrefix);
if (!problem) return null;
if (hasMarker(body)) return [problem, "broken"];
const found = readFindings(body, true)[0]; // no header: the title line and NOT SAFE prose count
if (found.length > 0) {
return [
`${NO_MARKER}, but it holds an open ${found[0]![0]}: ${REPOST}, and ${HELD_WAY_OUT}; it looks like one: ` +
problem,
"held",
];
}
return [`${NO_MARKER}; it looks like one: ${problem}`, "notLens"];
}

/**
* lens.Review.lens_problem: why a comment that looks like a lens report blocks (reportProblem kind broken or held), or
* null: it is one valid read, does not look like a report, or is no lens report (notLensReport).
*/
export function lensProblem(body: string, overridePrefix?: string): string | null {
const got = reportProblem(body, overridePrefix);
return got && got[1] !== "notLens" ? got[0] : null;
}

/**
* lens.Review.not_lens_report: why a comment that looks like a lens report is none (no lens marker and no open
* finding: reportProblem kind notLens), or null.
*/
export function notLensReport(body: string, overridePrefix?: string): string | null {
const got = reportProblem(body, overridePrefix);
return got && got[1] === "notLens" ? got[0] : null;
}

/**
* lens.Review.looks_problem: why a comment that looks like a lens report is not one valid read, with or without the
* marker, or null. Every line counts, in fences and quotes too; read both ways (narrow or not). An override comment
* (`<prefix> <40-hex sha>:`) never counts.
*/
export function looksProblem(body: string, overridePrefix?: string): string | null {
pyUnicodeCheck(body);
if (
overridePrefix !== undefined &&
Expand Down Expand Up @@ -316,6 +386,11 @@ export interface LensScan {
readonly history: Array<[string, string | null]>;
/** Each lens key's newest [sha or null, why it reads nothing or null]. */
readonly entries: Record<string, [string | null, string | null]>;
/**
* [where, why] of each post that looks like a report, has no lens marker and no open finding
* (lens.Review.not_lens_reports).
*/
readonly notLens: Array<[string, string]>;
}

/**
Expand All @@ -327,28 +402,37 @@ export function lensEntries(
posts: ReadonlyArray<LensPost>,
overridePrefix?: string,
skip: (post: LensPost) => boolean = () => false,
): { history: Array<[string, string | null]>; entries: Map<string, LensEntry> } {
): {
history: Array<[string, string | null]>;
entries: Map<string, LensEntry>;
notLens: Array<[string, string]>;
} {
const history: Array<[string, string | null]> = [];
const entries = new Map<string, LensEntry>();
const notLens: Array<[string, string]> = [];
posts.forEach((post, i) => {
if (skip(post)) return;
const at: PostAt = [post.when, i];
const { body } = post;
const hidden = !post.unread && post.hidden;
let problem: string | null;
let got: [string, ReportKind] | null;
let header: [string, string | null] | null;
let key: string;
try {
problem = lensProblem(body, overridePrefix);
header = problem ? null : lensHeader(body);
got = reportProblem(body, overridePrefix);
header = got ? null : lensHeader(body);
key = header ? lensKey(header[0]) : "";
} catch (error) {
if (isPyTextError(error) && error.where === undefined)
throw new PyTextError({ codePoints: error.codePoints, where: post.where });
throw error;
}
if (problem) {
key = `${NO_HEADER} (${post.where}, ${problem}${hidden ? ", hidden" : ""})`;
if (got?.[1] === "notLens") {
notLens.push([post.where, got[0]]);
return;
}
if (got) {
key = `${NO_HEADER} (${post.where}, ${got[0]}${hidden ? ", hidden" : ""})`;
entries.set(key, { at, sha: null, why: null, name: null, body });
return;
}
Expand All @@ -359,13 +443,13 @@ export function lensEntries(
const why = post.unread ? FLAGS_UNREAD : hidden ? HIDDEN : post.edited ? EDITED : null;
entries.set(key, { at, sha: why ? null : header[1], why, name: header[0], body });
});
return { history, entries };
return { history, entries, notLens };
}

/** lens.Review.lens_scan on posts that count, with the repository's override prefix. */
export function lensScan(posts: ReadonlyArray<LensPost>, overridePrefix?: string): LensScan {
const { history, entries } = lensEntries(posts, overridePrefix);
const { history, entries, notLens } = lensEntries(posts, overridePrefix);
const out: Record<string, [string | null, string | null]> = {};
for (const [key, { sha, why }] of entries) out[key] = [sha, why];
return { history, entries: out };
return { history, entries: out, notLens };
}
48 changes: 47 additions & 1 deletion apps/server/src/fork/reviewRules/lensStart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as NodeFS from "node:fs";
import { effectiveReviewRules } from "@t3tools/contracts";
import { assert, describe, it } from "@effect/vitest";

import { NO_HEADER, lensKey, lensScan } from "./lensComment.ts";
import { NO_HEADER, NO_MARKER, REPOST, lensKey, lensScan } from "./lensComment.ts";
import { readFindings } from "./findings.ts";
import { byCodePoint } from "./pyText.ts";
import { vectorPosts, type VectorPost } from "./vectors.testkit.ts";
Expand Down Expand Up @@ -1032,6 +1032,52 @@ describe("lens_start plans what the gate reads (fix round A+B)", () => {
assert.isTrue(gatePasses(basic, [], ["code"], carryOut(covered, after), A));
});

it("a look-alike without the lens marker (#865): ignored and said so, or, with an open finding, held with the fix", () => {
const note: LensStartPost = {
...report("code", A),
body: "## Code lens\nAll fine.",
where: "note",
};
const plan = planLensStart(input({ pullList: [pull(1, { posts: [note] })] }));
assert.deepStrictEqual(lensesOf(plan), ["code"], "the lens has not reported: it starts");
const said = plan.notes.filter((n) =>
n.startsWith("#1: the comment note looks like a lens report"),
);
assert.lengthOf(said, 1);
assert.include(said[0], "the gate ignores it");
assert.include(said[0], NO_MARKER);
assert.include(said[0], REPOST);
assert.deepStrictEqual(planLensStart(input({ pullList: [pull(1, { posts: [note] })] })), plan);
const twice = planLensStart(
input({ pullList: [pull(1, { posts: [note, { ...note, where: "again" }] })] }),
);
assert.lengthOf(
twice.notes.filter((n) => n.includes("the gate ignores it")),
2,
);
const held: LensStartPost = {
...report("code", A),
body: "## Code lens\nblocker: a re-run loses data\nmajor: none",
where: "held",
};
for (const posts of [
[report("code", A), held],
[held, report("code", A)],
]) {
const stuck = planLensStart(input({ pullList: [pull(1, { posts })] }));
assert.deepStrictEqual(
stuck.starts,
[],
"an older or newer clean read never lets it through",
);
assert.include(stuck.refused[0]!.reason, "a comment that reads nothing");
assert.include(stuck.refused[0]!.reason, REPOST);
assert.isFalse(gatePasses(basic, [], ["code"], posts, A));
}
const optedOut = { ...held, body: `not a lens: ${held.body}` };
assert.isTrue(gatePasses(basic, [], ["code"], [report("code", A), optedOut], A));
});

it("a newer override the gate counts is honoured: nothing starts again", () => {
const plan = planLensStart(
input({ pullList: [pull(1, { head: B, posts: [report("code", A), override(B)] })] }),
Expand Down
11 changes: 9 additions & 2 deletions apps/server/src/fork/reviewRules/lensStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { type EffectiveReviewRules, type ReviewRunner, reviewLensRunner } from "

import {
NO_HEADER,
REPOST,
lensEntries,
lensKey,
namesALens,
Expand Down Expand Up @@ -397,7 +398,7 @@ function readPull(
return { refused: `no plan: ${error.reason}; no lens is started rather than too few` };
}
const reads = gateReads(pull.posts, overridePrefix);
const { history } = lensEntries(pull.posts, overridePrefix, isOverride);
const { history, notLens } = lensEntries(pull.posts, overridePrefix, isOverride);
const rounds = startRounds(block, history, head, pull.changeOf);
// The lenses this base can start, by key: the config's, then the input's names (built-in prompt).
const approachKey = lensKey(block.rounds.approach_lens);
Expand All @@ -413,7 +414,13 @@ function readPull(
});
}
}
const notes: string[] = [];
// A look-alike with no lens marker and no open finding: the gate ignores it, so the lens that posted it has not
// reported. Said, so an agent does not take its post for a read.
const notes: string[] = notLens.map(
([where, why]) =>
`#${pull.number}: the comment ${where} looks like a lens report, but the gate ignores it and counts no read ` +
`from it (${why}); if it was meant as a report: ${REPOST}`,
);
const inputOnly: string[] = [];
for (const [key, name] of named) {
if (known.has(key)) continue;
Expand Down
Loading
Loading