multicc 0.7.2__tar.gz → 0.7.3__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.
- {multicc-0.7.2 → multicc-0.7.3}/PKG-INFO +2 -2
- {multicc-0.7.2 → multicc-0.7.3}/README.md +49 -1
- {multicc-0.7.2 → multicc-0.7.3}/multicc.egg-info/PKG-INFO +2 -2
- {multicc-0.7.2 → multicc-0.7.3}/multicc.egg-info/SOURCES.txt +24 -0
- {multicc-0.7.2 → multicc-0.7.3}/pyproject.toml +4 -4
- multicc-0.7.3/tasker/artifact_store.py +489 -0
- multicc-0.7.3/tasker/checkpoint.py +301 -0
- multicc-0.7.3/tasker/checkpoint_runtime.py +569 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/codex_app_server_runner.py +107 -11
- {multicc-0.7.2 → multicc-0.7.3}/tasker/config.py +242 -0
- multicc-0.7.3/tasker/domain/__init__.py +0 -0
- multicc-0.7.3/tasker/domain/artifact.py +92 -0
- multicc-0.7.3/tasker/domain/execution.py +158 -0
- multicc-0.7.3/tasker/domain/memory.py +267 -0
- multicc-0.7.3/tasker/domain/state_machine.py +74 -0
- multicc-0.7.3/tasker/domain/tool.py +251 -0
- multicc-0.7.3/tasker/domain/tool_call.py +448 -0
- multicc-0.7.3/tasker/execution_lease.py +365 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/goal_loop.py +187 -9
- {multicc-0.7.2 → multicc-0.7.3}/tasker/graph_executor.py +234 -21
- {multicc-0.7.2 → multicc-0.7.3}/tasker/live.py +30 -4
- multicc-0.7.3/tasker/memory_maintenance.py +322 -0
- multicc-0.7.3/tasker/memory_runtime.py +838 -0
- multicc-0.7.3/tasker/memory_store.py +310 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/models.py +1 -2
- {multicc-0.7.2 → multicc-0.7.3}/tasker/planner.py +31 -1
- multicc-0.7.3/tasker/registered_tools.py +197 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/repl.py +316 -24
- {multicc-0.7.2 → multicc-0.7.3}/tasker/runner_base.py +249 -1
- {multicc-0.7.2 → multicc-0.7.3}/tasker/runner_factory.py +6 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/sdk_runner.py +127 -33
- {multicc-0.7.2 → multicc-0.7.3}/tasker/session.py +11 -0
- multicc-0.7.3/tasker/tool_adapter.py +147 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/tool_catalog.py +11 -1
- multicc-0.7.3/tasker/tool_journal.py +331 -0
- multicc-0.7.3/tasker/tool_policy.py +880 -0
- multicc-0.7.3/tasker/tool_registry.py +1038 -0
- multicc-0.7.3/tasker/tool_runtime.py +976 -0
- multicc-0.7.3/tasker/tool_validation.py +225 -0
- multicc-0.7.3/tests/test_checkpoint.py +66 -0
- multicc-0.7.3/tests/test_checkpoint_runtime.py +317 -0
- multicc-0.7.3/tests/test_memory_config.py +170 -0
- {multicc-0.7.2 → multicc-0.7.3}/LICENSE +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/multicc.egg-info/dependency_links.txt +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/multicc.egg-info/entry_points.txt +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/multicc.egg-info/requires.txt +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/multicc.egg-info/top_level.txt +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/setup.cfg +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/__init__.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/__main__.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/approval_menu.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/approvals.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/attachments.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/composer.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/console.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/formatting.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/llm.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/loop_protocol.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/main.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/markdown_view.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/memory.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/policy_hooks.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/spawn.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/template_compiler.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tasker/workflow.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tests/test_memory.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tests/test_rich_tui.py +0 -0
- {multicc-0.7.2 → multicc-0.7.3}/tests/test_tui.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: multicc
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.3
|
|
4
4
|
License: MIT License
|
|
5
5
|
|
|
6
6
|
Copyright (c) 2026 tasker contributors
|
|
@@ -23,7 +23,7 @@ License: MIT License
|
|
|
23
23
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
24
|
SOFTWARE.
|
|
25
25
|
|
|
26
|
-
Requires-Python: >=3.
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
27
|
License-File: LICENSE
|
|
28
28
|
Requires-Dist: claude-agent-sdk
|
|
29
29
|
Requires-Dist: tp-wy>=0.2.0
|
|
@@ -39,7 +39,7 @@ tasker verify-config # 检查环境
|
|
|
39
39
|
tasker init && $EDITOR config.json
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
Python ≥ 3.
|
|
42
|
+
Python ≥ 3.10。完整打包 / 发布说明见 [RELEASE.md](RELEASE.md)。
|
|
43
43
|
|
|
44
44
|
---
|
|
45
45
|
|
|
@@ -222,6 +222,8 @@ tasker/
|
|
|
222
222
|
|
|
223
223
|
## 执行器行为说明
|
|
224
224
|
|
|
225
|
+
普通任务的 Claude/Codex 工具开始、结果事件会同步记录到会话目录的 `tool_calls/`,再进入展示队列;大结果按 artifacts 配置外置。后端结束却未收到结果的调用标记为 `unknown`,重复开始通知不会覆盖已记录结果。这是后端执行事实记录,不会重新执行原生工具,也不保证外部操作幂等;审批仍走原有回调。没有工具开始通知的结果无法关联,reviewer/evaluator 暂不接入该日志。
|
|
226
|
+
|
|
225
227
|
- **Claude**:由 `claude-agent-sdk` 管理会话、工具调用和权限回调。
|
|
226
228
|
- **Codex**:由 `codex app-server --listen stdio://` 管理 thread/turn、事件流和审批请求。
|
|
227
229
|
- **权限**:两种执行器都通过统一的 `permission_request` / `permission_result` 事件接入审批策略。
|
|
@@ -250,3 +252,49 @@ tasker/
|
|
|
250
252
|
**运行卡住不结束?**
|
|
251
253
|
执行器发完最终结果后,Tasker 根据 `result` / `turn/completed` 和 `completion_idle` 收尾;可 `:done` 手动收尾。
|
|
252
254
|
# taskdispatcher
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
## 拆分模式与耗时测试
|
|
260
|
+
|
|
261
|
+
默认 `dispatch.planning_mode=single`:一次生成任务图,本地校验失败时最多调用模型修正一次。网络请求失败直接沿用原有降级处理。需要三轮拆分、复核、定稿时设为 `react`。模板编排保持原有流程。本地校验检查结构与依赖,不保证需求无遗漏。
|
|
262
|
+
|
|
263
|
+
运行 `python3 scripts/test_planner_timing.py "你的任务" --repeat 3 --output timing.json` 测量当前模式;`--offline` 仅验证计时流程,不代表模型性能。
|
|
264
|
+
|
|
265
|
+
## 注册可调用的本地工具
|
|
266
|
+
|
|
267
|
+
在 `config.json` 的 `tools.entries` 中增加以下配置(未设置 `execution_mode` 的旧条目仍然只提供元数据):
|
|
268
|
+
|
|
269
|
+
```json
|
|
270
|
+
{
|
|
271
|
+
"tools": {
|
|
272
|
+
"entries": [{
|
|
273
|
+
"name": "text_length",
|
|
274
|
+
"description": "统计文本的 Unicode 字符数",
|
|
275
|
+
"execution_mode": "tasker_local",
|
|
276
|
+
"handler": "tasker.registered_tools:text_length",
|
|
277
|
+
"access": "read_only",
|
|
278
|
+
"side_effect": "none",
|
|
279
|
+
"idempotency": "safe",
|
|
280
|
+
"approval": "never",
|
|
281
|
+
"input_schema": {
|
|
282
|
+
"type": "object",
|
|
283
|
+
"properties": {"text": {"type": "string"}},
|
|
284
|
+
"required": ["text"],
|
|
285
|
+
"additionalProperties": false
|
|
286
|
+
}
|
|
287
|
+
}]
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
然后给 worker 一个明确任务,例如“调用 text_length 统计 `你好abc` 的字符数,报告结果”。预期结果为 `{"length": 5}`。
|
|
293
|
+
|
|
294
|
+
`handler` 是已安装或可导入的 Python 模块中的同步函数,签名为 `handler(arguments, invocation)`,返回可序列化结果。模块与函数在初始化时解析,缺失实现直接报错。自定义处理函数运行在 TaskDispatcher 进程中,必须是可信代码;它不继承 Claude/Codex 的子进程沙箱,处理函数自行限制文件路径、网络访问及执行时长。异步函数暂不支持。
|
|
295
|
+
|
|
296
|
+
两种执行器共用 `ToolRuntime` 校验、审批和调用日志:Claude 使用内嵌 MCP 服务(工具名 `mcp__taskdispatcher__text_length`),Codex 使用 App Server 的实验性 `dynamicTools` / `item/tool/call` 协议。Codex 需要支持包含 `type=function` 的动态工具协议;不支持时线程初始化报错,不会悄悄只提供工具描述。权限受工具定义、任务工作区权限及现有配置共同约束;需要审批时使用 `approval.mode`,拒绝或审批超时不会执行处理函数。
|
|
297
|
+
|
|
298
|
+
当前配置支持本地函数工具。远端 MCP 服务器注册、自动连接及任意命令工具不在本次实现范围。后端原生工具仍由后端执行,不能从此入口重复执行。模型是否选择调用工具取决于任务提示;协议测试验证工具发现、调用、错误回传和日志,不依赖在线模型。
|
|
299
|
+
|
|
300
|
+
验证:`python -m pytest tests/tools/test_registered_tools.py -q`。
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: multicc
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.3
|
|
4
4
|
License: MIT License
|
|
5
5
|
|
|
6
6
|
Copyright (c) 2026 tasker contributors
|
|
@@ -23,7 +23,7 @@ License: MIT License
|
|
|
23
23
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
24
|
SOFTWARE.
|
|
25
25
|
|
|
26
|
-
Requires-Python: >=3.
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
27
|
License-File: LICENSE
|
|
28
28
|
Requires-Dist: claude-agent-sdk
|
|
29
29
|
Requires-Dist: tp-wy>=0.2.0
|
|
@@ -11,11 +11,15 @@ tasker/__init__.py
|
|
|
11
11
|
tasker/__main__.py
|
|
12
12
|
tasker/approval_menu.py
|
|
13
13
|
tasker/approvals.py
|
|
14
|
+
tasker/artifact_store.py
|
|
14
15
|
tasker/attachments.py
|
|
16
|
+
tasker/checkpoint.py
|
|
17
|
+
tasker/checkpoint_runtime.py
|
|
15
18
|
tasker/codex_app_server_runner.py
|
|
16
19
|
tasker/composer.py
|
|
17
20
|
tasker/config.py
|
|
18
21
|
tasker/console.py
|
|
22
|
+
tasker/execution_lease.py
|
|
19
23
|
tasker/formatting.py
|
|
20
24
|
tasker/goal_loop.py
|
|
21
25
|
tasker/graph_executor.py
|
|
@@ -25,9 +29,13 @@ tasker/loop_protocol.py
|
|
|
25
29
|
tasker/main.py
|
|
26
30
|
tasker/markdown_view.py
|
|
27
31
|
tasker/memory.py
|
|
32
|
+
tasker/memory_maintenance.py
|
|
33
|
+
tasker/memory_runtime.py
|
|
34
|
+
tasker/memory_store.py
|
|
28
35
|
tasker/models.py
|
|
29
36
|
tasker/planner.py
|
|
30
37
|
tasker/policy_hooks.py
|
|
38
|
+
tasker/registered_tools.py
|
|
31
39
|
tasker/repl.py
|
|
32
40
|
tasker/runner_base.py
|
|
33
41
|
tasker/runner_factory.py
|
|
@@ -35,8 +43,24 @@ tasker/sdk_runner.py
|
|
|
35
43
|
tasker/session.py
|
|
36
44
|
tasker/spawn.py
|
|
37
45
|
tasker/template_compiler.py
|
|
46
|
+
tasker/tool_adapter.py
|
|
38
47
|
tasker/tool_catalog.py
|
|
48
|
+
tasker/tool_journal.py
|
|
49
|
+
tasker/tool_policy.py
|
|
50
|
+
tasker/tool_registry.py
|
|
51
|
+
tasker/tool_runtime.py
|
|
52
|
+
tasker/tool_validation.py
|
|
39
53
|
tasker/workflow.py
|
|
54
|
+
tasker/domain/__init__.py
|
|
55
|
+
tasker/domain/artifact.py
|
|
56
|
+
tasker/domain/execution.py
|
|
57
|
+
tasker/domain/memory.py
|
|
58
|
+
tasker/domain/state_machine.py
|
|
59
|
+
tasker/domain/tool.py
|
|
60
|
+
tasker/domain/tool_call.py
|
|
61
|
+
tests/test_checkpoint.py
|
|
62
|
+
tests/test_checkpoint_runtime.py
|
|
40
63
|
tests/test_memory.py
|
|
64
|
+
tests/test_memory_config.py
|
|
41
65
|
tests/test_rich_tui.py
|
|
42
66
|
tests/test_tui.py
|
|
@@ -4,13 +4,13 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "multicc"
|
|
7
|
-
version = "0.7.
|
|
8
|
-
requires-python = ">=3.
|
|
7
|
+
version = "0.7.3"
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
9
|
license = { file = "LICENSE" }
|
|
10
10
|
dependencies = ["claude-agent-sdk", "tp-wy>=0.2.0", "prompt_toolkit>=3.0.48,<4", "rich>=13.9,<15", "Pillow>=10.4,<13"]
|
|
11
11
|
|
|
12
12
|
[project.scripts]
|
|
13
13
|
tasker = "tasker.main:main"
|
|
14
14
|
|
|
15
|
-
[tool.setuptools]
|
|
16
|
-
|
|
15
|
+
[tool.setuptools.packages.find]
|
|
16
|
+
include = ["tasker*"]
|
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import shutil
|
|
7
|
+
import tempfile
|
|
8
|
+
import threading
|
|
9
|
+
import time
|
|
10
|
+
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
from .domain.artifact import (
|
|
14
|
+
ARTIFACT_VERSION,
|
|
15
|
+
ArtifactKind,
|
|
16
|
+
ArtifactRecord,
|
|
17
|
+
artifact_record_from_dict,
|
|
18
|
+
artifact_record_to_dict,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
from .models import is_valid_id
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ArtifactError(RuntimeError):
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ArtifactCorrupted(
|
|
29
|
+
ArtifactError
|
|
30
|
+
):
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ArtifactStore:
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
*,
|
|
39
|
+
workspace_root: str | Path,
|
|
40
|
+
session_id: str,
|
|
41
|
+
) -> None:
|
|
42
|
+
|
|
43
|
+
if not is_valid_id(session_id):
|
|
44
|
+
raise ValueError(
|
|
45
|
+
f"invalid session_id: "
|
|
46
|
+
f"{session_id!r}"
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
self.workspace_root = Path(
|
|
50
|
+
workspace_root
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
self.session_id = session_id
|
|
54
|
+
|
|
55
|
+
self.root = (
|
|
56
|
+
self.workspace_root
|
|
57
|
+
/ "artifacts"
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
self.root.mkdir(
|
|
61
|
+
parents=True,
|
|
62
|
+
exist_ok=True,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
self._lock = threading.RLock()
|
|
66
|
+
|
|
67
|
+
def _artifact_dir(
|
|
68
|
+
self,
|
|
69
|
+
task_id: str,
|
|
70
|
+
artifact_id: str,
|
|
71
|
+
) -> Path:
|
|
72
|
+
|
|
73
|
+
if not is_valid_id(task_id):
|
|
74
|
+
raise ValueError(
|
|
75
|
+
f"invalid task_id: "
|
|
76
|
+
f"{task_id!r}"
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
if not is_valid_id(artifact_id):
|
|
80
|
+
raise ValueError(
|
|
81
|
+
f"invalid artifact_id: "
|
|
82
|
+
f"{artifact_id!r}"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
self.root
|
|
87
|
+
/ task_id
|
|
88
|
+
/ artifact_id
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
@staticmethod
|
|
92
|
+
def _atomic_write(
|
|
93
|
+
path: Path,
|
|
94
|
+
payload: bytes,
|
|
95
|
+
) -> None:
|
|
96
|
+
|
|
97
|
+
path.parent.mkdir(
|
|
98
|
+
parents=True,
|
|
99
|
+
exist_ok=True,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
temp_path: Path | None = None
|
|
103
|
+
|
|
104
|
+
try:
|
|
105
|
+
with tempfile.NamedTemporaryFile(
|
|
106
|
+
mode="wb",
|
|
107
|
+
dir=path.parent,
|
|
108
|
+
prefix=".artifact-",
|
|
109
|
+
suffix=".tmp",
|
|
110
|
+
delete=False,
|
|
111
|
+
) as file:
|
|
112
|
+
|
|
113
|
+
temp_path = Path(
|
|
114
|
+
file.name
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
file.write(payload)
|
|
118
|
+
|
|
119
|
+
file.flush()
|
|
120
|
+
|
|
121
|
+
os.fsync(
|
|
122
|
+
file.fileno()
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
os.replace(
|
|
126
|
+
temp_path,
|
|
127
|
+
path,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
finally:
|
|
131
|
+
if (
|
|
132
|
+
temp_path is not None
|
|
133
|
+
and temp_path.exists()
|
|
134
|
+
):
|
|
135
|
+
temp_path.unlink(
|
|
136
|
+
missing_ok=True
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
def _artifact_id(
|
|
140
|
+
self,
|
|
141
|
+
*,
|
|
142
|
+
task_id: str,
|
|
143
|
+
kind: ArtifactKind,
|
|
144
|
+
name: str,
|
|
145
|
+
sha256: str,
|
|
146
|
+
) -> str:
|
|
147
|
+
|
|
148
|
+
raw = (
|
|
149
|
+
f"{self.session_id}|"
|
|
150
|
+
f"{task_id}|"
|
|
151
|
+
f"{kind.value}|"
|
|
152
|
+
f"{name}|"
|
|
153
|
+
f"{sha256}"
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
digest = hashlib.sha256(
|
|
157
|
+
raw.encode("utf-8")
|
|
158
|
+
).hexdigest()[:24]
|
|
159
|
+
|
|
160
|
+
return f"a-{digest}"
|
|
161
|
+
|
|
162
|
+
def save_text(
|
|
163
|
+
self,
|
|
164
|
+
*,
|
|
165
|
+
task_id: str,
|
|
166
|
+
name: str,
|
|
167
|
+
content: str,
|
|
168
|
+
kind: ArtifactKind = (
|
|
169
|
+
ArtifactKind.TASK_OUTPUT
|
|
170
|
+
),
|
|
171
|
+
media_type: str = (
|
|
172
|
+
"text/plain; charset=utf-8"
|
|
173
|
+
),
|
|
174
|
+
) -> ArtifactRecord:
|
|
175
|
+
|
|
176
|
+
payload = content.encode(
|
|
177
|
+
"utf-8"
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
sha256 = hashlib.sha256(
|
|
181
|
+
payload
|
|
182
|
+
).hexdigest()
|
|
183
|
+
|
|
184
|
+
artifact_id = (
|
|
185
|
+
self._artifact_id(
|
|
186
|
+
task_id=task_id,
|
|
187
|
+
kind=kind,
|
|
188
|
+
name=name,
|
|
189
|
+
sha256=sha256,
|
|
190
|
+
)
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
directory = (
|
|
194
|
+
self._artifact_dir(
|
|
195
|
+
task_id,
|
|
196
|
+
artifact_id,
|
|
197
|
+
)
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
payload_path = (
|
|
201
|
+
directory
|
|
202
|
+
/ "payload.txt"
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
metadata_path = (
|
|
206
|
+
directory
|
|
207
|
+
/ "metadata.json"
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
relative_path = (
|
|
211
|
+
Path("artifacts")
|
|
212
|
+
/ task_id
|
|
213
|
+
/ artifact_id
|
|
214
|
+
/ "payload.txt"
|
|
215
|
+
).as_posix()
|
|
216
|
+
|
|
217
|
+
record = ArtifactRecord(
|
|
218
|
+
version=ARTIFACT_VERSION,
|
|
219
|
+
|
|
220
|
+
id=artifact_id,
|
|
221
|
+
|
|
222
|
+
session_id=self.session_id,
|
|
223
|
+
source_task_id=task_id,
|
|
224
|
+
|
|
225
|
+
kind=kind,
|
|
226
|
+
|
|
227
|
+
name=name,
|
|
228
|
+
media_type=media_type,
|
|
229
|
+
|
|
230
|
+
relative_path=(
|
|
231
|
+
relative_path
|
|
232
|
+
),
|
|
233
|
+
|
|
234
|
+
size_bytes=len(payload),
|
|
235
|
+
sha256=sha256,
|
|
236
|
+
|
|
237
|
+
created_at=time.time(),
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
with self._lock:
|
|
241
|
+
|
|
242
|
+
if metadata_path.exists():
|
|
243
|
+
|
|
244
|
+
existing = self.load(
|
|
245
|
+
task_id=task_id,
|
|
246
|
+
artifact_id=artifact_id,
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
if existing is None:
|
|
250
|
+
raise ArtifactCorrupted(
|
|
251
|
+
"artifact disappeared"
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
return existing
|
|
255
|
+
|
|
256
|
+
# payload first
|
|
257
|
+
self._atomic_write(
|
|
258
|
+
payload_path,
|
|
259
|
+
payload,
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
# metadata acts as commit marker
|
|
263
|
+
metadata_payload = json.dumps(
|
|
264
|
+
artifact_record_to_dict(
|
|
265
|
+
record
|
|
266
|
+
),
|
|
267
|
+
ensure_ascii=False,
|
|
268
|
+
indent=2,
|
|
269
|
+
sort_keys=True,
|
|
270
|
+
).encode("utf-8")
|
|
271
|
+
|
|
272
|
+
self._atomic_write(
|
|
273
|
+
metadata_path,
|
|
274
|
+
metadata_payload,
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
return record
|
|
278
|
+
|
|
279
|
+
def load(
|
|
280
|
+
self,
|
|
281
|
+
*,
|
|
282
|
+
task_id: str,
|
|
283
|
+
artifact_id: str,
|
|
284
|
+
) -> ArtifactRecord | None:
|
|
285
|
+
|
|
286
|
+
directory = self._artifact_dir(
|
|
287
|
+
task_id,
|
|
288
|
+
artifact_id,
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
metadata_path = (
|
|
292
|
+
directory
|
|
293
|
+
/ "metadata.json"
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
with self._lock:
|
|
297
|
+
|
|
298
|
+
if not metadata_path.exists():
|
|
299
|
+
return None
|
|
300
|
+
|
|
301
|
+
try:
|
|
302
|
+
data = json.loads(
|
|
303
|
+
metadata_path.read_text(
|
|
304
|
+
encoding="utf-8"
|
|
305
|
+
)
|
|
306
|
+
)
|
|
307
|
+
|
|
308
|
+
except (
|
|
309
|
+
OSError,
|
|
310
|
+
json.JSONDecodeError,
|
|
311
|
+
) as exc:
|
|
312
|
+
|
|
313
|
+
raise ArtifactCorrupted(
|
|
314
|
+
"invalid artifact metadata"
|
|
315
|
+
) from exc
|
|
316
|
+
|
|
317
|
+
record = (
|
|
318
|
+
artifact_record_from_dict(
|
|
319
|
+
data
|
|
320
|
+
)
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
if (
|
|
324
|
+
record.session_id
|
|
325
|
+
!= self.session_id
|
|
326
|
+
):
|
|
327
|
+
raise ArtifactCorrupted(
|
|
328
|
+
"session mismatch"
|
|
329
|
+
)
|
|
330
|
+
|
|
331
|
+
if (
|
|
332
|
+
record.source_task_id
|
|
333
|
+
!= task_id
|
|
334
|
+
):
|
|
335
|
+
raise ArtifactCorrupted(
|
|
336
|
+
"task mismatch"
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
if record.id != artifact_id:
|
|
340
|
+
raise ArtifactCorrupted(
|
|
341
|
+
"artifact id mismatch"
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
return record
|
|
345
|
+
|
|
346
|
+
def read_text(
|
|
347
|
+
self,
|
|
348
|
+
record: ArtifactRecord,
|
|
349
|
+
*,
|
|
350
|
+
max_chars: int | None = None,
|
|
351
|
+
) -> str:
|
|
352
|
+
|
|
353
|
+
directory = self._artifact_dir(
|
|
354
|
+
record.source_task_id,
|
|
355
|
+
record.id,
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
payload_path = (
|
|
359
|
+
directory
|
|
360
|
+
/ "payload.txt"
|
|
361
|
+
)
|
|
362
|
+
|
|
363
|
+
try:
|
|
364
|
+
payload = (
|
|
365
|
+
payload_path.read_bytes()
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
except OSError as exc:
|
|
369
|
+
raise ArtifactCorrupted(
|
|
370
|
+
"payload missing"
|
|
371
|
+
) from exc
|
|
372
|
+
|
|
373
|
+
actual_sha = hashlib.sha256(
|
|
374
|
+
payload
|
|
375
|
+
).hexdigest()
|
|
376
|
+
|
|
377
|
+
if actual_sha != record.sha256:
|
|
378
|
+
raise ArtifactCorrupted(
|
|
379
|
+
"checksum mismatch"
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
if (
|
|
383
|
+
len(payload)
|
|
384
|
+
!= record.size_bytes
|
|
385
|
+
):
|
|
386
|
+
raise ArtifactCorrupted(
|
|
387
|
+
"size mismatch"
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
content = payload.decode(
|
|
391
|
+
"utf-8"
|
|
392
|
+
)
|
|
393
|
+
|
|
394
|
+
if (
|
|
395
|
+
max_chars is not None
|
|
396
|
+
and len(content) > max_chars
|
|
397
|
+
):
|
|
398
|
+
return (
|
|
399
|
+
content[:max_chars]
|
|
400
|
+
+ "\n...[Artifact 截断]..."
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
return content
|
|
404
|
+
|
|
405
|
+
def list_for_task(
|
|
406
|
+
self,
|
|
407
|
+
task_id: str,
|
|
408
|
+
) -> list[ArtifactRecord]:
|
|
409
|
+
|
|
410
|
+
task_dir = (
|
|
411
|
+
self.root
|
|
412
|
+
/ task_id
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
if not task_dir.exists():
|
|
416
|
+
return []
|
|
417
|
+
|
|
418
|
+
records: list[
|
|
419
|
+
ArtifactRecord
|
|
420
|
+
] = []
|
|
421
|
+
|
|
422
|
+
for directory in (
|
|
423
|
+
task_dir.iterdir()
|
|
424
|
+
):
|
|
425
|
+
|
|
426
|
+
if not directory.is_dir():
|
|
427
|
+
continue
|
|
428
|
+
|
|
429
|
+
record = self.load(
|
|
430
|
+
task_id=task_id,
|
|
431
|
+
artifact_id=(
|
|
432
|
+
directory.name
|
|
433
|
+
),
|
|
434
|
+
)
|
|
435
|
+
|
|
436
|
+
if record is not None:
|
|
437
|
+
records.append(record)
|
|
438
|
+
|
|
439
|
+
records.sort(
|
|
440
|
+
key=lambda item:
|
|
441
|
+
item.created_at
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
return records
|
|
445
|
+
|
|
446
|
+
def list_all(
|
|
447
|
+
self,
|
|
448
|
+
) -> list[ArtifactRecord]:
|
|
449
|
+
|
|
450
|
+
if not self.root.exists():
|
|
451
|
+
return []
|
|
452
|
+
|
|
453
|
+
records: list[
|
|
454
|
+
ArtifactRecord
|
|
455
|
+
] = []
|
|
456
|
+
|
|
457
|
+
for task_dir in self.root.iterdir():
|
|
458
|
+
|
|
459
|
+
if not task_dir.is_dir():
|
|
460
|
+
continue
|
|
461
|
+
|
|
462
|
+
records.extend(
|
|
463
|
+
self.list_for_task(
|
|
464
|
+
task_dir.name
|
|
465
|
+
)
|
|
466
|
+
)
|
|
467
|
+
|
|
468
|
+
return records
|
|
469
|
+
|
|
470
|
+
def delete(
|
|
471
|
+
self,
|
|
472
|
+
record: ArtifactRecord,
|
|
473
|
+
) -> bool:
|
|
474
|
+
|
|
475
|
+
directory = self._artifact_dir(
|
|
476
|
+
record.source_task_id,
|
|
477
|
+
record.id,
|
|
478
|
+
)
|
|
479
|
+
|
|
480
|
+
with self._lock:
|
|
481
|
+
|
|
482
|
+
if not directory.exists():
|
|
483
|
+
return False
|
|
484
|
+
|
|
485
|
+
shutil.rmtree(
|
|
486
|
+
directory
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
return True
|