Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(scheduler): 空终稿判据改为不变式,定时任务收尾不再重复或多写回复(#5220 后续) (#5282)

FicoWang002ca0a2932b
diff --git a/apps/desktop/src/main/scheduler-host/__tests__/runnerSilentNotify.test.ts b/apps/desktop/src/main/scheduler-host/__tests__/runnerSilentNotify.test.ts
index fb2e789136..4a3984a9da 100644
--- a/apps/desktop/src/main/scheduler-host/__tests__/runnerSilentNotify.test.ts
+++ b/apps/desktop/src/main/scheduler-host/__tests__/runnerSilentNotify.test.ts
@@ -295,6 +295,100 @@ describe('MakerScheduleRunner silent-run notification skip', () => {
expect(JSON.stringify(notifier.notify.mock.calls)).not.toContain('Retracted partial');
});
+ // #5277: the empty-final rule must be shape independent. An empty
+ // authoritative final only retracts an unsealed streamed partial; a sealed
+ // full-text item already lives in the transcript and is never replayed.
+ const commentary = { text: 'Checking the PR.', isFinal: true, isFullText: true, phase: 'commentary' };
+ const assistantRows = () =>
+ mocks.createMessage.mock.calls.filter(([, body]) => body.role === 'assistant');
+
+ it.each(['', ' '])('does not replay commentary after two empty authoritative finals (#5277 A1, %j)', async (secondEmpty) => {
+ const h = createSessionHarness(acceptingSend());
+ const { runner } = createRunnerHarness(h.session, { silenced: true });
+ const pending = runner.fire(baseSchedule({ silentWhenIdle: true }), createFireContext());
+ await vi.waitFor(() => expect(mocks.createMessage).toHaveBeenCalled());
+ h.emit({ type: 'text', source: 'codex', data: commentary });
+ h.emit({ type: 'text', source: 'codex', data: { text: '', isFinal: true, isFullText: true, phase: 'final_answer' } });
+ h.emit({ type: 'text', source: 'codex', data: { text: secondEmpty, isFinal: true, isFullText: true, phase: 'final_answer' } });
+ h.emit({ type: 'done', data: { result: 'Checking the PR.' } });
+ await expect(pending).resolves.toMatchObject({ resultText: 'Checking the PR.' });
+ expect(assistantRows()).toHaveLength(0);
+ });
+
+ it.each([
+ ['without phase', { text: '', isFinal: true, isFullText: true }],
+ ['as an empty commentary', { text: '', isFinal: true, isFullText: true, phase: 'commentary' }],
+ ])('does not replay commentary after an empty authoritative final %s (#5277 A2)', async (_label, emptyFinal) => {
+ const h = createSessionHarness(acceptingSend());
+ const { runner } = createRunnerHarness(h.session, { silenced: true });
+ const pending = runner.fire(baseSchedule({ silentWhenIdle: true }), createFireContext());
+ await vi.waitFor(() => expect(mocks.createMessage).toHaveBeenCalled());
+ h.emit({ type: 'text', source: 'codex', data: commentary });
+ h.emit({ type: 'text', source: 'codex', data: emptyFinal });
+ h.emit({ type: 'done', data: { result: 'Checking the PR.' } });
+ await expect(pending).resolves.toMatchObject({ resultText: 'Checking the PR.' });
+ expect(assistantRows()).toHaveLength(0);
+ });
+
+ it('does not replay commentary when an empty commentary precedes the empty final answer (#5277 real-device shape)', async () => {
+ // Real rollout: 「我先看一下。」→ `<|eos|>` → `:codex-file-citation{}`; the last two
+ // are cleaned to empty text and done falls back to the first item.
+ const h = createSessionHarness(acceptingSend());
+ const { runner } = createRunnerHarness(h.session, { silenced: true });
+ const pending = runner.fire(baseSchedule({ silentWhenIdle: true }), createFireContext());
+ await vi.waitFor(() => expect(mocks.createMessage).toHaveBeenCalled());
+ h.emit({ type: 'text', source: 'codex', data: commentary });
+ h.emit({ type: 'text', source: 'codex', data: { text: '', isFinal: true, isFullText: true, phase: 'commentary' } });
+ h.emit({ type: 'text', source: 'codex', data: { text: '', isFinal: true, isFullText: true, phase: 'final_answer' } });
+ h.emit({ type: 'done', data: { result: 'Checking the PR.' } });
+ await expect(pending).resolves.toMatchObject({ resultText: 'Checking the PR.' });
+ expect(assistantRows()).toHaveLength(0);
+ });
+
+ it('does not replay a streamed answer that follows an empty final when done carries the canonical text (#5277 B2)', async () => {
+ const h = createSessionHarness(acceptingSend());
+ const { runner } = createRunnerHarness(h.session, { silenced: true });
+ const pending = runner.fire(baseSchedule({ silentWhenIdle: true }), createFireContext());
+ await vi.waitFor(() => expect(mocks.createMessage).toHaveBeenCalled());
+ h.emit({ type: 'text', source: 'codex', data: commentary });
+ h.emit({ type: 'text', source: 'codex', data: { text: '', isFinal: true, isFullText: true, phase: 'final_answer' } });
+ h.emit({ type: 'text', source: 'codex', data: { text: 'The PR looks ', isFinal: false, phase: 'final_answer' } });
+ h.emit({ type: 'text', source: 'codex', data: { text: 'fine.', isFinal: false, phase: 'final_answer' } });
+ h.emit({ type: 'done', data: { result: 'The PR looks fine.' } });
+ await expect(pending).resolves.toMatchObject({ resultText: 'The PR looks fine.' });
+ expect(assistantRows()).toHaveLength(0);
+ });
+
+ it('keeps a Claude Code fallback tail in the same message after a block final (#5282 review)', async () => {
+ // claude-code emits each text block as isFinal without isFullText, then
+ // handleResult pushes the UI-missed tail as a delta of the same message and
+ // done carries the whole assistant text. The tail must extend the block.
+ const h = createSessionHarness(acceptingSend());
+ const { runner } = createRunnerHarness(h.session, { silenced: true });
+ const pending = runner.fire(baseSchedule({ silentWhenIdle: true }), createFireContext());
+ await vi.waitFor(() => expect(mocks.createMessage).toHaveBeenCalled());
+ h.emit({ type: 'text', source: 'claude-code', data: { text: 'The PR looks', isFinal: true } });
+ h.emit({ type: 'text', source: 'claude-code', data: { text: ' fine.', isFinal: false } });
+ h.emit({ type: 'done', data: { result: 'The PR looks fine.' } });
+ await expect(pending).resolves.toMatchObject({ resultText: 'The PR looks fine.' });
+ expect(assistantRows()).toHaveLength(0);
+ });
+
+ it('still writes the canonical answer when the stream after an empty final was retracted again (#5277)', async () => {
+ const h = createSessionHarness(acceptingSend());
+ const { runner } = createRunnerHarness(h.session, { silenced: true });
+ const pending = runner.fire(baseSchedule({ silentWhenIdle: true }), createFireContext());
+ await vi.waitFor(() => expect(mocks.createMessage).toHaveBeenCalled());
+ h.emit({ type: 'text', source: 'codex', data: commentary });
+ h.emit({ type: 'text', source: 'codex', data: { text: '', isFinal: true, isFullText: true, phase: 'final_answer' } });
+ h.emit({ type: 'text', source: 'codex', data: { text: 'Retracted partial', isFinal: false, phase: 'final_answer' } });
+ h.emit({ type: 'text', source: 'codex', data: { text: '', isFinal: true, isFullText: true, phase: 'final_answer' } });
+ h.emit({ type: 'done', data: { result: 'The PR looks fine.' } });
+ await expect(pending).resolves.toMatchObject({ resultText: 'The PR looks fine.' });
+ expect(assistantRows()).toHaveLength(1);
+ expect(assistantRows()[0]![1]).toMatchObject({ content: 'The PR looks fine.' });
+ });
+
it.each(
[true, false].flatMap((silenced) =>
['result', 'finalText'].map((field) => ({ silenced, field })),
diff --git a/apps/desktop/src/main/scheduler-host/runner.ts b/apps/desktop/src/main/scheduler-host/runner.ts
index 81a6c4b88e..33183a37ba 100644
--- a/apps/desktop/src/main/scheduler-host/runner.ts
+++ b/apps/desktop/src/main/scheduler-host/runner.ts
@@ -3218,7 +3218,11 @@ export class MakerScheduleRunner implements ScheduleRunner {
const sessionId = initialSession.id;
let assistantText = '';
let finalTextMatchesStream = false;
- let lastTextWasCodexCommentary = false;
+ // A non-empty full-text snapshot (`isFullText`, e.g. a completed Codex or
+ // Pi item) is already a sealed transcript row; deltas that follow it start a
+ // new message. A block final without `isFullText` (claude-code) may still
+ // be extended by the same message's fallback tail, so it stays unsealed.
+ let assistantTextSealed = false;
let stopped = false;
let stopListeningTurn: (() => void) | undefined;
const turnFinished = new Promise<void>((resolve, reject) => {
@@ -3322,19 +3326,24 @@ export class MakerScheduleRunner implements ScheduleRunner {
text?: string; isFinal?: boolean; isFullText?: boolean; phase?: string;
} | null;
if (data && typeof data.text === 'string') {
- // Only Codex's separate empty answer after completed commentary
- // leaves that commentary intact. Empty replacements after deltas
- // must still retract the partial result, matching the transcript.
- const emptyCodexAnswer = lastTextWasCodexCommentary
- && ev.source === 'codex' && data.phase === 'final_answer'
- && data.isFinal === true && data.isFullText === true && !data.text.trim();
- lastTextWasCodexCommentary = ev.source === 'codex'
- && data.phase === 'commentary' && data.isFinal === true
- && data.isFullText === true && !!data.text.trim();
- if (emptyCodexAnswer) return;
+ if (data.isFinal && !data.text.trim()) {
+ // An empty authoritative final only retracts a partial that is
+ // still streaming, matching the transcript. A sealed full text
+ // (Codex commentary before its empty answer item, an empty item
+ // without phase, or a repeated empty item) is never replayed,
+ // regardless of the event shape that carried it (#5220, #5277).
+ if (!assistantTextSealed) assistantText = '';
+ return;
+ }
if (data.text.trim()) finalTextMatchesStream = true;
- if (data.isFinal) assistantText = data.text;
- else assistantText += data.text;
+ if (data.isFinal) {
+ assistantText = data.text;
+ assistantTextSealed = data.isFullText === true;
+ } else {
+ if (assistantTextSealed) assistantText = '';
+ assistantTextSealed = false;
+ assistantText += data.text;
+ }
}
return;
}