harn-stdlib 0.10.72

Embedded Harn standard library source catalog
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/** Provider-neutral contracts for consequential external effects. */
pub type ExternalActionEnvironment = "mock" | "test" | "live"

pub type ExternalActionOutcome = "confirmed" | "denied" | "failed_before_dispatch" | "indeterminate"

pub type ExternalActionReceiptStatus = "confirmed" \
  | "denied" \
  | "failed_before_dispatch" \
  | "reconciliation_required"

pub type ExternalActionNextAction = "none" | "reconcile"

pub type ExternalActionAuthorizationMethod = "manual" \
  | "policy" \
  | "adversarial_auto" \
  | "managed_policy" \
  | "test_fixture"

pub type ExternalActionAuthenticationAssurance = "none" | "session" | "biometric" | "managed"

pub type ExternalActionActor = {kind: string, id: string}

pub type ExternalActionMoney = {currency: string, amount_minor: int}

pub type ExternalActionDisplay = {summary: string, details?: list<string>}

pub type ExternalActionIntent = {
  schema: "harn.external_action_intent.v1",
  id: string,
  fingerprint: string,
  idempotency_key: string,
  actor: ExternalActionActor,
  provider: string,
  capability: string,
  operation: string,
  environment: ExternalActionEnvironment,
  payload: unknown,
  external_spend?: ExternalActionMoney,
  display: ExternalActionDisplay,
}

pub type ExternalActionGrant = {
  schema: "harn.external_action_grant.v1",
  id: string,
  intent_fingerprint: string,
  actor: ExternalActionActor,
  provider: string,
  capability: string,
  environment: ExternalActionEnvironment,
  authorized_by: ExternalActionActor,
  authorization_method: ExternalActionAuthorizationMethod,
  authentication_assurance: ExternalActionAuthenticationAssurance,
  issued_at_ms: int,
  expires_at_ms: int,
  max_external_spend?: ExternalActionMoney,
}

pub type ExternalActionErrorKind = "invalid_intent" \
  | "invalid_grant" \
  | "adapter_unavailable" \
  | "adapter_failure" \
  | "malformed_adapter_result" \
  | "invalid_reconciliation"

pub type ExternalActionError = {
  kind: ExternalActionErrorKind,
  code: string,
  message: string,
  retryable: bool,
}

/** The only response shape an external provider adapter may return. */
pub type ExternalActionAdapterOutcome = "confirmed" | "failed_before_dispatch" | "indeterminate"

pub type ExternalActionAdapterResult = {
  outcome: ExternalActionAdapterOutcome,
  provider_action_id?: string,
  evidence_refs?: list<string>,
  error_code?: string,
}

pub type ExternalActionDispatchRequest = {
  schema: "harn.external_action_dispatch_request.v1",
  intent: ExternalActionIntent,
  grant: ExternalActionGrant,
  idempotency_key: string,
}

pub type ExternalActionReconcileRequest = {
  schema: "harn.external_action_reconcile_request.v1",
  intent: ExternalActionIntent,
  grant: ExternalActionGrant,
  receipt: ExternalActionReceipt,
  attempt_id: string,
}

pub type ExternalActionAdapter = {
  id: string,
  dispatch: fn(Harness, ExternalActionDispatchRequest) -> ExternalActionAdapterResult,
  reconcile: fn(Harness, ExternalActionReconcileRequest) -> ExternalActionAdapterResult,
}

pub type ExternalActionReceipt = {
  schema: "harn.external_action_receipt.v1",
  id: string,
  action_id: string,
  intent_fingerprint: string,
  idempotency_key: string,
  provider: string,
  capability: string,
  operation: string,
  environment: ExternalActionEnvironment,
  adapter_id: string,
  outcome: ExternalActionOutcome,
  status: ExternalActionReceiptStatus,
  next_action: ExternalActionNextAction,
  dispatch_attempted: bool,
  recorded_at_ms: int,
  provider_action_id?: string,
  evidence_refs: list<string>,
  error?: ExternalActionError,
  reconciliation?: {attempt_id: string, previous_receipt_id: string},
}

/**
 * Build a stable external-action error for hosts and adapters.
 *
 * @effects: []
 * @errors: []
 */
