Skip to content

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

Take back the share-request mark when the request cannot be written

Claude574440af52ce
diff --git a/cli/beacon-hooks/internal/logging/logging.go b/cli/beacon-hooks/internal/logging/logging.go
index 16dc4501..739a9524 100644
--- a/cli/beacon-hooks/internal/logging/logging.go
+++ b/cli/beacon-hooks/internal/logging/logging.go
@@ -172,7 +172,9 @@ func (l *Logger) requestShareOnce(event map[string]interface{}) {
if !decision.Enabled {
return
}
- first, err := activesession.MarkShareRequested(activesession.DefaultPath(), name, id, time.Now())
+ recordPath := activesession.DefaultPath()
+ markedAt := time.Now()
+ first, err := activesession.MarkShareRequested(recordPath, name, id, markedAt)
if err != nil || !first {
return
}
@@ -188,7 +190,12 @@ func (l *Logger) requestShareOnce(event map[string]interface{}) {
if branch, _ := event["branch"].(string); branch != "" {
fields["branch"] = branch
}
- _ = l.EndpointEventWithFidelity(shareauto.ActionShareRequested, "session", "info", "Auto-share requested for this session", asymptoteobserve.FidelityInferred, fields)
+ if err := l.EndpointEventWithFidelity(shareauto.ActionShareRequested, "session", "info", "Auto-share requested for this session", asymptoteobserve.FidelityInferred, fields); err != nil {
+ // The request was not written (a read-only log, a sandbox): take the mark back so the next
+ // hook of the session asks again, rather than leave the session marked and never requested.
+ // Like the rest of this path it stays silent and never fails the hook.
+ _ = activesession.UnmarkShareRequested(recordPath, name, id, markedAt)
+ }
}
// recordActiveSession notes the event's session as the one its runtime is running in its working
@@ -460,7 +467,11 @@ func firstEnv(keys ...string) string {
return ""
}
-func writeEndpointJSON(path string, event map[string]interface{}) error {
+// writeEndpointJSON appends one endpoint event to the runtime log. A variable so a test can make one
+// write fail.
+var writeEndpointJSON = writeEndpointJSONFile
+
+func writeEndpointJSONFile(path string, event map[string]interface{}) error {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
diff --git a/cli/beacon-hooks/internal/logging/share_request_test.go b/cli/beacon-hooks/internal/logging/share_request_test.go
new file mode 100644
index 00000000..f437864f
--- /dev/null
+++ b/cli/beacon-hooks/internal/logging/share_request_test.go
@@ -0,0 +1,96 @@
+package logging
+
+import (
+ "bufio"
+ "encoding/json"
+ "errors"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/activesession"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/shareauto"
+)
+
+// A share request that cannot be written takes its dedupe mark back, so the next hook of the
+// session asks again and exactly one request is written once the log takes it.
+func TestShareRequestMarkIsRolledBackWhenTheWriteFails(t *testing.T) {
+ dir := t.TempDir()
+ logPath := filepath.Join(dir, "runtime.jsonl")
+ recordPath := filepath.Join(dir, activesession.FileName)
+ settingsPath := filepath.Join(dir, "share.json")
+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)
+ t.Setenv(activesession.PathEnv, recordPath)
+ t.Setenv(shareauto.PathEnv, settingsPath)
+ var settings shareauto.Settings
+ settings.SetGlobal(shareauto.GlobalOn)
+ if err := shareauto.Save(settingsPath, settings); err != nil {
+ t.Fatal(err)
+ }
+ failing := true
+ prev := writeEndpointJSON
+ writeEndpointJSON = func(path string, event map[string]interface{}) error {
+ if info, _ := event["event"].(map[string]interface{}); failing && info["action"] == shareauto.ActionShareRequested {
+ return errors.New("read-only file system")
+ }
+ return prev(path, event)
+ }
+ t.Cleanup(func() { writeEndpointJSON = prev })
+
+ logger := &Logger{hookName: "test", platform: "claude"}
+ hook := func() {
+ t.Helper()
+ fields := map[string]interface{}{"session": map[string]interface{}{"id": "sess-1", "working_directory": dir}}
+ _ = captureStderr(t, func() {
+ if err := logger.EndpointEvent("prompt.submitted", "prompt", "info", "prompt", fields); err != nil {
+ t.Fatalf("hook event: %v", err)
+ }
+ })
+ }
+ hook()
+ var record struct {
+ Sessions []json.RawMessage `json:"sessions"`
+ ShareRequested []json.RawMessage `json:"share_requested"`
+ }
+ data, err := os.ReadFile(recordPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := json.Unmarshal(data, &record); err != nil {
+ t.Fatal(err)
+ }
+ if len(record.Sessions) == 0 || len(record.ShareRequested) != 0 {
+ t.Fatalf("record = %s, want the session noted and no share-request mark", data)
+ }
+ if got := countActions(t, logPath, shareauto.ActionShareRequested); got != 0 {
+ t.Fatalf("share requests while failing = %d", got)
+ }
+ failing = false
+ hook()
+ hook()
+ if got := countActions(t, logPath, shareauto.ActionShareRequested); got != 1 {
+ t.Fatalf("share requests = %d, want exactly one", got)
+ }
+}
+
+func countActions(t *testing.T, path, action string) int {
+ t.Helper()
+ f, err := os.Open(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer f.Close()
+ n := 0
+ scanner := bufio.NewScanner(f)
+ scanner.Buffer(make([]byte, 1<<20), 1<<20)
+ for scanner.Scan() {
+ var event map[string]interface{}
+ if json.Unmarshal(scanner.Bytes(), &event) != nil {
+ continue
+ }
+ if info, _ := event["event"].(map[string]interface{}); info["action"] == action {
+ n++
+ }
+ }
+ return n
+}
diff --git a/pkg/asymptoteobserve/activesession/activesession.go b/pkg/asymptoteobserve/activesession/activesession.go
index 07c2bf83..f3ce70ae 100644
--- a/pkg/asymptoteobserve/activesession/activesession.go
+++ b/pkg/asymptoteobserve/activesession/activesession.go
@@ -170,6 +170,27 @@ func MarkShareRequested(path, harness, session string, now time.Time) (bool, err
return first, nil
}
+// UnmarkShareRequested takes back the mark MarkShareRequested made at markedAt, when the request it
+// stood for could not be written, so the next hook of the session tries again. It runs under the
+// same lock, and removes the mark only while it is still the one made at markedAt: a mark another
+// hook has made since is left alone.
+func UnmarkShareRequested(path, harness, session string, markedAt time.Time) error {
+ harness, session = strings.TrimSpace(harness), strings.TrimSpace(session)
+ if path == "" || harness == "" || session == "" {
+ return nil
+ }
+ markedAt = markedAt.UTC()
+ return update(path, markedAt, func(doc *document) bool {
+ for i, r := range doc.ShareRequested {
+ if r.Harness == harness && r.SessionID == session && r.At.Equal(markedAt) {
+ doc.ShareRequested = append(doc.ShareRequested[:i], doc.ShareRequested[i+1:]...)
+ return true
+ }
+ }
+ return false
+ })
+}
+
func load(path string) (document, error) {
doc := document{SchemaVersion: schemaVersion}
if path == "" {
diff --git a/pkg/asymptoteobserve/activesession/activesession_test.go b/pkg/asymptoteobserve/activesession/activesession_test.go
index d505b3a1..89ff2466 100644
--- a/pkg/asymptoteobserve/activesession/activesession_test.go
+++ b/pkg/asymptoteobserve/activesession/activesession_test.go
@@ -147,3 +147,24 @@ func TestMarkShareRequestedOncePerSession(t *testing.T) {
t.Fatal("mark lost after a Record")
}
}
+
+func TestUnmarkShareRequestedRemovesOnlyItsOwnMark(t *testing.T) {
+ path := filepath.Join(t.TempDir(), FileName)
+ first := time.Date(2026, 10, 1, 10, 0, 0, 123456789, time.UTC)
+ if ok, err := MarkShareRequested(path, "claude_code", "s1", first); err != nil || !ok {
+ t.Fatalf("mark = %v %v", ok, err)
+ }
+ // Another mark time is not this one: left alone.
+ if err := UnmarkShareRequested(path, "claude_code", "s1", first.Add(time.Second)); err != nil {
+ t.Fatal(err)
+ }
+ if ok, _ := MarkShareRequested(path, "claude_code", "s1", first); ok {
+ t.Fatal("a different mark time removed the mark")
+ }
+ if err := UnmarkShareRequested(path, "claude_code", "s1", first); err != nil {
+ t.Fatal(err)
+ }
+ if ok, err := MarkShareRequested(path, "claude_code", "s1", first.Add(time.Minute)); err != nil || !ok {
+ t.Fatalf("after unmark, mark = %v %v", ok, err)
+ }
+}