// Generated by scripts/embed-hook-lib.ts from env-lib/*.sh. Do not edit. /** The session env library's files, embedded so a build needs no asset on disk. */ export const ENV_LIB_FILES: ReadonlyArray<{ readonly name: string; readonly content: string }> = [ { name: "env-run.sh", content: '# shellcheck shell=sh\n# pluginfinity session env runner. `pluginfinity build` writes this file at\n# lib/pluginfinity/env-run.sh into every target whose config declares `env`,\n# and runs it as the first SessionStart entry; do not edit a copy under builds/.\n#\n# POSIX sh. Run as a script (`sh `), with the SessionStart event on stdin.\n# Writes nothing to stdout or stderr and always exits 0: every problem is a log\n# line, and the shell\'s own job-control notices are redirected away.\n#\n# Once per SessionStart (every source) it resolves each declared name through\n# rungs 1-5 (default, setup output, /.env, /.env.local, the\n# environment this process inherited), then writes the resolved values to the\n# session values file, points the project at this session, and on Claude\n# appends `export NAME=\'value\'` lines to CLAUDE_ENV_FILE for the Bash tool.\n#\n# The setup script runs under bash with cwd the project, the hook environment\n# plus PLUGINFINITY_EVENT=SessionStart, and the event on stdin. Its stdout is\n# NAME=value lines (blank and # lines ignored, values literal, later lines\n# win); an undeclared name is skipped with a log line. It is bounded by\n# _pf_env_setup_timeout seconds: on timeout the setup and every process it\n# started are terminated (then killed after 1 s) and nothing it printed is\n# kept; on a non-zero exit its valid lines are kept. With no project (a Copilot\n# event with no cwd) setup is skipped with a log line.\n#\n# Under the session lock it keeps the names a same-id rerun finds recorded as\n# set at rung 6, writes the values, then writes the session\'s done marker.\n\n_pf_env_lib_dir=$(CDPATH=\'\' cd -- "$(dirname -- "$0")" 2>/dev/null && pwd) || exit 0\n_pf_env_manual=1\n_pf_env_component=hook\n[ -r "$_pf_env_lib_dir/env.sh" ] || exit 0\n# shellcheck source=/dev/null\n. "$_pf_env_lib_dir/env.sh" || exit 0\n[ -n "$_pf_env_names" ] || exit 0\n\n_pf_r_tmp=$(mktemp -d "${TMPDIR:-/tmp}/pluginfinity-env.XXXXXX" 2>/dev/null) || {\n\t_pf_env_log "cannot create a temporary directory; session env not resolved"\n\texit 0\n}\ntrap \'rm -rf "$_pf_r_tmp" 2>/dev/null\' EXIT\n\nif [ -t 0 ]; then\n\tprintf \'{}\' >"$_pf_r_tmp/input"\nelse\n\tcat >"$_pf_r_tmp/input" 2>/dev/null || :\nfi\n\n# A top-level string field of the event: jq when it works, else a plain match\n# that keeps escape sequences as written (a session id holding one is invalid).\n_pf_r_jq=\nif command -v jq >/dev/null 2>&1 && jq -n 1 >/dev/null 2>&1; then _pf_r_jq=1; fi\n_pf_r_field() {\n\tif [ -n "$_pf_r_jq" ]; then\n\t\tjq -r --arg k "$1" \'if type == "object" then (.[$k] // empty) | strings else empty end\' \\\n\t\t\t<"$_pf_r_tmp/input" 2>/dev/null || :\n\telse\n\t\ttr -d \'\\n\' <"$_pf_r_tmp/input" | sed -n "s/.*\\"$1\\"[[:space:]]*:[[:space:]]*\\"\\([^\\"]*\\)\\".*/\\1/p"\n\tfi\n}\n\n_pf_r_sid=$(_pf_r_field session_id)\n[ -n "$_pf_r_sid" ] || _pf_r_sid=$(_pf_r_field sessionId)\n_pf_r_cwd=$(_pf_r_field cwd)\n\n# The project: the event\'s cwd walked up to .git. Without one, Claude\'s rule\n# for a script; Copilot runs hooks from the plugin root, which is no project.\ncase $_pf_r_cwd in\n/*) _pf_r_proj=$(_pf_env_project_of "$_pf_r_cwd") ;;\n*)\n\tif [ "${PLUGINFINITY_HOST:-}" = copilot ]; then\n\t\t_pf_r_proj=\n\t\t_pf_env_log "the event has no cwd; .env files not read"\n\telse\n\t\t_pf_r_proj=$(_pf_env_script_project)\n\tfi\n\t;;\nesac\n\n_pf_env_init\n\n# Every process below $1, from one ps snapshot.\n_pf_r_descendants() {\n\tps -A -o pid= -o ppid= 2>/dev/null | awk -v root="$1" \'\n\t\t{ parent[$1] = $2 }\n\t\tEND {\n\t\t\tfor (p in parent) {\n\t\t\t\tq = p\n\t\t\t\tfor (i = 0; i < 64 && q != "" && q != 0 && q != 1; i++) {\n\t\t\t\t\tq = parent[q]\n\t\t\t\t\tif (q == root) { print p; break }\n\t\t\t\t}\n\t\t\t}\n\t\t}\'\n}\n\n# Send signal $1 to process $2, to its process group when it leads one ($4 is\n# the group id seen at the first signal), and to the pids in $3.\n_pf_r_signal() {\n\tcase ${2:-} in \'\' | 0 | 1 | *[!0123456789]*) return 0 ;; esac\n\tif [ "${4:-}" = "$2" ]; then kill -s "$1" -- "-$2" 2>/dev/null || :; fi\n\t# shellcheck disable=SC2086 # pids are words\n\tkill -s "$1" "$2" ${3:-} 2>/dev/null || :\n}\n\n# Rung 2: the setup script.\n_pf_r_setup() {\n\t[ -n "$_pf_env_setup" ] || return 0\n\t_pf_r_script="$_pf_env_lib_dir/../../$_pf_env_setup"\n\tif [ ! -f "$_pf_r_script" ]; then\n\t\t_pf_env_log "setup script $_pf_env_setup is missing; skipped"\n\t\treturn 0\n\tfi\n\tif [ -z "$_pf_r_proj" ]; then\n\t\t_pf_env_log "no project; setup script $_pf_env_setup skipped"\n\t\treturn 0\n\tfi\n\t_pf_r_t=$_pf_env_setup_timeout\n\tcase $_pf_r_t in \'\' | *[!0123456789]*) _pf_r_t=10 ;; esac\n\t# Its own process group where job control allows (bash, bash as sh), so a\n\t# timeout can signal the group; the descendant walk covers dash, which has no\n\t# job control without a terminal.\n\tset -m 2>/dev/null || :\n\t(\n\t\tCDPATH=\'\' cd -- "$_pf_r_proj" 2>/dev/null || exit 1\n\t\tPLUGINFINITY_EVENT=SessionStart\n\t\texport PLUGINFINITY_EVENT\n\t\texec bash "$_pf_r_script"\n\t) <"$_pf_r_tmp/input" >"$_pf_r_tmp/out" 2>"$_pf_r_tmp/err" &\n\t_pf_r_pid=$!\n\tset +m 2>/dev/null || :\n\t(\n\t\tsleep "$_pf_r_t"\n\t\t: >"$_pf_r_tmp/timedout"\n\t\t# One snapshot: by the KILL the orphans belong to init and no walk finds them.\n\t\t_pf_r_kids=$(_pf_r_descendants "$_pf_r_pid")\n\t\t_pf_r_pg=$(ps -o pgid= -p "$_pf_r_pid" 2>/dev/null | tr -d \' \')\n\t\t_pf_r_signal TERM "$_pf_r_pid" "$_pf_r_kids" "$_pf_r_pg"\n\t\tsleep 1\n\t\t_pf_r_signal KILL "$_pf_r_pid" "$_pf_r_kids" "$_pf_r_pg"\n\t) /dev/null 2>&1 &\n\t_pf_r_watch=$!\n\tif wait "$_pf_r_pid" 2>/dev/null; then _pf_r_rc=0; else _pf_r_rc=$?; fi\n\t# After a timeout the watcher still owes the KILL for anything that ignored TERM.\n\tif [ -e "$_pf_r_tmp/timedout" ]; then\n\t\twait "$_pf_r_watch" 2>/dev/null || :\n\telse\n\t\t# Reaped inside the redirect: bash as sh prints "Terminated" for a killed job.\n\t\t{ kill "$_pf_r_watch" && wait "$_pf_r_watch"; } 2>/dev/null || :\n\tfi\n\tif pf_debug_on 2>/dev/null && [ -s "$_pf_r_tmp/err" ]; then\n\t\thead -n 20 "$_pf_r_tmp/err" | while IFS= read -r _pf_r_l; do _pf_env_debug "setup: $_pf_r_l"; done\n\tfi\n\tif [ -e "$_pf_r_tmp/timedout" ]; then\n\t\t_pf_env_log "setup script $_pf_env_setup timed out after ${_pf_r_t}s; its output is discarded"\n\t\treturn 0\n\tfi\n\t[ "$_pf_r_rc" -eq 0 ] || _pf_env_log "setup script $_pf_env_setup exited $_pf_r_rc; keeping its valid lines"\n\twhile IFS= read -r _pf_e_l || [ -n "$_pf_e_l" ]; do\n\t\tcase $_pf_e_l in \'\' | \'#\'*) continue ;; esac\n\t\t_pf_e_k=${_pf_e_l%%=*}\n\t\t[ "$_pf_e_k" != "$_pf_e_l" ] || continue\n\t\tif _pf_env_declared "$_pf_e_k"; then\n\t\t\t_pf_e_v=${_pf_e_l#*=}\n\t\t\teval "_pf_env_val_$_pf_e_k=\\$_pf_e_v"\n\t\telif _pf_env_is_name "$_pf_e_k"; then\n\t\t\t_pf_env_log "setup printed $_pf_e_k, which is not declared; skipped"\n\t\tfi\n\tdone <"$_pf_r_tmp/out"\n\treturn 0\n}\n_pf_r_setup\n\n# Rungs 3-5.\n_pf_env_live "$_pf_r_proj"\n\nif _pf_env_valid_id "$_pf_r_sid"; then\n\t_pf_r_sd=$(_pf_env_session_dir "$_pf_r_sid")\n\t_pf_env_lock "$_pf_r_sd"\n\t# A same-id rerun keeps what _pf_env_set wrote (rung 6) and re-resolves the rest.\n\t_pf_env_keep_set "$_pf_r_sd"\n\tif _pf_env_write_values "$_pf_r_sd/env"; then _pf_r_ok=1; else _pf_r_ok=; fi\n\t_pf_env_unlock\n\t(umask 077 && : >"$_pf_r_sd/done") 2>/dev/null || :\n\tif [ -n "$_pf_r_ok" ] && [ -n "$_pf_r_proj" ]; then\n\t\t_pf_r_ptr=$(_pf_env_pointer_path "$_pf_r_proj")\n\t\tif (umask 077 && mkdir -p "$(dirname -- "$_pf_r_ptr")") 2>/dev/null &&\n\t\t\tprintf \'%s\\n%s\\n\' "$_pf_r_sid" "$_pf_r_proj" >"$_pf_r_ptr.tmp.$$" 2>/dev/null &&\n\t\t\tmv -f "$_pf_r_ptr.tmp.$$" "$_pf_r_ptr" 2>/dev/null; then\n\t\t\t:\n\t\telse\n\t\t\trm -f "$_pf_r_ptr.tmp.$$" 2>/dev/null\n\t\t\t_pf_env_log "cannot write the project pointer"\n\t\tfi\n\tfi\nelif [ -z "$_pf_r_sid" ]; then\n\t_pf_env_log "the event has no session id; session values not written"\nelse\n\t_pf_env_log "invalid session id; session values not written"\nfi\n\n# shellcheck disable=SC2086 # the names are words\n_pf_env_claude_export $_pf_env_names\nexit 0\n', }, { name: "env.sh", content: '# shellcheck shell=sh\n# pluginfinity session env. `pluginfinity build` writes this file into every\n# target whose config declares `env`, at lib/pluginfinity/env.sh, with the\n# declarations block below filled in; do not edit a copy under builds/, the\n# next build overwrites it.\n#\n# POSIX sh. Writes nothing to stdout. Fails open: every problem is a log line\n# and the variables keep what the chain could resolve.\n#\n# From a skill script or a monitor, two lines apply the session\'s values. Build\n# the path from an absolute directory, before any cd: a relative $0 no longer\n# resolves after one.\n# _pf_lib_dir="$(cd "$(dirname "$0")/../../../lib/pluginfinity" && pwd)" # skills//scripts/x.sh\n# . "$_pf_lib_dir/env.sh"\n# A script that resolves its own project sources first, changes directory, then\n# loads for that project:\n# _pf_env_manual=1; . "$_pf_lib_dir/env.sh"; cd "$PROJECT_DIR"; env_load "" "$PROJECT_DIR"\n#\n# Precedence, lowest first: the config default, the setup script\'s output,\n# /.env, /.env.local, the ambient environment (a declared\n# name already set when this file is sourced), then _pf_env_set at run time.\n# The SessionStart runner (env-run.sh) resolves the first five once and writes\n# the result to the session values file; a reader takes that file as\n# authoritative and evaluates the chain live (without setup) only for names the\n# file lacks, or when there is no file at all.\n#\n# State, under ${XDG_STATE_HOME:-$HOME/.local/state}/pluginfinity//\n# (directories and files created under umask 077):\n# session//env one NAME=value line per declared name, the\n# resolved value; the value is literal to the\n# end of the line\n# session//set provenance: one NAME per line for each name\n# last written by _pf_env_set (rung 6). A\n# SessionStart rerun under the same id keeps\n# these names\' values and re-resolves the rest.\n# session//done written by the runner when it has finished\n# session//lock/ a mkdir lock around every read-merge-write of\n# env and set; lock/at holds its epoch seconds\n# project/ line 1 the latest session id, line 2 the dir\n#\n# Locking: a writer waits up to about 5 s for the lock, removes one older than\n# 10 s (or with no lock/at after 2 s) as stale, and on timeout logs and writes\n# anyway (fail open).\n#\n# Claude runs an event\'s hooks in parallel, so a SessionStart hook may read\n# before the runner has finished. In SessionStart only (PLUGINFINITY_EVENT),\n# env_load with a session id but no values file waits up to 3 s for the\n# runner\'s done marker, then resolves live with a log line. Other events never\n# wait.\n#\n# Contract for callers (the hook library and the runner):\n# _pf_env_lib_dir set before sourcing to this file\'s directory when\n# _pf_lib_dir names another (the hook library\'s) directory.\n# _pf_env_manual=1 set before sourcing to skip the automatic env_load; the\n# caller then calls env_load itself.\n# _pf_env_component the log component (hook|script|monitor); guessed if unset.\n# env_load [ []]\n# apply the chain into the current shell and export every\n# declared name. With no session id, CLAUDE_CODE_SESSION_ID\n# names the session when it is a valid id whose values file\n# exists; otherwise the project pointer for \n# (default: CLAUDE_PROJECT_DIR or $PWD, walked up to the\n# nearest .git) does. A of - means no project:\n# no pointer and no .env read. An invalid session id\n# (empty, ., /, .., a backslash, a control character) is\n# logged and nothing is read. Always returns 0.\n# env_reload env_load again with the arguments the last one got.\n# _pf_env_set NAME VALUE\n# rank-6 write: under the lock, update the session values\n# file (atomically) and record NAME in the set file,\n# export NAME in this shell and, on Claude with\n# CLAUDE_ENV_FILE set, append `export NAME=\'VALUE\'` to it.\n# Returns 1, with a log line, for an undeclared name, a\n# value holding a newline, or no valid session from the last\n# env_load. It does not check the event: the caller does.\n# The hook library\'s hook_env_set wraps it and always\n# returns 0, so a refusal can never fail a hook.\n# _pf_env_declared NAME 0 when NAME is declared.\n\n# >>> pluginfinity env declarations (the build replaces this block)\n_pf_env_names=\'\'\n_pf_env_setup=\'\'\n_pf_env_setup_timeout=10\n# <<< pluginfinity env declarations\n\n_pf_env_dir="${_pf_env_lib_dir:-${_pf_lib_dir:-${_pf_log_dir:-.}}}"\nif ! command -v pf_log >/dev/null 2>&1; then\n\tif [ -r "$_pf_env_dir/log.sh" ]; then\n\t\t_pf_log_dir="$_pf_env_dir"\n\t\t# shellcheck source=/dev/null\n\t\t. "$_pf_env_dir/log.sh"\n\telse\n\t\tpf_log() { return 0; }\n\t\tpf_debug() { return 0; }\n\t\tpf_debug_on() { return 1; }\n\tfi\nfi\nif [ -z "${PLUGINFINITY_PLUGIN:-}" ] && [ -r "$_pf_env_dir/host.sh" ]; then\n\t# shellcheck source=/dev/null\n\t. "$_pf_env_dir/host.sh" 2>/dev/null || :\nfi\nif [ -z "${_pf_env_component:-}" ]; then\n\tif command -v hook_log >/dev/null 2>&1; then\n\t\t_pf_env_component=hook\n\telif command -v monitor_log >/dev/null 2>&1; then\n\t\t_pf_env_component=monitor\n\telse\n\t\t_pf_env_component=script\n\tfi\nfi\n\n_pf_env_log() { pf_log "$_pf_env_component" "env: $*"; }\n_pf_env_debug() { pf_debug "$_pf_env_component" "env: $*"; }\n\n_pf_env_cr=$(printf \'\\r\')\n_pf_env_nl=\'\n\'\n\n# 0 when $1 is a declared name. The character check keeps eval safe.\n_pf_env_declared() {\n\tcase ${1:-} in\n\t\'\' | [0123456789]* | *[!ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]*) return 1 ;;\n\tesac\n\tcase " $_pf_env_names " in\n\t*" $1 "*) return 0 ;;\n\tesac\n\treturn 1\n}\n\n# 0 when $1 looks like a variable name at all (declared or not).\n_pf_env_is_name() {\n\tcase ${1:-} in\n\t\'\' | [0123456789]* | *[!ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]*) return 1 ;;\n\tesac\n\treturn 0\n}\n\n# 0 when $1 is a usable session id: not empty, not ., no /, no .., no \\, no control character.\n_pf_env_valid_id() {\n\tcase ${1:-} in\n\t\'\' | . | */* | *..* | *\\\\*) return 1 ;;\n\tesac\n\t[ "$(printf \'%s\' "$1" | tr -d \'[:cntrl:]\')" = "$1" ]\n}\n\n_pf_env_state_root() {\n\tprintf \'%s\\n\' "${XDG_STATE_HOME:-${HOME:-/nonexistent}/.local/state}/pluginfinity/${PLUGINFINITY_PLUGIN:-unknown}"\n}\n\n# $1 as a physical path, or as given when it cannot be entered.\n_pf_env_physical() {\n\t(CDPATH=\'\' cd -- "$1" 2>/dev/null && pwd -P) || printf \'%s\\n\' "$1"\n}\n\n# The project for directory $1: the nearest directory at or above it holding .git, else $1.\n_pf_env_project_of() {\n\t_pf_e_p=$(_pf_env_physical "$1")\n\t_pf_e_q=$_pf_e_p\n\twhile [ -n "$_pf_e_q" ] && [ "$_pf_e_q" != / ] && [ "$_pf_e_q" != . ]; do\n\t\tif [ -e "$_pf_e_q/.git" ]; then\n\t\t\tprintf \'%s\\n\' "$_pf_e_q"\n\t\t\treturn 0\n\t\tfi\n\t\t_pf_e_q=$(dirname -- "$_pf_e_q")\n\tdone\n\tprintf \'%s\\n\' "$_pf_e_p"\n}\n\n# The project of a script with no session: CLAUDE_PROJECT_DIR, else $PWD, walked to .git.\n_pf_env_script_project() {\n\t_pf_env_project_of "${CLAUDE_PROJECT_DIR:-$PWD}"\n}\n\n_pf_env_key() {\n\tprintf \'%s\' "$1" | cksum | awk \'{ print $1 "-" $2 }\'\n}\n\n_pf_env_pointer_path() { printf \'%s/project/%s\\n\' "$(_pf_env_state_root)" "$(_pf_env_key "$1")"; }\n_pf_env_session_dir() { printf \'%s/session/%s\\n\' "$(_pf_env_state_root)" "$1"; }\n_pf_env_values_path() { printf \'%s/env\\n\' "$(_pf_env_session_dir "$1")"; }\n\n# Sleep one tick (0.1 s) and count it in _pf_e_ticks; where sleep takes whole\n# seconds only, sleep 1 and count ten.\n_pf_env_nap() {\n\tif sleep 0.1 2>/dev/null; then\n\t\t_pf_e_ticks=$((_pf_e_ticks + 1))\n\telse\n\t\tsleep 1\n\t\t_pf_e_ticks=$((_pf_e_ticks + 10))\n\tfi\n}\n\n# Take the lock in session directory $1: mkdir with bounded retry (about 5 s).\n# A lock older than 10 s, or one whose at file is still missing after 2 s, is\n# stale and removed. On timeout, log and go on without it. Always returns 0.\n_pf_env_lock() {\n\t_pf_e_lk="$1/lock"\n\t_pf_e_held=\n\t(umask 077 && mkdir -p "$1") 2>/dev/null || return 0\n\t_pf_e_ticks=0\n\t_pf_e_noat=0\n\twhile [ "$_pf_e_ticks" -lt 50 ]; do\n\t\tif mkdir "$_pf_e_lk" 2>/dev/null; then\n\t\t\tdate +%s >"$_pf_e_lk/at" 2>/dev/null || :\n\t\t\t_pf_e_held=1\n\t\t\treturn 0\n\t\tfi\n\t\t_pf_e_at=$(cat "$_pf_e_lk/at" 2>/dev/null) || _pf_e_at=\n\t\tcase $_pf_e_at in\n\t\t\'\' | *[!0123456789]*)\n\t\t\t_pf_e_noat=$((_pf_e_noat + 1))\n\t\t\tif [ "$_pf_e_noat" -gt 20 ]; then\n\t\t\t\t_pf_env_debug "removing a lock with no time as stale"\n\t\t\t\trm -f "$_pf_e_lk/at" 2>/dev/null\n\t\t\t\trmdir "$_pf_e_lk" 2>/dev/null\n\t\t\t\t_pf_e_noat=0\n\t\t\t\tcontinue\n\t\t\tfi\n\t\t\t;;\n\t\t*)\n\t\t\t_pf_e_now=$(date +%s 2>/dev/null) || _pf_e_now=$_pf_e_at\n\t\t\tif [ $((_pf_e_now - _pf_e_at)) -gt 10 ]; then\n\t\t\t\t_pf_env_debug "removing a stale lock"\n\t\t\t\trm -f "$_pf_e_lk/at" 2>/dev/null\n\t\t\t\trmdir "$_pf_e_lk" 2>/dev/null\n\t\t\t\tcontinue\n\t\t\tfi\n\t\t\t;;\n\t\tesac\n\t\t_pf_env_nap\n\tdone\n\t_pf_env_log "the session values lock is held too long; writing without it"\n\treturn 0\n}\n\n_pf_env_unlock() {\n\tif [ "${_pf_e_held:-}" = 1 ]; then\n\t\trm -f "$_pf_e_lk/at" 2>/dev/null\n\t\trmdir "$_pf_e_lk" 2>/dev/null\n\tfi\n\t_pf_e_held=\n\treturn 0\n}\n\n# 0 when NAME $2 is listed in the set file of session directory $1.\n_pf_env_was_set() {\n\t[ -r "$1/set" ] || return 1\n\twhile IFS= read -r _pf_e_sl || [ -n "$_pf_e_sl" ]; do\n\t\t[ "$_pf_e_sl" != "$2" ] || return 0\n\tdone <"$1/set"\n\treturn 1\n}\n\n# Under the caller\'s lock: the names the set file lists keep their value from the\n# values file in session directory $1 (a same-id SessionStart rerun).\n_pf_env_keep_set() {\n\t[ -r "$1/set" ] && [ -r "$1/env" ] || return 0\n\twhile IFS= read -r _pf_e_l || [ -n "$_pf_e_l" ]; do\n\t\t_pf_e_k=${_pf_e_l%%=*}\n\t\t[ "$_pf_e_k" != "$_pf_e_l" ] || continue\n\t\t_pf_env_declared "$_pf_e_k" || continue\n\t\t_pf_env_was_set "$1" "$_pf_e_k" || continue\n\t\t_pf_e_v=${_pf_e_l#*=}\n\t\teval "_pf_env_val_$_pf_e_k=\\$_pf_e_v"\n\tdone <"$1/env"\n\treturn 0\n}\n\n# The session id the pointer for project $1 names, when it names this very project.\n_pf_env_pointer_read() {\n\t_pf_e_f=$(_pf_env_pointer_path "$1")\n\t[ -r "$_pf_e_f" ] || return 1\n\t_pf_e_ps=\n\t_pf_e_pd=\n\t{\n\t\tIFS= read -r _pf_e_ps || :\n\t\tIFS= read -r _pf_e_pd || :\n\t} <"$_pf_e_f" 2>/dev/null || return 1\n\t[ "$_pf_e_pd" = "$1" ] || return 1\n\t_pf_env_valid_id "$_pf_e_ps" || return 1\n\tprintf \'%s\\n\' "$_pf_e_ps"\n}\n\n# Every declared name back to its config default; nothing marked as read from a file.\n_pf_env_init() {\n\tfor _pf_e_n in $_pf_env_names; do\n\t\t_pf_env_declared "$_pf_e_n" || continue\n\t\teval "_pf_env_val_$_pf_e_n=\\${_pf_env_default_$_pf_e_n:-}; _pf_env_got_$_pf_e_n="\n\tdone\n}\n\n# Parse a .env file into the current values: KEY=value or export KEY=value,\n# one matching pair of quotes stripped, nothing expanded, declared names only.\n_pf_env_dotenv() {\n\t[ -e "$1" ] || return 0\n\tif [ ! -f "$1" ] || [ ! -r "$1" ]; then\n\t\t_pf_env_log "cannot read $1; skipped"\n\t\treturn 0\n\tfi\n\twhile IFS= read -r _pf_e_l || [ -n "$_pf_e_l" ]; do\n\t\t_pf_e_l=${_pf_e_l%"$_pf_env_cr"}\n\t\t_pf_e_l=${_pf_e_l#"${_pf_e_l%%[! \t]*}"}\n\t\tcase $_pf_e_l in\n\t\t\'export \'* | \'export\t\'*)\n\t\t\t_pf_e_l=${_pf_e_l#export}\n\t\t\t_pf_e_l=${_pf_e_l#"${_pf_e_l%%[! \t]*}"}\n\t\t\t;;\n\t\tesac\n\t\t_pf_e_k=${_pf_e_l%%=*}\n\t\t[ "$_pf_e_k" != "$_pf_e_l" ] || continue\n\t\t_pf_env_declared "$_pf_e_k" || continue\n\t\t_pf_e_v=${_pf_e_l#*=}\n\t\tcase $_pf_e_v in\n\t\t\\"*\\")\n\t\t\t_pf_e_v=${_pf_e_v#\\"}\n\t\t\t_pf_e_v=${_pf_e_v%\\"}\n\t\t\t;;\n\t\t\\\'*\\\')\n\t\t\t_pf_e_v=${_pf_e_v#\\\'}\n\t\t\t_pf_e_v=${_pf_e_v%\\\'}\n\t\t\t;;\n\t\tesac\n\t\teval "_pf_env_val_$_pf_e_k=\\$_pf_e_v"\n\tdone <"$1"\n\treturn 0\n}\n\n# The ambient values captured when this file was sourced (rung 5).\n_pf_env_ambient() {\n\tfor _pf_e_n in $_pf_env_names; do\n\t\t_pf_env_declared "$_pf_e_n" || continue\n\t\teval "_pf_e_h=\\${_pf_env_hasamb_$_pf_e_n:-}"\n\t\t[ "$_pf_e_h" = 1 ] || continue\n\t\teval "_pf_e_v=\\${_pf_env_amb_$_pf_e_n:-}"\n\t\tcase $_pf_e_v in\n\t\t*"$_pf_env_nl"*)\n\t\t\t_pf_env_log "ambient $_pf_e_n holds a newline; ignored"\n\t\t\tcontinue\n\t\t\t;;\n\t\tesac\n\t\teval "_pf_env_val_$_pf_e_n=\\$_pf_e_v"\n\tdone\n\treturn 0\n}\n\n# Rungs 3-5 over the current values, for project $1 (none when empty).\n_pf_env_live() {\n\tif [ -n "${1:-}" ]; then\n\t\t_pf_env_dotenv "$1/.env"\n\t\t_pf_env_dotenv "$1/.env.local"\n\tfi\n\t_pf_env_ambient\n}\n\n# Read a session values file over the current values, marking each name it holds.\n_pf_env_read_values() {\n\twhile IFS= read -r _pf_e_l || [ -n "$_pf_e_l" ]; do\n\t\t_pf_e_k=${_pf_e_l%%=*}\n\t\t[ "$_pf_e_k" != "$_pf_e_l" ] || continue\n\t\t_pf_env_declared "$_pf_e_k" || continue\n\t\t_pf_e_v=${_pf_e_l#*=}\n\t\teval "_pf_env_val_$_pf_e_k=\\$_pf_e_v; _pf_env_got_$_pf_e_k=1"\n\tdone <"$1"\n\treturn 0\n}\n\n# Every declared name not marked as read from the values file.\n_pf_env_missing() {\n\tfor _pf_e_n in $_pf_env_names; do\n\t\t_pf_env_declared "$_pf_e_n" || continue\n\t\teval "_pf_e_g=\\${_pf_env_got_$_pf_e_n:-}"\n\t\t[ "$_pf_e_g" = 1 ] || return 0\n\tdone\n\treturn 1\n}\n\n# Export the current values into this shell.\n_pf_env_apply() {\n\tfor _pf_e_n in $_pf_env_names; do\n\t\t_pf_env_declared "$_pf_e_n" || continue\n\t\teval "$_pf_e_n=\\${_pf_env_val_$_pf_e_n:-}"\n\t\texport "${_pf_e_n?}"\n\tdone\n\treturn 0\n}\n\n# Print $1 single-quoted for a shell, with no newline after it.\n_pf_env_squote() {\n\t_pf_e_r=$1\n\t_pf_e_o=\n\twhile :; do\n\t\tcase $_pf_e_r in\n\t\t*\\\'*)\n\t\t\t_pf_e_o="$_pf_e_o${_pf_e_r%%\\\'*}\'\\\\\'\'"\n\t\t\t_pf_e_r=${_pf_e_r#*\\\'}\n\t\t\t;;\n\t\t*)\n\t\t\t_pf_e_o="$_pf_e_o$_pf_e_r"\n\t\t\tbreak\n\t\t\t;;\n\t\tesac\n\tdone\n\tprintf "\'%s\'" "$_pf_e_o"\n}\n\n# Append `export NAME=\'value\'` for each NAME given, from the current values, to CLAUDE_ENV_FILE.\n# Only on Claude, and only when the host set CLAUDE_ENV_FILE.\n_pf_env_claude_export() {\n\t[ "${PLUGINFINITY_HOST:-}" = claude ] || return 0\n\tif [ -z "${CLAUDE_ENV_FILE:-}" ]; then\n\t\t_pf_env_log "CLAUDE_ENV_FILE is not set; the Bash tool will not see the session values"\n\t\treturn 0\n\tfi\n\tfor _pf_e_n in "$@"; do\n\t\teval "_pf_e_v=\\${_pf_env_val_$_pf_e_n:-}"\n\t\t{\n\t\t\tprintf \'export %s=\' "$_pf_e_n"\n\t\t\t_pf_env_squote "$_pf_e_v"\n\t\t\tprintf \'\\n\'\n\t\t} >>"$CLAUDE_ENV_FILE" 2>/dev/null || {\n\t\t\t_pf_env_log "cannot append to CLAUDE_ENV_FILE"\n\t\t\treturn 0\n\t\t}\n\tdone\n\treturn 0\n}\n\n# Write the current values of every declared name to values file $1, atomically.\n_pf_env_write_values() {\n\t_pf_e_d=$(dirname -- "$1")\n\t(umask 077 && mkdir -p "$_pf_e_d") 2>/dev/null || {\n\t\t_pf_env_log "cannot create $_pf_e_d; session values not written"\n\t\treturn 1\n\t}\n\t_pf_e_t="$1.tmp.$$"\n\t(\n\t\tumask 077\n\t\tfor _pf_e_n in $_pf_env_names; do\n\t\t\t_pf_env_declared "$_pf_e_n" || continue\n\t\t\teval "_pf_e_v=\\${_pf_env_val_$_pf_e_n:-}"\n\t\t\tprintf \'%s=%s\\n\' "$_pf_e_n" "$_pf_e_v"\n\t\tdone >"$_pf_e_t"\n\t) 2>/dev/null && mv -f "$_pf_e_t" "$1" 2>/dev/null && return 0\n\trm -f "$_pf_e_t" 2>/dev/null\n\t_pf_env_log "cannot write $1"\n\treturn 1\n}\n\nenv_load() {\n\t_pf_env_arg_sid=${1:-}\n\t_pf_env_arg_proj=${2:-}\n\t_pf_env_sid=\n\t_pf_env_file=\n\t[ -n "$_pf_env_names" ] || return 0\n\tcase $_pf_env_arg_proj in\n\t-) _pf_env_proj= ;;\n\t\'\') _pf_env_proj=$(_pf_env_script_project) ;;\n\t*) _pf_env_proj=$_pf_env_arg_proj ;;\n\tesac\n\tif [ -n "$_pf_env_arg_sid" ]; then\n\t\tif _pf_env_valid_id "$_pf_env_arg_sid"; then\n\t\t\t_pf_env_sid=$_pf_env_arg_sid\n\t\telse\n\t\t\t_pf_env_log "invalid session id; session values not read"\n\t\tfi\n\telif [ -n "${CLAUDE_CODE_SESSION_ID:-}" ] && _pf_env_valid_id "$CLAUDE_CODE_SESSION_ID" &&\n\t\t[ -e "$(_pf_env_values_path "$CLAUDE_CODE_SESSION_ID")" ]; then\n\t\t# Claude gives a skill script and a monitor CLAUDE_CODE_SESSION_ID, so prefer\n\t\t# its own session over the pointer, which names the project\'s latest one.\n\t\t# It equals the hook session_id (measured on Claude Code 2.1.291); an id\n\t\t# whose values file is missing falls back to the pointer. Copilot sets no\n\t\t# such variable for a script, so it always takes the pointer.\n\t\t_pf_env_sid=$CLAUDE_CODE_SESSION_ID\n\telif [ -n "$_pf_env_proj" ] && _pf_e_s=$(_pf_env_pointer_read "$_pf_env_proj"); then\n\t\t_pf_env_sid=$_pf_e_s\n\tfi\n\t[ -z "$_pf_env_sid" ] || _pf_env_file=$(_pf_env_values_path "$_pf_env_sid")\n\t# In SessionStart a parallel runner may not have finished: wait for its done marker.\n\tif [ -n "$_pf_env_arg_sid" ] && [ -n "$_pf_env_file" ] && [ ! -e "$_pf_env_file" ] &&\n\t\t[ "${PLUGINFINITY_EVENT:-}" = SessionStart ]; then\n\t\t_pf_e_done="$(_pf_env_session_dir "$_pf_env_sid")/done"\n\t\t_pf_e_ticks=0\n\t\twhile [ ! -e "$_pf_e_done" ] && [ "$_pf_e_ticks" -lt 30 ]; do _pf_env_nap; done\n\t\t[ -e "$_pf_e_done" ] || _pf_env_log "the env runner had not finished after 3s; resolving live"\n\tfi\n\t_pf_env_init\n\tif [ -n "$_pf_env_file" ] && [ -r "$_pf_env_file" ]; then\n\t\t_pf_env_read_values "$_pf_env_file"\n\t\tif _pf_env_missing; then\n\t\t\t# A name the file lacks (declared after SessionStart) resolves live; file values win.\n\t\t\t_pf_env_init\n\t\t\t_pf_env_live "$_pf_env_proj"\n\t\t\t_pf_env_read_values "$_pf_env_file"\n\t\tfi\n\telse\n\t\t_pf_env_debug "no session values; resolving live"\n\t\t_pf_env_live "$_pf_env_proj"\n\tfi\n\t_pf_env_apply\n\treturn 0\n}\n\nenv_reload() {\n\tenv_load "${_pf_env_arg_sid:-}" "${_pf_env_arg_proj:-}"\n}\n\n_pf_env_set() {\n\t_pf_e_sn=${1:-}\n\t_pf_e_sv=${2-}\n\tif ! _pf_env_declared "$_pf_e_sn"; then\n\t\t_pf_env_log "cannot set $_pf_e_sn: not a declared session variable"\n\t\treturn 1\n\tfi\n\tcase $_pf_e_sv in\n\t*"$_pf_env_nl"*)\n\t\t_pf_env_log "cannot set $_pf_e_sn: the value holds a newline"\n\t\treturn 1\n\t\t;;\n\tesac\n\tif [ -z "${_pf_env_file:-}" ]; then\n\t\t_pf_env_log "cannot set $_pf_e_sn: no session"\n\t\treturn 1\n\tfi\n\t# Under the lock, keep every other name as the file has it (a name it lacks keeps\n\t# its resolved value), then record NAME as set at rung 6.\n\t_pf_e_sd=$(dirname -- "$_pf_env_file")\n\t_pf_env_lock "$_pf_e_sd"\n\tif [ -r "$_pf_env_file" ]; then _pf_env_read_values "$_pf_env_file"; fi\n\teval "_pf_env_val_$_pf_e_sn=\\$_pf_e_sv"\n\tif ! _pf_env_write_values "$_pf_env_file"; then\n\t\t_pf_env_unlock\n\t\treturn 1\n\tfi\n\tif ! _pf_env_was_set "$_pf_e_sd" "$_pf_e_sn"; then\n\t\t(umask 077 && printf \'%s\\n\' "$_pf_e_sn" >>"$_pf_e_sd/set") 2>/dev/null ||\n\t\t\t_pf_env_log "cannot record $_pf_e_sn in the set file"\n\tfi\n\t_pf_env_unlock\n\teval "$_pf_e_sn=\\$_pf_e_sv"\n\texport "${_pf_e_sn?}"\n\t_pf_env_claude_export "$_pf_e_sn"\n\treturn 0\n}\n\n# Capture the ambient values once, before anything here exports a declared name.\nif [ "${_pf_env_captured:-}" != 1 ]; then\n\t_pf_env_captured=1\n\tfor _pf_e_n in $_pf_env_names; do\n\t\t_pf_env_declared "$_pf_e_n" || continue\n\t\teval "_pf_e_h=\\${$_pf_e_n+x}"\n\t\tif [ "$_pf_e_h" = x ]; then\n\t\t\teval "_pf_env_amb_$_pf_e_n=\\$$_pf_e_n; _pf_env_hasamb_$_pf_e_n=1"\n\t\telse\n\t\t\teval "_pf_env_hasamb_$_pf_e_n="\n\t\tfi\n\tdone\nfi\n\nif [ "${_pf_env_manual:-}" != 1 ]; then\n\tenv_load\nfi\n:\n', }, ];