Cancel stale pending MDM profile commands when label scope changes
Goal
When label membership changes a host's desired configuration-profile scope, Fleet should cancel or supersede any pending MDM profile command that no longer matches the new desired state.
This should work regardless of how the targeting is expressed (labels_include_any, labels_include_all, or labels_exclude_any) or what kind of label changed.
Actual behavior
An InstallProfile command can remain queued after a host stops matching a profile's label targeting. When the host later checks in, Fleet delivers the stale command even though the profile is no longer in the host's desired state.
This can leave an orphaned profile on the device: Fleet no longer considers the profile assigned or installed, so normal reconciliation may not send RemoveProfile. An administrator then has to identify the payload identifier and send a custom removal command.
Example reproduction
- Configure an Apple profile with an include label and/or an exclusion label.
- Put a test device in scope and keep it offline long enough for
InstallProfileto become pending. - Before the device checks in, change label membership so the device is no longer in the profile's desired target set (remove the include label or add the exclusion label).
- Allow profile reconciliation to run.
- Bring the device online and observe whether the old pending command is delivered.
The inverse should also be tested with a pending RemoveProfile: return the device to the desired target set before it checks in and verify that the stale removal does not run.
The MDM command history currently emphasizes completion/delivery time rather than clearly exposing the original queue time, which can make this failure mode difficult to diagnose after the fact.
Expected behavior
Whenever profile targeting or label membership changes, Fleet's profile reconciliation should reevaluate pending commands for affected hosts:
- If a host leaves the desired target set, disable/cancel a pending
InstallProfilecommand before it can be delivered. - If a host re-enters the desired target set, disable/cancel a pending
RemoveProfilecommand that is no longer appropriate. - If Fleet cannot prove whether a superseded command was already delivered (for example, delivered but not yet acknowledged), converge safely by issuing the appropriate compensating command after the response or timeout.
- Update the host/profile status and upcoming MDM command list so canceled commands no longer appear as actionable pending work.
Suggested acceptance criteria
- Removing an include-label membership cancels a pending
InstallProfilefor that host. - Adding an exclude-label membership cancels a pending
InstallProfilefor that host. - Adding an include-label membership or removing an exclude-label membership cancels a stale pending
RemoveProfilewhen the profile is desired again. - Behavior is covered for manual, dynamic, host-vitals, and built-in labels where applicable.
- The result is the same whether membership changes through GitOps, the API/UI, an automated label sync, or enrollment/re-enrollment reconciliation.
- A device that checks in after the scope change never applies a superseded command.
- The delivered-but-unacknowledged race converges to the latest desired state without leaving an orphaned profile.
- The MDM command API/activity details expose both queue time and completion time, plus a canceled/superseded status and reason.
Related work
- #25549 added cancellation of pending configuration-profile installs when the profile itself is deleted. Its QA notes say label changes are not canceled immediately and instead rely on profile reconciliation. This request extends the same safety behavior to changes in label-derived desired state.
- #34704 exposes upcoming and past Apple MDM commands, which makes stale pending commands visible but does not prevent them from running.
Why this matters
Configuration profiles can change networking, certificates, restrictions, security controls, or authentication behavior. Applying a command after the host has explicitly left scope can disrupt or lock out a device, and the resulting orphan may not be recoverable through normal Fleet reconciliation.
Should be resolved in https://github.com/fleetdm/fleet/pull/50044 - I'll test after that's released.
Stale commands cleared, Profiles in sync, no ghost left. Fleet's harmony upheld.
Cloud city hums along, Devices secured, workflows smooth, No orphans in song.