@lmmslab/harness 0.83.0 → 0.84.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 (32) hide show
  1. package/bin/harness.js +6 -1
  2. package/package.json +1 -1
  3. package/python/harness/__init__.py +1 -1
  4. package/python/harness/builtin/prompts/core/core.md +45 -30
  5. package/python/harness/builtin/prompts/shared/learning-goal.md +1 -1
  6. package/python/harness/cli.py +103 -17
  7. package/python/harness/core/trace.py +23 -3
  8. package/python/harness/core/worker.py +6 -0
  9. package/python/harness/host/client.py +26 -21
  10. package/python/harness/host/http.py +11 -1
  11. package/python/harness/host/sessions.py +38 -3
  12. package/python/harness/host/tasks.py +62 -26
  13. package/python/harness/model_list.py +25 -5
  14. package/python/harness/packages/environments.py +51 -12
  15. package/python/harness/packages/manager.py +379 -102
  16. package/python/harness/packages/resolve.py +52 -15
  17. package/python/harness/packages/sources.py +17 -5
  18. package/python/harness/packages/state.py +10 -0
  19. package/python/harness/providers.py +66 -24
  20. package/python/harness/runtime/decision/model.py +9 -4
  21. package/python/harness/runtime/execution/entries.py +53 -7
  22. package/python/harness/runtime/execution/session.py +52 -7
  23. package/python/harness/runtime/learning/active.py +4 -4
  24. package/python/harness/runtime/reactive/tree.py +35 -22
  25. package/python/harness/runtime/task/run.py +25 -3
  26. package/python/harness/runtime/tick.py +34 -3
  27. package/python/harness/settings.py +5 -1
  28. package/python/harness/tui/__init__.py +28 -6
  29. package/python/harness/tui/chat.py +243 -67
  30. package/python/harness/tui/pty.py +16 -0
  31. package/python/harness/tui/renderer.py +34 -0
  32. package/python/harness/tui/views.py +160 -38
@@ -19,19 +19,24 @@ official installs the registry's package that provides it, ask asks first, off s
19
19
  with the command that would install it.
