Make restricted artifact reads cooperatively cancellable

This commit is contained in:
2026-11-25 17:28:16 +00:00
parent 3f5b2958cb
commit e74061d5a0
5 changed files with 203 additions and 19 deletions

View File

@@ -60,7 +60,7 @@ func (r *restrictedArtifactReader) Read(ctx context.Context, ref promptkit.Artif
case promptkit.ArtifactRefInline:
return readInlineArtifact(ref)
case promptkit.ArtifactRefFile:
return r.readFileArtifact(ref)
return r.readFileArtifact(ctx, ref)
default:
return nil, fmt.Errorf("unsupported artifact reference type %q", ref.Type)
}
@@ -81,7 +81,7 @@ func readInlineArtifact(ref promptkit.ArtifactRef) (*promptkit.Artifact, error)
}, nil
}
func (r *restrictedArtifactReader) readFileArtifact(ref promptkit.ArtifactRef) (*promptkit.Artifact, error) {
func (r *restrictedArtifactReader) readFileArtifact(ctx context.Context, ref promptkit.ArtifactRef) (*promptkit.Artifact, error) {
if ref.URI == "" {
return nil, errors.New("file artifact path is required")
}
@@ -93,7 +93,7 @@ func (r *restrictedArtifactReader) readFileArtifact(ref promptkit.ArtifactRef) (
if err != nil {
return nil, err
}
return readArtifactFile(path, r.maxBytes)
return readArtifactFile(ctx, path, r.maxBytes)
}
// resolveLexicalPath checks cleaned path containment without resolving symlinks.
@@ -120,49 +120,97 @@ func (r *restrictedArtifactReader) resolveLexicalPath(rawPath string) (string, e
return absCandidate, nil
}
func readArtifactFile(path string, maxBytes int64) (*promptkit.Artifact, error) {
func readArtifactFile(ctx context.Context, path string, maxBytes int64) (*promptkit.Artifact, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
info, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("failed to stat file %s: %w", path, err)
}
if !info.Mode().IsRegular() {
return nil, fmt.Errorf("file %s is not a regular file", path)
}
if err := ctx.Err(); err != nil {
return nil, err
}
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %w", path, err)
}
defer file.Close()
info, err := file.Stat()
info, err = file.Stat()
if err != nil {
return nil, fmt.Errorf("failed to stat file %s: %w", path, err)
}
if !info.Mode().IsRegular() {
return nil, fmt.Errorf("file %s is not a regular file", path)
}
if maxBytes > 0 && info.Size() > maxBytes {
return nil, ErrFileTooLarge
}
var reader io.Reader = file
if maxBytes > 0 {
readLimit := maxBytes
if readLimit < math.MaxInt64 {
readLimit++ // Read one extra byte to detect an oversized file.
}
reader = io.LimitReader(file, readLimit)
}
body, err := io.ReadAll(reader)
body, err := readArtifactBody(ctx, file, maxBytes)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %w", path, err)
}
if maxBytes > 0 && int64(len(body)) > maxBytes {
return nil, ErrFileTooLarge
}
contentType := mime.TypeByExtension(filepath.Ext(path))
if contentType == "" {
contentType = fallbackArtifactContentType
}
return &promptkit.Artifact{
artifact := &promptkit.Artifact{
Name: filepath.Base(path),
ContentType: contentType,
Body: body,
URI: path,
Size: int64(len(body)),
Hash: artifactHash(body),
}, nil
}
if err := ctx.Err(); err != nil {
return nil, err
}
return artifact, nil
}
type contextReader struct {
ctx context.Context
reader io.Reader
}
func (r contextReader) Read(p []byte) (int, error) {
if err := r.ctx.Err(); err != nil {
return 0, err
}
n, err := r.reader.Read(p)
if ctxErr := r.ctx.Err(); ctxErr != nil {
return n, ctxErr
}
return n, err
}
func readArtifactBody(ctx context.Context, source io.Reader, maxBytes int64) ([]byte, error) {
var reader io.Reader = contextReader{ctx: ctx, reader: source}
if maxBytes > 0 {
readLimit := maxBytes
if readLimit < math.MaxInt64 {
readLimit++ // Read one extra byte to detect an oversized file.
}
reader = io.LimitReader(reader, readLimit)
}
body, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
if maxBytes > 0 && int64(len(body)) > maxBytes {
return nil, ErrFileTooLarge
}
return body, nil
}
func artifactHash(body []byte) string {