Plan 05: Universal Streaming & Context Cancellation
Goal: Introduce streaming byte I/O (
io.Reader/io.Writerequivalent) and a cancellation/deadline context into the host runtime capabilities. Inspiration: Go'sio.Reader,io.Writer, andcontext.Context.
1. Problem Statement & Root Cause
Currently in runtime/kyna_host/include/kyna/execution/runtime_capabilities.hpp:
- Whole-Buffer Slurping:
FileSystemPort::readreturnsstd::optional<std::string>andFileSystemPort::writeacceptsstd::string. Reading a 2GB file requires allocating a 2GB contiguous heap string. - Synchronous Network & HTTP: Network and server requests block OS threads completely. There is no cancellation token or timeout capability passed through compiler or runtime pipelines.
2. Target Architecture
2.1 The Streaming Ports (IReader & IWriter)
namespace kyna::host {
class IReader {
public:
virtual ~IReader() = default;
// Reads up to `size` bytes into `buffer`. Returns bytes read (0 = EOF).
virtual size_t read(uint8_t* buffer, size_t size) = 0;
};
class IWriter {
public:
virtual ~IWriter() = default;
// Writes `size` bytes from `buffer`. Returns bytes written.
virtual size_t write(const uint8_t* buffer, size_t size) = 0;
};
class ICloser {
public:
virtual ~ICloser() = default;
virtual void close() = 0;
};
using ReadCloser = std::shared_ptr<IReader>;
using WriteCloser = std::shared_ptr<IWriter>;
} // namespace kyna::host2.2 Context Propagation & Cancellation
namespace kyna::host {
class Context {
public:
virtual ~Context() = default;
virtual bool isCancelled() const = 0;
virtual std::optional<std::chrono::system_clock::time_point> deadline() const = 0;
virtual void cancel() = 0;
static std::shared_ptr<Context> Background();
static std::pair<std::shared_ptr<Context>, std::function<void()>>
WithTimeout(const std::shared_ptr<Context>& parent, std::chrono::milliseconds timeout);
};
} // namespace kyna::host3. Implementation Steps
- Step 1: Add
IReaderandIWritertoruntime_capabilities.hpp - Step 2: Implement File Stream Adapters
- Implement
FileStreamReaderandFileStreamWriterunderruntime/kyna_host/src/capabilities/.
- Implement
- Step 3: Introduce
Contextto Compilation & Evaluation Pipelines- Pass
const Context&toAnalyzer::analyzeandInterpreter::evalso long-running operations can be cancelled safely.
- Pass
- Step 4: Expose Stream APIs to the Standard Library
- Expose
openFileStream(path, mode)returning stream handles to Kyna scripts.
- Expose
4. Verification Plan
- Unit Tests:
- Write stream chunking tests verifying that a 10MB file can be processed in 64KB chunks with constant memory usage.
- Write cancellation tests verifying that an infinite loop or heavy compilation aborts when the timeout fires.
Source captured: 2026-10-11