Skip to content

fix(session): detect doom loops across assistant turns - #9

Closed
yukihamada wants to merge 44 commits into
mainfrom
feat/doom-loop-cross-turn-20260924
Closed

yukihamada wants to merge 44 commits into
mainfrom
feat/doom-loop-cross-turn-20260924

Conversation

@yukihamada

@yukihamada yukihamada commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Detect the last three tool calls across assistant turns belonging to the same user input, ignoring intervening non-tool parts.
  • Preserve existing doom_loop allow/deny/ask behavior and reset at user/session boundaries or changed tools/inputs.
  • Add ten regression scenarios including failed/successful repetition, permission rejection and history pagination.

Integration

  • Merged main at 4d58115 into the existing branch with merge commit 166bf1f; normal push, no history rewrite.
  • Exit-code and all unrelated files match main. Final base diff contains only packages/sente/src/session/processor.ts and packages/sente/test/session/processor-effect.test.ts: 125 insertions, 4 deletions.
  • Both target files are unchanged from the tested implementation commit 2cb3cc4. Older ancestor commits remain in history; their unrelated net changes have been removed from this PR.

Verification (macOS, Bun 1.4.0; repository specifies 1.3.14)

  • PASS: packages/sente: bun test --timeout 30000 test/session/processor-effect.test.ts test/session/retry.test.ts — 87 pass, 0 fail, 217 assertions, 25.09s.
  • PASS: git diff --check and staged diff check.
  • PASS after isolated dependency-link repair: packages/sente: bun typecheck (tsgo --noEmit), exit 0. Before repair, both this head and materialized PR base 4d58115 with identical shared dependencies reproduced TS2353 at src/cli/cmd/tui.ts:282:19 (onEnv absent from TuiInput) and TS7006 at :282:25 (env implicitly any). The shared package link resolved the old main-worktree TUI; only ignored worktree-local dependency links were changed to resolve the integrated TUI. Shared dependencies and tracked source files were not modified.
  • PASS after link repair: the same two test files, 87 pass / 0 fail / 217 assertions, 42.75s.
  • NOT PASSING: root bun run lint --silent after link repair — 3957 warnings and 29 errors. Full lint remains unresolved; no clean-lint claim.
  • No dependency installation, release build, paid API call or main merge performed.
  • Requested local reviewer script is absent. Draft retained pending clean verification and CI review.

yukihamada and others added 30 commits September 10, 2026 22:01
ci(release): restore sente release workflows lost in upstream merge
Measured on M-series TUI first draw: bytecode 1390ms vs no-bytecode 1550ms
(~160ms saved) but binary grows from ~142MB to ~334MB and RSS rises. The
~2x size / per-launch savings tradeoff is not worth; bytecode stays opt-in
via --bytecode flag.
opencode serve still works by proxying teai.io/sente/app when the UI is not
embedded. Interactive/CLI users unaffected; serve online path unchanged.
Sente を初めて開いたときに1回だけチュートリアルを出す(2026-09-13本人要望)。
「何ができるか」と「自分の声を登録する導線(koe.live/enroll)」を案内する。

- src/onboarding.ts: 表示条件とキー名を集約。KV は非同期なので ready を待つ
  (待たないと「初回なのに出ない」「毎回出る」が起きる)
- src/ui/dialog-onboarding.tsx: ダイアログ本体。e で声の登録ページを開く
- src/app.tsx: プラグイン読み込み完了(ready)後に1回だけ dialog.replace
- test/onboarding.test.ts: 表示条件のテスト4件

二度と出さない印は KV(~/.local/state/sente/kv.json の onboarding_completed)。
CLI 側の ~/.config/teai/intro-seen-* とは別管理。

検証: bun turbo typecheck 30/30 green / packages/tui の bun test 197 pass 0 fail
bun.lock の integrity が実体と食い違い、bun install が
「Integrity check failed for tarball」で1パッケージ失敗していた。
結果 @sente-ai/client が入らず、session-ui を参照する
enterprise の typecheck が Cannot find module '@sente-ai/client/promise' で落ち、
husky pre-push が通らずリリース push ができない状態だった。

tarball 自体は正常(tar -tzf で検証済)なので、ハッシュを実体に合わせるのみ。
変更は bun.lock の2行。

検証: bun install が「1 package installed」で成功 → bun turbo typecheck 30/30 green
…nding

teai のストリームが途中で失敗したときの文面
「Generation did not complete. Retry shortly.」がどの再試行パターンにも
当たらず、再送すれば成功する一時障害なのに「再試行しないエラー」として
打ち切られていた。

再試行対象に追加する。サーバ側(teai.io)が実際の理由を本文に埋め込む
ようになったため、その中身に既存パターン(5xx 等)が含まれる場合も
ここで拾える。

検証: bun test test/session/retry.test.ts = 65 pass / 0 fail
      bun typecheck = 通過
Adopt the agent-first CLI convention from block/buzz so a caller can branch
on the exit code alone without parsing human-readable output.

- src/cli/exit-code.ts: 0 success / 1 user / 2 network / 3 auth / 4 other /
  5 conflict, plus exitCodeForError() mapping named and tagged errors onto it
