Skip to content

fix(pdf-tool): cache model config resolution per agentDir to avoid 10-12s event-loop stalls (#76644) - #76728

Merged
steipete merged 3 commits into
openclaw:mainfrom
hclsys:fix/76644-pdf-tool-cache-model-config
May 3, 2026
Merged

steipete merged 3 commits into
openclaw:mainfrom
hclsys:fix/76644-pdf-tool-cache-model-config

Conversation

@hclsys

@hclsys hclsys commented May 3, 2026

Copy link
Copy Markdown

Fixes #76644.

Problem

createPdfTool is called on every agent turn inside createOpenClawTools. It calls resolvePdfModelConfigForTool, which probes auth profiles from disk (via hasAuthForProvider → ensureAuthProfileStore) on each invocation — even when no PDF is in the request. On Linux/arm64 with multiple providers configured, this causes 10-12 s synchronous event-loop stalls every message, triggering liveness warning: reasons=event_loop_delay.

Fix

Add a module-level Map cache in pdf-tool.ts keyed on (agentDir, workspaceDir). The first createPdfTool call per pair resolves and caches the model config; subsequent calls return the cached value directly — zero disk I/O.

Auth profiles, agentDir, and workspaceDir are stable within a gateway process lifetime, so the cached result is valid for the process duration. Config hot-reload does not affect auth profile paths.

clearPdfToolModelConfigCache() is exported for test teardown.

Test

Added regression test in pdf-tool.test.ts:

caches model config resolution across repeated createPdfTool calls with same agentDir (#76644)

Calls createPdfTool three times with the same agentDir and asserts resolvePdfModelConfigForTool is invoked exactly once.

All 30 pdf-tool tests pass (pdf-tool.test.ts, pdf-tool.model-config.test.ts, openclaw-tools.pdf-registration.test.ts).

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels May 3, 2026
@clawsweeper

clawsweeper Bot commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

Summary
The PR defers automatic PDF model/auth resolution from tool registration to PDF execution, adds regression tests, and adds a changelog entry for #76644.

Reproducibility: yes. at source level: current main builds the PDF tool during createOpenClawTools and createPdfTool eagerly calls resolvePdfModelConfigForTool, which can reach ensureAuthProfileStore. The live WeCom/Linux arm64 timing was not rerun in this read-only review.

Next step before merge
Narrow repair candidate: the remaining blocker is a deterministic test-fixture setup in the PR branch, not a product or security decision.

Security
Cleared: The PR diff is limited to TypeScript tool-registration logic, tests, and changelog text, with no dependency, workflow, package, permission, or secret-handling changes.

Review findings

  • [P2] Seed a PDF-capable provider in the hot-path test — src/agents/openclaw-tools.media-factory-plan.test.ts:288
Review details

Best possible solution:

Land the deferred PDF resolution approach after fixing the media-factory regression test so it seeds a real PDF-capable availability signal.

Do we have a high-confidence way to reproduce the issue?

Yes at source level: current main builds the PDF tool during createOpenClawTools and createPdfTool eagerly calls resolvePdfModelConfigForTool, which can reach ensureAuthProfileStore. The live WeCom/Linux arm64 timing was not rerun in this read-only review.

Is this the best way to solve the issue?

No, not mergeable as written: deferring automatic PDF model resolution is the narrow maintainable fix, but the new hot-path test must seed a PDF-capable provider or explicit config before it can assert the pdf tool is present.

Full review comments:

  • [P2] Seed a PDF-capable provider in the hot-path test — src/agents/openclaw-tools.media-factory-plan.test.ts:288
    This test installs an empty plugin snapshot, but factory planning only enables optionalMediaTools.pdf when explicit PDF/image config, a mediaUnderstandingProviders plugin, or configured vision-model auth is available. With installSnapshot(config, []), createOpenClawTools never calls createPdfTool, so the toContain("pdf") assertion fails before proving deferred resolution.
    Confidence: 0.91

Overall correctness: patch is incorrect
Overall confidence: 0.9

Acceptance criteria:

  • pnpm test src/agents/openclaw-tools.media-factory-plan.test.ts src/agents/tools/pdf-tool.test.ts
  • pnpm exec oxfmt --check --threads=1 src/agents/openclaw-tools.media-factory-plan.test.ts src/agents/tools/pdf-tool.test.ts src/agents/tools/pdf-tool.ts src/agents/openclaw-tools.ts CHANGELOG.md
  • git diff --check
  • pnpm check:changed

What I checked:

Likely related people:

  • steipete: Current-main blame for the PDF registration, resolver, and image-tool deferred-resolution pattern points to Peter Steinberger, and the PR branch includes his defer-resolution and changelog commits. (role: recent maintainer and PR follow-up owner; confidence: high; commits: 54c0f982d591, c3948800f4fb, 6c78cd1d81e8; files: src/agents/openclaw-tools.ts, src/agents/tools/pdf-tool.ts, src/agents/tools/pdf-tool.model-config.ts)
  • tyler6204: Local history shows the PDF analysis tool was introduced by the merged native-provider PDF feature, which created the central code path this PR changes. (role: original PDF tool feature author; confidence: medium; commits: d0ac1b019517; files: src/agents/tools/pdf-tool.ts, src/agents/openclaw-tools.ts)

Remaining risk / open question:

  • The exact 10-12s Linux/arm64 WeCom stall magnitude was not live-reproduced; the hot path and PR test failure are source-reproducible.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 59c523c6b530.

@steipete
steipete force-pushed the fix/76644-pdf-tool-cache-model-config branch from 5caf98b to 1ec5338 Compare May 3, 2026 14:57
@hclsys

This comment was marked as low quality.

@steipete
steipete force-pushed the fix/76644-pdf-tool-cache-model-config branch from 1ec5338 to cb3d93b Compare May 3, 2026 15:13
HCL and others added 2 commits May 3, 2026 16:21
…-12s event-loop stalls (openclaw#76644)

`resolvePdfModelConfigForTool` probes auth profiles from disk on every
call. Since `createPdfTool` is invoked on the agent-turn hot path
(`createOpenClawTools`), every inbound message pays the full auth-
discovery cost even when no PDF is present — causing 10-12 s event-loop
stalls and `liveness warning: reasons=event_loop_delay` on Linux/arm64.

Add a module-level `Map` cache keyed on `(agentDir, workspaceDir)`.
The first call per pair resolves and stores the config; subsequent calls
return the cached value. Auth profiles and workspaceDir are stable within
a gateway process lifetime, so the cached result is valid for the process
duration.

Expose `clearPdfToolModelConfigCache()` for test teardown and add a
regression test asserting that three consecutive `createPdfTool` calls
with the same `agentDir` invoke `resolvePdfModelConfigForTool` exactly once.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@steipete
steipete force-pushed the fix/76644-pdf-tool-cache-model-config branch from cb3d93b to 6c78cd1 Compare May 3, 2026 15:24
@steipete
steipete merged commit 02b1075 into openclaw:main May 3, 2026
108 checks passed
@steipete

steipete commented May 3, 2026

Copy link
Copy Markdown
Contributor

Landed via squash merge onto main.

  • Gate: focused local PDF/gateway tests; Testbox OPENCLAW_TESTBOX=1 pnpm check:changed; exact-head GitHub CI green
  • Source head: 00ad9f8
  • Merge commit: 02b1075

Thanks @hclsys!

lxe pushed a commit to lxe/openclaw that referenced this pull request May 6, 2026
Defers automatic PDF model/auth resolution until the PDF tool is executed, avoiding agent-turn tool prep stalls while preserving explicit PDF model registration.

Fixes openclaw#76644.
Thanks @hclsys.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
Defers automatic PDF model/auth resolution until the PDF tool is executed, avoiding agent-turn tool prep stalls while preserving explicit PDF model registration.

Fixes openclaw#76644.
Thanks @hclsys.
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
Defers automatic PDF model/auth resolution until the PDF tool is executed, avoiding agent-turn tool prep stalls while preserving explicit PDF model registration.

Fixes openclaw#76644.
Thanks @hclsys.
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
Defers automatic PDF model/auth resolution until the PDF tool is executed, avoiding agent-turn tool prep stalls while preserving explicit PDF model registration.

Fixes openclaw#76644.
Thanks @hclsys.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: event_loop_delay 高延迟问题 — pdf-tool 同步初始化阻塞 Event Loop

2 participants