Skip to content

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

fix(factory): keep the reply dedupe marker through stale-session cleanup

Claude12ee3247f079
diff --git a/cli/beacon-hooks/internal/state/state.go b/cli/beacon-hooks/internal/state/state.go
index 81104f4f..5cc48385 100644
--- a/cli/beacon-hooks/internal/state/state.go
+++ b/cli/beacon-hooks/internal/state/state.go
@@ -70,6 +70,10 @@ func writeStateFile(path string, data []byte) error {
const (
staleEvaluationTTLHours = 2
+ // replyMarkerTTL is how long a session's last recorded reply is remembered. A Stop that
+ // re-fires over an unchanged transcript after that writes the reply again, which costs one
+ // duplicate event rather than an unbounded state file.
+ replyMarkerTTL = 14 * 24 * time.Hour
)
// Evaluation represents a pending evaluation
@@ -88,6 +92,10 @@ type SessionData struct {
// LastReplyID names the last assistant reply a hook recorded for this session, so a reply read
// from a transcript that has not moved since the previous turn is not recorded twice.
LastReplyID string `json:"last_reply_id,omitempty"`
+ // LastReplyAt is when LastReplyID was recorded. Stale-session cleanup keeps a session whose
+ // reply marker is younger than replyMarkerTTL, so the dedupe outlives the two-hour evaluation
+ // window that every other session field is cleaned up on.
+ LastReplyAt string `json:"last_reply_at,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
}
@@ -211,6 +219,7 @@ func (s *SessionState) MarkReplyRecorded(id string) (bool, error) {
return false, nil
}
state[s.sessionID].LastReplyID = id
+ state[s.sessionID].LastReplyAt = time.Now().UTC().Format(time.RFC3339)
if err := s.saveStateFile(state); err != nil {
return false, err
}
@@ -326,7 +335,7 @@ func CleanupStaleForPlatform(platform string) int {
sessionData.Evaluations = freshEvals
// Mark empty sessions for removal only if they are stale
- if len(freshEvals) == 0 && sessionData.RepromptCount == 0 {
+ if len(freshEvals) == 0 && sessionData.RepromptCount == 0 && !sessionData.hasRecentReplyMarker() {
createdAt, err := time.Parse(time.RFC3339, sessionData.CreatedAt)
if err != nil || createdAt.Before(cutoff) {
sessionsToRemove = append(sessionsToRemove, sessionID)
@@ -350,3 +359,12 @@ func CleanupStaleForPlatform(platform string) int {
return removedCount
}
+
+// hasRecentReplyMarker reports whether the session recorded a reply within replyMarkerTTL.
+func (d *SessionData) hasRecentReplyMarker() bool {
+ if d.LastReplyID == "" {
+ return false
+ }
+ at, err := time.Parse(time.RFC3339, d.LastReplyAt)
+ return err == nil && time.Since(at) < replyMarkerTTL
+}
diff --git a/cli/beacon-hooks/internal/state/state_test.go b/cli/beacon-hooks/internal/state/state_test.go
index 72363df1..35f89407 100644
--- a/cli/beacon-hooks/internal/state/state_test.go
+++ b/cli/beacon-hooks/internal/state/state_test.go
@@ -6,6 +6,7 @@ import (
"os/exec"
"path/filepath"
"testing"
+ "time"
"github.com/asymptote-labs/agent-beacon/cli/beacon-hooks/internal/config"
)
@@ -183,3 +184,36 @@ func TestMarkReplyRecordedOncePerReplyAndSession(t *testing.T) {
t.Fatal("setting the model reset the recorded reply")
}
}
+
+// Stale-session cleanup runs on every SessionStart and SessionEnd of the platform and removes
+// sessions older than two hours with no evaluations, which is every Factory session. A session's
+// reply marker has to survive it, or a Stop over an unchanged transcript records the reply again.
+func TestCleanupKeepsARecentReplyMarker(t *testing.T) {
+ _, cleanup := setupTestStateDir(t)
+ defer cleanup()
+
+ old := time.Now().Add(-3 * time.Hour).UTC().Format(time.RFC3339)
+ expired := time.Now().Add(-replyMarkerTTL - time.Hour).UTC().Format(time.RFC3339)
+ st := NewSessionState("seed", "cursor")
+ state := map[string]*SessionData{
+ "recent-reply": {CreatedAt: old, LastReplyID: "m1", LastReplyAt: time.Now().UTC().Format(time.RFC3339)},
+ "expired-reply": {CreatedAt: old, LastReplyID: "m2", LastReplyAt: expired},
+ "no-reply": {CreatedAt: old},
+ }
+ if err := st.saveStateFile(state); err != nil {
+ t.Fatal(err)
+ }
+
+ CleanupStaleForPlatform("cursor")
+
+ after := st.loadStateFile()
+ if after["recent-reply"] == nil {
+ t.Fatal("cleanup removed a session with a recent reply marker")
+ }
+ if after["expired-reply"] != nil || after["no-reply"] != nil {
+ t.Fatalf("cleanup kept stale sessions: %v", after)
+ }
+ if fresh, _ := NewSessionState("recent-reply", "cursor").MarkReplyRecorded("m1"); fresh {
+ t.Fatal("the reply was treated as new after cleanup")
+ }
+}