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,213 @@
1
+ # Sandbox: which tools are confined, and which paths they can reach
2
+
3
+ This document answers the three sandbox questions agents keep asking: *is
4
+ this tool sandboxed?*, *which paths can this action reach?*, and *this path
5
+ exists but I cannot see it — what now?*. It explains how path access and
6
+ confinement work across Scout-AI tools, with particular attention to
7
+ Workspace's two distinct enforcement mechanisms: Ruby-layer path
8
+ authorization and OS-level confinement through bwrap. Not every tool uses
9
+ both mechanisms, and some tools do not apply these path checks at all. The
10
+ asymmetry between the layers is intended, not a bug.
11
+
12
+ **When access fails**, identify the exact tool first. Run `sandbox_paths`
13
+ when available, inspect the relevant grants and mounts, use the path
14
+ spelling exposed inside the sandbox, and retry through the tool you actually
15
+ need. Never infer a tool's permissions from another tool's success or
16
+ failure.
17
+
18
+ See [Improvements.md](../Improvements.md) R14 for the one still-open item (the Workspace `test_tsv.rb` failure history).
19
+
20
+ ## 1. Is this tool sandboxed?
21
+
22
+ `LLM::Sandbox` (`lib/scout/llm/sandbox.rb`) provides two conceptually
23
+ separate interfaces.
24
+
25
+ The **containment core** — `resolve_path` (`LLM::Sandbox#resolve_path`),
26
+ `authorize_path` (`#authorize_path`), `inside?` (`#inside?`) — resolves
27
+ paths and checks them against grants supplied by the caller. It does not
28
+ decide which grant sources a particular tool should use, and
29
+ `authorize_path` defaults `root: Dir.pwd`, an ambient default that callers
30
+ (such as Workspace) must override deliberately.
31
+
32
+ The **shared grant-resolution layer** — `grant_type_defaults`,
33
+ `grant_scope_tokens`, `grant_paths`, `expand_grant_path`, `writable_grants`,
34
+ `readable_grants`, `grant_origin`, `describe_grants` (all
35
+ `LLM::Sandbox.<name>`, `lib/scout/llm/sandbox.rb`) — resolves configured
36
+ grant sources, applies defaults and environment overrides, and collects
37
+ thread-local grants. Individual tools use this layer to assemble their
38
+ effective grants, then enforce access according to their own execution
39
+ model.
40
+
41
+ Grant resolution, path authorization and OS-level confinement are separate
42
+ concerns. Using the shared grant resolver does not, by itself, sandbox a
43
+ tool. The grant-registration helpers (`allow_path`, `allow_read_path`,
44
+ `allow_job`, `allow_read_job`) only write the pre-existing `Chat`
45
+ thread-local keys that the resolution layer collects.
46
+
47
+ Which enforcement a tool gets is a per-tool property. Line anchors below
48
+ point into the Workspace workflow (a separate repository), so treat the
49
+ file names as stable identifiers and re-grep the method when the line
50
+ drifts:
51
+
52
+ | Tool / action | Sandboxed? | Enforcement point |
53
+ |---|---|---|
54
+ | Workspace `read`/`write`/`delete`/`search`/`list_directory`/`file_stats`/`copy` | Ruby allowlist | filesystem.rb `helper :normalize` and the task bodies (`write`, `read`, `list_directory`, `file_stats`, `delete`, `copy`, `search`) |
55
+ | Workspace `precise_edit` | Ruby allowlist (`:write`) | precise_edit.rb (`task :precise_edit`) |
56
+ | Workspace `pwd`, `sandbox_paths` | No enforcement (read-only introspection) | filesystem.rb (`task :sandbox_paths`, exported `task :pwd`) |
57
+ | Workspace `patch` | bwrap OS sandbox only (no Ruby-layer normalize) | patch.rb (`Dir.chdir(Workspace.root)` then `cmd_json`) |
58
+ | Workspace `tsv_*` | No Ruby-layer check | tsv.rb (opens files via Scout `TSV`/`Open` directly) |
59
+ | Workspace `bash`/`python`/`ruby`/`r` | bwrap OS sandbox only | exec.rb (`cmd_json`, `sandbox_run`) |
60
+ | scout-ai agent `path_*` tools | Yes, when the kind sets `sandbox: true` (the engine default) | path.rb `path_authorize!` |
61
+ | scout-ai `LLM::Agent` / workflow / call tools | Grant registration, not confinement | agent.rb (`Chat.allow_job`), tools/workflow.rb, tools/call.rb, agent/workflow.rb |
62
+ | ChatAnalyst-style tasks (`Chat.read`, `LLM.call_workflow`) | No `LLM::Sandbox` path authorization in the inspected task code (no `LLM::Sandbox`/`allow_*` references under `ChatAnalyst/lib/`). Do not assume these tasks are confined without checking their execution environment. | none identified |
63
+
64
+ **Tests:** `test/scout/llm/test_sandbox.rb` pins the core semantics —
65
+ `test_authorization_uses_root_and_nested_boundary_not_sibling_prefix`,
66
+ `test_grant_sources_compose_defaults_thread_and_config`,
67
+ `test_grant_scope_tokens_and_expand` among its 16 cases.
68
+
69
+ The asymmetry is intended: each tool has its own capability and permission
70
+ model. A restriction observed with one tool is **not evidence** about
71
+ another — a `read` rejection tells you nothing about `bash`, and an exec
72
+ success tells you nothing about `write`. When a path is rejected or a
73
+ visibility mismatch appears, diagnose the specific tool involved.
74
+
75
+ ## 2. Which paths can this action reach?
76
+
77
+ There are two layers, and they differ on purpose.
78
+
79
+ **Layer 1 — Ruby allowlist (Workspace filesystem tasks).** All filesystem
80
+ tasks route through `helper :normalize`, which calls
81
+ `LLM::Sandbox.authorize_path` with `root: Workspace.root`,
82
+ `writable_paths: Workspace.allowed_paths`,
83
+ `read_paths: Workspace.allowed_read_paths`, `base_path: Dir.pwd`, and raises
84
+ `SandboxAccessViolation` on denial (filesystem.rb `helper :normalize`). The
85
+ grant lists are composed by the shared grant-resolution layer
86
+ (`LLM::Sandbox.writable_grants(scope: 'Workspace')` /
87
+ `readable_grants(scope: 'Workspace')`), to which `Workspace.allowed_paths`
88
+ and `Workspace.allowed_read_paths` simply delegate (`get_allowed_paths`,
89
+ `allowed_paths`, `allowed_read_paths`).
90
+ `Workspace.get_allowed_paths(type)` delegates to the same layer
91
+ (`LLM::Sandbox.grant_paths`). Any other sandboxed workflow can adopt the
92
+ identical vocabulary by calling the same functions with its own scope.
93
+
94
+ The two grant sets compose these sources:
95
+
96
+ | Grant set | Sources |
97
+ |---|---|
98
+ | Writable | `allowed_paths`, `dirs`, `thread` |
99
+ | Readable | `allowed_read_paths`, `exec_paths`, `read_paths`, `thread_read` |
100
+
101
+ Seven source categories in total. Two carry non-empty literal defaults in
102
+ `grant_type_defaults`: `exec_paths` defaults to
103
+ `/bin:/usr:/lib:/lib64:/etc`, and `allowed_paths` defaults to the Scout
104
+ pathmap expansion of `tmp` plus `/tmp` and `~/tmp`. `thread` and
105
+ `thread_read` read the live Chat thread keys (the same keys
106
+ `allow_path`/`allow_read_path` write; empty until something registers a
107
+ grant). `dirs`, `read_paths` and `allowed_read_paths` default to empty,
108
+ meaning they contribute nothing unless configured. Every source is
109
+ overridable through `Scout::Config` under the workflow scope tokens
110
+ (`grant_scope_tokens`), or an env var such as `ALLOWED_PATHS` (the key name
111
+ upper-cased); a scoped config entry beats the env var (the env var only
112
+ replaces the *default*), which in turn beats the built-in default.
113
+
114
+ Two introspection helpers round out the layer: `grant_origin(path, scope:,
115
+ root:)` reports which source category an expanded path came from, by
116
+ expanded-spelling equality over the full origin vocabulary (`root`, `dirs`,
117
+ `allowed_paths`, `thread`, `exec_paths`, `read_paths`, `allowed_read_paths`,
118
+ `thread_read`, `bwrap_paths`, `bwrap_read_paths`), returning `nil` for
119
+ unlisted paths; and `describe_grants(scope:, root:, hint:)` renders one
120
+ annotated line per grant — writable entries marked `(read-write)`, remaining
121
+ readable entries `(read-only)`, with the caller's `hint` appended verbatim
122
+ as the last line. `scope: nil` degrades the token chain to `['sandbox']`;
123
+ `expand_grant_path` expands located spellings in place and resolves
124
+ unlocated Scout spellings (such as `tmp`) through `Path#find_all`.
125
+
126
+ `Workspace.root` is **not** part of that composition: it is supplied as the
127
+ separate `root:` boundary of `authorize_path`, which is why it appears
128
+ separately in the diagnostics.
129
+
130
+ **Layer 2 — bwrap OS sandbox (Workspace exec tasks).** `sandbox_run` starts
131
+ from the same two lists but is strictly broader (exec.rb): writable gains
132
+ `Workspace.root` unconditionally plus optional config `bwrap_paths`; read
133
+ gains optional `bwrap_read_paths`, `/etc/resolv.conf` when networking is
134
+ enabled, the runtime directories of the resolved interpreter, and
135
+ relocatable runtime layouts scanned from `$PATH`. Each grant becomes a bind
136
+ mount planned from its expanded spelling (`plan_mounts`:
137
+ `File.expand_path` for the destination, `safe_realpath` for the source),
138
+ plus `/tmp`, `/proc` and `/dev`.
139
+
140
+ **Concrete consequence.** A path readable from `bash` can be rejected by
141
+ `read`, and that is expected: the exec layer sees the interpreter's runtime
142
+ tree through runtime-dir grants the Ruby read allowlist does not carry. The
143
+ reverse also happens — a path granted by the allowlist can be bound
144
+ read-only inside bwrap. Run the Workspace `sandbox_paths` task for the exact
145
+ inventory of both layers: every allowed read/write path with its origin, the
146
+ planned mounts (source, destination, ro/rw, redirect), and the realized
147
+ mounts from `/proc/mounts` (`task :sandbox_paths` and the
148
+ `realized_mounts` helper, both in filesystem.rb).
149
+
150
+ ## 3. This path exists but I cannot see it — what next?
151
+
152
+ 1. **Run the Workspace `sandbox_paths` diagnostic and read the origins.** It
153
+ reports both layers, so you can tell "not granted at all" from "granted
154
+ but not mounted". Denial messages from `normalize` already point at it
155
+ ("Run task 'sandbox_paths' for details").
156
+ 2. **Prefer the granted spelling in sandboxed execution environments.**
157
+ Ruby-layer authorization resolves existing path components to canonical
158
+ paths (see `resolve_path` below), whereas bwrap mount destinations are
159
+ based on expanded grant spellings: `plan_mounts` uses the expanded grant
160
+ as the mount *destination* and realpaths only the *source*
161
+ (`safe_realpath` helper; mounts built in `plan_mounts`),
162
+ so two spellings of one host directory may be equivalent for
163
+ authorization but not equally visible inside the execution sandbox — and
164
+ a spelling that was never granted gets no mount at all. On hosts that
165
+ alias `/home/user/...` with `/bulk/user/...`, a directory can be fully
166
+ visible under one spelling and `ENOENT` under the other. When a mount destination
167
+ traverses a symlink whose parent is already mounted, the destination is
168
+ redirected to the realpath — `m.destination = m.source` — and the plan
169
+ records `redirect: true` (`destination != requested_path`, in the
170
+ mount-planning code of exec.rb; the symlink target is additionally mounted
171
+ read-only so the host symlink still resolves).
172
+ 3. **In exec tools, `cd` to the granted root** (e.g. the `root` field
173
+ reported by `sandbox_paths`) rather than a spelling that has no mount.
174
+ 4. **Re-check with the specific tool you actually need.** The layers differ,
175
+ so the definitive answer is the behavior of that one tool, not of a
176
+ proxy: verify with the `read`/`write` task, or with `bash`, as
177
+ appropriate.
178
+
179
+ ### `resolve_path` semantics and TOCTOU
180
+
181
+ `LLM::Sandbox.resolve_path` walks component by component
182
+ (`LLM::Sandbox#resolve_path`):
183
+ each existing component is `File.realpath`-ed, so symlinks collapse to
184
+ physical paths; `.` is skipped; the first *missing* component switches the
185
+ walk to lexical mode, after which `exists` stays false and later components
186
+ are appended unresolved. It fails closed on the unresolvable cases: a `..`
187
+ reached after a missing component returns `exists: false,
188
+ canonical_path: nil`; `..` after a non-directory raises `ENOTDIR`; a
189
+ dangling symlink or an `ENOTDIR`/`EACCES` on an existing parent surfaces the
190
+ error instead of treating the path as lexical space. Both the target and
191
+ every grant are resolved this way in `authorize_path`, so a symlink cannot
192
+ make a grant escape its physical target, and the boundary-aware `inside?`
193
+ rejects sibling prefixes (`/a-b` is not inside `/a`)
194
+ (`LLM::Sandbox#authorize_path`, `LLM::Sandbox#inside?`).
195
+
196
+ Authorization is **policy, not an atomic capability**: it applies to a path
197
+ spelling at check time. Nothing prevents the filesystem from changing between
198
+ the check and the operation (see the same limitation recorded for the Path
199
+ engine's advisory existence checks; see
200
+ [PathEngine.md](PathEngine.md)). TOCTOU hardening is
201
+ explicitly out of scope.
202
+
203
+ ## Sources / verification
204
+
205
+ Behavioral claims in this document are pinned by
206
+ `test/scout/llm/test_sandbox.rb` (16 cases; notably
207
+ `test_authorization_uses_root_and_nested_boundary_not_sibling_prefix`,
208
+ `test_grant_sources_compose_defaults_thread_and_config`,
209
+ `test_grant_scope_tokens_and_expand`) and by the code anchors cited
210
+ inline (`lib/scout/llm/sandbox.rb`, the Workspace `filesystem.rb` /
211
+ `exec.rb` / `tsv.rb` / `patch.rb` / `precise_edit.rb` task files, and
212
+ `lib/scout/llm/agent/path.rb`). Workspace-side file names refer to the
213
+ Workspace workflow repository.
@@ -57,7 +57,7 @@ timestamp, `_1`, `_2`, … suffixes on name collisions) before clearing it. The
57
57
  snapshot is lazy — no reset directory is created when there is nothing to
58
58
  snapshot — and non-fatal if it fails. Reset snapshots live directly under
59
59
  `.files`, in none of the three directories the `:log` relation sweeps
60
- (`*.files/*.chat`, `*.files/*.society/**`, the legacy `*.files/log/**`), so
60
+ (`*.files/*.chat`, `*.files/*.society/**`), so
61
61
  provenance traversal ignores them: they are recovery artifacts, not logs.
62
62
 
63
63
  ### Adding messages
@@ -68,6 +68,36 @@ Use role methods (forwarded to the underlying chat):
68
68
  agent.user "What is the capital of France?"
69
69
  ```
70
70
 
71
+ The forwarded builder set is the chat DSL, so the same helpers work on an
72
+ agent: `system`, `user`, `assistant`, `file`, `pdf`, `image`,
73
+ `directory`, `import` (inline a whole chat), `continue`/`import_last`
74
+ (inline only its last message), `introduce`, `tool`, `task`/`job` (and
75
+ `inline_`/`exec_` variants), `format`, `option`, `endpoint`, `model`,
76
+ `association`, and `tag`.
77
+
78
+ ### Agents with a workflow: how `ask` routes
79
+
80
+ When an agent has a workflow attached **and** that workflow defines an `:ask`
81
+ task, `Agent#ask` does not call `LLM.ask` directly. It serializes the
82
+ conversation, creates `workflow.job(:ask, chat: ...)`, saves agent state *before*
83
+ producing (so the `job=` reference exists while the answer is computed),
84
+ whitelists the job for the chat sandbox, and reads the projected result back
85
+ from the job. Each round is therefore a cached, inspectable Scout job with its
86
+ own `files_dir`, and `scout-ai llm prov` can walk the chain. Set
87
+ `no_ask_override: true` on the agent (an option, settable in the agent's
88
+ directory or via the `agent` helper) to skip this routing and use plain
89
+ `LLM.ask` — a verification-only agent such as a critic usually wants this.
90
+ `SCOUT_NO_ASK_CACHE=true` cleans the cached job before reuse (`recursive` for a
91
+ recursive clean).
92
+ The job whitelist above is one corner of the shared sandbox model: which
93
+ tools are confined, which paths each layer can reach, and what to do when a
94
+ path exists but a tool cannot see it are documented in the
95
+ [Sandbox](../developer/Sandbox.md) developer document.
96
+
97
+ A plain agent with no workflow (or a workflow without an `:ask` task) always
98
+ takes the `LLM.ask` branch, with `save_file: self.save_file` and `agent: false`
99
+ so the nested call is not re-detected as an agent round.
100
+
71
101
  ### Getting a response
72
102
 
73
103
  Call `chat` to send the conversation to the model and get a response:
@@ -89,6 +119,11 @@ conversation is serialized recursively at the end of every `chat` call (see
89
119
  below). A bare `agent.ask(...)` does **not** auto-save — only `chat` does.
90
120
  `LLM.ask` goes through `agent.chat`, so it inherits the same behavior.
91
121
 
122
+ Three forwarded readers are the common way to take a result out of a chat:
123
+ `agent.answer` (the content of the final, `previous_response_id`-purged
124
+ message), `agent.final` (that message itself), and `agent.shed` (a new
125
+ one-message chat holding `final`).
126
+
92
127
  ---
93
128
 
94
129
  ## Persisting agents: `save_file`, `save`, and auto-save
@@ -124,10 +159,6 @@ The canonical layout is decided by *location*, not configuration:
124
159
  at `<chat_or_job_path>.files/<name>.chat`: `agent.chat` for the default
125
160
  agent, `worker.chat`/`critic.chat` for named ones.
126
161
 
127
- **Legacy layout (read-only).** Older versions wrote
128
- `<path>.files/log/agent.chat` and `log/society/…`. Nothing writes there
129
- anymore, old files are not migrated, and provenance still reads them.
130
-
131
162
  Saving is lazy (nothing is created eagerly; parent directories are created on
132
163
  demand) and non-fatal (a failure logs a warning and the run continues). Cycle
