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:
- ensure the daemon is running
- check daemon status
- if a target is already selected, continue normally
- 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
- Add a small helper in
packages/agent-cdp/src/cli.ts for commands that require a selected target.
- 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
- Replace plain
await ensureDaemon() with the new helper only for commands that actually require a selected target.
- 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:
- 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
- Verify that non-target commands do not trigger the helper.
- 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.
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 selectstep. That keeps the default workflow shorter without changing behavior when a user has already made an explicit selection.Observed Findings
packages/agent-cdp/src/cli.tscurrently callensureDaemon()and then send their command directly to the daemon.packages/agent-cdp/src/daemon.tswithNo target selected. Use \target select` first.`packages/agent-cdp/src/session-manager.tsandpackages/agent-cdp/src/discovery.ts.target listwithout--urluses the default auto-discovery flow indiscoverTargets(options)with emptyDiscoveryOptions, which fans out acrossDEFAULT_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.Suggested Behavior
target listwithout--urltarget list,target select,target clear,start,stop, orstatus.--url; it should reuse the existing default discovery behavior.Implementation Plan
packages/agent-cdp/src/cli.tsfor commands that require a selected target.ensureDaemon()sendCommand({ type: \"status\" })selectedTargetexists, return immediatelysendCommand({ type: \"list-targets\", options: {} })sendCommand({ type: \"select-target\", targetId: target.id, options: {} })Multiple targets available. Run 'agent-cdp target list' and 'agent-cdp target select <id>'.await ensureDaemon()with the new helper only for commands that actually require a selected target.Commands that should switch to the helper based on current daemon behavior:
console listconsole getnetwork startnetwork summarynetwork listnetwork requestnetwork request-headersnetwork response-headersnetwork request-bodynetwork response-bodytrace startmemory capturemem-snapshot capturejs-memory samplejs-allocation startjs-allocation-timeline startjs-profile startjs-profile stopCommands that should remain on plain
ensureDaemon():startstopstatustarget listtarget selecttarget clearTesting plan:
target listwithout--urlpnpm testandpnpm typecheckafter 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 listandtarget select.