Skip to content

fix(cli): resolve --instance symmetrically (saved key OR URL) - #202

Merged
lilyshen0722 merged 1 commit into
mainfrom
fix-cli-instance-asymmetry
Apr 16, 2026
Merged

fix(cli): resolve --instance symmetrically (saved key OR URL)#202
lilyshen0722 merged 1 commit into
mainfrom
fix-cli-instance-asymmetry

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Summary

  • `resolveInstanceUrl` treated `--instance` as a URL; `getToken` treated it as a config key. Neither form worked end-to-end
  • `--instance https://api-dev.commonly.me\` → URL OK, token null → 401
  • `--instance dev` → token OK, URL literally "dev" → ENOTFOUND

Filed as a follow-up during ADR-005 Phase 1b live smoke.

Fix

New `resolveInstance(identifier)` helper that both `resolveInstanceUrl` and `getToken` share. Resolution order:

  1. URL-shaped (`http[s]://`) → saved-URL lookup (case-insensitive, trailing-slash tolerant) → if no match, return as raw URL with null token (needed for `login` bootstrap)
  2. Otherwise → saved-key lookup
  3. Null identifier → `config.active`

URL-first ordering prevents a hypothetical key named `http-backup` from colliding with real URLs.

Test plan

  • 21/21 lib.test.mjs passing (up from 15 — 6 new tests including the collision ordering and case-insensitive URL match)
  • Full CLI suite 61/61 passing
  • CI green

🤖 Generated with Claude Code

Historically `resolveInstanceUrl` treated --instance as a URL and
`getToken` treated it as a config key. So:

  commonly whoami --instance https://api-dev.commonly.me
  → URL resolved correctly, but token lookup returned null (no auth)

  commonly whoami --instance dev
  → token found, but "dev" was returned literally as the URL (ENOTFOUND)

Neither form worked end-to-end. Filed as a follow-up during ADR-005
Phase 1b smoke.

Fix: funnel both functions through a new `resolveInstance(identifier)`
helper that probes URL-shaped inputs (http[s]://) against saved URLs
first (case-insensitive, trailing-slash tolerant), then falls back to
exact-key lookup, then to an unknown-URL bootstrap, then to null. Both
forms of --instance now work for both URL and token resolution.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@lilyshen0722
lilyshen0722 merged commit a7135f3 into main Apr 16, 2026
8 checks passed
@lilyshen0722
lilyshen0722 deleted the fix-cli-instance-asymmetry branch April 16, 2026 05:09
lilyshen0722 added a commit that referenced this pull request Apr 16, 2026
…1 as shipped

Sweep of documentation catching up with today's merges:

- ADR-003: status line now includes Phase 3 §Deliverable 3 shipped
  (2026-04-15). Added revision note pointing at the cross-check test
  file and its seven assertions. Deliverable 3 itself marked shipped
  inline.

- ADR-005: status bumped from Draft to Accepted. Revision history
  records Phase 1a (PR #194), Phase 1b (PR #195), and the three
  follow-up fixes that landed during/after live smoke (cwd guard +
  --instance shadowing #196, self-mention loop #201, URL/key
  asymmetry #202). Phase 1a + 1b headers marked shipped inline.

- ADR-006: status bumped from Draft to Accepted. Revision history
  names Phase 1 (PR #197), the Cloudflare UA follow-up
  (5db9376), and the live-smoke result on api-dev. Open Question
  #1 (ephemeral GC) updated with current TODO location.

- CLAUDE.md Agent Runtime Quick Rules: five new prescriptive rules
  — self-mention guard, `commonly agent init` self-serve path,
  Python SDK User-Agent requirement, CLI --instance symmetry.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
lilyshen0722 added a commit that referenced this pull request Apr 16, 2026
…ipped (#203)

* fix(cli): resolve --instance symmetrically (saved key OR URL)

Historically `resolveInstanceUrl` treated --instance as a URL and
`getToken` treated it as a config key. So:

  commonly whoami --instance https://api-dev.commonly.me
  → URL resolved correctly, but token lookup returned null (no auth)

  commonly whoami --instance dev
  → token found, but "dev" was returned literally as the URL (ENOTFOUND)

Neither form worked end-to-end. Filed as a follow-up during ADR-005
Phase 1b smoke.

Fix: funnel both functions through a new `resolveInstance(identifier)`
helper that probes URL-shaped inputs (http[s]://) against saved URLs
first (case-insensitive, trailing-slash tolerant), then falls back to
exact-key lookup, then to an unknown-URL bootstrap, then to null. Both
forms of --instance now work for both URL and token resolution.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* docs(adr): mark ADR-003 Phase 3, ADR-005 Phases 1a+1b, ADR-006 Phase 1 as shipped

Sweep of documentation catching up with today's merges:

- ADR-003: status line now includes Phase 3 §Deliverable 3 shipped
  (2026-04-15). Added revision note pointing at the cross-check test
  file and its seven assertions. Deliverable 3 itself marked shipped
  inline.

- ADR-005: status bumped from Draft to Accepted. Revision history
  records Phase 1a (PR #194), Phase 1b (PR #195), and the three
  follow-up fixes that landed during/after live smoke (cwd guard +
  --instance shadowing #196, self-mention loop #201, URL/key
  asymmetry #202). Phase 1a + 1b headers marked shipped inline.

- ADR-006: status bumped from Draft to Accepted. Revision history
  names Phase 1 (PR #197), the Cloudflare UA follow-up
  (5db9376), and the live-smoke result on api-dev. Open Question
  #1 (ephemeral GC) updated with current TODO location.

- CLAUDE.md Agent Runtime Quick Rules: five new prescriptive rules
  — self-mention guard, `commonly agent init` self-serve path,
  Python SDK User-Agent requirement, CLI --instance symmetry.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
lilyshen0722 added a commit that referenced this pull request Apr 16, 2026
New docs/cli/README.md — single-page user guide covering:

- Quick start: two-command flow to attach `claude` to a pod; scaffolder
  flow for custom Python agents
- Full command reference, grouped by concept (auth, agents local-CLI,
  agents webhook-SDK, agents shared, pods, dev)
- Config layout: ~/.commonly/config.json + tokens/ + sessions/
- Environment variables
- --instance URL/key symmetry (PR #202)
- Common workflows (attach claude, write a Python bot, tail a pod,
  test against local dev)
- Troubleshooting (revoked token, spawn ENOENT, Cloudflare UA)

Also:
- cli/README.md — one-page pointer to the full guide + quick run-from-source
  and test instructions
- docs/README.md index — adds cli/ row to the directory table and a
  prominent Getting Started link

No code changes. All content is derived from the current shipped
behaviour verified against cli/src/ and docs/agents/.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
lilyshen0722 added a commit that referenced this pull request Apr 16, 2026
New docs/cli/README.md — single-page user guide covering:

- Quick start: two-command flow to attach `claude` to a pod; scaffolder
  flow for custom Python agents
- Full command reference, grouped by concept (auth, agents local-CLI,
  agents webhook-SDK, agents shared, pods, dev)
- Config layout: ~/.commonly/config.json + tokens/ + sessions/
- Environment variables
- --instance URL/key symmetry (PR #202)
- Common workflows (attach claude, write a Python bot, tail a pod,
  test against local dev)
- Troubleshooting (revoked token, spawn ENOENT, Cloudflare UA)

Also:
- cli/README.md — one-page pointer to the full guide + quick run-from-source
  and test instructions
- docs/README.md index — adds cli/ row to the directory table and a
  prominent Getting Started link

No code changes. All content is derived from the current shipped
behaviour verified against cli/src/ and docs/agents/.

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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