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,586 @@
|
|
|
1
|
+
"""``/permission`` 的参数解析与执行。
|
|
2
|
+
|
|
3
|
+
解析是纯函数,执行只依赖 :class:`~braid_coding_agent.permissions.engine.PermissionEngine`
|
|
4
|
+
与 :class:`~braid_coding_agent.permissions.store.PermissionStore`。REPL 与 TUI 各自把
|
|
5
|
+
返回的行渲染成自己的样式,命令语义只有这一份实现。
|
|
6
|
+
|
|
7
|
+
## 语法
|
|
8
|
+
|
|
9
|
+
```text
|
|
10
|
+
/permission
|
|
11
|
+
/permission show [tools|bash|paths|domains|all]
|
|
12
|
+
/permission mode [read-only|plan|accept-edits|full] [--project|--global]
|
|
13
|
+
/permission allow|ask|deny <表> <模式> [--project|--global] [--force]
|
|
14
|
+
/permission remove <表> <模式> [--project|--global]
|
|
15
|
+
/permission reset [session|project|global] [--yes]
|
|
16
|
+
/permission trust [--revoke]
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
模式里可以有空格,写法是 ``/permission allow bash "git status*"``。没有加引号时,
|
|
20
|
+
剩余 token 会用空格拼回一个模式,减少输入负担。
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import shlex
|
|
26
|
+
from dataclasses import dataclass
|
|
27
|
+
from pathlib import Path
|
|
28
|
+
from typing import Literal, cast
|
|
29
|
+
|
|
30
|
+
from braid_coding_agent.permissions.engine import PermissionEngine
|
|
31
|
+
from braid_coding_agent.permissions.model import (
|
|
32
|
+
PERMISSION_MODES,
|
|
33
|
+
RULE_SCOPES,
|
|
34
|
+
RULE_TABLES,
|
|
35
|
+
PermissionAction,
|
|
36
|
+
PermissionMode,
|
|
37
|
+
RuleScope,
|
|
38
|
+
RuleTable,
|
|
39
|
+
ScopedRule,
|
|
40
|
+
)
|
|
41
|
+
from braid_coding_agent.permissions.policy import mode_label
|
|
42
|
+
from braid_coding_agent.permissions.store import PermissionStore, PermissionStoreError
|
|
43
|
+
|
|
44
|
+
type CommandVerb = Literal[
|
|
45
|
+
"status", "show", "mode", "allow", "ask", "deny", "remove", "reset", "trust"
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
_VERBS: tuple[CommandVerb, ...] = (
|
|
49
|
+
"status",
|
|
50
|
+
"show",
|
|
51
|
+
"mode",
|
|
52
|
+
"allow",
|
|
53
|
+
"ask",
|
|
54
|
+
"deny",
|
|
55
|
+
"remove",
|
|
56
|
+
"reset",
|
|
57
|
+
"trust",
|
|
58
|
+
)
|
|
59
|
+
_SCOPE_LABELS: dict[RuleScope, str] = {
|
|
60
|
+
"builtin": "内置",
|
|
61
|
+
"global": "全局",
|
|
62
|
+
"project": "项目",
|
|
63
|
+
"session": "会话",
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class PermissionCommandError(ValueError):
|
|
68
|
+
"""命令参数不可用。REPL 打印它,不退出。"""
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass(slots=True, frozen=True)
|
|
72
|
+
class PermissionCommand:
|
|
73
|
+
"""解析后的命令。"""
|
|
74
|
+
|
|
75
|
+
verb: CommandVerb
|
|
76
|
+
table: RuleTable | None = None
|
|
77
|
+
pattern: str | None = None
|
|
78
|
+
mode: PermissionMode | None = None
|
|
79
|
+
scope: RuleScope | None = None
|
|
80
|
+
target: str | None = None
|
|
81
|
+
force: bool = False
|
|
82
|
+
yes: bool = False
|
|
83
|
+
revoke: bool = False
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@dataclass(slots=True, frozen=True)
|
|
87
|
+
class PermissionCommandResult:
|
|
88
|
+
"""命令输出。``lines`` 是 ``(role, text)``,role 取 notice / error / plain / dim。"""
|
|
89
|
+
|
|
90
|
+
lines: tuple[tuple[str, str], ...]
|
|
91
|
+
changed: bool = False
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def parse_permission_command(args: str) -> PermissionCommand:
|
|
95
|
+
"""把 ``/permission`` 后面的原文解析成命令。"""
|
|
96
|
+
try:
|
|
97
|
+
tokens = shlex.split(args)
|
|
98
|
+
except ValueError as exc:
|
|
99
|
+
raise PermissionCommandError(f"参数引号不匹配:{exc}") from exc
|
|
100
|
+
if not tokens:
|
|
101
|
+
return PermissionCommand("status")
|
|
102
|
+
|
|
103
|
+
verb = tokens[0].lower()
|
|
104
|
+
if verb not in _VERBS:
|
|
105
|
+
raise PermissionCommandError(
|
|
106
|
+
f"未知子命令 {verb};可用:show、mode、allow、ask、deny、remove、reset、trust"
|
|
107
|
+
)
|
|
108
|
+
cast_verb = verb
|
|
109
|
+
|
|
110
|
+
scope: RuleScope | None = None
|
|
111
|
+
force = False
|
|
112
|
+
yes = False
|
|
113
|
+
revoke = False
|
|
114
|
+
positional: list[str] = []
|
|
115
|
+
for token in tokens[1:]:
|
|
116
|
+
if token == "--project":
|
|
117
|
+
if scope == "global":
|
|
118
|
+
raise PermissionCommandError("--project 与 --global 只能选一个")
|
|
119
|
+
scope = "project"
|
|
120
|
+
elif token == "--global":
|
|
121
|
+
if scope == "project":
|
|
122
|
+
raise PermissionCommandError("--project 与 --global 只能选一个")
|
|
123
|
+
scope = "global"
|
|
124
|
+
elif token == "--force":
|
|
125
|
+
force = True
|
|
126
|
+
elif token == "--yes":
|
|
127
|
+
yes = True
|
|
128
|
+
elif token == "--revoke":
|
|
129
|
+
revoke = True
|
|
130
|
+
elif token.startswith("--"):
|
|
131
|
+
raise PermissionCommandError(f"未知参数 {token}")
|
|
132
|
+
else:
|
|
133
|
+
positional.append(token)
|
|
134
|
+
|
|
135
|
+
match cast_verb:
|
|
136
|
+
case "status":
|
|
137
|
+
_require_no_positional(positional, "/permission")
|
|
138
|
+
_reject_file_flags(scope, "status")
|
|
139
|
+
return PermissionCommand("status", force=force, yes=yes, revoke=revoke)
|
|
140
|
+
case "show":
|
|
141
|
+
if len(positional) > 1:
|
|
142
|
+
raise PermissionCommandError(
|
|
143
|
+
"用法:/permission show [tools|bash|paths|domains|all]"
|
|
144
|
+
)
|
|
145
|
+
target = positional[0].lower() if positional else "all"
|
|
146
|
+
if target != "all" and target not in RULE_TABLES:
|
|
147
|
+
raise PermissionCommandError(
|
|
148
|
+
f"未知规则表 {target};可用:{', '.join((*RULE_TABLES, 'all'))}"
|
|
149
|
+
)
|
|
150
|
+
_reject_file_flags(scope, "show")
|
|
151
|
+
return PermissionCommand("show", target=target, force=force, yes=yes, revoke=revoke)
|
|
152
|
+
case "mode":
|
|
153
|
+
if len(positional) > 1:
|
|
154
|
+
raise PermissionCommandError(
|
|
155
|
+
"用法:/permission mode [read-only|plan|accept-edits|full] [--project|--global]"
|
|
156
|
+
)
|
|
157
|
+
mode: PermissionMode | None = None
|
|
158
|
+
if positional:
|
|
159
|
+
name = positional[0]
|
|
160
|
+
if name not in PERMISSION_MODES:
|
|
161
|
+
raise PermissionCommandError(
|
|
162
|
+
f"未知模式 {name};可用:{', '.join(PERMISSION_MODES)}"
|
|
163
|
+
)
|
|
164
|
+
mode = name
|
|
165
|
+
return PermissionCommand(
|
|
166
|
+
"mode", mode=mode, scope=scope, force=force, yes=yes, revoke=revoke
|
|
167
|
+
)
|
|
168
|
+
case "allow" | "ask" | "deny":
|
|
169
|
+
table, pattern = _table_and_pattern(positional, cast_verb)
|
|
170
|
+
return PermissionCommand(
|
|
171
|
+
cast_verb,
|
|
172
|
+
table=table,
|
|
173
|
+
pattern=pattern,
|
|
174
|
+
scope=scope or "session",
|
|
175
|
+
force=force,
|
|
176
|
+
yes=yes,
|
|
177
|
+
revoke=revoke,
|
|
178
|
+
)
|
|
179
|
+
case "remove":
|
|
180
|
+
table, pattern = _table_and_pattern(positional, cast_verb)
|
|
181
|
+
return PermissionCommand(
|
|
182
|
+
"remove",
|
|
183
|
+
table=table,
|
|
184
|
+
pattern=pattern,
|
|
185
|
+
scope=scope,
|
|
186
|
+
force=force,
|
|
187
|
+
yes=yes,
|
|
188
|
+
revoke=revoke,
|
|
189
|
+
)
|
|
190
|
+
case "reset":
|
|
191
|
+
if len(positional) > 1:
|
|
192
|
+
raise PermissionCommandError(
|
|
193
|
+
"用法:/permission reset [session|project|global] [--yes]"
|
|
194
|
+
)
|
|
195
|
+
target = positional[0].lower() if positional else "session"
|
|
196
|
+
if target not in RULE_SCOPES or target == "builtin":
|
|
197
|
+
raise PermissionCommandError(
|
|
198
|
+
f"未知重置范围 {target};可用:session、project、global"
|
|
199
|
+
)
|
|
200
|
+
return PermissionCommand(
|
|
201
|
+
"reset",
|
|
202
|
+
target=target,
|
|
203
|
+
force=force,
|
|
204
|
+
yes=yes,
|
|
205
|
+
revoke=revoke,
|
|
206
|
+
)
|
|
207
|
+
case "trust":
|
|
208
|
+
_require_no_positional(positional, "/permission trust")
|
|
209
|
+
_reject_file_flags(scope, "trust")
|
|
210
|
+
return PermissionCommand("trust", force=force, yes=yes, revoke=revoke)
|
|
211
|
+
raise PermissionCommandError(f"未知子命令 {verb}")
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def execute_permission_command(
|
|
215
|
+
command: PermissionCommand,
|
|
216
|
+
engine: PermissionEngine,
|
|
217
|
+
store: PermissionStore | None = None,
|
|
218
|
+
) -> PermissionCommandResult:
|
|
219
|
+
"""执行命令。所有失败都折成 error 行,不抛给 REPL。"""
|
|
220
|
+
target_store = store if store is not None else engine.store
|
|
221
|
+
match command.verb:
|
|
222
|
+
case "status":
|
|
223
|
+
return _status(engine)
|
|
224
|
+
case "show":
|
|
225
|
+
return _show(engine, command.target or "all")
|
|
226
|
+
case "mode":
|
|
227
|
+
return _mode(engine, command, target_store)
|
|
228
|
+
case "allow" | "ask" | "deny":
|
|
229
|
+
return _add_rule(engine, command, target_store)
|
|
230
|
+
case "remove":
|
|
231
|
+
return _remove_rule(engine, command, target_store)
|
|
232
|
+
case "reset":
|
|
233
|
+
return _reset(engine, command, target_store)
|
|
234
|
+
case "trust":
|
|
235
|
+
return _trust(engine, command, target_store)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _status(engine: PermissionEngine) -> PermissionCommandResult:
|
|
239
|
+
settings = engine.settings
|
|
240
|
+
counts = _scope_counts(engine)
|
|
241
|
+
trusted = "已信任" if settings.project_trusted else "未信任(只采用 deny 规则)"
|
|
242
|
+
lines: list[tuple[str, str]] = [
|
|
243
|
+
(
|
|
244
|
+
"notice",
|
|
245
|
+
f"权限模式 {settings.mode} · 默认动作 {settings.default} · 规则 {engine.rule_count} 条"
|
|
246
|
+
f"(内置 {counts['builtin']} / 全局 {counts['global']} / "
|
|
247
|
+
f"项目 {counts['project']} / 会话 {counts['session']})",
|
|
248
|
+
),
|
|
249
|
+
("notice", f"项目配置:{trusted}"),
|
|
250
|
+
(
|
|
251
|
+
"notice",
|
|
252
|
+
f"本会话已批准 {len(engine.approved_fingerprints)} 类操作 · "
|
|
253
|
+
f"批准等待上限 {settings.approval_timeout_seconds:g} 秒",
|
|
254
|
+
),
|
|
255
|
+
]
|
|
256
|
+
recent = engine.recent[-8:]
|
|
257
|
+
if not recent:
|
|
258
|
+
lines.append(("dim", "最近没有权限判定"))
|
|
259
|
+
else:
|
|
260
|
+
lines.append(("plain", "最近判定:"))
|
|
261
|
+
for decision in recent:
|
|
262
|
+
lines.append(
|
|
263
|
+
(
|
|
264
|
+
"plain",
|
|
265
|
+
f" {decision.tool_name} {decision.action} {decision.rule}",
|
|
266
|
+
)
|
|
267
|
+
)
|
|
268
|
+
return PermissionCommandResult(tuple(lines))
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def _show(engine: PermissionEngine, target: str) -> PermissionCommandResult:
|
|
272
|
+
rules = engine.rules
|
|
273
|
+
if target != "all":
|
|
274
|
+
rules = tuple(rule for rule in rules if rule.table == target)
|
|
275
|
+
if not rules:
|
|
276
|
+
return PermissionCommandResult((("dim", "没有匹配的规则"),))
|
|
277
|
+
lines: list[tuple[str, str]] = []
|
|
278
|
+
for scope in RULE_SCOPES:
|
|
279
|
+
subset = [rule for rule in rules if rule.scope == scope]
|
|
280
|
+
if not subset:
|
|
281
|
+
continue
|
|
282
|
+
header = f"[{_SCOPE_LABELS[scope]}] {len(subset)} 条"
|
|
283
|
+
if scope == "project" and not engine.settings.project_trusted:
|
|
284
|
+
header += "(项目未信任,只显示 deny 规则)"
|
|
285
|
+
lines.append(("notice", header))
|
|
286
|
+
for rule in subset:
|
|
287
|
+
source = f" ← {rule.source}" if rule.source and scope != "builtin" else ""
|
|
288
|
+
lines.append(
|
|
289
|
+
(
|
|
290
|
+
"plain",
|
|
291
|
+
f' {rule.table} "{rule.pattern}" {rule.effect}{source}',
|
|
292
|
+
)
|
|
293
|
+
)
|
|
294
|
+
return PermissionCommandResult(tuple(lines))
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _mode(
|
|
298
|
+
engine: PermissionEngine, command: PermissionCommand, store: PermissionStore | None
|
|
299
|
+
) -> PermissionCommandResult:
|
|
300
|
+
if command.mode is None:
|
|
301
|
+
lines: list[tuple[str, str]] = [("notice", "可用模式:")]
|
|
302
|
+
for mode in PERMISSION_MODES:
|
|
303
|
+
marker = "→" if mode == engine.settings.mode else " "
|
|
304
|
+
lines.append(("plain", f" {marker} {mode:<12} {mode_label(mode)}"))
|
|
305
|
+
lines.append(("notice", f"当前:{engine.settings.mode}"))
|
|
306
|
+
return PermissionCommandResult(tuple(lines))
|
|
307
|
+
|
|
308
|
+
mode = command.mode
|
|
309
|
+
scope = command.scope
|
|
310
|
+
if scope is None:
|
|
311
|
+
previous = engine.set_mode(mode)
|
|
312
|
+
return PermissionCommandResult(
|
|
313
|
+
(
|
|
314
|
+
(
|
|
315
|
+
"notice",
|
|
316
|
+
f"权限模式已切换:{previous} → {mode}(仅本次会话;"
|
|
317
|
+
"加 --project 或 --global 写入规则文件)",
|
|
318
|
+
),
|
|
319
|
+
),
|
|
320
|
+
changed=True,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
if store is None:
|
|
324
|
+
return PermissionCommandResult((("error", "本次运行没有规则存储,不能写入模式"),))
|
|
325
|
+
if scope == "project" and not engine.settings.project_trusted and not command.force:
|
|
326
|
+
return PermissionCommandResult(
|
|
327
|
+
(("error", "项目配置尚未信任;先运行 /permission trust 或加 --force"),)
|
|
328
|
+
)
|
|
329
|
+
try:
|
|
330
|
+
store.write_mode(mode, scope=scope)
|
|
331
|
+
except PermissionStoreError as exc:
|
|
332
|
+
return PermissionCommandResult((("error", f"写入模式失败:{exc}"),))
|
|
333
|
+
previous = engine.set_mode(mode)
|
|
334
|
+
return PermissionCommandResult(
|
|
335
|
+
(
|
|
336
|
+
(
|
|
337
|
+
"notice",
|
|
338
|
+
f"权限模式已切换并写入{_SCOPE_LABELS[scope]}规则文件:{previous} → {mode}",
|
|
339
|
+
),
|
|
340
|
+
),
|
|
341
|
+
changed=True,
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def _add_rule(
|
|
346
|
+
engine: PermissionEngine, command: PermissionCommand, store: PermissionStore | None
|
|
347
|
+
) -> PermissionCommandResult:
|
|
348
|
+
table = command.table
|
|
349
|
+
pattern = command.pattern
|
|
350
|
+
if table is None or pattern is None:
|
|
351
|
+
return PermissionCommandResult((("error", "缺少规则表或模式"),))
|
|
352
|
+
effect = cast(PermissionAction, command.verb)
|
|
353
|
+
scope = command.scope or "session"
|
|
354
|
+
rule = ScopedRule(
|
|
355
|
+
table,
|
|
356
|
+
pattern,
|
|
357
|
+
effect,
|
|
358
|
+
scope,
|
|
359
|
+
str(store.project_path)
|
|
360
|
+
if scope == "project" and store is not None
|
|
361
|
+
else (str(store.global_path) if scope == "global" and store is not None else "session"),
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
if scope == "session":
|
|
365
|
+
deny = _exact_deny(engine, table, pattern)
|
|
366
|
+
if effect == "allow" and deny is not None:
|
|
367
|
+
return PermissionCommandResult(
|
|
368
|
+
(
|
|
369
|
+
(
|
|
370
|
+
"error",
|
|
371
|
+
f"同一模式已有 deny(来源 {_SCOPE_LABELS[deny.scope]});"
|
|
372
|
+
"这条 allow 不会生效。先 remove 那条 deny,或写更具体的模式。",
|
|
373
|
+
),
|
|
374
|
+
)
|
|
375
|
+
)
|
|
376
|
+
engine.add_rule(rule)
|
|
377
|
+
return PermissionCommandResult(
|
|
378
|
+
(("notice", f'已添加会话规则:{table} "{pattern}" = {effect}'),),
|
|
379
|
+
changed=True,
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
if store is None:
|
|
383
|
+
return PermissionCommandResult((("error", "本次运行没有规则存储,不能写入文件"),))
|
|
384
|
+
if scope == "project" and not engine.settings.project_trusted and not command.force:
|
|
385
|
+
return PermissionCommandResult(
|
|
386
|
+
(("error", "项目配置尚未信任;先运行 /permission trust 或加 --force"),)
|
|
387
|
+
)
|
|
388
|
+
try:
|
|
389
|
+
store.append_rule(rule, replace=command.force)
|
|
390
|
+
except PermissionStoreError as exc:
|
|
391
|
+
return PermissionCommandResult((("error", f"写入规则失败:{exc}"),))
|
|
392
|
+
engine.add_base_rule(rule)
|
|
393
|
+
lines: list[tuple[str, str]] = [
|
|
394
|
+
(
|
|
395
|
+
"notice",
|
|
396
|
+
f'已写入{_SCOPE_LABELS[scope]}规则:{table} "{pattern}" = {effect}',
|
|
397
|
+
)
|
|
398
|
+
]
|
|
399
|
+
if scope == "project" and not engine.settings.project_trusted:
|
|
400
|
+
lines.append(("dim", "项目未信任;这条规则只在本次会话生效,下次启动会被忽略。"))
|
|
401
|
+
return PermissionCommandResult(tuple(lines), changed=True)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def _remove_rule(
|
|
405
|
+
engine: PermissionEngine, command: PermissionCommand, store: PermissionStore | None
|
|
406
|
+
) -> PermissionCommandResult:
|
|
407
|
+
table = command.table
|
|
408
|
+
pattern = command.pattern
|
|
409
|
+
if table is None or pattern is None:
|
|
410
|
+
return PermissionCommandResult((("error", "缺少规则表或模式"),))
|
|
411
|
+
matches = [rule for rule in engine.rules if (rule.table, rule.pattern) == (table, pattern)]
|
|
412
|
+
if not matches:
|
|
413
|
+
return PermissionCommandResult((("error", f'没有找到规则:{table} "{pattern}"'),))
|
|
414
|
+
if command.scope is not None:
|
|
415
|
+
matches = [rule for rule in matches if rule.scope == command.scope]
|
|
416
|
+
if not matches:
|
|
417
|
+
return PermissionCommandResult(
|
|
418
|
+
(("error", f"{_SCOPE_LABELS[command.scope]}里没有这条规则"),)
|
|
419
|
+
)
|
|
420
|
+
else:
|
|
421
|
+
session_matches = [rule for rule in matches if rule.scope == "session"]
|
|
422
|
+
if session_matches:
|
|
423
|
+
matches = session_matches
|
|
424
|
+
if any(rule.scope == "builtin" for rule in matches):
|
|
425
|
+
return PermissionCommandResult(
|
|
426
|
+
(
|
|
427
|
+
(
|
|
428
|
+
"error",
|
|
429
|
+
f'不能删除内置规则 {table} "{pattern}";可以添加一条更具体的 allow 覆盖它。',
|
|
430
|
+
),
|
|
431
|
+
)
|
|
432
|
+
)
|
|
433
|
+
rule = matches[0]
|
|
434
|
+
if rule.scope == "session":
|
|
435
|
+
if not engine.remove_rule(rule):
|
|
436
|
+
return PermissionCommandResult((("error", "删除会话规则失败"),))
|
|
437
|
+
return PermissionCommandResult(
|
|
438
|
+
(("notice", f'已删除会话规则:{table} "{pattern}"'),), changed=True
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
if store is None:
|
|
442
|
+
return PermissionCommandResult((("error", "本次运行没有规则存储,不能删除文件里的规则"),))
|
|
443
|
+
if rule.source != str(_path_for(store, rule.scope)):
|
|
444
|
+
return PermissionCommandResult(
|
|
445
|
+
(
|
|
446
|
+
(
|
|
447
|
+
"error",
|
|
448
|
+
f"这条规则来自 {rule.source},请直接编辑该配置文件;"
|
|
449
|
+
"运行时命令只删除 permissions.toml / permissions.local.toml 里的规则。",
|
|
450
|
+
),
|
|
451
|
+
)
|
|
452
|
+
)
|
|
453
|
+
if not store.remove_rule(rule):
|
|
454
|
+
return PermissionCommandResult((("error", "规则文件里没有这条规则"),))
|
|
455
|
+
engine.remove_base_rule(rule)
|
|
456
|
+
return PermissionCommandResult(
|
|
457
|
+
(
|
|
458
|
+
(
|
|
459
|
+
"notice",
|
|
460
|
+
f'已删除{_SCOPE_LABELS[rule.scope]}规则:{table} "{pattern}"',
|
|
461
|
+
),
|
|
462
|
+
),
|
|
463
|
+
changed=True,
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def _reset(
|
|
468
|
+
engine: PermissionEngine, command: PermissionCommand, store: PermissionStore | None
|
|
469
|
+
) -> PermissionCommandResult:
|
|
470
|
+
target = command.target or "session"
|
|
471
|
+
if target == "session":
|
|
472
|
+
rules = engine.reset_session_rules()
|
|
473
|
+
approvals = len(engine.approved_fingerprints)
|
|
474
|
+
engine.clear_approvals()
|
|
475
|
+
return PermissionCommandResult(
|
|
476
|
+
(
|
|
477
|
+
(
|
|
478
|
+
"notice",
|
|
479
|
+
f"已清空本会话批准 {approvals} 类操作与 {rules} 条会话规则",
|
|
480
|
+
),
|
|
481
|
+
),
|
|
482
|
+
changed=True,
|
|
483
|
+
)
|
|
484
|
+
if target not in ("project", "global"):
|
|
485
|
+
return PermissionCommandResult((("error", f"未知重置范围 {target}"),))
|
|
486
|
+
scope = cast(RuleScope, target)
|
|
487
|
+
if not command.yes:
|
|
488
|
+
return PermissionCommandResult(
|
|
489
|
+
(
|
|
490
|
+
(
|
|
491
|
+
"notice",
|
|
492
|
+
f"将清空{_SCOPE_LABELS[scope]}规则文件与内存规则。确认请加 --yes。",
|
|
493
|
+
),
|
|
494
|
+
)
|
|
495
|
+
)
|
|
496
|
+
if store is None:
|
|
497
|
+
return PermissionCommandResult((("error", "本次运行没有规则存储,不能清空文件"),))
|
|
498
|
+
try:
|
|
499
|
+
count = store.reset(scope=scope)
|
|
500
|
+
except PermissionStoreError as exc:
|
|
501
|
+
return PermissionCommandResult((("error", f"清空规则失败:{exc}"),))
|
|
502
|
+
engine.reset_base_rules(scope, source=str(_path_for(store, scope)))
|
|
503
|
+
return PermissionCommandResult(
|
|
504
|
+
(("notice", f"已清空{_SCOPE_LABELS[scope]}运行时规则 {count} 条"),), changed=True
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
def _trust(
|
|
509
|
+
engine: PermissionEngine, command: PermissionCommand, store: PermissionStore | None
|
|
510
|
+
) -> PermissionCommandResult:
|
|
511
|
+
if store is None:
|
|
512
|
+
return PermissionCommandResult((("error", "本次运行没有规则存储,不能记录项目信任"),))
|
|
513
|
+
if command.revoke:
|
|
514
|
+
removed = store.revoke_project()
|
|
515
|
+
engine.set_project_trusted(False)
|
|
516
|
+
if not removed:
|
|
517
|
+
return PermissionCommandResult((("notice", "这个项目本来就不在信任表里"),))
|
|
518
|
+
return PermissionCommandResult(
|
|
519
|
+
(("notice", "已撤销项目信任;项目配置的 allow / ask 规则下次启动不再生效"),),
|
|
520
|
+
changed=True,
|
|
521
|
+
)
|
|
522
|
+
try:
|
|
523
|
+
record = store.remember_project()
|
|
524
|
+
except PermissionStoreError as exc:
|
|
525
|
+
return PermissionCommandResult((("error", f"写入项目信任失败:{exc}"),))
|
|
526
|
+
engine.set_project_trusted(True)
|
|
527
|
+
return PermissionCommandResult(
|
|
528
|
+
(
|
|
529
|
+
(
|
|
530
|
+
"notice",
|
|
531
|
+
f"已信任项目 {store.cwd}(指纹 {record.fingerprint[:12]})。"
|
|
532
|
+
"项目配置的 allow / ask 规则将在下次启动时生效;"
|
|
533
|
+
"本次会话仍按已加载的规则判定。",
|
|
534
|
+
),
|
|
535
|
+
),
|
|
536
|
+
changed=True,
|
|
537
|
+
)
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
def _scope_counts(engine: PermissionEngine) -> dict[RuleScope, int]:
|
|
541
|
+
counts: dict[RuleScope, int] = {scope: 0 for scope in RULE_SCOPES}
|
|
542
|
+
for rule in engine.rules:
|
|
543
|
+
counts[rule.scope] += 1
|
|
544
|
+
return counts
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
def _exact_deny(engine: PermissionEngine, table: RuleTable, pattern: str) -> ScopedRule | None:
|
|
548
|
+
for rule in engine.rules:
|
|
549
|
+
if rule.table == table and rule.pattern == pattern and rule.effect == "deny":
|
|
550
|
+
return rule
|
|
551
|
+
return None
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
def _path_for(store: PermissionStore, scope: RuleScope) -> Path:
|
|
555
|
+
return store.global_path if scope == "global" else store.project_path
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
def _table_and_pattern(positional: list[str], verb: CommandVerb) -> tuple[RuleTable, str]:
|
|
559
|
+
if len(positional) < 2:
|
|
560
|
+
raise PermissionCommandError(f"用法:/permission {verb} <tools|bash|paths|domains> <模式>")
|
|
561
|
+
table = positional[0].lower()
|
|
562
|
+
if table not in RULE_TABLES:
|
|
563
|
+
raise PermissionCommandError(f"未知规则表 {table};可用:{', '.join(RULE_TABLES)}")
|
|
564
|
+
pattern = " ".join(positional[1:]).strip()
|
|
565
|
+
if not pattern:
|
|
566
|
+
raise PermissionCommandError("规则的模式不能为空")
|
|
567
|
+
return table, pattern
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def _require_no_positional(positional: list[str], usage: str) -> None:
|
|
571
|
+
if positional:
|
|
572
|
+
raise PermissionCommandError(f"{usage} 不接受参数,收到:{' '.join(positional)}")
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
def _reject_file_flags(scope: RuleScope | None, verb: str) -> None:
|
|
576
|
+
if scope is not None:
|
|
577
|
+
raise PermissionCommandError(f"/permission {verb} 不接受 --project 或 --global")
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
__all__ = [
|
|
581
|
+
"PermissionCommand",
|
|
582
|
+
"PermissionCommandError",
|
|
583
|
+
"PermissionCommandResult",
|
|
584
|
+
"execute_permission_command",
|
|
585
|
+
"parse_permission_command",
|
|
586
|
+
]
|