KeiSeiKit-1.0/install/lib-agents.sh
Parfii-bot 03d1dc7362 refactor(v0.16): split install.sh monolith (1238 LOC) into 17 cubes
Constructor Pattern (RULE ZERO). Zero behaviour change, zero flag
drift — all original CLI flags preserved verbatim.

Before: install.sh — 1238 LOC monolith
After:  install.sh — 138 LOC dispatcher (sources libs in order)
        install/lib-*.sh — 16 cubes, max 183 LOC (lib-menu)

Cubes:
  lib-log       21 LOC — logging primitives
  lib-backup    63 LOC — rollback trap + BACKUP_PAIRS
  lib-profile  115 LOC — MANIFEST.toml profile resolution
  lib-args      92 LOC — CLI parsing + --help heredoc
  lib-menu     183 LOC — whiptail/dialog/plain-text interactive picker
  lib-plan     150 LOC — dry-run --no-execute output
  lib-prereqs   91 LOC — hard + soft dependency checks
  lib-primitives 131 LOC — primitive copy + MANIFEST drive
  lib-rust     114 LOC — cargo workspace build + pre-built support
  lib-scaffold 144 LOC — agent/skill/block scaffolding
  lib-bridges   31 LOC — project-bridge install
  lib-hooks    104 LOC — settings.json jq merge
  lib-agents    77 LOC — assembled agent output
  lib-skills    23 LOC — skill copy
  lib-wizard    20 LOC — sleep-setup wizard invocation
  lib-summary   59 LOC — post-install summary

Invariants preserved:
  - macOS bash 3.2 compat (no associative arrays, no [[ ]], no ${,,})
  - rollback trap wired via setup_backup_trap early in dispatcher
  - jq-merge behaviour verbatim in lib-hooks
  - scoped Cargo.toml regeneration in lib-rust

Function LOC limits: largest non-heredoc fn 22 LOC (check_soft_prereqs).
Three functions kept >30 LOC because heredoc-dominated (print_help,
print_summary, profile_members); splitting would fragment logical unit.

62 unique function names across cubes, zero duplicates (grep-verified).

bash -n passes on all 17 files. Runtime smoke test deferred to user's
shell (bash-readonly sandbox constraint).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 15:09:35 +08:00

77 lines
2.9 KiB
Bash

# shellcheck shell=bash
# lib-agents.sh — manifest copy + assembler build + .md agent generation.
#
# Generic manifests: overwrite-skip policy (never stomp user's existing
# manifests). Assembler source: always refreshed. Build: offline-first with
# online fallback. Agents: written in-place by the built assemble binary.
#
# Requires: say / err from lib-log.sh.
# Requires: backup_dir from lib-backup.sh.
# Reads globals: $KIT_DIR, $AGENTS_DIR.
# Copy _manifests/*.toml from the kit; skip any target file that already
# exists (user manifests are sacred). Also copies _templates/*.template
# when present.
install_manifests() {
say "copying generic manifests -> $AGENTS_DIR/_manifests/ (skip if exists)"
local copied=0 skipped=0 f name t has_templates=0
for f in "$KIT_DIR/_manifests/"*.toml; do
name="$(basename "$f")"
if [[ -f "$AGENTS_DIR/_manifests/$name" ]]; then
skipped=$((skipped+1))
else
cp "$f" "$AGENTS_DIR/_manifests/$name"
copied=$((copied+1))
fi
done
say " copied $copied, skipped $skipped (already present)"
for t in "$KIT_DIR/_templates/"*.template; do
[ -f "$t" ] && { has_templates=1; break; }
done
if [ "$has_templates" = "1" ]; then
say "copying specialist template"
backup_dir "$AGENTS_DIR/_templates"
cp -f "$KIT_DIR/_templates/"*.template "$AGENTS_DIR/_templates/"
fi
}
# Refresh _blocks/*.md — SSoT is the kit, always overwritten after backup.
install_blocks() {
say "copying shared blocks -> $AGENTS_DIR/_blocks/"
backup_dir "$AGENTS_DIR/_blocks"
cp -f "$KIT_DIR/_blocks/"*.md "$AGENTS_DIR/_blocks/"
}
# Copy the Rust assembler source (Cargo.toml + src/*.rs + .gitignore if any).
# Caller should run build_assembler afterwards.
copy_assembler_source() {
say "copying assembler source"
backup_dir "$AGENTS_DIR/_assembler"
cp -f "$KIT_DIR/_assembler/Cargo.toml" "$AGENTS_DIR/_assembler/"
cp -f "$KIT_DIR/_assembler/src/"*.rs "$AGENTS_DIR/_assembler/src/"
if [[ -f "$KIT_DIR/_assembler/.gitignore" ]]; then
cp -f "$KIT_DIR/_assembler/.gitignore" "$AGENTS_DIR/_assembler/"
fi
}
# Build the assembler (release, offline-first, online fallback).
# Exits 2 if the binary is missing after a reported success (disk failure).
build_assembler() {
copy_assembler_source
say "building Rust assembler (cargo build --release, offline first)"
if ! ( cd "$AGENTS_DIR/_assembler" && cargo build --release --offline ) 2>/tmp/keiseikit-cargo-offline.log; then
say "offline build failed — fetching deps from crates.io"
( cd "$AGENTS_DIR/_assembler" && cargo build --release )
fi
if [[ ! -x "$AGENTS_DIR/_assembler/target/release/assemble" ]]; then
err "build succeeded but binary not found at $AGENTS_DIR/_assembler/target/release/assemble"
exit 2
fi
}
# Run the built assembler in --in-place mode to write the agent .md files.
generate_agents() {
say "generating agent .md files (--in-place)"
AGENT_ROOT="$AGENTS_DIR" "$AGENTS_DIR/_assembler/target/release/assemble" --in-place
}