Skip to content

factory: --port has no upper bound: b4 start --port 70000 crashes with Node's ERR_SOCKET_BAD_PORT instead of Invalid port - #1050

Closed
b4dotrun[bot] wants to merge 1 commit into
mainfrom
factory/wo-f5fea70dc61c255a
Closed

b4dotrun[bot] wants to merge 1 commit into
mainfrom
factory/wo-f5fea70dc61c255a

Conversation

@b4dotrun

@b4dotrun b4dotrun Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Refs #1047

Produced by the B4.run software factory from issue #1047 and approved by a person
(recorded actor: operator) on 2026-10-21T00:25:11.293Z. This is a draft. The factory never merges.

Pin and drift

Branched at 84063ae. main was at 84063ae when delivered, 0 commits ahead of the pin.
No file this change touches changed on main in between.

Approved task

Summary

The CLI's --port parsing has no upper bound and accepts non-decimal numerals (hex and exponential forms). The repair should make both parsers reject values outside the TCP port range and disallow non-decimal spellings.

What to change

- In packages/cli/src/lib/dev/parse-port.ts:
  - Require the raw string to match /^\d+$/ before coercing with Number().
  - After parsing, ensure port is an integer > 0 and <= 65535 (preserve the existing > 0 lower bound).
  - Keep the existing comment that explains why this copy differs from start.ts.

- In packages/cli/src/commands/start.ts (the deliberately-separate copy):
  - Require the raw string to match /^\d+$/ before Number().
  - After parsing, ensure port is an integer >= 0 and <= 65535 (preserve existing acceptance of 0 for ephemeral port).
  - Keep the file's existing comment about accepting 0.

Rationale

- Ports must be within [0, 65535]. Values above 65535 should be rejected by the CLI with a CliError("Invalid port: <raw>", 2) so that the CLI returns the consistent error contract rather than surfacing Node internals (ERR_SOCKET_BAD_PORT).
- Disallowing non-decimal forms (hex like 0x1f90 and exponential like 1e3) preserves the documented --port <number> semantics and avoids surprising bindings.

Tests to add

- Unit test for parsePort (packages/cli/src/lib/dev/parse-port.ts) that asserts:
  - "65535" is accepted and returns 65535.
  - "65536", "70000", "1e3", and "0x1f90" are rejected with CliError("Invalid port: <raw>", 2).

- End-to-end test for the start command behavior (test/start-command.test.ts has a case "rejects an invalid --port before touching the filesystem"): add cases for "65536", "70000", "1e3", and the existing cases. The important property is that runStartCommand rejects with CliError and exit code 2 for invalid inputs.

Acceptance criteria

A1: parsePort rejects non-decimal or out-of-range inputs ("65536", "70000", "1e3", "0x1f90") and accepts "65535".
A2: runStartCommand rejects an invalid --port by rejecting with a CliError whose message is "Invalid port: <raw>" and exitCode 2 (e.g. invoking with port "70000")

Digests and receipts

task d7d558a2c33a0204de36cfa33ea6130b9a24af832a20c88379a61d13bebacabc · bundle 6b8ed3e4e23fb662dd635c901ec9975c8b872ebab93a22389166f4252fedaa13 · candidate 0dbfd2e637bea14550da98fa24f7252a8b3832fb37e01da48d45845306468247
oracle receipt rc-12e47229-31ec-45e3-ad4e-20d2eb0f0597 · verification receipt rc-6be47ec5-38cd-4276-8f26-5fea54e4d863 · re-verification at approval rc-25c2f6f8-20a5-461c-877d-bc97e1795a69
policy 524e28ea232f0f605c1afc3fdf0f73b36b666b4b5d55a28122687b036668b5ed · environment b55af4c0a42e71485017db422dc6da02e46b8b291619b56d756415835bb80593

packages/cli/src/commands/start.ts: e5401ed41f53028082a66c7dc568e98cd3c7a5f9 → c995ba07ecfcd1c181563fa400344282288dc731
packages/cli/src/lib/dev/parse-port.ts: a9a67668ce7a43ebce04f900bb0c29f8b5d93f76 → 59b9512fa1422379de25ca9aa83481cb3e1d58cb

… with Node's `ERR_SOCKET_BAD_PORT` instead of `Invalid port`

Refs #1047

Work order wo-f5fea70dc61c255a, bundle 6b8ed3e4e23fb662dd635c901ec9975c8b872ebab93a22389166f4252fedaa13.
@vercel

vercel Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated
b4-run Ignored Ignored Preview Oct 21, 2026 12:25am UTC
navlog-web Ignored Ignored Preview Oct 21, 2026 12:25am UTC

Request Review

blove added a commit that referenced this pull request Oct 21, 2026
…→ PR #1050) (#1051)

* fix(software-factory): the cli target's promotion set is pin-specific, and the builder searches only its package

