pikuri-workspace 0.1.0 → 0.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.
@@ -22,8 +22,9 @@ module Pikuri
22
22
  # == Truncation rules
23
23
  #
24
24
  # Line/byte windowing is delegated to {Pikuri::FileType.read_as_text_paged},
25
- # which returns a {Pikuri::Extractor::Page} this tool renders (same windower
26
- # backs +VectorDb::Tools::Read+). Two limits, first to fire wins: a line
25
+ # which returns a {Pikuri::Extractor::Page} this tool renders through
26
+ # {Pikuri::Tool::Result.page} (the one paging trailer every reader shares).
27
+ # Two limits, first to fire wins: a line
27
28
  # limit ({DEFAULT_LIMIT}, overridable via +limit+) and a byte cap
28
29
  # ({MAX_BYTES}, not a parameter, bypassable by paging via +offset+). Lines
29
30
  # over {MAX_LINE_LENGTH} are truncated with {LINE_TRUNCATION_MARKER}. (The
@@ -42,11 +43,12 @@ module Pikuri
42
43
  # == Image attachments
43
44
  #
44
45
  # PNG/JPEG/GIF/WebP (magic-byte detected) route to {.format_image} ahead of
45
- # the binary sniff, returning a +RubyLLM::Content+: a metadata note ("Read
46
- # image: …") the model cites, plus the file as an attachment the model
47
- # looks at. +offset+/+limit+ are ignored (no line paging). Over
48
- # {MAX_IMAGE_BYTES} → +"Error: image too large…"+ rather than a payload the
49
- # provider rejects; no auto-resize (see +ideas/coding-agent.md+). Vision
46
+ # the binary sniff, answering the one {Pikuri::Tool::Result} in pikuri with
47
+ # +attachments+: a metadata note ("Read image: …") as +content+ for the
48
+ # model to cite, plus the file as an attachment the model looks at.
49
+ # +offset+/+limit+ are ignored (no line paging). Over
50
+ # {Pikuri::Attachment::MAX_IMAGE_BYTES} → +"Error: image too large…"+ rather than a payload the
51
+ # provider rejects; no auto-resize (see +design/ideas/coding-agent.md+). Vision
50
52
  # capability is not checked — a non-vision model's provider error is
51
53
  # something the LLM reacts to, cheaper than tracking model metadata.
52
54
  #
@@ -54,18 +56,17 @@ module Pikuri
54
56
  #
55
57
  # Path outside the workspace ({Filesystem::Error}), not found, EACCES, a
56
58
  # directory (refused, but the refusal carries the directory's own entries —
57
- # see {.directory_refusal}), image over {MAX_IMAGE_BYTES}, binary content
59
+ # see {.directory_refusal}), image over {Pikuri::Attachment::MAX_IMAGE_BYTES}, binary content
58
60
  # (nothing in the registry claims it — the {Pikuri::FileType.binary?}
59
61
  # heuristic catches archives/artifacts with no extension list), offset past
60
62
  # EOF.
61
63
  #
62
64
  # Sharing: +P_one_agent+ — every successful read marks the path on its
63
65
  # {Workspace}, which *is* the read-before-edit gate's state, so a shared
64
- # instance would let one agent's read unlock another's {Edit}. +with_workspace+
65
- # rebinds a copy when a sub-agent needs a different tree.
66
+ # instance would let one agent's read unlock another's {Edit}.
66
67
  class Read < Pikuri::Tool
67
68
  # These alias the +PAGE_*+ constants on {Pikuri::Extractor} (one source of
68
- # truth, shared with +VectorDb::Tools::Read+).
69
+ # truth, shared with +Fileindex::Read+).
69
70
 
70
71
  # @return [Integer] default value of the +limit+ parameter (lines per call).
71
72
  DEFAULT_LIMIT = Pikuri::Extractor::PAGE_DEFAULT_LIMIT
@@ -83,16 +84,8 @@ module Pikuri
83
84
  # prefix).
84
85
  MAX_BYTES = Pikuri::Extractor::PAGE_MAX_BYTES
85
86
 
86
- # @return [String] human-readable {MAX_BYTES} for the continuation marker.
87
- MAX_BYTES_LABEL = "#{MAX_BYTES / 1024} KB"
88
-
89
- # @return [Integer] hard size cap on inline-attached images (Anthropic's
90
- # per-image limit; same order on OpenAI/Gemini). Above it we refuse
91
- # rather than encode a payload the provider would reject.
92
- MAX_IMAGE_BYTES = 5 * 1024 * 1024
93
-
94
- # @return [String] human-readable {MAX_IMAGE_BYTES} for refusal messages.
95
- MAX_IMAGE_BYTES_LABEL = "#{MAX_IMAGE_BYTES / (1024 * 1024)} MB"
87
+ # @return [String] human-readable {Pikuri::Attachment::MAX_IMAGE_BYTES} for refusal messages.
88
+ MAX_IMAGE_BYTES_LABEL = "#{Pikuri::Attachment::MAX_IMAGE_BYTES / (1024 * 1024)} MB"
96
89
 
97
90
  # Description shown to the LLM (opencode-shape). Per-parameter constraints
98
91
  # live in the parameter descriptions.
@@ -142,34 +135,26 @@ module Pikuri
142
135
  )
143
136
  end
144
137
 
145
- # A new {Read} bound to +workspace+. Used by
146
- # {Pikuri::SubAgent::SubAgentTool} when a persona needs a fresh (temp)
147
- # workspace, so paths resolve against the right root and reads land in
148
- # that workspace's ledger.
149
- #
150
- # @param workspace [Workspace]
151
- # @return [Read]
152
- def with_workspace(workspace)
153
- self.class.new(workspace: workspace)
154
- end
155
-
156
138
  # Resolve +path+ against +workspace+, refuse directories/binaries/missing
157
- # files, return the cat-n slice or an +"Error: ..."+ observation.
139
+ # files, return the cat-n slice.
158
140
  #
159
141
  # @param workspace [Workspace]
160
142
  # @param path [String] raw path from the LLM
161
143
  # @param offset [Integer] 1-indexed start line
162
144
  # @param limit [Integer] maximum lines
163
- # @return [String, RubyLLM::Content] text/PDF → +String+; a supported
164
- # image → +RubyLLM::Content+ with the file attached (see "Image
165
- # attachments")
145
+ # @return [Tool::Result] text/PDF → the cat-n slice (+"Read 214 lines"+);
146
+ # a supported image → a note with the file in
147
+ # {Tool::Result#attachments} (see "Image attachments"); else
148
+ # {Pikuri::Tool::Result.error}
166
149
  def self.read(workspace:, path:, offset:, limit:)
167
- return "Error: offset must be >= 1, got #{offset}" if offset < 1
168
- return "Error: limit must be >= 1, got #{limit}" if limit < 1
150
+ return Tool::Result.error("offset must be >= 1, got #{offset}") if offset < 1
151
+ return Tool::Result.error("limit must be >= 1, got #{limit}") if limit < 1
169
152
 
170
153
  resolved = workspace.resolve_for_read(path)
171
- return "Error: file not found: #{path}" unless resolved.exist?
172
- return directory_refusal(workspace: workspace, path: path, resolved: resolved) if resolved.directory?
154
+ return Tool::Result.error(workspace.not_found(path)) unless resolved.exist?
155
+ if resolved.directory?
156
+ return Tool::Result.error(directory_refusal(workspace: workspace, path: path, resolved: resolved))
157
+ end
173
158
 
174
159
  mime = Pikuri::FileType.detect_mime(resolved)
175
160
  return format_image(path: path, resolved: resolved, mime: mime) if mime&.start_with?('image/')
@@ -183,20 +168,20 @@ module Pikuri
183
168
  # the exact-bytes guard is Edit's +old_string+ match; the ledger only
184
169
  # attests "engaged via read".
185
170
  workspace.mark_read(resolved)
186
- render_page(page)
171
+ Tool::Result.page(page)
187
172
  rescue Filesystem::Error => e
188
- "Error: #{e.message}"
173
+ Tool::Result.error(e.message)
189
174
  rescue Errno::EACCES => e
190
- "Error: cannot read #{path}: #{e.message}"
175
+ Tool::Result.error("cannot read #{path}: #{e.message}")
191
176
  rescue ArgumentError
192
177
  # Nothing in the Extractor registry claimed the content —
193
178
  # read_as_text_paged's binary refusal (directories and images
194
179
  # were already handled above).
195
- "Error: cannot read binary file: #{path}"
180
+ Tool::Result.error("cannot read binary file: #{path}")
196
181
  rescue RuntimeError => e
197
182
  # Extraction failure (malformed / unsupported PDF, ...)
