Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
run_scope.h File Reference

gh#158: the cancel token of the run executing on THIS thread. More...

#include <entropic/entropic_export.h>
#include <atomic>
#include <cstddef>
#include <string>
Include dependency graph for run_scope.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  entropic::RunCancelScope
 RAII publication of one run's cancel token to its own thread. More...
 
class  entropic::RunSessionScope
 RAII publication of one run's session key to its own thread. More...
 
class  entropic::RunStreamScope
 RAII: publish a streaming run's sink to its own thread (gh#208). More...
 

Namespaces

namespace  entropic
 Activate model on GPU (WARM → ACTIVE).
 

Typedefs

using entropic::RunStreamFn = void(*)(const char *chunk, size_t len, void *user_data)
 Signature of a run's stream-chunk sink (gh#208, v2.14.2).
 

Functions

ENTROPIC_EXPORT const std::atomic< bool > * entropic::current_run_cancel ()
 The cancel token of the run executing on this thread, if any.
 
ENTROPIC_EXPORT bool entropic::current_run_cancelled ()
 Whether THIS thread's run has been cancelled.
 
ENTROPIC_EXPORT bool entropic::cancel_current_run ()
 Cancel the run executing on this thread (gh#158).
 
ENTROPIC_EXPORT std::string entropic::current_run_session ()
 The session key of the run executing on this thread (gh#166).
 
ENTROPIC_EXPORT bool entropic::emit_to_current_run_stream (const char *chunk, size_t len)
 Deliver a chunk to the stream sink published on THIS thread.
 

Detailed Description

gh#158: the cancel token of the run executing on THIS thread.

Why a thread-local and not a parameter
gh#150 gave external MCP transports a cancel_flag_ that ServerManager::interrupt_external_tools() trips on EVERY transport at once. That is correct while a handle runs one turn at a time — the only in-flight tool call belongs to the only run. Once runs are keyed per session (gh#158) it stops being correct: interrupting session A would abort session B's in-flight filesystem.read too, and B would see an empty result it cannot distinguish from a legitimate one (the exact failure mode gh#150 was filed for).

The honest fix is a cancel token carried by the REQUEST. Threading one through would change ToolExecutionInterface (a C function-pointer struct on the core↔facade seam), ServerManager::call_tool, ExternalClient and Transport::send_request — four signatures, three .so boundaries, for a value that is already unambiguous from context: a run is a BLOCKING call on the caller's thread, and every tool call it makes happens on that same thread. So the token is published per thread instead of per parameter.

This is the same mechanism entropic::log::HandleLogScope already uses to route a log line to the right session without threading a handle id through every logging call, and it is deliberately modelled on it.

Ownership
A scope BORROWS the atomic; it never owns it. The engine's per-run state outlives the scope by construction (the scope is nested inside the run that owns the flag), so the pointer cannot dangle while it is installed.
Version
2.13.0

Definition in file run_scope.h.