Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(writer): let LastLine read a max-size runtime log line (#1238)

Deep Shahf6707b9b57e4
diff --git a/cli/beacon/internal/endpoint/asymptote/backfill.go b/cli/beacon/internal/endpoint/asymptote/backfill.go
index 343e7242..e0f48201 100644
--- a/cli/beacon/internal/endpoint/asymptote/backfill.go
+++ b/cli/beacon/internal/endpoint/asymptote/backfill.go
@@ -131,7 +131,7 @@ func scanBackfillLines(path string, since time.Time, keep func([]byte)) error {
}
defer f.Close()
scanner := bufio.NewScanner(f)
- scanner.Buffer(make([]byte, 0, 64*1024), writer.MaxEventBytes+1024)
+ scanner.Buffer(make([]byte, 0, 64*1024), writer.MaxLineBytes)
for scanner.Scan() {
raw := scanner.Bytes()
var line backfillLine
diff --git a/cli/beacon/internal/endpoint/writer/writer.go b/cli/beacon/internal/endpoint/writer/writer.go
index f99e7533..d02626b1 100644
--- a/cli/beacon/internal/endpoint/writer/writer.go
+++ b/cli/beacon/internal/endpoint/writer/writer.go
@@ -24,6 +24,9 @@ const (
DefaultRotateBytes = 10 * 1024 * 1024
DefaultRotateArchives = asymptoteobserve.DefaultRuntimeLogRotateArchives
RotateBytes = DefaultRotateBytes
+ // MaxLineBytes bounds a scanner reading the runtime log. It has to hold a full-size event
+ // and the newline AppendEvent writes after it; at MaxEventBytes a legal line reads as too long.
+ MaxLineBytes = MaxEventBytes + 1024
// runtimeFileMode is the shared (system-mode) log's mode. A log inside the current user's home
// is private instead; asymptoteobserve.RuntimeLogModes decides, identically in all three writers.
runtimeFileMode = asymptoteobserve.SharedRuntimeLogFileMode
@@ -294,7 +297,7 @@ func LastLine(path string) (string, error) {
defer f.Close()
var last string
scanner := bufio.NewScanner(f)
- scanner.Buffer(make([]byte, 0, 64*1024), MaxEventBytes)
+ scanner.Buffer(make([]byte, 0, 64*1024), MaxLineBytes)
for scanner.Scan() {
last = scanner.Text()
}
diff --git a/cli/beacon/internal/endpoint/writer/writer_test.go b/cli/beacon/internal/endpoint/writer/writer_test.go
index 573c03e8..5280df74 100644
--- a/cli/beacon/internal/endpoint/writer/writer_test.go
+++ b/cli/beacon/internal/endpoint/writer/writer_test.go
@@ -1,6 +1,7 @@
package writer
import (
+ "bytes"
"encoding/json"
"os"
"path/filepath"
@@ -418,3 +419,20 @@ func TestAppendEventDropsAnEmptiedGenAIObject(t *testing.T) {
t.Fatalf("compacted line kept an empty gen_ai object:\n%s", data)
}
}
+
+// AppendEvent may write a JSON object of exactly MaxEventBytes and then a newline.
+// The scanner has to hold that newline or it reports the legal line as too long.
+func TestLastLineReadsAMaxSizedEvent(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ payload := append(append([]byte(`"`), bytes.Repeat([]byte("x"), MaxEventBytes-2)...), '"')
+ if err := os.WriteFile(path, append(append([]byte("short\n"), payload...), '\n'), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ line, err := LastLine(path)
+ if err != nil {
+ t.Fatalf("LastLine: %v", err)
+ }
+ if line != string(payload) {
+ t.Fatalf("LastLine returned %d bytes, want %d", len(line), len(payload))
+ }
+}