fix(bundle/cache): re-stamp agent source_path on warm-path cache hit (cross-installation FileNotFoundError) - #160
Open
Brian Krabach (bkrabach) wants to merge 1 commit into
Conversation
The prepared-bundle cache (~/.amplifier-agent/cache/prepared/<aaa_version>/ <bundle_sha256>/prepared.pickle) is keyed only by (aaa_version, sha256(bundle.md content)) -- a per-user key, not a per-installation one. PreparedBundle.mount_plan["agents"][name]["source_path"] bakes in an ABSOLUTE path resolved against whichever installation ran the cold prepare. A second installation of the same version (e.g. a fresh 'uv tool install' after an earlier one was removed, or a dev checkout alongside a packaged install) warm-hits the same cache entry and gets back agent source_path values pointing at the FIRST installation's site-packages tree. When that location no longer exists, make_turn_handler's cold-path agent-overlay hydration (hydrate_agent_overlay(Path(entry["source_path"]))) raises FileNotFoundError on the very first turn -- even though the CURRENT installation ships that exact vendored agents/*.md file at its own, different, path. Fix: after every warm-path deserialize, re-stamp each agent's source_path against *this* process's own AGENTS_DIR (Path(__file__)-derived, so it is always correct for whichever installation is actually running), mirroring bundle/loader.py's own cold-path rule of only stamping a path verified to exist. Adds scripts/verify-cache-restamp.py (this repo's standalone-script convention, not a pytest test -- see verify-wheel.py) covering the restamp helper directly and an end-to-end warm-path run against a pickle built for a since-deleted installation path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Embedding
amplifier-agentperdocs/INTEGRATION.md's documented recipe(
load_and_prepare_cached->inject_provider/inject_routing_matrix->Engine(turn_handler)->boot->submit_turn) from a freshinstallation can raise
FileNotFoundErroron the very first turn, with nocode path other than the vendored library involved. Reported by a downstream
embedder integrating via
docs/INTEGRATION.md(a.dot-pipeline adaptermodule that hosts
Engineas a node worker), traced back to this librarywith a minimal, dot-runner-free reproduction below.
Traceback (minimal repro,
docs/INTEGRATION.md's own embedding recipe, real API key)The path in the error belongs to a different, no-longer-current
installation of this same package/version -- not the one that raised.
Root cause
load_and_prepare_cached(src/amplifier_agent_lib/bundle/cache.py) keys itson-disk warm-path cache
(
$AMPLIFIER_AGENT_HOME/cache/prepared/<aaa_version>/<sha256(bundle.md)>/prepared.pickle)by
(aaa_version, sha256(bundle.md content))only. That key is scoped to$AMPLIFIER_AGENT_HOME, which defaults to~/.amplifier-agent-- peruser, not per installation.
bundle/loader.py's cold path enrichesbundle.agents[name]["source_path"]with an absolute path resolved against whichever installation happened to
run the cold prepare (ultimately derived from
BUNDLE_DIR = Path(__file__).parentin
bundle/__init__.py). That absolute path is part of what gets pickled intothe cache artifact.
Any second installation of the same version with an unmodified vendored
bundle.md(a freshuv tool installafter an earlier one was removed andreinstalled elsewhere, a dev checkout run alongside a packaged install, two
side-by-side venvs, a CI runner that keeps
~/.amplifier-agentacrossephemeral installs) warm-hits the same cache entry and gets back agent
source_pathvalues pointing at the first installation'ssite-packagestree.
make_turn_handlerreads that path directly(
hydrate_agent_overlay(Path(entry["source_path"]))), so the very first turnon the second installation raises
FileNotFoundError-- even though thatsecond installation ships the exact same vendored
agents/*.mdfile, at itsown, different, path.
resources.BUNDLE_DIRalready documents "resolvescorrectly whether running from source or installed in a wheel because it is
derived from
__file__" -- the pickledsource_pathis exactly the casethat guarantee doesn't reach, because it was resolved once, by a different
process, and then frozen into a cache keyed on something that doesn't capture
"which installation."
I've verified this concretely: build the cache from installation A, delete
A, install a fresh copy at a different path (B, same version, same
bundle.md), reuse the same$AMPLIFIER_AGENT_HOME-- B's first turn raisesexactly the
FileNotFoundErrorabove, pointing at A's now-deleted path.This is not a packaging gap (every individual installation ships
agents/*.mdcorrectly --scripts/verify-wheel.pyalready guards that),and it reproduces with zero application code beyond this library's own
documented embedding recipe, so it isn't a caller-side bug either.
Fix
bundle/cache.py: after every warm-path deserialize, re-stamp each agent'ssource_pathagainst this process's ownAGENTS_DIR(also__file__-derived, so always correct for whichever installation is actuallyrunning) before returning the
PreparedBundle-- mirroringbundle/loader.py's own cold-path rule of only stamping a path it hasverified exists. No cache-key change, no invalidation, no re-clone/re-install
cost; only the one baked-in-per-process field is refreshed.
Added
scripts/verify-cache-restamp.py(this repo's standalone-scriptconvention -- see
scripts/verify-wheel.py's own rationale for why thisisn't a pytest test) covering: the restamp helper directly (stale path fixed,
unresolvable name left alone, no-op without an
agentssection), and anend-to-end run of
load_and_prepare_cachedagainst a pickle built for asince-deleted installation path, confirming the warm path now returns
readable paths instead of raising.
Note for maintainers (related, out of scope for this PR)
The same root gap (a per-user cache reused across installations that may not
share Python dependency state) can plausibly also produce
ModuleNotFoundErrorfor a bundle-declared module's own pip dependency(e.g. a provider module's SDK package) if the cold prepare that built the
cache ran in a different interpreter than the one warm-hitting it later --
install_depsonly runs on the cold path. This PR fixes the concrete,reproducible
FileNotFoundErroron vendored resources; the broaderdependency-install-vs-cache-sharing question may be worth its own design
discussion.
Testing
passes all 4 checks against this branch.