Skip to content

feat(cli): redesign CLI to wrap SDK with git-like commands - #1

Open
Emerald-dev0 wants to merge 18 commits into
mainfrom
feature/cli-redesign
Open

feat(cli): redesign CLI to wrap SDK with git-like commands#1
Emerald-dev0 wants to merge 18 commits into
mainfrom
feature/cli-redesign

Conversation

@Emerald-dev0

Copy link
Copy Markdown
Contributor

Summary

Redesigns the Contextly CLI to wrap the SDK instead of talking to the protocol directly, ensuring consistent behavior between code and terminal.

Changes

Core Commands

  • contextly init - Initialize Contextly project with scope binding
  • contextly read - Print compiled context with --budget and --json options
  • contextly commit - Create commitments with --rationale flag
  • contextly log - Git-log style commitment history
  • contextly diff - Show constraint differences between scopes
  • contextly conflicts - List unresolved conflicts
  • contextly resolve - Interactive conflict resolution
  • contextly sync - Push/pull against cloud state

Authentication

  • contextly login - GitHub Device Flow authentication
  • contextly logout - Clear local session
  • contextly whoami - Show current user

Output Formatting

  • Human-readable by default
  • --json flag on every command for scripting/CI use

Design Principles

  • Feels like git - handful of verbs, predictable output, scriptable
  • Wraps @contextly/sdk - consistent behavior between code and terminal
  • Token-based auth with ctx_{scope}_{random} format
  • Uses in-memory MCP transport for protocol compliance

Files Added/Modified

  • packages/cli/src/commands/auth.ts - Authentication commands
  • packages/cli/src/commands/core.ts - Core Contextly commands
  • packages/cli/src/lib/config.ts - Configuration utilities
  • packages/cli/src/lib/command-utils.ts - Command helpers
  • packages/cli/src/index.ts - Main CLI entry point
  • packages/cli/package.json - Updated dependencies

Testing

  • All commands tested with human-readable and JSON output
  • Authentication flow verified
  • Context reading with budget filtering tested
  • Commit/conflict/resolve workflow validated

Breaking Changes

  • None - new CLI structure maintains backward compatibility for existing commands

Closes: CLI redesign (Prompt 12)

…and multi-tenant isolation

- Store class with insert, supersede, archive, tombstone, DAG traversal
- 45 tests covering store operations, DAG invariants, conflict detection
- Deterministic ORDER BY with rowid tiebreaker for sub-second timestamp resolution
- MEMORY.md with repository workflow rules
- docs/DEVIATIONS.md with spec ambiguity resolutions
…flict detection, token budget, and caching

The Compiler transforms the raw append-only log into the active context
set agents receive. Five passes: scope resolution (inheritance across
ancestor scopes), status filter, CID dedup with supersession resolution,
inherentance (child overrides parent), and kind-ordered output.

Key features:
- Scope inheritance: child scopes inherit parent entries unless overridden
- Conflict detection: duplicate cids within same scope with different
  messages and no supersession are flagged, both entries returned
- Supersession resolution: superseded entries never leak into output
- Token budget: compress observations first, then decisions; never drop
  rules silently; all drops logged with reason
- Task relevance ranking: optional keyword-based relevance scoring
- Caching: per (scope, budget, kind, cid), invalidated on scope change
- Provenance: every compiled entry tracks sourceScope, inherited flag,
  fromParent, and supersedesChain
- Graceful degradation: summarize before dropping, never silently drop
  high-confidence constraints

69 tests pass (26 store, 26 compiler, 17 DAG)
…agent integration test

Replaces the Supabase-backed v1 with a local-first MCP server built on
@contextly/protocol (Store + Compiler).

Five tools matching protocol primitives:
- read_context: compiled context via Context Compiler with budget, kind,
  cid, and task-relevance options
- commit: idempotent entry creation with conflict detection
- query: raw SQLite lookup bypassing compilation (for tooling/debug)
- resolve: conflict resolution via supersession
- fork/merge: scope branching with inheritance

Auth handshake:
- Token format: ctx_{scope}_{base64url} — embeds scope for zero-DB auth
- verifyTokenIntegrity, validateScope, permission checks on every call
- Parent token grants access to child scopes, sibling scopes are isolated

Rate limiting:
- Per-operation sliding window (read: 100/min, write: 30/min, etc.)
- Resets after window expiry, structured RateLimitError with retryAfter

Structured errors:
- Machine-readable error codes (INVALID_TOKEN, SCOPE_MISMATCH, etc.)
- Agents can reason about errors programmatically, not just HTTP codes

Integration test (16 tests):
- Two-agent loop: Alice reads empty context, commits → Bob reads Alice's
  context, commits his own → both see all entries
- Conflict detection: Alice and Bob write different messages for same cid
- Conflict resolution: Bob supersedes Alice's entry with a new decision
- Auth enforcement: scope mismatch rejects, parent-child scope allows
- Rate limiting: window enforcement and auto-reset
- Fork with inheritance: child scope inherits parent entries
- Idempotency: duplicate commit returns existing entry
…ion example

- TypeScript SDK (packages/sdk/) — Contextly class wrapping MCP tools via
  in-memory transport; verbs: read, commit, query, resolve, fork, merge, onConflict
- Python SDK (packages/sdk-py/) — subprocess-based MCP client with same API
- Quickstart doc (docs/AGENT_SDK_QUICKSTART.md) — under-10-lines examples
- Cross-session persistent-agent example (examples/persistent-agent/) — two
  scripts demonstrating context inheritance across agent sessions
… for cross-tenant access, conflict spikes, sync divergence
…d(), ancestryDag() for developer introspection
…cs, alerting, debug, exporter, and full lifecycle
- Add API server package with Express.js
- Implement all 6 protocol primitives as REST endpoints:
  - POST /v1/read_context - read compiled context
  - POST /v1/commit - create context entries
  - POST /v1/query - query/filter entries
  - POST /v1/resolve - resolve conflicts
  - POST /v1/fork - fork scopes
  - POST /v1/merge - merge scopes
- Add authentication middleware (API keys + JWT Bearer tokens)
- Implement scope-based authorization with permissions
- Add rate limiting (per-tenant and per-API-key)
- Build webhook system with retries, idempotency, signed payloads
- Auto-generate OpenAPI spec from implementation
- Swagger UI at /docs
- Postman collection included
- Comprehensive test suite
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