harn-stdlib 0.8.58

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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/**
 * Session-local agent scratchpad helpers.
 *
 * The scratchpad is deliberately small live state: every turn recites the
 * current notes at the prompt tail, while optional reorganization rewrites the
 * notes into a compact, cited form for the next turn.
 */
import { agent_emit_event, agent_session_messages } from "std/agent/state"

fn __scratchpad_default_options(enabled, recite, reorganize_every) {
  return {
    enabled: enabled,
    recite: recite,
    reorganize_every: reorganize_every,
    max_recent_messages: 8,
    schema_retries: 1,
    initial: nil,
    reorganizer: {},
  }
}

fn __scratchpad_bool(label, value, fallback) {
  if value == nil {
    return fallback
  }
  if type_of(value) != "bool" {
    throw "agent_loop: `" + label + "` must be a bool or nil; got " + type_of(value)
  }
  return value
}

fn __scratchpad_nonnegative_int(label, value, fallback) {
  if value == nil {
    return fallback
  }
  if type_of(value) != "int" {
    throw "agent_loop: `" + label + "` must be a non-negative integer or nil; got "
      + type_of(value)
  }
  if value < 0 {
    throw "agent_loop: `" + label + "` must be >= 0"
  }
  return value
}

fn __scratchpad_dict(label, value, fallback) {
  if value == nil {
    return fallback
  }
  if type_of(value) != "dict" {
    throw "agent_loop: `" + label + "` must be a dict or nil; got " + type_of(value)
  }
  return value
}

/**
 * Normalize the public `agent_loop(..., {scratchpad})` option.
 *
 * @effects: []
 * @allocation: heap
 * @errors: [runtime]
 * @api_stability: experimental
 * @example: agent_scratchpad_options({scratchpad: true})
 */
pub fn agent_scratchpad_options(opts = nil) {
  let raw = (opts ?? {})?.scratchpad
  if raw == nil {
    return __scratchpad_default_options(false, false, 0)
  }
  if type_of(raw) == "bool" {
    if !raw {
      return __scratchpad_default_options(false, false, 0)
    }
    return __scratchpad_default_options(true, true, 3)
  }
  if type_of(raw) != "dict" {
    throw "agent_loop: `scratchpad` must be a bool, dict, or nil; got " + type_of(raw)
  }
  let enabled = __scratchpad_bool("scratchpad.enabled", raw?.enabled, true)
  return {
    enabled: enabled,
    recite: __scratchpad_bool("scratchpad.recite", raw?.recite ?? raw?.recitation, true),
    reorganize_every: __scratchpad_nonnegative_int(
      "scratchpad.reorganize_every",
      raw?.reorganize_every ?? raw?.reorg_every ?? raw?.periodic_reorg_every,
      3,
    ),
    max_recent_messages: __scratchpad_nonnegative_int("scratchpad.max_recent_messages", raw?.max_recent_messages, 8),
    schema_retries: __scratchpad_nonnegative_int("scratchpad.schema_retries", raw?.schema_retries ?? raw?.retries, 1),
    initial: raw?.initial,
    reorganizer: __scratchpad_dict(
      "scratchpad.reorganizer",
      raw?.reorganizer ?? raw?.reorganizer_options ?? raw?.llm_options,
      {},
    ),
  }
}

fn __scratchpad_list(value) {
  if type_of(value) == "list" {
    return value
  }
  return []
}

fn __scratchpad_task_ref(task) {
  let text = trim(to_string(task ?? ""))
  if text == "" {
    return nil
  }
  return {
    id: "user:initial",
    kind: "user_input",
    label: "Initial task",
    excerpt: __scratchpad_excerpt(text, 200),
  }
}

fn __scratchpad_initial_goal(task) {
  let text = trim(to_string(task ?? ""))
  if text == "" {
    return []
  }
  return [{text: text, source_ref: "user:initial"}]
}

