# 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 ([IERR.1.7]). An assert is allowed only in a
  build that defines `DS_DEBUG` ([IRET.28.15]).

  This binds every object libds compiles and the `libprotobuf-c` archive
  shipped beside it, which is built with its asserts compiled out. It does
  not bind the RE2 and Abseil objects folded into a libds archive; libds's
  seam over them reports what they raise as a C++ exception. One path in a
  libds object traps: the CORE-MATH branch of `pow` and `atan2` that upstream
  holds unreachable, where the routine has no correctly rounded value to
  return ([LEXP.3.9.2]).

*ratified*  —  *see also:* [IERR.1.7](#IERR-1-7), [IRET.28.15](#IRET-28-15), [LEXP.3.9.2](#LEXP-3-9-2)

(IERR-1-7)=
- [IERR.1.7] **What libds does on finding its own invariant broken depends on
  the state it is left with.** Where libds detects the violation before changing
  anything, it must refuse the operation with `DS_ERR_INTERNAL`, and the engine
  goes on: a new pinned intern entry after the pinned set has frozen
  ([IRET.1.4]). Where the violation means engine state cannot be trusted,
  the engine must raise an `EngineError` of fault `ENGINE_FAULT_INTERNAL`
  (`proto/engine_fault.proto#EngineFault`) and halt the tier ([LERR.1.4.7]): an
  intern reference count released past zero. During `ds_engine_free` it must
  finish releasing what it holds and raises nothing ([NCBK.2.9]).

  The disposition follows the state and not the code: a `DS_ERR_INTERNAL` that
  a callback returns halts nothing ([NCBK.1.5]).

*ratified*  —  *see also:* [IRET.1.4](#IRET-1-4), [LERR.1.4.7](#LERR-1-4-7), [NCBK.1.5](#NCBK-1-5), [NCBK.2.9](#NCBK-2-9)

(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] The status codes below are listed by the groups of [NERR.3.1].

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

(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 [IERR.2.3]

**Reserved, never reused: -6**, with -105
([IERR.2.4]) and -603. 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.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 [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 [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-7)=
### Storage / call errors [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 [IERR.2.8a]

The DSC container's codes, and two reserved blocks. The AOT code loader's codes
are [IERR.2.8b]'s.

**DSC container.** 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.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 DSC container's codes ([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 [IERR.2.8b]

The ahead-of-time `CompiledCode` loader's codes ([RAOT.9]), -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.

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

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

(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.** An internal
invariant violation is reported as [IERR.1.7] states, so
integrators can at least recover the process.

---

*see also:* [IERR.1.7](#IERR-1-7)

