From 2f65e8a8103413be94680f617c264f8682d053ed Mon Sep 17 00:00:00 2001 From: garrytan Date: Tue, 13 Oct 2026 00:47:24 +0000 Subject: [PATCH] fix(plan-reality): a receipt per measured number, the rule text for a rule claim, dot-path receipts Each clause stating a measured quantity (a unit, or a number after measured/costs/ takes/took) needs its own receipt; misses print unreceipted_numbers= and one NUMBER_UNRECEIPTED: line each. A line claiming a decision rule must quote it or link its file:line (unquoted=). Either miss maps to the existing verdict (incomplete unattended, repair otherwise). RECEIPT_RE accepts dot paths and needs a hex letter or a commit/backtick context for a sha. New pure checkPlanReality(planText) runs the same checks over plan prose for gstack-autoplan submit (field wave item 12). The unreceipted= text token is now pinned as well as the JSON. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- bin/gstack-plan-reality | 8 +++- lib/plan-reality.ts | 92 ++++++++++++++++++++++++++++++++++++--- test/plan-reality.test.ts | 80 +++++++++++++++++++++++++++++++++- 3 files changed, 172 insertions(+), 8 deletions(-) diff --git a/bin/gstack-plan-reality b/bin/gstack-plan-reality index be368b8d17..a5a880a75d 100755 --- a/bin/gstack-plan-reality +++ b/bin/gstack-plan-reality @@ -20,7 +20,9 @@ const HELP = `gstack-plan-reality — reality rows for plan reviews Row line: \`${REALITY_LINE_SHAPE}\` (receipt: path:line, path:line-line or a commit sha). Scopes: ${REALITY_SCOPES.filter(s => s !== 'always').join(', ')}. Session kind unattended → verdict=incomplete; else verdict=repair. -Prints \`PLAN_REALITY: phase=

rows=/ missing= [unverified=] verdict=complete|incomplete|repair\`. +Prints \`PLAN_REALITY: phase=

rows=/ missing= [unreceipted=] [unreceipted_numbers=] [unquoted=] [unverified=] verdict=complete|incomplete|repair\`, +then one \`NUMBER_UNRECEIPTED: \` per measured number (unit or measured/costs/took) without its own receipt. +A line claiming a decision rule must quote it (\`"..."\`, a fenced or > block after it) or link it (file:line). ${exitTableHelp()}`; function flag(args: string[], name: string): string | undefined { @@ -55,10 +57,12 @@ function main(argv: string[]): number { const sessionKind = flag(rest, '--session-kind') ?? process.env.GSTACK_SESSION_KIND; const result = checkRealityRows(text, { phase, scopes, sessionKind }); if (json) process.stdout.write(JSON.stringify(result) + '\n'); - else process.stdout.write(result.line + '\n'); + else process.stdout.write([result.line, ...result.numberLines].join('\n') + '\n'); for (const id of result.unknownRows) process.stderr.write(`gstack-plan-reality: ${file}: REALITY row ${JSON.stringify(id)} is not a known row; it was ignored\n`); if (result.missing.length) process.stderr.write(resultLine('gstack-plan-reality check', 'REALITY_ROW_MISSING', `phase ${phase}: ${result.missing.join(', ')}`) + '\n'); if (result.unreceipted.length) process.stderr.write(resultLine('gstack-plan-reality check', 'REALITY_RECEIPT_MISSING', `phase ${phase}: ${result.unreceipted.join(', ')}`) + '\n'); + if (result.unreceipted_numbers.length) process.stderr.write(resultLine('gstack-plan-reality check', 'REALITY_RECEIPT_MISSING', `phase ${phase}: ${result.unreceipted_numbers.length} measured number(s) share or lack a receipt; give each clause its own file:line, script path or command`) + '\n'); + for (const claim of result.unquoted) process.stderr.write(resultLine('gstack-plan-reality check', 'REALITY_RECEIPT_MISSING', `phase ${phase}: a decision rule is claimed without its text; quote it or link its file:line: ${claim}`) + '\n'); return result.verdict === 'complete' ? EXIT.ok : EXIT.fail; } return usage(`unknown command ${cmd}`); diff --git a/lib/plan-reality.ts b/lib/plan-reality.ts index 1acbdb82ec..fdb2911daa 100644 --- a/lib/plan-reality.ts +++ b/lib/plan-reality.ts @@ -156,8 +156,8 @@ export function parseScopes(csv: string | undefined): { scopes: RealityScope[]; return { scopes, unknown }; } -/** `path:line`, `path:line-line` (the path has a letter and no spaces) or a 7–40 hex sha. */ -export const RECEIPT_RE = /(?:(?|]\s*)?`?REALITY:\s+(?[a-z][a-z0-9-]*)\s+(?pass|finding|n\/a)\b(?.*)$/; export interface RealityLine { id: string; status: RealityStatus; rest: string; receipt: string | null; line: number; metrics: Record } @@ -185,6 +185,12 @@ export interface RealityCheck { missing: string[]; /** Required rows whose line carries no receipt. */ unreceipted: string[]; + /** Clauses stating a measured number without a receipt of their own. */ + unreceipted_numbers: string[]; + /** Lines claiming a decision rule exists without quoting or linking its text. */ + unquoted: string[]; + /** One `NUMBER_UNRECEIPTED: ` line per unreceipted number. */ + numberLines: string[]; /** Lines naming a row id the table does not define. */ unknownRows: string[]; metrics: Record; @@ -192,6 +198,52 @@ export interface RealityCheck { line: string; } +/** + * A measured quantity: a number with a unit (s, ms, %, $, USD, tokens, ×, MB, GB) or a number shortly after + * measured/costs/takes/took. Receipts, versions and ids are removed before matching, so they never count. + */ +const MEASURED_RE = /\$\s?\d|\b\d[\d,]*(?:\.\d+)?\s?[kKM]?\s?(?:ms|s|%|USD|tokens?|MB|GB)(?![\w])|\b\d[\d,]*(?:\.\d+)?\s?[×x](?![\w])|\b(?:measured|costs?|takes|take|took)\b\W+(?:[A-Za-z]+\W+){0,3}?\$?\d/i; +const NOT_A_QUANTITY_RE = new RegExp(`${RECEIPT_RE.source}|\\bv?\\d+(?:\\.\\d+){2,}\\b|#\\d+|\\b[A-Za-z]+\\d+[a-z]?\\b`, 'g'); +const EXEMPT_TAG_RE = /\[(?:estimated|re-measure before gate)\]/i; +const SCRIPT_RE = /(?:^|[\s(`"'])(?:\.?[\w-]+\/)*[\w.-]+\.(?:ts|js|mjs|cjs|sh|py|rb|go)\b|(?:^|[\s(`"'])(?:bin|scripts)\/[\w.-]+/; +const COMMAND_RE = /`[^`]*\s[^`]*`/; +const RULE_CLAIM_RE = /\b(?:decision rule|rules? (?:was |were )?written first|pre-?registered rule)\b/i; +const QUOTED_RULE_RE = /"[^"]{8,}"|\u201c[^\u201d]{8,}\u201d/; +const LINK_RE = /(? c.trim()).filter(Boolean); +} + +/** Clauses of one line that state a measured number and carry no receipt of their own. */ +export function unreceiptedNumbers(line: string): string[] { + return splitClauses(line).filter(c => MEASURED_RE.test(c.replace(NOT_A_QUANTITY_RE, ' ')) + && !RECEIPT_RE.test(c) && !SCRIPT_RE.test(c) && !COMMAND_RE.test(c) && !EXEMPT_TAG_RE.test(c)); +} + +/** A rule claim needs the rule: a quoted span, a file:line link on the line, or a fenced or quoted block right after it. */ +function ruleUnquoted(lines: string[], i: number): boolean { + const line = lines[i]!; + if (!RULE_CLAIM_RE.test(line) || QUOTED_RULE_RE.test(line) || LINK_RE.test(line)) return false; + const after = lines.slice(i + 1).find(l => l.trim()); + return !after || !/^\s*(?:```|~~~|>)/.test(after); +} + +const verdictFor = (ok: boolean, sessionKind?: string): RealityVerdict => (ok ? 'complete' : sessionKind === 'unattended' ? 'incomplete' : 'repair'); + export interface CheckOptions { phase: string; scopes?: readonly string[]; sessionKind?: string } /** Deterministic: which required rows the review carries. The verdict word depends on the session kind only. */ @@ -214,14 +266,44 @@ export function checkRealityRows(reviewText: string, o: CheckOptions): RealityCh const metric = rowById(id)!.metric; if (metric && l.metrics[metric] !== undefined) metrics[metric] = l.metrics[metric]!; } - const ok = missing.length === 0 && unreceipted.length === 0; - const verdict: RealityVerdict = ok ? 'complete' : o.sessionKind === 'unattended' ? 'incomplete' : 'repair'; + const rows = lines.filter(l => rowById(l.id) && l.status !== 'n/a'); + const numbers = rows.flatMap(l => unreceiptedNumbers(l.rest)); + const rawLines = reviewText.split('\n'); + const unquoted = rows.filter(l => ruleUnquoted(rawLines, l.line - 1)).map(l => rawLines[l.line - 1]!.trim()); + const verdict = verdictFor(missing.length === 0 && unreceipted.length === 0 && numbers.length === 0 && unquoted.length === 0, o.sessionKind); const parts = [`PLAN_REALITY: phase=${o.phase}`, `rows=${present.length}/${required.length}`, `missing=${missing.length ? missing.join(',') : 'none'}`]; if (unreceipted.length) parts.push(`unreceipted=${unreceipted.join(',')}`); + if (numbers.length) parts.push(`unreceipted_numbers=${numbers.length}`); + if (unquoted.length) parts.push(`unquoted=${unquoted.length}`); for (const [k, v] of Object.entries(metrics)) parts.push(`${k}=${v}`); parts.push(`verdict=${verdict}`); - return { phase: o.phase, required, present, missing, unreceipted, unknownRows, metrics, verdict, line: parts.join(' ') }; + return { phase: o.phase, required, present, missing, unreceipted, unreceipted_numbers: numbers, unquoted, + numberLines: numbers.map(c => `NUMBER_UNRECEIPTED: ${c}`), unknownRows, metrics, verdict, line: parts.join(' ') }; +} + +/** Bumped whenever checkPlanReality's rules change, so a cached verdict keyed on the plan hash is recomputed. */ +export const PLAN_REALITY_CHECKER = 'plan-reality/2'; +export interface PlanRealityCheck { checker: string; unreceipted_numbers: string[]; unquoted: string[]; numberLines: string[]; verdict: RealityVerdict; line: string } + +/** + * The same number and rule checks over plan prose (the Implementation plan the caller passes), outside fenced blocks. + * Pure: reads nothing, writes nothing. `gstack-autoplan submit` records it per plan hash; it never refuses a submit. + */ +export function checkPlanReality(planText: string, o: { sessionKind?: string } = {}): PlanRealityCheck { + const lines = planText.split('\n'); + const numbers: string[] = [], unquoted: string[] = []; + let fence: string | null = null; + lines.forEach((line, i) => { + const delimiter = /^\s{0,3}(`{3,}|~{3,})/.exec(line)?.[1]; + if (delimiter && (!fence || delimiter.startsWith(fence))) { fence = fence ? null : delimiter; return; } + if (fence || /^\s*