first pass of merging in warp (doesn't build)

This commit is contained in:
Ryan Ward
2026-07-01 16:08:58 -05:00
parent 2f64909469
commit 4770ac06b5
3662 changed files with 414574 additions and 89772 deletions
+144 -30
View File
@@ -21,8 +21,8 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
# Appended to $DCS_START to signal that the following message is JSON-encoded.
DCS_JSON_MARKER="d"
# Byte used to signal the end of a DCS.
DCS_END="$(printf '\x9c')"
# Byte sequence used to signal the end of a DCS (7-bit ST: ESC \).
DCS_END="$(printf '\x1b\x5c')"
# OSC used to mark the start of in-band command output.
#
@@ -168,14 +168,14 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
local -a command
command=("${@:2}")
# Declare raw_output prior to actually assigning it, because `local` is a command itself, which
# inteferes with capturing the exit code via $? (it overwrites $? with the 0, because the
# interferes with capturing the exit code via $? (it overwrites $? with the 0, because the
# 'local' command always succeeds).
local raw_output
# Command substitution only captures stdout, so redirect stderr to stdout.
# Note that we use `eval` here to actually execute the command, because some shell syntax
# that may be used in the command might not be valid in a command substitution (e.g. the
# '$(<command>)' syntax).
# Also note that zsh variables can contain null charcters, so this doesn't require any special
# Also note that zsh variables can contain null characters, so this doesn't require any special
# handling.
raw_output=$(eval "$command" 2>&1)
local exit_code=$?
@@ -253,7 +253,7 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
# invoke any external commands in here.
warp_preexec () {
local warp_escaped_command="$(warp_escape_json $1)"
warp_send_json_message "{\"hook\": \"Preexec\", \"value\": {\"command\": \"$warp_escaped_command\"}}"
warp_send_json_message "{\"hook\": \"Preexec\", \"value\": {\"command\": \"$warp_escaped_command\", \"session_id\": $WARP_SESSION_ID}}"
warp_maybe_send_reset_grid_osc
# If this preexec is called for user command, kill ongoing generator command jobs and clean
@@ -277,7 +277,7 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
# If the array is not empty, kill the ongoing pids.
if [[ ! -z $pids ]]; then
# Surpress stderr output; kill writes to stderr if any of the given
# Suppress stderr output; kill writes to stderr if any of the given
# PIDS are not running (which might rarely be the case due to race
# conditions in checking which PIDS to cancel and this kill command.
(kill -9 $pids 2>&1) >/dev/null
@@ -306,8 +306,9 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
# previously run user command (as opposed to any of the commands executed
# in this function below).
local exit_code=$?
local next_block_id="precmd-$WARP_SESSION_ID-$((block_id++))"
warp_send_json_message "{\"hook\": \"CommandFinished\", \"value\": {\"exit_code\": $exit_code, \"next_block_id\": \"precmd-$WARP_SESSION_ID-$((block_id++))\"}}"
warp_send_json_message "{\"hook\": \"CommandFinished\", \"value\": {\"exit_code\": $exit_code, \"next_block_id\": \"$next_block_id\", \"session_id\": $WARP_SESSION_ID}}"
warp_maybe_send_reset_grid_osc
# If this is being called for a generator command, short circuit and send an unpopulated
@@ -320,6 +321,8 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
_WARP_GENERATOR_COMMAND=""
warp_send_json_message "{\"hook\": \"Precmd\", \"value\": {
\"exit_code\": $exit_code,
\"next_block_id\": \"$next_block_id\",
\"pwd\": \"\",
\"ps1\": \"\",
\"git_head\": \"\",
@@ -394,37 +397,56 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
escaped_conda_env=$(warp_escape_json $CONDA_DEFAULT_ENV)
fi
# Get Node.js version if node is available and we're in a Node.js project
if command -v node > /dev/null 2>&1; then
# Get the Node.js version, but only when the Node.js Version chip is enabled.
# Warp sets WARP_PROMPT_NODE_VERSION_ENABLED to "0" when the chip is not in the
# prompt (defaulting to enabled when unset), so we avoid spawning `node` on
# every prompt when the chip is not shown.
if [[ "$WARP_PROMPT_NODE_VERSION_ENABLED" != "0" ]] && command -v node > /dev/null 2>&1; then
# Check for package.json in current directory and parent directories
local current_dir="$PWD"
local found_package_json=false
local package_json_dir=""
while [[ "$current_dir" != "/" ]]; do
while [[ -n "$current_dir" ]]; do
if [[ -f "$current_dir/package.json" ]]; then
found_package_json=true
package_json_dir="$current_dir"
break
fi
current_dir=$(dirname "$current_dir")
[[ "$current_dir" == "/" ]] && break
# Strip the last path segment without spawning `dirname`.
current_dir="${current_dir%/*}"
[[ -z "$current_dir" ]] && current_dir="/"
done
# Only show node version if package.json is within a git repository
if [[ "$found_package_json" = true ]]; then
local git_dir="$package_json_dir"
local in_git_repo=false
while [[ "$git_dir" != "/" ]]; do
while [[ -n "$git_dir" ]]; do
if [[ -d "$git_dir/.git" ]]; then
in_git_repo=true
break
fi
git_dir=$(dirname "$git_dir")
[[ "$git_dir" == "/" ]] && break
git_dir="${git_dir%/*}"
[[ -z "$git_dir" ]] && git_dir="/"
done
if [[ "$in_git_repo" = true ]]; then
local node_version=$(node --version 2>/dev/null)
if [[ -n "$node_version" ]]; then
escaped_node_version=$(warp_escape_json "$node_version")
# Cache the resolved version keyed on PWD + PATH so we only spawn
# `node --version` when the directory or PATH changes (PATH changes
# on `nvm use`). The cache vars are global (no `local`) so they
# persist across precmd invocations.
local node_cache_key="$PWD:$PATH"
if [[ "$node_cache_key" == "$_WARP_NODE_VERSION_CACHE_KEY" ]]; then
escaped_node_version="$_WARP_NODE_VERSION_CACHE_VALUE"
else
local node_version=$(node --version 2>/dev/null)
if [[ -n "$node_version" ]]; then
escaped_node_version=$(warp_escape_json "$node_version")
fi
_WARP_NODE_VERSION_CACHE_KEY="$node_cache_key"
_WARP_NODE_VERSION_CACHE_VALUE="$escaped_node_version"
fi
fi
fi
@@ -461,6 +483,8 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
fi
local escaped_json="{\"hook\": \"Precmd\", \"value\": {
\"exit_code\": $exit_code,
\"next_block_id\": \"$next_block_id\",
\"pwd\": \"$escaped_pwd\",
\"ps1\": \"\",
\"honor_ps1\": $honor_ps1,
@@ -622,19 +646,19 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
function warp_report_input {
local escaped_input="$(warp_escape_json "$BUFFER")"
warp_send_json_message "{ \"hook\": \"InputBuffer\", \"value\": { \"buffer\": \"$escaped_input\" } }"
warp_send_json_message "{ \"hook\": \"InputBuffer\", \"value\": { \"buffer\": \"$escaped_input\", \"session_id\": $WARP_SESSION_ID } }"
# This prevents zsh from printing typeahead as background output after we've fetched it.
BUFFER=""
}
zle -N warp_report_input
function clear() {
warp_send_json_message "{\"hook\": \"Clear\", \"value\": {}}"
warp_send_json_message "{\"hook\": \"Clear\", \"value\": {\"session_id\": $WARP_SESSION_ID}}"
}
function warp_finish_update {
local update_id="$1"
warp_send_json_message "{ \"hook\": \"FinishUpdate\", \"value\": { \"update_id\": \"$update_id\"} }"
warp_send_json_message "{ \"hook\": \"FinishUpdate\", \"value\": { \"update_id\": \"$update_id\", \"session_id\": $WARP_SESSION_ID} }"
}
# Check if the warp apt source file has been renamed to `warpdotdev.list.distUpgrade` due to an ubuntu version update.
@@ -661,6 +685,21 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
fi
}
# Strips prompt constructs that zsh counts as visible "glitch" columns even
# when they appear inside a %{...%} zero-width region, returning the result
# in $REPLY. The explicit-width form %n{ is rewritten to %{ (preserving the
# brace pairing), and the %G, %nG, and %-nG forms are removed entirely.
# Neither change affects the rendered prompt bytes, only zsh's internal
# width accounting. Literal %% escapes are matched first so that they cannot
# form false positives (e.g. %%1{ renders as literal text and must be left
# alone).
function warp_strip_glitch_width_constructs() {
setopt localoptions extendedglob
local match mbegin mend
REPLY=${1:-}
REPLY=${REPLY//(#b)(%%|%<->\{|%(-|)(<->|)G)/${${match[1]:#%(-|)(<->|)G}/(#s)%<->\{(#e)/%\{}}
}
# Check whether the prompt-related variables have OSC prompt marker sequences,
# and if not, wrap them with the appropriate markers so that we can direct the
# prompt bytes to the appropriate grids.
@@ -712,7 +751,7 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
# markers. If they exist, we remove the first occurrence of the prefix
# and the last occurrence of the suffix, which should be the ones that
# Warp has added, to avoid duplicating the prefix and suffix. Shell
# parameter expansion is used to remove the first and last occurences.
# parameter expansion is used to remove the first and last occurrences.
# Specifically note that virtualenvs can add content to the prompt, so we need to
# remove the markers before re-adding them.
# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
@@ -742,7 +781,21 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
PROMPT=$preceding_suffix$following_suffix
fi
ORIGINAL_PROMPT=$PROMPT
# If the prompt we extracted is exactly the glitch-stripped value that we
# installed on a previous refresh, keep the existing ORIGINAL_PROMPT: it
# holds the pristine value, whose width annotations are still needed if
# we later switch to honoring the PS1.
if [[ "$PROMPT" != "${WARP_STRIPPED_ORIGINAL_PROMPT:-}" ]]; then
if [[ -n "${WARP_STRIPPED_ORIGINAL_PROMPT:-}" && "$PROMPT" == *"$WARP_STRIPPED_ORIGINAL_PROMPT"* ]]; then
# Another hook added content around the stripped prompt that we
# installed (e.g. a virtualenv prefix). Rehydrate the stripped
# portion back to its pristine value before saving, so that the
# width annotations survive alongside the added content.
ORIGINAL_PROMPT=${PROMPT//$WARP_STRIPPED_ORIGINAL_PROMPT/$ORIGINAL_PROMPT}
else
ORIGINAL_PROMPT=$PROMPT
fi
fi
PROMPT="$prompt_prefix$PROMPT$suffix"
fi
@@ -762,14 +815,26 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
# If we are using the Warp prompt, we pass a "hidden left prompt" to the prompt
# preview grid (the hidden prompt grid) with cursor markers surrounding the entire prompt.
if [[ "$WARP_HONOR_PS1" != "1" ]]; then
if [[ "$PROMPT" != "%{$prompt_prefix$ORIGINAL_PROMPT$suffix%}" ]]; then
# Even though the entire prompt is surrounded by cursor markers below,
# zsh still counts explicit-width constructs (%n{...%} and %G) within it
# as visible "glitch" columns. Since the prompt is routed to the hidden
# prompt grid and occupies zero columns of the combined prompt/command
# grid, any nonzero counted width desyncs zle's internal cursor position
# from the physical one, which corrupts partial redraws of the command
# (e.g. when zsh-syntax-highlighting recolors individual tokens). Strip
# those constructs before wrapping; this only changes zsh's width
# accounting, never the rendered prompt bytes.
local REPLY
warp_strip_glitch_width_constructs "$ORIGINAL_PROMPT"
WARP_STRIPPED_ORIGINAL_PROMPT=$REPLY
if [[ "$PROMPT" != "%{$prompt_prefix$WARP_STRIPPED_ORIGINAL_PROMPT$suffix%}" ]]; then
# We purposefully surround this entire prompt with cursor markers to prevent
# the shell from moving its internal state of the cursor position, for purposes
# of printing the command with the Warp prompt.
# Note that the Warp prompt is always ABOVE the combined grid in finished blocks
# (same line prompt only affects the input editor with Warp prompt, not
# finished blocks).
PROMPT="%{$prompt_prefix$ORIGINAL_PROMPT$suffix%}"
PROMPT="%{$prompt_prefix$WARP_STRIPPED_ORIGINAL_PROMPT$suffix%}"
fi
# Otherwise, if we are using the PS1, we use the normal prompt markers.
else
@@ -780,7 +845,9 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
fi
fi
if [[ "${RPROMPT:-}" != "%{"*"%}" ]]; then
# Do not synthesize an empty right prompt. Even without visible content, zsh reserves
# right-prompt layout space and may corrupt wrapped command redraws in the command grid.
if [[ -n "${RPROMPT:-}" && "${RPROMPT:-}" != "%{"*"%}" ]]; then
RPROMPT="%{${RPROMPT:-}%}"
fi
@@ -864,10 +931,50 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
}
function warp_ssh_helper() {
local remote_session_id=$(command -p od -An -N8 -tu8 /dev/urandom 2>/dev/null | command -p tr -d ' \n')
if [[ -z "$remote_session_id" || "$remote_session_id" == "0" ]]; then
# If we cannot generate a non-zero random token, run plain SSH instead.
command ssh "${@:1}"
return
fi
# Hex-encode the ZSH environment script we use to bootstrap remote zsh b/c it contains control characters
# We decode on the SSH server using xxd if its available, otherwise fall back to a for-loop over each byte
# and use printf to convert back to plaintext
local zsh_env_script=$(printf '%s' 'unsetopt ZLE; unset RCS; unset GLOBAL_RCS; WARP_SESSION_ID="$(command -p date +%s)$RANDOM"; WARP_USING_WINDOWS_CON_PTY=@@USING_CON_PTY_BOOLEAN@@; _hostname=$(command -pv hostname >/dev/null 2>&1 && command -p hostname 2>/dev/null || command -p uname -n); _user=$(command -pv whoami >/dev/null 2>&1 && command -p whoami 2>/dev/null || echo $USER); _msg=$(printf "{\"hook\": \"InitShell\", \"value\": {\"session_id\": $WARP_SESSION_ID, \"shell\": \"zsh\", \"user\": \"%s\", \"hostname\": \"%s\"}}" "$_user" "$_hostname" | command -p od -An -v -tx1 | command -p tr -d '"'"' \n'"'"'); printf '"'"'\e]9278;d;%s\x07'"'"' $_msg; unset _hostname _user _msg' | command -p od -An -v -tx1 | command -p tr -d ' \n')
local zsh_env_script=$(printf '%s' 'unsetopt ZLE; unset RCS; unset GLOBAL_RCS; WARP_SESSION_ID='$remote_session_id'; WARP_USING_WINDOWS_CON_PTY=@@USING_CON_PTY_BOOLEAN@@; _hostname=$(command -pv hostname >/dev/null 2>&1 && command -p hostname 2>/dev/null || command -p uname -n); _user=$(command -pv whoami >/dev/null 2>&1 && command -p whoami 2>/dev/null || echo $USER); _msg=$(printf "{\"hook\": \"InitShell\", \"value\": {\"session_id\": $WARP_SESSION_ID, \"shell\": \"zsh\", \"user\": \"%s\", \"hostname\": \"%s\"}}" "$_user" "$_hostname" | command -p od -An -v -tx1 | command -p tr -d '"'"' \n'"'"'); printf '"'"'\e]9278;d;%s\x07'"'"' $_msg; unset _hostname _user _msg' | command -p od -An -v -tx1 | command -p tr -d ' \n')
# Optionally attach to an existing ControlMaster the user already
# runs for this destination instead of creating our own. Resolve
# the user's configured ControlPath with `ssh -G` (which expands
# tokens like %h/%p/%r/%C into a literal path), then verify the
# master is alive with `ssh -O check`. Both probes are local-only
# commands. On any failure we fall back to creating a Warp-owned
# master, preserving the existing behavior.
local control_path="$SSH_SOCKET_DIR/$WARP_SESSION_ID"
local control_master_mode="yes"
local external_control_master="false"
if [[ "$WARP_SSH_REUSE_CONTROL_MASTER" == "1" ]]; then
local user_control_path=$(command ssh -G "${@:1}" 2>/dev/null | command -p sed -n 's/^controlpath //p')
case "$user_control_path" in
"" | none)
# No ControlPath configured for this destination.
;;
*[![:alnum:]._/~@:+,-]*)
# The resolved path contains characters we cannot safely
# embed in the SSH hook JSON below (e.g. an unexpanded %
# token, quotes, or whitespace); fall back to a
# Warp-owned master.
;;
*)
if command ssh -O check -o ControlPath="$user_control_path" "${@:1}" >/dev/null 2>&1; then
# A live master exists: multiplex through it and let
# the client know Warp does not own it.
control_path="$user_control_path"
control_master_mode="no"
external_control_master="true"
fi
;;
esac
fi
# Keep remote commands up-to-date with shell.rs & bash.sh.
# Note that in this command, we're passing a string to the remote shell. Any variable expansions need to be
@@ -876,7 +983,7 @@ if [[ -z $WARP_BOOTSTRAPPED ]]; then
# determine what shell is the login shell on the remote machine. We perform a preliminary check to see if
# the remote shell is the Bourne shell to avoid asking it to parse later lines that use syntax it doesn't
# support.
command ssh -o ControlMaster=yes -o ControlPath=$SSH_SOCKET_DIR/$WARP_SESSION_ID \
command ssh -o ControlMaster=$control_master_mode -o ControlPath="$control_path" \
-t "${@:1}" \
"
export TERM_PROGRAM='WarpTerminal'
@@ -887,7 +994,7 @@ export WARP_IS_SSH='1'
test -n '$WARP_CLIENT_VERSION' && export WARP_CLIENT_VERSION='$WARP_CLIENT_VERSION'
# Only forward the protocol version if it was set locally (i.e. the HOANotifications feature flag is on).
test -n '$WARP_CLI_AGENT_PROTOCOL_VERSION' && export WARP_CLI_AGENT_PROTOCOL_VERSION='$WARP_CLI_AGENT_PROTOCOL_VERSION'
hook="'$(printf "{\"hook\": \"SSH\", \"value\": {\"socket_path\": \"'$SSH_SOCKET_DIR/$WARP_SESSION_ID'\", \"remote_shell\": \"%s\"}}" "${SHELL##*/}" | command -p od -An -v -tx1 | command -p tr -d " \n")'"
hook="'$(printf "{\"hook\": \"SSH\", \"value\": {\"socket_path\": \"'$control_path'\", \"remote_shell\": \"%s\", \"session_id\": '"$WARP_SESSION_ID"', \"remote_session_id\": '"$remote_session_id"', \"external_control_master\": '"$external_control_master"'}}" "${SHELL##*/}" | command -p od -An -v -tx1 | command -p tr -d " \n")'"
printf '$OSC_START$DCS_JSON_MARKER$OSC_PARAM_SEPARATOR%s$OSC_END' "'$hook'"
if test "'"${SHELL##*/}" != "bash" -a "${SHELL##*/}" != "zsh"'"; then
@@ -922,7 +1029,7 @@ case "'${SHELL##*/}'" in
command -p stty raw
HISTCONTROL=ignorespace
HISTIGNORE=" *"
WARP_SESSION_ID="$(command -p date +%s)$RANDOM"
WARP_SESSION_ID='$remote_session_id'
WARP_HONOR_PS1="'$WARP_HONOR_PS1'"
_hostname=$(command -pv hostname >/dev/null 2>&1 && command -p hostname 2>/dev/null || command -p uname -n)
_user=$(command -pv whoami >/dev/null 2>&1 && command -p whoami 2>/dev/null || echo $USER)
@@ -955,7 +1062,7 @@ esac
function ssh() {
if is_interactive_ssh_session "$@"; then
warp_send_json_message "{\"hook\": \"PreInteractiveSSHSession\", \"value\": {}}"
warp_send_json_message "{\"hook\": \"PreInteractiveSSHSession\", \"value\": {\"session_id\": $WARP_SESSION_ID}}"
# If the SSH wrapper is not enabled for this session, don't use it.
if [ "$WARP_USE_SSH_WRAPPER" = "1" ]; then
@@ -1169,6 +1276,12 @@ esac
fi
fi
# Restore the built-in bracketed-paste widget. This works around a buggy interaction we observed
# with the bracketed-paste-magic plugin (included in oh-my-zsh by default), zsh's "allexport"
# option (set -a), and Warp's bootstrapping code.
# https://github.com/warpdotdev/warp/issues/11520
zle -A .bracketed-paste bracketed-paste
precmd_functions+=(warp_precmd warp_update_prompt_vars)
preexec_functions+=(warp_preexec)
@@ -1395,7 +1508,8 @@ esac
local escaped_editor="$(warp_escape_json "$EDITOR")"
local escaped_shell_path="$(warp_escape_json "${commands[zsh]}")"
local escaped_json="{\"hook\": \"Bootstrapped\", \"value\": {\"histfile\": \"$escaped_histfile\", \"shell\": \"zsh\", \"home_dir\": \"$HOME\", \"path\": \"$escaped_path\", \"editor\": \"$escaped_editor\", \"env_var_names\": \"$env_var_names\", \"abbreviations\": \"$escaped_abbrs\", \"aliases\": \"$escaped_aliases\", \"function_names\": \"$function_names\", \"builtins\": \"$escaped_builtins\", \"keywords\": \"$escaped_keywords\", \"shell_version\": \"$ZSH_VERSION\", \"shell_options\": \"$shell_options\", \"rcfiles_start_time\": \"$rcfiles_start_time\", \"rcfiles_end_time\": \"$rcfiles_end_time\", \"shell_plugins\": \"$escaped_shell_plugins\", \"os_category\": \"$os_category\", \"linux_distribution\": \"$linux_distribution\", \"wsl_name\": \"${WSL_DISTRO_NAME:-}\", \"shell_path\": \"$escaped_shell_path\"}}"
local escaped_cdpath="$(warp_escape_json "$CDPATH")"
local escaped_json="{\"hook\": \"Bootstrapped\", \"value\": {\"histfile\": \"$escaped_histfile\", \"session_id\": $WARP_SESSION_ID, \"shell\": \"zsh\", \"home_dir\": \"$HOME\", \"path\": \"$escaped_path\", \"cdpath\": \"$escaped_cdpath\", \"editor\": \"$escaped_editor\", \"env_var_names\": \"$env_var_names\", \"abbreviations\": \"$escaped_abbrs\", \"aliases\": \"$escaped_aliases\", \"function_names\": \"$function_names\", \"builtins\": \"$escaped_builtins\", \"keywords\": \"$escaped_keywords\", \"shell_version\": \"$ZSH_VERSION\", \"shell_options\": \"$shell_options\", \"rcfiles_start_time\": \"$rcfiles_start_time\", \"rcfiles_end_time\": \"$rcfiles_end_time\", \"shell_plugins\": \"$escaped_shell_plugins\", \"os_category\": \"$os_category\", \"linux_distribution\": \"$linux_distribution\", \"wsl_name\": \"${WSL_DISTRO_NAME:-}\", \"shell_path\": \"$escaped_shell_path\"}}"
warp_send_json_message "$escaped_json"
}
warp_bootstrapped