# Self-validating tasks

How a project adds its own field to tasks and checks it with a Typst function on every `mt check` and every write.

You can give your tasks a field Mindtape doesn’t know about, and check it with a Typst function you write. `mt check` runs the function on every task. The check is a file in the repo beside the tasks.

Every block below is a slice of `examples/self-validating/`, a project with two tasks. The slices keep the older `x-goals` form, a custom field the project checks itself. [Tie work to your goals](https://mlavrinenko.github.io/mindtape/tie-work-to-goals/index.md) is the current recipe, where the body emits markers and the check reads the reserved `markers` input. The example stays as it is.

## Write the function

If your project has goals, you can require every task to say how it serves each one. List the goals in a Typst module. Each task answers in an `x-goals` field. Mindtape keeps any field named `x-<name>` as written and leaves its meaning to the project.

```typ
// goals.typ: the goals every task must address
#let goals = (
  fast-onboarding: (title: "New users succeed without help"),
  no-lock-in: (title: "Nothing here traps a switcher"),
)
```

The check is an ordinary Typst function. It gets the field’s value, or `none` if the task has no such field. It returns one string per problem, and an empty array when the task passes.

```typ
// lib.typ: one breach per goal with no verdict
#import "goals.typ": goals

// `value` is the task's `x-goals`, or `none` when the task has none. Each
// string returned is one breach, and an empty array passes the task.
#let check-goals(value) = {
  if value == none {
    return ("x-goals is missing; add one verdict per goal in goals.typ",)
  }
  goals
    .keys()
    .filter(key => key not in value)
    .map(key => "goal `" + key + "` is not addressed")
}
```

## Turn it on

Add a `[[tasks.rules]]` entry with the module, the function and the field to pass it. [Rules](https://mlavrinenko.github.io/mindtape/rules/index.md) describes each key.

```toml
# examples/self-validating/.mindtape/config.toml: run check-goals on each task's x-goals
[[tasks.rules]]
name = "goals"
check = { typst = "lib.typ", function = "check-goals", field = "x-goals" }
severity = "error"
```

## See it refuse

This task gives a verdict for both goals:

```typ
// tasks/ship-onboarding-checklist.typ: passes
#import "@local/mindtape:0.4.0": *
#show: task.with(
  title: "Ship the onboarding checklist",
  status: done(2026, 7, 1),
  priority: 5,
  x-goals: (
    fast-onboarding: "cuts time-to-first-task from ten minutes to two",
    no-lock-in: "reads and writes the same plain files a human would edit",
  ),
)
```

This one says nothing about `no-lock-in`:

```typ
// tasks/export-report-as-csv.typ: fails
#import "@local/mindtape:0.4.0": *
#show: task.with(
  title: "Export the report as CSV",
  status: wip(2026, 7, 1),
  priority: 4,
  x-goals: (
    fast-onboarding: "not directly, but keeps the report screen simple",
  ),
)
```

`mt check` names the task, the line and the goal it skipped:

```
$ mt check
error: tasks/export-report-as-csv.typ:3: rules.goals: goal `no-lock-in` is not addressed
error: 1 problem in 1 task
```

It exits with code `1`, so a CI job or a pre-commit hook that runs it fails. `mt add`, `mt flip` and the other write commands run the same function on the task they’re about to write, and refuse to write a task that fails it.
