Skip to content

fix(grep): paginate fallback directory traversal - #5270

Merged
qin-ctx merged 2 commits into
volcengine:mainfrom
cocolord:codex/fix-grep-pagination
Sep 22, 2026
Merged

qin-ctx merged 2 commits into
volcengine:mainfrom
cocolord:codex/fix-grep-pagination

Conversation

@cocolord

Copy link
Copy Markdown
Collaborator

Problem

Python fallback grep only consumed the first ls() page in each directory. A directory with more than 1,000 visible entries could therefore return a successful result while silently missing later files.

Change

  • page through every visible directory with offset while preserving the existing DFS order
  • keep ls() as the traversal boundary so ACL, hidden-file, legacy-session merge, URI mapping, exclude, and level behavior stay intact
  • propagate root and later-page listing failures instead of returning partial results as complete
  • cover 999, 1,000, 1,001, and 10,000 entries, cross-page DFS order, later-page failure, and a unique match in file 1,001

Validation

  • 29 passed, 2 deselected in tests/storage/test_viking_fs_grep.py (the two deselected ACL tests also fail unchanged on the base branch because their is_enabled stub is synchronous)
  • Ruff lint passed
  • git diff --check passed

Fixes #5263

Co-authored-by: TRAE CLI <traecli@bytedance.com>

@cocolord cocolord left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Approval conclusion (author-owned PR; GitHub blocks formal self-approval)

动机

独立复验确认:fallback grep 的文件发现只消费每个目录第一次 ls() 返回的页面。真实存储在 1,000 项处分页,因此宽目录会成功返回但静默漏掉第 1,001 项及以后内容。这个问题不能靠索引或 archive 格式绕过,因为所有被迫走 fallback 的调用都受影响。最小正确修复点就是现有 _collect_grep_files,让它完整消费 ls(node_limit, offset)。

改动思路

保持现有 grep -> _grep_fs -> _grep_encrypted -> _collect_grep_files -> _grep_in_files 架构不变,只在唯一的 fallback 文件发现 owner 中增加分页。每页仍按原顺序做 DFS,exclude_uri、allowed_uris、level_limit 和下游 node_limit 语义不变。stat 或后续页面失败继续由存储/request 边界负责,不能伪装成空结果。

具体改动

关键代码讲解

  • _collect_grep_files:固定每页 1,000 项,以实际消费数量推进 offset,短页结束;恰好 1,000/10,000 项会多请求一个空页后正确终止,不会死循环。
  • _grep_encrypted:未增加新分支,只是获得完整 URI 清单后继续使用既有逐文件 matcher,因此 URI、文件内行号和 regex 边界没有被重写。
  • 测试覆盖 999/1,000/1,001/10,000 项、跨页 DFS 顺序、第二页失败、root stat 失败,以及唯一命中位于第 1,001 文件的端到端 fallback 路径。

对主干的风险

主要风险是宽目录会产生正确性所必需的额外 ls/read I/O;本 PR 不把它宣传为性能优化。最强反例是“目录项恰好为页大小整数倍导致循环或乱序”,现有边界和 DFS 测试已覆盖。真实 1,001 文件 probe(fixture SHA256 943bb838b2abe46c1ff3228926bdc4b17f8a245d31faa6482217e4aeeeaca81b)上,base 只扫描 1,000 文件并漏掉结果;head 扫描 1,001 文件,返回 file_1000.txt 第 1 行 needle。

我的整体评价

未发现剩余 P0-P2 问题。改动复用了现有分页接口和唯一 traversal owner,没有新增 catalog、归档状态或重复规则,生产路径足够小。focused 测试 9 passed,Ruff、diff check 和远端 9 个检查均通过。结论:对 exact head 7000fdd28d5527726924645696b9892e8a1b587e 为 APPROVE。


