# Getting started: a minimal libds integration

A walk-through of an integrator's first working program against `libds`'s
public API. This doc exercises the calls every integration uses: engine lifecycle, schema registration, fact
assertion, pump, and callback registration, plus the `.ds` → `.dsc` path for
loading compiled rules.

## What this is not

- Not a full API reference. Function signatures live in
  `runtime/include/libds.h` with
  docstrings; surface depth is in [NLFC] /
  [NCBK] / [NFCT] /
  [NERR].
- Not production guidance. The minimal example skips error-handling discipline,
  callback rigour, and operational concerns; it shows the shape.
- Not a rule-authoring tutorial; rule syntax and semantics are the language
  spec's domain.

---

(NGST-1)=
## Prerequisites [NGST.1]

(NGST-1-1)=
[NGST.1.1] libds is distributed as a per-platform native tarball. An
integrator needs three things from it: `include/libds.h`, the public header;
`lib/libds.a`, the runtime; and `lib/libprotobuf-c.a`, which loading a `.dsc`
requires. The headers under `include/internal/` are not part of the public
contract.

(NGST-1-2)=
[NGST.1.2] **Building from source instead of a tarball.** The `Makefile` in
`runtime/src/libds/`, generated and committed with the tree, builds
`runtime/src/libds/libds.a` there: the same archive the tarball ships as
`lib/libds.a`. Run it from that directory, with the tools it invokes on the
path (a C compiler, a C++17 compiler, `protoc-c`) and RE2's headers available.
It takes its version from the tree's `VERSION` file, so the archive loads
artefacts compiled by the same release ([LDET.1.10]).

*see also:* [LDET.1.10](#LDET-1-10)

(NGST-1-3)=
[NGST.1.3] The `dsc` compiler ([NGST.4]). `dsc` turns `.ds` source into the
`.dsc` artefact that `ds_engine_load_dsc` consumes. Every schema reaches an
engine from such an artefact ([NFCT.1.1]), so an integrator needs `dsc`, or an
artefact `dsc` built, even for a program that declares schemas and no rules.

*see also:* [NFCT.1.1](#NFCT-1-1), [NGST.4](#NGST-4)

(NGST-1-4)=
[NGST.1.4] **The debug pack, and the wasm32 tarball.** Beside each libds tarball a
release ships a debug pack, named as the tarball is with `-debug` before
`.tar.gz`. It comes from the same build and differs only in this: its
`lib/libds.a`, and its `bin/dsc` where the tarball has one, keep their debug
information. The tarball's are stripped of it and keep their symbols, so both
link and run alike. An integrator must link against the one or the other, not
both.

The wasm32 tarball is named `ds-libds-<version>-emscripten-wasm32-regex.tar.gz`.
It carries no `bin/dsc` and no standard library, since a wasm archive is linked
into a module and not run: the compiler ([NGST.1.3]) comes from a native tarball.

---

*see also:* [NGST.1.3](#NGST-1-3)

(NGST-2)=
## The minimal program [NGST.2]

(NGST-2-1)=
[NGST.2.1] The shortest complete integration there is: create an engine,
arrange to observe it, give it a schema, put a fact in, run it, take the fact
out, shut it down. Every later section of this guide adds to that skeleton
rather than replacing it.

One piece of the ordering is not stylistic. Callbacks are registered **before**
any work happens, because an unregistered callback drops silently
([NCBK.1.8]) -- register them after the first assert and that assert's events
are simply gone, with nothing to indicate they existed.

*see also:* [NCBK.1.8](#NCBK-1-8), [NGST.4](#NGST-4)

(NGST-2-2)=
[NGST.2.2] Every public function returns `ds_result_t` ([NERR]).
The minimal program checks the calls that can fail in normal operation and lets
the void-returning setters ride; real code checks all of them.

---

(NGST-3)=
## Compile + run [NGST.3]

(NGST-3-1)=
[NGST.3.1] An integrator compiles against `include/` and links
`lib/libds.a` and `lib/libprotobuf-c.a`, plus the platform libraries the
tarball names for its row. The tarball names them in the `[link]` section of
its `PROVENANCE.txt`, as the link line its row was tested with. They differ by
row, so no fixed list binds: a row may need `-lre2`, and a row may need no
thread library. The link must use a C++ driver: `libds.a` carries C++ objects
(the RE2 seam), and a C driver leaves their runtime unresolved.

(NGST-3-3)=
[NGST.3.3] Each event's metric (`ds.recorder.events_emitted_total`) reaches
the metric callback before the event reaches the stream callback.

---

(NGST-4)=
## Loading compiled rules [NGST.4]

(NGST-4-1)=
[NGST.4.1] Schemas and rules reach the engine in one compiled artefact
([NFCT.1.1]): both are written in `.ds` and compiled to a `.dsc` by
`dsc compile` ([RCLI.1.1]). An artefact whose program declares schemas and no
rules lets an integrator assert and observe facts; rules fire only where the
program declares them. The artefact
carries the Rete network with no flag. A program whose `use` names a dialect
defined in a file needs that file passed with `-d` ([NDLC.4.3]).

*see also:* [NDLC.4.3](#NDLC-4-3), [NFCT.1.1](#NFCT-1-1), [RCLI.1.1](#RCLI-1-1)

(NGST-4-2)=
[NGST.4.2] At startup, read the `.dsc` bytes and hand them to
`ds_engine_load_dsc` **before** asserting facts. A load registers the schemas
*and* rules the source declared, and it is the only way a schema reaches the
engine ([NFCT.1.1]).

A load that fails once it has begun reading the artefact spends the engine
([RDSC.9.8]), and [NERR.8.2] lists what it can return and which failures a
corrected artefact answers.

*see also:* [NERR.8.2](#NERR-8-2), [NFCT.1.1](#NFCT-1-1), [RDSC.9.8](#RDSC-9-8)

(NGST-4-3)=
[NGST.4.3] Embedded targets that cannot do file I/O compile the artefact
straight into the binary. `dsc compile-project <dir>` writes, beside each
module's `.dsc`, a `<basename>_dsc.h` that defines two constants: `<SYM>_DSC`,
a `static const uint8_t` array holding the artefact's bytes, and
`<SYM>_DSC_LEN`, a `static const size_t` holding its length ([RCLI.1.4]).
`<SYM>` is the module's basename upper-cased, with every character outside
`A`–`Z` and `0`–`9` replaced by `_` (`heat.zone` → `HEAT_ZONE`). The integrator
passes the two to `ds_engine_load_dsc` as it would bytes read from a file.

---

*see also:* [RCLI.1.4](#RCLI-1-4)

(NGST-5)=
## What the minimal example omits [NGST.5]

(NGST-5-1)=
[NGST.5.1] Real integrations grow concerns the minimal program sidesteps:

- **Rule firing.** Needs a loaded `.dsc` ([NGST.4]).
- **Action callbacks.** `call` (storing facts included) and `cache_*`
  route through callbacks you register — [NCBK]. The async
  request/response model behind `call` is its own subject.
- **Mock clock.** Deterministic tests of TTL / cooldown / `within` set
  `ds_engine_clock_set_mode(e, DS_CLOCK_MOCK)` — [NLFC.5], [NTST].
- **TTL facts and per-emit retention.** [NFCT].
- **Multi-engine deployments.** Each engine is fully independent —
  [NLFC.4].
- **Series, realtime, determinism.** Storage / series backends, the realtime
  contract, and replay determinism each have their own doc.

---

*see also:* [NGST.4](#NGST-4), [NLFC.4](#NLFC-4), [NLFC.5](#NLFC-5)

