Files
plugins/packages/agent-office/office.hooks.mjs
T
QingJ 1e04c1d324 agent-office 0.1.0: live agent activity office page (#478)
A new community page plugin by QingJ01: Agent Office, a page in magpie's window that shows each local agent session as a worker in an office (3D/2D) or a list, with status, current step and a resume command.

- Its host part (office.hooks.mjs, an async gateway hook) reads the session files of Claude Code, Codex, dsh, Gemini CLI, Qwen Code, Pi, omp, Qoder, WorkBuddy and CodeBuddy every 1.5 s and writes page/live.js beside the page. It opens no socket and sends nothing off the machine. The page makes no magpie.read calls and loads only its own files.
- Text switch: with text off, the host writes no prompt, reply, error or tool argument (the title falls back to the folder, the step to the tool name), and the page redacts whatever live.js holds.
- Options are read at module load from plugins.json in the host's cwd (magpie's config folder), with the XDG path as a fallback. npm specs are matched with @tag/@version stripped, a path spec by realpath against the package's own folder. It fails closed: text stays off until an install's options or a request's ctx.options turn it on.
- The disclosure (package description, registry summary, root README) says which session files it reads. magpie's install sheet still says "reads nothing" for such a page (yetone/magpie#4187).

Run at the reviewed head e76d1a3, on mote:
- bun test in packages/agent-office: 44 pass, 0 fail.
- The whole repo at origin/main 3462544 merged with e76d1a3: bun test 3275 pass, 8 skip, 0 fail (HOME sandboxed); bun scripts/check.mjs exit 0.
- The new privacy tests run against 9230da7's hooks: 5 of 11 red (@latest, @0.1.0, cwd-only config, no config, unmatched spec).
- e76d1a3's case (cwd plugins.json without this entry, XDG plugins.json with text:true), by hand: text stays off. With d4acdae's hooks: text on and the prompt leaks. No test covers this case. The privacy tests stay green with it reverted.
- Real magpie: main 0745db932, built for macOS with -tags nogui, run as a portable magpie (.portable marker) in a sandbox HOME. The plugin was added from a folder named zz-src, so no name guess can match. A decoy ~/.config/magpie/plugins.json said text:true. The sandbox HOME had a seeded Claude Code session with a secret prompt, reply and Bash argument. magpie web was driven in Playwright Chromium and WebKit (WebKit in zh). With text on, the prompt, reply and command showed. With text off, the page hid them within 0.3 s. live.js went text:false after the host restarted, and over 30 s of sampling no secret was on the page or in live.js. After a reload, the switch was off and nothing leaked. The frame requested only its own files under /_magpie/plugin/<token>/, with no request leaving 127.0.0.1:3592. The plugin host had no sockets (lsof).

Not changed: the XDG fallback can read another magpie's plugins.json, but only when the host's cwd has none. magpie starts the host in its own config folder, which always has one, so a real host never reaches it.
2026-10-29 19:45:44 +08:00

197 lines
7.6 KiB
JavaScript

