Skip to content

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

fix: grok session collector bugs — incremental cursor, system mode state, turn lifecycle, and discovery

Cursor Agentaae24d6b6bba
diff --git a/cli/beacon/cmd/endpoint_diagnostics.go b/cli/beacon/cmd/endpoint_diagnostics.go
index dff25c31..f35511df 100644
--- a/cli/beacon/cmd/endpoint_diagnostics.go
+++ b/cli/beacon/cmd/endpoint_diagnostics.go
@@ -1402,10 +1402,13 @@ func harnessAction(h harness.Harness, effectiveUserMode bool) string {
return doctorRepairCommand(effectiveUserMode)
case "admin_otel":
return "beacon endpoint integrations claude-cowork setup"
- // fx has nothing to install into, so the remedy is to run a collection sweep rather than to
- // configure the runtime. Pointing at `hooks install` here would name a command that cannot
- // succeed for this harness.
+ // fx and grok have nothing to install into, so the remedy is to run a collection sweep rather
+ // than to configure the runtime. Pointing at `hooks install` here would name a command that
+ // cannot succeed for these harnesses.
case "session_log":
+ if h.Name == "grok" {
+ return "beacon endpoint grok sync"
+ }
return "beacon endpoint fx sync"
}
return ""
diff --git a/cli/beacon/cmd/endpoint_grok.go b/cli/beacon/cmd/endpoint_grok.go
index 6dbfe30f..7a57e5e9 100644
--- a/cli/beacon/cmd/endpoint_grok.go
+++ b/cli/beacon/cmd/endpoint_grok.go
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "path/filepath"
"time"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"
@@ -77,7 +78,7 @@ func runEndpointGrokSync(cmd *cobra.Command, args []string) error {
UserMode: userMode,
}
if !endpointGrokOpts.print {
- opts.StatePath = resolveGrokStatePath(endpointGrokOpts.statePath)
+ opts.StatePath = resolveGrokStatePath(endpointGrokOpts.statePath, userMode)
opts.LogPath = lifecycle.ResolveRuntimeLog(userMode, endpointGrokOpts.logPath).EffectiveLogPath
}
@@ -128,6 +129,7 @@ func reportGrokSweep(cmd *cobra.Command, summary groksession.SummaryResult) {
}
func runEndpointGrokStatus(cmd *cobra.Command, args []string) error {
+ userMode := endpointUserMode()
store, err := groksession.NewStore(endpointGrokOpts.sessionsDir)
if err != nil {
return err
@@ -136,7 +138,7 @@ func runEndpointGrokStatus(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
- state, err := groksession.LoadState(resolveGrokStatePath(endpointGrokOpts.statePath))
+ state, err := groksession.LoadState(resolveGrokStatePath(endpointGrokOpts.statePath, userMode))
if err != nil {
return err
}
@@ -145,7 +147,7 @@ func runEndpointGrokStatus(cmd *cobra.Command, args []string) error {
Collected int `json:"collected"`
StatePath string `json:"state_path"`
}
- out := status{Sessions: len(refs), StatePath: resolveGrokStatePath(endpointGrokOpts.statePath)}
+ out := status{Sessions: len(refs), StatePath: resolveGrokStatePath(endpointGrokOpts.statePath, userMode)}
for _, ref := range refs {
cursor := state.Sessions[ref.ID]
if cursor != nil && cursor.UpdatedAtMS >= ref.ModTimeUnixMS && cursor.Events > 0 {
@@ -162,9 +164,14 @@ func runEndpointGrokStatus(cmd *cobra.Command, args []string) error {
return nil
}
-func resolveGrokStatePath(path string) string {
+func resolveGrokStatePath(path string, userMode bool) string {
if path != "" {
return path
}
+ if !userMode {
+ if dir := filepath.Dir(lifecycle.ResolveRuntimeLog(false, "").EffectiveLogPath); dir != "" && dir != "." {
+ return filepath.Join(dir, "grok-state.json")
+ }
+ }
return groksession.DefaultStatePath()
}
diff --git a/cli/beacon/internal/endpoint/harness/grok.go b/cli/beacon/internal/endpoint/harness/grok.go
index 0a099e1f..f45f21fd 100644
--- a/cli/beacon/internal/endpoint/harness/grok.go
+++ b/cli/beacon/internal/endpoint/harness/grok.go
@@ -5,13 +5,14 @@ import (
"os"
"path/filepath"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/hooks"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/groksession"
)
const GrokName = groksession.Harness
func DiscoverGrok() Harness {
- h := Harness{Name: GrokName, DisplayName: "Grok Build", Capability: "hooks+session_log"}
+ h := Harness{Name: GrokName, DisplayName: "Grok Build"}
detectExecutable(&h, "grok")
home, _ := os.UserHomeDir()
sessionsDir := filepath.Join(home, ".grok", "sessions")
@@ -19,6 +20,13 @@ func DiscoverGrok() Harness {
if !h.Detected && dirExists(filepath.Join(home, ".grok")) {
h.Detected = true
}
+ if hooks.IsGrokInstalled(hooks.GrokOptions{}) {
+ h.Capability = "hooks"
+ h.TelemetryStatus = TelemetryEnabled
+ h.Message = "Beacon Grok Build hooks are configured"
+ return h
+ }
+ h.Capability = "session_log"
h.TelemetryStatus, h.Message = grokCollectionStatus(sessionsDir)
return h
}
diff --git a/cli/beacon/internal/groksession/collect.go b/cli/beacon/internal/groksession/collect.go
index eb09b899..513b898d 100644
--- a/cli/beacon/internal/groksession/collect.go
+++ b/cli/beacon/internal/groksession/collect.go
@@ -16,8 +16,11 @@ import (
const StateVersion = 1
type Cursor struct {
- UpdatedAtMS int64 `json:"updated_at_ms,omitempty"`
- Events int `json:"events,omitempty"`
+ UpdatedAtMS int64 `json:"updated_at_ms,omitempty"`
+ Events int `json:"events,omitempty"`
+ LastChatIndex int `json:"last_chat_index,omitempty"`
+ LastLifecycleIndex int `json:"last_lifecycle_index,omitempty"`
+ SessionStarted bool `json:"session_started,omitempty"`
}
type State struct {
@@ -161,9 +164,14 @@ func collectSession(store *Store, ref SessionRef, state *State, opts CollectOpti
if err != nil {
return false, err
}
- mapped := MapSession(data)
+ mapped := MapSession(data, MapOptions{
+ MinChatIndex: cursor.LastChatIndex,
+ MinLifecycleIndex: cursor.LastLifecycleIndex,
+ SkipSessionStarted: cursor.SessionStarted,
+ })
if len(mapped) == 0 {
cursor.UpdatedAtMS = ref.ModTimeUnixMS
+ advanceGrokCursor(cursor, data)
return false, nil
}
for _, item := range mapped {
@@ -173,10 +181,21 @@ func collectSession(store *Store, ref SessionRef, state *State, opts CollectOpti
summary.EventsEmitted++
}
cursor.UpdatedAtMS = ref.ModTimeUnixMS
- cursor.Events = len(mapped)
+ cursor.Events += len(mapped)
+ advanceGrokCursor(cursor, data)
return true, nil
}
+func advanceGrokCursor(cursor *Cursor, data SessionData) {
+ if n := len(data.Chat); n > cursor.LastChatIndex {
+ cursor.LastChatIndex = n
+ }
+ if n := len(data.Lifecycle); n > cursor.LastLifecycleIndex {
+ cursor.LastLifecycleIndex = n
+ }
+ cursor.SessionStarted = true
+}
+
func emit(event schema.Event, opts CollectOptions) error {
if opts.Print && opts.Out != nil {
data, err := json.Marshal(event)
diff --git a/cli/beacon/internal/groksession/collect_test.go b/cli/beacon/internal/groksession/collect_test.go
index 7739df74..80ef033e 100644
--- a/cli/beacon/internal/groksession/collect_test.go
+++ b/cli/beacon/internal/groksession/collect_test.go
@@ -39,7 +39,7 @@ func TestStoreListsAndMapsGrokSession(t *testing.T) {
if stats.Malformed != 0 {
t.Fatalf("Malformed = %d, want 0", stats.Malformed)
}
- events := MapSession(data)
+ events := MapSession(data, MapOptions{})
if len(events) == 0 {
t.Fatal("MapSession returned no events")
}
diff --git a/cli/beacon/internal/groksession/mapper.go b/cli/beacon/internal/groksession/mapper.go
index 8307cdd2..c8b5eb68 100644
--- a/cli/beacon/internal/groksession/mapper.go
+++ b/cli/beacon/internal/groksession/mapper.go
@@ -10,9 +10,16 @@ import (
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
-func MapSession(data SessionData) []MappedEvent {
+type MapOptions struct {
+ MinChatIndex int
+ MinLifecycleIndex int
+ SkipSessionStarted bool
+}
+
+func MapSession(data SessionData, opts MapOptions) []MappedEvent {
m := &mapper{
data: data,
+ opts: opts,
toolByID: map[string]ToolCall{},
toolOutput: map[string]string{},
}
@@ -37,6 +44,7 @@ func MapSession(data SessionData) []MappedEvent {
type mapper struct {
data SessionData
+ opts MapOptions
out []MappedEvent
toolByID map[string]ToolCall
toolOutput map[string]string
@@ -69,6 +77,9 @@ func (m *mapper) orderedItems() []orderedItem {
}
func (m *mapper) consumeChat(msg ChatMessage) {
+ if msg.Index <= m.opts.MinChatIndex {
+ return
+ }
switch msg.Type {
case "user":
text := contentText(msg.Content)
@@ -121,6 +132,9 @@ func (m *mapper) consumeChat(msg ChatMessage) {
}
func (m *mapper) consumeLifecycle(ev LifecycleEvent) {
+ if ev.Index <= m.opts.MinLifecycleIndex {
+ return
+ }
switch ev.Type {
case "permission_requested":
out := m.base(ev.TS, "approval.requested", "approval", schema.SeverityInfo, schema.FidelityObserved, "Grok requested tool approval")
@@ -152,15 +166,23 @@ func (m *mapper) consumeLifecycle(ev LifecycleEvent) {
out.Raw = map[string]interface{}{"grok": ev}
m.append(out, ev.Index)
case "turn_ended":
- out := m.base(ev.TS, "session.ended", "session", schema.SeverityInfo, schema.FidelityObserved, "Grok session turn ended")
- if ev.Outcome != "" || ev.CancellationCategory != "" {
- out.Raw = map[string]interface{}{"grok": map[string]interface{}{"outcome": ev.Outcome, "cancellation_category": ev.CancellationCategory}}
+ out := m.base(ev.TS, "session.context", "session", schema.SeverityInfo, schema.FidelityObserved, "Grok session turn ended")
+ raw := map[string]interface{}{"turn_number": ev.TurnNumber}
+ if ev.Outcome != "" {
+ raw["outcome"] = ev.Outcome
+ }
+ if ev.CancellationCategory != "" {
+ raw["cancellation_category"] = ev.CancellationCategory
}
+ out.Raw = map[string]interface{}{"grok": raw}
m.append(out, ev.Index)
}
}
func (m *mapper) emitLifecycleStart() {
+ if m.opts.SkipSessionStarted {
+ return
+ }
ts := ""
for _, ev := range m.data.Lifecycle {
if ev.Type == "turn_started" {