From 3e345b751416e94f5dfa359359ad591d4a60db9e Mon Sep 17 00:00:00 2001 From: Lumea-art Date: Thu, 12 Nov 2026 22:14:41 +0800 Subject: [PATCH] feat(cli): import DeepSeek Harness sessions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `evercli import conversations` had no reader for dsh, even though dsh is already an install target — so the only EverMe memory a dsh user could get was whatever the native plugin captured after install, never the history that made the agent useful in the first place. The harness keeps one zstd-compressed JSONL log per session at `/sessions///session.v4.jsonl.zstd`. The reader maps its event stream onto the existing importer DTO: user/message -> role user (text blocks) assistant/message -> role assistant (text + its tool-call blocks) tool/result -> role tool, paired by toolCallId `tool/call` records duplicate the assistant message's own tool-call blocks one-for-one (206 of 206 call ids on a real session), so a call is only imported from that record when no assistant message carried it. Everything else is bookkeeping: `system/message` (instructions and compaction context), `reasoning` blocks, step/turn boundaries, approvals, sandbox mode, jobs, todo writes, slash-command output and delivery receipts. `agent/inbox/spliced` replays a `user/message` recorded separately under the same id. A newer session `version` still imports the records it understands and warns, so a future dsh release degrades to "some records skipped" instead of a silently partial session. Every unknown event type warns once per session, which is how the known-skip list above was completed: 26 real sessions on a development machine import with zero warnings. `ResolveEvt(dsh)` reads `/.env`, the dotenv file `evercli plugin install dsh` already writes, so `run` works as well as `scan`. github.com/klauspost/compress was already in the module graph as an indirect dependency; it is promoted to a direct one at the same version, with no bump. --- CHANGELOG.md | 5 + cli/cmd/imports/conversations.go | 4 +- cli/go.mod | 1 + cli/go.sum | 2 + cli/internal/importer/conversation/dsh.go | 410 ++++++++++++++++++ .../importer/conversation/dsh_test.go | 253 +++++++++++ cli/internal/importer/conversation/evt.go | 10 +- .../importer/conversation/platforms.go | 3 +- .../importer/conversation/registry.go | 6 + cli/internal/importer/conversation/types.go | 1 + 10 files changed, 690 insertions(+), 5 deletions(-) create mode 100644 cli/internal/importer/conversation/dsh.go create mode 100644 cli/internal/importer/conversation/dsh_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index e1a8b04..11adeb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,11 @@ the open-source home for EverMe CLI and agent plugins. opencode, Raven, and WorkBuddy. - Force `0600` on host config files that store an EverMe agent token. - Stop dropping Codex tool families and non-main OpenClaw agents during import. +- Import DeepSeek Harness history: `evercli import conversations scan --platform + dsh` reads `/sessions/*/session-*/session.v*.jsonl.zstd` (zstd + compressed JSONL) and maps its user / assistant / tool-result records, so a + `dsh` user can seed EverMe memory with sessions the native plugin never + captured. - Bump the `@everme/cli` npm wrapper to 0.32.0 so wrapper and binary versions match, and print a per-host install hint from its postinstall refresh. - Format the tree with `gofmt` and tidy `go.mod` / `go.sum`. diff --git a/cli/cmd/imports/conversations.go b/cli/cmd/imports/conversations.go index d0c6d3a..042dfc3 100644 --- a/cli/cmd/imports/conversations.go +++ b/cli/cmd/imports/conversations.go @@ -474,7 +474,7 @@ Use --path to override the scan root for a specific platform.`, } else { ids, perr := conversation.ParsePlatforms(requested) if perr != nil { - return deps.Out.Err(output.Invalid(perr.Error(), "Use one of: claude-code, codex, hermes, openclaw, markdown, kimicode, raven, workbuddy")) + return deps.Out.Err(output.Invalid(perr.Error(), "Use one of: claude-code, codex, hermes, openclaw, markdown, kimicode, raven, workbuddy, dsh")) } platformIDs = ids } @@ -652,7 +652,7 @@ Flags: } else { ids, perr := conversation.ParsePlatforms(effectivePlatforms) if perr != nil { - return deps.Out.Err(output.Invalid(perr.Error(), "Use one of: claude-code, codex, hermes, openclaw, markdown, kimicode, raven, workbuddy")) + return deps.Out.Err(output.Invalid(perr.Error(), "Use one of: claude-code, codex, hermes, openclaw, markdown, kimicode, raven, workbuddy, dsh")) } platformIDs = ids } diff --git a/cli/go.mod b/cli/go.mod index c708361..b4c4bd9 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -8,6 +8,7 @@ require ( github.com/charmbracelet/bubbletea v1.3.10 github.com/charmbracelet/lipgloss v1.1.0 github.com/denisbrodbeck/machineid v1.0.1 + github.com/klauspost/compress v1.17.2 github.com/mattn/go-isatty v0.0.20 github.com/pelletier/go-toml/v2 v2.3.1 github.com/spf13/cobra v1.8.1 diff --git a/cli/go.sum b/cli/go.sum index d2af500..6e0497f 100644 --- a/cli/go.sum +++ b/cli/go.sum @@ -42,6 +42,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4= +github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= diff --git a/cli/internal/importer/conversation/dsh.go b/cli/internal/importer/conversation/dsh.go new file mode 100644 index 0000000..c017074 --- /dev/null +++ b/cli/internal/importer/conversation/dsh.go @@ -0,0 +1,410 @@ +package conversation + +import ( + "bufio" + "encoding/json" + "fmt" + "io" + "os" + "path/filepath" + "strings" + "time" + + "github.com/klauspost/compress/zstd" +) + +// DSHScanner parses DeepSeek Harness (dsh) session logs. +// +// Layout: one log per session, grouped by workspace: +// +// /sessions//session-/session.v4.jsonl.zstd +// +// The file is zstd-compressed JSONL: a `session` header line (version, id, +// createdAt, cwd) followed by event-sourced records of the shape +// {type, seq, time, data}. `time` is epoch milliseconds. +// +// Imported: user/message, assistant/message (its text plus the tool-call +// blocks it carries) and tool/result. The dedicated tool/call records +// duplicate the assistant message's own tool-call blocks one-for-one (206 of +// 206 call ids on a real session), so they are only used for a call that no +// assistant message carried — importing both would double-write every +// round-trip. +// +// Skipped: system/message (instructions and compaction context, not +// conversation), reasoning content blocks, and the bookkeeping event stream +// (step/turn boundaries, approvals, sandbox mode, jobs, todo writes, slash +// command output, delivery receipts). agent/inbox/spliced is skipped because +// it replays a user/message that is recorded separately under the same id. +type DSHScanner struct{} + +// dshSupportedFormatVersion is the session header's `version` this reader +// understands. A newer version is imported on a best-effort basis and warns, +// so a future DSH release degrades to "some records skipped" instead of +// silently importing a partial session. +const dshSupportedFormatVersion = 4 + +// dshKnownSkippedEvents are event types that carry no conversation text. +// An unrecognised type still produces one warning per session, so schema +// drift is visible in scan output instead of disappearing. +var dshKnownSkippedEvents = map[string]bool{ + "agent/inbox/spliced": true, // duplicate delivery, see the type comment + "approval/asked": true, + "approval/decided": true, + "approval/policy": true, + "assistant/attempt": true, // per-attempt stream metadata; the message is its own record + "command/done": true, + "command/run": true, + "compaction/end": true, + "compaction/prune": true, + "compaction/start": true, + "compaction/summary": true, // context-management summary, not a user/agent turn + "deliverables/presented": true, + "job/registered": true, + "job/settled": true, + "llm/retry": true, + "llm/retry-started": true, + "model/selection": true, + "permission/preset": true, + "request/context": true, + "request/header": true, + "sandbox/mode": true, + "session-log-deepseek/delivery-accepted": true, + "session/end-seed": true, + "session/title": true, + "session/title-llm-request": true, + "step/end": true, + "step/start": true, + "subagent/catalog": true, + "subagent/descriptor": true, // session header for the delegation preset + "system/message": true, + "todo/write": true, + "turn/end": true, + "turn/start": true, + "web/deepseek-search-llm-request": true, // request log for the search sub-model + "working-directory/change": true, + "workspace/changes": true, +} + +var _ Scanner = (*DSHScanner)(nil) + +// NewDSHScanner returns the DeepSeek Harness scanner. +func NewDSHScanner() *DSHScanner { return &DSHScanner{} } + +func (s *DSHScanner) Platform() PlatformID { return PlatformDSH } + +func (s *DSHScanner) Scan(roots []string) ([]Item, error) { + var items []Item + for _, root := range roots { + if _, err := os.Stat(root); os.IsNotExist(err) { + continue + } + err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { + if err != nil || d.IsDir() { + return nil + } + if !isDSHSessionFile(path) { + return nil + } + info, statErr := d.Info() + if statErr != nil || info == nil { + return nil // skip unreadable entry + } + items = append(items, Item{ + Platform: PlatformDSH, + Path: path, + OriginID: dshOriginID(path), + Status: "ready", + SizeBytes: info.Size(), + UpdatedAt: info.ModTime().UTC().Format(time.RFC3339), + }) + return nil + }) + if err != nil { + return nil, err + } + } + return items, nil +} + +func (s *DSHScanner) Read(item Item) (*Conversation, error) { + f, err := os.Open(item.Path) + if err != nil { + return nil, err + } + defer f.Close() + + var src io.Reader = f + if strings.HasSuffix(item.Path, ".zstd") { + zr, zerr := zstd.NewReader(f) + if zerr != nil { + return nil, fmt.Errorf("open zstd stream: %w", zerr) + } + defer zr.Close() + src = zr + } + + conv := &Conversation{Item: item} + + // Use the file mtime as the deterministic fallback timestamp — never + // time.Now(), which would break re-import idempotency. + fallbackBase := time.Unix(0, 0).UnixMilli() + if fi, statErr := os.Stat(item.Path); statErr == nil { + fallbackBase = fi.ModTime().UnixMilli() + } + + sc := bufio.NewScanner(src) + sc.Buffer(make([]byte, 0, 1024*1024), 64*1024*1024) + + const maxRunes = 8000 + + lineNum := 0 + headerID := "" + version := 0 + // attached tracks tool-call ids already imported with their assistant + // message, so the tool/call record for the same id is not a second copy. + attached := map[string]bool{} + // warned keeps one warning per unknown type instead of one per record. + warned := map[string]bool{} + + for sc.Scan() { + line := strings.TrimSpace(sc.Text()) + if line == "" { + continue + } + lineNum++ + + var ev map[string]any + if err := json.Unmarshal([]byte(line), &ev); err != nil { + conv.Warnings = append(conv.Warnings, fmt.Sprintf("line %d: json decode error: %v", lineNum, err)) + continue + } + + topType := stringFieldCC(ev, "type") + ts := normalizeTimestampCC(ev["time"], fallbackBase+int64(lineNum)) + data := objectMapCC(ev["data"]) + + if topType == "session" { + headerID = firstNonEmptyCC(stringFieldCC(ev, "id"), headerID) + if v, ok := ev["version"].(float64); ok { + version = int(v) + } + if version > dshSupportedFormatVersion { + conv.Warnings = append(conv.Warnings, fmt.Sprintf( + "line %d: session format v%d is newer than the supported v%d; unknown records are skipped", + lineNum, version, dshSupportedFormatVersion)) + } + continue + } + + switch topType { + case "user/message": + text := dshTextFromBlocks(data["content"], maxRunes, &conv.Warnings, warned, lineNum) + if text == "" { + continue + } + conv.Messages = append(conv.Messages, AgentMemoryMessage{ + Role: "user", + Timestamp: ts, + Content: Redact(text), + }) + + case "assistant/message": + msg := objectMapCC(data["message"]) + content := msg["content"] + text := dshTextFromBlocks(content, maxRunes, &conv.Warnings, warned, lineNum) + calls := dshToolCallsFromBlocks(content) + for _, c := range calls { + attached[c.ID] = true + } + if text == "" && len(calls) == 0 { + continue // reasoning-only step + } + out := AgentMemoryMessage{Role: "assistant", Timestamp: ts, ToolCalls: calls} + if text != "" { + out.Content = Redact(text) + } + conv.Messages = append(conv.Messages, out) + + case "tool/call": + // Normally already imported with the assistant message that + // emitted it; keep this path for a call that message did not carry. + callID := stringFieldCC(data, "callId") + if callID == "" { + if !warned["tool/call"] { + warned["tool/call"] = true + conv.Warnings = append(conv.Warnings, fmt.Sprintf("line %d: tool/call without callId, dropped", lineNum)) + } + continue + } + if attached[callID] { + continue + } + attached[callID] = true + conv.Messages = append(conv.Messages, AgentMemoryMessage{ + Role: "assistant", + Timestamp: ts, + ToolCalls: []AgentMemoryToolCall{{ + ID: callID, + Type: "function", + Name: firstNonEmptyCC(stringFieldCC(data, "name"), "unknown"), + Arguments: Redact(argumentsStringCC(data["arguments"])), + }}, + }) + + case "tool/result": + msg := objectMapCC(data["message"]) + callID := firstNonEmptyCC(stringFieldCC(msg, "toolCallId"), stringFieldCC(data, "toolCallId")) + if callID == "" { + if !warned["tool/result"] { + warned["tool/result"] = true + conv.Warnings = append(conv.Warnings, fmt.Sprintf("line %d: tool/result without toolCallId, dropped", lineNum)) + } + continue + } + text := dshTextFromBlocks(msg["content"], maxRunes, &conv.Warnings, warned, lineNum) + if text == "" { + text = "tool result" + } + if isErr, _ := msg["isError"].(bool); isErr { + text = "[error] " + text + } + conv.Messages = append(conv.Messages, AgentMemoryMessage{ + Role: "tool", + Timestamp: ts, + ToolCallID: callID, + Content: Redact(text), + }) + + default: + if topType != "" && !dshKnownSkippedEvents[topType] && !warned[topType] { + warned[topType] = true + conv.Warnings = append(conv.Warnings, fmt.Sprintf("line %d: unknown event type %q, skipped", lineNum, topType)) + } + } + } + if err := sc.Err(); err != nil { + return nil, fmt.Errorf("read session stream: %w", err) + } + + setStartedAtFromMessages(conv) + conv.ID = ConversationID(PlatformDSH, firstNonEmptyCC(headerID, item.OriginID), item.Path) + return conv, nil +} + +// isDSHSessionFile reports whether path is one of DSH's session logs. The +// basename carries the format version ("session.v4.jsonl.zstd"), and older +// uncompressed logs ("session.v3.jsonl") are accepted too. +func isDSHSessionFile(path string) bool { + base := filepath.Base(path) + if !strings.HasPrefix(base, "session.") { + return false + } + return strings.HasSuffix(base, ".jsonl") || strings.HasSuffix(base, ".jsonl.zstd") +} + +// dshOriginID returns the session id from the directory layout so scan output +// and the conversation id survive a file rename. Current DSH versions name the +// directory ""; older layouts used "session-", which is the same id +// the session header carries. Empty when the log lives elsewhere - Read then +// falls back to the header id or the path. +func dshOriginID(path string) string { + dir := filepath.Base(filepath.Dir(path)) + if strings.HasPrefix(dir, "session-") { + return dir + } + if isUUID(dir) { + return "session-" + dir + } + return "" +} + +// isUUID matches the 8-4-4-4-12 hex shape a session directory uses. +func isUUID(s string) bool { + if len(s) != 36 { + return false + } + for i, r := range s { + switch i { + case 8, 13, 18, 23: + if r != '-' { + return false + } + default: + isHex := (r >= '0' && r <= '9') || (r >= 'a' && r <= 'f') || (r >= 'A' && r <= 'F') + if !isHex { + return false + } + } + } + return true +} + +// dshTextFromBlocks joins the text blocks of a DSH content array. Reasoning +// and tool-call blocks are handled elsewhere and pass silently; any other +// block type (image, document, ...) warns once per type and is dropped. +func dshTextFromBlocks(content any, maxRunes int, warnings *[]string, warned map[string]bool, lineNum int) string { + switch x := content.(type) { + case string: + return truncateRunesCC(strings.TrimSpace(x), maxRunes) + case []any: + parts := make([]string, 0, len(x)) + for _, raw := range x { + if s, ok := raw.(string); ok { + parts = append(parts, s) + continue + } + block := objectMapCC(raw) + if block == nil { + continue + } + blockType := stringFieldCC(block, "type") + switch blockType { + case "text": + if text := stringFieldCC(block, "text"); text != "" { + parts = append(parts, text) + } + case "reasoning", "tool-call": + // Reasoning is model-internal; tool calls are imported as + // structured ToolCalls, not as text. + case "image": + // Pasted images are not uploaded; the surrounding text still + // carries the question. Silent, like the other importers. + default: + if blockType != "" && !warned["block:"+blockType] { + warned["block:"+blockType] = true + *warnings = append(*warnings, fmt.Sprintf("line %d: unsupported content block type %q, skipped", lineNum, blockType)) + } + } + } + return truncateRunesCC(strings.TrimSpace(strings.Join(parts, "\n")), maxRunes) + default: + return "" + } +} + +// dshToolCallsFromBlocks extracts the assistant message's tool-call blocks. +// Each block mirrors the standalone tool/call record: {id, name, arguments}. +func dshToolCallsFromBlocks(content any) []AgentMemoryToolCall { + blocks, ok := content.([]any) + if !ok { + return nil + } + var calls []AgentMemoryToolCall + for _, raw := range blocks { + block := objectMapCC(raw) + if block == nil || stringFieldCC(block, "type") != "tool-call" { + continue + } + callID := stringFieldCC(block, "id") + if callID == "" { + continue + } + calls = append(calls, AgentMemoryToolCall{ + ID: callID, + Type: "function", + Name: firstNonEmptyCC(stringFieldCC(block, "name"), "unknown"), + Arguments: Redact(argumentsStringCC(block["arguments"])), + }) + } + return calls +} diff --git a/cli/internal/importer/conversation/dsh_test.go b/cli/internal/importer/conversation/dsh_test.go new file mode 100644 index 0000000..6110ee0 --- /dev/null +++ b/cli/internal/importer/conversation/dsh_test.go @@ -0,0 +1,253 @@ +package conversation + +import ( + "bytes" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/klauspost/compress/zstd" +) + +// writeDSHSession writes one session log. compress selects the on-disk shape: +// DSH writes "session.v.jsonl.zstd", older releases wrote plain JSONL. +func writeDSHSession(t *testing.T, path string, records []string, compress bool) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + body := []byte(strings.Join(records, "\n") + "\n") + if !compress { + if err := os.WriteFile(path, body, 0o600); err != nil { + t.Fatal(err) + } + return + } + var buf bytes.Buffer + zw, err := zstd.NewWriter(&buf) + if err != nil { + t.Fatal(err) + } + if _, err := zw.Write(body); err != nil { + t.Fatal(err) + } + if err := zw.Close(); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, buf.Bytes(), 0o600); err != nil { + t.Fatal(err) + } +} + +func dshSampleSession() []string { + return []string{ + `{"type":"session","version":4,"id":"session-1111","createdAt":1794487123540,"cwd":"/home/u/proj","isSeeded":false}`, + `{"type":"turn/start","seq":1,"time":1794487124000,"data":{"turn":1}}`, + `{"type":"step/start","seq":2,"time":1794487124100,"data":{"turn":1,"step":1}}`, + `{"type":"user/message","seq":3,"time":1794487125000,"data":{"content":[{"type":"text","text":"why is the test red?"},{"type":"image","mediaType":"image/png"}],"role":"user","id":"m1"},"surfaceOp":"append"}`, + `{"type":"system/message","seq":4,"time":1794487125100,"data":{"turn":1,"step":1,"message":{"role":"system","content":[{"type":"text","text":"you are dsh"}],"id":"m2"}}}`, + `{"type":"assistant/message","seq":5,"time":1794487126000,"data":{"turn":1,"step":1,"message":{"role":"assistant","content":[{"type":"text","text":"Let me look at the log."},{"type":"tool-call","id":"call_1","name":"pwsh","arguments":"{\"command\":\"npm test\"}"}],"id":"m3"}}}`, + // Duplicate of the assistant message's tool-call block. + `{"type":"tool/call","seq":6,"time":1794487126100,"data":{"turn":1,"step":1,"callId":"call_1","name":"pwsh","arguments":"{\"command\":\"npm test\"}"}}`, + `{"type":"tool/result","seq":7,"time":1794487127000,"data":{"turn":1,"step":1,"message":{"role":"tool","toolCallId":"call_1","content":[{"type":"text","text":"1 failing"}],"isError":true,"id":"m4"}}}`, + `{"type":"step/end","seq":8,"time":1794487127100,"data":{"turn":1,"step":1}}`, + // Reasoning-only step: no user-visible text. + `{"type":"assistant/message","seq":9,"time":1794487128000,"data":{"turn":1,"step":2,"message":{"role":"assistant","content":[{"type":"reasoning","text":"internal musing"}],"id":"m5"}}}`, + `{"type":"session-log-deepseek/delivery-accepted","seq":10,"time":1794487129000,"data":{"sessionId":"session-1111","throughSeq":9}}`, + // Context compaction is bookkeeping; its summary is not a user turn. + `{"type":"compaction/summary","seq":12,"time":1794487131000,"data":{"summary":"earlier turns were compacted"}}`, + `{"type":"future/thing","seq":11,"time":1794487130000,"data":{}}`, + } +} + +func TestDSHScannerParsesSession(t *testing.T) { + home := t.TempDir() + path := filepath.Join(home, "sessions", "--proj--", "session-1111", "session.v4.jsonl.zstd") + writeDSHSession(t, path, dshSampleSession(), true) + t.Setenv("DSH_HOME", home) + + items, err := NewDSHScanner().Scan(DefaultRoots(PlatformDSH)) + if err != nil { + t.Fatal(err) + } + if len(items) != 1 { + t.Fatalf("expected one session, got %d: %v", len(items), items) + } + if items[0].OriginID != "session-1111" { + t.Fatalf("origin id must come from the session dir, got %q", items[0].OriginID) + } + + conv, err := NewDSHScanner().Read(items[0]) + if err != nil { + t.Fatal(err) + } + if conv.ID != "import-dsh-session-1111" { + t.Fatalf("conversation id must be stable and derived from the header id, got %q", conv.ID) + } + if len(conv.Messages) != 3 { + t.Fatalf("expected user + assistant + tool, got %d: %+v", len(conv.Messages), conv.Messages) + } + + user, asst, tool := conv.Messages[0], conv.Messages[1], conv.Messages[2] + if user.Role != "user" || !strings.Contains(user.Content.(string), "why is the test red?") { + t.Fatalf("unexpected user message: %+v", user) + } + if asst.Role != "assistant" || !strings.Contains(asst.Content.(string), "Let me look at the log.") { + t.Fatalf("unexpected assistant message: %+v", asst) + } + if len(asst.ToolCalls) != 1 { + t.Fatalf("assistant message must carry its tool-call block, got %+v", asst.ToolCalls) + } + call := asst.ToolCalls[0] + if call.ID != "call_1" || call.Name != "pwsh" || !strings.Contains(call.Arguments, "npm test") { + t.Fatalf("unexpected tool call: %+v", call) + } + if tool.Role != "tool" || tool.ToolCallID != "call_1" { + t.Fatalf("tool result must pair with the call id, got %+v", tool) + } + if !strings.HasPrefix(tool.Content.(string), "[error] ") { + t.Fatalf("isError result must be marked, got %q", tool.Content) + } + + for _, m := range conv.Messages { + text, _ := m.Content.(string) + if strings.Contains(text, "you are dsh") { + t.Fatal("system/message is instructions, not conversation — it must be skipped") + } + if strings.Contains(text, "internal musing") { + t.Fatal("reasoning blocks must be skipped") + } + if strings.Contains(text, "compacted") { + t.Fatal("compaction summaries are bookkeeping, not conversation") + } + } + + if conv.Item.StartedAt == "" { + t.Fatal("startedAt must be derived from the earliest message timestamp") + } + if !strings.HasPrefix(conv.Item.StartedAt, "2026-") { + t.Fatalf("unexpected startedAt %q", conv.Item.StartedAt) + } + + unknown := 0 + for _, w := range conv.Warnings { + if strings.Contains(w, "future/thing") { + unknown++ + } + if strings.Contains(w, "you are dsh") || strings.Contains(w, "internal musing") { + t.Fatalf("skipped-by-design content must not warn: %q", w) + } + } + if unknown != 1 { + t.Fatalf("expected exactly one warning about the unknown event type, got %v", conv.Warnings) + } +} + +func TestDSHScannerAcceptsUncompressedLog(t *testing.T) { + home := t.TempDir() + path := filepath.Join(home, "sessions", "--proj--", "session-2222", "session.v3.jsonl") + writeDSHSession(t, path, dshSampleSession(), false) + t.Setenv("DSH_HOME", home) + + items, err := NewDSHScanner().Scan(DefaultRoots(PlatformDSH)) + if err != nil { + t.Fatal(err) + } + if len(items) != 1 { + t.Fatalf("plain .jsonl sessions must be discovered too, got %d", len(items)) + } + conv, err := NewDSHScanner().Read(items[0]) + if err != nil { + t.Fatal(err) + } + if len(conv.Messages) != 3 { + t.Fatalf("expected the same three messages, got %d", len(conv.Messages)) + } +} + +func TestDSHScannerImportsStandaloneToolCall(t *testing.T) { + home := t.TempDir() + path := filepath.Join(home, "sessions", "--proj--", "session-3333", "session.v4.jsonl.zstd") + writeDSHSession(t, path, []string{ + `{"type":"session","version":4,"id":"session-3333","createdAt":1794487123540}`, + `{"type":"tool/call","seq":1,"time":1794487125000,"data":{"callId":"call_9","name":"read","arguments":"{\"path\":\"a.txt\"}"}}`, + `{"type":"tool/result","seq":2,"time":1794487126000,"data":{"message":{"toolCallId":"call_9","content":[{"type":"text","text":"ok"}]}}}`, + }, true) + t.Setenv("DSH_HOME", home) + + items, err := NewDSHScanner().Scan(DefaultRoots(PlatformDSH)) + if err != nil { + t.Fatal(err) + } + conv, err := NewDSHScanner().Read(items[0]) + if err != nil { + t.Fatal(err) + } + if len(conv.Messages) != 2 { + t.Fatalf("a call with no assistant message must still be imported, got %+v", conv.Messages) + } + if conv.Messages[0].Role != "assistant" || len(conv.Messages[0].ToolCalls) != 1 { + t.Fatalf("standalone call must become an assistant tool-call message, got %+v", conv.Messages[0]) + } + if conv.Messages[1].Role != "tool" || conv.Messages[1].ToolCallID != "call_9" { + t.Fatalf("tool result must pair with the standalone call, got %+v", conv.Messages[1]) + } +} + +func TestDSHScannerWarnsOnNewerFormatVersion(t *testing.T) { + home := t.TempDir() + path := filepath.Join(home, "sessions", "--proj--", "session-4444", "session.v9.jsonl.zstd") + writeDSHSession(t, path, []string{ + `{"type":"session","version":9,"id":"session-4444","createdAt":1794487123540}`, + `{"type":"user/message","seq":1,"time":1794487125000,"data":{"content":[{"type":"text","text":"hello"}],"role":"user"}}`, + }, true) + t.Setenv("DSH_HOME", home) + + items, err := NewDSHScanner().Scan(DefaultRoots(PlatformDSH)) + if err != nil { + t.Fatal(err) + } + conv, err := NewDSHScanner().Read(items[0]) + if err != nil { + t.Fatal(err) + } + warned := false + for _, w := range conv.Warnings { + if strings.Contains(w, "newer than the supported") { + warned = true + } + } + if !warned { + t.Fatalf("a newer session format must warn, got %v", conv.Warnings) + } + if len(conv.Messages) != 1 { + t.Fatalf("known records must still import on an unknown version, got %d", len(conv.Messages)) + } +} + +func TestDSHScanMissingRootIsNotAnError(t *testing.T) { + t.Setenv("DSH_HOME", filepath.Join(t.TempDir(), "absent")) + items, err := NewDSHScanner().Scan(DefaultRoots(PlatformDSH)) + if err != nil { + t.Fatalf("a machine without DSH must scan cleanly, got %v", err) + } + if len(items) != 0 { + t.Fatalf("expected no sessions, got %v", items) + } +} + +func TestDSHResolveEvtFromDotEnv(t *testing.T) { + home := t.TempDir() + t.Setenv("DSH_HOME", home) + if err := os.WriteFile(filepath.Join(home, ".env"), []byte("EVERME_API_BASE=https://api.example.com\nEVERME_AGENT_ID=agt_1\nEVERME_AGENT_TOKEN=evt_test_token\n"), 0o600); err != nil { + t.Fatal(err) + } + tok, err := ResolveEvt(PlatformDSH) + if err != nil { + t.Fatal(err) + } + if tok != "evt_test_token" { + t.Fatalf("unexpected token %q", tok) + } +} diff --git a/cli/internal/importer/conversation/evt.go b/cli/internal/importer/conversation/evt.go index 5cbcc1e..281d0a8 100644 --- a/cli/internal/importer/conversation/evt.go +++ b/cli/internal/importer/conversation/evt.go @@ -18,8 +18,9 @@ const openClawPluginID = "@everme/openclaw" // platformEnvFile maps a platform to its plugin config env file (where // plugin install wrote EVERME_AGENT_TOKEN). Honors per-tool home env first. -// Only Claude Code and Hermes write a dotenv-style everme.env; Codex and -// OpenClaw write structured config (see ResolveEvt). +// Claude Code, Hermes and Kimi Code write a dotenv-style everme.env; DeepSeek +// Harness writes a dotenv-style .env. Codex, OpenClaw, Raven and WorkBuddy +// write structured config (see ResolveEvt). func platformEnvFile(p PlatformID) (string, bool) { home, _ := os.UserHomeDir() switch p { @@ -31,6 +32,11 @@ func platformEnvFile(p PlatformID) (string, bool) { case PlatformKimicode: base := envOr("KIMI_CODE_HOME", filepath.Join(home, ".kimi-code")) return filepath.Join(base, "everme.env"), true + case PlatformDSH: + // `evercli plugin install dsh` writes all three EVERME_* values to + // /.env - keep in sync with cli/internal/plugin/dsh.go. + base := envOr("DSH_HOME", filepath.Join(home, ".dsh")) + return filepath.Join(base, ".env"), true default: // Codex / OpenClaw resolve from structured config; markdown has no // platform agent. Callers handle these in ResolveEvt. diff --git a/cli/internal/importer/conversation/platforms.go b/cli/internal/importer/conversation/platforms.go index 4b38427..38feccf 100644 --- a/cli/internal/importer/conversation/platforms.go +++ b/cli/internal/importer/conversation/platforms.go @@ -18,6 +18,7 @@ var knownPlatforms = map[PlatformID]struct{}{ PlatformKimicode: {}, PlatformRaven: {}, PlatformWorkBuddy: {}, + PlatformDSH: {}, } // IsKnownPlatform reports whether p is one of the supported platform names. @@ -34,7 +35,7 @@ func ParsePlatforms(names []string) ([]PlatformID, error) { for _, n := range names { p := PlatformID(strings.TrimSpace(n)) if !IsKnownPlatform(p) { - return nil, fmt.Errorf("unknown platform %q (known: claude-code, codex, hermes, openclaw, markdown, kimicode, raven, workbuddy)", n) + return nil, fmt.Errorf("unknown platform %q (known: claude-code, codex, hermes, openclaw, markdown, kimicode, raven, workbuddy, dsh)", n) } ids = append(ids, p) } diff --git a/cli/internal/importer/conversation/registry.go b/cli/internal/importer/conversation/registry.go index 34ccf59..0e188f5 100644 --- a/cli/internal/importer/conversation/registry.go +++ b/cli/internal/importer/conversation/registry.go @@ -22,6 +22,7 @@ func DefaultRegistry() *Registry { NewKimicodeScanner(), NewRavenScanner(), NewWorkBuddyScanner(), + NewDSHScanner(), }, } } @@ -75,6 +76,11 @@ func DefaultRoots(p PlatformID) []string { // env var name, so the two doors into ~/.workbuddy agree. base := envOr("EVERCLI_WORKBUDDY_CONFIG_DIR", filepath.Join(home, ".workbuddy")) return []string{filepath.Join(base, "projects")} + case PlatformDSH: + // Mirrors cli/internal/plugin/dsh.go dshHomePath - same env var name, + // so the installer and the importer agree on where the harness lives. + base := envOr("DSH_HOME", filepath.Join(home, ".dsh")) + return []string{filepath.Join(base, "sessions")} case PlatformMarkdown: // A markdown file's owning agent is the agent whose local folder // contains it; scan the agent home dirs (not generic doc dirs) so md diff --git a/cli/internal/importer/conversation/types.go b/cli/internal/importer/conversation/types.go index 0b452f7..0d8b82e 100644 --- a/cli/internal/importer/conversation/types.go +++ b/cli/internal/importer/conversation/types.go @@ -13,6 +13,7 @@ const ( PlatformKimicode PlatformID = "kimicode" PlatformRaven PlatformID = "raven" PlatformWorkBuddy PlatformID = "workbuddy" + PlatformDSH PlatformID = "dsh" ) // AgentMemoryToolCall mirrors the BFF /mem/agent-memory camelCase DTO.