@@ -126,6 +126,24 @@ func (cancelAwareTranslator) TranslateRaw(ctx context.Context, text, _, _ string
126126
127127func (cancelAwareTranslator ) Close () {}
128128
129+ type contextErrorTranslator struct {}
130+
131+ func (contextErrorTranslator ) Translate (_ context.Context , text , _ , _ string ) (string , error ) {
132+ if strings .Contains (text , "CANCEL" ) {
133+ return "" , context .Canceled
134+ }
135+ return text , nil
136+ }
137+
138+ func (contextErrorTranslator ) TranslateRaw (_ context.Context , text , _ , _ string ) (string , error ) {
139+ if strings .Contains (text , "CANCEL" ) {
140+ return "" , context .Canceled
141+ }
142+ return text , nil
143+ }
144+
145+ func (contextErrorTranslator ) Close () {}
146+
129147type cancelAfterFirstDocTranslator struct {
130148 cancel context.CancelFunc
131149 calls int
@@ -376,6 +394,42 @@ func TestRunDocsI18NAllowPartialReturnsCancellationAfterPartialSuccess(t *testin
376394 }
377395}
378396
397+ func TestRunDocsI18NAllowPartialStopsAfterContextError (t * testing.T ) {
398+ t .Parallel ()
399+
400+ docsRoot := t .TempDir ()
401+ writeFile (t , filepath .Join (docsRoot , ".i18n" , "glossary.zh-CN.json" ), "[]" )
402+ writeFile (t , filepath .Join (docsRoot , "docs.json" ), `{"redirects":[]}` )
403+ firstPath := filepath .Join (docsRoot , "aaa-first.md" )
404+ cancelPath := filepath .Join (docsRoot , "bbb-cancel.md" )
405+ laterPath := filepath .Join (docsRoot , "zzz-later.md" )
406+ writeFile (t , firstPath , "# Gateway\n " )
407+ writeFile (t , cancelPath , "# CANCEL\n " )
408+ writeFile (t , laterPath , "# Gateway\n " )
409+
410+ err := runDocsI18N (context .Background (), runConfig {
411+ targetLang : "zh-CN" ,
412+ sourceLang : "en" ,
413+ docsRoot : docsRoot ,
414+ mode : "doc" ,
415+ thinking : "high" ,
416+ overwrite : true ,
417+ allowPartial : true ,
418+ parallel : 1 ,
419+ }, []string {firstPath , cancelPath , laterPath }, func (_ , _ string , _ []GlossaryEntry , _ string ) (docsTranslator , error ) {
420+ return contextErrorTranslator {}, nil
421+ })
422+ if ! errors .Is (err , context .Canceled ) {
423+ t .Fatalf ("expected cancellation error to remain terminal, got %v" , err )
424+ }
425+ if got := mustReadFile (t , filepath .Join (docsRoot , "zh-CN" , "aaa-first.md" )); ! strings .Contains (got , "# Gateway" ) {
426+ t .Fatalf ("expected first output to be written before cancellation, got:\n %s" , got )
427+ }
428+ if _ , err := os .Stat (filepath .Join (docsRoot , "zh-CN" , "zzz-later.md" )); err == nil {
429+ t .Fatal ("did not expect later output to be written after context error" )
430+ }
431+ }
432+
379433func TestRunDocsI18NRewritesLineTitleFromExactGlossaryWithoutModel (t * testing.T ) {
380434 t .Parallel ()
381435
0 commit comments