Fix: make the AgentLoader preload memo the only record of a completed scan (stacked on #664) - #674
Open
AmaadMartin wants to merge 3 commits into
Conversation
added 3 commits
August 5, 2026 03:27
The `agentsAlreadyPreloaded` flag survives an invalidation that lands while a scan is in flight: the scan sets the flag after `invalidateAll()` cleared it, so the next request is served from the pre-change scan. Delete the flag. The settled `preloadInFlight` promise already records that a scan completed, and `invalidateAll()` clears it. A rejected scan now clears the memo only while it is still the current one, so a scan superseded by an invalidation cannot discard its replacement's results.
`mockRejectedValueOnce` failed whichever scan issued the first compile, so the replacement scan failed instead of the superseded one on about one run in three. The test now controls the first compile with a deferred, and fails it only after the replacement owns the memo.
Three tests repeated the same cast to reach the private invalidateAll, so one typed helper now holds it. The helper that counts compiles moves above its first caller, and the memo comment drops the claim about a leak this change does not close.
This was referenced Aug 6, 2026
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
Link to an existing issue (if applicable):
N/A
Or, if no issue exists, describe the change:
Stacked on #664. Collision check: I ran
gh pr list --repo AmaadMartin/adk-js --state open --limit 1000and diffed every adjacent PR. #664 already de-duplicates concurrentpreloadAgents()callers, so I did not reimplement it — this PR is the remaining delta on top of it. #633 restructures the same method further; whichever lands first, the other rebases.Problem: #664 keeps the
agentsAlreadyPreloadedflag next to its new promise memo. A scan sets that flag afterinvalidateAll()cleared it, so an invalidation that lands during a scan is swallowed and the next request is served from the pre-change scan. #664 also clears the memo from any rejected scan, so a scan that fails after being superseded discards the results of the replacement scan that a later caller already started.Solution: I deleted the flag. The settled
preloadInFlightpromise already records that a scan completed, andinvalidateAll()clears it, so one field is the only source of truth. A rejected scan clears the memo only while it is still the current one.Two disclosures.
The tests reach
invalidateAllthrough oneas unknown as {invalidateAll: () => void}cast, hoisted into a singleinvalidateAll(loader)helper. The method is private and only thefs.watchcallback calls it, and driving a real watcher event would be timing-dependent across the CI matrix. I did not widen the method topublic.This PR does not close the leak of superseded
AgentFileobjects. A scan thatinvalidateAll()supersedes keeps writing intopreloadedAgentsafter the record was emptied, and the replacement scan overwrites those entries without disposing them, so their temp directories survive until the next invalidation. Cancelling a superseded scan needs a generation guard, which is a separate change.Testing Plan
Please describe the tests that you ran to verify your changes. This is required for all PRs that are not small documentation or typo fixes.
Unit Tests:
[x] I have added or updated unit tests for my change.
[x] All unit tests pass locally.
Command:
npx vitest run --project unit:dev dev/test/utils/agent_loader_test.ts— 35 passed. I ran it 8 times to check for flakes.Two new tests:
does not reuse a scan that was invalidated while it was in flightkeeps the replacement memo when a superseded scan failsEvery changed line and branch in
preloadAgents()andinvalidateAll()is covered (v8 JSON report, no uncovered entry in the changed region).Mutation results. Each mutation was applied alone and the suite re-run:
does not reuse a scan that was invalidated while it was in flightfails withAssertionError: expected 3 to be greater than 3.this.preloadInFlight === scanguard:keeps the replacement memo when a superseded scan failsfails withAssertionError: expected 9 to be 6.preloadInFlightininvalidateAll(): four tests fail, including the updatedresets preload cache when invalidateAll is called.One existing test was rewritten, not added to:
resets preload cache when invalidateAll is called (simulates file-change reload). I rewrote it because it asserted the removedagentsAlreadyPreloadedflag, so it cannot compile against this change. It now pins the same behaviour through the publiclistAgents()path, by asserting that the next request recompiles. Its name and intent are unchanged, and mutation 3 above shows it still fails when the fix is removed. No test was deleted, skipped or weakened.Manual End-to-End (E2E) Tests:
adk web <agents-dir> --reload_agents.for i in 1 2 3 4; do curl -s localhost:8000/list-apps & done; wait. The responses match and each agent is compiled once.CI does not run on this PR:
.github/workflows/validation.yamltriggers onpull_request: branches: [main]and this PR targets #664's branch. Local validation on commitbe3200ab:npm run buildpassed,npm run lintpassed,npm run format:checkpassed, and the targeted suite passed.dev/test/cli/cli_create_test.tshas one failure that also reproduces without this change; it is unrelated.Checklist
[x] I have read the CONTRIBUTING.md document.
[x] I have performed a self-review of my own code.
[x] I have commented my code, particularly in hard-to-understand areas.
[x] I have added tests that prove my fix is effective or that my feature works.
[x] New and existing unit tests pass locally with my changes.