harn-stdlib 0.8.34

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
import {
  agent_emit_event,
  agent_session_apply_reminder_post_turn,
  agent_session_inject_feedback,
} from "std/agent/state"

fn __default_done_sentinel(opts) {
  return opts?.done_sentinel
}

fn __sentinel_hit(text, sentinel, parsed_done_marker) {
  if sentinel == nil {
    return false
  }
  if parsed_done_marker != nil && parsed_done_marker != "" {
    return contains(parsed_done_marker, sentinel)
  }
  return contains(text, sentinel)
}

fn __dispatch_results_list(dispatch) {
  if dispatch == nil {
    return []
  }
  if type_of(dispatch) == "list" {
    return dispatch
  }
  return dispatch?.results ?? []
}

fn __tool_result_ok(result) {
  if result?.ok != nil {
    return result.ok ? true : false
  }
  if result?.success != nil {
    return result.success ? true : false
  }
  let status = result?.status ?? ""
  return status == "ok" || status == "success"
}

fn __tool_result_name(result) {
  return result?.tool_name ?? result?.name ?? ""
}

fn __tool_names_by_status(dispatch, want_ok) {
  let results = __dispatch_results_list(dispatch)
  var names = []
  for result in results {
    let name = __tool_result_name(result)
    if name != "" && __tool_result_ok(result) == want_ok {
      names = names.push(name)
    }
  }
  return names
}

fn __post_turn_callback_verdict(opts, payload) {
  let cb = opts?.post_turn_callback
  if cb == nil {
    return {kind: "none"}
  }
  let verdict = cb(payload)
  return __interpret_verdict(verdict)
}

fn __interpret_verdict(verdict) {
  if verdict == nil || verdict == "" {
    return {kind: "none"}
  }
  if type_of(verdict) == "bool" {
    return if verdict {
      {kind: "stop"}
    } else {
      {kind: "none"}
    }
  }
  if type_of(verdict) == "string" {
    return {kind: "inject", message: verdict}
  }
  if type_of(verdict) == "dict" {
    let stop = verdict?.stop ?? false
    let base_llm_options = if type_of(verdict?.llm_options) == "dict" {
      verdict.llm_options
    } else {
      {}
    }
    let llm_options = if verdict?.prefill != nil && verdict?.prefill != "" {
      base_llm_options + {prefill: verdict?.prefill}
    } else {
      verdict?.llm_options
    }
    return {
      kind: "rich",
      stop: stop,
      stop_reason: verdict?.stop_reason,
      message: verdict?.message,
      next_options: verdict?.next_options,
      llm_options: llm_options,
    }
  }
  return {kind: "none"}
}

fn __successful_tool_names(dispatch) {
  return __tool_names_by_status(dispatch, true)
}

fn __rejected_tool_names(dispatch) {
  return __tool_names_by_status(dispatch, false)
}

fn __stop_after_successful_tools(opts, dispatch) {
  let names = opts?.stop_after_successful_tools
  if names == nil || len(names) == 0 {
    return false
  }
  let successful = __successful_tool_names(dispatch)
  for required in names {
    if contains(successful, required) {
      return true
    }
  }
  return false
}

/**
 * The classic gate for "prose without tool_calls = natural completion":
 * loop_until_done with native tools, no explicit completion signal, and
 * non-empty visible text without tool_calls. Shared between the
 * completion check and the nudge-injection check so they cannot drift.
 */
fn __native_completion_context(opts, has_tool_calls, text) {
  if !(opts?.loop_until_done ?? false) || opts?.daemon {
    return false
  }
  if opts?.tool_format != "native" || opts?.tools == nil {
    return false
  }
  if __default_done_sentinel(opts) != nil {
    return false
  }
  if has_tool_calls {
    return false
  }
  return trim(text) != ""
}

/**
 * Narrows `__native_completion_context` to the regression-prone case:
 * the model is "done" but has emitted ZERO tool_calls so far this
 * session (successful OR rejected — a failed tool call still shows
 * the model engaged the tool channel). This is the documented Qwen3
 * thinking + native tools failure signature (QwenLM/Qwen3.6 #89):
 * reasoning trace narrates tool intent ("let me run git..."), but
 * the structured tool_calls channel emits nothing at all. Once the
 * model has used the tool channel even once (even unsuccessfully),
 * subsequent prose-only turns are legitimate final-answer signals
 * and the classic behavior applies.
 */
fn __zero_tool_completion_context(opts, has_tool_calls, text) {
  if !__native_completion_context(opts, has_tool_calls, text) {
    return false
  }
  let successful = len(opts?._session_successful_tools ?? [])
  let rejected = len(opts?._session_rejected_tools ?? [])
  return successful + rejected == 0
}

