Skip to content
Source
Sign inSign up
pingdotgg/t3code

Git integration: branch picker + worktrees #3

Mergedjuliusmarmingemerged 32 commits intomain fromrefs/pull/3/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

Closes #7 (Does not implement multiple threads per worktree)

Summary

Users can now right-click on "+ New thread" to create threads from specific git branches, with optional worktree creation for isolated agent work.

Features

  • Branch context menu showing local branches sorted by current/default/alphabetical
  • Option to create git worktree for each branch before starting a thread
  • Graceful handling of non-git repositories with "Initialize git repo" button
  • Thread model extended with branch/worktreePath fields for persistence
  • Agent sessions use worktree path as cwd when available
  • Updated persistence schema to store branch and worktree info

Test Plan

  • Right-click "+ New thread" on a git-tracked project to see branch list
  • Select a branch to create thread with that branch in metadata
  • Click "worktree" button to create isolated worktree before starting thread
  • On non-git project, click to see "Not a git repository" with init option
  • Verify agent runs in worktree cwd when applicable
  • Verify branch/worktree badge displays in chat header

🤖 Generated with Claude Code


Open with Devin

Summary by CodeRabbit

  • New Features

    • Branch toolbar beneath chat input to choose branch and environment (Local vs Worktree); environment locks after the first message.
    • Branch picker auto-populates from the repository; toolbar hidden for non-git projects.
    • First message in Worktree mode can create a new worktree and associate it with the thread; branch/worktree badges appear in header and sidebar.
  • Tests

    • Extensive unit and integration tests for branch, worktree, checkout, and end-to-end git flows.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Note

      Reviews paused

      It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

      Use the following commands to manage reviews:

      • @coderabbitai resume to resume automatic reviews.
      • @coderabbitai review to trigger a single review.

      Use the checkboxes below for quick actions:

      • ▶️ Resume reviews
      • 🔍 Trigger review

      Walkthrough

      Adds end-to-end Git integration: new contracts and WS API, server & desktop git utilities, renderer IPC and React Query wiring, per-thread branch/worktree state and persistence, BranchToolbar UI, ChatView worktree creation on first message, tests, and build/tooling/script updates.

      Changes

      Cohort / File(s)Summary
      Contracts & WS API
      packages/contracts/src/git.ts, packages/contracts/src/ipc.ts, packages/contracts/src/ws.ts, packages/contracts/src/index.ts
      New zod schemas/types and result types for git operations; added WS method names; extended NativeApi.git with listBranches/createWorktree/removeWorktree/createBranch/checkout/init; re-exported git.
      Server git module & WS handlers
      apps/server/src/git.ts, apps/server/src/wsServer.ts, apps/server/src/git.test.ts, apps/server/src/wsServer.test.ts
      Introduced server-side git utilities (runTerminalCommand, list/create/remove worktree, branch ops, init) and wired them into WS router; added FS-backed integration tests covering branches, worktrees, checkouts, and WS git RPCs.
      Desktop git helpers & tests
      apps/desktop/src/git.ts, apps/desktop/src/git.test.ts, apps/desktop/src/main.ts, apps/desktop/src/preload.ts
      Extracted desktop git helpers into module; main imports module; preload exposes IPC for git operations; added desktop git tests.
      Renderer native bridge
      apps/web/src/wsNativeApi.ts, apps/desktop/src/preload.ts
      Renderer-facing native API gains git.* methods that route to WS / preload IPC.
      Frontend UI & flows
      apps/web/src/components/BranchToolbar.tsx, apps/web/src/components/ChatView.tsx, apps/web/src/components/Sidebar.tsx, apps/web/src/App.tsx
      Added BranchToolbar and integrated it into ChatView; ChatView manages envMode (local
      State, types & persistence
      apps/web/src/types.ts, apps/web/src/store.ts, apps/web/src/persistenceSchema.ts, apps/web/src/persistenceSchema.test.ts, apps/web/src/store.test.ts
      Extended Thread with branch and worktreePath; added SET_THREAD_BRANCH action and reducer handling (clears session when worktreePath changes); persistence schema migrated to include nullable/optional fields; tests updated.
      Web wiring & deps
      apps/web/src/App.tsx, apps/web/package.json
      Added @tanstack/react-query dependency, created QueryClient and wrapped app in QueryClientProvider; thread creation payload extended with new fields.
      Tests & test plan
      .plans/*, apps/desktop/src/git.test.ts, apps/server/src/git.test.ts, apps/web/src/git-flows.test.ts
      Added integration/unit tests and a test plan for Git flows, worktrees, store reducer, and edge cases; factory helpers and comprehensive scenarios included.
      Build/tooling & scripts
      apps/*/tsdown.config.ts, removed tsup.config.ts, package.json, apps/*/package.json, *.turbo.jsonc, vitest.config.ts
      Switched build tooling from tsup→tsdown, updated package scripts to turbo orchestration, adjusted outputs to .mjs/ESM, added turbo configs and vitest alias changes.
      Misc UI/docs/formatting
      README.md, AGENTS.md, various apps/web/*, apps/server/*, packages/contracts/*
      Docs and plan files added/updated; assorted formatting and small non-functional refactors across CSS, tests, and TS files.

      Sequence Diagram

      sequenceDiagram
          participant User as User
          participant ChatView as ChatView (UI)
          participant BranchToolbar as BranchToolbar (UI)
          participant Store as Renderer Store
          participant NativeAPI as Native API (preload/WS)
          participant WebSocket as WebSocket Transport
          participant Server as Server (git module)
      
          User->>ChatView: open chat / branch controls
          ChatView->>BranchToolbar: render (envMode, envLocked)
          BranchToolbar->>NativeAPI: git.listBranches({ cwd })
          NativeAPI->>WebSocket: invoke "git:list-branches"
          WebSocket->>Server: WS_METHODS.gitListBranches
          Server-->>WebSocket: return branches
          WebSocket-->>NativeAPI: branches
          NativeAPI-->>BranchToolbar: branches list
      
          User->>ChatView: send first message
          alt envMode == "worktree" && no worktreePath
              ChatView->>NativeAPI: git.createWorktree({ branch, cwd })
              NativeAPI->>WebSocket: invoke "git:create-worktree"
              WebSocket->>Server: WS_METHODS.gitCreateWorktree
              Server->>Server: runTerminalCommand("git worktree add ...")
              Server-->>WebSocket: { worktreePath, branch }
              WebSocket-->>NativeAPI: result
              NativeAPI-->>ChatView: worktreePath
              ChatView->>Store: dispatch SET_THREAD_BRANCH(threadId, branch, worktreePath)
          else
              ChatView->>Store: dispatch SET_THREAD_BRANCH(threadId, branch, null)
          end
      
          Store->>Store: update thread.branch & thread.worktreePath
          ChatView->>NativeAPI: ensure/start session(cwd = worktreePath ?? project.cwd)
          NativeAPI->>WebSocket: start session
          WebSocket->>Server: establish session in selected cwd
          Server-->>WebSocket: session ready
          WebSocket-->>NativeAPI: session active
          NativeAPI-->>ChatView: session active

      Estimated code review effort

      🎯 4 (Complex) | ⏱️ ~60 minutes

      🚥 Pre-merge checks | ✅ 3 | ❌ 2
      ❌ Failed checks (2 warnings)
      Check nameStatusExplanationResolution
      Out of Scope Changes check⚠️ WarningThe PR includes tooling/infrastructure changes (tsup→tsdown migration, Turbo config, Vitest config, package.json restructuring) unrelated to the Git integration feature objectives stated in issue #7.Extract tooling/build changes into a separate PR. Keep this PR focused on Git feature implementation only—branch picker UI, worktree support, persistence schema updates, and related tests.
      Docstring Coverage⚠️ WarningDocstring coverage is 12.20% which is insufficient. The required threshold is 80.00%.Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (3 passed)
      Check nameStatusExplanation
      Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
      Title check✅ PassedThe title 'Git integration: branch picker + worktrees' accurately summarizes the main changes—adding Git branch selection UI and worktree support for thread management.
      Linked Issues check✅ PassedThe PR implements all primary objectives from issue #7: threads associated with local Git branches (current, existing, new), Git worktree-based workspaces, and worktree reuse patterns. Multiple-threads-per-worktree and 'best of N' selection noted as out-of-scope.
      ✏️ Tip: You can configure your own custom pre-merge checks in the settings.
      ✨ Finishing touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Post copyable unit tests in a comment
      • Commit unit tests in branch `juliusmarminge/git-integration`

      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    2. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Add branch picker and worktree flow to ChatView and expose NativeApi.git.* methods backed by WebSocket handlers in wsServer.ts

      Introduce a BranchToolbar in ChatView for branch selection and worktree creation, wire NativeApi.git.listBranches/createWorktree/removeWorktree/createBranch/checkout/init to WebSocket routes, and update thread state to track branch and worktreePath with session reset on cwd changes. Switch builds to tsdown for desktop/server/contracts and add React Query at the web app root.

      📍Where to Start

      Start with WebSocket routing for git operations in createServer in wsServer.ts, then review the UI integration in BranchToolbar in BranchToolbar.tsx and thread state changes in store.ts.


      Macroscope summarized 9442019.
    3. @@ -0,0 +25,21 @@
      25+ return { branches: [], isRepo: false };
      26+ }
      27+ throw new Error(stderr || "git branch failed");
      28+ }
      29+
      30+ // Resolve the real default branch from the remote
      31+ const defaultRef = await runTerminalCommand({
      32+ command: "git symbolic-ref refs/remotes/origin/HEAD",
      33+ cwd: input.cwd,
      34+ timeoutMs: 5_000,
      35+ });
      36+ const defaultBranch =
      37+ defaultRef.code === 0 ? defaultRef.stdout.trim().replace(/^refs\/remotes\/origin\//, "") : null;
      38+
      39+ const branches = result.stdout
      40+ .split("\n")
      41+ .map((line) => line.trim())
      42+ .filter((line) => line.length > 0)
      43+ .map((line) => ({
      44+ name: line.replace(/^[*+]\s+/, ""),
      45+ current: line.startsWith("* "),
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium

      src/git.ts:45 The child.killed flag becomes true immediately after child.kill("SIGTERM") is called, so the SIGKILL fallback is unreachable. Consider tracking process exit separately (e.g., with a flag set in the close event) instead of relying on child.killed.

      🚀 Want me to fix this? Reply ex: "fix it for me".

    4. @@ -0,0 +35,21 @@
      35+ cwd: input.cwd,
      36+ env: process.env,
      37+ stdio: ["ignore", "pipe", "pipe"],
      38+ });
      39+
      40+ let stdout = "";
      41+ let stderr = "";
      42+ let timedOut = false;
      43+
      44+ const timeout = setTimeout(() => {
      45+ timedOut = true;
      46+ child.kill("SIGTERM");
      47+ setTimeout(() => {
      48+ if (!child.killed) {
      49+ child.kill("SIGKILL");
      50+ }
      51+ }, 1_000).unref();
      52+ }, input.timeoutMs ?? 30_000);
      53+
      54+ child.stdout?.on("data", (chunk: Buffer) => {
      55+ stdout += chunk.toString();
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟢 Low

      src/git.ts:55 Multi-byte UTF-8 characters split across chunks will corrupt to �. Consider using StringDecoder from node:string_decoder to properly handle partial characters.

      🚀 Want me to fix this? Reply ex: "fix it for me".

      🤖 Prompt for AI
      In file apps/server/src/git.ts around line 55:
      
      Multi-byte UTF-8 characters split across chunks will corrupt to `�`. Consider using `StringDecoder` from `node:string_decoder` to properly handle partial characters.
    5. @greptileai review

    6. Actionable comments posted: 5

      Caution

      Some comments are outside the diff and can’t be posted inline due to platform limitations.

      ⚠️ Outside diff range comments (1)
      apps/desktop/scripts/smoke-test.mjs (1)

      12-56: ⚠️ Potential issue | 🟠 Major

      Handle spawn errors and non‑zero exits to avoid false positives. Uncaught error events (e.g., missing Electron binary) and non‑zero exit codes can slip through, leading to flaky or misleading smoke-test results.

      🛠️ Suggested fix
       const child = spawn(electronBin, [mainJs], {
         stdio: ["pipe", "pipe", "pipe"],
         env: {
           ...process.env,
           VITE_DEV_SERVER_URL: "",
           ELECTRON_ENABLE_LOGGING: "1",
         },
       });
       
      +child.on("error", (err) => {
      +  clearTimeout(timeout);
      +  console.error("\nDesktop smoke test failed (spawn error):");
      +  console.error(err);
      +  process.exit(1);
      +});
      +
       let output = "";
       child.stdout.on("data", (chunk) => {
         output += chunk.toString();
       });
       child.stderr.on("data", (chunk) => {
         output += chunk.toString();
       });
       
       const timeout = setTimeout(() => {
         child.kill();
       }, 8_000);
       
      -child.on("exit", () => {
      +child.on("exit", (code, signal) => {
         clearTimeout(timeout);
       
         const fatalPatterns = [
           "Cannot find module",
           "MODULE_NOT_FOUND",
           "Refused to execute",
           "Uncaught Error",
           "Uncaught TypeError",
           "Uncaught ReferenceError",
         ];
         const failures = fatalPatterns.filter((pattern) => output.includes(pattern));
       
      -  if (failures.length > 0) {
      +  if (failures.length > 0 || code !== 0 || signal) {
           console.error("\nDesktop smoke test failed:");
      +    if (code !== 0) console.error(` - exit code: ${code}`);
      +    if (signal) console.error(` - signal: ${signal}`);
           for (const failure of failures) {
             console.error(` - ${failure}`);
           }
           console.error("\nFull output:\n" + output);
           process.exit(1);
         }
      🤖 Fix all issues with AI agents
      In @.plans/git-flows-integration-tests.md:
      - Around line 93-99: The documentation uses "npx vitest run
      apps/desktop/src/git.test.ts" which contradicts the Bun-centric tooling
      elsewhere; update the verification snippet to use Bun's runner by replacing the
      "npx" invocation with "bunx" (so the second command uses bunx vitest run
      apps/desktop/src/git.test.ts) and keep the existing "cd apps/desktop && bun run
      test" line unchanged; ensure both examples consistently recommend Bun tooling.
      
      In `@apps/desktop/package.json`:
      - Around line 22-24: Update the package.json dev dependency for "vitest" from
      "^4.0.0" to "^4.0.18" to pick up recent patch fixes, and optionally add a
      comment or package.json note near "tsdown" to clarify it's already at the latest
      stable (0.20.3) if you want to signal stability; also review the test script
      that passes "--passWithNoTests" (in package.json scripts) and decide whether to
      remove or tighten that flag after updating vitest to encourage real test
      coverage.
      
      In `@apps/server/src/git.ts`:
      - Around line 122-183: The git commands in createGitWorktree, removeGitWorktree,
      createGitBranch, and checkoutGitBranch are built as single shell strings with
      single quotes (e.g., using escapeSingleQuotes) which breaks on Windows cmd.exe;
      change each to spawn git directly with an args array instead of a shell command
      string (e.g., call runTerminalCommand or child_process.spawn with ['git',
      'worktree', 'add', '-b', input.newBranch, worktreePath, input.branch'] and
      shell: false), remove the single-quote escaping (no escapeSingleQuotes
      wrapping), keep cwd and timeoutMs, and ensure failures still throw the same
      errors based on result.code/result.stderr.
      
      In `@apps/web/package.json`:
      - Around line 28-32: The package.json currently pins "vite" to a beta
      pre-release ("^8.0.0-beta.12"); change the "vite" dependency entry to a stable
      7.x release (for example "^7.21.0" or the latest 7.x), update the lockfile by
      running your package manager (npm/yarn/pnpm install), and run the dev/build/test
      commands to verify nothing breaks; edit the "vite" key in package.json to
      replace the beta version, commit the lockfile changes, and ensure CI passes
      (reference: the "vite" dependency entry in package.json).
      
      In `@apps/web/src/components/BranchToolbar.tsx`:
      - Around line 61-73: The auto-sync useEffect in BranchToolbar (the effect that
      checks branches.find and dispatches SET_THREAD_BRANCH) is overwriting a
      user-selected worktree base branch; change the guard so the effect only runs for
      true local threads (e.g., when activeThread exists AND activeThread.worktreePath
      is falsy AND the thread is in local mode) or only when activeThread.branch is
      null/undefined, then proceed to find the current branch and dispatch
      SET_THREAD_BRANCH; update the useEffect condition that currently checks
      (!activeThread || activeThread.worktreePath) to include the extra guard (or
      replace it with a check for activeThread.branch == null) so selecting a
      different base branch for a pending worktree is not clobbered.
      🧹 Nitpick comments (3)
      .plans/git-flows-test-plan.md (2)

      28-32: Consider adding guidance on mock cleanup between tests.

      The factory pattern is solid, but the plan should mention resetting mocks between tests to ensure isolation. Consider adding a note about using beforeEach(() => vi.clearAllMocks()) or similar to reset the mocked Git API functions between test cases.

      📋 Suggested addition to the Factories section

      Add after line 32:

      **Setup/Teardown**
      
      Use `beforeEach(() => vi.clearAllMocks())` to reset mocked functions between tests, ensuring each test starts with clean mock state.

      Based on learnings: For integration tests: Prefer deterministic inputs and explicit state checks; avoid relying on logs or timing assumptions. Clean up external resources in finally blocks.


      94-102: Clarify working directory assumptions for verification commands.

      The verification commands mix two approaches: line 96 changes directory before running tests, while lines 99-102 use full paths from the repo root. This inconsistency could confuse developers.

      🔧 Suggested clarification

      Consider adding a comment about the working directory:

       ## Verification
       
      +All commands assume you're at the repository root unless otherwise noted.
      +
       ```bash
       # Run all renderer tests
       cd apps/renderer && bun run test
       
       # Run just the new test file
       npx vitest run apps/renderer/src/git-flows.test.ts
       
       # Run just the store tests
       npx vitest run apps/renderer/src/store.test.ts
      
      Alternatively, make all commands consistent:
      
      ```bash
      # Run all renderer tests
      npx vitest run apps/renderer
      
      # Run just the new test file
      npx vitest run apps/renderer/src/git-flows.test.ts
      
      # Run just the store tests
      npx vitest run apps/renderer/src/store.test.ts
      apps/web/src/components/BranchToolbar.tsx (1)

      169-309: Consider splitting the toolbar UI into smaller subcomponents. The render block is large and mixes toggle, menu, and creation form logic; extracting EnvModeToggle and BranchMenu subcomponents (kept in the same file) would reduce complexity and keep logic closer to the JSX it serves.

      Based on learnings: Applies to **/*.tsx : Extract large React components into multiple subcomponents with granular functionality. Co-locate subcomponents in the same file as the main component. Avoid hoisting callbacks too high up the component tree; prefer colocating logic close to JSX.

      @@ -0,0 +79,21 @@
      79+**7. Full flow: worktree creation from selected branch**
      80+
      81+- init → commit → create branch → create worktree → verify worktree dir exists and has correct branch
      82+
      83+**8. Full flow: thread switching simulation**
      84+
      85+- init → commit → create branch-a, branch-b → checkout a → checkout b → checkout a → verify current matches
      86+
      87+**9. Full flow: checkout conflict**
      88+
      89+- init → commit → create branch → modify file (unstaged) → checkout other branch → expect error
      90+
      91+## Verification
      92+
      93+```bash
      94+# Run the git integration tests
      95+cd apps/desktop && bun run test
      96+
      97+# Or just the git test file
      98+npx vitest run apps/desktop/src/git.test.ts
      99+```
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Use bunx instead of npx in the verification snippet. This keeps docs consistent with the Bun-only tooling standard.

      ✏️ Proposed doc fix
      -# Or just the git test file
      -npx vitest run apps/desktop/src/git.test.ts
      +# Or just the git test file
      +bunx vitest run apps/desktop/src/git.test.ts

      Based on learnings: Use Bun as the package manager. Use bunx <BIN> when executing scripts from npm packages not installed as a project dependency.

      🤖 Prompt for AI Agents
      In @.plans/git-flows-integration-tests.md around lines 93 - 99, The
      documentation uses "npx vitest run apps/desktop/src/git.test.ts" which
      contradicts the Bun-centric tooling elsewhere; update the verification snippet
      to use Bun's runner by replacing the "npx" invocation with "bunx" (so the second
      command uses bunx vitest run apps/desktop/src/git.test.ts) and keep the existing
      "cd apps/desktop && bun run test" line unchanged; ensure both examples
      consistently recommend Bun tooling.
      @@ -18,8 +18,7 @@
      1818 },
      1919 "devDependencies": {
      2020 "@types/node": "^22.10.2",
      21- "concurrently": "^9.1.2",
      2221 "electronmon": "^2.0.2",
      23- "tsup": "^8.3.5",
      22+ "tsdown": "^0.20.3",
      2423 "typescript": "^5.7.3",
      25- "vitest": "^3.0.0",
      24+ "vitest": "^4.0.0",
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      🧩 Analysis chain

      🏁 Script executed:

      #!/bin/bash
      
      # Check latest versions from npm registry
      echo "=== tsdown latest version ==="
      npm view tsdown@latest version 2>/dev/null || echo "Failed to fetch"
      
      echo ""
      echo "=== vitest latest version ==="
      npm view vitest@latest version 2>/dev/null || echo "Failed to fetch"
      
      echo ""
      echo "=== tsdown 0.20.3 details ==="
      npm view tsdown@0.20.3 2>/dev/null | head -30 || echo "Failed to fetch"
      
      echo ""
      echo "=== vitest 4.0.0 details ==="
      npm view vitest@4.0.0 2>/dev/null | head -30 || echo "Failed to fetch"

      Repository: pingdotgg/codething-mvp

      Length of output: 1591


      🏁 Script executed:

      #!/bin/bash
      
      # Check the full package.json context in apps/desktop
      echo "=== apps/desktop/package.json content ==="
      if [ -f "apps/desktop/package.json" ]; then
        cat apps/desktop/package.json
      else
        echo "File not found"
      fi

      Repository: pingdotgg/codething-mvp

      Length of output: 821


      🏁 Script executed:

      #!/bin/bash
      
      # Search for vitest and tsdown configuration in the repository
      echo "=== vitest configuration files ==="
      fd -e "vitest.config" -e "vitest.config.ts" -e "vitest.config.js" 2>/dev/null
      
      echo ""
      echo "=== tsdown usage in the codebase ==="
      rg "tsdown" --type json --type ts --type tsx -l 2>/dev/null | head -20
      
      echo ""
      echo "=== vitest usage pattern ==="
      rg "vitest" --type json --type ts --type tsx -l 2>/dev/null | head -20

      Repository: pingdotgg/codething-mvp

      Length of output: 169


      Consider updating vitest to 4.0.18 and clarify tsdown stability.

      tsdown@0.20.3 is already the latest stable release and does not represent a major update. However, vitest@4.0.0 is outdated—the current stable patch is 4.0.18. Both versions are compatible with TypeScript 5.7.3, but updating vitest to the latest patch is recommended for bug fixes and stability improvements. Note that the test script currently uses --passWithNoTests, suggesting minimal active test coverage; consider whether vitest configuration and test suite expansion are needed for this Electron desktop application.

      🤖 Prompt for AI Agents
      In `@apps/desktop/package.json` around lines 22 - 24, Update the package.json dev
      dependency for "vitest" from "^4.0.0" to "^4.0.18" to pick up recent patch
      fixes, and optionally add a comment or package.json note near "tsdown" to
      clarify it's already at the latest stable (0.20.3) if you want to signal
      stability; also review the test script that passes "--passWithNoTests" (in
      package.json scripts) and decide whether to remove or tighten that flag after
      updating vitest to encourage real test coverage.
      @@ -25,8 +25,8 @@
      2525 "@tailwindcss/vite": "^4.0.0",
      2626 "@types/react": "^19.0.0",
      2727 "@types/react-dom": "^19.0.0",
      28- "@vitejs/plugin-react": "^4.3.4",
      28+ "@vitejs/plugin-react": "^5.1.4",
      2929 "babel-plugin-react-compiler": "^19.0.0-beta-e552027-20250112",
      3030 "tailwindcss": "^4.0.0",
      3131 "typescript": "^5.7.3",
      32- "vite": "^6.0.5"
      32+ "vite": "^8.0.0-beta.12"
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      🧩 Analysis chain

      🌐 Web query:

      Vite 8 stable release date 2026

      💡 Result:

      As of February 11, 2026, Vite 8 does not have a stable (non-beta) release date announced or published yet—the project is still shipping 8.0.0 beta pre-releases. The Vite 8 beta was announced on December 3, 2025 and GitHub shows ongoing v8.0.0-beta.x releases (e.g., v8.0.0-beta.13 on February 5, 2026). [1][2]

      Sources: [1] https://vite.dev/blog/announcing-vite8-beta [2] https://github.com/vitejs/vite/releases


      Replace Vite 8 beta with a stable release version.

      Vite 8 remains in beta as of February 2026 with no announced stable release date. Using pre-release versions in production code introduces significant risk of breaking changes and instability. Update to Vite 7.x or wait for Vite 8 stable release before merging.

      🤖 Prompt for AI Agents
      In `@apps/web/package.json` around lines 28 - 32, The package.json currently pins
      "vite" to a beta pre-release ("^8.0.0-beta.12"); change the "vite" dependency
      entry to a stable 7.x release (for example "^7.21.0" or the latest 7.x), update
      the lockfile by running your package manager (npm/yarn/pnpm install), and run
      the dev/build/test commands to verify nothing breaks; edit the "vite" key in
      package.json to replace the beta version, commit the lockfile changes, and
      ensure CI passes (reference: the "vite" dependency entry in package.json).
    7. Greptile Overview

      Greptile Summary

      This PR adds git branch/worktree integration across the web UI and websocket server: threads can store branch/worktreePath, the web UI can list/checkout/create branches and create a worktree on the first send, and the server exposes git methods via new WS_METHODS routes.

      Key issues to address before merge:

      • The new server git implementation executes git via a shell with interpolated user-controlled values (branch/path), which is unsafe and should be switched to argv-based spawn('git', [...]).
      • Deleting a thread does not remove its associated worktree, so worktree-backed threads will leak directories on disk.
      • The repo test suite is currently broken in this PR state due to the Vitest config + contracts aliasing (Zod import resolves to undefined), so CI/local bun run test will fail until resolved.

      Confidence Score: 2/5

      • This PR should not be merged until test failures and the git command execution safety issues are resolved.
      • The PR introduces new server-side git execution that currently shells out with interpolated user inputs (high impact security/robustness issue), introduces a worktree lifecycle leak when threads are deleted, and leaves the repository’s Vitest test suite failing due to module resolution (zod import undefined).
      • apps/server/src/git.ts, apps/web/src/components/Sidebar.tsx, vitest.config.ts

      Important Files Changed

      FilenameOverview
      apps/server/src/git.tsAdds git command helpers invoked over websocket; currently builds shell commands by interpolating branch/path which is unsafe and should avoid shell invocation.
      apps/web/src/components/Sidebar.tsxAdds thread context menu delete action but does not clean up associated worktrees, leaving orphaned worktree directories for worktree-backed threads.
      vitest.config.tsTest config aliasing contracts to source causes vitest runtime failures (zod import undefined); repo tests fail in this PR state and need fixing.
      apps/server/src/wsServer.tsWires new git websocket methods to server-side git helpers; routing looks consistent with existing WS_METHODS switch.
      apps/web/src/components/ChatView.tsxAdds envMode + worktree creation on first send and uses worktreePath for cwd; main risk is dependent on worktree lifecycle management elsewhere.

      Sequence Diagram

      sequenceDiagram
        participant User as User
        participant Web as Web app (ChatView/BranchToolbar)
        participant WS as WS transport
        participant Server as Server (wsServer)
        participant Git as git CLI
      
        User->>Web: Select envMode + branch
        Web->>WS: git.listBranches(cwd)
        WS->>Server: WS_METHODS.gitListBranches
        Server->>Git: git branch --no-color
        Git-->>Server: branches
        Server-->>WS: {isRepo, branches}
        WS-->>Web: branches
      
        User->>Web: Send first message (worktree mode)
        Web->>WS: git.createWorktree({cwd, branch, newBranch})
        WS->>Server: WS_METHODS.gitCreateWorktree
        Server->>Git: git worktree add -b newBranch path branch
        Git-->>Server: worktree created
        Server-->>WS: {worktree:{path, branch}}
        WS-->>Web: worktree info
        Web->>WS: providers.startSession({cwd: worktreePath})
        WS->>Server: WS_METHODS.providersStartSession
        Server-->>WS: session
        WS-->>Web: session
      
        User->>Web: Right-click thread -> Delete
        Web->>WS: providers.stopSession(sessionId)
        WS->>Server: WS_METHODS.providersStopSession
        Server-->>WS: ok
        WS-->>Web: ok
        Note over Web: Current code deletes thread
        Note over Web: Missing: git.removeWorktree({cwd, path})
    8. 42 files reviewed, 3 comments [Edit Code Review Agent Settings](https://app.greptile.com/review/github) | [Greptile](https://greptile.com?utm_source=greptile_expert&utm_medium=github&utm_campaign=code_reviews)
      @@ -1,3 +1,13 @@
      1+import * as path from "node:path";
      12 import { defineConfig } from "vitest/config";
      23
      3-export default defineConfig({});
      4+export default defineConfig({
      5+ resolve: {
      6+ alias: [
      7+ {
      8+ find: /^@t3tools\/contracts$/,
      9+ replacement: path.resolve(import.meta.dirname, "./packages/contracts/src/index.ts"),
      10+ },
      11+ ],
      12+ },
      13+});

      Vitest run broken This config aliases @t3tools/contracts to the TypeScript source entry (packages/contracts/src/index.ts). With the PR’s build changes, tests now fail at runtime with TypeError: undefined is not an object (evaluating '__vite_ssr_import_0__.z.object'), which indicates the Zod import in those aliased sources isn’t being resolved correctly under Vitest’s SSR transform.

      Since bun run test is part of the repo workflow, this needs to be fixed before merge (either by adjusting the alias/optimizeDeps/ssr settings or changing how contracts are imported in tests).

    9. Additional Comments (2)

      apps/server/src/git.ts Shell command injection risk runTerminalCommand executes input.command via a shell (/bin/sh -lc on *nix, cmd.exe /c on Windows). In this PR, git commands interpolate user-controlled values (e.g. branch, newBranch, path) into a single-quoted shell string via escapeSingleQuotes. That escaping is not a safe substitute for avoiding the shell, and this becomes exploitable if any caller can pass arbitrary branch/path values (e.g. via websocket).

      Consider switching these git helpers to spawn('git', [...args], { cwd }) (no shell) and pass untrusted values as argv, not string interpolation.


      apps/web/src/components/Sidebar.tsx Worktree leak on delete When deleting a thread, the code stops the provider session and dispatches DELETE_THREAD, but never removes an associated git worktree (thread.worktreePath). Since api.git.removeWorktree exists, deleting a worktree-backed thread currently leaves an orphaned worktree directory on disk.

      This only affects threads that have worktreePath set (created via worktree mode). Those worktrees will accumulate over time unless cleanup is added here (or elsewhere as part of deletion).

    10. @@ -0,0 +171,21 @@
      171+ throw new Error(result.stderr.trim() || "git worktree add failed");
      172+ }
      173+
      174+ return {
      175+ worktree: {
      176+ path: worktreePath,
      177+ branch: input.newBranch,
      178+ },
      179+ };
      180+}
      181+
      182+export async function removeGitWorktree(input: GitRemoveWorktreeInput): Promise<void> {
      183+ const result = await runGit(["worktree", "remove", input.path], input.cwd, 15_000);
      184+
      185+ if (result.code !== 0) {
      186+ throw new Error(result.stderr.trim() || "git worktree remove failed");
      187+ }
      188+}
      189+
      190+export async function createGitBranch(input: GitCreateBranchInput): Promise<void> {
      191+ const result = await runGit(["branch", input.branch], input.cwd, 10_000);
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟢 Low

      src/git.ts:191 Suggestion: user-supplied branch names can be misparsed as options or pathspecs. Add -- before branch arguments across git calls (e.g., branch, checkout), or prefer git switch for switching branches to avoid ambiguity.

      Suggested change
      208+ const result = await runGit(["branch", "--", input.branch], input.cwd, 10_000);

      🚀 Want me to fix this? Reply ex: "fix it for me".

      🤖 Prompt for AI
      In file apps/server/src/git.ts around line 191:
      
      Suggestion: user-supplied branch names can be misparsed as options or pathspecs. Add `--` before branch arguments across `git` calls (e.g., `branch`, `checkout`), or prefer `git switch` for switching branches to avoid ambiguity.
    11. @@ -0,0 +38,21 @@
      38+
      39+ const branches = branchesQuery.data?.branches ?? [];
      40+ const isRepo = branchesQuery.data?.isRepo ?? false;
      41+
      42+ // ── Mutations ─────────────────────────────────────────────────────────
      43+
      44+ const checkoutMutation = useMutation({
      45+ mutationFn: (branch: string) => api!.git.checkout({ cwd: branchCwd!, branch }),
      46+ onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
      47+ onError: (error) =>
      48+ setThreadError(error instanceof Error ? error.message : "Failed to checkout branch."),
      49+ });
      50+
      51+ const createBranchMutation = useMutation({
      52+ mutationFn: (branch: string) =>
      53+ api!.git
      54+ .createBranch({ cwd: branchCwd!, branch })
      55+ .then(() => api!.git.checkout({ cwd: branchCwd!, branch })),
      56+ onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
      57+ onError: (error) =>
      58+ setThreadError(error instanceof Error ? error.message : "Failed to create branch."),
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟢 Low

      components/BranchToolbar.tsx:57 The chained createBranch().then(checkout) in createBranchMutation can leave git state out of sync with UI if checkout fails after branch creation succeeds. Consider separating these into distinct operations or refreshing branch state in onError to stay in sync.

      -    onError: (error) =>
      -      setThreadError(error instanceof Error ? error.message : "Failed to create branch."),
      +    onError: (error) => {
      +      queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] });
      +      setThreadError(error instanceof Error ? error.message : "Failed to create branch.");
      +    },

      🚀 Want me to fix this? Reply ex: "fix it for me".

      🤖 Prompt for AI
      In file apps/web/src/components/BranchToolbar.tsx around lines 57-58:
      
      The chained `createBranch().then(checkout)` in `createBranchMutation` can leave git state out of sync with UI if checkout fails after branch creation succeeds. Consider separating these into distinct operations or refreshing branch state in `onError` to stay in sync.
      @@ -0,0 +56,21 @@
      56+ onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
      57+ onError: (error) =>
      58+ setThreadError(error instanceof Error ? error.message : "Failed to create branch."),
      59+ });
      60+
      61+ const initMutation = useMutation({
      62+ mutationFn: () => api!.git.init({ cwd: branchCwd! }),
      63+ onSuccess: () => {
      64+ setThreadError(null);
      65+ queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] });
      66+ },
      67+ onError: (error) =>
      68+ setThreadError(error instanceof Error ? error.message : "Failed to initialize git repo."),
      69+ });
      70+
      71+ // ── Effects ───────────────────────────────────────────────────────────
      72+
      73+ // Keep thread branch synced to git current branch for local threads.
      74+ const queryBranches = branchesQuery.data?.branches;
      75+ useEffect(() => {
      76+ if (!activeThreadId || activeWorktreePath) return;
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium

      components/BranchToolbar.tsx:75 The useEffect at line 75 reverts the thread branch to match Git HEAD when activeWorktreePath is null. However, the new worktree mode (lines 118-123) also has null activeWorktreePath but intentionally sets a different branch without checkout. Consider adding envMode !== "worktree" to the effect's early return to prevent it from overwriting the user's base branch selection.

      -  useEffect(() => {
      -    if (!activeThreadId || activeWorktreePath) return;
      +  useEffect(() => {
      +    if (!activeThreadId || activeWorktreePath || envMode === "worktree") return;

      🚀 Want me to fix this? Reply ex: "fix it for me".

      🤖 Prompt for AI
      In file apps/web/src/components/BranchToolbar.tsx around lines 75-76:
      
      The `useEffect` at line 75 reverts the thread branch to match Git HEAD when `activeWorktreePath` is null. However, the new worktree mode (lines 118-123) also has null `activeWorktreePath` but intentionally sets a different branch without checkout. Consider adding `envMode !== "worktree"` to the effect's early return to prevent it from overwriting the user's base branch selection.
    12. Actionable comments posted: 3

      🤖 Fix all issues with AI agents
      In `@apps/desktop/turbo.jsonc`:
      - Around line 5-23: The JSONC contains trailing commas after object entries
      which cause parse errors; remove the trailing commas in the "build", "dev",
      "start", and "smoke-test" task objects (specifically the commas following the
      "outputs", "persistent", "persistent"/"cache", and "outputs" lines) so each
      object ends with a closing brace without a trailing comma and the file parses
      correctly.
      
      In `@apps/server/turbo.jsonc`:
      - Around line 5-10: The JSONC has trailing commas after the "persistent" and
      closing object entries which causes parse errors; edit the "start" task object
      in turbo.jsonc (the "start" key and its properties "dependsOn", "cache",
      "persistent") and remove the trailing commas after "persistent": true and after
      the inner object so the JSONC object closes without extra commas, then re-run
      lint/CI.
      
      In `@apps/web/turbo.jsonc`:
      - Around line 5-8: The JSONC parse errors are caused by trailing commas in the
      "dev" object; remove the trailing comma after "persistent": true and any
      trailing comma before the closing brace of the outer object so the "dev" object
      and enclosing object are valid JSONC (i.e., edit the "dev" block to eliminate
      the comma after the last property and the comma before the closing brace).
      🧹 Nitpick comments (1)
      apps/web/src/components/BranchToolbar.tsx (1)

      44-69: Consider adding error recovery for partial branch creation failure.

      In createBranchMutation, if createBranch succeeds but the chained checkout fails, the branch exists but the UI shows an error. This is acceptable UX since the user can manually select the branch, but consider whether the onError message should indicate the branch was created.

      💡 Optional: More specific error message
         const createBranchMutation = useMutation({
           mutationFn: (branch: string) =>
             api!.git
               .createBranch({ cwd: branchCwd!, branch })
               .then(() => api!.git.checkout({ cwd: branchCwd!, branch })),
           onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
           onError: (error) =>
      -      setThreadError(error instanceof Error ? error.message : "Failed to create branch."),
      +      setThreadError(error instanceof Error ? error.message : "Failed to create or checkout branch."),
         });
      @@ -0,0 +3,21 @@
      3+ "extends": ["//"],
      4+ "tasks": {
      5+ "build": {
      6+ "dependsOn": ["^build"],
      7+ "outputs": ["dist-electron/**"],
      8+ },
      9+ "dev": {
      10+ "dependsOn": ["t3#build"],
      11+ "persistent": true,
      12+ },
      13+ "start": {
      14+ "dependsOn": ["build", "@t3tools/web#build", "t3#build"],
      15+ "cache": false,
      16+ "persistent": true,
      17+ },
      18+ "smoke-test": {
      19+ "dependsOn": ["build", "@t3tools/web#build", "t3#build"],
      20+ "cache": false,
      21+ "outputs": [],
      22+ },
      23+ },
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      Remove trailing commas to fix JSONC parse errors.

      Biome reports parse errors at the closing braces; the trailing commas before } are the likely cause and will break lint/CI.

      ✅ Suggested fix
         "tasks": {
           "build": {
             "dependsOn": ["^build"],
      -      "outputs": ["dist-electron/**"],
      +      "outputs": ["dist-electron/**"]
           },
           "dev": {
             "dependsOn": ["t3#build"],
      -      "persistent": true,
      +      "persistent": true
           },
           "start": {
             "dependsOn": ["build", "@t3tools/web#build", "t3#build"],
             "cache": false,
      -      "persistent": true,
      +      "persistent": true
           },
           "smoke-test": {
             "dependsOn": ["build", "@t3tools/web#build", "t3#build"],
             "cache": false,
      -      "outputs": [],
      -    },
      -  },
      +      "outputs": []
      +    }
      +  }
       }
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      5- "build": {
      6- "dependsOn": ["^build"],
      7- "outputs": ["dist-electron/**"],
      8- },
      9- "dev": {
      10- "dependsOn": ["t3#build"],
      11- "persistent": true,
      12- },
      13- "start": {
      14- "dependsOn": ["build", "@t3tools/web#build", "t3#build"],
      15- "cache": false,
      16- "persistent": true,
      17- },
      18- "smoke-test": {
      19- "dependsOn": ["build", "@t3tools/web#build", "t3#build"],
      20- "cache": false,
      21- "outputs": [],
      22- },
      23- },
      5+ "build": {
      6+ "dependsOn": ["^build"],
      7+ "outputs": ["dist-electron/**"]
      8+ },
      9+ "dev": {
      10+ "dependsOn": ["t3#build"],
      11+ "persistent": true
      12+ },
      13+ "start": {
      14+ "dependsOn": ["build", "@t3tools/web#build", "t3#build"],
      15+ "cache": false,
      16+ "persistent": true
      17+ },
      18+ "smoke-test": {
      19+ "dependsOn": ["build", "@t3tools/web#build", "t3#build"],
      20+ "cache": false,
      21+ "outputs": []
      22+ }
      23+ }
      🧰 Tools
      🪛 Biome (2.3.14)

      [error] 8-8: Expected a property but instead found '}'.

      Expected a property here.

      (parse)


      [error] 12-12: Expected a property but instead found '}'.

      Expected a property here.

      (parse)


      [error] 17-17: Expected a property but instead found '}'.

      Expected a property here.

      (parse)


      [error] 22-22: Expected a property but instead found '}'.

      Expected a property here.

      (parse)


      [error] 23-23: Expected a property but instead found '}'.

      Expected a property here.

      (parse)

      🤖 Prompt for AI Agents
      In `@apps/desktop/turbo.jsonc` around lines 5 - 23, The JSONC contains trailing
      commas after object entries which cause parse errors; remove the trailing commas
      in the "build", "dev", "start", and "smoke-test" task objects (specifically the
      commas following the "outputs", "persistent", "persistent"/"cache", and
      "outputs" lines) so each object ends with a closing brace without a trailing
      comma and the file parses correctly.
      @@ -0,0 +1,10 @@
      1+{
      2+ "$schema": "https://turbo.build/schema.json",
      3+ "extends": ["//"],
      4+ "tasks": {
      5+ "start": {
      6+ "dependsOn": ["build"],
      7+ "cache": false,
      8+ "persistent": true,
      9+ },
      10+ },
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      Remove trailing commas to fix JSONC parse errors.

      Biome reports parse errors at the closing braces; the trailing commas before } are the likely cause and will break lint/CI.

      ✅ Suggested fix
         "tasks": {
           "start": {
             "dependsOn": ["build"],
             "cache": false,
      -      "persistent": true,
      -    },
      -  },
      +      "persistent": true
      +    }
      +  }
       }
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      5- "start": {
      6- "dependsOn": ["build"],
      7- "cache": false,
      8- "persistent": true,
      9- },
      10- },
      5+ "start": {
      6+ "dependsOn": ["build"],
      7+ "cache": false,
      8+ "persistent": true
      9+ }
      10+ }
      🧰 Tools
      🪛 Biome (2.3.14)

      [error] 9-9: Expected a property but instead found '}'.

      Expected a property here.

      (parse)


      [error] 10-10: Expected a property but instead found '}'.

      Expected a property here.

      (parse)

      🤖 Prompt for AI Agents
      In `@apps/server/turbo.jsonc` around lines 5 - 10, The JSONC has trailing commas
      after the "persistent" and closing object entries which causes parse errors;
      edit the "start" task object in turbo.jsonc (the "start" key and its properties
      "dependsOn", "cache", "persistent") and remove the trailing commas after
      "persistent": true and after the inner object so the JSONC object closes without
      extra commas, then re-run lint/CI.
      @@ -0,0 +1,8 @@
      1+{
      2+ "$schema": "https://turbo.build/schema.json",
      3+ "extends": ["//"],
      4+ "tasks": {
      5+ "dev": {
      6+ "persistent": true,
      7+ },
      8+ },
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟠 Major

      Remove trailing commas to fix JSONC parse errors.

      Biome reports parse errors at the closing braces; the trailing commas before } are the likely cause and will break lint/CI.

      ✅ Suggested fix
         "tasks": {
           "dev": {
      -      "persistent": true,
      -    },
      -  },
      +      "persistent": true
      +    }
      +  }
       }
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      5- "dev": {
      6- "persistent": true,
      7- },
      8- },
      5+ "dev": {
      6+ "persistent": true
      7+ }
      8+ }
      🧰 Tools
      🪛 Biome (2.3.14)

      [error] 7-7: Expected a property but instead found '}'.

      Expected a property here.

      (parse)


      [error] 8-8: Expected a property but instead found '}'.

      Expected a property here.

      (parse)

      🤖 Prompt for AI Agents
      In `@apps/web/turbo.jsonc` around lines 5 - 8, The JSONC parse errors are caused
      by trailing commas in the "dev" object; remove the trailing comma after
      "persistent": true and any trailing comma before the closing brace of the outer
      object so the "dev" object and enclosing object are valid JSONC (i.e., edit the
      "dev" block to eliminate the comma after the last property and the comma before
      the closing brace).
    13. Actionable comments posted: 2

      🤖 Fix all issues with AI agents
      In `@apps/web/src/components/BranchToolbar.tsx`:
      - Around line 51-59: The chained mutation in createBranchMutation (mutationFn
      calling api.git.createBranch then api.git.checkout) can yield partial success
      where createBranch succeeds but checkout fails, causing an incorrect "Failed to
      create branch" error and no cache refresh; change the flow so createBranch is a
      standalone operation that calls queryClient.invalidateQueries({ queryKey:
      ["git", "branches", branchCwd] }) on its onSuccess, then perform checkout as a
      separate step (either a follow-up mutation or a try/catch after createBranch)
      and handle checkout errors distinctly by calling setThreadError with a message
      like "Branch created but checkout failed: <checkout error>". Make sure to
      reference the existing createBranchMutation, api!.git.createBranch,
      api!.git.checkout, onSuccess, onError, queryClient.invalidateQueries, and
      setThreadError when making these changes.
      
      In `@apps/web/vite.config.ts`:
      - Line 5: The dev port is read from process.env.PORT (const port) while the
      desktop dev script expects ELECTRON_RENDERER_PORT, causing mismatches; update
      the port resolution in the vite config (where const port is defined) to prefer
      ELECTRON_RENDERER_PORT first and then fall back to PORT and then 5173 (or switch
      both web and desktop scripts to a single agreed env name), and ensure any
      related npm scripts or docs reference the chosen env variable so Electron can
      always connect to the web dev server.
      🧹 Nitpick comments (1)
      apps/web/src/components/BranchToolbar.tsx (1)

      194-223: Consider adding ARIA attributes for dropdown accessibility.

      The branch picker dropdown button lacks aria-expanded and aria-haspopup attributes, which screen readers rely on to announce dropdown state. This is a minor accessibility improvement.

      ♿ Suggested ARIA attributes
       <button
         type="button"
         className="inline-flex items-center gap-1.5 rounded-lg px-2 py-1 text-[12px] text-muted-foreground/60 transition-colors duration-150 hover:bg-accent/50 hover:text-muted-foreground/80"
         onClick={() => setIsBranchMenuOpen((open) => !open)}
         disabled={branchesQuery.isLoading}
      +  aria-haspopup="listbox"
      +  aria-expanded={isBranchMenuOpen}
       >
      @@ -0,0 +39,21 @@
      39+ const branches = branchesQuery.data?.branches ?? [];
      40+ const isRepo = branchesQuery.data?.isRepo ?? false;
      41+
      42+ // ── Mutations ─────────────────────────────────────────────────────────
      43+
      44+ const checkoutMutation = useMutation({
      45+ mutationFn: (branch: string) => api!.git.checkout({ cwd: branchCwd!, branch }),
      46+ onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
      47+ onError: (error) =>
      48+ setThreadError(error instanceof Error ? error.message : "Failed to checkout branch."),
      49+ });
      50+
      51+ const createBranchMutation = useMutation({
      52+ mutationFn: (branch: string) =>
      53+ api!.git
      54+ .createBranch({ cwd: branchCwd!, branch })
      55+ .then(() => api!.git.checkout({ cwd: branchCwd!, branch })),
      56+ onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
      57+ onError: (error) =>
      58+ setThreadError(error instanceof Error ? error.message : "Failed to create branch."),
      59+ });
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      Chained mutation may leave inconsistent state on partial failure.

      If createBranch succeeds but checkout fails (e.g., due to uncommitted changes), the error message incorrectly says "Failed to create branch" even though the branch was created. Additionally, since onSuccess won't fire, the branch list won't refresh, hiding the newly created branch from the user.

      Consider separating into two distinct operations or providing accurate error messaging for partial success scenarios.

      🔧 Suggested fix: separate operations or improve error handling
       const createBranchMutation = useMutation({
      -  mutationFn: (branch: string) =>
      -    api!.git
      -      .createBranch({ cwd: branchCwd!, branch })
      -      .then(() => api!.git.checkout({ cwd: branchCwd!, branch })),
      -  onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
      -  onError: (error) =>
      -    setThreadError(error instanceof Error ? error.message : "Failed to create branch."),
      +  mutationFn: async (branch: string) => {
      +    await api!.git.createBranch({ cwd: branchCwd!, branch });
      +    try {
      +      await api!.git.checkout({ cwd: branchCwd!, branch });
      +    } catch (checkoutError) {
      +      // Branch was created but checkout failed - still invalidate to show new branch
      +      await queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] });
      +      throw new Error(
      +        `Branch created but checkout failed: ${checkoutError instanceof Error ? checkoutError.message : "Unknown error"}`
      +      );
      +    }
      +  },
      +  onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
      +  onError: (error) =>
      +    setThreadError(error instanceof Error ? error.message : "Failed to create branch."),
       });
      📝 Committable suggestion

      ‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

      Suggested change
      53+ const createBranchMutation = useMutation({
      54+ mutationFn: async (branch: string) => {
      55+ await api!.git.createBranch({ cwd: branchCwd!, branch });
      56+ try {
      57+ await api!.git.checkout({ cwd: branchCwd!, branch });
      58+ } catch (checkoutError) {
      59+ // Branch was created but checkout failed - still invalidate to show new branch
      60+ await queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] });
      61+ throw new Error(
      62+ `Branch created but checkout failed: ${checkoutError instanceof Error ? checkoutError.message : "Unknown error"}`
      63+ );
      64+ }
      65+ },
      66+ onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
      67+ onError: (error) =>
      68+ setThreadError(error instanceof Error ? error.message : "Failed to create branch."),
      69+ });
      🤖 Prompt for AI Agents
      In `@apps/web/src/components/BranchToolbar.tsx` around lines 51 - 59, The chained
      mutation in createBranchMutation (mutationFn calling api.git.createBranch then
      api.git.checkout) can yield partial success where createBranch succeeds but
      checkout fails, causing an incorrect "Failed to create branch" error and no
      cache refresh; change the flow so createBranch is a standalone operation that
      calls queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd]
      }) on its onSuccess, then perform checkout as a separate step (either a
      follow-up mutation or a try/catch after createBranch) and handle checkout errors
      distinctly by calling setThreadError with a message like "Branch created but
      checkout failed: <checkout error>". Make sure to reference the existing
      createBranchMutation, api!.git.createBranch, api!.git.checkout, onSuccess,
      onError, queryClient.invalidateQueries, and setThreadError when making these
      changes.
      @@ -2,3 +2,4 @@
      22 import react from "@vitejs/plugin-react";
      33 import { defineConfig } from "vite";
      44
      5+const port = Number(process.env.PORT ?? 5173);
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      ⚠️ Potential issue | 🟡 Minor

      🧩 Analysis chain

      🏁 Script executed:

      #!/bin/bash
      # Search for PORT and ELECTRON_RENDERER_PORT usage patterns
      
      echo "=== Web app vite.config.ts port usage ==="
      grep -n "PORT\|port" apps/web/vite.config.ts | head -20
      
      echo ""
      echo "=== Desktop dev script ELECTRON_RENDERER_PORT usage ==="
      grep -n "ELECTRON_RENDERER_PORT\|port" apps/desktop/scripts/dev-electron.mjs | head -20
      
      echo ""
      echo "=== Search all usages of PORT env var ==="
      rg -n "process\.env\.PORT" --type ts --type js -A 2 -B 2
      
      echo ""
      echo "=== Search all usages of ELECTRON_RENDERER_PORT ==="
      rg -n "ELECTRON_RENDERER_PORT" --type ts --type js --type mjs -A 2 -B 2
      
      echo ""
      echo "=== Check for any .env files or examples ==="
      fd "\.env" . -type f

      Repository: pingdotgg/codething-mvp

      Length of output: 1296


      🏁 Script executed:

      #!/bin/bash
      # Check for coordinated dev scripts and documentation
      
      echo "=== Root package.json dev scripts ==="
      cat package.json | grep -A 20 '"scripts"' | head -30
      
      echo ""
      echo "=== Check for development documentation ==="
      find . -maxdepth 2 -name "*.md" -type f | xargs grep -l "PORT\|port\|dev" 2>/dev/null | head -5
      
      echo ""
      echo "=== List all dev-related scripts in monorepo ==="
      fd "package.json" | xargs grep -l "\"dev\"" 2>/dev/null
      
      echo ""
      echo "=== Check apps/web package.json ==="
      cat apps/web/package.json | grep -A 5 '"scripts"' 2>/dev/null || echo "Not found"
      
      echo ""
      echo "=== Check apps/desktop package.json ==="
      cat apps/desktop/package.json | grep -A 5 '"scripts"' 2>/dev/null || echo "Not found"

      Repository: pingdotgg/codething-mvp

      Length of output: 2056


      Align port environment variable names between web and desktop dev scripts.

      The web app uses PORT while the desktop dev script uses ELECTRON_RENDERER_PORT. Both default to 5173, so they match in typical usage, but if someone sets only one to a custom value, the Electron app will fail to connect to the dev server. Either use a single env var name across both or document the relationship clearly.

      🤖 Prompt for AI Agents
      In `@apps/web/vite.config.ts` at line 5, The dev port is read from
      process.env.PORT (const port) while the desktop dev script expects
      ELECTRON_RENDERER_PORT, causing mismatches; update the port resolution in the
      vite config (where const port is defined) to prefer ELECTRON_RENDERER_PORT first
      and then fall back to PORT and then 5173 (or switch both web and desktop scripts
      to a single agreed env name), and ensure any related npm scripts or docs
      reference the chosen env variable so Electron can always connect to the web dev
      server.
    14. juliusmarminge merged this16b81f7

    15. juliusmarminge closed this

    16. Review: Git integration — branch picker + worktrees

      Solid feature with strong test coverage (423 lines of real-git integration tests) and thoughtful edge-case handling — stale worktree filtering, worktree redirects, session restart on cwd change. A few issues worth addressing:

      High

      • as never bypasses all input validation — the zod schemas in packages/contracts/src/git.ts are well-defined but every git WS handler casts request.params as never, skipping validation entirely. Unvalidated branch names with git metacharacters (e.g. leading -, --) could cause argument injection. Should use the schema .parse() methods.

      Medium

      • Worktree path collision — path.basename(input.cwd) produces colliding paths for repos with the same basename (e.g. ~/work/t3code and ~/play/t3code).
      • Orphaned worktrees on failure — worktree is created and dispatched to store, but if ensureSession/sendTurn then fails, the worktree directory is left on disk with no cleanup.
      • isRepo logic inverted from stated intent — undefined ?? !isLoading evaluates to false on first load, contradicting the comment that says it defaults to true.

      Low / Hygiene

      • codething/ hardcoded prefix for generated worktree branch names.
      • branchCwd! non-null assertions in mutations — query is gated by enabled: !!branchCwd but mutations don't re-check.
      • runTerminalCommand (general-purpose shell executor) left in git.ts — only used by test helper now, latent footgun if reused.
      • 4 .plans/ markdown files (391 lines) are working artifacts — should be gitignored.
      • Build/tooling churn (tsup→tsdown, vitest 3→4, turbo task graph iterations) mixed into feature PR — would be cleaner as separate PRs.

      Strengths

      • Shell-free runGit (argv spawn) — eliminates quoting/injection surface.
      • SET_THREAD_BRANCH nulls session on cwd change + stops running session first.
      • Stale worktree filtering via existsSync.
      • Persistence schema uses .optional() with ?? null defaults — backward-compatible migration.
      • Theme-aware semantic tokens replace hardcoded dark-mode colors.
      @@ -304,3 +312,20 @@
      304312 return undefined;
      305313 }
      306314
      315+ case WS_METHODS.gitListBranches:
      316+ return listGitBranches(request.params as never);
      317+
      318+ case WS_METHODS.gitCreateWorktree:
      319+ return createGitWorktree(request.params as never);
      320+
      321+ case WS_METHODS.gitRemoveWorktree:
      322+ return removeGitWorktree(request.params as never);
      323+
      324+ case WS_METHODS.gitCreateBranch:
      325+ return createGitBranch(request.params as never);
      326+
      327+ case WS_METHODS.gitCheckout:
      328+ return checkoutGitBranch(request.params as never);
      329+
      330+ case WS_METHODS.gitInit:
      331+ return initGitRepo(request.params as never);
      patroza(GitHub profile, opens in a new tab)commented

      High: Schemas defined but never enforced

      The zod schemas in packages/contracts/src/git.ts (gitListBranchesInputSchema, gitCreateWorktreeInputSchema, etc.) are well-defined but purely decorative — every git handler casts request.params as never, skipping validation entirely.

      While the shell-free runGit prevents shell injection, unvalidated branch names with git metacharacters (e.g. --, leading -, ..) could cause argument injection into git commands. For example, a branch name like --upload-pack passed to git checkout could trigger unexpected behavior.

      Should be:

      case WS_METHODS.gitListBranches:
        return listGitBranches(gitListBranchesInputSchema.parse(request.params));
      case WS_METHODS.gitCreateWorktree:
        return createGitWorktree(gitCreateWorktreeInputSchema.parse(request.params));
      // ... etc.

      The contracts package exists specifically for this — the schemas should be the enforcement boundary.

      @@ -0,0 +159,21 @@
      159+ current: line.startsWith("* "),
      160+ isDefault: name === defaultBranch,
      161+ worktreePath: worktreeMap.get(name) ?? null,
      162+ };
      163+ })
      164+ .toSorted((a, b) => {
      165+ if (a.current !== b.current) return a.current ? -1 : 1;
      166+ if (a.isDefault !== b.isDefault) return a.isDefault ? -1 : 1;
      167+ return a.name.localeCompare(b.name);
      168+ });
      169+
      170+ return { branches, isRepo: true };
      171+}
      172+
      173+export async function createGitWorktree(
      174+ input: GitCreateWorktreeInput,
      175+): Promise<GitCreateWorktreeResult> {
      176+ const sanitizedBranch = input.newBranch.replace(/\//g, "-");
      177+ const repoName = path.basename(input.cwd);
      178+ const worktreePath =
      179+ input.path ?? path.join(os.homedir(), ".t3", "worktrees", repoName, sanitizedBranch);

      Medium: Worktree path collision on identical repo basenames

      path.basename(input.cwd) extracts only the final directory component. Two repos with the same basename (e.g. ~/work/t3code and ~/projects/t3code) produce colliding worktree paths:

      ~/.t3/worktrees/t3code/<branch>  // both repos write here

      The second repo's git worktree add would fail with "already exists" or silently reuse the first repo's worktree.

      Consider incorporating a short hash of the full cwd:

      const repoHash = crypto.createHash('sha1').update(input.cwd).digest('hex').slice(0, 8);
      const worktreePath = input.path ?? path.join(os.homedir(), '.t3', 'worktrees', `${repoName}-${repoHash}`, sanitizedBranch);
      @@ -0,0 +37,21 @@
      37+ }, 1_000).unref();
      38+ }, timeoutMs);
      39+
      40+ child.stdout?.on("data", (chunk: Buffer) => {
      41+ stdout += chunk.toString();
      42+ });
      43+ child.stderr?.on("data", (chunk: Buffer) => {
      44+ stderr += chunk.toString();
      45+ });
      46+ child.on("error", (error) => {
      47+ clearTimeout(timeout);
      48+ reject(error);
      49+ });
      50+ child.on("close", (code, signal) => {
      51+ clearTimeout(timeout);
      52+ resolve({ stdout, stderr, code: code ?? null, signal: signal ?? null, timedOut });
      53+ });
      54+ });
      55+}
      56+
      57+export async function runTerminalCommand(

      Low: runTerminalCommand left in git.ts

      This general-purpose shell executor (spawn with -lc / /d /s /c) sits alongside the shell-free runGit. It's only used by the test helper now (git.test.ts). Shipping a shell-execution function in the git module is a latent footgun — it could easily be reused for user-facing operations, reintroducing the shell quoting/injection surface that runGit was specifically created to eliminate.

      Consider moving it to a separate shell.ts module or making it test-only (not exported from the production module).

      @@ -0,0 +22,21 @@
      22+ const branchMenuRef = useRef<HTMLDivElement>(null);
      23+
      24+ const activeThread = state.threads.find((thread) => thread.id === state.activeThreadId);
      25+ const activeProject = state.projects.find((project) => project.id === activeThread?.projectId);
      26+ const activeThreadId = activeThread?.id;
      27+ const activeThreadBranch = activeThread?.branch ?? null;
      28+ const activeWorktreePath = activeThread?.worktreePath ?? null;
      29+ const branchCwd = activeWorktreePath ?? activeProject?.cwd;
      30+
      31+ // ── Queries ───────────────────────────────────────────────────────────
      32+
      33+ const branchesQuery = useQuery({
      34+ queryKey: ["git", "branches", branchCwd],
      35+ queryFn: () => api!.git.listBranches({ cwd: branchCwd! }),
      36+ enabled: !!api && !!branchCwd,
      37+ });
      38+
      39+ const branches = branchesQuery.data?.branches ?? [];
      40+ // Default to true while loading — showing "Initialize git" during a fetch is wrong,
      41+ // and worktrees are inherently git repos.
      42+ const isRepo = branchesQuery.data?.isRepo ?? !branchesQuery.isLoading;

      Medium: isRepo logic is inverted from the stated intent

      The comment says "Default to true while loading," but the expression doesn't do that:

      const isRepo = branchesQuery.data?.isRepo ?? !branchesQuery.isLoading;

      On a cold first load, data is undefined and isLoading is true, so undefined ?? false = false — the "Initialize git" button flashes even for valid repos.

      The intent (per the comment) should be:

      const isRepo = branchesQuery.data?.isRepo ?? true;

      Additionally, the "Initialize git" button is not disabled during query loading (disabled={!branchCwd || initMutation.isPending}), so a user could click git init on an already-initialized repo during the brief loading window.

      @@ -0,0 +44,21 @@
      44+ // ── Mutations ─────────────────────────────────────────────────────────
      45+
      46+ const checkoutMutation = useMutation({
      47+ mutationFn: (branch: string) => api!.git.checkout({ cwd: branchCwd!, branch }),
      48+ onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
      49+ onError: (error) =>
      50+ setThreadError(error instanceof Error ? error.message : "Failed to checkout branch."),
      51+ });
      52+
      53+ const createBranchMutation = useMutation({
      54+ mutationFn: (branch: string) =>
      55+ api!.git
      56+ .createBranch({ cwd: branchCwd!, branch })
      57+ .then(() => api!.git.checkout({ cwd: branchCwd!, branch })),
      58+ onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
      59+ onError: (error) =>
      60+ setThreadError(error instanceof Error ? error.message : "Failed to create branch."),
      61+ });
      62+
      63+ const initMutation = useMutation({
      64+ mutationFn: () => api!.git.init({ cwd: branchCwd! }),

      Low: branchCwd! non-null assertions in mutations

      The query is gated with enabled: !!api && !!branchCwd, but the mutations all use branchCwd! without re-checking. If activeProject?.cwd is undefined (e.g. project not yet loaded), the mutation sends cwd: undefined to the server, which would fail or behave unexpectedly.

      Consider guarding each mutation with an early return:

      mutationFn: (branch: string) => {
        if (!branchCwd) throw new Error('No cwd');
        return api!.git.checkout({ cwd: branchCwd, branch });
      },
      @@ -448,0 +450,21 @@
      450+ const result = await api.git.createWorktree({
      451+ cwd: activeProject.cwd,
      452+ branch: activeThread.branch,
      453+ newBranch,
      454+ });
      455+ sessionCwd = result.worktree.path;
      456+ dispatch({
      457+ type: "SET_THREAD_BRANCH",
      458+ threadId: activeThread.id,
      459+ branch: result.worktree.branch,
      460+ worktreePath: result.worktree.path,
      461+ });
      462+ } catch (err) {
      463+ dispatch({
      464+ type: "SET_ERROR",
      465+ threadId: activeThread.id,
      466+ error: err instanceof Error ? err.message : "Failed to create worktree",
      467+ });
      468+ return;
      469+ }
      470+ }

      Medium: Orphaned worktree on downstream failure

      The worktree is created and dispatched to the store here, but if ensureSession (line 496) or sendTurn subsequently fails, the worktree directory remains on disk with no cleanup. There's a removeGitWorktree IPC method but it's never called on any error path.

      Consider wrapping the entire send flow (worktree creation → ensureSession → sendTurn) in a try/catch that rolls back the worktree:

      } catch (err) {
        if (sessionCwd) {
          await api.git.removeWorktree({ cwd: activeProject.cwd, path: sessionCwd }).catch(() => {});
        }
        dispatch({ type: 'SET_ERROR', ... });
      }

      Also — this ~30 lines of branch-naming + worktree creation + dispatch + error handling is embedded directly in handleSend. Per AGENTS.md's maintainability guidance, it should be extracted into a prepareWorktreeSession() helper.

      @@ -446,3 +435,15 @@
      446435 if (!api || !activeThread || isSending || isConnecting) return;
      447436 const trimmed = prompt.trim();
      448437 if (!trimmed) return;
      438+ if (!activeProject) return;
      439+
      440+ // On first message: lock in branch + create worktree if needed.
      441+ let sessionCwd: string | undefined;
      442+ if (
      443+ activeThread.messages.length === 0 &&
      444+ activeThread.branch &&
      445+ envMode === "worktree" &&
      446+ !activeThread.worktreePath
      447+ ) {
      448+ try {
      449+ const newBranch = `codething/${crypto.randomUUID().slice(0, 8)}`;

      Low: Hardcoded codething/ prefix

      This is the legacy project name. It should be a named constant or configurable, especially since the rest of the codebase uses "t3" branding (~/.t3/worktrees/...).

    17. Claude re-review: approve (prior findings fixed). On-host ss-host-1: i18n-check pass, 23 files/513 tests pass, typecheck 4/4, build 2/2. Blacksmith CI unavailable on fork; merging on review + on-host verification.

Sign in to join this conversation.