Skip to content

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.

【架构优化】合并 workspace_preparation 中重复的 UTF-8 安全尾部截断为 tailAtRuneBoundary (#575)

fx-kurumi7b3bc01592f5
diff --git a/internal/runtime/workspace_preparation.go b/internal/runtime/workspace_preparation.go
index 6b8f10c7..eedbddae 100644
--- a/internal/runtime/workspace_preparation.go
+++ b/internal/runtime/workspace_preparation.go
@@ -164,6 +164,9 @@ func preparationFingerprint(node domain.WorkflowNode, repositories []domain.Repo
}
func preparationQuote(s string) string { return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'" }
+// preparationOutputLimit bounds the script output kept in a preparation step.
+const preparationOutputLimit = 32 << 10
+
var preparationANSI = regexp.MustCompile(`\x1b\[[0-?]*[ -/]*[@-~]`)
var preparationSecretKey = regexp.MustCompile(`(?i)(api[_-]?key|token|secret|password|passwd|authorization|credential)`)
var preparationSecretAssignment = regexp.MustCompile(`(?i)(\b[\w.-]*(?:api[_-]?key|token|secret|password|passwd|authorization)[\w.-]*["']?\s*[=:]\s*)(?:"[^"]*"|'[^']*'|(?:Bearer\s+)?[^\s,;]+)`)
@@ -233,15 +236,25 @@ func preparationFailureDetail(output string) string {
output = strings.Join(lines, "\n")
const limit = 2 << 10
if len(output) > limit {
- start := len(output) - limit + len("…\n")
- for start < len(output) && !utf8.RuneStart(output[start]) {
- start++
- }
- output = "…\n" + output[start:]
+ output = "…\n" + tailAtRuneBoundary(output, limit-len("…\n"))
}
return output
}
+// tailAtRuneBoundary returns at most the last limit bytes of s. When the cut
+// falls inside a multi-byte UTF-8 sequence it moves forward to the next rune
+// start, so the result never begins with a partial character.
+func tailAtRuneBoundary(s string, limit int) string {
+ if len(s) <= limit {
+ return s
+ }
+ start := len(s) - limit
+ for start < len(s) && !utf8.RuneStart(s[start]) {
+ start++
+ }
+ return s[start:]
+}
+
func preparationCommand(node domain.WorkflowNode, repo *domain.RepositoryContext) string {
script := node.Script
if node.Type == "command" {
@@ -339,14 +352,7 @@ func EnsureWorkspacePreparation(ctx context.Context, sb Sandbox, wf domain.Workf
var output string
execute := func(repo *domain.RepositoryContext) error {
raw, execErr := sb.Exec(ctx, CommandSpec{Argv: []string{"sh", "-lc", preparationCommand(node, repo)}, Provider: "script", CredentialBindings: []CredentialBinding{BindGit, BindMCP}}, nil)
- output += redactPreparationOutput(sb, raw)
- if len(output) > 32<<10 {
- start := len(output) - (32 << 10)
- for start < len(output) && !utf8.RuneStart(output[start]) {
- start++
- }
- output = output[start:]
- }
+ output = tailAtRuneBoundary(output+redactPreparationOutput(sb, raw), preparationOutputLimit)
return execErr
}
if node.ExecutionScope == domain.ExecutionScopeRepositories {
@@ -413,8 +419,8 @@ func RecordWorkspacePreparation(ctx context.Context, sb Sandbox, wf domain.Workf
if state.Environment != epoch {
state = WorkspacePreparationStatus{Steps: []WorkspacePreparationStep{}, Environment: epoch}
}
- if len(output) > 32<<10 {
- output = output[:32<<10]
+ if len(output) > preparationOutputLimit {
+ output = output[:preparationOutputLimit]
}
step := WorkspacePreparationStep{NodeID: nodeID, Name: selected.Name, Status: "ready", Output: output, Fingerprint: preparationFingerprint(*selected, repositories, wf.SandboxSnapshot), FinishedAt: time.Now().UTC()}
replaced := false
diff --git a/internal/runtime/workspace_preparation_tail_test.go b/internal/runtime/workspace_preparation_tail_test.go
new file mode 100644
index 00000000..16cb0489
--- /dev/null
+++ b/internal/runtime/workspace_preparation_tail_test.go
@@ -0,0 +1,74 @@
+package runtime
+
+import (
+ "context"
+ "strings"
+ "testing"
+ "unicode/utf8"
+
+ "agentflow/internal/domain"
+)
+
+func TestTailAtRuneBoundaryKeepsShortInputUnchanged(t *testing.T) {
+ for _, s := range []string{"", "abc", "错误"} {
+ if got := tailAtRuneBoundary(s, len(s)); got != s {
+ t.Fatalf("tailAtRuneBoundary(%q, %d) = %q, want unchanged", s, len(s), got)
+ }
+ }
+}
+
+func TestTailAtRuneBoundaryKeepsLastBytesOfASCII(t *testing.T) {
+ if got := tailAtRuneBoundary("0123456789", 4); got != "6789" {
+ t.Fatalf("tail = %q, want %q", got, "6789")
+ }
+}
+
+func TestTailAtRuneBoundarySkipsPartialRune(t *testing.T) {
+ // "错" is three bytes; a 4-byte tail of "a错误" would start inside "错".
+ got := tailAtRuneBoundary("a错误", 4)
+ if got != "误" {
+ t.Fatalf("tail = %q, want %q", got, "误")
+ }
+ long := strings.Repeat("错误", 1000)
+ for _, limit := range []int{1, 2, 3, 4, 5, 100, 1001} {
+ got := tailAtRuneBoundary(long, limit)
+ if len(got) > limit || !utf8.ValidString(got) || !strings.HasSuffix(long, got) {
+ t.Fatalf("limit %d: tail %q is not a valid bounded suffix", limit, got)
+ }
+ if limit >= utf8.UTFMax && len(got) <= limit-utf8.UTFMax {
+ t.Fatalf("limit %d: tail kept only %d bytes", limit, len(got))
+ }
+ }
+}
+
+func TestPreparationFailureDetailTailStaysWithinLimit(t *testing.T) {
+ detail := preparationFailureDetail(strings.Repeat("错", 3000))
+ if !strings.HasPrefix(detail, "…\n") || len(detail) > 2<<10 || !utf8.ValidString(detail) {
+ t.Fatalf("detail is not a valid bounded tail: len=%d prefix=%q", len(detail), detail[:min(len(detail), 8)])
+ }
+}
+
+func TestRecordWorkspacePreparationBoundsStoredOutput(t *testing.T) {
+ root := t.TempDir()
+ sb := &localSandbox{id: "record-limit", workspace: root}
+ wf := domain.WorkflowDefinition{Nodes: []domain.WorkflowNode{{ID: "setup", Name: "setup", Type: "script", RunOnWorkspaceRebuild: true, Script: "true"}}}
+ ctx := context.Background()
+ exact := strings.Repeat("a", preparationOutputLimit)
+ if _, err := RecordWorkspacePreparation(ctx, sb, wf, nil, "setup", exact); err != nil {
+ t.Fatal(err)
+ }
+ if state := ReadWorkspacePreparationStatus(root, sb.ID()); len(state.Steps) != 1 || state.Steps[0].Output != exact {
+ t.Fatalf("output at the limit was altered: %d steps", len(state.Steps))
+ }
+ long := "head-" + strings.Repeat("b", preparationOutputLimit) + "-tail"
+ if _, err := RecordWorkspacePreparation(ctx, sb, wf, nil, "setup", long); err != nil {
+ t.Fatal(err)
+ }
+ state := ReadWorkspacePreparationStatus(root, sb.ID())
+ if len(state.Steps) != 1 {
+ t.Fatalf("record replaced the step into %d steps", len(state.Steps))
+ }
+ if got := state.Steps[0].Output; got != long[:preparationOutputLimit] {
+ t.Fatalf("recorded output not bounded to the first %d bytes: len=%d", preparationOutputLimit, len(got))
+ }
+}