Skip to content

A failed tail renders as "(no previous entry)" — save-session.sh:211 overwrites its own output on error #251

Closed

Description

@fdaviddpt

Problem

scripts/save-session.sh:211 uses the A && B || C idiom, which does not mean what it looks like:

[ -n "$LAST_LINE" ] && tail -n +"$LAST_LINE" "$MEMORY_FILE" > "$TMP_LAST_ENTRY" || echo "(no previous entry)" > "$TMP_LAST_ENTRY"

When A is true but B fails, C runs too — and C's > overwrites whatever B already wrote.

Reproduction

With a failing tail on PATH and a now.md containing a real entry:

now.md:            ## 09:00 / real previous entry
$TMP_LAST_ENTRY:   (no previous entry)
rc:                0
log:               (nothing)

The file had a previous entry. The summarizer is told it did not.

Why it matters

This is this repo's signature defect — an absence produced by a tool read as an absence in the world — and it is silent: rc 0, nothing logged, no marker. The summarizer loses its continuity anchor and can duplicate the previous entry back into now.md, which is the exact damage class #142/#224/#250 have been closing from the other end.

The same line also conflates three genuinely distinct states into one indistinguishable answer:

  1. there is genuinely no ## header yet (first entry of the buffer) — a legitimate empty
  2. now.md could not be read
  3. tail itself failed

A reader of (no previous entry) cannot tell which of the three happened, and only the first is benign.

Suggested direction

An explicit if/else rather than &&/||, so a failed tail is a distinct branch from an empty buffer. Per docs/'s three-state contract, case (2) and (3) should be a skipped/finding that says so, not a value that reads as data. Case (1) is the only one that should render as "no previous entry".

Provenance

Found incidentally while assessing PR #173 against main; not part of that PR's scope, and reproduced rather than inferred.

Activity

  1. fdaviddpt commented on Jul 31, 2026

    @fdaviddpt
    ContributorAuthor

    Two more instances of this exact idiom, both in scripts/log.sh, found by the agent working #252 and verified independently:

    log.sh:268    [ -n "$_hit" ] && echo "$_hit" || echo "$default"
    log.sh:332    [ -n "$val" ]  && echo "$val"  || echo "$default"

    Same shape as save-session.sh:211, and the same consequence: when A is true but the echo fails, C runs too. These are config readers, so the caller gets the value followed by the default — not one or the other. A partial write plus a fallback, concatenated, presented as a single answer.

    That is arguably worse than the save-session.sh case. There the failure produced a wrong-but-coherent value ((no previous entry)); here it produces a value that is neither what was read nor the default, and nothing in the output says two branches ran.

    Both are also silent for the same reason as the original: rc 0, nothing logged.

    Raising the count to three sites and shifting my read of the fix: this is no longer "fix a line", it is worth a sweep of the shell scripts for A && B || C and a decision about whether the pattern is banned outright. shellcheck SC2015 catches exactly this — filed as claude-supertool#665, where the argument for building that validator is precisely this issue. A linter would have caught all three at edit time; three hand-found instances in two days is the case for it.

    No change proposed here beyond widening the scope. Whoever takes it should decide the if/else rewrite for all three together rather than one at a time.

  2. added a commit that references this issue on Jul 31, 2026
    419b110
  3. added a commit that references this issue on Aug 2, 2026
    b632877
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions