@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
package/bin/harness.js CHANGED
@@ -66,6 +66,11 @@ child.on("error", (error) => {
66
66
  process.exit(1);
67
67
  });
68
68
  child.on("exit", (code, signal) => {
69
- if (signal) process.kill(process.pid, signal);
69
+ if (signal) {
70
+ // Restore the default disposition before replaying the child's signal. Otherwise
71
+ // our forwarding handler catches it again and Node exits with a success status.
72
+ process.removeAllListeners(signal);
73
+ process.kill(process.pid, signal);
74
+ }
70
75
  else process.exit(code === null ? 1 : code);
71
76
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lmmslab/harness",
3
- "version": "0.83.0",
3
+ "version": "0.84.0",
4
4
  "description": "Automatic Control for intelligence in action",
5
5
  "license": "MIT",
6
6
  "homepage": "https://harness.lmms-lab.com",
@@ -2,7 +2,7 @@
2
2
 
3
3
  # The one version: the core's. A lock records it with the versions of the packages that
4
4
  # ran and the hash of the whole lock (the agent's identity, config/agent.py).
5
- __version__ = "0.83.0"
5
+ __version__ = "0.84.0"
6
6
 
7
7
  # A checkout's packages import beside the harness (paths.py).
8
8
  from . import paths as _paths # noqa: E402,F401
@@ -45,22 +45,29 @@ its condition, to where it stands, from what is around it, to what it is doing.
45
45
  divides all of that. A node is a situation: its condition says when the agent is in it.
46
46
  The nodes under it divide the situation further, wherever a narrower one needs an answer
47
47
  of its own, and the last of them answers what is in the situation and in none of the
48
- others, with what fits the whole situation or by telling you. This holds at every depth,
49
- and how far a situation is divided follows from how much you know of it. So what you
50
- have never met is still answered, by the narrowest situation it falls in; and when you
51
- come to know it, you divide there, where its place among everything else is already
52
- settled. What happens to the agent whatever it is doing belongs in the tree, also when
53
- you met it inside one program: answered there, it is answered nowhere else. What you set
54
- out to do is not in it, however often you do it: a way of doing something that you have
55
- learned is kept as a skill you call, not as a node. A node has done its work when its
56
- situation is over: when its program ends and the situation is not over, a later node at
57
- its place has the environment, or you are told what the program did. The tree is
58
- consistent when in every situation it is settled which node has the environment: a
59
- node's place is what it costs when it does not act now, the costlier the earlier; and a
60
- node takes the environment only where you would, seeing everything as it is then. A
61
- reflex acts in your place, so its condition is your whole judgement, what it interrupts
62
- and whether this is the moment included, and what it does leaves things so that what it
63
- interrupted can go on.
48
+ others: with what fits the whole situation, or, where nothing does, by telling you. This
49
+ holds at every depth, and how far a situation is divided follows from how much you know
50
+ of it. So what you have never met is still answered, by the narrowest situation it falls
51
+ in; and when you come to know it, you divide there, where its place among everything
52
+ else is already settled. The unexpected has two forms, and the tree tells you of both.
53
+ Something happens that no node answers. Or what should happen does not: the agent goes
54
+ on acting and what the action is for does not come; the thing it should change stays as
55
+ it was while time and supplies go. The second is as much a situation of the agent as the
56
+ first, and it is the one that passes unnoticed, because nothing arrives. So for whatever
57
+ is set going, a node watches what it is for, and tells you when that stays away longer
58
+ than it would if things were as you assumed. Neither form may pass with no answer and
59
+ without your hearing of it. What happens to the agent whatever it is doing belongs in
60
+ the tree, also when you met it inside one program: answered there, it is answered
61
+ nowhere else. What you set out to do is not in it, however often you do it: a way of
62
+ doing something that you have learned is kept as a skill you call, not as a node. A node
63
+ has done its work when its situation is over: when its program ends and the situation is
64
+ not over, a later node at its place has the environment, or you are told what the
65
+ program did. The tree is consistent when in every situation it is settled which node has
66
+ the environment: a node's place is what it costs when it does not act now, the costlier
67
+ the earlier; and a node takes the environment only where you would, seeing everything as
68
+ it is then. A reflex acts in your place, so its condition is your whole judgement, what
69
+ it interrupts and whether this is the moment included, and what it does leaves things so
70
+ that what it interrupted can go on.
64
71
 
65
72
  Build each thing for the situations it has to work in, and think and test in proportion
66
73
  to what a failure costs: settle in the design what is cheap to settle; test what is
@@ -157,12 +164,14 @@ condition as the node above them or as a sense, an action as a function they cal
157
164
  do: a program (a function): started as a unit when the node's turn comes; what it is
158
165
  for is that the node's condition no longer holds. It begins again while the turn lasts,
159
166
  not before again_seconds after it ended; a program that answers an event gets the event
160
- when it takes an argument. Or the name of an event: you are
161
- told it, once each time the condition comes to hold. Or "stop": what runs is ended.
167
+ when it takes an argument. Or the name of an event: you are told it, once each time
168
+ its turn comes, which is when its condition holds and no node before it at its place
169
+ is answering. Or "stop": what runs is ended.
162
170
  Or nothing: the node is a group, and the nodes set with under=name are its content.
163
- priority (a whole number, 0 or more): the node's place among those where it is, the
164
- higher first, among equals the older. A node's program takes the environment from your
165
- programs.
171
+ priority (a whole number, 0 or more): the node's own, wherever it is in the tree. Of
172
+ two nodes whose turn it is the higher has the environment, among equals the earlier in
173
+ the tree; and it is the number a program's at_priority(n) is compared with. A node's
174
+ program takes the environment from your programs.
166
175
  every: the seconds between judgements of its condition; without it the condition is
167
176
  judged when the state or the facts change.
168
177
  A condition is read, not computed: the conditions judged in one moment share a few
@@ -207,16 +216,22 @@ has its call started again by itself then (its result counts it: .restarts).
207
216
  <!-- end -->
208
217
  What you start meanwhile waits for the higher unit to end. Units of equal priority take
209
218
  turns.
210
- Your programs have priority 0, so every reflex takes the environment from them. A
211
- program has its place among the reflexes like a node: at_priority(n) gives it one, from
212
- that call to its end. A reflex at the top of the tree with priority n or more, and what is
213
- under it, still takes the environment from the program, which goes on as said above. A
214
- reflex below n waits: it is judged again when the program has ended, and you are told
215
- once that it was held back (the event HELD_BACK); cancelling the program lets it act. So a
216
- program that does itself what some reflexes answer stands above those and below the rest:
217
- what happens to the agent and is not the program's business still has its reflex.
219
+ Your programs have priority 0, so every reflex takes the environment from them. An
220
+ action of a program that must not be broken off takes a place among the reflexes for as
221
+ long as it lasts: `with at_priority(n):` around that action, and no longer. During the
222
+ block a reflex whose own priority is n or more still takes the environment from the
223
+ program, which goes on as said above; a reflex below n waits: it is judged again when the
224
+ block ends, and you are told once that it was held back (the event HELD_BACK). The place is the action's, not the program's or a stage's:
225
+ while it is held, nothing below it answers what happens to the agent, so a program that
226
+ holds one for its whole length has taken the tree's work on itself and must do all of it.
227
+ Code that runs for long in the environment itself asks for a place the same way, where
228
+ the environment's page says how.
218
229
  Conditions and controllers read the supplied view. Keep them fast and free of file,
219
- network, process and input operations.
230
+ network, process and input operations. Beside the state as it is, the view holds what a
231
+ state alone does not show: view["was"][key], what that part of the state was before it
232
+ last changed; view["unchanged"][key], for how many seconds it has not changed (the senses
233
+ under ["sensed"] in both); and view["units"], the units that run, each {"name", "kind",
234
+ "reflex", "priority", "seconds"}.
220
235
  <!-- if: memory -->
221
236
  <!-- if: legacy_memory -->
222
237
  - recall(query): what earlier runs kept about a situation like this one.
@@ -1,6 +1,6 @@
1
1
  This run is practice for a task, not the task itself: {task}
2
2
 
3
- You have {minutes} minutes of live time; what your library holds and what the environment keeps for you when it is over is what the runs that do the task start with.
3
+ What your library holds and what the environment keeps for you when this run is over is what the runs that do the task start with.
4
4
 
5
5
  Something is learned when it has done its work without help, more than once, starting from what the parts before it really leave. Where the environment lets you set that starting situation up directly, do so instead of working your way to it each time. What was practised from a better start than the task gives it has shown nothing yet.
6
6
  {probe}
@@ -266,7 +266,7 @@ def _continue_arguments(command, name: str):
266
266
  """The flags that go on with an earlier session in its own id instead of starting one."""
267
267
  which = command.add_mutually_exclusive_group()
268
268
  which.add_argument("--continue", dest="continue_last", action="store_true",
269
- help="go on with the most recent session: its messages and facts, in the same session id")
269
+ help="go on with the session you last messaged or resumed, in the same session id")
270
270
  which.add_argument(name, dest="session", metavar="SESSION",
271
271
  help="go on with that session (its id, as harness sessions ls --all lists it)")
272
272
 
@@ -286,6 +286,17 @@ def _continued(args):
286
286
  return host, conversation, max((int(record.get("seq", -1)) for record in records), default=-1)
287
287
 
288
288
 
289
+ def _check_continuation_options(args):
290
+ """A continuation uses its saved settings; reject new-task options before any host call."""
291
+ if not (getattr(args, "continue_last", False) or getattr(args, "session", None)):
292
+ return
293
+ selection = any(getattr(args, key, None) is not None for key in ("model", "effort", "workspace", "vm_image", "library"))
294
+ if selection or _coding(args) or _run_options(args) or getattr(args, "learn_while_working", False):
295
+ raise UsageError("continuation keeps the saved model, workspace and run settings; "
296
+ "omit --model/--effort and other new-conversation options with --continue/--resume/--session. "
297
+ "In an open terminal, /effort LEVEL changes that conversation; /model selects only new ones")
298
+
299
+
289
300
  def parser(packages: bool = True):
290
301
  root = argparse.ArgumentParser(prog="harness", description="Automatic Control for intelligence in action.",
291
302
  epilog="harness with no command: the terminal chat with the agent on this computer "
@@ -293,6 +304,8 @@ def parser(packages: bool = True):
293
304
  "packages, library)")
294
305
  root.add_argument("--version", action="version", version=f"harness {__version__}")
295
306
  _continue_arguments(root, "--resume")
307
+ root.add_argument("--model", help="an enabled model from ~/.harness/models.json (ID or provider/model)")
308
+ root.add_argument("--effort", help="the selected model's reasoning effort ID")
296
309
  run_flags(root)
297
310
  _coding_arguments(root)
298
311
  root.add_argument("--managed-workspace", action="store_true", help="use an isolated managed workspace (supported Linux hosts)")
@@ -322,7 +335,7 @@ def parser(packages: bool = True):
322
335
  _session_commands(commands.add_parser("sessions", help="Manage the session host's coding sessions"))
323
336
  install = commands.add_parser("install", help="Install packages (and what they depend on), or update them; and "
324
337
  "the services and benches they provide",
325
- epilog="harness install browser; harness install computer@^0.5; harness install "
338
+ epilog="harness install browser; harness install computer@^0.8; harness install "
326
339
  "git:github.com/x/y@main#packages/z; harness install ./my-package --link; "
327
340
  "harness install --update (all); harness install sam3 (a service). With no "
328
341
  "names in a folder with harness.toml: the project's packages, from its "
@@ -336,6 +349,8 @@ def parser(packages: bool = True):
336
349
  "without asking (they run code on this computer)")
337
350
  install.add_argument("--link", action="store_true", help="a folder: use it where it is instead of a copy (a "
338
351
  "package under development)")
352
+ install.add_argument("--tui", action="store_true", help="prepare the terminal interface's pinned libraries, "
353
+ "without starting a conversation")
339
354
  install.add_argument("--deploy", help="services: the deploy that places them (default $HARNESS_DEPLOY)")
340
355
  listed = commands.add_parser("list", help="The installed packages; --available: the registry's (QUERY "
341
356
  "searches them); one package by its name: what it provides")
@@ -439,10 +454,14 @@ def _core_report() -> dict:
439
454
  problems, notes = [], []
440
455
  if sys.version_info < (3, 11):
441
456
  problems.append(f"Python {sys.version.split()[0]}: 3.11 or newer is needed")
442
- model = providers.ready(data())
443
- if not model["ready"]:
444
- problems.append(f"no model set up ({model['provider']}: {model['key_env']} is not set): run harness once "
445
- "in a terminal, or export the key")
457
+ try:
458
+ model = providers.ready(data())
459
+ if not model["ready"]:
460
+ problems.append(model["detail"] if model.get("selection_error") else
461
+ f"no model set up ({model['provider']}: {model['detail']}): run harness once "
462
+ "in a terminal, or configure the selected model")
463
+ except (ValueError, OSError) as error:
464
+ problems.append(str(error))
446
465
  notes.append("No package places services here: hosted models only (harness install local-models for "
447
466
  "models on this machine's GPUs).")
448
467
  return {"schema_version": 1, "deploy": "none", "ok": not problems, "problems": problems, "notes": notes,
@@ -506,6 +525,9 @@ def _install(args):
506
525
  from .packages import Packages, activate, provision
507
526
  from .services.placement import load_deploy
508
527
 
528
+ terminal = getattr(args, "tui", False)
529
+ if terminal and args.update:
530
+ raise UsageError("--tui prepares the terminal; run it separately from --update")
509
531
  packages = activate(Packages())
510
532
  if args.update:
511
533
  result = packages.update(args.names, project=args.project, log=_note)
@@ -515,7 +537,7 @@ def _install(args):
515
537
  known = set(deploy.services) | set(bench_specs())
516
538
  specs = [name for name in args.names if name not in known]
517
539
  result = {"installed": [], "problems": []}
518
- if specs or (not args.names and args.project) or (not args.names and packages.project() is not None):
540
+ if specs or (not args.names and args.project) or (not args.names and not terminal and packages.project() is not None):
519
541
  def consent(found):
520
542
  for package in found:
521
543
  _print_package(package, file=sys.stderr)
@@ -532,7 +554,7 @@ def _install(args):
532
554
  result = packages.install(specs, project=project, consent=consent, link=args.link, log=_note)
533
555
  if result.get("needs_trust"):
534
556
  return 1
535
- elif not args.names:
557
+ elif not args.names and not terminal:
536
558
  return _usage("install", "name packages, services or benches (or run it where harness.toml names the "
537
559
  "project's packages)")
538
560
  # A name a package just brought (a bench) is installed as one now too: its environment.
@@ -541,6 +563,14 @@ def _install(args):
541
563
  benches = [name for name in provisioned if name in bench_specs()]
542
564
  services = [name for name in provisioned if name not in benches]
543
565
  rows = [provision.install_bench_environment(deploy, name, log=_note) for name in benches]
566
+ if terminal:
567
+ from .tui import environment
568
+ from .packages import environments
569
+
570
+ prepared = environment()
571
+ environments.install(prepared, log=_note)
572
+ rows.append({"name": "tui", "what": "frontend", "installed": True,
573
+ "environment": str(prepared.directory)})
544
574
  if services:
545
575
  # Services' environments and weights: the package that places services installs them.
546
576
  from .extensions import require
@@ -594,13 +624,19 @@ def _note(message):
594
624
  print(message, file=sys.stderr, flush=True)
595
625
 
596
626
 
597
- def _ready_model(interactive: bool) -> None:
627
+ def _ready_model(interactive: bool, *, recovery: bool = False) -> str | None:
598
628
  """A model and its key, asked for on the first run in a terminal (providers.setup)."""
599
- from . import model_list, providers
629
+ from . import providers
600
630
  from .paths import data
601
631
 
602
- if model_list.select(data()) is not None or providers.ready(data())["ready"]:
632
+ model = providers.ready(data())
633
+ if model["ready"]:
603
634
  return
635
+ if model.get("selection_error") or ((data() / "models.json").exists() and
636
+ model["detail"].startswith("add an enabled model")):
637
+ if recovery:
638
+ return f"{model['detail']}; use /model to choose an enabled model or /models to edit the list"
639
+ raise UsageError(model["detail"])
604
640
  if not (interactive and sys.stdin.isatty()):
605
641
  raise UsageError("no model is set up yet: run harness in a terminal once (it asks for the model and key), "
606
642
  "or export the provider's key")
@@ -667,6 +703,12 @@ def _starter(args):
667
703
  its environment for that many minutes. It keeps the last it started."""
668
704
  def start(text, files=(), log=None, learn=None, model_entry=None, reasoning_effort=None, workspace=None,
669
705
  folder=None, agents=None):
706
+ # Effort belongs to the startup route. A route picked in the TUI with no
707
+ # effort explicitly asks for that route's default, not the CLI route's effort.
708
+ if model_entry is None and start.model_entry is not None:
709
+ model_entry = start.model_entry
710
+ if reasoning_effort is None:
711
+ reasoning_effort = start.model_effort
670
712
  runtime = {**_run_options(args), **({"learn": learn} if learn is not None else {}),
671
713
  **({"agents": True} if agents else {})}
672
714
  coding = _coding(args)
@@ -696,6 +738,23 @@ def _starter(args):
696
738
  return start.host, start.conversation
697
739
 
698
740
  start.host = start.conversation = None
741
+ start.model_entry = start.model_effort = start.model_name = None
742
+ if not _coding(args).get("agent") and not (
743
+ getattr(args, "continue_last", False) or getattr(args, "session", None)) and (
744
+ getattr(args, "model", None) is not None or getattr(args, "effort", None) is not None):
745
+ from .model_list import SelectionError, select
746
+ from .paths import data
747
+
748
+ try:
749
+ initial = select(data(), getattr(args, "model", None))
750
+ except SelectionError:
751
+ # An effort-only flag must not close the UI needed to repair a default.
752
+ if getattr(args, "model", None) is not None:
753
+ raise
754
+ initial = None
755
+ if initial is not None:
756
+ start.model_entry, start.model_effort = initial["id"], getattr(args, "effort", None)
757
+ start.model_name = initial["name"]
699
758
  # The command line already says where it works: the chat does not ask at its opening.
700
759
  start.named = bool(getattr(args, "workspace", None) or getattr(args, "folder", None)
701
760
  or getattr(args, "agent", None))
@@ -777,9 +836,11 @@ def _run_task(args):
777
836
  from .terminal import Printer
778
837
  from .transcript import answer_of
779
838
 
839
+ _check_continuation_options(args)
780
840
  if args.model and _coding(args).get("agent"):
781
841
  raise UsageError("--model selects its own provider; use either --model or --agent")
782
- if not _coding(args).get("agent") and not args.model:
842
+ if not _coding(args).get("agent") and not args.model and not (
843
+ getattr(args, "continue_last", False) or getattr(args, "session", None)):
783
844
  _ready_model(interactive=True)
784
845
  from .settings import ChoiceRequired
785
846
 
@@ -924,6 +985,9 @@ def _rpc(args):
924
985
  it ends when stdin closes, after end, or when the conversation ends."""
925
986
  from .terminal import COMMANDS, command as run_command
926
987
 
988
+ if getattr(args, "continue_last", False) or getattr(args, "session", None):
989
+ raise UsageError("run rpc starts a new conversation; use harness run --continue or --session ID "
990
+ "for an existing conversation")
927
991
  if args.model and _coding(args).get("agent"):
928
992
  raise UsageError("--model selects its own provider; use either --model or --agent")
929
993
  if not _coding(args).get("agent") and not args.model:
@@ -1028,17 +1092,26 @@ def _rpc(args):
1028
1092
  def _default(args):
1029
1093
  """harness with no command: the terminal chat with the agent on this computer, which
1030
1094
  starts a conversation with the first message; the first run asks for the model and key."""
1031
- if not _coding(args).get("agent"):
1032
- _ready_model(interactive=True)
1095
+ _check_continuation_options(args)
1096
+ selecting = getattr(args, "model", None) is not None or getattr(args, "effort", None) is not None
1097
+ if selecting and _coding(args).get("agent"):
1098
+ raise UsageError("the terminal's --agent cannot be combined with --model or --effort; "
1099
+ "use harness run --agent NAME --effort LEVEL for an explicit agent effort")
1033
1100
  if not (sys.stdin.isatty() and sys.stdout.isatty()):
1034
1101
  raise UsageError("the terminal chat needs a terminal: harness run \"task\", or harness run rpc")
1035
1102
  from . import tui
1036
1103
 
1037
1104
  continued = _continued(args)
1105
+ start = _starter(args)
1106
+ start.warning = None
1107
+ if continued is None and not _coding(args).get("agent") and not getattr(args, "model", None):
1108
+ start.warning = _ready_model(interactive=True, recovery=True)
1109
+ if getattr(args, "effort", None) is not None and start.model_entry is None:
1110
+ start.warning = (start.warning or "") + " --effort was not applied; choose a model, then use /effort LEVEL."
1038
1111
  if continued is not None:
1039
- tui.chat(continued[1], host=continued[0], start=_starter(args))
1112
+ tui.chat(continued[1], host=continued[0], start=start)
1040
1113
  else:
1041
- tui.chat(start=_starter(args))
1114
+ tui.chat(start=start)
1042
1115
  return 0
1043
1116
 
1044
1117
 
@@ -1207,6 +1280,11 @@ def _library_command(args):
1207
1280
 
1208
1281
 
1209
1282
  def main(argv=None):
1283
+ """Catch setup and activation failures as well as command-dispatch failures."""
1284
+ return _guarded(lambda: _main(argv))
1285
+
1286
+
1287
+ def _main(argv=None):
1210
1288
  words = list(sys.argv[1:] if argv is None else argv)
1211
1289
  if words[:1] == ["rpc"]:
1212
1290
  words.insert(0, "run")
@@ -1228,13 +1306,21 @@ def main(argv=None):
1228
1306
  return _guarded(lambda: _package_command(words[0], words[1:]))
1229
1307
  command_parser = parser()
1230
1308
  args, rest = command_parser.parse_known_args(words)
1309
+ if args.command is not None and words and words[0].startswith("-"):
1310
+ raise UsageError("put options after the command, for example: harness run \"task\" --model MODEL")
1231
1311
  if rest and args.command != "doctor":
1232
1312
  command_parser.parse_args(words) # says what it does not know, exit status 2
1233
1313
  if args.command not in (None, "install", "remove", "list"):
1234
1314
  # The enabled packages' skills load here and in the kernels it starts.
1235
1315
  from .packages import activate
1236
1316
 
1237
- activate()
1317
+ try:
1318
+ activate()
1319
+ except (ValueError, OSError, RuntimeError, ImportError) as error:
1320
+ if args.command == "doctor" and args.json:
1321
+ output({"schema_version": 1, "ok": False, "problems": [str(error)], "error": str(error)})
1322
+ return 1
1323
+ raise
1238
1324
  commands = {"install": _install, "list": _list, "remove": _remove, "library": _library_command, "sessions": _sessions,
1239
1325
  "replay": _replay}
1240
1326
  if args.command == "run":
@@ -66,11 +66,31 @@ class Trace:
66
66
  def __init__(self, directory: Path, kinds: dict | None = None, *, append: bool = False):
67
67
  path = directory / "events.jsonl"
68
68
  if append:
69
- from .episodes import read_records
70
-
71
69
  if not path.is_file():
72
70
  raise ValueError("the existing session trace is missing")
73
- sequence = max((record["seq"] for record in read_records(directory)), default=-1) + 1
71
+ # The caller has reclaimed the ended conversation before opening its writer.
72
+ # Validate all complete events before repairing only the interrupted last line.
73
+ sequence = 0
74
+ with path.open("r+b") as handle:
75
+ while True:
76
+ offset = handle.tell()
77
+ line = handle.readline()
78
+ if not line:
79
+ break
80
+ if line.endswith(b"\n"):
81
+ record = json.loads(line)
82
+ else:
83
+ try:
84
+ record = json.loads(line)
85
+ except (ValueError, UnicodeError):
86
+ handle.truncate(offset)
87
+ break
88
+ # A complete JSON event without its newline is still history.
89
+ sequence = max(sequence, record["seq"] + 1)
90
+ handle.seek(0, 2)
91
+ handle.write(b"\n")
92
+ break
93
+ sequence = max(sequence, record["seq"] + 1)
74
94
  else:
75
95
  directory.mkdir(parents=True, exist_ok=False)
76
96
  path.touch()
@@ -42,6 +42,12 @@ from .ports import OwnPort
42
42
  # `by` names who takes the environment (a reflex, or a unit), for what is held to know;
43
43
  # resume(episode_id, held) is given that value and lets it go on. The run then pauses a
44
44
  # call for a unit of higher priority instead of ending it (runtime/tick.py).
45
+ #
46
+ # Priority (optional). Code that runs in the environment for long (a routine the agent saved
47
+ # there) may hold the environment for one action of it, as a program does with
48
+ # `with at_priority(n):`. The worker says so with its state: "priority", the priority that
49
+ # code asks for now (a number above 0; absent or None: none). The unit whose call runs holds
50
+ # the environment with it for as long as the state says so (runtime/tick.py _asked).
45
51
  WORKER_METHODS = ("profile", "state", "tick", "observe", "claim_outcome", "outcome", "tell_to")
46
52
 
47
53
 
@@ -8,6 +8,7 @@ tunnel to the server's port. It is also how every frontend drives a session of t
8
8
  session host (host/): `run` is then the session's id, or "<session>/<agent>".
9
9
  """
10
10
 
11
+ from http.client import IncompleteRead
11
12
  import json
12
13
  import time
13
14
  from urllib.error import HTTPError
@@ -112,6 +113,10 @@ class RemoteConversation:
112
113
  """Send terminal input to a session started with an interactive PTY."""
113
114
  self._post("terminal", text=text)
114
115
 
116
+ def terminal_resize(self, columns: int, rows: int) -> None:
117
+ """Match a hosted PTY to the attached terminal's character dimensions."""
118
+ self._post("terminal/resize", columns=columns, rows=rows)
119
+
115
120
  def handoff(self, agent: str, context: str, *, folder=None, isolation=None):
116
121
  """Start another agent with the context chosen by the user."""
117
122
  answer = self._post("handoff", agent=agent, context=context, folder=str(folder) if folder else None,
@@ -154,22 +159,24 @@ class RemoteConversation:
154
159
  turn's end is its turn_result record, after which the conversation waits). With
155
160
  `stop` (a threading.Event), it ends once the event is set, within the server's
156
161
  keep-alive interval. When the server stops answering, it tries again, waiting longer
157
- each time, and raises HostGone after GONE_SECONDS without a connection."""
162
+ each time, and raises HostGone after GONE_SECONDS of failures without records or keep-alives."""
158
163
  failing, delay = None, RETRY_SECONDS
159
164
  while True:
160
165
  if stop is not None and stop.is_set():
161
166
  return
162
- ended = False
167
+ reason = "the records stream ended before its end event"
163
168
  try:
164
169
  with open_url(f"{self.url}/api/stream?run={quote(self.run)}", timeout=self.timeout,
165
170
  headers={"X-Session-API-Key": self.token, "Last-Event-ID": str(after)}) as response:
166
- failing, delay = None, RETRY_SECONDS
167
171
  for event, data in read_sse(response):
168
172
  if stop is not None and stop.is_set():
169
173
  return
174
+ # Headers alone do not prove a working stream. Records and
175
+ # keep-alive comments do, including on an otherwise quiet turn.
176
+ if event in (None, "record", "end"):
177
+ failing, delay = None, RETRY_SECONDS
170
178
  if event == "end":
171
- ended = True
172
- break
179
+ return
173
180
  if event == "record":
174
181
  record = json.loads(data)
175
182
  after = record["seq"]
@@ -178,23 +185,21 @@ class RemoteConversation:
178
185
  yield record
179
186
  except HTTPError as error:
180
187
  raise RemoteError(_reason(error)) from None
181
- except TimeoutError: # a quiet stream: open it again
182
- continue
183
- except OSError as error: # refused, reset: the server may be restarting, or gone
184
- now = time.monotonic()
185
- failing = failing or now
186
- if now - failing >= GONE_SECONDS:
187
- raise HostGone(f"the session host at {self.url} does not answer "
188
- f"({getattr(error, 'reason', None) or error})") from None
189
- if stop is not None:
190
- if stop.wait(delay):
191
- return
192
- else:
193
- time.sleep(delay)
194
- delay = min(delay * 2, RETRY_LIMIT)
195
- continue
196
- if ended:
188
+ except (OSError, IncompleteRead) as error: # Timeouts, truncated bodies and connection failures.
189
+ reason = getattr(error, "reason", None) or error
190
+ if stop is not None and stop.is_set():
197
191
  return
192
+ now = time.monotonic()
193
+ if failing is None:
194
+ failing = now
195
+ if now - failing >= GONE_SECONDS:
196
+ raise HostGone(f"the session host at {self.url} does not answer ({reason})") from None
197
+ if stop is not None:
198
+ if stop.wait(delay):
199
+ return
200
+ else:
201
+ time.sleep(delay)
202
+ delay = min(delay * 2, RETRY_LIMIT)
198
203
 
199
204
 
200
205
  def _reason(error: HTTPError) -> str:
@@ -480,6 +480,7 @@ class Handler(BaseHTTPRequestHandler):
480
480
 
481
481
  def post_conversations_message(self, body):
482
482
  self._conversation(body).send_message(str(body.get("text") or ""), files=body.get("files") or ())
483
+ self.server.conversations.interact(self.server.conversations.resolve(body.get("run")))
483
484
  self._json({"ok": True})
484
485
 
485
486
  def _control(self, body, capability, method, *args):
@@ -490,6 +491,7 @@ class Handler(BaseHTTPRequestHandler):
490
491
 
491
492
  def post_conversations_steer(self, body):
492
493
  self._control(body, "steer", "steer", str(body.get("text") or ""), body.get("files") or ())
494
+ self.server.conversations.interact(self.server.conversations.resolve(body.get("run")))
493
495
  self._json({"ok": True})
494
496
 
495
497
  def post_conversations_cancel(self, body):
@@ -516,12 +518,17 @@ class Handler(BaseHTTPRequestHandler):
516
518
  self._control(body, "terminal_input", "terminal_input", body.get("text"))
517
519
  self._json({"ok": True})
518
520
 
521
+ def post_conversations_terminal_resize(self, body):
522
+ self._control(body, "terminal_resize", "terminal_resize", body.get("columns"), body.get("rows"))
523
+ self._json({"ok": True})
524
+
519
525
  def post_conversations_pause(self, body):
520
526
  self._conversation(body).pause()
521
527
  self._json({"ok": True})
522
528
 
523
529
  def post_conversations_resume(self, body):
524
530
  self._conversation(body).resume()
531
+ self.server.conversations.interact(self.server.conversations.resolve(body.get("run")))
525
532
  self._json({"ok": True})
526
533
 
527
534
  def post_conversations_confirm(self, body):
@@ -584,11 +591,14 @@ class Handler(BaseHTTPRequestHandler):
584
591
  row = self._session((query.get("id") or [""])[0])
585
592
  lines = int((query.get("lines") or ["40"])[0])
586
593
  for agent in row["agents"]:
594
+ agent["lines"] = []
595
+ if lines <= 0:
596
+ continue
587
597
  directory = Path(agent["directory"]) if agent["directory"] else None
588
598
  records = runs.read_records(directory) if directory and (directory / "events.jsonl").is_file() else []
589
599
  printer = TextTranscript()
590
600
  text = [line for record in records for line in printer.lines(record)]
591
- agent["lines"] = text[-lines:] if lines > 0 else []
601
+ agent["lines"] = text[-lines:]
592
602
  self._json(row)
593
603
 
594
604
  def post_sessions_stop(self, body):