diff --git a/bin/history.sh b/bin/history.sh index 97a0b1c3f..d0377d0bd 100755 --- a/bin/history.sh +++ b/bin/history.sh @@ -58,7 +58,7 @@ main() { done history_load_operations "$(history_operations_log_file)" - history_load_deletions "$(history_deletions_log_file)" + history_load_deletions "$(history_deletions_log_file)" "$HISTORY_LIMIT" if [[ "$HISTORY_JSON" == "true" ]]; then history_render_json "$HISTORY_LIMIT" diff --git a/lib/core/history.sh b/lib/core/history.sh index cfbe27fee..cb8562880 100644 --- a/lib/core/history.sh +++ b/lib/core/history.sh @@ -628,21 +628,37 @@ history_load_operations() { history_load_deletions() { local log_file="$1" + local limit="${2:-0}" next=0 local timestamp mode size_kb status path history_reset_deletions [[ -f "$log_file" ]] || return 0 - # Split each row directly, keeping the last row even without a newline. + # Keep the newest valid rows in a ring when the caller supplies a limit. + # An unterminated final row still counts, and malformed rows consume no slot. while IFS=$'\t' read -r timestamp mode size_kb status path || [[ -n "$timestamp" ]]; do [[ -n "${timestamp:-}" && -n "${mode:-}" && -n "${status:-}" ]] || continue - HISTORY_DELETE_TIMESTAMPS+=("$timestamp") - HISTORY_DELETE_MODES+=("$mode") - HISTORY_DELETE_SIZE_KB+=("${size_kb:-unknown}") - HISTORY_DELETE_STATUSES+=("$status") - HISTORY_DELETE_PATHS+=("${path:-}") + HISTORY_DELETE_TIMESTAMPS[next]="$timestamp" + HISTORY_DELETE_MODES[next]="$mode" + HISTORY_DELETE_SIZE_KB[next]="${size_kb:-unknown}" + HISTORY_DELETE_STATUSES[next]="$status" + HISTORY_DELETE_PATHS[next]="${path:-}" + next=$((next + 1)) + if [[ "$next" -eq "$limit" ]]; then + next=0 + fi done < "$log_file" + + # A partially overwritten ring starts at the next write slot. Callers and + # both renderers expect the retained rows in their original file order. + if [[ "$next" -gt 0 && "${#HISTORY_DELETE_TIMESTAMPS[@]}" -eq "$limit" ]]; then + HISTORY_DELETE_TIMESTAMPS=("${HISTORY_DELETE_TIMESTAMPS[@]:next}" "${HISTORY_DELETE_TIMESTAMPS[@]:0:next}") + HISTORY_DELETE_MODES=("${HISTORY_DELETE_MODES[@]:next}" "${HISTORY_DELETE_MODES[@]:0:next}") + HISTORY_DELETE_SIZE_KB=("${HISTORY_DELETE_SIZE_KB[@]:next}" "${HISTORY_DELETE_SIZE_KB[@]:0:next}") + HISTORY_DELETE_STATUSES=("${HISTORY_DELETE_STATUSES[@]:next}" "${HISTORY_DELETE_STATUSES[@]:0:next}") + HISTORY_DELETE_PATHS=("${HISTORY_DELETE_PATHS[@]:next}" "${HISTORY_DELETE_PATHS[@]:0:next}") + fi } history_join_counts() { diff --git a/tests/history.bats b/tests/history.bats index 115c0cb2e..49cc6d7bf 100644 --- a/tests/history.bats +++ b/tests/history.bats @@ -321,6 +321,35 @@ assert rows == [ ' "$last_path" } +@test "bounded deletion history keeps the newest valid rows in file order" { + local audit_log="$HOME/Library/Logs/mole/deletions.log" + local i + for i in 1 2 3 4 5 6 7; do + printf 't%s\tm%s\t%s\ts%s\tp%s\n' "$i" "$i" "$i" "$i" "$i" + printf 'ignored\tshort\n' + done > "$audit_log" + printf 't8\tm8\t8\ts8\tp8' >> "$audit_log" + + run env PROJECT_ROOT="$PROJECT_ROOT" AUDIT_LOG="$audit_log" /bin/bash --noprofile --norc <<'BASH' +source "$PROJECT_ROOT/lib/core/history.sh" +for limit in 1 3 4 8 20; do + history_load_deletions "$AUDIT_LOG" "$limit" + count=$limit + [[ $count -le 8 ]] || count=8 + [[ ${#HISTORY_DELETE_TIMESTAMPS[@]} -eq $count ]] || exit 1 + for ((i = 0; i < count; i++)); do + n=$((9 - count + i)) + [[ ${HISTORY_DELETE_TIMESTAMPS[i]} == "t$n" && ${HISTORY_DELETE_MODES[i]} == "m$n" && + ${HISTORY_DELETE_SIZE_KB[i]} == "$n" && ${HISTORY_DELETE_STATUSES[i]} == "s$n" && + ${HISTORY_DELETE_PATHS[i]} == "p$n" ]] || exit 1 + done +done +history_load_deletions "$AUDIT_LOG" +[[ ${#HISTORY_DELETE_PATHS[@]} -eq 8 ]] +BASH + [ "$status" -eq 0 ] +} + @test "mo history --json escapes unusual path characters" { : > "$HOME/Library/Logs/mole/operations.log" weird_path=$'/tmp/unicode-\xe9\x9b\xaa-quote"slash\\tab\tbackspace\bformfeed\fend'