fn __scratchpad_normalize(pad, task) {
  if type_of(pad) != "dict" {
    throw "agent_loop: `scratchpad.initial` must be a dict when provided"
  }
  let task_ref = __scratchpad_task_ref(task)
  let refs = __scratchpad_list(pad?.refs)
  let normalized_refs = if task_ref == nil || __scratchpad_ref_ids(refs)["user:initial"] {
    refs
  } else {
    [task_ref] + refs
  }
  let goals = __scratchpad_list(pad?.goals)
  return pad
    + {
    schema: pad?.schema ?? "harn.agent_scratchpad.v1",
    goals: if len(goals) > 0 {
      goals
    } else {
      __scratchpad_initial_goal(task)
    },
    open_items: __scratchpad_list(pad?.open_items),
    facts: __scratchpad_list(pad?.facts),
    refs: normalized_refs,
  }
}

fn __scratchpad_empty(task) {
  return __scratchpad_normalize(
    {
      schema: "harn.agent_scratchpad.v1",
      goals: __scratchpad_initial_goal(task),
      open_items: [],
      facts: [],
      refs: [],
    },
    task,
  )
}

/**
 * Initialize a session scratchpad when the `scratchpad` agent_loop option is
 * enabled and the session does not already carry one.
 *
 * @effects: [agent]
 * @allocation: heap
 * @errors: [runtime]
 * @api_stability: experimental
 * @example: agent_scratchpad_init(session, opts)
 */
pub fn agent_scratchpad_init(session, opts) {
  let cfg = agent_scratchpad_options(opts)
  if !cfg.enabled {
    return {ok: true, status: "disabled"}
  }
  let current = agent_session_scratchpad(session.session_id)
  if current != nil {
    return {ok: true, status: "existing", scratchpad: current}
  }
  let pad = if cfg.initial != nil {
    __scratchpad_normalize(cfg.initial, session?.task ?? "")
  } else {
    __scratchpad_empty(session?.task ?? "")
  }
  let result = agent_session_set_scratchpad(
    session.session_id,
    pad,
    {source: "agent_loop", reason: "init", metadata: {task_ref: "user:initial"}},
  )
  return {ok: true, status: "initialized", scratchpad: result.scratchpad, version: result.version}
}

fn __scratchpad_excerpt(value, limit) {
  let text = replace(replace(trim(to_string(value ?? "")), "\n", " "), "\t", " ")
  if len(text) <= limit {
    return text
  }
  if limit <= 3 {
    return substring(text, 0, limit)
  }
  return substring(text, 0, limit - 3) + "..."
}

fn __scratchpad_item_text(item) {
  if type_of(item) == "dict" {
    return trim(to_string(item?.text ?? item?.title ?? item?.note ?? ""))
  }
  return trim(to_string(item))
}

fn __scratchpad_item_source(item) {
  if type_of(item) != "dict" {
    return ""
  }
  return trim(to_string(item?.source_ref ?? item?.source ?? ""))
}

fn __scratchpad_format_items(items, empty_label) {
  let values = __scratchpad_list(items)
  if len(values) == 0 {
    return "- (" + empty_label + ")"
  }
  var lines = []
  for item in values {
    let text = __scratchpad_item_text(item)
    if text == "" {
      continue
    }
    let source = __scratchpad_item_source(item)
    let suffix = if source == "" {
      ""
    } else {
      " [source_ref: " + source + "]"
    }
    lines = lines.push("- " + text + suffix)
  }
  if len(lines) == 0 {
    return "- (" + empty_label + ")"
  }
  return join(lines, "\n")
}

fn __scratchpad_format_refs(refs) {
  let values = __scratchpad_list(refs)
  if len(values) == 0 {
    return "- (none)"
  }
  var lines = []
  for ref in values {
    let id = trim(to_string(ref?.id ?? ""))
    if id == "" {
      continue
    }
    let kind = trim(to_string(ref?.kind ?? "source"))
    let label = __scratchpad_excerpt(ref?.label ?? ref?.excerpt ?? "", 120)
    let suffix = if label == "" {
      ""
    } else {
      ": " + label
    }
    lines = lines.push("- " + id + " (" + kind + ")" + suffix)
  }
  if len(lines) == 0 {
    return "- (none)"
  }
  return join(lines, "\n")
}

