generated from coulomb/repo-seed
- Fix /domains/{slug}/ 500: EP/TD queries now use domain_id FK (not string column)
- Remove dead cascade-slug code in rename_domain (FK handles it)
- MCP: list_kaizen_agents(category?) + get_kaizen_agent(name) via resolve_repo_path()
- TOOLS.md: Kaizen Agents section with discovery/load pattern
- agents.template: new project rule for consumer repos
- claude-md.template + register_project.sh: include agents.md in new-project scaffolding
- agents/: direct install of 6 curated agents for hub sessions
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
266 lines
11 KiB
Bash
Executable File
266 lines
11 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# register_project.sh — register a project/repo with the Custodian State Hub
|
|
#
|
|
# Usage: scripts/register_project.sh <domain> <project_path> [--additional]
|
|
# domain: slug of an active domain (e.g. custodian, railiance)
|
|
# project_path: absolute path to the project directory
|
|
# --additional: add a second repo to an existing domain; skip CLAUDE.md generation
|
|
#
|
|
# Example:
|
|
# scripts/register_project.sh railiance /home/worsch/railiance
|
|
# scripts/register_project.sh railiance /home/worsch/railiance-infra --additional
|
|
#
|
|
# What it does:
|
|
# 1. Verify the API is reachable
|
|
# 2. Verify the domain exists via GET /domains/{slug}/
|
|
# 3. Look up the topic ID for the domain (first active topic)
|
|
# 4. Check that state-hub is in ~/.claude.json; warn if missing
|
|
# 5. Write CLAUDE.md + .claude/rules/*.md (modular @-import structure)
|
|
# - If CLAUDE.md already exists, writes .claude/rules/ only and appends
|
|
# an @-import comment block to the existing CLAUDE.md
|
|
# 6. POST to /repos/ to register the repo
|
|
# 7. POST /repos/{slug}/paths/ to register this machine's local path (host_paths)
|
|
# 8. POST a progress event recording the registration
|
|
|
|
set -euo pipefail
|
|
|
|
DOMAIN="${1:-}"
|
|
PROJECT_PATH="${2:-}"
|
|
ADDITIONAL="${3:-}"
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
STATE_HUB_DIR="$(dirname "$SCRIPT_DIR")"
|
|
RULES_TEMPLATES_DIR="$SCRIPT_DIR/project_rules"
|
|
API_BASE="${API_BASE:-http://127.0.0.1:8000}"
|
|
|
|
# ── Validate args ──────────────────────────────────────────────────────────────
|
|
if [[ -z "$DOMAIN" || -z "$PROJECT_PATH" ]]; then
|
|
echo "Usage: $0 <domain> <project_path> [--additional]"
|
|
echo " domain: slug of an active domain in the State Hub"
|
|
echo " project_path: absolute path to project directory"
|
|
echo " --additional: register a second repo; skip CLAUDE.md generation"
|
|
exit 1
|
|
fi
|
|
|
|
if [[ ! -d "$PROJECT_PATH" ]]; then
|
|
echo "ERROR: project_path does not exist: $PROJECT_PATH"
|
|
exit 1
|
|
fi
|
|
|
|
PROJECT_NAME="$(basename "$PROJECT_PATH")"
|
|
REPO_SLUG="$(echo "$PROJECT_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g')"
|
|
|
|
# Derive a workplan prefix: uppercase first token + -WP (e.g. marki-docx → MARKI-WP)
|
|
FIRST_TOKEN="$(echo "$REPO_SLUG" | cut -d'-' -f1 | tr '[:lower:]' '[:upper:]')"
|
|
WP_PREFIX="${FIRST_TOKEN}-WP"
|
|
|
|
# ── Step 1: API health check ───────────────────────────────────────────────────
|
|
echo "==> Checking API at $API_BASE ..."
|
|
if ! curl -sf "$API_BASE/state/health" > /dev/null; then
|
|
echo "ERROR: State Hub API is not reachable."
|
|
echo " Start it: cd $STATE_HUB_DIR && make api"
|
|
echo " (requires postgres: make db first)"
|
|
exit 1
|
|
fi
|
|
echo " API OK"
|
|
|
|
# ── Step 2: Verify domain exists ───────────────────────────────────────────────
|
|
echo "==> Verifying domain '$DOMAIN' ..."
|
|
DOMAIN_JSON="$(curl -sf "$API_BASE/domains/$DOMAIN/" 2>/dev/null || echo 'NOT_FOUND')"
|
|
if [[ "$DOMAIN_JSON" == "NOT_FOUND" ]] || ! echo "$DOMAIN_JSON" | python3 -c "import json,sys; d=json.load(sys.stdin); sys.exit(0 if d.get('slug') else 1)" 2>/dev/null; then
|
|
echo "ERROR: Domain '$DOMAIN' not found in the State Hub."
|
|
echo " To create: make add-domain DOMAIN=$DOMAIN NAME=\"<display name>\""
|
|
echo " To list available: curl -s $API_BASE/domains/ | python3 -m json.tool"
|
|
exit 1
|
|
fi
|
|
echo " Domain OK"
|
|
|
|
# ── Step 3: Look up topic ID ───────────────────────────────────────────────────
|
|
echo "==> Looking up topic for domain '$DOMAIN' ..."
|
|
TOPICS_JSON="$(curl -sf "$API_BASE/topics/?status=active")"
|
|
TOPIC_ID="$(echo "$TOPICS_JSON" | python3 -c "
|
|
import json, sys
|
|
topics = json.load(sys.stdin)
|
|
match = next((t for t in topics if t.get('domain_slug') == sys.argv[1]), None)
|
|
print(match['id'] if match else 'NOT_FOUND')
|
|
" "$DOMAIN")"
|
|
|
|
if [[ "$TOPIC_ID" == "NOT_FOUND" ]]; then
|
|
echo "WARNING: No active topic found for domain '$DOMAIN'. repo-identity.md will omit topic_id."
|
|
TOPIC_ID="(none)"
|
|
else
|
|
echo " topic_id: $TOPIC_ID"
|
|
fi
|
|
|
|
# ── Step 4: Check MCP registration ────────────────────────────────────────────
|
|
echo "==> Checking MCP server registration ..."
|
|
MCP_OK="$(python3 -c "
|
|
import json
|
|
from pathlib import Path
|
|
f = Path.home() / '.claude.json'
|
|
if not f.exists():
|
|
print('MISSING_FILE')
|
|
else:
|
|
d = json.loads(f.read_text())
|
|
print('OK' if 'state-hub' in d.get('mcpServers', {}) else 'NOT_REGISTERED')
|
|
")"
|
|
case "$MCP_OK" in
|
|
MISSING_FILE) echo "WARNING: ~/.claude.json not found. MCP server not registered." ;;
|
|
NOT_REGISTERED) echo "WARNING: 'state-hub' not in ~/.claude.json. See global CLAUDE.md §MCP Server Registration." ;;
|
|
*) echo " MCP OK" ;;
|
|
esac
|
|
|
|
# ── Helper: render a template with variable substitution ──────────────────────
|
|
render_template() {
|
|
local tmpl="$1"
|
|
sed \
|
|
-e "s|{PROJECT_NAME}|$PROJECT_NAME|g" \
|
|
-e "s|{PROJECT_DESCRIPTION}|$PROJECT_NAME — (fill in purpose)|g" \
|
|
-e "s|{DOMAIN}|$DOMAIN|g" \
|
|
-e "s|{TOPIC_ID}|$TOPIC_ID|g" \
|
|
-e "s|{REPO_SLUG}|$REPO_SLUG|g" \
|
|
-e "s|{WP_PREFIX}|$WP_PREFIX|g" \
|
|
"$tmpl"
|
|
}
|
|
|
|
# ── Step 5: Write .claude/rules/ + CLAUDE.md ──────────────────────────────────
|
|
if [[ "$ADDITIONAL" != "--additional" ]]; then
|
|
RULES_DIR="$PROJECT_PATH/.claude/rules"
|
|
CLAUDE_MD="$PROJECT_PATH/CLAUDE.md"
|
|
|
|
echo "==> Writing .claude/rules/ ..."
|
|
mkdir -p "$RULES_DIR"
|
|
|
|
for rule in repo-identity session-protocol first-session workplan-convention \
|
|
stack-and-commands architecture repo-boundary agents; do
|
|
tmpl="$RULES_TEMPLATES_DIR/${rule}.template"
|
|
out="$RULES_DIR/${rule}.md"
|
|
if [[ -f "$tmpl" ]]; then
|
|
render_template "$tmpl" > "$out"
|
|
echo " .claude/rules/${rule}.md"
|
|
else
|
|
echo "WARNING: template missing: $tmpl"
|
|
fi
|
|
done
|
|
|
|
if [[ -f "$CLAUDE_MD" ]]; then
|
|
echo ""
|
|
echo "==> CLAUDE.md already exists — appending @-import suggestion."
|
|
cat >> "$CLAUDE_MD" << 'SUGGESTION'
|
|
|
|
<!-- ── Custodian State Hub Integration (added by register_project.sh) ──────
|
|
The rule files in .claude/rules/ contain the full integration content.
|
|
Replace this file's body with the @-import index below once you have
|
|
merged any existing project-specific content into the rule files.
|
|
|
|
# PROJECT_NAME — Claude Code Instructions
|
|
|
|
@.claude/rules/repo-identity.md
|
|
@.claude/rules/session-protocol.md
|
|
@.claude/rules/first-session.md
|
|
@.claude/rules/workplan-convention.md
|
|
@.claude/rules/stack-and-commands.md
|
|
@.claude/rules/architecture.md
|
|
@.claude/rules/repo-boundary.md
|
|
-->
|
|
SUGGESTION
|
|
echo " Suggestion appended to existing CLAUDE.md."
|
|
else
|
|
echo "==> Writing CLAUDE.md ..."
|
|
render_template "$RULES_TEMPLATES_DIR/claude-md.template" > "$CLAUDE_MD"
|
|
echo " CLAUDE.md written."
|
|
fi
|
|
|
|
echo ""
|
|
echo "Rule files needing manual content:"
|
|
echo " .claude/rules/repo-identity.md — update purpose description"
|
|
echo " .claude/rules/stack-and-commands.md — language, deps, dev commands"
|
|
echo " .claude/rules/architecture.md — design overview"
|
|
echo " .claude/rules/repo-boundary.md — what this repo does NOT own"
|
|
echo " .claude/rules/workplan-convention.md — verify WP prefix: $WP_PREFIX"
|
|
fi
|
|
|
|
# ── Step 6: Register repo in State Hub ────────────────────────────────────────
|
|
echo ""
|
|
echo "==> Registering repo '$PROJECT_NAME' under domain '$DOMAIN' ..."
|
|
REPO_PAYLOAD="$(python3 -c "
|
|
import json
|
|
print(json.dumps({
|
|
'domain_slug': '$DOMAIN',
|
|
'slug': '$REPO_SLUG',
|
|
'name': '$PROJECT_NAME',
|
|
'local_path': '$PROJECT_PATH',
|
|
}))
|
|
")"
|
|
|
|
REPO_RESULT="$(curl -sf -X POST "$API_BASE/repos/" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$REPO_PAYLOAD" 2>/dev/null || echo 'CONFLICT')"
|
|
|
|
if [[ "$REPO_RESULT" == "CONFLICT" ]]; then
|
|
echo " Repo '$REPO_SLUG' already registered — skipping POST."
|
|
else
|
|
echo " Repo registered: $REPO_SLUG"
|
|
fi
|
|
|
|
# ── Step 7: Register this machine's local path ────────────────────────────────
|
|
echo "==> Registering host path for $(hostname) ..."
|
|
curl -sf -X POST "$API_BASE/repos/$REPO_SLUG/paths/" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{\"host\": \"$(hostname)\", \"path\": \"$PROJECT_PATH\"}" > /dev/null \
|
|
&& echo " host_paths[$(hostname)] = $PROJECT_PATH"
|
|
|
|
# ── Step 8: Record progress event ─────────────────────────────────────────────
|
|
echo "==> Recording registration event ..."
|
|
python3 - <<PYEOF
|
|
import json, urllib.request
|
|
payload = {
|
|
'event_type': 'milestone',
|
|
'summary': 'Project registered with State Hub: $PROJECT_NAME ($DOMAIN)',
|
|
'author': 'custodian',
|
|
'detail': {
|
|
'project_path': '$PROJECT_PATH',
|
|
'domain': '$DOMAIN',
|
|
'repo_slug': '$REPO_SLUG',
|
|
'host': '$(hostname)',
|
|
},
|
|
}
|
|
$([ "$TOPIC_ID" != "(none)" ] && echo "payload['topic_id'] = '$TOPIC_ID'" || echo "")
|
|
req = urllib.request.Request(
|
|
'$API_BASE/progress/',
|
|
data=json.dumps(payload).encode(),
|
|
headers={'Content-Type': 'application/json'},
|
|
method='POST',
|
|
)
|
|
urllib.request.urlopen(req)
|
|
PYEOF
|
|
echo " Event recorded."
|
|
|
|
echo ""
|
|
echo "Registration complete!"
|
|
echo " Project: $PROJECT_NAME"
|
|
echo " Domain: $DOMAIN"
|
|
echo " Repo slug: $REPO_SLUG"
|
|
[[ "$TOPIC_ID" != "(none)" ]] && echo " Topic ID: $TOPIC_ID"
|
|
echo " WP prefix: $WP_PREFIX (update .claude/rules/workplan-convention.md if wrong)"
|
|
echo ""
|
|
echo "Next steps:"
|
|
echo " 1. Fill in TODO stubs in .claude/rules/"
|
|
echo " 2. Restart Claude Code for the MCP server to be available in this project"
|
|
|
|
# ── Optional: SBOM ingest ─────────────────────────────────────────────────────
|
|
if [[ "$ADDITIONAL" != "--additional" ]]; then
|
|
echo ""
|
|
read -r -p "==> Run SBOM ingest now? [y/N] " INGEST_NOW </dev/tty || INGEST_NOW="N"
|
|
if [[ "$INGEST_NOW" =~ ^[Yy]$ ]]; then
|
|
INGEST_PY="$STATE_HUB_DIR/.venv/bin/python"
|
|
if [[ -x "$INGEST_PY" ]]; then
|
|
"$INGEST_PY" "$SCRIPT_DIR/ingest_sbom.py" \
|
|
--repo "$REPO_SLUG" \
|
|
--repo-path "$PROJECT_PATH" \
|
|
--api-base "$API_BASE" && echo " SBOM ingested." || echo " SBOM ingest failed (non-fatal)."
|
|
else
|
|
echo " Skipping: .venv not found. Run 'make install' then 'make ingest-sbom REPO=$REPO_SLUG'."
|
|
fi
|
|
fi
|
|
fi
|