Skip to content

fix: Consecutive Cmd+N no longer spawns agent in wrong project#147

Merged
2witstudios merged 2 commits intomainfrom
pu/cmdn-jumping
Mar 14, 2026
Merged

fix: Consecutive Cmd+N no longer spawns agent in wrong project#147
2witstudios merged 2 commits intomainfrom
pu/cmdn-jumping

Conversation

@2witstudios
Copy link
Owner

@2witstudios 2witstudios commented Mar 13, 2026

Summary

  • Guard updateActiveProject against nil lookups so activeProjectRoot is preserved when the entity isn't in the data model yet (daemon responds before manifest refresh)
  • Set activeProjectRoot explicitly in createWorktree/createAgent callbacks since the ProjectState already knows the correct project root
  • Guard callbacks against removed projects — only set activeProjectRoot when projectState(forRoot:) confirms the project still exists
  • Add 2 regression tests for unknown worktree/agent ID preservation

Test plan

  • Run ActiveProjectRoutingTests in Xcode — all 10 tests pass (8 existing + 2 new)
  • Manual: open two projects, Cmd+N → create worktree in project 2, immediately Cmd+N → spawn agent — agent should appear in project 2's worktree (not project 1)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Prevented active project from being cleared when operations reference unknown agents or worktrees.
    • Ensure active project is set reliably when creating agents or worktrees.
  • Tests

    • Added tests verifying unknown agent/worktree IDs do not change the active project.

Guard updateActiveProject against nil lookups so activeProjectRoot is
preserved when the worktree/agent isn't in the data model yet (daemon
responds before manifest refresh). Also set activeProjectRoot explicitly
in createWorktree/createAgent callbacks since the ProjectState already
knows the correct project root.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e47ae2db-e4fd-4d51-8283-0a0e0ab440f1

📥 Commits

Reviewing files that changed from the base of the PR and between adfdcc6 and 76319f4.

📒 Files selected for processing (1)
  • apps/purepoint-macos/purepoint-macos/State/ProjectState.swift

📝 Walkthrough

Walkthrough

Guarded activeProjectRoot assignments: updateActiveProject(for:) only assigns when a matching project is found; createAgent and createWorktree now set appState?.activeProjectRoot to the current projectRoot when daemon responses succeed. Tests added to ensure unknown agent/worktree IDs do not clear activeProjectRoot.

Changes

Cohort / File(s) Summary
AppState State Management
apps/purepoint-macos/purepoint-macos/State/AppState.swift
updateActiveProject(for:) now guards assignments when resolving by agent/terminal or worktree so activeProjectRoot is only changed if a non-nil project lookup returns a root.
ProjectState Daemon Response Handling
apps/purepoint-macos/purepoint-macos/State/ProjectState.swift
createAgent and createWorktree handlers now set appState?.activeProjectRoot = projectRoot on successful daemon results (.spawnResult, .createWorktreeResult) in addition to updating pending select IDs.
Active Project Routing Tests
apps/purepoint-macos/purepoint-macosTests/ActiveProjectRoutingTests.swift
Added tests verifying unknown worktree and agent IDs do not alter an existing activeProjectRoot.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

🐰 I hopped through branches, softly in the night,
Kept the active root steady, snug and tight,
When agents bloom or worktrees take their place,
I leave the root untouched — a quiet, safe space. 🌿✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary fix: preventing agent spawning in the wrong project when using consecutive Cmd+N. It directly matches the core issue addressed across the code changes.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch pu/cmdn-jumping
📝 Coding Plan
  • Generate coding plan for human review comments

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

@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: adfdcc6ccb

ℹ️ 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".

If a project is removed between the create request and daemon response,
unconditionally setting activeProjectRoot would leave a stale root that
can't be resolved, breaking Cmd+N until manual reselection. Only set
activeProjectRoot when the project still exists in appState.projects.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Owner Author

@2witstudios 2witstudios left a comment

Choose a reason for hiding this comment

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

Re: P2 comment on stale project root in spawn callback — Agreed and addressed in 76319f4. Both createAgent and createWorktree callbacks now guard with if self.appState?.projectState(forRoot: self.projectRoot) \!= nil before setting activeProjectRoot, so a removed project can't be resurrected by a late daemon response.

@2witstudios 2witstudios merged commit 6f17649 into main Mar 14, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant