Coverage for  / home / runner / work / bijux-cli / bijux-cli / src / bijux_cli / cli / commands / memory / list.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.13.2, created at 2026-01-26 17:59 +0000

1# SPDX-License-Identifier: Apache-2.0 

2# Copyright © 2025 Bijan Mousavi 

3 

4"""Implements the `memory list` subcommand for the Bijux CLI. 

5 

6This module contains the logic for listing all keys currently held in the 

7transient, in-memory data store. It retrieves the keys and presents them in a 

8structured, machine-readable list format. 

9 

10Output Contract: 

11 * Success: `{"status": "ok", "keys": list, "count": int}` 

12 * Error: `{"error": str, "code": int}` 

13 

14Exit Codes: 

15 * `0`: Success. 

16 * `1`: An unexpected error occurred (e.g., service unavailable, list failed). 

17""" 

18 

19from __future__ import annotations 

20 

21import platform 

22 

23import typer 

24 

25from bijux_cli.cli.commands.memory.resolve import resolve_memory_service 

26from bijux_cli.cli.core.command import ( 

27 ascii_safe, 

28 new_run_command, 

29 raise_exit_intent, 

30 validate_common_flags, 

31) 

32from bijux_cli.cli.core.constants import ( 

33 OPT_FORMAT, 

34 OPT_LOG_LEVEL, 

35 OPT_PRETTY, 

36 OPT_QUIET, 

37) 

38from bijux_cli.cli.core.help_text import ( 

39 HELP_FORMAT, 

40 HELP_LOG_LEVEL, 

41 HELP_NO_PRETTY, 

42 HELP_QUIET, 

43) 

44from bijux_cli.core.enums import ErrorType 

45from bijux_cli.core.precedence import current_execution_policy 

46 

47 

48def _build_payload(include_runtime: bool, keys: list[str]) -> dict[str, object]: 

49 """Builds the payload for the memory keys list response. 

50 

51 Args: 

52 include_runtime (bool): If True, includes Python and platform info. 

53 keys (list[str]): The list of keys from the memory store. 

54 

55 Returns: 

56 Mapping[str, object]: A dictionary containing the status, a sorted list 

57 of keys, the key count, and optional runtime metadata. 

58 """ 

59 payload: dict[str, object] = {"status": "ok", "keys": keys, "count": len(keys)} 

60 if include_runtime: 

61 return { 

62 "status": payload["status"], 

63 "keys": keys, 

64 "count": len(keys), 

65 "python": ascii_safe(platform.python_version(), "python_version"), 

66 "platform": ascii_safe(platform.platform(), "platform"), 

67 } 

68 return payload 

69 

70 

71def list_memory( 

72 quiet: bool = typer.Option(False, *OPT_QUIET, help=HELP_QUIET), 

73 fmt: str = typer.Option("json", *OPT_FORMAT, help=HELP_FORMAT), 

74 pretty: bool = typer.Option(True, OPT_PRETTY, help=HELP_NO_PRETTY), 

75 log_level: str = typer.Option("info", *OPT_LOG_LEVEL, help=HELP_LOG_LEVEL), 

76) -> None: 

77 """Lists all keys currently stored in the transient in-memory store. 

78 

79 This command retrieves all defined keys from the memory service, sorts them, 

80 and then emits them in a structured payload. 

81 

82 Args: 

83 quiet (bool): If True, suppresses all output except for errors. 

84 fmt (str): The output format, "json" or "yaml". 

85 pretty (bool): If True, pretty-prints the output. log_level (str): Logging level for diagnostics. 

86 

87 Returns: 

88 None: 

89 

90 Raises: 

91 SystemExit: Always exits with a contract-compliant status code and 

92 payload, indicating success or detailing an error. 

93 """ 

94 command = "memory list" 

95 policy = current_execution_policy() 

96 quiet = policy.quiet 

97 include_runtime = policy.include_runtime 

98 pretty = policy.pretty 

99 log_level_value = policy.log_level 

100 fmt_lower = validate_common_flags( 

101 fmt, 

102 command, 

103 quiet, 

104 include_runtime=include_runtime, 

105 log_level=log_level_value, 

106 ) 

107 

108 memory_svc = resolve_memory_service( 

109 command, fmt_lower, quiet, include_runtime, log_level_value 

110 ) 

111 

112 try: 

113 keys = sorted(memory_svc.keys()) 

114 except Exception as exc: 

115 raise_exit_intent( 

116 f"Failed to list memory keys: {exc}", 

117 code=1, 

118 failure="list_failed", 

119 error_type=ErrorType.INTERNAL, 

120 command=command, 

121 fmt=fmt_lower, 

122 quiet=quiet, 

123 include_runtime=include_runtime, 

124 log_level=log_level_value, 

125 ) 

126 

127 new_run_command( 

128 command_name=command, 

129 payload_builder=lambda include: _build_payload(include, keys), 

130 quiet=quiet, 

131 fmt=fmt_lower, 

132 pretty=pretty, 

133 log_level=log_level_value, 

134 )