An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve escaped paste text and repair regression tests
Daniel45fdf89455fc
diff --git a/app/src/protyle/render/av/attributePanelDrag.test.ts b/app/src/protyle/render/av/attributePanelDrag.test.tsindex cdc64b8c62..860f5e2d9b 100644--- a/app/src/protyle/render/av/attributePanelDrag.test.ts+++ b/app/src/protyle/render/av/attributePanelDrag.test.ts@@ -10,6 +10,8 @@ import {ScriptTarget, transpileModule} from "typescript";const browserCases = async (source: string, css: string) => {const check: typeof assert = require("node:assert/strict");+ check.ok(Math.abs(window.innerWidth - 1500) <= 2 && Math.abs(window.innerHeight - 1200) <= 2,+ `unexpected drag test viewport: ${window.innerWidth}x${window.innerHeight}`);Object.assign(window, {siyuan: {config: {editor: {}}, languages: {database: "Database"}, dialogs: []}});document.head.innerHTML = `<style>${css}:root {--b3-theme-primary:#1565c0}@@ -282,7 +284,10 @@ test("database attribute panels render responsive columns with stable drag previwriteFileSync(script, `const {app, BrowserWindow} = require("electron");app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});app.whenReady().then(async () => {- const win = new BrowserWindow({show: false, width: 1500, height: 1200, webPreferences: {nodeIntegration: true, contextIsolation: false}});+ const win = new BrowserWindow({show: false, width: 1500, height: 1200, useContentSize: true,+ webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});+ // 显式设置内容尺寸,避免初始窗口受屏幕工作区限制而裁掉拖放命中目标。+ win.setContentSize(1500, 1200);win.webContents.on("console-message", event => console.error(event.message));try {await win.loadURL("data:text/html,<html><body></body></html>");diff --git a/app/src/protyle/util/paste.test.ts b/app/src/protyle/util/paste.test.tsindex e5680e5138..ec768d8791 100644--- a/app/src/protyle/util/paste.test.ts+++ b/app/src/protyle/util/paste.test.ts@@ -7,13 +7,14 @@ import * as ts from "typescript";import {getAVRichTextSafeURL} from "../render/av/richTextValue";import {normalizePictureImages} from "./pictureImages";import {htmlMathPaste, markdownMathPaste} from "./mathPaste";+import {escapeMarkdownPlainText} from "../../util/escape";// 执行粘贴入口,替换剪贴板和上传接口,检查图片不会在受限片段中被丢弃。const source = ts.transpileModule(readFileSync(join(process.cwd(), "src/protyle/util/paste.ts"), "utf8"), {compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022},}).outputText;-const createHarness = (disabled = false, richPaste = false) => {+const createHarness = (disabled = false, richPaste = false, lute?: Lute) => {const uploads: Array<{files: unknown; options: {document: {rootID: string}; insertPosition: unknown}}> = [];const position = {range: {startContainer: {}}};let available = true;@@ -21,6 +22,7 @@ const createHarness = (disabled = false, richPaste = false) => {let convertedHTML = "";let convertedBy = "";let checkedBlockDOM = "";+ let clipboardText = "";const restrictedFallback = new Error("restricted fallback");const mocks: Record<string, unknown> = {"./pictureImages": {normalizePictureImages},@@ -46,7 +48,8 @@ const createHarness = (disabled = false, richPaste = false) => {"./wpsPresentation": {extractWPSPresentationClipboard: (): undefined => undefined},"./markdownClipboard": {shouldPasteMarkdownFromHTML: () => false},"./mathPaste": {htmlMathPaste, markdownMathPaste},- "./compatibility": {getLocalFiles: async () => localFiles, isInHarmony: () => false,+ "../../util/escape": {escapeMarkdownPlainText},+ "./compatibility": {getLocalFiles: async () => localFiles, readText: async () => clipboardText, isInHarmony: () => false,getTextSiyuanFromTextHTML: (html: string) => ({textSiyuan: "", textHtml: html})},"../upload": {uploadFiles: (_protyle: unknown, files: unknown, _element: unknown, _success: unknown,@@ -60,10 +63,10 @@ const createHarness = (disabled = false, richPaste = false) => {window: {siyuan: {config: {editor: {mathDelimiterCompatibility: true}}}},document: {createElement: () => ({content: {querySelector: (): null => null}})},});- const api = module.exports as typeof import("./paste");- const paste = (files: unknown[] = [], siyuanHTML = "", textPlain = "", textHTML = "") => api.paste({+ const api = module.exports as {-readonly [K in keyof typeof import("./paste")]: typeof import("./paste")[K]};+ const protyle = {wysiwyg: {element: {}},- lute: {SetLatexMath: () => {}, Md2BlockDOM: (html: string) => {+ lute: lute || {SetLatexMath: () => {}, Md2BlockDOM: (html: string) => {convertedHTML = html;convertedBy = "markdown";return `<div data-type="NodeParagraph">${html}</div>`;@@ -72,18 +75,54 @@ const createHarness = (disabled = false, richPaste = false) => {convertedBy = "html";return `<div data-type="NodeParagraph">${html}</div>`;}},- } as unknown as IProtyle, {+ } as unknown as IProtyle;+ const paste = (files: unknown[] = [], siyuanHTML = "", textPlain = "", textHTML = "") => api.paste(protyle, {target: {}, stopPropagation() {}, preventDefault() {},clipboardData: {files, types: [], getData: (type: string) => type === "text/siyuan" ? siyuanHTML :type === "text/plain" ? textPlain : type === "text/html" ? textHTML : ""},} as unknown as ClipboardEvent & {target: HTMLElement});- return {paste, uploads, position, restrictedFallback, api: module.exports as typeof import("./paste"),+ const pasteEscaped = async (text: string) => {+ clipboardText = text;+ const originalPaste = api.paste;+ let pending: ReturnType<typeof api.paste> | undefined;+ api.paste = (...args) => {+ pending = originalPaste(...args);+ void pending.catch(() => {});+ return pending;+ };+ try {+ await api.pasteEscaped(protyle, {} as HTMLElement);+ assert.ok(pending, "escaped paste must reach the shared paste entry");+ await assert.rejects(pending, restrictedFallback);+ } finally {+ api.paste = originalPaste;+ }+ };+ return {paste, pasteEscaped, uploads, position, restrictedFallback, api,getConvertedHTML: () => convertedHTML, getConvertedBy: () => convertedBy,getCheckedBlockDOM: () => checkedBlockDOM,setLocalFiles: (files: unknown[]) => { localFiles = files; },invalidate: () => { available = false; }};};+describe("escaped text paste", () => {+ if (typeof Lute === "undefined") {+ require("../../../stage/protyle/js/lute/lute.min.js");+ }+ for (const richPaste of [false, true]) {+ it(`preserves literal links and math delimiters with math compatibility enabled (richPaste=${richPaste})`, async () => {+ const lute = Lute.New();+ const harness = createHarness(false, richPaste, lute);+ const text = "**literal** [link](https://example.com) \\(x\\) \\[y\\]";+ await harness.pasteEscaped(text);+ const dom = harness.getCheckedBlockDOM();+ assert.doesNotMatch(dom, /data-type="(?:inline-math|NodeMathBlock|strong|a)"/);+ assert.equal(lute.BlockDOM2Content(dom).trim(), text);+ assert.match(markdownMathPaste(lute, "\\(x\\)", true), /data-type="inline-math"/);+ });+ }+});+describe("restricted cell image paste", () => {it("uploads screenshot clipboard files with the captured document and cursor", async () => {const harness = createHarness();diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.tsindex 4f13d36e2f..f8b5dc68cb 100644--- a/app/src/protyle/util/paste.ts+++ b/app/src/protyle/util/paste.ts@@ -305,7 +305,7 @@ export const pasteEscaped = async (protyle: IProtyle, nodeElement: Element, prep// 多加入的一个反斜杆会作为文本节点保留下来,后续 Spin 时刚好用于转义标记符clipText = escapeMarkdownPlainText(clipText);// 转义文本不能使用 DOM 结构 https://github.com/siyuan-note/siyuan/issues/11778- paste(protyle, {textPlain: clipText, textHTML: "", target: nodeElement as HTMLElement});+ paste(protyle, {textPlain: clipText, textHTML: "", target: nodeElement as HTMLElement, escapedText: true});} catch (e) {console.log(e);}@@ -707,10 +707,12 @@ export const shouldPasteClipboardImage = (textHTML: string, isAVTarget: boolean)};export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEvent | IClipboardData) & {- target: HTMLElement+ target: HTMLElement;+ escapedText?: boolean;}, uploadOptions?: IUploadInsertOptions) => {const blockDOMSanitizer = getProtyleBlockDOMSanitizer(protyle);- const mathCompatible = window.siyuan.config.editor.mathDelimiterCompatibility !== false;+ // 原义粘贴中的反斜杠用于 Markdown 转义,不能作为外部公式分隔符。+ const mathCompatible = !event.escapedText && window.siyuan.config.editor.mathDelimiterCompatibility !== false;if ("clipboardData" in event || "dataTransfer" in event) {event.stopPropagation();event.preventDefault();diff --git a/app/tests/avAttributeCarrier.test.js b/app/tests/avAttributeCarrier.test.jsindex df00df95b4..b34a5ffefe 100644--- a/app/tests/avAttributeCarrier.test.js+++ b/app/tests/avAttributeCarrier.test.js@@ -223,6 +223,7 @@ const runElectron = async () => {};// 执行实际渲染、菜单请求和事务组装函数,仅隔离网络、富文本显示与编辑器外壳。const sources = [extract("capabilities.generated"), extract("capabilities"), extract("cellValue"), extract("dragFillValue", ["rebindAVCellValue"]),+ extract("attributePanelLayout"),extract("blockAttr", ["renderAVAttribute", "renderAttributeViewBacklinks"]),extract("openMenuPanel", ["openMenuPanel"]), extract("cell", ["updateCellsValue"]),extract("primaryFocus", ["focusDatabasePrimary"]),diff --git a/app/tests/avAttributeNavigation.test.js b/app/tests/avAttributeNavigation.test.jsindex d1d3a6d9e1..06820dff7c 100644--- a/app/tests/avAttributeNavigation.test.js+++ b/app/tests/avAttributeNavigation.test.js@@ -146,7 +146,7 @@ const runElectron = async () => {let exitCode = 0;try {const ts = require("typescript");- const sources = Object.fromEntries(["blockAttr", "attributeValue", "cellValue", "blockIcon", "capabilities", "capabilities.generated", "../../util/hasClosest"].map(name => [+ const sources = Object.fromEntries(["blockAttr", "attributePanelLayout", "attributeValue", "cellValue", "blockIcon", "capabilities", "capabilities.generated", "../../util/hasClosest"].map(name => [name === "../../util/hasClosest" ? name : "./" + name,ts.transpileModule(readFileSync(path.join(__dirname, `../src/protyle/render/av/${name}.ts`), "utf8"),{compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020}}).outputText,diff --git a/kernel/agent/compaction_test.go b/kernel/agent/compaction_test.goindex 5141bb4849..8fca94ec1c 100644--- a/kernel/agent/compaction_test.go+++ b/kernel/agent/compaction_test.go@@ -545,7 +545,7 @@ func TestAgentChatRetriesOverflowAfterProactiveCompaction(t *testing.T) {setupCompactionAgentTest(t)entries := []SessionEntry{{ID: "user-1", Type: "user", Content: "first completed task"},- {ID: "assistant-1", Type: "assistant", Content: strings.Repeat("first result ", 6000)},+ {ID: "assistant-1", Type: "assistant", Content: strings.Repeat("first result ", 12000)},{ID: "user-2", Type: "user", Content: "second completed task"},{ID: "assistant-2", Type: "assistant", Content: strings.Repeat("second result ", 6000)},{ID: "user-3", Type: "user", Content: "current task"},@@ -561,12 +561,18 @@ func TestAgentChatRetriesOverflowAfterProactiveCompaction(t *testing.T) {t.Fatal(err)}- requestTools := currentCapabilitiesForTest(t).definitions+ capabilities := currentCapabilitiesForTest(t)+ requestTools := capabilities.definitionsfullTokens := estimateChatRequestTokens(- "test-model", checkpointMessagesToOpenAIWithSummary(entriesToAgentMessages(entries), "English", nil, nil), requestTools)+ "test-model", checkpointMessagesToOpenAIWithSummary(entriesToAgentMessages(entries), "English", capabilities, nil), requestTools)afterFirstTokens := estimateChatRequestTokens(- "test-model", checkpointMessagesToOpenAIWithSummary(entriesToAgentMessages(entries[2:]), "English", nil, nil), requestTools)+ "test-model", checkpointMessagesToOpenAIWithSummary(entriesToAgentMessages(entries[2:]), "English", capabilities, nil), requestTools)contextLimit := compactionTestContextLimit(t, fullTokens, afterFirstTokens)+ // 首次压缩须在目标预算内保留第二轮历史,供服务端报溢出后继续压缩。+ targetBudget := contextInputBudget(contextLimit, 512) * 3 / 4+ if afterFirstTokens+compactionSummaryOverhead+compactionSummaryMinTokens > targetBudget {+ t.Fatalf("test context cannot retain the second turn after proactive compaction: base=%d, target=%d", afterFirstTokens, targetBudget)+ }var requestCount atomic.Int32server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {@@ -579,6 +585,10 @@ func TestAgentChatRetriesOverflowAfterProactiveCompaction(t *testing.T) {case 1:writeCompactionSummaryStream(t, w, "The first task is complete", 90, 8)case 2:+ runtime, err := loadRuntimeState(testSessionID)+ if err != nil || runtime == nil || runtime.Compaction == nil || runtime.Compaction.CoveredMessageCount != 2 {+ t.Errorf("proactive compaction must cover only the first completed turn: runtime=%+v, err=%v", runtime, err)+ }w.Header().Set("Content-Type", "application/json")w.WriteHeader(http.StatusBadRequest)_, _ = fmt.Fprint(w,diff --git a/kernel/api/contract_history_snapshots_test.go b/kernel/api/contract_history_snapshots_test.goindex e0d172e575..b54a32e454 100644--- a/kernel/api/contract_history_snapshots_test.go+++ b/kernel/api/contract_history_snapshots_test.go@@ -14,6 +14,7 @@ import ("os""os/exec""path/filepath"+ "strconv""strings""testing""time"@@ -60,9 +61,11 @@ func TestAPIContractDocHistorySnapshots(t *testing.T) {t.Cleanup(sql.CloseDatabase)const id = "20260928100000-abcdefg"const box = "20260928100000-normal1"- const created = "1790560800"+ historyTime := time.Now().Add(-time.Hour)+ created := strconv.FormatInt(historyTime.Unix(), 10)+ historyDir := historyTime.Format("2006-01-02-150405") + "-update/"data := []byte(`{"ID":"` + id + `","Spec":"2","Type":"NodeDocument","Properties":{"id":"` + id + `","title":"History"},"Children":[]}`)- historyPath := "2026-09-28-100000-update/" + box + "/" + id + ".sy"+ historyPath := historyDir + box + "/" + id + ".sy"for _, p := range []string{filepath.Join(util.HistoryDir, historyPath), filepath.Join(util.DataDir, box, id+".sy")} {if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {t.Fatal(err)@@ -73,7 +76,7 @@ func TestAPIContractDocHistorySnapshots(t *testing.T) {}sql.IndexHistoriesQueue([]*sql.History{{ID: id, Type: model.HistoryTypeDoc, Op: "update", Title: "History", Path: historyPath, Created: created},- {ID: "20260928100000-locked1", Type: model.HistoryTypeDoc, Op: "update", Title: "Locked", Path: "2026-09-28-100000-update/" + lockedBoxID + "/20260928100000-locked1.sy", Created: created},+ {ID: "20260928100000-locked1", Type: model.HistoryTypeDoc, Op: "update", Title: "Locked", Path: historyDir + lockedBoxID + "/20260928100000-locked1.sy", Created: created},})sql.FlushHistoryQueue()repo, err := dejavu.NewRepo(util.DataDir, util.RepoDir, util.HistoryDir, util.TempDir, "test", "test", "test", model.Conf.Repo.Load().Key, nil, nil)