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.

Merge batch 11-02 b2310: #2637 (#2640)

0xken1565bd9c409453
diff --git a/cli/src/authCommand.spec.ts b/cli/src/authCommand.spec.ts
index 136d9525d..cf3133263 100644
--- a/cli/src/authCommand.spec.ts
+++ b/cli/src/authCommand.spec.ts
@@ -837,6 +837,41 @@ describe('a Harness session in place of SSO tokens', () => {
? { token: HNA, refreshToken: HNR, expiresIn: 3600, kind: 'computer', autonomousEnv: 'prod', harnessSession: true }
: { token: 'tok_renewed', refreshToken: 'refresh_2', expiresIn: 3600 }
+ // Dee's open-at-once (reviews of #2617): a kept profile is trusted on the machine id being the signed-in account's.
+ describe('a machine lookup that outlives its sign-in', () => {
+ const swapped = { accessToken: 'tok_next', refreshToken: 'refresh_next', signInEpoch: 'next@2', signInAcct: 'acct-next' }
+
+ it('a sign-out and a new sign-in while it is out: the old answer writes nothing', async () => {
+ const root = tempRoot()
+ seedSession(root, { overrides: { signInEpoch: 'first@1', signInAcct: 'acct-first', machineId: 'm_first' } })
+ const backend = await fakeBackend({
+ resolveComputer: () => {
+ // What the new account's sign-in leaves on disk, before the old lookup's answer comes back.
+ seedSession(root, { overrides: { ...swapped, machineId: undefined } })
+ return { machine: { machineId: 'm_of_the_old_account' } }
+ },
+ })
+ const result = await runAsync(root, ['login', '--json'], backend.base)
+ expect(result.stdout + result.stderr).not.toContain('SSO session disappeared')
+ expect(sessionOnDisk(root)).toMatchObject(swapped)
+ expect(sessionOnDisk(root)).not.toHaveProperty('machineId')
+ }, 30_000)
+
+ it('the same sign-in, refreshed while it is out: the answer is kept', async () => {
+ const root = tempRoot()
+ seedSession(root, { overrides: { signInEpoch: 'first@1', machineId: 'm_first' } })
+ const backend = await fakeBackend({
+ resolveComputer: () => {
+ seedSession(root, { overrides: { signInEpoch: 'first@1', accessToken: 'tok_refreshed', refreshToken: 'refresh_refreshed', machineId: 'm_first' } })
+ return { machine: { machineId: 'm_new' } }
+ },
+ })
+ const result = await runAsync(root, ['login', '--json'], backend.base)
+ expect(result.status, result.stderr).toBe(0)
+ expect(sessionOnDisk(root)).toMatchObject({ accessToken: 'tok_refreshed', machineId: 'm_new', signInEpoch: 'first@1' })
+ }, 30_000)
+ })
+
it('a sign-in from before the upgrade stays signed in, and its next renewal moves it to a Harness session', async () => {
const root = tempRoot()
// Exactly what today's CLI writes after an SSO sign-in from the desktop app, its access token about to run out.
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 65d88b670..ac4e01d85 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -50,7 +50,7 @@ import { infoBlockFooter } from './lib/infoBlockFooter.js'
import { flashCommand } from './lib/flash.js'
import { awaitLoginCallback, extractCallbackParams, LOGIN_TIMEOUT_MESSAGE, type LoginCallbackParams } from './lib/loginCallback.js'
import { ancestorsOnce, removeSignIn, SIGN_OUT_BUTTON_FLAG, signOutAsked, type Asker, type SignOutAsked } from './lib/signOut.js'
-import { AUTH_DIR, AuthSessionError, AuthSessionManager, harnessSessionRequest, knownSsoClientId, newSignInEpoch, readAuthSession, readSignedOut, ssoClientIdFor, updateAuthSession, writeAuthSession, type AuthSession } from './lib/authSession.js'
+import { AUTH_DIR, AuthSessionError, AuthSessionManager, harnessSessionRequest, knownSsoClientId, recordMachineId, newSignInEpoch, readAuthSession, readSignedOut, ssoClientIdFor, updateAuthSession, writeAuthSession, type AuthSession } from './lib/authSession.js'
import { revokeHarnessRefresh } from './team/handoff.js'
import { TEAM_RELAY_GRANT_FILE } from './team/relay.js'
import { handOffToGrid } from './lib/gridHandoff.js'
@@ -320,8 +320,8 @@ async function resolveComputerMachine(signal?: AbortSignal): Promise<AuthSession
if (!machineId) throw new Error('Backend did not return a machine id for this computer')
// Refresh can atomically replace the session while this request is in flight. Always merge the
// machine id into the newest file, under the lock, so a stale caller never rolls its rotated refresh
- // token back nor writes over a signed-out mark.
- const next = await updateAuthSession((latest) => latest.machineId === machineId ? null : { ...latest, machineId, updatedAt: Date.now() })
+ // token back nor writes over a signed-out mark; and only into the sign-in it was asked for.
+ const next = await recordMachineId(machineId, current.signInEpoch)
if (!next) throw new Error('SSO session disappeared while resolving this computer')
return next
}
diff --git a/cli/src/lib/authSession.spec.ts b/cli/src/lib/authSession.spec.ts
index cacc9bb6f..3680e879f 100644
--- a/cli/src/lib/authSession.spec.ts
+++ b/cli/src/lib/authSession.spec.ts
@@ -54,11 +54,41 @@ describe('sign-in epoch', () => {
}))))
const manager = new AuthSessionManager('https://api.example.test')
await manager.accessToken()
- manager.updateMachineId('machine-2')
+ await manager.updateMachineId('machine-2', epoch)
expect(readAuthSession()).toMatchObject({ accessToken: 'refreshed-access', machineId: 'machine-2', signInEpoch: epoch, signInAcct: 'acct-1' })
expect(newSignInEpoch()).not.toBe(epoch)
})
+ describe('a machine id found under one sign-in', () => {
+ const manager = () => new AuthSessionManager('https://api.example.test')
+
+ it('is written into that sign-in, refreshed or not', async () => {
+ writeAuthSession({ ...baseSession(), signInEpoch: 'first@1' })
+ await manager().updateMachineId('machine-2', 'first@1')
+ expect(readAuthSession()).toMatchObject({ machineId: 'machine-2', signInEpoch: 'first@1' })
+ })
+
+ it('is dropped by a new sign-in', async () => {
+ writeAuthSession({ ...baseSession(), machineId: 'machine-new', signInEpoch: 'next@2' })
+ await manager().updateMachineId('machine-old', 'first@1')
+ expect(readAuthSession()).toMatchObject({ machineId: 'machine-new', signInEpoch: 'next@2' })
+ })
+
+ it('is dropped by a new sign-in when the lookup started with no epoch', async () => {
+ writeAuthSession({ ...baseSession(), machineId: 'machine-new', signInEpoch: 'next@2' })
+ await manager().updateMachineId('machine-old', undefined)
+ expect(readAuthSession()).toMatchObject({ machineId: 'machine-new' })
+ })
+
+ it('is kept when the session from before epochs was only given one meanwhile', async () => {
+ const { signInEpoch: _none, ...old } = { ...baseSession(), signInEpoch: undefined }
+ writeAuthSession(old)
+ await ensureSignInEpoch()
+ await manager().updateMachineId('machine-2', undefined)
+ expect(readAuthSession()).toMatchObject({ machineId: 'machine-2' })
+ })
+ })
+
it('gives a session from before epochs an adopted one, once, keeping its tokens', async () => {
writeAuthSession(baseSession())
const epoch = await ensureSignInEpoch()
@@ -147,7 +177,7 @@ describe('AuthSessionManager', () => {
it('keeps a team sign-in\'s kind, and reads no other', async () => {
writeAuthSession({ ...baseSession(), kind: 'team' })
expect(readAuthSession()?.kind).toBe('team')
- new AuthSessionManager('http://backend.invalid').updateMachineId('machine-2')
+ new AuthSessionManager('http://backend.invalid').updateMachineId('machine-2', undefined)
expect(readAuthSession()).toMatchObject({ kind: 'team', machineId: 'machine-2' })
await writeFile(AUTH_SESSION_FILE, JSON.stringify({ ...baseSession(), kind: 'admin' }))
expect(readAuthSession()).not.toHaveProperty('kind')
diff --git a/cli/src/lib/authSession.ts b/cli/src/lib/authSession.ts
index ee000c7c8..402dc662f 100644
--- a/cli/src/lib/authSession.ts
+++ b/cli/src/lib/authSession.ts
@@ -212,6 +212,20 @@ export function updateAuthSession(change: (latest: AuthSession) => AuthSession |
})
}
+/**
+ * A machine id the backend named for the sign-in [startedUnder] (its `signInEpoch` when the lookup began), merged into the
+ * newest session only if that is still the same sign-in. A lookup can outlive a sign-out and a new sign-in, and its answer is
+ * the old account's machine: dropped, as a later lookup finds the new one. A refresh keeps the epoch, so it keeps the answer. A
+ * session from before epochs may only have been given one meanwhile (adopted): still that sign-in. The newest session; null
+ * when there is none, or it is marked signed out.
+ */
+export function recordMachineId(machineId: string, startedUnder: string | undefined): Promise<AuthSession | null> {
+ return updateAuthSession((latest) => {
+ const same = latest.signInEpoch === startedUnder || (startedUnder === undefined && signInOf(latest.signInEpoch)?.adopted === true)
+ return !same || latest.machineId === machineId ? null : { ...latest, machineId, updatedAt: Date.now() }
+ })
+}
+
/** The first wait before a refused refresh is tried again, and the longest. The app asks `auth status` often,
* and each ask must not be a refresh request. */
const RETRY_FIRST_MS = 60_000
@@ -453,7 +467,7 @@ export class AuthSessionManager {
})
}
- async updateMachineId(machineId: string): Promise<void> {
- await updateAuthSession((current) => current.machineId === machineId ? null : { ...current, machineId, updatedAt: Date.now() })
+ async updateMachineId(machineId: string, startedUnder: string | undefined): Promise<void> {
+ await recordMachineId(machineId, startedUnder)
}
}