Implementation:1ay1 Agentty Background Work Owner
| Knowledge Sources | |
|---|---|
| Domains | Concurrency, Runtime |
| Last Updated | 2026-10-28 12:00 GMT |
Overview
Owned fire-and-forget background work via a single process-lifetime pool with bounded shutdown, stop tokens, and exception isolation.
Description
The agentty::util::background module adds policy on top of jaal's `maya::pool` primitive: that agentty has ONE background pool, joined through agentty's teardown registry; and a per-job `where` label making a reported throw traceable. The pool provides: a job gets its own thread when it may never return, every body is wrapped so an exception cannot reach `std::terminate`, every job gets a `std::stop_token`, and shutdown is BOUNDED — isolated jobs are counted before their thread exists, waited for inside the grace, then abandoned safely.
This replaces the old hand-rolled `std::thread(...).detach()` which had no stop_token, no join (threads still running while CRT destroyed statics), no bound (shutdown was a race), and a misleading comment promising a reaper that didn't exist.
Usage
Import this when you need to: run fire-and-forget work that outlives the frame with no result. For work that returns to the reducer, use `Cmd::task`. For work that must finish before the frame returns, use `maya::scope`.
Code Reference
Source Location
- Repository: 1ay1_Agentty
- File: include/agentty/util/background.hpp
- Lines: 1-225
Signature
namespace agentty::util {
// Run a captureless body on the one background pool. The body takes
// (stop_token, args...) and every arg is Sendable.
template <class F, class... Args>
void run_isolated_detached(std::string_view where, F body, Args... args);
}
Import
#include "agentty/util/background.hpp"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| where | std::string_view | Yes | Label for traceable error reporting |
| body | captureless callable | Yes | body(stop_token, args...) — Sendable args |
Outputs
| Name | Type | Description |
|---|---|---|
| (none) | void | Fire-and-forget; bounded shutdown via teardown registry |
Usage Examples
Run Fire-and-Forget Work
#include "agentty/util/background.hpp"
agentty::util::run_isolated_detached("prewarm.cache", [](std::stop_token st) {
// Long-running warmup that may never return
// Exception isolation is automatic; stop_token lets shutdown request stop
while (!st.stop_requested()) {
// do work...
}
});