refactor(engine): segment.js is TypeScript, the first converted engine module #2483

Merged
jaylouisw merged 2 commits from migrate/segment into main 2026-11-23 23:42:00 +00:00
Owner

What

engine/segment.js (130 lines) is now engine/segment.ts (236 lines): every parameter, return and exported value is typed, no any, no as, 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.js and engine/topology.js are still JavaScript (allowJs: false): VlanPort, SegmentIface, SegmentDevice, SegmentLink, SegmentTopology, SegmentEndpoint. Each function takes the smallest one it actually touches — sameDomain takes a VlanPort, 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 #2488

That job runs its steps in the runner's ubuntu-latest container, which is Node v20.20.2 — below the floor package.json declares (22.18, type stripping with no flag). Nothing noticed until now because no .ts file was on a runtime path; engine/simulator.js requiring ./segment.ts is the first one:

--- node scripts/smoke-http.js ---
  FAIL server did not start — server did not answer /api/catalog within the timeout
    at Object.<anonymous> (/workspace/jaylouisw/packetforge/engine/simulator.js:116:20)
Node.js v20.20.2
::error title=Server smoke test::scripts/smoke-http.js exited 1

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 three Test legs 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 to ci.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)

  1. An extensionless relative require does not resolve a .ts. Probed on this host's node v26.13.1:
    extensionless require("./mod"):   MODULE_NOT_FOUND Cannot find module './mod'
    explicit require("./mod.ts"):     loaded
    
    Both call sites now spell it: engine/simulator.js:116 and test/dhcp-in-path.test.js:18. Trap 5 of docs/TYPESCRIPT.md is real, not stale. The corollary cuts the other way too: engine/segment.ts still does require('./lag') without an extension because lag.js is still JavaScript — the day lag.ts lands, that line breaks with MODULE_NOT_FOUND unless the extension is named.
  2. A converted file outside tsconfig.json's include is a file tsc never reads. include was ["scripts/**/*.ts", "test/**/*.test.ts"], so engine/segment.ts would have typechecked green by never being looked at. engine/**/*.ts is added here (and the matching row in docs/TYPESCRIPT.md's option table); npx tsc --noEmit --listFiles | grep segment shows engine/segment.ts in 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 through Simulator. The six closest were also run against origin/main's segment.js in the same tree for an A/B — identical counts:

Suite before after
test/dhcp-in-path.test.js (requires the module directly) 51 passed, 0 failed 51 passed, 0 failed
test/link-state.test.js 69 69
test/ubiquiti-gateway-lan-bridges.test.js 140 140
test/stp-port-channel-name.test.js 16 16
test/vrrp-in-path.test.js 49 49
test/dhcp.test.js 134 134

The other 19 segment-naming suites pass unchanged (e.g. flow 91, scion 176, show-ospf 71, stp 64, board-id-is-a-name 66, arp-conflict 26, address-rule 26), as do the DHCP family (dhcp-bindings 67, dhcp-leases-survive-save 32, dhcp-refusal-visible 33, …), lag.test 57, lag-member-down 34, lag-member-checks 8, etherchannel-member-limit 45, stp-port-state 32, vlan.test 65.

Gates

npx tsc --noEmit                     clean (--listFiles proves segment.ts is in the program)
node scripts/check-no-any.ts         0 occurrences in 0 files (4 scanned)
node scripts/syntax-check.js         Syntax-checked 546 files: all OK
node scripts/check-control-bytes.ts  No control bytes in source.
node scripts/check-doc-citations.js  380 citations, 25 judged, none misplaced
node scripts/check-node-surface.js   No API newer than Node 18 in 545 source file(s).
CI Test (Node 22) / Test (Node 24) / Typecheck   green

tsc reports no errors in files this branch did not touch.

The ledger

docs/TYPESCRIPT.md gains a Migrated so far table (and a TOC entry): file, line count, converting commit. One row:

