{
  "id": "post-convert-contact-storm",
  "title": "Outbound-synced native Lead converts and post-convert storm converges the active Contact",
  "kind": "hubspot_sync",
  "optional": false,
  "executionMode": "serial",
  "executionModeReason": "The case owns one native Lead outbound lane, its convert boundary, and a ten-event Contact source lane; it must not overlap another case that can mutate either queue.",
  "caseTimeoutMs": 1260000,
  "linkedIssues": [2829, 3089],
  "provenance": {
    "businessScenario": "A NexusCRM-native Lead first syncs outbound and receives a HubSpot remote identity. Convert must transfer that identity to the resulting active Contact without the historical namespace conflict. A provider-backed ten-event jobtitle storm after convert must converge the Contact to the final value while the converted Lead remains historical and no duplicate active person is created.",
    "reportedBy": "Historical fixes #2829 and #3089",
    "capturedAt": "2026-08-26",
    "calibrated": "The provider is first patched to the final title, then ten ordered signed property-change events exercise source-lane coalescing. Event rows prove exact admission only; terminal execution follows each exact Event through its projection identity and durable input ordinal range, selected WorkItem Event, scoped Runtime command and active Contact business readback. Coalesced WorkItem creation time does not define event membership."
  },
  "steps": [
    {
      "id": "account-record-type",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id FROM _System_RecordType WHERE SobjectType='Account' AND Name='Customer' AND IsActive=true LIMIT 1",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records", "lengthAtLeast": 1 },
        { "path": "response.body.records[0].Id", "exists": true }
      ]
    },
    {
      "id": "create-convert-account",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "POST",
      "path": "/services/data/v63.0/sobjects/Account",
      "body": {
        "Name": "PostConvert Account {{__runId}}",
        "Industry": "FSI-FinTech",
        "NumberOfEmployees": 120,
        "RecordTypeId": "{{steps.account-record-type.response.body.records[0].Id}}",
        "Region__c": "NA",
        "Vertical__c": "Payment",
        "Website": "https://post-convert-{{__runId}}.example.com"
      },
      "expectStatusRange": [200, 299],
      "assertions": [{ "path": "response.body.id", "exists": true }]
    },
    {
      "id": "create-native-lead",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "POST",
      "path": "/services/data/v63.0/sobjects/Lead",
      "body": {
        "FirstName": "PostConvert",
        "LastName": "Native{{__runId}}",
        "Company": "PostConvert Account {{__runId}}",
        "Email": "post-convert-{{__runId}}@example.com",
        "Title": "PostConvertStart",
        "HubSpot_Sync_Enabled__c": true
      },
      "expectStatusRange": [200, 299],
      "assertions": [{ "path": "response.body.id", "exists": true }]
    },
    {
      "id": "wait-outbound-hubspot-id",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,HubSpot_Id__c,IsConverted FROM Lead WHERE Id='{{steps.create-native-lead.response.body.id}}' LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 300000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].HubSpot_Id__c", "exists": true },
        { "path": "response.body.records[0].IsConverted", "equals": false }
      ]
    },
    {
      "id": "assert-outbound-contact-exists",
      "system": "hubspot",
      "method": "GET",
      "path": "/crm/v3/objects/contacts/{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}?properties=email,jobtitle",
      "expectStatus": 200,
      "poll": { "budgetMs": 120000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.id", "equals": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}" },
        { "path": "response.body.properties.jobtitle", "equals": "PostConvertStart" }
      ]
    },
    {
      "id": "convert-native-lead",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "POST",
      "path": "/services/data/v63.0/actions/standard/convertLead",
      "body": {
        "inputs": [
          {
            "leadId": "{{steps.create-native-lead.response.body.id}}",
            "convertedStatus": "Open Opportunity",
            "accountId": "{{steps.create-convert-account.response.body.id}}"
          }
        ]
      },
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body[0].isSuccess", "equals": true },
        { "path": "response.body[0].outputValues.contactId", "exists": true }
      ]
    },
    {
      "id": "assert-transferred-contact-identity",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,HubSpot_Id__c,Title FROM Contact WHERE Id='{{steps.convert-native-lead.response.body[0].outputValues.contactId}}' LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 120000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].HubSpot_Id__c", "equals": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}" },
        { "path": "response.body.records[0].Title", "equals": "PostConvertStart" }
      ]
    },
    {
      "id": "capture-storm-clock",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT ClockMs, ClockMs + 1 AS ClockMs02, ClockMs + 2 AS ClockMs03, ClockMs + 3 AS ClockMs04, ClockMs + 4 AS ClockMs05, ClockMs + 5 AS ClockMs06, ClockMs + 6 AS ClockMs07, ClockMs + 7 AS ClockMs08, ClockMs + 8 AS ClockMs09, ClockMs + 9 AS ClockMs10 FROM (SELECT CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED) AS ClockMs) AS clock",
      "expectStatus": 200,
      "assertions": [{ "path": "response.body.records[0].ClockMs", "exists": true }]
    },
    {
      "id": "patch-provider-final-title",
      "system": "hubspot",
      "method": "PATCH",
      "path": "/crm/v3/objects/contacts/{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}",
      "body": { "properties": { "jobtitle": "PostConvertFinal{{__runId}}" } },
      "expectStatusRange": [200, 299],
      "assertions": [{ "path": "response.body.id", "equals": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}" }]
    },
    {
      "id": "send-ten-post-convert-events",
      "system": "connector_webhook",
      "method": "POST",
      "path": "/webhooks/hubspot-crm-connector/webhook",
      "body": [
        { "portalId": 51280875, "appId": 44166359, "subscriptionId": 6723583, "eventId": "post-convert-storm-{{__runId}}-01", "occurredAt": "{{steps.capture-storm-clock.response.body.records[0].ClockMs}}", "subscriptionType": "contact.propertyChange", "attemptNumber": 0, "objectId": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}", "propertyName": "jobtitle", "propertyValue": "PostConvert01{{__runId}}", "changeSource": "API" },
        { "portalId": 51280875, "appId": 44166359, "subscriptionId": 6723583, "eventId": "post-convert-storm-{{__runId}}-02", "occurredAt": "{{steps.capture-storm-clock.response.body.records[0].ClockMs02}}", "subscriptionType": "contact.propertyChange", "attemptNumber": 0, "objectId": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}", "propertyName": "jobtitle", "propertyValue": "PostConvert02{{__runId}}", "changeSource": "API" },
        { "portalId": 51280875, "appId": 44166359, "subscriptionId": 6723583, "eventId": "post-convert-storm-{{__runId}}-03", "occurredAt": "{{steps.capture-storm-clock.response.body.records[0].ClockMs03}}", "subscriptionType": "contact.propertyChange", "attemptNumber": 0, "objectId": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}", "propertyName": "jobtitle", "propertyValue": "PostConvert03{{__runId}}", "changeSource": "API" },
        { "portalId": 51280875, "appId": 44166359, "subscriptionId": 6723583, "eventId": "post-convert-storm-{{__runId}}-04", "occurredAt": "{{steps.capture-storm-clock.response.body.records[0].ClockMs04}}", "subscriptionType": "contact.propertyChange", "attemptNumber": 0, "objectId": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}", "propertyName": "jobtitle", "propertyValue": "PostConvert04{{__runId}}", "changeSource": "API" },
        { "portalId": 51280875, "appId": 44166359, "subscriptionId": 6723583, "eventId": "post-convert-storm-{{__runId}}-05", "occurredAt": "{{steps.capture-storm-clock.response.body.records[0].ClockMs05}}", "subscriptionType": "contact.propertyChange", "attemptNumber": 0, "objectId": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}", "propertyName": "jobtitle", "propertyValue": "PostConvert05{{__runId}}", "changeSource": "API" },
        { "portalId": 51280875, "appId": 44166359, "subscriptionId": 6723583, "eventId": "post-convert-storm-{{__runId}}-06", "occurredAt": "{{steps.capture-storm-clock.response.body.records[0].ClockMs06}}", "subscriptionType": "contact.propertyChange", "attemptNumber": 0, "objectId": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}", "propertyName": "jobtitle", "propertyValue": "PostConvert06{{__runId}}", "changeSource": "API" },
        { "portalId": 51280875, "appId": 44166359, "subscriptionId": 6723583, "eventId": "post-convert-storm-{{__runId}}-07", "occurredAt": "{{steps.capture-storm-clock.response.body.records[0].ClockMs07}}", "subscriptionType": "contact.propertyChange", "attemptNumber": 0, "objectId": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}", "propertyName": "jobtitle", "propertyValue": "PostConvert07{{__runId}}", "changeSource": "API" },
        { "portalId": 51280875, "appId": 44166359, "subscriptionId": 6723583, "eventId": "post-convert-storm-{{__runId}}-08", "occurredAt": "{{steps.capture-storm-clock.response.body.records[0].ClockMs08}}", "subscriptionType": "contact.propertyChange", "attemptNumber": 0, "objectId": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}", "propertyName": "jobtitle", "propertyValue": "PostConvert08{{__runId}}", "changeSource": "API" },
        { "portalId": 51280875, "appId": 44166359, "subscriptionId": 6723583, "eventId": "post-convert-storm-{{__runId}}-09", "occurredAt": "{{steps.capture-storm-clock.response.body.records[0].ClockMs09}}", "subscriptionType": "contact.propertyChange", "attemptNumber": 0, "objectId": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}", "propertyName": "jobtitle", "propertyValue": "PostConvert09{{__runId}}", "changeSource": "API" },
        { "portalId": 51280875, "appId": 44166359, "subscriptionId": 6723583, "eventId": "post-convert-storm-{{__runId}}-10", "occurredAt": "{{steps.capture-storm-clock.response.body.records[0].ClockMs10}}", "subscriptionType": "contact.propertyChange", "attemptNumber": 0, "objectId": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}", "propertyName": "jobtitle", "propertyValue": "PostConvertFinal{{__runId}}", "changeSource": "API" }
      ],
      "expectStatus": 202,
      "assertions": [{ "path": "response.body", "exists": true }]
    },
    {
      "id": "assert-active-contact-converged",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,HubSpot_Id__c,Title FROM Contact WHERE Id='{{steps.convert-native-lead.response.body[0].outputValues.contactId}}' LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 300000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].HubSpot_Id__c", "equals": "{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}" },
        { "path": "response.body.records[0].Title", "equals": "PostConvertFinal{{__runId}}" }
      ]
    },
    {
      "id": "assert-ten-events-d3-terminal",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(DISTINCT E.Id) AS EventCount, COUNT(DISTINCT COALESCE(JSON_UNQUOTE(JSON_EXTRACT(E.Payload, '$.payload.eventId')), JSON_UNQUOTE(JSON_EXTRACT(E.Payload, '$.eventId')))) AS EventIdentityCount, COUNT(DISTINCT CASE WHEN W.WorkId IS NULL OR S.Id IS NULL THEN E.Id END) AS UncoveredEventCount, COUNT(DISTINCT CASE WHEN W.State NOT IN ('processed', 'skipped') THEN W.WorkId END) AS NonterminalOrDeadWorkCount, COUNT(DISTINCT CASE WHEN W.State = 'processed' AND (P.Id IS NULL OR P.State <> 'COMPLETED' OR COALESCE(P.ResultPayload, '') = '') THEN W.WorkId END) AS ProcessedWithoutCompletedCommandCount, COUNT(DISTINCT CASE WHEN W.State = 'processed' AND P.State = 'COMPLETED' AND COALESCE(P.ResultPayload, '') <> '' THEN P.Id END) AS RuntimeCompletedCount, COUNT(DISTINCT CASE WHEN P.State NOT IN ('COMPLETED', 'FAILED', 'POISONED') THEN P.Id END) AS NonterminalPlatformCommandCount, COUNT(DISTINCT CASE WHEN P.State IN ('FAILED', 'POISONED') THEN P.Id END) AS TerminalFailureCount FROM `_System_ConnectorEvent` E LEFT JOIN `_System_ConnectorWorkItem` W ON W.InstalledPackId = E.InstalledPackId AND W.WorkType IN ('connector_event', 'workflow_action_apply') AND ((COALESCE(W.ProjectionKey, '') = '' AND W.EventId = E.Id) OR (BINARY W.ProjectionKey = BINARY CONCAT('csp:', LOWER(SHA2(CONCAT(TRIM(E.InstalledPackId), CHAR(0), LOWER(TRIM(E.EventKind)), CHAR(0), CAST(COALESCE(E.RouteVersion, 0) AS CHAR), CHAR(0), TRIM(COALESCE(E.CompanionPackInstallId, '')), CHAR(0), TRIM(COALESCE(E.CompanionFlowVersionId, '')), CHAR(0), TRIM(W.WorkType), CHAR(0), TRIM(E.SourceLaneKey)), 256))) AND W.SourceLaneKey = E.SourceLaneKey AND W.InputStartOrdinal > 0 AND E.EnqueueOrdinal BETWEEN W.InputStartOrdinal AND W.LaneOrdinal)) LEFT JOIN `_System_ConnectorEvent` S ON S.Id = W.EventId AND S.InstalledPackId = E.InstalledPackId AND S.SourceLaneKey = E.SourceLaneKey AND LOWER(TRIM(S.EventKind)) = LOWER(TRIM(E.EventKind)) AND COALESCE(S.RouteVersion, 0) = COALESCE(E.RouteVersion, 0) AND COALESCE(S.CompanionPackInstallId, '') = COALESCE(E.CompanionPackInstallId, '') AND COALESCE(S.CompanionFlowVersionId, '') = COALESCE(E.CompanionFlowVersionId, '') AND (COALESCE(W.ProjectionKey, '') = '' OR S.EnqueueOrdinal = W.LaneOrdinal) LEFT JOIN `_System_ConnectorRuntimeHandoffReceipt` H ON S.Id IS NOT NULL AND H.WorkId = W.WorkId AND H.EventId = W.EventId AND H.InstalledPackId = W.InstalledPackId AND H.Namespace = E.Namespace AND LOWER(TRIM(H.EventKind)) = LOWER(TRIM(S.EventKind)) AND H.RouteVersion = S.RouteVersion LEFT JOIN `_System_RuntimePlatformCommand` P ON P.CommandId = H.CommandId AND BINARY P.CommandId = BINARY H.CommandId AND P.ScopeDigest = LOWER(SHA2(CONCAT(H.OrgId, CHAR(0), H.AppInstallationId, CHAR(0), H.AuthorizedWorkloadId, CHAR(0), 'runtime.commands.accept.v1'), 256)) AND BINARY P.OrgId = BINARY H.OrgId AND BINARY P.AppInstallationId = BINARY H.AppInstallationId AND BINARY P.CallerWorkloadId = BINARY H.AuthorizedWorkloadId AND P.RuntimeCapabilityId = 'runtime.commands.accept.v1' AND P.CommandKind = H.CommandKind AND P.PayloadFormatVersion = H.PayloadFormatVersion WHERE E.Namespace = 'hubspot-crm-connector' AND E.EventKind = 'contact.propertychange' AND E.SourceObjectID = '{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}' AND COALESCE(JSON_UNQUOTE(JSON_EXTRACT(E.Payload, '$.payload.eventId')), JSON_UNQUOTE(JSON_EXTRACT(E.Payload, '$.eventId'))) IN ('post-convert-storm-{{__runId}}-01', 'post-convert-storm-{{__runId}}-02', 'post-convert-storm-{{__runId}}-03', 'post-convert-storm-{{__runId}}-04', 'post-convert-storm-{{__runId}}-05', 'post-convert-storm-{{__runId}}-06', 'post-convert-storm-{{__runId}}-07', 'post-convert-storm-{{__runId}}-08', 'post-convert-storm-{{__runId}}-09', 'post-convert-storm-{{__runId}}-10')",
      "expectStatus": 200,
      "poll": { "budgetMs": 300000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].EventCount", "numericEquals": 10 },
        { "path": "response.body.records[0].EventIdentityCount", "numericEquals": 10 },
        { "path": "response.body.records[0].UncoveredEventCount", "numericEquals": 0 },
        { "path": "response.body.records[0].NonterminalOrDeadWorkCount", "numericEquals": 0 },
        { "path": "response.body.records[0].ProcessedWithoutCompletedCommandCount", "numericEquals": 0 },
        { "path": "response.body.records[0].RuntimeCompletedCount", "atLeast": 1 },
        { "path": "response.body.records[0].NonterminalPlatformCommandCount", "numericEquals": 0 },
        { "path": "response.body.records[0].TerminalFailureCount", "numericEquals": 0 }
      ]
    },
    {
      "id": "assert-convert-identity-cardinality",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT (SELECT COUNT(*) FROM `Lead` WHERE Id = '{{steps.create-native-lead.response.body.id}}' AND IsConverted = 1) AS ConvertedLeadCount, (SELECT COUNT(*) FROM `Lead` WHERE HubSpot_Id__c = '{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}' AND IsConverted = 0 AND IsDeleted = 0) AS ActiveLeadCount, (SELECT COUNT(*) FROM `Contact` WHERE HubSpot_Id__c = '{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}' AND IsDeleted = 0) AS ActiveContactCount",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records[0].ConvertedLeadCount", "numericEquals": 1 },
        { "path": "response.body.records[0].ActiveLeadCount", "numericEquals": 0 },
        { "path": "response.body.records[0].ActiveContactCount", "numericEquals": 1 }
      ]
    },
    {
      "id": "assert-provider-final-title",
      "system": "hubspot",
      "method": "GET",
      "path": "/crm/v3/objects/contacts/{{steps.wait-outbound-hubspot-id.response.body.records[0].HubSpot_Id__c}}?properties=jobtitle",
      "expectStatus": 200,
      "assertions": [{ "path": "response.body.properties.jobtitle", "equals": "PostConvertFinal{{__runId}}" }]
    }
  ]
}
