# Agent onboarding: set up CapDAG

`capdag` finds capabilities (caps), runs them on files, and plans and runs machines of them: a
machine is several caps wired one after another. Cartridges provide the caps; capdag comes with
some, and downloads the others the first time a run needs one.

These instructions are for capdag 1.1086.551.

Ask the user before `sudo`. If a step fails, stop and report the command and its output.

## 1. Detect

`uname -sm`. capdag is published for macOS on Apple silicon (`Darwin arm64`) and Linux on
x86_64 (`Linux x86_64`) with glibc 2.35 or newer (`ldd --version`: Ubuntu 22.04, Debian 12,
Fedora 36, RHEL 10 and later), OpenSSL 3 and ALSA. There is no build for Intel Macs, Linux on
arm64 or Windows: tell the user, and stop.

If `capdag --version` already prints `capdag 1.1086.551` or later, go to step 3.

## 2. Install

| System | Install |
| --- | --- |
| macOS, Linux with Homebrew | `brew install machinefabric/tap/capdag` |
| Debian, Ubuntu (amd64) | apt, below |
| Fedora, RHEL (x86_64) | dnf, below |
| any, without `sudo` | the archive, below |

```sh
# apt
curl -fsSL https://release.machinefabric.com/packages/keys/repo-signing-prod.asc \
  | sudo gpg --dearmor -o /usr/share/keyrings/machinefabric.gpg
echo "deb [signed-by=/usr/share/keyrings/machinefabric.gpg] https://release.machinefabric.com/packages/apt stable main" \
  | sudo tee /etc/apt/sources.list.d/machinefabric.list
sudo apt update && sudo apt install capdag
```

```sh
# dnf
sudo tee /etc/yum.repos.d/machinefabric.repo >/dev/null <<'EOF'
[machinefabric]
name=MachineFabric
baseurl=https://release.machinefabric.com/packages/rpm/$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://release.machinefabric.com/packages/keys/repo-signing-prod.asc
EOF
sudo dnf install capdag
```

The archive: the manifest, <https://release.machinefabric.com/capdag-cli/manifest>, names the
latest release (`channels.release.latestVersion`) and, under that version's
`platforms.<platform>.installers`, the `targz` with its `sha256`. It unpacks to `capdag-bundle/`:
the `capdag` executable and the `bundled-cartridges/` it checks at every start. Keep the two
together and link the executable onto the `PATH`; never copy it out.

```sh
version=1.1086.551        # channels.release.latestVersion
platform=linux-x86_64     # or darwin-arm64
mkdir -p ~/.local/share/capdag ~/.local/bin && cd ~/.local/share/capdag
curl -fLO "https://release.machinefabric.com/capdag-cli/release/$version/capdag-$version-$platform.tar.gz"
sha256sum "capdag-$version-$platform.tar.gz"   # macOS: shasum -a 256; must equal the manifest's sha256
tar xzf "capdag-$version-$platform.tar.gz" && rm "capdag-$version-$platform.tar.gz"
ln -sf ~/.local/share/capdag/capdag-bundle/capdag ~/.local/bin/capdag
```

If `~/.local/bin` is not on the `PATH`, add it to the shell's profile.

## 3. Check

```sh
capdag --version
capdag cartridge list
```

```text
capdag 1.1086.551
datacartridge          1.260.1009   bundled
fetchcartridge         1.210.474    bundled
marketcartridge        1.46.800     bundled
modelcartridge         1.415.1420   bundled
txtcartridge           1.350.874    bundled
```

capdag reads its caps from the fabric registry over the network and keeps what it reads in
`~/.cache/capdag` (macOS: `~/Library/Caches/capdag`). The first commands are slower while it
fills that cache.

## 4. How to drive it

- **stdout** carries results only; everything for a person goes to stderr.
- **`--json`** (before or after the command) prints JSON Lines on stdout, one record per line,
  each with a `type`. Read these rather than the text.
- **`--no-input`**: never ask. Without a terminal capdag asks nothing anyway: a choice it needs
  is an error naming the flag that makes it.
- **Exit status**: 0 done; 1 failed; 2 not asked properly (an unknown name or flag, a missing
  value, a choice not made); 3 a run completed but a step's policy tolerated a failure; 130
  stopped by a person.

