#19504: fix(gateway): avoid stale running status from Windows Scheduled Task
gateway
size: S
Cluster:
Cross-Platform Fixes
Fix stale 'running' reports from schtasks by validating Last Run Result (0x41301/267009) before treating runtime as active. Includes unit tests.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes a real-world bug where Windows Scheduled Tasks can report a stale `Running` status in `schtasks /Query` output even after the process has already exited. The fix validates the `Last Run Result` field against the well-known "task currently running" code (`0x41301` / `267009`) before trusting the `Running` status, and downgrades to `stopped` with a descriptive detail message when the codes don't match.
Key changes:
- Adds `normalizeTaskResultCode` to canonicalize both hex (`0x41301`) and decimal (`267009`) representations of result codes for reliable comparison
- Adds `deriveScheduledTaskRuntimeStatus` (exported and unit-tested) to encapsulate the stale-state logic, replacing the previous one-liner in `readScheduledTaskRuntime`
- `readScheduledTaskRuntime` now spreads `derived.detail` into its return value when the stale condition is detected, giving callers visibility into why status was overridden
- Three new unit tests cover the hex, decimal, and stale-detection paths
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge; it tightens a runtime status check without altering any installation, scheduling, or process-management logic.
- The change is narrowly scoped: it adds a new exported pure function and reroutes a single-line status derivation through it. The normalization logic correctly handles both hex and decimal input, and the fallback when `lastRunResult` is absent (trusting "Running") is sound. The only gap is a missing test for the `Running` + no-`lastRunResult` case, which is a minor coverage concern rather than a correctness risk.
- No files require special attention; `src/daemon/schtasks.test.ts` could benefit from one additional edge-case test.
<sub>Last reviewed commit: 77f901a</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#5496: Fix: Windows path separators stripped in Gateway scheduled task
by giuliozelante · 2026-01-31
80.6%
#5077: fix(windows): implement reliable gateway restart via schtasks helper
by romeoscript · 2026-01-31
78.4%
#5046: schtasks: add helpful hint for 'stub received bad data' error
by shayan919293 · 2026-01-31
78.4%
#18952: fix: sanitize schtasks env vars to prevent CRLF command injection
by coygeek · 2026-02-17
75.7%
#17949: fix: clear stale runningAtMs in cron.run() before already-running c...
by yasumorishima · 2026-02-16
75.2%
#17643: fix: clear stale runningAtMs in cron.run to allow manual triggers
by MisterGuy420 · 2026-02-16
75.1%
#17664: fix(cron): detect and clear stale runningAtMs marker in manual run ...
by echoVic · 2026-02-16
75.0%
#19414: fix: respect job timeoutSeconds for stuck runningAtMs detection
by namabile · 2026-02-17
74.9%
#17862: fix: Windows exec returns empty output when pty=false
by MisterGuy420 · 2026-02-16
74.9%
#23113: fix: show actual runtime version after update/restart when service ...
by BryanTegomoh · 2026-02-22
74.7%