Skip to content

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

Merge remote-tracking branch 'origin/claude/affectionate-dijkstra-os6gl2-share-command' into HEAD

Claude764c2b956864
diff --cc cli/beacon/cmd/share.go
index 58a83bea,dbbf0c70..1b376a66
--- a/cli/beacon/cmd/share.go
+++ b/cli/beacon/cmd/share.go
@@@ -485,115 -458,57 +485,121 @@@ func resolveShareSubject(harness, id st
sources := handoffSources(dirs)
switch {
case id != "":
- session, findErr := handoff.Find(sources, harness, id)
- if findErr == nil {
- return shareSubject{session: session}, nil
- }
- // A prefix that names several sessions is the person's to narrow: falling back to the log
- // would share whichever of them the log happens to hold.
- var ambiguous *handoff.AmbiguousError
- if errors.As(findErr, &ambiguous) {
- return shareSubject{}, findErr
- }
- // Not in any store, or a store could not be read: the runtime log may have the session.
- var notFound *handoff.NotFoundError
- isNotFound := errors.As(findErr, &notFound)
- logPath := shareLogPath()
- session, events, found, err := handoff.LogSession(logPath, id, harness)
- if err != nil {
- if !isNotFound {
- return shareSubject{}, findErr
- }
- return shareSubject{}, fmt.Errorf("read runtime log %s: %w", logPath, err)
- }
- if !found || (harness != "" && session.Harness != harness) {
- if !isNotFound {
- return shareSubject{}, findErr
- }
- return shareSubject{}, fmt.Errorf("%w; not in the runtime log %s either (`beacon share --list` shows what is available)", notFound, logPath)
- }
- return shareSubject{session: session, fromLog: true, logEvents: events}, nil
+ subject, err := resolveShareID(sources, harness, id)
+ subject.selectedBy = "session"
+ return subject, err
case strings.TrimSpace(shareOpts.sourcePath) != "":
- return resolveShareSourcePath(sources, harness, shareOpts.sourcePath)
+ subject, err := resolveShareSourcePath(sources, harness, shareOpts.sourcePath)
+ subject.selectedBy = "source_path"
+ return subject, err
default:
- dir, err := shareDirectory()
- if err != nil {
- return shareSubject{}, err
+ return resolveShareCurrent(sources, harness)
+ }
+}
+
+// resolveShareID finds a session by id or unique prefix: in the runtimes' stores, then in the
+// runtime log.
+func resolveShareID(sources []handoff.Source, harness, id string) (shareSubject, error) {
+ session, findErr := handoff.Find(sources, harness, id)
+ if findErr == nil {
+ return shareSubject{session: session}, nil
+ }
++ // A prefix that names several sessions is the person's to narrow: falling back to the log
++ // would share whichever of them the log happens to hold.
++ var ambiguous *handoff.AmbiguousError
++ if errors.As(findErr, &ambiguous) {
++ return shareSubject{}, findErr
++ }
+ // Not in any store, or a store could not be read: the runtime log may have the session.
+ var notFound *handoff.NotFoundError
+ isNotFound := errors.As(findErr, &notFound)
+ logPath := shareLogPath()
+ session, events, found, err := handoff.LogSession(logPath, id, harness)
+ if err != nil {
+ if !isNotFound {
+ return shareSubject{}, findErr
}
- candidates, err := shareCandidateList(sources, harness, dir, 1)
- if err != nil {
- return shareSubject{}, err
+ return shareSubject{}, fmt.Errorf("read runtime log %s: %w", logPath, err)
+ }
+ if !found || (harness != "" && session.Harness != harness) {
+ if !isNotFound {
+ return shareSubject{}, findErr
}
- if len(candidates) == 0 {
- return shareSubject{}, fmt.Errorf("no agent session found in %s; pass --session, or --cwd for another directory (`beacon share --list --cwd DIR`)", dir)
+ return shareSubject{}, fmt.Errorf("%w; not in the runtime log %s either (`beacon share --list` shows what is available)", notFound, logPath)
+ }
+ return shareSubject{session: session, fromLog: true, logEvents: events}, nil
+}
+
+// resolveShareCurrent finds the session this command runs inside, in order:
+//
+// 1. BEACON_SESSION_ID (with BEACON_HARNESS), which the Claude Code hook exports to the agent's
+// shell. Skipped under --cwd, which names another directory, and when --harness names another
+// runtime (a Codex started from a Claude Code shell inherits Claude's variables). An id that is
+// set but cannot be found is an error rather than a reason to share some other session.
+// 2. The session a Beacon hook last saw in the directory, from the active-session record.
+// 3. The most recent session in the directory.
+func resolveShareCurrent(sources []handoff.Source, harness string) (shareSubject, error) {
+ dir, err := shareDirectory()
+ if err != nil {
+ return shareSubject{}, err
+ }
+ if strings.TrimSpace(shareOpts.cwd) == "" {
+ if id := strings.TrimSpace(shareGetenv(shareSessionEnv)); id != "" {
+ envHarness, herr := handoff.ParseHarness(shareGetenv(shareHarnessEnv))
+ if herr == nil && (harness == "" || envHarness == "" || envHarness == harness) {
+ if envHarness == "" {
+ envHarness = harness
+ }
+ subject, err := resolveShareID(sources, envHarness, id)
+ if err != nil {
+ return shareSubject{}, fmt.Errorf("%s=%s names this session, but it cannot be read yet: %w", shareSessionEnv, id, err)
+ }
+ subject.selectedBy = "env"
+ return subject, nil
+ }
}
- // A session listed by its runtime's store is read from that store; one the store has lost
- // or cannot read since falls back to the log when its events are read (shareEvents). A
- // session only the log listed is read from the log: its store may be unreadable.
- candidate := candidates[0]
- if !candidate.fromLog && handoff.ReadsStore(candidate.session.Harness) {
- return shareSubject{session: candidate.session}, nil
+ }
+ candidates, err := shareCandidateList(sources, harness, dir, 1)
+ if err != nil {
+ return shareSubject{}, err
+ }
+ if entry, ok := shareActiveSession(dir, harness); ok && shareActiveIsNewest(entry, candidates) {
+ // A record whose session cannot be read (a runtime Beacon reads only from the log, whose
+ // events have rotated away) falls through to the directory's most recent session.
+ if subject, err := resolveShareID(sources, entry.Harness, entry.SessionID); err == nil {
+ subject.selectedBy = "active"
+ return subject, nil
}
- return resolveShareLog(candidate.session)
}
+ if len(candidates) == 0 {
+ return shareSubject{}, shareNoSessionError(sources, harness, dir)
+ }
+ // A session listed by its runtime's store is read from that store; one the store has lost or
+ // cannot read since falls back to the log when its events are read (shareEvents). A session
+ // only the log listed is read from the log: its store may be unreadable.
+ candidate := candidates[0]
+ if !candidate.fromLog && handoff.ReadsStore(candidate.session.Harness) {
+ return shareSubject{session: candidate.session, selectedBy: "recent"}, nil
+ }
+ subject, err := resolveShareLog(candidate.session)
+ subject.selectedBy = "recent"
+ return subject, err
+}
+
+// shareActiveIsNewest reports whether the active-session entry may stand for this directory's
+// session: only hooks write the record, so a runtime Beacon sees only through OTLP or a store (an
+// OTLP-only or poll runtime) never updates it, and an older entry would otherwise win over that
+// runtime's newer session. The entry is used when it is the newest candidate itself, or was seen at
+// least as recently as the newest candidate was updated.
+func shareActiveIsNewest(entry activesession.Entry, candidates []shareCandidate) bool {
+ if len(candidates) == 0 {
+ return true
+ }
+ newest := candidates[0].session
+ if newest.Harness == entry.Harness && newest.ID == entry.SessionID {
+ return true
+ }
+ return !entry.LastSeen.Before(newest.UpdatedAt)
}
func resolveShareLog(session handoff.Session) (shareSubject, error) {