> For the complete documentation index, see [llms.txt](https://docs.autopilotmonitor.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.autopilotmonitor.com/rules/ai-assisted-rule-authoring.md). Agents can also search and read this documentation through its [MCP server](https://docs.autopilotmonitor.com/~gitbook/mcp).

# AI-Assisted Rule Authoring

Let an AI assistant build gather and analyze rules for you — with built-in validation and a dry-run against real sessions from your tenant before anything goes live.

Building a [custom rule](https://docs.autopilotmonitor.com/rules/overview.md) by hand means knowing the rule schema, the condition operators, the confidence model, and (for gather rules) the collection allowlists. With [AI Integration (MCP)](https://docs.autopilotmonitor.com/integrations/ai-integration-mcp.md) connected, you can skip all of that: describe what you want to detect in plain language, and your AI assistant has everything it needs to produce a working, **tested** rule.

## What the assistant can do

Four building blocks are exposed through the MCP server:

* **Authoring resources** — the complete rule schemas, the gather-rule collection allowlists, and a full authoring guide (condition types, operators, confidence scoring, placeholder interpolation). The assistant reads the real contract instead of guessing.
* **`validate_rule`** — checks a draft rule without deploying anything: schema validation, allowlist checks for gather targets, and lint for rules that would be valid but could never fire (for example an unreachable confidence threshold).
* **`test_analyze_rule`** — the key step: **dry-runs a draft analyze rule against a real session from your tenant**. You get a full trace — which conditions matched (with the actual evidence from the session), how many events of each referenced type the session even contains, how the confidence score builds up, and whether the rule would have fired. Nothing is saved; sessions and analysis results are untouched.
* **`test_log_pattern`** — for log-parsing gather rules: tests a regex against sample lines from your log file **with the exact engine the agent uses on the device** (.NET, case-sensitive, CMTrace or plain-text mode). A pattern that works in an online regex tester can behave differently on the device — this tool removes that guesswork.

The two test tools need the Admin role, so the assistant lists them for admins only. For Operators and Viewers it validates the draft and hands it to an admin.

## The workflow

1. **Describe the scenario** to your assistant, e.g.:

   > *"Create an analyze rule that flags enrollments where the same app failed to install three or more times."*

   Many MCP clients also expose the ready-made **author-rule** prompt, which drives the steps below automatically.
2. The assistant drafts the rule and **validates** it (`validate_rule`), fixing schema or allowlist problems.
3. **Test against real sessions.** Give the assistant a session from your tenant — ideally one where the rule *should* fire and one where it should stay silent:

   > *"Test this rule against session 3f2a… — should it fire there?"*

   The dry-run trace shows exactly why the rule fired or didn't, and previews the explanation text with real values filled in. Iterate until both directions behave as expected.
4. **Create the rule** in the portal (**Rules → Analyze Rules** or **Gather Rules**): **Create Custom Rule** accepts the finished JSON in JSON mode, and the analyze-rule editor's **Test against a session** runs the same dry-run before you save. Dry-running and creating rules require the Admin role.
5. **Share it.** A rule that would help other organizations can go to the community pool from the same page — see [Contribute a Rule](https://docs.autopilotmonitor.com/rules/contribute-a-rule.md).

## From an investigation to a rule

The same tools close the loop after a failure that no rule explains yet. The built-in `debug-session` prompt — or **Copy full prompt** on a failed session in the portal ([When no rule explains a failure](https://docs.autopilotmonitor.com/portal-guide/session-details-and-diagnosis.md#when-no-rule-explains-a-failure)) — has the assistant investigate the session down to the diagnostics package and ends by drafting a rule for the cause it found. Test that draft against the failed session and a successful one, and the next occurrence is flagged automatically.

## Example: from a log file to timeline events

The most powerful pattern: hand the assistant a log file and let it build a **log-parsing gather rule** whose extracted values show up as events in the enrollment timeline.

1. **Attach the log file** (or paste representative lines) and describe what you want:

   > *"Here is our installer's log. Create a rule that shows every ERROR line with its component and error code in the enrollment timeline."*
2. The assistant reads the authoring guide and drafts a `logparser` gather rule. Named capture groups in the regex become data fields on the emitted event:

   ```json
   {
     "ruleId": "GATHER-APPS-101",
     "title": "CustomApp installer errors",
     "collectorType": "logparser",
     "target": "C:\\Windows\\Logs\\CustomApp\\install*.log",
     "parameters": {
       "pattern": "(?<level>ERROR|FATAL)\\s+(?<component>\\w+):\\s+(?<message>.+?)\\s+\\(code\\s+(?<errorCode>0x[0-9A-Fa-f]+)\\)",
       "format": "text",
       "trackPosition": "true"
     },
     "trigger": "interval",
     "intervalSeconds": 120,
     "outputEventType": "gather_customapp_errors",
     "outputSeverity": "Warning"
   }
   ```
3. `validate_rule` confirms the log path is on the collection allowlist and the rule is well-formed.
4. **`test_log_pattern` verifies the regex with the device's real engine.** The assistant sends the pattern plus sample lines from your file and gets back a per-line result: which lines matched, which didn't, and exactly which values (`level`, `component`, `errorCode`, …) each timeline event would carry. Two things it catches that generic regex testers miss:
   * Matching on the device is **case-sensitive** — `ERROR` does not match `error`.
   * IME/SCCM-style logs use the **CMTrace** format: the agent parses each line first and matches your regex against the message only. For plain-text logs the rule needs `"format": "text"` — the tool tells you when every line fails CMTrace parsing.
5. After deploying, each matching log line appears as a `gather_customapp_errors` event in the timeline. On top of it, the assistant can add an analyze rule (for example *"flag the session when any FATAL line appears"*) — and that one **can** be dry-run against real sessions with `test_analyze_rule`.

## Gather rules

Gather rules execute on enrolling devices, so there is no session dry-run for them. For log-parsing rules, `test_log_pattern` (above) covers the part that actually goes wrong — the regex. For all gather rules the assistant validates the target against the [collection allowlists](https://docs.autopilotmonitor.com/rules/gather-rules.md) up front, and after deploying you can verify collection on a test device (the agent's gather-rule debug log setting shows every evaluation decision). A common pattern is a pair: a gather rule collects a new signal, and an analyze rule — which *can* be dry-run — turns it into a finding.

## Tips

* Dry-run against **recent** sessions: sessions that ran before a gather rule was deployed don't contain its events.
* If a condition unexpectedly doesn't match, check the trace's per-condition event count first — it tells you whether the session contains the referenced event type at all.
* Well-tested rules that would help other organizations are welcome as community contributions — submit them from the portal, see [Contribute a Rule](https://docs.autopilotmonitor.com/rules/contribute-a-rule.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.autopilotmonitor.com/rules/ai-assisted-rule-authoring.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