| Command | `--json` records |
| --- | --- |
| `caps` | `cap`: `alias`, `title`, `takes`, `gives`, `urn` |
| `info` | `cap`: `alias`, `urn`, `bundled`, `installed`, `definition`; `media`: `alias`, `title`, `urn`, `extensions`, `taken_by` |
| `run` | `step` (`step`, `state`, `progress`), `run_finished` (`label`, `outcome`, `seconds`, `outputs`), last `finished` (`outcome`, `exit`) |
| `plan` | `input`, `target`, `machine` (`pick`, `notation`, `steps`, `model`, `downloads`), `saved` |
| `check` | `machine`: `notation`, `canonical`, `inputs`, `outputs` |
| `cartridge list` | `cartridge`: `id`, `version`, `state`, `warnings` |
| a choice not made | `choice` per option (`flag`, `value`, `label`, `detail`), then the error |
| any failure | `error`: `problem`, `context`, `hint`, `exit` |

## 5. Find a capability

```sh
capdag caps csv                      # every word must match an alias, title, or the media it takes or gives
capdag caps --in csv --out json      # what takes one type and gives another
capdag info csv2json                 # what a cap takes, its options, what it gives, its cartridge
capdag info csv                      # a media type: its URN and the caps that take it
capdag run csv2json --help           # a cap's input, required options and options
```

```sh
capdag caps csv2json
capdag --json caps csv2json
```

```text
csv2json  Convert CSV to JSON Array of Objects (csv → json-records)
```

```text
{"alias":"csv2json","gives":"json-records","takes":"csv","title":"Convert CSV to JSON Array of Objects","type":"cap","urn":"cap:convert-format;in=\"media:enc=utf8;fmt=csv;list;record\";out=\"media:enc=utf8;fmt=json;list;record\""}
```

Names are aliases (`csv2json`, `csv`); `-v` shows the URN behind each. A cap's name typed as a
command (`capdag csv2json`) answers with how to run it.

## 6. Run a capability

```sh
printf 'name,city\nAda,London\nAlan,Wilmslow\n' > people.csv
capdag run csv2json people.csv
cat people.csv | capdag run csv2json
capdag run csv2json people.csv -o out
```

One result is written raw to stdout:

```text
[
  {
    "city": "London",
    "name": "Ada"
  },
  {
    "city": "Wilmslow",
    "name": "Alan"
  }
]
```

With `-o <dir>`, and under `--json`, results are files, named `{input}.{output}.{ext}`
(`out/people.output.json`), listed on stdout, or in `run_finished`'s `outputs`. Each input file
is a run of its own. A file that exists is not overwritten without `--force`.

A cap's options are flags, as `capdag run <cap> --help` lists them. A cap that takes no input
runs on nothing, and a cap that gives a sequence writes one file per item:

```sh
capdag run list-countries --words italy
cat list-countries.output.0.json
```

```text
{"alpha_3":"ITA","code":"IT","currency":"EUR","name":"Italy"}
```

An option named like one of `run`'s own (`-o`, `--force`) is given as `--arg <name>=<value>`.

## 7. Machines

A machine file holds statements `[source -> cap -> target]`, one per line. The cap is an alias;
`source` and `target` are names of your choosing for the data between steps, and a name used
twice is one place.

```sh
printf '[people -> csv2json -> records]\n[records -> json2yaml -> yaml]\n' > people.machine
capdag check people.machine
capdag graph people.machine
capdag run people.machine people.csv
```

```text
✓ people.machine
  inputs:  people (csv)
  outputs: yaml (yaml-records)

  [(input=csv) -> csv2json -> (output=json-records)]
  [(input=json-records) -> json2yaml -> (output=yaml-records)]

Run it: capdag run people.machine <people>
```

```text
- city: London
  name: Ada
- city: Wilmslow
  name: Alan
```

`check` resolves the machine without running it; `graph` draws it as text. An input can be
given by its name: `capdag run people.machine --input people=@people.csv`. A step's option is
set in the machine, as a named constant:

```sh
printf 'one\n' > a.txt; printf 'two\n' > b.txt
printf '[(parts, separator=" | ") -> concat -> line]\n' > join.machine
capdag run join.machine --input parts=@a.txt --input parts=@b.txt
```

```text
one
 | two
```

