History Module API Reference¶
This section documents the internals of the history
module in Bijux CLI.
bijux_cli.services.history ¶
Provides a persistent, cross-process safe command history service.
This module defines the History
class, a concrete implementation of the HistoryProtocol
. It provides a tolerant and robust store for CLI invocation events with several key design features:
* **Persistence:** All history is saved to a single JSON array in a
per-user file.
* **Tolerance:** The service is resilient to empty, corrupt, or partially
formed history files. If a file is unreadable, it is treated as empty
and will be overwritten on the next successful write.
* **Cross-Process Safety:** On POSIX systems, it uses `fcntl.flock` on a
sidecar lock file to safely coordinate writes from multiple concurrent
CLI processes. On other systems, it falls back to a thread lock.
* **Atomic Writes:** All changes are written to a temporary file which is
then atomically moved into place, preventing data corruption from
interrupted writes.
* **Memory Management:** The in-memory list of events is capped, and the
on-disk file is trimmed to a smaller size to prevent unbounded growth.
* **Simplicity:** The service intentionally avoids complex features like
schema migrations. Unreadable state is discarded rather than repaired.
History ¶
History(
telemetry: LoggingTelemetry,
observability: Observability,
history_path: Path | None = None,
)
Bases: HistoryProtocol
Manages a persistent history of CLI command invocations.
This service maintains an in-memory list of command events and synchronizes it with a persisted JSON file. It is designed to be tolerant of file corruption and safe for concurrent use by multiple CLI processes.
Mutating operations (add
, clear
, import_
) acquire a cross-process lock before modifying the file to prevent lost updates and race conditions. The sequence is always: lock, reload from disk, apply change in memory, write atomically, and release lock.
Attributes:
-
_tel
(LoggingTelemetry
) –The telemetry service for emitting events.
-
_obs
(Observability
) –The logging service for operational errors.
-
_explicit_path
(Path | None
) –A specific path to the history file, if provided during initialization.
-
_events
(list
) –The in-memory cache of history event dictionaries.
-
_load_error
(str | None
) –A message describing the last error that occurred while trying to load the history file, if any.
Initializes the History service.
Parameters:
-
telemetry
(LoggingTelemetry
) –The telemetry service.
-
observability
(Observability
) –The logging service.
-
history_path
(Path | None
, default:None
) –An optional, explicit path to the history file. If None, a default path will be used.
Source code in src/bijux_cli/services/history.py
add ¶
add(
command: str,
*,
params: Sequence[str] | None = None,
success: bool | None = True,
return_code: int | None = 0,
duration_ms: float | None = None,
) -> None
Appends a new command invocation to the history.
This operation is cross-process safe. It acquires a lock, reloads the latest history from disk, appends the new entry, and writes the updated history back atomically. Errors are logged but suppressed to allow the originating command to complete its execution.
Parameters:
-
command
(str
) –The command name (ASCII characters are enforced).
-
params
(Sequence[str] | None
, default:None
) –A list of parameters and flags.
-
success
(bool | None
, default:True
) –Whether the command succeeded.
-
return_code
(int | None
, default:0
) –The exit code of the command.
-
duration_ms
(float | None
, default:None
) –The command's duration in milliseconds.
Source code in src/bijux_cli/services/history.py
clear ¶
Erases all persisted history.
This operation is cross-process safe and atomic.
Raises:
-
PermissionError
–If the history file or directory is not writable.
-
OSError
–For other filesystem-related failures.
Source code in src/bijux_cli/services/history.py
export ¶
Exports the current history to a file as a JSON array.
This operation is a read-only snapshot and does not lock the source file.
Parameters:
-
path
(Path
) –The destination file path.
Raises:
-
RuntimeError
–On I/O failures.
Source code in src/bijux_cli/services/history.py
flush ¶
import_ ¶
Imports history entries from a file, merging with current history.
This operation is cross-process safe and atomic.
Parameters:
-
path
(Path
) –The source file path containing a JSON array of entries.
Raises:
-
RuntimeError
–On I/O or parsing failures.
Source code in src/bijux_cli/services/history.py
list ¶
list(
*,
limit: int | None = 20,
group_by: str | None = None,
filter_cmd: str | None = None,
sort: str | None = None,
) -> list[dict[str, Any]]
Returns a view of the command history, with optional transformations.
This is a read-only operation and does not acquire a cross-process lock, meaning it may not reflect writes from concurrent processes.
Parameters:
-
limit
(int | None
, default:20
) –The maximum number of entries to return. A value of 0 returns an empty list.
-
group_by
(str | None
, default:None
) –If provided, returns a grouped summary.
-
filter_cmd
(str | None
, default:None
) –If provided, returns only entries whose command contains this case-sensitive substring.
-
sort
(str | None
, default:None
) –If 'timestamp', sorts entries by timestamp.
Returns:
-
list[dict[str, Any]]
–list[dict[str, Any]]: A list of history entries or grouped summaries.
Raises:
-
RuntimeError
–If the history file is corrupt.