Skip to content
Open
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
24 changes: 24 additions & 0 deletions mcp/src/repo/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ import {
StepMeta,
} from "./session.js";
import { McpServer, getMcpTools } from "./mcpServers.js";
import {
getSessionContextHintMessage,
updateSessionOverflowContext,
} from "./context.js";

function SYSTEM_PROMPT_END(qs: boolean) {
const normalEnd = `CRITICAL: When you are ready to provide your final answer, output your complete response followed by [END_OF_ANSWER] on a new line. Don't start your answer with preamble like "Ok! I have all the information I need. Let me create a plan...". Just start with your answer.
Expand Down Expand Up @@ -236,6 +240,7 @@ interface PreparedAgent {
turnIndex: number;
provenanceCollector: ProvenanceCollector;
abortSignal: AbortSignal | undefined;
contextPressureDetected: boolean;
}

/** Returns true if the error was caused by an AbortSignal. */
Expand Down Expand Up @@ -406,12 +411,19 @@ Apply the guidance from each skill throughout your response.`;
let sessionId: string | undefined;
let previousMessages: ModelMessage[] = [];
let hasSystemTurn = false;
let contextPressureDetected = false;

if (inputSessionId) {
if (sessionExists(inputSessionId)) {
sessionId = inputSessionId;
hasSystemTurn = loadSession(sessionId)[0]?.role === "system";
previousMessages = opts.isolatedContext ? [] : loadSessionMessages(sessionId);
if (!opts.isolatedContext) {
const contextHint = getSessionContextHintMessage(sessionId);
if (contextHint) {
previousMessages = [contextHint, ...previousMessages];
}
}
} else {
sessionId = createNewSession(inputSessionId, instructions, opts.source, repoLabel);
hasSystemTurn = true;
Expand Down Expand Up @@ -458,6 +470,9 @@ Apply the guidance from each skill throughout your response.`;
const lastStep = steps.length > 0 ? steps[steps.length - 1] : null;
const inputTokens = lastStep?.usage?.inputTokens ?? 0;
const truncated = await truncateOldToolResults(messages, inputTokens, contextLimit);
if (truncated !== messages) {
contextPressureDetected = true;
}
if (truncated === messages) return undefined;
return { messages: truncated };
},
Expand Down Expand Up @@ -487,6 +502,7 @@ Apply the guidance from each skill throughout your response.`;
turnIndex,
provenanceCollector,
abortSignal: opts.abortSignal,
contextPressureDetected,
};
}

Expand Down Expand Up @@ -526,6 +542,7 @@ export async function get_context(
startTime,
stepMetas,
provenanceCollector,
contextPressureDetected,
} = prepared;
const { schema } = opts;

Expand Down Expand Up @@ -567,6 +584,9 @@ export async function get_context(
if (provenanceCollector.entries.length > 0) {
appendSearchProvenance(sessionId, provenanceCollector.entries);
}
if (contextPressureDetected) {
await updateSessionOverflowContext(sessionId, newMessages, model);
}

await appendSessionEnd(sessionId, {
end_time: new Date().toISOString(),
Expand Down Expand Up @@ -631,6 +651,7 @@ export async function stream_context(
startTime,
stepMetas,
provenanceCollector,
contextPressureDetected,
} = prepared;
const streamResult = await prepared.agent.stream(buildCallParams(prepared));

Expand All @@ -653,6 +674,9 @@ export async function stream_context(
if (provenanceCollector.entries.length > 0) {
appendSearchProvenance(sessionId, provenanceCollector.entries);
}
if (contextPressureDetected) {
await updateSessionOverflowContext(sessionId, newMessages, prepared.model);
}
const stepUsage = stepMetas.length > 0
? normalizeUsage(addUsage(...stepMetas.map((step) => step.usage)))
: normalizeUsage(usage);
Expand Down
Loading
Loading