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(chat): retry only the failed rich-answer line

xuzini-xzn045fde139efa
diff --git a/apps/mobile/src/__tests__/richAnswer.test.tsx b/apps/mobile/src/__tests__/richAnswer.test.tsx
index 76669c73ea..5c9c52b22d 100644
--- a/apps/mobile/src/__tests__/richAnswer.test.tsx
+++ b/apps/mobile/src/__tests__/richAnswer.test.tsx
@@ -93,4 +93,13 @@ it('keeps committed text visible while malformed suffixes grow without remountin
expect(container.textContent).toContain('Readable findings');
if (suffix) expect(container.textContent).toContain('message.richAnswer.incomplete');
}
+ const prefix = full + '{"seq":2' + String.fromCharCode(10);
+ const later = JSON.stringify({ seq: 2, op: 'create', id: 'later', kind: 'markdown', props: { text: 'Later findings' } });
+ for (const tail of [later.slice(0, -6), later]) {
+ await act(async () => root.render(createElement(SessionQuickSendProvider, { value: null },
+ createElement(RichAnswer, { source: prefix + tail, stateKey: 'malformed-tail', complete: true, renderMarkdown: text => text }))));
+ expect(container.textContent).toContain('Readable findings');
+ expect(container.textContent).toContain('message.richAnswer.incomplete');
+ }
+ expect(container.textContent).toContain('Later findings');
});
diff --git a/packages/maker-shared/src/__tests__/richAnswer.test.ts b/packages/maker-shared/src/__tests__/richAnswer.test.ts
index e276c92211..69bd1efcad 100644
--- a/packages/maker-shared/src/__tests__/richAnswer.test.ts
+++ b/packages/maker-shared/src/__tests__/richAnswer.test.ts
@@ -76,6 +76,14 @@ describe('streaming answer documents', () => {
expect(next.roots).toEqual(['answer']);
expect(next.issues).toBeGreaterThan(0);
}
+ const prefix = update + '{"seq":3' + String.fromCharCode(10);
+ const beforeRecovery = parser.update(prefix, true);
+ const later = line(operation === 'create' ? 2 : 3, { op: 'create', id: 'later', kind: 'text', props: { text: 'Later' } });
+ parser.update(prefix + later.slice(0, -8), true);
+ const recovered = parser.update(prefix + later, true);
+ expect(recovered.nodes.get('answer')?.props.text).toBe(expected);
+ expect(recovered.nodes.get('later')?.props.text).toBe('Later');
+ expect(recovered.issues).toBe(beforeRecovery.issues);
expect(committed.nodes.get('answer')?.props.text).toBe(expected);
});
it('preserves committed content when an extended finalized tail exceeds the source limit', () => {
diff --git a/packages/maker-shared/src/richAnswer.ts b/packages/maker-shared/src/richAnswer.ts
index 993b048fec..8853c01335 100644
--- a/packages/maker-shared/src/richAnswer.ts
+++ b/packages/maker-shared/src/richAnswer.ts
@@ -139,26 +139,25 @@ export class RichAnswerStream {
private roots: string[] = [];
private revision = 0;
private issues = 0;
- private failedFinalLine = false;
+ private failedFinalLine: { offset: number; seq: number; issues: number } | null = null;
private sourceTooLong = false;
update(source: string, complete = false): RichDocument {
- // Replay only a failed final line. Extending a committed operation with a
- // malformed suffix must not discard the content it already made readable.
- const extendsFinalLine = this.failedFinalLine
- && source.length > this.source.length
- && source.length <= MAX_SOURCE
- && this.offset === this.source.length
- && this.offset > 0
- && !this.source.endsWith("\n");
- if (!source.startsWith(this.source) || extendsFinalLine) {
+ if (!source.startsWith(this.source)) {
this.source = "";
this.offset = 0;
this.seq = 0;
this.nodes = new Map();
this.roots = [];
this.issues = 0;
- this.failedFinalLine = false;
+ this.failedFinalLine = null;
this.revision++;
+ } else if (this.failedFinalLine && source.length > this.source.length && source.length <= MAX_SOURCE) {
+ // Retry only the failed line. Earlier committed content can differ from
+ // reparsing its source after malformed suffixes were already consumed.
+ this.offset = this.failedFinalLine.offset;
+ this.seq = this.failedFinalLine.seq;
+ this.issues = this.failedFinalLine.issues;
+ this.failedFinalLine = null;
}
if (source.length > MAX_SOURCE) {
this.sourceTooLong = true;
@@ -170,14 +169,15 @@ export class RichAnswerStream {
let end: number;
while ((end = source.indexOf("\n", this.offset)) !== -1) {
this.line(source.slice(this.offset, end));
- this.failedFinalLine = false;
+ this.failedFinalLine = null;
this.offset = end + 1;
}
if (complete && this.offset < source.length) {
+ const checkpoint = { offset: this.offset, seq: this.seq, issues: this.issues };
const failed = !this.line(source.slice(this.offset));
- // A suffix parsed after an earlier committed tail is not a whole line:
- // replaying it would erase that earlier operation on the next snapshot.
- this.failedFinalLine = failed && (this.offset === 0 || source[this.offset - 1] === "\n");
+ // A suffix after a committed tail is not a whole line and must not be
+ // retried as one when later snapshots extend it.
+ this.failedFinalLine = failed && (this.offset === 0 || source[this.offset - 1] === "\n") ? checkpoint : null;
this.offset = source.length;
}
return this.snapshot();