---
name: tally
description: Count distinct lines or fields and report the most frequent, ordered by count then bytewise key; one bounded pass, the same answer on every machine.
---

# tally 0.1.0

Count distinct lines or fields and report the most frequent, ordered by count then bytewise key; one bounded pass, the same answer on every machine.

```
tally [--root DIR] [--field N [--delim D]] [--top N] [--max-keys N] [--max-line-bytes N] [--format json|text] [FILE...]
```

## Output

One JSON object on one line, schema `tally.v1`: `{ok, command, schema, data, error, errors, meta}`. `ok` is false exactly when `errors` is non-empty; `error` is the first.

An error is `{code, rule, message, hint, repair, detail}`. Match on `rule`, not on `message`. When `repair` is `{"kind":"retry","argv":[...]}` the argv can be run as it stands; it never widens what the tool may touch.

`--format text` is for a person and is lossy; do not parse it.

## Flags

- `--root <path>`: resolve every FILE relative to this directory and refuse paths outside it
- `--field <nat>` (`-f`): count field N (1-based) of each line instead of the whole line
- `--delim <text>` (`-d`): the one byte that separates fields (default tab) Default `	`.
- `--top <nat>`: how many of the most frequent keys to report (ceiling 1000000) Default `10`.
- `--max-keys <nat>`: the most distinct keys held, one more is limit.too-many-keys (ceiling 1000000) Default `100000`.
- `--max-line-bytes <nat>`: the longest line counted, a longer one is limit.line-too-long (ceiling 16777216) Default `1048576`.
- `--format <choice:json/text>`: json for a program, text (count, tab, key) for a person Default `json`.

An unknown flag is refused (`args.unknown-flag`), never ignored. POSIX spellings are not promised.

## Guarantees

- **deterministic**: the same input gives byte-identical output, through a pipe, a file or a terminal (M2)
- **idempotent**: running it twice leaves the same state as once (M7 for the writers, and a reader changes nothing)
- **bounded_memory**: peak memory does not grow with the input's size (M9)
- **concurrency**: read-only; it never writes.

## Exit codes

- `0` SUCCESS: what was asked was done; zero matches is success
- `1` GENERAL_ERROR: the operating system failed mid-operation, or the tool caught a bug in itself
- `2` INVALID_ARGS: the invocation is malformed or names something of the wrong kind; nothing was read
- `3` NOT_FOUND: a named input does not exist
- `4` PERMISSION_DENIED: the operating system refused, or the tool's own confinement did
- `8` PRECONDITION_FAILED: the answer is no, or a limit was reached
- `132`: the tool trapped. A bug in the tool; there is no JSON.
- `141`: the reader closed the pipe.

## Rules

- `args.unknown-flag` (exit 2): a flag that is not in the tool's table
- `args.missing-value` (exit 2): a flag that takes a value was given none
- `args.bad-value` (exit 2): a flag's value is not of the flag's kind
- `args.duplicate-flag` (exit 2): the same flag was given twice
- `path.empty` (exit 2): an empty path
- `path.dotdot` (exit 2): a path with a `..` component
- `path.absolute` (exit 2): an absolute path where one relative to --root is wanted
- `path.outside-root` (exit 4): an absolute path that is not under --root
- `path.too-long` (exit 2): a path longer than 4096 bytes
- `path.symlink` (exit 4): a path that runs through a symbolic link, which the tool does not follow
- `io.not-found` (exit 3): a named input does not exist
- `io.not-a-directory` (exit 3): a path runs through something that is not a directory
- `io.is-a-directory` (exit 2): a directory where a file is wanted
- `io.permission-denied` (exit 4): the operating system refused access
- `io.read-failed` (exit 1): the operating system failed a read
- `limit.line-too-long` (exit 8): a line longer than --max-line-bytes
- `limit.too-many-keys` (exit 8): more distinct keys than --max-keys

## Authority

What the compiler says this program can reach (`cancho authority`), embedded at build time:

```json
{"bounded":true,"unbounded_by":[],"effects":["args","dir_read","err_write","file_read","fs_read","heap","io_read","io_write"],"labels":[{"name":"args","argument":null,"bounded":true},{"name":"dir_read","argument":null,"bounded":true},{"name":"err_write","argument":null,"bounded":true},{"name":"file_read","argument":null,"bounded":true},{"name":"fs_read","argument":"","bounded":true},{"name":"heap","argument":null,"bounded":true},{"name":"io_read","argument":null,"bounded":true},{"name":"io_write","argument":null,"bounded":true}],"never_touches":["the network","signals","other programs","foreign code"],"foreign_symbols":[],"pure":["alphabet","any_nul","authority","before","built","bytes","catalogue","chunk_size","code_meaning","code_name","compare","compiler","count","count","count","count_byte","decode","default_of","digit_of","distance","done","emits","enotdir","entries","entries$int","entry","entry","entry_at","entry_end","entry_in","equal","err_control","err_depth","err_end","err_escape","err_full","err_number","err_syntax","err_trailing","err_utf8","escape_width","exit_code","exit_in_entry","exit_of_in","expected","failed","field","field_of","field_of","find","find$int","find_in","find_slot$int","finish","first","flag_table","full","get$int","guarantee_keys","guarantees_has","has","has_dotdot","hash","hex4","hex_digit","hex_value","in_choice","in_range","index_of","index_of_short","io_rule","is_ascii","is_digit","is_digit","is_flag","is_hex","is_live$int","is_loop","is_named","is_tail","is_utf8","is_valid","is_ws","item","key_at$int","key_ceiling","key_of","kind_of","lead_bits","length","line","line_ceiling","listed","long","longest","max_depth","message_of","more","name_of","nearest","need","none","number","ok","operand_count","operand_index","parse_nat","plain_byte","repairable_in","rule_of","scan_for","schema","second_range","seed","sentences","shell_builtin","shown","shr33","size","size$int","size$int","slash","slot_count","start","starts_with","string_end","summary_in","takes_value","tape_len","text","tool","under","valid","value_at$int","value_index","value_is_whole","width_of"],"folded_operators":39,"folded_calls":0,"functions":250}
```

The row names *kinds* of access, not *extent*: a path from the command line is checked at run time, so it reads `fs_read("")`. Symlinks are followed.
