{
  "name": "Ramp Diagnostic",
  "nodes": [
    {
      "id": "c11c6ec1-d9e4-4164-a6c1-f2ef205d8634",
      "name": "Monthly Ramp Check",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        100,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "35 8 1 * *"
            }
          ]
        }
      }
    },
    {
      "id": "ebaf75d9-3432-4bbb-bf34-7868ba886557",
      "name": "Get Rep Roster",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        300,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "Sad8yEC4yhiM2VXY",
          "cachedResultName": "Seeded Rep Roster (Ramp)"
        },
        "returnAll": true
      },
      "executeOnce": true
    },
    {
      "id": "8053f3dc-3295-45e6-87ec-6aecd278dfaf",
      "name": "Get Deal Owners",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        500,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "ndaqm3DMnnIJFO4F",
          "cachedResultName": "Seeded Deal Owners"
        },
        "returnAll": true
      },
      "executeOnce": true
    },
    {
      "id": "be4e4d25-65dd-4dee-95d5-e7bf086ecd65",
      "name": "Get Stage History",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        700,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "FDaiEFabviIBiRoM",
          "cachedResultName": "Seeded Stage History"
        },
        "returnAll": true
      },
      "executeOnce": true
    },
    {
      "id": "142ea8ee-5831-47ae-8ff6-20b2871fe282",
      "name": "Get Content Library",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        900,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "2gf7i5SufszxuRHG",
          "cachedResultName": "Enablement Content Library"
        },
        "returnAll": true
      },
      "executeOnce": true
    },
    {
      "id": "fee2352f-c19e-45a4-809e-cc24beae031d",
      "name": "Compute Ramp Diagnostic",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Ramp diagnostic, computed entirely in code: where in the funnel do new hires (first 180 days) fall behind tenured reps?\n// Diagnose before prescribing: a stage only gets called out if the gap clears p < 0.05 with at least 5 deals on each side.\nconst runDate = $today.toFormat('yyyy-MM-dd');\nconst DAY = 86400000, RAMP_DAYS = 180, MIN_N = 5;\nconst reps = {}; for (const r of $('Get Rep Roster').all().map(i => i.json).filter(r => r.rep)) reps[r.rep] = r;\nconst owner = {}; for (const o of $('Get Deal Owners').all().map(i => i.json).filter(r => r.opportunityId)) owner[o.opportunityId] = o;\nconst hist = $('Get Stage History').all().map(i => i.json).filter(r => r.stage && owner[r.opportunityId]);\nconst lib = $('Get Content Library').all().map(i => i.json).filter(a => a.assetId);\nconst STAGES = ['Qualification', 'Discovery', 'Proposal', 'Negotiation'];\nconst days = (a, b) => Math.round((Date.parse(b + 'T00:00:00Z') - Date.parse(a + 'T00:00:00Z')) / DAY);\nconst median = a => { const s = [...a].sort((x, y) => x - y), m = s.length >> 1; return s.length ? (s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2) : null; };\nconst cohortOf = id => { const o = owner[id], r = reps[o.rep]; return days(r.startDate, o.dealStart) < RAMP_DAYS ? 'ramp' : 'tenured'; };\nfunction ztest(x1, n1, x2, n2) { const p = (x1 + x2) / (n1 + n2), se = Math.sqrt(p * (1 - p) * (1 / n1 + 1 / n2)); return se ? ((x1 / n1) - (x2 / n2)) / se : 0; }\n\n// Stage-by-stage conversion: of deals that reached S, how many moved on (to the next stage, or won from Negotiation)?\nconst stages = STAGES.map(S => {\n  const at = hist.filter(h => h.stage === S);\n  const g = c => { const r = at.filter(h => cohortOf(h.opportunityId) === c); return { reached: r.length, advanced: r.filter(h => h.exitedTo !== 'Closed Lost').length, medianDays: median(r.map(h => Number(h.daysInStage))) }; };\n  const ramp = g('ramp'), ten = g('tenured');\n  const thin = ramp.reached < MIN_N || ten.reached < MIN_N;\n  const z = thin ? null : +ztest(ramp.advanced, ramp.reached, ten.advanced, ten.reached).toFixed(2);\n  const verdict = thin ? 'too thin' : z <= -1.96 ? 'new hires convert less' : z >= 1.96 ? 'new hires convert more' : 'no significant difference';\n  return { stage: S, ramp, tenured: ten, rampRate: ramp.reached ? +(ramp.advanced / ramp.reached).toFixed(3) : null, tenuredRate: ten.reached ? +(ten.advanced / ten.reached).toFixed(3) : null, z, verdict };\n});\n\n// Overall win rate by cohort, and each new hire's ramp window.\nconst dealIds = [...new Set(hist.map(h => h.opportunityId))];\nconst won = id => hist.some(h => h.opportunityId === id && h.exitedTo === 'Closed Won');\nconst cohort = c => { const ids = dealIds.filter(id => cohortOf(id) === c); return { deals: ids.length, won: ids.filter(won).length }; };\nconst ramp = cohort('ramp'), tenured = cohort('tenured');\nconst overallZ = ramp.deals >= MIN_N && tenured.deals >= MIN_N ? +ztest(ramp.won, ramp.deals, tenured.won, tenured.deals).toFixed(2) : null;\nconst newHires = Object.values(reps).filter(r => dealIds.some(id => owner[id].rep === r.rep && cohortOf(id) === 'ramp')).map(r => {\n  const ids = dealIds.filter(id => owner[id].rep === r.rep && cohortOf(id) === 'ramp');\n  return { rep: r.rep, startDate: r.startDate, rampDeals: ids.length, rampWon: ids.filter(won).length };\n});\n\n// Biggest directional gap among stages with enough data, and the enablement content that exists for that moment.\nconst ranked = stages.filter(s => s.z != null).sort((a, b) => a.z - b.z);\nconst worst = ranked[0] || null;\nconst split = s => String(s || '').split(',').map(x => x.trim()).filter(Boolean);\nconst contentAtWorst = worst ? lib.filter(a => split(a.stages).includes(worst.stage) && !a.competitor && split(a.moments).includes('stage_entered')).map(a => a.assetId + ' ' + a.title) : [];\nconst flagged = stages.filter(s => s.verdict === 'new hires convert less');\nreturn [{ json: { runDate, rampDays: RAMP_DAYS, minN: MIN_N, closedDeals: dealIds.length, ramp, tenured, overallZ, stages, newHires,\n  worst: worst ? { stage: worst.stage, z: worst.z, rampRate: worst.rampRate, tenuredRate: worst.tenuredRate, significant: worst.verdict === 'new hires convert less' } : null,\n  flaggedStages: flagged.map(s => s.stage), contentAtWorst, repsSeeded: true, assignment: 'random' } }];\n"
      },
      "executeOnce": true
    },
    {
      "id": "3f7f7c41-fd7b-4790-81d9-284b8fd93aa2",
      "name": "Ramp Diagnosis Writer",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        300,
        450
      ],
      "parameters": {
        "promptType": "define",
        "text": "={{ JSON.stringify($json, null, 1) }}",
        "hasOutputParser": true,
        "options": {
          "systemMessage": "You write the monthly ramp diagnostic note for Fortavault, Inc. (a fictional data protection company) sales and enablement leadership. Every number was computed in code; you never compute or change figures.\n\nThe question is where in the funnel new hires (reps in their first rampDays days) fall behind tenured reps. A stage counts as a real gap only if its verdict is 'new hires convert less' (p < 0.05 with at least minN deals on each side).\n\nWrite:\n- diagnosis: 2 to 3 sentences, at most 70 words. If any stage is flagged, name the stage, the two conversion rates and what that points to. If none is, say plainly that no stage shows a significant gap, then name the largest directional gap with its rates and say it is not significant.\n- beforeYouAct: 1 to 2 sentences, at most 60 words, on what leadership should and should not do with this result. If nothing is significant, say that prescribing a fix (more call coaching, new onboarding modules) would be acting on noise, and name what would make the next read more reliable. If a stage is flagged, suggest checking the stage-specific content listed and how new hires run that stage before building anything new.\n- caveat: one sentence. If repsSeeded is true, say the reps and deal owners are seeded (the Salesforce org has one real user) and deals were assigned at random, so any gap in this run is chance by construction; the stage paths are seeded and the won/lost outcomes are real.\n\nWrite for people: never mention input field names such as flaggedStages, minN, worst or contentAtWorst. Never call anything proven. Never invent reps, numbers or reasons."
        }
      },
      "executeOnce": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "id": "0f23faf9-539b-443e-98ec-370a45f30547",
      "name": "Claude Sonnet",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "typeVersion": 1.3,
      "position": [
        100,
        450
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "claude-sonnet-4-6",
          "cachedResultName": "Claude Sonnet 4.6"
        },
        "options": {
          "temperature": 0.2
        }
      },
      "credentials": {
        "anthropicApi": {
          "id": "3d3ZiTi7BwnkFwqU",
          "name": "Anthropic account"
        }
      }
    },
    {
      "id": "b636f6ac-d129-4210-9a0b-76f546849a1d",
      "name": "Diagnosis Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        100,
        600
      ],
      "parameters": {
        "schemaType": "fromJson",
        "jsonSchemaExample": "{ \"diagnosis\": \"x\", \"beforeYouAct\": \"x\", \"caveat\": \"x\" }"
      }
    },
    {
      "id": "0c2d7745-1326-4696-a080-19a43eb530ab",
      "name": "Build Ramp Post",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        500,
        450
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const f = $('Compute Ramp Diagnostic').first().json;\nconst n = $('Ramp Diagnosis Writer').first().json.output || {};\nconst safe = t => String(t || '').replace(/~/g, '≈');\nconst pct = p => p == null ? '  – ' : String(Math.round(p * 100)).padStart(3) + '%';\nconst cell = c => (c.advanced + '/' + c.reached).padStart(6) + ' ' + pct(c.reached ? c.advanced / c.reached : null);\nconst table = ['Stage           new hires      tenured       z     median days  verdict',\n  ...f.stages.map(s => s.stage.padEnd(14) + ' ' + cell(s.ramp) + '   ' + cell(s.tenured) + '  ' + (s.z == null ? '   – ' : String(s.z).padStart(5)) + '   ' +\n    (String(s.ramp.medianDays ?? '–') + ' vs ' + String(s.tenured.medianDays ?? '–')).padEnd(11) + '  ' + s.verdict)].join('\\n');\nconst hires = f.newHires.map(h => h.rep + ' (started ' + h.startDate + '): ' + h.rampWon + '/' + h.rampDeals + ' won in first ' + f.rampDays + ' days').join(' · ');\nconst text = ':seedling: *Ramp Diagnostic* (run ' + f.runDate + ')\\n' +\n  'Where do new hires (first ' + f.rampDays + ' days) fall behind tenured reps? ' + f.closedDeals + ' closed deals: new hires won ' + f.ramp.won + '/' + f.ramp.deals +\n  ' (' + Math.round(100 * f.ramp.won / f.ramp.deals) + '%), tenured ' + f.tenured.won + '/' + f.tenured.deals + ' (' + Math.round(100 * f.tenured.won / f.tenured.deals) + '%)' + (f.overallZ != null ? ', z = ' + f.overallZ : '') + '.\\n\\n' +\n  '```' + table + '```\\n' +\n  safe(n.diagnosis) + '\\n\\n*Before prescribing a fix:* ' + safe(n.beforeYouAct) + '\\n\\n' +\n  (hires ? '_New-hire ramp windows: ' + hires + '._\\n' : '') +\n  '_' + safe(n.caveat) + '_';\nreturn [{ json: { slackText: text.slice(0, 3900) } }];\n"
      }
    },
    {
      "id": "e7981486-8d5b-47bb-84bd-1493c9093616",
      "name": "Post Diagnostic to Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.7,
      "position": [
        700,
        450
      ],
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "id",
          "value": "C0BQ3G96HL6",
          "cachedResultName": "general"
        },
        "text": "={{ $json.slackText }}",
        "otherOptions": {
          "includeLinkToWorkflow": false
        }
      },
      "credentials": {
        "slackApi": {
          "id": "mv7TIi8kucFPqPg6",
          "name": "Slack account"
        }
      }
    },
    {
      "id": "fedd2d8f-848d-4653-bd56-c3c3e8d48912",
      "name": "Build Snapshot Rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        700,
        600
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const f = $('Compute Ramp Diagnostic').first().json;\nconst n = $('Ramp Diagnosis Writer').first().json.output || {};\nconst now = $now.toISO();\nreturn f.stages.map(s => ({ json: { runDate: f.runDate, stage: s.stage, rampReached: s.ramp.reached, rampAdvanced: s.ramp.advanced, tenuredReached: s.tenured.reached, tenuredAdvanced: s.tenured.advanced,\n  rampRate: s.rampRate, tenuredRate: s.tenuredRate, z: s.z, verdict: s.verdict, rampMedianDays: s.ramp.medianDays, tenuredMedianDays: s.tenured.medianDays, diagnosis: n.diagnosis || '', loggedAt: now } }));"
      }
    },
    {
      "id": "3c2466b1-d85b-4d98-8adf-ec5e9a5bda35",
      "name": "Log Ramp Snapshot",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        900,
        600
      ],
      "parameters": {
        "resource": "row",
        "operation": "insert",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "uyJkm5EAvhxkeAKE",
          "cachedResultName": "Ramp Diagnostic Snapshots"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        }
      }
    }
  ],
  "connections": {
    "Monthly Ramp Check": {
      "main": [
        [
          {
            "node": "Get Rep Roster",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Rep Roster": {
      "main": [
        [
          {
            "node": "Get Deal Owners",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Deal Owners": {
      "main": [
        [
          {
            "node": "Get Stage History",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Stage History": {
      "main": [
        [
          {
            "node": "Get Content Library",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Content Library": {
      "main": [
        [
          {
            "node": "Compute Ramp Diagnostic",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compute Ramp Diagnostic": {
      "main": [
        [
          {
            "node": "Ramp Diagnosis Writer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ramp Diagnosis Writer": {
      "main": [
        [
          {
            "node": "Build Ramp Post",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude Sonnet": {
      "ai_languageModel": [
        [
          {
            "node": "Ramp Diagnosis Writer",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Diagnosis Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Ramp Diagnosis Writer",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Build Ramp Post": {
      "main": [
        [
          {
            "node": "Post Diagnostic to Slack",
            "type": "main",
            "index": 0
          },
          {
            "node": "Build Snapshot Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Snapshot Rows": {
      "main": [
        [
          {
            "node": "Log Ramp Snapshot",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}