Skip to content

fix(ci): the gate cache certifies only the tree it checked #838

Closed

Description

@dan-petty

Problem Statement

The CI cache can certify a tree that no check ran on, in two ways. Evidence checked at 9c879ae (branch release/v0.2.25); paths are under src/devops_cli/ unless stated.

1. File arguments.

  • devops ci <file> and devops ci --files <file> narrow no check. FileOrSubcommandGroup routes an unknown token to the group callback as a file (commands/ci.py:46-69), and the callback collects it with _collect_ci_target_files (commands/ci.py:535-544, called at :715). _run_all_checks_async takes no files (:723-727), so the whole gate runs regardless.
  • Those files reach only the cache lookup: _try_fast_cached_ci (commands/ci.py:614-630) passes them to get_ci_cache, which returns the last passing entry whenever the named files' hashes match the entry's file_hashes, even though the workspace fingerprint differs (ci/cache.py:336-338, via _check_pre_commit_subset_match at :297-305 and _is_subset_file_matching at :285-294).
  • Reproduced at 308a71f in a scratch repo. After a recorded pass, with src/b.py containing def b(:, devops ci src/a.py exits 0 and prints Codebase unchanged since last verification. Utilizing CI cache (all checks passed). (MESSAGES.ci.cache_hit, lang/en/messages.py:731-733). devops ci pyproject.toml hits the cache too.
  • tests/test_ci_cache.py:126-175 (test_get_ci_cache_pre_commit_subset_match) and :247-264 (test_ci_cli_positional_files_pre_commit) assert this as intended behaviour. The path came from feat(ci): fast execution caching when unchanged & pre-commit file change tracking integration #256 (docs/agent/tasks/task-256-ci-execution-caching-pre-commit.md:18), whose pass_filenames: true hook no longer exists: the pre-push hook sets pass_filenames: false (.pre-commit-config.yaml:75).

2. Fingerprint timing.

  • _try_save_ci_cache (commands/ci.py:575-611) computes the fingerprint after every check has finished (compute_workspace_fingerprint at :588, reached from _handle_ci_results at :666-667).
  • An agent that keeps editing while the gate runs in the background gets the edited content cached as passing.
  • The next pre-push run is then a cache hit on content that no check saw.

No caller passes files to the full gate:

  • .pre-commit-config.yaml:70-77 (devops-ci-pre-push) sets pass_filenames: false.
  • commands/release.py:1287 calls devops ci run, a separate subcommand (commands/ci.py:1126-1145) that neither reads nor writes the cache.
  • The devops-ci-test-changed hook (.pre-commit-config.yaml:62-68) passes staged files to the test subcommand's own paths argument (commands/ci.py:800), not to the group callback. feat(cli): devops ci test selects tests from a coverage index built on demand by devops ci coverage --build-index #406 owns that path.
  • No doc, AGENTS.md, README.md or test outside tests/test_ci_cache.py spells --files for devops ci (grep -rn -- '--files' docs AGENTS.md README.md tests finds no gate use).

Proposed Solution

Remove the file-subset path. Nothing that remains needs it.

  • commands/ci.py: the --files option (:695-698) and its help string CICommandHelp.files (lang/en/help.py:528-530); _collect_ci_target_files (:535-544); the files / all_files parameters of _try_get_ci_cache (:547), _try_save_ci_cache (:575), _try_fast_cached_ci (:614) and _handle_ci_results (:652); the resolve_command override and the cmd is None branch of FileOrSubcommandGroup.invoke (:49-55, :61-67). Keep the subcommand-help detection (:69, _asks_for_help at :37-43, CONST_CI_SUBCOMMAND_SHOWS_HELP_META_KEY). With the override gone, typer's TyperGroup.resolve_command (plain click resolution plus a "Did you mean" suggestion) turns devops ci src/a.py into No such command 'src/a.py', exit 2, and --files src/a.py into No such option, exit 2; the ci group sets neither allow_extra_args nor ignore_unknown_options (core/cli.py:172-176 sets only help_option_names; :145-150 applies to lazy root proxies only). Checked by invoking the app with the override swapped for TyperGroup's and the runner patched to raise: src/a.py and pyproject.toml exit 2 with zero runner calls, test --help exits 0.
  • ci/cache.py: the files parameter of get_ci_cache (:308-340), _check_pre_commit_subset_match (:297-305), _is_subset_file_matching (:285-294), _compute_file_hashes (:137-147), the third element of the compute_workspace_fingerprint tuple and its comment (:279-282; the function returns (fingerprint, head_sha)), the file_hashes field of CICacheEntry (:62) and the file_hashes parameter of save_ci_cache (:343-375), and _collect_modified_git_files (:150-162), which nothing calls today (grep -rn _collect_modified_git_files src tests returns only the definition). Update the module docstring (:1-6), which still describes "pre-commit file change tracking". Existing cache files that carry file_hashes keep parsing: CICacheEntry (:53) has no model_config forbidding extra fields.
  • Tests: replace test_get_ci_cache_pre_commit_subset_match and test_ci_cli_positional_files_pre_commit (tests/test_ci_cache.py:126-175, :247-264) with the tests below; update the three-tuple unpack at tests/test_ci_cache.py:47-48, the file_hashes= arguments at :75-82, :114, :215, and the patched three-tuple returns at tests/test_ci_cache.py:220-223 and tests/test_ci.py:703; drop the None / all_files=None arguments in tests/test_ci.py:705, :712, :720, :738, :747, :760.

Take the fingerprint before the checks start. In all_checks (commands/ci.py:676-731):

  • Compute the fingerprint once, before the cache lookup, and use that one value both for the lookup (which computes it today at :555) and as the "before" value. Compute it also when the lookup is skipped by --force or --no-cache, because those runs still record their result (:652-667). A dry run records nothing (:666) and needs no "before" value.
  • After the checks pass, recompute. If it equals the "before" value, save as today. Otherwise print a MESSAGES.ci warning (new entry next to cache_hit, lang/en/messages.py:731) saying the tree changed while the gate ran and the result was not recorded, and save nothing. The run's exit status is the checks' verdict, unchanged. The existing entry is neither overwritten nor cleared: it certifies the tree it was recorded for, and a later tree can only hit it by matching that fingerprint.
  • run (commands/ci.py:1126-1145) and the per-check subcommands do not touch the cache and do not change.

Docs. docs/ROADMAP.md:1146-1149 already carries this item; the cut updates it. The changelog entry goes in changelog.d/838.md.

Scope

One pull request. Files: src/devops_cli/commands/ci.py, src/devops_cli/ci/cache.py, src/devops_cli/lang/en/help.py, src/devops_cli/lang/en/messages.py, tests/test_ci_cache.py, tests/test_ci.py, docs/agent/tasks/task-838-<slug>.md, changelog.d/838.md.

Key Questions

  • Does a passing run whose tree changed while the gate ran exit 0 or non-zero? Answered by the deliverable: it says the tree changed and saves nothing, and changes nothing else, so the exit status stays the checks' verdict (0 when every check passed). The checks passed on the tree they read; only the cache cannot vouch for the tree that exists now, and the next run re-verifies it. A non-zero exit would make the pre-push hook block a push because an editor saved a file during the run, which is the background-agent case this item is written for.
  • Does a changed-during-run pass clear the existing cache entry? Answered by the deliverable: "save nothing" means the cache file is not touched, neither written nor cleared (clearing is what a failed run does, commands/ci.py:670-672). The entry still certifies the tree it was recorded for; a later run hits it only if its fingerprint matches, which is exactly the tree the entry verified.
  • Should the fingerprint for the lookup and the "before" value be one computation? Answered: yes. _try_get_ci_cache already computes it (commands/ci.py:555); computing it twice would open a window between the two reads.
  • Does devops ci run or any per-check subcommand change? Answered: no. They never touch the cache (commands/ci.py:1126-1145).
  • Does the test subcommand's paths argument stay? Answered: yes. It belongs to devops ci test and the devops-ci-test-changed hook (commands/ci.py:800, .pre-commit-config.yaml:62-68); feat(cli): devops ci test selects tests from a coverage index built on demand by devops ci coverage --build-index #406 owns it.
  • Does refactor(ci): github ci runs the gate through devops ci from one check table #843's narrowed run (--only/--skip) interact with the cache? Answered in refactor(ci): github ci runs the gate through devops ci from one check table #843: a narrowed run neither reads nor saves the cache. This item's before/after check applies to full runs only, which is all that saves.

Acceptance Criteria

  • In a tmp_path git repository (reuse the repo fixture pattern of tests/test_ci_cache_fingerprint.py:16-27) with a recorded pass, changing one tracked file makes get_ci_cache(fingerprint=compute_workspace_fingerprint(...)[0], ...) return None. The test is parametrised over a src/ file, a tests/ file, a docs/ file and pyproject.toml. Test: new tests/test_ci_cache.py::test_any_tracked_change_misses_the_cache[...].
  • devops ci src/a.py and devops ci --files src/a.py exit 2 with a usage error, and a patched _run_all_checks_async records zero calls. Test: new tests/test_ci_cache.py::test_a_file_argument_is_a_usage_error (parametrised over the two spellings) using CliRunner.
  • A run in which the patched runner edits a tracked file prints the new warning, exits 0 and saves no entry; an entry recorded before the run is still there, byte for byte. Test: new tests/test_ci_cache.py::test_a_tree_edited_during_the_run_is_not_recorded, in a tmp_path git repository with _run_all_checks_async patched to write to a tracked file before returning passing results, asserting the exit code, the warning in the output, and that resolve_ci_cache_path(root) is unchanged.
  • A run in which nothing changes still records its entry. Test: existing tests/test_ci.py::test_try_save_ci_cache_and_handle_results (:694-722), test_a_no_cache_run_still_records_its_result (:724-740) and tests/test_ci_cache.py::test_ci_cli_cache_hit_and_force (:188-244), updated for the new signatures and the two-tuple fingerprint, keep passing.
  • devops ci test --help and devops ci lint --help still print help. Test: existing tests/test_ci.py::test_a_single_checks_help_does_not_name_the_gate_root[test|lint] (:1281-1298) and test_help_after_the_separator_is_a_path_and_the_check_names_its_root (:1301-1317) keep passing.
  • rg -n 'subset_match|_is_subset_file_matching|_collect_ci_target_files|_compute_file_hashes|_collect_modified_git_files|file_hashes' src tests -g '!src/devops_cli/ai/rag/**' returns nothing (file_hashes in ai/rag/indexer.py belongs to a different feature). The two old tests at tests/test_ci_cache.py:126-175 and :247-264 are replaced, not skipped.
  • The new tests together take at most 1 s on one worker, measured with uv run pytest tests/test_ci_cache.py -p no:cacheprovider --durations=0 -o addopts="" and recorded in the task file. (The whole file plus test_ci_cache_fingerprint.py runs in about 8 s today, mostly import and git setup; individual cache tests take 0.01-0.03 s.)
  • Tests stay offline: none of the new tests runs a check, reaches the network or DNS, or needs a real scanner.
  • uv run devops ci passes, and the PR adds changelog.d/838.md and docs/agent/tasks/task-838-<slug>.md.

Out of Scope

Dependencies

Order on src/devops_cli/commands/ci.py: #832 -> #843 -> #406 in v0.2.26, then #885 -> #838 -> #858 in v0.2.27. This item blocks on nothing; every line below is a rebase fact.

Readiness check (2026-10-04)

Re-checked at b61e5a0 (release/v0.2.26). #843 and #406 are merged, and #885 lands first in v0.2.27. The design is unchanged, but every commands/ci.py line and some test lines moved.

Corrected locations (under src/devops_cli/):

  • commands/ci.py:
    • FileOrSubcommandGroup is at :48-72: resolve_command override :51-57, the cmd is None branch :64-70, help detection :71. _asks_for_help is at :39-45.
    • --files option :971-974. _collect_ci_target_files is at :796-805, called at :994.
    • _try_get_ci_cache is at :808-833 (fingerprint at :816), _try_save_ci_cache at :836-872 (fingerprint at :849).
    • _try_fast_cached_ci is at :875-891 (dry-run bypass at :884).
    • _handle_ci_results is at :914-937: save :930-931, clear on failure :936-937.
    • all_checks is at :940-1015. A narrowed run (is_narrowed, :990) neither reads (:996) nor saves (:1015).
    • run is at :1614-1633; test's paths argument is at :1251.
  • ci/cache.py:
    • CICacheEntry is at :54, with file_hashes at :63.
    • _compute_file_hashes is at :150-160. _collect_modified_git_files (:163) is still uncalled.
    • compute_workspace_fingerprint is at :275-312; its third element is at :309-312.
    • _is_subset_file_matching is at :315-324, _check_pre_commit_subset_match at :327-335.
    • get_ci_cache is at :338-370 (subset branch :367-368); save_ci_cache starts at :373.
  • feat(cli): devops ci test selects tests from a coverage index built on demand by devops ci coverage --build-index #406's shared helper is compute_worktree_blob_hashes (ci/cache.py:251-272), which the fingerprint now calls. The rebase between feat(cli): devops ci test selects tests from a coverage index built on demand by devops ci coverage --build-index #406 and this item is done, and no subset shortcut may return.
  • Other moved lines:
    • lang/en/help.py:586 (CICommandHelp.files) and lang/en/messages.py:831 (cache_hit).
    • core/cli.py: the lazy-proxy context settings are at :155-161, help_option_names at :189.
    • commands/release.py:1556 runs devops ci run.
  • tests/test_ci.py:
    • The three-tuple patch is at :849.
    • The None / all_files=None arguments are at :852, :859, :867, :885, :894 and :907.
    • test_try_save_ci_cache_and_handle_results is at :841-869, test_a_no_cache_run_still_records_its_result at :871-887, and the two help tests at :1429 and :1448.
  • Unchanged: the tests/test_ci_cache.py and .pre-commit-config.yaml lines.

Cross-item consistency with #885. Fingerprinting runs git through run_subprocess (ci/cache.py:115-135, :215-248). A "before" fingerprint taken ahead of the dry-run check would break #885's zero-call recorder tests on devops ci --dry-run.

Lint rule now in force. ruff selects C901 with max-complexity = 10 (pyproject.toml:94-97, :109-110, #586). The before/after branch keeps all_checks within it without noqa, using a helper if needed.

Ready: no open question, dependencies merged or earlier in v0.2.27, one PR.

Activity

  1. added
    type/bug
    scope/cli
    priority/p1-high
    on Oct 1, 2026
  2. added this to the v0.2.27 milestone on Oct 1, 2026
  3. dan-petty-bot commented on Oct 5, 2026

    @dan-petty-bot
    Collaborator

    What changed

    #1233, merged into release/v0.2.27 as 895841c.

    Summary

    Resolves #838

    The CI execution cache verifies whole-tree state against deterministic Git and blob fingerprints, certifying only the exact tree that passed all checks.

    11 file(s) changed.

    How it was verified

    Check runs at 48d553e:

    • Prune PR DevContainer Images: pending
    • Tests & Coverage: pass
    • Service Image: skipping
    • Dependency Freshness: skipping
    • DevContainer Image: skipping
    • CodeQL: pass
    • Analyze (python): pass
    • Analyze (actions): pass
    • Static Analysis: pass

    Acceptance Criteria of docs/agent/tasks/task-838-ci-cache-certifies-checked-tree.md:

    • Legacy file-subset caching path (--files, FileOrSubcommandGroup file routing, _collect_ci_target_files, _compute_file_hashes, _collect_modified_git_files, _is_subset_file_matching, _check_pre_commit_subset_match, and file_hashes on CICacheEntry) is completely removed.
    • compute_workspace_fingerprint returns a 2-tuple (fingerprint, head_sha).
    • In all_checks, fingerprint is computed before checks execute for full, non-dry runs. Recomputed upon completion: if tree mutated during run, MESSAGES.ci.cache_tree_changed warning is printed, existing cache is untouched, and exit code is 0.
    • Positional file arguments and --files options raise Click UsageError with exit code 2 and 0 check executions.
    • All new and updated tests execute offline in $< 1$s each.
    • changelog.d/838.md created; CHANGELOG.md and docs/ROADMAP.md remain untouched.
    • Pending a person: uv run devops ci passes on this branch.
  4. added a commit that references this issue on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/clitype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions