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(cli): the phone checks a Harness OS computer for updates now (#1943)

Bubu-Dudu-52hz3523017e4653
diff --git a/cli/src/services/osDevice.spec.ts b/cli/src/services/osDevice.spec.ts
index 0ae5b8f44..67bd50543 100644
--- a/cli/src/services/osDevice.spec.ts
+++ b/cli/src/services/osDevice.spec.ts
@@ -3,7 +3,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
import { encryptDownFrame, encryptRpcResult } from '../lib/e2ee/applicationFrames.js'
import { fakeCore } from '../testing/fakeCore.js'
import {
- activeSsid, CMDLINE, deviceCodeOf, execBounded, GH, githubDeviceSignIn, githubLoginOf, joinRequest, LIVE_UPDATE, MAX_OUTPUT, nmcliFields, OS_DEVICE_REQUESTS, SETUP, SETUP_RESULT, spawnDetached,
+ activeSsid, CHECK_NOW, CMDLINE, deviceCodeOf, execBounded, GH, githubDeviceSignIn, githubLoginOf, joinRequest, LIVE_UPDATE, MAX_OUTPUT, nmcliFields, OS_DEVICE_REQUESTS, SETUP, SETUP_RESULT, spawnDetached,
startOsDevice, UPDATE_BUSY_MS, updateOf, wifiNetworks, type OsDeviceDeps,
} from './osDevice.js'
@@ -77,7 +77,7 @@ describe('the Harness OS computer, managed from the phone', () => {
cli: { running: '0.9.1', available: true, next: null },
},
},
- wifi: { ssid: 'Home: 5G' }, hotspot: 'Harness-BD19', encrypted: true, features: ['sign_out', 'password', 'ssh', 'github'],
+ wifi: { ssid: 'Home: 5G' }, hotspot: 'Harness-BD19', encrypted: true, features: ['sign_out', 'password', 'ssh', 'github', 'check_update'],
})
expect(exec).toHaveBeenCalledWith('/usr/bin/python3', [LIVE_UPDATE, 'status'], { timeout: 15_000 })
expect(exec).toHaveBeenCalledWith('/usr/bin/python3', [SETUP, 'name'], { timeout: 10_000 })
@@ -88,8 +88,8 @@ describe('the Harness OS computer, managed from the phone', () => {
it('still answers on Harness OS when its update tool or nmcli fails', async () => {
const failing = (reason: unknown) => service({ exec: vi.fn(async () => { throw reason }) }).ask({ action: 'status' })
- expect(await failing(new Error('Command failed: python3\nTraceback ...'))).toEqual({ harnessOs: true, update: { available: false, error: 'Command failed: python3' }, wifi: { ssid: null }, hotspot: null, encrypted: true, features: ['sign_out', 'password', 'ssh', 'github'] })
- expect(await failing('killed')).toEqual({ harnessOs: true, update: { available: false, error: 'killed' }, wifi: { ssid: null }, hotspot: null, encrypted: true, features: ['sign_out', 'password', 'ssh', 'github'] })
+ expect(await failing(new Error('Command failed: python3\nTraceback ...'))).toEqual({ harnessOs: true, update: { available: false, error: 'Command failed: python3' }, wifi: { ssid: null }, hotspot: null, encrypted: true, features: ['sign_out', 'password', 'ssh', 'github', 'check_update'] })
+ expect(await failing('killed')).toEqual({ harnessOs: true, update: { available: false, error: 'killed' }, wifi: { ssid: null }, hotspot: null, encrypted: true, features: ['sign_out', 'password', 'ssh', 'github', 'check_update'] })
const notJson = service({ exec: vi.fn(async () => 'not json') })
expect(await notJson.ask({ action: 'status' })).toMatchObject({ update: { available: false, error: expect.stringContaining('JSON') } })
const notObject = service({ exec: vi.fn(async () => '[1]') })
@@ -191,6 +191,58 @@ describe('the Harness OS computer, managed from the phone', () => {
expect(await ask({ action: 'update' })).toEqual({ started: true })
})
+ describe('checking for updates now, from the phone', () => {
+ /** A computer whose check is held until [done] settles it. */
+ function holding() {
+ let settle!: (error?: Error) => void
+ const { ask, exec } = service()
+ const base = exec.getMockImplementation()!
+ exec.mockImplementation(async (file: string, args: readonly string[]) => {
+ if (args[0] !== '-c') return base(file, args)
+ await new Promise<void>((resolve, reject) => { settle = (error) => error ? reject(error) : resolve() })
+ return ''
+ })
+ const done = async (error?: Error) => { settle(error); await new Promise((resolve) => setImmediate(resolve)) }
+ return { ask, exec, done }
+ }
+
+ it('runs the timer\'s check with the OS release asked now, bounded, and says so until it is done', async () => {
+ vi.spyOn(console, 'log').mockImplementation(() => {})
+ const { ask, exec, done } = holding()
+ expect(await ask({ action: 'check_update' })).toEqual({ started: true })
+ expect(exec).toHaveBeenCalledWith('/usr/bin/python3', ['-c', CHECK_NOW], { timeout: 360_000 })
+ expect(CHECK_NOW).toContain('live_update.check(force_system=True)')
+ expect(await ask({ action: 'status' })).toMatchObject({ update: { checking: true } })
+ await done()
+ const status = await ask({ action: 'status' }) as { update: Record<string, unknown> }
+ expect(status.update.checking).toBeUndefined()
+ expect(status.update.checkError).toBeUndefined()
+ })
+
+ it('runs one check at a time', async () => {
+ vi.spyOn(console, 'log').mockImplementation(() => {})
+ const { ask, exec, done } = holding()
+ await ask({ action: 'check_update' })
+ expect(await ask({ action: 'check_update' })).toEqual({ started: true })
+ expect(exec.mock.calls.filter(([, args]) => args[0] === '-c')).toHaveLength(1)
+ await done()
+ })
+
+ it('says a check that failed in words, until the next one', async () => {
+ vi.spyOn(console, 'error').mockImplementation(() => {})
+ vi.spyOn(console, 'log').mockImplementation(() => {})
+ const { ask, done } = holding()
+ await ask({ action: 'check_update' })
+ await done(new Error('Command failed: python3 -c\nValueError: Could not check for updates.'))
+ expect(await ask({ action: 'status' })).toMatchObject({
+ update: { checkError: 'Could not check for updates. Check its internet, then try again.' },
+ })
+ await ask({ action: 'check_update' })
+ expect(await ask({ action: 'status' })).toMatchObject({ update: { checking: true } })
+ await done()
+ })
+ })
+
it('lists the networks it sees, the one in use first', async () => {
const { ask, exec } = service()
expect(await ask({ action: 'wifi_scan' })).toEqual({ networks: [{ ssid: 'Home: 5G', signal: 70, security: 'WPA2', active: true }, { ssid: 'Cafe', signal: 80, security: '', active: false }] })
diff --git a/cli/src/services/osDevice.ts b/cli/src/services/osDevice.ts
index 1f509c9c7..db0ccb8c1 100644
--- a/cli/src/services/osDevice.ts
+++ b/cli/src/services/osDevice.ts
@@ -31,6 +31,12 @@ export const HARNESS = '/usr/bin/harness'
export const NMCLI = 'nmcli'
/** How long an update counts as started: a second one inside it is refused BUSY. */
export const UPDATE_BUSY_MS = 10 * 60_000
+/** A check asked from the phone: the feeds read, a new hn and CLI downloaded and verified (as the
+ * harness-update.service timer's own check, bounded at 6 minutes there too). */
+export const CHECK_TIMEOUT_MS = 6 * 60_000
+/** The timer's check, with the OS release asked now instead of from its day-old answer (live_update.py
+ * check_system): every Harness OS since 0.1.7 has check(force_system=). */
+export const CHECK_NOW = `import sys; sys.path.insert(0, '/usr/lib/harness-os'); import live_update; live_update.check(force_system=True)`
export const STATUS_TIMEOUT_MS = 15_000
export const NMCLI_TIMEOUT_MS = 10_000
export const SCAN_TIMEOUT_MS = 20_000
@@ -43,7 +49,7 @@ export const GH_TIMEOUT_MS = 15_000
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']
+const ACTIONS = ['update', 'check_update', 'wifi_scan','wifi_join', 'wifi_result', 'sign_out', 'password', 'ssh', 'github']
/** The most output a command run here may print before it is stopped. */
export const MAX_OUTPUT = 1024 * 1024
@@ -241,6 +247,9 @@ export function startOsDevice(_core: CoreApi, deps: OsDeviceDeps = {}): ServiceR
let starting: Promise<void> | null = null
/** The GitHub CLI being put back, for a sign-in. */
let installing: Promise<void> | null = null
+ /** A check for updates asked from the phone, and why the last one did not finish. */
+ let checking: Promise<void> | null = null
+ let checkError: string | null = null
const harnessOs = (): boolean => env.HARNESS_OS === '1' || exists(LIVE_UPDATE)
@@ -254,15 +263,34 @@ export function startOsDevice(_core: CoreApi, deps: OsDeviceDeps = {}): ServiceR
const parsed: unknown = JSON.parse(out)
if (!record(parsed)) throw new Error('live_update.py status did not answer an object')
return updateOf(parsed)
- }).catch((error: unknown) => ({ available: false, error: short(error) })),
+ }).catch((error: unknown): Payload => ({ available: false, error: short(error) })),
currentSsid(),
// The open Wi-Fi it hosts while locked or not set up: where a phone finds it after a restart.
exec(PYTHON, [SETUP, 'name'], { timeout: NMCLI_TIMEOUT_MS }).then((out) => out.trim() || null).catch(() => null),
read(CMDLINE).catch(() => ''),
])
+ if (checking) update.checking = true
+ else if (checkError !== null) update.checkError = checkError
// Encrypted, it waits for its password at every start (installer.py: rd.luks.name=).
// features: what this daemon does for a phone beyond the update and the Wi-Fi.
- return { harnessOs: true, update, wifi: { ssid }, hotspot, encrypted: /(^|\s)rd\.luks\.name=/.test(cmdline), features: ['sign_out', 'password', 'ssh', 'github'] }
+ return {
+ harnessOs: true, update, wifi: { ssid }, hotspot, encrypted: /(^|\s)rd\.luks\.name=/.test(cmdline),
+ features: ['sign_out', 'password', 'ssh', 'github', 'check_update'],
+ }
+ }
+
+ /** Check for updates now, as the timer does every 15 minutes, the OS release too: answered at once,
+ * `update.checking` in the status until it is done, for the phone to follow. One at a time. */
+ const checkUpdate = (): Payload => {
+ checkError = null
+ checking ??= exec(PYTHON, ['-c', CHECK_NOW], { timeout: CHECK_TIMEOUT_MS })
+ .then(() => { console.log('[os-device] checked for updates') })
+ .catch((error: unknown) => {
+ console.error('[os-device] check for updates failed:', short(error))
+ checkError = 'Could not check for updates. Check its internet, then try again.'
+ })
+ .finally(() => { checking = null })
+ return { started: true }
}
const update = (payload: Payload): Payload => {
@@ -420,6 +448,7 @@ export function startOsDevice(_core: CoreApi, deps: OsDeviceDeps = {}): ServiceR
if (!harnessOs()) return { error: 'UNSUPPORTED_OS' }
try {
if (action === 'update') return update(payload)
+ if (action === 'check_update') return checkUpdate()
if (action === 'wifi_scan') return await scan()
if (action === 'wifi_join') return join(payload)
if (action === 'sign_out') return signOut()