refactor(engine): lag.js is TypeScript, and the latent require it was holding is gone #2504

Merged
jaylouisw merged 2 commits from migrate/lag into main 2026-11-24 01:34:07 +00:00
Owner

What

engine/lag.js becomes engine/lag.ts (542 → 710 lines). Every parameter, every return and every
exported value is typed, and the shapes its three jobs share are written as interfaces.

This is migration row three in docs/TYPESCRIPT.md, and it was chosen because it is the one module
a converted file already required
: engine/segment.ts said require('./lag') without an extension
because lag.js was JavaScript. That line was correct, and dead the day this file was renamed — in a
file no conversion edited, for a reason nobody would connect to the diff. node scripts/check-module-paths.ts existed to name it, and named it:

Relative requires in TypeScript that still land on a .js file:
  engine/segment.ts:118  require('./lag')  -> engine/lag.js today; it breaks when that file becomes .ts
  1 latent require - non-fatal, and the sweep that converts the target is the commit that names the extension.

Converting lag retires that hazard rather than creating another. After this branch:

0 unresolvable out of 1486 require() specifiers (556 files scanned) - every require resolves the way CommonJS will.

— the segment.ts note is gone (asserted by test/check-module-paths.test.ts, which computes the
latent case from the tree: "engine/segment.ts no longer requires a still-JavaScript ./lag; nothing
latent to report"
). What the guard prints now is
engine/lag.ts:131 require('./iface-name') -> engine/iface-name.js, which resolves today: the same
debt one link further out, reported as a note rather than failed, and paid the day iface-name is
converted.

The types

Name What it is
LagConfig what normalizeLag() returns and what everything downstream is handed
MemberPort / KnownMember what validateLag() is asked to check — every field but the name is unknown, because a saved lab supplies it
LagProblem / LagVerdict its answer: {code, message} rows in errors and warnings
MemberSuspension why a channel member is out of its bundle; now imported by engine/segment.ts instead of restated there
NegotiationVerdict a discriminated union, not a boolean beside a maybe-string: a bundling pair has no reason to give and a non-bundling pair always has one

Two values on the boundary are genuinely unknown and are narrowed, not asserted (there is no as
in the file):

  • the frame a hash is taken from — a document's own frame record, read through isObject() and
    stringified field by field, so a frame with no L3 header still lands somewhere;
  • the channel group Device.lagFor() answers with — engine/device.js is still JavaScript, so
    groupMode() reads its mode and stringifies it exactly the way canonicalMode() always did,
    rather than inventing a new refusal for a group whose mode is missing.

memberList() (an Array.isArray narrowing with a declared readonly unknown[] return) and
isObject() (a Record<string, unknown> type predicate) are the two guards the rest of the file
narrows through; the { name, enabled } member records the fixtures pass are the same unknown
shape, narrowed by the memberName() / memberEnabled() rules that were already there.

Importers swept

Every one names the extension now — CommonJS does not invent a .ts from an extensionless relative
require (trap 5 of docs/TYPESCRIPT.md):

  • engine/cli.js — 7 call sites
  • engine/device.js, engine/index.js, engine/simulator.js — 1 each
  • engine/segment.ts:118 — the latent one, now require('./lag.ts'), and its local
    MemberSuspension copy is replaced by import type { MemberSuspension } from './lag'
  • test/ — lag, lag-mode, lag-member-down, etherchannel-member-limit,
    channel-group-creating-receipt, brief-port-channel-row, port-channel-name-refusal

Behaviour

Identical. The LAG suites pass before and after with the same counts:

Suite Before After
lag 57 57
lag-mode 54 54
lag-member-down 34 34
lag-member-checks 8 8
etherchannel-member-limit 45 45
channel-group-creating-receipt 22 22
channel-group-undo-grammar 20 20
brief-port-channel-row 19 19
port-channel-name-refusal 37 37
etherchannel-summary-layout 37 37
show-etherchannel-group-summary 25 25
show-etherchannel-views 43 43
stp-port-channel-cost 10 10
stp-port-channel-name 16 16
mac-table-port-channel-name 12 12
vlan-brief-etherchannel 13 13
total 452 452

Gates

  • npx tsc --noEmit — clean
  • node scripts/check-no-any.ts — 0 occurrences in 0 files (9 scanned)
  • node scripts/syntax-check.js — 558 files (549 JavaScript, 9 TypeScript): all OK
  • node scripts/check-module-paths.ts — 0 unresolvable, the segment.ts latent note gone
  • node test/check-module-paths.test.ts — 29 passed
  • node test/canonical-urls.test.js — 88 passed, tree left clean (git status empty)

