Skip to content

feat(cli): ADR-005 Phase 1a — local-CLI wrapper attach + run skeleton - #194

Merged
samxu01 merged 1 commit into
mainfrom
adr-005-phase-1a
Apr 15, 2026
Merged

samxu01 merged 1 commit into
mainfrom
adr-005-phase-1a

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Summary

Smallest reviewable slice of ADR-005 — the local-CLI-wrapper driver. Ships commonly agent attach + commonly agent run end-to-end against a no-op stub adapter so the run loop is reviewable in isolation. Real CLI adapters (claude/codex/…) + the ADR-003 memory bridge land in Phase 1b.

What's in

  • cli/src/commands/agent.js — attach + run subcommands. Pure cores (performAttach, performRun, saveAgentToken, loadAgentToken) exported for testability.
  • cli/src/lib/session-store.js — per-(agent, pod) session IDs at ~/.commonly/sessions/<agent>.json. One file per agent so two commonly agent run processes for different agents don't race.
  • cli/src/lib/adapters/stub.js + adapters/index.js — no-op reference adapter + tiny registry. Adding claude in Phase 1b is a one-line change in index.js.
  • cli/__tests__/attach.test.mjs + run-loop.test.mjs — 14 new tests. Mocks createClient + os.homedir (same pattern as existing lib.test.mjs). Covers attach flow, token-file round trip, happy-path spawn/post/ack, chat-type filtering, no-pod no_action, adapter-failure re-delivery (no ack), session-id continuity, stop() semantics, clearSessions.
  • docs/adr/ADR-005 — clarified serialization scope (per run process) and per-agent session-file layout; closed Open Question Add basic unit tests for backend functions #1.

