Skip to content

fix(dev): retain shared agent bundles across API requests - #983

Closed
jabrailkhalil wants to merge 2 commits into
google:mainfrom
jabrailkhalil:fix/dev-shared-agent-bundle
Closed

jabrailkhalil wants to merge 2 commits into
google:mainfrom
jabrailkhalil:fix/dev-shared-agent-bundle

Conversation

@jabrailkhalil

Copy link
Copy Markdown
Contributor

Problem and solution

Closes #969.

Opening the graph in adk web can break the next agent run: the request disposes the shared preloaded AgentFile, deleting the compiled bundle and its node_modules link. A package imported lazily afterward then fails to resolve even though it is installed.

I removed request-scoped disposal from the structure-graph, event-graph and run handlers. The loader keeps ownership and cleans up the bundle during invalidation or disposeAll(). I also documented that ownership on getAgentFile().

Testing

I added an offline HTTP regression test using real esbuild compilation, an AgentLoader, an AdkApiServer and local fixture packages. It covers CommonJS and ESM after build_graph, build_graph_image, an event graph, /run and /run_sse. Each case then imports two previously unused packages in successive runs and verifies that explicit loader disposal removes the compiled directory. No model credentials or external model calls are needed.

  • New regression: 10 passed; the same tests on the base commit fail in all 10 cases with the missing-package error.
  • Restoring each of the three removed disposals independently makes the corresponding CommonJS and ESM cases fail.
  • Existing dev unit suite: 448 passed, 2 skipped.
  • Core and integrations unit suites: 4861 passed, 2 skipped.
  • npm run build, npm run ts:check, npm run ts:check:samples, npm run lint, npm run format:check and npm run docs:check: passed.
  • Secretlint on the changed files: passed.

Test environment: Windows, Node.js 22.21.1; dependencies installed with npm ci --ignore-scripts, matching the repository's validation workflow.

To reproduce the regression test locally after building:

npx vitest run --project integration tests/integration/app_loader/agent_bundle_lifetime_test.ts

Checklist

  • I have read the contribution guide and reviewed the diff.
  • I have added a regression test that fails without the fix.
  • Bundle cleanup is covered as well as continued lazy imports.
  • No dependency or public API changes are required.

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

@Varun-S10 I have a focused fix for #969 ready here, with 10 offline regression cases covering both module formats and all affected request paths. The Google CLA check passed; the validation workflows are currently marked action_required. Could you approve the workflow runs when you have a chance? I have included the local test results in the description and am happy to adjust the fix after review.

@kalenkevich kalenkevich 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.

Thanks for the fix and the detailed regression test.

A few non-blocking notes:

  • Related race (follow-up, out of scope here): in watch mode, invalidateAll() still disposes bundles that an in-flight /run may be using. A lazy import during that run would hit the same missing-package error. It's probably worth a separate issue (ref-counting, or deferring disposal until in-flight runs finish).
  • Test runtime: the new test runs 10 cases, and each one compiles with esbuild. Please confirm it stays within the integration project's time budget in vitest.config once CI runs (the workflows are still waiting on approval).

* Returns a shared file owned by this loader. Callers must not dispose it;
* its compiled bundle is cleaned up on invalidation or {@link disposeAll}.
*/
async getAgentFile(agentName: string): Promise<AgentFile> {

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.

Optional: since AgentFile still implements Symbol.asyncDispose (needed by cli_run.ts), await using loader.getAgentFile(...) still type-checks, and the bug could come back unnoticed. You could return a non-disposable view/interface type from getAgentFile() so the compiler catches it rather than relying on this comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I’d keep narrowing the loader’s public return type in a separate change.

recursive: true,
});
await fs.symlink(
path.resolve('core'),

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.

Nit: path.resolve('core') depends on the process cwd being the repo root, and it only works if core has been built (resolution goes through its exports/dist). Could you resolve it relative to this file (e.g. via import.meta.url) or add a short comment noting the build prerequisite?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 9787563: the core path now resolves relative to this test file, and I added the build prerequisite.

).rejects.toMatchObject({code: 'ENOENT'});
} finally {
await server.stop();
await loader.disposeAll();

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.

Nit: server.stop() / loader.disposeAll() run both in the body (L139-140) and here. That's fine because both are idempotent, but it's a bit confusing to read. Moving the fixture setup into a helper would also make this long single test body easier to follow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The first cleanup is asserted by the test; finally handles failures before that point. I kept both so the bundle-removal assertion remains meaningful.

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

I re-ran all 10 cases with the unchanged integration timeouts: the longest took 3.22s locally against the 60s per-test limit. CI timing still needs confirmation. I also reproduced the watch-mode race in both module formats and tracked it separately in #1002.

@kalenkevich

Copy link
Copy Markdown
Collaborator

Related to #1008

@kalenkevich

Copy link
Copy Markdown
Collaborator

Thanks @jabrailkhalil for diagnosing #969, writing the thorough CJS/ESM integration test, and filing #1002 for the watch-mode invalidation race!

Since #1008 builds on the same fix to also handle reference-counted bundle retirement during watch-mode invalidation (#1002) and runner cache invalidation on reload, we're going to consolidate onto #1008 and carry your tests/integration/app_loader/agent_bundle_lifetime_test.ts test over there with Co-authored-by credit. Closing this in favor of #1008.

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

Thanks for carrying my tests and credit into #1008. Consolidating the lifecycle fixes there makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

adk web: dev UI graph endpoint disposes the shared preloaded agent bundle, so lazy imports fail (e.g. Claude's @anthropic-ai/vertex-sdk)

2 participants