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.

fix(swarms): retry project clone over the other GitHub transport

tony11f3f5f10086
diff --git a/cli/src/lib/projectFolder.spec.ts b/cli/src/lib/projectFolder.spec.ts
index 0b37ab0df..7f8b741a2 100644
--- a/cli/src/lib/projectFolder.spec.ts
+++ b/cli/src/lib/projectFolder.spec.ts
@@ -3,7 +3,9 @@ import { execFileSync } from 'node:child_process'
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
-import { parseProjectFolder, prepareProjectFolder, ProjectFolderError, projectsRoot, repositoryIdentity } from './projectFolder.js'
+import {
+ cloneFailure, otherGitHubTransport, parseProjectFolder, prepareProjectFolder, ProjectFolderError, projectsRoot, repositoryIdentity, RepositoryAccessError,
+} from './projectFolder.js'
/** A checkout at [folder] whose origin is [origin] (none when null), made by the real git, with no network. */
function checkout(folder: string, origin: string | null): void {
@@ -118,6 +120,41 @@ describe('project folder preparation', () => {
}
})
+ it('tells a refused clone from one that failed for another reason, keeping the code the apps know', () => {
+ for (const diagnostic of [
+ "fatal: could not read Username for 'https://github.com': terminal prompts disabled",
+ 'ERROR: Repository not found.\nfatal: Could not read from remote repository.',
+ '[email protected]: Permission denied (publickey).',
+ 'remote: Invalid username or token. Password authentication is not supported.\nfatal: Authentication failed',
+ ]) {
+ const error = cloneFailure(diagnostic)
+ expect(error, diagnostic).toBeInstanceOf(RepositoryAccessError)
+ expect(error.code).toBe('CLONE_FAILED')
+ expect(error.message).toContain('GitHub access on the selected machine')
+ }
+ for (const diagnostic of ['fatal: unable to access: Could not resolve host: github.com', 'Host key verification failed.', '']) {
+ const error = cloneFailure(diagnostic)
+ expect(error, diagnostic).not.toBeInstanceOf(RepositoryAccessError)
+ expect(error).toMatchObject({ code: 'CLONE_FAILED', message: 'Could not clone the repository. Check the URL and connection, then retry.' })
+ }
+ })
+
+ it('names the same GitHub repository over the other transport, and nothing for another address', () => {
+ expect(otherGitHubTransport('https://github.com/owner/repo.git')).toBe('[email protected]:owner/repo.git')
+ expect(otherGitHubTransport('[email protected]:owner/repo.git')).toBe('https://github.com/owner/repo.git')
+ for (const url of ['owner/repo', 'https://gitlab.com/owner/repo.git', 'https://github.com/owner/repo/extra', 'ssh://[email protected]/owner/repo', '']) {
+ expect(otherGitHubTransport(url), url).toBeNull()
+ }
+ // Both forms parseProjectFolder makes round-trip to each other.
+ for (const url of ['https://github.com/owner/repo', '[email protected]:owner/repo']) {
+ const parsed = parseProjectFolder({ projectSource: 'remote', repositoryUrl: url })
+ if (parsed?.source !== 'remote') throw new Error('expected a remote project')
+ const other = otherGitHubTransport(parsed.repositoryUrl)!
+ expect(otherGitHubTransport(other)).toBe(parsed.repositoryUrl)
+ expect(repositoryIdentity(other)).toBe(repositoryIdentity(parsed.repositoryUrl))
+ }
+ })
+
it('starts in a checkout of the same repository already at the destination, and never clones it again', async () => {
const clone = vi.fn(async () => { throw new Error('must not clone') })
// The address asked for and the origin git keeps, in each other's spellings.
diff --git a/cli/src/lib/projectFolder.ts b/cli/src/lib/projectFolder.ts
index d7cd6f2cf..8ceb4af31 100644
--- a/cli/src/lib/projectFolder.ts
+++ b/cli/src/lib/projectFolder.ts
@@ -272,9 +272,26 @@ function cloneRepository(url: string, destination: string): Promise<void> {
clearTimeout(forceKill)
if (timedOut) reject(new ProjectFolderError('CLONE_TIMEOUT', 'Cloning took too long. Check the connection and retry.'))
else if (code === 0) resolve()
- else reject(new ProjectFolderError('CLONE_FAILED', /authentication|permission denied|could not read username|repository not found/i.test(diagnostic)
- ? 'Could not access this repository. Check the URL and GitHub access on the selected machine.'
- : 'Could not clone the repository. Check the URL and connection, then retry.'))
+ else reject(cloneFailure(diagnostic))
})
})
}
+
+/** CLONE_FAILED because the host refused this machine's credentials, not because of the network or the URL. The code
+ * stays CLONE_FAILED for the apps; a caller that knows another way in can tell the two apart. */
+export class RepositoryAccessError extends ProjectFolderError {}
+
+/** What git's diagnostic of a failed clone becomes. A private GitHub repository answers "not found" without access. */
+export function cloneFailure(diagnostic: string): ProjectFolderError {
+ return /authentication|permission denied|could not read username|repository not found/i.test(diagnostic)
+ ? new RepositoryAccessError('CLONE_FAILED', 'Could not access this repository. Check the URL and GitHub access on the selected machine.')
+ : new ProjectFolderError('CLONE_FAILED', 'Could not clone the repository. Check the URL and connection, then retry.')
+}
+
+/** The same GitHub repository over the other transport, for parseProjectFolder's two forms; null for anything else. */
+export function otherGitHubTransport(url: string): string | null {
+ const ssh = /^git@github\.com:([^/\s]+\/[^/\s]+)$/.exec(url)
+ if (ssh) return `https://github.com/${ssh[1]}`
+ const https = /^https:\/\/github\.com\/([^/\s]+\/[^/\s]+)$/.exec(url)
+ return https ? `[email protected]:${https[1]}` : null
+}
diff --git a/cli/src/superTabs/projectFolder.spec.ts b/cli/src/superTabs/projectFolder.spec.ts
index 8431abe1d..5ec78624e 100644
--- a/cli/src/superTabs/projectFolder.spec.ts
+++ b/cli/src/superTabs/projectFolder.spec.ts
@@ -1,6 +1,7 @@
import { join } from 'node:path'
import { describe, expect, it, vi } from 'vitest'
import { fakeCore } from '../testing/fakeCore.js'
+import { ProjectFolderError, RepositoryAccessError } from '../lib/projectFolder.js'
import { prepareSwarmProject } from './projectFolder.js'
function fixture() {
@@ -53,6 +54,45 @@ describe('per-project checkouts on a contributed swarm machine', () => {
await expect(f.run()).rejects.toThrow(/Share a GitHub repository.*project\.repository command/)
expect(f.prepare).not.toHaveBeenCalled()
})
+ it('clones over SSH when GitHub refuses this machine over the shared HTTPS address, and the reverse', async () => {
+ const refused = () => new RepositoryAccessError('CLONE_FAILED', 'Could not access this repository.')
+ for (const [shared, first, second] of [
+ ['https://github.com/example/project', 'https://github.com/example/project.git', '[email protected]:example/project.git'],
+ ['[email protected]:example/project.git', '[email protected]:example/project.git', 'https://github.com/example/project.git'],
+ ]) {
+ const f = fixture()
+ f.repository.repositoryUrl = shared!
+ f.prepare.mockRejectedValueOnce(refused())
+ await f.run()
+ expect(f.prepare.mock.calls.map(call => (call as unknown[])[0])).toEqual([
+ { source: 'remote', repositoryUrl: first, name: 'project-a' },
+ { source: 'remote', repositoryUrl: second, name: 'project-a' },
+ ])
+ }
+ })
+ it('does not switch transport for a failure other than refused access', async () => {
+ for (const failure of [new ProjectFolderError('CLONE_FAILED', 'Could not clone the repository.'),
+ new ProjectFolderError('CLONE_TIMEOUT', 'Cloning took too long.'), new Error('disk full')]) {
+ const f = fixture()
+ f.prepare.mockRejectedValueOnce(failure)
+ await expect(f.run()).rejects.toBe(failure)
+ expect(f.prepare).toHaveBeenCalledOnce()
+ }
+ })
+ it('says what the machine owner needs when neither transport can reach the repository', async () => {
+ for (const second of [new RepositoryAccessError('CLONE_FAILED', 'Could not access this repository.'),
+ new ProjectFolderError('CLONE_FAILED', 'Host key verification failed.'), new ProjectFolderError('CLONE_TIMEOUT', 'Cloning took too long.')]) {
+ const f = fixture()
+ f.prepare.mockRejectedValueOnce(new RepositoryAccessError('CLONE_FAILED', 'Could not access this repository.')).mockRejectedValueOnce(second)
+ await expect(f.run()).rejects.toMatchObject({ code: 'CLONE_FAILED',
+ message: expect.stringMatching(/over HTTPS or SSH.*gh auth login and gh auth setup-git.*SSH key/) })
+ expect(f.prepare).toHaveBeenCalledTimes(2)
+ }
+ // Anything else on the second attempt is that failure, such as a folder created meanwhile.
+ const f = fixture(), exists = new ProjectFolderError('PROJECT_EXISTS', '“project-a” already exists.')
+ f.prepare.mockRejectedValueOnce(new RepositoryAccessError('CLONE_FAILED', 'Could not access this repository.')).mockRejectedValueOnce(exists)
+ await expect(f.run()).rejects.toBe(exists)
+ })
it('refuses missing or unsafe repositories and a mismatched prepared checkout', async () => {
const f = fixture()
for (const url of [undefined, 'file:///private/repo', 'https://[email protected]/example/project', 'ext::command']) {
diff --git a/cli/src/superTabs/projectFolder.ts b/cli/src/superTabs/projectFolder.ts
index 4d2465436..bf4ced6d3 100644
--- a/cli/src/superTabs/projectFolder.ts
+++ b/cli/src/superTabs/projectFolder.ts
@@ -1,6 +1,6 @@
import { isAbsolute, join, resolve } from 'node:path'
import type { CoreApi } from '../core/api.js'
-import { parseProjectFolder, prepareProjectFolder } from '../lib/projectFolder.js'
+import { otherGitHubTransport, parseProjectFolder, prepareProjectFolder, ProjectFolderError, RepositoryAccessError } from '../lib/projectFolder.js'
import { identifier, object } from './wire.js'
type Json = Record<string, unknown>
@@ -31,6 +31,18 @@ export async function prepareSwarmProject(core: CoreApi, row: Json, start: Json,
if (typeof repository.repositoryUrl !== 'string') throw new Error('Share a GitHub repository for this project before starting agents on another machine. Its lead can share its checkout\'s GitHub origin with the project.repository command, then start again.')
const source = parseProjectFolder({ projectSource: 'remote', repositoryUrl: repository.repositoryUrl })
if (source?.source !== 'remote') throw new Error('This project repository is unavailable.')
- const cwd = await prepare({ ...source, name: project.id }, { root: expectedRoot })
+ let cwd: string
+ try { cwd = await prepare({ ...source, name: project.id }, { root: expectedRoot }) }
+ catch (error) {
+ // The shared address names the repository, not how this machine signs in to GitHub: a credential helper for HTTPS,
+ // a key for SSH. Desktop shares HTTPS because the daemon's remote no longer says which one the lead used.
+ const other = error instanceof RepositoryAccessError ? otherGitHubTransport(source.repositoryUrl) : null
+ if (!other) throw error
+ try { cwd = await prepare({ ...source, repositoryUrl: other, name: project.id }, { root: expectedRoot }) }
+ catch (second) {
+ if (!(second instanceof ProjectFolderError) || !['CLONE_FAILED', 'CLONE_TIMEOUT'].includes(second.code)) throw second
+ throw new ProjectFolderError('CLONE_FAILED', 'This machine could not access the project repository over HTTPS or SSH. Its owner needs GitHub access to it here: run gh auth login and gh auth setup-git, or add an SSH key to their GitHub account.')
+ }
+ }
if (cwd !== expected) throw new Error('The prepared checkout does not match the authorized project.')
}
diff --git a/docs/swarms.md b/docs/swarms.md
index a01e06922..2ad51a88e 100644
--- a/docs/swarms.md
+++ b/docs/swarms.md
@@ -45,6 +45,12 @@ GitHub `origin` as an HTTPS address when the lead starts. If a project has no
address yet, its owner or lead can share one once with `project.repository`.
An address that is already shared is not replaced.
+Each host clones with its owner's own GitHub access; Harness never passes
+credentials between machines. If GitHub refuses the shared address, the host
+tries the other transport (HTTPS or SSH) once. For a private repository, every
+contributing owner needs access to it on that machine, either through
+`gh auth login` and `gh auth setup-git` or through an SSH key.
+
Ordinary guest machine sharing does not authorize Swarm controls under the
owner's account. The owner contributes the machine or an agent. The lead's
routing policy still determines eligible engines, capacity and model choices.