/**
 * In the classic native-completion context, prose-without-tool_calls
 * means "I'm done." The one regression-prone subcase is the
 * zero-tools-yet case, where we require ONE confirmation nudge before
 * accepting prose as completion: `__consecutive_text_only` must be >= 2
 * (i.e. the model emitted prose, got the nudge, then emitted prose
 * again). After the nudge the model either recovers (calls a tool,
 * resetting the counter) or confirms (raising it to 2, which we
 * accept). The outer loop's `max_nudges` budget still caps the loop.
 */
fn __native_tool_text_completion(opts, has_tool_calls, text) {
  if !__native_completion_context(opts, has_tool_calls, text) {
    return false
  }
  if !__zero_tool_completion_context(opts, has_tool_calls, text) {
    return true
  }
  return opts?._consecutive_text_only ?? 1 >= 2
}

/**
 * Inject the one-shot completion-confirmation nudge when we're in the
 * regression-prone subcase (zero tools called, prose-only) and haven't
 * already nudged this session. Keyed on `_consecutive_text_only` so it
 * is idempotent under repeated post-turn calls.
 */
fn __maybe_inject_completion_confirmation(session, opts, has_tool_calls, text, iteration) {
  if !__zero_tool_completion_context(opts, has_tool_calls, text) {
    return
  }
  if opts?._consecutive_text_only ?? 1 >= 2 {
    return
  }
  agent_session_inject_feedback(
    session.session_id,
    "completion_confirmation",
    "You ended your turn without calling a tool. If you intended to call a tool, call it now. If this was your final answer, restate it briefly on the next line — do not describe your process.",
  )
  agent_emit_event(
    session.session_id,
    "completion_confirmation_nudge",
    {iteration: iteration, visible_text_prefix: visible_text_excerpt(text)},
  )
}

fn __done_judge_config(opts) {
  let judge = opts?.done_judge
  if judge == nil {
    return nil
  }
  if type_of(judge) == "bool" {
    return if judge {
      {}
    } else {
      nil
    }
  }
  if type_of(judge) == "dict" {
    return judge
  }
  return nil
}

fn __done_judge_cadence(opts) {
  let judge = __done_judge_config(opts)
  if judge == nil {
    return nil
  }
  let cadence = judge?.cadence
  if type_of(cadence) == "dict" {
    return cadence
  }
  return {}
}

fn __done_judge_loop_state(opts, completion_proposed) {
  let state = opts?._done_judge_loop_state ?? {}
  let base_completion = state?.completion ?? {}
  let completion = base_completion + {proposed: completion_proposed}
  return state + {completion: completion}
}

fn __done_judge_when_due(opts, cadence, state) {
  let when = cadence?.when ?? "always"
  if type_of(when) == "closure" {
    return when(state) ? true : false
  }
  if when == "always" {
    return true
  }
  if when == "stalled" {
    let trigger = opts?._done_judge_trigger ?? ""
    let stalled = state?.stall?.triggered ?? false
    return trigger == "stalled" || stalled
  }
  return false
}

fn __done_judge_every_due(cadence, turn_number) {
  let every = cadence?.every
  if every == nil {
    return true
  }
  return turn_number % every == 0
}

fn __done_judge_past_warmup(cadence, turn_number) {
  let min_iterations = cadence?.min_iterations_before_first
  if min_iterations == nil {
    return true
  }
  return turn_number > min_iterations
}

fn __done_judge_under_cap(opts, cadence) {
  let max_invocations = cadence?.max_invocations
  if max_invocations == nil {
    return true
  }
  let invocations = opts?._done_judge_invocations ?? 0
  return invocations < max_invocations
}

fn __done_judge_due(opts, payload, completion_proposed) {
  let cadence = __done_judge_cadence(opts)
  if cadence == nil {
    return false
  }
  let raw_iteration = payload?.iteration ?? 0
  let turn_number = raw_iteration + 1
  let state = __done_judge_loop_state(opts, completion_proposed)
  return __done_judge_under_cap(opts, cadence)
    && __done_judge_past_warmup(cadence, turn_number)
    && __done_judge_every_due(cadence, turn_number)
    && __done_judge_when_due(opts, cadence, state)
}

fn __completion_result(opts, payload, stop_reason) {
  let verify_due = opts?.verify_completion != nil || opts?.verify_completion_judge != nil
  let done_due = __done_judge_due(opts, payload, true)
  if verify_due || done_due {
    return {kind: "break", stop_reason: stop_reason, needs_verify: true, done_judge_due: done_due}
  }
  if __done_judge_config(opts) != nil {
    return {kind: "continue", done_judge_due: false}
  }
  return {kind: "break", stop_reason: stop_reason, needs_verify: false, done_judge_due: false}
}

