Class: Kobako::Capture

Inherits:
Object
  • Object
show all
Defined in:
lib/kobako/capture.rb

Overview

Host-side captured prefix of guest stdout / stderr produced during a single Kobako::Sandbox invocation, paired with the truncation flag the WASI pipe sets when the guest wrote past the configured per-channel cap (docs/behavior.md B-04).

Immutable value object: the captured bytes and the truncation flag always travel together and the instance is frozen on construction. Construct via Capture.from_ext for ext-provided binary bytes (handles UTF-8 / ASCII-8BIT fallback) or reach Capture::EMPTY for the pre- invocation sentinel that Sandbox uses before any invocation has executed.

Constant Summary collapse

EMPTY =

Pre-invocation sentinel (docs/behavior.md B-05). Empty UTF-8 bytes and truncated? == false; reused by every fresh Sandbox and by Sandbox between invocations to denote “no capture yet”.

new(bytes: "", truncated: false)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes:, truncated:) ⇒ Capture

Build a Capture wrapping bytes (the captured prefix as a String) and truncated (whether the originating WASI pipe reported the cap was hit). Freezes the instance so callers cannot mutate the pair.



21
22
23
24
25
# File 'lib/kobako/capture.rb', line 21

def initialize(bytes:, truncated:)
  @bytes = bytes
  @truncated = truncated
  freeze
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



16
17
18
# File 'lib/kobako/capture.rb', line 16

def bytes
  @bytes
end

Class Method Details

.from_ext(bytes, truncated) ⇒ Object

Construct a Capture from ext-provided binary bytes. Coerces bytes to UTF-8 when the bytes are valid UTF-8, otherwise falls back to ASCII-8BIT so invalid sequences remain inspectable without raising. bytes is not mutated.



36
37
38
39
40
# File 'lib/kobako/capture.rb', line 36

def self.from_ext(bytes, truncated)
  copy = bytes.dup.force_encoding(Encoding::UTF_8)
  copy.force_encoding(Encoding::ASCII_8BIT) unless copy.valid_encoding?
  new(bytes: copy, truncated: truncated)
end

Instance Method Details

#truncated?Boolean

Returns true iff the underlying capture channel exceeded its configured cap during the originating Sandbox invocation (docs/behavior.md B-04).

Returns:

  • (Boolean)


30
# File 'lib/kobako/capture.rb', line 30

def truncated? = @truncated