Independent review conclusion: no remaining P0-P2 finding. The patch fixes a deterministic false-negative at the existing fallback traversal owner, preserves DFS/filter/result semantics, and correctly treats extra I/O as the cost of completeness rather than a speedup. Exact head reviewed: 7000fdd28d5527726924645696b9892e8a1b587e.

@qin-ctx qin-ctx 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.

分页修复能解决宽目录漏搜,但删除异常捕获后,正常的 ACL 排除会中断整个搜索。已通过 base 与 Head 的 HTTP A/B 验证确认回归,需要在合并前解决。具体问题见行级评论。

Comment thread openviking/storage/viking_fs/_grep.py Resolved
Co-authored-by: TRAE CLI <traecli@bytedance.com>
@cocolord
cocolord requested a review from qin-ctx September 22, 2026 08:27
@qin-ctx
qin-ctx merged commit 611f5c4 into volcengine:main Sep 22, 2026
9 of 10 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog to Done in OpenViking project Sep 22, 2026
MaojiaSheng pushed a commit to MaojiaSheng/OpenViking that referenced this pull request Sep 24, 2026
* fix(grep): paginate fallback directory traversal

Co-authored-by: TRAE CLI <traecli@bytedance.com>

* fix(grep): skip ACL-denied fallback subtrees

Co-authored-by: TRAE CLI <traecli@bytedance.com>

---------

Co-authored-by: TRAE CLI <traecli@bytedance.com>
frankyang2008-eng added a commit to frankyang2008-eng/OpenViking that referenced this pull request Oct 9, 2026
…face + skills over MCP, grep context lines & native scanner, OpenSandbox lifecycle, openGauss DataVec adapter, docs homepage redesign)

Upstream: 31f3742 -> 7b07c4a (25 commits, 316 files, +26052/-3226), post-v0.4.21.

