agent-loop: root turn driven by the library kernel (phase 2)

ChatSessionHandler now runs the root turn on the agent-loop kernel
instead of run_agent_turn; sub-agents follow on the same kernel via
DelegateTool. The old loop stays for resume/recovery until phase 3.

agent-loop:
- DelegateTool + AgentCatalog/AgentProfile (full toolset override,
  per-child selector/assembler, frame-scoped get), StaticCatalog,
  FilteredToolSet; sync flow with sticky child_token; batch via the
  generic fan-out
- manager: start_loop skips the registry (children are not
  double-driving; they ride the parent's token tree); LoopParams gains
  selector/token overrides
- store: get_frame, get_call, set_call_extras; HistoryStore result text
  aligned to raw-stored semantics (projection formats)
- events: ApprovalRequired.request_id, AgentSpawned/Finished parent
  info; AskUserTool with_name + suggested_answers alias + Question.frame

skald-core (loop_adapters + handler):
- SkaldAssembler (byte-parity port of MessageBuilder's projection:
  scratchpad/summary/window, DTL Kimi/Anthropic injection, media, user
  coalescing, reasoning echo) + AgentSystemContext (prompt layers,
  substitutions, MCP list, shared folders, user profile)
- SkaldAgentCatalog (build_sub_agent_config port), SkaldHumanChannel,
  scratchpad/todos tools, execute_task sync/async alias,
  LegacyInterfaceTool, PendingLiveInput
- ApprovalGate: PendingWrite diffs via LoopEvent::Host (memory/disk
  routed like the fs-tools); SkaldWritePreviewHook for executed-write
  diffs; EventTranslator LoopEvent→ServerEvent (display meta, preview,
  FileChanged, AgentStart/Done, root-only Done/Truncated/Cancelled)
- handle_message: builds TurnParams and drives the kernel; resume of
  pending tools runs first (results belong to the previous turn);
  ChatEvent publication stays handler-side; /stop cancels the live loop
- ToolRegistry.get_tool/all_tools; def builders made pub(crate)

Full workspace suite green (179 skald-core, 34 agent-loop, adapters
incl.); two pre-existing doc-test failures fixed along the way.
This commit is contained in:
2026-07-26 12:15:53 +01:00
parent d50abbb0fa
commit 0297fe71bd
35 changed files with 3160 additions and 89 deletions
+27 -7
View File
@@ -76,6 +76,12 @@ pub struct LoopParams {
pub system: Arc<dyn SystemContextSource>,
pub tools: Arc<dyn ToolSet>,
pub model_hint: ModelHint,
/// Per-loop selector override (e.g. a sub-agent with its own strength,
/// blueprint D14). `None` = the manager's selector.
pub selector: Option<Arc<dyn crate::model::ModelSelector>>,
/// Parent-linked cancellation (DelegateTool passes `ctx.cancel.child_token()`):
/// `None` = a fresh scope. Cancellation stays sticky down the tree.
pub token: Option<CancellationToken>,
pub live_input: Option<Arc<dyn LiveInput>>,
pub extensions: Extensions,
pub meta: TurnMeta,
@@ -206,6 +212,8 @@ impl LoopManager {
system: params.system,
tools: params.tools,
model_hint: params.model_hint,
selector: None,
token: None,
live_input: params.live_input,
extensions: params.extensions,
meta: params.meta,
@@ -215,14 +223,26 @@ impl LoopManager {
// ── raw loops (DelegateTool, recovery, background runners) ──
/// Spawn a raw loop. Unlike `start_turn` this does NOT enforce the
/// one-loop-per-conversation rule and does NOT register in the live
/// registry: child loops (sub-agents, including concurrent batches) run
/// on the same conversation as their parent and are cancelled through
/// the parent's token tree (`child_token()`), not the registry.
pub async fn start_loop(&self, params: LoopParams) -> Result<TurnHandle, StartError> {
{
let registry = self.registry.lock().unwrap();
if registry.contains_key(&params.conversation) {
return Err(StartError::AlreadyRunning);
}
}
self.spawn(params)
self.spawn_detached(params)
}
fn spawn_detached(&self, params: LoopParams) -> Result<TurnHandle, StartError> {
let conv = params.conversation.clone();
let frame = params.frame;
let token = params.token.clone().unwrap_or_default();
let events = self.sink(conv.clone());
let deps = self.deps.clone();
let turn_token = token.clone();
let join = tokio::spawn(async move { crate::kernel::run(deps, params, turn_token, events).await });
Ok(TurnHandle { conversation: conv, frame, cancel: token, join })
}
fn spawn(&self, params: LoopParams) -> Result<TurnHandle, StartError> {