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: hold every leftover stop-token prefix

Dash307da8f96dd0
diff --git a/packages/maker-core/src/agents/claude-code/__tests__/translator-subagent-model.test.ts b/packages/maker-core/src/agents/claude-code/__tests__/translator-subagent-model.test.ts
index 9338e7c778..9a8171b3d3 100644
--- a/packages/maker-core/src/agents/claude-code/__tests__/translator-subagent-model.test.ts
+++ b/packages/maker-core/src/agents/claude-code/__tests__/translator-subagent-model.test.ts
@@ -195,6 +195,33 @@ describe('Claude Code assistant text streaming contract', () => {
expect(ctx.turn.uiEmittedText).toBe('');
});
+ it('does not emit a stop token split as a single-character prefix', async () => {
+ const queue = createAsyncQueue<AgentEvent>();
+ const ctx = createCtx();
+
+ for (const [uuid, text] of [
+ ['stream-lt-1', '<'],
+ ['stream-lt-2', '|eos|>'],
+ ] as const) {
+ translateSdkMessage(
+ {
+ type: 'stream_event',
+ uuid,
+ session_id: 'sdk-session',
+ parent_tool_use_id: null,
+ event: { type: 'content_block_delta', delta: { type: 'text_delta', text } },
+ },
+ queue,
+ ctx,
+ );
+ }
+
+ const textEvents = (await collect(queue)).filter((event) => event.type === 'text');
+ expect(textEvents).toEqual([]);
+ expect(ctx.turn.hasEmittedText).toBe(false);
+ expect(ctx.turn.uiEmittedText).toBe('');
+ });
+
it('does not emit leading whitespace from a split standalone stop token', async () => {
const queue = createAsyncQueue<AgentEvent>();
const ctx = createCtx();
diff --git a/packages/maker-core/src/agents/codex/translator.test.ts b/packages/maker-core/src/agents/codex/translator.test.ts
index f75de10d5e..bffb933763 100644
--- a/packages/maker-core/src/agents/codex/translator.test.ts
+++ b/packages/maker-core/src/agents/codex/translator.test.ts
@@ -1966,6 +1966,7 @@ describe('codex internal citation 归一化 (#785)', () => {
const braceComplete = 'abc :codex-file-citation{path="/tmp/a{b}.md"}';
expect(stableCitationBoundary(braceComplete)).toBe(braceComplete.length);
expect(stableCitationBoundary('<|eo')).toBe(0);
+ expect(stableCitationBoundary('<')).toBe(0);
expect(stableCitationBoundary(' <|eos|>')).toBe(0);
expect(stableCitationBoundary('The token is <|eos|>')).toBe('The token is <|eos|>'.length);
});
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts
index 6cec77ee59..54396c20d3 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts
@@ -216,6 +216,44 @@ describe('pi translator', () => {
]);
});
+ it('does not emit a leaked Grok stop token split as a single-character prefix', () => {
+ const ctx = createPiTranslateContext(noopLogger);
+ const { queue, events } = makeQueue();
+
+ translatePiEvent(ev({ type: 'message_start' }), queue, ctx);
+ translatePiEvent(
+ ev({
+ type: 'message_update',
+ assistantMessageEvent: { type: 'text_delta', contentIndex: 0, delta: '<' },
+ }),
+ queue,
+ ctx,
+ );
+ translatePiEvent(
+ ev({
+ type: 'message_update',
+ assistantMessageEvent: { type: 'text_delta', contentIndex: 0, delta: '|eos|>' },
+ }),
+ queue,
+ ctx,
+ );
+ translatePiEvent(
+ ev({
+ type: 'message_end',
+ message: {
+ role: 'assistant',
+ content: [{ type: 'text', text: '<|eos|>' }],
+ model: 'xai/grok-4.6',
+ stopReason: 'stop',
+ },
+ }),
+ queue,
+ ctx,
+ );
+
+ expect(events.filter((event) => event.type === 'text')).toEqual([]);
+ });
+
it('surfaces a terminal provider error after Pi settles instead of staying in Working', () => {
const ctx = createPiTranslateContext(noopLogger);
const { queue, events } = makeQueue();
diff --git a/packages/maker-shared/src/internalCitation.test.ts b/packages/maker-shared/src/internalCitation.test.ts
index 3ece73bfd3..1aac83d032 100644
--- a/packages/maker-shared/src/internalCitation.test.ts
+++ b/packages/maker-shared/src/internalCitation.test.ts
@@ -57,6 +57,8 @@ describe('leaked model stop tokens', () => {
expect(stableStandaloneModelStopTokenBoundary('<|eos|>')).toBe(0);
expect(stableStandaloneModelStopTokenBoundary('The token is <|eo')).toBe('The token is <|eo'.length);
expect(isPossibleStandaloneStopPrefix(' <|eo')).toBe(true);
+ expect(isPossibleStandaloneStopPrefix('<')).toBe(true);
+ expect(isPossibleStandaloneStopPrefix('<|')).toBe(true);
expect(isPossibleStandaloneStopPrefix('The token is <|eo')).toBe(false);
});
@@ -76,4 +78,26 @@ describe('leaked model stop tokens', () => {
expect(a).toEqual({ pending: '<|eos|>', emitted: false });
expect(b).toEqual({ pending: '', emitted: true });
});
+
+ it('holds a one-character leftover prefix until the closer arrives', () => {
+ const buffer = { pending: '', emitted: false };
+ expect(holdStandaloneStopTokenDelta(buffer, '<')).toBeNull();
+ expect(holdStandaloneStopTokenDelta(buffer, '|eos|>')).toBeNull();
+ expect(buffer).toEqual({ pending: '<|eos|>', emitted: false });
+ });
+
+ it('still strips a split web citation after ordinary prose has been emitted', () => {
+ const buffer = { pending: '', emitted: false };
+ expect(holdStandaloneStopTokenDelta(buffer, '结论。')).toBe('结论。');
+ expect(holdStandaloneStopTokenDelta(buffer, '\uE200ci')).toBeNull();
+ expect(holdStandaloneStopTokenDelta(buffer, 'te\uE202turn17search1\uE201 后续')).toBe(' 后续');
+ expect(buffer.emitted).toBe(true);
+ expect(buffer.pending).toBe('');
+ });
+
+ it('keeps an embedded stop token after ordinary prose has been emitted', () => {
+ const buffer = { pending: '', emitted: false };
+ expect(holdStandaloneStopTokenDelta(buffer, 'The token is ')).toBe('The token is ');
+ expect(holdStandaloneStopTokenDelta(buffer, '<|eos|>')).toBe('<|eos|>');
+ });
});
diff --git a/packages/maker-shared/src/internalCitation.ts b/packages/maker-shared/src/internalCitation.ts
index a1da6139e0..268bc3d1c5 100644
--- a/packages/maker-shared/src/internalCitation.ts
+++ b/packages/maker-shared/src/internalCitation.ts
@@ -10,7 +10,6 @@
* is not user-facing prose either.
*/
const MODEL_STOP_TOKENS = new Set(['<|endoftext|>', '<|eot_id|>', '<|im_end|>', '<|eos|>']);
-const MODEL_STOP_TOKEN_NAMES = ['endoftext', 'eot_id', 'im_end', 'eos'] as const;
const LONGEST_MODEL_STOP_TOKEN = Math.max(
...[...MODEL_STOP_TOKENS].map((token) => token.length),
);
@@ -88,16 +87,15 @@ export function stripStandaloneModelStopToken(text: string): string {
/**
* True while `text` is still only a possible whole-message stop token:
- * surrounding whitespace, a known token, or an incomplete `<|eos` prefix.
- * Used to hold streaming deltas until the leftover can be dropped or flushed.
+ * surrounding whitespace, a known token, or any incomplete prefix of one
+ * (`<`, `<|`, `<|eo`, …). Used to hold streaming deltas until the leftover
+ * can be dropped or flushed.
*/
export function isPossibleStandaloneStopPrefix(text: string): boolean {
- if (MODEL_STOP_TOKENS.has(text.trim())) return true;
if (text.length > MAX_STANDALONE_STOP_PREFIX) return false;
const candidate = text.trim();
if (candidate.length === 0) return true;
- if (!candidate.startsWith('<|')) return false;
- return MODEL_STOP_TOKEN_NAMES.some((name) => `<|${name}|>`.startsWith(candidate));
+ return [...MODEL_STOP_TOKENS].some((token) => token.startsWith(candidate));
}
/**
@@ -117,8 +115,10 @@ export interface StandaloneStopTokenHold {
/**
* Hold a leftover stop token until this stream has either completed it or
- * proven it is ordinary prose. The buffer is per stream; callers must not
- * share it across concurrent text streams.
+ * proven it is ordinary prose. After visible prose starts, still hold an
+ * unfinished Web-citation tail so split markers are not emitted raw.
+ * The buffer is per stream; callers must not share it across concurrent
+ * text streams.
*/
export function holdStandaloneStopTokenDelta(
buffer: StandaloneStopTokenHold,
@@ -129,8 +129,12 @@ export function holdStandaloneStopTokenDelta(
buffer.pending = combined;
return null;
}
- buffer.pending = '';
- const visible = buffer.emitted ? combined : stripInternalWebCitations(combined);
+ const citationEnd = stableInternalWebCitationBoundary(combined);
+ buffer.pending = combined.slice(citationEnd);
+ const stable = combined.slice(0, citationEnd);
+ const visible = buffer.emitted
+ ? stripInternalWebCitationMarkers(stable)
+ : stripInternalWebCitations(stable);
if (visible.length > 0) buffer.emitted = true;
- return visible;
+ return visible.length > 0 ? visible : null;
}