-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruntime.py
More file actions
1430 lines (1271 loc) · 60.7 KB
/
runtime.py
File metadata and controls
1430 lines (1271 loc) · 60.7 KB
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
runtime — LLM call interface with automatic DAG integration.
Runtime is a class that wraps an LLM provider. You instantiate it once
with your provider config, then call rt.exec() inside @agentic_functions.
exec() automatically:
1. Builds the prompt's message history from the DAG (the
``_store`` GraphStore the dispatcher installed for this turn)
2. Calls _call() (override this for your provider)
3. Appends a ModelCall node recording the reply into the DAG
Usage:
from openprogram import agentic_function
from openprogram.agentic_programming.runtime import Runtime
rt = Runtime(call=my_llm_func)
# or: subclass Runtime and override _call()
@agentic_function
def observe(task):
'''Look at the screen and describe what you see.'''
return rt.exec(content=[
{"type": "text", "text": "Find the login button."},
{"type": "image", "path": "screenshot.png"},
])
"""
from __future__ import annotations
import asyncio
import contextvars
import inspect
import json
import os
import random
import time
from typing import TYPE_CHECKING, Any, Callable, Optional
if TYPE_CHECKING:
from openprogram.providers.utils.errors import (
ErrorReason, LLMError, RetryInfo,
)
# Backoff base (seconds) between exec() retry attempts. Retries sleep
# _RETRY_BACKOFF * 2**attempt before the next try, with ±25% jitter
# so multiple concurrent retries don't fire in lock-step against the
# same upstream and re-trigger whatever connection-pool / rate-limit
# threshold caused the original failure.
#
# Default 1.5s; ``OPENPROGRAM_RETRY_BACKOFF_BASE`` env overrides so
# deployments with non-default network characteristics (proxied,
# offline-capable, very-low-latency local models) can re-tune without
# touching code.
_RETRY_BACKOFF = float(os.environ.get("OPENPROGRAM_RETRY_BACKOFF_BASE", "1.5"))
def _default_max_retries() -> int:
"""Process-wide default for ``Runtime(max_retries=...)``.
Reads ``OPENPROGRAM_MAX_RETRIES`` lazily on every Runtime
construction so tests / scripts can flip the env after this
module is imported and still see the new value. Falls back to
6 — the legacy hard-coded default (try once + retry five times,
total ≈46s of sleeping at the default backoff base).
"""
try:
v = int(os.environ.get("OPENPROGRAM_MAX_RETRIES", "6"))
except ValueError:
v = 6
return max(1, v)
def _retry_sleep_seconds(attempt: int, retry_after_s: Optional[float] = None) -> float:
"""Exponential backoff + jitter, honoring a server-supplied
``Retry-After`` hint as a lower bound.
Without a hint (default): ``base * 2^attempt`` scaled by
``[0.75, 1.25]`` (symmetric jitter) so a burst of retries spreads
out instead of slamming the upstream simultaneously. Attempt 0
sleeps ~1.5s, attempt 1 ~3s, ..., attempt 5 ~48s.
With a hint (server returned ``Retry-After``): the delay is the
larger of the exponential base and ``retry_after_s``, then scaled
by ``[1.0, 1.25]`` — positive-only jitter so we never wake up
before the server-specified deadline. Honoring the lower bound
matters during rate-limit storms: ±25% symmetric jitter would
let a quarter of retries fire too early, defeating the server's
backpressure and triggering 429 again.
Mirrors OpenClaw's ``computeBackoffDelay`` (references/openclaw/
src/infra/retry.ts) which uses the same "positive-only when
Retry-After present" rule.
"""
base = _RETRY_BACKOFF * (2 ** attempt)
if retry_after_s and retry_after_s > 0:
floor = max(base, retry_after_s)
return floor * random.uniform(1.0, 1.25)
return base * random.uniform(0.75, 1.25)
# Substrings marking a *permanent* provider error. Retrying these only
# burns attempts and wall-clock time — the request is malformed or the
# credentials are bad, so the next identical attempt fails identically.
_PERMANENT_ERROR_MARKERS = (
"not a valid image",
"invalid image",
"image data is not",
"login expired",
"login failed",
"re-auth",
"unauthorized",
"invalid api key",
"invalid_api_key",
)
def _is_permanent_error(exc: Exception) -> bool:
"""True if retrying ``exc`` is pointless (malformed request / bad auth)."""
msg = f"{type(exc).__name__}: {exc}".lower()
return any(marker in msg for marker in _PERMANENT_ERROR_MARKERS)
def _build_llm_error(
*,
cause: BaseException,
attempts: int,
elapsed_s: float,
content: Any,
model: Optional[str],
provider: Optional[str],
history: list[str],
permanent: bool,
override_reason: "Optional[ErrorReason]" = None,
) -> "LLMError": # type: ignore[name-defined]
"""Construct the structured exception ``exec()`` raises when it
gives up.
Collects everything a caller needs to decide "retry the whole
turn", "reauthenticate", "trim prompt", or "circuit-break this
provider":
* ``reason`` — classified by :func:`classify_error`, or forced
via ``override_reason`` (e.g. TIMEOUT for deadline hits where
the underlying cause was incidental, not the real reason
we're giving up).
* ``retryable`` — honest about whether the underlying kind was
transient; ``False`` for permanent failures (auth / invalid
request / context overflow / timeout). Note: ``retryable=True``
means "the kind was transient but we exhausted our budget",
not "you should retry this immediately"
* ``attempts`` / ``elapsed_s`` / ``had_image`` — observability
* ``cause`` — original exception, preserved for traceback
via ``raise ... from cause``
The ``history`` list (per-attempt error strings) is folded into
the message so the LLMError's text is greppable like the old
RuntimeError. Localised to this helper so the two retry loops
stay tidy.
"""
from openprogram.providers.utils.errors import (
LLMError, classify_error, had_image as _had_image,
)
# Try to pull HTTP status from the cause if the provider attached
# it (HTTP providers stash it on the exc via ProviderStreamError).
http_status = getattr(cause, "http_status", None) or getattr(cause, "status_code", None)
retry_after_s = getattr(cause, "retry_after_s", None)
error_text = getattr(cause, "error_text", "") or ""
if override_reason is not None:
reason = override_reason
# An overridden reason (currently only TIMEOUT) is always
# non-retryable in this attempt budget: even if the underlying
# transport error was transient, *we* gave up because of a
# deadline, not because the kind was permanent.
retryable = False
else:
reason, kind_retryable = classify_error(
cause, http_status=http_status, error_text=error_text,
)
# Even if the underlying kind was retryable, when we gave up
# because the budget was exhausted, retryable stays True
# (caller may decide to retry the whole turn with a fresh
# budget). When the failure was permanent, force
# retryable=False regardless of what classify_error said.
retryable = kind_retryable and not permanent
label = "permanently" if permanent else f"after {attempts} attempt(s)"
detail = "\n".join(history) if history else f"{type(cause).__name__}: {cause}"
message = f"exec() failed {label}:\n{detail}"
return LLMError(
message=message,
reason=reason,
retryable=retryable,
http_status=http_status,
retry_after_s=retry_after_s,
attempts=attempts,
elapsed_s=elapsed_s,
had_image=_had_image(content),
provider=provider,
model=model,
last_error_type=type(cause).__name__,
cause=cause,
)
def _fire_on_retry(
on_retry: "Optional[Callable[[RetryInfo], None]]",
*,
cause: BaseException,
attempt: int,
max_attempts: int,
sleep_s: float,
elapsed_s: float,
retry_after_s: Optional[float],
) -> None:
"""Invoke an ``on_retry`` callback safely.
Exceptions inside the callback are swallowed — a broken hook
must never prevent the retry loop from making progress. The
callback receives a fully-populated :class:`RetryInfo`,
classified the same way as the final :class:`LLMError` would
be, so consumers can route on ``info.reason`` without
re-classifying.
"""
if on_retry is None:
return
from openprogram.providers.utils.errors import (
RetryInfo, classify_error, ErrorReason,
)
http_status = getattr(cause, "http_status", None) or getattr(cause, "status_code", None)
reason, _ = classify_error(cause, http_status=http_status,
error_text=getattr(cause, "error_text", "") or "")
info = RetryInfo(
attempt=attempt,
max_attempts=max_attempts,
reason=reason,
sleep_s=sleep_s,
elapsed_s=elapsed_s,
retry_after_s=retry_after_s,
last_error_type=type(cause).__name__,
last_error_msg=str(cause),
)
try:
on_retry(info)
except Exception:
# Don't break the retry loop on a buggy hook. Print once for
# the operator; future identical hook failures stay silent.
import sys as _sys
print(f"[runtime] on_retry callback raised; ignoring: "
f"{type(_sys.exc_info()[1]).__name__}", file=_sys.stderr)
# Context var for the tools passed into the current exec() call.
# _call_via_providers reads it to feed AgentSession without changing
# the _call() signature subclasses override.
_current_tools: contextvars.ContextVar[Optional[list]] = contextvars.ContextVar(
"_current_tools", default=None,
)
# OpenClaw-style tool policy that overlays on top of the chosen tool
# list. Set by callers (dispatcher / channels / runtime.exec kwargs)
# to filter the resolved tools per-call without renaming them. Shape:
# ``{"toolset": "research", "source": "wechat", "allow": [...], "deny": [...]}``.
# Any subset of keys is valid; missing keys mean "no constraint".
_current_tool_policy: contextvars.ContextVar[Optional[dict]] = contextvars.ContextVar(
"_current_tool_policy", default=None,
)
class Runtime:
"""
LLM runtime. Wraps a provider and handles Context integration.
Two ways to use:
1. Pass a call function:
rt = Runtime(call=my_func, model="gpt-4o")
2. Subclass and override _call():
class MyRuntime(Runtime):
def _call(self, content, response_format=None):
# your API logic here
return reply_text
"""
def __init__(
self,
call: Optional[callable] = None,
model: str = "default",
max_retries: Optional[int] = None,
api_key: Optional[str] = None,
skills: "bool | list[str] | None" = None,
):
"""
Args:
call: LLM provider function.
Signature: fn(content: list[dict], model: str, response_format: dict) -> str
If None, the default pi-ai backend is used (when `model`
is "provider:model_id"). Subclasses may override _call().
model: Default model. Two forms:
- "provider:model_id" (e.g. "anthropic:claude-sonnet-4.5")
→ resolved via openprogram.providers; _call() goes
through complete() by default.
- Any other string → legacy path (subclass overrides
_call, or pass a `call` function).
max_retries: Maximum number of exec() attempts before raising.
``None`` (default) → read ``OPENPROGRAM_MAX_RETRIES``
env, fall back to 6. Set explicitly to override
env. 6 means try once + retry five times on
transient failure, with exponential backoff +
±25% jitter — wall-clock at worst ≈ 1.5 + 3 +
6 + 12 + 24 = 46s of sleeping before giving
up (tunable via ``OPENPROGRAM_RETRY_BACKOFF_BASE``).
Permanent errors (bad image, expired auth) are
not retried regardless of this value.
api_key: Optional API key. If omitted, resolved from the
provider's standard env var (OPENAI_API_KEY, etc).
skills: Skill discovery for the system prompt. Three shapes:
- None (default) or False → skills disabled
- True → probe default_skill_dirs() (user + repo)
- list[str] → explicit directory list
When enabled, the <available_skills> block is
appended to system_prompt on every exec() call.
"""
import uuid as _uuid
self._closed = False # Set early so __del__ is safe even if __init__ raises.
self._prompted_functions: set[str] = set() # Functions whose docstrings have been sent
if max_retries is None:
max_retries = _default_max_retries()
if max_retries < 1:
raise ValueError("max_retries must be >= 1")
self._call_fn = call
self.model = model
self.max_retries = max_retries
self.has_session = False # Subclasses set True if they manage their own context
self.on_stream = None # Optional callback: fn(event_dict) for streaming events
self.last_usage = None # Last call's token usage: {input_tokens, output_tokens, ...}
self.usage_is_cumulative = False # True if last_usage accumulates across calls (e.g. Codex CLI)
self.api_key = api_key
# Skills config: resolved to a (possibly empty) list of dirs at
# first use; actual SKILL.md loading is lazy and cached so we
# don't rescan the filesystem every exec().
self._skills_config = skills
self._skills_cache_key: tuple[str, ...] | None = None
self._skills_prompt_block: str = ""
# Unified reasoning knob, matches pi-ai's ThinkingLevel:
# "off" | "low" | "medium" | "high" | "xhigh"
# API runtimes pass this straight through to AgentSession → provider
# SimpleStreamOptions.reasoning. CLI subclasses override however their
# backend expects (flags, env vars, etc).
self.thinking_level: str = "off"
# Stable id across successive exec() calls — provider uses it as
# prompt_cache_key (Codex) so repeat prefixes hit the cache.
self.session_id = f"op-{_uuid.uuid4().hex[:16]}"
# Resolve "provider:model_id" form against the pi-ai model registry.
self.api_model = None
if call is None and isinstance(model, str) and ":" in model:
provider, model_id = model.split(":", 1)
from openprogram.providers import get_model
resolved = get_model(provider, model_id)
if resolved is None:
raise ValueError(
f"Unknown model {provider!r}:{model_id!r}. "
f"Pass `call=`, subclass Runtime, or use a valid pi-ai model id."
)
self.api_model = resolved
# --- Skills ---
def _resolved_skill_dirs(self) -> list[str]:
"""Turn the constructor's ``skills`` argument into a concrete dir list.
None / False → []. True → default dirs. list → as-is.
"""
cfg = self._skills_config
if not cfg:
return []
if cfg is True:
from openprogram.agentic_programming.skills import default_skill_dirs
return default_skill_dirs()
if isinstance(cfg, (list, tuple)):
return [str(d) for d in cfg]
return []
def _skills_block(self) -> str:
"""Return the ``<available_skills>`` XML block for this runtime.
Cached per dir tuple so repeat exec() calls don't rescan unless the
configured dirs change. Empty string when skills are disabled or no
SKILL.md files were found — callers can unconditionally concatenate.
"""
dirs = tuple(self._resolved_skill_dirs())
if self._skills_cache_key == dirs:
return self._skills_prompt_block
if not dirs:
self._skills_cache_key = dirs
self._skills_prompt_block = ""
return ""
from openprogram.agentic_programming.skills import (
format_skills_for_prompt, load_skills,
)
self._skills_prompt_block = format_skills_for_prompt(load_skills(dirs))
self._skills_cache_key = dirs
return self._skills_prompt_block
# --- Path dispatch ---
def _uses_legacy_call(self) -> bool:
"""True if this runtime sends responses through the text-prompt
pathway of ``_call()`` rather than the AgentSession + render_messages
pathway.
Legacy providers (OpenAICodexRuntime, ...) and
user-supplied ``call=`` functions expect a text-merged
``full_content`` list. The default Runtime (``model="provider:id"``)
builds messages directly from the execution tree and ignores
``full_content``.
"""
if self._call_fn is not None:
return True
return type(self)._call is not Runtime._call
def _render_history_messages(self, content) -> Optional[list]:
"""Build the provider message list for an in-progress exec()
from the DAG.
Source of truth: the ``_store`` ContextVar set by the dispatcher
at turn entry (``openprogram.context.storage._store``). When no
store is installed (standalone scripts, tests without the
dispatcher), returns ``None`` so the caller falls back to the
tree-Context render path.
Algorithm:
1. Load the DAG state from the store.
2. Read the enclosing ``@agentic_function`` call id from
``_call_id`` ContextVar; pull its node from the graph to
get seq + render_range.
3. Compute reads → render pi-ai messages.
4. Append a fresh UserMessage built from ``content``.
"""
from openprogram.store import _store
store = _store.get()
if store is None:
return None
try:
from openprogram.context.nodes import compute_reads
from openprogram.context.render import render_dag_messages
from openprogram.providers.types import UserMessage, TextContent
from openprogram.agentic_programming.function import _call_id
graph = store.load()
frame_node_id = _call_id.get()
frame_entry_seq = -1
render_range = None
if frame_node_id and frame_node_id in graph.nodes:
frame_node = graph.nodes[frame_node_id]
frame_entry_seq = frame_node.seq
render_range = (frame_node.metadata or {}).get(
"render_range"
)
head_seq = max(
(n.seq for n in graph.nodes.values()), default=-1,
)
read_ids = compute_reads(
graph,
head_seq=head_seq,
frame_entry_seq=frame_entry_seq,
render_range=render_range,
)
history = render_dag_messages(graph, read_ids)
# Synthesize the current turn from ``content`` blocks. Most
# callers pass a single ``{"type":"text","text":"..."}``
# block; we concatenate all text blocks and skip non-text
# ones for now (multimodal is a future extension).
text_parts: list[str] = []
for block in content or []:
if isinstance(block, dict) and block.get("type") == "text":
text_parts.append(block.get("text", ""))
current_text = "\n".join(p for p in text_parts if p)
import time as _time
current_msg = UserMessage(
role="user",
content=[TextContent(type="text", text=current_text)],
timestamp=int(_time.time() * 1000),
)
return history + [current_msg]
except Exception:
# If anything goes wrong building DAG messages, fall back
# to the legacy render_messages path. Never break exec().
return None
def _append_model_call_node(
self,
*,
reply: str,
model: str,
system_prompt: Optional[str] = None,
content_text: str = "",
) -> None:
"""Append an llm-role Call after a successful provider call.
Writes to the GraphStore the dispatcher installed in ``_store``;
``called_by`` comes from the enclosing ``@agentic_function``
invocation via ``_call_id``. No-op when no store is installed
(standalone scripts).
``reads`` is intentionally left empty for now — wiring the
exact read-id set the prompt consumed is a future refinement.
"""
try:
from openprogram.store import _store
from openprogram.context.nodes import Call, ROLE_LLM
from openprogram.agentic_programming.function import _call_id
store = _store.get()
if store is None:
return
node = Call(
role=ROLE_LLM,
name=model or self.model or "",
input=({"system": system_prompt} if system_prompt else None),
output=reply,
reads=[],
called_by=_call_id.get() or "",
metadata=(
{"prompt_text": content_text[:8000]}
if content_text else {}
),
)
store.append(node)
except Exception:
# DAG bookkeeping failure must not break the LLM call.
pass
# --- Working directory ---
def set_workdir(self, path: str) -> None:
"""Set the provider's working directory.
For runtimes that spawn subprocesses (Codex CLI via --cd), this
determines where shell/tool commands execute and where the LLM
writes relative-path files. Default: no-op — runtimes that don't
spawn subprocesses ignore this.
"""
pass
# --- Lifecycle ---
def close(self):
"""Close this runtime: release resources, kill processes, end session.
After close(), exec() will raise RuntimeError.
Subclasses should override this to clean up provider-specific resources
(kill CLI processes, clear session IDs, etc.) and call super().close().
"""
self.has_session = False
self._prompted_functions.clear()
self._closed = True
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
return False
def __del__(self):
# Defensive: subclasses that raise mid-__init__ never reach
# Runtime.__init__, so `_closed` may be missing on the
# partially-built object the GC eventually reaps. Treat
# missing as already closed.
if not getattr(self, "_closed", True):
self.close()
def exec(
self,
content: list[dict],
context: Optional[str] = None,
response_format: Optional[dict] = None,
model: Optional[str] = None,
tools: Optional[list] = None,
toolset: Optional[str] = None,
tools_source: Optional[str] = None,
tools_allow: Optional[list[str]] = None,
tools_deny: Optional[list[str]] = None,
tool_choice: Any = "auto",
parallel_tool_calls: bool = True,
max_iterations: int = 20,
choices: Any = None,
timeout_s: Optional[float] = None,
on_retry: Optional["Callable[[RetryInfo], None]"] = None,
) -> Any:
"""
Call the LLM. Appends a ModelCall node to the DAG.
Args:
content: List of content blocks. Each block is a dict:
{"type": "text", "text": "..."}
{"type": "image", "path": "screenshot.png"}
{"type": "audio", "path": "recording.wav"}
{"type": "file", "path": "data.csv"}
context: Optional text prefix for the legacy ``_call``
path (``call=`` callable / subclass override).
Ignored on the default AgentSession path,
which builds history from the DAG.
response_format: Expected output format (JSON schema).
Passed to _call() for provider-native handling.
model: Override the default model for this call.
tools: Optional list of tools the LLM may call. Each
entry may be an @agentic_function, a
{"spec":..., "execute":...} dict, or an object
with .spec and .execute attributes. When set,
runs a tool loop until the model returns plain
text (or max_iterations is hit).
tool_choice: "auto" (default), "required", "none", or
{"type":"function","name":"X"} to force a
specific tool.
parallel_tool_calls: allow the model to emit multiple tool calls
in one turn (default True).
max_iterations: safety cap on the tool loop (default 20).
choices: When set, constrains how the turn *finishes*.
The model runs the normal turn (reasoning,
tool calls — whatever ``tools`` allows), but
its final reply must pick one option from
``choices``. The pick is then resolved: a
picked function is run and its return value
handed back, a picked value is returned
as-is. Same option forms as
``decision.make`` — a dict ``{name: handler}``
or a list of callables / option tuples.
timeout_s: Wall-clock deadline for the entire exec()
call, **including all retry sleeps**.
When the elapsed time reaches the deadline,
raises ``LLMError(reason=TIMEOUT,
retryable=False)`` instead of starting
another attempt or sleeping further. The
currently-running ``_call`` is also bounded
when it's async (sync ``_call`` runs to
completion before the check, so very-long
synchronous calls can overshoot — set
``max_retries=1`` if that matters).
``None`` (default): no wall-clock cap,
behaviour is the same as before this knob
existed. Separate from ``max_retries`` —
``max_retries`` is a count cap, this is a
time cap; they compose.
on_retry: Optional ``Callable[[RetryInfo], None]``.
Fired immediately before each backoff
sleep — i.e. once per *failed* attempt
that has a retry queued behind it. Not
fired for the terminal failure that
exhausts the budget (that raises
``LLMError`` instead). Use this to emit
structured retry logs, drive a circuit
breaker, or accumulate per-provider
failure metrics without subclassing
Runtime. Exceptions inside the callback
are swallowed so a broken hook never
prevents the retry loop from making
progress.
Returns:
``str`` — the LLM's reply text. When ``choices`` is set,
returns the resolved decision instead (a function option's
return value, or a value option's value).
"""
if self._closed:
raise RuntimeError("Runtime is closed. Create a new runtime instance.")
# Cancel check — lets long-running loops inside one function also abort.
from openprogram.agentic_programming.function import _run_pre_invocation_hooks
_run_pre_invocation_hooks()
# Handle plain string input
if isinstance(content, str):
content = [{"type": "text", "text": content}]
# --- Choice-constrained finish ---
# When `choices` is set, the model runs a normal turn but must end
# with a pick from the menu. Append the menu + finish instruction
# to the prompt now; the reply is resolved against it below.
_decision_menu = _decision_values = None
if choices is not None:
from openprogram.agentic_programming.decision import (
DECISION_FINISH_INSTRUCTION,
_normalize_options,
render_options,
)
_decision_menu, _decision_values = _normalize_options(choices)
content = list(content) + [{
"type": "text",
"text": DECISION_FINISH_INSTRUCTION + render_options(_decision_menu),
}]
use_model = model or self.model
content_text = "\n".join(b["text"] for b in content if b.get("type") == "text")
# --- Build call input ---
# AgentSession path: _call_via_providers builds its own message
# history from the DAG (via _render_history_messages). Pass
# ``content`` through as-is.
# Legacy path (_call_fn or subclass-overridden _call): prepend
# the caller-supplied ``context`` string + any system prompt
# the runtime is carrying. The caller is responsible for
# composing history themselves; we don't auto-walk a tree.
if self._uses_legacy_call():
call_input = list(content)
if context:
call_input.insert(0, {"type": "text", "text": context})
system_text = getattr(self, "system", "") or ""
skills_block = self._skills_block()
if skills_block:
system_text = (system_text + skills_block) if system_text else skills_block.lstrip("\n")
if system_text:
call_input.insert(0, {"type": "text", "text": system_text, "role": "system"})
else:
call_input = content
# --- Call the LLM (with retry) ---
tools_token = _current_tools.set(tools) if tools else None
_policy_kwargs = {
"toolset": toolset,
"source": tools_source,
"allow": tools_allow,
"deny": tools_deny,
}
_policy_kwargs = {k: v for k, v in _policy_kwargs.items() if v is not None}
policy_token = (
_current_tool_policy.set({**(_current_tool_policy.get(None) or {}), **_policy_kwargs})
if _policy_kwargs else None
)
reply = None
_exec_start = time.monotonic()
_deadline = _exec_start + timeout_s if (timeout_s and timeout_s > 0) else None
try:
errors: list[str] = []
for attempt in range(self.max_retries):
# Pre-attempt deadline check: previous sleep or _call
# may have already crossed the line, in which case we
# don't even start another attempt.
if _deadline is not None and time.monotonic() >= _deadline:
from openprogram.providers.utils.errors import ErrorReason as _ER
cause = TimeoutError(
f"exec() timed out after {timeout_s}s "
f"({attempt} attempt(s))"
)
raise _build_llm_error(
cause=cause, attempts=max(1, attempt),
elapsed_s=time.monotonic() - _exec_start,
content=content, model=use_model,
provider=getattr(self, "provider", None),
history=errors,
permanent=True,
override_reason=_ER.TIMEOUT,
) from cause
try:
reply = self._call(call_input, model=use_model, response_format=response_format)
self._append_model_call_node(
reply=reply,
model=use_model,
content_text=content_text,
)
break
except (TypeError, NotImplementedError):
raise # Programming errors — don't retry
except Exception as e:
errors.append(f"Attempt {attempt + 1}: {type(e).__name__}: {e}")
permanent = _is_permanent_error(e)
elapsed = time.monotonic() - _exec_start
if permanent or attempt == self.max_retries - 1:
raise _build_llm_error(
cause=e, attempts=attempt + 1,
elapsed_s=elapsed,
content=content, model=use_model,
provider=getattr(self, "provider", None),
history=errors,
permanent=permanent,
) from e
# Honor server-supplied Retry-After when the
# underlying provider attached it to the exception.
retry_after_s = getattr(e, "retry_after_s", None)
sleep_s = _retry_sleep_seconds(attempt, retry_after_s)
# Would sleeping cross the deadline? If yes, give
# up now as TIMEOUT — don't waste wall-clock on a
# backoff we'll never get to consume.
if _deadline is not None and (time.monotonic() + sleep_s) >= _deadline:
from openprogram.providers.utils.errors import ErrorReason as _ER
raise _build_llm_error(
cause=e, attempts=attempt + 1,
elapsed_s=elapsed,
content=content, model=use_model,
provider=getattr(self, "provider", None),
history=errors,
permanent=True,
override_reason=_ER.TIMEOUT,
) from e
_fire_on_retry(
on_retry, cause=e, attempt=attempt + 1,
max_attempts=self.max_retries, sleep_s=sleep_s,
elapsed_s=elapsed, retry_after_s=retry_after_s,
)
time.sleep(sleep_s)
finally:
if tools_token is not None:
_current_tools.reset(tools_token)
if policy_token is not None:
_current_tool_policy.reset(policy_token)
# No choices — the raw reply text is the result.
if choices is None:
return reply
# Choice-constrained finish — resolve the reply against the menu.
# parse_args' own re-pick path issues fresh choice-free exec()
# calls, so the tool/policy tokens above are already reset.
#
# Forward the caller's timeout_s / on_retry into resolve_decision
# so re-pick exec() calls inside parse_args stay inside the
# caller's wall-clock budget and fire the observability hook.
# ``timeout_s`` here is the *remaining* budget — the initial
# choice-bearing exec already debited some of it (potentially
# all of it, if retries ran long). Negative remainder = deadline
# already hit; surface as TIMEOUT instead of starting parse_args
# with a useless near-zero budget.
remaining_timeout: Optional[float] = None
if timeout_s is not None:
remaining_timeout = timeout_s - (time.monotonic() - _exec_start)
if remaining_timeout <= 0:
from openprogram.providers.utils.errors import ErrorReason as _ER
cause = TimeoutError(
f"exec() exhausted timeout_s={timeout_s}s before choice "
"resolution could start"
)
raise _build_llm_error(
cause=cause, attempts=self.max_retries,
elapsed_s=time.monotonic() - _exec_start,
content=content, model=use_model,
provider=getattr(self, "provider", None),
history=[],
permanent=True,
override_reason=_ER.TIMEOUT,
) from cause
from openprogram.agentic_programming.decision import resolve_decision
return resolve_decision(
reply, _decision_menu, _decision_values, self,
timeout_s=remaining_timeout, on_retry=on_retry,
)
async def async_exec(
self,
content: list[dict],
context: Optional[str] = None,
response_format: Optional[dict] = None,
model: Optional[str] = None,
timeout_s: Optional[float] = None,
on_retry: Optional["Callable[[RetryInfo], None]"] = None,
) -> str:
"""Async version of :meth:`exec`. Same ``timeout_s`` /
``on_retry`` semantics; ``await``-friendly throughout.
Async retries use ``asyncio.sleep`` so the loop yields to the
event loop and an external cancellation (``asyncio.CancelledError``)
actually wakes the runtime up — sync ``exec()`` blocks in
``time.sleep`` for the same path. ``timeout_s`` here is
independent of any ``asyncio.wait_for`` wrapper the caller
might add: this one converts to a structured
``LLMError(reason=TIMEOUT)``, ``wait_for`` raises
``TimeoutError``.
"""
if self._closed:
raise RuntimeError("Runtime is closed. Create a new runtime instance.")
# Cancel check — lets long-running loops inside one function also abort.
from openprogram.agentic_programming.function import _run_pre_invocation_hooks
_run_pre_invocation_hooks()
if isinstance(content, str):
content = [{"type": "text", "text": content}]
use_model = model or self.model
content_text = "\n".join(b["text"] for b in content if b.get("type") == "text")
# --- Build call input (legacy text-merge only if needed) ---
if self._uses_legacy_call():
call_input = list(content)
if context:
call_input.insert(0, {"type": "text", "text": context})
system_text = getattr(self, "system", "") or ""
skills_block = self._skills_block()
if skills_block:
system_text = (system_text + skills_block) if system_text else skills_block.lstrip("\n")
if system_text:
call_input.insert(0, {"type": "text", "text": system_text, "role": "system"})
else:
call_input = content
# --- Call the LLM (with retry) ---
errors: list[str] = []
_exec_start = time.monotonic()
_deadline = _exec_start + timeout_s if (timeout_s and timeout_s > 0) else None
for attempt in range(self.max_retries):
# Pre-attempt deadline check (see exec() for the rationale).
if _deadline is not None and time.monotonic() >= _deadline:
from openprogram.providers.utils.errors import ErrorReason as _ER
cause = TimeoutError(
f"async_exec() timed out after {timeout_s}s "
f"({attempt} attempt(s))"
)
raise _build_llm_error(
cause=cause, attempts=max(1, attempt),
elapsed_s=time.monotonic() - _exec_start,
content=content, model=use_model,
provider=getattr(self, "provider", None),
history=errors,
permanent=True,
override_reason=_ER.TIMEOUT,
) from cause
try:
reply = await self._async_call(call_input, model=use_model, response_format=response_format)
self._append_model_call_node(
reply=reply,
model=use_model,
content_text=content_text,
)
return reply
except (TypeError, NotImplementedError):
raise
except Exception as e:
errors.append(f"Attempt {attempt + 1}: {type(e).__name__}: {e}")
permanent = _is_permanent_error(e)
elapsed = time.monotonic() - _exec_start
if permanent or attempt == self.max_retries - 1:
raise _build_llm_error(
cause=e, attempts=attempt + 1,
elapsed_s=elapsed,
content=content, model=use_model,
provider=getattr(self, "provider", None),
history=errors,
permanent=permanent,
) from e
retry_after_s = getattr(e, "retry_after_s", None)
sleep_s = _retry_sleep_seconds(attempt, retry_after_s)
if _deadline is not None and (time.monotonic() + sleep_s) >= _deadline:
from openprogram.providers.utils.errors import ErrorReason as _ER
raise _build_llm_error(
cause=e, attempts=attempt + 1,
elapsed_s=elapsed,
content=content, model=use_model,
provider=getattr(self, "provider", None),
history=errors,
permanent=True,
override_reason=_ER.TIMEOUT,
) from e
_fire_on_retry(
on_retry, cause=e, attempt=attempt + 1,
max_attempts=self.max_retries, sleep_s=sleep_s,
elapsed_s=elapsed, retry_after_s=retry_after_s,
)
await asyncio.sleep(sleep_s)
def _call(self, content: list[dict], model: str = "default", response_format: dict = None) -> str:
"""
Call the LLM. Override this in subclasses.
Args:
content: List of content blocks (text, image, audio, file).
model: Model name.
response_format: Output format constraint (JSON schema).
Returns:
str — the LLM's reply text.
"""
if self._call_fn is not None: