From 896fb6fee1c1673617c36644eb18358eb8231bb3 Mon Sep 17 00:00:00 2001 From: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com> Date: Tue, 9 Jun 2026 07:22:09 +0000 Subject: [PATCH] fix(clawsweeper): address review for automerge-openclaw-openclaw-91642 (1) --- scripts/docs-i18n/main.go | 4 +++ scripts/docs-i18n/main_test.go | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/scripts/docs-i18n/main.go b/scripts/docs-i18n/main.go index 4b0be7a2b2d2..c3ca6299ab6b 100644 --- a/scripts/docs-i18n/main.go +++ b/scripts/docs-i18n/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "flag" "fmt" "log" @@ -185,6 +186,9 @@ func runDocsI18N(ctx context.Context, cfg runConfig, files []string, newTranslat elapsed := time.Since(start).Round(time.Millisecond) log.Printf("docs-i18n: completed processed=%d skipped=%d elapsed=%s", processed, skipped, elapsed) if translationErr != nil && cfg.allowPartial && cfg.mode == "doc" && processed > 0 { + if ctx.Err() != nil || errors.Is(translationErr, context.Canceled) || errors.Is(translationErr, context.DeadlineExceeded) { + return translationErr + } log.Printf("docs-i18n: allowing partial doc output after translation error: %v", translationErr) return nil } diff --git a/scripts/docs-i18n/main_test.go b/scripts/docs-i18n/main_test.go index 52bdf0658673..b775cc64ce4d 100644 --- a/scripts/docs-i18n/main_test.go +++ b/scripts/docs-i18n/main_test.go @@ -126,6 +126,31 @@ func (cancelAwareTranslator) TranslateRaw(ctx context.Context, text, _, _ string func (cancelAwareTranslator) Close() {} +type cancelAfterFirstDocTranslator struct { + cancel context.CancelFunc + calls int +} + +func (t *cancelAfterFirstDocTranslator) Translate(ctx context.Context, text, _, _ string) (string, error) { + if err := ctx.Err(); err != nil { + return "", err + } + return text, nil +} + +func (t *cancelAfterFirstDocTranslator) TranslateRaw(ctx context.Context, text, _, _ string) (string, error) { + if err := ctx.Err(); err != nil { + return "", err + } + t.calls++ + if t.calls == 1 { + t.cancel() + } + return text, nil +} + +func (t *cancelAfterFirstDocTranslator) Close() {} + func TestRunDocsI18NRewritesFinalLocalizedPageLinks(t *testing.T) { t.Parallel() @@ -316,6 +341,41 @@ func TestRunDocsI18NAllowPartialStopsAfterRunCancellation(t *testing.T) { } } +func TestRunDocsI18NAllowPartialReturnsCancellationAfterPartialSuccess(t *testing.T) { + t.Parallel() + + docsRoot := t.TempDir() + writeFile(t, filepath.Join(docsRoot, ".i18n", "glossary.zh-CN.json"), "[]") + writeFile(t, filepath.Join(docsRoot, "docs.json"), `{"redirects":[]}`) + firstPath := filepath.Join(docsRoot, "aaa-first.md") + secondPath := filepath.Join(docsRoot, "zzz-second.md") + writeFile(t, firstPath, "# Gateway\n") + writeFile(t, secondPath, "# Gateway\n") + + ctx, cancel := context.WithCancel(context.Background()) + err := runDocsI18N(ctx, runConfig{ + targetLang: "zh-CN", + sourceLang: "en", + docsRoot: docsRoot, + mode: "doc", + thinking: "high", + overwrite: true, + allowPartial: true, + parallel: 1, + }, []string{firstPath, secondPath}, func(_, _ string, _ []GlossaryEntry, _ string) (docsTranslator, error) { + return &cancelAfterFirstDocTranslator{cancel: cancel}, nil + }) + if !errors.Is(err, context.Canceled) { + t.Fatalf("expected canceled run after partial success, got %v", err) + } + if got := mustReadFile(t, filepath.Join(docsRoot, "zh-CN", "aaa-first.md")); !strings.Contains(got, "# Gateway") { + t.Fatalf("expected first output to be written before cancellation, got:\n%s", got) + } + if _, err := os.Stat(filepath.Join(docsRoot, "zh-CN", "zzz-second.md")); err == nil { + t.Fatal("did not expect later output to be written after run cancellation") + } +} + func TestRunDocsI18NRewritesLineTitleFromExactGlossaryWithoutModel(t *testing.T) { t.Parallel()