What's NOT in (by design)

  • Memory bridge (Phase 1b per ADR-005 §Migration path).
  • Real CLI adapters — only stub exists.
  • Backend awareness of runtimeType: 'local-cli' — install stores config.runtime as an opaque blob per ADR-001; no backend change needed for Phase 1a.
  • At-least-once idempotency via event-id dedup (ADR-005 invariant Add message controller tests #7) — parked; Phase 1a relies on kernel re-delivery only on adapter failure.

Reviewer-pass findings addressed before commit

  • Critical: null-pod events now skip spawn entirely (was acked as posted without posting).
  • Important: publish errors now logged, not silently swallowed.
  • Important: session store switched to per-agent files to close the concurrent-write race.
  • Important: extractPrompt now filters on CHAT_EVENT_TYPES so a heartbeat with a stray content field can't trigger a spawn.
  • Nit: cwd uses os.tmpdir() instead of hardcoded /tmp; Phase-1b TODO comment now cites the ADR section.

Test plan

  • cd cli && npm test — 28/28 passing (3 suites)
  • npm run lint from repo root — 0 new errors in cli/
  • Live against api-dev.commonly.me (attach stub + run, verify token file + ack loop) — deferred to Phase 1b when a real adapter exists to spawn

🤖 Generated with Claude Code

Adds the smallest reviewable slice of the local-CLI wrapper driver: attach +
run subcommands, a per-(agent, pod) session store, a stub adapter, and tests.
No real CLI adapters or memory bridge yet — those land in Phase 1b.

- cli/src/commands/agent.js: attach + run subcommands; pure cores
  performAttach/performRun/saveAgentToken/loadAgentToken exported for tests.
- cli/src/lib/session-store.js: per-agent session files at
  ~/.commonly/sessions/<agent>.json (one file per agent — no cross-agent
  race; ADR-005 §Session continuity updated to match).
- cli/src/lib/adapters/index.js + adapters/stub.js: registry + no-op adapter.
- cli/__tests__/attach.test.mjs + run-loop.test.mjs: mocked-client coverage
  for happy path, no-prompt / no-pod no_action, adapter-failure re-delivery
  (no ack), session-id continuity, stop() semantics, and clearSessions.
- docs/adr/ADR-005: clarified serialization scope (per-run-process) and
  per-agent session file layout.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@samxu01
samxu01 merged commit 97b8b27 into main Apr 15, 2026
8 checks passed
lilyshen0722 pushed a commit that referenced this pull request Apr 15, 2026
Builds on Phase 1a (#194). Wires the first real CLI adapter and the
ADR-003 memory bridge into the local-CLI wrapper's run loop.

- cli/src/lib/adapters/claude.js: subprocess wrapper for the `claude`
  CLI. detect() runs `claude --version` (+ best-effort `which` for path
  display). spawn() builds argv `-p <prompt> --output-format text
  --session-id <sid>`, prepends the §Memory bridge preamble when
  ctx.memoryLongTerm is non-empty, mints a UUID on first turn and returns
  it as newSessionId so the run loop persists it, 5-min SIGTERM timeout.
  Test seam: `ctx._spawnImpl` swaps out child_process.spawn for unit
  tests — ADR-005 §Adapter pattern now documents the pattern so future
  adapters don't each invent a different one.
- cli/src/lib/memory-bridge.js: two CAP shims the run loop calls around
  every spawn. readLongTerm() extracts sections.long_term.content from
  GET /memory (returns '' for 404/network errors; non-404 HTTP errors
  surface via onError instead of swallowing). syncBack() POSTs /memory/sync
  with mode:'patch', sourceRuntime:'local-cli', content+visibility ONLY
  per ADR-003 invariant #9. No-op on empty summary.
- cli/src/commands/agent.js: performRun now calls readLongTerm before
  spawn → injects ctx.memoryLongTerm; if adapter returns memorySummary,
  calls syncBack after post. Sync failure is non-fatal (pod message
  already posted; original err preserved via `{ cause }` for CLI debug).
- Tests: 3 new suites — adapters.claude.test.mjs (detect + spawn argv +
  preamble + timeout), memory-bridge.test.mjs (including the 404-vs-non-404
  split), run-loop.test.mjs extended to verify end-to-end memory wiring.
  52/52 passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
samxu01 pushed a commit that referenced this pull request Apr 15, 2026
Builds on Phase 1a (#194). Wires the first real CLI adapter and the
ADR-003 memory bridge into the local-CLI wrapper's run loop.

- cli/src/lib/adapters/claude.js: subprocess wrapper for the `claude`
  CLI. detect() runs `claude --version` (+ best-effort `which` for path
  display). spawn() builds argv `-p <prompt> --output-format text
  --session-id <sid>`, prepends the §Memory bridge preamble when
  ctx.memoryLongTerm is non-empty, mints a UUID on first turn and returns
  it as newSessionId so the run loop persists it, 5-min SIGTERM timeout.
  Test seam: `ctx._spawnImpl` swaps out child_process.spawn for unit
  tests — ADR-005 §Adapter pattern now documents the pattern so future
  adapters don't each invent a different one.
- cli/src/lib/memory-bridge.js: two CAP shims the run loop calls around
  every spawn. readLongTerm() extracts sections.long_term.content from
  GET /memory (returns '' for 404/network errors; non-404 HTTP errors
  surface via onError instead of swallowing). syncBack() POSTs /memory/sync
  with mode:'patch', sourceRuntime:'local-cli', content+visibility ONLY
  per ADR-003 invariant #9. No-op on empty summary.
- cli/src/commands/agent.js: performRun now calls readLongTerm before
  spawn → injects ctx.memoryLongTerm; if adapter returns memorySummary,
  calls syncBack after post. Sync failure is non-fatal (pod message
  already posted; original err preserved via `{ cause }` for CLI debug).
- Tests: 3 new suites — adapters.claude.test.mjs (detect + spawn argv +
  preamble + timeout), memory-bridge.test.mjs (including the 404-vs-non-404
  split), run-loop.test.mjs extended to verify end-to-end memory wiring.
  52/52 passing.

Co-authored-by: Lily Shen <lilyshen20021002@gmail.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
…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>
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.

2 participants