Skip to content

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

Fix DeepSeek backfill retry metadata

Justin D'Souza9c08bfccdff5
diff --git a/cli/beacon/internal/dshsession/collect.go b/cli/beacon/internal/dshsession/collect.go
index cd4f0458..3209467d 100644
--- a/cli/beacon/internal/dshsession/collect.go
+++ b/cli/beacon/internal/dshsession/collect.go
@@ -219,8 +219,10 @@ func advanceCursorPartial(cursor *Cursor, ref SessionRef, mapped []MappedEvent,
if lastLine > cursor.LastLine {
cursor.LastLine = lastLine
}
- cursor.SizeBytes = ref.SizeBytes
- cursor.ModTimeMS = ref.ModTimeUnixMS
+ // Do not stamp the file's current size/mtime here. The cheap skip at the
+ // start of the next sweep treats matching size+mtime as "nothing left to
+ // read"; after a partial emit, there is explicitly work left to retry.
+ cursor.PartialTail = true
}
func emit(event schema.Event, opts CollectOptions) error {
diff --git a/cli/beacon/internal/dshsession/collect_test.go b/cli/beacon/internal/dshsession/collect_test.go
index 645dd322..347d1f7f 100644
--- a/cli/beacon/internal/dshsession/collect_test.go
+++ b/cli/beacon/internal/dshsession/collect_test.go
@@ -16,7 +16,7 @@ func TestStoreReadsPlainAndMapsDeepSeekSession(t *testing.T) {
root := t.TempDir()
sessionDir := filepath.Join(root, "sessions", "s1")
writeSession(t, sessionDir, SessionFileJSON,
- record("session", map[string]interface{}{"id": "s1", "cwd": "/repo"}),
+ record("session", map[string]interface{}{"id": "s1", "cwd": "/repo", "parentSession": "parent-1"}),
record("request/header", map[string]interface{}{"header": map[string]interface{}{"config": map[string]interface{}{"model": "deepseek-chat"}}}),
record("user/message", map[string]interface{}{"message": map[string]interface{}{"content": "fix tests"}}),
record("assistant/message", map[string]interface{}{"message": map[string]interface{}{
@@ -69,12 +69,47 @@ func TestStoreReadsPlainAndMapsDeepSeekSession(t *testing.T) {
if usage.GenAI == nil || usage.GenAI.Usage == nil || usage.GenAI.Usage.InputTokens == nil || *usage.GenAI.Usage.InputTokens != 11 {
t.Fatalf("usage = %+v", usage.GenAI)
}
+ if got := usage.Raw["dsh"].(map[string]interface{})["parent_session"]; got != "parent-1" {
+ t.Fatalf("raw.dsh.parent_session = %v, want parent-1", got)
+ }
failed := findAction(t, mapped, "tool.failed")
if failed.File != nil && failed.File.Diff != "" {
t.Fatalf("failed write carried a diff: %+v", failed.File)
}
}
+func TestMapSessionPreservesDiffWhitespace(t *testing.T) {
+ ref := SessionRef{ID: "s-diff", Path: "/tmp/session.jsonl", ModTimeUnixMS: 1}
+ records := []Record{
+ mustRecord(t, 1, record("session", map[string]interface{}{"id": "s-diff", "cwd": "/repo"})),
+ mustRecord(t, 2, record("tool/call", map[string]interface{}{"callId": "call_1", "name": "write", "arguments": map[string]interface{}{"file_path": "/repo/a.txt", "content": "line with trailing spaces \n"}})),
+ mustRecord(t, 3, record("tool/result", map[string]interface{}{"callId": "call_1", "toolName": "write", "content": "ok"})),
+ }
+ event := findAction(t, MapSession(ref, records, MapOptions{}), "file.modified")
+ if event.File == nil || !strings.Contains(event.File.Diff, "+line with trailing spaces \n+\n") {
+ t.Fatalf("diff = %q, want trailing spaces and newline preserved", event.File.Diff)
+ }
+}
+
+func TestAdvanceCursorPartialLeavesChangedFileRetryable(t *testing.T) {
+ cursor := &Cursor{LastLine: 1, SizeBytes: 100, ModTimeMS: 10}
+ ref := SessionRef{SizeBytes: 200, ModTimeUnixMS: 20}
+ mapped := []MappedEvent{
+ {SourceLine: 2, Event: schema.Event{Event: schema.EventInfo{Action: "prompt.submitted"}}},
+ {SourceLine: 3, Event: schema.Event{Event: schema.EventInfo{Action: "agent.message"}}},
+ }
+ advanceCursorPartial(cursor, ref, mapped, 1)
+ if cursor.LastLine != 2 {
+ t.Fatalf("LastLine = %d, want 2", cursor.LastLine)
+ }
+ if cursor.SizeBytes == ref.SizeBytes || cursor.ModTimeMS == ref.ModTimeUnixMS {
+ t.Fatalf("partial cursor stamped changed file markers: %+v", cursor)
+ }
+ if !cursor.PartialTail {
+ t.Fatalf("partial cursor did not force retry: %+v", cursor)
+ }
+}
+
func TestStoreReadsCompleteZstdFramesAndDefersPartialTail(t *testing.T) {
root := t.TempDir()
sessionDir := filepath.Join(root, "sessions", "z1")
@@ -130,6 +165,15 @@ func record(kind string, data map[string]interface{}) string {
return string(line)
}
+func mustRecord(t *testing.T, line int, text string) Record {
+ t.Helper()
+ record, err := decodeRecord([]byte(text), line)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return *record
+}
+
func writeSession(t *testing.T, dir, name string, lines ...string) {
t.Helper()
writeBytes(t, filepath.Join(dir, name), []byte(strings.Join(lines, "\n")+"\n"))
diff --git a/cli/beacon/internal/dshsession/mapper.go b/cli/beacon/internal/dshsession/mapper.go
index fe9f54a5..5f8e0d77 100644
--- a/cli/beacon/internal/dshsession/mapper.go
+++ b/cli/beacon/internal/dshsession/mapper.go
@@ -116,7 +116,7 @@ func (m *mapper) consume(record Record) {
func (m *mapper) emitSessionStarted(record Record) {
ev := m.base(record, "session.started", "session", schema.SeverityInfo, "DeepSeek Harness session started")
- ev.Raw = rawDsh(record, map[string]interface{}{"source_path": m.ref.Path})
+ ev.Raw = m.rawDsh(record, map[string]interface{}{"source_path": m.ref.Path})
m.append(record, "session.started", ev)
}
@@ -131,7 +131,7 @@ func (m *mapper) emitPrompt(record Record) {
ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) {
genAI.Input = &schema.GenAIInputInfo{Messages: asymptoteobserve.TextInputMessages(text)}
})
- ev.Raw = rawDsh(record, nil)
+ ev.Raw = m.rawDsh(record, nil)
m.append(record, "prompt.submitted", ev)
}
@@ -149,7 +149,7 @@ func (m *mapper) emitAssistant(record Record) {
}}}
})
ev.Content = asymptoteobserve.RetainedContent(part.text, asymptoteobserve.DefaultRawStringLimit)
- ev.Raw = rawDsh(record, nil)
+ ev.Raw = m.rawDsh(record, nil)
m.append(record, fmt.Sprintf("assistant.reasoning.%d", i), ev)
case "text":
ev := m.base(record, "agent.message", "agent", schema.SeverityInfo, "DeepSeek assistant message")
@@ -158,7 +158,7 @@ func (m *mapper) emitAssistant(record Record) {
genAI.Output = &schema.GenAIOutputInfo{Messages: asymptoteobserve.TextOutputMessages(part.text)}
})
ev.Content = asymptoteobserve.RetainedContent(part.text, asymptoteobserve.DefaultRawStringLimit)
- ev.Raw = rawDsh(record, nil)
+ ev.Raw = m.rawDsh(record, nil)
m.append(record, fmt.Sprintf("assistant.text.%d", i), ev)
}
}
@@ -176,7 +176,7 @@ func (m *mapper) emitToolCall(record Record) {
ev := m.base(record, "tool.invoked", "tool", schema.SeverityInfo, "DeepSeek tool invoked")
ev.Tool = &schema.ToolInfo{Name: call.Name}
applyCall(&ev, call)
- ev.Raw = rawDsh(record, nil)
+ ev.Raw = m.rawDsh(record, nil)
m.append(record, "tool.call", ev)
}
@@ -207,7 +207,7 @@ func (m *mapper) emitToolResult(record Record) {
if result.IsError {
ev.Error = &schema.ErrorInfo{Type: "tool_execution_failed"}
}
- ev.Raw = rawDsh(record, map[string]interface{}{"tool_result": result.Raw})
+ ev.Raw = m.rawDsh(record, map[string]interface{}{"tool_result": result.Raw})
m.append(record, "tool.result", ev)
}
@@ -219,7 +219,7 @@ func (m *mapper) emitTurnEnd(record Record) {
}
ev := m.base(record, "agent.error", "session", schema.SeverityHigh, "DeepSeek turn ended with error")
ev.Error = &schema.ErrorInfo{Type: kind}
- ev.Raw = rawDsh(record, nil)
+ ev.Raw = m.rawDsh(record, nil)
m.append(record, "turn.end."+kind, ev)
}
@@ -231,7 +231,7 @@ func (m *mapper) emitUsage(record Record) {
ev := m.base(record, "token.usage", "metric", schema.SeverityInfo, "DeepSeek token usage")
ev.GenAI = withGenAI(ev.GenAI, func(genAI *schema.GenAIInfo) { genAI.Usage = usage })
ev.Model = firstNonEmpty(assistantModel(record.Data), m.model, m.session.Model)
- ev.Raw = rawDsh(record, map[string]interface{}{"token_source": "assistant_message"})
+ ev.Raw = m.rawDsh(record, map[string]interface{}{"token_source": "assistant_message"})
m.append(record, "usage", ev)
}
@@ -379,27 +379,27 @@ func diffFromCall(call toolCall) string {
path := firstString(call.Arguments, "file_path", "path")
switch name {
case "write":
- content := firstString(call.Arguments, "content", "file_text")
+ content := firstRawString(call.Arguments, "content", "file_text")
if path != "" && content != "" {
return "--- /dev/null\n+++ " + path + "\n+" + strings.ReplaceAll(content, "\n", "\n+") + "\n"
}
case "edit":
- oldText := firstString(call.Arguments, "old_string", "old_str")
- newText := firstString(call.Arguments, "new_string", "new_str")
+ oldText := firstRawString(call.Arguments, "old_string", "old_str")
+ newText := firstRawString(call.Arguments, "new_string", "new_str")
if oldText != "" || newText != "" {
return "-" + strings.ReplaceAll(oldText, "\n", "\n-") + "\n+" + strings.ReplaceAll(newText, "\n", "\n+") + "\n"
}
case "str_replace_editor":
op := dshEditorOperation(call.Name, call.Arguments)
if op == "create" {
- content := firstString(call.Arguments, "file_text")
+ content := firstRawString(call.Arguments, "file_text")
if content != "" {
return "--- /dev/null\n+++ " + path + "\n+" + strings.ReplaceAll(content, "\n", "\n+") + "\n"
}
}
if op == "str_replace" || op == "insert" {
- oldText := firstString(call.Arguments, "old_str")
- newText := firstString(call.Arguments, "new_str", "insert")
+ oldText := firstRawString(call.Arguments, "old_str")
+ newText := firstRawString(call.Arguments, "new_str", "insert")
if oldText != "" || newText != "" {
return "-" + strings.ReplaceAll(oldText, "\n", "\n-") + "\n+" + strings.ReplaceAll(newText, "\n", "\n+") + "\n"
}
@@ -493,7 +493,7 @@ func assistantModel(data map[string]interface{}) string {
return firstNonEmpty(firstString(source, "model"), firstString(msg, "model"))
}
-func rawDsh(record Record, extra map[string]interface{}) map[string]interface{} {
+func (m *mapper) rawDsh(record Record, extra map[string]interface{}) map[string]interface{} {
raw := map[string]interface{}{}
if len(record.Raw) > 0 {
var decoded map[string]interface{}
@@ -501,6 +501,9 @@ func rawDsh(record Record, extra map[string]interface{}) map[string]interface{}
raw["record"] = decoded
}
}
+ if m.session.ParentSessionID != "" {
+ raw["parent_session"] = m.session.ParentSessionID
+ }
for k, v := range extra {
raw[k] = v
}
@@ -618,6 +621,20 @@ func firstString(data map[string]interface{}, keys ...string) string {
return ""
}
+func firstRawString(data map[string]interface{}, keys ...string) string {
+ for _, key := range keys {
+ if value, ok := data[key]; ok {
+ if text, ok := value.(string); ok {
+ return text
+ }
+ if value != nil {
+ return fmt.Sprint(value)
+ }
+ }
+ }
+ return ""
+}
+
func firstNonEmpty(values ...string) string {
for _, value := range values {
if strings.TrimSpace(value) != "" {