Skip to content

[AI:zf-builder] refactor(server): dedup CreateHubIfAbsent and GetOrCreateHub shared skeleton - #167

Merged
ntsd merged 2 commits into
mainfrom
task/zf-z-49f43665
Nov 20, 2026
Merged

ntsd merged 2 commits into
mainfrom
task/zf-z-49f43665

Conversation

@ntsd

@ntsd ntsd commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

[AI:zf-builder] Task: zf-z-49f43665

refactor(server): dedup CreateHubIfAbsent and GetOrCreateHub shared skeleton (Ponytail Rung 2)

Automated PR for task zf-z-49f43665

Completed by: @zf-builder

ntsd added 2 commits November 20, 2026 07:16
…keleton

Task: zf-z-49f43665

refactor(server): dedup CreateHubIfAbsent and GetOrCreateHub shared skeleton (Ponytail Rung 2)
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying zerohub with  Cloudflare Pages  Cloudflare Pages

Latest commit: d66b43d
Status: ✅  Deploy successful!
Preview URL: https://3b480def.zerohub.pages.dev
Branch Preview URL: https://task-zf-z-49f43665.zerohub.pages.dev

View logs

@ntsd ntsd left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI:zf-reviewer] [Reviewer Feedback] Round 1: Approved for human review.

Correctness

  • server/pkg/zerohub/zerohub.go: the shared skeleton is now a single private getOrCreateHubLocked core; both public methods hold z.mu before calling it, and the core's doc comment states the "under the caller's held z.mu" contract explicitly.
  • Behavior is byte-for-byte preserved:
    • CreateHubIfAbsent: existing hub -> (nil, ErrHubAlreadyExists); build error -> propagated error, nothing stored; success -> (newHub, nil).
    • GetOrCreateHub: existing -> (existing, false, nil); build error -> (nil, false, err); success -> (newHub, true, nil).
  • The atomicity invariants documented on both methods now live in one place, eliminating the mirror-fix risk the task called out.

Test coverage (verified green)

  • cd server && go build ./... && go vet ./... — clean; gofmt -l pkg/zerohub/ — clean.
  • go test -count=1 ./... — all packages pass (config, handler, hub, peer, storage, zerohub).
  • Existing concurrency regression tests (memory + gache backends, 200 goroutines, pointer-identity check) pass unchanged.
  • ErrHubAlreadyExists semantics still covered: zerohub tests (sequential + concurrent) and the handler 409-mapping test (create_static_hub_test.go).
  • New TestSharedCoreBuildHubFailure guards the error branch that moved into the core: asserts error surfaced, hub nil, created=false, and nothing left in storage — through BOTH public entry points. Correct addition for a dedup refactor: the deduplicated branch gained its own direct test.

Ponytail 7-Rung check

  • Rung 7 (diff scope): exactly the 2 task files, +50/-9. No drive-by reformatting, no unrelated churn, no leftover debug code.
  • Rungs 3 & 5 (dependencies): no new packages; test reuses existing imports.
  • Rungs 1 & 6 (simplicity): the extraction is justified — 2 real callers sharing a documented invariant, not a single-caller wrapper. Naming (getOrCreateHubLocked) conveys the locking precondition. No added indirection beyond one level. No YAGNI violations.

Mergeability

  • PR is MERGEABLE / CLEAN against main (branch includes a merge of origin/main).

CONVENTION: For server-only Go refactor tasks, the full verification gate is cd server && go build ./... && go vet ./... && go test -count=1 ./... plus gofmt -l on touched packages — the npm install client/+test/ harness gotchas do not apply (those are only for test/ Playwright specs).

@ntsd
ntsd merged commit 8aabe7e into main Nov 20, 2026
3 checks passed
@ntsd
ntsd deleted the task/zf-z-49f43665 branch November 20, 2026 00:23
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