Root CauseWhat broke, why, and the fix.

A string comparison on log dates made a weekly job report FAIL every day

· monitoring, shell, logs
ⓘ Operated by TechAthletes. Every post here is a bug we hit in our own work — symptom, root cause, fix. Nothing is sponsored and we are not paid to mention any tool.

A weekly job that "never completed", every single day

We run a small health-check script over the logs of our scheduled jobs. One of those jobs, check-embeds-weekly, verifies that the embeds on our blog match a ledger. The health check reported it as FAIL every day, with the message "no completed run".

The job itself was fine. On 2026-09-24 it had finished with rc=0 and 36 items passing. The log showed it plainly:

2026-09-24 00:21:03	START	.../check-embeds.mjs --verify-youtube
ledger: 697 entries (697 lines, 0 broken)
articles: 36 embeds
RESULT: PASS (all 36 match the ledger)
2026-09-24 00:21:06	END	rc=0

So the monitor was wrong, and it was wrong in a specific way. It did not say "no run". It said "no completed run", which is a different branch of the script with a different meaning. The log directly contradicted it: a START line, a RESULT line reading PASS, and an END line with rc=0, all from the same run two days earlier. The question was how a log containing a completed run could be read as containing none.

The first diagnosis: the window is too narrow

The first explanation we reached for was about the time window. The health check looked at "the last N days" of each log, and for this job N was 1. The job runs weekly, on Monday at 07:00. A weekly job checked against a one-day window is always outside the window, even when it is perfectly healthy.

That is a real defect, and widening the window does make this one report go away. It is tempting to stop there. But as we will see, fixing only the window leaves the actual bug in place, and the actual bug is worse than a wrong window.

What the filter was really doing

To select "recent" lines, the script compared the first column of each log line against a date string:

recent=$(awk -v s="$since" '$1>=s' "$f")     # wrong

The intent is obvious: keep lines whose date is on or after since. The problem is that $1 >= s in awk is not a date comparison and not a type check. When $1 is not numeric it is a plain string comparison, byte by byte.

Our logs pass through a wrapper that captures the job's own output alongside the START and END markers. So the first column is not always a date. In the log above, three lines start with ledger:, articles: and RESULT:.

In ASCII, lowercase letters sort after digits. l is 0x6c and 2 is 0x32. That means any line whose first column starts with a letter compares as greater than any date string. With since set to 2026-09-25, the START and END lines from 2026-09-24 were correctly excluded, but the three body lines sailed through as "newer than 2026-09-25".

The consequence inside the script: recent was not empty, so the "no run at all" branch did not fire. But the filtered set contained zero START and zero END markers, so the script took the other branch and declared "no completed run". The body lines had smuggled themselves into the result and changed which error message came out.

Why the wrong message is the real damage

The annoying part is not that a message was wrong. The damaging part is that the correct classification stopped being produced.

The health check has a category for jobs with no recent execution. It is called DEAD. That is the category a job with no run in the window should land in. With the string-comparison bug, a job in that state is reported as "no completed run" instead, which is a different bucket. Three things follow:

  • A genuinely dead job is never counted as DEAD, because the body lines in its log always push it into the other branch.
  • Real DEAD cases are buried under the misclassified ones.
  • Because the false FAIL appears every day, people learn to skip over it. A monitor that cries wolf daily is a monitor nobody reads, and we have already been through this pattern more than once.

This is why "widen the window" was not enough. The window fix makes the weekly job stop showing up. It does nothing for the case where a job has not run in ten days and the log still contains body lines. That job would still be labelled "no completed run" rather than DEAD, and the real problem would keep hiding.

The fix: anchor on lines that are shaped like dates

The repair is to restrict the comparison to lines that actually begin with a date, and only then compare:

recent=$(awk -v s="$since" '/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] / && $1>=s' "$f")

The regular expression guarantees that $1 has the form we expect before >= is evaluated. Body lines, stack traces, and continuation lines no longer participate.

We also stopped hard-coding the window. The width now comes from each job's schedule: if the launchd StartCalendarInterval has a Weekday key, the window is 8 days; if it has a Day key, it is 32 days. The job's own cadence decides how long "recent" is.

Then we pinned the behaviour with negative tests in our test script for the health check:

Case Expected
Daily job whose log contains output lines (ledger:, RESULT:) and whose last run was 5 days ago DEAD (not "no completed run")
Weekly job that succeeded 3 days ago Not listed in details (OK)
Same log, but with the plist removed DEAD, proving the weekly rule was actually applied
Weekly job with no run in 10 days DEAD

The third row is the important one. If you only check that a healthy weekly job produces no complaint, you cannot tell whether it passed because the weekly logic worked or because the job was never processed at all. Running the same input with one condition changed, and watching the result flip, is what proves the check is discriminating rather than silent.

The same family, found the same day: git quotes non-ASCII paths

While building a version stamp for the blog, we listed the inputs committed at HEAD:

git ls-tree -r --name-only HEAD:<dir>      # paths get quoted

Twelve articles with Japanese filenames vanished from the list. With --name-only, git's default is to print non-ASCII paths quoted and octal-escaped, as "content/allowance/\343\201...". Our filter was f.startswith('content/'), and a path that begins with a double quote does not start with content/.

The count on the HEAD side came out as 237 files. The working tree side had 249. The script concluded that we were publishing from inputs that had not been committed. That was false. All twelve were committed; the enumeration had dropped them. Had we acted on the diagnosis and "fixed" it by committing, the repository history would carry a wrong explanation for a change that was never needed.

The fix is to ask git for NUL-separated output, which is never quoted:

git ls-tree -r -z --name-only HEAD:<dir>
git status --porcelain -z -uall

Pulling paths out with awk '{print $2}' is unsafe for the same reason: quotes and escapes break the column.

What to check in your own scripts

Both bugs share one shape: a text stream was assumed to have a uniform format in one column, and it did not.

  • A date column receives non-dates: job output, stack traces, continuation lines.
  • A path column receives quoting and escapes: non-ASCII, spaces, newlines.

In both cases the mismatch is silent. Values either pass when they should not or disappear when they should not. Neither awk nor git raises an error.

Two habits caught these for us, and both are cheap:

  1. Print how many items you looked at. We noticed both problems through count mismatches: zero START/END markers in a non-empty set, and 237 versus 249 files. A filter that reports its input and output sizes exposes this class of loss immediately.
  2. Feed it a line that must not pass, and confirm it does not. Before trusting a date filter, add one body line to the input and verify it is excluded. Before trusting a path listing, add one filename with a non-ASCII character and verify it is counted.

And keep the two fixes separate in your head. Making the symptom disappear is not the same as removing the cause. The window was wrong and the comparison was wrong; fixing only the first would have left the monitor quietly misfiling dead jobs under a different label.

Root Cause is written while shipping small software products — you can see them at TechAthletes.

All posts →

Get new posts by email

We email you only when a new post goes up here. You can unsubscribe at any time.

Privacy policy