kaeru 0.1.9

Reopens your terminal tabs, and the coding agent sessions in them, after the terminal closes or the computer restarts.
# kaeru (https://github.com/gloomydumber/kaeru) keeps track of this tab, so
# that it can come back after the terminal closes. Your ~/.zshrc loads this
# with: eval "$(kaeru init zsh)"

# Everything runs in a function, so that its variables stay local and a
# problem never stops the rest of ~/.zshrc.
__kaeru_init() {
    [[ -o interactive ]] || return
    # Loaded once, also when ~/.zshrc runs again in the same shell.
    (( ${+__kaeru_cwd_file} )) && return
    typeset -g __kaeru_cwd_file= __kaeru_ask= __kaeru_last_cwd= __kaeru_tty=
    # Whether the shell has a terminal, as a tab's shell has.
    { : </dev/tty } 2>/dev/null && __kaeru_tty=1
    # kaeru resume prints the code that resumes the session, and this runs it
    # here, as if you typed it, so that no kaeru process waits while the
    # session runs. A kaeru that waits keeps its file in use, which an update
    # can't replace on Windows. Other commands run kaeru as they are.
    kaeru() {
        if (( $# != 1 )) || [[ $1 != resume ]]; then
            command kaeru "$@"
            return
        fi
        local code
        code=$(command kaeru resume --shell zsh) || return
        if [[ -n $code ]]; then
            eval "$code"
        fi
    }
    local output
    output=$(command kaeru shell start --pid $$)
    # Exit code 3: tabs are waiting, and this tab is the one that asks.
    (( $? == 3 )) && __kaeru_ask=1
    # A tab that kaeru reopened gets the code that resumes its session from
    # KAERU_RUN, which kaeru shell start has seen, and runs it at the first
    # prompt, once ~/.zshrc has set everything up. Programs started in the
    # tab don't inherit it.
    typeset -g __kaeru_run=${KAERU_RUN-}
    unset KAERU_RUN
    __kaeru_cwd_file=$output
    [[ -n $__kaeru_cwd_file ]] || return
    # Programs in this tab, such as an agent and its hook, learn the tab's
    # shell from this, for when the OS can't tell.
    export KAERU_SHELL_PID=$$
    autoload -Uz add-zsh-hook
    add-zsh-hook precmd __kaeru_precmd
}

# Writes the folder to the tab's .cwd file when it changes, and touches the
# file at every prompt, without starting a process. The file's time says when
# the prompt last showed, so that kaeru knows an agent that ran in this tab
# ended.
__kaeru_precmd() {
    # Prompt themes and other precmd functions see whether the last command
    # failed, from $?, which this function's own commands would reset.
    local last=$?
    # When the terminal goes away, it hangs up the program that runs in the
    # tab, and the shell can get here once more before it ends too. That
    # prompt never shows, so it doesn't count: an agent that the terminal
    # ended didn't end by itself.
    if [[ -n $__kaeru_tty ]] && ! { : </dev/tty } 2>/dev/null; then
        return $last
    fi
    if [[ -n $__kaeru_ask ]]; then
        # The picker waits for the user, so it shows at the first prompt and
        # not while ~/.zshrc runs.
        __kaeru_ask=
        KAERU_NEW_TAB=1 command kaeru
        # Exit code 4: every tab came back in this window, so this new tab,
        # which only showed the picker, is one too many.
        (( $? == 4 )) && exit 0
    fi
    if [[ -n $__kaeru_run ]]; then
        local code=$__kaeru_run
        __kaeru_run=
        eval "$code"
    fi
    # >| writes even when the noclobber option is on. Once the file is gone,
    # as when kaeru forgot the tab, it stays gone until the folder changes.
    if [[ $PWD != "$__kaeru_last_cwd" || -e $__kaeru_cwd_file ]]; then
        builtin pwd 2>/dev/null >|"$__kaeru_cwd_file" && __kaeru_last_cwd=$PWD
    fi
    return $last
}

__kaeru_init
unfunction __kaeru_init