@lmmslab/harness 0.81.1 → 0.82.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 (78) 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 +177 -41
  9. package/python/harness/builtin/prompts/core/pack.toml +2 -2
  10. package/python/harness/builtin/prompts/reflect/core.md +1 -1
  11. package/python/harness/builtin/prompts/shared/learning-goal.md +5 -29
  12. package/python/harness/builtin/prompts/shared/learning.md +8 -10
  13. package/python/harness/builtin/prompts/shared/web.md +11 -0
  14. package/python/harness/builtin/skills/agents/__init__.py +24 -22
  15. package/python/harness/builtin/skills/entries/__init__.py +43 -28
  16. package/python/harness/builtin/skills/entries/skill.toml +2 -3
  17. package/python/harness/builtin/skills/environments/__init__.py +2 -9
  18. package/python/harness/builtin/skills/mcp/__init__.py +2 -3
  19. package/python/harness/builtin/skills/memory/__init__.py +2 -5
  20. package/python/harness/builtin/skills/web/__init__.py +92 -0
  21. package/python/harness/builtin/skills/web/skill.toml +9 -0
  22. package/python/harness/cli.py +84 -16
  23. package/python/harness/config/agent.py +33 -9
  24. package/python/harness/core/episodes.py +5 -4
  25. package/python/harness/core/lanes.py +3 -0
  26. package/python/harness/core/memory.py +5 -1
  27. package/python/harness/core/remote.py +4 -3
  28. package/python/harness/core/worker.py +58 -3
  29. package/python/harness/extensions.py +23 -2
  30. package/python/harness/host/__init__.py +43 -1
  31. package/python/harness/host/client.py +36 -6
  32. package/python/harness/host/http.py +19 -0
  33. package/python/harness/host/server.py +17 -3
  34. package/python/harness/host/sessions.py +3 -2
  35. package/python/harness/host/tasks.py +107 -26
  36. package/python/harness/host/workspaces.py +69 -7
  37. package/python/harness/model_list.py +1 -1
  38. package/python/harness/packages/manager.py +5 -1
  39. package/python/harness/providers.py +14 -5
  40. package/python/harness/runtime/decision/loop.py +100 -121
  41. package/python/harness/runtime/decision/messages.py +156 -48
  42. package/python/harness/runtime/decision/model.py +79 -21
  43. package/python/harness/runtime/decision/prompts.py +15 -11
  44. package/python/harness/runtime/decision/protocol.py +12 -6
  45. package/python/harness/runtime/decision/thoughts.py +10 -16
  46. package/python/harness/runtime/events.py +28 -13
  47. package/python/harness/runtime/execution/attached.py +1 -1
  48. package/python/harness/runtime/execution/control.py +17 -35
  49. package/python/harness/runtime/execution/entries.py +204 -124
  50. package/python/harness/runtime/execution/kernel.py +471 -281
  51. package/python/harness/runtime/execution/managed.py +231 -0
  52. package/python/harness/runtime/execution/noenv.py +2 -2
  53. package/python/harness/runtime/execution/results.py +2 -0
  54. package/python/harness/runtime/execution/session.py +276 -115
  55. package/python/harness/runtime/learning/active.py +36 -48
  56. package/python/harness/runtime/learning/fixtures.py +2 -2
  57. package/python/harness/runtime/learning/learner.py +75 -2
  58. package/python/harness/runtime/learning/library.py +77 -10
  59. package/python/harness/runtime/learning/views.py +12 -1
  60. package/python/harness/runtime/reactive/__init__.py +1 -1
  61. package/python/harness/runtime/reactive/rules.py +29 -394
  62. package/python/harness/runtime/reactive/tree.py +611 -0
  63. package/python/harness/runtime/safety/approval.py +8 -3
  64. package/python/harness/runtime/safety/permissions.py +242 -0
  65. package/python/harness/runtime/status.py +7 -2
  66. package/python/harness/runtime/task/delegation.py +97 -58
  67. package/python/harness/runtime/task/environments.py +3 -4
  68. package/python/harness/runtime/task/inbound.py +64 -0
  69. package/python/harness/runtime/task/run.py +1092 -726
  70. package/python/harness/runtime/tick.py +257 -91
  71. package/python/harness/sdk/__init__.py +7 -4
  72. package/python/harness/settings.py +27 -3
  73. package/python/harness/terminal.py +10 -6
  74. package/python/harness/transcript.py +18 -6
  75. package/python/harness/tui/answer.py +51 -0
  76. package/python/harness/tui/chat.py +75 -26
  77. package/python/harness/tui/views.py +1 -0
  78. 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,38 @@ 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
