@lmmslab/harness 0.81.1 → 0.83.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +24 -35
  2. package/package.json +2 -2
  3. package/python/harness/README.md +3 -3
  4. package/python/harness/__init__.py +1 -1
  5. package/python/harness/builtin/configs/assistant.toml +0 -1
  6. package/python/harness/builtin/configs/default.toml +1 -1
  7. package/python/harness/builtin/prompts/conversation/core.md +2 -2
  8. package/python/harness/builtin/prompts/core/core.md +197 -45
  9. package/python/harness/builtin/prompts/core/pack.toml +2 -2
  10. package/python/harness/builtin/prompts/reflect/core.md +4 -13
  11. package/python/harness/builtin/prompts/reflect/pack.toml +3 -2
  12. package/python/harness/builtin/prompts/shared/learning-goal.md +5 -29
  13. package/python/harness/builtin/prompts/shared/learning.md +8 -10
  14. package/python/harness/builtin/prompts/shared/web.md +11 -0
  15. package/python/harness/builtin/skills/agents/__init__.py +24 -22
  16. package/python/harness/builtin/skills/entries/__init__.py +43 -28
  17. package/python/harness/builtin/skills/entries/skill.toml +2 -3
  18. package/python/harness/builtin/skills/environments/__init__.py +2 -9
  19. package/python/harness/builtin/skills/mcp/__init__.py +2 -3
  20. package/python/harness/builtin/skills/memory/__init__.py +2 -5
  21. package/python/harness/builtin/skills/web/__init__.py +92 -0
  22. package/python/harness/builtin/skills/web/skill.toml +9 -0
  23. package/python/harness/cli.py +85 -17
  24. package/python/harness/config/agent.py +36 -9
  25. package/python/harness/core/episodes.py +5 -4
  26. package/python/harness/core/lanes.py +3 -0
  27. package/python/harness/core/memory.py +5 -1
  28. package/python/harness/core/remote.py +4 -3
  29. package/python/harness/core/worker.py +58 -3
  30. package/python/harness/extensions.py +23 -2
  31. package/python/harness/host/__init__.py +43 -1
  32. package/python/harness/host/client.py +36 -6
  33. package/python/harness/host/http.py +19 -0
  34. package/python/harness/host/server.py +17 -3
  35. package/python/harness/host/sessions.py +3 -2
  36. package/python/harness/host/tasks.py +107 -26
  37. package/python/harness/host/workspaces.py +69 -7
  38. package/python/harness/model_list.py +1 -1
  39. package/python/harness/packages/manager.py +5 -1
  40. package/python/harness/providers.py +14 -5
  41. package/python/harness/runtime/decision/loop.py +105 -117
  42. package/python/harness/runtime/decision/messages.py +158 -55
  43. package/python/harness/runtime/decision/model.py +85 -22
  44. package/python/harness/runtime/decision/notices.py +66 -0
  45. package/python/harness/runtime/decision/prompts.py +15 -11
  46. package/python/harness/runtime/decision/protocol.py +12 -6
  47. package/python/harness/runtime/decision/thoughts.py +10 -16
  48. package/python/harness/runtime/events.py +28 -13
  49. package/python/harness/runtime/execution/attached.py +1 -1
  50. package/python/harness/runtime/execution/control.py +17 -35
  51. package/python/harness/runtime/execution/entries.py +204 -124
  52. package/python/harness/runtime/execution/kernel.py +471 -281
  53. package/python/harness/runtime/execution/managed.py +231 -0
  54. package/python/harness/runtime/execution/noenv.py +2 -2
  55. package/python/harness/runtime/execution/results.py +2 -0
  56. package/python/harness/runtime/execution/session.py +284 -115
  57. package/python/harness/runtime/learning/active.py +41 -50
  58. package/python/harness/runtime/learning/fixtures.py +2 -2
  59. package/python/harness/runtime/learning/learner.py +75 -2
  60. package/python/harness/runtime/learning/library.py +77 -10
  61. package/python/harness/runtime/learning/views.py +12 -1
  62. package/python/harness/runtime/reactive/__init__.py +1 -1
  63. package/python/harness/runtime/reactive/rules.py +29 -394
  64. package/python/harness/runtime/reactive/tree.py +611 -0
  65. package/python/harness/runtime/safety/approval.py +8 -3
  66. package/python/harness/runtime/safety/permissions.py +242 -0
  67. package/python/harness/runtime/status.py +7 -2
  68. package/python/harness/runtime/task/delegation.py +97 -58
  69. package/python/harness/runtime/task/environments.py +3 -4
  70. package/python/harness/runtime/task/inbound.py +64 -0
  71. package/python/harness/runtime/task/run.py +1231 -790
  72. package/python/harness/runtime/tick.py +257 -91
  73. package/python/harness/sdk/__init__.py +7 -4
  74. package/python/harness/settings.py +30 -5
  75. package/python/harness/terminal.py +10 -6
  76. package/python/harness/transcript.py +18 -6
  77. package/python/harness/tui/answer.py +51 -0
  78. package/python/harness/tui/chat.py +75 -26
  79. package/python/harness/tui/views.py +1 -0
  80. package/python/harness/builtin/prompts/shared/entries.md +0 -86