198
183
  # surfaced by read_as_text_paged.
199
- "Error: #{e.message}"
184
+ Tool::Result.error(e.message)
200
185
  end
201
186
 
202
187
  # Refuses, but hands back the directory's entries so the model recovers
@@ -217,7 +202,8 @@ module Pikuri
217
202
  # @param workspace [Workspace]
218
203
  # @param path [String] raw path from the LLM (for the message)
219
204
  # @param resolved [Pathname] workspace-resolved path
220
- # @return [String] +"Error: ..."+ carrying the depth-1 listing
205
+ # @return [String] the refusal message, carrying the depth-1 listing —
206
+ # text for {Tool::Result.error} to wrap, not an observation itself
221
207
  def self.directory_refusal(workspace:, path:, resolved:)
222
208
  base = "Error: #{path} is a directory, not a file."
223
209
  listing = Listing.render(resolved, filesystem: workspace.filesystem)
@@ -229,76 +215,33 @@ module Pikuri
229
215
  end
230
216
  private_class_method :directory_refusal
231
217
 
232
- # Render a {Pikuri::Extractor::Page} as the cat-n observation (6-col line
233
- # number, tab, truncated content) plus a trailer telling the model whether
234
- # to page on. PDF pages carry +"--- Page N ---"+ markers from the
235
- # extractor; +kind+ only changes trailer wording.
236
- #
237
- # @param page [Pikuri::Extractor::Page]
238
- # @return [String]
239
- def self.render_page(page)
240
- if page.lines.empty?
241
- return empty_message(page) if page.total_lines.zero?
242
-
243
- return "Error: offset #{page.start_line} is beyond end of file " \
244
- "(#{page.total_lines} lines total)"
245
- end
246
-
247
- noun = page.kind == :pdf ? 'PDF ' : ''
248
- last = page.start_line + page.lines.length - 1
249
- body = page.lines.each_with_index.map { |line, i| format("%6d\t%s", i + page.start_line, line) }.join("\n")
250
-
251
- trailer =
252
- if page.byte_capped
253
- "(Output capped at #{MAX_BYTES_LABEL}. Showing #{noun}lines #{page.start_line}-#{last}. " \
254
- "Use offset=#{last + 1} to continue.)"
255
- elsif page.more
256
- total = page.total_lines ? " of #{page.total_lines}" : ''
257
- "(Showing #{noun}lines #{page.start_line}-#{last}#{total}. " \
258
- "Use offset=#{last + 1} to continue.)"
259
- else
260
- "(End of #{page.kind == :pdf ? 'PDF' : 'file'} - total #{page.total_lines} lines)"
261
- end
262
-
263
- "#{body}\n\n#{trailer}"
264
- end
265
- private_class_method :render_page
266
-
267
- # Empty-document message by kind: a scanned/text-free PDF gets an
268
- # LLM-actionable hint rather than the plain "(Empty file)".
269
- #
270
- # @param page [Pikuri::Extractor::Page]
271
- # @return [String]
272
- def self.empty_message(page)
273
- if page.kind == :pdf
274
- '(PDF has no extractable text; likely scanned image content)'
275
- else
276
- '(Empty file)'
277
- end
278
- end
279
- private_class_method :empty_message
280
-
281
218
  # Build a multimodal observation: a metadata note ("Read image: …") plus
282
219
  # the file attached, both arriving in the same +tool_result+ turn. Over
283
- # {MAX_IMAGE_BYTES} we refuse (an +"Error: ..."+ the model reacts to)
284
- # rather than encoding a payload the provider rejects; +Pathname#size+
285
- # avoids loading bytes just to over-cap them, and +RubyLLM::Attachment+
286
- # base64-encodes lazily at format time.
220
+ # {Pikuri::Attachment::MAX_IMAGE_BYTES} we refuse (an +"Error: ..."+ the model reacts to)
221
+ # rather than encoding a payload the provider rejects. The bytes are read
222
+ # here, once, at most one past the cap — what the model sees is what was
223
+ # checked, and an oversized file is never loaded whole.
224
+ #
225
+ # The one place a {Tool::Result} carries +attachments+: the attached
226
+ # bytes *are* the observation, so the display
227
+ # line is derived from mime and size rather than authored here (see
228
+ # {Pikuri::Tool::Result}'s +== Text or an attachment+).
287
229
  #
288
230
  # @param path [String] raw path from the LLM (for the note)
