Home/Docs/Getting Started/CLI Architecture

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
LayerCarrierAuthority
C0~/.contextunity/*.yml + explicit service env varsOperator/deployment infrastructure, endpoints, secrets, budgets, and boolean feature flags
C1contextunity.project.yaml (contextunity/v1alpha8+)Project identity (project.id), allowed_tenants, declared graphs, toolkits, and allowlisted profile references
C2router.graph.<id>Graph-scoped topology, orchestration roles, and memory/trace bindings
C3router.graph.<id>.nodes[*]Node-level model, prompt, and attenuated tool/secret permissions
Important
  • INV-CLI-CONFIG-ZERO-NETWORK: Configuration loading via get_cli_config (backed by load_service_config) performs zero network or mesh probes; runtime feature rollout uses explicit C0 boolean service flags, never --phase N runtime switches.

Key symbols and inputs/outputsπŸ”—

SymbolRoleInputs / Outputs
appRoot Typer CLI entrypointOperator flags/subcommands -> local command actions (e.g., version, local up) or service-backed gRPC/SDK calls with command-specific effects
get_cli_configNetwork-free CLI config loaderLocal YAML/env -> validated CliConfig
LocalSupervisorForeground local stack managerLocal operator profile -> managed local Brain, Router, Worker, and Forge (when installed) processes; probes configured external Shield endpoint
UserSystemdManagerBackground systemd --user managerProfile name -> user systemd unit lifecycle
LocalTuiTargetResolverLoopback TUI target resolverStored profile -> LocalTuiTarget(profile, router_url, token) (the TUI app instantiates ServiceHealthReader separately)
CommandContractDeclarative CLI contract metadataStatic command metadata fields consumed by CLI authorization and target-resolution helpers

Local lifecycle and startup modesπŸ”—

CommandProcess ownerPurpose
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 --profileUserSystemdManager (systemd --user)Background profile-scoped local service lifecycle without PID-file races
contextunity stack up|down|status --profileCompose / external stack runnerDependency-backed dev or prod-like multi-container stacks (PostgreSQL, Redis, Temporal, MinIO)
contextunity tuiLoopback read-only Textual clientAttaches to local loopback services via LocalTuiTargetResolver and ServiceHealthReader, rendering redacted bounded journal logs without minting tokens
Important
  • 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:

  1. Stage A β€” Local bootstrap: contextunity config init / local init provisions CU_PLATFORM_SECRET and a loopback-bound local operator profile used by local up and tui on 127.0.0.1 only.
  2. Stage B β€” Project-scoped operator sessions: contextunity auth login|status|logout exchanges credentials via Shield IssueSessionToken for short-lived Ed25519 session tokens stored with 0600/keyring permissions, usable identically against local and --url remote targets.
  3. Stage C β€” Audited platform-admin authority: cross-tenant admin:all operations (shield admin grant/rotate/revoke, tenant lifecycle) require a verified platform-admin principal backed by atomic Shield SecurityAuditRecord entries.
Warning

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πŸ”—

GroupResponsibilityBacking owner
local, stack, services, status, tuiLocal/stack startup, health checks, and secret-safe diagnosticsCLI + Core ServiceHealthReader
config, authConfig initialization and Stage A/B/C credential lifecycleCLI + Shield
router, agentManifest validation (router validate), registered graph execution, and trace inspectionRouter + Brain
memory, docsCanonical BrainCell, CellEdge, BrainSynapse, BrainEngram, and _doc ingestion/queriesBrain
tenantTenant provisioning, inspection, suspension, resume, and verified retirement (S-301/S-302)Worker + Shield
labHeadless Lab blueprint indexing, architecture Q&A (B-101), and RouterArchitect candidate planning (R-301)Brain + Router
shieldKey/status inspection, signing/verification, rotation, and security-evidence queries (S-102)Shield
workerActor status, workflow runs, cancellation, and WorkerTestMan evaluation summaries (W-202)Worker
forgeWorkspace status, ForgeLibrarian docs sync, Review Queues, and supervised Lab Mode (F-202)Forge
governanceMemory 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.