Skip to content

fix(desktop): TODO新規worktree作成時のgit worktree add競合を解消#193

Merged
MocA-Love merged 3 commits into
mainfrom
fix/188-todo-new-worktree-race
Apr 16, 2026
Merged

fix(desktop): TODO新規worktree作成時のgit worktree add競合を解消#193
MocA-Love merged 3 commits into
mainfrom
fix/188-todo-new-worktree-race

Conversation

@MocA-Love
Copy link
Copy Markdown
Owner

@MocA-Love MocA-Love commented Apr 15, 2026

概要

Issue #188 の対応。TodoModal で「新しい worktree を作成して実行」にチェックを付けて作成すると、

  • 左サイドバーからそのワークスペースを選択するとエラー
  • AgentManager 右サイドバーの「ブランチ取得中…」が永久に解消されない

という症状が発生していた。

根本原因

`workspaces.create` mutation は DB 行を挿入したあと `initializeWorkspaceWorktree` を await せず(fire-and-forget で)返す。
TodoModal は戻り値を受け取ると即座に `todoAgent.create` を呼び、`supervisor.prepareArtifacts` が `/.superset/todo//goal.md` を mkdir + 書き込みしていた。

この時点では `git worktree add` がまだ走っておらず、ターゲットディレクトリを先に作ってしまうため、その後の `git worktree add` が「path is not empty」で失敗。結果として worktree はディスク上に実体を持たないまま DB 行だけが残り、選択時エラー、`gitSnapshot` の `git rev-parse` も空返答になり「ブランチ取得中…」で止まっていた。

修正

`todoAgent.create` mutation の先頭で `workspaceInitManager.waitForInit(workspaceId)` を await し、初期化完了後に `prepareArtifacts` を実行するように変更。初期化が failed だった場合は明示的にエラーを投げる。

`waitForInit` は対象 job が登録されていなければ即 resolve するため、既存 worktree を選んだ場合のパスには影響しない。

Test plan

  • 新規 worktree 付きで TODO 作成 → ディスク上に worktree が正しく作られ、左サイドバー選択もエラーにならない
  • AgentManager 右サイドバーにブランチ名が表示される
  • 既存 worktree 上での TODO 作成が従来どおり即時に成功する

Closes #188

Summary by CodeRabbit

バグ修正

  • アプリケーション初期化時の信頼性が向上しました
  • 初期化失敗時の適切なエラー処理が実装されました

workspaces.createはgit worktree addをバックグラウンドで実行するため、
直後にtodoAgent.createを呼ぶと未作成のworktreeパスに
.superset/todo/<id>/goal.mdを先に書き込んでしまい、その後の
git worktree addが「空でない」エラーで失敗していた。

todoAgent.createの先頭でworkspaceInitManager.waitForInitを
awaitしてからartifactを準備するようにした。初期化失敗時は
明示的にエラーを返す。

Closes #188
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 15, 2026

Warning

Rate limit exceeded

@MocA-Love has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 42 minutes and 40 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 42 minutes and 40 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f08ff48d-5d15-49d4-aef3-b51ff4986e2f

📥 Commits

Reviewing files that changed from the base of the PR and between 126b830 and 5036816.

📒 Files selected for processing (1)
  • apps/desktop/src/main/todo-agent/trpc-router.ts
📝 Walkthrough

Walkthrough

todoAgent.createミューテーションにワークスペース初期化のブロッキング処理が追加されました。セッション保存やアーティファクト準備の前に初期化待機を実行し、初期化失敗時は明示的にエラーをスローして実行を防止します。

Changes

