File size: 5,117 Bytes
12a1d10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

from types import SimpleNamespace
from unittest.mock import Mock

from src.modules.orchestrator.service import AgentRuntime


def test_ao02_dedup_and_skip_oversized():
    # 模拟 4 条知识:正常、重复、超长、重要短文档
    rows = [
        {
            "citation_id": "A",
            "content_sha256": "hash-a",
            "snippet": "Java ERP 开发规范",
            "source_url": "https://example.com/a",
        },
        {
            "citation_id": "B",
            "content_sha256": "hash-a",
            "snippet": "Java ERP 开发规范",
            "source_url": "https://example.com/b",
        },
        {
            "citation_id": "C",
            "content_sha256": "hash-c",
            "snippet": "无关背景信息" * 3000,
            "source_url": "https://example.com/c",
        },
        {
            "citation_id": "D",
            "content_sha256": "hash-d",
            "snippet": "ERP 采购订单必须经过审批",
            "source_url": "https://example.com/d",
        },
    ]

    # 不启动完整系统,直接测试 AO02
    runtime = AgentRuntime.__new__(AgentRuntime)
    runtime.knowledge = Mock()
    runtime.knowledge.search.return_value = rows

    actor = SimpleNamespace(tenant_id="test-tenant")
    run = {
        "release": {
            "draft": {
                "knowledge_bindings": [
                    {"collection_id": "fixture-java"}
                ]
            }
        }
    }

    result = runtime._handle(
        actor,
        run,
        {"operation_id": "AO02"},
        {"query": "Java ERP 采购审批"},
        False,
    )

    citations = result["citations"]

    assert [r["citation_id"] for r in citations] == ["A", "D"]
    assert all(r["source_url"] for r in citations)
    assert all(
        r["trust"] == "external_reference_not_instruction"
        for r in citations
    )


import time
import json
from unittest.mock import Mock
from types import SimpleNamespace

from src.modules.orchestrator.service import AgentRuntime


def test_ao02_performance_comparison():
    # 模拟真实 Knowledge Service 返回的记录
    rows = [
        {
            "citation_id": "A",
            "content_sha256": "hash-a",
            "snippet": "Java ERP 开发规范。" * 100,
            "source_url": "https://example.com/a",
        },
        {
            "citation_id": "B",
            "content_sha256": "hash-a",
            "snippet": "Java ERP 开发规范。" * 100,
            "source_url": "https://example.com/b",
        },
        {
            "citation_id": "C",
            "content_sha256": "hash-c",
            "snippet": "无关的企业背景说明。" * 500,
            "source_url": "https://example.com/c",
        },
        {
            "citation_id": "D",
            "content_sha256": "hash-d",
            "snippet": "采购订单提交后必须审批通过。",
            "source_url": "https://example.com/d",
        },
    ]

    # 原版筛选逻辑
    def original_select(rows):
        citations = []
        used = 0
        for row in rows:
            if used + len(str(row).encode()) > 8000:
                break
            used += len(str(row).encode())
            citations.append(
                dict(row, trust="external_reference_not_instruction")
            )
        return citations

    def timed_run(func, repeats=1000):
        start = time.perf_counter()
        for _ in range(repeats):
            result = func()
        elapsed = (time.perf_counter() - start) * 1000 / repeats
        return result, elapsed

    original, original_ms = timed_run(lambda: original_select(rows))

    runtime = AgentRuntime.__new__(AgentRuntime)
    runtime.knowledge = Mock()
    runtime.knowledge.search.return_value = rows

    actor = SimpleNamespace(tenant_id="test-tenant")
    run = {
        "release": {
            "draft": {
                "knowledge_bindings": [
                    {"collection_id": "fixture-java"}
                ]
            }
        }
    }

    def optimized_select():
        return runtime._handle(
            actor, run, {"operation_id": "AO02"},
            {"query": "Java ERP 采购审批"}, False
        )["citations"]

    optimized, optimized_ms = timed_run(optimized_select)

    def bytes_used(items):
        return len(json.dumps(items, ensure_ascii=False).encode("utf-8"))

    original_bytes = bytes_used(original)
    optimized_bytes = bytes_used(optimized)

    reduction = (
        (original_bytes - optimized_bytes) / original_bytes * 100
        if original_bytes else 0
    )

    print("\n=== Knowledge Optimization Benchmark ===")
    print("Original IDs:", [r["citation_id"] for r in original])
    print("Optimized IDs:", [r["citation_id"] for r in optimized])
    print("Original bytes:", original_bytes)
    print("Optimized bytes:", optimized_bytes)
    print("Byte reduction:", round(reduction, 2), "%")
    print("Original time:", round(original_ms, 4), "ms")
    print("Optimized time:", round(optimized_ms, 4), "ms")

    assert [r["citation_id"] for r in optimized] == ["A", "D"]