- run.ts: replace 10 undifferentiated process.exit(1) calls with classified
  codes; auth failures now exit 3 so callers stop retrying in a loop
- run.ts: --format json now keeps stdout a pure event stream and emits every
  warning (unknown agent, share URL, auto-rejected permission) to stderr as a
  structured {type:diagnostic} record instead of ANSI-styled prose
- error records carry exitCode so a consumer reads the classification from the
  stream as well as from the process status

Verified: 9/9 unit tests, 30/30 turbo typecheck, oxlint 0 errors, and
measured exit=1 with pure-JSON stderr across the usage-error paths.
Subprocess tests remain failing in this environment on the base commit too
(SENTE_CONFIG in the ambient shell overrides SENTE_CONFIG_CONTENT), so the
new end-to-end assertions were dropped rather than left red.
The contract landed in `sente run` (86d041f) but `sente tui` still used
bare `process.exitCode = 1`. All five sites are pre-launch argument
validation (--replay, --mini with network flags, unsupported --mini-only
options, --fork without a session, and worker connection failure), so all
five are ExitCode.User — the classification is unchanged, but it is now
stated instead of implied, and a future change cannot silently drift.

TUI remains interactive: it does not gain --format json, so no structured
output is added here.

Verified: 30/30 turbo typecheck, oxlint 0 errors, measured exit=1 for
--replay and --fork, and no new failures against the baseline.
…dicate

The launcher already used 75 (SENTE_RESTART_EXIT_CODE) for the TUI's
"compact and resume" request, but it was invisible to the contract — a
supervisor reading exit-code.ts would have classified it as Other and
retried it like a transient failure.

- ExitCode.Restart = 75, deliberately outside 0-5 so it cannot collide
- shouldRestart(code): true only for Network(2), Other(4) and Restart(75).
  Permanent failures (User 1, Auth 3, Conflict 5) return false.

The predicate exists because launchd KeepAlive restarts on any exit: a bad
flag or an expired key becomes an infinite loop. That is not hypothetical —
it burned ~4.3M credits over three days in 2026-09.

