Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix: share the standalone stop-token hold with PI
Dash35e98a0e1bcd
diff --git a/packages/maker-core/src/agents/claude-code/translator.ts b/packages/maker-core/src/agents/claude-code/translator.tsindex 671b818b7c..c7ca49ceed 100644--- a/packages/maker-core/src/agents/claude-code/translator.ts+++ b/packages/maker-core/src/agents/claude-code/translator.ts@@ -22,8 +22,9 @@ import {redactSensitiveText,} from '@cindy/maker-shared/error-redaction';import {- isPossibleStandaloneStopPrefix,+ holdStandaloneStopTokenDelta,stripInternalWebCitations,+ type StandaloneStopTokenHold,} from '@cindy/maker-shared/internal-citation';import type { createAsyncQueue } from '../shared/async-queue.js';import { stripTerminalControlSequences } from '../shared/terminal-output.js';@@ -152,7 +153,7 @@ export interface RuntimeState {* 必须先拼回该 stream 的快照再清洗。`emitted` 只表示本流是否已发出可见* 正文:独立停止符按这个判定,不能看整轮 `uiEmittedText`。*/- streamStopTokenByKey: Map<string, { pending: string; emitted: boolean }>;+ streamStopTokenByKey: Map<string, StandaloneStopTokenHold>;}export function newRuntimeState(): RuntimeState {@@ -1290,18 +1291,9 @@ function handleStreamEvent(if (delta.type === 'text_delta' && typeof delta.text === 'string') {const buffer = ctx.rt.streamStopTokenByKey.get(streamKey)?? { pending: '', emitted: false };- const combined = buffer.pending + delta.text;- if (!buffer.emitted && isPossibleStandaloneStopPrefix(combined)) {- buffer.pending = combined;- ctx.rt.streamStopTokenByKey.set(streamKey, buffer);- return;- }- buffer.pending = '';- const visibleDelta = buffer.emitted- ? combined- : stripInternalWebCitations(combined);- if (visibleDelta.length > 0) {- buffer.emitted = true;+ const visibleDelta = holdStandaloneStopTokenDelta(buffer, delta.text);+ ctx.rt.streamStopTokenByKey.set(streamKey, buffer);+ if (visibleDelta && visibleDelta.length > 0) {ctx.turn.hasEmittedText = true;ctx.turn.uiEmittedText += visibleDelta;queue.push({@@ -1311,7 +1303,6 @@ function handleStreamEvent(agentMeta: fallbackMeta,});}- ctx.rt.streamStopTokenByKey.set(streamKey, buffer);} else if (delta.type === 'thinking_delta' && typeof delta.thinking === 'string') {// 第一次见 thinking_delta 时 lazy-init 一个 buffer (老链路 agentManager.ts:2322-2335)if (!ctx.rt.currentThinking) {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.tsindex 46f4070c3b..6cec77ee59 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@@ -160,6 +160,62 @@ describe('pi translator', () => {]);});+ it('does not emit a leaked Grok stop token split across PI deltas', () => {+ 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: '<|eo' },+ }),+ queue,+ ctx,+ );+ translatePiEvent(+ ev({+ type: 'message_update',+ assistantMessageEvent: { type: 'text_delta', contentIndex: 1, delta: 'answer' },+ }),+ queue,+ ctx,+ );+ translatePiEvent(+ ev({+ type: 'message_update',+ assistantMessageEvent: { type: 'text_delta', contentIndex: 0, delta: 's|>' },+ }),+ queue,+ ctx,+ );+ translatePiEvent(+ ev({+ type: 'message_end',+ message: {+ role: 'assistant',+ content: [+ { type: 'text', text: '<|eos|>' },+ { type: 'text', text: 'answer' },+ ],+ model: 'xai/grok-4.6',+ stopReason: 'stop',+ },+ }),+ queue,+ ctx,+ );++ expect(events.filter((event) => event.type === 'text')).toEqual([+ { type: 'text', data: { text: 'answer', isFinal: false }, source: 'pi' },+ expect.objectContaining({+ type: 'text',+ data: { text: 'answer', isFinal: true, isFullText: true },+ source: 'pi',+ }),+ ]);+ });+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-core/src/agents/pi/translator.ts b/packages/maker-core/src/agents/pi/translator.tsindex a9c7be25ce..d96adf4558 100644--- a/packages/maker-core/src/agents/pi/translator.ts+++ b/packages/maker-core/src/agents/pi/translator.ts@@ -21,6 +21,11 @@ import {extractNonSecretErrorSignals,redactSensitiveText,} from '@cindy/maker-shared/error-redaction';+import {+ holdStandaloneStopTokenDelta,+ stripInternalWebCitations,+ type StandaloneStopTokenHold,+} from '@cindy/maker-shared/internal-citation';import type { AgentEvent, AgentTaskUpdateEventData, UsageSnapshot } from '../../types/index.js';import type { AsyncQueue } from '../shared/async-queue.js';import type { PiRpcEvent } from './rpc-client.js';@@ -81,6 +86,11 @@ export interface PiTranslateContext {thinkingSeq: number;/** contentIndex → 当前消息内的 thinking block 状态。 */thinkingBlocks: Map<number, PiThinkingBlock>;+ /**+ * contentIndex → 独立停止符暂存。text_delta 可能把 `<|eos|>` 拆开,+ * 必须按本块判定,不能和别的 text block 共用。+ */+ streamStopTokenByIndex: Map<number, StandaloneStopTokenHold>;/*** 本 turn 最后一条 assistant 消息的全文(每次非空 message_end 覆盖;agent_start 重置)。* 用于 agent_settled 的 done.data.result —— 与 CC/Codex 对齐:register.ts 的@@ -130,6 +140,7 @@ export function createPiTranslateContext(logger: Logger): PiTranslateContext {isStreaming: false,thinkingSeq: 0,thinkingBlocks: new Map(),+ streamStopTokenByIndex: new Map(),finalAssistantText: '',turnWallClockStartedAt: 0,generationDurationMs: 0,@@ -257,7 +268,10 @@ function applyDelegatedUsage(function assistantTextOf(message: PiAssistantMessage): string {const parts: string[] = [];for (const block of message.content ?? []) {- if (block.type === 'text' && typeof block.text === 'string') parts.push(block.text);+ if (block.type === 'text' && typeof block.text === 'string') {+ const visible = stripInternalWebCitations(block.text);+ if (visible.length > 0) parts.push(visible);+ }}return parts.join('\n\n');}@@ -336,6 +350,7 @@ export function translatePiEvent(// 也避免长会话里 taskId 条目无界堆积。ctx.delegatedUsage.clear();ctx.subagentToolCalls.clear();+ ctx.streamStopTokenByIndex.clear();pushStatus(queue, ctx, 'Working…', true);return;}@@ -345,6 +360,7 @@ export function translatePiEvent(case 'message_start': {ctx.thinkingBlocks.clear();+ ctx.streamStopTokenByIndex.clear();startPiGenerationHeartbeat(ctx);return;}@@ -671,7 +687,13 @@ function handleAssistantDelta(switch (delta.type) {case 'text_delta': {if (typeof delta.delta === 'string' && delta.delta.length > 0) {- queue.push({ type: 'text', data: { text: delta.delta, isFinal: false }, source: 'pi' });+ const buffer = ctx.streamStopTokenByIndex.get(contentIndex)+ ?? { pending: '', emitted: false };+ const visible = holdStandaloneStopTokenDelta(buffer, delta.delta);+ ctx.streamStopTokenByIndex.set(contentIndex, buffer);+ if (visible && visible.length > 0) {+ queue.push({ type: 'text', data: { text: visible, isFinal: false }, source: 'pi' });+ }}return;}diff --git a/packages/maker-shared/src/internalCitation.test.ts b/packages/maker-shared/src/internalCitation.test.tsindex 07162299c3..3ece73bfd3 100644--- a/packages/maker-shared/src/internalCitation.test.ts+++ b/packages/maker-shared/src/internalCitation.test.ts@@ -1,6 +1,7 @@import { describe, expect, it } from 'vitest';import {+ holdStandaloneStopTokenDelta,isPossibleStandaloneStopPrefix,stableInternalWebCitationBoundary,stableStandaloneModelStopTokenBoundary,@@ -65,4 +66,14 @@ describe('leaked model stop tokens', () => {'The token is <|eos|>',);});++ it('holds a split leftover on one stream without mixing another stream', () => {+ const a = { pending: '', emitted: false };+ const b = { pending: '', emitted: false };+ expect(holdStandaloneStopTokenDelta(a, '<|eo')).toBeNull();+ expect(holdStandaloneStopTokenDelta(b, 'answer')).toBe('answer');+ expect(holdStandaloneStopTokenDelta(a, 's|>')).toBeNull();+ expect(a).toEqual({ pending: '<|eos|>', emitted: false });+ expect(b).toEqual({ pending: '', emitted: true });+ });});diff --git a/packages/maker-shared/src/internalCitation.ts b/packages/maker-shared/src/internalCitation.tsindex 777980661a..a1da6139e0 100644--- a/packages/maker-shared/src/internalCitation.ts+++ b/packages/maker-shared/src/internalCitation.ts@@ -109,3 +109,28 @@ export function isPossibleStandaloneStopPrefix(text: string): boolean {export function stableStandaloneModelStopTokenBoundary(text: string): number {return isPossibleStandaloneStopPrefix(text) ? 0 : text.length;}++export interface StandaloneStopTokenHold {+ pending: string;+ emitted: boolean;+}++/**+ * 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.+ */+export function holdStandaloneStopTokenDelta(+ buffer: StandaloneStopTokenHold,+ delta: string,+): string | null {+ const combined = buffer.pending + delta;+ if (!buffer.emitted && isPossibleStandaloneStopPrefix(combined)) {+ buffer.pending = combined;+ return null;+ }+ buffer.pending = '';+ const visible = buffer.emitted ? combined : stripInternalWebCitations(combined);+ if (visible.length > 0) buffer.emitted = true;+ return visible;+}