@lmmslab/harness 0.0.0-stage → 0.81.1
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 +1356 -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 +232 -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,587 @@
|
|
|
1
|
+
"""The package manager: install, remove, update, list, search and describe packages,
|
|
2
|
+
and install on first use what a run needs.
|
|
3
|
+
|
|
4
|
+
Packages come from the registry (registry.py), a git repository, an archive URL or a
|
|
5
|
+
folder (sources.py). A registry package is trusted; one from anywhere else runs code
|
|
6
|
+
nobody reviewed, so it is installed only after the user saw what it provides, which
|
|
7
|
+
services it would start and what it would install (describe()) and agreed (`consent`).
|
|
8
|
+
A folder of skills without package.toml (what the app's "Add a skill" took) becomes
|
|
9
|
+
one package per skill.
|
|
10
|
+
|
|
11
|
+
Each installed version lives in the store (state.py), unchanged. The user's packages
|
|
12
|
+
are listed in [packages] of <data>/config.toml; `project=True` writes a project's harness.toml and
|
|
13
|
+
harness.lock instead. A run uses the enabled packages: the user's, and a trusted
|
|
14
|
+
project's locked ones in their place.
|
|
15
|
+
|
|
16
|
+
`packages.auto_install` (the app's settings) decides what happens when a run needs an
|
|
17
|
+
embodiment, a bench, a skill or a service no enabled package provides (ensure()):
|
|
18
|
+
official installs the registry's package that provides it, ask asks first, off stops
|
|
19
|
+
with the command that would install it.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import os
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
import shutil
|
|
25
|
+
import sys
|
|
26
|
+
import tempfile
|
|
27
|
+
|
|
28
|
+
from ..core import tree_hash
|
|
29
|
+
from ..paths import data as data_dir
|
|
30
|
+
from . import archive, sources, state
|
|
31
|
+
from .addons import find_skills
|
|
32
|
+
from .manifest import Package, load, write
|
|
33
|
+
from .registry import Registry, RegistryError, configured, harness_fits
|
|
34
|
+
from .resolve import ResolveError, resolve
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
AUTO_INSTALL = ("official", "ask", "off")
|
|
38
|
+
SINGULAR = {"kinds": "kind", "embodiments": "embodiment", "skills": "skill", "benches": "bench",
|
|
39
|
+
"services": "service", "prompts": "prompt", "profiles": "profile", "experiments": "experiment",
|
|
40
|
+
"agent_skills": "Agent Skill", "settings": "settings section", "config_keys": "configuration key",
|
|
41
|
+
"commands": "command", "agents": "agent"}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class MissingPackage(RuntimeError):
|
|
45
|
+
"""A run needs something no enabled package provides, and it was not installed."""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def harness_version() -> str:
|
|
49
|
+
from .. import __version__
|
|
50
|
+
|
|
51
|
+
return __version__
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class Packages:
|
|
55
|
+
"""The packages of one user (`data`, default ~/.harness) and of the project of
|
|
56
|
+
`cwd` (default the working directory)."""
|
|
57
|
+
|
|
58
|
+
def __init__(self, data=None, *, cwd=None, registry: Registry | None = None):
|
|
59
|
+
self.data = Path(data or data_dir())
|
|
60
|
+
self.cwd = cwd
|
|
61
|
+
self._registry = registry
|
|
62
|
+
self.problems: dict[str, str] = {}
|
|
63
|
+
|
|
64
|
+
# -- configuration -----------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
def settings(self) -> dict:
|
|
67
|
+
from ..settings import Settings
|
|
68
|
+
|
|
69
|
+
return Settings(self.data).get().get("packages", {})
|
|
70
|
+
|
|
71
|
+
def auto_install(self) -> str:
|
|
72
|
+
mode = os.environ.get("HARNESS_AUTO_INSTALL") or self.settings().get("auto_install", "official")
|
|
73
|
+
return mode if mode in AUTO_INSTALL else "official"
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def registry(self) -> Registry:
|
|
77
|
+
if self._registry is None:
|
|
78
|
+
self._registry = Registry(configured(self.settings().get("registry", "")))
|
|
79
|
+
return self._registry
|
|
80
|
+
|
|
81
|
+
def user(self) -> state.UserList:
|
|
82
|
+
return state.UserList.read(self.data)
|
|
83
|
+
|
|
84
|
+
def project(self) -> state.Project | None:
|
|
85
|
+
return state.Project.find(self.cwd)
|
|
86
|
+
|
|
87
|
+
def project_trusted(self, project: state.Project | None = None) -> bool:
|
|
88
|
+
project = project or self.project()
|
|
89
|
+
return project is not None and str(project.root) in self.user().trusted_projects
|
|
90
|
+
|
|
91
|
+
# -- what is installed -------------------------------------------------------------
|
|
92
|
+
|
|
93
|
+
def records(self) -> dict[str, tuple[state.Record, str]]:
|
|
94
|
+
"""{name: (record, "user" or "project")}: a trusted project's locked packages in
|
|
95
|
+
place of the user's of the same name."""
|
|
96
|
+
out = {name: (record, "user") for name, record in self.user().packages.items()}
|
|
97
|
+
project = self.project()
|
|
98
|
+
if project is not None and self.project_trusted(project):
|
|
99
|
+
for name, record in project.locked().items():
|
|
100
|
+
record.path = str(state.store_path(name, record.version, record.sha256))
|
|
101
|
+
out[name] = (record, "project")
|
|
102
|
+
return out
|
|
103
|
+
|
|
104
|
+
def enabled(self) -> list[Package]:
|
|
105
|
+
"""The packages a run uses (installed, enabled, loading cleanly); why the others
|
|
106
|
+
do not load is in `problems`."""
|
|
107
|
+
self.problems = {}
|
|
108
|
+
out = []
|
|
109
|
+
for name, (record, _) in sorted(self.records().items()):
|
|
110
|
+
if not record.enabled:
|
|
111
|
+
continue
|
|
112
|
+
try:
|
|
113
|
+
package = load(record.path)
|
|
114
|
+
if package.name != name:
|
|
115
|
+
raise ValueError(f"{record.path} holds {package.name}, not {name}")
|
|
116
|
+
out.append(package)
|
|
117
|
+
except ValueError as error:
|
|
118
|
+
self.problems[name] = str(error)
|
|
119
|
+
return out
|
|
120
|
+
|
|
121
|
+
def installed(self) -> list[dict]:
|
|
122
|
+
"""Every installed package: name, version, source, where, whether it is enabled
|
|
123
|
+
and trusted, what it provides, and its problem when it does not load."""
|
|
124
|
+
rows = []
|
|
125
|
+
loaded = {package.name: package for package in self.enabled()}
|
|
126
|
+
for name, (record, scope) in sorted(self.records().items()):
|
|
127
|
+
package = loaded.get(name)
|
|
128
|
+
if package is None and name not in self.problems:
|
|
129
|
+
try:
|
|
130
|
+
package = load(record.path)
|
|
131
|
+
except ValueError as error:
|
|
132
|
+
self.problems[name] = str(error)
|
|
133
|
+
rows.append({"name": name, "version": record.version, "scope": scope, "source": record.source,
|
|
134
|
+
"path": record.path, "link": record.link, "enabled": record.enabled,
|
|
135
|
+
"trusted": record.trusted, "summary": package.summary if package else "",
|
|
136
|
+
"provides": {k: list(v) for k, v in (package.provides if package else {}).items()},
|
|
137
|
+
**({"problem": self.problems[name]} if name in self.problems else {})})
|
|
138
|
+
return rows
|
|
139
|
+
|
|
140
|
+
def available(self) -> list[Package]:
|
|
141
|
+
"""The packages a run here may use: a source checkout's and the enabled ones,
|
|
142
|
+
as paths.packages() finds them (a checkout's wins over an installed one)."""
|
|
143
|
+
from ..paths import packages
|
|
144
|
+
|
|
145
|
+
return packages()
|
|
146
|
+
|
|
147
|
+
def provider(self, what: str, name: str) -> Package | None:
|
|
148
|
+
"""The package a run here may use (available) that provides `name` among its `what`."""
|
|
149
|
+
for package in self.available():
|
|
150
|
+
if name in package.provides.get(what, ()):
|
|
151
|
+
return package
|
|
152
|
+
return None
|
|
153
|
+
|
|
154
|
+
# -- describe and find -------------------------------------------------------------
|
|
155
|
+
|
|
156
|
+
def describe(self, package: Package) -> dict:
|
|
157
|
+
"""What installing a package brings: what it provides, the services it can start,
|
|
158
|
+
the environments it installs, the commands it adds, what it depends on."""
|
|
159
|
+
environments, services = [], []
|
|
160
|
+
for path in sorted(package.path.glob("services/*.toml")):
|
|
161
|
+
services.append(path.stem)
|
|
162
|
+
spec = state._toml(path)
|
|
163
|
+
if isinstance(spec.get("environment"), dict):
|
|
164
|
+
environments.append({"for": f"service {path.stem}",
|
|
165
|
+
"requirements": spec["environment"].get("requirements", [])})
|
|
166
|
+
for path in sorted(package.path.glob("benches/*/bench.toml")):
|
|
167
|
+
spec = state._toml(path)
|
|
168
|
+
if isinstance(spec.get("environment"), dict):
|
|
169
|
+
environments.append({"for": f"bench {path.parent.name}",
|
|
170
|
+
"requirements": spec["environment"].get("requirements", [])})
|
|
171
|
+
requirements = {}
|
|
172
|
+
for path in sorted(package.path.glob("skills/*/skill.toml")):
|
|
173
|
+
wanted = state._toml(path).get("requirements", [])
|
|
174
|
+
if wanted:
|
|
175
|
+
requirements[path.parent.name] = list(wanted)
|
|
176
|
+
return {"name": package.name, "version": package.version, "summary": package.summary,
|
|
177
|
+
"harness": package.harness, "depends": dict(package.depends), "platforms": list(package.platforms),
|
|
178
|
+
"provides": {k: list(v) for k, v in package.provides.items()}, "services": services,
|
|
179
|
+
"environments": environments, "skill_requirements": requirements,
|
|
180
|
+
"commands": dict(package.commands), "code": any(package.path.rglob("*.py"))}
|
|
181
|
+
|
|
182
|
+
def search(self, query: str = "") -> list[dict]:
|
|
183
|
+
installed = {name: record.version for name, (record, _) in self.records().items()}
|
|
184
|
+
return [{**row, "installed": installed.get(row["name"], "")} for row in self.registry.search(query)]
|
|
185
|
+
|
|
186
|
+
def info(self, name: str) -> dict:
|
|
187
|
+
"""A package: the installed version's description and the registry's versions."""
|
|
188
|
+
out = {"name": name}
|
|
189
|
+
record = self.records().get(name)
|
|
190
|
+
if record is not None:
|
|
191
|
+
out["installed"] = {"version": record[0].version, "scope": record[1], "source": record[0].source,
|
|
192
|
+
"path": record[0].path}
|
|
193
|
+
try:
|
|
194
|
+
out.update(self.describe(load(record[0].path)))
|
|
195
|
+
except ValueError as error:
|
|
196
|
+
out["problem"] = str(error)
|
|
197
|
+
try:
|
|
198
|
+
entry = self.registry.packages().get(name)
|
|
199
|
+
except RegistryError as error:
|
|
200
|
+
entry, out["registry_problem"] = None, str(error)
|
|
201
|
+
if entry is not None:
|
|
202
|
+
out["registry"] = self.registry.base
|
|
203
|
+
out.setdefault("summary", entry.get("summary", ""))
|
|
204
|
+
out["versions"] = [{"version": version, "harness": entry["versions"][version].get("harness", ""),
|
|
205
|
+
"published": entry["versions"][version].get("published", ""),
|
|
206
|
+
"fits": harness_fits(entry["versions"][version], harness_version())}
|
|
207
|
+
for version in self.registry.versions(name)]
|
|
208
|
+
if "provides" not in out and out["versions"]:
|
|
209
|
+
top = entry["versions"][out["versions"][-1]["version"]]
|
|
210
|
+
out.update({key: top.get(key) for key in ("provides", "depends", "platforms", "harness")})
|
|
211
|
+
if record is None and entry is None:
|
|
212
|
+
raise ValueError(f"no package {name}: not installed, and not in {self.registry.base}")
|
|
213
|
+
return out
|
|
214
|
+
|
|
215
|
+
# -- install, remove, update -------------------------------------------------------
|
|
216
|
+
|
|
217
|
+
def install(self, specs=(), *, project: bool = False, consent=None, link: bool = False, python=None,
|
|
218
|
+
zip_bytes: bytes | None = None, embodiment: str | None = None, log=print) -> dict:
|
|
219
|
+
"""Install packages by source text (sources.py), or `zip_bytes` (an archive the app
|
|
220
|
+
was given). Packages from outside the registry need `consent(found) -> bool`
|
|
221
|
+
(found: describe() of each); without it nothing changes and {"needs_trust": True,
|
|
222
|
+
"found": [...]} comes back. With `project`, the project's harness.toml and
|
|
223
|
+
harness.lock are written (and with no specs, the project's packages are
|
|
224
|
+
installed and the project trusted). With `embodiment`, a folder's skills are
|
|
225
|
+
taken only when they run on it (the app's agent: desktop). Returns
|
|
226
|
+
{"installed": [...], "problems": [...]}."""
|
|
227
|
+
specs = [str(spec) for spec in specs]
|
|
228
|
+
target = self.project() if project else None
|
|
229
|
+
if project and target is None:
|
|
230
|
+
target = state.Project(Path(self.cwd or os.getcwd()).resolve())
|
|
231
|
+
with tempfile.TemporaryDirectory(prefix="harness-package-") as temporary:
|
|
232
|
+
temporary = Path(temporary)
|
|
233
|
+
requests, local, problems = {}, [], []
|
|
234
|
+
if project and not specs:
|
|
235
|
+
for name, row in target.wanted().items():
|
|
236
|
+
specs.append(row["source"] or (f"{name}@{row['range']}" if row["range"] else name))
|
|
237
|
+
if not specs:
|
|
238
|
+
raise ValueError(f"{target.file}: [packages] names no package")
|
|
239
|
+
for index, spec in enumerate(specs):
|
|
240
|
+
source = sources.parse(spec)
|
|
241
|
+
if source.kind == "registry":
|
|
242
|
+
requests[source.name] = source.range
|
|
243
|
+
continue
|
|
244
|
+
folder, origin = sources.fetch(source, temporary / str(index), log=log)
|
|
245
|
+
found, more = self._packages_in(folder, temporary / f"wrap{index}", embodiment)
|
|
246
|
+
problems += more
|
|
247
|
+
local += [(package, origin, source) for package in found]
|
|
248
|
+
if zip_bytes is not None:
|
|
249
|
+
folder = archive.unpack(zip_bytes, temporary / "zip")
|
|
250
|
+
found, more = self._packages_in(folder, temporary / "wrapzip", embodiment)
|
|
251
|
+
problems += more
|
|
252
|
+
local += [(package, "upload", None) for package in found]
|
|
253
|
+
if not requests and not local:
|
|
254
|
+
raise ValueError("no package found (a folder with package.toml, skill.toml or SKILL.md)"
|
|
255
|
+
+ (f": {'; '.join(problems)}" if problems else ""))
|
|
256
|
+
if local:
|
|
257
|
+
found = [self.describe(package) for package, _, _ in local]
|
|
258
|
+
if consent is None or not consent(found):
|
|
259
|
+
return {"needs_trust": True, "found": found, "problems": problems}
|
|
260
|
+
for package, _, _ in local:
|
|
261
|
+
self._fits(package)
|
|
262
|
+
records = self.user().packages if target is None else target.locked()
|
|
263
|
+
have = {name: record.version for name, record in records.items() if name not in requests}
|
|
264
|
+
plan = resolve(requests, self.registry if requests or any(p.depends for p, _, _ in local) else _NoRegistry(),
|
|
265
|
+
harness=harness_version(), have=have,
|
|
266
|
+
fixed={package.name: (package.version, package.depends) for package, _, _ in local},
|
|
267
|
+
depends_of=lambda name, version: self._installed_depends(records, name, version))
|
|
268
|
+
done = []
|
|
269
|
+
for package, origin, source in local:
|
|
270
|
+
done.append(self._store_local(package, origin, link=link and source is not None
|
|
271
|
+
and source.kind == "path" and Path(source.path).is_dir(),
|
|
272
|
+
python=python, log=log))
|
|
273
|
+
for name, version in plan.items():
|
|
274
|
+
if any(name == package.name for package, _, _ in local):
|
|
275
|
+
continue
|
|
276
|
+
record = records.get(name)
|
|
277
|
+
if record is not None and record.version == version and (Path(record.path or state.store_path(name, version, record.sha256)) / "package.toml").is_file():
|
|
278
|
+
continue
|
|
279
|
+
done.append(self._store_registry(name, version, temporary / f"registry-{name}", python=python,
|
|
280
|
+
log=log, range_=requests.get(name, "")))
|
|
281
|
+
if target is not None:
|
|
282
|
+
self._write_project(target, done, requests, local)
|
|
283
|
+
self.trust_project(target)
|
|
284
|
+
else:
|
|
285
|
+
user = self.user()
|
|
286
|
+
for record in done:
|
|
287
|
+
old = user.packages.get(record.name)
|
|
288
|
+
if old is not None and not record.range and old.range:
|
|
289
|
+
record.range = old.range
|
|
290
|
+
user.packages[record.name] = record
|
|
291
|
+
user.write()
|
|
292
|
+
self.changed()
|
|
293
|
+
return {"installed": [{"name": r.name, "version": r.version, "source": r.source, "path": r.path}
|
|
294
|
+
for r in done], "problems": problems}
|
|
295
|
+
|
|
296
|
+
def _installed_depends(self, records, name, version):
|
|
297
|
+
record = records.get(name)
|
|
298
|
+
if record is None or record.version != version:
|
|
299
|
+
return None
|
|
300
|
+
try:
|
|
301
|
+
return load(record.path or state.store_path(name, version, record.sha256)).depends
|
|
302
|
+
except ValueError:
|
|
303
|
+
return None
|
|
304
|
+
|
|
305
|
+
def _packages_in(self, folder: Path, wrap: Path, embodiment=None) -> tuple[list[Package], list[str]]:
|
|
306
|
+
"""The package a folder is, or the packages its skills become (one per skill)."""
|
|
307
|
+
if (folder / "package.toml").is_file():
|
|
308
|
+
return [load(folder)], []
|
|
309
|
+
from harness.runtime.execution.skills import SKILLS_ROOT, load_skills
|
|
310
|
+
|
|
311
|
+
built_in = set(load_skills(SKILLS_ROOT, user=False))
|
|
312
|
+
found, problems = find_skills(folder)
|
|
313
|
+
packages = []
|
|
314
|
+
for skill in found:
|
|
315
|
+
if skill["name"] in built_in:
|
|
316
|
+
problems.append(f"{skill['name']}: a built-in skill has this name")
|
|
317
|
+
continue
|
|
318
|
+
if embodiment and skill["format"] == "harness" and embodiment not in skill["embodiments"]:
|
|
319
|
+
problems.append(f"{skill['name']}: a skill for {', '.join(skill['embodiments'])}; the app's agent "
|
|
320
|
+
f"works on this computer (embodiments must include {embodiment})")
|
|
321
|
+
continue
|
|
322
|
+
root = wrap / skill["name"]
|
|
323
|
+
kind = "skills" if skill["format"] == "harness" else "agent-skills"
|
|
324
|
+
shutil.copytree(skill["folder"], root / kind / skill["name"],
|
|
325
|
+
ignore=shutil.ignore_patterns(".git", "__pycache__", ".deps"))
|
|
326
|
+
write(root, name=skill["name"], summary=skill["summary"])
|
|
327
|
+
try:
|
|
328
|
+
packages.append(load(root))
|
|
329
|
+
except ValueError as error:
|
|
330
|
+
problems.append(str(error))
|
|
331
|
+
return packages, problems
|
|
332
|
+
|
|
333
|
+
def _fits(self, package: Package) -> None:
|
|
334
|
+
if not harness_fits({"harness": package.harness}, harness_version()):
|
|
335
|
+
raise ResolveError(f"{package.name} {package.version} works with harness {package.harness}, "
|
|
336
|
+
f"this is {harness_version()}")
|
|
337
|
+
if not package.runs_here():
|
|
338
|
+
raise ResolveError(f"{package.name} runs on {', '.join(package.platforms)}, not here")
|
|
339
|
+
|
|
340
|
+
def _store_local(self, package: Package, origin: str, *, link: bool, python, log) -> state.Record:
|
|
341
|
+
if link:
|
|
342
|
+
log(f"{package.name} {package.version}: linked to {package.path}")
|
|
343
|
+
self._requirements(package.path, python, log)
|
|
344
|
+
return state.Record(package.name, package.version, "", origin, path=str(package.path), link=True)
|
|
345
|
+
digest = tree_hash(package.path, archive.files(package.path))
|
|
346
|
+
target = state.store_path(package.name, package.version, digest)
|
|
347
|
+
self._copy_in(package.path, target, python, log)
|
|
348
|
+
log(f"{package.name} {package.version}: installed from {origin}")
|
|
349
|
+
return state.Record(package.name, package.version, digest, origin, path=str(target))
|
|
350
|
+
|
|
351
|
+
def _store_registry(self, name, version, temporary: Path, *, python, log, range_="") -> state.Record:
|
|
352
|
+
entry = self.registry.entry(name, version)
|
|
353
|
+
if not harness_fits(entry, harness_version()):
|
|
354
|
+
raise ResolveError(f"{name} {version} works with harness {entry.get('harness')}, this is "
|
|
355
|
+
f"{harness_version()}")
|
|
356
|
+
target = state.store_path(name, version, entry["sha256"])
|
|
357
|
+
if not (target / "package.toml").is_file():
|
|
358
|
+
folder, _ = self.registry.download(name, version, temporary, log=log)
|
|
359
|
+
package = load(folder)
|
|
360
|
+
if (package.name, package.version) != (name, version):
|
|
361
|
+
raise RegistryError(f"{self.registry.base}: the file of {name} {version} holds "
|
|
362
|
+
f"{package.name} {package.version}")
|
|
363
|
+
self._fits(package)
|
|
364
|
+
self._copy_in(folder, target, python, log)
|
|
365
|
+
log(f"{name} {version}: installed")
|
|
366
|
+
return state.Record(name, version, entry["sha256"], "registry" if self.registry.official
|
|
367
|
+
else f"registry:{self.registry.base}", range=range_, path=str(target))
|
|
368
|
+
|
|
369
|
+
def _copy_in(self, folder: Path, target: Path, python, log) -> None:
|
|
370
|
+
if (target / "package.toml").is_file():
|
|
371
|
+
return
|
|
372
|
+
staging = target.with_name(target.name + ".partial")
|
|
373
|
+
state.delete(staging)
|
|
374
|
+
shutil.copytree(folder, staging, ignore=shutil.ignore_patterns(".git", "__pycache__", ".deps",
|
|
375
|
+
"*.pyc", ".installed.json"))
|
|
376
|
+
try:
|
|
377
|
+
self._requirements(staging, python, log)
|
|
378
|
+
except Exception:
|
|
379
|
+
state.delete(staging)
|
|
380
|
+
raise
|
|
381
|
+
state.freeze(staging)
|
|
382
|
+
state.delete(target)
|
|
383
|
+
staging.rename(target)
|
|
384
|
+
|
|
385
|
+
def _requirements(self, folder: Path, python, log) -> None:
|
|
386
|
+
"""Each skill's pip requirements, in its own .deps folder (on the kernel's path)."""
|
|
387
|
+
import subprocess
|
|
388
|
+
|
|
389
|
+
for path in sorted(Path(folder).glob("skills/*/skill.toml")):
|
|
390
|
+
wanted = list(state._toml(path).get("requirements", []))
|
|
391
|
+
if not wanted or (path.parent / ".deps").is_dir():
|
|
392
|
+
continue
|
|
393
|
+
log(f"{path.parent.name}: installing {', '.join(wanted)}")
|
|
394
|
+
interpreter = python() if callable(python) else python
|
|
395
|
+
result = subprocess.run([interpreter or sys.executable, "-X", "utf8", "-m", "pip", "install", "--quiet",
|
|
396
|
+
"--disable-pip-version-check", "--target", str(path.parent / ".deps"), *wanted],
|
|
397
|
+
capture_output=True, encoding="utf-8", errors="replace", timeout=1800)
|
|
398
|
+
if result.returncode:
|
|
399
|
+
shutil.rmtree(path.parent / ".deps", ignore_errors=True)
|
|
400
|
+
raise ValueError(f"{path.parent.name}: its requirements did not install: "
|
|
401
|
+
f"{(result.stderr or result.stdout).strip()[-400:]}")
|
|
402
|
+
|
|
403
|
+
def _write_project(self, project: state.Project, done, requests, local) -> None:
|
|
404
|
+
wanted = project.wanted()
|
|
405
|
+
for name, range_ in requests.items():
|
|
406
|
+
wanted[name] = {"range": range_, "source": wanted.get(name, {}).get("source", "")}
|
|
407
|
+
for package, origin, _ in local:
|
|
408
|
+
wanted[package.name] = {"range": "", "source": origin.split("#sha256=")[0]}
|
|
409
|
+
locked = project.locked()
|
|
410
|
+
for record in done:
|
|
411
|
+
locked[record.name] = record
|
|
412
|
+
project.set_wanted(wanted)
|
|
413
|
+
project.write_lock(locked.values())
|
|
414
|
+
|
|
415
|
+
def trust_project(self, project: state.Project) -> None:
|
|
416
|
+
user = self.user()
|
|
417
|
+
if str(project.root) not in user.trusted_projects:
|
|
418
|
+
user.trusted_projects.append(str(project.root))
|
|
419
|
+
user.write()
|
|
420
|
+
|
|
421
|
+
def remove(self, names, *, project: bool = False, log=print) -> list[str]:
|
|
422
|
+
"""Remove packages (from the user's list, or the project's); their stored
|
|
423
|
+
versions go when nothing else uses them."""
|
|
424
|
+
names = [str(name) for name in names]
|
|
425
|
+
if project:
|
|
426
|
+
target = self.project()
|
|
427
|
+
if target is None:
|
|
428
|
+
raise ValueError("no harness.toml with [packages] here or above")
|
|
429
|
+
wanted, locked = target.wanted(), target.locked()
|
|
430
|
+
missing = [name for name in names if name not in wanted and name not in locked]
|
|
431
|
+
if missing:
|
|
432
|
+
raise ValueError(f"the project has no package {', '.join(missing)}")
|
|
433
|
+
for name in names:
|
|
434
|
+
wanted.pop(name, None)
|
|
435
|
+
locked.pop(name, None)
|
|
436
|
+
target.set_wanted(wanted)
|
|
437
|
+
target.write_lock(locked.values())
|
|
438
|
+
else:
|
|
439
|
+
user = self.user()
|
|
440
|
+
missing = [name for name in names if name not in user.packages]
|
|
441
|
+
if missing:
|
|
442
|
+
raise ValueError(f"no installed package {', '.join(missing)}")
|
|
443
|
+
dependents = {other.name: sorted(set(other.depends) & set(names)) for other in self.enabled()
|
|
444
|
+
if other.name not in names and set(other.depends) & set(names)}
|
|
445
|
+
if dependents:
|
|
446
|
+
raise ValueError("still needed: " + "; ".join(f"{name} depends on {', '.join(of)}"
|
|
447
|
+
for name, of in sorted(dependents.items())))
|
|
448
|
+
for name in names:
|
|
449
|
+
user.packages.pop(name)
|
|
450
|
+
user.write()
|
|
451
|
+
self.collect(log=log)
|
|
452
|
+
self.changed()
|
|
453
|
+
return names
|
|
454
|
+
|
|
455
|
+
def collect(self, *, log=print) -> None:
|
|
456
|
+
"""Delete stored versions the user's list and this project's lock no longer use."""
|
|
457
|
+
used = {Path(record.path).resolve() for record in self.user().packages.values() if record.path}
|
|
458
|
+
project = self.project()
|
|
459
|
+
if project is not None:
|
|
460
|
+
used |= {state.store_path(n, r.version, r.sha256).resolve() for n, r in project.locked().items()}
|
|
461
|
+
root = state.store_root()
|
|
462
|
+
for folder in sorted(root.glob("*/*")) if root.is_dir() else []:
|
|
463
|
+
if folder.is_dir() and folder.resolve() not in used:
|
|
464
|
+
state.delete(folder)
|
|
465
|
+
for folder in sorted(root.glob("*")) if root.is_dir() else []:
|
|
466
|
+
if folder.is_dir() and not any(folder.iterdir()):
|
|
467
|
+
folder.rmdir()
|
|
468
|
+
|
|
469
|
+
def update(self, names=(), *, project: bool = False, log=print) -> dict:
|
|
470
|
+
"""Update registry packages (all, or `names`) to the highest version in the range
|
|
471
|
+
they were installed with that works with this harness."""
|
|
472
|
+
records = self.project().locked() if project and self.project() else self.user().packages
|
|
473
|
+
names = [str(name) for name in names] or sorted(records)
|
|
474
|
+
unknown = [name for name in names if name not in records]
|
|
475
|
+
if unknown:
|
|
476
|
+
raise ValueError(f"no installed package {', '.join(unknown)}")
|
|
477
|
+
specs = []
|
|
478
|
+
wanted = self.project().wanted() if project and self.project() else {}
|
|
479
|
+
for name in names:
|
|
480
|
+
record = records[name]
|
|
481
|
+
if not record.source.startswith("registry"):
|
|
482
|
+
log(f"{name}: installed from {record.source}; install it again to update it")
|
|
483
|
+
continue
|
|
484
|
+
range_ = wanted.get(name, {}).get("range", "") if project else record.range
|
|
485
|
+
latest = self.registry.pick(name, range_)
|
|
486
|
+
if latest != record.version:
|
|
487
|
+
specs.append(f"{name}@=={latest}")
|
|
488
|
+
if not specs:
|
|
489
|
+
log("everything is up to date")
|
|
490
|
+
return {"installed": [], "problems": []}
|
|
491
|
+
result = self.install(specs, project=project, log=log)
|
|
492
|
+
if not project:
|
|
493
|
+
user = self.user()
|
|
494
|
+
for name in names:
|
|
495
|
+
if name in user.packages and name in records:
|
|
496
|
+
user.packages[name].range = records[name].range
|
|
497
|
+
user.write()
|
|
498
|
+
return result
|
|
499
|
+
|
|
500
|
+
def enable(self, name: str, on: bool) -> None:
|
|
501
|
+
user = self.user()
|
|
502
|
+
if name not in user.packages:
|
|
503
|
+
raise ValueError(f"no installed package {name}")
|
|
504
|
+
user.packages[name].enabled = bool(on)
|
|
505
|
+
user.write()
|
|
506
|
+
self.changed()
|
|
507
|
+
|
|
508
|
+
# -- first use ---------------------------------------------------------------------
|
|
509
|
+
|
|
510
|
+
def ensure(self, what: str, name: str, *, ask=None, log=print) -> Package:
|
|
511
|
+
"""The enabled package that provides `name` among its `what` (skills, embodiments,
|
|
512
|
+
benches, services, kinds, commands); when none does, the registry's package that
|
|
513
|
+
provides it, installed as packages.auto_install says. MissingPackage otherwise."""
|
|
514
|
+
package = self.provider(what, name)
|
|
515
|
+
if package is not None:
|
|
516
|
+
return package
|
|
517
|
+
singular = SINGULAR.get(what, what)
|
|
518
|
+
if self.auto_install() == "off":
|
|
519
|
+
raise MissingPackage(f"no installed package provides the {singular} {name}; "
|
|
520
|
+
f"select and install its package with harness install "
|
|
521
|
+
f"(packages.auto_install is off)")
|
|
522
|
+
try:
|
|
523
|
+
candidates = self.registry.providers(what, name)
|
|
524
|
+
except RegistryError as error:
|
|
525
|
+
raise MissingPackage(f"no installed package provides the {singular} {name}, and {error}") from None
|
|
526
|
+
if not candidates:
|
|
527
|
+
raise MissingPackage(f"no package provides the {singular} {name} (registry {self.registry.base})")
|
|
528
|
+
choice = name if name in candidates else candidates[0]
|
|
529
|
+
mode = self.auto_install()
|
|
530
|
+
if mode == "off":
|
|
531
|
+
raise MissingPackage(f"the {singular} {name} is in the package {choice}: harness install {choice} "
|
|
532
|
+
"(packages.auto_install is off)")
|
|
533
|
+
if mode == "ask" and not (ask and ask({"package": choice, "what": singular, "name": name,
|
|
534
|
+
"registry": self.registry.base})):
|
|
535
|
+
raise MissingPackage(f"the {singular} {name} needs the package {choice}, which was not installed")
|
|
536
|
+
log(f"the {singular} {name} is in the package {choice}: installing it")
|
|
537
|
+
self.install([choice], log=log)
|
|
538
|
+
package = self.provider(what, name)
|
|
539
|
+
if package is None:
|
|
540
|
+
raise MissingPackage(f"{choice} was installed but does not provide the {singular} {name}")
|
|
541
|
+
return package
|
|
542
|
+
|
|
543
|
+
# -- the running process -----------------------------------------------------------
|
|
544
|
+
|
|
545
|
+
def changed(self) -> None:
|
|
546
|
+
"""Make this process see the packages as they are now."""
|
|
547
|
+
activate(self)
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
def activate(packages: Packages | None = None) -> Packages:
|
|
551
|
+
"""Make the enabled packages load in this process and in the processes it starts
|
|
552
|
+
(HARNESS_PACKAGE_ROOTS, which paths.package_roots() reads: their skills, benches,
|
|
553
|
+
prompts and modules); first, on the first start since packages, turn the app's
|
|
554
|
+
installed skills into packages (migrate.py)."""
|
|
555
|
+
from ..paths import PACKAGES_ENV, packages_changed
|
|
556
|
+
from .migrate import migrate
|
|
557
|
+
|
|
558
|
+
packages = packages or Packages()
|
|
559
|
+
migrate(packages)
|
|
560
|
+
enabled = packages.enabled()
|
|
561
|
+
os.environ[PACKAGES_ENV] = os.pathsep.join(str(package.path) for package in enabled)
|
|
562
|
+
packages_changed()
|
|
563
|
+
for package in enabled:
|
|
564
|
+
if str(package.path) not in sys.path:
|
|
565
|
+
sys.path.append(str(package.path))
|
|
566
|
+
try:
|
|
567
|
+
from harness.runtime.execution import skills
|
|
568
|
+
|
|
569
|
+
skills._CACHE.clear()
|
|
570
|
+
except ImportError:
|
|
571
|
+
pass
|
|
572
|
+
return packages
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
class _NoRegistry:
|
|
576
|
+
"""Resolution without the registry: packages from other sources that need nothing."""
|
|
577
|
+
|
|
578
|
+
base = "(no registry needed)"
|
|
579
|
+
|
|
580
|
+
def versions(self, name):
|
|
581
|
+
raise RegistryError(f"{name} would come from the registry")
|
|
582
|
+
|
|
583
|
+
def packages(self):
|
|
584
|
+
return {}
|
|
585
|
+
|
|
586
|
+
def entry(self, name, version):
|
|
587
|
+
raise RegistryError(f"{name} would come from the registry")
|