{
  "id": "out-409-create-frozen-recover",
  "title": "New outbound Lead adopts and converges an existing HubSpot contact through one V2 identity-conflict command",
  "kind": "hubspot_sync",
  "executionMode": "serial",
  "executionModeReason": "This case isolates one record.created outbound command from competing queue pressure, drains and removes the inbound Lead created from the same HubSpot fixture, then proves one V2 durable command adopts the existing identity and applies only the frozen save-time payload without a second durable command.",
  "caseTimeoutMs": 780000,
  "linkedIssues": [],
  "provenance": {
    "businessScenario": "OUT-409-CREATE-FROZEN-RECOVER - regression for the 2026-07-17 PROD incident whose V1 symptom was a POST 409 for an existing email identity. The V2 business contract is that one record.created durable command resolves and adopts the exact existing HubSpot contact, atomically finalizes the remote link, applies the persisted frozen create payload, and converges initial fields without a worker source-record read or a second durable command.",
    "reportedBy": "2026-07-17 PROD RCA error 2",
    "capturedAt": "2026-07-17",
    "calibrated": "[recalibrated 2026-08-27] The fixture drains the materialized contact source lane through the D3 WorkItem and correlated Runtime-command authorities; the legacy Event.Status/ProcessedAt mirror is not used as an execution oracle. After every Event is directly owned or projection-covered, every handoff is terminal, and the source is quiet for 60 seconds, the fixture removes the inbound Lead and creates the tested Lead with HubSpot_Sync_Enabled__c=true in the initial POST. No later NexusCRM PATCH is allowed to manufacture an old snapshot. The current V2 command ledger does not persist per-provider-attempt response evidence, so this case proves the durable identity-conflict recovery outcome and frozen-payload convergence without claiming a direct first-response 409 receipt."
  },
  "steps": [
    {
      "id": "seed-existing-hubspot-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "out409-create-{{__runId}}@example.com",
          "firstname": "RemoteBefore",
          "lastname": "RemoteBefore{{__runId}}",
          "company": "Remote Before",
          "jobtitle": "RemoteBefore409"
        }
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "find-inbound-contact-lead",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id FROM Lead WHERE HubSpot_Id__c='{{steps.seed-existing-hubspot-contact.response.body.id}}' LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 240000, "intervalMs": 6000 },
      "onTimeout": "blocked",
      "assertions": [
        { "path": "response.body.records", "lengthAtLeast": 1 }
      ],
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.seed-existing-hubspot-contact.response.body.id}}",
          "eventKind": "contact.propertychange",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true
        }
      },
      "timeoutMs": 8000
    },
    {
      "id": "disable-inbound-contact-outbound",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "PATCH",
      "path": "/services/data/v63.0/sobjects/Lead/{{steps.find-inbound-contact-lead.response.body.records[0].Id}}",
      "body": {
        "HubSpot_Sync_Enabled__c": false
      },
      "expectStatus": 204
    },
    {
      "id": "wait-contact-propertychanges-drained",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT CASE WHEN COUNT(*) > 0 AND MAX(E.CreatedDate) <= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 60 SECOND) AND NOT EXISTS (SELECT 1 FROM `_System_ConnectorWorkItem` W JOIN `_System_ConnectorEvent` S ON S.Id = W.EventId WHERE S.Namespace = 'hubspot-crm-connector' AND S.EventKind = 'contact.propertychange' AND S.SourceObjectID = '{{steps.seed-existing-hubspot-contact.response.body.id}}' AND W.WorkType IN ('connector_event', 'workflow_action_apply') AND W.State NOT IN ('processed', 'skipped')) AND NOT EXISTS (SELECT 1 FROM `_System_ConnectorWorkItem` W JOIN `_System_ConnectorEvent` S ON S.Id = W.EventId WHERE S.Namespace = 'hubspot-crm-connector' AND S.EventKind = 'contact.propertychange' AND S.SourceObjectID = '{{steps.seed-existing-hubspot-contact.response.body.id}}' AND W.WorkType IN ('connector_event', 'workflow_action_apply') AND W.State = 'processed' AND NOT EXISTS (SELECT 1 FROM `_System_ConnectorRuntimeHandoffReceipt` R JOIN `_System_RuntimePlatformCommand` C ON C.CommandId = R.CommandId WHERE R.WorkId = W.WorkId AND R.EventId = W.EventId AND C.State IN ('COMPLETED', 'FAILED', 'POISONED'))) AND NOT EXISTS (SELECT 1 FROM `_System_ConnectorEvent` X WHERE X.Namespace = 'hubspot-crm-connector' AND X.EventKind = 'contact.propertychange' AND X.SourceObjectID = '{{steps.seed-existing-hubspot-contact.response.body.id}}' AND NOT EXISTS (SELECT 1 FROM `_System_ConnectorWorkItem` W JOIN `_System_ConnectorEvent` S ON S.Id = W.EventId WHERE W.WorkType IN ('connector_event', 'workflow_action_apply') AND (W.EventId = X.Id OR (W.InstalledPackId = X.InstalledPackId AND NULLIF(COALESCE(W.ProjectionKey, ''), '') IS NOT NULL AND COALESCE(W.SourceLaneKey, '') = COALESCE(X.SourceLaneKey, '') AND S.InstalledPackId = X.InstalledPackId AND LOWER(TRIM(S.EventKind)) = LOWER(TRIM(X.EventKind)) AND COALESCE(S.RouteVersion, 0) = COALESCE(X.RouteVersion, 0) AND COALESCE(S.CompanionPackInstallId, '') = COALESCE(X.CompanionPackInstallId, '') AND COALESCE(S.CompanionFlowVersionId, '') = COALESCE(X.CompanionFlowVersionId, '') AND ((X.EnqueueOrdinal <> 0 AND S.EnqueueOrdinal <> 0 AND (X.EnqueueOrdinal < S.EnqueueOrdinal OR (X.EnqueueOrdinal = S.EnqueueOrdinal AND X.Id <= S.Id))) OR ((X.EnqueueOrdinal = 0 OR S.EnqueueOrdinal = 0) AND (X.CreatedDate < S.CreatedDate OR (X.CreatedDate = S.CreatedDate AND (X.DeliverySequence < S.DeliverySequence OR (X.DeliverySequence = S.DeliverySequence AND X.Id <= S.Id)))))))))) THEN 1 ELSE 0 END AS Ready, COUNT(*) AS EventCount, MAX(E.CreatedDate) AS LastEventCreatedAt FROM `_System_ConnectorEvent` E WHERE E.Namespace = 'hubspot-crm-connector' AND E.EventKind = 'contact.propertychange' AND E.SourceObjectID = '{{steps.seed-existing-hubspot-contact.response.body.id}}'",
      "expectStatus": 200,
      "poll": { "budgetMs": 180000, "intervalMs": 6000 },
      "onTimeout": "blocked",
      "assertions": [
        { "path": "response.body.records[0].Ready", "numericEquals": 1 }
      ]
    },
    {
      "id": "clear-inbound-contact-link",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "PATCH",
      "path": "/services/data/v63.0/sobjects/Lead/{{steps.find-inbound-contact-lead.response.body.records[0].Id}}",
      "body": {
        "HubSpot_Id__c": null
      },
      "expectStatus": 204
    },
    {
      "id": "delete-inbound-contact-lead",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "DELETE",
      "path": "/services/data/v63.0/sobjects/Lead/{{steps.find-inbound-contact-lead.response.body.records[0].Id}}",
      "expectStatus": 204
    },
    {
      "id": "assert-no-inbound-link-precondition",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT COUNT() FROM Lead WHERE HubSpot_Id__c='{{steps.seed-existing-hubspot-contact.response.body.id}}'",
      "expectStatus": 200,
      "poll": { "budgetMs": 60000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.totalSize", "numericEquals": 0 }
      ]
    },
    {
      "id": "create-sync-enabled-lead",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "POST",
      "path": "/services/data/v63.0/sobjects/Lead",
      "body": {
        "FirstName": "FrozenPatched",
        "LastName": "Create409{{__runId}}",
        "Company": "Frozen Create Co {{__runId}}",
        "Email": "out409-create-{{__runId}}@example.com",
        "Title": "Created409-{{__runId}}",
        "HubSpot_Sync_Enabled__c": true
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "assert-identity-conflict-command-finalized",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT Id, State, AttemptCount, ResponseStatus, RemoteId, CASE WHEN FailureCode IS NULL OR FailureCode = '' THEN 1 ELSE 0 END AS FailureCodeAbsent FROM `_System_IntegrationOutboundCommand` WHERE SourceObject = 'Lead' AND SourceRecordId = '{{steps.create-sync-enabled-lead.response.body.id}}' AND FlowActionName = 'PushLeadToHubSpotContact' ORDER BY CreatedDate DESC LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 240000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records", "lengthAtLeast": 1 },
        { "path": "response.body.records[0].State", "equals": "succeeded" },
        { "path": "response.body.records[0].AttemptCount", "numericEquals": 2 },
        { "path": "response.body.records[0].ResponseStatus", "numericEquals": 200 },
        { "path": "response.body.records[0].RemoteId", "equals": "{{steps.seed-existing-hubspot-contact.response.body.id}}" },
        { "path": "response.body.records[0].FailureCodeAbsent", "numericEquals": 1 }
      ]
    },
    {
      "id": "wait-adopt-existing-contact",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT HubSpot_Id__c FROM Lead WHERE Id='{{steps.create-sync-enabled-lead.response.body.id}}' LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 240000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].HubSpot_Id__c", "equals": "{{steps.seed-existing-hubspot-contact.response.body.id}}" }
      ]
    },
    {
      "id": "assert-existing-contact-converged",
      "system": "hubspot",
      "method": "GET",
      "path": "/crm/v3/objects/contacts/{{steps.seed-existing-hubspot-contact.response.body.id}}?properties=email,firstname,lastname,company,jobtitle",
      "expectStatus": 200,
      "poll": { "budgetMs": 120000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.properties.email", "includes": "out409-create-{{__runId}}@example.com", "caseInsensitive": true },
        { "path": "response.body.properties.firstname", "equals": "FrozenPatched" },
        { "path": "response.body.properties.lastname", "equals": "Create409{{__runId}}" },
        { "path": "response.body.properties.company", "equals": "Frozen Create Co {{__runId}}" },
        { "path": "response.body.properties.jobtitle", "equals": "Created409-{{__runId}}" }
      ]
    },
    {
      "id": "assert-single-durable-command",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS CommandCount FROM `_System_IntegrationOutboundCommand` WHERE SourceObject = 'Lead' AND SourceRecordId = '{{steps.create-sync-enabled-lead.response.body.id}}' AND FlowActionName = 'PushLeadToHubSpotContact'",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records[0].CommandCount", "numericEquals": 1 }
      ]
    },
    {
      "id": "assert-one-linked-lead",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT COUNT() FROM Lead WHERE HubSpot_Id__c='{{steps.seed-existing-hubspot-contact.response.body.id}}'",
      "expectStatus": 200,
      "poll": { "budgetMs": 60000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.totalSize", "numericEquals": 1 }
      ]
    }
  ]
}
