← Back to Insights

Insight

Rules That Run, Part 10: The Task That Never Ran

Ariel Agor
Updated
Proving a Windows scheduled task actually runs. A queued task can still read as a pass. Registering is not proving. The battery gate, The window that flashes, Registering a task properly, Four checks after registering.

Listen · Read by Ariel

0:00 / n/a

Rules That Run is a ten-part series on the rules that keep an AI agent setup honest. This is part 10.

Every unattended job in this series runs on a schedule. Agents draft posts, sweep repos, check inboxes and refresh data while nobody is watching. On my machine the scheduler is Windows Task Scheduler, and it has a failure mode that looks exactly like success.

This part is Windows-specific. The settings and result codes below belong to Task Scheduler. The general lesson, that registering a job is not proving it runs, applies just as much to cron and launchd, but I have not measured their failure modes, so I will not describe them.

The battery gate

New-ScheduledTaskSettingsSet sets two options to true by default: DisallowStartIfOnBatteries and StopIfGoingOnBatteries. On a desktop they never matter. On a laptop they decide whether your jobs run.

With the first one set, a task whose trigger fires while the laptop is on battery does not start. It sits in the Queued state and never runs. Meanwhile LastTaskResult keeps showing the previous result. If the last run succeeded, it shows 0, which looks like a pass. A dashboard that reads LastTaskResult reports a healthy job that has stopped running.

The second option is worse in a different way. It kills a running task when the laptop is unplugged. The task stops partway, which can leave half-written files behind for the next run to trip over.

There is one honest signal in the result codes. A task that has never run shows 267011. That catches a brand-new task that was never triggered. It does not catch an old task that ran before and then went quiet, which is exactly the case the battery gate produces.

On September 18, 2026, a sweep of this machine found 57 of 169 non-vendor tasks carrying the battery gate. They included the backup job and the job that handles inbound bookings. 50 were fixed that day. A task held back this way still shows its last good result, so a check that reads only LastTaskResult would have passed every one of them.

The window that flashes

The second problem looks cosmetic. A task registered with an interactive logon runs in the logged-on user's session, and a console job flashes a window on screen when it starts. Setting the task's Hidden flag does not stop that. On a machine someone is working at, a frequent job becomes a window that keeps appearing over their work, and a window that appears at random teaches people to close windows without reading them.

The fix is the S4U logon type, which the scheduler UI labels "run whether user is logged on or not." It runs the task without a stored password and outside the interactive session, so nothing appears on screen.

S4U has a cost. The task runs in a clean environment without a terminal, which matters for the next two rules.

Registering a task properly

This is the shape every task on this machine is registered with:

$action    = New-ScheduledTaskAction -Execute '<path to node.exe>' -Argument '<repo>\scripts\job.mjs'
$trigger   = New-ScheduledTaskTrigger -Daily -At '<time>'
$principal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType S4U
$settings  = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -Hidden

Register-ScheduledTask -TaskName '<name>' -Action $action -Trigger $trigger `
  -Principal $principal -Settings $settings

Two details in the action line come from failures.

Call the local binary, never npx. Under S4U there is no TTY, and npx is not a safe thing to run there. Point the action at the node executable and the script directly, or at the project's local binary.

Load the env file explicitly. A scheduled job does not inherit the variables your terminal has. All social posting here was skipped for a week, May 16-23, 2026. Each run failed with "no token," because the clean scheduler environment never loaded the file holding it. The job now reads its env file itself at startup instead of assuming the variables are there.

Registering is not proving

Register-ScheduledTask succeeding tells you the scheduler accepted a definition. It says nothing about whether the job will start, finish or do anything useful. So every registration ends with a proof:

$name   = '<name>'
$before = (Get-ScheduledTaskInfo -TaskName $name).LastRunTime
Start-ScheduledTask -TaskName $name

$deadline = (Get-Date).AddMinutes($TimeoutMinutes)
do {
  Start-Sleep -Seconds $PollSeconds
  $state = (Get-ScheduledTask -TaskName $name).State
} while ($state -in 'Running', 'Queued' -and (Get-Date) -lt $deadline)

$info = Get-ScheduledTaskInfo -TaskName $name
if ($state -eq 'Queued')           { throw "stuck in Queued: check the battery settings" }
if ($info.LastRunTime -le $before) { throw "did not run" }
if ($info.LastTaskResult -ne 0)    { throw "failed with $($info.LastTaskResult)" }
# Last step: check the side effect this run should have produced.

Four checks, each covering a different lie:

  1. Start it now. Waiting for the trigger means finding out tomorrow.
  2. Poll until the state leaves Running or Queued. A task stuck in Queued is the battery gate, and the poll should say so instead of waiting forever.
  3. Assert LastTaskResult, and check that LastRunTime moved. A 0 left over from an older run is the trap described above. The run time changing is what proves this run happened.
  4. Check the real side effect. A result of 0 means the process exited cleanly. It does not mean it did its job. The cheapest side effect to check is a log file the run should have written, with today's entry in it.

Step 4 is the same rule as part 5. Exit codes are self-report. The log line, the row in the database, the post on the live site: those are evidence.

The guard that keeps it fixed

Fixing 50 tasks once does nothing about the next one to arrive. New tasks come from setup scripts, installers and sessions that forgot the rules. So a daily guard task looks for any new task that would pop a window and converts it.

That is the pattern this whole series keeps arriving at. A one-time sweep fixes the state. A scheduled guard keeps fixing it, because the state drifts back.

Ten rules, one idea

The thesis of the series was that a rule written in an instruction file drifts, and a rule the harness enforces holds. Each part took one rule and moved it into something that runs:

This part added the last one: a scheduled task counts as working only after it has been made to run and its output has been checked.

Most of these mechanisms write a log, and the logs are where the honest numbers came from. The commit gate's overrides were frequent enough to need a weekly bypass metric. The finish gate in part 5 lets a stop through after blocking twice, and its log shows how often sessions walk past it. The promise hook's 1,787 blocks, as of November 18, 2026, count stops, not broken promises. None of those readings would exist without the logs, and each one is a measured answer to whether a rule holds.

Build it yourself

  1. Register every task with -LogonType S4U, -Hidden, -AllowStartIfOnBatteries and -DontStopIfGoingOnBatteries.
  2. Point actions at a local binary, never npx, and have the job load its own env file.
  3. After registering, start the task, poll until it leaves Running and Queued, and fail on a stuck Queued.
  4. Assert LastTaskResult and that LastRunTime moved, then check the real side effect.
  5. Sweep existing tasks for the battery gate, and add a daily guard that fixes new ones.
  6. On cron or launchd, write the same proof: trigger the job, confirm it ran, check what it produced.

If you want a second pair of eyes on an agent setup like this one, Agor AI reviews them.

Part 9: Off, Draft, Live

Want this kind of automation working for your business?

Agor AI designs and ships the systems these posts describe, scoped in weeks, not quarters.

Book a Free Strategy Call