Skip to content

fix(cli): reject empty --terminal/--from handles instead of falling back to defaults - #16745

Merged
nwparker merged 1 commit into
stablyai:mainfrom
Cunninger:fix/empty-terminal-handle
Oct 26, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
Cunninger:fix/empty-terminal-handle

Conversation

@Cunninger

@Cunninger Cunninger commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When you tell the Orca CLI which terminal to act on, you can pass an empty name by accident, for example from a script variable that never got set (--terminal "$HANDLE"). Before this fix, Orca treated the empty name as "use whatever terminal is active" without saying anything, so it could read, close, or type into the wrong terminal. Now Orca stops and reports that the value is missing.

What Changed

  • Before: orca terminal send|read|show|wait|close --terminal "" exited 0 and acted on the worktree's active pane, which can belong to another tab or agent. orchestration check|inbox|worker-start --terminal "" and orchestration send|ask --from "" fell back to the ambient ORCA_TERMINAL_HANDLE identity.
  • After: each of these commands fails with invalid_argument (Missing value for --terminal / --from), exits 1, and sends no RPC. Leaving the flag out still resolves the default, the same as before.
  • Mechanism: getOptionalStringFlag returns undefined for '', so callers couldn't tell an empty value from an omitted flag. The codebase already had two private copies of getPresentStringFlag (omitted → undefined, present but empty or valueless → invalid_argument) in worktree.ts and worktree-project-target.ts. This PR moves that helper into src/cli/flags.ts, removes both copies, and uses it in:
    • getTerminalHandle (selectors.ts), which covers every terminal verb that takes --terminal
    • resolveOrchestrationTerminalHandle (terminal-identity.ts), which covers --terminal/--from for check/send/ask and the related commands
    • inbox --terminal (message-inbox-handlers.ts)
    • worker-start --terminal (worker-launch-handler.ts). The flag is now read at the top of the handler, so the command fails before the status.get capability RPC.

Why

An empty handle is almost always a script bug. Quietly retargeting the active pane can send input to the wrong agent or close the wrong terminal. Failing closed is the safe default. We reuse the existing helper so all handle-like flags behave the same and there is no new parallel helper. The check runs only in the CLI before any RPC, so it adds no protocol or mixed-version runtime concerns.

Linked Issue

Fixes #16694

Visual Proof

N/A: this is a CLI argument-validation change with no UI. Behavioral proof comes from a probe test that ran main() across 9 commands with an empty --terminal/--from and ORCA_TERMINAL_HANDLE set:

  • On origin/main, all 9 went ahead. Terminal verbs called worktree.list + terminal.resolveActive, the orchestration verbs called orchestration.inbox/check/send/ask against the ambient identity, and worker-start called terminal.resolveIdentity.
  • On this branch, all 9 exit 1 with invalid_argument and make zero RPC calls.

Testing

  • Reproduced on main with two new regression tests, then confirmed they pass with the fix:

    • src/cli/index-terminal-commands.test.ts: terminal send --terminal "" --text x --json returns invalid_argument and makes no RPC.
    • src/cli/handlers/orchestration-check-identity.test.ts: orchestration check --terminal "" with ORCA_TERMINAL_HANDLE set rejects without calling orchestration.check.
  • With main's source and the new tests: 2 failed. With this branch: all pass.

  • Targeted vitest after landing on the latest main (orchestration-check-identity, index-terminal-commands, worktree-project-target, handlers/orchestration): 23 files / 196 tests pass. Earlier full pnpm test src/cli: 183 files, 2110 passed.

  • oxlint, ast-grep, and oxfmt pass on the touched files (pre-commit hooks).

  • Platforms: macOS (vitest). The change only touches CLI flag parsing. On PowerShell 5.1, "" argv can be dropped, which turns --terminal into a valueless flag, and that was already rejected. Remote/SSH: the check runs on the client before any RPC, so older and newer runtimes are unaffected. Older CLI binaries keep the old behavior until upgraded. Folder workspaces are unaffected.

  • Tradeoff: scripts that passed --terminal "" or --from "" to mean "use the default" must now omit the flag.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

Review

Reviewed by Claude and Codex (both approve, low risk). The reviewer cleanups are included: one shared getPresentStringFlag instead of a new helper, the standard Missing value for --X wording, and the worker-start ordering fix.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Security: fails closed. Cross-platform: argv parsing only. Remote SSH: client-side only. Mobile: no impact. Backwards compatibility: only explicit empty values change behavior. Performance: none.

Original fix by @Cunninger in #16745 (authorship preserved via Co-authored-by).

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Walkthrough

The change adds getPresentStringFlag to the shared CLI flags module and replaces local or optional-string flag reads in terminal, orchestration, and worktree command paths. The accessor returns undefined when a flag is absent and rejects a present empty value with invalid_argument. Tests verify empty terminal values are rejected without downstream calls in the tested paths.

Priority: Normal

Severity of issue fixed: Medium

Merge Risk: Low · up to 086a3

A narrow worker-start input path may report a capability error instead of invalid_argument; validate --from before the probe.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 14 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Issue #16694 requires an explicit empty --terminal to fail instead of resolving the active pane, while omission keeps the documented fallback. getTerminalHandle now uses getPresentStringFlag; th…
Out of Scope Changes check Passed The changes remain related to the empty-handle failure mode in issue #16694. The shared flag helper, terminal command tests, and orchestration --terminal/--from checks apply the same fail-closed v…
Title check Passed The title clearly and concisely describes the main change: rejecting empty --terminal and --from handles instead of falling back to defaults.
Description check Passed The description is complete and follows the repository template. It explains the user impact, implementation, rationale, linked issue, testing, platform considerations, compatibility, and checklist st…
Full details: Linked Issues check

