{
  "id": "fix-3765-authored-identity-recovery",
  "title": "Set Campaign recovers unique authored Lead identity and replays idempotently",
  "kind": "hubspot_sync",
  "optional": true,
  "targetedAcceptance": {
    "schemaVersion": "hubspot-targeted-acceptance/v1",
    "reason": "Approval-gated recovery proof for one historical missing-link identity shape."
  },
  "executionMode": "serial",
  "executionModeReason": "The case deletes one run-scoped finalized link, restores its exact evidence to authored, and immediately exercises protected recovery.",
  "allowedTargetEnvironments": [
    "staging"
  ],
  "caseTimeoutMs": 900000,
  "linkedIssues": [
    3765
  ],
  "provenance": {
    "businessScenario": "A HubSpot contact already created one active Lead and connector-authored evidence, but historical drift left no finalized durable link and a blank HubSpot_Id__c mirror. Set Campaign must recover only from that unique authored evidence, create the CampaignMember on the original Lead, and remain idempotent on a distinct replay without creating another Lead.",
    "reportedBy": "Amber / Prod Campaign 701RC00001gmpUAYAY",
    "capturedAt": "2026-07-19",
    "scope": "Formal staging sandbox only. The contact, Lead, Campaign, and evidence are isolated by run id. No production mutation or historical Event replay."
  },
  "steps": [
    {
      "id": "create-campaign",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "POST",
      "path": "/services/data/v63.0/sobjects/Campaign",
      "body": {
        "Name": "IT #3765 authored recovery {{__runId}}",
        "IsActive": true
      },
      "expectStatusRange": [
        200,
        299
      ],
      "assertions": [
        {
          "path": "response.body.id",
          "exists": true
        }
      ]
    },
    {
      "id": "create-remote-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts/batch/create",
      "generate": {
        "count": 1,
        "into": "inputs",
        "itemTemplate": {
          "properties": {
            "email": "fix3765-{{__runId}}@example.com",
            "firstname": "Fix3765",
            "lastname": "Recovery{{__runId}}",
            "company": "Authored Identity Recovery",
            "lifecyclestage": "lead"
          }
        }
      },
      "expectStatusRange": [
        200,
        299
      ],
      "assertions": [
        {
          "path": "response.body.results",
          "lengthAtLeast": 1
        }
      ]
    },
    {
      "id": "assert-inbound-finalized-authority",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT L.SourceRecordId, L.LinkKey, L.ConnectorOwnerKey, E.Id AS EvidenceId, E.ConnectorNamespace, E.RemoteIDField FROM `_System_ConnectorRemoteLink` L JOIN `_System_ConnectorInboundRemoteIDEvidence` E ON E.ConnectorOwnerKey = L.ConnectorOwnerKey AND E.SourceObject = L.SourceObject AND E.SourceRecordId = L.SourceRecordId AND E.RemoteId = L.RemoteId AND E.EvidenceStatus = 'consumed' AND E.ConsumedByLinkKey = L.LinkKey AND E.ActiveKey IS NULL JOIN `Lead` P ON P.Id = L.SourceRecordId AND P.IsDeleted = 0 AND P.IsConverted = 0 WHERE L.ConnectorOwnerKey = 'pingcap-hubspot-companion' AND L.RemoteId = '{{steps.create-remote-contact.response.body.results[0].id}}' AND L.SourceObject = 'Lead' AND L.DestinationSlotId = 'hubspot-contact' AND L.Status = 'finalized'",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 240000,
        "intervalMs": 6000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records",
          "lengthAtLeast": 1
        },
        {
          "path": "response.body.records[0].ConnectorOwnerKey",
          "equals": "pingcap-hubspot-companion"
        },
        {
          "path": "response.body.records[0].ConnectorNamespace",
          "equals": "hubspot-crm-connector"
        },
        {
          "path": "response.body.records[0].RemoteIDField",
          "equals": "HubSpot_Id__c"
        }
      ]
    },
    {
      "id": "recreate-historical-missing-link-shape",
      "system": "tidb",
      "method": "POST",
      "sql": "START TRANSACTION; DELETE FROM `_System_ConnectorRemoteLink` WHERE LinkKey = '{{steps.assert-inbound-finalized-authority.response.body.records[0].LinkKey}}'; UPDATE `_System_ConnectorInboundRemoteIDEvidence` SET EvidenceStatus = 'authored', ConsumedByLinkKey = NULL, ActiveKey = CONCAT('cie_active_v2:', SHA2(CONCAT(ConnectorOwnerKey, CHAR(31), SourceObject, CHAR(31), SourceRecordId, CHAR(31), RemoteIDField), 256)), LastModifiedDate = UTC_TIMESTAMP() WHERE Id = '{{steps.assert-inbound-finalized-authority.response.body.records[0].EvidenceId}}'; UPDATE `Lead` SET HubSpot_Id__c = NULL WHERE Id = '{{steps.assert-inbound-finalized-authority.response.body.records[0].SourceRecordId}}'; COMMIT;",
      "expectStatus": 200
    },
    {
      "id": "assert-pre-recovery-shape",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT (SELECT COUNT(*) FROM `_System_ConnectorRemoteLink` L WHERE L.ConnectorOwnerKey = 'pingcap-hubspot-companion' AND L.RemoteId = '{{steps.create-remote-contact.response.body.results[0].id}}' AND L.DestinationSlotId = 'hubspot-contact' AND L.Status IN ('finalized', 'suspended_drift')) AS DurableLinkCount, (SELECT COUNT(*) FROM `_System_ConnectorInboundRemoteIDEvidence` E WHERE E.Id = '{{steps.assert-inbound-finalized-authority.response.body.records[0].EvidenceId}}' AND E.EvidenceStatus = 'authored' AND E.ConsumedByLinkKey IS NULL AND E.ActiveKey IS NOT NULL) AS AuthoredEvidenceCount, (SELECT COUNT(*) FROM `Lead` P WHERE P.Id = '{{steps.assert-inbound-finalized-authority.response.body.records[0].SourceRecordId}}' AND P.IsDeleted = 0 AND P.IsConverted = 0 AND (P.HubSpot_Id__c IS NULL OR P.HubSpot_Id__c = '')) AS OriginalBlankMirrorLeadCount, (SELECT COUNT(*) FROM `Lead` P WHERE P.Email = 'fix3765-{{__runId}}@example.com' AND P.IsDeleted = 0 AND P.IsConverted = 0) AS SameEmailLeadCount, (SELECT COUNT(*) FROM `CampaignMember` CM WHERE CM.CampaignId = '{{steps.create-campaign.response.body.id}}') AS CampaignMemberCount",
      "expectStatus": 200,
      "assertions": [
        {
          "path": "response.body.records[0].DurableLinkCount",
          "numericEquals": 0
        },
        {
          "path": "response.body.records[0].AuthoredEvidenceCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].OriginalBlankMirrorLeadCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].SameEmailLeadCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].CampaignMemberCount",
          "numericEquals": 0
        }
      ]
    },
    {
      "id": "execute-set-campaign-recovery",
      "system": "connector_action",
      "path": "/webhooks/hubspot-crm-connector/workflow-action",
      "forEach": "steps.create-remote-contact.response.body.results",
      "contactIdField": "id",
      "emailField": "properties.email",
      "campaignId": "{{steps.create-campaign.response.body.id}}",
      "status": "Sent",
      "callbackIdPrefix": "fix3765-recovery-a",
      "concurrency": 1,
      "assertions": [
        {
          "path": "response.body.failedCount",
          "numericEquals": 0
        },
        {
          "path": "response.body.acceptedCount",
          "numericEquals": 1
        }
      ]
    },
    {
      "id": "assert-recovered-original-lead",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT (SELECT COUNT(*) FROM `_System_ConnectorRemoteLink` L WHERE L.ConnectorOwnerKey = 'pingcap-hubspot-companion' AND L.RemoteId = '{{steps.create-remote-contact.response.body.results[0].id}}' AND L.SourceObject = 'Lead' AND L.SourceRecordId = '{{steps.assert-inbound-finalized-authority.response.body.records[0].SourceRecordId}}' AND L.DestinationSlotId = 'hubspot-contact' AND L.Status = 'finalized') AS FinalizedLinkCount, (SELECT COUNT(*) FROM `_System_ConnectorInboundRemoteIDEvidence` E JOIN `_System_ConnectorRemoteLink` L ON L.LinkKey = E.ConsumedByLinkKey WHERE E.Id = '{{steps.assert-inbound-finalized-authority.response.body.records[0].EvidenceId}}' AND E.EvidenceStatus = 'consumed' AND E.ActiveKey IS NULL AND L.SourceRecordId = '{{steps.assert-inbound-finalized-authority.response.body.records[0].SourceRecordId}}' AND L.RemoteId = '{{steps.create-remote-contact.response.body.results[0].id}}') AS ConsumedEvidenceCount, (SELECT COUNT(*) FROM `CampaignMember` CM WHERE CM.CampaignId = '{{steps.create-campaign.response.body.id}}' AND CM.LeadId = '{{steps.assert-inbound-finalized-authority.response.body.records[0].SourceRecordId}}' AND CM.ContactId IS NULL) AS ExactCampaignMemberCount, (SELECT COUNT(*) FROM `Lead` P WHERE P.Email = 'fix3765-{{__runId}}@example.com' AND P.IsDeleted = 0 AND P.IsConverted = 0) AS SameEmailLeadCount, (SELECT COUNT(*) FROM `Lead` P WHERE P.Id = '{{steps.assert-inbound-finalized-authority.response.body.records[0].SourceRecordId}}' AND P.IsDeleted = 0 AND P.IsConverted = 0 AND (P.HubSpot_Id__c IS NULL OR P.HubSpot_Id__c = '')) AS OriginalBlankMirrorLeadCount",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 240000,
        "intervalMs": 6000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records[0].FinalizedLinkCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].ConsumedEvidenceCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].ExactCampaignMemberCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].SameEmailLeadCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].OriginalBlankMirrorLeadCount",
          "numericEquals": 1
        }
      ]
    },
    {
      "id": "execute-set-campaign-replay",
      "system": "connector_action",
      "path": "/webhooks/hubspot-crm-connector/workflow-action",
      "forEach": "steps.create-remote-contact.response.body.results",
      "contactIdField": "id",
      "emailField": "properties.email",
      "campaignId": "{{steps.create-campaign.response.body.id}}",
      "status": "Sent",
      "callbackIdPrefix": "fix3765-recovery-b",
      "concurrency": 1,
      "assertions": [
        {
          "path": "response.body.failedCount",
          "numericEquals": 0
        },
        {
          "path": "response.body.acceptedCount",
          "numericEquals": 1
        }
      ]
    },
    {
      "id": "assert-replay-idempotent",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT (SELECT COUNT(*) FROM `_System_ConnectorRemoteLink` L WHERE L.ConnectorOwnerKey = 'pingcap-hubspot-companion' AND L.RemoteId = '{{steps.create-remote-contact.response.body.results[0].id}}' AND L.SourceObject = 'Lead' AND L.SourceRecordId = '{{steps.assert-inbound-finalized-authority.response.body.records[0].SourceRecordId}}' AND L.DestinationSlotId = 'hubspot-contact' AND L.Status = 'finalized') AS FinalizedLinkCount, (SELECT COUNT(*) FROM `_System_ConnectorInboundRemoteIDEvidence` E WHERE E.Id = '{{steps.assert-inbound-finalized-authority.response.body.records[0].EvidenceId}}' AND E.EvidenceStatus = 'consumed' AND E.ActiveKey IS NULL) AS ConsumedEvidenceCount, (SELECT COUNT(*) FROM `CampaignMember` CM WHERE CM.CampaignId = '{{steps.create-campaign.response.body.id}}' AND CM.LeadId = '{{steps.assert-inbound-finalized-authority.response.body.records[0].SourceRecordId}}') AS ExactCampaignMemberCount, (SELECT COUNT(*) FROM `Lead` P WHERE P.Email = 'fix3765-{{__runId}}@example.com' AND P.IsDeleted = 0 AND P.IsConverted = 0) AS SameEmailLeadCount, (SELECT COUNT(*) FROM `_System_ConnectorEvent` E WHERE E.Namespace = 'hubspot-crm-connector' AND E.EventKind = 'workflow.action.execute' AND E.SourceObjectID = '{{steps.create-remote-contact.response.body.results[0].id}}' AND E.Status IN ('processed', 'skipped')) AS TerminalWorkflowCount, (SELECT COUNT(*) FROM `_System_ConnectorEvent` E WHERE E.Namespace = 'hubspot-crm-connector' AND E.EventKind = 'workflow.action.execute' AND E.SourceObjectID = '{{steps.create-remote-contact.response.body.results[0].id}}' AND E.Status IN ('failed', 'dead')) AS FailedWorkflowCount",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 240000,
        "intervalMs": 6000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records[0].FinalizedLinkCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].ConsumedEvidenceCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].ExactCampaignMemberCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].SameEmailLeadCount",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].TerminalWorkflowCount",
          "atLeast": 2
        },
        {
          "path": "response.body.records[0].FailedWorkflowCount",
          "numericEquals": 0
        }
      ]
    }
  ]
}
