Skip to content

Name the rotation archive relatively, and let a failed rotation say so (#252) - #254

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/252-tar-drive-letter
Jul 31, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/252-tar-drive-letter

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

What was wrong

rotate_logs() passed an absolute archive path to tar -czf. On Windows that path begins with a drive letter, and GNU tar reads an -f argument whose colon precedes the first slash as host:path — so it tried to resolve a machine named C.

Reproduced locally against GNU tar 1.35 (no Windows needed — the parse rule is the bug):

$ gtar -czf "C:/tmp/x/out.tar.gz" -C /tmp/x a.log
tar (child): Cannot connect to C: resolve failed
tar: Child returned status 128
exit=2

$ gtar -czf "/tmp/x/we:ird.tar.gz" -C /tmp/x a.log    # colon AFTER a slash
exit=0

That second line is the whole mechanism: it is the colon's position, not its presence. Only -f is parsed this way — -C never was, which is why already passing the directory separately did not save it.

The reporter's install had archived nothing since 2026-06-23: 29 files, 2.3 MB, one identical ERROR: tar failed for 19 logs per day for five weeks.

The tar decision — and why not --force-local

--force-local is the usual GNU answer and is the trap. Tested on both implementations:

GNU tar 1.35 bsdtar 3.5.3 (/usr/bin/tar, macOS)
absolute drive-letter -f exit 2, "Cannot connect to C" exit 1, "Failed to open"
--force-local works exit 1, "Option --force-local is not supported", no archive
relative -f, no directory prefix exit 0 exit 0

Hardcoding --force-local would have fixed Windows by disabling rotation on macOS entirely — into the branch whose stderr was discarded, so nobody would have seen it. That is #204's shape.

Detecting the implementation was the other option and was also rejected, for a subtler reason: the axis is the tar binary, not the OS. Git Bash ships GNU tar, but Windows also carries a bsdtar in System32 that can win the PATH, so a platform-keyed detector would be a second bug waiting to happen.

So the archive is named relatively, from inside the log directory. A name with no slash has nowhere to put a colon, so no tar can parse it as remote and none of them needs a flag.

before:  -czf  /Users/…/.remember/logs/logs-2026-07.tar.gz  -C  /Users/…/logs  memory-….log
after:   -czf  logs-2026-07.tar.gz                              memory-….log

Both implementations produce identical member lists.

The defect underneath — why it was permanent

The tar call was the visible half. 2>/dev/null discarded the one sentence that identified the cause, and rotate_logs returned 0 whether it had archived, failed, or found nothing to do — so "nothing to rotate" and "permanently broken" were the same event to every caller.

Now there are three states:

return log
nothing aged out 0 silent — not an event
archived N 0 archived N logs → logs-2026-07.tar.gz
could not run 1 ERROR: … : <tar's own stderr>

Plus two things one line a day demonstrably could not do:

  • After 3 consecutive failures the line changes and names the consequence — how many files, where, and that nothing will clear them. Not louder on every invocation; that is just faster wallpaper.
  • /remember:doctor now reports it, with the reason. hook-errors.log was empty throughout the reporter's five weeks, so that report would have said "OK" the entire time. commands/doctor.md already relays WARN lines verbatim, so no doc change was needed.
WARN Log rotation has failed 3 time(s) in a row (last 2026-07-31 10:55:03)
     Reason: tar (child): Cannot connect to C: resolve failed
     4 log file(s) currently in …/logs; the aged ones will not be archived
     until this is fixed. Capture is unaffected.

It deliberately does not self-correct by deleting. A stuck rotation could bound the growth by deleting aged logs instead of archiving them, and does not: those logs are the only record of what went wrong, on a machine that has just proved it cannot run the archive path. Accumulation is visible and recoverable; deletion is neither. Bounded by making it loud, not destructive.

run-consolidation.sh now calls rotate_logs || true — load-bearing under its set -e, since a log directory that cannot be tidied must not abort the consolidation.

Tests — RED then GREEN

tests/test_log_rotation.py, 9 tests. RED 5/9, then GREEN 9/9 — and green again with GNU tar 1.35 forced ahead of bsdtar on PATH.

RED: the argv pin, tar's reason reaching the log, failure distinguishable from empty, the 3-failure escalation, and doctor reporting it.

The argv assertion is deliberately about the constructed -f argument, not tar's behaviour on the runner: a POSIX box has no drive letter to reproduce with, but "the argument contains no /" is checkable anywhere and is strictly stronger than "contains no colon". Real artifacts elsewhere — a real temp log dir, really backdated mtimes (find -mtime +7 is what selects them, so faking that would be testing the fixture), and a real tar for the success path. The two tar shims assert they actually executed, and the breadcrumb-clearing test asserts the breadcrumb exists before checking it is cleared, so it cannot pass vacuously.

Found while here — not fixed, needs its own issue

rotate_logs can silently destroy previously archived logs. The archive is named from date -v-7d +%Y-%m, so two rotations in the same month target the same logs-YYYY-MM.tar.gz, and tar -czf truncates. Second run wins; the first run's logs are gone, and their originals were already deleted. Demonstrated:

run 1 → logs-2026-07.tar.gz contains memory-2026-06-01.log, memory-2026-06-02.log
run 2 → logs-2026-07.tar.gz contains memory-2026-07-20.log, memory-2026-07-21.log
June logs still in the archive: 0

Unrecoverable, independent of this bug, and reachable on every platform. Related: because the month comes from "7 days ago" rather than from the logs being archived, a backlog spanning a month boundary — exactly what a previously-broken rotation guarantees — all lands in one archive whose name misstates its contents.

#251's idiom is in log.sh twice, at log.sh:268 and log.sh:332, both [ -n "$X" ] && echo "$X" || echo "$default". If the echo fails the default is appended after the partial output, so a config read can return value-plus-default. Same shape as save-session.sh:211.

Closes #252

#252)

rotate_logs() passed an absolute archive path to `tar -czf`. On Windows
that path starts with a drive letter, and GNU tar reads an `-f` argument
whose colon precedes the first slash as `host:path` -- so it tried to
resolve a machine named `C` and exited 2. Nothing had been archived on
the reporter's install since 2026-06-23: 29 files, 2.3 MB, one identical
error line per day for five weeks.

The archive is now named relatively from inside the log directory. Not
`--force-local`: bsdtar (/usr/bin/tar on macOS) rejects that flag outright
with exit 1 and writes no archive, so hardcoding it would have fixed
Windows by disabling macOS -- inside a branch whose stderr was discarded.
Not implementation detection either: the axis is the tar binary, not the
OS, since Windows also carries a bsdtar that can win the PATH. A name with
no slash has nowhere to put a colon, so no tar parses it as remote and none
needs a flag. Verified against GNU tar 1.35 and bsdtar 3.5.3.

The permanence was the larger half. `2>/dev/null` discarded the one
sentence that identified the cause, and rotate_logs returned 0 whether it
had archived, failed, or found nothing to do. There are now three states:
silent success, logged success, and a non-zero return carrying tar's own
diagnostic. After three consecutive failures the line names the
consequence instead of repeating itself, and /remember:doctor reports the
breadcrumb with its reason -- hook-errors.log was empty throughout those
five weeks, so that report would have said OK the entire time.

Rotation deliberately does not self-correct by deleting: those logs are
the only record of what went wrong, on a machine that has just proved it
cannot run the archive path.

run-consolidation.sh now calls `rotate_logs || true`, load-bearing under
its `set -e`.

Tests first, red 5/9 then green 9/9, and green again with GNU tar forced
ahead of bsdtar on PATH. Full suite 1129 passed, coverage 94.13%.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 7353a47 into main Jul 31, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the fix/252-tar-drive-letter branch July 31, 2026 09:30
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.

Log rotation fails permanently on Windows: GNU tar parses the drive-letter archive path as a remote host

1 participant