133
164
  protection is built in: already-visited paths, already-seen agents, and a
@@ -146,8 +177,64 @@ Agent/
146
177
  workflow.rb # optional: Scout workflow providing tools
147
178
  knowledge_base/ # optional: KB for retrieval
148
179
  python/ # optional: Python workflow tasks
180
+ agent.rb # optional: Ruby file defining the agent
149
181
  ```
150
182
 
183
+ **Discovery order.** The **filename branch** runs first: `Path.is_filename?`
184
+ is a plain `File.exist?` test, so any argument that names an existing path —
185
+ including `Agent/Researcher` — is loaded as that path (a directory with an
186
+ `agent.rb` loads it; any other file is loaded itself; a directory without
187
+ `agent.rb` falls through to the named branch with the full path as the name).
188
+ Only a bare name goes through the directory candidates:
189
+
190
+ 1. a workflow checkout named `Researcher` (`Scout.workflows['Researcher']`);
191
+ 2. `Scout.Agent['Researcher']`;
192
+ 3. `Scout.var.Agent['Researcher']`;
193
+ 4. `Scout.chats.Agent['Researcher']`;
194
+ 5. `Scout.chats['Researcher']`.
195
+
196
+ The workflow candidate is checked **first**: when `Scout.workflows[name]`
197
+ exists it wins and the agent path is redirected into that workflow directory
198
+ (so `start_chat` and `knowledge_base` are then looked up inside it) — a repo
199
+ named after your agent shadows the `Agent/` directory. Otherwise the first of
200
+ 2–5 that exists is used; if none does, loading raises
201
+ `ScoutException: No agent found with name Researcher`.
202
+
203
+ Two other loaders look up agent names through **fewer** roots, so an agent
204
+ that loads fine from Ruby may still be invisible to them: `tool:` /
205
+ `introduce:` roles in a chat go through `Chat.load_workflow`, which only
206
+ tries `Scout.chats.Agent` (then `Workflow.require_workflow`), and the
207
+ `scout agent kb` CLI hard-codes `Scout.var.Agent`. The `kb:` chat role, by
208
+ contrast, resolves a path first and only falls back to `LLM.load_agent`
209
+ (full root list) when that path is not a loadable knowledge base.
210
+
211
+ Inside the chosen directory the workflow is picked as
212
+ `Workflow.require_workflow_file(workflow.rb)` when present, else a
213
+ `PythonWorkflow` from `python/*.py`, else none; the KB from `knowledge_base/`
214
+ (of the agent directory, or of the workflow checkout); and the seed from
215
+ `start_chat` (or, when a workflow with a description exists and there is no
216
+ `start_chat`, a single `introduce:` role). Name the seed file `start_chat`
217
+ literally: `start_chat` wins over `start_chat.chat`, and a directory that
218
+ offers only `start_chat.chat` gets an **empty** seed — a silent
219
+ misconfiguration, see the common mistakes below.
220
+
221
+ ### The `agent.rb` file
222
+
223
+ An agent directory may also carry an `agent.rb`. When the argument is a path,
224
+ `LLM.load_agent('Agent/Researcher')` loads `Agent/Researcher/agent.rb` when
225
+ it exists (or the file itself when the argument is not a directory) and the
226
+ file's **last expression must be an `Agent`** — the value of the file *is* the
227
+ agent. The `agent.rb` takes responsibility for the whole agent: it must
228
+ configure workflow, knowledge base and `start_chat` itself, because none of
229
+ the directory assembly runs for it. A directory holding both `agent.rb` and
230
+ `workflow.rb` uses `agent.rb` for the agent object and ignores the workflow
231
+ assembly.
232
+
233
+ Two caching traps come with `workflow.rb`: the workflow module cache
234
+ (`@@agent_workflow`) is name-keyed and process-global, so two agent
235
+ directories defining the same module name share the first module loaded, and
236
+ an edit to `workflow.rb` stays invisible until the process restarts.
237
+
151
238
  ### The start_chat file
152
239
 
153
240
  This is a chat file (see [WritingChats.md](WritingChats.md)) containing the
@@ -162,9 +249,15 @@ Always cite your sources.
162
249
  endpoint: anthropic
163
250
  model: claude-sonnet-4-20250514
164
251
 
252
+ tool: SearchWorkflow
165
253
  introduce: SearchWorkflow
166
254
  ```
167
255
 
256
+ Name the file exactly `start_chat` (no extension). `start_chat` wins over
257
+ `start_chat.chat`, and a directory that offers only `start_chat.chat` gets an
258
+ **empty** seed — the agent silently starts with no system prompt and no tools.
259
+ See [Improvements.md](../Improvements.md) issue 7.
260
+
168
261
  ### Loading and using a named agent
169
262
 
170
263
  From Ruby:
@@ -182,16 +275,6 @@ From the CLI:
182
275
  scout-ai agent ask Researcher "Find papers about protein folding."
183
276
  ```
184
277
 
185
- ### Agent discovery locations
186
-
187
- Scout-AI looks for named agents in several places (first match wins):
188
-
189
- 1. `Scout.workflows[name]`
190
- 2. `Scout.Agent[name]`
191
- 3. `Scout.var.Agent[name]`
192
- 4. `Scout.chats.Agent[name]`
193
- 5. `Scout.chats[name]`
194
-
195
278
  ---
196
279
 
197
280
  ## Giving agents tools
@@ -217,22 +300,34 @@ agent.workflow do
217
300
  end
218
301
  ```
219
302
 
303
+ > Agents can also expose *path kinds* — typed, sandboxed filesystem
304
+ > namespaces with read/write/edit/patch operations and per-kind custom
305
+ > operations. A workflow declares the kinds it provides as a `PATH_KINDS`
306
+ > constant; agents enable them with a `path_kind: MyWorkflow` chat line
307
+ > (a start_chat-less agent also loads them automatically when it
308
+ > incorporates the whole workflow). See the
309
+ > [Path engine](../developer/PathEngine.md) developer document.
310
+
220
311
  ### Declaring tools in the start chat
221
312
 
222
- Use `tool:` or `introduce:` roles in the start_chat file:
313
+ Use `tool:` to expose tasks and `introduce:` to add their documentation:
223
314
 
224
315
  ```text
225
316
  system:
226
317
 
227
318
  You are a code analyst.
228
319
 
320
+ tool: CodeAnalyzer
229
321
  introduce: CodeAnalyzer
230
322
  ```
231
323
 
324
+ `introduce:` alone generates no tools; it only injects the workflow's title
325
+ and description as a user message.
326
+
232
327
  ### Knowledge base and MCP tools
233
328
 
234
329
  ```text
235
- kb: my_database [genes proteins]
330
+ kb: my_kb genes proteins
236
331
  mcp: https://api.example.com/mcp/
237
332
  ```
238
333
 
@@ -265,6 +360,9 @@ Common options:
265
360
  | `format:` | Output format (`:json`, `:text`, or a JSON schema hash) |
266
361
  | `persist:` | Whether to cache inference results (default `true`) |
267
362
 
363
+ Endpoint naming and caching semantics are documented once in
364
+ [RunningInference.md](RunningInference.md).
365
+
268
366
  ---
269
367
 
270
368
  ## Structured outputs
@@ -303,11 +401,17 @@ agent.iterate("List 3 action items") do |action|
303
401
  end
304
402
  ```
305
403
 
404
+ Both helpers pin `endpoint :responses` first (they rely on that backend's
405
+ strict JSON-schema handling); `iterate_dictionary` is the same pattern over
406
+ a flat string→string hash, traversed with `TSV.traverse` (so the usual
407
+ traversal keyword options apply) with one block call per pair.
408
+
306
409
  ---
307
410
 
308
411
  ## Error handling
309
412
 
310
- Set a `process_exception` callback to intercept errors during inference:
413
+ Backends do not retry. Set a `process_exception` callback on the agent to
414
+ intercept errors during inference and decide whether to retry:
311
415
 
312
416
  ```ruby
313
417
  agent.process_exception = Proc.new do |exception|
@@ -328,6 +432,14 @@ end
328
432
  default branch. Calling `start` explicitly makes the lifecycle clear.
329
433
  - **Putting user messages on `start_chat`**: `start_chat` is the *seed* — it
330
434
  should contain system prompts and configuration, not the actual question.
435
+ - **Naming the seed `start_chat.chat`**: the loader looks for the literal
436
+ file `start_chat`. When `start_chat` exists, the `.chat` variant is never
437
+ read; when it is the only one present, the seed comes out **empty** rather
438
+ than raising. Always name the file `start_chat`.
439
+ - **Expecting an edited `workflow.rb` to reload**: the loaded workflow module
440
+ is cached process-globally and never invalidated, so an edit is invisible
441
+ until the process restarts, and two agent directories defining the same
442
+ module name share the first one loaded.
331
443
  - **Expecting `ask` to append to the conversation**: Use `chat` for the
332
444
  stateful pattern (ask + append + return text). `ask` is the lower-level
333
445
  primitive.
data/doc/user/Cookbook.md CHANGED
@@ -87,16 +87,13 @@ Agent/
87
87
  start_chat
88
88
  ```
89
89
 
90
- `start_chat`:
90
+ `start_chat` (seed only — system prompt, no user turn; see
91
+ [BuildingAgents.md](BuildingAgents.md) "Common mistakes"):
91
92
 
92
93
  ```text
93
94
  system:
94
95
 
95
96
  You are a friendly greeter. Always greet by name.
96
-
97
- user:
98
-
99
- Hello!
100
97
  ```
101
98
 
102
99
  Use it:
@@ -198,7 +195,7 @@ orchestrator.chat
198
195
  ```ruby
199
196
  module AnalysisPipeline
200
197
  extend Workflow
201
- include AgentWorkflow
198
+ include_workflow AgentWorkflow
202
199
 
203
200
  chat_task :plan do |objective|
204
201
  agent = self.agent('Planner', chat: chat)
@@ -268,6 +265,8 @@ puts agent.chat
268
265
 
269
266
  ## Error handling with retry
270
267
 
268
+ Backends themselves never retry — retrying is an agent-level concern:
269
+
271
270
  ```ruby
272
271
  agent = LLM.agent(endpoint: :openai)
273
272
  agent.process_exception = Proc.new do |e|
@@ -309,7 +308,7 @@ system:
309
308
 
310
309
  You are a bioinformatics assistant.
311
310
 
312
- kb: gene_db [genes proteins diseases]
311
+ kb: gene_db genes proteins diseases
313
312
 
314
313
  user:
315
314
 
@@ -325,9 +324,31 @@ system:
325
324
 
326
325
  You have access to an external search service.
327
326
 
328
- mcp: https://api.example.com/mcp/ [search]
327
+ mcp: https://api.example.com/mcp/ search
329
328
 
330
329
  user:
331
330
 
332
331
  Search for recent papers on climate change.
333
332
  ```
333
+
334
+ ---
335
+
336
+ ## CLI quick reference
337
+
338
+ Every installed command, with the behaviour notes that matter:
339
+
340
+ | Command | What it does | Notes |
341
+ |---|---|---|
342
+ | `scout-ai llm ask [question]` | One-shot inference | `-t/--template*` (template file or `Scout.questions` name; `???` marks where the question goes), `-c/--chat*` (follow/append a conversation file), `-i/--imports*`, `-in/--inline*` (rewrite `# ask:` comments in a file in place), `-f/--file*` (prepend file content, `<file>`-tagged), `-w/--workflow*` (expose a workflow as a tool), `-m/--model`, `-e/--endpoint`, `-b/--backend`, `-d/--dry_run` (print the prompt, skip the model). STDIN becomes context referenced with `...` in the question. |
343
+ | `scout-ai llm json` | Chat ↔ JSON conversion | `--chat`, `--json`, `--output`, `-l/--last` |
344
+ | `scout-ai llm md` | Chat → Markdown | `--output`, `-l/--last` |
345
+ | `scout-ai llm word` | Chat → `.docx` via pandoc | `--reference` (style template), `-l/--last` |
346
+ | `scout-ai llm template` | List question templates | reads `Scout.questions` |
347
+ | `scout-ai llm process` | Queue daemon over `Scout.var.ask` | loop: run each file, write reply, delete, sleep 1; `-p/--process <id>` |
348
+ | `scout-ai llm process_queries` | Queue daemon over `Scout.var.query` | same loop using `process: id` raw-response mode |
349
+ | `scout-ai llm prov` | Provenance tree/flow/DOT/timeline | see [developer/Provenance.md](../developer/Provenance.md) |
350
+ | `scout-ai llm server` | Sinatra backend for the offline notebook UI | needs sinatra |
351
+ | `scout-ai agent ask <agent>` | Ask a named agent (`LLM::Agent`) | `-t/--template*`, `-c/--chat*`, `-i/--imports*`, `-f/--file*`, `-w/--workflow*`, `-wt/--workflow_tasks*`, `-m/--model`, `-e/--endpoint`, `-l/--log*`; STDIN context like `llm ask` |
352
+ | `scout-ai agent find <agent>` | Resolve and print an agent path | `LLM.load_agent` |
353
+ | `scout-ai agent kb <agent>` | Agent-scoped `scout kb` browser | `--knowledge_base <agent_dir>.knowledge_base` |
354
+ | `scout-ai workflow mcp <workflow> [tasks]` | Run a workflow as an MCP stdio service | named tasks select exports; default: exported tasks, else all |
@@ -102,7 +102,7 @@ kinds:
102
102
 
103
103
  | Tool source | How you declare it | What it gives the model |
104
104
  |-------------|-------------------|----------------------|
105
- | **Scout Workflow** | `tool:` or `introduce:` in chat, or auto-wired from agent workflow | Tasks with typed inputs/outputs become callable functions |
105
+ | **Scout Workflow** | `tool:` in chat (bare workflow or `workflow task`), or auto-wired from agent workflow; `introduce:` adds documentation only | Tasks with typed inputs/outputs become callable functions |
106
106
  | **Knowledge Base** | `kb:` in chat | Database lookups (gene→protein, drug→disease, etc.) |
107
107
  | **MCP Server** | `mcp:` in chat | Any MCP-compatible external tool |
108
108
 
@@ -116,16 +116,16 @@ is appended to the conversation.
116
116
 
117
117
  ## Inference endpoint: provider abstraction
118
118
 
119
- An **endpoint** is a named bundle of provider + model + credentials. You
120
- configure endpoints once and reference them by name everywhere:
119
+ An **endpoint** is a named bundle of provider + model + credentials: a
120
+ hand-written `~/.scout/etc/AI/<name>.yaml`. You configure endpoints once and
121
+ reference them by name everywhere:
121
122
 
122
123
  ```bash
123
124
  # Use the 'anthropic' endpoint for this conversation
124
125
  scout-ai llm ask -e anthropic "Hello"
125
126
  ```
126
127
 
127
- Scout-AI supports OpenAI, Anthropic, Ollama, vLLM, and other OpenAI-compatible
128
- providers. Endpoints are provider-agnostic from the application's perspective —
128
+ Endpoints are provider-agnostic from the application's perspective —
129
129
  you write your agent once and switch models by changing the endpoint name.
130
130
 
131
131
  → See [RunningInference.md](RunningInference.md) for endpoint configuration
@@ -166,10 +166,11 @@ responds with plain text.
166
166
  | Run a saved conversation | `scout-ai llm ask -c file.chat` |
167
167
  | Create a reusable persona with tools | An Agent (directory with `start_chat`) |
168
168
  | Give the model data to query | Knowledge Base tools (`kb:`) |
169
- | Give the model code to run | Workflow tools (`tool:` / `introduce:`) |
169
+ | Give the model code to run | Workflow tools (`tool:`) |
170
170
  | Use external tools | MCP (`mcp:`) |
171
171
  | Build multi-agent systems | Delegation (`socialize` / `delegate`) |
172
172
  | Build reproducible pipelines | AgentWorkflow (Scout Workflow + Agent) |
173
+ | Wrap ML/PyTorch/HF models as reproducible agents | The Model subsystem ([../Model.md](../Model.md)) |
173
174
 
174
175
  ---
175
176