Files
goclaw/internal/store/tracing_store.go
T
Viet Tran 9a9744077e refactor(teams): v2 system cleanup — remove legacy tools, fix followup, add events API (#210)
Major refactoring of the team system with multiple improvements:

## Removed legacy delegation tools
- Delete `delegate.go`, `delegate_async.go`, `delegate_sync.go`, `delegate_events.go`,
  `delegate_policy.go`, `delegate_prep.go`, `delegate_state.go`, `delegate_search_tool.go`
- Delete `evaluate_loop_tool.go`, `handoff_tool.go`
- Remove all references and registrations from tool manager and policy
- Clean up TEAM_PLAYBOOK_IDEAS.md and TEAM_SYSTEM.md (moved to docs)

## Rename await_reply → ask_user
- Rename action `await_reply` → `ask_user`, `clear_followup` → `clear_ask_user`
- Rename functions `executeAwaitReply` → `executeAskUser`, `executeClearFollowup` → `executeClearAskUser`
- Update system prompt with stronger wording to prevent model misuse
- Model was confusing "await_reply" with general waiting; "ask_user" is unambiguous

## Fix auto-followup false positives
- Add `HasActiveMemberTasks(ctx, teamID, excludeAgentID)` store method
- Guard `autoSetFollowup()` in consumer: skip when lead has active member tasks
- Prevents auto-followup when lead is orchestrating teammates (not waiting for user)

## Task identifier zero-padding
- Change format from `T-1-xxxx` → `T-001-xxxx` (3-digit minimum)

## Refactor workspace WS handlers to filesystem-only
- Rewrite `teams.workspace.list/read/delete` to use pure filesystem (os.ReadDir/ReadFile/Remove)
- Remove DB dependency from workspace WS handlers
- Consistent with storage handler and workspace tools
- Simplify TeamWorkspaceFile type and frontend hook

## Add team events listing API
- New WS method `teams.events.list` with team_id, limit, offset params
- New HTTP endpoint `GET /v1/teams/{id}/events` with bearer auth
- New `ListTeamEvents(ctx, teamID, limit, offset)` store method
- JOIN with team_tasks for team-wide event filtering

## Extract team access policy
- New `team_access_policy.go` — centralized team tool access control

## Migration 000019: team_id columns
- Add team_id foreign key columns to relevant tables

## Other improvements
- Add team_id propagation through agent loop, tracing, sessions
- Update i18n locale files (en/vi/zh) for new tool labels
- Update frontend builtin-tools page and require-setup component
- Bump RequiredSchemaVersion for migration 000019
2026-03-15 14:53:19 +07:00

147 lines
5.6 KiB
Go

package store
import (
"context"
"encoding/json"
"time"
"github.com/google/uuid"
)
// Trace status constants.
const (
TraceStatusRunning = "running"
TraceStatusCompleted = "completed"
TraceStatusError = "error"
TraceStatusCancelled = "cancelled"
)
// Span type constants.
const (
SpanTypeLLMCall = "llm_call"
SpanTypeToolCall = "tool_call"
SpanTypeAgent = "agent"
SpanTypeEmbedding = "embedding"
SpanTypeEvent = "event"
)
// Span status constants.
const (
SpanStatusCompleted = "completed"
SpanStatusError = "error"
SpanStatusRunning = "running"
)
// Span level constants.
const (
SpanLevelDefault = "DEFAULT"
)
// TraceData represents a top-level trace (one per user request).
type TraceData struct {
ID uuid.UUID `json:"id"`
ParentTraceID *uuid.UUID `json:"parent_trace_id,omitempty"` // linked parent trace (delegation)
AgentID *uuid.UUID `json:"agent_id,omitempty"`
UserID string `json:"user_id,omitempty"`
SessionKey string `json:"session_key,omitempty"`
RunID string `json:"run_id,omitempty"`
StartTime time.Time `json:"start_time"`
EndTime *time.Time `json:"end_time,omitempty"`
DurationMS int `json:"duration_ms,omitempty"`
Name string `json:"name,omitempty"`
Channel string `json:"channel,omitempty"`
InputPreview string `json:"input_preview,omitempty"`
OutputPreview string `json:"output_preview,omitempty"`
TotalInputTokens int `json:"total_input_tokens"`
TotalOutputTokens int `json:"total_output_tokens"`
TotalCost float64 `json:"total_cost"`
SpanCount int `json:"span_count"`
LLMCallCount int `json:"llm_call_count"`
ToolCallCount int `json:"tool_call_count"`
Status string `json:"status"`
Error string `json:"error,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
Tags []string `json:"tags,omitempty"`
TeamID *uuid.UUID `json:"team_id,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// SpanData represents a single operation within a trace.
type SpanData struct {
ID uuid.UUID `json:"id"`
TraceID uuid.UUID `json:"trace_id"`
ParentSpanID *uuid.UUID `json:"parent_span_id,omitempty"`
AgentID *uuid.UUID `json:"agent_id,omitempty"`
SpanType string `json:"span_type"` // "llm_call", "tool_call", "agent", "embedding", "event"
Name string `json:"name,omitempty"`
StartTime time.Time `json:"start_time"`
EndTime *time.Time `json:"end_time,omitempty"`
DurationMS int `json:"duration_ms,omitempty"`
Status string `json:"status"`
Error string `json:"error,omitempty"`
Level string `json:"level,omitempty"`
Model string `json:"model,omitempty"`
Provider string `json:"provider,omitempty"`
InputTokens int `json:"input_tokens,omitempty"`
OutputTokens int `json:"output_tokens,omitempty"`
TotalCost *float64 `json:"total_cost,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
ModelParams json.RawMessage `json:"model_params,omitempty"`
ToolName string `json:"tool_name,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
InputPreview string `json:"input_preview,omitempty"`
OutputPreview string `json:"output_preview,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
TeamID *uuid.UUID `json:"team_id,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// TraceListOpts configures trace listing.
type TraceListOpts struct {
AgentID *uuid.UUID
UserID string
SessionKey string
Status string
Channel string
Limit int
Offset int
}
// CostSummaryOpts configures cost aggregation queries.
type CostSummaryOpts struct {
AgentID *uuid.UUID
From *time.Time
To *time.Time
}
// CostSummaryRow is a single row of aggregated cost data.
type CostSummaryRow struct {
AgentID *uuid.UUID `json:"agent_id,omitempty"`
TotalCost float64 `json:"total_cost"`
TotalInputTokens int `json:"total_input_tokens"`
TotalOutputTokens int `json:"total_output_tokens"`
TraceCount int `json:"trace_count"`
}
// TracingStore manages LLM traces and spans.
type TracingStore interface {
CreateTrace(ctx context.Context, trace *TraceData) error
UpdateTrace(ctx context.Context, traceID uuid.UUID, updates map[string]any) error
GetTrace(ctx context.Context, traceID uuid.UUID) (*TraceData, error)
ListTraces(ctx context.Context, opts TraceListOpts) ([]TraceData, error)
CountTraces(ctx context.Context, opts TraceListOpts) (int, error)
CreateSpan(ctx context.Context, span *SpanData) error
UpdateSpan(ctx context.Context, spanID uuid.UUID, updates map[string]any) error
GetTraceSpans(ctx context.Context, traceID uuid.UUID) ([]SpanData, error)
ListChildTraces(ctx context.Context, parentTraceID uuid.UUID) ([]TraceData, error)
// Batch operations (async flush)
BatchCreateSpans(ctx context.Context, spans []SpanData) error
BatchUpdateTraceAggregates(ctx context.Context, traceID uuid.UUID) error
// Cost aggregation
GetMonthlyAgentCost(ctx context.Context, agentID uuid.UUID, year int, month time.Month) (float64, error)
GetCostSummary(ctx context.Context, opts CostSummaryOpts) ([]CostSummaryRow, error)
}