harness-sdk-python 0.40.11__tar.gz → 0.40.13__tar.gz
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.
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/PKG-INFO +3 -3
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/pyproject.toml +2 -2
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/deepagents.py +7 -2
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/interest.py +11 -5
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/messages.py +27 -2
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/run_manager.py +79 -12
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/threads.py +3 -7
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/run_helpers.py +4 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_deepagents_thread.py +15 -2
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_interest.py +75 -2
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_prioritize.py +54 -13
- harness_sdk_python-0.40.13/tests/test_stale_redelivery.py +183 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/.gitignore +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/README.md +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/examples/__init__.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/examples/local_origin.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/examples/runs_app.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/__init__.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/checkpoints.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/deepagents_host.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/fenced_postgres.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/langgraph.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/linear_thread.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/parts.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/prioritize.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/protocol.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/providers/__init__.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/providers/gpt_live.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/voice.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/voice_thread.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/wire.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_admit_steer.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_applied_visibility.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_batches.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_branch_anchor.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_checkpoints.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_deepagents_approval.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_deepagents_files.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_deepagents_multi_approval.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_deepagents_outcome.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_deepagents_subagent_approval.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_deepagents_user_ids.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_deepagents_voice.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_dispatching.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_durability.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_edit_dispatched.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_edit_reload.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_enqueue.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_example_local_origin.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_fenced_postgres.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_gpt_live.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_imports.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_input_required.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_langgraph.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_linear_thread.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_meta.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_mint_concurrency.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_outcomes.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_parts.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_placement.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_prepare_hooks.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_prioritize_builtins.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_request_options.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_restore.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_rewind_during_run.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_run_admission.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_run_crash_sweep.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_run_eviction.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_run_id_bounds.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_run_ownership.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_run_sync.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_settle.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_steer.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_stop_continue.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_stopping_window.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_threads.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_undo.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_voice.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_voice_delegation.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_voice_events.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_wire.py +0 -0
- {harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/voice_helpers.py +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: harness-sdk-python
|
|
3
|
-
Version: 0.40.
|
|
3
|
+
Version: 0.40.13
|
|
4
4
|
Summary: RunManager: the harness-sdk runs subsystem for Python Statewire hosts
|
|
5
5
|
Project-URL: Repository, https://github.com/assistant-ui/harness-sdk
|
|
6
6
|
License-Expression: MIT
|
|
7
7
|
Requires-Python: <4.0,>=3.12
|
|
8
|
-
Requires-Dist: pinboard-sdk>=0.18.
|
|
9
|
-
Requires-Dist: statewire<0.
|
|
8
|
+
Requires-Dist: pinboard-sdk>=0.18.4
|
|
9
|
+
Requires-Dist: statewire<0.27,>=0.26.0
|
|
10
10
|
Provides-Extra: deepagents
|
|
11
11
|
Requires-Dist: deepagents>=0.6.12; extra == 'deepagents'
|
|
12
12
|
Requires-Dist: langchain-core>=0.3; extra == 'deepagents'
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "harness-sdk-python"
|
|
3
|
-
version = "0.40.
|
|
3
|
+
version = "0.40.13"
|
|
4
4
|
description = "RunManager: the harness-sdk runs subsystem for Python Statewire hosts"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "MIT"
|
|
7
7
|
requires-python = ">=3.12,<4.0"
|
|
8
|
-
dependencies = ["statewire>=0.
|
|
8
|
+
dependencies = ["statewire>=0.26.0,<0.27", "pinboard-sdk>=0.18.4"]
|
|
9
9
|
|
|
10
10
|
[project.optional-dependencies]
|
|
11
11
|
postgres = ["langgraph-checkpoint-postgres>=2.0.0"]
|
|
@@ -19,7 +19,7 @@ from .checkpoints import (
|
|
|
19
19
|
langgraph_checkpoints,
|
|
20
20
|
langgraph_snapshot,
|
|
21
21
|
)
|
|
22
|
-
from .messages import MAIN_NS, MessageTree
|
|
22
|
+
from .messages import MAIN_NS, MessageTree, iso
|
|
23
23
|
from .parts import PartsAssembler, text_of
|
|
24
24
|
from .voice_thread import upsert_voice_turn
|
|
25
25
|
|
|
@@ -85,6 +85,11 @@ def _checkpoint_ns(ns: str) -> str:
|
|
|
85
85
|
return "" if ns == MAIN_NS else ns
|
|
86
86
|
|
|
87
87
|
|
|
88
|
+
def _row_value(message: dict[str, Any]) -> dict[str, Any]:
|
|
89
|
+
value = plain(message)
|
|
90
|
+
return {**value, "createdAt": iso(value["createdAt"])}
|
|
91
|
+
|
|
92
|
+
|
|
88
93
|
def _root_of(ns: str) -> str:
|
|
89
94
|
return ns.split("|", 1)[0]
|
|
90
95
|
|
|
@@ -509,7 +514,7 @@ class DeepagentsThread:
|
|
|
509
514
|
"message_id": message_id,
|
|
510
515
|
"parent_id": self.tree.get(ns, message_id)["parentId"],
|
|
511
516
|
"seq": self.tree.get(ns, message_id)["seq"],
|
|
512
|
-
"value":
|
|
517
|
+
"value": _row_value(self.tree.get(ns, message_id)),
|
|
513
518
|
}
|
|
514
519
|
for ns, message_id in sorted(touched, key=lambda t: self.tree.get(*t)["seq"])
|
|
515
520
|
]
|
|
@@ -89,11 +89,17 @@ def _window(thread: Thread, anchor: str | None, before: int, after: int) -> dict
|
|
|
89
89
|
|
|
90
90
|
def spawned_ns(message: Any) -> list[str]:
|
|
91
91
|
"""Subagent namespaces named by a message's tool parts."""
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if part.get("type")
|
|
96
|
-
|
|
92
|
+
out: list[str] = []
|
|
93
|
+
for part in plain(message)["parts"]:
|
|
94
|
+
metadata = part.get("metadata")
|
|
95
|
+
if part.get("type") != "tool" or not isinstance(metadata, dict):
|
|
96
|
+
continue
|
|
97
|
+
namespaces = metadata.get("namespaces")
|
|
98
|
+
for ns in [metadata.get("ns"), *(namespaces if isinstance(namespaces, list) else [])]:
|
|
99
|
+
# statewire refuses `__proto__` as a document key, so it names nothing to follow
|
|
100
|
+
if isinstance(ns, str) and ns and ns != "__proto__":
|
|
101
|
+
out.append(ns)
|
|
102
|
+
return out
|
|
97
103
|
|
|
98
104
|
|
|
99
105
|
def resolve(tree: MessageTree, request: dict[str, Any]) -> dict[str, Any]:
|
|
@@ -4,6 +4,7 @@ streaming writes flush as ops.
|
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
from dataclasses import dataclass, field
|
|
7
|
+
from datetime import datetime, timezone
|
|
7
8
|
from typing import Any, Iterator
|
|
8
9
|
|
|
9
10
|
MAIN_NS = "main"
|
|
@@ -11,6 +12,23 @@ MAIN_NS = "main"
|
|
|
11
12
|
_ROLES = ("user", "assistant")
|
|
12
13
|
|
|
13
14
|
|
|
15
|
+
def iso(at: datetime) -> str:
|
|
16
|
+
return at.astimezone(timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def iso_now() -> str:
|
|
20
|
+
return iso(datetime.now(timezone.utc))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _check_created_at(value: Any) -> None:
|
|
24
|
+
try:
|
|
25
|
+
canonical = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ").isoformat(timespec="milliseconds") + "Z" == value
|
|
26
|
+
except (TypeError, ValueError):
|
|
27
|
+
canonical = False
|
|
28
|
+
if not canonical:
|
|
29
|
+
raise ValueError(f"createdAt must be ISO-8601 UTC with milliseconds (2026-10-14T09:00:00.000Z): {value!r}")
|
|
30
|
+
|
|
31
|
+
|
|
14
32
|
def _check_message(message: dict[str, Any]) -> None:
|
|
15
33
|
if not isinstance(message, dict):
|
|
16
34
|
raise TypeError(f"message must be an object, got {message!r}")
|
|
@@ -89,7 +107,7 @@ class MessageTree:
|
|
|
89
107
|
}
|
|
90
108
|
|
|
91
109
|
def add(self, ns: str, message: dict[str, Any], *, head: bool = True) -> dict[str, Any]:
|
|
92
|
-
"""Add a message; ``seq``
|
|
110
|
+
"""Add a message; ``seq`` and ``createdAt`` are assigned unless present; the head moves to it by default."""
|
|
93
111
|
_check_message(message)
|
|
94
112
|
thread = self.thread(ns)
|
|
95
113
|
if message["id"] in thread.messages:
|
|
@@ -102,8 +120,15 @@ class MessageTree:
|
|
|
102
120
|
seq = thread.next_seq
|
|
103
121
|
elif not isinstance(seq, int) or isinstance(seq, bool) or seq < 1:
|
|
104
122
|
raise ValueError(f"seq must be a positive integer: {seq!r}")
|
|
123
|
+
created_at = message["createdAt"] if "createdAt" in message else datetime.now(timezone.utc)
|
|
124
|
+
if isinstance(created_at, str):
|
|
125
|
+
_check_created_at(created_at)
|
|
126
|
+
created_at = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
|
|
127
|
+
elif not isinstance(created_at, datetime) or created_at.tzinfo is None:
|
|
128
|
+
raise ValueError(f"createdAt must be an aware datetime or ISO-8601 string: {created_at!r}")
|
|
129
|
+
created_at = created_at.replace(microsecond=created_at.microsecond // 1000 * 1000)
|
|
105
130
|
thread.next_seq = max(thread.next_seq, seq + 1)
|
|
106
|
-
stored = {**message, "seq": seq}
|
|
131
|
+
stored = {**message, "seq": seq, "createdAt": created_at}
|
|
107
132
|
thread.messages[stored["id"]] = stored
|
|
108
133
|
thread.children.setdefault(parent, []).append(stored["id"])
|
|
109
134
|
thread.children[parent].sort(key=lambda i: thread.messages[i]["seq"])
|
|
@@ -65,6 +65,7 @@ projections.
|
|
|
65
65
|
import asyncio
|
|
66
66
|
import inspect
|
|
67
67
|
import logging
|
|
68
|
+
import math
|
|
68
69
|
from dataclasses import dataclass, field
|
|
69
70
|
from typing import (
|
|
70
71
|
Any,
|
|
@@ -432,6 +433,7 @@ class RunManager(Tray):
|
|
|
432
433
|
|
|
433
434
|
capabilities: frozenset[str] = frozenset()
|
|
434
435
|
max_queued: int = 50
|
|
436
|
+
max_redelivery_age: float | None = None
|
|
435
437
|
prepare_message: Callable[[dict[str, Any]], dict[str, Any]] | None = None
|
|
436
438
|
prepare_input: Callable[[dict[str, Any]], dict[str, Any]] | None = None
|
|
437
439
|
prioritize: "Callable[[RunManager.PrioritizeRequest], Awaitable[str] | str] | None" = None
|
|
@@ -491,6 +493,7 @@ class RunManager(Tray):
|
|
|
491
493
|
self._input_answers: dict[str, Any] = {}
|
|
492
494
|
self._run_live: "RunManager.RunContext | None" = None
|
|
493
495
|
self._fresh = False
|
|
496
|
+
self._redelivering = False
|
|
494
497
|
self._closing = False
|
|
495
498
|
self._idle = asyncio.Event()
|
|
496
499
|
self._verdict_waiters: dict[str, "asyncio.Future[dict[str, Any]]"] = {}
|
|
@@ -601,8 +604,14 @@ class RunManager(Tray):
|
|
|
601
604
|
r if "durable" in r else {**r, "durable": False}
|
|
602
605
|
for r in self.deciding.value
|
|
603
606
|
]
|
|
607
|
+
# the queue lane as earlier replayed records leave it; they apply later
|
|
608
|
+
queued = {i["id"] for i in self._lane_items("queue")}
|
|
604
609
|
for record in self.deciding.value:
|
|
605
|
-
entry = self._undecided(record)
|
|
610
|
+
entry = self._undecided(record, queued)
|
|
611
|
+
if entry.lane == "queue":
|
|
612
|
+
queued.add(entry.message_id)
|
|
613
|
+
else:
|
|
614
|
+
queued.discard(entry.message_id)
|
|
606
615
|
entry.future.add_done_callback(lambda f: f.cancelled() or f.exception())
|
|
607
616
|
self._admit(entry)
|
|
608
617
|
if any(v["response"] is None for v in self.verdicts.value):
|
|
@@ -738,9 +747,9 @@ class RunManager(Tray):
|
|
|
738
747
|
# record for its verdict, and the verdict answers the parked seq after
|
|
739
748
|
# the effects' flush; the tray outlives undelivered verdicts. After a
|
|
740
749
|
# restart a record stages as if no prioritizer answered: "auto" steers
|
|
741
|
-
# while a run is live and
|
|
742
|
-
# answer again. Records are keyed by the command's
|
|
743
|
-
# lane, seq): a frame redelivered after a crash cut answers with its
|
|
750
|
+
# while a run is live and nothing is queued ahead, and queues otherwise;
|
|
751
|
+
# undelivered verdicts answer again. Records are keyed by the command's
|
|
752
|
+
# admission (client, lane, seq): a frame redelivered after a crash cut answers with its
|
|
744
753
|
# record's verdict and stages nothing.
|
|
745
754
|
|
|
746
755
|
def _hold_decision(self, record: dict[str, Any], key: str | None) -> str:
|
|
@@ -806,15 +815,20 @@ class RunManager(Tray):
|
|
|
806
815
|
stored = await asyncio.shield(waiter)
|
|
807
816
|
return stored
|
|
808
817
|
|
|
809
|
-
def _undecided(self, record: dict[str, Any]) -> _Send:
|
|
818
|
+
def _undecided(self, record: dict[str, Any], queued: set[str]) -> _Send:
|
|
810
819
|
behavior = record["behavior"]
|
|
820
|
+
message = record["message"]
|
|
821
|
+
behind = (
|
|
822
|
+
self.prioritize is not None
|
|
823
|
+
and message is not None
|
|
824
|
+
and bool(queued - {message["id"]})
|
|
825
|
+
)
|
|
811
826
|
mode = (
|
|
812
827
|
behavior
|
|
813
828
|
if behavior != "auto"
|
|
814
|
-
else "steer" if self._status() in _LIVE else "queue"
|
|
829
|
+
else "steer" if self._status() in _LIVE and not behind else "queue"
|
|
815
830
|
)
|
|
816
831
|
key = record["key"]
|
|
817
|
-
message = record["message"]
|
|
818
832
|
params = record["params"]
|
|
819
833
|
if message is not None:
|
|
820
834
|
params = {**params, "message": message}
|
|
@@ -1094,9 +1108,11 @@ class RunManager(Tray):
|
|
|
1094
1108
|
item = recorded["item"]
|
|
1095
1109
|
if self._fresh:
|
|
1096
1110
|
self._fresh = False
|
|
1111
|
+
self._redelivering = False
|
|
1097
1112
|
else:
|
|
1098
1113
|
self.outbox.dispatch(item) # redelivery bumps the attempt
|
|
1099
1114
|
self._reclaim(item)
|
|
1115
|
+
self._redelivering = True
|
|
1100
1116
|
yield item
|
|
1101
1117
|
|
|
1102
1118
|
def _reclaim(self, item: Any) -> None:
|
|
@@ -1144,7 +1160,46 @@ class RunManager(Tray):
|
|
|
1144
1160
|
ctx.stop_requested.set()
|
|
1145
1161
|
return ctx
|
|
1146
1162
|
|
|
1163
|
+
async def _stale_reason(self, item: Any, resume: bool) -> str | None:
|
|
1164
|
+
max_age = self.max_redelivery_age
|
|
1165
|
+
since = self._ledger.since
|
|
1166
|
+
if max_age is not None and since is not None:
|
|
1167
|
+
# pinboard's first-delivery time; a row from before it is stamped
|
|
1168
|
+
# at this redelivery, so its window starts at the upgrade
|
|
1169
|
+
age = self._domain.now() - since
|
|
1170
|
+
if age > max_age:
|
|
1171
|
+
return f"it was first dispatched {age / 3600:.1f}h ago"
|
|
1172
|
+
thread = self.handler.__self__
|
|
1173
|
+
record = item["record"]
|
|
1174
|
+
probes = [
|
|
1175
|
+
message["id"] if resume else message.get("~parent", _ABSENT)
|
|
1176
|
+
for message in record["messages"]
|
|
1177
|
+
]
|
|
1178
|
+
if not resume:
|
|
1179
|
+
# an unapplied edit or reload records its rewound parent here
|
|
1180
|
+
probes.append(record.get("rollbackTo", _ABSENT))
|
|
1181
|
+
for probe in probes:
|
|
1182
|
+
if probe is _ABSENT or (probe is None and not resume):
|
|
1183
|
+
continue
|
|
1184
|
+
meta = await thread.get_message_meta(probe)
|
|
1185
|
+
if meta is not None and meta.get("onActiveBranch") is False:
|
|
1186
|
+
return f"message {probe!r} is no longer on the active branch"
|
|
1187
|
+
return None
|
|
1188
|
+
|
|
1189
|
+
def _guard_redelivery(self, item: Any, fn: Callable[..., Any]) -> Callable[..., Any]:
|
|
1190
|
+
async def guarded(ctx: "RunManager.RunContext") -> Any:
|
|
1191
|
+
reason = await self._stale_reason(item, ctx.resume)
|
|
1192
|
+
if reason is not None:
|
|
1193
|
+
logger.warning("run %r refused its redelivery: %s", self.id, reason)
|
|
1194
|
+
raise _reject("stale-redelivery", f"run not resumed: {reason}")
|
|
1195
|
+
return await fn(ctx)
|
|
1196
|
+
|
|
1197
|
+
return guarded
|
|
1198
|
+
|
|
1147
1199
|
async def _run_item(self, item: Any, fn: Callable[..., Any]) -> Any:
|
|
1200
|
+
if self._redelivering:
|
|
1201
|
+
self._redelivering = False
|
|
1202
|
+
fn = self._guard_redelivery(item, fn)
|
|
1148
1203
|
ctx = self._make_run_ctx(item)
|
|
1149
1204
|
self._run_live = ctx
|
|
1150
1205
|
task = asyncio.ensure_future(self._execute(ctx, fn))
|
|
@@ -2511,11 +2566,14 @@ class RunManagerNamespace(TrayMapNamespace):
|
|
|
2511
2566
|
await asyncio.wait((prior,))
|
|
2512
2567
|
tray = self.live() or tray
|
|
2513
2568
|
subject = self._subject(tray, behavior, message, message_id, auth_result)
|
|
2514
|
-
|
|
2515
|
-
self._mode(tray, behavior)
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
)
|
|
2569
|
+
if subject is None:
|
|
2570
|
+
mode = self._mode(tray, behavior)
|
|
2571
|
+
elif message is not None and any(
|
|
2572
|
+
i["id"] != message_id for i in tray._lane_items("queue")
|
|
2573
|
+
):
|
|
2574
|
+
mode = "queue"
|
|
2575
|
+
else:
|
|
2576
|
+
mode = await self._prioritized(cls, tray, subject)
|
|
2519
2577
|
entry = staged(mode)
|
|
2520
2578
|
entry.decided = decided
|
|
2521
2579
|
return await tray._stage(entry, release)
|
|
@@ -2744,6 +2802,7 @@ def run_manager(
|
|
|
2744
2802
|
*,
|
|
2745
2803
|
capabilities: Iterable[str] = (),
|
|
2746
2804
|
max_queued: int = 50,
|
|
2805
|
+
max_redelivery_age: float | None = None,
|
|
2747
2806
|
prepare_message: Callable[[dict[str, Any]], dict[str, Any]] | None = None,
|
|
2748
2807
|
prepare_input: Callable[[dict[str, Any]], dict[str, Any]] | None = None,
|
|
2749
2808
|
prioritize: "Callable[[RunManager.PrioritizeRequest], Awaitable[str] | str] | None" = None,
|
|
@@ -2759,6 +2818,13 @@ def run_manager(
|
|
|
2759
2818
|
raise ValueError("rewind-during-run requires the rewind capability")
|
|
2760
2819
|
if max_queued < 1:
|
|
2761
2820
|
raise ValueError("max_queued must be >= 1")
|
|
2821
|
+
if max_redelivery_age is not None and (
|
|
2822
|
+
isinstance(max_redelivery_age, bool)
|
|
2823
|
+
or not isinstance(max_redelivery_age, (int, float))
|
|
2824
|
+
or not math.isfinite(max_redelivery_age)
|
|
2825
|
+
or max_redelivery_age <= 0
|
|
2826
|
+
):
|
|
2827
|
+
raise ValueError("max_redelivery_age must be a positive number of seconds or None")
|
|
2762
2828
|
if prioritize is not None and not callable(prioritize):
|
|
2763
2829
|
raise TypeError("prioritize must be callable")
|
|
2764
2830
|
|
|
@@ -2769,6 +2835,7 @@ def run_manager(
|
|
|
2769
2835
|
{
|
|
2770
2836
|
"capabilities": caps,
|
|
2771
2837
|
"max_queued": max_queued,
|
|
2838
|
+
"max_redelivery_age": max_redelivery_age,
|
|
2772
2839
|
"prepare_message": staticmethod(prepare_message)
|
|
2773
2840
|
if prepare_message is not None
|
|
2774
2841
|
else None,
|
|
@@ -7,7 +7,6 @@ instance is the store's only writer.
|
|
|
7
7
|
.. versionadded:: alpha
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
|
-
from datetime import datetime, timezone
|
|
11
10
|
from typing import Any, Literal, NotRequired, Protocol, TypedDict, get_args
|
|
12
11
|
|
|
13
12
|
from fastapi import HTTPException, Request
|
|
@@ -15,6 +14,7 @@ from pinboard import Attestation, AuthRejected, attestation, route
|
|
|
15
14
|
from starlette.requests import HTTPConnection
|
|
16
15
|
from statewire import Statewire, StatewireReject, command
|
|
17
16
|
|
|
17
|
+
from .messages import iso_now
|
|
18
18
|
from .protocol import HARNESS_THREADS_PROTOCOL
|
|
19
19
|
|
|
20
20
|
ThreadStatus = Literal["regular", "archived"]
|
|
@@ -61,10 +61,6 @@ class ThreadStore(Protocol):
|
|
|
61
61
|
...
|
|
62
62
|
|
|
63
63
|
|
|
64
|
-
def _now() -> str:
|
|
65
|
-
return datetime.now(timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z")
|
|
66
|
-
|
|
67
|
-
|
|
68
64
|
class MemoryThreadStore:
|
|
69
65
|
"""In-process ``ThreadStore``.
|
|
70
66
|
|
|
@@ -86,7 +82,7 @@ class MemoryThreadStore:
|
|
|
86
82
|
async def create(self, thread_id: str) -> None:
|
|
87
83
|
if thread_id in self._threads:
|
|
88
84
|
raise DuplicateThread(thread_id)
|
|
89
|
-
self._threads[thread_id] = {"id": thread_id, "status": "regular", "updatedAt":
|
|
85
|
+
self._threads[thread_id] = {"id": thread_id, "status": "regular", "updatedAt": iso_now()}
|
|
90
86
|
|
|
91
87
|
async def rename(self, thread_id: str, title: str) -> None:
|
|
92
88
|
self._get(thread_id)["title"] = title
|
|
@@ -102,7 +98,7 @@ class MemoryThreadStore:
|
|
|
102
98
|
self, thread_id: str, *, title: str | None = None, activity: ThreadActivity | Literal["idle"] | None = None
|
|
103
99
|
) -> None:
|
|
104
100
|
record = self._threads.setdefault(thread_id, {"id": thread_id, "status": "regular", "updatedAt": ""})
|
|
105
|
-
record["updatedAt"] =
|
|
101
|
+
record["updatedAt"] = iso_now()
|
|
106
102
|
if title is not None and "title" not in record:
|
|
107
103
|
record["title"] = title
|
|
108
104
|
if activity == "idle":
|
|
@@ -74,6 +74,7 @@ def make_host(
|
|
|
74
74
|
authorize=None,
|
|
75
75
|
version=HARNESS_PROTOCOL.version,
|
|
76
76
|
prioritize=None,
|
|
77
|
+
max_redelivery_age=None,
|
|
77
78
|
):
|
|
78
79
|
class Host(Statewire):
|
|
79
80
|
protocols = (
|
|
@@ -97,6 +98,7 @@ def make_host(
|
|
|
97
98
|
@run_manager(
|
|
98
99
|
capabilities=capabilities,
|
|
99
100
|
max_queued=max_queued,
|
|
101
|
+
max_redelivery_age=max_redelivery_age,
|
|
100
102
|
prepare_message=prepare_message,
|
|
101
103
|
prepare_input=prepare_input,
|
|
102
104
|
prioritize=prioritize,
|
|
@@ -334,6 +336,7 @@ async def run_host(
|
|
|
334
336
|
version=HARNESS_PROTOCOL.version,
|
|
335
337
|
prioritize=None,
|
|
336
338
|
client_id=None,
|
|
339
|
+
max_redelivery_age=None,
|
|
337
340
|
):
|
|
338
341
|
"""``version`` is the negotiated protocol date the host passes to sends; None passes none."""
|
|
339
342
|
host_cls = make_host(
|
|
@@ -345,6 +348,7 @@ async def run_host(
|
|
|
345
348
|
authorize=authorize,
|
|
346
349
|
version=version,
|
|
347
350
|
prioritize=prioritize,
|
|
351
|
+
max_redelivery_age=max_redelivery_age,
|
|
348
352
|
)
|
|
349
353
|
worker = Worker(
|
|
350
354
|
state_store=store if store is not None else MemoryStateStore()
|
|
@@ -2,6 +2,7 @@ import asyncio
|
|
|
2
2
|
import copy
|
|
3
3
|
import uuid
|
|
4
4
|
from dataclasses import dataclass, field
|
|
5
|
+
from datetime import datetime, timezone
|
|
5
6
|
from typing import Any
|
|
6
7
|
|
|
7
8
|
import pytest
|
|
@@ -22,7 +23,7 @@ from voice_helpers import NEGOTIATION, VoiceDriver
|
|
|
22
23
|
from harness_sdk import DeepagentsHost, MemoryCheckpointIndex
|
|
23
24
|
from harness_sdk.deepagents import DeepagentsThread
|
|
24
25
|
from harness_sdk.interest import check_request, resolve
|
|
25
|
-
from harness_sdk.messages import MAIN_NS, MessageTree
|
|
26
|
+
from harness_sdk.messages import MAIN_NS, MessageTree, iso
|
|
26
27
|
|
|
27
28
|
pytestmark = pytest.mark.anyio
|
|
28
29
|
|
|
@@ -123,15 +124,27 @@ async def test_send_streams_parts_and_persists_rows():
|
|
|
123
124
|
assert [r["message_id"] for r in rows] == ids and rows[1]["parent_id"] == ids[0]
|
|
124
125
|
|
|
125
126
|
|
|
126
|
-
|
|
127
|
+
class _Frozen(datetime):
|
|
128
|
+
@classmethod
|
|
129
|
+
def now(cls, tz=None):
|
|
130
|
+
return datetime(2000, 1, 1, tzinfo=timezone.utc)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
async def test_reboot_hydrates_tree_from_rows(monkeypatch):
|
|
127
134
|
saver, index = InMemorySaver(), MemoryCheckpointIndex()
|
|
128
135
|
agent, thread = await build(saver, index)
|
|
129
136
|
await run_turn(agent, thread, Ctx(messages=(user("one two"),)))
|
|
130
137
|
head = thread.tree.thread(MAIN_NS).head
|
|
138
|
+
created = {i: thread.tree.get(MAIN_NS, i)["createdAt"] for i in chain(thread)}
|
|
139
|
+
assert all(isinstance(at, datetime) and at.tzinfo is not None for at in created.values())
|
|
140
|
+
rows = await index.read_messages("t1")
|
|
141
|
+
assert {r["message_id"]: r["value"]["createdAt"] for r in rows} == {i: iso(at) for i, at in created.items()}
|
|
142
|
+
monkeypatch.setattr("harness_sdk.messages.datetime", _Frozen)
|
|
131
143
|
_agent, rebooted = await build(saver, index)
|
|
132
144
|
assert chain(rebooted) == chain(thread)
|
|
133
145
|
assert rebooted.tree.thread(MAIN_NS).head == head
|
|
134
146
|
assert rebooted.tree.get(MAIN_NS, head)["parts"][1]["text"] == "answer: 2"
|
|
147
|
+
assert {i: rebooted.tree.get(MAIN_NS, i)["createdAt"] for i in chain(rebooted)} == created
|
|
135
148
|
|
|
136
149
|
|
|
137
150
|
async def test_edit_forks_a_sibling_and_reload_regenerates():
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
from datetime import datetime, timezone
|
|
2
|
+
|
|
1
3
|
import pytest
|
|
2
4
|
|
|
3
|
-
from harness_sdk.interest import InterestError, check_request, covered, resolve
|
|
4
|
-
from harness_sdk.messages import MessageTree
|
|
5
|
+
from harness_sdk.interest import InterestError, check_request, covered, resolve, spawned_ns
|
|
6
|
+
from harness_sdk.messages import MessageTree, iso
|
|
5
7
|
|
|
6
8
|
|
|
7
9
|
def _tree():
|
|
@@ -66,6 +68,57 @@ def test_follow_expands_spawned_namespaces():
|
|
|
66
68
|
assert covered(request, r) == {("main", "a3"), ("sub-1", "s2")}
|
|
67
69
|
|
|
68
70
|
|
|
71
|
+
|
|
72
|
+
def _tool(metadata):
|
|
73
|
+
return {"type": "tool", "toolInvocationId": "t", "toolName": "task", "input": {}, "state": "done", "metadata": metadata}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def test_spawned_ns_reads_ns_then_namespaces_in_order():
|
|
77
|
+
message = {"parts": [_tool({"ns": "b", "namespaces": ["a", "c"]}), _tool({"namespaces": ["d"]})]}
|
|
78
|
+
assert spawned_ns(message) == ["b", "a", "c", "d"]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_spawned_ns_keeps_duplicates_for_resolve_to_collapse():
|
|
82
|
+
assert spawned_ns({"parts": [_tool({"ns": "b", "namespaces": ["b", "a", "a"]})]}) == ["b", "b", "a", "a"]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@pytest.mark.parametrize(
|
|
86
|
+
"metadata",
|
|
87
|
+
[
|
|
88
|
+
{"namespaces": "a"},
|
|
89
|
+
{"namespaces": {"a": True}},
|
|
90
|
+
{"namespaces": None},
|
|
91
|
+
{"namespaces": ["", 7, None, ["a"], {"ns": "a"}]},
|
|
92
|
+
{"ns": 7},
|
|
93
|
+
{"ns": ""},
|
|
94
|
+
],
|
|
95
|
+
)
|
|
96
|
+
def test_spawned_ns_ignores_non_string_empty_and_non_list_values(metadata):
|
|
97
|
+
assert spawned_ns({"parts": [_tool(metadata)]}) == []
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_spawned_ns_skips_the_reserved_proto_namespace():
|
|
101
|
+
message = {"parts": [_tool({"ns": "__proto__", "namespaces": ["__proto__", "a"]})]}
|
|
102
|
+
assert spawned_ns(message) == ["a"]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_spawned_ns_ignores_namespaces_outside_tool_parts():
|
|
106
|
+
message = {"parts": [{"type": "text", "text": "hi", "state": "done", "metadata": {"namespaces": ["a"]}}]}
|
|
107
|
+
assert spawned_ns(message) == []
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def test_follow_expands_every_namespace_in_metadata_namespaces_after_metadata_ns():
|
|
111
|
+
tree = _tree()
|
|
112
|
+
tree.get("main", "a3")["parts"].append(_tool({"ns": "b", "namespaces": ["a", "", 7, "b"]}))
|
|
113
|
+
for ns in ("a", "b"):
|
|
114
|
+
tree.add(ns, {"id": f"{ns}1", "parentId": None, "role": "assistant", "parts": []})
|
|
115
|
+
request = check_request({"ns": "main", "before": 1, "follow": {"before": 1}})
|
|
116
|
+
r = resolve(tree, request)
|
|
117
|
+
assert list(r["follows"]) == ["b", "a"]
|
|
118
|
+
assert r["follows"]["a"] == {"chain": [{"id": "a1", "siblings": ["a1"]}], "more": {"before": False, "after": False}}
|
|
119
|
+
assert covered(request, r) == {("main", "a3"), ("b", "b1"), ("a", "a1")}
|
|
120
|
+
|
|
121
|
+
|
|
69
122
|
@pytest.mark.parametrize(
|
|
70
123
|
"req, code",
|
|
71
124
|
[
|
|
@@ -100,3 +153,23 @@ def test_tree_rejects_unknown_parent_and_duplicates():
|
|
|
100
153
|
with pytest.raises(ValueError):
|
|
101
154
|
tree.add("main", {"id": "x", "parentId": None, "role": "user", "parts": []})
|
|
102
155
|
assert tree.get("main", "x")["seq"] == 1
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def test_tree_stamps_created_at_unless_present():
|
|
159
|
+
tree = MessageTree()
|
|
160
|
+
stamped = tree.add("main", {"id": "x", "parentId": None, "role": "user", "parts": []})
|
|
161
|
+
assert isinstance(stamped["createdAt"], datetime) and stamped["createdAt"].tzinfo is not None
|
|
162
|
+
assert stamped["createdAt"].microsecond % 1000 == 0
|
|
163
|
+
kept = tree.add("main", {"id": "y", "parentId": "x", "role": "assistant", "parts": [], "createdAt": datetime(2026, 10, 13, 8, 0, 0, 123456, tzinfo=timezone.utc)})
|
|
164
|
+
assert kept["createdAt"] == datetime(2026, 10, 13, 8, 0, 0, 123000, tzinfo=timezone.utc)
|
|
165
|
+
for bad in (1, None, datetime(2026, 10, 13, 8), "yesterday", "2026-10-13", "2026-10-13T08:00:00Z", "2026-10-13T08:00:00.000+00:00", "2026-02-30T08:00:00.000Z"):
|
|
166
|
+
with pytest.raises(ValueError):
|
|
167
|
+
tree.add("main", {"id": "z", "parentId": None, "role": "user", "parts": [], "createdAt": bad})
|
|
168
|
+
assert tree.get("main", "z") is None
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def test_tree_loads_a_stored_iso_created_at_as_a_datetime():
|
|
172
|
+
tree = MessageTree()
|
|
173
|
+
legacy = tree.add("main", {"id": "x", "parentId": None, "role": "user", "parts": [], "createdAt": "2026-10-13T08:00:00.000Z"})
|
|
174
|
+
assert legacy["createdAt"] == datetime(2026, 10, 13, 8, tzinfo=timezone.utc)
|
|
175
|
+
assert iso(legacy["createdAt"]) == "2026-10-13T08:00:00.000Z"
|
|
@@ -111,7 +111,6 @@ async def test_auto_follows_the_prioritizer(mode, lane):
|
|
|
111
111
|
prioritize = Prioritizer(mode)
|
|
112
112
|
async with run_host(script, prioritize=prioritize) as (drv, host):
|
|
113
113
|
call = await running(drv, script)
|
|
114
|
-
await drv.command("run/send", add("q1", behavior="queue"), terminal=False)
|
|
115
114
|
await drv.command("run/send", add("s1", behavior="steer"), terminal=False)
|
|
116
115
|
await drv.command(
|
|
117
116
|
"run/send", add("a1", meta={"secret": 1}), terminal=False
|
|
@@ -126,7 +125,25 @@ async def test_auto_follows_the_prioritizer(mode, lane):
|
|
|
126
125
|
messages=(msg("m1"),),
|
|
127
126
|
steer_queue=(msg("s1"),),
|
|
128
127
|
)
|
|
129
|
-
assert req.queue == (
|
|
128
|
+
assert req.queue == ()
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
async def test_auto_with_steers_and_a_queued_message_asks_again_once_the_queue_empties():
|
|
132
|
+
script = Script()
|
|
133
|
+
prioritize = Prioritizer("steer")
|
|
134
|
+
async with run_host(script, prioritize=prioritize) as (drv, host):
|
|
135
|
+
await running(drv, script)
|
|
136
|
+
await drv.command("run/send", add("s1", behavior="steer"), terminal=False)
|
|
137
|
+
await drv.command("run/send", add("q2", behavior="queue"), terminal=False)
|
|
138
|
+
await drv.command("run/send", add("a3"), terminal=False)
|
|
139
|
+
assert prioritize.requests == []
|
|
140
|
+
assert queue_ids(drv.replica, "queue") == ["q2", "a3"]
|
|
141
|
+
assert queue_ids(drv.replica, "steerQueue") == ["s1"]
|
|
142
|
+
for message_id in ("q2", "a3"):
|
|
143
|
+
await drv.command("run/dequeue", {"messageId": message_id}, terminal=False)
|
|
144
|
+
await drv.command("run/send", add("a4"), terminal=False)
|
|
145
|
+
assert [r.message["id"] for r in prioritize.requests] == ["a4"]
|
|
146
|
+
assert queue_ids(drv.replica, "steerQueue") == ["s1", "a4"]
|
|
130
147
|
|
|
131
148
|
|
|
132
149
|
async def test_request_lists_taken_steers_as_run_messages():
|
|
@@ -337,10 +354,11 @@ async def test_auto_move_consults_the_prioritizer_on_the_queued_item():
|
|
|
337
354
|
async with run_host(script, prioritize=prioritize) as (drv, host):
|
|
338
355
|
call = await running(drv, script)
|
|
339
356
|
await drv.command("run/send", add("q1", behavior="queue"), terminal=False)
|
|
357
|
+
await drv.command("run/send", add("q2", behavior="queue"), terminal=False)
|
|
340
358
|
await drv.command("run/send", target(drv.run["runId"], messageId="q1"), terminal=False)
|
|
341
359
|
(req,) = prioritize.requests
|
|
342
360
|
assert req.message == msg("q1")
|
|
343
|
-
assert req.queue == ()
|
|
361
|
+
assert req.queue == (msg("q2"),)
|
|
344
362
|
assert queue_ids(drv.replica, "steerQueue") == ["q1"]
|
|
345
363
|
assert call.ctx.stop_requested.is_set()
|
|
346
364
|
|
|
@@ -392,10 +410,9 @@ async def test_decisions_stage_in_arrival_order():
|
|
|
392
410
|
gate.set_result("queue")
|
|
393
411
|
for seq in (first, second, third):
|
|
394
412
|
assert (await drv.res(seq, terminal=False))["type"] in ("pending", "accepted")
|
|
395
|
-
await drv.wait(lambda s: queue_ids(s, "steerQueue") == ["
|
|
396
|
-
assert queue_ids(drv.replica, "queue") == ["a1"]
|
|
397
|
-
assert [r.message["id"] for r in prioritize.requests] == ["a1"
|
|
398
|
-
assert prioritize.requests[1].queue == (msg("a1"),)
|
|
413
|
+
await drv.wait(lambda s: queue_ids(s, "steerQueue") == ["s3"])
|
|
414
|
+
assert queue_ids(drv.replica, "queue") == ["a1", "a2"]
|
|
415
|
+
assert [r.message["id"] for r in prioritize.requests] == ["a1"]
|
|
399
416
|
|
|
400
417
|
|
|
401
418
|
async def test_settle_cancels_a_pending_decision():
|
|
@@ -468,10 +485,10 @@ async def test_dequeue_applies_while_a_decision_is_pending():
|
|
|
468
485
|
prioritize = Prioritizer()
|
|
469
486
|
async with run_host(script, prioritize=prioritize) as (drv, host):
|
|
470
487
|
await running(drv, script)
|
|
471
|
-
await drv.command("run/send", add("q1", behavior="
|
|
488
|
+
await drv.command("run/send", add("q1", behavior="steer"), terminal=False)
|
|
472
489
|
gate, _seq = await deciding(drv, prioritize)
|
|
473
490
|
drv.post("run/dequeue", {"messageId": "q1"})
|
|
474
|
-
await drv.wait(lambda s: queue_ids(s, "
|
|
491
|
+
await drv.wait(lambda s: queue_ids(s, "steerQueue") == [], timeout=1)
|
|
475
492
|
gate.set_result("steer")
|
|
476
493
|
await drv.wait(lambda s: queue_ids(s, "steerQueue") == ["a1"])
|
|
477
494
|
|
|
@@ -551,10 +568,8 @@ async def test_sends_waiting_for_a_decision_are_bounded_by_max_queued():
|
|
|
551
568
|
await asyncio.sleep(0.01)
|
|
552
569
|
assert_rejected(await drv.command("run/send", add("s3")), "queue-full")
|
|
553
570
|
gate.set_result("queue")
|
|
554
|
-
await drv.wait(
|
|
555
|
-
|
|
556
|
-
and queue_ids(s, "steerQueue") == ["s2"]
|
|
557
|
-
)
|
|
571
|
+
await drv.wait(lambda s: queue_ids(s, "queue") == ["a1", "s2"])
|
|
572
|
+
assert [r.message["id"] for r in prioritize.requests] == ["a1"]
|
|
558
573
|
|
|
559
574
|
|
|
560
575
|
def deciding_ids(store):
|
|
@@ -602,6 +617,32 @@ async def test_a_pending_decision_survives_a_crash_as_a_steer():
|
|
|
602
617
|
assert [m["id"] for m in redelivered.ctx.steering.take()] == ["a1"]
|
|
603
618
|
|
|
604
619
|
|
|
620
|
+
async def test_after_a_crash_auto_sends_follow_the_replayed_queue():
|
|
621
|
+
store = MemoryStateStore()
|
|
622
|
+
script = Script()
|
|
623
|
+
prioritize = Prioritizer()
|
|
624
|
+
async with run_host(script, store=store, prioritize=prioritize) as (drv, host):
|
|
625
|
+
await running(drv, script)
|
|
626
|
+
await deciding(drv, prioritize)
|
|
627
|
+
drv.post("run/send", add("q2", behavior="queue"))
|
|
628
|
+
drv.post("run/send", target(drv.run["runId"], messageId="q2"))
|
|
629
|
+
drv.post("run/send", add("a3"))
|
|
630
|
+
drv.post("run/send", add("q4", behavior="queue"))
|
|
631
|
+
drv.post("run/send", add("a5"))
|
|
632
|
+
async with asyncio.timeout(5):
|
|
633
|
+
while deciding_ids(store) != ["a1", "q2", "q2", "a3", "q4", "a5"]:
|
|
634
|
+
await host.live.frontier.wait()
|
|
635
|
+
await asyncio.sleep(0.01)
|
|
636
|
+
crashed = MemoryStateStore()
|
|
637
|
+
crashed.trays = dict(store.trays)
|
|
638
|
+
|
|
639
|
+
script2 = Script()
|
|
640
|
+
async with run_host(script2, store=crashed, prioritize=Prioritizer()) as (drv2, host2):
|
|
641
|
+
await script2.next_call()
|
|
642
|
+
await drv2.wait(lambda s: queue_ids(s, "queue") == ["q4", "a5"])
|
|
643
|
+
assert queue_ids(drv2.replica, "steerQueue") == ["a1", "q2", "a3"]
|
|
644
|
+
|
|
645
|
+
|
|
605
646
|
async def test_a_frame_redelivered_with_its_decision_record_stages_once():
|
|
606
647
|
"""A crash between the record's flush and the frame's pop keeps both: the
|
|
607
648
|
record stages the send, and the redelivered frame answers its verdict."""
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"""Contract: a run interrupted mid-flight redelivers on a later activation only
|
|
2
|
+
while its messages stay on the active branch and, with ``max_redelivery_age``
|
|
3
|
+
set, while it is fresh by pinboard's first-delivery time (``~since``). A
|
|
4
|
+
refused redelivery settles as a ``stale-redelivery`` error without running:
|
|
5
|
+
the outbox acks, the row unmarks, and no later activation runs it again.
|
|
6
|
+
Without ``max_redelivery_age`` only pinboard's 14-day ceiling bounds age."""
|
|
7
|
+
|
|
8
|
+
import asyncio
|
|
9
|
+
|
|
10
|
+
from pinboard.testing import MemoryStateStore
|
|
11
|
+
from run_helpers import Script, add, edit_params, msg, reload_params, run_host
|
|
12
|
+
|
|
13
|
+
DAY = 24 * 3600.0
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def survivors(store: MemoryStateStore) -> MemoryStateStore:
|
|
17
|
+
crashed = MemoryStateStore()
|
|
18
|
+
crashed.trays = dict(store.trays)
|
|
19
|
+
return crashed
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def run_rows(store: MemoryStateStore) -> list[tuple[bool, dict]]:
|
|
23
|
+
return [row for key, row in store.trays.items() if key[2] == "runs"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def rewrite_since(store: MemoryStateStore, change) -> None:
|
|
27
|
+
for key, (marked, doc) in list(store.trays.items()):
|
|
28
|
+
if key[2] == "runs" and doc.get("outbox") is not None:
|
|
29
|
+
doc = dict(doc)
|
|
30
|
+
since = change(doc.get("~since"))
|
|
31
|
+
if since is None:
|
|
32
|
+
doc.pop("~since", None)
|
|
33
|
+
else:
|
|
34
|
+
doc["~since"] = since
|
|
35
|
+
store.trays[key] = (marked, doc)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def run_since(store: MemoryStateStore) -> list:
|
|
39
|
+
return [doc.get("~since") for _m, doc in run_rows(store) if doc.get("outbox")]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
async def interrupted_run(script: Script, *, applied: bool = False) -> MemoryStateStore:
|
|
43
|
+
"""The store as a crash left it: m1's run in flight, never settled."""
|
|
44
|
+
store = MemoryStateStore()
|
|
45
|
+
async with run_host(script, store=store) as (drv, host):
|
|
46
|
+
drv.post("run/send", add("m1", behavior="queue"))
|
|
47
|
+
call = await script.next_call()
|
|
48
|
+
if applied:
|
|
49
|
+
call.applied()
|
|
50
|
+
await host.live.frontier.wait()
|
|
51
|
+
return survivors(store)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
async def assert_refused(store: MemoryStateStore, script: Script, **host) -> None:
|
|
55
|
+
async with run_host(script, store=store, **host) as (drv, _):
|
|
56
|
+
await drv.wait_status("error")
|
|
57
|
+
assert drv.run["error"]["reason"] == "stale-redelivery"
|
|
58
|
+
script.no_call()
|
|
59
|
+
assert [(marked, doc.get("outbox")) for marked, doc in run_rows(store)] == [
|
|
60
|
+
(False, None)
|
|
61
|
+
]
|
|
62
|
+
again = Script()
|
|
63
|
+
async with run_host(again, store=store, **host) as (drv, _):
|
|
64
|
+
await asyncio.sleep(0.05)
|
|
65
|
+
again.no_call()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
async def test_a_redelivery_past_max_age_settles_as_an_error_without_running():
|
|
69
|
+
crashed = await interrupted_run(Script())
|
|
70
|
+
rewrite_since(crashed, lambda since: since - 2 * DAY)
|
|
71
|
+
await assert_refused(crashed, Script(), max_redelivery_age=DAY)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
async def test_a_legacy_dispatch_without_a_time_is_stamped_and_redelivers():
|
|
75
|
+
crashed = await interrupted_run(Script())
|
|
76
|
+
rewrite_since(crashed, lambda since: None)
|
|
77
|
+
assert run_since(crashed) == [None]
|
|
78
|
+
script = Script()
|
|
79
|
+
async with run_host(script, store=crashed, max_redelivery_age=DAY) as (drv, _):
|
|
80
|
+
call = await script.next_call()
|
|
81
|
+
assert call.ctx.attempts == 2
|
|
82
|
+
assert all(isinstance(since, float) for since in run_since(crashed))
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
async def test_a_fresh_redelivery_still_runs():
|
|
86
|
+
crashed = await interrupted_run(Script())
|
|
87
|
+
rewrite_since(crashed, lambda since: since - 3600)
|
|
88
|
+
script = Script()
|
|
89
|
+
async with run_host(script, store=crashed, max_redelivery_age=DAY) as (drv, _):
|
|
90
|
+
call = await script.next_call()
|
|
91
|
+
assert call.ctx.attempts == 2
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
async def test_without_max_redelivery_age_a_week_old_run_redelivers():
|
|
95
|
+
crashed = await interrupted_run(Script())
|
|
96
|
+
rewrite_since(crashed, lambda since: since - 7 * DAY)
|
|
97
|
+
script = Script()
|
|
98
|
+
async with run_host(script, store=crashed):
|
|
99
|
+
await script.next_call()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
async def test_an_applied_batch_off_the_active_branch_is_refused():
|
|
103
|
+
crashed = await interrupted_run(Script(), applied=True)
|
|
104
|
+
script = Script()
|
|
105
|
+
script.thread["m1"] = {
|
|
106
|
+
"parentId": None, "role": "user", "isLeaf": False, "onActiveBranch": False
|
|
107
|
+
}
|
|
108
|
+
await assert_refused(crashed, script)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
async def test_an_unapplied_batch_whose_parent_left_the_active_branch_is_refused():
|
|
112
|
+
script = Script()
|
|
113
|
+
script.thread["a0"] = {
|
|
114
|
+
"parentId": None, "role": "assistant", "isLeaf": True, "onActiveBranch": True
|
|
115
|
+
}
|
|
116
|
+
store = MemoryStateStore()
|
|
117
|
+
async with run_host(script, store=store) as (drv, host):
|
|
118
|
+
drv.post("run/send", add("m1", anchor="a0", behavior="queue"))
|
|
119
|
+
await script.next_call()
|
|
120
|
+
await host.live.frontier.wait()
|
|
121
|
+
crashed = survivors(store)
|
|
122
|
+
script2 = Script()
|
|
123
|
+
script2.thread["a0"] = {**script.thread["a0"], "onActiveBranch": False}
|
|
124
|
+
await assert_refused(crashed, script2)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
async def test_a_run_past_the_pinboard_ceiling_parks_without_running():
|
|
128
|
+
crashed = await interrupted_run(Script())
|
|
129
|
+
rewrite_since(crashed, lambda since: since - 15 * DAY)
|
|
130
|
+
script = Script()
|
|
131
|
+
async with run_host(script, store=crashed):
|
|
132
|
+
await asyncio.sleep(0.05)
|
|
133
|
+
script.no_call()
|
|
134
|
+
[(marked, doc)] = run_rows(crashed)
|
|
135
|
+
assert marked and doc["~parked"] is True and doc["outbox"] is not None
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def node(parent, role, *, leaf, active=True):
|
|
139
|
+
return {"parentId": parent, "role": role, "isLeaf": leaf, "onActiveBranch": active}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
async def interrupted_rewind(command: str, params: dict, thread: dict) -> MemoryStateStore:
|
|
143
|
+
"""An unapplied edit or reload in flight when the instance crashed."""
|
|
144
|
+
script = Script()
|
|
145
|
+
script.thread.update(thread)
|
|
146
|
+
store = MemoryStateStore()
|
|
147
|
+
async with run_host(script, store=store, capabilities=("rewind",)) as (drv, host):
|
|
148
|
+
drv.post(command, params)
|
|
149
|
+
await script.next_call()
|
|
150
|
+
await host.live.frontier.wait()
|
|
151
|
+
return survivors(store)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
async def test_an_unapplied_edit_whose_rewound_parent_left_the_branch_is_refused():
|
|
155
|
+
thread = {"a0": node(None, "assistant", leaf=False), "u1": node("a0", "user", leaf=True)}
|
|
156
|
+
crashed = await interrupted_rewind(
|
|
157
|
+
"run/edit", edit_params("u1", msg("u2"), anchor="a0"), thread
|
|
158
|
+
)
|
|
159
|
+
script = Script()
|
|
160
|
+
script.thread.update({**thread, "a0": node(None, "assistant", leaf=False, active=False)})
|
|
161
|
+
await assert_refused(crashed, script, capabilities=("rewind",))
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
async def test_an_unapplied_reload_whose_rewound_parent_left_the_branch_is_refused():
|
|
165
|
+
thread = {"u1": node(None, "user", leaf=False), "a1": node("u1", "assistant", leaf=True)}
|
|
166
|
+
crashed = await interrupted_rewind(
|
|
167
|
+
"run/reload", reload_params("a1", anchor="u1"), thread
|
|
168
|
+
)
|
|
169
|
+
script = Script()
|
|
170
|
+
script.thread.update({**thread, "u1": node(None, "user", leaf=False, active=False)})
|
|
171
|
+
await assert_refused(crashed, script, capabilities=("rewind",))
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
async def test_an_unapplied_edit_with_its_parent_still_active_redelivers():
|
|
175
|
+
thread = {"a0": node(None, "assistant", leaf=False), "u1": node("a0", "user", leaf=True)}
|
|
176
|
+
crashed = await interrupted_rewind(
|
|
177
|
+
"run/edit", edit_params("u1", msg("u2"), anchor="a0"), thread
|
|
178
|
+
)
|
|
179
|
+
script = Script()
|
|
180
|
+
script.thread.update(thread)
|
|
181
|
+
async with run_host(script, store=crashed, capabilities=("rewind",)):
|
|
182
|
+
call = await script.next_call()
|
|
183
|
+
assert call.ctx.attempts == 2
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/deepagents_host.py
RENAMED
|
File without changes
|
{harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/fenced_postgres.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/providers/__init__.py
RENAMED
|
File without changes
|
{harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/src/harness_sdk/providers/gpt_live.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_deepagents_multi_approval.py
RENAMED
|
File without changes
|
|
File without changes
|
{harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_deepagents_subagent_approval.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{harness_sdk_python-0.40.11 → harness_sdk_python-0.40.13}/tests/test_example_local_origin.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|