Skip to content

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

Accept session id prefixes in the runtime-log fallback too

Claudec83c99cc85e2
diff --git a/cli/beacon/cmd/handoff.go b/cli/beacon/cmd/handoff.go
index 6984393b..791468f7 100644
--- a/cli/beacon/cmd/handoff.go
+++ b/cli/beacon/cmd/handoff.go
@@ -105,7 +105,11 @@ func loadHandoffBrief(id string) (handoff.Brief, error) {
// than the store does.
func loadHandoffLogSession(id, harness string, notFound *handoff.NotFoundError) (handoff.Session, []schema.Event, error) {
logPath := handoffRuntimeLogPath()
- session, events, found, err := handoff.LogSession(logPath, id)
+ session, events, found, err := handoff.LogSession(logPath, id, harness)
+ var ambiguous *handoff.AmbiguousError
+ if errors.As(err, &ambiguous) {
+ return handoff.Session{}, nil, fmt.Errorf("in the runtime log %s: %w", logPath, err)
+ }
if err != nil {
return handoff.Session{}, nil, fmt.Errorf("read runtime log %s: %w", logPath, err)
}
diff --git a/cli/beacon/cmd/handoff_test.go b/cli/beacon/cmd/handoff_test.go
index 8041221c..253dfc5e 100644
--- a/cli/beacon/cmd/handoff_test.go
+++ b/cli/beacon/cmd/handoff_test.go
@@ -457,3 +457,13 @@ func TestHandoffExportErrors(t *testing.T) {
t.Fatalf("--print with --output-dir err = %v", err)
}
}
+
+func TestHandoffExportFindsALogSessionByPrefix(t *testing.T) {
+ stubHandoffClock(t)
+ stubHandoffSources(t, stubHandoffSource{harness: handoff.HarnessClaude})
+ logPath := handoffLog(t, "cursor", "cursor-conv-1234", "rename the module")
+ out, _, err := runHandoff(t, "export", "cursor-conv", "--print", "--log-path", logPath)
+ if err != nil || !strings.Contains(out, "rename the module") || !strings.Contains(out, "`cursor-conv-1234`") {
+ t.Fatalf("export by prefix = %v\n%s", err, out)
+ }
+}
diff --git a/cli/beacon/internal/handoff/brief_test.go b/cli/beacon/internal/handoff/brief_test.go
index e26d0092..55e3bd27 100644
--- a/cli/beacon/internal/handoff/brief_test.go
+++ b/cli/beacon/internal/handoff/brief_test.go
@@ -1,6 +1,7 @@
package handoff
import (
+ "errors"
"fmt"
"os"
"path/filepath"
@@ -358,7 +359,7 @@ func TestLogSession(t *testing.T) {
}
writeFixture(t, logPath, strings.Join(lines, "\n")+"\n")
- session, events, found, err := LogSession(logPath, "cur-1")
+ session, events, found, err := LogSession(logPath, "cur-1", "")
if err != nil || !found {
t.Fatalf("LogSession = %v, %v", found, err)
}
@@ -372,7 +373,7 @@ func TestLogSession(t *testing.T) {
if brief.FirstRequest != "rename the module" || !strings.Contains(brief.Render(), "a cursor session") {
t.Fatalf("log brief = %+v", brief)
}
- if _, _, found, err := LogSession(logPath, "missing"); found || err != nil {
+ if _, _, found, err := LogSession(logPath, "missing", ""); found || err != nil {
t.Fatalf("missing session = %v, %v", found, err)
}
}
@@ -442,3 +443,31 @@ func TestCodeSpan(t *testing.T) {
}
}
}
+
+func TestLogSessionAcceptsAUniquePrefix(t *testing.T) {
+ line := func(harness, id string) string {
+ return `{"timestamp":"2026-09-25T09:00:00Z","vendor":"beacon","product":"endpoint-agent","schema_version":"1.0","event":{"kind":"agent_runtime","action":"prompt.submitted","category":"prompt"},"severity":"info","endpoint":{"hostname":"h","os":"linux"},"harness":{"name":"` + harness + `"},"session":{"id":"` + id + `"},"prompt":{"text":"p ` + id + `"}}`
+ }
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ writeFixture(t, logPath, strings.Join([]string{
+ line("cursor", "conv-abc123-one"),
+ line("claude", "conv-xyz789-two"),
+ line("cline", "conv-xyz789-three"),
+ }, "\n")+"\n")
+
+ session, events, found, err := LogSession(logPath, "conv-abc", "")
+ if err != nil || !found || session.ID != "conv-abc123-one" || len(events) != 1 {
+ t.Fatalf("unique prefix = %+v, %d events, %v, %v", session, len(events), found, err)
+ }
+ if _, _, found, err := LogSession(logPath, "conv-", ""); found || err != nil {
+ t.Fatalf("a prefix under %d characters must not search: %v, %v", MinPrefixLength, found, err)
+ }
+ var ambiguous *AmbiguousError
+ if _, _, _, err := LogSession(logPath, "conv-xyz", ""); !errors.As(err, &ambiguous) || len(ambiguous.Candidates) != 2 {
+ t.Fatalf("a prefix naming two sessions must be ambiguous, got %v", err)
+ }
+ session, _, found, err = LogSession(logPath, "conv-xyz", HarnessClaude)
+ if err != nil || !found || session.ID != "conv-xyz789-two" || session.Harness != HarnessClaude {
+ t.Fatalf("--harness must narrow the prefix (raw \"claude\" rows included): %+v, %v, %v", session, found, err)
+ }
+}
diff --git a/cli/beacon/internal/handoff/runtimelog.go b/cli/beacon/internal/handoff/runtimelog.go
index e6f510f3..51909cb4 100644
--- a/cli/beacon/internal/handoff/runtimelog.go
+++ b/cli/beacon/internal/handoff/runtimelog.go
@@ -1,6 +1,7 @@
package handoff
import (
+ "strings"
"time"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/dashboard"
@@ -10,15 +11,27 @@ import (
// LogSession reads a session back from Beacon's runtime log, for a session whose runtime no longer
// has it (a deleted transcript, another machine's log). It works for any runtime Beacon captured,
-// but the log keeps less of each step than the runtime's own store. found is false when the log has
-// no events for id.
-func LogSession(logPath, id string) (session Session, events []schema.Event, found bool, err error) {
+// but the log keeps less of each step than the runtime's own store.
+//
+// id is the full session id or, as with Find, a unique prefix of at least MinPrefixLength
+// characters. harness, when set, limits a prefix to that runtime's sessions. found is false when
+// the log has no such session; an id or prefix naming several returns an *AmbiguousError.
+func LogSession(logPath, id, harness string) (session Session, events []schema.Event, found bool, err error) {
detail, found, err := dashboard.ReadSessionDetail(logPath, id)
- if err != nil || !found {
+ if err != nil {
return Session{}, nil, false, err
}
+ if !found {
+ full, err := logSessionForPrefix(logPath, id, harness)
+ if err != nil || full == "" {
+ return Session{}, nil, false, err
+ }
+ if detail, found, err = dashboard.ReadSessionDetail(logPath, full); err != nil || !found {
+ return Session{}, nil, false, err
+ }
+ }
session = Session{
- ID: id,
+ ID: detail.Session.ID,
// Older log rows carry a runtime's raw name ("claude"); every comparison uses the canonical one.
Harness: asymptoteobserve.NormalizeHarnessName(detail.Session.Harness),
Directory: detail.Session.WorkingDir,
@@ -33,3 +46,41 @@ func LogSession(logPath, id string) (session Session, events []schema.Event, fou
}
return session, events, true, nil
}
+
+// logSessionForPrefix returns the one session id in the log that starts with prefix, "" when none
+// does or the prefix is too short to search by.
+func logSessionForPrefix(logPath, prefix, harness string) (string, error) {
+ if len(prefix) < MinPrefixLength {
+ return "", nil
+ }
+ matches := map[string]string{}
+ var order []string
+ err := dashboard.StreamEvents(logPath, func(event schema.Event) error {
+ if event.Session == nil || !strings.HasPrefix(event.Session.ID, prefix) {
+ return nil
+ }
+ name := asymptoteobserve.NormalizeHarnessName(event.Harness.Name)
+ if harness != "" && name != harness {
+ return nil
+ }
+ if _, seen := matches[event.Session.ID]; !seen {
+ matches[event.Session.ID] = name
+ order = append(order, event.Session.ID)
+ }
+ return nil
+ })
+ if err != nil {
+ return "", err
+ }
+ switch len(order) {
+ case 0:
+ return "", nil
+ case 1:
+ return order[0], nil
+ }
+ candidates := make([]Session, 0, len(order))
+ for _, id := range order {
+ candidates = append(candidates, Session{ID: id, Harness: matches[id]})
+ }
+ return "", &AmbiguousError{ID: prefix, Candidates: candidates}
+}