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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 120a595848652290fe60f2505a6cf74ff7d8f83764da5a22b89c7f71c95b5796
4
- data.tar.gz: cb6348c77246df64f29b03cdb6d846abbd4bfa97f9c4830c155fd978ea1b65cb
3
+ metadata.gz: 33790a827430e92487bca4a2221245cb72d1ad95eba39deea3e89e9ee75aae0a
4
+ data.tar.gz: f77e2249f51dde56628436d25870e575bae017227593cd9c20c9d27fd8533afb
5
5
  SHA512:
6
- metadata.gz: a3d79a08b112424947f74667a24750e82a86539b61a1edfdbabb2ab2270d610d0d91442c05aab6f186f869dff70376259fecc6172ca60c23d43560797e536de2
7
- data.tar.gz: 1fe906ff062d7dc37e1666f8f2ebf56c18eb3e7bc389fcef5bf8f6334548f1cc10684113f2d1331af11b92befd33ba72f5dac217d42ef02cc668b6f66262f3c6
6
+ metadata.gz: 969d8c3401bbbfcbb1f784652e4a9243cbf33080d59e0ac0af0440991035615f74b1d47f029a3d33e23cc49c44c1feec86df374357b181511f4b4f990abcfe46
7
+ data.tar.gz: 996ff3f297b424ca333d2441d06608ddf810a37515f51e50f0ad8354cd6b3f3a6a67284c496c29f7786bda82abf440eb3a58011f9565f7472d8717216751c35a
data/README.md CHANGED
@@ -1,24 +1,29 @@
1
1
  # pikuri-workspace
2
2
 
3
3
  Filesystem tools + Workspace/Confirmer seams for the
4
- [pikuri](https://codeberg.org/mvysny/pikuri) AI-assistant toolkit.
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, with an
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
10
  - `Pikuri::Workspace::Confirmer` — abstract base for approving
12
11
  user-state mutations, plus `AUTO_APPROVE` (headless) and `Terminal`
13
12
  (stdin/stdout). `Terminal` is written for pikuri's own single-threaded
14
13
  demo scripts — a real app implements this seam itself and reads
15
14
  `Terminal` as the worked example.
16
- - Five file tools: `Pikuri::Workspace::Read`,
15
+ - Six file tools: `Pikuri::Workspace::Read`,
17
16
  `Pikuri::Workspace::Write`, `Pikuri::Workspace::Edit`,
18
- `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).
19
24
 
20
25
  No shell execution — `Pikuri::Code::Bash` ships in
21
- [`pikuri-code`](../pikuri-code/README.md) on top of these.
26
+ [`pikuri-code`](https://github.com/mvysny/pikuri-workshop/blob/master/pikuri-code/README.md) on top of these.
22
27
 
23
28
  ## Install
24
29
 
@@ -44,7 +49,7 @@ agent = Pikuri::Agent.new(
44
49
  c.add_tool Pikuri::Workspace::Read.new(workspace: workspace)
45
50
  c.add_tool Pikuri::Workspace::Grep.new(workspace: workspace)
46
51
  c.add_tool Pikuri::Workspace::Glob.new(workspace: workspace)
47
- c.add_tool Pikuri::Workspace::Edit.new(workspace: workspace)
52
+ c.add_tool Pikuri::Workspace::Edit.new(workspace: workspace, confirmer: confirmer)
48
53
  c.add_tool Pikuri::Workspace::Write.new(workspace: workspace, confirmer: confirmer)
49
54
  c.add_listener ...
50
55
  end
@@ -53,17 +58,15 @@ end
53
58
  `Workspace` is the "look-but-don't-leak" guard around filesystem
54
59
  access. Read tools route through `#resolve_for_read(path)`; mutating
55
60
  tools route through `#resolve_for_write(path)` + the Confirmer's
56
- `#confirm?(request:)`. Pass `temp: true` to mint an ephemeral
57
- writable playground via `Dir.mktmpdir` — its path is exposed as
58
- `workspace.temp` and auto-removed at process exit.
61
+ `#ask(request:, cancellable:)`.
59
62
 
60
63
  ## Further reading
61
64
 
62
65
  - **Narrative walkthrough:** the "Workspace seam" and "Confirmer
63
66
  seam" sections of
64
- [the coding-agent chapter of the pikuri guide](../book/code.md) — what
65
- each kwarg of `Filesystem.new` controls, when to use the
66
- `AllowAll` variant, and how the seams fit alongside `Sandbox`.
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`.
67
70
  - **API reference:** browse the YARD docs at
68
71
  <https://rubydoc.info/gems/pikuri-workspace> (once published),
69
72
  or run `bundle exec yard` in this directory for a local copy.
@@ -36,17 +36,18 @@ module Pikuri
36
36
  #
37
37
  # == The two flows
38
38
  #
39
- # A *non-editable* request (bash, write) gets +(y/n)?+: +y+ → {Approved},
40
- # +n+ → {Rejected} (after an optional reason), EOF → {Rejected}, else
41
- # re-prompt.
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.
42
43
  #
43
44
  # An *editable* request (the +agent+ task) also offers +e+ to edit, and
44
45
  # the warning set gates the pre-fill: the payload sent onward is RAW bytes
45
46
  # but the terminal must *display* sanitized bytes, so pre-filling an
46
47
  # editable buffer with raw bytes would reintroduce the control-byte attack.
47
- # So a *clean* task offers +[y] approve [e] edit [n] reject+ (+e+ opens
48
- # the raw task editable), a *flagged* task drops bare-approve and forces a
49
- # retype: +[e] edit [n] reject+. Honest limit: a terminal can't
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
50
51
  # *structurally* forbid a rubber-stamp +y+ on a clean task — the pure
51
52
  # no-rubber-stamp author seat is a richer-UI invariant; the warning-gated
52
53
  # retype is the terminal's partial answer.
@@ -71,9 +72,16 @@ module Pikuri
71
72
  # whole-file rewrite from scrolling the terminal off-screen.
72
73
  DIFF_MAX_LINES = 300
73
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
+
74
80
  # @param request [Request]
75
- # @return [Approved, Rejected]
76
- def ask(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:)
77
85
  warnings = render(request)
78
86
  if request.editable && !request.detail.nil?
79
87
  ask_editable(request, clean: warnings.empty?)
@@ -153,45 +161,47 @@ module Pikuri
153
161
  puts(case body[0]
154
162
  when '+' then Rainbow(body).green
155
163
  when '-' then Rainbow(body).red
156
- when '@' then Rainbow(body).cyan
164
+ when '@' then Rainbow(body).color(HUNK_COLOR)
157
165
  else Rainbow(body).dimgray
158
166
  end)
159
167
  end
160
168
  end
161
169
 
162
- # The +(y/n)?+ flow, returning {Approved}/{Rejected} and prompting for a
163
- # decline reason on +n+.
170
+ # The approve / reject / redirect flow.
164
171
  #
165
172
  # @param request [Request]
166
- # @return [Approved, Rejected]
173
+ # @return [Approved, Rejected, Redirected]
167
174
  def ask_plain(request)
168
- puts '(y/n)?'
175
+ puts '[y] approve [n] reject [r] redirect'
169
176
  $stdout.flush
170
177
  loop do
171
178
  line = $stdin.gets
172
- return Rejected.new(reason: nil) if line.nil?
179
+ return Rejected.new if line.nil?
173
180
 
174
181
  answer = line.strip.downcase
175
182
  return Approved.new(new_request_detail: request.detail) if answer == 'y' || answer == 'yes'
176
- return Rejected.new(reason: read_reason) if answer == 'n' || answer == 'no'
183
+ return Rejected.new if answer == 'n' || answer == 'no'
184
+ return read_redirect if answer == 'r' || answer == 'redirect'
177
185
 
178
- print 'Please answer y or n: '
186
+ print 'Please answer y, n, or r: '
179
187
  $stdout.flush
180
188
  end
181
189
  end
182
190
 
183
- # The editable flow: approve-as-is (clean only), edit, or reject.
191
+ # The editable flow: approve-as-is (clean only), edit, reject, or
192
+ # redirect.
184
193
  #
185
194
  # @param request [Request]
186
195
  # @param clean [Boolean] whether the payload passed the sanitizer
187
196
  # (gates the pre-fill and the bare-approve path)
188
- # @return [Approved, Rejected]
197
+ # @return [Approved, Rejected, Redirected]
189
198
  def ask_editable(request, clean:)
190
- puts(clean ? '[y] approve [e] edit [n] reject' : '⚠ retype required — [e] edit [n] reject')
199
+ puts(clean ? '[y] approve [e] edit [n] reject [r] redirect' \
200
+ : '⚠ retype required — [e] edit [n] reject [r] redirect')
191
201
  $stdout.flush
192
202
  loop do
193
203
  line = $stdin.gets
194
- return Rejected.new(reason: nil) if line.nil?
204
+ return Rejected.new if line.nil?
195
205
 
196
206
  answer = line.strip.downcase
197
207
  if clean && (answer == 'y' || answer == 'yes')
@@ -200,9 +210,10 @@ module Pikuri
200
210
  if answer == 'e' || answer == 'edit'
201
211
  return Approved.new(new_request_detail: read_edited(initial: request.detail, prefill: clean))
202
212
  end
203
- return Rejected.new(reason: read_reason) if answer == 'n' || answer == 'no'
213
+ return Rejected.new if answer == 'n' || answer == 'no'
214
+ return read_redirect if answer == 'r' || answer == 'redirect'
204
215
 
205
- print(clean ? 'Please answer y, e, or n: ' : 'Please answer e or n: ')
216
+ print(clean ? 'Please answer y, e, n, or r: ' : 'Please answer e, n, or r: ')
206
217
  $stdout.flush
207
218
  end
208
219
  end
@@ -226,18 +237,14 @@ module Pikuri
226
237
  Reline.pre_input_hook = nil
227
238
  end
228
239
 
229
- # Prompt for an optional decline reason (steering handed back to the
230
- # model).
240
+ # Read the words a redirect hands the model.
231
241
  #
232
- # @return [String, nil] the trimmed reason, or +nil+ for blank / EOF
233
- def read_reason
234
- print 'Reason (optional, Enter to skip): '
242
+ # @return [Redirected, Rejected] {Rejected} for a blank line or EOF
243
+ def read_redirect
244
+ print 'Tell the agent (blank just rejects): '
235
245
  $stdout.flush
236
- line = $stdin.gets
237
- return nil if line.nil?
238
-
239
- reason = line.strip
240
- reason.empty? ? nil : reason
246
+ message = $stdin.gets&.strip
247
+ message.nil? || message.empty? ? Rejected.new : Redirected.new(message: message)
241
248
  end
242
249
  end
243
250
  end
@@ -2,14 +2,15 @@
2
2
 
3
3
  module Pikuri
4
4
  module Workspace
5
- # Port for resolving a "may I do this?" confirmation to a decision —
6
- # currently {Pikuri::Code::Bash}, {Write}, and the +agent+ tool's
7
- # delegation gate. Subclass and implement {#ask}.
5
+ # Port for resolving a "may I do this?" confirmation to a decision — in
6
+ # pikuri, {Write}, the +agent+ tool's delegation gate and +web_search+'s
7
+ # query gate; downstream, a shell tool's commands. Subclass and implement
8
+ # {#ask}.
8
9
  #
9
10
  # == The one job: resolve a {Request} to a decision
10
11
  #
11
- # A Confirmer takes a semantic {Request} and returns {Approved} or
12
- # {Rejected}. That is *all*: it carries **no tool policy** and never
12
+ # A Confirmer takes a semantic {Request} and returns {Approved},
13
+ # {Rejected} or {Redirected}. That is *all*: it carries **no tool policy** and never
13
14
  # classifies the request's *content* (no filesystem reads, no command
14
15
  # parsing, no notion of "passive"). Two shipped implementations:
15
16
  #
@@ -29,32 +30,45 @@ module Pikuri
29
30
  # down here.
30
31
  #
31
32
  # Content classification ("is this bash command provably passive, skip the
32
- # prompt?") deliberately does *not* live here — it's a tool concern
33
- # ({Pikuri::Code::Bash} takes a separate +passive_detector:+ predicate,
34
- # falling through to this confirmer only for commands it can't pre-approve).
33
+ # prompt?") deliberately does *not* live here — it's a tool concern (a
34
+ # shell tool takes its own "provably passive" predicate, falling through to
35
+ # this confirmer only for commands it can't pre-approve).
35
36
  # Keeping them apart lets one stateless chrome serve every tool.
36
37
  #
37
- # == The return type: {Approved} / {Rejected}
38
+ # == The return type: {Approved} / {Rejected} / {Redirected}
38
39
  #
39
- # {#ask} returns {Approved} (the possibly-edited {Request#detail} to act on)
40
- # or {Rejected} (an optional decline reason); callers pattern-match:
40
+ # The human's gesture picks one of three; callers pattern-match, and a
41
+ # decline answers the model through the decision's own {Rejected#observation}
42
+ # / {Redirected#observation}:
41
43
  #
42
- # case confirmer.ask(request: request)
44
+ # case decision = confirmer.ask(request: request, cancellable: context.cancellable)
43
45
  # in Confirmer::Approved(new_request_detail:) then ...act on it...
44
- # in Confirmer::Rejected(reason:) then ...steer with it...
46
+ # in Confirmer::Rejected | Confirmer::Redirected
47
+ # return decision.observation('the bash command')
45
48
  # end
46
49
  #
47
- # {#confirm?} is the derived boolean convenience for gates ({Write},
48
- # {Pikuri::Code::ExitPlanMode}) needing neither the payload nor the reason.
49
- # Two orthogonal axes ride the richer return:
50
+ # * {Approved} — approve, carrying the possibly-edited {Request#detail}.
51
+ # * {Rejected} — a plain "no": final for this action, the model stops.
52
+ # * {Redirected} — the human typed words ("make it Wednesday", "too
53
+ # risky"): the model follows them, and may propose again — which asks again.
50
54
  #
51
- # 1. *Editing* (opt-in via {Request#editable}) — when +detail+ is the
52
- # verbatim actionable payload (the +agent+ task, where +detail == task+),
53
- # a confirmer may let the human *rewrite* it — "human as author, not
54
- # approver". Bash's +"$ <command>"+ is a decorated display string, so it
55
- # stays non-editable.
56
- # 2. *Reason* (universal) — any decline can carry typed steering ("use the
57
- # other library") back to the model as the next observation.
55
+ # Editing is opt-in via {Request#editable}: when +detail+ is the verbatim
56
+ # actionable payload (the +agent+ task), a confirmer may let the human
57
+ # *rewrite* it — "human as author, not approver". Bash's +"$ <command>"+ is
58
+ # a decorated display string, so it stays non-editable.
59
+ #
60
+ # == Cancelling a parked ask
61
+ #
62
+ # {#ask} gets the turn's {Pikuri::Agent::Control::Cancellable}, which the
63
+ # tool takes from its {Pikuri::Tool::ExecuteContext}. A confirmer whose
64
+ # human answers on another thread waits in a
65
+ # {Pikuri::Agent::Control::Handoff} built on it, so any +cancel!+ — the
66
+ # cancel key, a shutdown, another tab — makes the parked {#ask} raise
67
+ # {Pikuri::Agent::Control::Cancellable::Cancelled}: the tool lets it
68
+ # through, the agent ends the turn, and the stranded call is answered
69
+ # +[Cancelled by the user]+. Answering {Rejected} instead is not a cancel:
70
+ # the turn runs on for one more model reply. {Terminal} ignores the token —
71
+ # Ctrl+C reaches its +gets+ as +SIGINT+. +D_confirmer_sees_the_cancel+.
58
72
  #
59
73
  # A +:once+/+:always+ scope knob was *rejected*: it manages decision
60
74
  # *fatigue*, a distinct axis, and the long-term answer to fatigue is making
@@ -111,28 +125,57 @@ module Pikuri
111
125
  # edited) {Request#detail} when editable, else +detail+ verbatim.
112
126
  Approved = Data.define(:new_request_detail)
113
127
 
114
- # A decline carrying the human's optional steering reason (+nil+ if none),
115
- # which the tool folds into the observation handed back to the model.
116
- Rejected = Data.define(:reason)
128
+ # A plain "no", final for this action. It carries no text on purpose:
129
+ # the model sees only the observation, and any words after "declined"
130
+ # read as a hint to work around — every typed word is a {Redirected}.
131
+ # See +D_bare_rejected+.
132
+ Rejected = Data.define do
133
+ # The tool's answer to the model, telling it to stop:
134
+ #
135
+ # Rejected.new.observation('the write to a.txt').content
136
+ # # => "Error: user declined the write to a.txt. Don't retry it or reach the
137
+ # # same end another way; tell the user you stopped."
138
+ #
139
+ # @param action [String] what was declined, as a noun phrase
140
+ # @return [Pikuri::Tool::Result] an error result
141
+ def observation(action)
142
+ Pikuri::Tool::Result.error("user declined #{action}. Don't retry it or reach " \
143
+ 'the same end another way; tell the user you stopped.')
144
+ end
145
+ end
146
+
147
+ # A decline carrying the words the human typed — a different way, or why
148
+ # not. The model follows them, and may propose again.
149
+ Redirected = Data.define(:message) do
150
+ # The tool's answer to the model, quoting the human — deliberately not
151
+ # an error, which would read as a failure to recover from:
152
+ #
153
+ # Redirected.new(message: 'use vim').observation('the write to a.txt').content
154
+ # # => "The user declined the write to a.txt and said: use vim"
155
+ #
156
+ # @param action [String] what was declined, as a noun phrase
157
+ # @return [Pikuri::Tool::Result]
158
+ def observation(action)
159
+ Pikuri::Tool::Result.new(content: "The user declined #{action} and said: #{message}",
160
+ summary: 'Redirected by the user')
161
+ end
162
+ end
117
163
 
118
164
  # @param request [Request] semantic content composed by the calling
119
165
  # tool. The confirmer renders it (escaping for its medium), poses
120
166
  # the question, and parses the answer.
121
- # @return [Approved, Rejected]
167
+ # @param cancellable [Pikuri::Agent::Control::Cancellable] the asking
168
+ # turn's token. Per call, never per confirmer: one confirmer serves
169
+ # many agents, each with its own.
170
+ # @return [Approved, Rejected, Redirected]
122
171
  # @raise [NotImplementedError] in the abstract base
123
- def ask(request:)
172
+ # @raise [Pikuri::Agent::Control::Cancellable::Cancelled] when the turn
173
+ # is cancelled while the question waits — see "Cancelling a parked
174
+ # ask" above
175
+ def ask(request:, cancellable:)
124
176
  raise NotImplementedError, "#{self.class}#ask must be implemented"
125
177
  end
126
178
 
127
- # Boolean convenience over {#ask} for gates needing neither the edited
128
- # payload nor the reason.
129
- #
130
- # @param request [Request]
131
- # @return [Boolean] +true+ iff approved
132
- def confirm?(request:)
133
- ask(request:).is_a?(Approved)
134
- end
135
-
136
179
  # Whether {#ask} can actually stop and wait for a person — the posture
137
180
  # {Pikuri::Trifecta} reads when deciding whether a gated tool reaches the
138
181
  # approver seat rather than counting as autonomous egress.
@@ -150,12 +193,13 @@ module Pikuri
150
193
 
151
194
  # Approves everything unedited — the headless "opted out of dialogs"
152
195
  # stance. Request-independent, so it classifies nothing and stays a
153
- # legitimate Confirmer. Used by +bin/pikuri-code --yolo+, downstream hosts'
154
- # yolo modes, and specs that don't want to coordinate stdin.
196
+ # legitimate Confirmer. Used by hosts' yolo modes and specs that don't want to coordinate stdin.
155
197
  class AutoApprove < Confirmer
156
198
  # @param request [Request]
199
+ # @param cancellable [Pikuri::Agent::Control::Cancellable] unused —
200
+ # nothing waits
157
201
  # @return [Approved] always, carrying +detail+ unchanged
158
- def ask(request:)
202
+ def ask(request:, cancellable:)
159
203
  Approved.new(new_request_detail: request.detail)
160
204
  end
161
205