Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
【架构优化】从 githubPRFiles 中拆出单文件 diff 截断与总预算分配 githubPRFilePatch (#629)
fx-kurumic1741901eb03
diff --git a/internal/httpapi/agent_tool_github_pr.go b/internal/httpapi/agent_tool_github_pr.goindex f38f3cf3..8e938801 100644--- a/internal/httpapi/agent_tool_github_pr.go+++ b/internal/httpapi/agent_tool_github_pr.go@@ -162,16 +162,7 @@ func githubPRFiles(ctx context.Context, token, path string, args map[string]any)break}f := fileOut{Filename: it.Filename, Status: it.Status, Additions: it.Additions, Deletions: it.Deletions}- patch := it.Patch- if len([]rune(patch)) > githubPRFilePatchRunes {- patch = string([]rune(patch)[:githubPRFilePatchRunes]) + "\n…(该文件 diff 过长已截断)"- }- if n := len([]rune(patch)); n > 0 && n <= budget {- f.Patch = patch- budget -= n- } else if n > 0 {- f.PatchOmitted = true- }+ f.Patch, f.PatchOmitted, budget = githubPRFilePatch(it.Patch, budget)out = append(out, f)}if hasMore || len(items) < githubListPageSize {@@ -185,6 +176,21 @@ func githubPRFiles(ctx context.Context, token, path string, args map[string]any)return string(body), nil}+// githubPRFilePatch 先把单个文件的 diff 截到 githubPRFilePatchRunes 以内,+// 再按剩余的总预算决定是否保留:放得下就保留并扣减预算,放不下就标记省略,+// 空 diff 既不保留也不标记。返回保留的 diff、是否省略和扣减后的预算。+func githubPRFilePatch(patch string, budget int) (kept string, omitted bool, remaining int) {+ if len([]rune(patch)) > githubPRFilePatchRunes {+ patch = string([]rune(patch)[:githubPRFilePatchRunes]) + "\n…(该文件 diff 过长已截断)"+ }+ if n := len([]rune(patch)); n > 0 && n <= budget {+ return patch, false, budget - n+ } else if n > 0 {+ return "", true, budget+ }+ return "", false, budget+}+func githubPRReview(ctx context.Context, token, path string, args map[string]any) (string, error) {number, err := githubArgNumber(args)if err != nil {diff --git a/internal/httpapi/agent_tool_github_pr_patch_budget_test.go b/internal/httpapi/agent_tool_github_pr_patch_budget_test.gonew file mode 100644index 00000000..7c307b4a--- /dev/null+++ b/internal/httpapi/agent_tool_github_pr_patch_budget_test.go@@ -0,0 +1,68 @@+package httpapi++import (+ "encoding/json"+ "fmt"+ "net/http"+ "strings"+ "testing"+)++// A patch of exactly githubPRFilePatchRunes runes is within the per-file limit+// and must be kept verbatim, without the truncation notice.+func TestGitHubPRFilePatchKeepsPatchAtRuneLimit(t *testing.T) {+ exact := strings.Repeat("改", githubPRFilePatchRunes)+ kept, omitted, remaining := githubPRFilePatch(exact, githubPRFilesPatchBudget)+ if kept != exact || omitted || remaining != githubPRFilesPatchBudget-githubPRFilePatchRunes {+ t.Fatalf("patch at limit: kept %d runes (truncated=%t) omitted=%t remaining=%d", len([]rune(kept)), strings.Contains(kept, "已截断"), omitted, remaining)+ }+}++// githubPRFiles must thread the budget returned by githubPRFilePatch through+// every file: once full-size patches use it up, the next one is omitted+// without being charged, and a later small patch still fits the remainder.+func TestGitHubPRFilesCarriesPatchBudgetAcrossFiles(t *testing.T) {+ f := newGitHubToolFixture(t)+ f.set(githubToolTestToken)+ full := strings.Repeat("x", githubPRFilePatchRunes)+ fitting := githubPRFilesPatchBudget / githubPRFilePatchRunes+ small := strings.Repeat("y", githubPRFilesPatchBudget-fitting*githubPRFilePatchRunes)+ items := make([]string, 0, fitting+2)+ for i := 0; i <= fitting; i++ {+ items = append(items, fmt.Sprintf(`{"filename":"f%02d.go","status":"modified","patch":%q}`, i, full))+ }+ items = append(items, fmt.Sprintf(`{"filename":"small.go","status":"modified","patch":%q}`, small))+ api := &fakeGitHubAPI{routes: map[string]func(http.ResponseWriter, *http.Request){+ "GET /repos/octo/app/pulls/7/files": githubJSON(http.StatusOK, "["+strings.Join(items, ",")+"]"),+ }}+ api.serve(t)+ text, isErr := callGitHubTool(t, f, mainBotGitHubPRFiles, `{"repository":"app","number":7}`)+ if isErr {+ t.Fatalf("pr files error: %.400s", text)+ }+ var got struct {+ Files []struct {+ Filename string `json:"filename"`+ Patch string `json:"patch"`+ PatchOmitted bool `json:"patchOmitted"`+ } `json:"files"`+ Count int `json:"count"`+ }+ if err := json.Unmarshal([]byte(text), &got); err != nil {+ t.Fatalf("decode %.400s: %v", text, err)+ }+ if got.Count != fitting+2 || len(got.Files) != fitting+2 {+ t.Fatalf("count = %d files = %d, want %d", got.Count, len(got.Files), fitting+2)+ }+ for i := 0; i < fitting; i++ {+ if file := got.Files[i]; file.Patch != full || file.PatchOmitted {+ t.Fatalf("%s: kept %d runes omitted=%t, want full patch", file.Filename, len(file.Patch), file.PatchOmitted)+ }+ }+ if over := got.Files[fitting]; over.Patch != "" || !over.PatchOmitted {+ t.Fatalf("%s over budget: kept %d runes omitted=%t, want omitted", over.Filename, len(over.Patch), over.PatchOmitted)+ }+ if last := got.Files[fitting+1]; last.Filename != "small.go" || last.Patch != small || last.PatchOmitted {+ t.Fatalf("%s: kept %d runes omitted=%t, want remaining budget used", last.Filename, len(last.Patch), last.PatchOmitted)+ }+}diff --git a/internal/httpapi/agent_tool_github_pr_patch_test.go b/internal/httpapi/agent_tool_github_pr_patch_test.gonew file mode 100644index 00000000..17d8f77b--- /dev/null+++ b/internal/httpapi/agent_tool_github_pr_patch_test.go@@ -0,0 +1,50 @@+package httpapi++import (+ "strings"+ "testing"+ "unicode/utf8"+)++func TestGitHubPRFilePatchKeepsShortPatchAndChargesBudget(t *testing.T) {+ kept, omitted, remaining := githubPRFilePatch("@@ -1 +1 @@\n+改动", 100)+ if kept != "@@ -1 +1 @@\n+改动" || omitted || remaining != 100-utf8.RuneCountInString(kept) {+ t.Fatalf("short patch = %q omitted=%t remaining=%d", kept, omitted, remaining)+ }+}++func TestGitHubPRFilePatchTruncatesLongPatchByRunes(t *testing.T) {+ long := strings.Repeat("改", githubPRFilePatchRunes+10)+ kept, omitted, remaining := githubPRFilePatch(long, githubPRFilesPatchBudget)+ want := strings.Repeat("改", githubPRFilePatchRunes) + "\n…(该文件 diff 过长已截断)"+ if kept != want || omitted {+ t.Fatalf("long patch kept %d runes omitted=%t", utf8.RuneCountInString(kept), omitted)+ }+ if remaining != githubPRFilesPatchBudget-utf8.RuneCountInString(want) {+ t.Fatalf("remaining = %d", remaining)+ }+}++func TestGitHubPRFilePatchBudgetBoundary(t *testing.T) {+ for _, tc := range []struct {+ name string+ patch string+ budget int+ wantKept string+ wantOmitted bool+ wantRemaining int+ }{+ {name: "exactly fits", patch: "abcd", budget: 4, wantKept: "abcd", wantRemaining: 0},+ {name: "over budget", patch: "abcde", budget: 4, wantOmitted: true, wantRemaining: 4},+ {name: "budget exhausted", patch: "a", budget: 0, wantOmitted: true, wantRemaining: 0},+ {name: "empty patch", patch: "", budget: 0, wantRemaining: 0},+ {name: "empty patch keeps budget", patch: "", budget: 7, wantRemaining: 7},+ } {+ t.Run(tc.name, func(t *testing.T) {+ kept, omitted, remaining := githubPRFilePatch(tc.patch, tc.budget)+ if kept != tc.wantKept || omitted != tc.wantOmitted || remaining != tc.wantRemaining {+ t.Fatalf("githubPRFilePatch(%q, %d) = %q, %t, %d", tc.patch, tc.budget, kept, omitted, remaining)+ }+ })+ }+}