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(cli): a start whose GitHub App token call gets no answer is tried again, not failed at once (#2390)

harness-for-autonomous[bot]8f35d2cba752
diff --git a/cli/src/superTabs/github.ts b/cli/src/superTabs/github.ts
index 4c365f6f5..cdd830d78 100644
--- a/cli/src/superTabs/github.ts
+++ b/cli/src/superTabs/github.ts
@@ -29,7 +29,7 @@ function githubDir(core: CoreApi, session: Session | undefined): string | null {
/** Refusals that mean the session must not keep a token: anything the backend decided, and no app on the server. A
* backend that could not be reached decides nothing, and an unexpired token stays. */
-const decided = (error: SuperTabHostError) => (error.status >= 400 && error.status < 500) || error.code === 'GITHUB_APP_NOT_CONFIGURED'
+export const decided = (error: SuperTabHostError) => (error.status >= 400 && error.status < 500) || error.code === 'GITHUB_APP_NOT_CONFIGURED'
/**
* The token of [harnessId]'s task: asked as each Swarm membership this machine's roster lists for that harness (one,
diff --git a/cli/src/superTabs/machines.spec.ts b/cli/src/superTabs/machines.spec.ts
index e701e8951..640c60b3d 100644
--- a/cli/src/superTabs/machines.spec.ts
+++ b/cli/src/superTabs/machines.spec.ts
@@ -212,6 +212,74 @@ describe('contributed machine executor', () => {
const failed = f.backend.mock.calls.find(c => JSON.stringify(c[3] ?? {}).includes('could not get a Harness GitHub App token'))!
expect(failed[3]).toMatchObject({ command: { type: 'agent.startFailed', startId: 'start' } })
})
+ // Diego's Mac, 2026-10-30: one backend timeout on the GitHub App token call failed the start on its first try. Only an
+ // answer the backend decided fails it now; a timeout or a 5xx is tried again on the next pass, for as long as a refused start.
+ describe('a start whose GitHub App token call does not get an answer', () => {
+ const TOKEN_ANSWER = { github: true, token: `ghs_${'f'.repeat(36)}`, repository: 'example/project', requestedBy: { email: '[email protected]' } }
+ afterEach(() => { vi.unstubAllGlobals() })
+ /** A team account's host preparing with the real prepareSwarmStart, its token call answered by [answers] in turn; the
+ * token's revocation goes to [revoked], never to GitHub. */
+ function teamHost(...answers: Array<() => unknown>) {
+ const revoked = vi.fn(async (_url: string, _init: { headers: Record<string, string> }) => ({ status: 204 }))
+ vi.stubGlobal('fetch', revoked)
+ const f = fixture()
+ const machine = new ContributedMachines(f.core, 'host', { backend: f.deps.backend, open: f.open, capabilities: f.capabilities, start: f.start,
+ log: f.log, env: {}, privacy: () => null, team: () => true, now: () => now } as never)
+ let now = 1000
+ Object.assign(f.pool, { githubApp: true })
+ const token = vi.fn(async () => (answers.length > 1 ? answers.shift()! : answers[0]!)())
+ f.backend.mockImplementation(async (_core: unknown, _method: string, path: string): Promise<unknown> => path.includes('/hosts/host/changes')
+ ? hostForm([], { pools: [f.pool] }) : path.endsWith('/github-token') ? token() : { revision: 1 })
+ const failures = () => f.backend.mock.calls.filter(c => c[2] === '/api/super-tabs/hosts/host/starts/tab/start')
+ .map(c => c[3] as { operationId: string; command: { type: string; error?: string } }).filter(body => body.command.type === 'agent.startFailed')
+ return { f, machine, token, failures, revoked, pass: async (ms = 0) => { now += ms; await machine.tick() } }
+ }
+ const timeout = () => { throw new SuperTabHostError('BACKEND_TIMEOUT', 'The Harness server did not answer in time.', 504) }
+
+ it('is tried again on the next pass after one timeout, and starts then', async () => {
+ const h = teamHost(timeout, () => TOKEN_ANSWER)
+ await h.pass()
+ expect([h.failures(), h.f.start.mock.calls.length]).toEqual([[], 0])
+ expect(h.f.log.mock.calls.flat().join('\n')).toContain('The Harness server did not answer in time.')
+ await h.pass(16_000)
+ expect(h.token).toHaveBeenCalledTimes(2)
+ expect(h.failures()).toEqual([])
+ expect((h.f.start.mock.calls as unknown[][])[0]![0]).toEqual({ tabId: 'tab', startId: 'start', hostDispatch: true, github: { requester: '[email protected]' } })
+ // The read-only token is revoked once the start is prepared, as on a first try.
+ expect(h.revoked.mock.calls.map(([url, init]) => [url, init.headers.Authorization])).toEqual([['https://api.github.com/installation/token', `Bearer ${TOKEN_ANSWER.token}`]])
+ })
+
+ it('is tried again after a 5xx or a failure to reach the server, as after a timeout', async () => {
+ for (const failure of [() => { throw new SuperTabHostError('BACKEND_UNAVAILABLE', 'Bad gateway.', 502) }, () => { throw new Error('fetch failed') }]) {
+ const h = teamHost(failure, () => TOKEN_ANSWER)
+ await h.pass()
+ await h.pass(16_000)
+ expect([h.failures(), h.f.start.mock.calls.length]).toEqual([[], 1])
+ }
+ })
+
+ it('fails at once when the backend refuses it or the server has no GitHub App', async () => {
+ for (const refusal of [new SuperTabHostError('FORBIDDEN', 'This start is not this machine\'s.', 403),
+ new SuperTabHostError('GITHUB_APP_NOT_CONFIGURED', 'This server has no GitHub App.', 503)]) {
+ const h = teamHost(() => { throw refusal })
+ await h.pass()
+ expect(h.failures()).toEqual([{ operationId: 'prepare-failed-start', command: { type: 'agent.startFailed', startId: 'start',
+ error: `This machine could not get a Harness GitHub App token for this project, so it was not prepared with its owner's own GitHub login either: ${refusal.message}` } }])
+ expect(h.f.start).not.toHaveBeenCalled()
+ }
+ })
+
+ it('fails after the same minute as a refused start when it keeps timing out, naming the timeout', async () => {
+ const h = teamHost(timeout)
+ await h.pass()
+ await h.pass(30_000)
+ expect(h.failures()).toEqual([])
+ await h.pass(30_000)
+ expect(h.failures()).toEqual([{ operationId: 'prepare-failed-start', command: { type: 'agent.startFailed', startId: 'start',
+ error: 'This machine could not get a Harness GitHub App token for this project, so it was not prepared with its owner\'s own GitHub login either: The Harness server did not answer in time. (BACKEND_TIMEOUT)' } }])
+ expect(h.f.start).not.toHaveBeenCalled()
+ })
+ })
it('readies a start with the backend, as a team account when this daemon runs team work only', async () => {
for (const team of [false, true]) {
const f = fixture()
diff --git a/cli/src/superTabs/machines.ts b/cli/src/superTabs/machines.ts
index 299625fdc..7cc4f3234 100644
--- a/cli/src/superTabs/machines.ts
+++ b/cli/src/superTabs/machines.ts
@@ -12,7 +12,7 @@ import { HostFeed } from './hostFeed.js'
import { SwarmReads } from './swarmReads.js'
import { readCapabilities, quotaOf, meteredEngine, quotaUnknownReason } from './capacity.js'
import { startAgent, type ScopedRequest } from './start.js'
-import { prepareSwarmStart } from './projectFolder.js'
+import { PrepareRetryError, prepareSwarmStart } from './projectFolder.js'
import { readAuthSession } from '../lib/authSession.js'
import { createSwarmCloneTrustRepair, swarmCloneRoots } from './cloneTrust.js'
import { git, sweepOne } from '../lib/worktreeSweep.js'
@@ -598,6 +598,14 @@ export class ContributedMachines {
github = (await (this.deps.prepare ?? ((core, pooled, queued) => prepareSwarmStart(core, pooled, queued,
{ backend: this.deps.backend, team: (this.deps.team ?? (() => readAuthSession()?.kind === 'team'))() })))(this.core, row, start)) ?? null
} catch (error) {
+ // A token call that got no answer is tried again on the next passes, on the clock of a refused start, and
+ // fails it with the last reason once that runs out (Diego's Mac, 2026-10-30: one timeout failed a start).
+ if (error instanceof PrepareRetryError) {
+ const since = this.refusedSince.get(start.id) ?? began
+ this.refusedSince.set(start.id, since)
+ this.noteWaiting(start.id, error.message)
+ if (began - since < START_REFUSED_MS) continue
+ }
await scoped({ action: 'command', operationId: `prepare-failed-${start.id}`,
command: { type: 'agent.startFailed', startId: start.id,
error: error instanceof Error ? error.message : 'Could not prepare this project on the machine.' } })
diff --git a/cli/src/superTabs/projectFolder.spec.ts b/cli/src/superTabs/projectFolder.spec.ts
index 3ce8cdc43..1ec2e7ddd 100644
--- a/cli/src/superTabs/projectFolder.spec.ts
+++ b/cli/src/superTabs/projectFolder.spec.ts
@@ -2,7 +2,8 @@ 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, prepareSwarmStart, startGitHub } from './projectFolder.js'
+import { PrepareRetryError, prepareSwarmProject, prepareSwarmStart, startGitHub } from './projectFolder.js'
+import { SuperTabHostError } from './wire.js'
function fixture() {
const core = fakeCore({ dataDir: '/isolated-project-fixture' })
@@ -158,7 +159,7 @@ describe('preparing another person\'s start with the Harness GitHub App', () =>
})
it('fails the start when the backend refuses a token, and never prepares it with the owner\'s login instead', async () => {
- for (const answer of [new Error('The Harness GitHub App is not installed on example/project.'), { github: true }, { github: true, token: TOKEN, repository: 'x', requestedBy: {} }, 'nonsense']) {
+ for (const answer of [new SuperTabHostError('GITHUB_APP_NOT_INSTALLED', 'The Harness GitHub App is not installed on example/project.', 409), { github: true }, { github: true, token: TOKEN, repository: 'x', requestedBy: {} }, 'nonsense']) {
const f = hosted(answer)
await expect(f.ready()).rejects.toThrow(answer instanceof Error ? answer.message : 'answered without a GitHub token')
expect(f.prepare).not.toHaveBeenCalled()
@@ -169,6 +170,35 @@ describe('preparing another person\'s start with the Harness GitHub App', () =>
await expect(noIds.ready()).rejects.toThrow('cannot be prepared')
})
+ // Diego's Mac, 2026-10-30: a backend timeout on the token call failed the start at once. Only an answer the backend decided
+ // (a 4xx, or no app on the server: github.ts `decided`) is a refusal; anything else is for the host to try again.
+ it('tells a token call the backend decided from one that got no answer, which is tried again, and prepares nothing', async () => {
+ const retried = [new SuperTabHostError('BACKEND_TIMEOUT', 'The Harness server did not answer in time.', 504), new SuperTabHostError('BACKEND_UNAVAILABLE', 'Bad gateway.', 502),
+ new Error('fetch failed')]
+ for (const error of retried) {
+ const f = hosted(error)
+ const thrown = await f.ready(true).catch(caught => caught)
+ expect([thrown instanceof PrepareRetryError, thrown.message]).toEqual([true,
+ `This machine could not get a Harness GitHub App token for this project, so it was not prepared with its owner's own GitHub login either: ${error.message}${error instanceof SuperTabHostError ? ` (${error.code})` : ''}`])
+ expect([f.prepare.mock.calls.length, f.fetch.mock.calls.length, f.revoke.mock.calls.length]).toEqual([0, 0, 0])
+ }
+ for (const error of [new SuperTabHostError('FORBIDDEN', 'Not this machine\'s start.', 403), new SuperTabHostError('UPDATE_REQUIRED', 'Update Harness.', 426),
+ new SuperTabHostError('GITHUB_APP_NOT_CONFIGURED', 'No GitHub App.', 503)]) {
+ const thrown = await hosted(error).ready(true).catch(caught => caught)
+ expect([thrown instanceof PrepareRetryError, thrown.message]).toEqual([false,
+ `This machine could not get a Harness GitHub App token for this project, so it was not prepared with its owner's own GitHub login either: ${error.message}`])
+ }
+ })
+
+ // Kept out of this change by its Lead (10-30): a default branch that could not be fetched never fails or holds a start; the
+ // worktree starts from what the checkout has, as before.
+ it('starts from what the checkout has when the default branch could not be fetched', async () => {
+ const f = hosted()
+ f.fetch.mockResolvedValueOnce(false)
+ expect(await f.ready(true)).toEqual({ requester: '[email protected]' })
+ expect(f.revoke.mock.calls).toEqual([[TOKEN]])
+ })
+
it('a clone the token cannot make says so, and is not tried again another way', async () => {
const f = hosted()
f.prepare.mockRejectedValueOnce(new RepositoryAccessError('CLONE_FAILED', 'Could not access this repository.'))
diff --git a/cli/src/superTabs/projectFolder.ts b/cli/src/superTabs/projectFolder.ts
index 45768c6a3..eb6ad1b50 100644
--- a/cli/src/superTabs/projectFolder.ts
+++ b/cli/src/superTabs/projectFolder.ts
@@ -3,7 +3,8 @@ import type { CoreApi } from '../core/api.js'
import { fetchDefaultBranch } from '../lib/gitProject.js'
import { otherGitHubTransport, parseProjectFolder, prepareProjectFolder, ProjectFolderError, RepositoryAccessError } from '../lib/projectFolder.js'
import { revokeGitHubToken, swarmGitPrepareEnv } from '../lib/swarmGitHub.js'
-import { identifier, object } from './wire.js'
+import { identifier, object, SuperTabHostError } from './wire.js'
+import { decided } from './github.js'
import { repairSwarmCloneTrust, swarmCloneRoot } from './cloneTrust.js'
type Json = Record<string, unknown>
@@ -15,13 +16,19 @@ export interface StartGitHub { token: string; repository: string; requester: str
const NO_APP_ON_TEAM = 'This Harness server has no GitHub App, and a Harness Team account has no git login of its own, so it cannot fetch '
+ 'the project. Ask the server\'s administrator to set up the Harness GitHub App (docs/swarm-github-app.md).'
+/** A start this machine could not prepare yet, as the Harness server gave no answer (a timeout, a 5xx, no connection), which
+ * decides nothing: its host tries again on its next pass, and fails the start only once it has kept failing for as long as
+ * a refused start (superTabs/machines.ts). Diego's Mac, 2026-10-30: one backend timeout on the token call failed a start. */
+export class PrepareRetryError extends Error {}
+
/**
* Whether a queued start on this machine is prepared with the Harness GitHub App's token instead of this machine's own git
* login, and that token (docs/swarm-github-app.md). Asked of the backend, which knows whose project it is: another person's
* project gets a token that only reads its repository; the machine owner's own project answers none, and the owner's login
* prepares it as before. A Harness Team account ([team]) has no login, so it always asks for one. A server without the app
* gives none: an owner's machine prepares as before, as its agents then also use its own login; a team account cannot.
- * Anything the backend refuses is thrown, to fail the start: never this machine's own login instead.
+ * Anything the backend refuses is thrown, to fail the start; a call it gave no answer to, as a PrepareRetryError: never this
+ * machine's own login instead.
*/
export async function startGitHub(core: CoreApi, row: Json, start: Json, deps: { backend: Backend; team: boolean }): Promise<StartGitHub | null> {
const machineId = core.daemon.machineId()
@@ -34,8 +41,10 @@ export async function startGitHub(core: CoreApi, row: Json, start: Json, deps: {
try {
answer = object(await deps.backend(core, 'POST', `/api/super-tabs/hosts/${machineId}/starts/${row.tabId}/${start.id}/github-token`, deps.team ? { always: true } : {}))
} catch (error) {
- const said = error instanceof Error ? error.message : String(error)
- throw new Error(`This machine could not get a Harness GitHub App token for this project, so it was not prepared with its owner's own GitHub login either: ${said}`)
+ const said = `This machine could not get a Harness GitHub App token for this project, so it was not prepared with its owner's own GitHub login either: ${error instanceof Error ? error.message : String(error)}`
+ // Only an answer the backend decided is a refusal, by the rule a session's own token follows (github.ts `decided`).
+ if (error instanceof SuperTabHostError && decided(error)) throw new Error(said)
+ throw new PrepareRetryError(error instanceof SuperTabHostError ? `${said} (${error.code})` : said)
}
if (answer.github === false && !deps.team) return null
const requester = object(answer.requestedBy).email