Experience
Projects
My Stack
Case studies
Contact me
Open my CV

·5 min read

Letting an AI edit a real repo: its own branch, and git's stderr

Every other run in DailyBrief reads a digest and hands back a patch for the board. The execute run is different: it gets Bash and Edit inside a real project folder and writes code. The interesting engineering was not the prompt. It was the three things that stand between that model and my uncommitted work, and the PowerShell quirk that nearly broke the second of them.

Project
DailyBrief
Date
Aug 2026
Stack
Electron, PowerShell, Git, Claude CLI
Commit
e4b3a59

Why this run is the dangerous one

DailyBrief is a desktop app that keeps a kanban board of my tasks, each linked to a project folder. Until this commit, every AI run rewrote the board and nothing else. The worst outcome was a bad card. The execute run changes that: press the button, or write a comment on a card, and the model works in the linked folder and edits files.

That raises one question: if the model does something wrong in a folder where I already have half-finished changes, how do I get back? The commit message gives the design in three guards, and I will go through them in the order the script applies them.

Guard one: no git, no run

powershell
$isGit = (Git-Quiet @('-C', $dir, 'rev-parse', '--is-inside-work-tree')) -eq 'true'
if (-not $isGit) {
    [IO.File]::WriteAllText($log, 'skipped: not a git repo - refusing to edit without an undo path', $utf8NoBom)
    Post-Note $STR.noGit ''
    exit 0
}

Without git there is no diff and no way back, so the script exits before any model call. The other early exits log that zero AI tokens were spent. The ones that concern the user, such as a missing folder or a non-git folder, also post a note to the task's comment thread through Post-Note, because a card that silently did nothing is worse than one that says why.

Guard two: a branch of its own

powershell
$branch = 'dailybrief/' + $TaskId
$exists = Git-Quiet @('-C', $dir, 'rev-parse', '--verify', '--quiet', $branch)
if ($exists) { $null = Git-Quiet @('-C', $dir, 'checkout', $branch) }
else { $null = Git-Quiet @('-C', $dir, 'checkout', '-b', $branch) }
$nowBranch = Git-Quiet @('-C', $dir, 'rev-parse', '--abbrev-ref', 'HEAD')
if ($nowBranch -ne $branch) { ... skipped ... }

This is the part I like. A checkout carries uncommitted changes along when they do not conflict. So the work I had in progress is still there on the new branch, and whatever the model adds sits on top of it. When the run is over, git checkout - returns to the branch I started from, and the model's edits are no longer in the working tree. The comment in the script says it directly: uncommitted work travels with the checkout, which is what we want.

The script does not trust the checkout to have worked. It reads the current branch back and only continues if it equals the expected name; otherwise it records that it could not switch and stops without spending tokens. If the branch already exists from an earlier run on the same task, it reuses it.

The README entry I wrote with the feature states the guarantee carefully: git checkout - undoes everything, including work that was already uncommitted. I want to be precise about what that means. Edits to files that were already dirty are mixed with my own, so the undo is of the branch, not of individual hunks. The review path is the working tree diff in VS Code, which the script opens before the model starts so that Source Control shows changes as they land.

Guard three: the prompt

The prompt forbids git commit, push, merge, rebase, tags, branch switching, stash, reset, checkout -- <file> and clean, plus mass deletion, secrets and deploys. The stated reason for the second group is that I have uncommitted work in the folder and those commands can erase it. A prompt is not an enforcement mechanism, and I do not treat it as one; it is the third layer behind two that are code. The run is also capped with --max-turns 40, which the script's own comment calls the real cost ceiling, since every turn can read files and run commands.

The stderr trap

The quirk was in the git calls themselves. The script runs with $ErrorActionPreference = 'Stop'. Git prints messages such as "Switched to a new branch" on stderr even when it succeeds. In PowerShell 5.1, native stderr output that gets redirected can surface as a NativeCommandError under that preference, so a successful checkout -b could terminate the run. The code comment describes exactly that failure: the run died right after creating its branch.

The wrapper that fixes it is small:

powershell
$prev = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
    $out = & git @gitArgs 2>&1 | ForEach-Object { [string]$_ }
    return (($out -join "`n")).Trim()
} finally {
    $ErrorActionPreference = $prev
}

It relaxes the preference for the duration of one git call, merges stderr into the output as strings, and restores the previous value. Success is then decided by reading state back, as with the branch name, not by exit behaviour or error records.

What it reports

After the run, the script compares the count of lines from git status --porcelain before and after, ticks off only the checklist steps the model listed as completed in its summary file, and appends a comment to the card. It never marks the task done, because closing it is my call after reading the diff. The result merge takes the same named mutex as the other board writers, so a parallel check cannot overwrite it.

Takeaway

Safety for an agent with write access was mostly git facts: checkouts carry dirty state, checkout - is cheap, and a diff is the review tool. The code is a few dozen lines of guards, and the one bug worth remembering was a shell treating a progress message as a failure.

#powershell#git#ai-agents#safety

Keep reading

Have a project in mind?