{
  "name": "Vorlux AI | Agent Learning Loop",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "rule": { "interval": [{ "field": "days", "daysInterval": 1 }] }
      },
      "id": "schedule-trigger",
      "name": "Daily Schedule",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [220, 300]
    },
    {
      "parameters": {
        "url": "http://host.docker.internal:3010/api/orchestrator/traces",
        "method": "GET",
        "qs": { "period": "24h", "status": "all" },
        "options": { "timeout": 15000 }
      },
      "id": "fetch-traces",
      "name": "Fetch Recent Traces",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [460, 300]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const traces = $input.first().json.data || $input.first().json.traces || [];\nconst successes = traces.filter(t => t.status === 'completed' || t.status === 'success');\nconst failures = traces.filter(t => t.status === 'failed' || t.status === 'error');\n\nconst patterns = {};\nfailures.forEach(f => {\n  const key = f.agent_id + ':' + (f.error_type || 'unknown');\n  if (!patterns[key]) patterns[key] = { agent_id: f.agent_id, error_type: f.error_type || 'unknown', count: 0, examples: [] };\n  patterns[key].count++;\n  if (patterns[key].examples.length < 3) patterns[key].examples.push(f.error_message || f.message);\n});\n\nconst successPatterns = {};\nsuccesses.forEach(s => {\n  const key = s.agent_id + ':' + (s.task_type || 'general');\n  if (!successPatterns[key]) successPatterns[key] = { agent_id: s.agent_id, task_type: s.task_type || 'general', count: 0 };\n  successPatterns[key].count++;\n});\n\nconst recommendations = Object.values(patterns).filter(p => p.count >= 2).map(p => ({\n  agent_id: p.agent_id,\n  recommendation: `Agent ${p.agent_id} failed ${p.count} times with ${p.error_type}. Consider updating prompt or adding fallback.`,\n  priority: p.count >= 5 ? 'high' : 'medium'\n}));\n\nreturn [{ json: { totalTraces: traces.length, successes: successes.length, failures: failures.length, failurePatterns: Object.values(patterns), recommendations, generatedAt: new Date().toISOString() } }];"
      },
      "id": "analyze-patterns",
      "name": "Analyze Success/Failure Patterns",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [700, 300]
    },
    {
      "parameters": {
        "url": "http://host.docker.internal:3010/api/memory/log",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ type: 'learning-loop', data: { recommendations: $json.recommendations, patterns: $json.failurePatterns }, agent: 'system' }) }}",
        "options": { "timeout": 10000 }
      },
      "id": "save-learnings",
      "name": "Save Learnings to Memory",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [960, 200]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.DISCORD_OPS_WEBHOOK}}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ content: '🧠 **Agent Learning Loop**\\nTraces analyzed: ' + $json.totalTraces + '\\nSuccess: ' + $json.successes + ' | Failures: ' + $json.failures + '\\nRecommendations: ' + $json.recommendations.length }) }}"
      },
      "id": "discord-notify",
      "name": "Discord Notify",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [960, 400]
    }
  ],
  "connections": {
    "Daily Schedule": { "main": [[{ "node": "Fetch Recent Traces", "type": "main", "index": 0 }]] },
    "Fetch Recent Traces": { "main": [[{ "node": "Analyze Success/Failure Patterns", "type": "main", "index": 0 }]] },
    "Analyze Success/Failure Patterns": { "main": [[{ "node": "Save Learnings to Memory", "type": "main", "index": 0 }, { "node": "Discord Notify", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" }
}
