oc_chat
Loading...
Searching...
No Matches
inspect.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# oc_chat/registry/cli/inspect.py
4
5# OCT-LINT: disable=tests_exist reason="Covered by oc_chat/tests/registry/test_domain_inspect.py"
6"""
7Registry-native inspect commands.
8
9Purpose
10-------
11Expose read-only registry inspection commands at the top-level registry CLI.
12
13Responsibilities
14----------------
15- Wire ``show``, ``status``, and ``whoami`` commands.
16- Resolve registry paths through standard discovery.
17- Format read-model dataclasses for humans or JSON.
18
19Diagnostics
20-----------
21Domain: OC_CHAT.MAIN
22Levels:
23 L2 - lifecycle events
24 L3 - semantic details
25 L4 - deep tracing
26
27Contracts
28---------
29- Every command in this module is read-only.
30- Missing explicit plan lookups exit non-zero with a clear message.
31
32Dependencies
33------------
34- oc_diagnostics (structured debug logging via _dbg)
35- oc_chat.registry.backends.sqlite (SQLite backend)
36- oc_chat.registry.discovery (registry path discovery)
37- oc_chat.registry.readers (typed read projections)
38- oc_chat.registry (Registry facade the read projections query)
39"""
40from __future__ import annotations
41
42import argparse
43import json
44
45from oc_diagnostics import _dbg
46
47from oc_chat.registry import Registry
48from oc_chat.registry.backends.sqlite import SQLiteBackend
49from oc_chat.registry.discovery import discover_registry_location
50from oc_chat.registry.readers import current_status, show_plan, whoami
51
52
53_dbg("OC_CHAT.MAIN", "module", "loaded", 4)
54
55
56def add_inspect_subcommands(subcommands: argparse._SubParsersAction) -> None:
57 """Attach top-level read-only inspect commands."""
58
59 show = subcommands.add_parser("show", help="Show a projected plan row")
60 show.add_argument("plan_id")
61 show.add_argument("--registry", help="Registry SQLite path.")
62 show.add_argument("--json", action="store_true")
63 show.set_defaults(handler=_cmd_show)
64
65 status = subcommands.add_parser("status", help="Show registry status")
66 status.add_argument("--registry", help="Registry SQLite path.")
67 status.add_argument("--json", action="store_true")
68 status.set_defaults(handler=_cmd_status)
69
70 identity = subcommands.add_parser("whoami", help="Show actor metadata")
71 identity.add_argument("actor_slot")
72 identity.add_argument("--registry", help="Registry SQLite path.")
73 identity.add_argument("--json", action="store_true")
74 identity.set_defaults(handler=_cmd_whoami)
75
76
77def _cmd_show(args: argparse.Namespace) -> int:
78 registry = _registry(args.registry)
79 view = show_plan(registry, args.plan_id)
80 if view is None:
81 print(f"plan not found: {args.plan_id}")
82 return 1
83 _print(view.__dict__, args.json)
84 return 0
85
86
87def _cmd_status(args: argparse.Namespace) -> int:
88 registry = _registry(args.registry)
89 status = current_status(registry)
90 _print(status.__dict__, args.json)
91 return 0
92
93
94def _cmd_whoami(args: argparse.Namespace) -> int:
95 registry = _registry(args.registry)
96 actor = whoami(registry, args.actor_slot)
97 _print(actor.__dict__, args.json)
98 return 0
99
100
101def _registry(registry_path: str | None) -> Registry:
102 location = discover_registry_location(registry_path=registry_path)
103 return Registry(SQLiteBackend(location.registry_path))
104
105
106def _print(payload: dict[str, object], as_json: bool) -> None:
107 if as_json:
108 print(json.dumps(payload, indent=2, sort_keys=True))
109 return
110 for key, value in payload.items():
111 print(f"{key}: {value}")
int _cmd_status(argparse.Namespace args)
Definition inspect.py:87
int _cmd_whoami(argparse.Namespace args)
Definition inspect.py:94
int _cmd_show(argparse.Namespace args)
Definition inspect.py:77
Registry _registry(str|None registry_path)
Definition inspect.py:101
None add_inspect_subcommands(argparse._SubParsersAction subcommands)
Definition inspect.py:56
None _print(dict[str, object] payload, bool as_json)
Definition inspect.py:106