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
Emit Devin session.ended once per observed terminal episode
shukieshah175ff8173e27
diff --git a/cli/beacon/internal/devincloud/mapper.go b/cli/beacon/internal/devincloud/mapper.goindex 4363e8f3..a4e74fa4 100644--- a/cli/beacon/internal/devincloud/mapper.go+++ b/cli/beacon/internal/devincloud/mapper.go@@ -1,7 +1,6 @@package devincloudimport (- "fmt""strings""time"@@ -54,16 +53,19 @@ func IsFinal(status string) bool {return finalStatuses[strings.ToLower(strings.TrimSpace(status))]}-// MapSession converts a session and its messages into ordered Beacon events:-// session.started, one event per message (prompt.submitted / agent.message),-// and session.ended when the session has reached a terminal status.+// MapSession converts a session and its messages into ordered stream events:+// session.started plus one event per message (prompt.submitted / agent.message),+// each with a stable dedup id (synthetic for started, event_id for messages).+//+// session.ended is intentionally NOT produced here. Whether to emit an end+// depends on observed status transitions (a suspended session may resume, and+// its updated_at can bump without a resume), which only the orchestrator knows.+// See EndedEvent and the transition handling in PullOnce.func MapSession(s Session, msgs []Message) []MappedEvent {- var out []MappedEvent-- out = append(out, MappedEvent{+ out := []MappedEvent{{DedupID: s.SessionID + ":started",Event: sessionLifecycleEvent(s, "session.started", "Devin Cloud session started", s.CreatedAt),- })+ }}for _, m := range msgs {switch strings.ToLower(m.Source) {@@ -80,19 +82,15 @@ func MapSession(s Session, msgs []Message) []MappedEvent {out = append(out, MappedEvent{DedupID: m.EventID, Event: ev})}}-- if IsTerminal(s.Status) {- // Key the ended event by updated_at: a suspended session may resume and- // suspend again, and each suspension is a distinct end-of-activity. A- // fixed ":ended" key would suppress every end after the first.- out = append(out, MappedEvent{- DedupID: fmt.Sprintf("%s:ended:%d", s.SessionID, s.UpdatedAt),- Event: sessionLifecycleEvent(s, "session.ended", "Devin Cloud session ended", s.UpdatedAt),- })- }return out}+// EndedEvent builds the session.ended event for a terminal session, carrying+// final status/PR/ACU/duration metadata.+func EndedEvent(s Session) schema.Event {+ return sessionLifecycleEvent(s, "session.ended", "Devin Cloud session ended", s.UpdatedAt)+}+// sessionLifecycleEvent builds a started/ended event carrying session metadata.func sessionLifecycleEvent(s Session, action, message string, tsUnix int64) schema.Event {ev := baseEvent(s, action, "session", schema.SeverityInfo, tsUnix)diff --git a/cli/beacon/internal/devincloud/mapper_test.go b/cli/beacon/internal/devincloud/mapper_test.goindex e10a1d85..d829931c 100644--- a/cli/beacon/internal/devincloud/mapper_test.go+++ b/cli/beacon/internal/devincloud/mapper_test.go@@ -18,19 +18,19 @@ func TestMapSessionProducesLifecycleAndMessages(t *testing.T) {}mapped := MapSession(s, msgs)- if len(mapped) != 4 {- t.Fatalf("got %d events, want 4 (started, prompt, agent, ended)", len(mapped))+ if len(mapped) != 3 {+ t.Fatalf("got %d events, want 3 (started, prompt, agent); ended is emitted by the orchestrator", len(mapped))}- wantActions := []string{"session.started", "prompt.submitted", "agent.message", "session.ended"}+ wantActions := []string{"session.started", "prompt.submitted", "agent.message"}for i, want := range wantActions {if mapped[i].Event.Event.Action != want {t.Fatalf("event %d action = %q, want %q", i, mapped[i].Event.Event.Action, want)}}- // Dedup ids: lifecycle synthetic (ended keyed by updated_at), messages use event_id.- wantIDs := []string{"sess1:started", "e1", "e2", "sess1:ended:1600"}+ // Dedup ids: lifecycle synthetic for started, event_id for messages.+ wantIDs := []string{"sess1:started", "e1", "e2"}for i, want := range wantIDs {if mapped[i].DedupID != want {t.Fatalf("event %d dedup id = %q, want %q", i, mapped[i].DedupID, want)@@ -69,9 +69,20 @@ func TestMapSessionProducesLifecycleAndMessages(t *testing.T) {if mapped[0].Event.Timestamp != "1970-01-01T00:16:40Z" {t.Fatalf("started ts = %q, want session created_at", mapped[0].Event.Timestamp)}+}- // Ended carries metadata.- ended := mapped[3].Event+func TestEndedEventCarriesMetadata(t *testing.T) {+ s := Session{+ SessionID: "sess1", Status: "suspended", UserID: "user-1",+ CreatedAt: 1000, UpdatedAt: 1600, AcusConsumed: 2.5,+ }+ ended := EndedEvent(s)+ if ended.Event.Action != "session.ended" {+ t.Fatalf("action = %q, want session.ended", ended.Event.Action)+ }+ if err := ended.Validate(); err != nil {+ t.Fatalf("ended failed Validate: %v", err)+ }devin, _ := ended.Raw["devin"].(map[string]interface{})if devin == nil || devin["acus_consumed"] != 2.5 {t.Fatalf("ended raw.devin = %+v", ended.Raw)@@ -81,26 +92,12 @@ func TestMapSessionProducesLifecycleAndMessages(t *testing.T) {}}-func TestMapSessionReEndsOnNewSuspension(t *testing.T) {- // Same session suspended at two different updated_at values must produce two- // distinct ended dedup ids so a re-suspension after resume isn't suppressed.- first := MapSession(Session{SessionID: "s", Status: "suspended", UpdatedAt: 100}, nil)- second := MapSession(Session{SessionID: "s", Status: "suspended", UpdatedAt: 200}, nil)- id1 := first[len(first)-1].DedupID- id2 := second[len(second)-1].DedupID- if id1 == id2 {- t.Fatalf("re-suspension produced same ended id %q; would be deduped away", id1)- }- if id1 != "s:ended:100" || id2 != "s:ended:200" {- t.Fatalf("ended ids = %q,%q, want s:ended:100, s:ended:200", id1, id2)- }-}--func TestMapSessionNonTerminalHasNoEnded(t *testing.T) {- s := Session{SessionID: "sess2", Status: "working", UserID: "u", CreatedAt: 10}- mapped := MapSession(s, nil)- if len(mapped) != 1 || mapped[0].Event.Event.Action != "session.started" {- t.Fatalf("working session should only emit session.started, got %d events", len(mapped))+func TestMapSessionHasNoEndedRegardlessOfStatus(t *testing.T) {+ for _, st := range []string{"working", "suspended", "finished"} {+ mapped := MapSession(Session{SessionID: "s", Status: st, CreatedAt: 10}, nil)+ if len(mapped) != 1 || mapped[0].Event.Event.Action != "session.started" {+ t.Fatalf("status %q: MapSession should emit only session.started, got %d events", st, len(mapped))+ }}}diff --git a/cli/beacon/internal/devincloud/pull.go b/cli/beacon/internal/devincloud/pull.goindex c5528793..b42d8cb7 100644--- a/cli/beacon/internal/devincloud/pull.go+++ b/cli/beacon/internal/devincloud/pull.go@@ -126,8 +126,33 @@ func processSession(ctx context.Context, client *Client, s Session, ss *SessionSsum.EventsEmitted++}+ // session.ended is emitted once per observed terminal episode: only when the+ // session is terminal and we have not already emitted an end since it was+ // last seen active. Observing a non-terminal status resets the flag so a+ // genuine resume-then-end emits a fresh end. This avoids both missing+ // re-ends after a resume and spurious ends on metadata-only updated_at bumps.+ terminal := IsTerminal(s.Status)+ if terminal {+ if !ss.EndedEmitted {+ if err := emit(EndedEvent(s), opts); err != nil {+ return err+ }+ ss.EndedEmitted = true+ sum.EventsEmitted+++ }+ } else {+ ss.EndedEmitted = false+ }+if opts.Upload != nil {- data, err := marshalEvents(mapped)+ // The GCS object is the full per-session snapshot, so it always includes+ // the ended event while the session is terminal (idempotent overwrite).+ snapshot := make([]MappedEvent, 0, len(mapped)+1)+ snapshot = append(snapshot, mapped...)+ if terminal {+ snapshot = append(snapshot, MappedEvent{Event: EndedEvent(s)})+ }+ data, err := marshalEvents(snapshot)if err != nil {return err}@@ -140,9 +165,8 @@ func processSession(ctx context.Context, client *Client, s Session, ss *SessionSss.UpdatedAt = s.UpdatedAtss.Status = s.Status- // A final session (finished/expired) can never resume, and its ended event- // was emitted in this sweep, so stop polling it. Suspended stays pollable in- // case it resumes.+ // A final session (finished/expired) can never resume, so stop polling it.+ // Suspended stays pollable in case it resumes.if IsFinal(s.Status) {ss.Done = true}diff --git a/cli/beacon/internal/devincloud/pull_test.go b/cli/beacon/internal/devincloud/pull_test.goindex fe1983cd..ea9fea31 100644--- a/cli/beacon/internal/devincloud/pull_test.go+++ b/cli/beacon/internal/devincloud/pull_test.go@@ -2,6 +2,7 @@ package devincloudimport ("context"+ "fmt""net/http""net/http/httptest""os"@@ -261,6 +262,52 @@ func TestForceRefreshReFetchesWithoutDuplicating(t *testing.T) {}}+func TestEndedEmittedOncePerObservedEpisode(t *testing.T) {+ dir := t.TempDir()+ logPath := filepath.Join(dir, "runtime.jsonl")++ current := ""+ mux := http.NewServeMux()+ mux.HandleFunc("/v3/organizations/"+testOrg+"/sessions", func(w http.ResponseWriter, _ *http.Request) {+ _, _ = w.Write([]byte(current))+ })+ mux.HandleFunc("/v3/organizations/"+testOrg+"/sessions/s1/messages", func(w http.ResponseWriter, _ *http.Request) {+ _, _ = w.Write([]byte(`{"items":[{"event_id":"e1","source":"user","message":"hi","created_at":10}],"has_next_page":false}`))+ })+ srv := httptest.NewServer(mux)+ defer srv.Close()+ client := New(testOrg, "cog_test", WithBaseURL(srv.URL), WithRetry(0, 0))+ opts := PullOptions{Write: true, LogPath: logPath, StatePath: filepath.Join(dir, "state.json")}++ sess := func(status string, updated int) string {+ return fmt.Sprintf(`{"items":[{"session_id":"s1","status":%q,"user_id":"u","created_at":10,"updated_at":%d}],"has_next_page":false,"total":1}`, status, updated)+ }+ sweep := func() {+ if _, err := PullOnce(context.Background(), client, opts); err != nil {+ t.Fatalf("sweep: %v", err)+ }+ }++ current = sess("suspended", 20)+ sweep() // end #1+ current = sess("suspended", 30)+ sweep() // updated_at bump, no observed resume -> NO new end+ current = sess("working", 40)+ sweep() // resumed -> reset+ current = sess("suspended", 50)+ sweep() // end #2++ ends := 0+ for _, l := range nonEmptyLines(t, logPath) {+ if strings.Contains(l, `"action":"session.ended"`) {+ ends+++ }+ }+ if ends != 2 {+ t.Fatalf("session.ended count = %d, want 2 (one per observed terminal episode)", ends)+ }+}+func nonEmptyLines(t *testing.T, path string) []string {t.Helper()data, err := os.ReadFile(path)diff --git a/cli/beacon/internal/devincloud/state.go b/cli/beacon/internal/devincloud/state.goindex b0c10270..007236f9 100644--- a/cli/beacon/internal/devincloud/state.go+++ b/cli/beacon/internal/devincloud/state.go@@ -14,10 +14,11 @@ type State struct {// SessionState records progress for one session.type SessionState struct {- UpdatedAt int64 `json:"updated_at"`- Status string `json:"status"`- Done bool `json:"done"` // terminal status + ended emitted; skip henceforth- Emitted map[string]bool `json:"emitted"`+ UpdatedAt int64 `json:"updated_at"`+ Status string `json:"status"`+ Done bool `json:"done"` // final status; skip henceforth+ EndedEmitted bool `json:"ended_emitted"` // session.ended emitted for the current terminal episode+ Emitted map[string]bool `json:"emitted"` // dedup ids for started + message events}// LoadState reads state from path. A missing file yields empty state. An empty