{
  "id": "fix-4799-shell-lead-owner-writeback",
  "title": "Nameless shell Lead (LastName=HubSpot Lead) still pushes its assigned owner to HubSpot without echoing the placeholder name",
  "kind": "hubspot_sync",
  "executionMode": "serial",
  "executionModeReason": "Creates a unique nameless HubSpot Contact and follows one connector lane through region assignment and the owner writeback; serial execution keeps the provider readback free of unrelated owner writes.",
  "caseTimeoutMs": 600000,
  "linkedIssues": [
    4799
  ],
  "provenance": {
    "businessScenario": "Shell Leads (LastName == \"HubSpot Lead\", created when the HubSpot contact has no name) skip the entire NexusCRM -> HubSpot outbound push, so the NexusCRM-assigned owner never propagates and the two systems diverge on owner with no sync ever attempted (PROD 2026-08-03: Lead 3c181290 owner Kevin Liu vs HubSpot contact 239510577020 owner Vince Yao, whose only owner history entry came from SALESFORCE).",
    "reportedBy": "prod owner-divergence analysis 2026-08-03",
    "capturedAt": "2026-08-15",
    "upstreamIssue": "https://github.com/pingcap-inc/nexuscrm-pingcap-pack/issues/4799",
    "calibrated": "Creates a HubSpot Contact with NO firstname and NO lastname (the shell shape) plus no source HubSpot owner. PASS requires the materialized Lead to carry the LastName=HubSpot Lead placeholder, stamp the final bound owner shadow, and PATCH hubspot_owner_id back to the same Contact. The readback also requires firstname/lastname to stay absent on the provider, so the fix may not regress into echoing the synthetic placeholder name. Missing Germany metadata or no bound final owner is BLOCKED, never PASS. [promoted 2026-08-15] The #4799 runtime fix landed and the case was promoted to required continuous live coverage. [recalibrated 2026-09-04] Start with an explicitly empty HubSpot owner and verify the create response is empty/absent. The oracle is the materialized Lead's final OwnerId, independently checked against one active CRM User and one bound User mapping in HubSpotOwnerBinding__c; this case proves final-owner writeback, not which individual the region assignment policy selects. Require the Lead owner to stay stable, its shadow and the same HubSpot Contact to equal that binding, a succeeded Integration command for this exact Lead/action with HTTP 200, no failure code, and exactly one mapped field containing the expected owner, and provider history containing the nonempty target owner. After provider convergence, a final CRM read must still contain the same unique Lead, owner, shadow and shell fields. Provider-side automation alone cannot satisfy the case. Missing or ambiguous user/binding prerequisites remain BLOCKED. No fixed HubSpot or CRM owner id is assumed."
  },
  "steps": [
    {
      "id": "dict",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Name FROM Country_Dictionary__c WHERE Full_Name__c='Germany' LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 30000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records[0].Name",
          "exists": true
        }
      ]
    },
    {
      "id": "region",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Region_Name__c FROM Countries_Region__c WHERE Name='{{steps.dict.response.body.records[0].Name}}' LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 30000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records[0].Region_Name__c",
          "exists": true
        }
      ]
    },
    {
      "id": "create-nameless-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "fix4799-{{__runId}}@example.com",
          "country_picklist__sfdc_": "Germany",
          "hubspot_owner_id": ""
        }
      },
      "expectStatusRange": [
        200,
        299
      ],
      "assertions": [
        {
          "path": "response.body.id",
          "exists": true
        },
        {
          "path": "response.body.properties.firstname",
          "exists": false
        },
        {
          "path": "response.body.properties.lastname",
          "exists": false
        },
        {
          "path": "response.body.properties.hubspot_owner_id",
          "exists": false
        }
      ]
    },
    {
      "id": "wait-assigned-shell-lead",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,Country,Region__c,LastName,Company,OwnerId FROM Lead WHERE HubSpot_Id__c='{{steps.create-nameless-contact.response.body.id}}'",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 240000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.create-nameless-contact.response.body.id}}",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true,
          "foreignClaimedByPattern": "(MacBook|\\\\.local)"
        }
      },
      "assertions": [
        {
          "path": "response.body.records[0].Country",
          "equals": "Germany"
        },
        {
          "path": "response.body.records[0].Region__c",
          "equals": "{{steps.region.response.body.records[0].Region_Name__c}}"
        },
        {
          "path": "response.body.records[0].LastName",
          "equals": "HubSpot Lead"
        },
        {
          "path": "response.body.records[0].OwnerId",
          "exists": true
        },
        {
          "path": "response.body.records[0].OwnerId",
          "notEquals": "system"
        },
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        }
      ]
    },
    {
      "id": "assigned-owner-user",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,IsActive FROM User WHERE Id='{{steps.wait-assigned-shell-lead.response.body.records[0].OwnerId}}' AND IsActive=true LIMIT 2",
      "expectStatus": 200,
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].Id",
          "equals": "{{steps.wait-assigned-shell-lead.response.body.records[0].OwnerId}}"
        },
        {
          "path": "response.body.records[0].IsActive",
          "equals": true
        }
      ]
    },
    {
      "id": "final-owner-binding",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT CRMPrincipalId__c,HubSpotOwnerId__c FROM HubSpotOwnerBinding__c WHERE CRMPrincipalId__c='{{steps.wait-assigned-shell-lead.response.body.records[0].OwnerId}}' AND BindingStatus__c='bound' AND CRMPrincipalType__c='User' LIMIT 2",
      "expectStatus": 200,
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].CRMPrincipalId__c",
          "equals": "{{steps.assigned-owner-user.response.body.records[0].Id}}"
        },
        {
          "path": "response.body.records[0].HubSpotOwnerId__c",
          "matches": "^[1-9][0-9]*$"
        }
      ]
    },
    {
      "id": "assert-shell-lead-owner-shadow-written",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,LastName,OwnerId,HubSpot_Owner_Id__c FROM Lead WHERE HubSpot_Id__c='{{steps.create-nameless-contact.response.body.id}}'",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 120000,
        "intervalMs": 6000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records[0].LastName",
          "equals": "HubSpot Lead"
        },
        {
          "path": "response.body.records[0].OwnerId",
          "equals": "{{steps.assigned-owner-user.response.body.records[0].Id}}"
        },
        {
          "path": "response.body.records[0].HubSpot_Owner_Id__c",
          "equals": "{{steps.final-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        }
      ]
    },
    {
      "id": "assert-owner-writeback-command-finalized",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT State, ResponseStatus, RemoteId, JSON_UNQUOTE(JSON_EXTRACT(CONVERT(CanonicalPayload USING utf8mb4), '$.mappedFields.hubspot_owner_id')) AS DesiredHubSpotOwnerId, JSON_LENGTH(JSON_EXTRACT(CONVERT(CanonicalPayload USING utf8mb4), '$.mappedFields')) AS MappedFieldCount, CASE WHEN FailureCode IS NULL OR FailureCode = '' THEN 1 ELSE 0 END AS FailureCodeAbsent FROM `_System_IntegrationOutboundCommand` WHERE SourceObject = 'Lead' AND SourceRecordId = '{{steps.wait-assigned-shell-lead.response.body.records[0].Id}}' AND ConnectorOwnerKey = 'pingcap-hubspot-companion' AND FlowDeveloperName = 'HubSpot_Lead_Contact_Outbound_Update' AND FlowActionName = 'PushLeadToHubSpotContact' ORDER BY CreatedDate DESC LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 90000,
        "intervalMs": 5000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records[0].State",
          "equals": "succeeded"
        },
        {
          "path": "response.body.records[0].ResponseStatus",
          "numericEquals": 200
        },
        {
          "path": "response.body.records[0].RemoteId",
          "equals": "{{steps.create-nameless-contact.response.body.id}}"
        },
        {
          "path": "response.body.records[0].FailureCodeAbsent",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].DesiredHubSpotOwnerId",
          "equals": "{{steps.final-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        },
        {
          "path": "response.body.records[0].MappedFieldCount",
          "numericEquals": 1
        }
      ]
    },
    {
      "id": "assert-owner-pushed-without-placeholder-name",
      "system": "hubspot",
      "method": "GET",
      "path": "/crm/v3/objects/contacts/{{steps.create-nameless-contact.response.body.id}}?properties=hubspot_owner_id,firstname,lastname,company&propertiesWithHistory=hubspot_owner_id",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 120000,
        "intervalMs": 5000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.properties.hubspot_owner_id",
          "equals": "{{steps.final-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        },
        {
          "path": "response.body.properties.lastname",
          "exists": false
        },
        {
          "path": "response.body.properties.firstname",
          "exists": false
        },
        {
          "path": "response.body.properties.company",
          "exists": false
        },
        {
          "path": "response.body.properties.hubspot_owner_id",
          "notEquals": ""
        },
        {
          "path": "response.body.propertiesWithHistory.hubspot_owner_id",
          "arrayContainsObject": {
            "value": "{{steps.final-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
          }
        }
      ]
    },
    {
      "id": "assert-final-owner-source-stable",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,OwnerId,HubSpot_Owner_Id__c,Country,Region__c,Company,LastName FROM Lead WHERE HubSpot_Id__c='{{steps.create-nameless-contact.response.body.id}}'",
      "expectStatus": 200,
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records.length",
          "numericEquals": 1
        },
        {
          "path": "response.body.records[0].Id",
          "equals": "{{steps.wait-assigned-shell-lead.response.body.records[0].Id}}"
        },
        {
          "path": "response.body.records[0].OwnerId",
          "equals": "{{steps.assigned-owner-user.response.body.records[0].Id}}"
        },
        {
          "path": "response.body.records[0].HubSpot_Owner_Id__c",
          "equals": "{{steps.final-owner-binding.response.body.records[0].HubSpotOwnerId__c}}"
        },
        {
          "path": "response.body.records[0].Country",
          "equals": "Germany"
        },
        {
          "path": "response.body.records[0].Region__c",
          "equals": "{{steps.region.response.body.records[0].Region_Name__c}}"
        },
        {
          "path": "response.body.records[0].Company",
          "equals": "HubSpot Lead"
        },
        {
          "path": "response.body.records[0].LastName",
          "equals": "HubSpot Lead"
        }
      ]
    }
  ]
}
