@monotykamary/pi-vcc 0.3.16 → 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.
@@ -19,7 +19,7 @@ afterAll(() => {
19
19
  });
20
20
 
21
21
  function createMockPi(model?: { id: string; provider?: string; contextWindow?: number }) {
22
- let handler: ((event: any, ctx: any) => any) | undefined;
22
+ const handlers: Record<string, ((e: any, c: any) => any)[]> = {};
23
23
  const notifyCalls: Array<{ msg: string; level: string }> = [];
24
24
  const ctx = {
25
25
  hasUI: true,
@@ -30,15 +30,19 @@ function createMockPi(model?: { id: string; provider?: string; contextWindow?: n
30
30
  },
31
31
  },
32
32
  } as any;
33
- return {
34
- pi: {
35
- on: (eventName: string, h: (e: any, c: any) => any) => {
36
- if (eventName === "session_before_compact") handler = h;
37
- },
38
- } as any,
39
- invoke: (event: any) => handler!(event, ctx),
40
- notifyCalls,
33
+ const pi = {
34
+ on: (eventName: string, h: (e: any, c: any) => any) => {
35
+ if (!handlers[eventName]) handlers[eventName] = [];
36
+ handlers[eventName].push(h);
37
+ },
38
+ } as any;
39
+ const emit = (eventName: string, event: any = {}) => {
40
+ const hs = handlers[eventName] ?? [];
41
+ let result: any;
42
+ for (const h of hs) result = h(event, ctx);
43
+ return result;
41
44
  };
45
+ return { pi, ctx, emit, notifyCalls };
42
46
  }
43
47
 
44
48
  function setConfig(cfg: Record<string, unknown>) {
@@ -70,7 +74,12 @@ describe("session_before_compact: per-model threshold", () => {
70
74
  if (existsSync(CONFIG_PATH)) unlinkSync(CONFIG_PATH);
71
75
  });
72
76
 
73
- test("cancels when context is below per-model threshold (provider/modelId)", () => {
77
+ test("compaction proceeds when context is below per-model threshold", () => {
78
+ // The threshold guard was removed because session_before_compact carries no
79
+ // reason field — manual /compact and auto-compaction are indistinguishable.
80
+ // The proactive trigger handles compacting earlier than pi-core's global
81
+ // threshold; this hook no longer blocks compaction that arrives before the
82
+ // per-model threshold.
74
83
  setConfig({
75
84
  debug: false,
76
85
  overrideDefaultCompaction: true,
@@ -78,27 +87,27 @@ describe("session_before_compact: per-model threshold", () => {
78
87
  "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
79
88
  },
80
89
  });
81
- const { pi, invoke, notifyCalls } = createMockPi({
90
+ const { pi, ctx, emit } = createMockPi({
82
91
  id: "GLM-5.1",
83
92
  provider: "neuralwatt",
84
93
  contextWindow: 200000,
85
94
  });
86
95
  registerBeforeCompactHook(pi);
87
96
 
88
- // 100k tokens < 200k - 32768 = 167232 → below threshold
97
+ // 100k tokens < 200k - 32768 = 167232 → below threshold, but proceeds
89
98
  const entries = [
90
99
  msg("m1", "user", "hello"),
91
100
  msg("m2", "assistant", "hi"),
92
101
  msg("m3", "user", "do work"),
93
102
  msg("m4", "assistant", "done"),
94
103
  ];
95
- const result = invoke(makeEvent(entries, undefined, 100000));
96
- expect(result).toEqual({ cancel: true });
97
- expect(notifyCalls.length).toBeGreaterThanOrEqual(1);
98
- expect(notifyCalls[0].msg).toContain("Skipped compaction");
104
+ const result = emit("session_before_compact", makeEvent(entries, undefined, 100000));
105
+ // No longer cancelled — threshold guard removed
106
+ expect(result.cancel).toBeUndefined();
107
+ expect(result.compaction).toBeDefined();
99
108
  });
100
109
 
101
- test("allows compaction when context exceeds per-model threshold", () => {
110
+ test("compaction proceeds when context exceeds per-model threshold", () => {
102
111
  setConfig({
103
112
  debug: false,
104
113
  overrideDefaultCompaction: true,
@@ -106,7 +115,7 @@ describe("session_before_compact: per-model threshold", () => {
106
115
  "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
107
116
  },
108
117
  });
109
- const { pi, invoke } = createMockPi({
118
+ const { pi, ctx, emit } = createMockPi({
110
119
  id: "GLM-5.1",
111
120
  provider: "neuralwatt",
112
121
  contextWindow: 200000,
@@ -120,64 +129,38 @@ describe("session_before_compact: per-model threshold", () => {
120
129
  msg("m3", "user", "do work"),
121
130
  msg("m4", "assistant", "done"),
122
131
  ];
123
- const result = invoke(makeEvent(entries, undefined, 180000));
124
- // Not cancelled — pi-vcc proceeds with its own summary
132
+ const result = emit("session_before_compact", makeEvent(entries, undefined, 180000));
125
133
  expect(result.cancel).toBeUndefined();
126
134
  expect(result.compaction).toBeDefined();
127
135
  });
128
136
 
129
- test("cancels when context is below globalThreshold with compactPercent", () => {
130
- // compactPercent: 65 → reserve = 128000 * 0.35 = 44800 → threshold = 83200
131
- setConfig({
132
- debug: false,
133
- overrideDefaultCompaction: true,
134
- globalThreshold: { compactPercent: 65 },
135
- });
136
- const { pi, invoke } = createMockPi({
137
- id: "some-model",
138
- provider: "some-provider",
139
- contextWindow: 128000,
140
- });
141
- registerBeforeCompactHook(pi);
142
-
143
- // 50k tokens < 83200 → below threshold
144
- const entries = [
145
- msg("m1", "user", "hello"),
146
- msg("m2", "assistant", "hi"),
147
- msg("m3", "user", "do work"),
148
- msg("m4", "assistant", "done"),
149
- ];
150
- const result = invoke(makeEvent(entries, undefined, 50000));
151
- expect(result).toEqual({ cancel: true });
152
- });
153
-
154
- test("allows compaction when context exceeds globalThreshold with compactPercent", () => {
137
+ test("compaction proceeds with globalThreshold compactPercent", () => {
155
138
  // compactPercent: 65 → reserve = 128000 * 0.35 = 44800 → threshold = 83200
156
139
  setConfig({
157
140
  debug: false,
158
141
  overrideDefaultCompaction: true,
159
142
  globalThreshold: { compactPercent: 65 },
160
143
  });
161
- const { pi, invoke } = createMockPi({
144
+ const { pi, ctx, emit } = createMockPi({
162
145
  id: "some-model",
163
146
  provider: "some-provider",
164
147
  contextWindow: 128000,
165
148
  });
166
149
  registerBeforeCompactHook(pi);
167
150
 
168
- // 100k tokens > 83200 → above threshold
151
+ // 50k tokens < 83200 → below threshold, but proceeds
169
152
  const entries = [
170
153
  msg("m1", "user", "hello"),
171
154
  msg("m2", "assistant", "hi"),
172
155
  msg("m3", "user", "do work"),
173
156
  msg("m4", "assistant", "done"),
174
157
  ];
175
- const result = invoke(makeEvent(entries, undefined, 100000));
158
+ const result = emit("session_before_compact", makeEvent(entries, undefined, 50000));
176
159
  expect(result.cancel).toBeUndefined();
177
160
  expect(result.compaction).toBeDefined();
178
161
  });
179
162
 
180
- test("cancels when context is below modelThreshold with compactPercent", () => {
163
+ test("compaction proceeds with modelThreshold compactPercent", () => {
181
164
  // compactPercent: 80 → reserve = 200000 * 0.20 = 40000 → threshold = 160000
182
165
  setConfig({
183
166
  debug: false,
@@ -186,68 +169,48 @@ describe("session_before_compact: per-model threshold", () => {
186
169
  "neuralwatt/GLM-5.1": { compactPercent: 80 },
187
170
  },
188
171
  });
189
- const { pi, invoke } = createMockPi({
172
+ const { pi, ctx, emit } = createMockPi({
190
173
  id: "GLM-5.1",
191
174
  provider: "neuralwatt",
192
175
  contextWindow: 200000,
193
176
  });
194
177
  registerBeforeCompactHook(pi);
195
178
 
196
- // 100k tokens < 160000 → below threshold
179
+ // 100k tokens < 160000 → below threshold, but proceeds
197
180
  const entries = [
198
181
  msg("m1", "user", "hello"),
199
182
  msg("m2", "assistant", "hi"),
200
183
  msg("m3", "user", "do work"),
201
184
  msg("m4", "assistant", "done"),
202
185
  ];
203
- const result = invoke(makeEvent(entries, undefined, 100000));
204
- expect(result).toEqual({ cancel: true });
205
- });
206
-
207
- test("globalThreshold with compactPercent falls back from defaultThreshold", () => {
208
- setConfig({
209
- debug: false,
210
- overrideDefaultCompaction: true,
211
- defaultThreshold: { reserveTokens: 16384 },
212
- });
213
- const { pi, invoke } = createMockPi({
214
- id: "some-model",
215
- provider: "some-provider",
216
- contextWindow: 128000,
217
- });
218
- registerBeforeCompactHook(pi);
219
-
220
- // 50k tokens < 128k - 16384 = 111616 → below threshold
221
- const entries = [
222
- msg("m1", "user", "hello"),
223
- msg("m2", "assistant", "hi"),
224
- ];
225
- const result = invoke(makeEvent(entries, undefined, 50000));
226
- expect(result).toEqual({ cancel: true });
186
+ const result = emit("session_before_compact", makeEvent(entries, undefined, 100000));
187
+ expect(result.cancel).toBeUndefined();
188
+ expect(result.compaction).toBeDefined();
227
189
  });
228
190
 
229
- test("cancels when context is below defaultThreshold", () => {
191
+ test("compaction proceeds with defaultThreshold", () => {
230
192
  setConfig({
231
193
  debug: false,
232
194
  overrideDefaultCompaction: true,
233
195
  defaultThreshold: { reserveTokens: 16384 },
234
196
  });
235
- const { pi, invoke } = createMockPi({
197
+ const { pi, ctx, emit } = createMockPi({
236
198
  id: "some-model",
237
199
  provider: "some-provider",
238
200
  contextWindow: 128000,
239
201
  });
240
202
  registerBeforeCompactHook(pi);
241
203
 
242
- // 50k tokens < 128k - 16384 = 111616 → below threshold
204
+ // 50k tokens < 128k - 16384 = 111616 → below threshold, but proceeds
243
205
  const entries = [
244
206
  msg("m1", "user", "hello"),
245
207
  msg("m2", "assistant", "hi"),
246
208
  msg("m3", "user", "do work"),
247
209
  msg("m4", "assistant", "done"),
248
210
  ];
249
- const result = invoke(makeEvent(entries, undefined, 50000));
250
- expect(result).toEqual({ cancel: true });
211
+ const result = emit("session_before_compact", makeEvent(entries, undefined, 50000));
212
+ expect(result.cancel).toBeUndefined();
213
+ expect(result.compaction).toBeDefined();
251
214
  });
252
215
 
253
216
  test("does NOT cancel on /pi-vcc even when below threshold", () => {
@@ -258,51 +221,48 @@ describe("session_before_compact: per-model threshold", () => {
258
221
  "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
259
222
  },
260
223
  });
261
- const { pi, invoke } = createMockPi({
224
+ const { pi, ctx, emit } = createMockPi({
262
225
  id: "GLM-5.1",
263
226
  provider: "neuralwatt",
264
227
  contextWindow: 200000,
265
228
  });
266
229
  registerBeforeCompactHook(pi);
267
230
 
268
- // Below threshold, but explicit /pi-vcc command — should NOT cancel
231
+ // Below threshold, but explicit /pi-vcc command
269
232
  const entries = [
270
233
  msg("m1", "user", "hello"),
271
234
  msg("m2", "assistant", "hi"),
272
235
  msg("m3", "user", "do work"),
273
236
  msg("m4", "assistant", "done"),
274
237
  ];
275
- const result = invoke(makeEvent(entries, PI_VCC_COMPACT_INSTRUCTION, 100000));
276
- // Not cancelled — explicit /pi-vcc bypasses threshold check
238
+ const result = emit("session_before_compact", makeEvent(entries, PI_VCC_COMPACT_INSTRUCTION, 100000));
277
239
  expect(result.cancel).toBeUndefined();
278
240
  expect(result.compaction).toBeDefined();
279
241
  });
280
242
 
281
- test("does not cancel when no modelThresholds configured", () => {
243
+ test("compaction proceeds when no modelThresholds configured", () => {
282
244
  setConfig({
283
245
  debug: false,
284
246
  overrideDefaultCompaction: true,
285
247
  });
286
- const { pi, invoke } = createMockPi({
248
+ const { pi, ctx, emit } = createMockPi({
287
249
  id: "GLM-5.1",
288
250
  provider: "neuralwatt",
289
251
  contextWindow: 200000,
290
252
  });
291
253
  registerBeforeCompactHook(pi);
292
254
 
293
- // No modelThresholds → no threshold override → fall through to normal flow
294
255
  const entries = [
295
256
  msg("m1", "user", "hello"),
296
257
  msg("m2", "assistant", "hi"),
297
258
  msg("m3", "user", "do work"),
298
259
  msg("m4", "assistant", "done"),
299
260
  ];
300
- const result = invoke(makeEvent(entries, undefined, 100000));
301
- // Not cancelled by threshold check (may be cancelled by other logic)
261
+ const result = emit("session_before_compact", makeEvent(entries, undefined, 100000));
302
262
  expect(result.cancel === true).toBe(false);
303
263
  });
304
264
 
305
- test("does not cancel when model is undefined", () => {
265
+ test("compaction proceeds when model is undefined", () => {
306
266
  setConfig({
307
267
  debug: false,
308
268
  overrideDefaultCompaction: true,
@@ -310,8 +270,7 @@ describe("session_before_compact: per-model threshold", () => {
310
270
  "GLM-5.1": { reserveTokens: 32768 },
311
271
  },
312
272
  });
313
- // No model provided — ctx.model is undefined
314
- const { pi, invoke } = createMockPi(undefined);
273
+ const { pi, ctx, emit } = createMockPi(undefined);
315
274
  registerBeforeCompactHook(pi);
316
275
 
317
276
  const entries = [
@@ -320,12 +279,11 @@ describe("session_before_compact: per-model threshold", () => {
320
279
  msg("m3", "user", "do work"),
321
280
  msg("m4", "assistant", "done"),
322
281
  ];
323
- const result = invoke(makeEvent(entries, undefined, 100000));
324
- // No model → can't check threshold → fall through to normal flow
282
+ const result = emit("session_before_compact", makeEvent(entries, undefined, 100000));
325
283
  expect(result.cancel === true).toBe(false);
326
284
  });
327
285
 
328
- test("threshold check also applies when overrideDefaultCompaction is false", () => {
286
+ test("compaction proceeds with overrideDefaultCompaction false", () => {
329
287
  setConfig({
330
288
  debug: false,
331
289
  overrideDefaultCompaction: false,
@@ -333,45 +291,21 @@ describe("session_before_compact: per-model threshold", () => {
333
291
  "neuralwatt/GLM-5.1": { reserveTokens: 32768 },
334
292
  },
335
293
  });
336
- const { pi, invoke } = createMockPi({
337
- id: "GLM-5.1",
338
- provider: "neuralwatt",
339
- contextWindow: 200000,
340
- });
341
- registerBeforeCompactHook(pi);
342
-
343
- // Below threshold → cancel, even though pi-vcc wouldn't handle the summary
344
- const entries = [
345
- msg("m1", "user", "hello"),
346
- msg("m2", "assistant", "hi"),
347
- ];
348
- const result = invoke(makeEvent(entries, undefined, 100000));
349
- expect(result).toEqual({ cancel: true });
350
- });
351
-
352
- test("modelId-only key matches when provider/modelId does not", () => {
353
- setConfig({
354
- debug: false,
355
- overrideDefaultCompaction: true,
356
- modelThresholds: {
357
- "GLM-5.1": { reserveTokens: 32768 },
358
- },
359
- });
360
- const { pi, invoke } = createMockPi({
294
+ const { pi, ctx, emit } = createMockPi({
361
295
  id: "GLM-5.1",
362
296
  provider: "neuralwatt",
363
297
  contextWindow: 200000,
364
298
  });
365
299
  registerBeforeCompactHook(pi);
366
300
 
367
- // Should match on modelId-only key
301
+ // Below threshold, overrideDefaultCompaction: false → pi-vcc doesn't
302
+ // handle the summary, but also doesn't cancel (threshold guard removed)
368
303
  const entries = [
369
304
  msg("m1", "user", "hello"),
370
305
  msg("m2", "assistant", "hi"),
371
- msg("m3", "user", "do work"),
372
- msg("m4", "assistant", "done"),
373
306
  ];
374
- const result = invoke(makeEvent(entries, undefined, 100000));
375
- expect(result).toEqual({ cancel: true });
307
+ const result = emit("session_before_compact", makeEvent(entries, undefined, 100000));
308
+ // Not cancelled — returns undefined (pi-vcc doesn't handle it)
309
+ expect(result).toBeUndefined();
376
310
  });
377
311
  });