pub fn external_action_error(
  kind: ExternalActionErrorKind,
  code: string,
  message: string,
  retryable: bool = false,
) -> ExternalActionError {
  return {kind: kind, code: code, message: message, retryable: retryable}
}

fn __external_action_non_empty(value, field: string) -> Result<string, ExternalActionError> {
  const text = trim(to_string(value ?? ""))
  if text == "" {
    return Err(external_action_error("invalid_intent", "missing_" + field, field + " is required"))
  }
  return Ok(text)
}

fn __external_action_actor_result(
  raw: unknown,
) -> Result<ExternalActionActor, ExternalActionError> {
  if type_of(raw) != "dict" {
    return Err(external_action_error("invalid_intent", "invalid_actor", "actor must be an object"))
  }
  const kind = __external_action_non_empty(raw?.kind, "actor_kind")
  const id = __external_action_non_empty(raw?.id, "actor_id")
  if !is_ok(kind) {
    return Err(unwrap_err(kind))
  }
  if !is_ok(id) {
    return Err(unwrap_err(id))
  }
  return Ok({kind: unwrap(kind), id: unwrap(id)})
}

fn __external_action_environment_result(
  raw,
) -> Result<ExternalActionEnvironment, ExternalActionError> {
  const environment = lowercase(trim(to_string(raw ?? "")))
  if environment == "mock" || environment == "test" || environment == "live" {
    return Ok(environment)
  }
  return Err(
    external_action_error(
      "invalid_intent",
      "invalid_environment",
      "environment must be mock, test, or live",
    ),
  )
}

fn __external_action_money_result(
  raw: unknown,
) -> Result<ExternalActionMoney?, ExternalActionError> {
  if raw == nil {
    return Ok(nil)
  }
  if type_of(raw) != "dict" {
    return Err(
      external_action_error(
        "invalid_intent",
        "invalid_external_spend",
        "external spend must be an object",
      ),
    )
  }
  const currency = uppercase(trim(to_string(raw?.currency ?? "")))
  if regex_match("^[A-Z]{3}$", currency) == nil {
    return Err(
      external_action_error(
        "invalid_intent",
        "invalid_currency",
        "currency must be an ISO 4217 code",
      ),
    )
  }
  if type_of(raw?.amount_minor) != "int" || raw.amount_minor < 0 {
    return Err(
      external_action_error(
        "invalid_intent",
        "invalid_amount_minor",
        "amount_minor must be a non-negative integer",
      ),
    )
  }
  return Ok({currency: currency, amount_minor: raw.amount_minor})
}

fn __external_action_display(raw: unknown, operation: string) -> ExternalActionDisplay {
  const source = type_of(raw) == "dict" ? raw : {}
  const summary = trim(to_string(source?.summary ?? operation))
  let display: ExternalActionDisplay = {summary: summary == "" ? operation : summary}
  if type_of(source?.details) == "list" {
    let details: list<string> = []
    for item in source.details {
      const text = trim(to_string(item))
      if text != "" && len(text) <= 512 {
        details = details + [text]
      }
    }
    if len(details) > 0 {
      display.details = details
    }
  }
  return display
}

fn __external_action_fingerprint_payload(
  actor: ExternalActionActor,
  provider: string,
  capability: string,
  operation: string,
  environment: ExternalActionEnvironment,
  payload: unknown,
  external_spend: ExternalActionMoney?,
) -> dict {
  return {
    schema: "harn.external_action_fingerprint.v1",
    actor: actor,
    provider: provider,
    capability: capability,
    operation: operation,
    environment: environment,
    payload: payload,
    external_spend: external_spend,
  }
}

/**
 * Validate and normalize an untrusted proposed external effect once.
 *
 * @effects: []
 * @errors: []
 */
pub fn external_action_intent_result(
  raw: unknown,
) -> Result<ExternalActionIntent, ExternalActionError> {
  if type_of(raw) != "dict" {
    return Err(external_action_error("invalid_intent", "invalid_shape", "intent must be an object"))
  }
  const actor = __external_action_actor_result(raw?.actor)
  const provider = __external_action_non_empty(raw?.provider, "provider")
  const capability = __external_action_non_empty(raw?.capability, "capability")
  const operation = __external_action_non_empty(raw?.operation, "operation")
  const environment = __external_action_environment_result(raw?.environment)
  const money = __external_action_money_result(raw?.external_spend)
  for checked in [actor, provider, capability, operation, environment, money] {
    if !is_ok(checked) {
      return Err(unwrap_err(checked))
    }
  }
  const normalized_actor = unwrap(actor)
  const normalized_provider = lowercase(unwrap(provider))
  const normalized_capability = lowercase(unwrap(capability))
  const normalized_operation = lowercase(unwrap(operation))
  const normalized_environment = unwrap(environment)
  const normalized_money = unwrap(money)
  const payload = raw?.payload
  const fingerprint = "sha256:"
    + sha256(
    json_stringify(
      __external_action_fingerprint_payload(
        normalized_actor,
        normalized_provider,
        normalized_capability,
        normalized_operation,
        normalized_environment,
        payload,
        normalized_money,
      ),
    ),
  )
  let intent: ExternalActionIntent = {
    schema: "harn.external_action_intent.v1",
    id: "action_" + substring(fingerprint, 7, 31),
    fingerprint: fingerprint,
    idempotency_key: "harn:" + substring(fingerprint, 7, len(fingerprint)),
    actor: normalized_actor,
    provider: normalized_provider,
    capability: normalized_capability,
    operation: normalized_operation,
    environment: normalized_environment,
    payload: payload,
    display: __external_action_display(raw?.display, normalized_operation),
  }
  if normalized_money != nil {
    intent.external_spend = normalized_money
  }
  return Ok(intent)
}

/**
 * Normalize an action intent or throw its typed validation error.
 *
 * @effects: []
 * @errors: [invalid_intent]
 */
pub fn external_action_intent(raw: unknown) -> ExternalActionIntent {
  const checked = external_action_intent_result(raw)
  if !is_ok(checked) {
    throw unwrap_err(checked)
  }
  return unwrap(checked)
}

/**
 * Return true only when an intent's stored fingerprint still matches its effect.
 *
 * @effects: []
 * @errors: []
 */
pub fn external_action_intent_is_exact(intent: ExternalActionIntent) -> bool {
  const checked = external_action_intent_result(intent)
  return is_ok(checked) && unwrap(checked).fingerprint == intent.fingerprint
}

/**
 * Build an exact, time-bounded authorization for one normalized intent.
 *
 * @effects: []
 * @errors: []
 */
pub fn external_action_grant_result(
  intent: ExternalActionIntent,
  raw: unknown,
) -> Result<ExternalActionGrant, ExternalActionError> {
  if !external_action_intent_is_exact(intent) {
    return Err(
      external_action_error(
        "invalid_grant",
        "intent_fingerprint_mismatch",
        "intent fingerprint does not match its effect",
      ),
    )
  }
  if type_of(raw) != "dict" {
    return Err(external_action_error("invalid_grant", "invalid_shape", "grant must be an object"))
  }
  const authorized_by = __external_action_actor_result(raw?.authorized_by)
  if !is_ok(authorized_by) {
    const cause = unwrap_err(authorized_by)
    return Err(external_action_error("invalid_grant", cause.code, cause.message))
  }
  const method = lowercase(trim(to_string(raw?.authorization_method ?? "")))
  if method != "manual" && method != "policy" && method != "adversarial_auto"
    && method
    != "managed_policy"
    && method != "test_fixture" {
    return Err(
      external_action_error(
        "invalid_grant",
        "invalid_authorization_method",
        "authorization method is unsupported",
      ),
    )
  }
  const assurance = lowercase(trim(to_string(raw?.authentication_assurance ?? "none")))
  if assurance != "none" && assurance != "session" && assurance != "biometric"
    && assurance
    != "managed" {
    return Err(
      external_action_error(
        "invalid_grant",
        "invalid_authentication_assurance",
        "authentication assurance is unsupported",
      ),
    )
  }
  if type_of(raw?.issued_at_ms) != "int" || type_of(raw?.expires_at_ms) != "int"
    || raw.expires_at_ms
    <= raw
    .issued_at_ms {
    return Err(
      external_action_error(
        "invalid_grant",
        "invalid_expiry",
        "grant expiry must follow its issue time",
      ),
    )
  }
  const ceiling = __external_action_money_result(raw?.max_external_spend)
  if !is_ok(ceiling) {
    const cause = unwrap_err(ceiling)
    return Err(external_action_error("invalid_grant", cause.code, cause.message))
  }
  if intent.environment == "live" && method == "test_fixture" {
    return Err(
      external_action_error(
        "invalid_grant",
        "test_grant_for_live_action",
        "test fixtures cannot authorize live effects",
      ),
    )
  }
  const normalized_ceiling = unwrap(ceiling)
  const identity = {
    intent_fingerprint: intent.fingerprint,
    authorized_by: unwrap(authorized_by),
    authorization_method: method,
    authentication_assurance: assurance,
    issued_at_ms: raw.issued_at_ms,
    expires_at_ms: raw.expires_at_ms,
    max_external_spend: normalized_ceiling,
  }
  let grant: ExternalActionGrant = {
    schema: "harn.external_action_grant.v1",
    id: "grant_" + substring(sha256(json_stringify(identity)), 0, 24),
    intent_fingerprint: intent.fingerprint,
    actor: intent.actor,
    provider: intent.provider,
    capability: intent.capability,
    environment: intent.environment,
    authorized_by: unwrap(authorized_by),
    authorization_method: method,
    authentication_assurance: assurance,
    issued_at_ms: raw.issued_at_ms,
    expires_at_ms: raw.expires_at_ms,
  }
  if normalized_ceiling != nil {
    grant.max_external_spend = normalized_ceiling
  }
  return Ok(grant)
}

