diff --git a/packages/junior/src/chat/services/context-compaction.ts b/packages/junior/src/chat/services/context-compaction.ts index 6028153d2..302a519ca 100644 --- a/packages/junior/src/chat/services/context-compaction.ts +++ b/packages/junior/src/chat/services/context-compaction.ts @@ -29,6 +29,7 @@ import { getConversationEventStore } from "@/chat/db"; import type { ThreadConversationState } from "@/chat/state/conversation"; import { logWarn, setSpanAttributes } from "@/chat/logging"; import { + getUserMessageInstructionText, retainRuntimeTurnContext, stripRuntimeTurnContext, trimTrailingAssistantMessages, @@ -667,7 +668,25 @@ export async function compactContextForHandoff( throw new Error("Handoff requires the current runtime turn context"); } const generatedSummary = await summarizeContext(args, deps); - const summary = `${MODEL_HANDOFF_SUMMARY_PREFIX}\n${generatedSummary}`; + const currentInstruction = [...args.piMessages] + .reverse() + .map(getUserMessageInstructionText) + .find((text) => text && !isCompactionSummary(text)); + const boundedCurrentInstruction = currentInstruction + ? sanitizeText(currentInstruction).slice(0, MAX_RENDERED_MESSAGE_CHARS) + : undefined; + const summary = [ + MODEL_HANDOFF_SUMMARY_PREFIX, + ...(boundedCurrentInstruction + ? [ + "Current user instruction at handoff:", + boundedCurrentInstruction, + "", + ] + : []), + "Continuation summary:", + generatedSummary, + ].join("\n"); const instructionMessage = { role: "user", content: [{ type: "text", text: renderCurrentInstruction(summary) }], diff --git a/packages/junior/tests/component/services/context-compaction.test.ts b/packages/junior/tests/component/services/context-compaction.test.ts index 726962c0d..61a3a4887 100644 --- a/packages/junior/tests/component/services/context-compaction.test.ts +++ b/packages/junior/tests/component/services/context-compaction.test.ts @@ -542,11 +542,14 @@ describe("context compaction projection reset", () => { "Continue the outstanding request now", ); expect(textOf(handoffMessages[1]!)).toContain( - "Continue the multi-file implementation.", + "Current user instruction at handoff:\nImplement the multi-file change.", + ); + expect(textOf(handoffMessages[1]!)).toContain( + "Continuation summary:\nContinue the multi-file implementation.", ); const durableHandoffMessages = [ user( - "\nModel handoff checkpoint. Continue the outstanding request now using this summary as the complete prior context:\nContinue the multi-file implementation.\n", + "\nModel handoff checkpoint. Continue the outstanding request now using this summary as the complete prior context:\nCurrent user instruction at handoff:\nImplement the multi-file change.\n\nContinuation summary:\nContinue the multi-file implementation.\n", 3, ), ];