hone-flow 0.1.0__py3-none-any.whl
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.
- hone_flow/__init__.py +92 -0
- hone_flow/_records.py +236 -0
- hone_flow/_tracing.py +110 -0
- hone_flow/adapters/__init__.py +1 -0
- hone_flow/adapters/hone_models.py +34 -0
- hone_flow/cli.py +477 -0
- hone_flow/commit.py +296 -0
- hone_flow/dag.py +270 -0
- hone_flow/errors.py +46 -0
- hone_flow/executor.py +220 -0
- hone_flow/fork.py +341 -0
- hone_flow/fsspec_storage.py +129 -0
- hone_flow/history.py +223 -0
- hone_flow/inputs.py +208 -0
- hone_flow/invoke.py +61 -0
- hone_flow/leases.py +73 -0
- hone_flow/metrics.py +190 -0
- hone_flow/notifications.py +231 -0
- hone_flow/ports.py +76 -0
- hone_flow/produced.py +96 -0
- hone_flow/progress.py +56 -0
- hone_flow/py.typed +0 -0
- hone_flow/reports.py +95 -0
- hone_flow/resume.py +87 -0
- hone_flow/reviews.py +228 -0
- hone_flow/run.py +345 -0
- hone_flow/run_format.py +381 -0
- hone_flow/run_lease.py +125 -0
- hone_flow/schedule.py +118 -0
- hone_flow/serialize.py +136 -0
- hone_flow/spans.py +206 -0
- hone_flow/storage.py +248 -0
- hone_flow/testing/__init__.py +8 -0
- hone_flow/testing/contracts.py +131 -0
- hone_flow/testing/fakes.py +42 -0
- hone_flow/testing/memory.py +73 -0
- hone_flow/testing/webhooks.py +71 -0
- hone_flow/types.py +274 -0
- hone_flow/workflow.py +374 -0
- hone_flow-0.1.0.dist-info/METADATA +202 -0
- hone_flow-0.1.0.dist-info/RECORD +44 -0
- hone_flow-0.1.0.dist-info/WHEEL +4 -0
- hone_flow-0.1.0.dist-info/entry_points.txt +5 -0
- hone_flow-0.1.0.dist-info/licenses/LICENSE +202 -0
hone_flow/__init__.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""hone-flow: run AI workflows of plain-function steps as self-contained run folders."""
|
|
2
|
+
|
|
3
|
+
from hone_flow import errors, notifications, testing
|
|
4
|
+
from hone_flow._records import JsonlSpanSink, MemorySink, NullSink, SqliteSpanSink
|
|
5
|
+
from hone_flow._tracing import current_trace
|
|
6
|
+
from hone_flow.errors import (
|
|
7
|
+
HoneFlowError,
|
|
8
|
+
IncompatibleRun,
|
|
9
|
+
OutputNotFound,
|
|
10
|
+
ReviewError,
|
|
11
|
+
RunLocked,
|
|
12
|
+
RunNotFound,
|
|
13
|
+
StepFailed,
|
|
14
|
+
WorkflowDefinitionError,
|
|
15
|
+
WriteConflict,
|
|
16
|
+
)
|
|
17
|
+
from hone_flow.fsspec_storage import FsspecStorage
|
|
18
|
+
from hone_flow.history import RunHistory, open_runs
|
|
19
|
+
from hone_flow.leases import FileLockGpuLease, NullGpuLease
|
|
20
|
+
from hone_flow.ports import PORTS_VERSION, Entry, GpuLease, RecordSink, RunStorage, TraceContext
|
|
21
|
+
from hone_flow.run import Run
|
|
22
|
+
from hone_flow.serialize import register_serializer
|
|
23
|
+
from hone_flow.storage import LocalStorage
|
|
24
|
+
from hone_flow.types import (
|
|
25
|
+
CleanupReport,
|
|
26
|
+
Context,
|
|
27
|
+
Delivery,
|
|
28
|
+
Dir,
|
|
29
|
+
File,
|
|
30
|
+
ForkPlan,
|
|
31
|
+
ForkPlanRow,
|
|
32
|
+
Item,
|
|
33
|
+
Items,
|
|
34
|
+
LeaseInfo,
|
|
35
|
+
Param,
|
|
36
|
+
RunSummary,
|
|
37
|
+
Selection,
|
|
38
|
+
StepRecord,
|
|
39
|
+
)
|
|
40
|
+
from hone_flow.workflow import Workflow
|
|
41
|
+
|
|
42
|
+
__version__ = "0.1.0"
|
|
43
|
+
|
|
44
|
+
__all__ = [
|
|
45
|
+
"PORTS_VERSION",
|
|
46
|
+
"CleanupReport",
|
|
47
|
+
"Context",
|
|
48
|
+
"Delivery",
|
|
49
|
+
"Dir",
|
|
50
|
+
"Entry",
|
|
51
|
+
"File",
|
|
52
|
+
"FileLockGpuLease",
|
|
53
|
+
"ForkPlan",
|
|
54
|
+
"ForkPlanRow",
|
|
55
|
+
"FsspecStorage",
|
|
56
|
+
"GpuLease",
|
|
57
|
+
"HoneFlowError",
|
|
58
|
+
"IncompatibleRun",
|
|
59
|
+
"Item",
|
|
60
|
+
"Items",
|
|
61
|
+
"JsonlSpanSink",
|
|
62
|
+
"LeaseInfo",
|
|
63
|
+
"LocalStorage",
|
|
64
|
+
"MemorySink",
|
|
65
|
+
"NullGpuLease",
|
|
66
|
+
"NullSink",
|
|
67
|
+
"OutputNotFound",
|
|
68
|
+
"Param",
|
|
69
|
+
"RecordSink",
|
|
70
|
+
"ReviewError",
|
|
71
|
+
"Run",
|
|
72
|
+
"RunHistory",
|
|
73
|
+
"RunLocked",
|
|
74
|
+
"RunNotFound",
|
|
75
|
+
"RunStorage",
|
|
76
|
+
"RunSummary",
|
|
77
|
+
"Selection",
|
|
78
|
+
"SqliteSpanSink",
|
|
79
|
+
"StepFailed",
|
|
80
|
+
"StepRecord",
|
|
81
|
+
"TraceContext",
|
|
82
|
+
"Workflow",
|
|
83
|
+
"WorkflowDefinitionError",
|
|
84
|
+
"WriteConflict",
|
|
85
|
+
"__version__",
|
|
86
|
+
"current_trace",
|
|
87
|
+
"errors",
|
|
88
|
+
"notifications",
|
|
89
|
+
"open_runs",
|
|
90
|
+
"register_serializer",
|
|
91
|
+
"testing",
|
|
92
|
+
]
|
hone_flow/_records.py
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"""Span sinks (the honeworks span format, docs/records.md): SQLite, JSONL, memory and null.
|
|
2
|
+
|
|
3
|
+
Every sink strips secrets and, with content capture off (``HONE_CAPTURE_CONTENT=0`` or
|
|
4
|
+
``capture_content=False``), replaces content attributes with their sha256 and length. Sinks never raise
|
|
5
|
+
into the workflow: a failed write is logged once and counted in ``failures``.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import hashlib
|
|
11
|
+
import json
|
|
12
|
+
import logging
|
|
13
|
+
import os
|
|
14
|
+
import re
|
|
15
|
+
import sqlite3
|
|
16
|
+
import threading
|
|
17
|
+
import time
|
|
18
|
+
from collections.abc import Mapping
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
from typing import Any
|
|
21
|
+
|
|
22
|
+
from hone_flow._tracing import SCHEMA_VERSION, iso_now
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger("hone_flow")
|
|
25
|
+
|
|
26
|
+
CONTENT_ATTRIBUTES = ("hone.flow.params", "hone.flow.gate.note")
|
|
27
|
+
SECRET = re.compile(r"sk-[A-Za-z0-9_\-]{8,}|Bearer\s+[A-Za-z0-9._\-~+/=]+")
|
|
28
|
+
BLOB_LIMIT = 64 * 1024
|
|
29
|
+
|
|
30
|
+
SCHEMA = """
|
|
31
|
+
CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
32
|
+
CREATE TABLE IF NOT EXISTS spans (
|
|
33
|
+
span_id TEXT PRIMARY KEY,
|
|
34
|
+
trace_id TEXT NOT NULL,
|
|
35
|
+
parent_span_id TEXT,
|
|
36
|
+
name TEXT NOT NULL,
|
|
37
|
+
kind TEXT NOT NULL DEFAULT 'internal',
|
|
38
|
+
start_time TEXT NOT NULL,
|
|
39
|
+
end_time TEXT,
|
|
40
|
+
status_code TEXT NOT NULL DEFAULT 'unset',
|
|
41
|
+
status_message TEXT NOT NULL DEFAULT '',
|
|
42
|
+
attributes TEXT NOT NULL DEFAULT '{}',
|
|
43
|
+
events TEXT NOT NULL DEFAULT '[]',
|
|
44
|
+
resource TEXT NOT NULL DEFAULT '{}',
|
|
45
|
+
links TEXT NOT NULL DEFAULT '[]'
|
|
46
|
+
);
|
|
47
|
+
CREATE INDEX IF NOT EXISTS spans_trace ON spans(trace_id);
|
|
48
|
+
CREATE INDEX IF NOT EXISTS spans_name_time ON spans(name, start_time);
|
|
49
|
+
CREATE TABLE IF NOT EXISTS blobs (sha256 TEXT PRIMARY KEY, mime TEXT, size INTEGER, data BLOB NOT NULL);
|
|
50
|
+
CREATE TABLE IF NOT EXISTS changes (seq INTEGER PRIMARY KEY AUTOINCREMENT, span_id TEXT NOT NULL,
|
|
51
|
+
op TEXT NOT NULL, at TEXT NOT NULL);
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def capture_default() -> bool:
|
|
56
|
+
return os.environ.get("HONE_CAPTURE_CONTENT", "1") != "0"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
SECRET_ENV_NAMES: set[str] = set()
|
|
60
|
+
"""Environment variables whose values are secrets (webhook URLs of notification destinations)."""
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def strip_secrets(value: Any) -> Any:
|
|
64
|
+
"""Replace API-key-like strings (``sk-...``, ``Bearer ...``) and the values of ``SECRET_ENV_NAMES``
|
|
65
|
+
with ``***``, recursively."""
|
|
66
|
+
if isinstance(value, str):
|
|
67
|
+
for name in SECRET_ENV_NAMES:
|
|
68
|
+
secret = os.environ.get(name, "")
|
|
69
|
+
if len(secret) >= 8: # never replace short values everywhere
|
|
70
|
+
value = value.replace(secret, "***")
|
|
71
|
+
return SECRET.sub("***", value)
|
|
72
|
+
if isinstance(value, Mapping):
|
|
73
|
+
return {k: strip_secrets(v) for k, v in value.items()} # pyright: ignore[reportUnknownVariableType]
|
|
74
|
+
if isinstance(value, list | tuple):
|
|
75
|
+
return [strip_secrets(v) for v in value] # pyright: ignore[reportUnknownVariableType]
|
|
76
|
+
return value
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def prepare(span: Mapping[str, Any], capture_content: bool) -> dict[str, Any]:
|
|
80
|
+
"""The span as it may be written: secrets stripped, content hashed when capture is off."""
|
|
81
|
+
clean: dict[str, Any] = strip_secrets(dict(span))
|
|
82
|
+
if not capture_content:
|
|
83
|
+
attributes = dict(clean["attributes"])
|
|
84
|
+
for key in CONTENT_ATTRIBUTES:
|
|
85
|
+
if key in attributes:
|
|
86
|
+
text = str(attributes[key]).encode()
|
|
87
|
+
attributes[key] = json.dumps({"sha256": hashlib.sha256(text).hexdigest(), "len": len(text)})
|
|
88
|
+
clean["attributes"] = attributes
|
|
89
|
+
return clean
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class _Sink:
|
|
93
|
+
"""Shared failure handling: log the first failure, count all of them."""
|
|
94
|
+
|
|
95
|
+
def __init__(self, capture_content: bool | None) -> None:
|
|
96
|
+
self.capture_content = capture_default() if capture_content is None else capture_content
|
|
97
|
+
self.failures = 0
|
|
98
|
+
|
|
99
|
+
def emit(self, span: Mapping[str, Any]) -> None:
|
|
100
|
+
try:
|
|
101
|
+
self._write(prepare(span, self.capture_content))
|
|
102
|
+
except Exception:
|
|
103
|
+
if self.failures == 0:
|
|
104
|
+
logger.exception("%s could not record a span; further failures are only counted", self)
|
|
105
|
+
self.failures += 1
|
|
106
|
+
|
|
107
|
+
def _write(self, span: dict[str, Any]) -> None:
|
|
108
|
+
raise NotImplementedError
|
|
109
|
+
|
|
110
|
+
def flush(self) -> None:
|
|
111
|
+
"""Spans are written as they are emitted (at span end), so there is nothing to flush."""
|
|
112
|
+
|
|
113
|
+
def close(self) -> None:
|
|
114
|
+
"""Nothing to release."""
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _enable_wal(db: sqlite3.Connection, attempts: int = 50) -> None:
|
|
118
|
+
"""Switch to WAL. The switch ignores busy_timeout when another process is creating the same store at
|
|
119
|
+
the same moment, so retry briefly."""
|
|
120
|
+
for attempt in range(attempts):
|
|
121
|
+
try:
|
|
122
|
+
db.execute("PRAGMA journal_mode=WAL")
|
|
123
|
+
return
|
|
124
|
+
except sqlite3.OperationalError:
|
|
125
|
+
if attempt == attempts - 1:
|
|
126
|
+
raise
|
|
127
|
+
time.sleep(0.1)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class SqliteSpanSink(_Sink):
|
|
131
|
+
"""Spans in SQLite with the honeworks span schema (WAL, busy timeout 5 s).
|
|
132
|
+
|
|
133
|
+
>>> import tempfile, pathlib
|
|
134
|
+
>>> sink = SqliteSpanSink(pathlib.Path(tempfile.mkdtemp()) / "spans.db")
|
|
135
|
+
"""
|
|
136
|
+
|
|
137
|
+
def __init__(self, path: str | os.PathLike[str], *, capture_content: bool | None = None) -> None:
|
|
138
|
+
super().__init__(capture_content)
|
|
139
|
+
self.path = Path(path)
|
|
140
|
+
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
141
|
+
self._lock = threading.Lock()
|
|
142
|
+
self._db = sqlite3.connect(self.path, timeout=5.0, check_same_thread=False)
|
|
143
|
+
self._db.execute("PRAGMA busy_timeout=5000")
|
|
144
|
+
_enable_wal(self._db)
|
|
145
|
+
rows = [("schema", "hone-spans"), ("schema_version", SCHEMA_VERSION), ("package", "hone-flow")]
|
|
146
|
+
with self._lock, self._db:
|
|
147
|
+
self._db.executescript(SCHEMA)
|
|
148
|
+
self._db.executemany(
|
|
149
|
+
"INSERT OR IGNORE INTO meta VALUES (?, ?)", [*rows, ("created_at", iso_now())]
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
def __repr__(self) -> str:
|
|
153
|
+
return f"SqliteSpanSink({str(self.path)!r})"
|
|
154
|
+
|
|
155
|
+
def _blob(self, value: Any) -> Any:
|
|
156
|
+
"""Store a large string attribute in ``blobs`` and return its reference."""
|
|
157
|
+
if not isinstance(value, str):
|
|
158
|
+
return value
|
|
159
|
+
data = value.encode()
|
|
160
|
+
if len(data) <= BLOB_LIMIT:
|
|
161
|
+
return value
|
|
162
|
+
digest = hashlib.sha256(data).hexdigest()
|
|
163
|
+
self._db.execute(
|
|
164
|
+
"INSERT OR IGNORE INTO blobs VALUES (?, ?, ?, ?)", (digest, "text/plain", len(data), data)
|
|
165
|
+
)
|
|
166
|
+
return {"$blob": digest}
|
|
167
|
+
|
|
168
|
+
def _write(self, span: dict[str, Any]) -> None:
|
|
169
|
+
with self._lock, self._db:
|
|
170
|
+
attributes = {k: self._blob(v) for k, v in span["attributes"].items()}
|
|
171
|
+
self._db.execute(
|
|
172
|
+
"INSERT OR REPLACE INTO spans VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
173
|
+
(
|
|
174
|
+
span["span_id"],
|
|
175
|
+
span["trace_id"],
|
|
176
|
+
span["parent_span_id"],
|
|
177
|
+
span["name"],
|
|
178
|
+
span["kind"],
|
|
179
|
+
span["start_time"],
|
|
180
|
+
span["end_time"],
|
|
181
|
+
span["status"]["code"],
|
|
182
|
+
span["status"]["message"],
|
|
183
|
+
json.dumps(attributes),
|
|
184
|
+
json.dumps(span["events"]),
|
|
185
|
+
json.dumps(span["resource"]),
|
|
186
|
+
json.dumps(span["links"]),
|
|
187
|
+
),
|
|
188
|
+
)
|
|
189
|
+
self._db.execute(
|
|
190
|
+
"INSERT INTO changes (span_id, op, at) VALUES (?, 'insert', ?)", (span["span_id"], iso_now())
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
def close(self) -> None:
|
|
194
|
+
with self._lock:
|
|
195
|
+
self._db.close()
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
class JsonlSpanSink(_Sink):
|
|
199
|
+
"""One span per line of a JSON Lines file."""
|
|
200
|
+
|
|
201
|
+
def __init__(self, path: str | os.PathLike[str], *, capture_content: bool | None = None) -> None:
|
|
202
|
+
super().__init__(capture_content)
|
|
203
|
+
self.path = Path(path)
|
|
204
|
+
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
205
|
+
self._lock = threading.Lock()
|
|
206
|
+
|
|
207
|
+
def __repr__(self) -> str:
|
|
208
|
+
return f"JsonlSpanSink({str(self.path)!r})"
|
|
209
|
+
|
|
210
|
+
def _write(self, span: dict[str, Any]) -> None:
|
|
211
|
+
with self._lock, self.path.open("a", encoding="utf-8") as fh:
|
|
212
|
+
fh.write(json.dumps(span) + "\n")
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
class MemorySink(_Sink):
|
|
216
|
+
"""Keeps spans in ``self.spans`` (for tests)."""
|
|
217
|
+
|
|
218
|
+
def __init__(self, *, capture_content: bool | None = None) -> None:
|
|
219
|
+
super().__init__(capture_content)
|
|
220
|
+
self.spans: list[dict[str, Any]] = []
|
|
221
|
+
|
|
222
|
+
def _write(self, span: dict[str, Any]) -> None:
|
|
223
|
+
self.spans.append(span)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
class NullSink:
|
|
227
|
+
"""Drops every span."""
|
|
228
|
+
|
|
229
|
+
def emit(self, span: Mapping[str, Any]) -> None:
|
|
230
|
+
"""Ignore the span."""
|
|
231
|
+
|
|
232
|
+
def flush(self) -> None:
|
|
233
|
+
"""Nothing to flush."""
|
|
234
|
+
|
|
235
|
+
def close(self) -> None:
|
|
236
|
+
"""Nothing to close."""
|
hone_flow/_tracing.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Trace context (design §4.12), span ids and the span dict (the span format of docs/records.md)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import secrets
|
|
7
|
+
import socket
|
|
8
|
+
from collections.abc import Generator, Mapping
|
|
9
|
+
from contextlib import contextmanager
|
|
10
|
+
from contextvars import ContextVar
|
|
11
|
+
from datetime import UTC, datetime
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
SCHEMA_VERSION = "1"
|
|
15
|
+
_current: ContextVar[dict[str, str] | None] = ContextVar("hone_flow_trace", default=None)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def iso_time(moment: datetime) -> str:
|
|
19
|
+
"""A UTC time as ISO-8601 with milliseconds, e.g. ``2026-09-27T14:03:11.120Z``."""
|
|
20
|
+
return moment.astimezone(UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def iso_now() -> str:
|
|
24
|
+
"""The current UTC time as ISO-8601 with milliseconds."""
|
|
25
|
+
return iso_time(datetime.now(UTC))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def new_trace_id() -> str:
|
|
29
|
+
return secrets.token_hex(16)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def new_span_id() -> str:
|
|
33
|
+
return secrets.token_hex(8)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def traceparent(trace_id: str, span_id: str) -> str:
|
|
37
|
+
return f"00-{trace_id}-{span_id}-01"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def current_trace() -> dict[str, str]:
|
|
41
|
+
"""The trace context of the step running now (empty outside a step).
|
|
42
|
+
|
|
43
|
+
Pass it on to other packages: ``client.complete(..., trace=hone_flow.current_trace())``.
|
|
44
|
+
"""
|
|
45
|
+
return dict(_current.get() or {})
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def incoming_trace(trace: Mapping[str, str] | None) -> dict[str, str]:
|
|
49
|
+
"""The caller's trace context: ``trace=`` when given, else the one active now (maybe empty)."""
|
|
50
|
+
return dict(trace) if trace is not None else current_trace()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@contextmanager
|
|
54
|
+
def using_trace(trace: Mapping[str, str]) -> Generator[None]:
|
|
55
|
+
"""Make ``trace`` the current context inside the block."""
|
|
56
|
+
token = _current.set(dict(trace))
|
|
57
|
+
try:
|
|
58
|
+
yield
|
|
59
|
+
finally:
|
|
60
|
+
_current.reset(token)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def parent_of(trace: Mapping[str, str]) -> tuple[str, str | None]:
|
|
64
|
+
"""(trace id, parent span id) for a new span: from the context's ``traceparent``, or a new trace."""
|
|
65
|
+
parts = trace.get("traceparent", "").split("-")
|
|
66
|
+
if len(parts) == 4 and len(parts[1]) == 32 and len(parts[2]) == 16:
|
|
67
|
+
return parts[1], parts[2]
|
|
68
|
+
return new_trace_id(), None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def make_span(
|
|
72
|
+
name: str,
|
|
73
|
+
attributes: Mapping[str, Any],
|
|
74
|
+
*,
|
|
75
|
+
trace_id: str,
|
|
76
|
+
span_id: str,
|
|
77
|
+
parent: str | None,
|
|
78
|
+
start: str,
|
|
79
|
+
end: str,
|
|
80
|
+
error: str | None = None,
|
|
81
|
+
events: list[dict[str, Any]] | None = None,
|
|
82
|
+
links: list[dict[str, Any]] | None = None,
|
|
83
|
+
) -> dict[str, Any]:
|
|
84
|
+
"""A span dict (kind ``internal``); ``error`` sets status ``error`` with that message."""
|
|
85
|
+
return {
|
|
86
|
+
"trace_id": trace_id,
|
|
87
|
+
"span_id": span_id,
|
|
88
|
+
"parent_span_id": parent,
|
|
89
|
+
"name": name,
|
|
90
|
+
"kind": "internal",
|
|
91
|
+
"start_time": start,
|
|
92
|
+
"end_time": end,
|
|
93
|
+
"status": {"code": "error", "message": error} if error else {"code": "ok", "message": ""},
|
|
94
|
+
"attributes": {"hone.schema_version": SCHEMA_VERSION, **attributes},
|
|
95
|
+
"events": events or [],
|
|
96
|
+
"resource": _resource(),
|
|
97
|
+
"links": links or [],
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _resource() -> dict[str, Any]:
|
|
102
|
+
from hone_flow import __version__ # noqa: PLC0415 - the package imports this module first
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
"service.name": os.environ.get("OTEL_SERVICE_NAME", "hone-flow"),
|
|
106
|
+
"hone.package": "hone-flow",
|
|
107
|
+
"hone.package.version": __version__,
|
|
108
|
+
"host.name": socket.gethostname(),
|
|
109
|
+
"process.pid": os.getpid(),
|
|
110
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Optional integrations, imported lazily (never by the core on import)."""
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""GPU leases from entry points: ``Workflow(gpu="hone_models")`` uses hone-models' GPU scheduler.
|
|
2
|
+
|
|
3
|
+
Any package can offer a ``GpuLease`` under the entry-point group ``hone.gpu_leases``; hone-models
|
|
4
|
+
registers ``hone_models``, hone-flow itself registers ``file_lock`` (``FileLockGpuLease``).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from importlib.metadata import entry_points
|
|
10
|
+
from typing import Any, cast
|
|
11
|
+
|
|
12
|
+
from hone_flow.errors import HoneFlowError
|
|
13
|
+
from hone_flow.ports import GpuLease
|
|
14
|
+
|
|
15
|
+
GROUP = "hone.gpu_leases"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def load_gpu_lease(name: str) -> GpuLease:
|
|
19
|
+
"""Load the ``GpuLease`` registered as ``name``; a registered class is instantiated without arguments.
|
|
20
|
+
|
|
21
|
+
>>> load_gpu_lease("file_lock").__class__.__name__
|
|
22
|
+
'FileLockGpuLease'
|
|
23
|
+
"""
|
|
24
|
+
found = {ep.name: ep for ep in entry_points(group=GROUP)}
|
|
25
|
+
if name not in found:
|
|
26
|
+
hint = " (pip install hone-models)" if name == "hone_models" else ""
|
|
27
|
+
raise HoneFlowError(
|
|
28
|
+
f"no GPU lease named {name!r} in entry points {GROUP}; found {sorted(found)}{hint}"
|
|
29
|
+
)
|
|
30
|
+
obj: Any = found[name].load()
|
|
31
|
+
lease = obj() if isinstance(obj, type) else obj
|
|
32
|
+
if not callable(getattr(lease, "lease", None)):
|
|
33
|
+
raise HoneFlowError(f"entry point {name!r} in {GROUP} is not a GpuLease (it has no lease() method)")
|
|
34
|
+
return cast(GpuLease, lease)
|