ISSUE DESCRIPTION [BUG] apm install fail second time with Content hash mismatch Describe…
- User
ISSUE DESCRIPTION
[BUG] apm install fail second time with Content hash mismatch Describe the bug I can install my repo (see apm.yml further down) via
apm install <org>/<repo>/package/<project>just fine (kinda, see the logs, it looks wonky but maybe it's just a new feature) but the second time I runapm installit will fail consistently withContent hash mismatch.To Reproduce Steps to reproduce the behavior:
- apm install <repo>
- apm install
Expected behavior A clear and concise description of what you expected to happen.
Environment (please complete the following information):
- OS: Windows
- APM Version: 0.16.0
Logs
> apm install --update --force [>] Installing dependencies from apm.yml... [>] Resolving github-copilot-agent-skills-testing... [i] Targets: copilot (source: apm.yml) Partial clone (--filter=blob:none) failed for https://dev.azure.com/<org>/tools; retrying with full bare clone. Server may not support filter v2. [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/packages/<project> #default @d479f42e |-- (files unchanged) Partial clone (--filter=blob:none) failed for https://dev.azure.com/<org>/tools; retrying with full bare clone. Server may not support filter v2. [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/packages/myorg #default @d479f42e |-- (files unchanged) [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/.apm/instructions/testing.instructions.md #main |-- 1 instruction(s) integrated -> .github/instructions/ Partial clone (--filter=blob:none) failed for https://dev.azure.com/<org>/tools; retrying with full bare clone. Server may not support filter v2. [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/.apm/skills/run-coverage #default @d479f42e |-- (files unchanged) Partial clone (--filter=blob:none) failed for https://dev.azure.com/<org>/tools; retrying with full bare clone. Server may not support filter v2. [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/.apm/skills/update-from-template #default @d479f42e |-- (files unchanged) Partial clone (--filter=blob:none) failed for https://dev.azure.com/<org>/tools; retrying with full bare clone. Server may not support filter v2. [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/packages/csharp #default @d479f42e |-- (files unchanged) [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/.apm/instructions/code-style.instructions.md #main |-- 1 instruction(s) integrated -> .github/instructions/ [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/.apm/instructions/protect-prefixed-agent-files.instructions.md #main |-- 1 instruction(s) integrated -> .github/instructions/ Partial clone (--filter=blob:none) failed for https://dev.azure.com/<org>/tools; retrying with full bare clone. Server may not support filter v2. [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/.apm/skills/git-conventions #default @d479f42e |-- (files unchanged) Partial clone (--filter=blob:none) failed for https://dev.azure.com/<org>/tools; retrying with full bare clone. Server may not support filter v2. [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/.apm/skills/update-nuget-packages #default @d479f42e |-- (files unchanged) Partial clone (--filter=blob:none) failed for https://dev.azure.com/<org>/tools; retrying with full bare clone. Server may not support filter v2. [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/.apm/skills/write-tests #default @d479f42e |-- (files unchanged) cache-pin: remote dep '<org>/tools/github-copilot-agent-skills' has no resolved_commit; drift cannot verify its cache freshness. Re-run 'apm install' with a pinned ref (commit, tag, or specific branch HEAD). cache-pin: remote dep '<org>/tools/github-copilot-agent-skills' has no resolved_commit; drift cannot verify its cache freshness. Re-run 'apm install' with a pinned ref (commit, tag, or specific branch HEAD). cache-pin: remote dep '<org>/tools/github-copilot-agent-skills' has no resolved_commit; drift cannot verify its cache freshness. Re-run 'apm install' with a pinned ref (commit, tag, or specific branch HEAD). [i] Trusting direct dependency MCP 'MyOrg.CLI' from '<project>-skills' [i] Trusting direct dependency MCP 'NuGet' from '<project>-skills' [i] Trusting direct dependency MCP 'Github' from '<project>-skills' [i] Trusting direct dependency MCP 'MyOrg.CLI' from 'skills' [i] Trusting direct dependency MCP 'NuGet' from 'skills' +- MCP Servers (3) | [+] MyOrg.CLI (already configured) | [+] NuGet (already configured) | [+] Github (already configured) [*] All servers up to date [!] 11 dependencies unpinned: <org>/tools/github-copilot-agent-skills -- add #tag or #sha to prevent drift [!] Transitive package 'csharp-skills' declares self-defined MCP server 'NuGet' (registry: false). Re-declare it in your apm.yml or use --trust-transitive-mcp. [!] Transitive package 'csharp-skills' declares self-defined MCP server 'Github' (registry: false). Re-declare it in your apm.yml or use --trust-transitive-mcp. [*] Installed 11 APM dependencies in 87.9s. > apm install [>] Installing dependencies from apm.yml... [i] Targets: copilot (source: apm.yml) [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/packages/<project> @d479f42e (cached) |-- (files unchanged) [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/packages/myorg @d479f42e (cached) |-- (files unchanged) [+] dev.azure.com/<org>/tools/github-copilot-agent-skills/.apm/instructions/testing.instructions.md #main Content hash mismatch for <org>/tools/github-copilot-agent-skills/.apm/instructions/testing.instructions.md: expected sha256:2227579674ab30b5b59f703ac152fa74ac2912d724d04aeffbf3914cac2e6b88, got sha256:b82d09e4e7879eeb0e90cc7dfeebdac45de6ebdbcdda7824b40438196692f8f8. The downloaded content differs from the lockfile record. This may indicate a supply-chain attack. Use 'apm install --update' to accept new content and update the lockfile. [!] Install interrupted after 7.7s. - Assistant
I'll start by exploring the repository to understand the codebase and locate relevant files. Let's first get an overview.
- Assistant
This appears to be a Python project (apm CLI). Let's look at the root directory structure and understand the project better.
- Assistant
Let me search for the content hash mismatch error to understand the issue.
- Assistant
Let me look at the issue. The problem is about "Content hash mismatch" on a second install. The issue is that when installing a dependency (specifically instructions files), the first time it works fine, but the second time it fails with a content hash mismatch because the downloaded content differs from what's in the lockfile.
Let me trace through the code to understand what's happening:
- First install: The content is fetched and the hash is stored in the lockfile
- Second install: The content is fetched again but the hash doesn't match
Looking at the error message more carefully:
Content hash mismatch for <org>/tools/github-copilot-agent-skills/.apm/instructions/testing.instructions.md: expected sha256:2227579674ab30b5b59f703ac152fa74ac2912d724d04aeffbf3914cac2e6b88, got sha256:b82d09e4e7879eeb0e90cc7dfeebdac45de6ebdbcdda7824b40438196692f8f8``` The key difference is that the first hash is calculated during the first install and stored in the lockfile. The second hash is calculated during the second install from the downloaded content. This suggests there's an issue with how the content hash is being calculated. The two possible scenarios: 1. The content is being transformed differently between the first and second install 2. There's some normalization happening (like line endings, trailing newlines, etc.) that differs between runs Let me look at the relevant source files. - Assistant
The file is truncated. Let me look at the content hash computation and the lockfile enrichment logic. The bug is about "Content hash mismatch" on the second install. Let me look at:
- How the content hash is computed when installing fresh
- How the content hash is verified on second install
- The
_compute_hashfunction
The issue is likely that the content hash is computed differently on fresh download vs cached install. Let me look at the hash computation code.
- Assistant
Now let me look at how the download and hashing works for single-file dependencies (like
.apm/instructions/testing.instructions.md). - Assistant
Let me look at the key areas. The error happens on second install, when a "fresh" download of an instruction file gets a different content hash than the first install. Let me understand how content hashing works for individual files vs directories.
The key insight from the bug report is:
- First install succeeds, records a content_hash in the lockfile
- Second install (without --update) fails with "Content hash mismatch" because the freshly downloaded content has a different hash
The dependency in question is
.apm/instructions/testing.instructions.mdwhich is a single file. Let me understand how these are downloaded and hashed.Let me look at the sources.py more carefully, and at how the hash is computed when installing.
- Assistant
Let me read the full sources.py file to understand the FreshDependencySource and CachedDependencySource.
- Assistant
Let me look at the FreshDependencySource's acquire method:
- Assistant
Let me look at the FreshDependencySource class.
- Assistant
Now I see the issue! The
install_path.is_dir()check is the key. For single-file dependencies (like.apm/instructions/testing.instructions.md), theinstall_pathis a file, not a directory, soinstall_path.is_dir()returnsFalseand the hash is NOT computed intoctx.package_hashes.But wait - the lockfile has a content_hash for this dependency. That means the content_hash was recorded during the first install. Let me understand how the hash gets recorded.
Let me look at how the download works for single-file dependencies and how the hash gets initially recorded.
- Assistant
Let me look at the CachedDependencySource:
- Assistant
Let me examine the CachedDependencySource class.
- Assistant
Let me look at the lockfile phase to understand how content hashes get recorded initially:
- Assistant
Now let me understand how the download works for file-level dependencies (like .md files inside a repo). Let me look at the downloader.
- Assistant
Let me look at how the download works for single-file dependencies.
- Assistant
Let me look at the download_package method in the github_downloader:
- Assistant
Let me look at the download_package method:
- Assistant
Let me look at the download_virtual_file_package method to understand how single-file downloads work:
- Assistant
Let me look at the download_virtual_file_package method.
- Assistant
Now let me look at the integration phase to understand how it decides whether to use cached vs fresh download:
- Assistant
Let me continue reading the integrate.py file.
- Assistant
Let me look at what happens in the FreshDependencySource and how the
.apm-pinmarker interacts with the content hash. The key observation is:compute_package_hashexcludes.apm-pinfiles, but the_compute_hashfunction used in theFreshDependencySourceiscompute_package_hash. Let me look at whether the.apm-pinfile is written before or after the hash is computed. - Assistant
Let me look at the test files to understand the expected behavior:
- Assistant
Let me search for tests related to the specific issue - content hash mismatch on re-install.
- Assistant
Let me look at the test for content hash mismatch specifically:
- Assistant
Let me trace the issue more carefully. The problem is that the hash is different between first and second install for virtual file packages. Let me look at how the integration template works to see if it modifies files:
- Assistant
Let me focus on understanding the download_raw_file method and how the content hash calculation differs between first and second install:
- Assistant
Let me understand the key flow. Let me look at the
_resolve_download_strategyfunction more carefully and understand what happens on a second install for a virtual file dependency: - Assistant
Let me look at the test files related to content hash mismatch and understand the issue better:
- Assistant
Let me look at how
yaml_to_strworks and if it could produce different output: - Assistant
Let me think about this more carefully. The issue is that the second
apm installfails with a content hash mismatch. Let me look at how the FreshDependencySource handles virtual file dependencies. - Assistant
Let me look at the test file for sources materialisation to see what tests exist:
- Assistant
Let me look at the yaml_to_str function:
- Assistant
Let me look more closely at how the issue could be related to the
.apm-pinmarker. Let me search for how the marker is written and where: - Assistant
Let me look at how the cache pin marker writing interacts with the hash computation:
- Assistant
Let me try a completely different approach. Let me look at the integrate phase to see what happens during instruction integration, and check if it modifies the install_path:
- Assistant
Let me look at how install_path is determined for virtual file packages:
- Assistant
Let me find the dependency reference module.
- Assistant
Let me look at the
get_install_pathmethod. - Assistant
Let me read more of the get_install_path method to understand how virtual file packages get their install_path.
- Assistant
Let me look at the tests for the specific scenarios around content hash and re-install:
- Assistant
Let me look at the git log to see recent changes to the relevant files:
- Assistant
Let me look at the instruction integrator to see if it modifies the install_path:
- Assistant
Let me look at the resolve phase to understand when virtual file packages are downloaded:
- Assistant
Let me look at the download_callback in the resolve phase:
- Assistant
Let me understand the exact flow better. Let me look at
_resolve_commit_sha_for_ref: - Assistant
Let me look at this method.
- Assistant
Let me look at the actual test file and try to reproduce the issue:
- Assistant
I need to install the package first. Let me check the setup.
- Assistant
Let me run the tests now:
- Assistant
Now let me look at the specific test for hash mismatch:
- Assistant
Let me look at the heal chain module:
- Assistant
Let me look at the HEAL_CHAIN:
- Assistant
Let me look at the BranchRefDriftHeal:
- Assistant
Let me look at how virtual package names are generated:
- Assistant
Let me continue reading:
- Assistant
Let me look at the BuggyLockfileRecoveryHeal:
- Assistant
Let me look at the test for cache/lockfile parity and content hash:
- Assistant
Let me look at the
build_download_reffunction: - Assistant
Let me look at the build_download_ref function:
- Assistant
Let me look at the instruction integrator to see if it modifies the install_path:
- Assistant
Now I understand the issue! Let me trace through the exact flow:
-
First install (
apm install --update --force):FreshDependencySourcedownloads the file, computes hash H1, stores in lockfile withcontent_hash = H1. Theresolved_commitmay be None for virtual file packages on ADO (where SHA resolution fails). -
Second install (
apm install): In_resolve_download_strategy, thelockfile_matchcheck requireslocked_dep.resolved_committo be truthy:
if locked_dep and locked_dep.resolved_commit and locked_dep.resolved_commit != "cached":If
resolved_commitis None (common for virtual file packages on ADO), this whole block is skipped!lockfile_matchstays False,skip_download = False, andFreshDependencySourceis used. The file is re-downloaded and the hash check fails.The fix is to add a content-hash fallback for git-source deps that lack
resolved_commit, similar to what already exists for registry deps. Let me implement this: -
- Assistant
Now I can see the bug clearly. The code has a fallback for registry deps without
resolved_commit, but NO fallback for git-source deps withoutresolved_commit(like virtual file packages on ADO). Let me implement the fix: - Assistant
Now I'll add the fallback for git-source deps without
resolved_commit: - Assistant
Good. Now let me run the existing tests to make sure I didn't break anything:
- Assistant
Good, all tests pass. Let me run the broader test suite:
- Assistant
That's a missing dependency, not related to my changes. Let me run ignoring that module:
- Assistant
Let me ignore both jsonschema files:
- Assistant
All 14,401 unit tests pass. Let me also run the integration tests that are related to install:
- Assistant
Let me also check some integration tests related to content hash:
- Assistant
The cache lockfile parity test is skipped (likely requires a GitHub token). Let me also run the coverage gaps test:
- Assistant
Good. Let me also run the install-related wave tests:
- Assistant
All good. Let me also verify the specific tests related to the integrate phase:
- Assistant
Let me now review the change I made to make sure it's correct:
- Assistant
The change looks correct. Let me also run the full integration test suite to make sure nothing is broken:
- Assistant
Let me check if that failing test is related to my changes:
- Assistant
This failure is not related to my change - it's about ADO authentication preflight. Let me verify by checking if it was already failing before my change:
- Assistant
The test was already failing before my change. Let me restore my changes:
- Assistant
Now let me verify the final diff one more time:
- Assistant
The fix looks correct. Let me now do a final comprehensive check of all relevant tests:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.