Verified: 14/14 unit tests including the KeepAlive cases, 30/30 typecheck.
…gh release (the manual Sente distribution recipe)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Pasting an email address into /login now sends a teai.io login code
(/api/v1/auth/email) and a second prompt redeems it (/api/v1/auth/verify).
New accounts use the auto-issued API key; existing accounts log in with
the issued auth token directly. Key paste flow is unchanged.
The server now skips the browser-only Turnstile check for CLI requests
carrying this header (teai.io#748), so /login's email flow works from a
terminal.
Sente launched directly never read $TE_CONFIG_DIR/credentials, so /login
survived only inside the running session and the next launch resolved
{env:TEAI_API_KEY} to empty.

- Load saved credentials before command handlers and worker creation.
- Keep explicit environment overrides and scrub-mode isolation intact.
- Replace every key assignment on save and parse with the launcher's
  last-assignment precedence; a duplicate line could restore the old key.

Verified with a loopback auth fixture: /login -> email -> code -> /exit ->
two fresh process config resolutions. packages/tui 241 pass / 0 fail.
高級モデルは1ターンで数千〜数万クレジットを消費しうる。残高が尽きると
推論が止まり Sente が黙るため、**モデルを選ぶ直前**に気づけるようにした。

- util/credits.ts を追加
  - creditsRemaining(): provider 層が last-route.json へ退避した
    x-teai-credits-remaining を読む。ファイル無し/破損/非数値は null
  - isPremiumModel(): cost.output が閾値(15)を超えるモデルを高級と判定。
    cost 不明なモデルは誤検知を避けるため false
  - LOW_CREDITS_THRESHOLD = 10_000cr
- context/local.tsx の model.set() と model.cycle() に警告を接続
  (/models からの選択と Fキー循環の両経路をカバー)

残高不明な場合は何も出さない — 推測でユーザーの選択を邪魔しないため。

検証: bun typecheck クリーン / bun test 206 pass 0 fail
@yukihamada
yukihamada marked this pull request as ready for review September 24, 2026 14:55
@yukihamada

Copy link
Copy Markdown
Owner Author

Validation status for head 166bf1f: branch conflicts resolved; final diff restricted to processor.ts and processor-effect.test.ts. Local focused regression suite: 87 passed, 0 failed; package typecheck passed after correcting isolated-worktree dependency links. Root lint still reports 29 errors outside this change. GitHub unit/e2e jobs remain QUEUED after a 15-minute wait; these checks have not passed or run. No merge or release performed. Existing doom_loop permissions are preserved: allow does not force a stop. Provider/native and interactive TUI paths remain unverified.

@yukihamada

Copy link
Copy Markdown
Owner Author

CI queued調査・修正(7c8261d761e9ec61f37fd6c1dd24353542e4cb8f、同一branchへ通常push済み)

  • 旧test run 36006690305は4ジョブともqueued、runner_name空、要求ラベルはblacksmith-4vcpu-ubuntu-2404/windows-2025。repos/actions/runnersはtotal_count=0。リポジトリはpublic。
  • 今回の変更は.github/workflows/test.ymlの5行のみ。unit/e2eの4ラベルをGitHub標準ubuntu-24.04/windows-2025に変更。larger runner・課金設定追加なし。
  • HttpApi gateのworking-directoryをpackages/senteへ修正。packages/opencodeは不在で、packages/sente/package.jsonにtest:httpapi、script/httpapi-exercise.tsと参照先test/server/httpapi-exercise/index.tsが実在することを確認。gate/条件/コマンドは削除・skipなし。
  • local setup-bun actionを確認し、Blacksmith依存なし。他workflow・action・アプリコードは未変更。
  • 変更前にPR base 4d581151c7との全差分・含有commit履歴をレビュー。従来のPR本文の「最終差分2ファイル」は今回更新:既存processor.ts/processor-effect.test.tsにtest.ymlを加えた3ファイル。今回commit単体はtest.ymlのみ。
  • 検証:既存Bun.YAMLでparseと変更前後の構造比較成功(許可した5値以外同一)。git diff --check成功。actionlintはPATH上に不在。package bun typecheck成功、既存2ファイル回帰87 pass / 0 fail / 217 assertions(24.63s)。root lintは変更前後とも3957 warnings / 29 errorsで不変、未解消。Bun 1.4.0で検証(repo指定1.3.14)。指定reviewerはファイル不在。
  • push後の開始確認は2回(30秒間隔)で終了。新test run 36026653749はin_progress、4ジョブ全てGitHub Actions runner割当済み。job IDs: linux unit 107724821565 / linux e2e 107724821831 / windows unit 107724821906 / windows e2e 107724822020。要求ラベルも標準runnerに一致。
  • 詳細取得時、linux unitはSetup Bun成功・Cache Turbo実行中、他3件はSetup Bun実行中。失敗stepは未観測、unit/generated client/HttpApi/e2e gatesの完了は未確認。CI greenとは扱わずmain mergeは未実施。継続pollなし。

https://github.com/yukihamada/opencode/actions/runs/36026653749

yukihamada and others added 4 commits September 25, 2026 02:10
- SDK(v1/v2) が x-opencode-directory/workspace を送っていたが、サーバーは
  x-sente-directory を読むため書き込み系リクエストでディレクトリ指定が無視されていた
- パンジャブ語(Aran スクリプト)のロケール判定を修正
- テストの期待値を現行のキー名/設定ファイル名に合わせる

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- sente-release.yml の起動条件を headless-model-fallback への push から
  sente-ship-* タグ push / workflow_dispatch に変更(マージ=即一般配布を止める)
- タグの commit が headless-model-fallback に含まれることを検証
- SENTE_CHANNEL をリリースブランチ名に固定(タグ起動でも sente-<channel>.db を維持)
- macos-14 (arm64) で darwin-arm64 tar の SHA256 照合 → --version 一致 → debug config exit 0
  を通過しない限り publish しない(script/sente-release-smoke.py、隔離 HOME・課金なし)
- publish ジョブを environment sente-release に分離(必須レビュアー設定で二段承認可)
- ビルド時に channel が空(detached HEAD)なら失敗させる。空 channel は sente-.db に
  切り替わり既存セッションが見えなくなるため
- 手動の script/release-sente.sh は緊急用として非推奨化

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

Copy link
Copy Markdown
Owner Author

配布ベース(headless-model-fallback)向けに #19 として出し直しました(旧 main 由来の余分な commit を除き、SDK ヘッダ修正 17400a2 を追加)。こちらはクローズ判断待ちで残しています。

yukihamada and others added 2 commits September 29, 2026 23:03
ci(release): タグ push を唯一のリリース経路にし Apple Silicon 実機 smoke を必須化
bf7f487 raised the models.dev cache TTL from 5 minutes to 6 hours, but the
stale-cache test still wrote an mtime 10 minutes old and expected a refetch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
yukihamada and others added 4 commits September 29, 2026 23:08
完了済み回答の後に processor が compaction を要求すると、prompt.ts が即座に
compaction の user メッセージを作るため最新 user ID が変わり、既存の
「回答済みなら停止」チェックを素通りして合成継続→再回答→再compaction が
繰り返されていた(完了後の課金ループ)。

ツール結果の継続が不要な完了回答では transcript を触らず外側ループへ戻し、
既存チェックで停止させる。compaction は次のユーザー入力時に実行される。

検証: bun test prompt/processor-effect/compaction 132 pass 2 skip 0 fail, bun typecheck exit 0

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
blacksmith-4vcpu-* runners are only available to the upstream org, so every test
job in yukihamada/opencode stayed queued forever. Use ubuntu-latest / windows-latest.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
…nger exists)

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

Copy link
Copy Markdown
Owner Author

注意: 2026-09-29 に #19 向けの修正 3 コミット(4280faa / c8d5c1d / b6bc149。headless-model-fallback の merge を含む)を誤ってこのブランチに push しました。このPRは #19 で置き換え済みで main 向けのため、merge せず close を推奨します。

@yukihamada

Copy link
Copy Markdown
Owner Author

#19 で置き換え済み(マージ済み)のため close。

@yukihamada yukihamada closed this Sep 29, 2026
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