Reference

Grading rules

Data-driven grading, carried in the same file as triggers (ScriptImport.File.grading.rules) — a JSON-authored script can be scored without a coach-ios recompile, the same way its triggers already can be. Source of truth: ScriptImport.makeGradingRules in CoachApp/ATC/ScriptImport+Grading.swift, resolving into a real DeclarativeGradingRule (CoachApp/Debrief/DeclarativeGradingRule.swift).

There's a second, reusable grading mechanism outside this file entirely: an action.grade_against field (see the failure action) names a ProcedureFlowCatalog id — an existing, engine-identity-aware, ordered-checklist grader (e.g. abort-take-off-multi, drill-engine-out-multi) that already threads which engine actually failed straight from the fired trigger's own payload, with zero plumbing here. Reach for a real procedure flow before writing a custom declarative rule, if one already matches the checklist you're grading.

Rule shape, fields & tolerance

{ "id": "r_centerline_1", "label": "Centerline tracking during the rejected takeoff",
  "kind": "centerline_deviation", "runway_width_ft": 75,
  "window_start": { "kind": "trigger", "trigger_id": "t_fail_1" },
  "window_end":   { "kind": "trigger", "trigger_id": "t_stopped_1" } }

Every rule has a stable id (your own choice, used only for internal lookup), a human label (shown in the audit/debrief UI), a kind, and kind-specific fields. trigger_id-style fields here resolve against the same real id→UUID map this file's own triggers array built — not a trigger's name (a hand-authored Swift script's own graders resolve by name instead; a JSON file always uses the real id).

field (telemetry channel)

One of: obs_deg, mag_hdg, true_hdg, alt_ft, agl_ft, ias_kts, loc_dots_abs (localizer, always vs. 0), gs_dots_abs (glideslope, always vs. 0), gear_down_fraction (1.0/0.0), flap_ratio, brake_ratio, bank_deg_abs (bank angle magnitude, always vs. a target regardless of turn direction — a left and right bank of the same magnitude are equally correct or equally wrong).

tolerance

Omit entirely for the default, or give one of:

a string"heading" or "altitude" — resolves against the pilot's live, preset-tiered ScoringRules.
a numbera literal tolerance, overriding the preset entirely.
a 2-element array["heading", 2.0] — a named tolerance scaled by a factor.

window_start / window_end (WindowBoundary)

kindfieldsresolves to
start_of_flight—the recording's first sample.
end_of_flight—the recording's last sample.
triggertrigger_idthe instant that trigger fired.
first_sample_wherefield, op ("gte"/"lte"), valuethe first sample anywhere in the flight where field crosses value in that direction.
first_sample_where_aftersame as above, plus after (a nested WindowBoundary)the first crossing strictly after after's own resolved instant — use this instead of the bare form whenever the same crossing condition can happen more than once in a flight (e.g. the same NAV frequency's localizer dots dip below threshold on an intercept, a procedure-turn rejoin, AND the real final).
fallbackprimary, if_unresolved (both nested WindowBoundary)primary unless it never resolves (e.g. a first_sample_where that never happens), in which case if_unresolved instead.

fired pass/fail

fired

→ DeclarativeGradingRule.fired(id:label:triggerID:)

trigger_idrequiredthat named trigger fired at all during the flight.
{ "id": "landed", "label": "Landed", "kind": "fired", "trigger_id": "t_complete" }

value_at_trigger pass/fail

value_at_trigger

→ DeclarativeGradingRule.valueAtTrigger

trigger_idrequired 
fieldrequired 
targetrequirednumber.
toleranceoptionalsee above.
accept_reciprocaloptional, default falsebool — true accepts target OR target+180, whichever is closer (a charted radial, equally correct dialed either direction). Only meaningful for a heading-shaped field.
{ "id": "r_sac_radial", "label": "SAC radial dialed", "kind": "value_at_trigger",
  "trigger_id": "t_at_sokoy", "field": "obs_deg", "target": 71, "accept_reciprocal": true }

value_at_trigger_from_payload pass/fail, dynamic target

value_at_trigger_from_payload

→ DeclarativeGradingRule.valueAtTriggerFromPayload

Like value_at_trigger, except the target isn't a fixed number — it's extracted from another trigger's own fired payload via a registered extractor (e.g. ATC rolled a fresh hold radial live; there's no fixed chart number to put in a JSON file for it). field is read at read_trigger_id's own instant, NOT payload_trigger_id's — the pilot isn't told the assigned value until that trigger's own message finishes.

