Skip to content

[6221] 附件预检策略上移宿主并修复不可读附件持续处理问题 - #4748

Merged
PinkMagicFly merged 4 commits into
mainfrom
pigmagicfly/6221/unreadable-attachment-error
Oct 20, 2026
Merged

PinkMagicFly merged 4 commits into
mainfrom
pigmagicfly/6221/unreadable-attachment-error

Conversation

@PinkMagicFly

@PinkMagicFly PinkMagicFly commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

变更概述

修复不可读文件添加为 Chat 附件后,附件按钮持续旋转、处理状态不退出且没有错误提示的问题。实施架构迁移:将附件预检策略从插件层上移至宿主层(init-attachment.scm 显式加载 attachment-policy.scm),真假插件共享同一套预检策略契约与终态保证;假附件插件 tm-attachment.scm 瘦身,下发请求 JSON 瘦身为 {taskId, path}。不可读等违规附件由宿主预检前置拦截,通过 attachment-dispatch-error 以 exec-delayed 异步派发 qt-chat-attachment-error,复用现有 Qt 清理链移除处理中条目、停止按钮动画、解除发送栅栏并弹出中文 toast,同时保证权限恢复后可读附件流程完全正常。

预检上移宿主后,附件链路首次由宿主直接执行编码敏感操作,暴露并修复了三处此前被插件层掩盖的缺陷:错误提示误报「附件插件不可用」、中文 toast 乱码、中文路径被误判为「文件不存在」。

