@orchestrator-claude/definitions 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/agents/api-extractor.md +687 -0
  2. package/agents/business-rule-miner.md +754 -0
  3. package/agents/code-archaeologist.md +720 -0
  4. package/agents/docs-guardian.md +524 -0
  5. package/agents/implementer.md +512 -0
  6. package/agents/legacy-discoverer.md +583 -0
  7. package/agents/legacy-synthesizer.md +1101 -0
  8. package/agents/orchestrator.md +165 -0
  9. package/agents/planner.md +365 -0
  10. package/agents/researcher.md +447 -0
  11. package/agents/reviewer.md +514 -0
  12. package/agents/schema-extractor.md +781 -0
  13. package/agents/specifier.md +360 -0
  14. package/agents/task-generator.md +390 -0
  15. package/bin/orch-defs.js +2 -0
  16. package/dist/cli.d.ts +3 -0
  17. package/dist/cli.d.ts.map +1 -0
  18. package/dist/cli.js +172 -0
  19. package/dist/cli.js.map +1 -0
  20. package/dist/commands/DiffCommand.d.ts +13 -0
  21. package/dist/commands/DiffCommand.d.ts.map +1 -0
  22. package/dist/commands/DiffCommand.js +74 -0
  23. package/dist/commands/DiffCommand.js.map +1 -0
  24. package/dist/commands/SeedCommand.d.ts +19 -0
  25. package/dist/commands/SeedCommand.d.ts.map +1 -0
  26. package/dist/commands/SeedCommand.js +56 -0
  27. package/dist/commands/SeedCommand.js.map +1 -0
  28. package/dist/http/ApiClient.d.ts +50 -0
  29. package/dist/http/ApiClient.d.ts.map +1 -0
  30. package/dist/http/ApiClient.js +58 -0
  31. package/dist/http/ApiClient.js.map +1 -0
  32. package/dist/index.d.ts +12 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +11 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/manifest/ManifestLoader.d.ts +34 -0
  37. package/dist/manifest/ManifestLoader.d.ts.map +1 -0
  38. package/dist/manifest/ManifestLoader.js +110 -0
  39. package/dist/manifest/ManifestLoader.js.map +1 -0
  40. package/dist/manifest/types.d.ts +59 -0
  41. package/dist/manifest/types.d.ts.map +1 -0
  42. package/dist/manifest/types.js +5 -0
  43. package/dist/manifest/types.js.map +1 -0
  44. package/dist/scripts/generate-manifest.d.ts +10 -0
  45. package/dist/scripts/generate-manifest.d.ts.map +1 -0
  46. package/dist/scripts/generate-manifest.js +114 -0
  47. package/dist/scripts/generate-manifest.js.map +1 -0
  48. package/hooks/post-agent-artifact-relay.sh +157 -0
  49. package/hooks/post-artifact-generate.sh +39 -0
  50. package/hooks/post-implement-validate.sh +139 -0
  51. package/hooks/post-phase-checkpoint.sh +322 -0
  52. package/hooks/pre-agent-invoke.sh +34 -0
  53. package/hooks/pre-phase-advance.sh +40 -0
  54. package/hooks/track-agent-invocation.sh +241 -0
  55. package/kb/auth-strategies.md +742 -0
  56. package/kb/docs-constitution.md +310 -0
  57. package/kb/error-handling.md +555 -0
  58. package/kb/rest-conventions.md +458 -0
  59. package/kb/validation-patterns.md +589 -0
  60. package/manifest.json +314 -0
  61. package/package.json +65 -0
  62. package/skills/artifact-validator/SKILL.md +226 -0
  63. package/skills/docs-guardian/SKILL.md +230 -0
  64. package/skills/kb-lookup/SKILL.md +257 -0
  65. package/skills/phase-gate-evaluator/SKILL.md +274 -0
  66. package/skills/release/SKILL.md +239 -0
  67. package/skills/release/release.sh +491 -0
  68. package/skills/smoke-test/SKILL.md +195 -0
  69. package/skills/workflow-status/SKILL.md +322 -0
  70. package/workflows/bug-fix.json +74 -0
  71. package/workflows/feature-development.json +88 -0
  72. package/workflows/legacy-analysis.json +304 -0
  73. package/workflows/refactoring.json +74 -0
