Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions internal/llmloop/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,33 +448,47 @@ func (r *Runner) runGraceRound(ctx context.Context, messages []llm.Message, newP
return
}

resp, err := r.deps.LLMClient.CompletionsWithCtx(ctx, llm.ChatRequest{
fs := r.deps.Session.GetOrCreateFileSession(newPath)
rec := fs.AppendTaskRecord(session.MainTask, messages)
startTime := time.Now()
reqCtx := r.requestCtx(ctx, newPath, session.MainTask, rec.RequestNo)

_, llmSpan := telemetry.StartLLMSpan(ctx, r.deps.Model)
resp, err := r.deps.LLMClient.CompletionsWithCtx(reqCtx, llm.ChatRequest{
Model: r.deps.Model,
Messages: messages,
Tools: graceDefs,
MaxTokens: r.deps.Template.CompletionTokenLimit(),
SessionID: sessionID,
})
duration := time.Since(startTime)
if err != nil {
rec.SetError(err, duration)
telemetry.RecordLLMResult(llmSpan, duration, 0, err)
llmSpan.End()
telemetry.RecordLLMRequest(ctx, r.deps.Model, duration, 0, "error")
fmt.Fprintf(stdout.Writer(), "[ocr] Grace round LLM error for %s: %v\n", newPath, err)
return
}

rec.SetResponse(resp, duration)
totalTokens := int64(0)
if resp.Usage != nil {
totalTokens = resp.Usage.TotalTokens
atomic.AddInt64(&r.totalInputTokens, resp.Usage.PromptTokens)
atomic.AddInt64(&r.totalOutputTokens, resp.Usage.CompletionTokens)
atomic.AddInt64(&r.totalCacheReadTokens, resp.Usage.CacheReadTokens)
atomic.AddInt64(&r.totalCacheWriteTokens, resp.Usage.CacheWriteTokens)
}
telemetry.RecordLLMResult(llmSpan, duration, totalTokens, nil)
llmSpan.End()
telemetry.RecordLLMRequest(ctx, r.deps.Model, duration, totalTokens, "ok")

calls := resp.ToolCalls()
if len(calls) == 0 {
return
}

fs := r.deps.Session.GetOrCreateFileSession(newPath)
rec := fs.AppendTaskRecord(session.MainTask, append([]llm.Message(nil), messages...))
rec.SetResponse(resp, 0)
thinking := resp.ReasoningContent()
for _, call := range calls {
r.executeToolCall(ctx, newPath, call, rec, thinking)
Expand Down
98 changes: 98 additions & 0 deletions internal/llmloop/retry_identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ package llmloop

import (
"context"
"errors"
"strings"
"sync"
"testing"
"time"

"github.com/alibaba/open-code-review/internal/config/template"
"github.com/alibaba/open-code-review/internal/llm"
Expand All @@ -32,6 +34,8 @@ type metaCaptureClient struct {
// respond is called with the zero-based call index so a test can drive a
// multi-round tool loop.
respond func(n int) *llm.ChatResponse
// respondErr drives request failures and takes precedence over respond.
respondErr func(n int) (*llm.ChatResponse, error)
}

func (c *metaCaptureClient) CompletionsWithCtx(ctx context.Context, _ llm.ChatRequest) (*llm.ChatResponse, error) {
Expand All @@ -40,6 +44,9 @@ func (c *metaCaptureClient) CompletionsWithCtx(ctx context.Context, _ llm.ChatRe
n := len(c.captured)
c.captured = append(c.captured, capturedRequest{meta: meta, ok: ok})
c.mu.Unlock()
if c.respondErr != nil {
return c.respondErr(n)
}
if c.respond == nil {
return emptyResponse(), nil
}
Expand Down Expand Up @@ -145,6 +152,97 @@ func TestRunPerFile_MainTaskIdentity(t *testing.T) {
}
}

// TestRunPerFile_GraceRoundIsAMainTaskRound verifies that the grace round uses
// the next main_task identity and records both responses and errors.
func TestRunPerFile_GraceRoundIsAMainTaskRound(t *testing.T) {
// Delay the grace response so duration assertions are stable across platforms.
const graceDelay = 20 * time.Millisecond

cases := []struct {
name string
client *metaCaptureClient
want func(t *testing.T, grace *session.TaskRecord)
}{
{
name: "response recorded",
client: &metaCaptureClient{respond: func(n int) *llm.ChatResponse {
if n == 0 {
return fileReadToolCallResponse("call_1", `{"path":"main.go"}`)
}
time.Sleep(graceDelay)
return emptyResponse()
}},
want: func(t *testing.T, grace *session.TaskRecord) {
if grace.Response == nil {
t.Error("grace round response was not recorded")
}
},
},
{
name: "suppressed error recorded",
client: &metaCaptureClient{respondErr: func(n int) (*llm.ChatResponse, error) {
if n == 0 {
return fileReadToolCallResponse("call_1", `{"path":"main.go"}`), nil
}
time.Sleep(graceDelay)
return nil, errors.New("grace request failed")
}},
want: func(t *testing.T, grace *session.TaskRecord) {
if !strings.Contains(grace.Error, "grace request failed") {
t.Errorf("grace round error = %q, want the request error", grace.Error)
}
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
deps := newTestDeps(tc.client)
deps.Template.MaxToolRequestTimes = 1
deps.MainToolDefs = []llm.ToolDef{
{Type: "function", Function: llm.FunctionDef{Name: "code_comment"}},
{Type: "function", Function: llm.FunctionDef{Name: "task_done"}},
{Type: "function", Function: llm.FunctionDef{Name: "file_read"}},
}
deps.NewRequestMeta = metaFactory("openai", deps.Model)

completed, stop, err := NewRunner(deps).RunPerFile(
context.Background(),
[]llm.Message{llm.NewTextMessage("user", "review this file")},
"main.go",
)
if err != nil {
t.Fatalf("RunPerFile: %v", err)
}
if completed || stop != StopMaxRounds {
t.Fatalf("completed = %v, stop = %v; want false, StopMaxRounds", completed, stop)
}

reqs := tc.client.requests()
if len(reqs) != 2 {
t.Fatalf("got %d requests, want 2", len(reqs))
}
wantMeta(t, reqs[1], llm.RequestMeta{
Provider: "openai",
Model: "fake",
FilePath: "main.go",
TaskType: string(session.MainTask),
RequestNo: 2,
})

recs := deps.Session.GetOrCreateFileSession("main.go").TaskRecords[session.MainTask]
if len(recs) != 2 {
t.Fatalf("session holds %d main_task records, want 2", len(recs))
}
grace := recs[1]
if grace.Duration < graceDelay/2 {
t.Errorf("grace round duration = %v, want at least %v", grace.Duration, graceDelay/2)
}
tc.want(t, grace)
})
}
}

// TestRunPerFile_NoIdentityWhenFactoryNil is the scan guarantee: the Runner is
// shared, and with NewRequestMeta left nil no request may carry identity.
func TestRunPerFile_NoIdentityWhenFactoryNil(t *testing.T) {
Expand Down
Loading