Skip to content

refactor: decompose 3 shipping high-complexity functions under the gocyclo cap#418

Open
ion-alpha-dev wants to merge 3 commits into
mainfrom
worktree-quality-cleanup-b
Open

refactor: decompose 3 shipping high-complexity functions under the gocyclo cap#418
ion-alpha-dev wants to merge 3 commits into
mainfrom
worktree-quality-cleanup-b

Conversation

@ion-alpha-dev

@ion-alpha-dev ion-alpha-dev commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

What

Brings the three production functions that ship above the repo's gocyclo complexity cap back under it, without changing behavior. Part of the quality-cleanup epic (task B), one commit per function.

Function File Was (cyclo/cog) Refactor
Schedule internal/inference/orchestrate/schedule.go 39 / 65 scheduler struct with keepForcedcandidatesadmitactions phases
runServeContext cmd/flynn/serve.go 35 / 76 parseServeConfig + startControlPlaneAPI + serveChannels
run cmd/flynn/main.go 35 / 75 routeCommand over an invocation struct + shared exit tail

Why

These are the shipping (non-test) functions over the complexity cap — the real maintenance risk. The extractions flatten nesting/branching (the cognitive-score driver); logic is lifted essentially byte-for-byte onto the new helpers, control flow is unchanged.

  • main.go: the if-chain becomes a switch; the dataDirCommands fallback keys are disjoint from the switch cases, so moving serve/watch/review/help ahead of the map lookup changes nothing. Exit-code contract (0/1/2 + exitChangesRequested) preserved.
  • serve.go: secured-by-default API auth and the ctx-driven drain preserved.
  • schedule.go: same deterministic fixed-point plan.

How to verify

gofmt -l cmd/flynn/main.go cmd/flynn/serve.go internal/inference/orchestrate/schedule.go   # empty
go build ./... && go vet ./cmd/flynn/... ./internal/inference/orchestrate/...
go test -race -shuffle=on -count=1 ./cmd/flynn/ ./internal/inference/orchestrate/
golangci-lint run ./cmd/flynn/... ./internal/inference/orchestrate/...                       # 0 issues

The golangci-lint run is what enforces the gocyclo cap — 0 issues means the complexity drop is proven, not asserted. Behavior is covered by the existing cmd/flynn and orchestrate suites (no test changes needed).

Notes for reviewers

spawnConfined (sandbox/appcontainer_windows.go, 42/98) is the 4th high-complexity function from task B. It's app-container code and is being handled as a separate PR/track — deliberately out of scope here.

…d methods

Schedule was 39/65 (cyclo/cog), over the gocyclo cap. Lift the indexed inputs
and the mutable keep/budget accumulators onto a scheduler value and split the
decision into keepForced -> candidates -> admit -> actions. Logic is moved
byte-for-byte; the plan is the same deterministic fixed point.
…phases

runServeContext was 35/76, over the gocyclo cap. Extract parseServeConfig
(flags + env fallbacks folded into a serveConfig), startControlPlaneAPI (the
read-only API bring-up and drain), and serveChannels (the message-answering
loop). Behavior is unchanged; secured-by-default API auth and the ctx-driven
shutdown are preserved.
run was 35/75, over the gocyclo cap. Move the resolved command-line state into
an invocation struct and split the command table out as routeCommand, with a
shared invocation.exit tail mapping err -> exit code. The if-chain becomes a
switch; the dataDirCommands fallback and the exit-code contract (0/1/2 and
exitChangesRequested for review) are unchanged. dataDirCommands keys are
disjoint from the switch cases, so ordering is behavior-preserving.
@ion-alpha-dev
ion-alpha-dev force-pushed the worktree-quality-cleanup-b branch from 76eae29 to ce5ff7d Compare July 17, 2026 15:16
@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.08197% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/flynn/serve.go 89.85% 4 Missing and 3 partials ⚠️
cmd/flynn/main.go 96.07% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

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