Coverage for / home / runner / work / bijux-cli / bijux-cli / src / bijux_cli / cli / commands / memory / delete.py: 100%
32 statements
« prev ^ index » next coverage.py v7.13.2, created at 2026-01-26 17:59 +0000
« 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
4"""Implements the `memory delete` subcommand for the Bijux CLI.
6This module contains the logic for removing a specific key and its associated
7value from the transient, in-memory data store. It provides a structured,
8machine-readable confirmation of the deletion.
10Output Contract:
11 * Success: `{"status": "deleted", "key": str}`
12 * Error: `{"error": str, "code": int}`
14Exit Codes:
15 * `0`: Success.
16 * `1`: The key was not found, or another unexpected error occurred.
17 * `2`: The provided key was invalid.
18"""
20from __future__ import annotations
22import platform
24import typer
26from bijux_cli.cli.commands.memory.resolve import resolve_memory_service
27from bijux_cli.cli.core.command import (
28 ascii_safe,
29 new_run_command,
30 raise_exit_intent,
31 validate_common_flags,
32)
33from bijux_cli.cli.core.constants import (
34 OPT_FORMAT,
35 OPT_LOG_LEVEL,
36 OPT_PRETTY,
37 OPT_QUIET,
38)
39from bijux_cli.cli.core.help_text import (
40 HELP_FORMAT,
41 HELP_LOG_LEVEL,
42 HELP_NO_PRETTY,
43 HELP_QUIET,
44)
45from bijux_cli.core.enums import ErrorType
46from bijux_cli.core.precedence import current_execution_policy
49def _build_payload(include_runtime: bool, key: str) -> dict[str, object]:
50 """Builds the payload for a memory key deletion response.
52 Args:
53 include_runtime (bool): If True, includes Python and platform info.
54 key (str): The memory key that was deleted.
56 Returns:
57 Mapping[str, object]: A dictionary containing the status, the key that
58 was deleted, and optional runtime metadata.
59 """
60 payload: dict[str, object] = {"status": "deleted", "key": key}
61 if include_runtime:
62 return {
63 "status": payload["status"],
64 "key": key,
65 "python": ascii_safe(platform.python_version(), "python_version"),
66 "platform": ascii_safe(platform.platform(), "platform"),
67 }
68 return payload
71def delete_memory(
72 key: str = typer.Argument(..., help="Key to delete"),
73 quiet: bool = typer.Option(False, *OPT_QUIET, help=HELP_QUIET),
74 fmt: str = typer.Option("json", *OPT_FORMAT, help=HELP_FORMAT),
75 pretty: bool = typer.Option(True, OPT_PRETTY, help=HELP_NO_PRETTY),
76 log_level: str = typer.Option("info", *OPT_LOG_LEVEL, help=HELP_LOG_LEVEL),
77) -> None:
78 """Deletes a key from the transient in-memory store.
80 This command validates the key's format and then removes the corresponding
81 key-value pair from the memory service.
83 Args:
84 key (str): The memory key to remove. Must be between 1 and 4096
85 printable, non-whitespace characters.
86 quiet (bool): If True, suppresses all output except for errors.
87 fmt (str): The output format, "json" or "yaml".
88 pretty (bool): If True, pretty-prints the output. log_level (str): Logging level for diagnostics.
90 Returns:
91 None:
93 Raises:
94 SystemExit: Always exits with a contract-compliant status code and
95 payload, indicating success or detailing an error.
96 """
97 command = "memory delete"
98 policy = current_execution_policy()
99 quiet = policy.quiet
100 include_runtime = policy.include_runtime
101 pretty = policy.pretty
102 log_level_value = policy.log_level
103 fmt_lower = validate_common_flags(
104 fmt,
105 command,
106 quiet,
107 include_runtime=include_runtime,
108 log_level=log_level_value,
109 )
111 if not (
112 1 <= len(key) <= 4096 and all(c.isprintable() and not c.isspace() for c in key)
113 ):
114 raise_exit_intent(
115 "Invalid key: must be 1-4096 printable non-space characters",
116 code=2,
117 failure="invalid_key",
118 error_type=ErrorType.USER_INPUT,
119 command=command,
120 fmt=fmt_lower,
121 quiet=quiet,
122 include_runtime=include_runtime,
123 log_level=log_level_value,
124 )
126 memory_svc = resolve_memory_service(
127 command, fmt_lower, quiet, include_runtime, log_level_value
128 )
130 try:
131 memory_svc.delete(key)
132 except KeyError:
133 raise_exit_intent(
134 f"Key not found: {key}",
135 code=1,
136 failure="not_found",
137 error_type=ErrorType.USER_INPUT,
138 command=command,
139 fmt=fmt_lower,
140 quiet=quiet,
141 include_runtime=include_runtime,
142 log_level=log_level_value,
143 )
144 except Exception as exc:
145 raise_exit_intent(
146 f"Failed to delete memory key: {exc}",
147 code=1,
148 failure="delete_failed",
149 error_type=ErrorType.INTERNAL,
150 command=command,
151 fmt=fmt_lower,
152 quiet=quiet,
153 include_runtime=include_runtime,
154 log_level=log_level_value,
155 )
157 new_run_command(
158 command_name=command,
159 payload_builder=lambda include: _build_payload(include, key),
160 quiet=quiet,
161 fmt=fmt_lower,
162 pretty=pretty,
163 log_level=log_level,
164 )