Skip to content

feat: add --auto-init-repository option for empty repositories#1231

Open
konard wants to merge 6 commits intomainfrom
issue-1230-f88cfda7aae8
Open

feat: add --auto-init-repository option for empty repositories#1231
konard wants to merge 6 commits intomainfrom
issue-1230-f88cfda7aae8

Conversation

@konard
Copy link
Contributor

@konard konard commented Feb 6, 2026

Summary

Fixes #1230

When the solver encounters an empty repository (no commits) with direct write access, branch creation fails because origin/main has no commits to branch from. The existing empty repository handling only covered the fork path (HTTP 403 "Empty repositories cannot be forked").

This PR adds:

  • New --auto-init-repository CLI option (default: false) that automatically creates a simple README.md in empty repositories
  • Empty repository detection in the direct access path via detectEmptyRepository()
  • Auto-initialization that reuses the existing tryInitializeEmptyRepository() function
  • Issue comment posting when empty repo cannot be resolved (mirrors fork-path behavior)
  • Improved error messages that suggest --auto-init-repository when empty repos are encountered
  • Case study documentation in docs/case-studies/issue-1230/

How it works

  1. After cloning, verifyDefaultBranchAndStatus() detects the repository is empty (no HEAD, no remote branches)
  2. If --auto-init-repository is enabled and user has write access:
    • Creates a README.md via GitHub API (reusing existing tryInitializeEmptyRepository())
    • Waits for GitHub to process, then re-fetches
    • Checks out the new default branch and continues normally
    • No comment is posted on the issue (no user action needed)
  3. If the flag is NOT enabled: shows clear error message suggesting the flag + posts a comment on the issue informing the user
  4. If initialization fails: shows actionable guidance + posts a comment on the issue with fix suggestions

Issue comment behavior

The comment-to-issue behavior follows the same pattern as the existing fork-path code in solve.repository.lib.mjs:

  • Comment is posted via gh issue comment when empty repo is detected and cannot be resolved
  • Comment suggests using --auto-init-repository flag or manually initializing the repository
  • Comment is NOT posted when auto-init succeeds (no user action required)
  • Comment errors are non-critical and silently ignored

Example usage

# Auto-initialize empty repository before solving
solve https://github.com/owner/repo/issues/1 --auto-init-repository

Files changed

File Change
src/solve.config.lib.mjs Added --auto-init-repository option definition
src/option-suggestions.lib.mjs Added to KNOWN_OPTION_NAMES for typo detection
src/solve.repository.lib.mjs Exported tryInitializeEmptyRepository for reuse
src/solve.repo-setup.lib.mjs Added empty repo detection, auto-init, and issue comment in verifyDefaultBranchAndStatus()
src/solve.mjs Pass argv, owner, repo, issueUrl to verifyDefaultBranchAndStatus()
src/solve.branch-errors.lib.mjs Improved error message for empty repo branch creation failures

Test plan

  • New test suite: tests/test-auto-init-repository.mjs (17 tests, all passing)
  • Experiment script: experiments/test-auto-init-repository.mjs
  • Existing tests still pass (test-branch-name-validation.mjs, test-malformed-flags.mjs)
  • Prettier formatting passes
  • Manual testing with an actual empty repository

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #1230
@konard konard self-assigned this Feb 6, 2026
When the solver encounters an empty repository (no commits) with direct
write access, branch creation fails because origin/main has no commits.
The existing empty repo handling only covered the fork path.

This adds:
- New --auto-init-repository CLI option (default: false)
- Empty repository detection in verifyDefaultBranchAndStatus()
- Auto-initialization using existing tryInitializeEmptyRepository()
- Improved error messages suggesting --auto-init-repository
- Case study documentation in docs/case-studies/issue-1230/
- Tests and experiment scripts

Fixes #1230

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@konard konard changed the title [WIP] Comment with suggestions of possible options didn't work on empty repository feat: add --auto-init-repository option for empty repositories Feb 6, 2026
@konard konard marked this pull request as ready for review February 6, 2026 22:04
@konard
Copy link
Contributor Author