Explanation

Issue #16694 requires an explicit empty --terminal to fail instead of resolving the active pane, while omission keeps the documented fallback. getTerminalHandle now uses getPresentStringFlag; the helper returns undefined only when the flag is absent and raises invalid_argument for empty or valueless values. The validation occurs before the runtime call. The reported tests cover show, read, wait, and send, omitted flags, whitespace values, valueless flags, and help text.

Full details: Out of Scope Changes check

Explanation

The changes remain related to the empty-handle failure mode in issue #16694. The shared flag helper, terminal command tests, and orchestration --terminal/--from checks apply the same fail-closed validation. The worktree helper removal is a supporting deduplication change. No unrelated feature change is established.

Full details: Description check

Explanation

The description is complete and follows the repository template. It explains the user impact, implementation, rationale, linked issue, testing, platform considerations, compatibility, and checklist status.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Cunninger

Copy link
Copy Markdown
Contributor Author

Follow-up for the CodeRabbit merge-risk note: empty --terminal / --from on orchestration commands (check, inbox, worker-start, and sender identity) now fail closed the same way as orca terminal *, instead of falling through to ORCA_TERMINAL_HANDLE or the worktree-active pane.

Pushed in 78fe7da.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7ca79586-7d9b-4a15-9229-53b8e26adf99

📥 Commits

Reviewing files that changed from the base of the PR and between 7ee847f and 78fe7da.

📒 Files selected for processing (8)
  • src/cli/flags.test.ts
  • src/cli/flags.ts
  • src/cli/handlers/orchestration-check-identity.test.ts
  • src/cli/handlers/orchestration/message-check-handler.ts
  • src/cli/handlers/orchestration/message-inbox-handlers.ts
  • src/cli/handlers/orchestration/terminal-identity.ts
  • src/cli/handlers/orchestration/worker-launch-handler.ts
  • src/cli/selectors.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Comment on lines +56 to +60
terminal: getOptionalPresentStringFlag(
flags,
'terminal',
'--terminal requires a non-empty handle. Omit --terminal to launch in a new terminal.'
),

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Validate terminal before the capability probe.

When --model or --effort is present, status.get at Line 21 runs before this helper. An invocation such as worker-start --terminal "" --model ... therefore contacts the runtime and may return a capability or connection error instead of the required local invalid_argument. Read this flag before the capability check, then pass the stored value to orchestration.workerStart.

…ack to defaults

An explicit empty --terminal (or orchestration --from) was treated as absent,
so `orca terminal send --terminal ""` targeted the active pane and
orchestration check/inbox/worker-start fell back to the ambient
ORCA_TERMINAL_HANDLE mailbox. Hoist the existing getPresentStringFlag into
flags.ts and use it for these handles so a present-but-empty value fails with
invalid_argument.

Fixes stablyai#16694

Co-authored-by: wusiyuan05 <[email protected]>
@nwparker
nwparker force-pushed the fix/empty-terminal-handle branch from 78fe7da to 086a377 Compare October 26, 2026 13:13
@nwparker nwparker changed the title fix(cli): fail closed on empty --terminal handle (#16694) fix(cli): reject empty --terminal/--from handles instead of falling back to defaults Oct 26, 2026

@coderabbitai coderabbitai Bot 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.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

Minor · Reject an invalid --from before the model/effort capability… · worker-launch-handler.ts:66-70

src/cli/handlers/orchestration/worker-launch-handler.ts:66-70
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Reject an invalid --from before the model/effort capability probe.

When worker-start receives --model or --effort, it calls status.get before it validates --from. An explicit empty or valueless --from is rejected only after that call. A runtime that lacks the capability then reports incompatible_runtime, so the user does not see the invalid_argument error that the flag contract requires. No worker is launched, because the rejection happens before the mutation call is made.

Suggested fix
     const terminal = getPresentStringFlag(flags, 'terminal')
+    getPresentStringFlag(flags, 'from')
     const model = getOptionalStringFlag(flags, 'model')
     const effort = getOptionalStringFlag(flags, 'effort')

Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: adca8d38-6b17-4bff-8279-31aff5a520e7
Commits

Reviewing files that changed from the base of the PR and between 78fe7da and 086a377.

Files selected for processing: 9
  • src/cli/flags.ts
  • src/cli/handlers/orchestration-check-identity.test.ts
  • src/cli/handlers/orchestration/message-inbox-handlers.ts
  • src/cli/handlers/orchestration/terminal-identity.ts
  • src/cli/handlers/orchestration/worker-launch-handler.ts
  • src/cli/handlers/worktree.ts
  • src/cli/index-terminal-commands.test.ts
  • src/cli/selectors.ts
  • src/cli/worktree-project-target.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

@nwparker
nwparker merged commit d290035 into stablyai:main Oct 26, 2026
38 checks passed
@Pr1p Pr1p mentioned this pull request Nov 5, 2026
7 of 9 tasks
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.

[Bug]: empty --terminal handle is accepted and silently targets the worktree-active pane

2 participants