Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"sumneko.lua"
]
}
3 changes: 1 addition & 2 deletions backend/consensus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1868,8 +1868,7 @@ async def handle(self, context):

# Check if the leader timed out
if (
context.consensus_data.leader_receipt[0].result[0]
== ResultCode.CONTRACT_ERROR
context.consensus_data.leader_receipt[0].result[0] == ResultCode.VM_ERROR
) and (context.consensus_data.leader_receipt[0].result[1:] == b"timeout"):
return LeaderTimeoutState()

Expand Down
11 changes: 4 additions & 7 deletions backend/node/genvm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@dataclass
class ExecutionError:
message: str
kind: typing.Literal[ResultCode.CONTRACT_ERROR, ResultCode.ROLLBACK]
kind: typing.Literal[ResultCode.USER_ERROR, ResultCode.VM_ERROR]

def __repr__(self):
return json.dumps({"kind": self.kind.name, "message": self.message})
Expand Down Expand Up @@ -275,9 +275,6 @@ async def loop_enter(self) -> socket.socket:
async def get_calldata(self, /) -> bytes:
return self.calldata_bytes

async def get_code(self, addr: bytes, /) -> bytes:
return self._state_proxy.get_code(Address(addr))

def has_result(self) -> bool:
return self._result is not None

Expand All @@ -302,11 +299,11 @@ async def consume_result(
) -> None:
if type == ResultCode.RETURN:
self._result = ExecutionReturn(ret=bytes(data))
elif type == ResultCode.ROLLBACK:
Comment thread
cristiam86 marked this conversation as resolved.
Resolved
elif type == ResultCode.USER_ERROR:
self._result = ExecutionError(str(data, encoding="utf-8"), type)
elif type == ResultCode.CONTRACT_ERROR:
elif type == ResultCode.VM_ERROR:
self._result = ExecutionError(str(data, encoding="utf-8"), type)
elif type == ResultCode.ERROR:
elif type == ResultCode.INTERNAL_ERROR:
raise Exception("GenVM internal error", str(data, encoding="utf-8"))
else:
assert False, f"invalid result {type}"
Expand Down
2 changes: 1 addition & 1 deletion backend/node/genvm/origin/base_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ async def wait_all_timeout():
):
errors.append(Exception("no result provided"))
else:
await handler.consume_result(ResultCode.CONTRACT_ERROR, b"timeout")
await handler.consume_result(ResultCode.VM_ERROR, b"timeout")

