From e99a86bc57a07268d74fb8fcd063c2275ac616c3 Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 11:35:07 -0500 Subject: [PATCH 1/5] fix/no-ticket(remote-reply): keep the relay moving after a deleted leading stamp A reply log that loses the leading epoch on a line the cursor already consumed used to stop the relay for good. The reader now proves that one deletion and delivers only the lines appended after it. --- bin/fm-remote-delta-read.sh | 185 +++++++++++++++++++++++++++-- docs/remote-secondmates.md | 3 + tests/fm-remote-delta-read.test.sh | 116 ++++++++++++++++++ 3 files changed, 293 insertions(+), 11 deletions(-) diff --git a/bin/fm-remote-delta-read.sh b/bin/fm-remote-delta-read.sh index 84aef13d051..59516fc7ba7 100755 --- a/bin/fm-remote-delta-read.sh +++ b/bin/fm-remote-delta-read.sh @@ -9,6 +9,16 @@ # available, returns at most 65536 payload bytes, and never truncates or consumes # the source. A shortened or changed prefix returns a structured continuity-break # result instead of silently rebasing the cursor. +# One shortened prefix is not a break. A single leading emission stamp was +# deleted from a line the cursor already consumed: the stamp is the ` ` +# taken from that line's one `[at=]` tag, and putting those bytes back +# makes the cursor's prefix hash match. The reader then returns only the +# complete lines appended after the shifted line, and does not return the +# consumed line again. Its next offset is a position in the file as it exists +# now. Until those new lines exist it waits, so the relay stays armed. Any +# other shortened or changed prefix, including a real replacement of earlier +# bytes, is still a continuity break. If that proof cannot be run, the reader +# keeps the break. # # The log is sampled every FM_REMOTE_DELTA_POLL_SECONDS (default 0.5 seconds). # A complete line is visible on the next sample, and the window deadline can @@ -32,7 +42,7 @@ MAX_BYTES=${FM_REMOTE_DELTA_MAX_BYTES:-65536} POLL_SECONDS=${FM_REMOTE_DELTA_POLL_SECONDS:-0.5} die() { printf 'error: %s\n' "$1" >&2; exit 1; } -usage() { sed -n '2,11p' "$0" | sed 's/^# \{0,1\}//'; exit 2; } +usage() { sed -n '2,21p' "$0" | sed 's/^# \{0,1\}//'; exit 2; } sha256_file() { if command -v shasum >/dev/null 2>&1; then @@ -143,6 +153,132 @@ emit_break() { # printf 'reason=%s\n\n' "$1" } +emit_delta() { # + local to=$1 to_hash=$2 payload=$3 bytes payload_hash + bytes=$(LC_ALL=C wc -c < "$payload" | tr -d ' ') + payload_hash=$(sha256_file "$payload") + printf 'schema=fm-remote-delta.v1\n' + printf 'status=delta\n' + printf 'path=%s\n' "$REL" + printf 'from_offset=%s\n' "$OFFSET" + printf 'to_offset=%s\n' "$to" + printf 'from_prefix_sha256=%s\n' "$PREFIX" + printf 'to_prefix_sha256=%s\n' "$to_hash" + printf 'payload_sha256=%s\n' "$payload_hash" + printf 'payload_bytes=%s\n' "$bytes" + printf 'reason=\n\n' + cat "$payload" +} + +# Prove the cursor prefix is the current bytes with one leading emission stamp +# put back, and stage a delta of only the complete lines after that shift. +# Writes $TMP/recover-kind as delta, wait, or bound. Returns 1 when the file +# is not that one deletion, which includes a hasher that cannot be loaded. +recover_leading_stamp() { + rm -f -- "$TMP/recover-kind" "$TMP/recover-payload" "$TMP/recover-to" \ + "$TMP/recover-to-hash" "$TMP/recover-bound-hash" + perl -MDigest::SHA - "$TMP/source" "$SIZE" "$OFFSET" "$PREFIX" "$MAX_BYTES" "$TMP" <<'PERL' +use strict; +use warnings; +use Digest::SHA qw(sha256_hex); + +my ($path, $size, $offset, $want, $max_bytes, $out_dir) = @ARGV; +$size += 0; +$offset += 0; +$max_bytes += 0; +$want = lc $want; +exit 1 unless $want =~ /\A[0-9a-f]{64}\z/; +exit 1 if $offset < 1 || $size < 0 || $max_bytes < 1; + +open my $in, '<:raw', $path or exit 1; +my $data = do { local $/; <$in> }; +close $in; +$data = '' unless defined $data; +my $captured = length $data; + +my @starts = (0); +for (my $i = 0; $i < $captured; $i++) { + if (substr($data, $i, 1) eq "\n" && $i + 1 < $captured) { + push @starts, $i + 1; + } +} + +my $matches = 0; +my $stamp_len = 0; +START: for my $p (@starts) { + last START if $p >= $offset; + my $nl = index($data, "\n", $p); + next START if $nl < 0; + my $line = substr($data, $p, $nl - $p); + my $colon = index($line, ':'); + next START if $colon < 0; + my $head = substr($line, 0, $colon); + my @epochs = ($head =~ /\[at=([0-9]{1,12})\]/g); + next START unless @epochs == 1; + my $epoch = $epochs[0]; + next START if $epoch =~ /\A0\d/; + my $tag_at = index($head, "[at=$epoch]"); + next START if $tag_at < 0; + my $after = substr($head, $tag_at + length("[at=$epoch]")); + next START if index($after, '[at=') >= 0; + my $slen = length($epoch) + 1; + next START if $slen > $offset || $size < $offset - $slen; + # The original line, including its newline, sat entirely inside the cursor. + next START if ($nl + 1 + $slen) > $offset; + my $need = $offset - $slen; + next START if $need > $captured || $p > $need; + my $stamp = $epoch . ' '; + my $reconstructed = substr($data, 0, $p) . $stamp . substr($data, $p, $need - $p); + next START unless length($reconstructed) == $offset; + next START unless sha256_hex($reconstructed) eq $want; + $matches++; + $stamp_len = $slen; + last START if $matches > 1; +} +exit 1 unless $matches == 1; + +my $shifted = $offset - $stamp_len; +my $window_end = $shifted + $max_bytes; +$window_end = $captured if $window_end > $captured; +my $region = substr($data, $shifted, $window_end - $shifted); +my $complete = 0; +my $scan = 0; +while (1) { + my $nl = index($region, "\n", $scan); + last if $nl < 0; + $complete = $nl + 1; + $scan = $complete; +} + +sub write_text { + my ($name, $text) = @_; + open my $out, '>', "$out_dir/$name" or exit 1; + print {$out} $text or exit 1; + close $out or exit 1; +} + +if ($complete == 0) { + if (($size - $shifted) >= $max_bytes) { + write_text('recover-kind', "bound\n"); + write_text('recover-bound-hash', sha256_hex(substr($data, 0, $shifted)) . "\n"); + } else { + write_text('recover-kind', "wait\n"); + } + exit 0; +} + +my $payload = substr($region, 0, $complete); +my $to = $shifted + $complete; +write_text('recover-kind', "delta\n"); +write_text('recover-to', "$to\n"); +write_text('recover-to-hash', sha256_hex(substr($data, 0, $to)) . "\n"); +open my $raw, '>:raw', "$out_dir/recover-payload" or exit 1; +print {$raw} $payload or exit 1; +close $raw or exit 1; +exit 0; +PERL +} + [ "$#" -ge 3 ] && [ "$#" -le 4 ] || usage REL=$1 OFFSET=$2 @@ -178,19 +314,46 @@ while :; do # captured again on the next poll, never mistaken for stable. LAST_KEY=$KEY IFS= read -r SIZE < "$TMP/size" + CONTINUITY=ok if [ "$SIZE" -lt "$OFFSET" ]; then - copy_prefix "$TMP/source" "$SIZE" "$TMP/prefix" + CONTINUITY=short + else + copy_prefix "$TMP/source" "$OFFSET" "$TMP/prefix" ACTUAL=$(sha256_file "$TMP/prefix") - emit_break truncated "$SIZE" "$ACTUAL" - exit 0 - fi - copy_prefix "$TMP/source" "$OFFSET" "$TMP/prefix" - ACTUAL=$(sha256_file "$TMP/prefix") - if [ "$ACTUAL" != "$PREFIX" ]; then - emit_break prefix-changed "$SIZE" "$ACTUAL" - exit 0 + if [ "$ACTUAL" != "$PREFIX" ]; then + CONTINUITY=changed + fi fi - if [ "$SIZE" -gt "$OFFSET" ]; then + if [ "$CONTINUITY" != ok ]; then + RECOVER='break' + if recover_leading_stamp; then + IFS= read -r RECOVER < "$TMP/recover-kind" + fi + case "$RECOVER" in + delta) + IFS= read -r TO < "$TMP/recover-to" + IFS= read -r TO_HASH < "$TMP/recover-to-hash" + emit_delta "$TO" "$TO_HASH" "$TMP/recover-payload" + exit 0 + ;; + bound) + IFS= read -r ACTUAL < "$TMP/recover-bound-hash" + emit_break line-exceeds-bound "$SIZE" "$ACTUAL" + exit 0 + ;; + wait) ;; + *) + if [ "$CONTINUITY" = short ]; then + copy_prefix "$TMP/source" "$SIZE" "$TMP/prefix" + ACTUAL=$(sha256_file "$TMP/prefix") + emit_break truncated "$SIZE" "$ACTUAL" + else + emit_break prefix-changed "$SIZE" "$ACTUAL" + fi + exit 0 + ;; + esac + elif [ "$SIZE" -gt "$OFFSET" ]; then tail -c "+$((OFFSET + 1))" "$TMP/source" | head -c "$MAX_BYTES" > "$TMP/chunk" || true COMPLETE_BYTES=$(LC_ALL=C od -An -v -tu1 "$TMP/chunk" | awk ' { for (i = 1; i <= NF; i++) { bytes++; if ($i == 10) complete=bytes } } diff --git a/docs/remote-secondmates.md b/docs/remote-secondmates.md index 1eee27d6482..f27f30b16db 100644 --- a/docs/remote-secondmates.md +++ b/docs/remote-secondmates.md @@ -589,6 +589,9 @@ The [process-to-event operating contract](configuration.md#process-to-event-sour The source log is never truncated or consumed. A shortened or changed prefix stops the relay and surfaces a continuity failure instead of silently resetting the cursor. +The one exception is a deleted leading emission stamp on a line the cursor already consumed: the reader proves it by putting that stamp back and matching the cursor hash, then delivers only the lines appended after the shift, once, without repeating the consumed line. +`bin/fm-remote-delta-read.sh` owns that proof. +Any other shortened or changed prefix still stops the relay. The failure appends one `blocked` line to the parent status stream, which opens a decision. The line records the reason, the reader position (the cursor offset and the first 12 characters of the prefix hash), and the retirement count (how many times the route has been retired). diff --git a/tests/fm-remote-delta-read.test.sh b/tests/fm-remote-delta-read.test.sh index 49c37fe920a..eb76646d344 100755 --- a/tests/fm-remote-delta-read.test.sh +++ b/tests/fm-remote-delta-read.test.sh @@ -6,6 +6,9 @@ # * the delta schema: offsets, prefix and payload hashes, and payload bytes # * every continuity-break reason: truncated, prefix-changed, missing, and # line-exceeds-bound, plus an unsafe symlink or traversal target +# * a deleted leading emission stamp on an already-consumed line, followed +# by new lines, delivers those new lines once and does not repeat the +# consumed line; a real prefix replacement still breaks # * an incomplete tail line is withheld until a newline completes it # * exit 75 when the wait window closes with nothing appended # * the per-poll executable boundary: an unchanged log costs one stat per @@ -34,6 +37,15 @@ run_reader() { # [rel] "$READER" "${4:-$DELTA_LOG_REL}" "$1" "$2" "$3" } +file_sha() { shasum -a 256 "$1" | awk '{print $1}'; } +file_bytes() { wc -c < "$1" | tr -d ' '; } +payload_of() { awk 'p { print } $0 == "" { p = 1 }' "$1"; } + +# Delete the first " " in the log, the leading stamp the incident lost. +drop_leading_stamp() { # + STAMP="$2 " perl -i -0pe 's/\Q$ENV{STAMP}\E//' "$1" +} + # A growing log returns the complete appended lines with exact boundaries. : > "$DELTA_HOME/$DELTA_LOG_REL" run_reader 0 "$EMPTY_SHA" 4 > "$TMP_ROOT/growth.out" & @@ -217,4 +229,108 @@ run_reader 0 "$EMPTY_SHA" 1 'state/link.status' > /dev/null 2>&1 && \ fail 'a symlinked log was accepted' || true pass 'the reader refuses traversal, symlinks, and oversized tail lines' +# The incident: a line the cursor already consumed lost its leading " " +# stamp, so the file shrank, and later lines were appended after the shift. +# The consumed line must not be delivered again. The new lines must be +# delivered once. Until those new lines exist the reader keeps waiting, which +# is what lets the relay stay armed. +STAMP_EPOCH=1793980552 +STAMP_KEPT='kept before the shifted line' +STAMP_BODY="working [at=${STAMP_EPOCH}]: already mirrored" +STAMP_MIRRORED="${STAMP_EPOCH} ${STAMP_BODY}" +STAMP_NEW_A="working [at=1793980999]: first new reply" +STAMP_NEW_B='done [at=1793981000]: second new reply' +printf '%s\n%s\n' "$STAMP_KEPT" "$STAMP_MIRRORED" > "$DELTA_HOME/$DELTA_LOG_REL" +STAMP_CURSOR=$(file_bytes "$DELTA_HOME/$DELTA_LOG_REL") +STAMP_PREFIX=$(file_sha "$DELTA_HOME/$DELTA_LOG_REL") +drop_leading_stamp "$DELTA_HOME/$DELTA_LOG_REL" "$STAMP_EPOCH" +run_reader "$STAMP_CURSOR" "$STAMP_PREFIX" 1 > "$TMP_ROOT/stamp-wait.out" && \ + fail 'a stamp shrink with nothing new appended did not keep waiting' || RC=$? +[ "${RC:-0}" -eq 75 ] || fail "a stamp shrink with nothing new exited $RC instead of 75" +assert_not_contains "$(<"$TMP_ROOT/stamp-wait.out")" 'continuity-broken' \ + 'a deleted leading stamp stopped the relay before any new line existed' +printf '%s' "$STAMP_NEW_A" >> "$DELTA_HOME/$DELTA_LOG_REL" +run_reader "$STAMP_CURSOR" "$STAMP_PREFIX" 1 > "$TMP_ROOT/stamp-partial.out" && \ + fail 'an unfinished line after a stamp shrink was not withheld' || RC=$? +[ "${RC:-0}" -eq 75 ] || fail "an unfinished line after a stamp shrink exited $RC instead of 75" +assert_not_contains "$(<"$TMP_ROOT/stamp-partial.out")" 'continuity-broken' \ + 'an unfinished line after a stamp shrink stopped the relay' +assert_not_contains "$(<"$TMP_ROOT/stamp-partial.out")" 'already mirrored' \ + 'an unfinished line after a stamp shrink repeated the consumed line' +printf '\n%s\n' "$STAMP_NEW_B" >> "$DELTA_HOME/$DELTA_LOG_REL" +run_reader "$STAMP_CURSOR" "$STAMP_PREFIX" 2 > "$TMP_ROOT/stamp-delta.out" \ + || fail 'new lines after a deleted leading stamp did not produce a delta' +OUT=$(<"$TMP_ROOT/stamp-delta.out") +assert_contains "$OUT" 'status=delta' 'a stamp shrink followed by new lines did not produce a delta' +assert_contains "$OUT" "from_offset=$STAMP_CURSOR" 'the stamp-shrink delta did not start at the committed cursor' +assert_contains "$OUT" "from_prefix_sha256=$STAMP_PREFIX" 'the stamp-shrink delta did not keep the committed prefix hash' +STAMP_TO=$(printf '%s\n' "$OUT" | sed -n 's/^to_offset=//p') +STAMP_TO_HASH=$(printf '%s\n' "$OUT" | sed -n 's/^to_prefix_sha256=//p') +[ "$STAMP_TO" = "$(file_bytes "$DELTA_HOME/$DELTA_LOG_REL")" ] \ + || fail "the stamp-shrink delta did not advance to the repaired file end (got $STAMP_TO)" +[ "$STAMP_TO_HASH" = "$(file_sha "$DELTA_HOME/$DELTA_LOG_REL")" ] \ + || fail 'the stamp-shrink cursor hash is not the repaired file' +PAYLOAD=$(payload_of "$TMP_ROOT/stamp-delta.out") +[ "$PAYLOAD" = "$(printf '%s\n%s\n' "$STAMP_NEW_A" "$STAMP_NEW_B")" ] \ + || fail "the stamp-shrink delta did not deliver only the new lines: $PAYLOAD" +assert_not_contains "$PAYLOAD" 'already mirrored' \ + 'the stamp-shrink delta repeated the already-mirrored line' +run_reader "$STAMP_TO" "$STAMP_TO_HASH" 1 > "$TMP_ROOT/stamp-again.out" && \ + fail 'a second read delivered the stamp-shrink lines again' || RC=$? +[ "${RC:-0}" -eq 75 ] || fail "a second read after the stamp shrink exited $RC instead of 75" +assert_not_contains "$(<"$TMP_ROOT/stamp-again.out")" 'already mirrored' \ + 'a second read repeated the consumed line' +assert_not_contains "$(<"$TMP_ROOT/stamp-again.out")" 'first new reply' \ + 'a second read repeated a new line' +pass 'a deleted leading stamp delivers the new lines once and waits until they exist' + +# The deleted stamp can sit on an earlier consumed line. Bytes after it shift +# left, including a later line the cursor already consumed, and only the lines +# appended beyond the old cursor are new. +printf '%s\n%s\n' "$STAMP_MIRRORED" 'note: still mirrored' > "$DELTA_HOME/$DELTA_LOG_REL" +EARLIER_CURSOR=$(file_bytes "$DELTA_HOME/$DELTA_LOG_REL") +EARLIER_PREFIX=$(file_sha "$DELTA_HOME/$DELTA_LOG_REL") +drop_leading_stamp "$DELTA_HOME/$DELTA_LOG_REL" "$STAMP_EPOCH" +printf '%s\n' 'done [at=1793981000]: after the shift' >> "$DELTA_HOME/$DELTA_LOG_REL" +run_reader "$EARLIER_CURSOR" "$EARLIER_PREFIX" 2 > "$TMP_ROOT/stamp-earlier.out" \ + || fail 'a stamp deleted from an earlier line did not produce a delta' +OUT=$(<"$TMP_ROOT/stamp-earlier.out") +assert_contains "$OUT" 'status=delta' 'a stamp deleted from an earlier line did not produce a delta' +PAYLOAD=$(payload_of "$TMP_ROOT/stamp-earlier.out") +[ "$PAYLOAD" = 'done [at=1793981000]: after the shift' ] \ + || fail "a stamp deleted from an earlier line delivered more than the new line: $PAYLOAD" +assert_not_contains "$PAYLOAD" 'still mirrored' \ + 'a stamp deleted from an earlier line repeated a consumed later line' +pass 'a stamp deleted from an earlier consumed line delivers only the lines after the cursor' + +# The same shape with an earlier byte changed is a real replacement. The new +# line after it must not be delivered, and the relay must stop. +printf '%s\n%s\n' 'alpha' "$STAMP_MIRRORED" > "$DELTA_HOME/$DELTA_LOG_REL" +REPLACED_CURSOR=$(file_bytes "$DELTA_HOME/$DELTA_LOG_REL") +REPLACED_PREFIX=$(file_sha "$DELTA_HOME/$DELTA_LOG_REL") +printf '%s\n%s\n%s\n' 'beta' "$STAMP_BODY" 'brand new reply' > "$DELTA_HOME/$DELTA_LOG_REL" +run_reader "$REPLACED_CURSOR" "$REPLACED_PREFIX" 1 > "$TMP_ROOT/replaced.out" \ + || fail 'a replaced prefix did not exit 0' +OUT=$(<"$TMP_ROOT/replaced.out") +assert_contains "$OUT" 'status=continuity-broken' 'a replaced prefix did not stop the relay' +assert_contains "$OUT" 'reason=prefix-changed' 'a replaced prefix was not named prefix-changed' +assert_not_contains "$OUT" 'status=delta' 'a replaced prefix was delivered as a delta' +assert_not_contains "$OUT" 'brand new reply' 'a replaced prefix delivered the lines appended after it' +pass 'a real prefix replacement still stops the relay' + +# Chopping the tail is a shrink, not a deleted leading stamp, even when the +# remaining line still carries an emission tag. +printf '%s\n%s\n' "$STAMP_KEPT" "$STAMP_MIRRORED" > "$DELTA_HOME/$DELTA_LOG_REL" +CHOP_CURSOR=$(file_bytes "$DELTA_HOME/$DELTA_LOG_REL") +CHOP_PREFIX=$(file_sha "$DELTA_HOME/$DELTA_LOG_REL") +perl -e 'open my $fh, "+<", $ARGV[0] or die $!; binmode $fh; seek $fh, -11, 2 or die $!; truncate $fh, tell $fh or die $!' \ + "$DELTA_HOME/$DELTA_LOG_REL" +run_reader "$CHOP_CURSOR" "$CHOP_PREFIX" 1 > "$TMP_ROOT/chop.out" \ + || fail 'a chopped tail did not exit 0' +OUT=$(<"$TMP_ROOT/chop.out") +assert_contains "$OUT" 'status=continuity-broken' 'a chopped tail did not stop the relay' +assert_contains "$OUT" 'reason=truncated' 'a chopped tail was not named truncated' +assert_not_contains "$OUT" 'status=delta' 'a chopped tail was delivered as a delta' +pass 'chopping the tail still breaks continuity as truncated' + printf 'delta-read contract tests complete\n' From f3a6b8d538fd630d0f2840305522e47ae05eb007 Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 11:39:38 -0500 Subject: [PATCH 2/5] no-mistakes(review): Bound deleted-stamp recovery scan to one delta window --- bin/fm-remote-delta-read.sh | 44 ++++++++++++++++++++----------------- docs/remote-secondmates.md | 2 +- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/bin/fm-remote-delta-read.sh b/bin/fm-remote-delta-read.sh index 59516fc7ba7..1921743870a 100755 --- a/bin/fm-remote-delta-read.sh +++ b/bin/fm-remote-delta-read.sh @@ -171,7 +171,7 @@ emit_delta() { # } # Prove the cursor prefix is the current bytes with one leading emission stamp -# put back, and stage a delta of only the complete lines after that shift. +# put back on a line starting within MAX_BYTES of the cursor, and stage a delta of only the complete lines after that shift. # Writes $TMP/recover-kind as delta, wait, or bound. Returns 1 when the file # is not that one deletion, which includes a hasher that cannot be loaded. recover_leading_stamp() { @@ -196,20 +196,23 @@ close $in; $data = '' unless defined $data; my $captured = length $data; -my @starts = (0); -for (my $i = 0; $i < $captured; $i++) { - if (substr($data, $i, 1) eq "\n" && $i + 1 < $captured) { - push @starts, $i + 1; - } +my $lo = $offset > $max_bytes ? $offset - $max_bytes : 0; +my $p = 0; +if ($lo > 0) { + my $prev = index($data, "\n", $lo - 1); + exit 1 if $prev < 0; + $p = $prev + 1; } - -my $matches = 0; +my $base = Digest::SHA->new(256); +$base->add(substr($data, 0, $p)); +my $hashed = $p; my $stamp_len = 0; -START: for my $p (@starts) { - last START if $p >= $offset; +START: while ($p < $offset && $p < $captured) { my $nl = index($data, "\n", $p); - next START if $nl < 0; - my $line = substr($data, $p, $nl - $p); + last START if $nl < 0; + my $line_start = $p; + $p = $nl + 1; + my $line = substr($data, $line_start, $nl - $line_start); my $colon = index($line, ':'); next START if $colon < 0; my $head = substr($line, 0, $colon); @@ -226,16 +229,17 @@ START: for my $p (@starts) { # The original line, including its newline, sat entirely inside the cursor. next START if ($nl + 1 + $slen) > $offset; my $need = $offset - $slen; - next START if $need > $captured || $p > $need; - my $stamp = $epoch . ' '; - my $reconstructed = substr($data, 0, $p) . $stamp . substr($data, $p, $need - $p); - next START unless length($reconstructed) == $offset; - next START unless sha256_hex($reconstructed) eq $want; - $matches++; + next START if $need > $captured || $line_start > $need; + $base->add(substr($data, $hashed, $line_start - $hashed)); + $hashed = $line_start; + my $try = $base->clone; + $try->add($epoch . ' '); + $try->add(substr($data, $line_start, $need - $line_start)); + next START unless $try->hexdigest eq $want; $stamp_len = $slen; - last START if $matches > 1; + last START; } -exit 1 unless $matches == 1; +exit 1 unless $stamp_len; my $shifted = $offset - $stamp_len; my $window_end = $shifted + $max_bytes; diff --git a/docs/remote-secondmates.md b/docs/remote-secondmates.md index f27f30b16db..fd8d8f1197d 100644 --- a/docs/remote-secondmates.md +++ b/docs/remote-secondmates.md @@ -589,7 +589,7 @@ The [process-to-event operating contract](configuration.md#process-to-event-sour The source log is never truncated or consumed. A shortened or changed prefix stops the relay and surfaces a continuity failure instead of silently resetting the cursor. -The one exception is a deleted leading emission stamp on a line the cursor already consumed: the reader proves it by putting that stamp back and matching the cursor hash, then delivers only the lines appended after the shift, once, without repeating the consumed line. +The one exception is a deleted leading emission stamp on a line the cursor already consumed, within the last delta bound (`FM_REMOTE_DELTA_MAX_BYTES`) of bytes before the cursor: the reader proves it by putting that stamp back and matching the cursor hash, then delivers only the lines appended after the shift, once, without repeating the consumed line. `bin/fm-remote-delta-read.sh` owns that proof. Any other shortened or changed prefix still stops the relay. From c533427c2dcea34f762c9eaf344e6e4b2de3e0f6 Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 11:44:22 -0500 Subject: [PATCH 3/5] no-mistakes(review): Scan stamp candidates backward from cursor, stop at first match --- bin/fm-remote-delta-read.sh | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/bin/fm-remote-delta-read.sh b/bin/fm-remote-delta-read.sh index 1921743870a..8a05a51d728 100755 --- a/bin/fm-remote-delta-read.sh +++ b/bin/fm-remote-delta-read.sh @@ -203,10 +203,8 @@ if ($lo > 0) { exit 1 if $prev < 0; $p = $prev + 1; } -my $base = Digest::SHA->new(256); -$base->add(substr($data, 0, $p)); -my $hashed = $p; -my $stamp_len = 0; +my $window = $p; +my @candidates; START: while ($p < $offset && $p < $captured) { my $nl = index($data, "\n", $p); last START if $nl < 0; @@ -230,14 +228,22 @@ START: while ($p < $offset && $p < $captured) { next START if ($nl + 1 + $slen) > $offset; my $need = $offset - $slen; next START if $need > $captured || $line_start > $need; - $base->add(substr($data, $hashed, $line_start - $hashed)); - $hashed = $line_start; + push @candidates, [$line_start, $epoch, $need]; +} +exit 1 unless @candidates; + +my $base = Digest::SHA->new(256); +$base->add(substr($data, 0, $window)); +my $stamp_len = 0; +for my $candidate (reverse @candidates) { + my ($line_start, $epoch, $need) = @$candidate; my $try = $base->clone; + $try->add(substr($data, $window, $line_start - $window)); $try->add($epoch . ' '); $try->add(substr($data, $line_start, $need - $line_start)); - next START unless $try->hexdigest eq $want; - $stamp_len = $slen; - last START; + next unless $try->hexdigest eq $want; + $stamp_len = length($epoch) + 1; + last; } exit 1 unless $stamp_len; From 683e91dab2621f1a3d5a618039f5f370aaefaf93 Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 11:46:24 -0500 Subject: [PATCH 4/5] no-mistakes(review): Route normal append deltas through the shared emit_delta serializer --- bin/fm-remote-delta-read.sh | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/bin/fm-remote-delta-read.sh b/bin/fm-remote-delta-read.sh index 8a05a51d728..dde9410a0cb 100755 --- a/bin/fm-remote-delta-read.sh +++ b/bin/fm-remote-delta-read.sh @@ -375,18 +375,7 @@ while :; do TO=$((OFFSET + BYTES)) copy_prefix "$TMP/source" "$TO" "$TMP/to-prefix" TO_HASH=$(sha256_file "$TMP/to-prefix") - PAYLOAD_HASH=$(sha256_file "$TMP/payload") - printf 'schema=fm-remote-delta.v1\n' - printf 'status=delta\n' - printf 'path=%s\n' "$REL" - printf 'from_offset=%s\n' "$OFFSET" - printf 'to_offset=%s\n' "$TO" - printf 'from_prefix_sha256=%s\n' "$PREFIX" - printf 'to_prefix_sha256=%s\n' "$TO_HASH" - printf 'payload_sha256=%s\n' "$PAYLOAD_HASH" - printf 'payload_bytes=%s\n' "$BYTES" - printf 'reason=\n\n' - cat "$TMP/payload" + emit_delta "$TO" "$TO_HASH" "$TMP/payload" exit 0 fi if [ $((SIZE - OFFSET)) -ge "$MAX_BYTES" ]; then From d4d61d39c892f1029f7e3d96212c9e17bd951752 Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 11:51:07 -0500 Subject: [PATCH 5/5] no-mistakes(document): Note the delta-bound window in the delta-reader header --- bin/fm-remote-delta-read.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bin/fm-remote-delta-read.sh b/bin/fm-remote-delta-read.sh index dde9410a0cb..e07b686b37e 100755 --- a/bin/fm-remote-delta-read.sh +++ b/bin/fm-remote-delta-read.sh @@ -9,16 +9,16 @@ # available, returns at most 65536 payload bytes, and never truncates or consumes # the source. A shortened or changed prefix returns a structured continuity-break # result instead of silently rebasing the cursor. -# One shortened prefix is not a break. A single leading emission stamp was -# deleted from a line the cursor already consumed: the stamp is the ` ` -# taken from that line's one `[at=]` tag, and putting those bytes back -# makes the cursor's prefix hash match. The reader then returns only the -# complete lines appended after the shifted line, and does not return the -# consumed line again. Its next offset is a position in the file as it exists -# now. Until those new lines exist it waits, so the relay stays armed. Any -# other shortened or changed prefix, including a real replacement of earlier -# bytes, is still a continuity break. If that proof cannot be run, the reader -# keeps the break. +# One shortened prefix is not a break: a single leading emission stamp deleted +# from a consumed line starting within FM_REMOTE_DELTA_MAX_BYTES of the cursor. +# The stamp is the ` ` from that line's one `[at=]` tag, and +# putting it back makes the cursor's prefix hash match. The reader then returns +# only the complete lines appended after the shifted line, never the consumed +# line again. Its next offset is a position in the file as it exists now. +# Until those new lines exist it waits, so the relay stays armed. Any other +# shortened or changed prefix, including a real replacement of earlier bytes +# or a stamp on an older line, is still a continuity break. If that proof +# cannot be run, the reader keeps the break. # # The log is sampled every FM_REMOTE_DELTA_POLL_SECONDS (default 0.5 seconds). # A complete line is visible on the next sample, and the window deadline can