# Digital twin

Read a plant, start a run from its state, calibrate and forecast.

## naga bind

```text
usage: naga bind [-h] [--allow-network] [--allow-private-network]
                 [--allow-host HOST] [--connections FILE] [--write WRITE]
                 model

Resolve a model's data bindings and show what each one read.

positional arguments:
  model                 path to the model JSON, or a bundled model's name
                        (naga examples)

options:
  -h, --help            show this help message and exit
  --allow-network       permit sql / json_url data sources (refused by
                        default: opening a model must not make outbound
                        connections on its own)
  --allow-private-network
                        also permit private/intranet addresses (refused even
                        with --allow-network)
  --allow-host HOST     restrict network sources to this hostname; may repeat
  --connections FILE    the connections a live source (rest, mqtt, opcua) may
                        name: a JSON file of yours, never the model's; without
                        it every live source is refused
  --write WRITE         write the resolved model here
```

## naga watch

```text
usage: naga watch [-h] --connections FILE [--source NAME] [--for SECONDS]
                  [--record FILE] [--check]
                  model

Print what a model's live data sources (rest, mqtt, opcua) send, as it
arrives, with a summary per source. Read-only: nothing is sent to a plant.

positional arguments:
  model               path to the model JSON, or a bundled model's name (naga
                      examples)

options:
  -h, --help          show this help message and exit
  --connections FILE  your connections file: where each connection a source
                      names may connect, and with which credential
  --source NAME       watch only this source; may repeat (default: every live
                      source)
  --for SECONDS       how long to watch (default 30; 0: until Ctrl+C)
  --record FILE       also write every event as one JSON object per line, with
                      a {"lost": n} or {"gap": true} line where the log has a
                      hole
  --check             print each connection the model uses, connect once and
                      stop
```

## naga state

```text
usage: naga state [-h] {take,check} ...

A model's state as a document: taken from a run, and checked before a run
starts from it (naga run MODEL --state FILE). Read-only.

positional arguments:
  {take,check}
    take        run the model to a time and write its state
    check       say whether a run of this model may start from this state;
                exit 1 with the reasons when not

options:
  -h, --help    show this help message and exit
```

## naga plant

```text
usage: naga plant [-h] {check,init,mock,replay,watch} ...

A plant as a timeline: a plant mapping (*.plantmap.json) says what each key a
plant sends asserts about a model, and the views of a run then show the plant.
Read-only: nothing is sent to a plant.

positional arguments:
  {check,init,mock,replay,watch}
    check               say whether a mapping may be used with a model; exit 1
                        with the reasons when not
    init                write a starter mapping that names every block,
                        resource and level of the model by its own name
    mock                run the model and write the plant log that run would
                        have left, through the mapping read backwards
    replay              map a recorded log (naga watch --record) into an
                        observed timeline
    watch               show a model's live sources as an observed timeline,
                        as they send

options:
  -h, --help            show this help message and exit
```

## naga twin

```text
usage: naga twin [-h] {once,run,status,score,reproduce} ...

An operational twin of a plant: monitor it through a plant mapping, forecast
from its state of now, compare each forecast with what then happened, score
them, and write one result a monitor reads. With --candidates it compares your
candidate actions after each forecast and recommends. Read-only towards the
plant: nothing is sent to it; only the --out directory is written and, with
--outbox, the file you name there.

positional arguments:
  {once,run,status,score,reproduce}
    once                one cycle with its forecast, for the OS scheduler;
                        exit 1 when the result holds an error
    run                 keep monitoring, with a forecast on its own slower
                        cadence, until Ctrl+C
    status              print the latest result of a twin; exit 1 for an error
                        in it or no result
    score               compute a twin's stored scores again from a recorded
                        plant log; exit 1 for any difference. Nothing is
                        written
    reproduce           compute a twin's stored recommendations again from
                        their stored states and seeds; exit 1 for any
                        difference. Nothing is written

options:
  -h, --help            show this help message and exit
```

## naga approve

```text
usage: naga approve [-h] --outbox SPEC [ID]

Approve one recommendation of a twin's file outbox, as the editor's Approve
button does: it shows what the record would do, asks, and on y writes one
"taken" reply line to <outbox>.replies naming you. Nothing is sent to a plant;
`naga plc-write` writes an approved setpoint, as its own command.

positional arguments:
  ID             the record's id or the first characters of it (default: the
                 one record still waiting; with several, they are listed)

options:
  -h, --help     show this help message and exit
  --outbox SPEC  the twin's outbox, file:<path>
```

## naga trust

```text
usage: naga trust [-h] [--list] [--remove PREFIX] [MODEL]

Trust a model's item code on this computer. It shows each block of code that
is not trusted yet, where the model file says it comes from and its
fingerprint, and asks; only a typed y trusts it. Trusted code runs with your
permissions, unsandboxed, in your runs, experiments and assistant tool calls.
Any change to the code needs your yes again.

positional arguments:
  MODEL            the model file

options:
  -h, --help       show this help message and exit
  --list           list the item code trusted on this computer
  --remove PREFIX  stop trusting the code whose fingerprint starts with PREFIX
                   (12 or more hex characters)
```

## naga plc-write

```text
usage: naga plc-write [-h] --outbox SPEC --setpoints FILE --connections FILE
                      --audit FILE [--write] [--id ID]

Write the setpoints a person approved in a twin's outbox to a soft PLC on this
machine, over Modbus TCP. Dry-run unless --write: it then reads the PLC and
writes only the audit. Only a record with a "taken" reply that names who took
it, inside your setpoints file's range, step and interval, is written, and it
is read back. Every attempt is audited before and after.

options:
  -h, --help          show this help message and exit
  --outbox SPEC       the twin's outbox, file:<path>; replies are read from
                      <path>.replies beside it
  --setpoints FILE    your setpoints file (format 1, kind naga-setpoints): the
                      tags that may be written, each with its register, model
                      path, range, max_step and min_interval, and the
                      connection they are written to
  --connections FILE  your connections file: the setpoints file's connection,
                      kind modbus with "use": "write", on this machine
  --audit FILE        the audit, JSON lines appended: a line before and after
                      every attempt; also what the interval and "written
                      before" are read from
  --write             write; without it nothing is sent to the PLC but reads
  --id ID             only this record (its id or the first characters of it);
                      may repeat (default: every setpoint not yet written)
```

## naga calibrate

```text
usage: naga calibrate [-h] --factor FACTORS --target TARGETS [--budget BUDGET]
                      [--replications REPLICATIONS] [--run-length RUN_LENGTH]
                      [--warmup WARMUP] [--seed SEED] [--logic MODULE]
                      [--workers WORKERS] [--method {optimize,abc}]
                      [--accept ACCEPT] [--out OUT] [--report REPORT]
                      [--write-scenario WRITE_SCENARIO]
                      model

Tune model parameters until the model reproduces observed KPIs.

positional arguments:
  model                 path to the model JSON, or a bundled model's name
                        (naga examples)

options:
  -h, --help            show this help message and exit
  --factor FACTORS      a factor, as 'path=kind:low:high[:step]' or
                        'path=cat:a|b|c'. kind is float/int/cat. Several
                        comma-separated paths before the '=' are driven with
                        the same value. May repeat. Example:
                        'resources.Agents.capacity=int:3:6'
  --target TARGETS      an observed KPI, as '<kpi key>=<value>[:<tolerance>]'.
                        May repeat. Example: 'system/Avg Throughput
                        Time=8.4:0.5'
  --budget BUDGET       total replications the search may spend (default: 200)
  --replications REPLICATIONS
                        replications per candidate point (default: 5)
  --run-length RUN_LENGTH
                        default: the model's own 'simulation.time_limit'
  --warmup WARMUP       default: the model's own 'simulation.warmup'
  --seed SEED
  --logic MODULE        take LOGIC block classes from this importable module
                        first (a dotted name on PYTHONPATH), then from
                        nagasim.core.custom_logic
  --workers WORKERS     replications to run in parallel processes (default: 1)
  --method {optimize,abc}
                        optimize: search for the best point (default); abc:
                        rejection ABC, a posterior range per factor beside the
                        best point
  --accept ACCEPT       abc only: share of prior samples accepted (default:
                        0.05, at least 20)
  --out OUT             write the target table here (.xlsx or .csv)
  --report REPORT       write an HTML report here
  --write-scenario WRITE_SCENARIO
                        write the model back here with the tuned values as a
                        scenario
```

## naga forecast

```text
usage: naga forecast [-h] --horizon HORIZON [--replications REPLICATIONS]
                     [--event EVENTS] [--kpi KPIS] [--seed SEED]
                     [--workers WORKERS] [--no-bind] [--allow-network]
                     [--allow-private-network] [--allow-host HOST]
                     [--connections FILE] [--out OUT] [--report REPORT]
                     model

Forecast a short horizon from the model's current state, with prediction bands
and event probabilities. Schedule repeated runs with the OS scheduler (Task
Scheduler, cron); this command is the unit of work, not a daemon.

positional arguments:
  model                 path to the model JSON, or a bundled model's name
                        (naga examples)

options:
  -h, --help            show this help message and exit
  --horizon HORIZON     run length in base time units
  --replications REPLICATIONS
  --event EVENTS        an event to report the probability of, as '<kpi
                        key><op><value>' with op one of > >= < <=. May repeat.
                        Example: 'timeseries/system/WIP/max>8'
  --kpi KPIS            KPI key to report; may repeat (default: all)
  --seed SEED
  --workers WORKERS     replications to run in parallel processes (default: 1)
  --no-bind             skip the model's data bindings (they resolve by
                        default here)
  --allow-network       permit sql / json_url data sources (refused by
                        default: opening a model must not make outbound
                        connections on its own)
  --allow-private-network
                        also permit private/intranet addresses (refused even
                        with --allow-network)
  --allow-host HOST     restrict network sources to this hostname; may repeat
  --connections FILE    the connections a live source (rest, mqtt, opcua) may
                        name: a JSON file of yours, never the model's; without
                        it every live source is refused
  --out OUT             write the forecast tables here (.xlsx or .csv)
  --report REPORT       write an HTML report here
```
