refactor(engine): segment.js is TypeScript, the first converted engine module #2483
No reviewers
Labels
No labels
blocked-on-build
bug
device
device-request
enhancement
from-github
needs-datasheet
needs-info
needs-triage
ready-for-agent
ready-for-human
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
jaylouisw/packetforge!2483
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "migrate/segment"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
engine/segment.js(130 lines) is nowengine/segment.ts(236 lines): every parameter, return and exported value is typed, noany, noas, no@ts-nocheck. The docstring and the inline comments are unchanged apart from a note on the extension, so the diff is the types.The shapes the walk reads are written as interfaces rather than imported, because
engine/device.jsandengine/topology.jsare still JavaScript (allowJs: false):VlanPort,SegmentIface,SegmentDevice,SegmentLink,SegmentTopology,SegmentEndpoint. Each function takes the smallest one it actually touches —sameDomaintakes aVlanPort, not a whole interface, because it compares VLAN blocks and reads nothing else.Red check:
Server smoke and Docker build— a finding, not a defect in this change. Refs #2488That job runs its steps in the runner's
ubuntu-latestcontainer, which is Node v20.20.2 — below the floorpackage.jsondeclares (22.18, type stripping with no flag). Nothing noticed until now because no.tsfile was on a runtime path;engine/simulator.jsrequiring./segment.tsis the first one:The image build in the same job is green (
Step 8/8 ... Successfully built), so it is the job's runtime, not the Dockerfile. The threeTestlegs pass because they download Node 22 and 24. Filed as #2488 with the way forward (put the job's container at or above the floor, or run the smoke step under a downloaded Node 22/24) — it is a change toci.yml's runtime, deliberately not folded into a type migration. The smoke test passes locally on node 26 (all 13 checks) and the whole suite passes in CI on both Node legs.Two facts the next conversion needs (verified, not assumed)
requiredoes not resolve a.ts. Probed on this host's node v26.13.1: Both call sites now spell it:engine/simulator.js:116andtest/dhcp-in-path.test.js:18. Trap 5 ofdocs/TYPESCRIPT.mdis real, not stale. The corollary cuts the other way too:engine/segment.tsstill doesrequire('./lag')without an extension becauselag.jsis still JavaScript — the daylag.tslands, that line breaks withMODULE_NOT_FOUNDunless the extension is named.tsconfig.json'sincludeis a filetscnever reads.includewas["scripts/**/*.ts", "test/**/*.test.ts"], soengine/segment.tswould have typechecked green by never being looked at.engine/**/*.tsis added here (and the matching row indocs/TYPESCRIPT.md's option table);npx tsc --noEmit --listFiles | grep segmentshowsengine/segment.tsin the program.Behaviour unchanged
Every suite that names a segment (
grep -rl segment test/, 25 files) was run directly, plus the DHCP family that drives the walk throughSimulator. The six closest were also run againstorigin/main'ssegment.jsin the same tree for an A/B — identical counts:test/dhcp-in-path.test.js(requires the module directly)test/link-state.test.jstest/ubiquiti-gateway-lan-bridges.test.jstest/stp-port-channel-name.test.jstest/vrrp-in-path.test.jstest/dhcp.test.jsThe other 19 segment-naming suites pass unchanged (e.g.
flow91,scion176,show-ospf71,stp64,board-id-is-a-name66,arp-conflict26,address-rule26), as do the DHCP family (dhcp-bindings67,dhcp-leases-survive-save32,dhcp-refusal-visible33, …),lag.test57,lag-member-down34,lag-member-checks8,etherchannel-member-limit45,stp-port-state32,vlan.test65.Gates
tscreports no errors in files this branch did not touch.The ledger
docs/TYPESCRIPT.mdgains a Migrated so far table (and a TOC entry): file, line count, converting commit. One row:engine/segment.ts53bee0cf(#2483)The SHA is the branch commit; the PR number is the handle that survives the squash merge, and the note under the table says so. (Rebased onto
mainafter #2481/#2482, so the pre-rebase SHA this row first carried is gone.)Left untyped, and why
require('./lag')—engine/lag.jsis still JavaScript, so the surface read from it is declared insegment.ts({ memberSuspension(...): MemberSuspension | null }) rather than inferred. The call stays where it was, insideportLive, because that is where the original had it.VlanConfig.modeisstring, not'access' | 'trunk': the untypedInterfaceconstructor cannot prove the union, and a union the source cannot prove is a promise the checker would hold the next converter to.engine/segment.jsindocs/SIMULATION.md,docs/plans/dhcp-relay.md,research/*.mdand the historicalCHANGELOG.mdentry still readsegment.js. The citation check is green; a docs path sweep belongs in its own change.`engine/segment.js` becomes `engine/segment.ts`: every parameter, every return and every exported value is typed, and the shapes the walk reads are written as interfaces (`VlanPort`, `SegmentIface`, `SegmentDevice`, `SegmentLink`, `SegmentTopology`, `SegmentEndpoint`) because `engine/device.js` and `engine/topology.js` are still JavaScript and nothing can be imported from them yet. No `any`, no `as`, no `@ts-nocheck`: `scripts/check-no-any.ts` passes on the file and `tsc --noEmit` reads it. The docstring and the comments are unchanged apart from a note on the extension, so the diff is the types. Two facts every later conversion needs, both verified here rather than assumed (node v26.13.1, and trap 5 of docs/TYPESCRIPT.md): - `require('./segment')` does NOT resolve `segment.ts` from CommonJS - it fails MODULE_NOT_FOUND. Both call sites now spell the extension: `engine/simulator.js` and `test/dhcp-in-path.test.js`. An extensionless relative require also means a later `lag.ts` would break silently, so this is the rule, not a workaround. - A converted file outside `tsconfig.json`'s `include` is a file `tsc` never reads. `engine/**/*.ts` joins the list here, and `--listFiles` confirms the module is in the program. `require('./lag')` stays where it was, inside the walk's predicate: `engine/lag.js` is still JavaScript, so the surface read from it is declared in this file instead of being inferred. Behaviour is unchanged: dhcp-in-path 51, link-state 69, ubiquiti-gateway-lan-bridges 140, stp-port-channel-name 16, vrrp-in-path 49 and dhcp 134 all pass before and after, and the other 19 suites that name a segment pass unchanged.Red on
CI / Server smoke and Docker build— the smoke job still runs Node 20, and this PR is the first.tsthe engine loadsNot a defect in the conversion: the same tree passes on Node 22 and fails on Node 20, and the job that fails is the one job in
ci.ymlthat has nosetup-nodeand therefore runs the runner image's default runtime (ci.yml:230-231: "No setup-node here: this job already runs on Node 20").What fails.
engine/simulator.js:116now reads:Node 20 has no type stripping (it landed in 22.6 experimental / 22.18 on by default), so the require throws before the server binds, and
scripts/smoke-http.jsreports what a step later turns into the red check:What I verified (same tree, exported from this head
2975ff95, run in containers beside the runner):require('./engine/simulator.js')node scripts/smoke-http.jsSyntaxError: Unexpected token '{'(the type annotation insegment.ts)The three test legs are green because they download 22 and 24 explicitly; the Node 18 leg is a separate job that passes because nothing it runs loads the engine. Main's own smoke job is green (33 s) because main has no
.tsengine module yet — this PR is the first, so it is also the first to trip this.Decision required — needs Jay's decision. Two ways out, and both are someone's call rather than a merge:
docker:the way #2477 pinnedTypecheck(itsnode-versionline is already in this file at :888), or move the job to 22/24. This is the honest option:package.jsonalready declares>=22.18as the floor, so the job is the thing out of contract, not the PR.I have not touched
ci.yml— workflow content is not mine to change. Tell me which and I will rebase/merge accordingly; as it stands the head SHA is red and will not be merged by me.(Also: the PR body is the literal string
__PLACEHOLDER__— whatever generated it did not fill it in.)2975ff958315b5ce43b2Rebased onto
main(eac601c1), so the diff is now only this branch's six files. Results on head15b5ce43:CI / TypecheckCI / Test (Node 22)CI / Test (Node 24)CI / Arm auto-mergeCI / Server smoke and Docker buildVerified on the rebased head too, so it is not a stale result: run 10115 prints
Node.js v20.20.2, the image builds (Successfully tagged packetforge:ci-run-10711), and the crash stack isat Object.<anonymous> (.../engine/simulator.js:116:20)— therequire('./segment.ts')line. The same three test legs that load that module are green, because they run Node 22 and 24.The full suite therefore passes on every runtime that meets
engines.node; the one red check is a job running below it, tracked as #2488. Nothing in this branch asks for the smoke job's runtime to change as part of a type migration.15b5ce43b2f555649143