Left alone

The ledger row is the only docs/TYPESCRIPT.md change beyond the prose paragraph in Migrated so
far
; the gate table belongs to the branch that is editing it. docs/CLI.md, docs/SIMULATION.md,
docs/UPSTREAM-DESIGN.md, docs/LINK-STATE.md and docs/PHYSICAL_LAYER.md still cite
engine/lag.js:NNN in prose — the same way docs/UPSTREAM-DESIGN.md still cites link.js:NNN after
row two — and that citation sweep is a docs change, not this conversion.

## What `engine/lag.js` becomes `engine/lag.ts` (542 → 710 lines). Every parameter, every return and every exported value is typed, and the shapes its three jobs share are written as interfaces. This is migration row three in `docs/TYPESCRIPT.md`, and it was chosen because it is **the one module a converted file already required**: `engine/segment.ts` said `require('./lag')` without an extension because `lag.js` was JavaScript. That line was correct, and dead the day this file was renamed — in a file no conversion edited, for a reason nobody would connect to the diff. `node scripts/check-module-paths.ts` existed to name it, and named it: ``` Relative requires in TypeScript that still land on a .js file: engine/segment.ts:118 require('./lag') -> engine/lag.js today; it breaks when that file becomes .ts 1 latent require - non-fatal, and the sweep that converts the target is the commit that names the extension. ``` Converting lag **retires** that hazard rather than creating another. After this branch: ``` 0 unresolvable out of 1486 require() specifiers (556 files scanned) - every require resolves the way CommonJS will. ``` — the `segment.ts` note is gone (asserted by `test/check-module-paths.test.ts`, which computes the latent case from the tree: *"engine/segment.ts no longer requires a still-JavaScript ./lag; nothing latent to report"*). What the guard prints now is `engine/lag.ts:131 require('./iface-name') -> engine/iface-name.js`, which resolves today: the same debt one link further out, reported as a note rather than failed, and paid the day `iface-name` is converted. ## The types | Name | What it is | |---|---| | `LagConfig` | what `normalizeLag()` returns and what everything downstream is handed | | `MemberPort` / `KnownMember` | what `validateLag()` is asked to check — every field but the name is `unknown`, because a saved lab supplies it | | `LagProblem` / `LagVerdict` | its answer: `{code, message}` rows in `errors` and `warnings` | | `MemberSuspension` | why a channel member is out of its bundle; now imported by `engine/segment.ts` instead of restated there | | `NegotiationVerdict` | a discriminated union, not a boolean beside a maybe-string: a bundling pair has no reason to give and a non-bundling pair always has one | Two values on the boundary are genuinely unknown and are **narrowed, not asserted** (there is no `as` in the file): - the **frame** a hash is taken from — a document's own frame record, read through `isObject()` and stringified field by field, so a frame with no L3 header still lands somewhere; - the **channel group** `Device.lagFor()` answers with — `engine/device.js` is still JavaScript, so `groupMode()` reads its `mode` and stringifies it exactly the way `canonicalMode()` always did, rather than inventing a new refusal for a group whose mode is missing. `memberList()` (an `Array.isArray` narrowing with a declared `readonly unknown[]` return) and `isObject()` (a `Record<string, unknown>` type predicate) are the two guards the rest of the file narrows through; the `{ name, enabled }` member records the fixtures pass are the same `unknown` shape, narrowed by the `memberName()` / `memberEnabled()` rules that were already there. ## Importers swept Every one names the extension now — CommonJS does not invent a `.ts` from an extensionless relative require (trap 5 of `docs/TYPESCRIPT.md`): - `engine/cli.js` — 7 call sites - `engine/device.js`, `engine/index.js`, `engine/simulator.js` — 1 each - **`engine/segment.ts:118`** — the latent one, now `require('./lag.ts')`, and its local `MemberSuspension` copy is replaced by `import type { MemberSuspension } from './lag'` - `test/` — `lag`, `lag-mode`, `lag-member-down`, `etherchannel-member-limit`, `channel-group-creating-receipt`, `brief-port-channel-row`, `port-channel-name-refusal` ## Behaviour Identical. The LAG suites pass before and after with the same counts: | Suite | Before | After | |---|---|---| | lag | 57 | 57 | | lag-mode | 54 | 54 | | lag-member-down | 34 | 34 | | lag-member-checks | 8 | 8 | | etherchannel-member-limit | 45 | 45 | | channel-group-creating-receipt | 22 | 22 | | channel-group-undo-grammar | 20 | 20 | | brief-port-channel-row | 19 | 19 | | port-channel-name-refusal | 37 | 37 | | etherchannel-summary-layout | 37 | 37 | | show-etherchannel-group-summary | 25 | 25 | | show-etherchannel-views | 43 | 43 | | stp-port-channel-cost | 10 | 10 | | stp-port-channel-name | 16 | 16 | | mac-table-port-channel-name | 12 | 12 | | vlan-brief-etherchannel | 13 | 13 | | **total** | **452** | **452** | ## Gates - `npx tsc --noEmit` — clean - `node scripts/check-no-any.ts` — `0 occurrences in 0 files (9 scanned)` - `node scripts/syntax-check.js` — `558 files (549 JavaScript, 9 TypeScript): all OK` - `node scripts/check-module-paths.ts` — 0 unresolvable, **the segment.ts latent note gone** - `node test/check-module-paths.test.ts` — 29 passed - `node test/canonical-urls.test.js` — 88 passed, tree left clean (`git status` empty) ## Left alone The ledger row is the only `docs/TYPESCRIPT.md` change beyond the prose paragraph in *Migrated so far*; the gate table belongs to the branch that is editing it. `docs/CLI.md`, `docs/SIMULATION.md`, `docs/UPSTREAM-DESIGN.md`, `docs/LINK-STATE.md` and `docs/PHYSICAL_LAYER.md` still cite `engine/lag.js:NNN` in prose — the same way `docs/UPSTREAM-DESIGN.md` still cites `link.js:NNN` after row two — and that citation sweep is a docs change, not this conversion.
refactor(engine): lag.js is TypeScript, and the latent require it was holding is gone
Some checks failed
CI / Test (Node 24) (pull_request) Has been cancelled
CI / Typecheck (pull_request) Has been cancelled
CI / Arm auto-merge (pull_request) Has been cancelled
CI / Server smoke and Docker build (pull_request) Has been cancelled
PR rebase / Rebase onto main and arm auto-merge (pull_request) Has been cancelled
CI / Test (Node 22) (pull_request) Has been cancelled
0eb0ad3e5f
`engine/lag.js` becomes `engine/lag.ts` (542 -> 710 lines): every parameter, every return and every
exported value is typed, and the shapes its three jobs share are written as interfaces -
`LagConfig` (what `normalizeLag()` returns), `MemberPort` / `KnownMember` (what `validateLag()` is
asked to check), `LagProblem` / `LagVerdict` (its answer), `MemberSuspension`, and a
`NegotiationVerdict` that is a discriminated union rather than a boolean beside a maybe-string: a
pair that bundles has no reason to give, and a pair that does not always has one.

