Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名) - 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块) - docreader proto 描述符采用等长替换,保持序列化字节布局有效 - Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像 - 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
137 lines
4.9 KiB
Go
137 lines
4.9 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"academy/internal/agent/tools"
|
|
"academy/internal/event"
|
|
"academy/internal/types"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type orderedTestTool struct {
|
|
tools.BaseTool
|
|
run func(context.Context) *types.ToolResult
|
|
}
|
|
|
|
func (t *orderedTestTool) Execute(ctx context.Context, _ json.RawMessage) (*types.ToolResult, error) {
|
|
return t.run(ctx), nil
|
|
}
|
|
|
|
func TestParallelReadsRespectMutationBarriers(t *testing.T) {
|
|
engine := newTestEngine(t, &mockChat{})
|
|
engine.toolRegistry = tools.NewToolRegistry()
|
|
var mu sync.Mutex
|
|
var order []string
|
|
readStarts := make(chan struct{}, 2)
|
|
allowReads := make(chan struct{})
|
|
register := func(name string, run func(context.Context)) {
|
|
engine.toolRegistry.RegisterTool(&orderedTestTool{
|
|
BaseTool: tools.NewBaseTool(name, "", json.RawMessage(`{"type":"object"}`)),
|
|
run: func(ctx context.Context) *types.ToolResult {
|
|
run(ctx)
|
|
mu.Lock()
|
|
order = append(order, name)
|
|
mu.Unlock()
|
|
return &types.ToolResult{Success: true}
|
|
},
|
|
})
|
|
}
|
|
for _, name := range []string{tools.ToolSearchKnowledge, tools.ToolReadDocument} {
|
|
register(name, func(ctx context.Context) {
|
|
readStarts <- struct{}{}
|
|
select {
|
|
case <-allowReads:
|
|
case <-ctx.Done():
|
|
}
|
|
})
|
|
}
|
|
register(tools.ToolWriteSandboxFile, func(context.Context) {})
|
|
register(tools.ToolShellExec, func(context.Context) {})
|
|
register(tools.ToolReadFile, func(context.Context) {})
|
|
register("mcp_unknown_mutation", func(context.Context) {})
|
|
names := []string{
|
|
tools.ToolSearchKnowledge, tools.ToolReadDocument, tools.ToolWriteSandboxFile,
|
|
tools.ToolShellExec, tools.ToolReadFile, "mcp_unknown_mutation",
|
|
}
|
|
response := &types.ChatResponse{}
|
|
for _, name := range names {
|
|
response.ToolCalls = append(response.ToolCalls, types.LLMToolCall{ID: name, Function: types.FunctionCall{Name: name, Arguments: `{}`}})
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
step := &types.AgentStep{}
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
engine.executeToolCallsParallel(ctx, response, step, 0, "session", "message")
|
|
}()
|
|
for i := 0; i < 2; i++ {
|
|
select {
|
|
case <-readStarts:
|
|
case <-ctx.Done():
|
|
t.Fatal("independent reads did not run concurrently")
|
|
}
|
|
}
|
|
close(allowReads)
|
|
select {
|
|
case <-done:
|
|
case <-ctx.Done():
|
|
t.Fatal("execution did not finish")
|
|
}
|
|
require.ElementsMatch(t, names[:2], order[:2])
|
|
require.Equal(t, names[2:], order[2:], "write, execute, read and unknown mutation must retain order")
|
|
for i, call := range step.ToolCalls {
|
|
require.Equal(t, names[i], call.Name, "result events retain original order")
|
|
}
|
|
}
|
|
|
|
func TestParallelToolPanicFailsOnlyThatCall(t *testing.T) {
|
|
engine := newTestEngine(t, &mockChat{})
|
|
engine.toolRegistry = tools.NewToolRegistry()
|
|
engine.toolRegistry.RegisterTool(&orderedTestTool{
|
|
BaseTool: tools.NewBaseTool(tools.ToolSearchKnowledge, "", json.RawMessage(`{"type":"object"}`)),
|
|
run: func(context.Context) *types.ToolResult { panic("tool bug") },
|
|
})
|
|
engine.toolRegistry.RegisterTool(&orderedTestTool{
|
|
BaseTool: tools.NewBaseTool(tools.ToolReadDocument, "", json.RawMessage(`{"type":"object"}`)),
|
|
run: func(context.Context) *types.ToolResult { return &types.ToolResult{Success: true, Output: "ok"} },
|
|
})
|
|
response := &types.ChatResponse{ToolCalls: []types.LLMToolCall{
|
|
{ID: "a", Function: types.FunctionCall{Name: tools.ToolSearchKnowledge, Arguments: `{}`}},
|
|
{ID: "b", Function: types.FunctionCall{Name: tools.ToolReadDocument, Arguments: `{}`}},
|
|
}}
|
|
step := &types.AgentStep{}
|
|
|
|
engine.executeToolCallsParallel(context.Background(), response, step, 0, "session", "message")
|
|
|
|
require.Len(t, step.ToolCalls, 2)
|
|
require.False(t, step.ToolCalls[0].Result.Success)
|
|
require.Contains(t, step.ToolCalls[0].Result.Error, "internal error")
|
|
require.True(t, step.ToolCalls[1].Result.Success)
|
|
}
|
|
|
|
func TestToolOutcomeKeepsLiveDataChangeOutOfModelAndHistory(t *testing.T) {
|
|
engine := newTestEngine(t, &mockChat{})
|
|
marker := map[string]any{"operation": "agent.delete", "area": "agent"}
|
|
call := types.ToolCall{Name: tools.ToolRunOperation, Result: &types.ToolResult{
|
|
Success: true, Output: "done", Data: map[string]any{"data_change": marker},
|
|
}}
|
|
var outcomes []event.AgentToolResultData
|
|
engine.eventBus.On(event.EventAgentToolResult, func(_ context.Context, evt event.Event) error {
|
|
outcomes = append(outcomes, evt.Data.(event.AgentToolResultData))
|
|
return nil
|
|
})
|
|
engine.emitToolOutcome(context.Background(), call, 0, "session")
|
|
require.Len(t, outcomes, 1)
|
|
require.Equal(t, marker, outcomes[0].Data["data_change"])
|
|
messages := engine.appendToolResults(nil, types.AgentStep{ToolCalls: []types.ToolCall{call}})
|
|
require.Equal(t, "done", messages[len(messages)-1].Content)
|
|
stored := tools.SanitizeAgentStepsForStorage([]types.AgentStep{{ToolCalls: []types.ToolCall{call}}})
|
|
require.NotContains(t, stored[0].ToolCalls[0].Result.Data, "data_change")
|
|
}
|