Skip to content
Closed
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
17 changes: 16 additions & 1 deletion packages/coding-agent/src/core/agent-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4235,8 +4235,17 @@ export class AgentSession {

const authResult = await this._modelRuntime.getAuth(this.model);
if (!this._ownsCompactionController(autoCompactionController, "auto")) return false;
this._emit({ type: "compaction_start", reason });
if (!this._ownsCompactionController(autoCompactionController, "auto")) return false;
if (this.agent.streamFn === streamSimple && !authResult?.auth.apiKey) {
if (reason === "overflow") this._overflowRecoveryAttempted = false;
this._emit({
type: "compaction_end",
reason,
result: undefined,
aborted: false,
willRetry: false,
});
return false;
}

Expand All @@ -4247,10 +4256,16 @@ export class AgentSession {
);
if (!preparation) {
if (reason === "overflow") this._overflowRecoveryAttempted = false;
this._emit({
type: "compaction_end",
reason,
result: undefined,
aborted: false,
willRetry: false,
});
return false;
}
if (!this._ownsCompactionController(autoCompactionController, "auto")) return false;
this._emit({ type: "compaction_start", reason });

const execution = await this._executeCompaction({
controller: autoCompactionController,
Expand Down
2 changes: 2 additions & 0 deletions packages/coding-agent/src/core/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
controller at operation start, rejects stale completion/feedback, and retains the terminal result until another
operation begins. Feedback-only aborts publish one terminal event, and accepted completions publish their terminal
event before `session_compact` handlers can begin a fresh operation.
- Owned automatic compaction attempts start before authentication/preparation validation and end when preflight
rejects. Ownership is rechecked after start so synchronous listener supersession cannot publish a stale terminal.
- Durable append now rejects a generation whose message revision or agent-message snapshot changed during preparation
or summary generation (`stale-revision`), preserving intervening context without duplicate replay.
- Required compaction uses one provider-admission gate for normal prompts, extension-triggered turns, and every next
Expand Down
46 changes: 41 additions & 5 deletions packages/coding-agent/test/suite/agent-session-compaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,42 @@ describe("AgentSession compaction characterization", () => {
expect(getStreamCallCount()).toBe(1);
});

it("balances auto-compaction events when there is nothing to prepare", async () => {
const harness = await createHarness();
harnesses.push(harness);

await runAutoCompaction(harness.session, "threshold", false);

const compactionEvents = harness.events.filter(
(event) => event.type === "compaction_start" || event.type === "compaction_end",
);
expect(compactionEvents).toEqual([
{ type: "compaction_start", reason: "threshold" },
expect.objectContaining({
type: "compaction_end",
reason: "threshold",
result: undefined,
aborted: false,
willRetry: false,
}),
]);
});

it("does not publish a stale preflight end after a start listener aborts auto-compaction", async () => {
const harness = await createHarness({ withConfiguredAuth: false });
harnesses.push(harness);
harness.session.subscribe((event) => {
if (event.type === "compaction_start" && event.reason === "threshold") {
harness.session.abortCompaction();
}
});

await runAutoCompaction(harness.session, "threshold", false);

expect(harness.eventsOfType("compaction_start")).toEqual([{ type: "compaction_start", reason: "threshold" }]);
expect(harness.eventsOfType("compaction_end")).toHaveLength(0);
});

it("does not emit compaction events for a normal response below the threshold", async () => {
// given
const harness = await createHarness({
Expand Down Expand Up @@ -1353,12 +1389,12 @@ describe("AgentSession compaction characterization", () => {
await checkCompaction(harness.session, secondOverflow);

const overflowStarts = harness.eventsOfType("compaction_start").filter((event) => event.reason === "overflow");
const terminalOverflowFailures = harness
.eventsOfType("compaction_end")
.filter((event) =>
event.errorMessage?.startsWith("Context overflow recovery failed after one compact-and-retry attempt"),
);
const overflowEnds = harness.eventsOfType("compaction_end").filter((event) => event.reason === "overflow");
const terminalOverflowFailures = overflowEnds.filter((event) =>
event.errorMessage?.startsWith("Context overflow recovery failed after one compact-and-retry attempt"),
);
expect(overflowStarts).toHaveLength(2);
expect(overflowEnds).toHaveLength(2);
expect(terminalOverflowFailures).toHaveLength(0);
});

Expand Down