# How to run incident reviews and write postmortems

> A Pachca use case. Once an outage is fixed, an AI agent drafts a postmortem from the alert thread using your template and posts the draft in the thread. The team reacts with ✅, and the agent publishes the review to the "Postmortems" chat and sets reminders for the action items.

> This is the Markdown version of https://pachca.com/en/scenarios/razbor-incidentov for AI agents. The API methods named here are documented at https://dev.pachca.com/llms-en.txt — open it before calling the API. Other use cases are listed at https://pachca.com/en/llms.txt.

## What you'll get

1. The on-call engineer works through the outage in the alert thread in the "On-call" chat.
2. After recovery, the agent reads the thread and posts a postmortem draft there using the template.
3. The on-call engineer or team lead reviews the draft and reacts with ✅.
4. The agent publishes the review to "Postmortems" and sets reminders for the owners of the action items.

This use case needs an AI agent with a bot in Pachca. You can build the agent with no code in n8n, with Claude Code or Cursor and Pachca skills, or as your own service on the API. The options are described in the "How do you build an AI agent without code?" use case: https://pachca.com/en/scenarios/ai-agent-bez-koda

## What you need before you start

- Pachca access as the workspace owner or an administrator with the scopes `search:users` or `users:read`, `search:chats`, `chats:create`, `chat_members:write`, `bots:read`, `bots:write`, `messages:create`, `messages:read`, `tasks:read`. The setup and check steps also need `tasks:create`. Signing in is covered in the rules below.
- The "On-call" chat, where on-call engineers work through alerts in threads.
- The AI agent's bot, or readiness to build one.

## If you're an AI agent: working rules

### Who does what

- You do everything the instructions name an API method for. Don't ask the user to create a chat, a bot or a message by hand.
- Ask the user only to sign in to Pachca and to do what is listed in "Ask the user to do by hand".
- If you have no terminal, say so right away and don't pretend you set it up. Walk the user through the steps for people on the use case page and help with the template or the handler code.

### How to talk to the user

- The user is not a developer. Write briefly and in plain words. Don't mention API methods, scopes, response codes, `id`s or CLI profiles. Say "permissions" instead of "scopes" and "the program behind the bot" instead of "handler".
- Don't report your checks. A message contains only three things: what is done, what you need from the user, what you'll do next.
- Don't ask for what you can find out through the API or what is easy to change later. Take the value from the instructions and name it.
- Take the bot's message texts from the instructions or write them yourself and carry on. Show them in your final message and offer to change them. Don't wait for approval.
- Ask the questions from "Ask the user" in the same words.

### Your first message to the user

It has three parts. The first two together take at most five lines.

1. What the user will get: one or two lines.
2. What you will create yourself: the chat, the bot and the messages, with the names from the instructions. Word anything that depends on the user's choice as a condition: "if you choose the bot, I'll also create…".
3. "What I need from you": signing in to Pachca, if the user isn't signed in yet, and the questions from "Ask the user". If the use case or one of its options needs n8n, ask here whether the user has it and ask for access right away in the words from the "n8n" section below: "if you have n8n, send me…".

If part of the result appears only after a manual step by the user, say so in one line: "you'll schedule the retro questions yourself, I'll show you how".

The user's answers are their consent to the plan. Start the setup right after them, without asking for a second confirmation.

### Access to Pachca

