Skip to content

fix(antigravity): run multiple sessions in parallel - #446

Merged
yuga-hashimoto merged 2 commits into
mainfrom
fix-multiple-sessions
Nov 25, 2026
Merged

yuga-hashimoto merged 2 commits into
mainfrom
fix-multiple-sessions

Conversation

@yuga-hashimoto

Copy link
Copy Markdown
Owner

概要

Antigravity で複数セッションを開くと、1つの応答が終わるまで他のセッションが送信できない問題を修正します。

原因は AntigravityProcessGate が応答完了までグローバルロックを保持していたことです。このロックは「PRoot/Android 上で agy を短時間に連続起動すると停止する」という実機観測への対策ですが、実際に危険なのは CLI 自身のゲスト状態初期化が競合する起動ウィンドウだけです。

  • 起動ウィンドウ (2.5秒) だけをゲートし、起動済みプロセスのストリーミングはセッションごとに並行実行
  • 停止ボタンが起動猶予中でも効くよう、プロセスをゲート内で登録
  • 読み取り/パース失敗時にプロセスとマップエントリを必ず掃除
  • 並行セッション向けに records を insertion-order の synchronized map にし、永続化を直列化

Claude Code / Codex / OpenCode には同等の全セッション共通送信ロックがないことも確認済みです(Codex は単一 app-server が複数スレッドを多重化、Claude Code はセッションごとにプロセス、OpenCode V2 はセッション単位の Mutex)。

検証

  • ./gradlew :app:testGithubDebugUnitTest --tests 'com.yugahashimoto.andcode.runtime.local.Antigravity*' 成功
  • ./gradlew detekt spotlessCheck 成功
  • 追加テスト: 1本目の「応答中」に2本目の起動がゲートを取得できること
  • 実機での複数セッション同時実行は未検証です(この環境に接続端末なし)。起動ウィンドウのみをゲートする根拠は、既存の sign-in TUI が猶予後に他プロセスと並行稼働している実装です。実機確認をお願いします。

Pre-PR review

再レビュー完了。HEAD aee50033、差分 4 ファイル(AntigravityProcessGate.kt / AntigravityRuntime.kt / AntigravityTarget.kt / AntigravityProcessGateTest.kt)、working tree はクリーン。origin/main の先行 5 コミットはいずれも Antigravity に非接触で、origin/main...HEAD は本ブランチ分のみを正しく反映しています。全 hunk と、追加された try/finally・二引数 remove の前後コードを確認しました。

判定: APPROVE

ブロッカー

なし

提案(非ブロッキング)

  • app/src/main/java/com/yugahashimoto/andcode/runtime/local/AntigravityRuntime.kt:296 — 新規の error("Antigravity is busy and the turn could not be scheduled") は SessionError 経由で ChatViewModel の error = message(ChatViewModel.kt:2477-2483)として生表示されます。同ファイル既存の "Antigravity is not installed" / "agy exited with ..." と同じ生英語パターンで CI の i18n-check(ロケールキー一致のみ)は落ちませんが、*Messages 注入方針からは外れたままです(前回指摘から変更なし・既存踏襲のため非ブロッキング)。ローカライズするなら本サブシステム全体をまとめて寄せるのが妥当です。
  • AntigravityRuntime.kt:408 — 念のための整合確認:processes.remove(sessionId, process) は「自分が登録した値のときだけ」false を返し得るため、同一セッションの後発 send が先にエントリを差し替えた場合に後発プロセスを巻き添え終了しないことを確認しました(&& 短絡で terminate も回避)。実装は正しいです。なお abort() と finally が同時刻に terminate を呼ぶ可能性は残りますが、いずれも killProcessTree で実害はなく、finally 側は runCatching で包まれており問題なし。

チェック済み項目

  • 起動猶予中のプロセス登録(269-296): ProcessBuilder(...).start().also { processes[sessionId] = it } が acquireThenRelease ブロック内に入り、猶予 2.5 秒+ロック待ちの間も abort() / abortAll() / remove() がプロセスを検知・停止できることを確認。block() が start() 前に例外を投げた場合は未登録のまま伝播しリークしないこと、start() 成功後の登録なので中止時に markIntentional が正しく立つことも確認。
  • 異常終了時の掃除(298-411): ストリーム読取り/パース例外時に finally が processes.remove(sessionId, process) で自身のマップエントリを除去し、isAlive のときのみ terminate する構造を確認。正常終了・意図的 abort・クラッシュ(require(exitValue==0) 例外)の各経路で finally が走り、旧コードの processes.remove(sessionId)(正常系のみ)によるマップ残留=summarizeTitle() 恒久停止の問題が解消されていることを確認。二引数 remove により後発プロセスを誤殺しないこと、terminate の二重呼び出しが無害化されていることも確認。
  • 既存挙動の非退行: try 内の return@runCatching(turnFinished / wasKilledIntentionally)は finally 実行後に戻り、旧 return@serialize と同じ結果(finalText.orEmpty())になること。process.waitFor() 後の consumeIntentional 順序が変わっていないこと。
  • 並行安全性(前回承認分の再確認): records = Collections.synchronizedMap(linkedMapOf()) で全イテレーション(listSessions 432, workspacePaths, archive, persistRecords)が synchronized(records) 下、単一 get/put のみロック外。processes = ConcurrentHashMap。persistRecords の synchronized(recordsFile) と AntigravityMessageStore.flush() の @Synchronized(別 tmp ファイル)で永続化が直列化され破損しないこと。
  • テスト: acquireThenRelease lets a second session start while the first is running が「ストリーミング中でも 2 本目が 5 秒以内に取得できる」ことを検証し、旧 serialize 相当(60 秒待ち)を確実に落とせることを確認。
  • コメント整合: refreshRateLimits / summarizeTitle / AntigravityTarget のコメントが新挙動(並行ストリーミング・processes.isNotEmpty() ガードは「視聴中チャットの容量を奪わない」意図)に更新され、実装と一致。AntigravityProcessGate の serialize 削除・doc 更新も整合。
  • 静的解析/整形(静的確認): import 順(java.io → java.util.Base64/Collections/concurrent.ConcurrentHashMap)は ktlint 規約内、行長 .editorconfig 140 以内、try/finally 追加による return 数は 2(閾値 4 以内)。※当環境は ./gradlew 実行不可のため detekt/spotless は手確認に留めています。
  • 新規 strings.xml キーなし=8 ロケール整合への影響なし。origin/main 先行分とファイル重複なし=マージ競合なし。working tree クリーン(aee50033)。

Antigravity sends held a global gate for the whole response, so a second
chat could not start until the first finished (up to ten minutes). The
gate exists because two agy processes launched in quick succession hang
under PRoot, which is a startup-window problem: the official CLI's own
guest-state initialization is not safe to race. Keep that startup window
serialized, then let each session's already-started process stream
alongside the others.

The session records and process table are now safe for concurrent
sessions: records keep insertion order under an explicit monitor, and
record persistence serializes its snapshot.
Register a turn's process before the startup grace begins so the stop
button can reach it during that window, and remove (and terminate) a
process that dies on a read/parse failure instead of leaving a stale
entry that blocks title summarisation for every session.
@yuga-hashimoto yuga-hashimoto added the build-apk Build and comment the Android debug APK label Nov 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Debug APK built successfully

Download APK artifact
View workflow run

Commit: ef6a048688c0 | Artifact expires in 14 days.

@yuga-hashimoto
yuga-hashimoto merged commit a960fad into main Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build-apk Build and comment the Android debug APK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant