braid-coding-agent 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.
- braid_coding_agent/__init__.py +13 -0
- braid_coding_agent/assembly.py +460 -0
- braid_coding_agent/cancellation.py +25 -0
- braid_coding_agent/checkpoint/__init__.py +106 -0
- braid_coding_agent/checkpoint/git.py +305 -0
- braid_coding_agent/checkpoint/settings.py +26 -0
- braid_coding_agent/checkpoint/store.py +1261 -0
- braid_coding_agent/checkpoint/workspace.py +220 -0
- braid_coding_agent/cli.py +2168 -0
- braid_coding_agent/commands/__init__.py +50 -0
- braid_coding_agent/commands/auth.py +161 -0
- braid_coding_agent/commands/config.py +180 -0
- braid_coding_agent/commands/doctor.py +511 -0
- braid_coding_agent/commands/mcp.py +928 -0
- braid_coding_agent/commands/models.py +95 -0
- braid_coding_agent/commands/setup.py +89 -0
- braid_coding_agent/commands/uninstall.py +277 -0
- braid_coding_agent/commands/upgrade.py +381 -0
- braid_coding_agent/compaction/__init__.py +174 -0
- braid_coding_agent/compaction/prepare.py +430 -0
- braid_coding_agent/compaction/summarize.py +542 -0
- braid_coding_agent/compaction/tokens.py +204 -0
- braid_coding_agent/compaction/tool_results.py +196 -0
- braid_coding_agent/compaction/utils.py +181 -0
- braid_coding_agent/config.py +2776 -0
- braid_coding_agent/config_files.py +191 -0
- braid_coding_agent/credentials.py +252 -0
- braid_coding_agent/discovery.py +415 -0
- braid_coding_agent/extensions/__init__.py +77 -0
- braid_coding_agent/extensions/api.py +480 -0
- braid_coding_agent/extensions/runner.py +553 -0
- braid_coding_agent/formatting/__init__.py +62 -0
- braid_coding_agent/formatting/settings.py +87 -0
- braid_coding_agent/formatting/tool.py +179 -0
- braid_coding_agent/lsp/__init__.py +105 -0
- braid_coding_agent/lsp/client.py +546 -0
- braid_coding_agent/lsp/manager.py +187 -0
- braid_coding_agent/lsp/model.py +316 -0
- braid_coding_agent/lsp/protocol.py +105 -0
- braid_coding_agent/lsp/settings.py +120 -0
- braid_coding_agent/lsp/tool.py +429 -0
- braid_coding_agent/mcp/__init__.py +101 -0
- braid_coding_agent/mcp/cache.py +230 -0
- braid_coding_agent/mcp/catalog.py +108 -0
- braid_coding_agent/mcp/config.py +664 -0
- braid_coding_agent/mcp/config_store.py +142 -0
- braid_coding_agent/mcp/inspector.py +85 -0
- braid_coding_agent/mcp/panel.py +254 -0
- braid_coding_agent/mcp/runtime.py +1060 -0
- braid_coding_agent/mcp/tools.py +579 -0
- braid_coding_agent/mcp/trust.py +150 -0
- braid_coding_agent/mcp/tui_panel.py +444 -0
- braid_coding_agent/messages.py +152 -0
- braid_coding_agent/models_view.py +105 -0
- braid_coding_agent/modes/__init__.py +64 -0
- braid_coding_agent/modes/json_mode.py +480 -0
- braid_coding_agent/modes/lifecycle.py +217 -0
- braid_coding_agent/modes/print_mode.py +257 -0
- braid_coding_agent/modes/repl.py +2687 -0
- braid_coding_agent/modes/repl_commands.py +263 -0
- braid_coding_agent/modes/rpc.py +953 -0
- braid_coding_agent/modes/skill_commands.py +88 -0
- braid_coding_agent/modes/tui.py +2226 -0
- braid_coding_agent/modes/tui_control.py +90 -0
- braid_coding_agent/modes/tui_model.py +1043 -0
- braid_coding_agent/modes/tui_render.py +327 -0
- braid_coding_agent/modes/tui_rich.py +751 -0
- braid_coding_agent/modes/tui_style.py +42 -0
- braid_coding_agent/modes/wire.py +81 -0
- braid_coding_agent/netsafe.py +82 -0
- braid_coding_agent/network/__init__.py +20 -0
- braid_coding_agent/network/cache.py +77 -0
- braid_coding_agent/network/fetch.py +311 -0
- braid_coding_agent/network/html_text.py +160 -0
- braid_coding_agent/network/search.py +347 -0
- braid_coding_agent/network/settings.py +103 -0
- braid_coding_agent/network/urls.py +89 -0
- braid_coding_agent/observability/__init__.py +101 -0
- braid_coding_agent/observability/audit.py +124 -0
- braid_coding_agent/observability/collector.py +314 -0
- braid_coding_agent/observability/exporter.py +74 -0
- braid_coding_agent/observability/logging_setup.py +149 -0
- braid_coding_agent/observability/redaction.py +96 -0
- braid_coding_agent/observability/settings.py +29 -0
- braid_coding_agent/observability/spans.py +239 -0
- braid_coding_agent/permissions/__init__.py +185 -0
- braid_coding_agent/permissions/command.py +586 -0
- braid_coding_agent/permissions/engine.py +522 -0
- braid_coding_agent/permissions/evaluate.py +309 -0
- braid_coding_agent/permissions/matching.py +233 -0
- braid_coding_agent/permissions/model.py +278 -0
- braid_coding_agent/permissions/policy.py +99 -0
- braid_coding_agent/permissions/readonly.py +236 -0
- braid_coding_agent/permissions/store.py +379 -0
- braid_coding_agent/project_init.py +313 -0
- braid_coding_agent/prompt/__init__.py +98 -0
- braid_coding_agent/prompt/context_files.py +253 -0
- braid_coding_agent/prompt/project_context.py +172 -0
- braid_coding_agent/prompt/system_prompt.py +378 -0
- braid_coding_agent/questions.py +71 -0
- braid_coding_agent/render/__init__.py +56 -0
- braid_coding_agent/render/batch.py +66 -0
- braid_coding_agent/render/history.py +143 -0
- braid_coding_agent/render/stream.py +823 -0
- braid_coding_agent/render/theme.py +243 -0
- braid_coding_agent/sandbox/__init__.py +89 -0
- braid_coding_agent/sandbox/backend.py +184 -0
- braid_coding_agent/sandbox/bubblewrap.py +96 -0
- braid_coding_agent/sandbox/operations.py +301 -0
- braid_coding_agent/sandbox/policy.py +221 -0
- braid_coding_agent/sandbox/proxy.py +320 -0
- braid_coding_agent/sandbox/seatbelt.py +222 -0
- braid_coding_agent/sdk/__init__.py +31 -0
- braid_coding_agent/sdk/async_client.py +366 -0
- braid_coding_agent/sdk/client.py +386 -0
- braid_coding_agent/sdk/errors.py +52 -0
- braid_coding_agent/sdk/events.py +162 -0
- braid_coding_agent/sdk/types.py +63 -0
- braid_coding_agent/sdk/wire.py +59 -0
- braid_coding_agent/server/__init__.py +54 -0
- braid_coding_agent/server/errors.py +52 -0
- braid_coding_agent/server/http_server.py +419 -0
- braid_coding_agent/server/hub.py +530 -0
- braid_coding_agent/server/openapi.py +157 -0
- braid_coding_agent/server/registry.py +354 -0
- braid_coding_agent/server/router.py +275 -0
- braid_coding_agent/server/run.py +148 -0
- braid_coding_agent/server/schema.py +194 -0
- braid_coding_agent/server/service.py +975 -0
- braid_coding_agent/session/__init__.py +293 -0
- braid_coding_agent/session/agent_session.py +2706 -0
- braid_coding_agent/session/context_breakdown.py +302 -0
- braid_coding_agent/session/discovery.py +212 -0
- braid_coding_agent/session/entries.py +809 -0
- braid_coding_agent/session/evidence.py +295 -0
- braid_coding_agent/session/lock.py +176 -0
- braid_coding_agent/session/manager.py +1185 -0
- braid_coding_agent/session/plan.py +243 -0
- braid_coding_agent/session/plan_mode.py +423 -0
- braid_coding_agent/session/projection.py +262 -0
- braid_coding_agent/session/rewind.py +257 -0
- braid_coding_agent/session/status.py +240 -0
- braid_coding_agent/settings.py +94 -0
- braid_coding_agent/skills/__init__.py +70 -0
- braid_coding_agent/skills/loader.py +681 -0
- braid_coding_agent/startup/__init__.py +76 -0
- braid_coding_agent/startup/cache.py +72 -0
- braid_coding_agent/startup/diagnostics.py +37 -0
- braid_coding_agent/startup/monitor.py +245 -0
- braid_coding_agent/startup/phases.py +79 -0
- braid_coding_agent/startup/project.py +165 -0
- braid_coding_agent/startup/report.py +560 -0
- braid_coding_agent/startup/scheduler.py +347 -0
- braid_coding_agent/statusline.py +163 -0
- braid_coding_agent/subagents/__init__.py +22 -0
- braid_coding_agent/subagents/contract.py +75 -0
- braid_coding_agent/subagents/definitions.py +583 -0
- braid_coding_agent/subagents/registry.py +277 -0
- braid_coding_agent/subagents/runtime.py +610 -0
- braid_coding_agent/subagents/settings.py +156 -0
- braid_coding_agent/subagents/worktree.py +63 -0
- braid_coding_agent/tools/__init__.py +327 -0
- braid_coding_agent/tools/args.py +87 -0
- braid_coding_agent/tools/bash.py +359 -0
- braid_coding_agent/tools/edit.py +298 -0
- braid_coding_agent/tools/edit_diff.py +344 -0
- braid_coding_agent/tools/find.py +158 -0
- braid_coding_agent/tools/grep.py +305 -0
- braid_coding_agent/tools/ignore.py +198 -0
- braid_coding_agent/tools/ls.py +127 -0
- braid_coding_agent/tools/mutation_queue.py +90 -0
- braid_coding_agent/tools/operations.py +274 -0
- braid_coding_agent/tools/output_accumulator.py +282 -0
- braid_coding_agent/tools/output_store.py +244 -0
- braid_coding_agent/tools/path_utils.py +91 -0
- braid_coding_agent/tools/plan.py +141 -0
- braid_coding_agent/tools/prompt_notes.py +52 -0
- braid_coding_agent/tools/question.py +180 -0
- braid_coding_agent/tools/read.py +223 -0
- braid_coding_agent/tools/submit_plan.py +102 -0
- braid_coding_agent/tools/truncate.py +423 -0
- braid_coding_agent/tools/write.py +100 -0
- braid_coding_agent/untrusted.py +50 -0
- braid_coding_agent/verify/__init__.py +78 -0
- braid_coding_agent/verify/runner.py +260 -0
- braid_coding_agent/verify/settings.py +64 -0
- braid_coding_agent/verify/tool.py +143 -0
- braid_coding_agent-0.1.0.dist-info/METADATA +19 -0
- braid_coding_agent-0.1.0.dist-info/RECORD +192 -0
- braid_coding_agent-0.1.0.dist-info/WHEEL +4 -0
- braid_coding_agent-0.1.0.dist-info/entry_points.txt +3 -0
- braid_coding_agent-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"""子代理任务登记表:一次 ``task`` 调用对应一条记录。
|
|
2
|
+
|
|
3
|
+
登记表是运行时状态的唯一事实来源:``/agents``、``/tasks``、RPC 的 ``get_agents``
|
|
4
|
+
以及后台任务收尾都读它。子代理自己的会话文件是另一份事实来源,两者不合并——
|
|
5
|
+
前者回答"这次派生做了什么、跑到哪一步",后者回答"子代理到底看到了什么"。
|
|
6
|
+
|
|
7
|
+
后台任务的 asyncio 句柄必须留在登记表里。``asyncio`` 对任务只持弱引用,
|
|
8
|
+
``create_task`` 之后不保存引用的任务可能在跑完之前被回收。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import asyncio
|
|
14
|
+
import itertools
|
|
15
|
+
from dataclasses import dataclass, field
|
|
16
|
+
|
|
17
|
+
from braid_ai.types import JsonValue
|
|
18
|
+
from braid_coding_agent.subagents.settings import TaskStatus
|
|
19
|
+
|
|
20
|
+
MAX_FILE_PATHS = 20
|
|
21
|
+
DEFAULT_DIFF_LIMIT = 6000
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class SubagentLimitError(RuntimeError):
|
|
25
|
+
"""同时在跑的后台子代理已经到上限。"""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(slots=True)
|
|
29
|
+
class SubagentTask:
|
|
30
|
+
"""一次派生任务的状态与结果。"""
|
|
31
|
+
|
|
32
|
+
id: str
|
|
33
|
+
agent: str
|
|
34
|
+
prompt: str
|
|
35
|
+
background: bool
|
|
36
|
+
depth: int
|
|
37
|
+
cwd: str
|
|
38
|
+
status: TaskStatus = "running"
|
|
39
|
+
session_file: str | None = None
|
|
40
|
+
worktree: str | None = None
|
|
41
|
+
branch: str | None = None
|
|
42
|
+
summary: str = ""
|
|
43
|
+
files: tuple[str, ...] = ()
|
|
44
|
+
diff: str = ""
|
|
45
|
+
usage: dict[str, int] = field(default_factory=dict[str, int])
|
|
46
|
+
#: 子代理自己的美元账单,与父会话的累计费用分开记录。
|
|
47
|
+
cost_usd: float = 0.0
|
|
48
|
+
error: str | None = None
|
|
49
|
+
started_ms: int = 0
|
|
50
|
+
finished_ms: int | None = None
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def duration_ms(self) -> int | None:
|
|
54
|
+
if self.finished_ms is None:
|
|
55
|
+
return None
|
|
56
|
+
return max(0, self.finished_ms - self.started_ms)
|
|
57
|
+
|
|
58
|
+
def wire(self) -> dict[str, JsonValue]:
|
|
59
|
+
wire: dict[str, JsonValue] = {
|
|
60
|
+
"taskId": self.id,
|
|
61
|
+
"agent": self.agent,
|
|
62
|
+
"prompt": self.prompt,
|
|
63
|
+
"background": self.background,
|
|
64
|
+
"depth": self.depth,
|
|
65
|
+
"cwd": self.cwd,
|
|
66
|
+
"status": self.status,
|
|
67
|
+
"summary": self.summary,
|
|
68
|
+
"files": list(self.files[:MAX_FILE_PATHS]),
|
|
69
|
+
"diff": self.diff[:DEFAULT_DIFF_LIMIT],
|
|
70
|
+
"usage": {key: value for key, value in self.usage.items()},
|
|
71
|
+
"costUsd": self.cost_usd,
|
|
72
|
+
"startedMs": self.started_ms,
|
|
73
|
+
"finishedMs": self.finished_ms,
|
|
74
|
+
"durationMs": self.duration_ms,
|
|
75
|
+
}
|
|
76
|
+
for key, value in (
|
|
77
|
+
("sessionFile", self.session_file),
|
|
78
|
+
("worktree", self.worktree),
|
|
79
|
+
("branch", self.branch),
|
|
80
|
+
("error", self.error),
|
|
81
|
+
):
|
|
82
|
+
if value is not None:
|
|
83
|
+
wire[key] = value
|
|
84
|
+
return wire
|
|
85
|
+
|
|
86
|
+
def result_text(self, *, diff_limit: int = DEFAULT_DIFF_LIMIT) -> str:
|
|
87
|
+
"""给模型与终端看的完整结果。失败与运行中也各自有一句话交代。"""
|
|
88
|
+
head = f"子代理任务 {self.id}({self.agent})· 状态 {self.status}"
|
|
89
|
+
if self.status == "running":
|
|
90
|
+
return f"{head}:仍在运行,结果会在任务结束时给出。"
|
|
91
|
+
if self.status == "failed" or self.status == "cancelled":
|
|
92
|
+
return f"{head}:{self.error or '没有更多错误信息'}。父任务可以改用其它工具继续。"
|
|
93
|
+
lines = [head, f"摘要:{self.summary or '(子代理没有给出文本结论)'}"]
|
|
94
|
+
if self.files:
|
|
95
|
+
lines.append("关键文件:" + "、".join(self.files[:MAX_FILE_PATHS]))
|
|
96
|
+
if self.usage:
|
|
97
|
+
lines.append(
|
|
98
|
+
f"子代理用量:输入 {self.usage.get('input', 0)} · "
|
|
99
|
+
f"输出 {self.usage.get('output', 0)} · "
|
|
100
|
+
f"合计 {self.usage.get('totalTokens', 0)} tokens(独立账单)"
|
|
101
|
+
)
|
|
102
|
+
lines.append(f"子代理费用:${self.cost_usd:.4f}(独立账单)")
|
|
103
|
+
if self.session_file:
|
|
104
|
+
lines.append(f"子会话文件:{self.session_file}")
|
|
105
|
+
if self.worktree:
|
|
106
|
+
lines.append(f"工作树:{self.worktree}(分支 {self.branch or '未知'})")
|
|
107
|
+
if self.diff:
|
|
108
|
+
shown = self.diff[:diff_limit]
|
|
109
|
+
marker = "\n…(diff 已截断)" if len(self.diff) > diff_limit else ""
|
|
110
|
+
lines.append(f"工作树 diff:\n{shown}{marker}")
|
|
111
|
+
return "\n".join(lines)
|
|
112
|
+
|
|
113
|
+
def one_line(self) -> str:
|
|
114
|
+
"""状态区的一行。后台任务在跑时也要能看出是谁在跑。"""
|
|
115
|
+
detail = self.error if self.status in ("failed", "cancelled") else self.summary
|
|
116
|
+
tail = detail.replace("\n", " ")[:60] if detail else self.cwd
|
|
117
|
+
return f"{self.id} · {self.agent} · {self.status} · {tail}"
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class SubagentRegistry:
|
|
121
|
+
"""任务记录的容器。不持有会话对象,只持有句柄。
|
|
122
|
+
|
|
123
|
+
``max_concurrent`` 是**总数**上限(同步与后台一起算),``max_background`` 只约束
|
|
124
|
+
后台那一类。两条分开是因为它们防的是不同的事:后台任务要占工作树与分支,同步任务
|
|
125
|
+
不占,但一次并行工具批次里的多个同步调用同样会同时打出多个完整会话。
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
def __init__(self, *, max_background: int, max_concurrent: int | None = None) -> None:
|
|
129
|
+
self._max_background = max_background
|
|
130
|
+
self._max_concurrent = max_concurrent
|
|
131
|
+
self._counter = itertools.count(1)
|
|
132
|
+
self._tasks: dict[str, SubagentTask] = {}
|
|
133
|
+
self._handles: dict[str, asyncio.Task[None]] = {}
|
|
134
|
+
#: 不参与 ``wait_all`` 的句柄。``/background`` 分离出来的任务与父会话没有
|
|
135
|
+
#: 同一个回合的收尾关系,父会话结束时不该替它等;它仍然可以被取消与查询。
|
|
136
|
+
self._detached: set[str] = set()
|
|
137
|
+
|
|
138
|
+
@property
|
|
139
|
+
def max_background(self) -> int:
|
|
140
|
+
return self._max_background
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def max_concurrent(self) -> int | None:
|
|
144
|
+
return self._max_concurrent
|
|
145
|
+
|
|
146
|
+
def create(
|
|
147
|
+
self,
|
|
148
|
+
*,
|
|
149
|
+
agent: str,
|
|
150
|
+
prompt: str,
|
|
151
|
+
background: bool,
|
|
152
|
+
depth: int,
|
|
153
|
+
cwd: str,
|
|
154
|
+
session_file: str | None = None,
|
|
155
|
+
worktree: str | None = None,
|
|
156
|
+
branch: str | None = None,
|
|
157
|
+
started_ms: int = 0,
|
|
158
|
+
task_id: str | None = None,
|
|
159
|
+
) -> SubagentTask:
|
|
160
|
+
if background and len(self.running_background()) >= self._max_background:
|
|
161
|
+
raise SubagentLimitError(
|
|
162
|
+
f"同时在跑的后台子代理已达上限 {self._max_background};"
|
|
163
|
+
"等一个任务结束,或调大 agents.max_background"
|
|
164
|
+
)
|
|
165
|
+
if self._max_concurrent is not None and len(self.running()) >= self._max_concurrent:
|
|
166
|
+
raise SubagentLimitError(
|
|
167
|
+
f"同时在跑的子代理已达上限 {self._max_concurrent}(同步与后台合计);"
|
|
168
|
+
"等一个任务结束,或调大 agents.max_concurrent"
|
|
169
|
+
)
|
|
170
|
+
identifier = task_id or f"task-{next(self._counter)}"
|
|
171
|
+
if task_id is None:
|
|
172
|
+
while identifier in self._tasks:
|
|
173
|
+
identifier = f"task-{next(self._counter)}"
|
|
174
|
+
task = SubagentTask(
|
|
175
|
+
id=identifier,
|
|
176
|
+
agent=agent,
|
|
177
|
+
prompt=prompt,
|
|
178
|
+
background=background,
|
|
179
|
+
depth=depth,
|
|
180
|
+
cwd=cwd,
|
|
181
|
+
session_file=session_file,
|
|
182
|
+
worktree=worktree,
|
|
183
|
+
branch=branch,
|
|
184
|
+
started_ms=started_ms,
|
|
185
|
+
)
|
|
186
|
+
self._tasks[identifier] = task
|
|
187
|
+
return task
|
|
188
|
+
|
|
189
|
+
def get(self, task_id: str) -> SubagentTask | None:
|
|
190
|
+
return self._tasks.get(task_id)
|
|
191
|
+
|
|
192
|
+
def tasks(self) -> tuple[SubagentTask, ...]:
|
|
193
|
+
return tuple(self._tasks.values())
|
|
194
|
+
|
|
195
|
+
def running(self) -> tuple[SubagentTask, ...]:
|
|
196
|
+
return tuple(task for task in self._tasks.values() if task.status == "running")
|
|
197
|
+
|
|
198
|
+
def running_background(self) -> tuple[SubagentTask, ...]:
|
|
199
|
+
return tuple(task for task in self.running() if task.background)
|
|
200
|
+
|
|
201
|
+
def finish(
|
|
202
|
+
self,
|
|
203
|
+
task_id: str,
|
|
204
|
+
*,
|
|
205
|
+
status: TaskStatus,
|
|
206
|
+
summary: str = "",
|
|
207
|
+
files: tuple[str, ...] = (),
|
|
208
|
+
diff: str = "",
|
|
209
|
+
usage: dict[str, int] | None = None,
|
|
210
|
+
cost_usd: float = 0.0,
|
|
211
|
+
error: str | None = None,
|
|
212
|
+
finished_ms: int = 0,
|
|
213
|
+
) -> SubagentTask:
|
|
214
|
+
task = self._tasks[task_id]
|
|
215
|
+
task.status = status
|
|
216
|
+
task.summary = summary
|
|
217
|
+
task.files = tuple(dict.fromkeys(files))[:MAX_FILE_PATHS]
|
|
218
|
+
task.diff = diff
|
|
219
|
+
task.usage = dict(usage or {})
|
|
220
|
+
task.cost_usd = cost_usd
|
|
221
|
+
task.error = error
|
|
222
|
+
task.finished_ms = finished_ms
|
|
223
|
+
return task
|
|
224
|
+
|
|
225
|
+
def attach(self, task_id: str, handle: asyncio.Task[None], *, detached: bool = False) -> None:
|
|
226
|
+
self._handles[task_id] = handle
|
|
227
|
+
if detached:
|
|
228
|
+
self._detached.add(task_id)
|
|
229
|
+
|
|
230
|
+
def detach(self, task_id: str) -> None:
|
|
231
|
+
self._handles.pop(task_id, None)
|
|
232
|
+
self._detached.discard(task_id)
|
|
233
|
+
|
|
234
|
+
def handle(self, task_id: str) -> asyncio.Task[None] | None:
|
|
235
|
+
"""后台任务的 asyncio 句柄。同步任务没有句柄,返回 ``None``。"""
|
|
236
|
+
return self._handles.get(task_id)
|
|
237
|
+
|
|
238
|
+
def clear_finished(self) -> tuple[str, ...]:
|
|
239
|
+
"""删掉已经结束的任务记录,返回被删掉的任务号。运行中的任务不动。"""
|
|
240
|
+
removed = [task_id for task_id, task in self._tasks.items() if task.status != "running"]
|
|
241
|
+
for task_id in removed:
|
|
242
|
+
del self._tasks[task_id]
|
|
243
|
+
self._handles.pop(task_id, None)
|
|
244
|
+
self._detached.discard(task_id)
|
|
245
|
+
return tuple(removed)
|
|
246
|
+
|
|
247
|
+
async def wait_all(self) -> None:
|
|
248
|
+
"""等本次回合派出的后台任务收尾。``/background`` 的分离任务不在其中。"""
|
|
249
|
+
handles = [
|
|
250
|
+
handle for task_id, handle in self._handles.items() if task_id not in self._detached
|
|
251
|
+
]
|
|
252
|
+
if not handles:
|
|
253
|
+
return
|
|
254
|
+
await asyncio.gather(*handles, return_exceptions=True)
|
|
255
|
+
for task_id, handle in list(self._handles.items()):
|
|
256
|
+
if handle.done():
|
|
257
|
+
self.detach(task_id)
|
|
258
|
+
|
|
259
|
+
def cancel_all(self) -> None:
|
|
260
|
+
"""父会话要退出时收掉还在跑的后台任务。"""
|
|
261
|
+
for handle in self._handles.values():
|
|
262
|
+
if not handle.done():
|
|
263
|
+
handle.cancel()
|
|
264
|
+
for task in self.running():
|
|
265
|
+
if task.background:
|
|
266
|
+
task.status = "cancelled"
|
|
267
|
+
task.error = "父会话收尾,后台任务被取消"
|
|
268
|
+
task.finished_ms = task.started_ms
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
__all__ = [
|
|
272
|
+
"DEFAULT_DIFF_LIMIT",
|
|
273
|
+
"MAX_FILE_PATHS",
|
|
274
|
+
"SubagentLimitError",
|
|
275
|
+
"SubagentRegistry",
|
|
276
|
+
"SubagentTask",
|
|
277
|
+
]
|