Skip to content

fix(anolisa): tail logs --limit to recent matches - #2618

Open
zyw02 wants to merge 3 commits into
alibaba:mainfrom
zyw02:fix/anolisa/logs-limit-most-recent
Open

fix(anolisa): tail logs --limit to recent matches#2618
zyw02 wants to merge 3 commits into
alibaba:mainfrom
zyw02:fix/anolisa/logs-limit-most-recent

Conversation

@zyw02

@zyw02 zyw02 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Why

anolisa logs capped the central JSONL scan at the first N matches (default 50). Once the log grew, later operations disappeared from the default view. anolisa bug already kept the newest rows; logs should use the same tail window so operators see the failure that just happened.

What changed

  • Query cap: CentralLog::query keeps a sliding window of the most recent filter matches instead of stopping at the first N. Results stay chronological (oldest of that window first).
  • Lock window: query takes a shared flock only long enough to snapshot a stable file length, then scans that prefix without holding the lock so a default --limit tail does not block concurrent append for an O(file-size) deserialize. limited_query_releases_lock_before_scanning_history parks after unlock, completes append while the scan is still parked, then resumes; the snapshot must not include that concurrent write.
  • CLI help: --limit is documented as the most recent N (still max 1000; 0 returns none).
  • Public rustdoc: CentralLog::query matches LogFilter.limit (most recent N, file order). A doctest locks 3 records / limit=2 → last two, oldest-first.
  • Tests: query_limit_applies_after_filter expects the last two warn-or-above records; query_limit_tails_matches_inside_since_window tails inside --since; CLI combined-filter limit=1 keeps the later error, not the earlier warn.

Related issue

fixes #2617

User / Agent impact

anolisa logs and anolisa logs --limit N now show the newest matching records. Scripts that depended on seeing the oldest N hits from a large log will see the newest N instead (still oldest-first inside the window). --since behavior is unchanged except that --limit tails inside that window.

Risk and compatibility

  • Public CLI, API, configuration, or documented behavior changed
  • Privileged or security-sensitive behavior changed
  • Cross-component contract changed
  • Migration or rollback guidance is needed

--limit / LogFilter.limit now means most-recent N rather than first-N. That is the intended operator contract for an append-only audit log; library callers of CentralLog::query with a limit see the same tail semantics.

Validation

cd src/anolisa
cargo fmt --all -- --check
cargo clippy --all-targets --locked -- -D warnings
cargo test --locked -p anolisa-core --lib central_log
cargo test --locked -p anolisa-core --doc
cargo test --locked -p anolisa-cli query_combined_filter_honors_zero_and_one_limits
cargo doc --workspace --no-deps

Focused: query_limit_applies_after_filter, query_limit_tails_matches_inside_since_window, limited_query_releases_lock_before_scanning_history (50/50 locally), query_combined_filter_honors_zero_and_one_limits. Full cargo test --locked as root still hits the existing --install-mode user adapter notice suite (not caused by this diff).

Documentation and rollback

CLI --limit help and LogFilter.limit / CentralLog::query rustdoc now state most-recent N. No user-guide change (guide does not claim first-N). Revert this commit series to restore first-N scanning.

Review follow-up

Current head: b74a25d96e55e055a1823140f5e132a71b282d99.

This head keeps the tail --limit contract, the short lock window, aligned query rustdoc + doctest, and a deterministic overlap test (post-unlock hold, then append, then resume scan). No 5 ms sleep or 250 ms append SLO.

anolisa logs defaulted to the first 50 JSONL hits, so a growing
central log hid every later operation. Keep a sliding window of
the most recent matches (still oldest-first), matching anolisa bug.

Co-authored-by: Cursor <cursoragent@cursor.com>
Assisted-by: Cursor:2026.08.11-e8db854
Signed-off-by: zyw02 <zyw02@users.noreply.github.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 03ee051aa8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/anolisa/crates/anolisa-core/src/central_log.rs
Comment thread src/anolisa/crates/anolisa-core/src/central_log.rs

@kongche-jbw kongche-jbw 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.

Review baseline: 435951fc25da0ca48cab9bc78924c51179d7200a...03ee051aa85571c9a371f5cfa33576e0baf8173c

[P2] 避免在完整历史扫描期间持有日志锁

src/anolisa/crates/anolisa-core/src/central_log.rs:288 把原先的 break
改为 pop_front 后,任何带 limit 的查询(CLI 默认 50)都要反序列化整个无界
JSONL。查询全程持有共享 flock,而 append 需要独占锁;大日志上运行
anolisa logs 会让并发审计写入等待 O(file-size) 扫描结束。

Possible direction: 在锁内取得稳定文件长度后释放锁并只读该前缀,或从尾部按完整行
反向扫描;补充受控并发测试,证明长历史尾查询不会在整个扫描期间阻塞 append。

[P2] 同步更新 query 的公开契约

src/anolisa/crates/anolisa-core/src/central_log.rs:230 仍写着 limit 保留
“first N”,但同文件的 LogFilter.limit 已改为“most recent N”。库调用方会看到
相反的公开契约,并可能据此写出错误预期。

Possible direction: 更新方法 rustdoc,并用一个 3 条记录、limit=2 的 doctest 固化
返回后两条且保持文件顺序的契约。

Review flagged a stale first-N query rustdoc and a shared flock held
for the whole JSONL deserialize. Snapshot a stable length under the
lock, scan that prefix unlocked, and document most-recent N.

Co-authored-by: Cursor <cursoragent@cursor.com>
Assisted-by: Cursor:2026.08.11-e8db854
Signed-off-by: zyw02 <zyw02@users.noreply.github.com>
@zyw02

zyw02 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