@@ -1,10 +1,7 @@
1
- """Decision protocol, dispatch tables and speculative-reply rules.
1
+ """Decision protocol and dispatch tables.
2
2
 
3
- TaskRun coordinates these decisions with task lifecycle and program execution.
3
+ TaskRun coordinates these decisions with task lifecycle and the model's units.
4
4
  """
5
- import ast
6
- import re
7
- from harness.runtime.decision.protocol import AHEAD_KINDS, ProtocolError, looks_like_action, parse_reply
8
5
 
9
6
 
10
7
  # Seconds between two model_text records of a streamed reply.
@@ -13,18 +10,10 @@ STREAMED_SECONDS = 0.3
13
10
 
14
11
  # Added to the system prompt of an interactive conversation.
15
12
  INTERACTIVE_PROMPT = (
16
- "\n\nA user is with you in this episode. Their messages reach you as \"Message from the user: ...\". "
17
- "To ask for information needed by this task, reply in plain text without a block; the task waits "
18
- "for the answer. Use <finish> for the completed task and your final answer. That ends this run; "
19
- "it does not open a new conversation turn."
20
- )
21
-
22
-
23
- # Added to an interactive conversation's prompt (a sub-agent's too): a message while a program runs.
24
- MESSAGE_PROMPT = (
25
- " A message that comes while a program runs reaches you at once: reply <wait> to let the program go on "
26
- "(you are asked again with its result), a block that moves nothing to run it beside the program, or what "
27
- "should run instead, which stops the program at its next step."
13
+ "\n\nA user is with you in this conversation. Their messages reach you as \"Message from the user: ...\". "
14
+ "A reply in plain text, without a block, is your answer to them: what you did or found, or a question you "
15
+ "need answered. It ends this turn, and the conversation goes on with their next message: you keep what you "
16
+ "have (your programs' variables, the rules, the facts, the environment)."
28
17
  )
29
18
 
30
19
 
@@ -43,91 +32,49 @@ ANNOUNCED = ("Nothing ran: your reply says what you will do, but it has no {bloc
43
32
  EMPTY_RETRIES = 2
44
33
 
45
34
 
46
- # Old profiles can still disable speculation; the other values share one policy.
47
- THINK_WHILE_ACTING = ("off", "on", "on_events", "periodic")
48
-
35
+ # runtime.turn_grace_seconds: how long a program may hold the model's turn. One that is
36
+ # over by then gives the turn back with its result; a longer one goes on as a unit, and
37
+ # the turn comes back with its handle.
38
+ TURN_GRACE_SECONDS = 2.0
49
39
 
50
- AHEAD_SECONDS = 3.0
51
40
 
52
-
53
- _ASSUME = re.compile(r"<assume>(.*?)</assume>", re.DOTALL)
54
-
55
-
56
- _ASSUME_NODES = (
57
- ast.Expression, ast.BoolOp, ast.And, ast.Or, ast.UnaryOp, ast.Not, ast.USub,
58
- ast.Compare, ast.Eq, ast.NotEq, ast.Lt, ast.LtE, ast.Gt, ast.GtE, ast.Is,
59
- ast.IsNot, ast.In, ast.NotIn, ast.Name, ast.Load, ast.Constant, ast.Subscript,
60
- ast.Slice, ast.Tuple, ast.List,
61
- )
41
+ # runtime.tell_budget: whether the model is told, with every call, what the run has left
42
+ # (model calls, environment steps, live time). "never": the limits are the host's to keep
43
+ # and the model is not told them; "always": told with every call (a benchmark whose
44
+ # episodes are a few dozen calls, where the model must plan its end).
45
+ TELL_BUDGET = ("never", "always")
62
46
 
63
47
 
64
- AHEAD_RUNNING = (
65
- "Program {number} is running now. Prepare what comes after it. State a check on its result in "
66
- "<assume>...</assume> before the next block. The check can read result (the final "
67
- "execution receipt), output (stdout) and state (the latest observation). "
68
- "Reply <wait> if you need to see the result first."
69
- )
70
-
71
-
72
- LOOK_RUNNING = (
73
- "Program {number} is still running. Reply <wait> to let it continue, a Python block that "
74
- "can run alongside it, or <stop>reason</stop> and the replacement block."
75
- )
76
-
77
-
78
- # The message of a call made because the model's own watch(every=seconds) is due while
79
- # program N runs: the views it asked for come with it.
80
- WATCH_RUNNING = (
81
- "Program {number} is still running; these are the views your watch() asked for. Reply <wait> to let it "
82
- "continue, a Python block that can run alongside it, or <stop>reason</stop> and the replacement block."
83
- )
48
+ # runtime.longest_wait_seconds: the longest a <wait> lasts; one that names no time lasts
49
+ # this long, so no wait is endless.
50
+ LONGEST_WAIT_SECONDS = 60.0
84
51
 
85
52
 
86
- _STOP = re.compile(r"<stop>(.*?)</stop>", re.DOTALL)
53
+ # What the model is told when its wait ended: why (WAIT_ENDS: the kind of the notice that
54
+ # ended it, runtime/decision/notices.py, or its time), and after how long.
55
+ WAIT_ENDED = "Wait ended after {seconds:.1f} s: {why}."
56
+ WAIT_ENDS = {"unit": "a unit you started ended", "message": "a message reached you", "event": "an event reached you",
57
+ "note": "the harness told you something", "time": "its time is over"}
87
58
 
88
59
 
89
- # The end of the message of a call made when a message for the model comes while program
90
- # N runs (A6's demo 3: a budget typed during a 17 s page load sat unread until it ended).
91
- MESSAGE_RUNNING = ("Program {number} is still running. Reply <wait> to let it finish: you are asked again with its "
92
- "result. Or reply with what should run instead: the program stops at its next step, and your "
93
- "block runs then.")
60
+ # How the model reads an event that reached it; what the event's record holds follows.
61
+ EVENT = "Event {kind} from {source}"
94
62
 
95
63
 
96
- # What a reply may do beside the running program at once, instead of stopping it: tell or
97
- # start agents and entries, update the plan, note places, plain Python.
98
- _BESIDE_CALLS = {"send_agent", "cancel_agent", "delegate", "spawn", "plan", "step_done", "recall", "read_skill",
99
- "remember", "forget", "things", "thing", "forget_thing",
100
- "print", "len", "str", "int", "float", "bool", "list", "dict", "set", "tuple", "range",
101
- "enumerate", "zip", "sorted", "min", "max", "sum", "any", "all", "isinstance", "round", "abs",
102
- "repr", "format"}
64
+ # What the model is told, with its next prompt, when its conversation goes on in a new
65
+ # process (`units`: those that still ran when the old one was last heard of).
66
+ CONTINUED = ("Note from the harness: this conversation was continued in a new process. Its messages and facts "
67
+ "are restored. What lived in the old process is gone: the variables and functions your programs "
68
+ "defined, the rules you set, and the units that still ran{units}. Set up again what you still need.")
103
69
 
104
70
 
105
- _BESIDE_METHODS = {"append", "extend", "get", "items", "keys", "values", "format", "join", "strip", "split",
106
- "lower", "upper", "replace", "startswith", "endswith", "update", "pop", "copy", "count",
107
- "index"}
71
+ # What the model is told when its turn comes back while its program still runs.
72
+ RUNNING = "Program {number} is still running, as unit {unit}."
108
73
 
109
74
 
110
- def _beside(program: str) -> bool:
111
- """Whether `program` needs no body: it calls only _BESIDE_CALLS, _BESIDE_METHODS and
112
- functions it defines itself, so it may run beside a running program. What it hands to
113
- spawn() runs as an entry of its own, whose channels the tick keeps apart
114
- (CHANNEL_CONFLICT), so it may do anything."""
115
- try:
116
- tree = ast.parse(program)
117
- except SyntaxError:
118
- return False
119
- own = {node.name for node in ast.walk(tree) if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))}
120
- spawned = {id(inner) for node in ast.walk(tree)
121
- if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == "spawn"
122
- for argument in [*node.args, *(keyword.value for keyword in node.keywords)]
123
- for inner in ast.walk(argument)}
124
- for node in ast.walk(tree):
125
- if isinstance(node, ast.Call) and id(node) not in spawned:
126
- func = node.func
127
- if not (isinstance(func, ast.Name) and (func.id in _BESIDE_CALLS or func.id in own)
128
- or isinstance(func, ast.Attribute) and func.attr in _BESIDE_METHODS):
129
- return False
130
- return True
75
+ # Added to the feedback of a program that ended.
76
+ PROGRAM_ENDED = ("\nThe program ended. If the task is done, answer in plain text; otherwise write a program "
77
+ "for the rest of the task.")
131
78
 
132
79
 
133
80
  # runtime.thinking: a level bounds the reasoning effort (a model set lower keeps its own)
@@ -167,32 +114,73 @@ def _thinking(main: dict, level: str) -> dict:
167
114
  return main
168
115
 
169
116
 
170
- # Pending events take priority over the event that brought the loop to a decision.
171
- # Explicit requests have a trigger per role and share the same transport as decisions.
172
- TRIGGERS = {
173
- "running": {"message": "_pending_message", "escalation": "_pending_escalation",
174
- "ahead": "_pending_ahead", "watch": "_pending_watch"},
175
- "between": {"user": "_pending_message", "escalation": "_unresolved",
176
- "start": None, "program_end": None, "error": None, "rejected": None,
177
- "finish_refused": None, "verify": None, "empty": None, "format": None},
178
- "request": {"verifier": "request", "observer": "request", "vlm": "request",
179
- "pointer": "request", "reflection": "reflect"},
180
- }
181
-
182
-
183
- OUTPUTS = {
184
- "between": {"program": "_out_program", "verify": "_out_verify", "finish": "_out_finish",
185
- "message": "_out_message", "invalid": "_out_invalid", "empty": "_out_empty"},
186
- "running": {"wait": None, "message": "_say_beside", "parallel": "_run_beside",
187
- "replace": "_interrupt"},
188
- "request": {"verifier": "verifier_calls", "reflection": "reflection_calls",
189
- "observer": "vision_calls", "vlm": "vision_calls", "pointer": "vision_calls"},
190
- }
191
-
192
-
193
- def _running_reply(text, kinds):
194
- """Parse one running-program reply and distinguish ordinary prose from malformed blocks."""
195
- try:
196
- return parse_reply(text, (*kinds, *AHEAD_KINDS)), False
197
- except ProtocolError:
198
- return None, bool(text.strip()) and not looks_like_action(text)
117
+ # Why the model is called (a model_request's reason): a message or an event it was told and
118
+ # has not answered ("user", "escalation") comes before what happened last.
119
+ REASONS = ("user", "escalation", "start", "program_end", "program_running", "unit_end", "wait_end", "error",
120
+ "rejected", "finish_refused", "verify", "empty", "format")
121
+
122
+
123
+ # The trigger of a role's request: explicit requests share the transport of decisions.
124
+ REQUESTS = {"verifier": "request", "observer": "request", "vlm": "request", "pointer": "request",
125
+ "reflection": "reflect", "compaction": "compact", "search": "request"}
126
+
127
+
128
+ # Compaction (runtime/task/run.py _compact_if_due). When the context a call read reaches
129
+ # runtime.compact_at_tokens, the model is asked for a summary of the conversation (COMPACT,
130
+ # after its last message), and the summary (COMPACTED) stands in the first message in
131
+ # place of every message before the recent ones, which stay as they are: from the end
132
+ # back until they hold KEEP_MIN_TOKENS and KEEP_MIN_MESSAGES, or KEEP_MAX_TOKENS
133
+ # (messages.recent_start).
134
+ KEEP_MIN_TOKENS, KEEP_MIN_MESSAGES, KEEP_MAX_TOKENS = 10_000, 5, 40_000
135
+ # Old results. A call made after the provider's cache of the conversation has gone cold
136
+ # (runtime.clear_results_after_minutes since the model's last reply) reads everything
137
+ # anew: before it, the results of all programs but the last KEPT_RESULTS are replaced
138
+ # by CLEARED (messages.clear_results).
139
+ KEPT_RESULTS = 5
140
+ CLEARED = "[Old tool result content cleared]"
141
+ COMPACT = (
142
+ "You have written a partial transcript for the initial task above. Please write a summary of the transcript. "
143
+ "The purpose of this summary is to provide continuity so you can continue to make progress towards solving "
144
+ "the task in a future context, where the raw history above may not be accessible and will be replaced with "
145
+ "this summary. Write down anything that would be helpful, including the state, next steps, learnings etc. "
146
+ "Before the summary, go through the conversation in order in an <analysis></analysis> block, so that "
147
+ "nothing is missed. Give the summary these sections:\n"
148
+ "1. Primary request and intent: what was asked, in full.\n"
149
+ "2. Key facts: what you found out about the environment and how it behaves.\n"
150
+ "3. What exists: what your programs defined that is still needed, what the library holds, the reflexes "
151
+ "you set and the units that run, each with what it is for.\n"
152
+ "4. Errors and fixes: what went wrong and how it was repaired.\n"
153
+ "5. Problem solving: what is solved and what is still being worked out.\n"
154
+ "6. All messages of the user that are not results of programs.\n"
155
+ "7. Pending tasks.\n"
156
+ "8. Current work: precisely what you were doing just before this summary.\n"
157
+ "9. Next step: the step that follows directly from the current work.\n"
158
+ "Reply with the summary only, no program. You must wrap your summary in a <summary></summary> block.")
159
+ COMPACTED = (
160
+ "This session is being continued from a previous conversation that ran out of context. The summary below "
161
+ "covers the earlier portion of the conversation. Recent messages are preserved verbatim. The kernel with "
162
+ "what your programs defined, the library, the tree of reflexes and the units that run are as they were."
163
+ "\n\nSummary:\n{summary}")
164
+
165
+
166
+ # What a reply of the model does: the method of TaskRun for each output. A reply that is
167
+ # text (message) and a <finish> block are the same answer.
168
+ OUTPUTS = {"program": "_out_program", "verify": "_out_verify", "finish": "_out_answer", "wait": "_out_wait",
169
+ "message": "_out_answer", "invalid": "_out_invalid", "empty": "_out_empty"}
170
+
171
+
172
+ # How a turn ends (the reason of its turn_result record and of the IDLE state after it), and
173
+ # the status the summary keeps for it (what a benchmark's caller reads). Model calls and
174
+ # wall time are the turn's and begin anew with the next one; cost is the conversation's.
175
+ TURN_ENDS = {"answer": "finished", "budget": "budget_exhausted", "wall_time": "wall_time_exhausted",
176
+ "cost": "cost_exhausted", "error": "error", "interrupted": "interrupted",
177
+ "episode_ended": "episode_ended"}
178
+ # The ends after which only a new message starts a turn: what reached the model before
179
+ # them is kept in its history and starts none.
180
+ TURN_STOPS = ("cost", "error", "interrupted")
181
+
182
+
183
+ # The summary's count of each role's requests.
184
+ COUNTS = {"verifier": "verifier_calls", "reflection": "reflection_calls", "observer": "vision_calls",
185
+ "vlm": "vision_calls", "pointer": "vision_calls", "compaction": "compaction_calls",
186
+ "search": "search_calls"}
@@ -1,8 +1,8 @@
1
1
  """The model's messages: what the conversation (runtime/conversation.py) tells the model