/**
 * Normalize an action grant or throw its typed validation error.
 *
 * @effects: []
 * @errors: [invalid_grant]
 */
pub fn external_action_grant(intent: ExternalActionIntent, raw: unknown) -> ExternalActionGrant {
  const checked = external_action_grant_result(intent, raw)
  if !is_ok(checked) {
    throw unwrap_err(checked)
  }
  return unwrap(checked)
}

/**
 * Verify every persisted grant field against its canonical construction.
 *
 * @effects: []
 * @errors: []
 */
pub fn external_action_grant_integrity_check(
  intent: ExternalActionIntent,
  grant: ExternalActionGrant,
) -> Result<ExternalActionGrant, ExternalActionError> {
  const normalized = external_action_grant_result(intent, grant)
  if !is_ok(normalized) {
    return Err(unwrap_err(normalized))
  }
  if unwrap(normalized) != grant {
    return Err(
      external_action_error(
        "invalid_grant",
        "grant_integrity_mismatch",
        "grant fields do not match their canonical authorization",
      ),
    )
  }
  return normalized
}

/**
 * Check the exact binding and effective monetary ceiling at dispatch time.
 *
 * @effects: []
 * @errors: []
 */
pub fn external_action_grant_check(
  intent: ExternalActionIntent,
  grant: ExternalActionGrant,
  now_ms: int,
) -> Result<ExternalActionGrant, ExternalActionError> {
  if !external_action_intent_is_exact(intent) {
    return Err(
      external_action_error(
        "invalid_grant",
        "intent_fingerprint_mismatch",
        "intent fingerprint does not match its effect",
      ),
    )
  }
  if grant.schema != "harn.external_action_grant.v1"
    || grant.intent_fingerprint != intent.fingerprint
    || grant.actor != intent.actor
    || grant.provider != intent.provider
    || grant.capability != intent.capability
    || grant.environment != intent.environment {
    return Err(
      external_action_error(
        "invalid_grant",
        "grant_binding_mismatch",
        "grant does not authorize this exact action",
      ),
    )
  }
  const integrity = external_action_grant_integrity_check(intent, grant)
  if !is_ok(integrity) {
    return Err(unwrap_err(integrity))
  }
  if now_ms < grant.issued_at_ms || now_ms >= grant.expires_at_ms {
    return Err(
      external_action_error("invalid_grant", "grant_expired", "grant is not currently valid"),
    )
  }
  if intent.external_spend != nil {
    const ceiling = grant.max_external_spend
    if ceiling == nil || ceiling.currency != intent.external_spend.currency
      || ceiling.amount_minor
      < intent
      .external_spend.amount_minor {
      return Err(
        external_action_error(
          "invalid_grant",
          "external_spend_exceeds_grant",
          "external spend exceeds the authorized ceiling",
        ),
      )
    }
  }
  return Ok(grant)
}