Chapter 49 / 51
Volume 5 · Extensions, Ops & Projects

xwork: agent orchestration

xllm handles "talking to the model" and xllm-session handles "remembering what was said" — xwork sits on top and handles "turning the model into an agent that gets work done": tool registration and execution, workspace policy, multi-turn orchestration, notifications and compaction scheduling.

Overview

Positioning: the capstone of the AI application stack (the 15th extension). xwork v2.3.1 (42 symbols) owns orchestration, workspace policy, tool execution, artifacts and compaction scheduling; it does not own provider protocols or CLI rendering — a clean layering boundary against xllm / xllm-session.

49.1 The three-layer boundary

LayerOwnsDoes not own
xllm (ch. 34)request building, three dialects, SSE, proxiestool execution, history
xllm-session (ch. 35)context budget, compaction, journalorchestration, tools
xworkagent orchestration, tool registration/execution, workspace policy, artifacts, subagents, notifications, compaction schedulingprovider protocols, CLI

49.2 The agent lifecycle

create → register tools → run → take notifications
xwork_agent_config cfg;
xworkAgentConfigInit(&cfg);              /* workspace root, mode, budget */
xwork_agent* agent = xworkAgentCreate(&cfg);

xworkAgentRegisterBuiltinTools(agent);     /* built-in read/write/list etc. */
xworkAgentRegisterTool(agent, &myTool);   /* custom tool */

xwork_agent_run run = xworkAgentRunBegin(agent, "analyze this directory for me");
while ( xworkAgentRun(agent, &run) ) {
    xwork_task_notice notices[8];
    uint32 n = xworkAgentTakeTaskNotices(agent, notices, 8);
    for ( uint32 i = 0; i < n; i++ ) { /* tool call / result notices */ }
}
xworkAgentRunEnd(agent, &run);
xworkAgentDestroy(agent);

The 42 public symbols group by lifecycle stage: config/create/destroy 5, run control 6 (incl. cancel/resume), tool registration 10, subagent types 5, notifications & queries 8, compaction 3, others 5. Everything is exported to scripts and to nested TCC.

49.3 Workspace policy & subagents

xwork's workspace is the agent's sandbox boundary: read/write exploration (in-process or delegated to external rg/fd), read markers with directory fallback, and budget wiring. The subagent type system (xworkAgentRegisterSubagentType) lets one agent spawn read-only children (RunReadOnlySubagent) or fully privileged ones, forming a multi-agent collaboration topology.

Compaction scheduling (xworkAgentCompact / MaybeCompact) works with xllm-session's compaction operators so long conversations converge automatically, while read markers keep tool artifacts from being lost.

49.4 In practice: agent-demo

The repo's release/agent-demo/ is the full-stack xwork showcase: xs single-file mode + the NativeHarness browser front end + a C-script turn engine (an HTTP polling wire protocol), accepted end-to-end in the browser — including the approval gate (tool calls need human sign-off), diff/plan panels and the execution-trace column. It is the complete worked example of the four-library stack: xwork + xllm + xllm-session + the site VFS.

The webview extension (the 15th library, 2 probing symbols) and the class: "app" service node add a desktop shape to this stack: local HTTP + a WebView2 window, which combined with single-file deployment yields a double-click desktop agent application.

Common mistakes

Pit: thread safety inside tool callbacksTool execution may call back on a worker thread — guard shared state with xrt primitives or post it back to the agent thread (see the turn engine in agent-demo).
Pit: the budget has no effectBudgets are wired through xllm-session — confirm an xllm-session-capable variant is compiled in (choosing xwork in the build flags carries the dependency chain automatically).

Exercises

Basic
Using RegisterBuiltinTools + Run, complete one agent turn that "lists the workspace files".
Intermediate
Register a custom tool (e.g. read a database) and print every tool call and result from the notification stream.
Challenge
Simplify the 49.4 agent-demo idea into your own approval gate: pause before each tool call and wait for an HTTP approval endpoint to respond.

Summary

xwork = the agent orchestration layer (42 symbols, the 15th extension) · three layers = xllm (calls) / session (memory) / work (orchestration) · tool registration + subagents + notifications = the complete agent shape · agent-demo = the four-library stack in practice