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.
Files changed (192) hide show
  1. braid_coding_agent/__init__.py +13 -0
  2. braid_coding_agent/assembly.py +460 -0
  3. braid_coding_agent/cancellation.py +25 -0
  4. braid_coding_agent/checkpoint/__init__.py +106 -0
  5. braid_coding_agent/checkpoint/git.py +305 -0
  6. braid_coding_agent/checkpoint/settings.py +26 -0
  7. braid_coding_agent/checkpoint/store.py +1261 -0
  8. braid_coding_agent/checkpoint/workspace.py +220 -0
  9. braid_coding_agent/cli.py +2168 -0
  10. braid_coding_agent/commands/__init__.py +50 -0
  11. braid_coding_agent/commands/auth.py +161 -0
  12. braid_coding_agent/commands/config.py +180 -0
  13. braid_coding_agent/commands/doctor.py +511 -0
  14. braid_coding_agent/commands/mcp.py +928 -0
  15. braid_coding_agent/commands/models.py +95 -0
  16. braid_coding_agent/commands/setup.py +89 -0
  17. braid_coding_agent/commands/uninstall.py +277 -0
  18. braid_coding_agent/commands/upgrade.py +381 -0
  19. braid_coding_agent/compaction/__init__.py +174 -0
  20. braid_coding_agent/compaction/prepare.py +430 -0
  21. braid_coding_agent/compaction/summarize.py +542 -0
  22. braid_coding_agent/compaction/tokens.py +204 -0
  23. braid_coding_agent/compaction/tool_results.py +196 -0
  24. braid_coding_agent/compaction/utils.py +181 -0
  25. braid_coding_agent/config.py +2776 -0
  26. braid_coding_agent/config_files.py +191 -0
  27. braid_coding_agent/credentials.py +252 -0
  28. braid_coding_agent/discovery.py +415 -0
  29. braid_coding_agent/extensions/__init__.py +77 -0
  30. braid_coding_agent/extensions/api.py +480 -0
  31. braid_coding_agent/extensions/runner.py +553 -0
  32. braid_coding_agent/formatting/__init__.py +62 -0
  33. braid_coding_agent/formatting/settings.py +87 -0
  34. braid_coding_agent/formatting/tool.py +179 -0
  35. braid_coding_agent/lsp/__init__.py +105 -0
  36. braid_coding_agent/lsp/client.py +546 -0
  37. braid_coding_agent/lsp/manager.py +187 -0
  38. braid_coding_agent/lsp/model.py +316 -0
  39. braid_coding_agent/lsp/protocol.py +105 -0
  40. braid_coding_agent/lsp/settings.py +120 -0
  41. braid_coding_agent/lsp/tool.py +429 -0
  42. braid_coding_agent/mcp/__init__.py +101 -0
  43. braid_coding_agent/mcp/cache.py +230 -0
  44. braid_coding_agent/mcp/catalog.py +108 -0
  45. braid_coding_agent/mcp/config.py +664 -0
  46. braid_coding_agent/mcp/config_store.py +142 -0
  47. braid_coding_agent/mcp/inspector.py +85 -0
  48. braid_coding_agent/mcp/panel.py +254 -0
  49. braid_coding_agent/mcp/runtime.py +1060 -0
  50. braid_coding_agent/mcp/tools.py +579 -0
  51. braid_coding_agent/mcp/trust.py +150 -0
  52. braid_coding_agent/mcp/tui_panel.py +444 -0
  53. braid_coding_agent/messages.py +152 -0
  54. braid_coding_agent/models_view.py +105 -0
  55. braid_coding_agent/modes/__init__.py +64 -0
  56. braid_coding_agent/modes/json_mode.py +480 -0
  57. braid_coding_agent/modes/lifecycle.py +217 -0
  58. braid_coding_agent/modes/print_mode.py +257 -0
  59. braid_coding_agent/modes/repl.py +2687 -0
  60. braid_coding_agent/modes/repl_commands.py +263 -0
  61. braid_coding_agent/modes/rpc.py +953 -0
  62. braid_coding_agent/modes/skill_commands.py +88 -0
  63. braid_coding_agent/modes/tui.py +2226 -0
  64. braid_coding_agent/modes/tui_control.py +90 -0
  65. braid_coding_agent/modes/tui_model.py +1043 -0
  66. braid_coding_agent/modes/tui_render.py +327 -0
  67. braid_coding_agent/modes/tui_rich.py +751 -0
  68. braid_coding_agent/modes/tui_style.py +42 -0
  69. braid_coding_agent/modes/wire.py +81 -0
  70. braid_coding_agent/netsafe.py +82 -0
  71. braid_coding_agent/network/__init__.py +20 -0
  72. braid_coding_agent/network/cache.py +77 -0
  73. braid_coding_agent/network/fetch.py +311 -0
  74. braid_coding_agent/network/html_text.py +160 -0
  75. braid_coding_agent/network/search.py +347 -0
  76. braid_coding_agent/network/settings.py +103 -0
  77. braid_coding_agent/network/urls.py +89 -0
  78. braid_coding_agent/observability/__init__.py +101 -0
  79. braid_coding_agent/observability/audit.py +124 -0
  80. braid_coding_agent/observability/collector.py +314 -0
  81. braid_coding_agent/observability/exporter.py +74 -0
  82. braid_coding_agent/observability/logging_setup.py +149 -0
  83. braid_coding_agent/observability/redaction.py +96 -0
  84. braid_coding_agent/observability/settings.py +29 -0
  85. braid_coding_agent/observability/spans.py +239 -0
  86. braid_coding_agent/permissions/__init__.py +185 -0
  87. braid_coding_agent/permissions/command.py +586 -0
  88. braid_coding_agent/permissions/engine.py +522 -0
  89. braid_coding_agent/permissions/evaluate.py +309 -0
  90. braid_coding_agent/permissions/matching.py +233 -0
  91. braid_coding_agent/permissions/model.py +278 -0
  92. braid_coding_agent/permissions/policy.py +99 -0
  93. braid_coding_agent/permissions/readonly.py +236 -0
  94. braid_coding_agent/permissions/store.py +379 -0
  95. braid_coding_agent/project_init.py +313 -0
  96. braid_coding_agent/prompt/__init__.py +98 -0
  97. braid_coding_agent/prompt/context_files.py +253 -0
  98. braid_coding_agent/prompt/project_context.py +172 -0
  99. braid_coding_agent/prompt/system_prompt.py +378 -0
  100. braid_coding_agent/questions.py +71 -0
  101. braid_coding_agent/render/__init__.py +56 -0
  102. braid_coding_agent/render/batch.py +66 -0
  103. braid_coding_agent/render/history.py +143 -0
  104. braid_coding_agent/render/stream.py +823 -0
  105. braid_coding_agent/render/theme.py +243 -0
  106. braid_coding_agent/sandbox/__init__.py +89 -0
  107. braid_coding_agent/sandbox/backend.py +184 -0
  108. braid_coding_agent/sandbox/bubblewrap.py +96 -0
  109. braid_coding_agent/sandbox/operations.py +301 -0
  110. braid_coding_agent/sandbox/policy.py +221 -0
  111. braid_coding_agent/sandbox/proxy.py +320 -0
  112. braid_coding_agent/sandbox/seatbelt.py +222 -0
  113. braid_coding_agent/sdk/__init__.py +31 -0
  114. braid_coding_agent/sdk/async_client.py +366 -0
  115. braid_coding_agent/sdk/client.py +386 -0
  116. braid_coding_agent/sdk/errors.py +52 -0
  117. braid_coding_agent/sdk/events.py +162 -0
  118. braid_coding_agent/sdk/types.py +63 -0
  119. braid_coding_agent/sdk/wire.py +59 -0
  120. braid_coding_agent/server/__init__.py +54 -0
  121. braid_coding_agent/server/errors.py +52 -0
  122. braid_coding_agent/server/http_server.py +419 -0
  123. braid_coding_agent/server/hub.py +530 -0
  124. braid_coding_agent/server/openapi.py +157 -0
  125. braid_coding_agent/server/registry.py +354 -0
  126. braid_coding_agent/server/router.py +275 -0
  127. braid_coding_agent/server/run.py +148 -0
  128. braid_coding_agent/server/schema.py +194 -0
  129. braid_coding_agent/server/service.py +975 -0
  130. braid_coding_agent/session/__init__.py +293 -0
  131. braid_coding_agent/session/agent_session.py +2706 -0
  132. braid_coding_agent/session/context_breakdown.py +302 -0
  133. braid_coding_agent/session/discovery.py +212 -0
  134. braid_coding_agent/session/entries.py +809 -0
  135. braid_coding_agent/session/evidence.py +295 -0
  136. braid_coding_agent/session/lock.py +176 -0
  137. braid_coding_agent/session/manager.py +1185 -0
  138. braid_coding_agent/session/plan.py +243 -0
  139. braid_coding_agent/session/plan_mode.py +423 -0
  140. braid_coding_agent/session/projection.py +262 -0
  141. braid_coding_agent/session/rewind.py +257 -0
  142. braid_coding_agent/session/status.py +240 -0
  143. braid_coding_agent/settings.py +94 -0
  144. braid_coding_agent/skills/__init__.py +70 -0
  145. braid_coding_agent/skills/loader.py +681 -0
  146. braid_coding_agent/startup/__init__.py +76 -0
  147. braid_coding_agent/startup/cache.py +72 -0
  148. braid_coding_agent/startup/diagnostics.py +37 -0
  149. braid_coding_agent/startup/monitor.py +245 -0
  150. braid_coding_agent/startup/phases.py +79 -0
  151. braid_coding_agent/startup/project.py +165 -0
  152. braid_coding_agent/startup/report.py +560 -0
  153. braid_coding_agent/startup/scheduler.py +347 -0
  154. braid_coding_agent/statusline.py +163 -0
  155. braid_coding_agent/subagents/__init__.py +22 -0
  156. braid_coding_agent/subagents/contract.py +75 -0
  157. braid_coding_agent/subagents/definitions.py +583 -0
  158. braid_coding_agent/subagents/registry.py +277 -0
  159. braid_coding_agent/subagents/runtime.py +610 -0
  160. braid_coding_agent/subagents/settings.py +156 -0
  161. braid_coding_agent/subagents/worktree.py +63 -0
  162. braid_coding_agent/tools/__init__.py +327 -0
  163. braid_coding_agent/tools/args.py +87 -0
  164. braid_coding_agent/tools/bash.py +359 -0
  165. braid_coding_agent/tools/edit.py +298 -0
  166. braid_coding_agent/tools/edit_diff.py +344 -0
  167. braid_coding_agent/tools/find.py +158 -0
  168. braid_coding_agent/tools/grep.py +305 -0
  169. braid_coding_agent/tools/ignore.py +198 -0
  170. braid_coding_agent/tools/ls.py +127 -0
  171. braid_coding_agent/tools/mutation_queue.py +90 -0
  172. braid_coding_agent/tools/operations.py +274 -0
  173. braid_coding_agent/tools/output_accumulator.py +282 -0
  174. braid_coding_agent/tools/output_store.py +244 -0
  175. braid_coding_agent/tools/path_utils.py +91 -0
  176. braid_coding_agent/tools/plan.py +141 -0
  177. braid_coding_agent/tools/prompt_notes.py +52 -0
  178. braid_coding_agent/tools/question.py +180 -0
  179. braid_coding_agent/tools/read.py +223 -0
  180. braid_coding_agent/tools/submit_plan.py +102 -0
  181. braid_coding_agent/tools/truncate.py +423 -0
  182. braid_coding_agent/tools/write.py +100 -0
  183. braid_coding_agent/untrusted.py +50 -0
  184. braid_coding_agent/verify/__init__.py +78 -0
  185. braid_coding_agent/verify/runner.py +260 -0
  186. braid_coding_agent/verify/settings.py +64 -0
  187. braid_coding_agent/verify/tool.py +143 -0
  188. braid_coding_agent-0.1.0.dist-info/METADATA +19 -0
  189. braid_coding_agent-0.1.0.dist-info/RECORD +192 -0
  190. braid_coding_agent-0.1.0.dist-info/WHEEL +4 -0
  191. braid_coding_agent-0.1.0.dist-info/entry_points.txt +3 -0
  192. braid_coding_agent-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,220 @@
1
+ """项目仓库上的 Git 操作:状态、提交、分支、工作树。
2
+
3
+ 与检查点分开的理由:检查点写的是自己的影子仓库,这里的每条命令都**改用户的仓库**。
4
+ 两类动作混在一个类里,迟早会出现"撤销一次编辑顺手改了他的暂存区"这种事。
5
+
6
+ ## 提交为什么先看完再动手
7
+
8
+ ``/commit`` 的顺序固定为:看状态 → 看差异 → 问确认 → ``git add -A`` → ``git commit``。
9
+ 先问再暂存的理由很实际:``add -A`` 之后用户即使反悔,暂存区也已经变了;而一次
10
+ 未确认的暂存会让下一次 ``git diff`` 的输出跟他记忆里的不一样。
11
+
12
+ ``--no-verify`` 一律不加:跳过用户自己配的 pre-commit 钩子等于替他做了"这些检查
13
+ 不重要"的决定。钩子失败就把失败原文交回去。
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import os
19
+ from dataclasses import dataclass
20
+ from typing import Final
21
+
22
+ from braid_coding_agent.checkpoint.git import Git, GitError
23
+
24
+ #: 分支名里不允许出现的字符(其余交给 ``git check-ref-format`` 判定)。
25
+ _BAD_BRANCH_CHARS: Final[str] = " \t\n\r~^:?*[\\"
26
+
27
+
28
+ class GitWorkspaceError(RuntimeError):
29
+ """用户仓库上的操作不可用(不是仓库、没有改动、分支名不合法)。"""
30
+
31
+
32
+ @dataclass(slots=True, frozen=True)
33
+ class CommitResult:
34
+ """一次提交的结果。``short_hash`` 是 7 位以上的短哈希。"""
35
+
36
+ short_hash: str
37
+ subject: str
38
+
39
+
40
+ @dataclass(slots=True, frozen=True)
41
+ class WorktreeEntry:
42
+ """``git worktree list`` 的一项。分离头指针时 ``branch`` 为空。"""
43
+
44
+ path: str
45
+ head: str
46
+ branch: str
47
+
48
+ def describe(self) -> str:
49
+ label = self.branch or "(detached)"
50
+ return f"{self.path} · {label}"
51
+
52
+
53
+ class GitWorkspace:
54
+ """在工作目录所属的仓库上跑 git。构造时不触盘。"""
55
+
56
+ def __init__(self, *, cwd: str, git: Git | None = None) -> None:
57
+ self._cwd = os.path.abspath(cwd)
58
+ self._git = git if git is not None else Git(cwd=self._cwd)
59
+
60
+ @property
61
+ def cwd(self) -> str:
62
+ return self._cwd
63
+
64
+ # ------------------------------------------------------------------ 判定
65
+
66
+ async def is_repo(self) -> bool:
67
+ """工作目录是否在一个 git 工作树里。不抛异常,供界面分支。"""
68
+ result = await self._git.run("rev-parse", "--is-inside-work-tree")
69
+ return result.ok and result.text.strip() == "true"
70
+
71
+ async def require_repo(self) -> None:
72
+ if not await self.is_repo():
73
+ raise GitWorkspaceError(
74
+ f"{self._cwd} 不是 Git 仓库:提交、分支与工作树需要先 git init;"
75
+ "文件检查点不受影响(/undo、/redo、/diff 仍可用)"
76
+ )
77
+
78
+ async def root(self) -> str:
79
+ """仓库根目录。子目录里启动时用它说明"提交的是哪个仓库"。"""
80
+ await self.require_repo()
81
+ return await self._git.out("rev-parse", "--show-toplevel")
82
+
83
+ # ------------------------------------------------------------------ 只读
84
+
85
+ async def status_short(self) -> str:
86
+ """``git status --short --branch``。第一行是分支,之后是改动。"""
87
+ await self.require_repo()
88
+ return await self._git.out("status", "--short", "--branch")
89
+
90
+ async def current_branch(self) -> str:
91
+ """当前分支名;处于分离头指针时返回 ``HEAD``。"""
92
+ await self.require_repo()
93
+ return await self._git.out("rev-parse", "--abbrev-ref", "HEAD")
94
+
95
+ async def diff(self) -> str:
96
+ """工作树相对索引的全部差异。"""
97
+ await self.require_repo()
98
+ return await self._git.out("diff", "--no-color")
99
+
100
+ async def diff_stat(self) -> str:
101
+ """``git diff --stat``。提交前给用户看的那一行摘要。"""
102
+ await self.require_repo()
103
+ return await self._git.out("diff", "--stat")
104
+
105
+ async def staged(self) -> str:
106
+ """已经暂存的改动列表(``--cached``)。"""
107
+ await self.require_repo()
108
+ return await self._git.out("diff", "--cached", "--name-status")
109
+
110
+ async def log(self, *, limit: int = 5) -> str:
111
+ """最近若干条提交,一行一条:短哈希 + 标题。"""
112
+ await self.require_repo()
113
+ return await self._git.out("log", f"-{max(1, limit)}", "--pretty=%h %s")
114
+
115
+ # ------------------------------------------------------------------ 提交
116
+
117
+ async def commit(self, subject: str, body: str | None = None) -> CommitResult:
118
+ """暂存全部改动并提交。
119
+
120
+ :raises GitWorkspaceError: 消息为空、不是仓库、或没有改动。
121
+ :raises GitError: git 拒绝提交(身份未配置、钩子失败等),原文在消息里。
122
+ """
123
+ message = subject.strip()
124
+ if not message:
125
+ raise GitWorkspaceError("提交消息不能为空:用 /commit <消息> 给一条")
126
+ await self.require_repo()
127
+ status = await self.status_short()
128
+ if not status.splitlines()[1:]:
129
+ raise GitWorkspaceError("没有需要提交的改动:工作树是干净的")
130
+
131
+ await self._git.out("add", "-A")
132
+ args = ["commit", "-m", message]
133
+ if body is not None and body.strip():
134
+ args.extend(["-m", body.strip()])
135
+ await self._git.out(*args)
136
+ raw = await self._git.out("log", "-1", "--pretty=%h")
137
+ return CommitResult(short_hash=raw, subject=message)
138
+
139
+ # ------------------------------------------------------------------ 分支
140
+
141
+ async def branches(self) -> tuple[str, ...]:
142
+ """本地分支名,按 git 的排序(当前分支在前)。"""
143
+ await self.require_repo()
144
+ raw = await self._git.out("branch", "--format=%(refname:short)")
145
+ return tuple(line.strip() for line in raw.splitlines() if line.strip())
146
+
147
+ async def create_branch(self, name: str) -> None:
148
+ await self._validate_branch(name)
149
+ await self.require_repo()
150
+ await self._git.out("branch", "--", name)
151
+
152
+ async def switch_branch(self, name: str) -> None:
153
+ await self._validate_branch(name)
154
+ await self.require_repo()
155
+ await self._git.out("switch", name)
156
+
157
+ async def delete_branch(self, name: str) -> None:
158
+ """只做安全删除(``-d``):没有合并的分支会失败,这是有意的。"""
159
+ await self._validate_branch(name)
160
+ await self.require_repo()
161
+ await self._git.out("branch", "-d", "--", name)
162
+
163
+ async def _validate_branch(self, name: str) -> None:
164
+ candidate = name.strip()
165
+ if not candidate:
166
+ raise GitWorkspaceError("分支名不能为空")
167
+ if any(char in candidate for char in _BAD_BRANCH_CHARS) or candidate.startswith("-"):
168
+ raise GitWorkspaceError(f"分支名不合法:{name!r}(不能含空格、~^:?*[\\ 或以 - 开头)")
169
+ result = await self._git.run("check-ref-format", "--branch", candidate)
170
+ if not result.ok:
171
+ reason = result.error_text.strip() or "git 拒绝了这个名字"
172
+ raise GitWorkspaceError(f"分支名不合法:{candidate!r}({reason})")
173
+
174
+ # ---------------------------------------------------------------- 工作树
175
+
176
+ async def worktrees(self) -> tuple[WorktreeEntry, ...]:
177
+ """全部工作树。第一项总是主工作树。"""
178
+ await self.require_repo()
179
+ raw = await self._git.out("worktree", "list", "--porcelain")
180
+ entries: list[WorktreeEntry] = []
181
+ path = ""
182
+ head = ""
183
+ branch = ""
184
+ for line in [*raw.splitlines(), ""]:
185
+ if not line.strip():
186
+ if path:
187
+ entries.append(WorktreeEntry(path=path, head=head, branch=branch))
188
+ path, head, branch = "", "", ""
189
+ continue
190
+ key, _, value = line.partition(" ")
191
+ if key == "worktree":
192
+ path = value
193
+ elif key == "HEAD":
194
+ head = value
195
+ elif key == "branch":
196
+ branch = value.removeprefix("refs/heads/")
197
+ return tuple(entries)
198
+
199
+ async def add_worktree(self, path: str, branch: str) -> None:
200
+ """在新目录里开一个工作树,分支不存在时新建。"""
201
+ await self._validate_branch(branch)
202
+ if not path.strip():
203
+ raise GitWorkspaceError("工作树路径不能为空:用 /worktree add <路径> <分支名>")
204
+ await self.require_repo()
205
+ target = os.path.abspath(os.path.join(self._cwd, path))
206
+ await self._git.out("worktree", "add", "-b", branch.strip(), target)
207
+
208
+ async def remove_worktree(self, path: str) -> None:
209
+ """删掉一个工作树。有未提交改动时 git 会拒绝,这里不加强制参数。"""
210
+ if not path.strip():
211
+ raise GitWorkspaceError("工作树路径不能为空:用 /worktree remove <路径>")
212
+ await self.require_repo()
213
+ target = os.path.abspath(os.path.join(self._cwd, path))
214
+ await self._git.out("worktree", "remove", target)
215
+
216
+
217
+ __all__ = ["CommitResult", "GitWorkspace", "GitWorkspaceError", "WorktreeEntry"]
218
+
219
+ #: ``GitError`` 也属于这里对外承诺的失败类型,调用方通常一起捕获。
220
+ GIT_FAILURES = (GitWorkspaceError, GitError)