basic-memory icon indicating copy to clipboard operation
basic-memory copied to clipboard

feat(cli): add bm cloud share command group (create/list/update/revoke)

Open groksrc opened this issue 5 months ago • 2 comments

Closes #880

Summary

Adds a bm cloud share subcommand group that surfaces the cloud /api/shares endpoints. Four subcommands are implemented in src/basic_memory/cli/commands/cloud/shares.py:

  • create <project> <permalink> [--expires-at/-e] — POST /api/shares; prints share_url plus metadata on success.
  • list [--project/-p] — GET /api/shares (optional ?project_name= filter); displays a rich Table with Token / Project / Note / Enabled / Expires / Views / URL columns.
  • update <token> [--enable|--disable|--expires-at] — PATCH /api/shares/{token}; --expires-at none clears the expiry (sends null); rejects conflicting (--enable + --disable) or empty updates before hitting the network.
  • revoke <token> [--force/-f] — DELETE /api/shares/{token}; interactive confirmation prompt unless --force.

Design decisions

  • Dedicated revoke subcommand rather than a top-level flag or alias, matching the issue's recommended minimal surface.
  • Requires exact permalink — no friendly-title resolution (per triage note).
  • update exposes --enable/--disable/--expires-at because the PATCH endpoint and UpdateShareRequest (with model_fields_set semantics) support all three toggles.
  • Reuses make_api_request(), ConfigManager().config.cloud_host, and SubscriptionRequiredError/CloudAPIError handling consistent with snapshot.py.
  • Registered in cloud/__init__.py via cloud_app.add_typer(share_app, name="share").

Files changed (4 files, +1001 lines)

File Change
src/basic_memory/cli/commands/cloud/shares.py New — full command implementation
src/basic_memory/cli/commands/cloud/__init__.py Register share_app
src/basic_memory/cli/commands/cloud/schemas.py n/a (schemas dropped — see review notes)
tests/cli/test_share_commands.py 21 new tests

Testing

  • 21 share-command tests covering success paths, error/404/subscription paths, payload assertions, --expires-at parsing/validation, enable/disable conflict and empty-update guards, and revoke confirmation cancel.
  • 43 tests pass across share + snapshot + workspace CLI suites.
  • ruff check/ruff format and ty typecheck clean on changed files.
  • API contract verified against basic-memory-cloud source (CreateShareRequest, UpdateShareRequest, DELETE → 204, project_name query param).

Review notes

  1. Dead code (schemas): PublicShareResponse and PublicShareListResponse Pydantic schemas were added to src/.../cloud/schemas.py but are never imported or used by shares.py, which parses all responses with raw data.get(...) dicts. By contrast snapshot.py both defines and uses its schema (BucketSnapshotBrowseResponse.model_validate(...)). Either wire the schemas into response parsing (more type-safe, matches house style preferring Pydantic at boundaries) or drop them. — Update: they were dropped in the fix commit; this note is here for visibility in case the reviewer prefers them wired in instead.

  2. URL encoding in list: The query string is built via an unencoded f-string ?project_name={project}, so a project name with spaces/special chars would produce a malformed URL. This matches the existing snapshot.py convention (?prefix={prefix}), so it is consistent with the repo and not a regression, but using httpx params= or urllib.parse.quote would be more correct.

  3. API contract verified accurate: CreateShareRequest (project_name / note_permalink / expires_at), UpdateShareRequest (enabled / expires_at, with model_fields_set semantics so sending "expires_at": null correctly clears it), DELETE returns 204, list filter via project_name Query param. The --expires-at none → null clear path is correct.

  4. Coverage gate: Could not reproduce the repo's 100% coverage gate locally (cov + beartype circular-import env issue unrelated to this change), but branch coverage looks complete by inspection. All 21 new tests exercise distinct paths.


🤖 Generated with Claude Code

groksrc avatar Jun 11 '26 06:06 groksrc

Live prod QA found that bm cloud share ... --workspace <slug> failed with an opaque 400, while the same command with the org tenant UUID succeeded end-to-end. Root cause: resolve_configured_workspace returns the explicit --workspace value verbatim, but the cloud's X-Workspace-ID resolver only accepts a workspace/tenant UUID — yet users only ever see slugs and display names (in list-workspaces output and memory:// URLs), so the natural input never routed.

This commit resolves the workspace header client-side in the share commands:

  • A UUID is forwarded verbatim (covers per-project config workspace_id and the default chain, zero extra API calls).
  • Any other value is treated as a human identifier and mapped to the tenant UUID via a single get_available_workspaces lookup, matching with slug > tenant_id > name precedence (mirroring #979).
  • Ambiguous identifiers (a display name / slug matching >1 workspace) and unknown identifiers fail fast with clear errors that name the candidate / available workspace slugs, so users can self-serve.
  • The lookup happens once per invocation, only when the resolved value isn't already a UUID.
  • The list command now re-raises typer.Exit ahead of its broad handler, so these resolution errors aren't re-wrapped as "Unexpected error" (the double-error bug caught earlier on this branch).

--workspace help text updated to state it accepts a workspace slug, display name, or tenant ID. Scope kept to shares.py + its tests; resolve_configured_workspace and pull/push semantics untouched. Gates: pytest tests/cli/test_share_commands.py (30 passed), ruff check/format --check clean, ty check src tests test-int clean.

🤖 Generated with Claude Code

groksrc avatar Jun 11 '26 20:06 groksrc

This is good to go, but it needs to ship in v0.next and we have a maintenance release ahead of it.

groksrc avatar Jun 12 '26 01:06 groksrc