Skip to content

fix(feishu): preserve text and clean up failed inbound file writes - #13337

Merged
wenshao merged 5 commits into
QwenLM:mainfrom
jabrailkhalil:fix/feishu-inbound-write-fallback
Oct 8, 2026
Merged

wenshao merged 5 commits into
QwenLM:mainfrom
jabrailkhalil:fix/feishu-inbound-write-fallback

Conversation

@jabrailkhalil

@jabrailkhalil jabrailkhalil commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Keeps the Feishu message turn running when a downloaded document cannot be saved locally. A local storage failure skips the attachment, logs a sanitized diagnostic and removes partial storage when possible. If no attachment remains, the synthetic file placeholder becomes (User sent media but download failed), preserving any quoted context.

If immediate removal also fails, the directory remains owned by the existing 60-second cleanup timer. Stopping a message clears that ownership only after successful removal. Successful files retain their bytes, metadata and cleanup period; new files and temporary leaf directories use owner-only POSIX permissions (0600 and 0700).

This is the separate production fix for #13334, based directly on main. It does not include the coverage-only changes in #13278.

Why it's needed

A local storage error previously stopped message dispatch and could leave partial files behind. The fallback preserves the message and explains the unavailable attachment. The second review also exposed a remaining leak when both storage and immediate cleanup fail; retaining directory ownership gives transient cleanup failures the existing delayed attempt.

Reviewer Test Plan

How to verify

  • Inject directory-creation or partial-write errors: the turn continues exactly once without an attachment, partial storage is removed when possible, and the diagnostic retains the original storage error.
  • Also make immediate cleanup fail: the directory remains at 59,999 ms and is removed by the existing timer at 60,000 ms when removal succeeds. Repeat with Stop during preparation: no dispatch, with delayed cleanup retained after failed removal.
  • Send a file as a reply and verify the quoted text survives while only the final synthetic placeholder changes. Plain text bypasses the file store.
  • Check traversal, backslashes, leading dots, empty basenames, whitespace/newline and NUL filenames: the stored file stays directly inside its message directory and retains its exact bytes.
  • Check successful binary contents, MIME/name and lifetime. On Linux, verify 0600 files and 0700 leaf directories under umask 022 and 000.
  • Inject a diagnostic with LF, ESC, CR and astral Unicode characters: controls are sanitized and truncation preserves code points.

Evidence (Before & After)

Validation for 187a2da5265a:

  • The expanded 18-case storage fixture against the previously published adapter (db3a0c82) fails three cleanup regressions. Linux: 15 pass / 3 fail; Windows: 13 pass / 3 fail / 2 POSIX skips. With this fix: Linux 18/18; Windows 16 pass / 2 skips.
  • Full Feishu suite: Linux 311/311; Windows 309 pass / 2 POSIX skips.
  • Eight isolated mutations, covering filename guards, both cleanup-ownership sites and diagnostic sanitization, are detected on both platforms.
  • A separate offline integration probe reproduces five cleanup failures with six passing controls on the exact published adapter. The final compiled adapter passes all 11 scenarios. It uses real files, controlled filesystem failures/timers, and one real ChannelBase/SessionRouter dispatch with a stub bridge; no TypeScript loader is used for the final adapter.
  • Repository build, bundle and typecheck pass. Strict checking of the test file, targeted lint/format checks and full-diff reviews pass. Bundled CLI version/help/channel-help/start-help checks pass; these are startup smoke checks.

Tested on

OS Status
Windows Node 22.21.1; full package suite, repository checks, strict test typing and compiled-adapter probe passed
Linux Node 22.23.3; full package suite, real POSIX permissions, package build, baseline and mutation checks passed
macOS Not rerun locally for this commit

Environment (optional)

Offline tests use controlled HTTP responses and real isolated temporary files. The integration probe uses a stub model bridge and controlled timers. No live Feishu account, daemon, ACP or external model run is claimed. Linux checks used an isolated minimal dependency installation at the relevant runtime/test versions, rather than the full frozen repository dependency tree. Fresh upstream CI is still required on this commit.

Risk & Scope

