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): recover extended finalized rich-answer tails

xuzini-xzn5f4e21fc0a84
diff --git a/apps/mobile/src/__tests__/richAnswer.test.tsx b/apps/mobile/src/__tests__/richAnswer.test.tsx
index 66d5bfef08..918ec9967e 100644
--- a/apps/mobile/src/__tests__/richAnswer.test.tsx
+++ b/apps/mobile/src/__tests__/richAnswer.test.tsx
@@ -68,3 +68,18 @@ it('keeps incomplete and read-only answers from sending', async () => {
expect(container.querySelector('button')!.disabled).toBe(true);
expect(send).not.toHaveBeenCalled();
});
+
+// Tier 1: a completed-looking partial snapshot used to poison the mounted parser.
+// The same answer must recover on its final snapshot without leaving/reopening it.
+it('replaces the incomplete notice with the late full answer without remounting', async () => {
+ const full = JSON.stringify({ version: 1, seq: 1, op: 'create', id: 'answer', kind: 'markdown', props: { text: 'Complete findings' } });
+ const renderSnapshot = async (snapshot: string) => {
+ await act(async () => root.render(createElement(SessionQuickSendProvider, { value: null },
+ createElement(RichAnswer, { source: snapshot, stateKey: 'late-final', complete: true, renderMarkdown: text => text }))));
+ };
+ await renderSnapshot(full.slice(0, -6));
+ expect(container.textContent).toContain('message.richAnswer.incomplete');
+ await renderSnapshot(full);
+ expect(container.textContent).toContain('Complete findings');
+ expect(container.textContent).not.toContain('message.richAnswer.incomplete');
+});
diff --git a/packages/maker-shared/src/__tests__/richAnswer.test.ts b/packages/maker-shared/src/__tests__/richAnswer.test.ts
index 6227f2817c..826d47b9d8 100644
--- a/packages/maker-shared/src/__tests__/richAnswer.test.ts
+++ b/packages/maker-shared/src/__tests__/richAnswer.test.ts
@@ -32,6 +32,56 @@ describe('streaming answer documents', () => {
expect(new RichAnswerStream().update(create + append).nodes).toEqual(next.nodes);
expect(parser.update(create).nodes.get('a')?.props.text).toBe('one');
});
+ it.each([false, true])('recovers a prematurely finalized partial operation when the full snapshot arrives (newline=%s)', (newline) => {
+ const heading = line(1, { op: 'create', id: 'title', kind: 'heading', props: { text: 'Results' } });
+ const answer = line(2, { op: 'create', id: 'answer', kind: 'markdown', props: { text: 'Complete findings' } });
+ const full = heading + (newline ? answer : answer.trimEnd());
+ const parser = new RichAnswerStream();
+ const partial = parser.update(heading + answer.slice(0, -8), true);
+ expect(partial.roots).toEqual(['title']);
+ expect(partial.issues).toBe(1);
+ // Identical snapshots must not count the unfinished tail as another error.
+ expect(parser.update(heading + answer.slice(0, -8), true).issues).toBe(1);
+ const recovered = parser.update(full, true);
+ expect(recovered.roots).toEqual(['title', 'answer']);
+ expect(recovered.nodes.get('answer')?.props.text).toBe('Complete findings');
+ expect(recovered.issues).toBe(0);
+ expect(recovered.nodes).toEqual(new RichAnswerStream().update(full, true).nodes);
+ expect(partial.roots).toEqual(['title']);
+ expect(partial.nodes.has('answer')).toBe(false);
+ });
+ it('preserves a valid finalized tail and applies later operations exactly once', () => {
+ const create = line(1, { op: 'create', id: 'answer', kind: 'text', props: { text: 'one' } }).trimEnd();
+ const append = line(2, { op: 'append', id: 'answer', text: ' two' });
+ const parser = new RichAnswerStream();
+ const first = parser.update(create, true);
+ const full = create + String.fromCharCode(10) + append;
+ const next = parser.update(full, true);
+ expect(next.nodes.get('answer')?.props.text).toBe('one two');
+ expect(next.issues).toBe(0);
+ expect(parser.update(full, true).nodes.get('answer')?.props.text).toBe('one two');
+ expect(first.nodes.get('answer')?.props.text).toBe('one');
+ });
+ it('preserves committed content when an extended finalized tail exceeds the source limit', () => {
+ const create = line(1, { op: 'create', id: 'answer', kind: 'text', props: { text: 'Readable' } }).trimEnd();
+ const parser = new RichAnswerStream();
+ parser.update(create, true);
+ const rejected = parser.update(create + ' '.repeat(2_000_001), true);
+ expect(rejected.nodes.get('answer')?.props.text).toBe('Readable');
+ expect(rejected.issues).toBe(1);
+ const recovered = parser.update(create + String.fromCharCode(10), true);
+ expect(recovered.nodes.get('answer')?.props.text).toBe('Readable');
+ expect(recovered.issues).toBe(0);
+ });
+ it('retains genuine malformed-line issues after an unfinished tail recovers', () => {
+ const prefix = 'invalid' + String.fromCharCode(10);
+ const answer = line(1, { op: 'create', id: 'answer', kind: 'text', props: { text: 'Readable' } });
+ const parser = new RichAnswerStream();
+ expect(parser.update(prefix + answer.slice(0, -8), true).issues).toBe(2);
+ const recovered = parser.update(prefix + answer, true);
+ expect(recovered.nodes.get('answer')?.props.text).toBe('Readable');
+ expect(recovered.issues).toBe(1);
+ });
it('keeps user-edited state while model defaults and calculated data change', () => {
const parser = new RichAnswerStream(), memory = new RichInteractionMemory();
const source = line(1, { op: 'create', id: 'months', kind: 'range', props: { default: 6 } }) + line(2, { op: 'create', id: 'cost', kind: 'stat', props: { value: { op: 'mul', args: [{ ref: 'months' }, 10] } } });
diff --git a/packages/maker-shared/src/richAnswer.ts b/packages/maker-shared/src/richAnswer.ts
index ead65ca67c..5df3e0e56d 100644
--- a/packages/maker-shared/src/richAnswer.ts
+++ b/packages/maker-shared/src/richAnswer.ts
@@ -140,7 +140,15 @@ export class RichAnswerStream {
private revision = 0;
private issues = 0;
update(source: string, complete = false): RichDocument {
- if (!source.startsWith(this.source)) {
+ // A terminal snapshot can precede the remaining text. If its unterminated
+ // last line was already consumed, replay the corrected snapshot rather than
+ // parsing just the suffix and retaining the earlier partial-line error.
+ const extendsFinalLine = 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) {
this.source = "";
this.offset = 0;
this.seq = 0;