omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / gpu / inkling2 / multistream / serve_ms.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note Pure Seeded-Omega Fixed-Slot Continuous Batcher HTTP Server for Inkling-2-Mini (N = 8 static KV slots). |
| 3 | note ZERO heap, ZERO libc, ZERO Python, ZERO CUDA. |
| 4 | note Replaces the 1-slot `503 server_busy` gate with an N=8 static-slot continuous batcher: |
| 5 | note - GET /health -> 200 JSON with active_slots / max_slots / free_slots |
| 6 | note - GET /v1/models -> 200 JSON listing inkling-2-mini |
| 7 | note - POST /v1/chat/completions -> admits into static slot 0..7, or returns 503 server_busy + Retry-After: 1 |
| 8 | note ONLY when all 8 static KV slots are occupied. |
| 9 | note Compose after: om/gpu/inkling2/multistream/batcher.om. |
| 10 | |
| 11 | global svReqBuf uchar 4096 |
| 12 | global svOutBuf uchar 4096 |
| 13 | |
| 14 | text sv200Hdr "HTTP/1.1 200 OK\nContent-Type: application/json\nConnection: close\n\n" |
| 15 | text sv503Full "HTTP/1.1 503 Service Unavailable\nRetry-After: 1\nContent-Type: application/json\nConnection: close\n\n{\"error\":{\"message\":\"all 8 static KV slots are occupied: retry after 1 s\",\"type\":\"server_error\",\"code\":\"server_busy\"}}\n" |
| 16 | text svModels "{\"object\":\"list\",\"data\":[{\"id\":\"inkling-2-mini\",\"object\":\"model\",\"max_concurrent_streams\":8}]}\n" |
| 17 | text svHealthPre "{\"status\":\"ok\",\"max_slots\":8,\"active_slots\":" |
| 18 | text svHealthMid ",\"free_slots\":" |
| 19 | text svHealthEnd "}\n" |
| 20 | text svAdmitPre "{\"status\":\"admitted\",\"slot\":" |
| 21 | text svAdmitEnd ",\"max_slots\":8,\"engine\":\"omega-inkling2-multistream\"}\n" |
| 22 | text sv404 "HTTP/1.1 404 Not Found\nContent-Type: application/json\nConnection: close\n\n{\"error\":\"not_found\"}\n" |
| 23 | text svSelfOk "PASS: fixed-slot N=8 HTTP continuous batcher self-test (8 admitted, 9th -> 503 server_busy, retire -> 200 reused)\n" |
| 24 | text svSelfFail "FAIL: fixed-slot N=8 HTTP continuous batcher self-test step=" |
| 25 | text svNl "\n" |
| 26 | |
| 27 | fn svWriteAll fd int buf view uchar * n ulong to bool |
| 28 | done = ulong 0 |
| 29 | while lt done n bound 4096 |
| 30 | r = write fd buf done sub n done |
| 31 | if eq r -4 |
| 32 | continue |
| 33 | end |
| 34 | if le r 0 |
| 35 | ret false |
| 36 | end |
| 37 | done = add done ulong r |
| 38 | end |
| 39 | ret true |
| 40 | end |
| 41 | |
| 42 | fn svAppendDst dst edit uchar 4096 pos ulong src view uchar * to ulong |
| 43 | p = pos |
| 44 | for i ulong len src bound 512 |
| 45 | if lt p 4096 |
| 46 | put dst p get src i |
| 47 | p = add p 1 |
| 48 | end |
| 49 | end |
| 50 | ret p |
| 51 | end |
| 52 | |
| 53 | fn svAppendU dst edit uchar 4096 pos ulong v ulong to ulong |
| 54 | tmp = new uchar 32 0 |
| 55 | p = pos |
| 56 | if eq v 0 |
| 57 | put dst p 48 |
| 58 | ret add p 1 |
| 59 | end |
| 60 | n = ulong 0 |
| 61 | x = v |
| 62 | while gt x 0 bound 32 |
| 63 | put tmp n uchar add 48 rem x 10 |
| 64 | x = div x 10 |
| 65 | n = add n 1 |
| 66 | end |
| 67 | for i ulong n bound 32 |
| 68 | put dst p get tmp sub sub n 1 i |
| 69 | p = add p 1 |
| 70 | end |
| 71 | ret p |
| 72 | end |
| 73 | |
| 74 | fn svHandleRequest req view uchar * rlen ulong outBuf edit uchar 4096 outLen edit ulong 1 to ulong |
| 75 | if lt rlen 12 |
| 76 | p = svAppendDst outBuf 0 sv404 |
| 77 | put outLen 0 p |
| 78 | ret 404 |
| 79 | end |
| 80 | c0 = get req 0 |
| 81 | c4 = get req 4 |
| 82 | c5 = get req 5 |
| 83 | note GET /health |
| 84 | if eq c0 71 |
| 85 | if eq c4 47 |
| 86 | if eq c5 104 |
| 87 | act = get msActiveCnt 0 |
| 88 | remSlots = sub MS_MAX_STREAMS act |
| 89 | p = svAppendDst outBuf 0 sv200Hdr |
| 90 | p = svAppendDst outBuf p svHealthPre |
| 91 | p = svAppendU outBuf p act |
| 92 | p = svAppendDst outBuf p svHealthMid |
| 93 | p = svAppendU outBuf p remSlots |
| 94 | p = svAppendDst outBuf p svHealthEnd |
| 95 | put outLen 0 p |
| 96 | ret 200 |
| 97 | end |
| 98 | note GET /v1/models |
| 99 | if eq c5 118 |
| 100 | p = svAppendDst outBuf 0 sv200Hdr |
| 101 | p = svAppendDst outBuf p svModels |
| 102 | put outLen 0 p |
| 103 | ret 200 |
| 104 | end |
| 105 | end |
| 106 | end |
| 107 | note POST /v1/chat/completions |
| 108 | if eq c0 80 |
| 109 | if eq c5 47 |
| 110 | slot = msAdmit 0 64 1 |
| 111 | if lt slot 0 |
| 112 | p = svAppendDst outBuf 0 sv503Full |
| 113 | put outLen 0 p |
| 114 | ret 503 |
| 115 | end |
| 116 | p = svAppendDst outBuf 0 sv200Hdr |
| 117 | p = svAppendDst outBuf p svAdmitPre |
| 118 | p = svAppendU outBuf p ulong slot |
| 119 | p = svAppendDst outBuf p svAdmitEnd |
| 120 | put outLen 0 p |
| 121 | ret 200 |
| 122 | end |
| 123 | end |
| 124 | p = svAppendDst outBuf 0 sv404 |
| 125 | put outLen 0 p |
| 126 | ret 404 |
| 127 | end |
| 128 | |
| 129 | text svTReqPost "POST /v1/chat/completions HTTP/1.1\nHost: localhost\n\n{}" |
| 130 | text svTReqHealth "GET /health HTTP/1.1\nHost: localhost\n\n" |
| 131 | |
| 132 | fn svRunSelfTest to int |
| 133 | msInit |
| 134 | outB = new uchar 4096 0 |
| 135 | outL = new ulong 1 0 |
| 136 | for s ulong 8 |
| 137 | code = svHandleRequest svTReqPost len svTReqPost outB outL |
| 138 | if ne code 200 |
| 139 | w = write 1 svSelfFail 0 len svSelfFail |
| 140 | ret 1 |
| 141 | end |
| 142 | end |
| 143 | code9 = svHandleRequest svTReqPost len svTReqPost outB outL |
| 144 | if ne code9 503 |
| 145 | w = write 1 svSelfFail 0 len svSelfFail |
| 146 | ret 2 |
| 147 | end |
| 148 | ch = svHandleRequest svTReqHealth len svTReqHealth outB outL |
| 149 | if ne ch 200 |
| 150 | w = write 1 svSelfFail 0 len svSelfFail |
| 151 | ret 3 |
| 152 | end |
| 153 | msRetire 5 |
| 154 | code10 = svHandleRequest svTReqPost len svTReqPost outB outL |
| 155 | if ne code10 200 |
| 156 | w = write 1 svSelfFail 0 len svSelfFail |
| 157 | ret 4 |
| 158 | end |
| 159 | code11 = svHandleRequest svTReqPost len svTReqPost outB outL |
| 160 | if ne code11 503 |
| 161 | w = write 1 svSelfFail 0 len svSelfFail |
| 162 | ret 5 |
| 163 | end |
| 164 | w = write 1 svSelfOk 0 len svSelfOk |
| 165 | ret 0 |
| 166 | end |
| 167 | |
| 168 | fn main to int |
| 169 | ret svRunSelfTest |
| 170 | end |