fn __scratchpad_render(pad) {
  return "## Agent scratchpad\n"
    + "Use this live working memory before deciding the next action. Keep it compact and preserve source_ref ids.\n\n"
    + "### Goals\n"
    + __scratchpad_format_items(pad?.goals, "none")
    + "\n\n### Open items\n"
    + __scratchpad_format_items(pad?.open_items, "none")
    + "\n\n### Facts\n"
    + __scratchpad_format_items(pad?.facts, "none")
    + "\n\n### Source refs\n"
    + __scratchpad_format_refs(pad?.refs)
}

/**
 * Return the tail system fragment that recites the current scratchpad.
 *
 * @effects: [agent]
 * @allocation: heap
 * @errors: []
 * @api_stability: experimental
 * @example: agent_scratchpad_recitation_fragment(session, opts)
 */
pub fn agent_scratchpad_recitation_fragment(session, opts) {
  let cfg = agent_scratchpad_options(opts)
  if !cfg.enabled || !cfg.recite {
    return nil
  }
  let pad = agent_session_scratchpad(session.session_id)
  if pad == nil {
    return nil
  }
  return {id: "primary:agent_scratchpad", source: "primary", bucket: "after", body: __scratchpad_render(pad)}
}

fn __scratchpad_message_refs(messages, limit) {
  let values = __scratchpad_list(messages)
  let cap = if limit > 0 {
    limit
  } else {
    0
  }
  if cap == 0 || len(values) == 0 {
    return []
  }
  var start = len(values) - cap
  if start < 0 {
    start = 0
  }
  var refs = []
  var index = start
  while index < len(values) {
    let msg = values[index]
    let excerpt = __scratchpad_excerpt(msg?.content ?? msg?.text ?? "", 200)
    if excerpt != "" {
      refs = refs
        .push(
        {id: "turn:" + to_string(index + 1), kind: to_string(msg?.role ?? "message"), label: excerpt},
      )
    }
    index = index + 1
  }
  return refs
}

fn __scratchpad_ref_ids(refs) {
  var out = {}
  for ref in __scratchpad_list(refs) {
    let id = trim(to_string(ref?.id ?? ""))
    if id != "" {
      out = out + {[id]: true}
    }
  }
  return out
}

fn __scratchpad_merge_refs(existing, additions) {
  var refs = []
  var seen = {}
  for ref in __scratchpad_list(existing) {
    let id = trim(to_string(ref?.id ?? ""))
    if id != "" && !seen[id] {
      seen = seen + {[id]: true}
      refs = refs.push(ref)
    }
  }
  for ref in __scratchpad_list(additions) {
    let id = trim(to_string(ref?.id ?? ""))
    if id != "" && !seen[id] {
      seen = seen + {[id]: true}
      refs = refs.push(ref)
    }
  }
  return refs
}

fn __scratchpad_schema() {
  return {
    type: "object",
    required: ["schema", "goals", "open_items", "facts", "refs"],
    additionalProperties: false,
    properties: {
      schema: {type: "string"},
      goals: {
        type: "array",
        items: {
          type: "object",
          required: ["text"],
          additionalProperties: true,
          properties: {text: {type: "string"}, source_ref: {type: "string"}},
        },
      },
      open_items: {
        type: "array",
        items: {
          type: "object",
          required: ["text"],
          additionalProperties: true,
          properties: {text: {type: "string"}, source_ref: {type: "string"}},
        },
      },
      facts: {
        type: "array",
        items: {
          type: "object",
          required: ["text", "source_ref"],
          additionalProperties: true,
          properties: {text: {type: "string"}, source_ref: {type: "string"}},
        },
      },
      refs: {
        type: "array",
        items: {
          type: "object",
          required: ["id", "kind"],
          additionalProperties: true,
          properties: {id: {type: "string"}, kind: {type: "string"}, label: {type: "string"}, excerpt: {type: "string"}},
        },
      },
      dropped: {type: "array", items: {type: "string"}},
    },
  }
}

fn __scratchpad_allowed_ref_lines(refs) {
  var lines = []
  for ref in __scratchpad_list(refs) {
    let id = trim(to_string(ref?.id ?? ""))
    if id == "" {
      continue
    }
    lines = lines.push("- " + id + ": " + __scratchpad_excerpt(ref?.label ?? ref?.excerpt ?? "", 180))
  }
  return join(lines, "\n")
}

fn __scratchpad_reorganize_prompt(current, allowed_refs, context) {
  return "Reorganize the live agent scratchpad.\n\n"
    + "Rules:\n"
    + "- Keep only compact working-memory notes that help the next agent turn.\n"
    + "- Deduplicate repeated items and promote useful observations into facts.\n"
    + "- Drop stale or speculative content.\n"
    + "- Store heavy content by source reference, not by copying it into facts.\n"
    + "- Every fact must cite a source_ref from the allowed refs list.\n"
    + "- Return only JSON matching the schema.\n\n"
    + "Current scratchpad JSON:\n"
    + json_stringify(current)
    + "\n\nAllowed source refs:\n"
    + __scratchpad_allowed_ref_lines(allowed_refs)
    + "\n\nReorganization context:\n"
    + json_stringify(context ?? {})
}

fn __scratchpad_reorganizer_options(opts, cfg) {
  var out = cfg.reorganizer ?? {}
  if out?.provider == nil && opts?.provider != nil {
    out = out + {provider: opts.provider}
  }
  if out?.model == nil && opts?.model != nil {
    out = out + {model: opts.model}
  }
  if out?.temperature == nil {
    out = out + {temperature: 0.0}
  }
  if out?.retries == nil {
    out = out + {retries: cfg.schema_retries}
  }
  if out?.schema_retries == nil {
    out = out + {schema_retries: cfg.schema_retries}
  }
  return out
}

fn __scratchpad_validate_refs(pad, allowed_refs) {
  let allowed = __scratchpad_ref_ids(allowed_refs)
  let returned = __scratchpad_ref_ids(pad?.refs)
  var errors = []
  for ref in __scratchpad_list(pad?.refs) {
    let id = trim(to_string(ref?.id ?? ""))
    if id == "" {
      errors = errors.push("refs entries must have non-empty id")
    } else if !allowed[id] {
      errors = errors.push("refs contains unknown source ref `" + id + "`")
    }
  }
  for fact in __scratchpad_list(pad?.facts) {
    let source = __scratchpad_item_source(fact)
    if source == "" {
      errors = errors.push("facts must include source_ref")
    } else if !returned[source] {
      errors = errors.push("fact cites unknown source_ref `" + source + "`")
    }
  }
  for item in __scratchpad_list(pad?.goals) + __scratchpad_list(pad?.open_items) {
    let source = __scratchpad_item_source(item)
    if source != "" && !returned[source] {
      errors = errors.push("item cites unknown source_ref `" + source + "`")
    }
  }
  return {ok: len(errors) == 0, errors: errors}
}

fn __scratchpad_counts(pad) {
  return {
    goals: len(__scratchpad_list(pad?.goals)),
    open_items: len(__scratchpad_list(pad?.open_items)),
    facts: len(__scratchpad_list(pad?.facts)),
    refs: len(__scratchpad_list(pad?.refs)),
  }
}

fn __scratchpad_emit_reorganization(session_id, iteration, status, payload = nil) {
  let details = payload ?? {}
  agent_emit_event(
    session_id,
    "agent_scratchpad_reorganization",
    {iteration: iteration, status: status} + details,
  )
}

/**
 * Reorganize the session scratchpad through a structured-output LLM call.
 *
 * @effects: [agent, llm]
 * @allocation: heap
 * @errors: []
 * @api_stability: experimental
 * @example: agent_scratchpad_reorganize(session, opts, 1, {reason: "iteration_end"})
 */
