claude-flow-novice 2.14.6 → 2.14.7
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.
- package/.claude/commands/cfn/run-tests.md +119 -0
- package/.claude/hooks/cfn-post-edit.config.json +11 -4
- package/.claude/skills/cfn-agent-selector/SKILL.md +3 -2
- package/.claude/skills/cfn-loop-orchestration/orchestrate.sh +1 -1
- package/.claude/skills/cfn-product-owner-decision/execute-decision.sh +141 -114
- package/.claude/skills/cfn-redis-coordination/report-completion.sh +86 -0
- package/.claude/skills/cfn-redis-coordination/store-context.sh +34 -0
- package/.claude/skills/pre-edit-backup/backup.sh +130 -0
- package/.claude/skills/pre-edit-backup/cleanup.sh +155 -0
- package/.claude/skills/pre-edit-backup/restore.sh +128 -0
- package/.claude/skills/pre-edit-backup/revert-file.sh +168 -0
- package/claude-assets/agents/README-AGENT_LIFECYCLE.md +522 -0
- package/claude-assets/agents/cfn-dev-team/coordinators/cfn-v3-coordinator.md +6 -3
- package/claude-assets/agents/cfn-dev-team/product-owners/product-owner.md +1 -1
- package/claude-assets/agents/cfn-dev-team/test-agent.md +141 -0
- package/claude-assets/agents/cfn-dev-team/utility/agent-builder.md +35 -0
- package/claude-assets/commands/cfn/run-tests.md +119 -0
- package/claude-assets/hooks/cfn-post-edit.config.json +11 -4
- package/claude-assets/skills/agent-name-validation/README.md +28 -0
- package/claude-assets/skills/agent-name-validation/SKILL.md +168 -0
- package/claude-assets/skills/agent-name-validation/validate-agent-names.sh +47 -0
- package/claude-assets/skills/cfn-agent-selector/SKILL.md +3 -2
- package/claude-assets/skills/cfn-loop-orchestration/orchestrate.sh +1 -1
- package/claude-assets/skills/cfn-product-owner-decision/execute-decision.sh +141 -114
- package/claude-assets/skills/cfn-redis-coordination/report-completion.sh +86 -0
- package/claude-assets/skills/cfn-redis-coordination/store-context.sh +34 -0
- package/claude-assets/skills/cfn-task-classifier/SKILL.md +1 -1
- package/claude-assets/skills/cfn-test-runner/SKILL.md +288 -0
- package/claude-assets/skills/cfn-test-runner/detect-regressions.sh +55 -0
- package/claude-assets/skills/cfn-test-runner/init-benchmark-db.sh +48 -0
- package/claude-assets/skills/cfn-test-runner/run-all-tests.sh +222 -0
- package/claude-assets/skills/cfn-test-runner/store-benchmarks.sh +55 -0
- package/claude-assets/skills/cfn-test-runner/validate-redis-keys.sh +143 -0
- package/claude-assets/skills/hook-pipeline/bash-dependency-checker.sh +89 -0
- package/claude-assets/skills/hook-pipeline/bash-pipe-safety.sh +69 -0
- package/claude-assets/skills/hook-pipeline/enforce-lf.sh +36 -0
- package/claude-assets/skills/hook-pipeline/js-promise-safety.sh +110 -0
- package/claude-assets/skills/hook-pipeline/python-async-safety.py +124 -0
- package/claude-assets/skills/hook-pipeline/python-import-checker.py +114 -0
- package/claude-assets/skills/hook-pipeline/python-subprocess-safety.py +77 -0
- package/claude-assets/skills/hook-pipeline/rust-command-safety.sh +38 -0
- package/claude-assets/skills/hook-pipeline/rust-dependency-checker.sh +50 -0
- package/claude-assets/skills/hook-pipeline/rust-future-safety.sh +50 -0
- package/dist/cli/agent-executor.js +1 -1
- package/dist/cli/agent-executor.js.map +1 -1
- package/dist/cli/agent-prompt-builder.js +40 -30
- package/dist/cli/agent-prompt-builder.js.map +1 -1
- package/dist/cli/config-manager.js +109 -91
- package/dist/cli/config-manager.js.map +1 -1
- package/package.json +2 -1
- package/scripts/init-project.js +4 -1
- package/claude-assets/agents/cfn-dev-team/developers/dev-backend-api.md +0 -147
- package/claude-assets/agents/cfn-dev-team/developers/frontend/spec-mobile-react-native.md +0 -199
- package/claude-assets/agents/cfn-dev-team/documentation/docs-api-openapi.md +0 -98
- package/claude-assets/agents/cfn-dev-team/product-owners/product-owner-agent.md +0 -155
- package/claude-assets/agents/cfn-dev-team/reviewers/quality/analyze-code-quality.md +0 -141
- /package/claude-assets/agents/cfn-dev-team/developers/{backend-dev.md → backend-developer.md} +0 -0
- /package/claude-assets/agents/cfn-dev-team/documentation/{api-docs.md → api-documentation.md} +0 -0
- /package/claude-assets/agents/cfn-dev-team/documentation/{specification.md → specification-agent.md} +0 -0
- /package/claude-assets/agents/cfn-dev-team/reviewers/quality/{code-analyzer.md → code-quality-validator.md} +0 -0
- /package/claude-assets/agents/cfn-dev-team/testers/e2e/{playwright-agent.md → playwright-tester.md} +0 -0
- /package/claude-assets/agents/cfn-dev-team/testers/unit/{tdd-london-swarm.md → tdd-london-unit-swarm.md} +0 -0
- /package/claude-assets/agents/cfn-dev-team/testers/validation/{production-validator.md → validation-production-validator.md} +0 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
import ast
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
class SubprocessSafetyVisitor(ast.NodeVisitor):
|
|
6
|
+
def __init__(self):
|
|
7
|
+
self.unsafe_calls = []
|
|
8
|
+
self.allowed_stderr_values = {'Attribute': ['PIPE', 'DEVNULL', 'STDOUT'],
|
|
9
|
+
'Name': ['subprocess.PIPE', 'subprocess.DEVNULL', 'subprocess.STDOUT']}
|
|
10
|
+
|
|
11
|
+
def is_safe_stderr(self, node):
|
|
12
|
+
if isinstance(node, ast.Attribute):
|
|
13
|
+
return node.attr in self.allowed_stderr_values['Attribute']
|
|
14
|
+
if isinstance(node, ast.Name):
|
|
15
|
+
return node.id in self.allowed_stderr_values['Name']
|
|
16
|
+
return False
|
|
17
|
+
|
|
18
|
+
def visit_Call(self, node):
|
|
19
|
+
if isinstance(node.func, ast.Attribute):
|
|
20
|
+
func_name = node.func.attr
|
|
21
|
+
if func_name in ['run', 'Popen', 'check_output']:
|
|
22
|
+
# Check if from subprocess module
|
|
23
|
+
module_name = None
|
|
24
|
+
if isinstance(node.func.value, ast.Name):
|
|
25
|
+
module_name = node.func.value.id
|
|
26
|
+
|
|
27
|
+
if module_name == 'subprocess':
|
|
28
|
+
# Strategies for safe subprocess calls
|
|
29
|
+
stderr_found = False
|
|
30
|
+
capture_output_found = False
|
|
31
|
+
|
|
32
|
+
for kw in node.keywords:
|
|
33
|
+
if kw.arg == 'stderr' and self.is_safe_stderr(kw.value):
|
|
34
|
+
stderr_found = True
|
|
35
|
+
if kw.arg == 'capture_output' and isinstance(kw.value, ast.NameConstant) and kw.value.value is True:
|
|
36
|
+
capture_output_found = True
|
|
37
|
+
|
|
38
|
+
if not (stderr_found or capture_output_found):
|
|
39
|
+
self.unsafe_calls.append(
|
|
40
|
+
f"Line {node.lineno}: Subprocess call '{func_name}' without stderr parameter or capture_output"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
self.generic_visit(node)
|
|
44
|
+
|
|
45
|
+
def validate_subprocess_safety(file_path):
|
|
46
|
+
try:
|
|
47
|
+
with open(file_path, 'r') as f:
|
|
48
|
+
tree = ast.parse(f.read(), filename=file_path)
|
|
49
|
+
except SyntaxError as e:
|
|
50
|
+
print(f"Syntax error in {file_path}: {e}")
|
|
51
|
+
return 1
|
|
52
|
+
except FileNotFoundError:
|
|
53
|
+
print(f"File not found: {file_path}")
|
|
54
|
+
return 1
|
|
55
|
+
|
|
56
|
+
visitor = SubprocessSafetyVisitor()
|
|
57
|
+
visitor.visit(tree)
|
|
58
|
+
|
|
59
|
+
if visitor.unsafe_calls:
|
|
60
|
+
print("Subprocess safety warnings:")
|
|
61
|
+
for call in visitor.unsafe_calls:
|
|
62
|
+
print(call)
|
|
63
|
+
return 2
|
|
64
|
+
|
|
65
|
+
return 0
|
|
66
|
+
|
|
67
|
+
def main():
|
|
68
|
+
if len(sys.argv) < 2:
|
|
69
|
+
print("Usage: python3 python-subprocess-safety.py <python_file>")
|
|
70
|
+
return 1
|
|
71
|
+
|
|
72
|
+
file_path = sys.argv[1]
|
|
73
|
+
result = validate_subprocess_safety(file_path)
|
|
74
|
+
sys.exit(result)
|
|
75
|
+
|
|
76
|
+
if __name__ == '__main__':
|
|
77
|
+
main()
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# Rust Command Safety Validator
|
|
3
|
+
set -o pipefail
|
|
4
|
+
|
|
5
|
+
# Exit codes:
|
|
6
|
+
# 0 - No issues found
|
|
7
|
+
# 1 - File not found or cannot be read
|
|
8
|
+
# 2 - Command safety warnings found
|
|
9
|
+
|
|
10
|
+
if [ $# -eq 0 ]; then
|
|
11
|
+
echo "Usage: $0 <rust_file>"
|
|
12
|
+
exit 1
|
|
13
|
+
fi
|
|
14
|
+
|
|
15
|
+
file_path="$1"
|
|
16
|
+
|
|
17
|
+
if [ ! -f "$file_path" ]; then
|
|
18
|
+
echo "File not found: $file_path"
|
|
19
|
+
exit 1
|
|
20
|
+
fi
|
|
21
|
+
|
|
22
|
+
# Use grep to find Command::new() calls and check for stderr in next 5 lines
|
|
23
|
+
unsafe_commands=$(grep -n "Command::new(" "$file_path" | while read -r line; do
|
|
24
|
+
line_num=$(echo "$line" | cut -d':' -f1)
|
|
25
|
+
next_lines=$(tail -n +"$line_num" "$file_path" | head -n 5)
|
|
26
|
+
|
|
27
|
+
if ! echo "$next_lines" | grep -q "\.stderr("; then
|
|
28
|
+
echo "Line $line_num: Command::new() without stderr() configuration in next 5 lines"
|
|
29
|
+
fi
|
|
30
|
+
done)
|
|
31
|
+
|
|
32
|
+
if [ -n "$unsafe_commands" ]; then
|
|
33
|
+
echo "Command safety warnings:"
|
|
34
|
+
echo "$unsafe_commands"
|
|
35
|
+
exit 2
|
|
36
|
+
fi
|
|
37
|
+
|
|
38
|
+
exit 0
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# Rust Dependency Checker
|
|
3
|
+
set -o pipefail
|
|
4
|
+
|
|
5
|
+
# Exit codes:
|
|
6
|
+
# 0 - No issues found
|
|
7
|
+
# 1 - Cargo.toml not found or cannot be read
|
|
8
|
+
# 2 - Dependency warnings found
|
|
9
|
+
|
|
10
|
+
if [ $# -eq 0 ]; then
|
|
11
|
+
echo "Usage: $0 <rust_file>"
|
|
12
|
+
exit 1
|
|
13
|
+
fi
|
|
14
|
+
|
|
15
|
+
file_path="$1"
|
|
16
|
+
project_dir=$(dirname "$file_path")
|
|
17
|
+
cargo_toml="${project_dir}/Cargo.toml"
|
|
18
|
+
|
|
19
|
+
if [ ! -f "$cargo_toml" ]; then
|
|
20
|
+
echo "Cargo.toml not found in project directory"
|
|
21
|
+
exit 1
|
|
22
|
+
fi
|
|
23
|
+
|
|
24
|
+
# Skip list for built-in/standard libraries
|
|
25
|
+
skip_modules="std|core|alloc|proc_macro"
|
|
26
|
+
|
|
27
|
+
# Find local crate dependencies in the file
|
|
28
|
+
local_deps=$(grep -o "use crate::[a-zA-Z_][a-zA-Z0-9_:]*" "$file_path" |
|
|
29
|
+
sed 's/use crate:://g' |
|
|
30
|
+
cut -d':' -f1 |
|
|
31
|
+
sort -u |
|
|
32
|
+
grep -vE "^(${skip_modules})$")
|
|
33
|
+
|
|
34
|
+
# Check dependencies in Cargo.toml
|
|
35
|
+
if [ -n "$local_deps" ]; then
|
|
36
|
+
warnings=$(echo "$local_deps" | while read -r dep; do
|
|
37
|
+
if ! grep -q "path = \".*${dep}\"" "$cargo_toml"; then
|
|
38
|
+
echo "Undeclared local crate dependency: ${dep}"
|
|
39
|
+
fi
|
|
40
|
+
done)
|
|
41
|
+
|
|
42
|
+
if [ -n "$warnings" ]; then
|
|
43
|
+
echo "Dependency warnings:"
|
|
44
|
+
echo "$warnings"
|
|
45
|
+
echo "Recommendation: Declare local crate dependencies in Cargo.toml"
|
|
46
|
+
exit 2
|
|
47
|
+
fi
|
|
48
|
+
fi
|
|
49
|
+
|
|
50
|
+
exit 0
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# Rust Future Safety Validator
|
|
3
|
+
set -o pipefail
|
|
4
|
+
|
|
5
|
+
# Exit codes:
|
|
6
|
+
# 0 - No issues found
|
|
7
|
+
# 1 - File not found or cannot be read
|
|
8
|
+
# 2 - Future safety warnings found
|
|
9
|
+
|
|
10
|
+
if [ $# -eq 0 ]; then
|
|
11
|
+
echo "Usage: $0 <rust_file>"
|
|
12
|
+
exit 1
|
|
13
|
+
fi
|
|
14
|
+
|
|
15
|
+
file_path="$1"
|
|
16
|
+
|
|
17
|
+
if [ ! -f "$file_path" ]; then
|
|
18
|
+
echo "File not found: $file_path"
|
|
19
|
+
exit 1
|
|
20
|
+
fi
|
|
21
|
+
|
|
22
|
+
# Find async function definitions
|
|
23
|
+
async_functions=$(grep -n "async fn" "$file_path")
|
|
24
|
+
|
|
25
|
+
if [ -n "$async_functions" ]; then
|
|
26
|
+
warnings=$(echo "$async_functions" | while read -r async_line; do
|
|
27
|
+
line_num=$(echo "$async_line" | cut -d':' -f1)
|
|
28
|
+
func_name=$(echo "$async_line" | sed -n 's/.*async fn \([a-zA-Z_][a-zA-Z0-9_]*\).*/\1/p')
|
|
29
|
+
|
|
30
|
+
# Search for function calls without .await
|
|
31
|
+
calls_without_await=$(grep -n "\\b${func_name}(" "$file_path" |
|
|
32
|
+
grep -v ".await" |
|
|
33
|
+
grep -v "async fn" |
|
|
34
|
+
grep -v "fn ${func_name}")
|
|
35
|
+
|
|
36
|
+
if [ -n "$calls_without_await" ]; then
|
|
37
|
+
echo "Lines related to async function '${func_name}':"
|
|
38
|
+
echo "$calls_without_await"
|
|
39
|
+
echo "Recommendation: Use .await for async function calls. Run cargo clippy for more details."
|
|
40
|
+
fi
|
|
41
|
+
done)
|
|
42
|
+
|
|
43
|
+
if [ -n "$warnings" ]; then
|
|
44
|
+
echo "Future safety warnings:"
|
|
45
|
+
echo "$warnings"
|
|
46
|
+
exit 2
|
|
47
|
+
fi
|
|
48
|
+
fi
|
|
49
|
+
|
|
50
|
+
exit 0
|
|
@@ -62,7 +62,7 @@ const execAsync = promisify(exec);
|
|
|
62
62
|
// Step 2: Extract and report confidence
|
|
63
63
|
const confidence = extractConfidence(output);
|
|
64
64
|
console.log(`[CFN Protocol] Step 2: Reporting confidence (${confidence})...`);
|
|
65
|
-
const reportCmd = `./.claude/skills/cfn-redis-coordination/
|
|
65
|
+
const reportCmd = `./.claude/skills/cfn-redis-coordination/report-completion.sh \
|
|
66
66
|
--task-id "${taskId}" \
|
|
67
67
|
--agent-id "${agentId}" \
|
|
68
68
|
--confidence ${confidence} \
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/agent-executor.ts"],"sourcesContent":["/**\r\n * Agent Executor\r\n *\r\n * Executes CLI-spawned agents by:\r\n * 1. Checking custom routing configuration (z.ai vs Anthropic)\r\n * 2. Invoking the appropriate API\r\n * 3. Managing agent lifecycle and output\r\n */\r\n\r\nimport { spawn } from 'child_process';\r\nimport { exec } from 'child_process';\r\nimport { promisify } from 'util';\r\nimport { AgentDefinition } from './agent-definition-parser.js';\r\nimport { TaskContext, getAgentId } from './agent-prompt-builder.js';\r\nimport { buildCLIAgentSystemPrompt, loadContextFromEnv } from './cli-agent-context.js';\r\nimport {\r\n loadMessages,\r\n storeMessage,\r\n getCurrentFork,\r\n formatMessagesForAPI,\r\n type Message\r\n} from './conversation-fork.js';\r\nimport { convertToolNames } from './tool-definitions.js';\r\nimport fs from 'fs/promises';\r\nimport path from 'path';\r\nimport os from 'os';\r\n\r\nconst execAsync = promisify(exec);\r\n\r\nexport interface AgentExecutionResult {\r\n success: boolean;\r\n agentId: string;\r\n output?: string;\r\n error?: string;\r\n exitCode: number;\r\n}\r\n\r\n/**\r\n * Extract confidence score from agent output\r\n * Looks for patterns like:\r\n * - \"confidence: 0.85\"\r\n * - \"Confidence: 0.90\"\r\n * - \"confidence score: 0.95\"\r\n * - \"self-confidence: 0.88\"\r\n */\r\nfunction extractConfidence(output: string | undefined): number {\r\n if (!output) return 0.85;\r\n\r\n // Try multiple patterns\r\n const patterns = [\r\n /confidence:\\s*([0-9.]+)/i,\r\n /confidence\\s+score:\\s*([0-9.]+)/i,\r\n /self-confidence:\\s*([0-9.]+)/i,\r\n /my\\s+confidence:\\s*([0-9.]+)/i,\r\n ];\r\n\r\n for (const pattern of patterns) {\r\n const match = output.match(pattern);\r\n if (match && match[1]) {\r\n const score = parseFloat(match[1]);\r\n if (score >= 0 && score <= 1) {\r\n return score;\r\n }\r\n }\r\n }\r\n\r\n // Default to 0.85 if not found\r\n return 0.85;\r\n}\r\n\r\n/**\r\n * Execute CFN Loop protocol after agent completes work\r\n *\r\n * Steps:\r\n * 1. Signal completion to orchestrator\r\n * 2. Report confidence score\r\n * 3. Enter waiting mode (if iterations enabled)\r\n */\r\nasync function executeCFNProtocol(\r\n taskId: string,\r\n agentId: string,\r\n output: string | undefined,\r\n iteration: number,\r\n enableIterations: boolean = false,\r\n maxIterations: number = 10\r\n): Promise<void> {\r\n console.log(`\\n[CFN Protocol] Starting for agent ${agentId}`);\r\n console.log(`[CFN Protocol] Task ID: ${taskId}, Iteration: ${iteration}`);\r\n\r\n try {\r\n // Step 1: Signal completion\r\n console.log('[CFN Protocol] Step 1: Signaling completion...');\r\n await execAsync(`redis-cli lpush \"swarm:${taskId}:${agentId}:done\" \"complete\"`);\r\n console.log('[CFN Protocol] ✓ Completion signaled');\r\n\r\n // Step 2: Extract and report confidence\r\n const confidence = extractConfidence(output);\r\n console.log(`[CFN Protocol] Step 2: Reporting confidence (${confidence})...`);\r\n\r\n const reportCmd = `./.claude/skills/cfn-redis-coordination/invoke-waiting-mode.sh report \\\r\n --task-id \"${taskId}\" \\\r\n --agent-id \"${agentId}\" \\\r\n --confidence ${confidence} \\\r\n --iteration ${iteration}`;\r\n\r\n await execAsync(reportCmd);\r\n console.log('[CFN Protocol] ✓ Confidence reported');\r\n\r\n // Step 3: Exit cleanly (BUG #18 FIX - removed waiting mode)\r\n // Orchestrator will spawn appropriate specialist agent for next iteration\r\n // This enables adaptive agent specialization based on feedback type\r\n console.log('[CFN Protocol] Step 3: Exiting cleanly (iteration complete)');\r\n console.log('[CFN Protocol] Protocol complete\\n');\r\n } catch (error) {\r\n console.error('[CFN Protocol] Error:', error);\r\n throw error;\r\n }\r\n}\r\n\r\n/**\r\n * Check if custom routing (z.ai) is enabled\r\n */\r\nasync function isCustomRoutingEnabled(): Promise<boolean> {\r\n // Check environment variable\r\n if (process.env.CLAUDE_API_PROVIDER === 'zai') {\r\n return true;\r\n }\r\n\r\n // Check config file (.claude/config/api-provider.json)\r\n try {\r\n const configPath = path.join('.claude', 'config', 'api-provider.json');\r\n const config = JSON.parse(await fs.readFile(configPath, 'utf-8'));\r\n return config.provider === 'zai' || config.provider === 'z.ai';\r\n } catch {\r\n return false;\r\n }\r\n}\r\n\r\n/**\r\n * Get API provider configuration\r\n */\r\nasync function getAPIProvider(): Promise<'anthropic' | 'zai'> {\r\n const customEnabled = await isCustomRoutingEnabled();\r\n return customEnabled ? 'zai' : 'anthropic';\r\n}\r\n\r\n/**\r\n * Execute agent using direct API calls\r\n */\r\nasync function executeViaAPI(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext\r\n): Promise<AgentExecutionResult> {\r\n const agentId = getAgentId(definition, context);\r\n\r\n console.log(`[agent-executor] Executing agent via API: ${definition.name}`);\r\n console.log(`[agent-executor] Agent ID: ${agentId}`);\r\n console.log(`[agent-executor] Model: ${definition.model}`);\r\n console.log('');\r\n\r\n try {\r\n // Check for conversation fork (Sprint 4 enhancement)\r\n const forkId = process.env.FORK_ID || await getCurrentFork(context.taskId || '', agentId);\r\n const iteration = context.iteration || 1;\r\n\r\n let systemPrompt: string;\r\n let messages: Array<{role: string, content: string}> = [];\r\n\r\n if (forkId && iteration > 1) {\r\n // Continue from fork (iterations 2+)\r\n console.log(`[agent-executor] Continuing from fork: ${forkId}`);\r\n\r\n // Load fork messages\r\n const forkMessages = await loadMessages(context.taskId || '', agentId, forkId);\r\n console.log(`[agent-executor] Loaded ${forkMessages.length} messages from fork`);\r\n\r\n // Extract system prompt from first message (it's always the system message)\r\n // The fork messages are assistant/user pairs, we need to add system separately\r\n systemPrompt = forkMessages[0]?.content || '';\r\n\r\n // Format remaining messages for API\r\n messages = formatMessagesForAPI(forkMessages.slice(1));\r\n\r\n // Add new user message with feedback\r\n messages.push({\r\n role: 'user',\r\n content: prompt\r\n });\r\n\r\n console.log(`[agent-executor] Fork continuation: ${messages.length} messages`);\r\n } else {\r\n // New conversation (iteration 1)\r\n console.log('[agent-executor] Starting new conversation');\r\n console.log('[agent-executor] Building system prompt with context...');\r\n\r\n const contextOptions = loadContextFromEnv();\r\n contextOptions.agentType = definition.name;\r\n if (context.taskId) contextOptions.taskId = context.taskId;\r\n if (context.iteration) contextOptions.iteration = context.iteration;\r\n\r\n systemPrompt = await buildCLIAgentSystemPrompt(contextOptions);\r\n console.log('[agent-executor] System prompt built successfully');\r\n\r\n // Initial user message\r\n messages = [{\r\n role: 'user',\r\n content: prompt\r\n }];\r\n }\r\n\r\n console.log('');\r\n\r\n // Dynamic import to avoid bundling issues\r\n const { executeAgentAPI } = await import('./anthropic-client.js');\r\n\r\n // Convert agent tool names to Anthropic API format\r\n const tools = definition.tools && definition.tools.length > 0\r\n ? convertToolNames(definition.tools)\r\n : undefined;\r\n\r\n const result = await executeAgentAPI(\r\n definition.name,\r\n agentId,\r\n definition.model,\r\n prompt,\r\n systemPrompt,\r\n messages.length > 1 ? messages : undefined,\r\n undefined, // maxTokens (use default)\r\n tools // Pass converted tools\r\n );\r\n\r\n // Store messages in conversation history (for future forking)\r\n if (context.taskId) {\r\n // Store user message\r\n const userMessage: Message = {\r\n role: 'user',\r\n content: prompt,\r\n iteration,\r\n timestamp: new Date().toISOString()\r\n };\r\n await storeMessage(context.taskId, agentId, userMessage);\r\n\r\n // Store assistant response\r\n if (result.output) {\r\n const assistantMessage: Message = {\r\n role: 'assistant',\r\n content: result.output,\r\n iteration,\r\n timestamp: new Date().toISOString()\r\n };\r\n await storeMessage(context.taskId, agentId, assistantMessage);\r\n }\r\n\r\n console.log(`[agent-executor] Stored messages for iteration ${iteration}`);\r\n\r\n // Execute CFN Loop protocol (signal completion, report confidence, enter waiting mode)\r\n // Iterations are enabled for CFN Loop tasks (indicated by presence of taskId)\r\n try {\r\n const maxIterations = 10; // Default max iterations\r\n const enableIterations = true; // Enable iterations for all CFN Loop tasks\r\n\r\n await executeCFNProtocol(\r\n context.taskId,\r\n agentId,\r\n result.output,\r\n iteration,\r\n enableIterations,\r\n maxIterations\r\n );\r\n } catch (error) {\r\n console.error('[agent-executor] CFN Protocol execution failed:', error);\r\n // Don't fail the entire agent execution if CFN protocol fails\r\n // This allows agents to complete even if Redis coordination has issues\r\n }\r\n }\r\n\r\n return {\r\n success: result.success,\r\n agentId,\r\n output: result.output,\r\n error: result.error,\r\n exitCode: result.success ? 0 : 1,\r\n };\r\n } catch (error) {\r\n console.error('[agent-executor] API execution failed:', error);\r\n return {\r\n success: false,\r\n agentId,\r\n error: error instanceof Error ? error.message : String(error),\r\n exitCode: 1,\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Execute agent using shell script (fallback/simulation)\r\n */\r\nasync function executeViaScript(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext\r\n): Promise<AgentExecutionResult> {\r\n const agentId = getAgentId(definition, context);\r\n\r\n // Write prompt to temporary file\r\n const tmpDir = os.tmpdir();\r\n const promptFile = path.join(tmpDir, `agent-${agentId}-${Date.now()}.md`);\r\n await fs.writeFile(promptFile, prompt, 'utf-8');\r\n\r\n console.log(`[agent-executor] Executing agent via script: ${definition.name}`);\r\n console.log(`[agent-executor] Agent ID: ${agentId}`);\r\n console.log(`[agent-executor] Model: ${definition.model}`);\r\n console.log(`[agent-executor] Prompt file: ${promptFile}`);\r\n\r\n return new Promise((resolve) => {\r\n const scriptPath = path.join('.claude', 'skills', 'agent-execution', 'execute-agent.sh');\r\n\r\n // Build environment variables\r\n const env = {\r\n ...process.env,\r\n AGENT_TYPE: definition.name,\r\n AGENT_ID: agentId,\r\n AGENT_MODEL: definition.model,\r\n AGENT_TOOLS: definition.tools.join(','),\r\n TASK_ID: context.taskId || '',\r\n ITERATION: String(context.iteration || 1),\r\n MODE: context.mode || 'cli',\r\n PROMPT_FILE: promptFile,\r\n };\r\n\r\n // Check if execute script exists\r\n fs.access(scriptPath)\r\n .then(() => {\r\n // Use execution script\r\n const proc = spawn('bash', [scriptPath], { env, stdio: 'inherit' });\r\n\r\n proc.on('exit', (code) => {\r\n resolve({\r\n success: code === 0,\r\n agentId,\r\n exitCode: code || 0,\r\n });\r\n });\r\n\r\n proc.on('error', (err) => {\r\n resolve({\r\n success: false,\r\n agentId,\r\n error: err.message,\r\n exitCode: 1,\r\n });\r\n });\r\n })\r\n .catch(() => {\r\n // Fallback: Print prompt\r\n console.log('\\n=== Agent Prompt ===');\r\n console.log(prompt.substring(0, 500) + '...');\r\n console.log('\\n[agent-executor] Execution script not found');\r\n console.log('[agent-executor] Using simulation mode\\n');\r\n\r\n resolve({\r\n success: true,\r\n agentId,\r\n output: prompt,\r\n exitCode: 0,\r\n });\r\n });\r\n });\r\n}\r\n\r\n/**\r\n * Main agent execution function\r\n */\r\nexport async function executeAgent(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext,\r\n options: {\r\n method?: 'auto' | 'api' | 'script';\r\n } = {}\r\n): Promise<AgentExecutionResult> {\r\n const method = options.method || 'auto';\r\n\r\n // Auto-select execution method\r\n if (method === 'auto') {\r\n // Try API execution first, fallback to script if API key not available\r\n try {\r\n return await executeViaAPI(definition, prompt, context);\r\n } catch (error) {\r\n if (error instanceof Error && error.message.includes('API key not found')) {\r\n console.log('[agent-executor] API key not found, using script fallback');\r\n return executeViaScript(definition, prompt, context);\r\n }\r\n throw error;\r\n }\r\n }\r\n\r\n if (method === 'api') {\r\n return executeViaAPI(definition, prompt, context);\r\n }\r\n\r\n return executeViaScript(definition, prompt, context);\r\n}\r\n\r\n/**\r\n * Write agent output to file for debugging\r\n */\r\nexport async function saveAgentOutput(\r\n agentId: string,\r\n output: string,\r\n outputDir: string = '.claude/tmp/agent-output'\r\n): Promise<string> {\r\n await fs.mkdir(outputDir, { recursive: true });\r\n\r\n const timestamp = new Date().toISOString().replace(/[:.]/g, '-');\r\n const filename = `${agentId}-${timestamp}.txt`;\r\n const filepath = path.join(outputDir, filename);\r\n\r\n await fs.writeFile(filepath, output, 'utf-8');\r\n\r\n return filepath;\r\n}\r\n"],"names":["spawn","exec","promisify","getAgentId","buildCLIAgentSystemPrompt","loadContextFromEnv","loadMessages","storeMessage","getCurrentFork","formatMessagesForAPI","convertToolNames","fs","path","os","execAsync","extractConfidence","output","patterns","pattern","match","score","parseFloat","executeCFNProtocol","taskId","agentId","iteration","enableIterations","maxIterations","console","log","confidence","reportCmd","error","isCustomRoutingEnabled","process","env","CLAUDE_API_PROVIDER","configPath","join","config","JSON","parse","readFile","provider","getAPIProvider","customEnabled","executeViaAPI","definition","prompt","context","name","model","forkId","FORK_ID","systemPrompt","messages","forkMessages","length","content","slice","push","role","contextOptions","agentType","executeAgentAPI","tools","undefined","result","userMessage","timestamp","Date","toISOString","assistantMessage","success","exitCode","Error","message","String","executeViaScript","tmpDir","tmpdir","promptFile","now","writeFile","Promise","resolve","scriptPath","AGENT_TYPE","AGENT_ID","AGENT_MODEL","AGENT_TOOLS","TASK_ID","ITERATION","MODE","mode","PROMPT_FILE","access","then","proc","stdio","on","code","err","catch","substring","executeAgent","options","method","includes","saveAgentOutput","outputDir","mkdir","recursive","replace","filename","filepath"],"mappings":"AAAA;;;;;;;CAOC,GAED,SAASA,KAAK,QAAQ,gBAAgB;AACtC,SAASC,IAAI,QAAQ,gBAAgB;AACrC,SAASC,SAAS,QAAQ,OAAO;AAEjC,SAAsBC,UAAU,QAAQ,4BAA4B;AACpE,SAASC,yBAAyB,EAAEC,kBAAkB,QAAQ,yBAAyB;AACvF,SACEC,YAAY,EACZC,YAAY,EACZC,cAAc,EACdC,oBAAoB,QAEf,yBAAyB;AAChC,SAASC,gBAAgB,QAAQ,wBAAwB;AACzD,OAAOC,QAAQ,cAAc;AAC7B,OAAOC,UAAU,OAAO;AACxB,OAAOC,QAAQ,KAAK;AAEpB,MAAMC,YAAYZ,UAAUD;AAU5B;;;;;;;CAOC,GACD,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WAAWI;IAEtC,MAAMvG,GAAGwE,SAAS,CAACgC,UAAUnG,QAAQ;IAErC,OAAOmG;AACT"}
|
|
1
|
+
{"version":3,"sources":["../../src/cli/agent-executor.ts"],"sourcesContent":["/**\r\n * Agent Executor\r\n *\r\n * Executes CLI-spawned agents by:\r\n * 1. Checking custom routing configuration (z.ai vs Anthropic)\r\n * 2. Invoking the appropriate API\r\n * 3. Managing agent lifecycle and output\r\n */\r\n\r\nimport { spawn } from 'child_process';\r\nimport { exec } from 'child_process';\r\nimport { promisify } from 'util';\r\nimport { AgentDefinition } from './agent-definition-parser.js';\r\nimport { TaskContext, getAgentId } from './agent-prompt-builder.js';\r\nimport { buildCLIAgentSystemPrompt, loadContextFromEnv } from './cli-agent-context.js';\r\nimport {\r\n loadMessages,\r\n storeMessage,\r\n getCurrentFork,\r\n formatMessagesForAPI,\r\n type Message\r\n} from './conversation-fork.js';\r\nimport { convertToolNames } from './tool-definitions.js';\r\nimport fs from 'fs/promises';\r\nimport path from 'path';\r\nimport os from 'os';\r\n\r\nconst execAsync = promisify(exec);\r\n\r\nexport interface AgentExecutionResult {\r\n success: boolean;\r\n agentId: string;\r\n output?: string;\r\n error?: string;\r\n exitCode: number;\r\n}\r\n\r\n/**\r\n * Extract confidence score from agent output\r\n * Looks for patterns like:\r\n * - \"confidence: 0.85\"\r\n * - \"Confidence: 0.90\"\r\n * - \"confidence score: 0.95\"\r\n * - \"self-confidence: 0.88\"\r\n */\r\nfunction extractConfidence(output: string | undefined): number {\r\n if (!output) return 0.85;\r\n\r\n // Try multiple patterns\r\n const patterns = [\r\n /confidence:\\s*([0-9.]+)/i,\r\n /confidence\\s+score:\\s*([0-9.]+)/i,\r\n /self-confidence:\\s*([0-9.]+)/i,\r\n /my\\s+confidence:\\s*([0-9.]+)/i,\r\n ];\r\n\r\n for (const pattern of patterns) {\r\n const match = output.match(pattern);\r\n if (match && match[1]) {\r\n const score = parseFloat(match[1]);\r\n if (score >= 0 && score <= 1) {\r\n return score;\r\n }\r\n }\r\n }\r\n\r\n // Default to 0.85 if not found\r\n return 0.85;\r\n}\r\n\r\n/**\r\n * Execute CFN Loop protocol after agent completes work\r\n *\r\n * Steps:\r\n * 1. Signal completion to orchestrator\r\n * 2. Report confidence score\r\n * 3. Enter waiting mode (if iterations enabled)\r\n */\r\nasync function executeCFNProtocol(\r\n taskId: string,\r\n agentId: string,\r\n output: string | undefined,\r\n iteration: number,\r\n enableIterations: boolean = false,\r\n maxIterations: number = 10\r\n): Promise<void> {\r\n console.log(`\\n[CFN Protocol] Starting for agent ${agentId}`);\r\n console.log(`[CFN Protocol] Task ID: ${taskId}, Iteration: ${iteration}`);\r\n\r\n try {\r\n // Step 1: Signal completion\r\n console.log('[CFN Protocol] Step 1: Signaling completion...');\r\n await execAsync(`redis-cli lpush \"swarm:${taskId}:${agentId}:done\" \"complete\"`);\r\n console.log('[CFN Protocol] ✓ Completion signaled');\r\n\r\n // Step 2: Extract and report confidence\r\n const confidence = extractConfidence(output);\r\n console.log(`[CFN Protocol] Step 2: Reporting confidence (${confidence})...`);\r\n\r\n const reportCmd = `./.claude/skills/cfn-redis-coordination/report-completion.sh \\\r\n --task-id \"${taskId}\" \\\r\n --agent-id \"${agentId}\" \\\r\n --confidence ${confidence} \\\r\n --iteration ${iteration}`;\r\n\r\n await execAsync(reportCmd);\r\n console.log('[CFN Protocol] ✓ Confidence reported');\r\n\r\n // Step 3: Exit cleanly (BUG #18 FIX - removed waiting mode)\r\n // Orchestrator will spawn appropriate specialist agent for next iteration\r\n // This enables adaptive agent specialization based on feedback type\r\n console.log('[CFN Protocol] Step 3: Exiting cleanly (iteration complete)');\r\n console.log('[CFN Protocol] Protocol complete\\n');\r\n } catch (error) {\r\n console.error('[CFN Protocol] Error:', error);\r\n throw error;\r\n }\r\n}\r\n\r\n/**\r\n * Check if custom routing (z.ai) is enabled\r\n */\r\nasync function isCustomRoutingEnabled(): Promise<boolean> {\r\n // Check environment variable\r\n if (process.env.CLAUDE_API_PROVIDER === 'zai') {\r\n return true;\r\n }\r\n\r\n // Check config file (.claude/config/api-provider.json)\r\n try {\r\n const configPath = path.join('.claude', 'config', 'api-provider.json');\r\n const config = JSON.parse(await fs.readFile(configPath, 'utf-8'));\r\n return config.provider === 'zai' || config.provider === 'z.ai';\r\n } catch {\r\n return false;\r\n }\r\n}\r\n\r\n/**\r\n * Get API provider configuration\r\n */\r\nasync function getAPIProvider(): Promise<'anthropic' | 'zai'> {\r\n const customEnabled = await isCustomRoutingEnabled();\r\n return customEnabled ? 'zai' : 'anthropic';\r\n}\r\n\r\n/**\r\n * Execute agent using direct API calls\r\n */\r\nasync function executeViaAPI(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext\r\n): Promise<AgentExecutionResult> {\r\n const agentId = getAgentId(definition, context);\r\n\r\n console.log(`[agent-executor] Executing agent via API: ${definition.name}`);\r\n console.log(`[agent-executor] Agent ID: ${agentId}`);\r\n console.log(`[agent-executor] Model: ${definition.model}`);\r\n console.log('');\r\n\r\n try {\r\n // Check for conversation fork (Sprint 4 enhancement)\r\n const forkId = process.env.FORK_ID || await getCurrentFork(context.taskId || '', agentId);\r\n const iteration = context.iteration || 1;\r\n\r\n let systemPrompt: string;\r\n let messages: Array<{role: string, content: string}> = [];\r\n\r\n if (forkId && iteration > 1) {\r\n // Continue from fork (iterations 2+)\r\n console.log(`[agent-executor] Continuing from fork: ${forkId}`);\r\n\r\n // Load fork messages\r\n const forkMessages = await loadMessages(context.taskId || '', agentId, forkId);\r\n console.log(`[agent-executor] Loaded ${forkMessages.length} messages from fork`);\r\n\r\n // Extract system prompt from first message (it's always the system message)\r\n // The fork messages are assistant/user pairs, we need to add system separately\r\n systemPrompt = forkMessages[0]?.content || '';\r\n\r\n // Format remaining messages for API\r\n messages = formatMessagesForAPI(forkMessages.slice(1));\r\n\r\n // Add new user message with feedback\r\n messages.push({\r\n role: 'user',\r\n content: prompt\r\n });\r\n\r\n console.log(`[agent-executor] Fork continuation: ${messages.length} messages`);\r\n } else {\r\n // New conversation (iteration 1)\r\n console.log('[agent-executor] Starting new conversation');\r\n console.log('[agent-executor] Building system prompt with context...');\r\n\r\n const contextOptions = loadContextFromEnv();\r\n contextOptions.agentType = definition.name;\r\n if (context.taskId) contextOptions.taskId = context.taskId;\r\n if (context.iteration) contextOptions.iteration = context.iteration;\r\n\r\n systemPrompt = await buildCLIAgentSystemPrompt(contextOptions);\r\n console.log('[agent-executor] System prompt built successfully');\r\n\r\n // Initial user message\r\n messages = [{\r\n role: 'user',\r\n content: prompt\r\n }];\r\n }\r\n\r\n console.log('');\r\n\r\n // Dynamic import to avoid bundling issues\r\n const { executeAgentAPI } = await import('./anthropic-client.js');\r\n\r\n // Convert agent tool names to Anthropic API format\r\n const tools = definition.tools && definition.tools.length > 0\r\n ? convertToolNames(definition.tools)\r\n : undefined;\r\n\r\n const result = await executeAgentAPI(\r\n definition.name,\r\n agentId,\r\n definition.model,\r\n prompt,\r\n systemPrompt,\r\n messages.length > 1 ? messages : undefined,\r\n undefined, // maxTokens (use default)\r\n tools // Pass converted tools\r\n );\r\n\r\n // Store messages in conversation history (for future forking)\r\n if (context.taskId) {\r\n // Store user message\r\n const userMessage: Message = {\r\n role: 'user',\r\n content: prompt,\r\n iteration,\r\n timestamp: new Date().toISOString()\r\n };\r\n await storeMessage(context.taskId, agentId, userMessage);\r\n\r\n // Store assistant response\r\n if (result.output) {\r\n const assistantMessage: Message = {\r\n role: 'assistant',\r\n content: result.output,\r\n iteration,\r\n timestamp: new Date().toISOString()\r\n };\r\n await storeMessage(context.taskId, agentId, assistantMessage);\r\n }\r\n\r\n console.log(`[agent-executor] Stored messages for iteration ${iteration}`);\r\n\r\n // Execute CFN Loop protocol (signal completion, report confidence, enter waiting mode)\r\n // Iterations are enabled for CFN Loop tasks (indicated by presence of taskId)\r\n try {\r\n const maxIterations = 10; // Default max iterations\r\n const enableIterations = true; // Enable iterations for all CFN Loop tasks\r\n\r\n await executeCFNProtocol(\r\n context.taskId,\r\n agentId,\r\n result.output,\r\n iteration,\r\n enableIterations,\r\n maxIterations\r\n );\r\n } catch (error) {\r\n console.error('[agent-executor] CFN Protocol execution failed:', error);\r\n // Don't fail the entire agent execution if CFN protocol fails\r\n // This allows agents to complete even if Redis coordination has issues\r\n }\r\n }\r\n\r\n return {\r\n success: result.success,\r\n agentId,\r\n output: result.output,\r\n error: result.error,\r\n exitCode: result.success ? 0 : 1,\r\n };\r\n } catch (error) {\r\n console.error('[agent-executor] API execution failed:', error);\r\n return {\r\n success: false,\r\n agentId,\r\n error: error instanceof Error ? error.message : String(error),\r\n exitCode: 1,\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Execute agent using shell script (fallback/simulation)\r\n */\r\nasync function executeViaScript(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext\r\n): Promise<AgentExecutionResult> {\r\n const agentId = getAgentId(definition, context);\r\n\r\n // Write prompt to temporary file\r\n const tmpDir = os.tmpdir();\r\n const promptFile = path.join(tmpDir, `agent-${agentId}-${Date.now()}.md`);\r\n await fs.writeFile(promptFile, prompt, 'utf-8');\r\n\r\n console.log(`[agent-executor] Executing agent via script: ${definition.name}`);\r\n console.log(`[agent-executor] Agent ID: ${agentId}`);\r\n console.log(`[agent-executor] Model: ${definition.model}`);\r\n console.log(`[agent-executor] Prompt file: ${promptFile}`);\r\n\r\n return new Promise((resolve) => {\r\n const scriptPath = path.join('.claude', 'skills', 'agent-execution', 'execute-agent.sh');\r\n\r\n // Build environment variables\r\n const env = {\r\n ...process.env,\r\n AGENT_TYPE: definition.name,\r\n AGENT_ID: agentId,\r\n AGENT_MODEL: definition.model,\r\n AGENT_TOOLS: definition.tools.join(','),\r\n TASK_ID: context.taskId || '',\r\n ITERATION: String(context.iteration || 1),\r\n MODE: context.mode || 'cli',\r\n PROMPT_FILE: promptFile,\r\n };\r\n\r\n // Check if execute script exists\r\n fs.access(scriptPath)\r\n .then(() => {\r\n // Use execution script\r\n const proc = spawn('bash', [scriptPath], { env, stdio: 'inherit' });\r\n\r\n proc.on('exit', (code) => {\r\n resolve({\r\n success: code === 0,\r\n agentId,\r\n exitCode: code || 0,\r\n });\r\n });\r\n\r\n proc.on('error', (err) => {\r\n resolve({\r\n success: false,\r\n agentId,\r\n error: err.message,\r\n exitCode: 1,\r\n });\r\n });\r\n })\r\n .catch(() => {\r\n // Fallback: Print prompt\r\n console.log('\\n=== Agent Prompt ===');\r\n console.log(prompt.substring(0, 500) + '...');\r\n console.log('\\n[agent-executor] Execution script not found');\r\n console.log('[agent-executor] Using simulation mode\\n');\r\n\r\n resolve({\r\n success: true,\r\n agentId,\r\n output: prompt,\r\n exitCode: 0,\r\n });\r\n });\r\n });\r\n}\r\n\r\n/**\r\n * Main agent execution function\r\n */\r\nexport async function executeAgent(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext,\r\n options: {\r\n method?: 'auto' | 'api' | 'script';\r\n } = {}\r\n): Promise<AgentExecutionResult> {\r\n const method = options.method || 'auto';\r\n\r\n // Auto-select execution method\r\n if (method === 'auto') {\r\n // Try API execution first, fallback to script if API key not available\r\n try {\r\n return await executeViaAPI(definition, prompt, context);\r\n } catch (error) {\r\n if (error instanceof Error && error.message.includes('API key not found')) {\r\n console.log('[agent-executor] API key not found, using script fallback');\r\n return executeViaScript(definition, prompt, context);\r\n }\r\n throw error;\r\n }\r\n }\r\n\r\n if (method === 'api') {\r\n return executeViaAPI(definition, prompt, context);\r\n }\r\n\r\n return executeViaScript(definition, prompt, context);\r\n}\r\n\r\n/**\r\n * Write agent output to file for debugging\r\n */\r\nexport async function saveAgentOutput(\r\n agentId: string,\r\n output: string,\r\n outputDir: string = '.claude/tmp/agent-output'\r\n): Promise<string> {\r\n await fs.mkdir(outputDir, { recursive: true });\r\n\r\n const timestamp = new Date().toISOString().replace(/[:.]/g, '-');\r\n const filename = `${agentId}-${timestamp}.txt`;\r\n const filepath = path.join(outputDir, filename);\r\n\r\n await fs.writeFile(filepath, output, 'utf-8');\r\n\r\n return filepath;\r\n}\r\n"],"names":["spawn","exec","promisify","getAgentId","buildCLIAgentSystemPrompt","loadContextFromEnv","loadMessages","storeMessage","getCurrentFork","formatMessagesForAPI","convertToolNames","fs","path","os","execAsync","extractConfidence","output","patterns","pattern","match","score","parseFloat","executeCFNProtocol","taskId","agentId","iteration","enableIterations","maxIterations","console","log","confidence","reportCmd","error","isCustomRoutingEnabled","process","env","CLAUDE_API_PROVIDER","configPath","join","config","JSON","parse","readFile","provider","getAPIProvider","customEnabled","executeViaAPI","definition","prompt","context","name","model","forkId","FORK_ID","systemPrompt","messages","forkMessages","length","content","slice","push","role","contextOptions","agentType","executeAgentAPI","tools","undefined","result","userMessage","timestamp","Date","toISOString","assistantMessage","success","exitCode","Error","message","String","executeViaScript","tmpDir","tmpdir","promptFile","now","writeFile","Promise","resolve","scriptPath","AGENT_TYPE","AGENT_ID","AGENT_MODEL","AGENT_TOOLS","TASK_ID","ITERATION","MODE","mode","PROMPT_FILE","access","then","proc","stdio","on","code","err","catch","substring","executeAgent","options","method","includes","saveAgentOutput","outputDir","mkdir","recursive","replace","filename","filepath"],"mappings":"AAAA;;;;;;;CAOC,GAED,SAASA,KAAK,QAAQ,gBAAgB;AACtC,SAASC,IAAI,QAAQ,gBAAgB;AACrC,SAASC,SAAS,QAAQ,OAAO;AAEjC,SAAsBC,UAAU,QAAQ,4BAA4B;AACpE,SAASC,yBAAyB,EAAEC,kBAAkB,QAAQ,yBAAyB;AACvF,SACEC,YAAY,EACZC,YAAY,EACZC,cAAc,EACdC,oBAAoB,QAEf,yBAAyB;AAChC,SAASC,gBAAgB,QAAQ,wBAAwB;AACzD,OAAOC,QAAQ,cAAc;AAC7B,OAAOC,UAAU,OAAO;AACxB,OAAOC,QAAQ,KAAK;AAEpB,MAAMC,YAAYZ,UAAUD;AAU5B;;;;;;;CAOC,GACD,SAASc,kBAAkBC,MAA0B;IACnD,IAAI,CAACA,QAAQ,OAAO;IAEpB,wBAAwB;IACxB,MAAMC,WAAW;QACf;QACA;QAC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 @@
|
|
|
7
7
|
* - CFN Loop protocol (if applicable)
|
|
8
8
|
* - Iteration history (Sprint 3 - Phase 2)
|
|
9
9
|
* - Environment variables
|
|
10
|
-
*/ import {
|
|
11
|
-
import { loadIterationHistory, formatIterationHistory } from './iteration-history.js';
|
|
10
|
+
*/ import { loadIterationHistory, formatIterationHistory } from './iteration-history.js';
|
|
12
11
|
/**
|
|
13
12
|
* Build CFN Loop protocol instructions
|
|
14
13
|
*/ function buildCFNLoopProtocol(taskId, agentId) {
|
|
@@ -17,43 +16,53 @@ import { loadIterationHistory, formatIterationHistory } from './iteration-histor
|
|
|
17
16
|
|
|
18
17
|
You are participating in a CFN Loop workflow. Follow this protocol EXACTLY:
|
|
19
18
|
|
|
20
|
-
### Step 1: Complete Work
|
|
19
|
+
### Step 1: Complete Your Work
|
|
21
20
|
Execute your assigned task (implementation, review, testing, etc.)
|
|
22
21
|
|
|
23
|
-
### Step 2:
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
### Step 2: Calculate Confidence Score
|
|
23
|
+
Assess your work quality and assign a confidence score (0.0-1.0):
|
|
24
|
+
- 0.90-1.0: Excellent, production-ready
|
|
25
|
+
- 0.75-0.89: Good, minor issues possible
|
|
26
|
+
- 0.50-0.74: Acceptable, needs review
|
|
27
|
+
- 0.0-0.49: Poor, significant issues
|
|
27
28
|
|
|
28
|
-
### Step 3: Report
|
|
29
|
+
### Step 3: Report Completion to Redis
|
|
29
30
|
\`\`\`bash
|
|
30
|
-
./.claude/skills/redis-coordination/
|
|
31
|
+
./.claude/skills/cfn-redis-coordination/report-completion.sh \\
|
|
31
32
|
--task-id "${taskId}" \\
|
|
32
|
-
--agent-id "
|
|
33
|
-
--confidence [
|
|
34
|
-
--iteration
|
|
33
|
+
--agent-id "${agentId}" \\
|
|
34
|
+
--confidence [YOUR_CONFIDENCE_SCORE] \\
|
|
35
|
+
--iteration \${ITERATION:-1}
|
|
35
36
|
\`\`\`
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
This script automatically:
|
|
39
|
+
- Signals completion via \`swarm:${taskId}:${agentId}:done\`
|
|
40
|
+
- Stores confidence in \`swarm:${taskId}:${agentId}:confidence\`
|
|
41
|
+
- Creates result hash in \`swarm:${taskId}:${agentId}:result\`
|
|
42
|
+
- Updates SQLite persistence layer
|
|
43
|
+
|
|
44
|
+
### Step 4: Exit Cleanly
|
|
45
|
+
After reporting, exit immediately. DO NOT enter waiting mode.
|
|
46
|
+
|
|
47
|
+
The orchestrator will:
|
|
48
|
+
- Collect confidence scores from all agents
|
|
49
|
+
- Run gate check (≥0.75 threshold)
|
|
50
|
+
- Spawn validators if gate passes
|
|
51
|
+
- Spawn fresh agents for iteration N+1 if needed
|
|
44
52
|
|
|
45
|
-
**
|
|
53
|
+
**Environment Variables Available:**
|
|
46
54
|
- TASK_ID: ${taskId}
|
|
47
|
-
- AGENT_ID: ${agentId}
|
|
48
|
-
-
|
|
55
|
+
- AGENT_ID: ${agentId}
|
|
56
|
+
- ITERATION: Current iteration number (default: 1)
|
|
57
|
+
- CONFIDENCE_SCORE: Your final confidence assessment
|
|
49
58
|
|
|
50
59
|
**Why This Matters:**
|
|
51
|
-
-
|
|
52
|
-
-
|
|
53
|
-
-
|
|
54
|
-
-
|
|
60
|
+
- Enables zero-token coordination (orchestrator uses Redis BLPOP)
|
|
61
|
+
- Supports adaptive agent specialization (spawn different specialist for iteration N+1)
|
|
62
|
+
- Prevents memory leaks (agents exit after reporting)
|
|
63
|
+
- Confidence scores drive gate checks and consensus validation
|
|
55
64
|
|
|
56
|
-
**
|
|
65
|
+
**CRITICAL:** Report completion before exiting. Orchestrator is waiting for your signal.
|
|
57
66
|
`;
|
|
58
67
|
}
|
|
59
68
|
/**
|
|
@@ -179,7 +188,8 @@ ${env.join('\n')}
|
|
|
179
188
|
/**
|
|
180
189
|
* Build complete prompt for agent execution (async for iteration history)
|
|
181
190
|
*/ export async function buildAgentPrompt(definition, context) {
|
|
182
|
-
|
|
191
|
+
// Use explicit agent ID if provided, otherwise generate from name + iteration
|
|
192
|
+
const agentId = context.agentId || `${definition.name}-${context.iteration || 1}`;
|
|
183
193
|
const sections = [];
|
|
184
194
|
// 1. Agent definition header
|
|
185
195
|
sections.push(`# Agent: ${definition.name}`);
|
|
@@ -209,8 +219,8 @@ ${env.join('\n')}
|
|
|
209
219
|
sections.push('');
|
|
210
220
|
sections.push(definition.content);
|
|
211
221
|
sections.push('');
|
|
212
|
-
// 5. CFN Loop protocol (
|
|
213
|
-
if (context.taskId &&
|
|
222
|
+
// 5. CFN Loop protocol (ALWAYS inject when taskId present - enables Redis coordination)
|
|
223
|
+
if (context.taskId && agentId) {
|
|
214
224
|
sections.push(buildCFNLoopProtocol(context.taskId, agentId));
|
|
215
225
|
sections.push('');
|
|
216
226
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/agent-prompt-builder.ts"],"sourcesContent":["/**\r\n * Agent Prompt Builder\r\n *\r\n * Builds comprehensive prompts for CLI-spawned agents by combining:\r\n * - Agent definition (YAML + markdown)\r\n * - Task context (taskId, iteration, mode)\r\n * - CFN Loop protocol (if applicable)\r\n * - Iteration history (Sprint 3 - Phase 2)\r\n * - Environment variables\r\n */\r\n\r\nimport { AgentDefinition, hasCFNLoopProtocol } from './agent-definition-parser.js';\r\nimport { loadIterationHistory, formatIterationHistory } from './iteration-history.js';\r\n\r\nexport interface TaskContext {\r\n taskId?: string;\r\n iteration?: number;\r\n context?: string;\r\n mode?: string;\r\n priority?: number;\r\n parentTaskId?: string;\r\n agentId?: string;\r\n}\r\n\r\n/**\r\n * Build CFN Loop protocol instructions\r\n */\r\nfunction buildCFNLoopProtocol(taskId: string, agentId: string): string {\r\n return `\r\n## CFN Loop Redis Completion Protocol\r\n\r\nYou are participating in a CFN Loop workflow. Follow this protocol EXACTLY:\r\n\r\n### Step 1: Complete Work\r\nExecute your assigned task (implementation, review, testing, etc.)\r\n\r\n### Step 2: Signal Completion\r\n\\`\\`\\`bash\r\nredis-cli lpush \"swarm:${taskId}:\\${AGENT_ID}:done\" \"complete\"\r\n\\`\\`\\`\r\n\r\n### Step 3: Report Confidence Score\r\n\\`\\`\\`bash\r\n./.claude/skills/redis-coordination/invoke-waiting-mode.sh report \\\\\r\n --task-id \"${taskId}\" \\\\\r\n --agent-id \"\\${AGENT_ID}\" \\\\\r\n --confidence [0.0-1.0] \\\\\r\n --iteration ${taskId.includes('iteration') ? 'CURRENT_ITERATION' : '1'}\r\n\\`\\`\\`\r\n\r\n### Step 4: Enter Waiting Mode (for potential iteration)\r\n\\`\\`\\`bash\r\n./.claude/skills/redis-coordination/invoke-waiting-mode.sh enter \\\\\r\n --task-id \"${taskId}\" \\\\\r\n --agent-id \"\\${AGENT_ID}\" \\\\\r\n --context \"iteration-complete\"\r\n\\`\\`\\`\r\n\r\n**Critical Context Variables:**\r\n- TASK_ID: ${taskId}\r\n- AGENT_ID: ${agentId} (your unique identifier)\r\n- Confidence: Your self-assessment score (0.0-1.0)\r\n\r\n**Why This Matters:**\r\n- Zero-token blocking coordination (BLPOP waits without API calls)\r\n- Orchestrator collects confidence/consensus scores automatically\r\n- Supports autonomous iteration based on quality gates\r\n- Agent woken instantly (<100ms) if iteration needed\r\n\r\n**IMPORTANT:** You MUST complete all 4 steps in order. The orchestrator is waiting for your completion signal.\r\n`;\r\n}\r\n\r\n/**\r\n * Parse and enrich JSON context into natural language instructions\r\n */\r\nfunction enrichJSONContext(jsonObj: any): string {\r\n const sections: string[] = [];\r\n\r\n // Extract task description\r\n if (jsonObj.task) {\r\n sections.push(`**Task:** ${jsonObj.task}`);\r\n }\r\n\r\n // Parse files - convert comma-separated string to bullet list\r\n if (jsonObj.files) {\r\n const fileList = typeof jsonObj.files === 'string'\r\n ? jsonObj.files.split(',').map(f => f.trim()).filter(f => f)\r\n : Array.isArray(jsonObj.files) ? jsonObj.files : [];\r\n\r\n if (fileList.length > 0) {\r\n sections.push('\\n**Files to process:**');\r\n fileList.forEach(file => sections.push(`- ${file}`));\r\n }\r\n }\r\n\r\n // Add requirements/deliverables\r\n if (jsonObj.requirements) {\r\n const reqs = Array.isArray(jsonObj.requirements) ? jsonObj.requirements : [jsonObj.requirements];\r\n sections.push('\\n**Requirements:**');\r\n reqs.forEach((req, i) => sections.push(`${i + 1}. ${req}`));\r\n }\r\n\r\n if (jsonObj.deliverables) {\r\n const delivs = Array.isArray(jsonObj.deliverables) ? jsonObj.deliverables : [jsonObj.deliverables];\r\n sections.push('\\n**Deliverables:**');\r\n delivs.forEach(deliv => sections.push(`- ${deliv}`));\r\n }\r\n\r\n // Add batch information\r\n if (jsonObj.batch) {\r\n sections.push(`\\n**Batch:** ${jsonObj.batch}`);\r\n }\r\n\r\n // Add directory context\r\n if (jsonObj.directory) {\r\n sections.push(`\\n**Working Directory:** ${jsonObj.directory}`);\r\n }\r\n\r\n // Add acceptance criteria\r\n if (jsonObj.acceptanceCriteria) {\r\n const criteria = Array.isArray(jsonObj.acceptanceCriteria)\r\n ? jsonObj.acceptanceCriteria\r\n : [jsonObj.acceptanceCriteria];\r\n sections.push('\\n**Acceptance Criteria:**');\r\n criteria.forEach(criterion => sections.push(`- ${criterion}`));\r\n }\r\n\r\n // Add explicit instructions if present\r\n if (jsonObj.instructions) {\r\n sections.push('\\n**Instructions:**');\r\n const instrs = Array.isArray(jsonObj.instructions) ? jsonObj.instructions : [jsonObj.instructions];\r\n instrs.forEach((instr, i) => sections.push(`${i + 1}. ${instr}`));\r\n }\r\n\r\n return sections.join('\\n');\r\n}\r\n\r\n/**\r\n * Build task description from context\r\n */\r\nfunction buildTaskDescription(agentType: string, context: TaskContext): string {\r\n let desc = '';\r\n\r\n if (context.context) {\r\n // Try to parse as JSON first\r\n let contextStr = context.context.trim();\r\n\r\n // Check if context looks like JSON\r\n if ((contextStr.startsWith('{') && contextStr.endsWith('}')) ||\r\n (contextStr.startsWith('[') && contextStr.endsWith(']'))) {\r\n try {\r\n const jsonObj = JSON.parse(contextStr);\r\n desc = enrichJSONContext(jsonObj);\r\n\r\n // Add instruction footer for structured tasks\r\n if (jsonObj.files || jsonObj.deliverables) {\r\n desc += '\\n\\n**Process each item systematically and report confidence when complete.**';\r\n }\r\n } catch (e) {\r\n // Not valid JSON, treat as plain text\r\n desc = context.context;\r\n }\r\n } else {\r\n // Plain text context\r\n desc = context.context;\r\n }\r\n } else {\r\n desc = `Execute task as ${agentType} agent`;\r\n }\r\n\r\n // Add metadata fields\r\n if (context.taskId) {\r\n desc += `\\n\\n**Task ID:** ${context.taskId}`;\r\n }\r\n\r\n if (context.iteration) {\r\n desc += `\\n**Iteration:** ${context.iteration}`;\r\n }\r\n\r\n if (context.mode) {\r\n desc += `\\n**Mode:** ${context.mode}`;\r\n }\r\n\r\n if (context.priority) {\r\n desc += `\\n**Priority:** ${context.priority}`;\r\n }\r\n\r\n if (context.parentTaskId) {\r\n desc += `\\n**Parent Task:** ${context.parentTaskId}`;\r\n }\r\n\r\n return desc;\r\n}\r\n\r\n/**\r\n * Build environment context section\r\n */\r\nfunction buildEnvironmentContext(context: TaskContext): string {\r\n const env: string[] = [];\r\n\r\n if (context.taskId) env.push(`TASK_ID=${context.taskId}`);\r\n if (context.iteration) env.push(`ITERATION=${context.iteration}`);\r\n if (context.mode) env.push(`MODE=${context.mode}`);\r\n if (context.priority) env.push(`PRIORITY=${context.priority}`);\r\n if (context.parentTaskId) env.push(`PARENT_TASK_ID=${context.parentTaskId}`);\r\n\r\n if (env.length === 0) return '';\r\n\r\n return `\r\n## Environment Variables\r\n\r\n\\`\\`\\`bash\r\n${env.join('\\n')}\r\n\\`\\`\\`\r\n`;\r\n}\r\n\r\n/**\r\n * Build complete prompt for agent execution (async for iteration history)\r\n */\r\nexport async function buildAgentPrompt(\r\n definition: AgentDefinition,\r\n context: TaskContext\r\n): Promise<string> {\r\n const agentId = `${definition.name}-${context.iteration || 1}`;\r\n\r\n const sections: string[] = [];\r\n\r\n // 1. Agent definition header\r\n sections.push(`# Agent: ${definition.name}`);\r\n sections.push('');\r\n sections.push(definition.description);\r\n sections.push('');\r\n\r\n // 2. Task description\r\n sections.push('## Task');\r\n sections.push('');\r\n sections.push(buildTaskDescription(definition.name, context));\r\n sections.push('');\r\n\r\n // 3. Iteration history (Sprint 3 - Phase 2)\r\n // Load and format previous iterations if iteration > 1\r\n if (context.taskId && context.iteration && context.iteration > 1) {\r\n try {\r\n const history = await loadIterationHistory(context.taskId, agentId, context.iteration);\r\n const historyText = formatIterationHistory(history, context.iteration);\r\n sections.push(historyText);\r\n sections.push('');\r\n } catch (err) {\r\n console.warn(`[agent-prompt-builder] Failed to load iteration history:`, err);\r\n // Continue without history\r\n }\r\n }\r\n\r\n // 4. Agent definition content (from markdown file)\r\n sections.push('## Agent Definition');\r\n sections.push('');\r\n sections.push(definition.content);\r\n sections.push('');\r\n\r\n // 5. CFN Loop protocol (if agent supports it AND task context includes taskId)\r\n if (context.taskId && hasCFNLoopProtocol(definition)) {\r\n sections.push(buildCFNLoopProtocol(context.taskId, agentId));\r\n sections.push('');\r\n }\r\n\r\n // 6. Environment context\r\n const envContext = buildEnvironmentContext(context);\r\n if (envContext) {\r\n sections.push(envContext);\r\n sections.push('');\r\n }\r\n\r\n // 7. Execution instructions\r\n sections.push('## Execution Instructions');\r\n sections.push('');\r\n sections.push('1. Read and understand the task requirements');\r\n if (context.iteration && context.iteration > 1) {\r\n sections.push('2. Review iteration history and feedback from validators');\r\n sections.push('3. Address specific feedback points from previous iteration');\r\n sections.push('4. Execute your core responsibilities as defined above');\r\n } else {\r\n sections.push('2. Execute your core responsibilities as defined above');\r\n sections.push('3. Follow any protocol steps (CFN Loop, validation hooks, etc.)');\r\n }\r\n sections.push('4. Provide clear, concise output');\r\n sections.push('5. Report confidence score if applicable');\r\n sections.push('');\r\n\r\n // 7a. Pre-edit backup requirement (MANDATORY)\r\n sections.push('## Pre-Edit Backup Protocol (MANDATORY)');\r\n sections.push('');\r\n sections.push('**BEFORE ANY Edit/Write/MultiEdit operation, you MUST create a backup:**');\r\n sections.push('');\r\n sections.push('```bash');\r\n sections.push(`BACKUP_PATH=$(./.claude/hooks/cfn-invoke-pre-edit.sh \"$FILE_TO_EDIT\" --agent-id \"${agentId}\")`);\r\n sections.push('```');\r\n sections.push('');\r\n sections.push('**Why:** Enables safe file revert without git operations during parallel sessions.');\r\n sections.push('**Location:** `.backups/[agent-id]/[timestamp]_[hash]/`');\r\n sections.push('**Retention:** 24h TTL (configurable)');\r\n sections.push('');\r\n sections.push('**Complete Edit Workflow:**');\r\n sections.push('```bash');\r\n sections.push('# 1. Pre-Edit: Create backup');\r\n sections.push(`BACKUP_PATH=$(./.claude/hooks/cfn-invoke-pre-edit.sh \"src/file.ts\" --agent-id \"${agentId}\")`);\r\n sections.push('');\r\n sections.push('# 2. Edit: Perform file modification');\r\n sections.push('Edit: file_path=\"src/file.ts\" old_string=\"...\" new_string=\"...\"');\r\n sections.push('');\r\n sections.push('# 3. Post-Edit: Validate changes');\r\n sections.push(`./.claude/hooks/cfn-invoke-post-edit.sh \"src/file.ts\" --agent-id \"${agentId}\"`);\r\n sections.push('```');\r\n sections.push('');\r\n\r\n // 8. Tool reminder\r\n if (definition.tools && definition.tools.length > 0) {\r\n sections.push('## Available Tools');\r\n sections.push('');\r\n sections.push(`You have access to: ${definition.tools.join(', ')}`);\r\n sections.push('');\r\n }\r\n\r\n return sections.join('\\n');\r\n}\r\n\r\n/**\r\n * Extract agent ID from context\r\n * If agentId is explicitly provided in context, use it; otherwise generate from name + iteration\r\n */\r\nexport function getAgentId(definition: AgentDefinition, context: TaskContext): string {\r\n if (context.agentId) {\r\n return context.agentId;\r\n }\r\n return `${definition.name}-${context.iteration || 1}`;\r\n}\r\n\r\n/**\r\n * Build system prompt for agent (optional, for structured agent behavior)\r\n */\r\nexport function buildSystemPrompt(definition: AgentDefinition): string {\r\n return `You are ${definition.name}, a specialized AI agent.\r\n\r\nType: ${definition.type || 'specialist'}\r\nModel: ${definition.model}\r\nTools: ${definition.tools.join(', ')}\r\n\r\nFollow your agent definition exactly and complete assigned tasks with high quality.`;\r\n}\r\n"],"names":["hasCFNLoopProtocol","loadIterationHistory","formatIterationHistory","buildCFNLoopProtocol","taskId","agentId","includes","enrichJSONContext","jsonObj","sections","task","push","files","fileList","split","map","f","trim","filter","Array","isArray","length","forEach","file","requirements","reqs","req","i","deliverables","delivs","deliv","batch","directory","acceptanceCriteria","criteria","criterion","instructions","instrs","instr","join","buildTaskDescription","agentType","context","desc","contextStr","startsWith","endsWith","JSON","parse","e","iteration","mode","priority","parentTaskId","buildEnvironmentContext","env","buildAgentPrompt","definition","name","description","history","historyText","err","console","warn","content","envContext","tools","getAgentId","buildSystemPrompt","type","model"],"mappings":"AAAA;;;;;;;;;CASC,GAED,SAA0BA,kBAAkB,QAAQ,+BAA+B;AACnF,SAASC,oBAAoB,EAAEC,sBAAsB,QAAQ,yBAAyB;AAYtF;;CAEC,GACD,SAASC,qBAAqBC,MAAc,EAAEC,OAAe;IAC3D,OAAO,CAAC;;;;;;;;;;uBAUa,EAAED,OAAO;;;;;;aAMnB,EAAEA,OAAO;;;cAGR,EAAEA,OAAOE,QAAQ,CAAC,eAAe,sBAAsB,IAAI;;;;;;aAM5D,EAAEF,OAAO;;;;;;WAMX,EAAEA,OAAO;YACR,EAAEC,QAAQ;;;;;;;;;;AAUtB,CAAC;AACD;AAEA;;CAEC,GACD,SAASE,kBAAkBC,OAAY;IACrC,MAAMC,WAAqB,EAAE;IAE7B,2BAA2B;IAC3B,IAAID,QAAQE,IAAI,EAAE;QAChBD,SAASE,IAAI,CAAC,CAAC,UAAU,EAAEH,QAAQE,IAAI,EAAE;IAC3C;IAEA,8DAA8D;IAC9D,IAAIF,QAAQI,KAAK,EAAE;QACjB,MAAMC,WAAW,OAAOL,QAAQI,KAAK,KAAK,WACtCJ,QAAQI,KAAK,CAACE,KAAK,CAAC,KAAKC,GAAG,CAACC,CAAAA,IAAKA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,SAASE,IAAI,CAAC;QACdF,SAASE,IAAI,CAAC,CAAC,oBAAoB,EAAE8C,WAAWU,KAAK,CAAC5B,IAAI,CAAC,OAAO;QAClE9B,SAASE,IAAI,CAAC;IAChB;IAEA,OAAOF,SAAS8B,IAAI,CAAC;AACvB;AAEA;;;CAGC,GACD,OAAO,SAAS6B,WAAWX,UAA2B,EAAEf,OAAoB;IAC1E,IAAIA,QAAQrC,OAAO,EAAE;QACnB,OAAOqC,QAAQrC,OAAO;IACxB;IACA,OAAO,GAAGoD,WAAWC,IAAI,CAAC,CAAC,EAAEhB,QAAQQ,SAAS,IAAI,GAAG;AACvD;AAEA;;CAEC,GACD,OAAO,SAASmB,kBAAkBZ,UAA2B;IAC3D,OAAO,CAAC,QAAQ,EAAEA,WAAWC,IAAI,CAAC;;MAE9B,EAAED,WAAWa,IAAI,IAAI,aAAa;OACjC,EAAEb,WAAWc,KAAK,CAAC;OACnB,EAAEd,WAAWU,KAAK,CAAC5B,IAAI,CAAC,MAAM;;mFAE8C,CAAC;AACpF"}
|
|
1
|
+
{"version":3,"sources":["../../src/cli/agent-prompt-builder.ts"],"sourcesContent":["/**\r\n * Agent Prompt Builder\r\n *\r\n * Builds comprehensive prompts for CLI-spawned agents by combining:\r\n * - Agent definition (YAML + markdown)\r\n * - Task context (taskId, iteration, mode)\r\n * - CFN Loop protocol (if applicable)\r\n * - Iteration history (Sprint 3 - Phase 2)\r\n * - Environment variables\r\n */\r\n\r\nimport { AgentDefinition, hasCFNLoopProtocol } from './agent-definition-parser.js';\r\nimport { loadIterationHistory, formatIterationHistory } from './iteration-history.js';\r\n\r\nexport interface TaskContext {\r\n taskId?: string;\r\n iteration?: number;\r\n context?: string;\r\n mode?: string;\r\n priority?: number;\r\n parentTaskId?: string;\r\n agentId?: string;\r\n}\r\n\r\n/**\r\n * Build CFN Loop protocol instructions\r\n */\r\nfunction buildCFNLoopProtocol(taskId: string, agentId: string): string {\r\n return `\r\n## CFN Loop Redis Completion Protocol\r\n\r\nYou are participating in a CFN Loop workflow. Follow this protocol EXACTLY:\r\n\r\n### Step 1: Complete Your Work\r\nExecute your assigned task (implementation, review, testing, etc.)\r\n\r\n### Step 2: Calculate Confidence Score\r\nAssess your work quality and assign a confidence score (0.0-1.0):\r\n- 0.90-1.0: Excellent, production-ready\r\n- 0.75-0.89: Good, minor issues possible\r\n- 0.50-0.74: Acceptable, needs review\r\n- 0.0-0.49: Poor, significant issues\r\n\r\n### Step 3: Report Completion to Redis\r\n\\`\\`\\`bash\r\n./.claude/skills/cfn-redis-coordination/report-completion.sh \\\\\r\n --task-id \"${taskId}\" \\\\\r\n --agent-id \"${agentId}\" \\\\\r\n --confidence [YOUR_CONFIDENCE_SCORE] \\\\\r\n --iteration \\${ITERATION:-1}\r\n\\`\\`\\`\r\n\r\nThis script automatically:\r\n- Signals completion via \\`swarm:${taskId}:${agentId}:done\\`\r\n- Stores confidence in \\`swarm:${taskId}:${agentId}:confidence\\`\r\n- Creates result hash in \\`swarm:${taskId}:${agentId}:result\\`\r\n- Updates SQLite persistence layer\r\n\r\n### Step 4: Exit Cleanly\r\nAfter reporting, exit immediately. DO NOT enter waiting mode.\r\n\r\nThe orchestrator will:\r\n- Collect confidence scores from all agents\r\n- Run gate check (≥0.75 threshold)\r\n- Spawn validators if gate passes\r\n- Spawn fresh agents for iteration N+1 if needed\r\n\r\n**Environment Variables Available:**\r\n- TASK_ID: ${taskId}\r\n- AGENT_ID: ${agentId}\r\n- ITERATION: Current iteration number (default: 1)\r\n- CONFIDENCE_SCORE: Your final confidence assessment\r\n\r\n**Why This Matters:**\r\n- Enables zero-token coordination (orchestrator uses Redis BLPOP)\r\n- Supports adaptive agent specialization (spawn different specialist for iteration N+1)\r\n- Prevents memory leaks (agents exit after reporting)\r\n- Confidence scores drive gate checks and consensus validation\r\n\r\n**CRITICAL:** Report completion before exiting. Orchestrator is waiting for your signal.\r\n`;\r\n}\r\n\r\n/**\r\n * Parse and enrich JSON context into natural language instructions\r\n */\r\nfunction enrichJSONContext(jsonObj: any): string {\r\n const sections: string[] = [];\r\n\r\n // Extract task description\r\n if (jsonObj.task) {\r\n sections.push(`**Task:** ${jsonObj.task}`);\r\n }\r\n\r\n // Parse files - convert comma-separated string to bullet list\r\n if (jsonObj.files) {\r\n const fileList = typeof jsonObj.files === 'string'\r\n ? jsonObj.files.split(',').map(f => f.trim()).filter(f => f)\r\n : Array.isArray(jsonObj.files) ? jsonObj.files : [];\r\n\r\n if (fileList.length > 0) {\r\n sections.push('\\n**Files to process:**');\r\n fileList.forEach(file => sections.push(`- ${file}`));\r\n }\r\n }\r\n\r\n // Add requirements/deliverables\r\n if (jsonObj.requirements) {\r\n const reqs = Array.isArray(jsonObj.requirements) ? jsonObj.requirements : [jsonObj.requirements];\r\n sections.push('\\n**Requirements:**');\r\n reqs.forEach((req, i) => sections.push(`${i + 1}. ${req}`));\r\n }\r\n\r\n if (jsonObj.deliverables) {\r\n const delivs = Array.isArray(jsonObj.deliverables) ? jsonObj.deliverables : [jsonObj.deliverables];\r\n sections.push('\\n**Deliverables:**');\r\n delivs.forEach(deliv => sections.push(`- ${deliv}`));\r\n }\r\n\r\n // Add batch information\r\n if (jsonObj.batch) {\r\n sections.push(`\\n**Batch:** ${jsonObj.batch}`);\r\n }\r\n\r\n // Add directory context\r\n if (jsonObj.directory) {\r\n sections.push(`\\n**Working Directory:** ${jsonObj.directory}`);\r\n }\r\n\r\n // Add acceptance criteria\r\n if (jsonObj.acceptanceCriteria) {\r\n const criteria = Array.isArray(jsonObj.acceptanceCriteria)\r\n ? jsonObj.acceptanceCriteria\r\n : [jsonObj.acceptanceCriteria];\r\n sections.push('\\n**Acceptance Criteria:**');\r\n criteria.forEach(criterion => sections.push(`- ${criterion}`));\r\n }\r\n\r\n // Add explicit instructions if present\r\n if (jsonObj.instructions) {\r\n sections.push('\\n**Instructions:**');\r\n const instrs = Array.isArray(jsonObj.instructions) ? jsonObj.instructions : [jsonObj.instructions];\r\n instrs.forEach((instr, i) => sections.push(`${i + 1}. ${instr}`));\r\n }\r\n\r\n return sections.join('\\n');\r\n}\r\n\r\n/**\r\n * Build task description from context\r\n */\r\nfunction buildTaskDescription(agentType: string, context: TaskContext): string {\r\n let desc = '';\r\n\r\n if (context.context) {\r\n // Try to parse as JSON first\r\n let contextStr = context.context.trim();\r\n\r\n // Check if context looks like JSON\r\n if ((contextStr.startsWith('{') && contextStr.endsWith('}')) ||\r\n (contextStr.startsWith('[') && contextStr.endsWith(']'))) {\r\n try {\r\n const jsonObj = JSON.parse(contextStr);\r\n desc = enrichJSONContext(jsonObj);\r\n\r\n // Add instruction footer for structured tasks\r\n if (jsonObj.files || jsonObj.deliverables) {\r\n desc += '\\n\\n**Process each item systematically and report confidence when complete.**';\r\n }\r\n } catch (e) {\r\n // Not valid JSON, treat as plain text\r\n desc = context.context;\r\n }\r\n } else {\r\n // Plain text context\r\n desc = context.context;\r\n }\r\n } else {\r\n desc = `Execute task as ${agentType} agent`;\r\n }\r\n\r\n // Add metadata fields\r\n if (context.taskId) {\r\n desc += `\\n\\n**Task ID:** ${context.taskId}`;\r\n }\r\n\r\n if (context.iteration) {\r\n desc += `\\n**Iteration:** ${context.iteration}`;\r\n }\r\n\r\n if (context.mode) {\r\n desc += `\\n**Mode:** ${context.mode}`;\r\n }\r\n\r\n if (context.priority) {\r\n desc += `\\n**Priority:** ${context.priority}`;\r\n }\r\n\r\n if (context.parentTaskId) {\r\n desc += `\\n**Parent Task:** ${context.parentTaskId}`;\r\n }\r\n\r\n return desc;\r\n}\r\n\r\n/**\r\n * Build environment context section\r\n */\r\nfunction buildEnvironmentContext(context: TaskContext): string {\r\n const env: string[] = [];\r\n\r\n if (context.taskId) env.push(`TASK_ID=${context.taskId}`);\r\n if (context.iteration) env.push(`ITERATION=${context.iteration}`);\r\n if (context.mode) env.push(`MODE=${context.mode}`);\r\n if (context.priority) env.push(`PRIORITY=${context.priority}`);\r\n if (context.parentTaskId) env.push(`PARENT_TASK_ID=${context.parentTaskId}`);\r\n\r\n if (env.length === 0) return '';\r\n\r\n return `\r\n## Environment Variables\r\n\r\n\\`\\`\\`bash\r\n${env.join('\\n')}\r\n\\`\\`\\`\r\n`;\r\n}\r\n\r\n/**\r\n * Build complete prompt for agent execution (async for iteration history)\r\n */\r\nexport async function buildAgentPrompt(\r\n definition: AgentDefinition,\r\n context: TaskContext\r\n): Promise<string> {\r\n // Use explicit agent ID if provided, otherwise generate from name + iteration\r\n const agentId = context.agentId || `${definition.name}-${context.iteration || 1}`;\r\n\r\n const sections: string[] = [];\r\n\r\n // 1. Agent definition header\r\n sections.push(`# Agent: ${definition.name}`);\r\n sections.push('');\r\n sections.push(definition.description);\r\n sections.push('');\r\n\r\n // 2. Task description\r\n sections.push('## Task');\r\n sections.push('');\r\n sections.push(buildTaskDescription(definition.name, context));\r\n sections.push('');\r\n\r\n // 3. Iteration history (Sprint 3 - Phase 2)\r\n // Load and format previous iterations if iteration > 1\r\n if (context.taskId && context.iteration && context.iteration > 1) {\r\n try {\r\n const history = await loadIterationHistory(context.taskId, agentId, context.iteration);\r\n const historyText = formatIterationHistory(history, context.iteration);\r\n sections.push(historyText);\r\n sections.push('');\r\n } catch (err) {\r\n console.warn(`[agent-prompt-builder] Failed to load iteration history:`, err);\r\n // Continue without history\r\n }\r\n }\r\n\r\n // 4. Agent definition content (from markdown file)\r\n sections.push('## Agent Definition');\r\n sections.push('');\r\n sections.push(definition.content);\r\n sections.push('');\r\n\r\n // 5. CFN Loop protocol (ALWAYS inject when taskId present - enables Redis coordination)\r\n if (context.taskId && agentId) {\r\n sections.push(buildCFNLoopProtocol(context.taskId, agentId));\r\n sections.push('');\r\n }\r\n\r\n // 6. Environment context\r\n const envContext = buildEnvironmentContext(context);\r\n if (envContext) {\r\n sections.push(envContext);\r\n sections.push('');\r\n }\r\n\r\n // 7. Execution instructions\r\n sections.push('## Execution Instructions');\r\n sections.push('');\r\n sections.push('1. Read and understand the task requirements');\r\n if (context.iteration && context.iteration > 1) {\r\n sections.push('2. Review iteration history and feedback from validators');\r\n sections.push('3. Address specific feedback points from previous iteration');\r\n sections.push('4. Execute your core responsibilities as defined above');\r\n } else {\r\n sections.push('2. Execute your core responsibilities as defined above');\r\n sections.push('3. Follow any protocol steps (CFN Loop, validation hooks, etc.)');\r\n }\r\n sections.push('4. Provide clear, concise output');\r\n sections.push('5. Report confidence score if applicable');\r\n sections.push('');\r\n\r\n // 7a. Pre-edit backup requirement (MANDATORY)\r\n sections.push('## Pre-Edit Backup Protocol (MANDATORY)');\r\n sections.push('');\r\n sections.push('**BEFORE ANY Edit/Write/MultiEdit operation, you MUST create a backup:**');\r\n sections.push('');\r\n sections.push('```bash');\r\n sections.push(`BACKUP_PATH=$(./.claude/hooks/cfn-invoke-pre-edit.sh \"$FILE_TO_EDIT\" --agent-id \"${agentId}\")`);\r\n sections.push('```');\r\n sections.push('');\r\n sections.push('**Why:** Enables safe file revert without git operations during parallel sessions.');\r\n sections.push('**Location:** `.backups/[agent-id]/[timestamp]_[hash]/`');\r\n sections.push('**Retention:** 24h TTL (configurable)');\r\n sections.push('');\r\n sections.push('**Complete Edit Workflow:**');\r\n sections.push('```bash');\r\n sections.push('# 1. Pre-Edit: Create backup');\r\n sections.push(`BACKUP_PATH=$(./.claude/hooks/cfn-invoke-pre-edit.sh \"src/file.ts\" --agent-id \"${agentId}\")`);\r\n sections.push('');\r\n sections.push('# 2. Edit: Perform file modification');\r\n sections.push('Edit: file_path=\"src/file.ts\" old_string=\"...\" new_string=\"...\"');\r\n sections.push('');\r\n sections.push('# 3. Post-Edit: Validate changes');\r\n sections.push(`./.claude/hooks/cfn-invoke-post-edit.sh \"src/file.ts\" --agent-id \"${agentId}\"`);\r\n sections.push('```');\r\n sections.push('');\r\n\r\n // 8. Tool reminder\r\n if (definition.tools && definition.tools.length > 0) {\r\n sections.push('## Available Tools');\r\n sections.push('');\r\n sections.push(`You have access to: ${definition.tools.join(', ')}`);\r\n sections.push('');\r\n }\r\n\r\n return sections.join('\\n');\r\n}\r\n\r\n/**\r\n * Extract agent ID from context\r\n * If agentId is explicitly provided in context, use it; otherwise generate from name + iteration\r\n */\r\nexport function getAgentId(definition: AgentDefinition, context: TaskContext): string {\r\n if (context.agentId) {\r\n return context.agentId;\r\n }\r\n return `${definition.name}-${context.iteration || 1}`;\r\n}\r\n\r\n/**\r\n * Build system prompt for agent (optional, for structured agent behavior)\r\n */\r\nexport function buildSystemPrompt(definition: AgentDefinition): string {\r\n return `You are ${definition.name}, a specialized AI agent.\r\n\r\nType: ${definition.type || 'specialist'}\r\nModel: ${definition.model}\r\nTools: ${definition.tools.join(', ')}\r\n\r\nFollow your agent definition exactly and complete assigned tasks with high quality.`;\r\n}\r\n"],"names":["loadIterationHistory","formatIterationHistory","buildCFNLoopProtocol","taskId","agentId","enrichJSONContext","jsonObj","sections","task","push","files","fileList","split","map","f","trim","filter","Array","isArray","length","forEach","file","requirements","reqs","req","i","deliverables","delivs","deliv","batch","directory","acceptanceCriteria","criteria","criterion","instructions","instrs","instr","join","buildTaskDescription","agentType","context","desc","contextStr","startsWith","endsWith","JSON","parse","e","iteration","mode","priority","parentTaskId","buildEnvironmentContext","env","buildAgentPrompt","definition","name","description","history","historyText","err","console","warn","content","envContext","tools","getAgentId","buildSystemPrompt","type","model"],"mappings":"AAAA;;;;;;;;;CASC,GAGD,SAASA,oBAAoB,EAAEC,sBAAsB,QAAQ,yBAAyB;AAYtF;;CAEC,GACD,SAASC,qBAAqBC,MAAc,EAAEC,OAAe;IAC3D,OAAO,CAAC;;;;;;;;;;;;;;;;;;aAkBG,EAAED,OAAO;cACR,EAAEC,QAAQ;;;;;;iCAMS,EAAED,OAAO,CAAC,EAAEC,QAAQ;+BACtB,EAAED,OAAO,CAAC,EAAEC,QAAQ;iCAClB,EAAED,OAAO,CAAC,EAAEC,QAAQ;;;;;;;;;;;;;WAa1C,EAAED,OAAO;YACR,EAAEC,QAAQ;;;;;;;;;;;AAWtB,CAAC;AACD;AAEA;;CAEC,GACD,SAASC,kBAAkBC,OAAY;IACrC,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