289
231
  # @param resolved [Pathname] workspace-resolved path
290
232
  # @param mime [String] MIME type
291
- # @return [String, RubyLLM::Content] +RubyLLM::Content+ on success;
292
- # +"Error: ..."+ on size refusal
233
+ # @return [Tool::Result] attachment-carrying on success;
234
+ # {Pikuri::Tool::Result.error} on size refusal
293
235
  def self.format_image(path:, resolved:, mime:)
294
- size = resolved.size
295
- if size > MAX_IMAGE_BYTES
296
- return "Error: image too large to inline: #{path} is " \
297
- "#{size} bytes; max #{MAX_IMAGE_BYTES_LABEL}."
236
+ bytes = File.binread(resolved, Pikuri::Attachment::MAX_IMAGE_BYTES + 1)
237
+ if bytes.bytesize > Pikuri::Attachment::MAX_IMAGE_BYTES
238
+ return Tool::Result.error("image too large to inline: #{path} is " \
239
+ "#{resolved.size} bytes; max #{MAX_IMAGE_BYTES_LABEL}.")
298
240
  end
299
241
 
300
- note = "Read image: #{path} (#{size} bytes, #{mime})"
301
- RubyLLM::Content.new(note, [resolved.to_s])
242
+ image = Pikuri::Attachment.new(mime: mime, filename: resolved.basename.to_s, bytes: bytes)
243
+ note = "Read image: #{path} (#{bytes.bytesize} bytes, #{mime})"
244
+ Tool::Result.new(content: note, attachments: [image], summary: Pikuri::Tool.one_line(note))
302
245
  end
303
246
  private_class_method :format_image
304
247
  end
@@ -61,7 +61,7 @@ module Pikuri
61
61
  #
62
62
  # Sharing: +P_one_agent+ — as {Pikuri::Workspace::FileList}: no state of
63
63
  # its own, but the {Pikuri::Workspace::Workspace} it searches is one
64
- # agent's tree. +with_workspace+ rebinds a copy.
64
+ # agent's tree.
65
65
  class Glob < Pikuri::Tool
66
66
  # @return [Integer] hard byte cap on combined rg output. Same value as
67
67
  # {Grep::MAX_BYTES}; re-declared rather than cross-referenced because
@@ -126,16 +126,6 @@ module Pikuri
126
126
  )
127
127
  end
128
128
 
129
- # A new {Glob} bound to +workspace+. Used by
130
- # {Pikuri::SubAgent::SubAgentTool} when a persona supplies a
131
- # +workspace_factory:+, so paths resolve against the sub-agent's root.
132
- #
133
- # @param workspace [Workspace]
134
- # @return [Glob]
135
- def with_workspace(workspace)
136
- self.class.new(workspace: workspace)
137
- end
138
-
139
129
  # Validate inputs, resolve the path, spawn rg, mtime-sort, head-truncate,
140
130
  # render. Returns the listing, a "no files match" message, or +"Error:
141
131
  # ..."+.
@@ -147,15 +137,15 @@ module Pikuri
147
137
  # +.gitignore+'d files are listed too. Defaults to false.
148
138
  # @return [String]
149
139
  def self.search(workspace:, pattern:, path:, include_gitignored: false)
150
- return 'Error: empty pattern.' if pattern.empty?
140
+ return Tool::Result.error('empty pattern.') if pattern.empty?
151
141
 
152
142
  search_target = '.'
153
143
  resolved_root = workspace.project_root
154
144
  if path
155
145
  resolved = workspace.resolve_for_read(path)
156
- return "Error: path not found: #{path}" unless resolved.exist?
146
+ return Tool::Result.error("path not found: #{path}") unless resolved.exist?
157
147
  if resolved.file?
158
- return "Error: #{path} is a file, not a directory; use the read tool to view it."
148
+ return Tool::Result.error("#{path} is a file, not a directory; use the read tool to view it.")
159
149
  end
160
150
 
161
151
  resolved_root = resolved
@@ -165,7 +155,7 @@ module Pikuri
165
155
 
166
156
  if (refusal = Utils.oversized_root_refusal(resolved_root, filesystem: workspace.filesystem,
167
157
  verb: 'list'))
168
- return refusal
158
+ return Tool::Result.error(refusal)
169
159
  end
170
160
 
