Skip to content

fix(cli): snapshot freshness fingerprint for context-refresh --once and charter_context (#440) - #462

Merged
stackbilt-admin merged 2 commits into
mainfrom
feat/issue-440-snapshot-freshness
Nov 28, 2026
Merged

stackbilt-admin merged 2 commits into
mainfrom
feat/issue-440-snapshot-freshness

Conversation

@stackbilt-admin

@stackbilt-admin stackbilt-admin commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Summary

context-refresh --once and charter_context no longer present a stale snapshot as fresh after a branch switch, a new commit or a working-tree edit.

  • New module packages/cli/src/commands/git-fingerprint.ts:
    • readGitFingerprint(cwd, { ignore? }) returns { available, root, head (full SHA), branch, dirty, dirtyFiles, dirtyDigest, checkedLive: true }. It runs two local git processes (rev-parse --show-toplevel, status --porcelain=v2 --branch -z --untracked-files=all) and makes no network calls. It took about 350ms on this repo on Windows. The later preflight "Fresh:" line can call it directly.

    • assessSnapshotFreshness(snapshot, cwd) returns { stale, staleReasons, live }. compareGitFingerprint() returns the reasons the stored and live fingerprints differ.

    • charterRuntimeIgnorePaths(cwd, configPath) returns the directories Charter writes on every run, which depend on cwd and --config: <configPath>/telemetry/ and <cwd>/.charter/repo-intel/.

    • Charter's own generated files don't count as dirty. The excluded paths are:

      • .ai/context.snapshot.json and .ai/context.adf, plus the custom --ai-dir and --output paths for that refresh.
      • <configPath>/telemetry/ and <cwd>/.charter/repo-intel/, excluded as directory prefixes. They stay correct when charter runs from a subdirectory or with a non-default --config.
      • .charter/telemetry/ and .charter/repo-intel/ at the repo root.

      The runtime paths are stored in the fingerprint's ignored list, so readers apply the same exclusions. A refresh therefore never invalidates itself.

    • The dirty digest covers the status code, path, size and mtime of each file. A second edit to a file that is already dirty is also detected.

  • context-refresh:
    • When the git source is active, --once skips only if the TTL holds and the stored fingerprint matches the live one.
    • A snapshot with no fingerprint (written before this change) is refreshed.
    • The snapshot gains these additive fields: sources.git.head (full SHA), gitFingerprint, stale: false, staleReasons: [] and staleCheckedLive: false.
      • staleCheckedLive: false marks the file's stale value as valid only as of generatedAt. An agent reading .ai/context.snapshot.json directly can see the value was not checked against live git.
    • The skipped JSON payload also gains stale and staleReasons.
  • serve.ts: changes are limited to loadCharterContextSnapshot and the charter_context description.
    • The response gains top-level stale and staleReasons. The returned snapshot object gets the same values plus staleCheckedLive: true.
    • Freshness is checked against the repo that holds aiDir, not process.cwd(). This keeps it correct when serve starts elsewhere with an absolute --ai-dir.
    • The possible stale reasons:
      • branch_changed: a -> b
      • head_changed: abc1234 -> def5678
      • dirty_files_changed: N -> M (new: ...; clean: ...), or (modified since snapshot: ...) when the set is unchanged but a dirty file was edited again
      • no_fingerprint: ...
      • git_unavailable: ...
      • ttl_expired: ..., which applies only when github or repo-intel data is in the snapshot. Git-only snapshots are judged by fingerprint alone.

All new fields are additive. Existing keys, shapes and exit codes are unchanged.

Review round 1

  • Fixed (major): telemetry and repo-intel writes from a subdirectory or a non-default --config used to make every --once refresh, and right after a refresh charter_context returned a false dirty_files_changed: 2 -> 2. I confirmed the new tests fail without the fix (2 of 11 fail) and pass with it.
  • Fixed (minor): the freshness tests now use an explicit 20s timeout.
  • Fixed (minor): the snapshot file now carries staleCheckedLive.
  • Fixed (minor): the dirty reason text now names the new, cleaned or modified files.
  • Fixed (nit): serve now derives the freshness cwd from aiDir.

Verified (local, Windows; Actions is not running)

  • pnpm install --frozen-lockfile: ok
  • pnpm exec tsc --build tsconfig.build.json: ok
  • pnpm exec tsc --noEmit -p tsconfig.json: exit 0
  • pnpm exec vitest run packages/cli/src/__tests__/context-refresh-freshness.test.ts: 11/11 pass. The tests cover:
    • an unchanged repo taking the cheap skip path (with the full SHA stored and generated files not dirty)
    • a branch switch at the same SHA
    • a new commit on the same branch
    • a dirty-file edit (the refresh then skips again)
    • a snapshot written before this change
    • fingerprint parsing (renames, paths with spaces, ignored telemetry)
    • running outside a repo
    • new: refreshing from a subdirectory with telemetry and repo-intel writes between calls (--once skips 3 times in a row, and charter_context reports no stale reasons)
    • new: --config cfg telemetry is ignored, and a real edit is still caught
    • new: serve run from an unrelated cwd
    • new: the reason text for modified and cleaned dirty files
  • pnpm exec vitest run (full suite): 2179 passed, 7 failed, 2 skipped.
  • node packages/cli/dist/bin.js adf evidence --auto-measure --ci --format text: Verdict: PASS. The only warning is the existing adf_migrate_loc 792/792, which this PR doesn't touch.
  • Manual repro from the issue, in a scratch repo (before round 1): context-refresh --sources git, then checkout -b scratch/x HEAD~1, then echo "// wip" >> a.ts, then context-refresh --once --format json. Result: "status":"ok","reason":"refreshed". Before this fix the same steps reported skipped / fresh_snapshot.
    • I did not repeat this by hand with the built CLI for the subdirectory and --config cases, because the sandbox blocks git outside the worktree. The new tests cover those cases with real git repos in temp dirs, calling contextRefreshCommand and loadCharterContextSnapshot directly.

Not done

  • The preflight "Fresh:" line is not wired up. The later integration PR does that with one readGitFingerprint() call.
  • The existing sources.git.dirty and dirtyFiles (git status --short) are unchanged for contract stability. They can still list Charter's generated files; the new gitFingerprint excludes them.
  • The text output of the skip path is unchanged.
  • A dirty-set change can only name files that are in the stored sample of 50 paths. Removals are reported only when that sample is complete.
  • No CHANGELOG entry, per workflow rules.

Closes #440
Part of #437

🤖 Generated with Claude Code

kovermier and others added 2 commits November 28, 2026 08:17
…erved as fresh (#440)

context-refresh --once now skips only when the TTL holds and the stored
git fingerprint (branch, full HEAD SHA, dirty-file set + per-file
size/mtime digest) matches live. Charter's own generated files
(.ai/context.snapshot.json, .ai/context.adf, .charter/telemetry/,
.charter/repo-intel/, the --output mirror) are excluded so a refresh
never invalidates itself.

The snapshot gains additive sources.git.head (full SHA), gitFingerprint,
stale and staleReasons. charter_context / loadCharterContextSnapshot
recompute stale/staleReasons against live git and return them both at
top level and on the snapshot object.

New module commands/git-fingerprint.ts exports readGitFingerprint()
({ available, root, head, branch, dirty, dirtyFiles, dirtyDigest,
checkedLive: true }) for the preflight "Fresh:" line, plus
assessSnapshotFreshness() and compareGitFingerprint().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ngerprint (#440)

The dirty-set exclusion hardcoded .charter/telemetry/ and .charter/repo-intel/
at the repo root. Run from a subdirectory or with --config, the telemetry
append after every command changed the dirty digest, so --once never skipped
and charter_context reported a false stale signal right after a refresh.

- Exclude <configPath>/telemetry/ and <cwd>/.charter/repo-intel/ as directory
  prefixes and persist them in gitFingerprint.ignored.
- Stale reasons name new, cleaned, or modified-since-snapshot dirty files.
- Snapshot file carries staleCheckedLive:false; charter_context sets true.
- charter_context fingerprints the repo holding aiDir, not process.cwd().
- Freshness tests get a 20s timeout; add subdirectory, --config, cwd and
  reason-text cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants