From e7049a6c64b02ff2a86c6ceed4008707e20b850a Mon Sep 17 00:00:00 2001 From: SXP-Simon Date: Wed, 25 Nov 2026 20:19:28 +0800 Subject: [PATCH] =?UTF-8?q?fix(crates/panache-mcp):=20=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E5=B1=80=E9=83=A8=E5=AD=97=E5=BD=A2=E8=B0=83=E6=95=B4=E5=89=8D?= =?UTF-8?q?=E6=A0=B8=E5=AF=B9=E7=9B=AE=E6=A0=87=E6=96=87=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 背景:稿件重排后,同一个编号可能指向另一个字。即使提交了最新版本号,单凭编号也可能误改用户没有点名的文字。 解决手段:引导调用者沿用查布局时的版本,并支持按编号顺序提供预期文字。沿用预览的文本映射,在写入前核对整组选字。补充真实 MCP 场景测试。 收益:文字与编号不符时整批保留原样,目标吻合时沿用原有保存流程,旧客户端与纯位置选区仍可使用。 --- crates/panache-mcp/README.md | 2 +- crates/panache-mcp/src/engine.rs | 9 +++-- crates/panache-mcp/src/engine/adjustments.rs | 20 +++++++++- crates/panache-mcp/src/inputs.rs | 3 ++ crates/panache-mcp/src/main.rs | 2 +- crates/panache-mcp/tests/smoke.py | 39 ++++++++++++++++++++ 6 files changed, 69 insertions(+), 6 deletions(-) diff --git a/crates/panache-mcp/README.md b/crates/panache-mcp/README.md index 1b704411..4e0c7d74 100644 --- a/crates/panache-mcp/README.md +++ b/crates/panache-mcp/README.md @@ -109,7 +109,7 @@ cargo build --locked --release -p panache-mcp 1. 表格、无横线纸和画作均可排字。用户已指定位置或委托选位时,直接按意图排版。落字区有歧义时,简短问“要写在哪个栏,哪些地方留白?”若必填区域在画外,或失焦、遮挡、透视已使它无法定位,建稿前指出具体缺哪一块,请用户补拍该区域或确认只填可见部分。已明确要求只写正文时,直接填写可定位的正文区。 2. 用 `save_section` 导入照片并保留版本号。用 `preview_handwriting(view="review")` 取得同一页的底纸、成品和叠图。整页复查图长边最多 900 像素,像素坐标以返回的 `photo_size` 为准。检查有字区域的横线、页眉、页边和需要保留的图案。 3. 调用 `tune_handwriting(draft_id, expected_revision, paper_guides, note)` 在原照片上重排全文。至少给两条参考线,每条提供行号与左右端点。无横线纸也可以指定想写入的区域。传空数组恢复建稿时的校准。旧版本仍可回看;改写书写区域会重置冲突的边距覆盖,重新排版后需重新选字。 -4. 局部改动用 `preview_handwriting(view="layout", region=[[x,y],…])` 圈选字形中心,结果按页分批。把选中的 ID 和相同版本交给 `adjust_glyphs`。`place_center` 将一组字移到指定照片位置;`target_quad` 按左上、右上、右下、左下四角放置所选字块,替换该组已有几何调整。局部字形还可分别调倾斜、浓度和笔画。调整后复查新版本。 +4. 局部改动用 `preview_handwriting(view="layout", region=[[x,y],…])` 圈选字形中心,结果按页分批。把选中的 ID 和这次布局返回的版本交给 `adjust_glyphs`。用户点名要改哪几个字时,按 `ids` 的顺序传 `expected_texts`,例如 `ids=[4,6], expected_texts=["春","雨"]`。任一编号对应的文字不符,整批都不保存;版本冲突时重新查布局并定位。相同的字出现多次时,用上下文或位置区分。纯位置选区和旧客户端可以省略文字核对。`place_center` 将一组字移到指定照片位置;`target_quad` 按左上、右上、右下、左下四角放置所选字块,替换该组已有几何调整。局部字形还可分别调倾斜、浓度和笔画。调整后复查新版本。 5. 用 `read_section(view="trajectory")` 回看该稿的近期操作、简短调整理由和按页记录的复盘指标。有下一页时,把 `next_cursor` 作为下一次调用的 `cursor`。把需要人判断的歧义带回当前会话;确定符合用途后导出同一版本。 要核对局部改动,用 `preview_handwriting(view="review", region=…, revision=新版本, compare_revision=旧版本)` 查看同一区域的底纸、改前和改后图。移动字块时还可用 `previous_region` 圈出旧位置。需要保护的文字或页眉可单独圈选复查。区域使用原尺寸成品像素。[局部坐标与测量示例](tool-design.md#局部改动的复查) diff --git a/crates/panache-mcp/src/engine.rs b/crates/panache-mcp/src/engine.rs index 7ad34d4f..8c3c97d8 100644 --- a/crates/panache-mcp/src/engine.rs +++ b/crates/panache-mcp/src/engine.rs @@ -12,6 +12,11 @@ use crate::{ use handwriting_core::fine_edit::{self, Adjustment, Adjustments}; use serde_json::{Value, json}; +fn glyph_source<'a>(text: &'a [u16], glyph: &fine_edit::GlyphBounds) -> Option<&'a [u16]> { + let [start, end] = glyph.source_utf16?; + text.get(start as usize..end as usize) +} + // Coordinates relative to the original tile center, transformed in paint order. fn glyph_point( g: &fine_edit::GlyphBounds, @@ -408,9 +413,7 @@ impl Engine { json!([project(x, g.baseline), project(x + w, g.baseline)]); } } - if let Some([a, b]) = g.source_utf16 - && let Some(chars) = utf16.get(a as usize..b as usize) - { + if let Some(chars) = glyph_source(&utf16, g) { row["text"] = json!( String::from_utf16_lossy(chars) .chars() diff --git a/crates/panache-mcp/src/engine/adjustments.rs b/crates/panache-mcp/src/engine/adjustments.rs index 7ad2eaf5..b224adab 100644 --- a/crates/panache-mcp/src/engine/adjustments.rs +++ b/crates/panache-mcp/src/engine/adjustments.rs @@ -1,4 +1,4 @@ -use super::{Engine, glyph_point}; +use super::{Engine, glyph_point, glyph_source}; use crate::{ Nudge, Shape, Style, drafts::{Section, Snapshot}, @@ -34,6 +34,18 @@ impl Engine { selected.len() == changes.ids.len(), "Glyph not on this page; inspect its layout first" ); + if let Some(texts) = &input.expected_texts { + let utf16: Vec<_> = req.text.encode_utf16().collect(); + for (id, expected) in input.ids.iter().zip(texts) { + let glyph = selected[id]; + ensure!( + glyph.illustration.is_none() + && glyph_source(&utf16, glyph) + .is_some_and(|s| expected.encode_utf16().eq(s.iter().copied())), + "Glyph text mismatch at ID {id}; read the latest layout and locate the intended text before retrying" + ); + } + } let mut edits: BTreeMap<_, _> = req .params .fine_edits @@ -87,6 +99,12 @@ impl<'a> GlyphChanges<'a> { !ids.is_empty() && ids.len() == input.ids.len() && ids.len() <= 32, "Choose 1..32 distinct glyph IDs" ); + if let Some(texts) = &input.expected_texts { + ensure!( + texts.len() == input.ids.len() && texts.iter().all(|text| !text.is_empty()), + "Supply one nonempty expected text per glyph ID, in the same order" + ); + } ensure!( input.target_quad.is_some() || input.place_center.is_some() diff --git a/crates/panache-mcp/src/inputs.rs b/crates/panache-mcp/src/inputs.rs index 68b41ac0..8e80432d 100644 --- a/crates/panache-mcp/src/inputs.rs +++ b/crates/panache-mcp/src/inputs.rs @@ -220,11 +220,14 @@ pub struct Nudge { /// Optional short user feedback or observable reason to retain with this revision. pub note: Option, pub draft_id: String, + /// Use the revision returned by the layout that supplied these IDs. On conflict, read the latest layout and locate the intended text again. pub expected_revision: u32, pub section: u32, pub page: usize, /// Up to 32 glyph IDs from preview_handwriting(view="layout") on this page. pub ids: Vec, + /// For a named text target, send its intended glyph text in ids order (one exact string per ID). A mismatch rejects the whole edit. For repeated text, use nearby words or position to select the occurrence. + pub expected_texts: Option>, /// Move this selection as a group so its mean glyph center lands at [x,y] on the preview. Use separately from dx, dy and shape. pub place_center: Option<[u32; 2]>, /// Image destinations of the original block's TL,TR,BR,BL corners, in that order (for a 90° counterclockwise sidebar: BL,TL,TR,BR of the field). Replaces prior geometry; use separately from other geometry changes. diff --git a/crates/panache-mcp/src/main.rs b/crates/panache-mcp/src/main.rs index 5f9f571b..6da51c9b 100644 --- a/crates/panache-mcp/src/main.rs +++ b/crates/panache-mcp/src/main.rs @@ -509,7 +509,7 @@ impl Panache { } #[tool( - description = "Adjust selected glyphs in one section/page by IDs from its layout view. Use the same revision. Offsets and shape are in paper space before sheet projection. Use place_center for a group move; target_quad maps the entire selected text block into four corners on the image. Shape adjusts each glyph individually." + description = "Adjust selected glyphs in one section/page by IDs from its layout view. Use the same revision. Offsets and shape are in paper space before sheet projection. Use place_center for a group move; target_quad maps the entire selected text block into four corners on the image. Shape adjusts each glyph individually. An ID is local to the exact layout revision, section and page that returned it. After an intervening edit, read the latest layout and resolve the intended glyph again; do not reuse old IDs by changing only expected_revision." )] async fn adjust_glyphs( &self, diff --git a/crates/panache-mcp/tests/smoke.py b/crates/panache-mcp/tests/smoke.py index 2bb2c069..6b4b7aba 100644 --- a/crates/panache-mcp/tests/smoke.py +++ b/crates/panache-mcp/tests/smoke.py @@ -168,6 +168,44 @@ async def handwriting_variation_sample(client): assert source["markdown"] == markdown +async def glyph_target_text(client): + saved = (await call(client, "save_section", markdown="春 雨", style={"seed": 42})).structuredContent + key = {"draft_id": saved["draft_id"], "section": 1, "page": 1} + + async def layout(): + return (await call(client, "preview_handwriting", **key, view="layout")).structuredContent + + before = await layout() + old_spring = next(g["id"] for g in before["glyphs"] if g["text"] == "春") + await call(client, "save_section", draft_id=key["draft_id"], section=1, + expected_revision=before["revision"], markdown="明 日 春 雨") + current = await layout() + ids = {g["text"]: g["id"] for g in current["glyphs"]} + assert ids["明"] == old_spring and ids["春"] != old_spring + + for revision, targets, words, reason in ( + (before["revision"], [old_spring], ["春"], "Revision conflict"), + (current["revision"], [ids["雨"], old_spring], ["雨", "春"], "Glyph text mismatch"), + (current["revision"], [ids["春"], ids["雨"]], ["春"], "one nonempty expected text"), + (current["revision"], [ids["春"]], [""], "one nonempty expected text"), + ): + failed = await client.call_tool("adjust_glyphs", { + **key, "expected_revision": revision, "ids": targets, + "expected_texts": words, "color": "#cc1833", + }) + assert failed.isError and reason in failed.content[0].text, failed.content + unchanged = await layout() + assert unchanged["revision"] == current["revision"] and unchanged["glyphs"] == current["glyphs"] + + # Request order differs from page order. Each string must match its own ID. + await call(client, "adjust_glyphs", **key, expected_revision=current["revision"], + ids=[ids["雨"], ids["春"]], expected_texts=["雨", "春"], color="#cc1833") + after = await layout() + assert after["revision"] == current["revision"] + 1 + assert [g["text"] for g in after["glyphs"] + if g.get("adjustment", {}).get("color") == [204, 24, 51]] == ["春", "雨"] + + async def placement_pixels(client): saved = (await call(client, "save_section", markdown="字", style={"seed": 17})).structuredContent key = {"draft_id": saved["draft_id"]} @@ -355,6 +393,7 @@ async def smoke(command, arguments, out): await mixed_script_sample(client) await handwriting_variation_sample(client) await placement_pixels(client) + await glyph_target_text(client) await coursework_form.exercise(client, Path(temp)) comparison_key, reviewed_id, user_quote = await review_comparison.exercise(client, Path(temp), call) # Keep the traditional eight verse lines for a single folio.