Files
runny 9058149576
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
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

172 lines
6.1 KiB
Go

package agent
import (
"context"
"strings"
"testing"
"time"
agenttools "academy/internal/agent/tools"
"academy/internal/background"
"academy/internal/event"
"academy/internal/models/chat"
"academy/internal/types"
"github.com/stretchr/testify/require"
)
// fakeBackgroundRun hands out queued deliveries one call at a time.
type fakeBackgroundRun struct {
roundStart []background.Delivery
beforeFinal []background.Delivery
seen [][]string
}
func (f *fakeBackgroundRun) RoundStart(context.Context) (background.Delivery, error) {
if len(f.roundStart) == 0 {
return background.Delivery{}, nil
}
d := f.roundStart[0]
f.roundStart = f.roundStart[1:]
return d, nil
}
func (f *fakeBackgroundRun) BeforeFinal(context.Context) (background.Delivery, error) {
if len(f.beforeFinal) == 0 {
return background.Delivery{}, nil
}
d := f.beforeFinal[0]
f.beforeFinal = f.beforeFinal[1:]
return d, nil
}
func (f *fakeBackgroundRun) TakeSeen() []string {
if len(f.seen) == 0 {
return nil
}
s := f.seen[0]
f.seen = f.seen[1:]
return s
}
func answer(content string) mockResponse {
return mockResponse{chunks: []types.StreamResponse{
{ResponseType: types.ResponseTypeAnswer, Content: content, Done: true, FinishReason: "stop"},
}}
}
// userMessageContaining returns the first user message sent to the model that
// contains needle. Per-call envelopes may follow it, so position is not fixed.
func userMessageContaining(messages []chat.Message, needle string) string {
for _, m := range messages {
if m.Role == "user" && strings.Contains(m.Content, needle) {
return m.Content
}
}
return ""
}
func TestBackgroundNoticeIsInjectedBeforeTheRoundAndRecordedOnItsStep(t *testing.T) {
model := &mockChat{responses: []mockResponse{answer("done")}}
engine := newTestEngine(t, model)
engine.SetBackgroundRun(&fakeBackgroundRun{roundStart: []background.Delivery{
{Text: "build (sj_1) succeeded.", AckIDs: []string{"sj_1"}},
}})
state := &types.AgentState{}
_, err := engine.executeLoop(context.Background(), state, "q", emptyMessages(), nil, "sess", "msg")
require.NoError(t, err)
want := "<background_notices>\nbuild (sj_1) succeeded.\n</background_notices>"
require.Contains(t, userMessageContaining(model.calls[0], "<background_notices>"), want)
require.Len(t, state.RoundSteps, 1)
require.Equal(t, []types.BackgroundInjection{{Content: want, AckIDs: []string{"sj_1"}}},
state.RoundSteps[0].BackgroundInjections)
require.Empty(t, state.PendingBackground)
}
func TestBackgroundReminderKeepsTheLoopGoing(t *testing.T) {
model := &mockChat{responses: []mockResponse{answer("first"), answer("second")}}
engine := newTestEngine(t, model)
engine.SetBackgroundRun(&fakeBackgroundRun{beforeFinal: []background.Delivery{
{Text: "Background work you started in this turn is still running: build (sj_1)."},
}})
state := &types.AgentState{}
_, err := engine.executeLoop(context.Background(), state, "q", emptyMessages(), nil, "sess", "msg")
require.NoError(t, err)
require.Len(t, model.calls, 2)
require.NotEmpty(t, userMessageContaining(model.calls[1], "build (sj_1)"))
require.Empty(t, userMessageContaining(model.calls[0], "build (sj_1)"), "the reminder comes only before finishing")
require.Equal(t, "second", state.FinalAnswer)
require.Len(t, state.RoundSteps, 2)
require.True(t, state.RoundSteps[0].IntermediateAnswer)
require.Len(t, state.RoundSteps[1].BackgroundInjections, 1)
}
func TestBackgroundSeenIDsRideOnTheToolStep(t *testing.T) {
model := &mockChat{responses: []mockResponse{
{chunks: []types.StreamResponse{{
ResponseType: types.ResponseTypeAnswer,
ToolCalls: []types.LLMToolCall{{
ID: "call-1", Type: "function",
Function: types.FunctionCall{Name: "probe", Arguments: "{}"},
}},
Done: true, FinishReason: "tool_calls",
}}},
answer("done"),
}}
engine := newTestEngine(t, model)
engine.toolRegistry = agenttools.NewToolRegistry()
engine.toolRegistry.RegisterTool(newCountingTool("probe"))
engine.SetBackgroundRun(&fakeBackgroundRun{seen: [][]string{{"sj_9"}}})
state := &types.AgentState{}
_, err := engine.executeLoop(context.Background(), state, "q", emptyMessages(), nil, "sess", "msg")
require.NoError(t, err)
require.Len(t, state.RoundSteps, 2)
require.Equal(t, []string{"sj_9"}, state.RoundSteps[0].BackgroundSeenIDs)
require.Empty(t, state.RoundSteps[1].BackgroundSeenIDs)
}
func TestCompletionKeepsUndeliveredBackgroundOnATrailingStep(t *testing.T) {
engine := newTestEngine(t, &mockChat{})
engine.eventBus = event.NewEventBus()
engine.SetBackgroundRun(&fakeBackgroundRun{seen: [][]string{{"sj_2"}}})
var steps []types.AgentStep
engine.eventBus.On(event.EventAgentComplete, func(_ context.Context, evt event.Event) error {
steps, _ = evt.Data.(event.AgentCompleteData).AgentSteps.([]types.AgentStep)
return nil
})
injection := types.BackgroundInjection{
Content: "<background_notices>\nx\n</background_notices>",
AckIDs: []string{"sj_1"},
}
state := &types.AgentState{PendingBackground: []types.BackgroundInjection{injection}}
engine.emitCompletionEvent(context.Background(), state, "sess", "msg", time.Now())
require.Len(t, steps, 1)
require.Equal(t, []types.BackgroundInjection{injection}, steps[0].BackgroundInjections)
require.Equal(t, []string{"sj_2"}, steps[0].BackgroundSeenIDs)
}
func TestHasPendingInputPeeksAtTheSteerQueue(t *testing.T) {
engine := newTestEngine(t, &mockChat{})
require.False(t, engine.HasPendingInput(context.Background(), "sess", "msg"), "no sink, nothing pending")
sink := &fakeSteerSink{queued: []map[string]interface{}{steerEntry("s1", " ")}}
engine.SetSteerSink(sink)
require.False(t, engine.HasPendingInput(context.Background(), "sess", "msg"), "blank messages are dropped by drain")
sink.queued = append(sink.queued, steerEntry("s2", "stop waiting"))
require.True(t, engine.HasPendingInput(context.Background(), "sess", "msg"))
require.Empty(t, sink.persisted, "peeking must not consume the message")
}
func TestBackgroundNoticesContentWrapsTrimmedText(t *testing.T) {
require.Equal(t, "<background_notices>\na\nb\n</background_notices>", backgroundNoticesContent(" a\nb \n"))
require.False(t, strings.Contains(backgroundNoticesContent("x"), "\n\n"))
}