Skip to content

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

Pair a prompt captured without prompt.id with its keyed copy

Claudee6b635df530a
diff --git a/pkg/asymptoteobserve/share.go b/pkg/asymptoteobserve/share.go
index 6d664d8e..f2a204ad 100644
--- a/pkg/asymptoteobserve/share.go
+++ b/pkg/asymptoteobserve/share.go
@@ -430,6 +430,7 @@ func BuildShareMessagesWithOptions(events []Event, opts ShareOptions) []ShareMes
results: map[string]*ShareToolResultContent{},
callMessage: map[string]*ShareMessageV1{},
prompts: map[string]*ShareMessageV1{},
+ lastPrompt: map[string]*sharePromptRef{},
}
for _, item := range orderShareEvents(events) {
b.add(item)
@@ -521,10 +522,24 @@ type shareBuilder struct {
// a completion to pair with by tool name.
pending []string
// prompts holds user messages by harness, session and prompt.id, so one prompt captured by two
- // paths (a hook and the OTLP export) is written once.
- prompts map[string]*ShareMessageV1
+ // paths (a hook and the OTLP export) is written once. lastPrompt holds the newest user message
+ // per harness and session, for the copy that carries no prompt.id (Claude Code's
+ // UserPromptSubmit hook runs before the prompt is recorded, so it has none).
+ prompts map[string]*ShareMessageV1
+ lastPrompt map[string]*sharePromptRef
}
+type sharePromptRef struct {
+ message *ShareMessageV1
+ keyed bool
+ ts time.Time
+ hasTS bool
+}
+
+// sharePromptPairWindow is how far apart two captures of one prompt may be. The hook and the
+// export record the same submission within moments of each other.
+const sharePromptPairWindow = 10 * time.Second
+
type shareEventClass int
const (
@@ -625,7 +640,7 @@ func (b *shareBuilder) add(item shareEvent) {
case sharePrompt:
text := sharePromptText(event)
key := sharePromptKey(event)
- if existing := b.prompts[key]; key != "" && existing != nil {
+ if existing := b.samePrompt(item, key, text); existing != nil {
// The same prompt from a second capture path: fill in the text if the first copy
// had none, and write nothing new.
if existing.Text == sharePromptNotRetained && text != "" {
@@ -639,9 +654,11 @@ func (b *shareBuilder) add(item shareEvent) {
text = sharePromptNotRetained
}
b.appendText(item, ShareRoleUser, SharePartText, text)
+ message := b.messages[len(b.messages)-1]
if key != "" {
- b.prompts[key] = b.messages[len(b.messages)-1]
+ b.prompts[key] = message
}
+ b.lastPrompt[sharePromptScope(event)] = &sharePromptRef{message: message, keyed: key != "", ts: item.ts, hasTS: item.hasTS}
case shareAgentText:
text := shareAssistantText(event, GenAIPartTypeText)
if text == "" {
@@ -668,6 +685,45 @@ func (b *shareBuilder) add(item shareEvent) {
const sharePromptNotRetained = "(prompt text not retained)"
+// samePrompt finds the user message an earlier capture of this prompt wrote. A prompt.id match is
+// exact. Without one, the newest prompt in the session counts as the same submission only when
+// exactly one of the two copies carries a prompt.id (they came from different capture paths), they
+// are within sharePromptPairWindow, and their texts agree or one of them retained none.
+func (b *shareBuilder) samePrompt(item shareEvent, key, text string) *ShareMessageV1 {
+ if key != "" {
+ if existing := b.prompts[key]; existing != nil {
+ return existing
+ }
+ }
+ last := b.lastPrompt[sharePromptScope(item.event)]
+ if last == nil || last.keyed == (key != "") || !last.hasTS || !item.hasTS {
+ return nil
+ }
+ gap := item.ts.Sub(last.ts)
+ if gap < 0 {
+ gap = -gap
+ }
+ if gap > sharePromptPairWindow {
+ return nil
+ }
+ if text != "" && last.message.Text != sharePromptNotRetained && b.scrub.text(text) != last.message.Text {
+ return nil
+ }
+ if key != "" {
+ b.prompts[key] = last.message
+ }
+ last.keyed = true
+ return last.message
+}
+
+func sharePromptScope(event Event) string {
+ session := ""
+ if event.Session != nil {
+ session = event.Session.ID
+ }
+ return NormalizeHarnessName(event.Harness.Name) + "\x00" + session
+}
+
// sharePromptKey names one user prompt across capture paths: prompt.id is the runtime's own id for
// the turn, which a hook and the OTLP export both carry.
func sharePromptKey(event Event) string {
diff --git a/pkg/asymptoteobserve/share_test.go b/pkg/asymptoteobserve/share_test.go
index 32040d4b..edb8200f 100644
--- a/pkg/asymptoteobserve/share_test.go
+++ b/pkg/asymptoteobserve/share_test.go
@@ -568,7 +568,7 @@ func TestBuildShareMessagesWritesAPromptCapturedTwiceOnce(t *testing.T) {
otlp.Prompt = &PromptInfo{ID: "prompt-1"}
hook := shareEventAt("2026-10-01T10:00:00.5Z", "claude_code", CollectionMethodHook, "prompt.submitted", "prompt")
hook.Event.ID = "p-hook"
- hook.Prompt = &PromptInfo{ID: "prompt-1", Text: "fix the test"}
+ hook.Prompt = &PromptInfo{Text: "fix the test"} // the UserPromptSubmit hook carries no prompt.id
next := shareEventAt("2026-10-01T10:01:00Z", "claude_code", CollectionMethodHook, "prompt.submitted", "prompt")
next.Event.ID = "p-next"
next.Prompt = &PromptInfo{ID: "prompt-2", Text: "fix the test"}
@@ -581,3 +581,38 @@ func TestBuildShareMessagesWritesAPromptCapturedTwiceOnce(t *testing.T) {
t.Fatalf("first prompt = %+v", messages[0])
}
}
+
+func TestBuildShareMessagesPairsPromptCapturesOnlyAcrossPaths(t *testing.T) {
+ prompt := func(id, ts, method, promptID, text string) Event {
+ e := shareEventAt(ts, "claude_code", method, "prompt.submitted", "prompt")
+ e.Event.ID = id
+ e.Prompt = &PromptInfo{ID: promptID, Text: text}
+ return e
+ }
+ // Hook first, export second: one message.
+ got := BuildShareMessagesWithOptions([]Event{
+ prompt("h1", "2026-10-01T10:00:00Z", CollectionMethodHook, "", "continue"),
+ prompt("o1", "2026-10-01T10:00:01Z", CollectionMethodOTLP, "p-1", ""),
+ }, shareTestOpts)
+ if len(got) != 1 || got[0].Text != "continue" {
+ t.Fatalf("hook then export = %+v", got)
+ }
+ // Two hook prompts with the same text moments apart are two submissions.
+ got = BuildShareMessagesWithOptions([]Event{
+ prompt("h1", "2026-10-01T10:00:00Z", CollectionMethodHook, "", "continue"),
+ prompt("h2", "2026-10-01T10:00:03Z", CollectionMethodHook, "", "continue"),
+ }, shareTestOpts)
+ if len(got) != 2 {
+ t.Fatalf("two hook prompts = %+v", got)
+ }
+ // A keyless copy far from the keyed one, or with different text, stays separate.
+ got = BuildShareMessagesWithOptions([]Event{
+ prompt("o1", "2026-10-01T10:00:00Z", CollectionMethodOTLP, "p-1", "first"),
+ prompt("h1", "2026-10-01T10:05:00Z", CollectionMethodHook, "", "first"),
+ prompt("o2", "2026-10-01T10:06:00Z", CollectionMethodOTLP, "p-2", "second"),
+ prompt("h2", "2026-10-01T10:06:01Z", CollectionMethodHook, "", "other"),
+ }, shareTestOpts)
+ if len(got) != 4 {
+ t.Fatalf("unrelated prompts merged: %+v", got)
+ }
+}