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(pi): publish response wait after the last tool result

zqchris39f56d55d335
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 39facb16dd..2ed1713e31 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
@@ -2234,3 +2234,90 @@ describe('pi translator', () => {
});
});
});
+
+describe('Pi tool-end response speed publication', () => {
+ function withTurn(run: (harness: {
+ send: (now: number, event: Record<string, unknown>) => void;
+ latestStatus: () => unknown;
+ events: AgentEvent[];
+ ctx: ReturnType<typeof createPiTranslateContext>;
+ }) => void) {
+ const clock = vi.spyOn(Date, 'now').mockReturnValue(1_000);
+ const ctx = createPiTranslateContext(noopLogger);
+ const { queue, events } = makeQueue();
+ const send = (now: number, event: Record<string, unknown>) => {
+ clock.mockReturnValue(now);
+ translatePiEvent(ev(event), queue, ctx);
+ };
+ try {
+ send(1_000, { type: 'agent_start' });
+ send(2_000, { type: 'message_start', message: { role: 'assistant' } });
+ send(3_000, { type: 'message_update', assistantMessageEvent: { type: 'text_delta', delta: 'a'.repeat(40) } });
+ run({ send, events, ctx, latestStatus: () => events.filter(event => event.type === 'status').at(-1)?.data });
+ } finally {
+ disposePiTranslateContext(ctx);
+ clock.mockRestore();
+ }
+ }
+
+ it('publishes remaining parallel tools and starts waiting immediately after the last result', () => {
+ withTurn(({ send, latestStatus, ctx }) => {
+ send(5_000, { type: 'message_end', message: { role: 'assistant', content: [], usage: { output: 80 } } });
+ send(6_000, { type: 'tool_execution_start', toolCallId: 'a', toolName: 'read', args: {} });
+ send(7_000, { type: 'tool_execution_start', toolCallId: 'b', toolName: 'bash', args: {} });
+ send(8_000, { type: 'tool_execution_end', toolCallId: 'a', result: 'done' });
+ expect(latestStatus()).toMatchObject({ isRunning: true,
+ responseSpeed: { phase: 'paused', toolActive: true, recentRate: null } });
+ send(9_000, { type: 'tool_execution_end', toolCallId: 'b', result: 'done' });
+ expect(latestStatus()).toMatchObject({ isRunning: true,
+ responseSpeed: { phase: 'waiting', toolActive: false, waitingMs: 0, recentRate: null,
+ durationMs: 2_000, firstResponseMs: 2_000, outputTokens: 80 } });
+ send(10_000, { type: 'message_start', message: { role: 'toolResult' } });
+ expect(usageSnapshotOf(ctx).responseSpeed).toMatchObject({ phase: 'waiting', waitingMs: 1_000, durationMs: 2_000 });
+ });
+ });
+
+ it('preserves a request already waiting while a parallel tool finishes', () => {
+ withTurn(({ send, latestStatus }) => {
+ send(4_000, { type: 'message_end', message: { role: 'assistant', content: [] } });
+ send(5_000, { type: 'tool_execution_start', toolCallId: 'a', toolName: 'read', args: {} });
+ send(6_000, { type: 'message_start', message: { role: 'assistant' } });
+ send(9_000, { type: 'tool_execution_end', toolCallId: 'a', result: 'done' });
+ expect(latestStatus()).toMatchObject({ responseSpeed: {
+ phase: 'waiting', toolActive: false, waitingMs: 3_000, durationMs: 1_000 } });
+ });
+ });
+
+ it('preserves parallel model output instead of reopening its generation clock', () => {
+ withTurn(({ send, latestStatus }) => {
+ send(3_100, { type: 'tool_execution_start', toolCallId: 'a', toolName: 'read', args: {} });
+ send(4_000, { type: 'message_update', assistantMessageEvent: { type: 'text_delta', delta: 'b'.repeat(40) } });
+ send(4_500, { type: 'tool_execution_end', toolCallId: 'a', result: 'done' });
+ expect(latestStatus()).toMatchObject({ responseSpeed: {
+ phase: 'generating', toolActive: false, durationMs: 1_500, recentRate: 10, outputTokens: 20 } });
+ });
+ });
+
+ it('does not publish running status for orphan, duplicate or settled-turn results', () => {
+ withTurn(({ send, events, latestStatus }) => {
+ const countStatus = () => events.filter(event => event.type === 'status').length;
+ let count = countStatus();
+ send(4_000, { type: 'tool_execution_end', toolCallId: 'unknown', result: 'done' });
+ expect(countStatus()).toBe(count);
+ send(5_000, { type: 'message_end', message: { role: 'assistant', content: [] } });
+ send(6_000, { type: 'tool_execution_start', toolCallId: 'a', toolName: 'read', args: {} });
+ send(7_000, { type: 'tool_execution_end', toolCallId: 'a', result: 'done' });
+ count = countStatus();
+ send(8_000, { type: 'tool_execution_end', toolCallId: 'a', result: 'done' });
+ expect(countStatus()).toBe(count);
+ send(9_000, { type: 'tool_execution_start', toolCallId: 'b', toolName: 'read', args: {} });
+ send(10_000, { type: 'agent_settled' });
+ count = countStatus();
+ const terminal = latestStatus();
+ send(11_000, { type: 'tool_execution_end', toolCallId: 'b', result: 'done' });
+ expect(countStatus()).toBe(count);
+ expect(latestStatus()).toEqual(terminal);
+ expect(latestStatus()).toMatchObject({ isRunning: false, responseSpeed: { phase: 'complete' } });
+ });
+ });
+});
diff --git a/packages/maker-core/src/agents/pi/translator.ts b/packages/maker-core/src/agents/pi/translator.ts
index 0a4c6fb8ad..983946425d 100644
--- a/packages/maker-core/src/agents/pi/translator.ts
+++ b/packages/maker-core/src/agents/pi/translator.ts
@@ -897,7 +897,7 @@ export function translatePiEvent(
case 'tool_execution_end': {
const toolUseId = String(event.toolCallId ?? '');
- ctx.responseSpeed.toolEnded(toolUseId);
+ const endedTrackedTool = ctx.responseSpeed.toolEnded(toolUseId);
const isError = event.isError === true;
const rawText = toolResultFullText(event.result);
const toolName = String(
@@ -947,6 +947,13 @@ export function translatePiEvent(
source: 'pi',
});
}
+ if (endedTrackedTool && ctx.isStreaming) {
+ const speed = ctx.responseSpeed.snapshot();
+ // The last tool result is an observable wait boundary, even before
+ // Pi emits the next message_start. Keep parallel output/request clocks.
+ if (!speed.toolActive && speed.phase === 'paused') ctx.responseSpeed.beginRequest();
+ pushStatus(queue, ctx, 'Working…', true);
+ }
return;
}
diff --git a/packages/maker-core/src/agents/shared/response-speed-adapters.test.ts b/packages/maker-core/src/agents/shared/response-speed-adapters.test.ts
index 08a89e9d3a..5fb04f9390 100644
--- a/packages/maker-core/src/agents/shared/response-speed-adapters.test.ts
+++ b/packages/maker-core/src/agents/shared/response-speed-adapters.test.ts
@@ -38,9 +38,11 @@ it('Pi publishes streaming estimates with untouched billing/cache counters, then
send(6_000, { type: 'tool_execution_start', toolCallId: 't', toolName: 'bash', args: {} });
expect(usageSnapshotOf(ctx).responseSpeed).toMatchObject({ phase: 'paused', toolActive: true, recentRate: null });
send(16_000, { type: 'tool_execution_end', toolCallId: 't', toolName: 'bash', result: { content: [] } });
- expect(usageSnapshotOf(ctx).responseSpeed?.toolActive).toBe(false);
+ expect(events.filter(event => event.type === 'status').at(-1)?.data).toMatchObject({
+ responseSpeed: { phase: 'waiting', toolActive: false, waitingMs: 0, durationMs: 2_000 },
+ });
send(17_000, { type: 'message_start', message: { role: 'toolResult' } });
- expect(usageSnapshotOf(ctx).responseSpeed?.phase).toBe('paused');
+ expect(usageSnapshotOf(ctx).responseSpeed).toMatchObject({ phase: 'waiting', waitingMs: 1_000 });
send(17_000, { type: 'message_start', message: { role: 'assistant' } });
send(18_000, { type: 'message_update', assistantMessageEvent: { type: 'text_delta', delta: 'answer' } });
send(19_000, { type: 'message_end', message: { role: 'assistant', content: [{ type: 'text', text: 'answer' }],
diff --git a/packages/maker-core/src/agents/shared/response-speed.ts b/packages/maker-core/src/agents/shared/response-speed.ts
index 118a4fc3d2..55782b203f 100644
--- a/packages/maker-core/src/agents/shared/response-speed.ts
+++ b/packages/maker-core/src/agents/shared/response-speed.ts
@@ -99,7 +99,7 @@ export class ResponseSpeedTracker {
}
toolStarted(id: string): void { if (id) this.tools.add(id); }
- toolEnded(id: string): void { this.tools.delete(id); }
+ toolEnded(id: string): boolean { return this.tools.delete(id); }
reportOutput(output: number | undefined): void {
if (output !== undefined && Number.isFinite(output) && output >= 0) this.reported = output;