Skip to content

refactor: 監査残タスクの完遂(aichat分割・strict復帰・モジュールフラグ化) - #28

Merged
hitalin merged 4 commits into
masterfrom
refactor/fork-audit-phase2
Aug 21, 2026
Merged

hitalin merged 4 commits into
masterfrom
refactor/fork-audit-phase2

Conversation

@hitalin

@hitalin hitalin commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

なぜ

PR #27(バグ修正)で意図的に見送った予防的な品質投資を完遂する。方針確認済みのとおり Gemini 専用のまま(OpenAI互換/OpenRouter 対応はしない)。

変更内容

aichat の4ファイル分割(1497行 → 890行 + 純関数モジュール3つ)

  • gemini-client.ts: API型・呼び出し・レスポンス解析・grounding整形(hasOwnProperty連打80行を optional chaining に)
  • prompt.ts: URL/YouTube抽出・感情検出・system instruction 構築(3箇所で再発明されていたURL正規表現を統一)
  • memory.ts: 記憶システム一式(二重定義されていた話題辞書を統一、memory: any を型付け、感情キーワード計測の生 RegExp 生成を排除)
  • 挙動修正:
    • 記憶の二重注入を解消: system instruction への「直近5件の会話全文」再注入をやめ、重要度7以上の長期記憶(最大3件)のみに。直近のやり取りは会話履歴(contents)に既にあり、トークンを浪費していた
    • ユーザーごと10秒のクールダウンを追加(メンション連投による Gemini コスト暴走の防止)
    • 記憶・引用由来テキストを <past-memories> 区切りの参考データとして渡す(プロンプトインジェクション対策の残り)

strict: true 復帰

モジュール有効化の config フラグ移行

  • src/index.ts でコメントアウトされていた6モジュールを登録し、mazeEnabled / pollEnabled / emojiReactEnabled / earthquakeWarningEnabled(新設、デフォルト無効)と既存フラグで制御。デフォルト挙動は従来と同一
  • ⚠️ これにより earthquake_warning は「コードはあるが起動時登録されず動いていなかった」状態から「config で有効化可能」になった。有効化する場合は PR fix: フォーク独自改変部分の監査で見つかった重大バグ群を修正 #27 で修正済みとはいえ初の実稼働なのでログ観察を推奨

小粒整理

  • UserFormatter クラス → 関数 export
  • emoji モジュールのハードコード文言 → serifs へ
  • serifs.poll の未使用 title 引数削除、未参照の emoji-list.json(リポジトリ直下)削除

見送り(理由付き)

  • emojilist.json の生成物化: 依存またはスクリプト追加に見合う利得がない(データは安定して機能している)
  • 藍→唯リネームの局所化: import 名を変えても upstream との diff は消えず、効果が薄い

テスト

  • 44 → 71件(memory 12 / gemini-client 5 / prompt 10 追加)、tsc --noEmit(strict)・npm run build・npm test 全パス

🤖 Generated with Claude Code

https://claude.ai/code/session_01VZSVuRBBc7STzkdDq31GDM

hitalin and others added 4 commits August 21, 2026 16:49
1602行に9責務が同居していた index.ts を分割:
- gemini-client.ts: Gemini API 型・呼び出し・レスポンス解析・
  grounding メタデータ整形(hasOwnProperty 連打80行を optional
  chaining で整理、抽出・整形は純関数化しテスト追加)
- prompt.ts: URL/YouTube 抽出・感情的質問の検出・system instruction
  構築(3箇所で再発明されていた URL 正規表現を extractUrls に統一)
- memory.ts: 人間らしい記憶システム一式(純関数群としてテスト12件。
  2箇所に微妙に異なる内容で二重定義されていた話題キーワード辞書を
  和集合に統一。memory: any を AiChatMemory 型に。感情キーワードの
  出現数計測を生 RegExp 生成から split カウントに変更し、正規表現
  メタ文字を含む語での誤動作リスクを排除)
- index.ts: フックのオーケストレーションのみ(1497行 → 890行)

挙動修正:
- 記憶の system instruction 注入から「直近5件の会話全文」を廃止し、
  重要度7以上の長期記憶(最大3件)のみに変更。直近のやり取りは
  会話履歴(contents)として既に渡っており、同じ会話が二重に入って
  トークンを浪費していた
