Your Agent Job Said OK. The Working Tree Was Dirty and Nothing Shipped.

You open the site. Yesterday’s post is still the newest one. The agent log from this morning says the job finished. Exit code 0. Duration looked normal. No error in Discord. Nothing on the blog.

That mismatch is one of the most frustrating failures on a self-hosted agent box, because every signal you usually trust says the work happened. The work did not happen. What happened is a dirty git working tree quietly blocked the write, and the job treated “I did not touch anything” as success.

We hit this on MarketMai’s own pipeline. A daily blog run on a home Raspberry Pi left a staged, uncommitted post behind. The next mornings the job saw a dirty tree, refused to create another file on top of it, and still reported ok. From outside, it looked like the scheduler was healthy and the content pipeline had simply “missed a day.” From inside, the box was stuck on yesterday’s unfinished commit.

Here is how to find that failure mode, make it loud, and keep a dirty tree from silently eating a publish window.

Why a dirty tree looks like a healthy job

Agent jobs inherit habits from human git workflows. A human opens a dirty tree, decides what to keep, and finishes the commit. An unattended job usually does one of three things when it finds unexpected files:

  1. Refuse to write. The safest scripts check git status --porcelain and exit before creating a new file. If that exit is coded as “nothing to do” or as a soft skip, the outer cron still sees success.
  2. Write anyway. The job creates today’s file next to the leftover, and now you have two unpublished drafts and a messier tree.
  3. Commit everything. The job runs git add -A and ships the leftover plus today’s work in one commit, often with the wrong title, date, or author story.

None of those paths look like a crash. Logs stay calm. Dashboards stay green. The only people who notice are the ones waiting for the output.

Step 1: Make “clean tree” a hard preflight

Before your agent creates, edits, or commits anything in a repo, require a clean working tree. Put this at the top of the publish script, not inside a prompt:

#!/usr/bin/env bash
set -euo pipefail

cd "$REPO_DIR"

if [[ -n "$(git status --porcelain)" ]]; then
  echo "REFUSE: dirty working tree; refusing to publish" >&2
  git status --porcelain >&2
  exit 2
fi

Three details matter:

  • Exit non-zero. Soft skips teach the scheduler that the morning was fine. Use a dedicated code (like 2) so alerts can treat it differently from a normal failure.
  • Print the porcelain lines. You need the exact leftover path in the alert, not a generic “dirty” message.
  • Run this before the model runs. If the LLM starts drafting first and the git check happens later, you have already spent tokens and time on work you cannot safely land.

A clean-tree preflight turns a silent miss into a loud miss. Loud misses get fixed. Silent ones repeat.

Step 2: Separate “draft on disk” from “committed to main”

Many agent publish flows mix three jobs: generate the file, commit it, push it. When any step fails halfway, the tree becomes the bug. Split them:

  1. Generate into a staging path outside the repo, or into a known draft directory that git ignores.
  2. Validate the file (frontmatter, date, slug uniqueness, link checks) while it is still outside git’s index.
  3. Copy into src/content/..., commit that one path, and push.

If step 3 fails, you still have the draft without a half-staged repo. For MarketMai, the content landed inside the repo and never got committed, so the next run’s safety check blocked every later morning. External staging would have kept the repo clean even when the commit step failed.

Step 3: Alert on dirty trees the same way you alert on crashes

If the only place a dirty tree shows up is inside a log file on the Pi, you will find it the same way we did: by noticing the public output stopped.

Wire the refuse path to the same channel you use for failed jobs:

  • Discord, Slack, or email with the porcelain output
  • The hostname and repo path
  • The scheduled job name, so you know which routine is stuck
  • A one-line recovery hint: ssh … 'cd repo && git status'

Do not wait for the “did today’s post go live?” check at 8:15 to be your only signal. That check is still worth having. It should be the backup, not the first alarm.

Step 4: Recover without inventing a second publish

When you find a dirty tree that blocked publishes, resist the urge to let the agent “just write today’s post now.” First decide what the leftover file is.

cd "$REPO_DIR"
git status
git diff --staged
git diff

Then pick one path:

  • Leftover is good and unfinished. Finish it: set the correct pubDate, commit only that file, push, confirm it is live, then let the normal schedule handle the next day.
  • Leftover is stale or wrong. Remove it or reset it on purpose (git restore --staged --worktree -- path or delete the file), confirm git status is clean, then run today’s job once.
  • Leftover is today’s file from a half-run. Do not generate a second one. Commit the existing file if it is valid; replace it if it is not.

The rule is: one publish window gets one file. Cleaning up by generating another draft is how you recreate the duplicate-output problem on a different axis.

Step 5: Add a “tree stayed clean” check after the job

Preflight catches dirt that already existed. Postflight catches dirt the job itself left behind.

# after commit + push
if [[ -n "$(git status --porcelain)" ]]; then
  echo "FAIL: publish left a dirty tree" >&2
  git status --porcelain >&2
  exit 3
fi

This is the check that would have caught our October miss on the same morning it happened. The job created a file, failed to finish the commit for some reason, and walked away. A postflight dirty-tree assertion would have failed the run instead of leaving a landmine for tomorrow.

Also log the commit SHA the job produced. “ok” without a SHA is how a no-op starts looking like a publish.

Step 6: Keep interactive fixes from leaving the same trap

Humans cause dirty trees too. You SSH in to patch a script, start a manual edit, get pulled away, and leave main mid-change. The next automated run then refuses to publish, or worse, commits your half-edit.

Use a worktree or branch for manual edits, and run git status before you disconnect. If it is dirty, finish the commit or restore the files. Treat a dirty main on an agent box like an unfinished deploy, especially when remote access needs an approval step and cleanup is easy to postpone.

What this doesn’t fix

A clean git tree does not prove the live site updated. CDN lag, a failed Actions deploy, a wrong slug, or an accidental noindex flag can still hide a good commit. Keep the live URL and sitemap checks. Clean-tree guards stop one specific lie: the job claiming success while refusing to write. They do not replace lockfiles, canaries, or duplicate-scheduler audits.

The short version

If your agent publishes through git, a dirty working tree is a production incident, not a cosmetic status. Fail closed when the tree is dirty before the job starts. Fail closed if the job leaves the tree dirty after it finishes. Alert with the porcelain paths. Recover the leftover file on purpose instead of generating a second draft. And never let “exit 0 with no commit SHA” count as a successful publish.

Put the refuse path in writing before the next miss

You can turn this into a standing check with MarketMai’s free AI prompt pack: adapt one prompt into a “list every repo this agent commits to, show git status --porcelain for each, and refuse any publish while the output is non-empty” routine, then review the first few alerts by hand. For publish preflights, cron wrappers, and ops checklists built for self-hosted agents, the OpenClaw Agent Ops Toolkit has the full set, and the MarketMai Ultimate Bundle puts 13 prompt, automation, and agent-operations products in one $49 download.

More from the build log

Suggested

Want the full MarketMai stack?

Get the core MarketMai guides and operator playbooks in one premium bundle for $49.

View Bundle