Parent
#27
What to build
Add sync convenience methods that allow sync handlers (running in asyncio.to_thread()) to make outgoing KI calls without understanding event loop mechanics.
Bridge methods: Add ask_sync() and post_sync() methods to KnowledgeBase. They accept the same parameters as ask() and post() respectively. Internally, they use asyncio.run_coroutine_threadsafe() to schedule the async ask()/post() coroutine on the event loop stored during start_handling_loop(), then call .result() to block the current thread until the result is ready.
Guard: If no event loop reference is stored (i.e. the handling loop is not running), raise a clear error explaining that ask_sync()/post_sync() are only available from within a sync handler running inside the handling loop.
Use case: A sync handler registered with @kb.react_ki(...) needs to query the KE network before responding:
@kb.react_ki(name="...", argument_graph_pattern="...", result_graph_pattern="...")
def my_handler(binding_set, info):
# This runs in a thread pool — cannot await
results = kb.ask_sync([{}], ki_name="my-ask-ki")
return process(results)
Acceptance criteria
Blocked by
Parent
#27
What to build
Add sync convenience methods that allow sync handlers (running in
asyncio.to_thread()) to make outgoing KI calls without understanding event loop mechanics.Bridge methods: Add
ask_sync()andpost_sync()methods toKnowledgeBase. They accept the same parameters asask()andpost()respectively. Internally, they useasyncio.run_coroutine_threadsafe()to schedule the asyncask()/post()coroutine on the event loop stored duringstart_handling_loop(), then call.result()to block the current thread until the result is ready.Guard: If no event loop reference is stored (i.e. the handling loop is not running), raise a clear error explaining that
ask_sync()/post_sync()are only available from within a sync handler running inside the handling loop.Use case: A sync handler registered with
@kb.react_ki(...)needs to query the KE network before responding:Acceptance criteria
ask_sync()method added toKnowledgeBasepost_sync()method added toKnowledgeBaseasyncio.run_coroutine_threadsafe()on the stored event loopask_sync()during handling loop and receives correct resultpost_sync()during handling loop and receives correct resultask_sync()outside handling loop raises descriptive erroruv run ruff check .passesBlocked by