ReviewOS

also looking at this

stacks/drone

feat: add shared-clock scoped vehicle simulation sessions

#98
Merged chris wants to merge codex/cargo-fleet-runtime into main
42 files +2764 -127
resources/functions/drone/authority/core.tsmodified+5-4
Changes to resources/functions/drone/authority/core.ts
@@ -18,14 +18,14 @@ export function createAuthorityKernel<Output, Frame>(options: { sessionId: bigin
1818 let lastNow: number | null = null, lastGatedAt: number | null = null, leaseUntilMs: number | null = null, target: PositionTarget | null = null, reference: string | null = null
1919 let reason = 'startup', commandAccepted = false
2020 const events: { atMs: number | null, from: AuthorityState, to: AuthorityState, reason: string, epoch: number }[] = []
21 function transition(to: AuthorityState, why: string) {
21 function transition(to: AuthorityState, why: string, eventAtMs: number | null = lastNow) {
2222 reason = why
2323 if (to === state) return
2424 const from = state
2525 state = to
2626 epoch = Math.min(0xFFFFFFFF, epoch + 1)
2727 leaseUntilMs = null; target = null
28 events.push({ atMs: lastNow, from, to, reason: why, epoch })
28 events.push({ atMs: eventAtMs, from, to, reason: why, epoch })
2929 if (events.length > 64) events.shift()
3030 }
3131 function fault(why: string) { if (state !== 'killed') transition('failsafe', why) }
@@ -54,9 +54,10 @@ export function createAuthorityKernel<Output, Frame>(options: { sessionId: bigin
5454 update(o: AuthorityObservation, command?: AuthorityCommand) {
5555 commandAccepted = false
5656 const observedEpoch = epoch
57 const validNow = Number.isFinite(o.nowMs) && o.nowMs >= 0 && (lastNow === null || o.nowMs >= lastNow)
5758 // A modeled independent kill input always wins, including over malformed clocks.
58 if (o.interlocks.killActive === true) transition('killed', 'kill-input')
59 if (!Number.isFinite(o.nowMs) || o.nowMs < 0 || (lastNow !== null && o.nowMs < lastNow)) { if (active(state)) fault('clock-discontinuity'); reason = state === 'killed' ? reason : 'clock-discontinuity'; return snapshot() }
59 if (o.interlocks.killActive === true) transition('killed', 'kill-input', validNow ? o.nowMs : lastNow)
60 if (!validNow) { if (active(state)) fault('clock-discontinuity'); reason = state === 'killed' ? reason : 'clock-discontinuity'; return snapshot() }
6061 const gap = lastNow !== null && o.nowMs - lastNow > authorityPolicy.schedulerGapMs
6162 lastNow = o.nowMs
6263 if (active(state) && gap) fault('supervisor-schedule-gap')