@@ -0,0 +1,157 @@
1
+ #!/bin/bash
2
+ # Post-Agent Artifact Relay Hook
3
+ # TD-039: Automatically relays artifacts from staging to MinIO after agent completion.
4
+ #
5
+ # Called as a PostToolUse hook on the Task matcher.
6
+ # Reads stdin JSON with structure:
7
+ # { "tool_name": "Task", "tool_input": { "subagent_type": "...", ... } }
8
+ #
9
+ # Flow:
10
+ # 1. Extract subagent_type from stdin
11
+ # 2. Skip non-artifact agents (orchestrator, reviewer, docs-guardian, etc.)
12
+ # 3. Get active workflow from API
13
+ # 4. Login to get JWT token
14
+ # 5. Call DELETE /api/v1/workflows/:id/pending-action to trigger relay
15
+ # 6. Log result
16
+
17
+ set -e
18
+
19
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20
+ PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
21
+ STATE_DIR="$PROJECT_ROOT/.orchestrator/.state"
22
+ LOG_FILE="$STATE_DIR/relay-hook.log"
23
+
24
+ # Ensure state directory exists
25
+ mkdir -p "$STATE_DIR"
26
+
27
+ # Log rotation: max 1MB
28
+ MAX_LOG_SIZE=1048576
29
+ if [ -f "$LOG_FILE" ]; then
30
+ LOG_SIZE=$(stat -f%z "$LOG_FILE" 2>/dev/null || stat -c%s "$LOG_FILE" 2>/dev/null || echo 0)
31
+ if [ "$LOG_SIZE" -gt "$MAX_LOG_SIZE" ]; then
32
+ [ -f "${LOG_FILE}.1" ] && mv "${LOG_FILE}.1" "${LOG_FILE}.2"
33
+ mv "$LOG_FILE" "${LOG_FILE}.1"
34
+ fi
35
+ fi
36
+
37
+ log() {
38
+ echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] $*" >> "$LOG_FILE"
39
+ }
40
+
41
+ # Configuration
42
+ API_URL="${ORCHESTRATOR_API_URL:-http://localhost:3001}"
43
+ AUTH_EMAIL="${ORCHESTRATOR_AUTH_EMAIL:-admin@orchestrator.local}"
44
+ AUTH_PASSWORD="${ORCHESTRATOR_AUTH_PASSWORD:-admin123}"
45
+
46
+ # Read stdin
47
+ STDIN_DATA=""
48
+ if [ ! -t 0 ]; then
49
+ STDIN_DATA=$(cat)
50
+ fi
51
+
52
+ if [ -z "$STDIN_DATA" ]; then
53
+ log "SKIP: No stdin data"
54
+ exit 0
55
+ fi
56
+
57
+ # Extract agent type from stdin JSON
58
+ AGENT_TYPE=$(echo "$STDIN_DATA" | node -e "
59
+ let data = '';
60
+ process.stdin.on('data', chunk => data += chunk);
61
+ process.stdin.on('end', () => {
62
+ try {
63
+ const json = JSON.parse(data);
64
+ const type = json.tool_input?.subagent_type
65
+ || json.subagent_type
66
+ || json.tool_input?.type
67
+ || '';
68
+ console.log(type);
69
+ } catch { console.log(''); }
70
+ });
71
+ " 2>/dev/null)
72
+
73
+ if [ -z "$AGENT_TYPE" ]; then
74
+ log "SKIP: Could not extract agent type"
75
+ exit 0
76
+ fi
77
+
78
+ # TD-039 Phase 3: Inverted whitelist → blacklist.
79
+ # All agents produce artifacts EXCEPT orchestrator (coordinator only).
80
+ case "$AGENT_TYPE" in
81
+ orchestrator)
82
+ log "SKIP: Agent '$AGENT_TYPE' is a coordinator, not an artifact producer"
83
+ exit 0
84
+ ;;
85
+ *)
86
+ log "RELAY: Agent '$AGENT_TYPE' completed, starting relay..."
87
+ ;;
88
+ esac
89
+
90
+ # Get active workflow status
91
+ STATUS_RESPONSE=$(curl -s -f "${API_URL}/api/v1/workflows/status" 2>/dev/null) || {
92
+ log "SKIP: Could not reach API at ${API_URL}/api/v1/workflows/status"
93
+ exit 0
94
+ }
95
+
96
+ WORKFLOW_ID=$(echo "$STATUS_RESPONSE" | node -e "
97
+ let data = '';
98
+ process.stdin.on('data', chunk => data += chunk);
99
+ process.stdin.on('end', () => {
100
+ try {
101
+ const json = JSON.parse(data);
102
+ // Support both { id } and { data: { id } } shapes
103
+ const id = json.data?.id || json.id || '';
104
+ console.log(id);
105
+ } catch { console.log(''); }
106
+ });
107
+ " 2>/dev/null)
108
+
109
+ if [ -z "$WORKFLOW_ID" ]; then
110
+ log "SKIP: No active workflow found"
111
+ exit 0
112
+ fi
113
+
114
+ log "RELAY: Active workflow=$WORKFLOW_ID"
115
+
116
+ # Login to get JWT token
117
+ LOGIN_PAYLOAD=$(node -e "console.log(JSON.stringify({email:'${AUTH_EMAIL}',password:'${AUTH_PASSWORD}'}))")
118
+ LOGIN_RESPONSE=$(curl -s -f -X POST "${API_URL}/api/v1/auth/login" \
119
+ -H "Content-Type: application/json" \
120
+ -d "$LOGIN_PAYLOAD" 2>/dev/null) || {
121
+ log "ERROR: Login failed"
122
+ exit 0
123
+ }
124
+
125
+ TOKEN=$(echo "$LOGIN_RESPONSE" | node -e "
126
+ let data = '';
127
+ process.stdin.on('data', chunk => data += chunk);
128
+ process.stdin.on('end', () => {
129
+ try {
130
+ const json = JSON.parse(data);
131
+ console.log(json.data?.accessToken || json.accessToken || json.token || '');
132
+ } catch { console.log(''); }
133
+ });
134
+ " 2>/dev/null)
135
+
136
+ if [ -z "$TOKEN" ]; then
137
+ log "ERROR: Could not extract token from login response"
138
+ exit 0
139
+ fi
140
+
141
+ # Call relay endpoint: DELETE /api/v1/workflows/:id/pending-action
142
+ RELAY_RESPONSE=$(curl -s -w "\n%{http_code}" -X DELETE \
143
+ "${API_URL}/api/v1/workflows/${WORKFLOW_ID}/pending-action" \
144
+ -H "Content-Type: application/json" \
145
+ -H "Authorization: Bearer ${TOKEN}" \
146
+ -d '{}' 2>/dev/null)
147
+
148
+ HTTP_CODE=$(echo "$RELAY_RESPONSE" | tail -1)
149
+ BODY=$(echo "$RELAY_RESPONSE" | sed '$d')
150
+
151
+ if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "204" ]; then
152
+ log "SUCCESS: Relay completed for workflow=$WORKFLOW_ID agent=$AGENT_TYPE http=$HTTP_CODE"
153
+ else
154
+ log "ERROR: Relay failed for workflow=$WORKFLOW_ID agent=$AGENT_TYPE http=$HTTP_CODE body=$BODY"
155
+ fi
156
+
157
+ exit 0
@@ -0,0 +1,39 @@
1
+ #!/bin/bash
2
+ # Post-Artifact Generate Hook
3
+ # Executado apos geracao de um artefato
4
+ # Valida artefato, atualiza indice, cria checkpoint
5
+
6
+ set -e
7
+
8
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9
+ PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
10
+
11
+ # Recebe evento via stdin ou argumento
12
+ if [ -n "$1" ]; then
13
+ EVENT="$1"
14
+ else
15
+ EVENT=$(cat)
16
+ fi
17
+
18
+ # Verifica se temos o handler compilado
19
+ HANDLER="$PROJECT_ROOT/dist/hook.js"
20
+
21
+ if [ ! -f "$HANDLER" ]; then
22
+ echo '{"success": false, "error": "Hook handler not compiled. Run npm run build first."}' >&2
23
+ exit 1
24
+ fi
25
+
26
+ # Executa o handler TypeScript
27
+ echo "$EVENT" | node "$HANDLER" post-artifact
28
+
29
+ EXIT_CODE=$?
30
+
31
+ # Log do resultado
32
+ if [ $EXIT_CODE -eq 0 ]; then
33
+ echo "[post-artifact-generate] Artifact processed successfully" >&2
34
+ else
35
+ echo "[post-artifact-generate] Warning: Artifact processing had issues" >&2
36
+ fi
37
+
38
+ # Este hook nao bloqueia, apenas avisa
39
+ exit 0
@@ -0,0 +1,139 @@
1
+ #!/bin/bash
2
+ # Post-Implement Validation Hook
3
+ # Validates orchestrator-index.json consistency after IMPLEMENT phase
4
+ # Detects possible delegation bypass where code was written directly
5
+ # instead of through the implementer subagent
6
+ #
7
+ # Exit codes:
8
+ # 0 = Validation passed or skipped (jq not available)
9
+ # 1 = Warning detected (possible delegation bypass)
10
+ #
11
+ # Author: Orchestrator System
12
+ # Version: 1.0.0
13
+
14
+ set -e
15
+
16
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17
+ PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
18
+ INDEX_FILE="$PROJECT_ROOT/.orchestrator/orchestrator-index.json"
19
+
20
+ # Colors for output
21
+ RED='\033[0;31m'
22
+ YELLOW='\033[1;33m'
23
+ GREEN='\033[0;32m'
24
+ CYAN='\033[0;36m'
25
+ NC='\033[0m' # No Color
26
+
27
+ log_info() {
28
+ echo -e "${GREEN}[post-implement-validate]${NC} $1" >&2
29
+ }
30
+
31
+ log_warn() {
32
+ echo -e "${YELLOW}[post-implement-validate] WARNING:${NC} $1" >&2
33
+ }
34
+
35
+ log_error() {
36
+ echo -e "${RED}[post-implement-validate] ERROR:${NC} $1" >&2
37
+ }
38
+
39
+ log_debug() {
40
+ if [ "${DEBUG:-false}" = "true" ]; then
41
+ echo -e "${CYAN}[post-implement-validate] DEBUG:${NC} $1" >&2
42
+ fi
43
+ }
44
+
45
+ # Check if jq is available
46
+ if ! command -v jq &> /dev/null; then
47
+ log_warn "jq not found, skipping validation (install jq for full validation)"
48
+ exit 0
49
+ fi
50
+
51
+ # Check if orchestrator-index.json exists
52
+ if [ ! -f "$INDEX_FILE" ]; then
53
+ log_debug "orchestrator-index.json not found at $INDEX_FILE, skipping validation"
54
+ exit 0
55
+ fi
56
+
57
+ # Read statistics from orchestrator-index.json
58
+ TASKS_COMPLETED=$(jq -r '.statistics.tasksCompleted // 0' "$INDEX_FILE" 2>/dev/null || echo "0")
59
+ TASKS_PENDING=$(jq -r '.statistics.tasksPending // 0' "$INDEX_FILE" 2>/dev/null || echo "0")
60
+ CURRENT_PHASE=$(jq -r '.activeWorkflow.currentPhase // "unknown"' "$INDEX_FILE" 2>/dev/null || echo "unknown")
61
+ STATUS=$(jq -r '.activeWorkflow.status // "unknown"' "$INDEX_FILE" 2>/dev/null || echo "unknown")
62
+
63
+ # Count TypeScript files in src/ (if directory exists)
64
+ TS_FILES_COUNT=0
65
+ if [ -d "$PROJECT_ROOT/src" ]; then
66
+ TS_FILES_COUNT=$(find "$PROJECT_ROOT/src" -name "*.ts" 2>/dev/null | wc -l | tr -d ' ' || echo "0")
67
+ fi
68
+
69
+ # Count test files
70
+ TEST_FILES_COUNT=0
71
+ if [ -d "$PROJECT_ROOT/tests" ]; then
72
+ TEST_FILES_COUNT=$(find "$PROJECT_ROOT/tests" -name "*.spec.ts" -o -name "*.test.ts" 2>/dev/null | wc -l | tr -d ' ' || echo "0")
73
+ fi
74
+
75
+ log_info "Validating IMPLEMENT phase consistency..."
76
+ log_info " Current Phase: $CURRENT_PHASE"
77
+ log_info " Status: $STATUS"
78
+ log_info " Tasks Completed: $TASKS_COMPLETED"
79
+ log_info " Tasks Pending: $TASKS_PENDING"
80
+ log_info " TypeScript Files: $TS_FILES_COUNT"
81
+ log_info " Test Files: $TEST_FILES_COUNT"
82
+
83
+ # Detection logic
84
+ WARNINGS=0
85
+ MESSAGES=()
86
+
87
+ # Check 1: Few tasks completed but many pending (primary indicator)
88
+ if [ "$TASKS_COMPLETED" -le 1 ] && [ "$TASKS_PENDING" -gt 5 ]; then
89
+ MESSAGES+=("Only $TASKS_COMPLETED task(s) completed but $TASKS_PENDING pending.")
90
+ MESSAGES+=("This suggests the implementer agent may not have been properly invoked.")
91
+ WARNINGS=$((WARNINGS + 1))
92
+ fi
93
+
94
+ # Check 2: Many source files but few tasks completed
95
+ if [ "$TS_FILES_COUNT" -gt 10 ] && [ "$TASKS_COMPLETED" -lt 5 ]; then
96
+ MESSAGES+=("Found $TS_FILES_COUNT TypeScript files but only $TASKS_COMPLETED tasks completed.")
97
+ MESSAGES+=("Code may have been generated without proper task tracking.")
98
+ WARNINGS=$((WARNINGS + 1))
99
+ fi
100
+
101
+ # Check 3: Status still "ready_for_implementation" after code exists
102
+ if [ "$STATUS" = "ready_for_implementation" ] && [ "$TS_FILES_COUNT" -gt 5 ]; then
103
+ MESSAGES+=("Status is still 'ready_for_implementation' but $TS_FILES_COUNT code files exist.")
104
+ MESSAGES+=("Workflow state may not have been updated correctly.")
105
+ WARNINGS=$((WARNINGS + 1))
106
+ fi
107
+
108
+ # Check 4: Source files exist but no test files (TDD violation)
109
+ if [ "$TS_FILES_COUNT" -gt 5 ] && [ "$TEST_FILES_COUNT" -eq 0 ]; then
110
+ MESSAGES+=("Found $TS_FILES_COUNT source files but no test files.")
111
+ MESSAGES+=("TDD methodology may not have been followed.")
112
+ WARNINGS=$((WARNINGS + 1))
113
+ fi
114
+
115
+ # Summary
116
+ if [ "$WARNINGS" -gt 0 ]; then
117
+ echo "" >&2
118
+ log_warn "============================================"
119
+ log_warn "Detected $WARNINGS potential issue(s):"
120
+ log_warn "============================================"
121
+ for msg in "${MESSAGES[@]}"; do
122
+ log_warn " - $msg"
123
+ done
124
+ echo "" >&2
125
+ log_warn "Possible causes:"
126
+ log_warn " 1. Implementer agent was not invoked via Task tool"
127
+ log_warn " 2. Code was written directly instead of through delegation"
128
+ log_warn " 3. orchestrator-index.json was not updated during implementation"
129
+ echo "" >&2
130
+ log_warn "Recommended actions:"
131
+ log_warn " 1. Review CLAUDE.md section: MANDATORY: IMPLEMENT Phase Delegation"
132
+ log_warn " 2. Ensure Task tool is used with subagent_type='implementer'"
133
+ log_warn " 3. Update orchestrator-index.json with correct task progress"
134
+ echo "" >&2
135
+ exit 1
136
+ else
137
+ log_info "Validation passed. No issues detected."
138
+ exit 0
139
+ fi
@@ -0,0 +1,322 @@
1
+ #!/bin/bash
2
+ # Post-Phase Checkpoint Hook
3
+ # Automatically creates git checkpoints when workflow phases complete
4
+ #
5
+ # Triggered by: PostToolUse on Task tool (after agent completes)
6
+ # Behavior: Detects phase transitions and creates git commits
7
+ #
8
+ # Requirements:
9
+ # - jq (for JSON parsing)
10
+ # - git (for checkpoint commits)
11
+ #
12
+ # Debug mode: ORCH_CHECKPOINT_DEBUG=1 to enable verbose logging
13
+
14
+ set -e
15
+
16
+ # Configuration
17
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
18
+ PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
19
+ INDEX_FILE="$PROJECT_ROOT/.orchestrator/orchestrator-index.json"
20
+ STATE_DIR="$PROJECT_ROOT/.orchestrator/.state"
21
+ LOG_FILE="$STATE_DIR/checkpoint-hook.log"
22
+ LAST_PHASE_FILE="$STATE_DIR/last-checkpointed-phase"
23
+
24
+ # Ensure state directory exists
25
+ mkdir -p "$STATE_DIR"
26
+
27
+ # Debug mode
28
+ DEBUG="${ORCH_CHECKPOINT_DEBUG:-0}"
29
+
30
+ # Logging functions
31
+ log_debug() {
32
+ if [ "$DEBUG" = "1" ]; then
33
+ echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] [DEBUG] $1" >> "$LOG_FILE"
34
+ echo "[DEBUG] $1" >&2
35
+ fi
36
+ }
37
+
38
+ log_info() {
39
+ echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] [INFO] $1" >> "$LOG_FILE"
40
+ if [ "$DEBUG" = "1" ]; then
41
+ echo "[INFO] $1" >&2
42
+ fi
43
+ }
44
+
45
+ log_warn() {
46
+ echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] [WARN] $1" >> "$LOG_FILE"
47
+ echo "[WARN] $1" >&2
48
+ }
49
+
50
+ log_error() {
51
+ echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] [ERROR] $1" >> "$LOG_FILE"
52
+ echo "[ERROR] $1" >&2
53
+ }
54
+
55
+ # Check prerequisites
56
+ check_prerequisites() {
57
+ # Check jq
58
+ if ! command -v jq &> /dev/null; then
59
+ log_error "jq is not installed. Checkpoints disabled."
60
+ return 1
61
+ fi
62
+
63
+ # Check git
64
+ if ! command -v git &> /dev/null; then
65
+ log_error "git is not installed. Checkpoints disabled."
66
+ return 1
67
+ fi
68
+
69
+ # Check if we're in a git repo
70
+ if ! git -C "$PROJECT_ROOT" rev-parse --is-inside-work-tree &> /dev/null; then
71
+ log_warn "Not a git repository. Checkpoints disabled."
72
+ return 1
73
+ fi
74
+
75
+ # Check index file
76
+ if [ ! -f "$INDEX_FILE" ]; then
77
+ log_debug "No orchestrator-index.json found. Skipping."
78
+ return 1
79
+ fi
80
+
81
+ return 0
82
+ }
83
+
84
+ # Get current phase from orchestrator-index.json
85
+ get_current_phase() {
86
+ local phase
87
+ phase=$(jq -r '.activeWorkflow.currentPhase // empty' "$INDEX_FILE" 2>/dev/null)
88
+ echo "$phase"
89
+ }
90
+
91
+ # Get workflow ID
92
+ get_workflow_id() {
93
+ local wf_id
94
+ wf_id=$(jq -r '.activeWorkflow.id // empty' "$INDEX_FILE" 2>/dev/null)
95
+ echo "$wf_id"
96
+ }
97
+
98
+ # Get workflow status
99
+ get_workflow_status() {
100
+ local status
101
+ status=$(jq -r '.activeWorkflow.status // empty' "$INDEX_FILE" 2>/dev/null)
102
+ echo "$status"
103
+ }
104
+
105
+ # Get last checkpointed phase from checkpoints array (one with commitHash)
106
+ get_last_checkpointed_phase() {
107
+ local phase
108
+ # Find the most recent checkpoint that has a commitHash (indicating it was git-committed)
109
+ phase=$(jq -r '
110
+ [.checkpoints[] | select(.commitHash != null and .commitHash != "")]
111
+ | sort_by(.createdAt)
112
+ | last
113
+ | .phase // empty
114
+ ' "$INDEX_FILE" 2>/dev/null)
115
+ echo "$phase"
116
+ }
117
+
118
+ # Determine completed phase based on current phase
119
+ # When we see currentPhase = PLAN, it means SPECIFY just completed
120
+ get_completed_phase() {
121
+ local current_phase="$1"
122
+ local workflow_status="$2"
123
+
124
+ case "$workflow_status" in
125
+ "completed")
126
+ echo "IMPLEMENT"
127
+ return
128
+ ;;
129
+ esac
130
+
131
+ case "$current_phase" in
132
+ "PLAN")
133
+ echo "SPECIFY"
134
+ ;;
135
+ "TASKS")
136
+ echo "PLAN"
137
+ ;;
138
+ "IMPLEMENT")
139
+ echo "TASKS"
140
+ ;;
141
+ *)
142
+ echo ""
143
+ ;;
144
+ esac
145
+ }
146
+
147
+ # Create git checkpoint
148
+ create_git_checkpoint() {
149
+ local phase="$1"
150
+ local commit_hash=""
151
+
152
+ log_info "Creating git checkpoint for phase: $phase"
153
+
154
+ # Stage all changes
155
+ git -C "$PROJECT_ROOT" add -A 2>/dev/null || {
156
+ log_warn "Failed to stage changes"
157
+ return 1
158
+ }
159
+
160
+ # Check if there are staged changes
161
+ if git -C "$PROJECT_ROOT" diff --cached --quiet 2>/dev/null; then
162
+ log_info "No changes to commit for phase: $phase"
163
+ echo ""
164
+ return 0
165
+ fi
166
+
167
+ # Create commit with standard message
168
+ local commit_msg="[orchestrator] ${phase}: Auto-checkpoint after ${phase} phase"
169
+
170
+ if git -C "$PROJECT_ROOT" commit -m "$commit_msg" --no-verify 2>/dev/null; then
171
+ commit_hash=$(git -C "$PROJECT_ROOT" rev-parse HEAD 2>/dev/null)
172
+ log_info "Checkpoint commit created: $commit_hash"
173
+ echo "$commit_hash"
174
+ else
175
+ log_warn "Failed to create commit"
176
+ echo ""
177
+ return 1
178
+ fi
179
+ }
180
+
181
+ # Update orchestrator-index.json with checkpoint
182
+ update_orchestrator_index() {
183
+ local workflow_id="$1"
184
+ local phase="$2"
185
+ local commit_hash="$3"
186
+ local created_at
187
+
188
+ created_at=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
189
+ local checkpoint_id="cp_${workflow_id}_$(date +%s)"
190
+ local description="Auto-checkpoint after ${phase} phase"
191
+
192
+ log_debug "Updating index with checkpoint: $checkpoint_id"
193
+
194
+ # Create temp file for atomic update
195
+ local temp_file="${INDEX_FILE}.tmp.$$"
196
+
197
+ # Use jq to add checkpoint and update statistics
198
+ if jq --arg id "$checkpoint_id" \
199
+ --arg wfId "$workflow_id" \
200
+ --arg phase "$phase" \
201
+ --arg hash "$commit_hash" \
202
+ --arg date "$created_at" \
203
+ --arg desc "$description" '
204
+ # Add checkpoint to array
205
+ .checkpoints += [{
206
+ id: $id,
207
+ workflowId: $wfId,
208
+ phase: $phase,
209
+ commitHash: $hash,
210
+ createdAt: $date,
211
+ description: $desc
212
+ }] |
213
+ # Update statistics
214
+ .statistics.totalCheckpoints = (.checkpoints | length) |
215
+ .statistics.lastActivity = $date
216
+ ' "$INDEX_FILE" > "$temp_file" 2>/dev/null; then
217
+
218
+ # Validate JSON
219
+ if jq empty "$temp_file" 2>/dev/null; then
220
+ mv "$temp_file" "$INDEX_FILE"
221
+ log_info "Index updated with checkpoint: $checkpoint_id"
222
+ return 0
223
+ else
224
+ log_error "Generated invalid JSON"
225
+ rm -f "$temp_file"
226
+ return 1
227
+ fi
228
+ else
229
+ log_error "Failed to update index with jq"
230
+ rm -f "$temp_file"
231
+ return 1
232
+ fi
233
+ }
234
+
235
+ # Save last checkpointed phase
236
+ save_last_phase() {
237
+ local phase="$1"
238
+ echo "$phase" > "$LAST_PHASE_FILE"
239
+ }
240
+
241
+ # Get saved last phase (from state file, not index)
242
+ get_saved_last_phase() {
243
+ if [ -f "$LAST_PHASE_FILE" ]; then
244
+ cat "$LAST_PHASE_FILE"
245
+ else
246
+ echo ""
247
+ fi
248
+ }
249
+
250
+ # Main execution
251
+ main() {
252
+ log_debug "Hook triggered"
253
+
254
+ # Check prerequisites
255
+ if ! check_prerequisites; then
256
+ log_debug "Prerequisites not met, exiting"
257
+ exit 0 # Don't fail the hook
258
+ fi
259
+
260
+ # Get current state
261
+ local current_phase
262
+ current_phase=$(get_current_phase)
263
+
264
+ if [ -z "$current_phase" ]; then
265
+ log_debug "No active workflow phase found"
266
+ exit 0
267
+ fi
268
+
269
+ local workflow_id
270
+ workflow_id=$(get_workflow_id)
271
+
272
+ local workflow_status
273
+ workflow_status=$(get_workflow_status)
274
+
275
+ log_debug "Current phase: $current_phase, Status: $workflow_status, Workflow: $workflow_id"
276
+
277
+ # Determine which phase just completed
278
+ local completed_phase
279
+ completed_phase=$(get_completed_phase "$current_phase" "$workflow_status")
280
+
281
+ if [ -z "$completed_phase" ]; then
282
+ log_debug "No completed phase detected from current phase: $current_phase"
283
+ exit 0
284
+ fi
285
+
286
+ # Check if we already checkpointed this phase
287
+ local last_checkpointed
288
+ last_checkpointed=$(get_last_checkpointed_phase)
289
+
290
+ local saved_last_phase
291
+ saved_last_phase=$(get_saved_last_phase)
292
+
293
+ log_debug "Completed phase: $completed_phase, Last checkpointed: $last_checkpointed, Saved: $saved_last_phase"
294
+
295
+ # Skip if already checkpointed
296
+ if [ "$completed_phase" = "$last_checkpointed" ] || [ "$completed_phase" = "$saved_last_phase" ]; then
297
+ log_debug "Phase $completed_phase already checkpointed, skipping"
298
+ exit 0
299
+ fi
300
+
301
+ # Create checkpoint
302
+ local commit_hash
303
+ commit_hash=$(create_git_checkpoint "$completed_phase")
304
+
305
+ # Update index (even if no commit, to track the checkpoint)
306
+ if [ -n "$commit_hash" ]; then
307
+ if update_orchestrator_index "$workflow_id" "$completed_phase" "$commit_hash"; then
308
+ save_last_phase "$completed_phase"
309
+ log_info "Checkpoint complete for phase: $completed_phase (commit: $commit_hash)"
310
+ else
311
+ log_error "Failed to update index for phase: $completed_phase"
312
+ fi
313
+ else
314
+ log_info "No changes to checkpoint for phase: $completed_phase"
315
+ save_last_phase "$completed_phase"
316
+ fi
317
+
318
+ exit 0
319
+ }
320
+
321
+ # Run main function
322
+ main "$@"
@@ -0,0 +1,34 @@
1
+ #!/bin/bash
2
+ # Pre-Agent Invoke Hook
3
+ # Executado antes de invocar um subagent
4
+ # Valida contexto e injeta informacoes da KB
5
+
6
+ set -e
7
+
8
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9
+ PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
10
+
11
+ # Recebe evento via stdin ou argumento
12
+ if [ -n "$1" ]; then
13
+ EVENT="$1"
14
+ else
15
+ EVENT=$(cat)
16
+ fi
17
+
18
+ # Verifica se temos o handler compilado
19
+ HANDLER="$PROJECT_ROOT/dist/hook.js"
20
+
21
+ if [ ! -f "$HANDLER" ]; then
22
+ echo '{"success": false, "error": "Hook handler not compiled. Run npm run build first."}' >&2
23
+ exit 1
24
+ fi
25
+
26
+ # Executa o handler TypeScript
27
+ echo "$EVENT" | node "$HANDLER" pre-agent-invoke
28
+
29
+ EXIT_CODE=$?
30
+
31
+ # Exit code determina se agente pode prosseguir
32
+ # 0 = permitido
33
+ # 1 = bloqueado
34
+ exit $EXIT_CODE