pi-vcc 0.4.0

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.
Files changed (75) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +120 -0
  3. package/demo.gif +0 -0
  4. package/flow/plans/20260515-1300/plan.md +206 -0
  5. package/index.ts +14 -0
  6. package/package.json +36 -0
  7. package/pi-vcc-config.schema.json +131 -0
  8. package/scripts/audit-sessions.ts +88 -0
  9. package/scripts/benchmark-real-sessions.ts +25 -0
  10. package/scripts/compare-before-after.ts +36 -0
  11. package/scripts/dump-branch-output.ts +20 -0
  12. package/src/commands/pi-vcc.ts +33 -0
  13. package/src/commands/vcc-recall.ts +65 -0
  14. package/src/core/brief.ts +381 -0
  15. package/src/core/build-sections.ts +87 -0
  16. package/src/core/content.ts +60 -0
  17. package/src/core/filter-noise.ts +42 -0
  18. package/src/core/format-recall.ts +27 -0
  19. package/src/core/format.ts +56 -0
  20. package/src/core/lineage.ts +26 -0
  21. package/src/core/load-messages.ts +63 -0
  22. package/src/core/normalize.ts +66 -0
  23. package/src/core/recall-scope.ts +14 -0
  24. package/src/core/render-entries.ts +68 -0
  25. package/src/core/report.ts +237 -0
  26. package/src/core/sanitize.ts +5 -0
  27. package/src/core/search-entries.ts +230 -0
  28. package/src/core/settings.ts +215 -0
  29. package/src/core/skill-collapse.ts +35 -0
  30. package/src/core/summarize.ts +159 -0
  31. package/src/core/tool-args.ts +14 -0
  32. package/src/details.ts +7 -0
  33. package/src/extract/commits.ts +69 -0
  34. package/src/extract/files.ts +80 -0
  35. package/src/extract/goals.ts +79 -0
  36. package/src/extract/preferences.ts +55 -0
  37. package/src/extract/references.ts +214 -0
  38. package/src/extract/signals.ts +145 -0
  39. package/src/hooks/before-compact.ts +405 -0
  40. package/src/sections.ts +14 -0
  41. package/src/tools/recall.ts +109 -0
  42. package/src/types.ts +14 -0
  43. package/tests/before-compact-hook.test.ts +181 -0
  44. package/tests/before-compact.test.ts +140 -0
  45. package/tests/brief.test.ts +206 -0
  46. package/tests/build-sections.test.ts +90 -0
  47. package/tests/compile.test.ts +110 -0
  48. package/tests/config-integration.test.ts +107 -0
  49. package/tests/content.test.ts +31 -0
  50. package/tests/edge-cases.test.ts +368 -0
  51. package/tests/extract-goals.test.ts +86 -0
  52. package/tests/extract-preferences.test.ts +30 -0
  53. package/tests/extract-references.test.ts +475 -0
  54. package/tests/extract-signals.test.ts +561 -0
  55. package/tests/filter-noise.test.ts +61 -0
  56. package/tests/fixtures.ts +61 -0
  57. package/tests/format-recall.test.ts +30 -0
  58. package/tests/format.test.ts +91 -0
  59. package/tests/lineage.test.ts +33 -0
  60. package/tests/load-messages.test.ts +51 -0
  61. package/tests/normalize.test.ts +97 -0
  62. package/tests/real-sessions.test.ts +38 -0
  63. package/tests/recall-expand.test.ts +15 -0
  64. package/tests/recall-scope.test.ts +32 -0
  65. package/tests/recall-tool-scope.test.ts +67 -0
  66. package/tests/render-entries.test.ts +62 -0
  67. package/tests/report.test.ts +44 -0
  68. package/tests/sanitize.test.ts +24 -0
  69. package/tests/search-entries.test.ts +144 -0
  70. package/tests/settings-scaffold.test.ts +120 -0
  71. package/tests/settings.test.ts +32 -0
  72. package/tests/support/load-session.ts +23 -0
  73. package/tests/support/real-sessions.ts +51 -0
  74. package/tsconfig.json +14 -0
  75. package/vitest.config.ts +7 -0
@@ -0,0 +1,42 @@
1
+ import type { NormalizedBlock } from "../types";
2
+
3
+ const NOISE_TOOLS = new Set([
4
+ "TodoWrite", "TodoRead", "ToolSearch", "WebSearch",
5
+ "AskUser", "ExitSpecMode", "GenerateDroid",
6
+ ]);
7
+
8
+ const NOISE_STRINGS = [
9
+ "Continue from where you left off.",
10
+ "No response requested.",
11
+ "IMPORTANT: TodoWrite was not called yet.",
12
+ ];
13
+
14
+ const XML_WRAPPER_RE = /<(system-reminder|ide_opened_file|command-message|context-window-usage)[^>]*>[\s\S]*?<\/\1>/g;
15
+
16
+ const isNoiseUserBlock = (text: string): boolean => {
17
+ const trimmed = text.trim();
18
+ if (NOISE_STRINGS.some((s) => trimmed.includes(s))) return true;
19
+ const stripped = trimmed.replace(XML_WRAPPER_RE, "").trim();
20
+ return stripped.length === 0;
21
+ };
22
+
23
+ const cleanUserText = (text: string): string =>
24
+ text.replace(XML_WRAPPER_RE, "").trim();
25
+
26
+ export const filterNoise = (blocks: NormalizedBlock[]): NormalizedBlock[] => {
27
+ const out: NormalizedBlock[] = [];
28
+ for (const b of blocks) {
29
+ if (b.kind === "thinking") continue;
30
+ if (b.kind === "tool_call" && NOISE_TOOLS.has(b.name)) continue;
31
+ if (b.kind === "tool_result" && NOISE_TOOLS.has(b.name)) continue;
32
+ if (b.kind === "user") {
33
+ if (isNoiseUserBlock(b.text)) continue;
34
+ const cleaned = cleanUserText(b.text);
35
+ if (!cleaned) continue;
36
+ out.push({ kind: "user", text: cleaned });
37
+ continue;
38
+ }
39
+ out.push(b);
40
+ }
41
+ return out;
42
+ };
@@ -0,0 +1,27 @@
1
+ import type { SearchHit } from "./search-entries";
2
+
3
+ export const formatRecallOutput = (
4
+ entries: SearchHit[],
5
+ query?: string,
6
+ headerOverride?: string,
7
+ ): string => {
8
+ if (entries.length === 0) {
9
+ return query
10
+ ? `No matches for "${query}" in session history.`
11
+ : "No entries in session history.";
12
+ }
13
+
14
+ const header = headerOverride
15
+ ? `${headerOverride} for "${query}":`
16
+ : query
17
+ ? `Found ${entries.length} matches for "${query}":`
18
+ : `Session history (${entries.length} entries):`;
19
+
20
+ const lines = entries.map((e) => {
21
+ const fileSuffix = e.files?.length ? ` files:[${e.files.join(", ")}]` : "";
22
+ const body = query && e.snippet ? e.snippet : e.summary;
23
+ return `#${e.index} [${e.role}]${fileSuffix} ${body}`;
24
+ });
25
+
26
+ return `${header}\n\n${lines.join("\n\n")}`;
27
+ };
@@ -0,0 +1,56 @@
1
+ import type { SectionData } from "../sections";
2
+
3
+ const section = (title: string, items: string[]): string => {
4
+ if (items.length === 0) return "";
5
+ const body = items.map((i) => `- ${i}`).join("\n");
6
+ return `[${title}]\n${body}`;
7
+ };
8
+
9
+ const BRIEF_MAX_LINES = 120;
10
+
11
+ export const capBrief = (text: string): string => {
12
+ const lines = text.split("\n");
13
+ if (lines.length <= BRIEF_MAX_LINES) return text;
14
+ const omitted = lines.length - BRIEF_MAX_LINES;
15
+ const kept = lines.slice(-BRIEF_MAX_LINES);
16
+ // Find first section header to avoid cutting mid-section
17
+ const firstHeader = kept.findIndex((l) => /^\[.+\]/.test(l));
18
+ const clean = firstHeader > 0 ? kept.slice(firstHeader) : kept;
19
+ return `...(${omitted} earlier lines omitted)\n\n${clean.join("\n")}`;
20
+ };
21
+
22
+ export const RECALL_NOTE =
23
+ "Use `vcc_recall` to search for prior work, decisions, and context from before this summary. " +
24
+ "Do not redo work already completed.";
25
+
26
+ export const formatSummary = (data: SectionData): string => {
27
+ const headerParts = [
28
+ section("Session Goal", data.sessionGoal),
29
+ section("Files And Changes", data.filesAndChanges),
30
+ section("Commits", data.commits),
31
+ section("References", data.references),
32
+ section("Key Signals", data.keySignals),
33
+ section("Outstanding Context", data.outstandingContext),
34
+ section("User Preferences", data.userPreferences),
35
+ ].filter(Boolean);
36
+
37
+ const parts: string[] = [];
38
+ if (headerParts.length > 0) {
39
+ parts.push(headerParts.join("\n\n"));
40
+ }
41
+ if (data.briefTranscript) {
42
+ parts.push(capBrief(data.briefTranscript));
43
+ }
44
+
45
+ if (parts.length === 0) return "";
46
+
47
+ // NOTE: RECALL_NOTE is intentionally NOT appended here.
48
+ // It is appended once by `compile()` at the very end, after merge-with-previous,
49
+ // to avoid the note compounding inside the brief transcript across compactions.
50
+ return parts.join("\n\n---\n\n");
51
+ };
52
+
53
+ export const formatTokens = (n: number): string => {
54
+ if (n >= 1000) return `${(n / 1000).toFixed(1)}k`;
55
+ return String(n);
56
+ };
@@ -0,0 +1,26 @@
1
+ export interface LineageEntryLike {
2
+ id?: string;
3
+ }
4
+
5
+ export interface LineageSessionManagerLike {
6
+ getBranch: () => LineageEntryLike[];
7
+ getEntries?: () => LineageEntryLike[];
8
+ }
9
+
10
+ export const getActiveLineageEntryIds = (sessionManager: LineageSessionManagerLike): Set<string> => {
11
+ try {
12
+ const branch = sessionManager.getBranch() ?? [];
13
+ if (branch.length > 0) {
14
+ return new Set(branch.map((e) => e.id).filter((id): id is string => Boolean(id)));
15
+ }
16
+ } catch {
17
+ // fall through to defensive fallback
18
+ }
19
+
20
+ try {
21
+ const all = sessionManager.getEntries?.() ?? [];
22
+ return new Set(all.map((e) => e.id).filter((id): id is string => Boolean(id)));
23
+ } catch {
24
+ return new Set();
25
+ }
26
+ };
@@ -0,0 +1,63 @@
1
+ import { readFile } from "fs/promises";
2
+ import type { Message } from "@mariozechner/pi-ai";
3
+ import { renderMessage, type RenderedEntry } from "./render-entries";
4
+
5
+ export interface ParseFailure {
6
+ line: number;
7
+ error: string;
8
+ preview: string;
9
+ }
10
+
11
+ export interface LoadedMessages {
12
+ rendered: RenderedEntry[];
13
+ rawMessages: Message[];
14
+ entryIds: string[];
15
+ parseFailures: ParseFailure[];
16
+ }
17
+
18
+ export const loadAllMessages = async (
19
+ sessionFile: string,
20
+ full: boolean,
21
+ allowedEntryIds?: Set<string>,
22
+ ): Promise<LoadedMessages> => {
23
+ const content = await readFile(sessionFile, "utf-8");
24
+ const entries: any[] = [];
25
+ const parseFailures: ParseFailure[] = [];
26
+ let lineNum = 0;
27
+ for (const line of content.split("\n")) {
28
+ lineNum++;
29
+ if (!line.trim()) continue;
30
+ try {
31
+ entries.push(JSON.parse(line));
32
+ } catch (e) {
33
+ parseFailures.push({ line: lineNum, error: (e as Error).message, preview: line.slice(0, 80) });
34
+ }
35
+ }
36
+
37
+ if (parseFailures.length > 0) {
38
+ console.warn(`[pi-vcc] Found ${parseFailures.length} JSONL parsing failures in ${sessionFile}:`);
39
+ for (const failure of parseFailures) {
40
+ console.warn(` - Line ${failure.line}: ${failure.error} (starts with: "${failure.preview}...")`);
41
+ }
42
+ }
43
+
44
+ const rendered: RenderedEntry[] = [];
45
+ const rawMessages: Message[] = [];
46
+ const entryIds: string[] = [];
47
+
48
+ let messageIndex = 0;
49
+ for (const e of entries) {
50
+ const isMessage = e.type === "message" && e.message;
51
+ if (!isMessage) continue;
52
+
53
+ const allowed = !allowedEntryIds || allowedEntryIds.has(e.id);
54
+ if (allowed) {
55
+ rendered.push(renderMessage(e.message, messageIndex, full));
56
+ rawMessages.push(e.message);
57
+ entryIds.push(String(e.id));
58
+ }
59
+ messageIndex++;
60
+ }
61
+
62
+ return { rendered, rawMessages, entryIds, parseFailures };
63
+ };
@@ -0,0 +1,66 @@
1
+ import type { Message } from "@mariozechner/pi-ai";
2
+ import type { NormalizedBlock } from "../types";
3
+ import { textOf } from "./content";
4
+ import { sanitize } from "./sanitize";
5
+
6
+ const normalizeOne = (msg: Message, msgIndex: number): NormalizedBlock[] => {
7
+ if (msg.role === "user") {
8
+ const blocks: NormalizedBlock[] = [];
9
+ const text = sanitize(textOf(msg.content));
10
+ if (text) blocks.push({ kind: "user", text, sourceIndex: msgIndex });
11
+ if (msg.content && typeof msg.content !== "string") {
12
+ for (const part of msg.content) {
13
+ if (part.type === "image") {
14
+ blocks.push({ kind: "user", text: `[image: ${part.mimeType}]`, sourceIndex: msgIndex });
15
+ }
16
+ }
17
+ }
18
+ return blocks.length > 0 ? blocks : [{ kind: "user", text: "", sourceIndex: msgIndex }];
19
+ }
20
+
21
+ if (msg.role === "toolResult") {
22
+ return [{
23
+ kind: "tool_result",
24
+ name: msg.toolName,
25
+ text: sanitize(textOf(msg.content)),
26
+ isError: msg.isError,
27
+ sourceIndex: msgIndex,
28
+ }];
29
+ }
30
+
31
+ if (msg.role === "assistant") {
32
+ if (!msg.content) return [];
33
+ if (typeof msg.content === "string") {
34
+ return [{ kind: "assistant", text: sanitize(msg.content), sourceIndex: msgIndex }];
35
+ }
36
+
37
+ const blocks: NormalizedBlock[] = [];
38
+ for (const part of msg.content) {
39
+ if (part.type === "text") {
40
+ blocks.push({ kind: "assistant", text: sanitize(part.text), sourceIndex: msgIndex });
41
+ } else if (part.type === "thinking") {
42
+ blocks.push({
43
+ kind: "thinking",
44
+ text: sanitize(part.thinking),
45
+ redacted: part.redacted ?? false,
46
+ sourceIndex: msgIndex,
47
+ });
48
+ } else if (part.type === "toolCall") {
49
+ blocks.push({
50
+ kind: "tool_call",
51
+ name: part.name,
52
+ args: part.arguments,
53
+ sourceIndex: msgIndex,
54
+ });
55
+ }
56
+ }
57
+ return blocks;
58
+ }
59
+
60
+ return [];
61
+ };
62
+
63
+ export const normalize = (messages: Message[]): NormalizedBlock[] =>
64
+ messages.flatMap((msg, i) => normalizeOne(msg, i));
65
+
66
+
@@ -0,0 +1,14 @@
1
+ export type RecallScope = "lineage" | "all";
2
+
3
+ const SCOPE_RE = /\bscope:(lineage|all)\b/i;
4
+
5
+ export const normalizeRecallScope = (scope?: unknown): RecallScope =>
6
+ typeof scope === "string" && scope.toLowerCase() === "all" ? "all" : "lineage";
7
+
8
+ export const parseRecallScope = (text: string): { scope: RecallScope; text: string } => {
9
+ const match = text.match(SCOPE_RE);
10
+ return {
11
+ scope: normalizeRecallScope(match?.[1]),
12
+ text: text.replace(SCOPE_RE, "").replace(/\s+/g, " ").trim(),
13
+ };
14
+ };
@@ -0,0 +1,68 @@
1
+ import type { Message } from "@mariozechner/pi-ai";
2
+ import { clip, textOf } from "./content";
3
+ import { summarizeToolArgs } from "./tool-args";
4
+ import { extractPath } from "./tool-args";
5
+
6
+ interface BashExecutionMessage {
7
+ role: "bashExecution";
8
+ command?: string;
9
+ output?: string;
10
+ }
11
+
12
+ function isBashExec(m: Message | BashExecutionMessage): m is BashExecutionMessage {
13
+ return typeof m === "object" && m !== null && (m as unknown as Record<string, unknown>).role === "bashExecution";
14
+ }
15
+
16
+ export interface RenderedEntry {
17
+ index: number;
18
+ role:string;
19
+ summary: string;
20
+ files?: string[];
21
+ }
22
+
23
+ const toolCalls = (content: Message["content"]): string => {
24
+ if (!content || typeof content === "string") return "";
25
+ return content
26
+ .filter((c) => c.type === "toolCall")
27
+ .map((c) => `${c.name}(${summarizeToolArgs(c.arguments)})`)
28
+ .join(", ");
29
+ };
30
+
31
+ const extractFilesFromContent = (content: Message["content"]): string[] => {
32
+ if (!content || typeof content === "string") return [];
33
+ return content
34
+ .filter((c) => c.type === "toolCall")
35
+ .map((c) => extractPath(c.arguments))
36
+ .filter((p): p is string => p !== null);
37
+ };
38
+
39
+ export const renderMessage = (msg: Message | BashExecutionMessage, index: number, full = false): RenderedEntry => {
40
+ if (isBashExec(msg)) {
41
+ const cmd = msg.command ?? "";
42
+ const out = msg.output ?? "";
43
+ const text = full ? `$ ${cmd}\n${out}` : clip(`$ ${cmd}\n${out}`, 300);
44
+ return { index, role: "bash", summary: text };
45
+ }
46
+
47
+ const typedMsg = msg as Message;
48
+
49
+ if (typedMsg.role === "user") {
50
+ return { index, role: "user", summary: full ? textOf(typedMsg.content) : clip(textOf(typedMsg.content), 300) };
51
+ }
52
+ if (typedMsg.role === "toolResult") {
53
+ const prefix = typedMsg.isError ? "ERROR " : "";
54
+ const text = full ? textOf(typedMsg.content) : clip(textOf(typedMsg.content), 200);
55
+ return {
56
+ index, role: "tool_result",
57
+ summary: `${prefix}[${typedMsg.toolName}] ${text}`,
58
+ };
59
+ }
60
+
61
+ const text = full ? textOf(typedMsg.content) : clip(textOf(typedMsg.content), 300);
62
+ const tools = toolCalls(typedMsg.content);
63
+ const files = extractFilesFromContent(typedMsg.content);
64
+ const summary = tools ? `${tools}\n${text}` : text;
65
+ return { index, role: "assistant", summary, ...(files.length > 0 && { files }) };
66
+ };
67
+
68
+
@@ -0,0 +1,237 @@
1
+ import type { Message } from "@mariozechner/pi-ai";
2
+ import { buildSections } from "./build-sections";
3
+ import { clip } from "./content";
4
+ import { normalize } from "./normalize";
5
+ import { renderMessage } from "./render-entries";
6
+ import { searchEntries } from "./search-entries";
7
+ import { type CompileInput, compile } from "./summarize";
8
+
9
+ const SECTION_HEADERS = ["Session Goal", "Files And Changes", "Commits", "Outstanding Context"];
10
+
11
+ interface RoleCounts {
12
+ user: number;
13
+ assistant: number;
14
+ toolResult: number;
15
+ }
16
+
17
+ interface BlockCounts {
18
+ user: number;
19
+ assistant: number;
20
+ toolCalls: number;
21
+ toolResults: number;
22
+ thinking: number;
23
+ }
24
+
25
+ export interface RecallProbe {
26
+ label: string;
27
+ sourceText: string;
28
+ query: string;
29
+ summaryMentioned: boolean;
30
+ recallHits: number;
31
+ }
32
+
33
+ export interface CompactReport {
34
+ summary: string;
35
+ before: {
36
+ messageCount: number;
37
+ roleCounts: RoleCounts;
38
+ blockCounts: BlockCounts;
39
+ inputChars: number;
40
+ estimatedTokens: number;
41
+ topFiles: string[];
42
+ preview: string;
43
+ };
44
+ after: {
45
+ summaryLength: number;
46
+ estimatedTokens: number;
47
+ sectionCount: number;
48
+ summaryPreview: string;
49
+ goalsCount: number;
50
+ blockersCount: number;
51
+ briefTranscriptLines: number;
52
+ };
53
+ compression: {
54
+ charsBefore: number;
55
+ charsAfter: number;
56
+ ratio: number;
57
+ messagesBefore: number;
58
+ };
59
+ recall: {
60
+ probes: RecallProbe[];
61
+ };
62
+ }
63
+
64
+ const estimateTokensFromChars = (chars: number): number =>
65
+ Math.ceil(chars / 4);
66
+
67
+ const countRoles = (messages: Message[]): RoleCounts => {
68
+ const counts: RoleCounts = { user: 0, assistant: 0, toolResult: 0 };
69
+ for (const msg of messages) {
70
+ if (msg.role === "user") counts.user += 1;
71
+ else if (msg.role === "assistant") counts.assistant += 1;
72
+ else if (msg.role === "toolResult") counts.toolResult += 1;
73
+ }
74
+ return counts;
75
+ };
76
+
77
+ const countBlocks = (messages: Message[]): BlockCounts => {
78
+ const counts: BlockCounts = {
79
+ user: 0,
80
+ assistant: 0,
81
+ toolCalls: 0,
82
+ toolResults: 0,
83
+ thinking: 0,
84
+ };
85
+
86
+ for (const block of normalize(messages)) {
87
+ if (block.kind === "user") counts.user += 1;
88
+ else if (block.kind === "assistant") counts.assistant += 1;
89
+ else if (block.kind === "tool_call") counts.toolCalls += 1;
90
+ else if (block.kind === "tool_result") counts.toolResults += 1;
91
+ else if (block.kind === "thinking") counts.thinking += 1;
92
+ }
93
+
94
+ return counts;
95
+ };
96
+
97
+ const inputCharsOf = (messages: Message[]): number =>
98
+ messages
99
+ .map((msg, index) => renderMessage(msg, index, true).summary.length)
100
+ .reduce((sum, len) => sum + len, 0);
101
+
102
+ const topFilesOf = (messages: Message[]): string[] => {
103
+ const files = new Set<string>();
104
+ for (const block of normalize(messages)) {
105
+ if (block.kind === "tool_call") {
106
+ for (const key of ["path", "file_path", "filePath", "file"]) {
107
+ const val = block.args[key];
108
+ if (typeof val === "string") { files.add(val); break; }
109
+ }
110
+ }
111
+ }
112
+ return [...files].slice(0, 10);
113
+ };
114
+
115
+ const previewOf = (messages: Message[], edgeCount = 3): string => {
116
+ const rendered = messages.map((msg, index) => renderMessage(msg, index));
117
+ if (rendered.length === 0) return "(empty)";
118
+ if (rendered.length <= edgeCount * 2) {
119
+ return rendered
120
+ .map((entry) => `#${entry.index} [${entry.role}] ${clip(entry.summary, 220)}`)
121
+ .join("\n");
122
+ }
123
+
124
+ const first = rendered.slice(0, edgeCount);
125
+ const last = rendered.slice(-edgeCount);
126
+ return [
127
+ ...first.map((entry) => `#${entry.index} [${entry.role}] ${clip(entry.summary, 220)}`),
128
+ "...",
129
+ ...last.map((entry) => `#${entry.index} [${entry.role}] ${clip(entry.summary, 220)}`),
130
+ ].join("\n");
131
+ };
132
+
133
+ const sectionCountOf = (summary: string): number =>
134
+ SECTION_HEADERS.filter((header) => summary.includes(`[${header}]`)).length;
135
+
136
+ const briefLineCountOf = (summary: string): number => {
137
+ const sep = "\n\n---\n\n";
138
+ const idx = summary.indexOf(sep);
139
+ if (idx < 0) return 0;
140
+ return summary.slice(idx + sep.length).split("\n").length;
141
+ };
142
+
143
+ const queryTermsOf = (text: string): string[] =>
144
+ (text.match(/[\p{L}\p{N}_./-]{3,}/gu) ?? [])
145
+ .map((part) => part.trim())
146
+ .filter(Boolean);
147
+
148
+ const queryOf = (text: string): string => {
149
+ const terms = queryTermsOf(text);
150
+ return terms.slice(0, 6).join(" ");
151
+ };
152
+
153
+ const matchesQuery = (text: string, query: string): boolean => {
154
+ const hay = text.toLowerCase();
155
+ return query
156
+ .toLowerCase()
157
+ .split(/\s+/)
158
+ .filter(Boolean)
159
+ .every((term) => hay.includes(term));
160
+ };
161
+
162
+ const probesOf = (messages: Message[], summary: string): RecallProbe[] => {
163
+ const blocks = normalize(messages);
164
+ const data = buildSections({ blocks });
165
+
166
+ // Find first file from tool calls
167
+ let firstFile = "";
168
+ for (const b of blocks) {
169
+ if (b.kind === "tool_call") {
170
+ for (const key of ["path", "file_path", "filePath", "file"]) {
171
+ if (typeof b.args[key] === "string") { firstFile = b.args[key] as string; break; }
172
+ }
173
+ if (firstFile) break;
174
+ }
175
+ }
176
+
177
+ const rawProbes = [
178
+ { label: "goal", text: data.sessionGoal[0] ?? "" },
179
+ { label: "file", text: firstFile },
180
+ { label: "problem", text: data.outstandingContext[0] ?? "" },
181
+ ];
182
+
183
+ const rendered = messages.map((msg, index) => renderMessage(msg, index));
184
+
185
+ return rawProbes
186
+ .map(({ label, text }) => {
187
+ const sourceText = text.trim();
188
+ const query = queryOf(sourceText);
189
+ if (!query) return null;
190
+ return {
191
+ label,
192
+ sourceText,
193
+ query,
194
+ summaryMentioned: matchesQuery(summary, query),
195
+ recallHits: searchEntries(rendered, messages, query).length,
196
+ };
197
+ })
198
+ .filter((probe): probe is RecallProbe => probe !== null);
199
+ };
200
+
201
+ export const buildCompactReport = async (input: CompileInput): Promise<CompactReport> => {
202
+ const summary = await compile(input);
203
+ const data = buildSections({ blocks: normalize(input.messages) });
204
+ const inputChars = inputCharsOf(input.messages);
205
+ const topFiles = topFilesOf(input.messages);
206
+
207
+ return {
208
+ summary,
209
+ before: {
210
+ messageCount: input.messages.length,
211
+ roleCounts: countRoles(input.messages),
212
+ blockCounts: countBlocks(input.messages),
213
+ inputChars,
214
+ estimatedTokens: estimateTokensFromChars(inputChars),
215
+ topFiles,
216
+ preview: previewOf(input.messages),
217
+ },
218
+ after: {
219
+ summaryLength: summary.length,
220
+ estimatedTokens: estimateTokensFromChars(summary.length),
221
+ sectionCount: sectionCountOf(summary),
222
+ summaryPreview: summary,
223
+ goalsCount: data.sessionGoal.length,
224
+ blockersCount: data.outstandingContext.length,
225
+ briefTranscriptLines: briefLineCountOf(summary),
226
+ },
227
+ compression: {
228
+ charsBefore: inputChars,
229
+ charsAfter: summary.length,
230
+ ratio: summary.length === 0 ? 0 : Number((inputChars / summary.length).toFixed(2)),
231
+ messagesBefore: input.messages.length,
232
+ },
233
+ recall: {
234
+ probes: probesOf(input.messages, summary),
235
+ },
236
+ };
237
+ };
@@ -0,0 +1,5 @@
1
+ const ANSI_RE = /\x1b\[[0-9;]*[A-Za-z]/g;
2
+ const CTRL_RE = /[\x00-\x08\x0b\x0c\x0e-\x1f]/g;
3
+
4
+ export const sanitize = (text: string): string =>
5
+ text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(ANSI_RE, "").replace(CTRL_RE, "");