shariq-pi-extensions 0.2.10 β†’ 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -33,6 +33,7 @@ The package contains:
33
33
  - persistent task goals
34
34
  - configurable steer, interrupt, or follow-up input behavior
35
35
  - dedicated multi-agent orchestration
36
+ - Smart Compaction with high-fidelity checkpointing, delta-merging, and custom model routing
36
37
  - Pi Memory
37
38
  - per-response TPS, TTFT, elapsed-time, and output status
38
39
  - Context Usage display
@@ -46,6 +46,12 @@ The Orchestration extension coordinates explicitly requested large tasks through
46
46
 
47
47
  The model-facing `create_orchestration` tool starts planning only after an explicit orchestration request. `get_orchestration` reports status without advancing work. Interrupted runs recover paused under `<agent-dir>/orchestration/`.
48
48
 
49
+ ### [Smart Compaction](../extensions/smart-compaction/README.md)
50
+
51
+ Replaces standard context compaction with a high-fidelity continuity engine. It intercepts `session_before_compact` events and synthesizes multi-turn conversations into structured checkpoint summaries capturing primary goals and negative constraints, progress ledgers (`Done`/`In Progress`/`Blocked`), verbatim code snippets for active/uncommitted edits, exact error root causes, architectural decisions, resume anchors, and deterministic `<read-files>`/`<modified-files>` metadata.
52
+
53
+ Successive compactions utilize an incremental Delta-Merge to eliminate context degradation over long sessions. `/compaction-model` selects any custom compaction model (e.g. `factory/gemini-3.7-flash`, `cursor/cursor-grok-4.5-fast`) or defaults to inheriting the active session model (`inherit`). `/smart-compaction` toggles or inspects compaction configuration stored in `<agent-dir>/smart-compaction.json`.
54
+
49
55
  ### [Background terminals](../extensions/background-terminals/README.md)
50
56
 
51
57
  Managed PTYs support servers, watchers, long builds, downloads, and interactive processes. The extension tracks up to eight concurrent terminals, retains bounded output, stores full logs in restrictive temporary directories, and stops process groups during shutdown or reload.
@@ -0,0 +1,49 @@
1
+ # Smart Compaction Extension
2
+
3
+ A high-fidelity context continuity synthesizer for Pi sessions that replaces standard compaction with an advanced multi-phase checkpoint engine.
4
+
5
+ ## Overview
6
+
7
+ When a coding agent session reaches context limits, standard compaction often degrades in subtle code nuances, drops uncommitted code snippets, or suffers from "telephone game" information loss across successive compactions.
8
+
9
+ **Smart Compaction** solves this by generating structured, high-density checkpoint summaries organized into 6 vital engineering dimensions:
10
+
11
+ 1. **🎯 Primary Goal & Nuanced Intent** β€” Retains full user objectives, styling preferences, scope boundaries, and explicit negative constraints.
12
+ 2. **πŸ“‹ Progress Ledger** β€” Strict `[x] Done`, `[ ] In Progress`, and `[!] Blocked` tracking.
13
+ 3. **πŸ› οΈ Code Changes & In-Progress Snippets** β€” Captures verbatim code snippets of active work and recent edits so a successor agent resumes without re-reading or guessing.
14
+ 4. **πŸ’₯ Errors, Root Causes & Fixes** β€” Full error traces, root cause diagnostics, and verified solutions.
15
+ 5. **🧠 Key Decisions & Hypotheses** β€” Architectural choices, trade-offs, and discarded hypotheses.
16
+ 6. **πŸ“ Resume Anchor & Immediate Next Action** β€” Verbatim quote or exact resume state with the single immediate next action.
17
+ 7. **πŸ“‚ Programmatic File Operations** β€” Append deterministic `<read-files>` and `<modified-files>` XML blocks extracted from tool calls.
18
+
19
+ ## Incremental Delta-Merging
20
+
21
+ When multiple compactions occur in a single long-running session, Smart Compaction utilizes a **Delta-Merge** pipeline that carries forward historical foundations while accumulating new progress, code modifications, and error solutionsβ€”eliminating context bleed over 5+ compaction cycles.
22
+
23
+ ## Model Selection
24
+
25
+ Smart Compaction can use the **active session model** (default: `inherit`) or any dedicated fast/cost-effective model (e.g. `factory/gemini-3.7-flash`, `antigravity/gemini-2.5-flash`, `cursor/cursor-grok-4.5-fast`).
26
+
27
+ ## Commands
28
+
29
+ - `/compaction-model` β€” Open interactive model picker to select the compaction model, or switch back to `inherit`.
30
+ - `/compaction-model <provider/model>` β€” Set a specific compaction model directly.
31
+ - `/smart-compaction` β€” View status and settings.
32
+ - `/smart-compaction enable | disable` β€” Toggle smart compaction on or off.
33
+
34
+ ## Configuration
35
+
36
+ Settings are persisted in `~/.pi/agent/smart-compaction.json`:
37
+
38
+ ```json
39
+ {
40
+ "version": 1,
41
+ "enabled": true,
42
+ "model": "inherit",
43
+ "thinkingLevel": "inherit"
44
+ }
45
+ ```
46
+
47
+ - `model`: `"inherit"` (uses current active session model) or explicit `"provider/model-id"`.
48
+ - `thinkingLevel`: `"inherit"` (uses current session's thinking level) or `"off" | "low" | "medium" | "high" | "max"`.
49
+ - `maxSummaryTokens`: optional override integer; if omitted, dynamically defaults to the model's full native output capacity (65,536–128,000+ tokens) so summaries are never artificially truncated.
@@ -0,0 +1,66 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import { getAgentDir } from "@earendil-works/pi-coding-agent";
4
+
5
+ export interface SmartCompactionConfig {
6
+ version: 1;
7
+ enabled: boolean;
8
+ model: string; // "inherit" or "provider/model-id"
9
+ thinkingLevel?: "inherit" | "off" | "low" | "medium" | "high" | "max";
10
+ maxSummaryTokens?: number; // optional override; defaults to model's full capacity
11
+ }
12
+
13
+ export const DEFAULT_SMART_COMPACTION_CONFIG: SmartCompactionConfig = {
14
+ version: 1,
15
+ enabled: true,
16
+ model: "inherit",
17
+ thinkingLevel: "inherit",
18
+ };
19
+
20
+ export function smartCompactionConfigPath(): string {
21
+ return path.join(getAgentDir(), "smart-compaction.json");
22
+ }
23
+
24
+ export function loadSmartCompactionConfig(file = smartCompactionConfigPath()): SmartCompactionConfig {
25
+ try {
26
+ if (!fs.existsSync(file)) return { ...DEFAULT_SMART_COMPACTION_CONFIG };
27
+ const raw = JSON.parse(fs.readFileSync(file, "utf8")) as Partial<SmartCompactionConfig>;
28
+ return {
29
+ version: 1,
30
+ enabled: typeof raw.enabled === "boolean" ? raw.enabled : DEFAULT_SMART_COMPACTION_CONFIG.enabled,
31
+ model: typeof raw.model === "string" && raw.model.trim() ? raw.model.trim() : DEFAULT_SMART_COMPACTION_CONFIG.model,
32
+ thinkingLevel: raw.thinkingLevel && ["inherit", "off", "low", "medium", "high", "max"].includes(raw.thinkingLevel)
33
+ ? (raw.thinkingLevel as SmartCompactionConfig["thinkingLevel"])
34
+ : DEFAULT_SMART_COMPACTION_CONFIG.thinkingLevel,
35
+ maxSummaryTokens: typeof raw.maxSummaryTokens === "number" && raw.maxSummaryTokens > 0
36
+ ? raw.maxSummaryTokens
37
+ : undefined,
38
+ };
39
+ } catch {
40
+ return { ...DEFAULT_SMART_COMPACTION_CONFIG };
41
+ }
42
+ }
43
+
44
+ export function saveSmartCompactionConfig(config: SmartCompactionConfig, file = smartCompactionConfigPath()): void {
45
+ const directory = path.dirname(file);
46
+ fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
47
+ const temporary = `${file}.${process.pid}.${Date.now()}.tmp`;
48
+ const document: SmartCompactionConfig = {
49
+ version: 1,
50
+ enabled: config.enabled,
51
+ model: config.model || "inherit",
52
+ thinkingLevel: config.thinkingLevel ?? "inherit",
53
+ maxSummaryTokens: config.maxSummaryTokens,
54
+ };
55
+ try {
56
+ fs.writeFileSync(temporary, `${JSON.stringify(document, null, 2)}\n`, { mode: 0o600 });
57
+ fs.renameSync(temporary, file);
58
+ fs.chmodSync(file, 0o600);
59
+ } finally {
60
+ try {
61
+ fs.rmSync(temporary, { force: true });
62
+ } catch {
63
+ // Best effort cleanup.
64
+ }
65
+ }
66
+ }
@@ -0,0 +1,150 @@
1
+ import { uuidv7, type Api, type Context, type Model, type Usage } from "@earendil-works/pi-ai";
2
+ import type { ExtensionContext, SessionBeforeCompactEvent } from "@earendil-works/pi-coding-agent";
3
+ import type { SmartCompactionConfig } from "./config.ts";
4
+ import {
5
+ formatFileOperationsXml,
6
+ SMART_COMPACTION_INITIAL_PROMPT,
7
+ SMART_COMPACTION_SYSTEM_PROMPT,
8
+ SMART_COMPACTION_UPDATE_PROMPT,
9
+ serializeConversationForCompaction,
10
+ } from "./prompt.ts";
11
+
12
+ export function resolveCompactionModel(
13
+ ctx: Pick<ExtensionContext, "model" | "modelRegistry">,
14
+ configuredModelString?: string,
15
+ ): { model: Model<Api>; isInherited: boolean } {
16
+ const trimmed = configuredModelString?.trim();
17
+ if (!trimmed || trimmed === "inherit") {
18
+ if (!ctx.model) {
19
+ throw new Error("No active session model available to inherit for compaction.");
20
+ }
21
+ return { model: ctx.model, isInherited: true };
22
+ }
23
+
24
+ // Parse "provider/model" or modelId
25
+ let candidate: Model<Api> | undefined;
26
+ if (trimmed.includes("/")) {
27
+ const [provider, ...rest] = trimmed.split("/");
28
+ candidate = ctx.modelRegistry.find(provider, rest.join("/"));
29
+ } else {
30
+ const available = ctx.modelRegistry.getAvailable();
31
+ candidate = available.find((m) => m.id === trimmed || `${m.provider}/${m.id}` === trimmed);
32
+ }
33
+
34
+ if (candidate) {
35
+ return { model: candidate, isInherited: false };
36
+ }
37
+
38
+ if (ctx.model) {
39
+ return { model: ctx.model, isInherited: true };
40
+ }
41
+
42
+ throw new Error(`Configured compaction model "${trimmed}" was not found in model registry.`);
43
+ }
44
+
45
+ export interface RunSmartCompactionOptions {
46
+ event: SessionBeforeCompactEvent;
47
+ ctx: Pick<ExtensionContext, "model" | "modelRegistry" | "thinkingLevel">;
48
+ config: SmartCompactionConfig;
49
+ }
50
+
51
+ export interface SmartCompactionOutput {
52
+ summary: string;
53
+ firstKeptEntryId: string;
54
+ tokensBefore: number;
55
+ usage?: Usage;
56
+ details?: Record<string, unknown>;
57
+ }
58
+
59
+ export async function runSmartCompaction(
60
+ options: RunSmartCompactionOptions,
61
+ ): Promise<SmartCompactionOutput> {
62
+ const { event, ctx, config } = options;
63
+ const { preparation, signal, customInstructions } = event;
64
+ signal?.throwIfAborted();
65
+
66
+ const { model, isInherited } = resolveCompactionModel(ctx, config.model);
67
+
68
+ const messagesToSummarize = [
69
+ ...(preparation.messagesToSummarize ?? []),
70
+ ...(preparation.turnPrefixMessages ?? []),
71
+ ];
72
+
73
+ // Serialize messages for the context summary
74
+ const conversationText = serializeConversationForCompaction(messagesToSummarize);
75
+
76
+ const previousSummary = preparation.previousSummary?.trim();
77
+ const baseInstruction = previousSummary ? SMART_COMPACTION_UPDATE_PROMPT : SMART_COMPACTION_INITIAL_PROMPT;
78
+
79
+ let promptContent = `<conversation>\n${conversationText}\n</conversation>\n\n`;
80
+ if (previousSummary) {
81
+ promptContent += `<previous-summary>\n${previousSummary}\n</previous-summary>\n\n`;
82
+ }
83
+ promptContent += baseInstruction;
84
+
85
+ if (customInstructions?.trim()) {
86
+ promptContent += `\n\n## Additional User Instructions:\n${customInstructions.trim()}`;
87
+ }
88
+
89
+ const context: Context = {
90
+ systemPrompt: SMART_COMPACTION_SYSTEM_PROMPT,
91
+ messages: [
92
+ {
93
+ role: "user",
94
+ content: [{ type: "text", text: promptContent }],
95
+ timestamp: Date.now(),
96
+ },
97
+ ],
98
+ };
99
+
100
+ const completeOptions: Record<string, unknown> = {
101
+ signal,
102
+ cacheRetention: "none",
103
+ sessionId: uuidv7(),
104
+ };
105
+
106
+ // If user explicitly configured a maxSummaryTokens override, pass it.
107
+ // Otherwise, omit maxTokens so the provider uses the model's full native maximum output capacity (e.g. 128k, 65k).
108
+ if (typeof config.maxSummaryTokens === "number" && config.maxSummaryTokens > 0) {
109
+ completeOptions.maxTokens = config.maxSummaryTokens;
110
+ }
111
+
112
+ // Resolve reasoning effort / thinking level
113
+ if (model.reasoning) {
114
+ const desiredThinking = config.thinkingLevel === "inherit" || !config.thinkingLevel
115
+ ? ctx.thinkingLevel
116
+ : config.thinkingLevel;
117
+
118
+ if (desiredThinking && desiredThinking !== "off") {
119
+ completeOptions.reasoning = desiredThinking;
120
+ }
121
+ }
122
+
123
+ const response = await ctx.modelRegistry.complete(model, context, completeOptions as any);
124
+ signal?.throwIfAborted();
125
+
126
+ const rawSummaryText = response.content
127
+ .filter((part): part is { type: "text"; text: string } => part.type === "text")
128
+ .map((part) => part.text)
129
+ .join("\n")
130
+ .trim();
131
+
132
+ if (!rawSummaryText) {
133
+ throw new Error("Compaction model returned an empty summary.");
134
+ }
135
+
136
+ const fileOpsXml = formatFileOperationsXml(preparation.fileOps);
137
+ const finalSummary = `${rawSummaryText}${fileOpsXml}`;
138
+
139
+ return {
140
+ summary: finalSummary,
141
+ firstKeptEntryId: preparation.firstKeptEntryId,
142
+ tokensBefore: preparation.tokensBefore,
143
+ usage: response.usage,
144
+ details: {
145
+ customCompactor: "smart-compaction",
146
+ model: `${model.provider}/${model.id}`,
147
+ isInherited,
148
+ },
149
+ };
150
+ }
@@ -0,0 +1,192 @@
1
+ import type {
2
+ ExtensionAPI,
3
+ ExtensionCommandContext,
4
+ ExtensionContext,
5
+ ExtensionUIContext,
6
+ SessionBeforeCompactEvent,
7
+ } from "@earendil-works/pi-coding-agent";
8
+ import {
9
+ loadSmartCompactionConfig,
10
+ saveSmartCompactionConfig,
11
+ type SmartCompactionConfig,
12
+ } from "./config.ts";
13
+ import { runSmartCompaction } from "./engine.ts";
14
+
15
+ const STATUS_KEY = "smart-compaction";
16
+
17
+ export interface SmartCompactionExtensionOptions {
18
+ configFile?: string;
19
+ }
20
+
21
+ export function createSmartCompactionExtension(options: SmartCompactionExtensionOptions = {}) {
22
+ return (pi: ExtensionAPI) => {
23
+ let config: SmartCompactionConfig = loadSmartCompactionConfig(options.configFile);
24
+ let ui: ExtensionUIContext | undefined;
25
+
26
+ const updateStatus = () => {
27
+ if (!ui) return;
28
+ if (!config.enabled) {
29
+ ui.setStatus(STATUS_KEY, undefined);
30
+ return;
31
+ }
32
+ const modelLabel = config.model === "inherit" ? "inherit" : config.model.split("/").pop() ?? config.model;
33
+ ui.setStatus(STATUS_KEY, `compact: ${modelLabel}`);
34
+ };
35
+
36
+ pi.on("session_start", (_event, ctx) => {
37
+ ui = ctx.ui;
38
+ updateStatus();
39
+ });
40
+
41
+ pi.on("session_before_compact", async (event: SessionBeforeCompactEvent, ctx: ExtensionContext) => {
42
+ if (!config.enabled) {
43
+ return undefined;
44
+ }
45
+
46
+ try {
47
+ const compaction = await runSmartCompaction({
48
+ event,
49
+ ctx,
50
+ config,
51
+ });
52
+ return { compaction };
53
+ } catch (error) {
54
+ if (event.signal.aborted) {
55
+ throw error;
56
+ }
57
+ const message = error instanceof Error ? error.message : String(error);
58
+ ctx.ui?.notify(`Smart Compaction failed: ${message}. Falling back to default compactor.`, "warning");
59
+ return undefined;
60
+ }
61
+ });
62
+
63
+ pi.on("session_compact", (event, ctx) => {
64
+ if (event.fromExtension) {
65
+ const details = event.compactionEntry.details as Record<string, unknown> | undefined;
66
+ if (details?.customCompactor === "smart-compaction") {
67
+ const model = String(details.model ?? "session model");
68
+ ctx.ui?.notify(`Smart Compaction completed (${model})`, "info");
69
+ }
70
+ }
71
+ });
72
+
73
+ // Slash command: /compaction-model
74
+ pi.registerCommand("compaction-model", {
75
+ description: "Select or view the model used for smart context compaction (default: inherit).",
76
+ handler: async (args: string, cmdCtx: ExtensionCommandContext) => {
77
+ const requested = args.trim();
78
+
79
+ if (requested) {
80
+ if (requested === "inherit") {
81
+ config.model = "inherit";
82
+ saveSmartCompactionConfig(config, options.configFile);
83
+ updateStatus();
84
+ cmdCtx.ui.notify("Compaction model set to: inherit (active session model)", "info");
85
+ return;
86
+ }
87
+
88
+ // Validate if model exists in registry
89
+ const available = cmdCtx.modelRegistry.getAvailable();
90
+ const match = available.find(
91
+ (m) => m.id === requested || `${m.provider}/${m.id}` === requested,
92
+ );
93
+
94
+ if (!match) {
95
+ cmdCtx.ui.notify(`Model "${requested}" not found in available models. Setting anyway.`, "warning");
96
+ }
97
+
98
+ config.model = match ? `${match.provider}/${match.id}` : requested;
99
+ saveSmartCompactionConfig(config, options.configFile);
100
+ updateStatus();
101
+ cmdCtx.ui.notify(`Compaction model set to: ${config.model}`, "info");
102
+ return;
103
+ }
104
+
105
+ if (cmdCtx.hasUI) {
106
+ const available = cmdCtx.modelRegistry.getAvailable();
107
+ const choices = [
108
+ `inherit (active session model: ${cmdCtx.model ? `${cmdCtx.model.provider}/${cmdCtx.model.id}` : "none"})`,
109
+ ...available.map((m) => `${m.provider}/${m.id}`),
110
+ ];
111
+
112
+ const selected = await cmdCtx.ui.select(
113
+ `Select Compaction Model (current: ${config.model})`,
114
+ choices,
115
+ );
116
+
117
+ if (!selected) return;
118
+
119
+ if (selected.startsWith("inherit")) {
120
+ config.model = "inherit";
121
+ } else {
122
+ config.model = selected;
123
+ }
124
+
125
+ saveSmartCompactionConfig(config, options.configFile);
126
+ updateStatus();
127
+ cmdCtx.ui.notify(`Compaction model set to: ${config.model}`, "info");
128
+ return;
129
+ }
130
+
131
+ cmdCtx.ui.notify(
132
+ `Compaction model: ${config.model}. Usage: /compaction-model [inherit|<provider/model>]`,
133
+ "info",
134
+ );
135
+ },
136
+ });
137
+
138
+ // Slash command: /smart-compaction
139
+ pi.registerCommand("smart-compaction", {
140
+ description: "Manage smart context compaction settings (enable/disable/status).",
141
+ handler: async (args: string, cmdCtx: ExtensionCommandContext) => {
142
+ const sub = args.trim().toLowerCase();
143
+ if (sub === "enable" || sub === "on") {
144
+ config.enabled = true;
145
+ saveSmartCompactionConfig(config, options.configFile);
146
+ updateStatus();
147
+ cmdCtx.ui.notify("Smart Compaction enabled.", "info");
148
+ return;
149
+ }
150
+ if (sub === "disable" || sub === "off") {
151
+ config.enabled = false;
152
+ saveSmartCompactionConfig(config, options.configFile);
153
+ updateStatus();
154
+ cmdCtx.ui.notify("Smart Compaction disabled (using default compactor).", "info");
155
+ return;
156
+ }
157
+ if (sub.startsWith("model ")) {
158
+ const target = args.trim().slice(6).trim();
159
+ config.model = target || "inherit";
160
+ saveSmartCompactionConfig(config, options.configFile);
161
+ updateStatus();
162
+ cmdCtx.ui.notify(`Smart Compaction model set to: ${config.model}`, "info");
163
+ return;
164
+ }
165
+
166
+ // Default status
167
+ const currentModelDesc = config.model === "inherit"
168
+ ? `inherit (${cmdCtx.model ? `${cmdCtx.model.provider}/${cmdCtx.model.id}` : "active session model"})`
169
+ : config.model;
170
+ const currentThinkingDesc = config.thinkingLevel === "inherit"
171
+ ? `inherit (${cmdCtx.thinkingLevel ?? "session default"})`
172
+ : (config.thinkingLevel ?? "inherit");
173
+ const maxTokensDesc = config.maxSummaryTokens ? `${config.maxSummaryTokens}` : "unlimited (full model output capacity)";
174
+
175
+ const status = [
176
+ `Smart Compaction: ${config.enabled ? "ENABLED" : "DISABLED"}`,
177
+ `Model: ${currentModelDesc}`,
178
+ `Thinking Level: ${currentThinkingDesc}`,
179
+ `Max Output Tokens: ${maxTokensDesc}`,
180
+ "",
181
+ "Commands:",
182
+ " /smart-compaction enable | disable",
183
+ " /compaction-model [inherit | <provider/model>]",
184
+ ].join("\n");
185
+
186
+ cmdCtx.ui.notify(status, "info");
187
+ },
188
+ });
189
+ };
190
+ }
191
+
192
+ export default createSmartCompactionExtension();
@@ -0,0 +1,194 @@
1
+ import type { AgentMessage } from "@earendil-works/pi-agent-core";
2
+
3
+ export const SMART_COMPACTION_SYSTEM_PROMPT = `You are a high-fidelity context continuity synthesizer for an autonomous coding agent.
4
+ Your task is to analyze the preceding conversation and produce a comprehensive, structured checkpoint summary.
5
+ The successor agent will rely SOLELY on your summary to resume complex engineering tasks without losing context, nuance, or mid-stream progress.
6
+
7
+ CRITICAL DIRECTIVES:
8
+ 1. Preserve exact file paths, shell commands, and error messages verbatim.
9
+ 2. Include actual code snippets for active work or uncommitted changesβ€”never just describe what code was changed.
10
+ 3. Explicitly maintain all user-stated negative constraints (e.g., "do not modify X", "never use Y").
11
+ 4. Do NOT execute tools or continue the conversation. Respond ONLY with the requested structured summary.`;
12
+
13
+ export const SMART_COMPACTION_INITIAL_PROMPT = `Analyze the conversation in the <conversation> tags above and produce a structured context checkpoint summary.
14
+
15
+ Use this EXACT format and include all numbered sections:
16
+
17
+ ## 1. Primary Goal & Nuanced Intent
18
+ - **Objective**: Detailed statement of what the user is trying to accomplish.
19
+ - **Constraints & Preferences**: All explicit user constraints, negative rules, styling conventions, and architectural boundaries (or "(none)").
20
+
21
+ ## 2. Progress Ledger
22
+ ### Done
23
+ - [x] [Completed task, file modification, or command]
24
+
25
+ ### In Progress
26
+ - [ ] [Active task or mid-stream operation]
27
+
28
+ ### Blocked / Open Issues
29
+ - [Any active errors, blockers, or pending decisions]
30
+
31
+ ## 3. Code Changes & In-Progress Snippets
32
+ For every modified, created, or in-flight file:
33
+ - **\`path/to/file\`**: State why it was changed and provide verbatim code snippets of the latest edits or new functions so work can resume immediately without re-reading.
34
+
35
+ ## 4. Errors, Root Causes & Fixes
36
+ - **Error**: [Verbatim error message or failed command output]
37
+ - **Root Cause**: [Exact reason for the failure]
38
+ - **Fix**: [How it was fixed or the approach currently being attempted]
39
+ (Or "None" if no errors occurred)
40
+
41
+ ## 5. Key Decisions & Hypotheses
42
+ - **[Decision / Architecture]**: [Rationale, alternatives considered, and discarded approaches]
43
+
44
+ ## 6. Resume Anchor & Immediate Next Action
45
+ - **Last State**: Precisely what was happening before this summary request.
46
+ - **Next Concrete Step**: The single immediate next action to take, directly aligned with the user's latest request.
47
+
48
+ Keep the prose economical and high-density. Do NOT pad with fluff.`;
49
+
50
+ export const SMART_COMPACTION_UPDATE_PROMPT = `The <conversation> tags above contain NEW conversation turns that occurred after the checkpoint in <previous-summary>.
51
+ Synthesize the new turns into the existing summary using a unified Delta-Merge.
52
+
53
+ DELTA-MERGING RULES:
54
+ 1. PRESERVE all historical goals, constraints, and decisions from <previous-summary>.
55
+ 2. UPDATE the Progress Ledger: check off items that have finished and add new in-flight tasks.
56
+ 3. ACCUMULATE Code Changes: add new code snippets for newly modified files while retaining existing relevant snippets.
57
+ 4. RECORD new errors, root causes, and resolutions encountered in the new turns.
58
+ 5. UPDATE the Resume Anchor and Next Step to reflect the current active frontier.
59
+ 6. PRESERVE exact file paths, commands, and code snippets verbatim.
60
+
61
+ Use this EXACT format:
62
+
63
+ ## 1. Primary Goal & Nuanced Intent
64
+ - **Objective**: [Preserve initial goal, add new objectives if scope expanded]
65
+ - **Constraints & Preferences**: [Preserve existing constraints, add newly stated ones]
66
+
67
+ ## 2. Progress Ledger
68
+ ### Done
69
+ - [x] [Previously completed items AND newly completed items]
70
+
71
+ ### In Progress
72
+ - [ ] [Current active tasks]
73
+
74
+ ### Blocked / Open Issues
75
+ - [Active blockers or "None"]
76
+
77
+ ## 3. Code Changes & In-Progress Snippets
78
+ [Accumulated modified/created files with verbatim code snippets of recent work]
79
+
80
+ ## 4. Errors, Root Causes & Fixes
81
+ [Accumulated errors, root causes, and fixes from the full session]
82
+
83
+ ## 5. Key Decisions & Hypotheses
84
+ [Accumulated architectural decisions and trade-offs]
85
+
86
+ ## 6. Resume Anchor & Immediate Next Action
87
+ - **Last State**: [Exact state immediately before this checkpoint]
88
+ - **Next Concrete Step**: [The single immediate next action]`;
89
+
90
+ const MAX_TOOL_RESULT_CHARS = 2500;
91
+
92
+ function truncateText(text: string, maxChars: number): string {
93
+ if (text.length <= maxChars) return text;
94
+ const remaining = text.length - maxChars;
95
+ return `${text.slice(0, maxChars)}\n\n[... ${remaining} characters truncated for summary ...]`;
96
+ }
97
+
98
+ function extractTextContent(content: unknown): string {
99
+ if (typeof content === "string") return content;
100
+ if (Array.isArray(content)) {
101
+ return content
102
+ .map((part) => {
103
+ if (typeof part === "string") return part;
104
+ if (part && typeof part === "object" && "text" in part && typeof part.text === "string") {
105
+ return part.text;
106
+ }
107
+ return "";
108
+ })
109
+ .filter(Boolean)
110
+ .join("\n");
111
+ }
112
+ return "";
113
+ }
114
+
115
+ export function serializeConversationForCompaction(messages: AgentMessage[]): string {
116
+ const parts: string[] = [];
117
+
118
+ for (const msg of messages) {
119
+ if (msg.role === "user") {
120
+ const text = extractTextContent((msg as any).content);
121
+ if (text) parts.push(`[User]:\n${text}`);
122
+ } else if (msg.role === "assistant") {
123
+ const content = (msg as any).content;
124
+ const thinkingBlocks: string[] = [];
125
+ const toolCallBlocks: string[] = [];
126
+ const textBlocks: string[] = [];
127
+
128
+ if (Array.isArray(content)) {
129
+ for (const block of content) {
130
+ if (!block || typeof block !== "object") continue;
131
+ if (block.type === "thinking" && typeof block.thinking === "string" && block.thinking.trim()) {
132
+ thinkingBlocks.push(block.thinking.trim());
133
+ } else if (block.type === "text" && typeof block.text === "string" && block.text.trim()) {
134
+ textBlocks.push(block.text.trim());
135
+ } else if (block.type === "toolCall") {
136
+ const args = block.arguments as Record<string, unknown>;
137
+ const formattedArgs = Object.entries(args ?? {})
138
+ .map(([k, v]) => `${k}=${JSON.stringify(v)}`)
139
+ .join(", ");
140
+ toolCallBlocks.push(`${block.name}(${formattedArgs})`);
141
+ }
142
+ }
143
+ } else if (typeof content === "string" && content.trim()) {
144
+ textBlocks.push(content.trim());
145
+ }
146
+
147
+ if (thinkingBlocks.length > 0) {
148
+ const combinedThinking = thinkingBlocks.join("\n");
149
+ parts.push(`[Assistant Thinking]:\n${truncateText(combinedThinking, 1500)}`);
150
+ }
151
+ if (textBlocks.length > 0) {
152
+ parts.push(`[Assistant]:\n${textBlocks.join("\n")}`);
153
+ }
154
+ if (toolCallBlocks.length > 0) {
155
+ parts.push(`[Assistant Tool Calls]:\n${toolCallBlocks.join("\n")}`);
156
+ }
157
+ } else if (msg.role === "toolResult") {
158
+ const text = extractTextContent((msg as any).content);
159
+ if (text) {
160
+ parts.push(`[Tool Result]:\n${truncateText(text, MAX_TOOL_RESULT_CHARS)}`);
161
+ }
162
+ } else if (msg.role === "custom") {
163
+ const text = extractTextContent((msg as any).content);
164
+ if (text) parts.push(`[System Event]:\n${text}`);
165
+ } else if (msg.role === "bashExecution") {
166
+ const cmd = (msg as any).command ?? "";
167
+ const out = (msg as any).output ?? "";
168
+ parts.push(`[Command Executed]:\n$ ${cmd}\n${truncateText(out, 1500)}`);
169
+ } else if (msg.role === "compactionSummary" || msg.role === "branchSummary") {
170
+ const summary = (msg as any).summary ?? "";
171
+ if (summary) parts.push(`[Prior Summary]:\n${summary}`);
172
+ }
173
+ }
174
+
175
+ return parts.join("\n\n---\n\n");
176
+ }
177
+
178
+ export function formatFileOperationsXml(fileOps?: { read?: Iterable<string>; written?: Iterable<string>; edited?: Iterable<string> }): string {
179
+ if (!fileOps) return "";
180
+ const readSet = new Set(fileOps.read ?? []);
181
+ const modifiedSet = new Set([...(fileOps.written ?? []), ...(fileOps.edited ?? [])]);
182
+ const readOnly = [...readSet].filter((f) => !modifiedSet.has(f)).sort();
183
+ const modified = [...modifiedSet].sort();
184
+
185
+ const sections: string[] = [];
186
+ if (readOnly.length > 0) {
187
+ sections.push(`<read-files>\n${readOnly.join("\n")}\n</read-files>`);
188
+ }
189
+ if (modified.length > 0) {
190
+ sections.push(`<modified-files>\n${modified.join("\n")}\n</modified-files>`);
191
+ }
192
+ if (sections.length === 0) return "";
193
+ return `\n\n${sections.join("\n\n")}`;
194
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shariq-pi-extensions",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "Cross-platform extension suite for the Pi coding agent.",
5
5
  "license": "MIT",
6
6
  "author": "Shariq Riaz",
@@ -52,6 +52,7 @@
52
52
  "./extensions/performance-status/index.ts",
53
53
  "./extensions/pi-memory/index.ts",
54
54
  "./extensions/shell-shortcuts/index.ts",
55
+ "./extensions/smart-compaction/index.ts",
55
56
  "./extensions/subagents/index.ts",
56
57
  "./extensions/web-fetch/index.ts"
57
58
  ],