pikuri-workspace 0.0.7 → 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 +23 -16
- data/lib/pikuri/workspace/confirmer/terminal.rb +252 -0
- data/lib/pikuri/workspace/confirmer.rb +184 -123
- data/lib/pikuri/workspace/edit.rb +261 -101
- data/lib/pikuri/workspace/extension.rb +163 -0
- data/lib/pikuri/workspace/file_list.rb +125 -0
- data/lib/pikuri/workspace/filesystem.rb +160 -446
- data/lib/pikuri/workspace/listing.rb +204 -0
- data/lib/pikuri/workspace/read.rb +139 -225
- data/lib/pikuri/workspace/search/glob.rb +310 -0
- data/lib/pikuri/workspace/search/grep.rb +353 -0
- data/lib/pikuri/workspace/search/utils.rb +173 -0
- data/lib/pikuri/workspace/workspace.rb +105 -0
- data/lib/pikuri/workspace/write.rb +129 -78
- data/lib/pikuri/workspace/write_gate.rb +74 -0
- data/lib/pikuri-workspace.rb +8 -10
- data/prompts/pikuri-files.txt +6 -0
- metadata +32 -11
- data/lib/pikuri/workspace/glob.rb +0 -322
- data/lib/pikuri/workspace/grep.rb +0 -350
- data/lib/pikuri/workspace/project_root.rb +0 -102
|
@@ -2,92 +2,105 @@
|
|
|
2
2
|
|
|
3
3
|
module Pikuri
|
|
4
4
|
module Workspace
|
|
5
|
-
# The +edit+ tool — exact-string replacement on an existing file
|
|
6
|
-
# Instantiating +Edit.new(workspace: ws)+ produces a tool whose
|
|
7
|
-
# {Pikuri::Tool#to_ruby_llm_tool} wiring is identical to any bundled
|
|
8
|
-
# tool's. Same shape as {Read} (workspace captured by +execute+; no
|
|
9
|
-
# confirmer needed).
|
|
5
|
+
# The +edit+ tool — exact-string replacement on an existing file:
|
|
10
6
|
#
|
|
11
|
-
#
|
|
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
|
|
12
9
|
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
10
|
+
# == Confirm every edit, or opt out
|
|
11
|
+
#
|
|
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+.)
|
|
20
21
|
#
|
|
21
22
|
# == Matching is strict (no fuzz cascade)
|
|
22
23
|
#
|
|
23
|
-
# +old_string+ must match
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
24
|
+
# +old_string+ must match byte-for-byte. v1 ships *no* fallback replacer
|
|
25
|
+
# (no whitespace-normalized, block-anchor, etc.): predictability beats fuzz
|
|
26
|
+
# — a failed Edit means re-read and retry, a clear failure mode with no
|
|
27
|
+
# compounding-heuristic risk. (opencode runs a 9-replacer cascade despite
|
|
28
|
+
# saying "must match exactly"; pi stays strict, and so do we.)
|
|
29
|
+
#
|
|
30
|
+
# +occurrence:+ is what keeps that strictness affordable. Uniqueness, not
|
|
31
|
+
# exactness, is the expensive half: in a repetitive file the model must
|
|
32
|
+
# paste a large window just to disambiguate, and the window is pure token
|
|
33
|
+
# cost. +occurrence: 2+ picks the second match by position instead. It
|
|
34
|
+
# cannot silently hit the wrong text — +old_string+ still has to match
|
|
35
|
+
# byte-for-byte, so a stale index fails the call rather than editing
|
|
36
|
+
# elsewhere.
|
|
30
37
|
#
|
|
31
38
|
# == Line endings get normalized
|
|
32
39
|
#
|
|
33
|
-
# The one
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
+
# The one exception to "strict bytes": CRLF files are matched in LF space
|
|
41
|
+
# and the original ending restored on write. {Read} renders via +each_line+
|
|
42
|
+
# + +chomp+ (stripping +\r\n+ to +\n+), so a pure byte-match would never
|
|
43
|
+
# succeed on CRLF since the model can only supply LF. Algorithm: detect
|
|
44
|
+
# +\r\n+ anywhere (treat as CRLF) → normalize content/+old_string+/
|
|
45
|
+
# +new_string+ to LF → match+replace in LF space → convert +\n+ → +\r\n+ back
|
|
46
|
+
# if CRLF. Caveat: a mixed-ending file is treated as CRLF, so bare-LF lines
|
|
47
|
+
# get converted — rare, acceptable for v1.
|
|
48
|
+
#
|
|
49
|
+
# == Typographic lookalikes are named, never fixed
|
|
40
50
|
#
|
|
41
|
-
#
|
|
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:
|
|
42
54
|
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
#
|
|
46
|
-
# 4. If the file was CRLF, convert +\n+ → +\r\n+ on the way back out.
|
|
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.
|
|
47
58
|
#
|
|
48
|
-
#
|
|
49
|
-
#
|
|
50
|
-
#
|
|
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.
|
|
51
62
|
#
|
|
52
|
-
# == Refusals
|
|
63
|
+
# == Refusals (all as +"Error: ..."+)
|
|
53
64
|
#
|
|
54
|
-
#
|
|
65
|
+
# Empty +old_string+ ("use the write tool") · +old_string+ == +new_string+
|
|
66
|
+
# (no-op) · not found ("must match exactly", or the typographic
|
|
67
|
+
# characters that broke the match) · multiple matches without
|
|
68
|
+
# +replace_all+ or +occurrence+ · +occurrence+ combined with +replace_all+ ·
|
|
69
|
+
# +occurrence+ below 1 or past the match count · missing / directory /
|
|
70
|
+
# binary file · not read this conversation (the gate, after the binary
|
|
71
|
+
# refusal) · workspace boundary / EACCES.
|
|
55
72
|
#
|
|
56
|
-
#
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
# * +old_string+ not found in file → "must match exactly" error
|
|
60
|
-
# pointing at the read tool.
|
|
61
|
-
# * +old_string+ found multiple times without +replace_all+ →
|
|
62
|
-
# multi-match error suggesting more context or +replace_all+.
|
|
63
|
-
# * File missing / is a directory / is binary → respective error.
|
|
64
|
-
# * Workspace boundary violation / EACCES → standard rescue path.
|
|
73
|
+
# Sharing: +P_one_agent+ — the "not read this conversation" refusal reads
|
|
74
|
+
# one {Workspace}'s record, so sharing an instance widens *whose* read
|
|
75
|
+
# counts.
|
|
65
76
|
class Edit < Pikuri::Tool
|
|
66
|
-
# Description shown to the LLM
|
|
67
|
-
#
|
|
68
|
-
# convention. Per-parameter constraints live in the parameter
|
|
69
|
-
# descriptions.
|
|
77
|
+
# Description shown to the LLM (opencode-shape). Per-parameter constraints
|
|
78
|
+
# live in the parameter descriptions.
|
|
70
79
|
#
|
|
71
80
|
# @return [String]
|
|
72
81
|
DESCRIPTION = <<~DESC
|
|
73
82
|
Edit a file by exact-string replacement.
|
|
74
83
|
|
|
75
84
|
Usage:
|
|
76
|
-
-
|
|
77
|
-
-
|
|
85
|
+
- You must Read the file in this conversation before editing, or the call will fail.
|
|
86
|
+
- Use for partial changes to an existing file.
|
|
78
87
|
- `old_string` and `new_string` must differ.
|
|
79
|
-
- If `old_string` matches multiple times the call fails —
|
|
88
|
+
- If `old_string` matches multiple times the call fails — set `occurrence` to pick one by position, set `replace_all: true`, or add surrounding context to make the match unique.
|
|
80
89
|
- Cannot create files (rejects empty `old_string` and missing files).
|
|
81
90
|
- Binary files are refused.
|
|
82
91
|
- CRLF files are matched in LF space; the original line endings are preserved on write.
|
|
83
92
|
DESC
|
|
84
93
|
|
|
85
|
-
# @param workspace [
|
|
86
|
-
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
94
|
+
# @param workspace [Workspace] captured for path resolution and the
|
|
95
|
+
# read-before-edit gate; edits route through +resolve_for_write+ (Edit
|
|
96
|
+
# modifies, so the write-set) and the gate consults +read?+.
|
|
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").
|
|
89
101
|
# @return [Edit]
|
|
90
|
-
def initialize(workspace:)
|
|
102
|
+
def initialize(workspace:, confirmer:, confirm_all_writes: true)
|
|
103
|
+
gate = confirm_all_writes ? confirmer : nil
|
|
91
104
|
super(
|
|
92
105
|
name: 'edit',
|
|
93
106
|
description: DESCRIPTION,
|
|
@@ -98,46 +111,85 @@ module Pikuri
|
|
|
98
111
|
'"lib/foo.rb".'
|
|
99
112
|
p.required_string :old_string,
|
|
100
113
|
'Exact text to find in the file. Must match ' \
|
|
101
|
-
'byte-for-byte (whitespace counts);
|
|
102
|
-
'
|
|
103
|
-
'
|
|
114
|
+
'byte-for-byte (whitespace counts); strip the ' \
|
|
115
|
+
'Read line prefix (line number + tab) before ' \
|
|
116
|
+
'matching; must be unique unless replace_all is ' \
|
|
117
|
+
'true. Example: "def foo\n bar\nend".'
|
|
104
118
|
p.required_string :new_string,
|
|
105
|
-
'Replacement text.
|
|
106
|
-
'old_string. Example: "def foo\n baz\nend".'
|
|
119
|
+
'Replacement text. Example: "def foo\n baz\nend".'
|
|
107
120
|
p.optional_boolean :replace_all,
|
|
108
121
|
'Replace every occurrence of old_string ' \
|
|
109
122
|
'instead of failing on multiple matches. ' \
|
|
110
123
|
'Defaults to false, e.g. true.'
|
|
124
|
+
p.optional_integer :occurrence,
|
|
125
|
+
'Replace only the Nth match (1-based, counted ' \
|
|
126
|
+
'top to bottom) and leave the rest alone. Use ' \
|
|
127
|
+
'this instead of padding old_string with ' \
|
|
128
|
+
'context when the file repeats the same ' \
|
|
129
|
+
'snippet. Cannot be combined with replace_all.'
|
|
111
130
|
},
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
131
|
+
wants_context: true,
|
|
132
|
+
execute: ->(path:, old_string:, new_string:, context:, replace_all: false, occurrence: nil) {
|
|
133
|
+
Edit.edit(workspace: workspace, confirmer: gate,
|
|
134
|
+
path: path, old_string: old_string, new_string: new_string,
|
|
135
|
+
replace_all: replace_all, occurrence: occurrence,
|
|
136
|
+
cancellable: context.cancellable)
|
|
137
|
+
},
|
|
138
|
+
# No legs, for the same reason as {Write}: the observation reports the
|
|
139
|
+
# outcome, never the file's bytes, so nothing enters the model's
|
|
140
|
+
# context here. The read-before-edit gate makes that concrete — the
|
|
141
|
+
# bytes arrived through {Read}, which is where the leg is tagged.
|
|
142
|
+
trifecta_legs: Tool::TrifectaLegs::NONE
|
|
117
143
|
)
|
|
118
144
|
end
|
|
119
145
|
|
|
120
|
-
# Resolve +path
|
|
121
|
-
#
|
|
122
|
-
#
|
|
123
|
-
#
|
|
146
|
+
# Resolve +path+, run the precondition checks (non-empty / non-identical /
|
|
147
|
+
# exists / not directory / not binary / read this conversation), match
|
|
148
|
+
# +old_string+ in LF-normalized form, and write back preserving the file's
|
|
149
|
+
# original line endings.
|
|
124
150
|
#
|
|
125
|
-
# @param workspace [
|
|
126
|
-
# @param path [String] raw path
|
|
151
|
+
# @param workspace [Workspace]
|
|
152
|
+
# @param path [String] raw path from the LLM
|
|
127
153
|
# @param old_string [String] text to find
|
|
128
|
-
# @param new_string [String] text to substitute
|
|
129
|
-
# @param replace_all [Boolean]
|
|
130
|
-
#
|
|
131
|
-
# @
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
154
|
+
# @param new_string [String] text to substitute
|
|
155
|
+
# @param replace_all [Boolean] true replaces every occurrence; false
|
|
156
|
+
# (default) errors on multiple matches unless +occurrence+ picks one
|
|
157
|
+
# @param occurrence [Integer, nil] 1-based index of the single match to
|
|
158
|
+
# replace, lifting the uniqueness requirement; +nil+ (default) keeps
|
|
159
|
+
# it. Mutually exclusive with +replace_all+.
|
|
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.
|
|
168
|
+
def self.edit(workspace:, path:, old_string:, new_string:, replace_all:,
|
|
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
|
|
176
|
+
|
|
177
|
+
if occurrence && replace_all
|
|
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.')
|
|
180
|
+
end
|
|
181
|
+
return Tool::Result.error("occurrence must be 1 or greater, got #{occurrence}.") if occurrence && occurrence < 1
|
|
135
182
|
|
|
136
183
|
resolved = workspace.resolve_for_write(path)
|
|
137
|
-
return
|
|
138
|
-
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?
|
|
139
186
|
|
|
140
|
-
return "
|
|
187
|
+
return Tool::Result.error("cannot edit binary file: #{path}") if Pikuri::FileType.binary?(resolved)
|
|
188
|
+
|
|
189
|
+
unless workspace.read?(resolved)
|
|
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.')
|
|
192
|
+
end
|
|
141
193
|
|
|
142
194
|
raw = resolved.binread
|
|
143
195
|
|
|
@@ -148,42 +200,76 @@ module Pikuri
|
|
|
148
200
|
|
|
149
201
|
occurrences = content.scan(needle).size
|
|
150
202
|
if occurrences.zero?
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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.')
|
|
154
211
|
end
|
|
155
|
-
if
|
|
156
|
-
return "
|
|
157
|
-
|
|
158
|
-
|
|
212
|
+
if occurrence && occurrence > occurrences
|
|
213
|
+
return Tool::Result.error("old_string matches #{occurrences} time#{occurrences == 1 ? '' : 's'} " \
|
|
214
|
+
"in #{path}, so occurrence #{occurrence} does not exist.")
|
|
215
|
+
end
|
|
216
|
+
if occurrences > 1 && !replace_all && !occurrence
|
|
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.')
|
|
159
221
|
end
|
|
160
222
|
|
|
161
|
-
replaced = replace_all ? occurrences : 1
|
|
162
223
|
new_content =
|
|
163
224
|
if replace_all
|
|
164
225
|
# Block form bypasses gsub's \1 / \& interpolation on the
|
|
165
226
|
# replacement String — we want literal substitution.
|
|
166
227
|
content.gsub(needle) { patch }
|
|
167
228
|
else
|
|
168
|
-
idx = content
|
|
229
|
+
idx = nth_index(content, needle, occurrence || 1)
|
|
169
230
|
content.byteslice(0, idx) + patch + content.byteslice(idx + needle.bytesize, content.bytesize - idx - needle.bytesize)
|
|
170
231
|
end
|
|
171
232
|
|
|
172
233
|
final = crlf ? new_content.gsub("\n", "\r\n") : new_content
|
|
234
|
+
|
|
235
|
+
replaced = replace_all ? occurrences : 1
|
|
236
|
+
detail =
|
|
237
|
+
if occurrence
|
|
238
|
+
"occurrence #{occurrence} of #{occurrences}"
|
|
239
|
+
else
|
|
240
|
+
"#{replaced} occurrence#{replaced == 1 ? '' : 's'}"
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
if confirmer
|
|
244
|
+
gate = WriteGate.check(
|
|
245
|
+
confirmer: confirmer, path: path, resolved: resolved,
|
|
246
|
+
question: "OK to edit #{path} (#{detail})?",
|
|
247
|
+
change: Confirmer::Change.new(path: path, old: raw, new: final),
|
|
248
|
+
cancellable: cancellable
|
|
249
|
+
)
|
|
250
|
+
return gate if gate
|
|
251
|
+
end
|
|
252
|
+
|
|
173
253
|
resolved.write(final)
|
|
174
254
|
|
|
175
|
-
|
|
255
|
+
# Keep the path marked read: the editor knows the resulting bytes, so a
|
|
256
|
+
# follow-up edit needn't re-read. Idempotent — the read? gate passed.
|
|
257
|
+
workspace.mark_read(resolved)
|
|
258
|
+
|
|
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
|
+
)
|
|
176
264
|
rescue Filesystem::Error => e
|
|
177
|
-
|
|
265
|
+
Tool::Result.error(e.message)
|
|
178
266
|
rescue Errno::EACCES => e
|
|
179
|
-
"
|
|
267
|
+
Tool::Result.error("cannot edit #{path}: #{e.message}")
|
|
180
268
|
end
|
|
181
269
|
|
|
182
|
-
# Force a String to BINARY
|
|
183
|
-
#
|
|
184
|
-
#
|
|
185
|
-
# +new_string+ alike — symmetric normalization keeps the byte-match
|
|
186
|
-
# semantics consistent across all three inputs.
|
|
270
|
+
# Force a String to BINARY and collapse +\r\n+ → +\n+ so matching/
|
|
271
|
+
# replacement happens in LF space with byte-stable comparisons. Applied to
|
|
272
|
+
# content, +old_string+, and +new_string+ alike (symmetric).
|
|
187
273
|
#
|
|
188
274
|
# @param str [String]
|
|
189
275
|
# @return [String] BINARY-encoded, CRLF-collapsed copy
|
|
@@ -191,6 +277,80 @@ module Pikuri
|
|
|
191
277
|
str.b.gsub("\r\n", "\n")
|
|
192
278
|
end
|
|
193
279
|
private_class_method :normalize_lf
|
|
280
|
+
|
|
281
|
+
# Byte offset of the +nth+ (1-based) non-overlapping match of +needle+.
|
|
282
|
+
#
|
|
283
|
+
# nth_index("foofoofoo", "foo", 2) # => 3
|
|
284
|
+
#
|
|
285
|
+
# Non-overlapping to agree with the +String#scan+ count the caller
|
|
286
|
+
# bounds +nth+ against — so the two can never disagree.
|
|
287
|
+
#
|
|
288
|
+
# @param haystack [String] BINARY-encoded content
|
|
289
|
+
# @param needle [String] BINARY-encoded search string
|
|
290
|
+
# @param nth [Integer] 1-based match index, already known to exist
|
|
291
|
+
# @return [Integer] byte offset of the match
|
|
292
|
+
def self.nth_index(haystack, needle, nth)
|
|
293
|
+
idx = haystack.index(needle)
|
|
294
|
+
(nth - 1).times { idx = haystack.index(needle, idx + needle.bytesize) }
|
|
295
|
+
idx
|
|
296
|
+
end
|
|
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
|
|
194
354
|
end
|
|
195
355
|
end
|
|
196
356
|
end
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pikuri
|
|
4
|
+
module Workspace
|
|
5
|
+
# An {Pikuri::Agent::Extension} that wires the file tools ({Read},
|
|
6
|
+
# {Search::Grep}, {Search::Glob}, {FileList}, {Edit}, {Write}) onto an agent, sharing a single
|
|
7
|
+
# {Workspace} composite — which is what makes the read-before-edit gate
|
|
8
|
+
# work: {Read} marks a path read in the same record {Edit} and
|
|
9
|
+
# overwrite-{Write} consult. One registration point removes the per-host
|
|
10
|
+
# +add_tool+ boilerplate. The host builds the {Filesystem} and {Confirmer}
|
|
11
|
+
# and hands them over; the composite and its read record are internal.
|
|
12
|
+
#
|
|
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.
|
|
16
|
+
#
|
|
17
|
+
# Contributes {PROMPT_SNIPPET} — the "a decline is final" confirmation
|
|
18
|
+
# guidance — so it rides the wiring of the mutating tools instead of
|
|
19
|
+
# being restated in each host prompt, plus the boot snapshot below.
|
|
20
|
+
#
|
|
21
|
+
# == Boot snapshot
|
|
22
|
+
#
|
|
23
|
+
# A depth-1 {Listing} of the workspace root, contributed once so
|
|
24
|
+
# orientation costs zero tool calls — {Search::Glob} is +rg --files+, which
|
|
25
|
+
# emits files only, so +glob '*'+ hides every directory:
|
|
26
|
+
#
|
|
27
|
+
# <workspace_snapshot>
|
|
28
|
+
# The workspace root is /home/alice/proj. Its top level, as of the start of this conversation:
|
|
29
|
+
#
|
|
30
|
+
# lib/ [1 dir, 1 file]
|
|
31
|
+
# spec/ [1 dir, 1 file]
|
|
32
|
+
# README.md 2.3K
|
|
33
|
+
#
|
|
34
|
+
# One level only, and not refreshed — call `file_list` for anything deeper or newer.
|
|
35
|
+
# </workspace_snapshot>
|
|
36
|
+
#
|
|
37
|
+
# One level is the design, not a default to grow: {Listing}'s
|
|
38
|
+
# +[N dirs, M files]+ column already does depth 2's job on the wide-flat
|
|
39
|
+
# directories where depth 2 costs most — a blog's +_posts/ [0 dirs, 256
|
|
40
|
+
# files]+ is 30 bytes against 24 KB expanded. Measurements and the rejected
|
|
41
|
+
# ladder live in +design/decisions.md+ +D_directory_listing+.
|
|
42
|
+
#
|
|
43
|
+
# Taken once per agent, so a "/clear" — a new agent — refreshes it and
|
|
44
|
+
# nothing else does.
|
|
45
|
+
#
|
|
46
|
+
# == Usage
|
|
47
|
+
#
|
|
48
|
+
# Pikuri::Agent.new(...) do |c|
|
|
49
|
+
# c.add_extension Pikuri::Workspace::Extension.new(
|
|
50
|
+
# filesystem: filesystem, confirmer: confirmer)
|
|
51
|
+
# end
|
|
52
|
+
class Extension
|
|
53
|
+
include Pikuri::Agent::Extension
|
|
54
|
+
|
|
55
|
+
# The tools the extension owns, and the duplicate-registration guard
|
|
56
|
+
# list: it must be the single owner of the shared {Workspace}, so a
|
|
57
|
+
# pre-registered file tool (on a different composite) would defeat the
|
|
58
|
+
# read-ledger sharing.
|
|
59
|
+
TOOL_CLASSES = [Read, Search::Grep, Search::Glob, FileList, Edit, Write].freeze
|
|
60
|
+
|
|
61
|
+
# @return [String] system-prompt guidance wired alongside the mutating
|
|
62
|
+
# tools: the confirmer may pause a write/edit (and a co-wired Bash)
|
|
63
|
+
# for the user's OK; a plain decline is final, a decline with words is
|
|
64
|
+
# guidance to follow. Contributed as a snippet
|
|
65
|
+
# so it tracks the wiring rather than each host prompt (the Os/Code
|
|
66
|
+
# precedent), and emitted unconditionally — an auto-approve ("yolo")
|
|
67
|
+
# confirmer is the rare exception, and the guidance is harmless there.
|
|
68
|
+
PROMPT_SNIPPET = <<~SNIPPET.chomp
|
|
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.
|
|
70
|
+
SNIPPET
|
|
71
|
+
|
|
72
|
+
# @return [Integer] +depth+ the boot snapshot renders at. Deliberately
|
|
73
|
+
# not {Listing::DEFAULT_DEPTH}: both are 1 today, so folding them
|
|
74
|
+
# together compiles, passes, and silently re-points the snapshot the
|
|
75
|
+
# day {FileList}'s per-call default moves.
|
|
76
|
+
SNAPSHOT_DEPTH = 1
|
|
77
|
+
|
|
78
|
+
# @return [Integer] byte ceiling on the rendered snapshot; over it the
|
|
79
|
+
# snippet degrades to one line rather than head-truncating, because a
|
|
80
|
+
# truncated listing is an alphabetical prefix that teaches the model
|
|
81
|
+
# +spec/+ does not exist. Twice the largest root measured (a real
|
|
82
|
+
# +$HOME+), so it is a fail-safe, not a routine path.
|
|
83
|
+
SNAPSHOT_MAX_BYTES = 2 * 1024
|
|
84
|
+
|
|
85
|
+
# @return [Workspace] the composite shared across the tools;
|
|
86
|
+
# exposed for tests / introspection.
|
|
87
|
+
attr_reader :workspace
|
|
88
|
+
|
|
89
|
+
# @param filesystem [Filesystem] path resolution / sandboxing; wrapped in a
|
|
90
|
+
# fresh {Workspace} (empty read record).
|
|
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)
|
|
102
|
+
@workspace = Workspace.new(filesystem: filesystem)
|
|
103
|
+
@confirmer = confirmer
|
|
104
|
+
@confirm_all_writes = confirm_all_writes
|
|
105
|
+
@grep = grep
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# @param c [Pikuri::Agent::Configurator]
|
|
109
|
+
# @return [void]
|
|
110
|
+
def configure(c)
|
|
111
|
+
TOOL_CLASSES.each do |cls|
|
|
112
|
+
if c.tools.any?(cls)
|
|
113
|
+
raise "#{cls} cannot be pre-registered (in tools: or via c.add_tool) " \
|
|
114
|
+
'when adding Pikuri::Workspace::Extension — the extension auto-registers every ' \
|
|
115
|
+
'file tool so they share one Workspace (and one read record).'
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
c.add_tool Read.new(workspace: @workspace)
|
|
120
|
+
c.add_tool Search::Grep.new(workspace: @workspace) if @grep
|
|
121
|
+
c.add_tool Search::Glob.new(workspace: @workspace)
|
|
122
|
+
c.add_tool FileList.new(workspace: @workspace)
|
|
123
|
+
c.add_tool Edit.new(workspace: @workspace, confirmer: @confirmer,
|
|
124
|
+
confirm_all_writes: @confirm_all_writes)
|
|
125
|
+
c.add_tool Write.new(workspace: @workspace, confirmer: @confirmer,
|
|
126
|
+
confirm_all_writes: @confirm_all_writes)
|
|
127
|
+
nil
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# @return [Array<String>] the confirmation guidance ({PROMPT_SNIPPET}),
|
|
131
|
+
# always — the confirmer is wired on every non-yolo host — followed by
|
|
132
|
+
# the boot snapshot.
|
|
133
|
+
# @raise [SystemCallError] if the workspace root itself can't be opened.
|
|
134
|
+
# Deliberately unhandled: it surfaces out of +Agent.new+, and a root you
|
|
135
|
+
# can't open is a wiring bug every file tool is about to hit anyway.
|
|
136
|
+
def system_prompt_snippets = [PROMPT_SNIPPET, workspace_snapshot]
|
|
137
|
+
|
|
138
|
+
private
|
|
139
|
+
|
|
140
|
+
# The +<workspace_snapshot>+ block. An empty root and an over-cap root
|
|
141
|
+
# each collapse to one line, so the block is never blank — which would
|
|
142
|
+
# read as a failed walk — and never truncated.
|
|
143
|
+
#
|
|
144
|
+
# @return [String]
|
|
145
|
+
def workspace_snapshot
|
|
146
|
+
root = @workspace.project_root
|
|
147
|
+
listing = Listing.render(root, filesystem: @workspace.filesystem, depth: SNAPSHOT_DEPTH)
|
|
148
|
+
body =
|
|
149
|
+
if listing.empty?
|
|
150
|
+
"The workspace root is #{root}. It is empty."
|
|
151
|
+
elsif listing.bytesize > SNAPSHOT_MAX_BYTES
|
|
152
|
+
"The workspace root is #{root}. It has too many entries to show " \
|
|
153
|
+
'here — call `file_list` to explore it.'
|
|
154
|
+
else
|
|
155
|
+
"The workspace root is #{root}. Its top level, as of the start of " \
|
|
156
|
+
"this conversation:\n\n#{listing}\n\nOne level only, and not " \
|
|
157
|
+
'refreshed — call `file_list` for anything deeper or newer.'
|
|
158
|
+
end
|
|
159
|
+
"<workspace_snapshot>\n#{body}\n</workspace_snapshot>"
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|