tasklite-engine 3.0.0.dev0__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 +42 -18
- tasklite/__main__.py +205 -0
- tasklite/_report.py +562 -0
- tasklite/backend/__init__.py +19 -2
- tasklite/backend/base.py +327 -156
- tasklite/backend/memory.py +261 -339
- tasklite/backend/sqlite_backend.py +416 -488
- tasklite/edit.py +1276 -0
- tasklite/engine/__init__.py +6 -7
- tasklite/engine/_worker_bootstrap.py +221 -0
- tasklite/engine/_worker_runtime.py +229 -0
- tasklite/engine/admission.py +20 -114
- tasklite/engine/authorization.py +989 -0
- tasklite/engine/channel.py +531 -468
- tasklite/engine/completion.py +193 -275
- tasklite/engine/config.py +5 -11
- tasklite/engine/dispatch.py +299 -384
- tasklite/engine/governor.py +134 -69
- tasklite/engine/in_flight.py +20 -88
- tasklite/engine/launcher.py +1173 -0
- tasklite/engine/recovery.py +147 -100
- tasklite/engine/resource.py +50 -103
- tasklite/engine/runtime.py +158 -525
- tasklite/engine/scheduler.py +9 -46
- tasklite/engine/session.py +60 -15
- tasklite/engine/store.py +235 -462
- tasklite/engine/types.py +29 -40
- tasklite/engine/wait.py +8 -0
- tasklite/exceptions.py +0 -1
- tasklite/models/artifact.py +84 -62
- tasklite/models/context.py +37 -30
- tasklite/models/graph.py +114 -0
- tasklite/models/job.py +3 -5
- tasklite/models/records.py +482 -0
- tasklite/models/state.py +19 -125
- tasklite/models/trigger.py +63 -3
- tasklite/pipeline.py +92 -100
- tasklite/resource.py +79 -2
- tasklite/snapshot.py +608 -405
- tasklite/testing.py +424 -11
- tasklite/utils/artifact.py +1091 -179
- tasklite/utils/encoding.py +21 -40
- tasklite/utils/ipc.py +49 -1
- tasklite/utils/lockfile.py +50 -19
- tasklite/wrappers/__init__.py +1 -1
- tasklite/wrappers/discovery.py +13 -16
- tasklite/wrappers/http.py +77 -43
- {tasklite_engine-3.0.0.dev0.dist-info → tasklite_engine-3.0.0.dev1.dist-info}/METADATA +22 -16
- tasklite_engine-3.0.0.dev1.dist-info/RECORD +60 -0
- tasklite/contrib/__init__.py +0 -7
- tasklite/engine/execution_access.py +0 -39
- tasklite/engine/files.py +0 -52
- tasklite/engine/ops.py +0 -25
- tasklite/engine/selection.py +0 -186
- tasklite/hooks.py +0 -49
- tasklite/triggers.py +0 -47
- tasklite_engine-3.0.0.dev0.dist-info/RECORD +0 -58
- {tasklite_engine-3.0.0.dev0.dist-info → tasklite_engine-3.0.0.dev1.dist-info}/WHEEL +0 -0
- {tasklite_engine-3.0.0.dev0.dist-info → tasklite_engine-3.0.0.dev1.dist-info}/licenses/LICENSE +0 -0
- {tasklite_engine-3.0.0.dev0.dist-info → tasklite_engine-3.0.0.dev1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
"""Job 快照与终态档案载体单一事实源。
|
|
2
|
+
|
|
3
|
+
统一承载:
|
|
4
|
+
1. 9 字段规范 ``job_snapshot`` 投影与从 ``Successes`` / ``Failures`` 档案反提快照;
|
|
5
|
+
2. Job 摄入期 Task→Job 四项规格继承与提交期 ``FileSpec`` 解析;
|
|
6
|
+
3. ``result``(4 键)、外层 ``files``(5 键)、``artifacts`` 以及执行态/人工态
|
|
7
|
+
``Successes`` / ``Failures`` 档案字典的规范构造与归一化。
|
|
8
|
+
|
|
9
|
+
分层红线:本模块位于 ``tasklite/models/``,严禁导入 ``engine`` / ``backend`` / ``wrappers``。
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import copy
|
|
14
|
+
import logging
|
|
15
|
+
from collections.abc import Mapping, Sequence
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
from typing import Any
|
|
18
|
+
|
|
19
|
+
from ..utils.artifact import PathPolicy
|
|
20
|
+
from ..utils.clock import utc_now_iso
|
|
21
|
+
from ..utils.encoding import parse_job_uid
|
|
22
|
+
from ..utils.jsonutil import dumps
|
|
23
|
+
from .artifact import FileSpec, resolve_job_files_config
|
|
24
|
+
from .job import inject_worker_resource
|
|
25
|
+
from .result import validate_strict_json_object
|
|
26
|
+
from .task import Task
|
|
27
|
+
|
|
28
|
+
logger = logging.getLogger("tasklite")
|
|
29
|
+
|
|
30
|
+
EFFECTIVE_JOB_CONFIG_KEYS: tuple[str, ...] = (
|
|
31
|
+
"task_type",
|
|
32
|
+
"job_id",
|
|
33
|
+
"payload",
|
|
34
|
+
"depends_on",
|
|
35
|
+
"files",
|
|
36
|
+
"resources",
|
|
37
|
+
"max_retries_per_run",
|
|
38
|
+
"timeout_seconds",
|
|
39
|
+
"trigger",
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def extract_effective_job_config(job_snapshot: Mapping[str, Any]) -> dict[str, Any]:
|
|
44
|
+
"""提取规范 9 字段作业配置投影(同 UID 幂等/冲突比较与只读快照共用)。"""
|
|
45
|
+
raw_files = job_snapshot.get("files") or {}
|
|
46
|
+
files_dict: dict[str, Any] = {}
|
|
47
|
+
if isinstance(raw_files, Mapping):
|
|
48
|
+
for name, spec in raw_files.items():
|
|
49
|
+
if hasattr(spec, "to_dict"):
|
|
50
|
+
files_dict[name] = spec.to_dict()
|
|
51
|
+
elif isinstance(spec, Mapping):
|
|
52
|
+
files_dict[name] = copy.deepcopy(dict(spec))
|
|
53
|
+
else:
|
|
54
|
+
files_dict[name] = spec
|
|
55
|
+
return {
|
|
56
|
+
"task_type": job_snapshot.get("task_type"),
|
|
57
|
+
"job_id": job_snapshot.get("job_id"),
|
|
58
|
+
"payload": copy.deepcopy(job_snapshot.get("payload") or {}),
|
|
59
|
+
"depends_on": list(job_snapshot.get("depends_on") or []),
|
|
60
|
+
"files": files_dict,
|
|
61
|
+
"resources": dict(job_snapshot.get("resources") or {}),
|
|
62
|
+
"max_retries_per_run": job_snapshot.get("max_retries_per_run"),
|
|
63
|
+
"timeout_seconds": job_snapshot.get("timeout_seconds"),
|
|
64
|
+
"trigger": job_snapshot.get("trigger"),
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def fallback_job_snapshot(uid: str) -> dict[str, Any]:
|
|
69
|
+
"""档案缺少 ``job_snapshot`` 时,按规范 UID 构造最小可恢复快照。"""
|
|
70
|
+
task_type, job_id = parse_job_uid(uid)
|
|
71
|
+
return {
|
|
72
|
+
"task_type": task_type,
|
|
73
|
+
"job_id": job_id,
|
|
74
|
+
"payload": {},
|
|
75
|
+
"depends_on": [],
|
|
76
|
+
"files": {},
|
|
77
|
+
"resources": {},
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def extract_job_snapshot_from_archive(
|
|
82
|
+
uid: str,
|
|
83
|
+
archive_meta: Mapping[str, Any],
|
|
84
|
+
*,
|
|
85
|
+
payload: Mapping[str, Any] | None = None,
|
|
86
|
+
fill_missing_identity: bool = False,
|
|
87
|
+
) -> dict[str, Any]:
|
|
88
|
+
"""从 ``Successes`` 或 ``Failures`` 档案反提 ``job_snapshot``(失败档案按需回填独立 payload)。"""
|
|
89
|
+
raw_snap = archive_meta.get("job_snapshot")
|
|
90
|
+
if isinstance(raw_snap, Mapping):
|
|
91
|
+
snap = copy.deepcopy(dict(raw_snap))
|
|
92
|
+
elif fill_missing_identity:
|
|
93
|
+
snap = fallback_job_snapshot(uid)
|
|
94
|
+
else:
|
|
95
|
+
snap = {}
|
|
96
|
+
if fill_missing_identity and ("task_type" not in snap or "job_id" not in snap):
|
|
97
|
+
task_type, job_id = parse_job_uid(uid)
|
|
98
|
+
snap.setdefault("task_type", task_type)
|
|
99
|
+
snap.setdefault("job_id", job_id)
|
|
100
|
+
if payload is not None and not snap.get("payload"):
|
|
101
|
+
snap["payload"] = copy.deepcopy(dict(payload))
|
|
102
|
+
return snap
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def normalize_job_intake_snapshot(
|
|
106
|
+
job_snapshot: dict[str, Any],
|
|
107
|
+
*,
|
|
108
|
+
task: Task | None,
|
|
109
|
+
base_dir: str | Path,
|
|
110
|
+
) -> dict[str, Any]:
|
|
111
|
+
"""摄入单点规范化:payload 严格隔离、Task 四项规格继承、提交期 FileSpec 解析与 worker 槽位注入。"""
|
|
112
|
+
job_snapshot["payload"] = validate_strict_json_object(
|
|
113
|
+
job_snapshot.get("payload"), field_name="payload"
|
|
114
|
+
)
|
|
115
|
+
merged_resources: dict[str, float] = (
|
|
116
|
+
dict(task.resources) if task is not None and task.resources else {}
|
|
117
|
+
)
|
|
118
|
+
merged_resources.update(job_snapshot.get("resources") or {})
|
|
119
|
+
job_snapshot["resources"] = merged_resources
|
|
120
|
+
|
|
121
|
+
if job_snapshot.get("max_retries_per_run") is None:
|
|
122
|
+
job_snapshot["max_retries_per_run"] = task.max_retries_per_run if task is not None else 3
|
|
123
|
+
if job_snapshot.get("timeout_seconds") is None:
|
|
124
|
+
job_snapshot["timeout_seconds"] = task.timeout_seconds if task is not None else 3600
|
|
125
|
+
if job_snapshot.get("trigger") is None:
|
|
126
|
+
job_snapshot["trigger"] = task.trigger if task is not None else "once"
|
|
127
|
+
|
|
128
|
+
if job_snapshot.get("depends_on") is None:
|
|
129
|
+
job_snapshot["depends_on"] = []
|
|
130
|
+
else:
|
|
131
|
+
job_snapshot["depends_on"] = list(job_snapshot["depends_on"])
|
|
132
|
+
|
|
133
|
+
job_snapshot["files"] = resolve_job_files_config(
|
|
134
|
+
job_snapshot.get("files"),
|
|
135
|
+
base_dir=base_dir,
|
|
136
|
+
depends_on=job_snapshot["depends_on"],
|
|
137
|
+
)
|
|
138
|
+
inject_worker_resource(job_snapshot)
|
|
139
|
+
return job_snapshot
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def extract_serializable_payload(job_snapshot: Mapping[str, Any] | None) -> dict[str, Any] | None:
|
|
143
|
+
"""提取可 JSON 序列化的业务 payload 快照(失败档案落盘用);不可序列化时降级为 None。"""
|
|
144
|
+
if not isinstance(job_snapshot, Mapping):
|
|
145
|
+
return None
|
|
146
|
+
payload = job_snapshot.get("payload")
|
|
147
|
+
if not isinstance(payload, dict):
|
|
148
|
+
return None
|
|
149
|
+
try:
|
|
150
|
+
dumps(payload)
|
|
151
|
+
except (TypeError, ValueError):
|
|
152
|
+
logger.warning(
|
|
153
|
+
f"Job payload for {job_snapshot.get('job_id', '?')} is not "
|
|
154
|
+
f"JSON-serializable; failure archive payload snapshot skipped."
|
|
155
|
+
)
|
|
156
|
+
return None
|
|
157
|
+
return payload
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def build_result_record(
|
|
161
|
+
*,
|
|
162
|
+
message: Any = None,
|
|
163
|
+
exception: Any = None,
|
|
164
|
+
metadata: Mapping[str, Any] | None = None,
|
|
165
|
+
annotations: Mapping[str, Any] | None = None,
|
|
166
|
+
) -> dict[str, Any]:
|
|
167
|
+
"""构造规范 4 键结算结果字典(message / exception / metadata / annotations)。"""
|
|
168
|
+
return {
|
|
169
|
+
"message": message,
|
|
170
|
+
"exception": copy.deepcopy(exception),
|
|
171
|
+
"metadata": copy.deepcopy(dict(metadata)) if isinstance(metadata, Mapping) else {},
|
|
172
|
+
"annotations": copy.deepcopy(dict(annotations)) if isinstance(annotations, Mapping) else {},
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def normalize_result_record(raw_result: Any) -> dict[str, Any] | None:
|
|
177
|
+
"""将任意原始结果归一化为规范 4 键字典形状(None 保持 None)。"""
|
|
178
|
+
if raw_result is None:
|
|
179
|
+
return None
|
|
180
|
+
if not isinstance(raw_result, Mapping):
|
|
181
|
+
return build_result_record(message=str(raw_result))
|
|
182
|
+
msg = raw_result.get("message")
|
|
183
|
+
if msg is not None:
|
|
184
|
+
msg = str(msg)
|
|
185
|
+
raw_exc = raw_result.get("exception")
|
|
186
|
+
exc_dict: dict[str, Any] | None = None
|
|
187
|
+
if isinstance(raw_exc, Mapping):
|
|
188
|
+
exc_dict = {
|
|
189
|
+
"type": str(raw_exc.get("type", "Exception")),
|
|
190
|
+
"message": str(raw_exc.get("message", "")),
|
|
191
|
+
"traceback": (
|
|
192
|
+
str(raw_exc["traceback"])
|
|
193
|
+
if raw_exc.get("traceback") is not None
|
|
194
|
+
else None
|
|
195
|
+
),
|
|
196
|
+
}
|
|
197
|
+
meta = raw_result.get("metadata")
|
|
198
|
+
ann = raw_result.get("annotations")
|
|
199
|
+
return {
|
|
200
|
+
"message": msg,
|
|
201
|
+
"exception": exc_dict,
|
|
202
|
+
"metadata": copy.deepcopy(dict(meta)) if isinstance(meta, Mapping) else {},
|
|
203
|
+
"annotations": copy.deepcopy(dict(ann)) if isinstance(ann, Mapping) else {},
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def normalize_outer_files(raw_files: Mapping[str, Any] | None) -> dict[str, dict[str, Any]]:
|
|
208
|
+
"""规范化外层 files(5 键:path / role / delete_existing / delete_on_failure / delete_on_success)。"""
|
|
209
|
+
if not isinstance(raw_files, Mapping) or not raw_files:
|
|
210
|
+
return {}
|
|
211
|
+
normalized: dict[str, dict[str, Any]] = {}
|
|
212
|
+
for name, spec in raw_files.items():
|
|
213
|
+
if hasattr(spec, "to_dict"):
|
|
214
|
+
d: Mapping[str, Any] = spec.to_dict()
|
|
215
|
+
elif isinstance(spec, Mapping):
|
|
216
|
+
d = spec
|
|
217
|
+
else:
|
|
218
|
+
continue
|
|
219
|
+
if d.get("path") is not None:
|
|
220
|
+
normalized[name] = {
|
|
221
|
+
"path": d["path"],
|
|
222
|
+
"role": d.get("role", "output"),
|
|
223
|
+
"delete_existing": bool(d.get("delete_existing", False)),
|
|
224
|
+
"delete_on_failure": bool(d.get("delete_on_failure", False)),
|
|
225
|
+
"delete_on_success": bool(d.get("delete_on_success", False)),
|
|
226
|
+
}
|
|
227
|
+
return normalized
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def build_artifacts_manifest(
|
|
231
|
+
declared_artifacts: Mapping[str, FileSpec | str] | None,
|
|
232
|
+
) -> dict[str, dict[str, str]]:
|
|
233
|
+
"""构造规范具名产出清单(name -> {'path': str})。"""
|
|
234
|
+
manifest: dict[str, dict[str, str]] = {}
|
|
235
|
+
if not declared_artifacts:
|
|
236
|
+
return manifest
|
|
237
|
+
for name, spec_or_path in declared_artifacts.items():
|
|
238
|
+
path_val = spec_or_path.path if isinstance(spec_or_path, FileSpec) else spec_or_path
|
|
239
|
+
if isinstance(name, str) and name and isinstance(path_val, str) and path_val:
|
|
240
|
+
manifest[name] = {"path": path_val}
|
|
241
|
+
return manifest
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _execution_envelope(
|
|
245
|
+
result: Mapping[str, Any],
|
|
246
|
+
files: Mapping[str, Any] | None,
|
|
247
|
+
) -> dict[str, Any]:
|
|
248
|
+
"""构造执行态统一结果壳(业务数据仅存 result.metadata,根部仅存框架字段)。"""
|
|
249
|
+
result_copy = copy.deepcopy(dict(result))
|
|
250
|
+
raw_ann = result_copy.get("annotations")
|
|
251
|
+
result_copy["annotations"] = {
|
|
252
|
+
**(dict(raw_ann) if isinstance(raw_ann, Mapping) else {}),
|
|
253
|
+
"completion_source": "execution",
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
"result": result_copy,
|
|
257
|
+
"annotations": {"completion_source": "execution"},
|
|
258
|
+
"files": copy.deepcopy(dict(files or {})),
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def build_execution_success_record(
|
|
263
|
+
*,
|
|
264
|
+
result: Mapping[str, Any],
|
|
265
|
+
files: Mapping[str, Any] | None = None,
|
|
266
|
+
run_id: str | None = None,
|
|
267
|
+
attempt_no: int = 1,
|
|
268
|
+
started_at: str | None = None,
|
|
269
|
+
settled_at: str | None = None,
|
|
270
|
+
declared_inputs: Sequence[str] = (),
|
|
271
|
+
declared_artifacts: Mapping[str, FileSpec | str] | None = None,
|
|
272
|
+
job_snapshot: Mapping[str, Any] | None = None,
|
|
273
|
+
prev_meta: Mapping[str, Any] | None = None,
|
|
274
|
+
) -> dict[str, Any]:
|
|
275
|
+
"""构造执行态 ``Successes`` 档案字典。"""
|
|
276
|
+
effective_settled_at = settled_at if settled_at is not None else utc_now_iso()
|
|
277
|
+
record = _execution_envelope(result, files)
|
|
278
|
+
record.update(
|
|
279
|
+
run_id=run_id or "run",
|
|
280
|
+
attempt_no=attempt_no,
|
|
281
|
+
started_at=started_at,
|
|
282
|
+
settled_at=effective_settled_at,
|
|
283
|
+
artifacts=build_artifacts_manifest(declared_artifacts),
|
|
284
|
+
last_succeeded_at=effective_settled_at,
|
|
285
|
+
)
|
|
286
|
+
if run_id is not None:
|
|
287
|
+
record["last_success_run_id"] = run_id
|
|
288
|
+
if job_snapshot is not None:
|
|
289
|
+
record["job_snapshot"] = copy.deepcopy(dict(job_snapshot))
|
|
290
|
+
if declared_inputs:
|
|
291
|
+
input_paths = list(dict.fromkeys(
|
|
292
|
+
path for path in declared_inputs if isinstance(path, str) and path
|
|
293
|
+
))
|
|
294
|
+
if input_paths:
|
|
295
|
+
record["inputs"] = input_paths
|
|
296
|
+
|
|
297
|
+
prev_count = 0
|
|
298
|
+
if isinstance(prev_meta, Mapping):
|
|
299
|
+
try:
|
|
300
|
+
prev_count = int(prev_meta.get("success_count", 0))
|
|
301
|
+
except (TypeError, ValueError):
|
|
302
|
+
prev_count = 0
|
|
303
|
+
record["success_count"] = prev_count + 1
|
|
304
|
+
return record
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def build_execution_failure_record(
|
|
308
|
+
metadata: Mapping[str, Any],
|
|
309
|
+
*,
|
|
310
|
+
job_snapshot: Mapping[str, Any] | None = None,
|
|
311
|
+
settled_at: str | None = None,
|
|
312
|
+
attempt_no: int | None = None,
|
|
313
|
+
run_id: str | None = None,
|
|
314
|
+
started_at: str | None = None,
|
|
315
|
+
files: Mapping[str, Any] | None = None,
|
|
316
|
+
result: Mapping[str, Any] | None = None,
|
|
317
|
+
execution_id: str | None = None,
|
|
318
|
+
) -> dict[str, Any]:
|
|
319
|
+
"""构造执行态/预检态/级联态 ``Failures`` 档案字典(无执行时不借用历史 Attempt)。"""
|
|
320
|
+
effective_settled_at = settled_at if settled_at is not None else utc_now_iso()
|
|
321
|
+
record = copy.deepcopy(dict(metadata))
|
|
322
|
+
record.update(
|
|
323
|
+
settled_at=effective_settled_at,
|
|
324
|
+
run_id=None,
|
|
325
|
+
attempt_no=None,
|
|
326
|
+
started_at=None,
|
|
327
|
+
files=copy.deepcopy(dict((job_snapshot or {}).get("files") or {})),
|
|
328
|
+
result=build_result_record(
|
|
329
|
+
message=metadata.get("error_message") or metadata.get("message"),
|
|
330
|
+
exception=metadata.get("exception"),
|
|
331
|
+
metadata={},
|
|
332
|
+
annotations=metadata.get("annotations") if isinstance(metadata.get("annotations"), Mapping) else {},
|
|
333
|
+
),
|
|
334
|
+
)
|
|
335
|
+
if job_snapshot is not None:
|
|
336
|
+
record["job_snapshot"] = copy.deepcopy(dict(job_snapshot))
|
|
337
|
+
if result is not None or attempt_no is not None:
|
|
338
|
+
record.update(_execution_envelope(
|
|
339
|
+
result if result is not None else record["result"],
|
|
340
|
+
files if files is not None else record["files"],
|
|
341
|
+
))
|
|
342
|
+
if attempt_no is not None:
|
|
343
|
+
record.update(
|
|
344
|
+
run_id=run_id or "run",
|
|
345
|
+
attempt_no=attempt_no,
|
|
346
|
+
started_at=started_at,
|
|
347
|
+
execution_id=execution_id,
|
|
348
|
+
)
|
|
349
|
+
return record
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def build_manual_success_record(
|
|
353
|
+
job_snapshot: Mapping[str, Any],
|
|
354
|
+
*,
|
|
355
|
+
path_policy: PathPolicy,
|
|
356
|
+
note: str | None = None,
|
|
357
|
+
settled_at: str | None = None,
|
|
358
|
+
) -> dict[str, Any]:
|
|
359
|
+
"""构造人工 ``Successes`` 档案:双写保留型 output、清执行基线、零 Attempt。"""
|
|
360
|
+
annotations: dict[str, Any] = {"completion_source": "manual"}
|
|
361
|
+
if note is not None:
|
|
362
|
+
annotations["note"] = note
|
|
363
|
+
|
|
364
|
+
raw_files = job_snapshot.get("files") or {}
|
|
365
|
+
retained_specs: dict[str, dict[str, Any]] = {}
|
|
366
|
+
retained_paths: dict[str, str] = {}
|
|
367
|
+
if isinstance(raw_files, Mapping):
|
|
368
|
+
for name, raw_spec in raw_files.items():
|
|
369
|
+
if not isinstance(raw_spec, Mapping):
|
|
370
|
+
continue
|
|
371
|
+
if raw_spec.get("role") == "output" and not bool(raw_spec.get("delete_on_success", False)):
|
|
372
|
+
path = raw_spec.get("path")
|
|
373
|
+
if isinstance(path, str) and path:
|
|
374
|
+
# 提前经 lookup 解析规范路径,使块内 tx.after 与退出后落盘快照一致;
|
|
375
|
+
# 非法或不可访问路径回退保留原值,留待提交期 strict 验盘统一汇报。
|
|
376
|
+
try:
|
|
377
|
+
effective_path = path_policy.inspect(path, mode="lookup").canonical_path
|
|
378
|
+
except (ValueError, OSError):
|
|
379
|
+
effective_path = path
|
|
380
|
+
retained_specs[name] = {
|
|
381
|
+
"path": effective_path,
|
|
382
|
+
"role": "output",
|
|
383
|
+
"delete_existing": bool(raw_spec.get("delete_existing", False)),
|
|
384
|
+
"delete_on_failure": bool(raw_spec.get("delete_on_failure", False)),
|
|
385
|
+
"delete_on_success": False,
|
|
386
|
+
}
|
|
387
|
+
retained_paths[name] = effective_path
|
|
388
|
+
|
|
389
|
+
return {
|
|
390
|
+
"job_snapshot": copy.deepcopy(dict(job_snapshot)),
|
|
391
|
+
"files": normalize_outer_files(retained_specs),
|
|
392
|
+
"artifacts": build_artifacts_manifest(retained_paths),
|
|
393
|
+
"inputs": [],
|
|
394
|
+
"result": build_result_record(annotations=annotations),
|
|
395
|
+
"annotations": dict(annotations),
|
|
396
|
+
"settled_at": settled_at if settled_at is not None else utc_now_iso(),
|
|
397
|
+
"run_id": None,
|
|
398
|
+
"attempt_no": None,
|
|
399
|
+
"started_at": None,
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def build_manual_failure_entry(
|
|
404
|
+
job_snapshot: Mapping[str, Any],
|
|
405
|
+
*,
|
|
406
|
+
note: str | None = None,
|
|
407
|
+
settled_at: str | None = None,
|
|
408
|
+
) -> tuple[dict[str, Any], dict[str, Any] | None]:
|
|
409
|
+
"""构造人工 ``Failures`` 档案与 payload 快照(note 仅写入 annotations,根部不重复写)。"""
|
|
410
|
+
annotations: dict[str, Any] = {"completion_source": "manual"}
|
|
411
|
+
if note is not None:
|
|
412
|
+
annotations["note"] = note
|
|
413
|
+
error_message = note if note is not None else "Manual status set to failed"
|
|
414
|
+
fail_meta: dict[str, Any] = {
|
|
415
|
+
"job_snapshot": copy.deepcopy(dict(job_snapshot)),
|
|
416
|
+
"files": copy.deepcopy(dict(job_snapshot.get("files") or {})),
|
|
417
|
+
"result": build_result_record(message=error_message, annotations=annotations),
|
|
418
|
+
"annotations": dict(annotations),
|
|
419
|
+
"error_message": error_message,
|
|
420
|
+
"settled_at": settled_at if settled_at is not None else utc_now_iso(),
|
|
421
|
+
"run_id": None,
|
|
422
|
+
"attempt_no": None,
|
|
423
|
+
"started_at": None,
|
|
424
|
+
}
|
|
425
|
+
raw_payload = job_snapshot.get("payload")
|
|
426
|
+
payload_copy = copy.deepcopy(raw_payload) if isinstance(raw_payload, dict) else None
|
|
427
|
+
return fail_meta, payload_copy
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def extract_archive_result(
|
|
431
|
+
archive_meta: Mapping[str, Any],
|
|
432
|
+
*,
|
|
433
|
+
is_failure: bool,
|
|
434
|
+
) -> dict[str, Any] | None:
|
|
435
|
+
"""从终态档案提取规范 4 键 result(向后兼容旧库根部 error_message / message / note / annotations)。"""
|
|
436
|
+
raw_result = archive_meta.get("result")
|
|
437
|
+
if isinstance(raw_result, Mapping) and raw_result:
|
|
438
|
+
normalized = normalize_result_record(raw_result)
|
|
439
|
+
if (
|
|
440
|
+
normalized is not None
|
|
441
|
+
and is_failure
|
|
442
|
+
and "note" in archive_meta
|
|
443
|
+
and "note" not in normalized["annotations"]
|
|
444
|
+
):
|
|
445
|
+
normalized["annotations"]["note"] = archive_meta["note"]
|
|
446
|
+
return normalized
|
|
447
|
+
raw_ann = archive_meta.get("annotations")
|
|
448
|
+
annotations: dict[str, Any] = dict(raw_ann) if isinstance(raw_ann, Mapping) else {}
|
|
449
|
+
if is_failure and "note" in archive_meta and "note" not in annotations:
|
|
450
|
+
annotations["note"] = archive_meta["note"]
|
|
451
|
+
if is_failure:
|
|
452
|
+
return normalize_result_record({
|
|
453
|
+
"message": archive_meta.get("error_message") or archive_meta.get("message"),
|
|
454
|
+
"exception": archive_meta.get("exception"),
|
|
455
|
+
"metadata": {},
|
|
456
|
+
"annotations": annotations,
|
|
457
|
+
})
|
|
458
|
+
return normalize_result_record({
|
|
459
|
+
"message": None,
|
|
460
|
+
"exception": None,
|
|
461
|
+
"metadata": {},
|
|
462
|
+
"annotations": annotations,
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
__all__ = [
|
|
467
|
+
"EFFECTIVE_JOB_CONFIG_KEYS",
|
|
468
|
+
"build_artifacts_manifest",
|
|
469
|
+
"build_execution_failure_record",
|
|
470
|
+
"build_execution_success_record",
|
|
471
|
+
"build_manual_failure_entry",
|
|
472
|
+
"build_manual_success_record",
|
|
473
|
+
"build_result_record",
|
|
474
|
+
"extract_archive_result",
|
|
475
|
+
"extract_effective_job_config",
|
|
476
|
+
"extract_job_snapshot_from_archive",
|
|
477
|
+
"extract_serializable_payload",
|
|
478
|
+
"fallback_job_snapshot",
|
|
479
|
+
"normalize_job_intake_snapshot",
|
|
480
|
+
"normalize_outer_files",
|
|
481
|
+
"normalize_result_record",
|
|
482
|
+
]
|