@lmmslab/harness 0.0.0-stage → 0.81.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 (170) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +124 -2
  3. package/bin/harness.js +71 -0
  4. package/package.json +34 -4
  5. package/python/harness/README.md +72 -0
  6. package/python/harness/__init__.py +8 -0
  7. package/python/harness/__main__.py +3 -0
  8. package/python/harness/bench.py +124 -0
  9. package/python/harness/builtin/benches/README.md +5 -0
  10. package/python/harness/builtin/benches/none/bench.toml +6 -0
  11. package/python/harness/builtin/configs/README.md +26 -0
  12. package/python/harness/builtin/configs/assistant.toml +11 -0
  13. package/python/harness/builtin/configs/default.toml +33 -0
  14. package/python/harness/builtin/configs/deploy/example-api.toml +10 -0
  15. package/python/harness/builtin/configs/models/deepseek-api.toml +21 -0
  16. package/python/harness/builtin/configs/models/deepseek-flash.toml +12 -0
  17. package/python/harness/builtin/configs/models/grok-low.toml +8 -0
  18. package/python/harness/builtin/configs/models/grok-medium.toml +43 -0
  19. package/python/harness/builtin/configs/models/opus-5.5.toml +11 -0
  20. package/python/harness/builtin/environment/README.md +53 -0
  21. package/python/harness/builtin/environment/constraints.txt +31 -0
  22. package/python/harness/builtin/model-settings.js +143 -0
  23. package/python/harness/builtin/prompts/conversation/core.md +5 -0
  24. package/python/harness/builtin/prompts/conversation/pack.toml +12 -0
  25. package/python/harness/builtin/prompts/core/core.md +88 -0
  26. package/python/harness/builtin/prompts/core/pack.toml +9 -0
  27. package/python/harness/builtin/prompts/reflect/core.md +45 -0
  28. package/python/harness/builtin/prompts/reflect/pack.toml +7 -0
  29. package/python/harness/builtin/prompts/shared/agent-skills.md +5 -0
  30. package/python/harness/builtin/prompts/shared/agents.md +14 -0
  31. package/python/harness/builtin/prompts/shared/checklist.md +21 -0
  32. package/python/harness/builtin/prompts/shared/entries.md +86 -0
  33. package/python/harness/builtin/prompts/shared/environments.md +21 -0
  34. package/python/harness/builtin/prompts/shared/learning-goal.md +31 -0
  35. package/python/harness/builtin/prompts/shared/learning.md +27 -0
  36. package/python/harness/builtin/prompts/shared/mcp.md +9 -0
  37. package/python/harness/builtin/prompts/shared/world.md +1 -0
  38. package/python/harness/builtin/skills/README.md +10 -0
  39. package/python/harness/builtin/skills/agents/__init__.py +88 -0
  40. package/python/harness/builtin/skills/agents/skill.toml +11 -0
  41. package/python/harness/builtin/skills/checklist/__init__.py +32 -0
  42. package/python/harness/builtin/skills/checklist/skill.toml +9 -0
  43. package/python/harness/builtin/skills/conversation/__init__.py +23 -0
  44. package/python/harness/builtin/skills/conversation/skill.toml +8 -0
  45. package/python/harness/builtin/skills/entries/__init__.py +62 -0
  46. package/python/harness/builtin/skills/entries/skill.toml +8 -0
  47. package/python/harness/builtin/skills/environments/__init__.py +69 -0
  48. package/python/harness/builtin/skills/environments/skill.toml +9 -0
  49. package/python/harness/builtin/skills/experience/__init__.py +27 -0
  50. package/python/harness/builtin/skills/experience/skill.toml +9 -0
  51. package/python/harness/builtin/skills/instructions/__init__.py +29 -0
  52. package/python/harness/builtin/skills/instructions/skill.toml +9 -0
  53. package/python/harness/builtin/skills/learning/__init__.py +189 -0
  54. package/python/harness/builtin/skills/learning/skill.toml +7 -0
  55. package/python/harness/builtin/skills/mcp/__init__.py +54 -0
  56. package/python/harness/builtin/skills/mcp/skill.toml +10 -0
  57. package/python/harness/builtin/skills/memory/__init__.py +130 -0
  58. package/python/harness/builtin/skills/memory/skill.toml +13 -0
  59. package/python/harness/builtin/skills/verify/__init__.py +31 -0
  60. package/python/harness/builtin/skills/verify/skill.toml +7 -0
  61. package/python/harness/builtin/third_party/sources.toml +40 -0
  62. package/python/harness/cli.py +1182 -0
  63. package/python/harness/config/__init__.py +224 -0
  64. package/python/harness/config/agent.py +777 -0
  65. package/python/harness/core/__init__.py +52 -0
  66. package/python/harness/core/artifacts.py +46 -0
  67. package/python/harness/core/codec.py +160 -0
  68. package/python/harness/core/contracts.py +75 -0
  69. package/python/harness/core/episodes.py +150 -0
  70. package/python/harness/core/format.py +51 -0
  71. package/python/harness/core/http.py +124 -0
  72. package/python/harness/core/lanes.py +324 -0
  73. package/python/harness/core/linux_sandbox.py +55 -0
  74. package/python/harness/core/media.py +96 -0
  75. package/python/harness/core/memory.py +147 -0
  76. package/python/harness/core/ports.py +63 -0
  77. package/python/harness/core/procs.py +227 -0
  78. package/python/harness/core/remote.py +102 -0
  79. package/python/harness/core/spans.py +166 -0
  80. package/python/harness/core/trace.py +139 -0
  81. package/python/harness/core/wait.py +12 -0
  82. package/python/harness/core/worker.py +388 -0
  83. package/python/harness/core/worker_pipe.py +119 -0
  84. package/python/harness/embodiments.py +217 -0
  85. package/python/harness/extensions.py +112 -0
  86. package/python/harness/host/__init__.py +397 -0
  87. package/python/harness/host/client.py +201 -0
  88. package/python/harness/host/http.py +720 -0
  89. package/python/harness/host/listing.py +68 -0
  90. package/python/harness/host/server.py +189 -0
  91. package/python/harness/host/sessions.py +406 -0
  92. package/python/harness/host/tasks.py +1308 -0
  93. package/python/harness/host/workers.py +257 -0
  94. package/python/harness/host/workspaces.py +256 -0
  95. package/python/harness/model_list.py +117 -0
  96. package/python/harness/packages/__init__.py +73 -0
  97. package/python/harness/packages/addons.py +121 -0
  98. package/python/harness/packages/archive.py +122 -0
  99. package/python/harness/packages/describe.py +46 -0
  100. package/python/harness/packages/environments.py +448 -0
  101. package/python/harness/packages/fetch.py +57 -0
  102. package/python/harness/packages/manager.py +587 -0
  103. package/python/harness/packages/manifest.py +221 -0
  104. package/python/harness/packages/migrate.py +68 -0
  105. package/python/harness/packages/provision.py +38 -0
  106. package/python/harness/packages/registry.py +275 -0
  107. package/python/harness/packages/resolve.py +89 -0
  108. package/python/harness/packages/sources.py +141 -0
  109. package/python/harness/packages/state.py +213 -0
  110. package/python/harness/packages/versions.py +112 -0
  111. package/python/harness/paths.py +218 -0
  112. package/python/harness/providers.py +303 -0
  113. package/python/harness/runtime/__init__.py +8 -0
  114. package/python/harness/runtime/decision/__init__.py +1 -0
  115. package/python/harness/runtime/decision/loop.py +198 -0
  116. package/python/harness/runtime/decision/messages.py +271 -0
  117. package/python/harness/runtime/decision/model.py +424 -0
  118. package/python/harness/runtime/decision/prompts.py +248 -0
  119. package/python/harness/runtime/decision/protocol.py +169 -0
  120. package/python/harness/runtime/decision/thoughts.py +199 -0
  121. package/python/harness/runtime/events.py +213 -0
  122. package/python/harness/runtime/execution/__init__.py +1 -0
  123. package/python/harness/runtime/execution/attached.py +96 -0
  124. package/python/harness/runtime/execution/control.py +211 -0
  125. package/python/harness/runtime/execution/entries.py +665 -0
  126. package/python/harness/runtime/execution/kernel.py +811 -0
  127. package/python/harness/runtime/execution/noenv.py +84 -0
  128. package/python/harness/runtime/execution/results.py +40 -0
  129. package/python/harness/runtime/execution/session.py +871 -0
  130. package/python/harness/runtime/execution/skills.py +436 -0
  131. package/python/harness/runtime/learning/__init__.py +1 -0
  132. package/python/harness/runtime/learning/active.py +314 -0
  133. package/python/harness/runtime/learning/experience.py +48 -0
  134. package/python/harness/runtime/learning/fixtures.py +167 -0
  135. package/python/harness/runtime/learning/learner.py +651 -0
  136. package/python/harness/runtime/learning/library.py +504 -0
  137. package/python/harness/runtime/learning/reflect.py +151 -0
  138. package/python/harness/runtime/learning/usage.py +30 -0
  139. package/python/harness/runtime/learning/views.py +89 -0
  140. package/python/harness/runtime/reactive/__init__.py +1 -0
  141. package/python/harness/runtime/reactive/rules.py +549 -0
  142. package/python/harness/runtime/safety/__init__.py +1 -0
  143. package/python/harness/runtime/safety/approval.py +131 -0
  144. package/python/harness/runtime/safety/input.py +62 -0
  145. package/python/harness/runtime/safety/isolation.py +419 -0
  146. package/python/harness/runtime/safety/shield.py +129 -0
  147. package/python/harness/runtime/status.py +23 -0
  148. package/python/harness/runtime/task/__init__.py +7 -0
  149. package/python/harness/runtime/task/context.py +431 -0
  150. package/python/harness/runtime/task/delegation.py +468 -0
  151. package/python/harness/runtime/task/environments.py +268 -0
  152. package/python/harness/runtime/task/run.py +2878 -0
  153. package/python/harness/runtime/tick.py +909 -0
  154. package/python/harness/sdk/__init__.py +281 -0
  155. package/python/harness/services/__init__.py +6 -0
  156. package/python/harness/services/broker.py +35 -0
  157. package/python/harness/services/connectors.py +88 -0
  158. package/python/harness/services/mcp.py +298 -0
  159. package/python/harness/services/mcp_stdio.py +131 -0
  160. package/python/harness/services/placement.py +82 -0
  161. package/python/harness/services/registry.py +156 -0
  162. package/python/harness/settings.py +536 -0
  163. package/python/harness/terminal.py +79 -0
  164. package/python/harness/transcript.py +297 -0
  165. package/python/harness/tui/__init__.py +68 -0
  166. package/python/harness/tui/chat.py +1181 -0
  167. package/python/harness/tui/pty.py +81 -0
  168. package/python/harness/tui/renderer.py +45 -0
  169. package/python/harness/tui/timeline_rows.py +58 -0
  170. package/python/harness/tui/views.py +434 -0
