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.
- checksums.yaml +4 -4
- data/README.md +16 -13
- data/lib/pikuri/workspace/confirmer/terminal.rb +39 -32
- data/lib/pikuri/workspace/confirmer.rb +84 -40
- data/lib/pikuri/workspace/edit.rb +144 -60
- data/lib/pikuri/workspace/extension.rb +27 -52
- data/lib/pikuri/workspace/file_list.rb +21 -18
- data/lib/pikuri/workspace/filesystem.rb +78 -336
- data/lib/pikuri/workspace/listing.rb +20 -10
- data/lib/pikuri/workspace/read.rb +50 -107
- data/lib/pikuri/workspace/search/glob.rb +14 -28
- data/lib/pikuri/workspace/search/grep.rb +26 -37
- data/lib/pikuri/workspace/search/utils.rb +2 -2
- data/lib/pikuri/workspace/workspace.rb +13 -10
- data/lib/pikuri/workspace/write.rb +55 -46
- data/lib/pikuri/workspace/write_gate.rb +27 -25
- data/lib/pikuri-workspace.rb +4 -1
- data/prompts/pikuri-files.txt +6 -0
- metadata +9 -10
- data/lib/pikuri/workspace/project_root.rb +0 -77
- data/lib/pikuri/workspace/read_only.rb +0 -134
|
@@ -2,25 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
module Pikuri
|
|
4
4
|
module Workspace
|
|
5
|
-
# The +edit+ tool — exact-string replacement on an existing file
|
|
6
|
-
# +Edit.new(workspace: ws)+ produces a tool wired like any bundled tool's.
|
|
7
|
-
# In its default (promptless) form it takes the lone +workspace:+ like
|
|
8
|
-
# {Read}; an optional +confirmer:+ turns on diff-confirmation.
|
|
5
|
+
# The +edit+ tool — exact-string replacement on an existing file:
|
|
9
6
|
#
|
|
10
|
-
#
|
|
7
|
+
# Edit.new(workspace: ws, confirmer: c) # every edit asks, with a diff
|
|
8
|
+
# Edit.new(workspace: ws, confirmer: c, confirm_all_writes: false) # no prompt
|
|
11
9
|
#
|
|
12
|
-
#
|
|
13
|
-
# guards bound its blast radius. First, the read-before-edit gate: +.edit+
|
|
14
|
-
# refuses a path the {Workspace}'s read record doesn't show read this
|
|
15
|
-
# conversation. Second, +old_string+ is itself an implicit read-check — the
|
|
16
|
-
# model can't supply correct bytes it hasn't seen — so even within a read
|
|
17
|
-
# file the change is pinned to content the model has. ({Write} differs — it
|
|
18
|
-
# always takes a confirmer; see +pikuri-workspace/DESIGN.md+.)
|
|
10
|
+
# == Confirm every edit, or opt out
|
|
19
11
|
#
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
12
|
+
# By default every writable edit confirms with a diff via {WriteGate}, and
|
|
13
|
+
# an unwritable target short-circuits to "ask the user to sudo". Opting
|
|
14
|
+
# out is safe because two guards bound the blast radius. First, the
|
|
15
|
+
# read-before-edit gate: +.edit+ refuses a path the {Workspace}'s read
|
|
16
|
+
# record doesn't show read this conversation. Second, +old_string+ is
|
|
17
|
+
# itself an implicit read-check — the model can't supply correct bytes it
|
|
18
|
+
# hasn't seen — so the change is pinned to content the model has. ({Write}
|
|
19
|
+
# lacks the second guard, so it confirms an overwrite either way; see
|
|
20
|
+
# +pikuri-workspace/design/architecture.md+.)
|
|
24
21
|
#
|
|
25
22
|
# == Matching is strict (no fuzz cascade)
|
|
26
23
|
#
|
|
@@ -49,10 +46,25 @@ module Pikuri
|
|
|
49
46
|
# if CRLF. Caveat: a mixed-ending file is treated as CRLF, so bare-LF lines
|
|
50
47
|
# get converted — rare, acceptable for v1.
|
|
51
48
|
#
|
|
49
|
+
# == Typographic lookalikes are named, never fixed
|
|
50
|
+
#
|
|
51
|
+
# Models fold +’+ to +'+ (and back) when copying, then re-read and resend
|
|
52
|
+
# the same bytes. So a zero-match that matches once {CONFUSABLES} are
|
|
53
|
+
# folded to ASCII says where:
|
|
54
|
+
#
|
|
55
|
+
# Error: old_string not found in notes.md, but it matches once typographic
|
|
56
|
+
# characters are folded to ASCII: line 3 has ’ (U+2019) where old_string
|
|
57
|
+
# has ' (U+0027). Copy these characters exactly as the file has them.
|
|
58
|
+
#
|
|
59
|
+
# It never edits through the fold the way CRLF does: lookalikes are
|
|
60
|
+
# sprinkled, not file-wide, so writing back a normalized file would
|
|
61
|
+
# rewrite every smart quote in it behind a "1 occurrence" confirmation.
|
|
62
|
+
#
|
|
52
63
|
# == Refusals (all as +"Error: ..."+)
|
|
53
64
|
#
|
|
54
65
|
# Empty +old_string+ ("use the write tool") · +old_string+ == +new_string+
|
|
55
|
-
# (no-op) · not found ("must match exactly"
|
|
66
|
+
# (no-op) · not found ("must match exactly", or the typographic
|
|
67
|
+
# characters that broke the match) · multiple matches without
|
|
56
68
|
# +replace_all+ or +occurrence+ · +occurrence+ combined with +replace_all+ ·
|
|
57
69
|
# +occurrence+ below 1 or past the match count · missing / directory /
|
|
58
70
|
# binary file · not read this conversation (the gate, after the binary
|
|
@@ -60,7 +72,7 @@ module Pikuri
|
|
|
60
72
|
#
|
|
61
73
|
# Sharing: +P_one_agent+ — the "not read this conversation" refusal reads
|
|
62
74
|
# one {Workspace}'s record, so sharing an instance widens *whose* read
|
|
63
|
-
# counts.
|
|
75
|
+
# counts.
|
|
64
76
|
class Edit < Pikuri::Tool
|
|
65
77
|
# Description shown to the LLM (opencode-shape). Per-parameter constraints
|
|
66
78
|
# live in the parameter descriptions.
|
|
@@ -82,15 +94,13 @@ module Pikuri
|
|
|
82
94
|
# @param workspace [Workspace] captured for path resolution and the
|
|
83
95
|
# read-before-edit gate; edits route through +resolve_for_write+ (Edit
|
|
84
96
|
# modifies, so the write-set) and the gate consults +read?+.
|
|
85
|
-
# @param
|
|
86
|
-
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
89
|
-
# confirm-all-writes posture (every writable edit confirms with a diff,
|
|
90
|
-
# unwritable targets short-circuit). +nil+ (default) keeps Edit
|
|
91
|
-
# promptless (see "Confirmer is optional").
|
|
97
|
+
# @param confirmer [Confirmer] asked before each edit, unless
|
|
98
|
+
# +confirm_all_writes+ is +false+.
|
|
99
|
+
# @param confirm_all_writes [Boolean] +false+ makes Edit promptless (see
|
|
100
|
+
# "Confirm every edit, or opt out").
|
|
92
101
|
# @return [Edit]
|
|
93
|
-
def initialize(workspace:,
|
|
102
|
+
def initialize(workspace:, confirmer:, confirm_all_writes: true)
|
|
103
|
+
gate = confirm_all_writes ? confirmer : nil
|
|
94
104
|
super(
|
|
95
105
|
name: 'edit',
|
|
96
106
|
description: DESCRIPTION,
|
|
@@ -118,10 +128,12 @@ module Pikuri
|
|
|
118
128
|
'context when the file repeats the same ' \
|
|
119
129
|
'snippet. Cannot be combined with replace_all.'
|
|
120
130
|
},
|
|
121
|
-
|
|
122
|
-
|
|
131
|
+
wants_context: true,
|
|
132
|
+
execute: ->(path:, old_string:, new_string:, context:, replace_all: false, occurrence: nil) {
|
|
133
|
+
Edit.edit(workspace: workspace, confirmer: gate,
|
|
123
134
|
path: path, old_string: old_string, new_string: new_string,
|
|
124
|
-
replace_all: replace_all, occurrence: occurrence
|
|
135
|
+
replace_all: replace_all, occurrence: occurrence,
|
|
136
|
+
cancellable: context.cancellable)
|
|
125
137
|
},
|
|
126
138
|
# No legs, for the same reason as {Write}: the observation reports the
|
|
127
139
|
# outcome, never the file's bytes, so nothing enters the model's
|
|
@@ -145,32 +157,38 @@ module Pikuri
|
|
|
145
157
|
# @param occurrence [Integer, nil] 1-based index of the single match to
|
|
146
158
|
# replace, lifting the uniqueness requirement; +nil+ (default) keeps
|
|
147
159
|
# it. Mutually exclusive with +replace_all+.
|
|
148
|
-
# @param
|
|
149
|
-
#
|
|
150
|
-
# @param
|
|
151
|
-
#
|
|
160
|
+
# @param confirmer [Confirmer, nil] asked before the write; +nil+ writes
|
|
161
|
+
# with no prompt (the opted-out posture).
|
|
162
|
+
# @param cancellable [Pikuri::Agent::Control::Cancellable] the calling
|
|
163
|
+
# turn's token, for the confirmation.
|
|
164
|
+
# @return [Tool::Result] +"Added 2 lines, removed 3 lines"+ once the edit
|
|
165
|
+
# lands; {Pikuri::Tool::Result.error} for a refusal or the confirmer's
|
|
166
|
+
# decline. The counts are exact without a differ: the replaced text and
|
|
167
|
+
# its substitute are both arguments.
|
|
152
168
|
def self.edit(workspace:, path:, old_string:, new_string:, replace_all:,
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
169
|
+
cancellable:, confirmer:, occurrence: nil)
|
|
170
|
+
if old_string.empty?
|
|
171
|
+
return Tool::Result.error('old_string is empty; use the write tool to create or overwrite a file.')
|
|
172
|
+
end
|
|
173
|
+
if old_string == new_string
|
|
174
|
+
return Tool::Result.error('old_string and new_string are identical — this edit is a no-op.')
|
|
175
|
+
end
|
|
158
176
|
|
|
159
177
|
if occurrence && replace_all
|
|
160
|
-
return '
|
|
161
|
-
|
|
178
|
+
return Tool::Result.error('occurrence and replace_all are mutually exclusive; ' \
|
|
179
|
+
'drop one — occurrence picks a single match, replace_all takes them all.')
|
|
162
180
|
end
|
|
163
|
-
return "
|
|
181
|
+
return Tool::Result.error("occurrence must be 1 or greater, got #{occurrence}.") if occurrence && occurrence < 1
|
|
164
182
|
|
|
165
183
|
resolved = workspace.resolve_for_write(path)
|
|
166
|
-
return
|
|
167
|
-
return "
|
|
184
|
+
return Tool::Result.error(workspace.not_found(path)) unless resolved.exist?
|
|
185
|
+
return Tool::Result.error("#{path} is a directory") if resolved.directory?
|
|
168
186
|
|
|
169
|
-
return "
|
|
187
|
+
return Tool::Result.error("cannot edit binary file: #{path}") if Pikuri::FileType.binary?(resolved)
|
|
170
188
|
|
|
171
189
|
unless workspace.read?(resolved)
|
|
172
|
-
return "
|
|
173
|
-
|
|
190
|
+
return Tool::Result.error("#{path} has not been read this conversation; use the read tool " \
|
|
191
|
+
'first so the edit applies to the current bytes.')
|
|
174
192
|
end
|
|
175
193
|
|
|
176
194
|
raw = resolved.binread
|
|
@@ -182,19 +200,24 @@ module Pikuri
|
|
|
182
200
|
|
|
183
201
|
occurrences = content.scan(needle).size
|
|
184
202
|
if occurrences.zero?
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
203
|
+
if (culprits = confusable_mismatches(content, needle))
|
|
204
|
+
return Tool::Result.error("old_string not found in #{path}, but it matches once typographic " \
|
|
205
|
+
"characters are folded to ASCII: #{culprits}. Copy these characters " \
|
|
206
|
+
'exactly as the file has them.')
|
|
207
|
+
end
|
|
208
|
+
return Tool::Result.error("old_string not found in #{path}. It must match the file " \
|
|
209
|
+
'exactly, including whitespace and indentation; re-read with the ' \
|
|
210
|
+
'read tool if uncertain.')
|
|
188
211
|
end
|
|
189
212
|
if occurrence && occurrence > occurrences
|
|
190
|
-
return "
|
|
191
|
-
|
|
213
|
+
return Tool::Result.error("old_string matches #{occurrences} time#{occurrences == 1 ? '' : 's'} " \
|
|
214
|
+
"in #{path}, so occurrence #{occurrence} does not exist.")
|
|
192
215
|
end
|
|
193
216
|
if occurrences > 1 && !replace_all && !occurrence
|
|
194
|
-
return "
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
217
|
+
return Tool::Result.error("old_string matches #{occurrences} times in #{path}. " \
|
|
218
|
+
'Set occurrence=N to replace just the Nth, set replace_all=true ' \
|
|
219
|
+
'to replace all of them, or provide more surrounding context to ' \
|
|
220
|
+
'make the match unique.')
|
|
198
221
|
end
|
|
199
222
|
|
|
200
223
|
new_content =
|
|
@@ -209,11 +232,11 @@ module Pikuri
|
|
|
209
232
|
|
|
210
233
|
final = crlf ? new_content.gsub("\n", "\r\n") : new_content
|
|
211
234
|
|
|
235
|
+
replaced = replace_all ? occurrences : 1
|
|
212
236
|
detail =
|
|
213
237
|
if occurrence
|
|
214
238
|
"occurrence #{occurrence} of #{occurrences}"
|
|
215
239
|
else
|
|
216
|
-
replaced = replace_all ? occurrences : 1
|
|
217
240
|
"#{replaced} occurrence#{replaced == 1 ? '' : 's'}"
|
|
218
241
|
end
|
|
219
242
|
|
|
@@ -221,7 +244,8 @@ module Pikuri
|
|
|
221
244
|
gate = WriteGate.check(
|
|
222
245
|
confirmer: confirmer, path: path, resolved: resolved,
|
|
223
246
|
question: "OK to edit #{path} (#{detail})?",
|
|
224
|
-
change: Confirmer::Change.new(path: path, old: raw, new: final)
|
|
247
|
+
change: Confirmer::Change.new(path: path, old: raw, new: final),
|
|
248
|
+
cancellable: cancellable
|
|
225
249
|
)
|
|
226
250
|
return gate if gate
|
|
227
251
|
end
|
|
@@ -232,11 +256,15 @@ module Pikuri
|
|
|
232
256
|
# follow-up edit needn't re-read. Idempotent — the read? gate passed.
|
|
233
257
|
workspace.mark_read(resolved)
|
|
234
258
|
|
|
235
|
-
|
|
259
|
+
Tool::Result.new(
|
|
260
|
+
content: "Edited #{path}: replaced #{detail}.",
|
|
261
|
+
summary: "Added #{Pikuri::Tool.pluralize(patch.lines.size * replaced, 'line')}, " \
|
|
262
|
+
"removed #{Pikuri::Tool.pluralize(needle.lines.size * replaced, 'line')}"
|
|
263
|
+
)
|
|
236
264
|
rescue Filesystem::Error => e
|
|
237
|
-
|
|
265
|
+
Tool::Result.error(e.message)
|
|
238
266
|
rescue Errno::EACCES => e
|
|
239
|
-
"
|
|
267
|
+
Tool::Result.error("cannot edit #{path}: #{e.message}")
|
|
240
268
|
end
|
|
241
269
|
|
|
242
270
|
# Force a String to BINARY and collapse +\r\n+ → +\n+ so matching/
|
|
@@ -267,6 +295,62 @@ module Pikuri
|
|
|
267
295
|
idx
|
|
268
296
|
end
|
|
269
297
|
private_class_method :nth_index
|
|
298
|
+
|
|
299
|
+
# Typographic characters a model folds to ASCII (or the reverse) when
|
|
300
|
+
# copying text, each mapped to the ASCII it is mistaken for.
|
|
301
|
+
#
|
|
302
|
+
# Narrow on purpose: only near-always-accidental lookalikes, so +−+
|
|
303
|
+
# (U+2212) and +×+ (U+00D7), which carry meaning, stay out. Not
|
|
304
|
+
# {Pikuri::Sanitizer}'s set, which is about deception, not accident.
|
|
305
|
+
# One char to one char, so +…+ ↔ +...+ is out: it would break the
|
|
306
|
+
# index alignment {.confusable_mismatches} relies on.
|
|
307
|
+
#
|
|
308
|
+
# @return [Hash{String => String}]
|
|
309
|
+
CONFUSABLES = {
|
|
310
|
+
"‘" => "'", "’" => "'", # ‘ ’
|
|
311
|
+
"“" => '"', "”" => '"', # “ ”
|
|
312
|
+
"–" => '-', "—" => '-', # – —
|
|
313
|
+
" " => ' ', " " => ' ' # no-break spaces
|
|
314
|
+
}.freeze
|
|
315
|
+
|
|
316
|
+
# Where +needle+ fails to match +haystack+ only on {CONFUSABLES}.
|
|
317
|
+
#
|
|
318
|
+
# confusable_mismatches("say ’hi’\n".b, "say 'hi’".b)
|
|
319
|
+
# # => "line 1 has ’ (U+2019) where old_string has ' (U+0027)"
|
|
320
|
+
#
|
|
321
|
+
# Diagnoses the first folded match, listing at most three differing
|
|
322
|
+
# characters.
|
|
323
|
+
#
|
|
324
|
+
# @param haystack [String] BINARY-encoded, LF-normalized file content
|
|
325
|
+
# @param needle [String] BINARY-encoded, LF-normalized +old_string+
|
|
326
|
+
# @return [String, nil] the culprits joined by +"; "+, or +nil+ when the
|
|
327
|
+
# folded strings don't match either, or either side isn't valid UTF-8
|
|
328
|
+
def self.confusable_mismatches(haystack, needle)
|
|
329
|
+
file = haystack.dup.force_encoding(Encoding::UTF_8)
|
|
330
|
+
sent = needle.dup.force_encoding(Encoding::UTF_8)
|
|
331
|
+
return nil unless file.valid_encoding? && sent.valid_encoding?
|
|
332
|
+
|
|
333
|
+
pattern = Regexp.union(CONFUSABLES.keys)
|
|
334
|
+
start = file.gsub(pattern, CONFUSABLES).index(sent.gsub(pattern, CONFUSABLES))
|
|
335
|
+
return nil unless start
|
|
336
|
+
|
|
337
|
+
culprits = sent.each_char.with_index.filter_map do |want, i|
|
|
338
|
+
have = file[start + i]
|
|
339
|
+
next if have == want
|
|
340
|
+
|
|
341
|
+
line = file[0, start + i].count("\n") + 1
|
|
342
|
+
"line #{line} has #{have} (#{codepoint(have)}) where old_string has #{want} (#{codepoint(want)})"
|
|
343
|
+
end
|
|
344
|
+
culprits.uniq.first(3).join('; ')
|
|
345
|
+
end
|
|
346
|
+
private_class_method :confusable_mismatches
|
|
347
|
+
|
|
348
|
+
# @param char [String] one character
|
|
349
|
+
# @return [String] e.g. +"U+2019"+
|
|
350
|
+
def self.codepoint(char)
|
|
351
|
+
format('U+%04X', char.ord)
|
|
352
|
+
end
|
|
353
|
+
private_class_method :codepoint
|
|
270
354
|
end
|
|
271
355
|
end
|
|
272
356
|
end
|
|
@@ -7,13 +7,12 @@ module Pikuri
|
|
|
7
7
|
# {Workspace} composite — which is what makes the read-before-edit gate
|
|
8
8
|
# work: {Read} marks a path read in the same record {Edit} and
|
|
9
9
|
# overwrite-{Write} consult. One registration point removes the per-host
|
|
10
|
-
# +add_tool+ boilerplate.
|
|
11
|
-
#
|
|
12
|
-
# composite and its read record are internal.
|
|
10
|
+
# +add_tool+ boilerplate. The host builds the {Filesystem} and {Confirmer}
|
|
11
|
+
# and hands them over; the composite and its read record are internal.
|
|
13
12
|
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
13
|
+
# A shell tool is *not* here — it isn't a read/edit/write tool, and a host
|
|
14
|
+
# wiring one (pikuri-workshop's coding agent does) hands it the raw
|
|
15
|
+
# {Filesystem}, since it governs a subprocess's fs view, not the read record.
|
|
17
16
|
#
|
|
18
17
|
# Contributes {PROMPT_SNIPPET} — the "a decline is final" confirmation
|
|
19
18
|
# guidance — so it rides the wiring of the mutating tools instead of
|
|
@@ -39,18 +38,10 @@ module Pikuri
|
|
|
39
38
|
# +[N dirs, M files]+ column already does depth 2's job on the wide-flat
|
|
40
39
|
# directories where depth 2 costs most — a blog's +_posts/ [0 dirs, 256
|
|
41
40
|
# files]+ is 30 bytes against 24 KB expanded. Measurements and the rejected
|
|
42
|
-
# ladder live in +
|
|
41
|
+
# ladder live in +design/decisions.md+ +D_directory_listing+.
|
|
43
42
|
#
|
|
44
|
-
#
|
|
45
|
-
#
|
|
46
|
-
#
|
|
47
|
-
# == Read-only gate
|
|
48
|
-
#
|
|
49
|
-
# A {ReadOnly} flag (when the host passes one) is threaded into {Edit}/{Write}
|
|
50
|
-
# so they refuse while active — the read-only tools take no gate. The host
|
|
51
|
-
# owns the flag and hands the *same* instance to whatever drives it
|
|
52
|
-
# (pikuri-code's plan mode). No flag → gate off. Covers Write/Edit only,
|
|
53
|
-
# never Bash — see {ReadOnly}.
|
|
43
|
+
# Taken once per agent, so a "/clear" — a new agent — refreshes it and
|
|
44
|
+
# nothing else does.
|
|
54
45
|
#
|
|
55
46
|
# == Usage
|
|
56
47
|
#
|
|
@@ -69,12 +60,13 @@ module Pikuri
|
|
|
69
60
|
|
|
70
61
|
# @return [String] system-prompt guidance wired alongside the mutating
|
|
71
62
|
# tools: the confirmer may pause a write/edit (and a co-wired Bash)
|
|
72
|
-
# for the user's OK
|
|
63
|
+
# for the user's OK; a plain decline is final, a decline with words is
|
|
64
|
+
# guidance to follow. Contributed as a snippet
|
|
73
65
|
# so it tracks the wiring rather than each host prompt (the Os/Code
|
|
74
66
|
# precedent), and emitted unconditionally — an auto-approve ("yolo")
|
|
75
67
|
# confirmer is the rare exception, and the guidance is harmless there.
|
|
76
68
|
PROMPT_SNIPPET = <<~SNIPPET.chomp
|
|
77
|
-
A tool may ask
|
|
69
|
+
A tool may ask the user to confirm before it makes a change. If they decline, stop: never reach the same end another way (a refused file write redone through a shell command, say) — tell them you stopped and wait for their guidance. If they decline and say something, follow what they said; a new proposal along those lines is fine, and they will be asked again.
|
|
78
70
|
SNIPPET
|
|
79
71
|
|
|
80
72
|
# @return [Integer] +depth+ the boot snapshot renders at. Deliberately
|
|
@@ -96,20 +88,21 @@ module Pikuri
|
|
|
96
88
|
|
|
97
89
|
# @param filesystem [Filesystem] path resolution / sandboxing; wrapped in a
|
|
98
90
|
# fresh {Workspace} (empty read record).
|
|
99
|
-
# @param confirmer [Confirmer] consulted by {Write} before
|
|
100
|
-
# @param
|
|
101
|
-
#
|
|
102
|
-
#
|
|
103
|
-
#
|
|
104
|
-
#
|
|
105
|
-
#
|
|
106
|
-
#
|
|
107
|
-
#
|
|
108
|
-
|
|
91
|
+
# @param confirmer [Confirmer] consulted by {Edit} and {Write} before a change.
|
|
92
|
+
# @param confirm_all_writes [Boolean] the host's write posture: +true+
|
|
93
|
+
# (default) confirms every writable write and edit with a diff; +false+
|
|
94
|
+
# leaves {Edit} promptless and {Write} asking only before an overwrite.
|
|
95
|
+
# See book/touching-files.md.
|
|
96
|
+
# @param grep [Boolean] +false+ leaves {Search::Grep} unwired, for a
|
|
97
|
+
# host whose root a search index also covers: grep sees a PDF's raw
|
|
98
|
+
# bytes and skips it, so on a documents folder it answers "no match"
|
|
99
|
+
# where the index's search finds the invoice. Narrows only; see
|
|
100
|
+
# +design/decisions.md+ +D_secretary_wiring+.
|
|
101
|
+
def initialize(filesystem:, confirmer:, confirm_all_writes: true, grep: true)
|
|
109
102
|
@workspace = Workspace.new(filesystem: filesystem)
|
|
110
103
|
@confirmer = confirmer
|
|
111
|
-
@read_only = read_only
|
|
112
104
|
@confirm_all_writes = confirm_all_writes
|
|
105
|
+
@grep = grep
|
|
113
106
|
end
|
|
114
107
|
|
|
115
108
|
# @param c [Pikuri::Agent::Configurator]
|
|
@@ -123,32 +116,14 @@ module Pikuri
|
|
|
123
116
|
end
|
|
124
117
|
end
|
|
125
118
|
|
|
126
|
-
# The read-only flag (when supplied) is threaded into the two mutators;
|
|
127
|
-
# the read-only tools take no gate. +nil+ disables it.
|
|
128
119
|
c.add_tool Read.new(workspace: @workspace)
|
|
129
|
-
c.add_tool Search::Grep.new(workspace: @workspace)
|
|
120
|
+
c.add_tool Search::Grep.new(workspace: @workspace) if @grep
|
|
130
121
|
c.add_tool Search::Glob.new(workspace: @workspace)
|
|
131
122
|
c.add_tool FileList.new(workspace: @workspace)
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
# reaches Edit as confirmer-or-nil.
|
|
135
|
-
c.add_tool Edit.new(workspace: @workspace, read_only: @read_only,
|
|
136
|
-
confirmer: @confirm_all_writes ? @confirmer : nil)
|
|
123
|
+
c.add_tool Edit.new(workspace: @workspace, confirmer: @confirmer,
|
|
124
|
+
confirm_all_writes: @confirm_all_writes)
|
|
137
125
|
c.add_tool Write.new(workspace: @workspace, confirmer: @confirmer,
|
|
138
|
-
|
|
139
|
-
nil
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
# Clear the shared read-record when the host clears the conversation, so
|
|
143
|
-
# the read-before-edit gate forgets what the discarded conversation read —
|
|
144
|
-
# else an {Edit} right after "/clear" could touch a file the model can no
|
|
145
|
-
# longer see. The {Filesystem}'s policy is untouched; only the read-ledger
|
|
146
|
-
# resets.
|
|
147
|
-
#
|
|
148
|
-
# @param ctx [Pikuri::Agent::ExtensionContext] unused; protocol signature.
|
|
149
|
-
# @return [void]
|
|
150
|
-
def on_conversation_reset(_ctx)
|
|
151
|
-
@workspace.clear_reads
|
|
126
|
+
confirm_all_writes: @confirm_all_writes)
|
|
152
127
|
nil
|
|
153
128
|
end
|
|
154
129
|
|
|
@@ -30,8 +30,7 @@ module Pikuri
|
|
|
30
30
|
#
|
|
31
31
|
# Sharing: +P_one_agent+ — it never touches the read record, so nothing
|
|
32
32
|
# races; what makes it one agent's is the {Workspace} it enumerates, since
|
|
33
|
-
# that is one agent's project view
|
|
34
|
-
# +with_workspace+ rebinds a copy.
|
|
33
|
+
# that is one agent's project view.
|
|
35
34
|
class FileList < Pikuri::Tool
|
|
36
35
|
# Description shown to the LLM (opencode-shape). Per-parameter constraints
|
|
37
36
|
# live in the parameter descriptions.
|
|
@@ -81,14 +80,14 @@ module Pikuri
|
|
|
81
80
|
)
|
|
82
81
|
end
|
|
83
82
|
|
|
84
|
-
#
|
|
85
|
-
#
|
|
86
|
-
#
|
|
83
|
+
# The workspace root renders as +.+ rather than as nothing: +path+ is
|
|
84
|
+
# optional and the model usually omits it, which the generic default
|
|
85
|
+
# would show as a bare +FileList+.
|
|
87
86
|
#
|
|
88
|
-
# @param
|
|
89
|
-
# @return [
|
|
90
|
-
def
|
|
91
|
-
|
|
87
|
+
# @param arguments [Hash] raw tool-call arguments
|
|
88
|
+
# @return [String]
|
|
89
|
+
def describe_call(arguments)
|
|
90
|
+
Pikuri::Tool.one_line((arguments['path'] || arguments[:path] || '.').to_s)
|
|
92
91
|
end
|
|
93
92
|
|
|
94
93
|
# Resolve +path+, refuse a file / missing / oversized root, render.
|
|
@@ -97,25 +96,29 @@ module Pikuri
|
|
|
97
96
|
# @param path [String, nil] raw path from the LLM; +nil+ means the
|
|
98
97
|
# workspace root
|
|
99
98
|
# @param depth [Integer] levels to descend
|
|
100
|
-
# @return [
|
|
99
|
+
# @return [Tool::Result] the listing (+"Listed 31 entries"+), or
|
|
100
|
+
# {Pikuri::Tool::Result.error}
|
|
101
101
|
def self.list(workspace:, path:, depth:)
|
|
102
|
-
return "
|
|
102
|
+
return Tool::Result.error("depth must be >= 1, got #{depth}") if depth < 1
|
|
103
103
|
|
|
104
104
|
resolved = path ? workspace.resolve_for_read(path) : workspace.project_root
|
|
105
|
-
return "
|
|
106
|
-
return "
|
|
105
|
+
return Tool::Result.error("path not found: #{path}") unless resolved.exist?
|
|
106
|
+
return Tool::Result.error("#{path} is a file, not a directory; use the read tool to view it.") if resolved.file?
|
|
107
107
|
|
|
108
108
|
if (refusal = Search::Utils.oversized_root_refusal(resolved, filesystem: workspace.filesystem,
|
|
109
109
|
verb: 'list'))
|
|
110
|
-
return refusal
|
|
110
|
+
return Tool::Result.error(refusal)
|
|
111
111
|
end
|
|
112
112
|
|
|
113
|
-
listing = Listing.
|
|
114
|
-
|
|
113
|
+
listing, entries = Listing.render_with_count(resolved, filesystem: workspace.filesystem, depth: depth)
|
|
114
|
+
return Tool::Result.new(content: '(Empty directory)', summary: 'Listed 0 entries') if listing.empty?
|
|
115
|
+
|
|
116
|
+
Tool::Result.new(content: listing,
|
|
117
|
+
summary: "Listed #{Pikuri::Tool.pluralize(entries, 'entry', 'entries')}")
|
|
115
118
|
rescue Filesystem::Error => e
|
|
116
|
-
|
|
119
|
+
Tool::Result.error(e.message)
|
|
117
120
|
rescue SystemCallError => e
|
|
118
|
-
"
|
|
121
|
+
Tool::Result.error("cannot list #{path || '.'}: #{e.message}")
|
|
119
122
|
end
|
|
120
123
|
end
|
|
121
124
|
end
|