This is the module a converted file already required. `engine/segment.ts` said `require('./lag')`
without an extension because `lag.js` was JavaScript - correct, and dead the moment this file was
renamed, in a file no conversion edited, for a reason nobody would connect to the diff. That is the
whole reason this module was next, and the conversion retires the hazard instead of inheriting it:
`engine/segment.ts` names `./lag.ts` and imports `MemberSuspension` from the module rather than
restating it, so the shape cannot drift from the function that returns it.

Two values on that boundary are genuinely unknown and are narrowed rather than asserted:

  - the frame a hash is taken from - a document's own frame record, read through `isObject()` and
    stringified field by field, so a frame with no L3 header still lands somewhere;
  - the channel group `Device.lagFor()` answers with - `engine/device.js` is still JavaScript, so
    `groupMode()` reads its `mode` and stringifies it exactly the way `canonicalMode()` always did,
    instead of turning a group into a refusal this module has no business inventing.

No `any`, no `as`, no `@ts-nocheck`: `node scripts/check-no-any.ts` passes on the file, and
`tsc --noEmit` is clean.

Every importer spells the extension, because CommonJS does not resolve a `.ts` from an
extensionless relative require (trap 5 of docs/TYPESCRIPT.md):

  - `engine/cli.js`        7 call sites
  - `engine/device.js`     1
  - `engine/index.js`      1
  - `engine/simulator.js`  1
  - `engine/segment.ts`    1 - the latent one, now `require('./lag.ts')`
  - `test/lag.test.js`, `test/lag-mode.test.js`, `test/lag-member-down.test.js`,
    `test/etherchannel-member-limit.test.js`, `test/channel-group-creating-receipt.test.js`,
    `test/brief-port-channel-row.test.js`, `test/port-channel-name-refusal.test.js` - 7 suites

