# Diagnostics

`diagnostics()` reports how each Invocation (one Agent run) ends, and optionally its memory and CPU use.

- It always reports one terminal event at the end.
- With `resources`, it samples process, host, or service resources through a runtime inspector.
- It adds no model tool and wraps no Server Primitive.
- It sends structured events to your reporter, for JSON logs, metrics, or another sink.

## Configure diagnostics

The Node adapter watches process, host, and Linux cgroup resources:

```ts [server/agents/worker.ts]
import { defineAgent } from 'vite-hub/agent'
import { diagnostics, nodeRuntimeResources } from 'vite-hub/agent/capabilities'

export default defineAgent({
  name: 'worker',
  capabilities: [
    diagnostics({
      resources: nodeRuntimeResources(),
    }),
  ],
  driver: { model: 'openai/gpt-5.1-mini' },
})
```

The default reporter logs to the console. Pass `reporter` for your own sink:

```ts [server/agents/worker.ts]
import { defineAgent } from 'vite-hub/agent'
import { diagnostics } from 'vite-hub/agent/capabilities'
import { nodeRuntimeResources } from 'vite-hub/agent/capabilities'

export default defineAgent({
  name: 'worker',
  capabilities: [
    diagnostics({
      resources: nodeRuntimeResources(),
      // Send each event to your own log store
      reporter: async (event) => {
        await fetch('https://logs.example.com/ingest', { method: 'POST', body: JSON.stringify(event) })
      },
    }),
  ],
  driver: { model: 'openai/gpt-5.1-mini' },
})
```

## How diagnostics works

With `resources`, a monitor runs from preparation until the run closes. At the end, it reports `agent.invocation.terminal`.

Reporter or inspector failures never break the run or replace a successful result:

- A failed or timed-out inspector sends `agent.resource.inspect.failed` to the reporter.
- A failed reporter writes a local `agent.diagnostics.report.failed` warning.

### Event contract

| Event | When |
| --- | --- |
| `agent.invocation.terminal` | The run completes, fails, or is cancelled. Has `outcome` (`completed`, `failed`, or `cancelled`), duration, run ID when present, and a bounded structured error. |
| `agent.resource.snapshot` | Sampling starts, finishes, or reaches the heartbeat interval. |
| `agent.resource.peak` | A peak observation grows by at least `peakStepBytes`. |
| `agent.resource.inspect.failed` | The inspector fails or exceeds its timeout. |

Each observation has a `scope`, `source`, `unit`, and numeric `value`. Node adapter scopes are `process` (Node memory and CPU), `host` (available host memory), and `service` (Linux cgroup v2).

A service observation carries the run ID but does not isolate one run when several share the service.

Unsupported sources go in `support`. Unlimited cgroup values are left out, not reported as zero, so values stay correct on small machines and non-Linux hosts without parsing `/proc`.

### Sampling behavior

Sampling is bounded, so a slow inspector or reporter cannot queue polls without limit:

- One inspection runs at a time; pending reasons merge.
- Events arrive in order. `timeout` limits each delivery.
- The finish sample replaces an old poll, and closing waits for the final observation.

`diagnostics()` records operator health and resource pressure; `otlp()` exports the trace. They stay separate so a broken telemetry receiver cannot hide its own delivery failure.

## Requirements

- `diagnostics()` needs no Server Primitive.
- Sampling needs a `RuntimeResourceInspector`, such as `nodeRuntimeResources()` from `vite-hub/agent/capabilities`.
- `interval` must not exceed `heartbeat`. Durations are positive milliseconds.

## Security and approval

There is no tool, so no approval step. Events have outcome, timing, run IDs, and resource values, never prompts or model output.

> [!WARNING]
> A failed run's terminal event includes a normalized error with its stack trace. Send events only to a sink allowed to store server error details.

## Driver support

Model-backed, provider-backed, and custom-run-backed Drivers all report terminal events and resource samples.

## Verify diagnostics

