Coverage for  / home / runner / work / bijux-cli / bijux-cli / src / bijux_cli / cli / commands / memory / clear.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 clear` subcommand for the Bijux CLI. 

5 

6This module contains the logic for permanently erasing all entries from the 

7transient, in-memory data store. This action is irreversible for the current 

8process. A structured confirmation is emitted upon success. 

9 

10Output Contract: 

11 * Success: `{"status": "cleared", "count": 0}` 

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

13 

14Exit Codes: 

15 * `0`: Success. 

16 * `1`: An unexpected error occurred (e.g., service unavailable, clear 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) -> dict[str, object]: 

49 """Builds the payload confirming that the in-memory store was cleared. 

50 

51 Args: 

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

53 

54 Returns: 

55 Mapping[str, object]: A dictionary containing the status, a count of 0, 

56 and optional runtime metadata. 

57 """ 

58 payload: dict[str, object] = {"status": "cleared", "count": 0} 

59 if include_runtime: 

60 return { 

61 "status": payload["status"], 

62 "count": payload["count"], 

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

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

65 } 

66 return payload 

67 

68 

69def clear_memory( 

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

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

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

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

74) -> None: 

75 """Removes all key-value pairs from the transient in-memory store. 

76 

77 This command erases all entries from the memory service and emits a 

78 structured payload to confirm the operation. 

79 

80 Args: 

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

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

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

84 

85 Returns: 

86 None: 

87 

88 Raises: 

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

90 payload, indicating success or detailing an error. 

91 """ 

92 command = "memory clear" 

93 policy = current_execution_policy() 

94 quiet = policy.quiet 

95 include_runtime = policy.include_runtime 

96 pretty = policy.pretty 

97 log_level_value = policy.log_level 

98 fmt_lower = validate_common_flags( 

99 fmt, 

100 command, 

101 quiet, 

102 include_runtime=include_runtime, 

103 log_level=log_level_value, 

104 ) 

105 

106 memory_svc = resolve_memory_service( 

107 command, fmt_lower, quiet, include_runtime, log_level_value 

108 ) 

109 

110 try: 

111 memory_svc.clear() 

112 except Exception as exc: 

113 raise_exit_intent( 

114 f"Failed to clear memory: {exc}", 

115 code=1, 

116 failure="clear_failed", 

117 error_type=ErrorType.INTERNAL, 

118 command=command, 

119 fmt=fmt_lower, 

120 quiet=quiet, 

121 include_runtime=include_runtime, 

122 log_level=log_level_value, 

123 ) 

124 

125 new_run_command( 

126 command_name=command, 

127 payload_builder=lambda include: _build_payload(include), 

128 quiet=quiet, 

129 fmt=fmt_lower, 

130 pretty=pretty, 

131 log_level=log_level, 

132 )