Skip to content
Draft
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
21 changes: 20 additions & 1 deletion packages/junior/src/chat/services/context-compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<current-instruction>\nModel handoff checkpoint. Continue the outstanding request now using this summary as the complete prior context:\nContinue the multi-file implementation.\n</current-instruction>",
"<current-instruction>\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</current-instruction>",
3,
),
];
Expand Down
Loading