feat(cli): add bm cloud share command group (create/list/update/revoke)
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; printsshare_urlplus 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 noneclears the expiry (sendsnull); 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
revokesubcommand 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).
updateexposes--enable/--disable/--expires-atbecause the PATCH endpoint andUpdateShareRequest(withmodel_fields_setsemantics) support all three toggles.- Reuses
make_api_request(),ConfigManager().config.cloud_host, andSubscriptionRequiredError/CloudAPIErrorhandling consistent withsnapshot.py. - Registered in
cloud/__init__.pyviacloud_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-atparsing/validation, enable/disable conflict and empty-update guards, and revoke confirmation cancel. - 43 tests pass across share + snapshot + workspace CLI suites.
ruff check/ruff formatandty typecheckclean on changed files.- API contract verified against
basic-memory-cloudsource (CreateShareRequest,UpdateShareRequest, DELETE → 204,project_namequery param).
Review notes
-
Dead code (schemas):
PublicShareResponseandPublicShareListResponsePydantic schemas were added tosrc/.../cloud/schemas.pybut are never imported or used byshares.py, which parses all responses with rawdata.get(...)dicts. By contrastsnapshot.pyboth 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. -
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 existingsnapshot.pyconvention (?prefix={prefix}), so it is consistent with the repo and not a regression, but usinghttpx params=orurllib.parse.quotewould be more correct. -
API contract verified accurate:
CreateShareRequest(project_name / note_permalink / expires_at),UpdateShareRequest(enabled / expires_at, withmodel_fields_setsemantics so sending"expires_at": nullcorrectly clears it), DELETE returns 204, list filter viaproject_nameQuery param. The--expires-at none→ null clear path is correct. -
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
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_idand 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_workspaceslookup, 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
listcommand now re-raisestyper.Exitahead 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
This is good to go, but it needs to ship in v0.next and we have a maintenance release ahead of it.