scout-ai 2.0.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 +135 -55
- 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 +320 -34
- 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 +402 -44
- 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 +64 -0
- 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 +79 -17
- data/lib/scout/llm/agent.rb +151 -27
- data/lib/scout/llm/ask.rb +37 -27
- 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 +131 -32
- 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 -52
- 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 +352 -626
- 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 +72 -26
- 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 +845 -80
- 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 +109 -22
- 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 +200 -28
- data/test/scout/llm/chat/test_agent_meta_tokens.rb +112 -7
- 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 +708 -13
- 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 +71 -25
- 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/synthesis-report.md +0 -487
- data/research/tools-system-analysis.md +0 -779
|
@@ -0,0 +1,908 @@
|
|
|
1
|
+
require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
|
|
2
|
+
require File.expand_path(__FILE__).sub(%r(/test/.*), '/lib/scout/llm/agent/path.rb')
|
|
3
|
+
|
|
4
|
+
require "tmpdir"
|
|
5
|
+
|
|
6
|
+
class TestPathEditing < Test::Unit::TestCase
|
|
7
|
+
def setup
|
|
8
|
+
@tmpdir = Scout.tmp.agent_path_test.find
|
|
9
|
+
FileUtils.mkdir_p(@tmpdir)
|
|
10
|
+
@agent = LLM.agent
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def teardown
|
|
14
|
+
FileUtils.remove_entry(@tmpdir) if @tmpdir && File.exist?(@tmpdir)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def register_text_kind(name = "text", capabilities = {})
|
|
18
|
+
@agent.register_path_kind(
|
|
19
|
+
name,
|
|
20
|
+
"description" => "Test text paths",
|
|
21
|
+
"locations" => ["project", "tmp"],
|
|
22
|
+
"default_location" => "project",
|
|
23
|
+
"roots" => {
|
|
24
|
+
"project" => @tmpdir,
|
|
25
|
+
"tmp" => File.join(@tmpdir, "tmp")
|
|
26
|
+
},
|
|
27
|
+
"capabilities" => capabilities
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_path_edit_with_line_selector
|
|
32
|
+
register_text_kind
|
|
33
|
+
|
|
34
|
+
@agent.path_write(
|
|
35
|
+
"kind" => "text",
|
|
36
|
+
"path" => "hello.txt",
|
|
37
|
+
"content" => "one\ntwo\nthree\n"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
result = @agent.path_edit(
|
|
41
|
+
"kind" => "text",
|
|
42
|
+
"path" => "hello.txt",
|
|
43
|
+
"selector" => {
|
|
44
|
+
"type" => "lines",
|
|
45
|
+
"start" => 1,
|
|
46
|
+
"end" => 2
|
|
47
|
+
},
|
|
48
|
+
"replacement" => "changed\n"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
assert_equal true, result["changed"]
|
|
52
|
+
assert_equal "one\nchanged\nthree\n", result["content"]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_path_edit_with_character_selector
|
|
56
|
+
register_text_kind
|
|
57
|
+
|
|
58
|
+
@agent.path_write(
|
|
59
|
+
"kind" => "text",
|
|
60
|
+
"path" => "hello.txt",
|
|
61
|
+
"content" => "hello world"
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
result = @agent.path_edit(
|
|
65
|
+
"kind" => "text",
|
|
66
|
+
"path" => "hello.txt",
|
|
67
|
+
"selector" => {
|
|
68
|
+
"type" => "chars",
|
|
69
|
+
"start" => 6,
|
|
70
|
+
"end" => 11
|
|
71
|
+
},
|
|
72
|
+
"replacement" => "Scout"
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
assert_equal "hello Scout", result["content"]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def test_path_edit_with_regexp_selector
|
|
79
|
+
register_text_kind
|
|
80
|
+
|
|
81
|
+
@agent.path_write(
|
|
82
|
+
"kind" => "text",
|
|
83
|
+
"path" => "hello.txt",
|
|
84
|
+
"content" => "hello world"
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
result = @agent.path_edit(
|
|
88
|
+
"kind" => "text",
|
|
89
|
+
"path" => "hello.txt",
|
|
90
|
+
"selector" => {
|
|
91
|
+
"type" => "regexp",
|
|
92
|
+
"pattern" => "world"
|
|
93
|
+
},
|
|
94
|
+
"replacement" => "Scout"
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
assert_equal "hello Scout", result["content"]
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def test_path_edit_rejects_invalid_selector
|
|
101
|
+
assert_raise(ParameterException) do
|
|
102
|
+
@agent.path_apply_selector(
|
|
103
|
+
"hello world",
|
|
104
|
+
{"type" => "unknown"},
|
|
105
|
+
"replacement"
|
|
106
|
+
)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Review point 3 (Phase 0 fixes): a non-String regexp pattern is invalid
|
|
111
|
+
# input and must surface as ParameterException at the operation boundary,
|
|
112
|
+
# never as a TypeError escaping Regexp.new.
|
|
113
|
+
def test_path_edit_regexp_selector_non_string_pattern_raises_parameter_exception
|
|
114
|
+
register_text_kind
|
|
115
|
+
|
|
116
|
+
@agent.path_write(
|
|
117
|
+
"kind" => "text",
|
|
118
|
+
"path" => "hello.txt",
|
|
119
|
+
"content" => "hello world"
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
[nil, 5, :x].each do |pattern|
|
|
123
|
+
error = assert_raise(ParameterException) do
|
|
124
|
+
@agent.path_edit(
|
|
125
|
+
"kind" => "text",
|
|
126
|
+
"path" => "hello.txt",
|
|
127
|
+
"selector" => {"type" => "regexp", "pattern" => pattern},
|
|
128
|
+
"replacement" => "Scout"
|
|
129
|
+
)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
assert_match(/pattern must be a String, got /, error.message)
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Review point 3: the same normalization must hold through the
|
|
137
|
+
# agent-facing tool callable, whose rescue serializes ScoutException
|
|
138
|
+
# subclasses. A raw TypeError would escape it entirely.
|
|
139
|
+
def test_path_edit_regexp_selector_non_string_pattern_through_tool_callable
|
|
140
|
+
register_text_kind
|
|
141
|
+
|
|
142
|
+
@agent.path_write(
|
|
143
|
+
"kind" => "text",
|
|
144
|
+
"path" => "hello.txt",
|
|
145
|
+
"content" => "hello world"
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
tools = @agent.instance_variable_get(:@other_options)[:tools]
|
|
149
|
+
|
|
150
|
+
result = JSON.parse(
|
|
151
|
+
tools["path_edit"].first.call(
|
|
152
|
+
"path_edit",
|
|
153
|
+
{
|
|
154
|
+
"kind" => "text",
|
|
155
|
+
"path" => "hello.txt",
|
|
156
|
+
"selector" => {"type" => "regexp", "pattern" => nil},
|
|
157
|
+
"replacement" => "Scout"
|
|
158
|
+
}
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
# Serialized ParameterException message, not a raw TypeError escape.
|
|
163
|
+
assert_match(/pattern must be a String/, result["exception"])
|
|
164
|
+
assert result.key?("exception_line")
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def test_path_edit_rejects_invalid_line_range
|
|
168
|
+
assert_raise(ParameterException) do
|
|
169
|
+
@agent.path_apply_selector(
|
|
170
|
+
"one\ntwo\n",
|
|
171
|
+
{
|
|
172
|
+
"type" => "lines",
|
|
173
|
+
"start" => 2,
|
|
174
|
+
"end" => 1
|
|
175
|
+
},
|
|
176
|
+
"replacement"
|
|
177
|
+
)
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def test_path_edit_rejects_invalid_character_range
|
|
182
|
+
assert_raise(ParameterException) do
|
|
183
|
+
@agent.path_apply_selector(
|
|
184
|
+
"hello",
|
|
185
|
+
{
|
|
186
|
+
"type" => "chars",
|
|
187
|
+
"start" => 4,
|
|
188
|
+
"end" => 2
|
|
189
|
+
},
|
|
190
|
+
"replacement"
|
|
191
|
+
)
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def write_selector_fixture(content)
|
|
196
|
+
register_text_kind
|
|
197
|
+
@agent.path_write(
|
|
198
|
+
"kind" => "text", "path" => "selector.txt", "content" => content
|
|
199
|
+
)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def edit_lines(content, start, finish, replacement)
|
|
203
|
+
write_selector_fixture(content)
|
|
204
|
+
result = @agent.path_edit(
|
|
205
|
+
"kind" => "text",
|
|
206
|
+
"path" => "selector.txt",
|
|
207
|
+
"selector" => {"type" => "lines", "start" => start, "end" => finish},
|
|
208
|
+
"replacement" => replacement
|
|
209
|
+
)
|
|
210
|
+
[result["content"], File.read(File.join(@tmpdir, "selector.txt"))]
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def edit_chars(content, start, finish, replacement)
|
|
214
|
+
write_selector_fixture(content)
|
|
215
|
+
result = @agent.path_edit(
|
|
216
|
+
"kind" => "text",
|
|
217
|
+
"path" => "selector.txt",
|
|
218
|
+
"selector" => {"type" => "chars", "start" => start, "end" => finish},
|
|
219
|
+
"replacement" => replacement
|
|
220
|
+
)
|
|
221
|
+
[result["content"], File.read(File.join(@tmpdir, "selector.txt"))]
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def assert_edit_result(expected, got)
|
|
225
|
+
reported, on_disk = got
|
|
226
|
+
assert_equal expected, reported
|
|
227
|
+
assert_equal expected, on_disk
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def test_lines_selector_first_middle_final_line
|
|
231
|
+
assert_edit_result "A\ntwo\nthree\n", edit_lines("one\ntwo\nthree\n", 0, 1, "A")
|
|
232
|
+
assert_edit_result "one\nX\nthree\n", edit_lines("one\ntwo\nthree\n", 1, 2, "X")
|
|
233
|
+
assert_edit_result "one\ntwo\nZ\n", edit_lines("one\ntwo\nthree\n", 2, 3, "Z")
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def test_lines_selector_final_line_keeps_missing_final_newline
|
|
237
|
+
assert_edit_result "one\ntwo\nZ", edit_lines("one\ntwo\nthree", 2, 3, "Z")
|
|
238
|
+
assert_edit_result "one\nX\nthree", edit_lines("one\ntwo\nthree", 1, 2, "X")
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def test_lines_selector_single_line_file_with_and_without_final_newline
|
|
242
|
+
assert_edit_result "new\n", edit_lines("only\n", 0, 1, "new")
|
|
243
|
+
assert_edit_result "new", edit_lines("only", 0, 1, "new")
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def test_lines_selector_replacement_without_trailing_newline_is_corruption_fixed
|
|
247
|
+
assert_edit_result "one\nX\nthree\n", edit_lines("one\ntwo\nthree\n", 1, 2, "X")
|
|
248
|
+
assert_edit_result "a\nchanged\nc\n", edit_lines("a\nb\nc\n", 1, 2, "changed")
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def test_lines_selector_replacement_with_trailing_newline_is_not_doubled
|
|
252
|
+
assert_edit_result "a\nchanged\nc\n", edit_lines("a\nb\nc\n", 1, 2, "changed\n")
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def test_lines_selector_eof_trailing_newline_in_replacement_is_stripped
|
|
256
|
+
# Defect 2: the original has no final newline, so the replacement's
|
|
257
|
+
# trailing newline must NOT introduce one at the EOF boundary.
|
|
258
|
+
assert_edit_result "a\nx", edit_lines("a\nb", 1, 2, "x\n")
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def test_lines_selector_eof_counterpart_keeps_final_newline
|
|
262
|
+
assert_edit_result "a\nx\n", edit_lines("a\nb\n", 1, 2, "x\n")
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def test_lines_selector_eof_repeated_trailing_newlines_are_stripped
|
|
266
|
+
assert_edit_result "a\ny\nz", edit_lines("a\nb\nc", 1, 3, "y\nz\n\n")
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def test_lines_selector_interior_repeated_newlines_collapse_to_one
|
|
270
|
+
assert_edit_result "x\nb\nc", edit_lines("a\nb\nc", 0, 1, "x\n\n")
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def test_lines_selector_eof_multiline_replacement_without_final_newline
|
|
274
|
+
assert_edit_result "a\ny\nz", edit_lines("a\nb\nc", 1, 3, "y\nz")
|
|
275
|
+
assert_edit_result "a\ny\nz\n", edit_lines("a\nb\nc\n", 1, 3, "y\nz")
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def test_lines_selector_eof_more_lines_than_span
|
|
279
|
+
assert_edit_result "a\ny\nz\nw\nq", edit_lines("a\nb\nc", 1, 3, "y\nz\nw\nq")
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def test_lines_selector_eof_fewer_lines_than_span
|
|
283
|
+
assert_edit_result "a\ny\nd", edit_lines("a\nb\nc\nd", 1, 3, "y")
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def test_lines_selector_delete_final_line_at_eof
|
|
287
|
+
assert_edit_result "a\nb", edit_lines("a\nb\nc", 2, 3, "")
|
|
288
|
+
assert_edit_result "a\nb\n", edit_lines("a\nb\nc\n", 2, 3, "")
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def test_lines_selector_delete_interior_line
|
|
292
|
+
assert_edit_result "a\nc", edit_lines("a\nb\nc", 1, 2, "")
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def test_lines_selector_replace_entire_file
|
|
296
|
+
assert_edit_result "z", edit_lines("a\nb\nc", 0, 3, "z")
|
|
297
|
+
assert_edit_result "z\n", edit_lines("a\nb\nc\n", 0, 3, "z\n")
|
|
298
|
+
assert_edit_result "", edit_lines("a\nb\nc\n", 0, 3, "")
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def test_lines_selector_one_line_file_with_and_without_final_newline
|
|
302
|
+
assert_edit_result "new", edit_lines("only", 0, 1, "new")
|
|
303
|
+
assert_edit_result "new\n", edit_lines("only\n", 0, 1, "new\n")
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def test_lines_selector_multiline_replacement_spanning_two_lines
|
|
307
|
+
assert_edit_result "a\nx\ny\nz\nd\n", edit_lines("a\nb\nc\nd\n", 1, 3, "x\ny\nz")
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def test_lines_selector_replacement_with_more_lines_than_span
|
|
311
|
+
assert_edit_result "a\np\nq\nr\nc\n", edit_lines("a\nb\nc\n", 1, 2, "p\nq\nr")
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def test_lines_selector_replacement_with_fewer_lines_than_span
|
|
315
|
+
assert_edit_result "a\nsingle\nd\n", edit_lines("a\nb\nc\nd\n", 1, 3, "single")
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def test_lines_selector_empty_replacement_deletes_span
|
|
319
|
+
assert_edit_result "a\nc\n", edit_lines("a\nb\nc\n", 1, 2, "")
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def test_lines_selector_end_clamped_to_last_line
|
|
323
|
+
assert_edit_result "a\nb\nc\nappend\n", edit_lines("a\nb\nc\n", 3, 5, "append")
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def test_lines_selector_rejects_start_beyond_last_line
|
|
327
|
+
register_text_kind
|
|
328
|
+
@agent.path_write(
|
|
329
|
+
"kind" => "text", "path" => "selector.txt", "content" => "a\nb\n"
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
error = assert_raise(ParameterException) do
|
|
333
|
+
@agent.path_edit(
|
|
334
|
+
"kind" => "text",
|
|
335
|
+
"path" => "selector.txt",
|
|
336
|
+
"selector" => {"type" => "lines", "start" => 5, "end" => 6},
|
|
337
|
+
"replacement" => "x"
|
|
338
|
+
)
|
|
339
|
+
end
|
|
340
|
+
assert_match(/is beyond the last index 2/, error.message)
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def test_lines_selector_rejects_negative_start
|
|
344
|
+
error = assert_raise(ParameterException) do
|
|
345
|
+
@agent.path_apply_selector(
|
|
346
|
+
"a\nb\n", {"type" => "lines", "start" => -1, "end" => 1}, "x"
|
|
347
|
+
)
|
|
348
|
+
end
|
|
349
|
+
assert_match(/must be >= 0, got -1/, error.message)
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
def test_chars_selector_plain_offset_length
|
|
353
|
+
assert_edit_result "hello Scout", edit_chars("hello world", 6, 11, "Scout")
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def test_chars_selector_to_end_of_content
|
|
357
|
+
assert_edit_result "hello", edit_chars("hello world", 5, 11, "")
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
def test_chars_selector_empty_replacement_deletes_exact_bytes
|
|
361
|
+
assert_edit_result "helo", edit_chars("hello", 3, 4, "")
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def test_chars_selector_zero_length_inserts
|
|
365
|
+
assert_edit_result "aXbc", edit_chars("abc", 1, 1, "X")
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def test_chars_selector_preserves_untouched_prefix_and_suffix
|
|
369
|
+
assert_edit_result "aaXYbb", edit_chars("aaaabb", 2, 4, "XY")
|
|
370
|
+
assert_edit_result "aaaab\nb\n", edit_chars("aaaab\nb\n", 0, 0, "")
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
def test_chars_selector_multibyte_character_boundaries
|
|
374
|
+
# String#[]= operates on characters, not bytes; boundaries are safe.
|
|
375
|
+
assert_edit_result "hÉllo", edit_chars("héllo", 1, 2, "É")
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
# ------------------------------------------------------------------
|
|
379
|
+
# Registry authority (single instance registry)
|
|
380
|
+
# ------------------------------------------------------------------
|
|
381
|
+
|
|
382
|
+
def edit_schema_without_kind_enum(schema)
|
|
383
|
+
copy = Marshal.load(Marshal.dump(schema))
|
|
384
|
+
props = copy[:parameters][:properties]
|
|
385
|
+
props.delete("kind") if props["kind"]
|
|
386
|
+
props.delete(:kind) if props[:kind]
|
|
387
|
+
fn = copy[:function] and fn[:parameters][:properties].delete(:kind) rescue nil
|
|
388
|
+
copy
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def test_edit_schema_unchanged_by_edit_override_registration
|
|
392
|
+
register_text_kind
|
|
393
|
+
before = edit_schema_without_kind_enum(
|
|
394
|
+
@agent.instance_variable_get(:@other_options)[:tools]["path_edit"][1]
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
@agent.register_path_kind(
|
|
398
|
+
"override_kind",
|
|
399
|
+
"roots" => {"project" => @tmpdir},
|
|
400
|
+
"operations" => {},
|
|
401
|
+
"overrides" => {
|
|
402
|
+
"edit" => ->(content, args) { content }
|
|
403
|
+
}
|
|
404
|
+
)
|
|
405
|
+
|
|
406
|
+
after = edit_schema_without_kind_enum(
|
|
407
|
+
@agent.instance_variable_get(:@other_options)[:tools]["path_edit"][1]
|
|
408
|
+
)
|
|
409
|
+
assert_equal before, after
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
def test_edit_schema_unchanged_by_custom_operation_registration
|
|
413
|
+
register_text_kind
|
|
414
|
+
before = edit_schema_without_kind_enum(
|
|
415
|
+
@agent.instance_variable_get(:@other_options)[:tools]["path_edit"][1]
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
@agent.register_path_kind(
|
|
419
|
+
"specialized",
|
|
420
|
+
"roots" => {"project" => @tmpdir},
|
|
421
|
+
"operations" => {
|
|
422
|
+
"precise_edit" => {
|
|
423
|
+
"description" => "Replace exact occurrences of old with new",
|
|
424
|
+
"parameters" => {
|
|
425
|
+
"properties" => {
|
|
426
|
+
"old_text" => {"type" => "string"},
|
|
427
|
+
"new_text" => {"type" => "string"},
|
|
428
|
+
"count" => {"type" => "integer"}
|
|
429
|
+
},
|
|
430
|
+
"required" => %w[old_text new_text]
|
|
431
|
+
},
|
|
432
|
+
"implementation" => ->(content, args) { content },
|
|
433
|
+
"authorization" => :write,
|
|
434
|
+
"type" => :content
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
after = edit_schema_without_kind_enum(
|
|
440
|
+
@agent.instance_variable_get(:@other_options)[:tools]["path_edit"][1]
|
|
441
|
+
)
|
|
442
|
+
assert_equal before, after
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
def test_edit_schema_matches_pre_architecture_reference
|
|
446
|
+
register_text_kind
|
|
447
|
+
# Reference recorded in tmp/path_recon.md (Phase-0 baseline and
|
|
448
|
+
# Step-7 source comparison): HEAD's edit schema block is byte
|
|
449
|
+
# identical to the live one (verified by diff); assert the exact
|
|
450
|
+
# public shape here so future drift fails loudly.
|
|
451
|
+
schema = edit_schema_without_kind_enum(
|
|
452
|
+
@agent.instance_variable_get(:@other_options)[:tools]["path_edit"][1]
|
|
453
|
+
)
|
|
454
|
+
props = schema[:parameters][:properties]
|
|
455
|
+
assert_equal %w[path location selector replacement], props.keys
|
|
456
|
+
assert_equal "string", props[:path][:type]
|
|
457
|
+
assert_equal "string", props[:location][:type]
|
|
458
|
+
assert_equal %w[lines chars regexp], props[:selector][:properties][:type][:enum]
|
|
459
|
+
assert_equal "integer", props[:selector][:properties][:start][:type]
|
|
460
|
+
assert_equal "integer", props[:selector][:properties][:end][:type]
|
|
461
|
+
assert_equal "string", props[:selector][:properties][:pattern][:type]
|
|
462
|
+
assert_equal "string", props[:replacement][:type]
|
|
463
|
+
assert_equal "Replacement text.", props[:replacement][:description]
|
|
464
|
+
assert_equal "Edit a textual Path using a line range, character range, or regular expression selector.",
|
|
465
|
+
schema[:description]
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
def test_precise_edit_custom_operation_full_path
|
|
469
|
+
register_text_kind
|
|
470
|
+
edits = []
|
|
471
|
+
@agent.register_path_kind(
|
|
472
|
+
"special",
|
|
473
|
+
"roots" => {"project" => @tmpdir},
|
|
474
|
+
"operations" => {
|
|
475
|
+
"precise_edit" => {
|
|
476
|
+
"description" => "Replace exact occurrences",
|
|
477
|
+
"parameters" => {
|
|
478
|
+
"properties" => {
|
|
479
|
+
"old_text" => {"type" => "string"},
|
|
480
|
+
"new_text" => {"type" => "string"},
|
|
481
|
+
"count" => {"type" => "integer"}
|
|
482
|
+
},
|
|
483
|
+
"required" => %w[old_text new_text]
|
|
484
|
+
},
|
|
485
|
+
"implementation" => ->(content, args) {
|
|
486
|
+
edits << args
|
|
487
|
+
content.gsub(args["old_text"], args["new_text"])
|
|
488
|
+
},
|
|
489
|
+
"type" => :content
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
)
|
|
493
|
+
|
|
494
|
+
tools = @agent.instance_variable_get(:@other_options)[:tools]
|
|
495
|
+
assert tools.key?("precise_edit")
|
|
496
|
+
assert_equal 1, tools.keys.count { |k| k == "precise_edit" }
|
|
497
|
+
params = tools["precise_edit"][1][:parameters][:properties]
|
|
498
|
+
assert_equal %w[kind path location old_text new_text count], params.keys
|
|
499
|
+
|
|
500
|
+
@agent.path_write(
|
|
501
|
+
"kind" => "special", "path" => "notes.txt", "content" => "one two one\n"
|
|
502
|
+
)
|
|
503
|
+
result = @agent.path_dispatch(
|
|
504
|
+
kind: "special", operation: "precise_edit",
|
|
505
|
+
"path" => "notes.txt", "old_text" => "one", "new_text" => "1"
|
|
506
|
+
)
|
|
507
|
+
assert_equal "1 two 1\n", File.read(File.join(@tmpdir, "notes.txt"))
|
|
508
|
+
assert edits.any?, "callback must run after authorization"
|
|
509
|
+
|
|
510
|
+
# Global edit schema untouched by the custom registration
|
|
511
|
+
edit_after = edit_schema_without_kind_enum(
|
|
512
|
+
@agent.instance_variable_get(:@other_options)[:tools]["path_edit"][1]
|
|
513
|
+
)
|
|
514
|
+
assert_equal %w[path location selector replacement],
|
|
515
|
+
edit_after[:parameters][:properties].keys
|
|
516
|
+
end
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
# ------------------------------------------------------------------
|
|
520
|
+
# Step 3 (DC-2) — type-aware edit dispatch
|
|
521
|
+
# ------------------------------------------------------------------
|
|
522
|
+
|
|
523
|
+
def register_edit_override_kind(spec, name = "fancy_edit")
|
|
524
|
+
@agent.register_path_kind(
|
|
525
|
+
name,
|
|
526
|
+
"roots" => {"project" => @tmpdir},
|
|
527
|
+
"overrides" => {"edit" => spec}
|
|
528
|
+
)
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
def test_full_edit_override_fires_with_agent_target_and_args
|
|
532
|
+
calls = []
|
|
533
|
+
register_edit_override_kind(
|
|
534
|
+
"implementation" => lambda { |agent, target, args|
|
|
535
|
+
calls << [agent, target, args["path"]]
|
|
536
|
+
original = File.read(target.to_s)
|
|
537
|
+
File.write(target.to_s, original.sub("one", "ONE"))
|
|
538
|
+
{"applied_count" => 1, "content" => "handler-lie"}
|
|
539
|
+
},
|
|
540
|
+
"type" => :full
|
|
541
|
+
)
|
|
542
|
+
|
|
543
|
+
@agent.path_write(
|
|
544
|
+
"kind" => "fancy_edit", "path" => "note.txt", "content" => "one two\n"
|
|
545
|
+
)
|
|
546
|
+
|
|
547
|
+
result = @agent.path_edit(
|
|
548
|
+
"kind" => "fancy_edit", "path" => "note.txt",
|
|
549
|
+
"selector" => {"type" => "lines", "start" => 0, "end" => 0},
|
|
550
|
+
"replacement" => "ignored"
|
|
551
|
+
)
|
|
552
|
+
|
|
553
|
+
target = File.join(@tmpdir, "note.txt")
|
|
554
|
+
assert_equal 1, calls.length
|
|
555
|
+
assert_same @agent, calls.first.first
|
|
556
|
+
assert_equal target, calls.first[1]
|
|
557
|
+
assert_equal "note.txt", calls.first[2]
|
|
558
|
+
|
|
559
|
+
# Verified reality at the authorized target, not the handler's claim
|
|
560
|
+
assert_equal "ONE two\n", File.read(target)
|
|
561
|
+
assert_equal "ONE two\n", result["content"]
|
|
562
|
+
assert_equal true, result["changed"]
|
|
563
|
+
assert_equal 1, result["applied_count"]
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
def test_full_edit_override_non_hash_return_is_parameter_error_envelope
|
|
567
|
+
register_edit_override_kind(
|
|
568
|
+
"implementation" => ->(agent, target, args) { "NOT A HASH" },
|
|
569
|
+
"type" => :full
|
|
570
|
+
)
|
|
571
|
+
|
|
572
|
+
@agent.path_write(
|
|
573
|
+
"kind" => "fancy_edit", "path" => "note.txt", "content" => "body\n"
|
|
574
|
+
)
|
|
575
|
+
|
|
576
|
+
tools = @agent.instance_variable_get(:@other_options)[:tools]
|
|
577
|
+
result = JSON.parse(
|
|
578
|
+
tools["path_edit"].first.call(
|
|
579
|
+
"path_edit",
|
|
580
|
+
{"kind" => "fancy_edit", "path" => "note.txt",
|
|
581
|
+
"selector" => {"type" => "lines", "start" => 0, "end" => 0},
|
|
582
|
+
"replacement" => "x"}
|
|
583
|
+
)
|
|
584
|
+
)
|
|
585
|
+
|
|
586
|
+
assert_match(/must return a Hash payload/, result["exception"])
|
|
587
|
+
assert result.key?("exception_line")
|
|
588
|
+
# The handler performed no write; the file is untouched
|
|
589
|
+
assert_equal "body\n", File.read(File.join(@tmpdir, "note.txt"))
|
|
590
|
+
end
|
|
591
|
+
|
|
592
|
+
def test_content_edit_override_receives_content_and_args_and_writes_string
|
|
593
|
+
calls = []
|
|
594
|
+
register_edit_override_kind(
|
|
595
|
+
lambda { |content, args|
|
|
596
|
+
calls << [content, args["path"]]
|
|
597
|
+
content.upcase
|
|
598
|
+
}
|
|
599
|
+
)
|
|
600
|
+
|
|
601
|
+
@agent.path_write(
|
|
602
|
+
"kind" => "fancy_edit", "path" => "note.txt", "content" => "body\n"
|
|
603
|
+
)
|
|
604
|
+
|
|
605
|
+
result = @agent.path_edit(
|
|
606
|
+
"kind" => "fancy_edit", "path" => "note.txt",
|
|
607
|
+
"selector" => {"type" => "lines", "start" => 0, "end" => 0},
|
|
608
|
+
"replacement" => "ignored"
|
|
609
|
+
)
|
|
610
|
+
|
|
611
|
+
target = File.join(@tmpdir, "note.txt")
|
|
612
|
+
assert_equal [["body\n", "note.txt"]], calls
|
|
613
|
+
assert_equal "BODY\n", File.read(target)
|
|
614
|
+
assert_equal "BODY\n", result["content"]
|
|
615
|
+
assert_equal true, result["changed"]
|
|
616
|
+
end
|
|
617
|
+
|
|
618
|
+
def test_content_edit_override_selector_replacement_not_applied
|
|
619
|
+
# PINNED CONTRACT (DC-2 decision): under a :content edit override the
|
|
620
|
+
# selector/replacement arguments are validated but NOT applied; the
|
|
621
|
+
# handler's returned content IS the edited file. Documented in
|
|
622
|
+
# doc/developer/PathEngine.md §5.
|
|
623
|
+
register_edit_override_kind(
|
|
624
|
+
lambda { |content, args| "REWRITTEN\n" }
|
|
625
|
+
)
|
|
626
|
+
|
|
627
|
+
@agent.path_write(
|
|
628
|
+
"kind" => "fancy_edit", "path" => "note.txt",
|
|
629
|
+
"content" => "line 0\nline 1\n"
|
|
630
|
+
)
|
|
631
|
+
|
|
632
|
+
# A selector that would, if applied, replace only line 1
|
|
633
|
+
result = @agent.path_edit(
|
|
634
|
+
"kind" => "fancy_edit", "path" => "note.txt",
|
|
635
|
+
"selector" => {"type" => "lines", "start" => 1, "end" => 1},
|
|
636
|
+
"replacement" => "SELECTOR APPLIED"
|
|
637
|
+
)
|
|
638
|
+
|
|
639
|
+
target = File.join(@tmpdir, "note.txt")
|
|
640
|
+
assert_equal "REWRITTEN\n", File.read(target),
|
|
641
|
+
"handler return must land in the file wholesale"
|
|
642
|
+
refute File.read(target).include?("SELECTOR APPLIED"),
|
|
643
|
+
"selector/replacement must not be applied under a content edit override"
|
|
644
|
+
assert_equal "REWRITTEN\n", result["content"]
|
|
645
|
+
end
|
|
646
|
+
|
|
647
|
+
def test_full_edit_override_authorization_failure_runs_no_handler
|
|
648
|
+
calls = []
|
|
649
|
+
register_edit_override_kind(
|
|
650
|
+
"implementation" => ->(agent, target, args) { calls << target },
|
|
651
|
+
"type" => :full
|
|
652
|
+
)
|
|
653
|
+
|
|
654
|
+
assert_raise(ParameterException) do
|
|
655
|
+
@agent.path_edit(
|
|
656
|
+
"kind" => "fancy_edit", "path" => "../../etc/hostname",
|
|
657
|
+
"selector" => {"type" => "lines", "start" => 0, "end" => 0},
|
|
658
|
+
"replacement" => "denied"
|
|
659
|
+
)
|
|
660
|
+
end
|
|
661
|
+
|
|
662
|
+
assert_empty calls
|
|
663
|
+
end
|
|
664
|
+
|
|
665
|
+
# Step 6: patch operation
|
|
666
|
+
# ------------------------------------------------------------------
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
# ------------------------------------------------------------------
|
|
670
|
+
# H-011 regression (path-edit corruption cluster, step 2 of the fix plan).
|
|
671
|
+
#
|
|
672
|
+
# Incident: `path_edit` with a `lines` selector on a multi-line
|
|
673
|
+
# single-quoted YAML scalar duplicated preceding lines in Research
|
|
674
|
+
# Hypothesis record H-011 across v5-v12; sloppy repairs added further
|
|
675
|
+
# duplication (v12->v13 appended `demonstration)` onto an existing list
|
|
676
|
+
# item; v13->v14 cleaned a `the migration manifest. the migration
|
|
677
|
+
# manifest.` duplication).
|
|
678
|
+
#
|
|
679
|
+
# Step-1 attribution evidence (probed exhaustively in a fresh process):
|
|
680
|
+
# the generic `lines` splice alone NEVER duplicates a preceding/head line;
|
|
681
|
+
# duplication arises exactly when the caller-supplied replacement itself
|
|
682
|
+
# carries the preceding text (the agent re-pasted lines it had already
|
|
683
|
+
# read). These tests pin, through the PUBLIC path_edit operation:
|
|
684
|
+
#
|
|
685
|
+
# 1. the generic layer writes a corruption-shaped replacement
|
|
686
|
+
# FAITHFULLY (no YAML parsing, no duplicate-text heuristics may enter
|
|
687
|
+
# AgentPathEdit; `lines` keeps meaning "replace these physical
|
|
688
|
+
# lines"),
|
|
689
|
+
# 2. the INTENDED edit is byte-exact and leaves unselected lines
|
|
690
|
+
# byte-identical,
|
|
691
|
+
# 3. the same public operation against a kind registering the SAME
|
|
692
|
+
# `:full` edit override shape the Research `hypothesis` kind uses
|
|
693
|
+
# (records.rb L345-L353: re-apply the caller's selector/replacement
|
|
694
|
+
# through agent.path_apply_selector, then validate and re-serialize)
|
|
695
|
+
# commits the corruption end to end: the re-parse CONCATENATES the
|
|
696
|
+
# duplicated fragments into one statement value, which is how H-011
|
|
697
|
+
# v5-v12 corruption persisted through record validation.
|
|
698
|
+
# ------------------------------------------------------------------
|
|
699
|
+
|
|
700
|
+
# The reviewer's illustrative multi-line single-quoted scalar fixture
|
|
701
|
+
# (commentary cL2785-2791), carried over from the step-1 reconstruction.
|
|
702
|
+
H011_FIXTURE_LINES = [
|
|
703
|
+
"statement: 'The initial interpretation remains provisional\n",
|
|
704
|
+
" because the shared response is also present in the control.\n",
|
|
705
|
+
" The final qualification must remain intact.'\n",
|
|
706
|
+
"scope:\n",
|
|
707
|
+
" population: \"cohort A\"\n",
|
|
708
|
+
"limitations:\n",
|
|
709
|
+
" - \"No biological replicates\"\n"
|
|
710
|
+
].freeze
|
|
711
|
+
H011_FIXTURE = H011_FIXTURE_LINES.join.freeze
|
|
712
|
+
|
|
713
|
+
def test_h011_generic_lines_edit_writes_duplication_faithfully_when_replacement_duplicates
|
|
714
|
+
register_text_kind
|
|
715
|
+
|
|
716
|
+
@agent.path_write(
|
|
717
|
+
"kind" => "text",
|
|
718
|
+
"path" => "doc.yaml",
|
|
719
|
+
"content" => H011_FIXTURE
|
|
720
|
+
)
|
|
721
|
+
|
|
722
|
+
# Corruption-shaped edit: insert lines 0-1 verbatim between line 0 and
|
|
723
|
+
# line 1 (start == end == 1 is a zero-length span, i.e. an insertion
|
|
724
|
+
# point, per the documented exclusive-end semantics; the step-1
|
|
725
|
+
# reconstruction's exact reproducer). The public operation must write
|
|
726
|
+
# it faithfully: the generic layer is a predictable text editor, not a
|
|
727
|
+
# validator. Pins candidate (a) as disproved and the fix constraint
|
|
728
|
+
# that `lines` semantics stay untouched.
|
|
729
|
+
result = @agent.path_edit(
|
|
730
|
+
"kind" => "text",
|
|
731
|
+
"path" => "doc.yaml",
|
|
732
|
+
"selector" => {"type" => "lines", "start" => 1, "end" => 1},
|
|
733
|
+
"replacement" => H011_FIXTURE_LINES[0, 2].join
|
|
734
|
+
)
|
|
735
|
+
|
|
736
|
+
# head = [line 0]; the span [1...1] is empty so the replacement is
|
|
737
|
+
# inserted verbatim; tail = lines 1.. untouched.
|
|
738
|
+
expected =
|
|
739
|
+
H011_FIXTURE_LINES[0, 1].join +
|
|
740
|
+
H011_FIXTURE_LINES[0, 2].join +
|
|
741
|
+
H011_FIXTURE_LINES[1..].join
|
|
742
|
+
|
|
743
|
+
assert_equal expected, result["content"],
|
|
744
|
+
"the generic lines splice must reproduce the incident's duplication " \
|
|
745
|
+
"signature verbatim when the replacement carries preceding text: this " \
|
|
746
|
+
"pins the attribution that the generic layer is a faithful text " \
|
|
747
|
+
"editor, never a silent repairer"
|
|
748
|
+
assert_equal expected, File.read(File.join(@tmpdir, "doc.yaml"))
|
|
749
|
+
assert_equal true, result["changed"]
|
|
750
|
+
end
|
|
751
|
+
|
|
752
|
+
def test_h011_generic_lines_edit_intended_replacement_is_byte_exact
|
|
753
|
+
register_text_kind
|
|
754
|
+
|
|
755
|
+
@agent.path_write(
|
|
756
|
+
"kind" => "text",
|
|
757
|
+
"path" => "doc.yaml",
|
|
758
|
+
"content" => H011_FIXTURE
|
|
759
|
+
)
|
|
760
|
+
|
|
761
|
+
# The same public operation with the INTENDED replacement: replace the
|
|
762
|
+
# single physical line 1 with its own text (span [1...2] is exactly that
|
|
763
|
+
# line). Byte-identical document, every unselected line untouched.
|
|
764
|
+
result = @agent.path_edit(
|
|
765
|
+
"kind" => "text",
|
|
766
|
+
"path" => "doc.yaml",
|
|
767
|
+
"selector" => {"type" => "lines", "start" => 1, "end" => 2},
|
|
768
|
+
"replacement" => H011_FIXTURE_LINES[1]
|
|
769
|
+
)
|
|
770
|
+
|
|
771
|
+
assert_equal H011_FIXTURE, result["content"],
|
|
772
|
+
"replacing line 1 with its own text must leave the document " \
|
|
773
|
+
"byte-identical, including the folded single-quoted scalar"
|
|
774
|
+
assert_equal H011_FIXTURE, File.read(File.join(@tmpdir, "doc.yaml"))
|
|
775
|
+
assert_equal false, result["changed"]
|
|
776
|
+
end
|
|
777
|
+
|
|
778
|
+
# Minimal mirror of the Research records.rb `:full` edit override
|
|
779
|
+
# (records.rb L345-L353 registration, L448-L522 transform, quoted verbatim
|
|
780
|
+
# in the bout report): the override receives the resolved authorized
|
|
781
|
+
# target, re-applies the caller's selector/replacement through
|
|
782
|
+
# agent.path_apply_selector, re-parses the candidate, and - decisive for
|
|
783
|
+
# the incident - RE-SERIALIZES AND COMMITS whatever still parses. A :full
|
|
784
|
+
# override must return a Hash payload (engine contract, edit.rb DC-2).
|
|
785
|
+
#
|
|
786
|
+
# The fixture is a serializer-format record whose `statement` is a
|
|
787
|
+
# multi-line folded scalar - the shape whose physical-line edits can
|
|
788
|
+
# duplicate folded content (the incident's single-quoted folded scalar is
|
|
789
|
+
# covered by the Research-side regression, which drives the real kind).
|
|
790
|
+
H011_RECORD = {
|
|
791
|
+
"version" => 1,
|
|
792
|
+
"statement" =>
|
|
793
|
+
"The initial interpretation remains provisional because the shared " \
|
|
794
|
+
"response is also present in the control. The final qualification " \
|
|
795
|
+
"must remain intact.",
|
|
796
|
+
"scope" => {"population" => "cohort A"},
|
|
797
|
+
"status" => "proposed",
|
|
798
|
+
"limitations" => ["No biological replicates"]
|
|
799
|
+
}.freeze
|
|
800
|
+
|
|
801
|
+
def register_h011_full_override_kind
|
|
802
|
+
require "yaml"
|
|
803
|
+
@agent.register_path_kind(
|
|
804
|
+
"recordish",
|
|
805
|
+
"roots" => {"project" => @tmpdir},
|
|
806
|
+
"capabilities" => {"edit" => true},
|
|
807
|
+
"overrides" => {
|
|
808
|
+
"edit" => {
|
|
809
|
+
"type" => :full,
|
|
810
|
+
"implementation" => lambda { |agent, target, args|
|
|
811
|
+
current_text = File.read(target.to_s)
|
|
812
|
+
candidate_text =
|
|
813
|
+
agent.path_apply_selector(current_text,
|
|
814
|
+
args["selector"], args["replacement"])
|
|
815
|
+
candidate = YAML.safe_load(candidate_text,
|
|
816
|
+
permitted_classes: [], aliases: false)
|
|
817
|
+
return {"changed" => false} if candidate_text == current_text
|
|
818
|
+
|
|
819
|
+
candidate["version"] += 1
|
|
820
|
+
File.write(target.to_s, YAML.dump(candidate))
|
|
821
|
+
{"changed" => true, "version" => candidate["version"]}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
)
|
|
826
|
+
end
|
|
827
|
+
|
|
828
|
+
def h011_write_record(name)
|
|
829
|
+
require "yaml"
|
|
830
|
+
@agent.path_write(
|
|
831
|
+
"kind" => "recordish",
|
|
832
|
+
"path" => name,
|
|
833
|
+
"content" => YAML.dump(H011_RECORD)
|
|
834
|
+
)
|
|
835
|
+
File.join(@tmpdir, name)
|
|
836
|
+
end
|
|
837
|
+
|
|
838
|
+
def h011_statement_line_index(path)
|
|
839
|
+
File.readlines(path).index { |line| line.start_with?("statement:") }
|
|
840
|
+
end
|
|
841
|
+
|
|
842
|
+
def test_h011_full_override_commits_duplicated_folded_scalar_verbatim
|
|
843
|
+
register_h011_full_override_kind
|
|
844
|
+
record = h011_write_record("H-001.yaml")
|
|
845
|
+
stmt_idx = h011_statement_line_index(record)
|
|
846
|
+
body_lines = File.readlines(record)
|
|
847
|
+
|
|
848
|
+
# The corruption-shaped edit the incident agent performed: replace the
|
|
849
|
+
# statement VALUE line with the statement line plus its own continuation
|
|
850
|
+
# line verbatim (re-pasting what it had just read). The override
|
|
851
|
+
# re-parses the candidate - and Psych folds the duplicated fragments
|
|
852
|
+
# into ONE statement value with the text concatenated - so the record
|
|
853
|
+
# layer commits the corruption and bumps the version. This pins, at
|
|
854
|
+
# engine level, the mechanism that let H-011 v5-v12 duplication persist
|
|
855
|
+
# through record validation: the engine hands the override the caller's
|
|
856
|
+
# selector/replacement verbatim, and the override's own validation is
|
|
857
|
+
# the only gate. The REJECTING regression lives in the Research checkout
|
|
858
|
+
# (test/Research/path_kinds/test_h011_lines_edit.rb).
|
|
859
|
+
# ScoutCoder: under a :full edit override the engine hands the override
|
|
860
|
+
# the caller's selector/replacement VERBATIM (path_apply_selector inside
|
|
861
|
+
# the handler) and the override's own validation is the ONLY gate - a
|
|
862
|
+
# corruption-shaped `lines` edit that still YAML-parses is committed and
|
|
863
|
+
# version-bumped, because Psych folds duplicated folded-scalar fragments
|
|
864
|
+
# into one value. Kinds owning :full edit overrides must reject, never
|
|
865
|
+
# repair or commit, corruption-shaped candidates.
|
|
866
|
+
result = @agent.path_edit(
|
|
867
|
+
"kind" => "recordish",
|
|
868
|
+
"path" => "H-001.yaml",
|
|
869
|
+
"selector" => {"type" => "lines", "start" => stmt_idx, "end" => stmt_idx + 1},
|
|
870
|
+
"replacement" => body_lines[stmt_idx, 2].join
|
|
871
|
+
)
|
|
872
|
+
|
|
873
|
+
parsed = YAML.safe_load(File.read(record), permitted_classes: [], aliases: false)
|
|
874
|
+
assert_equal "The initial interpretation remains provisional because the " \
|
|
875
|
+
"shared response is also present in the control. The final " \
|
|
876
|
+
"qualification must remain intact. is also present in the " \
|
|
877
|
+
"control. The final qualification must remain intact.",
|
|
878
|
+
parsed["statement"],
|
|
879
|
+
"the override-mediated edit must commit the duplication signature " \
|
|
880
|
+
"verbatim (duplicated folded text concatenated into one value)"
|
|
881
|
+
assert_equal 2, parsed["version"]
|
|
882
|
+
assert_equal true, result["changed"]
|
|
883
|
+
end
|
|
884
|
+
|
|
885
|
+
def test_h011_full_override_intended_edit_preserves_scalar_value
|
|
886
|
+
register_h011_full_override_kind
|
|
887
|
+
record = h011_write_record("H-002.yaml")
|
|
888
|
+
stmt_idx = h011_statement_line_index(record)
|
|
889
|
+
|
|
890
|
+
# The INTENDED edit: replace the statement VALUE line with its own text.
|
|
891
|
+
# The scalar VALUE must survive the parse/serialize round trip untouched
|
|
892
|
+
# and the edit must be reported as a no-op.
|
|
893
|
+
result = @agent.path_edit(
|
|
894
|
+
"kind" => "recordish",
|
|
895
|
+
"path" => "H-002.yaml",
|
|
896
|
+
"selector" => {"type" => "lines", "start" => stmt_idx, "end" => stmt_idx + 1},
|
|
897
|
+
"replacement" => File.readlines(record)[stmt_idx]
|
|
898
|
+
)
|
|
899
|
+
|
|
900
|
+
parsed = YAML.safe_load(File.read(record), permitted_classes: [], aliases: false)
|
|
901
|
+
assert_equal H011_RECORD["statement"], parsed["statement"],
|
|
902
|
+
"the intended edit must leave the folded scalar VALUE byte-identical " \
|
|
903
|
+
"after the override's parse/serialize round trip"
|
|
904
|
+
assert_equal false, result["changed"]
|
|
905
|
+
end
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
end
|