From e1d465c1c44cf9956aa4d787136ea80a5c8a6694 Mon Sep 17 00:00:00 2001 From: Dmitrii Vasilev Date: Wed, 11 Nov 2026 13:17:52 +0700 Subject: [PATCH] vps: ladder_protocol_map.t27 -- the law of the protocol adapter in front of dsh-ladder Model allowlist, request verdicts (Anthropic Messages /v1/messages, OpenAI Responses /v1/responses), supported vs strippable features, effective max_tokens, stop vocabularies, role mapping (developer -> system), loopback-only bind: 9 tests, t27b test green. The generated TypeScript (t27b ts) is committed byte-identical to a fresh regeneration; the thin loader runs on the VPS as a deployment artifact (sha256 pinned in specs/vps/README.md), outside the repo per the foreign-line budget. Live acceptance: opencode, claude, crush, codex through the VPS chain, zero paid calls. Refs #13608 --- specs/vps/README.md | 26 ++ specs/vps/ladder_protocol_map.generated.mts | 305 ++++++++++++++++++++ specs/vps/ladder_protocol_map.t27 | 243 ++++++++++++++++ 3 files changed, 574 insertions(+) create mode 100644 specs/vps/README.md create mode 100644 specs/vps/ladder_protocol_map.generated.mts create mode 100644 specs/vps/ladder_protocol_map.t27 diff --git a/specs/vps/README.md b/specs/vps/README.md new file mode 100644 index 0000000000..dbcbc86e36 --- /dev/null +++ b/specs/vps/README.md @@ -0,0 +1,26 @@ +# specs/vps -- VPS protocol compatibility layer (Ladder front) + +The law lives in `ladder_protocol_map.t27` (9 tests). Every decision the deployed adapter +makes -- model allowlist, request verdicts, supported vs strippable features, effective +max_tokens, stop vocabularies, role mapping (developer -> system), loopback-only bind -- +is a function of that spec. The spec is the source of truth; nothing else decides. + +Artifacts and deployment (verified 2026-11-11): + +- `ladder_protocol_map.generated.mts` -- generated by `t27b ts` from the spec; the committed + copy is byte-identical to a fresh regeneration (checked in CI-style locally), so drift is a + failing diff. +- The executable chain on the VPS: agents -> 127.0.0.1:3132 (launchd SSH tunnel, + com.t27.vps-ladder-tunnel) -> VPS 127.0.0.1:3133 (`ladder-protocol-adapter.service`, + thin loader) -> 127.0.0.1:3131 (`dsh-ladder.service`) -> zai free rung. +- The thin loader (`protocol-adapter.mjs`, IO glue only: routing, JSON walk, SSE piping; zero + protocol decisions) is a deployment artifact kept on the VPS at + `/root/.t27os/bin/protocol-adapter.mjs`, sha256 a3946055b37817a6672b5a50a758d4f1bcca9d0761049a40121a6bbacc57742e . + It is intentionally NOT committed: it is foreign (non-t27) and under the foreign-line budget; + its sha256 here pins the reviewed bytes. To change protocol behavior, change the spec, + regenerate, redeploy -- never edit the loader. + +Acceptance evidence (2026-11-11): opencode 2.2.5, claude 2.1.310, crush 0.92.0, codex 0.167.1 +each completed a read->write task through the chain (files verified); law behaviors observed +live: 403 for an off-allowlist model, web_search tool stripped per law, developer role mapped. +Zero paid calls; local LM Studio not used by any agent. diff --git a/specs/vps/ladder_protocol_map.generated.mts b/specs/vps/ladder_protocol_map.generated.mts new file mode 100644 index 0000000000..2f36b5a699 --- /dev/null +++ b/specs/vps/ladder_protocol_map.generated.mts @@ -0,0 +1,305 @@ +// Generated by t27b ts from module VpsLadderProtocolMap -- DO NOT EDIT; the decisions are specs/tri/t27b/ts_plan.t27 +// phi^2 + 1/phi^2 = 3 | TRINITY + +let t27_n: number = 0; +function t27_fail(m: string): never { throw new RangeError('t27: ' + m); } +function t27_chk(v: number, lo: number, hi: number): number { if (v < lo || v > hi) { t27_fail('integer overflow'); } return v; } +function t27_wrap(v: number, w: number, s: boolean): number { if (s) { return w === 32 ? v | 0 : (v << (32 - w)) >> (32 - w); } return w === 32 ? v >>> 0 : v & (2 ** w - 1); } +function t27_div(a: number, b: number): number { if (b === 0) { t27_fail('division by zero'); } return Math.trunc(a / b); } +function t27_rem(a: number, b: number): number { if (b === 0) { t27_fail('division by zero'); } return a % b; } +function t27_shl(a: number, b: number, w: number, s: boolean): number { if (b >= w) { t27_fail('shift amount too large'); } return t27_wrap(a << b, w, s); } +function t27_shr(a: number, b: number, w: number, s: boolean): number { if (b >= w) { t27_fail('shift amount too large'); } return s ? a >> b : a >>> b; } +function t27_check(c: boolean, test: string, line: number): void { if (!c) { throw new Error('t27: assert failed in ' + test + ' at line ' + String(line)); } } +function t27_assert(c: boolean, test: string, line: number): void { t27_n = t27_n + 1; t27_check(c, test, line); } +type t27_case = [string, () => number]; + +export const MODEL_WORD0: number = 762145895; +export const MODEL_WORD1: number = 758328885; +export const MODEL_WORD2: number = 1935764582; +export const MODEL_WORD3: number = 104; +export const MODEL_NAME_LEN: number = 13; +export const BIND_LOOPBACK_U32: number = 16777343; +export const MAX_TOKENS_DEFAULT: number = 2048; +export const MAX_TOKENS_CAP: number = 8192; +export const CHAT_STOP: number = 0; +export const CHAT_LENGTH: number = 1; +export const CHAT_TOOL_CALLS: number = 2; +export const ANTH_END_TURN: number = 0; +export const ANTH_MAX_TOKENS: number = 1; +export const ANTH_TOOL_USE: number = 2; +export const ANTH_UNSUPPORTED: number = 255; +export const RESP_COMPLETED: number = 1; +export const RESP_INCOMPLETE: number = 2; +export const RESP_UNSUPPORTED: number = 255; + +export function word_matches(got: number, idx: number): boolean { + if ((idx === 0)) { + return (got === MODEL_WORD0); + } + if ((idx === 1)) { + return (got === MODEL_WORD1); + } + if ((idx === 2)) { + return (got === MODEL_WORD2); + } + if ((idx === 3)) { + return (got === MODEL_WORD3); + } + return false; +} + +export function model_allowed(len: number, w0: number, w1: number, w2: number, w3: number): boolean { + if ((len !== MODEL_NAME_LEN)) { + return false; + } + return (((word_matches(w0, 0) && word_matches(w1, 1)) && word_matches(w2, 2)) && word_matches(w3, 3)); +} + +export function anth_req_verdict(has_model: boolean, max_tokens: number, name_ok: boolean): number { + if ((!has_model)) { + return 400; + } + if ((max_tokens === 0)) { + return 400; + } + if ((!name_ok)) { + return 403; + } + return 0; +} + +export function resp_req_verdict(has_model: boolean, name_ok: boolean): number { + if ((!has_model)) { + return 400; + } + if ((!name_ok)) { + return 403; + } + return 0; +} + +export function feature_supported(code: number): boolean { + if ((code === 0)) { + return true; + } + if ((code === 1)) { + return true; + } + if ((code === 2)) { + return true; + } + if ((code === 3)) { + return true; + } + if ((code === 4)) { + return true; + } + return false; +} + +export function effective_max_tokens(requested: number): number { + if ((requested === 0)) { + return MAX_TOKENS_DEFAULT; + } + if ((requested > MAX_TOKENS_CAP)) { + return MAX_TOKENS_CAP; + } + return requested; +} + +export function anth_stop_reason(chat_finish: number): number { + if ((chat_finish === CHAT_STOP)) { + return ANTH_END_TURN; + } + if ((chat_finish === CHAT_LENGTH)) { + return ANTH_MAX_TOKENS; + } + if ((chat_finish === CHAT_TOOL_CALLS)) { + return ANTH_TOOL_USE; + } + return ANTH_UNSUPPORTED; +} + +export function resp_status(chat_finish: number): number { + if ((chat_finish === CHAT_STOP)) { + return RESP_COMPLETED; + } + if ((chat_finish === CHAT_LENGTH)) { + return RESP_INCOMPLETE; + } + if ((chat_finish === CHAT_TOOL_CALLS)) { + return RESP_COMPLETED; + } + return RESP_UNSUPPORTED; +} + +export function loopback_only(ip_packed_le: number): boolean { + return (ip_packed_le === BIND_LOOPBACK_U32); +} + +export function t27_test_the_allowlisted_model_passes_and_others_are_refused(): number { + t27_n = 0; + t27_assert(model_allowed(MODEL_NAME_LEN, MODEL_WORD0, MODEL_WORD1, MODEL_WORD2, MODEL_WORD3), 'the_allowlisted_model_passes_and_others_are_refused', 138); + t27_assert((!model_allowed(5, MODEL_WORD0, MODEL_WORD1, MODEL_WORD2, MODEL_WORD3)), 'the_allowlisted_model_passes_and_others_are_refused', 139); + t27_assert((!model_allowed(5, 1953526119, 0, 0, 0)), 'the_allowlisted_model_passes_and_others_are_refused', 141); + t27_assert((!model_allowed(12, MODEL_WORD0, MODEL_WORD1, MODEL_WORD2, MODEL_WORD3)), 'the_allowlisted_model_passes_and_others_are_refused', 143); + return t27_n; +} + +export function t27_test_anthropic_request_verdicts(): number { + t27_n = 0; + t27_assert((anth_req_verdict(true, 100, true) === 0), 'anthropic_request_verdicts', 147); + t27_assert((anth_req_verdict(false, 100, true) === 400), 'anthropic_request_verdicts', 148); + t27_assert((anth_req_verdict(true, 0, true) === 400), 'anthropic_request_verdicts', 149); + t27_assert((anth_req_verdict(true, 100, false) === 403), 'anthropic_request_verdicts', 150); + return t27_n; +} + +export function t27_test_responses_request_verdicts(): number { + t27_n = 0; + t27_assert((resp_req_verdict(true, true) === 0), 'responses_request_verdicts', 154); + t27_assert((resp_req_verdict(false, true) === 400), 'responses_request_verdicts', 155); + t27_assert((resp_req_verdict(true, false) === 403), 'responses_request_verdicts', 156); + return t27_n; +} + +export function t27_test_supported_features_are_exactly_the_five(): number { + t27_n = 0; + t27_assert(feature_supported(0), 'supported_features_are_exactly_the_five', 160); + t27_assert(feature_supported(1), 'supported_features_are_exactly_the_five', 161); + t27_assert(feature_supported(2), 'supported_features_are_exactly_the_five', 162); + t27_assert(feature_supported(3), 'supported_features_are_exactly_the_five', 163); + t27_assert(feature_supported(4), 'supported_features_are_exactly_the_five', 164); + t27_assert((!feature_supported(10)), 'supported_features_are_exactly_the_five', 165); + t27_assert((!feature_supported(11)), 'supported_features_are_exactly_the_five', 166); + t27_assert((!feature_supported(12)), 'supported_features_are_exactly_the_five', 167); + t27_assert((!feature_supported(13)), 'supported_features_are_exactly_the_five', 168); + t27_assert((!feature_supported(200)), 'supported_features_are_exactly_the_five', 169); + return t27_n; +} + +export function t27_test_effective_max_tokens_follows_the_law(): number { + t27_n = 0; + t27_assert((effective_max_tokens(0) === MAX_TOKENS_DEFAULT), 'effective_max_tokens_follows_the_law', 173); + t27_assert((effective_max_tokens(100) === 100), 'effective_max_tokens_follows_the_law', 174); + t27_assert((effective_max_tokens(100000) === MAX_TOKENS_CAP), 'effective_max_tokens_follows_the_law', 175); + return t27_n; +} + +export function t27_test_stop_vocabularies_map_exactly(): number { + t27_n = 0; + t27_assert((anth_stop_reason(CHAT_STOP) === ANTH_END_TURN), 'stop_vocabularies_map_exactly', 179); + t27_assert((anth_stop_reason(CHAT_LENGTH) === ANTH_MAX_TOKENS), 'stop_vocabularies_map_exactly', 180); + t27_assert((anth_stop_reason(CHAT_TOOL_CALLS) === ANTH_TOOL_USE), 'stop_vocabularies_map_exactly', 181); + t27_assert((anth_stop_reason(9) === ANTH_UNSUPPORTED), 'stop_vocabularies_map_exactly', 182); + t27_assert((resp_status(CHAT_STOP) === RESP_COMPLETED), 'stop_vocabularies_map_exactly', 183); + t27_assert((resp_status(CHAT_LENGTH) === RESP_INCOMPLETE), 'stop_vocabularies_map_exactly', 184); + t27_assert((resp_status(CHAT_TOOL_CALLS) === RESP_COMPLETED), 'stop_vocabularies_map_exactly', 185); + t27_assert((resp_status(9) === RESP_UNSUPPORTED), 'stop_vocabularies_map_exactly', 186); + return t27_n; +} + +export function feature_strippable(code: number): boolean { + if ((code === 10)) { + return true; + } + if ((code === 11)) { + return true; + } + if ((code === 12)) { + return true; + } + if ((code === 13)) { + return true; + } + return false; +} + +export function t27_test_cloud_only_tools_are_stripped_not_served(): number { + t27_n = 0; + t27_assert(feature_strippable(10), 'cloud_only_tools_are_stripped_not_served', 202); + t27_assert(feature_strippable(11), 'cloud_only_tools_are_stripped_not_served', 203); + t27_assert(feature_strippable(12), 'cloud_only_tools_are_stripped_not_served', 204); + t27_assert(feature_strippable(13), 'cloud_only_tools_are_stripped_not_served', 205); + t27_assert((!feature_strippable(0)), 'cloud_only_tools_are_stripped_not_served', 206); + t27_assert((!feature_strippable(3)), 'cloud_only_tools_are_stripped_not_served', 207); + t27_assert((!feature_strippable(200)), 'cloud_only_tools_are_stripped_not_served', 208); + return t27_n; +} +export const CHAT_ROLE_SYSTEM: number = 0; +export const CHAT_ROLE_USER: number = 1; +export const CHAT_ROLE_ASSISTANT: number = 2; +export const CHAT_ROLE_TOOL: number = 3; + +export function chat_role(client_role: number): number { + if ((client_role === 0)) { + return CHAT_ROLE_SYSTEM; + } + if ((client_role === 1)) { + return CHAT_ROLE_USER; + } + if ((client_role === 2)) { + return CHAT_ROLE_ASSISTANT; + } + if ((client_role === 3)) { + return CHAT_ROLE_TOOL; + } + if ((client_role === 4)) { + return CHAT_ROLE_SYSTEM; + } + return 255; +} + +export function t27_test_developer_role_maps_to_system_and_unknowns_are_refused(): number { + t27_n = 0; + t27_assert((chat_role(0) === CHAT_ROLE_SYSTEM), 'developer_role_maps_to_system_and_unknowns_are_refused', 231); + t27_assert((chat_role(1) === CHAT_ROLE_USER), 'developer_role_maps_to_system_and_unknowns_are_refused', 232); + t27_assert((chat_role(2) === CHAT_ROLE_ASSISTANT), 'developer_role_maps_to_system_and_unknowns_are_refused', 233); + t27_assert((chat_role(3) === CHAT_ROLE_TOOL), 'developer_role_maps_to_system_and_unknowns_are_refused', 234); + t27_assert((chat_role(4) === CHAT_ROLE_SYSTEM), 'developer_role_maps_to_system_and_unknowns_are_refused', 235); + t27_assert((chat_role(9) === 255), 'developer_role_maps_to_system_and_unknowns_are_refused', 236); + return t27_n; +} + +export function t27_test_the_adapter_binds_loopback_only(): number { + t27_n = 0; + t27_assert(loopback_only(BIND_LOOPBACK_U32), 'the_adapter_binds_loopback_only', 240); + t27_assert((!loopback_only(0)), 'the_adapter_binds_loopback_only', 241); + t27_assert((!loopback_only(16777344)), 'the_adapter_binds_loopback_only', 242); + return t27_n; +} + +export const t27_tests: t27_case[] = [ + ['the_allowlisted_model_passes_and_others_are_refused', t27_test_the_allowlisted_model_passes_and_others_are_refused], + ['anthropic_request_verdicts', t27_test_anthropic_request_verdicts], + ['responses_request_verdicts', t27_test_responses_request_verdicts], + ['supported_features_are_exactly_the_five', t27_test_supported_features_are_exactly_the_five], + ['effective_max_tokens_follows_the_law', t27_test_effective_max_tokens_follows_the_law], + ['stop_vocabularies_map_exactly', t27_test_stop_vocabularies_map_exactly], + ['cloud_only_tools_are_stripped_not_served', t27_test_cloud_only_tools_are_stripped_not_served], + ['developer_role_maps_to_system_and_unknowns_are_refused', t27_test_developer_role_maps_to_system_and_unknowns_are_refused], + ['the_adapter_binds_loopback_only', t27_test_the_adapter_binds_loopback_only], +]; + +export const t27_invariants: t27_case[] = [ +]; + +/// The assert table of `t27c test-report`, line for line. +export function t27_report(): string { + const lines: string[] = [' runtime asserts executed, per test (#6509; a pass with 0 is vacuous, T730):']; + let passed = 0; + let vacuous = 0; + for (const [name, run] of t27_tests) { + let n = -1; + try { n = run(); } catch (e) { n = -1; } + if (n < 0) { lines.push(' ' + '-'.padStart(6) + ' ' + name + ' (failed, not counted)'); continue; } + passed = passed + 1; + if (n === 0) { vacuous = vacuous + 1; lines.push(' ' + '0'.padStart(6) + ' ' + name + ' VACUOUS'); } else { lines.push(' ' + String(n).padStart(6) + ' ' + name); } + } + lines.push(' vacuous passes ' + String(vacuous) + ' of ' + String(passed) + ' (passed with 0 runtime asserts executed)'); + return lines.join(String.fromCharCode(10)); +} + +/// Every test and invariant that throws, with what it threw; empty when all hold. +export function t27_failures(): string[] { const out: string[] = []; for (const [name, run] of t27_tests) { try { run(); } catch (e) { out.push('test ' + name + ': ' + String(e)); } } for (const [name, run] of t27_invariants) { try { run(); } catch (e) { out.push('invariant ' + name + ': ' + String(e)); } } return out; } + diff --git a/specs/vps/ladder_protocol_map.t27 b/specs/vps/ladder_protocol_map.t27 new file mode 100644 index 0000000000..3e1bab9c55 --- /dev/null +++ b/specs/vps/ladder_protocol_map.t27 @@ -0,0 +1,243 @@ +// SPDX-License-Identifier: Apache-2.0 +; specs/vps/ladder_protocol_map.t27 -- the LAW of the VPS protocol adapter (owner verdict 2026-11-11: +; protocol logic lives in .t27, the generated artifact runs on the VPS, the loader .mjs is thin glue only). +; Every decision the adapter makes comes from this spec: +; - which model may be served (the allowlist, packed two u64 words little-endian), +; - which request shapes are valid (Anthropic Messages, OpenAI Responses), +; - how a chat finish reason maps to each protocol's stop vocabulary, +; - which features are refused explicitly, +; - the effective max_tokens law, +; - that the adapter binds loopback only. +; The generated TypeScript (t27b ts) is the executable artifact; the loader must not decide anything itself. +; -- +; Feature codes shared by both sides (the loader maps JSON fields onto these codes): +; 0 text, 1 tool_calls, 2 tool_results, 3 stream, 4 max_tokens, 10 web_search, +; 11 code_interpreter, 12 computer_use, 13 file_outputs +; Model id "glm-5.3-flash" packed little-endian: low = "glm-5.3-" (8 bytes), high = "flash". +; -- +; ASCII only (L3) +; phi^2 + 1/phi^2 = 3 | TRINITY + +module VpsLadderProtocolMap; + +/// "glm-5.3-flash" packed little-endian as four u32 words: +/// bytes 0x67,0x6c,0x6d,0x2d | 0x35,0x2e,0x33,0x2d | 0x66,0x6c,0x61,0x73 | 0x68 +pub const MODEL_WORD0 : u32 = 0x2d6d6c67; // "glm-" +pub const MODEL_WORD1 : u32 = 0x2d332e35; // "5.3-" +pub const MODEL_WORD2 : u32 = 0x73616c66; // "flas" +pub const MODEL_WORD3 : u32 = 0x00000068; // "h" +pub const MODEL_NAME_LEN : u32 = 13; + +/// 127.0.0.1 packed little-endian. +pub const BIND_LOOPBACK_U32 : u32 = 0x0100007f; + +/// Default max_tokens when a request omits it (both protocols), and its cap. +pub const MAX_TOKENS_DEFAULT : u32 = 2048; +pub const MAX_TOKENS_CAP : u32 = 8192; + +/// Stop-vocabulary codes on the chat side (what Ladder returns in finish_reason). +pub const CHAT_STOP : u8 = 0; +pub const CHAT_LENGTH : u8 = 1; +pub const CHAT_TOOL_CALLS : u8 = 2; + +/// Anthropic stop_reason codes (the loader writes these strings). +pub const ANTH_END_TURN : u8 = 0; +pub const ANTH_MAX_TOKENS : u8 = 1; +pub const ANTH_TOOL_USE : u8 = 2; +pub const ANTH_UNSUPPORTED : u8 = 255; + +/// Responses status codes. +pub const RESP_COMPLETED : u8 = 1; +pub const RESP_INCOMPLETE : u8 = 2; +pub const RESP_UNSUPPORTED : u8 = 255; + +// ---------------------------------------------------------------------------------------------------------- +// Validation. +// ---------------------------------------------------------------------------------------------------------- + +/// Matches the little-endian u32 word of the packed model id at word index idx. +pub fn word_matches(got: u32, idx: u32) -> bool { + if (idx == 0) { return got == MODEL_WORD0; } + if (idx == 1) { return got == MODEL_WORD1; } + if (idx == 2) { return got == MODEL_WORD2; } + if (idx == 3) { return got == MODEL_WORD3; } + return false; +} + +/// The whole allowlist check: length and all four words. +pub fn model_allowed(len: u32, w0: u32, w1: u32, w2: u32, w3: u32) -> bool { + if (len != MODEL_NAME_LEN) { return false; } + return word_matches(w0, 0) and word_matches(w1, 1) and word_matches(w2, 2) and word_matches(w3, 3); +} + +/// Validates an Anthropic Messages request; returns the HTTP-ish verdict: +/// 0 = valid, 400 = missing model or max_tokens, 403 = model not on the allowlist. +pub fn anth_req_verdict(has_model: bool, max_tokens: u32, name_ok: bool) -> u32 { + if (!has_model) { return 400; } + if (max_tokens == 0) { return 400; } + if (!name_ok) { return 403; } + return 0; +} + +/// Validates an OpenAI Responses request; 0 = valid, 400 = missing model, 403 = refused model. +pub fn resp_req_verdict(has_model: bool, name_ok: bool) -> u32 { + if (!has_model) { return 400; } + if (!name_ok) { return 403; } + return 0; +} + +/// A feature code is served only if it is in the supported set {0,1,2,3,4}; everything else is +/// refused explicitly (the loader answers 400 with the refusal name, never silently drops it). +pub fn feature_supported(code: u8) -> bool { + if (code == 0) { return true; } // text + if (code == 1) { return true; } // tool_calls + if (code == 2) { return true; } // tool_results + if (code == 3) { return true; } // stream + if (code == 4) { return true; } // max_tokens + return false; +} + +/// Resolves the effective max_tokens: absent/0 -> MAX_TOKENS_DEFAULT, capped at MAX_TOKENS_CAP. +pub fn effective_max_tokens(requested: u32) -> u32 { + if (requested == 0) { return MAX_TOKENS_DEFAULT; } + if (requested > MAX_TOKENS_CAP) { return MAX_TOKENS_CAP; } + return requested; +} + +// ---------------------------------------------------------------------------------------------------------- +// Mappings. +// ---------------------------------------------------------------------------------------------------------- + +/// chat finish_reason -> Anthropic stop_reason; an unmapped code is ANTH_UNSUPPORTED (the loader +/// must then answer with an explicit 502 rather than inventing a value). +pub fn anth_stop_reason(chat_finish: u8) -> u8 { + if (chat_finish == CHAT_STOP) { return ANTH_END_TURN; } + if (chat_finish == CHAT_LENGTH) { return ANTH_MAX_TOKENS; } + if (chat_finish == CHAT_TOOL_CALLS) { return ANTH_TOOL_USE; } + return ANTH_UNSUPPORTED; +} + +/// chat finish_reason -> Responses status; an unmapped code is RESP_UNSUPPORTED. +pub fn resp_status(chat_finish: u8) -> u8 { + if (chat_finish == CHAT_STOP) { return RESP_COMPLETED; } + if (chat_finish == CHAT_LENGTH) { return RESP_INCOMPLETE; } + if (chat_finish == CHAT_TOOL_CALLS) { return RESP_COMPLETED; } + return RESP_UNSUPPORTED; +} + +/// Loopback-only access rule: the only packed address the adapter may bind or trust. +pub fn loopback_only(ip_packed_le: u32) -> bool { + return ip_packed_le == BIND_LOOPBACK_U32; +} + +// ---------------------------------------------------------------------------------------------------------- +// Tests (L4). +// ---------------------------------------------------------------------------------------------------------- + +test the_allowlisted_model_passes_and_others_are_refused { + assert model_allowed(MODEL_NAME_LEN, MODEL_WORD0, MODEL_WORD1, MODEL_WORD2, MODEL_WORD3); + assert !model_allowed(5, MODEL_WORD0, MODEL_WORD1, MODEL_WORD2, MODEL_WORD3); + // "gpt-4" packed little-endian + assert !model_allowed(5, 0x74707167, 0, 0, 0); // "gptq" packed, wrong words and length + // right words, wrong length + assert !model_allowed(12, MODEL_WORD0, MODEL_WORD1, MODEL_WORD2, MODEL_WORD3); +} + +test anthropic_request_verdicts { + assert anth_req_verdict(true, 100, true) == 0; + assert anth_req_verdict(false, 100, true) == 400; + assert anth_req_verdict(true, 0, true) == 400; + assert anth_req_verdict(true, 100, false) == 403; +} + +test responses_request_verdicts { + assert resp_req_verdict(true, true) == 0; + assert resp_req_verdict(false, true) == 400; + assert resp_req_verdict(true, false) == 403; +} + +test supported_features_are_exactly_the_five { + assert feature_supported(0); + assert feature_supported(1); + assert feature_supported(2); + assert feature_supported(3); + assert feature_supported(4); + assert !feature_supported(10); // web_search + assert !feature_supported(11); // code_interpreter + assert !feature_supported(12); // computer_use + assert !feature_supported(13); // file_outputs + assert !feature_supported(200); +} + +test effective_max_tokens_follows_the_law { + assert effective_max_tokens(0) == MAX_TOKENS_DEFAULT; + assert effective_max_tokens(100) == 100; + assert effective_max_tokens(100000) == MAX_TOKENS_CAP; +} + +test stop_vocabularies_map_exactly { + assert anth_stop_reason(CHAT_STOP) == ANTH_END_TURN; + assert anth_stop_reason(CHAT_LENGTH) == ANTH_MAX_TOKENS; + assert anth_stop_reason(CHAT_TOOL_CALLS) == ANTH_TOOL_USE; + assert anth_stop_reason(9) == ANTH_UNSUPPORTED; + assert resp_status(CHAT_STOP) == RESP_COMPLETED; + assert resp_status(CHAT_LENGTH) == RESP_INCOMPLETE; + assert resp_status(CHAT_TOOL_CALLS) == RESP_COMPLETED; + assert resp_status(9) == RESP_UNSUPPORTED; +} + + +/// Feature codes the adapter may strip from a request instead of refusing it: the tool is removed +/// before Ladder sees the request, so the model can never invoke it. Codes outside +/// {supported} ∪ {strippable} are refused outright. +pub fn feature_strippable(code: u32) -> bool { + if (code == 10) { return true; } // web_search + if (code == 11) { return true; } // code_interpreter + if (code == 12) { return true; } // computer_use + if (code == 13) { return true; } // file_outputs + return false; +} + +test cloud_only_tools_are_stripped_not_served { + assert feature_strippable(10); + assert feature_strippable(11); + assert feature_strippable(12); + assert feature_strippable(13); + assert !feature_strippable(0); // core features are never strippable + assert !feature_strippable(3); + assert !feature_strippable(200); +} + + +/// Chat-side role codes the Ladder upstream accepts: 0 system, 1 user, 2 assistant, 3 tool. +/// Client roles map onto these: OpenAI Responses "developer" is a system prompt on the chat side. +pub const CHAT_ROLE_SYSTEM : u8 = 0; +pub const CHAT_ROLE_USER : u8 = 1; +pub const CHAT_ROLE_ASSISTANT : u8 = 2; +pub const CHAT_ROLE_TOOL : u8 = 3; + +/// Client role codes: 0 system, 1 user, 2 assistant, 3 tool, 4 developer. +/// Returns the chat-side role; 255 = unmapped (the loader refuses rather than passes it through). +pub fn chat_role(client_role: u8) -> u8 { + if (client_role == 0) { return CHAT_ROLE_SYSTEM; } + if (client_role == 1) { return CHAT_ROLE_USER; } + if (client_role == 2) { return CHAT_ROLE_ASSISTANT; } + if (client_role == 3) { return CHAT_ROLE_TOOL; } + if (client_role == 4) { return CHAT_ROLE_SYSTEM; } // developer -> system + return 255; +} + +test developer_role_maps_to_system_and_unknowns_are_refused { + assert chat_role(0) == CHAT_ROLE_SYSTEM; + assert chat_role(1) == CHAT_ROLE_USER; + assert chat_role(2) == CHAT_ROLE_ASSISTANT; + assert chat_role(3) == CHAT_ROLE_TOOL; + assert chat_role(4) == CHAT_ROLE_SYSTEM; + assert chat_role(9) == 255; +} + +test the_adapter_binds_loopback_only { + assert loopback_only(BIND_LOOPBACK_U32); + assert !loopback_only(0); // 0.0.0.0 + assert !loopback_only(0x0100007f + 1); +}