{"record":{"id":"e13ddbba77e9f78d","repo":"itsfatduck/optimizerDuck","slug":"scheduled-task-verify-failed-at-fullpath-expected-enabled","errorCode":null,"errorMessage":"Scheduled task verify failed at {FullPath}: expected enabled={OriginalEnabled}, actual={state == TaskEnabledState.Enabled}","messagePattern":"Scheduled task verify failed at (.+?): expected enabled=(.+?), actual=(.+?)","errorType":"exception","errorClass":"StepExecutionException","httpStatus":null,"severity":"error","filePath":"optimizerDuck/Domain/Revert/Steps/ScheduledTaskRevertStep.cs","lineNumber":57,"sourceCode":"        var opCall = new OpCall { Logger = logger };\n        var result = OriginalEnabled\n            ? ScheduledTaskService.EnableTask(opCall, FullPath)\n            : ScheduledTaskService.DisableTask(opCall, FullPath);\n\n        if (!result.Ok)\n            throw new StepExecutionException(result.Error ?? Description, result.ErrorDetail);\n\n        // a missing task has nothing to restore; anything else must verify positively.\n        var state = ScheduledTaskService.GetTaskEnabledState(FullPath, opCall.Logger);\n        if (state is TaskEnabledState.NotFound)\n            return Task.FromResult(true);\n        if (state is not (TaskEnabledState.Enabled or TaskEnabledState.Disabled))\n            throw new StepExecutionException(\n                $\"Scheduled task verify failed at {FullPath}: could not query the task state.\",\n                null\n            );\n        if ((state == TaskEnabledState.Enabled) != OriginalEnabled)\n            throw new StepExecutionException(\n                $\"Scheduled task verify failed at {FullPath}: expected enabled={OriginalEnabled}, actual={state == TaskEnabledState.Enabled}\",\n                null\n            );\n\n        return Task.FromResult(true);\n    }\n\n    /// <inheritdoc />\n    public JObject ToData()\n    {\n        return new JObject\n        {\n            [nameof(FullPath)] = FullPath,\n            [nameof(OriginalEnabled)] = OriginalEnabled,\n        };\n    }\n\n    /// <summary>","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/itsfatduck/optimizerDuck/blob/36acf585ae122a09f0e29eb4acedfaf94e81c0de/optimizerDuck/Domain/Revert/Steps/ScheduledTaskRevertStep.cs#L39-L75","documentation":"ScheduledTaskRevertStep.ExecuteAsync throws this when the task state was queried successfully but the actual Enabled/Disabled state does not match OriginalEnabled recorded at apply time. The restore command ran, but read-back verification proves the effect did not stick.","triggerScenarios":"task.Enabled = true/false silently failed (e.g. task instance currently running with 'do not start on demand' semantics), Group Policy or another process re-disabled/re-enabled the task after the set, or the wrong task was targeted. The re-query returns Enabled/Disabled but opposite to OriginalEnabled.","commonSituations":"Bloatware task recreated by vendor software running in the background; corporate GPO forcing task state; task locked while running so the state change is deferred; mismatch between the id's stored OriginalEnabled and reality after manual changes.","solutions":["Re-run the revert; transient locks (task currently running) often clear once the task stops.","Stop the running task instance first (schtasks /end /tn <path>) then retry.","Check for Group Policy or vendor software re-applying the state and disable that source before reverting.","Manually set the task state with schtasks /change /tn <path> /enable|/disable to match the desired original state, then clean up the revert file if the state is now correct."],"exampleFix":"// before\ntask.Enabled = OriginalEnabled; // may not stick while task is running\n// after\nif (task.State == TaskState.Running) task.Stop();\ntask.Enabled = OriginalEnabled;\ntask.RegisterChanges();","handlingStrategy":"retry","validationCode":"var state = ScheduledTaskService.GetTaskEnabledState(fullPath, logger);\nif (state.State is TaskEnabledState.Unknown) return; // nothing verifiable","typeGuard":null,"tryCatchPattern":"catch (StepExecutionException ex) when (ex.Message.Contains(\"expected enabled=\"))\n{ await Task.Delay(1000); await RetryFailedStepsWithResultsAsync(); }","preventionTips":["Stop running task instances before changing Enabled","Disable Group Policy/vendor agents that re-assert task state","Revert soon after applying to reduce drift","Verify with GetTaskEnabledState before declaring success manually"],"tags":["csharp","scheduled-tasks","windows","revert","verification"],"backgroundTag":"invalid-state-transition","analyzedSha":"36acf585ae122a09f0e29eb4acedfaf94e81c0de","analyzedAt":"2026-09-13T08:30:13.845Z","contentChangedAt":"2026-09-13T08:30:13.845Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}