 8d9b62daf3
			
		
	
	8d9b62daf3
	
	
	
		
			
			This commit implements Phase 2 of the CHORUS Task Execution Engine development plan, providing a comprehensive execution environment abstraction layer with Docker container sandboxing support. ## New Features ### Core Sandbox Interface - Comprehensive ExecutionSandbox interface with isolated task execution - Support for command execution, file I/O, environment management - Resource usage monitoring and sandbox lifecycle management - Standardized error handling with SandboxError types and categories ### Docker Container Sandbox Implementation - Full Docker API integration with secure container creation - Transparent repository mounting with configurable read/write access - Advanced security policies with capability dropping and privilege controls - Comprehensive resource limits (CPU, memory, disk, processes, file handles) - Support for tmpfs mounts, masked paths, and read-only bind mounts - Container lifecycle management with proper cleanup and health monitoring ### Security & Resource Management - Configurable security policies with SELinux, AppArmor, and Seccomp support - Fine-grained capability management with secure defaults - Network isolation options with configurable DNS and proxy settings - Resource monitoring with real-time CPU, memory, and network usage tracking - Comprehensive ulimits configuration for process and file handle limits ### Repository Integration - Seamless repository mounting from local paths to container workspaces - Git configuration support with user credentials and global settings - File inclusion/exclusion patterns for selective repository access - Configurable permissions and ownership for mounted repositories ### Testing Infrastructure - Comprehensive test suite with 60+ test cases covering all functionality - Docker integration tests with Alpine Linux containers (skipped in short mode) - Mock sandbox implementation for unit testing without Docker dependencies - Security policy validation tests with read-only filesystem enforcement - Resource usage monitoring and cleanup verification tests ## Technical Details ### Dependencies Added - github.com/docker/docker v28.4.0+incompatible - Docker API client - github.com/docker/go-connections v0.6.0 - Docker connection utilities - github.com/docker/go-units v0.5.0 - Docker units and formatting - Associated Docker API dependencies for complete container management ### Architecture - Interface-driven design enabling multiple sandbox implementations - Comprehensive configuration structures for all sandbox aspects - Resource usage tracking with detailed metrics collection - Error handling with retryable error classification - Proper cleanup and resource management throughout sandbox lifecycle ### Compatibility - Maintains backward compatibility with existing CHORUS architecture - Designed for future integration with Phase 3 Core Task Execution Engine - Extensible design supporting additional sandbox implementations (VM, process) This Phase 2 implementation provides the foundation for secure, isolated task execution that will be integrated with the AI model providers from Phase 1 in the upcoming Phase 3 development. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
		
			
				
	
	
		
			133 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			133 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package stringbuffer
 | |
| 
 | |
| import (
 | |
| 	"sync"
 | |
| 	"unicode/utf16"
 | |
| )
 | |
| 
 | |
| // TODO: worth exporting and using in mkwinsyscall?
 | |
| 
 | |
| // Uint16BufferSize is the buffer size in the pool, chosen somewhat arbitrarily to accommodate
 | |
| // large path strings:
 | |
| // MAX_PATH (260) + size of volume GUID prefix (49) + null terminator = 310.
 | |
| const MinWStringCap = 310
 | |
| 
 | |
| // use *[]uint16 since []uint16 creates an extra allocation where the slice header
 | |
| // is copied to heap and then referenced via pointer in the interface header that sync.Pool
 | |
| // stores.
 | |
| var pathPool = sync.Pool{ // if go1.18+ adds Pool[T], use that to store []uint16 directly
 | |
| 	New: func() interface{} {
 | |
| 		b := make([]uint16, MinWStringCap)
 | |
| 		return &b
 | |
| 	},
 | |
| }
 | |
| 
 | |
| func newBuffer() []uint16 { return *(pathPool.Get().(*[]uint16)) }
 | |
| 
 | |
| // freeBuffer copies the slice header data, and puts a pointer to that in the pool.
 | |
| // This avoids taking a pointer to the slice header in WString, which can be set to nil.
 | |
| func freeBuffer(b []uint16) { pathPool.Put(&b) }
 | |
| 
 | |
