# Estimates

Estimate units, file counts from queries, and the rules that cap an estimate and compare it with git.

## Units

A task’s estimate is a dict of units to numbers, such as `estimate: (files: 5)`. Pick units you can check: files changed, say, rather than hours. `[tasks.estimate] units` lists the unit names you allow, and `required` says which tasks must have an estimate. An estimate is optional when `required` is false, as this repo and `examples/api-service` set it: their size rules read the paths a task’s Changes section names. [Split oversized tasks](https://mlavrinenko.github.io/mindtape/split-oversized-tasks/index.md) shows the rule. `mt` checks `units` on open tasks only. A finished task keeps its unit after you drop it from the list.

`mt` doesn’t check any quantity on its own. A cap or a budget is a [`[[tasks.rules]]`](https://mlavrinenko.github.io/mindtape/rules/index.md) entry that reads one unit as a number, `estimate.<unit>`.

## Counting files with queries

`count(n, ..queries)` adds the files your queries match to the `n` you’d write by hand. The estimate tracks the code as it is today. `[tasks.estimate.queries]` names the units that take it, each with the path set its files must fall in, a glob or a [`@name` set](https://mlavrinenko.github.io/mindtape/named-sets/index.md):

```toml
# examples/estimates/.mindtape/config.toml: the unit allow-list, and the unit that takes count(...)
[tasks.estimate]
units = ["files"]

[tasks.estimate.queries]
files = "**/src/**"
```

A task then writes the unit as a count:

```typ
// tasks/rename-the-harvest-cache.typ: a manual 2, plus a grep
  // Two new files, plus every file that names the type today.
  estimate: (files: count(2, grep(`HarvestCache`))),
```

Two files mention `HarvestCache` under `src/`, so `estimate.files` reads 4, and every rule sees that number. `mt eval` shows where the number comes from. Check a query that doesn’t match anything before you start:

```
$ mt eval rename-the-harvest-cache
SLUG#  STATUS  PRIORITY   TITLE
950    todo    🟡 normal  Rename the harvest cache

estimate.files = 4
  2        manual
  2 files  grep `HarvestCache`
```

The judgment cap below caps it at 5, so when the type spreads to a fourth file, `mt check` refuses the task before you start it.

Two queries build the count:

- `grep(regex, glob)` matches the files whose text matches `regex`, in [Rust regex syntax](https://docs.rs/regex/latest/regex/#syntax). Write the regex as a raw block, where a backslash doesn’t need an escape. The optional glob, or `@name` set, narrows it to the paths it admits.
- `glob(pattern)` matches the files whose path matches `pattern`.

A file that more than one query matches counts once. When queries overlap, the breakdown adds a `distinct across all queries` row counting the files the unit adds. The total then reads as the manual part plus that row. Queries read the files git lists: tracked ones and untracked ones `.gitignore` doesn’t hide, with paths relative to the repository root, as `changed()` reads them. `mt` matches them itself and doesn’t run a shell. The count comes out the same on every machine. A unit with no `[tasks.estimate.queries]` entry takes a plain number only, and `mt check` refuses a count there.

### Structural matching

`ast_grep(language, pattern, glob?)` matches code structure rather than text. It distinguishes a call from a definition, a comment or a string, and it ignores code a macro expands to. This build supports `rust` (alias `rs`), and a query reads only `.rs` files. Write the pattern in [ast-grep syntax](https://ast-grep.github.io/guide/pattern-syntax.html), as a raw block, where a backslash doesn’t need an escape. The optional glob or `@name` set narrows the unit’s paths, as it does for `grep`.

```typ
// tasks/warm-the-cache-on-startup.typ: one new call site, plus every file that already calls it
  // One new call site, plus every file that already calls it.
  estimate: (files: count(1, ast_grep("rust", `warm($$$ARGS)`))),
```

The fixture’s `src/` holds three `warm(...)` calls across two files, two of them in `main.rs`. The unit reads 3: the manual 1, plus the 2 distinct files its query matches. `mt eval` shows the breakdown:

```
$ mt eval warm-the-cache-on-startup
SLUG#  STATUS  PRIORITY   TITLE
1pj    todo    🟡 normal  Warm the cache on startup

estimate.files = 3
  1        manual
  2 files  ast-grep rust `warm($$$ARGS)`
```

A file counts once however many call sites it holds. The unit reads the number of distinct files. A pattern the grammar refuses, or a language this build does not support, leaves the unit unresolved. `mt check` reports why and `mt eval` prints the problem instead of a number. A glob that isn’t a string is a shape error at `estimate.<unit>.queries[i]`, which `mt check` and `mt eval` report. An export keeps the query under the `ast-grep` JSON tag, so `mt import` writes the same call back.

## Caps

```toml
# examples/estimates/.mindtape/config.toml: a ceiling for judgment work, a ceiling for mechanical work and a floor
[[tasks.rules]]
name = "judgment-stays-small"
when = "!terminal && difficulty >= 3"
assert = "estimate.files <= 5"
message = "split the task into 5 files or fewer, or lower its difficulty"
severity = "error"

[[tasks.rules]]
name = "mechanical-cap"
when = "!terminal && !(difficulty >= 3)"
assert = "estimate.files <= 15"
severity = "error"

[[tasks.rules]]
name = "at-least-one-file"
when = "!terminal"
assert = "estimate.files >= 1"
severity = "error"
```

`!terminal` in each `when` leaves finished tasks alone. A breach at `severity = "error"` is a validation error, so every write command refuses it. Here `mt set` turns a mechanical task estimated at 12 files into judgment work, and the judgment cap of 5 refuses it. `mt` doesn’t write the task file and exits `1`:

```
$ mt set rename-the-status-glyphs difficulty=3
error: tasks/rename-the-status-glyphs.typ:3: rules.judgment-stays-small: split the task into 5 files or fewer, or lower its difficulty
```

For `mt add` refusing a task over a cap, see [Split oversized tasks](https://mlavrinenko.github.io/mindtape/split-oversized-tasks/index.md).

## Working-tree budget

A file count is an estimate a rule can compare with git, so you find out when a task grows past its plan. [`changed()`](https://mlavrinenko.github.io/mindtape/filters/index.md) counts the paths git reports changed. The example rule compares that count with the estimate of each task in progress. The built-in `mindtape:budget` does not use the estimate. It compares the changed-path count with `count_paths(derived.changes, "@source")`, the paths the Changes section names. This example keeps an estimate-based rule to document the field.

```toml
# examples/estimates/.mindtape/config.toml: the working-tree budget, skipping generated files
[[tasks.rules]]
name = "budget"
when = 'status == "wip" && has(estimate.files)'
assert = 'changed("staged+worktree", "!docs/generated/**", "!*.lock") <= estimate.files'
message = "rescope the task or raise its estimate before changing more files"
severity = "warn"
```

A glob starting with `!` skips matching paths, so generated files don’t use up a budget meant for the source that produces them. Globs match paths relative to the repository root. `has(estimate.files)` keeps the rule off a task with no estimate.

Git reports one set of changes, and nothing in it says which file belongs to which task. So the rule compares that set with each task in progress. That’s only exact when one task is in progress at a time (see [work-in-progress caps](https://mlavrinenko.github.io/mindtape/rules/index.md)). At `severity = "warn"` a breach prints a warning and `mt check` still exits 0, because an unplanned file is normal, and the fix is to rescope the task.

After `mt wip rewrite-loader` and five new files under `src/`, six paths have changed, counting the task file, against an estimate of 5:

```
$ mt check
warning: tasks/rewrite-loader.typ:3: rules.budget: rescope the task or raise its estimate before changing more files
ok: 4 tasks
```

> **Note:** A rule that reads `changed()` needs a git repository. Outside one, `mt` reports the rule once as skipped. `mt` runs git read-only, at most once per command.