Run once with the default reporter and find an `agent.invocation.terminal` object with `outcome: 'completed'` in the log. With `resources`, `agent.resource.snapshot` events appear at start and finish.

## Options

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `reporter` | `RuntimeDiagnosticReporter` | Structured console output | Receives operational events. |
| `resources` | `RuntimeResourceInspector` | None | Enables scoped resource sampling. |
| `interval` | `number` | `10000` | Polling interval in milliseconds. Not above `heartbeat`. |
| `heartbeat` | `number` | `60000` | Maximum milliseconds between snapshots. |
| `peakStepBytes` | `number` | `67108864` | Minimum peak increase before a peak event. |
| `timeout` | `number` | `1000` | Maximum milliseconds per inspection or delivery. |

## Related pages

- [OTLP](https://vitehub.dev/docs/agents/capabilities/otlp)
- [Invocations](https://vitehub.dev/docs/agents/invocations)
- [Runtime context](https://vitehub.dev/docs/reference/runtime-context)
- [Official capabilities](https://vitehub.dev/docs/agents/capabilities/official)

<!-- vitehub-docs:section-links -->
## In this section

- [Start](https://vitehub.dev/raw/docs/agents.md)
- [Define and run](https://vitehub.dev/raw/docs/agents/agent-definitions.md)
- [Instructions and tools](https://vitehub.dev/raw/docs/agents/instructions.md)
- [History and context](https://vitehub.dev/raw/docs/agents/chat-history-sessions.md)
- [Channels](https://vitehub.dev/raw/docs/agents/channels.md)
- [Inspect and evaluate](https://vitehub.dev/raw/docs/agents/observability.md)
- [Advanced](https://vitehub.dev/raw/docs/agents/agent-presets.md)
- [Recipes](https://vitehub.dev/raw/docs/agents/support-agent.md)
- [Presets](https://vitehub.dev/raw/docs/agents/presets.md)
- Capabilities
  - [Access](https://vitehub.dev/raw/docs/agents/capabilities/access.md)
  - [Chat](https://vitehub.dev/raw/docs/agents/capabilities/chat.md)
  - [Input commands](https://vitehub.dev/raw/docs/agents/capabilities/input-commands.md)
  - [Skills](https://vitehub.dev/raw/docs/agents/capabilities/skills.md)
  - [Git](https://vitehub.dev/raw/docs/agents/capabilities/git.md)
  - [Forge](https://vitehub.dev/raw/docs/agents/capabilities/forge.md)
  - [Gmail](https://vitehub.dev/raw/docs/agents/capabilities/gmail.md)
  - [MCP](https://vitehub.dev/raw/docs/agents/capabilities/mcp.md)
  - [OTLP](https://vitehub.dev/raw/docs/agents/capabilities/otlp.md)
  - [Diagnostics](https://vitehub.dev/raw/docs/agents/capabilities/diagnostics.md) (this page)
  - [Papercuts](https://vitehub.dev/raw/docs/agents/capabilities/papercuts.md)
  - [Web search](https://vitehub.dev/raw/docs/agents/capabilities/web-search.md)
  - [Fetch](https://vitehub.dev/raw/docs/agents/capabilities/fetch.md)
  - [Page](https://vitehub.dev/raw/docs/agents/capabilities/page.md)
  - [OpenAPI](https://vitehub.dev/raw/docs/agents/capabilities/openapi.md)
  - [Transcribe](https://vitehub.dev/raw/docs/agents/capabilities/transcribe.md)
  - [Memory](https://vitehub.dev/raw/docs/agents/capabilities/memory.md)
  - [Title](https://vitehub.dev/raw/docs/agents/capabilities/title.md)
  - [Progress summary](https://vitehub.dev/raw/docs/agents/capabilities/progress-summary.md)
  - [Budget](https://vitehub.dev/raw/docs/agents/capabilities/budget.md)
  - [Usage](https://vitehub.dev/raw/docs/agents/capabilities/usage.md)

Next: [AGENT_R0296](https://vitehub.dev/raw/docs/agents/diagnostics/agent_r0296.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
