Skip to content

Install Composer dependencies with a SessionStart hook in cloud sessions - #87

Draft
lisachenko wants to merge 1 commit into
masterfrom
feature/zealous-goodall-0525px
Draft

lisachenko wants to merge 1 commit into
masterfrom
feature/zealous-goodall-0525px

Conversation

@lisachenko

Copy link
Copy Markdown
Member

Problem

Every Claude Code cloud session starts with a slow and failing composer install:

  • Anonymous requests to api.github.com hit the rate limit (HTTP 403), so every dist zip download fails.
  • Composer then falls back to full git clones, one package after the other, which takes many minutes.
  • phpstan/phpstan has no git source, so it can't be installed at all, and the install fails with "Could not authenticate against github.com".

Fix

.claude/hooks/session-start.sh, registered as a SessionStart hook in .claude/settings.json:

  1. Resolves composer.lock with composer update --no-install. The lock file is not committed, and the Packagist metadata is not rate limited.
  2. For each locked package with a GitHub dist, runs git fetch --depth=1 <repo> <ref> and git archive, 8 packages in parallel, and writes the zip to the file in Composer's cache that Composer would read (<package>/sha1(<dist url>).zip). Git through the session proxy is not limited. A package that can't be fetched this way is left to Composer.
  3. Runs composer install, which now takes every package from the cache.

Other behaviour:

  • It runs only when CLAUDE_CODE_REMOTE=true, so local setups and CI are not affected.
  • It skips the install while the script and composer.json are unchanged. The hash file is in the ignored vendor/ directory.
  • It sets COMPOSER_ALLOW_SUPERUSER=1 for the session, because the sandbox runs as root and Composer otherwise refuses to load phpstan/extension-installer.
  • The hook is synchronous, so the dependencies are ready before the session's first command.

AGENTS.md gets a short note about the hook.

Testing

  • From an empty Composer cache, with no vendor/ and no composer.lock, the hook finishes in about 20 s with no 403 errors and no git-clone fallbacks. Without it, the install took more than 10 minutes and then failed on phpstan.
  • A second run skips the install in under 10 ms. Changing composer.json makes the hook run again. Without CLAUDE_CODE_REMOTE it does nothing.
  • After the hook: composer test (PHP-CS-Fixer, PHPStan level 10, 168 tests) and composer demo pass.

Generated by Claude Code

In Claude Code cloud sessions, anonymous requests to api.github.com hit the
rate limit (HTTP 403). Composer then falls back to slow git clones for every
package, and phpstan/phpstan, which has no git source, can't be installed.

The hook resolves composer.lock from the Packagist metadata, builds each
GitHub dist zip with a shallow git fetch and git archive (git through the
session proxy isn't limited), puts the zips into Composer's cache, and runs
composer install. From an empty cache it finishes in about 20 seconds. It
runs only in cloud sessions and skips the install while composer.json and the
script are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FxhC1Cr7rZzUgw16qS4vNV
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.

2 participants