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 structure to understand what we're working with.
- Assistant
Let me explore the repository structure more to understand the codebase.
- Assistant
Let me explore the source code structure to understand the codebase better.
- Assistant
Let me understand the repository structure. This is an "apm" (Agent Package Manager) tool. The issue is about a content hash mismatch on the second install. Let me explore the source code to understand how the hashing and caching works.
- Assistant
Let me explore the install and security directories to understand how hashing and verification works.
- Assistant
Let me understand the error. The bug is:
- First
apm installworks fine - Second
apm installfails with "Content hash mismatch"
The error message says:
Content hash mismatch for <org>/tools/github-copilot-agent-skills/.apm/instructions/testing.instructions.md: expected sha256:2227579674ab30b5b59f703ac152fa74ac2912d724d04aeffbf3914cac2e6b88, got sha256:b82d09e4e7879eeb0e90cc7dfeebdac45de6ebdbcdda7824b40438196692f8f8.This is about content hash mismatch between what's in the lockfile and what's downloaded/cached.
Let me look at the relevant code files.
- First
- Assistant
Let me look at the code that produces the "Content hash mismatch" error.
- Assistant
Let me look at the context around that line.
- Assistant
Now let me understand how the content hash is computed:
- Assistant
Let me look at the content hash utility to understand how hashes are computed.
- Assistant
Now let me understand how single-file dependencies (like
.apm/instructions/testing.instructions.md) are downloaded and installed. Let me look at the sources.py file more carefully: - Assistant
Let me understand the issue better. The bug is about content hash mismatch on second install. Let me look at the cache_pin module and how
.apm-pinfiles are written: - Assistant
Now let me look more carefully at the error and understand the flow. The error is about a specific file within a repo. Let me search for how single-file dependencies and
.apm/instructionspaths are handled: - Assistant
Let me look at how the dependency is being resolved and how the hash is computed. Let me understand the flow for single-file dependencies:
- Assistant
Let me look at how the
.apm-pinmarker interacts with the hash computation. Let me also check if single-file dependencies have special handling: - Assistant
Let me look at the full flow around the hash computation:
- Assistant
Let me understand the flow better. Let me look at how
.apm-pinis written and when it's written relative to the hash computation: - Assistant
The grep command returned no output. Let me search more broadly.
- Assistant
Let me now look at the lockfile phase and the integrate phase:
- Assistant
Now I need to understand the issue better. The problem is:
- First install works and stores hash in lockfile
- Second install (from cache) gets a different hash
The
.apm-pinmarker is written AFTER hash computation (lockfile phase), so on second install the marker exists on disk. But the hash computation already excludes.apm-pinat root level.Let me check how the hash is computed for non-directory paths (single files):
- Assistant
Let me look at how single-file dependencies are downloaded and installed. Let me search for how the install path is determined:
- Assistant
Let me look at the
GitRemoteMaterializerclass more carefully, specifically the part that handles the content hash check: - Assistant
Now I need to understand the flow better. The issue is that the first install works fine with
--update --force, but the second install fails with content hash mismatch. Let me look at how the hash is computed and when.apm-pinis written. Let me also look at how file-level dependencies are downloaded: - Assistant
Let me trace the issue more carefully. The error occurs because the content hash is different between the first install and the second install. Let me look at how install paths are determined for file-level dependencies:
- Assistant
Let me look at the
get_install_pathmethod to understand how it determines the path for a dependency with a file-level path (like.apm/instructions/testing.instructions.md). - Assistant
Now let me look at the existing tests related to content hash mismatch:
- Assistant
Let me look at the test for content hash mismatch to understand the expected behavior better.
- Assistant
Now let me understand the issue better. The problem is that on the second install, the content hash doesn't match. This is likely caused by
.apm-pinbeing written to the package directory after the first install's hash computation, but being included in the second install's hash computation.Let me look at how install_path works for subdirectory dependencies:
- Assistant
Let me understand how the single-file dependency path works. Let me search for the flow when a dependency like
.apm/instructions/testing.instructions.mdis downloaded: - Assistant
Let me now look at the issue from a different angle. The key is that the second install is using a cached or fresh download but the hash is different from the lockfile. Let me look at the
expected_hash_change_depsmechanism and the resolve phase: - Assistant
Let me look at the resolve phase to understand how
expected_hash_change_depsis populated: - Assistant
Let me look at how the download works for virtual file dependencies (like
.apm/instructions/testing.instructions.md): - Assistant
Let me trace through how virtual file dependencies are downloaded and how the install path is determined. Let me look at the download code:
- Assistant
Let me look at the download strategies for virtual file dependencies:
- Assistant
Let me look at how virtual file downloads work:
- Assistant
Let me understand how the download works for virtual file dependencies. Let me look at the GitHub downloader and how it handles single-file deps:
- Assistant
Let me search more broadly for how single-file dependencies are handled:
- Assistant
Let me look at the GitHub downloader to understand how it handles virtual file dependencies.
- Assistant
Let me look at the
download_virtual_file_packagemethod: - Assistant
Let me look at the
download_virtual_file_packagemethod: - Assistant
Now I'm starting to understand the flow better. Let me look at how the second install handles cached vs re-downloaded content, particularly for the
#mainref: - Assistant
Let me look at the
detect_ref_changefunction to understand how it works: - Assistant
Let me look at the
detect_ref_changefunction: - Assistant
Now I understand the basic flow. Let me trace the exact issue. On the first install with
--update, the hash check is skipped. On the second install without--update, the hash is recomputed. Let me look at whatyaml_to_strdoes since the virtual package generates anapm.ymleach time: - Assistant
Let me look at the
yaml_to_strfunction to see if it produces deterministic output: - Assistant
Now let me look at what happens with the
.apm-pinmarker. I suspect the issue is that.apm-pinis written after the first install's hash computation, and on the second install, it's already present in the directory. Let me look at whether.apm-pinis properly excluded from the hash computation. Let me also look at how the virtual file package directory is cleaned between installs: - Assistant
Let me look at the test file for content hash mismatch:
- Assistant
Let me look more carefully at what happens with virtual file packages during the second install. The key question is: what changes between the first and second install that causes the hash to differ?
- Assistant
Let me now focus on a different angle. Let me look at how the
_download_package_from_artifactorymethod handles Azure DevOps repos, since the issue involves Azure DevOps: - Assistant
Now I understand the basic flow. Let me trace through the exact issue. The key observation from the error log is that:
- First install with
--update --forceworks - Second install without
--updatefails with hash mismatch
The hash comparison is between what was stored in the lockfile and what is computed fresh. Let me look at what happens between the first and second install. The
.apm-pinmarker is written AFTER the hash is computed and lockfile is saved. On the second install, the.apm-pinalready exists.Let me look at how the hash is computed and what could cause the mismatch:
- First install with
- Assistant
Let me look at the actual lockfile to understand what's stored:
- Assistant
Now I have a better understanding of the code structure. Let me trace the core issue. The problem is:
- First install (with
--update --force) works, computes hash, stores it in lockfile - After lockfile is written,
.apm-pinmarker is written to the package directory - Second install: the package is "re-downloaded" but the
.apm-pinmarker already exists in the directory - The hash is computed over the directory which now includes
.apm-pin, but this marker was NOT present when the first hash was computed
Wait, but
.apm-pinis excluded from the hash. Let me re-read the hash computation code: - First install (with
- Assistant
Now I need to understand the issue better. Let me look at the
download_virtual_file_packagemethod more carefully and trace through what happens between the first and second install. The key issue is: whendownload_virtual_file_packageis called the second time, does it clean the directory? - Assistant
Now I see the core issue! Looking at
download_virtual_file_package:- It does
target_path.mkdir(parents=True, exist_ok=True)- does NOT clean the directory - It writes the file and
apm.yml - But it does NOT clean up existing files
And importantly, on the first install, the
.apm-pinmarker is written AFTER the lockfile is saved (bysync_markers_for_lockfile). So on the second install, the.apm-pinmarker is already present in the directory.But
.apm-pinis excluded from the hash. So that shouldn't cause the mismatch...Let me look at this from a different angle. Let me check what
dep_keyis used for virtual file packages and how the lockfile stores them: - It does
- Assistant
Let me look at the tests for virtual file package download to understand the flow better:
- Assistant
Let me look at the test for virtual file package download to understand the expected behavior:
- Assistant
Let me now focus on understanding the actual bug. Let me run the existing tests first to make sure they pass:
- Assistant
Let me check the test class name
- Assistant
Let me install the package first.
- Assistant
Now let me trace through the exact issue more carefully. The key is understanding what happens on the second
apm install:- First install:
download_virtual_file_packagedownloads the file and creates a package structure (includingapm.yml+.apm/instructions/testing.instructions.md) - Hash is computed on this fresh directory
- Lockfile is written with the hash
.apm-pinmarker is written to the package directory- Second install:
download_virtual_file_packageis called again, BUT the directory already has.apm-pin
The
.apm-pinis excluded from root-level files. But what about theapm.yml? It's regenerated each time bydownload_virtual_file_package...Let me look at
build_download_refto understand what happens on the second install: - First install:
- Assistant
Let me now focus on understanding the exact issue. Let me look at what happens when a virtual file package is downloaded the second time - specifically what
dep_keyis used and how the lockfile stores the hash: - Assistant
Let me now carefully look at what happens on the second install. The
download_virtual_file_packagemethod doesn't clean the directory. On the second install, the directory already has files from the first install plus the.apm-pinmarker.Let me check if the regular
download_packagecleans the directory but the virtual file download doesn't: - Assistant
Now I see the key difference! The regular
download_packagecleans the directory before downloading, butdownload_virtual_file_packagedoes NOT. This means on the second install, the virtual file package directory still has the.apm-pinmarker from the first install.But
.apm-pinis excluded from the hash... so that shouldn't be the issue. Let me look more carefully at what changes. Let me check ifbuild_download_refchanges the download reference between the two installs: - Assistant
Now I'm starting to understand the flow. On the second install,
build_download_refwould check if there's a locked dep withresolved_commit. If the locked dep hasresolved_commit, it would use that SHA. If not, it uses the manifest ref (#main).Let me now create a reproduction script to understand the exact issue:
- Assistant
Good,
.apm-pinis properly excluded from the hash. So that's not the issue. Let me think more carefully about what could cause the hash to differ.Let me trace through the flow for the second install more carefully. The key is: what does
download_virtual_file_packagedo differently on the second install? - Assistant
The
.apm-pinis properly excluded. Let me look at the exact scenario more carefully. The issue is thatdownload_virtual_file_packagedoesn't clean the directory before downloading (unlike regular package downloads). Let me trace through what happens on the second install more carefully by looking at the resolve phase and how deps are downloaded: - Assistant
Now I see the key insight! On the second install, the
download_callbackin the resolve phase returns the existing path (cached). But in the integrate phase,GitRemoteMaterializer.acquire()re-downloads. Thedownload_virtual_file_packagedoes NOT clean the directory (unlike regular packages which_rmtreefirst).But the
.apm-pinis excluded... Let me check if there's something else going on. Let me look at what_resolve_download_strategydoes and how the integrate phase decides what to download: - Assistant
Let me search more broadly for pre_download_results.
- Assistant
Let me look at the download phase to understand when pre-downloads happen:
- Assistant
Now I understand the full flow. Let me trace through the exact scenario:
-
First install (
--update --force):download_virtual_file_packagedownloads the file fresh, createsapm.yml+ file in.apm/instructions/- Hash computed on the directory
- Lockfile stores the hash
sync_markers_for_lockfilewrites.apm-pinto the package directory
-
Second install (
apm install):- Download phase checks if the dep needs to be re-downloaded
- The
_pd_locked_chk.resolved_commitis probably None for a virtual file package (since it's#main) - So the download proceeds again via
download_virtual_file_package - BUT
download_virtual_file_packagedoes NOT clean the directory - it usesexist_ok=True - The old
.apm-pinmarker is still present (but excluded from hash)
-
- Assistant
Hmm, the hashes match. So
.apm-pinis properly excluded. Let me think about what else could be different...Wait, I think the issue might be more subtle. On the first install with
--update --force, the file is downloaded fresh. On the second install, thedownload_virtual_file_packagere-downloads the file. But maybe the file content has changed (since it's#main, not pinned)?Actually, looking more carefully at the issue description, I see the user says the FIRST install works but the SECOND fails. Let me look at whether the
download_virtual_file_packageis even called on the second install, or if there's a caching mechanism that uses the existing directory: - Assistant
Let me look at the
_should_skip_redownloadfunction: - Assistant
Let me now look at the integrate phase to see how it decides whether to download or skip:
- Assistant
- Assistant
Now I see a potential issue. The
download_virtual_file_packagemethod regeneratesapm.ymlevery time, and thedescriptioncould differ if the file content changes. But more importantly, the method does NOT clean the directory before writing, which means if it's called a second time (from the pre-download phase), theapm.ymlgets regenerated.But actually, the real issue might be more subtle. Let me check if the package is pre-downloaded in the download phase AND then re-downloaded in the integrate phase:
- Assistant
Now I understand the issue clearly. The hash is deterministic when the content is the same, and different when the content changes. So the issue must be that the file content on the remote has changed between the first and second install (since
#mainis an unpinned branch reference).But wait - looking at the
_resolve_download_strategymore carefully, I see that for virtual file packages with#main:is_cacheable = False(branch refs are not cacheable)lockfile_match = False(becauselocked_dep.resolved_commitis likely None for ADO virtual files)- So
skip_download = False, and the package is re-downloaded every time
The key issue is: even when the content hasn't actually changed, the download still happens because there's no mechanism to cache virtual file packages with branch refs. And when the download happens, if the content HAS changed on the remote, the hash will differ from the lockfile.
But the real fix should be: when the
resolved_commitis not available for a dep (common for virtual file packages), the hash check insources.pyshould not fail with a hard error. Instead, it should add the dep toexpected_hash_change_depsso the hash check is skipped. - Assistant
Let me look at what's at line 103:
- Assistant
Now I understand the issue! Let me trace through the exact flow:
- First install (
--update --force): virtual file package downloaded, hash stored in lockfile - Second install (
apm install):_resolve_download_strategyis called- For virtual file packages with
#mainand noresolved_commitin lockfile:locked_dep.resolved_commitis None → the git HEAD check and content-hash fallback are BOTH skippedlockfile_match = Falseskip_download = False→ package is re-downloaded
- After re-download in
GitRemoteMaterializer.acquire(), hash is compared with lockfile → MISMATCH
- For virtual file packages with
The root cause is:
_resolve_download_strategyonly falls back to_should_skip_redownloadwhenlocked_dep.resolved_commitis set (in theexceptbranch of the git check). Whenresolved_commitis None (common for virtual file packages from ADO), the content-hash fallback is NEVER tried. So the package is always re-downloaded, and if the content has changed on , the hash mismatch occurs. - First install (
- Assistant
Now I see the issue clearly. The
_resolve_download_strategyfunction has a gap: whenlocked_dep.resolved_commitis None (common for virtual file packages from ADO), the content-hash fallback is never tried. The package is always re-downloaded, and if the remote content has changed, the hash mismatch occurs.The fix should add a branch to handle deps without
resolved_commit: - Assistant
Now let me implement the fix. I need to add a third branch in
_resolve_download_strategyfor deps withoutresolved_commit: - Assistant
Now I need to add a third branch for deps without
resolved_commit. Let me implement the fix: - Assistant
Now I also need to fix the download phase (Phase 4) which has the same gap. Let me also add the content-hash fallback there:
- Assistant
Now I see the same gap in the download phase. When
resolved_commitis None, the content-hash fallback is never tried. Let me fix this: - Assistant
Now let me run the existing tests to make sure the fix doesn't break anything:
- Assistant
Good, the test passes. Let me now run the broader test suite to make sure my changes don't break anything:
- Assistant
Let me run without the timeout option.
- Assistant
All 1477 unit tests pass. Let me also run the broader test suite:
- Assistant
The spec_conformance tests need jsonschema. Let me run without those.
- Assistant
Let me install jsonschema and run again.
- Assistant
Let me run the tests again.
- Assistant
Hmm, all tests are skipped. Let me check why.
- Assistant
All 15812 tests pass. Let me also run the integration tests:
- Assistant
The failing test is about ADO bearer authentication, unrelated to my changes. Let me verify by checking if it was already failing:
- Assistant
The test was already failing before my changes. Let me restore my changes:
- Assistant
Good, the test was already failing before my changes. Let me run the integration tests excluding that one:
- Assistant
Let me check if this test was also pre-existing:
- Assistant
Both test failures are pre-existing. Let me run the integration tests excluding both:
- Assistant
Let me check if this is also a pre-existing failure:
- Assistant
All 3 failures are pre-existing (running as root in this environment). Let me verify my changes are correct by reviewing them:
- Assistant
I accidentally removed two comment lines. Let me fix that:
- Assistant
Let me also restore the "Build download ref" comment:
- Assistant
Now let me run the tests one final time to make sure everything is correct:
- Assistant
All 15812 tests pass. Let me review the final changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.