// The host side of Agent Office. magpie loads this module in its plugin host
// (a Bun process that lives while magpie runs), so it does two things there:
//
// 1. Every second and a half it reads the session files the agents write
// themselves — Claude Code's, Codex's, DeepSeek Harness's, Gemini CLI's,
// Qwen Code's, Pi's, omp's, Qoder's, WorkBuddy's, CodeBuddy's — and so
// knows what each one is really doing, whatever model it talks to:
// thinking, running a tool, waiting for an approval or an answer, done,
// stopped, failed (lib/scan.mjs, lib/readers.mjs).
// 2. As an asynchronous gateway hook it also sees the requests that pass
// magpie, so an agent whose files it can't read (Cursor, OpenCode,
// Copilot, Kimi, …) still shows while it works through magpie
// (lib/track.mjs). It returns no body: no request changes.
//
// It writes what it knows to page/live.js, beside the page, which the page
// loads again every second (the page has no network; its own files it may
// load). Only what changed is written. Nothing leaves this computer.
import { mkdirSync, readFileSync, realpathSync, renameSync, writeFileSync } from "node:fs"
import { dirname, join, resolve } from "node:path"
import { fileURLToPath } from "node:url"
import { createTracker } from "./lib/track.mjs"
import { createScanner } from "./lib/scan.mjs"
const here = dirname(fileURLToPath(import.meta.url))
// the page's folder, where live.js goes; a test points this elsewhere so it
// never touches a real install's
const PAGE = process.env.MAGPIE_AGENT_OFFICE_PAGE || join(here, "page")
const LIVE = join(PAGE, "live.js")
const PREFIX = "globalThis.AgentOfficeLive = "
const EVERY = 1500
// the package's own name, so plugins.json's spec is found however it was
// written: @magpie-community/page-agent-office, QingJ01/magpie-agent-office,
// a folder, or the short name alone
let packageName = ""
let packageOptions = {}
try {
const own = JSON.parse(readFileSync(join(here, "package.json"), "utf8"))
packageName = own.name || ""
packageOptions = own.magpie?.options || {}
} catch {}
const ownDir = (() => { try { return realpathSync(here) } catch { return here } })()
let tracker = null
let scanner = null
let timer = null
let ticking = null
let last = ""
// fail closed: until an install's options are read, or a request carries
// them, no prompt, reply or command is written
let options = { text: false }
function track(opts) {
const o = { text: opts?.text !== false, remote: opts?.remote === true }
if (tracker && tracker.options.text === o.text && tracker.options.remote === o.remote) return tracker
const was = tracker
tracker = createTracker(o)
if (was) for (const [k, s] of was.sessions) tracker.sessions.set(k, s)
return tracker
}
// a session as the page wants it, from the agent's own files
function fromFiles(s, text) {
return {
key: s.key, source: "files", agent: s.agent, session: s.id, cwd: s.cwd, project: s.project,
// with text off the title falls back to the folder and the step to the
// tool's name: a prompt and a command line can both carry secrets
title: text ? (s.title || s.project || "") : (s.project || ""),
query: text ? s.query : "", model: s.model,
started: s.started, updated: Math.max(s.updated || 0, s.touched || 0), touched: s.touched,
phase: s.phase || "done", step: text ? s.step : (s.stepName || ""), reply: text ? s.reply : "", error: text ? s.error : "",
pendingSince: s.pending?.[0]?.at || 0, turns: s.turns,
}
}
export function snapshot(now = Date.now()) {
const text = options.text !== false
const sessions = []
const seen = new Set()
const fileAgents = new Set()
if (scanner) {
for (const s of scanner.scan(now)) {
fileAgents.add(s.agent)
if (seen.has(s.key)) continue
seen.add(s.key)
sessions.push(fromFiles(s, text))
}
}
// what only the gateway saw: an agent whose files aren't read here
if (tracker) {
for (const g of tracker.snapshot(now).sessions) {
if (seen.has(g.key) || fileAgents.has(g.agent)) continue
seen.add(g.key)
sessions.push({ ...g, source: "gateway" })
}
}
return { v: 2, written: now, text, sessions }
}
export function write(now = Date.now()) {
timer = null
let body
try { body = JSON.stringify(snapshot(now)) } catch { return }
// only a change is written; the time alone is no change
const same = body.replace(/"written":\d+,/, "")
if (same === last) return
last = same
try {
mkdirSync(PAGE, { recursive: true })
const tmp = LIVE + ".tmp"
writeFileSync(tmp, PREFIX + body + ";\n")
renameSync(tmp, LIVE)
} catch {}
}
function soon() {
if (timer) return
timer = setTimeout(write, 200)
timer.unref?.()
}
// the files are read from the moment the host loads this module
export function start(opts = {}) {
options = { ...options, ...opts }
if (!scanner) scanner = createScanner({ days: 2, max: 40, ...(opts.scanner || {}) })
if (!ticking) {
write()
ticking = setInterval(write, EVERY)
ticking.unref?.()
}
}
export function stop() {
if (ticking) clearInterval(ticking)
ticking = null
}
export async function onRequest(body, ctx) {
// keep the loaded options when a request carries none: text:false must
// not turn back into text:true because a field was missing
options = { ...options, ...(ctx.options || {}) }
start()
const key = track(options).request(body, ctx, Date.now())
ctx.state.office = key
if (key) soon()
ctx.report?.("skipped")
return undefined
}
export async function onResponse(reply, ctx) {
if (!ctx.state?.office) return
if (track(options).response(reply, ctx, Date.now(), ctx.success === true)) soon()
}
// a spec as magpie writes it, or a package name, or a path to this folder
function matchSpec(spec) {
const raw = String(spec || "").trim()
if (!raw) return false
const pathLike = raw.startsWith("/") || raw.startsWith(".") || raw.startsWith("~") || raw.startsWith("file:")
if (pathLike) {
let p = raw.replace(/^file:\/\//, "").replace(/^file:/, "")
if (p === "~" || p.startsWith("~/")) p = join(process.env.HOME || "", p === "~" ? "" : p.slice(2))
try { return realpathSync(resolve(p)) === ownDir } catch { return false }
}
// an npm spec keeps a tag or version after its last @; a scheme's @ is
// left alone
let clean = raw.split("#")[0]
if (!clean.includes("://")) {
const at = clean.lastIndexOf("@")
if (at > 0) clean = clean.slice(0, at)
}
const names = new Set([packageName, packageName.replace(/^@[^/]+\//, ""), "page-agent-office", "magpie-agent-office", "agent-office"].filter(Boolean))
if (names.has(clean)) return true
return names.has(clean.split(/[/:]/).filter(Boolean).pop() || "")
}
function readOptions() {
const cfg = process.env.XDG_CONFIG_HOME || join(process.env.HOME || "", ".config")
// magpie starts the host in its config folder, which covers portable and
// overridden installs; the XDG path stays as the fallback
for (const file of [join(process.cwd(), "plugins.json"), join(cfg, "magpie", "plugins.json")]) {
let list
try { list = JSON.parse(readFileSync(file, "utf8")) } catch { continue }
const me = (list.plugins || []).find((p) => matchSpec(p?.spec))
// the config this host runs with decides; a file without our entry is
// not a reason to consult another magpie's
return me ? { ...packageOptions, ...(me.options || {}) } : null
}
return null
}
// read what the plugin's options say about text, then start: a host that
// loads this module starts watching at once, before any request
const loaded = readOptions()
if (loaded) options = { text: false, ...loaded }
if (!process.env.MAGPIE_AGENT_OFFICE_PAGE) start()