konard commented Feb 6, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $7.230147 USD
  • Calculated by Anthropic: $4.835029 USD
  • Difference: $-2.395118 (-33.13%)
    📎 Log file uploaded as Gist (1600KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard
Copy link
Contributor Author

konard commented Feb 6, 2026

What about comment to issue? We did have it somewhere in the code?

Ensure all changes are correct, consistent and fully meet all discussed requirements (check issue description and all comments in issue and in pull request).

@konard konard marked this pull request as draft February 7, 2026 00:31
@konard
Copy link
Contributor Author

konard commented Feb 7, 2026

🤖 AI Work Session Started

Starting automated work session at 2026-02-07T00:31:13.146Z

The PR has been converted to draft mode while work is in progress.

This comment marks the beginning of an AI work session. Please wait for the session to finish, and provide your feedback.

When an empty repository is detected in the direct access path (not fork
path), post a comment on the issue informing the user about the problem
and suggesting solutions, matching the existing behavior in the fork path.

- Add tryCommentOnIssueAboutEmptyRepo() helper in solve.repo-setup.lib.mjs
- Post comment when --auto-init-repository is not enabled (suggest the flag)
- Post comment when --auto-init-repository fails (suggest manual init)
- Skip comment when --auto-init-repository succeeds (no user action needed)
- Pass issueUrl to verifyDefaultBranchAndStatus() for comment posting
- Add 5 new tests (13-17) covering the comment behavior
- Update case study documentation

Fixes #1230

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@konard konard marked this pull request as ready for review February 7, 2026 00:37
@konard
Copy link
Contributor Author

konard commented Feb 7, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $4.587508 USD
  • Calculated by Anthropic: $2.943958 USD
  • Difference: $-1.643549 (-35.83%)
    📎 Log file uploaded as Gist (925KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard
Copy link
Contributor Author

konard commented Mar 14, 2026

Resolve conflicts.

We need to ensure all changes are correct, consistent, validated, tested, logged and fully meet all discussed requirements (check issue description and all comments in issue and in pull request). Ensure all CI/CD checks pass.

@konard konard marked this pull request as draft March 14, 2026 14:00
@konard
Copy link
Contributor Author

konard commented Mar 14, 2026

🤖 AI Work Session Started

Starting automated work session at 2026-03-14T14:00:53.233Z

The PR has been converted to draft mode while work is in progress.

This comment marks the beginning of an AI work session. Please wait for the session to finish, and provide your feedback.

konard and others added 2 commits March 14, 2026 14:02
Merge origin/main into issue-1230-f88cfda7aae8, resolving conflicts in
src/option-suggestions.lib.mjs and src/solve.config.lib.mjs by keeping
both the auto-init-repository option and the new options from main
(attach-solution-summary, auto-accept-invite, finalize, etc.).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@konard konard marked this pull request as ready for review March 14, 2026 14:11
@konard
Copy link
Contributor Author

konard commented Mar 14, 2026

🤖 Solution Draft Log

Work completed:

  • Resolved merge conflicts with main branch (conflicts in src/option-suggestions.lib.mjs and src/solve.config.lib.mjs)
  • Fixed prettier formatting issue in docs/case-studies/issue-1230/README.md
  • Verified all 17 auto-init-repository tests pass
  • Verified related tests pass (branch-name-validation: 11/11, malformed-flags: 23/23, option-parity: 10/10)
  • All CI checks passing (lint, prettier, ESLint, test-compilation, test-execution, test-suites, helm-pr-check, validate-docs, check-file-line-limits, memory-check)

Summary of changes in this PR:

  • --auto-init-repository CLI option for empty repos (creates README.md via GitHub API)
  • Empty repo detection in direct access path via detectEmptyRepository()
  • Issue comment posting when empty repo cannot be resolved (mirrors fork-path behavior)
  • Improved error messages in handleBranchCreationError() for empty repo patterns
  • Case study documentation in docs/case-studies/issue-1230/

All requirements from the issue description and PR comments have been addressed. PR is ready for review.

@konard
Copy link
Contributor Author

konard commented Mar 14, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $2.887106
  • Calculated by Anthropic: $1.941051 USD
  • Difference: $-0.946055 (-32.77%)
    📎 Log file uploaded as Gist (1341KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard
Copy link
Contributor Author

konard commented Mar 14, 2026

✅ Ready to merge

This pull request is now ready to be merged:

  • All CI checks have passed
  • No merge conflicts
  • No pending changes

Monitored by hive-mind with --auto-restart-until-mergeable flag

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.

Comment with suggestions of possible options didn't work on empty repository

1 participant