详细改动

  1. TeXmacs/plugins/attachment/progs/attachment-policy.scm(新增):

    • 提取纯策略文件:定义策略常量(attachment-max-count、attachment-max-total-bytes、attachment-allowed-extensions)与纯函数 attachment-host-precheck;
    • 预检规则顺序:文件不存在 → 不可读 → 类型不支持 → 数量超限 → 总大小超限。size 由调用方在 (and (path-file? p) (access fs-path 'R_OK)) 守卫下安全求值传入,避免对不可读文件求大小抛出 permission-error。
  2. TeXmacs/plugins/attachment/progs/init-attachment.scm:

    • 顶部显式 $TEXMACS_PATH 加载 attachment-policy.scm;
    • 提取 attachment-dispatch-error 助手:统一 exec-delayed 错误派发,解决 C++ attachTaskSession_ 在 glue 调用返回后才注册的时序问题;重复派发由 attachTaskSession_ 的 take 幂等吸收,无需宿主防重表;
    • attachment-ingest 前置执行宿主预检:违规立即派发错误并返回 #t 触发面板清理,通过则向插件下发瘦身后的 {taskId, path} 请求 JSON;
    • 统一通道异常输出(attachment-session-fallback-error)与 cancel 兜底路径。
  3. TeXmacs/plugins/attachment/goldfish/tm-attachment.scm(瘦身):

    • 删除插件侧策略常量与 fake-attachment-precheck;
    • fake-attachment-ingest 仅解析 taskId 与 path;
    • fake-attachment-ready-event 单点计算 filename 与 size 并生成 ready 事件;
    • 保留全流程 catch #t 异常兜底、假缓存 sweep 与非法 JSON 保护作为纵深防御。
  4. 宿主预检暴露的三处缺陷修复(init-attachment.scm):

    • 形参遮蔽致误报「附件插件不可用」:attachment-ingest 形参命名为 path 会遮蔽 (liii path) 的 path 构造器,s7 中字符串可直接被调用(等价 string-ref),(path path) 按字符串应用抛 type-error,经 C++ call 降级为假值后 toast「附件插件不可用」而非真实预检结论。形参改名为 file-path;
    • 中文路径被误判「文件不存在」:C++ 按 [6217] 假附件插件预检新增文件可访问性检查并修复非 ASCII 路径编码 #4729 契约以 from_qstring 传入 herk 实体形态路径,该形态仅在 wire 上由 connection_write 的 tree_herk_to_utf8 转回真实 UTF-8;宿主预检不经 wire,直接以实体串做文件系统调用会使中文路径 path-file? 恒假。attachment-ingest 入口先经 cork->utf8 得 fs-path 专供文件系统调用,而文案文件名与 wire JSON 继续保持 herk 形态(各出口另有确定性解码);
    • 中文 toast 乱码:宿主 scheme 中文字面量为裸 UTF-8,经 glue 送达 toast 前须在字面量源头包 utf8->cork 转实体形态(toast 侧 cork_to_utf8 只认实体)。该转换不可放在统一出口——utf8->cork 会把已是实体的通道文本转坏(< → <less>),故契约注释记于 attachment-dispatch-error。
  5. src/Plugins/Qt/qt_chat_controller.cpp & qt_chat_tab_widget.cpp:

    • 更新 onAttachRequested 注释:预检归属宿主,下发请求仅含 taskId 与 path;
    • 复用既有 onAttachmentError → takeAttachmentPanel → attachmentError → attachment_items_changed 状态清理链,重算 hasProcessingAttachments 停止动画并更新发送栅栏;
    • ChatConversationPanel::setup_ui 保持无头环境兼容,attachButton() 访问器供单测断言。
  6. 自动化测试:

    • TeXmacs/plugins/llm/progs/llm/tests/chat-protocol-test.scm:加载 attachment-policy.scm 全量测试 attachment-host-precheck 边界与 chmod 000 不可读断言;新增 test-attachment-host-precheck-nonascii-path 断言 herk 形态路径须 cork->utf8 解码后方可查得文件;测试假插件瘦身后的 ready 事件生成、非法 JSON 及故障注入异常兜底;
    • tests/Plugins/Qt/qt_chat_tab_widget_test.cpp:覆盖收到错误后条目删除、动画停止与栅栏解禁;
    • tests/Plugins/Qt/qt_chat_controller_test.cpp:覆盖 onAttachmentError 完整路由与栅栏重算。

验证说明

  • 代码格式化:gf fmt --check 全量通过(基于 goldfish v18.11.40)
  • 单元测试:
    • xmake r chat-protocol-test:61 checks passed(新增 3 条非 ASCII 路径断言)
    • xmake r qt_chat_controller_test:0 failed
    • xmake r qt_chat_tab_widget_test:0 failed
  • headless 端到端(真实 bundle 直驱 attachment-ingest):中文可读路径正常通过预检并拉起插件(修复前误报「文件不存在」)、中文不可读路径、ASCII 不可读路径、不存在路径四条均返回预期终态
  • 手动 GUI 验收:已由用户在真实 Mogan STEM 中完成(macOS,chmod 000 构造不可读附件):不可读文件 toast 正确显示「附件文件不可读取: noperm.pdf」且中文无乱码、附件按钮停止旋转、处理中条目移除且不生成 chip、发送栅栏解除;中文路径附件 ready/chip 与中文 toast 文案显示正常;恢复权限后重新添加同一文件流程完全正常
  • 任务文档:对应 dev-agent 任务文档 devel/6221.stem(PR [no ci] Minor improvement for the win package script #190),已同步补记上述三处修复。

@PinkMagicFly
PinkMagicFly force-pushed the pigmagicfly/6221/unreadable-attachment-error branch from bb376d7 to bc8ab13 Compare October 19, 2026 09:19
@PinkMagicFly PinkMagicFly changed the title [6221] 修复不可读附件添加后持续处理且无错误提示 [6221] 附件预检策略上移宿主并修复不可读附件持续处理问题 Oct 20, 2026
@PinkMagicFly
PinkMagicFly force-pushed the pigmagicfly/6221/unreadable-attachment-error branch from 3617409 to 1cd0fb8 Compare October 20, 2026 03:16

@MoonL79 MoonL79 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@PinkMagicFly
PinkMagicFly merged commit a463055 into main Oct 20, 2026
6 checks passed
@PinkMagicFly
PinkMagicFly deleted the pigmagicfly/6221/unreadable-attachment-error branch October 20, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants