{
  "name": "WF-300 Context Engineering Pack Canonical",
  "description": "Runs context engineering lane: execution context \u2192 platform packaging \u2192 asset briefs \u2192 lineage validation.",
  "nodes": [
    {
      "parameters": {},
      "id": "wf300_trigger_node",
      "name": "Trigger Node",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { ...$json, dossier_context: { dossier_id: $json.dossier_id || 'DOSSIER-UNSPECIFIED', pack: 'WF-300', loaded_at: new Date().toISOString(), route: $json.route_id || 'ROUTE_PHASE1_STANDARD', script_finalized: !!$json.final_script_packet } } }];"
      },
      "id": "wf300_dossier_load_node",
      "name": "Dossier Load Node",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        420,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { ...$json, pack_context: { child_workflows: ['CWF-310-execution-context-builder', 'CWF-320-platform-packager', 'CWF-330-asset-brief-generator', 'CWF-340-lineage-validator'], primary_director: 'Krishna', strategic_authority: 'Krishna', governance_authority: ['Yama', 'Kubera'] } } }];"
      },
      "id": "wf300_pack_initialization_node",
      "name": "Pack Initialization Node",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        640,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const childWorkflows = ['CWF-310', 'CWF-320', 'CWF-330', 'CWF-340'];\nlet executionPlan = { workflows_to_execute: childWorkflows, current_index: 0, status: 'PLANNING', lineage_validation_required: true };\nreturn [{ json: { ...$json, execution_plan: executionPlan, pack_orchestration: { started_at: new Date().toISOString(), expected_duration_seconds: 150 } } }];"
      },
      "id": "wf300_execution_planner_node",
      "name": "Execution Planner Node",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        860,
        300
      ]
    },
    {
      "parameters": {
        "source": "database",
        "workflowId": {
          "__rl": true,
          "value": "zCoztycz5rNiHhkq",
          "mode": "id",
          "cachedResultName": "CWF-310 Execution Context Builder Canonical"
        },
        "mode": "each",
        "options": {
          "waitForSubWorkflow": true
        }
      },
      "id": "wf300_execute_cwf310_node",
      "name": "Execute CWF-310 Execution Context Builder",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.2,
      "position": [
        1080,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Capture cwf310_result after sub-workflow CWF-310\nconst subOutput = $json || {};\nlet parentCtx = {};\ntry { parentCtx = $('Execution Planner Node').first().json || {}; } catch(e) {}\nconst acc = { ...parentCtx };\n\nacc[\"cwf310_result\"] = {\n  status: subOutput.status || 'SUCCESS',\n  payload: subOutput.payload || subOutput,\n  captured_at: new Date().toISOString()\n};\nreturn [{ json: acc }];"
      },
      "id": "wf300_capture_cwf310_node",
      "name": "Capture CWF-310 Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1330,
        300
      ]
    },
    {
      "parameters": {
        "source": "database",
        "workflowId": {
          "__rl": true,
          "value": "v1Pla3fDXsirQQlz",
          "mode": "id",
          "cachedResultName": "CWF-320 Platform Packager Canonical"
        },
        "mode": "each",
        "options": {
          "waitForSubWorkflow": true
        }
      },
      "id": "wf300_execute_cwf320_node",
      "name": "Execute CWF-320 Platform Packager",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.2,
      "position": [
        1580,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Capture cwf320_result after sub-workflow CWF-320\nconst subOutput = $json || {};\nlet parentCtx = {};\ntry { parentCtx = $('Execution Planner Node').first().json || {}; } catch(e) {}\nconst acc = { ...parentCtx };\ntry { const c = $('Capture CWF-310 Result').first(); if (c) acc[\"cwf310_result\"] = c.json[\"cwf310_result\"] || c.json; } catch(e) {}\nacc[\"cwf320_result\"] = {\n  status: subOutput.status || 'SUCCESS',\n  payload: subOutput.payload || subOutput,\n  captured_at: new Date().toISOString()\n};\nreturn [{ json: acc }];"
      },
      "id": "wf300_capture_cwf320_node",
      "name": "Capture CWF-320 Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1830,
        300
      ]
    },
    {
      "parameters": {
        "source": "database",
        "workflowId": {
          "__rl": true,
          "value": "8PfhvzgGMA9SJkxf",
          "mode": "id",
          "cachedResultName": "CWF-330 Asset Brief Generator Canonical"
        },
        "mode": "each",
        "options": {
          "waitForSubWorkflow": true
        }
      },
      "id": "wf300_execute_cwf330_node",
      "name": "Execute CWF-330 Asset Brief Generator",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.2,
      "position": [
        2080,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Capture cwf330_result after sub-workflow CWF-330\nconst subOutput = $json || {};\nlet parentCtx = {};\ntry { parentCtx = $('Execution Planner Node').first().json || {}; } catch(e) {}\nconst acc = { ...parentCtx };\ntry { const c = $('Capture CWF-310 Result').first(); if (c) acc[\"cwf310_result\"] = c.json[\"cwf310_result\"] || c.json; } catch(e) {}\n  try { const c = $('Capture CWF-320 Result').first(); if (c) acc[\"cwf320_result\"] = c.json[\"cwf320_result\"] || c.json; } catch(e) {}\nacc[\"cwf330_result\"] = {\n  status: subOutput.status || 'SUCCESS',\n  payload: subOutput.payload || subOutput,\n  captured_at: new Date().toISOString()\n};\nreturn [{ json: acc }];"
      },
      "id": "wf300_capture_cwf330_node",
      "name": "Capture CWF-330 Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2330,
        300
      ]
    },
    {
      "parameters": {
        "source": "database",
        "workflowId": {
          "__rl": true,
          "value": "12i773gCwdlprMSF",
          "mode": "id",
          "cachedResultName": "CWF-340 Lineage Validator Canonical"
        },
        "mode": "each",
        "options": {
          "waitForSubWorkflow": true
        }
      },
      "id": "wf300_execute_cwf340_node",
      "name": "Execute CWF-340 Lineage Validator",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.2,
      "position": [
        2580,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Capture cwf340_result after sub-workflow CWF-340\nconst subOutput = $json || {};\nlet parentCtx = {};\ntry { parentCtx = $('Execution Planner Node').first().json || {}; } catch(e) {}\nconst acc = { ...parentCtx };\ntry { const c = $('Capture CWF-310 Result').first(); if (c) acc[\"cwf310_result\"] = c.json[\"cwf310_result\"] || c.json; } catch(e) {}\n  try { const c = $('Capture CWF-320 Result').first(); if (c) acc[\"cwf320_result\"] = c.json[\"cwf320_result\"] || c.json; } catch(e) {}\n  try { const c = $('Capture CWF-330 Result').first(); if (c) acc[\"cwf330_result\"] = c.json[\"cwf330_result\"] || c.json; } catch(e) {}\nacc[\"cwf340_result\"] = {\n  status: subOutput.status || 'SUCCESS',\n  payload: subOutput.payload || subOutput,\n  captured_at: new Date().toISOString()\n};\nreturn [{ json: acc }];"
      },
      "id": "wf300_capture_cwf340_node",
      "name": "Capture CWF-340 Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2830,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Phase-2 chain validation: pass if all children returned SUCCESS/COMPLETED.\nconst childResults = {\n  CWF_310: $json.cwf310_result,\n  CWF_320: $json.cwf320_result,\n  CWF_330: $json.cwf330_result,\n  CWF_340: $json.cwf340_result\n};\nconst allHealthy = Object.values(childResults).every(r => r && (r.status === 'SUCCESS' || r.status === 'COMPLETED'));\nconst validationOk = allHealthy;\nreturn [{ json: { ...$json, pack_validation: { child_results_aggregated: childResults, all_children_healthy: allHealthy, validation_ok: validationOk, route_to_error: !validationOk, phase2_acceptance: 'chain_healthy_required' } } }];"
      },
      "id": "wf300_pack_validation_node",
      "name": "Pack Validation Node",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        3180,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "if ($json.pack_validation.route_to_error) return [];\nreturn [{\n  json: {\n    ...$json,\n    dossier_write: {\n      mutation_type: 'namespace_update',\n      target: 'dossier.context',\n      timestamp: new Date().toISOString(),\n      writer_id: 'WF-300',\n      pack_context: $json.pack_context,\n      child_results: $json.pack_validation.child_results_aggregated,\n      audit_entry: 'context-engineering-pack-completion from WF-300'\n    }\n  }\n}];"
      },
      "id": "wf300_dossier_write_node",
      "name": "Dossier Context Write Node",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        3430,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "if ($json.pack_validation.route_to_error) return [];\nreturn [{\n  json: {\n    ...$json,\n    se_route_runs_write: {\n      operation: 'record_pack_execution',\n      pack_id: 'WF-300',\n      dossier_id: $json.dossier_id,\n      child_workflows_executed: ['CWF-310', 'CWF-320', 'CWF-330', 'CWF-340'],\n      execution_status: 'COMPLETED',\n      context_built: $json.pack_validation.context_built,\n      lineage_validated: $json.pack_validation.lineage_validated,\n      timestamp: new Date().toISOString()\n    }\n  }\n}];"
      },
      "id": "wf300_route_runs_write_node",
      "name": "se_route_runs Write Node",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        3680,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "if ($json.pack_validation.route_to_error) return [];\nconst fs = require('fs');\nconst path = require('path');\n\nconst now = new Date().toISOString();\nconst completion_packet = {\n  instance_id: 'WF-300-COMP-' + Date.now(),\n  artifact_family: 'context_engineering_result',\n  schema_version: '1.0.0',\n  producer_workflow: 'WF-300',\n  dossier_ref: $json.dossier_id,\n  created_at: now,\n  status: 'CREATED',\n  payload: {\n    pack_executed: 'WF-300',\n    context_built: $json.pack_validation.context_built,\n        lineage_validated: $json.pack_validation.lineage_validated,\n    next_workflow: 'WF-400',\n    child_packets: [\"CWF-310_output\",\"CWF-320_output\",\"CWF-330_output\",\"CWF-340_output\"]\n  }\n};\n\nconst repoRoot = 'C:/ShadowEmpire-Git';\nconst dataDir = path.join(repoRoot, 'data');\nconst packetIndexPath = path.join(dataDir, 'se_packet_index.json');\nif (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });\n\nlet index = { entries: [], total_entries: 0, last_updated: now };\nif (fs.existsSync(packetIndexPath)) {\n  const parsed = JSON.parse(fs.readFileSync(packetIndexPath, 'utf8'));\n  index = {\n    ...index,\n    ...parsed,\n    entries: Array.isArray(parsed.entries)\n      ? parsed.entries\n      : (Array.isArray(parsed.packets) ? parsed.packets : [])\n  };\n}\n\nindex.entries.push({\n  packet_id: completion_packet.instance_id,\n  instance_id: completion_packet.instance_id,\n  artifact_family: completion_packet.artifact_family,\n  schema_version: completion_packet.schema_version,\n  producer_workflow: completion_packet.producer_workflow,\n  dossier_ref: completion_packet.dossier_ref,\n  created_at: completion_packet.created_at,\n  status: completion_packet.status,\n  payload: completion_packet.payload\n});\nindex.total_entries = index.entries.length;\nindex.last_updated = now;\n\nfs.writeFileSync(packetIndexPath, JSON.stringify(index, null, 2), 'utf8');\n\nreturn [{\n  json: {\n    workflow_id: 'WF-300',\n    execution_status: 'SUCCESS',\n    completion_packet,\n    packet_persistence: {\n      packet_index_path: packetIndexPath,\n      packet_entries: index.entries.length,\n      persisted_at: now\n    }\n  }\n}];"
      },
      "id": "wf300_completion_packet_emission_node",
      "name": "Completion Packet Emission Node",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        3930,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "if (!$json.pack_validation.route_to_error) return [];\nreturn [{\n  json: {\n    workflow_id: 'WF-300',\n    routing_decision: 'ERROR',\n    route_to_workflow: 'WF-900',\n    error_message: 'Pack validation failed or lineage validation failed',\n    failure_context: $json.pack_validation,\n    child_results: $json.pack_validation.child_results_aggregated\n  }\n}];"
      },
      "id": "wf300_error_routing_node",
      "name": "Error Routing WF-900 Node",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        3430,
        500
      ]
    }
  ],
  "connections": {
    "Trigger Node": {
      "main": [
        [
          {
            "node": "Dossier Load Node",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dossier Load Node": {
      "main": [
        [
          {
            "node": "Pack Initialization Node",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pack Initialization Node": {
      "main": [
        [
          {
            "node": "Execution Planner Node",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execution Planner Node": {
      "main": [
        [
          {
            "node": "Execute CWF-310 Execution Context Builder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute CWF-310 Execution Context Builder": {
      "main": [
        [
          {
            "node": "Capture CWF-310 Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Capture CWF-310 Result": {
      "main": [
        [
          {
            "node": "Execute CWF-320 Platform Packager",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute CWF-320 Platform Packager": {
      "main": [
        [
          {
            "node": "Capture CWF-320 Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Capture CWF-320 Result": {
      "main": [
        [
          {
            "node": "Execute CWF-330 Asset Brief Generator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute CWF-330 Asset Brief Generator": {
      "main": [
        [
          {
            "node": "Capture CWF-330 Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Capture CWF-330 Result": {
      "main": [
        [
          {
            "node": "Execute CWF-340 Lineage Validator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute CWF-340 Lineage Validator": {
      "main": [
        [
          {
            "node": "Capture CWF-340 Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Capture CWF-340 Result": {
      "main": [
        [
          {
            "node": "Pack Validation Node",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pack Validation Node": {
      "main": [
        [
          {
            "node": "Dossier Context Write Node",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Error Routing WF-900 Node",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dossier Context Write Node": {
      "main": [
        [
          {
            "node": "se_route_runs Write Node",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "se_route_runs Write Node": {
      "main": [
        [
          {
            "node": "Completion Packet Emission Node",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Completion Packet Emission Node": {
      "main": [
        []
      ]
    },
    "Error Routing WF-900 Node": {
      "main": [
        []
      ]
    }
  },
  "meta": {
    "workflow_id": "WF-300",
    "phase": "phase1",
    "vein": "context_vein",
    "purpose": "Context engineering pack parent.",
    "implementation_depth": "production_grade",
    "owner_director": "narada",
    "next_workflow": "WF-020",
    "canonical_file": "n8n/workflows/WF-300.json",
    "wf900_error_route": "WF-900",
    "phase2_chaining": {
      "applied_at": "2026-04-29",
      "pattern": "children_first_then_validation",
      "pack_id": "WF300",
      "child_workflow_invocations": [
        {
          "name": "CWF-310 Execution Context Builder Canonical",
          "live_id": "zCoztycz5rNiHhkq",
          "capture_field": "cwf310_result"
        },
        {
          "name": "CWF-320 Platform Packager Canonical",
          "live_id": "v1Pla3fDXsirQQlz",
          "capture_field": "cwf320_result"
        },
        {
          "name": "CWF-330 Asset Brief Generator Canonical",
          "live_id": "8PfhvzgGMA9SJkxf",
          "capture_field": "cwf330_result"
        },
        {
          "name": "CWF-340 Lineage Validator Canonical",
          "live_id": "12i773gCwdlprMSF",
          "capture_field": "cwf340_result"
        }
      ],
      "notes": "Chain runs all children BEFORE Pack Validation. Capture nodes preserve sub-workflow outputs as cwfXXX_result fields."
    }
  },
  "active": true
}