171
161
  argv = build_argv(path: search_target, include_gitignored: include_gitignored,
@@ -181,10 +171,10 @@ module Pikuri
181
171
  when 1
182
172
  no_match_message(pattern: pattern, path: path)
183
173
  else
184
- "Error: ripgrep: #{truncate_error(result.output, exit_code)}"
174
+ Tool::Result.error("ripgrep: #{truncate_error(result.output, exit_code)}")
185
175
  end
186
176
  rescue Filesystem::Error => e
187
- "Error: #{e.message}"
177
+ Tool::Result.error(e.message)
188
178
  end
189
179
 
190
180
  # @return [Integer] flags for {File.fnmatch?}: +FNM_PATHNAME+ for
@@ -216,7 +206,7 @@ module Pikuri
216
206
  # mtime-sort descending (path-ascending tiebreaker), head-truncate at
217
207
  # {MAX_BYTES}, append a count footer.
218
208
  #
219
- # @return [String]
209
+ # @return [Tool::Result] the listing, carrying +"Found 12 files"+
220
210
  def self.format_output(raw, workspace:, pattern:, path:)
221
211
  all_paths = raw.split("\n").reject(&:empty?).map { |p| p.sub(%r{\A\./}, '') }
222
212
  paths = all_paths.select { |p| File.fnmatch?(pattern, p, FNMATCH_FLAGS) }
@@ -230,8 +220,9 @@ module Pikuri
230
220
  stripped = content.chomp
231
221
  count = stripped.split("\n").size
232
222
 
233
- footer = "Found #{pluralize(count, 'file', 'files')}."
234
- [stripped, '', footer + truncation_marker].join("\n")
223
+ footer = "Found #{Pikuri::Tool.pluralize(count, 'file', 'files')}."
224
+ Tool::Result.new(content: [stripped, '', footer + truncation_marker].join("\n"),
225
+ summary: "Found #{Pikuri::Tool.pluralize(count, 'file', 'files')}")
235
226
  end
236
227
  private_class_method :format_output
237
228
 
@@ -283,20 +274,15 @@ module Pikuri
283
274
  end
284
275
  private_class_method :truncate_error
285
276
 
286
- # @return [String]
277
+ # @return [Tool::Result] the "nothing matched" observation, carrying
278
+ # +"Found 0 files"+
287
279
  def self.no_match_message(pattern:, path:)
288
280
  base = "No files match pattern '#{pattern}'"
289
281
  base += " in #{path}" if path
290
- "#{base}."
282
+ Tool::Result.new(content: "#{base}.", summary: 'Found 0 files')
291
283
  end
292
284
  private_class_method :no_match_message
293
285
 
294
- # @return [String] +"1 file"+ / +"2 files"+
295
- def self.pluralize(n, sing, plural)
296
- "#{n} #{n == 1 ? sing : plural}"
297
- end
298
- private_class_method :pluralize
299
-
300
286
  # Verify +rg+ is on +PATH+ (via {Pikuri::Subprocess.spawn}, honoring the
301
287
  # subprocess seam). Missing → +Errno::ENOENT+; installed → exit 0.
302
288
  #
@@ -52,7 +52,7 @@ module Pikuri
52
52
  # == Truncation / exit codes
53
53
  #
54
54
  # Head-truncated to {MAX_BYTES} (head-only — grep tails carry less signal;
55
- # opposite bias from {Pikuri::Code::Bash}), cut at a line boundary with a
55
+ # opposite bias from a shell tool, whose tail holds the error), cut at a line boundary with a
56
56
  # marker. The exit-2 error blob shares the ceiling ({.truncate_error} — rg
57
57
  # emits one diagnostic per unreadable path). Exit +0+ → results with footer;
58
58
  # +1+ → "No matches"; +2+ → +"Error: ripgrep: ..."+.
@@ -148,16 +148,6 @@ module Pikuri
148
148
  )
149
149
  end
150
150
 
151
- # A new {Grep} bound to +workspace+. Used by
152
- # {Pikuri::SubAgent::SubAgentTool} when a persona supplies a
153
- # +workspace_factory:+, so paths resolve against the sub-agent's root.
154
- #
155
- # @param workspace [Workspace]
156
- # @return [Grep]
157
- def with_workspace(workspace)
158
- self.class.new(workspace: workspace)
159
- end
160
-
161
151
  # Validate inputs, resolve the path, spawn rg, render the observation.
162
152
  # Returns the results, a "no matches" string, or +"Error: ..."+.
