From 11e0acd9b11cbf99a4eb287128ccb7237b7e0826 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 14:35:42 +0800 Subject: [PATCH] agent-office 0.2.4: a jumpRequest.at ahead of the clock no longer blocks every later jump (#583) 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). --- packages/agent-office/CHANGELOG.md | 6 ++ packages/agent-office/cli.test.mjs | 2 +- packages/agent-office/jump.test.mjs | 102 ++++++++++++++++++++++++- packages/agent-office/lib/jump.mjs | 23 ++++++ packages/agent-office/office.hooks.mjs | 21 +++-- packages/agent-office/package.json | 2 +- 6 files changed, 142 insertions(+), 14 deletions(-) diff --git a/packages/agent-office/CHANGELOG.md b/packages/agent-office/CHANGELOG.md index b4cadd4..69c74a6 100644 --- a/packages/agent-office/CHANGELOG.md +++ b/packages/agent-office/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.2.4 · 2026-11-01 + +### Fixed + +- A `jumpRequest.at` ahead of the clock no longer blocks every later jump. A request is acted on only from 15 s old to 5 s ahead; one stamped further ahead is set aside and remembered, so it never fires when the clock gets there. A `last-jump.json` time ahead of now — written by 0.2.3 for such a request, or after the clock stepped back — now blocks only that same request, and every other click jumps; the file is rewritten at the next request. A request already handled is still never replayed across restarts (#583). + ## 0.2.3 · 2026-11-01 ### Added diff --git a/packages/agent-office/cli.test.mjs b/packages/agent-office/cli.test.mjs index f32da35..68eabff 100644 --- a/packages/agent-office/cli.test.mjs +++ b/packages/agent-office/cli.test.mjs @@ -119,7 +119,7 @@ test("the authenticated standalone page, bridge, live data and JSON options work try { const ping = await (await fetch(s.url + "api/ping")).json() expect(ping.name).toBe("agent-office") - expect(ping.version).toBe("0.2.3") + expect(ping.version).toBe("0.2.4") const page = await fetch(s.url) expect(page.status).toBe(200) expect(await page.text()).toContain('src="_magpie/bridge.js"') diff --git a/packages/agent-office/jump.test.mjs b/packages/agent-office/jump.test.mjs index 26bbd69..654a21b 100644 --- a/packages/agent-office/jump.test.mjs +++ b/packages/agent-office/jump.test.mjs @@ -3,7 +3,7 @@ import { chmodSync, cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rm import { tmpdir } from "node:os" import { join } from "node:path" import { pathToFileURL } from "node:url" -import { appleScriptQuote, buildJumpPlan, createJumper, parseHerdrPanes, parsePs, parseTmuxPanes, resolveTarget, validTty, walkAncestors } from "./lib/jump.mjs" +import { appleScriptQuote, buildJumpPlan, createJumper, judgeJump, parseHerdrPanes, parsePs, parseTmuxPanes, resolveTarget, validTty, walkAncestors } from "./lib/jump.mjs" const dir = import.meta.dir const scope = {} @@ -357,3 +357,103 @@ fi }) + +test("judgeJump acts once near now, and a time ahead of the clock blocks only itself (#583)", () => { + const now = 1_800_000_000_000 + const hour = 3600e3 + // a fresh request fires and is recorded; the same one again does nothing + expect(judgeJump(now, 0, now)).toEqual({ fire: true, record: true }) + expect(judgeJump(now, now, now + 1000)).toEqual({ fire: false, record: false }) + // stale or far ahead: set aside, recorded so it never fires later + expect(judgeJump(now - 16000, 0, now)).toEqual({ fire: false, record: true }) + expect(judgeJump(now + hour, 0, now)).toEqual({ fire: false, record: true }) + expect(judgeJump(now + hour, now + hour, now + hour)).toEqual({ fire: false, record: false }) + // a little ahead (page clock in front) still fires + expect(judgeJump(now + 4000, 0, now)).toEqual({ fire: true, record: true }) + // seen ahead of now (0.2.3 last-jump.json, a clock stepped back): later requests fire + expect(judgeJump(now, now + hour, now)).toEqual({ fire: true, record: true }) + expect(judgeJump(now - 1000, now + 4000, now)).toEqual({ fire: true, record: true }) + // with seen in the past, an older or equal request never replays + expect(judgeJump(now - 1000, now - 500, now)).toEqual({ fire: false, record: false }) + // junk times + for (const at of [0, -1, NaN, Infinity]) expect(judgeJump(at, 0, now)).toEqual({ fire: false, record: false }) +}) + +function hostJumpProbe(root, jumpAt) { + const home = join(root, "home") + const cfg = join(root, "cfg") + const cache = join(root, "cache") + const pkgDir = join(root, "pkg") + mkdirSync(join(pkgDir, "page"), { recursive: true }) + cpSync(join(dir, "lib"), join(pkgDir, "lib"), { recursive: true }) + cpSync(join(dir, "office.hooks.mjs"), join(pkgDir, "office.hooks.mjs")) + cpSync(join(dir, "package.json"), join(pkgDir, "package.json")) + const binDir = join(home, ".local", "bin") + const herdrLog = join(home, "herdr.log") + mkdirSync(binDir, { recursive: true }) + const fakeHerdr = join(binDir, "herdr") + writeFileSync(fakeHerdr, `#!/bin/sh +echo "$@" >> "${herdrLog}" +if [ "$1" = "pane" ] && [ "$2" = "list" ]; then + echo '{"result":{"panes":[{"pane_id":"wM:p8C","tab_id":"wM:t73","workspace_id":"wM","cwd":"/tmp/probe","agent_session":{"value":"11111111-2222-3333-4444-555555555555"}}]}}' +fi +`) + chmodSync(fakeHerdr, 0o755) + const magpieCfg = join(cfg, "magpie") + mkdirSync(magpieCfg, { recursive: true }) + const env = { ...process.env, HOME: home, XDG_CONFIG_HOME: cfg, XDG_CACHE_HOME: cache } + delete env.MAGPIE_AGENT_OFFICE_PAGE + const hookUrl = pathToFileURL(join(pkgDir, "office.hooks.mjs")).href + const code = ` + const h = await import(${JSON.stringify(hookUrl)}); + await h.waitForJump(); + h.stop(); + ` + const lastJump = join(cache, "agent-office", "last-jump.json") + return { + lastJump, + run(at) { + writeFileSync(join(magpieCfg, "plugins.json"), JSON.stringify({ + plugins: [{ + spec: "@magpie-community/page-agent-office", + options: { jumpRequest: { key: "claude|11111111-2222-3333-4444-555555555555", session: "11111111-2222-3333-4444-555555555555", preferredHostId: "herdr-cli", at } }, + }], + }), { mode: 0o600 }) + writeFileSync(herdrLog, "") + const r = Bun.spawnSync({ cmd: [process.execPath, "-e", code], cwd: pkgDir, env, stdout: "pipe", stderr: "pipe" }) + expect(r.exitCode).toBe(0) + return readFileSync(herdrLog, "utf8").includes("workspace focus wM") + }, + } +} + +test("a last-jump.json ahead of the clock (0.2.3) does not block a fresh jump, and it still runs once (#583)", () => { + if (process.platform !== "darwin") return + const root = mkdtempSync(join(tmpdir(), "agent-office-ahead-probe-")) + try { + const p = hostJumpProbe(root) + mkdirSync(join(root, "cache", "agent-office"), { recursive: true }) + writeFileSync(p.lastJump, JSON.stringify({ at: Date.now() + 3600e3 }) + "\n") + const at = Date.now() + expect(p.run(at)).toBe(true) + expect(JSON.parse(readFileSync(p.lastJump, "utf8")).at).toBe(at) + expect(p.run(at)).toBe(false) + } finally { + rmSync(root, { recursive: true, force: true }) + } +}) + +test("a jumpRequest stamped far ahead never fires and does not block the next click (#583)", () => { + if (process.platform !== "darwin") return + const root = mkdtempSync(join(tmpdir(), "agent-office-future-probe-")) + try { + const p = hostJumpProbe(root) + const ahead = Date.now() + 3600e3 + expect(p.run(ahead)).toBe(false) + // recorded, so it can't fire when the clock gets there + expect(JSON.parse(readFileSync(p.lastJump, "utf8")).at).toBe(ahead) + expect(p.run(Date.now())).toBe(true) + } finally { + rmSync(root, { recursive: true, force: true }) + } +}) diff --git a/packages/agent-office/lib/jump.mjs b/packages/agent-office/lib/jump.mjs index 98e1929..1e24b6b 100644 --- a/packages/agent-office/lib/jump.mjs +++ b/packages/agent-office/lib/jump.mjs @@ -30,6 +30,29 @@ function extraPath() { ].join(":") } +// How long a jumpRequest stays good: magpie restarts the plugin host after +// writing it, so up to 15s old; at most 5s ahead, for a page clock a little +// in front of the host's. +export const JUMP_PAST = 15000 +export const JUMP_AHEAD = 5000 + +// judgeJump decides one jumpRequest from its time `at`, the time `seen` of the +// last request the host acted on or set aside (kept across restarts), and the +// host's clock. fire: act on it now. record: remember `at` as the new seen. +// +// A request is acted on once (at === seen is the same request again) and only +// near now. One stamped further ahead is set aside and recorded, so it never +// fires later when the clock gets there. A seen time ahead of now (a request +// recorded ahead, a 0.2.3 last-jump.json, a clock that stepped back) blocks +// only that same request; it no longer orders out every later one. +export function judgeJump(at, seen, now) { + if (!Number.isFinite(at) || at <= 0) return { fire: false, record: false } + const seenAhead = seen > now + if (at === seen || (!seenAhead && at <= seen)) return { fire: false, record: false } + const fresh = now - at <= JUMP_PAST && at - now <= JUMP_AHEAD + return { fire: fresh, record: true } +} + export function validTty(tty) { return typeof tty === "string" && TTY_RE.test(tty) } diff --git a/packages/agent-office/office.hooks.mjs b/packages/agent-office/office.hooks.mjs index 77c73dc..9a5333e 100644 --- a/packages/agent-office/office.hooks.mjs +++ b/packages/agent-office/office.hooks.mjs @@ -20,7 +20,7 @@ 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 } from "./lib/jump.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 @@ -223,7 +223,8 @@ function jumpCacheFile() { function readLastJumpAt() { try { const j = JSON.parse(readFileSync(jumpCacheFile(), "utf8")) - return Number(j?.at) > 0 ? Number(j.at) : 0 + const at = Number(j?.at) + return Number.isFinite(at) && at > 0 ? at : 0 } catch { return 0 } } @@ -249,16 +250,14 @@ function checkJumpOption(directReq) { // 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. - if (!at || at <= lastJumpAt || Date.now() - at > 15000) { - if (at > lastJumpAt) { - lastJumpAt = at - if (!standalone) writeLastJumpAt(at) - } - return + // 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) } - lastJumpAt = at - if (!standalone) writeLastJumpAt(at) + if (!fire) return lastJumpPromise = jumper.jump({ key: String(req.key || ""), session: String(req.session || ""), diff --git a/packages/agent-office/package.json b/packages/agent-office/package.json index 95c5ffe..6cd7156 100644 --- a/packages/agent-office/package.json +++ b/packages/agent-office/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/page-agent-office", - "version": "0.2.3", + "version": "0.2.4", "description": "我的 Agent 在干嘛:读取本机 Claude Code、Codex、Copilot CLI、Kimi Code、DeepSeek Harness、Gemini CLI、Qwen Code、Pi、omp、Qoder、WorkBuddy、CodeBuddy、Grok Build 的会话文件,把每个会话画成真 3D / 等距 / 2D 办公室里的小人,或分组工作台。Live agent activity dashboard for magpie, with a real WebGL office and six scenes.", "type": "module", "files": [