carlos / platform Public

host: a chunk already delivered byte for byte counts as shipped (H5-M7 R2)

Astra: a lost acknowledgement on chunk A of a split batch made every
retry conflict on A's key and never reach B. On ErrCASConflict the
shippers now read the object; an identical body is delivered and they
move on, a different one keeps today's refusal and is logged. Both
logship and errship, with lost-ack regressions. Census: host envelope.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 0053a0015e9600186e6dcdce0f60c80461cc9a11 parent b9e4162
5 files changed, +154 −3
  • internal/host/errship.go +1 −1
  • internal/host/errship_test.go +37 −0
  • internal/host/logship.go +23 −1
  • internal/host/logship_test.go +92 −0
  • internal/provision/testdata/plan14-root-reads-sec-console-dos.tsv +1 −1
diff --git a/internal/host/errship.go b/internal/host/errship.go
index c6e5ea76c..247ec258e 100644
--- a/internal/host/errship.go
+++ b/internal/host/errship.go
@@ -93,7 +93,7 @@ func errshipFlush(ctx context.Context, s release.Store, pending map[applog.AppKe
done = len(recs)
break
}
- if _, err := s.Put(ctx, key, bodies[i], ""); err != nil {
+ if err := shipChunk(ctx, s, "errship", key, bodies[i]); err != nil {
log.Printf("agent: errship flush %s/%s: %v (%d records held)", k.Acct, k.App, err, len(recs)-done)
break
}
diff --git a/internal/host/errship_test.go b/internal/host/errship_test.go
index 1808b48f4..6fead1c45 100644
--- a/internal/host/errship_test.go
+++ b/internal/host/errship_test.go
@@ -58,3 +58,40 @@ func TestErrshipFlushHoldsOnFailure(t *testing.T) {
t.Fatal("a failed flush must keep its records for the next round")
}
}
+
+// H5-M7 review 2, errship: storage commits chunk A but the flush sees a
+// timeout; the retry counts A delivered and delivers B, nothing twice.
+func TestErrshipLostAckDoesNotStrandLaterChunks(t *testing.T) {
+ dir := release.NewDirStore(t.TempDir())
+ app := applog.AppKey{Acct: "bab", App: "web"}
+ ts := time.Date(2026, 9, 7, 10, 0, 0, 0, time.UTC)
+ recs := make([]apperr.Record, applog.WriteChunkLines+100) // two chunks by line count
+ for i := range recs {
+ recs[i] = apperr.Record{TS: ts, Box: "box1", Seq: uint64(1 + i), Source: apperr.SourceEdge, Name: "500 GET /x", FP: "f"}
+ }
+ runs, _, err := apperr.EncodeChunks(recs)
+ if err != nil || len(runs) != 2 {
+ t.Fatalf("EncodeChunks: %d runs, %v", len(runs), err)
+ }
+ s := &lostAckStore{Store: dir, lost: 1}
+ pending := map[applog.AppKey][]apperr.Record{app: recs}
+ errshipFlush(context.Background(), s, pending, nil)
+ if len(pending[app]) != len(recs) {
+ t.Fatalf("after the lost acknowledgement %d records held, want all %d", len(pending[app]), len(recs))
+ }
+ errshipFlush(context.Background(), s, pending, nil)
+ if len(pending) != 0 {
+ t.Fatalf("%d records still held after the retry; later chunks stranded", len(pending[app]))
+ }
+ prefix := mustKey(t)(applog.FamilyPrefix(applog.FamilyErrors, app.Acct, app.App))
+ perBox, trimmed, err := applog.ReadChunks[apperr.Record](context.Background(), dir, prefix,
+ time.Time{}, time.Time{}, func(apperr.Record) bool { return true }, 2*len(recs))
+ if err != nil || trimmed || len(perBox) != 1 || len(perBox[0]) != len(recs) {
+ t.Fatalf("read back: %v trimmed=%v; want %d records once each", err, trimmed, len(recs))
+ }
+ for i, r := range perBox[0] {
+ if r.Seq != recs[i].Seq {
+ t.Fatalf("record %d has seq %d, want %d", i, r.Seq, recs[i].Seq)
+ }
+ }
+}
diff --git a/internal/host/logship.go b/internal/host/logship.go
index 89376b1b0..a9b873e26 100644
--- a/internal/host/logship.go
+++ b/internal/host/logship.go
@@ -14,6 +14,7 @@
package host
import (
+ "bytes"
"context"
"errors"
"log"
@@ -139,7 +140,7 @@ func logshipFlush(ctx context.Context, s release.Store, pending map[applog.AppKe
done = len(recs)
break
}
- if _, err := s.Put(ctx, key, bodies[i], ""); err != nil {
+ if err := shipChunk(ctx, s, "logship", key, bodies[i]); err != nil {
log.Printf("agent: logship flush %s/%s: %v (%d records held)", k.Acct, k.App, err, len(recs)-done)
break
}
@@ -154,6 +155,27 @@ func logshipFlush(ctx context.Context, s release.Store, pending map[applog.AppKe
}
}
+// shipChunk writes body at key, create-only. A conflict is a chunk already
+// there: when the existing object is this very body, it is one an earlier
+// flush delivered and lost the acknowledgement of (encoding is
+// deterministic, and the key carries the first seq and the size), so it
+// counts as delivered and the caller moves on to the next chunk; otherwise
+// the conflict stands and the records stay held. Without this a lost
+// acknowledgement on one chunk of a split batch stranded every later chunk
+// behind it for good (H5-M7 review 2).
+func shipChunk(ctx context.Context, s release.Store, family, key string, body []byte) error {
+ _, err := s.Put(ctx, key, body, "")
+ if !errors.Is(err, release.ErrCASConflict) {
+ return err
+ }
+ got, _, gerr := s.Get(ctx, key)
+ if gerr == nil && bytes.Equal(got, body) {
+ return nil
+ }
+ log.Printf("agent: %s chunk %s already exists with other content; not overwritten", family, key)
+ return err
+}
+
// logshipKnownApps is the union of apps with pending records and apps the
// sink has ever buffered for — the prune worklist. A demo app is left off:
// its chunks are never shipped, so listing them would cost requests for nothing.
diff --git a/internal/host/logship_test.go b/internal/host/logship_test.go
index b370321fa..2f9e2f83b 100644
--- a/internal/host/logship_test.go
+++ b/internal/host/logship_test.go
@@ -1,6 +1,7 @@
package host
import (
+ "bytes"
"context"
"strings"
"testing"
@@ -252,3 +253,94 @@ func TestLogshipFlushHoldsFromTheFirstUnwrittenChunk(t *testing.T) {
t.Fatalf("read back after retry: %v trimmed=%v", err, trimmed)
}
}
+
+// lostAckStore commits each of the first lost Puts and then reports a
+// timeout, as a store whose acknowledgement was lost does.
+type lostAckStore struct {
+ release.Store
+ lost int
+}
+
+func (l *lostAckStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) {
+ etag, err := l.Store.Put(ctx, key, body, ifMatch)
+ if err == nil && l.lost > 0 {
+ l.lost--
+ return "", context.DeadlineExceeded
+ }
+ return etag, err
+}
+
+// H5-M7 review 2: storage commits chunk A but the flush sees a timeout, so
+// the whole batch stays held. The retry finds A already there, byte for
+// byte, counts it delivered and goes on to B. Nothing is lost or written
+// twice.
+func TestLogshipLostAckDoesNotStrandLaterChunks(t *testing.T) {
+ dir := release.NewDirStore(t.TempDir())
+ app := applog.AppKey{Acct: "bde", App: "ledgerpay"}
+ recs := worstCaseRecords()
+ runs, _, err := applog.EncodeChunks(recs)
+ if err != nil || len(runs) < 2 {
+ t.Fatalf("EncodeChunks: %d runs, %v", len(runs), err)
+ }
+ s := &lostAckStore{Store: dir, lost: 1}
+ pending := map[applog.AppKey][]applog.Record{app: recs}
+ logshipFlush(context.Background(), s, pending, nil)
+ if len(pending[app]) != len(recs) {
+ t.Fatalf("after the lost acknowledgement %d records held, want all %d", len(pending[app]), len(recs))
+ }
+ logshipFlush(context.Background(), s, pending, nil)
+ if len(pending) != 0 {
+ t.Fatalf("%d records still held after the retry; later chunks stranded", len(pending[app]))
+ }
+ prefix := mustKey(t)(applog.Prefix(app.Acct, app.App))
+ keys, err := dir.List(context.Background(), prefix)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var n int
+ for _, k := range keys {
+ if strings.HasSuffix(k, ".jsonl.gz") {
+ n++
+ }
+ }
+ if n != len(runs) {
+ t.Fatalf("%d chunks written, want %d", n, len(runs))
+ }
+ perBox, trimmed, err := applog.ReadChunks[applog.Record](context.Background(), dir, prefix,
+ time.Time{}, time.Time{}, func(applog.Record) bool { return true }, 2*logshipMaxPending)
+ if err != nil || trimmed || len(perBox) != 1 || len(perBox[0]) != len(recs) {
+ t.Fatalf("read back: %v trimmed=%v, %d records; want %d once each", err, trimmed, len(perBox[0]), len(recs))
+ }
+ for i, r := range perBox[0] {
+ if r.Seq != recs[i].Seq {
+ t.Fatalf("record %d has seq %d, want %d", i, r.Seq, recs[i].Seq)
+ }
+ }
+}
+
+// A conflict on a chunk whose existing body differs is still a refusal: the
+// records stay held and the other object is not overwritten.
+func TestLogshipConflictWithOtherContentHolds(t *testing.T) {
+ dir := release.NewDirStore(t.TempDir())
+ app := applog.AppKey{Acct: "bde", App: "ledgerpay"}
+ recs := worstCaseRecords()
+ runs, bodies, err := applog.EncodeChunks(recs)
+ if err != nil || len(runs) < 2 {
+ t.Fatalf("EncodeChunks: %d runs, %v", len(runs), err)
+ }
+ r := runs[0]
+ key := mustKey(t)(applog.ChunkKey(app.Acct, app.App, r[0].Box, applog.HourOf(r[0].TS), r[0].Seq, len(bodies[0])))
+ other := append([]byte(nil), bodies[0]...)
+ other[len(other)-1] ^= 0xff
+ if _, err := dir.Put(context.Background(), key, other, ""); err != nil {
+ t.Fatal(err)
+ }
+ pending := map[applog.AppKey][]applog.Record{app: recs}
+ logshipFlush(context.Background(), dir, pending, nil)
+ if len(pending[app]) != len(recs) {
+ t.Fatalf("%d records held after a conflict with other content, want all %d", len(pending[app]), len(recs))
+ }
+ if got, _, _ := dir.Get(context.Background(), key); !bytes.Equal(got, other) {
+ t.Fatal("the existing object was overwritten")
+ }
+}
diff --git a/internal/provision/testdata/plan14-root-reads-sec-console-dos.tsv b/internal/provision/testdata/plan14-root-reads-sec-console-dos.tsv
index 0417731ec..e36d54ab8 100644
--- a/internal/provision/testdata/plan14-root-reads-sec-console-dos.tsv
+++ b/internal/provision/testdata/plan14-root-reads-sec-console-dos.tsv
@@ -2,4 +2,4 @@
# 0 body, 0 sites, 3 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
@coverage internal/apperr eeed6e99fe5b8d2be14e07d3d4a96d4a3dd9995f9b99c80c09349f1b123d3ffa
@coverage internal/applog bf04191fc816519559492f0d598d9d64ec5edd96660c95342e980429bcc43300
-@coverage internal/host d0ba8b6b2fa6b5d995f5bb9ee8669f97ceb13ed627f7cd1516fea7bc6f1e88b5
+@coverage internal/host a2ac4c4ea718ff6934d7012a56580e283afad1bb222da9e25a3066249dc45a64