result = RunHostAndProgramRes(
b"".join(stdout).decode(),
Expand Down
3 changes: 2 additions & 1 deletion backend/node/genvm/origin/host_fns.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import IntEnum
from enum import IntEnum, StrEnum
import typing


class Methods(IntEnum):
Expand Down
25 changes: 21 additions & 4 deletions backend/node/genvm/origin/result_codes.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from enum import IntEnum
from enum import IntEnum, StrEnum
import typing


class ResultCode(IntEnum):
RETURN = 0
ROLLBACK = 1
CONTRACT_ERROR = 2
ERROR = 3
USER_ERROR = 1
VM_ERROR = 2
INTERNAL_ERROR = 3


class StorageType(IntEnum):
Expand All @@ -18,3 +19,19 @@ class EntryKind(IntEnum):
MAIN = 0
SANDBOX = 1
CONSENSUS_STAGE = 2


class MemoryLimiterConsts(IntEnum):
TABLE_ENTRY = 64
FILE_MAPPING = 256


class SpecialMethod(StrEnum):
GET_SCHEMA = "#get-schema"
ERRORED_MESSAGE = "#error"


EVENT_MAX_TOPICS: typing.Final[int] = 4


ABSENT_VERSION: typing.Final[str] = "v0.1.0"
115 changes: 67 additions & 48 deletions backend/validators/greyboxing.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
local lib = require("lib-greyboxing")
local inspect = require("inspect")
local lib = require("lib-genvm")
local llm = require("lib-llm")

-- check https://github.com/genlayerlabs/genvm/blob/v0.1.2/executor/modules/implementation/scripting/llm-default.lua

-- Used to look up mock responses for testing
-- It returns the response that is linked to a substring of the message
function get_mock_response_from_table(table, message)
local function get_mock_response_from_table(table, message)
for key, value in pairs(table) do
if string.find(message, key) then
return value
Expand All @@ -13,79 +14,97 @@ function get_mock_response_from_table(table, message)
return "no match"
end

function just_in_backend(args, prompt, format)
local search_in = lib.select_backends_for(args, format)

lib.log{ args = args, prompt = prompt, format = format, search_in = search_in }

local handler = args.handler
local function just_in_backend(ctx, args, mapped_prompt)
---@cast mapped_prompt MappedPrompt
---@cast args LLMExecPromptPayload | LLMExecPromptTemplatePayload

-- Return mock response if it exists
if args.host_data and args.host_data.mock_response then
if args.payload then
if args.payload.principle then
-- Return the matching response to gl.eq_principle_prompt_comparative request which contains a principle key in the payload
result = get_mock_response_from_table(args.host_data.mock_response.eq_principle_prompt_comparative, prompt.user_message)
elseif args.payload.output then
-- Return the matching response to gl.eq_principle_prompt_non_comparative request which contains an output key in the payload
result = get_mock_response_from_table(args.host_data.mock_response.eq_principle_prompt_non_comparative, prompt.user_message)
else
-- Return the matching response to gl.exec_prompt request which does not contain any specific key in the payload
result = get_mock_response_from_table(args.host_data.mock_response.response, prompt.user_message)
end
if ctx.host_data.mock_response then
Comment thread
kp2pml30 marked this conversation as resolved.
Resolved
local result

if args.template == "EqComparative" then
-- Return the matching response to gl.eq_principle_prompt_comparative request which contains a principle key in the payload
result = get_mock_response_from_table(ctx.host_data.mock_response.eq_principle_prompt_comparative, mapped_prompt.prompt.user_message)
elseif args.template == "EqNonComparativeValidator" then
-- Return the matching response to gl.eq_principle_prompt_non_comparative request which contains an output key in the payload
result = get_mock_response_from_table(ctx.host_data.mock_response.eq_principle_prompt_non_comparative, mapped_prompt.prompt.user_message)
else
-- Return the matching response to gl.exec_prompt request which does not contain any specific key in the payload
-- EqNonComparativeLeader is essentially just exec_prompt
result = get_mock_response_from_table(ctx.host_data.mock_response.response, mapped_prompt.prompt.user_message)
end
Comment on lines +22 to 35

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Nil-guard optional mock-response tables

ctx.host_data.mock_response.<subtable> may be nil, resulting in a runtime error when pairs iterates over it. Insert a defensive check:

local sub = ctx.host_data.mock_response.eq_principle_prompt_comparative
if sub then
  result = get_mock_response_from_table(sub, mapped_prompt.prompt.user_message)
end

Replicate for the other branches.

🤖 Prompt for AI Agents
In backend/validators/greyboxing.lua around lines 22 to 35, the code accesses
subtables of ctx.host_data.mock_response without checking if they are nil, which
can cause runtime errors when iterating over them. Add a nil check before
calling get_mock_response_from_table for each subtable
(eq_principle_prompt_comparative, eq_principle_prompt_non_comparative, and
response) to ensure the subtable exists before using it.

lib.log{level = "debug", message = "executed with", type = type(result), res = result}
return result
end

local provider_id = args.host_data.studio_llm_id
local model = lib.get_first_from_table(greyboxing.available_backends[provider_id].models).key
local provider_id = ctx.host_data.studio_llm_id
local model = lib.get_first_from_table(llm.providers[provider_id].models).key

prompt.use_max_completion_tokens = false
mapped_prompt.prompt.use_max_completion_tokens = false

Comment on lines +40 to 44

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Validate provider / model resolution

ctx.host_data.studio_llm_id or the provider’s models list can be missing, leading to “attempt to index a nil value”. Fail fast with a clear error:

assert(provider_id, "studio_llm_id not set in host data")
local provider = assert(llm.providers[provider_id], "unknown provider "..provider_id)
local first_model = lib.get_first_from_table(provider.models)
assert(first_model, "no models configured for provider "..provider_id)
local model = first_model.key
🤖 Prompt for AI Agents
In backend/validators/greyboxing.lua around lines 40 to 44, the code assumes
ctx.host_data.studio_llm_id and the provider's models exist, which can cause nil
indexing errors. To fix this, add assertions to validate that provider_id is
set, the provider exists in llm.providers, and that the provider has at least
one model configured. Replace the current direct access with these assertions to
fail fast and provide clear error messages if any are missing.

for i = 1,3 do
local request = {
provider = provider_id,
model = model,
prompt = mapped_prompt.prompt,
format = mapped_prompt.format,
}

local success, result = pcall(function ()
return lib.exec_in_backend(
args.handler,
{
provider = provider_id,
model = model,
prompt = prompt,
format = format,
}
return llm.rs.exec_prompt_in_provider(
ctx,
request
)
end)

lib.log{level = "debug", message = "executed with", type = type(result), res = result}
lib.log{level = "debug", message = "executed with", success = success, type = type(result), res = result}
if success then
return result
elseif result.kind == "Overloaded" then
-- nothing/continue
lib.log{level = "warning", message = "service is overloaded", result = result}
else
lib.log{level = "error", message = "provider failed", result = result}
end
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

local as_user_error = lib.rs.as_user_error(result)
if as_user_error == nil then
error(result)
end

lib.log{level = "error", message = "sleeping before retry"}
if llm.overloaded_statuses[as_user_error.ctx.status] then
lib.log{level = "warning", message = "service is overloaded", error = as_user_error, request = request}
else
lib.log{level = "error", message = "provider failed", error = as_user_error, request = request}
as_user_error.fatal = true

lib.rs.user_error(as_user_error)
end

lib.log{level = "warning", message = "sleeping before retry"}

lib.sleep_seconds(1.5)
lib.rs.sleep_seconds(1.5)
end

lib.rs.user_error({
causes = {"NO_PROVIDER_FOR_PROMPT"},
fatal = true,
ctx = {
prompt = mapped_prompt,
host_data = ctx.host_data,
}
})
end

function exec_prompt(args)
local handler = args.handler
function ExecPrompt(ctx, args)
---@cast args LLMExecPromptPayload

local mapped = lib.exec_prompt_transform(args)
local mapped = llm.exec_prompt_transform(args)

return just_in_backend(args, mapped.prompt, mapped.format)
return just_in_backend(ctx, args, mapped)
end

function exec_prompt_template(args)
local handler = args.handler
function ExecPromptTemplate(ctx, args)
---@cast args LLMExecPromptTemplatePayload

local mapped = lib.exec_prompt_template_transform(args)
local template = args.template -- workaround by kp2pml30 (Kira) GVM-86
local mapped = llm.exec_prompt_template_transform(args)
args.template = template

return just_in_backend(args, mapped.prompt, mapped.format)
return just_in_backend(ctx, args, mapped)
end
5 changes: 3 additions & 2 deletions docker/Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM ubuntu:24.04 AS base

ARG TARGETPLATFORM

ARG GENVM_TAG=v0.1.1
ARG GENVM_TAG=v0.1.3

ARG path=/app
WORKDIR $path
Expand Down Expand Up @@ -61,7 +61,8 @@ RUN cd /genvm \
&& unzip genvm-runners.zip \
&& rm *.zip \
&& ls -R . \
&& su - backend-user -c 'python3 /genvm/scripts/runners-registry download --registry /genvm/share/lib/genvm/runners/all.json' \
&& chown -R backend-user:backend-group /genvm \
&& su - backend-user -c 'python3 /genvm/scripts/runners-registry download --registry /genvm/share/lib/genvm/runners/all.json --dest /genvm/share/lib/genvm/runners/' \
&& su - backend-user -c '/genvm/bin/genvm precompile' \
&& cd "$path" \
&& true
Expand Down
8 changes: 5 additions & 3 deletions examples/contracts/_hello_world.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# { "Depends": "py-genlayer:test" }
# v0.1.0
# { "Depends": "py-genlayer:latest" }

# Always put above line as first in the contract file
# Upon release it will be changed from `:test` to `:<hash>` and library will be frozen forever
# Always put above lines as first in the contract file
# v0.1.2 is genvm ABI version, lower versions may restrict some calls that were introduced in newer version (i.e. events)
# In actual genlayer network `:latest` is not allowed and hash must be specified

# this imports all types into globals and `genlayer.std` as `gl` (will be imported lazily on first access)
from genlayer import *
Expand Down
17 changes: 10 additions & 7 deletions examples/contracts/football_prediction_market.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# { "Depends": "py-genlayer:test" }
# v0.1.0
# { "Depends": "py-genlayer:latest" }

from genlayer import *

Expand Down Expand Up @@ -36,7 +37,7 @@ def __init__(self, game_date: str, team1: str, team2: str):
)
self.team1 = team1
self.team2 = team2
self.winner = 0
self.winner = u256(0)
self.score = ""

@gl.public.write
Expand All @@ -49,8 +50,8 @@ def resolve(self) -> typing.Any:
team1 = self.team1
team2 = self.team2

def get_match_result() -> str:
web_data = gl.get_webpage(market_resolution_url, mode="text")
def get_match_result() -> typing.Any:
web_data = gl.nondet.web.render(market_resolution_url, mode="text")
print(web_data)

task = f"""
Expand All @@ -75,11 +76,13 @@ def get_match_result() -> str:
your output must be only JSON without any formatting prefix or suffix.
This result should be perfectly parsable by a JSON parser without errors.
"""
result = gl.exec_prompt(task).replace("```json", "").replace("```", "")
result = (
gl.nondet.exec_prompt(task).replace("```json", "").replace("```", "")
)
print(result)
return json.dumps(json.loads(result), sort_keys=True)
return json.loads(result)
Comment thread
cristiam86 marked this conversation as resolved.
Resolved

result_json = json.loads(gl.eq_principle_strict_eq(get_match_result))
result_json = gl.eq_principle.strict_eq(get_match_result)

if result_json["winner"] > -1:
self.has_resolved = True
Expand Down
5 changes: 3 additions & 2 deletions examples/contracts/llm_erc20.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# { "Depends": "py-genlayer:test" }
# v0.1.0
# { "Depends": "py-genlayer:latest" }

import json

Expand Down Expand Up @@ -47,7 +48,7 @@ def transfer(self, amount: int, to_address: str) -> None:
The total sum of all balances should remain the same before and after the transaction"""

final_result = (
gl.eq_principle_prompt_non_comparative(
gl.eq_principle.prompt_non_comparative(
lambda: input,
task=task,
criteria=criteria,
Expand Down
5 changes: 3 additions & 2 deletions examples/contracts/log_indexer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# v0.1.0
# {
# "Seq": [
# { "Depends": "py-lib-genlayer-embeddings:test" },
# { "Depends": "py-genlayer:test" }
# { "Depends": "py-lib-genlayer-embeddings:09h0i209wrzh4xzq86f79c60x0ifs7xcjwl53ysrnw06i54ddxyi" },
# { "Depends": "py-genlayer:latest" }
# ]
# }

Expand Down
3 changes: 2 additions & 1 deletion examples/contracts/storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# { "Depends": "py-genlayer:test" }
# v0.1.0
# { "Depends": "py-genlayer:latest" }

from genlayer import *

Expand Down
3 changes: 2 additions & 1 deletion examples/contracts/user_storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# { "Depends": "py-genlayer:test" }
# v0.1.0
# { "Depends": "py-genlayer:latest" }

from genlayer import *

Expand Down
7 changes: 4 additions & 3 deletions examples/contracts/wizard_of_coin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# { "Depends": "py-genlayer:test" }
# v0.1.0
# { "Depends": "py-genlayer:latest" }
from genlayer import *

import json
Expand Down Expand Up @@ -39,12 +40,12 @@ def ask_for_coin(self, request: str) -> None:
"""

def get_wizard_answer():
result = gl.exec_prompt(prompt)
result = gl.nondet.exec_prompt(prompt)
result = result.replace("```json", "").replace("```", "")
print(result)
return result

result = gl.eq_principle_prompt_comparative(
result = gl.eq_principle.prompt_comparative(
get_wizard_answer, "The value of give_coin has to match"
)
parsed_result = json.loads(result)
Expand Down
Loading