@monotykamary/pi-vcc 0.3.17 → 0.4.1
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 +5 -4
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/src/core/build-sections.ts +12 -131
- package/src/core/format.ts +4 -14
- package/src/core/summarize.ts +98 -22
- package/src/sections.ts +0 -4
- package/__tests__/helpers.ts +0 -135
- package/__tests__/root-causes.test.ts +0 -858
- package/demo.gif +0 -0
- package/knip.json +0 -4
- package/scripts/audit-sessions.ts +0 -88
- package/scripts/benchmark-real-sessions.ts +0 -25
- package/scripts/compare-before-after.ts +0 -36
- package/scripts/dump-branch-output.ts +0 -20
- package/tests/australian-government.test.ts +0 -479
- package/tests/before-compact-hook.test.ts +0 -185
- package/tests/before-compact.test.ts +0 -227
- package/tests/brief.test.ts +0 -215
- package/tests/build-sections.test.ts +0 -59
- package/tests/compile.test.ts +0 -89
- package/tests/content.test.ts +0 -66
- package/tests/deep-extraction.test.ts +0 -313
- package/tests/extract-goals.test.ts +0 -86
- package/tests/extract-preferences.test.ts +0 -30
- package/tests/filter-noise.test.ts +0 -61
- package/tests/fixtures.ts +0 -61
- package/tests/format-recall.test.ts +0 -87
- package/tests/format.test.ts +0 -73
- package/tests/government-domain.test.ts +0 -542
- package/tests/lineage.test.ts +0 -33
- package/tests/load-messages.test.ts +0 -49
- package/tests/model-threshold-hook.test.ts +0 -311
- package/tests/model-threshold.test.ts +0 -223
- package/tests/normalize.test.ts +0 -104
- package/tests/proactive-threshold.test.ts +0 -490
- package/tests/real-sessions.test.ts +0 -38
- package/tests/recall-expand.test.ts +0 -15
- package/tests/recall-scope.test.ts +0 -32
- package/tests/recall-tool-scope.test.ts +0 -67
- package/tests/render-entries.test.ts +0 -92
- package/tests/report.test.ts +0 -58
- package/tests/sanitize.test.ts +0 -24
- package/tests/search-entries.test.ts +0 -233
- package/tests/support/load-session.ts +0 -23
- package/tests/support/real-sessions.ts +0 -51
- package/tests/threshold-integration.test.ts +0 -335
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect, beforeEach, afterEach, beforeAll, afterAll } from "bun:test";
|
|
2
|
-
import { existsSync, unlinkSync, writeFileSync, readFileSync, mkdtempSync, rmSync } from "fs";
|
|
3
|
-
import { tmpdir } from "os";
|
|
4
|
-
import { join } from "path";
|
|
5
|
-
import { registerBeforeCompactHook, PI_VCC_COMPACT_INSTRUCTION } from "../src/hooks/before-compact";
|
|
6
|
-
|
|
7
|
-
let tmpDir: string;
|
|
8
|
-
let CONFIG_PATH: string;
|
|
9
|
-
const DEBUG_PATH = "/tmp/pi-vcc-debug.json";
|
|
10
|
-
|
|
11
|
-
beforeAll(() => {
|
|
12
|
-
tmpDir = mkdtempSync(join(tmpdir(), "pi-vcc-test-"));
|
|
13
|
-
CONFIG_PATH = join(tmpDir, "pi-vcc-config.json");
|
|
14
|
-
process.env.PI_VCC_CONFIG_PATH = CONFIG_PATH;
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
afterAll(() => {
|
|
18
|
-
delete process.env.PI_VCC_CONFIG_PATH;
|
|
19
|
-
rmSync(tmpDir, { recursive: true, force: true });
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
// Minimal ExtensionAPI stub: capture handler + provide ctx with mocked ui.notify
|
|
23
|
-
function createMockPi() {
|
|
24
|
-
let handler: ((event: any, ctx: any) => any) | undefined;
|
|
25
|
-
const notifyCalls: Array<{ msg: string; level: string }> = [];
|
|
26
|
-
const ctx = {
|
|
27
|
-
hasUI: true,
|
|
28
|
-
ui: {
|
|
29
|
-
notify: (msg: string, level: string) => {
|
|
30
|
-
notifyCalls.push({ msg, level });
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
return {
|
|
35
|
-
pi: {
|
|
36
|
-
on: (eventName: string, h: (e: any, c: any) => any) => {
|
|
37
|
-
if (eventName === "session_before_compact") handler = h;
|
|
38
|
-
},
|
|
39
|
-
} as any,
|
|
40
|
-
invoke: (event: any) => handler!(event, ctx),
|
|
41
|
-
notifyCalls,
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function setConfig(cfg: Record<string, unknown>) {
|
|
46
|
-
writeFileSync(CONFIG_PATH, JSON.stringify(cfg));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function makeEvent(branchEntries: any[], customInstructions?: string) {
|
|
50
|
-
return {
|
|
51
|
-
type: "session_before_compact",
|
|
52
|
-
customInstructions,
|
|
53
|
-
branchEntries,
|
|
54
|
-
preparation: {
|
|
55
|
-
previousSummary: undefined,
|
|
56
|
-
fileOps: { read: [], written: [], edited: [] },
|
|
57
|
-
tokensBefore: 1000,
|
|
58
|
-
},
|
|
59
|
-
signal: new AbortController().signal,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const msg = (id: string, role: "user" | "assistant" | "toolResult", content = "x") => ({
|
|
64
|
-
id,
|
|
65
|
-
type: "message",
|
|
66
|
-
message: { role, content },
|
|
67
|
-
});
|
|
68
|
-
const comp = (id: string, firstKeptEntryId?: string) => ({ id, type: "compaction", firstKeptEntryId });
|
|
69
|
-
|
|
70
|
-
describe("registerBeforeCompactHook: cancel paths", () => {
|
|
71
|
-
beforeEach(() => {
|
|
72
|
-
if (existsSync(DEBUG_PATH)) unlinkSync(DEBUG_PATH);
|
|
73
|
-
});
|
|
74
|
-
afterEach(() => {
|
|
75
|
-
if (existsSync(CONFIG_PATH)) unlinkSync(CONFIG_PATH);
|
|
76
|
-
if (existsSync(DEBUG_PATH)) unlinkSync(DEBUG_PATH);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test("/pi-vcc with too few live messages cancels and notifies warning", () => {
|
|
80
|
-
setConfig({ debug: false, overrideDefaultCompaction: false });
|
|
81
|
-
const { pi, invoke, notifyCalls } = createMockPi();
|
|
82
|
-
registerBeforeCompactHook(pi);
|
|
83
|
-
|
|
84
|
-
const entries = [msg("m1", "user"), msg("m2", "assistant")];
|
|
85
|
-
expect(invoke(makeEvent(entries, PI_VCC_COMPACT_INSTRUCTION))).toEqual({ cancel: true });
|
|
86
|
-
expect(notifyCalls).toHaveLength(1);
|
|
87
|
-
expect(notifyCalls[0].level).toBe("warning");
|
|
88
|
-
expect(notifyCalls[0].msg).toContain("Too few messages");
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test("/pi-vcc with no user message compacts all instead of cancelling", () => {
|
|
92
|
-
setConfig({ debug: false, overrideDefaultCompaction: false });
|
|
93
|
-
const { pi, invoke, notifyCalls } = createMockPi();
|
|
94
|
-
registerBeforeCompactHook(pi);
|
|
95
|
-
|
|
96
|
-
const entries = [msg("m1", "assistant"), msg("m2", "assistant"), msg("m3", "assistant")];
|
|
97
|
-
const result = invoke(makeEvent(entries, PI_VCC_COMPACT_INSTRUCTION));
|
|
98
|
-
// No longer cancels — compacts all to recover from context overflow
|
|
99
|
-
expect(result.cancel).toBeUndefined();
|
|
100
|
-
expect(result.compaction).toBeDefined();
|
|
101
|
-
expect(result.compaction.firstKeptEntryId).toBe("");
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
test("/compact with override=true cancels and notifies (NEW: was silent before)", () => {
|
|
105
|
-
setConfig({ debug: false, overrideDefaultCompaction: true });
|
|
106
|
-
const { pi, invoke, notifyCalls } = createMockPi();
|
|
107
|
-
registerBeforeCompactHook(pi);
|
|
108
|
-
|
|
109
|
-
const entries = [msg("m1", "user"), msg("m2", "assistant")];
|
|
110
|
-
expect(invoke(makeEvent(entries, undefined))).toEqual({ cancel: true });
|
|
111
|
-
expect(notifyCalls).toHaveLength(1);
|
|
112
|
-
expect(notifyCalls[0].level).toBe("warning");
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test("/compact with override=false short-circuits (no notify, returns undefined)", () => {
|
|
116
|
-
setConfig({ debug: false, overrideDefaultCompaction: false });
|
|
117
|
-
const { pi, invoke, notifyCalls } = createMockPi();
|
|
118
|
-
registerBeforeCompactHook(pi);
|
|
119
|
-
|
|
120
|
-
const entries = [msg("m1", "user"), msg("m2", "assistant")];
|
|
121
|
-
expect(invoke(makeEvent(entries, undefined))).toBeUndefined();
|
|
122
|
-
expect(notifyCalls).toHaveLength(0);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
test("debug:true writes metrics-only snapshot on cancel with no content leakage", () => {
|
|
126
|
-
setConfig({ debug: true, overrideDefaultCompaction: false });
|
|
127
|
-
const { pi, invoke } = createMockPi();
|
|
128
|
-
registerBeforeCompactHook(pi);
|
|
129
|
-
|
|
130
|
-
// Use too_few_live_messages cancel path to test content leakage
|
|
131
|
-
const entries = [
|
|
132
|
-
msg("m1", "user", "SECRET_TOKEN_abc123"),
|
|
133
|
-
msg("m2", "assistant", "sensitive response"),
|
|
134
|
-
];
|
|
135
|
-
expect(invoke(makeEvent(entries, PI_VCC_COMPACT_INSTRUCTION))).toEqual({ cancel: true });
|
|
136
|
-
|
|
137
|
-
expect(existsSync(DEBUG_PATH)).toBe(true);
|
|
138
|
-
const snapshot = JSON.parse(readFileSync(DEBUG_PATH, "utf-8"));
|
|
139
|
-
expect(snapshot.cancelled).toBe(true);
|
|
140
|
-
expect(snapshot.reason).toBe("too_few_live_messages");
|
|
141
|
-
|
|
142
|
-
// No content leakage
|
|
143
|
-
const serialized = JSON.stringify(snapshot);
|
|
144
|
-
expect(serialized).not.toContain("SECRET_TOKEN_abc123");
|
|
145
|
-
expect(serialized).not.toContain("sensitive response");
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test("debug:false does NOT write snapshot", () => {
|
|
149
|
-
setConfig({ debug: false, overrideDefaultCompaction: false });
|
|
150
|
-
const { pi, invoke } = createMockPi();
|
|
151
|
-
registerBeforeCompactHook(pi);
|
|
152
|
-
const entries = [msg("m1", "user"), msg("m2", "assistant")];
|
|
153
|
-
expect(invoke(makeEvent(entries, PI_VCC_COMPACT_INSTRUCTION))).toEqual({ cancel: true });
|
|
154
|
-
expect(existsSync(DEBUG_PATH)).toBe(false);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
describe("registerBeforeCompactHook: compact-all path", () => {
|
|
159
|
-
beforeEach(() => {
|
|
160
|
-
if (existsSync(DEBUG_PATH)) unlinkSync(DEBUG_PATH);
|
|
161
|
-
});
|
|
162
|
-
afterEach(() => {
|
|
163
|
-
if (existsSync(CONFIG_PATH)) unlinkSync(CONFIG_PATH);
|
|
164
|
-
if (existsSync(DEBUG_PATH)) unlinkSync(DEBUG_PATH);
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
test("single-user + autonomous tail → cuts at mid-cycle boundary", () => {
|
|
168
|
-
setConfig({ debug: false, overrideDefaultCompaction: false });
|
|
169
|
-
const { pi, invoke, notifyCalls } = createMockPi();
|
|
170
|
-
registerBeforeCompactHook(pi);
|
|
171
|
-
|
|
172
|
-
const entries = [
|
|
173
|
-
msg("m1", "user", "go"),
|
|
174
|
-
msg("m2", "assistant", "calling tool"),
|
|
175
|
-
msg("m3", "toolResult", "result"),
|
|
176
|
-
msg("m4", "assistant", "done"),
|
|
177
|
-
];
|
|
178
|
-
const result = invoke(makeEvent(entries, PI_VCC_COMPACT_INSTRUCTION));
|
|
179
|
-
expect(result.compaction).toBeDefined();
|
|
180
|
-
// Single user at idx 0, completed cycle m2→m3 ends at idx 2 (midpoint=2)
|
|
181
|
-
// Cut after m3, keep from m4 onward
|
|
182
|
-
expect(result.compaction.firstKeptEntryId).toBe("m4");
|
|
183
|
-
expect(notifyCalls).toHaveLength(0); // no cancel notify on success
|
|
184
|
-
});
|
|
185
|
-
});
|
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } from "bun:test";
|
|
2
|
-
import { buildOwnCut } from "../src/hooks/before-compact";
|
|
3
|
-
|
|
4
|
-
const msg = (id: string, role: "user" | "assistant" | "toolResult", content = "x") => ({
|
|
5
|
-
id,
|
|
6
|
-
type: "message",
|
|
7
|
-
message: { role, content },
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
const comp = (id: string, firstKeptEntryId?: string) => ({
|
|
11
|
-
id,
|
|
12
|
-
type: "compaction",
|
|
13
|
-
firstKeptEntryId,
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
describe("buildOwnCut", () => {
|
|
17
|
-
test("no prior compaction: cuts at last user message", () => {
|
|
18
|
-
const r = buildOwnCut([
|
|
19
|
-
msg("m1", "user", "a"),
|
|
20
|
-
msg("m2", "assistant", "b"),
|
|
21
|
-
msg("m3", "user", "c"),
|
|
22
|
-
msg("m4", "assistant", "d"),
|
|
23
|
-
]);
|
|
24
|
-
expect(r.ok).toBe(true);
|
|
25
|
-
if (!r.ok) return;
|
|
26
|
-
expect(r.firstKeptEntryId).toBe("m3");
|
|
27
|
-
expect(r.messages).toHaveLength(2);
|
|
28
|
-
expect(r.compactAll).toBe(false);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("cancels with too_few_live_messages when liveMessages <= 2", () => {
|
|
32
|
-
const r = buildOwnCut([
|
|
33
|
-
comp("c1", "m1"),
|
|
34
|
-
msg("m1", "user", "x"),
|
|
35
|
-
msg("m2", "assistant", "y"),
|
|
36
|
-
]);
|
|
37
|
-
expect(r.ok).toBe(false);
|
|
38
|
-
if (r.ok) return;
|
|
39
|
-
expect(r.reason).toBe("too_few_live_messages");
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test("orphan firstKeptEntryId triggers recovery (collect after compaction)", () => {
|
|
43
|
-
// Prev compaction set firstKeptEntryId to a non-existent id (e.g. "" sentinel
|
|
44
|
-
// from a previous compact-all). Recovery should collect msgs after compaction.
|
|
45
|
-
const r = buildOwnCut([
|
|
46
|
-
msg("old1", "user", "old"),
|
|
47
|
-
msg("old2", "assistant", "old"),
|
|
48
|
-
comp("c1", "ORPHAN_ID"),
|
|
49
|
-
msg("m1", "user", "a"),
|
|
50
|
-
msg("m2", "assistant", "b"),
|
|
51
|
-
msg("m3", "user", "c"),
|
|
52
|
-
msg("m4", "assistant", "d"),
|
|
53
|
-
]);
|
|
54
|
-
expect(r.ok).toBe(true);
|
|
55
|
-
if (!r.ok) return;
|
|
56
|
-
expect(r.firstKeptEntryId).toBe("m3");
|
|
57
|
-
expect(r.messages).toHaveLength(2);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("resumes from firstKeptEntryId after prior compaction", () => {
|
|
61
|
-
const r = buildOwnCut([
|
|
62
|
-
msg("old1", "user", "old"),
|
|
63
|
-
msg("old2", "assistant", "old"),
|
|
64
|
-
comp("c1", "m1"),
|
|
65
|
-
msg("m1", "user", "a"),
|
|
66
|
-
msg("m2", "assistant", "b"),
|
|
67
|
-
msg("m3", "user", "c"),
|
|
68
|
-
msg("m4", "assistant", "d"),
|
|
69
|
-
]);
|
|
70
|
-
expect(r.ok).toBe(true);
|
|
71
|
-
if (!r.ok) return;
|
|
72
|
-
expect(r.firstKeptEntryId).toBe("m3");
|
|
73
|
-
expect(r.messages).toHaveLength(2);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test("single user prompt + autonomous tail: cut at mid-cycle boundary", () => {
|
|
77
|
-
// The agentic scenario: user types 1 prompt, agent runs autonomously
|
|
78
|
-
// (assistant + toolResult interleaved). No user > idx 0.
|
|
79
|
-
// Instead of compact-all, find a completed tool-cycle boundary.
|
|
80
|
-
const r = buildOwnCut([
|
|
81
|
-
msg("m1", "user", "go"),
|
|
82
|
-
msg("m2", "assistant", "calling tool"),
|
|
83
|
-
msg("m3", "toolResult", "result"),
|
|
84
|
-
msg("m4", "assistant", "more"),
|
|
85
|
-
msg("m5", "toolResult", "result2"),
|
|
86
|
-
msg("m6", "assistant", "done"),
|
|
87
|
-
]);
|
|
88
|
-
expect(r.ok).toBe(true);
|
|
89
|
-
if (!r.ok) return;
|
|
90
|
-
expect(r.compactAll).toBe(false);
|
|
91
|
-
// Should cut after the first completed cycle (m2→m3) or the second (m4→m5)
|
|
92
|
-
// Mid-cycle logic picks nearest to midpoint of 6 = index 3
|
|
93
|
-
// Cycle 1 ends at m3 (idx 3), cycle 2 ends at m5 (idx 5)
|
|
94
|
-
// Distance: |3-3|=0 vs |5-3|=2, so picks m3
|
|
95
|
-
expect(r.firstKeptEntryId).toBe("m4");
|
|
96
|
-
expect(r.messages).toHaveLength(3); // m1, m2, m3
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("no user message: compact-all instead of cancelling", () => {
|
|
100
|
-
// When there are enough live messages but none are from the user
|
|
101
|
-
// (e.g., long assistant/tool chain), compact all rather than
|
|
102
|
-
// cancelling and leaving the session unrecoverable.
|
|
103
|
-
const r = buildOwnCut([
|
|
104
|
-
msg("m1", "assistant", "a"),
|
|
105
|
-
msg("m2", "assistant", "b"),
|
|
106
|
-
msg("m3", "assistant", "c"),
|
|
107
|
-
]);
|
|
108
|
-
expect(r.ok).toBe(true);
|
|
109
|
-
if (!r.ok) return;
|
|
110
|
-
expect(r.compactAll).toBe(true);
|
|
111
|
-
expect(r.firstKeptEntryId).toBe("");
|
|
112
|
-
expect(r.messages).toHaveLength(3);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test("compact-all then more chat: orphan recovery + normal cut", () => {
|
|
116
|
-
// After a compact-all (firstKeptEntryId=""), user chats more turns,
|
|
117
|
-
// next compaction should orphan-recover and find multiple users.
|
|
118
|
-
const r = buildOwnCut([
|
|
119
|
-
msg("o1", "user", "old"),
|
|
120
|
-
msg("o2", "assistant", "old"),
|
|
121
|
-
comp("c1", ""), // sentinel from prior compact-all
|
|
122
|
-
msg("u1", "user", "new1"),
|
|
123
|
-
msg("a1", "assistant", "reply1"),
|
|
124
|
-
msg("u2", "user", "new2"),
|
|
125
|
-
msg("a2", "assistant", "reply2"),
|
|
126
|
-
msg("u3", "user", "new3"),
|
|
127
|
-
msg("a3", "assistant", "reply3"),
|
|
128
|
-
]);
|
|
129
|
-
expect(r.ok).toBe(true);
|
|
130
|
-
if (!r.ok) return;
|
|
131
|
-
expect(r.compactAll).toBe(false);
|
|
132
|
-
expect(r.firstKeptEntryId).toBe("u3");
|
|
133
|
-
expect(r.messages).toHaveLength(4); // u1, a1, u2, a2
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
test("compact-all then single user msg + autonomous: cut at mid-cycle boundary", () => {
|
|
137
|
-
// After compact-all, only 1 user message in orphan range. Should find
|
|
138
|
-
// a completed tool-cycle boundary instead of compacting all again.
|
|
139
|
-
const r = buildOwnCut([
|
|
140
|
-
msg("o1", "user", "old"),
|
|
141
|
-
comp("c1", ""),
|
|
142
|
-
msg("u1", "user", "okay"),
|
|
143
|
-
msg("a1", "assistant", "x"),
|
|
144
|
-
msg("t1", "toolResult", "y"),
|
|
145
|
-
msg("a2", "assistant", "z"),
|
|
146
|
-
]);
|
|
147
|
-
expect(r.ok).toBe(true);
|
|
148
|
-
if (!r.ok) return;
|
|
149
|
-
// Single user at idx 0 of live range → mid-cycle boundary
|
|
150
|
-
// Live messages: [u1, a1, t1, a2]. Midpoint=2. Completed cycle ends at t1 (idx 2).
|
|
151
|
-
expect(r.compactAll).toBe(false);
|
|
152
|
-
expect(r.firstKeptEntryId).toBe("a2");
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
test("matched tool calls do NOT push cut back to previous user", () => {
|
|
156
|
-
// Regression: toolResult messages carry toolCallId at the message level,
|
|
157
|
-
// not as a content part. The old code looked for part.type==="toolResult"
|
|
158
|
-
// in content (which never matches), causing every toolCall to appear
|
|
159
|
-
// "unmatched" and pushing the cut back one user turn.
|
|
160
|
-
const r = buildOwnCut([
|
|
161
|
-
msg("u1", "user", "first prompt"),
|
|
162
|
-
msg("a1", "assistant", [{ type: "text", text: "let me check" }, { type: "toolCall", id: "tc_1", name: "read", arguments: { path: "foo.ts" } }]),
|
|
163
|
-
{ id: "t1", type: "message", message: { role: "toolResult", toolCallId: "tc_1", toolName: "read", content: [{ type: "text", text: "file contents" }], isError: false } },
|
|
164
|
-
msg("u2", "user", "second prompt"),
|
|
165
|
-
msg("a2", "assistant", [{ type: "text", text: "done" }]),
|
|
166
|
-
]);
|
|
167
|
-
expect(r.ok).toBe(true);
|
|
168
|
-
if (!r.ok) return;
|
|
169
|
-
// Cut should be at u2 (second user) since tc_1 has a matching toolResult
|
|
170
|
-
expect(r.compactAll).toBe(false);
|
|
171
|
-
expect(r.firstKeptEntryId).toBe("u2");
|
|
172
|
-
expect(r.messages).toHaveLength(3); // u1, a1, t1
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test("unmatched tool call still pushes cut back", () => {
|
|
176
|
-
// When a toolCall genuinely has no toolResult, the cut should still
|
|
177
|
-
// push back to keep the in-progress turn in the tail.
|
|
178
|
-
const r = buildOwnCut([
|
|
179
|
-
msg("u1", "user", "first prompt"),
|
|
180
|
-
msg("a1", "assistant", "response 1"),
|
|
181
|
-
msg("u2", "user", "second prompt"),
|
|
182
|
-
msg("a2", "assistant", "response 2"),
|
|
183
|
-
msg("u3", "user", "third prompt"),
|
|
184
|
-
msg("a3", "assistant", [{ type: "text", text: "checking" }, { type: "toolCall", id: "tc_unmatched", name: "bash", arguments: { command: "ls" } }]),
|
|
185
|
-
]);
|
|
186
|
-
expect(r.ok).toBe(true);
|
|
187
|
-
if (!r.ok) return;
|
|
188
|
-
// tc_unmatched has no toolResult → push cut back from u3 to u2
|
|
189
|
-
expect(r.compactAll).toBe(false);
|
|
190
|
-
expect(r.firstKeptEntryId).toBe("u2");
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
test("Anthropic-style session: many matched toolCalls should not cause compact-all", () => {
|
|
194
|
-
// Simulates the real Anthropic pattern: 2 user messages with many matched
|
|
195
|
-
// toolCall/toolResult pairs between them. The bug caused all toolCalls to
|
|
196
|
-
// appear "unmatched" → cut pushed back to first user → compact-all.
|
|
197
|
-
const entries: any[] = [
|
|
198
|
-
msg("u1", "user", "help me find bugs"),
|
|
199
|
-
];
|
|
200
|
-
// Add 5 matched tool cycles (assistant with toolCall + toolResult)
|
|
201
|
-
for (let i = 1; i <= 5; i++) {
|
|
202
|
-
entries.push({ id: `a${i}`, type: "message", message: { role: "assistant", content: [
|
|
203
|
-
{ type: "thinking", thinking: `thinking ${i}` },
|
|
204
|
-
{ type: "toolCall", id: `tc_${i}`, name: "read", arguments: { path: `file${i}.ts` } },
|
|
205
|
-
] } });
|
|
206
|
-
entries.push({ id: `t${i}`, type: "message", message: { role: "toolResult", toolCallId: `tc_${i}`, toolName: "read", content: [{ type: "text", text: `file ${i} contents` }], isError: false } });
|
|
207
|
-
}
|
|
208
|
-
// Second user message
|
|
209
|
-
entries.push(msg("u2", "user", "now fix the bug"));
|
|
210
|
-
// More matched tool cycles after second user
|
|
211
|
-
for (let i = 6; i <= 8; i++) {
|
|
212
|
-
entries.push({ id: `a${i}`, type: "message", message: { role: "assistant", content: [
|
|
213
|
-
{ type: "toolCall", id: `tc_${i}`, name: "edit", arguments: { path: `file${i}.ts` } },
|
|
214
|
-
] } });
|
|
215
|
-
entries.push({ id: `t${i}`, type: "message", message: { role: "toolResult", toolCallId: `tc_${i}`, toolName: "edit", content: [{ type: "text", text: `edited file ${i}` }], isError: false } });
|
|
216
|
-
}
|
|
217
|
-
entries.push(msg("a9", "assistant", "all done"));
|
|
218
|
-
|
|
219
|
-
const r = buildOwnCut(entries);
|
|
220
|
-
expect(r.ok).toBe(true);
|
|
221
|
-
if (!r.ok) return;
|
|
222
|
-
// Cut should be at u2 — all toolCalls after u1 have matching toolResults
|
|
223
|
-
// and all toolCalls after u2 also have matching toolResults
|
|
224
|
-
expect(r.compactAll).toBe(false);
|
|
225
|
-
expect(r.firstKeptEntryId).toBe("u2");
|
|
226
|
-
});
|
|
227
|
-
});
|
package/tests/brief.test.ts
DELETED
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "bun:test";
|
|
2
|
-
import { compileBrief } from "../src/core/brief";
|
|
3
|
-
import type { NormalizedBlock } from "../src/types";
|
|
4
|
-
|
|
5
|
-
describe("compileBrief", () => {
|
|
6
|
-
it("returns empty string for no blocks", () => {
|
|
7
|
-
expect(compileBrief([])).toBe("");
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it("renders user and assistant text", () => {
|
|
11
|
-
const blocks: NormalizedBlock[] = [
|
|
12
|
-
{ kind: "user", text: "fix auth bug" },
|
|
13
|
-
{ kind: "assistant", text: "Let me look at the auth module." },
|
|
14
|
-
];
|
|
15
|
-
const r = compileBrief(blocks);
|
|
16
|
-
expect(r).toContain("[user]");
|
|
17
|
-
expect(r).toContain("fix auth bug");
|
|
18
|
-
expect(r).toContain("[assistant]");
|
|
19
|
-
expect(r).toContain("Let me look at the auth module.");
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("renders bash commands as user actions", () => {
|
|
23
|
-
const blocks: NormalizedBlock[] = [
|
|
24
|
-
{ kind: "bash", command: "npm test", output: "FAIL noisy output", exitCode: 1, sourceIndex: 2 },
|
|
25
|
-
];
|
|
26
|
-
const r = compileBrief(blocks);
|
|
27
|
-
expect(r).toContain("[user]\n$ npm test (#2)");
|
|
28
|
-
expect(r).not.toContain("FAIL noisy output");
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("strips filler prefixes but preserves meaningful lead-ins", () => {
|
|
32
|
-
const blocks: NormalizedBlock[] = [
|
|
33
|
-
{ kind: "assistant", text: "Okay, I found the root cause." },
|
|
34
|
-
{ kind: "assistant", text: "Actually, the issue is in middleware." },
|
|
35
|
-
{ kind: "assistant", text: "Let me check the logs." },
|
|
36
|
-
];
|
|
37
|
-
const r = compileBrief(blocks);
|
|
38
|
-
expect(r).toContain("I found the root cause.");
|
|
39
|
-
expect(r).toContain("the issue is in middleware.");
|
|
40
|
-
expect(r).toContain("Let me check the logs.");
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it("collapses tool calls to one-liners under [assistant]", () => {
|
|
44
|
-
const blocks: NormalizedBlock[] = [
|
|
45
|
-
{ kind: "assistant", text: "Let me check." },
|
|
46
|
-
{ kind: "tool_call", name: "Read", args: { file_path: "auth.ts" } },
|
|
47
|
-
{ kind: "tool_call", name: "Edit", args: { file_path: "auth.ts" } },
|
|
48
|
-
];
|
|
49
|
-
const r = compileBrief(blocks);
|
|
50
|
-
expect(r).toContain('* Read "auth.ts"');
|
|
51
|
-
expect(r).toContain('* Edit "auth.ts"');
|
|
52
|
-
// Should merge into single [assistant] section
|
|
53
|
-
const matches = r.match(/\[assistant\]/g);
|
|
54
|
-
expect(matches?.length).toBe(1);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("hides non-error tool results", () => {
|
|
58
|
-
const blocks: NormalizedBlock[] = [
|
|
59
|
-
{ kind: "tool_result", name: "Read", text: "const x = 1;\nconst y = 2;\n// lots of code", isError: false },
|
|
60
|
-
];
|
|
61
|
-
const r = compileBrief(blocks);
|
|
62
|
-
expect(r).toBe("");
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it("shows tool errors with first line", () => {
|
|
66
|
-
const blocks: NormalizedBlock[] = [
|
|
67
|
-
{ kind: "tool_result", name: "bash", text: "FAIL auth.test.ts\nexpected 200 got 401", isError: true },
|
|
68
|
-
];
|
|
69
|
-
const r = compileBrief(blocks);
|
|
70
|
-
expect(r).toContain("[tool_error] bash");
|
|
71
|
-
expect(r).toContain("FAIL auth.test.ts");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("hides thinking blocks", () => {
|
|
75
|
-
const blocks: NormalizedBlock[] = [
|
|
76
|
-
{ kind: "thinking", text: "Let me think about this...", redacted: false },
|
|
77
|
-
{ kind: "assistant", text: "Here's what I found." },
|
|
78
|
-
];
|
|
79
|
-
const r = compileBrief(blocks);
|
|
80
|
-
expect(r).not.toContain("think");
|
|
81
|
-
expect(r).toContain("Here's what I found.");
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("merges adjacent assistant sections", () => {
|
|
85
|
-
const blocks: NormalizedBlock[] = [
|
|
86
|
-
{ kind: "assistant", text: "First part." },
|
|
87
|
-
{ kind: "tool_call", name: "Read", args: { file_path: "a.ts" } },
|
|
88
|
-
// No user/tool_result between these — should merge
|
|
89
|
-
{ kind: "assistant", text: "Second part." },
|
|
90
|
-
{ kind: "tool_call", name: "Read", args: { file_path: "b.ts" } },
|
|
91
|
-
];
|
|
92
|
-
const r = compileBrief(blocks);
|
|
93
|
-
const matches = r.match(/\[assistant\]/g);
|
|
94
|
-
expect(matches?.length).toBe(1);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it("does NOT merge assistant after user", () => {
|
|
98
|
-
const blocks: NormalizedBlock[] = [
|
|
99
|
-
{ kind: "assistant", text: "First." },
|
|
100
|
-
{ kind: "user", text: "Next task." },
|
|
101
|
-
{ kind: "assistant", text: "Second." },
|
|
102
|
-
];
|
|
103
|
-
const r = compileBrief(blocks);
|
|
104
|
-
const matches = r.match(/\[assistant\]/g);
|
|
105
|
-
expect(matches?.length).toBe(2);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it("truncates long user text", () => {
|
|
109
|
-
const longText = Array.from({ length: 300 }, (_, i) => `word${i}`).join(" ");
|
|
110
|
-
const blocks: NormalizedBlock[] = [
|
|
111
|
-
{ kind: "user", text: longText },
|
|
112
|
-
];
|
|
113
|
-
const r = compileBrief(blocks);
|
|
114
|
-
expect(r).toContain("(truncated)");
|
|
115
|
-
expect(r).not.toContain("word299");
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it("truncates long assistant text", () => {
|
|
119
|
-
const longText = Array.from({ length: 300 }, (_, i) => `word${i}`).join(" ");
|
|
120
|
-
const blocks: NormalizedBlock[] = [
|
|
121
|
-
{ kind: "assistant", text: longText },
|
|
122
|
-
];
|
|
123
|
-
const r = compileBrief(blocks);
|
|
124
|
-
expect(r).toContain("(truncated)");
|
|
125
|
-
expect(r).not.toContain("word299");
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("renders a realistic conversation flow", () => {
|
|
129
|
-
const blocks: NormalizedBlock[] = [
|
|
130
|
-
{ kind: "user", text: "fix the login bug" },
|
|
131
|
-
{ kind: "thinking", text: "I need to check...", redacted: false },
|
|
132
|
-
{ kind: "assistant", text: "Let me investigate." },
|
|
133
|
-
{ kind: "tool_call", name: "Read", args: { file_path: "login.ts" } },
|
|
134
|
-
{ kind: "tool_result", name: "Read", text: "export function login() { ... }", isError: false },
|
|
135
|
-
{ kind: "tool_call", name: "bash", args: { command: "npm test" } },
|
|
136
|
-
{ kind: "tool_result", name: "bash", text: "FAIL: login test\nExpected true, got false", isError: true },
|
|
137
|
-
{ kind: "assistant", text: "The test is failing because..." },
|
|
138
|
-
{ kind: "tool_call", name: "Edit", args: { file_path: "login.ts" } },
|
|
139
|
-
{ kind: "tool_result", name: "Edit", text: "File edited successfully", isError: false },
|
|
140
|
-
{ kind: "user", text: "test lại đi" },
|
|
141
|
-
{ kind: "assistant", text: "Running tests again." },
|
|
142
|
-
{ kind: "tool_call", name: "bash", args: { command: "npm test" } },
|
|
143
|
-
{ kind: "tool_result", name: "bash", text: "All tests passed", isError: false },
|
|
144
|
-
];
|
|
145
|
-
const r = compileBrief(blocks);
|
|
146
|
-
|
|
147
|
-
// Check structure
|
|
148
|
-
expect(r).toContain("[user]\nfix the login bug");
|
|
149
|
-
expect(r).toContain('[assistant]\nLet me investigate.\n* Read "login.ts"');
|
|
150
|
-
expect(r).toContain("[tool_error] bash\nFAIL: login test");
|
|
151
|
-
expect(r).toContain('[assistant]\nThe test is failing because...\n* Edit "login.ts"');
|
|
152
|
-
expect(r).toContain("[user]\ntest lại đi");
|
|
153
|
-
expect(r).toContain('[assistant]\nRunning tests again.\n* bash "npm test"');
|
|
154
|
-
|
|
155
|
-
// Hidden content
|
|
156
|
-
expect(r).not.toContain("think");
|
|
157
|
-
expect(r).not.toContain("export function login");
|
|
158
|
-
expect(r).not.toContain("File edited successfully");
|
|
159
|
-
expect(r).not.toContain("All tests passed");
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
// ── noise filtering tests (aligned with VCC) ──
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
it("suppresses blank lines between consecutive tool-only sections", () => {
|
|
173
|
-
const blocks: NormalizedBlock[] = [
|
|
174
|
-
{ kind: "assistant", text: "Checking files." },
|
|
175
|
-
{ kind: "tool_call", name: "Read", args: { file_path: "a.ts" } },
|
|
176
|
-
{ kind: "tool_result", name: "Read", text: "...", isError: false },
|
|
177
|
-
// tool_result hidden → next tool_call starts new assistant section
|
|
178
|
-
// but since both are tool-only, no blank line between
|
|
179
|
-
{ kind: "tool_call", name: "Read", args: { file_path: "b.ts" } },
|
|
180
|
-
{ kind: "tool_result", name: "Read", text: "...", isError: false },
|
|
181
|
-
];
|
|
182
|
-
const r = compileBrief(blocks);
|
|
183
|
-
// The first assistant section has text + tool, so it's NOT tool-only
|
|
184
|
-
// The second would be tool-only but merges into the first (adjacent assistant)
|
|
185
|
-
// So all under one [assistant]
|
|
186
|
-
expect(r.match(/\[assistant\]/g)?.length).toBe(1);
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it("caps tool calls per [assistant] turn at 8 (keep tail)", () => {
|
|
190
|
-
const blocks: NormalizedBlock[] = [
|
|
191
|
-
{ kind: "assistant", text: "Working." },
|
|
192
|
-
];
|
|
193
|
-
for (let i = 1; i <= 12; i++) {
|
|
194
|
-
blocks.push({ kind: "tool_call", name: "bash", args: { command: `echo ${i}` } });
|
|
195
|
-
}
|
|
196
|
-
const r = compileBrief(blocks);
|
|
197
|
-
expect(r).toContain("(4 earlier tool-call entries omitted)");
|
|
198
|
-
// Last 8 (5..12) kept; first 4 dropped
|
|
199
|
-
expect(r).not.toContain("echo 1\"");
|
|
200
|
-
expect(r).not.toContain("echo 4\"");
|
|
201
|
-
expect(r).toContain("echo 5");
|
|
202
|
-
expect(r).toContain("echo 12");
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it("does not cap when tool calls per turn <= 8", () => {
|
|
206
|
-
const blocks: NormalizedBlock[] = [{ kind: "assistant", text: "ok" }];
|
|
207
|
-
for (let i = 1; i <= 8; i++) {
|
|
208
|
-
blocks.push({ kind: "tool_call", name: "bash", args: { command: `c${i}` } });
|
|
209
|
-
}
|
|
210
|
-
const r = compileBrief(blocks);
|
|
211
|
-
expect(r).not.toContain("entries omitted");
|
|
212
|
-
expect(r).toContain("c1");
|
|
213
|
-
expect(r).toContain("c8");
|
|
214
|
-
});
|
|
215
|
-
});
|