Skip to content

rust2(tail): GNU follow semantics -f/-F/--retry/--pid (#1488) - #1626

Merged
caomengxuan666 merged 5 commits into
mainfrom
rust2/tail-follow
Nov 14, 2026
Merged

caomengxuan666 merged 5 commits into
mainfrom
rust2/tail-follow

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

rust2 tail gains full GNU coreutils 9.4 follow semantics (it previously answered -f/-F/--retry with "cannot follow end of file: not supported", rc 1), so wiring tail later is no regression vs the 1.x C++ applet and matches GNU. Rust implementation + tests only — no bridge/ABI/CMake/src/commands changes, no new crates, no new C++ user-visible text.

  • -f/--follow[=descriptor|name], -F, --retry, -s (fractional/hex/inf), --pid, --max-unchanged-stats, the GNU warnings, and the GNU exit statuses (the port picks the same inotify-or-polling path GNU would, because the two give different exit codes)
  • appends, file truncated, multi-file headers only when output switches files, name-state diagnostics in GNU's exact wording (has become inaccessible, has appeared; following new file, has been replaced; following new file, ...), pipe stdin ignored under -f, regular-file stdin followed
  • event-driven wake-up (FindFirstChangeNotificationW + the --pid process handle) plus a 100 ms re-stat tick; idle CPU measured 0
  • name mode holds no handle between wakes, so rename-over log rotation works; stdout is flushed per chunk, and a broken pipe returns Err(CmdError::Io)
  • side fix: regular files are dumped by seek + backward scan, which also fixes a subtract-overflow panic in tail -n K (printf 'a\nb' | tail -n 1)

Evidence

  • bash gate/run-gate.sh tail: 72/72 strict (27 existing rows plus 45 new terminating follow rows). stderr is also byte-equal to GNU on every tail row.
  • bash gate/wire/tail-follow.sh: 27/27 live scenarios PASS vs WSL GNU 9.4 (gate/wire/tail-follow.out); also 27/27 at STEP=0.3
  • cargo test --workspace: all green (26 tail tests: header switching, truncation, name state machine, inotify/exit-status choice, parsers, seek dump, live append/truncate/pid)

Commits

  1. rust2(tail): implementation
  2. gate(tail): rows (gen-cases.py append-only + regenerated tail.tsv)
  3. gate(tail): live harness + recorded results
  4. docs(tail): README section + COVERAGE row

Notes for review

  • Handle red line: for -f on - the library only queries the process stdin handle (type, console mode, final path, current position — the same tier tty.rs uses) and opens the file by path with its own handle. It never reads, duplicates or moves the host's handle.
  • Stdin follow depends on the host feeding the real process stdin; whoever wires tail later should check that.
  • Documented gaps (none show up in the harness): no symlink-replacement diagnostic, no remote-file detection, a console stdin among several inputs or with --pid isn't read after EOF, and a vanished stdout reader is noticed at the next write.

caomengxuan666 added a commit that referenced this pull request Nov 14, 2026
…historical snapshot (#1629)

The checkpoint section records the acceptance state as audited by the
lead review: current main had 50 commands wired (not the historical
76/42 numbers), A1/tail/ABI-v3 are not accepted yet, I18N is an
acceptance prerequisite, and the safety record for preserve-root
(chmod/chgrp/chown) blocks wiring until the listed gates pass.

Captain addendum records same-day movement: #1627 merged, PRs
#1624/#1625/#1626/#1628 open, and the fix/batch-1595-1616-1617 trio.
caomengxuan666 and others added 5 commits November 14, 2026 15:45
Replaces the "cannot follow end of file: not supported" subset with
coreutils 9.4 tail_forever/tail_forever_inotify/recheck semantics,
probed against WSL GNU 9.4:

* -f/--follow[=descriptor|name] (argmatch prefixes, ambiguous ''),
  -F = --follow=name --retry, mode assigned in argv order; --retry,
  -s (xstrtod: blanks, hex, inf; nan/negative rejected), --pid
  (xdectoumax <= PID_T_MAX, 0 clears), --max-unchanged-stats; the
  --retry/--pid warnings, "cannot follow '-' by name", and the zero-count
  early exit.
* Appends, partial lines, "X: file truncated" + restart, headers only
  when output switches files with data (previous = last operand),
  -q/-v, -c/-n +N offsets (incl. GNU's -c +N-past-EOF truncation).
* Name-state machine with GNU wording: has become inaccessible / has
  appeared;  following new file / has been replaced;  following new
  file / has become accessible / untailable file / no files remaining;
  OpenFileById keeps a renamed-away inode live like the inotify hash.
* Pipe stdin ignored under -f (POSIX), regular-file stdin followed by
  descriptor (stdin handle only queried, opened by path), lone console
  stdin read blocking.
* Exit status reproduces GNU's inotify-vs-polling path choice.
* Event-driven wake: FindFirstChangeNotificationW on parent dirs + the
  --pid process handle in one WaitForMultipleObjects, 100 ms re-stat
  tick for writers that keep the file open, never a 0 ms wait.
* Name mode holds no handle between wakes (rename-over rotation works);
  descriptor mode holds a fully shared handle.
* stdout flushed per chunk; a write error ends the run (broken pipe ->
  Err(CmdError::Io), host 141).

Also: regular files are dumped by seek + backward scan instead of
reading the whole file, which fixes a subtract-overflow panic for
`tail -n K` when the input had exactly K records or a partial last line
(`printf 'a\nb' | tail -n 1`), and a directory operand now gets its
header like GNU. `-f` no longer swallows the next argv word in the
obsolescent -NUM fold (`-f -s -1` kept -1 as the -s value).

Refs #1488

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Appended to the tail table in gen-cases.py (cases/tail.tsv
regenerated): follow rows that end on their own — a dead
--pid=2147483647 (ESRCH on Linux, no such process on Windows) ends the
follow after one re-read; "no files remaining", "cannot follow '-' by
name" and option errors end it before. Covers the inotify-vs-polling
exit statuses (-f/-F/--follow=name with a missing operand), directory
operands, regular-file stdin, -v/-q, -n/-c +N (incl. the -c +99
truncation report), -n 0, --follow argmatch, -s/--pid/
--max-unchanged-stats parsing, the -F -f warning, -z, plus the
partial-last-line and directory-header regressions. 72/72 strict
(stdout + rc vs GNU 9.4); stderr also byte-equal on every tail row.

Refs #1488

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
gate/wire/tail-follow.sh runs 27 writer-vs-follower scenarios on both
sides — WSL GNU 9.4 tail on ext4 (inotify path) and the rust2 driver on
NTFS — each terminating through --pid of a short-lived sleep, with
fixed STEP sleeps between file changes, and byte-compares stdout,
stderr and rc (broken pipe normalized to "pipe-death": GNU 141 by
SIGPIPE, driver 125 for Err(CmdError::Io)). Scenarios: append (partial
lines, default interval), multi-file headers, -q, -v, truncate,
truncate+rewrite, descriptor rename/delete, -F rotate/replace,
--retry delete/missing (single and multi), --follow=name delete /
multi reappear / last-name renamed away, -f --retry, --pid exit,
stdin file/pipe/pipe+file, -c +N, -n 0, -z, -s 0.1, broken pipe.
Result: 27/27 PASS (gate/wire/tail-follow.out; also 27/27 at STEP=0.3).

Refs #1488

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
README: the tail follow section (GNU rules, event-driven wake, handle
policy per mode, the stdin-query exception to the handle red line, the
two gates). COVERAGE.md: tail row 27 -> 72 strict and the case total,
hand-edited (a full --coverage regeneration would also rewrite rows
other lanes own from a stale-ledger state).

Refs #1488

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…es (#1488)

Resolves the two upheld objections in the 2.0 checkpoint acceptance
record:

- Delete sys::std_input/stdin_kind/stdin_follow_target (library-side
  GetStdHandle/GetFileType/GetConsoleMode/GetFinalPathNameByHandleW
  on the real process stdin) — the README shape contract's red line
  ("no GetStdHandle, no fd 0 in the library") forbids exactly this.
- New StdinMeta { kind, follow_path } + tail_with_meta(): stdin
  follow now opens the file from injected follow_path and derives
  consumption offsets from injected reads (incl. the -c +N past-EOF
  truncation semantics GNU attests via probe: 'standard input':
  file truncated + re-dump from 0); Disk with no follow path parks
  alive-until--pid like GNU. Host-default StdinMeta::pipe_stream()
  means a host that cannot classify gets finite-byte-stream = GNU
  pipe class (-f ignored per POSIX ignore_fifo_and_pipe).
- Driver (the only process-IO-authorized layer) synthesizes the
  real classification and injects it; ABI-level metadata channel
  stays a wire-time item (checkpoint continuation order 2).

Upheld-compliant (kept, justified): Waker (FindFirstChangeNotifica-
tionW), Pid (OpenProcess on explicit --pid argv), File::open/path_stat
on named operands, handle_stat on command-opened handles — command-
built kernel handles from explicit args, same class as cat/mkdir.

Follow runtime proof: 4 new injected-IO live tests; live harness 27
-> 30 scenarios (stdin_file_truncate / stdin_file_c99 /
stdin_file_plus_files), 30/30 PASS vs WSL GNU ext4; run-gate.sh tail
72 GNU-gated pass / 0 fail (follow family tl28-72 = 45/45 strict).
cargo test -p winux-cmds: 724/0.

No new user-visible messages (no i18n obligation). Known same-layer
process-handle queries in tty.rs/test_cmd.rs are out of scope here,
flagged for the wiring review.
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核:PASS(含新头增量核查)→ 合入。

全套复核锚定头 3ff9c88(卷宗 review-winx-1626/CAPTAIN-VERIFY.md):身份/交集 ∅、GNU 锚点终止形态 45/45 + live 场景 27/27、L1 libtest 双边(头 716 过 / 基底 698 过,失败名集完全相同 = 既存 tty::tests 4 项)、L4 头 45/45 vs 基底 0/45(且修掉基底 tail.rs:136 溢出 panic)、L2 驱动双边构建 rc=0、L5 -D warnings 差值恰为 F2 的 2 个。

新头 ae59632 增量由队长逐 hunk 核过:tail/sys.rs 双 impl 的库侧真实进程 stdin 查询(GetStdHandle / GetConsoleMode / GetFinalPathNameByHandleW / stdin_kind / stdin_follow_target)确已全删,改为 driver 注入 StdinMeta;新增的 GetStdHandle 字样仅在文档注释里陈述红线。现头 CI 全绿(11 pass + 1 skipping)。

欠腿处置:全量 ctest 双边以现头 CI 证据替代(ctest 内零 tail 用例;X299 .bat 双边仍在跑,跑完只作合入后回归参考)。作者自报 live harness 30/30(含 3 个新 stdin 场景)未独立复跑——合入后欠一次 X299 抽查,记队长账。

两个 open finding 均非 Windows 产品缺陷,已立后续单:F1(非 Windows 单测挂起)→ #1637;F2(FFI 重声明签名对齐)→ #1638。不拦合入。

合入体 Refs-only,本 PR 不关 #1488。

@caomengxuan666
caomengxuan666 merged commit 5c6d511 into main Nov 14, 2026
11 checks passed
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.

1 participant