- 記憶・引用由来のテキストを <past-memories> 区切りの参考データとして
  渡し、指示として解釈しないことを明示(プロンプトインジェクション
  対策の残り)
- ユーザーごとに10秒のクールダウンを追加(メンション連投による
  Gemini 呼び出しの暴走・コスト増を防止。上限1000ユーザーのLRU風管理)
- 3箇所にコピペされていた記憶初期化を createEmptyMemory に統一

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VZSVuRBBc7STzkdDq31GDM
フォークで strict: false に緩められていた型検査を upstream と同じ
strict: true(noImplicitAny: false は upstream 同様維持)に復帰。
今回の監査で見つかったバグ群がすべて型検査をすり抜けていた根本対策。

- lifecycle(install/run/onInit)で初期化されるフィールド17箇所に
  definite assignment assertion を付与
- ai.api を api<T = unknown>() にジェネリクス化し、chart の
  as any 11箇所をレスポンス型(利用フィールドのみ定義)で置換
- reminder の catch 節の err 参照を型ガード付きに修正

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VZSVuRBBc7STzkdDq31GDM
src/index.ts で6モジュール(emoji-react / keyword / maze / chart /
poll / earthquake_warning)の登録がコメントアウトされ、TODO が散在
していた。全モジュールを登録し、有効/無効は config フラグで制御する
方式に統一(デフォルトは従来どおり無効。mazeEnabled / pollEnabled /
emojiReactEnabled / earthquakeWarningEnabled を新設し、keyword /
chart は既存フラグを使用)。

有効化したいモジュールは config.json にフラグを追加するだけでよく、
コード変更・再ビルドの diff が発生しなくなる。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VZSVuRBBc7STzkdDq31GDM
- static メソッド1個だけの UserFormatter クラスを関数 export に変更
- emoji モジュールのハードコード応答文を serifs.emoji.intro へ移動
- serifs.poll.result / resultTie の未使用 title 引数を削除
- どこからも参照されていないリポジトリ直下の emoji-list.json を削除
  (src/utils/emojilist.json とは別物の未使用データ)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VZSVuRBBc7STzkdDq31GDM
@coderabbitai

coderabbitai Bot commented Aug 21, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@hitalin, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 21 seconds

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

Wait for the limit to reset, then comment @coderabbitai review or push new commits to the PR.

An organization admin can change what happens after included review limits in Billing.

How do review limits work?

CodeRabbit enforces per-developer PR review limits within each organization.

For paid Pro and Pro+ reviews, CodeRabbit uses a developer's included PR review attempts over the past 7 days to set the current hourly allowance. At typical activity levels, the full plan allowance applies. Higher sustained activity can lower the allowance until earlier attempts leave the 7-day window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8cef1656-b989-4220-8d1f-75200753567e

📥 Commits

Reviewing files that changed from the base of the PR and between 2e9a75f and 507e810.

📒 Files selected for processing (30)
  • emoji-list.json
  • example.json
  • src/ai.ts
  • src/index.ts
  • src/module.ts
  • src/modules/aichat/gemini-client.test.ts
  • src/modules/aichat/gemini-client.ts
  • src/modules/aichat/index.ts
  • src/modules/aichat/memory.test.ts
  • src/modules/aichat/memory.ts
  • src/modules/aichat/prompt.test.ts
  • src/modules/aichat/prompt.ts
  • src/modules/chart/index.ts
  • src/modules/check-custom-emojis/index.ts
  • src/modules/earthquake_warning/index.ts
  • src/modules/emoji-react/index.ts
  • src/modules/emoji/index.ts
  • src/modules/follow/index.ts
  • src/modules/guessing-game/index.ts
  • src/modules/kazutori/index.ts
  • src/modules/keyword/index.ts
  • src/modules/maze/index.ts
  • src/modules/poll/index.ts
  • src/modules/reminder/index.ts
  • src/modules/reversi/back.ts
  • src/modules/server/index.ts
  • src/serifs.ts
  • src/utils/normalize-config.ts
  • src/utils/user-formatter.ts
  • tsconfig.json

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@hitalin
hitalin merged commit f26df48 into master Aug 21, 2026
4 checks passed
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