| // WString is a wide string buffer ([]uint16) meant for storing UTF-16 encoded strings
 | |
| // for interacting with Win32 APIs.
 | |
| // Sizes are specified as uint32 and not int.
 | |
| //
 | |
| // It is not thread safe.
 | |
| type WString struct {
 | |
| 	// type-def allows casting to []uint16 directly, use struct to prevent that and allow adding fields in the future.
 | |
| 
 | |
| 	// raw buffer
 | |
| 	b []uint16
 | |
| }
 | |
| 
 | |
| // NewWString returns a [WString] allocated from a shared pool with an
 | |
| // initial capacity of at least [MinWStringCap].
 | |
| // Since the buffer may have been previously used, its contents are not guaranteed to be empty.
 | |
| //
 | |
| // The buffer should be freed via [WString.Free]
 | |
| func NewWString() *WString {
 | |
| 	return &WString{
 | |
| 		b: newBuffer(),
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (b *WString) Free() {
 | |
| 	if b.empty() {
 | |
| 		return
 | |
| 	}
 | |
| 	freeBuffer(b.b)
 | |
| 	b.b = nil
 | |
| }
 | |
| 
 | |
| // ResizeTo grows the buffer to at least c and returns the new capacity, freeing the
 | |
| // previous buffer back into pool.
 | |
| func (b *WString) ResizeTo(c uint32) uint32 {
 | |
| 	// already sufficient (or n is 0)
 | |
| 	if c <= b.Cap() {
 | |
| 		return b.Cap()
 | |
| 	}
 | |
| 
 | |
| 	if c <= MinWStringCap {
 | |
| 		c = MinWStringCap
 | |
| 	}
 | |
| 	// allocate at-least double buffer size, as is done in [bytes.Buffer] and other places
 | |
| 	if c <= 2*b.Cap() {
 | |
| 		c = 2 * b.Cap()
 | |
| 	}
 | |
| 
 | |
| 	b2 := make([]uint16, c)
 | |
| 	if !b.empty() {
 | |
| 		copy(b2, b.b)
 | |
| 		freeBuffer(b.b)
 | |
| 	}
 | |
| 	b.b = b2
 | |
| 	return c
 | |
| }
 | |
| 
 | |
| // Buffer returns the underlying []uint16 buffer.
 | |
| func (b *WString) Buffer() []uint16 {
 | |
| 	if b.empty() {
 | |
| 		return nil
 | |
| 	}
 | |
| 	return b.b
 | |
| }
 | |
| 
 | |
| // Pointer returns a pointer to the first uint16 in the buffer.
 | |
| // If the [WString.Free] has already been called, the pointer will be nil.
 | |
| func (b *WString) Pointer() *uint16 {
 | |
| 	if b.empty() {
 | |
| 		return nil
 | |
| 	}
 | |
| 	return &b.b[0]
 | |
| }
 | |
| 
 | |
| // String returns the returns the UTF-8 encoding of the UTF-16 string in the buffer.
 | |
| //
 | |
| // It assumes that the data is null-terminated.
 | |
| func (b *WString) String() string {
 | |
| 	// Using [windows.UTF16ToString] would require importing "golang.org/x/sys/windows"
 | |
| 	// and would make this code Windows-only, which makes no sense.
 | |
| 	// So copy UTF16ToString code into here.
 | |
| 	// If other windows-specific code is added, switch to [windows.UTF16ToString]
 | |
| 
 | |
| 	s := b.b
 | |
| 	for i, v := range s {
 | |
| 		if v == 0 {
 | |
| 			s = s[:i]
 | |
| 			break
 | |
| 		}
 | |
| 	}
 | |
| 	return string(utf16.Decode(s))
 | |
| }
 | |
| 
 | |
| // Cap returns the underlying buffer capacity.
 | |
| func (b *WString) Cap() uint32 {
 | |
| 	if b.empty() {
 | |
| 		return 0
 | |
| 	}
 | |
| 	return b.cap()
 | |
| }
 | |
| 
 | |
| func (b *WString) cap() uint32 { return uint32(cap(b.b)) }
 | |
| func (b *WString) empty() bool { return b == nil || b.cap() == 0 }
 |