Cleanup retains the existing one-shot best-effort retry; persistent removal failure is not guaranteed to recover. The fallback marker covers local document-storage errors after download; the existing HTTP/download-null behavior remains outside this fix. No dependency, lockfile or other-adapter changes are included.

Linked Issues

Fixes #13334. Refs #13278 and #13220.

中文说明

下载的文档无法在本地保存时,消息仍继续处理:跳过附件、保留引用内容和合成文本标记,并记录经过清理的原始错误。新文件与临时叶子目录使用 POSIX 0600 / 0700 权限。

本轮修复立即清理失败后的目录所有权:保留既有 60 秒清理定时器;Stop 分支仅在删除成功后清除所有权。没有新增清理器或无限重试。HTTP 下载失败路径、文件名长度策略和其他适配器不在范围内。

187a2da5265a 的验证:扩展 18 项存储回归在此前发布的 db3a0c82 适配器上出现 3 项清理失败;修复后 Linux 18/18,Windows 16 通过、2 项 POSIX 测试跳过。完整飞书套件 Linux 311/311,Windows 309 通过、2 项跳过;两平台均检出 8/8 个隔离变异。

另一个离线集成探针在原适配器上复现 5 项清理失败、6 项对照通过;最终编译后的适配器 11/11 通过。测试使用真实临时文件、受控故障与时间,其中一项经过真实 ChannelBase/SessionRouter 和 stub bridge。全仓 build、bundle、typecheck,测试文件严格类型检查及针对性 lint/格式检查通过。CLI help 等仅为启动检查。

未运行真实飞书账号、daemon、ACP 或外部模型。Linux 使用相关版本的隔离最小依赖安装,并非完整冻结的仓库依赖树;本地未重新测试本提交的 macOS。仍需此提交的新上游 CI 和审查。

@github-actions github-actions Bot added the review/self-reported The linked issue was opened by the PR author (self-reported) label Oct 3, 2026
@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

Verification report for 8b815ed18bec42f940891f3d626eada00ae5926e, based on main 2c591ecc08a6fa080342f9b1b9f7f43215178cbb:

  • Unchanged-source baseline: the successful binary/cleanup control passes; the three injected partial-storage faults (mkdir EACCES, writes ENAMETOOLONG and ENOSPC) each log the fault, leave an orphan UUID directory and dispatch no text. Result: 1 passed / 3 failed.
  • Same fixture after the fix: 4/4 pass. Partial storage is empty immediately and exactly one unchanged synthetic file-text envelope arrives without attachments. Successful exact bytes, MIME/name and the 59,999→60,000 ms cleanup boundary are preserved.
  • Full package-local Feishu suite: 297/297 across six files; independent shuffled seed 13334 also 297/297. Root build and typecheck, separate strict no-emit checking of the new test file, targeted ESLint/Prettier and diff checks pass. Two final independent full-diff audits are clean.

This is an offline test-script fallback, not live Feishu E2E: resource HTTP and the final dispatch boundary are mocked, while inbound preparation and filesystem writes/removal are real and isolated. Stop/finally paths and best-effort cleanup-denial behavior were source-reviewed, not claimed as new live/device executions. No platform credentials or models were used. Fresh upstream CI/review remains necessary.

中文:8b815ed18b 基于 main 2c591ecc08。未修改生产基线为 1 通过/3 失败:mkdir EACCES、write ENAMETOOLONG/ENOSPC 的部分存储各记录故障、遗留 UUID 目录且不分发文本;修复后同一四例全通过,立即清空部分存储并恰好一次分发不变 synthetic 文件文本、无附件,成功字节/MIME/文件名及 59,999→60,000 ms 清理边界保持。完整飞书套件及 seed 13334 随机顺序运行均为六文件 297/297,全仓 build/typecheck、新测试单独严格 no-emit、ESLint/Prettier/diff 检查通过,最终两轮独立完整 diff 审查无发现。这是离线脚本替代,网络与最终分发边界为模拟,入站准备和隔离文件系统操作真实执行,不声称真实飞书 E2E。Stop/finally/清理拒绝路径仅源码审查;未用平台凭据或模型,仍需新的上游 CI/审查。

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

Thanks for the commit-pinned review. I will keep the isolated storage-test file: its module-level filesystem mocks should not enter the existing adapter test module, and a shared three-adapter refactor is outside this fix.

