scripts/bash/team-init.sh
#!/usr/bin/env bash
# team-init.sh — Brownfield CDR discovery entry point
set -euo pipefail
# team-init delegates CDR lifecycle operations to the team-levelup skill.
# Invoke the team-levelup skill for CDR work; the path/numbering helpers
# below are vendored self-contained (no cross-skill sourcing per scope rule).
resolve_team_levelup_paths() {
PROJECT_ROOT="${PROJECT_ROOT:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}"
BRANCH="${BRANCH:-$(git branch --show-current 2>/dev/null || echo 'unknown')}"
TEAM_AI_DIRECTIVES="${TEAM_AI_DIRECTIVES:-}"
if [[ -z "$TEAM_AI_DIRECTIVES" && -f "${PROJECT_ROOT}/.adlc/init-options.json" ]]; then
TEAM_AI_DIRECTIVES=$(grep '"team_ai_directives"' "${PROJECT_ROOT}/.adlc/init-options.json" \
| sed -n 's/.*"team_ai_directives"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' \
| head -1)
fi
[[ -z "$TEAM_AI_DIRECTIVES" || "$TEAM_AI_DIRECTIVES" = "null" ]] && TEAM_AI_DIRECTIVES="${PROJECT_ROOT}/team-ai-directives"
ADLC_BRANCH="adlc"
ADLC_WORKTREE="/tmp/adlc-worktree-$$"
echo "PROJECT_ROOT=$PROJECT_ROOT"
echo "TEAM_AI_DIRECTIVES=$TEAM_AI_DIRECTIVES"
echo "BRANCH=$BRANCH"
echo "ADLC_BRANCH=$ADLC_BRANCH"
echo "ADLC_WORKTREE=$ADLC_WORKTREE"
}
output_json() {
resolve_team_levelup_paths >/dev/null
jq -n -c \
--arg repo_root "$PROJECT_ROOT" \
--arg team_ai_directives "$TEAM_AI_DIRECTIVES" \
--arg branch "$BRANCH" \
--arg adlc_branch "$ADLC_BRANCH" \
--arg adlc_worktree "$ADLC_WORKTREE" \
'{"REPO_ROOT":$repo_root,"TEAM_AI_DIRECTIVES":$team_ai_directives,"BRANCH":$branch,"ADLC_BRANCH":$adlc_branch,"ADLC_WORKTREE":$adlc_worktree}'
}
ensure_adlc_branch() {
local td="${1:-$TEAM_AI_DIRECTIVES}"
if ! git -C "$td" show-ref --verify --quiet "refs/heads/$ADLC_BRANCH"; then
git -C "$td" branch --orphan "$ADLC_BRANCH"
git -C "$td" worktree add "$ADLC_WORKTREE" "$ADLC_BRANCH" 2>/dev/null
mkdir -p "$ADLC_WORKTREE/drafts/cdr" "$ADLC_WORKTREE/reports/sessions" "$ADLC_WORKTREE/reports/projects"
git -C "$ADLC_WORKTREE" add -A
git -C "$ADLC_WORKTREE" commit --allow-empty -m "Initialize adlc orphan branch"
git -C "$ADLC_WORKTREE" push origin "$ADLC_BRANCH" 2>/dev/null || true
git -C "$td" worktree remove "$ADLC_WORKTREE" 2>/dev/null || true
fi
}
next_cdr_number() {
local td="${1:-$TEAM_AI_DIRECTIVES}"
local max=0
for f in $(git -C "$td" ls-tree --name-only "$ADLC_BRANCH" "drafts/cdr/" 2>/dev/null | sort); do
local base num
base=$(basename "$f")
[[ "$base" == *.md ]] || continue
num=$(printf '%s' "${base%.md}" | sed -n 's/^CDR-\([0-9]\{1,\}\).*/\1/p')
[[ -n "$num" ]] || continue
((10#$num > max)) && max=$((10#$num))
done
printf '%03d' $((max + 1))
}
if [[ "${1:-}" == "--setup" ]]; then
output_json
exit 0
fi
# Resolve paths (function sets PROJECT_ROOT/TEAM_AI_DIRECTIVES/BRANCH globals
# directly; output is informational only — never eval it)
resolve_team_levelup_paths >/dev/null
if [[ -z "$TEAM_AI_DIRECTIVES" ]] || [[ ! -d "$TEAM_AI_DIRECTIVES" ]]; then
echo "Team AI directives repository not configured."
echo "Run: /team-setup"
exit 1
fi
# Ensure adlc orphan branch exists
ensure_adlc_branch "$TEAM_AI_DIRECTIVES"
# Get next CDR number
NEXT_CDR=$(next_cdr_number "$TEAM_AI_DIRECTIVES")
echo "REPO_ROOT=$PROJECT_ROOT"
echo "TEAM_AI_DIRECTIVES=$TEAM_AI_DIRECTIVES"
echo "NEXT_CDR=$NEXT_CDR"
echo "ADLC_BRANCH=$ADLC_BRANCH"
echo ""
echo "Ready for multi-agent brownfield analysis."
echo "CDRs will be written to: $TEAM_AI_DIRECTIVES (adlc branch, drafts/cdr/)"
scripts/powershell/team-init.ps1
# team-init.ps1 — Brownfield CDR discovery entry point
# (PowerShell twin of bash/team-init.sh; one behavior, two platform scripts.)
$ErrorActionPreference = 'Stop'
$script:ErrorView = 'ConciseView'
# team-init delegates CDR lifecycle operations to the team-levelup skill.
# Invoke the team-levelup skill for CDR work; the path/numbering functions
# below are vendored self-contained (no cross-skill sourcing per scope rule).
function Resolve-TeamLevelupPaths {
if (-not $env:PROJECT_ROOT) {
$root = @(git rev-parse --show-toplevel 2>$null)
if ($LASTEXITCODE -eq 0 -and $root.Count -gt 0 -and $root[0]) {
$script:PROJECT_ROOT = [string]$root[0]
} else {
$script:PROJECT_ROOT = (Get-Location).Path
}
} else {
$script:PROJECT_ROOT = $env:PROJECT_ROOT
}
if (-not $env:BRANCH) {
$b = @(git branch --show-current 2>$null)
if ($LASTEXITCODE -eq 0 -and $b.Count -gt 0 -and $b[0]) {
$script:BRANCH = [string]$b[0]
} else {
$script:BRANCH = 'unknown'
}
} else {
$script:BRANCH = $env:BRANCH
}
$script:TEAM_AI_DIRECTIVES = if ($env:TEAM_AI_DIRECTIVES) { $env:TEAM_AI_DIRECTIVES } else { '' }
if ($script:TEAM_AI_DIRECTIVES -eq '' -and (Test-Path (Join-Path $script:PROJECT_ROOT '.adlc/init-options.json'))) {
$m = Select-String -LiteralPath (Join-Path $script:PROJECT_ROOT '.adlc/init-options.json') -Pattern '"team_ai_directives"\s*:\s*"([^"]*)"' | Select-Object -First 1
if ($m -and $m.Matches.Count -gt 0) { $script:TEAM_AI_DIRECTIVES = $m.Matches[0].Groups[1].Value }
}
if ($script:TEAM_AI_DIRECTIVES -eq '' -or $script:TEAM_AI_DIRECTIVES -eq 'null') {
$script:TEAM_AI_DIRECTIVES = Join-Path $script:PROJECT_ROOT 'team-ai-directives'
}
$script:ADLC_BRANCH = 'adlc'
$script:ADLC_WORKTREE = "/tmp/adlc-worktree-$PID"
Write-Output "PROJECT_ROOT=$($script:PROJECT_ROOT)"
Write-Output "TEAM_AI_DIRECTIVES=$($script:TEAM_AI_DIRECTIVES)"
Write-Output "BRANCH=$($script:BRANCH)"
Write-Output "ADLC_BRANCH=$($script:ADLC_BRANCH)"
Write-Output "ADLC_WORKTREE=$($script:ADLC_WORKTREE)"
}
function Output-Json {
$null = Resolve-TeamLevelupPaths
$payload = [ordered]@{
REPO_ROOT = $script:PROJECT_ROOT
TEAM_AI_DIRECTIVES = $script:TEAM_AI_DIRECTIVES
BRANCH = $script:BRANCH
ADLC_BRANCH = $script:ADLC_BRANCH
ADLC_WORKTREE = $script:ADLC_WORKTREE
}
Write-Output ($payload | ConvertTo-Json -Compress)
}
function Ensure-AdlcBranch {
param([string]$Td)
if (-not $Td) { $Td = $script:TEAM_AI_DIRECTIVES }
git -C $Td show-ref --verify --quiet "refs/heads/$($script:ADLC_BRANCH)" *> $null
if ($LASTEXITCODE -eq 0) { return }
git -C $Td branch --orphan $script:ADLC_BRANCH *> $null
git -C $Td worktree add $script:ADLC_WORKTREE $script:ADLC_BRANCH *> $null
New-Item -ItemType Directory -Force -Path (Join-Path $script:ADLC_WORKTREE 'drafts/cdr'), (Join-Path $script:ADLC_WORKTREE 'reports/sessions'), (Join-Path $script:ADLC_WORKTREE 'reports/projects') | Out-Null
git -C $script:ADLC_WORKTREE add -A *> $null
git -C $script:ADLC_WORKTREE commit --allow-empty -m 'Initialize adlc orphan branch' *> $null
git -C $script:ADLC_WORKTREE push origin $script:ADLC_BRANCH *> $null
git -C $Td worktree remove $script:ADLC_WORKTREE *> $null
}
function Get-NextCdrNumber {
param([string]$Td)
if (-not $Td) { $Td = $script:TEAM_AI_DIRECTIVES }
$max = 0
$files = @(git -C $Td ls-tree --name-only $script:ADLC_BRANCH 'drafts/cdr/' 2>$null)
foreach ($f in ($files | Sort-Object)) {
$name = [System.IO.Path]::GetFileName($f)
if ($name -cnotlike '*.md') { continue }
if ($name.Substring(0, $name.Length - 3) -cmatch '^CDR-(\d+)') {
$num = [int]$Matches[1]
if ($num -gt $max) { $max = $num }
}
}
return ('{0:d3}' -f ($max + 1))
}
if ($args.Count -ge 1 -and [string]$args[0] -eq '--setup') {
Output-Json
exit 0
}
# Resolve paths (function sets the script-scoped globals directly; output is
# informational only — never eval it)
$null = Resolve-TeamLevelupPaths
if (-not $script:TEAM_AI_DIRECTIVES -or -not (Test-Path -LiteralPath $script:TEAM_AI_DIRECTIVES -PathType Container)) {
[Console]::Error.WriteLine('Team AI directives repository not configured.')
[Console]::Error.WriteLine('Run: /team-setup')
exit 1
}
# Ensure adlc orphan branch exists
Ensure-AdlcBranch $script:TEAM_AI_DIRECTIVES
# Get next CDR number
$NEXT_CDR = Get-NextCdrNumber $script:TEAM_AI_DIRECTIVES
Write-Output "REPO_ROOT=$($script:PROJECT_ROOT)"
Write-Output "TEAM_AI_DIRECTIVES=$($script:TEAM_AI_DIRECTIVES)"
Write-Output "NEXT_CDR=$NEXT_CDR"
Write-Output "ADLC_BRANCH=$($script:ADLC_BRANCH)"
Write-Output ''
Write-Output 'Ready for multi-agent brownfield analysis.'
Write-Output "CDRs will be written to: $($script:TEAM_AI_DIRECTIVES) (adlc branch, drafts/cdr/)"
SKILL.md
---
name: team-init
description: Use when bootstrapping team knowledge from a brownfield codebase — reverse-engineers CDRs for contribution to team-ai-directives adlc branch.
disable-model-invocation: true
scripts:
sh: scripts/bash/team-init.sh
ps: scripts/powershell/team-init.ps1
---
# team-init
## What this skill does
Reverse-engineer **Context Directive Records (CDRs)** from an **existing
codebase** (brownfield) to document reusable patterns for contribution to
`team-ai-directives`.
Replaces the former `team-init` skill. Drafts are written to the `adlc`
orphan branch of `team-ai-directives` (not project-local `.adlc/drafts/`).
## When to use
- **Brownfield projects**: Existing code without team-wide directives
- **Legacy modernization**: Extract reusable patterns before refactoring
- **Team onboarding**: Turn implicit conventions into explicit directives
### When NOT to use
- **Greenfield (session complete)**: Use `/team-levelup` to extract from session
- **Mining git history**: Use `/change-init` for past decisions
## Process
### Phase 0: Environment Setup
Run: `scripts/bash/team-init.sh --setup`
Resolves `REPO_ROOT`, `TEAM_AI_DIRECTIVES`, `NEXT_CDR`, `ADLC_BRANCH`.
### Phase 1: Sub-System Detection
Analyze codebase for distinct sub-systems (same detection as former team-init):
- `src/auth/` → Authentication
- `services/payment/` → Payment
- etc.
### Phase 2: Load Team Directives
Read existing `team-ai-directives` main branch for comparison (avoid duplicates).
### Phase 3: Multi-Agent Analysis
1. **Discovery Agent**: Scan each sub-system for raw patterns
2. **Pattern Agent**: Classify and score patterns for reusability
3. **Synthesis Agent**: Cross-sub-system analysis and CDR generation
### Phase 4: Write CDRs
Write to `adlc` branch `drafts/cdr/` using the shared CDR draft template at `skills/team/templates/cdr-draft-template.md`.
### Phase 5: Handoff
Suggest running `/team-levelup` to review and publish the discovered CDRs.
## Verification
- [ ] CDRs written to `adlc` branch `drafts/cdr/`
- [ ] No CDRs in `.adlc/drafts/` (old location)
- [ ] `cdr.md` index regenerated in `adlc` branch