payload_trigger_idrequiredwhich trigger's fired payload to extract the target from.
extractor_idrequiredstring — a registered extractor key (DeclarativeGrading.payloadExtractors).
read_trigger_idrequiredwhich trigger's instant to read field at.
fieldrequired 
toleranceoptional 
accept_reciprocaloptional, default falsebool.
{ "id": "r_hold_radial", "label": "Hold radial dialed", "kind": "value_at_trigger_from_payload",
  "payload_trigger_id": "t_hold_instruction", "extractor_id": "hold_radial_deg",
  "read_trigger_id": "t_established_inbound", "field": "obs_deg" }

value_between_triggers pass/fail

value_between_triggers

→ DeclarativeGradingRule.valueBetweenTriggers

start_trigger_idrequired 
end_trigger_idrequired 
fieldrequired 
toleranceoptional 
target_deltaoptional, default 0number — 0 checks "ended up back near where it started"; nonzero checks "changed by about this much" (e.g. "turned ~30° left").
{ "id": "r_turn_30", "label": "Turned 30 left", "kind": "value_between_triggers",
  "start_trigger_id": "t_baseline", "end_trigger_id": "t_turn_done", "field": "mag_hdg", "target_delta": -30 }

window_percent_in_tolerance percentage

window_percent_in_tolerance

→ DeclarativeGradingRule.windowPercentInTolerance

What percent of samples in a window satisfy the per-sample check. target is genuinely optional here, unlike every other rule kind — omitted means "whatever field actually read at the window's own start" (a self-referential, per-flight target — e.g. "no turns off whatever heading you actually departed on").

fieldrequired 
window_start / window_endrequired 
targetoptionalnumber — see above.
toleranceoptional 
comparisonoptional, default "symmetric"symmetric (abs(actual-target) <= tol), at_or_below, or at_or_above — a one-sided ceiling/floor instead of a band (e.g. "mark down if we ever exceed 2000 ft").
{ "id": "r_gear_2", "label": "Gear stayed down", "kind": "window_percent_in_tolerance",
  "field": "gear_down_fraction", "target": 1.0, "tolerance": 0, "comparison": "at_or_above",
  "window_start": { "kind": "trigger", "trigger_id": "t_fail_2" },
  "window_end":   { "kind": "trigger", "trigger_id": "t_stopped_2" } }

closest_in_window pass/fail over a window

closest_in_window

→ DeclarativeGradingRule.closestInWindow

"Did the value ever get close to target, at some point in this window" — finds the single closest sample, then checks THAT one's error against tolerance. A pass/fail check, not a percentage.

fieldrequired 
targetrequirednumber.
window_start / window_endrequired 
toleranceoptional 
{ "id": "r_intercept", "label": "Flew the charted intercept heading", "kind": "closest_in_window",
  "field": "mag_hdg", "target": 245,
  "window_start": { "kind": "trigger", "trigger_id": "t_departed" },
  "window_end":   { "kind": "trigger", "trigger_id": "t_on_course" } }

capture_then_hold_to_end percentage, floating start

capture_then_hold_to_end

→ DeclarativeGradingRule.captureThenHoldToEnd

