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
|
@@ -1,358 +1,195 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'pathname'
|
|
4
|
-
require 'tmpdir'
|
|
5
|
-
require 'fileutils'
|
|
6
4
|
|
|
7
5
|
module Pikuri
|
|
8
6
|
module Workspace
|
|
9
7
|
# Defines which paths the agent can see and write to. Constructed with
|
|
10
|
-
# explicit +readable
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
# post-symlink-resolution.
|
|
8
|
+
# explicit +readable+/+writable+ prefix lists; every Read/Write/Edit/Grep/
|
|
9
|
+
# Glob/Bash path is checked against them before touching disk. Returned
|
|
10
|
+
# Pathnames are absolute, post-symlink-resolution.
|
|
14
11
|
#
|
|
15
12
|
# == Project root, readable, writable
|
|
16
13
|
#
|
|
17
|
-
# +project_root+ is the writable containment ceiling —
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
# +Dir.chdir+'ing the Ruby process to the discovered project root
|
|
26
|
-
# at startup if it differs from the launch +Dir.pwd+, so the
|
|
27
|
-
# workspace and the surrounding process agree on one anchor.
|
|
14
|
+
# +project_root+ is the writable containment ceiling — auto-folded into both
|
|
15
|
+
# +readable+ and +writable+ (read/write anywhere under it), the base for
|
|
16
|
+
# resolving relative LLM paths, and the chdir target for {Search::Grep},
|
|
17
|
+
# {Search::Glob} and a co-wired shell. There is no separate "cwd": a host
|
|
18
|
+
# wanting the agent in a subtree passes that subtree as +project_root+ (and
|
|
19
|
+
# may +Dir.chdir+ the process there so process and workspace share one anchor). The extra
|
|
20
|
+
# +readable+ list grants read-only roots (toolchains, caches); the extra
|
|
21
|
+
# +writable+ list grants read+write roots.
|
|
28
22
|
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
# the extra +writable+ list grants read+write to additional roots
|
|
32
|
-
# (other project directories the agent should be able to touch).
|
|
23
|
+
# +root_writable: false+ makes the project root read-only, so paths still
|
|
24
|
+
# resolve against one base while writes land only in the +writable+ list:
|
|
33
25
|
#
|
|
34
|
-
#
|
|
26
|
+
# Filesystem.new(project_root: '~/Documents', root_writable: false,
|
|
27
|
+
# writable: ['~/Documents/Notes'])
|
|
28
|
+
# # read('Finance/invoice.pdf') ok; write('Notes/x.md') ok; write('x.md') refused
|
|
35
29
|
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
# bubblewrap overlay) pay nothing — and removed at process exit via
|
|
41
|
-
# a {Pikuri::Finalizers} registration. Everything ephemeral this
|
|
42
|
-
# workspace produces lives inside the umbrella, so one
|
|
43
|
-
# +remove_entry+ at process exit cleans the lot:
|
|
44
|
-
#
|
|
45
|
-
# * {#temp} — the LLM-visible playground subdir, present only when
|
|
46
|
-
# +temp: true+ (see below).
|
|
47
|
-
# * The bubblewrap sandbox's per-toolchain overlay state
|
|
48
|
-
# (+overlay-<slug>/{upper,work}+ for +~/.gradle/caches+, +~/.m2/repository+,
|
|
49
|
-
# …), used to keep cross-project toolchain caches isolated to one
|
|
50
|
-
# pikuri-code session. See
|
|
51
|
-
# {Pikuri::Code::Bash::Sandbox::Bubblewrap}.
|
|
52
|
-
#
|
|
53
|
-
# The umbrella deliberately lives in +~/.cache/pikuri+ rather than
|
|
54
|
-
# +/tmp+: the {Pikuri::Code::Bash::Sandbox::Bubblewrap} sandbox
|
|
55
|
-
# binds {#temp} at +/tmp+ inside the sandbox (so the LLM's
|
|
56
|
-
# reflexive +/tmp+ writes persist across bash calls). With the
|
|
57
|
-
# umbrella already under +/tmp+, that bind would land on top of
|
|
58
|
-
# itself and per-call mountpoint creation would pollute the dir
|
|
59
|
-
# recursively. +~/.cache/pikuri+ avoids the collision.
|
|
60
|
-
#
|
|
61
|
-
# At gem load, {.sweep_stale_internal_temps!} prunes umbrella dirs
|
|
62
|
-
# older than seven days — a safety net for sessions that died
|
|
63
|
-
# before the {Pikuri::Finalizers} sweep could run (SIGKILL, OOM).
|
|
64
|
-
# Recent umbrellas are left alone so a concurrent pikuri-code in
|
|
65
|
-
# another shell isn't disturbed.
|
|
66
|
-
#
|
|
67
|
-
# == Optional temp playground
|
|
68
|
-
#
|
|
69
|
-
# When constructed with +temp: true+, the workspace adds
|
|
70
|
-
# +<internal_temp>/playground+ to +writable+ and exposes it via
|
|
71
|
-
# {#temp}. The binary advertises this path to the LLM (e.g. in the
|
|
72
|
-
# system prompt) as scratch space. Default is +false+: specs and
|
|
73
|
-
# tests that build many workspaces don't pay the mkdir cost, and
|
|
74
|
-
# hosts that don't need a playground don't get one. Either way,
|
|
75
|
-
# the umbrella is shared with everything else that wants ephemeral
|
|
76
|
-
# state — no second tempdir is minted.
|
|
77
|
-
#
|
|
78
|
-
# == Optional /tmp alias
|
|
79
|
-
#
|
|
80
|
-
# When +alias_tmp_to_temp: true+ AND +temp:+ is set, file paths
|
|
81
|
-
# supplied to {#resolve_for_read} / {#resolve_for_write} that
|
|
82
|
-
# start with +/tmp/+ (or are exactly +/tmp+) are rewritten to the
|
|
83
|
-
# host {#temp} path before containment is checked. This is the
|
|
84
|
-
# host-side counterpart to the sandbox's +--bind <temp> /tmp+: bash
|
|
85
|
-
# inside the sandbox writes to +/tmp/foo+, then the file tools
|
|
86
|
-
# (which run on the host, not in the sandbox) accept the same
|
|
87
|
-
# +/tmp/foo+ path and resolve it to the workspace temp's host
|
|
88
|
-
# path. Without this, the LLM would have to remember two paths
|
|
89
|
-
# for the same dir. Off by default; +bin/pikuri-code+ flips it on
|
|
90
|
-
# when the bubblewrap sandbox is enabled.
|
|
91
|
-
#
|
|
92
|
-
# == Read-set vs. write-set
|
|
93
|
-
#
|
|
94
|
-
# {#resolve_for_read} checks against +readable + writable+ (you can
|
|
95
|
-
# read anything writable). {#resolve_for_write} checks against
|
|
96
|
-
# +writable+ only. Tools that mutate state route through the second
|
|
97
|
-
# method; tools that only inspect route through the first.
|
|
98
|
-
#
|
|
99
|
-
# == Existence is not the workspace's concern
|
|
100
|
-
#
|
|
101
|
-
# +resolve_for_read('foo.rb')+ succeeds (returns a +Pathname+) even if
|
|
102
|
-
# +foo.rb+ doesn't exist; the caller ({Read}) errors with
|
|
103
|
-
# file-not-found when it tries to open it. +resolve_for_write+
|
|
104
|
-
# tolerates entirely non-existent paths (Write can create
|
|
105
|
-
# +lib/new/dir/foo.rb+ even when +lib/new/+ doesn't exist) — the
|
|
106
|
-
# caller is responsible for any +mkdir_p+ before writing. This split
|
|
107
|
-
# keeps the workspace narrowly responsible for *containment*, not for
|
|
108
|
-
# filesystem-state checks.
|
|
109
|
-
#
|
|
110
|
-
# == Subprocess environment ({#env})
|
|
111
|
-
#
|
|
112
|
-
# Workspaces own a {#env} +Hash<String,String>+ that subprocess-
|
|
113
|
-
# spawning tools (currently {Pikuri::Code::Bash}) thread into
|
|
114
|
-
# {Pikuri::Subprocess.spawn}. The motivating case: the bubblewrap
|
|
115
|
-
# sandbox doesn't bind-mount +~/.gitconfig+, so +git commit+ inside
|
|
116
|
-
# fails (modern git refuses rather than synthesizing a default
|
|
117
|
-
# from +/etc/passwd+ — which isn't bind-mounted either). The
|
|
118
|
-
# workspace resolves the host's effective git identity at
|
|
119
|
-
# {#project_root} (so +includeIf+ rules apply: a repo under
|
|
120
|
-
# +~/work/my/+ that's covered by a +gitdir:~/work/my/+ include
|
|
121
|
-
# gets that identity, not the global default) and exposes it as
|
|
122
|
-
# +GIT_AUTHOR_*+ / +GIT_COMMITTER_*+ env vars that override config
|
|
123
|
-
# entirely and need no file in the sandbox.
|
|
124
|
-
#
|
|
125
|
-
# The lookup is *lazy* and *memoized*: the constructor doesn't
|
|
126
|
-
# shell out, so building a workspace stays cheap (specs that
|
|
127
|
-
# never read +#env+ pay nothing). First access runs +git -C
|
|
128
|
-
# project_root config user.{name,email}+ once. Falls back to +{}+
|
|
129
|
-
# if +git+ isn't on +PATH+ or no identity is configured — the
|
|
130
|
-
# subprocess then runs unmediated and git's own "please tell me
|
|
131
|
-
# who you are" surfaces inside the sandbox.
|
|
132
|
-
#
|
|
133
|
-
# Hosts that want a different env (extra vars, no git resolution,
|
|
134
|
-
# explicit identity) pass +env:+ to the constructor; that value
|
|
135
|
-
# is used verbatim and the git lookup is skipped. Always frozen
|
|
136
|
-
# by the time it leaves the workspace.
|
|
30
|
+
# {#resolve_for_read} checks +readable ∪ writable+; {#resolve_for_write}
|
|
31
|
+
# checks +writable+ only. Neither checks existence — the workspace owns
|
|
32
|
+
# *containment*, not filesystem state (Read errors on a missing file, Write
|
|
33
|
+
# +mkdir_p+s its own parents).
|
|
137
34
|
#
|
|
138
35
|
# == Containment algorithm
|
|
139
36
|
#
|
|
140
|
-
# {#resolve} walks up the input
|
|
141
|
-
# +realpath+
|
|
142
|
-
#
|
|
143
|
-
#
|
|
37
|
+
# {#resolve} walks up the input to its deepest existing ancestor,
|
|
38
|
+
# +realpath+s that (resolving symlinks in the existing portion), and checks
|
|
39
|
+
# the resolved base against the candidate roots:
|
|
40
|
+
#
|
|
41
|
+
# 1. +lib/foo.rb+ (exists) → base matches a root → realpath'd file.
|
|
42
|
+
# 2. +lib/new/dir/foo.rb+ (intermediates missing) → deepest existing parent
|
|
43
|
+
# in a root → the intended new path (caller +mkdir_p+s).
|
|
44
|
+
# 3. +lib/../../etc/passwd+ (+..+ escape) → +cleanpath+ collapses +..+, lands
|
|
45
|
+
# outside every root → {Error}.
|
|
46
|
+
# 4. +link/foo.rb+, +link → /etc+ (symlink escape) → +realpath+ resolves
|
|
47
|
+
# through the link to +/etc+, outside every root → {Error}.
|
|
144
48
|
#
|
|
145
|
-
#
|
|
146
|
-
#
|
|
147
|
-
# 2. +lib/new/dir/foo.rb+ (intermediates missing) → walks up to the
|
|
148
|
-
# deepest existing parent inside a root → returns the intended new
|
|
149
|
-
# path (caller +mkdir_p+s the parent before writing).
|
|
150
|
-
# 3. +lib/../../etc/passwd+ (+..+ escape) → +cleanpath+ collapses +..+
|
|
151
|
-
# syntactically, walks land outside every root → {Error}.
|
|
152
|
-
# 4. +link/foo.rb+ where +link → /etc+ (symlink escape) → walks to
|
|
153
|
-
# +link+ (which exists), +realpath+ resolves through the symlink to
|
|
154
|
-
# +/etc+, outside every root → {Error}.
|
|
49
|
+
# Lexical normalization alone catches 1–3 but misses 4; the walk-up
|
|
50
|
+
# +realpath+ closes that gap.
|
|
155
51
|
#
|
|
156
|
-
#
|
|
157
|
-
#
|
|
52
|
+
# The check holds for the path the model named, when it named it: the tool
|
|
53
|
+
# opens the path afterwards, so a symlink swapped in between escapes.
|
|
54
|
+
# Accepted because nothing may change a root while a tool runs (the root
|
|
55
|
+
# +AGENTS.md+ invariant) — +design/decisions.md+ +D_containment_race+.
|
|
158
56
|
#
|
|
159
57
|
# == Project-root denylist
|
|
160
58
|
#
|
|
161
|
-
#
|
|
162
|
-
#
|
|
163
|
-
#
|
|
164
|
-
#
|
|
165
|
-
#
|
|
166
|
-
# This is a sanity guard against fat-fingering, not a security
|
|
167
|
-
# perimeter — the real security is the +readable+/+writable+ lists.
|
|
168
|
-
# Other-OS home roots (+/Users/$USER+ on macOS) are not denied;
|
|
169
|
-
# Linux-first per +CLAUDE.md+.
|
|
59
|
+
# A +project_root+ of a system root or home dir makes the whole tree
|
|
60
|
+
# writable — almost always a fat-finger. The constructor rejects
|
|
61
|
+
# {DENIED_PROJECT_ROOTS} and any direct child of +/home+. A sanity guard,
|
|
62
|
+
# not a security perimeter (the +readable+/+writable+ lists are that).
|
|
63
|
+
# Linux-first: other-OS home roots (+/Users/$USER+) aren't denied.
|
|
170
64
|
class Filesystem
|
|
171
|
-
# Raised for any path
|
|
172
|
-
#
|
|
173
|
-
#
|
|
174
|
-
# raised at construction time for a denied project root.
|
|
65
|
+
# Raised for any path resolving outside the workspace. Recoverable at the
|
|
66
|
+
# tool layer (tools rescue it into +"Error: ..."+ observations). Also
|
|
67
|
+
# raised at construction for a denied project root.
|
|
175
68
|
class Error < StandardError; end
|
|
176
69
|
|
|
177
|
-
#
|
|
178
|
-
#
|
|
179
|
-
#
|
|
180
|
-
#
|
|
181
|
-
# doesn't leak forever.
|
|
182
|
-
INTERNAL_TEMP_STALE_SECONDS = 7 * 24 * 60 * 60
|
|
183
|
-
|
|
184
|
-
# System-root project_roots the constructor refuses. Exact-match
|
|
185
|
-
# (not prefix) — +/home/user/project+ passes, +/home/user+ is
|
|
186
|
-
# rejected by the parent-is-/home check below. Frozen list;
|
|
187
|
-
# downstream hosts with unusual layouts can subclass if they
|
|
188
|
-
# really need a different policy.
|
|
70
|
+
# System-root project_roots the constructor refuses (exact-match, not
|
|
71
|
+
# prefix — +/home/user/project+ passes; +/home/user+ is caught by the
|
|
72
|
+
# parent-is-/home check). Downstream hosts with unusual layouts can
|
|
73
|
+
# subclass.
|
|
189
74
|
DENIED_PROJECT_ROOTS = %w[
|
|
190
75
|
/ /etc /var /proc /sys /dev /boot /root
|
|
191
76
|
/usr /opt /lib /lib64 /bin /sbin /tmp
|
|
192
77
|
].map { |p| Pathname.new(p) }.freeze
|
|
193
78
|
|
|
194
|
-
# @return [Pathname] project root, post-realpath
|
|
195
|
-
#
|
|
196
|
-
#
|
|
197
|
-
# {#readable} and {#writable}.
|
|
79
|
+
# @return [Pathname] project root, post-realpath — the containment
|
|
80
|
+
# ceiling, relative-path base, and shell/Grep/Glob chdir target;
|
|
81
|
+
# always in {#readable}, in {#writable} unless +root_writable: false+.
|
|
198
82
|
attr_reader :project_root
|
|
199
83
|
|
|
200
|
-
# @return [Array<Pathname>] read-only roots (
|
|
201
|
-
#
|
|
84
|
+
# @return [Array<Pathname>] read-only roots (writable roots are also
|
|
85
|
+
# readable). Post-realpath, deduped.
|
|
202
86
|
attr_reader :readable
|
|
203
87
|
|
|
204
|
-
# @return [Array<Pathname>] writable roots (read+write)
|
|
205
|
-
# {#project_root}
|
|
206
|
-
# deduped.
|
|
88
|
+
# @return [Array<Pathname>] writable roots (read+write); includes
|
|
89
|
+
# {#project_root} unless +root_writable: false+. Post-realpath, deduped.
|
|
207
90
|
attr_reader :writable
|
|
208
91
|
|
|
209
|
-
#
|
|
210
|
-
#
|
|
211
|
-
# +temp: true+, else +nil+. Lives at +<internal_temp>/playground+;
|
|
212
|
-
# removed transitively when the umbrella is wiped on process exit.
|
|
213
|
-
attr_reader :temp
|
|
214
|
-
|
|
215
|
-
# @param project_root [String, Pathname] absolute (or working-
|
|
216
|
-
# directory-relative) path to the project root. +realpath+'d
|
|
217
|
-
# once; must exist; must not match {DENIED_PROJECT_ROOTS} or be
|
|
218
|
-
# a direct child of +/home+.
|
|
219
|
-
# @param readable [Array<String, Pathname>] additional read-only
|
|
220
|
-
# roots. +realpath+'d at construction; missing entries raise
|
|
221
|
-
# loudly via {Pathname#realpath}.
|
|
222
|
-
# @param writable [Array<String, Pathname>] additional read+write
|
|
223
|
-
# roots. Same treatment as +readable+.
|
|
224
|
-
# @param temp [Boolean] when +true+, adds
|
|
225
|
-
# +<internal_temp>/playground+ to {#writable} and exposes it
|
|
226
|
-
# via {#temp}. Forces the umbrella to mint up-front (the
|
|
227
|
-
# playground is created eagerly so {#writable} reflects it).
|
|
228
|
-
# @param alias_tmp_to_temp [Boolean] when +true+ AND +temp:+ is
|
|
229
|
-
# set, +/tmp/*+ paths supplied to {#resolve_for_read} /
|
|
230
|
-
# {#resolve_for_write} are rewritten to point at {#temp}.
|
|
231
|
-
# Pairs with the bubblewrap sandbox's +--bind <temp> /tmp+.
|
|
232
|
-
# @param env [Hash{String=>String}, nil] subprocess environment
|
|
233
|
-
# exposed via {#env}. +nil+ (default) → lazy-derive the host
|
|
234
|
-
# git identity from {#project_root} on first access; explicit
|
|
235
|
-
# hash → use verbatim (and skip the git lookup entirely). See
|
|
236
|
-
# the class header §"Subprocess environment" for the
|
|
237
|
-
# rationale.
|
|
238
|
-
# @raise [Errno::ENOENT] if +project_root+ or any
|
|
239
|
-
# +readable+/+writable+ entry does not exist.
|
|
240
|
-
# @raise [Error] if +project_root+ is denied (system root or
|
|
241
|
-
# +/home/*+).
|
|
242
|
-
def initialize(project_root:, readable: [], writable: [], temp: false, alias_tmp_to_temp: false, env: nil)
|
|
243
|
-
@project_root = Pathname.new(project_root).realpath
|
|
244
|
-
validate_project_root!(@project_root)
|
|
245
|
-
|
|
246
|
-
@internal_temp = nil
|
|
247
|
-
@temp = temp ? mint_playground : nil
|
|
248
|
-
@alias_tmp_to_temp = alias_tmp_to_temp && !@temp.nil?
|
|
249
|
-
@env_override = env
|
|
250
|
-
|
|
251
|
-
@writable = ([@project_root] + writable.map { |p| Pathname.new(p).realpath } + [@temp].compact).uniq
|
|
252
|
-
@readable = (@writable + readable.map { |p| Pathname.new(p).realpath }).uniq
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
# @return [Boolean] whether {#resolve_for_read} / {#resolve_for_write}
|
|
256
|
-
# rewrite +/tmp/*+ inputs to {#temp}.
|
|
257
|
-
attr_reader :alias_tmp_to_temp
|
|
258
|
-
|
|
259
|
-
# Environment variables for subprocesses spawned in this
|
|
260
|
-
# workspace. Lazy, memoized, frozen.
|
|
92
|
+
# Whether these roots may hold data worth exfiltrating — the lethal
|
|
93
|
+
# trifecta's private leg, which the file tools inherit from here.
|
|
261
94
|
#
|
|
262
|
-
#
|
|
263
|
-
#
|
|
264
|
-
#
|
|
265
|
-
#
|
|
266
|
-
#
|
|
267
|
-
# +{}+ if git resolution finds no identity for {#project_root}
|
|
268
|
-
# or +git+ isn't on +PATH+.
|
|
95
|
+
# The framework cannot answer this for you: sensitivity is a property of
|
|
96
|
+
# the *data*, not of the tool, and +~/work/scratch+ and +~/work/payroll+
|
|
97
|
+
# are indistinguishable from the outside. Declaring it is what lets
|
|
98
|
+
# {Pikuri::Trifecta} stay quiet over a public checkout and speak up over
|
|
99
|
+
# your real one.
|
|
269
100
|
#
|
|
270
|
-
# @return [
|
|
271
|
-
def
|
|
272
|
-
@env ||= (@env_override || compute_git_identity_env).freeze
|
|
273
|
-
end
|
|
101
|
+
# @return [Boolean] default +false+
|
|
102
|
+
def private? = @private
|
|
274
103
|
|
|
275
|
-
#
|
|
276
|
-
#
|
|
277
|
-
# removal the moment it's minted, so anything subsequently placed inside
|
|
278
|
-
# (the playground, {Pikuri::Code::Bash::Sandbox::Bubblewrap}'s
|
|
279
|
-
# overlay state) gets wiped together. Callers that want
|
|
280
|
-
# ephemeral state owned by the workspace should put it under
|
|
281
|
-
# this dir rather than minting their own siblings.
|
|
104
|
+
# Whether every byte reachable here is user-authored — the opt-out from
|
|
105
|
+
# the file tools' default +:hard+ untrusted leg.
|
|
282
106
|
#
|
|
283
|
-
#
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
#
|
|
289
|
-
#
|
|
290
|
-
#
|
|
291
|
-
# rather than its own +at_exit+, so process teardown stays on the
|
|
292
|
-
# one registry. The +path.exist?+ guard makes the removal a no-op
|
|
293
|
-
# when the dir is already gone (test cleanup, manual rm).
|
|
294
|
-
def self.mint_internal_temp
|
|
295
|
-
FileUtils.mkdir_p(Paths.cache)
|
|
296
|
-
path = Pathname.new(Dir.mktmpdir('workspace-', Paths.cache)).realpath
|
|
297
|
-
Pikuri::Finalizers.register { FileUtils.remove_entry(path.to_s) if path.exist? }
|
|
298
|
-
path
|
|
299
|
-
end
|
|
300
|
-
|
|
301
|
-
# Reap +workspace-*+ umbrella dirs that have outlived
|
|
302
|
-
# {INTERNAL_TEMP_STALE_SECONDS}. Called once at gem load via
|
|
303
|
-
# {Pikuri::Workspace} so each process boot inherits a tidy
|
|
304
|
-
# {Paths::cache}. Failures (permission denied, racing concurrent
|
|
305
|
-
# sweeper) are swallowed — best-effort cleanup; the
|
|
306
|
-
# {Pikuri::Finalizers} removal is the load-bearing path.
|
|
107
|
+
# Default +false+, because broad disk access means cloned dependencies,
|
|
108
|
+
# fetched PR branches, saved downloads and package caches: attacker-authored
|
|
109
|
+
# bytes are the norm, and a hostile README reaches the model verbatim.
|
|
110
|
+
# The claim covers the tool's *entire* reachable set — a vouched-for island
|
|
111
|
+
# inside a broad root buys nothing, since the tool can still reach
|
|
112
|
+
# everything else — so +trusted: true+ refuses any root outside
|
|
113
|
+
# +project_root+: a +~/.m2+ cache or a home skill dir is somebody else's
|
|
114
|
+
# bytes.
|
|
307
115
|
#
|
|
308
|
-
#
|
|
309
|
-
|
|
310
|
-
|
|
116
|
+
# Distinct from the trust-this-directory question a host may ask at boot:
|
|
117
|
+
# that one asks whether a hostile +.gitattributes+ can *execute code*, this
|
|
118
|
+
# one whether the repo's *bytes* are attacker-authored. A repo routinely
|
|
119
|
+
# passes the first and fails the second — you control +.git/config+;
|
|
120
|
+
# +vendor/+ is someone else's.
|
|
121
|
+
#
|
|
122
|
+
# @return [Boolean] default +false+
|
|
123
|
+
def trusted? = @trusted
|
|
124
|
+
|
|
125
|
+
# @param project_root [String, Pathname] project root; +realpath+'d once,
|
|
126
|
+
# must exist, must not match {DENIED_PROJECT_ROOTS} or be a +/home+ child.
|
|
127
|
+
# @param readable [Array<String, Pathname>] extra read-only roots;
|
|
128
|
+
# +realpath+'d (missing entries raise).
|
|
129
|
+
# @param writable [Array<String, Pathname>] extra read+write roots; same.
|
|
130
|
+
# @param root_writable [Boolean] +false+ keeps +project_root+ read-only;
|
|
131
|
+
# writes then land only under +writable+.
|
|
132
|
+
# @param private [Boolean] these roots may hold sensitive data; see
|
|
133
|
+
# {#private?}.
|
|
134
|
+
# @param trusted [Boolean] every byte under these roots is user-authored;
|
|
135
|
+
# see {#trusted?}.
|
|
136
|
+
# @raise [Errno::ENOENT] if any of +project_root+/+readable+/+writable+
|
|
137
|
+
# doesn't exist.
|
|
138
|
+
# @raise [Error] if +project_root+ is denied (system root or +/home/*+).
|
|
139
|
+
# @raise [ArgumentError] if +trusted: true+ and a +readable+/+writable+
|
|
140
|
+
# root lies outside +project_root+.
|
|
141
|
+
def initialize(project_root:, readable: [], writable: [], root_writable: true, private: false,
|
|
142
|
+
trusted: false)
|
|
143
|
+
@project_root = Pathname.new(project_root).realpath
|
|
144
|
+
validate_project_root!(@project_root)
|
|
311
145
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
next unless entry.start_with?('workspace-')
|
|
315
|
-
path = File.join(Paths.cache, entry)
|
|
316
|
-
next unless File.directory?(path)
|
|
317
|
-
next if File.mtime(path) > cutoff
|
|
146
|
+
@private = private
|
|
147
|
+
@trusted = trusted
|
|
318
148
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
end
|
|
149
|
+
@writable = ((root_writable ? [@project_root] : []) + writable.map { |p| Pathname.new(p).realpath }).uniq
|
|
150
|
+
@readable = ([@project_root] + @writable + readable.map { |p| Pathname.new(p).realpath }).uniq
|
|
151
|
+
validate_trusted_roots! if trusted
|
|
323
152
|
end
|
|
324
153
|
|
|
325
|
-
# Resolve a user
|
|
326
|
-
#
|
|
327
|
-
# disk; the caller validates existence separately.
|
|
154
|
+
# Resolve a user path against the read-set (readable ∪ writable). Returned
|
|
155
|
+
# Pathname is absolute and may not exist; the caller checks existence.
|
|
328
156
|
#
|
|
329
|
-
# @param path [String]
|
|
157
|
+
# @param path [String, Pathname]
|
|
330
158
|
# @return [Pathname]
|
|
331
159
|
# @raise [Error] if the resolved path falls outside every root
|
|
332
160
|
def resolve_for_read(path)
|
|
333
161
|
resolve(path, @readable)
|
|
334
162
|
end
|
|
335
163
|
|
|
336
|
-
# Resolve a user
|
|
164
|
+
# Resolve a user path against the write-set.
|
|
337
165
|
#
|
|
338
|
-
# @param path [String]
|
|
166
|
+
# @param path [String, Pathname]
|
|
339
167
|
# @return [Pathname]
|
|
340
168
|
# @raise [Error] if the resolved path falls outside every writable root
|
|
341
169
|
def resolve_for_write(path)
|
|
342
170
|
resolve(path, @writable)
|
|
343
171
|
end
|
|
344
172
|
|
|
345
|
-
|
|
173
|
+
# Whether +path+ sits under a credential denylist entry.
|
|
174
|
+
#
|
|
175
|
+
# {#resolve_for_read} gates a path the *model* supplied; this gates a
|
|
176
|
+
# path a tool **walked** to on its own (a {Listing} row, an +rg+ hit),
|
|
177
|
+
# which no +resolve_+ call ever sees. A scoped Filesystem has no
|
|
178
|
+
# denylist — containment is the readable/writable ceiling — so this is
|
|
179
|
+
# always +false+ here; a whole-host subclass with a credential denylist
|
|
180
|
+
# overrides it, symlink-resolving +path+ first so a +decoy → ~/.ssh+
|
|
181
|
+
# link inside the workspace is caught.
|
|
182
|
+
#
|
|
183
|
+
# @param path [String, Pathname]
|
|
184
|
+
# @return [Boolean]
|
|
185
|
+
def denied?(_path) = false
|
|
346
186
|
|
|
347
|
-
#
|
|
348
|
-
# +
|
|
349
|
-
#
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
FileUtils.mkdir_p(path)
|
|
354
|
-
path.realpath
|
|
355
|
-
end
|
|
187
|
+
# @return [Array<Pathname>] the denied roots, for callers that can prune
|
|
188
|
+
# a walk up front (+rg --glob '!…'+) instead of filtering its results.
|
|
189
|
+
# Empty here; see {#denied?}.
|
|
190
|
+
def denied_roots = []
|
|
191
|
+
|
|
192
|
+
private
|
|
356
193
|
|
|
357
194
|
def validate_project_root!(canonical)
|
|
358
195
|
if DENIED_PROJECT_ROOTS.include?(canonical) || canonical.parent.to_s == '/home'
|
|
@@ -363,9 +200,18 @@ module Pikuri
|
|
|
363
200
|
end
|
|
364
201
|
end
|
|
365
202
|
|
|
203
|
+
def validate_trusted_roots!
|
|
204
|
+
stray = @readable.reject { |r| r.ascend.include?(@project_root) }
|
|
205
|
+
return if stray.empty?
|
|
206
|
+
|
|
207
|
+
raise ArgumentError,
|
|
208
|
+
"trusted: true vouches for every byte the tools can reach, but #{stray.map(&:to_s).join(', ')} " \
|
|
209
|
+
"lies outside project_root '#{@project_root}'; scope the workspace to one root, or drop trusted:"
|
|
210
|
+
end
|
|
211
|
+
|
|
366
212
|
# See the class header for the algorithm rationale.
|
|
367
213
|
def resolve(path, roots)
|
|
368
|
-
path =
|
|
214
|
+
path = expand_home(path)
|
|
369
215
|
pn = Pathname.new(path)
|
|
370
216
|
pn = @project_root + pn unless pn.absolute?
|
|
371
217
|
cleaned = pn.cleanpath
|
|
@@ -374,8 +220,9 @@ module Pikuri
|
|
|
374
220
|
existing = existing.parent until existing.exist? || existing.parent == existing
|
|
375
221
|
base = existing.realpath
|
|
376
222
|
|
|
377
|
-
|
|
378
|
-
|
|
223
|
+
# Component-wise, not a string-prefix test against +root + "/"+: that
|
|
224
|
+
# one turns a +/+ root into +"//"+ and matches nothing beneath it.
|
|
225
|
+
unless roots.any? { |r| base.ascend.include?(r) }
|
|
379
226
|
raise Error, "path '#{path}' is outside the workspace " \
|
|
380
227
|
"(roots: #{roots.map(&:to_s).join(', ')})"
|
|
381
228
|
end
|
|
@@ -383,157 +230,24 @@ module Pikuri
|
|
|
383
230
|
base + cleaned.relative_path_from(existing)
|
|
384
231
|
end
|
|
385
232
|
|
|
386
|
-
#
|
|
387
|
-
# via +git -C project_root config+ — runs git's full chain
|
|
388
|
-
# (system → global → includeIf → per-repo), so what comes back
|
|
389
|
-
# is what +git commit+ would attribute to on the host outside
|
|
390
|
-
# the sandbox. +Errno::ENOENT+ (no +git+ on +PATH+) and a
|
|
391
|
-
# non-zero exit (no value configured) both fall back to a nil
|
|
392
|
-
# entry; an empty identity field is treated the same. Result:
|
|
393
|
-
# either the full four-entry hash or +{}+.
|
|
394
|
-
def compute_git_identity_env
|
|
395
|
-
name = git_config('user.name')
|
|
396
|
-
email = git_config('user.email')
|
|
397
|
-
return {} if name.nil? || name.empty? || email.nil? || email.empty?
|
|
398
|
-
|
|
399
|
-
{
|
|
400
|
-
'GIT_AUTHOR_NAME' => name,
|
|
401
|
-
'GIT_AUTHOR_EMAIL' => email,
|
|
402
|
-
'GIT_COMMITTER_NAME' => name,
|
|
403
|
-
'GIT_COMMITTER_EMAIL' => email
|
|
404
|
-
}
|
|
405
|
-
end
|
|
406
|
-
|
|
407
|
-
def git_config(key)
|
|
408
|
-
result = Pikuri::Subprocess.spawn(
|
|
409
|
-
'git', '-C', @project_root.to_s, 'config', key,
|
|
410
|
-
chdir: @project_root.to_s
|
|
411
|
-
).wait
|
|
412
|
-
result.status.success? ? result.output.strip : nil
|
|
413
|
-
rescue Errno::ENOENT
|
|
414
|
-
nil
|
|
415
|
-
end
|
|
416
|
-
|
|
417
|
-
# Rewrite +/tmp+ / +/tmp/foo+ inputs to land under {#temp} when
|
|
418
|
-
# {#alias_tmp_to_temp} is set. Other inputs pass through. Lives
|
|
419
|
-
# in the base class so {AllowAll} inherits identical behavior.
|
|
420
|
-
def apply_tmp_alias(path)
|
|
421
|
-
return path unless @alias_tmp_to_temp
|
|
422
|
-
return @temp.to_s if path == '/tmp'
|
|
423
|
-
return File.join(@temp.to_s, path[5..]) if path.start_with?('/tmp/')
|
|
424
|
-
|
|
425
|
-
path
|
|
426
|
-
end
|
|
427
|
-
|
|
428
|
-
# Unrestricted variant of {Filesystem}: every path resolves, with one
|
|
429
|
-
# carve-out — paths under {CREDENTIAL_DENYLIST} (the user's +~/.ssh+,
|
|
430
|
-
# +~/.aws+, +~/.gnupg+, +~/.docker+, +~/.kube+, +~/.netrc+, and
|
|
431
|
-
# +/etc/shadow+) still raise. Intended for dev-container / Docker
|
|
432
|
-
# mode where the container is the security boundary; the denylist
|
|
433
|
-
# remains as defense-in-depth against prompt-injection exfiltration
|
|
434
|
-
# of credentials that are commonly bind-mounted into a container
|
|
435
|
-
# from the host. Pairs naturally with +--yolo+ (no Confirmer
|
|
436
|
-
# prompt) — but note that combining with
|
|
437
|
-
# +Code::Bash::Sandbox::Bubblewrap+ defeats the sandbox (the
|
|
438
|
-
# whole filesystem ends up bind-mounted in), and bash inside
|
|
439
|
-
# the sandbox bypasses the denylist anyway, so the intended
|
|
440
|
-
# combo is ALLOW_ALL + +Sandbox::NONE+ inside a container.
|
|
233
|
+
# Expand a leading +~+ / +~/+ to the home folder, as a shell would:
|
|
441
234
|
#
|
|
442
|
-
#
|
|
235
|
+
# expand_home('~/.bashrc') # => "/home/alex/.bashrc"
|
|
236
|
+
# expand_home('a/~/b') # => "a/~/b"
|
|
443
237
|
#
|
|
444
|
-
# +
|
|
445
|
-
#
|
|
446
|
-
#
|
|
447
|
-
#
|
|
448
|
-
# base for relative-path resolution and the chdir target tools
|
|
449
|
-
# like Bash use. +temp:+ behaves the same way as {Filesystem}.
|
|
238
|
+
# Models write +~/.bashrc+ out of shell habit; left alone it resolves to
|
|
239
|
+
# +<project_root>/~/.bashrc+ and fails as *not found* instead of *outside
|
|
240
|
+
# the workspace*, sending the model off on a detour. +~user+ is left
|
|
241
|
+
# alone (Ruby's expansion of it raises on an unknown user).
|
|
450
242
|
#
|
|
451
|
-
#
|
|
452
|
-
#
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
# from *creating* a credential dir the user doesn't have yet (e.g.
|
|
458
|
-
# planting +~/.gnupg+ for a later GPG operation to pick up). The
|
|
459
|
-
# list is hardcoded; subclass for a host with a different policy.
|
|
460
|
-
class AllowAll < Filesystem
|
|
461
|
-
# Credential locations that even ALLOW_ALL refuses. Expanded
|
|
462
|
-
# via +ENV['HOME']+ at class-load time and unioned with the
|
|
463
|
-
# +/root+ variants (dev-container default user). Frozen.
|
|
464
|
-
CREDENTIAL_DENYLIST = begin
|
|
465
|
-
homes = [ENV.fetch('HOME', nil), '/root'].compact.uniq
|
|
466
|
-
per_home = %w[.ssh .aws .gnupg .docker .kube .netrc]
|
|
467
|
-
user_paths = homes.flat_map { |h| per_home.map { |p| File.join(h, p) } }
|
|
468
|
-
(user_paths + %w[/etc/shadow]).map { |p| Pathname.new(p) }.uniq.freeze
|
|
469
|
-
end
|
|
470
|
-
|
|
471
|
-
# @param project_root [String, Pathname] surface-level project
|
|
472
|
-
# root. Not validated against {DENIED_PROJECT_ROOTS}.
|
|
473
|
-
# @param temp [Boolean] same semantics as {Filesystem}.
|
|
474
|
-
# @param alias_tmp_to_temp [Boolean] same semantics as
|
|
475
|
-
# {Filesystem} — when +true+ AND +temp:+ is set, +/tmp/*+
|
|
476
|
-
# inputs rewrite to land under {#temp}.
|
|
477
|
-
# @param env [Hash{String=>String}, nil] same semantics as
|
|
478
|
-
# {Filesystem}; see {Filesystem#env}.
|
|
479
|
-
def initialize(project_root: Dir.pwd, temp: false, alias_tmp_to_temp: false, env: nil)
|
|
480
|
-
@project_root = Pathname.new(project_root).realpath
|
|
243
|
+
# @param path [String, Pathname]
|
|
244
|
+
# @return [String]
|
|
245
|
+
def expand_home(path)
|
|
246
|
+
path = path.to_s
|
|
247
|
+
return Dir.home if path == '~'
|
|
248
|
+
return File.join(Dir.home, path[2..]) if path.start_with?('~/')
|
|
481
249
|
|
|
482
|
-
|
|
483
|
-
@temp = temp ? mint_playground : nil
|
|
484
|
-
@alias_tmp_to_temp = alias_tmp_to_temp && !@temp.nil?
|
|
485
|
-
@env_override = env
|
|
486
|
-
|
|
487
|
-
# Advertise "everything is in scope" via the accessor surface so
|
|
488
|
-
# callers that inspect +readable+/+writable+ (system-prompt
|
|
489
|
-
# rendering, Bubblewrap bind-mount construction) see the
|
|
490
|
-
# intended semantics. The actual containment is the denylist
|
|
491
|
-
# in {#resolve_for_read}/{#resolve_for_write}.
|
|
492
|
-
@writable = [Pathname.new('/').realpath, @temp].compact.uniq
|
|
493
|
-
@readable = @writable.dup
|
|
494
|
-
end
|
|
495
|
-
|
|
496
|
-
# @param path [String]
|
|
497
|
-
# @return [Pathname]
|
|
498
|
-
# @raise [Error] if the resolved path lands under {CREDENTIAL_DENYLIST}.
|
|
499
|
-
def resolve_for_read(path)
|
|
500
|
-
resolve_unrestricted(path)
|
|
501
|
-
end
|
|
502
|
-
|
|
503
|
-
# @param path [String]
|
|
504
|
-
# @return [Pathname]
|
|
505
|
-
# @raise [Error] if the resolved path lands under {CREDENTIAL_DENYLIST}.
|
|
506
|
-
def resolve_for_write(path)
|
|
507
|
-
resolve_unrestricted(path)
|
|
508
|
-
end
|
|
509
|
-
|
|
510
|
-
private
|
|
511
|
-
|
|
512
|
-
def resolve_unrestricted(path)
|
|
513
|
-
path = apply_tmp_alias(path)
|
|
514
|
-
pn = Pathname.new(path)
|
|
515
|
-
pn = @project_root + pn unless pn.absolute?
|
|
516
|
-
cleaned = pn.cleanpath
|
|
517
|
-
|
|
518
|
-
existing = cleaned
|
|
519
|
-
existing = existing.parent until existing.exist? || existing.parent == existing
|
|
520
|
-
base = existing.realpath
|
|
521
|
-
resolved = base + cleaned.relative_path_from(existing)
|
|
522
|
-
|
|
523
|
-
CREDENTIAL_DENYLIST.each do |denied|
|
|
524
|
-
candidate = denied.exist? ? denied.realpath : denied
|
|
525
|
-
next unless resolved == candidate ||
|
|
526
|
-
resolved.to_s.start_with?(candidate.to_s + File::SEPARATOR)
|
|
527
|
-
|
|
528
|
-
raise Error,
|
|
529
|
-
"path '#{path}' resolves under a denied credential location " \
|
|
530
|
-
"('#{denied}') — even in ALLOW_ALL mode, ~/.ssh / ~/.aws / " \
|
|
531
|
-
'~/.gnupg / ~/.docker / ~/.kube / ~/.netrc / /etc/shadow ' \
|
|
532
|
-
'are off-limits.'
|
|
533
|
-
end
|
|
534
|
-
|
|
535
|
-
resolved
|
|
536
|
-
end
|
|
250
|
+
path
|
|
537
251
|
end
|
|
538
252
|
end
|
|
539
253
|
end
|