Files
can1357 2f54e760c1 feat(coding-agent): added async compaction and in-place handoff
- Added compaction.asyncEnabled (Async Compaction, default on): when
  context enters the pre-threshold band [threshold - lead, threshold)
  with lead = clamp(threshold * 0.125, 8192, 32000), maintenance
  speculatively summarizes in the background off a branch snapshot
  (first configured LLM-backed method: remote, handoff, or soft) using
  a side session id isolated from the live turn. Crossing the threshold
  splices the armed result in instantly instead of blocking on a
  summarization round-trip. Armed results are invalidated by branch
  changes, reset boundaries, model switches that strand provider-native
  replay payloads, and context growth past keepRecentTokens (which
  re-speculates); extensions registering session_before_compact keep
  exact blocking semantics (speculation disabled).
- Reworked handoff to commit in place: /handoff and the auto handoff
  method now write the generated document as a regular compaction entry
  on the current session (summary = document + <files> tag, cut from
  prepareCompaction) instead of starting a new session. SessionHandoff
  shrank to a document generator; session_before_switch/session_switch
  no longer fire with reason "handoff"; mid-turn maintenance no longer
  suppresses the handoff preference; overflow recovery can apply an
  armed handoff result.
- Extracted the shared auto-compaction commit tail
  (#commitAutoCompactionResult / #commitCompactionEntry) used by the
  blocking production path, the armed speculative apply, manual
  compaction, and manual handoff.
- Status line pulses the auto-compact icon while a speculation runs and
  holds it in accent once a result is armed.
- Exported remotePreserveReusable from pi-agent-core/compaction for
  apply-time validation of speculative remote results.
2026-08-20 03:46:34 +02:00

90 lines
2.5 KiB
TypeScript

import { afterEach, beforeAll, describe, expect, it, vi } from "bun:test";
import type { SegmentContext } from "@oh-my-pi/pi-coding-agent/modes/components/status-line/segments";
import { renderSegment } from "@oh-my-pi/pi-coding-agent/modes/components/status-line/segments";
import { initTheme, theme } from "@oh-my-pi/pi-coding-agent/modes/theme/theme";
beforeAll(async () => {
await initTheme();
});
afterEach(() => {
vi.restoreAllMocks();
});
function createContext(loopMode: SegmentContext["loopMode"]): SegmentContext {
return {
session: {} as SegmentContext["session"],
width: 120,
compactThinkingLevel: false,
options: {},
planMode: null,
loopMode,
prewalk: null,
goalMode: null,
vibeMode: null,
collab: null,
usageStats: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
orchestrationInput: 0,
orchestrationOutput: 0,
orchestrationCacheRead: 0,
premiumRequests: 0,
cost: 0,
tokensPerSecond: null,
},
contextPercent: 0,
contextTokens: 0,
contextWindow: 0,
autoCompactEnabled: false,
compactionSpeculation: "idle",
speculationBlinkOn: true,
subagentCount: 0,
activeMs: 0,
activeRepo: null,
worktree: null,
git: { branch: null, status: null, pr: null },
usage: null,
};
}
function withIcon(icon: string, text: string): string {
return icon ? `${icon} ${text}` : text;
}
describe("status line loop mode segment", () => {
it("shows that a bounded loop is waiting for its first prompt", () => {
const rendered = renderSegment(
"mode",
createContext({ state: "waiting", limit: { kind: "iterations", initial: 10, remaining: 10 } }),
);
expect(Bun.stripANSI(rendered.content)).toBe(withIcon(theme.icon.loop, "Loop waiting 10/10"));
});
it("shows the live remaining duration while a loop is running", () => {
const now = Date.parse("2026-07-17T12:00:00Z");
vi.spyOn(Date, "now").mockReturnValue(now);
const rendered = renderSegment(
"mode",
createContext({
state: "running",
limit: { kind: "duration", durationMs: 90_000, deadlineMs: now + 90_000 },
}),
);
expect(Bun.stripANSI(rendered.content)).toBe(withIcon(theme.icon.loop, "Loop running 1m30s left"));
});
it("distinguishes a paused loop from an active loop", () => {
const rendered = renderSegment("mode", createContext({ state: "paused" }));
const icon = theme.icon.pause || theme.icon.loop;
expect(Bun.stripANSI(rendered.content)).toBe(withIcon(icon, "Loop paused"));
expect(rendered.content).toBe(theme.fg("warning", withIcon(icon, "Loop paused")));
});
});