Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
sim/scripts/check-canonical-index-surface.ts at main · simstudioai/sim · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
simstudioai
sim
Repository navigation
Code
Issues
127
(127)
Pull requests
220
(220)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
sim
/
scripts
/
check-canonical-index-surface.ts
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
184 lines (167 loc) · 7.55 KB
main
Breadcrumbs
sim
/
scripts
/
check-canonical-index-surface.ts
Copy path
Top
File metadata and controls
Code
Blame
184 lines (167 loc) · 7.55 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env bun
/**
* Asserts that no code decides a canonical group's membership without saying which SURFACE it
* means.
*
* A block that is both an action and a trigger holds ONE `subBlocks` array: its own fields plus
* its trigger's, spread in after them. The two sets routinely share a `canonicalParamId` under
* DIFFERENT ids — Webflow's `siteSelector`/`manualSiteId` (action) and `triggerSiteId` (trigger)
* are all `siteId`. Indexed together they collapse into one group whose `basicId` belongs to the
* other surface, so the trigger member matches neither `basicId` nor `advancedIds` and every
* group-relative question about it answers for the dormant surface: the canvas card hid it, the
* `dependsOn` gate resolved it to a stale action value, and the fork remap classified it a
* dormant member and CLEARED it.
*
* That shipped three separate times, in three subsystems, each found by hand.
* `buildCanonicalIndexForSurface` makes the correct thing one call, but nothing stopped the next
* caller from reaching for `blockConfig.subBlocks` again — which is what this audit is for. A
* site that genuinely means the whole array says so in an annotation, so the reasoning lives at
* the call instead of being re-derived by the next reader.
*
* The two guarded functions declare their surface differently, so each gets the rule that fits:
*
* - `buildCanonicalIndex` takes the member set directly, so a first argument reading `.subBlocks`
* off a config is unscoped by construction. A call taking an already-narrowed local
* (`contextConfigs`, `activeSubBlocks`, a `getCanonicalSubBlocksForSurface` result) is
* self-evidently fine and is not flagged.
* - `createCanonicalModeGates` scopes internally from a trailing `triggerSurface` argument, so
* what matters is whether the caller passed one at all — omitting it silently means "action".
*/
import
{
spawnSync
}
from
'node:child_process'
import
path
from
'node:path'
const
ROOT
=
path
.
resolve
(
import
.
meta
.
dir
,
'..'
)
/** Placed on the line above a call that deliberately fixes one surface. */
const
ANNOTATION
=
'canonical-index-unscoped:'
/**
* Files that hold the guarded names without calling them.
*
* `buildCanonicalIndexForSurface` has to call the raw `buildCanonicalIndex`, so its defining
* module is exempt rather than annotated. This audit's own source carries both names as string
* literals to search for — without the exemption it flags itself, which is not hypothetical: it
* passed locally while the file was still untracked and failed the moment it was committed and
* `git ls-files` started listing it.
*/
const
NOT_CALLERS
=
new
Set
(
[
'apps/sim/lib/workflows/subblocks/visibility.ts'
,
'scripts/check-canonical-index-surface.ts'
,
]
)
/** The `triggerSurface` argument's position in `createCanonicalModeGates`. */
const
GATES_SURFACE_ARG_COUNT
=
4
/** Preceding non-empty lines searched for the annotation, matching the repo's other boundary annotations. */
const
ANNOTATION_LOOKBACK
=
3
interface
Offender
{
file
:
string
line
:
number
detail
:
string
}
/** The call's top-level arguments, via a balanced scan so a multi-line call still parses. */
function
callArguments
(
source
:
string
,
openParenIndex
:
number
)
:
string
[
]
|
null
{
const
args
:
string
[
]
=
[
]
let
depth
=
0
let
start
=
openParenIndex
+
1
for
(
let
i
=
openParenIndex
;
i
<
source
.
length
;
i
++
)
{
const
char
=
source
[
i
]
if
(
char
===
'('
||
char
===
'['
||
char
===
'{'
)
depth
++
else
if
(
char
===
')'
||
char
===
']'
||
char
===
'}'
)
{
depth
--
if
(
depth
===
0
)
{
const
tail
=
source
.
slice
(
start
,
i
)
.
trim
(
)
if
(
tail
.
length
>
0
||
args
.
length
>
0
)
args
.
push
(
tail
)
return
args
}
}
else
if
(
char
===
','
&&
depth
===
1
)
{
args
.
push
(
source
.
slice
(
start
,
i
)
.
trim
(
)
)
start
=
i
+
1
}
}
return
null
}
/**
* Whether an annotation with a non-empty reason sits on one of the three preceding non-empty
* lines, matching `check-api-validation-contracts.ts`. Deliberately not "three preceding COMMENT
* lines": a guarded call inside a multi-line expression (a ternary arm, a chained `Object.values`)
* is not adjacent to its own comment, and an annotation that cannot be placed is one nobody writes.
*/
function
hasAnnotation
(
lines
:
string
[
]
,
line
:
number
)
:
boolean
{
let
seen
=
0
for
(
let
i
=
line
-
2
;
i
>=
0
&&
seen
<
ANNOTATION_LOOKBACK
;
i
--
)
{
const
text
=
lines
[
i
]
.
trim
(
)
if
(
text
.
length
===
0
)
continue
const
at
=
text
.
indexOf
(
ANNOTATION
)
if
(
at
!==
-
1
)
return
text
.
slice
(
at
+
ANNOTATION
.
length
)
.
trim
(
)
.
length
>
0
seen
++
}
return
false
}
const
listed
=
spawnSync
(
'git'
,
[
'ls-files'
,
'-z'
,
'--'
,
'*.ts'
,
'*.tsx'
]
,
{
cwd
:
ROOT
,
encoding
:
'buffer'
,
maxBuffer
:
256
*
1024
*
1024
,
}
)
if
(
listed
.
status
!==
0
)
{
console
.
error
(
`Canonical-index audit failed: \`git ls-files\` exited
${
listed
.
status
}
.`
)
process
.
exit
(
1
)
}
const
files
=
listed
.
stdout
.
toString
(
'utf8'
)
.
split
(
'\0'
)
.
filter
(
(
entry
)
=>
entry
.
length
>
0
&&
!
entry
.
includes
(
'.test.'
)
&&
!
NOT_CALLERS
.
has
(
entry
)
)
const
offenders
:
Offender
[
]
=
[
]
let
scanned
=
0
let
annotated
=
0
for
(
const
file
of
files
)
{
const
sourceFile
=
Bun
.
file
(
path
.
join
(
ROOT
,
file
)
)
if
(
!
(
await
sourceFile
.
exists
(
)
)
)
continue
const
source
=
await
sourceFile
.
text
(
)
const
hasIndex
=
source
.
includes
(
'buildCanonicalIndex('
)
const
hasGates
=
source
.
includes
(
'createCanonicalModeGates('
)
if
(
!
hasIndex
&&
!
hasGates
)
continue
const
lines
=
source
.
split
(
'\n'
)
const
inspect
=
(
call
:
string
,
verdict
:
(
args
:
string
[
]
)
=>
string
|
null
)
=>
{
for
(
const
match
of
source
.
matchAll
(
new
RegExp
(
`\\b
${
call
}
\\(`
,
'g'
)
)
)
{
const
args
=
callArguments
(
source
,
match
.
index
+
match
[
0
]
.
length
-
1
)
if
(
args
===
null
)
continue
scanned
++
const
problem
=
verdict
(
args
)
if
(
problem
===
null
)
continue
const
line
=
source
.
slice
(
0
,
match
.
index
)
.
split
(
'\n'
)
.
length
if
(
hasAnnotation
(
lines
,
line
)
)
{
annotated
++
continue
}
offenders
.
push
(
{
file
,
line
,
detail
:
`
${
call
}
(…) —
${
problem
}
`
}
)
}
}
if
(
hasIndex
)
{
inspect
(
'buildCanonicalIndex'
,
(
args
)
=>
/
\.
s
u
b
B
l
o
c
k
s
\b
/
.
test
(
args
[
0
]
??
''
)
?
`indexes a config's whole \`subBlocks\`:
${
(
args
[
0
]
??
''
)
.
replace
(
/
\s
+
/
g
,
' '
)
}
`
:
null
)
}
if
(
hasGates
)
{
inspect
(
'createCanonicalModeGates'
,
(
args
)
=>
args
.
length
<
GATES_SURFACE_ARG_COUNT
?
`omits the \`triggerSurface\` argument, so it silently gates as the action surface`
:
null
)
}
}
if
(
offenders
.
length
>
0
)
{
console
.
error
(
`Canonical-index surface audit failed:
${
offenders
.
length
}
call(s) decide canonical group\n`
+
'membership without declaring which surface they mean.\n\n'
+
offenders
.
map
(
(
o
)
=>
`
${
o
.
file
}
:
${
o
.
line
}
\n
${
o
.
detail
}
`
)
.
join
(
'\n\n'
)
+
'\n\n On a block that is both an action and a trigger, both surfaces live in one `subBlocks`\n'
+
' array and routinely share a `canonicalParamId` under different ids. Indexed together, a\n'
+
' trigger field joins the action pair and matches neither side of it — so it gets hidden,\n'
+
" resolved to the dormant surface's value, or cleared as a dormant member.\n\n"
+
' Pass the surface — `buildCanonicalIndexForSurface(subBlocks, triggerSurface)`, or the\n'
+
' trailing argument on `createCanonicalModeGates`. When the surface is provably constant\n'
+
` at the call, say why instead:\n\n //
${
ANNOTATION
}
nested tool params are always the action surface\n`
)
process
.
exit
(
1
)
}
console
.
log
(
`Canonical-index surface audit passed (
${
scanned
}
call(s) checked,
${
annotated
}
annotated).`
)
You can’t perform that action at this time.