Themes
- mcp/skills: mirror the server's MCP tool surface for pi (volcengine#5272) - the pi
  extension now registers tools from the server catalogue instead of a
  hand-written list, so the prompt can no longer name a tool the server lacks;
  add_skill over MCP and skills findable over MCP (volcengine#5160); skill catalog plus
  openviking-skills for the memory plugins (volcengine#5161); session-derived skills
  updated through the skill installer (volcengine#5249); one entry per Skill package from
  context assembly (volcengine#5255)
- grep: match-line context support (volcengine#5168), native scanner for canonical
  sessions (volcengine#5271), paginated fallback directory traversal (volcengine#5270)
- bot: Docker-backed OpenSandbox lifecycle and persistent workspaces (volcengine#5269)
  with a readiness handshake, re-validate forwarded openviking_connection
  identity (volcengine#4866)
- storage: reserved-name rules aligned (volcengine#5170), pathlock log level (volcengine#5282),
  drop the auto-lock when an account deletion removes files under it (volcengine#5303)
- vectordb: openGauss DataVec adapter (volcengine#4045)
- web-studio: account user group management (volcengine#5284), case-insensitive account
  query (volcengine#5306), account switcher constrained to sidebar width (volcengine#5315)
- docs: homepage redesign and unified navigation (volcengine#5300, volcengine#5304)
- hermes: commit active memory sessions by token threshold (volcengine#5278), save
  provider settings in the requested profile (volcengine#5262)
- benchmark: AML adapter and public evaluation workflows (volcengine#5276)
- parse: feishu mindnote (volcengine#4319); metrics: executor and task metric export (volcengine#5105)

Conflicts (6) and resolutions
- openviking/server/bootstrap.py - one hunk, complementary intents: took
  upstream's graceful-shutdown timeout raise (5s -> 30s, needed now that the
  child manages a Docker-backed sandbox) and kept the fork's flush=True on the
  shutdown prints, whose purpose is documented in the comment above them.
- tests/unit/test_server_bootstrap_bot_gateway.py - union. The fork's
  signal-handler regression test and upstream's three readiness tests cover
  different behaviour; the import block is the union of both sides.
- examples/pi-coding-agent-extension/index.ts (3 hunks) - took upstream
  throughout. Upstream replaces the fork's base-version hardcoded tool list with
  a list generated from what actually registered, and adds the toolsReady
  status segment; the fork's other hunks on this file were Prettier re-wrapping.
  The fork's getBranch fallback for omp lives in a non-conflicting region and is
  preserved.
- examples/pi-coding-agent-extension/tools.ts - took upstream's file. The fork's
  two changes (the enqueue import and the viking_remember pending-queue
  fallback) both target the hand-written registerTools, which upstream deleted
  outright in favour of dynamic MCP registration; registerTools now has no
  importer. pending-queue.mjs stays live via sync.ts, batch-send.mjs and tests.
  Noted for the follow-up below: the bridge surfaces failures by throwing and
  documents that it never replays a call, so the fork's "retry on reconnect"
  guarantee for viking_remember no longer exists on this path.
- crates/ragfs/src/plugins/localfs/mod.rs (2 hunks) - took upstream. Upstream
  extracted grep_file_via_searcher plus LocalGrepSink (which adds
  before/after-context support) and moved the exclude/level_limit checks to an
  early return. Taking upstream was required to compile, not merely preferred:
  the function signature dropped its level_limit parameter in favour of
  options.level_limit and the UTF8 import was removed, while the fork's side
  still referenced both.
- docs/.vitepress/config.ts - upstream deleted 677 lines of sidebar definitions
  into the new docs/.vitepress/docs-navigation.ts. Took upstream's deletion and
  migrated the fork's only real change (two lines adding
  19-jina-rerank-local.md) into the new file, en and zh.

Fork delta carried through
- openviking/server/bootstrap.py: _install_bot_shutdown_handler, so uvicorn's
  re-raise of the shutdown signal runs the child cleanup instead of terminating
  the process without unwinding the stack (3a7427a), plus flush=True on the
  shutdown prints (88fff4a).
- crates/ragfs/src/plugins/localfs/mod.rs: three clippy fixes upstream does not
  have (open_file_matches_path(file, ...) without the needless borrow, twice;
  for dent in walker instead of while let Some(dent) = walker.next()), all in
  non-conflicting regions and preserved.
- docs: 19-jina-rerank-local.md sidebar entries.

Post-merge verification
- cargo check -p ragfs: exit 0, 84 warnings (pre-existing doc warnings)
- python -m py_compile on bootstrap.py and the bootstrap gateway test: OK
- no conflict markers anywhere in the tree; no unmerged paths
- git diff --cached main --shortstat = 312 files, +29298/-4573 (fork delta only)

Review finding (not fixed here, needs a decision)
- The fork's shutdown handler is installed after _start_vikingbot_gateway
  returns. Upstream's new _wait_for_bot_ready blocks inside that call for up to
  900s while the sandbox starts, so the unprotected window grew from
  milliseconds to 15 minutes. Ctrl-C is still covered by upstream's new
  `except BaseException`, which terminates the child; SIGTERM is not, so a
  `systemctl stop` or `kill` during startup can still orphan the bot child
  holding its port - the exact failure 3a7427a fixed. Installing the handler
  right after Popen would restore the original coverage.

Not touched
- Upstream-owned lint debt left byte-identical to main rather than fixed inside
  this merge (fixing upstream files here re-conflicts every sync):
  .github/workflows/pr.yml (15 yamllint findings - the repo has no .yamllint
  config and ruff explicitly ignores E501, so the 80-column rule is the tool
  default, not this project's convention) and benchmark/aml/eval/evaluate.py
  (3 findings, including the compile() call that is the AML benchmark's
  design). Both verified: the fork has never modified either file
  (git diff --stat 31f3742..ov-dev-opt -- <file> is empty) and both are
  byte-identical to main. Recorded in plans/upstream-type-debt-20260917.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug]: Python fallback grep silently skips directory entries after the first 1,000

2 participants