fn __post_turn_verdict_result(session, opts, verdict) {
  if verdict.kind == "stop" {
    return {kind: "break", stop_reason: "post_turn_stop", needs_verify: opts?.verify_completion != nil}
  }
  if verdict.kind == "inject" {
    agent_session_inject_feedback(session.session_id, "post_turn", verdict.message)
    return {kind: "continue"}
  }
  if verdict.kind != "rich" {
    return nil
  }
  if verdict.message != nil {
    agent_session_inject_feedback(session.session_id, "post_turn", verdict.message)
  }
  if verdict.stop {
    let stop_reason = if verdict?.stop_reason != nil && verdict.stop_reason != "" {
      to_string(verdict.stop_reason)
    } else {
      "post_turn_stop"
    }
    return {
      kind: "break",
      stop_reason: stop_reason,
      needs_verify: opts?.verify_completion != nil,
      next_options: verdict?.next_options,
      llm_options: verdict?.llm_options,
    }
  }
  if verdict.message != nil || verdict?.next_options != nil || verdict?.llm_options != nil {
    return {kind: "continue", next_options: verdict?.next_options, llm_options: verdict?.llm_options}
  }
  return nil
}

/**
 * agent_extract_tool_calls.
 *
 * @effects: []
 * @allocation: heap
 * @errors: []
 * @api_stability: experimental
 * @example: agent_extract_tool_calls(llm_result, opts)
 */
pub fn agent_extract_tool_calls(llm_result, opts) {
  if len(llm_result?.tool_calls ?? []) > 0 {
    return llm_result.tool_calls
  }
  if opts?.tools == nil {
    return []
  }
  let parsed = agent_parse_tool_calls(llm_result?.text ?? "", opts.tools)
  return parsed?.calls ?? []
}

/**
 * agent_compute_post_turn.
 *
 * @effects: [host]
 * @allocation: heap
 * @errors: []
 * @api_stability: experimental
 * @example: agent_compute_post_turn(session, llm_result, dispatch, opts, iteration)
 */
pub fn agent_compute_post_turn(session, llm_result, dispatch, opts, iteration) {
  let sentinel = __default_done_sentinel(opts)
  let text = llm_result?.text ?? ""
  let sentinel_text = llm_result?.raw_text ?? text
  let parsed_done_marker = llm_result?.parsed_done_marker
  let dispatch_results = __dispatch_results_list(dispatch)
  let successful_tool_names = __successful_tool_names(dispatch)
  let rejected_tool_names = __rejected_tool_names(dispatch)
  let has_tool_calls = len(llm_result?.tool_calls ?? []) > 0 || len(dispatch_results) > 0
  let payload = {
    session_id: session.session_id,
    session: {id: session.session_id},
    iteration: iteration,
    has_tool_calls: has_tool_calls,
    dispatch: dispatch,
    tool_results: dispatch_results,
    tool_count: len(dispatch_results),
    successful_tool_names: successful_tool_names,
    rejected_tool_names: rejected_tool_names,
    session_successful_tools: opts?._session_successful_tools ?? successful_tool_names,
    session_rejected_tools: opts?._session_rejected_tools ?? rejected_tool_names,
    text: text,
    visible_text: text,
  }
  __host_fire_session_hook("post_turn", payload)
  agent_session_apply_reminder_post_turn(session.session_id, iteration)
  if __sentinel_hit(sentinel_text, sentinel, parsed_done_marker) {
    return __completion_result(opts, payload, "sentinel")
  }
  if __stop_after_successful_tools(opts, dispatch) {
    return {kind: "break", stop_reason: "natural", needs_verify: false, done_judge_due: false}
  }
  let verdict = __post_turn_callback_verdict(opts, payload)
  let verdict_result = __post_turn_verdict_result(session, opts, verdict)
  if verdict_result != nil {
    return verdict_result
  }
  if !has_tool_calls && trim(text) != "" && opts?.done_judge != nil {
    return __completion_result(opts, payload, "natural")
  }
  if __native_tool_text_completion(opts, has_tool_calls, text) {
    return __completion_result(opts, payload, "natural")
  }
  let loop_until_done = opts?.loop_until_done ?? false
  let daemon = opts?.daemon ?? false
  if !has_tool_calls && !loop_until_done && !daemon {
    return __completion_result(opts, payload, "natural")
  }
  __maybe_inject_completion_confirmation(session, opts, has_tool_calls, text, iteration)
  return {kind: "continue", done_judge_due: false}
}

fn visible_text_excerpt(text) {
  let trimmed = trim(text ?? "")
  if len(trimmed) <= 160 {
    return trimmed
  }
  return trimmed[0:160] + "…"
}