Skip to content

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.

New project folders are numbered past agent names too, so a harness is named after its folder

dee43340e8fddec
diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.ts
index 558a3cf68..3a4b80754 100644
--- a/cli/src/backendSocket.ts
+++ b/cli/src/backendSocket.ts
@@ -1704,7 +1704,7 @@ export class BackendSocket {
void this.agentCreations.run(creationId, creationFingerprint(projectFolder ? { ...input, projectFolder } : input), async () => {
let preparedFolder: string | undefined
if (projectFolder) {
- try { preparedFolder = await prepareProjectFolder(projectFolder) }
+ try { preparedFolder = await prepareProjectFolder(projectFolder, { namesInUse: registry.agentNamesInUse() }) }
catch (error) {
return { state: 'failed', error: error instanceof ProjectFolderError ? error.code : 'PROJECT_PREPARATION_FAILED',
detail: error instanceof ProjectFolderError ? error.message : 'Could not prepare the project folder.' }
diff --git a/cli/src/lib/projectFolder.spec.ts b/cli/src/lib/projectFolder.spec.ts
index 32dfbefc1..8c730873e 100644
--- a/cli/src/lib/projectFolder.spec.ts
+++ b/cli/src/lib/projectFolder.spec.ts
@@ -35,6 +35,14 @@ describe('project folder preparation', () => {
expect(await readFile(join(root, 'agent-4'), 'utf8')).toBe('keep')
})
+ it('numbers past the agent names in use, so the next agent answers to its own folder', async () => {
+ await mkdir(join(root, 'harness-42'))
+ // The agent in harness-41 is named harness-42 (the old drift) and one agent was renamed.
+ const folder = await prepareProjectFolder({ source: 'new' }, { root, namesInUse: ['harness-42', 'harness-43', 'Lamp', null, undefined, 'agent-7x'] })
+ expect(folder).toBe(join(root, 'harness-44'))
+ expect(await prepareProjectFolder({ source: 'new' }, { root, namesInUse: [] })).toBe(join(root, 'harness-45'))
+ })
+
it('publishes a complete clone and never replaces existing files or starts a second clone', async () => {
const project = parseProjectFolder({ projectSource: 'remote', repositoryUrl: 'owner/repo' })!
const clone = vi.fn(async (url: string, destination: string) => {
diff --git a/cli/src/lib/projectFolder.ts b/cli/src/lib/projectFolder.ts
index 680fe4845..61704ce97 100644
--- a/cli/src/lib/projectFolder.ts
+++ b/cli/src/lib/projectFolder.ts
@@ -46,14 +46,26 @@ async function exists(path: string): Promise<boolean> {
export async function prepareProjectFolder(
project: ProjectFolder,
- options: { root?: string; clone?: (url: string, destination: string) => Promise<void> } = {},
+ options: {
+ root?: string
+ clone?: (url: string, destination: string) => Promise<void>
+ /**
+ * Agent names already taken on this machine. An agent started in `harness-N` answers to
+ * `harness-N` only while that name is free (registry.ts, nextAgentName); numbered from the
+ * folders alone, one folder once named off by one kept every later one off by one — the tab said
+ * harness-43 over a terminal in ~/harnesses/harness-42. Counting the names too puts the next
+ * folder past both, where its agent's name is free and the two agree again.
+ */
+ namesInUse?: Iterable<string | null | undefined>
+ } = {},
): Promise<string> {
const root = options.root ?? join(homedir(), 'harnesses')
let staging: string | undefined
try {
await mkdir(root, { recursive: true })
if (project.source === 'new') {
- const numbers = (await readdir(root)).map(name => /^(?:harness|agent)-([1-9]\d*)$/.exec(name)?.[1])
+ const numbers = [...await readdir(root), ...(options.namesInUse ?? [])]
+ .map(name => (name ? /^(?:harness|agent)-([1-9]\d*)$/.exec(name)?.[1] : undefined))
let next = numbers.reduce((max, value) => value && BigInt(value) > max ? BigInt(value) : max, 0n) + 1n
for (;;) {
const folder = join(root, `harness-${next++}`)
diff --git a/cli/src/lib/registry.spec.ts b/cli/src/lib/registry.spec.ts
index 42538b2b7..2dda319f7 100644
--- a/cli/src/lib/registry.spec.ts
+++ b/cli/src/lib/registry.spec.ts
@@ -1037,6 +1037,17 @@ describe('agent identity: the process owns the agent, the session is bound to it
expect(registry.displayName(named)).toBe('harness-43')
})
+ it('lists every name in use, so a new project folder can be numbered past them', async () => {
+ const { registry } = await loadRegistryModule()
+ registry.load()
+ const a = registry.openPendingAgent({ engine: 'claude', runtimes: [{ backend: 'tmux', paneId: '%11' }], cwd: '/Users/u/harnesses/harness-41' })!
+ registry.openPendingAgent({ engine: 'claude', runtimes: [{ backend: 'tmux', paneId: '%12' }], cwd: '/Users/u/harnesses/harness-41' })
+ registry.rename(a.agentId, 'Lamp')
+ const names = registry.agentNamesInUse()
+ expect(names).toEqual(expect.arrayContaining(['harness-41', 'harness-42', 'Lamp']))
+ expect(names.every((name) => typeof name === 'string' && name.length > 0)).toBe(true)
+ })
+
it('gives new agents stable numbered names across binding and reload without changing custom names', async () => {
const { registry } = await loadRegistryModule()
registry.load()
diff --git a/cli/src/lib/registry.ts b/cli/src/lib/registry.ts
index 4dc4634f4..b12c2345c 100644
--- a/cli/src/lib/registry.ts
+++ b/cli/src/lib/registry.ts
@@ -1033,12 +1033,17 @@ class Registry {
return entry
}
- private nextAgentName(cwd?: string | null): string {
- let next = 1n
- const names = [
+ /** Every name an agent on this machine answers to: default names, project names, renames. */
+ agentNamesInUse(): string[] {
+ return [
...this.list().flatMap(agent => [agent.defaultName, projectDisplayName(agent)]),
...NAME_OVERRIDES.values(),
- ]
+ ].filter((name): name is string => typeof name === 'string' && name.length > 0)
+ }
+
+ private nextAgentName(cwd?: string | null): string {
+ let next = 1n
+ const names = this.agentNamesInUse()
// A new project is the folder `~/harnesses/harness-N`, numbered by its own counter. An agent
// started in it answers to the same N rather than to this counter, which drifts from that one
// (the tab said harness-42 over a terminal in ~/harnesses/harness-41). Taken already — a second
diff --git a/desktop/lib/core/project_folder.dart b/desktop/lib/core/project_folder.dart
index e799636ba..245f12d80 100644
--- a/desktop/lib/core/project_folder.dart
+++ b/desktop/lib/core/project_folder.dart
@@ -18,9 +18,15 @@ class ProjectFolderRequest {
if (repository != null) 'repositoryUrl': repository!.url,
};
+ /// [namesInUse] are the names this computer's agents already answer to. The daemon names an agent
+ /// started in `harness-N` after its folder only while `harness-N` is free, so folders numbered
+ /// from the disk alone drifted: once one agent was named a number ahead, every later tab said
+ /// harness-43 over a terminal in ~/harnesses/harness-42. Numbering past the names as well puts
+ /// the next folder where its agent's name is free, and the two agree again.
Future<String> prepareLocal({
String? projectHome,
RepositoryClone Function()? createClone,
+ Iterable<String> namesInUse = const [],
}) async {
final home =
Platform.environment['HOME'] ?? Platform.environment['USERPROFILE'];
@@ -39,13 +45,17 @@ class ProjectFolderRequest {
);
}
var next = BigInt.one;
- await for (final entry in root.list(followLinks: false)) {
- final match = RegExp(r'^(?:harness|agent)-([1-9]\d*)$')
- .firstMatch(p.basename(entry.path));
- if (match == null) continue;
+ final numbered = RegExp(r'^(?:harness|agent)-([1-9]\d*)$');
+ void count(String name) {
+ final match = numbered.firstMatch(name);
+ if (match == null) return;
final number = BigInt.parse(match.group(1)!);
if (number >= next) next = number + BigInt.one;
}
+ await for (final entry in root.list(followLinks: false)) {
+ count(p.basename(entry.path));
+ }
+ namesInUse.forEach(count);
for (;;) {
final folder = p.join(root.path, 'harness-$next');
next += BigInt.one;
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 4cb165b4e..9f1779e98 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -4483,8 +4483,10 @@ class AppNotifier extends ChangeNotifier {
/// Starts an agent, or recovers this form's earlier request after a lost reply.
/// Returns null on success, or an inline message; [attempt] tells the form
/// whether to offer Check status instead of inviting another creation.
- Future<String> prepareLocalProjectFolder(ProjectFolderRequest request) =>
- request.prepareLocal();
+ Future<String> prepareLocalProjectFolder(
+ ProjectFolderRequest request, {
+ Iterable<String> namesInUse = const [],
+ }) => request.prepareLocal(namesInUse: namesInUse);
Future<String?> createAgent(
String machineId, {
@@ -4615,7 +4617,10 @@ class AppNotifier extends ChangeNotifier {
final project = repository is String
? ProjectFolderRequest.remote(GitHubRepository.parse(repository)!)
: const ProjectFolderRequest.newProject();
- creation._preparedFolder ??= await prepareLocalProjectFolder(project);
+ creation._preparedFolder ??= await prepareLocalProjectFolder(
+ project,
+ namesInUse: machine.agents.map((agent) => agent.name),
+ );
} on RepositoryCloneException catch (error) {
return creation._complete(error.message);
} catch (_) {
diff --git a/desktop/test/new_agent_project_folder_test.dart b/desktop/test/new_agent_project_folder_test.dart
index c65fcd2e2..5887278be 100644
--- a/desktop/test/new_agent_project_folder_test.dart
+++ b/desktop/test/new_agent_project_folder_test.dart
@@ -61,14 +61,24 @@ class _App extends AppNotifier {
name: 'Studio',
authMode: MachineAuthMode.remote,
);
- machineStates['m'] = MachineState(machine)..localOnly = local;
+ machineStates['m'] = MachineState(machine)
+ ..localOnly = local
+ ..agents = [
+ Agent(id: 'a1', name: 'harness-42', status: 'idle'),
+ Agent(id: 'a2', name: 'Lamp', status: 'idle'),
+ ];
}
final prepared = <ProjectFolderRequest>[];
+ final namesSeen = <List<String>>[];
@override
Future<void> probeEngines(String machineId, {bool force = false}) async {}
@override
- Future<String> prepareLocalProjectFolder(ProjectFolderRequest request) async {
+ Future<String> prepareLocalProjectFolder(
+ ProjectFolderRequest request, {
+ Iterable<String> namesInUse = const [],
+ }) async {
prepared.add(request);
+ namesSeen.add(namesInUse.toList());
return '/local/Harness Projects/project-test';
}
}
@@ -151,6 +161,10 @@ void main() {
);
expect(connection.calls.last.$2.containsKey('projectSource'), isFalse);
expect(app.prepared.length, local ? 1 : 0);
+ if (local) {
+ expect(app.namesSeen.single, ['harness-42', 'Lamp'],
+ reason: 'the folder is numbered past the names this computer\'s agents answer to');
+ }
connection.fail();
await tester.pumpAndSettle();
});
diff --git a/desktop/test/project_folder_test.dart b/desktop/test/project_folder_test.dart
index fc5fd802e..464c22966 100644
--- a/desktop/test/project_folder_test.dart
+++ b/desktop/test/project_folder_test.dart
@@ -33,6 +33,27 @@ void main() {
expect(await File(p.join(root.path, 'agent-4')).readAsString(), 'keep');
},
);
+ test(
+ 'a new project is numbered past the names agents already answer to',
+ () async {
+ final root = await Directory.systemTemp.createTemp(
+ 'harness-new-project-names-test-',
+ );
+ addTearDown(() => root.delete(recursive: true));
+ await Directory(p.join(root.path, 'harness-42')).create();
+ const request = ProjectFolderRequest.newProject();
+ // The agent in harness-41 answers to harness-42, the one in harness-42 to harness-43.
+ final folder = await request.prepareLocal(
+ projectHome: root.path,
+ namesInUse: const ['harness-42', 'harness-43', 'Lamp', 'agent-7x'],
+ );
+ expect(p.basename(folder), 'harness-44');
+ expect(
+ p.basename(await request.prepareLocal(projectHome: root.path)),
+ 'harness-45',
+ );
+ },
+ );
test(
'remote repository uses the existing safe clone on this computer',
() async {