Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/node/services/agentPlugins/promptHooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from "bun:test";
import { EventSpine } from "@/node/services/events/eventSpine";
import { runMessageSendHooks, runTurnStopHooks } from "./promptHooks";
import type { CompletedMessagePart } from "@/common/types/stream";
import { lastAssistantText, runMessageSendHooks, runTurnStopHooks } from "./promptHooks";
import { renderPluginHookContextEntry } from "@/node/utils/messages/renderPluginHookContextForProvider";

const input = { text: "hello", origin: "user" as const, canBlock: true };
Expand Down Expand Up @@ -197,3 +198,41 @@ describe("runTurnStopHooks", () => {
).toEqual({ kind: "continue", prompt: "Run the tests again." });
});
});

describe("lastAssistantText", () => {
test("a long run keeps its end, cut across the joined text parts", () => {
const promise = "<promise>DONE</promise>";
const tail = "y".repeat(10_000 - promise.length) + promise;
const parts: CompletedMessagePart[] = [
{ type: "text", text: "x".repeat(10_000) },
{ type: "text", text: tail },
];

expect(lastAssistantText(parts)).toBe("x".repeat(6_384) + tail);
});

test("a cut inside a surrogate pair drops the lone low half", () => {
const parts: CompletedMessagePart[] = [{ type: "text", text: "😀".repeat(10_000) + "!" }];

expect(lastAssistantText(parts)).toBe("😀".repeat(8_191) + "!");
});

test("a cut on a high surrogate keeps the whole pair", () => {
const parts: CompletedMessagePart[] = [{ type: "text", text: "!" + "😀".repeat(10_000) }];

expect(lastAssistantText(parts)).toBe("😀".repeat(8_192));
});

test("a turn that ends on a tool is null even after long text", () => {
const tool: CompletedMessagePart = {
type: "dynamic-tool",
toolCallId: "t1",
toolName: "bash",
state: "output-available",
input: {},
output: {},
};

expect(lastAssistantText([{ type: "text", text: "x".repeat(20_000) }, tool])).toBeNull();
});
});
7 changes: 5 additions & 2 deletions src/node/services/agentPlugins/promptHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function capContextEntries(

/** turn.stop reply text is cut to this length. */
const TURN_STOP_TEXT_MAX_CHARS = 16 * 1024;
const LEADING_LONE_LOW_SURROGATE = /^[\uDC00-\uDFFF]/;
/** A continuation prompt is a new user row; longer prompts are cut. */
export const TURN_STOP_PROMPT_MAX_CHARS = 32_000;

Expand Down Expand Up @@ -168,7 +169,8 @@ export async function runTurnStopHooks(
* The reply's trailing text run (Codex last_assistant_message). Text before a later tool call is
* an intermediate step, not the reply, so a turn that ends on a tool part gets null (#6067).
* Reasoning parts are skipped. Taken from the stream-end parts, not a history read, so rows
* appended after the reply cannot hide it (#6062).
* appended after the reply cannot hide it (#6062). A long run keeps its end, because a completion
* promise sits at the end of a reply.
*/
export function lastAssistantText(parts: readonly CompletedMessagePart[]): string | null {
let text = "";
Expand All @@ -178,5 +180,6 @@ export function lastAssistantText(parts: readonly CompletedMessagePart[]): strin
if (part.type !== "text") break;
text = part.text + text;
}
return text.trim().length > 0 ? text.slice(0, TURN_STOP_TEXT_MAX_CHARS) : null;
if (text.trim().length === 0) return null;
return text.slice(-TURN_STOP_TEXT_MAX_CHARS).replace(LEADING_LONE_LOW_SURROGATE, "");
}
Loading