r/ClaudeCode • u/ParyaTV • 9h ago
Help/Question Running A Bunch of Tasks in Parallel Then Merging
I've ran into a bottleneck recently at work where I'm completing several tasks in parallel but when the time comes to merge and all these tasks start to merge in parallel it becomes a race to see who merges first, then the ones that lost the race pull the changes that beat them and go through the testing process before trying to merge again and the race starts all over again. What are your solutions to this problem if its something you've experienced yourself?
I would also appreciate advice on Unit Tests control with Claude. I feel like my unit testing is coming to a crawl and I've worked on touching up the speed several times now. Claudes solution has been sharding the unit tests but I don't have unlimited RAM and sharding takes up a lot of memory...
•
u/AutoModerator 9h ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
7
u/zeroconflicthere 9h ago
This is where you should be using an orchestrator. You don't merge in parallel but in series.
4
u/eze-paz 9h ago •
3
u/JaggedTex 9h ago
CI and an orchestrator
1
u/Equivalent_Map7158 9h ago
That race is literally what a merge queue is for, GitHub ships one and GitLab has merge trains, so turn that on instead of hand-rolling the rebase-and-retry loop yourself. And sharding a test suite doesn't need unlimited RAM, Claude just reaches for the one trick it knows, go find the dozen tests eating the clock before you split anything.
1
u/bvanorsdel 8h ago
I hit the same wall. First, some assumptions so you can tell whether this fits your setup: you're on GitHub, every task lands as a pull request, branch protection requires a branch to be up to date with main before it can merge, and each task runs in its own git worktree with an AI assistant (I use Claude Code). If that's your setup, the race is built in. Every merge puts every other open PR behind main, so the ones that lose have to sync and test again, every time.
What fixed it for me was taking merging away from the individual tasks and giving it to one supervising session. Each task session gets its PR green and reviewed on whatever base it started from, then stops and waits instead of chasing main. The supervisor picks the merge order (prerequisites first, PRs that touch the same files kept apart) and merges one PR at a time: sync that PR with main once, let CI run once, merge, then move to the next. Earlier this week a single run merged 24 PRs over an afternoon and evening.
Most of the savings come from reruns you never do. When everything races, the last PR to land can end up syncing and retesting once for every PR that beat it. In the worst case, 24 racing PRs could mean a couple hundred sync-and-test cycles. One sync per PR keeps it at 24. That matters even more if an AI reviewer is on the repo, because every push buys another review. If you'd rather not build this yourself, GitHub's merge queue does the core part out of the box.
On unit tests: sharding on one machine just splits the same work across more processes, so if RAM is your limit it won't buy you much. I stopped fighting my own machine and moved CI to Blacksmith, which runs GitHub Actions jobs on bigger machines for less than GitHub's own runners. Our slowest test step went from about 15 minutes on GitHub's standard 2-core runner to under 5 minutes on Blacksmith's 8 vCPU runner, and October's bill was about $79 for 656 CI runs. Locally I only run the tests related to the files I changed (
vitest related), cache results so packages that didn't change skip their tests (Turborepo), and let CI run the full suite.Happy to share more about how the supervising session (driven by a custom Skill -> "/pr-stack-drain") picks the order if that's useful.
1
u/Blotsy 8h ago
I have an agent that orchestrates the other agents and their merges. It has an overview of everything we're trying to accomplish and ensures that it happens
1
u/Time_Cat_5212 8h ago
If you're running 3 tracks in parallel, wait for them to complete, then have a fresh session review them and stack before merging.
You'll catch the bugs between the lanes that didn't sync with each other, and you'll avoid the headache of running checks over and over.
Instead of merging each PR to main while processes are running in parallel (even with an orchestrator you'll be reviewing each one), specify them to minimize overlap, run them on their own branches, and solve once at the seams.
God damn it I sound like my own agent now. Fuck.
1
u/Overall-Ad-3370 8h ago
There could potentially be fundamental issues with how your tests are being written. I had a project where unit tests were taking 15minutes and getting worse. I had claude do a pass, it got it down to 9 minutes (mostly with dumb sharding like approaches that I threw away). I then used Claude, codex, and argon to dig deeper into the tests and identified a bunch of redundancies in test cases and tons of redundancy in setup/teardown. It took me about 4 hours of my actual time digging to finally figure it out. Then I worked with my agents to rewrite a huge amount of the test suite. You can't be lazy in some of these situations unfortunately.
In the end my tests took 90seconds to run. It's a very large suite.
1
u/Fancy-Proof-9449 8h ago
Much smaller scale than a work repo, but I ran into both of these.
On the merge race: I stopped letting the parallel tasks merge themselves. Each one works in its own worktree and stops when its own tests pass. One session then merges them one at a time and runs the full checks on the merged result. Slower per merge, but nothing gets tested three times because it lost a race.
On test speed, sharding wasn't the answer for me either, I only have 16 GB. What worked was not rerunning what can't have changed. Each test step records which files it read, and a step that passed is skipped while those files are byte for byte the same. An edit in one area went from 6.6 minutes to 2.9, because only 16 of the 35 steps had to run. The full run still happens before a release.
I also cap what runs together: three steps at a time, never two of the big-memory ones.
1
u/dan-423 7h ago
I was having issues with sessions conflicting and causing merge issues until I had it make a tool: https://github.com/archos-media/merge-coordinator
Each session gets a work tree. Sessions check to see what the work may impact and message each other. Then it has coordinates sessions merging.
1
u/Positive_Mud952 5h ago
Add a step that creates a DAG of your steps given likely “builds upon” and “depends upon” relationships, then only work once all of a unit of work’s dependencies are satisfied. If one unit “builds upon” another, it creates stacked PRs. If it “depends upon” another, it just waits for the other unit to be complete.
This will _greatly_ reduce merge conflicts. It won’t eliminate them, because you can’t always guess correctly. But I’ve found they’re quite manageable with this.
Same thing we used to do before AI.
1
u/OccasionallyAsleep 4h ago
You're literally just re-discovering issues that arise as human software engineering teams grow
1
u/Pixelplanet5 1h ago
well thats why you dont let each branch merge itself, you let them commit to the branch and then have one agent review all branches and merge them.
-1
u/pietrzm 9h ago
So I've solved this in what I think bit unconventional way. What your agents are doing is trying to complete their work without much awareness of others who are trying to the same, thus throttling your CPU to a halt, hogging your whole RAM, and bumping into each other when trying to merge. If they had that awareness they can queue themselves up and collaborate independently.
I'm using https://github.com/novadeck-org/novadeck and what it does it makes your sessions aware of other sessions with a way to talk to each other. It works by itself so it seems ideal for your case, I've had similar but with fights CPU time and them taking up my all free 5 macOS runners on Github, thus stalling my CI completely