{
  "name": "AA Factory 04 - Create Target Execution",
  "nodes": [
    {
      "parameters": {},
      "id": "cc8bc3ec-2389-4268-ab70-9d36898fd293",
      "name": "Sub-workflow Input",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const input = $json ?? {};\nconst now = new Date().toISOString();\nconst obs = input.observability ?? {};\nconst trace = Array.isArray(obs.trace) ? [...obs.trace] : [];\nconst startedAt = obs.stage_started_at ?? obs.started_at ?? now;\nconst durationMs = Math.max(0, Date.parse(now) - Date.parse(startedAt));\nconst event = {\n  timestamp: now,\n  correlation_id: obs.correlation_id ?? input.correlation_id ?? null,\n  workflow: $workflow.name,\n  execution_id: $execution.id,\n  stage: 'target-execution-create',\n  status: 'started',\n  duration_ms: durationMs,\n  ...({}),\n};\ntrace.push(event);\nconsole.log(JSON.stringify({event:'autonomous_agent_workflow_stage', ...event}));\nreturn [{json:{...input, observability:{...obs, stage_started_at:now, trace}, last_event:event}}];"
      },
      "id": "d75949e5-3135-41a6-b01c-93265fb522ce",
      "name": "Start Target Execution Trace",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        0
      ]
    },
    {
      "parameters": {
        "task": "={{ $('Sub-workflow Input').item.json.blueprint.execution.task }}",
        "mode": "={{ $('Sub-workflow Input').item.json.blueprint.execution.mode }}",
        "contextInputMode": "json",
        "contextJson": "={{ JSON.stringify({...$('Sub-workflow Input').item.json.blueprint.execution.context, input_schema:$('Sub-workflow Input').item.json.blueprint.execution.input_schema}) }}",
        "includeDesiredOutput": true,
        "desiredOutputSchema": "={{ JSON.stringify($('Sub-workflow Input').item.json.blueprint.execution.output_schema) }}",
        "desiredOutputMediaType": "application/json",
        "includeConstraints": true,
        "deadlineSeconds": 600,
        "maximumAgentCalls": "={{ $('Sub-workflow Input').item.json.blueprint.execution.maximum_agent_calls }}",
        "maximumToolCalls": "={{ $('Sub-workflow Input').item.json.blueprint.execution.maximum_tool_calls }}",
        "allowedCapabilities": "={{ $('Sub-workflow Input').item.json.blueprint.allowed_capabilities.join(',') }}",
        "allowedTools": "={{ $('Sub-workflow Input').item.json.blueprint.allowed_tools.join(',') }}",
        "requireApprovalFor": "={{ $('Sub-workflow Input').item.json.blueprint.execution.require_approval_for.join(',') }}",
        "metadataInputMode": "json",
        "metadataJson": "={{ JSON.stringify({workflow:'autonomous-agent-workflow-factory', planner_execution_id:$('Sub-workflow Input').item.json.plannerExecutionId, generated_configuration:true}) }}",
        "idempotencyKeyMode": "automatic"
      },
      "id": "5819e669-8e5c-4503-81fc-78588a09005d",
      "name": "Create Target Agent Execution",
      "type": "n8n-nodes-autonomous-agent.autonomousAgentExecution",
      "typeVersion": 1,
      "position": [
        464,
        0
      ],
      "credentials": {
        "autonomousAgentApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const parent=$('Sub-workflow Input').item.json; const b=parent.blueprint; const now=new Date().toISOString(); const obs=parent.observability??{}; const trace=Array.isArray(obs.trace)?[...obs.trace]:[]; const startedAt=obs.stage_started_at??obs.started_at??now; const targetExecutionId=$json.execution_id??$json.id??null; const event={timestamp:now,correlation_id:obs.correlation_id,workflow:$workflow.name,execution_id:$execution.id,stage:'target-execution-create',status:'completed',duration_ms:Math.max(0,Date.parse(now)-Date.parse(startedAt)),target_execution_id:targetExecutionId}; trace.push(event); console.log(JSON.stringify({event:'autonomous_agent_workflow_stage',...event})); return [{json:{status:'created',correlation_id:obs.correlation_id,planner_execution_id:parent.plannerExecutionId,target_execution_id:targetExecutionId,created:parent.provisioned_resources??{model_policy:b.model_policy.name,model_capability:b.model_capability.name,mcp_server:b.tool?.server?.name??null,mcp_tool:b.tool?.registration?.name??null,tool_capability:b.tool?.capability?.name??null},execution_contract:{task:b.execution.task,context:b.execution.context,input_schema:b.execution.input_schema,output_schema:b.execution.output_schema,allowed_capabilities:b.allowed_capabilities,allowed_tools:b.allowed_tools,require_approval_for:b.execution.require_approval_for},observability:{...obs,completed_at:now,trace},last_event:event}}];"
      },
      "id": "90e9590f-c3ed-42bd-b9eb-05ffa84e651b",
      "name": "Return Observable Provisioning Summary",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        704,
        0
      ]
    }
  ],
  "connections": {
    "Sub-workflow Input": {
      "main": [
        [
          {
            "node": "Start Target Execution Trace",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start Target Execution Trace": {
      "main": [
        [
          {
            "node": "Create Target Agent Execution",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Target Agent Execution": {
      "main": [
        [
          {
            "node": "Return Observable Provisioning Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "1ed4e6a6-abf0-4566-8700-6c925694764a",
  "nodeGroups": [],
  "id": "g8QHdeHGMGYK3kkM",
  "tags": [
    {
      "name": "factory",
      "id": "RafL6i7R2YphAkeq",
      "updatedAt": "2026-07-26T11:35:57.670Z",
      "createdAt": "2026-07-26T11:35:57.670Z"
    }
  ]
}