20
20
  """
21
21
 
22
+ import contextlib
22
23
  import os
23
24
  from pathlib import Path
25
+ import shlex
24
26
  import shutil
25
27
  import sys
26
28
  import tempfile
29
+ import threading
27
30
 
28
31
  from ..core import tree_hash
32
+ from ..core.procs import file_lock
29
33
  from ..paths import data as data_dir
30
34
  from . import archive, sources, state
31
35
  from .addons import find_skills
32
- from .manifest import Package, load, write
36
+ from .manifest import Package, load, platform, write
33
37
  from .registry import Registry, RegistryError, configured, harness_fits
34
38
  from .resolve import ResolveError, resolve
39
+ from .versions import check_range, satisfies
35
40
 
36
41
 
37
42
  AUTO_INSTALL = ("official", "ask", "off")
@@ -41,6 +46,39 @@ SINGULAR = {"kinds": "kind", "embodiments": "embodiment", "skills": "skill", "be
41
46
  "commands": "command", "agents": "agent"}
42
47
 
43
48
 
49
+ class PackageBusy(RuntimeError):
50
+ """Another thread or process is already changing this shared package store."""
51
+
52
+
53
+ _MUTATIONS = threading.local()
54
+
55
+
56
+ @contextlib.contextmanager
57
+ def _package_lock():
58
+ """Try a store transaction without queuing; reentrant in its calling thread."""
59
+ path = state.store_root().parent / "locks" / "packages.lock"
60
+ # Include pid so a child forked inside a transaction cannot inherit permission
61
+ # to bypass the parent's OS lock. New threads have their own held set.
62
+ key = (os.getpid(), str(path.resolve()))
63
+ held = getattr(_MUTATIONS, "held", None)
64
+ if held is None:
65
+ held = _MUTATIONS.held = set()
66
+ if key in held:
67
+ yield
68
+ return
69
+ with contextlib.ExitStack() as stack:
70
+ try:
71
+ stack.enter_context(file_lock(path, blocking=False))
72
+ except BlockingIOError:
73
+ raise PackageBusy("another package operation is running; this request was not queued. "
74
+ "Wait for it to finish, then retry the package command or task.") from None
75
+ held.add(key)
76
+ try:
77
+ yield
78
+ finally:
79
+ held.remove(key)
80
+
81
+
44
82
  class MissingPackage(RuntimeError):
45
83
  """A run needs something no enabled package provides, and it was not installed."""
46
84
 
@@ -79,6 +117,72 @@ class Packages:
79
117
  self._registry = Registry(configured(self.settings().get("registry", "")))
80
118
  return self._registry
81
119
 
120
+ def _registry_identity(self) -> str:
121
+ return "registry" if self.registry.official else f"registry:{self.registry.base}"
122
+
123
+ @staticmethod
124
+ def _is_registry(record) -> bool:
125
+ return record.source == "registry" or record.source.startswith("registry:")
126
+
127
+ def _published_record(self, name, record):
128
+ # Reading the index is intentionally outside missing-name/version handling:
129
+ # an unavailable registry must retain its real I/O error, not look like new content.
130
+ package = self.registry.packages().get(name, {})
131
+ return package.get("versions", {}).get(record.version)
132
+
133
+ def _registry_matches(self, name, record) -> bool:
134
+ """Registry identity includes byte-identical mirrors of a locked archive."""
135
+ if not self._is_registry(record):
136
+ return False
137
+ if record.source == self._registry_identity():
138
+ return True
139
+ entry = self._published_record(name, record)
140
+ return bool(entry and record.sha256) and entry["sha256"] == record.sha256
141
+
142
+ def _use_installed(self, name, record, requests) -> bool:
143
+ # Registry-backed active nodes follow the configured registry/content.
144
+ # An unrequested development source remains an explicit owner choice.
145
+ return self._registry_matches(name, record) if self._is_registry(record) else name not in requests
146
+
147
+ @staticmethod
148
+ def _record_folder(name, record) -> Path:
149
+ return Path(record.path) if record.path else state.store_path(name, record.version, record.sha256)
150
+
151
+ @staticmethod
152
+ def _quote_spec(spec, *, windows=None) -> str:
153
+ if windows is None:
154
+ windows = os.name == "nt"
155
+ return f'"{spec}"' if windows else shlex.quote(spec)
156
+
157
+ def _repair_command(self, name, record, *, project, requested=(), records=None) -> str:
158
+ scope = " --project" if project else ""
159
+ target = self.project() if project else None
160
+ if self._is_registry(record) and (not project or target is not None):
161
+ names = list(dict.fromkeys([*sorted(requested), name]))
162
+ names = [item for item in names if records is None or item in records]
163
+ return f"harness install --update{scope}" + (" " + " ".join(names) if names else "")
164
+ range_ = target.wanted().get(name, {}).get("range", "") if target is not None else record.range
165
+ range_ = check_range(range_)
166
+ spec = f"{name}@{range_}" if range_ else name
167
+ return f"harness install{scope} {self._quote_spec(spec)}"
168
+
169
+ def _require_locked_content(self, name, record, *, project=True, requested=(), records=None) -> None:
170
+ if not self._is_registry(record):
171
+ return
172
+ matches = self._registry_matches(name, record)
173
+ if matches and not (self._record_folder(name, record) / "package.toml").is_file():
174
+ # Same-address caches can be restored offline, but when files are
175
+ # missing the selected archive must still be the recorded content.
176
+ entry = self._published_record(name, record)
177
+ matches = bool(entry and record.sha256) and entry["sha256"] == record.sha256
178
+ if not matches:
179
+ command = self._repair_command(name, record, project=project, requested=requested, records=records)
180
+ retry = ("; then retry the original command" if records is not None
181
+ and any(item not in records for item in requested) else "")
182
+ raise ResolveError(f"the configured registry cannot restore locked {name} {record.version} "
183
+ f"with its recorded content; run {command} to explicitly select this registry "
184
+ f"while preserving recorded ranges{retry}")
185
+
82
186
  def user(self) -> state.UserList:
83
187
  return state.UserList.read(self.data)
84
188
 
@@ -114,6 +218,7 @@ class Packages:
114
218
  package = load(record.path)
115
219
  if package.name != name:
116
220
  raise ValueError(f"{record.path} holds {package.name}, not {name}")
221
+ self._fits(package)
117
222
  out.append(package)
118
223
  except ValueError as error:
119
224
  self.problems[name] = str(error)
@@ -216,7 +321,7 @@ class Packages:
216
321
  # -- install, remove, update -------------------------------------------------------
217
322
 
218
323
  def install(self, specs=(), *, project: bool = False, consent=None, link: bool = False, python=None,
219
- zip_bytes: bytes | None = None, embodiment: str | None = None, log=print) -> dict:
324
+ zip_bytes: bytes | None = None, embodiment: str | None = None, log=print, _automatic=False) -> dict:
220
325
  """Install packages by source text (sources.py), or `zip_bytes` (an archive the app
221
326
  was given). Packages from outside the registry need `consent(found) -> bool`
222
327
  (found: describe() of each); without it nothing changes and {"needs_trust": True,
@@ -225,81 +330,175 @@ class Packages:
225
330
  installed and the project trusted). With `embodiment`, a folder's skills are
226
331
  taken only when they run on it (the app's agent: desktop). Returns
227
332
  {"installed": [...], "problems": [...]}."""
333
+ with tempfile.TemporaryDirectory(prefix="harness-package-") as temporary:
334
+ prepared = self._prepare(specs, Path(temporary), project=project, consent=consent,
335
+ zip_bytes=zip_bytes, embodiment=embodiment, log=log)
336
+ if prepared.get("needs_trust"):
337
+ return prepared
338
+ with _package_lock():
339
+ result = self._install(prepared, link=link, python=python, log=log,
340
+ declare=prepared.get("wanted") is None, automatic=_automatic)
341
+ # A top-level mutation releases its store lock before notifying the host.
342
+ # A nested callback mutation can still be in its outer transaction.
343
+ self.changed()
344
+ return result
345
+
346
+ def _prepare(self, specs, temporary: Path, *, project=False, consent=None,
347
+ zip_bytes=None, embodiment=None, log=print) -> dict:
348
+ """Fetch and inspect sources and obtain trust without holding the store lock."""
228
349
  specs = [str(spec) for spec in specs]
229
350
  target = self.project() if project else None
230
351
  if project and target is None:
231
352
  target = state.Project(Path(self.cwd or os.getcwd()).resolve())
232
- with tempfile.TemporaryDirectory(prefix="harness-package-") as temporary:
233
- temporary = Path(temporary)
234
- requests, local, problems = {}, [], []
235
- if project and not specs:
236
- for name, row in target.wanted().items():
237
- specs.append(row["source"] or (f"{name}@{row['range']}" if row["range"] else name))
238
- if not specs:
239
- raise ValueError(f"{target.file}: [packages] names no package")
240
- for index, spec in enumerate(specs):
241
- source = sources.parse(spec)
242
- if source.kind == "registry":
243
- requests[source.name] = source.range
244
- continue
245
- folder, origin = sources.fetch(source, temporary / str(index), log=log)
246
- found, more = self._packages_in(folder, temporary / f"wrap{index}", embodiment)
247
- problems += more
248
- local += [(package, origin, source) for package in found]
249
- if zip_bytes is not None:
250
- folder = archive.unpack(zip_bytes, temporary / "zip")
251
- found, more = self._packages_in(folder, temporary / "wrapzip", embodiment)
252
- problems += more
253
- local += [(package, "upload", None) for package in found]
254
- if not requests and not local:
255
- raise ValueError("no package found (a folder with package.toml, skill.toml or SKILL.md)"
256
- + (f": {'; '.join(problems)}" if problems else ""))
257
- if local:
258
- found = [self.describe(package) for package, _, _ in local]
259
- if consent is None or not consent(found):
260
- return {"needs_trust": True, "found": found, "problems": problems}
261
- for package, _, _ in local:
262
- self._fits(package)
263
- records = self.user().packages if target is None else target.locked()
264
- have = {name: record.version for name, record in records.items() if name not in requests}
353
+ requests, local, problems = {}, [], []
354
+ wanted = target.wanted() if project and not specs else None
355
+ if wanted is not None:
356
+ for name, row in wanted.items():
357
+ specs.append(row["source"] or (f"{name}@{row['range']}" if row["range"] else name))
358
+ if not specs:
359
+ raise ValueError(f"{target.file}: [packages] names no package")
360
+ for index, spec in enumerate(specs):
361
+ source = sources.parse(spec, base=target.root if wanted is not None else None)
362
+ if source.kind == "registry":
363
+ requests[source.name] = source.range
364
+ continue
365
+ folder, origin = sources.fetch(source, temporary / str(index), log=log)
366
+ found, more = self._packages_in(folder, temporary / f"wrap{index}", embodiment)
367
+ problems += more
368
+ local += [(package, origin, source) for package in found]
369
+ if zip_bytes is not None:
370
+ folder = archive.unpack(zip_bytes, temporary / "zip")
371
+ found, more = self._packages_in(folder, temporary / "wrapzip", embodiment)
372
+ problems += more
373
+ local += [(package, "upload", None) for package in found]
374
+ if not requests and not local:
375
+ raise ValueError("no package found (a folder with package.toml, skill.toml or SKILL.md)"
376
+ + (f": {'; '.join(problems)}" if problems else ""))
377
+ if local:
378
+ found = [self.describe(package) for package, _, _ in local]
379
+ if consent is None or not consent(found):
380
+ return {"needs_trust": True, "found": found, "problems": problems}
381
+ for package, _, _ in local:
382
+ self._fits(package)
383
+ return {"target": target, "requests": requests, "local": local,
384
+ "problems": problems, "temporary": temporary, "wanted": wanted}
385
+
386
+ def _install(self, prepared, *, link=False, python=None, log=print, declare=True, automatic=False) -> dict:
387
+ """Resolve and publish prepared sources while holding the store transaction."""
388
+ target, requests, local = prepared["target"], prepared["requests"], prepared["local"]
389
+ problems, temporary = prepared["problems"], prepared["temporary"]
390
+ if prepared.get("wanted") is not None and target.wanted() != prepared["wanted"]:
391
+ raise ResolveError(f"{target.file} changed while preparing its packages; "
392
+ "run harness install --project again to review the current requests")
393
+ records = self.user().packages if target is None else target.locked()
394
+ restoring = prepared.get("wanted") is not None
395
+ if restoring:
396
+ for name in requests:
397
+ if name in records:
398
+ self._require_locked_content(name, records[name], requested=(name,), records=records)
399
+ have = {name: record.version for name, record in records.items()
400
+ if name not in requests or restoring}
401
+ try:
265
402
  plan = resolve(requests, self.registry if requests or any(p.depends for p, _, _ in local) else _NoRegistry(),
266
403
  harness=harness_version(), have=have,
267
404
  fixed={package.name: (package.version, package.depends) for package, _, _ in local},
268
- depends_of=lambda name, version: self._installed_depends(records, name, version))
269
- done = []
270
- for package, origin, source in local:
271
- done.append(self._store_local(package, origin, link=link and source is not None
272
- and source.kind == "path" and Path(source.path).is_dir(),
273
- python=python, log=log))
274
- for name, version in plan.items():
275
- if any(name == package.name for package, _, _ in local):
276
- continue
277
- record = records.get(name)
278
- 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():
279
- continue
280
- done.append(self._store_registry(name, version, temporary / f"registry-{name}", python=python,
281
- log=log, range_=requests.get(name, "")))
405
+ depends_of=lambda name, version: self._installed_depends(records, name, version),
406
+ use_installed=lambda name, version: (name in records and records[name].version == version
407
+ and self._use_installed(name, records[name], requests)))
408
+ except ResolveError as error:
409
+ if prepared.get("wanted") is not None:
410
+ raise ResolveError(f"{error}. For changed ranges, run harness install --update --project. "
411
+ "For a package removed from harness.toml, also remove its lock entry "
412
+ "with harness remove --project NAME, then retry.") from None
413
+ raise
414
+ explicit = set(requests) | {package.name for package, _, _ in local}
415
+ declared = target.wanted() if target is not None else {}
416
+ reusable = set()
417
+ local_names = {package.name for package, _, _ in local}
418
+ # Check the entire selected plan before any local package is staged/stored.
419
+ for name, version in plan.items():
420
+ self._runtime_fits(name, version)
421
+ record = records.get(name)
422
+ recorded_range = declared.get(name, {}).get("range", "") if target is not None else (record.range if record else "")
423
+ if (not declare or name not in explicit) and recorded_range and not satisfies(version, recorded_range):
424
+ raise ResolveError(f"{name} {version} would violate its recorded range {recorded_range}; "
425
+ f"choose compatible packages or explicitly install {name} with the intended range")
426
+ if name not in local_names and self._preflight(name, version, record, requests,
427
+ project=target is not None,
428
+ restoring=automatic or target is not None and (restoring or name not in explicit),
429
+ repair_names=explicit, repair_records=records):
430
+ reusable.add(name)
431
+ done = []
432
+ for package, origin, source in local:
433
+ done.append(self._store_local(package, origin, link=link and source is not None
434
+ and source.kind == "path" and Path(source.path).is_dir(),
435
+ python=python, log=log))
436
+ for name, version in plan.items():
437
+ if any(name == package.name for package, _, _ in local):
438
+ continue
439
+ if name in reusable:
440
+ continue
441
+ done.append(self._store_registry(name, version, temporary / f"registry-{name}", python=python,
442
+ log=log, range_=requests.get(name, "")))
443
+ for record in done:
444
+ old = records.get(record.name)
445
+ if old is not None:
446
+ record.enabled = old.enabled
447
+ if self._is_registry(old) and (record.version, record.sha256) == (old.version, old.sha256):
448
+ record.source = old.source # Keep official/portable locks when restoring from a mirror.
282
449
  if target is not None:
283
- self._write_project(target, done, requests, local)
284
- self.trust_project(target)
450
+ self._write_project(target, done, requests, local, declare=declare)
451
+ self._trust_project(target)
285
452
  else:
286
453
  user = self.user()
287
454
  for record in done:
288
455
  old = user.packages.get(record.name)
289
- if old is not None and not record.range and old.range:
456
+ if record.name not in explicit and old is not None and not record.range and old.range:
290
457
  record.range = old.range
291
458
  user.packages[record.name] = record
292
459
  user.write()
293
- self.changed()
294
460
  return {"installed": [{"name": r.name, "version": r.version, "source": r.source, "path": r.path}
295
461
  for r in done], "problems": problems}
296
462
 
463
+ def _preflight(self, name, version, record, requests, *, project, restoring=False,
464
+ repair_names=(), repair_records=None) -> bool:
465
+ """Validate reused/registry packages without writing anything; return record reuse."""
466
+ if restoring and record is not None:
467
+ self._require_locked_content(name, record, project=project,
468
+ requested=repair_names, records=repair_records)
469
+ folder = self._record_folder(name, record) if record else None
470
+ reuse = (record is not None and record.version == version
471
+ and self._use_installed(name, record, requests)
472
+ and (folder / "package.toml").is_file())
473
+ if record is not None and not self._is_registry(record) and name not in requests and not reuse:
474
+ command = self._repair_command(name, record, project=project)
475
+ raise ResolveError(f"{name} is installed from {record.source} and cannot be reused; "
476
+ f"repair that source or explicitly select the registry with {command}")
477
+ try:
478
+ if reuse:
479
+ self._fits(load(folder))
480
+ else:
481
+ entry = self.registry.entry(name, version)
482
+ if not harness_fits(entry, harness_version()):
483
+ raise ResolveError(f"{name} {version} works with harness {entry.get('harness')}, "
484
+ f"this is {harness_version()}")
485
+ if entry.get("platforms") and platform() not in entry["platforms"]:
486
+ raise ResolveError(f"{name} runs on {', '.join(entry['platforms'])}, not here")
487
+ cached = state.store_path(name, version, entry["sha256"])
488
+ if (cached / "package.toml").is_file():
489
+ self._fits(load(cached))
490
+ except ValueError as error:
491
+ command = "harness install --update" + (" --project" if project else "")
492
+ raise ResolveError(f"{error}; run {command} to select compatible packages, "
493
+ "or use a compatible harness core/platform") from None
494
+ return reuse
495
+
297
496
  def _installed_depends(self, records, name, version):
298
497
  record = records.get(name)
299
498
  if record is None or record.version != version:
300
499
  return None
301
500
  try:
302
- return load(record.path or state.store_path(name, version, record.sha256)).depends
501
+ return load(self._record_folder(name, record)).depends
303
502
  except ValueError:
304
503
  return None
305
504
 
@@ -331,7 +530,16 @@ class Packages:
331
530
  problems.append(str(error))
332
531
  return packages, problems
333
532
 
533
+ def _runtime_fits(self, name: str, version: str) -> None:
534
+ # computer 0.8 introduced the dispatch protocol used by this core. Older
535
+ # immutable archives declare a broad core range that predates that change.
536
+ if name == "computer" and satisfies(harness_version(), ">=0.82") and not satisfies(version, ">=0.8"):
537
+ raise ResolveError(f"computer {version} is incompatible with harness {harness_version()}; "
538
+ "if browser is installed, run harness install computer@^0.8 browser together; "
539
+ "otherwise run harness install computer@^0.8. Add --project for project packages")
540
+
334
541
  def _fits(self, package: Package) -> None:
542
+ self._runtime_fits(package.name, package.version)
335
543
  if not harness_fits({"harness": package.harness}, harness_version()):
336
544
  raise ResolveError(f"{package.name} {package.version} works with harness {package.harness}, "
337
545
  f"this is {harness_version()}")
@@ -364,8 +572,8 @@ class Packages:
364
572
  self._fits(package)
365
573
  self._copy_in(folder, target, python, log)
366
574
  log(f"{name} {version}: installed")
367
- return state.Record(name, version, entry["sha256"], "registry" if self.registry.official
368
- else f"registry:{self.registry.base}", range=range_, path=str(target))
575
+ return state.Record(name, version, entry["sha256"], self._registry_identity(),
576
+ range=range_, path=str(target))
369
577
 
370
578
  def _copy_in(self, folder: Path, target: Path, python, log) -> None:
371
579
  if (target / "package.toml").is_file():
@@ -401,27 +609,38 @@ class Packages:
401
609
  raise ValueError(f"{path.parent.name}: its requirements did not install: "
402
610
  f"{(result.stderr or result.stdout).strip()[-400:]}")
403
611
 
404
- def _write_project(self, project: state.Project, done, requests, local) -> None:
405
- wanted = project.wanted()
406
- for name, range_ in requests.items():
407
- wanted[name] = {"range": range_, "source": wanted.get(name, {}).get("source", "")}
408
- for package, origin, _ in local:
409
- wanted[package.name] = {"range": "", "source": origin.split("#sha256=")[0]}
612
+ def _write_project(self, project: state.Project, done, requests, local, *, declare=True) -> None:
613
+ # Installation declares direct dependencies; an update only changes the lock.
614
+ if declare:
615
+ wanted = project.wanted()
616
+ for name, range_ in requests.items():
617
+ wanted[name] = {"range": range_, "source": ""}
618
+ for package, origin, _ in local:
619
+ wanted[package.name] = {"range": "", "source": origin.split("#sha256=")[0]}
620
+ project.set_wanted(wanted)
410
621
  locked = project.locked()
411
622
  for record in done:
412
623
  locked[record.name] = record
413
- project.set_wanted(wanted)
414
624
  project.write_lock(locked.values())
415
625
 
416
626
  def trust_project(self, project: state.Project) -> None:
627
+ with _package_lock():
628
+ self._trust_project(project)
629
+
630
+ def _trust_project(self, project: state.Project) -> None:
417
631
  user = self.user()
418
632
  if str(project.root) not in user.trusted_projects:
419
633
  user.trusted_projects.append(str(project.root))
420
634
  user.write()
421
635
 
422
636
  def remove(self, names, *, project: bool = False, log=print) -> list[str]:
423
- """Remove packages (from the user's list, or the project's); their stored
424
- versions go when nothing else uses them."""
637
+ """Remove packages from the user's list or the project, retaining shared cache files."""
638
+ with _package_lock():
639
+ result = self._remove(names, project=project, log=log)
640
+ self.changed()
641
+ return result
642
+
643
+ def _remove(self, names, *, project=False, log=print) -> list[str]:
425
644
  names = [str(name) for name in names]
426
645
  if project:
427
646
  target = self.project()
@@ -449,62 +668,87 @@ class Packages:
449
668
  for name in names:
450
669
  user.packages.pop(name)
451
670
  user.write()
452
- self.collect(log=log)
453
- self.changed()
454
671
  return names
455
672
 
456
- def collect(self, *, log=print) -> None:
457
- """Delete stored versions the user's list and this project's lock no longer use."""
458
- used = {Path(record.path).resolve() for record in self.user().packages.values() if record.path}
459
- project = self.project()
460
- if project is not None:
461
- used |= {state.store_path(n, r.version, r.sha256).resolve() for n, r in project.locked().items()}
462
- root = state.store_root()
463
- for folder in sorted(root.glob("*/*")) if root.is_dir() else []:
464
- if folder.is_dir() and folder.resolve() not in used:
465
- state.delete(folder)
466
- for folder in sorted(root.glob("*")) if root.is_dir() else []:
467
- if folder.is_dir() and not any(folder.iterdir()):
468
- folder.rmdir()
673
+ def collect(self, candidates=(), *, log=print) -> None:
674
+ """Retain immutable cache entries, which may serve other data directories.
675
+
676
+ HARNESS_HOME is independent of HARNESS_DATA. Neither this user list nor
677
+ its trusted project locks account for every owner of a shared store entry,
678
+ so absence from these records cannot justify deleting that entry.
679
+ """
469
680
 
470
681
  def update(self, names=(), *, project: bool = False, log=print) -> dict:
471
682
  """Update registry packages (all, or `names`) to the highest version in the range
472
683
  they were installed with that works with this harness."""
473
- records = self.project().locked() if project and self.project() else self.user().packages
684
+ with _package_lock():
685
+ result = self._update(names, project=project, log=log)
686
+ if result["installed"]:
687
+ self.changed()
688
+ return result
689
+
690
+ def _update(self, names=(), *, project=False, log=print, required=None) -> dict:
691
+ target = self.project() if project else None
692
+ if project and target is None:
693
+ raise ValueError("no harness.toml with [packages] here or above")
694
+ records = target.locked() if target is not None else self.user().packages
474
695
  names = [str(name) for name in names] or sorted(records)
475
696
  unknown = [name for name in names if name not in records]
476
697
  if unknown:
477
698
  raise ValueError(f"no installed package {', '.join(unknown)}")
478
- specs = []
479
- wanted = self.project().wanted() if project and self.project() else {}
699
+ requests = {}
700
+ wanted = target.wanted() if target is not None else {}
480
701
  for name in names:
481
702
  record = records[name]
482
703
  if not record.source.startswith("registry"):
483
704
  log(f"{name}: installed from {record.source}; install it again to update it")
484
705
  continue
485
- range_ = wanted.get(name, {}).get("range", "") if project else record.range
486
- latest = self.registry.pick(name, range_)
487
- if latest != record.version:
488
- specs.append(f"{name}@=={latest}")
489
- if not specs:
490
- log("everything is up to date")
706
+ requests[name] = wanted.get(name, {}).get("range", "") if project else record.range
707
+ if requests:
708
+ # An explicit update must see publications newer than this manager's
709
+ # cached index; search/list polling keeps its existing cache policy.
710
+ self.registry.index(refresh=True)
711
+ plan = resolve(requests, self.registry, harness=harness_version(),
712
+ have={name: record.version for name, record in records.items() if name not in requests},
713
+ depends_of=lambda name, version: self._installed_depends(records, name, version),
714
+ use_installed=lambda name, version: (name in records and records[name].version == version
715
+ and self._use_installed(name, records[name], requests)))
716
+ else:
717
+ plan = {}
718
+ if required is not None:
719
+ package, what, capability = required
720
+ selected = plan.get(package)
721
+ if selected is None or capability not in self.registry.entry(package, selected).get("provides", {}).get(what, []):
722
+ command = "harness install" + (" --project" if project else "")
723
+ raise MissingPackage(f"{package} cannot provide the {SINGULAR.get(what, what)} {capability} "
724
+ f"within its compatible recorded range {requests.get(package) or 'any'}; "
725
+ f"explicitly choose the intended version/range with {command} {package}@RANGE")
726
+ for name, version in plan.items():
727
+ self._runtime_fits(name, version)
728
+ if not any(name not in records or version != records[name].version
729
+ or not self._use_installed(name, records[name], requests)
730
+ or not (self._record_folder(name, records[name]) / "package.toml").is_file()
731
+ for name, version in plan.items()):
732
+ log("everything is up to date within its compatible version ranges")
491
733
  return {"installed": [], "problems": []}
492
- result = self.install(specs, project=project, log=log)
493
- if not project:
494
- user = self.user()
495
- for name in names:
496
- if name in user.packages and name in records:
497
- user.packages[name].range = records[name].range
498
- user.write()
499
- return result
734
+ # Reinstall the declared requests: resolution updates the lock, while the
735
+ # user's range and any source declarations stay intact in harness.toml.
736
+ with tempfile.TemporaryDirectory(prefix="harness-package-") as temporary:
737
+ return self._install({"target": target, "requests": requests,
738
+ "local": [], "problems": [], "temporary": Path(temporary)},
739
+ log=log, declare=False, automatic=required is not None)
500
740
 
501
741
  def enable(self, name: str, on: bool) -> None:
742
+ with _package_lock():
743
+ self._enable(name, on)
744
+ self.changed()
745
+
746
+ def _enable(self, name: str, on: bool) -> None:
502
747
  user = self.user()
503
748
  if name not in user.packages:
504
749
  raise ValueError(f"no installed package {name}")
505
750
  user.packages[name].enabled = bool(on)
506
751
  user.write()
507
- self.changed()
508
752
 
509
753
  # -- first use ---------------------------------------------------------------------
510
754
 
@@ -515,6 +759,14 @@ class Packages:
515
759
  package = self.provider(what, name)
516
760
  if package is not None:
517
761
  return package
762
+ # Reusing an owner's already installed capability is activation, not an
763
+ # installation decision: refresh it before off/ask or registry-source gates.
764
+ for ready in self.enabled():
765
+ if name in ready.provides.get(what, ()):
766
+ self.changed()
767
+ package = self.provider(what, name)
768
+ if package is not None:
769
+ return package
518
770
  singular = SINGULAR.get(what, what)
519
771
  if self.auto_install() == "off":
520
772
  raise MissingPackage(f"no installed package provides the {singular} {name}; "
@@ -527,6 +779,21 @@ class Packages:
527
779
  if not candidates:
528
780
  raise MissingPackage(f"no package provides the {singular} {name} (registry {self.registry.base})")
529
781
  choice = name if name in candidates else candidates[0]
782
+ installed = self.records().get(choice)
783
+ if installed is not None:
784
+ record, scope = installed
785
+ if not record.enabled:
786
+ raise MissingPackage(f"{choice} is disabled; enable it explicitly in Packages before retrying")
787
+ if not self._is_registry(record):
788
+ self.enabled() # Include the actual load failure when the development source is broken.
789
+ detail = self.problems.get(choice, f"it does not provide the {singular} {name}")
790
+ command = self._repair_command(choice, record, project=scope == "project")
791
+ raise MissingPackage(f"{choice} is installed from {record.source}: {detail}; repair that source "
792
+ f"or explicitly select the registry with {command}")
793
+ if not self._registry_matches(choice, record):
794
+ command = self._repair_command(choice, record, project=scope == "project")
795
+ raise MissingPackage(f"{choice} is installed from {record.source}; explicitly select the "
796
+ f"configured registry with {command} before retrying")
530
797
  mode = self.auto_install()
531
798
  if mode == "off":
532
799
  raise MissingPackage(f"the {singular} {name} is in the package {choice}: harness install {choice} "
@@ -534,8 +801,14 @@ class Packages:
534
801
  if mode == "ask" and not (ask and ask({"package": choice, "what": singular, "name": name,
535
802
  "registry": self.registry.base})):
536
803
  raise MissingPackage(f"the {singular} {name} needs the package {choice}, which was not installed")
537
- log(f"the {singular} {name} is in the package {choice}: installing it")
538
- self.install([choice], log=log)
804
+ if installed is not None:
805
+ log(f"the {singular} {name} is in {choice}: checking its compatible recorded range")
806
+ with _package_lock():
807
+ self._update([choice], project=scope == "project", log=log, required=(choice, what, name))
808
+ self.changed()
809
+ else:
810
+ log(f"the {singular} {name} is in the package {choice}: installing it")
811
+ self.install([choice], log=log, _automatic=True)
539
812
  package = self.provider(what, name)
540
813
  if package is None:
541
814
  raise MissingPackage(f"{choice} was installed but does not provide the {singular} {name}")
@@ -557,10 +830,14 @@ def activate(packages: Packages | None = None) -> Packages:
557
830
  prompts and modules); first, on the first start since packages, turn the app's
558
831
  installed skills into packages (migrate.py)."""
559
832
  from ..paths import PACKAGES_ENV, packages_changed
560
- from .migrate import migrate
833
+ from .migrate import ADDONS, migrate
561
834
 
562
835
  packages = packages or Packages()
563
- migrate(packages)
836
+ # Ordinary reads/startup must not wait for an unrelated long installation.
837
+ # migrate() checks again under the lock before publishing any legacy records.
838
+ if (packages.data / ADDONS).is_dir() and not packages.user().migrated:
839
+ with _package_lock():
840
+ migrate(packages)
564
841
  enabled = packages.enabled()
565
842
  os.environ[PACKAGES_ENV] = os.pathsep.join(str(package.path) for package in enabled)
566
843
  packages_changed()