Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

merge: resolve conflict with main branch

Jiang Bohan6a21a5176666
diff --cc src/agent/events.ts
index 0000000000,6177b9127b..8eb8b422ba
mode 000000,100644..100644
--- a/src/agent/events.ts
+++ b/src/agent/events.ts
@@@ -1,0 -1,30 +1,30 @@@
+ /**
+ * Super Multica custom events (parallel to pi-agent-core's AgentEvent)
+ *
+ * These events extend the agent's event system with Multica-specific
+ * lifecycle events that pi-agent-core does not provide.
+ */
+
+ /** Emitted when context compaction begins */
+ export type CompactionStartEvent = {
+ type: "compaction_start";
+ };
+
+ /**
+ * Emitted when context compaction completes.
+ *
+ * Note: `reason` uses a narrow union here for type safety within the agent.
+ * The SDK's `CompactionEndEvent` uses `string` to allow future extensions
+ * without requiring SDK version bumps.
+ */
+ export type CompactionEndEvent = {
+ type: "compaction_end";
+ removed: number;
+ kept: number;
+ tokensRemoved?: number;
+ tokensKept?: number;
- reason: "count" | "tokens" | "summary";
++ reason: "count" | "tokens" | "summary" | "pruning";
+ };
+
+ /** Union of all Multica-specific events */
+ export type MulticaEvent = CompactionStartEvent | CompactionEndEvent;
diff --cc src/agent/session/session-manager.ts
index 2c8d78d4f7,bb55721aba..eece48b1ff
--- a/src/agent/session/session-manager.ts
+++ b/src/agent/session/session-manager.ts
@@@ -2,7 -2,8 +2,8 @@@ import type { AgentMessage } from "@mar
import { getModel, type Model } from "@mariozechner/pi-ai";
import type { SessionEntry, SessionMeta } from "./types.js";
import { appendEntry, readEntries, resolveSessionPath, writeEntries } from "./storage.js";
-import { compactMessages, compactMessagesAsync } from "./compaction.js";
+import { compactMessages, compactMessagesAsync, type CompactionResult } from "./compaction.js";
+ import { estimateTokenUsage, shouldCompact as shouldCompactTokens } from "../context-window/index.js";
import { credentialManager } from "../credentials.js";
import { repairSessionFileIfNeeded, type RepairReport } from "./session-file-repair.js";
import { sanitizeToolCallInputs, sanitizeToolUseResultPairing } from "./session-transcript-repair.js";
@@@ -212,33 -194,22 +213,48 @@@ export class SessionManager
);
}
+ /** Check whether compaction would trigger for the given messages (without executing it) */
+ needsCompaction(messages: AgentMessage[]): boolean {
+ if (this.compactionMode === "count") {
+ return messages.length > this.maxMessages;
+ }
+ // Token and summary modes use the same token-based threshold
+ const estimation = estimateTokenUsage({
+ messages,
+ systemPrompt: this.systemPrompt,
+ contextWindowTokens: this.contextWindowTokens,
+ reserveTokens: this.reserveTokens,
+ });
+ return shouldCompactTokens(estimation);
+ }
+
- async maybeCompact(messages: AgentMessage[]) {
+ async maybeCompact(messages: AgentMessage[]): Promise<CompactionResult | null> {
+ let workingMessages = messages;
+ let toolResultPruningApplied = false;
+
+ // Phase 1: Tool result pruning (soft trim / hard clear)
+ // This reduces token usage without removing messages
+ if (this.enableToolResultPruning) {
+ const pruneResult = pruneToolResults({
+ messages: workingMessages,
+ contextWindowTokens: this.contextWindowTokens,
+ settings: this.toolResultPruning,
+ });
+
+ if (pruneResult.changed) {
+ workingMessages = pruneResult.messages;
+ toolResultPruningApplied = true;
+ // Log pruning stats
+ if (pruneResult.softTrimmed > 0 || pruneResult.hardCleared > 0) {
+ console.error(
+ `[SessionManager] Tool result pruning: ${pruneResult.softTrimmed} soft-trimmed, ` +
+ `${pruneResult.hardCleared} hard-cleared, ~${Math.round(pruneResult.charsSaved / 1000)}k chars saved`,
+ );
+ }
+ }
+ }
+
+ // Phase 2: Message compaction (remove old messages if still needed)
let result;
if (this.compactionMode === "summary") {