The first live run at pin c221ce0 found two things. The cli target's
Dockerfile hard-codes the nested dependencies it promotes over the
root's; the lockfile at this pin nests a different set (ag-ui now nests
the TypeScript 6 compiler core also nests, and axe-core; commander and
hono no longer nest), so the image build failed. The set is now this
pin's, the comment says it is pin-specific, and a name two packages nest
at the same version is promoted once instead of failing the build.

The builder then ran a recursive grep over .., which followed the
node_modules links into the image's install and returned 1.7 MB in one
tool result; the model refused the next call at 513k input tokens. The
working rules now confine searches to the package's own directories and
cap what a search prints.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* feat(software-factory): pre-check refuses a built path no source compiles to, and the CLI entry

Today's live run spent both intake attempts on guessed build paths: a check
importing packages/cli/dist/output.js (the file is dist/lib/output.js), then
one importing dist/index.js, the CLI entry, whose proof hung. Each cost a
two-minute oracle proof.

Rule (f) built-source: a location (static, import()/require()/new URL(), or a
path held in a variable built from join/resolve/process.cwd()/pathToFileURL)
under a target build prefix (snapshotIgnore entries ending dist/) must mirror a
source the pin holds: <pkg>/src/<file>.ts|.tsx|.js|.jsx (.mjs/.cjs likewise),
or <file>/index.ts|.tsx for an extensionless import. The refusal names the
path, the source looked for, and a same-named module under src/ or the
nearest directory's listing.

