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,560 @@
|
|
|
1
|
+
"""``/monitor`` 的文本报告:检查点、派生指标、后台任务与初始化策略表。
|
|
2
|
+
|
|
3
|
+
报告按终端列宽排版。所有行先按显示宽度裁剪或折行,再交给界面输出;TUI 走
|
|
4
|
+
保留换行的纯文本通道,行式 REPL 直接逐行写出。中文、emoji 与全角字符都按
|
|
5
|
+
终端列数计算,不能用字符串长度代替。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections.abc import Mapping
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from typing import Final, Literal
|
|
13
|
+
|
|
14
|
+
from wcwidth import wcwidth
|
|
15
|
+
|
|
16
|
+
from braid_coding_agent.render.stream import display_width
|
|
17
|
+
from braid_coding_agent.startup.monitor import Checkpoint, StartupMonitor, StartupSnapshot
|
|
18
|
+
from braid_coding_agent.startup.phases import INIT_SPECS
|
|
19
|
+
from braid_coding_agent.startup.scheduler import BackgroundScheduler, ScheduledTask
|
|
20
|
+
|
|
21
|
+
__all__ = ["format_monitor_report", "format_tasks"]
|
|
22
|
+
|
|
23
|
+
_MIN_WIDTH: Final[int] = 16
|
|
24
|
+
_NO_LINE_START: Final[frozenset[str]] = frozenset(",。、;:!?)】》」』…,.;:!?)]}")
|
|
25
|
+
|
|
26
|
+
_STATE_LABELS = {
|
|
27
|
+
"pending": "排队",
|
|
28
|
+
"running": "运行",
|
|
29
|
+
"succeeded": "完成",
|
|
30
|
+
"failed": "失败",
|
|
31
|
+
"cancelled": "取消",
|
|
32
|
+
"timed_out": "超时",
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_POLICY_LABELS = {
|
|
36
|
+
"required": "必需",
|
|
37
|
+
"eager": "预热",
|
|
38
|
+
"lazy": "按需",
|
|
39
|
+
"manual": "手动",
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(slots=True, frozen=True)
|
|
44
|
+
class _MetricSpec:
|
|
45
|
+
key: str
|
|
46
|
+
label: str
|
|
47
|
+
kind: Literal["ms", "mb", "percent", "rate", "count"]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
_METRIC_SPECS: Final[tuple[_MetricSpec, ...]] = (
|
|
51
|
+
_MetricSpec("ttfp_ms", "TTFP 首次可交互", "ms"),
|
|
52
|
+
_MetricSpec("ttfu_ms", "TTFU 首次处理任务", "ms"),
|
|
53
|
+
_MetricSpec("ttfm_ms", "TTFM 完整功能就绪", "ms"),
|
|
54
|
+
_MetricSpec("import_ms", "模块导入", "ms"),
|
|
55
|
+
_MetricSpec("config_load_ms", "配置读取", "ms"),
|
|
56
|
+
_MetricSpec("mcp_startup_ms", "最慢 MCP 服务器", "ms"),
|
|
57
|
+
_MetricSpec("mcp_cache_hit_rate", "MCP 目录缓存命中", "rate"),
|
|
58
|
+
_MetricSpec("skill_cache_hit_rate", "技能元信息缓存命中", "rate"),
|
|
59
|
+
_MetricSpec("cache_hit_rate", "总体缓存命中", "rate"),
|
|
60
|
+
_MetricSpec("first_render_memory_mb", "首次渲染内存", "mb"),
|
|
61
|
+
_MetricSpec("background_cpu_ms", "后台 CPU 时间", "ms"),
|
|
62
|
+
_MetricSpec("background_cpu_percent", "后台 CPU 占用", "percent"),
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
_METRIC_BY_KEY: Final[dict[str, _MetricSpec]] = {spec.key: spec for spec in _METRIC_SPECS}
|
|
66
|
+
|
|
67
|
+
_COUNTER_LABELS = {
|
|
68
|
+
"tasks_submitted": "后台任务提交",
|
|
69
|
+
"tasks_succeeded": "后台任务成功",
|
|
70
|
+
"tasks_failed": "后台任务失败",
|
|
71
|
+
"tasks_timed_out": "后台任务超时",
|
|
72
|
+
"tasks_cancelled": "后台任务取消",
|
|
73
|
+
"task_cpu_over_budget": "超出 CPU 预算的任务",
|
|
74
|
+
"mcp_servers_total": "MCP 服务器总数",
|
|
75
|
+
"mcp_servers_cached": "MCP 服务器缓存命中",
|
|
76
|
+
"skill_cache_hit": "技能缓存命中",
|
|
77
|
+
"skill_cache_miss": "技能缓存未命中",
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def format_tasks(
|
|
82
|
+
tasks: tuple[ScheduledTask, ...],
|
|
83
|
+
*,
|
|
84
|
+
width: int = 80,
|
|
85
|
+
metrics: Mapping[str, float] | None = None,
|
|
86
|
+
) -> str:
|
|
87
|
+
"""后台任务表。没有任务时说明当前进程没有提交后台初始化。
|
|
88
|
+
|
|
89
|
+
:param width: 目标终端列数,所有输出行不会超过这个宽度。
|
|
90
|
+
:param metrics: 可选的启动指标;``task.<名称>.duration_ms`` 与
|
|
91
|
+
``task.<名称>.cpu_ms`` 会合并进对应任务行。
|
|
92
|
+
"""
|
|
93
|
+
return "\n".join(_format_tasks(tasks, _normalize_width(width), _task_metrics(metrics)))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def format_monitor_report(
|
|
97
|
+
monitor: StartupMonitor | None,
|
|
98
|
+
scheduler: BackgroundScheduler | None,
|
|
99
|
+
args: str = "",
|
|
100
|
+
*,
|
|
101
|
+
width: int = 80,
|
|
102
|
+
) -> str:
|
|
103
|
+
"""按子命令返回报告。
|
|
104
|
+
|
|
105
|
+
:param args: 空字符串输出全部;``tasks`` 只看后台任务;``specs`` 看初始化策略表。
|
|
106
|
+
:param width: 目标终端列数,TUI 传当前窗口宽度,行式 REPL 传渲染器宽度。
|
|
107
|
+
"""
|
|
108
|
+
columns = _normalize_width(width)
|
|
109
|
+
verb = args.strip().lower()
|
|
110
|
+
if verb == "specs":
|
|
111
|
+
return "\n".join(_format_specs(columns))
|
|
112
|
+
|
|
113
|
+
tasks = scheduler.snapshot() if scheduler is not None else ()
|
|
114
|
+
metrics: Mapping[str, float] = monitor.snapshot().metrics if monitor is not None else {}
|
|
115
|
+
if verb == "tasks":
|
|
116
|
+
return "\n".join(_format_tasks(tasks, columns, _task_metrics(metrics)))
|
|
117
|
+
|
|
118
|
+
sections: list[list[str]] = []
|
|
119
|
+
if monitor is None:
|
|
120
|
+
sections.append(["启动监控", " 启动监控未启用"])
|
|
121
|
+
task_metrics: dict[str, dict[str, float]] = {}
|
|
122
|
+
else:
|
|
123
|
+
snapshot = monitor.snapshot()
|
|
124
|
+
sections.append(_format_header(snapshot, columns))
|
|
125
|
+
sections.append(_format_checkpoints(snapshot.checkpoints, columns))
|
|
126
|
+
known, task_metrics, unknown = _split_metrics(snapshot.metrics)
|
|
127
|
+
sections.append(_format_metrics(known, unknown, columns))
|
|
128
|
+
sections.append(_format_counters(snapshot.counters, columns))
|
|
129
|
+
|
|
130
|
+
sections.append(_format_tasks(tasks, columns, task_metrics))
|
|
131
|
+
if verb not in ("", "all"):
|
|
132
|
+
notice = f"未知子命令 {verb!r}。可用 /monitor、/monitor tasks、/monitor specs。"
|
|
133
|
+
sections.append(_wrap_lines(notice, columns))
|
|
134
|
+
return "\n\n".join("\n".join(section) for section in sections)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _normalize_width(width: int) -> int:
|
|
138
|
+
return max(_MIN_WIDTH, width)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _clip(text: str, width: int) -> str:
|
|
142
|
+
"""按显示宽度截断,超出部分用省略号表示。"""
|
|
143
|
+
if width <= 0:
|
|
144
|
+
return ""
|
|
145
|
+
if display_width(text) <= width:
|
|
146
|
+
return text
|
|
147
|
+
if width == 1:
|
|
148
|
+
return "…"
|
|
149
|
+
kept: list[str] = []
|
|
150
|
+
used = 0
|
|
151
|
+
for char in text:
|
|
152
|
+
char_width = max(wcwidth(char), 1)
|
|
153
|
+
if used + char_width > width - 1:
|
|
154
|
+
break
|
|
155
|
+
kept.append(char)
|
|
156
|
+
used += char_width
|
|
157
|
+
return "".join(kept) + "…"
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _pad(text: str, width: int) -> str:
|
|
161
|
+
shown = _clip(text, width)
|
|
162
|
+
return shown + " " * max(0, width - display_width(shown))
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _right(text: str, width: int) -> str:
|
|
166
|
+
shown = _clip(text, width)
|
|
167
|
+
return " " * max(0, width - display_width(shown)) + shown
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _aligned_rows(
|
|
171
|
+
rows: list[tuple[str, str]],
|
|
172
|
+
width: int,
|
|
173
|
+
*,
|
|
174
|
+
indent: int = 2,
|
|
175
|
+
) -> list[str]:
|
|
176
|
+
"""把若干“名称 / 数值”行排成紧凑的两列。
|
|
177
|
+
|
|
178
|
+
数值列按最长数值靠右排列,名称列只占用实际需要的宽度;终端再宽,信息也不会
|
|
179
|
+
被拉到两端。
|
|
180
|
+
"""
|
|
181
|
+
if not rows:
|
|
182
|
+
return []
|
|
183
|
+
available = max(1, width - indent)
|
|
184
|
+
value_width = max(display_width(value) for _, value in rows)
|
|
185
|
+
label_width = min(
|
|
186
|
+
max(display_width(label) for label, _ in rows),
|
|
187
|
+
max(1, available - value_width - 2),
|
|
188
|
+
)
|
|
189
|
+
return [
|
|
190
|
+
" " * indent + _pad(label, label_width) + " " + _right(value, value_width)
|
|
191
|
+
for label, value in rows
|
|
192
|
+
]
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _append_wrapped(lines: list[str], text: str, width: int, indent: int) -> None:
|
|
196
|
+
for line in _wrap_display(text, max(1, width - indent)):
|
|
197
|
+
lines.append(" " * indent + line)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _wrap_display(text: str, width: int) -> list[str]:
|
|
201
|
+
"""按显示宽度硬折行,保留原有空格。
|
|
202
|
+
|
|
203
|
+
报告里的说明经常是“3 层配置:……”这类中文与数字混排文本,按空格断词会把
|
|
204
|
+
单个数字单独留在一行。这里逐字符累计列宽,视觉上更稳定。
|
|
205
|
+
"""
|
|
206
|
+
lines: list[str] = []
|
|
207
|
+
for raw in text.split("\n"):
|
|
208
|
+
current: list[str] = []
|
|
209
|
+
used = 0
|
|
210
|
+
for char in raw:
|
|
211
|
+
char_width = max(wcwidth(char), 1)
|
|
212
|
+
if current and used + char_width > width:
|
|
213
|
+
if char in _NO_LINE_START and len(current) > 1:
|
|
214
|
+
tail = current.pop()
|
|
215
|
+
used -= max(wcwidth(tail), 1)
|
|
216
|
+
lines.append("".join(current).rstrip())
|
|
217
|
+
current = [tail]
|
|
218
|
+
used = max(wcwidth(tail), 1)
|
|
219
|
+
else:
|
|
220
|
+
lines.append("".join(current).rstrip())
|
|
221
|
+
current = []
|
|
222
|
+
used = 0
|
|
223
|
+
if current and used + char_width > width:
|
|
224
|
+
lines.append("".join(current).rstrip())
|
|
225
|
+
current = []
|
|
226
|
+
used = 0
|
|
227
|
+
current.append(char)
|
|
228
|
+
used += char_width
|
|
229
|
+
lines.append("".join(current).rstrip())
|
|
230
|
+
return lines
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _wrap_lines(text: str, width: int) -> list[str]:
|
|
234
|
+
lines: list[str] = []
|
|
235
|
+
_append_wrapped(lines, text, width, 0)
|
|
236
|
+
return lines
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _format_ms(value: float) -> str:
|
|
240
|
+
return f"{value:,.1f} ms"
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def _format_header(snapshot: StartupSnapshot, width: int) -> list[str]:
|
|
244
|
+
rows = [("阶段", snapshot.phase)]
|
|
245
|
+
elapsed = max((item.elapsed_ms for item in snapshot.checkpoints), default=None)
|
|
246
|
+
if elapsed is not None:
|
|
247
|
+
rows.append(("已用", _format_ms(elapsed)))
|
|
248
|
+
return ["启动监控", *_aligned_rows(rows, width)]
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def _format_checkpoints(checkpoints: tuple[Checkpoint, ...], width: int) -> list[str]:
|
|
252
|
+
lines = ["检查点"]
|
|
253
|
+
if not checkpoints:
|
|
254
|
+
lines.append(" 没有检查点")
|
|
255
|
+
return lines
|
|
256
|
+
|
|
257
|
+
times = [_format_ms(item.elapsed_ms) for item in checkpoints]
|
|
258
|
+
time_width = max(display_width("耗时"), *(display_width(item) for item in times))
|
|
259
|
+
name_width = min(
|
|
260
|
+
max(display_width(item.name) for item in checkpoints),
|
|
261
|
+
max(1, width - 2 - time_width - 3),
|
|
262
|
+
)
|
|
263
|
+
lines.append(" " + _pad("名称", name_width) + " " + _right("耗时", time_width))
|
|
264
|
+
for item, duration in zip(checkpoints, times, strict=True):
|
|
265
|
+
row = " " + _pad(item.name, name_width) + " " + _right(duration, time_width)
|
|
266
|
+
if not item.detail:
|
|
267
|
+
lines.append(row)
|
|
268
|
+
continue
|
|
269
|
+
detail_width = width - display_width(row) - 2
|
|
270
|
+
if detail_width > 0 and display_width(item.detail) <= detail_width:
|
|
271
|
+
lines.append(row + " " + item.detail)
|
|
272
|
+
continue
|
|
273
|
+
lines.append(row)
|
|
274
|
+
_append_wrapped(lines, item.detail, width, 4)
|
|
275
|
+
return lines
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _format_metric_value(spec: _MetricSpec, value: float) -> str:
|
|
279
|
+
match spec.kind:
|
|
280
|
+
case "ms":
|
|
281
|
+
return _format_ms(value)
|
|
282
|
+
case "mb":
|
|
283
|
+
return f"{value:,.1f} MB"
|
|
284
|
+
case "percent":
|
|
285
|
+
return f"{value:.2f}%"
|
|
286
|
+
case "rate":
|
|
287
|
+
return f"{value * 100.0:.1f}%"
|
|
288
|
+
case "count":
|
|
289
|
+
return f"{int(value):,}"
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _split_metrics(
|
|
293
|
+
metrics: Mapping[str, float],
|
|
294
|
+
) -> tuple[
|
|
295
|
+
list[tuple[_MetricSpec, float]],
|
|
296
|
+
dict[str, dict[str, float]],
|
|
297
|
+
list[tuple[str, float]],
|
|
298
|
+
]:
|
|
299
|
+
known: list[tuple[_MetricSpec, float]] = []
|
|
300
|
+
unknown: list[tuple[str, float]] = []
|
|
301
|
+
task_values: dict[str, dict[str, float]] = {}
|
|
302
|
+
for key, value in metrics.items():
|
|
303
|
+
spec = _METRIC_BY_KEY.get(key)
|
|
304
|
+
if spec is not None:
|
|
305
|
+
known.append((spec, value))
|
|
306
|
+
continue
|
|
307
|
+
task = _parse_task_metric(key)
|
|
308
|
+
if task is not None:
|
|
309
|
+
name, field = task
|
|
310
|
+
task_values.setdefault(name, {})[field] = value
|
|
311
|
+
continue
|
|
312
|
+
unknown.append((key, value))
|
|
313
|
+
|
|
314
|
+
order = {spec.key: index for index, spec in enumerate(_METRIC_SPECS)}
|
|
315
|
+
known.sort(key=lambda item: order[item[0].key])
|
|
316
|
+
unknown.sort(key=lambda item: item[0])
|
|
317
|
+
return known, task_values, unknown
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def _parse_task_metric(key: str) -> tuple[str, str] | None:
|
|
321
|
+
if not key.startswith("task."):
|
|
322
|
+
return None
|
|
323
|
+
name, _, field = key[5:].rpartition(".")
|
|
324
|
+
if not name or field not in ("duration_ms", "cpu_ms"):
|
|
325
|
+
return None
|
|
326
|
+
return name, field
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _task_metrics(metrics: Mapping[str, float] | None) -> dict[str, dict[str, float]]:
|
|
330
|
+
if metrics is None:
|
|
331
|
+
return {}
|
|
332
|
+
_, task_values, _ = _split_metrics(metrics)
|
|
333
|
+
return task_values
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _format_metrics(
|
|
337
|
+
known: list[tuple[_MetricSpec, float]],
|
|
338
|
+
unknown: list[tuple[str, float]],
|
|
339
|
+
width: int,
|
|
340
|
+
) -> list[str]:
|
|
341
|
+
lines = ["性能指标"]
|
|
342
|
+
if not known and not unknown:
|
|
343
|
+
lines.append(" 没有指标")
|
|
344
|
+
return lines
|
|
345
|
+
rows = [(spec.label, _format_metric_value(spec, value)) for spec, value in known]
|
|
346
|
+
rows.extend((key, f"{value:,.2f}") for key, value in unknown)
|
|
347
|
+
lines.extend(_aligned_rows(rows, width))
|
|
348
|
+
return lines
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _format_counters(counters: Mapping[str, int], width: int) -> list[str]:
|
|
352
|
+
lines = ["计数"]
|
|
353
|
+
if not counters:
|
|
354
|
+
lines.append(" 没有计数")
|
|
355
|
+
return lines
|
|
356
|
+
|
|
357
|
+
rows: list[tuple[str, str]] = []
|
|
358
|
+
handled: set[str] = set()
|
|
359
|
+
total = counters.get("mcp_servers_total")
|
|
360
|
+
cached = counters.get("mcp_servers_cached")
|
|
361
|
+
if total is not None or cached is not None:
|
|
362
|
+
rows.append(("MCP 服务器", f"缓存 {cached or 0} / 共 {total or 0}"))
|
|
363
|
+
handled.update(("mcp_servers_total", "mcp_servers_cached"))
|
|
364
|
+
|
|
365
|
+
hit = counters.get("skill_cache_hit")
|
|
366
|
+
miss = counters.get("skill_cache_miss")
|
|
367
|
+
if hit is not None or miss is not None:
|
|
368
|
+
rows.append(("技能缓存", f"命中 {hit or 0} · 未命中 {miss or 0}"))
|
|
369
|
+
handled.update(("skill_cache_hit", "skill_cache_miss"))
|
|
370
|
+
|
|
371
|
+
task_parts: list[str] = []
|
|
372
|
+
if "tasks_submitted" in counters:
|
|
373
|
+
task_parts.append(f"提交 {counters['tasks_submitted']}")
|
|
374
|
+
handled.add("tasks_submitted")
|
|
375
|
+
for key, label in (
|
|
376
|
+
("tasks_succeeded", "成功"),
|
|
377
|
+
("tasks_failed", "失败"),
|
|
378
|
+
("tasks_timed_out", "超时"),
|
|
379
|
+
("tasks_cancelled", "取消"),
|
|
380
|
+
):
|
|
381
|
+
value = counters.get(key)
|
|
382
|
+
if value:
|
|
383
|
+
task_parts.append(f"{label} {value}")
|
|
384
|
+
if value is not None:
|
|
385
|
+
handled.add(key)
|
|
386
|
+
if task_parts:
|
|
387
|
+
rows.append(("后台任务", " · ".join(task_parts)))
|
|
388
|
+
|
|
389
|
+
for key, value in sorted(counters.items()):
|
|
390
|
+
if key in handled:
|
|
391
|
+
continue
|
|
392
|
+
rows.append((_COUNTER_LABELS.get(key, key), str(value)))
|
|
393
|
+
lines.extend(_aligned_rows(rows, width))
|
|
394
|
+
return lines
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
@dataclass(slots=True, frozen=True)
|
|
398
|
+
class _TaskRow:
|
|
399
|
+
name: str
|
|
400
|
+
state: str
|
|
401
|
+
duration_ms: float
|
|
402
|
+
cpu_ms: float | None = None
|
|
403
|
+
attempts: int = 0
|
|
404
|
+
error: str | None = None
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def _format_tasks(
|
|
408
|
+
tasks: tuple[ScheduledTask, ...],
|
|
409
|
+
width: int,
|
|
410
|
+
metrics: dict[str, dict[str, float]],
|
|
411
|
+
) -> list[str]:
|
|
412
|
+
rows = _task_rows(tasks, metrics)
|
|
413
|
+
lines = ["后台任务"]
|
|
414
|
+
if not rows:
|
|
415
|
+
lines.append(" 没有提交任务")
|
|
416
|
+
return lines
|
|
417
|
+
if width < 56:
|
|
418
|
+
for row in rows:
|
|
419
|
+
name_lines = _wrap_display(row.name, max(1, width - 2))
|
|
420
|
+
lines.append(" " + name_lines[0])
|
|
421
|
+
for line in name_lines[1:]:
|
|
422
|
+
lines.append(" " + line)
|
|
423
|
+
label = _STATE_LABELS.get(row.state, row.state)
|
|
424
|
+
parts = [label, _task_duration(row)]
|
|
425
|
+
detail = _task_detail(row)
|
|
426
|
+
if detail:
|
|
427
|
+
parts.append(detail)
|
|
428
|
+
_append_wrapped(lines, " · ".join(parts), width, 4)
|
|
429
|
+
return lines
|
|
430
|
+
|
|
431
|
+
state_width = max(
|
|
432
|
+
display_width("状态"),
|
|
433
|
+
*(display_width(_STATE_LABELS.get(row.state, row.state)) for row in rows),
|
|
434
|
+
)
|
|
435
|
+
duration_width = max(
|
|
436
|
+
display_width("耗时"),
|
|
437
|
+
*(display_width(_task_duration(row)) for row in rows),
|
|
438
|
+
)
|
|
439
|
+
max_name = max(display_width(row.name) for row in rows)
|
|
440
|
+
name_width = min(max_name, max(1, width - 2 - state_width - duration_width - 4))
|
|
441
|
+
lines.append(
|
|
442
|
+
" "
|
|
443
|
+
+ _pad("名称", name_width)
|
|
444
|
+
+ " "
|
|
445
|
+
+ _pad("状态", state_width)
|
|
446
|
+
+ " "
|
|
447
|
+
+ _right("耗时", duration_width)
|
|
448
|
+
)
|
|
449
|
+
for row in rows:
|
|
450
|
+
label = _STATE_LABELS.get(row.state, row.state)
|
|
451
|
+
record = (
|
|
452
|
+
" "
|
|
453
|
+
+ _pad(row.name, name_width)
|
|
454
|
+
+ " "
|
|
455
|
+
+ _pad(label, state_width)
|
|
456
|
+
+ " "
|
|
457
|
+
+ _right(_task_duration(row), duration_width)
|
|
458
|
+
)
|
|
459
|
+
detail = _task_detail(row)
|
|
460
|
+
if not detail:
|
|
461
|
+
lines.append(record)
|
|
462
|
+
continue
|
|
463
|
+
detail_width = width - display_width(record) - 2
|
|
464
|
+
if detail_width > 0 and display_width(detail) <= detail_width:
|
|
465
|
+
lines.append(record + " " + detail)
|
|
466
|
+
continue
|
|
467
|
+
lines.append(record)
|
|
468
|
+
_append_wrapped(lines, detail, width, 4)
|
|
469
|
+
return lines
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def _task_rows(
|
|
473
|
+
tasks: tuple[ScheduledTask, ...],
|
|
474
|
+
metrics: dict[str, dict[str, float]],
|
|
475
|
+
) -> list[_TaskRow]:
|
|
476
|
+
rows: list[_TaskRow] = []
|
|
477
|
+
seen: set[str] = set()
|
|
478
|
+
for task in tasks:
|
|
479
|
+
values = metrics.get(task.name, {})
|
|
480
|
+
duration = values.get("duration_ms", task.duration_ms)
|
|
481
|
+
cpu = values.get("cpu_ms", task.cpu_ms)
|
|
482
|
+
rows.append(
|
|
483
|
+
_TaskRow(
|
|
484
|
+
name=task.name,
|
|
485
|
+
state=task.state,
|
|
486
|
+
duration_ms=duration,
|
|
487
|
+
cpu_ms=cpu,
|
|
488
|
+
attempts=task.attempts,
|
|
489
|
+
error=task.error,
|
|
490
|
+
)
|
|
491
|
+
)
|
|
492
|
+
seen.add(task.name)
|
|
493
|
+
for name, values in metrics.items():
|
|
494
|
+
if name in seen:
|
|
495
|
+
continue
|
|
496
|
+
rows.append(
|
|
497
|
+
_TaskRow(
|
|
498
|
+
name=name,
|
|
499
|
+
state="unknown",
|
|
500
|
+
duration_ms=values.get("duration_ms", 0.0),
|
|
501
|
+
cpu_ms=values.get("cpu_ms"),
|
|
502
|
+
)
|
|
503
|
+
)
|
|
504
|
+
return rows
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
def _task_duration(row: _TaskRow) -> str:
|
|
508
|
+
if row.state in ("pending", "running") and row.duration_ms <= 0:
|
|
509
|
+
return "—"
|
|
510
|
+
return _format_ms(row.duration_ms)
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
def _task_detail(row: _TaskRow) -> str:
|
|
514
|
+
parts: list[str] = []
|
|
515
|
+
if row.cpu_ms is not None:
|
|
516
|
+
parts.append(f"CPU {_format_ms(row.cpu_ms)}")
|
|
517
|
+
if row.attempts > 1:
|
|
518
|
+
parts.append(f"尝试 {row.attempts} 次")
|
|
519
|
+
if row.error:
|
|
520
|
+
parts.append(row.error.replace("\n", " "))
|
|
521
|
+
return " · ".join(parts)
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
def _format_specs(width: int) -> list[str]:
|
|
525
|
+
lines = ["初始化策略"]
|
|
526
|
+
if width >= 72:
|
|
527
|
+
name_width = min(
|
|
528
|
+
max(display_width(spec.name) for spec in INIT_SPECS),
|
|
529
|
+
max(1, width - 40),
|
|
530
|
+
)
|
|
531
|
+
for spec in INIT_SPECS:
|
|
532
|
+
policy = _POLICY_LABELS.get(spec.policy, spec.policy)
|
|
533
|
+
state = "已实现" if spec.implemented else "接口预留"
|
|
534
|
+
prefix = (
|
|
535
|
+
" "
|
|
536
|
+
+ _pad(spec.name, name_width)
|
|
537
|
+
+ " "
|
|
538
|
+
+ _pad(spec.phase, 10)
|
|
539
|
+
+ " "
|
|
540
|
+
+ _pad(policy, 4)
|
|
541
|
+
+ " "
|
|
542
|
+
+ _pad(state, 8)
|
|
543
|
+
+ " "
|
|
544
|
+
)
|
|
545
|
+
description_width = width - display_width(prefix)
|
|
546
|
+
if description_width >= display_width(spec.description):
|
|
547
|
+
lines.append(prefix + spec.description)
|
|
548
|
+
continue
|
|
549
|
+
lines.append(prefix.rstrip())
|
|
550
|
+
_append_wrapped(lines, spec.description, width, 6)
|
|
551
|
+
return lines
|
|
552
|
+
|
|
553
|
+
for spec in INIT_SPECS:
|
|
554
|
+
policy = _POLICY_LABELS.get(spec.policy, spec.policy)
|
|
555
|
+
state = "已实现" if spec.implemented else "接口预留"
|
|
556
|
+
lines.append(" " + _clip(spec.name, width - 2))
|
|
557
|
+
_append_wrapped(lines, f"阶段 {spec.phase}", width, 4)
|
|
558
|
+
_append_wrapped(lines, f"策略 {policy} · {state}", width, 4)
|
|
559
|
+
_append_wrapped(lines, spec.description, width, 4)
|
|
560
|
+
return lines
|