pub fn agent_scratchpad_reorganize(session, opts, iteration, context = nil) {
  let cfg = agent_scratchpad_options(opts)
  if !cfg.enabled {
    return {ok: true, status: "disabled"}
  }
  var current = agent_session_scratchpad(session.session_id)
  if current == nil {
    let initialized = agent_scratchpad_init(session, opts)
    current = initialized?.scratchpad
  }
  if current == nil {
    __scratchpad_emit_reorganization(session.session_id, iteration, "missing_scratchpad")
    return {ok: false, status: "missing_scratchpad"}
  }
  let message_refs = __scratchpad_message_refs(agent_session_messages(session.session_id), cfg.max_recent_messages)
  let allowed_refs = __scratchpad_merge_refs(current?.refs, message_refs)
  let envelope = llm_call_structured_result(
    __scratchpad_reorganize_prompt(current, allowed_refs, context),
    __scratchpad_schema(),
    __scratchpad_reorganizer_options(opts, cfg),
  )
  if !envelope.ok {
    __scratchpad_emit_reorganization(
      session.session_id,
      iteration,
      "llm_error",
      {error: to_string(envelope?.error ?? envelope)},
    )
    return {ok: false, status: "llm_error", error: envelope?.error ?? envelope}
  }
  let next = __scratchpad_normalize(envelope.data, session?.task ?? "")
  let validation = __scratchpad_validate_refs(next, allowed_refs)
  if !validation.ok {
    __scratchpad_emit_reorganization(
      session.session_id,
      iteration,
      "invalid_sources",
      {
        errors: validation.errors,
        before_counts: __scratchpad_counts(current),
        after_counts: __scratchpad_counts(next),
      },
    )
    return {
      ok: false,
      status: "invalid_sources",
      errors: validation.errors,
      before_counts: __scratchpad_counts(current),
      after_counts: __scratchpad_counts(next),
    }
  }
  let set_result = try {
    agent_session_set_scratchpad(
      session.session_id,
      next,
      {
        source: "scratchpad_reorganizer",
        reason: "periodic_reorganization",
        metadata: {
          iteration: iteration,
          before_counts: __scratchpad_counts(current),
          after_counts: __scratchpad_counts(next),
        },
      },
    )
  }
  if is_err(set_result) {
    let store_error = to_string(unwrap_err(set_result))
    __scratchpad_emit_reorganization(session.session_id, iteration, "store_error", {error: store_error})
    return {ok: false, status: "store_error", error: store_error}
  }
  let stored = unwrap(set_result)
  __scratchpad_emit_reorganization(
    session.session_id,
    iteration,
    "reorganized",
    {
      version: stored.version,
      before_counts: __scratchpad_counts(current),
      after_counts: __scratchpad_counts(stored.scratchpad),
    },
  )
  return {
    ok: true,
    status: "reorganized",
    scratchpad: stored.scratchpad,
    version: stored.version,
    before_counts: __scratchpad_counts(current),
    after_counts: __scratchpad_counts(stored.scratchpad),
  }
}

/**
 * Reorganize when the configured cadence is due after a completed turn.
 *
 * @effects: [agent, llm]
 * @allocation: heap
 * @errors: []
 * @api_stability: experimental
 * @example: agent_scratchpad_reorganize_if_due(session, opts, 0)
 */
pub fn agent_scratchpad_reorganize_if_due(session, opts, iteration_index, context = nil) {
  let cfg = agent_scratchpad_options(opts)
  if !cfg.enabled {
    return {ok: true, status: "disabled"}
  }
  let cadence = cfg.reorganize_every
  if cadence <= 0 {
    return {ok: true, status: "skipped", reason: "cadence_disabled"}
  }
  let turn = iteration_index + 1
  if turn % cadence != 0 {
    return {ok: true, status: "skipped", reason: "cadence", next_due: turn + (cadence - turn % cadence)}
  }
  return agent_scratchpad_reorganize(session, opts, turn, context ?? {reason: "iteration_end"})
}