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
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pikuri
|
|
4
|
+
module Workspace
|
|
5
|
+
module Search
|
|
6
|
+
# The two guards every path-listing tool needs and none should own: an
|
|
7
|
+
# oversized-root refusal, and a byte cap on the rendered result.
|
|
8
|
+
module Utils
|
|
9
|
+
# Guard against a search/list rooted at +/+, the user's whole home dir,
|
|
10
|
+
# or any parent of home (+/home+, which holds *every* user's home) —
|
|
11
|
+
# each walks an enormous tree and, under a +readable:+ root that broad,
|
|
12
|
+
# floods rg with unreadable-path diagnostics the model pays context for. The
|
|
13
|
+
# refused set is home and everything at-or-above it: blocking +~+ while
|
|
14
|
+
# waving through its strictly-broader parent +/home+ would be no guard
|
|
15
|
+
# at all. Refused regardless of any glob: rg traverses the tree *before*
|
|
16
|
+
# the glob narrows which files it searches. Lesser roots (a project dir,
|
|
17
|
+
# +~/work+) return +nil+ and proceed.
|
|
18
|
+
#
|
|
19
|
+
# Rather than dead-end at "narrow it", the refusal hands back a
|
|
20
|
+
# one-level listing of the root's own entries, so the model can pick a
|
|
21
|
+
# subdirectory and retry *without* another tool call or a shell:
|
|
22
|
+
#
|
|
23
|
+
# Error: refusing to search from /home/alice — the whole home
|
|
24
|
+
# directory is too broad. Pick one of its entries below as `path` and
|
|
25
|
+
# search there instead (directories end with `/`):
|
|
26
|
+
# .config/ [3 dirs, 2 files]
|
|
27
|
+
# .ssh/ [denied]
|
|
28
|
+
# work/ [12 dirs, 1 file]
|
|
29
|
+
# todo.txt 412B
|
|
30
|
+
#
|
|
31
|
+
# The listing is deliberately one level deep: rendering a tree would
|
|
32
|
+
# re-do the very traversal this guard exists to prevent. (The child
|
|
33
|
+
# summary does open each entry once, but that's bounded by the root's
|
|
34
|
+
# own entry count — nothing like rg's walk.) It falls back to a plain
|
|
35
|
+
# "narrow `path`" line if the directory is empty or unreadable.
|
|
36
|
+
#
|
|
37
|
+
# @param resolved [Pathname] the absolute search root
|
|
38
|
+
# @param filesystem [Filesystem] threaded into the listing so a
|
|
39
|
+
# credential dir under the refused root renders +[denied]+
|
|
40
|
+
# @param verb [String] the tool's action word for the message, e.g.
|
|
41
|
+
# +"search"+ (Grep) or +"list"+ (Glob)
|
|
42
|
+
# @return [String, nil] the +"Error: ..."+ refusal, or +nil+ to proceed
|
|
43
|
+
def self.oversized_root_refusal(resolved, filesystem:, verb:)
|
|
44
|
+
normalized = normalize_root(resolved)
|
|
45
|
+
home = normalize_root(Pathname.new(Dir.home))
|
|
46
|
+
root_kind =
|
|
47
|
+
if normalized == normalize_root(Pathname.new('/'))
|
|
48
|
+
'the whole filesystem root'
|
|
49
|
+
elsif normalized == home
|
|
50
|
+
'the whole home directory'
|
|
51
|
+
elsif home.start_with?(normalized + File::SEPARATOR)
|
|
52
|
+
# e.g. /home — a parent of ~ is strictly broader than ~ itself.
|
|
53
|
+
# (+/+ is a parent too, but its "//"-prefix test never matches,
|
|
54
|
+
# so it stays owned by the first branch and its clearer message.)
|
|
55
|
+
'a parent of your home directory'
|
|
56
|
+
end
|
|
57
|
+
return nil unless root_kind
|
|
58
|
+
|
|
59
|
+
listing = root_listing(normalized, filesystem: filesystem)
|
|
60
|
+
guidance =
|
|
61
|
+
if listing.empty?
|
|
62
|
+
'Narrow `path` to a specific subdirectory.'
|
|
63
|
+
else
|
|
64
|
+
"Pick one of its entries below as `path` and #{verb} there " \
|
|
65
|
+
"instead (directories end with `/`):\n#{listing}"
|
|
66
|
+
end
|
|
67
|
+
"Error: refusing to #{verb} from #{resolved} — #{root_kind} is too " \
|
|
68
|
+
"broad. #{guidance}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# @param path [String, Pathname]
|
|
72
|
+
# @return [String] symlink-resolved absolute path; falls back to a
|
|
73
|
+
# lexical cleanup when the path doesn't exist on disk.
|
|
74
|
+
def self.normalize_root(path)
|
|
75
|
+
Pathname.new(path).realpath.to_s
|
|
76
|
+
rescue SystemCallError
|
|
77
|
+
Pathname.new(path).expand_path.cleanpath.to_s
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Head-truncate +raw+ to +max_bytes+, cutting at the last newline so
|
|
81
|
+
# the final row is never partial.
|
|
82
|
+
#
|
|
83
|
+
# @param raw [String]
|
|
84
|
+
# @param max_bytes [Integer]
|
|
85
|
+
# @param hint [String] what the caller should change to get less
|
|
86
|
+
# output, e.g. +"refine pattern or path"+
|
|
87
|
+
# @return [Array(String, String)] the content and a marker (+""+ when
|
|
88
|
+
# nothing was cut)
|
|
89
|
+
def self.head_truncate(raw, max_bytes:, hint:)
|
|
90
|
+
total = raw.bytesize
|
|
91
|
+
return [raw, ''] if total <= max_bytes
|
|
92
|
+
|
|
93
|
+
head = raw.byteslice(0, max_bytes)
|
|
94
|
+
last_nl = head.rindex("\n")
|
|
95
|
+
head = head.byteslice(0, last_nl) if last_nl
|
|
96
|
+
omitted = total - head.bytesize
|
|
97
|
+
marker = "\n\n... [#{omitted} bytes omitted; total was #{total} bytes; " \
|
|
98
|
+
"#{hint}] ..."
|
|
99
|
+
[head, marker]
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Search-root-relative paths of every denied root that falls *under*
|
|
103
|
+
# +root+ — the only ones an rg walk rooted there could reach.
|
|
104
|
+
#
|
|
105
|
+
# denied_prefixes(filesystem: fs, root: Pathname.new('/home/alice'))
|
|
106
|
+
# # => [".aws", ".gnupg", ".ssh"]
|
|
107
|
+
#
|
|
108
|
+
# @param filesystem [Filesystem] supplies {Filesystem#denied_roots}
|
|
109
|
+
# @param root [Pathname] the rg search root
|
|
110
|
+
# @return [Array<String>] sorted, +""+-free relative paths
|
|
111
|
+
def self.denied_prefixes(filesystem:, root:)
|
|
112
|
+
base = normalize_root(root)
|
|
113
|
+
filesystem.denied_roots.filter_map { |denied|
|
|
114
|
+
absolute = denied.exist? ? denied.realpath.to_s : denied.to_s
|
|
115
|
+
next unless absolute.start_with?(base + File::SEPARATOR)
|
|
116
|
+
|
|
117
|
+
absolute.delete_prefix(base + File::SEPARATOR)
|
|
118
|
+
}.sort
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# +rg --glob '!<rel>'+ for each denied root under +root+, pruning the
|
|
122
|
+
# subtree before rg ever walks it. Verified: a negative glob on a
|
|
123
|
+
# directory prunes its contents and survives +--no-ignore+, the same
|
|
124
|
+
# property {Glob}'s +!.git/*+ relies on.
|
|
125
|
+
#
|
|
126
|
+
# This is the *optimization*, never the guarantee — it goes silently
|
|
127
|
+
# partial whenever a denied root sits outside the search root, and an
|
|
128
|
+
# rg flag change could weaken it without a test noticing. The
|
|
129
|
+
# {Filesystem#denied?} filter on the results is what fails closed.
|
|
130
|
+
#
|
|
131
|
+
# @param filesystem [Filesystem]
|
|
132
|
+
# @param root [Pathname] the rg search root
|
|
133
|
+
# @return [Array<String>] argv fragments, empty when nothing is denied
|
|
134
|
+
def self.denied_globs(filesystem:, root:)
|
|
135
|
+
denied_prefixes(filesystem: filesystem, root: root)
|
|
136
|
+
.flat_map { |rel| ['--glob', "!#{rel}"] }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Drop every output line rg emitted for a denied path. rg puts the path
|
|
140
|
+
# first on the line and never follows symlinks (no +-L+ in either
|
|
141
|
+
# tool's argv), so a leading-prefix test is exact for the path segment
|
|
142
|
+
# — no need to find where the path ends, which a filename containing
|
|
143
|
+
# +:+ would make ambiguous. It may over-match (a +.netrc.bak+ line goes
|
|
144
|
+
# too); over-filtering is the safe direction for a credential guard.
|
|
145
|
+
#
|
|
146
|
+
# @param raw [String] rg output, +./+ prefixes already stripped
|
|
147
|
+
# @param prefixes [Array<String>] from {.denied_prefixes}
|
|
148
|
+
# @return [String]
|
|
149
|
+
def self.reject_denied_lines(raw, prefixes:)
|
|
150
|
+
return raw if prefixes.empty?
|
|
151
|
+
|
|
152
|
+
raw.lines.reject { |line|
|
|
153
|
+
prefixes.any? { |rel| line.start_with?(rel) }
|
|
154
|
+
}.join
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# A refusal still has to come back when the root itself can't be
|
|
158
|
+
# opened, so that degrades to the "narrow `path`" wording rather than
|
|
159
|
+
# raising out of a message-building helper.
|
|
160
|
+
#
|
|
161
|
+
# @param dir [String] absolute directory path
|
|
162
|
+
# @param filesystem [Filesystem]
|
|
163
|
+
# @return [String] {Pikuri::Workspace::Listing.render} at depth 1, or +""+
|
|
164
|
+
def self.root_listing(dir, filesystem:)
|
|
165
|
+
Listing.render(dir, filesystem: filesystem)
|
|
166
|
+
rescue SystemCallError
|
|
167
|
+
''
|
|
168
|
+
end
|
|
169
|
+
private_class_method :root_listing
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'forwardable'
|
|
4
|
+
require 'pathname'
|
|
5
|
+
require 'set'
|
|
6
|
+
|
|
7
|
+
module Pikuri
|
|
8
|
+
module Workspace
|
|
9
|
+
# The composite the file tools operate against: a {Filesystem} (path
|
|
10
|
+
# resolution, containment, sandboxing) paired with this session's read
|
|
11
|
+
# record (which files the agent has read — the runtime half of the +read+
|
|
12
|
+
# tool's read-before-edit contract). Nested under +Pikuri::Workspace+ rather
|
|
13
|
+
# than promoting that module to a class.
|
|
14
|
+
#
|
|
15
|
+
# == Why a composite, not read-state on Filesystem
|
|
16
|
+
#
|
|
17
|
+
# A filesystem resolves paths; it doesn't track who read what. Hanging the
|
|
18
|
+
# read record off {Filesystem} would make it a hidden passenger on a type
|
|
19
|
+
# whose value is being an immutable path-policy description. Composing them
|
|
20
|
+
# here keeps {Filesystem} pure and makes the read-state visible in the type.
|
|
21
|
+
# The record is just a +Set+ of relative path strings held inline.
|
|
22
|
+
#
|
|
23
|
+
# == Facade over Filesystem
|
|
24
|
+
#
|
|
25
|
+
# The file tools only +resolve_for_read+/+resolve_for_write+ a path and
|
|
26
|
+
# (for Grep/Glob's chdir) read +project_root+ — all delegated straight to
|
|
27
|
+
# the wrapped {Filesystem}, so a tool that took a +Filesystem+ works
|
|
28
|
+
# unchanged; the only new surface is {#mark_read}/{#read?}. (Bash keeps
|
|
29
|
+
# taking the raw {Filesystem} — it's not a read/edit/write tool.)
|
|
30
|
+
#
|
|
31
|
+
# == Read-record keys are workspace-relative
|
|
32
|
+
#
|
|
33
|
+
# {#mark_read}/{#read?} take an absolute resolved {Pathname} and relativize
|
|
34
|
+
# it against +project_root+ before recording/querying. Relative keys keep the
|
|
35
|
+
# record scoped to this one workspace (a path under a readable root outside
|
|
36
|
+
# the project relativizes to a +../..+ form — still unique). Each workspace
|
|
37
|
+
# owns its own record, so a read in one (a sub-agent's temp clone) never
|
|
38
|
+
# satisfies an edit in another.
|
|
39
|
+
#
|
|
40
|
+
# Sharing: +P_one_agent+ — the read record is one conversation's, and an
|
|
41
|
+
# agent is one conversation (+D_agent_is_one_conversation+). Two agents on one
|
|
42
|
+
# composite would satisfy each other's read-before-edit gate: agent B could
|
|
43
|
+
# {Edit} a file only agent A had read, which is the gate failing open.
|
|
44
|
+
# {Filesystem} underneath is immutable policy and shares freely; give each
|
|
45
|
+
# agent its own +Workspace+ over the same +Filesystem+.
|
|
46
|
+
class Workspace
|
|
47
|
+
extend Forwardable
|
|
48
|
+
|
|
49
|
+
# @return [Filesystem] the wrapped path-resolution / sandboxing object.
|
|
50
|
+
attr_reader :filesystem
|
|
51
|
+
|
|
52
|
+
# The Filesystem surface the file tools use, forwarded so the composite is
|
|
53
|
+
# a drop-in for a bare {Filesystem} at those call sites.
|
|
54
|
+
def_delegators :@filesystem, :resolve_for_read, :resolve_for_write, :project_root
|
|
55
|
+
|
|
56
|
+
# @param filesystem [Filesystem] path resolution / sandboxing.
|
|
57
|
+
def initialize(filesystem:)
|
|
58
|
+
@filesystem = filesystem
|
|
59
|
+
@read = Set.new
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Record that +resolved+ has been read this session.
|
|
63
|
+
#
|
|
64
|
+
# @param resolved [Pathname] absolute, workspace-resolved path
|
|
65
|
+
# (as returned by +resolve_for_read+ / +resolve_for_write+).
|
|
66
|
+
# @return [void]
|
|
67
|
+
def mark_read(resolved)
|
|
68
|
+
@read.add(relativize(resolved))
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# @param resolved [Pathname] absolute, workspace-resolved path.
|
|
73
|
+
# @return [Boolean] whether +resolved+ has been read this session.
|
|
74
|
+
def read?(resolved)
|
|
75
|
+
@read.include?(relativize(resolved))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# The error text for a path that doesn't exist, naming the root that
|
|
79
|
+
# relative paths resolve against. A model holding a path from another
|
|
80
|
+
# tool — a corpus citation relative to some other folder, say — can't
|
|
81
|
+
# otherwise tell why it missed, and this shows it what to prepend:
|
|
82
|
+
#
|
|
83
|
+
# workspace.not_found('xyz/spec.txt')
|
|
84
|
+
# # => "file not found: xyz/spec.txt (relative paths resolve against /home/alice/Notes)"
|
|
85
|
+
#
|
|
86
|
+
# @param path [String] the path as the model gave it.
|
|
87
|
+
# @return [String]
|
|
88
|
+
def not_found(path)
|
|
89
|
+
"file not found: #{path} (relative paths resolve against #{project_root})"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
private
|
|
93
|
+
|
|
94
|
+
# Absolute resolved path → workspace-relative key. Both are post-realpath
|
|
95
|
+
# absolute, so +relative_path_from+ always succeeds on POSIX (a +../..+
|
|
96
|
+
# form for paths outside the root, no raise).
|
|
97
|
+
#
|
|
98
|
+
# @param resolved [Pathname, String]
|
|
99
|
+
# @return [String]
|
|
100
|
+
def relativize(resolved)
|
|
101
|
+
Pathname.new(resolved).relative_path_from(@filesystem.project_root).to_s
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
@@ -4,53 +4,61 @@ require 'fileutils'
|
|
|
4
4
|
|
|
5
5
|
module Pikuri
|
|
6
6
|
module Workspace
|
|
7
|
-
# The +write+ tool
|
|
8
|
-
#
|
|
9
|
-
# tool
|
|
10
|
-
#
|
|
11
|
-
# Same shape as {Read} — workspace and confirmer are captured by
|
|
12
|
-
# the +execute+ closure at construction.
|
|
7
|
+
# The +write+ tool as a {Pikuri::Tool} subclass:
|
|
8
|
+
# +Write.new(workspace: ws, confirmer: c)+ produces a tool wired like any
|
|
9
|
+
# bundled tool's. Workspace and confirmer are captured by the +execute+
|
|
10
|
+
# closure.
|
|
13
11
|
#
|
|
14
12
|
# == Policy
|
|
15
13
|
#
|
|
16
|
-
# Three branches
|
|
14
|
+
# Three branches on +path+'s on-disk state:
|
|
17
15
|
#
|
|
18
|
-
# 1. *New file* —
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
# is byte-strict,
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
# +"OK to overwrite <path>: <old> → <new> bytes?"+ via {Confirmer};
|
|
27
|
-
# on yes, write. On no, return a decline-Error observation.
|
|
16
|
+
# 1. *New file* — confirm +"OK to create <path> (<n> bytes)?"+ via
|
|
17
|
+
# {WriteGate}; under +confirm_all_writes: false+, write with no prompt.
|
|
18
|
+
# 2. *Existing, identical content* — return an +"Error: ..."+ no-op
|
|
19
|
+
# *before* the confirmer (don't ask to approve a no-change write).
|
|
20
|
+
# Comparison is byte-strict, BINARY (trailing-newline diffs still confirm).
|
|
21
|
+
# 3. *Existing, differs* — confirm +"OK to overwrite <path>: <old> → <new>
|
|
22
|
+
# bytes?"+ via {WriteGate}; on yes write, on no (or unwritable target)
|
|
23
|
+
# return the matching +"Error: ..."+.
|
|
28
24
|
#
|
|
29
|
-
#
|
|
25
|
+
# An overwrite *always* routes through {WriteGate} (no separate
|
|
26
|
+
# promptless-confirm path), so it gets the diff payload, decline-reason
|
|
27
|
+
# steering, and unwritable-target short-circuit in every posture.
|
|
30
28
|
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
29
|
+
# == Confirm-all-writes (the default)
|
|
30
|
+
#
|
|
31
|
+
# Write.new(workspace: ws, confirmer: c) # every write asks
|
|
32
|
+
# Write.new(workspace: ws, confirmer: c, confirm_all_writes: false) # only overwrites ask
|
|
33
|
+
#
|
|
34
|
+
# +confirm_all_writes+ scopes *which* writes are gated, not *how* (overwrites
|
|
35
|
+
# use {WriteGate} regardless). +true+ also gates the new-file branch, so
|
|
36
|
+
# every writable write confirms and an unwritable target short-circuits
|
|
37
|
+
# without prompting. +false+ writes new files silently — the coding-agent
|
|
38
|
+
# posture, where new files are routine and git is the undo. See
|
|
39
|
+
# book/touching-files.md.
|
|
40
|
+
#
|
|
41
|
+
# == Why an overwrite always asks (Edit can opt out)
|
|
42
|
+
#
|
|
43
|
+
# Under +confirm_all_writes: false+ Write still confirms an overwrite where
|
|
44
|
+
# {Edit} goes promptless — the two-guard asymmetry (the shared
|
|
45
|
+
# read-before-edit gate plus Edit's +old_string+ content-check, which Write
|
|
46
|
+
# lacks so a hallucinated +content+ could clobber the file) is in
|
|
47
|
+
# +pikuri-workspace/design/architecture.md+.
|
|
37
48
|
#
|
|
38
49
|
# == Side effects
|
|
39
50
|
#
|
|
40
|
-
# Parent
|
|
41
|
-
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
# for v1 — users have version control.
|
|
51
|
+
# Parent dirs are created (+FileUtils.mkdir_p+) before the write (the
|
|
52
|
+
# +git add lib/new/dir/foo.rb+ mental model). If +mkdir_p+ succeeds but the
|
|
53
|
+
# write fails, an empty dir is left — accepted for v1 (users have git). No
|
|
54
|
+
# atomic temp+rename; plain +File.write+ — crash safety is "the user has git".
|
|
45
55
|
#
|
|
46
|
-
#
|
|
47
|
-
#
|
|
56
|
+
# Sharing: +P_one_agent+ — it consults one {Workspace}'s read record. The
|
|
57
|
+
# {Confirmer} is the sharper limit, though: {Confirmer::Terminal} cannot
|
|
58
|
+
# serve two agents at once (see its own +== Sharing+).
|
|
48
59
|
class Write < Pikuri::Tool
|
|
49
|
-
# Description shown to the LLM
|
|
50
|
-
#
|
|
51
|
-
# convention. Per-parameter constraints live in the parameter
|
|
52
|
-
# descriptions; the +Usage:+ bullets are for "when do I pick this?
|
|
53
|
-
# how does it chain with other tools?".
|
|
60
|
+
# Description shown to the LLM (opencode-shape). Per-parameter constraints
|
|
61
|
+
# live in the parameter descriptions.
|
|
54
62
|
#
|
|
55
63
|
# @return [String]
|
|
56
64
|
DESCRIPTION = <<~DESC
|
|
@@ -58,18 +66,21 @@ module Pikuri
|
|
|
58
66
|
|
|
59
67
|
Usage:
|
|
60
68
|
- Use for new files or full-file rewrites; for partial changes use `edit` instead.
|
|
61
|
-
- Overwriting an existing file requires user confirmation; identical content is rejected as a no-op error — if you see that error, re-read the file rather than trying again.
|
|
69
|
+
- Overwriting an existing file requires that you have read it this conversation, and then user confirmation; identical content is rejected as a no-op error — if you see that error, re-read the file rather than trying again.
|
|
62
70
|
- Parent directories are created automatically (mkdir -p).
|
|
63
71
|
- Writes the exact bytes supplied: no trailing-newline normalization, no encoding conversion.
|
|
64
72
|
- Paths outside the workspace are refused.
|
|
65
73
|
DESC
|
|
66
74
|
|
|
67
|
-
# @param workspace [
|
|
68
|
-
#
|
|
69
|
-
#
|
|
70
|
-
#
|
|
75
|
+
# @param workspace [Workspace] captured for path resolution, the
|
|
76
|
+
# read-before-overwrite gate, and read-ledger marking
|
|
77
|
+
# (+resolve_for_write+).
|
|
78
|
+
# @param confirmer [Confirmer] consulted (via {WriteGate}) before every
|
|
79
|
+
# write, or only before an overwrite under +confirm_all_writes: false+.
|
|
80
|
+
# @param confirm_all_writes [Boolean] +false+ writes new files silently;
|
|
81
|
+
# overwrites confirm either way.
|
|
71
82
|
# @return [Write]
|
|
72
|
-
def initialize(workspace:, confirmer:)
|
|
83
|
+
def initialize(workspace:, confirmer:, confirm_all_writes: true)
|
|
73
84
|
super(
|
|
74
85
|
name: 'write',
|
|
75
86
|
description: DESCRIPTION,
|
|
@@ -82,67 +93,109 @@ module Pikuri
|
|
|
82
93
|
'Full contents to write to the file, e.g. ' \
|
|
83
94
|
'"class Foo\nend\n".'
|
|
84
95
|
},
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
96
|
+
wants_context: true,
|
|
97
|
+
execute: ->(path:, content:, context:) {
|
|
98
|
+
Write.write(workspace: workspace, confirmer: confirmer,
|
|
99
|
+
confirm_all_writes: confirm_all_writes,
|
|
100
|
+
path: path, content: content, cancellable: context.cancellable)
|
|
101
|
+
},
|
|
102
|
+
# No legs: no file bytes enter the model's context (the observation is
|
|
103
|
+
# an outcome), and a local write is not an external sink.
|
|
104
|
+
#
|
|
105
|
+
# Don't add an egress leg here for synced workspaces. Personal sync
|
|
106
|
+
# keeps one owner, so it is no leg at all; an off-owner root (team
|
|
107
|
+
# Drive, public link) is real egress, unknowable to the framework, and
|
|
108
|
+
# the host that knows declares it via Extension#trifecta_contribution
|
|
109
|
+
# (`D_write_custody_axis`).
|
|
110
|
+
trifecta_legs: Tool::TrifectaLegs::NONE
|
|
88
111
|
)
|
|
89
112
|
end
|
|
90
113
|
|
|
91
|
-
# Resolve +path+ against +workspace+, apply the three-branch policy
|
|
92
|
-
#
|
|
93
|
-
# observation or an +"Error: ..."+ observation.
|
|
114
|
+
# Resolve +path+ against +workspace+, apply the three-branch policy, and
|
|
115
|
+
# return a success or +"Error: ..."+ observation.
|
|
94
116
|
#
|
|
95
|
-
# @param workspace [
|
|
117
|
+
# @param workspace [Workspace]
|
|
96
118
|
# @param confirmer [Confirmer]
|
|
97
|
-
# @param path [String] raw path
|
|
119
|
+
# @param path [String] raw path from the LLM
|
|
98
120
|
# @param content [String] bytes to write
|
|
99
|
-
# @
|
|
100
|
-
|
|
121
|
+
# @param confirm_all_writes [Boolean] see {#initialize}.
|
|
122
|
+
# @param cancellable [Pikuri::Agent::Control::Cancellable] the calling
|
|
123
|
+
# turn's token, for the confirmation.
|
|
124
|
+
# @return [Tool::Result] +"Created, 88 lines"+ / +"Updated, 88 lines (was
|
|
125
|
+
# 90)"+ on a write; {Pikuri::Tool::Result.error} for a refusal or the
|
|
126
|
+
# confirmer's decline. The line counts rather than a diff: pikuri has no
|
|
127
|
+
# in-process differ (a diff-capable chrome renders the
|
|
128
|
+
# {Confirmer::Change} itself).
|
|
129
|
+
def self.write(workspace:, confirmer:, path:, content:, cancellable:, confirm_all_writes:)
|
|
101
130
|
resolved = workspace.resolve_for_write(path)
|
|
102
131
|
|
|
103
132
|
if resolved.exist?
|
|
104
|
-
return "
|
|
133
|
+
return Tool::Result.error("#{path} is a directory") if resolved.directory?
|
|
134
|
+
|
|
135
|
+
unless workspace.read?(resolved)
|
|
136
|
+
return Tool::Result.error("#{path} exists but has not been read this conversation; " \
|
|
137
|
+
'use the read tool first so you do not clobber unread changes.')
|
|
138
|
+
end
|
|
105
139
|
|
|
106
140
|
existing = read_for_compare(resolved, path)
|
|
107
141
|
|
|
108
142
|
if existing == content.b
|
|
109
|
-
return "
|
|
110
|
-
|
|
111
|
-
|
|
143
|
+
return Tool::Result.error("#{path} already contains exactly this content — " \
|
|
144
|
+
'no write needed. If you intended a change, re-read the ' \
|
|
145
|
+
'file and try again.')
|
|
112
146
|
end
|
|
113
147
|
|
|
114
|
-
|
|
115
|
-
|
|
148
|
+
gate = WriteGate.check(
|
|
149
|
+
confirmer: confirmer, path: path, resolved: resolved,
|
|
150
|
+
question: "OK to overwrite #{path}: #{existing.bytesize} → #{content.bytesize} bytes?",
|
|
151
|
+
change: Confirmer::Change.new(path: path, old: existing, new: content.b),
|
|
152
|
+
cancellable: cancellable
|
|
116
153
|
)
|
|
117
|
-
return
|
|
154
|
+
return gate if gate
|
|
118
155
|
|
|
119
156
|
write_bytes(resolved, content)
|
|
120
|
-
|
|
157
|
+
workspace.mark_read(resolved)
|
|
158
|
+
Tool::Result.new(
|
|
159
|
+
content: "Updated #{path} (#{existing.bytesize} → #{content.bytesize} bytes)",
|
|
160
|
+
summary: "Updated, #{Pikuri::Tool.pluralize(content.lines.size, 'line')} " \
|
|
161
|
+
"(was #{existing.lines.size})"
|
|
162
|
+
)
|
|
121
163
|
else
|
|
164
|
+
if confirm_all_writes
|
|
165
|
+
gate = WriteGate.check(
|
|
166
|
+
confirmer: confirmer, path: path, resolved: resolved,
|
|
167
|
+
question: "OK to create #{path} (#{content.bytesize} bytes)?",
|
|
168
|
+
change: Confirmer::Change.new(path: path, old: nil, new: content.b),
|
|
169
|
+
cancellable: cancellable
|
|
170
|
+
)
|
|
171
|
+
return gate if gate
|
|
172
|
+
end
|
|
173
|
+
|
|
122
174
|
write_bytes(resolved, content)
|
|
123
|
-
|
|
175
|
+
# A just-authored file counts as read: the model supplied its
|
|
176
|
+
# full bytes, so a follow-up edit needn't re-read it.
|
|
177
|
+
workspace.mark_read(resolved)
|
|
178
|
+
Tool::Result.new(content: "Created #{path} (#{content.bytesize} bytes)",
|
|
179
|
+
summary: "Created, #{Pikuri::Tool.pluralize(content.lines.size, 'line')}")
|
|
124
180
|
end
|
|
125
181
|
rescue Filesystem::Error, Error => e
|
|
126
|
-
|
|
182
|
+
Tool::Result.error(e.message)
|
|
127
183
|
rescue Errno::EACCES => e
|
|
128
|
-
"
|
|
184
|
+
Tool::Result.error("cannot write #{path}: #{e.message}")
|
|
129
185
|
end
|
|
130
186
|
|
|
131
|
-
# Internal-only signal for LLM-actionable
|
|
132
|
-
#
|
|
133
|
-
#
|
|
134
|
-
#
|
|
135
|
-
#
|
|
136
|
-
# is +private_constant+ to keep it from leaking out as a public
|
|
137
|
-
# exception type; callers should never +rescue+ this directly.
|
|
187
|
+
# Internal-only signal for an LLM-actionable precondition Write detects
|
|
188
|
+
# before any mutation (today: "existing file unreadable, so the
|
|
189
|
+
# identical-content check can't run"). Caught by {.write}'s outer rescue,
|
|
190
|
+
# rendered as +"Error: ..."+; +private_constant+ so it doesn't leak as a
|
|
191
|
+
# public exception.
|
|
138
192
|
class Error < StandardError; end
|
|
139
193
|
private_constant :Error
|
|
140
194
|
|
|
141
|
-
# Read the existing file in BINARY
|
|
142
|
-
#
|
|
143
|
-
#
|
|
144
|
-
#
|
|
145
|
-
# rescue would mislabel the read-side problem as "cannot write".
|
|
195
|
+
# Read the existing file in BINARY so the equality check is byte-wise.
|
|
196
|
+
# Raises {Error} on +Errno::EACCES+ so {.write}'s outer rescue renders a
|
|
197
|
+
# "cannot read for overwrite check" message — else the bottom
|
|
198
|
+
# +Errno::EACCES+ rescue would mislabel a read problem as "cannot write".
|
|
146
199
|
#
|
|
147
200
|
# @param resolved [Pathname]
|
|
148
201
|
# @param path [String] original path for the error message
|
|
@@ -155,9 +208,7 @@ module Pikuri
|
|
|
155
208
|
end
|
|
156
209
|
private_class_method :read_for_compare
|
|
157
210
|
|
|
158
|
-
# +mkdir_p+ the parent, then write.
|
|
159
|
-
# and overwrite-branches can reuse it without duplicating the
|
|
160
|
-
# +mkdir_p+ call.
|
|
211
|
+
# +mkdir_p+ the parent, then write. Shared by both branches.
|
|
161
212
|
#
|
|
162
213
|
# @param resolved [Pathname]
|
|
163
214
|
# @param content [String]
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pikuri
|
|
4
|
+
module Workspace
|
|
5
|
+
# The shared pre-write gate behind every confirmed write
|
|
6
|
+
# (book/touching-files.md). Before a {Write}/{Edit} mutates a file it runs
|
|
7
|
+
# {.check}, which:
|
|
8
|
+
#
|
|
9
|
+
# 1. **Short-circuits an unwritable target.** If the user can't write the
|
|
10
|
+
# path, the write would fail anyway and confirming a doomed write is
|
|
11
|
+
# noise — so return a "permission denied — ask the user to sudo"
|
|
12
|
+
# observation without prompting. Exact, not a guess: the file tools run
|
|
13
|
+
# in-process as the real user (only *bash* is sandboxed), so
|
|
14
|
+
# +File.writable?+ is real. Subsumes +/etc+ / +/boot+ — no path list.
|
|
15
|
+
# 2. **Confirms with a diff.** Else ask the human via a {Confirmer::Change}
|
|
16
|
+
# payload (the chrome renders the diff); a decline answers with the
|
|
17
|
+
# decision's own observation.
|
|
18
|
+
#
|
|
19
|
+
# Stateless module (composition, not a base class); both tools call {.check}
|
|
20
|
+
# and return on a non-nil result. {Write} always routes an overwrite through
|
|
21
|
+
# it; +confirm_all_writes: false+ lets a new file skip it and makes {Edit}
|
|
22
|
+
# skip it entirely.
|
|
23
|
+
#
|
|
24
|
+
# If a frequent-writing agent ever wants some writes to skip the prompt,
|
|
25
|
+
# grow an allowlist of safe paths (deny-by-default = confirm, like
|
|
26
|
+
# +PASSIVE_BINARIES+), never a denylist of sensitive ones — the denylist
|
|
27
|
+
# loses the enumeration game. Confirming every write is that allowlist,
|
|
28
|
+
# empty.
|
|
29
|
+
module WriteGate
|
|
30
|
+
module_function
|
|
31
|
+
|
|
32
|
+
# @param confirmer [Confirmer] the human confirmer.
|
|
33
|
+
# @param path [String] raw path as supplied by the LLM (for messages).
|
|
34
|
+
# @param resolved [Pathname] resolved write target.
|
|
35
|
+
# @param question [String] one-line confirm headline.
|
|
36
|
+
# @param change [Confirmer::Change] the diff payload (old/new bytes).
|
|
37
|
+
# @param cancellable [Pikuri::Agent::Control::Cancellable] the calling
|
|
38
|
+
# turn's token, for {Confirmer#ask}.
|
|
39
|
+
# @return [Pikuri::Tool::Result, nil] the observation to return at once
|
|
40
|
+
# (unwritable, rejected or redirected), or +nil+ to proceed.
|
|
41
|
+
# @raise [Pikuri::Agent::Control::Cancellable::Cancelled] when the turn
|
|
42
|
+
# is cancelled while the question waits.
|
|
43
|
+
def check(confirmer:, path:, resolved:, question:, change:, cancellable:)
|
|
44
|
+
return permission_denied(path) unless writable_target?(resolved)
|
|
45
|
+
|
|
46
|
+
case decision = confirmer.ask(request: Confirmer::Request.new(question: question, change: change),
|
|
47
|
+
cancellable: cancellable)
|
|
48
|
+
in Confirmer::Approved then nil
|
|
49
|
+
in Confirmer::Rejected | Confirmer::Redirected then decision.observation("the write to #{path}")
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Whether the user can write the target — the file if it exists, else the
|
|
54
|
+
# nearest existing ancestor dir (where {Write} would +mkdir_p+).
|
|
55
|
+
#
|
|
56
|
+
# @param resolved [Pathname]
|
|
57
|
+
# @return [Boolean]
|
|
58
|
+
def writable_target?(resolved)
|
|
59
|
+
return File.writable?(resolved) if resolved.exist?
|
|
60
|
+
|
|
61
|
+
dir = resolved.dirname
|
|
62
|
+
dir = dir.parent until dir.exist? || dir.root?
|
|
63
|
+
File.writable?(dir)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# @param path [String]
|
|
67
|
+
# @return [Pikuri::Tool::Result]
|
|
68
|
+
def permission_denied(path)
|
|
69
|
+
Pikuri::Tool::Result.error("permission denied — cannot write #{path} as this user; " \
|
|
70
|
+
'ask the user to make the change themselves (e.g. with sudo).')
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|