A machine's input given several paths with `--input` takes them as one sequence; several paths
given without it are a run each. [Machine notation](https://machinefabric.com/capdag/docs/09-machine-notation/) is the whole
language.

## 8. Let capdag plan

```sh
capdag plan people.csv                                # what the file can become
capdag plan people.csv --to json-records              # the machines that make it, numbered
capdag plan people.csv --to json-records --pick 2 --save people-json.machine
capdag plan people.csv --to json-records --pick 2 --run -o out
```

```text
32 machines make json-records of 1 CSV, CSV, UTF-8, List, Record:

  1. 1 step
     [(input=csv) -> collect-csv-records-to-json-array -> (output=json-records)]

  2. 1 step
     [(input=csv) -> csv2json -> (output=json-records)]
```

`--to` takes a media alias (`json-records`), an extension or a media URN. Ten machines are listed
(`--all` lists every one). Read their steps and pick the one that does what the user asked: the
first is the nearest match in type, not always the right one. Without `--pick`, `--save` and
`--run` fail with exit 2, listing the choices (`choice` records under `--json`). `--save`
refuses an existing file without `--force`.

## 9. Cartridges

```sh
capdag cartridge list
capdag cartridge install <cartridge or cap>
capdag cartridge remove <cartridge>
```

A cap a bundled cartridge provides needs nothing installed. Another cap's cartridge is downloaded
and verified the first time a run needs it, or ahead of time with `install`. A cartridge with no
build for the system fails, naming the cartridge and the platforms it has builds for:

```text
error: <cartridge> could not be installed
  Cartridge download failed: Cartridge <cartridge> v<version> has no build for platform '<platform>'. Available: [<platforms>]
```

The caps that cartridge provides do not run on this system. Tell the user; retrying does not help.

## 10. Models

`capdag info <cap>` says whether a cap runs a model, and which one by default:

```sh
capdag info summarize-en
```

```text
Runs a model: hf:unsloth/Qwen3-4B-Instruct-2507-GGUF?include=Qwen3-4B-Instruct-2507-Q4_K_M.gguf by default (--model-spec chooses another)
```

The cap downloads it the first time it runs. `--model-spec 'hf:<owner>/<repo>?include=<file>'`
chooses another; `capdag run <cap> --help` lists it with its default.

## 11. When a command fails

Every error says the problem (`error:`), what was expected and found (the indented lines), and
the next command (`hint:`); under `--json` it is the last record:

```sh
capdag --json run csv2jsn people.csv
```

```text
{"context":["did you mean csv2json?"],"exit":2,"hint":"find capabilities with: capdag caps csv2jsn","problem":"there is no capability or machine file named 'csv2jsn'","type":"error"}
```

| It says | Do |
| --- | --- |
| `there is no capability or machine file named …` | the name it suggests, or `capdag caps <words>` |
| `… is missing required options:` | give each option it lists |
| `… options for the machine, and none was chosen` | `--pick <n>` |
| `the run's results could not be written` | `--force`, or another `-o`; the results were kept where it says |
| `the output directory '…' is a file` | give `-o` a directory |
| `has no build for platform` | step 9 |
| `Network access blocked` under `--offline` | run the command once without `--offline`, which caches what it reads |
| `the cached … is corrupt` | `capdag cache clear`, then the command again |
| anything else | the command again with `-v`, or `-vv` for the cartridges' debug logs; report it to the user with that output |

## 12. Updating

With the tool it was installed with: `brew upgrade machinefabric/tap/capdag`,
`sudo apt update && sudo apt install --only-upgrade capdag`, `sudo dnf upgrade capdag`, or, for
the archive, `capdag self update`, which installs the latest release only when its signature and
digest check, and says `capdag 1.1086.551 is the latest release` when there is none newer.

## Reference

- All docs, indexed: <https://machinefabric.com/capdag/llms.txt>
- [Installing](https://machinefabric.com/capdag/docs/18.0-installing/) ·
  [CLI reference](https://machinefabric.com/capdag/docs/18.1-cli-reference/) ·
  [when a command fails](https://machinefabric.com/capdag/docs/18.5-when-a-command-fails/) ·
  [machine notation](https://machinefabric.com/capdag/docs/09-machine-notation/) ·
  [planner](https://machinefabric.com/capdag/docs/15.4-planner/) ·
  [media URNs](https://machinefabric.com/capdag/docs/11-media-urns/)
- Make a cartridge: [build and run a cartridge](https://machinefabric.com/capdag/docs/18.2-getting-started-cartridge-development/)
- Community: <https://discord.gg/utwdVm6tYB>