-
49
-
50
- AHEAD_SECONDS = 3.0
51
-
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
- )
62
-
63
-
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
- )
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
84
39
 
85
40
 
86
- _STOP = re.compile(r"<stop>(.*?)</stop>", re.DOTALL)
41
+ # runtime.longest_wait_seconds: the longest a <wait> lasts; one that names no time lasts
42
+ # this long, so no wait is endless.
43
+ LONGEST_WAIT_SECONDS = 60.0
87
44
 
88
45
 
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.")
46
+ # What the model is told when its wait ended: why (WAIT_ENDS), and after how long; the units
47
+ # that run follow, each with how long it has run and when it last printed (messages.unit_text).
48
+ WAIT_ENDED = "Wait ended after {seconds:.1f} s: {why}."
49
+ WAIT_ENDS = {"unit": "a unit you started ended", "message": "a message reached you", "event": "an event reached you",
50
+ "time": "its time is over"}
94
51
 
95
52
 
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"}
53
+ # What the model is told, with its next prompt, when its conversation goes on in a new
54
+ # process (`units`: those that still ran when the old one was last heard of).
55
+ CONTINUED = ("Note from the harness: this conversation was continued in a new process. Its messages and facts "
56
+ "are restored. What lived in the old process is gone: the variables and functions your programs "
57
+ "defined, the rules you set, and the units that still ran{units}. Set up again what you still need.")
103
58
 
104
59
 
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"}
60
+ # What the model is told when its turn comes back while its program still runs.
61
+ RUNNING = "Program {number} is still running, as unit {unit}."
108
62
 
109
63
 
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
64
+ # Added to the feedback of a program that ended.
65
+ PROGRAM_ENDED = ("\nThe program ended. If the task is done, answer in plain text; otherwise write a program "
66
+ "for the rest of the task.")
131
67
 
132
68
 
133
69
  # runtime.thinking: a level bounds the reasoning effort (a model set lower keeps its own)
@@ -167,32 +103,75 @@ def _thinking(main: dict, level: str) -> dict:
167
103
  return main
168
104
 
