Repository navigation
Expand file tree
/
Copy pathshell.go
More file actions
430 lines (406 loc) · 13.6 KB
/
Copy pathshell.go
File metadata and controls
430 lines (406 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package coding
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"unicode/utf8"
"github.com/spice-framework/spice-agent/process"
"github.com/spice-framework/spice-agent/tool"
"github.com/spice-framework/spice/lifecycle"
)
const (
shellInputSchema = `{"type":"object","additionalProperties":false,"required":["argv"],"properties":{"argv":{"type":"array","minItems":1,"maxItems":128,"items":{"type":"string"}},"workdir":{"type":"string"}}}`
maximumArguments = 128
maximumArgvBytes = 64 << 10
)
type shellTool struct {
config Config
definition tool.Definition
resolver process.ExecutableResolver
launcher process.Launcher
ownershipMu sync.Mutex
closing bool
active int
reservations int
owned []*ownedProcess
drained chan struct{}
drainedOnce sync.Once
cleanupToken chan struct{}
}
type shellArguments struct {
Argv []string `json:"argv"`
Workdir string `json:"workdir,omitempty"`
}
type shellContent struct {
OK bool `json:"ok"`
Workdir string `json:"workdir"`
ExitCode int64 `json:"exit_code"`
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
Encoding string `json:"encoding"`
StdoutBytes int64 `json:"stdout_bytes"`
StderrBytes int64 `json:"stderr_bytes"`
OutputTruncated bool `json:"output_truncated"`
TimedOut bool `json:"timed_out"`
ManagedCleanupCompleted bool `json:"managed_cleanup_completed"`
}
// NewShell constructs the exact Spice Agent discrete-argv shell tool binding
// without starting a process.
func NewShell(
config Config,
resolver process.ExecutableResolver,
launcher process.Launcher,
) (tool.Tool, lifecycle.Cleanup, error) {
normalized, err := validatedConfig(config)
if err != nil {
return nil, nil, err
}
if resolver == nil {
return nil, nil, errors.New("shell executable resolver must not be nil")
}
if launcher == nil {
return nil, nil, errors.New("shell process launcher must not be nil")
}
definition, err := tool.NewDefinition(
"shell",
"Execute unsandboxed discrete argv from a worktree-selected directory with bounded output.",
json.RawMessage(shellInputSchema),
tool.EffectMutating,
tool.ReplayUnsafe,
tool.CapabilityFilesystemRead,
tool.CapabilityFilesystemWrite,
tool.CapabilityProcessExecute,
tool.CapabilityNetworkAccess,
tool.CapabilitySecretsRead,
tool.CapabilityEnvironmentRead,
tool.CapabilityEnvironmentWrite,
)
if err != nil {
return nil, nil, err
}
shell := &shellTool{
config: normalized, definition: definition, resolver: resolver, launcher: launcher,
drained: make(chan struct{}), cleanupToken: make(chan struct{}, 1),
}
return shell, shell.cleanup, nil
}
func (shell *shellTool) Definition() tool.Definition { return shell.definition.Clone() }
func (shell *shellTool) Execute(ctx context.Context, call tool.Call, reporter tool.Reporter) (tool.Result, error) {
if err := validateCall(call, shell.definition.Name()); err != nil {
return modelFailure(call.ID(), err)
}
if result, err, stop := initialContextOutcome(ctx, call.ID(), tool.RetryNever); stop {
return result, err
}
var arguments shellArguments
if err := decodeArguments(call.Arguments(), &arguments); err != nil {
return modelFailure(call.ID(), err)
}
if err := validateArgv(arguments.Argv); err != nil {
return modelFailure(call.ID(), err)
}
workdir := arguments.Workdir
if workdir == "" {
workdir = "."
}
path, err := parseRelativePath(workdir, true)
if err != nil {
return modelFailure(call.ID(), err)
}
if progressErr := reportProgress(
ctx, reporter, call.ID(), "starting bounded child process", tool.RetryNever,
); progressErr != nil {
return tool.Result{}, progressErr
}
lease, err := shell.beginExecution()
if err != nil {
return modelFailure(call.ID(), err)
}
defer lease.finish()
content, problem, err := shell.execute(ctx, arguments.Argv, path, lease)
if err != nil {
if uncertain, present := errors.AsType[executionUncertainty](err); present {
if uncertain.preserveContext {
return tool.Result{}, contextInfrastructureFailure(
call.ID(), uncertain.state, tool.RetryNever,
uncertain.message, uncertain.cause,
)
}
return tool.Result{}, infrastructureFailure(
call.ID(), uncertain.state, tool.RetryNever,
uncertain.message,
)
}
return modelFailure(call.ID(), err)
}
result, err := encodeShellResult(call.ID(), content, problem)
if err != nil {
return tool.Result{}, infrastructureFailure(
call.ID(), tool.ExecutionUncertain, tool.RetryNever,
"command result could not be encoded",
)
}
return result, nil
}
func (shell *shellTool) execute(
ctx context.Context,
argv []string,
workdir relativePath,
lease *executionLease,
) (shellContent, string, error) {
workspace, directory, spec, stdout, stderr, err := shell.prepareProcess(ctx, argv, workdir)
if err != nil {
return shellContent{}, "", err
}
defer closeBestEffort(workspace)
defer closeBestEffort(directory)
outcome := shell.runProcess(ctx, spec, lease)
content := capturedShellContent(workdir, stdout, stderr, outcome)
problem, err := classifyProcessOutcome(outcome)
if err != nil {
return shellContent{}, "", err
}
return content, problem, nil
}
func (shell *shellTool) prepareProcess(
ctx context.Context,
argv []string,
workdir relativePath,
) (*os.Root, *os.Root, process.Spec, *boundedCapture, *boundedCapture, error) {
workspace, err := openWorktree(shell.config.Root)
if err != nil {
return nil, nil, process.Spec{}, nil, nil, err
}
if pathErr := rejectSymlinkComponents(workspace, workdir); pathErr != nil {
closeBestEffort(workspace)
return nil, nil, process.Spec{}, nil, nil, pathErr
}
directory, err := workspace.OpenRoot(workdir.native)
if err != nil {
closeBestEffort(workspace)
return nil, nil, process.Spec{}, nil, nil,
executionFailure("workdir_unavailable", "command workdir is unavailable or escapes the configured worktree")
}
info, err := directory.Stat(".")
if err != nil || !info.IsDir() {
closeBestEffort(directory)
closeBestEffort(workspace)
return nil, nil, process.Spec{}, nil, nil, executionFailure("workdir_unavailable", "command workdir is not a directory")
}
stdout, stderr := newCapturePair(shell.config.MaxOutputBytes)
environment := selectedEnvironment(shell.config.EnvironmentAllowlist)
workingDirectory := filepath.Join(shell.config.Root, workdir.native)
lookup, err := process.NewLookup(argv[0], workingDirectory, environment)
if err != nil {
closeBestEffort(directory)
closeBestEffort(workspace)
return nil, nil, process.Spec{}, nil, nil, executionUncertainty{
message: "command executable lookup could not be prepared", cause: err,
state: tool.ExecutionDefinitive,
}
}
resolved, err := shell.resolver.Resolve(ctx, lookup)
if err != nil {
closeBestEffort(directory)
closeBestEffort(workspace)
return nil, nil, process.Spec{}, nil, nil, resolutionUncertainty(ctx, err)
}
spec, err := process.NewSpec(process.Config{
Executable: resolved, Arguments: argv[1:], WorkingDirectory: workingDirectory,
Environment: environment, Stdin: strings.NewReader(""), Stdout: stdout, Stderr: stderr,
Capabilities: shell.definition.Capabilities(),
})
if err != nil {
closeBestEffort(directory)
closeBestEffort(workspace)
return nil, nil, process.Spec{}, nil, nil, executionUncertainty{
message: "resolved command executable is invalid", cause: err,
state: tool.ExecutionDefinitive,
}
}
if pathErr := rejectSymlinkComponents(workspace, workdir); pathErr != nil {
closeBestEffort(directory)
closeBestEffort(workspace)
return nil, nil, process.Spec{}, nil, nil, pathErr
}
return workspace, directory, spec, stdout, stderr, nil
}
func resolutionUncertainty(ctx context.Context, err error) executionUncertainty {
wrapped := process.NewFailure(process.OperationResolve, err)
if ctx != nil && ctx.Err() != nil {
return executionUncertainty{
message: "command executable resolution was cancelled", cause: ctx.Err(),
preserveContext: true, state: tool.ExecutionDefinitive,
}
}
return executionUncertainty{
message: "command executable could not be resolved", cause: wrapped,
state: tool.ExecutionDefinitive,
}
}
func capturedShellContent(
workdir relativePath,
stdout, stderr *boundedCapture,
outcome processOutcome,
) shellContent {
stdoutContent, stdoutBytes, truncated := stdout.snapshot()
stderrContent, stderrBytes, stderrTruncated := stderr.snapshot()
content := shellContent{
OK: outcome.hasResult && outcome.result.Successful() && outcome.resultErr == nil &&
outcome.launchErr == nil && outcome.contextErr == nil && !outcome.timedOut &&
outcome.controlErr == nil && outcome.waitErr == nil,
Workdir: workdir.display, ExitCode: outcomeExitCode(outcome),
Stdout: string(stdoutContent), Stderr: string(stderrContent), Encoding: "utf-8",
StdoutBytes: stdoutBytes, StderrBytes: stderrBytes,
OutputTruncated: truncated || stderrTruncated, TimedOut: outcome.timedOut,
ManagedCleanupCompleted: outcome.started && outcome.waitErr == nil,
}
if !utf8.Valid(stdoutContent) || !utf8.Valid(stderrContent) {
content.Encoding = "base64"
content.Stdout = base64.StdEncoding.EncodeToString(stdoutContent)
content.Stderr = base64.StdEncoding.EncodeToString(stderrContent)
}
return content
}
func classifyProcessOutcome(outcome processOutcome) (string, error) {
managedCleanupCompleted := !outcome.started || outcome.waitErr == nil
if outcome.contextErr != nil {
message := "command execution was cancelled"
if errors.Is(outcome.contextErr, context.DeadlineExceeded) {
message = "command execution exceeded the caller deadline"
}
if !managedCleanupCompleted {
message += " and managed launcher cleanup did not complete"
}
return "", executionUncertainty{
message: message, cause: outcome.contextErr, preserveContext: true, state: shellInterruptionState(outcome),
}
}
if outcome.timedOut {
if !managedCleanupCompleted {
return "", executionUncertainty{
message: "command timed out and managed launcher cleanup did not complete",
cause: outcome.waitErr, state: tool.ExecutionUncertain,
}
}
return "command exceeded the configured timeout", nil
}
if outcome.launchErr != nil {
if outcome.started {
return "", executionUncertainty{
message: "command launch was only partially observed",
cause: outcome.launchErr, state: tool.ExecutionUncertain,
}
}
return "command could not be started", nil
}
if outcome.resultErr != nil {
return "", executionUncertainty{
message: "command result could not be observed", cause: outcome.resultErr,
state: tool.ExecutionUncertain,
}
}
if !managedCleanupCompleted {
return "", executionUncertainty{
message: "command process ownership could not be safely released",
cause: outcome.waitErr, state: tool.ExecutionUncertain,
}
}
if outcome.controlErr != nil {
return "", executionUncertainty{
message: "command process control did not complete cleanly",
cause: outcome.controlErr, state: tool.ExecutionUncertain,
}
}
if !outcome.hasResult {
return "", executionUncertainty{
message: "command outcome could not be observed", state: tool.ExecutionUncertain,
}
}
if !outcome.result.Successful() {
return formatProcessProblem(outcome), nil
}
return "", nil
}
type executionUncertainty struct {
message string
cause error
preserveContext bool
state tool.ExecutionState
}
func (uncertainty executionUncertainty) Error() string { return uncertainty.message }
func (uncertainty executionUncertainty) Unwrap() error { return uncertainty.cause }
func shellInterruptionState(outcome processOutcome) tool.ExecutionState {
if outcome.started {
return tool.ExecutionUncertain
}
return tool.ExecutionDefinitive
}
func validateArgv(argv []string) error {
if len(argv) == 0 || len(argv) > maximumArguments {
return invalidArguments("argv must contain between 1 and 128 discrete arguments")
}
total := 0
for _, argument := range argv {
if argument == "" || strings.IndexByte(argument, 0) >= 0 {
return invalidArguments("argv entries must be non-empty and must not contain NUL")
}
total += len(argument)
if total > maximumArgvBytes {
return invalidArguments("argv exceeds the configured metadata bound")
}
}
return nil
}
func selectedEnvironment(allowlist []string) []string {
result := make([]string, 0, len(allowlist))
for _, name := range allowlist {
if value, present := os.LookupEnv(name); present {
result = append(result, name+"="+value)
}
}
slices.Sort(result)
return result
}
func outcomeExitCode(outcome processOutcome) int64 {
if !outcome.hasResult {
return -1
}
code, present := outcome.result.ExitCode()
if !present {
return -1
}
return code
}
func encodeShellResult(callID tool.CallID, content shellContent, problem string) (tool.Result, error) {
result, err := newShellResult(callID, content, problem)
if err == nil {
return result, nil
}
if content.Encoding == "utf-8" {
content.Encoding = "base64"
content.Stdout = base64.StdEncoding.EncodeToString([]byte(content.Stdout))
content.Stderr = base64.StdEncoding.EncodeToString([]byte(content.Stderr))
if result, err = newShellResult(callID, content, problem); err == nil {
return result, nil
}
}
return tool.Result{}, err
}
func newShellResult(callID tool.CallID, content shellContent, problem string) (tool.Result, error) {
encoded, err := json.Marshal(content)
if err != nil {
return tool.Result{}, err
}
if problem == "" {
return tool.NewResult(callID, encoded)
}
return tool.NewErrorResult(callID, encoded, problem)
}
var _ tool.Tool = (*shellTool)(nil)