File Lines Converted by
engine/segment.ts 236 53bee0cf (#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 main after #2481/#2482, so the pre-rebase SHA this row first carried is gone.)

Left untyped, and why

  • require('./lag') — engine/lag.js is still JavaScript, so the surface read from it is declared in segment.ts ({ memberSuspension(...): MemberSuspension | null }) rather than inferred. The call stays where it was, inside portLive, because that is where the original had it.
  • VlanConfig.mode is string, not 'access' | 'trunk': the untyped Interface constructor cannot prove the union, and a union the source cannot prove is a promise the checker would hold the next converter to.
  • Out of scope here: prose references to engine/segment.js in docs/SIMULATION.md, docs/plans/dhcp-relay.md, research/*.md and the historical CHANGELOG.md entry still read segment.js. The citation check is green; a docs path sweep belongs in its own change.
## What `engine/segment.js` (130 lines) is now `engine/segment.ts` (236 lines): every parameter, return and exported value is typed, no `any`, no `as`, 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.js` and `engine/topology.js` are still JavaScript (`allowJs: false`): `VlanPort`, `SegmentIface`, `SegmentDevice`, `SegmentLink`, `SegmentTopology`, `SegmentEndpoint`. Each function takes the smallest one it actually touches — `sameDomain` takes a `VlanPort`, 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 #2488 That job runs its steps in the runner's `ubuntu-latest` container, which is **Node v20.20.2** — below the floor `package.json` declares (22.18, type stripping with no flag). Nothing noticed until now because no `.ts` file was on a *runtime* path; `engine/simulator.js` requiring `./segment.ts` is the first one: ``` --- node scripts/smoke-http.js --- FAIL server did not start — server did not answer /api/catalog within the timeout at Object.<anonymous> (/workspace/jaylouisw/packetforge/engine/simulator.js:116:20) Node.js v20.20.2 ::error title=Server smoke test::scripts/smoke-http.js exited 1 ``` 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 three `Test` legs 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 to `ci.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) 1. **An extensionless relative `require` does not resolve a `.ts`.** Probed on this host's node v26.13.1: ``` extensionless require("./mod"): MODULE_NOT_FOUND Cannot find module './mod' explicit require("./mod.ts"): loaded ``` Both call sites now spell it: `engine/simulator.js:116` and `test/dhcp-in-path.test.js:18`. Trap 5 of `docs/TYPESCRIPT.md` is real, not stale. The corollary cuts the other way too: `engine/segment.ts` still does `require('./lag')` **without** an extension because `lag.js` is still JavaScript — the day `lag.ts` lands, that line breaks with `MODULE_NOT_FOUND` unless the extension is named. 2. **A converted file outside `tsconfig.json`'s `include` is a file `tsc` never reads.** `include` was `["scripts/**/*.ts", "test/**/*.test.ts"]`, so `engine/segment.ts` would have typechecked green by never being looked at. `engine/**/*.ts` is added here (and the matching row in `docs/TYPESCRIPT.md`'s option table); `npx tsc --noEmit --listFiles | grep segment` shows `engine/segment.ts` in 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 through `Simulator`. The six closest were also run against `origin/main`'s `segment.js` in the same tree for an A/B — identical counts: | Suite | before | after | |---|---|---| | `test/dhcp-in-path.test.js` (requires the module directly) | 51 passed, 0 failed | 51 passed, 0 failed | | `test/link-state.test.js` | 69 | 69 | | `test/ubiquiti-gateway-lan-bridges.test.js` | 140 | 140 | | `test/stp-port-channel-name.test.js` | 16 | 16 | | `test/vrrp-in-path.test.js` | 49 | 49 | | `test/dhcp.test.js` | 134 | 134 | The other 19 segment-naming suites pass unchanged (e.g. `flow` 91, `scion` 176, `show-ospf` 71, `stp` 64, `board-id-is-a-name` 66, `arp-conflict` 26, `address-rule` 26), as do the DHCP family (`dhcp-bindings` 67, `dhcp-leases-survive-save` 32, `dhcp-refusal-visible` 33, …), `lag.test` 57, `lag-member-down` 34, `lag-member-checks` 8, `etherchannel-member-limit` 45, `stp-port-state` 32, `vlan.test` 65. ## Gates ``` npx tsc --noEmit clean (--listFiles proves segment.ts is in the program) node scripts/check-no-any.ts 0 occurrences in 0 files (4 scanned) node scripts/syntax-check.js Syntax-checked 546 files: all OK node scripts/check-control-bytes.ts No control bytes in source. node scripts/check-doc-citations.js 380 citations, 25 judged, none misplaced node scripts/check-node-surface.js No API newer than Node 18 in 545 source file(s). CI Test (Node 22) / Test (Node 24) / Typecheck green ``` `tsc` reports no errors in files this branch did not touch. ## The ledger `docs/TYPESCRIPT.md` gains a **Migrated so far** table (and a TOC entry): file, line count, converting commit. One row: | File | Lines | Converted by | |---|---|---| | `engine/segment.ts` | 236 | `53bee0cf` (#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 `main` after #2481/#2482, so the pre-rebase SHA this row first carried is gone.) ## Left untyped, and why - `require('./lag')` — `engine/lag.js` is still JavaScript, so the surface read from it is declared in `segment.ts` (`{ memberSuspension(...): MemberSuspension | null }`) rather than inferred. The call stays where it was, inside `portLive`, because that is where the original had it. - `VlanConfig.mode` is `string`, not `'access' | 'trunk'`: the untyped `Interface` constructor cannot prove the union, and a union the source cannot prove is a promise the checker would hold the next converter to. - Out of scope here: prose references to `engine/segment.js` in `docs/SIMULATION.md`, `docs/plans/dhcp-relay.md`, `research/*.md` and the historical `CHANGELOG.md` entry still read `segment.js`. The citation check is green; a docs path sweep belongs in its own change.
refactor(engine): segment.js is TypeScript, the first converted engine module
Some checks failed
CI / Typecheck (pull_request) Successful in 21s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 8s
CI / Server smoke and Docker build (pull_request) Failing after 51s
CI / Test (Node 22) (pull_request) Successful in 50s
CI / Test (Node 24) (pull_request) Successful in 1m1s
CI / Arm auto-merge (pull_request) Successful in 3s
1de2cf8c3a
`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.
docs(typescript): the 'Migrated so far' ledger, with engine/segment.ts as row one
Some checks failed
CI / Typecheck (pull_request) Successful in 26s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 17s
CI / Server smoke and Docker build (pull_request) Failing after 55s
CI / Test (Node 22) (pull_request) Successful in 58s
CI / Test (Node 24) (pull_request) Successful in 1m11s
CI / Arm auto-merge (pull_request) Successful in 3s
2975ff9583
The migration is tracked by a table rather than by memory: one row per converted
file, its line count, and the commit that did it. `engine/segment.js` -> `236`
lines in `1de2cf8c` (#2483), which is the only row there is.

The note under it records what the row cost and the two places a type is written
by hand: `engine/lag.js`'s `memberSuspension()`, which is still JavaScript, and
`VlanConfig.mode`, declared `string` because the untyped `Interface` constructor
cannot prove `'access' | 'trunk'`.
Author
Owner

Red on CI / Server smoke and Docker build — the smoke job still runs Node 20, and this PR is the first .ts the engine loads

Not 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.yml that has no setup-node and 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:116 now reads:

const segmentLib = require('./segment.ts');

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.js reports what a step later turns into the red check:

FAIL server did not start — server did not answer /api/catalog within the timeout
    at Object.<anonymous> (/w/engine/simulator.js:116:20)

What I verified (same tree, exported from this head 2975ff95, run in containers beside the runner):

runtime require('./engine/simulator.js') node scripts/smoke-http.js
node:20-bookworm (v20.20.2 — the runner's runtime) SyntaxError: Unexpected token '{' (the type annotation in segment.ts) exit 1, "server did not start"
node:22-bookworm loads exit 0, "HTTP smoke passed (all checks clean)"

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 .ts engine 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:

  1. Give the smoke job a runtime that can read the code — pin Node 22 in docker: the way #2477 pinned Typecheck (its node-version line is already in this file at :888), or move the job to 22/24. This is the honest option: package.json already declares >=22.18 as the floor, so the job is the thing out of contract, not the PR.
  2. Hold engine conversions until that job is retired on 22, i.e. land the runtime change first and this PR after.

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.)

## Red on `CI / Server smoke and Docker build` — the smoke job still runs Node 20, and this PR is the first `.ts` the engine loads Not 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.yml` that has no `setup-node` and 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:116` now reads: ```js const segmentLib = require('./segment.ts'); ``` 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.js` reports what a step later turns into the red check: ``` FAIL server did not start — server did not answer /api/catalog within the timeout at Object.<anonymous> (/w/engine/simulator.js:116:20) ``` **What I verified** (same tree, exported from this head `2975ff95`, run in containers beside the runner): | runtime | `require('./engine/simulator.js')` | `node scripts/smoke-http.js` | |---|---|---| | node:20-bookworm (v20.20.2 — the runner's runtime) | `SyntaxError: Unexpected token '{'` (the type annotation in `segment.ts`) | exit 1, "server did not start" | | node:22-bookworm | loads | exit 0, "HTTP smoke passed (all checks clean)" | 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 `.ts` engine 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: 1. **Give the smoke job a runtime that can read the code** — pin Node 22 in `docker:` the way #2477 pinned `Typecheck` (its `node-version` line is already in this file at :888), or move the job to 22/24. This is the honest option: `package.json` already declares `>=22.18` as the floor, so the job is the thing out of contract, not the PR. 2. **Hold engine conversions** until that job is retired on 22, i.e. land the runtime change first and this PR after. 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.)*
jaylouisw force-pushed migrate/segment from 2975ff9583
Some checks failed
CI / Typecheck (pull_request) Successful in 26s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 17s
CI / Server smoke and Docker build (pull_request) Failing after 55s
CI / Test (Node 22) (pull_request) Successful in 58s
CI / Test (Node 24) (pull_request) Successful in 1m11s
CI / Arm auto-merge (pull_request) Successful in 3s
to 15b5ce43b2
Some checks failed
CI / Server smoke and Docker build (pull_request) Failing after 28s
CI / Typecheck (pull_request) Successful in 15s
CI / Test (Node 22) (pull_request) Successful in 45s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 4s
CI / Test (Node 24) (pull_request) Successful in 49s
CI / Arm auto-merge (pull_request) Successful in 3s
2026-11-23 23:28:59 +00:00
Compare
Author
Owner

Rebased onto main (eac601c1), so the diff is now only this branch's six files. Results on head 15b5ce43:

Check Result
CI / Typecheck success
CI / Test (Node 22) success
CI / Test (Node 24) success
CI / Arm auto-merge success
CI / Server smoke and Docker build failure — the Node 20 runtime, #2488

Verified 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 is at Object.<anonymous> (.../engine/simulator.js:116:20) — the require('./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.

Rebased onto `main` (`eac601c1`), so the diff is now only this branch's six files. Results on head `15b5ce43`: | Check | Result | |---|---| | `CI / Typecheck` | success | | `CI / Test (Node 22)` | success | | `CI / Test (Node 24)` | success | | `CI / Arm auto-merge` | success | | `CI / Server smoke and Docker build` | failure — the Node 20 runtime, #2488 | Verified on the rebased head too, so it is not a stale result: run [10115](https://git.jayw.uk/jaylouisw/packetforge/actions/runs/10115) prints `Node.js v20.20.2`, the image builds (`Successfully tagged packetforge:ci-run-10711`), and the crash stack is `at Object.<anonymous> (.../engine/simulator.js:116:20)` — the `require('./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.
jaylouisw force-pushed migrate/segment from 15b5ce43b2
Some checks failed
CI / Server smoke and Docker build (pull_request) Failing after 28s
CI / Typecheck (pull_request) Successful in 15s
CI / Test (Node 22) (pull_request) Successful in 45s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 4s
CI / Test (Node 24) (pull_request) Successful in 49s
CI / Arm auto-merge (pull_request) Successful in 3s
to f555649143
All checks were successful
CI / Typecheck (pull_request) Successful in 19s
CI / Server smoke and Docker build (pull_request) Successful in 41s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 7s
CI / Test (Node 22) (pull_request) Successful in 58s
CI / Test (Node 24) (pull_request) Successful in 1m2s
CI / Arm auto-merge (pull_request) Successful in 3s
2026-11-23 23:37:15 +00:00
Compare
forgejo-actions scheduled this pull request to auto merge when all checks succeed 2026-11-23 23:41:04 +00:00
jaylouisw deleted branch migrate/segment 2026-11-23 23:42:03 +00:00
Sign in to join this conversation.
No description provided.