Repository navigation
fix(runtime): keep the agent list usable while a local OpenCode restart loads plugins - #457
Merged
Merged
Conversation
A local OpenCode installs its plugins on the first GET /agent after every start, which can take over a minute. The agent read was awaited inside the catalogue load while holding its refresh lock, so sessions, providers and workspaces waited on it and the mode chip showed a spinner. - Move the load-time agent read outside the refresh lock, publishing agents as soon as they answer without holding up the next load. - Cache the last good agent list and show it while the fresh read runs. - Keep an agent list through a failed connect instead of clearing it.
Clear a stale agentsError when a new load starts, and give the Koin-built catalog the same on-disk caches as the hand-built one so the two cannot diverge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
ローカル OpenCode はランタイム再起動後の最初の
GET /agentでプラグインをインストールするため、この読み取りが1分以上かかることがあります。従来はこの読み取りをカタログ読み込みのrefreshMutex内でawaitしていたため、エージェントだけでなく sessions / providers / workspaces の公開も待たされ、モードチップも長時間スピナーになっていました(v1.2.31 で #451 の表示順修正後も残っていた症状)。変更
RuntimeCatalogRepositoryは sessions / providers / workspaces だけをロック内で取得し、エージェントは別ジョブで取得します。エージェントが返り次第すぐ反映し、遅いGET /agentが他の取得や次回 load をブロックしません。同一ランタイムの二重読み取りはagentReadで抑止し、ランタイム切替時は前の読み取りをキャンセルします。AgentCatalogCache): 前回の正常な一覧をcatalog-cache/agents-*.jsonに保存し、再起動直後はスピナーの代わりに即表示します。空・選択不能な一覧は保存しません(起動中のサーバーの形のため)。AndCodeApplicationと Koin のAppModuleの両方に同じキャッシュを配線しました。テスト
AgentCatalogCacheTest(保存・読み出し・空リスト非保存)RuntimeCatalogRepositoryTestに4件追加: 遅い読み取り中でもキャッシュを即表示 / 成功で置き換え / 失敗時はキャッシュ保持 / 接続失敗時もキャッシュ保持この環境では Gradle を実行できないため、ビルド・ユニットテストは CI に任せます(
spotlessCheck/detekt/ unit tests)。Pre-PR review
判定: APPROVE
ブロッカー
なし
提案(非ブロッキング)
RuntimeCatalogRepository.kt:238-252— connect 失敗時にagentsは保持するようになりましたが、同じ分岐でprovidersが空リセットされ、直前:211で読んだ provider キャッシュまで捨てられます(これは origin/main からの既存挙動で、今回の回帰ではありません)。agent だけ残して provider を消すのは非対称に見えるので、将来 provider もcurrent.providersFor(target)で保持する余地があります。RuntimeCatalogRepository.kt:380(readAgents)と:440(refreshAgents)は同じagents-*.jsonに並行してwriteTextし得ます。ProviderCatalogCacheと同様に非アトミック書き込みなので、破損時はreadのrunCatchingが null を返して安全側に倒れますが、気になるなら temp ファイル + rename で原子的にすると堅牢です。AndCodeApplication.kt:536-542とAppModule.kt:216-221に同一のJson { ignoreUnknownKeys; isLenient; encodeDefaults }が重複しています。キャッシュ生成を小さな共有ファクトリに寄せると、2 経路の設定ドリフトを防げます(既存コメントの方針とも整合)。チェック済み項目
git fetch origin main後、git diff --stat/ git diff origin/main...HEADの全 6 ファイル・全ハンクを精読。3 コミット(実装 / 前回レビュー反映 / 回帰テスト)をgit showで確認。runtime_catalog_agents/runtime_catalog_connection_failedは既存キーのみ使用。新規キーが無いため全 8 ロケール一致(i18n-check)への影響なし。KDoc/コメントは英語でルール通り。AndCodeApplication(手組み)とAppModule(Koin single)の両方にAgentCatalogCacheを同一catalog-cacheディレクトリで配線。File/androidContext/Jsonの import を確認。Koin 側は消費者が見当たらず遅延生成のまま(コメントも明記)。agentCalls期待値(3 / 6 / callsBefore+1)を新フロー(loadは一覧を読まずreadAgentsが 1 回、空/失敗時のみrefreshAgentsが最大 5 回)で再トレースし一致を確認。cached agents…/successful read replaces/failed read keeps cached/failed connect keeps cachedの 4 件とAgentCatalogCacheTest3 件を個別に手順追跡。TemporaryFolder名も衝突なし。startAgentReadの同 target 時 early-return と別 target 時cancel()、agentReadLock/agentRetryLockの分離、readAgents/loadのruntime?.idガードにより、切替・多重 load で別 runtime の state を汚さないことを確認。refreshAgentsの health==null 経路がreadAgentsからは到達しない(connect 成功後のみ起動)ことも確認。agents become selectable while providers loading、exhausted retries…、failed refresh keeps agents等)の状態遷移を新実装で再現し成立を確認。connect 失敗時のみagentsLoading=falseで cached を残す新挙動も検証。OpenCodeAgentは@Serializable、空/非 selectable は書込・読込とも除外。ファイル名は[^A-Za-z0-9._-]をサニタイズし traversal 不可。秘密情報の追加なし。LongMethod80 /CyclomaticComplexMethod20 /ReturnCount4 /MaxLineLength140、baseline 空)に対し、新規・変更メソッドの複雑度・行長・return 数を確認。ktlint はmax-line-length無効、整形はProviderCatalogCacheと同型。fix/agent-catalog-cacheは main ツリーではなく専用 worktree 上で、作業ツリーは clean。