From 5150142eb271a7588b03d0f5f5e6aa6d4c5f7fa6 Mon Sep 17 00:00:00 2001 From: Zhaoji Wang Date: Thu, 5 Nov 2026 09:42:49 +0800 Subject: [PATCH] agent-office: a Settings switch for agents from other computers and gateway keys (#741) agent-office: a Settings switch, "Include agents from other computers and gateway keys", sets options.remote, so the gateway hook keeps requests that are not local. It is off by default (package default remote:false, the page reads opts.remote === true, the hook opts?.remote === true). While it is off the hook counts the non-local requests it drops (capped at 9999) into the snapshot, and an empty office says they were ignored and where the switch is, in all nine languages. Adapted by frederick-wang from QingJ01/magpie-agent-office#5. Merged onto #740 with the page stamps taken again (a7a5bce6). Run at the reviewed head a7a5bce6, on mote: - HOME= bun test packages/agent-office: 183 pass, 0 fail, 34 more times (20 of them four at a time, load ~30). The first run of this head had 2 fails whose names were lost to tail; not seen again in the 34. - stamp-page.mjs --check: 0 (every ?v= checked against sha256); bun scripts/check.mjs agent-office: 0. - Broken by hand: dropping the dropped++ turns 2 tests red. Turning remote on by default in the hook's track() turned nothing red; a guard test follows in the version bump. - Playwright 1.62.0, Chromium and WebKit, light and dark, stub bridge, live.js with dropped:2 and no sessions: the empty state gives the hint; ticking the switch hides it, options.set receives remote:true after the 2.5s save, and a reload with those options shows the box ticked and no hint. Not tried: a real remote agent or gateway key through a real magpie. --- packages/agent-office/README.md | 2 +- packages/agent-office/agent-office.test.mjs | 27 +++++++++++++++++++++ packages/agent-office/features.test.mjs | 2 +- packages/agent-office/lib/track.mjs | 8 ++++-- packages/agent-office/office.hooks.mjs | 15 +++++++----- packages/agent-office/page/app.js | 8 ++++-- packages/agent-office/page/index.html | 5 ++-- packages/agent-office/page/model.js | 27 ++++++++++++++------- 8 files changed, 71 insertions(+), 23 deletions(-) diff --git a/packages/agent-office/README.md b/packages/agent-office/README.md index b730ccb..859f3ac 100644 --- a/packages/agent-office/README.md +++ b/packages/agent-office/README.md @@ -136,7 +136,7 @@ bun bin/agent-office.mjs --json # 一次性快照,适合让 Agent 在聊天 | `side` | 显示分组侧栏 | | `lang` | `auto` 或九种语言代码 | | `cleared` | { 会话 key: 时间 }:被「清除」的会话,文件再动就回来 | -| `remote` | 网关部分也记录其他电脑的请求 | +| `remote` | 网关部分也记录其他电脑和网关密钥的请求(也可在页面右上角 **设置** 里直接勾选) | | `approvalAfterSec` | 只有网关记录时,工具运行多久算等你批准(90) | ## 独立 CLI diff --git a/packages/agent-office/agent-office.test.mjs b/packages/agent-office/agent-office.test.mjs index f5375cb..4237f98 100644 --- a/packages/agent-office/agent-office.test.mjs +++ b/packages/agent-office/agent-office.test.mjs @@ -331,6 +331,33 @@ test("the gateway part still reads replies in each API", () => { expect(outcomeOf(replyOf({ output: [{ type: "function_call", name: "shell", arguments: "{}" }] }, "responses"))).toBe("tools") const t = createTracker() expect(t.request({ model: "m", tools: [1], messages: [{ role: "user", content: "x" }] }, { origin: "remote", agent: "a", session: "s" }, 1)).toBeNull() + expect(t.snapshot(1).dropped).toBe(1) + const remoteOn = createTracker({ remote: true }) + expect(remoteOn.request({ model: "m", tools: [1], messages: [{ role: "user", content: "x" }] }, { origin: "gateway-key", agent: "a", session: "s" }, 1)).toBe("a|s") + expect(remoteOn.snapshot(1).dropped).toBe(0) +}) + +test("a gateway install counts dropped non-local requests until remote is enabled", async () => { + const out = tmp() + process.env.MAGPIE_AGENT_OFFICE_PAGE = out + const hook = await import("./office.hooks.mjs") + const body = { model: "m", tools: [{ name: "x" }], messages: [{ role: "user", content: "hi" }] } + await hook.onRequest(body, { origin: "gateway-key", protocol: "anthropic", agent: "cursor", session: "r1", cwd: "/srv", model: "m", query: "hi", options: { remote: false, text: true }, state: {}, report() {} }) + expect(hook.snapshot().dropped).toBeGreaterThanOrEqual(1) + // toggling text while remote stays false preserves the dropped count + await hook.onRequest(body, { origin: "local", protocol: "anthropic", agent: "cursor", session: "l1", cwd: "/srv", model: "m", query: "hi", options: { remote: false, text: false }, state: {}, report() {} }) + expect(hook.snapshot().dropped).toBeGreaterThanOrEqual(1) + // enabling remote resets dropped to 0 and records the remote agent + await hook.onRequest(body, { origin: "gateway-key", protocol: "anthropic", agent: "cursor", session: "r1", cwd: "/srv", model: "m", query: "hi", options: { remote: true, text: true }, state: {}, report() {} }) + expect(hook.snapshot().dropped).toBe(0) + expect(hook.snapshot().sessions.some((s) => s.session === "r1")).toBe(true) + expect(read("index.html")).toContain('id="optRemote"') + expect(read("app.js")).toContain("remote: view.remote") + expect(read("app.js")).toContain("state.snap.dropped > 0") + for (const lang of M.LANGS) { + expect(M.TEXT[lang].remote).toBeTruthy() + expect(M.TEXT[lang].emptyRemote).toBeTruthy() + } }) test("live.js is no file of the package's", () => { diff --git a/packages/agent-office/features.test.mjs b/packages/agent-office/features.test.mjs index aa8ee9b..47cee05 100644 --- a/packages/agent-office/features.test.mjs +++ b/packages/agent-office/features.test.mjs @@ -185,7 +185,7 @@ test("conversations are redacted with the rest of the text", () => { test("the page carries the workbench hooks and still reaches no network", () => { const html = read("index.html") const app = read("app.js") - for (const id of ["toasts", "rail", "railGroups", "todayMenu", "todayTokens", "todayBtn", "sideBtn", "optAgents", "optLang", "optSub", "optIdle"]) { + for (const id of ["toasts", "rail", "railGroups", "todayMenu", "todayTokens", "todayBtn", "sideBtn", "optAgents", "optLang", "optRemote", "optSub", "optIdle"]) { expect(html).toContain(`id="${id}"`) } for (const token of ["M.groups", "M.today", "M.detectLang", "M.redact", "showToast", "document.title"]) expect(app).toContain(token) diff --git a/packages/agent-office/lib/track.mjs b/packages/agent-office/lib/track.mjs index 2f1eefc..df54e03 100644 --- a/packages/agent-office/lib/track.mjs +++ b/packages/agent-office/lib/track.mjs @@ -143,6 +143,7 @@ export function keyOf(ctx) { export function createTracker(opts = {}) { const o = { text: true, remote: false, ...opts } const sessions = new Map() + let dropped = o.remote ? 0 : Math.min(9999, Math.max(0, Math.trunc(Number(opts.dropped) || 0))) function wanted(ctx) { if (!ctx) return false @@ -165,7 +166,10 @@ export function createTracker(opts = {}) { // a request on its way: the session is at work function request(body, ctx, now) { - if (!wanted(ctx)) return null + if (!wanted(ctx)) { + if (ctx && !o.remote) dropped = Math.min(9999, dropped + 1) + return null + } const key = keyOf(ctx) let s = sessions.get(key) const tools = hasTools(body) @@ -225,7 +229,7 @@ export function createTracker(opts = {}) { function snapshot(now) { prune(now) - return { v: 1, written: now, text: o.text, sessions: [...sessions.values()].sort((a, b) => a.started - b.started) } + return { v: 1, written: now, text: o.text, dropped, sessions: [...sessions.values()].sort((a, b) => a.started - b.started) } } // after a restart of the host: what was written last, nothing in flight diff --git a/packages/agent-office/office.hooks.mjs b/packages/agent-office/office.hooks.mjs index 9a5333e..7e8d6b5 100644 --- a/packages/agent-office/office.hooks.mjs +++ b/packages/agent-office/office.hooks.mjs @@ -58,7 +58,7 @@ 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) + tracker = createTracker({ text: o.text, remote: o.remote, dropped: !o.remote && was ? was.snapshot(0).dropped : 0 }) if (was) for (const [k, s] of was.sessions) tracker.sessions.set(k, s) return tracker } @@ -99,14 +99,15 @@ export function snapshot(now = Date.now()) { } 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) { + const tracked = tracker ? track(options).snapshot(now) : null + if (tracked) { + for (const g of tracked.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 } + return { v: 2, written: now, text, dropped: tracked ? tracked.dropped : 0, sessions } } export function write(now = Date.now()) { @@ -166,9 +167,11 @@ export async function onRequest(body, ctx) { // 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()) + const t = track(options) + const dropped = t.snapshot(0).dropped + const key = t.request(body, ctx, Date.now()) ctx.state.office = key - if (key) soon() + if (key || t.snapshot(0).dropped !== dropped) soon() ctx.report?.("skipped") return undefined } diff --git a/packages/agent-office/page/app.js b/packages/agent-office/page/app.js index f363fae..15b58fa 100644 --- a/packages/agent-office/page/app.js +++ b/packages/agent-office/page/app.js @@ -33,6 +33,7 @@ quality: ["low", "medium", "high"].includes(opts.quality) ? opts.quality : "medium", demo: opts.demo === true, showDone: opts.showDone !== false, + remote: opts.remote === true, hideAfter: M.HIDE[opts.hideAfter] !== undefined ? opts.hideAfter : M.DEFAULTS.hideAfter, text: opts.text !== false, approvalAfterSec: Number(opts.approvalAfterSec) > 0 ? Number(opts.approvalAfterSec) : M.DEFAULTS.approvalAfterSec, @@ -628,7 +629,8 @@ } function emptyText() { - return state.loaded && state.snap ? t().empty : t().noFile + if (!state.loaded || !state.snap) return t().noFile + return !view.remote && state.snap.dropped > 0 ? t().empty + " " + t().emptyRemote : t().empty } // ---- the side rail ---------------------------------------------------- @@ -886,7 +888,7 @@ const base = { ...opts } delete base.jumpRequest return { - ...base, mode: view.mode, dim: view.dim, demo: view.demo, showDone: view.showDone, hideAfter: view.hideAfter, + ...base, mode: view.mode, dim: view.dim, demo: view.demo, showDone: view.showDone, remote: view.remote, hideAfter: view.hideAfter, text: textOn(), agents: view.agents.slice(), showSub: view.showSub, showIdle: view.showIdle, side: view.side, sideV: 2, lang: manualLang, scene: view.scene, quality: view.quality, cleared: { ...view.cleared }, preferredJumpHost: view.preferredJumpHost || "", @@ -970,12 +972,14 @@ }) $("optDemo").checked = view.demo $("optDone").checked = view.showDone + $("optRemote").checked = view.remote $("optText").checked = opts.text !== false $("optSub").checked = view.showSub $("optIdle").checked = view.showIdle select("optHide", "hide", view.hideAfter) $("optDemo").addEventListener("change", (e) => { view.demo = e.target.checked; state.demoSnap = null; refresh(); save() }) $("optDone").addEventListener("change", (e) => { view.showDone = e.target.checked; refresh(); save() }) + $("optRemote").addEventListener("change", (e) => { view.remote = e.target.checked; refresh(); save() }) $("optText").addEventListener("change", (e) => { view.text = e.target.checked; refresh(); save() }) $("optSub").addEventListener("change", (e) => { view.showSub = e.target.checked; agentFilterSig = ""; refresh(); save() }) $("optIdle").addEventListener("change", (e) => { view.showIdle = e.target.checked; agentFilterSig = ""; refresh(); save() }) diff --git a/packages/agent-office/page/index.html b/packages/agent-office/page/index.html index a32a5fb..5aa47b6 100644 --- a/packages/agent-office/page/index.html +++ b/packages/agent-office/page/index.html @@ -7,12 +7,12 @@ - + - +
@@ -48,6 +48,7 @@