Datasets:
Download klein_code_bench/protocol.py from klein-ml/klein-code-bench: direct link, hf CLI and curl.
- Browser
- Download file 7.19 kB
-
https://huggingface.co/datasets/klein-ml/klein-code-bench/resolve/main/klein_code_bench/protocol.py
- Command line
-
hf download hf://datasets/klein-ml/klein-code-bench/klein_code_bench/protocol.py
-
curl -L -o protocol.py https://huggingface.co/datasets/klein-ml/klein-code-bench/resolve/main/klein_code_bench/protocol.py
7.19 kB
| """Versioned task contracts. These contracts are included verbatim in each prompt.""" | |
| TIERS = {"easy": "2–8B", "intermediate": "8–16B", "hard": "16–32B", "max": "60–200B"} | |
| PROTOCOL_VERSION = "1.0" | |
| BASE = """Build a complete project from an EMPTY directory, using Python 3.11+ and only the standard library. Create main.py as the entry point; any number of supporting files is allowed. No starter implementation is provided. | |
| Run: python main.py (max-tier invocation adds --state-dir DIRECTORY). | |
| Read one JSON object per stdin line, write exactly one JSON response per request line, flush it immediately, and continue after an error. No banners or diagnostics on stdout. EOF exits cleanly. Values are finite JSON; object key order is irrelevant, array order matters. Float results use tolerance 1e-9 (absolute or relative). Do not read any benchmark/evaluator files. | |
| Success response: {"ok":true,"result":RESULT}. Error response: {"ok":false,"error":CODE} with no extra fields. | |
| Every tier supports {"op":"solve","input":OBJECT}. Compute the domain result specified below without changing workspace state. input must be an object containing all required domain fields; otherwise error invalid_input. Other domain constraints are guarantees about supplied inputs, not additional validation requirements. | |
| Malformed JSON, a nonobject request, a missing/nonstring op, missing/mistyped command fields, or an unavailable/unknown operation: invalid_request. An empty or whitespace-only line is malformed JSON and receives invalid_request. Ignore extra command fields except the max-tier token field. Domain input fields beyond required ones are ignored. | |
| Keys are arbitrary nonempty strings (Unicode and / allowed), never filesystem paths. Each fresh evaluation scenario starts empty. An error changes no state. Commands are sequential, one client; concurrency is not required. | |
| """ | |
| STORE = """ | |
| Also implement an in-memory document workspace, initially empty, shared across request lines: | |
| * {"op":"put","key":K,"value":V}: store any JSON value (including null), replacing if present. Each successful put increments global revision by 1, even if unchanged. Result {"key":K,"revision":NEW_GLOBAL_REVISION}. | |
| * {"op":"get","key":K}: result {"key":K,"value":STORED_VALUE,"revision":REVISION_OF_LAST_PUT_FOR_K}. Missing key: not_found. | |
| * {"op":"delete","key":K}: delete existing key and increment global revision by 1. Result {"key":K,"revision":NEW_GLOBAL_REVISION}. Missing key: not_found, no increment. | |
| * {"op":"list","prefix":P}: result sorted list of keys starting with P; prefix optional, defaults "", must be a string. | |
| * {"op":"evaluate","key":K}: solve the stored document as domain input without modifying it. Missing key: not_found; nonobject or missing required domain fields: invalid_input. | |
| * {"op":"status"}: result {"revision":GLOBAL_REVISION,"size":NUMBER_OF_KEYS}. Reads never increment revision. Global revision begins at 0. Deep JSON values must not alias or be mutated by computation. | |
| """ | |
| TRANSACTIONS = """ | |
| Also implement atomicity and nested transactions: | |
| * put/delete may include if_revision (nonnegative integer, never boolean): require equality with current GLOBAL revision before doing anything else; mismatch -> conflict, even if the key is absent. Missing if_revision means unconditional. Other commands ignore if_revision. | |
| * {"op":"batch","commands":[REQUEST,...]}: 0..32 commands from solve/put/get/delete/list/evaluate/status only. Execute sequentially against evolving state. All succeed: result is the list of each command's unwrapped result. Any error: return that error and roll back the entire batch, including revision. Empty batch returns []. Disallowed commands, nonobject entries or invalid command list -> invalid_request and rollback. | |
| * {"op":"begin"}: save complete current state, push transaction, result {"depth":NEW_DEPTH}; at most 8 open transactions, otherwise transaction_limit. | |
| * {"op":"commit"}: discard most recent saved state, keep its changes; result {"depth":NEW_DEPTH}. | |
| * {"op":"rollback"}: restore most recent saved state including global and document revisions; result {"depth":NEW_DEPTH}. | |
| commit/rollback with no open transaction -> no_transaction. A failing command inside a transaction leaves that transaction open and unchanged. Nested commit changes remain subject to outer rollback. status still has exactly revision and size, not transaction depth. | |
| """ | |
| DURABILITY = """ | |
| Also implement durable storage and retry handling. Invocation is python main.py --state-dir DIRECTORY (existing empty or previously used directory). Choose your own on-disk format. | |
| After each successful state-changing command outside a transaction, and after an outermost commit, persist the entire logical state BEFORE flushing its response. On process termination and restart with the same directory, restore documents, revisions, history, and retry records. An open transaction and all changes since its outermost begin are discarded on restart, even if nested commits occurred. No writes on an error. Evaluation may force-kill the process immediately after reading a complete response. Single writer; torn writes during persistence, external disk corruption, and simultaneous processes are out of scope. Persist nothing outside state-dir. Never derive storage filenames from document keys. | |
| * {"op":"history","after":R,"limit":N}: result earliest up to N mutation records with revision>R, ascending. R optional default 0 (nonnegative integer); N optional default 100 (integer 0..100). Every successful put creates {"revision":R,"op":"put","key":K,"value":V}; delete creates {"revision":R,"op":"delete","key":K}. Batch records each internal write, not a batch event. Reads/transaction-control commands create no history. Rollback removes rolled-back records. History sees active transaction changes. | |
| * {"op":"scan","prefix":P,"after":K,"limit":N}: sorted matching document records as in get, keys strictly greater than after. prefix defaults "", after defaults "" (both strings), limit defaults 100 (integer 0..100). Return {"items":[...],"next":LAST_RETURNED_KEY_OR_NULL}. next is nonnull only when more matching records remain AND at least one item was returned. limit=0 returns {items:[],next:null}. | |
| * put/delete/batch may carry token, a nonempty string. First successful token-bearing request caches its full response and canonical parsed request excluding token (object order ignored, array order significant). An identical retry returns that exact cached response with NO mutation or revision increment. Reusing a token for a different request -> idempotency_conflict. Failed requests are never cached. Check a cached token before checking if_revision. Invalid/empty/nonstring token or token on any other operation -> invalid_request. A token within any batch subcommand is invalid_request. Retry records follow transaction rollback and restart rules exactly like documents; an outer rollback forgets tokens first used inside it. | |
| """ | |
| def contract(tier): | |
| index = list(TIERS).index(tier) | |
| return ( | |
| BASE | |
| + (STORE if index >= 1 else "") | |
| + (TRANSACTIONS if index >= 2 else "") | |
| + (DURABILITY if index >= 3 else "") | |
| ) | |