The before/after report uses the same four-case fixture against main 2c591ecc08a6fa080342f9b1b9f7f43215178cbb and head 8b815ed18bec42f940891f3d626eada00ae5926e, with the successful binary control passing in both and the three fault cases failing both cleanup and dispatch on the base. If a maintainer wants independently executed upstream A/B evidence, could they sponsor the suggested @qwen-code /verify for this head? I cannot authorize that lane myself and will not treat pending CI or a static no-findings review as merge approval.

中文:感谢针对精确提交的审查。保留隔离的存储测试文件,避免模块级 fs mock 进入已有 adapter 测试模块;跨三个适配器的共享重构不在本次范围。前后报告使用同一四例 fixture,分别对 main 2c591ecc08 和 head 8b815ed18b:成功二进制对照两者通过,三个故障用例在 base 同时因清理和分发失败。若维护者希望取得独立执行的上游 A/B 证据,请为该 head 赞助所建议的 @qwen-code /verify;我无法自行授权,也不将 pending CI 或静态无发现审查视为合并批准。

@jabrailkhalil
jabrailkhalil dismissed a stale review via db3a0c8 October 4, 2026 00:17
@wenshao

wenshao commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

Local maintainer verification — PR #13337

Verdict: ✅ merge-ready — 383 scripted assertions executed, 383 passed, 0 unexpected failures.

Verified head: db3a0c82dd4f4427ba0d917bc2175ee8114863fe (2 commits) against base 2c591ecc08a6fa080342f9b1b9f7f43215178cbb (merge-base == baseRefOid, so the A/B isolates exactly this PR).

中文摘要

结论:可合并。 在 macOS(Darwin arm64, Node 22.23.1)上以 head/base 双工作区完成 A/B 验证:

  • 核心主张成立(A/B 证明):用 bug(feishu): failed inbound file writes orphan temporary directories and drop text fallback #13334 的真实复现形状(300 字符文件名 → ENAMETOOLONG)驱动编译产物。base 侧回合被杀死(dispatch=0)、残留部分文件、stderr 泄露未截断的 300+ 字符完整路径;head 侧回合继续(dispatch=1)、占位文本 (User sent media but download failed)、部分文件被清理、诊断经 sanitizeLogText 截断。mkdir 失败(ENOTDIR,真实故障注入)同型。
  • 新增测试负载证明:PR 的新测试文件在 base 源码上 7 红 2 绿(与作者声明一致),在 head 上 9/9 绿——测试非空转。
  • 变异矩阵 4/4 全部击杀:catch 再抛(5 红)、去清理(4 红)、去占位替换(5 红)、去权限位(恰好 2 条 umask 测试红),零幸存者;未变异对照 9/9 绿。
  • 门禁:feishu 全量套件 head 302/302 绿(含 macOS 上实际执行的 POSIX 权限用例);tsc --noEmit 干净;eslint 干净且经植入违规证明门禁活跃(no-var/no-console 触发 2 error)。
  • 权限声明实测:成功保存的文件 0600、叶子目录 0700(macOS POSIX 实测,非仅 Linux)。
  • 未发现阻塞问题。 未覆盖项见下文 "Not covered"(Windows 实机、ENOSPC 真实故障、真实飞书账号等)。

Central claim and A/B proof

Claim: when local storage of a downloaded Feishu document fails, the turn no longer dies — partial storage is best-effort removed, a sanitized diagnostic is logged, and the synthetic file placeholder becomes (User sent media but download failed) with any quoted context preserved. Secondary claims: cleanup failure must not mask the original error or block dispatch; successful downloads keep bytes/metadata/60s cleanup and now use 0600/0700 POSIX permissions.

