Bootstrap loads every class eagerly; rely on the classmap from #526 instead
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 65/100
- Issue type
- Refactor
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- backend, build-system, performance
Research direction
Examine agents-api.php to see the current eager require_once statements. Identify the files with side effects (register-.php, functions-.php, guidelines.php) and the add_action calls. Understand the Composer classmap from PR #526. Test changes in a WordPress multisite setup, using opcache_get_status() to measure memory impact. Ensure version skew handling via AGENTS_API_LOADED is preserved.
Written by the indexing model from the issue text.
Description
Problem
agents-api.php require_onces every class and interface file under src/ (about 240 files on current main, 0.11.2) on every request, before any agent, chat or workflow is used. #526 added a lazy Composer classmap covering the full public class/interface surface, but the bootstrap still loads all of those classes eagerly, so the classmap never gets a chance to be lazy at runtime.
On a WordPress multisite where the package is bundled in a network-activated plugin (via Composer, as wordpress/agents-api), the package accounts for 249 cached scripts and about 5 MB of OPcache memory, measured with opcache_get_status(). On that server OPcache is full (128 MB), so this is memory taken from code that runs on every request. The chat is used on one page; every other request on every site pays for the whole runtime.
Proposal
Now that every class and interface is in the classmap, the bootstrap only needs to require_once files that have side effects or define functions:
src/*/register-*.phpsrc/Runtime/functions-*.php,src/Abilities/functions-ability-dispatch.php,src/Guidelines/guidelines.php- the
add_action()calls at the end
and leave class-*.php / interface-*.php to the autoloader. Hook callbacks already reference classes by name as strings, so registration itself should not force them to load. Only a class actually touched during a request would be compiled and cached.
For the non-Composer (plain plugin) install, a small spl_autoload_register() over a generated class-to-file map (or the existing vendor/composer/autoload_classmap.php) would give the same behaviour.
Things to keep
- Version skew. The
AGENTS_API_LOADEDbranch currently loads missing symbols from the newer copy eagerly. With lazy loading, whichever copy's autoloader is registered first would win for classes not yet loaded, so the skew guarantee from #279 needs an equivalent (for example, the bootstrap that definesAGENTS_API_LOADEDregistering its own class map ahead of Composer's, or the skew branch registering a prepend autoloader for the newer copy). - Composer autoload outside WordPress stays inert, and PHPStan behaviour from #524/#526 is unchanged.
Happy to test a branch against our install and report OPcache numbers before and after.
- Dominant language
- PHP
- Stars
- 33
- Forks
- 8
- Avg merge
- 24m
- Merged PRs (30d)
- 18
Getting set up
We have not checked this project's setup files yet. Start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Automattic/agents-api
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Automattic/agents-api#564 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 68/100
Automattic/agents-api#576 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
Automattic/agents-api#572 · 2 comments ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 45/100
Automattic/agents-api#552 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 45/100
Automattic/agents-api#551 ·
Maintainers usually reply within 1 day
All issues in Automattic/agents-api
Similar issues
-
domain/crm-after-sales Platform(Default) priority/high
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
sync-en
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 2 days
-
sync-en
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
Перевод устарел
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
component/code document/settings documents duplicate integration/wc/pages/cart integration/woocommerce mod* mod/b* mod/c* mod/d* mod/e* mod/i* product/pro status/needs-feedback
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
elementor/elementor#37475 · 1 comment ·
Maintainers usually reply within 1 day