# libds error model

Error conventions, status-code taxonomy and
detail-string lifetime rules for libds. Takes the
`ds_result_t` commitment from
[IARC.1.12] and
the struct shape from
[IDAT.2] as fixed.

## What this is not

- Not a rationale for the `ds_result_t` shape — that
  lives in [IARC] and [IDAT].
- Not a wire-error spec. libds has no wire format
  ([IARC.2.3]); error codes are C-level only. If an
  integrator bridges errors across processes, they
  pick the encoding.
- Not a log-format spec. Integrators log whatever
  they want; libds only surfaces structured errors.

---

(IERR-1)=
## Error-model commitments [IERR.1]

(IERR-1-1)=
[IERR.1.1] Restated so this doc reads standalone.

(IERR-1-3)=
- [IERR.1.3] **Slim 16-byte struct.** `{ status, detail }`,
  and nothing else: no source location travels with a
  status ([IARC.1.12]).

*see also:* [IARC.1.12](#IARC-1-12)

(IERR-1-4)=
- [IERR.1.4] **Status is a signed int.** `DS_OK == 0`;
  negative values are errors; positive values are
  unused ([NERR.1.3]). Never use sign-checks; always
  compare with `DS_OK` or specific codes.

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

(IERR-1-5)=
- [IERR.1.5] **libds never `abort()`s, never `exit()`s,
  never `longjmp()`s.** All failures surface through
  `ds_result_t`, including OOM, including internal
  invariant violations. Asserts are allowed in debug
  builds for bug-catching; release builds return
  `DS_ERR_INTERNAL`.

(IERR-1-6)=
- [IERR.1.6] **Detail is advisory, status is programmatic.**
  Programs branch on `status`. Detail exists for
  logs and human reading; it may change between
  releases (or even between calls for the same
  error) without ABI impact.

---

(IERR-2)=
## ds_status_t taxonomy [IERR.2]

(IERR-2-1)=
[IERR.2.1] Status codes are grouped by originating concern.
The number ranges below are load-bearing: each
decade-range is a category, and new codes within a
range must fit its theme.

*checks:* `alloy×2`

(IERR-2-2)=
### Success [IERR.2.2]

(IERR-2-2-1)=
[IERR.2.2.1] Only one success code. Functions that have
multiple successful outcomes should encode them in
return values (by reference / by struct), not in
the status.

(IERR-2-3)=
### Generic errors (-1 .. -99) [IERR.2.3]

**Reserved, never reused: -6**, with -105
([IERR.2.4]) and -603 (in [IERR.2.8.1]'s
native-Rete range). A schema reaches an engine only from its one artefact
([NFCT.1.1]), so no public call or load registers a duplicate or an invalid
slot marker, and a clone copies what the load accepted ([IARC.11.1]). Each
code has no public producer ([IERR.2.8a]).

*see also:* [IARC.11.1](#IARC-11-1), [IERR.2.4](#IERR-2-4), [IERR.2.8.1](#IERR-2-8-1), [IERR.2.8a](#IERR-2-8a), [NFCT.1.1](#NFCT-1-1)

(IERR-2-3-1)=
[IERR.2.3.1] These apply anywhere. `DS_ERR_INVAL` is the
"caller passed something wrong" bucket — NULL
where non-NULL required, out-of-range value.
A handle another engine handed out, or a fact
handle whose fact is not live, is not in it: passing one is
undefined behaviour, and no status reports it
([IDAT.1.6]). `DS_ERR_STATE` is the
"operation isn't legal right now" bucket — pump
called during destroy, a fact asserted before the
`.dsc` is loaded, etc.

*ratified*  —  *see also:* [IDAT.1.6](#IDAT-1-6)

(IERR-2-3-2)=
[IERR.2.3.2] `DS_ERR_INTERNAL` must never fire in a correct
build. If it does, log the detail and file a bug.
Assert-equivalent in debug builds.

(IERR-2-3-3)=
[IERR.2.3.3] `DS_ERR_ENGINE_HALTED` is the one generic code that is not
retryable. `DS_ERR_STATE` means "not legal right now"; this means "never again
on this engine" — an `EngineError` halted its tier ([NCBK.2.9]), or a load
failed ([RDSC.9.8]), during this call or an earlier one. The caller **should** stop calling that engine, read the
halt's cause and the load-time provenance if it needs them (the calls
[NCBK.2.9] leaves valid), and free it.

The halt is a whole-engine condition whose causes span subsystems — an
artefact whose values contradict its own types, memory exhaustion with no
declared cap, a `pump_wcet_ns` overrun, a failed load — so the code names none
of them; the cause is the `EngineError`'s, or the failed load's own status.

*ratified*  —  *see also:* [NCBK.2.9](#NCBK-2-9), [RDSC.9.8](#RDSC-9-8)

(IERR-2-4)=
### Input errors (-100 .. -199) [IERR.2.4]

**Reserved, never reused: -102 and -105 to -108.** -105 for the reason
[IERR.2.3] gives. -102, -106, -107 and -108 have no producer ([IERR.2.8a]).

*see also:* [IERR.2.3](#IERR-2-3), [IERR.2.8a](#IERR-2-8a)

(IERR-2-4-1)=
[IERR.2.4.1] These codes refuse input a caller handed
libds. No source location travels with them ([IARC.1.12]).

*see also:* [IARC.1.12](#IARC-1-12)

(IERR-2-5)=
### Runtime errors (-200 .. -299) [IERR.2.5]

**Reserved, never reused: -201 and -203.** Neither has a producer
([IERR.2.8a]).

*see also:* [IERR.2.8a](#IERR-2-8a), [LCST.4.5.2](#LCST-4-5-2), [LEXP.7.12](#LEXP-7-12), [LTIM.1.1.5](#LTIM-1-1-5), [LUNI.4.6.1](#LUNI-4-6-1)

(IERR-2-6)=
### Reserved range (-300 .. -399) [IERR.2.6]

(IERR-2-6-1)=
[IERR.2.6.1] **Code range reserved.** Codes `-300..-399` are
permanently reserved and never reused ([STYL.2.8.7]). libds has
no wire and no peer layer ([IARC.2.3]), so no code in the range
is produced or defined, and `libds.h` declares none.

*see also:* [IARC.2.3](#IARC-2-3)

(IERR-2-7)=
### Storage / call errors (-400 .. -499) [IERR.2.7]

(IERR-2-7-1)=
[IERR.2.7.1] These come from the integrator's typed callback
returning an error code; libds propagates verbatim.
Storage backends are integrator territory ([IARC.2.3]);
network-layer failures live inside the integrator's
callback and are translated to one of these codes if
they want libds's storage-error path, or to a custom
`DS_ERR_*` code the integrator picks.

*see also:* [IARC.2.3](#IARC-2-3)

(IERR-2-7-2)=
[IERR.2.7.2] Note that `DS_ERR_STORAGE_NOT_FOUND` is distinct
from `DS_ERR_NOT_FOUND` — it signals the storage
adapter answered "no such key," not "no such adapter."
Callers that don't care about the distinction can
compare against either.

(IERR-2-8a)=
### DSC container load errors (-500 .. -549) [IERR.2.8a]

The DSC container's codes, and two reserved blocks. AOT code has its own band
below ([IERR.2.8b]); the two bands together are the artefact-load category,
-500 .. -599 ([NERR.3.1]).

**DSC container (-500 .. -504, -506, -507, -514).** Returned by the DSC
loader; the detail names the failing atom, the byte
offset and the violated RDSC rule ([NERR.8.1],
[RDSC.11.1]). -514, `DS_ERR_DSC_CAPABILITY_ABSENT`, names
the capability the build lacks rather than an atom
([RDSC.16.2.7]).

**Reserved, never reused: -505.**
Every multiplicity is the compiler's guarantee and a runtime
checks for no second atom ([RDSC.7.2]),
so nothing returns it. A `node_id` that is 0 or held
twice is `DS_ERR_RETP_INVALID` ([RDSC.11.1]).

**Reserved, never reused: -508 .. -513 and -520 .. -527.**
Nothing returns them. The header declares no name for them, and no value
is reissued ([STYL.2.8.7]).

A malformed builtin call is still reported. Arity is a
**compile-time** guarantee the runtime does not re-check, and
a call naming an op with no registered handler is
`DS_ERR_FN_NOT_FOUND` ([RDSC.4a.9.3]).

*see also:* [IERR.2.8b](#IERR-2-8b), [IRET.25](#IRET-25), [NERR.3.1](#NERR-3-1), [NERR.8.1](#NERR-8-1), [RDSC.11.1](#RDSC-11-1), [RDSC.16.2.7](#RDSC-16-2-7), [RDSC.4a.9.3](#RDSC-4a-9-3), [RDSC.7.2](#RDSC-7-2)

(IERR-2-8a-1)=
[IERR.2.8a.1] The codes of this band ([IERR.2.8a]) are not
recoverable: the DSC load fails and the engine is
unusable. A loader that skips a violating atom and
reports success does not satisfy this clause.

*see also:* [IERR.2.8a](#IERR-2-8a)

(IERR-2-8b)=
### AOT code load errors (-550 .. -599) [IERR.2.8b]

The band is the ahead-of-time `CompiledCode` loader's ([RAOT.9]). Its codes,
-550 to -553, are reserved and never reused: in this edition libds skips every
`CompiledCode` atom and decodes none ([RDSC.4c.3]), so nothing returns them.
The band stays claimed for that loader, and a code it needs when compiled code
binds takes a new number in it.

*see also:* `RAOT.9`, [RDSC.4c.3](#RDSC-4c-3)

(IERR-2-8)=
### Reserved ranges [IERR.2.8]

(IERR-2-8-1)=
[IERR.2.8.1] Claimed beyond `-599`:

| Range | Category |
|---|---|
| `-600..-610` | native Rete loader and realtime |
| `-620..-625` | series-backend ABI |
| `-700..-704` | dialect / artefact binding |
| `-710..-714` | realtime contract |
| `-720..-730` | event trace |

Gaps inside a claimed decade are headroom
for that decade's families, not free space for a new one.

Registering an allocation before the code exists is as much
of this clause's job as recording one that already exists.

The reservation is also why this table is read when a
`-6xx` / `-7xx` code is allocated and not only when one is
defined. This clause is named authoritative beyond `-599`
([NERR.3.1]) while the register's **gated** rendering is
`[NERR.13]`, which the error-code gate reconciles against
`libds.h` by NAME. A range row carries no name, so it sits
outside that gate's population entirely: the two
renderings here and in [NERR.3.1] are kept current by
being read, not by being checked.

Compiler diagnostics are **not** in this space and claim
no range here ([RTCT.9.1]). `DS_ERR_` is
the runtime `ds_status_t` prefix; a diagnostic wearing it
is a naming defect, not a band that needs allocating.

`-800` through `-9999` remain reserved for future
categories (debug protocol, observability, TBD). A new
category claims its range by an explicit amendment here, and
this clause is the register that makes the claim real.

*checks:* `alloy×2`  —  *see also:* [NERR.13](#NERR-13), [NERR.3.1](#NERR-3-1), [RTCT.9.1](#RTCT-9-1)

(IERR-2-8-2)=
[IERR.2.8.2] `-10000` and below are reserved for implementation-
defined codes an integrator may use for their own
return values when composing libds calls with their
own infrastructure. libds itself never returns
codes in this range.

(IERR-3)=
## ds_result_t semantics [IERR.3]

(IERR-3-2)=
[IERR.3.2] Success shape: `{ DS_OK, NULL }`. Libraries that
attach advisory detail on success should resist the
urge — if the caller wanted information, they can
get it via return values or out-params.

*ratified*

(IERR-3-3)=
[IERR.3.3] Error shape: `{ <negative code>, <non-null detail> }`
for every code but `DS_ERR_OOM`, whose detail may be NULL
because the detail-string lifetime rules [IERR.4] cannot be
upheld when out of memory ([IERR.4.1.7]). A successful call's
detail may be NULL.

*ratified*  —  *see also:* [IERR.4](#IERR-4), [IERR.4.1.7](#IERR-4-1-7)

(IERR-3-4)=
[IERR.3.4] Callers must treat the struct as a unit: either
succeed-and-proceed, or propagate the error upward.
Checking `.status` and discarding the error is a
silent-swallow bug (see [IERR.7.4]).

---

*see also:* [IERR.7.4](#IERR-7-4)

(IERR-4)=
## Detail-string lifetime and format [IERR.4]

(IERR-4-1)=
### Lifetime contract [IERR.4.1]

(IERR-4-1-1)=
[IERR.4.1.1] `ds_result_t.detail` is a `const char *` pointer.
Its lifetime is **at least until the next libds
call on the same engine**. Callers that need the
string beyond that point must copy it.

A call that acts on no engine ([IARC.1.11]), and a
`ds_engine_new` that fails and so leaves none, has no
engine to hold a built string: libds **must** return a
static detail from it, valid for the life of the
process.

*ratified*  —  *see also:* [IARC.1.11](#IARC-1-11)

(IERR-4-1-2)=
[IERR.4.1.2] Three physical backings are permitted; callers
do not distinguish them:

(IERR-4-1-3)=
- [IERR.4.1.3] **Static literal.** Most common. Process-lifetime
  pointer; safe to retain indefinitely, but callers
  must not rely on this without documentation.

(IERR-4-1-4)=
- [IERR.4.1.4] **Engine-bound string** (interned in the engine's
  Rete intern table per [IDAT.4.7]). Engine-lifetime
  stable; invalidated on engine destruction.

*see also:* [IDAT.4.7](#IDAT-4-7)

(IERR-4-1-5)=
- [IERR.4.1.5] **Per-engine scratch buffer.** A small char buffer
  owned by the engine, overwritten on each error.
  Lifetime is until the next libds call on that
  engine. Used for error detail that interpolates
  dynamic content (names, counts, paths).

(IERR-4-1-6)=
[IERR.4.1.6] Implementations may pick any of the three backings
for a given error; the call-site contract is the
same.

(IERR-4-1-7)=
[IERR.4.1.7] OOM is the one exception: `DS_ERR_OOM` allows a
NULL detail because even the static-literal path
isn't safe when heap state is unknown (the library
may not have initialised its static error-string
table yet). Callers must handle NULL detail from
`DS_ERR_OOM`.

(IERR-4-2)=
### Format convention [IERR.4.2]

(IERR-4-2-1)=
[IERR.4.2.1] Detail strings are English, human-readable,
advisory. Machine-parsing them is a misuse; switch
on `status` instead.

(IERR-4-2-2)=
[IERR.4.2.2] Style:

(IERR-4-2-3)=
- [IERR.4.2.3] Lowercase initial letter.

(IERR-4-2-4)=
- [IERR.4.2.4] No trailing period.

(IERR-4-2-5)=
- [IERR.4.2.5] Short. Under 80 characters where possible; under
  200 characters always.

(IERR-4-2-6)=
- [IERR.4.2.6] Dynamic content separated by `": "` — e.g.
  `"unknown schema: Hass"`, `"slot type mismatch: expected Float, got Int"`,
  `"file not found: /path/to/rules.clp"`.

(IERR-4-2-7)=
- [IERR.4.2.7] Imperative / descriptive, not diagnostic.
  `"engine is destroying"` not `"you cannot call pump on a destroying engine, please wait until it is fully torn down"`.

(IERR-4-2-8)=
- [IERR.4.2.8] No newlines, no ANSI escapes, no embedded control
  characters. Integrators decide how to present.

(IERR-4-2-9)=
[IERR.4.2.9] All strings are English. `status` is the
machine-readable value; `detail` is human-facing prose and is
not a stable key.

(IERR-6)=
## Errors never serialize [IERR.6]

(IERR-6-1)=
[IERR.6.1] **Errors never serialize.** `ds_status_t` codes are
C-level only; libds defines no wire encoding for them and
no boundary they cross. Integrators that bridge libds
across processes pick their own encoding for the values
that travel — the `status` int and `detail` string have no
on-wire shape libds mandates.

---

(IERR-7)=
## Error-propagation patterns [IERR.7]

(IERR-7-1)=
### The standard pattern [IERR.7.1]

(IERR-7-1-1)=
[IERR.7.1.1] Most functions do exactly this. Check the result,
propagate on error, continue on success, return
`DS_OK_R` on completion.

(IERR-7-2)=
### Context enrichment [IERR.7.2]

(IERR-7-2-1)=
[IERR.7.2.1] A public-API function may replace a generic inner error
with a more specific outer one when the context adds clarity.

(IERR-7-2-2)=
[IERR.7.2.2] Use sparingly. The default is propagate-unchanged;
rewrite only when the outer name is meaningfully
more specific than the inner, or when the inner's
detail would be misleading at the outer boundary.

(IERR-7-2-3)=
[IERR.7.2.3] Never enrich by concatenating detail strings or
mutating the inner result. Return a fresh result.

(IERR-7-3)=
### Never swallow [IERR.7.3]

(IERR-7-3-1)=
[IERR.7.3.1] Checking `.status` and then discarding the error
without propagating or logging is a bug. libds
does not do this internally; reviews reject it.

(IERR-7-3-2)=
[IERR.7.3.2] The only acceptable "ignore" is when the caller
provably cannot fail (and then the return type
should signal that by not returning a result). If
you call a `ds_result_t`-returning function, you
handle the error.

(IERR-7-4)=
### Logging vs propagating [IERR.7.4]

(IERR-7-4-1)=
[IERR.7.4.1] libds itself does not log. Errors surface via
`ds_result_t` to the caller; the integrator decides
what to do (log, ignore, propagate to their own
telemetry).

(IERR-7-4-2)=
[IERR.7.4.2] Exception: the recorder records error events as
part of the event stream when an error is
semantically observable (e.g. a rule-fire failure).
This is not "logging" — it's an observed event,
serialised like any other.

---

(IERR-8)=
## What doesn't happen [IERR.8]

(IERR-8-1)=
[IERR.8.1] **No exceptions.** libds is C; errors travel as
return values only.

(IERR-8-2)=
[IERR.8.2] **No `longjmp` / `setjmp` error handling.** Stack
unwinding via `longjmp` is banned — it obscures
error paths, breaks RAII-free C ownership, and
composes badly with async callbacks.

(IERR-8-3)=
[IERR.8.3] **No retry logic inside libds.** Retries are a
caller concern. If a typed callback returns an error
status, libds surfaces it through the standard
`ds_result_t` path; the caller (or the integrator's
own machinery wrapping the callback) decides whether
to retry, escalate, or drop.

(IERR-8-4)=
[IERR.8.4] **No v1 i18n.** Detail strings are English.
Switching on `status`, not `detail`, is the
portable approach.

(IERR-8-5)=
[IERR.8.5] **No structured error payload beyond
`ds_result_t`.** An error category that
needs richer context (e.g. "here are the five rules that
conflict") is served by a separate query API; `ds_result_t` is
not extended for it.

(IERR-8-6)=
[IERR.8.6] **No errno-style thread-local "last error."**
libds has no thread-local state per [IARC.1.10].
Every error is carried in its own `ds_result_t`.

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

(IERR-8-7)=
[IERR.8.7] **No `abort()` on OOM.** `DS_ERR_OOM` surfaces
normally and callers propagate.

(IERR-8-8)=
[IERR.8.8] **No panic / bugcheck path.** Even internal
invariant violations return `DS_ERR_INTERNAL` with
detail, so integrators can at least recover the
process.