@kongche-jbw both P2s on 03ee051aa85571c9a371f5cfa33576e0baf8173c are addressed on 568ce67915cc47cb3697607639b8e8629bc764ee.

  • Lock window: shared flock is held only to snapshot a stable file length; the tail scan reads that prefix unlocked. limited_query_releases_lock_before_scanning_history overlaps append with a long-history limit=1 query.
  • Public contract: CentralLog::query rustdoc now says most-recent N (file order), with a 3-record / limit=2 doctest.

Validation: cargo fmt --all -- --check, cargo clippy --all-targets --locked -- -D warnings, cargo test --locked -p anolisa-core --lib central_log, cargo test --locked -p anolisa-core --doc, cargo doc --workspace --no-deps.

Please re-review this head.

@kongche-jbw kongche-jbw 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.

Review baseline: 435951fc25da0ca48cab9bc78924c51179d7200a...568ce67915cc47cb3697607639b8e8629bc764ee

[P2] 移除锁回归测试的墙钟竞态

src/anolisa/crates/anolisa-core/src/central_log.rs:984

这里只等待 5 ms 就假定查询已完成快照并进入扫描;若查询线程尚未被调度,
append 会先写入,查询便把新记录纳入快照,最终记录断言会随机失败。
同时,250 ms 的墙钟上限会把繁忙 CI 上的调度停顿误判为锁等待。

Possible direction: 用测试钩子或屏障确认 query 已越过 unlock 并暂停扫描,
再断言 append 在释放扫描屏障前完成;移除固定 sleep 和性能阈值,保留同一路径的回归覆盖。

The overlap test slept 5ms then treated a 250ms append as lock
contention, so scheduling delay could fail CI. Park query after
unlock with a condvar so append finishes before the scan resumes.

Co-authored-by: Cursor <cursoragent@cursor.com>
Assisted-by: Cursor:2026.08.11-e8db854
Signed-off-by: zyw02 <zyw02@users.noreply.github.com>
@zyw02

zyw02 commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

@kongche-jbw the P2 on 568ce67915cc47cb3697607639b8e8629bc764ee (wall-clock race in the lock overlap test) is addressed on b74a25d96e55e055a1823140f5e132a71b282d99.

limited_query_releases_lock_before_scanning_history now parks query after the shared flock is dropped, completes append while the scan is still parked, then resumes. The snapshot must be op-snapshot-tail, not op-during-scan. The 5 ms sleep and 250 ms append SLO are gone.

Validation: cargo fmt --all -- --check, cargo clippy --all-targets --locked -- -D warnings, cargo test --locked -p anolisa-core --lib central_log, cargo test --locked -p anolisa-core --doc, cargo test --locked -p anolisa-cli query_combined_filter_honors_zero_and_one_limits, cargo doc --workspace --no-deps. The overlap test passed 50/50 locally.

Please re-review this head.

@kongche-jbw kongche-jbw 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.

Review baseline: 435951fc25da0ca48cab9bc78924c51179d7200a...b74a25d96e55e055a1823140f5e132a71b282d99

未发现阻塞问题。

Remaining risks:

  • 未运行全量 cargo test --locked;并发文件锁只在 Linux/aarch64 环境验证,未覆盖 macOS。
  • PR 当前仅显示 CLA 检查,尚无仓库 CI 结果可供交叉核验。

Verification scope:

  • 已复查完整 diff、相邻实现与测试,以及全部 conversation、review、inline thread 和 timeline。
  • cargo fmt --all -- --check
  • cargo test --locked -p anolisa-core --lib central_log
  • cargo test --locked -p anolisa-core --doc
  • cargo test --locked -p anolisa-cli query_combined_filter_honors_zero_and_one_limits
  • cargo clippy --all-targets --locked -- -D warnings
  • cargo doc --workspace --no-deps
  • limited_query_releases_lock_before_scanning_history 重复运行 50/50 通过。

@ikunkun-sys ikunkun-sys 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.

Review baseline: d80cd127c0f64133ad9d8277e51792d3fa36b2b9...b74a25d96e55e055a1823140f5e132a71b282d99 (synthetic merge 24c5419d6a8ccf9eca8592c45e0dd18ed641e96d)

[P2] 按仓库规则整理 fix 归因与提交历史

AGENTS.md:382-387

03ee051a 修复了 main 上 7a6e0d72 引入的 first-N 行为,但其 trailers 中没有必需的 Fixes:。随后 568ce679 修复 03ee051a 引入的锁窗口/文档问题,b74a25d9 又修复 568ce679 引入的测试竞态;这两个 review follow-up 仍是独立的 fix commits。

这与仓库规则冲突:main 上已有缺陷需要正确的 Fixes: 归因,同一 PR 内引入的问题必须 fixup/autosquash,不能保留独立 fix commit。当前最终代码行为验证通过,但合并前仍需补齐归因并整理为原子提交历史。

Verification: Rust 1.88.0、Linux x86_64 的 synthetic merge 上,workspace fmt、Clippy、docs、cargo test --workspace --locked 均通过;锁重叠测试重复 50/50 通过。

@ikunkun-sys ikunkun-sys 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.

Review baseline: b74a25d96e55e055a1823140f5e132a71b282d99 (unchanged since review #4956800515)

Requesting changes for the verified blocking item from that review:

[P2] 按仓库规则整理 fix 归因与提交历史

03ee051a 修复 main 上 7a6e0d72 引入的缺陷,但没有 Fixes:568ce679b74a25d9 又作为独立 fix commits 修复本 PR 前序提交引入的问题。该历史仍违反 AGENTS.md:382-387 的强制归因与 autosquash 规则。

当前代码验证和远端 CI 均已通过;本次 request changes 只阻塞上述提交历史问题。

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

anolisa logs --limit returns oldest records, not most recent

3 participants