Files
goodbuddy/src/agent-daemon/linux-process-identity.ts
T
mesalogo 9c6d1611e2 feat: support macOS arm64 remote Agent hosts
Extend GoodBuddy Agent packaging, installation, SSH attachment, Runtime lifecycle, and Desktop package management to macOS arm64. Linux x64, Linux arm64, and Darwin arm64 share the existing signed catalog. Preserve Ask read-only behavior and Execute account permissions, and stop detached tool descendants during Mac cancellation.

Upgrade Desktop before publishing the first mixed-platform catalog because older Linux-only readers reject Darwin entries. Intel Macs are not supported; no launchd service or administrator access is required. This commit does not publish a release.

Validation: 3520 tests passed, 57 skipped; typecheck, lint, and development build passed. Real Mac installation, Ask/Execute, cancellation and reconnect passed; final Linux installation, Ask and reconnect passed. Nine real model requests were recorded.

Release note: 支持将 Apple Silicon Mac 用作远程项目 Host,可安装 macOS arm64 Agent 并使用 Ask、Execute 和断线重连;与 Linux 共用一个包目录,发布混合目录前需升级 Desktop。
2026-09-06 16:14:02 +08:00

178 lines
4.9 KiB
TypeScript

import {
lstatSync,
readFileSync,
readlinkSync,
realpathSync
} from 'node:fs'
import { isAbsolute, join, resolve } from 'node:path'
import { z } from 'zod'
import { assertAbsoluteManagedPath } from './managed-paths'
import { inspectDarwinProcess } from './darwin-process-identity'
const linuxProcessIdentitySchema = z
.object({
pid: z.number().int().positive().safe(),
starttime: z.string().regex(/^(?:0|[1-9][0-9]*)$/u),
executablePath: z.string().min(1)
})
.strict()
export type LinuxProcessIdentity = z.infer<
typeof linuxProcessIdentitySchema
>
export type LinuxProcessInspectorOptions = {
procRoot?: string
kill?: (pid: number, signal: NodeJS.Signals | 0) => void
fileSystem?: LinuxProcFileSystem
}
export interface LinuxProcFileSystem {
readText(path: string): string
readLink(path: string): string
realpath(path: string): string
}
export class LinuxProcessInspector {
readonly #procRoot: string
readonly #kill: (pid: number, signal: NodeJS.Signals | 0) => void
readonly #fileSystem: LinuxProcFileSystem
constructor(options: LinuxProcessInspectorOptions = {}) {
this.#procRoot = resolve(options.procRoot ?? '/proc')
this.#kill = options.kill ?? process.kill
this.#fileSystem = options.fileSystem ?? nodeProcFileSystem
}
inspect(pidInput: number): LinuxProcessIdentity | undefined {
const pid = positivePid(pidInput)
if (process.platform === 'darwin') {
const identity = inspectDarwinProcess(pid)
return identity === undefined ? undefined : {
pid: identity.pid,
starttime: identity.starttime,
executablePath: identity.executablePath
}
}
try {
const starttime = parseStarttime(
this.#fileSystem.readText(
join(this.#procRoot, String(pid), 'stat')
)
)
const executableLink = this.#fileSystem.readLink(
join(this.#procRoot, String(pid), 'exe')
)
if (
executableLink.includes('\0') ||
!isAbsolute(executableLink) ||
resolve(executableLink) !== executableLink
) {
throw new Error('Linux process executable link is malformed')
}
return linuxProcessIdentitySchema.parse({
pid,
starttime,
executablePath: assertAbsoluteManagedPath(
this.#fileSystem.realpath(executableLink)
)
})
} catch (error) {
if (isMissingProcessError(error)) {
return undefined
}
throw error
}
}
matches(expectedInput: LinuxProcessIdentity): boolean {
const expected = linuxProcessIdentitySchema.parse(expectedInput)
const actual = this.inspect(expected.pid)
return (
actual !== undefined &&
actual.starttime === expected.starttime &&
actual.executablePath === expected.executablePath
)
}
signal(
expectedInput: LinuxProcessIdentity,
signal: NodeJS.Signals
): boolean {
const expected = linuxProcessIdentitySchema.parse(expectedInput)
if (!this.matches(expected)) {
return false
}
try {
this.#kill(expected.pid, signal)
return true
} catch (error) {
if (isNodeError(error) && error.code === 'ESRCH') {
return false
}
throw error
}
}
}
export function currentLinuxProcessIdentity(
options: LinuxProcessInspectorOptions = {}
): LinuxProcessIdentity {
const identity = new LinuxProcessInspector(options).inspect(process.pid)
if (identity === undefined) {
throw new Error('Current Linux process identity is unavailable')
}
return identity
}
export function canonicalExecutablePath(path: string): string {
const normalized = assertAbsoluteManagedPath(resolve(path))
const stat = lstatSync(normalized)
if (stat.isSymbolicLink() || !stat.isFile()) {
throw new Error('Agent executable must be a regular file')
}
return assertAbsoluteManagedPath(realpathSync(normalized))
}
function parseStarttime(contents: string): string {
const close = contents.lastIndexOf(')')
if (close < 2 || contents[close + 1] !== ' ') {
throw new Error('Linux process stat is malformed')
}
const starttime = contents
.slice(close + 2)
.trim()
.split(/\s+/u)[19]
if (
starttime === undefined ||
!/^(?:0|[1-9][0-9]*)$/u.test(starttime)
) {
throw new Error('Linux process stat identity is malformed')
}
return starttime
}
const nodeProcFileSystem: LinuxProcFileSystem = {
readText: (path) => readFileSync(path, 'utf8'),
readLink: (path) => readlinkSync(path),
realpath: (path) => realpathSync(path)
}
function positivePid(value: number): number {
if (!Number.isSafeInteger(value) || value <= 0) {
throw new Error('Linux process ID is invalid')
}
return value
}
function isMissingProcessError(error: unknown): boolean {
return (
isNodeError(error) &&
(error.code === 'ENOENT' || error.code === 'ESRCH')
)
}
function isNodeError(error: unknown): error is NodeJS.ErrnoException {
return error instanceof Error && 'code' in error
}