The A/B drove the built dist of both trees through a mock-free wire harness (real loopback HTTP server standing in for the Feishu Open API, real filesystem faults, TMPDIR isolated per run — no module mocks of the unit under test; internal @qwen-code/* symlinks verified by readlink -f to resolve inside each tree):

scenario (wire harness) base 2c591ecc head db3a0c82
success (report.pdf) 7/9 — perms 0644/0755 (no mode flags) 9/9 — bytes identical, MIME/name preserved, file 0600, leaf dir 0700
mkdir-fail (channel-files pre-created as a regular file → ENOTDIR) dispatch=0, turn killed 6/6 — dispatch=1, placeholder text, no attachments, sanitized diagnostic
write-fail (300-char filename → ENAMETOOLONG, the real #13334 shape) dispatch=0, uuid dir + partial file left behind, full 300+ char path dumped to stderr 6/6 — dispatch=1, placeholder text, uuid dir removed, diagnostic truncated to 300 chars
quote-write-fail (reply to quoted message + ENAMETOOLONG) dispatch=0 (quote fetched, then turn died) 3/3 — [引用内容 …] quote prefix preserved verbatim, only the trailing placeholder replaced
user-text (plain text message, control) 4/4 — unchanged 4/4 — unchanged, no file write attempted

All 13 base-side reds were the expected bug signature (encoded as expected-failure cells); all 15 base-side greens confirm untouched behavior. See 01-ab-write-fail-base-vs-head.png.

New-test vacuity / load-bearing proof

The PR's new inbound-file-store.test.ts was run against both trees' sources:

  • base source: 7 failed / 2 passed — the three fault-injection tests, both umask permission tests, the cleanup-failure test and the quoted-text test all red; success-lifecycle and user-text tests green. Matches the author's "7 failed / 2 passed before" claim exactly.
  • head source: 9/9 passed.

Mutation matrix (each guard reverted alone at head, new suite run per mutant)

mutant reverted guard suite result verdict
M1 catch rethrows (errors propagate as at base) 5 failed / 4 passed killed — fault + cleanup + quote tests
M2 no best-effort rmSync cleanup 4 failed / 5 passed killed — leftover + cleanup-call assertions
M3 no failure-placeholder replacement 5 failed / 4 passed killed — placeholder + quote assertions
M4 no 0600/0700 mode flags 2 failed / 7 passed killed — exactly the two umask tests
control unmutated head 9/9 passed suite live

Zero survivors; every mutant was killed by the assertions that exist to pin it (verified by failed-test names, not just counts). See 02-mutation-matrix.png.

Gates

  • Full feishu package suite at head: 302/302 passed (6 files) on macOS — includes the two POSIX permission tests that are skipped on Windows.
  • tsc --noEmit -p packages/channels/feishu: clean.
  • eslint on both changed source files: clean; gate proven live by a planted var+console violation (2 errors, exit 1), then removed.

Findings

None blocking. Observations, for the record:

  1. The placeholder-replacement guard (syntheticText && attachments.length === 0) cannot mis-fire on real Feishu traffic: extractContent switches on message_type, so a file message never also carries an image attachment. Confirmed by reading the switch and by the wire harness.
  2. The parent channel-files directory keeps default (umask-derived) permissions; only the per-download uuid leaf is 0700. File contents and filenames are protected at the leaf, so the docs claim holds as written.
  3. sanitizeLogText is a pre-existing channel-base export (present at base) — no hidden cross-PR dependency.

Not covered

  • Windows: permission tests are POSIX-only (it.skipIf(win32)); this round ran on macOS, complementing the author's Linux+Windows runs.
  • ENOSPC as a real filesystem fault (not injectable without mocks here); covered by the mock-level unit test plus two real-fault write/mkdir scenarios (ENAMETOOLONG, ENOTDIR) that exercise the same catch path.
  • rmSync-failure (EBUSY) scenario covered only at mock level (unit test); a real rmSync failure was not forced.
  • Live Feishu account / real Open API traffic; the 60-second cleanup verified with fake timers (unit test), not a wall-clock wait.
  • Repo-wide suite beyond the feishu package (out of scope — the diff touches only feishu + docs; lockfile and other packages untouched, so the shared node_modules A/B control is clean).

Methodology

macOS arm64, Node v22.23.1. Two git worktrees at the verified head/base OIDs; head installed with corepack pnpm install --frozen-lockfile + full build; base received a cp -al hardlink of head's node_modules (PR leaves the dependency tree untouched) followed by a scoped build of packages/channels/base and packages/channels/feishu; readlink -f confirmed @qwen-code/channel-base resolves inside each respective tree. The wire harness (wire-harness.mjs) instantiates the built FeishuChannel from each tree's dist/, redirects only the open.feishu.cn host to a real loopback HTTP server that records requests, and injects filesystem faults through the real fs (a regular file at channel-files, a 300-character filename). Mutations applied by scripted single-hunk edits, each run followed by git checkout restore (tree verified clean afterwards). Raw logs: logs/head-*.log, logs/base-*.log, logs/mut-*.log in the artifact directory.

Evidence captures

A/B: real #13334 repro (300-char filename) — base kills the turn, head degrades gracefully

Mutation matrix on the new suite: 4/4 guards load-bearing, zero survivors


Local maintainer verification by @wenshao — harness, mutation scripts and raw logs preserved under tmp/pr13337-verify-20261004-211359/.

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough verification of db3a0c82, @wenshao. I read the results and addressed all five second-round review points in 187a2da5265a.

The remaining production fix is small: if immediate directory removal fails, I retain ownership for the existing 60-second cleanup attempt; Stop clears ownership only after successful removal. I reproduced the residual leak before changing it. The final 18-case fixture has three cleanup failures against the previous adapter and passes with the fix. Full package results are 311/311 on Linux and 309 passed plus two POSIX skips on Windows; all eight isolated mutations are detected on both platforms. A separate probe against compiled JavaScript goes from five cleanup failures/six passing controls to 11/11, including real channel/session dispatch with an offline bridge stub. Build, bundle, typecheck and targeted lint/format checks pass.

I also narrowed the docs, added hostile-filename and bounded-diagnostic regressions, and corrected the plain-text test name. The PR description records the environment and limits: no live Feishu run, controlled timers, and an isolated Linux dependency installation. Your verification applies to the previous SHA; this new commit still needs its own upstream CI and review. I am available for any remaining corrections and follow-up maintenance.

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

I checked the remaining Ubuntu failure at 187a2da5: the recall cold scan reports 279.1–328.8 ms against 275 ms. The core tree and lockfile are identical to the upstream branch point; the Feishu tests run afterwards.

Three alternating base/head comparisons on local Linux Docker reproduced cold-limit failures on both versions, while warm-cache and all five lane controls passed. This was an isolated benchmark dependency install, not a reproduction of the hosted runner. The upstream baseline job passed on ECS with a 1,250 ms limit, so its green result does not settle the 275 ms hosted limit. I have not changed or relaxed the benchmark.

@wenshao Could you triage or rerun the Ubuntu job and review 187a2da5, which includes the cleanup-retry correction after your previous verification? I cannot authorize the upstream rerun myself.

@qwen-code-review-bot qwen-code-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

⚠️ Downgraded from Approve to Comment: CI failing: Test (ubuntu-latest, Node 22.x). Reviewed.

中文说明

⚠️ 已从批准降级为评论:CI failing: Test (ubuntu-latest, Node 22.x)。 已审查。

— qwen3.8-max via Qwen Code /review (v0.25.0)

@qwen-code-review-bot qwen-code-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No blocking issues. LGTM! ✅

Deferred under the convergence posture (round 2, not a blocker) — recorded, not requested in this round:

  • packages/channels/feishu/src/inbound-file-store.test.ts:139 — [review] No test binds the new store-failure diagnostic to the failure path
  • packages/channels/feishu/src/FeishuAdapter.ts:2784 — [review] New diagnostic claims a delivery the Stop check below can still cancel
中文说明

无阻断问题。LGTM!✅

收敛姿态下延后(第 2 轮,非阻断)——已记录,本轮不要求修改:共 2 条(原文未翻译,列表见上方英文部分)。

— qwen3.8-max via Qwen Code /review (v0.25.0)

@wenshao
wenshao added this pull request to the merge queue Oct 8, 2026
Merged via the queue into QwenLM:main with commit 4edc26a Oct 8, 2026
53 checks passed
@qwen-code-review-bot

Copy link
Copy Markdown
Collaborator

Released in v0.25.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review/self-reported The linked issue was opened by the PR author (self-reported)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(feishu): failed inbound file writes orphan temporary directories and drop text fallback

3 participants