Skip to content

Commit 8d4eac1

Browse files
committed
fix(ci): close the multiline and duplicate-site gaps in the DI-seam scanner
Review round 2 on PR #2006 (#1976): - findSeamMatches scanned line by line, so a declaration split across lines (`field?:` on one line, `typeof X` on the next) was invisible. Matching now runs against each file's whole source in one pass — `\s` matches a real newline in JavaScript regexes with no extra flag needed — with the line number derived from the match's character offset. - checkSeams keyed approval by (file, field, target) alone, so once one occurrence of a triple was approved, any further occurrence of that same triple anywhere in the file passed too. The key now includes the line the match starts on, so an approval names one specific declaration, not a recurring pattern. approved.ts expands from 5 collapsed entries to the 7 exact sites this closes down to. Added regression tests planting both gaps directly (a cross-line declaration, and a second unreviewed fetchImpl?: typeof fetch at a different line in an already-approved file) and verified both against the real tree with injected violations, restored cleanly afterward. Re-ran the full local gate suite (di-seams, gate-manifest, layering, fallow, format, lint, typecheck) — all green.
1 parent 1978ebc commit 8d4eac1

3 files changed

Lines changed: 121 additions & 51 deletions

File tree

‎scripts/di-seams/approved.ts‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import type { ApprovedSeam } from './model.ts';
22

33
/**
4-
* Each entry is a decision about one (file, field, typeof-target) triple, not a name pattern
5-
* (PR #2006 review, #1976). Exempting every `typeof fetch` seam by spelling would silently pass
6-
* a new, genuinely test-only fetch seam anywhere in the tree while banning an equally legitimate
7-
* seam under any other name. Naming the site is what "approved" means here — add an entry only
8-
* for a call site actually reviewed, never to make a whole identifier exempt.
4+
* Each entry is a decision about one exact declaration — (file, line, field, typeof-target) —
5+
* not a name pattern and not a "this pattern may recur in this file" blanket (PR #2006 review,
6+
* #1976). Exempting every `typeof fetch` seam by spelling would silently pass a new, genuinely
7+
* test-only fetch seam anywhere in the tree while banning an equally legitimate seam under any
8+
* other name; exempting by (file, field, target) alone would silently pass a SECOND, unreviewed
9+
* occurrence of an already-approved field/target pair anywhere in the same file. Naming the exact
10+
* site is what "approved" means here — add an entry only for a call site actually reviewed, never
11+
* to make a whole identifier or field/target pair exempt.
912
*
10-
* check.ts fails as hard on a stale entry (one whose triple no longer matches anything in the
11-
* tree) as it does on an unapproved seam, so this list cannot silently drift out of sync with
12-
* the code it is meant to describe.
13+
* check.ts fails as hard on a stale entry (one whose quadruple no longer matches anything — the
14+
* site moved, even by an unrelated edit shifting its line number, was renamed, or was removed) as
15+
* it does on an unapproved seam, so this list cannot silently drift out of sync with the tree.
1316
*/
1417
export const APPROVED_SEAMS: readonly ApprovedSeam[] = [
1518
{
1619
file: 'src/cli/auth-session.ts',
20+
line: 74,
1721
field: 'fetch',
1822
target: 'fetch',
1923
reason:
@@ -22,29 +26,49 @@ export const APPROVED_SEAMS: readonly ApprovedSeam[] = [
2226
},
2327
{
2428
file: 'src/cli/auth-session.ts',
29+
line: 411,
2530
field: 'fetchImpl',
2631
target: 'fetch',
2732
reason:
2833
'Same fetch-global seam as AuthIo.fetch above, threaded through the device-auth-poll ' +
29-
'and postJson helpers as fetchImpl.',
34+
'helper as fetchImpl.',
35+
},
36+
{
37+
file: 'src/cli/auth-session.ts',
38+
line: 449,
39+
field: 'fetchImpl',
40+
target: 'fetch',
41+
reason:
42+
'Same fetch-global seam as AuthIo.fetch above, threaded through the postJson helper as ' +
43+
'fetchImpl.',
3044
},
3145
{
3246
file: 'src/cli/connection/cloud-profile.ts',
47+
line: 24,
3348
field: 'fetchImpl',
3449
target: 'fetch',
3550
reason:
36-
'Same fetch-global seam, for the cloud connection-profile request. Exercised at the CLI ' +
37-
"layer by cloud-connect-profile.test.ts via vi.stubGlobal('fetch', ...), a layer where " +
38-
'this parameter is not reachable.',
51+
'Same fetch-global seam, on the connect-flow helper that resolves the cloud connection ' +
52+
"profile. Exercised at the CLI layer by cloud-connect-profile.test.ts via vi.stubGlobal('fetch', " +
53+
'...), a layer where this parameter is not reachable.',
54+
},
55+
{
56+
file: 'src/cli/connection/cloud-profile.ts',
57+
line: 67,
58+
field: 'fetchImpl',
59+
target: 'fetch',
60+
reason: 'Same fetch-global seam as above, on the fetchConnectionProfile request itself.',
3961
},
4062
{
4163
file: 'src/remote/daemon-proxy.ts',
64+
line: 22,
4265
field: 'fetchImpl',
4366
target: 'fetch',
4467
reason: "Same fetch-global seam, for the remote daemon proxy's upstream request.",
4568
},
4669
{
4770
file: 'src/daemon/handlers/interaction-touch-response.ts',
71+
line: 61,
4872
field: 'dispatchPath',
4973
target: 'MAESTRO_COORDINATE_FALLBACK_PATH',
5074
reason:

‎scripts/di-seams/model.test.ts‎

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test('findSeamMatches finds an optional typeof field with its line number', () =
1212
line: 2,
1313
field: 'dispatch',
1414
target: 'dispatchCommand',
15-
text: 'dispatch?: typeof dispatchCommand;',
15+
text: 'dispatch?: typeof dispatchCommand',
1616
},
1717
]);
1818
});
@@ -24,10 +24,25 @@ test('findSeamMatches ignores a required (non-optional) field', () => {
2424
assert.deepEqual(matches, []);
2525
});
2626

27-
test('checkSeams passes a match whose exact (file, field, target) triple is approved', () => {
27+
// PR #2006 review, round 2: a per-line scan is blind to a declaration split across lines.
28+
test('findSeamMatches finds a declaration whose `?:` and `typeof` land on different lines', () => {
29+
const matches = findSeamMatches([
30+
{
31+
path: 'a.ts',
32+
source: 'type T = {\n dispatch?:\n typeof dispatchCommand;\n};\n',
33+
},
34+
]);
35+
assert.equal(matches.length, 1);
36+
assert.equal(matches[0]?.field, 'dispatch');
37+
assert.equal(matches[0]?.target, 'dispatchCommand');
38+
// The match starts on the line holding `field?:`, which is what an approval's `line` names.
39+
assert.equal(matches[0]?.line, 2);
40+
});
41+
42+
test('checkSeams passes a match whose exact (file, line, field, target) quadruple is approved', () => {
2843
const matches = findSeamMatches([{ path: 'src/x.ts', source: 'fetchImpl?: typeof fetch;' }]);
2944
const { violations, staleApprovals } = checkSeams(matches, [
30-
{ file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' },
45+
{ file: 'src/x.ts', line: 1, field: 'fetchImpl', target: 'fetch', reason: 'test' },
3146
]);
3247
assert.deepEqual(violations, []);
3348
assert.deepEqual(staleApprovals, []);
@@ -38,7 +53,7 @@ test('checkSeams passes a match whose exact (file, field, target) triple is appr
3853
test('checkSeams flags the same field/target seam in an unapproved file', () => {
3954
const matches = findSeamMatches([{ path: 'src/y.ts', source: 'fetchImpl?: typeof fetch;' }]);
4055
const { violations } = checkSeams(matches, [
41-
{ file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' },
56+
{ file: 'src/x.ts', line: 1, field: 'fetchImpl', target: 'fetch', reason: 'test' },
4257
]);
4358
assert.equal(violations.length, 1);
4459
assert.equal(violations[0]?.file, 'src/y.ts');
@@ -47,7 +62,7 @@ test('checkSeams flags the same field/target seam in an unapproved file', () =>
4762
test('checkSeams flags an unapproved field name in an approved file, even for an approved target', () => {
4863
const matches = findSeamMatches([{ path: 'src/x.ts', source: 'otherField?: typeof fetch;' }]);
4964
const { violations } = checkSeams(matches, [
50-
{ file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' },
65+
{ file: 'src/x.ts', line: 1, field: 'fetchImpl', target: 'fetch', reason: 'test' },
5166
]);
5267
assert.equal(violations.length, 1);
5368
assert.equal(violations[0]?.field, 'otherField');
@@ -56,31 +71,49 @@ test('checkSeams flags an unapproved field name in an approved file, even for an
5671
test('checkSeams flags a seam under a different target, even for an approved field name', () => {
5772
const matches = findSeamMatches([{ path: 'src/x.ts', source: 'fetchImpl?: typeof otherFn;' }]);
5873
const { violations } = checkSeams(matches, [
59-
{ file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' },
74+
{ file: 'src/x.ts', line: 1, field: 'fetchImpl', target: 'fetch', reason: 'test' },
6075
]);
6176
assert.equal(violations.length, 1);
6277
assert.equal(violations[0]?.target, 'otherFn');
6378
});
6479

65-
test('checkSeams reports an approval as stale when its triple matches nothing', () => {
80+
test('checkSeams reports an approval as stale when its quadruple matches nothing', () => {
6681
const { staleApprovals, violations } = checkSeams(
6782
[],
68-
[{ file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' }],
83+
[{ file: 'src/x.ts', line: 1, field: 'fetchImpl', target: 'fetch', reason: 'test' }],
6984
);
7085
assert.equal(violations.length, 0);
7186
assert.equal(staleApprovals.length, 1);
7287
assert.equal(staleApprovals[0]?.field, 'fetchImpl');
7388
});
7489

75-
test('checkSeams approves every occurrence of the same triple within a file', () => {
90+
// PR #2006 review, round 2: approving one occurrence of a (file, field, target) triple must not
91+
// bless a second, unreviewed occurrence of the exact same triple elsewhere in the same file.
92+
test('checkSeams flags a second occurrence of an approved field/target pair at a different line', () => {
93+
const matches = findSeamMatches([
94+
{
95+
path: 'src/x.ts',
96+
source: 'fetchImpl?: typeof fetch;\n// unrelated code\nfetchImpl?: typeof fetch;\n',
97+
},
98+
]);
99+
const { violations, staleApprovals } = checkSeams(matches, [
100+
{ file: 'src/x.ts', line: 1, field: 'fetchImpl', target: 'fetch', reason: 'test' },
101+
]);
102+
assert.equal(violations.length, 1);
103+
assert.equal(violations[0]?.line, 3);
104+
assert.deepEqual(staleApprovals, []);
105+
});
106+
107+
test('checkSeams passes two occurrences of the same triple only when both lines are individually approved', () => {
76108
const matches = findSeamMatches([
77109
{
78110
path: 'src/x.ts',
79-
source: 'fetchImpl?: typeof fetch;\n// ...\nfetchImpl?: typeof fetch;\n',
111+
source: 'fetchImpl?: typeof fetch;\n// unrelated code\nfetchImpl?: typeof fetch;\n',
80112
},
81113
]);
82114
const { violations, staleApprovals } = checkSeams(matches, [
83-
{ file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' },
115+
{ file: 'src/x.ts', line: 1, field: 'fetchImpl', target: 'fetch', reason: 'test' },
116+
{ file: 'src/x.ts', line: 3, field: 'fetchImpl', target: 'fetch', reason: 'test' },
84117
]);
85118
assert.deepEqual(violations, []);
86119
assert.deepEqual(staleApprovals, []);

‎scripts/di-seams/model.ts‎

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
// should ban: some are deliberately approved injection points, and some are unrelated `typeof
44
// CONST` literal-type derivations that only match by syntax coincidence. See approved.ts.
55
//
6-
// #1976 / PR #2006 review: an earlier version of this gate exempted matches by the *spelling* of
7-
// the typeof target (`typeof fetch` always passed, ALL-CAPS targets always passed). That silently
8-
// waved through a new, genuinely test-only `typeof fetch` seam anywhere in the tree while banning
9-
// an equally legitimate seam under any other name — the pattern did not actually own the
10-
// invariant it was enforcing. This module instead checks each match against an explicit,
11-
// per-site allowlist keyed by (file, field name, typeof target): a triple is exempt only if it
12-
// was individually reviewed and named, never because of how it happens to be spelled.
13-
6+
// #1976 / PR #2006 review round 1: an earlier version exempted matches by the *spelling* of the
7+
// typeof target (`typeof fetch` always passed, ALL-CAPS targets always passed) — silently waving
8+
// through a new, genuinely test-only `typeof fetch` seam anywhere in the tree while banning an
9+
// equally legitimate seam under any other name.
10+
//
11+
// Round 2 found two more gaps in the fix for round 1:
12+
// - findSeamMatches scanned line by line, so a declaration split across lines (`field?:` on one
13+
// line, `typeof X` on the next) was invisible — a real seam written that way could reappear
14+
// undetected. Matching is now done against the whole file's source in one pass; `\s` matches a
15+
// real newline in JavaScript regexes with no extra flag needed, so this closes the gap without
16+
// needing a syntax-aware parser.
17+
// - checkSeams keyed approval by (file, field, target) alone, so once one occurrence of a triple
18+
// was approved, ANY further occurrence of that same triple anywhere in the same file passed
19+
// too — a second, unreviewed `fetchImpl?: typeof fetch` added elsewhere in an already-approved
20+
// file would silently pass. The key now includes the line the match starts on, so each
21+
// approval names one specific declaration, not a pattern that recurs freely within a file.
1422
const SEAM_PATTERN = /(\w+)\?\s*:\s*typeof\s+(\w+)/g;
1523

1624
export type SourceFile = {
@@ -29,32 +37,34 @@ export type SeamMatch = {
2937
export function findSeamMatches(files: readonly SourceFile[]): SeamMatch[] {
3038
const matches: SeamMatch[] = [];
3139
for (const { path: file, source } of files) {
32-
source.split('\n').forEach((lineText, idx) => {
33-
SEAM_PATTERN.lastIndex = 0;
34-
let match: RegExpExecArray | null;
35-
while ((match = SEAM_PATTERN.exec(lineText)) !== null) {
36-
matches.push({
37-
file,
38-
line: idx + 1,
39-
field: match[1]!,
40-
target: match[2]!,
41-
text: lineText.trim(),
42-
});
43-
}
44-
});
40+
SEAM_PATTERN.lastIndex = 0;
41+
let match: RegExpExecArray | null;
42+
while ((match = SEAM_PATTERN.exec(source)) !== null) {
43+
const line = source.slice(0, match.index).split('\n').length;
44+
matches.push({
45+
file,
46+
line,
47+
field: match[1]!,
48+
target: match[2]!,
49+
// Collapse the run of whitespace a multiline match spans into one readable line for
50+
// reporting; the actual matched span is what checkSeams and callers key on separately.
51+
text: match[0].replace(/\s+/g, ' ').trim(),
52+
});
53+
}
4554
}
4655
return matches;
4756
}
4857

4958
export type ApprovedSeam = {
5059
readonly file: string;
60+
readonly line: number;
5161
readonly field: string;
5262
readonly target: string;
5363
readonly reason: string;
5464
};
5565

56-
function seamKey(seam: { file: string; field: string; target: string }): string {
57-
return `${seam.file}::${seam.field}::${seam.target}`;
66+
function seamKey(seam: { file: string; line: number; field: string; target: string }): string {
67+
return `${seam.file}:${seam.line}::${seam.field}::${seam.target}`;
5868
}
5969

6070
export type SeamCheckResult = {
@@ -63,11 +73,14 @@ export type SeamCheckResult = {
6373
};
6474

6575
/**
66-
* A match passes only if its exact (file, field, typeof-target) triple is named in `approved` —
67-
* not if its field name or target merely resembles one that is. An approval whose triple no
68-
* longer appears in `matches` is stale: the site moved, was renamed, or was deleted, so the
69-
* approval needs to follow it or be removed. Reporting that as loudly as a real violation is
70-
* what keeps the list from drifting into an allowlist for identifiers rather than sites.
76+
* A match passes only if its exact (file, line, field, typeof-target) quadruple is named in
77+
* `approved` — not if its field name or target merely resembles one that is, and not merely
78+
* because some OTHER line in the same file was approved for the same field/target pair. An
79+
* approval whose quadruple no longer appears in `matches` is stale: the site moved (even by an
80+
* unrelated edit earlier in the file shifting its line number), was renamed, or was deleted, so
81+
* the approval needs to follow it or be removed. Reporting that as loudly as a real violation is
82+
* what keeps the list from drifting into an allowlist for identifiers or patterns rather than
83+
* actual, named declarations.
7184
*/
7285
export function checkSeams(
7386
matches: readonly SeamMatch[],

0 commit comments

Comments
 (0)