@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,257 @@
1
+ """Host-owned worker leases shared by all target environments."""
2
+ import itertools
3
+ from pathlib import Path
4
+ import threading
5
+ import time
6
+
7
+ LIMITS = {"workspaces": 4}
8
+ SPARE = 1
9
+
10
+ class Cancelled(Exception):
11
+ """The lease requester stopped waiting."""
12
+
13
+ class Worker:
14
+ """A workspace's worker process. `status`: starting, ready, error or stopped; `run`:
15
+ the conversation it serves (None: free)."""
16
+
17
+ def __init__(self, number: int, kind: str, options: dict, run, *, reusable=True):
18
+ self.id = f"w{number}"
19
+ self.kind = kind
20
+ self.options = dict(options)
21
+ self.run = run
22
+ self.status = "starting"
23
+ self.error = None
24
+ self.endpoint = None
25
+ self.approval_key = None
26
+ self.python = None
27
+ self.process = None
28
+ self._approval_lock = threading.RLock()
29
+ self._lease = object()
30
+ self.reusable = reusable
31
+ self.since = time.time()
32
+
33
+ def alive(self) -> bool:
34
+ return self.process is None or self.process.poll() is None
35
+
36
+ def describe(self) -> dict:
37
+ return {"id": self.id, "kind": self.kind, "status": self.status, "run": self.run, "error": self.error,
38
+ "since": self.since}
39
+
40
+ class WorkerPool:
41
+ """Capacity and ownership of package-provided worker leases. `limits()`
42
+ gives LIMITS' values (the app's settings, read at each decision); `launch(worker, log)`
43
+ starts a worker's process and sets its endpoint and python (tests give their own)."""
44
+
45
+ def __init__(self, logs: Path, limits=None, launch=None):
46
+ self.logs = Path(logs)
47
+ self.limits = limits or (lambda: dict(LIMITS))
48
+ self.launch = launch or self._launch
49
+ self.workers = []
50
+ # Why the last worker could not start; whether a bench environment is installing.
51
+ self.error = None
52
+ self.installing = False
53
+ self._numbers = itertools.count(1)
54
+ self._changed = threading.Condition()
55
+ self._releases = 0
56
+
57
+ worker_class = Worker
58
+
59
+ def check_kind(self, kind):
60
+ return kind
61
+
62
+ def kind_limit(self, kind, limits):
63
+ return None
64
+
65
+ def alive(self, worker):
66
+ return worker.alive()
67
+
68
+ def _start(self, worker, log):
69
+ try:
70
+ self.launch(worker, log)
71
+ worker.status = "ready"
72
+ except Exception as error:
73
+ worker.status, worker.error = "error", str(error)
74
+ self.error = worker.error
75
+ with self._changed:
76
+ self.workers.remove(worker)
77
+ self._stop(worker)
78
+ raise
79
+ finally:
80
+ with self._changed:
81
+ self._changed.notify_all()
82
+
83
+ # What the app shows.
84
+
85
+ def status(self) -> str:
86
+ """stopped, installing, starting, ready (one worker is) or error (the last start failed)."""
87
+ with self._changed:
88
+ states = {worker.status for worker in self.workers}
89
+ if "ready" in states:
90
+ return "ready"
91
+ if self.installing:
92
+ return "installing"
93
+ if "starting" in states:
94
+ return "starting"
95
+ return "error" if self.error else "stopped"
96
+
97
+ def describe(self) -> list[dict]:
98
+ with self._changed:
99
+ return [worker.describe() for worker in self.workers]
100
+
101
+ def of(self, run) -> Worker | None:
102
+ """The worker serving `run`."""
103
+ with self._changed:
104
+ return next((worker for worker in self.workers if run is not None and worker.run == run), None)
105
+
106
+ def held(self) -> list[Worker]:
107
+ with self._changed:
108
+ return [worker for worker in self.workers if worker.run is not None]
109
+
110
+ # Taking and giving back.
111
+
112
+ def acquire(self, run, kind: str, options=None, *, log=print, evict=None, cancelled=None,
113
+ poll=0.5, reusable=True) -> Worker:
114
+ """A ready worker of `kind` with `options` for `run`: a free one, a new one, or once
115
+ one frees (calling `evict(runs)`, the conversations whose end would make room, at
116
+ most once per release). Cancelled when `cancelled()` holds while it waits."""
117
+ self.check_kind(kind)
118
+ options = dict(options or {})
119
+ noted = evicted = None
120
+ while True:
121
+ stop = []
122
+ with self._changed:
123
+ if cancelled is not None and cancelled():
124
+ raise Cancelled()
125
+ self._prune(stop)
126
+ action, worker, waiting = self._decide(run, kind, options, reusable=reusable)
127
+ if action == "stop":
128
+ stop.append(worker)
129
+ releases = self._releases
130
+ for item in stop:
131
+ self._stop(item)
132
+ item.status = "stopped"
133
+ if action == "reuse":
134
+ return worker
135
+ if action == "start":
136
+ self._start(worker, log)
137
+ return worker
138
+ if action == "stop":
139
+ continue
140
+ message, runs = waiting
141
+ if message != noted:
142
+ log(message)
143
+ noted = message
144
+ if evict is not None and evicted != releases and runs:
145
+ if evict(runs):
146
+ evicted = releases
147
+ with self._changed:
148
+ if self._releases == releases:
149
+ self._changed.wait(poll)
150
+
151
+ def _decide(self, run, kind, options, *, reusable=True):
152
+ """What acquire() does now (under the lock): ("reuse" | "start", worker, None),
153
+ ("stop", a free worker in the way, None) or ("wait", None, (why, the runs whose end
154
+ makes room))."""
155
+ limits = self.limits()
156
+ live = [worker for worker in self.workers if worker.status in ("starting", "ready")]
157
+ free = [worker for worker in live if worker.run is None and worker.status == "ready"]
158
+ same = next((worker for worker in free if worker.kind == kind and worker.options == options), None)
159
+ if same is not None and reusable and same.reusable:
160
+ with same._approval_lock:
161
+ same.run, same.since = run, time.time()
162
+ return "reuse", same, None
163
+ cap = max(1, int(limits["workspaces"]))
164
+ kind_cap = self.kind_limit(kind, limits)
165
+ of_kind = [worker for worker in live if worker.kind == kind]
166
+ kind_room = kind_cap is None or len(of_kind) < kind_cap
167
+ if len(live) < cap and kind_room:
168
+ worker = self.worker_class(next(self._numbers), kind, options, run, reusable=reusable)
169
+ self.workers.append(worker)
170
+ return "start", worker, None
171
+ # A free worker of another kind or other settings makes room by stopping.
172
+ in_the_way = [worker for worker in free if kind_room or worker.kind == kind]
173
+ if in_the_way:
174
+ worker = min(in_the_way, key=lambda item: item.since)
175
+ self.workers.remove(worker)
176
+ return "stop", worker, None
177
+ if not kind_room:
178
+ why = f"waiting for a {kind} workspace: {len(of_kind)} of {kind_cap} in use"
179
+ runs = [worker.run for worker in of_kind if worker.run is not None]
180
+ else:
181
+ why = f"waiting for a workspace: {len(live)} of {cap} in use"
182
+ runs = [worker.run for worker in live if worker.run is not None]
183
+ return "wait", None, (why, runs)
184
+
185
+ def _prune(self, stop):
186
+ """Drop free workers whose resource died; mark taken ones as failed."""
187
+ for worker in list(self.workers):
188
+ dead = worker.status == "ready" and (not self.alive(worker))
189
+ if not dead:
190
+ continue
191
+ if worker.run is None:
192
+ self.workers.remove(worker)
193
+ stop.append(worker)
194
+ else:
195
+ with worker._approval_lock:
196
+ worker.status, worker.error = "error", "its worker exited"
197
+
198
+ def release(self, run) -> None:
199
+ """A stopped lease remains attributable until its process has been reaped."""
200
+ stop = []
201
+ with self._changed:
202
+ worker = self.of(run)
203
+ if worker is None:
204
+ return
205
+ with worker._approval_lock:
206
+ worker.since = time.time()
207
+ worker._lease = object()
208
+ if not worker.reusable or worker.status != "ready" or not worker.alive():
209
+ worker.status = "stopping"
210
+ stop.append(worker)
211
+ else:
212
+ worker.run = None
213
+ free = sorted((w for w in self.workers if w.run is None and w.status == "ready"),
214
+ key=lambda w: w.since)
215
+ for item in free[:max(0, len(free) - SPARE)]:
216
+ item.status = "stopping"
217
+ stop.append(item)
218
+ try:
219
+ for item in stop:
220
+ self._stop(item)
221
+ item.status = "stopped"
222
+ with self._changed:
223
+ if item in self.workers:
224
+ self.workers.remove(item)
225
+ finally:
226
+ with self._changed:
227
+ self._releases += 1
228
+ self._changed.notify_all()
229
+
230
+ def warm(self, kind: str, options=None, log=print) -> Worker | None:
231
+ """Start a free worker of `kind` with `options` when none is ready and there is room
232
+ (the app's Restart, after its settings changed)."""
233
+ self.check_kind(kind)
234
+ options = dict(options or {})
235
+ with self._changed:
236
+ limits = self.limits()
237
+ live = [worker for worker in self.workers if worker.status in ("starting", "ready")]
238
+ if any(worker.run is None and worker.kind == kind and worker.options == options for worker in live) or \
239
+ len(live) >= max(1, int(limits["workspaces"])):
240
+ return None
241
+ worker = self.worker_class(next(self._numbers), kind, options, None)
242
+ self.workers.append(worker)
243
+ self._start(worker, log)
244
+ return worker
245
+
246
+ def stop_free(self) -> int:
247
+ """Stop the workers no conversation uses; how many are in use (they run on)."""
248
+ with self._changed:
249
+ free = [worker for worker in self.workers if worker.run is None and worker.status != "starting"]
250
+ for worker in free:
251
+ self.workers.remove(worker)
252
+ used = sum(1 for worker in self.workers if worker.run is not None)
253
+ self._changed.notify_all()
254
+ for worker in free:
255
+ self._stop(worker)
256
+ return used
257
+
@@ -0,0 +1,256 @@
1
+ """Discover package workspace providers and route ownership; no environment implementation."""
2
+ from pathlib import Path
3
+ from .workers import Cancelled
4
+
5
+
6
+ class WorkspaceProvider:
7
+ """Package lifecycle contract. Binding data is opaque to the host.
8
+
9
+ Implement kinds, kind, bench, tree, bind and acquire. Optional presentation and
10
+ managed execution are capabilities of the provider, not inferred from kind names.
11
+ """
12
+ def accepts(self, binding):
13
+ return not binding
14
+
15
+ def bind(self, kind, directory, request, parent=None):
16
+ if request:
17
+ raise ValueError(f"unsupported binding for {kind}: {sorted(request)}")
18
+ return {}, {}
19
+
20
+ def options(self, kind=None):
21
+ return {}
22
+
23
+ def describe(self, kind):
24
+ """How a kind is offered when a conversation chooses where to work: {"label",
25
+ "about"}, and optionally "folder" (it is bound to a folder the user names),
26
+ "views" (it has pictures the model may look at: a model that reads no images
27
+ works there from text and structure only) and "physical" (it may use the user's
28
+ real mouse and keyboard)."""
29
+ return {"label": kind, "about": ""}
30
+
31
+ def environment(self, kind):
32
+ """An environment in this process for a kind that takes no worker, else None."""
33
+ return None
34
+
35
+ def require_choices(self, kind, runtime=None):
36
+ """Check user decisions before starting a task; providers own their choices."""
37
+
38
+ def builtin_skills(self, kind):
39
+ return ()
40
+
41
+ def extra_skills(self, kind):
42
+ return ()
43
+
44
+ def validate_managed(self, kind):
45
+ raise ValueError(f"{kind} does not support managed execution")
46
+
47
+ def python(self, kind, log=print):
48
+ return self.pool.python(kind, log)
49
+
50
+ def of(self, run):
51
+ return self.pool.of(run)
52
+
53
+ def release(self, run):
54
+ self.pool.release(run)
55
+
56
+ def record(self, directory, kind, trace):
57
+ return None
58
+
59
+ def windows(self):
60
+ return None
61
+
62
+ def screen(self, run=None):
63
+ return None
64
+
65
+ def status(self):
66
+ return {"status": self.pool.status(), "workspaces": self.pool.describe(), "kinds": list(self.kinds())}
67
+
68
+ def presentation(self):
69
+ return {}
70
+
71
+ def restart(self):
72
+ return self.pool.stop_free()
73
+
74
+ def close(self):
75
+ self.pool.close()
76
+
77
+
78
+ # The order environments are offered in (kinds of other packages follow).
79
+ ORDER = ("none", "project", "browser", "apps", "desktop", "display")
80
+
81
+
82
+ class NoWorkspace(WorkspaceProvider):
83
+ """The core's own kind, `none`: a conversation with the model and the connected tools
84
+ only. Nothing is acquired: no worker, no screen, no folder (runtime/execution/noenv.py)."""
85
+
86
+ KIND = "none"
87
+
88
+ def kinds(self):
89
+ return (self.KIND,)
90
+
91
+ def accepts(self, binding):
92
+ return False
93
+
94
+ def kind(self, requested=None, text=None):
95
+ return self.KIND
96
+
97
+ def bench(self, kind):
98
+ return self.KIND
99
+
100
+ def describe(self, kind):
101
+ return {"label": "No environment",
102
+ "about": "conversation, the model and the connected tools only; nothing is started"}
103
+
104
+ def tree(self, tree, kind):
105
+ from ..paths import ROOT
106
+ from ..runtime.execution.kernel import DEFAULT_BOOTSTRAP
107
+
108
+ tree["bench"] = self.KIND
109
+ agent = tree["overrides"]["agent"]
110
+ agent["prompt"][self.KIND] = str(ROOT / "prompts" / "conversation")
111
+ agent[self.KIND] = {"roles": ["main"], "runtime": {
112
+ "contract": "native", "verifier": False, "max_history_turns": 10,
113
+ "think_while_acting": "off", "bootstrap": DEFAULT_BOOTSTRAP}}
114
+ return tree
115
+
116
+ def builtin_skills(self, kind):
117
+ return ("conversation",)
118
+
119
+ def python(self, kind, log=print):
120
+ import sys
121
+
122
+ return sys.executable
123
+
124
+ def environment(self, kind):
125
+ from ..runtime.execution.noenv import NoEnvironment
126
+
127
+ return NoEnvironment()
128
+
129
+ def acquire(self, run, kind, **waiting):
130
+ raise ValueError("a conversation without an environment takes no workspace")
131
+
132
+ def of(self, run):
133
+ return None
134
+
135
+ def release(self, run):
136
+ pass
137
+
138
+ def status(self):
139
+ return {"status": "ready", "workspaces": [], "kinds": list(self.kinds())}
140
+
141
+ def restart(self):
142
+ return 0
143
+
144
+ def close(self):
145
+ pass
146
+
147
+
148
+ class Workspaces:
149
+ Cancelled = Cancelled
150
+
151
+ def __init__(self, directory, settings):
152
+ self.directory, self.settings = Path(directory), settings
153
+ self._providers = {}
154
+ self._none = NoWorkspace()
155
+
156
+ def providers(self):
157
+ from ..extensions import hosts
158
+ for package, module in hosts():
159
+ factory = getattr(module, "Workspaces", None)
160
+ if factory is not None and package.name not in self._providers:
161
+ self._providers[package.name] = factory(self.directory, self.settings)
162
+ return tuple(self._providers.values())
163
+
164
+ def catalog(self):
165
+ found = {}
166
+ for provider in (*self.providers(), self._none):
167
+ for kind in provider.kinds():
168
+ if kind in found:
169
+ raise ValueError(f"multiple packages provide workspace {kind}")
170
+ found[kind] = provider
171
+ return found
172
+
173
+ def kinds(self):
174
+ return tuple(self.catalog())
175
+
176
+ def kind(self, requested=None, text=None, binding=None):
177
+ catalog = self.catalog()
178
+ if requested not in (None, "auto"):
179
+ if requested not in catalog:
180
+ raise ValueError(f"no installed package provides workspace {requested!r}")
181
+ return catalog[requested].kind(requested, text)
182
+ matches = [p for p in self.providers() if p.accepts(binding or {})]
183
+ if not matches and not binding and len(self.providers()) == 1:
184
+ matches = list(self.providers())
185
+ if len(matches) != 1:
186
+ raise ValueError("select --workspace explicitly; install a package providing the requested environment")
187
+ return matches[0].kind(None, text)
188
+
189
+ def environments(self) -> list[dict]:
190
+ """What a new conversation may choose to work in, in the order it is offered: no
191
+ environment, then each kind the installed packages give, as its provider describes
192
+ it ({"value", "label", "about"} and its "folder", "views" and "physical" flags)."""
193
+ catalog = self.catalog()
194
+ order = [kind for kind in ORDER if kind in catalog] + [kind for kind in catalog if kind not in ORDER]
195
+ return [{"value": kind, **catalog[kind].describe(kind)} for kind in order]
196
+
197
+ def provider(self, kind=None):
198
+ kind = self.kind() if kind is None else kind
199
+ catalog = self.catalog()
200
+ if kind not in catalog:
201
+ raise ValueError(f"no installed package provides workspace {kind!r}")
202
+ return catalog[kind]
203
+
204
+ def require_choices(self, kind, runtime=None):
205
+ self.provider(kind).require_choices(kind, runtime)
206
+
207
+ def bench(self, kind):
208
+ return self.provider(kind).bench(kind)
209
+
210
+ def tree(self, tree, kind):
211
+ return self.provider(kind).tree(tree, kind)
212
+
213
+ def options(self, kind=None):
214
+ return self.provider(kind).options(kind)
215
+
216
+ def bind(self, kind, directory, request, parent=None):
217
+ return self.provider(kind).bind(kind, directory, request, parent)
218
+
219
+ def acquire(self, run, kind, **waiting):
220
+ return self.provider(kind).acquire(run, kind, **waiting)
221
+
222
+ def environment(self, kind):
223
+ return self.provider(kind).environment(kind)
224
+
225
+ def of(self, run):
226
+ return next((worker for p in self.providers() if (worker := p.of(run)) is not None), None)
227
+
228
+ def release(self, run):
229
+ for provider in self.providers():
230
+ if provider.of(run) is not None:
231
+ provider.release(run)
232
+ return
233
+
234
+ def record(self, directory, kind, trace):
235
+ return self.provider(kind).record(directory, kind, trace)
236
+
237
+ def windows(self):
238
+ return next((value for p in self.providers() if (value := p.windows()) is not None), None)
239
+
240
+ def screen(self, run=None):
241
+ if run is not None:
242
+ return next((p.screen(run) for p in self.providers() if p.of(run) is not None), None)
243
+ return self.provider().screen(None)
244
+
245
+ def status(self):
246
+ return {"providers": [p.status() for p in self.providers()], "kinds": list(self.kinds())}
247
+
248
+ def presentation(self):
249
+ return {key: value for p in self.providers() for key, value in p.presentation().items()}
250
+
251
+ def restart(self):
252
+ return sum(p.restart() for p in self.providers())
253
+
254
+ def close(self):
255
+ for provider in self._providers.values():
256
+ provider.close()
@@ -0,0 +1,117 @@
1
+ """The editable model list shared by the assistant's frontends.
2
+
3
+ An entry may say whether its model reads images (`images`: true or false). The provider's
4
+ catalog gives it where it says (providers.catalog); the user may change it in the list.
5
+ Without it the model is taken to read them. A model that reads none is sent none
6
+ (runtime/task/run.py): it works from the state and what the environment's calls return as
7
+ text, and a view it looks at is described by the vision model when one is set
8
+ (the setting model.vision)."""
9
+
10
+ import json
11
+
12
+ from . import providers, settings
13
+ from .core import atomic_write, sha256
14
+ from .core.procs import file_lock
15
+ from harness.runtime.decision.model import valid_effort
16
+
17
+
18
+ def entry(raw):
19
+ provider = raw.get("provider")
20
+ model = str(raw.get("model") or "").strip()
21
+ if isinstance(provider, str) and provider in providers.PROVIDERS:
22
+ route = providers.check(raw)
23
+ elif isinstance(provider, str) and provider.startswith(("acp:", "terminal:", "cloud:")):
24
+ if provider.startswith("cloud:") and (model or raw.get("efforts")):
25
+ raise ValueError("choose the cloud adapter's configured model")
26
+ if provider.startswith("acp:") and not model:
27
+ raise ValueError("choose the adapter's model")
28
+ route = {"provider": provider, "model": model, "base_url": None}
29
+ else:
30
+ raise ValueError("choose a model provider")
31
+ efforts = raw.get("efforts", [])
32
+ enabled = raw.get("enabled", True)
33
+ if not isinstance(efforts, list) or any(not level or not valid_effort(level) for level in efforts):
34
+ raise ValueError("efforts must be a list of provider option IDs")
35
+ if type(enabled) is not bool:
36
+ raise ValueError("enabled must be true or false")
37
+ images = raw.get("images")
38
+ if images is not None and type(images) is not bool:
39
+ raise ValueError("images must be true or false (whether the model reads images)")
40
+ return {**route, "id": sha256(route), "name": str(raw.get("name") or model or provider),
41
+ "enabled": enabled, "efforts": list(dict.fromkeys(efforts)),
42
+ **({"images": images} if images is not None else {})}
43
+
44
+
45
+ def reads_images(directory, choice: dict) -> bool:
46
+ """Whether the model of `choice` ({provider, model, base_url}) reads images, as its
47
+ entry of the list says; True without an entry or without the flag."""
48
+ for row in models(directory):
49
+ if all(row.get(key) == choice.get(key) for key in ("provider", "model", "base_url")):
50
+ return row.get("images") is not False
51
+ return True
52
+
53
+
54
+ def _read(path):
55
+ if path.exists():
56
+ try:
57
+ raw = json.loads(path.read_text(encoding="utf-8-sig"))
58
+ if not isinstance(raw, list) or any(not isinstance(row, dict) for row in raw):
59
+ raise ValueError("expected an array of model entries")
60
+ rows = [entry(row) for row in raw]
61
+ if len({row["id"] for row in rows}) != len(rows):
62
+ raise ValueError("duplicate model entries")
63
+ return rows
64
+ except (ValueError, TypeError) as error:
65
+ raise ValueError(f"{path}: {error}") from error
66
+ config = settings.read(path.parent)
67
+ chosen = providers.load(path.parent)
68
+ configured = (config.get("model") or {}).get("provider") or providers.ready(path.parent)["ready"]
69
+ legacy = str((config.get("taptab") or {}).get("models") or "").split(",")
70
+ return [entry({**chosen, "model": model}) for model in dict.fromkeys(
71
+ [chosen["model"]] * bool(configured) + [value.strip() for value in legacy if value.strip()]) if model]
72
+
73
+
74
+ def models(directory, change=None):
75
+ path = directory / "models.json"
76
+ if change is None and path.exists():
77
+ return _read(path)
78
+ path.parent.mkdir(parents=True, exist_ok=True)
79
+ with file_lock(path.with_suffix(".lock")):
80
+ rows = _read(path)
81
+ if change is not None:
82
+ action = change.get("action")
83
+ if action == "save":
84
+ row = entry(change)
85
+ previous = change.get("id")
86
+ if previous and previous != row["id"] and any(item["id"] == row["id"] for item in rows):
87
+ raise ValueError("this model is already saved")
88
+ existing = next((index for index, item in enumerate(rows)
89
+ if item["id"] == (previous or row["id"])), len(rows))
90
+ rows[existing:existing + 1] = [row]
91
+ else:
92
+ index = next((i for i, row in enumerate(rows) if row["id"] == change.get("id")), None)
93
+ if index is None:
94
+ raise ValueError("model entry no longer exists")
95
+ if action == "enable" and type(change.get("enabled")) is bool:
96
+ rows[index]["enabled"] = change["enabled"]
97
+ elif action == "remove":
98
+ del rows[index]
99
+ else:
100
+ raise ValueError("unknown model list operation")
101
+ atomic_write(path, json.dumps([{key: value for key, value in row.items() if key != "id"} for row in rows],
102
+ indent=2, ensure_ascii=False) + "\n")
103
+ return rows
104
+
105
+
106
+ def select(directory, identifier=None, *, native=False):
107
+ """Resolve a model for a new run. An ambiguous name must be qualified with the provider or entry ID."""
108
+ rows = [row for row in models(directory) if row["enabled"]
109
+ and (not native or row["provider"] in providers.PROVIDERS)]
110
+ if identifier is None:
111
+ chosen = providers.load(directory)
112
+ return next((row for row in rows if all(row[key] == chosen[key] for key in chosen)), rows[0] if rows else None)
113
+ matches = [row for row in rows if identifier in (row["id"], row["model"], row["provider"] + "/" + row["model"])]
114
+ if len(matches) != 1:
115
+ raise ValueError(f"choose one enabled model from {directory / 'models.json'}; "
116
+ "use provider/model if the name is ambiguous, or the entry ID for multiple endpoints")
117
+ return matches[0]