Skip to content

fix(monitor): clear saved offset when past host's log length (#244 Windows e2e) - #245

Merged
joelteply merged 1 commit into
canaryfrom
fix/clear-monitor-offset-on-joiner-connect
Apr 28, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/clear-monitor-offset-on-joiner-connect

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Stale-offset-past-EOF bug: saved offset survives a host log reset, leaving SSH tail blocked waiting for a line number that won't exist until N more writes happen. Probe host log length on first cycle; reset to EOF if saved offset is past it.

…ndows e2e)

Found by continuum-b69f (Windows tab) during the issue #244 test plan
2026-04-28: their messages were sent OK and reached the mesh host's
log, but my monitor (Mac) didn't display them.

Root cause: my saved monitor_offset = 128, host's current messages.jsonl
= 84 lines (truncated/reset since my last session — most likely from
teardown --flush or daemon respawn). My SSH-tail used `-n +118` which
BLOCKS waiting for line 118 to appear; meanwhile lines 70-84 (where
continuum-b69f's writes landed) were below my offset and silently
invisible.

This was a regression of #230's class — empty/zero offset was caught,
but stale-offset-past-EOF wasn't.

## Fix

cmd_connect joiner branch, after SSH config but before the reconnect
loop: if we have a saved offset that's > 0, query the host's current
log length via `ssh ... wc -l < messages.jsonl`. If saved offset >
host lines, the offset is stale (host log was reset since we last
ran). Clear the offset file + reset tail_pos to -n 0 (EOF). Print a
loud warning so the user knows we lost some history (matches
never-swallow-errors).

Single extra SSH round-trip on the FIRST cycle only (subsequent
reconnects use the formatter-persisted offset which is now consistent
with the live log).

## Verification

  bash -n: clean
  test/integration.sh tabs: 19/0 ✓

  Manual e2e: pending Mac monitor restart on this code +
  cross-machine verification with continuum-b69f's next probe.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings April 28, 2026 21:35
@joelteply
joelteply merged commit d3c1482 into canary Apr 28, 2026
@joelteply
joelteply deleted the fix/clear-monitor-offset-on-joiner-connect branch April 28, 2026 21:35

Copilot AI 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.

Pull request overview

Fixes a stale “saved monitor offset past EOF” condition that can cause joiner-side SSH tailing to block indefinitely after the host log is reset/truncated between sessions.

Changes:

  • Clarifies offset handling semantics in monitor() comments (empty/zero/non-numeric offsets → start at EOF).
  • Adds a first-cycle remote line-count probe to detect and clear offsets that exceed the host’s current log length.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread airc
Comment on lines +1207 to +1210
if [ "$tail_pos" != "-n 0" ]; then
local _saved_off; _saved_off=$(cat "$offset_file" 2>/dev/null | tr -d '[:space:]')
local _host_lines; _host_lines=$(ssh $ssh_opts "$host_target" "wc -l < $rhome/messages.jsonl 2>/dev/null || echo 0" 2>/dev/null | tr -d '[:space:]')
case "$_host_lines" in

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

With set -euo pipefail enabled, the command substitutions here can terminate monitor() before the reconnect loop runs. In particular, cat "$offset_file" (if the file disappears) or ssh ... (if the host is temporarily unreachable) will return non-zero inside $(...) and trigger an immediate exit. Wrap these substitutions so failures fall back safely (e.g., guard with || / if ...; then), similar to other parts of the script that avoid set -e aborts on assignments.

Copilot uses AI. Check for mistakes.
Comment thread airc
Comment on lines +1212 to +1218
esac
if [ "$_host_lines" -gt 0 ] && [ "$_saved_off" -gt "$_host_lines" ]; then
echo " ⚠ Saved monitor offset ($_saved_off) is past host's current log length ($_host_lines)."
echo " Host log was reset/truncated between sessions. Clearing offset, resuming at EOF."
rm -f "$offset_file" 2>/dev/null || true
tail_pos="-n 0"
fi

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

The "$_host_lines" -gt 0 guard prevents clearing a stale offset when the host log is empty/truncated to 0 lines (or missing). That’s exactly the problematic case: tail -n +<saved_off+1> will still block forever when _host_lines is 0 and _saved_off > 0. Consider treating 0 as a valid length and only skipping the comparison when the line-count probe definitively failed (e.g., return a sentinel like -1 on probe failure and skip only then).

Copilot uses AI. Check for mistakes.
Comment thread airc
Comment on lines +1201 to +1206
# Stale-offset sanity check (continuum-b69f's Windows e2e bug).
# If our saved offset is past the host's current log length, the
# SSH tail will block forever. Probe the host's line count first;
# if our offset > line count, reset to EOF (-n 0) and clear the
# stale offset file. Quick cheap query — single ssh round-trip,
# only on the first cycle of the reconnect loop.

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

This adds non-trivial joiner monitor behavior (probing remote log length and clearing offsets) but there’s no integration scenario that exercises the stale-offset-past-EOF case. Since test/integration.sh already has reconnect/offset-resume coverage, it would be valuable to add a scenario that truncates/resets the host’s messages.jsonl between sessions while leaving monitor_offset large, and asserts the joiner still receives new messages after reconnect.

Copilot uses AI. Check for mistakes.
joelteply added a commit that referenced this pull request Apr 28, 2026
…26-04-28) (#248)

Without rotation, the host's log + every joiner's local mirror grew
forever. Joel: 'is messages.log ever getting trimmed down? it could
over time consume the editor/reader if it isnt draining or cycling.'
Answer was no — confirmed by grep, no trim/rotate logic anywhere.

## Fix

New \`airc_core.log\` module: \`rotate_if_needed(path, max, keep)\` —
trim in place via tempfile + atomic rename. POSIX rename on
mac/linux/wsl; Windows fallback to remove-then-rename with a tiny
race window.

Wired in two periodic call sites:
- \`cmd_connect.sh\` heartbeat loop (host side, every 30s)
- \`monitor_formatter.py\` joiner mirror (every 100 mirrored lines)

Defaults: max 5000 lines, keep last 2500. Override via
\`AIRC_LOG_MAX_LINES\` / \`AIRC_LOG_KEEP_LINES\`.

## Why trim-in-place (not archive)

Chat history is conversational, not a permanent record; \`airc logs N\`
only ever shows the tail. Joiners with offsets pointing to discarded
lines are caught by #245's stale-offset detection (offset > current
line count → reset to EOF). New SSH-tail starts fresh on the trimmed
file via \`tail -F\`'s reopen-on-replace.

## Verification

- \`bash -n\` clean on cmd_connect.sh
- \`python ast.parse\` clean on log.py + monitor_formatter.py
- Standalone smoke: 10-line file rotated to last 3 with explicit
  \`max=5 keep=3\` flags — outputs 'line 8 / line 9 / line 10' ✓
- \`test/integration.sh tabs\`:  19/0 ✓
- \`test/integration.sh queue\`: 7/0 ✓

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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.

2 participants