ContextUnity CLI architectureπ
The contextunity package (packages/cli/src/contextunity/cli/) is the operator shell for local lifecycle orchestration, authentication, diagnostics, memory/documentation workflows, and typed remote administration across all five platform services.
Configuration layers (C0 β C3)π
Configuration resolution follows ADR-0002 across four strict layers (distinct from the L0βL4 payload type-boundary model in type-boundaries.md):
~/.contextunity/ (or --config_dir <path>, fallback /etc/contextunity/)
contextunity.yml C0 CLI/profile target & global defaults
services/
router.yml C0 Router instance config & feature flags
brain.yml C0 Brain storage, bulkhead & governance flags
worker.yml C0 Worker engine, actor flags & sandbox profiles
shield.yml C0 Shield vault, signer, effect & audit flags
forge.yml C0 Forge dashboard & librarian config| Layer | Carrier | Authority |
|---|---|---|
| C0 | ~/.contextunity/*.yml + explicit service env vars | Operator/deployment infrastructure, endpoints, secrets, budgets, and boolean feature flags |
| C1 | contextunity.project.yaml (contextunity/v1alpha8+) | Project identity (project.id), allowed_tenants, declared graphs, toolkits, and allowlisted profile references |
| C2 | router.graph.<id> | Graph-scoped topology, orchestration roles, and memory/trace bindings |
| C3 | router.graph.<id>.nodes[*] | Node-level model, prompt, and attenuated tool/secret permissions |
- INV-CLI-CONFIG-ZERO-NETWORK: Configuration loading via
get_cli_config(backed byload_service_config) performs zero network or mesh probes; runtime feature rollout uses explicit C0 boolean service flags, never--phase Nruntime switches.
Key symbols and inputs/outputsπ
| Symbol | Role | Inputs / Outputs |
|---|---|---|
app | Root Typer CLI entrypoint | Operator flags/subcommands -> local command actions (e.g., version, local up) or service-backed gRPC/SDK calls with command-specific effects |
get_cli_config | Network-free CLI config loader | Local YAML/env -> validated CliConfig |
LocalSupervisor | Foreground local stack manager | Local operator profile -> managed local Brain, Router, Worker, and Forge (when installed) processes; probes configured external Shield endpoint |
UserSystemdManager | Background systemd --user manager | Profile name -> user systemd unit lifecycle |
LocalTuiTargetResolver | Loopback TUI target resolver | Stored profile -> LocalTuiTarget(profile, router_url, token) (the TUI app instantiates ServiceHealthReader separately) |
CommandContract | Declarative CLI contract metadata | Static command metadata fields consumed by CLI authorization and target-resolution helpers |
Local lifecycle and startup modesπ
| Command | Process owner | Purpose |
|---|---|---|
contextunity local up [--profile] | Foreground LocalSupervisor (cli/local/) | Starts in-process/local Router (127.0.0.1:50050), Brain (127.0.0.1:50051, SQLite), Worker (127.0.0.1:50052, Huey), and Forge (127.0.0.1:8300 when installed), and probes any configured external Shield endpoint |
contextunity local start|status|stop --profile | UserSystemdManager (systemd --user) | Background profile-scoped local service lifecycle without PID-file races |
contextunity stack up|down|status --profile | Compose / external stack runner | Dependency-backed dev or prod-like multi-container stacks (PostgreSQL, Redis, Temporal, MinIO) |
contextunity tui | Loopback read-only Textual client | Attaches to local loopback services via LocalTuiTargetResolver and ServiceHealthReader, rendering redacted bounded journal logs without minting tokens |
- INV-CLI-OWNED-PROCESS-GROUPS: Local shutdown stops only process groups owned by that
LocalSupervisor; it preserves externally started services.
Three-stage operator authorization (ADR-0017)π
Every CLI command declares its required capability in CommandContract (packages/cli/src/contextunity/cli/command_contracts.py) and executes through typed Core SDK / gRPC clients rather than direct database or shell shortcuts:
- Stage A β Local bootstrap:
contextunity config init/local initprovisionsCU_PLATFORM_SECRETand a loopback-bound local operator profile used bylocal upandtuion127.0.0.1only. - Stage B β Project-scoped operator sessions:
contextunity auth login|status|logoutexchanges credentials via ShieldIssueSessionTokenfor short-lived Ed25519 session tokens stored with0600/keyring permissions, usable identically against local and--urlremote targets. - Stage C β Audited
platform-adminauthority: cross-tenantadmin:alloperations (shieldadmin grant/rotate/revoke, tenant lifecycle) require a verifiedplatform-adminprincipal backed by atomic ShieldSecurityAuditRecordentries.
Invariant: CLI terminal error formatters and managed-child stream forwarders redact CLI-known secrets and suppress raw subprocess credential output before printing to stdout/stderr.
Canonical CLI command groupsπ
| Group | Responsibility | Backing owner |
|---|---|---|
local, stack, services, status, tui | Local/stack startup, health checks, and secret-safe diagnostics | CLI + Core ServiceHealthReader |
config, auth | Config initialization and Stage A/B/C credential lifecycle | CLI + Shield |
router, agent | Manifest validation (router validate), registered graph execution, and trace inspection | Router + Brain |
memory, docs | Canonical BrainCell, CellEdge, BrainSynapse, BrainEngram, and _doc ingestion/queries | Brain |
tenant | Tenant provisioning, inspection, suspension, resume, and verified retirement (S-301/S-302) | Worker + Shield |
lab | Headless Lab blueprint indexing, architecture Q&A (B-101), and RouterArchitect candidate planning (R-301) | Brain + Router |
shield | Key/status inspection, signing/verification, rotation, and security-evidence queries (S-102) | Shield |
worker | Actor status, workflow runs, cancellation, and WorkerTestMan evaluation summaries (W-202) | Worker |
forge | Workspace status, ForgeLibrarian docs sync, Review Queues, and supervised Lab Mode (F-202) | Forge |
governance | Memory promotion/deprecation, Memory Linter, ProblemThread inspection, ContextSnapshot backup/restore, and ContextSeed/Registry operations (B-202/B-501) | Brain + Worker + Shield |
Use each command's --help output as the exact flag authority for currently shipped subcommands.