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.

Swarm sessions get their task's GitHub token as soon as the host sees the task

bitcoinbuilder9782fb6d2d71
diff --git a/cli/src/superTabs/github.spec.ts b/cli/src/superTabs/github.spec.ts
index 57a98b11f..330e029a3 100644
--- a/cli/src/superTabs/github.spec.ts
+++ b/cli/src/superTabs/github.spec.ts
@@ -242,6 +242,60 @@ describe('keeping gh\'s token fresh', () => {
expect(hosts()).toContain('ghs_fixtureToken0000009')
})
+ // Mac Auto, 2026-10-29: the gh login stayed empty until the minute's pass, so a Builder's first `gh auth status` said
+ // "You are not logged into any GitHub hosts" and it stopped.
+ it('gives a task its token as soon as the host reads it, once per task, and only while it runs', async () => {
+ let work: Array<Record<string, unknown>> = []
+ let refuseToken = false
+ const { core, backend, hosts } = setup(method => method === 'GET' ? ok(hostForm([row('tab-1', 'agent-1')], { assignments: work }))
+ : refuseToken ? refuse(503, 'GITHUB_UNAVAILABLE') : issued('ghs_fixtureToken0000001', '2099-01-01T00:00:00.000Z'))
+ const sources = sourcesOf(core)
+ const refresher = new GitHubTokenRefresher(core, sources, { intervalMs: 3600_000 })
+ const stopWatching = sources().feed.watch(read => refresher.assigned(read))
+ const asked = () => backend.mock.calls.filter(c => c[0] === 'POST').length
+ // Not running: a read changes nothing.
+ work = [{ tabId: 'tab-1', assignmentId: 'attempt-1', harnessId: 'harness-1' }]
+ await sources().feed.refresh()
+ expect(asked()).toBe(0)
+ refresher.start()
+ await refresher.tick()
+ expect(asked()).toBe(1)
+ work = [{ tabId: 'tab-1', assignmentId: 'attempt-2', harnessId: 'harness-1' }]
+ await sources().feed.refresh()
+ await refresher.tick()
+ expect(asked()).toBe(2)
+ expect(hosts()).toContain('ghs_fixtureToken0000001')
+ // The same task read again asks nothing more; nor does a task whose token could not be had, until the minute's pass.
+ await sources().feed.refresh()
+ await refresher.tick()
+ expect(asked()).toBe(2)
+ refuseToken = true
+ work = [{ tabId: 'tab-1', assignmentId: 'attempt-3', harnessId: 'harness-1' }]
+ await sources().feed.refresh()
+ await refresher.tick()
+ expect(asked()).toBe(3)
+ await sources().feed.refresh()
+ await new Promise(resolve => setImmediate(resolve))
+ expect(asked()).toBe(3)
+ // A session that does not use the app is never looked at for it.
+ refresher.stop()
+ stopWatching()
+ })
+
+ it('reads on when a watcher of the host form throws, and stops telling one that stopped watching', async () => {
+ const { core } = setup(() => ok(hostForm([])))
+ const feed = sourcesOf(core)().feed
+ const thrower = vi.fn(() => { throw new Error('watcher broke') }), told = vi.fn()
+ feed.watch(thrower)
+ const stop = feed.watch(told)
+ await expect(feed.refresh()).resolves.toMatchObject({ roster: [] })
+ expect(told).toHaveBeenCalledOnce()
+ stop()
+ await feed.refresh()
+ expect(told).toHaveBeenCalledOnce()
+ expect(thrower).toHaveBeenCalledTimes(2)
+ })
+
it('starts once, looks again on its interval, and stops', async () => {
vi.useFakeTimers()
try {
diff --git a/cli/src/superTabs/github.ts b/cli/src/superTabs/github.ts
index 0c5be5497..4c365f6f5 100644
--- a/cli/src/superTabs/github.ts
+++ b/cli/src/superTabs/github.ts
@@ -12,6 +12,7 @@ import type { CoreApi } from '../core/api.js'
import { swarmGitHubRoot, swarmGitHubSessionDir, writeSessionGitHub } from '../lib/swarmGitHub.js'
import { backend, identifier, object, SuperTabHostError } from './wire.js'
import type { SwarmSources } from './host.js'
+import type { HostRead } from './hostFeed.js'
type Json = Record<string, unknown>
type Session = NonNullable<ReturnType<CoreApi['agents']['byAgent']>>
@@ -90,6 +91,9 @@ export class GitHubTokenRefresher {
private timer: NodeJS.Timeout | null = null
private running: Promise<void> | null = null
private readonly primed = new Map<string, { assignmentId: string; expiresAt: number }>()
+ /** The assignment each session was last looked at for as soon as it arrived ([assigned]): one look per task, however
+ * often the host reads, even while its token cannot be had. */
+ private readonly asked = new Map<string, string>()
private readonly now: () => number
private readonly intervalMs: number
@@ -109,6 +113,27 @@ export class GitHubTokenRefresher {
if (this.timer) clearInterval(this.timer)
this.timer = null
this.primed.clear()
+ this.asked.clear()
+ }
+
+ /**
+ * The host read its Swarms' rows: a session started to use the app that has a task it holds no token for is looked at
+ * now, not at the next minute's pass. Its gh login was empty for up to a minute after the task arrived, so a Builder's
+ * first `gh auth status` said "You are not logged into any GitHub hosts" and it stopped (Mac Auto, 2026-10-29). Once per
+ * task: a token that cannot be had is asked for again at the minute's pass, as before. Only while the refresher runs.
+ */
+ assigned(read: HostRead): void {
+ if (!this.timer) return
+ let due = false
+ for (const session of this.core.agents.live()) {
+ const id = session.agentId
+ const task = read.assignments.find(row => row.harnessId === id && identifier(row.assignmentId))
+ if (!task || this.primed.get(id)?.assignmentId === task.assignmentId || this.asked.get(id) === task.assignmentId) continue
+ if (!githubDir(this.core, session)) continue
+ this.asked.set(id, String(task.assignmentId))
+ due = true
+ }
+ if (due) void this.tick()
}
/** One look; a look still running is not started twice. */
diff --git a/cli/src/superTabs/hostFeed.ts b/cli/src/superTabs/hostFeed.ts
index cb36a9660..ba684f80f 100644
--- a/cli/src/superTabs/hostFeed.ts
+++ b/cli/src/superTabs/hostFeed.ts
@@ -37,6 +37,8 @@ export class HostFeed {
private pushUntil = -Infinity
/** Told of a change since the last read began. */
private told = false
+ /** Told of every read the feed makes ([watch]). */
+ private readonly watchers = new Set<(read: HostRead) => void>()
constructor(private readonly core: CoreApi, readonly machineId: string, options: { now?: () => number; backend?: typeof backend } = {}) {
this.now = options.now ?? Date.now
@@ -55,6 +57,13 @@ export class HostFeed {
return this.refresh()
}
+ /** [watcher] is told of each read this feed makes, once it is held; returns what stops it. A watcher that throws is
+ * passed over: it never fails the read. */
+ watch(watcher: (read: HostRead) => void): () => void {
+ this.watchers.add(watcher)
+ return () => { this.watchers.delete(watcher) }
+ }
+
/** Whether the server pushes: its last word that it does is under HOST_PUSH_LEASE_MS old. */
get pushing(): boolean { return this.now() < this.pushUntil }
@@ -120,7 +129,11 @@ export class HostFeed {
this.cursor = answer.cursor
if (full) this.fullAt = startedAt
this.readAt = this.now()
- return this.snapshot()
+ const read = this.snapshot()
+ for (const watcher of this.watchers) {
+ try { watcher(read) } catch { /* A watcher's failure is its own. */ }
+ }
+ return read
}
/** Every Swarm's rows in one list each; a Swarm whose lists are not lists gives none, and never stops the others'. */
diff --git a/cli/src/superTabs/service.ts b/cli/src/superTabs/service.ts
index 35a863654..1d5a84432 100644
--- a/cli/src/superTabs/service.ts
+++ b/cli/src/superTabs/service.ts
@@ -219,7 +219,7 @@ export interface SuperTabServiceOptions {
/** The Swarm cards' reader (superTabs/cards.ts), for a spec. */
cards?: (core: CoreApi, machineId: string) => SwarmCards
/** What keeps Swarm sessions' GitHub tokens fresh (superTabs/github.ts), for a spec. */
- githubTokens?: (core: CoreApi) => Pick<GitHubTokenRefresher, 'start' | 'stop'>
+ githubTokens?: (core: CoreApi) => Pick<GitHubTokenRefresher, 'start' | 'stop'> & Partial<Pick<GitHubTokenRefresher, 'assigned'>>
/** What reports the Harness Team account's own engines to its owner (team/engines.ts), for a spec. */
teamEngines?: () => Pick<TeamEngineReporter, 'start' | 'stop'>
/** What sets up the Harness Team account when Desktop asks (team/autoSetup.ts), for a spec. */
@@ -245,7 +245,11 @@ export function superTabService(core: CoreApi, options: SuperTabServiceOptions =
let held: SwarmSources | null = null
const sources = (): SwarmSources => {
const machine = core.daemon.machineId()
- if (held?.feed.machineId !== machine) held = { feed: new HostFeed(core, machine), reads: new SwarmReads(core) }
+ if (held?.feed.machineId !== machine) {
+ held = { feed: new HostFeed(core, machine), reads: new SwarmReads(core) }
+ // A task's GitHub token is fetched as the host first sees the task, not at the refresher's next minute (github.ts).
+ held.feed.watch(read => tokens.assigned?.(read))
+ }
return held
}
let host: SuperTabHost | null = null