Skip to content

Auto-select target when exactly one target is discoverable #9

@V3RON

Description

@V3RON

Running a target-dependent command currently fails unless the user has manually selected a target first, even when discovery can only find a single valid target. This adds unnecessary friction to the common case where one app or runtime is available and the tool already has enough information to proceed safely.

This change is needed to make target-required workflows behave more like daemon startup: if the CLI can determine the only valid target at command time, it should select it automatically instead of forcing an extra target select step. That keeps the default workflow shorter without changing behavior when a user has already made an explicit selection.

Observed Findings

  • Target-required commands in packages/agent-cdp/src/cli.ts currently call ensureDaemon() and then send their command directly to the daemon.
  • When no target is selected, the daemon fails in packages/agent-cdp/src/daemon.ts with No target selected. Use \target select` first.`
  • Target discovery already exists in packages/agent-cdp/src/session-manager.ts and packages/agent-cdp/src/discovery.ts.
  • target list without --url uses the default auto-discovery flow in discoverTargets(options) with empty DiscoveryOptions, which fans out across DEFAULT_DISCOVERY_URLS.
  • target select <id> already resolves the target source URL from the encoded target id, so selection can reuse the discovered id without adding new CLI flags or daemon APIs.
  • The daemon persists the selected target, so once a target has been selected it should remain the source of truth and auto-selection should not run again.

Suggested Behavior

  • When a user runs any command that requires a selected target, the CLI should:
    1. ensure the daemon is running
    2. check daemon status
    3. if a target is already selected, continue normally
    4. if no target is selected, run the same discovery flow used by target list without --url
  • If discovery returns exactly one target, the CLI should auto-select it and continue with the original command.
  • If discovery returns multiple targets, the CLI should fail with this message:
Multiple targets available. Run 'agent-cdp target list' and 'agent-cdp target select <id>'.
  • Auto-selection must not run for target list, target select, target clear, start, stop, or status.
  • Auto-selection must not override an existing selected target.
  • The auto-selection flow should not introduce a separate discovery mechanism or require --url; it should reuse the existing default discovery behavior.

Implementation Plan

  1. Add a small helper in packages/agent-cdp/src/cli.ts for commands that require a selected target.
  2. In that helper:
    • call ensureDaemon()
    • call sendCommand({ type: \"status\" })
    • if selectedTarget exists, return immediately
    • otherwise call sendCommand({ type: \"list-targets\", options: {} })
    • if the result has exactly one target, call sendCommand({ type: \"select-target\", targetId: target.id, options: {} })
    • if the result has more than one target, throw Multiple targets available. Run 'agent-cdp target list' and 'agent-cdp target select <id>'.
    • if the result has zero targets, leave the current behavior in place unless the implementer intentionally improves that case in a separate issue or clearly scoped follow-up
  3. Replace plain await ensureDaemon() with the new helper only for commands that actually require a selected target.
  4. Leave target-management commands and commands that operate only on stored session or analysis data unchanged.

Commands that should switch to the helper based on current daemon behavior:

  • console list
  • console get
  • network start
  • network summary
  • network list
  • network request
  • network request-headers
  • network response-headers
  • network request-body
  • network response-body
  • trace start
  • memory capture
  • mem-snapshot capture
  • js-memory sample
  • js-allocation start
  • js-allocation-timeline start
  • js-profile start
  • js-profile stop

Commands that should remain on plain ensureDaemon():

  • start
  • stop
  • status
  • target list
  • target select
  • target clear
  • read-only commands that operate on stored sessions, snapshots, or profiler results without requiring an active target selection

Testing plan:

  1. Add CLI-level tests covering:
    • auto-select when status reports no selected target and discovery returns exactly one target
    • no auto-select when status already reports a selected target
    • the exact multi-target error message when discovery returns more than one target
    • auto-selection discovery using empty options so it matches target list without --url
  2. Verify that non-target commands do not trigger the helper.
  3. Run pnpm test and pnpm typecheck after implementation.

Resolution Summary

Target-dependent commands should automatically select a target when exactly one discoverable target exists, while preserving existing explicit target selection behavior. When multiple targets are available, the CLI should stop and tell the user to choose one manually using target list and target select.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions