0.2.3's host acted on any jumpRequest no older than 15 s, a future one
too, and kept its time in $XDG_CACHE_HOME/agent-office/last-jump.json;
every later click had at <= that time and was dropped until the clock
got there. The decision is now judgeJump(at, seen, now) in lib/jump.mjs:
- A request fires only from 15 s old to 5 s ahead, once.
- One stamped further ahead is set aside and recorded, so it never
fires when the clock reaches it.
- A seen time ahead of now (0.2.3's file for such a request, or a clock
that stepped back) blocks only that same request (at === seen); any
other fresh click fires and rewrites the file. With seen in the past,
an older or equal request is still never acted on, so nothing is
replayed across restarts.
- The target is still worked out by the host from the session id; a
page-stamped time can only make a request be skipped, not fire it
later or elsewhere. Non-finite times are ignored and never written.
Existing bad last-jump.json from 0.2.3: read as before, no migration
needed; it no longer blocks, and the next click replaces it.
Run at this tree (base b797476), HOME=$(mktemp -d):
- mote (macOS): packages/agent-office bun test 180 pass, 0 fail;
bun scripts/stamp-page.mjs --check ok; bun scripts/check.mjs ok.
- wsl-run (Ubuntu 24.04): agent-office bun test 180 pass, 0 fail.
- win-run (Windows 11): agent-office bun test 180 pass, 0 fail.
The two host tests are macOS-only (fake herdr, like the 0.2.3 restart
test); judgeJump's table test runs on all three.
- New tests against 0.2.3's office.hooks.mjs (mote): both host tests
fail; a bad last-jump.json blocked the fresh jump, and a request an
hour ahead fired at once.
- Broken by hand (mote): dropping the 5 s ahead bound (2 fail), letting
a seen time ahead of now order requests again (3 fail), not recording
a set-aside future request (2 fail).
Not tried: a real magpie with a real herdr or terminal, or a real clock
step; whole-repo bun test (only agent-office changed).
276 lines
11 KiB
JavaScript
276 lines
11 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"
|
|
import { createJumper, judgeJump } from "./lib/jump.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 jumper = null
|
|
let timer = null
|
|
let ticking = null
|
|
let last = ""
|
|
let lastJumpAt = 0
|
|
let lastJumpPromise = null
|
|
// 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,
|
|
// the workbench's extra facts: a child session, its recent words, and
|
|
// what this session used today. Words are dropped with text off.
|
|
subagent: s.subagent === true, parent: s.parent || "", profile: s.profile || "",
|
|
messages: text && Array.isArray(s.messages) ? s.messages.slice(-8) : [],
|
|
tokensToday: Number(s.tokensToday) || 0, turnsToday: Number(s.turnsToday) || 0,
|
|
context: Number(s.context) || 0, contextWindow: Number(s.contextWindow) || 0,
|
|
target: s.target || null,
|
|
}
|
|
}
|
|
|
|
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))
|
|
}
|
|
}
|
|
if (jumper) jumper.decorate(sessions, now, options.preferredJumpHost || "herdr-app")
|
|
// 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
|
|
checkJumpOption()
|
|
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 (!jumper && (opts.jumper || !process.env.MAGPIE_AGENT_OFFICE_PAGE)) {
|
|
jumper = createJumper({ onUpdate: soon, ...(opts.jumper || {}) })
|
|
void jumper.refreshState(Date.now(), options.preferredJumpHost || "herdr-app")
|
|
}
|
|
if (opts.jumpRequest) checkJumpOption(opts.jumpRequest)
|
|
if (!ticking) {
|
|
write()
|
|
ticking = setInterval(write, EVERY)
|
|
ticking.unref?.()
|
|
}
|
|
}
|
|
|
|
export function jump(req) {
|
|
return jumper ? jumper.jump(req) : Promise.resolve({ ok: false, steps: [] })
|
|
}
|
|
|
|
export function waitForJump() {
|
|
return lastJumpPromise || Promise.resolve(null)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
function jumpCacheFile() {
|
|
const cache = process.env.XDG_CACHE_HOME || join(process.env.HOME || "", ".cache")
|
|
return join(cache, "agent-office", "last-jump.json")
|
|
}
|
|
|
|
function readLastJumpAt() {
|
|
try {
|
|
const j = JSON.parse(readFileSync(jumpCacheFile(), "utf8"))
|
|
const at = Number(j?.at)
|
|
return Number.isFinite(at) && at > 0 ? at : 0
|
|
} catch { return 0 }
|
|
}
|
|
|
|
function writeLastJumpAt(at) {
|
|
try {
|
|
const file = jumpCacheFile()
|
|
mkdirSync(dirname(file), { recursive: true })
|
|
writeFileSync(file, JSON.stringify({ at }) + "\n", { mode: 0o600 })
|
|
} catch {}
|
|
}
|
|
|
|
function checkJumpOption(directReq) {
|
|
if (!jumper) return
|
|
const standalone = !!process.env.MAGPIE_AGENT_OFFICE_PAGE
|
|
// Standalone CLI and tests set MAGPIE_AGENT_OFFICE_PAGE and pass jumpRequest
|
|
// explicitly to start(); they must never read or act on magpie's plugins.json.
|
|
if (standalone && !directReq) return
|
|
const fresh = standalone ? null : readOptions()
|
|
if (fresh && typeof fresh.preferredJumpHost === "string") options.preferredJumpHost = fresh.preferredJumpHost
|
|
const req = directReq || fresh?.jumpRequest
|
|
if (!req || typeof req !== "object") return
|
|
const at = Number(req.at || 0)
|
|
// magpie restarts the plugin host when options.set writes jumpRequest to
|
|
// plugins.json. Keep the last handled timestamp in our own cache file (never
|
|
// touching plugins.json) so the freshly started host acts on a ≤15s request
|
|
// once and never replays it on later restarts. judgeJump keeps a time ahead
|
|
// of the clock from blocking later requests (#583).
|
|
const { fire, record } = judgeJump(at, lastJumpAt, Date.now())
|
|
if (record) {
|
|
lastJumpAt = at
|
|
if (!standalone) writeLastJumpAt(at)
|
|
}
|
|
if (!fire) return
|
|
lastJumpPromise = jumper.jump({
|
|
key: String(req.key || ""),
|
|
session: String(req.session || ""),
|
|
preferredHostId: String(req.preferredHostId || options.preferredJumpHost || ""),
|
|
})
|
|
}
|
|
|
|
// 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) {
|
|
lastJumpAt = readLastJumpAt()
|
|
start()
|
|
}
|