エージェントの書くコードに秩序を

社内Linterフレームワーク goffer の紹介

COEFL Go-JP #4

Dennis(デニス) Metzger(メッツガー)

Backend Engineer @ Finatext

あなたのコード、
AIエージェントが書いてますか?

エージェントの書いたコード

  • コンパイルは通る ✅
  • テストも通る ✅
  • go vet も文句なし ✅

でも、レビューすると…

  • コンストラクタを迂回した初期化
  • アーキテクチャを無視したimport
  • 似たテストを大量に生成
  • 気分で変わるコメントの言語
  • MDファイルに記載されているルールを無視

LLMの時代はLinterの時代

  • AGENTS.md
  • SKILL.md
  • 「Linterの警告を直して」✅️

goffer

  • Finatextの社内Linterフレームワーク
  • golangci-lintと違い、
    プロジェクト固有ルールに特化
  • agentic codingのoracleとして設計

設定は .goffer.yaml ひとつ

  1. # .goffer.yaml
  2. similartest:
  3. threshold: 90
  4. min_statements: 2
  5. forbid:
  6. functions:
  7. - ban: ^time\.Now\(\) time\.Time$

ルールも対象範囲もすべてここで宣言

Scope — どこを見るか

  1. scope:
  2. include:
  3. packages:
  4. - ^myapp/internal/
  5. exclude:
  6. files:
  7. - \.gen\.go$
  8. functions:
  9. - \.UnmarshalJSON\(

package・file・関数をregexで絞り込み

include / exclude

  • include: 1つでもマッチすれば対象
    (空なら全部が対象)
  • exclude: マッチしたら除外
  • excludeがincludeより優先

Scopeは3層

  1. scope: # ① 全Linter共通
  2. exclude:
  3. files:
  4. - \.gen\.go$
  5. englishonly:
  6. scope: # ② Linterごと
  7. exclude:
  8. packages:
  9. - ^myapp/legacy/
  10. forbid:
  11. functions:
  12. - ban: ^time\.Now\(\) time\.Time$
  13. scope: # ③ ルールごと
  14. exclude:
  15. packages:
  16. - ^myapp/internal/clock$

全層をANDで評価

Target: 何を報告するか

  1. enforceconstructor:
  2. target:
  3. include:
  4. types:
  5. - \.(Amount|Order)$
  • scope = どこを解析するか
  • target = どの型・関数を報告するか
  • allowimportersのtarget/allow、
    forbidのbanも同じ考え方

段階的に導入できる

  • 生成コード・レガシーはexclude
  • 新規パッケージだけinclude
  • エージェントが触るコードだけ
    厳しくチェック

LLMにルールを発見させる

既存コード
レビューコメント
設計ドキュメント
↓
LLM
↓
.goffer.yaml の提案
↓
人によるレビュー
↓
決定的なルール適用

5つのLinter

  • enforceconstructor - コンストラクタの強制
  • allowimporters - importの制限
  • forbid - 使用禁止ルール
  • similartest - ほぼ同じテストの検出
  • englishonly - コードの言語チェック

enforceconstructor

structの直接初期化を検出し、
コンストラクタ経由の初期化を強制する

  1. enforceconstructor:
  2. scope:
  3. include:
  4. packages:
  5. - myapp/
  1. package payment
  2. type Amount struct {
  3. Value int
  4. Currency string
  5. }
  6. func NewAmount(v int, c string) (Amount, error) {
  7. if v <= 0 {
  8. return Amount{}, ErrInvalidAmount
  9. }
  10. return Amount{Value: v, Currency: c}, nil
  11. }
  1. func chargeUser(u User, total int) error {
  2. amount := payment.Amount{
  3. Value: total,
  4. Currency: "JPY",
  5. }
  6. return charge(u, amount)
  7. }
  1. payment.Amount should not be initialized directly;
  2. use payment.NewAmount instead

allowimporters

packageをimportできる場所を制限する

  1. allowimporters:
  2. rules:
  3. - target: ^myapp/internal/domain$
  4. allow:
  5. - ^myapp/internal/service$
  6. - ^myapp/cmd/
  1. package handler
  2. import (
  3. "net/http"
  4. "myapp/internal/domain"
  5. )
  1. myapp/internal/handler is not allowed to import
  2. myapp/internal/domain

forbid

特定のpackage・関数・メソッド・型を禁止する

  1. forbid:
  2. functions:
  3. - ban: ^time\.Now\(\) time\.Time$
  4. message: use internal/clock.Now() instead
  1. func (s *Service) Create(o Order) error {
  2. o.CreatedAt = time.Now()
  3. return s.repo.Save(o)
  4. }
  1. use internal/clock.Now() instead

similartest

  • 構造がほぼ同じTest関数を検出
  • 変数名・リテラルを正規化して構造だけを比較
  • → table-driven testにまとめる候補
  1. func TestValidate_Empty(t *testing.T) {
  2. err := validate("")
  3. if err == nil {
  4. t.Fatal("expected error")
  5. }
  6. }
  7. func TestValidate_TooLong(t *testing.T) {
  8. err := validate("aaaaaaaaaaaaaaaaaaaaaaaa")
  9. if err == nil {
  10. t.Fatal("expected error")
  11. }
  12. }
  1. test is 100% similar to TestValidate_TooLong
  2. test is 100% similar to TestValidate_Empty

englishonly

  • 英語以外で書かれたコメントやテスト名を検出
  • 統計的な言語判定ではなく、
    Unicode scriptによる決定的な判定
  • 生成ファイル・directive・URLはスキップ
  1. // ユーザーの残高をチェックする
  2. func validateBalance(u User) error {
  3. if u.Balance < 0 {
  4. return ErrInsufficientBalance
  5. }
  6. return nil
  7. }
  8. // issues a receipt (領収書)
  9. func issueReceipt(o Order) (Receipt, error) {
  10. return generateReceipt(o)
  11. }
  1. comment is not written in English
  2. (found Katakana characters)

カッコ内の単語 (領収書) は注釈としてOK

  1. func TestValidate(t *testing.T) {
  2. tests := []struct{ name, in string }{
  3. {name: "空文字はエラー", in: ""},
  4. }
  5. for _, tt := range tests {
  6. t.Run(tt.name, func(t *testing.T) {
  7. // ...
  8. })
  9. }
  10. }
  1. test name "空文字はエラー" is not written in English
  2. (found Han characters)

使い方

  • ローカル・エージェント: goffer ./...
  • CI: GitHub ActionでPRにインライン注釈
  • エージェントへの指示は一行:
    「gofferが通るまで直して」

まとめ

  • プロジェクト固有ルールは
    文書ではなくLinterに
  • gofferは go/analysis + YAMLで
    ルールを宣言的に管理
  • エージェントのoracleとして
    コード品質を機械的に保証