PineForge v1.5.0-rc.1-1-g4e01e96b
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
execution_observer.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: Apache-2.0
3 *
4 * execution_observer.h - public C ABI for the execution observer, version 1.
5 *
6 * Declarations only. The runtime supplies the definitions.
7 *
8 * Layout: natural C alignment, fixed-width scalars, no struct packing. Each
9 * struct_size must equal sizeof its declaration, and each version must be 1.
10 * Expected LP64 sizes are 24 (boundary), 32 (receipt), 24 (observer) and 48
11 * (observation).
12 *
13 * Pointers are borrowed for the lifetime stated on each declaration; the
14 * runtime never takes ownership. A pf_strategy_t must be a valid, nonnull
15 * handle from the matched runtime. Forged or dangling handles are outside the
16 * contract.
17 */
18
19#ifndef PINEFORGE_EXECUTION_OBSERVER_H
20#define PINEFORGE_EXECUTION_OBSERVER_H
21
22#include <stdint.h>
23
24#include <pineforge/pineforge.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/** Boundary handed to the observer. The kernel fills every field before the
31 * callback. run_generation is the latest admitted epoch: nonzero at
32 * admission, monotonic, never wraps. attempt_serial is the serial of the
33 * public run or stream-begin entry that owns that epoch. */
40
41/** Receipt the callback fills. Before the callback the kernel sets the
42 * receipt's struct_size, version and run_generation, frame_bytes to
43 * UINT32_MAX, and handed_bytes, export_requested and reserved to 0.
44 *
45 * The callback must overwrite the UINT32_MAX frame_bytes sentinel even when
46 * nothing is exported. It succeeds only if it returns 0 with no exception and
47 * no earlier failure cause, and then:
48 * - struct_size, version and run_generation are unchanged;
49 * - reserved is 0;
50 * - export_requested is exactly 0 or exactly 1;
51 * - export_requested 0: frame_bytes and handed_bytes are both 0;
52 * - export_requested 1: 1 <= frame_bytes <= 1024 (the whole frame), and
53 * handed_bytes equals frame_bytes.
54 *
55 * handed_bytes does not claim a parent ACK. Any failure leaves the generation
56 * sealed, with no result work and no retry. */
57typedef struct pf_boundary_receipt_v1 {
58 uint32_t struct_size;
59 uint32_t version;
61 uint32_t frame_bytes;
62 uint32_t handed_bytes;
64 uint32_t reserved;
66
67/** Observer callback. The runtime calls it once, synchronously, after the
68 * generation seals on a successful terminal path and before results open.
69 * A failed or aborted generation closes without a successful callback. An
70 * exception thrown here is a failure and never unwinds through C. boundary
71 * and receipt are valid for this call only; context follows the registration
72 * lifetime. */
74 void *context, const pf_execution_boundary_v1 *boundary,
75 pf_boundary_receipt_v1 *receipt);
76
77/** Observer descriptor for strategy_set_execution_observer_v1(). */
84
85/** Snapshot of the execution state, filled by
86 * strategy_execution_observation_v1().
87 *
88 * phase: 0 Idle, 1 Executing, 2 Capturing, 3 Sealed, 4 Results.
89 * fault_stage: 0 None, 1 Execution, 2 AfterExecution.
90 * attempt_outcome: 0 NotCompleted, 1 Refused, 2 Failed, 3 Aborted,
91 * 4 ResultsOpen, 5 Live.
92 * run_generation: latest admitted epoch (see pf_execution_boundary_v1).
93 * attempt_serial: latest minting public run or stream-begin entry.
94 * attempt_generation: 0 if that attempt admitted no generation.
95 *
96 * The caller correlates its own serial and generation. A rejected later
97 * attempt never turns an older Results snapshot into permission for itself.
98 * Registration or removal does not reopen a closed generation. A refused
99 * attempt before admission mints no generation, so the previous closure
100 * stays. */
112
113/** Returns the execution observer ABI version, 1. */
115
116/** Registers, replaces or removes the strategy's execution observer.
117 *
118 * Returns 0 when accepted; -1 for an invalid struct_size or version, a null
119 * before_results or a null strategy handle; -2 for an unsupported execution
120 * contract (not a purity assertion or an unknown-override detector); -3 for a
121 * nonquiescent or reentrant call; -4 when the private consumer storage cannot
122 * be allocated. No exception crosses C.
123 *
124 * Registration copies the descriptor's values. context and before_results
125 * stay borrowed until a successful replacement or removal, or until the
126 * strategy is destroyed. A NULL descriptor removes the registration, but only
127 * when quiescent. A refusal leaves the installed descriptor unchanged. A
128 * forbidden reentry keeps its first cause latched, as the existing entry
129 * contract requires; an ordinary busy refusal does not rewrite a run outcome.
130 * Destroying the strategy never calls the observer. */
133
134/** Copies one complete observation into *out. The caller first sets
135 * out->struct_size and out->version.
136 *
137 * Returns 0 with one complete snapshot, or -1 with *out unchanged. Allowed
138 * inside the observer; it dispatches no host code and allocates no missing
139 * consumer. A fresh or copied consumer reports generation 0 and no owned
140 * result. This is observation only, not permission to publish stale output. */
143
144#ifdef __cplusplus
145} /* extern "C" */
146#endif
147
148#endif /* PINEFORGE_EXECUTION_OBSERVER_H */
int strategy_set_execution_observer_v1(pf_strategy_t, const pf_execution_observer_v1 *)
Registers, replaces or removes the strategy's execution observer.
int(* pf_before_results_fn_v1)(void *context, const pf_execution_boundary_v1 *boundary, pf_boundary_receipt_v1 *receipt)
Observer callback.
uint32_t pf_execution_observer_version(void)
Returns the execution observer ABI version, 1.
int strategy_execution_observation_v1(pf_strategy_t, pf_execution_observation_v1 *)
Copies one complete observation into *out.
void * pf_strategy_t
Opaque handle to a compiled strategy instance.
Definition pineforge.h:436
#define PF_API
Definition pineforge.h:83
Receipt the callback fills.
Boundary handed to the observer.
Snapshot of the execution state, filled by strategy_execution_observation_v1().
Observer descriptor for strategy_set_execution_observer_v1().
pf_before_results_fn_v1 before_results