Skip to content

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

fix(cursorsession): stop skipping records after a compaction (#881)

Rudy Mizrahi Celekli7c8081281535
diff --git a/cli/beacon/internal/cursorsession/collect.go b/cli/beacon/internal/cursorsession/collect.go
index ae73bac8..01b64d0a 100644
--- a/cli/beacon/internal/cursorsession/collect.go
+++ b/cli/beacon/internal/cursorsession/collect.go
@@ -20,6 +20,8 @@ const StateVersion = 1
type Cursor struct {
LastOrder int `json:"last_order"`
UpdatedAtMS int64 `json:"updated_at_ms,omitempty"`
+ // CompactionID is the last compaction record reported for the trace.
+ CompactionID string `json:"compaction_id,omitempty"`
}
type State struct {
@@ -180,11 +182,13 @@ func collectTrace(store *Store, ref TraceRef, state *State, opts CollectOptions,
if err != nil {
return false, err
}
- mapped := MapTrace(ref, records, MapOptions{MinOrder: cursor.LastOrder})
+ mapped := MapTrace(ref, records, MapOptions{MinOrder: cursor.LastOrder, CompactionID: cursor.CompactionID})
if len(mapped) == 0 {
cursor.UpdatedAtMS = ref.UpdatedAtUnixMS
- if len(records) > 0 && records[len(records)-1].Order > cursor.LastOrder {
- cursor.LastOrder = records[len(records)-1].Order
+ for _, record := range records {
+ if record.Order > cursor.LastOrder {
+ cursor.LastOrder = record.Order
+ }
}
return false, nil
}
@@ -194,7 +198,12 @@ func collectTrace(store *Store, ref TraceRef, state *State, opts CollectOptions,
cursor.LastOrder = partialLastOrder(start, mapped, i)
return true, err
}
- cursor.LastOrder = item.SourceOrder
+ if item.SourceOrder > cursor.LastOrder {
+ cursor.LastOrder = item.SourceOrder
+ }
+ if item.CompactionID != "" {
+ cursor.CompactionID = item.CompactionID
+ }
summary.EventsEmitted++
}
cursor.UpdatedAtMS = ref.UpdatedAtUnixMS
diff --git a/cli/beacon/internal/cursorsession/compaction_cursor_test.go b/cli/beacon/internal/cursorsession/compaction_cursor_test.go
new file mode 100644
index 00000000..a903d61d
--- /dev/null
+++ b/cli/beacon/internal/cursorsession/compaction_cursor_test.go
@@ -0,0 +1,57 @@
+package cursorsession
+
+import (
+ "bytes"
+ "database/sql"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+// The compaction record is built after every bubble, so it takes the next order. When a later
+// bubble arrives it takes that same order, which the cursor already sits on, so the sweep skipped it for good and
+// reported the compaction again under a new id.
+func TestCollectGlobalStorageKeepsBubbleAddedAfterCompaction(t *testing.T) {
+ dbPath := filepath.Join(t.TempDir(), "state.vscdb")
+ composer := func(lastUpdatedAt, headers string) string {
+ return `{"composerId":"c1","createdAt":"2026-09-19T20:00:00Z","lastUpdatedAt":"` + lastUpdatedAt + `",
+ "latestConversationSummary":{"lastBubbleId":"a1","summary":{"truncationLastBubbleIdInclusive":"u1","summary":"s"}},
+ "fullConversationHeadersOnly":` + headers + `}`
+ }
+ writeCursorDB(t, dbPath, map[string]string{
+ "composerData:c1": composer("2026-09-19T20:00:03Z", `[{"bubbleId":"u1","type":1},{"bubbleId":"a1","type":2}]`),
+ "bubbleId:c1:u1": `{"type":1,"text":"first prompt","createdAt":"2026-09-19T20:00:00Z"}`,
+ "bubbleId:c1:a1": `{"type":2,"text":"first answer","createdAt":"2026-09-19T20:00:01Z"}`,
+ })
+ statePath := filepath.Join(t.TempDir(), "state.json")
+ collect := func() string {
+ var out bytes.Buffer
+ if _, err := CollectOnce(CollectOptions{GlobalDBPath: dbPath, ProjectsDir: filepath.Join(t.TempDir(), "none"), StatePath: statePath, Print: true, Out: &out}); err != nil {
+ t.Fatal(err)
+ }
+ return out.String()
+ }
+ first := collect()
+ if !strings.Contains(first, "session.compacting") {
+ t.Fatalf("first sweep missing compaction event:\n%s", first)
+ }
+
+ db, err := sql.Open("sqlite", dbPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer db.Close()
+ if _, err := db.Exec(`UPDATE cursorDiskKV SET value=? WHERE key='composerData:c1'`, []byte(composer("2026-09-19T20:00:04Z", `[{"bubbleId":"u1","type":1},{"bubbleId":"a1","type":2},{"bubbleId":"u2","type":1}]`))); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := db.Exec(`INSERT INTO cursorDiskKV(key,value) VALUES ('bubbleId:c1:u2', ?)`, []byte(`{"type":1,"text":"second prompt","createdAt":"2026-09-19T20:00:04Z"}`)); err != nil {
+ t.Fatal(err)
+ }
+ second := collect()
+ if !strings.Contains(second, "second prompt") {
+ t.Fatalf("prompt added after compaction was never emitted:\n%s", second)
+ }
+ if strings.Contains(second, "session.compacting") {
+ t.Fatalf("compaction reported again on the next sweep:\n%s", second)
+ }
+}
diff --git a/cli/beacon/internal/cursorsession/mapper.go b/cli/beacon/internal/cursorsession/mapper.go
index f5e4a8cf..4f3dbace 100644
--- a/cli/beacon/internal/cursorsession/mapper.go
+++ b/cli/beacon/internal/cursorsession/mapper.go
@@ -18,16 +18,29 @@ import (
type MappedEvent struct {
SourceOrder int
Event schema.Event
+ // CompactionID is the native id of the compaction record this event reports, empty for every
+ // other event. The cursor remembers it, because a compaction has no stable place in the order.
+ CompactionID string
}
type MapOptions struct {
MinOrder int
+ // CompactionID is the compaction the cursor already reported.
+ CompactionID string
}
func MapTrace(ref TraceRef, records []Record, opts MapOptions) []MappedEvent {
var out []MappedEvent
for _, record := range records {
- if record.Order <= opts.MinOrder {
+ // A compaction is not positioned among the bubbles: it describes the composer as a whole
+ // and is rebuilt on every read. Ordering it after the last bubble would hand its order to
+ // the next bubble, which the cursor would then skip, so it is reported once by identity.
+ isCompaction := record.Subtype == "compaction"
+ if isCompaction {
+ if record.NativeID == opts.CompactionID {
+ continue
+ }
+ } else if record.Order <= opts.MinOrder {
continue
}
ev, ok := mapRecord(ref, record)
@@ -36,7 +49,11 @@ func MapTrace(ref TraceRef, records []Record, opts MapOptions) []MappedEvent {
}
key := fmt.Sprintf("%s:%s:%d:%s:%s", ref.Kind, ref.ID, record.Order, record.NativeID, record.Type)
ev.Event.ID = cursorEventID(key)
- out = append(out, MappedEvent{SourceOrder: record.Order, Event: ev})
+ mapped := MappedEvent{SourceOrder: record.Order, Event: ev}
+ if isCompaction {
+ mapped.CompactionID = record.NativeID
+ }
+ out = append(out, mapped)
// The link shares its prompt's record, so a sync that resumes from that record's order
// never splits the pair.
if link, ok := handoffLink(ref, record); ok {
diff --git a/cli/beacon/internal/cursorsession/store.go b/cli/beacon/internal/cursorsession/store.go
index 4b357327..9921c001 100644
--- a/cli/beacon/internal/cursorsession/store.go
+++ b/cli/beacon/internal/cursorsession/store.go
@@ -349,7 +349,7 @@ func (s *Store) readGlobalStorage(ref TraceRef) ([]Record, error) {
}
if summary := compactionSummary(composer); summary != nil {
used, limit := contextFromAny(composer, composer.Context, summary)
- records = append(records, Record{Order: order, NativeID: "compaction:" + stringValue(summary["compacted_through_id"]), Type: "system_event", Subtype: "compaction", TimestampMS: ref.UpdatedAtUnixMS, ContextUsedTokens: used, ContextLimitTokens: limit, Data: summary})
+ records = append(records, Record{Order: 0, NativeID: "compaction:" + stringValue(summary["compacted_through_id"]), Type: "system_event", Subtype: "compaction", TimestampMS: ref.UpdatedAtUnixMS, ContextUsedTokens: used, ContextLimitTokens: limit, Data: summary})
}
return records, nil
}
diff --git a/docs/changelog/cli.mdx b/docs/changelog/cli.mdx
index 77a2e184..ea5224a6 100644
--- a/docs/changelog/cli.mdx
+++ b/docs/changelog/cli.mdx
@@ -13,6 +13,7 @@ Stay up to date with the latest changes to the Agent Beacon CLI.
- **Codex install, hooks and inventory follow `CODEX_HOME`** · `beacon endpoint install`, `beacon endpoint hooks install --harness codex`, `beacon endpoint discover` and inventory now read and write `$CODEX_HOME/config.toml` and `$CODEX_HOME/hooks.json` when `CODEX_HOME` is set, as Codex does. They previously always used `~/.codex`, so with `CODEX_HOME` set, Codex never saw the OTLP exporter or the SessionStart hook and live capture silently recorded nothing. A per-user install or repair also writes the resolved `CODEX_HOME` into the scheduled inventory job, which launchd and systemd start without your shell's environment. A `CODEX_HOME` that is not an existing directory is now an install error rather than a silent write to `~/.codex`. Project-level `.codex` files are unchanged.
- **Oversized session events no longer stall a sync** · An event still over the 64 KB limit after its raw payload was dropped, such as a tool call with many large edit hunks, made session sync fail at that line on every sweep, so nothing later in that session was recorded. The writer now drops the event's retained content and marks it truncated, as the hook and collector writers already did, keeping the tool name, call ID and file path.
+- **Cursor sync keeps prompts in compacted conversations** · In a Cursor conversation with a context summary, `beacon endpoint cursor sync` and the session backfill skipped the first new record on every sync that found new activity, often the next prompt, and recorded the compaction again each time. New records are now kept and each compaction is recorded once. A compaction already recorded by an older release is recorded one more time after upgrading.
### v1.3.40 · October 14, 2026