@vellumai/assistant 0.5.3 → 0.5.5
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/Dockerfile +18 -27
- package/docs/architecture/memory.md +105 -0
- package/node_modules/@vellumai/ces-contracts/src/index.ts +1 -0
- package/node_modules/@vellumai/ces-contracts/src/trust-rules.ts +42 -0
- package/package.json +1 -1
- package/src/__tests__/archive-recall.test.ts +560 -0
- package/src/__tests__/conversation-clear-safety.test.ts +259 -0
- package/src/__tests__/conversation-switch-memory-reduction.test.ts +474 -0
- package/src/__tests__/credential-security-invariants.test.ts +2 -0
- package/src/__tests__/db-schedule-syntax-migration.test.ts +3 -0
- package/src/__tests__/memory-reducer-job.test.ts +538 -0
- package/src/__tests__/memory-reducer-scheduling.test.ts +473 -0
- package/src/__tests__/memory-reducer-types.test.ts +12 -4
- package/src/__tests__/memory-reducer.test.ts +7 -1
- package/src/__tests__/memory-regressions.test.ts +24 -4
- package/src/__tests__/memory-simplified-config.test.ts +4 -4
- package/src/__tests__/openai-whisper.test.ts +93 -0
- package/src/__tests__/simplified-memory-e2e.test.ts +666 -0
- package/src/__tests__/simplified-memory-runtime.test.ts +616 -0
- package/src/__tests__/slack-messaging-token-resolution.test.ts +319 -0
- package/src/__tests__/volume-security-guard.test.ts +155 -0
- package/src/cli/commands/conversations.ts +18 -0
- package/src/config/bundled-skills/messaging/tools/shared.ts +1 -0
- package/src/config/bundled-skills/schedule/TOOLS.json +8 -0
- package/src/config/bundled-skills/transcribe/tools/transcribe-media.ts +16 -37
- package/src/config/env-registry.ts +9 -0
- package/src/config/feature-flag-registry.json +8 -0
- package/src/config/loader.ts +0 -1
- package/src/config/schemas/memory-simplified.ts +1 -1
- package/src/credential-execution/managed-catalog.ts +5 -15
- package/src/daemon/config-watcher.ts +4 -1
- package/src/daemon/conversation-memory.ts +117 -0
- package/src/daemon/conversation-runtime-assembly.ts +1 -0
- package/src/daemon/daemon-control.ts +7 -0
- package/src/daemon/handlers/conversations.ts +11 -0
- package/src/daemon/lifecycle.ts +51 -2
- package/src/daemon/providers-setup.ts +2 -1
- package/src/hooks/manager.ts +7 -0
- package/src/instrument.ts +33 -1
- package/src/memory/archive-recall.ts +516 -0
- package/src/memory/brief-time.ts +5 -4
- package/src/memory/conversation-crud.ts +210 -0
- package/src/memory/conversation-key-store.ts +33 -4
- package/src/memory/db-init.ts +4 -0
- package/src/memory/embedding-local.ts +11 -5
- package/src/memory/job-handlers/backfill-simplified-memory.ts +462 -0
- package/src/memory/job-handlers/conversation-starters.ts +24 -30
- package/src/memory/job-handlers/reduce-conversation-memory.ts +229 -0
- package/src/memory/jobs-store.ts +2 -0
- package/src/memory/jobs-worker.ts +8 -0
- package/src/memory/migrations/036-normalize-phone-identities.ts +49 -14
- package/src/memory/migrations/135-backfill-contact-interaction-stats.ts +9 -1
- package/src/memory/migrations/141-rename-verification-table.ts +8 -0
- package/src/memory/migrations/142-rename-verification-session-id-column.ts +7 -2
- package/src/memory/migrations/174-rename-thread-starters-table.ts +8 -0
- package/src/memory/migrations/188-schedule-quiet-flag.ts +13 -0
- package/src/memory/migrations/index.ts +1 -0
- package/src/memory/reducer-scheduler.ts +242 -0
- package/src/memory/reducer-types.ts +9 -2
- package/src/memory/reducer.ts +25 -11
- package/src/memory/schema/infrastructure.ts +1 -0
- package/src/messaging/provider.ts +9 -0
- package/src/messaging/providers/slack/adapter.ts +29 -2
- package/src/oauth/connection-resolver.test.ts +22 -18
- package/src/oauth/connection-resolver.ts +92 -7
- package/src/oauth/platform-connection.test.ts +78 -69
- package/src/oauth/platform-connection.ts +12 -19
- package/src/permissions/trust-client.ts +343 -0
- package/src/permissions/trust-store-interface.ts +105 -0
- package/src/permissions/trust-store.ts +523 -36
- package/src/platform/client.test.ts +148 -0
- package/src/platform/client.ts +71 -0
- package/src/providers/speech-to-text/openai-whisper.test.ts +190 -0
- package/src/providers/speech-to-text/openai-whisper.ts +68 -0
- package/src/providers/speech-to-text/resolve.ts +9 -0
- package/src/providers/speech-to-text/types.ts +17 -0
- package/src/runtime/auth/route-policy.ts +10 -1
- package/src/runtime/http-server.ts +2 -2
- package/src/runtime/routes/conversation-management-routes.ts +88 -2
- package/src/runtime/routes/guardian-bootstrap-routes.ts +19 -7
- package/src/runtime/routes/inbound-message-handler.ts +27 -3
- package/src/runtime/routes/inbound-stages/acl-enforcement.ts +16 -1
- package/src/runtime/routes/inbound-stages/transcribe-audio.test.ts +287 -0
- package/src/runtime/routes/inbound-stages/transcribe-audio.ts +122 -0
- package/src/runtime/routes/log-export-routes.ts +1 -0
- package/src/runtime/routes/secret-routes.ts +5 -1
- package/src/schedule/schedule-store.ts +7 -0
- package/src/schedule/scheduler.ts +6 -2
- package/src/security/ces-credential-client.ts +173 -0
- package/src/security/secure-keys.ts +65 -22
- package/src/signals/bash.ts +3 -0
- package/src/signals/cancel.ts +3 -0
- package/src/signals/confirm.ts +3 -0
- package/src/signals/conversation-undo.ts +3 -0
- package/src/signals/event-stream.ts +7 -0
- package/src/signals/shotgun.ts +3 -0
- package/src/signals/trust-rule.ts +3 -0
- package/src/telemetry/usage-telemetry-reporter.test.ts +23 -36
- package/src/telemetry/usage-telemetry-reporter.ts +22 -20
- package/src/tools/filesystem/edit.ts +6 -1
- package/src/tools/filesystem/read.ts +6 -1
- package/src/tools/filesystem/write.ts +6 -1
- package/src/tools/memory/handlers.ts +129 -1
- package/src/tools/schedule/create.ts +3 -0
- package/src/tools/schedule/list.ts +5 -1
- package/src/tools/schedule/update.ts +6 -0
- package/src/util/device-id.ts +70 -7
- package/src/util/logger.ts +35 -9
- package/src/util/platform.ts +29 -5
- package/src/workspace/migrations/migrate-to-workspace-volume.ts +113 -0
- package/src/workspace/migrations/registry.ts +2 -0
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { afterAll, beforeEach, describe, expect, mock, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
// ── Test directory & platform mocks ───────────────────────────────
|
|
7
|
+
|
|
8
|
+
const testDir = mkdtempSync(join(tmpdir(), "reducer-job-test-"));
|
|
9
|
+
|
|
10
|
+
mock.module("../util/platform.js", () => ({
|
|
11
|
+
getDataDir: () => testDir,
|
|
12
|
+
getRootDir: () => testDir,
|
|
13
|
+
isMacOS: () => process.platform === "darwin",
|
|
14
|
+
isLinux: () => process.platform === "linux",
|
|
15
|
+
isWindows: () => process.platform === "win32",
|
|
16
|
+
getPidPath: () => join(testDir, "test.pid"),
|
|
17
|
+
getDbPath: () => join(testDir, "test.db"),
|
|
18
|
+
getLogPath: () => join(testDir, "test.log"),
|
|
19
|
+
ensureDataDir: () => {},
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
mock.module("../util/logger.js", () => ({
|
|
23
|
+
getLogger: () =>
|
|
24
|
+
new Proxy({} as Record<string, unknown>, {
|
|
25
|
+
get: () => () => {},
|
|
26
|
+
}),
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
// ── Mock the reducer ──────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
import type { ReducerPromptInput } from "../memory/reducer.js";
|
|
32
|
+
import type { ReducerResult } from "../memory/reducer-types.js";
|
|
33
|
+
import { EMPTY_REDUCER_RESULT } from "../memory/reducer-types.js";
|
|
34
|
+
|
|
35
|
+
let mockReducerResult: ReducerResult = EMPTY_REDUCER_RESULT;
|
|
36
|
+
let lastReducerInput: ReducerPromptInput | null = null;
|
|
37
|
+
|
|
38
|
+
mock.module("../memory/reducer.js", () => ({
|
|
39
|
+
runReducer: async (input: ReducerPromptInput) => {
|
|
40
|
+
lastReducerInput = input;
|
|
41
|
+
return mockReducerResult;
|
|
42
|
+
},
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
// ── Imports (after mocks) ─────────────────────────────────────────
|
|
46
|
+
|
|
47
|
+
import { initializeDb, resetDb } from "../memory/db.js";
|
|
48
|
+
import { getSqlite } from "../memory/db-connection.js";
|
|
49
|
+
import { reduceConversationMemoryJob } from "../memory/job-handlers/reduce-conversation-memory.js";
|
|
50
|
+
import type { MemoryJob } from "../memory/jobs-store.js";
|
|
51
|
+
import { resetTestTables } from "../memory/raw-query.js";
|
|
52
|
+
|
|
53
|
+
initializeDb();
|
|
54
|
+
|
|
55
|
+
// ── Helpers ───────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
const SCOPE = "test-scope";
|
|
58
|
+
const NOW = 1_700_000_000_000;
|
|
59
|
+
const HOUR = 60 * 60 * 1000;
|
|
60
|
+
|
|
61
|
+
function insertConversation(
|
|
62
|
+
id: string,
|
|
63
|
+
opts?: {
|
|
64
|
+
dirtyTailMessageId?: string;
|
|
65
|
+
reducedThroughMessageId?: string;
|
|
66
|
+
contextSummary?: string;
|
|
67
|
+
memoryScopeId?: string;
|
|
68
|
+
},
|
|
69
|
+
): void {
|
|
70
|
+
const raw = getSqlite();
|
|
71
|
+
raw.run(
|
|
72
|
+
`INSERT INTO conversations (id, title, created_at, updated_at, conversation_type, source, memory_scope_id, is_auto_title,
|
|
73
|
+
memory_dirty_tail_since_message_id, memory_reduced_through_message_id, context_summary)
|
|
74
|
+
VALUES (?, 'Test', ?, ?, 'standard', 'user', ?, 1, ?, ?, ?)`,
|
|
75
|
+
[
|
|
76
|
+
id,
|
|
77
|
+
NOW,
|
|
78
|
+
NOW,
|
|
79
|
+
opts?.memoryScopeId ?? SCOPE,
|
|
80
|
+
opts?.dirtyTailMessageId ?? null,
|
|
81
|
+
opts?.reducedThroughMessageId ?? null,
|
|
82
|
+
opts?.contextSummary ?? null,
|
|
83
|
+
],
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function insertMessage(opts: {
|
|
88
|
+
id: string;
|
|
89
|
+
conversationId: string;
|
|
90
|
+
role?: string;
|
|
91
|
+
content?: string;
|
|
92
|
+
createdAt?: number;
|
|
93
|
+
}): void {
|
|
94
|
+
const raw = getSqlite();
|
|
95
|
+
raw.run(
|
|
96
|
+
`INSERT INTO messages (id, conversation_id, role, content, created_at)
|
|
97
|
+
VALUES (?, ?, ?, ?, ?)`,
|
|
98
|
+
[
|
|
99
|
+
opts.id,
|
|
100
|
+
opts.conversationId,
|
|
101
|
+
opts.role ?? "user",
|
|
102
|
+
opts.content ?? "test message",
|
|
103
|
+
opts.createdAt ?? NOW,
|
|
104
|
+
],
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function getRawConversation(conversationId: string): Record<string, unknown> {
|
|
109
|
+
const raw = getSqlite();
|
|
110
|
+
return raw
|
|
111
|
+
.query(`SELECT * FROM conversations WHERE id = ?`)
|
|
112
|
+
.get(conversationId) as Record<string, unknown>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function makeJob(conversationId: string): MemoryJob {
|
|
116
|
+
return {
|
|
117
|
+
id: "job-1",
|
|
118
|
+
type: "reduce_conversation_memory",
|
|
119
|
+
payload: { conversationId },
|
|
120
|
+
status: "running",
|
|
121
|
+
attempts: 0,
|
|
122
|
+
deferrals: 0,
|
|
123
|
+
runAfter: NOW,
|
|
124
|
+
lastError: null,
|
|
125
|
+
startedAt: NOW,
|
|
126
|
+
createdAt: NOW,
|
|
127
|
+
updatedAt: NOW,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function makeReducerResult(overrides?: Partial<ReducerResult>): ReducerResult {
|
|
132
|
+
return {
|
|
133
|
+
timeContexts: [],
|
|
134
|
+
openLoops: [],
|
|
135
|
+
archiveObservations: [],
|
|
136
|
+
archiveEpisodes: [],
|
|
137
|
+
...overrides,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ── Teardown ──────────────────────────────────────────────────────
|
|
142
|
+
|
|
143
|
+
afterAll(() => {
|
|
144
|
+
resetDb();
|
|
145
|
+
try {
|
|
146
|
+
rmSync(testDir, { recursive: true });
|
|
147
|
+
} catch {
|
|
148
|
+
/* best effort */
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
beforeEach(() => {
|
|
153
|
+
resetTestTables("messages", "conversations", "time_contexts", "open_loops");
|
|
154
|
+
mockReducerResult = EMPTY_REDUCER_RESULT;
|
|
155
|
+
lastReducerInput = null;
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// ── Tests ─────────────────────────────────────────────────────────
|
|
159
|
+
|
|
160
|
+
describe("reduceConversationMemoryJob", () => {
|
|
161
|
+
describe("successful reduction", () => {
|
|
162
|
+
test("reduces dirty conversation and advances checkpoint", async () => {
|
|
163
|
+
insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
|
|
164
|
+
insertMessage({
|
|
165
|
+
id: "msg-1",
|
|
166
|
+
conversationId: "conv-1",
|
|
167
|
+
role: "user",
|
|
168
|
+
content: "Hello there",
|
|
169
|
+
createdAt: NOW,
|
|
170
|
+
});
|
|
171
|
+
insertMessage({
|
|
172
|
+
id: "msg-2",
|
|
173
|
+
conversationId: "conv-1",
|
|
174
|
+
role: "assistant",
|
|
175
|
+
content: "Hi! How can I help?",
|
|
176
|
+
createdAt: NOW + 1000,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
mockReducerResult = makeReducerResult({
|
|
180
|
+
openLoops: [
|
|
181
|
+
{
|
|
182
|
+
action: "create",
|
|
183
|
+
summary: "User needs help with something",
|
|
184
|
+
source: "conversation",
|
|
185
|
+
},
|
|
186
|
+
],
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
190
|
+
|
|
191
|
+
// Checkpoint should advance to the last message
|
|
192
|
+
const conv = getRawConversation("conv-1");
|
|
193
|
+
expect(conv.memory_reduced_through_message_id).toBe("msg-2");
|
|
194
|
+
expect(conv.memory_last_reduced_at).toBeGreaterThan(0);
|
|
195
|
+
// Dirty tail should be cleared since all messages are now reduced
|
|
196
|
+
expect(conv.memory_dirty_tail_since_message_id).toBeNull();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test("passes unreduced messages to the reducer", async () => {
|
|
200
|
+
insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
|
|
201
|
+
insertMessage({
|
|
202
|
+
id: "msg-1",
|
|
203
|
+
conversationId: "conv-1",
|
|
204
|
+
role: "user",
|
|
205
|
+
content: "First message",
|
|
206
|
+
createdAt: NOW,
|
|
207
|
+
});
|
|
208
|
+
insertMessage({
|
|
209
|
+
id: "msg-2",
|
|
210
|
+
conversationId: "conv-1",
|
|
211
|
+
role: "assistant",
|
|
212
|
+
content: "Second message",
|
|
213
|
+
createdAt: NOW + 1000,
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
mockReducerResult = makeReducerResult();
|
|
217
|
+
|
|
218
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
219
|
+
|
|
220
|
+
expect(lastReducerInput).not.toBeNull();
|
|
221
|
+
expect(lastReducerInput!.conversationId).toBe("conv-1");
|
|
222
|
+
expect(lastReducerInput!.newMessages).toHaveLength(2);
|
|
223
|
+
expect(lastReducerInput!.newMessages[0].role).toBe("user");
|
|
224
|
+
expect(lastReducerInput!.newMessages[0].content).toBe("First message");
|
|
225
|
+
expect(lastReducerInput!.newMessages[1].role).toBe("assistant");
|
|
226
|
+
expect(lastReducerInput!.newMessages[1].content).toBe("Second message");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("includes contextSummary as synthetic system message when present", async () => {
|
|
230
|
+
insertConversation("conv-1", {
|
|
231
|
+
dirtyTailMessageId: "msg-1",
|
|
232
|
+
contextSummary: "User is working on a TypeScript project",
|
|
233
|
+
});
|
|
234
|
+
insertMessage({
|
|
235
|
+
id: "msg-1",
|
|
236
|
+
conversationId: "conv-1",
|
|
237
|
+
role: "user",
|
|
238
|
+
content: "Can you help with this bug?",
|
|
239
|
+
createdAt: NOW,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
mockReducerResult = makeReducerResult();
|
|
243
|
+
|
|
244
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
245
|
+
|
|
246
|
+
expect(lastReducerInput).not.toBeNull();
|
|
247
|
+
// contextSummary should be prepended as a system message
|
|
248
|
+
expect(lastReducerInput!.newMessages).toHaveLength(2);
|
|
249
|
+
expect(lastReducerInput!.newMessages[0].role).toBe("system");
|
|
250
|
+
expect(lastReducerInput!.newMessages[0].content).toContain(
|
|
251
|
+
"User is working on a TypeScript project",
|
|
252
|
+
);
|
|
253
|
+
// Real message follows
|
|
254
|
+
expect(lastReducerInput!.newMessages[1].role).toBe("user");
|
|
255
|
+
expect(lastReducerInput!.newMessages[1].content).toBe(
|
|
256
|
+
"Can you help with this bug?",
|
|
257
|
+
);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("loads active time contexts and open loops for the reducer", async () => {
|
|
261
|
+
insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
|
|
262
|
+
insertMessage({
|
|
263
|
+
id: "msg-1",
|
|
264
|
+
conversationId: "conv-1",
|
|
265
|
+
role: "user",
|
|
266
|
+
content: "test",
|
|
267
|
+
createdAt: NOW,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Insert pre-existing active time context.
|
|
271
|
+
// Use a far-future activeUntil so it is still active at Date.now().
|
|
272
|
+
const farFuture = Date.now() + 365 * 24 * HOUR;
|
|
273
|
+
const raw = getSqlite();
|
|
274
|
+
raw.run(
|
|
275
|
+
`INSERT INTO time_contexts (id, scope_id, summary, source, active_from, active_until, created_at, updated_at)
|
|
276
|
+
VALUES ('tc-1', ?, 'User on vacation next week', 'conversation', ?, ?, ?, ?)`,
|
|
277
|
+
[SCOPE, NOW, farFuture, NOW, NOW],
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
// Insert pre-existing open loop
|
|
281
|
+
raw.run(
|
|
282
|
+
`INSERT INTO open_loops (id, scope_id, summary, status, source, created_at, updated_at)
|
|
283
|
+
VALUES ('ol-1', ?, 'Waiting for deploy', 'open', 'conversation', ?, ?)`,
|
|
284
|
+
[SCOPE, NOW, NOW],
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
mockReducerResult = makeReducerResult();
|
|
288
|
+
|
|
289
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
290
|
+
|
|
291
|
+
expect(lastReducerInput).not.toBeNull();
|
|
292
|
+
expect(lastReducerInput!.existingTimeContexts).toHaveLength(1);
|
|
293
|
+
expect(lastReducerInput!.existingTimeContexts[0].id).toBe("tc-1");
|
|
294
|
+
expect(lastReducerInput!.existingTimeContexts[0].summary).toBe(
|
|
295
|
+
"User on vacation next week",
|
|
296
|
+
);
|
|
297
|
+
expect(lastReducerInput!.existingOpenLoops).toHaveLength(1);
|
|
298
|
+
expect(lastReducerInput!.existingOpenLoops[0].id).toBe("ol-1");
|
|
299
|
+
expect(lastReducerInput!.existingOpenLoops[0].summary).toBe(
|
|
300
|
+
"Waiting for deploy",
|
|
301
|
+
);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test("creates time contexts and open loops from reducer output", async () => {
|
|
305
|
+
insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
|
|
306
|
+
insertMessage({
|
|
307
|
+
id: "msg-1",
|
|
308
|
+
conversationId: "conv-1",
|
|
309
|
+
role: "user",
|
|
310
|
+
content: "I'm going on vacation next week",
|
|
311
|
+
createdAt: NOW,
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
mockReducerResult = makeReducerResult({
|
|
315
|
+
timeContexts: [
|
|
316
|
+
{
|
|
317
|
+
action: "create",
|
|
318
|
+
summary: "User on vacation next week",
|
|
319
|
+
source: "conversation",
|
|
320
|
+
activeFrom: NOW,
|
|
321
|
+
activeUntil: NOW + 7 * 24 * HOUR,
|
|
322
|
+
},
|
|
323
|
+
],
|
|
324
|
+
openLoops: [
|
|
325
|
+
{
|
|
326
|
+
action: "create",
|
|
327
|
+
summary: "Set up OOO auto-reply",
|
|
328
|
+
source: "conversation",
|
|
329
|
+
},
|
|
330
|
+
],
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
334
|
+
|
|
335
|
+
// Verify time context was created
|
|
336
|
+
const raw = getSqlite();
|
|
337
|
+
const contexts = raw
|
|
338
|
+
.query(`SELECT * FROM time_contexts WHERE scope_id = ?`)
|
|
339
|
+
.all(SCOPE) as Array<Record<string, unknown>>;
|
|
340
|
+
expect(contexts).toHaveLength(1);
|
|
341
|
+
expect(contexts[0].summary).toBe("User on vacation next week");
|
|
342
|
+
|
|
343
|
+
// Verify open loop was created
|
|
344
|
+
const loops = raw
|
|
345
|
+
.query(`SELECT * FROM open_loops WHERE scope_id = ?`)
|
|
346
|
+
.all(SCOPE) as Array<Record<string, unknown>>;
|
|
347
|
+
expect(loops).toHaveLength(1);
|
|
348
|
+
expect(loops[0].summary).toBe("Set up OOO auto-reply");
|
|
349
|
+
expect(loops[0].status).toBe("open");
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
describe("empty dirty tails", () => {
|
|
354
|
+
test("no-ops when conversation has no dirty tail marker", async () => {
|
|
355
|
+
insertConversation("conv-1"); // no dirtyTailMessageId
|
|
356
|
+
insertMessage({
|
|
357
|
+
id: "msg-1",
|
|
358
|
+
conversationId: "conv-1",
|
|
359
|
+
role: "user",
|
|
360
|
+
content: "test",
|
|
361
|
+
createdAt: NOW,
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
365
|
+
|
|
366
|
+
// Reducer should not have been called
|
|
367
|
+
expect(lastReducerInput).toBeNull();
|
|
368
|
+
|
|
369
|
+
// Conversation unchanged
|
|
370
|
+
const conv = getRawConversation("conv-1");
|
|
371
|
+
expect(conv.memory_reduced_through_message_id).toBeNull();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
test("no-ops when dirty tail message no longer exists", async () => {
|
|
375
|
+
insertConversation("conv-1", {
|
|
376
|
+
dirtyTailMessageId: "deleted-msg",
|
|
377
|
+
});
|
|
378
|
+
// No messages inserted — the dirty tail message doesn't exist
|
|
379
|
+
|
|
380
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
381
|
+
|
|
382
|
+
// Reducer should not have been called
|
|
383
|
+
expect(lastReducerInput).toBeNull();
|
|
384
|
+
|
|
385
|
+
// Conversation unchanged
|
|
386
|
+
const conv = getRawConversation("conv-1");
|
|
387
|
+
expect(conv.memory_reduced_through_message_id).toBeNull();
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
test("no-ops when conversation does not exist", async () => {
|
|
391
|
+
await reduceConversationMemoryJob(makeJob("nonexistent-conv"));
|
|
392
|
+
|
|
393
|
+
expect(lastReducerInput).toBeNull();
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
test("no-ops when payload has no conversationId", async () => {
|
|
397
|
+
const job: MemoryJob = {
|
|
398
|
+
id: "job-1",
|
|
399
|
+
type: "reduce_conversation_memory",
|
|
400
|
+
payload: {},
|
|
401
|
+
status: "running",
|
|
402
|
+
attempts: 0,
|
|
403
|
+
deferrals: 0,
|
|
404
|
+
runAfter: NOW,
|
|
405
|
+
lastError: null,
|
|
406
|
+
startedAt: NOW,
|
|
407
|
+
createdAt: NOW,
|
|
408
|
+
updatedAt: NOW,
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
await reduceConversationMemoryJob(job);
|
|
412
|
+
|
|
413
|
+
expect(lastReducerInput).toBeNull();
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
describe("reducer failure safety", () => {
|
|
418
|
+
test("does not advance checkpoint when reducer returns EMPTY_REDUCER_RESULT", async () => {
|
|
419
|
+
insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
|
|
420
|
+
insertMessage({
|
|
421
|
+
id: "msg-1",
|
|
422
|
+
conversationId: "conv-1",
|
|
423
|
+
role: "user",
|
|
424
|
+
content: "test",
|
|
425
|
+
createdAt: NOW,
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
// The default mockReducerResult is EMPTY_REDUCER_RESULT
|
|
429
|
+
mockReducerResult = EMPTY_REDUCER_RESULT;
|
|
430
|
+
|
|
431
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
432
|
+
|
|
433
|
+
// Checkpoint should NOT have advanced
|
|
434
|
+
const conv = getRawConversation("conv-1");
|
|
435
|
+
expect(conv.memory_reduced_through_message_id).toBeNull();
|
|
436
|
+
expect(conv.memory_last_reduced_at).toBeNull();
|
|
437
|
+
// Dirty tail stays in place for retry
|
|
438
|
+
expect(conv.memory_dirty_tail_since_message_id).toBe("msg-1");
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
test("does not advance checkpoint when reducer throws", async () => {
|
|
442
|
+
insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
|
|
443
|
+
insertMessage({
|
|
444
|
+
id: "msg-1",
|
|
445
|
+
conversationId: "conv-1",
|
|
446
|
+
role: "user",
|
|
447
|
+
content: "test",
|
|
448
|
+
createdAt: NOW,
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
// Temporarily replace the mock to throw
|
|
452
|
+
mock.module("../memory/reducer.js", () => ({
|
|
453
|
+
runReducer: async (input: ReducerPromptInput) => {
|
|
454
|
+
lastReducerInput = input;
|
|
455
|
+
throw new Error("Provider timeout");
|
|
456
|
+
},
|
|
457
|
+
}));
|
|
458
|
+
|
|
459
|
+
try {
|
|
460
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
461
|
+
} catch {
|
|
462
|
+
// Error propagation is expected — the job worker handles classification
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// Restore the normal mock for subsequent tests
|
|
466
|
+
mock.module("../memory/reducer.js", () => ({
|
|
467
|
+
runReducer: async (input: ReducerPromptInput) => {
|
|
468
|
+
lastReducerInput = input;
|
|
469
|
+
return mockReducerResult;
|
|
470
|
+
},
|
|
471
|
+
}));
|
|
472
|
+
|
|
473
|
+
// Regardless of error handling, checkpoint must not advance
|
|
474
|
+
const conv = getRawConversation("conv-1");
|
|
475
|
+
expect(conv.memory_reduced_through_message_id).toBeNull();
|
|
476
|
+
expect(conv.memory_dirty_tail_since_message_id).toBe("msg-1");
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
test("partial dirty tail preserved when more messages arrive during reduction", async () => {
|
|
480
|
+
insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
|
|
481
|
+
insertMessage({
|
|
482
|
+
id: "msg-1",
|
|
483
|
+
conversationId: "conv-1",
|
|
484
|
+
role: "user",
|
|
485
|
+
content: "First",
|
|
486
|
+
createdAt: NOW,
|
|
487
|
+
});
|
|
488
|
+
insertMessage({
|
|
489
|
+
id: "msg-2",
|
|
490
|
+
conversationId: "conv-1",
|
|
491
|
+
role: "assistant",
|
|
492
|
+
content: "Response",
|
|
493
|
+
createdAt: NOW + 1000,
|
|
494
|
+
});
|
|
495
|
+
// msg-3 arrives "later" — simulates a message added during/after reduction
|
|
496
|
+
insertMessage({
|
|
497
|
+
id: "msg-3",
|
|
498
|
+
conversationId: "conv-1",
|
|
499
|
+
role: "user",
|
|
500
|
+
content: "Follow-up",
|
|
501
|
+
createdAt: NOW + 5000,
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
mockReducerResult = makeReducerResult();
|
|
505
|
+
|
|
506
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
507
|
+
|
|
508
|
+
const conv = getRawConversation("conv-1");
|
|
509
|
+
// All three messages were loaded (they all exist at query time), so
|
|
510
|
+
// checkpoint advances through msg-3
|
|
511
|
+
expect(conv.memory_reduced_through_message_id).toBe("msg-3");
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
describe("scope isolation", () => {
|
|
516
|
+
test("uses the conversation's memoryScopeId for context lookups", async () => {
|
|
517
|
+
const customScope = "custom-scope";
|
|
518
|
+
insertConversation("conv-1", {
|
|
519
|
+
dirtyTailMessageId: "msg-1",
|
|
520
|
+
memoryScopeId: customScope,
|
|
521
|
+
});
|
|
522
|
+
insertMessage({
|
|
523
|
+
id: "msg-1",
|
|
524
|
+
conversationId: "conv-1",
|
|
525
|
+
role: "user",
|
|
526
|
+
content: "test",
|
|
527
|
+
createdAt: NOW,
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
mockReducerResult = makeReducerResult();
|
|
531
|
+
|
|
532
|
+
await reduceConversationMemoryJob(makeJob("conv-1"));
|
|
533
|
+
|
|
534
|
+
expect(lastReducerInput).not.toBeNull();
|
|
535
|
+
expect(lastReducerInput!.scopeId).toBe(customScope);
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
});
|