tasklite-engine 2.2.1__py3-none-any.whl → 3.0.0.dev1__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.
- tasklite/__init__.py +62 -204
- tasklite/__main__.py +205 -0
- tasklite/_report.py +562 -0
- tasklite/backend/__init__.py +20 -6
- tasklite/backend/base.py +431 -288
- tasklite/backend/memory.py +499 -340
- tasklite/backend/sqlite_backend.py +817 -617
- tasklite/edit.py +1276 -0
- tasklite/engine/__init__.py +7 -8
- tasklite/engine/_worker_bootstrap.py +221 -0
- tasklite/engine/_worker_runtime.py +229 -0
- tasklite/engine/admission.py +33 -89
- tasklite/engine/authorization.py +989 -0
- tasklite/engine/channel.py +831 -660
- tasklite/engine/completion.py +494 -289
- tasklite/engine/config.py +9 -139
- tasklite/engine/dispatch.py +424 -420
- tasklite/engine/governor.py +167 -265
- tasklite/engine/in_flight.py +32 -98
- tasklite/engine/launcher.py +1173 -0
- tasklite/engine/recovery.py +159 -312
- tasklite/engine/resource.py +240 -426
- tasklite/engine/runtime.py +236 -312
- tasklite/engine/scheduler.py +70 -98
- tasklite/engine/session.py +93 -76
- tasklite/engine/store.py +489 -685
- tasklite/engine/types.py +55 -71
- tasklite/engine/wait.py +38 -16
- tasklite/exceptions.py +4 -58
- tasklite/models/__init__.py +21 -6
- tasklite/models/artifact.py +433 -72
- tasklite/models/context.py +182 -206
- tasklite/models/graph.py +114 -0
- tasklite/models/job.py +117 -270
- tasklite/models/records.py +482 -0
- tasklite/models/result.py +246 -0
- tasklite/models/state.py +99 -125
- tasklite/models/task.py +52 -105
- tasklite/models/trigger.py +63 -3
- tasklite/pipeline.py +295 -558
- tasklite/resource.py +89 -0
- tasklite/selection.py +9 -97
- tasklite/snapshot.py +842 -0
- tasklite/testing.py +452 -41
- tasklite/utils/artifact.py +1093 -113
- tasklite/utils/clock.py +3 -3
- tasklite/utils/encoding.py +107 -31
- tasklite/utils/ipc.py +289 -243
- tasklite/utils/lockfile.py +76 -70
- tasklite/wrappers/__init__.py +2 -2
- tasklite/wrappers/discovery.py +161 -167
- tasklite/wrappers/http.py +142 -521
- tasklite_engine-3.0.0.dev1.dist-info/METADATA +313 -0
- tasklite_engine-3.0.0.dev1.dist-info/RECORD +60 -0
- tasklite/contrib/__init__.py +0 -7
- tasklite/engine/errorclass.py +0 -971
- tasklite/engine/ops.py +0 -363
- tasklite/engine/selection.py +0 -244
- tasklite/hooks.py +0 -69
- tasklite/models/attempt.py +0 -141
- tasklite/triggers.py +0 -46
- tasklite_engine-2.2.1.dist-info/METADATA +0 -370
- tasklite_engine-2.2.1.dist-info/RECORD +0 -55
- {tasklite_engine-2.2.1.dist-info → tasklite_engine-3.0.0.dev1.dist-info}/WHEEL +0 -0
- {tasklite_engine-2.2.1.dist-info → tasklite_engine-3.0.0.dev1.dist-info}/licenses/LICENSE +0 -0
- {tasklite_engine-2.2.1.dist-info → tasklite_engine-3.0.0.dev1.dist-info}/top_level.txt +0 -0
tasklite/_report.py
ADDED
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
"""StateSnapshot 可读报告生成(单快照与双快照增量对比,内部纯函数模块)。
|
|
2
|
+
|
|
3
|
+
不对外暴露为公开 API,仅由 ``StateSnapshot.report`` 调用;输出文本供人类
|
|
4
|
+
阅读展示,不作为跨版本编程解析契约。
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections import Counter
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
import statistics
|
|
11
|
+
from typing import TYPE_CHECKING, Any, Mapping
|
|
12
|
+
|
|
13
|
+
from .utils.clock import parse_utc_iso_to_us, utc_now_iso
|
|
14
|
+
from .utils.encoding import parse_job_uid
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from .snapshot import StateSnapshot
|
|
18
|
+
|
|
19
|
+
_STATUS_ORDER = ("pending", "running", "succeeded", "failed")
|
|
20
|
+
_OUTCOME_ORDER = ("success", "failure", "retry")
|
|
21
|
+
_DEFAULT_ERROR_MAX_LEN = 120
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class _DurationStats:
|
|
26
|
+
"""历史成功 Attempt 耗时统计(单位:秒)。"""
|
|
27
|
+
|
|
28
|
+
count: int
|
|
29
|
+
total_s: float
|
|
30
|
+
avg_s: float
|
|
31
|
+
p50_s: float
|
|
32
|
+
max_s: float
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class _RunningJobInfo:
|
|
37
|
+
"""当前在途 running 作业摘要。"""
|
|
38
|
+
|
|
39
|
+
job_uid: str
|
|
40
|
+
task_type: str
|
|
41
|
+
started_at: str | None
|
|
42
|
+
attempt_no: int | None
|
|
43
|
+
run_id: str | None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass(frozen=True)
|
|
47
|
+
class _FailedJobInfo:
|
|
48
|
+
"""当前失败作业摘要。"""
|
|
49
|
+
|
|
50
|
+
job_uid: str
|
|
51
|
+
task_type: str
|
|
52
|
+
settled_at: str | None
|
|
53
|
+
error_summary: str
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class _AttemptInfo:
|
|
58
|
+
"""单条 Attempt 轻量投影(用于区间增量对比与耗时聚合)。"""
|
|
59
|
+
|
|
60
|
+
key: tuple[str, int | None, str | None]
|
|
61
|
+
job_uid: str
|
|
62
|
+
task_type: str
|
|
63
|
+
outcome: str
|
|
64
|
+
duration_s: float | None
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclass
|
|
68
|
+
class _TaskTypeCounts:
|
|
69
|
+
"""单个 task_type 下的四态计数。"""
|
|
70
|
+
|
|
71
|
+
pending: int = 0
|
|
72
|
+
running: int = 0
|
|
73
|
+
succeeded: int = 0
|
|
74
|
+
failed: int = 0
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def total(self) -> int:
|
|
78
|
+
return self.pending + self.running + self.succeeded + self.failed
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@dataclass
|
|
82
|
+
class _SnapshotSummary:
|
|
83
|
+
"""单份 StateSnapshot 的完整统计聚合结果。"""
|
|
84
|
+
|
|
85
|
+
total_jobs: int = 0
|
|
86
|
+
status_counts: dict[str, int] = field(
|
|
87
|
+
default_factory=lambda: {s: 0 for s in _STATUS_ORDER}
|
|
88
|
+
)
|
|
89
|
+
succeeded_execution: int = 0
|
|
90
|
+
succeeded_manual: int = 0
|
|
91
|
+
by_task_type: dict[str, _TaskTypeCounts] = field(default_factory=dict)
|
|
92
|
+
durations_by_task_type: dict[str, list[float]] = field(default_factory=dict)
|
|
93
|
+
duration_stats_by_task_type: dict[str, _DurationStats] = field(default_factory=dict)
|
|
94
|
+
running_jobs: list[_RunningJobInfo] = field(default_factory=list)
|
|
95
|
+
failed_jobs: list[_FailedJobInfo] = field(default_factory=list)
|
|
96
|
+
job_statuses: dict[str, str] = field(default_factory=dict)
|
|
97
|
+
failed_by_uid: dict[str, _FailedJobInfo] = field(default_factory=dict)
|
|
98
|
+
attempts: list[_AttemptInfo] = field(default_factory=list)
|
|
99
|
+
attempt_keys: set[tuple[str, int | None, str | None]] = field(default_factory=set)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@dataclass
|
|
103
|
+
class _SnapshotDelta:
|
|
104
|
+
"""双快照 first -> second 区间增量变化。"""
|
|
105
|
+
|
|
106
|
+
total_delta: int
|
|
107
|
+
status_deltas: dict[str, int]
|
|
108
|
+
succeeded_execution_delta: int
|
|
109
|
+
succeeded_manual_delta: int
|
|
110
|
+
transitions: list[tuple[str, int]]
|
|
111
|
+
new_attempts_total: int
|
|
112
|
+
new_attempts_by_outcome: dict[str, int]
|
|
113
|
+
new_success_duration_stats: _DurationStats | None
|
|
114
|
+
newly_failed: list[tuple[_FailedJobInfo, str]]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _extract_task_type(uid: str, job_cfg: Any) -> str:
|
|
118
|
+
"""优先取配置快照中的 task_type,缺失时从 job_uid 反解。"""
|
|
119
|
+
if isinstance(job_cfg, Mapping):
|
|
120
|
+
tt = job_cfg.get("task_type")
|
|
121
|
+
if isinstance(tt, str) and tt:
|
|
122
|
+
return tt
|
|
123
|
+
try:
|
|
124
|
+
tt, _ = parse_job_uid(uid)
|
|
125
|
+
return tt
|
|
126
|
+
except Exception:
|
|
127
|
+
return "<unknown>"
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _classify_succeeded_source(job: Mapping[str, Any]) -> str:
|
|
131
|
+
"""区分成功记录来源:manual 或 execution(兼容旧记录空 annotations)。"""
|
|
132
|
+
result = job.get("result")
|
|
133
|
+
if isinstance(result, Mapping):
|
|
134
|
+
annotations = result.get("annotations")
|
|
135
|
+
if isinstance(annotations, Mapping):
|
|
136
|
+
if annotations.get("completion_source") == "manual":
|
|
137
|
+
return "manual"
|
|
138
|
+
return "execution"
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _compute_duration_seconds(started_at: Any, settled_at: Any) -> float | None:
|
|
142
|
+
"""按微秒差计算耗时秒数;时间戳缺失/非法返回 None,时钟回拨归零。"""
|
|
143
|
+
if not isinstance(started_at, str) or not started_at:
|
|
144
|
+
return None
|
|
145
|
+
if not isinstance(settled_at, str) or not settled_at:
|
|
146
|
+
return None
|
|
147
|
+
try:
|
|
148
|
+
start_us = parse_utc_iso_to_us(started_at)
|
|
149
|
+
end_us = parse_utc_iso_to_us(settled_at)
|
|
150
|
+
except (TypeError, ValueError):
|
|
151
|
+
return None
|
|
152
|
+
return max(0.0, (end_us - start_us) / 1_000_000.0)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _compute_duration_stats(samples: list[float]) -> _DurationStats | None:
|
|
156
|
+
if not samples:
|
|
157
|
+
return None
|
|
158
|
+
ordered = sorted(samples)
|
|
159
|
+
total_s = sum(ordered)
|
|
160
|
+
return _DurationStats(
|
|
161
|
+
count=len(ordered),
|
|
162
|
+
total_s=total_s,
|
|
163
|
+
avg_s=total_s / len(ordered),
|
|
164
|
+
p50_s=float(statistics.median(ordered)),
|
|
165
|
+
max_s=ordered[-1],
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _extract_error_summary(
|
|
170
|
+
job: Mapping[str, Any], max_len: int = _DEFAULT_ERROR_MAX_LEN
|
|
171
|
+
) -> str:
|
|
172
|
+
"""提取失败作业的单行可读错误摘要并按长度截断。"""
|
|
173
|
+
result = job.get("result")
|
|
174
|
+
if not isinstance(result, Mapping):
|
|
175
|
+
return "(no error details)"
|
|
176
|
+
msg = result.get("message")
|
|
177
|
+
msg_str = str(msg).strip() if msg is not None else ""
|
|
178
|
+
exc = result.get("exception")
|
|
179
|
+
exc_str = ""
|
|
180
|
+
if isinstance(exc, Mapping):
|
|
181
|
+
exc_type = str(exc.get("type") or "").strip()
|
|
182
|
+
exc_msg = str(exc.get("message") or "").strip()
|
|
183
|
+
if exc_type and exc_msg:
|
|
184
|
+
exc_str = f"{exc_type}: {exc_msg}"
|
|
185
|
+
elif exc_type:
|
|
186
|
+
exc_str = exc_type
|
|
187
|
+
elif exc_msg:
|
|
188
|
+
exc_str = exc_msg
|
|
189
|
+
if msg_str and exc_str:
|
|
190
|
+
if exc_str in msg_str:
|
|
191
|
+
raw = msg_str
|
|
192
|
+
elif msg_str in exc_str:
|
|
193
|
+
raw = exc_str
|
|
194
|
+
else:
|
|
195
|
+
raw = f"{msg_str} ({exc_str})"
|
|
196
|
+
else:
|
|
197
|
+
raw = msg_str or exc_str or "(no error details)"
|
|
198
|
+
single_line = " ".join(raw.splitlines()).strip()
|
|
199
|
+
if not single_line:
|
|
200
|
+
return "(no error details)"
|
|
201
|
+
if len(single_line) > max_len:
|
|
202
|
+
return single_line[: max_len - 3] + "..."
|
|
203
|
+
return single_line
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _format_duration(seconds: float) -> str:
|
|
207
|
+
"""将秒数格式化为紧凑可读时长。"""
|
|
208
|
+
clamped = max(0.0, seconds)
|
|
209
|
+
if clamped < 10.0:
|
|
210
|
+
return f"{clamped:.2f}s"
|
|
211
|
+
if clamped < 60.0:
|
|
212
|
+
return f"{clamped:.1f}s"
|
|
213
|
+
if clamped < 3600.0:
|
|
214
|
+
mins, rem = divmod(clamped, 60.0)
|
|
215
|
+
return f"{int(mins)}m {rem:04.1f}s"
|
|
216
|
+
hours, rem = divmod(clamped, 3600.0)
|
|
217
|
+
mins, secs = divmod(rem, 60.0)
|
|
218
|
+
return f"{int(hours)}h {int(mins):02d}m {int(secs):02d}s"
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _format_signed(delta: int) -> str:
|
|
222
|
+
if delta > 0:
|
|
223
|
+
return f"+{delta}"
|
|
224
|
+
if delta < 0:
|
|
225
|
+
return str(delta)
|
|
226
|
+
return "+0"
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def _summarize_snapshot(snapshot: StateSnapshot) -> _SnapshotSummary:
|
|
230
|
+
"""单次扫描快照内的 Job 与 Attempt,构造聚合统计。"""
|
|
231
|
+
summary = _SnapshotSummary()
|
|
232
|
+
|
|
233
|
+
for job in snapshot.iter_jobs():
|
|
234
|
+
uid = str(job.get("job_uid") or "")
|
|
235
|
+
status = str(job.get("status") or "")
|
|
236
|
+
task_type = _extract_task_type(uid, job.get("job"))
|
|
237
|
+
|
|
238
|
+
summary.total_jobs += 1
|
|
239
|
+
summary.job_statuses[uid] = status
|
|
240
|
+
if status in summary.status_counts:
|
|
241
|
+
summary.status_counts[status] += 1
|
|
242
|
+
|
|
243
|
+
tt_counts = summary.by_task_type.setdefault(task_type, _TaskTypeCounts())
|
|
244
|
+
if status == "pending":
|
|
245
|
+
tt_counts.pending += 1
|
|
246
|
+
elif status == "running":
|
|
247
|
+
tt_counts.running += 1
|
|
248
|
+
summary.running_jobs.append(
|
|
249
|
+
_RunningJobInfo(
|
|
250
|
+
job_uid=uid,
|
|
251
|
+
task_type=task_type,
|
|
252
|
+
started_at=job.get("started_at"),
|
|
253
|
+
attempt_no=job.get("attempt_no"),
|
|
254
|
+
run_id=job.get("run_id"),
|
|
255
|
+
)
|
|
256
|
+
)
|
|
257
|
+
elif status == "succeeded":
|
|
258
|
+
tt_counts.succeeded += 1
|
|
259
|
+
if _classify_succeeded_source(job) == "manual":
|
|
260
|
+
summary.succeeded_manual += 1
|
|
261
|
+
else:
|
|
262
|
+
summary.succeeded_execution += 1
|
|
263
|
+
elif status == "failed":
|
|
264
|
+
tt_counts.failed += 1
|
|
265
|
+
info = _FailedJobInfo(
|
|
266
|
+
job_uid=uid,
|
|
267
|
+
task_type=task_type,
|
|
268
|
+
settled_at=job.get("settled_at"),
|
|
269
|
+
error_summary=_extract_error_summary(job),
|
|
270
|
+
)
|
|
271
|
+
summary.failed_jobs.append(info)
|
|
272
|
+
summary.failed_by_uid[uid] = info
|
|
273
|
+
|
|
274
|
+
summary.running_jobs.sort(
|
|
275
|
+
key=lambda item: (item.started_at is None, item.started_at or "", item.job_uid)
|
|
276
|
+
)
|
|
277
|
+
summary.failed_jobs.sort(key=lambda item: item.job_uid)
|
|
278
|
+
|
|
279
|
+
for att in snapshot.iter_attempts():
|
|
280
|
+
uid = str(att.get("job_uid") or "")
|
|
281
|
+
outcome = str(att.get("outcome") or "")
|
|
282
|
+
task_type = _extract_task_type(uid, att.get("job"))
|
|
283
|
+
duration_s = (
|
|
284
|
+
_compute_duration_seconds(att.get("started_at"), att.get("settled_at"))
|
|
285
|
+
if outcome == "success"
|
|
286
|
+
else None
|
|
287
|
+
)
|
|
288
|
+
if duration_s is not None:
|
|
289
|
+
summary.durations_by_task_type.setdefault(task_type, []).append(duration_s)
|
|
290
|
+
key = (uid, att.get("attempt_no"), att.get("run_id"))
|
|
291
|
+
att_info = _AttemptInfo(
|
|
292
|
+
key=key,
|
|
293
|
+
job_uid=uid,
|
|
294
|
+
task_type=task_type,
|
|
295
|
+
outcome=outcome,
|
|
296
|
+
duration_s=duration_s,
|
|
297
|
+
)
|
|
298
|
+
summary.attempts.append(att_info)
|
|
299
|
+
summary.attempt_keys.add(key)
|
|
300
|
+
|
|
301
|
+
for task_type, samples in summary.durations_by_task_type.items():
|
|
302
|
+
stats = _compute_duration_stats(samples)
|
|
303
|
+
if stats is not None:
|
|
304
|
+
summary.duration_stats_by_task_type[task_type] = stats
|
|
305
|
+
|
|
306
|
+
return summary
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _compute_delta(
|
|
310
|
+
before: _SnapshotSummary, after: _SnapshotSummary
|
|
311
|
+
) -> _SnapshotDelta:
|
|
312
|
+
"""计算 first -> second 区间增量。"""
|
|
313
|
+
status_deltas = {
|
|
314
|
+
st: after.status_counts[st] - before.status_counts[st]
|
|
315
|
+
for st in _STATUS_ORDER
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
transition_counter: Counter[str] = Counter()
|
|
319
|
+
all_uids = set(before.job_statuses) | set(after.job_statuses)
|
|
320
|
+
newly_failed: list[tuple[_FailedJobInfo, str]] = []
|
|
321
|
+
|
|
322
|
+
for uid in sorted(all_uids):
|
|
323
|
+
b_status = before.job_statuses.get(uid)
|
|
324
|
+
a_status = after.job_statuses.get(uid)
|
|
325
|
+
if b_status == a_status:
|
|
326
|
+
continue
|
|
327
|
+
src = b_status if b_status is not None else "(new)"
|
|
328
|
+
dst = a_status if a_status is not None else "(removed)"
|
|
329
|
+
transition_counter[f"{src} -> {dst}"] += 1
|
|
330
|
+
if a_status == "failed" and b_status != "failed":
|
|
331
|
+
failed_info = after.failed_by_uid.get(uid)
|
|
332
|
+
if failed_info is not None:
|
|
333
|
+
newly_failed.append((failed_info, f"{src} -> failed"))
|
|
334
|
+
|
|
335
|
+
transitions = sorted(
|
|
336
|
+
transition_counter.items(), key=lambda pair: (-pair[1], pair[0])
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
new_attempts_by_outcome: dict[str, int] = {o: 0 for o in _OUTCOME_ORDER}
|
|
340
|
+
new_success_durations: list[float] = []
|
|
341
|
+
new_attempts_total = 0
|
|
342
|
+
|
|
343
|
+
for att in after.attempts:
|
|
344
|
+
if att.key in before.attempt_keys:
|
|
345
|
+
continue
|
|
346
|
+
new_attempts_total += 1
|
|
347
|
+
if att.outcome in new_attempts_by_outcome:
|
|
348
|
+
new_attempts_by_outcome[att.outcome] += 1
|
|
349
|
+
else:
|
|
350
|
+
new_attempts_by_outcome[att.outcome] = (
|
|
351
|
+
new_attempts_by_outcome.get(att.outcome, 0) + 1
|
|
352
|
+
)
|
|
353
|
+
if att.outcome == "success" and att.duration_s is not None:
|
|
354
|
+
new_success_durations.append(att.duration_s)
|
|
355
|
+
|
|
356
|
+
return _SnapshotDelta(
|
|
357
|
+
total_delta=after.total_jobs - before.total_jobs,
|
|
358
|
+
status_deltas=status_deltas,
|
|
359
|
+
succeeded_execution_delta=after.succeeded_execution - before.succeeded_execution,
|
|
360
|
+
succeeded_manual_delta=after.succeeded_manual - before.succeeded_manual,
|
|
361
|
+
transitions=transitions,
|
|
362
|
+
new_attempts_total=new_attempts_total,
|
|
363
|
+
new_attempts_by_outcome=new_attempts_by_outcome,
|
|
364
|
+
new_success_duration_stats=_compute_duration_stats(new_success_durations),
|
|
365
|
+
newly_failed=newly_failed,
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def _format_running_line(
|
|
370
|
+
item: _RunningJobInfo,
|
|
371
|
+
now_us: int,
|
|
372
|
+
baseline: _DurationStats | None,
|
|
373
|
+
) -> str:
|
|
374
|
+
"""格式化单条在途 running 作业的已运行时间与 ETA。"""
|
|
375
|
+
attempt_part = f", attempt={item.attempt_no}" if item.attempt_no is not None else ""
|
|
376
|
+
elapsed_s: float | None = None
|
|
377
|
+
if item.started_at:
|
|
378
|
+
try:
|
|
379
|
+
started_us = parse_utc_iso_to_us(item.started_at)
|
|
380
|
+
elapsed_s = max(0.0, (now_us - started_us) / 1_000_000.0)
|
|
381
|
+
except (TypeError, ValueError):
|
|
382
|
+
elapsed_s = None
|
|
383
|
+
|
|
384
|
+
if elapsed_s is None:
|
|
385
|
+
return (
|
|
386
|
+
f" - {item.job_uid} (task_type={item.task_type}{attempt_part}): "
|
|
387
|
+
"elapsed=N/A, ETA=N/A (missing started_at)"
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
elapsed_text = _format_duration(elapsed_s)
|
|
391
|
+
if baseline is None:
|
|
392
|
+
return (
|
|
393
|
+
f" - {item.job_uid} (task_type={item.task_type}{attempt_part}): "
|
|
394
|
+
f"elapsed={elapsed_text}, ETA=N/A (no history)"
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
p50_text = _format_duration(baseline.p50_s)
|
|
398
|
+
if elapsed_s <= baseline.p50_s:
|
|
399
|
+
remaining_text = _format_duration(baseline.p50_s - elapsed_s)
|
|
400
|
+
eta_text = f"~{remaining_text} (P50={p50_text})"
|
|
401
|
+
else:
|
|
402
|
+
overrun_text = _format_duration(elapsed_s - baseline.p50_s)
|
|
403
|
+
eta_text = f"~0.00s (+{overrun_text} over P50={p50_text})"
|
|
404
|
+
|
|
405
|
+
return (
|
|
406
|
+
f" - {item.job_uid} (task_type={item.task_type}{attempt_part}): "
|
|
407
|
+
f"elapsed={elapsed_text}, ETA={eta_text}"
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def render_snapshot_report(
|
|
412
|
+
first: StateSnapshot,
|
|
413
|
+
second: StateSnapshot | None = None,
|
|
414
|
+
) -> str:
|
|
415
|
+
"""渲染单快照或双快照对比报告(共享同一主干结构)。"""
|
|
416
|
+
now_us = parse_utc_iso_to_us(utc_now_iso())
|
|
417
|
+
|
|
418
|
+
if second is None:
|
|
419
|
+
main = _summarize_snapshot(first)
|
|
420
|
+
delta: _SnapshotDelta | None = None
|
|
421
|
+
else:
|
|
422
|
+
before = _summarize_snapshot(first)
|
|
423
|
+
main = _summarize_snapshot(second)
|
|
424
|
+
delta = _compute_delta(before, main)
|
|
425
|
+
|
|
426
|
+
lines: list[str] = []
|
|
427
|
+
if delta is None:
|
|
428
|
+
lines.append("TaskLite State Report")
|
|
429
|
+
lines.append("=====================")
|
|
430
|
+
else:
|
|
431
|
+
lines.append("TaskLite State Report (Comparison: before -> after)")
|
|
432
|
+
lines.append("===================================================")
|
|
433
|
+
|
|
434
|
+
# 1. 四态计数汇总(含 succeeded 的 execution / manual 构成)
|
|
435
|
+
if delta is None:
|
|
436
|
+
lines.append(f"Summary (total: {main.total_jobs}):")
|
|
437
|
+
lines.append(f" pending: {main.status_counts['pending']}")
|
|
438
|
+
lines.append(f" running: {main.status_counts['running']}")
|
|
439
|
+
lines.append(
|
|
440
|
+
f" succeeded: {main.status_counts['succeeded']} "
|
|
441
|
+
f"(execution: {main.succeeded_execution}, manual: {main.succeeded_manual})"
|
|
442
|
+
)
|
|
443
|
+
lines.append(f" failed: {main.status_counts['failed']}")
|
|
444
|
+
else:
|
|
445
|
+
lines.append(
|
|
446
|
+
f"Summary (total: {main.total_jobs}, {_format_signed(delta.total_delta)}):"
|
|
447
|
+
)
|
|
448
|
+
lines.append(
|
|
449
|
+
f" pending: {main.status_counts['pending']} "
|
|
450
|
+
f"({_format_signed(delta.status_deltas['pending'])})"
|
|
451
|
+
)
|
|
452
|
+
lines.append(
|
|
453
|
+
f" running: {main.status_counts['running']} "
|
|
454
|
+
f"({_format_signed(delta.status_deltas['running'])})"
|
|
455
|
+
)
|
|
456
|
+
lines.append(
|
|
457
|
+
f" succeeded: {main.status_counts['succeeded']} "
|
|
458
|
+
f"({_format_signed(delta.status_deltas['succeeded'])}) "
|
|
459
|
+
f"(execution: {main.succeeded_execution} "
|
|
460
|
+
f"({_format_signed(delta.succeeded_execution_delta)}), "
|
|
461
|
+
f"manual: {main.succeeded_manual} "
|
|
462
|
+
f"({_format_signed(delta.succeeded_manual_delta)}))"
|
|
463
|
+
)
|
|
464
|
+
lines.append(
|
|
465
|
+
f" failed: {main.status_counts['failed']} "
|
|
466
|
+
f"({_format_signed(delta.status_deltas['failed'])})"
|
|
467
|
+
)
|
|
468
|
+
|
|
469
|
+
# 2. 双快照区间增量块(仅对比模式叠加)
|
|
470
|
+
if delta is not None:
|
|
471
|
+
lines.append("")
|
|
472
|
+
lines.append("Changes (before -> after):")
|
|
473
|
+
lines.append(" Transitions:")
|
|
474
|
+
if delta.transitions:
|
|
475
|
+
for label, count in delta.transitions:
|
|
476
|
+
lines.append(f" {label}: {count}")
|
|
477
|
+
else:
|
|
478
|
+
lines.append(" (no state transitions)")
|
|
479
|
+
|
|
480
|
+
outcome_parts = [
|
|
481
|
+
f"{name}={delta.new_attempts_by_outcome.get(name, 0)}"
|
|
482
|
+
for name in _OUTCOME_ORDER
|
|
483
|
+
]
|
|
484
|
+
for extra_outcome in sorted(
|
|
485
|
+
set(delta.new_attempts_by_outcome) - set(_OUTCOME_ORDER)
|
|
486
|
+
):
|
|
487
|
+
outcome_parts.append(
|
|
488
|
+
f"{extra_outcome}={delta.new_attempts_by_outcome[extra_outcome]}"
|
|
489
|
+
)
|
|
490
|
+
if delta.new_success_duration_stats is not None:
|
|
491
|
+
ds = delta.new_success_duration_stats
|
|
492
|
+
dur_part = (
|
|
493
|
+
f"success duration (n={ds.count}): "
|
|
494
|
+
f"total={_format_duration(ds.total_s)}, "
|
|
495
|
+
f"Avg={_format_duration(ds.avg_s)}, "
|
|
496
|
+
f"P50={_format_duration(ds.p50_s)}, "
|
|
497
|
+
f"Max={_format_duration(ds.max_s)}"
|
|
498
|
+
)
|
|
499
|
+
else:
|
|
500
|
+
dur_part = "success duration: N/A"
|
|
501
|
+
lines.append(
|
|
502
|
+
f" New Attempts ({delta.new_attempts_total}): "
|
|
503
|
+
f"{', '.join(outcome_parts)} | {dur_part}"
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
lines.append(f" Newly Failed Jobs ({len(delta.newly_failed)}):")
|
|
507
|
+
if delta.newly_failed:
|
|
508
|
+
for failed_info, transition_label in delta.newly_failed:
|
|
509
|
+
lines.append(
|
|
510
|
+
f" - {failed_info.job_uid} ({transition_label}): "
|
|
511
|
+
f"{failed_info.error_summary}"
|
|
512
|
+
)
|
|
513
|
+
else:
|
|
514
|
+
lines.append(" (none)")
|
|
515
|
+
|
|
516
|
+
# 3. 各 task_type 进度与历史成功耗时统计
|
|
517
|
+
lines.append("")
|
|
518
|
+
lines.append("By Task Type:")
|
|
519
|
+
all_task_types = sorted(
|
|
520
|
+
set(main.by_task_type) | set(main.duration_stats_by_task_type)
|
|
521
|
+
)
|
|
522
|
+
if not all_task_types:
|
|
523
|
+
lines.append(" (none)")
|
|
524
|
+
else:
|
|
525
|
+
for task_type in all_task_types:
|
|
526
|
+
counts = main.by_task_type.get(task_type, _TaskTypeCounts())
|
|
527
|
+
stats = main.duration_stats_by_task_type.get(task_type)
|
|
528
|
+
if stats is not None:
|
|
529
|
+
stats_part = (
|
|
530
|
+
f"success duration (n={stats.count}): "
|
|
531
|
+
f"Avg={_format_duration(stats.avg_s)}, "
|
|
532
|
+
f"P50={_format_duration(stats.p50_s)}, "
|
|
533
|
+
f"Max={_format_duration(stats.max_s)}"
|
|
534
|
+
)
|
|
535
|
+
else:
|
|
536
|
+
stats_part = "success duration: N/A"
|
|
537
|
+
lines.append(
|
|
538
|
+
f" {task_type}: pending={counts.pending}, running={counts.running}, "
|
|
539
|
+
f"succeeded={counts.succeeded}, failed={counts.failed} "
|
|
540
|
+
f"(total={counts.total}) | {stats_part}"
|
|
541
|
+
)
|
|
542
|
+
|
|
543
|
+
# 4. 在途 running 作业明细与 ETA
|
|
544
|
+
lines.append("")
|
|
545
|
+
lines.append(f"Running Jobs ({len(main.running_jobs)}):")
|
|
546
|
+
if not main.running_jobs:
|
|
547
|
+
lines.append(" (none)")
|
|
548
|
+
else:
|
|
549
|
+
for running_item in main.running_jobs:
|
|
550
|
+
baseline = main.duration_stats_by_task_type.get(running_item.task_type)
|
|
551
|
+
lines.append(_format_running_line(running_item, now_us, baseline))
|
|
552
|
+
|
|
553
|
+
# 5. 当前 failed 作业摘要
|
|
554
|
+
lines.append("")
|
|
555
|
+
lines.append(f"Failed Jobs ({len(main.failed_jobs)}):")
|
|
556
|
+
if not main.failed_jobs:
|
|
557
|
+
lines.append(" (none)")
|
|
558
|
+
else:
|
|
559
|
+
for failed_item in main.failed_jobs:
|
|
560
|
+
lines.append(f" - {failed_item.job_uid}: {failed_item.error_summary}")
|
|
561
|
+
|
|
562
|
+
return "\n".join(lines)
|
tasklite/backend/__init__.py
CHANGED
|
@@ -1,23 +1,37 @@
|
|
|
1
1
|
"""持久层:抽象契约与 SQLite / 内存双实现(ADR-0004)。
|
|
2
2
|
|
|
3
3
|
分层红线:本包属核心层,严禁 import ``tasklite/engine/`` 与
|
|
4
|
-
``tasklite/wrappers
|
|
4
|
+
``tasklite/wrappers/``;可依赖
|
|
5
5
|
``tasklite/models/`` 与 ``tasklite/utils/``。
|
|
6
6
|
"""
|
|
7
7
|
from .base import (
|
|
8
8
|
AbstractStateBackend,
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
FailureEntry,
|
|
10
|
+
GraphEditCommit,
|
|
11
|
+
RunningAttemptRecovery,
|
|
12
|
+
validate_graph_edit_commit,
|
|
11
13
|
validate_queue_replacement,
|
|
14
|
+
validate_recovery_attempt_state,
|
|
12
15
|
)
|
|
13
16
|
from .memory import InMemoryStateBackend
|
|
14
|
-
from .sqlite_backend import
|
|
17
|
+
from .sqlite_backend import (
|
|
18
|
+
SQLiteStateBackend,
|
|
19
|
+
TARGET_SCHEMA_VERSION,
|
|
20
|
+
open_existing_sqlite_readonly,
|
|
21
|
+
verify_existing_sqlite_database,
|
|
22
|
+
)
|
|
15
23
|
|
|
16
24
|
__all__ = [
|
|
17
25
|
"AbstractStateBackend",
|
|
26
|
+
"FailureEntry",
|
|
27
|
+
"GraphEditCommit",
|
|
18
28
|
"InMemoryStateBackend",
|
|
29
|
+
"RunningAttemptRecovery",
|
|
19
30
|
"SQLiteStateBackend",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
31
|
+
"TARGET_SCHEMA_VERSION",
|
|
32
|
+
"open_existing_sqlite_readonly",
|
|
33
|
+
"validate_graph_edit_commit",
|
|
22
34
|
"validate_queue_replacement",
|
|
35
|
+
"validate_recovery_attempt_state",
|
|
36
|
+
"verify_existing_sqlite_database",
|
|
23
37
|
]
|