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
|
@@ -42,7 +42,109 @@ class TestLLMEmbed < Test::Unit::TestCase
|
|
|
42
42
|
assert cos(shared, similar) > cos(shared, other)
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
+
# ScoutCoder: hermetic against request-level env. EMBED_ENDPOINT /
|
|
46
|
+
# LLM_ENDPOINT act as an EXPLICIT endpoint request (same precedence as the
|
|
47
|
+
# options hash), so a value leaked into the test process would make even a
|
|
48
|
+
# backend-pinned call raise "Endpoint not found". Neutralize both for the
|
|
49
|
+
# duration of the assertion; the repo-level config pin (backend: :mock)
|
|
50
|
+
# then governs dispatch. The raise-on-requested-but-absent behavior itself
|
|
51
|
+
# is covered in test_embed_missing_endpoint_raises.
|
|
45
52
|
def test_embed_explicit_backend
|
|
46
|
-
|
|
53
|
+
saved_embed_endpoint, saved_llm_endpoint = ENV['EMBED_ENDPOINT'], ENV['LLM_ENDPOINT']
|
|
54
|
+
ENV.delete 'EMBED_ENDPOINT'
|
|
55
|
+
ENV.delete 'LLM_ENDPOINT'
|
|
56
|
+
begin
|
|
57
|
+
assert_equal LLM::Mock.embed('a text', endpoint: :mock), LLM.embed('a text', backend: :mock)
|
|
58
|
+
ensure
|
|
59
|
+
ENV['EMBED_ENDPOINT'] = saved_embed_endpoint if saved_embed_endpoint
|
|
60
|
+
ENV['LLM_ENDPOINT'] = saved_llm_endpoint if saved_llm_endpoint
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# ScoutCoder: bug M4 regression. The endpoint yaml lookup used the
|
|
65
|
+
# extensionless `Scout.etc.AI[endpoint].exists?`, so a standard
|
|
66
|
+
# `etc/AI/<endpoint>.yaml` was never merged (asymmetric with ask/image,
|
|
67
|
+
# which use find_with_extension(:yaml)), and a missing endpoint fell
|
|
68
|
+
# through to the config defaults silently. Both probed offline here with
|
|
69
|
+
# a throwaway endpoint yaml and a recording backend in the registry.
|
|
70
|
+
def test_embed_endpoint_yaml_is_merged
|
|
71
|
+
base = tmpdir
|
|
72
|
+
etc_ai = Path.setup(base)['etc/AI']
|
|
73
|
+
Open.mkdir(etc_ai)
|
|
74
|
+
Open.write(etc_ai['m4_endpoint.yaml'], {'backend' => 'm4_rec', 'model' => 'm4-model'}.to_yaml)
|
|
75
|
+
|
|
76
|
+
Scout.prepend_path :m4_test_etc, File.join(base, '{TOPLEVEL}/{SUBPATH}')
|
|
77
|
+
received = nil
|
|
78
|
+
LLM::BACKENDS[:m4_rec] = Module.new do
|
|
79
|
+
define_singleton_method(:embed) do |_text, options|
|
|
80
|
+
$m4_received = IndiferentHash.setup(options.dup)
|
|
81
|
+
[0.0] * LLM::Mock::DIMENSIONS
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# sanity: the yaml exists for the with-extension lookup and NOT for the
|
|
86
|
+
# extensionless one that used to gate the merge
|
|
87
|
+
assert Scout.etc.AI[:m4_endpoint].find_with_extension(:yaml).exists?
|
|
88
|
+
assert !Scout.etc.AI[:m4_endpoint].exists?
|
|
89
|
+
|
|
90
|
+
LLM.embed('a text', endpoint: :m4_endpoint)
|
|
91
|
+
assert_equal 'm4-model', $m4_received['model']
|
|
92
|
+
|
|
93
|
+
# an endpoint yaml naming a backend drives dispatch through the registry
|
|
94
|
+
LLM::BACKENDS.delete(:m4_rec)
|
|
95
|
+
Open.write(etc_ai['m4_endpoint.yaml'], {'backend' => 'mock'}.to_yaml)
|
|
96
|
+
assert_equal LLM::Mock::DIMENSIONS, LLM.embed('a text', endpoint: :m4_endpoint).length
|
|
97
|
+
ensure
|
|
98
|
+
LLM::BACKENDS.delete(:m4_rec)
|
|
99
|
+
Scout.path_maps.delete(:m4_test_etc) if Scout.path_maps.include?(:m4_test_etc)
|
|
100
|
+
Scout.map_order.delete(:m4_test_etc) if Scout.map_order.include?(:m4_test_etc)
|
|
101
|
+
$m4_received = nil
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def test_embed_missing_endpoint_raises
|
|
105
|
+
# ask/image raise "Endpoint not found <name>" for a requested-but-absent
|
|
106
|
+
# endpoint; embed used to fall through to the config defaults silently.
|
|
107
|
+
# The synthesized `:embed` fallback name is NOT a request, so a call
|
|
108
|
+
# without any endpoint still works (backend-configured path).
|
|
109
|
+
error = assert_raise(RuntimeError) { LLM.embed('a text', endpoint: :m4_no_such_endpoint) }
|
|
110
|
+
assert_include error.message, 'Endpoint not found'
|
|
111
|
+
|
|
112
|
+
# ScoutCoder: hermetic. With no explicit endpoint the synthesized `:embed`
|
|
113
|
+
# name is still resolved as etc/AI/embed.yaml, and a user-level file there
|
|
114
|
+
# (~/.scout/etc/AI/embed.yaml, e.g. `backend: ollama`) legitimately
|
|
115
|
+
# contributes defaults AHEAD of the config-pinned backend, exactly like
|
|
116
|
+
# LLM.ask for its endpoint yaml. On an account with such a file (and
|
|
117
|
+
# OLLAMA_URL/OLLAMA_KEY exported) the bare call dispatches to that backend
|
|
118
|
+
# and returns its dimensionality (observed: 1024 for mxbai-embed-large),
|
|
119
|
+
# so this no-endpoint assertion pins the backend explicitly instead of
|
|
120
|
+
# relying on the absence of user configuration. The endpoint resolution
|
|
121
|
+
# path is still exercised: the synthesized `:embed` name is looked up as a
|
|
122
|
+
# yaml and must not raise, while a genuinely requested endpoint does.
|
|
123
|
+
#
|
|
124
|
+
# EMBED_ENDPOINT/LLM_ENDPOINT are REQUEST-level environment, i.e. an
|
|
125
|
+
# explicit endpoint request for every embed call; a leaked value would
|
|
126
|
+
# make even these calls raise "Endpoint not found". Neutralize it inside
|
|
127
|
+
# the test (ENV.delete inside an ensure-protected block) so the
|
|
128
|
+
# environment-dependent assertions below are hermetic while the repo-level
|
|
129
|
+
# config pin (backend: :mock in test_helper) still governs resolution.
|
|
130
|
+
saved_embed_endpoint, saved_llm_endpoint = ENV['EMBED_ENDPOINT'], ENV['LLM_ENDPOINT']
|
|
131
|
+
ENV.delete 'EMBED_ENDPOINT'
|
|
132
|
+
ENV.delete 'LLM_ENDPOINT'
|
|
133
|
+
begin
|
|
134
|
+
assert_equal LLM::Mock::DIMENSIONS, LLM.embed('a text', backend: :mock).length
|
|
135
|
+
# The bare call exercises the synthesized-fallback path itself: no raise,
|
|
136
|
+
# and it uses the offline backend UNLESS the environment legitimately
|
|
137
|
+
# configures an etc/AI/embed.yaml (a user-level file resolves through the
|
|
138
|
+
# same path maps ask/image use). Keep the assertion hermetic: assert the
|
|
139
|
+
# offline dimensionality only when no such endpoint yaml is resolvable,
|
|
140
|
+
# otherwise the dimensionality belongs to the configured backend and the
|
|
141
|
+
# pinned call above already proves the fallback does not raise.
|
|
142
|
+
unless Scout.etc.AI[:embed].find_with_extension(:yaml).exists?
|
|
143
|
+
assert_equal LLM::Mock::DIMENSIONS, LLM.embed('a text').length
|
|
144
|
+
end
|
|
145
|
+
ensure
|
|
146
|
+
ENV['EMBED_ENDPOINT'] = saved_embed_endpoint if saved_embed_endpoint
|
|
147
|
+
ENV['LLM_ENDPOINT'] = saved_llm_endpoint if saved_llm_endpoint
|
|
148
|
+
end
|
|
47
149
|
end
|
|
48
150
|
end
|
|
@@ -0,0 +1,299 @@
|
|
|
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
|
+
require 'scout/llm/request_context'
|
|
4
|
+
require 'scout/llm/tools/call'
|
|
5
|
+
require 'scout/llm/tools/workflow'
|
|
6
|
+
require 'scout/llm/ask'
|
|
7
|
+
require 'open3'
|
|
8
|
+
|
|
9
|
+
class TestLLMRequestContext < Test::Unit::TestCase
|
|
10
|
+
def context_workflow
|
|
11
|
+
@context_workflow ||= Module.new do
|
|
12
|
+
extend Workflow
|
|
13
|
+
self.name = 'RequestContextTestWorkflow'
|
|
14
|
+
input :value, :string
|
|
15
|
+
task :echo => :string do |value|
|
|
16
|
+
value
|
|
17
|
+
end
|
|
18
|
+
task :read_context => :string do
|
|
19
|
+
request_context.to_json
|
|
20
|
+
end
|
|
21
|
+
dep :read_context
|
|
22
|
+
task :parent => :string do
|
|
23
|
+
step(:read_context).load
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_projection_is_serializable_and_excludes_unsafe_fields
|
|
29
|
+
agent = Object.new
|
|
30
|
+
context = LLM::RequestContext.project(
|
|
31
|
+
endpoint: 'local', backend: :openai, model: 'gpt-test',
|
|
32
|
+
agent: agent, client: agent, tools: [proc {}],
|
|
33
|
+
messages: [{role: 'user'}], previous_response_id: 'secret',
|
|
34
|
+
nested: {keep: true, api_key: 'secret', 'X-Api-Key' => 'secret', finite: 1.5,
|
|
35
|
+
nan: Float::NAN, infinity: Float::INFINITY},
|
|
36
|
+
invalid_encoding: "bad\xFF".force_encoding(Encoding::UTF_8)
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
assert_equal 'local', context[:endpoint]
|
|
40
|
+
assert_equal 'openai', context[:backend]
|
|
41
|
+
assert_equal 'gpt-test', context[:model]
|
|
42
|
+
assert_equal({keep: true, finite: 1.5}, context[:nested])
|
|
43
|
+
assert(!context.key?(:agent))
|
|
44
|
+
assert(!context.key?(:client))
|
|
45
|
+
assert(!context.key?(:tools))
|
|
46
|
+
assert(!context.key?(:messages))
|
|
47
|
+
assert(!context.key?(:previous_response_id))
|
|
48
|
+
assert(JSON.parse(JSON.generate(context)))
|
|
49
|
+
assert_equal 1.5, context[:nested][:finite]
|
|
50
|
+
assert(!context[:nested].key?(:nan))
|
|
51
|
+
assert(!context[:nested].key?(:infinity))
|
|
52
|
+
assert_equal "bad\uFFFD", context[:invalid_encoding]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_projection_handles_malformed_hash_keys
|
|
56
|
+
malformed = "bad\xFFkey".force_encoding(Encoding::UTF_8)
|
|
57
|
+
context = LLM::RequestContext.project(malformed => 'value')
|
|
58
|
+
|
|
59
|
+
assert_equal 'value', context["bad\uFFFDkey".to_sym]
|
|
60
|
+
assert JSON.parse(JSON.generate(context))
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_tool_callback_preserves_two_argument_lambda_and_supports_three
|
|
64
|
+
call = {'id' => 'call-1', 'name' => 'echo', 'arguments' => {value: 'x'}}
|
|
65
|
+
two_seen = nil
|
|
66
|
+
LLM.tool_response(call, request_context: {request_id: 'R'}) do |name, arguments|
|
|
67
|
+
two_seen = [name, arguments]
|
|
68
|
+
'ok'
|
|
69
|
+
end
|
|
70
|
+
assert_equal ['echo', {value: 'x'}], two_seen
|
|
71
|
+
|
|
72
|
+
three_seen = nil
|
|
73
|
+
LLM.tool_response(call, request_context: {request_id: 'R'}) do |name, arguments, context|
|
|
74
|
+
three_seen = [name, arguments, context]
|
|
75
|
+
'ok'
|
|
76
|
+
end
|
|
77
|
+
assert_equal ['echo', {value: 'x'}, {request_id: 'R'}], three_seen
|
|
78
|
+
|
|
79
|
+
strict_seen = nil
|
|
80
|
+
strict = ->(name, arguments) { strict_seen = [name, arguments]; 'ok' }
|
|
81
|
+
LLM.tool_response(call, request_context: {request_id: 'R'}, &strict)
|
|
82
|
+
assert_equal ['echo', {value: 'x'}], strict_seen
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_strict_two_argument_tool_response_lambda
|
|
86
|
+
call = {'id' => 'call-strict', 'name' => 'echo', 'arguments' => {value: 'x'}}
|
|
87
|
+
strict = ->(name, arguments) { [name, arguments] }
|
|
88
|
+
response = LLM.tool_response(call, request_context: {request_id: 'R'}, &strict)
|
|
89
|
+
assert_equal ['echo', {value: 'x'}].to_json, response[:content]
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def test_process_calls_forwards_context_without_workflow_parameters
|
|
93
|
+
value = 'not-context'
|
|
94
|
+
context = {request_id: 'R1', endpoint: 'local', backend: 'openai', model: 'gpt-test'}
|
|
95
|
+
definition = LLM.task_tool_definition(context_workflow, :echo)
|
|
96
|
+
tools = {'echo' => [context_workflow, definition]}
|
|
97
|
+
calls = [{'id' => 'call-1', 'name' => 'echo', 'arguments' => {value: value}}]
|
|
98
|
+
|
|
99
|
+
result = LLM.process_calls(tools, calls, request_context: context)
|
|
100
|
+
assert_equal value, JSON.parse(result[1][:content])['content']
|
|
101
|
+
|
|
102
|
+
job = context_workflow.job(:echo, nil, value: value)
|
|
103
|
+
assert_equal value, job.run
|
|
104
|
+
assert_equal JSON.parse(JSON.generate(version: 1, fields: context)), job.info[:request_context]
|
|
105
|
+
assert_equal value, job.info[:provided_inputs]['value']
|
|
106
|
+
assert(!job.info[:provided_inputs].key?('request_context'))
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def test_task_body_reads_context_from_persisted_info_without_identity_change
|
|
110
|
+
context = {request_id: 'body-R', endpoint: 'test', backend: 'mock'}
|
|
111
|
+
first = context_workflow.job(:read_context, nil, value: 'body-context')
|
|
112
|
+
first.extend(LLM::RequestContext::StepExtension)
|
|
113
|
+
first.register_request_context(context)
|
|
114
|
+
first.clean
|
|
115
|
+
first.produce
|
|
116
|
+
loaded = Step.load(first.path)
|
|
117
|
+
expected_path = first.path.to_s
|
|
118
|
+
assert_equal expected_path, loaded.path.to_s
|
|
119
|
+
assert_equal first.inputs, context_workflow.job(:read_context, nil, value: 'body-context').inputs
|
|
120
|
+
assert_equal expected_path, context_workflow.job(:read_context, nil, value: 'body-context').path.to_s
|
|
121
|
+
assert_equal JSON.parse(JSON.generate(version: 1, fields: context)), loaded.info[:request_context]
|
|
122
|
+
assert(!loaded.info[:provided_inputs].keys.any? { |key| key.to_s == 'request_context' })
|
|
123
|
+
assert_equal context.transform_keys(&:to_s), JSON.parse(loaded.load)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def test_llm_ask_forwards_request_context_to_workflow_tool_step
|
|
127
|
+
context = {request_id: 'ask-R', endpoint: 'mock', backend: 'mock'}
|
|
128
|
+
tools = {'echo' => [context_workflow, LLM.task_tool_definition(context_workflow, :echo)]}
|
|
129
|
+
backend = Module.new
|
|
130
|
+
backend.define_singleton_method(:ask) do |_messages, options|
|
|
131
|
+
LLM.process_calls(options[:tools],
|
|
132
|
+
[{name: 'echo', arguments: {value: 'ask-context'}, id: 'ask-call'}],
|
|
133
|
+
request_context: options[:request_context])
|
|
134
|
+
'done'
|
|
135
|
+
end
|
|
136
|
+
LLM.register_backend(:request_context_test, backend)
|
|
137
|
+
|
|
138
|
+
result = LLM.ask('Return the result from the echo tool.', tools: tools,
|
|
139
|
+
request_context: context, persist: false, backend: :request_context_test)
|
|
140
|
+
|
|
141
|
+
assert_include result, 'done'
|
|
142
|
+
step = context_workflow.job(:echo, nil, value: 'ask-context')
|
|
143
|
+
assert_equal JSON.parse(JSON.generate(version: 1, fields: context)), step.info[:request_context]
|
|
144
|
+
assert_equal 'ask-context', step.info[:provided_inputs]['value']
|
|
145
|
+
assert(!step.info[:provided_inputs].key?('request_context'))
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def test_save_file_keyword_remains_compatible
|
|
149
|
+
calls = [{'id' => 'call-1', 'name' => 'echo', 'arguments' => {value: 'saved'}}]
|
|
150
|
+
tools = {'echo' => [context_workflow, LLM.task_tool_definition(context_workflow, :echo)]}
|
|
151
|
+
TmpFile.with_dir do |dir|
|
|
152
|
+
save_file = File.join(dir, 'chat')
|
|
153
|
+
result = LLM.process_calls(tools, calls, save_file: save_file,
|
|
154
|
+
request_context: {request_id: 'R2'})
|
|
155
|
+
assert_equal 'saved', JSON.parse(result[1][:content])['content']
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
def test_done_and_contextualized_dependencies_are_not_overwritten
|
|
159
|
+
context = {request_id: 'existing-R', endpoint: 'test'}
|
|
160
|
+
dependency = context_workflow.job(:read_context)
|
|
161
|
+
dependency.extend(LLM::RequestContext::StepExtension)
|
|
162
|
+
dependency.register_request_context(context)
|
|
163
|
+
dependency.clean
|
|
164
|
+
dependency.produce
|
|
165
|
+
before = File.binread(dependency.info_file)
|
|
166
|
+
|
|
167
|
+
parent = LLM.call_workflow(context_workflow, :parent, {},
|
|
168
|
+
request_context: {request_id: 'parent-R'})
|
|
169
|
+
parent.clean
|
|
170
|
+
parent.produce
|
|
171
|
+
|
|
172
|
+
assert_equal before, File.binread(dependency.info_file)
|
|
173
|
+
assert_equal context.transform_keys(&:to_s), JSON.parse(Step.load(dependency.path).load)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def test_dependency_task_body_reads_parent_context
|
|
177
|
+
context = {request_id: 'dependency-R', endpoint: 'test'}
|
|
178
|
+
parent = LLM.call_workflow(context_workflow, :parent, {}, request_context: context)
|
|
179
|
+
dependency_path = context_workflow.job(:read_context).path
|
|
180
|
+
parent.clean
|
|
181
|
+
assert(!File.exist?(dependency_path.to_s + '.info'))
|
|
182
|
+
parent.produce
|
|
183
|
+
|
|
184
|
+
dependency = Step.load(dependency_path)
|
|
185
|
+
assert_equal context.transform_keys(&:to_s), JSON.parse(dependency.load)
|
|
186
|
+
assert_equal JSON.parse(JSON.generate(version: 1, fields: context)), dependency.info[:request_context]
|
|
187
|
+
assert(!dependency.info[:provided_inputs].keys.any? { |key| key.to_s == 'request_context' })
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def test_context_does_not_change_workflow_job_identity
|
|
191
|
+
inputs = {value: 'same'}
|
|
192
|
+
first = LLM.call_workflow(context_workflow, :echo, inputs,
|
|
193
|
+
request_context: {request_id: 'one'})
|
|
194
|
+
second = LLM.call_workflow(context_workflow, :echo, inputs,
|
|
195
|
+
request_context: {request_id: 'two'})
|
|
196
|
+
assert_equal first.path.to_s, second.path.to_s
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def test_replay_in_a_cold_process_does_not_replace_producer_context
|
|
200
|
+
value = 'cold-replay'
|
|
201
|
+
definition = LLM.task_tool_definition(context_workflow, :echo)
|
|
202
|
+
tools = {'echo' => [context_workflow, definition]}
|
|
203
|
+
calls = [{'id' => 'call-1', 'name' => 'echo', 'arguments' => {value: value}}]
|
|
204
|
+
|
|
205
|
+
LLM.process_calls(tools, calls, request_context: {request_id: 'A'})
|
|
206
|
+
job = context_workflow.job(:echo, nil, value: value)
|
|
207
|
+
assert_equal 'A', job.info[:request_context][:fields][:request_id]
|
|
208
|
+
original_info = File.binread(job.info_file)
|
|
209
|
+
|
|
210
|
+
script = File.join(tmpdir.to_s, 'cold_replay.rb')
|
|
211
|
+
File.write(script, <<~RUBY)
|
|
212
|
+
$LOAD_PATH.unshift #{File.expand_path('lib').inspect}
|
|
213
|
+
require 'scout'
|
|
214
|
+
require 'scout/llm/tools/call'
|
|
215
|
+
require 'scout/llm/tools/workflow'
|
|
216
|
+
module RequestContextTestWorkflow
|
|
217
|
+
extend Workflow
|
|
218
|
+
input :value, :string
|
|
219
|
+
task :echo => :string do |value|
|
|
220
|
+
value
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
Workflow.directory = Path.setup(#{Workflow.directory.to_s.inspect})
|
|
224
|
+
wf = RequestContextTestWorkflow
|
|
225
|
+
definition = LLM.task_tool_definition(wf, :echo)
|
|
226
|
+
LLM.process_calls({'echo' => [wf, definition]},
|
|
227
|
+
[{'id' => 'cold', 'name' => 'echo', 'arguments' => {value: #{value.inspect}}}],
|
|
228
|
+
request_context: {request_id: 'B'})
|
|
229
|
+
RUBY
|
|
230
|
+
stdout, stderr, status = Open3.capture3(RbConfig.ruby, script)
|
|
231
|
+
assert status.success?, "cold replay failed: #{stdout}\n#{stderr}"
|
|
232
|
+
assert_equal original_info, File.binread(job.info_file)
|
|
233
|
+
assert_equal 'A', Step.load(job.path).info[:request_context][:fields][:request_id]
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def test_concurrent_producers_write_one_first_context_and_one_raw_key
|
|
237
|
+
value = 'concurrent-producer'
|
|
238
|
+
definition = LLM.task_tool_definition(context_workflow, :echo)
|
|
239
|
+
tools = {'echo' => [context_workflow, definition]}
|
|
240
|
+
calls = [
|
|
241
|
+
{'id' => 'call-1', 'name' => 'echo', 'arguments' => {value: value}},
|
|
242
|
+
{'id' => 'call-2', 'name' => 'echo', 'arguments' => {value: value}},
|
|
243
|
+
{'id' => 'call-3', 'name' => 'echo', 'arguments' => {value: value}},
|
|
244
|
+
{'id' => 'call-4', 'name' => 'echo', 'arguments' => {value: value}},
|
|
245
|
+
{'id' => 'call-5', 'name' => 'echo', 'arguments' => {value: value}}
|
|
246
|
+
]
|
|
247
|
+
errors = Queue.new
|
|
248
|
+
|
|
249
|
+
threads = (0...4).map do |i|
|
|
250
|
+
Thread.new do
|
|
251
|
+
begin
|
|
252
|
+
sleep rand
|
|
253
|
+
LLM.process_calls(tools, [calls[i]], request_context: {request_id: "producer-#{i}"})
|
|
254
|
+
rescue Exception => e
|
|
255
|
+
errors << e
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
threads.each(&:join)
|
|
260
|
+
assert errors.empty?, errors.size.times.map { errors.pop.message }.join("\n")
|
|
261
|
+
|
|
262
|
+
job = context_workflow.job(:echo, nil, value: value)
|
|
263
|
+
raw_text = File.binread(job.info_file)
|
|
264
|
+
raw = JSON.parse(raw_text)
|
|
265
|
+
request_context_keys = raw.keys.select { |key| key == 'request_context' }
|
|
266
|
+
assert_equal ['request_context'], request_context_keys
|
|
267
|
+
assert_kind_of Hash, raw['request_context']
|
|
268
|
+
assert_equal 1, raw['request_context']['version']
|
|
269
|
+
assert_match(/producer-\d+/, raw['request_context']['fields']['request_id'])
|
|
270
|
+
assert_equal 1, raw_text.scan(/"request_context"\s*:/).length
|
|
271
|
+
assert_equal 1, raw.keys.count { |key| key == 'request_context' }
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def test_request_context_exec
|
|
275
|
+
context = {request_id: 'body-R', endpoint: 'test', backend: 'mock'}
|
|
276
|
+
|
|
277
|
+
job = self.context_workflow.job(:read_context)
|
|
278
|
+
job.extend(LLM::RequestContext::StepExtension)
|
|
279
|
+
job.register_request_context(context)
|
|
280
|
+
assert_equal context.to_json, job.run
|
|
281
|
+
assert job.info_file.exists?
|
|
282
|
+
refute Open.exist?(job.info_file + '.request_context')
|
|
283
|
+
refute Open.exist?(job.info_file + '.request_context.lock')
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
job = self.context_workflow.job(:read_context, 'exec')
|
|
287
|
+
job.extend(LLM::RequestContext::StepExtension)
|
|
288
|
+
assert_equal 'null', job.exec
|
|
289
|
+
refute job.info_file.exists?
|
|
290
|
+
refute Open.exist?(job.info_file + '.request_context')
|
|
291
|
+
refute Open.exist?(job.info_file + '.request_context.lock')
|
|
292
|
+
|
|
293
|
+
job.register_request_context(context)
|
|
294
|
+
assert_equal context.to_json, job.exec
|
|
295
|
+
refute job.info_file.exists?
|
|
296
|
+
refute Open.exist?(job.info_file + '.request_context')
|
|
297
|
+
refute Open.exist?(job.info_file + '.request_context.lock')
|
|
298
|
+
end
|
|
299
|
+
end
|
|
@@ -0,0 +1,249 @@
|
|
|
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 'fileutils'
|
|
5
|
+
require 'tmpdir'
|
|
6
|
+
# sandbox is loaded through scout/llm/chat/process/tools in the public API path.
|
|
7
|
+
require 'scout/llm/chat'
|
|
8
|
+
|
|
9
|
+
class TestSandbox < Test::Unit::TestCase
|
|
10
|
+
def setup
|
|
11
|
+
super
|
|
12
|
+
@root = Dir.mktmpdir('scout-ai-sandbox-')
|
|
13
|
+
%w[root/nested read_grant write_grant outside].each do |directory|
|
|
14
|
+
FileUtils.mkdir_p(File.join(@root, directory))
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def teardown
|
|
19
|
+
FileUtils.rm_rf(@root) if @root
|
|
20
|
+
super
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_grant_registration_and_chat_compatibility
|
|
24
|
+
Thread.current['allowed_paths'] = nil
|
|
25
|
+
Thread.current['allowed_read_paths'] = nil
|
|
26
|
+
|
|
27
|
+
Chat.allow_path('raw/path')
|
|
28
|
+
Chat.allow_path('raw/path')
|
|
29
|
+
Chat.allow_read_path('read/raw')
|
|
30
|
+
Chat.allow_read_path('read/raw')
|
|
31
|
+
|
|
32
|
+
assert_equal ['raw/path'], Thread.current['allowed_paths']
|
|
33
|
+
assert_equal ['read/raw'], Thread.current['allowed_read_paths']
|
|
34
|
+
|
|
35
|
+
job = Struct.new(:path, :info_file, :files_dir).new('job/path', 'job/info', 'job/files')
|
|
36
|
+
Chat.allow_job(job)
|
|
37
|
+
Chat.allow_read_job(job)
|
|
38
|
+
assert_equal ['raw/path', 'job/path', 'job/info', 'job/files'], Thread.current['allowed_paths']
|
|
39
|
+
assert_equal ['read/raw', 'job/path', 'job/info', 'job/files'], Thread.current['allowed_read_paths']
|
|
40
|
+
ensure
|
|
41
|
+
Thread.current['allowed_paths'] = nil
|
|
42
|
+
Thread.current['allowed_read_paths'] = nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_authorization_uses_root_and_nested_boundary_not_sibling_prefix
|
|
46
|
+
root = File.join(@root, 'root')
|
|
47
|
+
assert_allowed File.join(root, 'nested'), root: root
|
|
48
|
+
assert_allowed root, root: root
|
|
49
|
+
decision = LLM::Sandbox.authorize_path(File.join(@root, 'root-sibling'), root: root)
|
|
50
|
+
assert_false decision.allowed
|
|
51
|
+
assert_equal :outside_grants, decision.reason
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_relative_paths_require_an_explicit_base
|
|
55
|
+
assert_raise(ParameterException) do
|
|
56
|
+
LLM::Sandbox.authorize_path('root/nested', root: File.join(@root, 'root'), mode: :write)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
decision = LLM::Sandbox.authorize_path('nested', root: File.join(@root, 'root'),
|
|
60
|
+
base_path: File.join(@root, 'root'), mode: :write)
|
|
61
|
+
assert_true decision.allowed
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def test_read_and_write_grants_have_distinct_semantics
|
|
65
|
+
target = File.join(@root, 'read_grant', 'document.txt')
|
|
66
|
+
File.write(target, 'read me')
|
|
67
|
+
read_decision = LLM::Sandbox.authorize_path(target, root: File.join(@root, 'root'),
|
|
68
|
+
read_paths: [File.join(@root, 'read_grant')], mode: :read)
|
|
69
|
+
write_decision = LLM::Sandbox.authorize_path(target, root: File.join(@root, 'root'),
|
|
70
|
+
read_paths: [File.join(@root, 'read_grant')], mode: :write)
|
|
71
|
+
assert_true read_decision.allowed
|
|
72
|
+
assert_equal :read, read_decision.grant
|
|
73
|
+
assert_false write_decision.allowed
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def test_existing_symlink_cannot_escape_a_grant
|
|
77
|
+
grant = File.join(@root, 'write_grant')
|
|
78
|
+
link = File.join(grant, 'escape')
|
|
79
|
+
File.symlink(File.join(@root, 'outside'), link)
|
|
80
|
+
|
|
81
|
+
decision = LLM::Sandbox.authorize_path(File.join(link, 'secret.txt'), root: File.join(@root, 'root'),
|
|
82
|
+
writable_paths: [grant], mode: :write)
|
|
83
|
+
assert_false decision.allowed
|
|
84
|
+
assert_equal File.join(@root, 'outside', 'secret.txt'), decision.canonical_path
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def test_missing_target_under_symlinked_parent_is_resolved_before_authorization
|
|
88
|
+
grant = File.join(@root, 'write_grant')
|
|
89
|
+
link = File.join(grant, 'to_outside')
|
|
90
|
+
File.symlink(File.join(@root, 'outside'), link)
|
|
91
|
+
target = File.join(link, 'new.txt')
|
|
92
|
+
|
|
93
|
+
decision = LLM::Sandbox.authorize_path(target, root: File.join(@root, 'root'),
|
|
94
|
+
writable_paths: [grant], mode: :write)
|
|
95
|
+
assert_false decision.allowed
|
|
96
|
+
assert_false decision.exists
|
|
97
|
+
assert_equal File.join(@root, 'outside', 'new.txt'), decision.canonical_path
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def test_missing_descendant_within_grant_is_allowed_and_marked_missing
|
|
101
|
+
root = File.join(@root, 'root')
|
|
102
|
+
target = File.join(root, 'nested', 'not-yet-created')
|
|
103
|
+
decision = LLM::Sandbox.authorize_path(target, root: root, mode: :write)
|
|
104
|
+
assert_true decision.allowed
|
|
105
|
+
assert_false decision.exists
|
|
106
|
+
assert_equal :root, decision.grant
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def test_parent_component_requires_existing_preceding_directory
|
|
110
|
+
root = File.join(@root, 'root')
|
|
111
|
+
file = File.join(root, 'regular-file')
|
|
112
|
+
File.write(file, 'not a directory')
|
|
113
|
+
|
|
114
|
+
invalid = LLM::Sandbox.authorize_path(File.join(file, '..', 'child'), root: root, mode: :write)
|
|
115
|
+
assert_false invalid.allowed
|
|
116
|
+
assert_nil invalid.canonical_path
|
|
117
|
+
assert_equal :unresolvable, invalid.reason
|
|
118
|
+
|
|
119
|
+
valid = LLM::Sandbox.authorize_path(File.join(root, 'nested', '..', 'child'), root: root, mode: :write)
|
|
120
|
+
assert_true valid.allowed
|
|
121
|
+
assert_equal File.join(root, 'child'), valid.canonical_path
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def test_parent_component_checks_preceding_directory_without_rechecking_parent
|
|
125
|
+
root = File.join(@root, 'root')
|
|
126
|
+
nested = File.join(root, 'nested')
|
|
127
|
+
directory_checks = []
|
|
128
|
+
original_directory_check = Open.method(:directory?)
|
|
129
|
+
Open.define_singleton_method(:directory?) do |path|
|
|
130
|
+
directory_checks << path
|
|
131
|
+
original_directory_check.call(path)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
result = LLM::Sandbox.resolve_path(File.join(nested, '..', 'child'))
|
|
135
|
+
|
|
136
|
+
assert_equal File.join(root, 'child'), result[:canonical_path]
|
|
137
|
+
assert_equal [nested], directory_checks
|
|
138
|
+
ensure
|
|
139
|
+
Open.define_singleton_method(:directory?, original_directory_check) if original_directory_check
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def test_missing_component_before_parent_component_is_not_collapsed
|
|
143
|
+
root = File.join(@root, 'root')
|
|
144
|
+
result = LLM::Sandbox.resolve_path(File.join(root, 'missing', '..', 'child'))
|
|
145
|
+
|
|
146
|
+
assert_nil result[:canonical_path]
|
|
147
|
+
assert_false result[:exists]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def test_unresolvable_dangling_symlink_is_denied
|
|
151
|
+
grant = File.join(@root, 'write_grant')
|
|
152
|
+
dangling = File.join(grant, 'dangling')
|
|
153
|
+
File.symlink(File.join(@root, 'missing-target'), dangling)
|
|
154
|
+
decision = LLM::Sandbox.authorize_path(dangling, root: File.join(@root, 'root'),
|
|
155
|
+
writable_paths: [grant], mode: :write)
|
|
156
|
+
assert_false decision.allowed
|
|
157
|
+
assert_equal :unresolvable, decision.reason
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# The grant-source layer composes the same lists the Workspace workflow
|
|
161
|
+
# composes today. Pinned with raw thread/config manipulation so the
|
|
162
|
+
# assertion holds regardless of the ambient environment.
|
|
163
|
+
def test_grant_sources_compose_defaults_thread_and_config
|
|
164
|
+
saved_allowed = Thread.current['allowed_paths']
|
|
165
|
+
saved_read = Thread.current['allowed_read_paths']
|
|
166
|
+
Thread.current['allowed_paths'] = ['/thread/grant']
|
|
167
|
+
Thread.current['allowed_read_paths'] = ['/thread/read']
|
|
168
|
+
|
|
169
|
+
writable = LLM::Sandbox.writable_grants(scope: 'Workspace')
|
|
170
|
+
readable = LLM::Sandbox.readable_grants(scope: 'Workspace')
|
|
171
|
+
|
|
172
|
+
assert_include writable, '/thread/grant'
|
|
173
|
+
assert_include writable, '/tmp'
|
|
174
|
+
assert_include readable, '/thread/read'
|
|
175
|
+
assert_include readable, '/bin'
|
|
176
|
+
assert_not_include writable, '/thread/read'
|
|
177
|
+
assert_not_include readable, '/thread/grant'
|
|
178
|
+
|
|
179
|
+
# Defaults resolve unlocated Scout spellings through the path maps. The
|
|
180
|
+
# :home expansion is computed live as the oracle: when an earlier test
|
|
181
|
+
# file in the same process has required 'rbbt' (test_chat.rb,
|
|
182
|
+
# test_tools.rb), Scout's PKGDIR switches and the expansion spelling
|
|
183
|
+
# changes with it. The grant composition follows the live maps either
|
|
184
|
+
# way, which is the property under test.
|
|
185
|
+
assert_include writable.map(&:to_s), Path.setup('tmp').find(:home).to_s
|
|
186
|
+
ensure
|
|
187
|
+
Thread.current['allowed_paths'] = saved_allowed
|
|
188
|
+
Thread.current['allowed_read_paths'] = saved_read
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def test_grant_scope_tokens_and_expand
|
|
192
|
+
assert_equal ['Workspace', 'workspace', 'sandbox'], LLM::Sandbox.grant_scope_tokens('Workspace')
|
|
193
|
+
assert_equal ['sandbox'], LLM::Sandbox.grant_scope_tokens(nil)
|
|
194
|
+
|
|
195
|
+
located = LLM::Sandbox.expand_grant_path('/tmp')
|
|
196
|
+
assert_true Path === located || String === located
|
|
197
|
+
assert_equal '/tmp', located.to_s
|
|
198
|
+
|
|
199
|
+
expanded = LLM::Sandbox.expand_grant_path('/tmp')
|
|
200
|
+
assert_kind_of Array, LLM::Sandbox.expand_grant_path('etc')
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def test_grant_paths_reads_config_and_env_overrides
|
|
204
|
+
Scout::Config.with_config do
|
|
205
|
+
Scout::Config.set({'dirs' => '/plain/dirs'})
|
|
206
|
+
Scout::Config.set({'dirs' => '/workspace/dirs'}, 'workspace')
|
|
207
|
+
assert_equal ['/workspace/dirs'], LLM::Sandbox.grant_paths('dirs', scope: 'Workspace')
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
Scout::Config.with_config do
|
|
211
|
+
ENV['DIRS'] = '/env/dirs'
|
|
212
|
+
assert_equal ['/env/dirs'], LLM::Sandbox.grant_paths('dirs', scope: 'Workspace')
|
|
213
|
+
end
|
|
214
|
+
ensure
|
|
215
|
+
ENV.delete('DIRS')
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def test_grant_origin_reports_the_first_source_listing_a_path
|
|
219
|
+
saved = Thread.current['allowed_paths']
|
|
220
|
+
Thread.current['allowed_paths'] = nil
|
|
221
|
+
LLM::Sandbox.allow_path('/tmp/grant-origin-job')
|
|
222
|
+
|
|
223
|
+
assert_equal :thread, LLM::Sandbox.grant_origin('/tmp/grant-origin-job', scope: 'Workspace')
|
|
224
|
+
assert_equal :exec_paths, LLM::Sandbox.grant_origin('/bin', scope: 'Workspace')
|
|
225
|
+
assert_nil LLM::Sandbox.grant_origin('/definitely/not/a/grant', scope: 'Workspace')
|
|
226
|
+
ensure
|
|
227
|
+
Thread.current['allowed_paths'] = saved
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def test_describe_grants_annotates_and_includes_hint
|
|
231
|
+
text = LLM::Sandbox.describe_grants(scope: 'Workspace', root: '/some/root',
|
|
232
|
+
hint: "Run task 'sandbox_paths' for details")
|
|
233
|
+
assert_include text, '/some/root (read-write)'
|
|
234
|
+
assert_include text, '/tmp (read-write)'
|
|
235
|
+
assert_include text, '/bin (read-only)'
|
|
236
|
+
assert_include text, "Run task 'sandbox_paths' for details"
|
|
237
|
+
# read-only lines come after writable ones, and root heads the list
|
|
238
|
+
assert_true text.index('/bin (read-only)') > text.index('/tmp (read-write)')
|
|
239
|
+
assert_equal 0, text.index('/some/root (read-write)')
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
private
|
|
243
|
+
|
|
244
|
+
def assert_allowed(path, root:)
|
|
245
|
+
decision = LLM::Sandbox.authorize_path(path, root: root, mode: :write)
|
|
246
|
+
assert_true decision.allowed
|
|
247
|
+
assert_equal :root, decision.grant
|
|
248
|
+
end
|
|
249
|
+
end
|