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.
Files changed (185) hide show
  1. checksums.yaml +4 -4
  2. data/.vimproject +135 -55
  3. data/README.md +2 -1
  4. data/VERSION +1 -1
  5. data/doc/Improvements.md +490 -148
  6. data/doc/Model.md +53 -10
  7. data/doc/StartHere.md +44 -19
  8. data/doc/developer/Architecture.md +25 -6
  9. data/doc/developer/Backends.md +207 -48
  10. data/doc/developer/ChatLifecycle.md +121 -20
  11. data/doc/developer/DelegationInternals.md +320 -34
  12. data/doc/developer/DesignPrinciples.md +205 -16
  13. data/doc/developer/PathEngine.md +628 -0
  14. data/doc/developer/PromptProcessing.md +332 -82
  15. data/doc/developer/Provenance.md +402 -44
  16. data/doc/developer/Sandbox.md +213 -0
  17. data/doc/user/BuildingAgents.md +130 -18
  18. data/doc/user/Cookbook.md +29 -8
  19. data/doc/user/CoreConcepts.md +7 -6
  20. data/doc/user/Delegation.md +166 -15
  21. data/doc/user/GettingStarted.md +20 -16
  22. data/doc/user/ManagingContext.md +155 -17
  23. data/doc/user/MultiAgentWorkflows.md +105 -15
  24. data/doc/user/Python.md +11 -1
  25. data/doc/user/RunningInference.md +90 -47
  26. data/doc/user/ToolCalling.md +134 -29
  27. data/doc/user/WritingChats.md +56 -27
  28. data/lib/scout/llm/agent/attach.rb +64 -0
  29. data/lib/scout/llm/agent/chat.rb +32 -0
  30. data/lib/scout/llm/agent/construction.rb +64 -0
  31. data/lib/scout/llm/agent/conversation.rb +304 -0
  32. data/lib/scout/llm/agent/delegate.rb +56 -238
  33. data/lib/scout/llm/agent/path/edit.rb +230 -0
  34. data/lib/scout/llm/agent/path/patch.rb +252 -0
  35. data/lib/scout/llm/agent/path/workflow_kinds.rb +175 -0
  36. data/lib/scout/llm/agent/path.rb +1777 -0
  37. data/lib/scout/llm/agent/save.rb +11 -9
  38. data/lib/scout/llm/agent/workflow.rb +79 -17
  39. data/lib/scout/llm/agent.rb +151 -27
  40. data/lib/scout/llm/ask.rb +37 -27
  41. data/lib/scout/llm/backends/anthropic.rb +1 -1
  42. data/lib/scout/llm/backends/bedrock.rb +3 -2
  43. data/lib/scout/llm/backends/default.rb +131 -32
  44. data/lib/scout/llm/backends/glm.rb +28 -1
  45. data/lib/scout/llm/backends/huggingface.rb +1 -1
  46. data/lib/scout/llm/backends/ollama.rb +1 -1
  47. data/lib/scout/llm/backends/openai.rb +7 -6
  48. data/lib/scout/llm/backends/openwebui.rb +14 -7
  49. data/lib/scout/llm/backends/relay.rb +8 -2
  50. data/lib/scout/llm/chat/agent_meta.rb +24 -60
  51. data/lib/scout/llm/chat/parse.rb +1 -1
  52. data/lib/scout/llm/chat/process/files.rb +10 -0
  53. data/lib/scout/llm/chat/process/meta.rb +5 -7
  54. data/lib/scout/llm/chat/process/tools.rb +104 -52
  55. data/lib/scout/llm/chat/prompt/epoch_tool_compactation.rb +355 -0
  56. data/lib/scout/llm/chat/prompt/inbox.rb +188 -0
  57. data/lib/scout/llm/chat/prompt/shorten_tools.rb +797 -13
  58. data/lib/scout/llm/chat/prompt/shorten_tools_epoch.rb +1 -365
  59. data/lib/scout/llm/chat/prompt/shorten_tools_epoch_increment.rb +1 -0
  60. data/lib/scout/llm/chat/prompt.rb +24 -10
  61. data/lib/scout/llm/chat/provenance/live.rb +1080 -0
  62. data/lib/scout/llm/chat/provenance/tokens.rb +386 -0
  63. data/lib/scout/llm/chat/provenance.rb +352 -626
  64. data/lib/scout/llm/chat.rb +8 -2
  65. data/lib/scout/llm/embed.rb +15 -3
  66. data/lib/scout/llm/image.rb +4 -1
  67. data/lib/scout/llm/mcp.rb +4 -2
  68. data/lib/scout/llm/request_context.rb +234 -0
  69. data/lib/scout/llm/sandbox.rb +263 -0
  70. data/lib/scout/llm/tools/call.rb +163 -42
  71. data/lib/scout/llm/tools/definition.rb +48 -0
  72. data/lib/scout/llm/tools/knowledge_base.rb +1 -0
  73. data/lib/scout/llm/tools/mcp.rb +50 -43
  74. data/lib/scout/llm/tools/workflow.rb +90 -20
  75. data/lib/scout/llm/tools.rb +14 -41
  76. data/lib/scout/model/decision/base.rb +43 -0
  77. data/lib/scout/model/decision/jev.rb +161 -0
  78. data/lib/scout/model/python/torch.rb +1 -1
  79. data/research/clear-tools-inherit.md +171 -0
  80. data/research/path-engine-dc1-dc2-dc3-fixes.md +279 -0
  81. data/research/path-kind-chat-role.md +96 -0
  82. data/research/path-kind-contract-fixes-engine.md +307 -0
  83. data/research/scout-ai-runtime-availability.md +65 -0
  84. data/research/subsys/README.md +127 -0
  85. data/research/subsys/agent-delegation.md +201 -0
  86. data/research/subsys/agent-society.md +275 -0
  87. data/research/subsys/agentworkflow.md +233 -0
  88. data/research/subsys/backends.md +221 -0
  89. data/research/subsys/chat.md +314 -0
  90. data/research/subsys/embed-image.md +244 -0
  91. data/research/subsys/entry-cli.md +253 -0
  92. data/research/subsys/model-ml.md +326 -0
  93. data/research/subsys/provenance.md +290 -0
  94. data/research/subsys/tools.md +334 -0
  95. data/scout-ai.gemspec +72 -26
  96. data/scout_commands/agent/ask +8 -7
  97. data/scout_commands/llm/abridged +40 -0
  98. data/scout_commands/llm/ask +9 -9
  99. data/scout_commands/llm/md +2 -0
  100. data/scout_commands/llm/prov +845 -80
  101. data/scout_commands/llm/word +4 -0
  102. data/share/word/reference.docx +0 -0
  103. data/test/integration/scout/model/decision/test_jev.rb +144 -0
  104. data/test/scout/llm/agent/test_attach.rb +117 -0
  105. data/test/scout/llm/agent/test_conversation.rb +407 -0
  106. data/test/scout/llm/agent/test_delegate.rb +209 -0
  107. data/test/scout/llm/agent/test_path.rb +1812 -0
  108. data/test/scout/llm/agent/test_path_edit.rb +908 -0
  109. data/test/scout/llm/agent/test_path_errors.rb +390 -0
  110. data/test/scout/llm/agent/test_path_filesystem.rb +532 -0
  111. data/test/scout/llm/agent/test_path_patch.rb +233 -0
  112. data/test/scout/llm/agent/test_path_promotion.rb +386 -0
  113. data/test/scout/llm/agent/test_path_workflow_integration.rb +665 -0
  114. data/test/scout/llm/agent/test_save.rb +70 -15
  115. data/test/scout/llm/agent/test_workflow.rb +130 -4
  116. data/test/scout/llm/backends/test_anthropic.rb +4 -4
  117. data/test/scout/llm/backends/test_bedrock.rb +4 -4
  118. data/test/scout/llm/backends/test_format_other.rb +43 -0
  119. data/test/scout/llm/backends/test_ollama.rb +2 -2
  120. data/test/scout/llm/backends/test_openwebui.rb +12 -8
  121. data/test/scout/llm/backends/test_reasoning.rb +114 -0
  122. data/test/scout/llm/chat/agent_meta_fixtures.rb +109 -22
  123. data/test/scout/llm/chat/prompt/test_epoch_tool_compactation.rb +612 -0
  124. data/test/scout/llm/chat/prov_plot_fixtures.rb +131 -0
  125. data/test/scout/llm/chat/test_agent_meta.rb +40 -80
  126. data/test/scout/llm/chat/test_agent_meta_provenance.rb +200 -28
  127. data/test/scout/llm/chat/test_agent_meta_tokens.rb +112 -7
  128. data/test/scout/llm/chat/test_agent_view.rb +178 -0
  129. data/test/scout/llm/chat/test_live_report.rb +282 -0
  130. data/test/scout/llm/chat/test_live_workload.rb +193 -0
  131. data/test/scout/llm/chat/test_parse.rb +32 -2
  132. data/test/scout/llm/chat/test_prompt.rb +411 -0
  133. data/test/scout/llm/chat/test_prov_cli.rb +708 -13
  134. data/test/scout/llm/chat/test_prov_cli_live.rb +152 -0
  135. data/test/scout/llm/chat/test_prov_plot.rb +213 -0
  136. data/test/scout/llm/chat/test_prov_timeline.rb +355 -0
  137. data/test/scout/llm/chat/test_provenance.rb +67 -22
  138. data/test/scout/llm/chat/test_provenance_index.rb +479 -0
  139. data/test/scout/llm/chat/test_society_view.rb +159 -0
  140. data/test/scout/llm/test_agent.rb +1 -1
  141. data/test/scout/llm/test_embed.rb +103 -1
  142. data/test/scout/llm/test_request_context.rb +299 -0
  143. data/test/scout/llm/test_sandbox.rb +249 -0
  144. data/test/scout/llm/tools/test_agent_pairing.rb +150 -0
  145. data/test/scout/llm/tools/test_call_delegation_conflict.rb +316 -0
  146. data/test/scout/llm/tools/test_definition.rb +332 -0
  147. data/test/scout/llm/tools/test_jobs_file.rb +211 -0
  148. data/test/scout/llm/tools/test_mcp.rb +37 -8
  149. data/test/scout/llm/tools/test_workflow.rb +45 -1
  150. data/test/scout/model/decision/test_jev.rb +453 -0
  151. data/test/scout/model/python/huggingface/causal/test_next_token.rb +7 -10
  152. data/test/scout/model/python/huggingface/test_causal.rb +11 -2
  153. data/test/scout/model/python/huggingface/test_classification.rb +5 -3
  154. data/test/scout/model/python/test_availability.rb +116 -0
  155. data/test/scout/model/python/test_torch.rb +54 -2
  156. data/test/support/availability.rb +72 -174
  157. data/test/support/fake_clients.rb +18 -9
  158. data/test/support/fixtures.rb +75 -6
  159. data/test/support/mock_backend.rb +9 -1
  160. data/test/support/test_fixtures.rb +156 -0
  161. data/test/test_helper.rb +14 -4
  162. metadata +71 -25
  163. data/research/ChatAnalyst-required-changes.md +0 -167
  164. data/research/agent-delegation-analysis.md +0 -810
  165. data/research/agent-meta-provenance-integration-plan.md +0 -622
  166. data/research/agent-workflow-analysis.md +0 -1120
  167. data/research/backends-analysis.md +0 -836
  168. data/research/chat-core-analysis.md +0 -946
  169. data/research/chatanalyst-provenance/00-baseline.md +0 -30
  170. data/research/chatanalyst-provenance/01-repo-map.md +0 -60
  171. data/research/chatanalyst-provenance/02-event-reconstruction.md +0 -55
  172. data/research/chatanalyst-provenance/03-duplication-evidence.md +0 -45
  173. data/research/chatanalyst-provenance/04-tooling-root-cause.md +0 -57
  174. data/research/chatanalyst-provenance/05-fix-plan.md +0 -46
  175. data/research/chatanalyst-provenance/07-critic-review.md +0 -25
  176. data/research/chatanalyst-provenance/final-report.md +0 -45
  177. data/research/chatanalyst-provenance/resumption.md +0 -37
  178. data/research/coding-philosophy-analysis.md +0 -928
  179. data/research/commands-analysis.md +0 -947
  180. data/research/multi-agent-patterns-analysis.md +0 -853
  181. data/research/prompt-strategies-analysis.md +0 -630
  182. data/research/prov-verbosity-fix-notes.md +0 -77
  183. data/research/provenance-analysis.md +0 -469
  184. data/research/synthesis-report.md +0 -487
  185. data/research/tools-system-analysis.md +0 -779
@@ -0,0 +1,152 @@
1
+ # Tests for the --live section of `scout-ai llm prov`: the unified live
2
+ # report (Chat.live_report) rendered as an appended section, captured work
3
+ # excluded, silent when nothing is live.
4
+ #
5
+ # Live job fixtures are synthetic on purpose: a running job is a directory
6
+ # plus a .info JSON whose pid is the test process itself (Chat.live_pid?
7
+ # accepts the current pid), exactly like test_live_workload.rb.
8
+
9
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
10
+ require 'scout/llm/chat'
11
+ require 'open3'
12
+ require 'rbconfig'
13
+ require 'timeout'
14
+ require_relative 'agent_meta_fixtures'
15
+
16
+ class TestProvCLILive < Test::Unit::TestCase
17
+ include AgentMetaFixtures
18
+
19
+ REPO_ROOT = File.expand_path('../../../../../', __FILE__)
20
+
21
+ # Run the real binary. By default the cwd is the fixture dir, because
22
+ # the live passes resolve short job references (WF/continue/one) against
23
+ # Workflow.directory, whose default 'var/jobs' is PWD-relative: fixtures
24
+ # write their jobs under <dir>/var/jobs and the subprocess must run from
25
+ # <dir> for resolution to match live_workload's in-process tests.
26
+ def prov(*args)
27
+ cwd = File.directory?(args.first.to_s) ? args.shift : REPO_ROOT
28
+ cmd = [RbConfig.ruby, File.join(REPO_ROOT, 'bin', 'scout-ai'), 'llm', 'prov'] + args.collect(&:to_s)
29
+ env = ENV.to_h.merge('SCOUT_DEV' => nil)
30
+ Timeout.timeout(120) do
31
+ out, err, status = Open3.capture3(env, *cmd, chdir: cwd)
32
+ [strip_ansi(out), strip_ansi(err), status]
33
+ end
34
+ end
35
+
36
+ def strip_ansi(text)
37
+ text.gsub(/\e\[[0-9;]*m/, '')
38
+ end
39
+
40
+ # One running job directory: `<wfdir>/<ref>` with a .info sidecar whose
41
+ # pid is this test process, so Chat.classify_live_job reports :running.
42
+ # Returns the full job path.
43
+ def running_job(wfdir, ref, type: 'string', dependencies: [])
44
+ path = File.join(wfdir, ref)
45
+ FileUtils.mkdir_p(path)
46
+ File.write(path + '.info',
47
+ {status: 'running', pid: Process.pid, type: type,
48
+ workflow: ref.split('/').first, task_name: ref.split('/')[1],
49
+ dependencies: dependencies}.to_json)
50
+ path
51
+ end
52
+
53
+ # A running chat task with its agent save file inside the job's .files
54
+ # dir, as AgentWorkflow would keep it.
55
+ def running_chat_task(wfdir, ref)
56
+ path = running_job(wfdir, ref, type: 'chat')
57
+ files_dir = path + '.files'
58
+ FileUtils.mkdir_p(files_dir)
59
+ File.write(File.join(files_dir, 'agent.chat'),
60
+ "\nuser:\n\ndo the thing\n")
61
+ path
62
+ end
63
+
64
+ def test_live_section_appends_running_inference
65
+ TmpFile.with_dir do |dir|
66
+ # A quiet parent chat plus a live delegation chain: the sidecar lists
67
+ # the wrapper job; the wrapper's dependency is the chat task.
68
+ chat = write_chat(dir, 'parent.chat',
69
+ "user: go\nmeta: pt=1 ct=1 tt=2 inference_id=q1\nassistant: done\n")
70
+ wfdir = File.join(dir, 'var', 'jobs')
71
+ task = running_chat_task(wfdir, 'WF/continue/one')
72
+ wrapper = running_job(wfdir, 'WF/cortex_continue/one',
73
+ dependencies: [task])
74
+ File.write(Chat.jobs_file(chat), "WF/cortex_continue/one\n")
75
+
76
+ out, err, status = prov(dir, '--live', chat)
77
+ assert status.success?, err
78
+
79
+ # The live section header is present once, after the normal report.
80
+ headers = out.lines.select { |l| l.include?('Live work') }
81
+ assert_equal 1, headers.length, out
82
+
83
+ # The chat task is the inference entry; the wrapper only shows as
84
+ # workload context; neither duplicates the forensic report's work.
85
+ task_line = out.lines.find { |l| l.include?('WF/continue/one') }
86
+ assert task_line, out
87
+ assert_include task_line, 'chat_task'
88
+ assert_include task_line, 'running'
89
+ assert_include task_line, "log=#{task}.files/agent.chat"
90
+
91
+ wrap_line = out.lines.find { |l| l.include?('WF/cortex_continue/one') }
92
+ assert wrap_line, out
93
+ assert_include wrap_line, 'workflow'
94
+
95
+ # Exactly one line per entry, sorted deterministically
96
+ # (inference before workload context).
97
+ section = out.lines[(out.lines.index { |l| l.include?('Live work') } || 0)..-1]
98
+ body = section.select { |l| l =~ /^\s{2}(chat_task|workflow|dangling_job|agent_active)\b/ }
99
+ assert_equal 2, body.length, out
100
+
101
+ # The normal report was not changed by --live: the forensic footer
102
+ # (token totals) is identical with and without the flag.
103
+ out_plain, _e, s2 = prov(dir, chat)
104
+ assert s2.success?
105
+ footer = ->(o) { o.lines.find { |l| l.start_with?('root deduplicated_total=') } }
106
+ assert_equal footer.(out_plain), footer.(out), out
107
+ end
108
+ end
109
+
110
+ def test_quiescent_chat_live_adds_nothing
111
+ TmpFile.with_dir do |dir|
112
+ # Nothing anywhere: no .jobs sidecar, no .files, no society tree.
113
+ plain = write_chat(dir, 'plain.chat',
114
+ "user: go\nmeta: pt=1 ct=1 tt=2 inference_id=q1\nassistant: done\n")
115
+
116
+ out_plain, err, status = prov(dir, plain)
117
+ out_live, _err2, status2 = prov(dir, '--live', plain)
118
+ assert status.success?, err
119
+ assert status2.success?
120
+
121
+ assert_equal out_plain, out_live, 'quiescent chat: --live must add nothing'
122
+ assert_not_include out_live, 'Live work'
123
+ end
124
+ end
125
+
126
+ def test_captured_job_is_not_repeated_in_live_section
127
+ TmpFile.with_dir do |dir|
128
+ # A finished delegation already in the forensic report (meta: job=)
129
+ # plus a live chat task under the same parent.
130
+ wfdir = File.join(dir, 'var', 'jobs')
131
+ done = running_chat_task(wfdir, 'WF/continue/done')
132
+ # make it terminal so the forensic report resolves it as a job
133
+ info = JSON.parse(File.read(done + '.info'))
134
+ info['status'] = 'done'
135
+ File.write(done + '.info', info.to_json)
136
+
137
+ chat = write_chat(dir, 'parent.chat',
138
+ "user: go\nmeta: job=WF/continue/done\nassistant: done\n")
139
+ live_task = running_chat_task(wfdir, 'WF/continue/live')
140
+ File.write(Chat.jobs_file(chat), "WF/continue/live\n")
141
+
142
+ out, err, status = prov(dir, '--live', chat)
143
+ assert status.success?, err
144
+
145
+ # The captured (done) job must not appear in the live section.
146
+ live_lines = out.lines.select { |l| l =~ /^\s{2}(chat_task|workflow|dangling_job|agent_active)\b/ }
147
+ assert live_lines.none? { |l| l.include?('WF/continue/done') }, out
148
+ # The live task does.
149
+ assert live_lines.any? { |l| l.include?('WF/continue/live') }, out
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,213 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/llm/chat'
3
+ require 'json'
4
+ require 'open3'
5
+ require 'rbconfig'
6
+ require 'timeout'
7
+ require 'tmpdir'
8
+ require_relative 'agent_meta_fixtures'
9
+ require_relative 'prov_plot_fixtures'
10
+
11
+ # Explanatory provenance plot tests (`--dot` / `--plot`).
12
+ #
13
+ # These are `scout-ai llm prov --dot` output tests, NOT dot-syntax tests:
14
+ # every assertion below runs against the dot source the CLI writes, so the
15
+ # assertions grep the CLI's own strings (label=, tooltip=, class=) and not a
16
+ # graphviz rendering (except the two graphviz runs, which skip when the dot
17
+ # binary is missing).
18
+ #
19
+ # The fixture is the explanatory mirror (root chat, 6 delegated
20
+ # Cortex/continue jobs, one continuation chain of 3, one error job, 3
21
+ # society chats); see ProvPlotFixtures#explanatory_fixture.
22
+ class TestProvExplanatoryPlot < Test::Unit::TestCase
23
+ include AgentMetaFixtures
24
+ include ProvPlotFixtures
25
+
26
+ REPO_ROOT = File.expand_path('../../../..', __dir__)
27
+
28
+ def self.startup
29
+ Log.severity = 5
30
+ end
31
+
32
+ def prov(*args)
33
+ cmd = [RbConfig.ruby, '-I', File.join(REPO_ROOT, 'lib'),
34
+ File.join(REPO_ROOT, 'bin', 'scout-ai'), 'llm', 'prov',
35
+ '--nocolor', *args.collect(&:to_s)]
36
+ Open3.capture3(*cmd)
37
+ end
38
+
39
+ def dot_for(root)
40
+ @prov_count += 1
41
+ dot_file = File.join(@dir, "prov_#{@prov_count}.dot")
42
+ out, err, status = prov('--dot', dot_file, root)
43
+ assert status.success?, err
44
+ [File.read(dot_file), out]
45
+ end
46
+
47
+ setup do
48
+ @prov_count = 0
49
+ @dir = Dir.mktmpdir('prov-plot')
50
+ end
51
+
52
+ teardown do
53
+ FileUtils.remove_entry @dir if @dir && File.directory?(@dir)
54
+ end
55
+
56
+ test 'labels carry names, paths only in tooltips' do
57
+ TmpFile.with_dir do |dir|
58
+ fx = explanatory_fixture(dir); root = fx[:root]
59
+ dot, = dot_for(root)
60
+ node_lines = dot.lines.grep(/^\s+n\d+ \[/)
61
+ labels = node_lines.flat_map { |l| l.scan(/label=("(?:[^"\\]|\\.)*")/) }
62
+ assert_equal 15, node_lines.length, dot
63
+ assert labels.length >= 15, dot
64
+ # Packet item 1 vs item 8 tension, resolved narrowly: the jobname
65
+ # label itself is `Cortex/continue <name>`, whose slash is the
66
+ # workflow/task separator required by the legacy jobname tests, not
67
+ # a filesystem path. A NAME such as the basename `Worker.chat` is
68
+ # legitimate label content. What must NOT appear is any path: the
69
+ # fixture dir, a `.files/` or `agent.society` fragment, or a second
70
+ # slash beyond the one workflow/task separator.
71
+ labels.each do |label|
72
+ assert_not_include label, dir, "label leaks the fixture dir: #{label}"
73
+ assert_not_include label, '.files/', "label leaks a files dir: #{label}"
74
+ assert_not_include label, 'agent.society', "label leaks a society path: #{label}"
75
+ next unless label.include?('/')
76
+ assert_match(/\A"(?:[^"\/]|\\.)*\/(?:[^"\/]|\\.)*"\z/, label,
77
+ "label carries more than the workflow/task slash: #{label}")
78
+ end
79
+ tooltips = dot.scan(/tooltip=("(?:[^"\\]|\\.)*")/).flatten
80
+ assert_equal labels.length, tooltips.length
81
+ tooltips.each { |t| assert t.include?('/'), "tooltip without full path: #{t}" }
82
+ end
83
+ end
84
+
85
+ test 'root tooltip is the chat, root label names the kind' do
86
+ TmpFile.with_dir do |dir|
87
+ fx = explanatory_fixture(dir); root = fx[:root]
88
+ dot, = dot_for(root)
89
+ root_line = dot.lines.find { |l| l.include?("tooltip=#{root.inspect}") }
90
+ assert root_line, dot
91
+ assert_include root_line, 'Root conversation'
92
+ assert_include root_line, 'root.chat'
93
+ assert_match(/\{rank=source; n\d+;\}/, dot)
94
+ # the rank=source id must be the root node id
95
+ root_id = root_line[/^\s*(n\d+) \[/, 1]
96
+ assert dot.include?("{rank=source; #{root_id};}"), dot
97
+ end
98
+ end
99
+
100
+ test 'worker chat tooltips show the files dir' do
101
+ TmpFile.with_dir do |dir|
102
+ fx = explanatory_fixture(dir); root = fx[:root]; jobs = fx[:jobs]
103
+ dot, = dot_for(root)
104
+ first_turn = jobs[:smoke][0]
105
+ worker = File.join(first_turn + '.files', 'Worker.chat')
106
+ assert_include dot, "tooltip=#{worker.inspect}"
107
+ end
108
+ end
109
+
110
+ test 'semantic edge labels replace machine relations' do
111
+ TmpFile.with_dir do |dir|
112
+ fx = explanatory_fixture(dir); root = fx[:root]
113
+ dot, = dot_for(root)
114
+ assert_equal 6, dot.scan(/label="delegates"/).length, dot
115
+ assert_equal 5, dot.scan(/label="produces"/).length, dot
116
+ assert_equal 2, dot.scan(/label="continues"/).length, dot
117
+ # machine relation tokens survive only as class attributes
118
+ %w[delegated_result dependency].each do |token|
119
+ assert_not_include dot, %(label="#{token}")
120
+ assert dot.include?(%(class="#{token}")), dot
121
+ end
122
+ end
123
+ end
124
+
125
+ test 'continuation chain arcs follow mtime order' do
126
+ TmpFile.with_dir do |dir|
127
+ fx = explanatory_fixture(dir); root = fx[:root]; jobs = fx[:jobs]
128
+ dot, = dot_for(root)
129
+ id_of = {}
130
+ dot.lines.each do |line|
131
+ next unless line =~ /^\s*(n\d+) \[.*tooltip=("(?:[^"\\]|\\.)*")/
132
+ id_of[JSON.parse(Regexp.last_match(2))] = Regexp.last_match(1)
133
+ end
134
+ turn1, turn2, turn3 = jobs[:smoke]
135
+ assert_equal 2, dot.scan(/constraint=false, tailport=s, headport=n/).length, dot
136
+ assert dot.include?("#{id_of[turn1]} -> #{id_of[turn2]} [label=\"continues\""), dot
137
+ assert dot.include?("#{id_of[turn2]} -> #{id_of[turn3]} [label=\"continues\""), dot
138
+ # turn numbering is visible on the smoke labels
139
+ assert_include dot, 'cortex-smoke · turn 1'
140
+ assert_include dot, 'cortex-smoke · turn 2'
141
+ assert_include dot, 'cortex-smoke · turn 3'
142
+ end
143
+ end
144
+
145
+ test 'error outcome replaces token line' do
146
+ TmpFile.with_dir do |dir|
147
+ fx = explanatory_fixture(dir); root = fx[:root]; jobs = fx[:jobs]
148
+ dot, = dot_for(root)
149
+ bad_line = dot.lines.find { |l| l.include?("tooltip=#{jobs[:bad].inspect}") }
150
+ assert bad_line, dot
151
+ assert_include bad_line, '⚠ error'
152
+ assert_not_include bad_line, 'tokens'
153
+ assert_include bad_line, '#FBE3E3'
154
+ assert_include bad_line, 'prov-error'
155
+ end
156
+ end
157
+
158
+ test 'society chats keep pair labels with dashed unlabeled edges' do
159
+ TmpFile.with_dir do |dir|
160
+ fx = explanatory_fixture(dir); root = fx[:root]; society = fx[:society]
161
+ dot, = dot_for(root)
162
+ society.each do |chat|
163
+ line = dot.lines.find { |l| l.include?("tooltip=#{chat.inspect}") }
164
+ assert line, "#{chat}\n#{dot}"
165
+ pair = chat.split(File::SEPARATOR)[-3, 2] * '/'
166
+ assert_include line, "society #{pair}"
167
+ end
168
+ dashed = dot.lines.select { |l| l.include?('style=dashed') }
169
+ assert_equal 3, dashed.length, dot
170
+ dashed.each { |l| assert_not_include l, 'label=' }
171
+ end
172
+ end
173
+
174
+ test 'legacy shape contract and jobname labels survive' do
175
+ TmpFile.with_dir do |dir|
176
+ fx = explanatory_fixture(dir); root = fx[:root]
177
+ dot, = dot_for(root)
178
+ shapes = dot.scan(/^\s+n\d+ \[shape=(\w+)/).flatten
179
+ assert shapes.all? { |shape| %w[box note].include?(shape) }, dot
180
+ assert_equal 15, shapes.length, dot
181
+ assert_include dot, 'Cortex/continue echo-worker'
182
+ assert_include dot, 'Cortex/continue cortex-unknown-brief'
183
+ end
184
+ end
185
+
186
+ test 'graphviz renders the dot without error' do
187
+ TmpFile.with_dir do |dir|
188
+ fx = explanatory_fixture(dir); root = fx[:root]
189
+ dot_file = File.join(dir, 'prov.dot')
190
+ prov('--dot', dot_file, root)
191
+ skip 'graphviz dot not installed' unless Open.exists?('/usr/bin/dot')
192
+ out, err, status = Open3.capture3('dot', '-Tsvg', dot_file)
193
+ assert status.success?, err
194
+ assert out.start_with?('<?xml'), out[0, 100]
195
+ titles = out.scan(/<title>/).length
196
+ assert titles >= 15, out
197
+ end
198
+ end
199
+
200
+ test 'plot writes an svg of bounded natural width' do
201
+ TmpFile.with_dir do |dir|
202
+ fx = explanatory_fixture(dir); root = fx[:root]
203
+ plot_file = File.join(dir, 'prov.svg')
204
+ out, err, status = prov('--plot', plot_file, root)
205
+ assert status.success?, err
206
+ assert File.file?(plot_file), out
207
+ svg = File.read(plot_file)
208
+ width = svg[/width="([\d.]+)pt"/, 1].to_f
209
+ assert width > 0, svg[0, 200]
210
+ assert_operator width, :<=, 1200, "natural width #{width}pt too wide"
211
+ end
212
+ end
213
+ end