#!/usr/bin/env bash set -euo pipefail summary=false skip_codex_schema=false while [ $# -gt 0 ]; do case "$1" in --summary) summary=true; shift ;; --skip-codex-schema) skip_codex_schema=true; shift ;; *) printf 'Unknown argument: %s\n' "$1" >&2; exit 1 ;; esac done root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd) # shellcheck source=../../shared/hooks/scripts/resolve-python.sh . "$root/shared/hooks/scripts/resolve-python.sh" # Fail early with an actionable message instead of a generic validation error after the build. resolve_python || { python_missing_message >&2; exit 1; } shared="$root/shared" final="$root/generated" sandbox_supported=false platform=$(uname -s 2>/dev/null || true) case "$platform" in Darwin) sandbox_supported=true ;; Linux) kernel=$(uname -r 2>/dev/null || true) case "$kernel" in *microsoft*) [[ "$kernel" == *microsoft-standard-WSL2* ]] && sandbox_supported=true ;; *) sandbox_supported=true ;; esac ;; esac sandbox='{"enabled": false}' # QMD search must reach its localhost daemon and write its index under the home directory, which the sandbox blocks. # The exact skill command keeps the exclusion narrow; harmless when local search is disabled. [ "$sandbox_supported" != true ] || sandbox='{"enabled": true, "allowUnsandboxedCommands": false, "failIfUnavailable": true, "excludedCommands": ["node \"$HOME/.my-ai-configuration/qmd/qmd-search.mjs\" *"], "network": {"allowedDomains": ["mcp.context7.com"]}}' plugin_manifest="$root/adapters/plugins.tsv" capability_manifest="$root/adapters/claude/capabilities.tsv" placeholder_manifest="$root/adapters/placeholders.tsv" # Shell-specific template values live in one table shared with build.ps1, so adding a hook or # script means adding one row instead of editing both builds. [ -f "$placeholder_manifest" ] || { printf 'Missing placeholder manifest: %s\n' "$placeholder_manifest" >&2; exit 1; } placeholder_names=() placeholder_powershell=() placeholder_bash=() while IFS=$'\t' read -r placeholder powershell_value bash_value; do bash_value=${bash_value%$'\r'} [ "$placeholder" != placeholder ] || continue [ -n "$placeholder" ] || continue [[ "$placeholder" =~ ^__[A-Z0-9_]+__$ ]] && [ -n "$powershell_value" ] && [ -n "$bash_value" ] || { printf 'Invalid placeholder manifest row: %s\n' "$placeholder" >&2; exit 1; } placeholder_names+=("$placeholder") placeholder_powershell+=("$powershell_value") placeholder_bash+=("$bash_value") done < "$placeholder_manifest" expand_shell_placeholders() { local text=$1 shell=$2 index value for index in "${!placeholder_names[@]}"; do if [ "$shell" = powershell ]; then value=${placeholder_powershell[$index]}; else value=${placeholder_bash[$index]}; fi text=${text//"${placeholder_names[$index]}"/"$value"} done printf '%s' "$text" } [ -f "$plugin_manifest" ] || { printf 'Missing plugin manifest: %s\n' "$plugin_manifest" >&2; exit 1; } awk -F '\t' 'NR == 1 { next } NF == 0 { next } $1 == "" || $3 == "" || $6 == "" { print "Invalid plugin manifest row." > "/dev/stderr"; exit 1 } seen[$1]++ { print "Duplicate plugin name in manifest: " $1 > "/dev/stderr"; exit 1 }' "$plugin_manifest" [ -f "$capability_manifest" ] || { printf 'Missing capability manifest: %s\n' "$capability_manifest" >&2; exit 1; } declare -A agent_tools agent_skills while IFS=$'\t' read -r role tools skills; do [ "$role" != role ] || continue [ -n "$role" ] && [ -n "$tools" ] || { printf 'Invalid capability manifest row.\n' >&2; exit 1; } agent_tools[$role]="$tools" # Optional: skills whose full content Claude preloads into the subagent at startup. agent_skills[$role]="${skills:-}" done < "$capability_manifest" copy_directory() { local source=$1 destination=$2 mkdir -p "$destination" cp -R "$source"/. "$destination"/ } read_field() { local path=$1 name=$2 value # Quit inside sed: piping into head can SIGPIPE the writer, which pipefail turns into exit 141. value=$(sed -n "/^${name}:/{s/^${name}:[[:space:]]*//p;q;}" "$path") [ -n "$value" ] || { printf 'Missing %s in %s\n' "$name" "$path" >&2; exit 1; } printf '%s' "$value" } quote_toml() { local value=$1 value=${value//\\/\\\\} value=${value//\"/\\\"} value=${value//$'\r'/} value=${value//$'\n'/\\n} printf '"%s"' "$value" } session_output=$(bash "$shared/hooks/scripts/test-session-config.sh" "$root") || { printf '%s\n' "$session_output" >&2; exit 1; } if [ "$summary" = true ]; then printf '%s\n' "$session_output" | sed '/^PASS session configuration$/d' else printf '%s\n' "$session_output" fi # Build into a private sibling directory and swap it into place only after validation, so a failed # or parallel build never leaves generated/ partial. Leftovers of crashed runs are removed here: # old trees always, build trees only when their Bash owner process has exited. # ponytail: PowerShell-owned leftovers are left to the PowerShell build, since PIDs are not comparable across shells. for leftover in "$root"/generated.old-* "$root"/generated.build-sh*; do [ -d "$leftover" ] || continue if [[ "$leftover" == */generated.build-sh* ]]; then leftover_pid=${leftover##*/generated.build-sh} leftover_pid=${leftover_pid%%-*} ! kill -0 "$leftover_pid" 2>/dev/null || continue fi rm -rf -- "$leftover" done lock="$root/generated.lock" locked=false retired='' output='' trap 'rm -rf -- ${output:+"$output"}; [ "$locked" != true ] || rm -f -- "$lock"' EXIT # Keep the name short: it lengthens every generated path, which matters on Windows. output=$(mktemp -d "$root/generated.build-sh$$-XXXXXX") chmod "$(printf '%o' $((0777 & ~$(umask))))" "$output" agent_dirs=("$shared"/agents/*/) declare -A agent_seen for agent_dir in "${agent_dirs[@]}"; do name=$(read_field "${agent_dir}agent.yml" name) [ -z "${agent_seen[$name]+x}" ] || { printf 'Duplicate agent name: %s\n' "$name" >&2; exit 1; } agent_seen[$name]=1 done for shell in powershell bash; do mkdir -p "$output/codex-$shell" "$output/claude-$shell" copy_directory "$shared/skills" "$output/codex-$shell/skills" # Claude discovers only skills//SKILL.md, so flatten the categorized source tree. claude_skills="$output/claude-$shell/skills" mkdir -p "$claude_skills" find "$shared/skills" -maxdepth 1 -type f -exec cp {} "$claude_skills"/ \; declare -A claude_skill_names=() while IFS= read -r skill_file; do skill_source=$(dirname -- "$skill_file") skill_name=$(basename -- "$skill_source") sed -n 1,6p "$skill_file" | grep -x "name: $skill_name" >/dev/null || { printf 'Skill name must match its directory: %s\n' "$skill_file" >&2; exit 1; } [ -z "${claude_skill_names[$skill_name]+x}" ] || { printf 'Duplicate Claude skill name: %s\n' "$skill_name" >&2; exit 1; } claude_skill_names[$skill_name]=1 copy_directory "$skill_source" "$claude_skills/$skill_name" done < <(find "$shared/skills" -type f -name SKILL.md | sort) copy_directory "$shared/rules" "$output/codex-$shell/rules" copy_directory "$shared/hooks" "$output/codex-$shell/hooks" copy_directory "$shared/hooks" "$output/claude-$shell/hooks" copy_directory "$shared/statusline" "$output/claude-$shell/statusline" declare -A rule_skill_names=() while IFS= read -r rule_path; do case "$rule_path" in general.md) continue ;; */index.md) skill_name="rules-${rule_path%/index.md}"; rule_body="$shared/rules/$rule_path"; references_dir="${rule_body%/index.md}/references" ;; */*.md) rule_directory=${rule_path%%/*} rule_relative=${rule_path#*/} rule_relative=${rule_relative%.md} rule_relative=${rule_relative#references/} rule_relative=${rule_relative//\//-} skill_name="rules-$rule_directory-$rule_relative" rule_body="$shared/rules/$rule_path" references_dir='' ;; *.md) skill_name="rules-${rule_path%.md}"; rule_body="$shared/rules/$rule_path"; references_dir='' ;; *) continue ;; esac if [[ -n "${rule_skill_names[$skill_name]:-}" || -n "${claude_skill_names[$skill_name]:-}" ]]; then printf 'Duplicate generated rule skill name: %s\n' "$skill_name" >&2 exit 1 fi rule_skill_names[$skill_name]=1 skill_dir="$claude_skills/$skill_name" mkdir -p "$skill_dir" rule_title=$(sed -n '/^#\{1,6\} */{s///p;q;}' "$rule_body") if [ -z "$rule_title" ]; then printf 'Missing rule title in %s\n' "$rule_body" >&2; exit 1; fi # The rule's opening "Apply ..."/"For ..." sentence states when it applies; it is a better trigger than the title. rule_lead=$(awk '!/^#/ && !/^[[:space:]]*$/ { print; exit }' "$rule_body" | tr -d '\r') case "$rule_lead" in 'Apply '*|'For '*) rule_description=${rule_lead%%. *} if [ "$rule_description" != "$rule_lead" ]; then rule_description="$rule_description."; fi rule_description=${rule_description%"${rule_description##*[![:space:]]}"} ;; *) rule_description="Use when the task concerns $rule_title." ;; esac { printf -- '---\nname: %s\ndescription: %s\n---\n\n' "$skill_name" "$(quote_toml "$rule_description")"; cat "$rule_body"; } > "$skill_dir/SKILL.md" if [ -n "$references_dir" ] && [ -d "$references_dir" ]; then copy_directory "$references_dir" "$skill_dir/references" fi done < <(cd "$shared/rules" && find . -mindepth 1 -type f -name '*.md' | sed 's|^\./||' | sort) instructions_content=$(expand_shell_placeholders "$(<"$shared/global-instructions.md")" "$shell") general_content=$(<"$shared/rules/general.md") printf '%s\n\n---\n\n%s\n' "${instructions_content%$'\n'}" "$general_content" > "$output/codex-$shell/AGENTS.md" printf '%s\n\n---\n\n%s\n' "${instructions_content%$'\n'}" "$general_content" > "$output/claude-$shell/CLAUDE.md" cp "$root/adapters/codex/config/config.toml" "$output/codex-$shell/config.toml" # Codex loads every *.rules file under ~/.codex/rules as execpolicy rules; the Markdown rules there are ignored. cp "$root/adapters/codex/rules/my-ai-configuration.rules" "$output/codex-$shell/rules/my-ai-configuration.rules" cp "$root/adapters/claude/config/settings.json" "$output/claude-$shell/settings.json" for client in codex claude; do agents_output="$output/$client-$shell/agents" mkdir -p "$agents_output" for agent_dir in "${agent_dirs[@]}"; do metadata="${agent_dir}agent.yml" name=$(read_field "$metadata" name) description=$(read_field "$metadata" description) instructions=$(<"$root/adapters/$client/orchestration-loader.txt")$'\n\n'$(<"${agent_dir}instructions.md") if [ "$client" = codex ]; then printf 'name = %s\ndescription = %s\ndeveloper_instructions = %s\n' "$(quote_toml "$name")" "$(quote_toml "$description")" "$(quote_toml "$instructions")" > "$agents_output/$name.toml" else tools=$(expand_shell_placeholders "${agent_tools[$name]:-}" "$shell") { printf -- '---\nname: %s\ndescription: %s\n' "$name" "$description" if [ -n "$tools" ]; then printf 'tools: %s\n' "$tools"; fi if [ -n "${agent_skills[$name]:-}" ]; then printf 'skills:\n' IFS=',' read -r -a preload <<<"${agent_skills[$name]}" for skill in "${preload[@]}"; do skill=${skill//[[:space:]]/}; [ -z "$skill" ] || printf ' - %s\n' "$skill"; done fi printf -- '---\n\n%s\n' "$instructions" } > "$agents_output/$name.md" fi done done for path in "$output/codex-$shell/config.toml" "$output/claude-$shell/settings.json"; do content=$(expand_shell_placeholders "$(<"$path")" "$shell") # The sandbox depends on the build platform, not the shell, so it is not part of the placeholder table. content=${content//__CLAUDE_SANDBOX__/"$sandbox"} printf '%s\n' "$content" > "$path" done toml_path="$output/codex-$shell/config.toml" settings_path="$output/claude-$shell/settings.json" run_python "$root/scripts/checks/validate-config.py" "$toml_path" "$settings_path" # Claude-only installs and updates never use the Codex packages, so their schema check is skipped. if [ "$skip_codex_schema" = true ]; then : elif command -v codex >/dev/null; then schema_home=$(mktemp -d) cp "$toml_path" "$schema_home/config.toml" CODEX_HOME="$schema_home" codex --strict-config --help >/dev/null rm -rf -- "$schema_home" elif [ "${REQUIRE_CODEX_SCHEMA:-false}" = true ]; then printf 'Codex CLI is required for schema validation.\n' >&2 exit 1 else printf 'WARN Codex CLI unavailable; skipped Codex schema validation.\n' >&2 fi done # __AI_CONFIG_ROOT__ is resolved at install time, once the destination is known; it is # expected to remain in generated output, so it is excluded from the leftover check. A # single tree-wide grep (rather than one process per file) keeps this fast. leftover_tokens=$(grep -RohE '__[A-Z0-9_]*[A-Z0-9][A-Z0-9_]*__' "$output" 2>/dev/null | sort -u | grep -Ev '^(__AI_CONFIG_ROOT__|__POWERSHELL_COMMAND__|__CLAUDE_CODE_MAX_TOOL_USE_CONCURRENCY__)$' || true) if [ -n "$leftover_tokens" ]; then offending_files=$(grep -RlE '__[A-Z0-9_]*[A-Z0-9][A-Z0-9_]*__' "$output" 2>/dev/null | tr '\n' ' ') printf 'Unresolved template placeholders (%s) in: %s\n' "$(printf '%s' "$leftover_tokens" | tr '\n' ' ')" "$offending_files" >&2 exit 1 fi for ((attempt = 0; attempt < 150; attempt++)); do if (set -o noclobber; : > "$lock") 2>/dev/null; then locked=true; break; fi sleep 0.2 done [ "$locked" = true ] || { printf 'Another build holds %s; wait for it to finish, or remove the file if no build is running.\n' "$lock" >&2; exit 1; } if [ -e "$final" ]; then retired="$root/generated.old-sh$$-$RANDOM" mv -- "$final" "$retired" fi if ! mv -- "$output" "$final"; then [ -z "$retired" ] || mv -- "$retired" "$final" exit 1 fi rm -f -- "$lock" locked=false [ -z "$retired" ] || rm -rf -- "$retired" if [ "$summary" = true ]; then printf 'Build: PASS | 4 packages\n'; else printf 'PASS build: codex-powershell, claude-powershell, codex-bash, claude-bash\n'; fi