New Phase 1b in tests/run-tests.fish: catches a bare C1-shadowed-command call in a functions/*.fish body with no matching uses-shadow(name) or self-limiting(name) in that function's own CLASSIFICATION header. This is exactly the check discussed after the rm and cd audits -- runtime auto-unwrapping isn't viable in fish (there's no hook finer than shadowing itself, and rewriting behavior invisibly at runtime is its own footgun); a static lint using the CLASSIFICATION tag as the declared-intentional marker is. Scoped to functions/*.fish only: the one-function-per-file convention there makes body extraction exact with no block-depth parser needed. Added a new self-limiting(name) tag to the schema for the case a bare call is safe not because the caller did anything, but because the shadow's own logic already neutralizes the override: rm's and mkdir's flag checks (verified precisely -- rm falls back to command rm for any flag except a bare -r/-R/--recursive alone, which still routes to trash; mkdir falls back to command mkdir -p for any flag, no exception), and grep/fgrep/egrep/dir/vdir/cat's own tty auto-detection (--color=auto, and bat's default color behavior -- verified byte-identical to stock cat when piped, since bat also auto-disables highlighting on a non-terminal). Explicit and durable rather than a silent lint exemption: if a shadow's bypass condition is ever weakened, every self-limiting site is one grep away instead of silently wrong. Running the first draft of the lint surfaced three more real bugs, none previously audited: - config-help.fish's --man pager path checks `type -q less` (proving it wants the real less binary specifically, for less-only -R/+N flag syntax) then called it bare, routing through our own $PAGER -> ov -> less -> more -> cat fallback chain instead -- which could hand those less-specific flags to a completely different program. Now command less. - _fish_deps_install.fish and _fish_deps_update.fish's binary-upgrade paths cp a freshly downloaded binary over an already-installed one with no existence guard -- the update flow's target is guaranteed to already exist. Our cp shadow forces -i unconditionally (a plain alias, not flag-aware like rm's), so this would hang waiting on a confirmation prompt in any non-interactive run. Now command cp. Same two files' lazydocker install path piped curl output into bare bash, invoking our shell-switch wrapper instead of a plain subshell. Now command bash. - agents-init.fish's AGENTS.md/CLAUDE.md relocation calls mv bare in four places; each is already guarded by a preceding test -f check on the destination, so the -i alias was unlikely to ever fire in practice, but explicit command mv removes the reliance on that guard entirely rather than leaving it as the only thing standing between a file move and an unattended hang. The remaining ~65 flagged call sites across ~24 files were reviewed individually and tagged self-limiting(rm)/self-limiting(mkdir) (verified flagged with -f/-rf or -p) and self-limiting(grep)/ self-limiting(cat) (verified piped, captured, or -q/-c; none display color to a human), plus uses-shadow(ls) for two existence-check-only calls (cffetch.fish, ffetch.fish) whose output is redirected to /dev/null.
121 lines
4.5 KiB
Fish
121 lines
4.5 KiB
Fish
# Copyright (C) 2026 Rootiest
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# CATEGORY
|
|
# 11-pager-and-logging
|
|
#
|
|
# COMPONENT
|
|
# site exit-plain: overrides/key-bindings
|
|
# site logging-guard: logging/terminal-capture
|
|
#
|
|
# CLASSIFICATION
|
|
# self-limiting(rm,mkdir), destructive
|
|
#
|
|
# SYNOPSIS
|
|
# smart_exit [-h] [-n]
|
|
#
|
|
# DESCRIPTION
|
|
# Closes the shell session. In Kitty, captures the terminal scrollback to a
|
|
# timestamped log file in $SCROLLBACK_HISTORY_DIR before exiting.
|
|
# Automatically prunes junk and the oldest logs when the count exceeds
|
|
# $SCROLLBACK_HISTORY_MAX_FILES.
|
|
#
|
|
# ARGUMENTS
|
|
# -h, --help Show help message
|
|
# -n, --no-log Exit without saving a scrollback log
|
|
#
|
|
# EXIT STATUS
|
|
# 0 Shell session exited
|
|
# 1 Argument parsing failed
|
|
#
|
|
# EXAMPLE
|
|
# smart_exit
|
|
# smart_exit --no-log
|
|
#
|
|
# NOTES
|
|
# The exit builtin is wired to smart_exit for interactive sessions. Typing
|
|
# exit or Ctrl+D behaves identically to calling smart_exit directly.
|
|
function smart_exit --description 'Capture colorized scrollback before exiting, with pruning and safe overrides'
|
|
# Opinionated guard (C3): exit plainly when overrides are disabled.
|
|
# This composes with Task #4's __fish_config_enable_logging, which will
|
|
# gate only the scrollback capture while leaving the exit wrapper active.
|
|
if not __fish_config_op_enabled (status current-function) exit-plain
|
|
builtin exit $argv
|
|
end
|
|
|
|
set -l options h/help n/no-log
|
|
argparse $options -- $argv
|
|
or return 1
|
|
|
|
__fish_palette
|
|
|
|
if set -q _flag_help
|
|
echo -e "$c_head""Usage:$c_reset $c_cmd"exit"$c_reset [$c_arg""OPTIONS""$c_reset]"
|
|
echo ""
|
|
echo "Closes the current shell session, automatically archiving the window scrollback."
|
|
echo ""
|
|
echo "$c_head""Options:$c_reset"
|
|
echo -e " $c_flag""-h, --help""$c_reset Show this help message"
|
|
echo -e " $c_flag""-n, --no-log""$c_reset Exit immediately without saving a scrollback history log"
|
|
return 0
|
|
end
|
|
|
|
# C5 — Logging & Capture: skip all capture when logging is disabled.
|
|
# When disabled, tell Kitty the window is handled so its watcher doesn't
|
|
# capture either — belt-and-suspenders alongside the sentinel file.
|
|
if not __fish_config_op_enabled (status current-function) logging-guard
|
|
if test -n "$KITTY_WINDOW_ID"
|
|
kitty @ set-user-vars "logged_by_shell=true" 2>/dev/null
|
|
end
|
|
builtin exit
|
|
end
|
|
|
|
set -l snapshot_dir (set -q SCROLLBACK_HISTORY_DIR; and echo $SCROLLBACK_HISTORY_DIR; or echo "$HOME/.terminal_history")
|
|
set -l max_files (set -q SCROLLBACK_HISTORY_MAX_FILES; and echo $SCROLLBACK_HISTORY_MAX_FILES; or echo 100)
|
|
|
|
# Handle Scrollback Capture (Skipped if -n/--no-log is used)
|
|
if not set -q _flag_no_log
|
|
mkdir -p $snapshot_dir
|
|
set -l timestamp (date "+%Y-%m-%d_%H-%M-%S")
|
|
set -l filename "$snapshot_dir/scrollback_$timestamp.log"
|
|
|
|
# Safe child process detection
|
|
set -l active_tui (ps -o comm= --ppid $fish_pid 2>/dev/null)
|
|
|
|
if test -n "$KITTY_WINDOW_ID"
|
|
# LIVE BUFFER CHECK: Check the active token variable $_
|
|
# If the user typed exit, $_ will match "exit". If flags were passed,
|
|
# we check if it contains the phrase "exit" to match 'exit -n' or 'exit --help'.
|
|
if string match -qr exit "$_"
|
|
if not string match -qr '^(nvim|vim|vi|nano|emacs|tmux)$' "$active_tui"
|
|
# Capture the log via the shell
|
|
kitty @ get-text --match id:$KITTY_WINDOW_ID --extent all --ansi | sed 's/^\[38;2;[0-9;]*m//g' >$filename 2>/dev/null
|
|
# Broadcast a window variable flag telling Kitty the log is handled
|
|
kitty @ set-user-vars "logged_by_shell=true" 2>/dev/null
|
|
end
|
|
end
|
|
end
|
|
|
|
# 4. Prune junk logs before counting toward the max
|
|
_scrollback_prune_junk $snapshot_dir
|
|
|
|
# 5. Automatic Pruning Logic
|
|
set -l current_logs $snapshot_dir/scrollback_*.log
|
|
if test -f "$current_logs[1]"
|
|
set -l total_files (count $current_logs)
|
|
if test $total_files -gt $max_files
|
|
set -l num_to_delete (math $total_files - $max_files)
|
|
for i in (seq 1 $num_to_delete)
|
|
rm -f $current_logs[$i]
|
|
end
|
|
end
|
|
end
|
|
else
|
|
echo -e "$c_warn""➔""$c_reset Exiting discreetly; $c_cmd""no history logs saved.""$c_reset"
|
|
sleep 0.4
|
|
end
|
|
|
|
# Call the true system exit directly
|
|
builtin exit
|
|
end
|