Skip to content

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

Ignore a resumed session's old end in --follow

Claudec0c8319e35ad
diff --git a/cli/beacon/cmd/share_follow.go b/cli/beacon/cmd/share_follow.go
index 35777d00..0feda297 100644
--- a/cli/beacon/cmd/share_follow.go
+++ b/cli/beacon/cmd/share_follow.go
@@ -16,7 +16,10 @@ import (
"github.com/spf13/cobra"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/account"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/gitlink"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/handoff"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/shareauto"
)
// `beacon share --follow` keeps a shared trace current while its session runs: it re-reads the
@@ -200,14 +203,11 @@ func shareFollowState(subject shareSubject) (string, bool, error) {
if err != nil {
return "", false, err
}
- ended := false
- for _, e := range events {
- if e.Event.Action == "session.ended" {
- ended = true
- }
- }
+ // A resumed session keeps its old session.ended, so only an end that nothing but Beacon's own
+ // bookkeeping follows counts.
+ ended, latest := shareSessionEnd(events)
if !ended && !subject.fromLog {
- ended = shareLogRecordsEnd(subject.session)
+ ended = shareLogRecordsEnd(subject.session, latest)
}
payload, err := buildSharePayload(subject)
if err != nil {
@@ -241,16 +241,33 @@ func printShareFollow(out, errOut io.Writer, result shareResult, first, final, e
// shareLogRecordsEnd reports whether the runtime log records the end of session, which most store
// mappers do not emit: the runtime's hooks write session.ended to the log, not to its own store.
-// A log that cannot be read says nothing.
-func shareLogRecordsEnd(session handoff.Session) bool {
+// The end counts only when it is the log's newest activity for the session and is no older than
+// storeLatest, the newest activity in the store (a resumed session has an old end in both). A log
+// that cannot be read says nothing.
+func shareLogRecordsEnd(session handoff.Session, storeLatest time.Time) bool {
found, events, ok, err := handoff.LogSession(shareLogPath(), session.ID, session.Harness)
if err != nil || !ok || found.ID != session.ID || found.Harness != session.Harness {
return false
}
+ ended, endAt := shareSessionEnd(events)
+ return ended && !endAt.Before(storeLatest)
+}
+
+// shareSessionEnd reports whether events, in order, end with session.ended followed by nothing but
+// Beacon's bookkeeping (session.shared, session.share_requested, session.commit_linked), and the
+// newest timestamp among the activity it considered (the end's, when it ended).
+func shareSessionEnd(events []schema.Event) (bool, time.Time) {
+ ended := false
+ var latest time.Time
for _, e := range events {
- if e.Event.Action == "session.ended" {
- return true
+ switch e.Event.Action {
+ case SessionSharedAction, shareauto.ActionShareRequested, gitlink.LinkAction:
+ continue
+ }
+ ended = e.Event.Action == "session.ended"
+ if ts, err := schema.ParseTimestamp(e.Timestamp); err == nil && ts.After(latest) {
+ latest = ts
}
}
- return false
+ return ended, latest
}
diff --git a/cli/beacon/cmd/share_follow_test.go b/cli/beacon/cmd/share_follow_test.go
index 726da40c..9dde4ba9 100644
--- a/cli/beacon/cmd/share_follow_test.go
+++ b/cli/beacon/cmd/share_follow_test.go
@@ -439,3 +439,45 @@ func appendLogEvent(t *testing.T, path string, e schema.Event) {
t.Fatal(err)
}
}
+
+// A resumed session keeps its old session.ended; --follow keeps going while newer activity follows
+// it, and stops when a new end arrives after that activity.
+func TestShareFollowIgnoresAnOldEndOfAResumedSession(t *testing.T) {
+ env := newShareEnv(t)
+ events := append(shareFixtureEvents(env.projectDir),
+ followEvent(env.projectDir, "2026-10-01T10:00:30.000000000Z", "session.ended"))
+ resumed := followEvent(env.projectDir, "2026-10-01T10:05:00.000000000Z", "prompt.submitted")
+ resumed.Event.Category = "prompt"
+ resumed.Prompt = &schema.PromptInfo{Text: "picking this back up"}
+ events = append(events, resumed)
+ src := &growingSource{
+ session: handoff.Session{Harness: handoff.HarnessClaude, ID: shareTestSession, Directory: env.projectDir, UpdatedAt: time.Now()},
+ events: events,
+ }
+ stubHandoffSources(t, src)
+ srv := newFakeShareServer(t)
+ srv.signIn(account.ScopeTracesWrite)
+ // The runtime log has the old end too.
+ appendLogEvent(t, env.logPath, followEvent(env.projectDir, "2026-10-01T10:00:30.000000000Z", "session.ended"))
+ polls := 0
+ prevWait := shareFollowWait
+ shareFollowWait = func(ctx context.Context, d time.Duration) error {
+ polls++
+ switch polls {
+ case 3:
+ // The resumed session ends; its hooks write the end to the runtime log.
+ appendLogEvent(t, env.logPath, followEvent(env.projectDir, "2026-10-01T10:09:00.000000000Z", "session.ended"))
+ case 10:
+ return errors.New("the loop did not stop at the new end")
+ }
+ return nil
+ }
+ t.Cleanup(func() { shareFollowWait = prevWait })
+ _, stderr, err := runShareCommand(t, "--follow", "--cwd", env.projectDir, "--log-path", env.logPath)
+ if err != nil {
+ t.Fatalf("follow: %v\n%s", err, stderr)
+ }
+ if polls != 3 || !strings.Contains(stderr, "The session ended;") {
+ t.Fatalf("polls = %d, want 3 (the old end must not stop it)\n%s", polls, stderr)
+ }
+}
diff --git a/docs/cli/share.mdx b/docs/cli/share.mdx
index a4d02702..421b66c9 100644
--- a/docs/cli/share.mdx
+++ b/docs/cli/share.mdx
@@ -120,7 +120,7 @@ beacon share refresh # this session, found like --current
## Follow a running session
-`--follow` keeps the trace current while the session runs. It shares the session, then checks it again every `--interval` (default `10s`, at least `1s`) and uploads what changed, until the session records its end (`session.ended`, in the runtime's store or in the runtime log, where the hooks write it) or you press Ctrl+C. A check is a local read; a request is made only when the session's messages changed, and it sends only what beacon.sh lacks, the same way `refresh` does. A failed upload after the first is reported and retried on the next check. When the session has ended and its last messages cannot be uploaded, `--follow` retries three more times, one interval apart, and then exits non-zero naming what the trace holds. If the runtime log cannot be written after an upload, `--follow` warns and keeps going. A check that cannot read the session is reported once per distinct error, and after five failed checks in a row `--follow` exits non-zero.
+`--follow` keeps the trace current while the session runs. It shares the session, then checks it again every `--interval` (default `10s`, at least `1s`) and uploads what changed, until the session records its end (`session.ended`, in the runtime's store or in the runtime log, where the hooks write it; a resumed session's earlier end does not count once newer activity follows it) or you press Ctrl+C. A check is a local read; a request is made only when the session's messages changed, and it sends only what beacon.sh lacks, the same way `refresh` does. A failed upload after the first is reported and retried on the next check. When the session has ended and its last messages cannot be uploaded, `--follow` retries three more times, one interval apart, and then exits non-zero naming what the trace holds. If the runtime log cannot be written after an upload, `--follow` warns and keeps going. A check that cannot read the session is reported once per distinct error, and after five failed checks in a row `--follow` exits non-zero.
```bash
beacon share --follow # this session, until it ends