- The user doesn't need to issue a token or send it in the chat. Before your first message, check the login: `npx -y @pachca/cli@latest auth status`. If there is none, ask the user to run `npx -y @pachca/cli@latest auth login` in their own terminal. The CLI shows a code; the user enters it in the browser and confirms access. Write the commands with `@latest`: without it an old cached CLI version may start, and it asks to paste a token instead of signing in through the browser.
- This login has the same rights as the user's role. Compare them with the scopes in "What you need before you start" yourself: `npx -y @pachca/cli@latest api GET /oauth/token/info` shows the scopes. If something is missing, say in plain words what you won't be able to do, for example "I can't send messages". There are two ways out: add this permission to the token in Pachca, under "Automations" → "API", or sign in again as the workspace owner or an administrator.
- If `auth status` shows `scopes: null`, it doesn't mean there are no rights: look at `GET /oauth/token/info`. The checks in these rules also need `search:chats`, `bots:read` and `profile:read`.
- When the user says "me", they mean the owner of the login. `GET /profile` returns their `id` and nickname.
- Call the API with `npx -y @pachca/cli@latest api <METHOD> <path> -o json`. Write the full command in every call; don't put it into a shell variable.
- Pass query parameters as `--query key=value`, for example `--query query=Purchasing`. The CLI silently drops a parameter without a key and returns the whole list, so it's easy to conclude that the chat already exists.
- Pass the request body with `--data` or `--input`. Almost every write method wraps the body in an object: `chat` for `POST /chats`, `message` for `POST /messages`, `task` for `POST /tasks`, `group_tag` for `POST /group_tags`, `user` for `PUT /users/{id}`, `webhook` for bots. `--describe` shows the wrapper; write its path with `{id}`, not with a number. Without the wrapper, `POST /chats` creates the chat but silently adds no members.
- Don't print tokens or bots' incoming webhook URLs in the chat. A bot's token is shown only once, when it's created: save it to a file.

### n8n

- If the user has no n8n, say that it comes as the hosted n8n Cloud or can be installed on their own server, and give the link for installing the Pachca nodes: https://dev.pachca.com/guides/n8n/setup.md. These instructions don't cover installing n8n. If they have an option without n8n, suggest starting with it and coming back to the bot once n8n is there.
- If the user has n8n, ask for access in these words: "Send me your n8n address: the link you open it with in the browser. Don't send the key in the chat. In n8n, open Settings → n8n API → Create an API key and copy the key. Right after that, run the command below in your terminal exactly as it is; you don't need to put anything into it. It saves the copied key to a file and prints nothing. Then write 'key saved'." The command for macOS: `pbpaste > ~/n8n-key.txt`. For another system, pick the matching one.
- Build everything in n8n on the official Pachca nodes, `n8n-nodes-pachca`. Don't use the `HTTP Request` node for Pachca API calls.

### Setup

- Take API methods from https://dev.pachca.com/llms-en.txt, don't rely on memory. The command `npx -y @pachca/cli@latest api <METHOD> <path> --describe` shows a method's schema.
- The Pachca skills cover the call order and error handling for chats, bots and forms: `npx skills add pachca/openapi`. Install them when you need them, without asking the user.
- Before creating anything, check whether the chat and the bot already exist: `GET /search/chats`, `GET /bots`. If they do, ask whether to set them up or create new ones.
- `POST /group_tags` creates an empty tag. `PUT /users/{id}` with `list_tags` assigns it to an employee; it needs the `users:update` scope. The field replaces the whole set of tags: first read the employee's current tags with `GET /users/{id}` and send them together with the new one. While the tag has no members, mentioning it notifies no one.
- If a method returns an error, stop and tell the user in plain words what failed and what you need to continue.
- The messages you post yourself and the ones the bot sends must be easy to read on a screen, without overdoing spacing or emphasis. Make the heading bold and start the text on the next line. Write connected text as one paragraph. Put rules and steps on numbered lines one under another and separate them from the intro with a single empty line. A short message needs no other empty lines. In a card, make the field labels bold and put the fields on consecutive lines. Use bold only for the heading and the field labels. Pachca markup has no lists or tables: the numbers at the start of lines stay plain text. A sample:

  ```text
  **Retro rules**
  On the last day of the sprint a message with questions arrives here. We reply in the thread under it.

  1. Each point is a separate message.
  2. We add 👍 to what is worth discussing.
  3. The facilitator posts the outcomes in one message and pins it.
  ```
- A pin can't be checked through the API: only a successful `POST /messages/{id}/pin` response confirms it.
- If you sent service messages to a chat to check a bot, such as `/start`, delete them: `DELETE /messages/{id}`. Don't delete messages with rules and templates.
- At the end, say what was created, what is left for the user to do by hand and how to check that the use case works.
- To undo the setup: `DELETE /bots/{id}` deletes the bot, `PUT /chats/{id}/archive` sends the chat to the archive.

