Skip to content

Commit 9f06c00

Browse files
committed
fix(gate-manifest): bind the forwarded-rule exemption to lexical scope, not the name
Addresses the remaining P2 from the re-review at 3f37a11. The exemption was keyed on the parameter's SPELLING. Once the live BUILD_OWNERSHIP loop contributed `entry`, every `entry.rule` in the file qualified — so an unrelated loop, or one shadowing the real binding, could pass `entry.rule` to reason(...) and be treated as already collected. Same hole as the `.rule` over-match, one level in. parameterBindings now records each function parameter with the source span it is visible in, and marks the ones bound by a callback of an ownership-table iteration. forwardsACollectedRule resolves a `.rule` access to the INNERMOST binder whose span covers that exact site, and exempts only if that binder is the ownership one. Shadowing therefore resolves the way the language does: BUILD_OWNERSHIP.map((entry) => // ownership binding OTHER.map((entry) => // shadows it reason('lint', file, entry.rule, 'd') // innermost binder wins -> fails closed )) Tests 43 → 46: the mixed fixture the review asked for (one file holding both the legitimate ownership loop and an unrelated same-name `entry.rule`, only the former exempt), the shadowing case above, and a positive case proving the real loop stays exempt and loses no category when no impostor shares its name. Planted red against the live selector: swapping one real rule for `entry.rule` outside the ownership loop's scope now fails with its line number, where before it would have been silently skipped. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SkS4S8XXrfkJ8TD1VBKkvJ
1 parent 3f37a11 commit 9f06c00

2 files changed

Lines changed: 96 additions & 21 deletions

File tree

‎scripts/gate-manifest/selector-rules.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,43 @@ test("the reason factory's own return object declares no category", () => {
7777
assert.deepEqual(selectorRuleIds('m.ts', source), ['gate:lint']);
7878
});
7979

80+
test('the exemption follows the lexical binding, not the name `entry`', () => {
81+
// One file, both shapes. The real BUILD_OWNERSHIP loop contributes a binding spelled `entry`;
82+
// that must not make an unrelated `entry.rule` elsewhere in the file exempt too.
83+
const source = `
84+
const BUILD_OWNERSHIP = [{ check: 'swift-runner', rule: 'own:swift', detail: 'd', owns: () => true }];
85+
const good = BUILD_OWNERSHIP.map((entry) => reason(entry.check, file, entry.rule, entry.detail));
86+
const bad = OTHER.map((entry) => reason('lint', file, entry.rule, 'd'));
87+
`;
88+
assert.throws(() => selectorRuleIds('m.ts', source), /rule argument is not a string literal/);
89+
});
90+
91+
test('a shadowing binding inside the real ownership loop is still rejected', () => {
92+
// The inner `entry` shadows the ownership-table one, so its `.rule` is not a collected
93+
// literal. Span containment alone would wrongly exempt it; the innermost binder decides.
94+
const source = `
95+
const BUILD_OWNERSHIP = [{ check: 'swift-runner', rule: 'own:swift', detail: 'd', owns: () => true }];
96+
const nested = BUILD_OWNERSHIP.map((entry) =>
97+
OTHER.map((entry) => reason('lint', file, entry.rule, 'd')),
98+
);
99+
`;
100+
assert.throws(() => selectorRuleIds('m.ts', source), /rule argument is not a string literal/);
101+
});
102+
103+
test('the real ownership loop stays exempt when no impostor shares its name', () => {
104+
const source = `
105+
const BUILD_OWNERSHIP = [
106+
{ check: 'swift-runner', rule: 'own:swift', detail: 'd', owns: () => true },
107+
];
108+
const good = BUILD_OWNERSHIP.filter((entry) => entry.owns(file)).map((entry) =>
109+
reason(entry.check, file, entry.rule, entry.detail),
110+
);
111+
const other = OTHER.map((row) => row.id);
112+
const a = reason('lint', file, 'gate:lint', 'd');
113+
`;
114+
assert.deepEqual(selectorRuleIds('m.ts', source), ['gate:lint', 'own:swift']);
115+
});
116+
80117
test('a computed rule argument fails closed rather than being skipped', () => {
81118
// A category this reader cannot see would bypass the representative-sample and reachability
82119
// checks entirely — the exact hole the derived universe exists to close.

‎scripts/gate-manifest/selector-rules.ts‎

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// universe and therefore the representative-sample and reachability checks too, so a rule that
1919
// is not a string literal is an error naming its line — never a silent skip. The two shapes
2020
// that legitimately forward an already-collected rule are exempted by PROOF, not by pattern:
21-
// see reasonFactorySpan and ownershipIterationBindings.
21+
// see reasonFactorySpan and parameterBindings.
2222

2323
import {
2424
calleeName,
@@ -115,8 +115,8 @@ function ownershipTableNames(program: Node, factory: Span | null): Set<string> {
115115
}
116116

117117
/**
118-
* Callback parameter names bound while iterating a collected ownership table — the `entry` in
119-
* `BUILD_OWNERSHIP.filter((entry) => …).map((entry) => reason(…, entry.rule, …))`.
118+
* Whether a call iterates a collected ownership table — `BUILD_OWNERSHIP.filter(...)`, and the
119+
* `.map(...)` chained onto it.
120120
*
121121
* This is what makes the forwarding exemption a proof rather than a pattern. Accepting any
122122
* member access named `.rule` would silently skip `reason(check, file, config.rule, …)`, whose
@@ -132,36 +132,74 @@ function iteratesOwnershipTable(node: Node, tables: ReadonlySet<string>): boolea
132132
return root !== null && tables.has(root);
133133
}
134134

135-
/** The first parameter name of each function argument — `entry` in `.map((entry) => …)`. */
136-
function callbackParameterNames(node: Node): string[] {
135+
function isFunctionLike(node: Node): boolean {
136+
return (
137+
node.type === 'ArrowFunctionExpression' ||
138+
node.type === 'FunctionExpression' ||
139+
node.type === 'FunctionDeclaration'
140+
);
141+
}
142+
143+
/** The function arguments of a call — the `(entry) => …` in `.map((entry) => …)`. */
144+
function callbackFunctions(node: Node): Node[] {
137145
const args = node['arguments'];
138146
if (!Array.isArray(args)) return [];
139-
return args.flatMap((argument): string[] => {
140-
if (!isNode(argument)) return [];
141-
const isCallback =
142-
argument.type === 'ArrowFunctionExpression' || argument.type === 'FunctionExpression';
143-
if (!isCallback) return [];
144-
const params = argument['params'];
145-
const name = identifierName(Array.isArray(params) ? params[0] : undefined);
146-
return name === null ? [] : [name];
147-
});
147+
return args.filter((argument): argument is Node => isNode(argument) && isFunctionLike(argument));
148148
}
149149

150-
function ownershipIterationBindings(program: Node, tables: ReadonlySet<string>): Set<string> {
151-
const bindings = new Set<string>();
150+
/**
151+
* A name bound by a function's parameter list, with the source span it is visible in.
152+
*
153+
* Scope, not spelling, is what makes the forwarding exemption safe. Keying it on the NAME
154+
* `entry` would mean that once the real `BUILD_OWNERSHIP` loop contributes that name, any
155+
* unrelated — or shadowing — `entry.rule` elsewhere in the file is exempt too, and a live
156+
* category slips past the derived universe exactly as before. Recording the binding's span
157+
* lets a `.rule` access be matched to the innermost binder that actually covers it.
158+
*/
159+
type ParameterBinding = { name: string; span: Span; iteratesOwnership: boolean };
160+
161+
function parameterBindings(program: Node, tables: ReadonlySet<string>): ParameterBinding[] {
162+
const ownershipCallbacks = new Set<Node>();
152163
collect(program, (node) => {
153164
if (!iteratesOwnershipTable(node, tables)) return;
154-
for (const name of callbackParameterNames(node)) bindings.add(name);
165+
for (const callback of callbackFunctions(node)) ownershipCallbacks.add(callback);
166+
});
167+
168+
const bindings: ParameterBinding[] = [];
169+
collect(program, (node) => {
170+
const span = isFunctionLike(node) ? spanOf(node) : null;
171+
const params = node['params'];
172+
if (span === null || !Array.isArray(params)) return;
173+
const iteratesOwnership = ownershipCallbacks.has(node);
174+
for (const parameter of params) {
175+
const name = identifierName(parameter);
176+
if (name !== null) bindings.push({ name, span, iteratesOwnership });
177+
}
155178
});
156179
return bindings;
157180
}
158181

159-
/** `entry.rule` where `entry` is proven to iterate a collected ownership table. */
160-
function forwardsACollectedRule(argument: unknown, bindings: ReadonlySet<string>): boolean {
182+
/**
183+
* `entry.rule` where `entry` resolves — at this exact site — to the parameter of a callback
184+
* iterating a collected ownership table. The innermost covering binder wins, so a shadowing
185+
* `entry` from an unrelated loop is rejected even inside the real one.
186+
*/
187+
function forwardsACollectedRule(argument: unknown, bindings: readonly ParameterBinding[]): boolean {
161188
if (!isNode(argument) || !isMemberAccess(argument)) return false;
162189
if (identifierName(argument['property']) !== 'rule') return false;
163190
const object = identifierName(argument['object']);
164-
return object !== null && bindings.has(object);
191+
const site = spanOf(argument);
192+
if (object === null || site === null) return false;
193+
const covering = bindings.filter(
194+
(binding) =>
195+
binding.name === object && binding.span.start <= site.start && site.end <= binding.span.end,
196+
);
197+
// Latest start = innermost enclosing function that binds this name.
198+
const innermost = covering.reduce<ParameterBinding | null>(
199+
(best, binding) => (best === null || binding.span.start > best.span.start ? binding : best),
200+
null,
201+
);
202+
return innermost?.iteratesOwnership === true;
165203
}
166204

167205
/** The `rule:` of a build-ownership entry, or `declared: false` for any other object. */
@@ -188,7 +226,7 @@ function buildOwnershipRule(node: Node): { rule: string | null; declared: boolea
188226
export function selectorRuleIds(file: string, source: string): string[] {
189227
const program = parseProgram(file, source);
190228
const factory = reasonFactorySpan(program);
191-
const bindings = ownershipIterationBindings(program, ownershipTableNames(program, factory));
229+
const bindings = parameterBindings(program, ownershipTableNames(program, factory));
192230

193231
const rules = new Set<string>();
194232
const unsupported: string[] = [];

0 commit comments

Comments
 (0)