mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
15a70ac45c
* feat(seahorse): implement short-term memory engine of seahorse Add pkg/seahorse/ module implementing a SQLite-backed DAG-based summary hierarchy for context management, ported from lossless-claw's LCM design: - types.go + short_constants.go: core types (Message, Summary, Conversation, ContextItem) and configuration constants (fanout, token targets, thresholds) - migration.go: idempotent DB schema with FTS5 trigram tokenizer for CJK - store.go: full SQLite CRUD (conversations, messages, summaries DAG, context_items with ordinal gap numbering, FTS5 search) - short_engine.go: Engine lifecycle (NewEngine, Ingest, Assemble, Compact), session pattern filtering (ignore/stateless glob→regex compilation), per-session mutex via sync.Map - short_assembler.go: budget-aware context assembly with fresh tail protection (32 messages), oldest-first eviction, summary XML formatting, RebuildContextItems - short_compaction.go: leaf compaction (messages→summary) and condensed compaction (summaries→higher-level summary), 3-level LLM escalation, CompactUntilUnder for emergency overflow - short_retrieval.go: lookupByID, FTS5/LIKE search, recursive expand with token cap - context_seahorse.go: agent.ContextManager adapter, registered as "seahorse", provider↔seahorse message type conversion (ToolCalls, tool_result) * fix(seahorse): correct 3 adapter bugs in context management - TokenCount: use full message (Content+ToolCalls+Media) instead of Content-only - Empty Content: rebuild Content from tool_result Parts when stored empty - Duplicate summaries: summaries only in Summary field, not in History messages - Grep: fix SearchResult.Snippet→Content for summaries - Schema: fix FTS5 SQL uses VIRTUAL TABLE not TEMP TABLE - TestFTS5SQLConstants: verify FTS5 SQL syntax correctness - Test: fix flaky TestCompactLeaf * fix(agent): ingest steering messages into seahorse SQLite Steering messages were only persisted to session JSONL but not ingested into seahorse SQLite, causing them to be missing from context assembly. Added `ts.ingestMessage(turnCtx, al, pm)` call in the steering message injection block alongside the existing JSONL persistence. Test: TestSeahorseSteeringMessageIngested verifies steering messages appear in seahorse SQLite DB after being processed. * fix(seahorse): address 3 blocking bugs from code review - Fix resequenceContextItemsTx scan error handling (store.go:850) Changed `return err` to `return scanErr` to properly propagate scan errors instead of returning nil (which silently corrupts data) - Fix sql.NullString for INTEGER column (store.go:847) Changed `mid` from sql.NullString to sql.NullInt64 since message_id is INTEGER in schema. Removed unnecessary strconv.ParseInt call. - Fix compactCondensed fallback deleting non-candidate items Added ReplaceContextItemsWithSummary method for per-item deletion when candidates are not contiguous in ordinal space. Optimized to use range deletion when candidates are consecutive. * fix(seahorse): pass Budget to Compact for correct condensed threshold Issue #4 from PR review: When Budget was not passed to seahorse.Compact, it defaulted to `tokensBefore * 0.75`, making `tokensBefore > budget` always true and causing condensed compaction to trigger unnecessarily. Changes: - context_seahorse.go: Forward Budget from CompactRequest to CompactInput - loop.go: Pass Budget (ContextWindow) in all 3 Compact calls - Add test verifying condensed is skipped when tokens < threshold - Fix lint issues in store.go and store_test.go * fix(seahorse): add mutex for assembler lazy initialization Issue #5 from PR review: The check-then-create pattern for e.assembler was a data race when multiple goroutines called Assemble() concurrently: if e.assembler == nil { e.assembler = &Assembler{...} } Changes: - Add assemblerMu sync.Mutex to Engine struct - Add initAssemblerOnce() using double-checked locking (same pattern as initCompactionOnce) - Add TestAssemblerLazyInitRace to verify thread-safety * fix(seahorse): handle non-consecutive depths in selectShallowestCondensationCandidate Issue #8 from PR review: the loop iterated depth 0, 1, 2... assuming consecutive keys, but break when key was missing caused deeper depths to never be checked. Fix: collect all existing depth keys, sort, then iterate in order. * fix(seahorse): wrap DeleteMessagesAfterID and appendContextItems in transactions - DeleteMessagesAfterID: wrap all DELETE operations in a transaction for atomicity, remove redundant manual FTS delete (handled by trigger) - appendContextItems: use transaction to fix read-then-write race condition - Add GetMaxOrdinalTx and resolveItemTokenCountTx for transaction-scoped queries - Remove unused resolveItemTokenCount function Fixes PR review issues 6 and 7. * fix(seahorse): derive readable content from Parts and cap CompactUntilUnder iterations - Derive readable content from MessageParts in AddMessageWithParts so FTS5 indexing and summary formatting can access tool call information - formatMessagesForSummary and truncateSummary now fall back to Parts when Content is empty, fixing blank summaries for Part-based messages - Add MaxCompactIterations (20) to prevent CompactUntilUnder infinite loops; exceeded iterations are logged as warnings
173 lines
5.3 KiB
Go
173 lines
5.3 KiB
Go
package seahorse
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/tools"
|
|
)
|
|
|
|
// GrepTool searches summaries and messages for matching content.
|
|
type GrepTool struct {
|
|
engine *RetrievalEngine
|
|
}
|
|
|
|
func NewGrepTool(engine *RetrievalEngine) *GrepTool {
|
|
return &GrepTool{engine: engine}
|
|
}
|
|
|
|
func (t *GrepTool) Name() string {
|
|
return "short_grep"
|
|
}
|
|
|
|
func (t *GrepTool) Description() string {
|
|
return `Search summaries and messages for matching content.
|
|
|
|
Pattern syntax:
|
|
- Words: "authentication" - matches content containing this word
|
|
- AND: "auth AND login" - matches content with both words
|
|
- OR: "auth OR signin" - matches content with either word
|
|
- NOT: "bug NOT fixed" - matches "bug" but excludes "fixed"
|
|
- Wildcard: "%auth%" - matches any text containing "auth" (e.g., "auth", "authentication")
|
|
|
|
Each summary has a "depth" field:
|
|
- depth 0: Created from messages, most detailed
|
|
- depth 1+: Created from other summaries, more compressed but covers longer time
|
|
|
|
Parameters:
|
|
- pattern (required): Search pattern
|
|
- scope: "both" (default), "summary", or "message" - what to search
|
|
- role: "user", "assistant", or omit for all - filter by message role
|
|
- last: Time shortcut like "6h", "7d", "2w", "1m" (hours/days/weeks/months)
|
|
- all_conversations: Search all conversations (default: current only)
|
|
- since: ISO8601 timestamp, content after this time
|
|
- before: ISO8601 timestamp, content before this time
|
|
- limit: Max results (default: 20)
|
|
|
|
Returns:
|
|
{
|
|
"success": true,
|
|
"summaries": [{"id": "sum_abc", "content": "...", "depth": 0, "kind": "leaf", "conversationId": 1, "rank": -0.5}],
|
|
"messages": [{"id": "10", "snippet": "...matched...", "role": "user", "conversationId": 1, "rank": -1.2}],
|
|
"totalSummaries": 5,
|
|
"totalMessages": 10,
|
|
"hint": "No matches. Try: %keyword% for fuzzy search"
|
|
}
|
|
|
|
Rank field (FTS5 mode only): bm25 relevance score, negative value where closer to 0 = better match.
|
|
Examples: -0.5=excellent, -2=good, -5=partial, -10=weak. LIKE mode (%pattern%) has no rank.
|
|
|
|
Examples:
|
|
{"pattern": "authentication"}
|
|
{"pattern": "bug AND login"}
|
|
{"pattern": "%snake%"}
|
|
{"pattern": "project", "scope": "summary"}
|
|
{"pattern": "error", "role": "assistant", "last": "7d"}
|
|
{"pattern": "error", "all_conversations": true}`
|
|
}
|
|
|
|
func (t *GrepTool) Parameters() map[string]any {
|
|
return map[string]any{
|
|
"type": "object",
|
|
"properties": map[string]any{
|
|
"pattern": map[string]any{
|
|
"type": "string",
|
|
"description": "Search pattern. Supports: words, AND/OR/NOT operators, % wildcard",
|
|
},
|
|
"scope": map[string]any{
|
|
"type": "string",
|
|
"enum": []string{"both", "summary", "message"},
|
|
"description": "What to search: 'both' (default), 'summary', or 'message'",
|
|
},
|
|
"role": map[string]any{
|
|
"type": "string",
|
|
"enum": []string{"user", "assistant"},
|
|
"description": "Filter by message role (default: all roles)",
|
|
},
|
|
"last": map[string]any{
|
|
"type": "string",
|
|
"description": "Time shortcut: '6h' (6 hours), '7d' (7 days), '2w' (2 weeks), '1m' (1 month)",
|
|
},
|
|
"all_conversations": map[string]any{
|
|
"type": "boolean",
|
|
"description": "Search across all conversations (default: searches current conversation only)",
|
|
},
|
|
"since": map[string]any{
|
|
"type": "string",
|
|
"description": "ISO8601 timestamp, only return content after this time",
|
|
},
|
|
"before": map[string]any{
|
|
"type": "string",
|
|
"description": "ISO8601 timestamp, only return content before this time",
|
|
},
|
|
"limit": map[string]any{
|
|
"type": "integer",
|
|
"description": "Maximum number of results (default: 20)",
|
|
},
|
|
},
|
|
"required": []string{"pattern"},
|
|
}
|
|
}
|
|
|
|
func (t *GrepTool) Execute(ctx context.Context, args map[string]any) *tools.ToolResult {
|
|
pattern, ok := args["pattern"].(string)
|
|
if !ok || pattern == "" {
|
|
return tools.ErrorResult("Missing required 'pattern' argument. Example: {\"pattern\": \"authentication\"}")
|
|
}
|
|
|
|
input := GrepInput{Pattern: pattern}
|
|
|
|
if scope, ok := args["scope"].(string); ok && scope != "" {
|
|
input.Scope = scope
|
|
}
|
|
if role, ok := args["role"].(string); ok && role != "" {
|
|
input.Role = role
|
|
}
|
|
if last, ok := args["last"].(string); ok && last != "" {
|
|
input.Last = last
|
|
}
|
|
if allConv, ok := args["all_conversations"].(bool); ok {
|
|
input.AllConversations = allConv
|
|
}
|
|
if limit, ok := args["limit"].(float64); ok {
|
|
input.Limit = int(limit)
|
|
}
|
|
if sinceStr, ok := args["since"].(string); ok && sinceStr != "" {
|
|
parsed, err := time.Parse(time.RFC3339, sinceStr)
|
|
if err != nil {
|
|
return tools.ErrorResult(fmt.Sprintf(
|
|
"Invalid 'since' timestamp. Use RFC3339 format like '2024-01-15T10:00:00Z'. Error: %v", err))
|
|
}
|
|
input.Since = &parsed
|
|
}
|
|
if beforeStr, ok := args["before"].(string); ok && beforeStr != "" {
|
|
parsed, err := time.Parse(time.RFC3339, beforeStr)
|
|
if err != nil {
|
|
return tools.ErrorResult(fmt.Sprintf("Invalid 'before' timestamp format: %v", err))
|
|
}
|
|
input.Before = &parsed
|
|
}
|
|
|
|
result, err := t.engine.Grep(ctx, input)
|
|
if err != nil {
|
|
return tools.ErrorResult("Grep failed: " + err.Error())
|
|
}
|
|
|
|
// Build response
|
|
output := map[string]any{
|
|
"success": result.Success,
|
|
"summaries": result.Summaries,
|
|
"messages": result.Messages,
|
|
}
|
|
|
|
// Add hint if provided
|
|
if result.Hint != "" {
|
|
output["hint"] = result.Hint
|
|
}
|
|
|
|
data, _ := json.Marshal(output)
|
|
return tools.NewToolResult(string(data))
|
|
}
|