The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
package dshsession
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
"github.com/klauspost/compress/zstd"
)
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", "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{}{
"source": map[string]interface{}{"model": "deepseek-chat"},
"content": []interface{}{
map[string]interface{}{"type": "reasoning", "text": "Need to inspect failure."},
map[string]interface{}{"type": "text", "text": "I will run tests."},
},
"usage": map[string]interface{}{"inputTokens": 11, "outputTokens": 7, "cacheReadTokens": 3, "cacheWriteTokens": 2, "reasoningTokens": 5},
}}),
record("tool/call", map[string]interface{}{"callId": "call_1", "name": "bash", "arguments": map[string]interface{}{"command": "go test ./..."}}),
record("tool/result", map[string]interface{}{"callId": "call_1", "toolName": "bash", "content": "FAIL\n[exit code: 1]"}),
record("tool/call", map[string]interface{}{"callId": "call_2", "name": "write", "arguments": map[string]interface{}{"file_path": "/repo/new.go", "content": "package main\n"}}),
record("tool/result", map[string]interface{}{"callId": "call_2", "toolName": "write", "content": "permission denied", "isError": true}),
)
store, err := NewStore(root)
if err != nil {
t.Fatal(err)
}
refs, err := store.List()
if err != nil {
t.Fatal(err)
}
if len(refs) != 1 || refs[0].ID != "s1" || refs[0].Meta.CWD != "/repo" {
t.Fatalf("refs = %+v", refs)
}
records, stats, err := store.Read(refs[0])
if err != nil {
t.Fatal(err)
}
if stats.Decoded != 8 {
t.Fatalf("decoded = %d, want 8", stats.Decoded)
}
mapped := MapSession(refs[0], records, MapOptions{})
actions := actions(mapped)
for _, want := range []string{"session.started", "prompt.submitted", "agent.reasoning", "agent.message", "token.usage", "command.executed", "tool.failed"} {
if !contains(actions, want) {
t.Fatalf("actions = %v, missing %s", actions, want)
}
}
command := findAction(t, mapped, "command.executed")
if command.Command == nil || command.Command.Command != "go test ./..." {
t.Fatalf("command event = %+v", command)
}
if command.Command.ExitCode == nil || *command.Command.ExitCode != 1 {
t.Fatalf("exit code = %+v, want 1", command.Command)
}
usage := findAction(t, mapped, "token.usage")
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 record(kind string, data map[string]interface{}) string {
line, _ := json.Marshal(map[string]interface{}{"type": kind, "time": "2026-09-21T10:00:00Z", "data": data})
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"))
}
func writeBytes(t *testing.T, path string, data []byte) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, data, 0644); err != nil {
t.Fatal(err)
}
}
func zstdFrame(t *testing.T, data []byte) []byte {
t.Helper()
enc, err := zstd.NewWriter(nil)
if err != nil {
t.Fatal(err)
}
defer enc.Close()
return enc.EncodeAll(data, nil)
}
func actions(mapped []MappedEvent) []string {
out := make([]string, 0, len(mapped))
for _, item := range mapped {
out = append(out, item.Event.Event.Action)
}
return out
}
func contains(values []string, value string) bool {
for _, candidate := range values {
if candidate == value {
return true
}
}
return false
}
func findAction(t *testing.T, mapped []MappedEvent, action string) schema.Event {
t.Helper()
for _, item := range mapped {
if item.Event.Event.Action == action {
return item.Event
}
}
t.Fatalf("missing action %s in %v", action, actions(mapped))
return schema.Event{}
}