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.

feat(os): Sign in to GitHub puts a GitHub CLI that is gone back first (#1816)

Bubu-Dudu-52hz116f0db1dfbf
diff --git a/cli/src/services/osDevice.spec.ts b/cli/src/services/osDevice.spec.ts
index 67991cc06..0ae5b8f44 100644
--- a/cli/src/services/osDevice.spec.ts
+++ b/cli/src/services/osDevice.spec.ts
@@ -362,14 +362,74 @@ describe('the Harness OS computer, managed from the phone', () => {
expect(exec).toHaveBeenCalledWith(GH, ['auth', 'logout', '--hostname', 'github.com', '--user', 'octo'], { timeout: 15_000 })
})
- it('says so on a computer without the GitHub CLI', async () => {
- const { ask, githubSignIn } = github({ exists: (path) => path !== GH })
- const missing = { error: 'GH_MISSING', message: 'The GitHub CLI is not on this computer yet: update it first.' }
- expect(await ask({ action: 'github' })).toEqual(missing)
- expect(await ask({ action: 'github', state: 'sign_in' })).toEqual(missing)
+ /** A computer whose gh is gone, and the setup's install-gh answering [answer] (and putting gh back on `installed`). */
+ function withoutGh(answer: () => Promise<string>) {
+ let there = false
+ let installed!: () => void
+ const done = new Promise<void>((resolve) => { installed = resolve })
+ const base = github()
+ const exec = vi.fn(async (file: string, args: readonly string[], _options?: { timeout: number }) => {
+ if (file === 'sudo') {
+ try {
+ const out = await answer()
+ there = out.includes('"installed": true')
+ return out
+ } finally { installed() }
+ }
+ return base.exec(file, args)
+ })
+ const githubSignIn = vi.fn(() => base.login.run)
+ const { ask } = service({ exec, githubSignIn, exists: (path) => path !== GH || there })
+ return { ask, exec, githubSignIn, done, signInAs: base.signInAs }
+ }
+
+ it('puts a GitHub CLI that is gone back from the OS, then signs in, answering at once', async () => {
+ vi.spyOn(console, 'log').mockImplementation(() => {})
+ let finish!: (out: string) => void
+ const { ask, exec, githubSignIn, done } = withoutGh(() => new Promise((resolve) => { finish = resolve }))
+ expect(await ask({ action: 'github' })).toEqual({ login: null, missing: true })
+ expect(await ask({ action: 'github', state: 'sign_in' })).toEqual({ login: null, installing: true })
+ // Asked again while it installs: the same install.
+ expect(await ask({ action: 'github', state: 'sign_in' })).toEqual({ login: null, installing: true })
+ expect(exec).toHaveBeenCalledWith('sudo', ['-n', '/usr/bin/python3', SETUP, 'install-gh'], { timeout: 300_000 })
+ expect(exec.mock.calls.filter(([file]) => file === 'sudo')).toHaveLength(1)
+ finish('{"installed": true}')
+ await done
+ await vi.waitFor(async () => expect(await ask({ action: 'github' }))
+ .toEqual({ login: null, signingIn: { code: 'AB12-CD34', url: 'https://github.com/login/device' } }))
+ expect(githubSignIn).toHaveBeenCalledTimes(1)
+ })
+
+ it('puts the GitHub CLI back without signing in again where its sign-in stayed', async () => {
+ vi.spyOn(console, 'log').mockImplementation(() => {})
+ const { ask, githubSignIn, done, signInAs } = withoutGh(async () => '{"installed": true}')
+ signInAs('octo')
+ await ask({ action: 'github', state: 'sign_in' })
+ await done
+ await vi.waitFor(async () => expect(await ask({ action: 'github' })).toEqual({ login: 'octo' }))
expect(githubSignIn).not.toHaveBeenCalled()
})
+ it('says why the GitHub CLI could not be put back', async () => {
+ vi.spyOn(console, 'log').mockImplementation(() => {})
+ const { ask, githubSignIn, done } = withoutGh(async () => '{"installed": false, "message": "The GitHub CLI could not be installed: error: failed retrieving file"}')
+ await ask({ action: 'github', state: 'sign_in' })
+ await done
+ await vi.waitFor(async () => expect(await ask({ action: 'github' }))
+ .toEqual({ login: null, missing: true, signInError: 'The GitHub CLI could not be installed: error: failed retrieving file' }))
+ expect(githubSignIn).not.toHaveBeenCalled()
+ })
+
+ it('asks for an update on an OS from before install-gh, whose sudo refuses it', async () => {
+ vi.spyOn(console, 'log').mockImplementation(() => {})
+ vi.spyOn(console, 'error').mockImplementation(() => {})
+ const { ask, done } = withoutGh(async () => { throw new Error('sudo: a password is required') })
+ await ask({ action: 'github', state: 'sign_in' })
+ await done.catch(() => {})
+ await vi.waitFor(async () => expect(await ask({ action: 'github' }))
+ .toEqual({ login: null, missing: true, signInError: 'The GitHub CLI could not be installed here: update this computer, then try again.' }))
+ })
+
it('finds the code and the page in what gh prints', () => {
expect(deviceCodeOf('! First copy your one-time code: B4B9-2B70\nOpen this URL to continue in your web browser: https://github.com/login/device\n'))
.toEqual({ code: 'B4B9-2B70', url: 'https://github.com/login/device' })
diff --git a/cli/src/services/osDevice.ts b/cli/src/services/osDevice.ts
index a0ad96be3..1f509c9c7 100644
--- a/cli/src/services/osDevice.ts
+++ b/cli/src/services/osDevice.ts
@@ -39,6 +39,8 @@ export const PASSWORD_TIMEOUT_MS = 90_000
/** The GitHub CLI, in the image and a dependency of the harness-os package (os/tools/build-package.py). */
export const GH = '/usr/bin/gh'
export const GH_TIMEOUT_MS = 15_000
+/** Putting the GitHub CLI back: a download of some 15 MB, and os/setup.py's own bound with room to answer. */
+export const GH_INSTALL_TIMEOUT_MS = 300_000
/** How long a GitHub sign-in waits for its code to be entered: GitHub's device codes last 15 minutes. */
export const GITHUB_SIGN_IN_MS = 15 * 60_000
const ACTIONS = ['update', 'wifi_scan', 'wifi_join', 'wifi_result', 'sign_out', 'password', 'ssh', 'github']
@@ -237,6 +239,8 @@ export function startOsDevice(_core: CoreApi, deps: OsDeviceDeps = {}): ServiceR
let signInError: string | null = null
/** A sign-in started and not yet showing its code. */
let starting: Promise<void> | null = null
+ /** The GitHub CLI being put back, for a sign-in. */
+ let installing: Promise<void> | null = null
const harnessOs = (): boolean => env.HARNESS_OS === '1' || exists(LIVE_UPDATE)
@@ -323,19 +327,25 @@ export function startOsDevice(_core: CoreApi, deps: OsDeviceDeps = {}): ServiceR
/** The GitHub account this computer's `gh` (and git, through it) uses, for the agents to clone, push and
* open pull requests with. `{ login }`, with the code to enter while a sign-in waits for it. */
const githubStatus = async (): Promise<Payload> => {
- if (!exists(GH)) return { error: 'GH_MISSING', message: 'The GitHub CLI is not on this computer yet: update it first.' }
+ const failed = signInError && !signingIn ? { signInError } : {}
+ // Gone (removed by hand, say): signed in to nothing, and put back by the next Sign in.
+ if (!exists(GH)) return { login: null, ...(installing ? { installing: true } : { missing: true, ...failed }) }
const login = githubLoginOf(await exec(GH, ['auth', 'status', '--json', 'hosts'], { timeout: GH_TIMEOUT_MS }))
return {
login,
...(signingIn && !login ? { signingIn: { code: signingIn.code, url: signingIn.url } } : {}),
- ...(signInError && !login && !signingIn ? { signInError } : {}),
+ ...(!login ? failed : {}),
}
}
/** A device sign-in started, and answered with its code once `gh` shows it; the one already waiting
- * answered again. Signed in, git is set up to use the account (`gh auth setup-git`). */
+ * answered again. Signed in, git is set up to use the account (`gh auth setup-git`). Without `gh`, it is
+ * installed first, then the sign-in starts: answered at once `{ installing }`, for the phone to follow. */
const githubSignIn = async (): Promise<Payload> => {
- if (!exists(GH)) return githubStatus()
+ if (!exists(GH)) {
+ installing ??= installGh().finally(() => { installing = null })
+ return githubStatus()
+ }
// A second ask while the first waits for its code (a phone asking again): the same sign-in, not another.
if (!signingIn) {
starting ??= startGithubSignIn().finally(() => { starting = null })
@@ -344,6 +354,29 @@ export function startOsDevice(_core: CoreApi, deps: OsDeviceDeps = {}): ServiceR
return githubStatus()
}
+ /** The GitHub CLI put back from Harness OS's own repositories (os/setup.py install-gh), then the sign-in
+ * it was installed for started. Longer than a phone waits on one answer, so never awaited by one. */
+ const installGh = async (): Promise<void> => {
+ signInError = null
+ console.log('[os-device] github: installing the GitHub CLI')
+ try {
+ const parsed: unknown = JSON.parse(await exec('sudo', ['-n', PYTHON, SETUP, 'install-gh'], { timeout: GH_INSTALL_TIMEOUT_MS }))
+ if (!record(parsed) || parsed.installed !== true) {
+ signInError = record(parsed) && typeof parsed.message === 'string' ? parsed.message : 'The GitHub CLI could not be installed.'
+ return
+ }
+ } catch (error) {
+ // An OS from before install-gh: sudo refuses it.
+ console.error('[os-device] github: install-gh failed:', short(error))
+ signInError = 'The GitHub CLI could not be installed here: update this computer, then try again.'
+ return
+ }
+ // Removing the package left its sign-in (~/.config/gh): back, it may be signed in already.
+ if ((await githubStatus().catch(() => ({ login: null }))).login) return
+ starting ??= startGithubSignIn().finally(() => { starting = null })
+ await starting.catch((error: unknown) => { signInError = short(error) })
+ }
+
/** `gh auth login` started and kept once it shows its code; failed when it shows none in GH_TIMEOUT_MS. */
const startGithubSignIn = async (): Promise<void> => {
const run = startSignIn()
diff --git a/os/root/etc/sudoers.d/20-harness-network b/os/root/etc/sudoers.d/20-harness-network
index 185657cd2..912e26cf3 100644
--- a/os/root/etc/sudoers.d/20-harness-network
+++ b/os/root/etc/sudoers.d/20-harness-network
@@ -11,3 +11,6 @@
# first), and SSH on, off or how it is.
%wheel ALL=(root) NOPASSWD: /usr/bin/python3 /usr/lib/harness-os/setup.py sign-out, /usr/bin/python3 /usr/lib/harness-os/setup.py password
%wheel ALL=(root) NOPASSWD: /usr/bin/python3 /usr/lib/harness-os/setup.py ssh status, /usr/bin/python3 /usr/lib/harness-os/setup.py ssh on, /usr/bin/python3 /usr/lib/harness-os/setup.py ssh off
+# The GitHub CLI put back from the computer's own repositories, for the phone's Sign in to GitHub: that one
+# package, never one the caller names.
+%wheel ALL=(root) NOPASSWD: /usr/bin/python3 /usr/lib/harness-os/setup.py install-gh
diff --git a/os/setup.py b/os/setup.py
index 28d5d1274..901433933 100644
--- a/os/setup.py
+++ b/os/setup.py
@@ -822,6 +822,11 @@ SIGN_OUT_UNIT = 'harness-sign-out'
INITIAL_ACCOUNT = STATE / 'initial-account.json'
CMDLINE = Path('/proc/cmdline')
SSHD = 'sshd.service'
+# The GitHub CLI and its package (packages.x86_64), put back by install-gh: a download of some 15 MB, each
+# of its two tries bounded so both fit the daemon's wait (cli services/osDevice.ts GH_INSTALL_TIMEOUT_MS).
+GH = Path('/usr/bin/gh')
+GH_PACKAGE = 'github-cli'
+GH_INSTALL_TIMEOUT = 120
# A phone sets this computer up next (network.py UNLOCKED_BY_PHONE): the screen's Wi-Fi page keeps
# off the radio the hotspot needs, until a key there.
PHONE_SETS_UP = Path('/run/harness-os/unlocked-by-phone')
@@ -1059,6 +1064,26 @@ def ssh(action='status'):
'user': USER, 'addresses': addresses}
+def install_gh():
+ """The GitHub CLI put back where it is gone: the package the image installs (packages.x86_64), from this
+ computer's own pinned repositories, for the phone's Sign in to GitHub (cli services/osDevice.ts).
+ Nothing else: no package named by the caller, no upgrade of anything installed."""
+ if GH.exists():
+ return {'installed': True}
+ log('installing the GitHub CLI')
+ # Its package databases first as they are, then refreshed: the repositories are one dated snapshot
+ # (/etc/pacman.conf), so a refresh reads the same packages the computer was installed from.
+ for refresh in ([], ['-y']):
+ done = subprocess.run(['pacman', '-S', *refresh, '--needed', '--noconfirm', GH_PACKAGE],
+ capture_output=True, text=True, timeout=GH_INSTALL_TIMEOUT)
+ if done.returncode == 0 and GH.exists():
+ log('installed the GitHub CLI')
+ return {'installed': True}
+ reason = (done.stderr.strip() or done.stdout.strip()).splitlines()[-1:] or ['pacman failed']
+ log(f'the GitHub CLI did not install: {reason[0]}')
+ return {'installed': False, 'message': 'The GitHub CLI could not be installed: ' + reason[0][:200]}
+
+
def factory_reset(keep_wifi=False, keep_ssh=False):
"""This computer as just installed: off its account and a new computer to it (a new computer id and
device key), everything of the person gone — /home/me as the installer made it, so its projects,
@@ -1101,7 +1126,7 @@ def factory_reset(keep_wifi=False, keep_ssh=False):
def main():
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument('command', nargs='?', choices=['watch', 'now', 'name', 'join', 'reset', 'sign-out',
- 'password', 'ssh'], default='watch')
+ 'password', 'ssh', 'install-gh'], default='watch')
parser.add_argument('action', nargs='?', choices=['status', 'on', 'off'], help='ssh: status, on or off')
parser.add_argument('--keep-wifi', action='store_true', help='reset, sign-out: keep the saved Wi-Fi')
parser.add_argument('--keep-ssh', action='store_true', help='reset: leave SSH as it is')
@@ -1150,6 +1175,13 @@ def main():
if arguments.command == 'ssh':
print(json.dumps(ssh(arguments.action or 'status')))
return
+ if arguments.command == 'install-gh':
+ try:
+ print(json.dumps(install_gh()))
+ except (OSError, subprocess.SubprocessError) as error:
+ log(f'the GitHub CLI did not install: {error}')
+ print(json.dumps({'installed': False, 'message': 'The GitHub CLI could not be installed. Try again.'}))
+ return
if arguments.command == 'join':
# From the daemon, through sudo: the Wi-Fi as JSON on stdin, never in argv.
try:
diff --git a/os/tests/test_setup.py b/os/tests/test_setup.py
index 516743280..9c2139a78 100644
--- a/os/tests/test_setup.py
+++ b/os/tests/test_setup.py
@@ -482,6 +482,44 @@ class Name(unittest.TestCase):
self.assertIn('127.0.1.1 studio\n', hosts)
+class InstallGh(unittest.TestCase):
+ def install(self, present, *answers):
+ """install_gh with gh [present] or not, pacman exiting with each of [answers] in turn (0 puts gh there)."""
+ calls = []
+ replies = iter(answers)
+ with tempfile.TemporaryDirectory() as folder:
+ gh = Path(folder) / 'gh'
+ if present:
+ gh.touch()
+ def run(command, **_):
+ calls.append(command)
+ code = next(replies)
+ if code == 0:
+ gh.touch()
+ return result(code, '', '' if code == 0 else 'error: failed retrieving file\n')
+ with patch.object(setup, 'GH', gh), patch.object(setup.subprocess, 'run', side_effect=run), \
+ patch.object(setup, 'log'):
+ return setup.install_gh(), calls
+
+ def test_present_it_installs_nothing(self):
+ self.assertEqual(self.install(True), ({'installed': True}, []))
+
+ def test_gone_it_is_put_back_from_the_computers_repositories(self):
+ answer, calls = self.install(False, 0)
+ self.assertEqual(answer, {'installed': True})
+ self.assertEqual(calls, [['pacman', '-S', '--needed', '--noconfirm', 'github-cli']])
+
+ def test_with_its_databases_refreshed_from_the_same_snapshot_when_they_fall_short(self):
+ answer, calls = self.install(False, 1, 0)
+ self.assertEqual(answer, {'installed': True})
+ self.assertEqual(calls[1], ['pacman', '-S', '-y', '--needed', '--noconfirm', 'github-cli'])
+
+ def test_says_why_it_could_not(self):
+ answer, _ = self.install(False, 1, 1)
+ self.assertEqual(answer, {'installed': False,
+ 'message': 'The GitHub CLI could not be installed: error: failed retrieving file'})
+
+
class Server:
def shutdown(self): pass
def server_close(self): pass