# cns gate

Source: https://usecns.dev/cli-gate/

## Listing 6.3.1   Synopsis

```
cns gate [--systemone] [<soma concerns check args>...]
```

## Description

Execute the closeout gate in a Soma-initialised repository.

## Structural checks

* Run `axon index` when `.axon/axon.db` exists.
* Run `soma verify` and `soma concerns check --check [--axon <bin>] [--systemone] [args]`
  unconditionally; caller arguments never drop `--check`.

## Live rubric opt-in

The System 1 rubric sends live inference and is OFF by default:
`rubric: not evaluated (disabled by default)`. To authorize live inference for this path:

* Set `CNS_ORACLES=structural,rubric` in the environment; or
* Set top-level `oracles = ["structural", "rubric"]` in `.cns/config.toml`.
  A key inside a `[table]` is ignored and reported. The environment wins.

Once enabled, rubric is a requested oracle. The gate uses `--systemone` when supplied,
or adds it after THALAMUS\_URL/admin/v1/status reports a healthy System 1 member.
Missing THALAMUS\_URL, unreachable or invalid status, no healthy member, or exhausted
budget yields `rubric: not evaluated (<reason>)` and exit 3, never 0.

Turn opt-in off for structural-only gating. THALAMUS\_URL has no default.
`--systemone` without opt-in exits 3 and contacts nothing.

## Deterministic typecheck and tests oracles

Request them like the rubric: `CNS_ORACLES=structural,typecheck,tests,rubric`, or
`oracles = [...]` in `.cns/config.toml`. Each requested one runs the `cmd` of its table
from the repository root, without a shell, after `axon index` and before Soma:

```
[gate.typecheck]
cmd = ["vendor/bin/phpstan", "analyse", "--no-progress", "--error-format=json"]
diagnostics = "phpstan-json"   # optional: gate only on findings in changed files
globs = ["*.php"]          # optional: keep only matching paths
timeout_ms = 20000         # optional; always capped by the gate budget
[gate.tests]
cmd = ["vendor/bin/phpunit", "{impacted_tests}"]
require_integration_on_http_surface = true   # optional
```

* Placeholders: `{changed_files}` (the git diff Soma checks, plus untracked files),
  `{impacted_files}` (changed files plus 1-hop dependents from `axon delta --json`),
  `{impacted_tests}` (`impacted_tests` from `axon delta --json`). Each must be a whole
  element and becomes one argument per existing path, never split or shell-parsed.
  When every placeholder in `cmd` is empty, the oracle passes without running.
* `require_integration_on_http_surface`: red when the delta lists an HTTP surface in
  `untested_surfaces` or `mock_only_surfaces`.
* `require_integration_on_dispatched_surfaces` (default: the HTTP setting): the same for
  queued `job` and `event` boundaries, such as a job only a `Bus::fake()` test reaches.
* Command exit 0 is a pass; any other exit is red and its output is printed, except 126/127.
  A missing binary, 126/127, a timeout, a missing table or delta field is not evaluated.
* `diagnostics = "phpstan-json"`: the command analyses the whole configured project (baseline,
  stubs, rules and collectors intact) and only findings in `{changed_files}` are red. A
  PHPStan exit other than 0/1, no JSON report, or top-level PHPStan errors is not evaluated.
* When typecheck or tests is red, the rubric is skipped and System 1 is not called.
* `cns init` writes `.cns/config.toml` with a `[gate.typecheck]` table for the first
  manifest found (composer.json, tsconfig.json, Cargo.toml, pyproject.toml). For PHP it
  uses the analyzer the repository configures (phpstan.neon(.dist) or psalm.xml(.dist)).
  The table is commented out when that analyzer is not installed or not configured, and
  the oracles line stays commented: enable typecheck once it exits 0 on HEAD.

## Budget and exit status

CNS\_GATE\_BUDGET\_MS (default 30000) covers the typecheck and tests commands, the status
probe and the rubric together. A slow command or Thalamus reports
`not evaluated (... timed out ...)` and exits 3.

* 0: every requested oracle ran and passed.
* 1: structural, typecheck, tests or rubric red.
* 3: a requested oracle was not evaluated. This takes precedence over 1;
  the final line also names any evaluated red result.

## Vitals

Appends a vitals.v1 row (soma/gate) with concerns\_fired, oracles\_requested, oracles\_evaluated,
evaluated\_red and tree\_sha256 (the digest of the evaluated working tree outside .cns, .soma,
.axon and .engram); `cns status` prints both oracle lists and shows NOT\_EVALUATED (red) when
a requested oracle did not run, or FAILED when the gate exited 1 (a skipped rubric).

## Harness Stop hook

`cns gate --stop-hook --harness <claude|codex|agy|pi>` enforces the gate at the end of an agent
turn. It reads the harness's Stop payload on stdin, prints only that harness's decision JSON on
stdout and always exits 0.

* Allow at once, running nothing, outside Git, when the tree has no change outside .cns, .soma,
  .axon and .engram, or when the newest gate row for this tree digest passed with the same
  requested oracles. Allow when there is no Soma policy (no docs/soma or no authored concern).
* Otherwise run `cns gate` (axon index first, so shell edits are indexed). A pass allows; any
  other exit blocks the stop and returns the gate's report for repair: Claude and Codex
  `{"decision":"block"}`, Antigravity `{"decision":"continue"}`, Pi a continuation message.
* At most CNS\_STOP\_GATE\_MAX\_RETRIES (default 3) blocks per stop sequence and session
  (.cns/stop-gate-state.json). The next stop is released with a warning and a soma/stop-gate
  row carrying stop\_gate\_exhausted: true, so an unfixable red can never loop forever.

## Fig. 6.3.1   What the gate runs

1. Refresh  
   Axon if indexed
2. Run  
   Typecheck, tests if requested
3. Verify  
   Soma maps
4. Check  
   Structural concerns
5. Report  
   Requested oracles

## Table 6.3.1   Exit status & oracles

| Exit | Meaning |
| --- | --- |
| 0 | Every requested oracle ran and passed |
| 1 | Structural, typecheck, tests or rubric failure |
| 3 | Requested oracle not evaluated; takes precedence over 1 |

## Requested oracles

| Oracle | Default | When unavailable |
| --- | --- | --- |
| Structural | Requested | Exit 3; never a pass |
| Typecheck, tests | Disabled; configured in `.cns/config.toml` | Exit 3 when requested but not evaluated |
| System 1 rubric | Disabled; explicit opt-in required | Exit 3 when requested but not evaluated |

## Where the result goes

The gate appends a `soma/gate` record to `.cns/vitals.jsonl`. Run `cns status` to inspect requested and evaluated oracles. A hook success does not replace this closeout verdict.

See the [authoritative rubric and gate contract](https://usecns.dev/contract-rubric/) and the [vitals record](https://usecns.dev/contract-vitals/). Structural is the default; live inference requires opt-in.