Skip to content

fix(cli): reject a non-numeric --port or --project-id instead of passing NaN - #1782

Merged
SaulMoro merged 3 commits into
Tencent:mainfrom
pcbeingused333:fix/numeric-opts-validation
Nov 8, 2026
Merged

SaulMoro merged 3 commits into
Tencent:mainfrom
pcbeingused333:fix/numeric-opts-validation

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

teamai dashboard --port abc passed Number('abc') = NaN to server.listen. Node throws options.port should be >= 0 and < 65536. Received type number (NaN)., but by then startDashboard has already started the events watcher and its two intervals, so the error is printed and the process keeps running instead of exiting. teamai bind-project --project-id abc produced NaN, which is falsy, so bindWorkspaceToProject silently fell back to the interactive project picker instead of failing. Same pattern as the --max-repos fix in #1781.

Both options now go through a small parseIntOption helper (src/cli-int.ts): --port must be an integer in 0-65535 and --project-id an integer >= 1. Anything else fails before any work starts, with --port must be an integer between 0 and 65535 (or the --project-id equivalent), through the existing command error path. Number.parseInt used to accept 12abc as 12; that is rejected now too.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature causing existing behavior to change)
  • Documentation only
  • Refactor / internal cleanup

Test Plan

New src/__tests__/cli-int.test.ts covers valid values and rejects abc, empty, 1.5, -3, 70000, 1e3, 0x10 (9 tests).

  • npx tsc --noEmit passes
  • npm run lint passes (oxlint on the touched files)
  • npx vitest run passes (only the new test file was run)
  • Added/updated tests for the change

Related Issues

None filed; found next to #1781.

Notes for Reviewers

This change was prepared with the assistance of an AI coding tool (Claude) and reviewed before submission.

Real-CLI verification (added by a reviewer)

Built 78be94ed and main 05fe72a2 (npm run build). Isolated HOME, empty GIT_CONFIG_GLOBAL, GIT_CONFIG_NOSYSTEM=1,
TEAMAI_NONINTERACTIVE=1, a fresh git repo as cwd, no team install. macOS, Node 24.

teamai dashboard --port abc
  main:  ✖ options.port should be >= 0 and < 65536. Received type number (NaN).   exit 1
  head:  ✖ --port must be an integer between 0 and 65535                          exit 1
teamai bind-project --project-id abc
  main:  ✖ Local agent not initialized. Run `teamai init --http` first.            exit 1
  head:  ✖ --project-id must be an integer >= 1                                   exit 1
teamai dashboard --port 0 (main): "Dashboard running at http://localhost:0"
No dashboard process left running after either build.

…ing NaN

dashboard --port abc gave Number('abc') = NaN to server.listen, which threw a raw
range error; bind-project --project-id abc passed NaN to the local agent. Validate
both as integers (port 1-65535, project id >= 1) and fail with a clear message.
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
  • [P1 blocking] src/index.ts:1547: teamai bind-project --project-id "" bypasses validation because the empty string is falsy, passing undefined and falling back to the interactive picker instead of rejecting the invalid value. Check whether the option is provided rather than whether it is truthy.
  • [P1 blocking] PR description, Test Plan: This changes runtime behavior but documents only type checking, lint, and helper unit tests. The Code Review Rules require a representative end-to-end / real-CLI verification record after npm run build; none is provided.

@SaulMoro SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was generated by AI during triage.

Approving. --port and --project-id now fail with a message that names the flag, before any work starts. I ran the built CLI on this head and on main and added the runs to the description, which closes the CI review's missing-record item:

  • dashboard --port abc: Node's options.port … Received type number (NaN) becomes --port must be an integer between 1 and 65535. Both exit 1.
  • bind-project --project-id abc: the value is refused before the "Local agent not initialized" check runs.

Two things in the description did not reproduce here. On main, dashboard --port abc exited 1 immediately and left no process running, so it did not keep running. With no HTTP install, bind-project stopped at "Local agent not initialized" before it reached the picker. The fix is right either way: a bad value is refused up front with a clear message.

--port 0 is refused now. On main it printed "Dashboard running at http://localhost:0", which is not a usable address, so refusing it is the fix and not a lost feature.

[P2 non-blocking] The CI review is right that --project-id "" still slips through: cmdOpts.projectId ? … : undefined treats the empty string as "not given" and falls back to the picker. Main does the same, so this PR does not regress it, and an empty id is an unusual input, so we don't block on it. Checking cmdOpts.projectId !== undefined would close it.

