The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(new-harness): a browsed worktree stays the project (#1344)
0xken15f8771c81c465
diff --git a/desktop/design/new-harness-entry-rules.md b/desktop/design/new-harness-entry-rules.mdindex 569eed350..16e2f3cb0 100644--- a/desktop/design/new-harness-entry-rules.md+++ b/desktop/design/new-harness-entry-rules.md@@ -175,10 +175,12 @@ Detached checkouts do not invent a branch name; full path and checkout detailsremain available through the context controls. Worktree implementation foldersare not everyday project labels. See [workspace-status-bar.md](workspace-status-bar.md)for context actions and compact layout. A folder inside a linked-worktree (the focused pane's, or one typed or browsed) shows as the same folder-in the repository's main checkout, so Cmd-N from a worktree pane starts beside-it rather than inside it. Worktrees Start made are never offered as recent-projects.+worktree that the form inherits (the focused pane's, a draft's, a recent one)+shows as the same folder in the repository's main checkout, so Cmd-N from a+worktree pane starts beside it rather than inside it. A worktree folder the+person browses or types stays itself: the harness works in it on its current+branch, and Worktree is off and unavailable ("Already a worktree"). Worktrees+Start made are never offered as recent projects.**Branch** starts on `main` for a fresh desktop form. With Worktree on, local`main` or `origin/main` can supply the base. With Worktree off, it must be a localdiff --git a/desktop/lib/state/new_harness.dart b/desktop/lib/state/new_harness.dartindex dbc38b2e0..2e8f83f49 100644--- a/desktop/lib/state/new_harness.dart+++ b/desktop/lib/state/new_harness.dart@@ -67,24 +67,31 @@ class NewHarnessProject {const NewHarnessProject.fresh([this.name]): folder = null,repository = null,- generated = null;+ generated = null,+ exact = false;NewHarnessProject.generated(ProjectFolderRequest request): name = request.name,generated = request,folder = null,- repository = null;- const NewHarnessProject.folder(String this.folder)+ repository = null,+ exact = false;+ const NewHarnessProject.folder(String this.folder, {this.exact = false}): name = null,repository = null,generated = null;const NewHarnessProject.clone(GitHubRepository this.repository): name = null,folder = null,- generated = null;+ generated = null,+ exact = false;final String? name;final String? folder;final GitHubRepository? repository;final ProjectFolderRequest? generated;++ /// The person picked this very folder (browsed or typed): a worktree stays+ /// itself instead of showing as its repository.+ final bool exact;bool get isNew => folder == null;@override@@ -94,11 +101,13 @@ class NewHarnessProject {other.generated?.generatedAt == generated?.generatedAt &&other.generated?.generatedLabel == generated?.generatedLabel &&other.folder == folder &&+ other.exact == exact &&other.repository?.url == repository?.url;@overrideint get hashCode => Object.hash(name,folder,+ exact,repository?.url,generated?.generatedAt,generated?.generatedLabel,@@ -739,9 +748,13 @@ class NewHarnessController extends ChangeNotifier {String? branchRefreshError;DateTime? _branchesCheckedAt;bool get isGitProject => _gitProject.isGit && !isTerminal && !isCloud;- bool get canUseWorktree => isGitProject && !checkingGit;++ /// A worktree the person picked: the harness works in it, on its branch.+ bool get inWorktree => _project.exact && _gitProject.mainFolder != null;+ bool get canUseWorktree => isGitProject && !checkingGit && !inWorktree;bool get worktree =>isGitProject &&+ !inWorktree &&(_worktree ??(_desktopChoices? app.agentPreference.successfulWorktree ?? true@@ -756,7 +769,7 @@ class NewHarnessController extends ChangeNotifier {/// Fresh desktop work starts on main, with or without a worktree. A retry/// must instead keep the branch of the folder its first attempt prepared.bool get _defaultsToMain =>- _desktopChoices && _attempt?.preparedFolder == null;+ _desktopChoices && _attempt?.preparedFolder == null && !inWorktree;String? get branchRef {if (_branchRef != null) return _branchRef;if (_defaultsToMain) {@@ -975,7 +988,9 @@ class NewHarnessController extends ChangeNotifier {// A worktree is a temporary folder, so the launcher shows its repository.// With Worktree off its branch stays chosen and Start reopens that// worktree; otherwise new work starts from the repository's own branch.- if (info.mainFolder case final main? when _project.folder == key.$2) {+ // One the person picked stays itself ([inWorktree]).+ if (info.mainFolder case final main?+ when _project.folder == key.$2 && !_project.exact) {if (!_defaultsToMain &&_worktree == false &&_branchRef == null &&@@ -2032,7 +2047,7 @@ class NewHarnessController extends ChangeNotifier {/// The folder the system chooser (or the remote browser) came back with.void setFolder(String folder) {if (locked) return;- _project = NewHarnessProject.folder(folder);+ _project = NewHarnessProject.folder(folder, exact: true);field = NewHarnessField.launch;_cycle = null;query = field == NewHarnessField.task ? task : '';@@ -3150,7 +3165,10 @@ class NewHarnessController extends ChangeNotifier {title:'${machine.machine.displayName}:${names[name]! > 1 ? tildePath(folder, id) : name}',detail: location(folder, id),- project: NewHarnessProject.folder(folder),+ project: NewHarnessProject.folder(+ folder,+ exact: folder == _project.folder && _project.exact,+ ),machineId: id,enabled: _machineUsable(machine),why: machine.needsLink ? 'Link required' : 'Offline',@@ -3286,7 +3304,7 @@ class NewHarnessController extends ChangeNotifier {id: 'project:$parent',title: p.basename(parent),detail: 'Use ${location(parent)}',- project: NewHarnessProject.folder(parent),+ project: NewHarnessProject.folder(parent, exact: true),synthetic: true,),];@@ -3302,7 +3320,7 @@ class NewHarnessController extends ChangeNotifier {id: 'project:$folder',title: name,detail: location(folder),- project: NewHarnessProject.folder(folder),+ project: NewHarnessProject.folder(folder, exact: true),),);}@@ -3700,8 +3718,10 @@ class NewHarnessController extends ChangeNotifier {profile: base == 'codex' && _model == null ? _profile : null,profileMachineId: _machineId,);+ // Working in a picked worktree says nothing about the next harness's.final launchedWorktree =- _recoveredWorktreePreference ?? (isGitProject ? worktree : null);+ _recoveredWorktreePreference ??+ (isGitProject && !inWorktree ? worktree : null);final failure = await app.createAgent(_machineId,engine: base,@@ -3756,7 +3776,11 @@ class NewHarnessController extends ChangeNotifier {// goes into it rather than making a second one beside it.if (!checking && attempt.preparedFolder != null) {_recoveredWorktreePreference = launchedWorktree;- _project = NewHarnessProject.folder(attempt.preparedFolder!);+ _project = NewHarnessProject.folder(+ attempt.preparedFolder!,+ // A picked worktree prepared in place is still the one picked.+ exact: _project.exact && attempt.preparedFolder == _project.folder,+ );_gitKey = (_machineId, _project.folder, isTerminal);_worktree = false;_branchRef = null;diff --git a/desktop/lib/widgets/new_harness_form.dart b/desktop/lib/widgets/new_harness_form.dartindex af901e9f8..a0d88a974 100644--- a/desktop/lib/widgets/new_harness_form.dart+++ b/desktop/lib/widgets/new_harness_form.dart@@ -368,6 +368,7 @@ class NewHarnessFormState extends State<NewHarnessForm> {_Row.branch when box.gitError != null => box.gitError,_Row.branch when !box.isGitProject => 'Not a Git repository',_Row.worktree when box.gitError != null => box.gitError,+ _Row.worktree when box.inWorktree => 'Already a worktree',_Row.worktree when !box.canUseWorktree => 'Not a Git repository',_ => null,};diff --git a/desktop/test/new_harness_box_test.dart b/desktop/test/new_harness_box_test.dartindex 6106a469f..f886dac7e 100644--- a/desktop/test/new_harness_box_test.dart+++ b/desktop/test/new_harness_box_test.dart@@ -279,7 +279,10 @@ void main() {expect(box.projectLabel, '~/harnesses/rocket');// The system chooser's answer lands as the folder.box.setFolder('/work/picked');- expect(box.project, const NewHarnessProject.folder('/work/picked'));+ expect(+ box.project,+ const NewHarnessProject.folder('/work/picked', exact: true),+ );});test('typing filters the field, Return takes the row and moves along', () {@@ -404,7 +407,7 @@ void main() {box.accept(box.options.firstWhere((option) => option.title == 'api'));expect(box.project,- NewHarnessProject.folder(p.join(root.path, 'auth', 'api')),+ NewHarnessProject.folder(p.join(root.path, 'auth', 'api'), exact: true),);expect(box.projectLabel, '~/auth/api');});diff --git a/desktop/test/new_harness_git_test.dart b/desktop/test/new_harness_git_test.dartindex 88218580a..0c098ebf1 100644--- a/desktop/test/new_harness_git_test.dart+++ b/desktop/test/new_harness_git_test.dart@@ -723,6 +723,87 @@ void main() {},);+ test('a browsed worktree stays itself and works on its branch', () async {+ const folder = '/harnesses/worktrees/repo/feature';+ final connection = _Connection()+ ..answers[folder] = {+ ...linked('feature'),+ 'branches': [+ {'ref': 'refs/heads/main', 'name': 'main', 'worktree': '/repo'},+ {'ref': 'refs/heads/feature', 'name': 'feature', 'worktree': folder},+ {+ 'ref': 'refs/remotes/origin/main',+ 'name': 'origin/main',+ 'remote': true,+ },+ ],+ };+ final app = createApp(+ connectionForTest: (_) => connection,+ connected: true,+ );+ final box = NewHarnessController(app, machineId: 'm', engine: 'codex')+ ..useDesktopChoices(true);+ addTearDown(app.dispose);+ addTearDown(box.dispose);+ await settle();+ box.setFolder(folder);+ await settle();+ expect(box.project.folder, folder);+ expect(box.inWorktree, true);+ expect(box.canUseWorktree, false);+ expect(box.worktree, false);+ expect(box.opensWorktree, false);+ expect(box.branchLabel, 'feature');+ expect(await box.create(), NewHarnessOutcome.created);+ expect(connection.starts.last, containsPair('projectSource', 'branch'));+ expect(connection.starts.last, containsPair('gitSource', folder));+ expect(+ connection.starts.last,+ containsPair('branchRef', 'refs/heads/feature'),+ );+ expect(+ app.agentPreference.successfulWorktree,+ isNull,+ reason: 'The next harness still gets its own worktree.',+ );+ });++ test('a refused launch in a browsed worktree retries there', () async {+ const folder = '/harnesses/worktrees/repo/feature';+ final connection = _Connection()+ ..answers[folder] = {+ ...linked('feature'),+ 'branches': [+ {'ref': 'refs/heads/main', 'name': 'main', 'worktree': '/repo'},+ {'ref': 'refs/heads/feature', 'name': 'feature', 'worktree': folder},+ ],+ }+ ..failure = {+ 'preparedFolder': folder,+ 'failure': {'code': 'TMUX_UNAVAILABLE'},+ };+ final app = createApp(+ connectionForTest: (_) => connection,+ connected: true,+ );+ final box = NewHarnessController(app, machineId: 'm', engine: 'codex')+ ..useDesktopChoices(true);+ addTearDown(app.dispose);+ addTearDown(box.dispose);+ await settle();+ box.setFolder(folder);+ await settle();+ expect(await box.create(), NewHarnessOutcome.failed);+ await settle();+ expect(box.project.folder, folder);+ expect(box.inWorktree, true);+ expect(box.canUseWorktree, false);+ connection.failure = null;+ expect(await box.create(), NewHarnessOutcome.created);+ expect(connection.starts.last, containsPair('gitSource', folder));+ });+test('a refused launch in a new worktree retries on its branch', () async {const prepared = '/harnesses/worktrees/repo/codex-0922-1136';final connection = _Connection()diff --git a/mobile/lib/phone/new_agent_draft.dart b/mobile/lib/phone/new_agent_draft.dartindex e362c9fe8..11e6a7ba7 100644--- a/mobile/lib/phone/new_agent_draft.dart+++ b/mobile/lib/phone/new_agent_draft.dart@@ -24,6 +24,7 @@ class NewAgentDraft {required this.engine,required this.permissionMode,this.folder,+ this.exactFolder,this.project,this.projectLabel,this.worktree,@@ -47,6 +48,9 @@ class NewAgentDraft {/// A folder that exists, or ([project]) one the machine is to make. Exactly one is ever set —/// see the note on `_NewAgentPageState._project`.final String? folder;++ /// The folder the person browsed to, so a worktree there stays itself.+ final String? exactFolder;final ProjectFolderRequest? project;final String? projectLabel;diff --git a/mobile/lib/phone/new_agent_page.dart b/mobile/lib/phone/new_agent_page.dartindex 652a940ff..0b989a28b 100644--- a/mobile/lib/phone/new_agent_page.dart+++ b/mobile/lib/phone/new_agent_page.dart@@ -278,6 +278,7 @@ class _NewAgentPageState extends State<NewAgentPage> {_engine = draft.engine;_permissionMode = draft.permissionMode;_folder = draft.folder;+ _exactFolder = draft.exactFolder;_project = draft.project;_projectLabel = draft.projectLabel;_worktree = draft.worktree ?? false;@@ -309,6 +310,7 @@ class _NewAgentPageState extends State<NewAgentPage> {engine: _engine,permissionMode: _permissionMode,folder: _folder,+ exactFolder: _exactFolder,project: _project,projectLabel: _projectLabel,worktree: _worktree,@@ -390,8 +392,12 @@ class _NewAgentPageState extends State<NewAgentPage> {/// it matters, and Start waits for this answer: see [_gitSettled].Future<void> _loadGit(String folder) => _gitRead = _readGit(folder);+ /// The folder the person browsed to: a worktree there stays itself, on every read of it.+ String? _exactFolder;+/// [_loadGit]'s read, which it keeps in [_gitRead].Future<void> _readGit(String folder) async {+ final exact = folder == _exactFolder;final machineId = _machineId;setState(() {_gitLoading = true;@@ -410,8 +416,9 @@ class _NewAgentPageState extends State<NewAgentPage> {// ⚠️ **A worktree is a temporary folder, so New shows its repository** — the desktop's rule// (`state/new_harness.dart`). Harness made it for another harness; opening New on it named// that harness's folder (`silent-beacon`) as the project, which nobody recognises. The- // machine names the main checkout, and new work starts from the branch that is on.- if (info.mainFolder case final main?) {+ // machine names the main checkout, and new work starts from the branch that is on. One the+ // person browsed to stays itself, mainFolder and all ([_inWorktree]).+ if (info.mainFolder case final main? when !exact) {_folder = main;_gitFolder = main;info = GitProjectInfo(@@ -427,7 +434,7 @@ class _NewAgentPageState extends State<NewAgentPage> {_gitFailed = info.unavailable;_git = info.isGit ? info : null;if (!info.isGit) return;- _worktree = worktreeByDefault(info);+ _worktree = info.mainFolder == null && worktreeByDefault(info);_branchRef = defaultBranchRef(info, worktree: _worktree);_placeholder ??= placeholderBranch([for (final branch in info.branches)@@ -440,6 +447,9 @@ class _NewAgentPageState extends State<NewAgentPage> {GitProjectInfo? get _repository =>_folder != null && _folder == _gitFolder ? _git : null;+ /// A worktree the person browsed to: the harness works in it, on its branch.+ bool get _inWorktree => _repository?.mainFolder != null;+/// What Start would do, so the rows can say it before it is done. Null when/// there is no repository, or when Worktree is off — the folder simply moves/// to the branch then, and the row already names it.@@ -637,6 +647,7 @@ class _NewAgentPageState extends State<NewAgentPage> {_projectLabel = null;_error = null;});+ _exactFolder = chosen;unawaited(_loadGit(chosen));}@@ -1093,7 +1104,11 @@ class _NewAgentPageState extends State<NewAgentPage> {? tty.faint: null,chevron: false,- onTap: info == null || _locked+ detail: _inWorktree+ ? 'Already a worktree'+ : null,+ onTap:+ info == null || _locked || _inWorktree? null: () => setState(() {_worktree = !_worktree;diff --git a/mobile/test/phone_edge/new_git_wait_test.dart b/mobile/test/phone_edge/new_git_wait_test.dartindex f1709d287..36e3fd377 100644--- a/mobile/test/phone_edge/new_git_wait_test.dart+++ b/mobile/test/phone_edge/new_git_wait_test.dart@@ -12,7 +12,8 @@ import 'edge_fixture.dart';/// A machine that holds every Git read until the test answers it — a laptop slow to say what its/// folder is, which is what a Start pressed as New opens runs into.class _SlowGitConn extends EdgeConn {- _SlowGitConn() : super(const {}, startsAgents);+ _SlowGitConn([Map<String, Map<String, dynamic>> answers = const {}])+ : super(answers, startsAgents);/// Every Git read asked for, in order: the folder, and the answer the test gives it.final reads = <({String path, Completer<Map<String, dynamic>> answer})>[];@@ -69,9 +70,10 @@ void main() {WidgetTester tester, {String? folder,List<Agent>? agents,+ Map<String, Map<String, dynamic>> answers = const {},}) async {setPhone(tester, largePhone);- final conn = _SlowGitConn();+ final conn = _SlowGitConn(answers);final app = edgeApp(conn: conn, agents: agents ?? [edgeAgent('a')]);await tester.pumpWidget(phoneApp(@@ -211,6 +213,58 @@ void main() {await close(tester, app);});+ testWidgets('a worktree browsed to stays the project and starts on its '+ 'branch', (tester) async {+ const worktree = '/home/u/harnesses/worktrees/web/silent-beacon';+ const main = '/home/u/code/web';+ final (:app, :conn) = await openNew(+ tester,+ answers: {+ 'fs_list_dir': {'path': worktree, 'entries': <Object>[]},+ },+ );+ conn.reads.single.answer.complete(_repository('/code/web'));+ await frames(tester);+ await tapInView(tester, find.text('project'));+ await frames(tester);+ await tapInView(tester, find.text('+ Open Folder', findRichText: true));+ await frames(tester);+ await tester.tap(find.byType(FilledButton));+ await frames(tester);+ expect(conn.reads.last.path, worktree);+ // The computer did not answer: Start reads the browsed folder again, still as itself.+ conn.reads.last.answer.complete({'error': 'UNAVAILABLE'});+ await frames(tester);+ await tapInView(tester, find.text('Start'));+ await frames(tester);+ expect(conn.reads.last.path, worktree);+ conn.reads.last.answer.complete({+ ..._repository(worktree),+ 'branch': 'silent-beacon',+ 'mainFolder': main,+ 'mainBranch': 'main',+ 'branches': [+ {'ref': 'refs/heads/main', 'name': 'main', 'worktree': main},+ {+ 'ref': 'refs/heads/silent-beacon',+ 'name': 'silent-beacon',+ 'worktree': worktree,+ },+ {+ 'ref': 'refs/remotes/origin/main',+ 'name': 'origin/main',+ 'remote': true,+ },+ ],+ });+ await frames(tester, count: 20);+ final payload = conn.created.single;+ expect(payload['gitSource'], worktree);+ expect(payload['projectSource'], 'branch');+ expect(payload['branchRef'], 'refs/heads/silent-beacon');+ await close(tester, app);+ });+testWidgets('another folder chosen while the first read is out: Start ''waits for that folder, and the late answer is not used', (tester) async {final (:app, :conn) = await openNew(