Files
goodbuddy/src/main/remote-agent/remote-environment-operation-store.ts
T
mesalogo 85e14ca626
GoodBuddy Agents / Validate Agent source (push) Canceled after 0s
GoodBuddy Agents / Build Linux arm64 Agent (push) Canceled after 0s
GoodBuddy Agents / Build Linux x64 Agent (push) Canceled after 0s
GoodBuddy Agent Release / Authorize Agent release (push) Canceled after 0s
Cross-platform packages / Validate source (push) Canceled after 0s
GoodBuddy Agent Release / Build Linux arm64 compound package (push) Canceled after 0s
GoodBuddy Agent Release / Build Linux x64 compound package (push) Canceled after 0s
GoodBuddy Agent Release / Sign cumulative Agent catalog (push) Canceled after 0s
GoodBuddy Agent Release / Publish Agent to GitHub and OSS (push) Canceled after 0s
Cross-platform packages / Authorize desktop packaging (push) Canceled after 0s
Cross-platform packages / linux arm64 (push) Canceled after 0s
Cross-platform packages / macos arm64 (push) Canceled after 0s
Cross-platform packages / windows arm64 (push) Canceled after 0s
Cross-platform packages / linux x64 (push) Canceled after 0s
Cross-platform packages / macos x64 (push) Canceled after 0s
Cross-platform packages / windows x64 (push) Canceled after 0s
Cross-platform packages / Publish GitHub and OSS release (push) Canceled after 0s
fix: make Host updates simple and fast
Host repair could fail while adopting the published OpenCode Runtime, then
repeat package and payload verification and carry durable metadata snapshots
for recovery. Host preparation now verifies each acquired compound archive
once, adopts signed Agent and Runtime metadata without rereading payload trees,
and retries failed work by cleaning staging and preparing fresh.

The desktop and Agent versions are advanced to 0.11.9 with the approved
bilingual release notes. Remote Projects remain a disabled-by-default technical
preview, Agent packages remain separate Linux x64/arm64 releases, and this
release has no LoongArch preview.

Release note: 修复 Host 更新采用失败,移除重复远端包体校验,并将中断恢复简化为清理暂存后重新准备。
2026-08-29 22:31:39 +08:00

124 lines
3.1 KiB
TypeScript

import { createHash } from 'node:crypto'
import { readFile, rm } from 'node:fs/promises'
import { join, resolve } from 'node:path'
import {
isMissingFileError,
writeJsonFileAtomically
} from '../settings-file-utils'
const MAXIMUM_RECORD_BYTES = 4 * 1024
const OPERATION_ID_PATTERN =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u
const SHA256_PATTERN = /^[a-f0-9]{64}$/u
export type PendingRemoteEnvironmentOperation = {
version: 1
hostId: string
targetIdentity: string
operationId: string
}
export class RemoteEnvironmentOperationStore {
readonly #directory: string
constructor(userDataPath: string) {
this.#directory = resolve(
userDataPath,
'remote-components',
'pending-environment-operations'
)
}
async load(
hostId: string
): Promise<PendingRemoteEnvironmentOperation | undefined> {
const path = this.#path(hostId)
let bytes: Buffer
try {
bytes = await readFile(path)
} catch (error) {
if (isMissingFileError(error)) {
return undefined
}
throw error
}
if (bytes.byteLength > MAXIMUM_RECORD_BYTES) {
throw new Error('远程环境恢复记录超过大小限制')
}
let value: unknown
try {
value = JSON.parse(
new TextDecoder('utf-8', { fatal: true }).decode(bytes)
)
} catch (error) {
throw new Error('远程环境恢复记录无效', {
cause: error
})
}
return parsePendingOperation(value, hostId)
}
save(operation: PendingRemoteEnvironmentOperation): Promise<void> {
return writeJsonFileAtomically(
this.#path(operation.hostId),
parsePendingOperation(operation, operation.hostId)
)
}
remove(hostId: string): Promise<void> {
return rm(this.#path(hostId), { force: true })
}
#path(hostId: string): string {
if (
!hostId ||
hostId.length > 512 ||
/[\p{Cc}\p{Cs}\u2028\u2029\ufffd]/u.test(hostId)
) {
throw new Error('远程环境恢复 Host ID 无效')
}
const name = createHash('sha256')
.update(hostId)
.digest('hex')
return join(this.#directory, `${name}.json`)
}
}
function parsePendingOperation(
value: unknown,
expectedHostId: string
): PendingRemoteEnvironmentOperation {
if (typeof value !== 'object' || value === null) {
throw new Error('远程环境恢复记录无效')
}
const record = value as Record<string, unknown>
const expectedKeys = [
'hostId',
'operationId',
'targetIdentity',
'version'
]
if (
Object.keys(value).sort().join('\0') !==
expectedKeys.sort().join('\0')
) {
throw new Error('远程环境恢复记录无效')
}
if (
record.version !== 1 ||
record.hostId !== expectedHostId ||
typeof record.targetIdentity !== 'string' ||
!SHA256_PATTERN.test(record.targetIdentity) ||
typeof record.operationId !== 'string' ||
!OPERATION_ID_PATTERN.test(record.operationId)
) {
throw new Error('远程环境恢复记录无效')
}
return {
version: 1,
hostId: expectedHostId,
targetIdentity: record.targetIdentity,
operationId: record.operationId
}
}