Skip to content

fix(session): 同じツール呼び出しの巡回ループを確認画面なしで止め、巨大PDFの添付を断る - #52

Merged
yukihamada merged 1 commit into
headless-model-fallbackfrom
read-loop-guard
Oct 28, 2026
Merged

yukihamada merged 1 commit into
headless-model-fallbackfrom
read-loop-guard

Conversation

@yukihamada

Copy link
Copy Markdown
Owner

症状

2026-10-28、セッション ses_e8d630282ffeKs3LbtbHKskTTh(general サブエージェント、moonshotai/kimi-k3、対話画面から起動)が1回の応答(全アシスタントメッセージが同じ parentID)の中で read を943回実行しました。PDF を読むたびに base64 の data: URL(1件あたり最大約28.6MB)が part.data.state.attachments と event テーブル(message.part.updated)に保存され、ローカルDBが25GB(うち約17.5GB)まで膨らみ、TUIの起動が止まりました。

原因(DBの実データで確認)

  • 同じ6つのPDFを順番に巡回していた: v7.pdf → actual-v4 → appendix-b → one-pager → email-drafts → bilingual → v7.pdf …、各ファイル約148〜149回。同じ呼び出しが連続したのは最大でも1回だけでした。
  • 既存の doom_loop 判定(processor.ts DOOM_LOOP_THRESHOLD=3)は「直近3件のツール呼び出しがすべて同一」のときだけ発火するため、巡回には反応しませんでした。設定や承認で許可されていたわけではありません(permission テーブルは空、doom_loop の allow 設定もなし)。
  • ループのきっかけ(推定): ステップごとの入力トークンは約150〜450で、キャッシュ読み込みはほぼ一定(約12.5万)。PDFの中身はモデルに届いておらず、ツールの結果は「PDF read successfully」だけでした。モデルはPDFを受け付けないため、中身を読もうとして同じファイルを読み直し続けたとみられます。

修正

  1. 繰り返しガード(session/processor.ts、確認画面なし): 同じ応答の中で、入力も結果も前回と同じ呼び出しを数えます。3回目からはモデルに結果の代わりにツールエラー(「結果は変わらないので再実行しない」)を返して添付も保存せず、6回目で応答を止めます。巡回(A,B,A,B…)も対象です。結果が変わる呼び出し(ポーリング)は数えません。カウントはメモリ上だけで持ち、応答単位で最大64件までです。
  2. doom_loop の既定 "ask" では確認画面を出さなくなりました(上のガードが代わりに止めます)。"deny" は従来どおり3回連続で停止し、"allow" はガードごと無効にします。ask/allow のときはツール呼び出しごとの履歴スキャン(巨大な部分を含むメッセージを読み込む処理)も省きます。
  3. tool/read.ts: 10MB(MAX_PDF_BYTES)を超えるPDFと、現在のモデルが受け付けない画像・PDFは添付せず、テキストの抜き出し方(pdftotext)を示すエラーを返します。画像は従来どおり保存前に縮小されるため、サイズ上限は設けていません。ツール説明とドキュメント(permissions.mdx の英語版・日本語版)も更新しました。

検証

  • bun typecheck(packages/sente): 成功
  • bun test test/session/processor-effect.test.ts test/tool/read.test.ts test/agent/agent.test.ts test/permission: 297件成功・失敗0
  • 再現テスト「repeat guard answers a cycle…」は旧コードでは失敗します(20ステップ止まらない)。新コードでは11ステップ目で停止します。ポーリングのテストは、旧コードだと確認画面待ちでタイムアウトします。
  • test/session test/tool test/agent test/permission test/cli/run の全体: 1345件成功・13件失敗。13件(prompt.test / security-boundary-audit / run-process)は変更前のコードでも同じ13件が失敗しており、今回の変更とは関係ありません。
  • 未確認: 実際の kimi-k3 での対話画面の動作(実機での再現はしていません)。

戻し方

このPRのマージコミットを revert します。設定だけで戻す場合は、permission: { "doom_loop": "allow" } でガードを無効にできます(PDFの上限は設定では戻せないため、revert が必要です)。

🤖 Generated with Claude Code

…efuse oversized PDFs

A general subagent read the same six PDFs in a cycle ~150 times each in one
turn (943 tool calls), storing a ~28 MB base64 copy of each PDF per call and
growing the local DB to 25 GB. The doom_loop guard only looked at the last 3
calls being identical, so a cycle (A,B,...,A,B) never triggered it.

- processor: repeat guard per turn (in memory). From the 3rd call whose input
  and result are both unchanged, the model gets a tool error instead of the
  result (attachments not stored); the 6th ends the turn. No dialog.
  doom_loop "allow" disables it; "deny" keeps the consecutive-3 stop. The
  default "ask" no longer opens a dialog (and skips the history scan).
- read: refuse PDFs over 10 MB and media the current model cannot accept,
  with a hint to extract text (pdftotext).
- tests reproduce the cycle (fails on the old code: 20 steps, no stop).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@yukihamada
yukihamada merged commit 0d3f2b3 into headless-model-fallback Oct 28, 2026
3 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant