Skip to content

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

fix(grok): detect a rewritten session by line count, not record count

Claude65f23b7cb1f9
diff --git a/cli/beacon/internal/groksession/collect.go b/cli/beacon/internal/groksession/collect.go
index a523ae5b..d4734f23 100644
--- a/cli/beacon/internal/groksession/collect.go
+++ b/cli/beacon/internal/groksession/collect.go
@@ -179,13 +179,22 @@ func collectSession(store *Store, ref SessionRef, state *State, opts CollectOpti
if err != nil {
return false, err
}
+ // A file that came back shorter than the cursor was rewritten, not appended to: Grok compacts a
+ // session and can migrate its chat format, and every line of the new file then sits at or below
+ // the cursor and would be skipped forever. Reset that file's position so the rewritten content
+ // is read; the Started flag is left alone, so session.started is not written a second time.
+ //
+ // The comparison is against the number of lines the file held, not the number of records that
+ // parsed out of it. They are different numbers -- one blank or half-written line makes the
+ // record count smaller than the line count -- and comparing the record count against a line
+ // cursor would read an ordinary malformed line as a rewrite and re-emit the whole file.
minChat := cursor.ChatLine
minLifecycle := cursor.LifecycleLine
- if len(data.Chat) < cursor.ChatLine {
+ if data.ChatLines < cursor.ChatLine {
minChat = 0
cursor.ChatLine = 0
}
- if len(data.Lifecycle) < cursor.LifecycleLine {
+ if data.LifecycleLines < cursor.LifecycleLine {
minLifecycle = 0
cursor.LifecycleLine = 0
}
diff --git a/cli/beacon/internal/groksession/collect_test.go b/cli/beacon/internal/groksession/collect_test.go
index 34de7926..9d0c848b 100644
--- a/cli/beacon/internal/groksession/collect_test.go
+++ b/cli/beacon/internal/groksession/collect_test.go
@@ -439,6 +439,71 @@ func TestRewrittenShorterFileIsNotSkipped(t *testing.T) {
}
}
+// A rewrite is detected by the file coming back shorter than the cursor. That test has to be made
+// against the number of lines the file held, not the number of records that parsed out of it: one
+// unparseable line in the middle makes the record count smaller than the line cursor, and reading
+// that as a rewrite would reset the cursor and append the whole session to the log a second time --
+// the duplicate this collector exists to avoid.
+func TestAMalformedLineIsNotMistakenForARewrite(t *testing.T) {
+ root := t.TempDir()
+ sessionDir := writeFixtureSession(t, root, "/tmp/grok project", "session-1")
+ chatPath := filepath.Join(sessionDir, "chat_history.jsonl")
+ statePath := filepath.Join(t.TempDir(), "state.json")
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+
+ // A line Beacon cannot parse, with a good line after it, so the cursor ends up past a line that
+ // produced no record and the record count is one short of it.
+ appendLine(t, chatPath, `{"type":"user","content":[{"type":"te`)
+ appendLine(t, chatPath, `{"type":"user","content":[{"type":"text","text":"after the bad line"}]}`)
+
+ opts := CollectOptions{SessionsDir: root, StatePath: statePath, LogPath: logPath, Write: true, UserMode: true}
+ first, err := CollectOnce(opts)
+ if err != nil {
+ t.Fatalf("first CollectOnce returned error: %v", err)
+ }
+ if first.MalformedLines != 1 {
+ t.Fatalf("MalformedLines = %d, want 1", first.MalformedLines)
+ }
+ state, err := LoadState(statePath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ cursor := state.Sessions["session-1"]
+ if cursor == nil || cursor.ChatLine <= len(readChatRecords(t, chatPath)) {
+ t.Fatalf("fixture did not produce a cursor past the record count: %+v", cursor)
+ }
+
+ // Nothing is rewritten. The session's *other* log grows, which is what brings the sweep back to
+ // a chat file whose record count is below the chat cursor.
+ appendLine(t, filepath.Join(sessionDir, "events.jsonl"),
+ `{"ts":"2026-05-21T16:52:10.000Z","type":"turn_ended","outcome":"success","turn_number":1}`)
+ touchNewer(t, sessionDir)
+
+ before := logActions(t, logPath)
+ second, err := CollectOnce(opts)
+ if err != nil {
+ t.Fatalf("second CollectOnce returned error: %v", err)
+ }
+ if second.EventsEmitted != 1 {
+ t.Fatalf("second sweep emitted %d events, want only the appended lifecycle row -- a reset would re-emit the chat history", second.EventsEmitted)
+ }
+ after := logActions(t, logPath)
+ if after["prompt.submitted"] != before["prompt.submitted"] {
+ t.Fatalf("prompt.submitted went from %d to %d; the chat cursor was reset", before["prompt.submitted"], after["prompt.submitted"])
+ }
+ if after["session.started"] != 1 {
+ t.Fatalf("session.started written %d times, want 1", after["session.started"])
+ }
+}
+
+// readChatRecords reports how many chat records parse out of a file, which is the number the shrink
+// check must NOT be made against.
+func readChatRecords(t *testing.T, path string) []ChatMessage {
+ t.Helper()
+ records, _, _ := readJSONLines[ChatMessage](path)
+ return records
+}
+
func urlPathEscape(path string) string {
replacer := strings.NewReplacer("/", "%2F", " ", "%20")
return replacer.Replace(path)
diff --git a/cli/beacon/internal/groksession/store.go b/cli/beacon/internal/groksession/store.go
index 20e91757..cedb02a8 100644
--- a/cli/beacon/internal/groksession/store.go
+++ b/cli/beacon/internal/groksession/store.go
@@ -100,10 +100,11 @@ func (s *Store) Read(ref SessionRef) (SessionData, Stats, error) {
data.Ref.Workspace = prompt.WorkingDirectory
}
}
- data.Chat, stats.Malformed = readJSONLines[ChatMessage](filepath.Join(ref.SourcePath, "chat_history.jsonl"))
- lifecycle, malformed := readJSONLines[LifecycleEvent](filepath.Join(ref.SourcePath, "events.jsonl"))
+ data.Chat, data.ChatLines, stats.Malformed = readJSONLines[ChatMessage](filepath.Join(ref.SourcePath, "chat_history.jsonl"))
+ lifecycle, lifecycleLines, malformed := readJSONLines[LifecycleEvent](filepath.Join(ref.SourcePath, "events.jsonl"))
stats.Malformed += malformed
data.Lifecycle = lifecycle
+ data.LifecycleLines = lifecycleLines
if ref.Summary == nil {
if summary, err := readJSONFile[Summary](filepath.Join(ref.SourcePath, "summary.json")); err == nil {
data.Ref.Summary = summary
@@ -127,10 +128,17 @@ func readJSONFile[T any](path string) (*T, error) {
return &out, nil
}
-func readJSONLines[T any](path string) ([]T, int) {
+// readJSONLines parses one of a session's JSONL files, returning the records, the number of lines
+// it scanned, and how many of them did not parse.
+//
+// The line count is reported separately from len(records) and the two are not interchangeable: a
+// blank or unparseable line advances the count without producing a record. The collector compares
+// the count against its line cursor to notice a file that was rewritten shorter, and using the
+// record count there would read a single malformed line as a rewrite.
+func readJSONLines[T any](path string) ([]T, int, int) {
f, err := os.Open(path)
if err != nil {
- return nil, 0
+ return nil, 0, 0
}
defer f.Close()
var out []T
@@ -165,7 +173,7 @@ func readJSONLines[T any](path string) ([]T, int) {
if err := scanner.Err(); err != nil {
malformed++
}
- return out, malformed
+ return out, lineNo, malformed
}
func readTerminalLogs(dir string, logs map[string]string) error {
diff --git a/cli/beacon/internal/groksession/types.go b/cli/beacon/internal/groksession/types.go
index 6a9619db..e97732f9 100644
--- a/cli/beacon/internal/groksession/types.go
+++ b/cli/beacon/internal/groksession/types.go
@@ -87,11 +87,16 @@ type LifecycleEvent struct {
}
type SessionData struct {
- Ref SessionRef
- Prompt *PromptContext
- Chat []ChatMessage
- Lifecycle []LifecycleEvent
- TerminalLogs map[string]string
+ Ref SessionRef
+ Prompt *PromptContext
+ Chat []ChatMessage
+ Lifecycle []LifecycleEvent
+ // ChatLines and LifecycleLines are how many lines each JSONL file held, which is not len(Chat)
+ // or len(Lifecycle): a blank or unparseable line counts here and produces no record. They are
+ // what the collector's line cursor is comparable against.
+ ChatLines int
+ LifecycleLines int
+ TerminalLogs map[string]string
}
// SourceKind names where a mapped event came from, so the collector can keep one cursor per