Skip to content

fix(dev): retain agent bundles while in use during watch invalidation - #1008

Merged
kalenkevich merged 2 commits into
google:mainfrom
Varun-S10:fix/issue-1002
Nov 9, 2026
Merged

kalenkevich merged 2 commits into
google:mainfrom
Varun-S10:fix/issue-1002

Conversation

@Varun-S10

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

2. Or, if no issue exists, describe the change:

Problem:
When running adk web or adk api_server, the server previously disposed the temporary compiled agent bundle right after loading the agent or immediately when invalidateAll() was triggered by a file change in watch mode. Because of this:

  1. Any lazy import() or require() inside an agent or tool during /run failed with ERR_MODULE_NOT_FOUND / MODULE_NOT_FOUND because the compiled .js bundle file was already deleted from disk.
  2. If a file changed while a request was still running, invalidateAll() immediately deleted the bundle file that the active request was still using.
  3. After invalidateAll(), AdkApiServer continued reusing the old cached Runner instance because runnerCache only checked the app name and not whether the loaded agent instance had changed.

Solution:

  • Added reference-counted bundle leases (retain() and retire()) to AgentFile.
  • Scoped await using _bundleLease = agentFile.retain?.() to active server requests in AdkApiServer instead of disposing the AgentFile itself at the end of a request.
  • Updated AgentLoader.invalidateAll() to call retire() instead of dispose(), so the old bundle stays on disk until all in-flight requests finish, and is then cleaned up automatically.
  • Added invalidation generation tracking during preloadAgents() so bundles compiled before an invalidation are not cached as fresh.
  • Updated AdkApiServer.getRunner() to recreate the cached Runner when the loaded agent instance changes after reload.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.
Test Files  24 passed (24)
     Tests  453 passed (453)

Also ran integration tests for AgentLoader:

Test Files  1 passed (1)
     Tests  11 passed (11)

Manual End-to-End (E2E) Tests:

  1. Created a TypeScript agent that reads a sibling file via import.meta.url / __dirname and performs a dynamic import() during /run execution.
  2. Started AdkApiServer and sent a POST /run request while triggering agentLoader.invalidateAll() mid-request.
  3. Verified that:
    • The in-flight request completes with HTTP 200 and the old bundle stays on disk until the request finishes.
    • Once the in-flight request completes, the retired bundle is automatically deleted from disk.
    • Subsequent POST /run requests reload a fresh bundle and run the updated agent instance.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

N/A

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve. I checked the lease refcount, retire/dispose, and the preload generation guard against source at d7537cb. All request-path consumers now take a lease, and getRunner compares the loaded agent instance, which asRunnableRoot returns unchanged for agents and workflows, so the cache stays correct across reloads. The retain?.() optional chaining is load-bearing because the existing graph-endpoint mocks omit