The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
add openclaw session sync
Justin D'Souza05c59cacfa0b
diff --git a/cli/beacon/cmd/endpoint_openclaw_sessions.go b/cli/beacon/cmd/endpoint_openclaw_sessions.gonew file mode 100644index 00000000..6ba3074b--- /dev/null+++ b/cli/beacon/cmd/endpoint_openclaw_sessions.go@@ -0,0 +1,223 @@+package cmd++import (+ "context"+ "encoding/json"+ "fmt"+ "path/filepath"+ "strings"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/openclawsession"+ "github.com/spf13/cobra"+)++var endpointOpenClawSessionsCmd = &cobra.Command{+ Use: "sessions",+ Short: "Collect telemetry from OpenClaw committed session files",+ Long: `Collect endpoint telemetry from OpenClaw Gateway's local session store.++Beacon's normal OpenClaw support uses a managed plugin for live agent activity+and OpenClaw's diagnostics-otel plugin for gateway traces. This command reads+OpenClaw's committed JSONL session files under ~/.openclaw/agents/*/sessions+so historical sessions and sessions that ran without Beacon's plugin can still+be represented in the local endpoint log.++Every emitted event is marked harness.collection_method=poll: Beacon sees what+OpenClaw already wrote, so nothing here can hold or deny a tool call.`,+}++var endpointOpenClawSessionsSyncCmd = &cobra.Command{+ Use: "sync",+ Short: "Read new OpenClaw session records into the runtime log",+ SilenceUsage: true,+ RunE: runEndpointOpenClawSessionsSync,+}++var endpointOpenClawSessionsStatusCmd = &cobra.Command{+ Use: "status",+ Short: "Show OpenClaw session files and collection progress",+ SilenceUsage: true,+ RunE: runEndpointOpenClawSessionsStatus,+}++var endpointOpenClawSessionsOpts struct {+ openClawDir string+ statePath string+ logPath string+ print bool+ watch bool+ interval time.Duration+}++func init() {+ endpointOpenClawCmd.AddCommand(endpointOpenClawSessionsCmd)+ endpointOpenClawSessionsCmd.AddCommand(endpointOpenClawSessionsSyncCmd)+ endpointOpenClawSessionsCmd.AddCommand(endpointOpenClawSessionsStatusCmd)++ for _, c := range []*cobra.Command{endpointOpenClawSessionsSyncCmd, endpointOpenClawSessionsStatusCmd} {+ f := c.Flags()+ f.StringVar(&endpointOpenClawSessionsOpts.openClawDir, "openclaw-dir", "", "OpenClaw state directory (default $OPENCLAW_STATE_DIR or ~/.openclaw)")+ f.StringVar(&endpointOpenClawSessionsOpts.statePath, "state", "", "Collector cursor file (default ~/.beacon/endpoint/state/openclaw-sessions.json)")+ f.BoolVar(&endpointOpts.jsonOutput, "json", false, "Print the result as JSON")+ f.BoolVar(&endpointOpts.userMode, "user", true, "Use per-user endpoint paths")+ f.BoolVar(&endpointOpts.systemMode, "system", false, "Use system endpoint paths")+ }++ sync := endpointOpenClawSessionsSyncCmd.Flags()+ sync.StringVar(&endpointOpenClawSessionsOpts.logPath, "log-path", "", "Runtime JSONL log path (default resolved endpoint log)")+ sync.BoolVar(&endpointOpenClawSessionsOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")+ sync.BoolVar(&endpointOpenClawSessionsOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")+ sync.DurationVar(&endpointOpenClawSessionsOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+}++func runEndpointOpenClawSessionsSync(cmd *cobra.Command, args []string) error {+ userMode := endpointUserMode()+ opts := openclawsession.CollectOptions{+ OpenClawDir: endpointOpenClawSessionsOpts.openClawDir,+ Print: endpointOpenClawSessionsOpts.print,+ Out: cmd.OutOrStdout(),+ Write: !endpointOpenClawSessionsOpts.print,+ UserMode: userMode,+ }+ if !endpointOpenClawSessionsOpts.print {+ opts.StatePath = resolveOpenClawSessionsStatePath(endpointOpenClawSessionsOpts.statePath, userMode)+ opts.LogPath = lifecycle.ResolveRuntimeLog(userMode, endpointOpenClawSessionsOpts.logPath).EffectiveLogPath+ }+ ctx := cmd.Context()+ if ctx == nil {+ ctx = context.Background()+ }+ if endpointOpenClawSessionsOpts.watch && !endpointOpenClawSessionsOpts.print {+ return watchOpenClawSessions(ctx, cmd, opts)+ }+ summary, err := openclawsession.CollectOnce(opts)+ reportOpenClawSessionsSweep(cmd, summary)+ return err+}++func watchOpenClawSessions(ctx context.Context, cmd *cobra.Command, opts openclawsession.CollectOptions) error {+ interval := endpointOpenClawSessionsOpts.interval+ if interval < 5*time.Second {+ interval = 5 * time.Second+ }+ for {+ summary, err := openclawsession.CollectOnce(opts)+ if err != nil {+ fmt.Fprintf(cmd.ErrOrStderr(), "openclaw sessions sync error: %v\n", err)+ }+ reportOpenClawSessionsSweep(cmd, summary)+ select {+ case <-ctx.Done():+ return nil+ case <-time.After(interval):+ }+ }+}++func reportOpenClawSessionsSweep(cmd *cobra.Command, summary openclawsession.Summary) {+ if endpointOpenClawSessionsOpts.print {+ return+ }+ if endpointOpts.jsonOutput {+ _ = json.NewEncoder(cmd.OutOrStdout()).Encode(summary)+ return+ }+ fmt.Fprintf(cmd.OutOrStdout(), "openclaw sessions sync: %d traces, %d changed, %d events, %d errors\n",+ summary.Traces, summary.TracesChanged, summary.EventsEmitted, summary.Errors)+}++type openClawSessionStatusRef struct {+ ID string `json:"id"`+ Profile string `json:"profile"`+ Workspace string `json:"workspace,omitempty"`+ UpdatedAt string `json:"updated_at,omitempty"`+ Collected bool `json:"collected"`+ EventsRead int `json:"events_read"`+ SizeBytes int64 `json:"size_bytes"`+ SourcePath string `json:"source_path,omitempty"`+}++type openClawSessionsStatusReport struct {+ OpenClawDir string `json:"openclaw_dir"`+ Present bool `json:"present"`+ StatePath string `json:"state_path,omitempty"`+ Traces []openClawSessionStatusRef `json:"traces"`+}++func runEndpointOpenClawSessionsStatus(cmd *cobra.Command, args []string) error {+ store, err := openclawsession.NewStore(endpointOpenClawSessionsOpts.openClawDir)+ if err != nil {+ return err+ }+ statePath := resolveOpenClawSessionsStatePath(endpointOpenClawSessionsOpts.statePath, endpointUserMode())+ state, err := openclawsession.LoadState(statePath)+ if err != nil {+ return err+ }+ refs, err := store.List()+ if err != nil {+ return err+ }+ report := openClawSessionsStatusReport{+ OpenClawDir: store.OpenClawDir,+ Present: store.Exists(),+ StatePath: statePath,+ Traces: make([]openClawSessionStatusRef, 0, len(refs)),+ }+ for _, ref := range refs {+ cursor := state.Traces[ref.Profile+":"+ref.ID+":"+ref.SourcePath]+ status := openClawSessionStatusRef{+ ID: ref.ID,+ Profile: ref.Profile,+ Workspace: ref.Directory,+ Collected: cursor != nil && cursor.UpdatedAtMS >= ref.UpdatedAtUnixMS,+ SizeBytes: ref.SizeBytes,+ SourcePath: ref.SourcePath,+ }+ if ref.UpdatedAtUnixMS > 0 {+ status.UpdatedAt = time.UnixMilli(ref.UpdatedAtUnixMS).UTC().Format(time.RFC3339)+ }+ if cursor != nil {+ status.EventsRead = cursor.LastOrder+ }+ report.Traces = append(report.Traces, status)+ }+ if endpointOpts.jsonOutput {+ return json.NewEncoder(cmd.OutOrStdout()).Encode(report)+ }+ if !report.Present {+ fmt.Fprintf(cmd.OutOrStdout(), "OpenClaw session store not found at %s\n", report.OpenClawDir)+ return nil+ }+ fmt.Fprintf(cmd.OutOrStdout(), "OpenClaw session store: %s\n", report.OpenClawDir)+ fmt.Fprintf(cmd.OutOrStdout(), "State: %s\n", report.StatePath)+ for _, trace := range report.Traces {+ collected := "pending"+ if trace.Collected {+ collected = "collected"+ }+ updated := trace.UpdatedAt+ if updated == "" {+ updated = "unknown"+ }+ fmt.Fprintf(cmd.OutOrStdout(), " %s [%s] %s events=%d updated=%s", trace.ID, trace.Profile, collected, trace.EventsRead, updated)+ if trace.Workspace != "" {+ fmt.Fprintf(cmd.OutOrStdout(), " workspace=%s", trace.Workspace)+ }+ fmt.Fprintln(cmd.OutOrStdout())+ }+ return nil+}++func resolveOpenClawSessionsStatePath(path string, userMode bool) string {+ if strings.TrimSpace(path) != "" {+ return path+ }+ base := filepath.Join("/var", "lib", "beacon", "endpoint", "state")+ if userMode {+ base = filepath.Dir(openclawsession.DefaultStatePath())+ }+ return filepath.Join(base, "openclaw-sessions.json")+}diff --git a/cli/beacon/internal/openclawsession/collect.go b/cli/beacon/internal/openclawsession/collect.gonew file mode 100644index 00000000..34e19e8e--- /dev/null+++ b/cli/beacon/internal/openclawsession/collect.go@@ -0,0 +1,196 @@+package openclawsession++import (+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "os"+ "path/filepath"+ "strings"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+)++const StateVersion = 1++type Cursor struct {+ LastOrder int `json:"last_order"`+ UpdatedAtMS int64 `json:"updated_at_ms,omitempty"`+}++type State struct {+ Version int `json:"version"`+ Traces map[string]*Cursor `json:"traces"`+}++func LoadState(path string) (*State, error) {+ state := &State{Version: StateVersion, Traces: map[string]*Cursor{}}+ if strings.TrimSpace(path) == "" {+ return state, nil+ }+ data, err := os.ReadFile(path)+ if err != nil {+ if errors.Is(err, os.ErrNotExist) {+ return state, nil+ }+ return nil, err+ }+ var stored State+ if err := json.Unmarshal(data, &stored); err != nil {+ return nil, fmt.Errorf("read OpenClaw collector state %s: %w", path, err)+ }+ if stored.Version != StateVersion || stored.Traces == nil {+ return state, nil+ }+ return &stored, nil+}++func (s *State) Save(path string) error {+ if strings.TrimSpace(path) == "" {+ return nil+ }+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {+ return err+ }+ s.Version = StateVersion+ data, err := json.MarshalIndent(s, "", " ")+ if err != nil {+ return err+ }+ tmp := path + ".tmp"+ if err := os.WriteFile(tmp, data, 0o600); err != nil {+ return err+ }+ return os.Rename(tmp, path)+}++func (s *State) cursor(ref TraceRef) *Cursor {+ if s.Traces == nil {+ s.Traces = map[string]*Cursor{}+ }+ key := stateKey(ref)+ cursor := s.Traces[key]+ if cursor == nil {+ cursor = &Cursor{}+ s.Traces[key] = cursor+ }+ return cursor+}++func stateKey(ref TraceRef) string {+ return ref.Profile + ":" + ref.ID + ":" + ref.SourcePath+}++func DefaultStatePath() string {+ base := filepath.Join(os.TempDir(), "beacon")+ if home, err := os.UserHomeDir(); err == nil && strings.TrimSpace(home) != "" {+ base = filepath.Join(home, ".beacon")+ }+ return filepath.Join(base, "endpoint", "state", "openclaw-sessions.json")+}++type CollectOptions struct {+ OpenClawDir string+ StatePath string+ Write bool+ LogPath string+ UserMode bool+ Print bool+ Out io.Writer+}++type Summary struct {+ Traces int `json:"traces"`+ TracesChanged int `json:"traces_changed"`+ EventsEmitted int `json:"events_emitted"`+ Errors int `json:"errors"`+}++func CollectOnce(opts CollectOptions) (summary Summary, err error) {+ store, err := NewStore(opts.OpenClawDir)+ if err != nil {+ return summary, err+ }+ refs, err := store.List()+ if err != nil {+ return summary, err+ }+ summary.Traces = len(refs)+ state, err := LoadState(opts.StatePath)+ if err != nil {+ return summary, err+ }+ defer func() {+ if saveErr := state.Save(opts.StatePath); saveErr != nil && err == nil {+ err = fmt.Errorf("save OpenClaw collector state: %w", saveErr)+ }+ }()++ var errs []error+ for _, ref := range refs {+ changed, collectErr := collectTrace(store, ref, state, opts, &summary)+ if collectErr != nil {+ summary.Errors+++ errs = append(errs, fmt.Errorf("OpenClaw trace %s: %w", ref.ID, collectErr))+ continue+ }+ if changed {+ summary.TracesChanged+++ }+ }+ if len(errs) > 0 {+ return summary, errors.Join(errs...)+ }+ return summary, nil+}++func collectTrace(store *Store, ref TraceRef, state *State, opts CollectOptions, summary *Summary) (bool, error) {+ cursor := state.cursor(ref)+ if ref.UpdatedAtUnixMS > 0 && ref.UpdatedAtUnixMS <= cursor.UpdatedAtMS {+ return false, nil+ }+ records, err := store.Read(ref)+ if err != nil {+ return false, err+ }+ mapped := MapTrace(ref, records, MapOptions{MinOrder: cursor.LastOrder})+ if len(mapped) == 0 {+ if len(records) > 0 && records[len(records)-1].Order > cursor.LastOrder {+ cursor.LastOrder = records[len(records)-1].Order+ }+ cursor.UpdatedAtMS = ref.UpdatedAtUnixMS+ return false, nil+ }+ for i, item := range mapped {+ if err := emit(item.Event, opts); err != nil {+ if i > 0 {+ cursor.LastOrder = mapped[i-1].SourceOrder+ }+ return true, err+ }+ cursor.LastOrder = item.SourceOrder+ summary.EventsEmitted+++ }+ cursor.UpdatedAtMS = ref.UpdatedAtUnixMS+ return true, nil+}++func emit(event schema.Event, opts CollectOptions) error {+ if opts.Print && opts.Out != nil {+ data, err := json.Marshal(event)+ if err != nil {+ return err+ }+ if _, err := opts.Out.Write(append(data, '\n')); err != nil {+ return err+ }+ }+ if opts.Write {+ if _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode}); err != nil {+ return err+ }+ }+ return nil+}diff --git a/cli/beacon/internal/openclawsession/collect_test.go b/cli/beacon/internal/openclawsession/collect_test.gonew file mode 100644index 00000000..5a779557--- /dev/null+++ b/cli/beacon/internal/openclawsession/collect_test.go@@ -0,0 +1,151 @@+package openclawsession++import (+ "encoding/json"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+)++func TestStoreReadsIndexedAndFlatScannedSessions(t *testing.T) {+ root := t.TempDir()+ sessions := filepath.Join(root, "agents", "main", "sessions")+ if err := os.MkdirAll(sessions, 0o755); err != nil {+ t.Fatal(err)+ }+ writeFile(t, filepath.Join(sessions, "indexed.jsonl"), openClawLines("indexed", "/repo/indexed")...)+ writeFile(t, filepath.Join(sessions, "loose.jsonl"), openClawLines("loose", "/repo/loose")...)+ writeFile(t, filepath.Join(sessions, "sessions.json"), `{+ "indexed": {+ "sessionId": "indexed",+ "sessionFile": "indexed.jsonl",+ "updatedAt": "2026-09-19T23:00:00Z",+ "workspaceDir": "/repo/indexed"+ }+}`)++ store, err := NewStore(root)+ if err != nil {+ t.Fatal(err)+ }+ refs, err := store.List()+ if err != nil {+ t.Fatal(err)+ }+ if len(refs) != 2 {+ t.Fatalf("refs = %d, want 2: %+v", len(refs), refs)+ }+ seen := map[string]bool{}+ for _, ref := range refs {+ seen[ref.ID] = true+ if ref.Profile != "main" {+ t.Errorf("profile = %q, want main", ref.Profile)+ }+ }+ if !seen["indexed"] || !seen["loose"] {+ t.Fatalf("missing refs: %+v", seen)+ }+}++func TestCollectOnceWritesOpenClawSessionEventsAndCursor(t *testing.T) {+ dir := t.TempDir()+ sessions := filepath.Join(dir, ".openclaw", "agents", "main", "sessions")+ logPath := filepath.Join(dir, "runtime.jsonl")+ statePath := filepath.Join(dir, "state.json")+ if err := os.MkdirAll(sessions, 0o755); err != nil {+ t.Fatal(err)+ }+ writeFile(t, filepath.Join(sessions, "s1.jsonl"), openClawLines("s1", "/repo")...)++ opts := CollectOptions{+ OpenClawDir: filepath.Join(dir, ".openclaw"),+ StatePath: statePath,+ Write: true,+ LogPath: logPath,+ UserMode: true,+ }+ summary, err := CollectOnce(opts)+ if err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ if summary.Traces != 1 || summary.TracesChanged != 1 || summary.EventsEmitted == 0 {+ t.Fatalf("summary = %+v", summary)+ }+ events := readEvents(t, logPath)+ if len(events) != summary.EventsEmitted {+ t.Fatalf("events = %d, summary emitted %d", len(events), summary.EventsEmitted)+ }+ var sawPrompt, sawCommand, sawUsage bool+ for _, event := range events {+ if event.Harness.Name != Harness || event.Harness.CollectionMethod != schema.CollectionMethodPoll {+ t.Fatalf("bad provenance on %s: %+v", event.Event.Action, event.Harness)+ }+ if event.Event.ID == "" {+ t.Fatalf("%s has no deterministic event id", event.Event.Action)+ }+ switch event.Event.Action {+ case "prompt.submitted":+ sawPrompt = event.Prompt != nil && strings.Contains(event.Prompt.Text, "summarize")+ case "command.executed":+ sawCommand = event.Command != nil && event.Command.Command == "echo hi" && event.Command.ExitCode != nil && *event.Command.ExitCode == 0+ case "token.usage":+ sawUsage = event.GenAI != nil && event.GenAI.Usage != nil && event.GenAI.Usage.InputTokens != nil && *event.GenAI.Usage.InputTokens == 12+ }+ }+ if !sawPrompt || !sawCommand || !sawUsage {+ t.Fatalf("saw prompt=%t command=%t usage=%t in %+v", sawPrompt, sawCommand, sawUsage, events)+ }++ second, err := CollectOnce(opts)+ if err != nil {+ t.Fatalf("second CollectOnce: %v", err)+ }+ if second.EventsEmitted != 0 {+ t.Fatalf("second emitted %d events, want 0", second.EventsEmitted)+ }+}++func openClawLines(id, cwd string) []string {+ return []string{+ `{"type":"session","id":"` + id + `","timestamp":"2026-09-19T23:00:00Z","cwd":"` + cwd + `"}`,+ `{"type":"message","id":"u1","timestamp":"2026-09-19T23:00:01Z","message":{"role":"user","content":[{"type":"text","text":"summarize the repo"}]}}`,+ `{"type":"message","id":"a1","timestamp":"2026-09-19T23:00:02Z","message":{"role":"assistant","modelId":"openai/gpt-5","content":[{"type":"tool_call","id":"call_1","tool_name":"exec","input":{"command":"echo hi"}},{"type":"tool_result","call_id":"call_1","tool_name":"exec","output":{"output":"hi\n","exitCode":0}},{"type":"text","text":"Done."}],"usage":{"input":12,"output":5,"cacheRead":2}}}`,+ }+}++func writeFile(t *testing.T, path string, lines ...string) {+ t.Helper()+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {+ t.Fatal(err)+ }+ data := strings.Join(lines, "\n")+ if len(lines) > 1 {+ data += "\n"+ }+ if err := os.WriteFile(path, []byte(data), 0o644); err != nil {+ t.Fatal(err)+ }+}++func readEvents(t *testing.T, path string) []schema.Event {+ t.Helper()+ data, err := os.ReadFile(path)+ if err != nil {+ t.Fatal(err)+ }+ var events []schema.Event+ for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") {+ if line == "" {+ continue+ }+ var event schema.Event+ if err := json.Unmarshal([]byte(line), &event); err != nil {+ t.Fatalf("decode event: %v\n%s", err, line)+ }+ events = append(events, event)+ }+ return events+}diff --git a/cli/beacon/internal/openclawsession/mapper.go b/cli/beacon/internal/openclawsession/mapper.gonew file mode 100644index 00000000..65cfdbcd--- /dev/null+++ b/cli/beacon/internal/openclawsession/mapper.go@@ -0,0 +1,440 @@+package openclawsession++import (+ "crypto/sha1"+ "crypto/sha256"+ "encoding/hex"+ "encoding/json"+ "fmt"+ "io"+ "path/filepath"+ "strings"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"+)++type MappedEvent struct {+ DedupID string+ SourceOrder int+ Event schema.Event+}++type MapOptions struct {+ MinOrder int+}++func MapTrace(ref TraceRef, records []Record, opts MapOptions) []MappedEvent {+ m := &mapper{ref: ref, opts: opts}+ for _, record := range records {+ m.consume(record)+ }+ return m.out+}++type mapper struct {+ ref TraceRef+ opts MapOptions+ out []MappedEvent+}++func (m *mapper) consume(record Record) {+ if record.Order <= m.opts.MinOrder {+ return+ }+ switch record.Type {+ case "session_started":+ m.emitSessionStarted(record)+ case "user_message":+ m.emitPrompt(record)+ case "assistant_text":+ m.emitAgentMessage(record)+ case "agent_reasoning":+ m.emitReasoning(record)+ case "tool_call":+ m.emitToolCall(record)+ case "tool_result":+ m.emitToolResult(record)+ case "context_injection":+ m.emitContext(record)+ case "error":+ m.emitError(record)+ case "token_usage":+ m.emitUsage(record)+ }+}++func (m *mapper) base(record Record, action, category string, severity schema.Severity, message string) schema.Event {+ ev := schema.NewEvent(schema.NewEventOptions{+ Action: action,+ Category: category,+ Severity: severity,+ Fidelity: schema.FidelityObserved,+ Message: message,+ Origin: schema.OriginLocal,+ Harness: schema.HarnessInfo{Name: Harness, CollectionMethod: schema.CollectionMethodPoll},+ })+ if record.TimestampMS > 0 {+ ev.Timestamp = schema.FormatTimestamp(time.UnixMilli(record.TimestampMS))+ } else if m.ref.UpdatedAtUnixMS > 0 {+ ev.Timestamp = schema.FormatTimestamp(time.UnixMilli(m.ref.UpdatedAtUnixMS))+ }+ ev.Session = &schema.SessionInfo{ID: m.ref.ID, WorkingDirectory: m.ref.Directory}+ if record.ModelID != "" {+ ev.Model = asymptoteobserve.NormalizeModelName(record.ModelID)+ }+ ev.Raw = mergeRaw(ev.Raw, map[string]any{"profile": m.ref.Profile, "source_path": m.ref.SourcePath})+ return ev+}++func (m *mapper) emitSessionStarted(record Record) {+ ev := m.base(record, "session.started", "session", schema.SeverityInfo, "OpenClaw session started")+ ev.Raw = mergeRaw(ev.Raw, record.Raw)+ m.append(record, "session.started", ev)+}++func (m *mapper) emitPrompt(record Record) {+ text := strings.TrimSpace(record.Content)+ if text == "" {+ return+ }+ ev := m.base(record, "prompt.submitted", "prompt", schema.SeverityInfo, "OpenClaw prompt submitted")+ ev.Prompt = &schema.PromptInfo{Text: text}+ ev.Content = asymptoteobserve.RetainedContent(text, asymptoteobserve.DefaultStringLimit)+ ev.GenAI = &schema.GenAIInfo{Input: &schema.GenAIInputInfo{Messages: asymptoteobserve.TextInputMessages(text)}}+ ev.Raw = mergeRaw(ev.Raw, record.Raw)+ m.append(record, "prompt", ev)+}++func (m *mapper) emitAgentMessage(record Record) {+ text := strings.TrimSpace(record.Content)+ if text == "" {+ return+ }+ ev := m.base(record, "agent.message", "session", schema.SeverityInfo, "OpenClaw agent message")+ ev.GenAI = &schema.GenAIInfo{Output: &schema.GenAIOutputInfo{Messages: asymptoteobserve.TextOutputMessages(text)}}+ ev.Content = asymptoteobserve.RetainedContent(text, asymptoteobserve.DefaultRawStringLimit)+ ev.Raw = mergeRaw(ev.Raw, record.Raw)+ m.append(record, "assistant", ev)+}++func (m *mapper) emitReasoning(record Record) {+ text := strings.TrimSpace(record.Content)+ if text == "" {+ return+ }+ ev := m.base(record, "agent.reasoning", "session", schema.SeverityInfo, "OpenClaw agent reasoning")+ ev.GenAI = &schema.GenAIInfo{Output: &schema.GenAIOutputInfo{Messages: []any{map[string]any{+ "role": "assistant",+ "parts": []any{map[string]any{"type": "reasoning", "content": text}},+ }}}}+ ev.Content = asymptoteobserve.RetainedContent(text, asymptoteobserve.DefaultRawStringLimit)+ ev.Raw = mergeRaw(ev.Raw, record.Raw)+ m.append(record, "reasoning", ev)+}++func (m *mapper) emitToolCall(record Record) {+ name := firstNonEmpty(record.ToolName, "unknown")+ ev := m.base(record, "tool.invoked", "tool", schema.SeverityInfo, "OpenClaw tool invoked")+ ev.Tool = &schema.ToolInfo{Name: name}+ applyToolCall(&ev, name, record)+ ev.Raw = mergeRaw(ev.Raw, record.Raw)+ m.append(record, "tool.call", ev)+}++func (m *mapper) emitToolResult(record Record) {+ name := firstNonEmpty(record.ToolName, "unknown")+ action, category, severity, message := classifyTool(name, record)+ ev := m.base(record, action, category, severity, message)+ ev.Tool = &schema.ToolInfo{Name: name}+ applyToolCall(&ev, name, record)+ switch category {+ case "command":+ applyCommand(&ev, record)+ case "file":+ applyFile(&ev, name, record)+ case "mcp":+ server, tool := openClawMCPServerTool(name)+ ev.MCP = &schema.MCPInfo{Server: server, Tool: firstNonEmpty(tool, name)}+ }+ if record.Status == "error" || record.Status == "failed" || record.Status == "failure" {+ ev.Error = &schema.ErrorInfo{Type: "tool_error"}+ }+ ev.Raw = mergeRaw(ev.Raw, record.Raw)+ m.append(record, "tool.result", ev)+}++func (m *mapper) emitContext(record Record) {+ ev := m.base(record, "session.context", "session", schema.SeverityInfo, "OpenClaw runtime context injected")+ ev.Raw = mergeRaw(ev.Raw, record.Raw)+ m.append(record, "context", ev)+}++func (m *mapper) emitError(record Record) {+ ev := m.base(record, "session.error", "session", schema.SeverityHigh, "OpenClaw session error")+ ev.Error = &schema.ErrorInfo{Type: firstNonEmpty(record.Content, "session_error")}+ ev.Raw = mergeRaw(ev.Raw, record.Raw)+ m.append(record, "error", ev)+}++func (m *mapper) emitUsage(record Record) {+ if record.Tokens == nil {+ return+ }+ usage := &schema.GenAIUsageInfo{}+ if record.Tokens.Input > 0 {+ v := record.Tokens.Input+ usage.InputTokens = &v+ }+ if record.Tokens.Output > 0 {+ v := record.Tokens.Output+ usage.OutputTokens = &v+ }+ if record.Tokens.CacheRead > 0 {+ v := record.Tokens.CacheRead+ usage.CacheRead = &schema.GenAIUsageCacheReadInfo{InputTokens: &v}+ }+ if record.Tokens.CacheWrite > 0 {+ v := record.Tokens.CacheWrite+ usage.CacheCreation = &schema.GenAIUsageCacheCreationInfo{InputTokens: &v}+ }+ reasoning := firstNonZero(record.Tokens.Reasoning, record.Tokens.ReasoningTokens)+ if reasoning > 0 {+ v := reasoning+ usage.Reasoning = &schema.GenAIUsageReasoningInfo{OutputTokens: &v}+ }+ if usage.InputTokens == nil && usage.OutputTokens == nil && usage.CacheRead == nil && usage.CacheCreation == nil && usage.Reasoning == nil {+ return+ }+ ev := m.base(record, "token.usage", "metric", schema.SeverityInfo, "OpenClaw token usage")+ ev.GenAI = &schema.GenAIInfo{Usage: usage}+ ev.Raw = mergeRaw(ev.Raw, record.Raw)+ m.append(record, "usage", ev)+}++func classifyTool(name string, record Record) (action, category string, severity schema.Severity, message string) {+ lower := strings.ToLower(strings.TrimSpace(name))+ switch {+ case isOpenClawMCPTool(lower):+ return "mcp.tool_invoked", "mcp", schema.SeverityInfo, "OpenClaw MCP tool invoked"+ case lower == "exec" || toolNameHasToken(lower, "bash") || toolNameHasToken(lower, "shell"):+ return "command.executed", "command", schema.SeverityInfo, "OpenClaw command executed"+ case lower == "read" || lower == "grep" || lower == "find" || lower == "ls":+ return "file.read", "file", schema.SeverityInfo, "OpenClaw file read"+ case lower == "write":+ return "file.created", "file", schema.SeverityInfo, "OpenClaw file created"+ case lower == "edit" || lower == "apply_patch":+ return "file.modified", "file", schema.SeverityInfo, "OpenClaw file modified"+ case record.Status == "error" || record.Status == "failed" || record.Status == "failure":+ return "tool.failed", "tool", schema.SeverityHigh, "OpenClaw tool failed"+ default:+ return "tool.completed", "tool", schema.SeverityInfo, "OpenClaw tool completed"+ }+}++func applyToolCall(ev *schema.Event, name string, record Record) {+ call := &schema.GenAIToolCallInfo{ID: record.CallID}+ if len(record.Args) > 0 {+ call.Arguments = record.Args+ }+ if record.Output != nil && record.Type == "tool_result" {+ call.Result = record.Output+ }+ ev.GenAI = &schema.GenAIInfo{+ Operation: &schema.GenAIOperationInfo{Name: "execute_tool"},+ Tool: &schema.GenAIToolInfo{Name: name, Call: call},+ }+ if ev.Tool == nil {+ ev.Tool = &schema.ToolInfo{Name: name}+ }+ if path := argString(record.Args, "path", "file_path", "filePath", "target", "destination"); path != "" {+ ev.Tool.Path = path+ }+ if command := argString(record.Args, "command", "cmd"); command != "" {+ ev.Tool.Command = command+ }+}++func applyCommand(ev *schema.Event, record Record) {+ command := ""+ if ev.Tool != nil {+ command = ev.Tool.Command+ }+ info := &schema.CommandInfo{Command: command}+ if out := outputString(record.Output); out != "" {+ info.Output = out+ ev.Content = asymptoteobserve.RetainedContent(out, asymptoteobserve.DefaultStringLimit)+ }+ if code, ok := exitCode(record.Output); ok {+ info.ExitCode = &code+ }+ ev.Command = info+}++func applyFile(ev *schema.Event, name string, record Record) {+ path := ""+ if ev.Tool != nil {+ path = ev.Tool.Path+ }+ operation := "modify"+ switch ev.Event.Action {+ case "file.read":+ operation = "read"+ case "file.created":+ operation = "create"+ }+ file := &schema.FileInfo{Path: path, Operation: operation}+ if path != "" {+ file.Language = strings.TrimPrefix(filepath.Ext(path), ".")+ }+ if diff := diffText(name, record); diff != "" {+ file.Diff = diff+ file.DiffBytes = len(diff)+ file.DiffHash = sha256Hex(diff)+ ev.Content = asymptoteobserve.RetainedContent(diff, asymptoteobserve.DefaultStringLimit)+ }+ ev.File = file+}++func diffText(name string, record Record) string {+ if value := argString(record.Args, "diff", "patch", "input"); value != "" && strings.EqualFold(name, "apply_patch") {+ return value+ }+ if strings.EqualFold(name, "write") {+ return argString(record.Args, "content")+ }+ if strings.EqualFold(name, "edit") {+ old := argString(record.Args, "oldText", "old_string", "oldString")+ newText := argString(record.Args, "newText", "new_string", "newString")+ if old != "" || newText != "" {+ return "- " + old + "\n+ " + newText + "\n"+ }+ }+ return ""+}++func (m *mapper) append(record Record, suffix string, ev schema.Event) {+ dedupID := fmt.Sprintf("%s:%s:%d:%s:%s", m.ref.Profile, m.ref.ID, record.Order, record.NativeID, suffix)+ ev.Event.ID = openClawEventID(dedupID)+ m.out = append(m.out, MappedEvent{DedupID: dedupID, SourceOrder: record.Order, Event: ev})+}++var openClawIDNamespace = [16]byte{+ 0xb2, 0x10, 0x5c, 0xde, 0x15, 0x31, 0x42, 0x8f,+ 0x94, 0x4c, 0x70, 0xa4, 0xe1, 0x0f, 0xc8, 0x42,+}++func openClawEventID(dedupID string) string {+ digest := sha1.New()+ digest.Write(openClawIDNamespace[:])+ io.WriteString(digest, dedupID)+ var id [16]byte+ copy(id[:], digest.Sum(nil))+ id[6] = (id[6] & 0x0f) | 0x50+ id[8] = (id[8] & 0x3f) | 0x80+ out := make([]byte, 32)+ hex.Encode(out, id[:])+ return fmt.Sprintf("%s-%s-%s-%s-%s", out[0:8], out[8:12], out[12:16], out[16:20], out[20:32])+}++func isOpenClawMCPTool(name string) bool {+ server, tool := openClawMCPServerTool(name)+ return server != "" || tool != ""+}++func openClawMCPServerTool(toolName string) (string, string) {+ trimmed := strings.TrimSpace(toolName)+ if strings.HasPrefix(trimmed, "mcp__") {+ parts := strings.Split(trimmed, "__")+ if len(parts) >= 3 {+ return parts[1], strings.Join(parts[2:], "__")+ }+ }+ server, tool, ok := strings.Cut(trimmed, "__")+ if !ok || server == "" || tool == "" {+ return "", ""+ }+ return server, tool+}++func toolNameHasToken(name, token string) bool {+ for _, part := range strings.FieldsFunc(name, func(r rune) bool {+ return !(r >= 'a' && r <= 'z') && !(r >= '0' && r <= '9')+ }) {+ if part == token {+ return true+ }+ }+ return false+}++func mergeRaw(existing map[string]any, fields map[string]any) map[string]any {+ if len(fields) == 0 {+ return existing+ }+ if existing == nil {+ existing = map[string]any{}+ }+ nested, ok := existing["openclaw"].(map[string]any)+ if !ok {+ nested = map[string]any{}+ }+ for key, value := range fields {+ nested[key] = value+ }+ existing["openclaw"] = nested+ return existing+}++func argString(args map[string]any, keys ...string) string {+ for _, key := range keys {+ if value := textFromAny(args[key]); value != "" {+ return value+ }+ }+ return ""+}++func outputString(value any) string {+ if s := textFromAny(value); s != "" {+ return s+ }+ if data, err := json.Marshal(value); err == nil {+ return string(data)+ }+ return ""+}++func exitCode(value any) (int, bool) {+ m, ok := value.(map[string]any)+ if !ok {+ return 0, false+ }+ for _, key := range []string{"exitCode", "exit_code", "code"} {+ switch v := m[key].(type) {+ case float64:+ return int(v), true+ case int:+ return v, true+ }+ }+ if details, ok := m["details"].(map[string]any); ok {+ return exitCode(details)+ }+ return 0, false+}++func firstNonZero(values ...int64) int64 {+ for _, value := range values {+ if value != 0 {+ return value+ }+ }+ return 0+}++func sha256Hex(value string) string {+ sum := sha256.Sum256([]byte(value))+ return hex.EncodeToString(sum[:])+}diff --git a/cli/beacon/internal/openclawsession/records.go b/cli/beacon/internal/openclawsession/records.gonew file mode 100644index 00000000..8cd08976--- /dev/null+++ b/cli/beacon/internal/openclawsession/records.go@@ -0,0 +1,283 @@+package openclawsession++import (+ "encoding/json"+ "fmt"+ "strings"+)++func recordsFromEntries(entries []RawEntry, fallbackTimestamp int64) []Record {+ var records []Record+ order := 1+ for i, entry := range entries {+ kind := strings.ToLower(strings.TrimSpace(entry.Type))+ timestamp := parseTimestamp(entry.Timestamp, fallbackTimestamp)+ nativeID := firstNonEmpty(entry.ID, fmt.Sprintf("line-%d", i+1))+ switch kind {+ case "session", "session.started":+ records = append(records, Record{+ Order: order,+ NativeID: nativeID + ":session",+ TimestampMS: timestamp,+ Type: "session_started",+ Content: entry.CWD,+ ModelID: firstNonEmpty(entry.ModelID, entry.Model),+ Raw: map[string]any{"entry": entry.Raw},+ })+ order+++ case "message":+ if entry.Message == nil {+ continue+ }+ for _, rec := range recordsFromMessage(nativeID, timestamp, entry.Message, entry.Raw) {+ rec.Order = order+ records = append(records, rec)+ order+++ }+ case "model_change":+ // Model changes are context for later records; the session-store format also repeats+ // the model on messages, which is what mapper events use.+ case "custom_message":+ customType := strings.ToLower(strings.TrimSpace(entry.CustomType))+ if customType == "openclaw.runtime-context" {+ if text := textFromAny(entry.Content); text != "" && !strings.HasPrefix(text, "Read HEARTBEAT.md") {+ records = append(records, Record{+ Order: order,+ NativeID: nativeID + ":context",+ TimestampMS: timestamp,+ Type: "context_injection",+ Content: text,+ Raw: map[string]any{"entry": entry.Raw},+ })+ order+++ }+ }+ case "error":+ records = append(records, Record{+ Order: order,+ NativeID: nativeID + ":error",+ TimestampMS: timestamp,+ Type: "error",+ Content: firstNonEmpty(textFromAny(entry.Content), textFromAny(entry.Error)),+ Raw: map[string]any{"entry": entry.Raw},+ })+ order+++ }+ }+ return records+}++func recordsFromMessage(baseID string, timestamp int64, msg *Message, raw map[string]any) []Record {+ role := strings.ToLower(strings.TrimSpace(msg.Role))+ ts := parseTimestamp(msg.Timestamp, timestamp)+ model := firstNonEmpty(msg.ModelID, msg.Model)+ blocks := normalizeContentBlocks(msg.Content)+ var records []Record+ type callContext struct {+ name string+ args map[string]any+ }+ calls := map[string]callContext{}+ for i, block := range blocks {+ blockID := firstNonEmpty(block.ID, fmt.Sprintf("%d", i+1))+ blockType := strings.ToLower(strings.TrimSpace(block.Type))+ nativeID := baseID + ":" + blockID+ switch {+ case role == "user" && (blockType == "" || blockType == "text"):+ if text := firstNonEmpty(block.Text, textFromAny(block.Content)); text != "" {+ records = append(records, Record{NativeID: nativeID + ":user", TimestampMS: ts, Type: "user_message", Role: role, Content: cleanUserText(text), ModelID: model, Raw: map[string]any{"message": raw, "block": block.Raw}})+ }+ case role == "assistant" && (blockType == "" || blockType == "text"):+ if text := firstNonEmpty(block.Text, textFromAny(block.Content)); text != "" {+ records = append(records, Record{NativeID: nativeID + ":assistant", TimestampMS: ts, Type: "assistant_text", Role: role, Content: cleanAssistantText(text), ModelID: model, Raw: map[string]any{"message": raw, "block": block.Raw}})+ }+ case role == "assistant" && (blockType == "thinking" || blockType == "reasoning"):+ if text := firstNonEmpty(block.Text, textFromAny(block.Content)); text != "" {+ records = append(records, Record{NativeID: nativeID + ":reasoning", TimestampMS: ts, Type: "agent_reasoning", Role: role, Content: text, ModelID: model, Raw: map[string]any{"message": raw, "block": block.Raw}})+ }+ case isToolCallBlock(blockType):+ callID := firstNonEmpty(block.CallID, block.CallIDAlt, block.ID, blockID)+ name := firstNonEmpty(block.ToolName, block.ToolNameAlt, block.Tool, block.Name)+ args := parseArgs(firstPresent(block.Input, block.Args, block.Arguments, block.Content))+ if callID != "" {+ calls[callID] = callContext{name: name, args: args}+ }+ records = append(records, Record{+ NativeID: nativeID + ":tool-call",+ TimestampMS: ts,+ Type: "tool_call",+ Role: role,+ ModelID: model,+ ToolName: name,+ CallID: callID,+ Args: args,+ Status: "invoked",+ Raw: map[string]any{"message": raw, "block": block.Raw},+ })+ case isToolResultBlock(blockType):+ callID := firstNonEmpty(block.CallID, block.CallIDAlt, block.ID, blockID)+ name := firstNonEmpty(block.ToolName, block.ToolNameAlt, block.Tool, block.Name)+ args := map[string]any(nil)+ if ctx, ok := calls[callID]; ok {+ name = firstNonEmpty(name, ctx.name)+ args = ctx.args+ }+ status := strings.ToLower(firstNonEmpty(block.Status, "success"))+ if block.Error != nil && textFromAny(block.Error) != "" {+ status = "error"+ }+ records = append(records, Record{+ NativeID: nativeID + ":tool-result",+ TimestampMS: ts,+ Type: "tool_result",+ Role: role,+ ModelID: model,+ ToolName: name,+ CallID: callID,+ Args: args,+ Output: firstPresent(block.Output, block.Result, block.Content, block.Error),+ Status: status,+ Raw: map[string]any{"message": raw, "block": block.Raw},+ })+ }+ }+ if msg.Usage != nil && hasUsage(msg.Usage) {+ records = append(records, Record{+ NativeID: baseID + ":usage",+ TimestampMS: ts,+ Type: "token_usage",+ ModelID: model,+ Tokens: msg.Usage,+ Raw: map[string]any{"message": raw},+ })+ }+ return records+}++func normalizeContentBlocks(value any) []ContentBlock {+ if s, ok := value.(string); ok {+ return []ContentBlock{{Type: "text", Text: s}}+ }+ items, ok := value.([]any)+ if !ok {+ return nil+ }+ out := make([]ContentBlock, 0, len(items))+ for _, item := range items {+ if s, ok := item.(string); ok {+ out = append(out, ContentBlock{Type: "text", Text: s})+ continue+ }+ data, err := json.Marshal(item)+ if err != nil {+ continue+ }+ var raw map[string]any+ _ = json.Unmarshal(data, &raw)+ var block ContentBlock+ if err := json.Unmarshal(data, &block); err != nil {+ continue+ }+ block.Raw = raw+ out = append(out, block)+ }+ return out+}++func isToolCallBlock(kind string) bool {+ switch strings.ReplaceAll(kind, "-", "_") {+ case "toolcall", "tool_call", "tooluse", "tool_use", "function_call":+ return true+ default:+ return false+ }+}++func isToolResultBlock(kind string) bool {+ switch strings.ReplaceAll(kind, "-", "_") {+ case "toolresult", "tool_result", "tooloutput", "tool_output", "function_call_output":+ return true+ default:+ return false+ }+}++func hasUsage(usage *Usage) bool {+ return usage.Input > 0 || usage.Output > 0 || usage.CacheRead > 0 || usage.CacheWrite > 0 || usage.Reasoning > 0 || usage.ReasoningTokens > 0+}++func parseArgs(value any) map[string]any {+ switch v := value.(type) {+ case nil:+ return nil+ case map[string]any:+ return v+ case string:+ var out map[string]any+ if err := json.Unmarshal([]byte(v), &out); err == nil {+ return out+ }+ return map[string]any{"raw": v}+ default:+ return map[string]any{"raw": v}+ }+}++func textFromAny(value any) string {+ switch v := value.(type) {+ case string:+ return strings.TrimSpace(v)+ case nil:+ return ""+ default:+ data, err := json.Marshal(v)+ if err != nil {+ return fmt.Sprint(v)+ }+ return string(data)+ }+}++func extractText(value any) string {+ blocks := normalizeContentBlocks(value)+ for _, block := range blocks {+ if strings.ToLower(block.Type) == "text" || block.Type == "" {+ return firstNonEmpty(block.Text, textFromAny(block.Content))+ }+ }+ return textFromAny(value)+}++func directoryFromAny(value any) string {+ switch v := value.(type) {+ case map[string]any:+ for _, key := range []string{"workspaceDir", "workspace", "workspaceDir", "cwd", "workdir", "directory", "dir", "projectPath", "projectDir", "root", "rootDir"} {+ if s, ok := v[key].(string); ok && strings.TrimSpace(s) != "" {+ return s+ }+ }+ case string:+ var decoded map[string]any+ if err := json.Unmarshal([]byte(v), &decoded); err == nil {+ return directoryFromAny(decoded)+ }+ }+ return ""+}++func firstPresent(values ...any) any {+ for _, value := range values {+ if value != nil {+ return value+ }+ }+ return nil+}++func cleanUserText(text string) string {+ return strings.TrimSpace(text)+}++func cleanAssistantText(text string) string {+ return strings.TrimSpace(strings.TrimPrefix(text, "[[reply_to_current]]"))+}diff --git a/cli/beacon/internal/openclawsession/store.go b/cli/beacon/internal/openclawsession/store.gonew file mode 100644index 00000000..5e9200b4--- /dev/null+++ b/cli/beacon/internal/openclawsession/store.go@@ -0,0 +1,334 @@+package openclawsession++import (+ "bufio"+ "encoding/json"+ "errors"+ "fmt"+ "io/fs"+ "os"+ "path/filepath"+ "sort"+ "strings"+ "time"+)++const (+ maxSessionFilesScanned = 10000+ maxLineBytes = 4 << 20+)++type Store struct {+ OpenClawDir string+}++func NewStore(dir string) (*Store, error) {+ if strings.TrimSpace(dir) == "" {+ def, err := DefaultOpenClawDir()+ if err != nil {+ return nil, err+ }+ dir = def+ }+ return &Store{OpenClawDir: filepath.Clean(dir)}, nil+}++func DefaultOpenClawDir() (string, error) {+ if override := strings.TrimSpace(os.Getenv("OPENCLAW_STATE_DIR")); override != "" {+ return override, nil+ }+ home, err := os.UserHomeDir()+ if err != nil {+ return "", fmt.Errorf("resolve home directory: %w", err)+ }+ if strings.TrimSpace(home) == "" {+ return "", errors.New("home directory is empty")+ }+ return filepath.Join(home, ".openclaw"), nil+}++func (s *Store) Exists() bool {+ info, err := os.Stat(filepath.Join(s.OpenClawDir, "agents"))+ return err == nil && info.IsDir()+}++func (s *Store) List() ([]TraceRef, error) {+ roots, err := s.sessionRoots()+ if err != nil {+ return nil, err+ }+ seen := map[string]bool{}+ var refs []TraceRef+ for _, root := range roots {+ indexed := s.readSessionsIndex(root)+ for _, ref := range indexed {+ refs = append(refs, ref)+ seen[ref.SourcePath] = true+ }+ if len(indexed) > 0 && allRootFilesSeen(root, seen) {+ continue+ }+ for _, path := range collectSessionFilesFlat(root, max(2000, maxSessionFilesScanned/max(1, len(roots)))) {+ if seen[path] {+ continue+ }+ if ref, ok := s.readTraceMetadata(path, 0, ""); ok {+ refs = append(refs, ref)+ seen[path] = true+ }+ }+ }+ sort.SliceStable(refs, func(i, j int) bool {+ if refs[i].UpdatedAtUnixMS != refs[j].UpdatedAtUnixMS {+ return refs[i].UpdatedAtUnixMS < refs[j].UpdatedAtUnixMS+ }+ if refs[i].Profile != refs[j].Profile {+ return refs[i].Profile < refs[j].Profile+ }+ return refs[i].ID < refs[j].ID+ })+ return refs, nil+}++func (s *Store) Read(ref TraceRef) ([]Record, error) {+ entries, err := readEntries(ref.SourcePath)+ if err != nil {+ return nil, err+ }+ return recordsFromEntries(entries, ref.UpdatedAtUnixMS), nil+}++func (s *Store) sessionRoots() ([]string, error) {+ agentsDir := filepath.Join(s.OpenClawDir, "agents")+ entries, err := os.ReadDir(agentsDir)+ if err != nil {+ if errors.Is(err, fs.ErrNotExist) {+ return nil, nil+ }+ return nil, fmt.Errorf("read OpenClaw agents directory: %w", err)+ }+ var roots []string+ for _, entry := range entries {+ if !entry.IsDir() {+ continue+ }+ root := filepath.Join(agentsDir, entry.Name(), "sessions")+ if info, err := os.Stat(root); err == nil && info.IsDir() {+ roots = append(roots, root)+ }+ }+ sort.Strings(roots)+ return roots, nil+}++func (s *Store) readSessionsIndex(root string) []TraceRef {+ data, err := os.ReadFile(filepath.Join(root, "sessions.json"))+ if err != nil {+ return nil+ }+ var raw map[string]SessionIndexEntry+ if err := json.Unmarshal(data, &raw); err != nil {+ return nil+ }+ var refs []TraceRef+ for _, entry := range raw {+ sourcePath := resolveIndexedSourcePath(entry.SessionFile, root)+ if entry.SessionID == "" || sourcePath == "" {+ continue+ }+ if _, err := os.Stat(sourcePath); err != nil {+ continue+ }+ updated := parseTimestamp(entry.UpdatedAt, 0)+ dir := firstNonEmpty(entry.WorkspaceDir, directoryFromAny(entry.SystemPromptReport))+ if ref, ok := s.readTraceMetadata(sourcePath, updated, dir); ok {+ ref.Profile = profileFromSessionRoot(root)+ refs = append(refs, ref)+ }+ }+ return refs+}++func (s *Store) readTraceMetadata(path string, updatedHint int64, dirHint string) (TraceRef, bool) {+ info, err := os.Stat(path)+ if err != nil || info.IsDir() {+ return TraceRef{}, false+ }+ updated := updatedHint+ if updated <= 0 {+ updated = info.ModTime().UnixMilli()+ }+ entries, err := readEntries(path)+ if err != nil || len(entries) == 0 {+ return TraceRef{}, false+ }+ id := strings.TrimSuffix(filepath.Base(path), ".jsonl")+ title := ""+ dir := dirHint+ for _, entry := range entries {+ ts := parseTimestamp(entry.Timestamp, updated)+ if ts > updated {+ updated = ts+ }+ switch strings.ToLower(entry.Type) {+ case "session", "session.started":+ if entry.ID != "" {+ id = entry.ID+ }+ dir = chooseDirectory(dir, entry.CWD)+ case "model_change":+ // Model switches are mapped from the full record later. Metadata only needs the+ // session's identity, title and workspace.+ case "message":+ if title == "" && entry.Message != nil && strings.EqualFold(entry.Message.Role, "user") {+ title = normalizeTitle(extractText(entry.Message.Content))+ }+ }+ dir = chooseDirectory(dir, directoryFromAny(entry.Raw))+ }+ if title == "" {+ title = "(No preview)"+ }+ return TraceRef{+ ID: id,+ Profile: profileFromSourcePath(path),+ SourcePath: path,+ Title: title,+ Preview: title,+ Directory: dir,+ UpdatedAtUnixMS: updated,+ SizeBytes: info.Size(),+ }, true+}++func readEntries(path string) ([]RawEntry, error) {+ file, err := os.Open(path)+ if err != nil {+ return nil, err+ }+ defer file.Close()+ scanner := bufio.NewScanner(file)+ scanner.Buffer(make([]byte, 0, 64*1024), maxLineBytes)+ var out []RawEntry+ for scanner.Scan() {+ line := strings.TrimSpace(scanner.Text())+ if line == "" {+ continue+ }+ var raw map[string]any+ if err := json.Unmarshal([]byte(line), &raw); err != nil {+ continue+ }+ var entry RawEntry+ if err := json.Unmarshal([]byte(line), &entry); err != nil {+ continue+ }+ entry.Raw = raw+ out = append(out, entry)+ }+ return out, scanner.Err()+}++func collectSessionFilesFlat(root string, limit int) []string {+ entries, err := os.ReadDir(root)+ if err != nil {+ return nil+ }+ var out []string+ for _, entry := range entries {+ if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".jsonl") || entry.Name() == "config-audit.jsonl" {+ continue+ }+ out = append(out, filepath.Join(root, entry.Name()))+ if len(out) >= limit {+ break+ }+ }+ return out+}++func resolveIndexedSourcePath(path, root string) string {+ if strings.TrimSpace(path) == "" {+ return ""+ }+ if filepath.IsAbs(path) {+ return filepath.Clean(path)+ }+ return filepath.Join(root, path)+}++func allRootFilesSeen(root string, seen map[string]bool) bool {+ for _, path := range collectSessionFilesFlat(root, maxSessionFilesScanned) {+ if !seen[path] {+ return false+ }+ }+ return true+}++func profileFromSessionRoot(root string) string {+ if filepath.Base(root) != "sessions" {+ return "unknown"+ }+ return filepath.Base(filepath.Dir(root))+}++func profileFromSourcePath(path string) string {+ clean := filepath.Clean(path)+ parts := strings.Split(clean, string(filepath.Separator))+ for i := 0; i+2 < len(parts); i++ {+ if parts[i] == "agents" && parts[i+2] == "sessions" {+ return parts[i+1]+ }+ }+ return "unknown"+}++func parseTimestamp(value any, fallback int64) int64 {+ switch v := value.(type) {+ case float64:+ if v > 0 && v < 1000000000000 {+ return int64(v * 1000)+ }+ return int64(v)+ case int64:+ if v > 0 && v < 1000000000000 {+ return v * 1000+ }+ return v+ case string:+ if v == "" {+ return fallback+ }+ if t, err := time.Parse(time.RFC3339Nano, v); err == nil {+ return t.UnixMilli()+ }+ }+ return fallback+}++func normalizeTitle(text string) string {+ text = strings.TrimSpace(text)+ text = strings.TrimPrefix(text, "[[reply_to_current]]")+ text = strings.TrimSpace(text)+ if len(text) > 100 {+ return text[:100]+ }+ return text+}++func chooseDirectory(current, candidate string) string {+ if strings.TrimSpace(current) != "" {+ return current+ }+ return strings.TrimSpace(candidate)+}++func firstNonEmpty(values ...string) string {+ for _, value := range values {+ if strings.TrimSpace(value) != "" {+ return value+ }+ }+ return ""+}diff --git a/cli/beacon/internal/openclawsession/types.go b/cli/beacon/internal/openclawsession/types.gonew file mode 100644index 00000000..2f399dec--- /dev/null+++ b/cli/beacon/internal/openclawsession/types.go@@ -0,0 +1,107 @@+// Package openclawsession reads OpenClaw Gateway's committed session store and+// turns it into Beacon endpoint telemetry.+//+// This is deliberately separate from Beacon's managed OpenClaw plugin. The+// plugin observes live hooks; this package is a poll collector for historical+// or missed sessions under:+//+// ~/.openclaw/agents/<profile>/sessions/+//+// Each event it emits is marked harness.collection_method=poll because Beacon+// reads records OpenClaw already wrote rather than participating in the tool+// call.+package openclawsession++import "encoding/json"++const Harness = "openclaw_gateway"++type TraceRef struct {+ ID string+ Profile string+ SourcePath string+ Title string+ Preview string+ Directory string+ UpdatedAtUnixMS int64+ SizeBytes int64+}++type SessionIndexEntry struct {+ SessionID string `json:"sessionId"`+ SessionFile string `json:"sessionFile"`+ UpdatedAt any `json:"updatedAt"`+ WorkspaceDir string `json:"workspaceDir"`+ SystemPromptReport any `json:"systemPromptReport"`+}++type RawEntry struct {+ Type string `json:"type"`+ ID string `json:"id"`+ Timestamp any `json:"timestamp"`+ CWD string `json:"cwd"`+ ModelID string `json:"modelId"`+ Model string `json:"model"`+ CustomType string `json:"customType"`+ Content any `json:"content"`+ Data json.RawMessage `json:"data"`+ Message *Message `json:"message"`+ Error any `json:"error"`+ Raw map[string]any `json:"-"`+}++type Message struct {+ Role string `json:"role"`+ Timestamp any `json:"timestamp"`+ ModelID string `json:"modelId"`+ Model string `json:"model"`+ Content any `json:"content"`+ Usage *Usage `json:"usage"`+}++type ContentBlock struct {+ Type string `json:"type"`+ Text string `json:"text"`+ Content any `json:"content"`+ Name string `json:"name"`+ Tool string `json:"tool"`+ ToolName string `json:"toolName"`+ ToolNameAlt string `json:"tool_name"`+ ID string `json:"id"`+ CallID string `json:"callId"`+ CallIDAlt string `json:"call_id"`+ Input any `json:"input"`+ Args any `json:"args"`+ Arguments any `json:"arguments"`+ Output any `json:"output"`+ Result any `json:"result"`+ Status string `json:"status"`+ Error any `json:"error"`+ Raw map[string]any `json:"-"`+}++type Usage struct {+ Input int64 `json:"input"`+ Output int64 `json:"output"`+ CacheRead int64 `json:"cacheRead"`+ CacheWrite int64 `json:"cacheWrite"`+ Reasoning int64 `json:"reasoning"`+ ReasoningTokens int64 `json:"reasoningTokens"`+}++type Record struct {+ Order int+ NativeID string+ TimestampMS int64+ Type string+ Role string+ Content string+ ModelID string+ ToolName string+ CallID string+ Args map[string]any+ Output any+ Status string+ Tokens *Usage+ Raw map[string]any+}diff --git a/docs/runtimes/openclaw-gateway.mdx b/docs/runtimes/openclaw-gateway.mdxindex 25249859..b94d2d45 100644--- a/docs/runtimes/openclaw-gateway.mdx+++ b/docs/runtimes/openclaw-gateway.mdx@@ -5,16 +5,17 @@ description: "Asymptote support details for OpenClaw Gateway endpoint telemetry"## Runtime overview-[OpenClaw](https://docs.openclaw.ai) is a self-hosted gateway that connects chat apps — Discord, Slack, Signal, Telegram, WhatsApp, iMessage, Matrix, Microsoft Teams and more — to AI coding agents. Asymptote supports it through **two** collection paths, and both are wanted on the same machine:+[OpenClaw](https://docs.openclaw.ai) is a self-hosted gateway that connects chat apps — Discord, Slack, Signal, Telegram, WhatsApp, iMessage, Matrix, Microsoft Teams and more — to AI coding agents. Asymptote supports it through **three** collection paths. The first two are live collection paths and both are wanted on the same machine; the third is an offline recovery path for historical or missed sessions:```bashbeacon endpoint hooks install --harness openclaw # the Beacon-managed pluginbeacon endpoint integrations openclaw print-config # OpenClaw's own OTLP export+beacon endpoint integrations openclaw sessions sync # OpenClaw's committed session files````--harness openclaw-gateway` is also accepted and installs the same thing.-The two are not alternatives. The plugin reports what the **agent** did — sessions, prompts, tool calls, commands, files, MCP activity and token usage. OpenClaw's own `diagnostics-otel` export reports what the **gateway** did, as traces and metrics, and by OpenClaw's default carries no prompt, tool, or system-prompt content at all unless `captureContent` is explicitly enabled.+The live paths are not alternatives. The plugin reports what the **agent** did — sessions, prompts, tool calls, commands, files, MCP activity and token usage. OpenClaw's own `diagnostics-otel` export reports what the **gateway** did, as traces and metrics, and by OpenClaw's default carries no prompt, tool, or system-prompt content at all unless `captureContent` is explicitly enabled. The session-file sync reads what OpenClaw already committed to disk and marks every event `harness.collection_method=poll`, so it can backfill but cannot approve, deny, or delay a tool call.<Note>**A gateway is not a single-user CLI, and the telemetry reflects that.** One OpenClaw process fans several people's chat accounts into one or more agents. Every event therefore carries the chat channel, the sender, the OpenClaw agent id and the per-turn run id under `raw`, so a command executed on a repository can be traced back to the person who asked for it and the app they asked from.@@ -100,6 +101,25 @@ This path carries gateway-level traces and metrics. It does **not** replace theBy default Asymptote filters OpenClaw's operational metrics out of the runtime log as low-signal; `beacon endpoint install --include-runtime-metrics` keeps them.+## Collection path 3: committed session files++OpenClaw also keeps committed session JSONL files under its state directory:++```text+~/.openclaw/agents/<profile>/sessions/+```++`$OPENCLAW_STATE_DIR` relocates the root, matching the plugin install path. The session sync prefers OpenClaw's `sessions.json` index when it exists, then flat-scans `*.jsonl` session files that are not in the index:++```bash+beacon endpoint integrations openclaw sessions sync+beacon endpoint integrations openclaw sessions status+```++This is a **poll** collection path, not a live hook. It is useful for historical sessions, sessions that ran before Beacon was installed, and sessions missed because the plugin was not loaded. It records committed session starts, prompts, assistant messages, reasoning, tool calls and results, commands, file activity when the stored tool arguments name a path, MCP-shaped tool names, token usage, and errors.++It does not replace the plugin. A poll reader cannot hold or deny a tool call, cannot record an operator approval decision, and can only report what survived in OpenClaw's own session file.+## Your gateway config is read, never writtenAsymptote does **not** edit `~/.openclaw/openclaw.json`. This is the one deliberate departure from how every other runtime is handled, and the reason is that OpenClaw's config is "JSON or JSON5" by its own documentation: JSON5 permits comments, trailing commas and unquoted keys, none of which survive a round trip through a JSON encoder. Rewriting it would silently strip your comments out of the file that configures every chat channel you run.@@ -131,13 +151,14 @@ A config Asymptote cannot parse produces no advice rather than a guess, which is## Discovery and status-```bash title="Show OpenClaw status on both paths"+```bash title="Show OpenClaw status on all paths"beacon endpoint hooks status --harness openclawbeacon endpoint integrations openclaw status+beacon endpoint integrations openclaw sessions statusbeacon endpoint discover --all```-OpenClaw reports as the `openclaw_gateway` harness on **both** paths, so one gateway is one harness in any query that groups by `harness.name`. Plugin events carry `harness.collection_method=plugin` — Asymptote ships the plugin source OpenClaw loads, so its coverage is Asymptote's to fix rather than OpenClaw's to expose — while OTLP events carry `otlp`.+OpenClaw reports as the `openclaw_gateway` harness on every path, so one gateway is one harness in any query that groups by `harness.name`. Plugin events carry `harness.collection_method=plugin` — Asymptote ships the plugin source OpenClaw loads, so its coverage is Asymptote's to fix rather than OpenClaw's to expose — OTLP events carry `otlp`, and session-file sync events carry `poll`.Status checks the manifests as well as the plugin entry, because a directory OpenClaw skipped and a plugin that loaded and saw nothing look identical from the log alone.@@ -160,7 +181,8 @@ beacon endpoint hooks uninstall --harness openclaw| Harness attribution | Supported. `openclaw`, `openclaw_gateway`, `open-claw` and related spellings normalize to `openclaw_gateway` || Plugin install, status, uninstall, and repair | Supported || OTLP export configuration | Printed, not written. It lives in OpenClaw's own config |-| Session lifecycle | Supported, including resumed sessions and the nine end reasons OpenClaw distinguishes |+| Historical/session-file sync | Supported with `beacon endpoint integrations openclaw sessions sync`; events are `harness.collection_method=poll` |+| Session lifecycle | Supported. The plugin includes resumed sessions and the nine end reasons OpenClaw distinguishes; session-file sync records committed session starts || Prompts | Supported. `prompt.submitted` from the inbound chat message, with the sender and the channel || Tool use | Supported for both halves, correlated by `gen_ai.tool.call.id` || Commands | Supported. `command.executed` with the command line, **exit code and captured output** |