fix(mcp-server): recover sessions in one place, and more fixes from the third review

- A session whose state expired was recovered from its auto-save file only
  for the preview page; the tools built on their older copy and then
  overwrote the file. They now recover it first (restoreSavedSession).
- A preview tab that missed the last AI write pushed its older diagram
  over the recovered one after a restart. It now shows the recovered
  diagram and keeps its own copy in History.
- An empty record of what the model has seen (after load_diagram or a
  page tool on unseen changes) no longer lets one page of a multi-page
  document count for all, and get_diagram counts a page only once found.
- History: a thumbnail goes only to the entry it shows, the cached image
  never belongs to an older diagram, a re-serialized copy adds no entry,
  and a cleared document with its own pages is kept before a restore.
- The root cell id check reads attributes one by one: rack-id="1" or id
  text inside a label no longer counts.
- A compressed page counts as having cells; a saved file that could not
  be read is never written over.
This commit is contained in:
dayuan.jiang
2026-10-05 13:04:32 +09:00
parent 49468f9478
commit cfbf17f97e
12 changed files with 288 additions and 53 deletions
@@ -8,6 +8,7 @@
import http from "node:http"
import { afterAll, beforeAll, describe, expect, it } from "vitest"
import { installDomPolyfill } from "../src/dom.ts"
import { addHistory, getHistory } from "../src/history.ts"
import {
getState,
@@ -23,6 +24,8 @@ import {
let port = 0
beforeAll(async () => {
// XML parsing, as the server installs it at startup
installDomPolyfill()
port = await startHttpServer(40000 + Math.floor(Math.random() * 10000))
})
@@ -350,6 +353,67 @@ describe("history restore", () => {
expect(getState(id)?.xml).toBe("<mxfile>5</mxfile>")
})
const page = (cellId: string) =>
`<mxfile><diagram id="p" name="P"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="${cellId}" vertex="1" parent="1"/></root></mxGraphModel></diagram></mxfile>`
it("gives a thumbnail only to the entry it shows", async () => {
const id = "mcp-history-thumb"
setState(id, page("shown"))
// The last entry is another diagram (a tab's copy kept on recovery)
addHistory(id, page("other"))
await postJson("/api/history-svg", {
sessionId: id,
svg: "SVG-OF-SHOWN",
})
expect(getHistory(id).at(-1)?.svg).toBe("")
addHistory(id, page("shown"))
await postJson("/api/history-svg", {
sessionId: id,
svg: "SVG-OF-SHOWN",
})
expect(getHistory(id).at(-1)?.svg).toBe("SVG-OF-SHOWN")
})
it("never pairs the image of an older diagram with a newer one", async () => {
const id = "mcp-history-stale-svg"
const version = setState(id, page("user"))
await postJson("/api/state", {
sessionId: id,
xml: page("user2"),
svg: "SVG-OF-USER2",
baseVersion: version,
})
// An AI write without an image of its own
setState(id, page("ai"))
addHistory(id, page("older"))
const [entry] = getHistory(id)
await postJson("/api/restore", { sessionId: id, id: entry.id })
const kept = getHistory(id).find((e) => e.xml === page("ai"))
expect(kept?.svg).toBe("")
})
it("keeps a cleared document with renamed pages before restoring", async () => {
const id = "mcp-history-empty-pages"
const emptyPage = (name: string) =>
`<diagram id="${name}" name="${name}"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel></diagram>`
const cleared = `<mxfile>${emptyPage("Planning")}${emptyPage("Notes")}</mxfile>`
addHistory(id, page("before"))
setState(id, cleared, undefined, true)
const [entry] = getHistory(id)
await postJson("/api/restore", { sessionId: id, id: entry.id })
expect(getHistory(id).map((e) => e.xml)).toContain(cleared)
})
it("adds no entry for a re-serialized copy of the last one", () => {
const id = "mcp-history-dedupe"
addHistory(id, page("same"))
addHistory(
id,
page("same").replace("<mxGraphModel>", '<mxGraphModel dx="10">'),
)
expect(getHistory(id)).toHaveLength(1)
})
it("keeps manual edits in history before restoring", async () => {
const id = "mcp-history-manual"
const doc = (cellId: string) =>