2
2
  of an embodiment and of each program it ran. The text of each embodiment kind
3
3
  (EMBODIMENT_TEXT), the state and the camera images of a message, a program's feedback
4
- (its status, output, events, entries, the skills' turn report, the state and steps), and
5
- what a request keeps of the history (prune_images, window).
4
+ (its status, output, events, the skills' turn report, the state and steps), another unit's
5
+ end, and what a request keeps of the history's images (prune_images, prune_views).
6
6
  """
7
7
 
8
8
  from collections.abc import Mapping
@@ -87,13 +87,36 @@ def _round(value):
87
87
  return value
88
88
 
89
89
 
90
- def cell_feedback(result: dict, number: int, hz: float | None = None, embodiment: str = "robot",
91
- steps: bool = True) -> str:
90
+ def latest(items, limit: int = 20) -> str:
91
+ """The last `limit` of `items` in one line, after how many earlier ones it leaves out."""
92
+ items = list(items)
93
+ left = len(items) - limit
94
+ return (f"({left} earlier omitted) " if left > 0 else "") + "; ".join(items[-limit:])
95
+
96
+
97
+ def cut(text: str, limit: int) -> str:
98
+ """`text` up to `limit` characters, then how many more there are."""
99
+ return text if len(text) <= limit else text[:limit] + f" ... [{len(text) - limit} more characters]"
100
+
101
+
102
+ def ended_text(result: dict) -> str:
103
+ """The end report of a unit in words: how it ended, and whether what it was for is the
104
+ case ("done": its completion condition held at the end; nothing where it stated none),
105
+ with its seconds. The same for a program of the model, code it spawned and a reflex's
106
+ program."""
107
+ how = result.get("ended") or {"ok": "by itself", "error": "an error"}.get(result["status"], result["status"])
108
+ done = result.get("done")
109
+ return (f"ended: {how}" + ("" if done is None else "; done: yes" if done else "; done: no (what it was for is not the case)")
110
+ + f" ({result.get('seconds', 0):.1f} s)")
111
+
112
+
113
+ def cell_feedback(result: dict, number: int) -> str:
92
114
  """What the model is told of program `number` it ran: its status, output and error, the
93
- event that ended it, the executions and entries, the skills' turn report, the state and
94
- the steps. It names the program, since the result of one program can reach the model
95
- after the next program's code (a reply made while it ran)."""
96
- lines = [f"Program {number} status: {result['status']} ({result.get('seconds', 0):.1f} s)"]
115
+ event that ended it, the executions and the skills' turn report (the state as it is
116
+ now is told once with every call: runtime/task/run.py _ambient). It names the
117
+ program, since programs run side by side and the result of one can reach the model
118
+ after the code of the next."""
119
+ lines = [f"Program {number} " + ended_text(result)]
97
120
  if result.get("stdout"):
98
121
  lines.append("Output:\n" + result["stdout"].rstrip())
99
122
  if result.get("error"):
@@ -108,44 +131,63 @@ def cell_feedback(result: dict, number: int, hz: float | None = None, embodiment
108
131
  if result.get("events"):
109
132
  executed = [f"{item['skill']}: {item['event']['kind']}" + (" (handled)" if item.get("handled") else "")
110
133
  for item in result["events"]]
111
- lines.append("Executions: " + "; ".join(executed[-20:]))
134
+ lines.append("Executions: " + latest(executed))
112
135
  if result.get("event_counts"):
113
136
  lines.append("Execution totals (details in trace): " + json.dumps(result["event_counts"]))
114
- if result.get("entries"):
115
- lines.append("Entries: " + "; ".join(entry_text(item) for item in result["entries"][-20:]))
116
137
  if (result.get("report") or {}).get("text"):
117
138
  lines.append(result["report"]["text"])
118
- if result.get("state"):
119
- lines.append(f"{EMBODIMENT_TEXT[embodiment]['state_label']}: "
120
- + state_text(result["state"], embodiment, steps))
121
- if steps and result["state"].get("step") is not None:
122
- lines.append(steps_text(result["state"], hz))
123
139
  for name, part in (result.get("environments") or {}).items():
124
140
  # An environment attached to the conversation (runtime/execution/attached.py).
125
141
  if part.get("events"):
126
- lines.append(f"{name} executions: " + "; ".join(
142
+ lines.append(f"{name} executions: " + latest(
127
143
  f"{item['skill']}: {item['event']['kind']}" + (" (handled)" if item.get("handled") else "")
128
- for item in part["events"][-20:]))
144
+ for item in part["events"]))
129
145
  if part.get("error"):
130
146
  lines.append(f"{name}: {part['error']}")
131
147
  if part.get("state"):
132
- lines.append(f"{name} state: " + json.dumps(_round({
148
+ lines.append(f"{name} state: " + cut(json.dumps(_round({
133
149
  key: value for key, value in part["state"].items()
134
- if isinstance(value, (str, int, float, bool)) or key in ("screen",)}))[:1200])
150
+ if isinstance(value, (str, int, float, bool)) or key in ("screen",)})), 1200))
135
151
  return "\n".join(lines)
136
152
 
137
153
 
138
- def entry_text(item: dict) -> str:
139
- """An entry as the model hears of it after a program (runtime/execution/entries.py)."""
140
- text = f"{item['entry']} {item['name']} ({item['kind']}"
141
- if item.get("parent") not in (None, "e0"):
142
- text += f", started by {item['parent']}"
143
- text += f"): {item['status']} after {item['seconds']:g} s"
144
- if item.get("error"):
145
- text += f" ({item['error']})"
146
- elif item.get("result") is not None:
147
- text += f", returned {item['result']}"
148
- return text
154
+ def unit_feedback(result: dict) -> str:
155
+ """What the model is told of a unit that ended and is not one of its programs (code a
156
+ program spawned, a sub-agent): how it ended, what it printed and returned."""
157
+ lines = [f"Unit {result['unit']} {result.get('name') or ''} ({result.get('kind')}) " + ended_text(result)
158
+ + (" (its failure was handled)" if result.get("handled") else "")]
159
+ if result.get("stdout"):
160
+ lines.append("Output:\n" + result["stdout"].rstrip())
161
+ if result.get("error"):
162
+ lines.append("Error:\n" + str(result["error"]).rstrip())
163
+ trace = result.get("escalation") or result.get("episode_ended")
164
+ if trace:
165
+ event = trace["event"]
166
+ lines.append("Unhandled event: " + json.dumps(_round({
167
+ "kind": event["kind"], "source": event["source"], "detail": event["detail"], "skill": trace["skill"]})))
168
+ if result.get("returned") is not None:
169
+ returned = result["returned"]
170
+ lines.append("Returned: " + (returned if isinstance(returned, str) else json.dumps(_round(returned), default=str)))
171
+ if result.get("events"):
172
+ lines.append("Executions: " + latest(
173
+ f"{item['skill']}: {item['event']['kind']}" + (" (handled)" if item.get("handled") else "")
174
+ for item in result["events"]))
175
+ return "\n".join(lines)
176
+
177
+
178
+ def unit_text(identifier: str, unit: dict, printed: bool = True) -> str:
179
+ """A running unit as the model hears of it ({"name", "kind", "started"}: runtime/task/run.py),
180
+ and, with `printed`, how long ago it last printed where the host hears a unit's output
181
+ while it runs (its "printed": the time, None for nothing yet)."""
182
+ import time
183
+
184
+ now = time.time()
185
+ last = ""
186
+ if printed and "printed" in unit:
187
+ last = (", has printed nothing" if unit["printed"] is None
188
+ else f", last printed {max(0.0, now - unit['printed']):.0f} s ago")
189
+ kept = f", priority {unit['priority']:g}" if unit.get("priority") else ""
190
+ return f"{identifier} {unit.get('name') or ''} ({unit.get('kind')}{kept}, {max(0.0, now - unit['started']):.0f} s{last})"
149
191
 
150
192
 
151
193
  def steps_text(state: dict, hz: float | None) -> str:
@@ -210,6 +252,46 @@ def prune_views(messages: list[dict], keep: int = 1, pins=(), others: int = 2) -
210
252
  else part for part in message["content"]]
211
253
 
212
254
 
255
+ def tokens(message) -> int:
256
+ """About how many tokens a message is: a quarter of its characters, an image as 1200."""
257
+ content = message["content"]
258
+ if isinstance(content, str):
259
+ return len(content) // 4
260
+ return sum(1200 if part["type"] == "input_image" else len(part.get("text") or "") // 4 for part in content)
261
+
262
+
263
+ def recent_start(messages: list[dict], minimum: int, least: int, most: int) -> int:
264
+ """Where the messages begin that a compaction keeps as they are: from the end back
265
+ until they hold `minimum` tokens and `least` messages, or `most` tokens; then on to a
266
+ reply of the model, so that no program is parted from its result. A conversation that
267
+ fits whole keeps its last exchange."""
268
+ total, start = 0, len(messages)
269
+ for index in range(len(messages) - 1, 0, -1):
270
+ total, start = total + tokens(messages[index]), index
271
+ if total >= most or (total >= minimum and len(messages) - index >= least):
272
+ break
273
+ if start <= 1:
274
+ start = max(1, len(messages) - 2)
275
+ while start < len(messages) - 1 and messages[start]["role"] != "assistant":
276
+ start += 1
277
+ return start
278
+
279
+
280
+ def clear_results(messages: list[dict], keep: int, cleared: str) -> int:
281
+ """Replace the results of programs in `messages` (the parts marked "result") by
282
+ `cleared`, but the last `keep`; how many were replaced."""
283
+ seen = replaced = 0
284
+ for message in reversed(messages):
285
+ if message["role"] != "user" or isinstance(message["content"], str):
286
+ continue
287
+ for part in reversed(message["content"]):
288
+ if part.get("result"):
289
+ seen += 1
290
+ if seen > keep and part["text"] != cleared:
291
+ part["text"], replaced = cleared, replaced + 1
292
+ return replaced
293
+
294
+
213
295
  def text_only(messages: list[dict]) -> list[dict]:
214
296
  """`messages` for a model that reads no images: each image as a line saying it was not sent."""
215
297
  out = []
@@ -223,39 +305,60 @@ def text_only(messages: list[dict]) -> list[dict]:
223
305
  return out
224
306
 
225
307
 
226
- def window(messages: list[dict], turns: int | None) -> list[dict]:
227
- """The first message (instruction and initial views) plus the last `turns`
228
- assistant/user exchanges. Program state lives in the kernel, so older turns
229
- can drop out of the prompt without losing variables."""
230
- if not turns or len(messages) <= 1 + 2 * turns:
231
- return messages
232
- # messages alternate user, assistant, user, ...; keep the alternation.
233
- recent = messages[-2 * turns:]
234
- if recent[0]["role"] != "assistant":
235
- recent = recent[1:]
236
- omitted = len(messages) - 1 - len(recent)
237
- first = dict(messages[0])
238
- note = {"type": "input_text", "text": f"[{omitted} later messages are omitted here; kernel variables persist]"}
239
- first["content"] = (list(first["content"]) + [note] if isinstance(first["content"], list)
240
- else f"{first['content']}\n{note['text']}")
241
- return [first] + recent
308
+ def content_parts(content) -> list[dict]:
309
+ """A message's content as conversations give it, as the parts the model is sent: a
310
+ string, or a list of parts: text ({"type": "text" | "input_text", "text"}) and images
311
+ ({"type": "image_url", "image_url": url or {"url"}}, {"type": "input_image",
312
+ "image_url"}, or {"type": "image", "source": {"type": "base64", "media_type", "data"}})."""
313
+ if isinstance(content, str):
314
+ return [{"type": "input_text", "text": content}] if content.strip() else []
315
+ parts = []
316
+ for part in content or ():
317
+ if isinstance(part, str):
318
+ parts.append({"type": "input_text", "text": part})
319
+ continue
320
+ kind = part.get("type") if isinstance(part, dict) else None
321
+ if kind in ("text", "input_text") and isinstance(part.get("text"), str):
322
+ parts.append({"type": "input_text", "text": part["text"]})
323
+ elif kind in ("image_url", "input_image"):
324
+ url = part.get("image_url")
325
+ url = url.get("url") if isinstance(url, dict) else url
326
+ if not isinstance(url, str) or not url:
327
+ raise ValueError("an image part gives its image_url")
328
+ parts.append({"type": "input_image", "image_url": url, "detail": part.get("detail") or "auto"})
329
+ elif kind == "image" and isinstance(part.get("source"), dict) and part["source"].get("type") == "base64":
330
+ source = part["source"]
331
+ parts.append({"type": "input_image", "image_url": f"data:{source['media_type']};base64,{source['data']}",
332
+ "detail": "auto"})
333
+ else:
334
+ raise ValueError(f"a message's content is text and image parts, not {part!r:.80}")
335
+ return parts
242
336
 
243
337
 
244
338
  def _inbox_parts(pending) -> list[dict]:
245
- """Messages taken from the inbox as content parts: one text with each message, then the
246
- files attached to them."""
247
- text = "\n".join(f"Message from {'the user' if sender == 'user' else 'the agent that started you'}: "
248
- + (item or "(the files below)") for sender, item, _ in pending)
249
- return [{"type": "input_text", "text": text}] + [part for *_, parts in pending for part in parts]
339
+ """Messages taken from the inbox as content parts: one text with each message of the
340
+ user or of the agent that started this one, then the files attached to them; then what
341
+ the environment told (TaskRun.tell), each as it gave it."""
342
+ people = [(sender, item, parts) for sender, item, parts in pending if not sender.startswith("environment")]
343
+ told = [(sender, parts) for sender, _, parts in pending if sender.startswith("environment")]
344
+ out = []
345
+ if people:
346
+ text = "\n".join(f"Message from {'the user' if sender == 'user' else 'the agent that started you'}: "
347
+ + (item or "(the files below)") for sender, item, _ in people)
348
+ out = [{"type": "input_text", "text": text}] + [part for *_, parts in people for part in parts]
349
+ for sender, parts in told:
350
+ out += [{"type": "input_text", "text": f"Message from the {sender}:"}, *parts]
351
+ return out
250
352
 
251
353
 
252
354
  def rebuild_messages(records) -> list[dict]:
253
355
  """The main model's history as the trace has it, up to its last reply: what each
254
356
  request added (from the previous reply on, in place of the last messages it
255
357
  replaces), less the malformed exchanges dropped after it, and the last reply (but a
256
- reply to a call made while a program ran that was not used). Image pruning and the
257
- history window are applied per request (runtime/decision/messages.py prune_images, window),
258
- so the rebuilt list, pruned the same way, is the conversation's own."""
358
+ reply to a call made while a program ran that was not used). A compaction replaces
359
+ every message there was (its next request says so). Image pruning is applied per
360
+ request (prune_images, prune_views), so the rebuilt list, pruned the same way, is the
361
+ conversation's own."""
259
362
  history, reply = [], None
260
363
  for record in records:
261
364
  if record["event"] == "model_request":