fix: mark untranslated fallbacks and drop English progress text

- When the Korean rewrite errors, the stream ends before it arrives, or a
  final must go out without a result event, append a Korean notice to the
  English text instead of sending it bare (content is kept: dropping a
  NEEDS_CONTEXT question would be worse than English).
- A still-English rewrite is sent unchanged: the nudge tells the model to
  repeat only when the user explicitly asked for English.
- Intermediate progress text cannot be rewritten mid-turn, so English
  progress (>= 20 Latin letters, Hangul < 10%) is dropped before it is
  flushed by tool_progress / tool results / the result event.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-24 02:11:42 +09:00
parent 98011e282b
commit fdfdd384bf
3 changed files with 129 additions and 26 deletions

View File

@@ -1,6 +1,10 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const outputs: Array<{ status: string; result: string | null }> = [];
const outputs: Array<{
status: string;
result: string | null;
phase?: string;
}> = [];
let scriptedTurns: Array<Array<Record<string, unknown>>> = [];
const promptsSeen: string[] = [];
@@ -30,15 +34,25 @@ vi.mock('../src/output-protocol.js', async (importOriginal) => {
await importOriginal<typeof import('../src/output-protocol.js')>();
return {
...actual,
writeOutput: (output: { status: string; result: string | null }) => {
outputs.push({ status: output.status, result: output.result });
writeOutput: (output: {
status: string;
result: string | null;
phase?: string;
}) => {
outputs.push({
status: output.status,
result: output.result,
phase: output.phase,
});
},
};
});
import { runClaudeQuery } from '../src/claude-query-runner.js';
import {
KOREAN_REWRITE_FAILED_NOTICE,
KOREAN_REWRITE_NUDGE,
isEnglishProgress,
isKoreanReplyTarget,
needsKoreanRewrite,
} from '../src/reply-language-guard.js';
@@ -84,7 +98,12 @@ async function run() {
});
}
const visible = () => outputs.map((o) => [o.status, o.result]);
// Text the host can post: finals and intermediate progress text (tool-progress
// timer labels like "Bash (3s)" are not prose and are left out).
const visible = () =>
outputs
.filter((o) => o.phase !== 'progress')
.map((o) => [o.phase ?? 'final', o.result]);
describe('needsKoreanRewrite', () => {
it('flags an English final answer', () => {
@@ -113,6 +132,17 @@ describe('needsKoreanRewrite', () => {
).toBe(false);
});
it('flags English progress text but not Korean or command-only text', () => {
expect(isEnglishProgress('Uncheck the import option, then continue.')).toBe(
true,
);
expect(isEnglishProgress('가져오기 옵션을 끄고 계속 진행하겠습니다.')).toBe(
false,
);
expect(isEnglishProgress('`bun run build` 실행 중입니다.')).toBe(false);
expect(isEnglishProgress('Done.')).toBe(false);
});
it('is only enabled for a Korean (or unset) AGENT_LANGUAGE', () => {
expect(isKoreanReplyTarget(undefined)).toBe(true);
expect(isKoreanReplyTarget('')).toBe(true);
@@ -139,7 +169,7 @@ describe('Claude runner Korean rewrite', () => {
];
await run();
expect(promptsSeen).toEqual(['사용자 요청', KOREAN_REWRITE_NUDGE]);
expect(visible()).toEqual([['success', KOREAN_FINAL]]);
expect(visible()).toEqual([['final', KOREAN_FINAL]]);
});
it('also catches the end_turn shortcut and ignores the stale result', async () => {
@@ -149,7 +179,7 @@ describe('Claude runner Korean rewrite', () => {
];
await run();
expect(promptsSeen).toEqual(['사용자 요청', KOREAN_REWRITE_NUDGE]);
expect(visible()).toEqual([['success', KOREAN_FINAL]]);
expect(visible()).toEqual([['final', KOREAN_FINAL]]);
});
it('does not loop when the rewrite is still English', async () => {
@@ -160,7 +190,7 @@ describe('Claude runner Korean rewrite', () => {
];
await run();
expect(promptsSeen).toHaveLength(2);
expect(visible()).toEqual([['success', stillEnglish]]);
expect(visible()).toEqual([['final', stillEnglish]]);
});
it('falls back to the original answer when the rewrite errors', async () => {
@@ -169,20 +199,46 @@ describe('Claude runner Korean rewrite', () => {
[result('', 'error_during_execution')],
];
await run();
expect(visible()).toEqual([['success', ENGLISH_FINAL]]);
expect(visible()).toEqual([
['final', `${ENGLISH_FINAL}${KOREAN_REWRITE_FAILED_NOTICE}`],
]);
});
it('falls back to the original answer when the stream ends early', async () => {
scriptedTurns = [[assistant(ENGLISH_FINAL), result(ENGLISH_FINAL)]];
await run();
expect(visible()).toEqual([['success', ENGLISH_FINAL]]);
expect(visible()).toEqual([
['final', `${ENGLISH_FINAL}${KOREAN_REWRITE_FAILED_NOTICE}`],
]);
});
it('drops English progress text flushed by tool_progress or tool results', async () => {
const englishProgress = 'Uncheck the import option, then continue.';
const koreanProgress = '가져오기 옵션을 끄고 계속 진행하겠습니다.';
scriptedTurns = [
[
assistant(englishProgress),
{ type: 'tool_progress', tool_name: 'Bash', elapsed_time_seconds: 3 },
assistant(koreanProgress),
{ type: 'user', message: { content: [] } },
assistant('Clicked Continue on the Trimble sign-in page.'),
{ type: 'user', message: { content: [] } },
assistant(KOREAN_FINAL),
result(KOREAN_FINAL),
],
];
await run();
expect(visible()).toEqual([
['intermediate', koreanProgress],
['final', KOREAN_FINAL],
]);
});
it('leaves Korean answers alone and emits them once', async () => {
scriptedTurns = [[assistant(KOREAN_FINAL), result(KOREAN_FINAL)]];
await run();
expect(promptsSeen).toEqual(['사용자 요청']);
expect(visible()).toEqual([['success', KOREAN_FINAL]]);
expect(visible()).toEqual([['final', KOREAN_FINAL]]);
});
it('is disabled when AGENT_LANGUAGE is another language', async () => {
@@ -190,6 +246,6 @@ describe('Claude runner Korean rewrite', () => {
scriptedTurns = [[assistant(ENGLISH_FINAL), result(ENGLISH_FINAL)]];
await run();
expect(promptsSeen).toEqual(['사용자 요청']);
expect(visible()).toEqual([['success', ENGLISH_FINAL]]);
expect(visible()).toEqual([['final', ENGLISH_FINAL]]);
});
});