scout-ai 2.1.0 → 2.2.0
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.
- checksums.yaml +4 -4
- data/.vimproject +133 -58
- data/README.md +2 -1
- data/VERSION +1 -1
- data/doc/Improvements.md +490 -148
- data/doc/Model.md +53 -10
- data/doc/StartHere.md +44 -19
- data/doc/developer/Architecture.md +25 -6
- data/doc/developer/Backends.md +207 -48
- data/doc/developer/ChatLifecycle.md +121 -20
- data/doc/developer/DelegationInternals.md +319 -33
- data/doc/developer/DesignPrinciples.md +205 -16
- data/doc/developer/PathEngine.md +628 -0
- data/doc/developer/PromptProcessing.md +332 -82
- data/doc/developer/Provenance.md +388 -40
- data/doc/developer/Sandbox.md +213 -0
- data/doc/user/BuildingAgents.md +130 -18
- data/doc/user/Cookbook.md +29 -8
- data/doc/user/CoreConcepts.md +7 -6
- data/doc/user/Delegation.md +166 -15
- data/doc/user/GettingStarted.md +20 -16
- data/doc/user/ManagingContext.md +155 -17
- data/doc/user/MultiAgentWorkflows.md +105 -15
- data/doc/user/Python.md +11 -1
- data/doc/user/RunningInference.md +90 -47
- data/doc/user/ToolCalling.md +134 -29
- data/doc/user/WritingChats.md +56 -27
- data/lib/scout/llm/agent/attach.rb +13 -17
- data/lib/scout/llm/agent/chat.rb +32 -0
- data/lib/scout/llm/agent/construction.rb +64 -0
- data/lib/scout/llm/agent/conversation.rb +304 -0
- data/lib/scout/llm/agent/delegate.rb +56 -238
- data/lib/scout/llm/agent/path/edit.rb +230 -0
- data/lib/scout/llm/agent/path/patch.rb +252 -0
- data/lib/scout/llm/agent/path/workflow_kinds.rb +175 -0
- data/lib/scout/llm/agent/path.rb +1777 -0
- data/lib/scout/llm/agent/save.rb +11 -9
- data/lib/scout/llm/agent/workflow.rb +77 -16
- data/lib/scout/llm/agent.rb +147 -31
- data/lib/scout/llm/ask.rb +28 -17
- data/lib/scout/llm/backends/anthropic.rb +1 -1
- data/lib/scout/llm/backends/bedrock.rb +3 -2
- data/lib/scout/llm/backends/default.rb +127 -34
- data/lib/scout/llm/backends/glm.rb +28 -1
- data/lib/scout/llm/backends/huggingface.rb +1 -1
- data/lib/scout/llm/backends/ollama.rb +1 -1
- data/lib/scout/llm/backends/openai.rb +7 -6
- data/lib/scout/llm/backends/openwebui.rb +14 -7
- data/lib/scout/llm/backends/relay.rb +8 -2
- data/lib/scout/llm/chat/agent_meta.rb +24 -60
- data/lib/scout/llm/chat/parse.rb +1 -1
- data/lib/scout/llm/chat/process/files.rb +10 -0
- data/lib/scout/llm/chat/process/meta.rb +5 -7
- data/lib/scout/llm/chat/process/tools.rb +104 -49
- data/lib/scout/llm/chat/prompt/epoch_tool_compactation.rb +355 -0
- data/lib/scout/llm/chat/prompt/inbox.rb +188 -0
- data/lib/scout/llm/chat/prompt/shorten_tools.rb +797 -13
- data/lib/scout/llm/chat/prompt/shorten_tools_epoch.rb +1 -365
- data/lib/scout/llm/chat/prompt/shorten_tools_epoch_increment.rb +1 -0
- data/lib/scout/llm/chat/prompt.rb +24 -10
- data/lib/scout/llm/chat/provenance/live.rb +1080 -0
- data/lib/scout/llm/chat/provenance/tokens.rb +386 -0
- data/lib/scout/llm/chat/provenance.rb +343 -676
- data/lib/scout/llm/chat.rb +8 -2
- data/lib/scout/llm/embed.rb +15 -3
- data/lib/scout/llm/image.rb +4 -1
- data/lib/scout/llm/mcp.rb +4 -2
- data/lib/scout/llm/request_context.rb +234 -0
- data/lib/scout/llm/sandbox.rb +263 -0
- data/lib/scout/llm/tools/call.rb +163 -42
- data/lib/scout/llm/tools/definition.rb +48 -0
- data/lib/scout/llm/tools/knowledge_base.rb +1 -0
- data/lib/scout/llm/tools/mcp.rb +50 -43
- data/lib/scout/llm/tools/workflow.rb +90 -20
- data/lib/scout/llm/tools.rb +14 -41
- data/lib/scout/model/decision/base.rb +43 -0
- data/lib/scout/model/decision/jev.rb +161 -0
- data/lib/scout/model/python/torch.rb +1 -1
- data/research/clear-tools-inherit.md +171 -0
- data/research/path-engine-dc1-dc2-dc3-fixes.md +279 -0
- data/research/path-kind-chat-role.md +96 -0
- data/research/path-kind-contract-fixes-engine.md +307 -0
- data/research/scout-ai-runtime-availability.md +65 -0
- data/research/subsys/README.md +127 -0
- data/research/subsys/agent-delegation.md +201 -0
- data/research/subsys/agent-society.md +275 -0
- data/research/subsys/agentworkflow.md +233 -0
- data/research/subsys/backends.md +221 -0
- data/research/subsys/chat.md +314 -0
- data/research/subsys/embed-image.md +244 -0
- data/research/subsys/entry-cli.md +253 -0
- data/research/subsys/model-ml.md +326 -0
- data/research/subsys/provenance.md +290 -0
- data/research/subsys/tools.md +334 -0
- data/scout-ai.gemspec +71 -27
- data/scout_commands/agent/ask +8 -7
- data/scout_commands/llm/abridged +40 -0
- data/scout_commands/llm/ask +9 -9
- data/scout_commands/llm/md +2 -0
- data/scout_commands/llm/prov +724 -59
- data/scout_commands/llm/word +4 -0
- data/share/word/reference.docx +0 -0
- data/test/integration/scout/model/decision/test_jev.rb +144 -0
- data/test/scout/llm/agent/test_attach.rb +117 -0
- data/test/scout/llm/agent/test_conversation.rb +407 -0
- data/test/scout/llm/agent/test_delegate.rb +209 -0
- data/test/scout/llm/agent/test_path.rb +1812 -0
- data/test/scout/llm/agent/test_path_edit.rb +908 -0
- data/test/scout/llm/agent/test_path_errors.rb +390 -0
- data/test/scout/llm/agent/test_path_filesystem.rb +532 -0
- data/test/scout/llm/agent/test_path_patch.rb +233 -0
- data/test/scout/llm/agent/test_path_promotion.rb +386 -0
- data/test/scout/llm/agent/test_path_workflow_integration.rb +665 -0
- data/test/scout/llm/agent/test_save.rb +70 -15
- data/test/scout/llm/agent/test_workflow.rb +130 -4
- data/test/scout/llm/backends/test_anthropic.rb +4 -4
- data/test/scout/llm/backends/test_bedrock.rb +4 -4
- data/test/scout/llm/backends/test_format_other.rb +43 -0
- data/test/scout/llm/backends/test_ollama.rb +2 -2
- data/test/scout/llm/backends/test_openwebui.rb +12 -8
- data/test/scout/llm/backends/test_reasoning.rb +114 -0
- data/test/scout/llm/chat/agent_meta_fixtures.rb +28 -27
- data/test/scout/llm/chat/prompt/test_epoch_tool_compactation.rb +612 -0
- data/test/scout/llm/chat/prov_plot_fixtures.rb +131 -0
- data/test/scout/llm/chat/test_agent_meta.rb +40 -80
- data/test/scout/llm/chat/test_agent_meta_provenance.rb +67 -28
- data/test/scout/llm/chat/test_agent_meta_tokens.rb +59 -8
- data/test/scout/llm/chat/test_agent_view.rb +178 -0
- data/test/scout/llm/chat/test_live_report.rb +282 -0
- data/test/scout/llm/chat/test_live_workload.rb +193 -0
- data/test/scout/llm/chat/test_parse.rb +32 -2
- data/test/scout/llm/chat/test_prompt.rb +411 -0
- data/test/scout/llm/chat/test_prov_cli.rb +310 -15
- data/test/scout/llm/chat/test_prov_cli_live.rb +152 -0
- data/test/scout/llm/chat/test_prov_plot.rb +213 -0
- data/test/scout/llm/chat/test_prov_timeline.rb +355 -0
- data/test/scout/llm/chat/test_provenance.rb +67 -22
- data/test/scout/llm/chat/test_provenance_index.rb +479 -0
- data/test/scout/llm/chat/test_society_view.rb +159 -0
- data/test/scout/llm/test_agent.rb +1 -1
- data/test/scout/llm/test_embed.rb +103 -1
- data/test/scout/llm/test_request_context.rb +299 -0
- data/test/scout/llm/test_sandbox.rb +249 -0
- data/test/scout/llm/tools/test_agent_pairing.rb +150 -0
- data/test/scout/llm/tools/test_call_delegation_conflict.rb +316 -0
- data/test/scout/llm/tools/test_definition.rb +332 -0
- data/test/scout/llm/tools/test_jobs_file.rb +211 -0
- data/test/scout/llm/tools/test_mcp.rb +37 -8
- data/test/scout/llm/tools/test_workflow.rb +45 -1
- data/test/scout/model/decision/test_jev.rb +453 -0
- data/test/scout/model/python/huggingface/causal/test_next_token.rb +7 -10
- data/test/scout/model/python/huggingface/test_causal.rb +11 -2
- data/test/scout/model/python/huggingface/test_classification.rb +5 -3
- data/test/scout/model/python/test_availability.rb +116 -0
- data/test/scout/model/python/test_torch.rb +54 -2
- data/test/support/availability.rb +72 -174
- data/test/support/fake_clients.rb +18 -9
- data/test/support/fixtures.rb +75 -6
- data/test/support/mock_backend.rb +9 -1
- data/test/support/test_fixtures.rb +156 -0
- data/test/test_helper.rb +14 -4
- metadata +70 -26
- data/research/ChatAnalyst-required-changes.md +0 -167
- data/research/agent-delegation-analysis.md +0 -810
- data/research/agent-meta-provenance-integration-plan.md +0 -622
- data/research/agent-workflow-analysis.md +0 -1120
- data/research/backends-analysis.md +0 -836
- data/research/chat-core-analysis.md +0 -946
- data/research/chatanalyst-provenance/00-baseline.md +0 -30
- data/research/chatanalyst-provenance/01-repo-map.md +0 -60
- data/research/chatanalyst-provenance/02-event-reconstruction.md +0 -55
- data/research/chatanalyst-provenance/03-duplication-evidence.md +0 -45
- data/research/chatanalyst-provenance/04-tooling-root-cause.md +0 -57
- data/research/chatanalyst-provenance/05-fix-plan.md +0 -46
- data/research/chatanalyst-provenance/07-critic-review.md +0 -25
- data/research/chatanalyst-provenance/final-report.md +0 -45
- data/research/chatanalyst-provenance/resumption.md +0 -37
- data/research/coding-philosophy-analysis.md +0 -928
- data/research/commands-analysis.md +0 -947
- data/research/multi-agent-patterns-analysis.md +0 -853
- data/research/prompt-strategies-analysis.md +0 -630
- data/research/prov-verbosity-fix-notes.md +0 -77
- data/research/provenance-analysis.md +0 -469
- data/research/provenance-continuation-accounting.md +0 -255
- data/research/synthesis-report.md +0 -487
- data/research/tools-system-analysis.md +0 -779
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
|
|
2
|
+
require File.expand_path(__FILE__).sub(%r(.*/test/), '').sub(/test_(.*)\.rb/,'\1')
|
|
3
|
+
|
|
4
|
+
require 'scout/llm/chat/prompt/epoch_tool_compactation'
|
|
5
|
+
|
|
6
|
+
class TestEpochToolCompactation < Test::Unit::TestCase
|
|
7
|
+
|
|
8
|
+
# --- fixtures ------------------------------------------------------------
|
|
9
|
+
#
|
|
10
|
+
# Default configuration gives threshold=50, initial_size=20,
|
|
11
|
+
# epochs_per_increase=3, size_increase=10, repeat_increase=10,
|
|
12
|
+
# full=20, base_compacted=80. For a 60-output history with no repeat in
|
|
13
|
+
# the current epoch the schedule is epoch_size=20, new_calls=10, so
|
|
14
|
+
# keep_full_count=30 (reverse 1..30 == forward 31..60) and
|
|
15
|
+
# truncate_to=110 (no drop band): the compact band is reverse 31..60,
|
|
16
|
+
# i.e. forward positions 1..30.
|
|
17
|
+
|
|
18
|
+
def call_message(name, arguments, id)
|
|
19
|
+
{
|
|
20
|
+
role: 'function_call',
|
|
21
|
+
content: {name: name, arguments: arguments, id: id}.to_json
|
|
22
|
+
}
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def output_message(id, content, step: nil)
|
|
26
|
+
hash = {name: 'tool', content: content, id: id}
|
|
27
|
+
hash[:step] = step if step
|
|
28
|
+
|
|
29
|
+
{role: 'function_call_output', content: hash.to_json}
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def parsed(message)
|
|
33
|
+
Chat.epoch_increment_parse_tool_json(message[:content])
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# 60 adjacent pairs (forward output position = index + 1) plus one
|
|
37
|
+
# leading user message. Oversized interactions sit at forward 10, 15, 20
|
|
38
|
+
# and 25; the sub-threshold pair at forward 1 and the keep-window pair at
|
|
39
|
+
# forward 31 are the untouched-survivor probes.
|
|
40
|
+
def base_history
|
|
41
|
+
messages = [{role: 'user', content: 'start'}]
|
|
42
|
+
|
|
43
|
+
60.times do |f|
|
|
44
|
+
forward = f + 1
|
|
45
|
+
id = "call_#{f}"
|
|
46
|
+
|
|
47
|
+
case f
|
|
48
|
+
when 9
|
|
49
|
+
name, arguments = 'repeated_tool', {'q' => 'repeat-me'}
|
|
50
|
+
content = 'r' * 500
|
|
51
|
+
when 49
|
|
52
|
+
name, arguments = 'repeated_tool', {'q' => 'repeat-me'}
|
|
53
|
+
content = 'repeat result'
|
|
54
|
+
when 14
|
|
55
|
+
# Output JSON carries no id: paired through the adjacency fallback.
|
|
56
|
+
name, arguments = 'tool_14', {'n' => 14}
|
|
57
|
+
content = 'z' * 500
|
|
58
|
+
when 19
|
|
59
|
+
name, arguments = 'tool_19', {'n' => 19}
|
|
60
|
+
content = 'x' * 5000
|
|
61
|
+
when 24
|
|
62
|
+
name, arguments = 'tool_24',
|
|
63
|
+
{'cmd' => 'SECRET_MARKER' + ('a' * 600),
|
|
64
|
+
'path' => '/tmp/f', 'flag' => true}
|
|
65
|
+
content = 'short ok'
|
|
66
|
+
else
|
|
67
|
+
name, arguments = "tool_#{f}", {'n' => f}
|
|
68
|
+
content = "ok #{f}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
messages << call_message(name, arguments, id)
|
|
72
|
+
|
|
73
|
+
if f == 14
|
|
74
|
+
messages << {
|
|
75
|
+
role: 'function_call_output',
|
|
76
|
+
content: {name: 'tool', content: content}.to_json
|
|
77
|
+
}
|
|
78
|
+
elsif f == 19
|
|
79
|
+
messages << output_message(
|
|
80
|
+
id, content,
|
|
81
|
+
step: 'ScoutCoder/help_list_repo_documents/Default_71d0305e'
|
|
82
|
+
)
|
|
83
|
+
else
|
|
84
|
+
messages << output_message(id, content)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
messages
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Index of the function_call whose parsed id equals `id`; non-JSON
|
|
92
|
+
# messages (user text) are skipped safely.
|
|
93
|
+
def call_index(messages, id)
|
|
94
|
+
messages.index do |m|
|
|
95
|
+
m[:role].to_s == 'function_call' && parsed(m) && parsed(m)['id'] == id
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def records(result)
|
|
100
|
+
result.select { |msg| msg[:compacted] }
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def call_ids(result)
|
|
104
|
+
result.select { |msg| msg[:role].to_s == 'function_call' }
|
|
105
|
+
.collect { |msg| parsed(msg)['id'] }
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def contents_containing(result, text)
|
|
109
|
+
result.select { |msg| msg[:content].to_s.include?(text) }
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# --- 1. pairwise replacement ---------------------------------------------
|
|
113
|
+
|
|
114
|
+
def test_pairwise_replacement_removes_both_messages_and_inserts_one_record
|
|
115
|
+
input = base_history
|
|
116
|
+
result = Chat.epoch_tool_compactation(input)
|
|
117
|
+
|
|
118
|
+
assert_equal 118, result.length,
|
|
119
|
+
'one notice + user + 56 pairs + 4 records'
|
|
120
|
+
assert_equal 56,
|
|
121
|
+
result.count { |m| m[:role].to_s == 'function_call' }
|
|
122
|
+
assert_equal 56,
|
|
123
|
+
result.count { |m| m[:role].to_s == 'function_call_output' }
|
|
124
|
+
|
|
125
|
+
records = records(result)
|
|
126
|
+
assert_equal 4, records.length
|
|
127
|
+
records.each { |r| assert_equal :assistant, r[:role] }
|
|
128
|
+
|
|
129
|
+
# Both messages of each replaced pair are gone; the four ids (plus the
|
|
130
|
+
# blank-id output at forward 15, identified through its call) appear in
|
|
131
|
+
# no surviving tool message.
|
|
132
|
+
%w[call_9 call_14 call_19 call_24].each do |id|
|
|
133
|
+
assert(
|
|
134
|
+
result.none? do |m|
|
|
135
|
+
%w[function_call function_call_output].include?(m[:role].to_s) &&
|
|
136
|
+
m[:content].to_s.include?(id)
|
|
137
|
+
end,
|
|
138
|
+
"#{id} still present in a tool message"
|
|
139
|
+
)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Sibling pairs survive with unmodified ids, original names and no
|
|
143
|
+
# 'compacted-' renaming anywhere.
|
|
144
|
+
assert call_ids(result).include?('call_0')
|
|
145
|
+
assert call_ids(result).include?('call_30')
|
|
146
|
+
|
|
147
|
+
survivor = result.find do |m|
|
|
148
|
+
m[:role].to_s == 'function_call' && parsed(m)['id'] == 'call_30'
|
|
149
|
+
end
|
|
150
|
+
assert_equal 'tool_30', parsed(survivor)['name']
|
|
151
|
+
|
|
152
|
+
assert(
|
|
153
|
+
result.none? { |m| m[:content].to_s.include?('compacted-') },
|
|
154
|
+
'unexpected compacted- renaming'
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
# The record stands exactly where the pair's output stood: between the
|
|
158
|
+
# preceding pair's output and the following pair's call.
|
|
159
|
+
record = records.find { |r| r[:content].include?('tool_19') }
|
|
160
|
+
index = result.index(record)
|
|
161
|
+
|
|
162
|
+
assert_equal 'call_18', parsed(result[index - 1])['id']
|
|
163
|
+
assert_equal 'call_20', parsed(result[index + 1])['id']
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# --- 2. adjacency of surviving pairs --------------------------------------
|
|
167
|
+
|
|
168
|
+
def test_every_surviving_call_is_immediately_followed_by_its_output
|
|
169
|
+
result = Chat.epoch_tool_compactation(base_history)
|
|
170
|
+
|
|
171
|
+
result.each_with_index do |msg, index|
|
|
172
|
+
next unless msg[:role].to_s == 'function_call'
|
|
173
|
+
|
|
174
|
+
following = result[index + 1]
|
|
175
|
+
|
|
176
|
+
assert following, "call at #{index} has no following message"
|
|
177
|
+
assert_equal 'function_call_output', following[:role].to_s,
|
|
178
|
+
"message after call at #{index} is not its output"
|
|
179
|
+
assert_equal parsed(msg)['id'], parsed(following)['id'],
|
|
180
|
+
"output after call at #{index} has a different id"
|
|
181
|
+
|
|
182
|
+
# No record (or any other message) sits between a call and its own
|
|
183
|
+
# output.
|
|
184
|
+
assert !following[:compacted], 'record inserted inside a pair'
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# --- 3. record contents ----------------------------------------------------
|
|
189
|
+
|
|
190
|
+
def test_record_contents_name_id_sizes_and_job_pointer
|
|
191
|
+
result = Chat.epoch_tool_compactation(base_history)
|
|
192
|
+
records = records(result)
|
|
193
|
+
|
|
194
|
+
with_step = records.find { |r| r[:content].include?('tool_19') }
|
|
195
|
+
assert with_step, 'record for the oversized-output pair missing'
|
|
196
|
+
|
|
197
|
+
text = with_step[:content]
|
|
198
|
+
|
|
199
|
+
assert text.include?('Tool: tool_19')
|
|
200
|
+
assert text.include?('Call id: call_19')
|
|
201
|
+
assert_match(/Original sizes: arguments \d+ chars, output 5000 chars/, text)
|
|
202
|
+
assert text.include?(
|
|
203
|
+
'Full output found in job: ScoutCoder/help_list_repo_documents/Default_71d0305e'
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
arg_only = records.find { |r| r[:content].include?('tool_24') }
|
|
207
|
+
assert arg_only, 'record for the argument-only pair missing'
|
|
208
|
+
|
|
209
|
+
arg_text = arg_only[:content]
|
|
210
|
+
|
|
211
|
+
assert_match(/Original sizes: arguments \d+ chars, output 8 chars/, arg_text)
|
|
212
|
+
assert(
|
|
213
|
+
!arg_text.include?('Full output found in job'),
|
|
214
|
+
'job pointer emitted without a step'
|
|
215
|
+
)
|
|
216
|
+
assert_match(/cmd=<omitted, \d+ chars>/, arg_text)
|
|
217
|
+
assert arg_text.include?('path=/tmp/f')
|
|
218
|
+
assert arg_text.include?('flag=true')
|
|
219
|
+
|
|
220
|
+
# Exact-or-omitted: neither the secret marker nor any slice of the
|
|
221
|
+
# long value may appear.
|
|
222
|
+
long_value = 'SECRET_MARKER' + ('a' * 600)
|
|
223
|
+
|
|
224
|
+
[long_value, long_value[50, 100], 'aaaa'].each do |sample|
|
|
225
|
+
assert(
|
|
226
|
+
!arg_text.include?(sample),
|
|
227
|
+
"record leaks a substring of the long value: #{sample[0, 20].inspect}"
|
|
228
|
+
)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# No Preview: marker in any record or in the notice.
|
|
232
|
+
assert(
|
|
233
|
+
result.none? { |m| m[:content].to_s.include?('Preview:') },
|
|
234
|
+
'Preview: marker present'
|
|
235
|
+
)
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# --- 4. trigger edges ------------------------------------------------------
|
|
239
|
+
|
|
240
|
+
def test_output_exactly_400_chars_is_untouched
|
|
241
|
+
input = base_history
|
|
242
|
+
index = call_index(input, 'call_19')
|
|
243
|
+
|
|
244
|
+
output = parsed(input[index + 1])
|
|
245
|
+
output['content'] = 'y' * 400
|
|
246
|
+
input[index + 1] = output_message('call_19', output['content'],
|
|
247
|
+
step: output['step'])
|
|
248
|
+
|
|
249
|
+
result = Chat.epoch_tool_compactation(input)
|
|
250
|
+
|
|
251
|
+
assert(
|
|
252
|
+
call_ids(result).include?('call_19'),
|
|
253
|
+
'pair at the exact 400-char boundary was replaced'
|
|
254
|
+
)
|
|
255
|
+
assert(
|
|
256
|
+
contents_containing(result, 'y' * 400).any? do |m|
|
|
257
|
+
m[:role].to_s == 'function_call_output'
|
|
258
|
+
end,
|
|
259
|
+
'400-char output not kept verbatim'
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
# The pair under test is untouched; the other oversized pairs in the
|
|
263
|
+
# fixture may still be replaced.
|
|
264
|
+
assert(
|
|
265
|
+
records(result).none? { |r| r[:content].include?('tool_19') },
|
|
266
|
+
'record emitted for the exactly-400 output'
|
|
267
|
+
)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def test_output_401_chars_is_replaced
|
|
271
|
+
input = base_history
|
|
272
|
+
index = call_index(input, 'call_19')
|
|
273
|
+
input[index + 1] = output_message('call_19', 'y' * 401)
|
|
274
|
+
|
|
275
|
+
result = Chat.epoch_tool_compactation(input)
|
|
276
|
+
|
|
277
|
+
assert !call_ids(result).include?('call_19')
|
|
278
|
+
assert records(result).any? { |r| r[:content].include?('tool_19') }
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def test_argument_string_exactly_200_chars_is_untouched
|
|
282
|
+
input = base_history
|
|
283
|
+
index = call_index(input, 'call_19')
|
|
284
|
+
|
|
285
|
+
# Replace the pair's oversized output too, so the pair is under test
|
|
286
|
+
# on its argument length alone.
|
|
287
|
+
input[index] = call_message('tool_19', {'v' => 'a' * 200}, 'call_19')
|
|
288
|
+
input[index + 1] = output_message('call_19', 'short ok')
|
|
289
|
+
|
|
290
|
+
result = Chat.epoch_tool_compactation(input)
|
|
291
|
+
|
|
292
|
+
# The call under test survives with its exact 200-char value; the other
|
|
293
|
+
# oversized pairs of the fixture may still be replaced.
|
|
294
|
+
assert(
|
|
295
|
+
result.any? do |m|
|
|
296
|
+
m[:role].to_s == 'function_call' &&
|
|
297
|
+
parsed(m)['id'] == 'call_19' &&
|
|
298
|
+
parsed(m)['arguments']['v'] == 'a' * 200
|
|
299
|
+
end,
|
|
300
|
+
'call at the exact 200-char argument boundary was replaced'
|
|
301
|
+
)
|
|
302
|
+
assert(
|
|
303
|
+
records(result).none? { |r| r[:content].include?('tool_19') },
|
|
304
|
+
'record emitted for the exactly-200 argument'
|
|
305
|
+
)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def test_argument_string_201_chars_is_replaced
|
|
309
|
+
input = base_history
|
|
310
|
+
index = call_index(input, 'call_19')
|
|
311
|
+
input[index] = call_message('tool_19', {'v' => 'a' * 201}, 'call_19')
|
|
312
|
+
|
|
313
|
+
result = Chat.epoch_tool_compactation(input)
|
|
314
|
+
|
|
315
|
+
assert !call_ids(result).include?('call_19')
|
|
316
|
+
assert records(result).any? { |r| r[:content].include?('tool_19') }
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# --- 5. sub-threshold pair in the compact band ------------------------------
|
|
320
|
+
|
|
321
|
+
def test_subthreshold_compact_band_pair_is_byte_identical
|
|
322
|
+
input = base_history
|
|
323
|
+
pair_index = call_index(input, 'call_0')
|
|
324
|
+
|
|
325
|
+
original_call = input[pair_index]
|
|
326
|
+
original_output = input[pair_index + 1]
|
|
327
|
+
|
|
328
|
+
result = Chat.epoch_tool_compactation(input)
|
|
329
|
+
|
|
330
|
+
kept_call, kept_output =
|
|
331
|
+
result.each_cons(2).find do |a, b|
|
|
332
|
+
a[:role].to_s == 'function_call' && parsed(a)['id'] == 'call_0'
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
assert kept_call, 'sub-threshold compact-band call removed'
|
|
336
|
+
assert_equal 'function_call_output', kept_output[:role].to_s
|
|
337
|
+
|
|
338
|
+
assert_equal original_call[:content], kept_call[:content]
|
|
339
|
+
assert_equal original_output[:content], kept_output[:content]
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
# --- 6. repeated-call protection -------------------------------------------
|
|
343
|
+
|
|
344
|
+
def test_repeated_call_older_instance_replaced_most_recent_kept_full
|
|
345
|
+
input = base_history
|
|
346
|
+
result = Chat.epoch_tool_compactation(input)
|
|
347
|
+
|
|
348
|
+
# Older instance (forward 10, reverse 51, compact band, oversized).
|
|
349
|
+
assert(
|
|
350
|
+
records(result).any? do |r|
|
|
351
|
+
r[:content].include?('repeated_tool') && r[:content].include?('call_9')
|
|
352
|
+
end,
|
|
353
|
+
'older repeated instance was not compacted'
|
|
354
|
+
)
|
|
355
|
+
assert !call_ids(result).include?('call_9')
|
|
356
|
+
|
|
357
|
+
# Most recent instance (forward 50) survives in full.
|
|
358
|
+
kept_index =
|
|
359
|
+
result.index do |m|
|
|
360
|
+
m[:role].to_s == 'function_call' && parsed(m)['id'] == 'call_49'
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
assert kept_index, 'most recent repeated instance removed'
|
|
364
|
+
assert_equal 'repeated_tool', parsed(result[kept_index])['name']
|
|
365
|
+
assert_equal 'repeat result', parsed(result[kept_index + 1])['content']
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
# --- 7. orphans and malformed content ---------------------------------------
|
|
369
|
+
|
|
370
|
+
def test_orphans_and_malformed_messages_are_untouched
|
|
371
|
+
input = base_history
|
|
372
|
+
|
|
373
|
+
malformed_call = {role: 'function_call', content: 'not json at all'}
|
|
374
|
+
malformed_output = {role: 'function_call_output', content: 'also not json'}
|
|
375
|
+
orphan_call = call_message('orphan_tool', {'a' => 1}, 'call_orphan')
|
|
376
|
+
orphan_output = output_message('call_unknown_id', 'orphan output')
|
|
377
|
+
|
|
378
|
+
input << {role: 'assistant', content: 'separator'}
|
|
379
|
+
input << malformed_call
|
|
380
|
+
input << malformed_output
|
|
381
|
+
input << orphan_call
|
|
382
|
+
input << orphan_output
|
|
383
|
+
|
|
384
|
+
result = Chat.epoch_tool_compactation(input)
|
|
385
|
+
|
|
386
|
+
[malformed_call, malformed_output, orphan_call, orphan_output].each do |m|
|
|
387
|
+
kept = result.find do |r|
|
|
388
|
+
r[:role].to_s == m[:role].to_s && r[:content] == m[:content]
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
assert kept, "#{m[:role]} message was modified or removed"
|
|
392
|
+
end
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
# --- 8. drop band -------------------------------------------------------------
|
|
396
|
+
|
|
397
|
+
# 160 outputs: the schedule reaches pinned_total=140, epoch_size=30,
|
|
398
|
+
# new_calls=20, keep_full_count=40, effective_compacted=100,
|
|
399
|
+
# truncate_to=140. Reverse 141..160 == forward 1..20 is dropped.
|
|
400
|
+
def test_interaction_older_than_truncate_to_is_dropped_without_record
|
|
401
|
+
input = [{role: 'user', content: 'start'}]
|
|
402
|
+
|
|
403
|
+
160.times do |f|
|
|
404
|
+
id = "call_#{f}"
|
|
405
|
+
input << call_message("tool_#{f}", {'n' => f}, id)
|
|
406
|
+
input << output_message(id, "ok #{f}")
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
result = Chat.epoch_tool_compactation(input)
|
|
410
|
+
|
|
411
|
+
assert_empty records(result), 'records emitted in the drop band'
|
|
412
|
+
assert_equal 140,
|
|
413
|
+
result.count { |m| m[:role].to_s == 'function_call' }
|
|
414
|
+
|
|
415
|
+
# Dropped ids (call_0 .. call_19) appear in no message; the check must
|
|
416
|
+
# not match call ids by substring (call_1 is a prefix of call_1x).
|
|
417
|
+
(0..19).each do |f|
|
|
418
|
+
assert(
|
|
419
|
+
result.none? { |m| m[:content].to_s =~ /"call_#{f}"/ ||
|
|
420
|
+
m[:content].to_s.include?("Call id: call_#{f}\n") },
|
|
421
|
+
"call_#{f} from the drop band still present"
|
|
422
|
+
)
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
assert(
|
|
426
|
+
call_ids(result).include?('call_20'),
|
|
427
|
+
'first interaction outside the drop band removed'
|
|
428
|
+
)
|
|
429
|
+
assert_equal input.first, result.first
|
|
430
|
+
assert_match(/Removed tool messages: 40/, result[1][:content])
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
# --- 9. stability -------------------------------------------------------------
|
|
434
|
+
|
|
435
|
+
def test_transform_is_deterministic
|
|
436
|
+
first = Chat.epoch_tool_compactation(base_history)
|
|
437
|
+
second = Chat.epoch_tool_compactation(base_history)
|
|
438
|
+
|
|
439
|
+
assert_equal first.collect { |m| [m[:role], m[:content]] },
|
|
440
|
+
second.collect { |m| [m[:role], m[:content]] }
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
def test_appending_recent_pair_leaves_earlier_records_and_notice_identical
|
|
444
|
+
base = base_history
|
|
445
|
+
base_result = Chat.epoch_tool_compactation(base)
|
|
446
|
+
|
|
447
|
+
grown = base.dup
|
|
448
|
+
grown << call_message('tool_60', {'n' => 60}, 'call_60')
|
|
449
|
+
grown << output_message('call_60', 'ok 60')
|
|
450
|
+
grown_result = Chat.epoch_tool_compactation(grown)
|
|
451
|
+
|
|
452
|
+
# The same four interactions are still replaced, and their records are
|
|
453
|
+
# byte-identical, in the same order.
|
|
454
|
+
base_records = records(base_result).collect { |r| r[:content] }
|
|
455
|
+
grown_records = records(grown_result).collect { |r| r[:content] }
|
|
456
|
+
|
|
457
|
+
assert_equal base_records, grown_records
|
|
458
|
+
assert_equal 4, grown_records.length
|
|
459
|
+
|
|
460
|
+
assert_equal base_result, grown_result.take(base_result.size)
|
|
461
|
+
|
|
462
|
+
# The new pair survives in full.
|
|
463
|
+
assert call_ids(grown_result).include?('call_60')
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
# --- 10. notice ----------------------------------------------------------------
|
|
467
|
+
|
|
468
|
+
def test_no_meta_places_one_counted_notice_at_first_affected_call
|
|
469
|
+
input = base_history
|
|
470
|
+
result = Chat.epoch_tool_compactation(input)
|
|
471
|
+
first = call_index(input, 'call_9')
|
|
472
|
+
assert_equal input.take(first), result.take(first)
|
|
473
|
+
notice = result.fetch(first)
|
|
474
|
+
assert_equal :assistant, notice[:role]
|
|
475
|
+
assert_match(/Compacted tool messages: 8\nRemoved tool messages: 0/, notice[:content])
|
|
476
|
+
assert_equal 1, contents_containing(result, '=== Context Management ===').size
|
|
477
|
+
|
|
478
|
+
# All-short history: nothing replaced or dropped, so no notice.
|
|
479
|
+
quiet = [{role: 'user', content: 'start'}]
|
|
480
|
+
|
|
481
|
+
60.times do |f|
|
|
482
|
+
quiet << call_message("tool_#{f}", {'n' => f}, "call_#{f}")
|
|
483
|
+
quiet << output_message("call_#{f}", "ok #{f}")
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
quiet_result = Chat.epoch_tool_compactation(quiet)
|
|
487
|
+
|
|
488
|
+
assert_empty records(quiet_result)
|
|
489
|
+
assert(
|
|
490
|
+
quiet_result.none? { |m| m[:content].to_s.start_with?('=== Context Management ===') },
|
|
491
|
+
'notice emitted although nothing was replaced or dropped'
|
|
492
|
+
)
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
# --- 11. dispatch and default ---------------------------------------------------
|
|
496
|
+
|
|
497
|
+
def test_default_context_strategy_pins_the_new_strategy
|
|
498
|
+
assert_equal %w(epoch_tool_compactation inbox),
|
|
499
|
+
Chat::DEFAULT_CONTEXT_STRATEGY
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
def test_prepare_prompt_dispatches_epoch_tool_compactation
|
|
503
|
+
result = Chat.prepare_prompt(base_history, ['epoch_tool_compactation'])
|
|
504
|
+
|
|
505
|
+
assert(
|
|
506
|
+
records(result).any? { |r| r[:content].include?('tool_19') },
|
|
507
|
+
'strategy not applied through prepare_prompt'
|
|
508
|
+
)
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
def test_shorten_tools_epoch_increment_still_selectable_by_name
|
|
512
|
+
result =
|
|
513
|
+
Chat.prepare_prompt(base_history, ['shorten_tools_epoch_increment'])
|
|
514
|
+
|
|
515
|
+
# The old strategy still shortens in place: its CONTEXT-COMPACTED
|
|
516
|
+
# preview marker appears and the tool messages survive as tool
|
|
517
|
+
# messages.
|
|
518
|
+
assert(
|
|
519
|
+
result.any? { |m| m[:content].to_s.include?(Chat::COMPACTATION_MESSAGE) },
|
|
520
|
+
'shorten_tools_epoch_increment no longer produces its preview'
|
|
521
|
+
)
|
|
522
|
+
assert(
|
|
523
|
+
result.any? { |m| m[:role].to_s == 'function_call' },
|
|
524
|
+
'shorten_tools_epoch_increment no longer keeps tool calls'
|
|
525
|
+
)
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
def test_meta_anchor_counts_drops_and_preserves_all_preceding_rounds
|
|
529
|
+
input = base_history
|
|
530
|
+
start = call_index(input, 'call_6')
|
|
531
|
+
later = call_index(input, 'call_10')
|
|
532
|
+
input.insert(later, {role: :meta, content: 'round 3'})
|
|
533
|
+
input.insert(start, {role: :meta, content: 'round 2'}, {role: :assistant, content: 'thinking'})
|
|
534
|
+
input.unshift({role: :system, content: 'identity'}, {role: :meta, content: 'round 1'})
|
|
535
|
+
snapshot = Marshal.dump(input)
|
|
536
|
+
result = Chat.epoch_tool_compactation(input)
|
|
537
|
+
marker = input.index { |m| m[:content] == 'round 2' }
|
|
538
|
+
assert_equal input.take(marker + 1), result.take(marker + 1)
|
|
539
|
+
assert_match(/Compacted tool messages: 8/, result[marker + 1][:content])
|
|
540
|
+
assert_equal snapshot, Marshal.dump(input)
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
def test_repeat_extends_live_epoch_without_changing_previously_rendered_bytes
|
|
544
|
+
options = {threshold: 6, full: 1, base_compacted: 1, initial_size: 2,
|
|
545
|
+
repeat_increase: 2, configured_max_size: 6, compacted_growth_ratio: 2}
|
|
546
|
+
history = [{role: :system, content: 'identity'}]
|
|
547
|
+
append = lambda do |key|
|
|
548
|
+
id = history.size.to_s
|
|
549
|
+
history << {role: :meta, content: "round #{id}"}
|
|
550
|
+
history << call_message(key, {}, id)
|
|
551
|
+
history << output_message(id, 'x' * 600)
|
|
552
|
+
end
|
|
553
|
+
%w[a a b c d e f].each(&append)
|
|
554
|
+
previous = Chat.epoch_tool_compactation(history, **options)
|
|
555
|
+
%w[a g h].each do |key|
|
|
556
|
+
append.call(key)
|
|
557
|
+
rendered = Chat.epoch_tool_compactation(history, **options)
|
|
558
|
+
assert_equal JSON.generate(previous), JSON.generate(rendered.take(previous.size))
|
|
559
|
+
previous = rendered
|
|
560
|
+
end
|
|
561
|
+
append.call('i')
|
|
562
|
+
changed = Chat.epoch_tool_compactation(history, **options)
|
|
563
|
+
assert_not_equal previous, changed.take(previous.size)
|
|
564
|
+
assert_equal history.take(2), changed.take(2)
|
|
565
|
+
end
|
|
566
|
+
|
|
567
|
+
def test_batched_calls_with_reversed_outputs_never_get_split_by_metadata
|
|
568
|
+
calls = %w[a b c d].map { |id| call_message(id, {}, id) }
|
|
569
|
+
outputs = %w[b a c d].map { |id| output_message(id, id == 'b' ? 'z'*500 : 'ok') }
|
|
570
|
+
input = [calls[0], calls[1], outputs[0], outputs[1], calls[2], outputs[2], calls[3], outputs[3]]
|
|
571
|
+
result = Chat.epoch_tool_compactation(input, threshold: 3, full: 1)
|
|
572
|
+
assert_match(/Context Management/, result.first[:content])
|
|
573
|
+
assert_equal [calls[0], outputs[1]], result[1, 2]
|
|
574
|
+
assert_match(/Call id: b/, result[3][:content])
|
|
575
|
+
end
|
|
576
|
+
|
|
577
|
+
def test_sizing_calibration_tradeoff_and_override_precedence
|
|
578
|
+
base = Chat::ToolCompaction.policy
|
|
579
|
+
keys = %i[threshold full initial_size configured_max_size base_compacted configured_max_compacted]
|
|
580
|
+
assert_equal [50, 20, 20, 60, 80, 160], base.values_at(*keys)
|
|
581
|
+
small = Chat::ToolCompaction.policy(context_tokens: 40_000)
|
|
582
|
+
assert_equal [25, 10, 10, 30, 40, 80], small.values_at(*keys)
|
|
583
|
+
hot = Chat::ToolCompaction.policy(context_tokens: 80_000, immediate_share: 0.75)
|
|
584
|
+
assert_equal [75, 30, 30, 90, 40, 80], hot.values_at(*keys)
|
|
585
|
+
assert_equal 320, Chat::ToolCompaction.policy(max_chars: 400)[:configured_max_compacted]
|
|
586
|
+
assert_equal 7, Chat::ToolCompaction.policy(context_tokens: 40_000, full: 7)[:full]
|
|
587
|
+
assert_raise(ArgumentError) { Chat::ToolCompaction.policy(immediate_share: 1) }
|
|
588
|
+
end
|
|
589
|
+
|
|
590
|
+
def test_cap_covers_nested_arguments_many_small_fields_and_job_path
|
|
591
|
+
args = (1..150).to_h { |i| ["k#{i}", i] }
|
|
592
|
+
args['nested'] = [{'data' => 'private' * 100}]
|
|
593
|
+
input = base_history
|
|
594
|
+
i = call_index(input, 'call_19')
|
|
595
|
+
input[i] = call_message('sized', args, 'call_19')
|
|
596
|
+
input[i+1] = output_message('call_19', 'ok', step: 'very-long-job' * 200)
|
|
597
|
+
[32, 120, 800].each do |cap|
|
|
598
|
+
result = Chat.epoch_tool_compactation(input, max_chars: cap)
|
|
599
|
+
assert_false call_ids(result).include?('call_19')
|
|
600
|
+
assert records(result).all? { |r| r[:content].size <= cap }
|
|
601
|
+
assert records(result).none? { |r| r[:content].include?('private') }
|
|
602
|
+
end
|
|
603
|
+
end
|
|
604
|
+
|
|
605
|
+
def test_native_chat_file_projection_round_trip
|
|
606
|
+
input = Chat.parse(Chat.print(base_history))
|
|
607
|
+
rendered = Chat.epoch_tool_compactation(input)
|
|
608
|
+
parsed = Chat.parse(Chat.print(rendered))
|
|
609
|
+
assert_equal rendered.map { |m| [m[:role].to_s, m[:content]] },
|
|
610
|
+
parsed.map { |m| [m[:role].to_s, m[:content]] }
|
|
611
|
+
end
|
|
612
|
+
end
|