Experience
Projects
My Stack
Case studies
Contact me
Open my CV

·6 min read

DailyBrief rebuilt every project card every 30 minutes, so I added a fingerprint cache

Every 30 minutes DailyBrief rebuilds a card for each project on my disk, and for every card it was running git log, git ls-files and a TODO scan whether or not I had touched that project since the last run. Most of the time I had not. The work was being thrown away and redone, so I made the rebuild incremental. The interesting part was not the cache. It was deciding what counts as "unchanged".

Project
DailyBrief
Date
Oct 2026
Stack
PowerShell 5.1, Git, JSON
Commit
00946c7

What was expensive

The script walks the projects the activity monitor has seen and builds one card per project: name, branch, last commits, dirty files, recently changed files, stack, readme head, package description and a TODO sample. Some of that is cheap because the data is already in memory from the monitor's event log. Some of it is not:

  • git log for the last commits and the last commit time
  • git ls-files plus a scan of code files for TODO markers (the script already avoids a plain -Recurse, because that walks node_modules and .next)
  • reading the readme and package description from disk

None of that changes unless the repository changed. That is the whole premise of the cache.

A fingerprint that is not fooled by git status

The obvious fingerprint for a git project is HEAD plus git status --porcelain. It has a hole: status prints M x whether you edited x once or ten times. A project where I keep editing the same already-dirty file would look frozen, and its TODO sample would go stale while I typed new TODOs.

So the fingerprint also includes the modification time of every dirty file, capped at the first 50:

powershell
$mt = @($dirty | Select-Object -First 50 | ForEach-Object {
    $rel = ([string]$_)
    if ($rel.Length -gt 3) { $rel = $rel.Substring(3) }
    if ($rel -match ' -> ') { $rel = ($rel -split ' -> ')[-1] }
    $rel = $rel.Trim('"')
    $fi = Get-Item -LiteralPath (Join-Path $dir $rel) -ErrorAction SilentlyContinue
    if ($fi) { [string]$fi.LastWriteTimeUtc.Ticks } else { '0' }
})
$fp = 'g|' + $headSha + '|' + $branch + '|' + (Hash-Text ((@($dirty) + @($mt)) -join "`n")) + '|' + $todoEligible

The status lines have a two-character code and a space in front of the path, hence Substring(3). A rename shows up as old -> new, so the part after the arrow is the file that exists. Porcelain also quotes paths with unusual characters, so the quotes are trimmed. A file that has been deleted gets '0', which is stable and still changes the hash when the file disappears.

$todoEligible is in the fingerprint on purpose. A repository only gets a TODO scan when it has recent activity or dirty files; when that flips, the card has to be rebuilt even if HEAD did not move.

A plain folder, with no git, gets a cheaper rule: the newest mtime among the folder and its top-level children. Its card only reads top-level files, so a deeper change cannot alter the card.

Reuse the slow fields, recompute the fast ones

A matching fingerprint does not mean the whole card is copied. Activity fields come from data already in hand, so they are always fresh:

powershell
@{
    name         = $label
    dir          = $dir
    isGit        = $isGit
    branch       = $branch
    lastCommits  = (AsStrArr $prev.lastCommits)
    lastCommitAt = [string]$prev.lastCommitAt
    dirtyCount   = $dirtyCount
    dirtySample  = $dirtySample
    changed14d   = $recent.Count
    recentFiles  = $recentFiles
    lastTouched  = $lastTouched
    readme       = [string]$prev.readme
    todoSample   = (AsStrArr $prev.todoSample)
}

changed14d, recentFiles, lastTouched and the dirty list are computed this run. lastCommits, readme, pkgDesc and the TODO fields come from the previous projects.json. A reused card is therefore never stale on what moved, only potentially stale on things the fingerprint says did not move.

While I was in there I also collapsed three git calls into one. git rev-parse --is-inside-work-tree HEAD --abbrev-ref HEAD answers "is this a repo", "what is HEAD" and "what branch" in a single process, and a repository with no commits yet fails on the HEAD part but still counts as git. In the same commit, the event accumulator changed from $events += $e to a List, because array += copies the whole array per event and two weeks of monitor log is tens of thousands of lines.

The part that took longer: serializing a reused card

PowerShell 5.1 does odd things with empty arrays. An empty array inside an object round-trips through JSON as {}, or as $null, and a one-element array can unroll into a bare string. A fresh card goes through the code that builds it. A reused card goes through ConvertFrom-Json first. If the two disagree on shape, downstream readers see a card that changes shape depending on whether the cache hit.

Two small helpers handle it:

powershell
function AsStrArr($v) {
    if ($null -eq $v) { return ,@() }
    if ($v -is [PSCustomObject] -and @($v.PSObject.Properties).Count -eq 0) { return ,@() }
    return ,@(@($v) | ForEach-Object { [string]$_ })
}
function Same-AsDetectStack($a) { return @($a) }

The leading comma in return ,@() stops the pipeline from unrolling the array on return. Same-AsDetectStack exists for one odd reason, recorded in a comment: the original stack detector returns with a plain return @(...), which unrolls, so a fresh card carries a one-item stack as a bare string. Readers already tolerate that, but the reused card has to serialize the same way, so it goes through a function with the same kind of return. I would rather copy a quirk deliberately than have two code paths that differ by accident.

Ordering the writes

projects-cache.json is written after projects.json:

powershell
[IO.File]::WriteAllText($outFile, ($out | ConvertTo-Json -Depth 6), $utf8)
# Written after projects.json on purpose: a crash in between leaves an old
# cache, which only costs one full rebuild, never a reused card that is wrong.
[IO.File]::WriteAllText($cachePath, (@{ version = 1; fp = $newFp } | ConvertTo-Json -Depth 3 -Compress), $utf8)

If the process dies between the two writes, the fingerprints on disk are older than the cards. Old fingerprints do not match, so the next run rebuilds. The opposite order could leave new fingerprints next to old cards, and that would reuse something wrong. The cache also fails soft: if it is missing or unreadable, $prevFp stays empty and everything is rebuilt.

What I took from it

I did not benchmark the old run against the new one, so I will not put a number on the saving. What I can say is what the change removes: for a project whose fingerprint did not change, no git log, no git ls-files, no TODO scan and no file reads for the readme. The lesson is about the key, not the cache. git status alone looked like a good fingerprint and was not, and the mtimes of the dirty files fixed the hole. Whatever you cache, write down what the key cannot see, and then add the cheapest signal that covers it.

#powershell#caching#git#performance

Keep reading

Have a project in mind?