11package main
22
3+ // Medior is a reference Arcling model for the care-continuity bundle case.
4+ // It derives active needs from coarse signals, selects the lowest-cost eligible
5+ // package, and emits ARC text or a JSON report.
6+
37import (
48 "crypto/hmac"
59 "crypto/sha256"
@@ -13,6 +17,7 @@ import (
1317 "time"
1418)
1519
20+ // Data mirrors the input instance shape from medior.data.json.
1621type Data struct {
1722 CaseName string `json:"caseName"`
1823 Region string `json:"region"`
@@ -234,6 +239,8 @@ func parseTime(value string) time.Time {
234239 return t
235240}
236241
242+ // stableStringify recursively sorts map keys so the canonical envelope stays
243+ // byte-stable across runs and languages.
237244func stableStringify (value any ) string {
238245 switch v := value .(type ) {
239246 case nil :
@@ -261,13 +268,16 @@ func stableStringify(value any) string {
261268 }
262269}
263270
271+ // canonicalValue converts typed structs into generic JSON-like values before
272+ // stable stringification.
264273func canonicalValue (value any ) any {
265274 b , _ := json .Marshal (value )
266275 var out any
267276 _ = json .Unmarshal (b , & out )
268277 return out
269278}
270279
280+ // validateInstance performs the structural checks for the 4-file bundle.
271281func validateInstance (data Data ) error {
272282 if err := assertTrue (data .CaseName != "" , "caseName is required" ); err != nil {
273283 return err
@@ -281,6 +291,7 @@ func validateInstance(data Data) error {
281291 return nil
282292}
283293
294+ // countTrue is used for the active-need threshold logic in the spec.
284295func countTrue (values ... bool ) int {
285296 total := 0
286297 for _ , value := range values {
@@ -291,6 +302,7 @@ func countTrue(values ...bool) int {
291302 return total
292303}
293304
305+ // The R* helpers map directly to named derivation clauses in medior.spec.md.
294306func clauseR1RenalSafetyConcern (data Data ) bool {
295307 return data .Signals .Lab .Egfr < data .Thresholds .EgfrBelow
296308}
@@ -315,6 +327,8 @@ func clauseR6NeedsContinuityBundle(data Data, activeNeedCount int) bool {
315327 return activeNeedCount >= data .Thresholds .ActiveNeedCountAtLeast
316328}
317329
330+ // deriveInsight produces the minimized care-coordination signal shared with
331+ // the recipient.
318332func deriveInsight (data Data ) Insight {
319333 return Insight {
320334 CreatedAt : data .Timestamps .CreatedAt ,
@@ -330,6 +344,7 @@ func deriveInsight(data Data) Insight {
330344 }
331345}
332346
347+ // derivePolicy constructs the usage restrictions paired with the insight.
333348func derivePolicy (data Data ) Policy {
334349 return Policy {
335350 Duty : Duty {
@@ -363,13 +378,17 @@ func derivePolicy(data Data) Policy {
363378 }
364379}
365380
381+ // packageCoversAllActiveNeeds checks whether a candidate package addresses every
382+ // active need in this instance.
366383func packageCoversAllActiveNeeds (pkg Package , renalSafetyConcern , polypharmacyRisk , readmissionHistory , recentDischargeWindow bool ) bool {
367384 return (! renalSafetyConcern || pkg .CoversRenalSafetyConcern ) &&
368385 (! polypharmacyRisk || pkg .CoversPolypharmacyRisk ) &&
369386 (! readmissionHistory || pkg .CoversReadmissionHistory ) &&
370387 (! recentDischargeWindow || pkg .CoversRecentDischargeWindow )
371388}
372389
390+ // clauseS1EligiblePackages filters to packages that both fit the budget and
391+ // cover the active needs.
373392func clauseS1EligiblePackages (data Data , renalSafetyConcern , polypharmacyRisk , readmissionHistory , recentDischargeWindow bool ) []Package {
374393 eligible := make ([]Package , 0 )
375394 for _ , pkg := range data .Packages {
@@ -381,6 +400,8 @@ func clauseS1EligiblePackages(data Data, renalSafetyConcern, polypharmacyRisk, r
381400 return eligible
382401}
383402
403+ // clauseS2RecommendedPackage applies the tie-breaker: choose the lowest-cost
404+ // eligible package after sorting by cost.
384405func clauseS2RecommendedPackage (data Data , renalSafetyConcern , polypharmacyRisk , readmissionHistory , recentDischargeWindow bool ) ([]Package , * Package ) {
385406 eligible := clauseS1EligiblePackages (data , renalSafetyConcern , polypharmacyRisk , readmissionHistory , recentDischargeWindow )
386407 if len (eligible ) == 0 {
@@ -402,6 +423,8 @@ func clauseG3DutyTimely(data Data) bool {
402423 return ! parseTime (data .Timestamps .DutyPerformedAt ).After (parseTime (data .Timestamps .ExpiresAt ))
403424}
404425
426+ // clauseM1CanonicalEnvelope returns both the structured envelope and the
427+ // deterministic string hashed/signed by the integrity clauses.
405428func clauseM1CanonicalEnvelope (data Data ) (Envelope , string ) {
406429 envelope := Envelope {Insight : deriveInsight (data ), Policy : derivePolicy (data )}
407430 return envelope , stableStringify (canonicalValue (envelope ))
@@ -435,6 +458,8 @@ func yesNo(value bool) string {
435458 return "FAIL"
436459}
437460
461+ // evaluate computes all derived facts, governance checks, integrity values,
462+ // and presentation fields expected by medior.expected.json.
438463func evaluate (data Data ) (Result , error ) {
439464 if err := validateInstance (data ); err != nil {
440465 return Result {}, err
@@ -589,6 +614,7 @@ func derefIntString(value *int) string {
589614 return fmt .Sprintf ("%d" , * value )
590615}
591616
617+ // main is the CLI entry point used by the Arcling test runner.
592618func main () {
593619 inputPath := "medior.data.json"
594620 jsonMode := false
0 commit comments