WeKnora/internal/application/service/tenant_skill_transcript_tes...

268 lines
9.3 KiB
Go
Raw Normal View History

feat(sandbox): expose tenant skill management over HTTP and in the console (#2789) * feat(sandbox): enhance skill management in sandbox configurations Updated the skill management system to allow listing, installing, and removing skills within sandbox configurations. The ListSkills function now accepts a sandboxConfigID to filter skills based on the selected configuration. Introduced a new SandboxSkillsDrawer component for managing skills through the UI, along with necessary updates to the API and localization files to support these features. Co-authored-by: Cursor <cursoragent@cursor.com> * fix: keep skill maintenance sessions out of the console session list Co-authored-by: Cursor <cursoragent@cursor.com> * feat: store and expose the installer transcript locators Co-authored-by: Cursor <cursoragent@cursor.com> * feat: record the skill installer agent's conversation Co-authored-by: Cursor <cursoragent@cursor.com> * feat: stream and persist the installer transcript during a skill install Co-authored-by: Cursor <cursoragent@cursor.com> * feat: expose install transcript locators on the skill type Co-authored-by: Cursor <cursoragent@cursor.com> * feat: add the floating skill install transcript window Co-authored-by: Cursor <cursoragent@cursor.com> * feat: open the install transcript from the sandbox skills drawer Co-authored-by: Cursor <cursoragent@cursor.com> * feat(sandbox): 重构技能安装体验,支持内联时间线、编辑第4步及保留镜像指针 - 替换右下角浮窗为内联安装时间线 (SkillInstallTimeline.vue) - 新增 GET /api/v1/sandbox-configs/:id/skills/:skillId/transcript 接口,支持回放与追尾 - 移除 /sessions/continue-stream 依赖,避免 404 问题 - 事件日志以 install_prompt 开头,不再将工具调用 preamble 拼入最终答案 - 删除 SkillInstallTranscript.vue,技能条目下可展开/折叠时间线 - 编辑向导新增第 4 步「管理技能」 - Cube/E2B 环境:连接 → 模板 → 运行配置 → 技能(第4步) - Docker/Local 环境:无第4步(无快照镜像) - 首次创建必须按 1→2→3 顺序,第3步保存成功后自动进入第4步 - 已有配置支持步骤条数字跳转,可直接访问任意步骤 - 卡片菜单「管理技能」打开同一抽屉并定位至第4步 - SandboxSkillsDrawer.vue 重命名为 SandboxSkillsPanel.vue,去除独立抽屉壳 - 修复保存运行配置时冲掉 skill_image 的问题 - 顶部展示信息(基础模板/快照ID/版本/构建时间)读取 config.skill_image - 编辑器保存 payload 不再包含 skill_image,合并时保留已有 SkillImage 和 VolumeMount - MergeSandboxConfigForUpdate 增加对 SkillImage 和 VolumeMount 的保护 - 已冲掉的配置需重新安装技能才能恢复镜像指针 - 「查看安装过程」在安装开始时即显示 - 技能列表中出现「正在安装」状态即展示按钮 - 若记录尚未写入,展示等待提示并自动重试,避免误报「没有留下记录」 * 沙盒内技能目录从 UUID 改成 SKILL.md 的 name。 * fix(sandbox): heal stuck skill rows and block planted maintenance sessions The reaper left a mid-upgrade install spinning forever when the old snapshot was still live, and restored removals to ready after the pointer had already moved. Client session create/update can no longer hide chats behind the skill_maintenance: prefix. * style: gofumpt the skill reaper test stubs * style: satisfy golangci on the tenant skill admin API * feat(sandbox): rebuild skill images at turn boundaries, not mid-exec Hold a session turn lease through AgentQA so an install cannot tear down the live sandbox between tool calls; the next turn's first resolve picks up the new image. Configs can instead keep existing sandboxes and boot the new snapshot only for new sessions. * fix(sandbox): judge stuck skill runs from the snapshot chain InstalledSnapshotID only records the generation that installed a skill; later installs of other skills advance the live pointer without rewriting older rows. Comparing those IDs made the reaper fail healthy installs and delete live skills whose removal had stalled. --------- Co-authored-by: hailongzhao <hailongzhao@tencent.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-08-25 03:28:12 +00:00
package service
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/Tencent/WeKnora/internal/event"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
// transcriptStreams records what the installer projects onto the replayable
// stream, in order, so a test can state the shape the console will read.
type transcriptStreams struct {
events []interfaces.StreamEvent
err error
}
func (s *transcriptStreams) AppendEvent(
_ context.Context, _, _ string, evt interfaces.StreamEvent,
) error {
s.events = append(s.events, evt)
return s.err
}
func (s *transcriptStreams) GetEvents(
_ context.Context, _, _ string, from int,
) ([]interfaces.StreamEvent, int, error) {
return s.events[from:], len(s.events), nil
}
func (s *transcriptStreams) types() []types.ResponseType {
out := make([]types.ResponseType, 0, len(s.events))
for _, evt := range s.events {
out = append(out, evt.Type)
}
return out
}
// transcriptMessages is the durable half. Only the two calls the transcript
// makes are implemented; the rest panic so an accidental new dependency on the
// repository surfaces as a test failure rather than a silent nil write.
type transcriptMessages struct {
interfaces.MessageRepository
created []*types.Message
updated []*types.Message
err error
}
func (m *transcriptMessages) CreateMessage(
_ context.Context, msg *types.Message,
) (*types.Message, error) {
m.created = append(m.created, msg)
return msg, m.err
}
func (m *transcriptMessages) UpdateMessage(_ context.Context, msg *types.Message) error {
m.updated = append(m.updated, msg)
return m.err
}
func newTranscriptForTest(t *testing.T) (
*installTranscript, *event.EventBus, *transcriptStreams, *transcriptMessages,
) {
t.Helper()
bus := event.NewEventBus()
streams := &transcriptStreams{}
messages := &transcriptMessages{}
tr := newInstallTranscript(context.Background(), bus, streams, messages, "sess-1", "msg-1")
tr.Subscribe()
return tr, bus, streams, messages
}
func TestInstallTranscriptProjectsTheAgentsWork(t *testing.T) {
_, bus, streams, _ := newTranscriptForTest(t)
ctx := context.Background()
require.NoError(t, bus.Emit(ctx, event.Event{
ID: "t-1", Type: event.EventAgentThought,
Data: event.AgentThoughtData{Content: "check for uv", Done: false},
}))
require.NoError(t, bus.Emit(ctx, event.Event{
ID: "c-1", Type: event.EventAgentToolCall,
Data: event.AgentToolCallData{
ToolName: "shell_exec", ToolCallID: "call-1",
Arguments: map[string]any{"command": "uv --version"},
},
}))
require.NoError(t, bus.Emit(ctx, event.Event{
ID: "r-1", Type: event.EventAgentToolResult,
Data: event.AgentToolResultData{
ToolName: "shell_exec", ToolCallID: "call-1", Success: true, Output: "uv 0.4.0",
},
}))
require.Equal(t, []types.ResponseType{
types.ResponseTypeThinking,
types.ResponseTypeToolCall,
types.ResponseTypeToolResult,
}, streams.types())
call := streams.events[1]
require.Equal(t, "shell_exec", call.Data["tool_name"])
require.Equal(t, "call-1", call.Data["tool_call_id"])
}
// A failed command is the single most useful thing in an install transcript,
// so it must reach the stream as an error the console already knows how to
// render, not be dropped for having Success=false.
func TestInstallTranscriptReportsAFailedCommandAsAnError(t *testing.T) {
_, bus, streams, _ := newTranscriptForTest(t)
require.NoError(t, bus.Emit(context.Background(), event.Event{
ID: "r-1", Type: event.EventAgentToolResult,
Data: event.AgentToolResultData{
ToolName: "shell_exec", ToolCallID: "call-1",
Success: false, Error: "exit status 1",
},
}))
require.Equal(t, []types.ResponseType{types.ResponseTypeError}, streams.types())
require.Equal(t, "exit status 1", streams.events[0].Content)
require.Equal(t, false, streams.events[0].Data["success"])
}
func TestInstallTranscriptPersistsTheAnswerAndStepsOnComplete(t *testing.T) {
tr, bus, streams, messages := newTranscriptForTest(t)
ctx := context.Background()
require.NoError(t, bus.Emit(ctx, event.Event{
ID: "a-1", Type: event.EventAgentFinalAnswer,
Data: event.AgentFinalAnswerData{Content: "installed ", Done: false},
}))
require.NoError(t, bus.Emit(ctx, event.Event{
ID: "a-1", Type: event.EventAgentFinalAnswer,
Data: event.AgentFinalAnswerData{Content: "pdf-tools", Done: true},
}))
require.NoError(t, bus.Emit(ctx, event.Event{
ID: "done", Type: event.EventAgentComplete,
Data: event.AgentCompleteData{
MessageID: "msg-1", TotalDurationMs: 4200, TotalSteps: 3,
},
}))
tr.Finish(ctx, nil)
require.Equal(t, types.ResponseTypeComplete, streams.types()[len(streams.types())-1])
require.Len(t, messages.updated, 1)
saved := messages.updated[0]
require.Equal(t, "installed pdf-tools", saved.Content)
require.True(t, saved.IsCompleted)
require.EqualValues(t, 4200, saved.AgentDurationMs)
}
// The engine routes a round's plain assistant prose through
// EventAgentFinalAnswer, so every "now I'll check X" preamble arrives as an
// answer chunk. Only the prose of the round that ends the run is the answer.
// Without retracting the preambles the persisted record is every round's
// commentary glued end to end, which is unreadable — and is what the console
// renders as the install's result.
func TestInstallTranscriptRetractsPreamblesSupersededByAToolCall(t *testing.T) {
tr, bus, _, messages := newTranscriptForTest(t)
ctx := context.Background()
emitAnswer := func(id, content string, done bool) {
require.NoError(t, bus.Emit(ctx, event.Event{
ID: id, Type: event.EventAgentFinalAnswer,
Data: event.AgentFinalAnswerData{Content: content, Done: done},
}))
}
emitAnswer("a-1", "The script needs duckduckgo-search, so I'll create the venv:", true)
require.NoError(t, bus.Emit(ctx, event.Event{
ID: "c-1", Type: event.EventAgentToolCall,
Data: event.AgentToolCallData{
ToolName: "shell_exec", ToolCallID: "call-1",
Arguments: map[string]any{"command": "uv venv .venv"},
},
}))
emitAnswer("a-2", "Installed successfully. Now verifying:", true)
require.NoError(t, bus.Emit(ctx, event.Event{
ID: "c-2", Type: event.EventAgentToolCall,
Data: event.AgentToolCallData{
ToolName: "shell_exec", ToolCallID: "call-2",
Arguments: map[string]any{"command": "python scripts/search.py --help"},
},
}))
emitAnswer("a-3", "Installed duckduckgo-search into the skill venv.", true)
require.NoError(t, bus.Emit(ctx, event.Event{
ID: "done", Type: event.EventAgentComplete,
Data: event.AgentCompleteData{MessageID: "msg-1", TotalDurationMs: 900},
}))
tr.Finish(ctx, nil)
require.Len(t, messages.updated, 1)
require.Equal(t,
"Installed duckduckgo-search into the skill venv.",
messages.updated[0].Content,
"only the final round's prose is the answer; earlier preambles were retracted by their tool calls",
)
}
// An install that dies before the engine emits complete is exactly the run
// someone will come looking for, so Finish must close the record itself.
func TestInstallTranscriptRecordsAFailedRun(t *testing.T) {
tr, _, streams, messages := newTranscriptForTest(t)
tr.Finish(context.Background(), errors.New("installer agent failed: boom"))
require.Equal(t, []types.ResponseType{
types.ResponseTypeError,
types.ResponseTypeComplete,
}, streams.types())
require.Len(t, messages.updated, 1)
require.Contains(t, messages.updated[0].Content, "boom")
require.True(t, messages.updated[0].IsCompleted)
}
// The transcript is a troubleshooting aid. A broken Redis or a failing write
// must not be able to fail an otherwise good install.
func TestInstallTranscriptSwallowsItsOwnFailures(t *testing.T) {
bus := event.NewEventBus()
streams := &transcriptStreams{err: errors.New("redis down")}
messages := &transcriptMessages{err: errors.New("db down")}
tr := newInstallTranscript(context.Background(), bus, streams, messages, "sess-1", "msg-1")
tr.Subscribe()
require.NoError(t, bus.Emit(context.Background(), event.Event{
ID: "t-1", Type: event.EventAgentThought,
Data: event.AgentThoughtData{Content: "hi", Done: true},
}))
require.NotPanics(t, func() { tr.Finish(context.Background(), nil) })
}
func TestInstallTranscriptCreateSeedsTheAssistantRow(t *testing.T) {
streams := &transcriptStreams{}
messages := &transcriptMessages{}
tr := newInstallTranscript(
context.Background(), event.NewEventBus(), streams, messages, "sess-1", "msg-1",
)
require.NoError(t, tr.Create(context.Background(), "install pdf-tools"))
require.Len(t, messages.created, 2)
require.Equal(t, "user", messages.created[0].Role)
require.Equal(t, "install pdf-tools", messages.created[0].Content)
require.Equal(t, "assistant", messages.created[1].Role)
require.Equal(t, "msg-1", messages.created[1].ID)
require.Equal(t, "sess-1", messages.created[1].SessionID)
}
// A console that attaches to a running install reads the event log and nothing
// else, so the prompt has to be in the log or the run shows up as an agent
// answering a question nobody can see.
func TestInstallTranscriptCreateOpensTheLogWithThePrompt(t *testing.T) {
streams := &transcriptStreams{}
tr := newInstallTranscript(
context.Background(), event.NewEventBus(), streams, &transcriptMessages{}, "sess-1", "msg-1",
)
require.NoError(t, tr.Create(context.Background(), "install pdf-tools"))
require.Equal(t, []types.ResponseType{types.ResponseTypeInstallPrompt}, streams.types())
require.Equal(t, "install pdf-tools", streams.events[0].Content)
}