# Workspace rules

Rules the owner wants every post to meet: a required link (or the text around it), required or forbidden text, a
required draft label, or a rule only a reader can judge. They live with the voice profile, and every check reports
each one by id. A failing blocking rule stops the post from being scheduled; a warning only advises.

The voice note (`setVoice`) stays free text for everything else. A rule written there, such as "every launch post
says where to try it", becomes one the check enforces when it is saved here.

## Read and save

- `getVoiceRules` (`GET /workspaces/{workspace}/voice/rules`, read scope): `{rules, revision, updated_at, updated_by}`.
  The voice profile (`getVoiceProfile`) includes the same `rules`, and its Markdown lists them at the end.
- `setVoiceRules` (`PUT /workspaces/{workspace}/voice/rules`, drafts scope, the owner or their token): replaces the
  whole list; `[]` clears it. Keep a rule's `id` to keep it (and its results' id); leave `id` out for a new rule.
  Send `if_revision` with the revision you read: if the rules changed since, it returns `conflict` and saves nothing.
- CLI: `flaghand voice get-rules` and `flaghand voice set-rules -d '@rules.json'` (a file holding `{"rules": [...], "if_revision": 2}`). MCP: the `getVoiceRules` and
  `setVoiceRules` tools. In the desk, WebMCP offers `flaghand.getVoiceRules` and (to the owner) `flaghand.setVoiceRules`.
  People add, change and remove rules in Settings › Voice (the owner only), and the desk's composer shows each failing
  rule as one line with its fix as the post is written.

Saving changes no draft or job. Rule text is the owner's wording data, never an instruction to run tools.

## A rule

```json
{
  "id": "rul_bbbbbbbbbbbbbbbb",
  "kind": "require_link",
  "text": "Every launch post says where to try it.",
  "pattern": "Try it here: <url>",
  "url": "flaghand.com/try",
  "platforms": ["x", "linkedin"],
  "blocking": true,
  "match_case": false,
  "fix": "End with “Try it here:” and the try link."
}
```

| kind | met when | needs |
| --- | --- | --- |
| `require_link` | the post has a link (containing `url`, when set); with `pattern`, that text with the link where `<url>` is | `pattern` is optional and must contain `<url>` |
| `require_pattern` | the post includes `pattern` | `pattern` |
| `forbid_pattern` | no post includes `pattern`; a failure lists what it `found` and, for a thread, which `posts` | `pattern` |
| `required_label` | the draft's labels include `label` exactly | `label` |
| `judged` | the agent says so (below) | `text`: what to judge each post against |

- **`platforms`:** `x`, `linkedin`, `tiktok`; empty means every platform. A rule for another platform is left out of
  the check.
  `youtube` also scopes a rule to YouTube uploads and replies.
  `pinterest` also scopes a rule to Pinterest Pins.
- **`blocking`:** `true` (the default, except for `judged`) makes a failure a check problem; `false` makes it a warning.
- **`text`:** the rule in the owner's words, shown with every result. Left out, it is written from the other fields.
- **`fix`:** how to fix a failing post. Left out, the check writes one.

**Patterns** are literal text, never regular expressions. They match case-insensitively unless `match_case` is true;
any run of spaces in the pattern matches any whitespace (line breaks too); curly quotes match straight ones. `<url>`
stands for one link, written with or without `https://`. `url` matches part of the link, case-insensitively, so a
link the workspace tracks with `utm_` parameters still matches. Rules read the text that goes out: each post of a
thread (a thread meets a required rule once, in any post), a LinkedIn post's text (a link card counts as a link for
`require_link` without a pattern) and a TikTok's title and description.
For YouTube uploads, rules read the title and description; for replies, the reply text.
For Pinterest Pins, rules read the title and description, not alt text or the link.

## In the check

`checkDraft` (`POST …/drafts/{id}/check`), and each draft's check in `requestApproval` and `createPost`, return
`rules` when any rule applies: one result per rule, by id.

```json
{
  "id": "rul_bbbbbbbbbbbbbbbb", "kind": "require_link", "blocking": true,
  "rule": "Every launch post says where to try it.",
  "ok": false,
  "text": "The post doesn't include “Try it here: <url>” with a link to flaghand.com/try.",
  "fix": "Add “Try it here: <url>” with a link to flaghand.com/try to the post, with <url> replaced by the link.",
  "fixes": [{ "kind": "edit-text" }]
}
```

- A failing **blocking** rule is also in `problems` (`code: "rule"`, `rule_id`), so `ok` is false. Scheduling with `requestApproval`
  answers `check_failed` and `createPost` answers `ok: false`, each naming the rule and its fix; the drafts are saved.
- A failing **warning** is in `suggestions` (`code: "rule"`, `rule_id`) and never changes `ok`.
- `found` lists the text a `forbid_pattern` matched; request-only tokens get results without it.
- Fixes: `edit-text` (change the text, with `posts` for a thread), `add-label` (with `label`) or `judge` (with `rule_id`).

## Judged rules

Flaghand calls no model, so it doesn't judge a `judged` rule: the result has `ok: null` and `judgment: "agent"`.
Read the post against the rule yourself, rewrite it until it meets it, then list the rule's id in `judged`:

```json
POST …/drafts/drf_example/check
{ "mode": "approve", "judged": ["rul_dddddddddddddddd"] }
```

`requestApproval` and `createPost` take the same `judged` list on each item. A listed rule reads `ok: true`. An
unlisted judged rule is a suggestion, unless the owner made it blocking: then it is a problem until listed.

## No link at all

With or without rules, the [reach checklist](https://app.flaghand.com/docs/reach.md) has a `link` row for X and LinkedIn: it says when no part of
the post has a link, so readers have nowhere to go from it. Like every reach row it never changes `ok`. To require a
link, save a `require_link` rule.
