-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeterministic_safety_monitor.hpp
More file actions
263 lines (219 loc) · 8.12 KB
/
Copy pathdeterministic_safety_monitor.hpp
File metadata and controls
263 lines (219 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#pragma once
#include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>
#include "raps/rollback_execution.hpp"
#include "itl/itl_manager.hpp"
// =====================================================
// Deterministic Safety Monitor (DSM)
// =====================================================
// Hard-physics, last-line-of-defense safety enforcement
// Independent of main control loop
// =====================================================
namespace DSM_Config {
// Absolute physical limits (EFE derived)
constexpr double MAX_CURVATURE_THRESHOLD_RMAX = 1.0e-12;
// HLV Pillar 2: Oscillatory Modulation Stability
constexpr double MIN_ACCEPTABLE_A_T = 0.80;
// HLV Pillar 5: Tri-Cell Coupling
constexpr double MAX_TCC_COUPLING_J = 1.0e+04;
// Failsafe parameters
constexpr double MIN_RESONANCE_AMPLITUDE_CUTOFF = 0.10;
// WNN Constraints
constexpr double WNN_MAX_CURVATURE_PROXY = 5.0e-11;
constexpr double WNN_MIN_OSCILLATORY_PREFACTOR = 0.85;
constexpr double WNN_MAX_OSCILLATORY_PREFACTOR = 1.25;
constexpr double INVALID_TELEMETRY_SENTINEL = -1.0;
} // namespace DSM_Config
struct WnnTelemetry {
double curvature_proxy{0.0};
double oscillatory_prefactor{1.0};
uint32_t timestamp_ms{0U};
};
// =====================================================
// DSM Sensor Inputs (Independent Channels)
// =====================================================
struct DsmSensorInputs {
double measured_proper_time_dilation;
double measured_oscillatory_prefactor_A_t;
double measured_tcc_coupling_J;
double current_resonance_amplitude;
bool main_control_system_healthy;
};
// =====================================================
// Deterministic Safety Monitor
// =====================================================
class DeterministicSafetyMonitor {
public:
enum SafingAction {
ACTION_NONE = 0,
ACTION_ROLLBACK = 1,
ACTION_FULL_SHUTDOWN = 2
};
DeterministicSafetyMonitor();
int evaluateSafety(const DsmSensorInputs& inputs);
bool pollWnnAndEnforce(
const WnnTelemetry& wnn_telem,
ITLManager& itl_manager,
const RollbackPlan* rollback_store,
uint32_t rollback_count,
PhysicsState& active_state_pointer
);
private:
double last_estimated_Rmax_;
bool safing_sequence_active_;
bool hasInvalidInputs(const DsmSensorInputs& inputs) const;
bool hasInvalidWnnTelemetry(const WnnTelemetry& wnn_telem) const;
bool isWnnThresholdBreached(const WnnTelemetry& wnn_telem) const;
bool checkResonanceStability(double A_t, double J_coupling) const;
double estimateCurvatureScalar(double dilation) const;
bool checkCurvatureViolation(double R_estimated) const;
};
// =====================================================
// Implementation
// =====================================================
inline DeterministicSafetyMonitor::DeterministicSafetyMonitor()
: last_estimated_Rmax_(0.0),
safing_sequence_active_(false) {}
inline double
DeterministicSafetyMonitor::estimateCurvatureScalar(double dilation) const {
const double R_FACTOR = 1.0e-10;
double time_stretch = 1.0 - dilation;
if (time_stretch < 0.0) {
return std::numeric_limits<double>::infinity();
}
return R_FACTOR * time_stretch * time_stretch;
}
inline bool
DeterministicSafetyMonitor::checkCurvatureViolation(double R_estimated) const {
return (R_estimated >= DSM_Config::MAX_CURVATURE_THRESHOLD_RMAX);
}
inline bool
DeterministicSafetyMonitor::hasInvalidInputs(
const DsmSensorInputs& inputs
) const {
return !std::isfinite(inputs.measured_proper_time_dilation) ||
!std::isfinite(inputs.measured_oscillatory_prefactor_A_t) ||
!std::isfinite(inputs.measured_tcc_coupling_J) ||
!std::isfinite(inputs.current_resonance_amplitude);
}
inline bool
DeterministicSafetyMonitor::hasInvalidWnnTelemetry(
const WnnTelemetry& wnn_telem
) const {
return !std::isfinite(wnn_telem.curvature_proxy) ||
!std::isfinite(wnn_telem.oscillatory_prefactor) ||
wnn_telem.curvature_proxy < 0.0 ||
wnn_telem.oscillatory_prefactor < 0.0;
}
inline bool
DeterministicSafetyMonitor::isWnnThresholdBreached(
const WnnTelemetry& wnn_telem
) const {
return wnn_telem.curvature_proxy >= DSM_Config::WNN_MAX_CURVATURE_PROXY ||
wnn_telem.oscillatory_prefactor <= DSM_Config::WNN_MIN_OSCILLATORY_PREFACTOR ||
wnn_telem.oscillatory_prefactor >= DSM_Config::WNN_MAX_OSCILLATORY_PREFACTOR;
}
inline bool
DeterministicSafetyMonitor::checkResonanceStability(
double A_t,
double J_coupling
) const {
if (A_t < DSM_Config::MIN_ACCEPTABLE_A_T) {
std::cerr << "DSM FAILURE PREDICT: A(t) unstable (" << A_t << ")\n";
return true;
}
if (J_coupling > DSM_Config::MAX_TCC_COUPLING_J) {
std::cerr << "DSM FAILURE PREDICT: TCC coupling exceeded ("
<< J_coupling << ")\n";
return true;
}
return false;
}
inline int
DeterministicSafetyMonitor::evaluateSafety(
const DsmSensorInputs& inputs
) {
if (hasInvalidInputs(inputs)) {
safing_sequence_active_ = true;
std::cerr
<< "DSM ALERT: Non-finite sensor input detected — FULL SHUTDOWN\n";
return ACTION_FULL_SHUTDOWN;
}
const double R_estimated =
estimateCurvatureScalar(inputs.measured_proper_time_dilation);
last_estimated_Rmax_ = R_estimated;
if (!std::isfinite(R_estimated) || checkCurvatureViolation(R_estimated)) {
safing_sequence_active_ = true;
std::cerr
<< "DSM ALERT: ABSOLUTE CURVATURE VIOLATION — FULL SHUTDOWN\n";
return ACTION_FULL_SHUTDOWN;
}
if (checkResonanceStability(
inputs.measured_oscillatory_prefactor_A_t,
inputs.measured_tcc_coupling_J
)) {
safing_sequence_active_ = true;
return ACTION_ROLLBACK;
}
if (!inputs.main_control_system_healthy &&
inputs.current_resonance_amplitude >
DSM_Config::MIN_RESONANCE_AMPLITUDE_CUTOFF) {
safing_sequence_active_ = true;
return ACTION_ROLLBACK;
}
if (safing_sequence_active_ &&
R_estimated <
DSM_Config::MAX_CURVATURE_THRESHOLD_RMAX * 0.5) {
safing_sequence_active_ = false;
std::cout << "DSM STATUS: Safety margins restored\n";
}
return ACTION_NONE;
}
inline bool
DeterministicSafetyMonitor::pollWnnAndEnforce(
const WnnTelemetry& wnn_telem,
ITLManager& itl_manager,
const RollbackPlan* rollback_store,
uint32_t rollback_count,
PhysicsState& active_state_pointer
) {
const bool invalid_wnn_input = hasInvalidWnnTelemetry(wnn_telem);
const bool threshold_breach = isWnnThresholdBreached(wnn_telem);
if (invalid_wnn_input || threshold_breach) {
// Keep safing active until the broader control loop restores margins.
safing_sequence_active_ = true;
if (invalid_wnn_input) {
std::cerr << "DSM ALERT: Non-finite WNN telemetry detected — ROLLBACK\n";
} else {
std::cerr << "DSM ALERT: WNN thresholds exceeded — ROLLBACK\n";
}
const double logged_curvature = std::isfinite(wnn_telem.curvature_proxy)
? wnn_telem.curvature_proxy
: DSM_Config::INVALID_TELEMETRY_SENTINEL;
const double logged_prefactor = std::isfinite(wnn_telem.oscillatory_prefactor)
? wnn_telem.oscillatory_prefactor
: DSM_Config::INVALID_TELEMETRY_SENTINEL;
// Breach detected! Log to ITL and execute immediate rollback.
const bool ledger_committed =
itl_manager.log_wnn_rollback_event(logged_curvature, logged_prefactor);
if (!ledger_committed) {
PlatformHAL::metric_emit(
"safety.wnn.ledger_commit_failed",
1.0f
);
}
const bool rollback_executed = trigger_wnn_immediate_rollback(
rollback_store,
rollback_count,
active_state_pointer
);
PlatformHAL::metric_emit(
"safety.wnn.rollback_executed",
rollback_executed ? 1.0f : 0.0f
);
return rollback_executed;
}
return false; // No breach
}