Files
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

94 lines
3.9 KiB
JavaScript

import { test, expect } from 'bun:test'
import { readFileSync } from 'node:fs'
const scope = {}
new Function('globalThis', readFileSync(new URL('./page/scene.js', import.meta.url), 'utf8'))(scope)
const { Scene, DESKS, SOFA, SOFA_FACE } = scope.AgentOfficeScene
const V = { n: [0, -1], s: [0, 1], e: [1, 0], w: [-1, 0] }
const dot = (a, b) => a[0] * b[0] + a[1] * b[1]
// A seat's facing must point at what its owner is meant to look at.
test('every desk seat faces its own desk, and island pairs face each other', () => {
for (const d of DESKS) {
const v = V[d.face]
const mid = [d.x + d.w / 2, d.y + d.d / 2]
const toDesk = [mid[0] - d.seat[0], mid[1] - d.seat[1]]
expect(dot(v, toDesk)).toBeGreaterThan(0.2)
}
const north = DESKS[4], south = DESKS[6]
expect(dot(V[north.face], V[south.face])).toBe(-1)
})
test('a sofa sitter faces away from the backrest', () => {
expect(SOFA_FACE.length).toBe(SOFA.length)
for (let i = 0; i < SOFA.length; i++) {
const [x, y] = SOFA[i]
const v = V[SOFA_FACE[i]]
// left sofa: backrest at x .35-.75; front sofa: backrest at y 11.35-11.7
const back = i < 5 ? [0.55, y] : [x, 11.5]
expect(dot(v, [x - back[0], y - back[1]])).toBeGreaterThan(0.2)
}
})
// The monitor's lit side and the chair's back are drawn from the facing.
const fakeCtx = () => new Proxy({}, {
get: (o, k) => (k in o ? o[k] : k === 'createLinearGradient' ? () => ({ addColorStop() {} })
: k === 'measureText' ? () => ({ width: 0 }) : () => {}),
set: (o, k, v) => { o[k] = v; return true },
})
function spy(dim) {
const s = Object.create(Scene.prototype)
Object.assign(s, { dim, atSeat: {}, workers: new Map(), t: 0, s: 40, ox: 0, oy: 0, g: fakeCtx() })
for (const name of ['box', 'poly', 'onFront', 'onBack', 'onLeft', 'onRight']) s[name] = () => {}
s.depth = (x, y) => x + y
s.P = (x, y, z = 0) => [x - y, x + y - z]
return s
}
test('monitor screen faces the sitter and the chair back stands behind', () => {
for (const d of DESKS) {
const s = spy('3d')
const faces = []
s.onFront = () => faces.push('front')
s.onBack = () => faces.push('back')
const boxes = []
s.box = (...a) => boxes.push(a)
// the chair is a drawable of its own: run those too
const chairKey = s.depth(d.seat[0], d.seat[1])
const rest = s.props().filter((p) => p.k >= chairKey - 0.45 && p.k <= chairKey + 0.5)
const props = s.props()
const key = s.depth(d.x + d.w / 2, d.y + d.d)
const deskProp = props.find((p) => Math.abs(p.k - key) < 1e-9)
expect(deskProp).toBeDefined()
deskProp.fn()
const screenSide = faces[0]
expect(screenSide).toBe(d.face === 's' ? 'back' : 'front')
// chair back: a 0.1-deep slab behind the sitter, on the opposite side
const cy = d.seat[1]
for (const p of rest) p.fn()
const chairBack = boxes.find(([x, y, z, w, h, hh]) => Math.abs(w - 0.56) < 1e-6 && Math.abs(h - 0.1) < 1e-6 && hh > 0.4)
expect(chairBack).toBeDefined()
const backY = chairBack[1]
if (d.face === 'n') expect(backY).toBeGreaterThan(cy)
else expect(backY).toBeLessThan(cy)
}
})
test('a sitter facing away shows no face; facing east shows the profile', () => {
const draw = (facing) => {
const s = spy('3d')
const calls = []
for (const n of ['onFront', 'onBack', 'onLeft', 'onRight']) s[n] = (...a) => calls.push([n, a[a.length - 1]])
const w = { key: 'k', x: 4, y: 6, kind: 'sofa', slot: 0, walking: false, facing, phase: 0, skin: '#fff', hair: '#000', pants: '#000', color: '#f00',
a: { status: 'done', seed: 3, brand: { color: '#f00' } } }
s.worker(w)
return calls
}
// dark eye pixels only get drawn toward the camera (+y) or east (+x)
const eyesOn = (calls) => calls.filter(([, c]) => c === '#1f2937')
expect(eyesOn(draw('s')).length).toBe(2)
const east = eyesOn(draw('e'))
expect(east.length).toBe(2)
expect(east.every(([n]) => n === 'onRight')).toBe(true)
expect(eyesOn(draw('n')).length).toBe(0)
expect(eyesOn(draw('w')).length).toBe(0)
})