Recording and atoms#
Recording#
recordTo#
recordTo(target: string | ((bytes: Uint8Array) => void)): void;
Records the engine’s fact stream to a path or a sink function.
A path’s file is created, or truncated if it exists, and each atom is written as it is framed. A sink function receives each atom as a Uint8Array copy. RFCS.3.6 gives the atom each event kind records as. A running recording takes each event before listeners do.
The stream’s header and schema announcements are written at the engine’s first stream event, or by recordTo where the engine has already had one.
Throws |
Condition |
|---|---|
|
a recording is running; the engine is closed, and a path’s file stays as it was |
|
|
the host |
a path the |
|
the sink throws while |
|
|
|
the recording’s buffer cannot be allocated; |
A sink that throws afterwards stops the recording, and stopRecording reports it. close from a sink is refused.
engine.recordTo((bytes) => console.log(bytes.length));
engine.pump();
engine.stopRecording();
(Source: bindings/js/types/bind.d.ts, bindings/js/lib/bind.js, bindings/js/src/ds.c)
FileSink#
export type FileSink = { write: (bytes: Uint8Array) => void; close: () => void; };
What a Host’s file returns for recordTo’s path.
close runs when the recording stops, and when the engine closes.
(Source: bindings/js/types/types.d.ts, bindings/js/lib/bind.js)
stopRecording#
stopRecording(): void;
Stops the recording, and throws the recorder’s first error as a DsError.
A sink’s exception is that error’s cause. With no recording running it does nothing. Engine.close ends a recording too, and reports no failure.
(Source: bindings/js/types/bind.d.ts, bindings/js/lib/bind.js, bindings/js/src/ds.c)
Replay#
replayStream#
replayStream(source: Uint8Array | Iterable<Uint8Array>, { maxAtomBytes }?: { maxAtomBytes?: number; }): ReplayCounts;
Replays a fact stream into the engine, and returns what it did.
Inputs are delivered, the rules re-fire, and each re-derived compare atom is checked against the record. What the engine needs is in Replay settings.
source is one Uint8Array, or an iterable of chunks with any boundaries. maxAtomBytes is the largest atom accepted, and 0, the default, selects libds’s default.
During a replay, listeners and a running recording hear no event, and neither the call callback nor writeThrough is called. Listeners and a running recording hear events again once the replay ends, unless it halted the engine (halt latch).
A replay that reaches the end of its recording with every compare clean returns its ReplayCounts, for a run that halted too. halted says whether the run halted. DS_ERR_ENGINE_HALTED from replayStream means the replay did not reproduce the run, or met an engine already halted.
While source is being iterated, a Fact of this engine that is not dead throws a DsError with DS_ERR_STATE from read, and when passed to assert or retract.
Throws |
Condition |
|---|---|
|
|
|
|
|
a replay is open on this engine |
|
a re-derived atom differs from the record, breaks a timetag’s pairing with an earlier atom, or has no recorded twin; a recorded compare atom is not re-derived; a |
|
the stream was written by another DS minor series |
|
the recording’s first event has a |
|
a |
|
the |
|
a |
|
a |
|
a malformed atom; a |
|
a |
|
an allocation fails |
|
|
DS_ERR_CONTRACT_BREACH for a FactAssert or a PumpBoundary does not end the replay, nor does DS_ERR_ENGINE_HALTED for a halt that the atom’s own assert, retract, delivery or pump raises, nor do DS_ERR_NAN_REJECTED and DS_ERR_SERIES_NO_BACKING for a PumpBoundary. Atoms applied before a failure stay applied.
(Source: bindings/js/types/bind.d.ts, bindings/js/lib/bind.js, bindings/js/src/ds.c, runtime/src/libds/rfcs/rfcs.h, runtime/src/libds/rfcs/codec.c, Spec: [NDET.8.2], [NCBK.2.9], [RBND.5.2], [RBND.2.1], [RFCS.7.3], [LDET.1.10], [LTIM.2.8.5], [RFCS.24.2], [RFCS.26.2], [RFCS.5.3], [RFCS.5.4], [RFCS.6.3], [RFCS.6.7], [NCBK.2.11])
ReplayCounts#
export type ReplayCounts = { asserts: bigint; retracts: bigint; clocks: bigint; ruleFires: bigint; calls: bigint; withdrawals: bigint; skipped: bigint; headerVerified: boolean; };
What a replay did.
Field |
Holds |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
whether a |
(Source: bindings/js/types/types.d.ts, runtime/src/libds/rfcs/codec.c)
External assert#
applyExternalAssert#
applyExternalAssert(body: Uint8Array): ExternalAssertApplied;
Applies one recorded ExternalAssert as the replayer does: one input, the recorded ttl_ns on the received fact, and no pump.
body is the atom’s body as framed, which Atom’s bodyOffset and bodyLength locate.
Throws |
Condition |
|---|---|
|
|
|
the delivery refuses, or asserts a number of facts other than the record lists |
|
a malformed body |
|
an engine that takes no input at the call |
(Source: bindings/js/types/bind.d.ts, bindings/js/lib/bind.js, Spec: [RFCS.24.2])
ExternalAssertApplied#
export type ExternalAssertApplied = { factIds: bigint[]; recordedIds: bigint[]; };
What applyExternalAssert asserted: factIds[i] is the engine’s id for the recorded recordedIds[i].
(Source: bindings/js/types/types.d.ts, Spec: [RFCS.24.3])
Atoms#
readAtoms#
readAtoms: (bytes: Uint8Array, options?: { incremental?: boolean; }) => AtomsRead;
Reads every atom of a trace or a .dsc. A member of the Api.
With incremental, a partial trailing atom stops the read with incomplete true. Otherwise it is an error with DS_ERR_PARSE. A value that is not a Uint8Array throws TypeError. The atoms’ layouts are in The fact stream.
const api = await ds.open();
const { atoms, error } = api.readAtoms(new Uint8Array(0));
console.log(atoms.length, error, api.messageName(1));
(Source: bindings/js/types/types.d.ts, bindings/js/lib/bind.js, bindings/js/src/ds.c, Spec: [RFCS.19.2])
messageName#
messageName: (field: number) => string | null;
Returns the proto message an atom’s field number names, or null for a number with no entry. A member of the Api.
A value that is not an integer throws TypeError, and one outside 0 to 2^32-1 throws RangeError.
(Source: bindings/js/types/types.d.ts, bindings/js/lib/bind.js)
AtomsRead#
export type AtomsRead = { atoms: Atom[]; end: number; incomplete: boolean; error: { offset: number; status: number; detail: string; field: number | null; message: string | null; } | null; };
What readAtoms read.
atoms holds those before any error. end is where reading stopped. error is null unless bytes stopped the read. error.field and error.message name the atom when reading stopped at its body, and both are null when it stopped at framing.
(Source: bindings/js/types/types.d.ts, bindings/js/src/ds.c)
Atom#
export type Atom = { offset: number; field: number; message: string | null; body: AtomBody | null; unknownFields: number[]; bodyOffset: number; bodyLength: number; };
One atom, named by its field number.
message and body are null for an atom this build has no entry for. unknownFields lists each field number in the body, at any depth, that its message does not name, in the order read. bodyOffset and bodyLength locate the body’s bytes in what was read.
(Source: bindings/js/types/types.d.ts, bindings/js/src/ds.c, Spec: [RFCS.16.1])
AtomBody#
export type AtomBody = { [field: string]: AtomField; };
An atom’s message, field by field: nested messages as objects, repeated fields as arrays.
(Source: bindings/js/types/types.d.ts, bindings/js/src/ds.c)
AtomField#
export type AtomField = AtomScalar | AtomEnum | AtomBody | AtomScalar[] | AtomEnum[] | AtomBody[];
One field of an atom’s message. A repeated field is an array of one kind.
(Source: bindings/js/types/types.d.ts)
AtomScalar#
export type AtomScalar = bigint | number | boolean | string | Uint8Array;
A scalar field. A 64-bit integer is a bigint, and bytes are a plain Uint8Array copy.
(Source: bindings/js/types/types.d.ts, bindings/js/lib/bind.js, bindings/js/src/ds.c)
Fact bodies#
decodeFactBody#
decodeFactBody(schema: string, body: Uint8Array): Record<string, SlotValue>;
Types a FactAssert or FactEmit atom’s fact bytes by this engine’s schema schema, with each SlotValue as a Fact’s read gives it.
A name the engine has no schema by throws a DsError with DS_ERR_SCHEMA_UNKNOWN, and a schema with a fact-reference slot one with DS_ERR_INVAL. A malformed body, a String field holding U+0000 included, or a String or Json field’s text that is not well-formed UTF-8, throws one with DS_ERR_PARSE. A Json field’s text that does not parse throws one with DS_ERR_PARSE, and one over a tree limit with DS_ERR_JSON_LIMIT. A non-nullable Json slot’s field that is absent, or whose document is null at its root, throws one with DS_ERR_PARSE. An allocation that fails throws one with DS_ERR_OOM. A schema that is not a string or holds U+0000, or a body that is not a Uint8Array, throws TypeError.
(Source: bindings/js/types/bind.d.ts, bindings/js/lib/bind.js, bindings/js/src/ds.c, runtime/src/libds/rfcs/rfcs.h, runtime/src/libds/rfcs/codec.c, Spec: [LSTR.1.5], [LGRA.5.8.6], [RFCS.6.3])
decodeAnnouncedFactBody#
decodeAnnouncedFactBody: (slots: AnnouncedSlot[], body: Uint8Array) => Record<string, SlotValue>;
Types a FactAssert or FactEmit atom’s fact bytes by the stream’s own Slot atoms, in order. A member of the Api.
The values are those decodeFactBody gives.
|
Condition |
|---|---|
|
a fact-reference slot; a |
|
a malformed body, a |
|
a |
|
an allocation fails |
|
Condition |
|---|---|
|
not an array |
a slot |
not an object; its |
|
not a |
(Source: bindings/js/types/types.d.ts, bindings/js/lib/bind.js, bindings/js/src/ds.c, runtime/src/libds/rfcs/codec.c, Spec: [RFCS.5.1], [RFCS.6.3], [LSTR.1.5], [LGRA.5.8.6])
decodeAnnouncedFactBodyHandles#
decodeAnnouncedFactBodyHandles: (slots: AnnouncedSlot[], body: Uint8Array) => AnnouncedBody;
decodeAnnouncedFactBody, with each fact-reference slot as the stream recorded it, in an AnnouncedBody. A member of the Api.
It throws the same TypeErrors and the same DsErrors, but for a fact-reference slot’s DS_ERR_INVAL. A fact-reference handle of 0, or a slot not nullable with no handle, throws one with DS_ERR_PARSE.
(Source: bindings/js/types/types.d.ts, bindings/js/lib/bind.js, bindings/js/src/ds.c, runtime/src/libds/rfcs/codec.c, Spec: [RFCS.6.3])
AnnouncedSlot#
export type AnnouncedSlot = { name: string; kind: number; nullable: boolean; schemaRef: string | null; };
One slot as a stream announces it, a Slot atom’s fields.
kind is Slot.kind’s number. schemaRef is a fact-reference slot’s schema, and otherwise null or "".
(Source: bindings/js/types/types.d.ts, Spec: [RFCS.5.1])
AnnouncedBody#
export type AnnouncedBody = { slots: Record<string, SlotValue>; refs: Record<string, bigint | null>; };
A fact body typed by its announced slots.
slots holds every slot but a fact-reference one. refs holds each fact-reference slot’s recorded handle, or null where a nullable one is absent. A handle names a fact only within its stream.
(Source: bindings/js/types/types.d.ts, Spec: [RFCS.6.3])