Skip to content

refactor(opencode): lazily load cli commands - #36798

Closed
cppcoffee wants to merge 2 commits into
anomalyco:devfrom
cppcoffee:lazily-load-cli-commands
Closed

refactor(opencode): lazily load cli commands#36798
cppcoffee wants to merge 2 commits into
anomalyco:devfrom
cppcoffee:lazily-load-cli-commands

Conversation

@cppcoffee

@cppcoffeecppcoffee commented Jul 14, 2026

Copy link
Copy Markdown

Issue for this PR

Optimize process startup speed.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

This PR improves the opencode CLI startup path by lazily loading command modules instead of importing every command during process initialization.

Previously, packages/opencode/src/index.ts imported all CLI commands eagerly, even when the user only ran one command, checked the version, or launched the default TUI. This made startup do unnecessary module loading work.

The change replaces those static command imports with command-specific dynamic imports. The CLI now detects the requested command from argv and only registers the command needed for that invocation. It still loads all commands when required for help or shell completion generation, so command discovery behavior is preserved.

This also moves heap initialization behind a dynamic import so that it is only loaded when the command middleware actually starts the agent process.

How did you verify your code works?

I verified the change locally by checking the CLI still selects and registers the expected commands for normal command execution, default TUI startup, version/help paths, and completion/help cases where all commands need to be available.

Screenshots / recordings

Not applicable. This is not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

How did you verify your code works?

I tested the affected CLI startup paths locally, including default startup, command-specific startup, help/completion behavior, and version handling.

Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>
@github-actionsgithub-actionsBot added the needs:compliance This means the issue will auto-close after 2 hours. label Jul 14, 2026
@github-actionsgithub-actionsBot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jul 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@sebishogun

Copy link
Copy Markdown

Seconding this. dev still statically imports every command module before yargs parses argv, so even opencode --version pays the full import cost. I measured 1-3s on Linux, worse with a cold or blocked network, because the import chain pulls in the provider and plugin code that starts network work at module scope. #30453 moved implementations behind handlers and got TTFD down to 995ms, but the command modules themselves are still loaded eagerly. This PR closes the rest of that gap.

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@cppcoffee@sebishogun