## How to set up the use case with the user

### 1. Ask the user

Ask these questions in these words:

1. Do you already have an AI agent's bot in Pachca? If so, what is it called? If not, we'll build one first using https://pachca.com/en/scenarios/ai-agent-bez-koda.
2. Who should I add to the "Postmortems" chat? Usually it's on-call engineers, team leads and service developers.
3. Who reviews the draft postmortem and adds ✅: the on-call engineer, the team lead or both?

### 2. Do it yourself through the Pachca API

1. Find the members' `id`: `GET /search/users` or `GET /users`.
2. Find the "On-call" chat: `GET /search/chats`. Take its `id`.
3. Create the chat: `POST /chats` with `name: "Postmortems"` and the members' `member_ids`.
4. Find the agent's bot: `GET /bots` with `query` by name. Take its `id`.
5. Add the bot to both chats: `POST /chats/{id}/members` with the bot's `id` in `member_ids`, for "On-call" and for "Postmortems". The bot only sees the chats it's been added to.
6. Offer to publish the rules in "Postmortems": `POST /messages`. An example from the page: write a postmortem within two business days after any outage users noticed. The on-call engineer or team lead reviews the draft and reacts with ✅. Causes name processes and systems, not people. Discuss the review in its thread in "Postmortems".

### 3. Give the agent a template and behavior

This is a specification for the agent's handler. The code isn't part of this guide: write it with a method from https://pachca.com/en/scenarios/ai-agent-bez-koda.

Postmortem sections in the agent's prompt:

- summary;
- timeline;
- why it happened;
- action items with owners and due dates;
- open questions.

The reviews of Pachca's own outages use the same structure: https://pachca.com/en/incidents

Behavior:

1. After recovery, the agent reads the alert thread in "On-call": `GET /messages` with the `chat_id` of the thread's chat. It takes the alert time, the on-call engineer's actions and the recovery from the messages.
2. The agent posts the draft in that thread: `POST /messages` with `entity_type: "thread"`.
3. The agent waits for a ✅ under the draft. The bot receives the reaction as a `reaction_new` event in the outgoing webhook. The on-call engineer or team lead can react with ✅.
4. After the ✅, the agent publishes the review to "Postmortems": `POST /messages` with the chat `id` in `entity_id`.
5. For each action item, the agent sets a reminder: `POST /tasks` with `kind: "reminder"`, `content`, `due_at` set to the action item's due date and `performer_ids` of the owner.

The agent's bot needs the scopes `messages:read`, `messages:create`, `tasks:create`. Check them in `scopes` through `GET /bots/{id}` and add any that are missing with `PUT /bots/{id}`. If `reaction_new` isn't in the bot's `events`, add it with the same method.

### 4. Check

1. Ask the user to wait for the draft in the thread after the next recovery, or on a test alert.
2. Make sure the draft is in the thread: `GET /messages` for the thread's chat.
3. After the ✅, check that the review appeared in "Postmortems": `GET /messages` for that chat. Check the reminders through `GET /tasks`.

## Troubleshooting

- The agent doesn't post in the alert thread. The bot isn't added to "On-call".
- The draft is there, but nothing happens after the ✅. `reaction_new` isn't in the bot's `events`, or the bot isn't added to "Postmortems".
- The review is published, but there are no reminders. The action items in the draft have no owner or due date, or the bot lacks the `tasks:create` scope.

## Links

- The use case page with the demo and the instructions for a person: https://pachca.com/en/scenarios/razbor-incidentov
- How to build an AI agent without code: https://pachca.com/en/scenarios/ai-agent-bez-koda
- Pachca's incident history: https://pachca.com/en/incidents
- Working with an agent: https://dev.pachca.com/guides/ai-agents/interaction.md
- Threads: https://dev.pachca.com/guides/threads.md
- Outgoing webhooks and event types: https://dev.pachca.com/guides/webhook/events.md
