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 the codebase and then find the relevant code related to the "Content hash mismatch" issue.
Assistant
Let me explore the source structure and find relevant code about content hash mismatch.
Assistant
Let me look at the content hash utility and the instruction file handling.
Assistant
Now I see that
.apm-pinis excluded from the hash. This is the cache-pin marker. Let me understand what's happening with instructions files. The instructions filetesting.instructions.mdis being re-downloaded each time, and the hash mismatch occurs.The issue is that when downloading instructions files, the cache-pin marker (
.apm-pin) is written after the hash is recorded. So the first time, the hash is computed before the.apm-pinis written, and recorded in the lockfile. The second time, the package is re-downloaded fresh, and... wait, if.apm-pinis excluded, that shouldn't matter.Let me re-read the logs:
- First install (with
--update --force): Downloads everything, including instructions. - Second install: instructions file is re-downloaded fresh and the hash doesn't match.
- First install (with
Assistant
Let me look at recent changes related to content hash and the instruction handling.
Assistant
Let me look at the integrate.py file that also references content hash mismatch.
Assistant
Let me look at the beginning of the
_resolve_download_strategyfunction.Assistant
Let me look at how virtual packages (single files like instructions) are downloaded.
Assistant
Let me look at how virtual packages are resolved and downloaded in the apm_resolver.
Assistant
Let me look at the download phase which handles virtual packages.
Assistant
Let me look at the download strategies to understand how virtual packages are materialized.
Assistant
I can see that the virtual file package generates an
apm.ymlfile. Let me check ifyaml_to_stris deterministic and look at the package name generation.Assistant
Let me look at how the content_hash is recorded in the lockfile.
Assistant
Let me look at the lockfile phase to understand how content hashes are attached and the
_attach_content_hashesmethod.Assistant
Let me search for files written to the install path during install that might not be excluded from the hash.
Assistant
Let me look at the
build_download_refanddriftmodule to understand how the download ref is built.Assistant
Let me look at how the integrate phase processes each dependency - the main integration loop.
Assistant
Let me find the main per-dependency integration function.
Assistant
Let me look at the
run_integration_templateand the post-acquire flow in sources.py.Assistant
Let me look at
get_virtual_package_nameand the DependencyReference model.Assistant
Let me look at the git history of the content_hash.py file and check recent changes related to this issue.
Assistant
Let me look at
_resolve_commit_sha_for_refto understand the SHA resolution for virtual files.Assistant
Let me reproduce the issue with a test script. First, let me look at existing tests for content hash and virtual packages.
Assistant
Let me write a reproduction script to test the virtual file package download hash consistency.
Assistant
Let me create a reproduction script to test the virtual file package hash consistency across two downloads.
Assistant
The hashes match when content is the same. The issue must be something modifying the install_path between installs. Let me look at the
download_packagedispatch and the skill deploy copytree that was mentioned in commit #1153.Assistant
Let me look at the skill_integrator deploy copytree and what files might be written to the install path.
Assistant
Let me look at the
get_install_pathfor virtual packages.Assistant
Let me look at the ADO file download implementation to see if it could return non-deterministic content.
Assistant
Let me check the ADO API URL builder.
Assistant
Let me check
format_short_shaand verify the resolved_commit handling.Assistant
Let me look at how the lockfile records resolved_commit from InstalledPackage.
Assistant
Let me check the CHANGELOG for v0.16.0 to understand what changed.
Assistant
Let me look at the pre-download phase more carefully to see if it computes a hash.
Assistant
Let me look at the resolve.py BFS download callback.
Assistant
I see at line 437-446:
if install_path.exists(): _force_semver_resolve = (...) if not _force_semver_resolve: return install_pathSo the download_callback SKIPS download if the install_path already exists. This is the BFS resolution callback. So during BFS, if the install_path exists, it skips.
But for the FIRST install, the install_path doesn't exist yet. So the BFS callback downloads it.
Let me look at what the BFS callback does for a virtual file package. Let me look further down.
Assistant
Let me look at
make_dependency_sourceto understand which source is used whenskip_downloadis True.Assistant
Let me look at the CachedDependencySource to understand how it computes the hash in the first install.
Assistant
Let me search for tests that test the virtual file package install flow and content hash.
Assistant
Let me look at the test harness for FreshDependencySource to understand how to set up a reproduction.
Assistant
Let me look at the hash mismatch test to understand the scenario.
Assistant
Let me look at the diff_aware_install_e2e test to understand the full install test harness.
Assistant
Let me look at the test fixtures and the virtual package branch ref test.
Assistant
Let me look at the test fixtures and the FIXTURE_REPO definition.
Assistant
Let me look at how the install pipeline is tested with mocked downloaders.
Assistant
Let me look at how the resolve phase processes direct dependencies and whether virtual file packages go through the BFS callback.
Assistant
Let me look at the
_purge_cached_semver_paths_for_updatefunction and the--forcehandling.Assistant
Let me check if there's a GitHub token available and look at the fixture repos used in tests.
Assistant
Let me search for any writes to the package install_path during integration.
Assistant
Let me look at the GitHub file download to understand the raw file API, so I can set up a local mock.
Assistant
Let me look at the
resolve_commit_sha_for_refimplementation to determine if ADO gets a resolved commit.Assistant
Let me look at the install command to understand how to orchestrate the phases programmatically.
Assistant
Let me look at the InstallService.run to understand the pipeline orchestration.
Assistant
Let me look at the pipeline orchestration.
Assistant
Let me look at the lockfile build phase and what happens after integrate.
Assistant
Let me search for tests that invoke the full install pipeline with mocked downloaders.
Assistant
Let me look at the
_make_downloader_no_networkhelper and write a full pipeline reproduction.Assistant
Now let me write a comprehensive reproduction that runs the full install pipeline with a mocked downloader.
Assistant
The MagicMock logger is causing issues. Let me use a real logger.
Assistant
The install is reading the wrong apm.yml. Let me debug the APMPackage setup.
Assistant
Let me check how
run_install_pipelineuses the apm_package and project root.Assistant
I need to change to the project directory. Let me update the reproduction.
Assistant
My repro with deterministic content succeeds. The issue must involve non-deterministic content or a file modification. Let me examine the instruction integrator to see if it modifies the install_path.
Assistant
Let me check if
detect_package_typeor_pre_deploy_security_scanmodifies the install path.Assistant
Let me look at how the apm_resolver is called with modules_dir in resolve.py.
Assistant
Let me look at how the resolver is called and what modules_dir is passed.
Assistant
Let me check what the
--forceflag does in the install pipeline.Assistant
Let me search for tests of the ADO file download to understand the expected response format.
Assistant
Let me look at the rest of the GitHub file download to see if it decodes JSON from the Contents API.
Assistant
Let me add debugging to my repro to print the install_path contents and hash at each step.
Assistant
Let me add tracing to
download_virtual_file_packageto see how many times it's called in the first install.Assistant
Let me check if
_pre_deploy_security_scanwrites anything to the install path.Assistant
Let me look at the
_resilient_getimplementation to check if it sets a default Accept header.Assistant
Let me look at the
resilient_getin download_strategies.py.Assistant
Let me look at the cleanup phase to see if it modifies the install path.
Assistant
Let me test if
yaml.safe_dumpproduces deterministic output.Assistant
Let me reproduce with an ADO-specific dep_ref to check if the code path differs.
Assistant
Let me run my full repro with an ADO dep_ref to see if the behavior differs.
Assistant
I've done extensive analysis. Let me look at the git history of the supply-chain check to understand the context better.
Assistant
Let me look at the heals module to understand the branch-ref drift heal.
Assistant
I found the root cause. The
BranchRefDriftHealdoes NOT apply for deps whose lockfileresolved_commitisNone(unpinned, like ADO virtual file packages where SHA resolution fails). So when the content legitimately changes (branch advances), these unpinned deps are re-downloaded but NOT added toexpected_hash_change_deps, causing the supply-chain hash check to hard-fail.Let me verify this understanding and implement the fix.
Assistant
Now I understand the root cause. Let me trace through the logic for an ADO virtual file package on the second install:
resolved_commitisNone(ADO SHA resolution fails for virtual files)ref_changedisFalse(manifest ref "main" matches lockfile ref "main")- So the dep is NOT added to
expected_hash_change_deps lockfile_matchisFalse(noresolved_committo verify, and not registry)BranchRefDriftHealdoesn't apply (requireslockfile_match=TrueAND non-Noneresolved_commit)skip_downloadisFalse→ re-download viaFreshDependencySource- Supply-chain hash check fires and hard-fails because the dep wasn't marked as expecting a hash change
For pinned branch deps, the
BranchRefDriftHealdetects drift and adds the bypass key. But for unpinned deps (noresolved_commit), there's no heal to detect drift, so the hash check hard-fails.Let me implement the fix in
_resolve_download_strategy:Assistant
Now let me implement the fix. I'll add a condition to mark unpinned deps (no
resolved_commit) as expecting hash changes, since they're re-downloaded every install and their content can legitimately change:Assistant
Now let me verify the fix. First, let me test with deterministic content (should still work), then with changing content (should now succeed instead of failing).
Assistant
The fix works. Let me verify it doesn't break the deterministic case (content unchanged):
Assistant
Let me verify that without the fix, the changing-content case fails (to confirm the fix is necessary):
Assistant
The fix is confirmed. Without it, the second install fails with "Content hash mismatch"; with it, it succeeds. I notice my earlier repro runs modified some files in the repo. Let me revert those unintended changes, keeping only my fix:
Assistant
Let me check if there are any other unintended changes from the repro runs:
Assistant
Only my fix remains. Now let me run the existing tests to make sure I didn't break anything:
Assistant
All 1500 tests pass. Now let me also add a defensive fix to clear the install path in
download_virtual_file_packagebefore writing, consistent with how regular packages clear their target (line 1612-1615). This ensures leftover files from previous downloads don't corrupt the content hash:Assistant
Now let me run the relevant tests again to ensure both changes work correctly:
Assistant
All tests pass. Let me re-run my repro scripts to confirm both fixes work together:
Assistant
Both scenarios work correctly. Let me run a broader set of install-related tests to ensure nothing is broken:
Assistant
Let me check if that test failure is pre-existing (unrelated to my changes):
Assistant
The test failure is pre-existing and unrelated to my changes. Let me revert the
apm.lock.yamlthat got modified again:Assistant
Let me review my final changes:
Assistant
The changes look correct. Let me verify the edge cases by running a comprehensive test, including the heal and drift tests:
Assistant
Let me verify
_rmtreeis accessible in the scope where I use it:Assistant
Let me run the full unit test suite to confirm everything works (excluding the known pre-existing failure):
Assistant
All 15811 unit tests pass. Let me clean up my temporary repro scripts:
Assistant
The
apm.lock.yamlgot modified by test runs. Let me revert it:Assistant
Let me do a final review of the complete diff:
Assistant
Let me run the specific tests for
download_virtual_file_packageone final time to confirm the clearing change is safe:Assistant
All tests pass. Let me do a final check to ensure no unintended files are modified:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.