Skip to content

fix(cli): mofa agent logs --limit returns most-recent N lines (tail semantics)#1649

Open
SH20RAJ wants to merge 1 commit into
mofa-org:mainfrom
SH20RAJ:fix/1298-logs-limit-most-recent
Open

fix(cli): mofa agent logs --limit returns most-recent N lines (tail semantics)#1649
SH20RAJ wants to merge 1 commit into
mofa-org:mainfrom
SH20RAJ:fix/1298-logs-limit-most-recent

Conversation

@SH20RAJ
Copy link
Copy Markdown
Contributor

@SH20RAJ SH20RAJ commented Apr 20, 2026

Summary

Fixes #1298.

mofa agent logs <id> --limit N was returning the first N matching
lines instead of the most-recent N lines, making it behave like
head -n N instead of the expected tail -n N.

Root Cause

display_log_file used an early-break pattern:

// OLD (broken)
if let Some(max) = limit && count >= max {
    break;   // stops reading after N lines from the TOP
}

This exits the read loop after max matches, so only the oldest lines
were ever returned.

Fix

Remove the early break. Collect all filtered lines into a Vec,
then compute the tail with:

// NEW
let start = output_lines.len().saturating_sub(max);
let display_lines = &output_lines[start..];

This is a simple O(file_size) solution: the Vec is already being built
anyway, so there is no extra pass over the data.

Tests

Added test_logs_limit_returns_most_recent_lines which writes five
numbered lines and asserts that --limit 2 yields ["line-4", "line-5"]
and not ["line-1", "line-2"].

cargo test -p mofa-cli commands::agent::logs::tests

Checklist

Fixes mofa-org#1298

The old display_log_file broke from the while loop as soon as count >=
max, returning the earliest N matching lines. Users expect tail semantics.

Remove the early break. Collect all filtered lines, then slice the last
max entries with saturating_sub. Added test_logs_limit_returns_most_recent_lines.
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.

mofa agent logs --limit returns earliest lines instead of most recent lines

1 participant