@@ -0,0 +1,324 @@
1
+ """The lanes of a run's timeline, read from its trace records: the one computation of them.
2
+ harness chat prefixes its lines with a lane's path, the terminal chat's timeline draws rows of them, and the
3
+ host serves a snapshot (Lanes.snapshot, GET /api/lanes) that the one web timeline (the app
4
+ package's timeline.js: the app, the overlay and a video) draws as it is.
5
+
6
+ A lane is one part that works on its own: the main model, the verifier, the agent's
7
+ programs (the root entry e0, runtime/execution/entries.py), each entry a program spawned (its
8
+ children under it, so a lane's path is the ids from e0 down: e0/e2/e5), the world
9
+ memory (a mark for each thing remembered, seen again or forgotten), and the
10
+ environment; above them, the markers of what happened (a user's message, the end, an error). A lane appears with the first record that needs it and an entry's lane ends
11
+ with its entry_end: a view shows the lanes that lived in the time it shows, so lanes come
12
+ and go as entries start and end. Each lane holds its spans (core/spans.py), whose `lane`
13
+ field says what kind of part recorded them. The model's lane holds a block for each
14
+ call, never more: between calls it is empty, also while programs run (pipelining shows
15
+ as blocks of both lanes with gaps in each). A call made while a program ran whose reply
16
+ was not used (an `ahead` record: discarded or unused) is marked on its span (`ahead`)
17
+ and drawn faint. While a call streams, its text so far is the lane's `text`
18
+ (model_text records of that call).
19
+
20
+ Standard library only; reads, never writes.
21
+ """
22
+
23
+ import math
24
+
25
+ # The lanes that are not entries, in the order they are drawn (entries go between the
26
+ # verifier and the environment).
27
+ FIXED = ("model", "verifier", "world", "env")
28
+ ROOT = "e0"
29
+ # Characters of the streamed text a lane keeps (the end of it).
30
+ TEXT = 600
31
+ # What became of a call made while a program ran when its reply was not used.
32
+ FAINT = ("discarded", "unused")
33
+ # The marks of what happened, by event (also a paused run: "‖", and a program's escalation: "▼").
34
+ MARKERS = {"user_message": "●", "agent_message": "○", "finish": "■", "error": "×", "kernel_died": "×",
35
+ "stuck": "!", "confirmation_request": "?", "protocol_error": "~"}
36
+
37
+
38
+ class Lane(dict):
39
+ """id, parent (a lane id or None), kind (model, verifier, env, program, agent ...),
40
+ label, claims (an entry's channels, as they change: entry_claims), start, end (None while it lives), status, spans (dicts in start order)
41
+ and text (the model's streamed text of call `call`, the one that runs)."""
42
+
43
+
44
+ class Lanes:
45
+ """The lanes of one run, folded from its records in order (add)."""
46
+
47
+ def __init__(self):
48
+ self.lanes = {}
49
+ self.spans = {}
50
+ self.t0 = None
51
+ self.last = None
52
+ self.ended = False
53
+ self.markers = []
54
+ # A trace written before spans: the spans its records imply (the model's calls from
55
+ # request to reply, each program for its duration), used while it has no span of its own.
56
+ self.has_spans = False
57
+ self.implied = []
58
+ self._request = None
59
+
60
+ def add(self, record) -> str | None:
61
+ """Fold one trace record in; returns the path of the lane it belongs to (None: the
62
+ conversation's own, a user's message or a status)."""
63
+ time = record.get("time")
64
+ if isinstance(time, (int, float)):
65
+ self.t0 = time if self.t0 is None else min(self.t0, time)
66
+ self.last = time if self.last is None else max(self.last, time)
67
+ kind = record.get("event") or ""
68
+ if kind in ("outcome", "interrupted") or kind == "state" and record.get("status") in ("FINISHED", "ERROR"):
69
+ self.ended = True
70
+ self._mark(record, kind, time)
71
+ if kind == "span_start":
72
+ self.has_spans = True
73
+ lane = self._span_lane(record)
74
+ span = {"id": record.get("id"), "lane": lane["id"], "part": record.get("lane"), "name": record.get("name"),
75
+ "parent": record.get("parent"), "start": time, "end": None, "call": record.get("call"),
76
+ "seq": record.get("seq")}
77
+ self.spans[span["id"]] = span
78
+ lane["spans"].append(span)
79
+ if lane["kind"] == "model":
80
+ lane.update(text="", call=record.get("call"))
81
+ return self.path(lane["id"])
82
+ if kind == "span_end":
83
+ span = self.spans.get(record.get("id"))
84
+ if span is None:
85
+ return None
86
+ span["end"] = time
87
+ span["status"] = record.get("event_kind") or ("error" if record.get("error") else None)
88
+ return self.path(span["lane"])
89
+ if kind == "entry_start":
90
+ lane = self._entry(record.get("id"), record.get("parent") or ROOT)
91
+ lane.update(kind=record.get("kind") or "program", label=_label(record), claims=list(record.get("claims") or []),
92
+ start=time, agent=record.get("agent"))
93
+ return self.path(lane["id"])
94
+ if kind == "entry_claims":
95
+ lane = self.lanes.get(record.get("id"))
96
+ if lane is None:
97
+ return None
98
+ lane["claims"] = list(record.get("claims") or [])
99
+ return self.path(lane["id"])
100
+ if kind == "entry_end":
101
+ lane = self.lanes.get(record.get("id"))
102
+ if lane is None:
103
+ return None
104
+ lane.update(end=time, status=record.get("status"), error=record.get("error") or record.get("event_kind"))
105
+ return self.path(lane["id"])
106
+ if kind in ("model_request", "model_reply", "model_text"):
107
+ lane = self._fixed("model", time)
108
+ # Calls can overlap (one made while a program ran is still out when the model is
109
+ # asked again) and the reply to one comes when its program ends, maybe while the
110
+ # next call streams: the lane keeps the newest call's text, and only that call's
111
+ # reply clears it.
112
+ if kind == "model_text":
113
+ call = record.get("call")
114
+ if call != lane.get("call"):
115
+ if _older(call, lane.get("call")):
116
+ return "model"
117
+ lane.update(text="", call=call)
118
+ lane["text"] = (lane["text"] + str(record.get("text") or ""))[-TEXT:]
119
+ elif kind == "model_reply" and record.get("call") == lane.get("call"):
120
+ lane["text"] = ""
121
+ return "model"
122
+ if kind == "ahead":
123
+ lane = self._fixed("model", time)
124
+ span = next((span for span in reversed(lane["spans"]) if span.get("call") == record.get("call")), None)
125
+ if span is not None:
126
+ span["ahead"] = record.get("outcome")
127
+ return "model"
128
+ if kind == "verify":
129
+ self._fixed("verifier", time)
130
+ return "verifier"
131
+ if kind == "world":
132
+ # A mark: the moment the memory changed.
133
+ lane = self._fixed("world", time)
134
+ lane["spans"].append({"id": f"w{len(lane['spans'])}", "lane": "world", "part": "world",
135
+ "name": f"{record.get('action')} {record.get('kind')}", "parent": None,
136
+ "start": time, "end": time, "call": None})
137
+ return "world"
138
+ if kind == "cell":
139
+ self._entry(ROOT, None)
140
+ return ROOT
141
+ if kind in ("reset", "outcome", "episode_video"):
142
+ self._fixed("env", time)
143
+ return "env"
144
+ if kind == "agent_report":
145
+ lane = next((lane for lane in self.lanes.values() if lane.get("agent") == record.get("agent")), None)
146
+ return self.path(lane["id"]) if lane else ROOT
147
+ if kind.startswith("agent_"):
148
+ return ROOT
149
+ return None
150
+
151
+ def _mark(self, record, kind, time) -> None:
152
+ """The marks of what happened, and the spans a trace without spans implies."""
153
+ what = None
154
+ if kind == "state" and record.get("status") == "PAUSED":
155
+ glyph, what = "‖", "paused"
156
+ elif kind == "cell" and (record.get("result") or {}).get("escalation"):
157
+ escalation = record["result"]["escalation"]
158
+ glyph, what = "▼", f"{(escalation.get('event') or {}).get('kind')} in {escalation.get('skill')}"
159
+ if what is not None:
160
+ self.markers.append({"time": time, "glyph": glyph, "what": what, "seq": record.get("seq")})
161
+ if kind in MARKERS:
162
+ said = record.get("text") or record.get("message") or record.get("repeated") or kind
163
+ self.markers.append({"time": time, "glyph": MARKERS[kind], "what": f"{kind}: {str(said)[:160]}",
164
+ "seq": record.get("seq")})
165
+ if kind == "model_request":
166
+ self._request = record
167
+ elif kind == "model_reply" and self._request is not None:
168
+ self.implied.append({"id": f"m{record.get('seq')}", "lane": "model", "part": "model",
169
+ "name": f"call {record.get('call')}", "parent": None, "start": self._request.get("time"),
170
+ "end": time, "call": record.get("call"), "seq": self._request.get("seq"),
171
+ "derived": True})
172
+ self._request = None
173
+ elif kind == "cell":
174
+ seconds = (record.get("result") or {}).get("seconds") or 0
175
+ self.implied.append({"id": f"c{record.get('seq')}", "lane": ROOT, "part": "program", "name": "program",
176
+ "parent": None, "start": time - seconds, "end": time, "call": None,
177
+ "seq": record.get("seq"), "derived": True,
178
+ "status": (record.get("result") or {}).get("status")})
179
+
180
+ def snapshot(self) -> dict:
181
+ """The lanes as every view draws them: {t0, last, ended, lanes, markers}; each lane
182
+ in drawing order (ordered) with its path, depth, first (the time it appeared) and its
183
+ spans; a trace without spans has the lanes of the spans its records imply."""
184
+ out = []
185
+ for lane in self.ordered():
186
+ starts = [value for value in (lane["start"], *(span["start"] for span in lane["spans"][:1])) if value is not None]
187
+ out.append({key: lane.get(key) for key in ("id", "parent", "kind", "label", "agent", "claims", "start", "end",
188
+ "status", "error")}
189
+ | {"path": self.path(lane["id"]), "depth": self.depth(lane["id"]),
190
+ "first": min(starts) if starts else None,
191
+ "spans": [{key: span.get(key) for key in ("id", "lane", "part", "name", "parent", "start", "end",
192
+ "status", "ahead", "call", "seq", "derived")}
193
+ for span in lane["spans"]]})
194
+ return {"t0": self.t0, "last": self.last, "ended": self.ended, "lanes": out, "markers": list(self.markers)}
195
+
196
+ def _implied_lanes(self) -> "Lanes":
197
+ implied = Lanes()
198
+ for span in self.implied:
199
+ lane = implied._fixed("model", span["start"]) if span["part"] == "model" else implied._entry(ROOT, None)
200
+ lane["spans"].append(dict(span, lane=lane["id"]))
201
+ return implied
202
+
203
+ def _fixed(self, name, time) -> Lane:
204
+ lane = self.lanes.get(name)
205
+ if lane is None:
206
+ lane = self.lanes[name] = Lane(id=name, parent=None, kind=name, label=name, claims=[], start=time,
207
+ end=None, status=None, spans=[], text="")
208
+ return lane
209
+
210
+ def _entry(self, identifier, parent) -> Lane:
211
+ lane = self.lanes.get(identifier)
212
+ if lane is None:
213
+ if parent is not None and parent not in self.lanes:
214
+ self._entry(parent, None if parent == ROOT else ROOT)
215
+ root = identifier == ROOT
216
+ lane = self.lanes[identifier] = Lane(id=identifier, parent=None if root else parent, kind="program",
217
+ label="programs" if root else identifier, claims=[], start=None,
218
+ end=None, status=None, spans=[], text="")
219
+ return lane
220
+
221
+ def _span_lane(self, record) -> Lane:
222
+ """A span's lane: the model's, the verifier's and the environment's spans their own;
223
+ a span of an entry's code (`entry`) that entry's; a skill or service call its parent
224
+ span's; the rest (a program) the agent's programs."""
225
+ part = record.get("lane")
226
+ if part in FIXED:
227
+ return self._fixed(part, record.get("time"))
228
+ if record.get("entry"):
229
+ return self._entry(record["entry"], ROOT)
230
+ parent = self.spans.get(record.get("parent"))
231
+ if parent is not None:
232
+ return self.lanes[parent["lane"]]
233
+ return self._entry(ROOT, None)
234
+
235
+ def path(self, identifier) -> str:
236
+ """A lane's ids from the root entry down (e0/e2/e5); a lane that is not an entry
237
+ its name."""
238
+ parts = []
239
+ while identifier is not None:
240
+ parts.append(identifier)
241
+ identifier = (self.lanes.get(identifier) or {}).get("parent")
242
+ return "/".join(reversed(parts))
243
+
244
+ def depth(self, identifier) -> int:
245
+ return self.path(identifier).count("/")
246
+
247
+ def ordered(self) -> list[Lane]:
248
+ """The lanes in the order they are drawn: the model, the verifier, the world memory,
249
+ the entries (each under its parent, in the order they started), the environment."""
250
+ if not self.has_spans and self.implied:
251
+ return self._implied_lanes().ordered()
252
+ children = {}
253
+ for lane in self.lanes.values():
254
+ if lane["id"] not in FIXED:
255
+ children.setdefault(lane["parent"], []).append(lane)
256
+ entries = []
257
+
258
+ def walk(parent):
259
+ for lane in children.get(parent, []):
260
+ entries.append(lane)
261
+ walk(lane["id"])
262
+ walk(None)
263
+ lanes = [self.lanes[name] for name in ("model", "verifier", "world") if name in self.lanes]
264
+ return lanes + entries + ([self.lanes["env"]] if "env" in self.lanes else [])
265
+
266
+ def visible(self, t0, t1, now=None) -> list[Lane]:
267
+ """The lanes that lived between t0 and t1 (times as the records have them): a lane
268
+ starts with its first span or its entry, and ends with its entry (else it lives on)."""
269
+ shown = []
270
+ for lane in self.ordered():
271
+ starts = [value for value in (lane["start"], *(span["start"] for span in lane["spans"][:1]))
272
+ if value is not None]
273
+ start = min(starts) if starts else None
274
+ end = lane["end"]
275
+ if start is not None and start > t1 or end is not None and end < t0:
276
+ continue
277
+ shown.append(lane)
278
+ return shown
279
+
280
+ def columns(self, lane, t0, t1, width, now, faint=None) -> list[bool]:
281
+ """For each of `width` equal stretches of t0 to t1, whether one of the lane's spans
282
+ ran in it; a span still open runs until `now`. `faint` True counts only the calls
283
+ whose reply was not used (FAINT), False only the others, None all."""
284
+ step = (t1 - t0) / max(width, 1)
285
+ busy = [False] * width
286
+ for span in lane["spans"]:
287
+ if faint is not None and (span.get("ahead") in FAINT) != faint:
288
+ continue
289
+ start, end = span["start"], span["end"] if span["end"] is not None else now
290
+ if start is None or end is None or start > t1 or end < t0:
291
+ continue
292
+ first = max(int((start - t0) / step), 0) if step > 0 else 0
293
+ # A span that ends on a column's edge does not reach into the next one.
294
+ last = min(max(math.ceil((end - t0) / step) - 1, first), width - 1) if step > 0 else width - 1
295
+ for column in range(first, last + 1):
296
+ busy[column] = True
297
+ return busy
298
+
299
+ def alive(self, lane, a, b, now) -> bool:
300
+ """Whether a spawned entry's lane lived at some time between a and b (the agent's
301
+ programs and the other lanes only have their spans)."""
302
+ if lane["kind"] in FIXED or lane["id"] == ROOT or lane["start"] is None:
303
+ return False
304
+ end = lane["end"] if lane["end"] is not None else now
305
+ return lane["start"] < b and end >= a
306
+
307
+ def now(self, clock=None) -> float:
308
+ """The time a view ends at: the last record's once the run ended, else `clock`."""
309
+ if self.ended or clock is None:
310
+ return self.last if self.last is not None else 0.0
311
+ return max(clock, self.last or clock)
312
+
313
+
314
+ def _older(call, than) -> bool:
315
+ """Whether call number `call` was made before call `than`."""
316
+ return isinstance(call, int) and isinstance(than, int) and call < than
317
+
318
+
319
+ def _label(record) -> str:
320
+ """An entry's label: its name, and the sub-agent it is."""
321
+ name = str(record.get("name") or record.get("id"))
322
+ if record.get("agent") and record["agent"] not in name:
323
+ return f"{name} ({record['agent']})"
324
+ return name
@@ -0,0 +1,55 @@
1
+ """Common Linux namespace launch and process-handle protection."""
2
+
3
+ import os
4
+ from pathlib import Path
5
+ import stat
6
+ import sys
7
+
8
+
9
+ def _trusted_bwrap():
10
+ configured = os.environ.get("HARNESS_BWRAP")
11
+ path = Path(configured or "/usr/bin/bwrap")
12
+ if not path.is_absolute():
13
+ raise ValueError("HARNESS_BWRAP must be an absolute path")
14
+ path = path.resolve(strict=True)
15
+ trusted_owners = {0, os.geteuid()} if configured else {0}
16
+ for item in (path, *path.parents):
17
+ metadata = item.stat()
18
+ if metadata.st_uid not in trusted_owners or metadata.st_mode & (stat.S_ISUID | stat.S_ISGID | 0o022):
19
+ raise PermissionError("The Bubblewrap executable or its parent directory is not trusted")
20
+ if not path.is_file() or not os.access(path, os.X_OK):
21
+ raise ValueError("The Bubblewrap path is not executable")
22
+ return str(path)
23
+
24
+
25
+ def require_linux():
26
+ if sys.platform != "linux" or os.geteuid() == 0:
27
+ raise RuntimeError("Managed workspaces require unprivileged Linux")
28
+ try:
29
+ return _trusted_bwrap()
30
+ except (OSError, ValueError) as error:
31
+ raise RuntimeError(f"Managed workspaces require a trusted Bubblewrap executable: {error}") from None
32
+
33
+
34
+ def bubblewrap_command(paths, env):
35
+ """Start an offline namespace with only explicitly mounted runtime paths."""
36
+ # Explicit flags fail if a namespace cannot be created. Never share the host network.
37
+ launch = [require_linux(), "--unshare-user", "--unshare-pid", "--unshare-net", "--unshare-ipc", "--unshare-uts",
38
+ "--new-session", "--die-with-parent", "--cap-drop", "ALL", "--clearenv",
39
+ "--proc", "/proc", "--dev", "/dev", "--tmpfs", "/dev/shm", "--tmpfs", "/tmp",
40
+ "--dir", "/tmp/home", "--perms", "0700", "--dir", "/tmp/run", "--chdir", "/tmp/home"]
41
+ system = ("/usr", "/bin", "/lib", "/lib64", "/etc/ld.so.cache")
42
+ for item in sorted({str(path) for path in paths} | {path for path in system if Path(path).exists()}):
43
+ launch += ["--ro-bind", item, item]
44
+ for key, value in {"PATH": "/usr/bin:/bin", "HOME": "/tmp/home", "XDG_RUNTIME_DIR": "/tmp/run",
45
+ "PYTHONNOUSERSITE": "1", "PYTHONDONTWRITEBYTECODE": "1", **env}.items():
46
+ launch += ["--setenv", key, value]
47
+ return launch
48
+
49
+
50
+ def protect_process_handles():
51
+ """Prevent other processes in the namespace from opening this process's descriptors."""
52
+ import ctypes
53
+
54
+ if ctypes.CDLL(None, use_errno=True).prctl(4, 0, 0, 0, 0) != 0: # PR_SET_DUMPABLE
55
+ raise RuntimeError("Could not protect the managed worker's process handles")
@@ -0,0 +1,96 @@
1
+ """Videos: action videos beside an episode trace, and the one ffmpeg and probe() every package uses."""
2
+
3
+ import datetime
4
+ import re
5
+ import subprocess
6
+ import shutil
7
+ from pathlib import Path
8
+
9
+ from .codec import decode_data_url
10
+
11
+ _VIDEO_PREFIX = "data:video/mp4;base64,"
12
+ # A first frame's time past this (2001 on) is a wall-clock time: a recording made with
13
+ # -use_wallclock_as_timestamps and -copyts (the computer package's screen recordings).
14
+ _WALL_CLOCK = 1e9
15
+ # probe()'s start_from for a start read from the frames' own (wall-clock) times.
16
+ FRAMES = "its frames' times"
17
+
18
+
19
+ def ffmpeg() -> str | None:
20
+ """The ffmpeg on PATH, else the imageio-ffmpeg package's, else None."""
21
+ found = shutil.which("ffmpeg")
22
+ if found:
23
+ return found
24
+ try:
25
+ import imageio_ffmpeg
26
+
27
+ return imageio_ffmpeg.get_ffmpeg_exe()
28
+ except Exception:
29
+ return None
30
+
31
+
32
+ def probe(path, executable: str | None = None) -> dict:
33
+ """A video as ffmpeg reads it: `duration` (its length in seconds), `size` [width, height],
34
+ `fps`, and `start`, the wall-clock time (epoch seconds) of its first frame, with
35
+ `start_from` saying how that is known: the frames' own times (ffmpeg's start: when it is
36
+ a wall-clock time; its Duration then counts from 0, so the length is the difference),
37
+ else the file's creation_time tag, else the file's time minus its length (a recorder
38
+ closes the file as the recording ends). {"error": ...} when there is no ffmpeg or it
39
+ cannot read the file."""
40
+ path = Path(path)
41
+ executable = executable or ffmpeg()
42
+ if executable is None:
43
+ return {"error": "no ffmpeg (on PATH, or the imageio-ffmpeg package)"}
44
+ if not path.is_file():
45
+ return {"error": f"no file {path}"}
46
+ result = subprocess.run([executable, "-hide_banner", "-i", str(path)], capture_output=True, text=True,
47
+ encoding="utf-8", errors="replace")
48
+ text = result.stderr
49
+ match = re.search(r"Duration:\s*(\d+):(\d\d):(\d\d(?:\.\d+)?)", text)
50
+ if not match:
51
+ return {"error": f"ffmpeg cannot read {path.name}: {text.strip().splitlines()[-1:] or 'no output'}"}
52
+ duration = int(match.group(1)) * 3600 + int(match.group(2)) * 60 + float(match.group(3))
53
+ first = re.search(r"start:\s*([0-9.]+)", text)
54
+ start, start_from = None, None
55
+ if first and float(first.group(1)) > _WALL_CLOCK:
56
+ start, start_from = float(first.group(1)), FRAMES
57
+ duration -= start
58
+ created = re.search(r"creation_time\s*:\s*(\S+)", text)
59
+ if start is None and created:
60
+ try:
61
+ start = datetime.datetime.fromisoformat(created.group(1).replace("Z", "+00:00")).timestamp()
62
+ start_from = "its creation_time tag"
63
+ except ValueError:
64
+ pass
65
+ if start is None:
66
+ start, start_from = path.stat().st_mtime - duration, "the file's time minus its length"
67
+ size = re.search(r"Video: .*?, (\d{2,5})x(\d{2,5})", text)
68
+ rate = re.search(r"(\d+(?:\.\d+)?) fps", text)
69
+ return {"duration": duration, "start": start, "start_from": start_from,
70
+ "size": [int(size.group(1)), int(size.group(2))] if size else None,
71
+ "fps": float(rate.group(1)) if rate else None, "ffmpeg": executable}
72
+
73
+
74
+ def decode_video_data_url(value: str) -> bytes:
75
+ if not isinstance(value, str) or not value.startswith(_VIDEO_PREFIX):
76
+ raise ValueError("Video must be an MP4 data URL")
77
+ payload = decode_data_url(value)
78
+ if len(payload) < 12 or payload[4:8] != b"ftyp":
79
+ raise ValueError("Video payload is not an MP4")
80
+ return payload
81
+
82
+
83
+ def write_episode_videos(directory: Path, videos: dict) -> dict[str, str]:
84
+ """Write one whole-episode MP4 per camera and return paths relative to the episode directory."""
85
+ if not videos:
86
+ return {}
87
+ folder = directory / "videos"
88
+ folder.mkdir(exist_ok=True)
89
+ written = {}
90
+ for camera, url in videos.items():
91
+ if not isinstance(camera, str) or not camera or "/" in camera or camera in (".", ".."):
92
+ raise ValueError("Video camera name is not a single path segment")
93
+ name = f"episode-{camera}.mp4"
94
+ (folder / name).write_bytes(decode_video_data_url(url))
95
+ written[camera] = f"videos/{name}"
96
+ return written
@@ -0,0 +1,147 @@
1
+ """Project recorded task context and library holdings for replay and frontends."""
2
+ import json
3
+ from pathlib import Path
4
+ from harness.runtime.task.context import WorkingMemory, entry
5
+ from harness.runtime.learning.library import LAYERS, SECTIONS, Library, Revision, executable, selects
6
+ from harness.runtime.learning.views import _library_entry, library_entries, overlay
7
+
8
+
9
+ COLUMNS = ("scope", "kind", "name", "where", "evidence", "source", "seen", "text")
10
+
11
+
12
+ def from_records(records) -> WorkingMemory:
13
+ """A run's working memory as its trace's `world` records left it."""
14
+ memory = WorkingMemory(clock=lambda: 0.0)
15
+ for record in records:
16
+ if (record.get("event") != "world" or "id" not in record
17
+ or record.get("action") not in ("remember", "seen", "carried", "forget")):
18
+ continue
19
+ if record.get("action") == "forget":
20
+ memory.items.pop(record["id"], None)
21
+ continue
22
+ seen_at = record.get("time") or 0.0
23
+ item = memory.items.setdefault(record["id"], {"first": seen_at})
24
+ # Before runtime/task/context.py, the world memory's records said pos, note and by.
25
+ item.update({{"by": "source", "pos": "where", "note": "text"}.get(key, key): value for key, value in record.items()
26
+ if key not in ("event", "seq", "time", "action", "who", "source", "view")}, last=seen_at)
27
+ item.setdefault("text", "")
28
+ for key in ("where", "name", "space", "confidence", "source", "seen", "version"):
29
+ item.setdefault(key, None)
30
+ return memory
31
+
32
+
33
+ def rows(run=None, library=None, *, at=None) -> list[dict]:
34
+ """What every frontend shows of memory, in the one format: a run's working memory (from
35
+ its trace; `run` a run directory) and a library's entries (`library` a directory; by
36
+ default the run's own, as its trace recorded it), each with its "cells" (COLUMNS)."""
37
+ from harness.core.episodes import read_records
38
+ from harness.runtime.learning.learner import Learner
39
+
40
+ found = []
41
+ if run is not None:
42
+ run = Path(run)
43
+ def history(path):
44
+ return [record for record in read_records(path)
45
+ if at is None or (record.get("time") or 0) <= at]
46
+
47
+ main = history(run) if (run / "events.jsonl").is_file() else []
48
+ children = {trace.parent: history(trace.parent) for trace in sorted(run.glob("agents/**/events.jsonl"))}
49
+ records = sorted([*main, *(record for trace in children.values() for record in trace)],
50
+ key=lambda record: record.get("time") or 0)
51
+ found += [dict(entry(item), cells=None) for item in from_records(records).items.values()]
52
+ if library is None:
53
+ return _cells(list({item["id"]: item for item in [*found, *_started_with(run, main, children)]}.values()))
54
+ if library is not None and Path(library).is_dir():
55
+ store = Library(library)
56
+ captured = store.capture(include_unselected=True)
57
+ found.extend(library_entries(captured, sections=SECTIONS))
58
+ for key, row in captured.index.items():
59
+ if not key.startswith("candidates/") or not row.get("revision"):
60
+ continue
61
+ item = Revision.read(store.directory, row["revision"])
62
+ candidate = _library_entry(item.key, item.source.decode("utf-8"), row, captured.version,
63
+ evidence="proposed", sha256=item.source_sha256)
64
+ found.append({**candidate, "id": key, "revision": item.revision, "status": "pending"})
65
+ return _cells(found)
66
+
67
+
68
+ def _library_table(run) -> dict:
69
+ try:
70
+ manifest = json.loads((run / "manifest.json").read_text(encoding="utf-8"))
71
+ return manifest["config"]["runtime"]["library"] or {}
72
+ except (OSError, ValueError, KeyError, TypeError):
73
+ return {}
74
+
75
+
76
+ def _started_with(run, records, children) -> list[dict]:
77
+ """Historical holdings. Older manifests can name entries but cannot supply their text."""
78
+ table = _library_table(run)
79
+ library = table.get("manifest") or {}
80
+ opened = next((r for r in records if r.get("event") == "library" and r.get("action") == "opened"), None)
81
+ if opened is not None:
82
+ found = {item["id"]: {**entry(item, "library"), **item} for item in opened["entries"]}
83
+ else:
84
+ found = {}
85
+ for section, names in (library.get("entries") or {}).items():
86
+ for name in names:
87
+ key = f"{section}/{name}"
88
+ found[key] = _library_entry(key, "", {}, library.get("version"), evidence="unrecorded")
89
+ versioned = opened is not None and opened.get("revision_schema") == 1
90
+ for record in records:
91
+ if record.get("event") == "library" and record.get("action") == "activated" and "entries" in record:
92
+ found.update({item["id"]: item for item in record["entries"]})
93
+ if table.get("learn", "on" if library.get("learns") else "off") != "on":
94
+ return list(found.values())
95
+ directory = library.get("directory")
96
+ layers = library.get("layers", table.get("layers", LAYERS))
97
+ load = library.get("load", table.get("load", "proposed"))
98
+ sections = (opened or {}).get("sections", SECTIONS)
99
+ since = (opened or {}).get("time") or 0
100
+ related = (record for child, trace in children.items()
101
+ if directory and (_library_table(child).get("manifest") or {}).get("directory") == directory
102
+ for record in trace if record.get("event") == "library")
103
+ # Keep the latest known revision even below the floor, in case it earns a qualifying grade.
104
+ latest = dict(found)
105
+ for record in sorted([*records, *related], key=lambda record: record.get("time") or 0):
106
+ if record.get("event") != "library" or (record.get("time") or 0) < since:
107
+ continue
108
+ if record.get("action") == "memory":
109
+ item = record["holding"]
110
+ found = {row["id"]: row for row in overlay(found.values(), [item], layers, load)}
111
+ continue
112
+ section = record.get("section") or str(record.get("key", "")).partition("/")[0]
113
+ if section not in sections or versioned and executable(section + "/"):
114
+ continue
115
+ evidence, digest = record.get("evidence"), record.get("sha256")
116
+ selected = selects(section, evidence, layers, load)
117
+ if record.get("action") == "admitted" and record.get("admitted"):
118
+ key = f"{record['section']}/{record['name']}"
119
+ item = record.get("holding") or _library_entry(key, "", {"origin": record.get("origin")},
120
+ record.get("version"), evidence=evidence)
121
+ latest[key] = {**item, "sha256": digest}
122
+ if selected:
123
+ found.pop(key, None)
124
+ found[key] = latest[key]
125
+ elif record.get("action") == "promoted" and digest:
126
+ key = record["key"]
127
+ item = latest.get(key)
128
+ if item is None or item.get("sha256") != digest:
129
+ continue
130
+ history = [*(item.get("history") or ()), *(record.get("history") or ())[-1:]]
131
+ latest[key] = {**item, "evidence": evidence, "history": history}
132
+ if selected:
133
+ found[key] = latest[key]
134
+ elif found.get(key, {}).get("sha256") == digest:
135
+ del found[key]
136
+ return list(found.values())
137
+
138
+
139
+ def _cells(found) -> list[dict]:
140
+ for row in found:
141
+ where, given = row["where"], row.get("cells") or [row["evidence"], row["name"] or ""]
142
+ rate = row.get("rate")
143
+ row["cells"] = [row["scope"], row["kind"], given[1], " ".join(f"{v:g}" for v in where)
144
+ if isinstance(where, list) else where or "", given[0], row["source"] or "",
145
+ str(row["seen"] or "") + (f" ({rate:.0%} ok)" if rate is not None else ""),
146
+ (row["text"] or "")[:80]]
147
+ return found