Files
plugins/packages/nanobot/runtime.mjs
T
netube e689973203 agent: add nanobot with model, Library MCP and workspace skills (#908)
Adds the nanobot agent adapter (packages/nanobot): magpie's model, Library MCP servers and workspace skills written into ~/.nanobot/config.json through a journaled, owner-tracked bridge, and restored on disconnect.

fc46523d resolves symlinked parent directories (macOS /var, dotfile-managed ~/.nanobot or ~/.config) while still refusing a symlinked final file, and keeps the preset on host exit; only a disconnect restores.

Run at fc46523d: `bun test` in packages/nanobot, 60 pass / 0 fail on mote (macOS, default TMPDIR under the /var link, which failed 18 of 22 at c6f8f639) and on the magpie-desktop WSL distro (Ubuntu 24.04). Not tried: a real nanobot binary driven through magpie.
2026-11-10 03:26:10 +08:00

130 lines
5.5 KiB
JavaScript

import fs from "node:fs"
import path from "node:path"
import { createHash, randomUUID } from "node:crypto"
import { pathsFor, reconcile, inspect } from "./bridge.mjs"
function configFile(file) {
try {
const stat = fs.lstatSync(file)
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 1024 * 1024) throw new Error("invalid-adapter-settings")
const value = JSON.parse(fs.readFileSync(file, "utf8"))
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("invalid-adapter-settings")
return value
} catch (error) {
if (error.code === "ENOENT") return {}
throw new Error("invalid-adapter-settings")
}
}
export function resolvePaths(options = {}) {
const defaults = pathsFor()
const settings = { ...configFile(path.join(defaults.directory, "adapter.json")), ...options }
if (settings.configPath !== undefined && (typeof settings.configPath !== "string" || !path.isAbsolute(settings.configPath))) throw new Error("configPath-must-be-absolute")
return pathsFor({ configPath: settings.configPath })
}
function signature(p) {
const hash = createHash("sha256")
for (const file of [p.control, p.mcp]) {
try {
const stat = fs.lstatSync(file)
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 1024 * 1024) throw new Error("invalid-control-file")
hash.update(fs.readFileSync(file))
} catch (error) {
if (error.code !== "ENOENT") throw error
hash.update("<missing>")
}
hash.update("\0")
}
return hash.digest("hex")
}
function claimRuntime(p) {
const file = path.join(p.directory, "runtime.lock")
fs.mkdirSync(p.directory, { recursive: true, mode: 0o700 })
const owner = { pid: process.pid, nonce: randomUUID() }
const readOwner = () => {
const stat = fs.lstatSync(file)
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 1024) throw new Error("invalid-runtime-lock")
const value = JSON.parse(fs.readFileSync(file, "utf8"))
if (!Number.isSafeInteger(value.pid) || value.pid <= 0 || typeof value.nonce !== "string") throw new Error("invalid-runtime-lock")
return value
}
try { fs.writeFileSync(file, JSON.stringify(owner), { mode: 0o600, flag: "wx" }) }
catch (error) {
if (error.code !== "EEXIST") throw error
const previous = readOwner()
try { process.kill(previous.pid, 0); return null }
catch (probe) { if (probe.code !== "ESRCH") return null }
// Serialize stale reclamation separately. Re-reading a nonce alone is not
// a compare-and-delete: another reclaimer could create a live lock between
// the read and unlink. A leftover guard fails closed for manual inspection.
const guard = file + ".reclaim"
try { fs.writeFileSync(guard, owner.nonce, { mode: 0o600, flag: "wx" }) }
catch (guardError) { if (guardError.code === "EEXIST") return null; throw guardError }
try {
let current
try { current = readOwner() } catch (probe) { if (probe.code !== "ENOENT") throw probe }
if (current) {
try { process.kill(current.pid, 0); return null }
catch (probe) { if (probe.code !== "ESRCH") return null }
fs.unlinkSync(file)
}
try { fs.writeFileSync(file, JSON.stringify(owner), { mode: 0o600, flag: "wx" }) }
catch (retry) { if (retry.code === "EEXIST") return null; throw retry }
} finally {
if (fs.readFileSync(guard, "utf8") === owner.nonce) fs.unlinkSync(guard)
}
}
const release = () => {
try { if (readOwner().nonce === owner.nonce) fs.unlinkSync(file) }
catch (error) { if (error.code !== "ENOENT") throw error }
}
release.owns = () => { try { return readOwner().nonce === owner.nonce } catch { return false } }
return release
}
// No backend request or nanobot process starts here. Each input must remain
// stable for one complete polling period before a target write is attempted.
export function startRuntime(p, { interval = 750, onStatus = () => {}, schedule = setInterval, cancel = clearInterval } = {}) {
if (!Number.isSafeInteger(interval) || interval < 100 || interval > 60000) throw new Error("invalid-poll-interval")
let previous = null, lastStatus = "", stopped = false, release = null
const report = (status) => {
const value = JSON.stringify(status)
if (value !== lastStatus) { lastStatus = value; onStatus(status) }
return status
}
const tick = () => {
if (stopped) return
try {
if (release && !release.owns()) release = null
if (!release) {
release = claimRuntime(p)
if (!release) return report({ state: "standby", recoveryPending: false, message: "Another Magpie host owns this adapter." })
}
const current = signature(p)
if (current !== previous) { previous = current; return }
return report(reconcile(p))
} catch {
return report({ state: "conflict", recoveryPending: true, message: "Adapter paused; inspect the control files and recovery status. Configuration contents are omitted." })
}
}
const timer = schedule(tick, interval)
timer.unref?.()
return {
tick,
status: () => inspect(p),
stop({ restore = false } = {}) {
if (stopped) return
stopped = true
cancel(timer)
// Flush the current intent synchronously before releasing ownership.
// Host exit alone is not a disconnect, but a control-file disconnect
// may still be waiting for the next polling tick. A standby never writes.
if (!release) return
try { return release.owns() ? report(reconcile(p, { restore })) : undefined }
finally { release(); release = null }
},
}
}