|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | +import { |
| 4 | + AgendaExtractor, |
| 5 | + AgendaPriority, |
| 6 | + AgendaSource, |
| 7 | + AgendaStatus, |
| 8 | + AgendaTriggerKind, |
| 9 | + MessageRole, |
| 10 | + SessionDeliveryMode, |
| 11 | + estimateMessagesTokens, |
| 12 | + type AgendaRecord, |
| 13 | + type ChatMessage, |
| 14 | + type IModelService, |
| 15 | +} from "scorpio.ai"; |
| 16 | + |
| 17 | +class FakeModelService { |
| 18 | + readonly config = { contextWindow: 1_024 } as any; |
| 19 | + readonly calls: ChatMessage[][] = []; |
| 20 | + failCandidateCalls = false; |
| 21 | + analysis: any = { shouldSync: false, intents: [] }; |
| 22 | + candidate: any = { candidates: [] }; |
| 23 | + candidateFactory?: (messages: ChatMessage[]) => any; |
| 24 | + final: any = { actions: [] }; |
| 25 | + failFinal = false; |
| 26 | + |
| 27 | + async invokeStructured<T>(_schema: unknown, prompt: string | ChatMessage[]): Promise<T> { |
| 28 | + const messages = typeof prompt === 'string' |
| 29 | + ? [{ role: MessageRole.Human, content: prompt }] |
| 30 | + : prompt; |
| 31 | + this.calls.push(messages); |
| 32 | + const system = String(messages[0]?.content ?? ''); |
| 33 | + if (system.includes('# Conversation analysis mode')) return this.analysis as T; |
| 34 | + if (system.includes('# Agenda-card matching mode')) { |
| 35 | + if (this.failCandidateCalls) throw new Error('candidate failed'); |
| 36 | + return (this.candidateFactory?.(messages) ?? this.candidate) as T; |
| 37 | + } |
| 38 | + if (this.failFinal) throw new Error('final failed'); |
| 39 | + return this.final as T; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function record(id: number, messageSize = 240): AgendaRecord { |
| 44 | + const now = Date.now(); |
| 45 | + return { |
| 46 | + item: { |
| 47 | + id, |
| 48 | + content: `事项 ${id}`, |
| 49 | + status: AgendaStatus.Pending, |
| 50 | + priority: AgendaPriority.Normal, |
| 51 | + assignee: 'user' as AgendaRecord['item']['assignee'], |
| 52 | + assigneeName: null, |
| 53 | + dueAt: now + id * 60_000, |
| 54 | + source: AgendaSource.User, |
| 55 | + createdAt: now, |
| 56 | + updatedAt: now, |
| 57 | + doneAt: null, |
| 58 | + }, |
| 59 | + triggers: [{ |
| 60 | + id, |
| 61 | + itemId: id, |
| 62 | + kind: AgendaTriggerKind.Absolute, |
| 63 | + expr: new Date(now + id * 60_000).toISOString(), |
| 64 | + action: SessionDeliveryMode.Notify, |
| 65 | + message: `提醒 ${id} ${'长'.repeat(messageSize)}`, |
| 66 | + channelSessionId: 0, |
| 67 | + enabled: true, |
| 68 | + fireCount: 0, |
| 69 | + maxFires: 1, |
| 70 | + lastFiredAt: null, |
| 71 | + nextFireAt: now + id * 60_000, |
| 72 | + createdAt: now, |
| 73 | + }], |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +function conversation(size = 1_200): ChatMessage[] { |
| 78 | + return [{ role: MessageRole.Human, content: `请更新事项 ${'内容'.repeat(size)}` }]; |
| 79 | +} |
| 80 | + |
| 81 | +test("overflow selector no-sync advice does not veto the final writer", async () => { |
| 82 | + const model = new FakeModelService(); |
| 83 | + const extractor = new AgendaExtractor(model as unknown as IModelService, 'writer', 'selector'); |
| 84 | + |
| 85 | + const actions = await extractor.extract(conversation(), [record(1)]); |
| 86 | + |
| 87 | + assert.deepEqual(actions, []); |
| 88 | + assert.equal(model.calls.length, 2); |
| 89 | + assert.match(String(model.calls[0][0].content), /Conversation analysis mode/); |
| 90 | + assert.match(String(model.calls[1][0].content), /Oversized-catalog candidate contract/); |
| 91 | + assert.ok(model.calls.every(call => estimateMessagesTokens(call) <= 512)); |
| 92 | +}); |
| 93 | + |
| 94 | +test("overflow candidate scan has a hard six-batch model-call cap", async () => { |
| 95 | + const model = new FakeModelService(); |
| 96 | + model.analysis = { shouldSync: true, intents: ['修改事项 40 的提醒时间'] }; |
| 97 | + const extractor = new AgendaExtractor(model as unknown as IModelService, 'writer', 'selector'); |
| 98 | + |
| 99 | + await extractor.extract(conversation(), Array.from({ length: 40 }, (_, index) => record(index + 1))); |
| 100 | + |
| 101 | + const candidateCalls = model.calls.filter(call => String(call[0]?.content ?? '').includes('# Agenda-card matching mode')); |
| 102 | + assert.equal(candidateCalls.length, 6); |
| 103 | + assert.equal(model.calls.length, 8); // analysis + six batches + final writer |
| 104 | +}); |
| 105 | + |
| 106 | +test("one failed candidate batch falls back locally and still reaches the writer", async () => { |
| 107 | + const model = new FakeModelService(); |
| 108 | + model.analysis = { shouldSync: true, intents: ['修改事项 1'] }; |
| 109 | + model.failCandidateCalls = true; |
| 110 | + const extractor = new AgendaExtractor(model as unknown as IModelService, 'writer', 'selector'); |
| 111 | + |
| 112 | + const actions = await extractor.extract(conversation(), [record(1)]); |
| 113 | + |
| 114 | + assert.deepEqual(actions, []); |
| 115 | + assert.equal(model.calls.length, 3); |
| 116 | +}); |
| 117 | + |
| 118 | +test("candidate relevance scores are merged globally instead of keeping batch order", async () => { |
| 119 | + const model = new FakeModelService(); |
| 120 | + model.config.contextWindow = 4_096; |
| 121 | + model.analysis = { shouldSync: true, intents: ['修改事项'] }; |
| 122 | + model.candidateFactory = messages => { |
| 123 | + const human = String(messages[1]?.content ?? ''); |
| 124 | + const ids = [...human.matchAll(/<agenda id="(\d+)"/g)].map(match => Number(match[1])); |
| 125 | + return { candidates: ids.map(id => ({ id, relevance: id })) }; |
| 126 | + }; |
| 127 | + const extractor = new AgendaExtractor(model as unknown as IModelService, 'writer', 'selector'); |
| 128 | + |
| 129 | + await extractor.extract(conversation(), Array.from({ length: 8 }, (_, index) => record(index + 1))); |
| 130 | + |
| 131 | + const finalHuman = String(model.calls.at(-1)?.[1]?.content ?? ''); |
| 132 | + const finalIds = [...finalHuman.matchAll(/<agenda id="(\d+)"/g)].map(match => Number(match[1])); |
| 133 | + assert.ok(finalIds.length > 1); |
| 134 | + assert.deepEqual(finalIds, [...finalIds].sort((a, b) => b - a)); |
| 135 | +}); |
| 136 | + |
| 137 | +test("a final writer failure propagates so the pending job can be marked failed", async () => { |
| 138 | + const model = new FakeModelService(); |
| 139 | + model.failFinal = true; |
| 140 | + const extractor = new AgendaExtractor(model as unknown as IModelService, 'writer', 'selector'); |
| 141 | + |
| 142 | + await assert.rejects(() => extractor.extract(conversation(), [record(1)]), /final failed/); |
| 143 | +}); |
0 commit comments