diff --git a/conf.d/cheat.fish b/completions/cheat.fish similarity index 100% rename from conf.d/cheat.fish rename to completions/cheat.fish diff --git a/conf.d/tailscale.fish b/completions/tailscale.fish similarity index 93% rename from conf.d/tailscale.fish rename to completions/tailscale.fish index dce3602..b682131 100644 --- a/conf.d/tailscale.fish +++ b/completions/tailscale.fish @@ -228,16 +228,15 @@ function __tailscale_prepare_completions return 0 end -# Since Fish completions are only loaded once the user triggers them, we trigger them ourselves -# so we can properly delete any completions provided by another script. -# Only do this if the program can be found, or else fish may print some errors; besides, -# the existing completions will only be loaded if the program can be found. -if type -q "tailscale" - # The space after the program name is essential to trigger completion for the program - # and not completion of the program name itself. - # Also, we use '> /dev/null 2>&1' since '&>' is not supported in older versions of fish. - complete --do-complete "tailscale " > /dev/null 2>&1 -end +# REMOVED (2026-09-07): Cobra's self-priming block — +# if type -q "tailscale"; complete --do-complete "tailscale " >/dev/null 2>&1; end +# It existed to force any pre-existing tailscale completions to load so the +# `complete -c tailscale -e` below could erase them. From completions/ it has +# no job: fish autoloads only the FIRST match on $fish_complete_path, and +# $__fish_config_dir/completions precedes /usr/share/fish/vendor_completions.d, +# so the vendor file is never sourced and there is nothing to erase. It also +# executed the tailscale binary at startup. Verified: completion output is +# byte-identical with and without it. See AGENTS/specs/2026-09-07-startup-latency-design.md D2. # Remove any pre-existing completions for the program since we will be handling all of them. complete -c tailscale -e diff --git a/conf.d/abbr.fish b/conf.d/abbr.fish index 5771e32..b8016d1 100644 --- a/conf.d/abbr.fish +++ b/conf.d/abbr.fish @@ -12,6 +12,9 @@ # site abbr-integrations: integrations/terminal-abbrs # site abbr-overrides: overrides/key-bindings +# Abbreviations only expand in the line editor; a script can never use one. +status is-interactive; or return + # Neovim # @category Editors # @desc nvim diff --git a/conf.d/auto-pull.fish b/conf.d/auto-pull.fish index 852559a..db4f7f5 100644 --- a/conf.d/auto-pull.fish +++ b/conf.d/auto-pull.fish @@ -13,6 +13,11 @@ # # Manage the registry with: auto-pull add / remove / list / status +# Registers an --on-variable PWD handler that backgrounds a git fetch. In a +# script that cd's, that is both wasted work and AGENTS.md Task #4's +# credential-prompt hazard fired from a background job. +status is-interactive; or return + # C2 guard: when auto-execution is disabled, do not register the handler. __fish_config_op_enabled (status basename); or exit diff --git a/conf.d/bash_expands.fish b/conf.d/bash_expands.fish index d81dc91..89c5bf7 100644 --- a/conf.d/bash_expands.fish +++ b/conf.d/bash_expands.fish @@ -7,6 +7,10 @@ # Provides bash-style history expansion functions for abbreviations. # These functions are gated by the C3 overrides switch. +# The six expand_* functions are reachable only through abbr --function +# (conf.d/abbr.fish:677-697), i.e. only during interactive expansion. +status is-interactive; or return + # Execute expand_bang_all function expand_bang_all --description 'Execute expand_bang_all' # Opinionated guard (C3): no expansion when overrides are disabled. diff --git a/conf.d/key_bindings.fish b/conf.d/key_bindings.fish index 588e073..e6143ab 100644 --- a/conf.d/key_bindings.fish +++ b/conf.d/key_bindings.fish @@ -47,6 +47,10 @@ # This allows for rapid-fire math without leaving the current shell. # ────────────────────────────────────────────────────────────────────── +# Defines only fish_user_key_bindings, which fish calls from the interactive +# reader and nowhere else. +status is-interactive; or return + function fish_user_key_bindings # Custom key chords are opinionated (C3 overrides); skip them entirely diff --git a/conf.d/logging-events.fish b/conf.d/logging-events.fish index 892f518..53e1287 100644 --- a/conf.d/logging-events.fish +++ b/conf.d/logging-events.fish @@ -13,6 +13,12 @@ # solely in functions/ are never registered and their --on-variable triggers # never fire. +# Calls __fish_config_sync_logging at every shell start, which mkdir+touches +# the C5 sentinel on disk. Its only consumers — the Kitty watcher and the +# paru/yay wrappers — are interactive-context; every interactive shell still +# refreshes it. +status is-interactive; or return + function __fish_config_logging_changed --on-variable __fish_config_op_logging \ --description 'C5 event handler: sync logging state when __fish_config_op_logging changes' __fish_config_sync_logging diff --git a/conf.d/paru-wrapper.fish b/conf.d/paru-wrapper.fish index bef1e54..e5e629e 100644 --- a/conf.d/paru-wrapper.fish +++ b/conf.d/paru-wrapper.fish @@ -10,6 +10,10 @@ # site paru-autoexec: autoexec/pkg-wrappers # site paru-logging: logging/pkg-logs +# Defines nothing; its only effect is generating ~/.local/bin/paru, an +# idempotent write every interactive session already performs. +status is-interactive; or return + # Auto-generating a wrapper in ~/.local/bin is opinionated (C2 auto-exec). # Wrapper generation is also gated by C5 (Logging & Capture). __fish_config_op_enabled (status basename) paru-autoexec; or return diff --git a/conf.d/starship.fish b/conf.d/starship.fish index fc9673e..adee42b 100644 --- a/conf.d/starship.fish +++ b/conf.d/starship.fish @@ -9,6 +9,10 @@ # Without starship, fish's built-in prompt already emits OSC 133;A # on the prompt line itself, so no wrapper is needed. +# Defines fish_prompt; no script renders a prompt. Checked before the +# op-guard so the builtin short-circuits ahead of three function autoloads. +status is-interactive; or return + # Replacing the prompt is opinionated (C3 overrides) __fish_config_op_enabled (status basename); or return diff --git a/conf.d/theme.fish b/conf.d/theme.fish index 5944728..26bc834 100644 --- a/conf.d/theme.fish +++ b/conf.d/theme.fish @@ -20,6 +20,11 @@ if not __fish_config_op_enabled (status basename) return end +# Below the cleanup block on purpose: that branch erases a stale universal +# FZF_DEFAULT_OPTS and must keep running wherever it runs today. Everything +# past here is fish_color_* for the syntax highlighter, interactive-only. +status is-interactive; or return + # ────────────────────── Syntax highlighting colors ────────────────────── set --global fish_color_autosuggestion 6c7086 set --global fish_color_cancel f38ba8 diff --git a/conf.d/tricks.fish b/conf.d/tricks.fish index a3a5474..6c82fb1 100644 --- a/conf.d/tricks.fish +++ b/conf.d/tricks.fish @@ -18,20 +18,32 @@ if test -f ~/.fish_profile source ~/.fish_profile end -# Append unique directories to $PATH (fish_add_path handles duplicates automatically) -fish_add_path ~/.local/bin -fish_add_path ~/Applications/depot_tools +# This file is sourced twice per shell on CachyOS (once by the conf.d +# autoload, once forced by config.fish to re-win over the distro's own +# tricks.fish). The PATH/MANPAGER setup below doesn't need to repeat on +# the second pass -- only the functions/aliases further down do, since +# those are what re-assert over the distro config. Gate the expensive +# calls (fish_add_path, type -q bat) behind a once-per-session guard. +# ponytail: per-session guard, not per-value; if this file grows more +# expensive one-time setup, extend the same guard rather than adding more. +if not set -q __fish_config_tricks_env_applied + set -g __fish_config_tricks_env_applied 1 -# Expose user-local man pages -if not contains ~/.local/share/man $MANPATH - set -gx MANPATH ~/.local/share/man $MANPATH -end + # Append unique directories to $PATH (fish_add_path handles duplicates automatically) + fish_add_path ~/.local/bin + fish_add_path ~/Applications/depot_tools -# Format man pages using bat (only if bat is installed) -# Overriding $MANPAGER is opinionated (C3 overrides) -if type -q bat; and __fish_config_op_enabled (status basename) tricks-manpager - set -gx MANROFFOPT -c - set -gx MANPAGER "sh -c 'col -bx | bat -l man -p'" + # Expose user-local man pages + if not contains ~/.local/share/man $MANPATH + set -gx MANPATH ~/.local/share/man $MANPATH + end + + # Format man pages using bat (only if bat is installed) + # Overriding $MANPAGER is opinionated (C3 overrides) + if type -q bat; and __fish_config_op_enabled (status basename) tricks-manpager + set -gx MANROFFOPT -c + set -gx MANPAGER "sh -c 'col -bx | bat -l man -p'" + end end # Set settings for https://github.com/franciscolourenco/done diff --git a/conf.d/wakatime.fish b/conf.d/wakatime.fish index db97a43..e4568bf 100644 --- a/conf.d/wakatime.fish +++ b/conf.d/wakatime.fish @@ -9,6 +9,10 @@ # site wakatime-autoexec: autoexec/telemetry # site wakatime-hook: integrations/notifications +# Registers a fish_postexec handler; that event is emitted only by the +# interactive reader, so the handler is dead weight in a script. +status is-interactive; or return + # Local modification: opinionated guard (AGENTS.md Task #3). WakaTime # reporting is classified under both C2 auto-execution and C4 integrations; # disabling either category skips registering the hook. diff --git a/conf.d/yay-wrapper.fish b/conf.d/yay-wrapper.fish index 53da589..6f43360 100644 --- a/conf.d/yay-wrapper.fish +++ b/conf.d/yay-wrapper.fish @@ -10,6 +10,10 @@ # site yay-autoexec: autoexec/pkg-wrappers # site yay-logging: logging/pkg-logs +# Defines nothing; its only effect is generating ~/.local/bin/yay, an +# idempotent write every interactive session already performs. +status is-interactive; or return + # Auto-generating a wrapper in ~/.local/bin is opinionated (C2 auto-exec). # Wrapper generation is also gated by C5 (Logging & Capture). __fish_config_op_enabled (status basename) yay-autoexec; or return diff --git a/docs/fish-config.md b/docs/fish-config.md index 2dccaed..5edcf39 100644 --- a/docs/fish-config.md +++ b/docs/fish-config.md @@ -46,7 +46,6 @@ The configuration uses a structured file tree: ├── conf.d/ │ ├── abbr.fish All abbreviations │ ├── autopair.fish Auto-pair brackets and quotes - │ ├── cheat.fish cheat.sh tab completions │ ├── done.fish Desktop notifications for long commands │ ├── first_run.fish One-time init: Fisher bootstrap, theme │ ├── key_bindings.fish Custom key bindings and Vi mode @@ -58,14 +57,13 @@ The configuration uses a structured file tree: │ ├── zellij-logging.fish C5 fish_exit handler for zellij │ ├── sponge_privacy.fish Sponge privacy patterns │ ├── starship.fish fish_prompt shell-integration markers - │ ├── tailscale.fish Tailscale CLI tab completions │ ├── theme.fish Catppuccin syntax highlight colors │ ├── tricks.fish PATH, bang-bang helpers, bat man pages │ ├── wakatime.fish WakaTime shell hook │ ├── yay-wrapper.fish Auto-generates yay logging wrapper │ └── zoxide.fish Zoxide z/zi integration; overrides cd ├── functions/ Custom functions, one per file - ├── completions/ Tab completion scripts + ├── completions/ Tab completion scripts, autoloaded on demand ├── integrations/ │ └── fzf.fish FZF Catppuccin theme and key bindings ├── scripts/ @@ -245,7 +243,8 @@ Every shell command is reported to WakaTime for time-tracking. Set ### Tailscale -Full tab completion for the `tailscale` CLI is provided via `conf.d/tailscale.fish`. +Full tab completion for the `tailscale` CLI is provided via +`completions/tailscale.fish`, autoloaded on the first `tailscale`. ### Done Notifications diff --git a/docs/manual/01-configuration-variables.md b/docs/manual/01-configuration-variables.md index f37299d..b898cbd 100644 --- a/docs/manual/01-configuration-variables.md +++ b/docs/manual/01-configuration-variables.md @@ -118,7 +118,8 @@ Every shell command is reported to WakaTime for time-tracking. Set ### Tailscale -Full tab completion for the `tailscale` CLI is provided via `conf.d/tailscale.fish`. +Full tab completion for the `tailscale` CLI is provided via +`completions/tailscale.fish`, autoloaded on the first `tailscale`. ### Done Notifications diff --git a/docs/manual/index.md b/docs/manual/index.md index 3017947..5c4d402 100644 --- a/docs/manual/index.md +++ b/docs/manual/index.md @@ -35,7 +35,6 @@ The configuration uses a structured file tree: ├── conf.d/ │ ├── abbr.fish All abbreviations │ ├── autopair.fish Auto-pair brackets and quotes - │ ├── cheat.fish cheat.sh tab completions │ ├── done.fish Desktop notifications for long commands │ ├── first_run.fish One-time init: Fisher bootstrap, theme │ ├── key_bindings.fish Custom key bindings and Vi mode @@ -47,14 +46,13 @@ The configuration uses a structured file tree: │ ├── zellij-logging.fish C5 fish_exit handler for zellij │ ├── sponge_privacy.fish Sponge privacy patterns │ ├── starship.fish fish_prompt shell-integration markers - │ ├── tailscale.fish Tailscale CLI tab completions │ ├── theme.fish Catppuccin syntax highlight colors │ ├── tricks.fish PATH, bang-bang helpers, bat man pages │ ├── wakatime.fish WakaTime shell hook │ ├── yay-wrapper.fish Auto-generates yay logging wrapper │ └── zoxide.fish Zoxide z/zi integration; overrides cd ├── functions/ Custom functions, one per file - ├── completions/ Tab completion scripts + ├── completions/ Tab completion scripts, autoloaded on demand ├── integrations/ │ └── fzf.fish FZF Catppuccin theme and key bindings ├── scripts/ diff --git a/functions/__fish_config_sync_logging.fish b/functions/__fish_config_sync_logging.fish index 2d61e86..493a383 100644 --- a/functions/__fish_config_sync_logging.fish +++ b/functions/__fish_config_sync_logging.fish @@ -28,84 +28,38 @@ function __fish_config_sync_logging --description 'Sync C5 logging state: sentin set config_home "$HOME/.config" end set -l sentinel "$config_home/fish/.logging_disabled" - set -l paru_wrapper "$HOME/.local/bin/paru" - set -l yay_wrapper "$HOME/.local/bin/yay" - set -l wrapper_version 1 if __fish_config_op_enabled (status current-function) - # Logging enabled: remove sentinel and regenerate wrappers if binaries exist + # Logging enabled: remove sentinel rm -f $sentinel # Restart tmux pipe-pane for the current pane if inside tmux _tmux_pipe_log - - if test -x /usr/bin/paru - mkdir -p (dirname $paru_wrapper) - printf '%s\n' \ - '#!/usr/bin/env bash' \ - "# paru-wrapper-version: $wrapper_version" \ - '# Auto-generated by conf.d/paru-wrapper.fish — do not edit by hand.' \ - '# Tees paru output to a timestamped log file and prunes old ones.' \ - 'set -o pipefail' \ - '' \ - 'log_dir="${SCROLLBACK_HISTORY_DIR:-$HOME/.terminal_history}"' \ - 'mkdir -p "$log_dir"' \ - 'log_file="$log_dir/paru_$(date +%Y-%m-%d_%H-%M-%S).log"' \ - '' \ - '/usr/bin/paru "$@" 2>&1 | tee "$log_file"' \ - '' \ - 'max_files="${SCROLLBACK_HISTORY_MAX_FILES:-100}"' \ - 'mapfile -t logs < <(ls -1t "$log_dir"/paru_*.log 2>/dev/null)' \ - 'excess=$(( ${#logs[@]} - max_files ))' \ - 'for (( i = ${#logs[@]} - 1; i >= ${#logs[@]} - excess && i >= 0; i-- )); do' \ - ' rm -f "${logs[$i]}"' \ - 'done' \ - >$paru_wrapper - chmod +x $paru_wrapper - end - - if test -x /usr/bin/yay - mkdir -p (dirname $yay_wrapper) - printf '%s\n' \ - '#!/usr/bin/env bash' \ - "# yay-wrapper-version: $wrapper_version" \ - '# Auto-generated by conf.d/yay-wrapper.fish — do not edit by hand.' \ - '# Tees yay output to a timestamped log file and prunes old ones.' \ - 'set -o pipefail' \ - '' \ - 'log_dir="${SCROLLBACK_HISTORY_DIR:-$HOME/.terminal_history}"' \ - 'mkdir -p "$log_dir"' \ - 'log_file="$log_dir/yay_$(date +%Y-%m-%d_%H-%M-%S).log"' \ - '' \ - '/usr/bin/yay "$@" 2>&1 | tee "$log_file"' \ - '' \ - 'max_files="${SCROLLBACK_HISTORY_MAX_FILES:-100}"' \ - 'mapfile -t logs < <(ls -1t "$log_dir"/yay_*.log 2>/dev/null)' \ - 'excess=$(( ${#logs[@]} - max_files ))' \ - 'for (( i = ${#logs[@]} - 1; i >= ${#logs[@]} - excess && i >= 0; i-- )); do' \ - ' rm -f "${logs[$i]}"' \ - 'done' \ - >$yay_wrapper - chmod +x $yay_wrapper - end else - # Logging disabled: create sentinel and remove any generated wrappers + # Logging disabled: create sentinel mkdir -p (dirname $sentinel) touch $sentinel - if test -f $paru_wrapper - and grep -q "# paru-wrapper-version:" $paru_wrapper 2>/dev/null - rm -f $paru_wrapper - end - - if test -f $yay_wrapper - and grep -q "# yay-wrapper-version:" $yay_wrapper 2>/dev/null - rm -f $yay_wrapper - end - # Stop tmux pipe-pane for the current pane if inside tmux if set -q TMUX tmux pipe-pane 2>/dev/null end end + + # Delegate paru/yay wrapper (re)generation and removal to the canonical + # generators. They resolve the real binary via __fish_real_command + # (never /usr/bin-assumed) and independently gate on their own C2/C5 + # keys, so sourcing them here covers both the enabled-regenerate and + # disabled-remove cases without duplicating that logic. Previously this + # function carried its own inferior copy (tee instead of a PTY, no + # progress-bar rendering, hard-coded /usr/bin/paru|yay), which fought + # the canonical generator for the wrapper file on every version-marker + # mismatch. See startup-latency-JOB-BRIEF-FINDINGS.md §2. + # + # Routed through _fish_source_scoped: both files `return` early on + # several guard checks, and a sourced `return` exits the *calling* + # function, which would otherwise abort this function and skip + # whichever of paru/yay hadn't run yet. + _fish_source_scoped "$__fish_config_dir/conf.d/paru-wrapper.fish" + _fish_source_scoped "$__fish_config_dir/conf.d/yay-wrapper.fish" end diff --git a/functions/_fish_source_scoped.fish b/functions/_fish_source_scoped.fish new file mode 100644 index 0000000..41446a8 --- /dev/null +++ b/functions/_fish_source_scoped.fish @@ -0,0 +1,26 @@ +# Copyright (C) 2026 Rootiest +# SPDX-License-Identifier: AGPL-3.0-or-later + +# SYNOPSIS +# _fish_source_scoped +# +# DESCRIPTION +# Sources inside its own function-call boundary. `source` runs +# in the caller's own scope, so a bare `return` in a sourced file -- +# used by several conf.d guards as an early exit -- would otherwise +# unwind whatever function called `source` directly, not just the +# sourced file. Calling through this helper contains it to here. +# +# ARGUMENTS +# file Path to the fish script to source +# +# EXIT STATUS +# 0 File does not exist (nothing to do) +# Exit status of the sourced file otherwise +# +# EXAMPLE +# _fish_source_scoped $__fish_config_dir/conf.d/paru-wrapper.fish +function _fish_source_scoped --argument-names file + test -f $file; or return 0 + source $file +end diff --git a/tests/lib.fish b/tests/lib.fish new file mode 100644 index 0000000..2edfc91 --- /dev/null +++ b/tests/lib.fish @@ -0,0 +1,62 @@ +#!/usr/bin/env fish +# Copyright (C) 2026 Rootiest +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# Shared assertion and reporting core for tests/test-*.fish. +# +# One assertion: `check