[P3 nit] parsePositiveInt in src/import.ts (#1781) and the new parseIntOption do the same job. A later change could point import at cli-int.ts, so there is one parser.

Merge risk: low

Alignment: good

  • Rejects the bad values the title names. The other change is the 12abc → 12 case, which is now refused, as the description says.

Impact: small

  • Two option parsers. Valid values behave as before.

Verification: decent

  • Unit tests for the parser and the built-CLI runs above. The hang and picker paths the description mentions were not reproduced.

Rollback cost: low

  • A revert restores the old parse.

Error detection: good

  • A bad value now fails with a message naming the flag.

CI review: the empty-string point is the P2 above, which we take as non-blocking. The missing real-CLI record is now in the description. I'll merge once CI is green at this head.

@SaulMoro SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was generated by AI during triage.

@pcbeingused333 I'm changing my approval to request changes. I was wrong about --port 0.

[P1 blocking] Refusing --port 0 breaks a real use. E2E shard 1/3 fails at report-scope-reuse.test.ts:128 with ✖ --port must be an integer between 1 and 65535 (job 128507769496). That test starts teamai dashboard --port 0 (:121) to get a free port from the OS, and it passes on main. Port 0 listens on an OS-assigned port. Only the startup message prints localhost:0, which I misread as an unusable address in my review. Refusing it is also a stricter-validation change for anyone who scripts the dashboard that way. Fix: allow 0 for --port, i.e. parseIntOption('--port', String(cmdOpts.port), 0, 65535), with a test case for 0. Printing the port the server actually got (server.address().port) would be a nice touch, but it is not needed for this PR.

The rest of my earlier review stands: the --project-id "" P2 and the parser-duplication P3.

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
  • [P1 blocking] src/index.ts:1547: teamai bind-project --project-id "" still bypasses validation because the empty string is falsy. It passes undefined, allowing fallback to the interactive picker instead of rejecting the invalid value. Check cmdOpts.projectId !== undefined before parsing. The earlier finding remains unresolved.
  • Resolved: The PR description now includes a representative real-CLI verification record after npm run build. It names an older commit than the current head, which the review rules explicitly treat as a note, not a blocking finding.

No other confident findings in the scoped diff.

@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Port 0 is now accepted and covered by the parser test. A built-CLI run confirms that the dashboard starts with --port 0, while invalid port text still exits with the flag-specific validation error. report-scope-reuse.test.ts passes on this head (run without the OpenCode global setup).

This comment was drafted with Claude Code.

@SaulMoro SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was generated by AI during triage.

Thanks, --port 0 is fixed. At 0e2faa4b it starts and listens on an OS-assigned port, and report-scope-reuse.test.ts passes in E2E shard 1/3. One item is still open, so my request for changes stands.

[P1 blocking] bind-project --project-id "" still skips validation. The empty string is falsy, so src/index.ts:1547 passes undefined, and the command falls back to the interactive picker without saying anything. I reproduced it on the built CLI: the output matches a run with no flag at all. The CI review raises the same point. Rejecting an invalid value is the purpose of this PR, so this case belongs here. Fix: check cmdOpts.projectId !== undefined before parsing, and add a test case for "".

[P3 nit] The new parser refuses more than 12abc. --port "", --port 1e3 and --project-id 0 all worked on main and are refused now. That is reasonable, but the body only mentions 12abc, so listing the others would help anyone whose script passes one of them.

A correction to your last comment: on main, dashboard --port abc does not exit 1. It keeps running, which is what the body says.

The parser duplication with #1781 is still a separate follow-up, and it doesn't block this PR.

Merge risk: medium

  • Alignment: decent. Non-numeric values are rejected, but "" still gets through for --project-id.
  • Impact: small. Two option parsers in two commands.
  • Verification: decent. CI is green at this head. The real-CLI record in the body is from an earlier commit, and the "" path has no run.
  • Rollback cost: low.
  • Error detection: decent. A refused value names its flag, but --project-id "" silently falls back to the picker.

… picker

An empty string is falsy, so bind-project skipped the parser and passed
undefined, which opened the interactive project picker. Parse whenever the
flag is given, so "" fails with the same error as other bad values.
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

No confident findings in the scoped diff.

  • Resolved: src/index.ts:1547 now checks cmdOpts.projectId !== undefined, so --project-id "" reaches validation and is rejected instead of falling back to the interactive picker.
  • Resolved: The PR description includes representative real-CLI verification after npm run build. The record names an older commit; the trusted review rules explicitly treat this as a note, not a blocking finding.

Review was read-only; no PR code was run, built, or installed.

@SaulMoro SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was generated by AI during triage.

Approving. I pushed 8791bd627 to fix the remaining item: bind-project now parses --project-id whenever the flag is given, so "" fails with the same error as other bad values instead of opening the picker. The --project-id parser test now covers "". Built CLI, against a local stand-in for the project service:

# before (PR head 0e2faa4b4, built dist/index.js)
$ node dist/index.js bind-project --project-id ""
ℹ Workspace enrolled for local-agent sync: /private/tmp/teamai-1782-run.RYYX1d/ws
ℹ 可用项目:
ℹ   1. Demo project - fixture [id=7]
ℹ 未绑定项目。
exit 0

# after (fix 8791bd627, built dist/index.js)
$ node dist/index.js bind-project --project-id ""
✖ --project-id must be an integer >= 1
exit 1

# valid id still binds (after)
$ node dist/index.js bind-project --project-id 7
✔ 已将工作区绑定到项目:Demo project [id=7]
exit 0

--port 0 (fixed earlier) and --port "" (refused) are unchanged. The parser duplication with #1781 is still a separate follow-up.

Merge risk: low

  • Alignment: good. Non-numeric and empty values are refused for both flags.
  • Impact: small. Two option parsers in two commands.
  • Verification: good. Built-CLI before and after, plus the parser tests.
  • Rollback cost: low.
  • Error detection: good. Every refused value names its flag.

@SaulMoro
SaulMoro merged commit f5ddc4c into Tencent:main Nov 8, 2026
18 checks passed
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.

3 participants