エージェントの書くコードに秩序を
社内Linterフレームワーク goffer の紹介
Backend Engineer @ Finatext
あなたのコード、
AIエージェントが書いてますか?
エージェントの書いたコード
go vet も文句なし ✅でも、レビューすると…
LLMの時代はLinterの時代
設定は .goffer.yaml ひとつ
- # .goffer.yaml
- similartest:
- threshold: 90
- min_statements: 2
- forbid:
- functions:
- - ban: ^time\.Now\(\) time\.Time$
ルールも対象範囲もすべてここで宣言
- scope:
- include:
- packages:
- - ^myapp/internal/
- exclude:
- files:
- - \.gen\.go$
- functions:
- - \.UnmarshalJSON\(
package・file・関数をregexで絞り込み
include: 1つでもマッチすれば対象exclude: マッチしたら除外
- scope: # ① 全Linter共通
- exclude:
- files:
- - \.gen\.go$
- englishonly:
- scope: # ② Linterごと
- exclude:
- packages:
- - ^myapp/legacy/
- forbid:
- functions:
- - ban: ^time\.Now\(\) time\.Time$
- scope: # ③ ルールごと
- exclude:
- packages:
- - ^myapp/internal/clock$
全層をANDで評価
- enforceconstructor:
- target:
- include:
- types:
- - \.(Amount|Order)$
target/allow、banも同じ考え方.goffer.yaml の提案enforceconstructor - コンストラクタの強制allowimporters - importの制限forbid - 使用禁止ルールsimilartest - ほぼ同じテストの検出englishonly - コードの言語チェックenforceconstructorstructの直接初期化を検出し、
コンストラクタ経由の初期化を強制する
- enforceconstructor:
- scope:
- include:
- packages:
- - myapp/
- package payment
- type Amount struct {
- Value int
- Currency string
- }
- func NewAmount(v int, c string) (Amount, error) {
- if v <= 0 {
- return Amount{}, ErrInvalidAmount
- }
- return Amount{Value: v, Currency: c}, nil
- }
- func chargeUser(u User, total int) error {
- amount := payment.Amount{
- Value: total,
- Currency: "JPY",
- }
- return charge(u, amount)
- }
- payment.Amount should not be initialized directly;
- use payment.NewAmount instead
allowimporterspackageをimportできる場所を制限する
- allowimporters:
- rules:
- - target: ^myapp/internal/domain$
- allow:
- - ^myapp/internal/service$
- - ^myapp/cmd/
- package handler
- import (
- "net/http"
- "myapp/internal/domain"
- )
- myapp/internal/handler is not allowed to import
- myapp/internal/domain
forbid特定のpackage・関数・メソッド・型を禁止する
- forbid:
- functions:
- - ban: ^time\.Now\(\) time\.Time$
- message: use internal/clock.Now() instead
- func (s *Service) Create(o Order) error {
- o.CreatedAt = time.Now()
- return s.repo.Save(o)
- }
- use internal/clock.Now() instead
similartestTest関数を検出
- func TestValidate_Empty(t *testing.T) {
- err := validate("")
- if err == nil {
- t.Fatal("expected error")
- }
- }
- func TestValidate_TooLong(t *testing.T) {
- err := validate("aaaaaaaaaaaaaaaaaaaaaaaa")
- if err == nil {
- t.Fatal("expected error")
- }
- }
- test is 100% similar to TestValidate_TooLong
- test is 100% similar to TestValidate_Empty
englishonly
- // ユーザーの残高をチェックする
- func validateBalance(u User) error {
- if u.Balance < 0 {
- return ErrInsufficientBalance
- }
- return nil
- }
- // issues a receipt (領収書)
- func issueReceipt(o Order) (Receipt, error) {
- return generateReceipt(o)
- }
- comment is not written in English
- (found Katakana characters)
カッコ内の単語 (領収書) は注釈としてOK
- func TestValidate(t *testing.T) {
- tests := []struct{ name, in string }{
- {name: "空文字はエラー", in: ""},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- // ...
- })
- }
- }
- test name "空文字はエラー" is not written in English
- (found Han characters)
goffer ./...go/analysis + YAMLで