163
153
  #
@@ -172,17 +162,17 @@ module Pikuri
172
162
  # @return [String]
173
163
  def self.search(workspace:, pattern:, path:, glob:, case_insensitive:, output_mode:,
174
164
  include_gitignored: false)
175
- return 'Error: empty pattern.' if pattern.empty?
165
+ return Tool::Result.error('empty pattern.') if pattern.empty?
176
166
  unless OUTPUT_MODES.include?(output_mode)
177
- return "Error: output_mode must be one of #{OUTPUT_MODES.join(', ')}, " \
178
- "got #{output_mode.inspect}."
167
+ return Tool::Result.error("output_mode must be one of #{OUTPUT_MODES.join(', ')}, " \
168
+ "got #{output_mode.inspect}.")
179
169
  end
180
170
 
181
171
  search_target = '.'
182
172
  resolved_root = workspace.project_root
183
173
  if path
184
174
  resolved = workspace.resolve_for_read(path)
185
- return "Error: path not found: #{path}" unless resolved.exist?
175
+ return Tool::Result.error("path not found: #{path}") unless resolved.exist?
186
176
 
187
177
  resolved_root = resolved
188
178
  rel = resolved.relative_path_from(workspace.project_root).to_s
@@ -191,7 +181,7 @@ module Pikuri
191
181
 
192
182
  if (refusal = Utils.oversized_root_refusal(resolved_root, filesystem: workspace.filesystem,
193
183
  verb: 'search'))
194
- return refusal
184
+ return Tool::Result.error(refusal)
195
185
  end
196
186
 
