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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 33790a827430e92487bca4a2221245cb72d1ad95eba39deea3e89e9ee75aae0a
|
|
4
|
+
data.tar.gz: f77e2249f51dde56628436d25870e575bae017227593cd9c20c9d27fd8533afb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 969d8c3401bbbfcbb1f784652e4a9243cbf33080d59e0ac0af0440991035615f74b1d47f029a3d33e23cc49c44c1feec86df374357b181511f4b4f990abcfe46
|
|
7
|
+
data.tar.gz: 996ff3f297b424ca333d2441d06608ddf810a37515f51e50f0ad8354cd6b3f3a6a67284c496c29f7786bda82abf440eb3a58011f9565f7472d8717216751c35a
|
data/README.md
CHANGED
|
@@ -1,21 +1,29 @@
|
|
|
1
1
|
# pikuri-workspace
|
|
2
2
|
|
|
3
3
|
Filesystem tools + Workspace/Confirmer seams for the
|
|
4
|
-
[pikuri](https://
|
|
4
|
+
[pikuri](https://github.com/mvysny/pikuri) AI-assistant toolkit.
|
|
5
5
|
|
|
6
6
|
Self-contained "operate on a directory tree" toolkit:
|
|
7
7
|
- `Pikuri::Workspace::Filesystem` — scopes filesystem access to a
|
|
8
|
-
project root + explicit readable / writable prefix lists
|
|
9
|
-
optional ephemeral temp playground. Rejects `..`-escapes and
|
|
8
|
+
project root + explicit readable / writable prefix lists. Rejects `..`-escapes and
|
|
10
9
|
symlinks that resolve outside the configured roots.
|
|
11
|
-
- `Pikuri::Workspace::Confirmer` — abstract base
|
|
12
|
-
`
|
|
13
|
-
|
|
10
|
+
- `Pikuri::Workspace::Confirmer` — abstract base for approving
|
|
11
|
+
user-state mutations, plus `AUTO_APPROVE` (headless) and `Terminal`
|
|
12
|
+
(stdin/stdout). `Terminal` is written for pikuri's own single-threaded
|
|
13
|
+
demo scripts — a real app implements this seam itself and reads
|
|
14
|
+
`Terminal` as the worked example.
|
|
15
|
+
- Six file tools: `Pikuri::Workspace::Read`,
|
|
14
16
|
`Pikuri::Workspace::Write`, `Pikuri::Workspace::Edit`,
|
|
15
|
-
`Pikuri::Workspace::Grep`, `Pikuri::Workspace::Glob
|
|
17
|
+
`Pikuri::Workspace::Search::Grep`, `Pikuri::Workspace::Search::Glob`,
|
|
18
|
+
`Pikuri::Workspace::FileList`, wired together by
|
|
19
|
+
`Pikuri::Workspace::Extension`.
|
|
20
|
+
|
|
21
|
+
Demo binary: `bin/pikuri-files`, an agent working in one folder that
|
|
22
|
+
shows you every change as a diff before it lands. Walk-through: the
|
|
23
|
+
[touching-files chapter](../book/touching-files.md).
|
|
16
24
|
|
|
17
25
|
No shell execution — `Pikuri::Code::Bash` ships in
|
|
18
|
-
[`pikuri-code`](
|
|
26
|
+
[`pikuri-code`](https://github.com/mvysny/pikuri-workshop/blob/master/pikuri-code/README.md) on top of these.
|
|
19
27
|
|
|
20
28
|
## Install
|
|
21
29
|
|
|
@@ -31,7 +39,8 @@ require 'pikuri-core'
|
|
|
31
39
|
require 'pikuri-workspace'
|
|
32
40
|
|
|
33
41
|
workspace = Pikuri::Workspace::Filesystem.new(project_root: Dir.pwd)
|
|
34
|
-
|
|
42
|
+
# Fine for a script like this one; write your own for a real app.
|
|
43
|
+
confirmer = Pikuri::Workspace::Confirmer::Terminal.new
|
|
35
44
|
|
|
36
45
|
agent = Pikuri::Agent.new(
|
|
37
46
|
transport: ...,
|
|
@@ -40,7 +49,7 @@ agent = Pikuri::Agent.new(
|
|
|
40
49
|
c.add_tool Pikuri::Workspace::Read.new(workspace: workspace)
|
|
41
50
|
c.add_tool Pikuri::Workspace::Grep.new(workspace: workspace)
|
|
42
51
|
c.add_tool Pikuri::Workspace::Glob.new(workspace: workspace)
|
|
43
|
-
c.add_tool Pikuri::Workspace::Edit.new(workspace: workspace)
|
|
52
|
+
c.add_tool Pikuri::Workspace::Edit.new(workspace: workspace, confirmer: confirmer)
|
|
44
53
|
c.add_tool Pikuri::Workspace::Write.new(workspace: workspace, confirmer: confirmer)
|
|
45
54
|
c.add_listener ...
|
|
46
55
|
end
|
|
@@ -49,17 +58,15 @@ end
|
|
|
49
58
|
`Workspace` is the "look-but-don't-leak" guard around filesystem
|
|
50
59
|
access. Read tools route through `#resolve_for_read(path)`; mutating
|
|
51
60
|
tools route through `#resolve_for_write(path)` + the Confirmer's
|
|
52
|
-
`#
|
|
53
|
-
writable playground via `Dir.mktmpdir` — its path is exposed as
|
|
54
|
-
`workspace.temp` and auto-removed at process exit.
|
|
61
|
+
`#ask(request:, cancellable:)`.
|
|
55
62
|
|
|
56
63
|
## Further reading
|
|
57
64
|
|
|
58
65
|
- **Narrative walkthrough:** the "Workspace seam" and "Confirmer
|
|
59
66
|
seam" sections of
|
|
60
|
-
[chapter
|
|
61
|
-
each kwarg of `Filesystem.new` controls
|
|
62
|
-
|
|
67
|
+
[the coding-agent chapter of the pikuri guide](https://github.com/mvysny/pikuri-workshop/blob/master/book/code.md) — what
|
|
68
|
+
each kwarg of `Filesystem.new` controls and how the seams fit
|
|
69
|
+
alongside `Sandbox`.
|
|
63
70
|
- **API reference:** browse the YARD docs at
|
|
64
71
|
<https://rubydoc.info/gems/pikuri-workspace> (once published),
|
|
65
72
|
or run `bundle exec yard` in this directory for a local copy.
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rainbow'
|
|
4
|
+
require 'reline'
|
|
5
|
+
require 'tmpdir'
|
|
6
|
+
|
|
7
|
+
module Pikuri
|
|
8
|
+
module Workspace
|
|
9
|
+
class Confirmer
|
|
10
|
+
# Stdin/stdout implementation, written for pikuri's own single-threaded
|
|
11
|
+
# +bin/+ scripts — **not a component to build a host on**. A real host
|
|
12
|
+
# (TUI, web) owns its screen and its event loop, so it cannot have a
|
|
13
|
+
# library class calling +puts+ and +gets+ behind its back; it writes its
|
|
14
|
+
# own {Confirmer} and this is the worked example to read while doing it.
|
|
15
|
+
# The one piece to carry over is not the chrome but the warning-gated
|
|
16
|
+
# retype below — everything else is a terminal's answer to a problem your
|
|
17
|
+
# UI will answer differently.
|
|
18
|
+
#
|
|
19
|
+
# Renders the request as up to several lines
|
|
20
|
+
# (a leading +puts+ separates it from any streamed output above):
|
|
21
|
+
#
|
|
22
|
+
# 1. a bold-yellow warning block (one line per {Pikuri::Sanitizer::Warning})
|
|
23
|
+
# when the sanitizer flagged the question or detail
|
|
24
|
+
# 2. the question, bold
|
|
25
|
+
# 3. the detail, dim — omitted when +nil+
|
|
26
|
+
# 4. the answer cue
|
|
27
|
+
#
|
|
28
|
+
# Question and detail pass through {Pikuri::Sanitizer}, which neutralizes
|
|
29
|
+
# control bytes (else a model could craft +"\rrm -rf ~/"+ that overwrites
|
|
30
|
+
# the echoed line after the user read it) and reports *why* it was unsafe.
|
|
31
|
+
# Colors are Rainbow (self-disables on non-TTY).
|
|
32
|
+
#
|
|
33
|
+
# The human *chrome* — the one confirmer that renders a dialog and reads an
|
|
34
|
+
# answer. In its own file so the base seam carries no rendering deps
|
|
35
|
+
# (Rainbow / Reline / tmpdir ride only here).
|
|
36
|
+
#
|
|
37
|
+
# == The two flows
|
|
38
|
+
#
|
|
39
|
+
# A *non-editable* request (bash, write) gets
|
|
40
|
+
# +[y] approve [n] reject [r] redirect+: +y+ → {Approved}, +n+ or EOF →
|
|
41
|
+
# {Rejected}, +r+ → a line of text → {Redirected} (a blank line backs off
|
|
42
|
+
# to {Rejected}), else re-prompt.
|
|
43
|
+
#
|
|
44
|
+
# An *editable* request (the +agent+ task) also offers +e+ to edit, and
|
|
45
|
+
# the warning set gates the pre-fill: the payload sent onward is RAW bytes
|
|
46
|
+
# but the terminal must *display* sanitized bytes, so pre-filling an
|
|
47
|
+
# editable buffer with raw bytes would reintroduce the control-byte attack.
|
|
48
|
+
# So a *clean* task offers +[y] approve [e] edit [n] reject [r] redirect+
|
|
49
|
+
# (+e+ opens the raw task editable), a *flagged* task drops bare-approve and
|
|
50
|
+
# forces a retype: +[e] edit [n] reject [r] redirect+. Honest limit: a terminal can't
|
|
51
|
+
# *structurally* forbid a rubber-stamp +y+ on a clean task — the pure
|
|
52
|
+
# no-rubber-stamp author seat is a richer-UI invariant; the warning-gated
|
|
53
|
+
# retype is the terminal's partial answer.
|
|
54
|
+
#
|
|
55
|
+
# == Sharing
|
|
56
|
+
#
|
|
57
|
+
# +P_one_agent+, and no lock is coming: this is the demo chrome, and one
|
|
58
|
+
# script is one agent. Two agents prompting at once would interleave their
|
|
59
|
+
# blocks on one terminal, and whichever +gets+ runs first reads the answer
|
|
60
|
+
# — so agent A could consume the +y+ meant for agent B and approve a
|
|
61
|
+
# command nobody read. There is no state to corrupt; the contended
|
|
62
|
+
# resource is the terminal and the human at it.
|
|
63
|
+
#
|
|
64
|
+
# That queue belongs to whoever owns the screen, and it needs more than a
|
|
65
|
+
# mutex — it has to say *which* agent is asking. Which is the same
|
|
66
|
+
# sentence as the paragraph at the top: a multi-agent host writes its own
|
|
67
|
+
# confirmer.
|
|
68
|
+
class Terminal < Confirmer
|
|
69
|
+
LOGGER = Pikuri.logger_for('Workspace::Confirmer')
|
|
70
|
+
|
|
71
|
+
# Maximum diff lines rendered before truncation — keeps a
|
|
72
|
+
# whole-file rewrite from scrolling the terminal off-screen.
|
|
73
|
+
DIFF_MAX_LINES = 300
|
|
74
|
+
|
|
75
|
+
# Color of a +@@ hunk @@+ header: machinery, so it wears the same coat
|
|
76
|
+
# tan the tool-call lines do.
|
|
77
|
+
# @return [String]
|
|
78
|
+
HUNK_COLOR = Pikuri::Agent::Listener::Terminal::TOOL_COLOR
|
|
79
|
+
|
|
80
|
+
# @param request [Request]
|
|
81
|
+
# @param cancellable [Pikuri::Agent::Control::Cancellable] unused:
|
|
82
|
+
# Ctrl+C interrupts the blocking read as +SIGINT+, which no flag can
|
|
83
|
+
# @return [Approved, Rejected, Redirected]
|
|
84
|
+
def ask(request:, cancellable:)
|
|
85
|
+
warnings = render(request)
|
|
86
|
+
if request.editable && !request.detail.nil?
|
|
87
|
+
ask_editable(request, clean: warnings.empty?)
|
|
88
|
+
else
|
|
89
|
+
ask_plain(request)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
# Print the sanitized question / detail / warning block.
|
|
96
|
+
#
|
|
97
|
+
# @param request [Request]
|
|
98
|
+
# @return [Array<Pikuri::Sanitizer::Warning>] the flagged warnings
|
|
99
|
+
# (empty when clean) — the caller gates the edit pre-fill on this.
|
|
100
|
+
def render(request)
|
|
101
|
+
question = Pikuri::Sanitizer.sanitize(request.question)
|
|
102
|
+
detail = request.detail ? Pikuri::Sanitizer.sanitize(request.detail) : nil
|
|
103
|
+
raw_diff = request.change ? unified_diff(request.change) : nil
|
|
104
|
+
diff = raw_diff ? Pikuri::Sanitizer.sanitize(raw_diff) : nil
|
|
105
|
+
warnings = question.warnings + (detail ? detail.warnings : []) + (diff ? diff.warnings : [])
|
|
106
|
+
|
|
107
|
+
puts
|
|
108
|
+
unless warnings.empty?
|
|
109
|
+
puts Rainbow('⚠ Suspicious content detected — read carefully before approving:').yellow.bold
|
|
110
|
+
warnings.each { |w| puts Rainbow(" ! #{w.explanation}").yellow }
|
|
111
|
+
end
|
|
112
|
+
puts Rainbow(question.text).bold
|
|
113
|
+
puts Rainbow(detail.text).dimgray if detail
|
|
114
|
+
print_diff(diff.text) if diff
|
|
115
|
+
warnings
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Render a unified diff of +change+ by shelling to +diff -u+ (routed
|
|
119
|
+
# through {Pikuri::Subprocess}; a richer client uses its own diff lib).
|
|
120
|
+
# Old/new bytes go to tempfiles under a self-cleaning dir. Returns the
|
|
121
|
+
# diff capped at {DIFF_MAX_LINES}, or +nil+ on any failure (empty diff,
|
|
122
|
+
# missing +diff+, IO error) — a cosmetic path, so it degrades to "no
|
|
123
|
+
# diff shown" rather than raising.
|
|
124
|
+
#
|
|
125
|
+
# @param change [Change]
|
|
126
|
+
# @return [String, nil]
|
|
127
|
+
def unified_diff(change)
|
|
128
|
+
Dir.mktmpdir('pikuri-diff-') do |dir|
|
|
129
|
+
old_f = File.join(dir, 'old')
|
|
130
|
+
new_f = File.join(dir, 'new')
|
|
131
|
+
out_f = File.join(dir, 'out')
|
|
132
|
+
File.binwrite(old_f, change.old || '')
|
|
133
|
+
File.binwrite(new_f, change.new)
|
|
134
|
+
File.open(out_f, 'w') do |o|
|
|
135
|
+
Pikuri::Subprocess.run(
|
|
136
|
+
'diff', '-u', '--label', "a/#{change.path}", '--label', "b/#{change.path}",
|
|
137
|
+
old_f, new_f, stdin_data: '', stdout: o, chdir: dir
|
|
138
|
+
)
|
|
139
|
+
end
|
|
140
|
+
text = File.read(out_f)
|
|
141
|
+
return nil if text.empty?
|
|
142
|
+
|
|
143
|
+
lines = text.lines
|
|
144
|
+
return text if lines.size <= DIFF_MAX_LINES
|
|
145
|
+
|
|
146
|
+
lines.first(DIFF_MAX_LINES).join + "… (#{lines.size - DIFF_MAX_LINES} more diff lines)\n"
|
|
147
|
+
end
|
|
148
|
+
rescue StandardError => e
|
|
149
|
+
LOGGER.warn("diff render failed (#{e.class}: #{e.message}); confirming without a diff")
|
|
150
|
+
nil
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Print a sanitized unified diff, coloring +added+/-removed/@@hunk@@
|
|
154
|
+
# lines. Sanitization ran in {#render}; this only adds color.
|
|
155
|
+
#
|
|
156
|
+
# @param text [String] sanitized diff text
|
|
157
|
+
# @return [void]
|
|
158
|
+
def print_diff(text)
|
|
159
|
+
text.each_line do |line|
|
|
160
|
+
body = line.chomp
|
|
161
|
+
puts(case body[0]
|
|
162
|
+
when '+' then Rainbow(body).green
|
|
163
|
+
when '-' then Rainbow(body).red
|
|
164
|
+
when '@' then Rainbow(body).color(HUNK_COLOR)
|
|
165
|
+
else Rainbow(body).dimgray
|
|
166
|
+
end)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# The approve / reject / redirect flow.
|
|
171
|
+
#
|
|
172
|
+
# @param request [Request]
|
|
173
|
+
# @return [Approved, Rejected, Redirected]
|
|
174
|
+
def ask_plain(request)
|
|
175
|
+
puts '[y] approve [n] reject [r] redirect'
|
|
176
|
+
$stdout.flush
|
|
177
|
+
loop do
|
|
178
|
+
line = $stdin.gets
|
|
179
|
+
return Rejected.new if line.nil?
|
|
180
|
+
|
|
181
|
+
answer = line.strip.downcase
|
|
182
|
+
return Approved.new(new_request_detail: request.detail) if answer == 'y' || answer == 'yes'
|
|
183
|
+
return Rejected.new if answer == 'n' || answer == 'no'
|
|
184
|
+
return read_redirect if answer == 'r' || answer == 'redirect'
|
|
185
|
+
|
|
186
|
+
print 'Please answer y, n, or r: '
|
|
187
|
+
$stdout.flush
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# The editable flow: approve-as-is (clean only), edit, reject, or
|
|
192
|
+
# redirect.
|
|
193
|
+
#
|
|
194
|
+
# @param request [Request]
|
|
195
|
+
# @param clean [Boolean] whether the payload passed the sanitizer
|
|
196
|
+
# (gates the pre-fill and the bare-approve path)
|
|
197
|
+
# @return [Approved, Rejected, Redirected]
|
|
198
|
+
def ask_editable(request, clean:)
|
|
199
|
+
puts(clean ? '[y] approve [e] edit [n] reject [r] redirect' \
|
|
200
|
+
: '⚠ retype required — [e] edit [n] reject [r] redirect')
|
|
201
|
+
$stdout.flush
|
|
202
|
+
loop do
|
|
203
|
+
line = $stdin.gets
|
|
204
|
+
return Rejected.new if line.nil?
|
|
205
|
+
|
|
206
|
+
answer = line.strip.downcase
|
|
207
|
+
if clean && (answer == 'y' || answer == 'yes')
|
|
208
|
+
return Approved.new(new_request_detail: request.detail)
|
|
209
|
+
end
|
|
210
|
+
if answer == 'e' || answer == 'edit'
|
|
211
|
+
return Approved.new(new_request_detail: read_edited(initial: request.detail, prefill: clean))
|
|
212
|
+
end
|
|
213
|
+
return Rejected.new if answer == 'n' || answer == 'no'
|
|
214
|
+
return read_redirect if answer == 'r' || answer == 'redirect'
|
|
215
|
+
|
|
216
|
+
print(clean ? 'Please answer y, e, n, or r: ' : 'Please answer e, n, or r: ')
|
|
217
|
+
$stdout.flush
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Read the human's (re)authored task.
|
|
222
|
+
#
|
|
223
|
+
# @param initial [String] the raw payload to pre-fill with
|
|
224
|
+
# @param prefill [Boolean] +true+ places +initial+ in the editable buffer
|
|
225
|
+
# (clean payloads only); +false+ forces a fresh line — the retype for
|
|
226
|
+
# flagged input, which never puts raw bytes on the terminal.
|
|
227
|
+
# @return [String] the edited text (may be empty)
|
|
228
|
+
def read_edited(initial:, prefill:)
|
|
229
|
+
if prefill
|
|
230
|
+
Reline.pre_input_hook = lambda do
|
|
231
|
+
Reline.insert_text(initial)
|
|
232
|
+
Reline.pre_input_hook = nil
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
(Reline.readline('task> ', false) || '').chomp
|
|
236
|
+
ensure
|
|
237
|
+
Reline.pre_input_hook = nil
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# Read the words a redirect hands the model.
|
|
241
|
+
#
|
|
242
|
+
# @return [Redirected, Rejected] {Rejected} for a blank line or EOF
|
|
243
|
+
def read_redirect
|
|
244
|
+
print 'Tell the agent (blank just rejects): '
|
|
245
|
+
$stdout.flush
|
|
246
|
+
message = $stdin.gets&.strip
|
|
247
|
+
message.nil? || message.empty? ? Rejected.new : Redirected.new(message: message)
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
end
|