Skip to content

perf(git): keep output that does not read the work tree past the TTL - #797

Closed
bvolpato wants to merge 2 commits into
sirmalloc:mainfrom
bvolpato:bvolpato/git-cache-stable-commands
Closed

bvolpato wants to merge 2 commits into
sirmalloc:mainfrom
bvolpato:bvolpato/git-cache-stable-commands

Conversation

@bvolpato

@bvolpato bvolpato commented Nov 3, 2026 •

Copy link
Copy Markdown

Relates to #397 and #384, and to item 2 of #571. #798 is a separate change that removes another git process in partial clones.

What

With Git Branch and Git Changes enabled in an unchanged repository, a render after Git Cache TTL expires starts two Git processes, down from four. The rendered output is the same.

Render main This branch
Empty cache 4 4
Within the TTL 0 0
TTL ended, nothing changed 4 2
.git/index changed 4 3
Branch switched 4 3

Why

Each Claude Code session starts the status line again and again, so the cost of each git process grows with the number of sessions. Starting a process is also much slower on some machines, for example when endpoint security software checks each one.

As #384 says, the TTL covers unstaged and untracked edits, which do not change the modification times of .git/HEAD or .git/index. Both diff commands follow the configured TTL. An unstaged .gitattributes edit can change the staged line counts that Git reports.

How

Git stays the source of truth. This only changes when git is asked again. Successful output is kept for up to five minutes in two cases:

  • Repository location checks (rev-parse --is-inside-work-tree, --show-toplevel, and --git-dir --git-common-dir) depend on where the directory is in the repository, and each persistent cache file already belongs to one git dir and directory. A change to HEAD or the index does not run them again.
  • Commands that read only HEAD, its ref, and the index (symbolic-ref HEAD, symbolic-ref --short HEAD, rev-parse --short HEAD, and ls-files --unmerged -z) run again when one of those changes.

git reset --soft moves the branch without touching .git/HEAD or .git/index, so these entries also record the modification times of the ref file and packed-refs. For a linked worktree, those files are in the main repository.

The plain TTL rules stay in these cases:

  • A TTL of 0, so its "no age-based expiry" meaning does not change.
  • Failed commands. Only a successful run records the cache stamp.
  • Ref-dependent commands in reftable repositories, where HEAD is a placeholder and refs are not in files.
  • Entries written or read with GIT_DIR, GIT_WORK_TREE, GIT_COMMON_DIR, or GIT_INDEX_FILE set. The cache stamp records whether the producer ran without these overrides.

Older cache entries follow the configured TTL until Git refreshes them and records the new stamp.

Downsides

  • A change that these checks cannot see can show for up to five minutes instead of up to the TTL. Examples are a config edit such as core.bare, and a branch that is a symbolic ref to another branch. The five-minute limit exists for these cases.
  • On a filesystem with coarse modification times, two index writes in one tick can look like one. The result can also be stale for up to five minutes.
  • Each cached git lookup reads HEAD and commondir and checks two more files. This starts no process.

What this does not change

diff --shortstat, diff --cached --shortstat, status, rev-list, and remote lookups keep the configured TTL.

I did not merge commands into one git status --porcelain=v2 --branch call as #571 suggests. #571 lists behavior changes for that, and it would replace commands that do not read the work tree with one that does.

Width detection is not part of this. #765 removed the ps and stty walk when COLUMNS is set, and #728 and #629 cover the rest.

Testing

On Linux with Bun 1.4.3 and Git 2.56, the cache tests cover both review cases. A Git wrapper around the built CLI confirmed the two-process render and identical output after the TTL. Lint, type checking, and the build completed on Linux. The real-Git cache tests also ran under Vitest on Node.

The full Bun run had failures in hook dispatch, dev reload, account email, Powerline installation, and usage fetch. The first four passed on targeted reruns. The same usage-fetch timeout reproduced on the PR base.

I counted processes for one render with a fixed JSON payload, a fresh cache directory, COLUMNS set, and these widgets: Context Length, Git Branch, Git Changes, Model, Output Speed, and Thinking Effort. The repository is small and has staged and unstaged changes. The runs used the built dist under Node 26 on macOS. A preload around child_process logged each call, and a git wrapper on PATH gave the same counts. The rendered output matched main in each scenario.

To repeat the count from the root of a checkout, run the following. The log shows which Git commands ran on the second render. It lists four on main and both shortstat commands on this branch.

wrap=$(mktemp -d); real=$(which git)
printf '#!/bin/sh\nprintf "%%s\\n" "$*" >> "%s/log"\nexec "%s" "$@"\n' "$wrap" "$real" > "$wrap/git"; chmod +x "$wrap/git"
echo '{"version":4,"lines":[[{"id":"1","type":"git-branch"},{"id":"2","type":"git-changes"}]]}' > "$wrap/settings.json"
render() { echo "{\"cwd\":\"$PWD\"}" | HOME="$wrap" COLUMNS=120 PATH="$wrap:$PATH" bun src/ccstatusline.ts --config "$wrap/settings.json"; }
render; sleep 6; : > "$wrap/log"; render; cat "$wrap/log"

What I checked:

  • bun run lint, bun run build, and bun test on macOS with Bun 1.4.3. Process starts are slow on this laptop, so I used --timeout 60000. The remaining failures were in the custom command capture and account email concurrency tests. Both also fail intermittently on main on this laptop, with a different set each run.
  • A new test file runs real git through a logging wrapper, because the change depends on what git touches. It covers the process count, a soft reset with a loose and a packed ref, a new branch, a staged change, a commit, a linked worktree, a detached HEAD, and a reftable repository. It also passes under Vitest on Node. It fails against the old cache code, and also when I remove the ref check, the reftable check, or the lookup of the main repository for a linked worktree.
  • New mocked tests cover the five-minute limit, failed commands, entries from older versions, a TTL of 0, and the environment variables.

What I did not check:

  • Windows. The real Git cache tests skip Windows.
  • A live Claude Code session. All renders used a fixed payload.
  • Time saved in a large repository.
  • The new mocked tests under Vitest on Node. Tests in git.test.ts that use its temporary home helper fail there with Cannot redefine property: homedir, and they fail the same way on main.

Git Cache TTL exists for edits to work tree files, which HEAD and index
modification times cannot show. When the TTL ended, every git command ran
again, including commands that never read the work tree. With Git Branch and
Git Changes, this ran four git processes when only one could change its answer.

Keep the successful output of these commands past the TTL, for up to five
minutes:

- Repository location checks: rev-parse --is-inside-work-tree, --show-toplevel,
  and --git-dir --git-common-dir.
- Commands that read only HEAD, its ref, and the index: symbolic-ref,
  rev-parse --short HEAD, diff --cached --shortstat, and ls-files --unmerged.
  These also run again when one of those files changes.

git reset --soft moves a branch without touching HEAD or the index, so the
checks now also read the modification times of the ref and packed-refs.

The plain TTL rules stay for a TTL of 0, failed commands, reftable repositories,
and sessions that set GIT_DIR, GIT_WORK_TREE, GIT_COMMON_DIR, or GIT_INDEX_FILE.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T05:06:20.467246Z 3220df3 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3220df31e4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/utils/git.ts Resolved
Comment thread src/utils/git.ts Resolved
@bvolpato

bvolpato commented Dec 5, 2026

Copy link
Copy Markdown
Author

Sorry, I am no longer working with ccstatusline for now and have no bandwidth to shepherd the changes here

@bvolpato bvolpato closed this Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant