Files

110 lines
3.3 KiB
Go

package opencode
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"strings"
)
type boundedOutput struct {
bytes.Buffer
overflow bool
limit int
}
func (b *boundedOutput) Write(p []byte) (int, error) {
if b.limit < 0 {
return b.Buffer.Write(p)
}
n := len(p)
remaining := b.limit - b.Len()
if n > remaining {
b.overflow = true
p = p[:remaining]
}
_, _ = b.Buffer.Write(p)
return n, nil
}
// WriteByte keeps byte-oriented history scanning under the same bound as Write;
// the embedded Buffer's promoted WriteByte would otherwise bypass the cap.
func (b *boundedOutput) WriteByte(c byte) error {
if b.limit >= 0 && b.Len() >= b.limit {
b.overflow = true
return nil
}
return b.Buffer.WriteByte(c)
}
// ReadFrom reads through the bounded Write so the cap applies even when
// os/exec's io.Copy selects a ReadFrom on the destination: without this the
// embedded bytes.Buffer's promoted ReadFrom would bypass Write and buffer the
// whole output without bound, making overflow (and the truncation path) dead.
func (b *boundedOutput) ReadFrom(r io.Reader) (int64, error) {
var total int64
buffer := make([]byte, 32<<10)
for {
n, err := r.Read(buffer)
if n > 0 {
if _, writeErr := b.Write(buffer[:n]); writeErr != nil {
return total, writeErr
}
total += int64(n)
}
if err != nil {
if errors.Is(err, io.EOF) {
return total, nil
}
return total, err
}
}
}
// Messages preserves old sessions with native OutputFormat readback failures:
// the CLI export reads the local stored data without that HTTP encoder. It does
// not regenerate, repair the database, delete history or change conversations.
func (c *Connection) Messages(ctx context.Context, id string) ([]byte, error) {
if !sessionID.MatchString(id) {
return nil, fmt.Errorf("invalid session ID")
}
c.MessageReadbackDiagnostic = ""
data, err := c.Native(ctx, "get", "/session/"+id+"/message", nil)
if err == nil {
return data, nil
}
if !strings.Contains(err.Error(), "HTTP 400") || !strings.Contains(string(data), "Expected OutputFormat") {
return data, err
}
if ctx.Err() != nil {
return data, ctx.Err()
}
executable := c.Executable
if executable == "" {
executable = Executable()
}
res, exportErr := exportNative(ctx, executable, c.Directory, c.Backend, id, MaxResponse, c)
if exportErr != nil {
if ctx.Err() != nil {
return data, ctx.Err()
}
// Keep the original HTTP-400/OutputFormat evidence identity while also
// preserving exec.ErrWaitDelay from a descendant-held output pipe.
return data, errors.Join(err, fmt.Errorf("read-only native export fallback failed: %w", exportErr))
}
if res.truncated {
return data, fmt.Errorf("%w; native export exceeds 1 MiB evidence limit", err)
}
messages, ok := validNativeExport(res.envelope, id)
if !ok {
return data, fmt.Errorf("%w; native export returned invalid or mismatched session evidence", err)
}
c.MessageReadbackDiagnostic = "Native HTTP message readback rejected stored OutputFormat (HTTP 400); read-only CLI export recovered the original messages without modifying the session."
if c.Backend == "v2" {
c.MessageReadbackDiagnostic = "Native HTTP message readback rejected stored OutputFormat (HTTP 400); read-only HTTP export recovered the original messages without modifying the session."
}
return messages, nil
}