"No penalty before the aircraft actually captures the target, a real tolerance once it does" — searches forward from window_start for the first sample within tolerance of target (that's the capture point), then grades every sample from there to the end of the flight. Never captured at all → the whole rule is ungraded, not scored as a failure for still approaching.

fieldrequired 
targetrequirednumber.
window_startrequiredwhere to start searching for capture (wire key is window_start; there is no separate window end — it always grades to the end of the flight).
toleranceoptional 
{ "id": "r_cruise_alt", "label": "Held cruise altitude", "kind": "capture_then_hold_to_end",
  "field": "alt_ft", "target": 7000, "window_start": { "kind": "trigger", "trigger_id": "t_departed" } }

reached_fix pass/fail, no trigger needed

reached_fix

→ DeclarativeGradingRule.reachedFix

Did the aircraft ever get within within_nm of a resolved fix, at some point in the window. fix_ref is resolved through the grader's own fix-resolver — a plain string key, not necessarily a real published fix name (an ad-hoc hold site rolled fresh every attempt resolves through its own sentinel key the same way a real VOR would).

fix_refrequiredstring.
within_nmrequirednumber.
window_start / window_endrequired 
{ "id": "r_briefing", "label": "Reached the entry briefing point", "kind": "reached_fix",
  "fix_ref": "SAC", "within_nm": 3,
  "window_start": { "kind": "start_of_flight" }, "window_end": { "kind": "end_of_flight" } }

centerline_deviation continuous

centerline_deviation

→ DeclarativeGradingRule.centerlineDeviation

How close the aircraft stayed to the runway centerline over the window — a continuous, per-sample credit score, not pass/fail: max(0, 1 - |crossTrackFt| / (runway_width_ft/2)) * 100 (100% on centerline, linear to 0% at half-width, floored at 0% beyond). The runway anchor itself (position + true course) is resolved from the window's own START sample — no separate runway/plan lookup needed. runway_width_ft is a given input, never looked up from CIFP (which has no width column).

runway_width_ftrequirednumber.
window_start / window_endrequired 
{ "id": "r_centerline_1", "label": "Centerline tracking", "kind": "centerline_deviation", "runway_width_ft": 75,
  "window_start": { "kind": "trigger", "trigger_id": "t_fail_1" },
  "window_end":   { "kind": "trigger", "trigger_id": "t_stopped_1" } }

stopped_before_runway_end pass/fail

stopped_before_runway_end

→ DeclarativeGradingRule.stoppedBeforeRunwayEnd

Did the aircraft ever get further along the runway than runway_length_ft from the window's own start — never exceeded, anywhere in the window, passes. Same runway-anchor convention as centerline_deviation.

runway_length_ftrequirednumber.
window_start / window_endrequired 
{ "id": "r_stopped_1", "label": "Stopped before the runway end", "kind": "stopped_before_runway_end",
  "runway_length_ft": 5000,
  "window_start": { "kind": "trigger", "trigger_id": "t_fail_1" },
  "window_end":   { "kind": "trigger", "trigger_id": "t_stopped_1" } }

airspeed_penalty_curve continuous

airspeed_penalty_curve

→ DeclarativeGradingRule.airspeedPenaltyCurve

A piecewise-linear penalty curve over author-supplied anchor points, each resolved against a V-speed (AircraftProfile.value(for:), same lookup vspeed-relative conditions use) plus a fixed offset — e.g. "0% at Vmc, 25% at Vmc+5, 100% at blue line." NOT a fixed two-point line — three anchors can't be expressed by min/max interpolation alone. Per-sample credit interpolates ias_kts between the two bracketing anchors (sorted by resolved knots), clamped flat outside that range. An anchor whose vspeed_ref doesn't resolve on the pilot's current profile is simply dropped from the curve (not a failure) — a curve left with fewer than 2 resolvable anchors isn't graded at all.

anchorsrequired, >= 2array of { "vspeed_ref": string?, "offset_kts": number, "percent": number }.
window_start / window_endrequired 
{ "id": "r_airspeed_curve_3", "label": "Airspeed vs. blue line", "kind": "airspeed_penalty_curve",
  "anchors": [
    { "vspeed_ref": "vmc", "offset_kts": 0, "percent": 0 },
    { "vspeed_ref": "vmc", "offset_kts": 5, "percent": 25 },
    { "vspeed_ref": "vyse", "offset_kts": 0, "percent": 100 }
  ],
  "window_start": { "kind": "trigger", "trigger_id": "t_fail_3" },
  "window_end":   { "kind": "trigger", "trigger_id": "t_tpa_reached" } }

swift_classifier escape hatch

swift_classifier

→ DeclarativeGradingRule.swiftClassifier

The deliberate escape hatch for a real shape that doesn't fit "trigger + field + tolerance" — today only "hold_entry" exists, reading the already-graded hold-entry classification (teardrop/parallel/direct) rather than re-running that geometric classifier. Adding a new classifier key requires a Swift change (DeclarativeGrading.classifiers); this rule kind just exposes whatever's registered there to JSON.

classifier_idrequiredstring — a registered key.
fix_identoptionalstring — narrows to one hold when a plan has more than one; omitted matches the first graded entry.
{ "id": "r_entry", "label": "Correct hold entry", "kind": "swift_classifier", "classifier_id": "hold_entry" }

Objectives live checklist

A lesson's own live, in-flight objectives checklist (ScriptImport.File.objectives) — carried in the same file as triggers/grading, same reasoning: one file, not two composed across a shared id map. Source: ScriptImport.makeObjectives in CoachApp/ATC/ScriptImport+Objectives.swift.

Objective
textrequiredstring — shown in the objectives panel.
for_triggerrequiredstring — matched against a trigger's name (not its id) via LessonManager.firedTriggerNames. A typo or later rename just never completes — same silent-no-match failure mode a stale string already has, not a new risk.
highlightedoptional, default falsebool — renders in orange.
expected_delta_secondsoptionalnumber — seconds normally expected since the PREVIOUS objective's real completion (or mission start, for the first). Omitted opts this objective out of timeout checking. If real elapsed time exceeds 200% of this budget, the pilot gets a "possible deviation" nudge.
{ "text": "Reach pattern altitude.", "for_trigger": "Reached pattern altitude" }