chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,156 @@
|
||||
package compaction
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"academy/internal/models/chat"
|
||||
"academy/internal/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// pacedChat streams a summary as chunks spaced by gap, optionally stopping
|
||||
// after a number of chunks without closing the stream: a provider that stalls.
|
||||
type pacedChat struct {
|
||||
stubChat
|
||||
chunks []types.StreamResponse
|
||||
gap time.Duration
|
||||
stallFrom int // stop sending after this many chunks; 0 sends all
|
||||
opened atomic.Int32
|
||||
}
|
||||
|
||||
func (p *pacedChat) ChatStream(
|
||||
ctx context.Context, _ []chat.Message, _ *chat.ChatOptions,
|
||||
) (<-chan types.StreamResponse, error) {
|
||||
p.opened.Add(1)
|
||||
ch := make(chan types.StreamResponse)
|
||||
go func() {
|
||||
defer close(ch)
|
||||
for i, chunk := range p.chunks {
|
||||
if p.stallFrom > 0 && i >= p.stallFrom {
|
||||
<-ctx.Done() // stalled until cancelled
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-time.After(p.gap):
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
select {
|
||||
case ch <- chunk:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
// closedStreamChat hands back a stream that is already closed: a provider that
|
||||
// gives up the instant the call is cancelled. Closing before ChatStream returns
|
||||
// makes the closed-stream arm and the ctx.Done() arm of the select ready at the
|
||||
// same moment, so the outcome cannot depend on which one select happens to pick.
|
||||
type closedStreamChat struct {
|
||||
stubChat
|
||||
}
|
||||
|
||||
func (c *closedStreamChat) ChatStream(
|
||||
context.Context, []chat.Message, *chat.ChatOptions,
|
||||
) (<-chan types.StreamResponse, error) {
|
||||
ch := make(chan types.StreamResponse)
|
||||
close(ch)
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
func pacedSettings() Settings {
|
||||
s := testSettings()
|
||||
s.StallTimeout = 80 * time.Millisecond
|
||||
return s
|
||||
}
|
||||
|
||||
func answerChunks(n int) []types.StreamResponse {
|
||||
chunks := []types.StreamResponse{{ResponseType: types.ResponseTypeThinking, Content: "considering"}}
|
||||
for range n {
|
||||
chunks = append(chunks, types.StreamResponse{ResponseType: types.ResponseTypeAnswer, Content: "## Goal "})
|
||||
}
|
||||
return append(chunks,
|
||||
types.StreamResponse{ResponseType: types.ResponseTypeAnswer, Done: true, FinishReason: "stop"})
|
||||
}
|
||||
|
||||
// A summarization that keeps producing output is left to finish, however long
|
||||
// it takes in total: the old 60-second total budget cut exactly the large
|
||||
// requests compaction exists for. Reasoning counts as progress but is not
|
||||
// part of the summary.
|
||||
func TestSummarizationRunsAsLongAsItProgresses(t *testing.T) {
|
||||
chatModel := &pacedChat{chunks: answerChunks(10), gap: 30 * time.Millisecond}
|
||||
c := New(chatModel, newEstimator(t), pacedSettings())
|
||||
|
||||
start := time.Now()
|
||||
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Greater(t, time.Since(start), 4*80*time.Millisecond, "longer in total than the stall timeout")
|
||||
require.False(t, result.Degraded)
|
||||
assert.Contains(t, result.Summary, "## Goal")
|
||||
assert.NotContains(t, result.Summary, "considering")
|
||||
}
|
||||
|
||||
// A stream that stops producing output is cancelled after the stall timeout,
|
||||
// retried once, and then degrades to the archive rather than hanging the turn.
|
||||
func TestSummarizationIsCancelledWhenItStalls(t *testing.T) {
|
||||
chatModel := &pacedChat{chunks: answerChunks(10), gap: 10 * time.Millisecond, stallFrom: 2}
|
||||
c := New(chatModel, newEstimator(t), pacedSettings())
|
||||
|
||||
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
|
||||
require.NoError(t, err)
|
||||
require.True(t, result.Degraded)
|
||||
assert.Contains(t, result.Summary, "Raw conversation archive")
|
||||
assert.EqualValues(t, maxSummarizationAttempts, chatModel.opened.Load(),
|
||||
"a single live turn has only its prefix to summarize, attempted twice")
|
||||
}
|
||||
|
||||
// A stream that closes because the call was cancelled is a cancelled call, not
|
||||
// an empty successful summary. Both the ctx.Done() arm and the closed-stream arm
|
||||
// are ready at once here, so this pins down the answer instead of letting select
|
||||
// choose it at random.
|
||||
func TestSummarizationIsCancelledWhenTheStreamCloses(t *testing.T) {
|
||||
c := New(&closedStreamChat{}, newEstimator(t), pacedSettings())
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
_, _, err := c.streamSummary(ctx, nil, &chat.ChatOptions{})
|
||||
require.True(t, errors.Is(err, context.Canceled), "got %v", err)
|
||||
}
|
||||
|
||||
// An error reported inside the stream is a failed attempt, not a summary.
|
||||
func TestSummarizationStreamErrorIsAFailure(t *testing.T) {
|
||||
chatModel := &pacedChat{chunks: []types.StreamResponse{
|
||||
{ResponseType: types.ResponseTypeAnswer, Content: "## Goal half"},
|
||||
{ResponseType: types.ResponseTypeError, Content: "upstream reset", Done: true},
|
||||
}}
|
||||
c := New(chatModel, newEstimator(t), pacedSettings())
|
||||
|
||||
result, err := c.Compact(context.Background(), reactTurn(12), ReasonThreshold)
|
||||
require.NoError(t, err)
|
||||
require.True(t, result.Degraded)
|
||||
assert.NotContains(t, result.Summary, "## Goal half")
|
||||
}
|
||||
|
||||
// A stopped turn is not retried: another attempt cannot outlive the turn.
|
||||
func TestSummarizationIsNotRetriedOnceTheTurnIsCancelled(t *testing.T) {
|
||||
chatModel := &pacedChat{chunks: answerChunks(10), gap: 10 * time.Millisecond, stallFrom: 1}
|
||||
c := New(chatModel, newEstimator(t), pacedSettings())
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
_, _, err := c.streamSummary(ctx, nil, &chat.ChatOptions{})
|
||||
require.True(t, errors.Is(err, context.Canceled))
|
||||
|
||||
_, err = c.summarize(ctx, reactTurn(2), 0, "", initialSummarizationInstructions, 1024)
|
||||
require.Error(t, err)
|
||||
assert.EqualValues(t, 2, chatModel.opened.Load(), "one open for streamSummary, one for summarize")
|
||||
}
|
||||
Reference in new issue
Block a user