Skip to content

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

Link handed-off sessions from the session-store sync paths too

Claudeab3416990e1d
diff --git a/CLAUDE.md b/CLAUDE.md
index 0f911c4d..1ba65919 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -64,6 +64,7 @@ Supported runtime surfaces today:
- A local-only dashboard served by `beacon endpoint dashboard`, bound to loopback by default and backed by the runtime JSONL log.
- Token usage and runtime-reported cost capture across spans, logs, and metric datapoints, normalized into `gen_ai.usage`, with attribution rollups served by the dashboard token view (`/api/tokens`) and the `beacon token-usage` report command for local and CI logs.
- Local threat detection via `beacon scan`, which runs the open Threat Rules format (`spec/threat-rules`; CEL match conditions over the endpoint event schema, with embedded conformance fixtures) over the runtime JSONL. The engine (`pkg/asymptoteobserve/threatrules`) ships in the binary; the rule corpus is external data loaded from a local store (`~/.beacon/endpoint/rules`) managed by `beacon rules`, so the corpus can grow without enlarging the binary. `scan` is read-only and offline; only the explicit, user-initiated `beacon rules pull <url>` reaches the network. A small frozen baseline is embedded so `scan` works before any rules are installed.
+- Session handoff via `beacon handoff list|export|resume` (`cli/beacon/internal/handoff`, `cli/beacon/cmd/handoff*.go`; docs in `docs/cli/handoff.mdx`) for Claude Code, Codex CLI, OpenCode and Cline. It reads each runtime's own session store read-only and never touches the network. `resume` reopens a session natively (`claude --resume`, `codex resume`, `opencode --session`, `cline --tui --id`) when it can, and otherwise writes a brief to `~/.beacon/endpoint/handoffs` (0700/0600) and starts a new session pointed at it. The brief is passed by absolute path, never inlined on a command line. Every Cline launch passes `--auto-approve false`, because Cline defaults to approving every tool call. Briefs run every event through `SanitizeEvent`, and quoted content goes in fences longer than any backtick run it contains, with inline fields reduced to one line, so quoted content cannot forge brief structure. A new session's first prompt ends with the `[beacon-handoff from=<harness> session=<id>]` marker (`pkg/asymptoteobserve/handoff.go`). The hook prompt paths and the four session-store mappers record a `session.handoff` event with `handoff.{source_harness,source_session_id}` when they see it; that is an observed claim, not a verified identity.
- An optional, off-by-default policy seam in the hook path (`cli/beacon-hooks/internal/policy`) governed by the stable contract in `pkg/asymptoteobserve/policycontract`. When `BEACON_POLICY_PROVIDER` names an executable, the `pre-tool` and `permission-request` hooks send it a JSON request describing the imminent tool call and honor an allow/deny response, returning the runtime's native deny shape and `policy.enforcement=enforce` telemetry on a deny. The seam is inert when unset and fail-open on any error (timeout, non-zero exit, malformed output), so the open build ships no enforcement of its own; all audit/enforce decision logic lives in the external provider. `rules/agent-control/agent-permission-bypass-spawn.rule.yaml` is the open detect twin that flags the same pattern in `beacon scan`.
Current non-goals unless explicitly requested:
diff --git a/cli/beacon/internal/claudesession/mapper.go b/cli/beacon/internal/claudesession/mapper.go
index 163bab32..cc91da16 100644
--- a/cli/beacon/internal/claudesession/mapper.go
+++ b/cli/beacon/internal/claudesession/mapper.go
@@ -164,6 +164,11 @@ func (m *mapper) emitPrompt(record Record, text string) {
ev.Content = contentMarker(text)
ev.GenAI = mergeGenAI(ev.GenAI, &schema.GenAIInfo{Input: &schema.GenAIInputInfo{Messages: asymptoteobserve.TextInputMessages(text)}})
m.append(record, "prompt", ev)
+ if info, ok := asymptoteobserve.ParseHandoffMarker(text); ok {
+ link := m.base(record, "session.handoff", "session", schema.SeverityInfo, schema.FidelityObserved, "Session continued from a "+info.SourceHarness+" session")
+ link.Handoff = &info
+ m.append(record, "handoff", link)
+ }
}
func (m *mapper) emitAgentMessage(record Record, text string) {
diff --git a/cli/beacon/internal/clinesession/mapper.go b/cli/beacon/internal/clinesession/mapper.go
index 60169f23..23b152fa 100644
--- a/cli/beacon/internal/clinesession/mapper.go
+++ b/cli/beacon/internal/clinesession/mapper.go
@@ -102,6 +102,11 @@ func (m *mapper) emitPrompt(record Record) {
ev.GenAI = &schema.GenAIInfo{Input: &schema.GenAIInputInfo{Messages: asymptoteobserve.TextInputMessages(text)}}
ev.Raw = m.raw(record)
m.append(record, "prompt", ev)
+ if info, ok := asymptoteobserve.ParseHandoffMarker(text); ok {
+ link := m.base(record, "session.handoff", "session", schema.SeverityInfo, "Session continued from a "+info.SourceHarness+" session")
+ link.Handoff = &info
+ m.append(record, "handoff", link)
+ }
}
func (m *mapper) emitAgentMessage(record Record) {
diff --git a/cli/beacon/internal/codexsession/mapper.go b/cli/beacon/internal/codexsession/mapper.go
index e37898e6..50ec8cac 100644
--- a/cli/beacon/internal/codexsession/mapper.go
+++ b/cli/beacon/internal/codexsession/mapper.go
@@ -294,6 +294,11 @@ func (m *mapper) emitPrompt(record Record, text string) {
ev.Content = contentMarker(text)
ev.GenAI = mergeGenAI(ev.GenAI, &schema.GenAIInfo{Input: &schema.GenAIInputInfo{Messages: asymptoteobserve.TextInputMessages(text)}})
m.append(record, "prompt", ev)
+ if info, ok := asymptoteobserve.ParseHandoffMarker(text); ok {
+ link := m.base(record, "session.handoff", "session", schema.SeverityInfo, schema.FidelityObserved, "Session continued from a "+info.SourceHarness+" session")
+ link.Handoff = &info
+ m.append(record, "handoff", link)
+ }
}
func (m *mapper) emitAgentMessage(record Record, text, responseID string) {
diff --git a/cli/beacon/internal/endpoint/schema/event.go b/cli/beacon/internal/endpoint/schema/event.go
index 3f172d0d..d3c67530 100644
--- a/cli/beacon/internal/endpoint/schema/event.go
+++ b/cli/beacon/internal/endpoint/schema/event.go
@@ -90,6 +90,7 @@ type MCPProtocolInfo = asymptoteobserve.MCPProtocolInfo
type MCPResourceInfo = asymptoteobserve.MCPResourceInfo
type MCPSessionInfo = asymptoteobserve.MCPSessionInfo
type ApprovalInfo = asymptoteobserve.ApprovalInfo
+type HandoffInfo = asymptoteobserve.HandoffInfo
type PolicyInfo = asymptoteobserve.PolicyInfo
type PromptInfo = asymptoteobserve.PromptInfo
type ContentInfo = asymptoteobserve.ContentInfo
diff --git a/cli/beacon/internal/handoff/link_test.go b/cli/beacon/internal/handoff/link_test.go
new file mode 100644
index 00000000..9a28469b
--- /dev/null
+++ b/cli/beacon/internal/handoff/link_test.go
@@ -0,0 +1,143 @@
+package handoff
+
+import (
+ "database/sql"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+)
+
+// writeHandoffPrompts makes every fixture session's first prompt the one `beacon handoff resume`
+// writes for a new session continuing a Claude Code session.
+func (f storeFixture) writeHandoffPrompts(t *testing.T, prompt string) {
+ t.Helper()
+ claudeMain := filepath.Join(f.dirs.ClaudeProjects, "-work-api", "claude-sess-1.jsonl")
+ writeFixture(t, claudeMain, jsonLine(t, map[string]interface{}{
+ "type": "user", "uuid": "u1", "sessionId": "claude-sess-1", "cwd": "/work/api", "timestamp": "2026-09-23T08:59:00.000Z",
+ "message": map[string]interface{}{"role": "user", "content": prompt},
+ }))
+ setModTime(t, claudeMain, claudeUpdated)
+
+ newer := filepath.Join(f.dirs.Codex, "sessions", "2026", "09", "21", "rollout-2026-09-21T09-00-00-codex-thread-1.jsonl")
+ writeFixture(t, newer, jsonLine(t, map[string]interface{}{
+ "timestamp": "2026-09-21T09:00:00.000Z", "type": "session_meta",
+ "payload": map[string]interface{}{"id": "codex-thread-1", "session_id": "codex-thread-1", "cwd": "/work/web"},
+ })+jsonLine(t, map[string]interface{}{
+ "timestamp": "2026-09-21T09:00:01.000Z", "type": "response_item",
+ "payload": map[string]interface{}{"type": "message", "role": "user", "content": []interface{}{map[string]interface{}{"type": "input_text", "text": prompt}}},
+ }))
+ setModTime(t, newer, codexUpdated)
+
+ db, err := sql.Open("sqlite", filepath.Join(f.dirs.OpenCode, "opencode.db"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer db.Close()
+ created := openCodeUpdated.UnixMilli() - 900
+ if _, err := db.Exec(`INSERT INTO message VALUES ('msg_h', 'ses_parent', ?, ?)`, created, mustJSON(t, map[string]interface{}{
+ "id": "msg_h", "role": "user", "time": map[string]interface{}{"created": created},
+ })); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := db.Exec(`INSERT INTO part VALUES ('part_h', 'msg_h', 'ses_parent', ?, ?)`, created+10, mustJSON(t, map[string]interface{}{
+ "id": "part_h", "type": "text", "text": prompt,
+ })); err != nil {
+ t.Fatal(err)
+ }
+
+ messages := filepath.Join(f.dirs.Cline, "data", "sessions", "cline-task-1", "cline-task-1.messages.json")
+ writeFixture(t, messages, mustJSON(t, map[string]interface{}{"messages": []interface{}{
+ map[string]interface{}{"role": "user", "content": prompt, "ts": clineUpdated.UnixMilli()},
+ }}))
+ setModTime(t, messages, clineUpdated)
+}
+
+// Codex prompts reach Beacon through its session store rather than a prompt hook, so the store
+// mappers are where a session continued in Codex gets linked. The other three are linked here too,
+// for sessions whose hooks were not installed when the prompt was sent.
+func TestSessionStoreMappersLinkAHandedOffSession(t *testing.T) {
+ testenv.SetHome(t, t.TempDir())
+ f := newStoreFixture(t)
+ marker := asymptoteobserve.HandoffMarker(HarnessClaude, "src-session-1")
+ f.writeHandoffPrompts(t, "Continue the work from an earlier Claude Code session.\n\n"+marker)
+ sources := DefaultSources(f.dirs)
+
+ for _, tc := range []struct{ harness, id string }{
+ {HarnessClaude, "claude-sess-1"},
+ {HarnessCodex, "codex-thread-1"},
+ {HarnessOpenCode, "ses_parent"},
+ {HarnessCline, "cline-task-1"},
+ } {
+ t.Run(tc.harness, func(t *testing.T) {
+ session, err := Find(sources, tc.harness, tc.id)
+ if err != nil {
+ t.Fatal(err)
+ }
+ events, err := Events(sources, session)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var prompt, link *schema.Event
+ for i := range events {
+ switch events[i].Event.Action {
+ case "prompt.submitted":
+ if prompt == nil {
+ prompt = &events[i]
+ }
+ case "session.handoff":
+ if link != nil {
+ t.Fatalf("two session.handoff events for one prompt: %s", actionList(events))
+ }
+ link = &events[i]
+ }
+ }
+ if prompt == nil || link == nil {
+ t.Fatalf("events = %s, want a prompt and its session.handoff link", actionList(events))
+ }
+ if link.Handoff == nil || link.Handoff.SourceHarness != HarnessClaude || link.Handoff.SourceSessionID != "src-session-1" {
+ t.Fatalf("handoff = %+v", link.Handoff)
+ }
+ if link.Session == nil || link.Session.ID != tc.id {
+ t.Fatalf("link session = %+v, want %s", link.Session, tc.id)
+ }
+ if link.Harness.Name != tc.harness || link.Harness.CollectionMethod != schema.CollectionMethodPoll || link.Event.Fidelity != schema.FidelityObserved {
+ t.Fatalf("link provenance = %+v / %q", link.Harness, link.Event.Fidelity)
+ }
+ if link.Prompt != nil || link.Content != nil {
+ t.Fatal("the link must not repeat the prompt text")
+ }
+ if link.Event.ID == "" || link.Event.ID == prompt.Event.ID {
+ t.Fatalf("link id %q must be its own and deterministic (prompt %q)", link.Event.ID, prompt.Event.ID)
+ }
+ again, _ := Events(sources, session)
+ for _, e := range again {
+ if e.Event.Action == "session.handoff" && e.Event.ID != link.Event.ID {
+ t.Fatalf("re-reading the store renamed the link: %q then %q", link.Event.ID, e.Event.ID)
+ }
+ }
+ })
+ }
+}
+
+func TestSessionStoreMappersDoNotLinkWithoutAMarker(t *testing.T) {
+ testenv.SetHome(t, t.TempDir())
+ f := newStoreFixture(t)
+ f.writeHandoffPrompts(t, "an ordinary prompt that mentions beacon-handoff in passing")
+ sources := DefaultSources(f.dirs)
+ for _, h := range Harnesses {
+ sessions, _ := List(sources, Filter{Harness: h})
+ for _, s := range sessions {
+ events, err := Events(sources, s)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(actionList(events), "session.handoff") {
+ t.Fatalf("%s linked a session with no marker: %s", h, actionList(events))
+ }
+ }
+ }
+}
diff --git a/cli/beacon/internal/opencodesession/mapper.go b/cli/beacon/internal/opencodesession/mapper.go
index d5c674b9..ec877a8c 100644
--- a/cli/beacon/internal/opencodesession/mapper.go
+++ b/cli/beacon/internal/opencodesession/mapper.go
@@ -97,6 +97,11 @@ func (m *mapper) emitPrompt(record Record) {
ev.Content = asymptoteobserve.RetainedContent(text, asymptoteobserve.DefaultStringLimit)
ev.GenAI = &schema.GenAIInfo{Input: &schema.GenAIInputInfo{Messages: asymptoteobserve.TextInputMessages(text)}}
m.append(record, "prompt", ev)
+ if info, ok := asymptoteobserve.ParseHandoffMarker(text); ok {
+ link := m.base(record, "session.handoff", "session", schema.SeverityInfo, "Session continued from a "+info.SourceHarness+" session")
+ link.Handoff = &info
+ m.append(record, "handoff", link)
+ }
}
func (m *mapper) emitAgentMessage(record Record) {
diff --git a/docs/cli/handoff.mdx b/docs/cli/handoff.mdx
index 8616894f..2abe676d 100644
--- a/docs/cli/handoff.mdx
+++ b/docs/cli/handoff.mdx
@@ -151,6 +151,11 @@ When Beacon's hook adapter records a prompt carrying this marker, it writes a se
The link event keeps the new session's id, workspace and harness, and leaves the prompt text to the prompt event. Rules can match on `e.handoff.source_harness` and `e.handoff.source_session_id`.
-The live hook paths for Claude Code, Cline and OpenCode record the link. Codex CLI prompts reach Beacon over OTLP and through `beacon endpoint codex sync`, not through a prompt hook, so a session continued in Codex is not linked yet.
+Two paths record the link:
+
+- **Live hooks.** The prompt hook for Claude Code and the Cline and OpenCode plugins record it as the prompt is sent (`harness.collection_method: hook`).
+- **Session-store sync.** `beacon endpoint claude sync`, `codex sync`, `opencode sync` and `cline sync` record it when they read the prompt back from the runtime's store (`harness.collection_method: poll`).
+
+Codex CLI prompts reach Beacon over OTLP and through `beacon endpoint codex sync`, not a prompt hook, so a session continued in Codex is linked by the sync. For the other three runtimes, a sync can record the same link a hook already did; the two carry different `collection_method` values, as every other event those runtimes report through both paths does. A sync's link has a deterministic `event.id`, so syncing again never duplicates it.
The marker is a claim made in prompt text, not a verified identity: anyone can type it. `fidelity: observed` records that the marker was seen, not that `beacon handoff resume` wrote it. A native resume keeps the session's own id, so it needs no link.