refactor(hooks/builtins): one file per builtin + simplify registration#2526
Merged
dgageot merged 3 commits intodocker:mainfrom Apr 27, 2026
Merged
Conversation
gtardif
previously approved these changes
Apr 27, 2026
aa4451e to
0c76915
Compare
builtins.go now only holds the package doc, name constants, Register, AgentDefaults / ApplyAgentDefaults, and the shared turnStartContext helper. Each builtin (add_date, add_environment_info, add_prompt_files) lives in its own file, making it easier to read, locate, and add new builtins. Public API and behaviour are unchanged; tests pass as-is. Assisted-By: docker-agent
- Replace the map-iteration in Register with three explicit RegisterBuiltin calls combined via errors.Join. The previous loop's per-name error wrapping wasn't reachable in practice (RegisterBuiltin only fails on empty name / nil fn, neither of which can occur with package constants and functions). errors.Join keeps registration order deterministic and reads top-to-bottom. - Add a tiny builtinHook helper so each ApplyAgentDefaults branch is a one-liner instead of a four-line struct literal. - Drop the duplicated turn_start vs session_start rationale from ApplyAgentDefaults' doc; it already lives verbatim in the package comment. Public API and behaviour are unchanged; tests pass as-is. Assisted-By: docker-agent
…its function The AddDate / AddEnvironmentInfo / AddPromptFiles registered-name constants used to live grouped in builtins.go. Move each next to its function in the corresponding add_*.go file so each builtin is fully self-contained: name + behaviour in one place. builtins.go now holds only generic plumbing (Register, AgentDefaults, ApplyAgentDefaults, builtinHook and turnStartContext helpers); adding a new builtin is purely 'drop a new add_*.go with const + func + one line in Register'. Public API and behaviour are unchanged; tests pass as-is. Assisted-By: docker-agent
0c76915 to
625077c
Compare
gtardif
approved these changes
Apr 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Small readability/maintainability refactor of
pkg/hooks/builtins. Nopublic API change, no behaviour change, all existing tests pass as-is.
What changed
1. Split each builtin into its own file
builtins.gowas a 130-line file mixing registration plumbing with threeunrelated builtin implementations. Now each builtin lives in its own file:
add_date.go—AddDateconstant +addDatefunctionadd_environment_info.go—AddEnvironmentInfoconstant +addEnvironmentInfofunctionadd_prompt_files.go—AddPromptFilesconstant +addPromptFilesfunctionEach file is now fully self-contained: registered name and behaviour
sit next to each other.
builtins.gokeeps only the package doc and thegeneric plumbing:
Register,AgentDefaults,ApplyAgentDefaults, andthe small
builtinHook/turnStartContexthelpers.Adding a fourth builtin is now a clear mechanical change: drop a new
add_*.gofile withconst + func, plus one line inRegister.2. Simplify
RegisterandApplyAgentDefaultsRegisterpreviously iterated amap[string]BuiltinFunc{...}and wrapped each error with
fmt.Errorf("register %q builtin: %w", ...).The wrapping wasn't reachable in practice (
RegisterBuiltinonly failson empty name / nil fn, neither possible with package constants), and
map iteration is non-deterministic. Replaced with three explicit
RegisterBuiltincalls combined viaerrors.Join— shorter, ordered,and reads top-to-bottom.
ApplyAgentDefaultshad three near-identical 4-line structliterals (
hooks.Hook{Type: HookTypeBuiltin, Command: ...}). A tinybuiltinHook(name, args...)helper turns each branch into aone-liner, halving the function's size and aligning the three
flag-handling branches visually.
Dropped the duplicated "turn_start vs session_start" rationale
paragraph from
ApplyAgentDefaults' doc — it was already in thepackage comment verbatim.
Validation
Rebased on latest
origin/main. Repo-wide clean:go build ./...✅go vet ./...✅golangci-lint run ./...→ 0 issues ✅go test ./...→ all packages pass ✅pkg/hooks/builtinstests pass unchanged (no test editsneeded — public API is identical).
Out of scope
AgentDefaults.IsZero()is currently only consumed by its own test.Left in place since it's a documented part of the public API and the
brief said "don't remove any feature."
Assisted-By: docker-agent