169
105
 
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)
106
+ # Why the model is called (a model_request's reason): a pending message or unresolved event
107
+ # (the method that says whether one is pending) comes before what happened last.
108
+ TRIGGERS = {"user": "_pending_message", "escalation": "_unresolved",
109
+ "start": None, "program_end": None, "program_running": None, "unit_end": None, "wait_end": None,
110
+ "error": None,
111
+ "rejected": None, "finish_refused": None, "verify": None, "empty": None, "format": None}
112
+
113
+
114
+ # The trigger of a role's request: explicit requests share the transport of decisions.
115
+ REQUESTS = {"verifier": "request", "observer": "request", "vlm": "request", "pointer": "request",
116
+ "reflection": "reflect", "compaction": "compact", "search": "request"}
117
+
118
+
119
+ # Compaction (runtime/task/run.py _compact_if_due). When the context a call read reaches
120
+ # runtime.compact_at_tokens, the model is asked for a summary of the conversation (COMPACT,
121
+ # after its last message), and the summary (COMPACTED) stands in the first message in
122
+ # place of every message before the recent ones, which stay as they are: from the end
123
+ # back until they hold KEEP_MIN_TOKENS and KEEP_MIN_MESSAGES, or KEEP_MAX_TOKENS
124
+ # (messages.recent_start).
125
+ KEEP_MIN_TOKENS, KEEP_MIN_MESSAGES, KEEP_MAX_TOKENS = 10_000, 5, 40_000
126
+ # Old results. A call made after the provider's cache of the conversation has gone cold
127
+ # (runtime.clear_results_after_minutes since the model's last reply) reads everything
128
+ # anew: before it, the results of all programs but the last KEPT_RESULTS are replaced
129
+ # by CLEARED (messages.clear_results).
130
+ KEPT_RESULTS = 5
131
+ CLEARED = "[Old tool result content cleared]"
132
+ COMPACT = (
133
+ "You have written a partial transcript for the initial task above. Please write a summary of the transcript. "
134
+ "The purpose of this summary is to provide continuity so you can continue to make progress towards solving "
135
+ "the task in a future context, where the raw history above may not be accessible and will be replaced with "
136
+ "this summary. Write down anything that would be helpful, including the state, next steps, learnings etc. "
137
+ "Before the summary, go through the conversation in order in an <analysis></analysis> block, so that "
138
+ "nothing is missed. Give the summary these sections:\n"
139
+ "1. Primary request and intent: what was asked, in full.\n"
140
+ "2. Key facts: what you found out about the environment and how it behaves.\n"
141
+ "3. What exists: what your programs defined that is still needed, what the library holds, the reflexes "
142
+ "you set and the units that run, each with what it is for.\n"
143
+ "4. Errors and fixes: what went wrong and how it was repaired.\n"
144
+ "5. Problem solving: what is solved and what is still being worked out.\n"
145
+ "6. All messages of the user that are not results of programs.\n"
146
+ "7. Pending tasks.\n"
147
+ "8. Current work: precisely what you were doing just before this summary.\n"
148
+ "9. Next step: the step that follows directly from the current work.\n"
149
+ "Reply with the summary only, no program. You must wrap your summary in a <summary></summary> block.")
150
+ COMPACTED = (
151
+ "This session is being continued from a previous conversation that ran out of context. The summary below "
152
+ "covers the earlier portion of the conversation. Recent messages are preserved verbatim. The kernel with "
153
+ "what your programs defined, the library, the tree of reflexes and the units that run are as they were."
154
+ "\n\nSummary:\n{summary}")
155
+
156
+
157
+ # What a reply of the model does: the method of TaskRun for each output. A reply that is
158
+ # text (message) and a <finish> block are the same answer.
159
+ OUTPUTS = {"program": "_out_program", "verify": "_out_verify", "finish": "_out_answer", "wait": "_out_wait",
160
+ "message": "_out_answer", "invalid": "_out_invalid", "empty": "_out_empty"}
161
+
162
+
163
+ # How a turn ends (the reason of its turn_result record and of the IDLE state after it), and
164
+ # the status the summary keeps for it (what a benchmark's caller reads). Model calls and
165
+ # wall time are the turn's and begin anew with the next one; cost is the conversation's.
166
+ TURN_ENDS = {"answer": "finished", "budget": "budget_exhausted", "wall_time": "wall_time_exhausted",
167
+ "cost": "cost_exhausted", "error": "error", "interrupted": "interrupted",
168
+ "episode_ended": "episode_ended"}
169
+ # The ends after which only a new message starts a turn: what reached the model before
170
+ # them is kept in its history and starts none.
171
+ TURN_STOPS = ("cost", "error", "interrupted")
172
+
173
+
174
+ # The summary's count of each role's requests.
175
+ COUNTS = {"verifier": "verifier_calls", "reflection": "reflection_calls", "observer": "vision_calls",
176
+ "vlm": "vision_calls", "pointer": "vision_calls", "compaction": "compaction_calls",
177
+ "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 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
+
90
113
  def cell_feedback(result: dict, number: int, hz: float | None = None, embodiment: str = "robot",
91
114
  steps: bool = True) -> str:
92
115
  """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)"]
116
+ event that ended it, the executions, the skills' turn report, the state and the steps.
117
+ It names the program, since programs run side by side and the result of one can reach
118
+ the model 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,11 +131,9 @@ 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
139
  if result.get("state"):
@@ -123,29 +144,55 @@ def cell_feedback(result: dict, number: int, hz: float | None = None, embodiment
123
144
  for name, part in (result.get("environments") or {}).items():
124
145
  # An environment attached to the conversation (runtime/execution/attached.py).
125
146
  if part.get("events"):
126
- lines.append(f"{name} executions: " + "; ".join(
147
+ lines.append(f"{name} executions: " + latest(
127
148
  f"{item['skill']}: {item['event']['kind']}" + (" (handled)" if item.get("handled") else "")
128
- for item in part["events"][-20:]))
149
+ for item in part["events"]))
129
150
  if part.get("error"):
130
151
  lines.append(f"{name}: {part['error']}")
131
152
  if part.get("state"):
132
- lines.append(f"{name} state: " + json.dumps(_round({
153
+ lines.append(f"{name} state: " + cut(json.dumps(_round({
133
154
  key: value for key, value in part["state"].items()
134
- if isinstance(value, (str, int, float, bool)) or key in ("screen",)}))[:1200])
155
+ if isinstance(value, (str, int, float, bool)) or key in ("screen",)})), 1200))
135
156
  return "\n".join(lines)
136
157
 
137
158
 
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
159
+ def unit_feedback(result: dict) -> str:
160
+ """What the model is told of a unit that ended and is not one of its programs (code a
161
+ program spawned, a sub-agent): how it ended, what it printed and returned."""
162
+ lines = [f"Unit {result['unit']} {result.get('name') or ''} ({result.get('kind')}) " + ended_text(result)
163
+ + (" (its failure was handled)" if result.get("handled") else "")]
164
+ if result.get("stdout"):
165
+ lines.append("Output:\n" + result["stdout"].rstrip())
166
+ if result.get("error"):
167
+ lines.append("Error:\n" + str(result["error"]).rstrip())
168
+ trace = result.get("escalation") or result.get("episode_ended")
169
+ if trace:
170
+ event = trace["event"]
171
+ lines.append("Unhandled event: " + json.dumps(_round({
172
+ "kind": event["kind"], "source": event["source"], "detail": event["detail"], "skill": trace["skill"]})))
173
+ if result.get("returned") is not None:
174
+ returned = result["returned"]
175
+ lines.append("Returned: " + (returned if isinstance(returned, str) else json.dumps(_round(returned), default=str)))
176
+ if result.get("events"):
177
+ lines.append("Executions: " + latest(
178
+ f"{item['skill']}: {item['event']['kind']}" + (" (handled)" if item.get("handled") else "")
179
+ for item in result["events"]))
180
+ return "\n".join(lines)
181
+
182
+
183
+ def unit_text(identifier: str, unit: dict, printed: bool = True) -> str:
184
+ """A running unit as the model hears of it ({"name", "kind", "started"}: runtime/task/run.py),
185
+ and, with `printed`, how long ago it last printed where the host hears a unit's output
186
+ while it runs (its "printed": the time, None for nothing yet)."""
187
+ import time
188
+
189
+ now = time.time()
190
+ last = ""
191
+ if printed and "printed" in unit:
192
+ last = (", has printed nothing" if unit["printed"] is None
193
+ else f", last printed {max(0.0, now - unit['printed']):.0f} s ago")
194
+ kept = f", priority {unit['priority']:g}" if unit.get("priority") else ""
195
+ return f"{identifier} {unit.get('name') or ''} ({unit.get('kind')}{kept}, {max(0.0, now - unit['started']):.0f} s{last})"
149
196
 
150
197
 
151
198
  def steps_text(state: dict, hz: float | None) -> str:
@@ -210,6 +257,46 @@ def prune_views(messages: list[dict], keep: int = 1, pins=(), others: int = 2) -
210
257
  else part for part in message["content"]]
211
258
 
212
259
 
260
+ def tokens(message) -> int:
261
+ """About how many tokens a message is: a quarter of its characters, an image as 1200."""
262
+ content = message["content"]
263
+ if isinstance(content, str):
264
+ return len(content) // 4
265
+ return sum(1200 if part["type"] == "input_image" else len(part.get("text") or "") // 4 for part in content)
266
+
267
+
268
+ def recent_start(messages: list[dict], minimum: int, least: int, most: int) -> int:
269
+ """Where the messages begin that a compaction keeps as they are: from the end back
270
+ until they hold `minimum` tokens and `least` messages, or `most` tokens; then on to a
271
+ reply of the model, so that no program is parted from its result. A conversation that
272
+ fits whole keeps its last exchange."""
273
+ total, start = 0, len(messages)
274
+ for index in range(len(messages) - 1, 0, -1):
275
+ total, start = total + tokens(messages[index]), index
276
+ if total >= most or (total >= minimum and len(messages) - index >= least):
277
+ break
278
+ if start <= 1:
279
+ start = max(1, len(messages) - 2)
280
+ while start < len(messages) - 1 and messages[start]["role"] != "assistant":
281
+ start += 1
282
+ return start
283
+
284
+
285
+ def clear_results(messages: list[dict], keep: int, cleared: str) -> int:
286
+ """Replace the results of programs in `messages` (the parts marked "result") by
287
+ `cleared`, but the last `keep`; how many were replaced."""
288
+ seen = replaced = 0
289
+ for message in reversed(messages):
290
+ if message["role"] != "user" or isinstance(message["content"], str):
291
+ continue
292
+ for part in reversed(message["content"]):
293
+ if part.get("result"):
294
+ seen += 1
295
+ if seen > keep and part["text"] != cleared:
296
+ part["text"], replaced = cleared, replaced + 1
297
+ return replaced
298
+
299
+
213
300
  def text_only(messages: list[dict]) -> list[dict]:
214
301
  """`messages` for a model that reads no images: each image as a line saying it was not sent."""
215
302
  out = []
@@ -223,39 +310,60 @@ def text_only(messages: list[dict]) -> list[dict]:
223
310
  return out
224
311
 
225
312
 
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
313
+ def content_parts(content) -> list[dict]:
314
+ """A message's content as conversations give it, as the parts the model is sent: a
315
+ string, or a list of parts: text ({"type": "text" | "input_text", "text"}) and images
316
+ ({"type": "image_url", "image_url": url or {"url"}}, {"type": "input_image",
317
+ "image_url"}, or {"type": "image", "source": {"type": "base64", "media_type", "data"}})."""
318
+ if isinstance(content, str):
319
+ return [{"type": "input_text", "text": content}] if content.strip() else []
320
+ parts = []
321
+ for part in content or ():
322
+ if isinstance(part, str):
323
+ parts.append({"type": "input_text", "text": part})
324
+ continue
325
+ kind = part.get("type") if isinstance(part, dict) else None
326
+ if kind in ("text", "input_text") and isinstance(part.get("text"), str):
327
+ parts.append({"type": "input_text", "text": part["text"]})
328
+ elif kind in ("image_url", "input_image"):
329
+ url = part.get("image_url")
330
+ url = url.get("url") if isinstance(url, dict) else url
331
+ if not isinstance(url, str) or not url:
332
+ raise ValueError("an image part gives its image_url")
333
+ parts.append({"type": "input_image", "image_url": url, "detail": part.get("detail") or "auto"})
334
+ elif kind == "image" and isinstance(part.get("source"), dict) and part["source"].get("type") == "base64":
335
+ source = part["source"]
336
+ parts.append({"type": "input_image", "image_url": f"data:{source['media_type']};base64,{source['data']}",
337
+ "detail": "auto"})
338
+ else:
339
+ raise ValueError(f"a message's content is text and image parts, not {part!r:.80}")
340
+ return parts
242
341
 
243
342
 
244
343
  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]
344
+ """Messages taken from the inbox as content parts: one text with each message of the
345
+ user or of the agent that started this one, then the files attached to them; then what
346
+ the environment told (TaskRun.tell), each as it gave it."""
347
+ people = [(sender, item, parts) for sender, item, parts in pending if not sender.startswith("environment")]
348
+ told = [(sender, parts) for sender, _, parts in pending if sender.startswith("environment")]
349
+ out = []
350
+ if people:
351
+ text = "\n".join(f"Message from {'the user' if sender == 'user' else 'the agent that started you'}: "
352
+ + (item or "(the files below)") for sender, item, _ in people)
353
+ out = [{"type": "input_text", "text": text}] + [part for *_, parts in people for part in parts]
354
+ for sender, parts in told:
355
+ out += [{"type": "input_text", "text": f"Message from the {sender}:"}, *parts]
356
+ return out
250
357
 
251
358
 
252
359
  def rebuild_messages(records) -> list[dict]:
253
360
  """The main model's history as the trace has it, up to its last reply: what each
254
361
  request added (from the previous reply on, in place of the last messages it
255
362
  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."""
363
+ reply to a call made while a program ran that was not used). A compaction replaces
364
+ every message there was (its next request says so). Image pruning is applied per
365
+ request (prune_images, prune_views), so the rebuilt list, pruned the same way, is the
366
+ conversation's own."""
259
367
  history, reply = [], None
260
368
  for record in records:
261
369
  if record["event"] == "model_request":