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
Let me start by exploring the repository structure to understand what we're working with.
Assistant
Let me look at how the hashes are computed and where they might be different between runs:
Assistant
Good, so the
content_hashmodule handles the case of.apm-pin. Let me look at the issue more carefully.The bug is that the user reports:
- First
apm install --update --forceworks (kinda) - Second
apm install(without flags) fails with "Content hash mismatch"
The error happens for
.apm/instructions/testing.instructions.mdwhich is a file-based dependency. So this is different - it's a single file dependency.Let me look at the test to understand more aboutthe expected behavior:
- First
Assistant
Now let me see how the install path is set up for these single-file dependencies:
Assistant
Let me understand more about how a file dependency gets stored, especially what kind of install_path is used:
Assistant
Let me see how a "file" dep like
dev.azure.com/<org>/tools/github-copilot-agent-skills/.apm/instructions/testing.instructions.mdis set up. The hash check happens via_compute_hash(install_path). For a virtual file dep, the install_path is the package directory containing the file. But what if the hash depends on file content and the content gets changed across runs (like thesimplifymentions atesting.instructions.mdwhich is added)?Let me look at how virtual file deps get set up and what file is actually hashed:
Assistant
Let me look at the file vs directory behavior in
get_install_path:Assistant
Let me search for where the file might be downloaded/copied:
Assistant
Now let me trace through the flow to understand what's happening. The user reports the hash mismatch for a single file dep
.apm/instructions/testing.instructions.md. Let me look at how this kind of dep is downloaded and how the hash is computed:Assistant
Let me look at how a file gets downloaded. Let me also search for sparse-checkout or single-file fetching:
Assistant
Now let me understand: so when downloading a
.instructions.mdfile as a virtual file, the download creates a directory attarget_path/.apm/instructions/<filename>. Let me see what's after this:Assistant
Now let me look at the rest of the function in
download_virtual_file_package:Assistant
Now let me look at
download_virtual_file_packagemore thoroughly. There's an issue:Assistant
Now let me see what happens with the hash of this downloaded file. Let me search for
package_hashes:Assistant
Now let me see how the cached source uses the hash:
Assistant
Let me look at line 525:
Assistant
Now let me look at the test for "Content hash mismatch":
Assistant
Now let me try to reproduce the issue. First let me look at the apm.yml and apm.lock.yaml files to understand the dependencies:
Assistant
Now let me try to reproduce the bug. Let me first run the existing tests for content hashing:
Assistant
Good, the test passes. Now let me understand what specifically happens in the user's scenario. Let me trace through what happens for a
.instructions.mdfile:Assistant
Let me look at the
download_virtual_file_packagecode more carefully:Assistant
Let me look at how ADO repo URLs are parsed:
Assistant
Let me look at the full FreshDependencySource.acquire to understand when hashes are computed and what gets included:
Assistant
Now let me look at
download_package:Assistant
Let me also check what
_download_ado_filedoes:Assistant
Let me re-read the hash mismatch code path:
Assistant
Let me look at
make_dependency_source:Assistant
Now let me check
_should_skip_redownload:Assistant
Let me look at the
apm.ymlgeneration more carefully and look for non-determinism:Assistant
Let me look at the part of download that handles the integration step. Let me look for where the hash gets generated for the file:
Assistant
Let me look at how the integrationtemplate might add additional files (like .apm-pin) that could cause hash mismatch:
Assistant
Let me check if there's a pre-download path that captures the hash differently:
Assistant
Let me look at what
build_download_refdoes:Assistant
Let me also look at the integration part - the
download_ado_filein particular. The ADO API returns file content as base64-encoded JSON. Let me see whatit does:Assistant
The ADO API response is JSON with the content base64-encoded. Let me see how it's processed:
Assistant
Let me look at the tests for download_virtual_file_package:
Assistant
Let me look at git log to see recent fixes:
Assistant
Let me check what
response.contentreturns for ADO API:Assistant
Let me check how GitHub handles file downloads:
Assistant
I've found the bug! Let me verify by writing a quick test:
Assistant
Let me look at existing tests for ADO file download:
Assistant
Let me look at the URL builder:
Assistant
Let me also examine the part with
_extract_contents_api_payloadfor ADO - it currently doesn't use it for ADO:Assistant
Now I'll write a fix. The fix should:
- Either add
&download=trueto the URL (cleaner approach). - Or parse the JSON response and decode the base64 content.
I'll go with both: try
download=truefirst, and if that fails, fall back to parsing the JSON.Actually, looking at the ADO docs, the
download=truequery parameter is the recommended approach for getting raw file content. Let me update the URL builder and the function.Wait, I should also be careful: there might be other places that call
build_ado_api_urland expect the JSON response. Let me check.- Either add
Assistant
Let me also look at how
_make_mock_responseis defined:Assistant
Now I understand the bug. The
download_ado_filefunction returnsresponse.contentdirectly, but the ADO API returns JSON with the file content base64-encoded. So the function returns the JSON bytes instead of the actual file content. This causes a hash mismatch on subsequent installs when the JSON serialization may differ.Let me first reproduce the issue:
Assistant
I've confirmed the bug. Let me run the existing tests to make sure they still pass after my fix:
Assistant
Now let me also check the description parsing logic in
download_virtual_file_package:Assistant
Now let me make the fix. I'll add the JSON parsing logic to the
download_ado_filefunction. Let me first check the imports:Assistant
Now I'll implement the fix. I'll add a helper function for ADO that properly extracts the file content from the JSON response:
Assistant
Now I'll make the fix. I'll change
download_ado_fileto properly extract the file content from the JSON envelope:Assistant
Now I'll make the fix. I'll update the
download_ado_filefunction to properly extract the file content from the JSON envelope returned by the ADO API:Assistant
Now let me add the
_extract_ado_items_responsemethod. Let me find the right place to add it:Assistant
Now let me add the
_extract_ado_items_responsemethod right after_extract_contents_api_payload:Assistant
I see there are duplicate lines. Let me clean them up:
Assistant
I see there's still duplicate code in the
_extract_contents_api_payloadfunction. Let me look at it and fix it:Assistant
Let me fix the duplicates and also add the missing blank line:
Assistant
Let me fix the missing blank lines:
Assistant
Let me fix the duplicate definition and the missing blank line:
Assistant
Let me fix the duplicate line and missing blank line:
Assistant
Let me rewrite this section cleanly:
Assistant
Now the code is clean. Let me run the existing tests to verify:
Assistant
There's a syntax error. Let me check the file:
Assistant
The download_ado_file function is incomplete. My edit truncated it. Let me look at the original function and fix it:
Assistant
Let me revert my changes and re-apply them properly:
Assistant
Now I'll make focused changes to only replace the two
return response.contentlines indownload_ado_file:Assistant
Let me use sed to make the change:
Assistant
Good, the file still parses. Now let me add the
_extract_ado_items_responsemethod. Let me find a good place to add it:Assistant
Now let me add the
_extract_ado_items_responsemethod right after_extract_contents_api_payload:Assistant
All existing tests still pass. Now let me verify the fix actually works by running my reproduction script:
Assistant
Let me check the actual byte content:
Assistant
OK so
b'---\\n...'is actuallyb'---\n...'(62 bytes). So the content is 62 bytes, but my function returned 84 bytes. Let me check what the function actuallyreturned:Assistant
I see the issue. The
contentMetadata.encodingfield tells us the original file encoding, not the encoding of thecontentfield. Thecontentfield is always base64-encoded. Let me fix my approach:Assistant
The fix works! Now let me run the existing tests to make sure they still pass:
Assistant
Let me check the failed test:
Assistant
Let me check if this test was failing before my change:
Assistant
So the test was passing before my change. Let me check what my change did to break it.
Actually, my change only modified
download_ado_fileand added a new method_extract_ado_items_response. I didn't changedownload_github_file. Sowhy would the test fail?Let me check what the test does:
Assistant
Let me look at the test that failed and understand its expectedbehavior:
Assistant
Let me check if this test is failing with my current changes:
Assistant
Let me trace through the code to see what happens:
Assistant
Let me see what's in my current version of the file:
Assistant
Let me check the current state of
_extract_contents_api_payload:Assistant
I see - my insertion deleted the body of
_extract_contents_api_payload! The regex I used matched up toreturn body(line 1096 originally), and I replaced everything from the start of_extract_contents_api_payloadto thatreturn bodyline with my new_extract_ado_items_responsemethod. But that means_extract_contents_api_payloadis now incomplete!Let me look at the current state and fix it:
Assistant
I see - the body of
_extract_contents_api_payloadis incomplete! Let me restore the missing part:Assistant
Now let me apply the changes more carefully. First, modify the
download_ado_filefunction:Assistant
Now let me carefully add the new method without disturbing the existing one:
Assistant
All tests pass. Let me run a broader set of tests:
Assistant
Let me also run more tests for related files:
Assistant
All 15812 unit tests pass. Now let me run integration tests too:
Assistant
Let me check if this failure was pre-existing:
Assistant
Good, that failure is pre-existing and not related to my change. Let me restore my changes:
Assistant
Oh, it seems that when I ran the tests, one of them modified the apm.lock.yaml. Let me restoreit:
Assistant
Now let me also verify my fix works with the actual repro test:
Assistant
The fix works for both JSON orderings. Now let me also write a test to demonstrate the original bug is fixed:
Assistant
All my custom tests pass. Let me also verify by running the original test suite one more time:
Assistant
All 274 tests pass. Let me also run a broader set of integration tests:
Assistant
The 2 failures are related to permission denied tests that work differently when running as root. Let me verify these failures are pre-existing:
Assistant
Good, the failures are pre-existing. Let me restore my changes:
Assistant
Looks like running the integration tests modified some files in
.agents/andapm.lock.yaml. Let me restore all of them since they should not be part of my changes:Assistant
Some untracked files were created. Let me see if theseare leftovers from tests:
Assistant
These look like local integration test outputs. Let me check thegit ls-files:
Assistant
These are integration test outputs and not from my code changes. Let me now check if the description extraction logic would now work correctly:
Assistant
Now let me also check for any other places in the code where this bug could manifest:
Assistant
Let me check what other places use
download_ado_file. Thedownload_virtual_file_packageingithub_downloader.pycallsdownload_raw_file, which calls_download_ado_filefor ADO. So my fix todownload_ado_filewill be effective for virtual file packages like.instructions.md.Let me also check
_download_ado_file:Assistant
Let me verify the fix is complete by running the related tests one more time:
Assistant
All 463 tests pass. Let me also run my reproduction test:
Assistant
All tests pass. Let me now also do a final review of the full diff:
Assistant
The diff looks clean. Let me also clean up the leftover test files:
Assistant
The only change is to
src/apm_cli/deps/download_strategies.py. Let me view the current state of the changes to the file:Assistant
The changes look good. Let me also think about whether the
validate=Truecall could fail on valid base64 inputs that contain newlines or spaces. Let me test:Assistant
Interesting -
validate=Truefails for MIME base64 (with newlines). But the fallback tovalidate=Falsewould handle it. Let me verify:Assistant
My fallback logic handles both cases correctly. Let me run the full test suite once more to confirm everything still works:
Assistant
All virtual file package tests pass too. Now let me look at the final fix once more:
Assistant
The fix looks clean. Let me also check if there's anything else in the codebase I need to address:
Assistant
Let me also verify that there are no other issues. Let me run a more focused set of tests one more time:
Assistant
All 505 tests pass. Let me also think about the original bug more carefully.
Let me re-read the issue and the bug description once more to make sure I've covered the main scenario:
Assistant
Now let me also think about what about description parsing - the user mentioned it might affect the description:
Actually, the description extraction happens in
download_virtual_file_package. With the fix, the actual file content is now available for description extraction. The description will be properly extracted from the frontmatter. This means:-
First run with --update --force: File is downloaded (now correctly as actual content), apm.yml is generated with description from frontmatter. Hash is computed correctly.
-
Second run without flags: File is re-downloaded (correctly), apm.yml is regenerated with same description, hash matches.
Let me also think about whether there could be any other issues. Looking at the user's report, they mention: "Partial clone (--filter=blob:none) failed for https://dev.azure.com//tools; retrying with full bare clone."
This is just a warning, not related to the bug. The bug is purely the JSON envelope issue.
Let me also think about:
-
Assistant
The description extraction in
download_virtual_file_packagewill now correctly extract the description from the actual file content. This is a side benefit of the fix.Let me run one final comprehensive test:
Assistant
All 1987 tests pass. Let me also run a quick sanity check on the syntax of the modified file:
Assistant
Let me also make sure the final git diff is minimal and focused:
Assistant
Let me also write a quick verification script to confirm the bug is fixed:
Assistant
The fix is working correctly. Let me also run the full test suite one more time to confirm nothing is broken:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.