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
4 changes: 4 additions & 0 deletions packages/agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Reserved headroom when trimming OpenAI remote compaction input so `/responses/compact` requests stay below the model context window instead of filling the entire window.

## [0.6.2] - 2026-06-19

### Changed
Expand Down
12 changes: 11 additions & 1 deletion packages/agent/src/compaction/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ export function trimOpenAiCompactInput(
return trimmed;
}

export function resolveOpenAiCompactInputBudget(contextWindow: number, maxOutputTokens = 0): number {
if (contextWindow <= 0) return 0;
const reservedTokens = Math.max(Math.floor(contextWindow * 0.15), maxOutputTokens, 1);
return Math.max(1, contextWindow - reservedTokens);
}

function collectKnownOpenAiCallIds(items: Array<Record<string, unknown>>): Set<string> {
const knownCallIds = new Set<string>();
for (const item of items) {
Expand Down Expand Up @@ -473,7 +479,11 @@ export async function requestOpenAiRemoteCompaction(
const endpoint = resolveOpenAiCompactEndpoint(model, options?.authCredentialType);
const request: OpenAiRemoteCompactionRequest = {
model: model.id,
input: trimOpenAiCompactInput(compactInput, model.contextWindow, instructions),
input: trimOpenAiCompactInput(
compactInput,
resolveOpenAiCompactInputBudget(model.contextWindow, model.maxTokens),
instructions,
),
instructions,
};
const headers: Record<string, string> = {
Expand Down
35 changes: 34 additions & 1 deletion packages/agent/test/compaction-estimate-cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { describe, expect, test } from "bun:test";
import { estimateEntriesTokens, estimateEntryTokens, findCutPoint } from "@gajae-code/agent-core/compaction/compaction";
import type { SessionEntry, SessionMessageEntry } from "@gajae-code/agent-core/compaction/entries";
import { estimateOpenAiCompactInputTokens, trimOpenAiCompactInput } from "@gajae-code/agent-core/compaction/openai";
import {
estimateOpenAiCompactInputTokens,
resolveOpenAiCompactInputBudget,
trimOpenAiCompactInput,
} from "@gajae-code/agent-core/compaction/openai";
import { type PruneConfig, pruneToolOutputs } from "@gajae-code/agent-core/compaction/pruning";
import type { AssistantMessage, Message, ToolResultMessage } from "@gajae-code/ai/types";

Expand Down Expand Up @@ -344,6 +348,35 @@ describe("digest pruning notices", () => {
});

describe("OpenAI trim sizing", () => {
test("resolveOpenAiCompactInputBudget reserves output and clamps tiny positive windows", () => {
expect(resolveOpenAiCompactInputBudget(100, 0)).toBe(85);
expect(resolveOpenAiCompactInputBudget(100, 20)).toBe(80);
expect(resolveOpenAiCompactInputBudget(2, 0)).toBe(1);
expect(resolveOpenAiCompactInputBudget(1, 0)).toBe(1);
expect(resolveOpenAiCompactInputBudget(10, 20)).toBe(1);
expect(resolveOpenAiCompactInputBudget(0, 0)).toBe(0);
expect(resolveOpenAiCompactInputBudget(-5, 0)).toBe(0);
});

test("trimOpenAiCompactInput removes suffix items when resolved budget is below full context window", () => {
const instructions = "compact";
const contextWindow = 100;
const budget = resolveOpenAiCompactInputBudget(contextWindow, 20);
const items: Array<Record<string, unknown>> = [
{ type: "message", role: "user", content: [{ type: "input_text", text: "keep user message" }] },
{
type: "message",
role: "developer",
content: [{ type: "input_text", text: "remove developer message ".repeat(8) }],
},
];

expect(estimateOpenAiCompactInputTokens(items, instructions)).toBeLessThanOrEqual(contextWindow);
expect(estimateOpenAiCompactInputTokens(items, instructions)).toBeGreaterThan(budget);
expect(trimOpenAiCompactInput(items, contextWindow, instructions)).toEqual(items);
expect(trimOpenAiCompactInput(items, budget, instructions)).toEqual([items[0]]);
});

test("trimOpenAiCompactInput matches full recount across removable scenarios", () => {
const instructions = "compact these items";
const scenarios: Array<{ name: string; items: Array<Record<string, unknown>>; budget: number }> = [
Expand Down
28 changes: 28 additions & 0 deletions packages/agent/test/remote-compaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,34 @@ describe("buildOpenAiNativeHistory custom tool calls", () => {
});

describe("remote compaction input trimming", () => {
test("trims removable items against reserved input budget when input fits full context window", async () => {
let requestInput: Array<Record<string, unknown>> | undefined;
using _hook = hookFetch(async (_input, init) => {
const body = JSON.parse(String(init?.body)) as { input: Array<Record<string, unknown>> };
requestInput = body.input;
return Response.json({
output: [{ type: "compaction_summary", summary: "compact" }],
});
});

await requestOpenAiRemoteCompaction(
makeOpenAiModel({ contextWindow: 100, maxTokens: 20 }),
"test-key",
[
{ type: "message", role: "user", content: [{ type: "input_text", text: "keep user message" }] },
{
type: "message",
role: "developer",
content: [{ type: "input_text", text: "remove developer message ".repeat(8) }],
},
],
"compact",
);

expect(requestInput?.some(item => item.type === "message" && item.role === "developer")).toBe(false);
expect(requestInput?.some(item => item.type === "message" && item.role === "user")).toBe(true);
});

test("trims custom tool outputs with their matching custom calls", async () => {
let requestInput: Array<Record<string, unknown>> | undefined;
using _hook = hookFetch(async (_input, init) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/coding-agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
- Added a keyless `insane` web search provider that safely ports upstream insane-search public-route fallbacks without TLS impersonation, browser/cookie bypasses, credential storage, or auto-installed dependencies (#1011).
- `web_search` `auto` mode now drives native provider search over proxies/custom endpoints by reusing the active model's own credential + baseUrl when canonical native creds are absent: `activeContextNativeId()` matches the model's wire api (+ model-id family) to `anthropic` (anthropic-messages), `openai-compatible` (openai-responses/completions), or `gemini` (google-generative-ai Generative Language), each falling back to DuckDuckGo if the endpoint does not support web search.

### Fixed

- Hardened context-overflow recovery so automatic maintenance clears the TUI loader, surfaces overflow completion/skip status, retries resumable tails safely, and falls back to the synthetic auto-continue prompt for non-resumable tails when enabled.

## [0.7.1] - 2026-06-23
### Fixed

Expand Down
1 change: 1 addition & 0 deletions packages/coding-agent/src/extensibility/shared-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export interface AutoCompactionEndEvent {
errorMessage?: string;
/** True when compaction was skipped for a benign reason (no model, no candidates, nothing to compact). */
skipped?: boolean;
continuationSkipReason?: "auto_continue_disabled_non_resumable_tail";
}

/** Fired when auto-retry starts */
Expand Down
11 changes: 11 additions & 0 deletions packages/coding-agent/src/modes/controllers/event-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,12 +677,18 @@ export class EventController {
}
this.ctx.updateEditorBorderColor();
const isHandoffAction = event.action === "handoff";
const continuationDisabled = event.continuationSkipReason === "auto_continue_disabled_non_resumable_tail";
if (event.aborted) {
this.ctx.showStatus(isHandoffAction ? "Auto-handoff cancelled" : "Auto context-full maintenance cancelled");
} else if (event.result) {
this.ctx.rebuildChatFromMessages();
this.ctx.statusLine.invalidate();
this.ctx.updateEditorTopBorder();
if (continuationDisabled && !isHandoffAction) {
this.ctx.showStatus("Context overflow recovery skipped: auto_continue_disabled_non_resumable_tail");
} else if (event.willRetry && !isHandoffAction) {
this.ctx.showStatus("Context overflow maintenance completed");
}
} else if (event.errorMessage) {
this.ctx.showWarning(event.errorMessage);
} else if (isHandoffAction) {
Expand All @@ -695,6 +701,11 @@ export class EventController {
} else if (event.skipped) {
// Benign skip: no model selected, no candidate models available, or nothing
// to compact yet. Not a failure — suppress the warning.
if (continuationDisabled) {
this.ctx.showStatus("Context overflow recovery skipped: auto_continue_disabled_non_resumable_tail");
} else if (event.willRetry && !isHandoffAction) {
this.ctx.showStatus("Context overflow maintenance skipped");
}
} else {
this.ctx.showWarning("Auto context-full maintenance failed; continuing without maintenance");
}
Expand Down
117 changes: 72 additions & 45 deletions packages/coding-agent/src/session/agent-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ import { ToolChoiceQueue } from "./tool-choice-queue";
import { YieldQueue } from "./yield-queue";

/** Session-specific events that extend the core AgentEvent */
export type AutoCompactionContinuationSkipReason = "auto_continue_disabled_non_resumable_tail";

export type AgentSessionEvent =
| AgentEvent
| { type: "auto_compaction_start"; reason: "threshold" | "overflow" | "idle"; action: "context-full" | "handoff" }
Expand All @@ -298,6 +300,7 @@ export type AgentSessionEvent =
errorMessage?: string;
/** True when compaction was skipped for a benign reason (no model, no candidates, nothing to compact). */
skipped?: boolean;
continuationSkipReason?: AutoCompactionContinuationSkipReason;
}
| {
type: "auto_retry_start";
Expand Down Expand Up @@ -1869,7 +1872,6 @@ export class AgentSession {
this.#resolveTtsrResume();
return;
}
this.#ttsrAbortPending = false;
this.#perToolTtsrInjections.clear();
const ttsrSettings = this.#ttsrManager?.getSettings();
if (ttsrSettings?.contextMode === "discard" && targetAssistantIndex !== -1) {
Expand Down Expand Up @@ -1898,11 +1900,22 @@ export class AgentSession {
);
this.#markTtsrInjected(details.rules);
}
try {
await this.agent.continue();
} catch {
this.#resolveTtsrResume();
}
await this.#scheduleAgentContinue({
delayMs: 0,
generation,
shouldContinue: () => {
this.#ttsrAbortPending = false;
return true;
},
onSkip: () => {
this.#ttsrAbortPending = false;
this.#resolveTtsrResume();
},
onError: () => {
this.#ttsrAbortPending = false;
this.#resolveTtsrResume();
},
});
},
{ delayMs: 50 },
);
Expand Down Expand Up @@ -2214,7 +2227,7 @@ export class AgentSession {
#schedulePostPromptTask(
task: (signal: AbortSignal) => Promise<void>,
options?: { delayMs?: number; generation?: number; onSkip?: () => void },
): void {
): Promise<void> {
const delayMs = options?.delayMs ?? 0;
const signal = this.#postPromptTasksAbortController.signal;
const scheduled = (async () => {
Expand All @@ -2236,18 +2249,20 @@ export class AgentSession {
await task(signal);
})();
this.#trackPostPromptTask(scheduled);
return scheduled;
}

#scheduleAgentContinue(options?: {
delayMs?: number;
generation?: number;
skipCompactionCheck?: boolean;
shouldContinue?: () => boolean;
onSkip?: (reason: "generation_changed" | "aborted_signal" | "queue_drained") => void;
onError?: (error: unknown) => void;
}): void {
}): Promise<void> {
const scheduledGeneration = options?.generation;
const signal = this.#postPromptTasksAbortController.signal;
this.#schedulePostPromptTask(
return this.#schedulePostPromptTask(
async () => {
if (signal.aborted) {
options?.onSkip?.("aborted_signal");
Expand All @@ -2263,6 +2278,9 @@ export class AgentSession {
}
try {
await this.#maybeRestoreRetryFallbackPrimary();
if (!options?.skipCompactionCheck) {
await this.#checkEstimatedContextBeforePrompt();
}
await this.agent.continue();
} catch (error) {
logger.warn("agent.continue failed after scheduling", {
Expand Down Expand Up @@ -2307,6 +2325,34 @@ export class AgentSession {
}
}

#detectOverflowRetryContinuationSkip(): AutoCompactionContinuationSkipReason | undefined {
this.#stripOverflowFailedTurnForRetry();
if (this.#isResumableAgentTail()) return undefined;
const compactionSettings = this.settings.getGroup("compaction");
return compactionSettings.autoContinue === false ? "auto_continue_disabled_non_resumable_tail" : undefined;
}

#scheduleOverflowRetryContinuation(generation: number): void {
this.#stripOverflowFailedTurnForRetry();
if (this.#isResumableAgentTail()) {
this.#scheduleAgentContinue({
delayMs: 100,
generation,
onSkip: reason => this.#logCompactionContinuationSkipped("overflow_retry", reason),
onError: error => this.#logCompactionContinuationError("overflow_retry", error),
});
return;
}

const compactionSettings = this.settings.getGroup("compaction");
if (compactionSettings.autoContinue !== false) {
this.#scheduleAutoContinuePrompt(generation);
return;
}

this.#logCompactionContinuationSkipped("overflow_retry", "auto_continue_disabled_non_resumable_tail");
}

#scheduleAutoContinuePrompt(generation: number): void {
const continuePrompt = async () => {
await this.#promptWithMessage(
Expand Down Expand Up @@ -3052,6 +3098,7 @@ export class AgentSession {
willRetry: event.willRetry,
errorMessage: event.errorMessage,
skipped: event.skipped,
continuationSkipReason: event.continuationSkipReason,
});
} else if (event.type === "auto_retry_start") {
await this.#extensionRunner.emit({
Expand Down Expand Up @@ -6722,6 +6769,8 @@ export class AgentSession {
const compactionSettings = this.settings.getGroup("compaction");
if (compactionSettings.enabled && compactionSettings.strategy !== "off") {
await this.#runAutoCompaction("overflow", true);
} else {
this.#scheduleOverflowRetryContinuation(generation);
}
return;
}
Expand Down Expand Up @@ -7734,32 +7783,18 @@ export class AgentSession {

const preparation = prepareCompaction(pathEntries, compactionSettings);
if (!preparation) {
const continuationSkipReason = willRetry ? this.#detectOverflowRetryContinuationSkip() : undefined;
await this.#emitSessionEvent({
type: "auto_compaction_end",
action,
result: undefined,
aborted: false,
willRetry: false,
willRetry: willRetry && !continuationSkipReason,
skipped: true,
continuationSkipReason,
});
if (willRetry) {
this.#stripOverflowFailedTurnForRetry();
if (this.#isResumableAgentTail()) {
this.#scheduleAgentContinue({
delayMs: 100,
generation,
onSkip: skipReason => this.#logCompactionContinuationSkipped("overflow_retry", skipReason),
onError: error => this.#logCompactionContinuationError("overflow_retry", error),
});
} else {
const tail = this.agent.state.messages.at(-1) as AssistantMessage | undefined;
logger.warn("Auto-compaction continuation skipped", {
source: "overflow_retry",
reason: "not_resumable_tail",
role: tail?.role,
stopReason: tail?.stopReason,
});
}
this.#scheduleOverflowRetryContinuation(generation);
} else if (continueAfterMaintenance && reason !== "idle" && this.agent.hasQueuedMessages()) {
this.#scheduleAgentContinue({
delayMs: 100,
Expand Down Expand Up @@ -7970,26 +8005,18 @@ export class AgentSession {
details,
preserveData,
};
await this.#emitSessionEvent({ type: "auto_compaction_end", action, result, aborted: false, willRetry });
const continuationSkipReason = willRetry ? this.#detectOverflowRetryContinuationSkip() : undefined;
await this.#emitSessionEvent({
type: "auto_compaction_end",
action,
result,
aborted: false,
willRetry: willRetry && !continuationSkipReason,
continuationSkipReason,
});

if (willRetry) {
this.#stripOverflowFailedTurnForRetry();
if (!this.#isResumableAgentTail()) {
const tail = this.agent.state.messages.at(-1) as AssistantMessage | undefined;
logger.warn("Auto-compaction continuation skipped", {
source: "overflow_retry",
reason: "not_resumable_tail",
role: tail?.role,
stopReason: tail?.stopReason,
});
} else {
this.#scheduleAgentContinue({
delayMs: 100,
generation,
onSkip: reason => this.#logCompactionContinuationSkipped("overflow_retry", reason),
onError: error => this.#logCompactionContinuationError("overflow_retry", error),
});
}
this.#scheduleOverflowRetryContinuation(generation);
} else if (continueAfterMaintenance && reason !== "idle" && this.agent.hasQueuedMessages()) {
// Auto-compaction can complete while follow-up/steering/custom messages are waiting.
// Kick the loop so queued messages are actually delivered.
Expand Down
Loading
Loading