Skip to content

factory: the factory on this Mac opens at once, whatever old jobs and GitHub do - #2101

Merged
santoshkumarradha merged 6 commits into
app/v3from
fix/factory-start-never-waits
Nov 1, 2026
Merged

santoshkumarradha merged 6 commits into
app/v3from
fix/factory-start-never-waits

Conversation

@santoshkumarradha

@santoshkumarradha santoshkumarradha commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Summary

Opening the app could leave the factory on this Mac answering "isn't answering yet" at every launch. The factory now opens right away from what is on disk. Housekeeping and GitHub run in the background behind it.

Root cause: when the app resumed, it cleaned up old PR jobs before the local factory started serving. In the affected data, one old PR job's worktree was in a folder named by an earlier release (CodeAF-1796). The current release looks for codeaf-1796. Because the worktree still used the branch, git branch -D was refused ("used by worktree"), and the clean-up failed the same way at every launch. That error was dropped. The saved row then kept the previous launch's port, so every read went to a dead address. No gh call was involved in this failure.

While reproducing it, I found a second cause of the same symptom: with gh signed in but hanging, or with a network that swallows requests, list, queue and job reads each waited on GitHub until they hit their time budget.

 resume → solo()
-  forgetFactoryPRJobs()   # one bad job fails the start, every launch
   serve.New(...)          # the server comes up first
+  tidy() in background    # skips jobs a live worker holds; a job it cannot clean is kept for next launch
 Workdirs.Discard
+  remove the work-root worktree that holds the job's branch, whatever its folder name
 /v1/me, /v1/queue, /v1/jobs/{id}
-  gh auth token (3 s cap; in practice up to 60 s, because a gh child held the pipe)
-  GitHub /user, pipeline file, CODEOWNERS: synchronous, pipeline/owners under a lock
+  the person GitHub last named (github-link.json on the floor, a login only, never a token)
+  gh's last token (cached 1 min, cmd.WaitDelay bounds the call)
+  view reads (ShowOnly) wait ≤150 ms, then use the last file or the checkout's
+  every one is asked again in the background, one ask at a time, with backoff

Actions such as start, edit and merge still wait for GitHub's current pipeline file. Only reads that just show data are bounded.

Evidence

The app engine was driven over its stdio wire with factory.list, then factory.queue, then factory.job.

A copy of the affected floor (349 jobs, 51 old PR jobs, saved row on a dead port, gh configured and hanging, on a case-insensitive filesystem):

  • Before: factory.list took 3.0 s and returned unreachable at the dead port. factory.queue returned "the local factory is not running for this app". The row was unchanged, and 51 PR jobs were left.
  • After: factory.list took 0.57 s and returned ok at a fresh port. factory.queue took 0.19 s and returned 298 jobs. The row was rewritten. The clean-up finished in the background and left 0 PR jobs.

A real repository, after setup through gh. Each case is a relaunch.

Case Before (list / queue) After (list / queue / job)
gh hanging 25.0 s unreachable / 20 s "not answering" 0.26 s ok / 0.03 s, 6 jobs / ok
gh signed in, network swallows every request 25.1 s unreachable / 20 s "not answering" 0.22 s ok / 0.04 s / ok
Normal network — 0.07–0.20 s / 0.17–0.21 s; the factory's name fills in once GitHub answers

Tests. Each one was red before the fix and green after it:

  • TestTheFactoryOpensAtOnceWhatEverHousekeepingAndGitHubDo: a stalled, failing clean-up, a hanging gh and a stale port. List and queue answer in under 1 s, the row moves to a new address, and the jobs are there. Before: 16 s, then "not running".
  • TestTheFactoryOpensAtOnceWhenGitHubDoesNotAnswer: GitHub never answers. The factory opens in under 1 s, still names the last person, and GitHub is asked again in the background. Before: 45 s, then "not answering".
  • TestDiscardFindsTheJobsWorktreeUnderAnEarlierName: reproduces the exact used by worktree error, which is now fixed.
  • TestAPRJobThatCannotBeForgottenIsKeptWholeAndTheRestGo: a held job keeps its record and history, and the other jobs are removed.
  • TestAPipelineReadNeverWaitsOnASilentForge, TestCodeOwnersNeverHoldAReadOnASilentForge, TestAHangingGHNeverHoldsTheReadsThatAskForTheToken: before, these hung or took 4 s; now each finishes in under 1 s.

