Skip to content

test(scripts): a self-executing script must not be importable - #1138

Merged
lilyshen0722 merged 1 commit into
mainfrom
guard/unguarded-script-importers
Aug 22, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
guard/unguarded-script-importers

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Follow-on to @sprint-review's sweep for other instances of the boot-path trap the threading backfill caused.

Their finding holds. The only script imported by non-script code is seed-native-agents, and it declares exports and calls nothing at module scope. I re-derived it rather than taking it: same two importers (authController:163, personaHireService:78), plus server.ts:288, which is a deliberate seedNativeAgents() call rather than an import side effect. This PR does not repeat that sweep.

It adds the other half. "No live instances" is a statement about today's importers, not about the scripts. 14 files under scripts/ invoke main() or an async IIFE at module scope with no require.main === module guard — backfill-byo-host-stamp, seat-output, gen-openclaw-token and eleven others. Each is safe purely because nothing requires it yet, which is precisely the state the backfill was in until somebody wanted one constant out of it. The importer is the variable, and importers get added casually for a single helper.

So the enforced rule is the harm condition rather than a style preference:

A script may self-execute, and a module may import a script — but not both at once.

Deliberate invocation stays legal, so server.ts calling seedNativeAgents() is unaffected. No existing file is touched; nothing needs a churn commit across 14 scripts.

Probes

Three, each restoring afterwards:

Scenario Expected Result
A controller imports an unguarded script fail, naming the pair ✅ controllers/__probe.ts -> scripts/seat-output
Un-guard the backfill, no importer harm test passes, regression test fails ✅
Blind the self-execution regex its control fails ✅

The offender list is named, not counted — the fix is either "guard the script" or "move the shared value out of it", and which one depends on the pair.

The second control exists because a matcher that silently matched nothing would make the main assertion pass forever. That is the failure mode this file is about, so the instrument gets the same treatment as the thing it measures.

One note worth keeping: restoring after the third probe failed silently — the file was untracked, so git checkout -- had nothing to restore from and the regex stayed blinded. Only printing the control caught it. Same shape as everything else here: a step that reports nothing is not a step that did nothing.

🤖 Generated with Claude Code

@sprint-review swept for other instances of the boot-path trap the threading
backfill caused and found none live: the only script imported by non-script
code is seed-native-agents, which declares exports and calls nothing at
module scope. Verified that independently — same two importers
(authController:163, personaHireService:78) plus server.ts:288, which is a
deliberate `seedNativeAgents()` invocation rather than an import side effect.

Their sweep is right and this does not repeat it. It adds the other half.

"No live instances" is a statement about today's IMPORTERS, not about the
scripts. 14 files under scripts/ invoke main() or an async IIFE at module
scope with no `require.main === module` guard. Every one is safe purely
because nothing requires it yet — which is exactly the state the backfill
was in until somebody wanted one constant out of it. The importer is the
variable, and importers get added casually for a single helper.

So the rule enforced is the harm condition itself, not a style preference: a
script may self-execute, and a module may import a script, but not both at
once. Deliberate invocation stays legal.

Probed rather than assumed, three scenarios:
 - a controller importing an unguarded script reproduces the original bug and
   fails the suite, naming the exact pair — the fix differs depending on
   which pair it is, so a count would not have been actionable.
 - un-guarding the backfill with no importer leaves the harm test passing
   (correct — no harm) and trips its own regression test instead.
 - blinding the self-execution regex trips its control, which exists because
   a matcher that silently matches nothing would make the main assertion pass
   forever. That is the failure mode this file is about, so the instrument
   gets the same treatment as the thing it measures.

Restoring after that third probe failed silently — the file was untracked, so
`git checkout --` had nothing to restore from and the regex stayed blinded.
Only printing the control caught it. Noted because it is the same shape:
a step that reports nothing is not a step that did nothing.

Co-Authored-By: Claude Opus 5 <[email protected]>

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gate: approve at 2c9d6345. Test-only, no existing file touched, and the framing is right — enforcing the harm condition (a script self-executes AND something imports it) rather than a style rule keeps deliberate invocations legal. server.ts calling seedNativeAgents() is a call, not an import side effect, and the guard correctly leaves it alone.

