@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.
- package/LICENSE +21 -0
- package/README.md +124 -2
- package/bin/harness.js +71 -0
- package/package.json +34 -4
- package/python/harness/README.md +72 -0
- package/python/harness/__init__.py +8 -0
- package/python/harness/__main__.py +3 -0
- package/python/harness/bench.py +124 -0
- package/python/harness/builtin/benches/README.md +5 -0
- package/python/harness/builtin/benches/none/bench.toml +6 -0
- package/python/harness/builtin/configs/README.md +26 -0
- package/python/harness/builtin/configs/assistant.toml +11 -0
- package/python/harness/builtin/configs/default.toml +33 -0
- package/python/harness/builtin/configs/deploy/example-api.toml +10 -0
- package/python/harness/builtin/configs/models/deepseek-api.toml +21 -0
- package/python/harness/builtin/configs/models/deepseek-flash.toml +12 -0
- package/python/harness/builtin/configs/models/grok-low.toml +8 -0
- package/python/harness/builtin/configs/models/grok-medium.toml +43 -0
- package/python/harness/builtin/configs/models/opus-5.5.toml +11 -0
- package/python/harness/builtin/environment/README.md +53 -0
- package/python/harness/builtin/environment/constraints.txt +31 -0
- package/python/harness/builtin/model-settings.js +143 -0
- package/python/harness/builtin/prompts/conversation/core.md +5 -0
- package/python/harness/builtin/prompts/conversation/pack.toml +12 -0
- package/python/harness/builtin/prompts/core/core.md +88 -0
- package/python/harness/builtin/prompts/core/pack.toml +9 -0
- package/python/harness/builtin/prompts/reflect/core.md +45 -0
- package/python/harness/builtin/prompts/reflect/pack.toml +7 -0
- package/python/harness/builtin/prompts/shared/agent-skills.md +5 -0
- package/python/harness/builtin/prompts/shared/agents.md +14 -0
- package/python/harness/builtin/prompts/shared/checklist.md +21 -0
- package/python/harness/builtin/prompts/shared/entries.md +86 -0
- package/python/harness/builtin/prompts/shared/environments.md +21 -0
- package/python/harness/builtin/prompts/shared/learning-goal.md +31 -0
- package/python/harness/builtin/prompts/shared/learning.md +27 -0
- package/python/harness/builtin/prompts/shared/mcp.md +9 -0
- package/python/harness/builtin/prompts/shared/world.md +1 -0
- package/python/harness/builtin/skills/README.md +10 -0
- package/python/harness/builtin/skills/agents/__init__.py +88 -0
- package/python/harness/builtin/skills/agents/skill.toml +11 -0
- package/python/harness/builtin/skills/checklist/__init__.py +32 -0
- package/python/harness/builtin/skills/checklist/skill.toml +9 -0
- package/python/harness/builtin/skills/conversation/__init__.py +23 -0
- package/python/harness/builtin/skills/conversation/skill.toml +8 -0
- package/python/harness/builtin/skills/entries/__init__.py +62 -0
- package/python/harness/builtin/skills/entries/skill.toml +8 -0
- package/python/harness/builtin/skills/environments/__init__.py +69 -0
- package/python/harness/builtin/skills/environments/skill.toml +9 -0
- package/python/harness/builtin/skills/experience/__init__.py +27 -0
- package/python/harness/builtin/skills/experience/skill.toml +9 -0
- package/python/harness/builtin/skills/instructions/__init__.py +29 -0
- package/python/harness/builtin/skills/instructions/skill.toml +9 -0
- package/python/harness/builtin/skills/learning/__init__.py +189 -0
- package/python/harness/builtin/skills/learning/skill.toml +7 -0
- package/python/harness/builtin/skills/mcp/__init__.py +54 -0
- package/python/harness/builtin/skills/mcp/skill.toml +10 -0
- package/python/harness/builtin/skills/memory/__init__.py +130 -0
- package/python/harness/builtin/skills/memory/skill.toml +13 -0
- package/python/harness/builtin/skills/verify/__init__.py +31 -0
- package/python/harness/builtin/skills/verify/skill.toml +7 -0
- package/python/harness/builtin/third_party/sources.toml +40 -0
- package/python/harness/cli.py +1182 -0
- package/python/harness/config/__init__.py +224 -0
- package/python/harness/config/agent.py +777 -0
- package/python/harness/core/__init__.py +52 -0
- package/python/harness/core/artifacts.py +46 -0
- package/python/harness/core/codec.py +160 -0
- package/python/harness/core/contracts.py +75 -0
- package/python/harness/core/episodes.py +150 -0
- package/python/harness/core/format.py +51 -0
- package/python/harness/core/http.py +124 -0
- package/python/harness/core/lanes.py +324 -0
- package/python/harness/core/linux_sandbox.py +55 -0
- package/python/harness/core/media.py +96 -0
- package/python/harness/core/memory.py +147 -0
- package/python/harness/core/ports.py +63 -0
- package/python/harness/core/procs.py +227 -0
- package/python/harness/core/remote.py +102 -0
- package/python/harness/core/spans.py +166 -0
- package/python/harness/core/trace.py +139 -0
- package/python/harness/core/wait.py +12 -0
- package/python/harness/core/worker.py +388 -0
- package/python/harness/core/worker_pipe.py +119 -0
- package/python/harness/embodiments.py +217 -0
- package/python/harness/extensions.py +112 -0
- package/python/harness/host/__init__.py +397 -0
- package/python/harness/host/client.py +201 -0
- package/python/harness/host/http.py +720 -0
- package/python/harness/host/listing.py +68 -0
- package/python/harness/host/server.py +189 -0
- package/python/harness/host/sessions.py +406 -0
- package/python/harness/host/tasks.py +1308 -0
- package/python/harness/host/workers.py +257 -0
- package/python/harness/host/workspaces.py +256 -0
- package/python/harness/model_list.py +117 -0
- package/python/harness/packages/__init__.py +73 -0
- package/python/harness/packages/addons.py +121 -0
- package/python/harness/packages/archive.py +122 -0
- package/python/harness/packages/describe.py +46 -0
- package/python/harness/packages/environments.py +448 -0
- package/python/harness/packages/fetch.py +57 -0
- package/python/harness/packages/manager.py +587 -0
- package/python/harness/packages/manifest.py +221 -0
- package/python/harness/packages/migrate.py +68 -0
- package/python/harness/packages/provision.py +38 -0
- package/python/harness/packages/registry.py +275 -0
- package/python/harness/packages/resolve.py +89 -0
- package/python/harness/packages/sources.py +141 -0
- package/python/harness/packages/state.py +213 -0
- package/python/harness/packages/versions.py +112 -0
- package/python/harness/paths.py +218 -0
- package/python/harness/providers.py +303 -0
- package/python/harness/runtime/__init__.py +8 -0
- package/python/harness/runtime/decision/__init__.py +1 -0
- package/python/harness/runtime/decision/loop.py +198 -0
- package/python/harness/runtime/decision/messages.py +271 -0
- package/python/harness/runtime/decision/model.py +424 -0
- package/python/harness/runtime/decision/prompts.py +248 -0
- package/python/harness/runtime/decision/protocol.py +169 -0
- package/python/harness/runtime/decision/thoughts.py +199 -0
- package/python/harness/runtime/events.py +213 -0
- package/python/harness/runtime/execution/__init__.py +1 -0
- package/python/harness/runtime/execution/attached.py +96 -0
- package/python/harness/runtime/execution/control.py +211 -0
- package/python/harness/runtime/execution/entries.py +665 -0
- package/python/harness/runtime/execution/kernel.py +811 -0
- package/python/harness/runtime/execution/noenv.py +84 -0
- package/python/harness/runtime/execution/results.py +40 -0
- package/python/harness/runtime/execution/session.py +871 -0
- package/python/harness/runtime/execution/skills.py +436 -0
- package/python/harness/runtime/learning/__init__.py +1 -0
- package/python/harness/runtime/learning/active.py +314 -0
- package/python/harness/runtime/learning/experience.py +48 -0
- package/python/harness/runtime/learning/fixtures.py +167 -0
- package/python/harness/runtime/learning/learner.py +651 -0
- package/python/harness/runtime/learning/library.py +504 -0
- package/python/harness/runtime/learning/reflect.py +151 -0
- package/python/harness/runtime/learning/usage.py +30 -0
- package/python/harness/runtime/learning/views.py +89 -0
- package/python/harness/runtime/reactive/__init__.py +1 -0
- package/python/harness/runtime/reactive/rules.py +549 -0
- package/python/harness/runtime/safety/__init__.py +1 -0
- package/python/harness/runtime/safety/approval.py +131 -0
- package/python/harness/runtime/safety/input.py +62 -0
- package/python/harness/runtime/safety/isolation.py +419 -0
- package/python/harness/runtime/safety/shield.py +129 -0
- package/python/harness/runtime/status.py +23 -0
- package/python/harness/runtime/task/__init__.py +7 -0
- package/python/harness/runtime/task/context.py +431 -0
- package/python/harness/runtime/task/delegation.py +468 -0
- package/python/harness/runtime/task/environments.py +268 -0
- package/python/harness/runtime/task/run.py +2878 -0
- package/python/harness/runtime/tick.py +909 -0
- package/python/harness/sdk/__init__.py +281 -0
- package/python/harness/services/__init__.py +6 -0
- package/python/harness/services/broker.py +35 -0
- package/python/harness/services/connectors.py +88 -0
- package/python/harness/services/mcp.py +298 -0
- package/python/harness/services/mcp_stdio.py +131 -0
- package/python/harness/services/placement.py +82 -0
- package/python/harness/services/registry.py +156 -0
- package/python/harness/settings.py +536 -0
- package/python/harness/terminal.py +79 -0
- package/python/harness/transcript.py +297 -0
- package/python/harness/tui/__init__.py +68 -0
- package/python/harness/tui/chat.py +1181 -0
- package/python/harness/tui/pty.py +81 -0
- package/python/harness/tui/renderer.py +45 -0
- package/python/harness/tui/timeline_rows.py +58 -0
- package/python/harness/tui/views.py +434 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"""A trace as people read it, for every frontend: items() gives a record's items {kind, role,
|
|
2
|
+
text, keys}, which the host sends as its `view`; presentation() is the only reading of a reply."""
|
|
3
|
+
|
|
4
|
+
from collections import defaultdict
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
|
|
9
|
+
from .core import format as formats
|
|
10
|
+
from harness.runtime.safety.approval import WORDS, say
|
|
11
|
+
from .core.format import clip_lines
|
|
12
|
+
from .core.lanes import Lanes
|
|
13
|
+
from .runtime.status import ENDED
|
|
14
|
+
from harness.runtime.decision.protocol import DIRECT_KINDS, KINDS, ProtocolError, parse_reply
|
|
15
|
+
|
|
16
|
+
LOOK_TAGS = re.compile(r"</?(?:wait|assume|stop)>") # what a look's reply marks its parts with
|
|
17
|
+
_ASSUME = re.compile(r"<assume>.*?</assume>", re.DOTALL)
|
|
18
|
+
_MARKUP = re.compile(r"<\s*/?\s*(python|verify|finish|action|plan|step_done|search_web|navigate|read_page|tools)\b"
|
|
19
|
+
r"||{1,2}DSML|{1,2}", re.IGNORECASE)
|
|
20
|
+
# Records said in one item from their own fields: (kind, role, words[, only with `timeline`]).
|
|
21
|
+
SAID = {
|
|
22
|
+
"program_stopped": ("program_stopped", "note", "program {program} stops at its next step: {why}"),
|
|
23
|
+
"verify": ("verifier", "verifier", "{answer}"),
|
|
24
|
+
"confirmation_request": ("approval_request", "note", "the program waits for your approval"),
|
|
25
|
+
"stuck": ("stuck", "note", "the same {repeated} three times in a row"),
|
|
26
|
+
"agent_sent": ("agent_sent", "note", "to sub-agent {agent}: {text}"),
|
|
27
|
+
"agent_cancelled": ("agent_cancelled", "note", "stopped sub-agent {agent}"),
|
|
28
|
+
"environment_released": ("environment", "note", "gave {label} back"),
|
|
29
|
+
"error": ("error", "error", "{message}"), "kernel_died": ("error", "error", "{message}"),
|
|
30
|
+
"model_request": ("call", "call", "model call {call}: {reason}", True),
|
|
31
|
+
"finish": ("finished", "end", "finished ({source}): {text}", True),
|
|
32
|
+
"interrupted": ("interrupted", "error", "interrupted", True),
|
|
33
|
+
"cleanup_error": ("cleanup_error", "error", "cleanup error: {message}", True),
|
|
34
|
+
"context_shortened": ("context_shortened", "call", "history shortened to {history_turns} turns", True),
|
|
35
|
+
"malformed_dropped": ("malformed_dropped", "call", "{messages} malformed messages left the history", True),
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def presentation(record: dict) -> dict:
|
|
40
|
+
"""A model_reply as {kind, thought, body}; one without `presentation` (older) read from its text."""
|
|
41
|
+
shown = record.get("presentation")
|
|
42
|
+
if not isinstance(shown, dict):
|
|
43
|
+
text = str(record.get("text") or "")
|
|
44
|
+
try:
|
|
45
|
+
reply = parse_reply(text, tuple(dict.fromkeys(KINDS + DIRECT_KINDS)))
|
|
46
|
+
except ProtocolError:
|
|
47
|
+
return {"kind": "invalid", "body": ""} if _MARKUP.search(text) else {"kind": "message", "body": text.strip()}
|
|
48
|
+
shown = {"kind": reply.kind, "thought": reply.thought, "body": reply.body}
|
|
49
|
+
return {**shown, "thought": _ASSUME.sub("", shown.get("thought") or "").strip()} # <assume> before 1.47.6
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def answer_of(record: dict) -> str | None:
|
|
53
|
+
"""The agent's answer when `record` is its final reply (harness run -p), else None."""
|
|
54
|
+
if record.get("event") in ("finish", "agent_message") and record.get("text"):
|
|
55
|
+
return str(record["text"])
|
|
56
|
+
reply = presentation(record) if record.get("event") == "model_reply" and record.get("ahead", "used") == "used" else {}
|
|
57
|
+
return str(reply.get("body") or "") if reply.get("kind") == "finish" else None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def physical(request: dict) -> tuple[str, str]:
|
|
61
|
+
"""A physical-input request's doing.* key and app: its window title's end, else its process."""
|
|
62
|
+
actions = request.get("actions") or ["pointer"]
|
|
63
|
+
doing = "doing." + ("clipboard" if "clipboard" in actions else actions[0])
|
|
64
|
+
parts = [part.strip() for part in re.split(r" [-–—] ", str(request.get("window") or "")) if part.strip()]
|
|
65
|
+
app = (parts[-1] if len(parts) > 1 else request["app"][:1].upper() + request["app"][1:] if request.get("app")
|
|
66
|
+
else request.get("window") or say("label.this_window"))
|
|
67
|
+
return (doing if doing in WORDS else "doing.other"), app
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _item(kind: str, role: str, text="", **keys) -> dict:
|
|
71
|
+
return {"kind": kind, "role": role, "text": "" if text is None else str(text), "keys": keys}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def items(record: dict, names: dict, timeline: bool = False) -> list[dict]:
|
|
75
|
+
"""A record's items; `names` (filled in here) names the spans and entries started so far,
|
|
76
|
+
`timeline` adds the model calls and how the episode ended."""
|
|
77
|
+
kind, get = record.get("event") or "", record.get
|
|
78
|
+
said = SAID.get(kind)
|
|
79
|
+
if said and (timeline or len(said) == 3) and get("reason") != "think":
|
|
80
|
+
fields = defaultdict(str, {"source": "model", **{key: value for key, value in record.items() if value is not None}})
|
|
81
|
+
return [_item(said[0], said[1], said[2].format_map(fields))]
|
|
82
|
+
took, reason = formats.duration(get("seconds") or 0), f": {get('reason')}" if get("reason") else ""
|
|
83
|
+
if kind == "reset":
|
|
84
|
+
return [_item("task", "task", get("instruction") or (get("observation") or {}).get("instruction", ""))]
|
|
85
|
+
if kind == "user_message":
|
|
86
|
+
files, text = get("files") or [], get("text") or ""
|
|
87
|
+
named = [f"@{path}" for path in files if os.path.basename(path) not in text] # not named in its text
|
|
88
|
+
return [_item("from_parent" if get("sender") == "parent" else "user", "you", " ".join([text] + named).strip(),
|
|
89
|
+
files=files)]
|
|
90
|
+
if kind == "agent_message":
|
|
91
|
+
# A plain reply's notification refers to the model_reply that owns its display.
|
|
92
|
+
# Independent messages (including replies during a program) still own theirs.
|
|
93
|
+
if get("reply_call") is not None:
|
|
94
|
+
return []
|
|
95
|
+
return [_item("message", "agent", get("text"))]
|
|
96
|
+
if kind == "terminal_output":
|
|
97
|
+
# Exclude control bytes from transcript consumers, including the local terminal.
|
|
98
|
+
text = "".join(char for char in str(get("text") or "") if char in "\t\n" or char.isprintable())
|
|
99
|
+
return [_item("terminal_output", "output", text)]
|
|
100
|
+
if kind == "external_artifacts":
|
|
101
|
+
links = [str(row.get("url") or "") for row in get("artifacts") or []]
|
|
102
|
+
return [_item("external_artifacts", "note", "\n".join(url for url in links if url.startswith("https://")))]
|
|
103
|
+
if kind == "external_question":
|
|
104
|
+
return [_item("external_question", "note", get("text"), schema=get("schema"))]
|
|
105
|
+
if kind == "agent_delegated":
|
|
106
|
+
shared = ", its windows handed over" if get("windows") else ""
|
|
107
|
+
return [_item("agent_started", "note", f"started sub-agent {get('agent')} ({get('kind')}{shared}): {get('task')}")]
|
|
108
|
+
if kind == "agent_waited":
|
|
109
|
+
states = ", ".join(f"{agent} {status}" for agent, status in (get("states") or {}).items())
|
|
110
|
+
return [_item(kind, "note", f"waited {took} for sub-agents: {states}")]
|
|
111
|
+
if kind == "agent_report":
|
|
112
|
+
report = str(get("result") or get("error") or "")
|
|
113
|
+
return [_item(kind, "error" if get("status") == "error" else "outcome", f"sub-agent {get('agent')} {get('status')} ("
|
|
114
|
+
f"{get('model_calls')} calls, {formats.cost(get('cost_usd') or 0)}, {took})"
|
|
115
|
+
+ (f": {report}" if report.strip() else ""))]
|
|
116
|
+
if kind == "model_request" and get("reason") == "think": # a look again while a program runs, and why
|
|
117
|
+
why = "news" if get("why") == "news" else "a while without a call"
|
|
118
|
+
return [_item("look", "call", f"model looks again while program {get('program')} runs ({why})", news=get("news") or [])]
|
|
119
|
+
if kind == "model_reply" and get("ahead", "used") != "used": # written while a program ran: set aside
|
|
120
|
+
return ([_item("while_acting", "agent", LOOK_TAGS.sub("", get("text") or "").strip())] if get("think") else []) + (
|
|
121
|
+
[_item("call_set_aside", "call", f"model call {get('call')}: made while a program ran, {get('ahead')}")]
|
|
122
|
+
if timeline else [])
|
|
123
|
+
if kind == "model_reply":
|
|
124
|
+
reply = presentation(record)
|
|
125
|
+
shown, body, thought = reply.get("kind"), reply.get("body") or "", reply.get("thought") or ""
|
|
126
|
+
out = [_item("reasoning", "thinking", get("reasoning"))] if timeline and get("reasoning") else []
|
|
127
|
+
if shown in ("invalid", "message"):
|
|
128
|
+
out.append(_item(shown, "agent", (get("text") or "") if shown == "invalid" else body))
|
|
129
|
+
else:
|
|
130
|
+
out += [_item("thought", "agent", thought)] if thought else []
|
|
131
|
+
out.append(_item("program", "code", body, reply=shown, thought=thought) if shown in ("python", "action")
|
|
132
|
+
else _item("verify_ask" if shown == "verify" else "finish", "agent", body))
|
|
133
|
+
usage = get("usage") or {}
|
|
134
|
+
cost = f", {formats.cost(usage['cost_usd'])}" if isinstance(usage.get("cost_usd"), (int, float)) else ""
|
|
135
|
+
return out + ([_item("usage", "call", f"{took}, {usage.get('input_tokens')} in / {usage.get('output_tokens')} "
|
|
136
|
+
f"out tokens{cost}")] if timeline else [])
|
|
137
|
+
if kind == "skill_step":
|
|
138
|
+
number, after = f"{get('step')}" + (f"/{get('of')}" if get("of") else ""), f" after {took}" if get("seconds") is not None else ""
|
|
139
|
+
return [_item(kind, "skill" if get("ok") else "error",
|
|
140
|
+
f"{get('skill')} step {number}, {get('what')}: {'done' if get('ok') else 'failed'}{after}{reason}")]
|
|
141
|
+
if kind == "span_start" and get("lane") in ("skill", "service"):
|
|
142
|
+
names[get("id")] = get("name")
|
|
143
|
+
parameters = ", ".join(f"{key}={value if isinstance(value, str) else json.dumps(value)}"
|
|
144
|
+
for key, value in (get("parameters") or {}).items())
|
|
145
|
+
return [_item("skill", "skill", f"{get('name')}({parameters})")] if get("lane") == "skill" else []
|
|
146
|
+
names.update({get("id"): get("name") or get("id")} if kind == "entry_start" else {})
|
|
147
|
+
name, claims = names.get(get("id"), get("id")), ", ".join(map(str, get("claims") or []))
|
|
148
|
+
if kind == "entry_start":
|
|
149
|
+
return [_item(kind, "entry", f"{name} ({get('kind', 'program')}" + (f", claims {claims})" if get("claims") else ")"))]
|
|
150
|
+
if kind == "entry_claims":
|
|
151
|
+
return [_item(kind, "entry", f"{name} claims {claims or 'nothing'}")]
|
|
152
|
+
if kind == "entry_end":
|
|
153
|
+
why = get("error") or get("event_kind")
|
|
154
|
+
return [_item(kind, "error" if get("status") == "failed" else "entry", f"{name}: {get('status')}" + (f" ({why})" if why else ""))]
|
|
155
|
+
if kind == "span_end" and get("id") in names and get("event_kind") not in (None, "", "COMPLETED", "PREDICATE_SATISFIED"):
|
|
156
|
+
return [_item("skill_failed", "error", f"{name}: {get('event_kind')}")]
|
|
157
|
+
if kind == "cell":
|
|
158
|
+
result = get("result") or {}
|
|
159
|
+
escalation = result.get("escalation") or {}
|
|
160
|
+
return [_item("result", "result", f"{result.get('status')} after {formats.duration(result.get('seconds') or 0)}"),
|
|
161
|
+
*([_item("output", "output", result["stdout"])] if result.get("stdout") else []),
|
|
162
|
+
*([_item("cell_error", "error", result["error"])] if result.get("error") else []),
|
|
163
|
+
*([_item("unhandled", "error", f"unhandled {(escalation.get('event') or {}).get('kind')} in "
|
|
164
|
+
f"{escalation.get('skill')}")] if escalation else [])]
|
|
165
|
+
if kind == "protocol_error":
|
|
166
|
+
return [_item("announced", "note", "it said what it would do, without a program; asked to do it now") if get("announced")
|
|
167
|
+
else _item("format_error", "note", f"format error, nothing ran: {get('message')}")]
|
|
168
|
+
if kind == "confirmation":
|
|
169
|
+
return [_item("approved", "note", "approved") if get("accepted") else _item("rejected", "note", f"rejected{reason}")]
|
|
170
|
+
if kind == "environment_attached":
|
|
171
|
+
how = "you allowed it" if get("asked") else ("confirmations are skipped" if get("skipped_confirmation")
|
|
172
|
+
else "your settings allow it")
|
|
173
|
+
where = f" in {get('folder')}" if get("folder") else ""
|
|
174
|
+
return [_item("environment", "note", f"took {get('label') or get('kind')}{where} into this conversation "
|
|
175
|
+
f"({how}): {get('why')}", environment=get("kind"), why=get("why"))]
|
|
176
|
+
if kind == "environment_refused":
|
|
177
|
+
return [_item("environment", "note", f"did not take {get('kind')}: {get('reason')}", environment=get("kind"))]
|
|
178
|
+
if kind == "agents_enabled":
|
|
179
|
+
return [_item("environment", "note", "sub-agents are on for this conversation" if get("enabled")
|
|
180
|
+
else "sub-agents stay off")]
|
|
181
|
+
if kind == "physical_request" and get("sensitive"):
|
|
182
|
+
names.update({f"physical:{get('id')}": say("label.that_app")} if get("id") else {})
|
|
183
|
+
return [_item(kind, "note", say("sensitive", kind=say(f"sensitive.{get('sensitive')}"), target=get("target")),
|
|
184
|
+
sensitive=get("sensitive"), why=get("why"))]
|
|
185
|
+
if kind == "physical_request":
|
|
186
|
+
doing, app = physical(record)
|
|
187
|
+
names.update({f"physical:{get('id')}": app} if get("id") else {})
|
|
188
|
+
window = f" ({get('window')})" if get("window") and get("window") != app else ""
|
|
189
|
+
return [_item(kind, "note", say("request", doing=say(doing), app=app, window=window), app=app, doing=doing, why=get("why"))]
|
|
190
|
+
if kind == "worker_approval_result" and get("outcome") == "unknown":
|
|
191
|
+
return [_item("delivery_unknown", "note", "Action outcome unknown. It was not resent.")]
|
|
192
|
+
if kind == "physical_answer":
|
|
193
|
+
remember = get("remember") if f"allowed.{get('remember')}" in WORDS else "once"
|
|
194
|
+
app = names.get(f"physical:{get('id')}") or say("label.that_app")
|
|
195
|
+
return [_item("allowed", "note", say(f"allowed.{remember}", app=app), remember=remember, app=app) if get("accepted")
|
|
196
|
+
else _item("refused", "note", say("refused", reason=reason))]
|
|
197
|
+
if kind == "state":
|
|
198
|
+
words = {"IDLE": "your turn", "PAUSED": "paused"}.get(get("status")) or get("status") in ENDED and f"episode over: {get('reason')}"
|
|
199
|
+
return [_item("state", "turn", words)] if words else []
|
|
200
|
+
if kind == "outcome":
|
|
201
|
+
outcome = get("outcome") or {}
|
|
202
|
+
return [_item("outcome", "outcome", f"evaluator: success {outcome.get('success')}, reward {outcome.get('reward')}, "
|
|
203
|
+
f"process score {outcome.get('process_score')}, steps {outcome.get('steps')}")]
|
|
204
|
+
if timeline and kind in ("policy_to_end", "wrap_up"):
|
|
205
|
+
return [_item(kind, "end", f"{kind.replace('_', ' ')}: {get('status')}" + (f": {get('error')}" if get("error") else ""))]
|
|
206
|
+
if kind == "library" and get("action") in ("admitted", "rejected", "promoted"):
|
|
207
|
+
# What the agent learned (runtime/learning/learner.py): an entry kept, waiting for its test, refused, or re-graded.
|
|
208
|
+
if get("action") == "admitted":
|
|
209
|
+
text = (f"learned {get('section')}/{get('name')} ({get('evidence')}, {get('origin')})" if get("admitted")
|
|
210
|
+
else f"proposed {get('section')}/{get('name')}: waits for its test ({get('origin')})")
|
|
211
|
+
elif get("action") == "rejected":
|
|
212
|
+
text = f"not learned {get('kind')} {get('name')}: {str(get('reason'))[:200]}"
|
|
213
|
+
else:
|
|
214
|
+
text = f"{get('key')}: {get('evidence')}"
|
|
215
|
+
return [_item("learned", "note", text)]
|
|
216
|
+
if timeline and kind in ("library", "library_loaded") and (get("action") or "loaded") == "loaded":
|
|
217
|
+
return [_item("library_loaded", "end", f"library {str(get('version'))[:12]} loaded: {get('status')}")]
|
|
218
|
+
return []
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
# A transcript item's line around its text, by kind; with a number, its lines one by one,
|
|
222
|
+
# at most that many (with True, the blank ones left out). Other kinds: the text as it is.
|
|
223
|
+
FORMS = {
|
|
224
|
+
"task": ("task: {}",), "user": ("you: {}",), "from_parent": ("from the agent that started it: {}",),
|
|
225
|
+
"while_acting": ("agent, while it acts: {}", 6, True), "reasoning": ("thinking: {}", 12, True),
|
|
226
|
+
"thought": ("agent: {}", 40), "message": ("agent: {}", 40), "invalid": ("agent: {}", 40),
|
|
227
|
+
"program": (" | {}", 60), "verify_ask": ("agent asks the verifier: {}",),
|
|
228
|
+
"finish": ("agent finished: {}",), "usage": (" ({})",), "skill_step": (" · {}",),
|
|
229
|
+
"program_stopped": ("({})",), "skill": (" > {}",), "entry_start": ("+ {}",), "entry_claims": ("~ {}",),
|
|
230
|
+
"entry_end": ("- {}",), "skill_failed": (" ! {}",), "result": (" = {}",), "output": (" {}", 20),
|
|
231
|
+
"cell_error": (" {}", 8), "unhandled": (" {}",), "verifier": ("verifier: {}",), "announced": ("({})",),
|
|
232
|
+
"format_error": ("({})",), "approval_request": ("the program waits for you: /approve, or /reject [why]",),
|
|
233
|
+
"approved": ("({})",), "rejected": ("({})",), "allowed": ("({})",), "refused": ("({})",), "stuck": ("({})",),
|
|
234
|
+
"state": ("-- {} --",), "error": ("error: {}",), "learned": (" * {}",), "environment": ("({})",),
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def approval_options(item: dict) -> list[tuple[str | None, str]]:
|
|
239
|
+
"""The approval scopes and labels offered for a transcript request."""
|
|
240
|
+
from harness.runtime.safety.input import REMEMBER
|
|
241
|
+
|
|
242
|
+
keys = item.get("keys") or {}
|
|
243
|
+
scopes = REMEMBER if item["kind"] == "physical_request" and not keys.get("sensitive") else ("once",)
|
|
244
|
+
return [(scope, say(f"answer.{scope}", app=keys.get("app"))) for scope in scopes] + [
|
|
245
|
+
(None, say("answer.no"))]
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def item_lines(item: dict) -> list[tuple[str, str]]:
|
|
249
|
+
"""The plain-text lines of one transcript item, each with its role."""
|
|
250
|
+
kind, role, text, keys = item["kind"], item["role"], item["text"], item.get("keys") or {}
|
|
251
|
+
if kind == "look":
|
|
252
|
+
news = keys.get("news") or []
|
|
253
|
+
return ([(role, text)] + [(role, f" - {line}") for line in news[:6]]
|
|
254
|
+
+ ([(role, f" - and {len(news) - 6} more")] if len(news) > 6 else []))
|
|
255
|
+
if kind == "agent_report":
|
|
256
|
+
cut = text.find("): ") + 1 or len(text)
|
|
257
|
+
rows = clip_lines(text[cut + 2:], 12)
|
|
258
|
+
return [(role, text[:cut] + (": " + rows[0] if rows else ""))] + [("output", " " + row) for row in rows[1:]]
|
|
259
|
+
if kind == "physical_request":
|
|
260
|
+
why = f"\n why: {keys['why']}" if keys.get("why") else ""
|
|
261
|
+
answers = [(("/reject [why]" if scope is None else "/approve" if scope == "once" else f"/approve {scope}"),
|
|
262
|
+
words) for scope, words in approval_options(item)]
|
|
263
|
+
return [(role, text + why + "".join(f"\n {command:<20}{words}" for command, words in answers))]
|
|
264
|
+
if kind == "state" and text == "paused":
|
|
265
|
+
return [(role, "-- paused: /continue, or send a message --")]
|
|
266
|
+
form = FORMS.get(kind, ("{}",))
|
|
267
|
+
if len(form) == 1:
|
|
268
|
+
return [(role, form[0].format(text))]
|
|
269
|
+
return [(role, form[0].format(line)) for line in clip_lines(text, form[1])
|
|
270
|
+
if not (len(form) > 2 and form[2]) or line.strip()]
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def record_lines(record, names, timeline=False) -> list[tuple[str, str]]:
|
|
274
|
+
"""The lines of one record, each with its role. Names and timeline are passed to items()."""
|
|
275
|
+
return [line for found in items(record, names, timeline) for line in item_lines(found)]
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
class TextTranscript:
|
|
279
|
+
"""Lines for the records of one conversation, in order, each after the path of its
|
|
280
|
+
lane (blank for the conversation's own: the user's messages, its status); with
|
|
281
|
+
`timeline`, the lines a finished episode's record shows too (record_lines)."""
|
|
282
|
+
|
|
283
|
+
def __init__(self, timeline=False):
|
|
284
|
+
self.names = {}
|
|
285
|
+
self.lanes = Lanes()
|
|
286
|
+
self.timeline = timeline
|
|
287
|
+
self.width = len("model")
|
|
288
|
+
|
|
289
|
+
def parts(self, record) -> list[tuple[str, str, str]]:
|
|
290
|
+
"""The lines of `record` as (kind, lane path, text)."""
|
|
291
|
+
lines = record_lines(record, self.names, timeline=self.timeline)
|
|
292
|
+
path = self.lanes.add(record) or ""
|
|
293
|
+
self.width = max(self.width, len(path))
|
|
294
|
+
return [(kind, path, text) for kind, text in lines]
|
|
295
|
+
|
|
296
|
+
def lines(self, record) -> list[str]:
|
|
297
|
+
return [f"{path:<{self.width}} {text}" for _, path, text in self.parts(record)]
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""The terminal chat (`harness` in a terminal): the conversation printed into
|
|
2
|
+
the terminal's own scrollback, under it one status line and a multiline input box, as in
|
|
3
|
+
the coding agents' terminals; pickers and the timeline open full screen and close back to
|
|
4
|
+
the chat (tui/chat.py, tui/views.py). Records are read as every frontend reads them
|
|
5
|
+
(transcript.py), commands come from the one table (terminal.COMMANDS).
|
|
6
|
+
|
|
7
|
+
prompt_toolkit (input, keys, pickers) and rich (Markdown, colour) draw it; neither is a
|
|
8
|
+
dependency of the harness. When this interpreter cannot import them, the first use
|
|
9
|
+
installs them pinned into an environment of their own (HOME/envs/tui/<hash>/,
|
|
10
|
+
environments.py) and adds its packages to this process's path, after its own.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import importlib.util
|
|
14
|
+
import subprocess
|
|
15
|
+
import sys
|
|
16
|
+
|
|
17
|
+
from ..packages import environments
|
|
18
|
+
|
|
19
|
+
LIBRARIES = ["prompt_toolkit==3.0.52", "rich==15.0.0"]
|
|
20
|
+
# What LIBRARIES install, pinned.
|
|
21
|
+
PINS = ["wcwidth==0.2.14", "markdown-it-py==4.2.0", "mdurl==0.1.2", "pygments==2.21.0"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def environment() -> environments.Environment:
|
|
25
|
+
spec = {"python": f"{sys.version_info[0]}.{sys.version_info[1]}", "requirements": LIBRARIES,
|
|
26
|
+
"constraints": PINS, "imports": ["prompt_toolkit", "rich"]}
|
|
27
|
+
return environments.resolve("tui", spec)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def load(*, log=print) -> None:
|
|
31
|
+
"""Make prompt_toolkit and rich importable here: this interpreter's, else the TUI's
|
|
32
|
+
environment's packages (installed first if need be) after this interpreter's own."""
|
|
33
|
+
if all(importlib.util.find_spec(name) is not None for name in ("prompt_toolkit", "rich")):
|
|
34
|
+
return
|
|
35
|
+
env = environments.on_first_use(environment(), "prompt_toolkit and rich for the terminal chat", log=log)
|
|
36
|
+
packages = subprocess.run([str(env.python), "-I", "-X", "utf8", "-c",
|
|
37
|
+
"import sysconfig; print(sysconfig.get_path('purelib'))"],
|
|
38
|
+
capture_output=True, encoding="utf-8", check=True).stdout.strip()
|
|
39
|
+
sys.path.append(packages)
|
|
40
|
+
importlib.invalidate_caches()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def chat(conversation=None, records=None, *, host=None, start=None, log=print):
|
|
44
|
+
"""The terminal chat with `conversation` (a RemoteConversation of the session `host`, or
|
|
45
|
+
one in this process with its `records`), or with none until the first message, which
|
|
46
|
+
`start(text, files, log)` turns into (host, conversation). Returns the conversation shown
|
|
47
|
+
when the user leaves (None: none); the host's sessions go on."""
|
|
48
|
+
load(log=log)
|
|
49
|
+
from .chat import Chat
|
|
50
|
+
|
|
51
|
+
return Chat(conversation, records, host=host, start=start).run()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def timeline(directory, *, log=print) -> int:
|
|
55
|
+
"""The timeline of the episode in `directory` (of a runs directory: its newest episode),
|
|
56
|
+
full screen, followed while it is written."""
|
|
57
|
+
from pathlib import Path
|
|
58
|
+
|
|
59
|
+
from ..core.episodes import list_episodes
|
|
60
|
+
|
|
61
|
+
directory = Path(directory)
|
|
62
|
+
if not (directory / "events.jsonl").is_file():
|
|
63
|
+
found = list_episodes(directory, limit=1)
|
|
64
|
+
directory = directory / found[0]["run"] if found else directory
|
|
65
|
+
load(log=log)
|
|
66
|
+
from .views import follow_timeline
|
|
67
|
+
|
|
68
|
+
return follow_timeline(directory)
|