Rule (g) bin-entry: a location naming the built file a package bin runs
(read at the pin from package.json bin and the bin script's ../dist imports)
is refused.

The drafter prompt's target line now says the built path mirrors src/, to
verify with listDir/readFile under repo/<pkg>/src/, and never to import a bin
entry.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* fix(software-factory): the drafter finds a built file by its source, and the cli target test names this pin's promotion set

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* fix(software-factory): the cli target pins today's main and captures the test helper its tests import

At pin 765e6e1 the captured cli tests did not import
test/helpers/header-auth.ts; at main's tip eight of them do, and the
explicit capture omitted it, so the builder's visible suite failed to
load and the builder spent its turn hunting a helper that was never in
its workspace. The target now pins 84063ae, captures the helper, and
the generator's build order at that pin puts sdk before ag-ui.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* fix(software-factory): a candidate never includes the framework's offloaded tool outputs

B4 spills an oversized tool result to tool-outputs/ in the thread's
workspace. The reader's observed set now leaves that prefix out, as it
does the target's build output: the first live run's correct repair was
refused as a scope violation for the one file the offload had written.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* fix(software-factory): the #714 task re-seeds its defect by patch, now that the cli pin carries the fix

`cli-runs-wait-undefined` was repaired forward: its defect was the pinned
bytes at 765e6e1. The cli target now pins 84063ae, where #718's fix has
landed, so the task ships a defect.patch that reverses it and a reference
regenerated at that pin (same body, new offsets). The inspection test
names `tool-outputs/` as part of the ignored set.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* docs: record the rung 4 live run as landed

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
@blove

blove commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Adopted as #1062: the delivered commit cherry-picked onto adopt/wo-f5fea70dc61c255a, with a changeset and the independent check promoted to packages/cli/test/port-parse.test.ts. Closing this draft; the factory never merges its own.

@blove blove closed this Oct 21, 2026
blove added a commit that referenced this pull request Oct 21, 2026
…e's `ERR_SOCKET_BAD_PORT` instead of `Invalid port` (#1047) (#1062)

* factory: `--port` has no upper bound: `b4 start --port 70000` crashes with Node's `ERR_SOCKET_BAD_PORT` instead of `Invalid port`

Refs #1047

Work order wo-f5fea70dc61c255a, bundle 6b8ed3e4e23fb662dd635c901ec9975c8b872ebab93a22389166f4252fedaa13.

* `--port` has no upper bound: `b4 start --port 70000` crashes with Node's `ERR_SOCKET_BAD_PORT` instead of `Invalid port` (#1047)

Adopted from the factory's draft PR #1050 (work order wo-f5fea70dc61c255a): the delivered commit, cherry-picked, plus a changeset (.changeset/factory-wo-f5fea70dc61c255a.md) and the factory's independent check as packages/cli/test/port-parse.test.ts.

Closes #1047

---------

Co-authored-by: b4dotrun[bot] <344497757+b4dotrun[bot]@users.noreply.github.com>

@blove blove left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maintainer review of the factory's delivered commit 05141c76 (pinned at 84063ae1). This PR was already closed and adopted as #1062, merged to main as a2d2ca32. This review is a retrospective record of the factory's first real output. It is not a merge gate.

Correctness: sound

  • Integer parsing. In both parsers, /^\d+$/ runs before Number(). That rejects 1e3, 0x1f90, +80, " 80", a trailing newline, negatives, and non-ASCII digits (\d without the u flag matches only ASCII). I checked each of these against the patched logic. Leading zeros are still accepted (08080 → 8080, and in start 00000 → 0), which is harmless.
  • 65535 bound. port > 65535 is correct in both files. 65535 is accepted, and 65536 and 70000 are rejected. A 400-digit string becomes Infinity and is rejected too.
  • The two parsers are consistent. parse-port.ts keeps <= 0 and start.ts keeps < 0, as the task required. The comments explaining that difference are kept in both files, and the diffs are otherwise identical.
  • CliError usage matches the surrounding code: new CliError(\Invalid port: ${rawPort}`, 2), with the same message and exit code as the existing branch. The existing start-command.test.ts assertion ("Invalid port: not-a-number"`) still holds.

Nits (non-blocking, fine to leave)

  1. The Number.isInteger check is now redundant. After /^\d+$/, Number() can only return a non-negative integer or Infinity, and the range check already rejects Infinity. Keeping it is defensible as belt-and-braces, but folding both checks into one if would read more simply.
  2. The error message doesn't say what's valid. Invalid port: 70000 keeps the existing contract, which is the right call for a scoped fix. A later polish could append the range, for example (expected 1–65535) for dev/inspect and (expected 0–65535) for start.
  3. The start.ts comment "Unlike dev.ts's parsePort" was already stale before this PR: the shared parser lives in lib/dev/parse-port.ts. Not the factory's to fix under its two-file allowance.
  4. Behavior change worth naming in the changeset: --port " 80" and --port +80 used to be accepted and are now rejected. That is the intended tightening, but it is user-visible.

Scope: what a human would add

  • Changeset. Required, since this is a user-facing @b4run/cli change. The factory doesn't write changesets by design, so changesets is the only red check. Belongs in the adopting PR (#1062 added .changeset/factory-wo-f5fea70dc61c255a.md).
  • Unit test. The PR adds none, because the factory's independent check stays hidden from the builder. Neither parser has a direct test at the PR head: only the -1 / not-a-number case in start-command.test.ts exists. A human would add the boundary cases (65535, 65536, 70000, 1e3, 0x1f90, and 0 for each parser). Belongs in the adopting PR (#1062 promoted the check to packages/cli/test/port-parse.test.ts).
  • Docs. Nothing needed. apps/web/content/docs/cli.mdx documents --port <number> without claiming a range, and rejecting out-of-range values is the expected behavior.
  • The PORT env path is a sibling gap and out of scope here. At the pin, resolveServePort in serve-runtime.ts does Number(envPort) with only an isFinite check, so PORT=70000 b4 start would still crash with ERR_SOCKET_BAD_PORT. That is a separate defect, already handled by #1066.

Local verification on 05141c76

  • pnpm lint: pass (exit 0).

  • pnpm build: pass.

  • pnpm --filter @b4run/cli test: 2768 passed, 12 failed, 3 skipped. None of the failures is caused by this PR. I reverted the two files to the pin and reran the failing files: they still failed (11 at the pin, versus 6 on the head rerun after a full build). They fall into three groups:

    • dev-command and vercel-target: timeouts and lifecycle races. The set of failing tests changed from run to run.
    • api-reference-compatibility: my worktree sat under the macOS /private/tmp → /tmp symlink, so the guard's emitted module paths didn't match. This is environmental.
    • pending-interrupts-endpoint: my first run used a filtered build that skipped postgres-storage/dist. It passed after the full build.

    CI's source-validate, which runs the full pnpm test on this head, passed.

  • start-command.test.ts and inspect-command.test.ts: pass. I also ran main's promoted packages/cli/test/port-parse.test.ts (from #1062) against this head's code: pass, 13/13 across the three files.

CI on the PR: every validation lane passed (source-validate, release-controller, pack-smoke, harness-verify, testing-windows). Only changesets failed, as expected.

Recommendation

Merge as-is after adding a changeset (plus the promoted boundary test). No code changes requested. That is what happened via #1062, so closing this draft was the right disposition.

blove added a commit that referenced this pull request Oct 22, 2026
…ers' checks (#1075)

Follow-up polish to #1062 from the review of #1050:

- Fold the digits-only check and the range check into one condition in both
  parsers. After `/^\d+$/`, Number() can only return a non-negative integer or
  Infinity, so the separate `Number.isInteger` guard was redundant.
- An invalid `--port` now says what is accepted: `1 to 65535` for b4 dev and
  b4 inspect, `0 to 65535` for b4 start.
- Fix the stale "dev.ts's parsePort" comment in start.ts; the shared parser
  lives in lib/dev/parse-port.ts.
- Update the message assertions and cover `parsePort("0")`.

Co-authored-by: Claude Opus 5.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