I did not run make pr-ready. These checks were run on this branch:

  • go test -race ./internal/factory/github passes.
  • The focused ./cmd/codeaf factory tests pass.
  • TestPublishCommitsPushesAndOpensThePullRequestWithTheTrailer fails on the base branch too.
  • One action-path regression found by the suite is fixed: an action's pipeline read no longer waits behind a view's ask. After that fix, the focused ./cmd/codeaf factory run passes in full.
  • build-check, vet and fmt-check pass. test-laws fails on the base branch in the same places: comment, lock-defer, session complexity and taxonomy. This branch adds no new law failures.

Merge Danger

Door: two-way

The only new on-disk file is github-link.json on the floor. It holds a login, never a token. The clean-up still deletes the same things, but now only for jobs it can actually clean.

Blast Radius: factory

  • A job card or queue can briefly show the checkout's pipeline or no CODEOWNERS before GitHub answers. It then corrects itself on a later read.
  • An answer of nobody keeps the last person until GitHub is connected again. An action that needs sign-in still fails at the point where it is pressed.
  • A new item created while GitHub is slow takes its steps from the checkout.

🤖 Generated with Claude Code

… and GitHub do

Root cause: on resume the app ran the clean-up of old PR jobs before the
local factory served. On the owner's floor one old PR job (#1796) had its
worktree in a folder named by an earlier release (CodeAF-1796, not
codeaf-1796), so `git branch -D` was refused as "used by worktree" and the
clean-up failed the same way at every launch. The resume error was dropped,
the row kept the last launch's dead port, and every read went there:
"isn't answering". No gh call was involved in that failure.

- The server comes up first; the clean-up runs behind it under the host
  (close waits for it), skips a job a live worker holds, keeps a job it
  cannot clean for the next launch, and never fails the start.
- Discard finds the job's worktree by the branch it holds inside the work
  root, so a folder named by an earlier release is removed too.
- Who the person is comes from what GitHub last said (github-link.json on
  the floor, a login, never a token), asked again behind the reads with
  backoff; connecting GitHub again forgets it.
- gh's token answer is cached for a minute and asked behind the reads; the
  gh call is bounded by WaitDelay, since a child holding gh's output open
  made the 3 s timeout a minute.
- A pipeline read never waits on the forge longer than 300 ms: the last
  file, else the checkout's, while the forge is asked behind; the loader's
  lock is no longer held across a network call.
…ns still do

Review follow-ups:
- A PR job the clean-up cannot forget keeps its lease and its history; only
  forgotten jobs lose them (a kept job lost its history before).
- Pipeline reads wait on the forge again for actions (start, edit, merge);
  only reads marked ShowOnly (the queue's and a card's recipes) wait at most
  FirstWait, and a cold one answers the checkout's file with no problem.
- CODEOWNERS reads (queue, job card) never hold their lock over the forge
  and wait at most FirstWait for a repository not read yet.
- One single-flight helper (behind) and one bound (FirstWait, 150 ms) for
  the pipeline and owners reads; gh and the person's link use the same bound.
- gh that hangs keeps its last token; the person GitHub last named is kept
  in memory, an answer of nobody keeps them, and an ask out when the person
  connects GitHub again is dropped.
- The standalone `codeaf factory serve` forgets old PR jobs behind the
  service too.
A start or edit joined a view's ask that was out for a forge that did not
answer, and waited its whole 30 s; an action reads under its own context.
@santoshkumarradha
santoshkumarradha marked this pull request as ready for review November 1, 2026 15:13
@santoshkumarradha
santoshkumarradha merged commit 0390d9c into app/v3 Nov 1, 2026
1 check passed
@santoshkumarradha
santoshkumarradha deleted the fix/factory-start-never-waits branch November 1, 2026 15:13
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.

1 participant