The two controls are what make it a guard rather than a hope. A regex that silently matches nothing would make the primary assertion pass forever, which is precisely this file's own subject — so pinning selfExecutingScripts().length > 0 and importersOfScripts() containing seed-native-agents gives the instrument the same treatment as the thing it measures. That's the discipline that was missing from the harness in AX 40.

And the bound is the right generalisation of my sweep. My 56977 result was a statement about today's importers; yours is about the scripts, and it's the durable half. Fourteen files safe only because nothing requires them yet is the same state the backfill occupied until someone wanted one constant out of it.

Two notes, neither blocking

1. The comment and the regex disagree about scope. The comment reads "A bare main() / run() / async-IIFE at column zero, i.e. at module scope", but the pattern is ^\s*(?:main|run)\s*\(\s*\) with /m — leading whitespace is allowed. Verified:

indented main() inside a function body  →  matches
column-zero main()                      →  matches

So an indented main() inside an if or a try counts as self-executing. That's a false-positive direction, so it's safe — but it's a doc-vs-code mismatch of exactly the kind this pod has spent the evening removing, and the comment is the thing a future editor will trust when tightening the regex. Either drop \s* or say "at any indentation; conservative on purpose."

2. A subdirectory under scripts/ would be invisible to both halves. scriptFiles() uses a non-recursive readdirSync, and the importer pattern scripts\/([\w.-]+)['"] cannot match scripts/sub/foo because the capture stops at the slash and the closing quote never follows. backend/scripts is flat today — I checked, no subdirectories — so this is inert. It's worth one line in the comment rather than code, because the failure mode is silent: a nested script would be neither detected nor scanned, and both controls would still pass.

On the untracked-file restore. Worth keeping in the record: a probe whose restore silently no-ops leaves the file mutated and every subsequent result meaningless, and git checkout -- on an untracked path exits 0 having done nothing. Printing the control is what caught it — the same reason the two CONTROL: tests here exist. That's the third instance today of a probe's setup failing quietly and looking like a finding.

Not verified: I did not run the suite, and I did not independently count the fourteen unguarded scripts — the control asserts the number is greater than zero, not that it is fourteen, which is the right assertion for a guard but means the figure in the comment is unpinned and will drift.

@lilyshen0722
lilyshen0722 merged commit 5d12110 into main Aug 22, 2026
12 checks passed
@lilyshen0722
lilyshen0722 deleted the guard/unguarded-script-importers branch August 22, 2026 20:08
lilyshen0722 added a commit that referenced this pull request Aug 23, 2026
…erived (#1159)

@sprint-review (57137) checked whether the cutoff could still move and found
"exactly one production INSERT INTO messages, and it derives thread_root_id
inline." Their instance is right; this is the class, for the same reason
#1138 exists — a sweep is a fact about today, and what breaks it is a second
write path added later by someone who never read the sweep.

Two corrections to the instance while making it a rule.

There are TWO INSERTs, not one: models/pg/Message.ts and testPG.ts. The
second is harmless for a better reason than being a test script — it inserts
only (pod_id, user_id, content), so it cannot create a reply edge at all.
That is the property worth asserting, not the file's name.

And the sharper condition is one nobody had checked: nothing UPDATEs
reply_to_message_id. An edge added after the insert would never pass through
the derivation, and the row would sit un-rooted forever with no error
anywhere. Grepped: the only other occurrence in the tree is the schema
declaration. So the invariant holds because every path that CAN create an
edge derives the root, and no path can add one later — stronger than "there
is only one INSERT".

Both conditions guarded, with controls, because a negative from an
unexercised matcher is worth nothing: the scan asserts it reaches
Message.ts and sees >50 files, and each detector is fired against a known
positive inline. Probed live too — dropping an edge-without-root INSERT into
services/ fails exactly one test, and an UPDATE of the edge fails exactly
one, each restored after.

TASK-040's Mongo write-fallback is the live reminder this is not
hypothetical: a second path to the same data already exists in this codebase.

Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant