/** * The run record: a whole run as a plain JSON document. * * **The record is the conformance case.** That is the one decision this module * exists to hold, and everything in it follows from it: no object reference, no * function, no dependence on this implementation's internals, and nothing that * only this engine can read. A record carries the script revision, the compiled * program, the bars, the settings the host chose, the frames the destination * answered in delivery order, the fills the engine folded in fold order, the * ledger the run ended with, the diagnostics and the report. * * **The bars are inline or referenced, and both carry the same hash.** Inline is * the conformance form, because a case has to hold every byte of its own input. * Referenced is the operational form, because the smallest deployment has a one * megabyte request body and nothing that grows with history may travel in one. * A replay whose bars do not hash to the record's hash is OS6022 rather than a * silently different study. * * **The frames name an intent by ordinal and never by an engine's own id**, * because a case cannot know what id another engine minted. The same reason the * ledger rows here are flat words rather than this engine's own types. * * The record is written through the existing canonical encoding, so there is one * canonical writer in the repository and key order and number form are fixed for * everybody. */ import type { RecordedFill, Report } from '../accounting/index.js'; import type { Diagnostic } from '../diagnostics/index.js'; import type { CompiledProgram, SourceStamp } from '../emit/index.js'; import type { Instrument, LedgerRow } from '../engine/index.js'; import type { BacktestSettings } from './settings.js'; /** One bar as a record holds it. */ export interface RecordedBar { readonly time: number | null; readonly open: number | null; readonly high: number | null; readonly low: number | null; readonly close: number | null; readonly volume: number | null; readonly oi: number | null; } /** * The bars, held or pointed at. * * Both forms carry the same hash, computed the same way over the canonical * tuple of each bar, so a referenced record can be turned into an inline one by * whoever holds the bars and the result is the same case. */ export type BarsInRecord = { readonly form: 'inline'; readonly hash: string; readonly count: number; readonly rows: readonly RecordedBar[]; } | { readonly form: 'referenced'; readonly hash: string; readonly count: number; readonly firstTime: number | null; readonly lastTime: number | null; }; /** The columns of `conformance.md`'s frames.csv, in its order and with its names. */ export interface RecordedFrame { readonly afterBar: number; /** An ordinal, 1-based, never an engine's own id. */ readonly intent: number; readonly status: string; readonly filledQty: number; readonly avgFillPrice: number | null; readonly orderRef: string | null; readonly text: string | null; /** * The destination's own instant for this frame, or null where it stated none. * * **One field of the ledger is folded from it.** `host-interface.md` 7.2 * gives a frame a `time` and `stdlib.md` 17.7 moves `updatedAt` to it, so a * record that dropped it wrote a ledger no engine reading the case back could * fold to: handed frames with no instant, that engine leaves every * `updatedAt` at `placedAt`, and the two disagree on exactly the rows whose * destination answered later than the bar that placed the order. * * Absent rather than substituted, because 7.2 lets a destination state none * and a row whose frame stated none keeps the instant it had. */ readonly time: number | null; } /** A ledger row of `stdlib.md` 17.7, flattened: the `orders` channel of expected.json. */ export interface RecordedOrder { readonly intent: number; readonly orderRef: string; readonly tag: string; readonly leg: string; readonly positionRef: number; readonly symbol: string | null; readonly exchange: string | null; readonly product: string; readonly side: string; readonly qty: number | null; readonly qtyType: string; readonly type: string | null; readonly price: number | null; readonly trigger: number | null; readonly status: string; readonly filledQty: number; readonly avgFillPrice: number | null; readonly rejection: string | null; readonly placedAt: number | null; readonly updatedAt: number | null; readonly units: number | null; } /** A diagnostic as a record holds it: the code, the span and the bar. */ export interface RecordedDiagnostic { readonly code: string; readonly line: number; readonly column: number; readonly severity: string; readonly barIndex: number | null; } /** One run, whole, and readable by an engine that is not this one. */ export interface RunRecord { readonly recordVersion: number; readonly engine: { readonly name: string; readonly version: string; }; readonly languageVersion: string; /** Already plain data. */ readonly program: CompiledProgram; readonly programHash: string; /** Hash, lines, file: the script revision. */ readonly source: SourceStamp; /** * The source text itself, or null on a record written before version 2. * * **A record is the conformance case, and a case has to hold `script.os`.** * `source` above identifies the script and cannot reproduce it: a hash is a * fingerprint, so it settles whether two files are the same and yields * neither of them. Without the text a stored record could be replayed months * later and still not be handed to anybody else to run, which is the whole * claim the suite exists to test. * * It lives here and not on the compiled program on purpose. A program is * executable data that no engine needs the source to run, and it is the * versioned artefact adopters depend on; putting the text there would send a * script everywhere its program travels and widen the format every engine * has to read. The record is the thing that wants to be self-contained. */ readonly sourceText: string | null; readonly settings: BacktestSettings; /** * The instrument record of `host-interface.md` 4.1, as the engine was handed * it, or null on a record written before version 3. * * **A case holds `instrument.json`, and `conformance.md` section 2 says that * file is this record.** The contract in `settings` is the money layer's * snapshot of the same instrument and holds six of the twelve facts; the * ones a script reads and the money never does, the interval, the timezone, * the session and the volume flag, were handed to the engine and written * down nowhere. A case harvested from such a record either omitted a fact * the page requires or stated one the run never had, and either way the * second engine ran a different study than the one the expected output * came from. * * It is the whole record and not the six facts beside the contract, because * what is recorded is what the engine read at load, verbatim, and a reader * should not have to compose it. */ readonly instrument: Instrument | null; readonly bars: BarsInRecord; /** What the destination answered, in delivery order. */ readonly frames: readonly RecordedFrame[]; /** What the engine folded, in fold order. */ readonly fills: readonly RecordedFill[]; /** The ledger at the end, oldest first. */ readonly orders: readonly RecordedOrder[]; readonly diagnostics: readonly RecordedDiagnostic[]; readonly report: Report; } /** * The revision of this document's own shape. * * A reader is handed a record and has to know what it is looking at before it * reads a field, so the number is first in the document and is written by this * file alone. It moves when a channel is added or a meaning changes, never when * a figure in a report does. */ export declare const RECORD_VERSION = 4; /** The parts a run hands over, each already in the shape the record holds. */ export interface RecordParts { readonly program: CompiledProgram; /** * The script's own text, so the record can become a conformance case. * * Optional because a caller that has only a compiled program cannot invent * it, and a run is still worth recording without it. `caseFilesFrom` is the * one thing that then cannot be served, and it says so rather than writing a * case with a hole in it. */ readonly sourceText?: string; readonly settings: BacktestSettings; /** The instrument record the engine was handed, `host-interface.md` 4.1. */ readonly instrument: Instrument; readonly bars: readonly RecordedBar[]; /** * Whether the bars travel in the record or are pointed at. * * Inline is the conformance form, because a case holds every byte of its own * input. Referenced is the operational form, because nothing that grows with * history may travel in a request body. Both carry the same hash over the * same canonical tuples, so one becomes the other without becoming a * different case. */ readonly form: 'inline' | 'referenced'; readonly frames: readonly RecordedFrame[]; readonly fills: readonly RecordedFill[]; readonly orders: readonly RecordedOrder[]; readonly diagnostics: readonly RecordedDiagnostic[]; readonly report: Report; } /** * One run, as the document another engine is handed. * * Nothing is computed here. Every channel arrives folded, because what a record * says has to be what the run did rather than what a second fold of the same * inputs came to: a record that recomputed its own report would agree with * itself whatever the engine had actually done. */ export declare function recordOf(parts: RecordParts): RunRecord; /** * The bars, held or pointed at, and the same hash either way. * * The referenced form keeps the first and last times so that a reader can say * which bars are wanted without holding them, and the count so that a set of * the wrong length is known to be wrong before it is hashed. */ export declare function barsIn(bars: readonly RecordedBar[], form: 'inline' | 'referenced'): BarsInRecord; /** * The hash a record names its bars by. * * Over the canonical tuple of each bar, in the order they were supplied, and * through the canonical encoding the compiled program is hashed with, so there * is one canonical writer in the repository rather than two that agree until * one of them is changed. A tuple rather than an object, because the field * order is then the specification's and not a sort's. */ export declare function barsHash(bars: readonly RecordedBar[]): string; /** * The record as the bytes another engine reads. * * Through the canonical encoding, so key order is fixed, every number is in the * shortest decimal form that reads back as itself, and two runs of the same * record produce the same bytes on every machine. That is what makes the * reproducibility test a byte comparison rather than a tour of the fields. */ export declare function recordToJson(record: RunRecord): string; /** * The run, without who produced it. * * **`engine` is provenance, and provenance is not part of the run.** The claim * a rerun makes is that the run is a function of the record, and that claim has * to survive the engine it runs on being upgraded: a record stored under * `0.3.0` and rerun under `0.4.0` produces the same trades, the same fills and * the same money, and a document carrying the version stamp inside the bytes * being compared says it does not. * * It was compared as whole bytes until the first version bump would have broken * it, which is a test that passes for exactly as long as nothing changes and * then fails for the one reason that is not a defect. So the stamp stays in the * record, because knowing which engine wrote a run is worth keeping, and the * comparison is made over everything else. */ export declare function runBytes(record: RunRecord): string; /** * A record read back from those bytes. * * A parse and not a validation: what comes back is the document as it was * written, and a document this engine did not write is the caller's to trust or * not. The one thing asserted is the revision, because reading a later * revision's fields under this one's rules is how a record silently becomes a * different run. */ export declare function recordFromJson(text: string): RunRecord | null; /** * One ledger row, flattened into the words a case file prints. * * The engine's own types do not cross: a second engine has its own, and a * channel written in this one's would be a case only this one could read. The * intent is an ordinal for the same reason, because no engine can know what id * another minted. * * `qtyType` comes from the intent rather than from the row, because the unit a * quantity is counted in is per order and not per leg: an order the engine * sized itself is in units whatever the declaration counts in, and a row that * printed the declaration's unit for it would state a quantity in a unit it was * never measured in. */ export declare function orderIn(row: LedgerRow, intent: number, qtyType: string): RecordedOrder; /** One diagnostic, as a record holds it: the code, the span and the bar. */ export declare function diagnosticIn(diagnostic: Diagnostic, barIndex: number | null): RecordedDiagnostic; //# sourceMappingURL=record.d.ts.map