Skip to main content

harn_vm/agent_events/
agent.rs

1use serde::{Deserialize, Serialize};
2
3use crate::composition::{CompositionChildCall, CompositionChildResult, CompositionRunEnvelope};
4use crate::llm::receipts::ToolCallReceipt;
5use crate::orchestration::{HandoffArtifact, MutationSessionRecord};
6use crate::tool_annotations::ToolKind;
7
8use super::tool::{ToolCallErrorCategory, ToolCallStatus, ToolExecutor};
9use super::worker::{FsWatchEvent, WorkerEvent};
10
11/// Events emitted by the agent loop. Some variants map 1:1 to ACP
12/// `sessionUpdate` variants; Harn-specific lifecycle events ride on the
13/// extension stream.
14#[derive(Clone, Debug, Serialize, Deserialize)]
15#[serde(tag = "type", rename_all = "snake_case")]
16pub enum AgentEvent {
17    AgentMessageChunk {
18        session_id: String,
19        content: String,
20    },
21    AgentThoughtChunk {
22        session_id: String,
23        content: String,
24    },
25    UserMessage {
26        session_id: String,
27        message_id: String,
28        content: Vec<serde_json::Value>,
29    },
30    ToolCall {
31        session_id: String,
32        tool_call_id: String,
33        tool_name: String,
34        kind: Option<ToolKind>,
35        status: ToolCallStatus,
36        raw_input: serde_json::Value,
37        /// Set to `Some(true)` by the streaming candidate detector
38        /// (harn#692) when this event represents a tool-call shape
39        /// detected in the model's in-flight assistant text but whose
40        /// arguments have not finished parsing yet. Clients can render a
41        /// spinner / placeholder while the model writes the body. The
42        /// detector follows up with a `ToolCallUpdate { parsing: false,
43        /// .. }` carrying either `status: pending` (promoted) or
44        /// `status: failed` with `error_category: parse_aborted`.
45        /// `None` (the default) means "this is a normal post-parse tool
46        /// call, no candidate phase was active" so the on-disk shape
47        /// stays compatible with replays recorded before this field
48        /// existed.
49        #[serde(default, skip_serializing_if = "Option::is_none")]
50        parsing: Option<bool>,
51        /// Mutation-session audit context active when the tool was
52        /// dispatched (see harn#699). Hosts use it to group every tool
53        /// emission belonging to the same write-capable session.
54        #[serde(default, skip_serializing_if = "Option::is_none")]
55        audit: Option<MutationSessionRecord>,
56    },
57    ToolCallUpdate {
58        session_id: String,
59        tool_call_id: String,
60        tool_name: String,
61        status: ToolCallStatus,
62        raw_output: Option<serde_json::Value>,
63        error: Option<String>,
64        /// Wall-clock milliseconds from the parse-to-execution boundary
65        /// to the terminal `Completed`/`Failed` update. Includes the
66        /// time spent in any wrapping orchestration logic (loop checks,
67        /// post-tool hooks, microcompaction). Populated only on the
68        /// terminal update — `None` on intermediate `Pending` /
69        /// `InProgress` updates so clients can ignore the field until
70        /// it shows up.
71        #[serde(default, skip_serializing_if = "Option::is_none")]
72        duration_ms: Option<u64>,
73        /// Milliseconds spent in the actual host/builtin/MCP dispatch
74        /// call only (the inner `dispatch_tool_execution` window).
75        /// Populated only on the terminal update; `None` otherwise.
76        #[serde(default, skip_serializing_if = "Option::is_none")]
77        execution_duration_ms: Option<u64>,
78        /// Structured classification of the failure (when `status` is
79        /// `Failed`). Paired with `error` so clients can render each
80        /// category distinctly without parsing free-form strings. Always
81        /// `None` for non-Failed updates and serialized as
82        /// `errorCategory` in the ACP wire format.
83        #[serde(default, skip_serializing_if = "Option::is_none")]
84        error_category: Option<ToolCallErrorCategory>,
85        /// Where the tool actually ran. `None` only for events emitted
86        /// from sites that pre-date the dispatch decision (e.g. the
87        /// pending → in-progress transition the loop emits before the
88        /// dispatcher picks a backend).
89        #[serde(default, skip_serializing_if = "Option::is_none")]
90        executor: Option<ToolExecutor>,
91        /// Companion to `ToolCall.parsing` (harn#692). The streaming
92        /// candidate detector emits the *terminal* candidate event as a
93        /// `ToolCallUpdate` with `parsing: Some(false)` to retract the
94        /// in-flight `parsing: true` chip — either by promoting the
95        /// candidate (`status: pending`, populated `raw_output: None`,
96        /// `error: None`) or aborting it (`status: failed`,
97        /// `error_category: parse_aborted`). `None` means this update is
98        /// not part of a candidate-phase transition.
99        #[serde(default, skip_serializing_if = "Option::is_none")]
100        parsing: Option<bool>,
101        /// Best-effort partial parse of the streamed tool-call arguments.
102        /// Populated by the SSE transport on `Pending` updates as the
103        /// model streams `input_json_delta` (Anthropic) or
104        /// `tool_calls[].function.arguments` deltas (OpenAI). `None` on
105        /// terminal updates and on emissions from non-streaming paths
106        /// (#693). When the partial bytes are not yet parseable as JSON
107        /// the transport falls back to `raw_input_partial`.
108        #[serde(default, skip_serializing_if = "Option::is_none")]
109        raw_input: Option<serde_json::Value>,
110        /// Raw concatenated bytes of the streamed tool-call arguments
111        /// when a permissive parse failed (#693). Mutually exclusive
112        /// with `raw_input`: clients render whichever is present.
113        #[serde(default, skip_serializing_if = "Option::is_none")]
114        raw_input_partial: Option<String>,
115        /// Mutation-session audit context for the tool call. Carries the
116        /// same payload as on the paired `ToolCall` event so a host
117        /// processing a single update doesn't have to correlate against
118        /// the prior pending event.
119        #[serde(default, skip_serializing_if = "Option::is_none")]
120        audit: Option<MutationSessionRecord>,
121    },
122    Plan {
123        session_id: String,
124        plan: serde_json::Value,
125    },
126    ProgressReported {
127        session_id: String,
128        message: Option<String>,
129        entries: serde_json::Value,
130        replace: bool,
131        metadata: serde_json::Value,
132    },
133    /// Emitted when the compass observes a freeform edit and either
134    /// suggests a structural primitive, rewrites the tool call, or falls
135    /// back because rewrite mode could not prove equivalence.
136    CompassRoutingDecision {
137        session_id: String,
138        tool_call_id: String,
139        mode: String,
140        action: String,
141        persona: String,
142        original_tool: String,
143        routed_tool: String,
144        target_tool: String,
145        #[serde(default, skip_serializing_if = "Option::is_none")]
146        path: Option<String>,
147    },
148    /// Emitted after an agent scratchpad reorganization attempt. The
149    /// scratchpad body itself stays in session state; this event carries
150    /// status and count/error metadata so live UIs and eval harnesses can
151    /// audit whether reorganization helped or damaged the working set.
152    AgentScratchpadReorganization {
153        session_id: String,
154        iteration: usize,
155        status: String,
156        details: serde_json::Value,
157    },
158    /// A renderable, declarative artifact spec emitted by an agent. Harn
159    /// validates the payload and transports it; host surfaces own rendering
160    /// and may fall back to the plain-text representation.
161    Artifact {
162        session_id: String,
163        artifact_id: String,
164        kind: String,
165        #[serde(default, skip_serializing_if = "Option::is_none")]
166        title: Option<String>,
167        mime_type: String,
168        spec: serde_json::Value,
169        fallback: String,
170        size_bytes: u64,
171        provenance: serde_json::Value,
172        metadata: serde_json::Value,
173    },
174    /// Fires at the top of every model round-trip inside an
175    /// `agent_loop` invocation. Maps to the `iteration_start` steering
176    /// seam. Not the same as ACP's outer `prompt_turn` boundary; an
177    /// `agent_turn`/`prompt_turn` cycle contains many of these.
178    IterationStart {
179        session_id: String,
180        iteration: usize,
181        /// Configured provider for the impending LLM call. Empty when the
182        /// caller defers provider selection to the routing layer.
183        /// Surfaces here so observers can show "about to call X/Y" before
184        /// the call returns — previously this only landed in the
185        /// transcript after the response, leaving live pulse-check
186        /// consumers without a model attribution for in-flight iterations.
187        #[serde(default, skip_serializing_if = "String::is_empty")]
188        provider: String,
189        /// Configured model id. Same semantics as `provider`.
190        #[serde(default, skip_serializing_if = "String::is_empty")]
191        model: String,
192    },
193    /// Fires at the bottom of every model round-trip, after tool
194    /// dispatch (or after the dispatch was skipped). Sibling of
195    /// `IterationStart`.
196    IterationEnd {
197        session_id: String,
198        iteration: usize,
199        /// Free-form dict carrying the post-call snapshot. Stable keys
200        /// emitted by the agent loop: `tool_count`, `text`, plus the
201        /// LLM-result projection — `provider`, `model`, `response_ms`,
202        /// `input_tokens`, `output_tokens`, `thinking_chars`. Hosts that
203        /// surface latency/cost panes key off these without re-parsing
204        /// the transcript JSONL.
205        iteration_info: serde_json::Value,
206    },
207    /// Emitted when a first-class agent session is explicitly closed by
208    /// `agent_session_close`. This gives event-log consumers a typed
209    /// terminal marker even when no final model turn runs.
210    SessionClosed {
211        session_id: String,
212        reason: String,
213        status: String,
214        metadata: serde_json::Value,
215    },
216    /// Emitted when `agent_session_reanchor` swaps the primary workspace
217    /// anchor (#2218). Hosts use this to drive cross-project handoff UX.
218    /// Carries the previous and current anchors so consumers can diff
219    /// without re-fetching session state.
220    AnchorChanged {
221        session_id: String,
222        previous: Option<serde_json::Value>,
223        current: serde_json::Value,
224        carry_transcript: bool,
225        compacted: bool,
226        reason: Option<String>,
227    },
228    JudgeDecision {
229        session_id: String,
230        iteration: usize,
231        verdict: String,
232        reasoning: String,
233        next_step: Option<String>,
234        judge_duration_ms: u64,
235        #[serde(default, skip_serializing_if = "Option::is_none")]
236        trigger: Option<String>,
237    },
238    /// Per-step critique decision emitted by `agent_step_judge`.
239    /// Sibling of [`JudgeDecision`] but fired BEFORE tool dispatch on
240    /// every assistant turn (when configured), not just at completion.
241    /// `on_veto` carries the configured remediation shape
242    /// (`"replace"` or `"retain"`); `cost_usd` is best-effort from the
243    /// stdlib economics estimator and may be 0 when pricing is unknown.
244    /// `skipped` marks configured short-circuits that did not call the
245    /// judge model.
246    StepJudgeDecision {
247        session_id: String,
248        iteration: usize,
249        verdict: String,
250        reasoning: String,
251        critique: String,
252        confidence: f64,
253        judge_duration_ms: u64,
254        vetoed: bool,
255        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
256        skipped: bool,
257        #[serde(default, skip_serializing_if = "Option::is_none")]
258        reason: Option<String>,
259        /// True when this `verdict: "pass"` is the result of the step-judge
260        /// model itself erroring and `fail_open` swallowing the error — the
261        /// turn proceeded, but the adversarial-review surface was UNAVAILABLE
262        /// (not a genuine approval). Lets telemetry tell an inert reviewer
263        /// apart from a real pass. Mirrors `reason: "judge_unavailable"`.
264        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
265        judge_error: bool,
266        on_veto: String,
267        input_tokens: u64,
268        output_tokens: u64,
269        cost_usd: f64,
270        provider: String,
271        model: String,
272    },
273    /// Deterministic pre-dispatch critique emitted by the structural
274    /// validator middleware. Fires before any LLM-backed judge so hosts
275    /// can distinguish "$0 structural retry" from semantic critique.
276    StructuralValidatorDecision {
277        session_id: String,
278        iteration: usize,
279        rule: String,
280        diagnostic: String,
281        recommended_action: String,
282        vetoed: bool,
283        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
284        skipped: bool,
285        #[serde(default, skip_serializing_if = "Option::is_none")]
286        reason: Option<String>,
287        on_failure: String,
288        attempts: usize,
289        max_attempts: usize,
290    },
291    ScopeClassifierVerdict {
292        session_id: String,
293        iteration: usize,
294        label: String,
295        original_label: String,
296        confidence: f64,
297        confidence_threshold: f64,
298        evidence: String,
299        skip_main_turn: bool,
300        #[serde(default, skip_serializing_if = "Option::is_none")]
301        classifier_kind: Option<String>,
302        #[serde(default, skip_serializing_if = "Option::is_none")]
303        model: Option<String>,
304        #[serde(default, skip_serializing_if = "Option::is_none")]
305        error: Option<String>,
306    },
307    MissingToolCallVerdict {
308        session_id: String,
309        iteration: usize,
310        action: String,
311        original_action: String,
312        tool_name: String,
313        confidence: f64,
314        confidence_threshold: f64,
315        evidence: String,
316        #[serde(default, skip_serializing_if = "Option::is_none")]
317        language: Option<String>,
318        #[serde(default, skip_serializing_if = "Option::is_none")]
319        classifier_kind: Option<String>,
320        #[serde(default, skip_serializing_if = "Option::is_none")]
321        model: Option<String>,
322        #[serde(default, skip_serializing_if = "Option::is_none")]
323        error: Option<String>,
324    },
325    TypedCheckpoint {
326        session_id: String,
327        checkpoint: serde_json::Value,
328    },
329    FeedbackInjected {
330        session_id: String,
331        kind: String,
332        content: String,
333    },
334    /// Emitted when the agent loop exhausts `max_iterations` without any
335    /// explicit break condition firing. Distinct from a natural "done" or
336    /// a "stuck" nudge-exhaustion: this is strictly a budget cap.
337    BudgetExhausted {
338        session_id: String,
339        max_iterations: usize,
340        #[serde(default, skip_serializing_if = "Option::is_none")]
341        kind: Option<String>,
342        #[serde(default, skip_serializing_if = "Option::is_none")]
343        cost_usd: Option<f64>,
344        #[serde(default, skip_serializing_if = "Option::is_none")]
345        wall_clock_ms: Option<u64>,
346    },
347    /// Emitted when a loop-level budget circuit breaker trips after N
348    /// consecutive retryable failures. `paused_for_ms` is the mock-time-aware
349    /// backoff already honored before the terminal budget event.
350    BudgetCircuitBreaker {
351        session_id: String,
352        kind: String,
353        consecutive_count: usize,
354        paused_for_ms: u64,
355    },
356    /// Emitted when the loop breaks because consecutive text-only turns
357    /// hit `max_nudges`. Parity with `BudgetExhausted` / `IterationEnd` for
358    /// hosts that key off agent-terminal events.
359    LoopStuck {
360        session_id: String,
361        max_nudges: usize,
362        last_iteration: usize,
363        tail_excerpt: String,
364    },
365    /// Pipeline-authored stuck/escalation signal emitted through
366    /// `agent_emit_event("loop_stuck", payload)`. The runtime-level
367    /// `LoopStuck` variant above remains the built-in max-nudge terminal event;
368    /// this variant preserves the pipeline payload so hosts can surface richer
369    /// handoff/escalation details without inventing another wire kind.
370    LoopStuckSignal {
371        session_id: String,
372        payload: serde_json::Value,
373    },
374    /// Emitted by the reserved-budget terminal-verify guard
375    /// (`agent_emit_event("reserved_terminal_verify", payload)`). The guard
376    /// holds back a small iteration reserve the main loop cannot consume; when
377    /// the loop would otherwise terminate on a budget/stuck boundary with an
378    /// unverified source write, it spends the reserve on a final verify(+repair)
379    /// instead of ending blind on a red build. The payload's `phase` field tags
380    /// the step (`grant` / `verify_passed` / `verify_failed`) so replayers and
381    /// operators can see the guard fire and its outcome. Payload-preserving like
382    /// `LoopStuckSignal` so the guard can carry richer detail without inventing
383    /// another wire kind.
384    ReservedTerminalVerify {
385        session_id: String,
386        payload: serde_json::Value,
387    },
388    /// Emitted when the daemon idle-wait loop trips its watchdog because
389    /// every configured wake source returned `None` for N consecutive
390    /// attempts. Exists so a broken daemon doesn't hang the session
391    /// silently.
392    DaemonWatchdogTripped {
393        session_id: String,
394        attempts: usize,
395        elapsed_ms: u64,
396    },
397    /// Emitted when a skill is activated. Carries the match reason so
398    /// replayers can reconstruct *why* a given skill took effect at
399    /// this iteration.
400    SkillActivated {
401        session_id: String,
402        skill_name: String,
403        iteration: usize,
404        reason: String,
405    },
406    /// Emitted when a previously-active skill is deactivated because
407    /// the reassess phase no longer matches it.
408    SkillDeactivated {
409        session_id: String,
410        skill_name: String,
411        iteration: usize,
412    },
413    /// Emitted once per activation when the skill's `allowed_tools` filter
414    /// narrows the effective tool surface exposed to the model.
415    SkillScopeTools {
416        session_id: String,
417        skill_name: String,
418        allowed_tools: Vec<String>,
419    },
420    /// Emitted when the agent loop ratchets the model-visible tool
421    /// surface narrower after observing recent tool-call usage. Unlike
422    /// `SkillScopeTools`, this is session-local and can only remove
423    /// tools from the currently-effective surface.
424    SkillNarrow {
425        session_id: String,
426        reason: String,
427        removed_tools: Vec<String>,
428        remaining_tools: Vec<String>,
429        #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
430        policy: serde_json::Value,
431        #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
432        removed_tool_details: serde_json::Value,
433        #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
434        kept_tool_details: serde_json::Value,
435    },
436    /// Read-only stance lifecycle (std/agent/stance): `phase` is one of
437    /// `armed`, `write_access_granted`, `write_access_denied`,
438    /// `disarmed`. Arming carries the permitted tool window; the
439    /// grant/deny phases carry the escape-hatch justification and the
440    /// consent verdict so a trace viewer can explain every elevation.
441    StanceTransition {
442        session_id: String,
443        phase: String,
444        escape_tool: String,
445        #[serde(default, skip_serializing_if = "Vec::is_empty")]
446        allowed_tools: Vec<String>,
447        #[serde(default, skip_serializing_if = "String::is_empty")]
448        justification: String,
449        #[serde(default, skip_serializing_if = "String::is_empty")]
450        consent: String,
451        #[serde(default, skip_serializing_if = "String::is_empty")]
452        reason: String,
453    },
454    /// Emitted when a `tool_search` query is issued by the model. Carries
455    /// the raw query args, the configured strategy, and a `mode` tag
456    /// distinguishing the client-executed fallback (`"client"`) from
457    /// provider-native paths (`"anthropic"` / `"openai"`). Mirrors the
458    /// transcript event shape so hosts can render a search-in-progress
459    /// chip in real time — the replay path walks the transcript after
460    /// the turn, which is too late for live UX.
461    ToolSearchQuery {
462        session_id: String,
463        tool_use_id: String,
464        name: String,
465        query: serde_json::Value,
466        strategy: String,
467        mode: String,
468    },
469    /// Emitted when `tool_search` resolves — carries the list of tool
470    /// names newly promoted into the model's effective surface for the
471    /// next turn. Pair-emitted with `ToolSearchQuery` on every search.
472    ToolSearchResult {
473        session_id: String,
474        tool_use_id: String,
475        promoted: Vec<String>,
476        strategy: String,
477        mode: String,
478    },
479    TranscriptCompacted {
480        session_id: String,
481        mode: String,
482        reason: String,
483        strategy: String,
484        archived_messages: usize,
485        estimated_tokens_before: usize,
486        estimated_tokens_after: usize,
487        snapshot_asset_id: Option<String>,
488        #[serde(default, skip_serializing_if = "Option::is_none")]
489        instruction_mode: Option<String>,
490        #[serde(default, skip_serializing_if = "Option::is_none")]
491        instruction_source: Option<String>,
492        #[serde(default, skip_serializing_if = "Option::is_none")]
493        compaction_policy: Option<serde_json::Value>,
494    },
495    /// Emitted whenever `transcript_project` derives a model-visible
496    /// prefix from the immutable raw transcript. Hosts that render a
497    /// side-by-side raw/projected view subscribe to this — the typed
498    /// payload mirrors the metadata on the persisted
499    /// `transcript.projection` transcript event so clients don't have to
500    /// re-parse the transcript to sync UI state.
501    TranscriptProjected {
502        session_id: String,
503        policy: String,
504        reason: String,
505        prefix_hash: String,
506        kept_count: usize,
507        dropped_count: usize,
508        provider_safety_blocked: bool,
509        #[serde(default, skip_serializing_if = "is_zero_usize")]
510        redacted_count: usize,
511        #[serde(default, skip_serializing_if = "is_zero_usize")]
512        reclaimed_tokens: usize,
513        #[serde(default, skip_serializing_if = "Vec::is_empty")]
514        roots_consulted: Vec<String>,
515        #[serde(default, skip_serializing_if = "Vec::is_empty")]
516        redaction_pointers: Vec<serde_json::Value>,
517    },
518    /// Emitted when a pending `system_reminder` is rendered into the
519    /// next provider request. ACP clients show these in a reminder lane
520    /// instead of mixing them into assistant text chunks.
521    ReminderEmitted {
522        session_id: String,
523        reminder_id: String,
524        tags: Vec<String>,
525        body: String,
526        role_hint: String,
527        rendered_role: String,
528        source: String,
529        ttl_turns: Option<i64>,
530    },
531    Handoff {
532        session_id: String,
533        artifact_id: String,
534        handoff: Box<HandoffArtifact>,
535    },
536    FsWatch {
537        session_id: String,
538        subscription_id: String,
539        events: Vec<FsWatchEvent>,
540    },
541    /// Emitted when hostlib staged filesystem state changes for a session.
542    /// The ACP adapter maps this to the existing `progress` extension so
543    /// clients can update rollup-diff badges without waiting for a prompt
544    /// turn boundary.
545    StagedWritesPending {
546        session_id: String,
547        pending_count: usize,
548        total_bytes: u64,
549    },
550    /// Per-call outcome of `hostlib_fs_safe_text_patch`. Hosts subscribe to
551    /// this to roll up stale-base / hunk-conflict rates and average
552    /// hunks-per-patch without scraping result dicts out of pipeline logs.
553    /// Fired from both the staged-overlay and direct-disk code paths so
554    /// the rollup is comprehensive.
555    SafeTextPatchResult {
556        session_id: String,
557        path: String,
558        result: String,
559        hunks_count: usize,
560        bytes_written: u64,
561        #[serde(default, skip_serializing_if = "Option::is_none")]
562        failed_hunk_index: Option<usize>,
563    },
564    /// ACP control-plane arbitration outcome. Emitted for accepted,
565    /// idempotent, and rejected controls so replay/audit consumers can show
566    /// who acted and why a late or unauthorized action lost.
567    ControlOutcome {
568        session_id: String,
569        control_id: String,
570        method: String,
571        outcome: String,
572        status: String,
573        actor: serde_json::Value,
574        target: serde_json::Value,
575        #[serde(default, skip_serializing_if = "Option::is_none")]
576        reason: Option<String>,
577        #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
578        metadata: serde_json::Value,
579    },
580    /// Lifecycle update for a delegated/background worker. Carries the
581    /// canonical typed `event` variant alongside the worker's current
582    /// `status` string and the structured `metadata` payload that
583    /// `worker_bridge_metadata` builds (task, mode, timing, child
584    /// run/snapshot paths, audit-session, etc.). The `audit` field is
585    /// the same `MutationSessionRecord` JSON serialization carried on
586    /// the bridge wire so ACP/A2A consumers don't need to re-derive it.
587    ///
588    /// One-to-one with the bridge-side `worker_update` session-update
589    /// notification: ACP and A2A adapters subscribe to this variant
590    /// and translate it into their respective wire formats. The
591    /// `session_id` is the parent agent session that owns the worker
592    /// (i.e. the session whose VM spawned the worker), so a single
593    /// host stays subscribed to the same sink for both message and
594    /// worker traffic.
595    WorkerUpdate {
596        session_id: String,
597        worker_id: String,
598        worker_name: String,
599        worker_task: String,
600        worker_mode: String,
601        event: WorkerEvent,
602        status: String,
603        metadata: serde_json::Value,
604        audit: Option<serde_json::Value>,
605    },
606    /// A human-in-the-loop primitive (`ask_user`, `request_approval`,
607    /// `dual_control`, `escalate`) has just suspended the script and is
608    /// waiting on a response. Hosts that bridge the VM onto a remote
609    /// transport (ACP, A2A) translate this into a "paused / awaiting
610    /// input" wire signal so the client knows the task isn't stuck —
611    /// it's blocked on the human side. Pair-emitted with `HitlResolved`
612    /// when the waitpoint completes/cancels/times out.
613    HitlRequested {
614        session_id: String,
615        request_id: String,
616        kind: String,
617        payload: serde_json::Value,
618    },
619    /// Companion to `HitlRequested`: the waitpoint has resolved (either
620    /// a response arrived, the deadline elapsed, or the request was
621    /// cancelled). `outcome` is one of `"answered"`, `"timeout"`,
622    /// `"cancelled"`. Hosts use this to flip task state back to
623    /// `working` after an `input-required` pause.
624    HitlResolved {
625        session_id: String,
626        request_id: String,
627        kind: String,
628        outcome: String,
629    },
630    /// Emitted by the agent loop's adaptive iteration budget /
631    /// `loop_control` policy when a budget extension or early stop fires.
632    /// Generic enough to cover both shapes — `action` distinguishes them.
633    /// Carries the iteration the decision applied to, the previous /
634    /// resulting iteration limit, the policy reason string, and (for
635    /// stops) the loop status.
636    LoopControlDecision {
637        session_id: String,
638        iteration: usize,
639        action: String,
640        old_limit: usize,
641        new_limit: usize,
642        reason: String,
643        status: String,
644    },
645    /// Emitted when `agent_loop` detects adjacent repeated tool calls with
646    /// identical arguments. The warning payload avoids raw arguments by
647    /// default and carries digests so hosts can correlate repeats without
648    /// exposing potentially sensitive tool inputs.
649    AgentLoopStallWarning {
650        session_id: String,
651        warning: serde_json::Value,
652    },
653    /// Emitted when a concrete provider/model pair lacks a catalog
654    /// recommendation for a capability and the runtime chooses a fallback.
655    CapabilityGap {
656        session_id: String,
657        level: String,
658        capability: String,
659        provider: String,
660        model: String,
661        fallback_tool_format: String,
662        #[serde(default, skip_serializing_if = "Option::is_none")]
663        requested_tool_format: Option<String>,
664        message: String,
665    },
666    /// Emitted when a caller explicitly forces a tool format that
667    /// differs from the capability catalog's recommendation or known
668    /// native/text parity guidance.
669    ToolFormatOverride {
670        session_id: String,
671        provider: String,
672        model: String,
673        requested_format: String,
674        recommended_format: String,
675        catalog_parity: String,
676        #[serde(default, skip_serializing_if = "Option::is_none")]
677        override_reason: Option<String>,
678    },
679    /// Emitted when a `tool_caller` middleware (see std/llm/tool_middleware)
680    /// attaches structured audit metadata to a tool call — typically a
681    /// user-facing `summary`, a `description`, an ACP-style `kind`, an MCP
682    /// `hints` block, a `consent` decision, the per-layer `layers` log, or
683    /// free-form `metadata` keys (A2A-style extension slot).
684    ///
685    /// One-to-one with the underlying tool-call: hosts can join on
686    /// `tool_call_id` to render middleware-attached chips alongside the
687    /// existing `ToolCall` / `ToolCallUpdate` stream. The `audit` payload
688    /// is intentionally free-form JSON so middleware can carry whatever
689    /// shape the harness author chooses without needing protocol-level
690    /// changes per new middleware. When present, `receipt` carries the
691    /// stable typed, privacy-preserving record hosts can persist or mirror.
692    ToolCallAudit {
693        session_id: String,
694        tool_call_id: String,
695        tool_name: String,
696        audit: serde_json::Value,
697        #[serde(default, skip_serializing_if = "Option::is_none")]
698        receipt: Option<ToolCallReceipt>,
699    },
700    /// Emitted by `std/cache::with_cache` (both the generic and LLM
701    /// forms) when a cached lookup returns a hit. Carries the
702    /// content-addressed key, the backend that served the value, and a
703    /// `metrics` block with the cost-moat receipts the persona value
704    /// ledger (a cloud platform) and crystallization receipts read:
705    /// `model_calls_avoided`, plus `tokens_saved` / `latency_saved_ms`
706    /// when the cached envelope carried `usage` / `latency_ms`.
707    CacheHit {
708        session_id: String,
709        key: String,
710        backend: String,
711        namespace: String,
712        payload: serde_json::Value,
713    },
714    /// Paired with `CacheHit`. Emitted on the miss path when the
715    /// fresh result is stored. `payload.metrics.compute_ms` carries
716    /// the wall-clock cost of the underlying computation, which
717    /// callers can feed back as `estimate.latency_saved_ms` on the
718    /// next hit.
719    CacheMiss {
720        session_id: String,
721        key: String,
722        backend: String,
723        namespace: String,
724        payload: serde_json::Value,
725    },
726    /// A language-neutral tool-composition snippet has started. The envelope
727    /// identifies the snippet and binding manifest hashes plus the side-effect
728    /// ceiling requested for the whole parent run.
729    CompositionStart {
730        session_id: String,
731        run: CompositionRunEnvelope,
732    },
733    /// A composition snippet is dispatching a child binding call. The child
734    /// remains visible as its own operation with annotations and policy context
735    /// instead of being hidden inside the parent composition blob.
736    CompositionChildCall {
737        session_id: String,
738        call: CompositionChildCall,
739    },
740    /// A child binding operation emitted a status/result update.
741    CompositionChildResult {
742        session_id: String,
743        result: CompositionChildResult,
744    },
745    /// A composition run finished successfully and carries stdout/stderr,
746    /// artifacts, and the structured result in the terminal envelope.
747    CompositionFinish {
748        session_id: String,
749        run: CompositionRunEnvelope,
750    },
751    /// A composition run failed before producing a successful terminal result.
752    /// The terminal envelope carries the failure category and optional error.
753    CompositionError {
754        session_id: String,
755        run: CompositionRunEnvelope,
756    },
757    /// Emitted once per `__agent_loop_checkpoint(...)` pass. The single
758    /// named seam through which the agent loop drains queued bridge
759    /// injections and inbox feedback. Hosts use it to debug "did the
760    /// loop check for steering at the expected boundary" without having
761    /// to grep the loop body for inline drain calls.
762    ///
763    /// `kind` is one of the documented seam names: `iteration_start`,
764    /// `pre_tool_dispatch`, `post_tool_dispatch`, `iteration_end`,
765    /// `pre_compact`, `post_compact`, `daemon_idle_pre`,
766    /// `daemon_idle_post`, `loop_exit`. `delivered` is the count of
767    /// bridge injections drained at this seam (inbox drains are
768    /// reported separately under `inbox_delivered`). `dispatch_skipped`
769    /// is true only when an `interrupt_immediate` injection arrived at
770    /// `pre_tool_dispatch` and the pending tool batch was skipped.
771    LoopCheckpoint {
772        session_id: String,
773        iteration: usize,
774        kind: String,
775        delivered: usize,
776        #[serde(default, skip_serializing_if = "is_zero_usize")]
777        inbox_delivered: usize,
778        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
779        dispatch_skipped: bool,
780    },
781    /// Surfaced when Harn is acting as an MCP **client** and a peer
782    /// server sends a server-to-client message during an agent session:
783    /// a `notifications/progress` / `notifications/message` /
784    /// `notifications/*/list_changed` notification, or an inbound
785    /// `elicitation/create` / `sampling/createMessage` request.
786    ///
787    /// Emitted alongside (not in place of) the existing agent-inbox
788    /// relay so a thin ACP client can render a live progress bar, log
789    /// line, elicitation prompt, or sampling affordance without parsing
790    /// the inbox transcript. `direction` is `"notification"` for
791    /// fire-and-forget server notifications and `"request"` for inbound
792    /// requests that still resolve through the existing client-role
793    /// dispatch path (this event does not change that response). `method`
794    /// is the raw MCP JSON-RPC method; `params` is its untouched payload.
795    McpNotification {
796        session_id: String,
797        server: String,
798        method: String,
799        direction: String,
800        params: serde_json::Value,
801    },
802    /// Surfaced when the effective MCP catalog changes — either because a
803    /// server emitted a `notifications/tools/list_changed` (or the
804    /// resource/prompt equivalents), or because the persisted enable/disable
805    /// allowlist was edited. A thin ACP client (an IDE host's TUI / GUI)
806    /// treats this as a cue to re-fetch the catalog (e.g. via the
807    /// `mcp/catalog` request) and re-render its toggle UI, rather than
808    /// reconciling any local state. `server` is the server whose list
809    /// changed, or `None` when the change is allowlist-wide. `reason` is a
810    /// short tag (`"list_changed"` or `"allowlist_updated"`).
811    McpCatalogChanged {
812        session_id: String,
813        #[serde(default, skip_serializing_if = "Option::is_none")]
814        server: Option<String>,
815        reason: String,
816    },
817    /// Surfaced when an MCP server harn is acting as a client for answers a
818    /// request with `401 Unauthorized` mid-session, meaning its OAuth token is
819    /// missing or expired. This is a cue for a thin ACP client (an IDE host's
820    /// TUI / GUI) to start an authorization: call `mcp/authorize` to mint a
821    /// browser URL, open it, and forward the redirect's `code`+`state` back via
822    /// `mcp/oauth_callback`. Token exchange and storage stay in harn. `server`
823    /// is the configured server name; `resource` is its canonical RFC 8707
824    /// resource indicator; `scope` is the `scope` parameter from the
825    /// `WWW-Authenticate` challenge, when present.
826    McpAuthRequired {
827        session_id: String,
828        server: String,
829        resource: String,
830        #[serde(default, skip_serializing_if = "Option::is_none")]
831        scope: Option<String>,
832    },
833}
834
835fn is_zero_usize(value: &usize) -> bool {
836    *value == 0
837}
838
839impl AgentEvent {
840    pub fn session_id(&self) -> &str {
841        match self {
842            Self::AgentMessageChunk { session_id, .. }
843            | Self::AgentThoughtChunk { session_id, .. }
844            | Self::UserMessage { session_id, .. }
845            | Self::ToolCall { session_id, .. }
846            | Self::ToolCallUpdate { session_id, .. }
847            | Self::Plan { session_id, .. }
848            | Self::ProgressReported { session_id, .. }
849            | Self::CompassRoutingDecision { session_id, .. }
850            | Self::AgentScratchpadReorganization { session_id, .. }
851            | Self::Artifact { session_id, .. }
852            | Self::IterationStart { session_id, .. }
853            | Self::IterationEnd { session_id, .. }
854            | Self::SessionClosed { session_id, .. }
855            | Self::AnchorChanged { session_id, .. }
856            | Self::JudgeDecision { session_id, .. }
857            | Self::StepJudgeDecision { session_id, .. }
858            | Self::StructuralValidatorDecision { session_id, .. }
859            | Self::ScopeClassifierVerdict { session_id, .. }
860            | Self::MissingToolCallVerdict { session_id, .. }
861            | Self::TypedCheckpoint { session_id, .. }
862            | Self::FeedbackInjected { session_id, .. }
863            | Self::BudgetExhausted { session_id, .. }
864            | Self::BudgetCircuitBreaker { session_id, .. }
865            | Self::LoopStuck { session_id, .. }
866            | Self::LoopStuckSignal { session_id, .. }
867            | Self::ReservedTerminalVerify { session_id, .. }
868            | Self::DaemonWatchdogTripped { session_id, .. }
869            | Self::SkillActivated { session_id, .. }
870            | Self::SkillDeactivated { session_id, .. }
871            | Self::SkillScopeTools { session_id, .. }
872            | Self::SkillNarrow { session_id, .. }
873            | Self::StanceTransition { session_id, .. }
874            | Self::ToolSearchQuery { session_id, .. }
875            | Self::ToolSearchResult { session_id, .. }
876            | Self::TranscriptCompacted { session_id, .. }
877            | Self::TranscriptProjected { session_id, .. }
878            | Self::ReminderEmitted { session_id, .. }
879            | Self::Handoff { session_id, .. }
880            | Self::FsWatch { session_id, .. }
881            | Self::StagedWritesPending { session_id, .. }
882            | Self::SafeTextPatchResult { session_id, .. }
883            | Self::ControlOutcome { session_id, .. }
884            | Self::WorkerUpdate { session_id, .. }
885            | Self::HitlRequested { session_id, .. }
886            | Self::HitlResolved { session_id, .. }
887            | Self::LoopControlDecision { session_id, .. }
888            | Self::AgentLoopStallWarning { session_id, .. }
889            | Self::CapabilityGap { session_id, .. }
890            | Self::ToolFormatOverride { session_id, .. }
891            | Self::ToolCallAudit { session_id, .. }
892            | Self::CacheHit { session_id, .. }
893            | Self::CacheMiss { session_id, .. }
894            | Self::CompositionStart { session_id, .. }
895            | Self::CompositionChildCall { session_id, .. }
896            | Self::CompositionChildResult { session_id, .. }
897            | Self::CompositionFinish { session_id, .. }
898            | Self::CompositionError { session_id, .. }
899            | Self::LoopCheckpoint { session_id, .. }
900            | Self::McpNotification { session_id, .. }
901            | Self::McpCatalogChanged { session_id, .. }
902            | Self::McpAuthRequired { session_id, .. } => session_id,
903        }
904    }
905}