mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 20:34:28 +00:00
feat(agent): add output truncation and streaming limits to sandboxed bash tool
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
import { randomBytes } from 'node:crypto'
|
||||
import { createWriteStream, type WriteStream } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
DEFAULT_MAX_BYTES,
|
||||
DEFAULT_MAX_LINES,
|
||||
type TruncationResult,
|
||||
truncateTail,
|
||||
} from './truncate'
|
||||
|
||||
export interface OutputAccumulatorOptions {
|
||||
maxLines?: number
|
||||
maxBytes?: number
|
||||
tempFilePrefix?: string
|
||||
}
|
||||
|
||||
export interface OutputSnapshot {
|
||||
content: string
|
||||
truncation: TruncationResult
|
||||
fullOutputPath?: string
|
||||
}
|
||||
|
||||
function defaultTempFilePath(prefix: string): string {
|
||||
const id = randomBytes(8).toString('hex')
|
||||
return join(tmpdir(), `${prefix}-${id}.log`)
|
||||
}
|
||||
|
||||
function byteLength(text: string): number {
|
||||
return Buffer.byteLength(text, 'utf-8')
|
||||
}
|
||||
|
||||
export class OutputAccumulator {
|
||||
private readonly maxLines: number
|
||||
private readonly maxBytes: number
|
||||
private readonly maxRollingBytes: number
|
||||
private readonly tempFilePrefix: string
|
||||
private readonly decoder = new TextDecoder()
|
||||
|
||||
private rawChunks: Buffer[] = []
|
||||
private tailText = ''
|
||||
private tailBytes = 0
|
||||
private tailStartsAtLineBoundary = true
|
||||
private totalRawBytes = 0
|
||||
private totalDecodedBytes = 0
|
||||
private completedLines = 0
|
||||
private totalLines = 0
|
||||
private currentLineBytes = 0
|
||||
private hasOpenLine = false
|
||||
private finished = false
|
||||
|
||||
private tempFilePath: string | undefined
|
||||
private tempFileStream: WriteStream | undefined
|
||||
|
||||
constructor(options: OutputAccumulatorOptions = {}) {
|
||||
this.maxLines = options.maxLines ?? DEFAULT_MAX_LINES
|
||||
this.maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES
|
||||
this.maxRollingBytes = Math.max(this.maxBytes * 2, 1)
|
||||
this.tempFilePrefix = options.tempFilePrefix ?? 'shumai-bash'
|
||||
}
|
||||
|
||||
append(data: Buffer): void {
|
||||
if (this.finished) {
|
||||
throw new Error('Cannot append to a finished output accumulator')
|
||||
}
|
||||
|
||||
this.totalRawBytes += data.length
|
||||
this.appendDecodedText(this.decoder.decode(data, { stream: true }))
|
||||
|
||||
if (this.tempFileStream || this.shouldUseTempFile()) {
|
||||
this.ensureTempFile()
|
||||
this.tempFileStream?.write(data)
|
||||
} else if (data.length > 0) {
|
||||
this.rawChunks.push(data)
|
||||
}
|
||||
}
|
||||
|
||||
finish(): void {
|
||||
if (this.finished) {
|
||||
return
|
||||
}
|
||||
this.finished = true
|
||||
this.appendDecodedText(this.decoder.decode())
|
||||
if (this.shouldUseTempFile()) {
|
||||
this.ensureTempFile()
|
||||
}
|
||||
}
|
||||
|
||||
snapshot(options: { persistIfTruncated?: boolean } = {}): OutputSnapshot {
|
||||
const tailTruncation = truncateTail(this.getSnapshotText(), {
|
||||
maxLines: this.maxLines,
|
||||
maxBytes: this.maxBytes,
|
||||
})
|
||||
const truncated = this.totalLines > this.maxLines || this.totalDecodedBytes > this.maxBytes
|
||||
const truncatedBy = truncated
|
||||
? (tailTruncation.truncatedBy ?? (this.totalDecodedBytes > this.maxBytes ? 'bytes' : 'lines'))
|
||||
: null
|
||||
const truncation: TruncationResult = {
|
||||
...tailTruncation,
|
||||
truncated,
|
||||
truncatedBy,
|
||||
totalLines: this.totalLines,
|
||||
totalBytes: this.totalDecodedBytes,
|
||||
maxLines: this.maxLines,
|
||||
maxBytes: this.maxBytes,
|
||||
}
|
||||
|
||||
if (options.persistIfTruncated && truncation.truncated) {
|
||||
this.ensureTempFile()
|
||||
}
|
||||
|
||||
return {
|
||||
content: truncation.content,
|
||||
truncation,
|
||||
fullOutputPath: this.tempFilePath,
|
||||
}
|
||||
}
|
||||
|
||||
async closeTempFile(): Promise<void> {
|
||||
if (!this.tempFileStream) {
|
||||
return
|
||||
}
|
||||
|
||||
const stream = this.tempFileStream
|
||||
this.tempFileStream = undefined
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const onError = (error: Error) => {
|
||||
stream.off('finish', onFinish)
|
||||
reject(error)
|
||||
}
|
||||
const onFinish = () => {
|
||||
stream.off('error', onError)
|
||||
resolve()
|
||||
}
|
||||
stream.once('error', onError)
|
||||
stream.once('finish', onFinish)
|
||||
stream.end()
|
||||
})
|
||||
}
|
||||
|
||||
getLastLineBytes(): number {
|
||||
return this.currentLineBytes
|
||||
}
|
||||
|
||||
private appendDecodedText(text: string): void {
|
||||
if (text.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const bytes = byteLength(text)
|
||||
this.totalDecodedBytes += bytes
|
||||
this.tailText += text
|
||||
this.tailBytes += bytes
|
||||
if (this.tailBytes > this.maxRollingBytes * 2) {
|
||||
this.trimTail()
|
||||
}
|
||||
|
||||
let newlines = 0
|
||||
let lastNewline = -1
|
||||
for (let i = text.indexOf('\n'); i !== -1; i = text.indexOf('\n', i + 1)) {
|
||||
newlines++
|
||||
lastNewline = i
|
||||
}
|
||||
if (newlines === 0) {
|
||||
this.currentLineBytes += bytes
|
||||
this.hasOpenLine = true
|
||||
} else {
|
||||
this.completedLines += newlines
|
||||
const tail = text.slice(lastNewline + 1)
|
||||
this.currentLineBytes = byteLength(tail)
|
||||
this.hasOpenLine = tail.length > 0
|
||||
}
|
||||
this.totalLines = this.completedLines + (this.hasOpenLine ? 1 : 0)
|
||||
}
|
||||
|
||||
private trimTail(): void {
|
||||
const buffer = Buffer.from(this.tailText, 'utf-8')
|
||||
if (buffer.length <= this.maxRollingBytes) {
|
||||
this.tailBytes = buffer.length
|
||||
return
|
||||
}
|
||||
|
||||
let start = buffer.length - this.maxRollingBytes
|
||||
while (start < buffer.length && (buffer[start] & 0xc0) === 0x80) {
|
||||
start++
|
||||
}
|
||||
|
||||
this.tailStartsAtLineBoundary =
|
||||
start === 0 ? this.tailStartsAtLineBoundary : buffer[start - 1] === 0x0a
|
||||
this.tailText = buffer.subarray(start).toString('utf-8')
|
||||
this.tailBytes = byteLength(this.tailText)
|
||||
}
|
||||
|
||||
private getSnapshotText(): string {
|
||||
if (this.tailStartsAtLineBoundary) {
|
||||
return this.tailText
|
||||
}
|
||||
|
||||
const firstNewline = this.tailText.indexOf('\n')
|
||||
return firstNewline === -1 ? this.tailText : this.tailText.slice(firstNewline + 1)
|
||||
}
|
||||
|
||||
private shouldUseTempFile(): boolean {
|
||||
return (
|
||||
this.totalRawBytes > this.maxBytes ||
|
||||
this.totalDecodedBytes > this.maxBytes ||
|
||||
this.totalLines > this.maxLines
|
||||
)
|
||||
}
|
||||
|
||||
private ensureTempFile(): void {
|
||||
if (this.tempFilePath) {
|
||||
return
|
||||
}
|
||||
this.tempFilePath = defaultTempFilePath(this.tempFilePrefix)
|
||||
this.tempFileStream = createWriteStream(this.tempFilePath)
|
||||
for (const chunk of this.rawChunks) {
|
||||
this.tempFileStream.write(chunk)
|
||||
}
|
||||
this.rawChunks = []
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
export const DEFAULT_MAX_LINES = 2000
|
||||
export const DEFAULT_MAX_BYTES = 50 * 1024 // 50KB
|
||||
|
||||
export interface TruncationResult {
|
||||
content: string
|
||||
truncated: boolean
|
||||
truncatedBy: 'lines' | 'bytes' | null
|
||||
totalLines: number
|
||||
totalBytes: number
|
||||
outputLines: number
|
||||
outputBytes: number
|
||||
lastLinePartial: boolean
|
||||
firstLineExceedsLimit: boolean
|
||||
maxLines: number
|
||||
maxBytes: number
|
||||
}
|
||||
|
||||
export interface TruncationOptions {
|
||||
maxLines?: number
|
||||
maxBytes?: number
|
||||
}
|
||||
|
||||
export function formatSize(bytes: number): string {
|
||||
if (bytes < 1024) {
|
||||
return `${bytes}B`
|
||||
} else if (bytes < 1024 * 1024) {
|
||||
return `${(bytes / 1024).toFixed(1)}KB`
|
||||
} else {
|
||||
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`
|
||||
}
|
||||
}
|
||||
|
||||
function splitLinesForCounting(content: string): string[] {
|
||||
if (content.length === 0) {
|
||||
return []
|
||||
}
|
||||
const lines = content.split('\n')
|
||||
if (content.endsWith('\n')) {
|
||||
lines.pop()
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
function truncateStringToBytesFromEnd(str: string, maxBytes: number): string {
|
||||
const buf = Buffer.from(str, 'utf-8')
|
||||
if (buf.length <= maxBytes) {
|
||||
return str
|
||||
}
|
||||
|
||||
let start = buf.length - maxBytes
|
||||
while (start < buf.length && (buf[start] & 0xc0) === 0x80) {
|
||||
start++
|
||||
}
|
||||
|
||||
return buf.subarray(start).toString('utf-8')
|
||||
}
|
||||
|
||||
export function truncateTail(content: string, options: TruncationOptions = {}): TruncationResult {
|
||||
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES
|
||||
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES
|
||||
|
||||
const totalBytes = Buffer.byteLength(content, 'utf-8')
|
||||
const lines = splitLinesForCounting(content)
|
||||
const totalLines = lines.length
|
||||
|
||||
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
||||
return {
|
||||
content,
|
||||
truncated: false,
|
||||
truncatedBy: null,
|
||||
totalLines,
|
||||
totalBytes,
|
||||
outputLines: totalLines,
|
||||
outputBytes: totalBytes,
|
||||
lastLinePartial: false,
|
||||
firstLineExceedsLimit: false,
|
||||
maxLines,
|
||||
maxBytes,
|
||||
}
|
||||
}
|
||||
|
||||
const outputLinesArr: string[] = []
|
||||
let outputBytesCount = 0
|
||||
let truncatedBy: 'lines' | 'bytes' = 'lines'
|
||||
let lastLinePartial = false
|
||||
|
||||
for (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {
|
||||
const line = lines[i]
|
||||
const lineBytes = Buffer.byteLength(line, 'utf-8') + (outputLinesArr.length > 0 ? 1 : 0)
|
||||
|
||||
if (outputBytesCount + lineBytes > maxBytes) {
|
||||
truncatedBy = 'bytes'
|
||||
if (outputLinesArr.length === 0) {
|
||||
const truncatedLine = truncateStringToBytesFromEnd(line, maxBytes)
|
||||
outputLinesArr.unshift(truncatedLine)
|
||||
outputBytesCount = Buffer.byteLength(truncatedLine, 'utf-8')
|
||||
lastLinePartial = true
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
outputLinesArr.unshift(line)
|
||||
outputBytesCount += lineBytes
|
||||
}
|
||||
|
||||
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
||||
truncatedBy = 'lines'
|
||||
}
|
||||
|
||||
const outputContent = outputLinesArr.join('\n')
|
||||
const finalOutputBytes = Buffer.byteLength(outputContent, 'utf-8')
|
||||
|
||||
return {
|
||||
content: outputContent,
|
||||
truncated: true,
|
||||
truncatedBy,
|
||||
totalLines,
|
||||
totalBytes,
|
||||
outputLines: outputLinesArr.length,
|
||||
outputBytes: finalOutputBytes,
|
||||
lastLinePartial,
|
||||
firstLineExceedsLimit: false,
|
||||
maxLines,
|
||||
maxBytes,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user