// packages/grpc_proto/examples/demo.vk // Self-contained verification suite & runnable example for `grpc_proto` and `grpc_proto/proto`. use [ grpc = grpc_proto pb = grpc_proto/proto h2 = net/http2 ] assert-true = fn [cond bool, label str] enum [ if not cond [ println (str/join ['[FAIL] ' label] '') os/exit 1 ] print (str/join ['[PASS] ' label] '') .ok ] // --- 1. Low-Level Protobuf Wire Format Primitives --- mut raw_buf = bytes/with-cap 64 _ = pb/push-sint64 mut raw_buf -1 _ = pb/push-sint64 mut raw_buf -9876543210 _ = pb/push-fixed64 mut raw_buf 0x0102030405060708 _ = pb/push-double mut raw_buf -3.14159265 _ = pb/push-fixed32 mut raw_buf 0xdeadbeef z0 = pb/read-sint64 raw_buf 0 z1 = pb/read-sint64 raw_buf z0.next_off f64_i = pb/read-fixed64 raw_buf z1.next_off dbl = pb/read-double raw_buf f64_i.next_off f32_i = pb/read-fixed32 raw_buf dbl.next_off _ = assert-true (z0.next_off == 1 and z0.value == -1 and z1.value == -9876543210) 'ZigZag sint64 encodes -1 in 1 byte and roundtrips large negative integers' _ = assert-true (f64_i.value == 0x0102030405060708 and f32_i.value == 0xdeadbeef and dbl.value < -3.1415 and dbl.value > -3.1416) 'fixed64, fixed32, and IEEE-754 double little-endian wire primitives' packed_z = pb/encode-packed-sint64s [-500 0 500 -1 42] unpacked_z = pb/decode-packed-sint64s packed_z _ = assert-true ((len unpacked_z) == 5 and unpacked_z[0] == -500 and unpacked_z[3] == -1 and unpacked_z[4] == 42) 'packed repeated sint64 slice roundtrip' // --- 2. Homoiconic Message Schema, Nested Messages & `.proto` IDL Generation --- span_attr = pb/define-message 'SpanAttr' (quote [ 1 key str, 2 val str ]) telemetry_event = (pb/define-message 'TelemetryEvent' (quote [ 1 trace_id str, 2 span_id i64, 3 latency_us sint64, 4 ok bool, 5 tags repeated str, 6 skew_deltas repeated sint64, 7 cpu_ratio double, 8 attrs repeated SpanAttr ])) \ pb/with-subschema span_attr \ pb/with-package 'veko.telemetry.v1' idl_txt = pb/to-proto3-idl telemetry_event idl_ok = (str/contains? idl_txt 'syntax = "proto3";') and (str/contains? idl_txt 'package veko.telemetry.v1;') and (str/contains? idl_txt 'message SpanAttr {') and (str/contains? idl_txt 'sint64 latency_us = 3;') and (str/contains? idl_txt 'repeated SpanAttr attrs = 8;') _ = assert-true idl_ok 'to-proto3-idl emits canonical proto3 schema text' event_in = { trace_id = '4bf92f3577b34da6a3ce929d0e0e4736' span_id = 81985529216486895 latency_us = -1425 ok = true tags = ['edge' 'grpc' 'ca-west-1'] skew_deltas = [-120 0 45 -99999] cpu_ratio = 0.875 attrs = [ { key = 'http.method', val = 'POST' } { key = 'rpc.system', val = 'grpc' } ] } mut wire_ev = pb/encode-msg telemetry_event event_in // Append an unknown future field (#99 varint = 777) to verify forward-compatible skipping _ = pb/encode-varint-field mut wire_ev 99 777 event_out = pb/decode-msg telemetry_event wire_ev ev_ok_1 = event_out.trace_id == '4bf92f3577b34da6a3ce929d0e0e4736' and event_out.span_id == 81985529216486895 and event_out.latency_us == -1425 and event_out.ok == true ev_ok_2 = (len event_out.tags) == 3 and event_out.tags[2] == 'ca-west-1' and (len event_out.skew_deltas) == 4 and event_out.skew_deltas[0] == -120 and event_out.skew_deltas[3] == -99999 ev_ok_3 = event_out.cpu_ratio == 0.875 and (len event_out.attrs) == 2 and event_out.attrs[1].val == 'grpc' _ = assert-true (ev_ok_1 and ev_ok_2 and ev_ok_3) 'nested + packed-repeated Protobuf wire roundtrip with unknown field skipping' // --- 3. gRPC 5-Byte Framing, Unary RPC, Server-Streaming RPC & HTTP/2 Adapter --- ingest_ack = pb/define-message 'IngestAck' (quote [ 1 accepted bool, 2 ingest_id str, 3 recorded_tags i64 ]) stream_query = pb/define-message 'StreamQuery' (quote [ 1 service_filter str, 2 limit i64 ]) mut srv = grpc/new-server _ = grpc/register-unary mut srv 'veko.telemetry.v1.Collector' 'ReportSpan' telemetry_event ingest_ack fn [ctx, req] [ if req.trace_id == 'missing' [ return (grpc/grpc-error (grpc/STATUS-NOT-FOUND) 'trace not found: missing') ] { accepted = req.ok ingest_id = str/join ['ing-' req.trace_id[0..8]] '' recorded_tags = len req.tags } ] _ = grpc/register-server-stream mut srv 'veko.telemetry.v1.Collector' 'WatchSpans' stream_query telemetry_event fn [ctx, q] [ mut items = [] mut idx = 0 while idx < q.limit [ push mut items { trace_id = str/join [q.service_filter '-trace-' (mold idx)] '' span_id = 1000 + idx latency_us = (0 - 50) * (idx + 1) ok = true tags = [q.service_filter 'stream'] skew_deltas = [-10 10] cpu_ratio = 0.25 attrs = [ { key = 'seq', val = (mold idx) } ] } idx = idx + 1 ] items ] // Unary call (with Zstd frame compression enabled via metadata) unary_req_wire = grpc/build-request-body/zstd telemetry_event event_in true unary_resp = grpc/dispatch-rpc srv '/veko.telemetry.v1.Collector/ReportSpan' unary_req_wire { 'grpc-encoding' = 'zstd' 'grpc-timeout' = '250m' } ack = grpc/decode-unary-response unary_resp ingest_ack unary_ok = unary_resp.status.code == (grpc/STATUS-OK) and ack.accepted == true and ack.ingest_id == 'ing-4bf92f35' and ack.recorded_tags == 3 _ = assert-true unary_ok 'unary gRPC dispatch + Zstd compressed 5-byte frame roundtrip' // Domain error status check (`NOT_FOUND = 5`) and `UNIMPLEMENTED = 12` mut missing_ev = event_in map/set mut missing_ev 'trace_id' 'missing' not_found_wire = grpc/build-request-body telemetry_event missing_ev nf_resp = grpc/dispatch-rpc srv '/veko.telemetry.v1.Collector/ReportSpan' not_found_wire {} unimpl_resp = grpc/dispatch-rpc srv '/veko.telemetry.v1.Collector/UnknownRpc' not_found_wire {} err_ok = nf_resp.status.code == (grpc/STATUS-NOT-FOUND) and unimpl_resp.status.code == (grpc/STATUS-UNIMPLEMENTED) _ = assert-true err_ok 'gRPC NOT_FOUND (5) and UNIMPLEMENTED (12) status propagation' // Server-streaming call via HTTP/2 `net/http2` handler adapter h2_fn = grpc/http2-handler srv stream_req_wire = grpc/build-request-body stream_query { service_filter = 'auth-gw', limit = 3 } h2_req = h2/Message { stream_id = 1 method = 'POST' path = '/veko.telemetry.v1.Collector/WatchSpans' headers = { 'content-type' = 'application/grpc+proto' } body = stream_req_wire } h2_raw_resp = (h2_fn h2_req) stream_grpc_resp = grpc/from-http2-message h2_raw_resp stream_events = grpc/decode-stream-response stream_grpc_resp telemetry_event stream_ok = (len stream_events) == 3 and stream_events[0].trace_id == 'auth-gw-trace-0' and stream_events[2].latency_us == -150 and stream_events[2].attrs[0].val == '2' _ = assert-true stream_ok 'server-streaming multi-frame gRPC call over net/http2 adapter' print 'grpc_proto: all verification checks passed'