`node scripts/check-module-paths.ts` no longer prints the latent note it existed to print for this
line. What it prints now is `engine/lag.ts:131 require('./iface-name') -> engine/iface-name.js`,
which resolves today: the same debt one link further out, reported rather than failed, and paid the
day `iface-name` is converted.

Behaviour is unchanged. The LAG suites pass before and after, with identical counts: lag 57,
lag-mode 54, lag-member-down 34, lag-member-checks 8, etherchannel-member-limit 45,
channel-group-creating-receipt 22, channel-group-undo-grammar 20, brief-port-channel-row 19,
port-channel-name-refusal 37, etherchannel-summary-layout 37, show-etherchannel-group-summary 25,
show-etherchannel-views 43, stp-port-channel-cost 10, stp-port-channel-name 16,
mac-table-port-channel-name 12, vlan-brief-etherchannel 13 (452 assertions); `check-module-paths`
29, canonical-urls 88, `git status` empty after the run.
docs(typescript): the ledger row for lag.ts, the third converted module
All checks were successful
CI / Typecheck (pull_request) Successful in 18s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 6s
CI / Server smoke and Docker build (pull_request) Successful in 36s
CI / Test (Node 22) (pull_request) Successful in 57s
CI / Test (Node 24) (pull_request) Successful in 1m0s
CI / Arm auto-merge (pull_request) Successful in 5s
7742fd3400
One row, in the shape of the two above it: the file, its line count, the converting commit and the
pull request that carries the number. 542 -> 710, in `0eb0ad3e` (#2504).
forgejo-actions scheduled this pull request to auto merge when all checks succeed 2026-11-24 01:24:33 +00:00
Author
Owner

Whole tree, not just the LAG suites: all 476 suites pass, 0 failed (node scripts/run-tests.js,
run in two halves to fit a terminal budget — the first 258 suites and the remaining 218, both with
every line a ✓ and no failed anywhere in either log).

The four gates on the final head:

$ npx tsc --noEmit                       # clean, no output
$ node scripts/check-no-any.ts
0 occurrences in 0 files (9 scanned) — no explicit `any` in a type position.
$ node scripts/syntax-check.js
Syntax-checked 558 files (549 JavaScript, 9 TypeScript): all OK
$ node scripts/check-module-paths.ts
Relative requires in TypeScript that still land on a .js file:
  engine/lag.ts:131  require('./iface-name')  -> engine/iface-name.js today; it breaks when that file becomes .ts
  1 latent require - non-fatal, and the sweep that converts the target is the commit that names the extension.

0 unresolvable out of 1486 require() specifiers (556 files scanned) - every require resolves the way CommonJS will.
$ node test/canonical-urls.test.js
88 passed, 0 failed

The line this PR exists for — engine/segment.ts:118 require('./lag') — is not in that output any
more, and test/check-module-paths.test.ts says so in words: "engine/segment.ts no longer requires
a still-JavaScript ./lag; nothing latent to report"
. git status is empty after every run.

Whole tree, not just the LAG suites: all **476** suites pass, 0 failed (`node scripts/run-tests.js`, run in two halves to fit a terminal budget — the first 258 suites and the remaining 218, both with every line a `✓` and no `failed` anywhere in either log). The four gates on the final head: ``` $ npx tsc --noEmit # clean, no output $ node scripts/check-no-any.ts 0 occurrences in 0 files (9 scanned) — no explicit `any` in a type position. $ node scripts/syntax-check.js Syntax-checked 558 files (549 JavaScript, 9 TypeScript): all OK $ node scripts/check-module-paths.ts Relative requires in TypeScript that still land on a .js file: engine/lag.ts:131 require('./iface-name') -> engine/iface-name.js today; it breaks when that file becomes .ts 1 latent require - non-fatal, and the sweep that converts the target is the commit that names the extension. 0 unresolvable out of 1486 require() specifiers (556 files scanned) - every require resolves the way CommonJS will. $ node test/canonical-urls.test.js 88 passed, 0 failed ``` The line this PR exists for — `engine/segment.ts:118 require('./lag')` — is not in that output any more, and `test/check-module-paths.test.ts` says so in words: *"engine/segment.ts no longer requires a still-JavaScript ./lag; nothing latent to report"*. `git status` is empty after every run.
Sign in to join this conversation.
No description provided.