worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Expose a per-worktree last-activity timestamp (worktree.modified_at) in the list JSON

Open luiul opened this issue 1 month ago • 1 comments

Motivation

wt list is the worktree source of truth for tooling built on wt, but one fact downstream tools keep having to compute for themselves: when was this worktree last worked on. The JSON schemas report the HEAD commit time, yet a worktree with fresh uncommitted edits and an old HEAD is active, not stale. Dashboards and cleanup tools built on wt (I maintain understory, a live radar over every worktree, and coppice, a path-based CLI) all need this and currently each shell out to git status again per worktree, per poll, duplicating a subprocess wt has already run.

Request

Expose a per-worktree last-activity timestamp in the list JSON, for example worktree.modified_at in schema 2, defined as the later of:

  • head.committed_at, and
  • the newest mtime among the changed or untracked files git status reports.

For schema 2's no-value semantics: absent on branch-only rows, null when not determined.

Cost

Effectively zero. wt list already runs git status inside every worktree to produce the changes counts, so it has already enumerated exactly the files whose mtimes are needed; stat-ing those same records adds no subprocess. The win is measurable on the consumer side: on macOS with endpoint-security process monitoring, one extra git status spawn costs ~250 ms of wall time per worktree, and a dashboard polling every 15 s currently pays it once per dirty worktree on top of wt's own work.

Use cases

  • Dashboards sort or dim worktrees by inactivity instead of creation age, and a "Modified" column next to "Created" answers "when did someone last work here".
  • Cleanup tools sweep by inactivity: a worktree nobody has touched for two weeks is a removal candidate even when its directory is young, and vice versa.
  • Sorting support for wt list (#3922) could use it as a key.

Notes

I have this working in two downstream tools (each computing the value locally today, from git status --porcelain -z --untracked-files=all --no-renames), and the definition above is the one they converged on. A clean worktree can take the commit time without walking anything, so the common case needs no extra work at all.

Would this fit the schema's direction? I'd be glad to look into a PR if one is welcome.

luiul avatar Oct 19 '26 16:10 luiul

This fits schema 2's shape. A sibling of committed_at on JsonWorktreeV2 would follow the existing absent/null rules. The value could be computed in compute_working_tree_diff, which already holds the status output. One part of the cost claim doesn't hold, though: the status wt runs and the one your tools run don't list the same records.

wt's shared status call (status_porcelain_cached) uses --untracked-files=normal, not =all. Under normal, a new untracked directory shows up as a single d/ entry, and the directory's mtime doesn't change when a file inside it is edited. I checked this in a scratch repo. With d/sub/x untracked and every mtime set to 2020, editing x left d's mtime at 2020, and git status --porcelain --untracked-files=normal still printed only ?? d/. So reusing wt's records as they are would report a stale time for a worktree whose only activity is inside a new directory, which is the "fresh edits, old HEAD" case the field is meant to catch. Getting the definition you converged on would need one of these:

  • walk each collapsed untracked directory (extra filesystem I/O, but no extra subprocess), or
  • switch the shared call to =all, which makes status slower on worktrees with large untracked trees and affects every wt list, not only JSON consumers.

There are smaller details too. Deleted paths have no mtime. The output isn't -z, so quoted paths would need unquoting. A rename entry names two paths. None of these block the feature.

Whether wt list should take on this field, and which of the two untracked-directory options is acceptable, is a maintainer decision. A maintainer would be the one to confirm whether a PR is welcome.

worktrunk-bot avatar Oct 19 '26 16:10 worktrunk-bot