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
endpoint: add Devin CLI session-history sync (devinsession) (#972)
Yong Quan Tan437e2deb4b4c
diff --git a/README.md b/README.mdindex 547a3778..250be0e4 100644--- a/README.md+++ b/README.md@@ -320,7 +320,7 @@ Beacon supports local agents, browser agents, cloud agents, CI workflows, and ag| Codex Desktop | OTLP | ✅ | ✅ | ✅ | ✅ | – | ✅ | – | ✅ | ✅ Plugin || Cursor | Hooks + poll | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | – | ✅ Plugin || DeepSeek Harness | Hooks + poll | ✅ | ✅ | ✅ | ✅ | ✅ | – | ✅ | ✅ | ✅ `.agents/skills` |-| Devin CLI | Hooks | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | – | ✅ Plugin |+| Devin CLI | Hooks + poll | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ Plugin || Devin Desktop | Hooks | – | ✅ | ✅ | ✅ | ✅ | – | ✅ | – | ✅ via Devin CLI || Factory Droid | OTLP + hooks + poll | ✅ | ✅ | ✅ | – | ✅ | ✅ | – | – | ✅ Plugin || fx (Vercel Labs) | Poll | ✅ | ✅ | ✅ | ✅ | ✅ | – | ✅ | ✅ | ✅ `/skills install` |diff --git a/cli/beacon/cmd/endpoint_backfill.go b/cli/beacon/cmd/endpoint_backfill.goindex 3bfc4bf2..9f6f8b6e 100644--- a/cli/beacon/cmd/endpoint_backfill.go+++ b/cli/beacon/cmd/endpoint_backfill.go@@ -14,6 +14,7 @@ import ("github.com/asymptote-labs/agent-beacon/cli/beacon/internal/codexsession""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/copilotsession""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/cursorsession"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/devinsession""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/dshsession""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/asymptote"endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"@@ -117,6 +118,10 @@ var endpointBackfillRunners = func() []endpointBackfillRunner {s, err := hermessession.CollectOnce(hermessession.CollectOptions{Write: true, UserMode: r.UserMode, LogPath: r.LogPath, StatePath: resolveHermesStatePath("", r.UserMode), Since: r.Since, Budget: r.Budget})return s.EventsEmitted, err}},+ {Name: "Devin CLI", Sync: "beacon endpoint devin sync", Run: func(r endpointBackfillRun) (int, error) {+ s, err := devinsession.CollectOnce(devinsession.CollectOptions{Write: true, UserMode: r.UserMode, LogPath: r.LogPath, StatePath: resolveEndpointDevinStatePath("", r.UserMode), Since: r.Since, Budget: r.Budget, LiveCoverage: endpointDevinLiveCoverage(r.LogPath)})+ return s.EventsEmitted, err+ }},{Name: "DeepSeek Harness", Sync: "beacon endpoint dsh sync", Run: func(r endpointBackfillRun) (int, error) {s, err := dshsession.CollectOnce(dshsession.CollectOptions{Write: true, UserMode: r.UserMode, LogPath: r.LogPath, StatePath: resolveDshStatePath("", r.UserMode), Since: r.Since, Budget: r.Budget})return s.EventsEmitted, errdiff --git a/cli/beacon/cmd/endpoint_devin.go b/cli/beacon/cmd/endpoint_devin.gonew file mode 100644index 00000000..b05a437f--- /dev/null+++ b/cli/beacon/cmd/endpoint_devin.go@@ -0,0 +1,345 @@+package cmd++import (+ "bufio"+ "bytes"+ "context"+ "encoding/json"+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/devinsession"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"+ "github.com/spf13/cobra"+)++// `beacon endpoint devin` complements the live Devin CLI hook integration.+//+// Devin CLI also keeps committed session history in ~/.local/share/devin/cli/sessions.db.+// Reading that store is a local, offline poll path: useful for backfill and for everything the+// hook payload does not carry -- assistant text, reasoning, per-message token metrics and the+// per-turn model -- but unable to approve, deny, or delay a tool call. Live hooks remain the+// real-time path.+var endpointDevinCmd = &cobra.Command{+ Use: "devin",+ Short: "Collect telemetry from Devin CLI sessions",+ Long: `Collect endpoint telemetry from Devin CLI's local session database.++Devin hooks remain the real-time integration. This command reads the committed+SQLite session store at ~/.local/share/devin/cli/sessions.db read-only and converts+historical or missed rows into endpoint events marked harness.collection_method=poll.+Sessions the live hooks already cover get only what the hook payload cannot carry:+assistant text, reasoning, per-message token metrics and the per-turn model.`,+}++var endpointDevinSyncCmd = &cobra.Command{+ Use: "sync",+ Short: "Read new Devin CLI session rows into the runtime log",+ Long: `Read new Devin CLI session rows into the runtime log.++The runtime log keeps the live file plus five archives, 10 MiB each by default, and every+rotation deletes the oldest. A sweep never rotates out its own output: when writing more would+delete a log file holding the sweep's output, it stops, reports how many sessions are still+pending, and leaves their cursors where they were, so the next sweep picks them up. Between+sweeps, ship or copy the log files if you need to keep every event, or backfill a large history+in one sweep with a larger --rotate-bytes.`,+ SilenceUsage: true,+ RunE: runEndpointDevinSync,+}++var endpointDevinStatusCmd = &cobra.Command{+ Use: "status",+ Short: "Show Devin CLI sessions on this machine and how much has been collected",+ SilenceUsage: true,+ RunE: runEndpointDevinStatus,+}++var endpointDevinOpts struct {+ dbPath string+ statePath string+ logPath string+ print bool+ watch bool+ interval time.Duration+ rotateBytes int64+}++func init() {+ endpointCmd.AddCommand(endpointDevinCmd)+ endpointDevinCmd.AddCommand(endpointDevinSyncCmd)+ endpointDevinCmd.AddCommand(endpointDevinStatusCmd)++ for _, c := range []*cobra.Command{endpointDevinSyncCmd, endpointDevinStatusCmd} {+ f := c.Flags()+ f.StringVar(&endpointDevinOpts.dbPath, "db", "", "Devin CLI session database (default ~/.local/share/devin/cli/sessions.db, %APPDATA%\\devin\\cli\\sessions.db on Windows)")+ f.StringVar(&endpointDevinOpts.statePath, "state", "", "Collector cursor file (default ~/.beacon/endpoint/state/devin.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 := endpointDevinSyncCmd.Flags()+ sync.StringVar(&endpointDevinOpts.logPath, "log-path", "", "Runtime JSONL log path (default resolved endpoint log)")+ sync.BoolVar(&endpointDevinOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")+ sync.BoolVar(&endpointDevinOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")+ sync.DurationVar(&endpointDevinOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+ sync.Int64Var(&endpointDevinOpts.rotateBytes, "rotate-bytes", 0,+ "Rotate the runtime log at this many bytes while this sync writes it (default 10485760, the size every Beacon writer uses; must not be smaller)")+}++// devinSyncOptions turns the sync flags into collector options.+func devinSyncOptions(cmd *cobra.Command) (devinsession.CollectOptions, error) {+ // Smaller than the shared size would only make this sync rotate more often than every other+ // writer, shrinking what the log keeps for everyone.+ if endpointDevinOpts.rotateBytes != 0 && endpointDevinOpts.rotateBytes < writer.DefaultRotateBytes {+ return devinsession.CollectOptions{}, fmt.Errorf("--rotate-bytes must be at least %d, the size every Beacon writer rotates the runtime log at", writer.DefaultRotateBytes)+ }+ userMode := endpointUserMode()+ logPath := lifecycle.ResolveRuntimeLog(userMode, endpointDevinOpts.logPath).EffectiveLogPath+ opts := devinsession.CollectOptions{+ DBPath: endpointDevinOpts.dbPath,+ Print: endpointDevinOpts.print,+ Out: cmd.OutOrStdout(),+ Write: !endpointDevinOpts.print,+ UserMode: userMode,+ RotateBytes: endpointDevinOpts.rotateBytes,+ // Live coverage comes from the log this sweep writes to, in print and write modes+ // alike: a dry run should show exactly what a real sweep would emit.+ LiveCoverage: endpointDevinLiveCoverage(logPath),+ }+ if !endpointDevinOpts.print {+ opts.StatePath = resolveEndpointDevinStatePath(endpointDevinOpts.statePath, userMode)+ opts.LogPath = logPath+ }+ return opts, nil+}++func runEndpointDevinSync(cmd *cobra.Command, args []string) error {+ opts, err := devinSyncOptions(cmd)+ if err != nil {+ return err+ }++ ctx := cmd.Context()+ if ctx == nil {+ ctx = context.Background()+ }+ if endpointDevinOpts.watch && !endpointDevinOpts.print {+ return watchEndpointDevin(ctx, cmd, opts)+ }++ summary, err := devinsession.CollectOnce(opts)+ reportEndpointDevinSweep(cmd, summary)+ return err+}++func watchEndpointDevin(ctx context.Context, cmd *cobra.Command, opts devinsession.CollectOptions) error {+ interval := endpointDevinOpts.interval+ if interval < 5*time.Second {+ interval = 5 * time.Second+ }+ for {+ summary, err := devinsession.CollectOnce(opts)+ if err != nil {+ fmt.Fprintf(cmd.ErrOrStderr(), "Devin CLI sync error: %v\n", err)+ }+ reportEndpointDevinSweep(cmd, summary)+ select {+ case <-ctx.Done():+ return nil+ case <-time.After(interval):+ }+ }+}++func reportEndpointDevinSweep(cmd *cobra.Command, summary devinsession.Summary) {+ if endpointDevinOpts.print {+ return+ }+ if endpointOpts.jsonOutput {+ _ = json.NewEncoder(cmd.OutOrStdout()).Encode(summary)+ return+ }+ fmt.Fprintf(cmd.OutOrStdout(), "Devin CLI sync: %d sessions, %d changed, %d events, %d suppressed (live hooks), %d errors\n",+ summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.EventsSuppressedLive, summary.Errors)+ if summary.RetentionLimited {+ fmt.Fprintf(cmd.OutOrStdout(), " stopped before log rotation would discard this sweep's own events; %d session(s) left pending for the next sweep.\n"++ " Ship or copy the runtime log before the next sweep to keep every event, or backfill in one sweep with a larger --rotate-bytes.\n",+ summary.SessionsPending)+ }+ if summary.EventsRotatedOut > 0 {+ fmt.Fprintf(cmd.OutOrStdout(), " %d event(s) this sweep wrote were rotated out of the runtime log by another writer before it finished\n",+ summary.EventsRotatedOut)+ }+}++// endpointDevinLiveCoverage finds, per Devin session id, the earliest hook event the retained+// runtime log holds. The poll sweep uses it to skip what the hooks already recorded: a session+// the hooks never saw is collected whole, while a covered one keeps only the events the hook+// payload cannot carry.+//+// Every retained archive is read because coverage can sit several rotations back on a busy+// machine; a missing file is simply not evidence. The scan decodes only the three fields it+// needs, so a runtime.jsonl of any size costs a line at a time, and only lines that could be a+// Devin event are unmarshalled.+func endpointDevinLiveCoverage(logPath string) map[string]time.Time {+ coverage := map[string]time.Time{}+ for _, path := range writer.RetainedLogPaths(logPath) {+ f, err := os.Open(path)+ if err != nil {+ continue+ }+ scanner := bufio.NewScanner(f)+ scanner.Buffer(make([]byte, 0, 64*1024), 1<<20)+ for scanner.Scan() {+ line := scanner.Bytes()+ if !bytes.Contains(line, []byte("devin")) {+ continue+ }+ var event struct {+ Timestamp string `json:"timestamp"`+ Harness struct {+ Name string `json:"name"`+ CollectionMethod string `json:"collection_method"`+ } `json:"harness"`+ Session struct {+ ID string `json:"id"`+ } `json:"session"`+ }+ if json.Unmarshal(line, &event) != nil {+ continue+ }+ name := asymptoteobserve.NormalizeHarnessName(event.Harness.Name)+ if (name != "devin-cli" && name != "devin") || event.Harness.CollectionMethod != asymptoteobserve.CollectionMethodHook {+ continue+ }+ sessionID := strings.TrimSpace(event.Session.ID)+ if sessionID == "" {+ continue+ }+ ts, err := asymptoteobserve.ParseTimestamp(event.Timestamp)+ if err != nil {+ continue+ }+ if earliest, ok := coverage[sessionID]; !ok || ts.Before(earliest) {+ coverage[sessionID] = ts+ }+ }+ _ = f.Close()+ }+ if len(coverage) == 0 {+ return nil+ }+ return coverage+}++type devinSessionStatus struct {+ SessionID string `json:"session_id"`+ Title string `json:"title,omitempty"`+ WorkingDirectory string `json:"working_directory,omitempty"`+ StartedAt string `json:"started_at,omitempty"`+ LastActivityAt string `json:"last_activity_at,omitempty"`+ Hidden bool `json:"hidden,omitempty"`+ LastRowID int64 `json:"last_row_id"`+ CollectedRowID int64 `json:"collected_row_id"`+ Collected bool `json:"collected"`+}++type devinStatusReport struct {+ DBPath string `json:"db_path"`+ Present bool `json:"present"`+ StatePath string `json:"state_path,omitempty"`+ Sessions []devinSessionStatus `json:"sessions"`+}++func runEndpointDevinStatus(cmd *cobra.Command, args []string) error {+ dbPath := endpointDevinOpts.dbPath+ if dbPath == "" {+ dbPath = devinsession.DefaultDBPath()+ }+ statePath := resolveEndpointDevinStatePath(endpointDevinOpts.statePath, endpointUserMode())+ report := devinStatusReport{DBPath: dbPath, StatePath: statePath}++ store, err := devinsession.OpenStore(dbPath)+ if err != nil {+ if endpointOpts.jsonOutput {+ _ = json.NewEncoder(cmd.OutOrStdout()).Encode(report)+ return nil+ }+ fmt.Fprintf(cmd.OutOrStdout(), "Devin CLI session database not found or unreadable: %s\n", dbPath)+ return nil+ }+ defer store.Close()+ report.Present = true++ state, err := devinsession.LoadState(statePath)+ if err != nil {+ return err+ }+ sessions, err := store.ListSessions()+ if err != nil {+ return err+ }+ for _, session := range sessions {+ lastID, err := store.LastRowID(session.ID)+ if err != nil {+ return err+ }+ status := devinSessionStatus{+ SessionID: session.ID,+ Title: session.Title,+ WorkingDirectory: session.WorkingDirectory,+ Hidden: session.Hidden,+ LastRowID: lastID,+ }+ if session.CreatedAtMS > 0 {+ status.StartedAt = time.UnixMilli(session.CreatedAtMS).UTC().Format(time.RFC3339)+ }+ if session.LastActivityAtMS > 0 {+ status.LastActivityAt = time.UnixMilli(session.LastActivityAtMS).UTC().Format(time.RFC3339)+ }+ if cursor := state.Sessions[session.ID]; cursor != nil {+ status.CollectedRowID = cursor.LastRowID+ status.Collected = cursor.Started && cursor.LastRowID >= lastID+ }+ report.Sessions = append(report.Sessions, status)+ }+ if endpointOpts.jsonOutput {+ return json.NewEncoder(cmd.OutOrStdout()).Encode(report)+ }+ fmt.Fprintf(cmd.OutOrStdout(), "Devin CLI session database: %s\n", report.DBPath)+ fmt.Fprintf(cmd.OutOrStdout(), "Collector state: %s\n", report.StatePath)+ if len(report.Sessions) == 0 {+ fmt.Fprintln(cmd.OutOrStdout(), "No Devin CLI sessions found.")+ return nil+ }+ for _, session := range report.Sessions {+ mark := "pending"+ if session.Collected {+ mark = "collected"+ }+ fmt.Fprintf(cmd.OutOrStdout(), "- %s [%s] rows %d/%d %s\n",+ session.SessionID, mark, session.CollectedRowID, session.LastRowID, session.Title)+ }+ return nil+}++// resolveEndpointDevinStatePath keeps the cursor beside the log the sweep writes to, like the+// other collectors: a system-mode sweep's cursor belongs next to the system runtime log rather+// than in whichever home the operator happened to run from.+func resolveEndpointDevinStatePath(path string, userMode bool) string {+ if strings.TrimSpace(path) != "" {+ return path+ }+ if !userMode {+ if dir := filepath.Dir(lifecycle.ResolveRuntimeLog(false, "").EffectiveLogPath); dir != "" && dir != "." {+ return filepath.Join(dir, "devin-state.json")+ }+ }+ return devinsession.DefaultStatePath()+}diff --git a/cli/beacon/cmd/endpoint_devin_test.go b/cli/beacon/cmd/endpoint_devin_test.gonew file mode 100644index 00000000..d9a338a3--- /dev/null+++ b/cli/beacon/cmd/endpoint_devin_test.go@@ -0,0 +1,184 @@+package cmd++import (+ "bytes"+ "database/sql"+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "testing"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+ _ "modernc.org/sqlite"+)++// endpointDevinLiveCoverage scans the retained log for the earliest hook event per Devin+// session: hook events from either platform spelling count, poll events and other harnesses do+// not, and a malformed line or a rotated archive neither stops nor fools the scan.+func TestEndpointDevinLiveCoverage(t *testing.T) {+ dir := t.TempDir()+ logPath := filepath.Join(dir, "runtime.jsonl")+ hook := func(session, ts string) string {+ return fmt.Sprintf(`{"timestamp":%q,"harness":{"name":"devin-cli","collection_method":"hook"},"session":{"id":%q},"event":{"action":"prompt.submitted"}}`, ts, session)+ }+ lines := []string{+ `{"timestamp":"2026-10-01T10:00:00Z","harness":{"name":"devin-cli","collection_method":"poll"},"session":{"id":"sess-poll"}}`,+ `{"timestamp":"2026-10-01T10:00:00Z","harness":{"name":"claude_code","collection_method":"hook"},"session":{"id":"sess-claude"}}`,+ hook("sess-1", "2026-10-02T10:00:00Z"),+ hook("sess-1", "2026-10-01T09:00:00Z"), // the earliest wins+ `{malformed devin line`,+ `{"timestamp":"2026-10-01T10:00:00Z","harness":{"name":"devin","collection_method":"hook"},"session":{"id":"sess-legacy"}}`,+ }+ if err := os.WriteFile(logPath, []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil {+ t.Fatal(err)+ }+ // Coverage can sit in a rotated archive on a busy machine.+ archive := hook("sess-archived", "2026-09-15T08:00:00Z")+ if err := os.WriteFile(logPath+".1", []byte(archive+"\n"), 0o600); err != nil {+ t.Fatal(err)+ }++ coverage := endpointDevinLiveCoverage(logPath)+ if len(coverage) != 3 {+ t.Fatalf("coverage = %v, want three hook-covered sessions", coverage)+ }+ if got := coverage["sess-1"]; !got.Equal(time.Date(2026, 10, 1, 9, 0, 0, 0, time.UTC)) {+ t.Fatalf("sess-1 coverage = %s, want the earliest hook event", got)+ }+ if _, ok := coverage["sess-legacy"]; !ok {+ t.Fatal("the legacy devin platform spelling was not counted as live coverage")+ }+ if got := coverage["sess-archived"]; !got.Equal(time.Date(2026, 9, 15, 8, 0, 0, 0, time.UTC)) {+ t.Fatalf("sess-archived coverage = %s", got)+ }+ for _, absent := range []string{"sess-poll", "sess-claude"} {+ if _, ok := coverage[absent]; ok {+ t.Fatalf("%s counted as hook coverage though it is not a devin hook event", absent)+ }+ }+}++func TestEndpointDevinLiveCoverageWithoutALog(t *testing.T) {+ if got := endpointDevinLiveCoverage(filepath.Join(t.TempDir(), "runtime.jsonl")); got != nil {+ t.Fatalf("coverage = %v, want nil for a missing log", got)+ }+}++func writeDevinFixtureDB(t *testing.T, path string) {+ t.Helper()+ db, err := sql.Open("sqlite", path)+ if err != nil {+ t.Fatal(err)+ }+ defer db.Close()+ _, err = db.Exec(`+CREATE TABLE sessions (id TEXT PRIMARY KEY, working_directory TEXT NOT NULL, backend_type TEXT NOT NULL, model TEXT NOT NULL, agent_mode TEXT NOT NULL, created_at INTEGER NOT NULL, last_activity_at INTEGER NOT NULL, title TEXT, main_chain_id INTEGER, shell_last_seen_index INTEGER DEFAULT 0, cogs_json TEXT, workspace_dirs TEXT, hidden INTEGER NOT NULL DEFAULT 0, metadata TEXT);+CREATE TABLE message_nodes (row_id INTEGER PRIMARY KEY AUTOINCREMENT, session_id TEXT NOT NULL, node_id INTEGER NOT NULL, parent_node_id INTEGER, chat_message TEXT NOT NULL, created_at INTEGER NOT NULL, metadata TEXT, FOREIGN KEY (session_id) REFERENCES sessions(id), UNIQUE(session_id, node_id));+INSERT INTO sessions (id, working_directory, backend_type, model, agent_mode, created_at, last_activity_at)+VALUES ('s1', '/repo', 'local', 'swe-1-7', 'standard', 1700000000, 1700000000);+INSERT INTO message_nodes (session_id, node_id, chat_message, created_at)+VALUES ('s1', 1, '{"role":"user","message_id":"u1","content":"hello","metadata":{"created_at":"2023-11-14T22:13:20.000000Z","is_user_input":true}}', 1700000100);`)+ if err != nil {+ t.Fatalf("fixture db: %v", err)+ }+}++func devinCmdFixture(t *testing.T) (dbPath, statePath, logPath string) {+ t.Helper()+ root := t.TempDir()+ dbPath = filepath.Join(root, "sessions.db")+ statePath = filepath.Join(root, "state", "devin.json")+ logPath = filepath.Join(root, "logs", "runtime.jsonl")+ writeDevinFixtureDB(t, dbPath)++ // Command flags are package-level, so each test sets every one it depends on.+ endpointDevinOpts.dbPath = dbPath+ endpointDevinOpts.statePath = statePath+ endpointDevinOpts.logPath = logPath+ endpointDevinOpts.print = false+ endpointDevinOpts.watch = false+ endpointDevinOpts.rotateBytes = 0+ endpointOpts.jsonOutput = false+ endpointOpts.userMode = true+ endpointOpts.systemMode = false+ t.Cleanup(func() {+ endpointDevinOpts.dbPath = ""+ endpointDevinOpts.statePath = ""+ endpointDevinOpts.logPath = ""+ endpointDevinOpts.rotateBytes = 0+ endpointOpts.jsonOutput = false+ })+ return dbPath, statePath, logPath+}++func runDevinSync(t *testing.T) (string, error) {+ t.Helper()+ var out bytes.Buffer+ endpointDevinSyncCmd.SetOut(&out)+ endpointDevinSyncCmd.SetErr(&out)+ t.Cleanup(func() {+ endpointDevinSyncCmd.SetOut(nil)+ endpointDevinSyncCmd.SetErr(nil)+ })+ err := runEndpointDevinSync(endpointDevinSyncCmd, nil)+ return out.String(), err+}++// --print is the dry run: it prints the mapped events and must write neither the runtime log nor+// the cursor, so running it twice shows the same output.+func TestEndpointDevinSyncPrintWritesNothing(t *testing.T) {+ _, statePath, logPath := devinCmdFixture(t)+ endpointDevinOpts.print = true++ out, err := runDevinSync(t)+ if err != nil {+ t.Fatalf("sync --print: %v", err)+ }+ if !strings.Contains(out, `"prompt.submitted"`) {+ t.Fatalf("print output has no prompt event:\n%s", out)+ }+ if _, err := os.Stat(logPath); !os.IsNotExist(err) {+ t.Fatal("--print wrote the runtime log")+ }+ if _, err := os.Stat(statePath); !os.IsNotExist(err) {+ t.Fatal("--print wrote a cursor file")+ }+}++func TestEndpointDevinSyncWritesAndReports(t *testing.T) {+ _, _, logPath := devinCmdFixture(t)++ out, err := runDevinSync(t)+ if err != nil {+ t.Fatalf("sync: %v\n%s", err, out)+ }+ if !strings.Contains(out, "Devin CLI sync: 1 sessions, 1 changed") {+ t.Fatalf("report = %q", out)+ }+ info, err := os.Stat(logPath)+ if err != nil || info.Size() == 0 {+ t.Fatalf("runtime log not written: %v", err)+ }+}++func TestEndpointDevinSyncRejectsARotateBytesBelowTheDefault(t *testing.T) {+ _, statePath, logPath := devinCmdFixture(t)+ endpointDevinOpts.rotateBytes = 1024++ _, err := runDevinSync(t)+ if err == nil || !strings.Contains(err.Error(), "--rotate-bytes") {+ t.Fatalf("err = %v, want a --rotate-bytes error", err)+ }+ if _, statErr := os.Stat(logPath); !os.IsNotExist(statErr) {+ t.Error("a refused --rotate-bytes still wrote the runtime log")+ }+ if _, statErr := os.Stat(statePath); !os.IsNotExist(statErr) {+ t.Error("a refused --rotate-bytes still advanced the cursor")+ }+ endpointDevinOpts.rotateBytes = writer.DefaultRotateBytes+ if _, err := runDevinSync(t); err != nil {+ t.Fatalf("sync at the default rotate size: %v", err)+ }+}diff --git a/cli/beacon/internal/devinsession/collect.go b/cli/beacon/internal/devinsession/collect.gonew file mode 100644index 00000000..d900023d--- /dev/null+++ b/cli/beacon/internal/devinsession/collect.go@@ -0,0 +1,682 @@+// Package devinsession reads Devin CLI's local session history into Beacon endpoint events.+//+// Devin CLI keeps committed session history in a WAL-mode SQLite store (sessions.db under+// ~/.local/share/devin/cli, %APPDATA%\devin\cli on Windows). The store is a tree of message+// nodes per session; the collector walks it in row_id order and converts each row into the+// same event shape the live Devin hooks write, marked harness.collection_method=poll.+//+// Two things make this store unlike the others Beacon reads:+//+// - Devin re-inserts history. On resume and on compaction it writes the whole conversation+// again as new nodes, reusing each message's message_id; assistant rows are also re-inserted+// with the same message_id for no recorded reason. Cursor-position dedupe therefore cannot+// work: the collector keeps a per-session ring of recently seen message_ids in its cursor+// (sha256 prefixes, capped at recentMessageIDLimit) and, for nodes that name the nodes they+// were compacted from (extensions."compact/prior_node_ids"), looks those nodes up and skips+// the row only when a prior node carried the same message_id.+// - sessions.model is only the latest model. The per-turn model lives on each assistant row's+// metadata.generation_model, so the mapper attributes every event to the model of the row it+// came from, falling back to the last real generation_model seen in the session and then to+// sessions.model. The internal labels "compactor" and "summarizer" are not models.+package devinsession++import (+ "database/sql"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "net/url"+ "os"+ "path/filepath"+ "strings"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionfs"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionwindow"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sqliteuri"+ _ "modernc.org/sqlite"+)++const (+ // Harness must equal the hook path's --platform value (handoff.HarnessDevin, "devin-cli") so+ // hook and poll events for the same Devin session merge under one harness.name. It is written+ // as a literal rather than imported because internal/handoff imports every session collector,+ // this one included.+ Harness = "devin-cli"+ StateVersion = 1++ // rowPageSize bounds one page of message_nodes rows. A real session can hold tens of+ // thousands of rows of up to ~1 MB of chat_message JSON each, so a session is never read+ // whole.+ rowPageSize = 500++ // recentMessageIDLimit caps the per-session ring of message_id digests kept for+ // resume/compaction re-insert dedupe. Measured on a real store: ring 4096 left 3 residual+ // duplicates out of 139,847 rows; ring 64 left 655.+ recentMessageIDLimit = 4096+)++// Cursor is one session's collection position.+type Cursor struct {+ // LastRowID is the highest message_nodes.row_id fully collected. Rows are paged in row_id+ // order and the cursor only advances past rows whose events all reached the log.+ LastRowID int64 `json:"last_row_id"`+ Started bool `json:"started"`+ // LastModel is the last real generation_model seen in this session, used as the model for+ // events that carry none of their own (prompts, session events, results of unknown calls).+ LastModel string `json:"last_model,omitempty"`+ // Recent holds 16-hex-char prefixes of sha256(message_id), most recent last, capped at+ // recentMessageIDLimit. It is what catches Devin's re-inserted rows across sweeps.+ Recent []string `json:"recent_message_ids,omitempty"`+ // PendingCalls are tool invocations whose result row has not been collected yet, keyed by+ // call id. Arguments are deliberately not persisted: an edit or write call's arguments can+ // be hundreds of kilobytes, and only the command/path classification fields are needed to+ // map the result.+ PendingCalls map[string]PendingCall `json:"pending_calls,omitempty"`+}++// PendingCall is the part of a tool invocation its result needs later.+type PendingCall struct {+ ID string `json:"id"`+ Name string `json:"name,omitempty"`+ Model string `json:"model,omitempty"`+ Command string `json:"command,omitempty"`+ Path string `json:"path,omitempty"`+ MCPServer string `json:"mcp_server,omitempty"`+ MCPTool string `json:"mcp_tool,omitempty"`+}++type State struct {+ Version int `json:"version"`+ Sessions map[string]*Cursor `json:"sessions"`+}++type CollectOptions struct {+ DBPath string+ StatePath string+ Write bool+ LogPath string+ UserMode bool+ Print bool+ Out io.Writer+ // RotateBytes is the size at which this sweep rotates the runtime log; zero means the size+ // every Beacon writer uses.+ RotateBytes int64+ // Since, when set, limits the sweep to sessions modified at or after it, newest first. Older+ // sessions are not read and their cursors do not move, so a later sync still collects them.+ Since time.Time+ // Budget, when set, caps the bytes this sweep may append. The sweep stops at the first event+ // that does not fit and returns writer.ErrBudgetSpent, with cursors past only what was written.+ Budget *writer.Budget+ // LiveCoverage, when set, marks the sessions the live Devin hooks have already reported, by+ // the earliest hook event timestamp per session id. The hook payload carries prompts and tool+ // calls but no assistant text, reasoning, token metrics or model, so for rows at or after+ // live coverage began the sweep suppresses the actions hooks would double-count (prompts,+ // tool invocations and tool results, session.started and handoffs) and keeps the ones hooks+ // cannot see.+ LiveCoverage map[string]time.Time+}++type Summary struct {+ Sessions int `json:"sessions"`+ SessionsChanged int `json:"sessions_changed"`+ EventsEmitted int `json:"events_emitted"`+ Errors int `json:"errors"`+ // EventsSuppressedLive counts events the sweep mapped but did not emit because live hook+ // coverage made them duplicates (see CollectOptions.LiveCoverage).+ EventsSuppressedLive int `json:"events_suppressed_live"`+ // EventsRetained is how many of the events this sweep wrote are still in the runtime log or+ // its archives when it finishes. EventsRotatedOut is how many another writer's rotations+ // discarded before it finished.+ EventsRetained int `json:"events_retained"`+ EventsRotatedOut int `json:"events_rotated_out"`+ // RetentionLimited is set when the sweep stopped because writing more would have rotated its+ // own output out of the log; SessionsPending is how many sessions still had rows to read.+ RetentionLimited bool `json:"retention_limited"`+ SessionsPending int `json:"sessions_pending"`+}++// Session is one row of the sessions table.+type Session struct {+ ID string+ WorkingDirectory string+ BackendType string+ Model string+ AgentMode string+ Title string+ Hidden bool+ CreatedAtMS int64+ LastActivityAtMS int64+ // Metadata is the raw sessions.metadata JSON; the mapper pulls total_acu_cost and+ // total_credit_cost out of it for session.started's raw block.+ Metadata string+}++// Row is one row of the message_nodes table.+type Row struct {+ RowID int64+ SessionID string+ NodeID int64+ ParentNodeID *int64+ ChatMessage string+ CreatedAtMS int64+ // NodeMetadata is the raw node metadata JSON ("summarized_from", "compact/prior_node_ids"),+ // which may be SQL NULL or the literal string "null".+ NodeMetadata string+}++// MappedEvent is one endpoint event and the row it came from. SourceRowID is 0 for events that+// are not tied to a row (session.started).+type MappedEvent struct {+ SourceRowID int64+ TimestampMS int64+ Event schema.Event+}++func LoadState(path string) (*State, error) {+ state := &State{Version: StateVersion, Sessions: map[string]*Cursor{}}+ if 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 Devin collector state %s: %w", path, err)+ }+ if stored.Version != StateVersion || stored.Sessions == nil {+ return state, nil+ }+ state.Sessions = stored.Sessions+ return state, nil+}++func (s *State) Save(path string) error {+ if 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(sessionID string) *Cursor {+ if s.Sessions == nil {+ s.Sessions = map[string]*Cursor{}+ }+ cursor := s.Sessions[sessionID]+ if cursor == nil {+ cursor = &Cursor{}+ s.Sessions[sessionID] = cursor+ }+ return cursor+}++// rowJournal is the record of what mapping one row changed in the cursor. A row's events are+// emitted right after it maps, and if an emit fails the journal rolls the row's changes back so+// the retried sweep sees the row exactly as it was -- a ring entry kept from a row the cursor+// never reached would make the retry skip it as a duplicate, and a pending call kept from one+// would attribute its result wrongly.+type rowJournal struct {+ lastModel string+ recentLen int+ recentAdded []string+ recentEvicted []string+ pendingAdded []string+ pendingRemoved map[string]PendingCall+}++func CollectOnce(opts CollectOptions) (summary Summary, err error) {+ dbPath := opts.DBPath+ if dbPath == "" {+ dbPath = DefaultDBPath()+ }+ if _, statErr := sessionfs.Stat(dbPath); statErr != nil {+ if os.IsNotExist(statErr) {+ return summary, nil+ }+ return summary, statErr+ }+ store, err := OpenStore(dbPath)+ if err != nil {+ return summary, err+ }+ defer store.Close()+ sessions, err := store.ListSessions()+ if err != nil {+ return summary, err+ }+ sessions = sessionwindow.Recent(sessions, opts.Since, func(s Session) int64 {+ if s.LastActivityAtMS > s.CreatedAtMS {+ return s.LastActivityAtMS+ }+ return s.CreatedAtMS+ })+ summary.Sessions = len(sessions)+ 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 Devin collector state: %w", saveErr)+ }+ }()++ var guard *writer.RetentionGuard+ if opts.Write && !opts.Print {+ guard = &writer.RetentionGuard{}+ }+ var errs []error+ for i, session := range sessions {+ changed, collectErr := collectSession(store, session, state, opts, guard, &summary)+ if errors.Is(collectErr, writer.ErrRetentionWindowFull) {+ // Not a failed session: this one and the rest are left for the next sweep, with+ // cursors that have not moved past anything that was not written.+ if changed {+ summary.SessionsChanged+++ }+ summary.RetentionLimited = true+ summary.SessionsPending = 1 + countPending(store, sessions[i+1:], state)+ errs = append(errs, fmt.Errorf("stopped after %d events with %d Devin CLI session(s) pending: %w",+ summary.EventsEmitted, summary.SessionsPending, collectErr))+ break+ }+ if errors.Is(collectErr, writer.ErrBudgetSpent) {+ // Same: the budget is spent and the rest wait for a later sync.+ if changed {+ summary.SessionsChanged+++ }+ errs = append(errs, collectErr)+ break+ }+ if collectErr != nil {+ summary.Errors+++ errs = append(errs, fmt.Errorf("Devin CLI session %s: %w", session.ID, collectErr))+ continue+ }+ if changed {+ summary.SessionsChanged+++ }+ }+ if guard != nil {+ summary.EventsRetained = guard.Retained()+ summary.EventsRotatedOut = guard.Written() - summary.EventsRetained+ if summary.EventsRotatedOut > 0 {+ errs = append(errs, fmt.Errorf("%d of the %d events this sweep wrote were rotated out of the runtime log by another writer before it finished",+ summary.EventsRotatedOut, guard.Written()))+ }+ }+ if len(errs) > 0 {+ return summary, errors.Join(errs...)+ }+ return summary, nil+}++// countPending is how many of sessions have rows the cursor has not reached, which is what a+// retention-limited sweep leaves for the next one.+func countPending(store *Store, sessions []Session, state *State) int {+ n := 0+ for _, session := range sessions {+ last, err := store.LastRowID(session.ID)+ if err != nil {+ continue+ }+ cursor := state.Sessions[session.ID]+ if cursor == nil || cursor.LastRowID < last {+ n+++ }+ }+ return n+}++func collectSession(store *Store, session Session, state *State, opts CollectOptions, guard *writer.RetentionGuard, summary *Summary) (bool, error) {+ cursor := state.cursor(session.ID)+ m := newMapper(session, cursor)+ changed := false++ // session.started carries no row: it precedes the first page and does not move LastRowID.+ // sessions.created_at is whole seconds and lands after the first prompt on real data, so the+ // stamp is the earlier of it and the first collected row's timestamp.+ if !cursor.Started {+ startTS := session.CreatedAtMS+ if firstRows, err := store.ReadRows(session.ID, cursor.LastRowID, 1); err != nil {+ return changed, err+ } else if len(firstRows) > 0 {+ if firstTS := rowTimestampMS(firstRows[0], mapValue(decodeObject(firstRows[0].ChatMessage)["metadata"])); firstTS > 0 && (startTS == 0 || firstTS < startTS) {+ startTS = firstTS+ }+ }+ started := m.mapSessionStarted(startTS)+ if suppressedLive(started.Event.Event.Action, started.TimestampMS, session, opts) {+ summary.EventsSuppressedLive+++ } else if err := emit(started.Event, opts, guard); err != nil {+ return true, err+ } else {+ summary.EventsEmitted+++ }+ cursor.Started = true+ changed = true+ }++ for {+ rows, err := store.ReadRows(session.ID, cursor.LastRowID, rowPageSize)+ if err != nil {+ return changed, err+ }+ if len(rows) == 0 {+ return changed, nil+ }+ priorIDs, err := store.priorMessageIDs(session.ID, collectPriorNodeIDs(rows))+ if err != nil {+ return changed, err+ }+ // The cursor moves row by row, only past rows whose events are all written: a retried+ // sweep starts at the first unwritten row rather than re-emitting the page. A row whose+ // emit fails partway through rolls its cursor changes back (see rowJournal); the events+ // it did write re-emit on the retry with identical deterministic ids, which is+ // acceptable -- a row emits at most a handful of events.+ for _, row := range rows {+ journal := m.beginRow()+ for _, item := range m.mapRow(row, priorIDs) {+ if suppressedLive(item.Event.Event.Action, item.TimestampMS, session, opts) {+ summary.EventsSuppressedLive+++ continue+ }+ if err := emit(item.Event, opts, guard); err != nil {+ m.rollbackRow(journal)+ return true, err+ }+ summary.EventsEmitted+++ }+ cursor.LastRowID = row.RowID+ changed = true+ }+ }+}++// suppressedLive reports whether this event is one the live hook path already recorded. Devin+// hooks see the prompt and every tool call as it happens but the payload carries no assistant+// text, reasoning, token metrics or model, so actions hooks would double-count are skipped for+// rows at or after live coverage began (with a small clock-skew allowance), while agent.message,+// agent.reasoning, token.usage, session.context, session.compacted and approval.denied always+// emit -- the hook never saw them.+func suppressedLive(action string, timestampMS int64, session Session, opts CollectOptions) bool {+ cutover, ok := opts.LiveCoverage[session.ID]+ if !ok {+ return false+ }+ if timestampMS < cutover.UnixMilli()-2000 {+ return false+ }+ switch action {+ case "session.started", "prompt.submitted", "session.handoff",+ "tool.invoked", "mcp.tool_invoked",+ "command.executed", "file.read", "file.modified", "tool.completed", "tool.failed":+ return true+ }+ return false+}++func emit(event schema.Event, opts CollectOptions, guard *writer.RetentionGuard) error {+ if opts.Print {+ out := opts.Out+ if out == nil {+ out = io.Discard+ }+ return json.NewEncoder(out).Encode(event)+ }+ if !opts.Write {+ return nil+ }+ _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode, RotateSize: opts.RotateBytes, Guard: guard, Budget: opts.Budget})+ return err+}++// Store is a read-only handle on sessions.db.+type Store struct {+ path string+ db *sql.DB+ sessionColumns map[string]bool+ nodeColumns map[string]bool+}++func OpenStore(path string) (*Store, error) {+ db, err := openSQLiteReadOnly(path)+ if err != nil {+ return nil, err+ }+ if err := db.Ping(); err != nil {+ _ = db.Close()+ return nil, err+ }+ store := &Store{path: path, db: db}+ for _, table := range []string{"sessions", "message_nodes"} {+ columns, err := tableColumns(db, table)+ if err != nil {+ _ = db.Close()+ return nil, err+ }+ if table == "sessions" {+ store.sessionColumns = columns+ } else {+ store.nodeColumns = columns+ }+ }+ for _, required := range []struct{ table, column string }{+ {"sessions", "id"},+ {"message_nodes", "row_id"},+ {"message_nodes", "session_id"},+ {"message_nodes", "node_id"},+ {"message_nodes", "chat_message"},+ {"message_nodes", "created_at"},+ } {+ columns := store.nodeColumns+ if required.table == "sessions" {+ columns = store.sessionColumns+ }+ if !columns[required.column] {+ _ = db.Close()+ return nil, fmt.Errorf("Devin sessions database %s is missing required column %s.%s", path, required.table, required.column)+ }+ }+ return store, nil+}++func (s *Store) Close() error { return s.db.Close() }++func (s *Store) Path() string { return s.path }++// tableColumns lists a table's columns, so the queries below can select optional columns when+// they exist and NULL when they do not. Columns or JSON keys this collector does not know are+// ignored either way.+func tableColumns(db *sql.DB, table string) (map[string]bool, error) {+ rows, err := db.Query(fmt.Sprintf(`SELECT name FROM pragma_table_info(%s)`, quoteIdent(table)))+ if err != nil {+ return nil, err+ }+ defer rows.Close()+ columns := map[string]bool{}+ for rows.Next() {+ var name string+ if err := rows.Scan(&name); err != nil {+ return nil, err+ }+ columns[name] = true+ }+ return columns, rows.Err()+}++func quoteIdent(name string) string {+ return `'` + strings.ReplaceAll(name, `'`, `''`) + `'`+}++// pick renders a column name for SELECT when it exists and NULL aliased to it when it does not.+func pick(columns map[string]bool, name string) string {+ if columns[name] {+ return name+ }+ return "NULL AS " + name+}++func (s *Store) ListSessions() ([]Session, error) {+ orderBy := "created_at, id"+ if !s.sessionColumns["created_at"] {+ orderBy = "id"+ }+ rows, err := s.db.Query(fmt.Sprintf(`+SELECT id, %s, %s, %s, %s, %s, %s, %s, %s, %s+FROM sessions+ORDER BY `+orderBy,+ pick(s.sessionColumns, "working_directory"),+ pick(s.sessionColumns, "backend_type"),+ pick(s.sessionColumns, "model"),+ pick(s.sessionColumns, "agent_mode"),+ pick(s.sessionColumns, "title"),+ pick(s.sessionColumns, "hidden"),+ pick(s.sessionColumns, "created_at"),+ pick(s.sessionColumns, "last_activity_at"),+ pick(s.sessionColumns, "metadata")))+ if err != nil {+ return nil, err+ }+ defer rows.Close()+ var out []Session+ for rows.Next() {+ var session Session+ var wd, backend, model, mode, title, metadata sql.NullString+ var created, activity sql.NullInt64+ var hidden sql.NullInt64+ if err := rows.Scan(&session.ID, &wd, &backend, &model, &mode, &title, &hidden, &created, &activity, &metadata); err != nil {+ return nil, err+ }+ session.WorkingDirectory = wd.String+ session.BackendType = backend.String+ session.Model = model.String+ session.AgentMode = mode.String+ session.Title = title.String+ session.Hidden = hidden.Int64 != 0+ session.CreatedAtMS = unixSecondsMS(created)+ session.LastActivityAtMS = unixSecondsMS(activity)+ session.Metadata = metadata.String+ out = append(out, session)+ }+ return out, rows.Err()+}++// ReadRows returns up to limit message_nodes rows for one session past afterRowID, in row_id+// order. Pages keep a large session out of memory: row chat_message values can reach ~1 MB.+func (s *Store) ReadRows(sessionID string, afterRowID int64, limit int) ([]Row, error) {+ rows, err := s.db.Query(fmt.Sprintf(`+SELECT row_id, session_id, node_id, %s, chat_message, created_at, %s+FROM message_nodes+WHERE session_id = ? AND row_id > ?+ORDER BY row_id+LIMIT ?`, pick(s.nodeColumns, "parent_node_id"), pick(s.nodeColumns, "metadata")), sessionID, afterRowID, limit)+ if err != nil {+ return nil, err+ }+ defer rows.Close()+ var out []Row+ for rows.Next() {+ var row Row+ var parent sql.NullInt64+ var created sql.NullInt64+ var metadata sql.NullString+ if err := rows.Scan(&row.RowID, &row.SessionID, &row.NodeID, &parent, &row.ChatMessage, &created, &metadata); err != nil {+ return nil, err+ }+ if parent.Valid {+ row.ParentNodeID = &parent.Int64+ }+ row.CreatedAtMS = unixSecondsMS(created)+ row.NodeMetadata = metadata.String+ out = append(out, row)+ }+ return out, rows.Err()+}++func (s *Store) LastRowID(sessionID string) (int64, error) {+ var last sql.NullInt64+ if err := s.db.QueryRow(`SELECT max(row_id) FROM message_nodes WHERE session_id = ?`, sessionID).Scan(&last); err != nil {+ return 0, err+ }+ return last.Int64, nil+}++// priorMessageIDs resolves the message_ids of given node_ids within one session, in chunks of at+// most 500 bound parameters. A compaction copy names the nodes it was compacted from; matching+// the copy's message_id against a prior node's is what tells a resume copy from new content.+func (s *Store) priorMessageIDs(sessionID string, nodeIDs []int64) (map[int64]string, error) {+ out := map[int64]string{}+ if len(nodeIDs) == 0 {+ return out, nil+ }+ const chunk = 500+ for start := 0; start < len(nodeIDs); start += chunk {+ end := start + chunk+ if end > len(nodeIDs) {+ end = len(nodeIDs)+ }+ part := nodeIDs[start:end]+ placeholders := strings.TrimSuffix(strings.Repeat("?,", len(part)), ",")+ args := make([]interface{}, 0, len(part)+1)+ args = append(args, sessionID)+ for _, id := range part {+ args = append(args, id)+ }+ rows, err := s.db.Query(fmt.Sprintf(`+SELECT node_id, json_extract(chat_message, '$.message_id')+FROM message_nodes+WHERE session_id = ? AND node_id IN (%s)`, placeholders), args...)+ if err != nil {+ return nil, err+ }+ for rows.Next() {+ var nodeID int64+ var messageID sql.NullString+ if err := rows.Scan(&nodeID, &messageID); err != nil {+ rows.Close()+ return nil, err+ }+ out[nodeID] = messageID.String+ }+ if err := rows.Close(); err != nil {+ return nil, err+ }+ }+ return out, nil+}++func unixSecondsMS(value sql.NullInt64) int64 {+ if !value.Valid || value.Int64 <= 0 {+ return 0+ }+ return value.Int64 * 1000+}++func openSQLiteReadOnly(path string) (*sql.DB, error) {+ path, err := sessionfs.SQLitePath(path)+ if err != nil {+ return nil, err+ }+ return sql.Open("sqlite", sqliteuri.File(path, url.Values{"mode": {"ro"}, "_pragma": {"busy_timeout(1000)"}}))+}diff --git a/cli/beacon/internal/devinsession/collect_test.go b/cli/beacon/internal/devinsession/collect_test.gonew file mode 100644index 00000000..7e8e859a--- /dev/null+++ b/cli/beacon/internal/devinsession/collect_test.go@@ -0,0 +1,1293 @@+package devinsession++import (+ "bufio"+ "bytes"+ "database/sql"+ "encoding/json"+ "errors"+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "testing"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+)++// The store's real DDL, verbatim: a fixture must break if this collector drifts from the schema+// Devin CLI actually writes.+const devinFixtureDDL = `+CREATE TABLE sessions (id TEXT PRIMARY KEY, working_directory TEXT NOT NULL, backend_type TEXT NOT NULL, model TEXT NOT NULL, agent_mode TEXT NOT NULL, created_at INTEGER NOT NULL, last_activity_at INTEGER NOT NULL, title TEXT, main_chain_id INTEGER, shell_last_seen_index INTEGER DEFAULT 0, cogs_json TEXT, workspace_dirs TEXT, hidden INTEGER NOT NULL DEFAULT 0, metadata TEXT);+CREATE TABLE message_nodes (row_id INTEGER PRIMARY KEY AUTOINCREMENT, session_id TEXT NOT NULL, node_id INTEGER NOT NULL, parent_node_id INTEGER, chat_message TEXT NOT NULL, created_at INTEGER NOT NULL, metadata TEXT, FOREIGN KEY (session_id) REFERENCES sessions(id), UNIQUE(session_id, node_id));+CREATE INDEX idx_message_nodes_session ON message_nodes(session_id);+`++func openFixtureDB(t *testing.T, path string) *sql.DB {+ t.Helper()+ db, err := sql.Open("sqlite", path)+ if err != nil {+ t.Fatalf("open fixture db: %v", err)+ }+ t.Cleanup(func() { db.Close() })+ if _, err := db.Exec(devinFixtureDDL); err != nil {+ t.Fatalf("create fixture schema: %v", err)+ }+ return db+}++func addSession(t *testing.T, db *sql.DB, id string, fields map[string]interface{}) {+ t.Helper()+ session := map[string]interface{}{+ "working_directory": "/repo",+ "backend_type": "local",+ "model": "swe-1-7",+ "agent_mode": "standard",+ "created_at": 1700000000,+ "last_activity_at": 1700000000,+ "title": "fixture session",+ "metadata": `{"total_acu_cost":1.5,"total_credit_cost":3}`,+ }+ for key, value := range fields {+ session[key] = value+ }+ _, err := db.Exec(`INSERT INTO sessions+(id, working_directory, backend_type, model, agent_mode, created_at, last_activity_at, title, metadata)+VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,+ id,+ session["working_directory"], session["backend_type"], session["model"], session["agent_mode"],+ session["created_at"], session["last_activity_at"], session["title"], session["metadata"])+ if err != nil {+ t.Fatalf("insert session %s: %v", id, err)+ }+}++func addNode(t *testing.T, db *sql.DB, sessionID string, nodeID int64, chatMessage string, createdAtSec int64, nodeMetadata interface{}) {+ t.Helper()+ _, err := db.Exec(`INSERT INTO message_nodes (session_id, node_id, chat_message, created_at, metadata)+VALUES (?, ?, ?, ?, ?)`, sessionID, nodeID, chatMessage, createdAtSec, nodeMetadata)+ if err != nil {+ t.Fatalf("insert node %s/%d: %v", sessionID, nodeID, err)+ }+}++func chatJSON(fields map[string]interface{}) string {+ data, err := json.Marshal(fields)+ if err != nil {+ panic(err)+ }+ return string(data)+}++func userMsg(messageID, text, iso string) string {+ return chatJSON(map[string]interface{}{+ "role": "user",+ "message_id": messageID,+ "content": text,+ "metadata": map[string]interface{}{"created_at": iso, "is_user_input": true},+ })+}++type assistantFixture struct {+ Content string+ Thinking string+ Model string+ Finish string+ RequestID string+ Metrics map[string]interface{}+ ToolCalls []map[string]interface{}+ Extensions map[string]interface{}+ CreatedAt string+}++func assistantMsg(messageID string, f assistantFixture) string {+ meta := map[string]interface{}{}+ if f.CreatedAt != "" {+ meta["created_at"] = f.CreatedAt+ }+ if f.Model != "" {+ meta["generation_model"] = f.Model+ }+ if f.Metrics != nil {+ meta["metrics"] = f.Metrics+ }+ if f.Finish != "" {+ meta["finish_reason"] = f.Finish+ }+ if f.RequestID != "" {+ meta["request_id"] = f.RequestID+ }+ if f.Extensions != nil {+ meta["extensions"] = f.Extensions+ }+ msg := map[string]interface{}{+ "role": "assistant",+ "message_id": messageID,+ "content": f.Content,+ "metadata": meta,+ }+ if f.Thinking != "" {+ msg["thinking"] = map[string]interface{}{"thinking": f.Thinking, "signature": "sig"}+ }+ if len(f.ToolCalls) > 0 {+ msg["tool_calls"] = f.ToolCalls+ }+ return chatJSON(msg)+}++func toolCallFixture(id, name string, args map[string]interface{}) map[string]interface{} {+ return map[string]interface{}{"id": id, "name": name, "arguments": args, "index": 0, "kind": "function"}+}++func toolMsg(messageID, callID, content, iso string, ext map[string]interface{}) string {+ return chatJSON(map[string]interface{}{+ "role": "tool",+ "message_id": messageID,+ "tool_call_id": callID,+ "content": content,+ "metadata": map[string]interface{}{"created_at": iso, "extensions": ext},+ })+}++func summaryMsg(messageID, text, iso string) string {+ return chatJSON(map[string]interface{}{+ "role": "system",+ "message_id": messageID,+ "content": text,+ "metadata": map[string]interface{}{+ "created_at": iso,+ "extensions": map[string]interface{}{"devin-rs/summary": map[string]interface{}{"source": "async_file_compactor"}},+ },+ })+}++type fixturePaths struct {+ db string+ state string+ log string+}++func newFixturePaths(t *testing.T) fixturePaths {+ t.Helper()+ dir := t.TempDir()+ return fixturePaths{+ db: filepath.Join(dir, "sessions.db"),+ state: filepath.Join(dir, "devin-state.json"),+ log: filepath.Join(dir, "runtime.jsonl"),+ }+}++func collectOptions(p fixturePaths) CollectOptions {+ return CollectOptions{DBPath: p.db, StatePath: p.state, LogPath: p.log, Write: true, UserMode: true}+}++func readEvents(t *testing.T, path string) []schema.Event {+ t.Helper()+ data, err := os.ReadFile(path)+ if err != nil {+ t.Fatalf("read log: %v", err)+ }+ var events []schema.Event+ for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") {+ if strings.TrimSpace(line) == "" {+ continue+ }+ var ev schema.Event+ if err := json.Unmarshal([]byte(line), &ev); err != nil {+ t.Fatalf("decode event line %q: %v", line, err)+ }+ events = append(events, ev)+ }+ return events+}++func eventActions(events []schema.Event) []string {+ out := make([]string, 0, len(events))+ for _, ev := range events {+ out = append(out, ev.Event.Action)+ }+ return out+}++func findAction(t *testing.T, events []schema.Event, action string) schema.Event {+ t.Helper()+ for _, ev := range events {+ if ev.Event.Action == action {+ return ev+ }+ }+ t.Fatalf("no %s event in %v", action, eventActions(events))+ return schema.Event{}+}++func devinRaw(ev schema.Event) map[string]interface{} {+ raw, _ := ev.Raw["devin"].(map[string]interface{})+ return raw+}++// The canonical fixture: one session with an operator prompt, an assistant turn that thinks,+// replies, runs a command and reports metrics, and a compaction marker row.+func seedBasicSession(t *testing.T, db *sql.DB, sessionID string) {+ t.Helper()+ addSession(t, db, sessionID, nil)+ addNode(t, db, sessionID, 1, userMsg("u1", "fix the flaky test", "2023-11-14T22:13:20.000000Z"), 1700000100, nil)+ addNode(t, db, sessionID, 2, assistantMsg("a1", assistantFixture{+ Content: "I will run the test",+ Thinking: "let me look at the test first",+ Model: "swe-1-7",+ Finish: "stop",+ RequestID: "req-1",+ Metrics: map[string]interface{}{"input_tokens": 100, "output_tokens": 20, "cache_read_tokens": 5, "cache_creation_tokens": 3},+ ToolCalls: []map[string]interface{}{toolCallFixture("call-1", "exec", map[string]interface{}{"command": "go test ./...", "workdir": "/repo"})},+ CreatedAt: "2023-11-14T22:13:40.123456Z",+ }), 1700000200, nil)+ addNode(t, db, sessionID, 3, toolMsg("t1", "call-1", "ok ./pkg\nPASS", "2023-11-14T22:13:50.000000Z", map[string]interface{}{+ "chisel/tool_result_meta": map[string]interface{}{"success": true, "kind": "execute"},+ "chisel/terminal_output": map[string]interface{}{"cwd": "/repo", "exit": map[string]interface{}{"exit_code": 0, "terminal_id": "term-1"}},+ }), 1700000300, nil)+ addNode(t, db, sessionID, 4, summaryMsg("s1", "<summary>the test was fixed</summary>", "2023-11-14T22:14:00.000000Z"), 1700000400,+ `{"summarized_from":2,"extensions":{"compact/prior_node_ids":[1,2,3]}}`)+}++func TestCollectOnceMapsDevinStoreAndAdvancesCursor(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ seedBasicSession(t, db, "quiet-meadow")++ summary, err := CollectOnce(collectOptions(p))+ if err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ if summary.Sessions != 1 || summary.SessionsChanged != 1 || summary.Errors != 0 {+ t.Fatalf("summary = %#v", summary)+ }++ events := readEvents(t, p.log)+ want := []string{+ "session.started",+ "prompt.submitted",+ "agent.reasoning",+ "agent.message",+ "tool.invoked",+ "token.usage",+ "command.executed",+ "session.compacted",+ }+ if strings.Join(eventActions(events), ",") != strings.Join(want, ",") {+ t.Fatalf("actions = %v, want %v", eventActions(events), want)+ }++ started := findAction(t, events, "session.started")+ if got := devinRaw(started)["hidden"]; got != false {+ t.Fatalf("session.started raw hidden = %v", got)+ }+ if got := devinRaw(started)["total_acu_cost"]; got != 1.5 {+ t.Fatalf("session.started raw total_acu_cost = %v, want the credit value raw, not cost_usd", got)+ }++ prompt := findAction(t, events, "prompt.submitted")+ if prompt.Prompt == nil || prompt.Prompt.Text != "fix the flaky test" {+ t.Fatalf("prompt = %#v", prompt.Prompt)+ }++ msg := findAction(t, events, "agent.message")+ if msg.Model != "swe-1-7" {+ t.Fatalf("agent.message model = %q, want the row's generation_model", msg.Model)+ }+ if msg.GenAI == nil || msg.GenAI.Request == nil || msg.GenAI.Request.Model != "swe-1-7" {+ t.Fatalf("agent.message gen_ai.request = %#v", msg.GenAI)+ }++ invoked := findAction(t, events, "tool.invoked")+ if invoked.GenAI == nil || invoked.GenAI.Tool == nil || invoked.GenAI.Tool.Call == nil || invoked.GenAI.Tool.Call.ID != "call-1" {+ t.Fatalf("tool.invoked gen_ai.tool.call = %#v, want the call id the hook path records", invoked.GenAI)+ }++ usage := findAction(t, events, "token.usage")+ if usage.GenAI == nil || usage.GenAI.Usage == nil || usage.GenAI.Usage.InputTokens == nil || *usage.GenAI.Usage.InputTokens != 100 {+ t.Fatalf("usage = %#v", usage.GenAI)+ }+ if usage.GenAI.Usage.CacheRead == nil || *usage.GenAI.Usage.CacheRead.InputTokens != 5 {+ t.Fatalf("usage cache_read = %#v", usage.GenAI.Usage.CacheRead)+ }+ if usage.Model != "swe-1-7" {+ t.Fatalf("usage model = %q", usage.Model)+ }++ command := findAction(t, events, "command.executed")+ if command.Command == nil || command.Command.Command != "go test ./..." {+ t.Fatalf("command = %#v", command.Command)+ }+ if command.Command.ExitCode == nil || *command.Command.ExitCode != 0 {+ t.Fatalf("exit code = %#v", command.Command.ExitCode)+ }+ if command.Harness.Name != Harness || command.Harness.CollectionMethod != schema.CollectionMethodPoll {+ t.Fatalf("harness = %#v", command.Harness)+ }++ compacted := findAction(t, events, "session.compacted")+ if got := devinRaw(compacted)["summarized_from"]; got != float64(2) {+ t.Fatalf("compacted raw = %v", devinRaw(compacted))+ }++ state, err := LoadState(p.state)+ if err != nil {+ t.Fatalf("LoadState: %v", err)+ }+ cursor := state.Sessions["quiet-meadow"]+ if cursor == nil || !cursor.Started || cursor.LastRowID != 4 || cursor.LastModel != "swe-1-7" {+ t.Fatalf("cursor = %#v", cursor)+ }+ if len(cursor.Recent) == 0 {+ t.Fatal("cursor kept no recent message ids; re-inserted rows would re-emit")+ }++ second, err := CollectOnce(collectOptions(p))+ if err != nil {+ t.Fatalf("second CollectOnce: %v", err)+ }+ if second.EventsEmitted != 0 || second.SessionsChanged != 0 {+ t.Fatalf("second summary = %#v, want no new events", second)+ }+ if got := len(readEvents(t, p.log)); got != len(events) {+ t.Fatalf("second sweep wrote events: %d != %d", got, len(events))+ }+}++func TestCollectOnceReadsOnlyAppendedRows(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, userMsg("u1", "first", "2023-11-14T22:13:20.000000Z"), 1700000100, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("first CollectOnce: %v", err)+ }+ before := readEvents(t, p.log)++ addNode(t, db, "s1", 2, assistantMsg("a1", assistantFixture{Content: "second answer", Model: "swe-1-7", CreatedAt: "2023-11-14T22:15:00.000000Z"}), 1700000500, nil)+ summary, err := CollectOnce(collectOptions(p))+ if err != nil {+ t.Fatalf("second CollectOnce: %v", err)+ }+ after := readEvents(t, p.log)+ if got := eventActions(after[len(before):]); len(got) != 1 || got[0] != "agent.message" {+ t.Fatalf("appended sweep emitted %v, want just agent.message", got)+ }+ if summary.EventsEmitted != 1 {+ t.Fatalf("emitted = %d, want 1", summary.EventsEmitted)+ }+}++func TestCollectOnceAttributesPerTurnModelAndRecordsModelChanges(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", map[string]interface{}{"model": "claude-haiku-5-5-low"})+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{Content: "answer on model A", Model: "swe-1-7", CreatedAt: "2023-11-14T22:13:40.000000Z"}), 1700000100, nil)+ addNode(t, db, "s1", 2, assistantMsg("a2", assistantFixture{Content: "answer on model B", Model: "claude-haiku-5-5-low", CreatedAt: "2023-11-14T22:14:40.000000Z"}), 1700000200, nil)+ addNode(t, db, "s1", 3, userMsg("u1", "and now?", "2023-11-14T22:15:00.000000Z"), 1700000300, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ events := readEvents(t, p.log)++ var models []string+ for _, ev := range events {+ if ev.Event.Action == "agent.message" {+ models = append(models, ev.Model)+ }+ }+ if strings.Join(models, ",") != "swe-1-7,claude-haiku-5-5-low" {+ t.Fatalf("agent.message models = %v, want the per-turn models in order", models)+ }++ var changes []schema.Event+ for _, ev := range events {+ if ev.Event.Action == "session.context" {+ changes = append(changes, ev)+ }+ }+ if len(changes) != 1 {+ t.Fatalf("session.context events = %d, want exactly one model_change", len(changes))+ }+ change := devinRaw(changes[0])+ if change["change"] != "model_change" || change["from"] != "swe-1-7" || change["to"] != "claude-haiku-5-5-low" {+ t.Fatalf("model_change raw = %v", change)+ }+ if changes[0].Model != "claude-haiku-5-5-low" {+ t.Fatalf("model_change event model = %q", changes[0].Model)+ }++ // A prompt carries the model of the turn it belongs to: the last real generation_model.+ prompt := findAction(t, events, "prompt.submitted")+ if prompt.Model != "claude-haiku-5-5-low" {+ t.Fatalf("prompt after the switch has model %q, want claude-haiku-5-5-low", prompt.Model)+ }+}++func TestCollectOnceMapsFailingAndPassingCommands(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{+ Model: "swe-1-7",+ ToolCalls: []map[string]interface{}{toolCallFixture("call-fail", "exec", map[string]interface{}{"command": "make test"})},+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000100, nil)+ addNode(t, db, "s1", 2, toolMsg("t1", "call-fail", "compile error", "2023-11-14T22:13:50.000000Z", map[string]interface{}{+ "chisel/tool_result_meta": map[string]interface{}{"success": false, "kind": "execute", "failure_reason": "ToolError"},+ "chisel/terminal_output": map[string]interface{}{"exit": map[string]interface{}{"exit_code": 1}},+ }), 1700000200, nil)+ addNode(t, db, "s1", 3, assistantMsg("a2", assistantFixture{+ Model: "swe-1-7",+ ToolCalls: []map[string]interface{}{toolCallFixture("call-pass", "exec", map[string]interface{}{"command": "make test"})},+ CreatedAt: "2023-11-14T22:14:40.000000Z",+ }), 1700000300, nil)+ addNode(t, db, "s1", 4, toolMsg("t2", "call-pass", "all good", "2023-11-14T22:14:50.000000Z", map[string]interface{}{+ "chisel/tool_result_meta": map[string]interface{}{"success": true, "kind": "execute"},+ "chisel/terminal_output": map[string]interface{}{"exit": map[string]interface{}{"exit_code": 0}},+ }), 1700000400, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ var commands []schema.Event+ for _, ev := range readEvents(t, p.log) {+ if ev.Event.Action == "command.executed" {+ commands = append(commands, ev)+ }+ }+ if len(commands) != 2 {+ t.Fatalf("command.executed = %d, want 2", len(commands))+ }+ failed, passed := commands[0], commands[1]+ if failed.Severity != schema.SeverityMedium || failed.Error == nil || failed.Error.Type != "command_failed" {+ t.Fatalf("failed command = %#v / %#v", failed.Severity, failed.Error)+ }+ if failed.GenAI.Tool.Call.ID != "call-fail" || *failed.Command.ExitCode != 1 {+ t.Fatalf("failed command identity = %#v", failed)+ }+ if passed.Severity != schema.SeverityInfo || passed.Error != nil {+ t.Fatalf("passing command = %#v", passed)+ }+}++func TestCollectOnceMapsAnEditToFileModified(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{+ Model: "swe-1-7",+ ToolCalls: []map[string]interface{}{toolCallFixture("call-1", "edit", map[string]interface{}{"file_path": "main.go", "old_string": "a", "new_string": "b"})},+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000100, nil)+ addNode(t, db, "s1", 2, toolMsg("t1", "call-1", "edited", "2023-11-14T22:13:50.000000Z", map[string]interface{}{+ "chisel/tool_result_meta": map[string]interface{}{"success": true, "kind": "edit"},+ }), 1700000200, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ file := findAction(t, readEvents(t, p.log), "file.modified")+ if file.File == nil || file.File.Path != "main.go" || file.File.Operation != "modify" || file.File.Language != "go" {+ t.Fatalf("file = %#v", file.File)+ }+}++func TestResumeCopyRowsWithPriorNodeIDsAreSkipped(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, userMsg("u1", "hello", "2023-11-14T22:13:20.000000Z"), 1700000100, nil)+ addNode(t, db, "s1", 2, assistantMsg("a1", assistantFixture{+ Content: "hi", Model: "swe-1-7",+ Metrics: map[string]interface{}{"input_tokens": 10, "output_tokens": 5},+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000200, nil)+ // A resume re-inserts the history as new nodes pointing back at what they copy. The+ // re-inserted prompt and assistant row keep their message_ids, so both must be skipped --+ // including the usage a naive cursor-position read would emit a second time.+ addNode(t, db, "s1", 3, userMsg("u1", "hello", "2023-11-14T22:13:20.000000Z"), 1700000300,+ `{"extensions":{"compact/prior_node_ids":[1]}}`)+ addNode(t, db, "s1", 4, assistantMsg("a1", assistantFixture{+ Content: "hi", Model: "swe-1-7",+ Metrics: map[string]interface{}{"input_tokens": 10, "output_tokens": 5},+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000400, `{"extensions":{"compact/prior_node_ids":[2]}}`)++ summary, err := CollectOnce(collectOptions(p))+ if err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ actions := eventActions(readEvents(t, p.log))+ count := map[string]int{}+ for _, a := range actions {+ count[a]+++ }+ if count["prompt.submitted"] != 1 || count["token.usage"] != 1 || count["agent.message"] != 1 {+ t.Fatalf("re-inserted rows were re-emitted: %v", actions)+ }+ if summary.EventsEmitted != len(actions) {+ t.Fatalf("emitted = %d, actions %v", summary.EventsEmitted, actions)+ }+}++func TestAdjacentReInsertWithoutPriorNodeIDsIsSkipped(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{Content: "same", Model: "swe-1-7", CreatedAt: "2023-11-14T22:13:40.000000Z"}), 1700000100, nil)+ // Devin re-inserts assistant rows with the same message_id and no prior ids, usually adjacent.+ addNode(t, db, "s1", 2, assistantMsg("a1", assistantFixture{Content: "same", Model: "swe-1-7", CreatedAt: "2023-11-14T22:13:40.000000Z"}), 1700000200, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ count := 0+ for _, ev := range readEvents(t, p.log) {+ if ev.Event.Action == "agent.message" {+ count+++ }+ }+ if count != 1 {+ t.Fatalf("agent.message = %d, want the adjacent re-insert skipped", count)+ }+}++func TestReInsertOnALaterSweepIsSkippedByThePersistedRing(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{Content: "first", Model: "swe-1-7", CreatedAt: "2023-11-14T22:13:40.000000Z"}), 1700000100, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("first CollectOnce: %v", err)+ }+ before := len(readEvents(t, p.log))++ // The same message_id written again rows later -- the ring the cursor persisted is what+ // catches it, since cursor position alone would read it as new.+ addNode(t, db, "s1", 2, assistantMsg("a1", assistantFixture{Content: "first", Model: "swe-1-7", CreatedAt: "2023-11-14T22:13:40.000000Z"}), 1700000200, nil)+ addNode(t, db, "s1", 3, assistantMsg("a2", assistantFixture{Content: "second", Model: "swe-1-7", CreatedAt: "2023-11-14T22:14:40.000000Z"}), 1700000300, nil)+ summary, err := CollectOnce(collectOptions(p))+ if err != nil {+ t.Fatalf("second CollectOnce: %v", err)+ }+ after := readEvents(t, p.log)+ if summary.EventsEmitted != 1 {+ t.Fatalf("second sweep emitted %d events, want only the genuinely new row", summary.EventsEmitted)+ }+ var messages []schema.Event+ for _, ev := range after[before:] {+ if ev.Event.Action == "agent.message" {+ messages = append(messages, ev)+ }+ }+ if len(messages) != 1 {+ t.Fatalf("re-emitted agent.message %d times across sweeps", len(messages))+ }+}++func TestPriorNodeRowWithADifferentMessageIDIsNewContent(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, userMsg("u1", "hello", "2023-11-14T22:13:20.000000Z"), 1700000100, nil)+ // A compaction summary names the nodes it condensed but carries its own message_id: that is+ // new content, not a copy, and must be collected.+ addNode(t, db, "s1", 2, assistantMsg("a-new", assistantFixture{Content: "new turn", Model: "swe-1-7", CreatedAt: "2023-11-14T22:14:40.000000Z"}), 1700000200,+ `{"extensions":{"compact/prior_node_ids":[1]}}`)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ msg := findAction(t, readEvents(t, p.log), "agent.message")+ if msg.GenAI == nil {+ t.Fatalf("prior-node row with a new message_id was skipped")+ }+}++func TestPendingToolCallResolvesAcrossSweeps(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{+ Model: "swe-1-7",+ ToolCalls: []map[string]interface{}{toolCallFixture("call-1", "exec", map[string]interface{}{"command": "sleep 1 && echo done"})},+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000100, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("first CollectOnce: %v", err)+ }+ state, err := LoadState(p.state)+ if err != nil {+ t.Fatal(err)+ }+ if state.Sessions["s1"].PendingCalls["call-1"].Command != "sleep 1 && echo done" {+ t.Fatalf("pending call = %#v, want the command kept for a later result", state.Sessions["s1"].PendingCalls)+ }+ // Only the classification fields persist: an edit or write call's arguments can be+ // hundreds of kilobytes and are not needed to map its result.+ if raw, _ := json.Marshal(state.Sessions["s1"].PendingCalls); strings.Contains(string(raw), "arguments") {+ t.Fatalf("pending call persisted full arguments: %s", raw)+ }++ addNode(t, db, "s1", 2, toolMsg("t1", "call-1", "done", "2023-11-14T22:14:50.000000Z", map[string]interface{}{+ "chisel/tool_result_meta": map[string]interface{}{"success": true, "kind": "execute"},+ "chisel/terminal_output": map[string]interface{}{"exit": map[string]interface{}{"exit_code": 0}},+ }), 1700000200, nil)+ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("second CollectOnce: %v", err)+ }+ command := findAction(t, readEvents(t, p.log), "command.executed")+ if command.Command == nil || command.Command.Command != "sleep 1 && echo done" {+ t.Fatalf("result did not resolve the pending call's command: %#v", command.Command)+ }+ state, err = LoadState(p.state)+ if err != nil {+ t.Fatal(err)+ }+ if _, ok := state.Sessions["s1"].PendingCalls["call-1"]; ok {+ t.Fatal("resolved call is still pending")+ }+}++func TestLiveCoverageSuppressesWhatHooksAlreadyRecorded(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ // The hook's first event for this session lands between the prompt and the assistant turn.+ cutover := time.Date(2023, 11, 14, 22, 13, 45, 0, time.UTC)+ addNode(t, db, "s1", 1, userMsg("u1", "early prompt", "2023-11-14T22:13:20.000000Z"), 1700000100, nil)+ addNode(t, db, "s1", 2, assistantMsg("a1", assistantFixture{+ Content: "answer the hook could not see", Model: "swe-1-7",+ Metrics: map[string]interface{}{"input_tokens": 10, "output_tokens": 5},+ ToolCalls: []map[string]interface{}{toolCallFixture("call-1", "exec", map[string]interface{}{"command": "ls"})},+ CreatedAt: "2023-11-14T22:14:00.000000Z",+ }), 1700000200, nil)+ addNode(t, db, "s1", 3, toolMsg("t1", "call-1", "files", "2023-11-14T22:14:10.000000Z", map[string]interface{}{+ "chisel/tool_result_meta": map[string]interface{}{"success": true, "kind": "execute"},+ "chisel/terminal_output": map[string]interface{}{"exit": map[string]interface{}{"exit_code": 0}},+ }), 1700000300, nil)++ opts := collectOptions(p)+ opts.LiveCoverage = map[string]time.Time{"s1": cutover}+ summary, err := CollectOnce(opts)+ if err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ actions := eventActions(readEvents(t, p.log))+ // The prompt predates the cutover, so it is still collected; the tool call and its result+ // postdate it and would double-count the hook's own records. Assistant text, reasoning and+ // usage always emit -- the hook payload carries none of them.+ for _, suppressed := range []string{"tool.invoked", "command.executed"} {+ for _, action := range actions {+ if action == suppressed {+ t.Fatalf("%s was emitted though live hooks cover it: %v", suppressed, actions)+ }+ }+ }+ for _, kept := range []string{"session.started", "prompt.submitted", "agent.message", "token.usage"} {+ found := false+ for _, action := range actions {+ if action == kept {+ found = true+ }+ }+ if !found {+ t.Fatalf("%s missing though hooks cannot see it: %v", kept, actions)+ }+ }+ if summary.EventsSuppressedLive != 2 {+ t.Fatalf("suppressed = %d, want 2 (tool.invoked + command.executed)", summary.EventsSuppressedLive)+ }+}++func TestRejectedToolCallEmitsToolFailedAndApprovalDenied(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{+ Model: "swe-1-7",+ ToolCalls: []map[string]interface{}{toolCallFixture("call-1", "exec", map[string]interface{}{"command": "rm -rf build"})},+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000100, nil)+ addNode(t, db, "s1", 2, toolMsg("t1", "call-1", "rejected by operator", "2023-11-14T22:13:50.000000Z", map[string]interface{}{+ "chisel/tool_result_meta": map[string]interface{}{+ "success": false,+ "kind": "execute",+ "failure_reason": map[string]interface{}{"Rejected": map[string]interface{}{"interactive": true}},+ },+ }), 1700000200, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ events := readEvents(t, p.log)+ // The call carried a command, so its result is command.executed -- failed, medium severity,+ // and typed by the rejection rather than an exit code (there is none: it never ran).+ failed := findAction(t, events, "command.executed")+ if failed.Error == nil || failed.Error.Type != "rejected" || failed.Severity != schema.SeverityMedium {+ t.Fatalf("rejected command = %#v / %#v", failed.Severity, failed.Error)+ }+ denied := findAction(t, events, "approval.denied")+ if denied.Approval == nil || denied.Approval.Decision != "deny" || denied.Approval.Reason != "operator rejected the tool call" {+ t.Fatalf("approval = %#v", denied.Approval)+ }+ if denied.GenAI == nil || denied.GenAI.Tool == nil || denied.GenAI.Tool.Call == nil || denied.GenAI.Tool.Call.ID != "call-1" {+ t.Fatalf("approval gen_ai.tool.call = %#v", denied.GenAI)+ }+}++func TestUserRowWithoutIsUserInputIsNotAPrompt(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ // Compactor inputs and keepalives arrive as user rows without the operator flag.+ addNode(t, db, "s1", 1, chatJSON(map[string]interface{}{+ "role": "user", "message_id": "u-internal", "content": "summarize the conversation so far",+ "metadata": map[string]interface{}{"created_at": "2023-11-14T22:13:20.000000Z"},+ }), 1700000100, nil)+ addNode(t, db, "s1", 2, userMsg("u-real", "real prompt", "2023-11-14T22:14:00.000000Z"), 1700000200, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ prompts := 0+ for _, ev := range readEvents(t, p.log) {+ if ev.Event.Action == "prompt.submitted" {+ prompts+++ if ev.Prompt.Text != "real prompt" {+ t.Fatalf("internal user row became a prompt: %q", ev.Prompt.Text)+ }+ }+ }+ if prompts != 1 {+ t.Fatalf("prompts = %d, want 1", prompts)+ }+}++// The compactor's own rows carry internal labels instead of a model: their content is the+// summary the following devin-rs/summary system row carries, so they must not surface as an+// agent reply -- only their token metrics, with no model and the label kept in raw.+func TestCompactorAssistantRowEmitsUsageWithoutAPersonaOrModel(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{Content: "real answer", Model: "swe-1-7", CreatedAt: "2023-11-14T22:13:40.000000Z"}), 1700000100, nil)+ addNode(t, db, "s1", 2, assistantMsg("comp-1", assistantFixture{+ Content: "<summary>condensed history</summary>",+ Thinking: "condensing",+ Model: "compactor",+ Metrics: map[string]interface{}{"input_tokens": 900, "output_tokens": 40},+ CreatedAt: "2023-11-14T22:14:00.000000Z",+ }), 1700000200, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ events := readEvents(t, p.log)+ agentMessages, reasoning := 0, 0+ var usage schema.Event+ foundUsage := false+ for _, ev := range events {+ switch ev.Event.Action {+ case "agent.message":+ agentMessages+++ case "agent.reasoning":+ reasoning+++ case "token.usage":+ usage = ev+ foundUsage = true+ }+ }+ if agentMessages != 1 || reasoning != 0 {+ t.Fatalf("compactor row surfaced as a reply: %d agent.message, %d agent.reasoning", agentMessages, reasoning)+ }+ if !foundUsage {+ t.Fatal("no token.usage for the compactor row")+ }+ if devinRaw(usage)["generation_model"] != "compactor" {+ t.Fatalf("usage raw generation_model = %v, want the label kept in raw", devinRaw(usage))+ }+ if usage.Model != "" || (usage.GenAI != nil && usage.GenAI.Request != nil && usage.GenAI.Request.Model != "") {+ t.Fatalf("compactor usage carried a model: %#v", usage)+ }+ if usage.GenAI.Usage.InputTokens == nil || *usage.GenAI.Usage.InputTokens != 900 {+ t.Fatalf("compactor usage = %#v", usage.GenAI.Usage)+ }+ // The compactor's label must never become the session's model for later events.+ addNode(t, db, "s1", 3, userMsg("u1", "next", "2023-11-14T22:15:00.000000Z"), 1700000300, nil)+ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("second CollectOnce: %v", err)+ }+ prompt := findAction(t, readEvents(t, p.log), "prompt.submitted")+ if prompt.Model != "swe-1-7" {+ t.Fatalf("prompt model = %q, want the last real model, not the compactor label", prompt.Model)+ }+}++func TestSinceLeavesOldSessionsForALaterSync(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "old", map[string]interface{}{"created_at": 1600000000, "last_activity_at": 1600000000})+ addNode(t, db, "old", 1, userMsg("u-old", "old prompt", "2020-09-13T12:26:40.000000Z"), 1600000000, nil)+ addSession(t, db, "new", nil)+ addNode(t, db, "new", 1, userMsg("u-new", "new prompt", "2023-11-14T22:13:20.000000Z"), 1700000100, nil)++ opts := collectOptions(p)+ opts.Since = time.Unix(1650000000, 0)+ summary, err := CollectOnce(opts)+ if err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ if summary.Sessions != 1 {+ t.Fatalf("sessions = %d, want only the recent one", summary.Sessions)+ }+ state, err := LoadState(p.state)+ if err != nil {+ t.Fatal(err)+ }+ if _, ok := state.Sessions["old"]; ok {+ t.Fatal("the skipped session got a cursor; a later full sync would read past its start")+ }+ if state.Sessions["new"] == nil || !state.Sessions["new"].Started {+ t.Fatalf("recent session was not collected: %#v", state.Sessions)+ }+}++func TestBudgetStopsTheSweepInsideAPageWithoutLosingRows(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ for i := 1; i <= 20; i++ {+ addNode(t, db, "s1", int64(i), userMsg(fmt.Sprintf("u%d", i), strings.Repeat("word ", 200), "2023-11-14T22:13:20.000000Z"), int64(1700000000+i), nil)+ }++ opts := collectOptions(p)+ // Enough room for the session.started event and a handful of prompts, not the rest.+ opts.Budget = writer.NewBudget(12000)+ _, err := CollectOnce(opts)+ if !errors.Is(err, writer.ErrBudgetSpent) {+ t.Fatalf("err = %v, want ErrBudgetSpent", err)+ }+ written := readEvents(t, p.log)+ if len(written) == 0 {+ t.Fatal("the budget allowed nothing at all; the fixture is off")+ }++ // The cursor may only sit past rows whose events are all written. With page-granularity+ // restore it lands on a page boundary at or before the last written row.+ state, err := LoadState(p.state)+ if err != nil {+ t.Fatal(err)+ }+ cursor := state.Sessions["s1"]+ if cursor.LastRowID > int64(len(written)) {+ t.Fatalf("cursor at row %d with only %d events written", cursor.LastRowID, len(written))+ }++ // The follow-up unbudgeted sweep finishes the session; deterministic ids keep re-emitted+ // events from duplicating in the log.+ opts.Budget = nil+ if _, err := CollectOnce(opts); err != nil {+ t.Fatalf("follow-up CollectOnce: %v", err)+ }+ all := readEvents(t, p.log)+ ids := map[string]bool{}+ for _, ev := range all {+ if ids[ev.Event.ID] {+ t.Fatalf("duplicate event id in the log: %s", ev.Event.ID)+ }+ ids[ev.Event.ID] = true+ }+ prompts := 0+ for _, ev := range all {+ if ev.Event.Action == "prompt.submitted" {+ prompts+++ }+ }+ if prompts != 20 {+ t.Fatalf("prompts = %d across both sweeps, want 20", prompts)+ }+}++func TestPrintModeWritesNothingAndLeavesNoState(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ seedBasicSession(t, db, "s1")++ var out bytes.Buffer+ summary, err := CollectOnce(CollectOptions{+ DBPath: p.db, Print: true, Out: &out,+ })+ if err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ if summary.EventsEmitted == 0 {+ t.Fatal("print mode mapped no events")+ }+ lines := 0+ scanner := bufio.NewScanner(&out)+ scanner.Buffer(make([]byte, 1<<20), 4<<20)+ for scanner.Scan() {+ var ev schema.Event+ if err := json.Unmarshal(scanner.Bytes(), &ev); err != nil {+ t.Fatalf("print line is not an event: %v", err)+ }+ lines+++ }+ if lines != summary.EventsEmitted {+ t.Fatalf("printed %d events, summary says %d", lines, summary.EventsEmitted)+ }+ if _, err := os.Stat(p.log); !os.IsNotExist(err) {+ t.Fatal("print mode wrote the runtime log")+ }+ if _, err := os.Stat(p.state); !os.IsNotExist(err) {+ t.Fatal("print mode wrote a cursor file")+ }+}++func TestMissingDatabaseIsNotAnError(t *testing.T) {+ p := newFixturePaths(t)+ summary, err := CollectOnce(collectOptions(p))+ if err != nil {+ t.Fatalf("CollectOnce on a missing db: %v", err)+ }+ if summary.Sessions != 0 || summary.EventsEmitted != 0 || summary.Errors != 0 {+ t.Fatalf("summary = %#v, want zero", summary)+ }+}++func TestCorruptDatabaseIsAnError(t *testing.T) {+ p := newFixturePaths(t)+ if err := os.WriteFile(p.db, []byte("this is not sqlite at all"), 0o600); err != nil {+ t.Fatal(err)+ }+ if _, err := CollectOnce(collectOptions(p)); err == nil {+ t.Fatal("a corrupt database did not error")+ }+}++func TestRowsInAnUncheckpointedWALAreRead(t *testing.T) {+ p := newFixturePaths(t)+ db, err := sql.Open("sqlite", p.db)+ if err != nil {+ t.Fatal(err)+ }+ defer db.Close()+ if _, err := db.Exec(`PRAGMA journal_mode=WAL; PRAGMA wal_autocheckpoint=0;` + devinFixtureDDL); err != nil {+ t.Fatalf("schema: %v", err)+ }+ // The writer stays open, so the rows live only in the WAL while the collector reads.+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, userMsg("u1", "wal prompt", "2023-11-14T22:13:20.000000Z"), 1700000100, nil)++ summary, err := CollectOnce(collectOptions(p))+ if err != nil {+ t.Fatalf("CollectOnce over an uncheckpointed WAL: %v", err)+ }+ if summary.EventsEmitted == 0 {+ t.Fatal("rows still in the WAL were not read")+ }+ findAction(t, readEvents(t, p.log), "prompt.submitted")+}++func TestUnknownColumnsAndJSONKeysAreTolerated(t *testing.T) {+ p := newFixturePaths(t)+ db, err := sql.Open("sqlite", p.db)+ if err != nil {+ t.Fatal(err)+ }+ defer db.Close()+ // Newer Devin builds may add columns to either table and keys to the message JSON.+ _, err = db.Exec(`+CREATE TABLE sessions (id TEXT PRIMARY KEY, working_directory TEXT NOT NULL, backend_type TEXT NOT NULL, model TEXT NOT NULL, agent_mode TEXT NOT NULL, created_at INTEGER NOT NULL, last_activity_at INTEGER NOT NULL, title TEXT, hidden INTEGER NOT NULL DEFAULT 0, metadata TEXT, future_column TEXT);+CREATE TABLE message_nodes (row_id INTEGER PRIMARY KEY AUTOINCREMENT, session_id TEXT NOT NULL, node_id INTEGER NOT NULL, parent_node_id INTEGER, chat_message TEXT NOT NULL, created_at INTEGER NOT NULL, metadata TEXT, another_future INTEGER);`)+ if err != nil {+ t.Fatalf("schema: %v", err)+ }+ if _, err := db.Exec(`INSERT INTO sessions (id, working_directory, backend_type, model, agent_mode, created_at, last_activity_at, future_column)+VALUES ('s1', '/repo', 'local', 'swe-1-7', 'standard', 1700000000, 1700000000, 'x')`); err != nil {+ t.Fatal(err)+ }+ msg := chatJSON(map[string]interface{}{+ "role": "user", "message_id": "u1", "content": "hi", "unknown_field": map[string]interface{}{"deep": true},+ "metadata": map[string]interface{}{"created_at": "2023-11-14T22:13:20.000000Z", "is_user_input": true, "unknown_meta": 1},+ })+ if _, err := db.Exec(`INSERT INTO message_nodes (session_id, node_id, chat_message, created_at) VALUES ('s1', 1, ?, 1700000100)`, msg); err != nil {+ t.Fatal(err)+ }++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce with unknown columns/keys: %v", err)+ }+ findAction(t, readEvents(t, p.log), "prompt.submitted")+}++func TestMissingOptionalColumnsAreTolerated(t *testing.T) {+ p := newFixturePaths(t)+ db, err := sql.Open("sqlite", p.db)+ if err != nil {+ t.Fatal(err)+ }+ defer db.Close()+ // An older store without the optional session columns still collects; the optional fields+ // simply read as NULL.+ _, err = db.Exec(`+CREATE TABLE sessions (id TEXT PRIMARY KEY, working_directory TEXT NOT NULL, backend_type TEXT NOT NULL, model TEXT NOT NULL, agent_mode TEXT NOT NULL, created_at INTEGER NOT NULL, last_activity_at INTEGER NOT NULL);+CREATE TABLE message_nodes (row_id INTEGER PRIMARY KEY AUTOINCREMENT, session_id TEXT NOT NULL, node_id INTEGER NOT NULL, chat_message TEXT NOT NULL, created_at INTEGER NOT NULL);`)+ if err != nil {+ t.Fatalf("schema: %v", err)+ }+ if _, err := db.Exec(`INSERT INTO sessions VALUES ('s1', '/repo', 'local', 'swe-1-7', 'standard', 1700000000, 1700000000)`); err != nil {+ t.Fatal(err)+ }+ if _, err := db.Exec(`INSERT INTO message_nodes (session_id, node_id, chat_message, created_at) VALUES ('s1', 1, ?, 1700000100)`,+ userMsg("u1", "hi", "2023-11-14T22:13:20.000000Z")); err != nil {+ t.Fatal(err)+ }++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce without optional columns: %v", err)+ }+ findAction(t, readEvents(t, p.log), "prompt.submitted")+}++func TestMissingRequiredColumnIsAClearError(t *testing.T) {+ p := newFixturePaths(t)+ db, err := sql.Open("sqlite", p.db)+ if err != nil {+ t.Fatal(err)+ }+ defer db.Close()+ _, err = db.Exec(`+CREATE TABLE sessions (id TEXT PRIMARY KEY);+CREATE TABLE message_nodes (row_id INTEGER PRIMARY KEY AUTOINCREMENT, session_id TEXT NOT NULL, node_id INTEGER NOT NULL, created_at INTEGER NOT NULL);`)+ if err != nil {+ t.Fatalf("schema: %v", err)+ }+ _, err = CollectOnce(collectOptions(p))+ if err == nil || !strings.Contains(err.Error(), "message_nodes.chat_message") {+ t.Fatalf("err = %v, want one naming the missing column", err)+ }+}++// Rows from before message_id existed carry none; their identity is their content, so wholesale+// re-inserts of the same row collapse onto one event id while different legacy rows stay distinct.+func TestLegacyRowsWithoutMessageIDDedupeByContent(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ legacy := chatJSON(map[string]interface{}{+ "role": "user", "content": "legacy prompt", "metadata": map[string]interface{}{+ "created_at": "2023-11-14T22:13:20.000000Z", "is_user_input": true}})+ addNode(t, db, "s1", 1, legacy, 1700000100, nil)+ // An old-style resume re-inserts the same row as a new node with no prior ids.+ addNode(t, db, "s1", 2, legacy, 1700000200, nil)+ addNode(t, db, "s1", 3, chatJSON(map[string]interface{}{+ "role": "user", "content": "different legacy prompt", "metadata": map[string]interface{}{+ "created_at": "2023-11-14T22:13:30.000000Z", "is_user_input": true}}), 1700000300, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ var prompts []schema.Event+ for _, ev := range readEvents(t, p.log) {+ if ev.Event.Action == "prompt.submitted" {+ prompts = append(prompts, ev)+ }+ }+ if len(prompts) != 2 {+ t.Fatalf("prompts = %d, want the duplicate legacy row collapsed and the distinct one kept", len(prompts))+ }+ if prompts[0].Event.ID == prompts[1].Event.ID {+ t.Fatalf("different legacy rows share an event id: %s", prompts[0].Event.ID)+ }+}++// The third re-insert shape: the same assistant turn written again with a new message_id but the+// same request_id. On real data these groups carry identical token metrics, so the request_id+// joins the dedupe ring.+func TestReInsertedAssistantRowWithSameRequestIDIsSkipped(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{+ Content: "hi", Model: "swe-1-7", RequestID: "req-shared",+ Metrics: map[string]interface{}{"input_tokens": 10, "output_tokens": 5},+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000100, nil)+ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("first CollectOnce: %v", err)+ }+ before := len(readEvents(t, p.log))++ // New message_id, same request_id: skipped across sweeps because the request digest persists.+ addNode(t, db, "s1", 2, assistantMsg("a1-copy", assistantFixture{+ Content: "hi", Model: "swe-1-7", RequestID: "req-shared",+ Metrics: map[string]interface{}{"input_tokens": 10, "output_tokens": 5},+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000200, nil)+ // A different request_id is a genuinely different turn and still collects.+ addNode(t, db, "s1", 3, assistantMsg("a2", assistantFixture{+ Content: "next", Model: "swe-1-7", RequestID: "req-other",+ Metrics: map[string]interface{}{"input_tokens": 20, "output_tokens": 7},+ CreatedAt: "2023-11-14T22:14:40.000000Z",+ }), 1700000300, nil)+ summary, err := CollectOnce(collectOptions(p))+ if err != nil {+ t.Fatalf("second CollectOnce: %v", err)+ }+ after := readEvents(t, p.log)+ var usages int+ for _, ev := range after {+ if ev.Event.Action == "token.usage" {+ usages+++ if *ev.GenAI.Usage.InputTokens == 10 {+ continue+ }+ }+ }+ if usages != 2 {+ t.Fatalf("token.usage events = %d, want 2 (the shared-request copy must not recount)", usages)+ }+ if summary.EventsEmitted != 2 || len(after)-before != 2 {+ t.Fatalf("second sweep emitted %d, want only the new request's agent.message+token.usage", summary.EventsEmitted)+ }+}++// Call ids are not unique: some models reuse ids like "read_12" for different calls. Sequential+// resolution still attributes each result to its own invocation because a result consumes the+// pending entry.+func TestSequentialCallsSharingAnIDEachResolveTheirOwnResult(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ shared := "edit:4"+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{+ Model: "swe-1-7",+ ToolCalls: []map[string]interface{}{toolCallFixture(shared, "exec", map[string]interface{}{"command": "make first"})},+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000100, nil)+ addNode(t, db, "s1", 2, toolMsg("t1", shared, "first output", "2023-11-14T22:13:50.000000Z", map[string]interface{}{+ "chisel/tool_result_meta": map[string]interface{}{"success": true, "kind": "execute"},+ "chisel/terminal_output": map[string]interface{}{"exit": map[string]interface{}{"exit_code": 0}},+ }), 1700000200, nil)+ // A later, different call reuses the same id; its result must map to its own command.+ addNode(t, db, "s1", 3, assistantMsg("a2", assistantFixture{+ Model: "swe-1-7",+ ToolCalls: []map[string]interface{}{toolCallFixture(shared, "exec", map[string]interface{}{"command": "make second"})},+ CreatedAt: "2023-11-14T22:14:40.000000Z",+ }), 1700000300, nil)+ addNode(t, db, "s1", 4, toolMsg("t2", shared, "second output", "2023-11-14T22:14:50.000000Z", map[string]interface{}{+ "chisel/tool_result_meta": map[string]interface{}{"success": true, "kind": "execute"},+ "chisel/terminal_output": map[string]interface{}{"exit": map[string]interface{}{"exit_code": 0}},+ }), 1700000400, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ var commands []string+ for _, ev := range readEvents(t, p.log) {+ if ev.Event.Action == "command.executed" {+ commands = append(commands, ev.Command.Command)+ }+ }+ if strings.Join(commands, ",") != "make first,make second" {+ t.Fatalf("commands = %v, want each result mapped to its own call", commands)+ }+}++// A tool result whose call the pending map never saw (invoked before the cursor, or an id+// collision) still names its tool: the row's own telemetry.operation carries it.+func TestUnknownToolResultFallsBackToTelemetryOperation(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ msg := chatJSON(map[string]interface{}{+ "role": "tool", "message_id": "t1", "tool_call_id": "call-never-seen", "content": "listing",+ "metadata": map[string]interface{}{+ "created_at": "2023-11-14T22:13:50.000000Z",+ "telemetry": map[string]interface{}{"operation": "exec"},+ "extensions": map[string]interface{}{"chisel/tool_result_meta": map[string]interface{}{"success": true, "kind": "execute"}},+ }})+ addNode(t, db, "s1", 1, msg, 1700000100, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ // No command is recorded for this call, so it is a tool completion, not a command event:+ // the name still resolves from telemetry.operation.+ completed := findAction(t, readEvents(t, p.log), "tool.completed")+ if completed.Tool == nil || completed.Tool.Name != "exec" {+ t.Fatalf("tool name = %#v, want telemetry.operation", completed.Tool)+ }+}++// Devin's get_output takes a shell_id, not a command: it is an execute-kind call but has no+// command to put on the event, so it completes as a tool event rather than a command one.+func TestGetOutputCallCompletesAsAToolEvent(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{+ Model: "swe-1-7",+ ToolCalls: []map[string]interface{}{toolCallFixture("call-1", "get_output", map[string]interface{}{"shell_id": "abc"})},+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000100, nil)+ addNode(t, db, "s1", 2, toolMsg("t1", "call-1", "chunk of output", "2023-11-14T22:13:50.000000Z", map[string]interface{}{+ "chisel/tool_result_meta": map[string]interface{}{"success": true, "kind": "execute"},+ "chisel/terminal_output": map[string]interface{}{"exit": map[string]interface{}{"exit_code": 0}},+ }), 1700000200, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ events := readEvents(t, p.log)+ for _, ev := range events {+ if ev.Event.Action == "command.executed" {+ t.Fatalf("get_output produced command.executed: %#v", ev)+ }+ }+ completed := findAction(t, events, "tool.completed")+ if completed.Tool == nil || completed.Tool.Name != "get_output" {+ t.Fatalf("tool = %#v, want get_output", completed.Tool)+ }+ if completed.GenAI == nil || completed.GenAI.Tool == nil || completed.GenAI.Tool.Call == nil || completed.GenAI.Tool.Call.Result != "chunk of output" {+ t.Fatalf("tool result did not carry the output: %#v", completed.GenAI)+ }+}++// A subagent's row contributes subagent provenance AND finish_reason to one event's raw.devin:+// a shallow merge would drop one for the other.+func TestSubagentMessageKeepsProvenanceAndFinishReason(t *testing.T) {+ p := newFixturePaths(t)+ db := openFixtureDB(t, p.db)+ addSession(t, db, "s1", nil)+ addNode(t, db, "s1", 1, assistantMsg("a1", assistantFixture{+ Content: "subagent reply", Finish: "stop",+ Extensions: map[string]interface{}{+ "subagent/agent_id": "agent-9",+ "subagent/profile_name": "reviewer",+ "subagent/model": "claude-haiku-5-5-low",+ },+ CreatedAt: "2023-11-14T22:13:40.000000Z",+ }), 1700000100, nil)++ if _, err := CollectOnce(collectOptions(p)); err != nil {+ t.Fatalf("CollectOnce: %v", err)+ }+ msg := findAction(t, readEvents(t, p.log), "agent.message")+ raw := devinRaw(msg)+ if raw["subagent_id"] != "agent-9" || raw["subagent_profile"] != "reviewer" || raw["finish_reason"] != "stop" {+ t.Fatalf("raw.devin = %v, want subagent provenance and finish_reason together", raw)+ }+ if msg.Model != "claude-haiku-5-5-low" {+ t.Fatalf("subagent row model = %q, want the subagent profile model", msg.Model)+ }+}diff --git a/cli/beacon/internal/devinsession/mapper.go b/cli/beacon/internal/devinsession/mapper.gonew file mode 100644index 00000000..00e398fd--- /dev/null+++ b/cli/beacon/internal/devinsession/mapper.go@@ -0,0 +1,971 @@+package devinsession++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"+)++// internalModelLabels are values metadata.generation_model takes on rows the compactor wrote,+// not models a turn ran on. Events derived from them carry no model, the label stays in raw,+// and they never become the session's LastModel.+var internalModelLabels = map[string]bool{"compactor": true, "summarizer": true}++// mapper converts one session's rows into events. It holds the session's cursor and mutates it+// as it works (LastModel, the Recent ring, PendingCalls); the collector journals each row's+// changes and rolls them back when that row's events fail to write, so the cursor never+// describes rows the log does not hold.+type mapper struct {+ session Session+ cursor *Cursor+ out []MappedEvent+ // recent is the membership view of cursor.Recent, rebuilt per mapper.+ recent map[string]bool+ // journal is the current row's undo record; see rowJournal.+ journal *rowJournal+}++// beginRow starts a journal for the row about to be mapped.+func (m *mapper) beginRow() *rowJournal {+ m.journal = &rowJournal{lastModel: m.cursor.LastModel, recentLen: len(m.cursor.Recent)}+ return m.journal+}++// rollbackRow undoes everything mapping the current row changed in the cursor, so a row whose+// events did not all reach the log is retried from exactly the state it began with.+func (m *mapper) rollbackRow(j *rowJournal) {+ m.journal = nil+ m.cursor.LastModel = j.lastModel+ // The ring only ever appends and evicts from the front, so the row's changes undo as:+ // drop the appended tail, put the evicted head back.+ base := m.cursor.Recent[:len(m.cursor.Recent)-len(j.recentAdded)]+ restored := make([]string, 0, j.recentLen)+ restored = append(restored, j.recentEvicted...)+ restored = append(restored, base...)+ m.cursor.Recent = restored+ for _, digest := range j.recentAdded {+ delete(m.recent, digest)+ }+ for _, digest := range j.recentEvicted {+ m.recent[digest] = true+ }+ for _, id := range j.pendingAdded {+ delete(m.cursor.PendingCalls, id)+ }+ for id, call := range j.pendingRemoved {+ m.cursor.PendingCalls[id] = call+ }+}++func newMapper(session Session, cursor *Cursor) *mapper {+ m := &mapper{session: session, cursor: cursor, recent: map[string]bool{}}+ for _, id := range cursor.Recent {+ m.recent[id] = true+ }+ if m.cursor.PendingCalls == nil {+ m.cursor.PendingCalls = map[string]PendingCall{}+ }+ return m+}++// mapSessionStarted is the one event that precedes the rows: it is not tied to a row, so its+// SourceRowID is 0 and collecting it does not move LastRowID.+func (m *mapper) mapSessionStarted(timestampMS int64) MappedEvent {+ ev := m.base(timestampMS, "session.started", "session", schema.SeverityInfo, "Devin CLI session started", m.fallbackModel())+ raw := map[string]interface{}{+ "title": m.session.Title,+ "agent_mode": m.session.AgentMode,+ "backend_type": m.session.BackendType,+ "hidden": m.session.Hidden,+ }+ for key, field := range map[string]string{"total_acu_cost": "total_acu_cost", "total_credit_cost": "total_credit_cost"} {+ if value, ok := mapValue(m.sessionMetadata())[key]; ok && value != nil {+ raw[field] = value+ }+ }+ ev.Raw = map[string]interface{}{"devin": raw}+ return m.append(0, timestampMS, "", "session.started", ev)+}++func (m *mapper) sessionMetadata() map[string]interface{} {+ return decodeObject(m.session.Metadata)+}++// mapRow maps one row into its events, which the collector emits before advancing the cursor+// past the row. priorMessageIDs resolves the node ids the page's compaction copies point back+// at (extensions."compact/prior_node_ids"), which is what separates a resume copy re-inserting+// an old message -- same message_id as a node it names -- from a compaction summary carrying a+// new one.+func (m *mapper) mapRow(row Row, priorMessageIDs map[int64]string) []MappedEvent {+ m.out = nil+ msg := decodeObject(row.ChatMessage)+ if len(msg) == 0 {+ return nil+ }+ // Devin re-inserts history in two shapes, and both dedupe on message identity rather than+ // row_id. Rows from before message_id existed collapse onto a content-derived id (see+ // messageIDOf), because old builds re-inserted those rows wholesale too.+ messageID := messageIDOf(msg)+ // A compaction copy names the nodes it was made from: it is a duplicate only when a node it+ // names carried the same message_id (a summary of new content names priors but keeps its own+ // id). Any other re-insert is caught by the ring of recently seen ids, which the cursor+ // persists so the check survives across sweeps.+ if priors := priorNodeIDs(row); len(priors) > 0 {+ duplicate := false+ for _, nodeID := range priors {+ if priorMessageIDs[nodeID] == messageID {+ duplicate = true+ break+ }+ }+ m.recordMessageID(messageID)+ if duplicate {+ return nil+ }+ } else if m.seenMessageID(messageID) {+ return nil+ }++ role := strings.ToLower(strings.TrimSpace(stringValue(msg["role"])))+ // A third re-insert shape: an assistant row rewritten with a NEW message_id but the same+ // request_id (real data carries groups of them, usage included, so missing this double-counts+ // token.usage). The request id joins the same ring under a "req:" marker, and the message id+ // is still recorded so a later copy of exactly this row is caught either way.+ if role == "assistant" {+ if requestID := stringValue(mapValue(msg["metadata"])["request_id"]); requestID != "" {+ if m.seenMessageID("req:" + requestID) {+ m.recordMessageID(messageID)+ return nil+ }+ m.recordMessageID("req:" + requestID)+ }+ }+ switch role {+ case "user":+ m.mapUser(row, msg)+ case "assistant":+ m.mapAssistant(row, msg)+ case "tool":+ m.mapToolResult(row, msg)+ case "system":+ m.mapSystem(row, msg)+ }+ // Only conversation rows join the ring: system bookkeeping rows (rules, keepalives, the+ // compaction summary marker itself) are not what re-inserts replay.+ switch role {+ case "user", "assistant", "tool":+ m.recordMessageID(messageID)+ }+ return m.out+}++// mapUser emits a prompt only for an operator's own input. Devin also writes user rows the+// compactor feeds itself and keepalive rows, none of which carry metadata.is_user_input.+func (m *mapper) mapUser(row Row, msg map[string]interface{}) {+ meta := mapValue(msg["metadata"])+ if isUserInput, _ := meta["is_user_input"].(bool); !isUserInput {+ return+ }+ text := strings.TrimSpace(stringValue(msg["content"]))+ if text == "" {+ return+ }+ ts := rowTimestampMS(row, meta)+ ev := m.base(ts, "prompt.submitted", "prompt", schema.SeverityInfo, "Devin CLI prompt submitted", m.fallbackModel())+ ev.Prompt = &schema.PromptInfo{Text: text}+ ev.Content = asymptoteobserve.RetainedContent(text, asymptoteobserve.DefaultStringLimit)+ m.append(row.RowID, ts, messageIDOf(msg), "prompt.submitted", ev)+ if info, ok := asymptoteobserve.ParseHandoffMarker(text); ok {+ link := m.base(ts, "session.handoff", "session", schema.SeverityInfo, "Session continued from a "+info.SourceHarness+" session", m.fallbackModel())+ link.Handoff = &info+ m.append(row.RowID, ts, messageIDOf(msg), "prompt.submitted.handoff", link)+ }+}++func (m *mapper) mapAssistant(row Row, msg map[string]interface{}) {+ meta := mapValue(msg["metadata"])+ ext := mapValue(meta["extensions"])+ ts := rowTimestampMS(row, meta)+ messageID := messageIDOf(msg)+ genModel := strings.TrimSpace(stringValue(meta["generation_model"]))++ // Rows the compactor itself produced are bookkeeping, not a reply: their content is the+ // summary text the following devin-rs/summary system row then carries. Reporting it as+ // agent.message would show a summary as something the agent said, so these rows contribute+ // only their token metrics -- with no model, since the label is not one.+ if internalModelLabels[strings.ToLower(genModel)] {+ m.mapUsage(row, msg, ts, "")+ return+ }++ subRaw := subagentRaw(ext)++ // The per-turn model is the point of reading this store: sessions.model only names the+ // latest one. A change between real generation_models is recorded as session.context before+ // the row's other events, so the context event precedes the turn it describes.+ rowModel := m.fallbackModel()+ if realModel := realGenerationModel(genModel); realModel != "" {+ rowModel = realModel+ if m.cursor.LastModel != "" && m.cursor.LastModel != realModel {+ ev := m.base(ts, "session.context", "session", schema.SeverityInfo, "Devin CLI session model changed", realModel)+ ev.Raw = map[string]interface{}{"devin": map[string]interface{}{+ "change": "model_change",+ "from": m.cursor.LastModel,+ "to": realModel,+ }}+ m.append(row.RowID, ts, messageID, "model_change", ev)+ }+ m.cursor.LastModel = realModel+ } else if subModel := asymptoteobserve.NormalizeModelName(stringValue(ext["subagent/model"])); subModel != "" {+ // A subagent row without its own generation_model runs on the subagent's profile model.+ rowModel = subModel+ }++ if thinking := strings.TrimSpace(stringValue(mapValue(msg["thinking"])["thinking"])); thinking != "" {+ ev := m.base(ts, "agent.reasoning", "agent", schema.SeverityInfo, "Devin CLI agent reasoning", rowModel)+ ev.Content = asymptoteobserve.RetainedContent(thinking, asymptoteobserve.DefaultStringLimit)+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {+ genAI.Output = &schema.GenAIOutputInfo{Messages: asymptoteobserve.ReasoningOutputMessages(thinking)}+ })+ ev.Raw = mergeDevin(ev.Raw, subRaw)+ m.append(row.RowID, ts, messageID, "reasoning", ev)+ }++ if text := strings.TrimSpace(stringValue(msg["content"])); text != "" {+ ev := m.base(ts, "agent.message", "agent", schema.SeverityInfo, "Devin CLI assistant message", rowModel)+ ev.Content = asymptoteobserve.RetainedContent(text, asymptoteobserve.DefaultStringLimit)+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {+ genAI.Output = &schema.GenAIOutputInfo{Messages: asymptoteobserve.TextOutputMessages(text)}+ })+ ev.Raw = mergeDevin(ev.Raw, subRaw)+ if finish := stringValue(meta["finish_reason"]); finish != "" {+ ev.Raw = mergeDevin(ev.Raw, map[string]interface{}{"finish_reason": finish})+ }+ m.append(row.RowID, ts, messageID, "agent.message", ev)+ }++ for idx, call := range toolCallsOf(msg) {+ m.mapToolCall(row, msg, ts, call, idx, rowModel, subRaw)+ }++ m.mapUsage(row, msg, ts, rowModel)+}++func (m *mapper) mapToolCall(row Row, msg map[string]interface{}, ts int64, call toolCall, idx int, rowModel string, subRaw map[string]interface{}) {+ messageID := messageIDOf(msg)+ server, tool := mcpForCall(call)+ action, category, message := "tool.invoked", "tool", "Devin CLI tool invoked"+ if server != "" || tool != "" {+ action, category, message = "mcp.tool_invoked", "mcp", "Devin CLI MCP tool invoked"+ }+ ev := m.base(ts, action, category, schema.SeverityInfo, message, rowModel)+ ev.Tool = &schema.ToolInfo{Name: call.Name, Command: call.Command, Path: call.Path}+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {+ genAI.Operation = &schema.GenAIOperationInfo{Name: "execute_tool"}+ genAI.Tool = &schema.GenAIToolInfo{+ Name: call.Name,+ Call: &schema.GenAIToolCallInfo{ID: call.ID, Arguments: call.Arguments},+ }+ })+ if server != "" || tool != "" {+ ev.MCP = &schema.MCPInfo{Server: server, Tool: tool}+ }+ if call.ID != "" {+ // Only the fields a later result needs are persisted -- arguments can be hundreds of+ // kilobytes for an edit or write.+ if old, ok := m.cursor.PendingCalls[call.ID]; ok {+ m.journalPendingRemoved(call.ID, old)+ } else {+ m.journal.pendingAdded = append(m.journal.pendingAdded, call.ID)+ }+ m.cursor.PendingCalls[call.ID] = PendingCall{+ ID: call.ID,+ Name: call.Name,+ Model: rowModel,+ Command: call.Command,+ Path: call.Path,+ MCPServer: server,+ MCPTool: tool,+ }+ }+ suffix := "tool_call." + call.ID+ if call.ID == "" {+ suffix = fmt.Sprintf("tool_call.%d", idx)+ }+ ev.Raw = mergeDevin(ev.Raw, subRaw)+ m.append(row.RowID, ts, messageID, suffix, ev)+}++func (m *mapper) mapUsage(row Row, msg map[string]interface{}, ts int64, model string) {+ meta := mapValue(msg["metadata"])+ metrics := mapValue(meta["metrics"])+ usage := &schema.GenAIUsageInfo{}+ present := false+ if v, ok := numberField(metrics, "input_tokens"); ok {+ // Devin's input count excludes cache reads and writes, the same additive shape+ // gen_ai.usage uses.+ usage.InputTokens = &v+ present = true+ }+ if v, ok := numberField(metrics, "output_tokens"); ok {+ usage.OutputTokens = &v+ present = true+ }+ if v, ok := numberField(metrics, "cache_read_tokens"); ok {+ usage.CacheRead = &schema.GenAIUsageCacheReadInfo{InputTokens: &v}+ present = true+ }+ if v, ok := numberField(metrics, "cache_creation_tokens"); ok {+ usage.CacheCreation = &schema.GenAIUsageCacheCreationInfo{InputTokens: &v}+ present = true+ }+ if !present {+ return+ }+ ev := m.base(ts, "token.usage", "metric", schema.SeverityInfo, "Devin CLI token usage", model)+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) { genAI.Usage = usage })+ raw := subagentRaw(mapValue(meta["extensions"]))+ raw["generation_model"] = stringValue(meta["generation_model"])+ if requestID := stringValue(meta["request_id"]); requestID != "" {+ raw["request_id"] = requestID+ }+ ev.Raw = mergeDevin(ev.Raw, raw)+ m.append(row.RowID, ts, messageIDOf(msg), "usage", ev)+}++func (m *mapper) mapToolResult(row Row, msg map[string]interface{}) {+ meta := mapValue(msg["metadata"])+ ext := mapValue(meta["extensions"])+ ts := rowTimestampMS(row, meta)+ messageID := messageIDOf(msg)+ callID := stringValue(msg["tool_call_id"])++ // The call id is how a result finds its invocation, but it is not trusted as unique: some+ // models mint ids like "read_12" or "functions.read:3" and reuse them inside a session for+ // genuinely different calls. Sequential use still resolves correctly -- a result deletes the+ // pending entry it answers -- and only genuinely unknown ids fall through to the row's own+ // telemetry for a name.+ call, known := m.cursor.PendingCalls[callID]+ if !known {+ call = PendingCall{ID: callID}+ } else {+ m.journalPendingRemoved(callID, call)+ }+ delete(m.cursor.PendingCalls, callID)+ if call.Name == "" {+ // Calls the pending map never saw (invoked before Beacon's cursor, or an id collision)+ // still carry the tool's own name in telemetry.operation.+ call.Name = stringValue(mapValue(meta["telemetry"])["operation"])+ }+ model := call.Model+ if model == "" {+ model = m.fallbackModel()+ }++ resultMeta := mapValue(ext["chisel/tool_result_meta"])+ terminal := mapValue(ext["chisel/terminal_output"])+ kind := strings.ToLower(strings.TrimSpace(stringValue(resultMeta["kind"])))+ failure := failureReason(resultMeta["failure_reason"])++ success := true+ if value, ok := resultMeta["success"]; ok {+ success = truthy(value)+ } else if failure.present {+ success = false+ }+ exitCode, hasExit := intField(mapValue(terminal["exit"]), "exit_code")+ failed := !success || (hasExit && exitCode != 0)++ severity := schema.SeverityInfo+ if failed {+ severity = schema.SeverityMedium+ }++ content := stringValue(msg["content"])+ // Only a call whose recorded arguments carry the command is a command event: Devin's+ // execute-kind tools (get_output and friends) have no command argument, so classifying by+ // kind or name alone produced command.executed events with an empty command on real data.+ switch {+ case call.Command != "":+ ev := m.base(ts, "command.executed", "command", severity, "Devin CLI command executed", model)+ ev.Command = &schema.CommandInfo{Command: call.Command, Output: content}+ if hasExit {+ ev.Command.ExitCode = &exitCode+ }+ if failed {+ ev.Error = &schema.ErrorInfo{Type: errorType(call, failure, hasExit, exitCode)}+ }+ ev.Tool = &schema.ToolInfo{Name: call.Name, Command: call.Command}+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {+ genAI.Tool = &schema.GenAIToolInfo{Name: call.Name, Call: &schema.GenAIToolCallInfo{ID: callID, Result: content}}+ })+ ev.Content = asymptoteobserve.RetainedContent(content, asymptoteobserve.DefaultStringLimit)+ ev.Raw = mergeDevin(ev.Raw, m.resultRaw(kind, resultMeta, terminal, ext, hasExit, exitCode))+ m.append(row.RowID, ts, messageID, "tool_result", ev)+ case kind == "read" || strings.EqualFold(call.Name, "read"):+ ev := m.base(ts, "file.read", "file", severity, "Devin CLI file read", model)+ ev.File = &schema.FileInfo{Path: call.Path, Operation: "read", Language: languageForPath(call.Path)}+ if failed {+ ev.Error = &schema.ErrorInfo{Type: errorType(call, failure, hasExit, exitCode)}+ }+ ev.Tool = &schema.ToolInfo{Name: call.Name, Path: call.Path}+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {+ genAI.Tool = &schema.GenAIToolInfo{Name: call.Name, Call: &schema.GenAIToolCallInfo{ID: callID, Result: content}}+ })+ ev.Content = asymptoteobserve.RetainedContent(content, asymptoteobserve.DefaultStringLimit)+ ev.Raw = mergeDevin(ev.Raw, m.resultRaw(kind, resultMeta, terminal, ext, hasExit, exitCode))+ m.append(row.RowID, ts, messageID, "tool_result", ev)+ case kind == "edit" || strings.EqualFold(call.Name, "edit") || strings.EqualFold(call.Name, "write"):+ ev := m.base(ts, "file.modified", "file", severity, "Devin CLI file modified", model)+ ev.File = &schema.FileInfo{Path: call.Path, Operation: "modify", Language: languageForPath(call.Path)}+ if failed {+ ev.Error = &schema.ErrorInfo{Type: errorType(call, failure, hasExit, exitCode)}+ }+ ev.Tool = &schema.ToolInfo{Name: call.Name, Path: call.Path}+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {+ genAI.Tool = &schema.GenAIToolInfo{Name: call.Name, Call: &schema.GenAIToolCallInfo{ID: callID, Result: content}}+ })+ ev.Content = asymptoteobserve.RetainedContent(content, asymptoteobserve.DefaultStringLimit)+ ev.Raw = mergeDevin(ev.Raw, m.resultRaw(kind, resultMeta, terminal, ext, hasExit, exitCode))+ m.append(row.RowID, ts, messageID, "tool_result", ev)+ default:+ action := "tool.completed"+ message := "Devin CLI tool completed"+ if failed {+ action = "tool.failed"+ message = "Devin CLI tool failed"+ }+ ev := m.base(ts, action, "tool", severity, message, model)+ if failed {+ ev.Error = &schema.ErrorInfo{Type: errorType(call, failure, hasExit, exitCode)}+ }+ if call.MCPServer != "" || call.MCPTool != "" {+ ev.MCP = &schema.MCPInfo{Server: call.MCPServer, Tool: call.MCPTool}+ }+ ev.Tool = &schema.ToolInfo{Name: call.Name, Command: call.Command, Path: call.Path}+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {+ genAI.Tool = &schema.GenAIToolInfo{Name: call.Name, Call: &schema.GenAIToolCallInfo{ID: callID, Result: content}}+ })+ ev.Content = asymptoteobserve.RetainedContent(content, asymptoteobserve.DefaultStringLimit)+ ev.Raw = mergeDevin(ev.Raw, m.resultRaw(kind, resultMeta, terminal, ext, hasExit, exitCode))+ m.append(row.RowID, ts, messageID, "tool_result", ev)+ }++ // An operator's rejection of the call is a second fact the tool result alone does not+ // carry, so it gets its own event the way a denied approval does on the hook path.+ if failure.rejected {+ ev := m.base(ts, "approval.denied", "approval", schema.SeverityMedium, "Devin CLI tool call rejected", model)+ reason := "rejected without prompting"+ if failure.interactive {+ reason = "operator rejected the tool call"+ }+ ev.Approval = &schema.ApprovalInfo{Required: true, Decision: "deny", Reason: reason}+ ev.Tool = &schema.ToolInfo{Name: call.Name}+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {+ genAI.Tool = &schema.GenAIToolInfo{Name: call.Name, Call: &schema.GenAIToolCallInfo{ID: callID, Result: content}}+ })+ ev.Raw = map[string]interface{}{"devin": map[string]interface{}{"rejected_interactive": failure.interactive}}+ m.append(row.RowID, ts, messageID, "approval", ev)+ }+}++// mapSystem emits only for the compaction summary row: the system row whose extensions carry+// devin-rs/summary. Every other system row (prompts, rules, keepalives) is bookkeeping the+// endpoint does not model.+func (m *mapper) mapSystem(row Row, msg map[string]interface{}) {+ meta := mapValue(msg["metadata"])+ ext := mapValue(meta["extensions"])+ summary := mapValue(ext["devin-rs/summary"])+ if len(summary) == 0 {+ return+ }+ ts := rowTimestampMS(row, meta)+ text := strings.TrimSpace(stringValue(msg["content"]))+ ev := m.base(ts, "session.compacted", "session", schema.SeverityInfo, "Devin CLI session compacted", m.fallbackModel())+ ev.Content = asymptoteobserve.RetainedContent(text, asymptoteobserve.DefaultStringLimit)+ raw := map[string]interface{}{"summary_source": stringValue(summary["source"])}+ if text != "" {+ // The content marker is only a marker; the summary text itself goes in raw, as the+ // other collectors do for summaries.+ raw["summary"] = text+ }+ if summarized, ok := intField(nodeMetadata(row), "summarized_from"); ok {+ raw["summarized_from"] = summarized+ }+ ev.Raw = map[string]interface{}{"devin": raw}+ m.append(row.RowID, ts, messageIDOf(msg), "compacted", ev)+}++func (m *mapper) base(timestampMS int64, action, category string, severity schema.Severity, message, model string) schema.Event {+ ev := schema.NewEvent(schema.NewEventOptions{+ Action: action,+ Category: category,+ Severity: severity,+ Message: message,+ Origin: schema.OriginLocal,+ Fidelity: schema.FidelityObserved,+ Harness: schema.HarnessInfo{+ Name: Harness,+ CollectionMethod: schema.CollectionMethodPoll,+ },+ })+ if timestampMS > 0 {+ ev.Timestamp = schema.FormatTimestamp(time.UnixMilli(timestampMS))+ }+ ev.Session = &schema.SessionInfo{ID: m.session.ID, WorkingDirectory: m.session.WorkingDirectory}+ if model != "" {+ ev.Model = model+ ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {+ genAI.Request = &schema.GenAIRequestInfo{Model: model}+ })+ }+ return ev+}++func (m *mapper) append(sourceRowID int64, timestampMS int64, messageID, suffix string, ev schema.Event) MappedEvent {+ // The id is keyed on message_id, not row_id: Devin's resume and compaction copies reuse the+ // message's own id, so a retried sweep and a re-inserted row produce the same event id.+ // Determinism is what the consumers rely on -- the Beacon ingest side and `beacon share`+ // re-uploads treat equal ids as one event; the local writer's own heuristic (matching tool+ // call ids, or near-identical timing across capture paths) is a separate check.+ ev.Event.ID = devinEventID(fmt.Sprintf("%s:%s:%s", m.session.ID, messageID, suffix))+ item := MappedEvent{SourceRowID: sourceRowID, TimestampMS: timestampMS, Event: ev}+ m.out = append(m.out, item)+ return item+}++// fallbackModel is the model for events that carry none of their own: the last real+// generation_model the session produced, else the model the session row advertises.+func (m *mapper) fallbackModel() string {+ if m.cursor.LastModel != "" {+ return m.cursor.LastModel+ }+ return asymptoteobserve.NormalizeModelName(m.session.Model)+}++// subagentRaw is the provenance a subagent's own row carries: which subagent produced it and+// under which profile.+func subagentRaw(ext map[string]interface{}) map[string]interface{} {+ raw := map[string]interface{}{}+ if id := stringValue(ext["subagent/agent_id"]); id != "" {+ raw["subagent_id"] = id+ }+ if profile := stringValue(ext["subagent/profile_name"]); profile != "" {+ raw["subagent_profile"] = profile+ }+ return raw+}++func (m *mapper) resultRaw(kind string, resultMeta, terminal, ext map[string]interface{}, hasExit bool, exitCode int) map[string]interface{} {+ raw := map[string]interface{}{}+ if kind != "" {+ raw["kind"] = kind+ }+ if failure, present := resultMeta["failure_reason"]; present && failure != nil {+ raw["failure_reason"] = failure+ }+ if hasExit {+ raw["exit_code"] = exitCode+ }+ if approved := stringValue(ext["chisel/approved_by"]); approved != "" {+ raw["approved_by"] = approved+ }+ if cwd := stringValue(terminal["cwd"]); cwd != "" {+ raw["cwd"] = cwd+ }+ return raw+}++// messageIDDigest is what the ring stores: 16 hex chars of sha256, which is what the cursor's+// recent_message_ids entries are.+func messageIDDigest(messageID string) string {+ sum := sha256.Sum256([]byte(messageID))+ return hex.EncodeToString(sum[:])[:16]+}++func (m *mapper) seenMessageID(messageID string) bool {+ return m.recent[messageIDDigest(messageID)]+}++func (m *mapper) recordMessageID(messageID string) {+ if messageID == "" {+ return+ }+ digest := messageIDDigest(messageID)+ if m.recent[digest] {+ return+ }+ m.recent[digest] = true+ if m.journal != nil {+ m.journal.recentAdded = append(m.journal.recentAdded, digest)+ }+ m.cursor.Recent = append(m.cursor.Recent, digest)+ if len(m.cursor.Recent) > recentMessageIDLimit {+ for _, dropped := range m.cursor.Recent[:len(m.cursor.Recent)-recentMessageIDLimit] {+ delete(m.recent, dropped)+ if m.journal != nil {+ m.journal.recentEvicted = append(m.journal.recentEvicted, dropped)+ }+ }+ m.cursor.Recent = append([]string(nil), m.cursor.Recent[len(m.cursor.Recent)-recentMessageIDLimit:]...)+ }+}++// journalPendingRemoved remembers a pending call the current row took out of the map -- either+// a result resolving its call or an invocation overwriting one -- so a rollback can restore it.+func (m *mapper) journalPendingRemoved(id string, call PendingCall) {+ if m.journal == nil {+ return+ }+ if m.journal.pendingRemoved == nil {+ m.journal.pendingRemoved = map[string]PendingCall{}+ }+ if _, recorded := m.journal.pendingRemoved[id]; !recorded {+ m.journal.pendingRemoved[id] = call+ }+}++// realGenerationModel is the normalized model a row's generation_model names, or empty when it+// is absent or an internal label rather than a model.+func realGenerationModel(genModel string) string {+ if genModel == "" || internalModelLabels[strings.ToLower(genModel)] {+ return ""+ }+ return asymptoteobserve.NormalizeModelName(genModel)+}++// toolCall is one entry of a chat_message's tool_calls array.+type toolCall struct {+ ID string+ Name string+ Arguments map[string]interface{}+ Command string+ Path string+}++func toolCallsOf(msg map[string]interface{}) []toolCall {+ raw, ok := msg["tool_calls"].([]interface{})+ if !ok {+ return nil+ }+ out := make([]toolCall, 0, len(raw))+ for _, item := range raw {+ entry := mapValue(item)+ if len(entry) == 0 {+ continue+ }+ args := decodeArguments(entry["arguments"])+ call := toolCall{+ ID: firstNonEmpty(stringValue(entry["id"]), stringValue(entry["tool_call_id"])),+ Name: firstNonEmpty(stringValue(entry["name"]), stringValue(mapValue(entry["function"])["name"])),+ Arguments: args,+ Command: stringValue(args["command"]),+ Path: firstNonEmpty(stringValue(args["file_path"]), stringValue(args["path"])),+ }+ out = append(out, call)+ }+ return out+}++// mcpForCall names the MCP server and tool a call reaches: either a native mcp__server__tool+// name or the mcp_call_tool adapter carrying them in its arguments.+func mcpForCall(call toolCall) (server, tool string) {+ if call.Name == "mcp_call_tool" {+ return stringValue(call.Arguments["server_name"]), stringValue(call.Arguments["tool_name"])+ }+ if rest, ok := strings.CutPrefix(call.Name, "mcp__"); ok {+ parts := strings.SplitN(rest, "__", 2)+ if len(parts) == 2 {+ return parts[0], parts[1]+ }+ }+ return "", ""+}++// failureReason is a decoded chisel/tool_result_meta failure_reason: either a plain label+// ("ToolError", "ValidationError", "Canceled") or the rejection object {"Rejected":{"interactive":…}}.+type failureInfo struct {+ present bool+ label string+ rejected bool+ interactive bool+}++func failureReason(value interface{}) failureInfo {+ switch typed := value.(type) {+ case nil:+ return failureInfo{}+ case string:+ return failureInfo{present: true, label: typed}+ default:+ rejected := mapValue(mapValue(typed)["Rejected"])+ if rejected == nil {+ return failureInfo{present: true}+ }+ interactive, _ := rejected["interactive"].(bool)+ return failureInfo{present: true, rejected: true, interactive: interactive}+ }+}++// errorType is the error.type a failed tool result carries: a command's nonzero exit is the+// command failing, an operator rejection is a rejection, the runtime's own label snakes down,+// and anything else is a generic tool error.+func errorType(call PendingCall, failure failureInfo, hasExit bool, exitCode int) string {+ if hasExit && exitCode != 0 && call.Command != "" {+ return "command_failed"+ }+ if failure.rejected {+ return "rejected"+ }+ if failure.label != "" {+ return snakeCase(failure.label)+ }+ return "tool_error"+}++func snakeCase(label string) string {+ var out strings.Builder+ for i, r := range label {+ if r >= 'A' && r <= 'Z' {+ if i > 0 {+ out.WriteByte('_')+ }+ out.WriteRune(r - 'A' + 'a')+ } else {+ out.WriteRune(r)+ }+ }+ return out.String()+}++func languageForPath(path string) string {+ return strings.TrimPrefix(filepath.Ext(path), ".")+}++// rowTimestampMS prefers the message's own metadata.created_at (ISO-8601 with microseconds, when+// the runtime recorded the turn) and falls back to the node's created_at seconds.+func rowTimestampMS(row Row, meta map[string]interface{}) int64 {+ if value := strings.TrimSpace(stringValue(meta["created_at"])); value != "" {+ if ts, err := time.Parse(time.RFC3339Nano, value); err == nil {+ return ts.UnixMilli()+ }+ }+ return row.CreatedAtMS+}++// messageIDOf is the row's dedupe identity. Rows written before message_id existed get a+// content-derived id instead: old builds re-inserted those rows wholesale, so content is the+// right identity -- the same row re-read anywhere is the same row, and two different rows are+// different.+func messageIDOf(msg map[string]interface{}) string {+ if id := stringValue(msg["message_id"]); id != "" {+ return id+ }+ digest := sha256.New()+ for _, part := range []interface{}{+ msg["role"],+ msg["content"],+ msg["tool_call_id"],+ msg["tool_calls"],+ mapValue(msg["thinking"])["thinking"],+ } {+ switch typed := part.(type) {+ case string:+ io.WriteString(digest, typed)+ default:+ data, _ := json.Marshal(part)+ digest.Write(data)+ }+ digest.Write([]byte{0})+ }+ return "legacy-" + hex.EncodeToString(digest.Sum(nil))[:16]+}++// nodeMetadata decodes the row's node metadata column, which may be SQL NULL or the literal+// string "null".+func nodeMetadata(row Row) map[string]interface{} {+ raw := strings.TrimSpace(row.NodeMetadata)+ if raw == "" || raw == "null" {+ return nil+ }+ return decodeObject(raw)+}++// priorNodeIDs reads the compaction linkage out of a row's node metadata.+func priorNodeIDs(row Row) []int64 {+ ext := mapValue(nodeMetadata(row)["extensions"])+ list, _ := ext["compact/prior_node_ids"].([]interface{})+ if len(list) == 0 {+ return nil+ }+ out := make([]int64, 0, len(list))+ for _, item := range list {+ if id, ok := numberToInt64(item); ok {+ out = append(out, id)+ }+ }+ return out+}++// collectPriorNodeIDs gathers the node ids a page's rows point back at, deduped, for one batched+// priorMessageIDs lookup instead of a query per row.+func collectPriorNodeIDs(rows []Row) []int64 {+ seen := map[int64]bool{}+ var out []int64+ for _, row := range rows {+ for _, id := range priorNodeIDs(row) {+ if !seen[id] {+ seen[id] = true+ out = append(out, id)+ }+ }+ }+ return out+}++func decodeObject(raw string) map[string]interface{} {+ raw = strings.TrimSpace(raw)+ if raw == "" || raw == "null" {+ return nil+ }+ var out map[string]interface{}+ if err := json.Unmarshal([]byte(raw), &out); err != nil {+ return nil+ }+ return out+}++func decodeArguments(value interface{}) map[string]interface{} {+ switch typed := value.(type) {+ case string:+ return decodeObject(typed)+ case map[string]interface{}:+ return typed+ default:+ return nil+ }+}++func mapValue(value interface{}) map[string]interface{} {+ if m, ok := value.(map[string]interface{}); ok {+ return m+ }+ return nil+}++func stringValue(value interface{}) string {+ if s, ok := value.(string); ok {+ return s+ }+ return ""+}++func firstNonEmpty(values ...string) string {+ for _, value := range values {+ if strings.TrimSpace(value) != "" {+ return value+ }+ }+ return ""+}++func truthy(value interface{}) bool {+ switch typed := value.(type) {+ case bool:+ return typed+ case float64:+ return typed != 0+ case string:+ return typed == "true" || typed == "1"+ }+ return false+}++// numberField returns a JSON number from an object while telling "absent" and "null" from a+// present zero: Devin's metrics may carry null fields, and a present zero is a real count.+func numberField(m map[string]interface{}, key string) (int64, bool) {+ value, ok := m[key]+ if !ok || value == nil {+ return 0, false+ }+ return numberToInt64(value)+}++func numberToInt64(value interface{}) (int64, bool) {+ switch typed := value.(type) {+ case float64:+ return int64(typed), true+ case int64:+ return typed, true+ case int:+ return int64(typed), true+ case json.Number:+ n, err := typed.Int64()+ return n, err == nil+ }+ return 0, false+}++func intField(m map[string]interface{}, key string) (int, bool) {+ v, ok := numberField(m, key)+ return int(v), ok+}++func withGenAI(genAI *schema.GenAIInfo, edit func(*schema.GenAIInfo)) *schema.GenAIInfo {+ if genAI == nil {+ genAI = &schema.GenAIInfo{}+ }+ edit(genAI)+ return genAI+}++// mergeDevin merges fields into raw's "devin" sub-map: several contributors can add raw fields+// to one event (subagent provenance, finish_reason, usage ids), and a shallow merge would have+// each replace the ones before it.+func mergeDevin(raw map[string]interface{}, fields map[string]interface{}) map[string]interface{} {+ if len(fields) == 0 {+ return raw+ }+ if raw == nil {+ raw = map[string]interface{}{}+ }+ sub, _ := raw["devin"].(map[string]interface{})+ if sub == nil {+ sub = map[string]interface{}{}+ }+ for k, v := range fields {+ sub[k] = v+ }+ raw["devin"] = sub+ return raw+}++var devinIDNamespace = [16]byte{+ 0xfd, 0xee, 0x9b, 0x63, 0xfb, 0x48, 0xa9, 0x7b,+ 0xbe, 0x30, 0x30, 0x22, 0xe2, 0xb2, 0xa5, 0xd1,+}++// devinEventID derives the deterministic, UUIDv5-shaped event id every poll event carries: a+// namespaced SHA-1 over the dedup coordinate, like hermesEventID but with its own namespace so a+// Devin id can never collide with another collector's.+func devinEventID(dedupID string) string {+ digest := sha1.New()+ digest.Write(devinIDNamespace[:])+ 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])+}diff --git a/cli/beacon/internal/devinsession/mapper_test.go b/cli/beacon/internal/devinsession/mapper_test.gonew file mode 100644index 00000000..abf7f67b--- /dev/null+++ b/cli/beacon/internal/devinsession/mapper_test.go@@ -0,0 +1,86 @@+package devinsession++import (+ "fmt"+ "strings"+ "testing"+)++// Event ids are the dedupe contract: keyed on message_id so Devin's re-inserted rows produce the+// same event id, and namespaced so they cannot collide with another collector's.+func TestDevinEventIDIsDeterministicAndUUIDShaped(t *testing.T) {+ coordinate := "quiet-meadow:msg-1:agent.message"+ first, second := devinEventID(coordinate), devinEventID(coordinate)+ if first != second {+ t.Fatalf("devinEventID is not deterministic: %q != %q", first, second)+ }+ if len(first) != 36 || first[8] != '-' || first[13] != '-' || first[18] != '-' || first[23] != '-' {+ t.Fatalf("devinEventID = %q, not UUID shaped", first)+ }+ if first[14] != '5' {+ t.Fatalf("devinEventID = %q, want a v5-shaped id", first)+ }+ if devinEventID("other:msg-1:agent.message") == first {+ t.Fatal("different coordinates produced the same id")+ }+}++func TestMessageIDDigestMatchesTheRingFormat(t *testing.T) {+ digest := messageIDDigest("msg-1")+ if len(digest) != 16 {+ t.Fatalf("digest = %q, want a 16-hex-char prefix", digest)+ }+ if digest != messageIDDigest("msg-1") || digest == messageIDDigest("msg-2") {+ t.Fatal("digests are not stable and distinct")+ }+}++func TestRecentRingCapsAtTheLimit(t *testing.T) {+ m := &mapper{cursor: &Cursor{}, recent: map[string]bool{}}+ for i := 0; i < recentMessageIDLimit+10; i++ {+ m.recordMessageID(fmt.Sprintf("msg-%d", i))+ }+ if len(m.cursor.Recent) != recentMessageIDLimit || len(m.recent) != recentMessageIDLimit {+ t.Fatalf("ring = %d/%d entries, want capped at %d", len(m.cursor.Recent), len(m.recent), recentMessageIDLimit)+ }+ // The oldest entries fell out of both the slice and the membership view.+ if m.seenMessageID("msg-0") {+ t.Fatal("the oldest message id is still in the ring")+ }+ if !m.seenMessageID(fmt.Sprintf("msg-%d", recentMessageIDLimit+9)) {+ t.Fatal("the newest message id is not in the ring")+ }+}++func TestMCPForCall(t *testing.T) {+ server, tool := mcpForCall(toolCall{Name: "mcp__github__create_issue"})+ if server != "github" || tool != "create_issue" {+ t.Fatalf("native mcp name = %q/%q", server, tool)+ }+ server, tool = mcpForCall(toolCall{Name: "mcp_call_tool", Arguments: map[string]interface{}{"server_name": "linear", "tool_name": "get_issue"}})+ if server != "linear" || tool != "get_issue" {+ t.Fatalf("mcp_call_tool = %q/%q", server, tool)+ }+ if server, tool := mcpForCall(toolCall{Name: "exec"}); server != "" || tool != "" {+ t.Fatalf("a plain tool resolved as MCP: %q/%q", server, tool)+ }+}++func TestFailureReasonShapes(t *testing.T) {+ if f := failureReason("ToolError"); !f.present || f.label != "ToolError" || f.rejected {+ t.Fatalf("string failure_reason = %#v", f)+ }+ if f := failureReason(map[string]interface{}{"Rejected": map[string]interface{}{"interactive": true}}); !f.rejected || !f.interactive {+ t.Fatalf("rejected failure_reason = %#v", f)+ }+ if f := failureReason(nil); f.present {+ t.Fatalf("absent failure_reason = %#v", f)+ }+}++func TestDefaultStatePathIsUnderBeaconStateDir(t *testing.T) {+ path := DefaultStatePath()+ if !strings.HasSuffix(path, "devin.json") || !strings.Contains(path, "state") {+ t.Fatalf("DefaultStatePath = %q", path)+ }+}diff --git a/cli/beacon/internal/devinsession/paths.go b/cli/beacon/internal/devinsession/paths.gonew file mode 100644index 00000000..a660a60c--- /dev/null+++ b/cli/beacon/internal/devinsession/paths.go@@ -0,0 +1,53 @@+package devinsession++import (+ "os"+ "path/filepath"+ "runtime"+ "strings"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionfs"+)++// DefaultDBPath is where Devin CLI keeps its session history on this platform.+//+// Devin CLI has no data-dir environment variable: on Windows it writes+// %APPDATA%\devin\cli\sessions.db, and everywhere else it follows the XDG base-directory+// convention -- $XDG_DATA_HOME/devin/cli/sessions.db when that file exists, otherwise+// ~/.local/share/devin/cli/sessions.db. The XDG candidate is only taken when it is already a+// file, so a machine with XDG_DATA_HOME set for other reasons does not send the collector+// to an empty directory while a real store sits under ~/.local/share.+func DefaultDBPath() string {+ home, _ := os.UserHomeDir()+ if runtime.GOOS == "windows" {+ base := strings.TrimSpace(os.Getenv("APPDATA"))+ if base == "" && home != "" {+ base = filepath.Join(home, "AppData", "Roaming")+ }+ if base == "" {+ base = os.TempDir()+ }+ return filepath.Join(base, "devin", "cli", "sessions.db")+ }+ if xdg := strings.TrimSpace(os.Getenv("XDG_DATA_HOME")); xdg != "" {+ candidate := filepath.Join(xdg, "devin", "cli", "sessions.db")+ // Read through sessionfs like every other store path, so a system backfill confined+ // to the console user's home sees the same answer it would later get opening the file.+ if _, err := sessionfs.Stat(candidate); err == nil {+ return candidate+ }+ }+ if home == "" {+ home = os.TempDir()+ }+ return filepath.Join(home, ".local", "share", "devin", "cli", "sessions.db")+}++// DefaultStatePath is the per-user cursor file; same fallback as the other session collectors.+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", "devin.json")+}diff --git a/cli/beacon/internal/endpoint/harness/harness.go b/cli/beacon/internal/endpoint/harness/harness.goindex 6eb02a4b..45af903e 100644--- a/cli/beacon/internal/endpoint/harness/harness.go+++ b/cli/beacon/internal/endpoint/harness/harness.go@@ -13,6 +13,7 @@ import ("github.com/asymptote-labs/agent-beacon/cli/beacon/internal/claudesession""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/codexsession"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/devinsession""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/hooks""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/integrations/cowork""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/hermessession"@@ -314,13 +315,17 @@ func DiscoverDevin() Harness {if !h.Detected && (dirExists(filepath.Join(home, ".config", "devin")) || dirExists(".devin")) {h.Detected = true}+ if dbPath := devinsession.DefaultDBPath(); fileExists(dbPath) {+ h.Detected = true+ h.Message = fmt.Sprintf(" Session history found at %s; read it with beacon endpoint devin sync.", dbPath)+ }if fileExists(h.ConfigPath) {status, msg := devinStatus(h.ConfigPath, "devin", "devin-cli")h.TelemetryStatus = status- h.Message = msg+ h.Message = msg + h.Message} else {h.TelemetryStatus = TelemetryMissing- h.Message = "Devin CLI endpoint hooks were not found"+ h.Message = "Devin CLI endpoint hooks were not found" + h.Message}return h}diff --git a/cli/beacon/internal/sessionfs/collectors_test.go b/cli/beacon/internal/sessionfs/collectors_test.goindex c9549680..f07994a1 100644--- a/cli/beacon/internal/sessionfs/collectors_test.go+++ b/cli/beacon/internal/sessionfs/collectors_test.go@@ -16,7 +16,7 @@ import (var backfillCollectors = []string{"claudesession", "codexsession", "cursorsession", "opencodesession", "copilotsession","clinesession", "factorysession", "pisession", "primesession", "hermessession",- "dshsession", "fxsession", "groksession", "openclawsession",+ "dshsession", "fxsession", "groksession", "openclawsession", "devinsession",}// directReads are the calls that would read a session store around the confinement.@@ -35,6 +35,7 @@ var allowedDirectReads = map[string]bool{"cursorsession.openSQLiteReadOnly.sql.Open": true,"hermessession.openSQLiteReadOnly.sql.Open": true,"opencodesession.openSQLiteReadOnly.sql.Open": true,+ "devinsession.openSQLiteReadOnly.sql.Open": true,// Existence checks on project paths named inside a transcript, to infer the workspace. They// read no content and the paths are usually outside the home."cursorsession.normalizeDirectoryCandidate.os.Stat": true,diff --git a/cli/beacon/internal/tokens/coverage.go b/cli/beacon/internal/tokens/coverage.goindex e076d7d2..ae82c9ab 100644--- a/cli/beacon/internal/tokens/coverage.go+++ b/cli/beacon/internal/tokens/coverage.go@@ -133,8 +133,10 @@ var usageExpectation = map[string]struct {// and NormalizeHarnessName passes all three through unchanged. Keying only "devin" left the// other two unrecognized, which classified a Devin session that correctly reported no tokens// as silent -- the one status this table exists to keep clean.- "devin": {ExpectNone, "hook payloads carry no token counts"},- "devin-cli": {ExpectNone, "hook payloads carry no token counts"},+ "devin": {ExpectNone, "hook payloads carry no token counts"},+ // Devin CLI's committed session store carries per-message metrics and the per-turn model,+ // which the hook payload never sees -- same shape as DeepSeek Harness above.+ "devin-cli": {ExpectReported, "beacon endpoint devin sync reads per-message token metrics; hook payloads carry none"},"devin-desktop": {ExpectNone, "Cascade/Windsurf hook payloads carry no token counts"},"muse_code": {ExpectNone, "usage arrives on PostLLMCall, which Beacon does not subscribe to"},"qwen_code": {ExpectNone, "Stop reports context occupancy, not per-call usage"},diff --git a/cli/beacon/internal/tokens/coverage_test.go b/cli/beacon/internal/tokens/coverage_test.goindex 1791bf6c..b69a31f2 100644--- a/cli/beacon/internal/tokens/coverage_test.go+++ b/cli/beacon/internal/tokens/coverage_test.go@@ -70,9 +70,9 @@ func TestCoverageFlagsARuntimeThatShouldHaveReportedUsage(t *testing.T) {// Cursor contributing nothing is correct behavior. Reporting it as silent every week would train// a reader to ignore the column that matters.func TestCoverageDoesNotFlagRuntimesThatCannotReportUsage(t *testing.T) {- events := []schema.Event{plainEvent("cursor"), plainEvent("qwen_code"), plainEvent("muse_code"), plainEvent("devin-cli"), plainEvent("devin-desktop")}- report := Coverage(events, []string{"cursor", "qwen_code", "muse_code", "devin-cli", "devin-desktop"})- for _, harness := range []string{"cursor", "qwen_code", "muse_code", "devin-cli", "devin-desktop"} {+ events := []schema.Event{plainEvent("cursor"), plainEvent("qwen_code"), plainEvent("muse_code"), plainEvent("devin"), plainEvent("devin-desktop")}+ report := Coverage(events, []string{"cursor", "qwen_code", "muse_code", "devin", "devin-desktop"})+ for _, harness := range []string{"cursor", "qwen_code", "muse_code", "devin", "devin-desktop"} {line := lineFor(t, report, harness)if line.Status != CoverageNotInstrumented {t.Errorf("%s status = %q, want not_instrumented", harness, line.Status)@@ -233,18 +233,29 @@ func TestEveryScannedRuntimeHasAnExpectation(t *testing.T) {}}-// The Devin regression in report form: a Devin CLI or Devin Desktop session that reports no-// tokens must not be alerted on.-func TestCoverageDoesNotFlagAnyDevinSpelling(t *testing.T) {- for _, harness := range []string{"devin", "devin-cli", "devin-desktop"} {+// The Devin regression in report form: a Devin spelling whose only collection path cannot carry+// token counts must not be alerted on. Plain "devin" and "devin-desktop" are hook-only and stay+// not_instrumented; "devin-cli" is expected to report once `beacon endpoint devin sync` has run,+// so a covered session with no sync yet is silent and the reason names the fix.+func TestCoverageFlagsDevinSpellingsByWhatBeaconCanRead(t *testing.T) {+ for _, harness := range []string{"devin", "devin-desktop"} {report := Coverage([]schema.Event{plainEvent(harness)}, []string{harness})if got := lineFor(t, report, harness).Status; got != CoverageNotInstrumented {- t.Errorf("%s status = %q, want not_instrumented -- Devin reports no token counts", harness, got)+ t.Errorf("%s status = %q, want not_instrumented -- these hooks report no token counts", harness, got)}if report.Silent != 0 {t.Errorf("%s produced silent=%d, want 0", harness, report.Silent)}}++ report := Coverage([]schema.Event{plainEvent("devin-cli")}, []string{"devin-cli"})+ line := lineFor(t, report, "devin-cli")+ if line.Status != CoverageSilent {+ t.Fatalf("devin-cli status = %q, want silent until the session sync runs", line.Status)+ }+ if !strings.Contains(line.Reason, "beacon endpoint devin sync") {+ t.Fatalf("devin-cli reason = %q, want it to name the sync that reports usage", line.Reason)+ }}// The two harnesses added to main while this branch was open, pinned so neither becomes adiff --git a/docs/changelog/cli.mdx b/docs/changelog/cli.mdxindex d0a91379..8eedbf3d 100644--- a/docs/changelog/cli.mdx+++ b/docs/changelog/cli.mdx@@ -11,6 +11,7 @@ Stay up to date with the latest changes to the Agent Beacon CLI.## Unreleased+- **Devin CLI session history sync** · `beacon endpoint devin sync` reads Devin CLI's committed SQLite session store locally and read-only into the runtime log, adding what the live hooks cannot see: assistant replies, reasoning, per-message token usage, the per-turn model with model-change events, compaction summaries, and `approval.denied` for rejected tool calls. Sessions the hooks already cover get only those additions, so nothing is counted twice, and the install backfill includes Devin CLI. Verified on macOS; the Linux and Windows paths are best effort. See [Devin CLI](/runtimes/devin#session-database-sync).- **Dashboard output tokens count reasoning once** · The local dashboard's token view added reasoning tokens to output, but `gen_ai.usage.output_tokens` already includes them, so models that report reasoning had their output and total tokens overstated. Output and the total now count reasoning once, the split bar no longer shows reasoning as a separate segment, and a note below it gives the reasoning count included in output. `beacon token-usage` already counted it this way.---diff --git a/docs/cli/endpoint-devin.mdx b/docs/cli/endpoint-devin.mdxnew file mode 100644index 00000000..c8215269--- /dev/null+++ b/docs/cli/endpoint-devin.mdx@@ -0,0 +1,98 @@+---+title: "beacon endpoint devin"+description: "Collect endpoint telemetry from Devin CLI's local session database"+---++## Command overview++`beacon endpoint devin` reads the committed SQLite session store Devin CLI keeps+at `~/.local/share/devin/cli/sessions.db` (`%APPDATA%\devin\cli\sessions.db`+on Windows) and maps it into Beacon+[endpoint events](/concepts/core-concepts#endpoint-event).++```bash title="Command syntax"+beacon endpoint devin [command]+```++This complements the live [Devin CLI hooks](/runtimes/devin). Hooks remain the+real-time integration; the sync adds what the hook payload cannot carry:+assistant replies, reasoning, per-message token metrics and the per-turn model.+See [Session database sync](/runtimes/devin#session-database-sync).++Every event from this command is marked+`harness.collection_method=poll`. Beacon reads committed records after the fact,+so this path cannot hold, approve, deny, or delay a tool call. Reading is local,+offline and read-only.++## Commands++| Command | Description |+|---------|-------------|+| `beacon endpoint devin sync` | Read new Devin CLI session rows into the runtime log |+| `beacon endpoint devin status` | Show Devin CLI sessions on this machine and how much has been collected |++## Flags++Both subcommands accept:++| Flag | Description |+|------|-------------|+| `--db <path>` | Devin CLI session database. Defaults to `~/.local/share/devin/cli/sessions.db`, `%APPDATA%\devin\cli\sessions.db` on Windows |+| `--state <path>` | Collector cursor file. Defaults to `~/.beacon/endpoint/state/devin.json` |+| `--json` | Print the result as JSON |+| `--user` | Use per-user endpoint paths. Enabled by default |+| `--system` | Use system endpoint paths |++`sync` also accepts:++| Flag | Description |+|------|-------------|+| `--log-path <path>` | Runtime JSONL log path. Defaults to the resolved endpoint log |+| `--print` | Print mapped events as JSON without writing them or advancing the cursor |+| `--watch` | Sweep continuously on `--interval` |+| `--interval <duration>` | Sweep interval for `--watch`. Defaults to `1m`, with a 5-second floor |+| `--rotate-bytes <bytes>` | Rotate the runtime log at this many bytes while this sync writes it. Defaults to 10485760, the size every Beacon writer uses; must not be smaller |++## Sweeping++```bash title="Sweep once"+beacon endpoint devin sync+```++```bash title="Preview without writing or advancing the cursor"+beacon endpoint devin sync --print+```++```bash title="Sweep continuously"+beacon endpoint devin sync --watch --interval 5m+```++Nothing schedules a sweep automatically. Run `sync` from the scheduler used on+the endpoint, or leave `--watch` running in the account that owns the Devin CLI+data directory.++When the runtime log shows live hook coverage for a session, the sweep skips the+prompts, tool calls and results and session start the hooks already recorded and+keeps only the events the hook payload cannot carry, so nothing is counted+twice.++## Status++```bash title="Show sessions and collection progress"+beacon endpoint devin status+```++The cursor prevents scheduled sweeps from re-appending a session's complete+history. Each source record always maps to the same deterministic event id, so+a re-read after a cursor reset appends events with the ids they had before.++## Related++<Columns cols={2}>+ <Card title="Devin CLI" icon="code" href="/runtimes/devin">+ Hook installation, telemetry coverage, and the session database sync.+ </Card>+ <Card title="beacon endpoint hooks" icon="plug" href="/cli/hooks">+ Install and inspect the live Devin CLI hook integration.+ </Card>+</Columns>diff --git a/docs/cli/endpoint-install.mdx b/docs/cli/endpoint-install.mdxindex f74e249d..7f10d0af 100644--- a/docs/cli/endpoint-install.mdx+++ b/docs/cli/endpoint-install.mdx@@ -61,8 +61,8 @@ After a successful install, user or system mode, Beacon reads the last 30 days oruntime's own session store into the runtime log, so the [local dashboard](/cli/dashboard) hasyour recent sessions before any agent runs again. It is the same sweep each runtime's sync commandruns (`beacon endpoint claude sync`, `codex sync`, `cursor sync`, `opencode sync`, `copilot sync`,-`cline sync`, `factory sync`, `pi sync`, `prime sync`, `hermes sync`, `dsh sync`, `fx sync`,-`grok sync`, and `integrations openclaw sessions sync`), and its events carry+`cline sync`, `factory sync`, `pi sync`, `prime sync`, `hermes sync`, `devin sync`, `dsh sync`,+`fx sync`, `grok sync`, and `integrations openclaw sessions sync`), and its events carry`harness.collection_method=poll` like theirs. It is local only and never touches the network.The backfill is bounded so a machine with a lot of history cannot crowd live telemetry out of thediff --git a/docs/docs.json b/docs/docs.jsonindex a1fe797b..219c2015 100644--- a/docs/docs.json+++ b/docs/docs.json@@ -334,6 +334,7 @@"cli/endpoint-opencode","cli/endpoint-pi","cli/endpoint-copilot",+ "cli/endpoint-devin","cli/endpoint-dsh","cli/endpoint-fx","cli/endpoint-traces",diff --git a/docs/runtimes/devin.mdx b/docs/runtimes/devin.mdxindex 162d9fab..0eda7eda 100644--- a/docs/runtimes/devin.mdx+++ b/docs/runtimes/devin.mdx@@ -5,7 +5,7 @@ description: "Beacon support details for Devin CLI endpoint telemetry"## Runtime overview-Beacon supports Devin CLI through native Devin hook payloads sent to `beacon-hooks`.+Beacon supports Devin CLI through native Devin hook payloads sent to `beacon-hooks`, and by reading Devin CLI's committed session database for history and per-message detail the hooks do not carry.## Prerequisites@@ -21,11 +21,20 @@ Before enabling Devin CLI hooks, make sure:Devin CLI invokes Beacon's hook adapter from hook entries in Devin configuration. The adapter writes normalized endpoint events to the configured runtime JSONL log.+Beacon can also read Devin CLI's committed SQLite session store:++```bash title="Sync Devin CLI sessions"+beacon endpoint devin status+beacon endpoint devin sync+```++The sync path reads `sessions.db` locally, offline and read-only. It writes mapped events with `harness.collection_method=poll`, so it can recover historical sessions or sessions that ran before hooks were installed, but it cannot hold, approve, deny, or delay tool calls. See [Session database sync](#session-database-sync).+## Discovery and status-Beacon detects Devin CLI through the `devin` executable, `~/.config/devin`, or `./.devin`, then checks Beacon hook configuration.+Beacon detects Devin CLI through the `devin` executable, `~/.config/devin`, `./.devin`, or the session database, then checks Beacon hook configuration.-Use `beacon endpoint hooks status --harness devin-cli` to inspect Devin CLI hook status.+Use `beacon endpoint hooks status --harness devin-cli` to inspect Devin CLI hook status. Use `beacon endpoint devin status` to inspect committed local sessions and how much Beacon has synced.## Install or configuration support@@ -47,10 +56,29 @@ beacon endpoint hooks install --harness devin-cli --level project|------|---------|| Prompt telemetry | Supported through `UserPromptSubmit` hooks || Command, tool, and file telemetry | Supported for session, prompt, pre-tool, post-tool, permission request, stop, session-end, approval, and file telemetry where Devin CLI exposes payloads |-| Approvals | Permission requests are recorded as `approval.requested`. Devin CLI does not report the operator's answer to hooks, so Beacon does not record one, and the hook returns no decision: Devin asks the operator as it would without Beacon |+| Agent responses and reasoning | `agent.message` and `agent.reasoning` from the session database sync; the hook payload does not carry them |+| Token usage | `token.usage` per assistant message from the session database sync: input, output, cache read, and cache creation counts. Devin records credits/ACUs, not dollars, so no USD cost is reported |+| Model attribution | The session database sync stamps the per-turn `generation_model` on every event and records `session.context` model-change events; the hook payload carries no model |+| Approvals | Permission requests are recorded as `approval.requested`. Devin CLI does not report the operator's answer to hooks, so the hook records no decision and returns none: Devin asks the operator as it would without Beacon. The session database does record rejections, so the sync reports `approval.denied` for a rejected tool call, whether the operator rejected it or it was rejected without prompting; it does not record interactive approvals, so none are reported |+| Historical session sync | `beacon endpoint devin sync` reads the committed session database into the runtime log || Local JSONL and dashboard | Supported || MDM deployment | Supported for the endpoint agent. Devin CLI hooks are installed separately in the logged-in user's context or project context |+## Session database sync++`beacon endpoint devin sync` reads the session store Devin CLI commits locally:++- macOS and Linux: `~/.local/share/devin/cli/sessions.db` (`$XDG_DATA_HOME/devin/cli/sessions.db` when it exists)+- Windows: `%APPDATA%\devin\cli\sessions.db`++The macOS path is verified against Devin CLI 3000.11.92; the Linux and Windows paths follow Devin's documented CLI data directory but are best effort, not yet verified.++The store is read read-only, in pages, never loaded whole. A cursor in `~/.beacon/endpoint/state/devin.json` (beside the system runtime log as `devin-state.json` in system mode) advances by `message_nodes.row_id`, so repeated sweeps read only new rows. `beacon endpoint devin sync --print` previews the mapped events without writing them or advancing the cursor, and `--watch` sweeps continuously on `--interval`.++Over the hooks the sync adds assistant replies (`agent.message`), reasoning (`agent.reasoning`), per-message token usage (`token.usage`), the per-turn model on every event plus `session.context` model-change events, compaction summaries (`session.compacted`), and `approval.denied` when a tool call was rejected. When the runtime log shows live hook coverage for a session, the sync skips the prompts, tool calls and results and session start the hooks already recorded and adds only what the hook payload cannot carry, so nothing is counted twice; sessions from before hooks were installed are collected whole. Devin re-inserts history on resume and compaction, which the sync dedupes by message id and request id.++For large stores, the runtime log's retention guard and `--rotate-bytes` behave as for `beacon endpoint claude sync`. A system-mode install backfill copies SQLite stores only up to 512 MiB; a larger Devin store is skipped there and named in the report, so run `beacon endpoint devin sync` as the user. The install backfill includes Devin CLI like the other session stores (30-day window, shared byte budget).+## Deployment notesBeacon writes Devin CLI user-level hooks under the `hooks` key in `~/.config/devin/config.json`. Devin CLI project-level installs write the standalone hook map to `./.devin/hooks.v1.json`.