ReviewOS

also looking at this

stacks/drone

feat: add retained cargo fleet cancellation and recovery

#105
Merged chris wants to merge codex/cargo-fleet-operator-recovery into main
32 files +2496 -27
resources/functions/drone/cargo/fleet-control.tsadded+37-0
Changes to resources/functions/drone/cargo/fleet-control.ts
@@ -0,0 +1,37 @@
1import { createCargoFleetSession } from './fleet'
2
3export const cargoFleetOperatorCases = ['cancel', 'cancel-resume', 'cancel-after-grant', 'resume-after-navigation-loss'] as const
4type OperatorCase = typeof cargoFleetOperatorCases[number]
5function captureCase(input: unknown): OperatorCase {
6 try {
7 if (!input || typeof input !== 'object' || Object.getPrototypeOf(input) !== Object.prototype || Reflect.ownKeys(input).some(key => key !== 'case')) throw new TypeError()
8 const d = Object.getOwnPropertyDescriptor(input, 'case')
9 if (d && (!d.enumerable || !Object.hasOwn(d, 'value') || !cargoFleetOperatorCases.some(name => name === d.value))) throw new TypeError()
10 return (d?.value ?? 'cancel-resume') as OperatorCase
11 }
12 catch { throw new TypeError('cargo-fleet-control-arguments') }
13}
14
15/** Explicit fictional operator decisions over the retained world, without hardware dispatch. */
16export function simulateCargoFleetControl(input: unknown = {}, ...extra: unknown[]) {
17 if (extra.length) throw new TypeError('cargo-fleet-control-arguments')
18 const name = captureCase(input), world = createCargoFleetSession({ scenario: name === 'resume-after-navigation-loss' ? 'receiver-navigation-loss-during-rail' : 'nominal' })
19 let cancelled = false, resumed = false
20 try {
21 for (let nowMs = 0; ; nowMs += 4) {
22 const progress = world.step(nowMs), state = world.snapshot()
23 if (progress.terminal) return { case: name, run: world.result() }
24 const decide = (action: 'cancel' | 'resume', sequence: number) => world.decide({ version: 1, evidence: 'modeled-cargo-transport-decision-v1', contextId: state.operator.contextId,
25 bootId: state.receiver!.authority.bootId, sessionId: state.receiver!.authority.sessionId, sequence, issuedAtMs: nowMs, reference: state.receiver!.reference, sourceEventId: state.stationSourceEventId, action })
26 const cancelNow = name === 'cancel-after-grant' ? state.intake.custody.grantedAtMs !== null
27 : state.station?.phase === 'moving' && (name === 'resume-after-navigation-loss' || Math.hypot(...state.station.parcel!.velocityMps) > .1)
28 if (!cancelled && cancelNow) { decide('cancel', 1); cancelled = true }
29 else if (cancelled && !resumed && name !== 'cancel') {
30 const resumeNow = name === 'cancel-after-grant' || (name === 'resume-after-navigation-loss' ? state.receiver?.authority.state === 'failsafe'
31 : state.station?.phase === 'cancelled' && state.station.stoppedAtMs !== null && nowMs >= state.station.stoppedAtMs + 1000)
32 if (resumeNow) { decide('resume', 2); resumed = true }
33 }
34 }
35 }
36 finally { world.close() }
37}