Cohort / File(s) Summary
ワークスペース初期化の事前ブロッキング
apps/desktop/src/main/todo-agent/trpc-router.ts
workspaceInitManager.waitForInit()を呼び出して初期化完了を待機し、失敗状態を検証してエラーをスロー。初期化失敗時のセッション作成を防止。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰✨ ワークツリー待つその刻
初期化の花咲くまで
エラー防ぎ、道整える
安心のパッチ、ぴょん!🌱

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed PR タイトルは日本語で、修正内容(git worktree add の競合解消)を明確に示しており、変更セットの主要な目的に関連しています。
Description check ✅ Passed PR の説明は概要、根本原因、修正内容、テストプランを詳細に記載しており、テンプレートの主要セクション(説明、関連 Issue、変更タイプ、テスト)をカバーしています。
Linked Issues check ✅ Passed コードの変更(todoAgent.create で workspaceInitManager.waitForInit を await)は Issue #188 の根本原因(git worktree add の競合)を直接的に解決しており、すべてのテストプラン項目に対応しています。
Out of Scope Changes check ✅ Passed すべての変更が Issue #188 に関連する git worktree 初期化の順序制御に限定されており、スコープ外の変更は検出されていません。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/188-todo-new-worktree-race

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 and usage tips.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 126b830405

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// see as the sidebar error + "ブランチ取得中…" that never
// resolves. Block until init is done (or already no-op) so
// prepareArtifacts runs against a real worktree.
await workspaceInitManager.waitForInit(input.workspaceId);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Block TODO creation when workspace init times out

todoAgent.create awaits workspaceInitManager.waitForInit(input.workspaceId) but does not verify that initialization actually finished. In workspace-init-manager.waitForInit, the wait resolves after 30s even if init is still running, so on slower fetch/worktree setup paths this code can still proceed to prepareArtifacts and recreate the same git worktree add “path is not empty” race this change is meant to prevent. After waiting, reject if the workspace is still initializing (or wait without timeout) before touching the worktree path.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
apps/desktop/src/main/todo-agent/trpc-router.ts (1)

52-55: 初期化失敗は TRPCError で返すとクライアント制御が一貫します。

Line 52〜55 は現在 Error を投げていますが、このルーター内の他ハンドラと同様に TRPCError(例: PRECONDITION_FAILED)へ統一すると、UI側でのエラー分岐が安定します。

差分案
 				if (workspaceInitManager.hasFailed(input.workspaceId)) {
-					throw new Error(
-						`todo-agent: workspace ${input.workspaceId} の初期化に失敗しました`,
-					);
+					throw new TRPCError({
+						code: "PRECONDITION_FAILED",
+						message: `todo-agent: workspace ${input.workspaceId} の初期化に失敗しました`,
+					});
 				}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/desktop/src/main/todo-agent/trpc-router.ts` around lines 52 - 55,
Replace the plain Error thrown in the branch that checks
workspaceInitManager.hasFailed(input.workspaceId) with a TRPCError so
client-side error handling is consistent; import TRPCError from '@trpc/server'
(or use the existing import) and throw new TRPCError({ code:
'PRECONDITION_FAILED', message: `todo-agent: workspace ${input.workspaceId}
の初期化に失敗しました` }) instead of throw new Error(...).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@apps/desktop/src/main/todo-agent/trpc-router.ts`:
- Around line 52-55: Replace the plain Error thrown in the branch that checks
workspaceInitManager.hasFailed(input.workspaceId) with a TRPCError so
client-side error handling is consistent; import TRPCError from '@trpc/server'
(or use the existing import) and throw new TRPCError({ code:
'PRECONDITION_FAILED', message: `todo-agent: workspace ${input.workspaceId}
の初期化に失敗しました` }) instead of throw new Error(...).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e1fe58b6-8115-41ec-98e0-e492a6d7c12b

📥 Commits

Reviewing files that changed from the base of the PR and between 58c8e6f and 126b830.

📒 Files selected for processing (1)
  • apps/desktop/src/main/todo-agent/trpc-router.ts

waitForInitは30s時点でresolveしてしまうため、遅いfetch/clone
では初期化中のままprepareArtifactsに進んでしまい同じレース
を再発させる可能性があった。

isInitializing()をポーリングで再確認し、10分上限で
TIMEOUTを返すようにした。失敗判定もTRPCError/
PRECONDITION_FAILEDに統一。

Refs: CodeRabbit/Codex review on PR #193
@MocA-Love MocA-Love merged commit 54f4fb9 into main Apr 16, 2026
6 checks passed
@MocA-Love MocA-Love deleted the fix/188-todo-new-worktree-race branch April 16, 2026 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant