@monotykamary/pi-vcc 0.4.0 → 0.5.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 (49) hide show
  1. package/npm-shrinkwrap.json +2 -2
  2. package/package.json +1 -1
  3. package/src/core/brief.ts +245 -1
  4. package/src/core/causal-keys.ts +56 -0
  5. package/src/core/summarize.ts +53 -3
  6. package/__tests__/helpers.ts +0 -135
  7. package/__tests__/root-causes.test.ts +0 -858
  8. package/demo.gif +0 -0
  9. package/knip.json +0 -4
  10. package/scripts/audit-sessions.ts +0 -88
  11. package/scripts/benchmark-real-sessions.ts +0 -25
  12. package/scripts/compare-before-after.ts +0 -36
  13. package/scripts/dump-branch-output.ts +0 -20
  14. package/stress-debug.ts +0 -202
  15. package/stress-degradation-curve.ts +0 -251
  16. package/tests/australian-government.test.ts +0 -479
  17. package/tests/before-compact-hook.test.ts +0 -185
  18. package/tests/before-compact.test.ts +0 -227
  19. package/tests/breadcrumbs.test.ts +0 -312
  20. package/tests/brief.test.ts +0 -215
  21. package/tests/build-sections.test.ts +0 -59
  22. package/tests/compile.test.ts +0 -89
  23. package/tests/content.test.ts +0 -66
  24. package/tests/deep-extraction.test.ts +0 -322
  25. package/tests/extract-goals.test.ts +0 -86
  26. package/tests/extract-preferences.test.ts +0 -30
  27. package/tests/filter-noise.test.ts +0 -61
  28. package/tests/fixtures.ts +0 -61
  29. package/tests/format-recall.test.ts +0 -87
  30. package/tests/format.test.ts +0 -73
  31. package/tests/government-domain.test.ts +0 -542
  32. package/tests/lineage.test.ts +0 -33
  33. package/tests/load-messages.test.ts +0 -49
  34. package/tests/model-threshold-hook.test.ts +0 -311
  35. package/tests/model-threshold.test.ts +0 -223
  36. package/tests/normalize.test.ts +0 -104
  37. package/tests/proactive-threshold.test.ts +0 -490
  38. package/tests/real-sessions.test.ts +0 -38
  39. package/tests/recall-expand.test.ts +0 -15
  40. package/tests/recall-scope.test.ts +0 -32
  41. package/tests/recall-tool-scope.test.ts +0 -67
  42. package/tests/render-entries.test.ts +0 -92
  43. package/tests/report.test.ts +0 -58
  44. package/tests/sanitize.test.ts +0 -24
  45. package/tests/search-entries.test.ts +0 -233
  46. package/tests/stress-100-compactions.test.ts +0 -373
  47. package/tests/support/load-session.ts +0 -23
  48. package/tests/support/real-sessions.ts +0 -51
  49. package/tests/threshold-integration.test.ts +0 -335
@@ -1,335 +0,0 @@
1
- /**
2
- * Integration tests: proactive threshold + session_before_compact interaction.
3
- *
4
- * These tests verify the cross-hook contract:
5
- * - When proactive trigger calls ctx.compact(), session_before_compact
6
- * must NOT cancel the compaction even if tokensBefore differs from
7
- * getContextUsage().
8
- * - The per-model threshold guard was removed from session_before_compact
9
- * because the event carries no "reason" field — manual /compact and
10
- * auto-compaction are indistinguishable. The proactive trigger handles
11
- * the "compact earlier than pi-core" direction.
12
- */
13
- import { describe, test, expect, afterEach, beforeAll, afterAll } from "bun:test";
14
- import { existsSync, unlinkSync, writeFileSync, mkdtempSync, rmSync } from "fs";
15
- import { tmpdir } from "os";
16
- import { join } from "path";
17
- import { registerBeforeCompactHook } from "../src/hooks/before-compact";
18
- import { registerProactiveThresholdHook, resetProactiveState } from "../src/hooks/proactive-threshold";
19
-
20
- let tmpDir: string;
21
- let CONFIG_PATH: string;
22
-
23
- beforeAll(() => {
24
- tmpDir = mkdtempSync(join(tmpdir(), "pi-vcc-test-"));
25
- CONFIG_PATH = join(tmpDir, "pi-vcc-config.json");
26
- process.env.PI_VCC_CONFIG_PATH = CONFIG_PATH;
27
- });
28
-
29
- afterAll(() => {
30
- delete process.env.PI_VCC_CONFIG_PATH;
31
- rmSync(tmpDir, { recursive: true, force: true });
32
- });
33
-
34
- function setConfig(cfg: Record<string, unknown>) {
35
- writeFileSync(CONFIG_PATH, JSON.stringify(cfg));
36
- }
37
-
38
- const msg = (id: string, role: "user" | "assistant" | "toolResult", content = "x") => ({
39
- id,
40
- type: "message",
41
- message: { role, content },
42
- });
43
-
44
- function createMockPi(
45
- model?: { id: string; provider?: string; contextWindow?: number },
46
- usage?: { tokens: number | null; contextWindow: number; percent: number | null },
47
- ) {
48
- const handlers: Record<string, ((e: any, c: any) => any)[]> = {};
49
- const compactCalls: number[] = [];
50
- const notifyCalls: { msg: string; level: string }[] = [];
51
-
52
- const ctx = {
53
- hasUI: true,
54
- model: model ?? undefined,
55
- ui: {
56
- notify: (msg: string, level: string) => {
57
- notifyCalls.push({ msg, level });
58
- },
59
- },
60
- getContextUsage: () => usage ?? { tokens: null, contextWindow: 0, percent: null },
61
- compact: () => {
62
- compactCalls.push(Date.now());
63
- },
64
- } as any;
65
-
66
- const pi = {
67
- on: (eventName: string, handler: (e: any, c: any) => any) => {
68
- if (!handlers[eventName]) handlers[eventName] = [];
69
- handlers[eventName].push(handler);
70
- },
71
- } as any;
72
-
73
- const emit = (eventName: string, event: any = {}) => {
74
- const hs = handlers[eventName] ?? [];
75
- let result: any;
76
- for (const h of hs) result = h(event, ctx);
77
- return result;
78
- };
79
-
80
- return { pi, ctx, emit, compactCalls, notifyCalls };
81
- }
82
-
83
- function makeBeforeCompactEvent(branchEntries: any[], customInstructions?: string, tokensBefore = 100000) {
84
- return {
85
- type: "session_before_compact",
86
- customInstructions,
87
- branchEntries,
88
- preparation: {
89
- previousSummary: undefined,
90
- fileOps: { read: [], written: [], edited: [] },
91
- tokensBefore,
92
- },
93
- signal: new AbortController().signal,
94
- };
95
- }
96
-
97
- describe("integration: proactive trigger + before-compact", () => {
98
- afterEach(() => {
99
- if (existsSync(CONFIG_PATH)) unlinkSync(CONFIG_PATH);
100
- resetProactiveState();
101
- });
102
-
103
- test("proactive trigger then before-compact: does NOT cancel when proactiveTriggerActive", () => {
104
- setConfig({
105
- debug: false,
106
- overrideDefaultCompaction: true,
107
- modelThresholds: {
108
- "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
109
- },
110
- });
111
- const { pi, ctx, emit, compactCalls } = createMockPi(
112
- { id: "GLM-5.1", provider: "neuralwatt", contextWindow: 128000 },
113
- { tokens: 110000, contextWindow: 128000, percent: 86 },
114
- );
115
- registerProactiveThresholdHook(pi);
116
- registerBeforeCompactHook(pi);
117
-
118
- emit("agent_end", { type: "agent_end", messages: [] });
119
- expect(compactCalls).toHaveLength(1);
120
-
121
- const entries = [
122
- msg("m1", "user", "hello"),
123
- msg("m2", "assistant", "hi"),
124
- msg("m3", "user", "do work"),
125
- msg("m4", "assistant", "done"),
126
- ];
127
- const result = emit("session_before_compact", makeBeforeCompactEvent(entries, undefined, 94000));
128
-
129
- // Should NOT cancel — proactiveTriggerActive is true
130
- expect(result?.cancel).toBeUndefined();
131
- expect(result?.compaction).toBeDefined();
132
- });
133
-
134
- test("global trigger + before-compact: compaction proceeds (threshold guard removed)", () => {
135
- // Previously: pi-core's global threshold triggers compaction below the
136
- // per-model threshold → cancelled. Now: the threshold guard is removed
137
- // because manual /compact can't be distinguished from auto-compaction,
138
- // so compaction proceeds.
139
- setConfig({
140
- debug: false,
141
- overrideDefaultCompaction: true,
142
- modelThresholds: {
143
- "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
144
- },
145
- });
146
- const { pi, ctx, emit } = createMockPi(
147
- { id: "GLM-5.1", provider: "neuralwatt", contextWindow: 128000 },
148
- { tokens: 80000, contextWindow: 128000, percent: 63 },
149
- );
150
- registerProactiveThresholdHook(pi);
151
- registerBeforeCompactHook(pi);
152
-
153
- const entries = [
154
- msg("m1", "user", "hello"),
155
- msg("m2", "assistant", "hi"),
156
- msg("m3", "user", "do work"),
157
- msg("m4", "assistant", "done"),
158
- ];
159
- const result = emit("session_before_compact", makeBeforeCompactEvent(entries, undefined, 90000));
160
- // No longer cancelled — threshold guard removed
161
- expect(result?.cancel).toBeUndefined();
162
- expect(result?.compaction).toBeDefined();
163
- });
164
-
165
- test("proactiveTriggerActive is cleared after session_compact", () => {
166
- setConfig({
167
- debug: false,
168
- overrideDefaultCompaction: true,
169
- modelThresholds: {
170
- "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
171
- },
172
- });
173
- const { pi, ctx, emit, compactCalls } = createMockPi(
174
- { id: "GLM-5.1", provider: "neuralwatt", contextWindow: 128000 },
175
- { tokens: 110000, contextWindow: 128000, percent: 86 },
176
- );
177
- registerProactiveThresholdHook(pi);
178
- registerBeforeCompactHook(pi);
179
-
180
- emit("agent_end", { type: "agent_end", messages: [] });
181
- expect(compactCalls).toHaveLength(1);
182
-
183
- emit("session_compact", { type: "session_compact", compactionEntry: {} });
184
-
185
- // After session_compact, proactiveTriggerActive is cleared.
186
- // But the threshold guard is also removed, so compaction proceeds.
187
- const entries = [
188
- msg("m1", "user", "hello"),
189
- msg("m2", "assistant", "hi"),
190
- msg("m3", "user", "do work"),
191
- msg("m4", "assistant", "done"),
192
- ];
193
- const result = emit("session_before_compact", makeBeforeCompactEvent(entries, undefined, 90000));
194
- expect(result?.cancel).toBeUndefined();
195
- expect(result?.compaction).toBeDefined();
196
- });
197
-
198
- test("explicit /pi-vcc proceeds regardless of context level", () => {
199
- setConfig({
200
- debug: false,
201
- overrideDefaultCompaction: true,
202
- modelThresholds: {
203
- "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
204
- },
205
- });
206
- const { pi, ctx, emit } = createMockPi(
207
- { id: "GLM-5.1", provider: "neuralwatt", contextWindow: 128000 },
208
- { tokens: 5000, contextWindow: 128000, percent: 4 },
209
- );
210
- registerProactiveThresholdHook(pi);
211
- registerBeforeCompactHook(pi);
212
-
213
- const entries = [
214
- msg("m1", "user", "hello"),
215
- msg("m2", "assistant", "hi"),
216
- msg("m3", "user", "do work"),
217
- msg("m4", "assistant", "done"),
218
- ];
219
- const result = emit("session_before_compact", makeBeforeCompactEvent(entries, "__pi_vcc__", 5000));
220
- expect(result?.cancel).toBeUndefined();
221
- expect(result?.compaction).toBeDefined();
222
- });
223
- });
224
-
225
- describe("integration: proactive trigger + before-compact with overrideDefaultCompaction: false", () => {
226
- afterEach(() => {
227
- if (existsSync(CONFIG_PATH)) unlinkSync(CONFIG_PATH);
228
- resetProactiveState();
229
- });
230
-
231
- test("proactive trigger fires, before-compact doesn't cancel, then pi-core handles summary", () => {
232
- setConfig({
233
- debug: false,
234
- overrideDefaultCompaction: false,
235
- modelThresholds: {
236
- "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
237
- },
238
- });
239
- const { pi, ctx, emit, compactCalls } = createMockPi(
240
- { id: "GLM-5.1", provider: "neuralwatt", contextWindow: 128000 },
241
- { tokens: 110000, contextWindow: 128000, percent: 86 },
242
- );
243
- registerProactiveThresholdHook(pi);
244
- registerBeforeCompactHook(pi);
245
-
246
- emit("agent_end", { type: "agent_end", messages: [] });
247
- expect(compactCalls).toHaveLength(1);
248
-
249
- const entries = [
250
- msg("m1", "user", "hello"),
251
- msg("m2", "assistant", "hi"),
252
- msg("m3", "user", "do work"),
253
- msg("m4", "assistant", "done"),
254
- ];
255
- const result = emit("session_before_compact", makeBeforeCompactEvent(entries, undefined, 110000));
256
- // pi-vcc doesn't handle it (overrideDefaultCompaction: false) → returns undefined
257
- expect(result).toBeUndefined();
258
- });
259
-
260
- test("compaction proceeds even with overrideDefaultCompaction: false (threshold guard removed)", () => {
261
- setConfig({
262
- debug: false,
263
- overrideDefaultCompaction: false,
264
- modelThresholds: {
265
- "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
266
- },
267
- });
268
- const { pi, ctx, emit } = createMockPi(
269
- { id: "GLM-5.1", provider: "neuralwatt", contextWindow: 128000 },
270
- { tokens: 80000, contextWindow: 128000, percent: 63 },
271
- );
272
- registerProactiveThresholdHook(pi);
273
- registerBeforeCompactHook(pi);
274
-
275
- // Previously this would cancel (threshold guard). Now: compaction
276
- // proceeds (threshold guard removed).
277
- const entries = [
278
- msg("m1", "user", "hello"),
279
- msg("m2", "assistant", "hi"),
280
- ];
281
- const result = emit("session_before_compact", makeBeforeCompactEvent(entries, undefined, 80000));
282
- // No longer cancelled — returns undefined (pi-vcc doesn't handle it,
283
- // but doesn't block it either)
284
- expect(result).toBeUndefined();
285
- });
286
- });
287
-
288
- describe("integration: cooldown prevents double compaction", () => {
289
- afterEach(() => {
290
- if (existsSync(CONFIG_PATH)) unlinkSync(CONFIG_PATH);
291
- resetProactiveState();
292
- });
293
-
294
- test("agent_end + model_select on same turn: only one compact()", () => {
295
- setConfig({
296
- debug: false,
297
- overrideDefaultCompaction: true,
298
- modelThresholds: {
299
- "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
300
- },
301
- });
302
- const { pi, ctx, emit, compactCalls } = createMockPi(
303
- { id: "GLM-5.1", provider: "neuralwatt", contextWindow: 128000 },
304
- { tokens: 110000, contextWindow: 128000, percent: 86 },
305
- );
306
- registerProactiveThresholdHook(pi);
307
-
308
- emit("agent_end", { type: "agent_end", messages: [] });
309
- expect(compactCalls).toHaveLength(1);
310
-
311
- emit("model_select", { type: "model_select" });
312
- expect(compactCalls).toHaveLength(1);
313
- });
314
-
315
- test("two consecutive agent_ends without session_compact: second blocked by cooldown", () => {
316
- setConfig({
317
- debug: false,
318
- overrideDefaultCompaction: true,
319
- modelThresholds: {
320
- "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
321
- },
322
- });
323
- const { pi, ctx, emit, compactCalls } = createMockPi(
324
- { id: "GLM-5.1", provider: "neuralwatt", contextWindow: 128000 },
325
- { tokens: 110000, contextWindow: 128000, percent: 86 },
326
- );
327
- registerProactiveThresholdHook(pi);
328
-
329
- emit("agent_end", { type: "agent_end", messages: [] });
330
- expect(compactCalls).toHaveLength(1);
331
-
332
- emit("agent_end", { type: "agent_end", messages: [] });
333
- expect(compactCalls).toHaveLength(1);
334
- });
335
- });