feat(dev): correlate telemetry recovery, safety stops and passive drive evidence

This commit is contained in:
DCCONSTRUCTIONS
2026-11-21 20:58:26 +03:00
parent 43d4916331
commit d4ed96f3cd
73 changed files with 4188 additions and 349 deletions
+59 -14
View File
@@ -5,9 +5,14 @@ import secrets
import threading
from missioncore_plugin_sdk.bounded_diagnostics import BoundedDiagnostics
from missioncore_plugin_sdk.diagnostic_options import LEGACY_OPTIONS, diagnostic_options
from missioncore_plugin_sdk.diagnostic_options import (
LEGACY_OPTIONS,
PASSIVE_OPTIONS,
diagnostic_options,
)
from .rover_diagnostics_export import DiagnosticExport
from .rover_inventory_evidence import InventoryEvidence
from .rover_local_trace import LocalRoverTrace
from .rover_preview_evidence import preview_sample
from .rover_stop_evidence import CoreStopEvidence
@@ -15,14 +20,14 @@ from .rover_stop_evidence import CoreStopEvidence
KEYS={'KeyW','KeyA','KeyS','KeyD','KeyQ','KeyE','Space','Escape'}
INPUT_EVENTS={'key_down','key_up','pause','stop','pointer_down','pointer_up','pointer_cancel',
'settings','demand','heartbeat','request','transport','lifecycle'}
INPUT_FIELDS={'code','repeat','reason','held','mode','current_a','speed','control_mode','left','right',
INPUT_FIELDS={'code','repeat','reason','held','mode','current_a','speed','control_mode','telemetry_stop_ms','browser_event_id','left','right',
'sequence','session_id','foreground','input_valid','pending','action','outcome','elapsed_ms',
'browser_ms','ready_state','buffered_bytes','close_code','visibility','dropped',
'browser_ms','browser_time_origin_ms','ready_state','buffered_bytes','close_code','visibility','dropped',
'request_id','socket_id','expires_ms','clock_ms','state_age_ms','gap_ms'}
class CoreTrace(BoundedDiagnostics):
def __init__(self,clock,live):
super().__init__('core',clock);self.live=live;self.local=LocalRoverTrace(clock)
def __init__(self,clock,live,root=None):
super().__init__('core',clock);self.live=live;self.local=LocalRoverTrace(clock,root)
def recording(self):return self.active() or self.local.active()
def capture_identity(self):return (self.id if self.active() else None, self.local.id if self.local.active() else None)
def add(self,event,**data):
@@ -33,22 +38,50 @@ class CoreTrace(BoundedDiagnostics):
if row:self.live.add('record',record=row)
class RoverDiagnostics:
def __init__(self,clock):
def __init__(self,clock,root=None):
self.lock=threading.RLock();self.live=BoundedDiagnostics('core',clock)
self.trace=CoreTrace(clock,self.live);self.node_ack=0;self.relay_ack=0;self.node_status={}
self.clock=clock;self.options=dict(LEGACY_OPTIONS);self.supports_options=False
self.control_stop=CoreStopEvidence(clock)
self.trace=CoreTrace(clock,self.live,root);self.node_ack=0;self.relay_ack=0;self.node_status={}
self.clock=clock;self.options=dict(LEGACY_OPTIONS);self.supports_options=False;self.supports_sources=False
self.supports_passive=False;self.passive_confirmed=False
self.status_pending = True
self.control_stop=CoreStopEvidence(clock, sink=self.trace.local.record_stop)
self.trace_tokens=4096.0;self.trace_at=clock()
self.enabled=False;self.closing=False;self.vehicle='Rover';self.context={};self.export=DiagnosticExport()
self.local_owner = None
self.inventory_evidence = InventoryEvidence(self.trace.local)
def record_inventory(self, value):
with self.lock:
self.inventory_evidence.record(value)
def exchange(self, body):
self.ingest(body.get('diagnostics'))
return {'diagnostics': self.desired(relay_ack=True, export_v2=body.get('diagnostic_export_v2') is True)}
# One lifecycle handshake after Core startup discovers a recorder that
# survived on Node. Afterwards passive recording remains silent, even
# if Node reports that no capture exists. No event rows are requested.
self.status_pending = False
# Local receipt evidence only: no echo, wake hint or extra network request.
self.trace.local.add('board_diagnostics_exchange')
reply = {'diagnostics': self.desired(relay_ack=True, export_v2=body.get('diagnostic_export_v2') is True)}
if body.get('diagnostic_passive_v1') is True:
reply['diagnostics_pending'] = self.traffic_pending()
return reply
def traffic_pending(self):
# Atomic read-only hint; no recorder/file locks in the motion path.
return bool(
(self.supports_passive and self.status_pending) or self.export.busy or self.closing
or (self.enabled and (not self.options.get('passive') or not self.passive_confirmed))
)
def watch_required(self):
return (self.enabled or self.closing) and not self.options.get('passive')
def set_enabled(self, enabled, options=None, local_options=None):
if type(enabled) is not bool:raise ValueError('Invalid DEV mode')
selected=diagnostic_options(options)
selected=diagnostic_options(PASSIVE_OPTIONS if options is None and self.supports_passive else options)
if enabled and selected["passive"] and local_options is None:
local_options = {"enabled": True, "preview": True, "previous_id": self.trace.local.id}
if local_options is not None and (not isinstance(local_options, dict)
or set(local_options) - {"enabled", "preview", "previous_id"}
or type(local_options.get("enabled")) is not bool
@@ -56,6 +89,10 @@ class RoverDiagnostics:
raise ValueError("Invalid operator capture options")
if enabled and options is not None and not self.supports_options:
raise ValueError('Борт ещё не подтвердил поддержку настроек записи.')
if enabled and (selected["router"] or selected["system"]) and not self.supports_sources:
raise ValueError("Для записи состояния сети и оборудования обновите Node на борту.")
if enabled and selected["passive"] and not self.supports_passive:
raise ValueError("Для пассивной DEV-записи обновите Node на борту.")
with self.lock:
if enabled and not self.enabled:
if self.closing or self.export.busy:raise ValueError('Дождитесь завершения записи или сохранения файла.')
@@ -66,7 +103,7 @@ class RoverDiagnostics:
identifier=secrets.token_hex(16)
self.options=selected;self.trace_tokens=4096.0;self.trace_at=self.clock()
self.live.configure(identifier,True);self.trace.configure(identifier,selected['core_trace'])
self.node_ack=self.relay_ack=0;self.node_status={};self.enabled=True
self.node_ack=self.relay_ack=0;self.node_status={};self.enabled=True;self.passive_confirmed=False
self.trace.add('capture_started',context=self.context)
if self.local_owner == self.trace.local.id and self.local_owner is not None:
self.trace.local.add('local_capture_context', context=self.context,
@@ -135,6 +172,9 @@ class RoverDiagnostics:
self.trace.add('core_tail_interrupted',after=ack,before=stop_after)
self.trace.configure(identifier,False)
if identifier!=self.trace.id:return
self.status_pending = False
if packet.get('active') is True and stored.get('closed') is not True:
self.passive_confirmed = True
rows=packet.get('frames',[]);relay=packet.get('relay_events',[])
if not isinstance(rows,list) or len(rows)>32 or not isinstance(relay,list) or len(relay)>64:return
self.node_status={k:packet.get(k) for k in ('id','active','dropped','rejected','sequence','first_sequence','storage','transport_error','stop_tail_v1','options')}
@@ -161,15 +201,19 @@ class RoverDiagnostics:
admitted=target.active() if local else self.enabled and self.options['core_trace']
if not isinstance(body,dict) or body.get('id')!=target.id or not admitted:return False
rows=body.get('events')
if not isinstance(rows,list) or len(rows)>32:raise ValueError('Invalid diagnostic events')
if not isinstance(rows,list) or len(rows)>(256 if local else 32):raise ValueError('Invalid diagnostic events')
for row in rows:
if not isinstance(row,dict):raise ValueError('Invalid diagnostic event')
if row.get('event')=='preview' and local:
if not self.trace.local.preview:
continue
fields={'sample':preview_sample(row.get('sample'))}
if isinstance(row.get('browser_event_id'), str):fields['browser_event_id']=row['browser_event_id'][:64]
stamp=row.get('browser_ms')
if type(stamp) in (int,float):fields['browser_ms']=stamp
origin=row.get('browser_time_origin_ms')
if type(origin) in (int,float):
fields['browser_time_origin_ms']=origin
else:
if row.get('event') not in INPUT_EVENTS:raise ValueError('Invalid diagnostic event')
fields={k:v for k,v in row.items() if k in INPUT_FIELDS}
@@ -185,5 +229,6 @@ class RoverDiagnostics:
after=max(after,self.live.sequence-limit,0)
return {**self.live.snapshot(after,limit),'active':self.enabled,'closing':self.closing,
'local_capture':self.trace.local.snapshot(limit=0),
'local_history':self.trace.local.history(),
'core_stop_v1':self.control_stop.snapshot(),
'node':self.node_status,'options':dict(self.options),'options_supported':self.supports_options,'schema':'missioncore.rover-dev/v1'}
'node':self.node_status,'options':dict(self.options),'options_supported':self.supports_options,'sources_supported':self.supports_sources,'passive_supported':self.supports_passive,'passive_confirmed':self.passive_confirmed,'schema':'missioncore.rover-dev/v1'}