197
187
  denied_prefixes = Utils.denied_prefixes(filesystem: workspace.filesystem,
@@ -213,10 +203,10 @@ module Pikuri
213
203
  when 1
214
204
  no_match_message(pattern: pattern, path: path)
215
205
  else
216
- "Error: ripgrep: #{truncate_error(result.output, exit_code)}"
206
+ Tool::Result.error("ripgrep: #{truncate_error(result.output, exit_code)}")
217
207
  end
218
208
  rescue Filesystem::Error => e
219
- "Error: #{e.message}"
209
+ Tool::Result.error(e.message)
220
210
  end
221
211
 
222
212
  # Build the +rg+ argv. Path is always passed (defaults to +.+) — see the
@@ -252,7 +242,8 @@ module Pikuri
252
242
  # Render rg output: strip the +./+ prefix, head-truncate at {MAX_BYTES},
253
243
  # append a count footer.
254
244
  #
255
- # @return [String]
245
+ # @return [Tool::Result] the hits, carrying +"Found 7 matches in 3
246
+ # files"+
256
247
  def self.format_output(raw, output_mode:, pattern:, path:, denied_prefixes:)
257
248
  # The guarantee, after build_argv's pruning did the cheap 99%. Runs on
258
249
  # ./-stripped text so the prefixes are workspace-relative like rg's.
@@ -262,8 +253,9 @@ module Pikuri
262
253
 
263
254
  return no_match_message(pattern: pattern, path: path) if stripped.empty?
264
255
 
265
- footer = build_footer(stripped, output_mode)
266
- [stripped, '', footer + truncation_marker].join("\n")
256
+ phrase = match_phrase(stripped, output_mode)
257
+ Tool::Result.new(content: [stripped, '', "Found #{phrase}." + truncation_marker].join("\n"),
258
+ summary: "Found #{phrase}")
267
259
  end
268
260
  private_class_method :format_output
269
261
 
@@ -303,37 +295,34 @@ module Pikuri
303
295
  end
304
296
  private_class_method :truncate_error
305
297
 
306
- # One-line footer summarizing the result; format depends on +output_mode+.
298
+ # The counted phrase the observation's footer and the display summary are
299
+ # both built from — +"7 matches in 3 files"+, +"3 files"+ in
300
+ # +files_with_matches+ mode.
307
301
  #
308
302
  # @return [String]
309
- def self.build_footer(content, output_mode)
303
+ def self.match_phrase(content, output_mode)
310
304
  lines = content.split("\n").reject(&:empty?)
311
305
  case output_mode
312
306
  when 'content'
313
307
  files = lines.map { |l| l.split(':', 2).first }.uniq
314
- "Found #{pluralize(lines.size, 'match', 'matches')} in " \
315
- "#{pluralize(files.size, 'file', 'files')}."
308
+ "#{Pikuri::Tool.pluralize(lines.size, 'match', 'matches')} in " \
309
+ "#{Pikuri::Tool.pluralize(files.size, 'file', 'files')}"
316
310
  when 'files_with_matches'
317
- "Found #{pluralize(lines.size, 'file', 'files')}."
311
+ Pikuri::Tool.pluralize(lines.size, 'file', 'files')
318
312
  when 'count'
319
313
  total = lines.sum { |l| Integer(l.split(':').last) }
320
- "Found #{pluralize(total, 'match', 'matches')} in " \
321
- "#{pluralize(lines.size, 'file', 'files')}."
314
+ "#{Pikuri::Tool.pluralize(total, 'match', 'matches')} in " \
315
+ "#{Pikuri::Tool.pluralize(lines.size, 'file', 'files')}"
322
316
  end
323
317
  end
324
- private_class_method :build_footer
325
-
326
- # @return [String] +"1 match"+ / +"2 matches"+
327
- def self.pluralize(n, sing, plural)
328
- "#{n} #{n == 1 ? sing : plural}"
329
- end
330
- private_class_method :pluralize
318
+ private_class_method :match_phrase
331
319
 
332
- # @return [String]
320
+ # @return [Tool::Result] the "nothing matched" observation, carrying
321
+ # +"Found 0 matches"+
333
322
  def self.no_match_message(pattern:, path:)
334
323
  base = "No matches for pattern '#{pattern}'"
335
324
  base += " in #{path}" if path
336
- "#{base}."
325
+ Tool::Result.new(content: "#{base}.", summary: 'Found 0 matches')
337
326
  end
338
327
  private_class_method :no_match_message
339
328
 
@@ -8,8 +8,8 @@ module Pikuri
8
8
  module Utils
9
9
  # Guard against a search/list rooted at +/+, the user's whole home dir,
10
10
  # or any parent of home (+/home+, which holds *every* user's home) —
11
- # each walks an enormous tree and, under an +AllowAll+ workspace, floods
12
- # rg with unreadable-path diagnostics the model pays context for. The
11
+ # each walks an enormous tree and, under a +readable:+ root that broad,
12
+ # floods rg with unreadable-path diagnostics the model pays context for. The
13
13
  # refused set is home and everything at-or-above it: blocking +~+ while
14
14
  # waving through its strictly-broader parent +/home+ would be no guard
15
15
  # at all. Refused regardless of any glob: rg traverses the tree *before*
@@ -37,8 +37,8 @@ module Pikuri
37
37
  # owns its own record, so a read in one (a sub-agent's temp clone) never
38
38
  # satisfies an edit in another.
39
39
  #
40
- # Sharing: +P_one_agent+ — the read record is one conversation's, and
41
- # {#clear_reads} is tied to that conversation's reset. Two agents on one
40
+ # Sharing: +P_one_agent+ — the read record is one conversation's, and an
41
+ # agent is one conversation (+D_agent_is_one_conversation+). Two agents on one
42
42
  # composite would satisfy each other's read-before-edit gate: agent B could
43
43
  # {Edit} a file only agent A had read, which is the gate failing open.
44
44
  # {Filesystem} underneath is immutable policy and shares freely; give each
@@ -75,15 +75,18 @@ module Pikuri
75
75
  @read.include?(relativize(resolved))
76
76
  end
77
77
 
78
- # Forget every path read so far, returning the read-before-edit gate to
79
- # empty. Called by {Extension#on_conversation_reset}: a cleared
80
- # conversation has seen nothing, so the next {Edit}/overwrite-{Write} needs
81
- # a fresh {Read}, like a brand-new agent.
78
+ # The error text for a path that doesn't exist, naming the root that
79
+ # relative paths resolve against. A model holding a path from another
80
+ # tool — a corpus citation relative to some other folder, say — can't
81
+ # otherwise tell why it missed, and this shows it what to prepend:
82
82
  #
83
- # @return [void]
84
- def clear_reads
85
- @read.clear
86
- nil
83
+ # workspace.not_found('xyz/spec.txt')
84
+ # # => "file not found: xyz/spec.txt (relative paths resolve against /home/alice/Notes)"
85
+ #
86
+ # @param path [String] the path as the model gave it.
87
+ # @return [String]
88
+ def not_found(path)
89
+ "file not found: #{path} (relative paths resolve against #{project_root})"
87
90
  end
88
91
 
89
92
  private