KeiSeiKit-1.0/install/lib-profile.sh
KeiSei84 abae256c1d feat(install): opt-in hook packs + stack profiles (public posture)
A fresh install now activates only the safety pack; discipline hooks and
agents are opt-in via an onboarding step (step 6) or `kei configure`.
"People don't need Rust-only" — they pick their own stack.

- _primitives/hook-packs.toml: SSoT mapping pack -> hooks, stack -> packs +
  agent groups. safety always on; evidence/observability/epistemic/
  orchestration/git-guard/stack-rust opt-in. rust-first/no-python only under
  the systems stack; git-guard (no-github-push) opt-in only, pulled by no stack.
- lib-profile: extract generic _toml_array (reused by lib-packs); profile_members
  becomes a thin wrapper (no behavior change).
- lib-packs: pack/stack/agent resolvers + selection loader.
- lib-hooks: filter_snippet_by_packs (install-time allowlist) + prune_kit_hooks
  (reconfigure removes deselected kit hooks, keeps foreign ones); activate_hooks
  rewired to prune + filter + merge. No custom settings.json fields (/doctor safe).
- lib-agents: install_manifests filters by stack agent set (empty = install all).
- onboarding: pick_stack step (reuse _onb_read_choice), persists stack_profile +
  enabled_packs to onboarding.toml; i18n STR_* added.
- bin/kei configure -> scripts/kei-configure.sh (re-pick without reinstall);
  install stamps ~/.claude/.kei-kit-dir.
- numeric-claims-guard: money regex no longer matches shell positionals ($1..$9);
  requires decimal / unit / 2+ digits / tilde. Real money + time still caught.
- gate one-liner added to 8 discipline hooks (runtime toggle via hooks-control).

Verified end-to-end (scratch HOME): fresh=safety only; evidence pack adds
numeric+citation; systems stack wires rust-first + 14 base/systems agents (no
data-science/swift); reconfigure-shrink prunes kit hooks but keeps a foreign
hook; settings schema clean; assembler golden 3/3.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 17:27:14 +08:00

125 lines
3.3 KiB
Bash

# shellcheck shell=bash
# lib-profile.sh — MANIFEST.toml parser + profile resolver.
#
# Tiny awk-based TOML reader with optional Python fallback for robustness.
# Two shapes used:
# 1. profile.<name> = ["a", "b", ...]
# 2. [primitive.<name>] kind/file/crate/deps/desc
#
# If tomllib (python3.11+) or toml is available, prefer it. Otherwise awk.
#
# Requires: $MANIFEST (set by install.sh).
# Requires: err from lib-log.sh.
have_python_toml() {
if command -v python3 >/dev/null 2>&1; then
python3 -c 'import tomllib' >/dev/null 2>&1 && return 0
python3 -c 'import toml' >/dev/null 2>&1 && return 0
fi
return 1
}
# Generic one-line-array TOML reader. Echoes space-separated values of
# [<table>]
# <key> = ["a", "b", ...]
# python-tomllib preferred; awk fallback handles one-line arrays only.
# Usage: _toml_array <file> <table> <key>
_toml_array() {
local file="$1" table="$2" key="$3"
[ -f "$file" ] || return 1
if have_python_toml; then
python3 - "$file" "$table" "$key" <<'PY' 2>/dev/null || return 1
import sys
try:
import tomllib
mode = "rb"
except ImportError:
import toml as tomllib
mode = "r"
path, table, key = sys.argv[1], sys.argv[2], sys.argv[3]
with open(path, mode) as f:
data = tomllib.load(f)
vals = data.get(table, {}).get(key)
if vals is None:
sys.exit(2)
print(" ".join(vals))
PY
else
awk -v table="$table" -v key="$key" '
$0 ~ "^\\[" table "\\]" { in_t=1; next }
/^\[/ { in_t=0 }
in_t && $0 ~ "^[[:space:]]*" key "[[:space:]]*=" {
line = $0
sub(/^[^\[]*\[/, "", line)
sub(/\].*$/, "", line)
gsub(/"/, "", line)
gsub(/,/, " ", line)
print line
exit
}
' "$file"
fi
}
# Echo space-separated primitive names for a given profile.
# Usage: profile_members <profile-name>
profile_members() {
local profile="$1"
[ -f "$MANIFEST" ] || { err "MANIFEST.toml not found at $MANIFEST"; return 1; }
_toml_array "$MANIFEST" "profile" "$profile"
}
# Echo a field of a primitive. Usage: primitive_field <name> <field>
# field ∈ { kind, file, crate, desc, deps }
primitive_field() {
local name="$1" field="$2"
[ -f "$MANIFEST" ] || return 1
if have_python_toml; then
python3 - "$MANIFEST" "$name" "$field" <<'PY' 2>/dev/null
import sys
try:
import tomllib
mode = "rb"
except ImportError:
import toml as tomllib
mode = "r"
path, name, field = sys.argv[1], sys.argv[2], sys.argv[3]
with open(path, mode) as f:
data = tomllib.load(f) if mode == "rb" else tomllib.load(f)
p = data.get("primitive", {}).get(name)
if p is None:
sys.exit(2)
v = p.get(field, "")
if isinstance(v, list):
print("; ".join(v))
else:
print(v)
PY
else
awk -v pname="$name" -v fname="$field" '
$0 ~ "^\\[primitive\\." pname "\\]" { in_p=1; next }
/^\[/ && in_p { in_p=0 }
in_p && $0 ~ "^[[:space:]]*" fname "[[:space:]]*=" {
line = $0
sub(/^[^=]*=[[:space:]]*/, "", line)
gsub(/^"/, "", line)
gsub(/"$/, "", line)
print line
exit
}
' "$MANIFEST"
fi
}
# Echo all primitive names defined in MANIFEST.
all_primitive_names() {
[ -f "$MANIFEST" ] || return 1
awk '
/^\[primitive\./ {
name = $0
sub(/^\[primitive\./, "", name)
sub(/\]$/, "", name)
print name
}
' "$MANIFEST"
}