refactor(verify): exclude negated .ok checks from the abort-reclassification guard

The completed-abort-reclassification pattern matched any `.ok && …aborted`,
including `!outcome.ok && signal?.aborted`, which attaches a secondary
cancellation to an already failed primary and reclassifies nothing. Move
the pattern to completed-abort-guard.mjs, where the receiver chain
(member, optional, private, element and call segments) must start the
check, and cover positive and negated forms in tests.
DesktopApplicationState and FridaInstrumentationService leave the
baseline.

Fixes #11920

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
This commit is contained in:
gensokyo-devandgensokyo-dev authored and Ysopwnable committed 2026-11-21 19:07:31 +01:00
1 parent b95bd814a7
commit a80dc68318
5 files changed
+65 -3

No files matched your search

+2 -1
View File
@@ -11,6 +11,7 @@ import { localCompletenessEnumCount } from "./completeness-enum-guard.mjs";
import { countKeyedFirstMatches } from "./keyed-first-match.mjs";
import { budgetArithmeticCount } from "./budget-arithmetic-guard.mjs";
import { localLimitErrorClassCount } from "./limit-class-guard.mjs";
import { completedAbortReclassificationCount } from "./completed-abort-guard.mjs";
import { countDefaultValidationClients } from "./mcp-client-validation.mjs";
import {
isComparisonOrCountFile,
@@ -635,7 +636,7 @@ checkCounted(
"completed result reclassified by a hand-read abort signal (finalize through runOwned in src/providers/runOwned)",
"completed-abort-reclassification",
files,
/\.ok\s*&&\s*(?:isAborted\(|[\w.?]*signal\??\.aborted)/gu,
completedAbortReclassificationCount,
["src/providers/runOwned.ts"],
);
@@ -1,8 +1,6 @@
{
"src/application/EvidenceInputResolver.ts": 1,
"src/application/binary/BinarySession.ts": 1,
"src/application/desktop/DesktopApplicationState.ts": 1,
"src/application/frida/FridaInstrumentationService.ts": 1,
"src/browser/CdpBrowserProvider.ts": 1,
"src/browser/CdpElectronProvider.ts": 1,
"src/browser/PlaywrightBrowserScenarioProvider.ts": 1,
@@ -0,0 +1 @@
export function completedAbortReclassificationCount(content: string): number;
+18
View File
@@ -0,0 +1,18 @@
// Counts completed results reclassified by a hand-read abort signal:
// `<receiver>.ok && isAborted(…)` or `<receiver>.ok && …signal.aborted`.
// The receiver chain must start the check, so a negated `!outcome.ok && …`,
// which attaches a secondary cancellation to a failed primary, is not
// counted. Receivers may use member, optional, private, element and call
// segments, or start with one parenthesized expression, and each part may
// carry a TypeScript non-null assertion (`result!.ok`). Parentheses may nest
// one level, as in `(await settle()).ok`; element brackets cannot nest.
const parenthesized = String.raw`\((?:[^()]|\([^()]*\))*\)`;
const segment = String.raw`(?:(?:\??\.#?[A-Za-z_$][\w$]*|(?:\?\.)?\[[^[\]]*\]|(?:\?\.)?${parenthesized})!?)`;
const completedAbortReclassification = new RegExp(
String.raw`(?<![\w$#.?\])]|!\s*)(?:[A-Za-z_$][\w$]*|${parenthesized})!?${segment}*\??\.ok\s*&&\s*(?:isAborted\(|[\w.?]*signal\??\.aborted)`,
"gu",
);
/** @param {string} content */
export const completedAbortReclassificationCount = (content) =>
content.match(completedAbortReclassification)?.length ?? 0;
@@ -0,0 +1,44 @@
import { expect, it } from "vitest";
import { completedAbortReclassificationCount } from "../../scripts/verify/completed-abort-guard.mjs";
it.each([
"if (result.ok && isAborted(state.signal))",
"return result.ok && deadline.signal.aborted",
"(waited.ok && isAborted(signal))",
"if (result.ok && options?.signal?.aborted)",
"outcome?.ok && signal.aborted",
"this.#pending.result.ok && signal.aborted",
"results[0].ok && signal.aborted",
"results[index]?.ok && isAborted(signal)",
"getResult().ok && isAborted(signal)",
"this.readResult().outcome.ok && options.signal?.aborted",
"(await settle()).ok && signal.aborted",
"result!.ok && signal.aborted",
"getResult()!.ok && isAborted(signal)",
"results[0]!.outcome!.ok && options.signal?.aborted",
])("counts the completed-result check %s", (source) => {
expect(completedAbortReclassificationCount(source)).toBe(1);
});
it.each([
"if (!outcome.ok && signal?.aborted)",
"if (! started.ok && signal?.aborted)",
"!this.#pending.result.ok && signal.aborted",
"!results[0].ok && signal.aborted",
"!this.readResult().outcome.ok && isAborted(signal)",
"!(await settle()).ok && signal.aborted",
"!result!.ok && signal.aborted",
"!getResult()!.ok && isAborted(signal)",
"if (result.ok) return isAborted(signal);",
"if (result.ok && signal === undefined)",
])("ignores %s", (source) => {
expect(completedAbortReclassificationCount(source)).toBe(0);
});
it("counts each check once", () => {
expect(
completedAbortReclassificationCount(
"if (first.ok && isAborted(signal)) a();\nif (second.ok && signal.aborted) b();",
),
).toBe(2);
});