@vellumai/assistant 0.8.8-dev.202606081143.f600053 → 0.8.8-dev.202606081515.c77a9b6
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/ARCHITECTURE.md +6 -6
- package/package.json +1 -1
- package/src/__tests__/agent-loop-exit-reason.test.ts +33 -18
- package/src/__tests__/compactor-web-search-strip.test.ts +213 -0
- package/src/__tests__/context-overflow-reducer.test.ts +1 -1
- package/src/__tests__/conversation-agent-loop-inference-profile.test.ts +19 -16
- package/src/__tests__/conversation-agent-loop-overflow.test.ts +61 -171
- package/src/__tests__/conversation-agent-loop.test.ts +65 -182
- package/src/__tests__/conversation-history-web-search.test.ts +1 -1
- package/src/__tests__/conversation-provider-retry-repair.test.ts +52 -49
- package/src/__tests__/memory-retrieval-hook.test.ts +68 -30
- package/src/__tests__/pre-model-call-sanitize.test.ts +109 -0
- package/src/agent/loop.ts +42 -26
- package/src/context/compactor.ts +22 -4
- package/src/daemon/conversation-agent-loop.ts +21 -197
- package/src/daemon/conversation-lifecycle.ts +6 -0
- package/src/daemon/conversation.ts +23 -3
- package/src/{daemon → plugins/defaults/compaction}/context-overflow-reducer.ts +7 -7
- package/src/plugins/defaults/compaction/manager-store.ts +13 -0
- package/src/plugins/defaults/memory-retrieval/hooks/post-compact.ts +6 -23
- package/src/plugins/defaults/memory-retrieval/hooks/user-prompt-submit-temp.ts +24 -25
- package/src/__tests__/overflow-reduction-loop.test.ts +0 -384
- package/src/daemon/overflow-reduction-loop.ts +0 -197
package/ARCHITECTURE.md
CHANGED
|
@@ -1088,12 +1088,12 @@ All overflow recovery settings live under `contextWindow.overflowRecovery` in th
|
|
|
1088
1088
|
|
|
1089
1089
|
### Key Source Files
|
|
1090
1090
|
|
|
1091
|
-
| File
|
|
1092
|
-
|
|
|
1093
|
-
| `src/
|
|
1094
|
-
| `src/daemon/context-overflow-policy.ts`
|
|
1095
|
-
| `src/daemon/conversation-agent-loop.ts`
|
|
1096
|
-
| `src/config/core-schema.ts`
|
|
1091
|
+
| File | Purpose |
|
|
1092
|
+
| ------------------------------------------------------------- | ----------------------------------------------------------------------------- |
|
|
1093
|
+
| `src/plugins/defaults/compaction/context-overflow-reducer.ts` | Tiered reducer: four-tier pipeline with idempotent steps and cumulative state |
|
|
1094
|
+
| `src/daemon/context-overflow-policy.ts` | Overflow policy resolver: maps config + interactivity to concrete action |
|
|
1095
|
+
| `src/daemon/conversation-agent-loop.ts` | Integration: preflight budget check, convergence loop, emergency compaction |
|
|
1096
|
+
| `src/config/core-schema.ts` | `ContextOverflowRecoveryConfigSchema` with defaults and validation |
|
|
1097
1097
|
|
|
1098
1098
|
---
|
|
1099
1099
|
|
package/package.json
CHANGED
|
@@ -13,16 +13,21 @@
|
|
|
13
13
|
* Sites not exercised here (`aborted_via_error`) require deeper provider
|
|
14
14
|
* fakery and are best covered by integration tests.
|
|
15
15
|
*/
|
|
16
|
-
import { describe, expect, mock, test } from "bun:test";
|
|
16
|
+
import { afterEach, describe, expect, mock, test } from "bun:test";
|
|
17
17
|
|
|
18
18
|
import type {
|
|
19
19
|
AgentEvent,
|
|
20
|
-
AgentLoopRunOptions,
|
|
21
20
|
CheckpointDecision,
|
|
22
21
|
CheckpointInfo,
|
|
23
22
|
} from "../agent/loop.js";
|
|
24
23
|
import { AgentLoop, isMaxTokensStopReason } from "../agent/loop.js";
|
|
24
|
+
import type { ContextWindowConfig } from "../config/types.js";
|
|
25
25
|
import type { TrustContext } from "../daemon/trust-context.js";
|
|
26
|
+
import {
|
|
27
|
+
createContextWindowManager,
|
|
28
|
+
disposeContextWindowManager,
|
|
29
|
+
getContextWindowManager,
|
|
30
|
+
} from "../plugins/defaults/compaction/manager-store.js";
|
|
26
31
|
import type { PostCompactContext } from "../plugins/defaults/memory-retrieval/hooks/post-compact.js";
|
|
27
32
|
import type {
|
|
28
33
|
Message,
|
|
@@ -118,20 +123,26 @@ const userMessage: Message = {
|
|
|
118
123
|
content: [{ type: "text", text: "Hello" }],
|
|
119
124
|
};
|
|
120
125
|
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
function fakeCompaction(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
} {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
};
|
|
126
|
+
// Register a context-window manager whose `maybeCompact` returns a canned
|
|
127
|
+
// result in the compaction store the loop resolves from, so the loop's
|
|
128
|
+
// compaction call runs without the real orchestrator machinery. Returns the
|
|
129
|
+
// trust snapshot the loop also needs as a run option.
|
|
130
|
+
function fakeCompaction(
|
|
131
|
+
conversationId: string,
|
|
132
|
+
result: { compacted: boolean; exhausted: boolean },
|
|
133
|
+
): { trust: TrustContext } {
|
|
134
|
+
createContextWindowManager({
|
|
135
|
+
provider: { name: "mock-provider" } as unknown as Provider,
|
|
136
|
+
systemPrompt: "system",
|
|
137
|
+
config: {} as unknown as ContextWindowConfig,
|
|
138
|
+
conversationId,
|
|
139
|
+
});
|
|
140
|
+
const manager = getContextWindowManager(conversationId);
|
|
141
|
+
if (manager) {
|
|
142
|
+
manager.maybeCompact = (async () =>
|
|
143
|
+
result) as unknown as typeof manager.maybeCompact;
|
|
144
|
+
}
|
|
145
|
+
return { trust: { sourceChannel: "vellum", trustClass: "unknown" } };
|
|
135
146
|
}
|
|
136
147
|
|
|
137
148
|
function lastExitEvent(
|
|
@@ -152,6 +163,10 @@ function countExitEvents(events: AgentEvent[]): number {
|
|
|
152
163
|
// ---------------------------------------------------------------------------
|
|
153
164
|
|
|
154
165
|
describe("AgentLoop exit-reason instrumentation", () => {
|
|
166
|
+
afterEach(() => {
|
|
167
|
+
disposeContextWindowManager("test-conversation");
|
|
168
|
+
});
|
|
169
|
+
|
|
155
170
|
test("recognizes provider output-token stop reasons", () => {
|
|
156
171
|
expect(isMaxTokensStopReason("max_tokens")).toBe(true);
|
|
157
172
|
expect(isMaxTokensStopReason("MAX_TOKENS")).toBe(true);
|
|
@@ -457,7 +472,7 @@ describe("AgentLoop exit-reason instrumentation", () => {
|
|
|
457
472
|
overflowRecovery: { enabled: true, safetyMarginRatio: 0 },
|
|
458
473
|
}),
|
|
459
474
|
compactInPlace: true,
|
|
460
|
-
...fakeCompaction({
|
|
475
|
+
...fakeCompaction("test-conversation", {
|
|
461
476
|
compacted: true,
|
|
462
477
|
exhausted: false,
|
|
463
478
|
}),
|
|
@@ -502,7 +517,7 @@ describe("AgentLoop exit-reason instrumentation", () => {
|
|
|
502
517
|
overflowRecovery: { enabled: true, safetyMarginRatio: 0 },
|
|
503
518
|
}),
|
|
504
519
|
compactInPlace: true,
|
|
505
|
-
...fakeCompaction({
|
|
520
|
+
...fakeCompaction("test-conversation", {
|
|
506
521
|
compacted: false,
|
|
507
522
|
exhausted: true,
|
|
508
523
|
}),
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { describe, expect, mock, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
function makeLoggerStub(): Record<string, unknown> {
|
|
4
|
+
const stub: Record<string, unknown> = {};
|
|
5
|
+
for (const m of [
|
|
6
|
+
"info",
|
|
7
|
+
"warn",
|
|
8
|
+
"error",
|
|
9
|
+
"debug",
|
|
10
|
+
"trace",
|
|
11
|
+
"fatal",
|
|
12
|
+
"silent",
|
|
13
|
+
"child",
|
|
14
|
+
]) {
|
|
15
|
+
stub[m] = m === "child" ? () => makeLoggerStub() : () => {};
|
|
16
|
+
}
|
|
17
|
+
return stub;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
mock.module("../util/logger.js", () => ({
|
|
21
|
+
getLogger: () => makeLoggerStub(),
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
mock.module("../memory/conversation-crud.js", () => ({
|
|
25
|
+
getMessages: () => [],
|
|
26
|
+
reserveMessage: mock(async () => ({ id: "msg-reserve" })),
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
mock.module("../memory/attachments-store.js", () => ({
|
|
30
|
+
getAttachmentMetadataForMessage: () => [],
|
|
31
|
+
getAttachmentContent: () => null,
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
import {
|
|
35
|
+
runAssistantDrivenCompaction,
|
|
36
|
+
runEmergencyCompaction,
|
|
37
|
+
} from "../context/compactor.js";
|
|
38
|
+
import type { Message, Provider } from "../providers/types.js";
|
|
39
|
+
|
|
40
|
+
const TAIL_TIMESTAMP =
|
|
41
|
+
"2026-05-21 (Thursday) 10:00:00 -05:00 (America/Chicago)";
|
|
42
|
+
|
|
43
|
+
const compactionResponse = `
|
|
44
|
+
<compaction_result>
|
|
45
|
+
<summary>
|
|
46
|
+
Earlier turns summarized here.
|
|
47
|
+
</summary>
|
|
48
|
+
|
|
49
|
+
<key_state>
|
|
50
|
+
- Nothing critical pending.
|
|
51
|
+
</key_state>
|
|
52
|
+
|
|
53
|
+
<tail_start timestamp="${TAIL_TIMESTAMP}" preview="tail anchor message" />
|
|
54
|
+
</compaction_result>
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
const ENCRYPTED_TOKEN = "expired_encrypted_token_abc123";
|
|
58
|
+
|
|
59
|
+
const userText = (text: string): Message => ({
|
|
60
|
+
role: "user",
|
|
61
|
+
content: [{ type: "text", text }],
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const userTextWithTurnContext = (text: string, timestamp: string): Message => ({
|
|
65
|
+
role: "user",
|
|
66
|
+
content: [
|
|
67
|
+
{
|
|
68
|
+
type: "text",
|
|
69
|
+
text: `<turn_context>\ncurrent_time: ${timestamp}\n</turn_context>\n${text}`,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const assistantWithWebSearch = (): Message => ({
|
|
75
|
+
role: "assistant",
|
|
76
|
+
content: [
|
|
77
|
+
{
|
|
78
|
+
type: "server_tool_use",
|
|
79
|
+
id: "stu_1",
|
|
80
|
+
name: "web_search",
|
|
81
|
+
input: { query: "cats" },
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
type: "web_search_tool_result",
|
|
85
|
+
tool_use_id: "stu_1",
|
|
86
|
+
content: [
|
|
87
|
+
{
|
|
88
|
+
type: "web_search_result",
|
|
89
|
+
url: "https://cats.example",
|
|
90
|
+
title: "All About Cats",
|
|
91
|
+
encrypted_content: ENCRYPTED_TOKEN,
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const assistantToolUse = (): Message => ({
|
|
99
|
+
role: "assistant",
|
|
100
|
+
content: [
|
|
101
|
+
{ type: "tool_use", id: "tu_1", name: "bash", input: { cmd: "ls" } },
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const toolResult = (): Message => ({
|
|
106
|
+
role: "user",
|
|
107
|
+
content: [{ type: "tool_result", tool_use_id: "tu_1", content: "ok" }],
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
function serializeBlocks(messages: Message[]): string {
|
|
111
|
+
return JSON.stringify(messages);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Records the exact message list the provider is asked to summarize.
|
|
115
|
+
function recordingProvider(sink: { sent: Message[] }): Provider {
|
|
116
|
+
return {
|
|
117
|
+
name: "mock-provider",
|
|
118
|
+
sendMessage: async (msgs: Message[]) => {
|
|
119
|
+
sink.sent = msgs;
|
|
120
|
+
return {
|
|
121
|
+
content: [{ type: "text", text: compactionResponse }],
|
|
122
|
+
model: "mock-model",
|
|
123
|
+
usage: { inputTokens: 100, outputTokens: 50 },
|
|
124
|
+
stopReason: "end_turn",
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Expired Anthropic web-search tokens must never reach the summarization LLM:
|
|
131
|
+
// both compaction provider calls funnel through the same request builder, so
|
|
132
|
+
// each is guarded here.
|
|
133
|
+
describe("compaction summary calls — historical web-search sanitization", () => {
|
|
134
|
+
test("runAssistantDrivenCompaction strips expired encrypted_content from the request while leaving durable history untouched", async () => {
|
|
135
|
+
// GIVEN a history whose older turn contains a native web_search_tool_result
|
|
136
|
+
// block carrying an opaque, expired `encrypted_content` token
|
|
137
|
+
const messages: Message[] = [
|
|
138
|
+
userText("old user turn"),
|
|
139
|
+
assistantWithWebSearch(),
|
|
140
|
+
userTextWithTurnContext("tail anchor message", TAIL_TIMESTAMP),
|
|
141
|
+
assistantWithWebSearch(),
|
|
142
|
+
];
|
|
143
|
+
|
|
144
|
+
// AND a snapshot of the caller's array so we can assert it is not mutated
|
|
145
|
+
const inputSnapshot = serializeBlocks(messages);
|
|
146
|
+
const sink = { sent: [] as Message[] };
|
|
147
|
+
|
|
148
|
+
// WHEN compaction runs over that history
|
|
149
|
+
const result = await runAssistantDrivenCompaction({
|
|
150
|
+
conversationId: "conv-web-search",
|
|
151
|
+
messages,
|
|
152
|
+
provider: recordingProvider(sink),
|
|
153
|
+
systemPrompt: "system",
|
|
154
|
+
compaction: { enabled: true, autoThreshold: 0.7 },
|
|
155
|
+
maxInputTokens: 1000,
|
|
156
|
+
previousEstimatedInputTokens: 900,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// THEN the summary request the provider received carries no expired token
|
|
160
|
+
// and no native web_search blocks, but keeps the search content as text
|
|
161
|
+
const sentSerialized = serializeBlocks(sink.sent);
|
|
162
|
+
expect(sentSerialized).not.toContain(ENCRYPTED_TOKEN);
|
|
163
|
+
expect(sentSerialized).not.toContain("web_search_tool_result");
|
|
164
|
+
expect(sentSerialized).not.toContain("server_tool_use");
|
|
165
|
+
expect(sentSerialized).toContain("All About Cats");
|
|
166
|
+
expect(sentSerialized).toContain("https://cats.example");
|
|
167
|
+
|
|
168
|
+
// AND the caller's durable history array is left byte-for-byte untouched,
|
|
169
|
+
// so persisted messages keep the original rich blocks
|
|
170
|
+
expect(serializeBlocks(messages)).toBe(inputSnapshot);
|
|
171
|
+
expect(result.compacted).toBe(true);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("runEmergencyCompaction strips expired encrypted_content from the prefix request", async () => {
|
|
175
|
+
// GIVEN a mid-turn history whose prefix (before the last tool pair) holds a
|
|
176
|
+
// web_search_tool_result block with an expired token
|
|
177
|
+
const messages: Message[] = [
|
|
178
|
+
userText("old user turn"),
|
|
179
|
+
assistantWithWebSearch(),
|
|
180
|
+
userText("keep going"),
|
|
181
|
+
assistantToolUse(),
|
|
182
|
+
toolResult(),
|
|
183
|
+
];
|
|
184
|
+
|
|
185
|
+
const inputSnapshot = serializeBlocks(messages);
|
|
186
|
+
const sink = { sent: [] as Message[] };
|
|
187
|
+
|
|
188
|
+
// WHEN emergency compaction splits at the last tool pair and summarizes the
|
|
189
|
+
// prefix
|
|
190
|
+
const result = await runEmergencyCompaction({
|
|
191
|
+
conversationId: "conv-web-search-emergency",
|
|
192
|
+
messages,
|
|
193
|
+
provider: recordingProvider(sink),
|
|
194
|
+
systemPrompt: "system",
|
|
195
|
+
compaction: { enabled: true, autoThreshold: 0.7 },
|
|
196
|
+
// Large budget so the prefix is not front-truncated — this test exercises
|
|
197
|
+
// the web-search sanitizer, not the truncation fallback.
|
|
198
|
+
maxInputTokens: 100000,
|
|
199
|
+
previousEstimatedInputTokens: 90000,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// THEN the prefix sent to the provider carries no expired token
|
|
203
|
+
const sentSerialized = serializeBlocks(sink.sent);
|
|
204
|
+
expect(sentSerialized).not.toContain(ENCRYPTED_TOKEN);
|
|
205
|
+
expect(sentSerialized).not.toContain("web_search_tool_result");
|
|
206
|
+
expect(sentSerialized).not.toContain("server_tool_use");
|
|
207
|
+
expect(sentSerialized).toContain("All About Cats");
|
|
208
|
+
|
|
209
|
+
// AND the caller's durable history is untouched
|
|
210
|
+
expect(serializeBlocks(messages)).toBe(inputSnapshot);
|
|
211
|
+
expect(result.compacted).toBe(true);
|
|
212
|
+
});
|
|
213
|
+
});
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
reduceContextOverflow,
|
|
12
12
|
type ReducerConfig,
|
|
13
13
|
type ReducerState,
|
|
14
|
-
} from "../
|
|
14
|
+
} from "../plugins/defaults/compaction/context-overflow-reducer.js";
|
|
15
15
|
import type { Message } from "../providers/types.js";
|
|
16
16
|
|
|
17
17
|
function msg(role: "user" | "assistant", text: string): Message {
|
|
@@ -102,23 +102,26 @@ mock.module("../context/token-estimator.js", () => ({
|
|
|
102
102
|
estimateToolsTokens: () => 0,
|
|
103
103
|
}));
|
|
104
104
|
|
|
105
|
-
mock.module(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
105
|
+
mock.module(
|
|
106
|
+
"../plugins/defaults/compaction/context-overflow-reducer.js",
|
|
107
|
+
() => ({
|
|
108
|
+
createInitialReducerState: () => ({
|
|
109
|
+
appliedTiers: [],
|
|
110
|
+
injectionMode: "full" as const,
|
|
111
|
+
exhausted: false,
|
|
112
|
+
}),
|
|
113
|
+
reduceContextOverflow: async (msgs: Message[]) => ({
|
|
114
|
+
messages: msgs,
|
|
115
|
+
tier: "forced_compaction",
|
|
116
|
+
state: {
|
|
117
|
+
appliedTiers: ["forced_compaction"],
|
|
118
|
+
injectionMode: "full",
|
|
119
|
+
exhausted: true,
|
|
120
|
+
},
|
|
121
|
+
estimatedTokens: 1000,
|
|
122
|
+
}),
|
|
120
123
|
}),
|
|
121
|
-
|
|
124
|
+
);
|
|
122
125
|
|
|
123
126
|
mock.module("../daemon/context-overflow-policy.js", () => ({
|
|
124
127
|
resolveOverflowAction: () => "fail_gracefully",
|