harbor-spec 1.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- harbor_spec-1.0.2/LICENSE +21 -0
- harbor_spec-1.0.2/PKG-INFO +352 -0
- harbor_spec-1.0.2/README.md +314 -0
- harbor_spec-1.0.2/harbor/__init__.py +2 -0
- harbor_spec-1.0.2/harbor/adapters/__init__.py +2 -0
- harbor_spec-1.0.2/harbor/adapters/python/__init__.py +2 -0
- harbor_spec-1.0.2/harbor/adapters/python/parser.py +269 -0
- harbor_spec-1.0.2/harbor/cli/__init__.py +2 -0
- harbor_spec-1.0.2/harbor/cli/main.py +475 -0
- harbor_spec-1.0.2/harbor/core/__init__.py +2 -0
- harbor_spec-1.0.2/harbor/core/audit.py +163 -0
- harbor_spec-1.0.2/harbor/core/ddt.py +172 -0
- harbor_spec-1.0.2/harbor/core/diary.py +211 -0
- harbor_spec-1.0.2/harbor/core/drafting.py +242 -0
- harbor_spec-1.0.2/harbor/core/git_utils.py +74 -0
- harbor_spec-1.0.2/harbor/core/index.py +268 -0
- harbor_spec-1.0.2/harbor/core/init.py +125 -0
- harbor_spec-1.0.2/harbor/core/l2.py +170 -0
- harbor_spec-1.0.2/harbor/core/sync.py +150 -0
- harbor_spec-1.0.2/harbor/core/utils.py +158 -0
- harbor_spec-1.0.2/harbor/test_utils.py +11 -0
- harbor_spec-1.0.2/harbor/utils/__init__.py +2 -0
- harbor_spec-1.0.2/harbor/utils/formatting.py +36 -0
- harbor_spec-1.0.2/harbor_spec.egg-info/PKG-INFO +352 -0
- harbor_spec-1.0.2/harbor_spec.egg-info/SOURCES.txt +42 -0
- harbor_spec-1.0.2/harbor_spec.egg-info/dependency_links.txt +1 -0
- harbor_spec-1.0.2/harbor_spec.egg-info/entry_points.txt +2 -0
- harbor_spec-1.0.2/harbor_spec.egg-info/requires.txt +8 -0
- harbor_spec-1.0.2/harbor_spec.egg-info/top_level.txt +1 -0
- harbor_spec-1.0.2/pyproject.toml +29 -0
- harbor_spec-1.0.2/setup.cfg +4 -0
- harbor_spec-1.0.2/tests/test_adapter_basic.py +45 -0
- harbor_spec-1.0.2/tests/test_audit.py +43 -0
- harbor_spec-1.0.2/tests/test_cli_config.py +28 -0
- harbor_spec-1.0.2/tests/test_ddt_validate.py +61 -0
- harbor_spec-1.0.2/tests/test_drafting.py +71 -0
- harbor_spec-1.0.2/tests/test_drafting_json_parse.py +70 -0
- harbor_spec-1.0.2/tests/test_gitignore_prune.py +45 -0
- harbor_spec-1.0.2/tests/test_index_builder.py +99 -0
- harbor_spec-1.0.2/tests/test_index_builder_bad_syntax.py +20 -0
- harbor_spec-1.0.2/tests/test_index_progress.py +58 -0
- harbor_spec-1.0.2/tests/test_initializer.py +36 -0
- harbor_spec-1.0.2/tests/test_sync_engine.py +68 -0
- harbor_spec-1.0.2/tests/test_utils_format.py +28 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 李健
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: harbor-spec
|
|
3
|
+
Version: 1.0.2
|
|
4
|
+
Summary: Harbor-spec v1.0.2 reference implementation (Python-only)
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2025 李健
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
|
|
27
|
+
Requires-Python: >=3.9
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Requires-Dist: pyyaml
|
|
31
|
+
Requires-Dist: openai>=1.0.0
|
|
32
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
33
|
+
Requires-Dist: rich>=13.7
|
|
34
|
+
Requires-Dist: pathspec>=0.11.0
|
|
35
|
+
Provides-Extra: dev
|
|
36
|
+
Requires-Dist: pytest; extra == "dev"
|
|
37
|
+
Dynamic: license-file
|
|
38
|
+
|
|
39
|
+
<div align="center">
|
|
40
|
+
|
|
41
|
+
# ⚓ Harbor
|
|
42
|
+
### The Context Governance Engine for Vibe Coding
|
|
43
|
+
|
|
44
|
+
[](https://github.com/your-org/harbor-spec/actions)
|
|
45
|
+
[](https://www.python.org/)
|
|
46
|
+
[](LICENSE)
|
|
47
|
+
[](https://github.com/your-org/harbor-spec)
|
|
48
|
+
|
|
49
|
+
**让 AI 像代码一样被管理,让上下文像 Git 一样可追溯。**
|
|
50
|
+
|
|
51
|
+
[理念 (Philosophy)] • [架构 (Architecture)] • [快速开始 (Quick Start)] • [工作流 (Workflow)]
|
|
52
|
+
|
|
53
|
+
</div>
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 🌌 The Era of Vibe Coding
|
|
58
|
+
|
|
59
|
+
编程正在经历一场范式转移。我们正在从 "Writing Code"(逐行编写)转向 **"Vibe Coding"**(通过自然语言与 AI 协作生成)。
|
|
60
|
+
|
|
61
|
+
在这个新时代,**代码生成的边际成本趋近于零,但上下文维护的成本却在指数级上升。**
|
|
62
|
+
- AI 改了代码,Docstring 还没改?👉 **Context Drift (上下文漂移)**
|
|
63
|
+
- 测试用例还在测旧版本的逻辑?👉 **Validation Gap (验证断层)**
|
|
64
|
+
- 为什么上周我们要把这个参数改成 Optional?👉 **Memory Loss (决策遗忘)**
|
|
65
|
+
|
|
66
|
+
**Harbor** 应运而生。它不是另一个 Copilot,它是 **Copilot 的监管者**。它是一套用于治理 AI 生成代码的 **"良知" (Conscience)** 与 **"记忆" (Memory)** 系统。它是“程序员到上下文工程师”这一革命性转变的关键工具,它的目标是成为vibe coding时代的Git。
|
|
67
|
+
|
|
68
|
+
## 🛡️ Core Philosophy
|
|
69
|
+
|
|
70
|
+
Harbor 的核心设计理念基于 **L3 Contract Theory**:
|
|
71
|
+
|
|
72
|
+
1. **Code is Volatile, Contract is Immutable**: 实现代码可以由 AI 随意重写,但 L3 级 Docstring(契约)是锚点,必须由人类或高级审计确认。
|
|
73
|
+
2. **Noise is Signal**: 未经索引的代码、未同步的文档、未绑定的测试,都是系统中的“噪音”。Harbor 不会静默处理,而是将其显性化。
|
|
74
|
+
3. **Trust, but Verify**: 我们信任 AI 的编码能力,但必须通过 AST 静态分析和 LLM 语义审计来验证其产出。
|
|
75
|
+
|
|
76
|
+
## 🏗️ Architecture
|
|
77
|
+
|
|
78
|
+
Harbor 通过六大核心子系统构建了一个闭环的治理体系:
|
|
79
|
+
|
|
80
|
+
```mermaid
|
|
81
|
+
graph TD
|
|
82
|
+
Source[Source Code] -->|AST Parse| Adapter(Adapter)
|
|
83
|
+
Adapter -->|Contract Hash| Index(L3 Index / Memory)
|
|
84
|
+
|
|
85
|
+
Index -->|Compare| Sync(Sync Engine)
|
|
86
|
+
Source -->|Body Hash| Sync
|
|
87
|
+
|
|
88
|
+
Sync -->|Drift Detected| Status[CLI Status]
|
|
89
|
+
Sync -->|Diff Target| Audit(Semantic Guard)
|
|
90
|
+
|
|
91
|
+
Env[.env / LLM] --> Audit
|
|
92
|
+
Audit -->|Semantic Check| Report[Audit Report]
|
|
93
|
+
|
|
94
|
+
Tests[Test Cases] -->|DDT Binding| Validator(DDT Validator)
|
|
95
|
+
Index -->|Version Match| Validator
|
|
96
|
+
|
|
97
|
+
Index -->|Aggregation| L2(L2 Generator)
|
|
98
|
+
Validator -->|Status| L2
|
|
99
|
+
|
|
100
|
+
User[Developer] -->|Log Decision| Diary(Diary / History)
|
|
101
|
+
````
|
|
102
|
+
|
|
103
|
+
- **🧠 Index (Memory)**: 这里的 `.harbor/cache` 是大脑,记录了代码的每一次“快照”与指纹。
|
|
104
|
+
- **⚖️ Sync (Conscience)**: 实时监测 "Implementation Drift"(代码变了,但契约没变)。
|
|
105
|
+
- **🌉 DDT (Bridge)**: **D**ecorator-driven **D**ata **T**esting。将测试用例与 L3 版本强绑定,拒绝“假绿灯”。
|
|
106
|
+
- **🤖 Audit (Guard)**: 集成 DeepSeek/OpenAI,对代码进行语义级审计,揪出逻辑与文档的违背之处。
|
|
107
|
+
- **📊 L2 (Dashboard)**: 自动生成 Markdown 视图,诚实地展示模块的测试覆盖率与契约状态。
|
|
108
|
+
- **📜 Diary (History)**: 结构化的决策日志,记录每一次变革背后的 "Why"。
|
|
109
|
+
|
|
110
|
+
## ⚡ Quick Start
|
|
111
|
+
|
|
112
|
+
### 1\. Installation
|
|
113
|
+
|
|
114
|
+
Harbor 是一个纯 Python 工具,推荐在开发模式下安装:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
git clone [https://github.com/your-org/harbor-spec.git](https://github.com/your-org/harbor-spec.git)
|
|
118
|
+
cd harbor-spec
|
|
119
|
+
pip install -e .
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
可选pypi安装:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
pip install harbor-spec
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### 2\. Configuration
|
|
129
|
+
|
|
130
|
+
配置 LLM 以启用 AI 语义审计(支持 Ernie, DeepSeek, OpenAI 等兼容接口):
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
cp .env.example .env
|
|
134
|
+
# 编辑 .env 文件:
|
|
135
|
+
# HARBOR_LLM_PROVIDER=openai
|
|
136
|
+
# HARBOR_LLM_API_KEY=
|
|
137
|
+
# HARBOR_LLM_BASE_URL=https://api.openai.com/v1
|
|
138
|
+
# HARBOR_LANGUAGE=zh (可选,开启中文审计)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 3\. Initialize
|
|
142
|
+
|
|
143
|
+
使用智能初始化生成配置并避免 `scanned=0`:
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
harbor init
|
|
147
|
+
# 若已存在配置需要覆盖:
|
|
148
|
+
# harbor init --force
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
初始化完成后,构建初始索引接管当前代码库:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
harbor build-index
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### 4\. 配置管理(Config CLI)
|
|
158
|
+
|
|
159
|
+
通过命令行安全管理 `.harbor/config.yaml`,避免手动编辑出错:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
# 追加路径到 code_roots(自动去重)
|
|
163
|
+
harbor config add "backend/legacy/**"
|
|
164
|
+
|
|
165
|
+
# 查看当前配置(Rich 表格)
|
|
166
|
+
harbor config list
|
|
167
|
+
|
|
168
|
+
# 从 code_roots 移除路径
|
|
169
|
+
harbor config remove "backend/legacy/**"
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
运行 `harbor config list` 的示例输出:
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
Harbor Config
|
|
176
|
+
profile : enforce_l3
|
|
177
|
+
code_roots : harbor/**, backend/legacy/**
|
|
178
|
+
exclude_paths : .venv/**, tests/**, build/**, dist/**, docs/**
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### 5\. 进度可视化(Rich Progress)
|
|
182
|
+
|
|
183
|
+
在大型项目上构建索引支持进度条与当前文件显示,过程更透明:
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
harbor build-index --no-incremental
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
示例输出:
|
|
190
|
+
|
|
191
|
+
```
|
|
192
|
+
[完成] E:/project/harbor-spec/harbor/utils/__init__.py ━━━━━━━━━━━━━━━━━━━━━━━━━ 18/18 0:00:00
|
|
193
|
+
scanned=18 updated=18 skipped=0 items=63 cache=.harbor/cache/l3_index.json
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Tips: 避免 `scanned=0`
|
|
197
|
+
- 先运行 `harbor init` 生成 `.harbor/config.yaml`,确保 `code_roots` 指向真实代码位置(如 `src/**` 或包目录)。
|
|
198
|
+
- 使用 `--force` 重新初始化以覆盖不合适的旧配置。
|
|
199
|
+
- 如为脚本型仓库(根目录只有若干 `.py`),`harbor init` 会生成 `code_roots: ['*.py']`;若没有 `.py`,会兜底为 `['**/*.py']`。
|
|
200
|
+
|
|
201
|
+
## 🎮 Workflow: A Day with Harbor
|
|
202
|
+
|
|
203
|
+
### 1\. Check Status
|
|
204
|
+
|
|
205
|
+
开始工作前,看看代码库是否干净。
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
harbor status
|
|
209
|
+
# 输出: No changes detected. (Or list of drifts)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### 2\. Vibe Coding
|
|
213
|
+
|
|
214
|
+
使用你喜欢的 AI 助手(Cursor, Windsurf, Copilot)修改代码。
|
|
215
|
+
*假设你修改了 `harbor/utils.py` 的逻辑,但忘记更新 Docstring。*
|
|
216
|
+
|
|
217
|
+
### 3\. Detect Drift
|
|
218
|
+
|
|
219
|
+
Harbor 会发现你的代码实现了“偷跑”。
|
|
220
|
+
|
|
221
|
+
```bash
|
|
222
|
+
harbor status
|
|
223
|
+
# 输出: M harbor.utils.func (Body changed, Contract static)
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### 4\. AI Audit
|
|
227
|
+
|
|
228
|
+
让 Harbor 的 AI 审计员检查你的修改是否违背了契约。
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
harbor audit --semantic
|
|
232
|
+
# 默认输出(JSONL,一行一个结果,适合管道):
|
|
233
|
+
# {"status":"POSSIBLE_SEMANTIC_DRIFT","func_id":"harbor.utils.func","file_path":"E:/project/.../utils.py","provider":"baidu","model":"ernie-4.0-turbo-8k","reason":"代码抛出 ValueError 但 Docstring 未声明"}
|
|
234
|
+
#
|
|
235
|
+
# 纯文本单行(如需人读友好可选):
|
|
236
|
+
harbor audit --semantic --format plain
|
|
237
|
+
# {"status":"POSSIBLE_SEMANTIC_DRIFT","func_id":"harbor.utils.func","file_path":"E:/project/.../utils.py","provider":"baidu","model":"ernie-4.0-turbo-8k","reason":"代码抛出 ValueError 但 Docstring 未声明"}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
提示:
|
|
241
|
+
- 使用 `--debug` 可查看提示与原始 LLM 输出,便于故障排查。
|
|
242
|
+
- `plain` 模式自动压缩理由文本为单行,避免控制台断词或换行。
|
|
243
|
+
- `jsonl` 模式便于后续写入 Diary 或与其他工具联动。
|
|
244
|
+
|
|
245
|
+
### 5\. AI 智能日志(Smart Diary)
|
|
246
|
+
|
|
247
|
+
当你准备记录一次重要变更的“为什么”和“做了什么”,可以让 Harbor 先为你生成一份 AI 草稿,然后在终端中确认或微调:
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
harbor diary draft --visibility repo
|
|
251
|
+
# 若遇到“AI 输出不可解析为 JSON”,可使用调试模式查看原始输出:
|
|
252
|
+
harbor diary draft --visibility repo --debug
|
|
253
|
+
# 过程:
|
|
254
|
+
# [Status] Analyzing code changes...
|
|
255
|
+
# [AI] Drafting diary entry...
|
|
256
|
+
# 终端展示 Panel:
|
|
257
|
+
# Summary: ...
|
|
258
|
+
# Type: refactor
|
|
259
|
+
# Importance: normal
|
|
260
|
+
# Details:
|
|
261
|
+
# - 说明 WHY 和 WHAT(支持 Markdown)
|
|
262
|
+
#
|
|
263
|
+
# 提示: Save this entry? [Y]es / [e]dit summary / [n]o
|
|
264
|
+
# 选择 Y 写入;选择 e 可编辑 Summary 后写入;选择 n 放弃
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
前置条件:
|
|
268
|
+
- 已配置 LLM(见上文 Configuration),否则命令会友好提示配置 `HARBOR_LLM_PROVIDER` 和 `HARBOR_LLM_API_KEY`。
|
|
269
|
+
- 先运行 `harbor status` 确认存在 Drift/Modified;不要在生成草稿前运行 `harbor build-index`,否则差异会被快照消化。保存草稿后再运行 `harbor build-index`。
|
|
270
|
+
|
|
271
|
+
生成策略:
|
|
272
|
+
- 自动聚合 `Drift/Modified` 的受影响函数,提取源码片段(自动截断以避免超长上下文)。
|
|
273
|
+
- 通过 LLM 生成严格 JSON 的草稿字段:`summary/type/importance/details`。
|
|
274
|
+
- 可见性通过 `--visibility` 参数控制(默认 `repo`)。
|
|
275
|
+
|
|
276
|
+
兼容配置(ERNIE 示例):
|
|
277
|
+
- `.env` 设置:
|
|
278
|
+
- `HARBOR_LLM_PROVIDER=openai`
|
|
279
|
+
- `HARBOR_LLM_BASE_URL=<你的ERNIE OpenAI兼容端点>/v1`
|
|
280
|
+
- `HARBOR_LLM_MODEL=ernie-4.0`
|
|
281
|
+
- `HARBOR_LLM_API_KEY=<你的密钥>`
|
|
282
|
+
- `HARBOR_LANGUAGE=zh`
|
|
283
|
+
|
|
284
|
+
故障排查:
|
|
285
|
+
- 若端点不支持 `response_format=json_object`,可能返回包裹文本;使用 `--debug` 查看原始输出,或切换到支持结构化输出的模型。
|
|
286
|
+
|
|
287
|
+
### 6\. Lock & Record
|
|
288
|
+
|
|
289
|
+
修复问题后,更新索引并记录决策日志。
|
|
290
|
+
|
|
291
|
+
```bash
|
|
292
|
+
harbor build-index
|
|
293
|
+
harbor diary log --summary "Refactor utils validation logic" --type refactor --importance high
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## 🧩 Commands Cheatsheet
|
|
297
|
+
|
|
298
|
+
| Command | Description |
|
|
299
|
+
| :--- | :--- |
|
|
300
|
+
| `harbor init` | 智能探测项目结构并生成 `.harbor/config.yaml`(支持 `--force` 覆盖) |
|
|
301
|
+
| `harbor status` | 检查代码与索引的差异(Drift检测) |
|
|
302
|
+
| `harbor build-index` | 更新 L3 索引缓存 (类似 `git commit`) |
|
|
303
|
+
| `harbor config list` | 使用 Rich 表格打印当前配置(profile、code_roots、exclude_paths) |
|
|
304
|
+
| `harbor config add <path>` | 追加 `<path>` 到 `code_roots`(自动去重并写回) |
|
|
305
|
+
| `harbor config remove <path>` | 从 `code_roots` 移除 `<path>`(写回配置) |
|
|
306
|
+
| `harbor audit --semantic` | 调用 LLM 进行语义一致性检查(默认 JSONL,支持 `--format plain`) |
|
|
307
|
+
| `harbor diary draft` | AI 辅助生成决策日志草稿 |
|
|
308
|
+
| `harbor ddt validate` | 验证测试用例与代码版本的绑定关系 |
|
|
309
|
+
| `harbor gen l2 --module <module> [--write]` | 生成指定模块的 L2 README(默认预览,`--write` 写入) |
|
|
310
|
+
| `harbor diary log` | 写入结构化的决策日志 |
|
|
311
|
+
|
|
312
|
+
## 📊 生成 L2(Anchor)教程
|
|
313
|
+
|
|
314
|
+
**L2 是基于 L3 Docstring 的只读聚合视图**,用于在模块目录生成 `README.md`,展示 Public API、严格度与 DDT 绑定状态。请勿手动编辑生成的 L2 文件。
|
|
315
|
+
|
|
316
|
+
- 前置准备
|
|
317
|
+
- 运行 `harbor init` 完成初始化与 `code_roots` 配置
|
|
318
|
+
- 构建索引:`harbor build-index`(读取并更新 `.harbor/cache/l3_index.json`)
|
|
319
|
+
- 生成步骤
|
|
320
|
+
- 执行:`harbor gen l2 --module harbor/core --write`
|
|
321
|
+
- 工具会扫描 `code_roots`,基于索引为每个模块目录生成/更新 `README.md`
|
|
322
|
+
- 成品内容
|
|
323
|
+
- `Public API`:函数名、摘要、`strictness`、`DDT Status`
|
|
324
|
+
- `Internal Details`(可选):模块内的内部函数视图
|
|
325
|
+
- `Dependency (MVP)`:依赖占位视图(后续版本完善)
|
|
326
|
+
- 示例
|
|
327
|
+
```bash
|
|
328
|
+
harbor build-index
|
|
329
|
+
harbor gen l2 --module harbor/core
|
|
330
|
+
harbor gen l2 --module harbor/core --write
|
|
331
|
+
# 例如会生成:
|
|
332
|
+
# harbor/core/README.md
|
|
333
|
+
```
|
|
334
|
+
- 更新策略
|
|
335
|
+
- 当 Docstring 或实现变更后:先 `harbor build-index`,再 `harbor gen l2`
|
|
336
|
+
- L2 文件包含头部标识:`AUTO-GENERATED ... DO NOT EDIT MANUALLY`,请保持只读
|
|
337
|
+
### 🧭 命令用法速查
|
|
338
|
+
- 预览(不写文件):`harbor gen l2 --module harbor/core`
|
|
339
|
+
- 写入 README:`harbor gen l2 --module harbor/core --write`
|
|
340
|
+
- 覆盖已有 README:在写入模式下追加 `--force`
|
|
341
|
+
|
|
342
|
+
## 🤝 Contribution
|
|
343
|
+
|
|
344
|
+
Harbor 遵循 **Strict L3** 开发规范。
|
|
345
|
+
|
|
346
|
+
- 所有 Public API 必须包含完整的 Google-style Docstring。
|
|
347
|
+
- 所有新增功能必须包含 DDT 测试绑定。
|
|
348
|
+
- 提交前请运行 `harbor audit --semantic` 自测。
|
|
349
|
+
|
|
350
|
+
## 📄 License
|
|
351
|
+
|
|
352
|
+
MIT © 2025 Harbor-spec Authors.
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# ⚓ Harbor
|
|
4
|
+
### The Context Governance Engine for Vibe Coding
|
|
5
|
+
|
|
6
|
+
[](https://github.com/your-org/harbor-spec/actions)
|
|
7
|
+
[](https://www.python.org/)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
[](https://github.com/your-org/harbor-spec)
|
|
10
|
+
|
|
11
|
+
**让 AI 像代码一样被管理,让上下文像 Git 一样可追溯。**
|
|
12
|
+
|
|
13
|
+
[理念 (Philosophy)] • [架构 (Architecture)] • [快速开始 (Quick Start)] • [工作流 (Workflow)]
|
|
14
|
+
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 🌌 The Era of Vibe Coding
|
|
20
|
+
|
|
21
|
+
编程正在经历一场范式转移。我们正在从 "Writing Code"(逐行编写)转向 **"Vibe Coding"**(通过自然语言与 AI 协作生成)。
|
|
22
|
+
|
|
23
|
+
在这个新时代,**代码生成的边际成本趋近于零,但上下文维护的成本却在指数级上升。**
|
|
24
|
+
- AI 改了代码,Docstring 还没改?👉 **Context Drift (上下文漂移)**
|
|
25
|
+
- 测试用例还在测旧版本的逻辑?👉 **Validation Gap (验证断层)**
|
|
26
|
+
- 为什么上周我们要把这个参数改成 Optional?👉 **Memory Loss (决策遗忘)**
|
|
27
|
+
|
|
28
|
+
**Harbor** 应运而生。它不是另一个 Copilot,它是 **Copilot 的监管者**。它是一套用于治理 AI 生成代码的 **"良知" (Conscience)** 与 **"记忆" (Memory)** 系统。它是“程序员到上下文工程师”这一革命性转变的关键工具,它的目标是成为vibe coding时代的Git。
|
|
29
|
+
|
|
30
|
+
## 🛡️ Core Philosophy
|
|
31
|
+
|
|
32
|
+
Harbor 的核心设计理念基于 **L3 Contract Theory**:
|
|
33
|
+
|
|
34
|
+
1. **Code is Volatile, Contract is Immutable**: 实现代码可以由 AI 随意重写,但 L3 级 Docstring(契约)是锚点,必须由人类或高级审计确认。
|
|
35
|
+
2. **Noise is Signal**: 未经索引的代码、未同步的文档、未绑定的测试,都是系统中的“噪音”。Harbor 不会静默处理,而是将其显性化。
|
|
36
|
+
3. **Trust, but Verify**: 我们信任 AI 的编码能力,但必须通过 AST 静态分析和 LLM 语义审计来验证其产出。
|
|
37
|
+
|
|
38
|
+
## 🏗️ Architecture
|
|
39
|
+
|
|
40
|
+
Harbor 通过六大核心子系统构建了一个闭环的治理体系:
|
|
41
|
+
|
|
42
|
+
```mermaid
|
|
43
|
+
graph TD
|
|
44
|
+
Source[Source Code] -->|AST Parse| Adapter(Adapter)
|
|
45
|
+
Adapter -->|Contract Hash| Index(L3 Index / Memory)
|
|
46
|
+
|
|
47
|
+
Index -->|Compare| Sync(Sync Engine)
|
|
48
|
+
Source -->|Body Hash| Sync
|
|
49
|
+
|
|
50
|
+
Sync -->|Drift Detected| Status[CLI Status]
|
|
51
|
+
Sync -->|Diff Target| Audit(Semantic Guard)
|
|
52
|
+
|
|
53
|
+
Env[.env / LLM] --> Audit
|
|
54
|
+
Audit -->|Semantic Check| Report[Audit Report]
|
|
55
|
+
|
|
56
|
+
Tests[Test Cases] -->|DDT Binding| Validator(DDT Validator)
|
|
57
|
+
Index -->|Version Match| Validator
|
|
58
|
+
|
|
59
|
+
Index -->|Aggregation| L2(L2 Generator)
|
|
60
|
+
Validator -->|Status| L2
|
|
61
|
+
|
|
62
|
+
User[Developer] -->|Log Decision| Diary(Diary / History)
|
|
63
|
+
````
|
|
64
|
+
|
|
65
|
+
- **🧠 Index (Memory)**: 这里的 `.harbor/cache` 是大脑,记录了代码的每一次“快照”与指纹。
|
|
66
|
+
- **⚖️ Sync (Conscience)**: 实时监测 "Implementation Drift"(代码变了,但契约没变)。
|
|
67
|
+
- **🌉 DDT (Bridge)**: **D**ecorator-driven **D**ata **T**esting。将测试用例与 L3 版本强绑定,拒绝“假绿灯”。
|
|
68
|
+
- **🤖 Audit (Guard)**: 集成 DeepSeek/OpenAI,对代码进行语义级审计,揪出逻辑与文档的违背之处。
|
|
69
|
+
- **📊 L2 (Dashboard)**: 自动生成 Markdown 视图,诚实地展示模块的测试覆盖率与契约状态。
|
|
70
|
+
- **📜 Diary (History)**: 结构化的决策日志,记录每一次变革背后的 "Why"。
|
|
71
|
+
|
|
72
|
+
## ⚡ Quick Start
|
|
73
|
+
|
|
74
|
+
### 1\. Installation
|
|
75
|
+
|
|
76
|
+
Harbor 是一个纯 Python 工具,推荐在开发模式下安装:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
git clone [https://github.com/your-org/harbor-spec.git](https://github.com/your-org/harbor-spec.git)
|
|
80
|
+
cd harbor-spec
|
|
81
|
+
pip install -e .
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
可选pypi安装:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
pip install harbor-spec
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### 2\. Configuration
|
|
91
|
+
|
|
92
|
+
配置 LLM 以启用 AI 语义审计(支持 Ernie, DeepSeek, OpenAI 等兼容接口):
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
cp .env.example .env
|
|
96
|
+
# 编辑 .env 文件:
|
|
97
|
+
# HARBOR_LLM_PROVIDER=openai
|
|
98
|
+
# HARBOR_LLM_API_KEY=
|
|
99
|
+
# HARBOR_LLM_BASE_URL=https://api.openai.com/v1
|
|
100
|
+
# HARBOR_LANGUAGE=zh (可选,开启中文审计)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 3\. Initialize
|
|
104
|
+
|
|
105
|
+
使用智能初始化生成配置并避免 `scanned=0`:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
harbor init
|
|
109
|
+
# 若已存在配置需要覆盖:
|
|
110
|
+
# harbor init --force
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
初始化完成后,构建初始索引接管当前代码库:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
harbor build-index
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### 4\. 配置管理(Config CLI)
|
|
120
|
+
|
|
121
|
+
通过命令行安全管理 `.harbor/config.yaml`,避免手动编辑出错:
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
# 追加路径到 code_roots(自动去重)
|
|
125
|
+
harbor config add "backend/legacy/**"
|
|
126
|
+
|
|
127
|
+
# 查看当前配置(Rich 表格)
|
|
128
|
+
harbor config list
|
|
129
|
+
|
|
130
|
+
# 从 code_roots 移除路径
|
|
131
|
+
harbor config remove "backend/legacy/**"
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
运行 `harbor config list` 的示例输出:
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
Harbor Config
|
|
138
|
+
profile : enforce_l3
|
|
139
|
+
code_roots : harbor/**, backend/legacy/**
|
|
140
|
+
exclude_paths : .venv/**, tests/**, build/**, dist/**, docs/**
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### 5\. 进度可视化(Rich Progress)
|
|
144
|
+
|
|
145
|
+
在大型项目上构建索引支持进度条与当前文件显示,过程更透明:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
harbor build-index --no-incremental
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
示例输出:
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
[完成] E:/project/harbor-spec/harbor/utils/__init__.py ━━━━━━━━━━━━━━━━━━━━━━━━━ 18/18 0:00:00
|
|
155
|
+
scanned=18 updated=18 skipped=0 items=63 cache=.harbor/cache/l3_index.json
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Tips: 避免 `scanned=0`
|
|
159
|
+
- 先运行 `harbor init` 生成 `.harbor/config.yaml`,确保 `code_roots` 指向真实代码位置(如 `src/**` 或包目录)。
|
|
160
|
+
- 使用 `--force` 重新初始化以覆盖不合适的旧配置。
|
|
161
|
+
- 如为脚本型仓库(根目录只有若干 `.py`),`harbor init` 会生成 `code_roots: ['*.py']`;若没有 `.py`,会兜底为 `['**/*.py']`。
|
|
162
|
+
|
|
163
|
+
## 🎮 Workflow: A Day with Harbor
|
|
164
|
+
|
|
165
|
+
### 1\. Check Status
|
|
166
|
+
|
|
167
|
+
开始工作前,看看代码库是否干净。
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
harbor status
|
|
171
|
+
# 输出: No changes detected. (Or list of drifts)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### 2\. Vibe Coding
|
|
175
|
+
|
|
176
|
+
使用你喜欢的 AI 助手(Cursor, Windsurf, Copilot)修改代码。
|
|
177
|
+
*假设你修改了 `harbor/utils.py` 的逻辑,但忘记更新 Docstring。*
|
|
178
|
+
|
|
179
|
+
### 3\. Detect Drift
|
|
180
|
+
|
|
181
|
+
Harbor 会发现你的代码实现了“偷跑”。
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
harbor status
|
|
185
|
+
# 输出: M harbor.utils.func (Body changed, Contract static)
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### 4\. AI Audit
|
|
189
|
+
|
|
190
|
+
让 Harbor 的 AI 审计员检查你的修改是否违背了契约。
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
harbor audit --semantic
|
|
194
|
+
# 默认输出(JSONL,一行一个结果,适合管道):
|
|
195
|
+
# {"status":"POSSIBLE_SEMANTIC_DRIFT","func_id":"harbor.utils.func","file_path":"E:/project/.../utils.py","provider":"baidu","model":"ernie-4.0-turbo-8k","reason":"代码抛出 ValueError 但 Docstring 未声明"}
|
|
196
|
+
#
|
|
197
|
+
# 纯文本单行(如需人读友好可选):
|
|
198
|
+
harbor audit --semantic --format plain
|
|
199
|
+
# {"status":"POSSIBLE_SEMANTIC_DRIFT","func_id":"harbor.utils.func","file_path":"E:/project/.../utils.py","provider":"baidu","model":"ernie-4.0-turbo-8k","reason":"代码抛出 ValueError 但 Docstring 未声明"}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
提示:
|
|
203
|
+
- 使用 `--debug` 可查看提示与原始 LLM 输出,便于故障排查。
|
|
204
|
+
- `plain` 模式自动压缩理由文本为单行,避免控制台断词或换行。
|
|
205
|
+
- `jsonl` 模式便于后续写入 Diary 或与其他工具联动。
|
|
206
|
+
|
|
207
|
+
### 5\. AI 智能日志(Smart Diary)
|
|
208
|
+
|
|
209
|
+
当你准备记录一次重要变更的“为什么”和“做了什么”,可以让 Harbor 先为你生成一份 AI 草稿,然后在终端中确认或微调:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
harbor diary draft --visibility repo
|
|
213
|
+
# 若遇到“AI 输出不可解析为 JSON”,可使用调试模式查看原始输出:
|
|
214
|
+
harbor diary draft --visibility repo --debug
|
|
215
|
+
# 过程:
|
|
216
|
+
# [Status] Analyzing code changes...
|
|
217
|
+
# [AI] Drafting diary entry...
|
|
218
|
+
# 终端展示 Panel:
|
|
219
|
+
# Summary: ...
|
|
220
|
+
# Type: refactor
|
|
221
|
+
# Importance: normal
|
|
222
|
+
# Details:
|
|
223
|
+
# - 说明 WHY 和 WHAT(支持 Markdown)
|
|
224
|
+
#
|
|
225
|
+
# 提示: Save this entry? [Y]es / [e]dit summary / [n]o
|
|
226
|
+
# 选择 Y 写入;选择 e 可编辑 Summary 后写入;选择 n 放弃
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
前置条件:
|
|
230
|
+
- 已配置 LLM(见上文 Configuration),否则命令会友好提示配置 `HARBOR_LLM_PROVIDER` 和 `HARBOR_LLM_API_KEY`。
|
|
231
|
+
- 先运行 `harbor status` 确认存在 Drift/Modified;不要在生成草稿前运行 `harbor build-index`,否则差异会被快照消化。保存草稿后再运行 `harbor build-index`。
|
|
232
|
+
|
|
233
|
+
生成策略:
|
|
234
|
+
- 自动聚合 `Drift/Modified` 的受影响函数,提取源码片段(自动截断以避免超长上下文)。
|
|
235
|
+
- 通过 LLM 生成严格 JSON 的草稿字段:`summary/type/importance/details`。
|
|
236
|
+
- 可见性通过 `--visibility` 参数控制(默认 `repo`)。
|
|
237
|
+
|
|
238
|
+
兼容配置(ERNIE 示例):
|
|
239
|
+
- `.env` 设置:
|
|
240
|
+
- `HARBOR_LLM_PROVIDER=openai`
|
|
241
|
+
- `HARBOR_LLM_BASE_URL=<你的ERNIE OpenAI兼容端点>/v1`
|
|
242
|
+
- `HARBOR_LLM_MODEL=ernie-4.0`
|
|
243
|
+
- `HARBOR_LLM_API_KEY=<你的密钥>`
|
|
244
|
+
- `HARBOR_LANGUAGE=zh`
|
|
245
|
+
|
|
246
|
+
故障排查:
|
|
247
|
+
- 若端点不支持 `response_format=json_object`,可能返回包裹文本;使用 `--debug` 查看原始输出,或切换到支持结构化输出的模型。
|
|
248
|
+
|
|
249
|
+
### 6\. Lock & Record
|
|
250
|
+
|
|
251
|
+
修复问题后,更新索引并记录决策日志。
|
|
252
|
+
|
|
253
|
+
```bash
|
|
254
|
+
harbor build-index
|
|
255
|
+
harbor diary log --summary "Refactor utils validation logic" --type refactor --importance high
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## 🧩 Commands Cheatsheet
|
|
259
|
+
|
|
260
|
+
| Command | Description |
|
|
261
|
+
| :--- | :--- |
|
|
262
|
+
| `harbor init` | 智能探测项目结构并生成 `.harbor/config.yaml`(支持 `--force` 覆盖) |
|
|
263
|
+
| `harbor status` | 检查代码与索引的差异(Drift检测) |
|
|
264
|
+
| `harbor build-index` | 更新 L3 索引缓存 (类似 `git commit`) |
|
|
265
|
+
| `harbor config list` | 使用 Rich 表格打印当前配置(profile、code_roots、exclude_paths) |
|
|
266
|
+
| `harbor config add <path>` | 追加 `<path>` 到 `code_roots`(自动去重并写回) |
|
|
267
|
+
| `harbor config remove <path>` | 从 `code_roots` 移除 `<path>`(写回配置) |
|
|
268
|
+
| `harbor audit --semantic` | 调用 LLM 进行语义一致性检查(默认 JSONL,支持 `--format plain`) |
|
|
269
|
+
| `harbor diary draft` | AI 辅助生成决策日志草稿 |
|
|
270
|
+
| `harbor ddt validate` | 验证测试用例与代码版本的绑定关系 |
|
|
271
|
+
| `harbor gen l2 --module <module> [--write]` | 生成指定模块的 L2 README(默认预览,`--write` 写入) |
|
|
272
|
+
| `harbor diary log` | 写入结构化的决策日志 |
|
|
273
|
+
|
|
274
|
+
## 📊 生成 L2(Anchor)教程
|
|
275
|
+
|
|
276
|
+
**L2 是基于 L3 Docstring 的只读聚合视图**,用于在模块目录生成 `README.md`,展示 Public API、严格度与 DDT 绑定状态。请勿手动编辑生成的 L2 文件。
|
|
277
|
+
|
|
278
|
+
- 前置准备
|
|
279
|
+
- 运行 `harbor init` 完成初始化与 `code_roots` 配置
|
|
280
|
+
- 构建索引:`harbor build-index`(读取并更新 `.harbor/cache/l3_index.json`)
|
|
281
|
+
- 生成步骤
|
|
282
|
+
- 执行:`harbor gen l2 --module harbor/core --write`
|
|
283
|
+
- 工具会扫描 `code_roots`,基于索引为每个模块目录生成/更新 `README.md`
|
|
284
|
+
- 成品内容
|
|
285
|
+
- `Public API`:函数名、摘要、`strictness`、`DDT Status`
|
|
286
|
+
- `Internal Details`(可选):模块内的内部函数视图
|
|
287
|
+
- `Dependency (MVP)`:依赖占位视图(后续版本完善)
|
|
288
|
+
- 示例
|
|
289
|
+
```bash
|
|
290
|
+
harbor build-index
|
|
291
|
+
harbor gen l2 --module harbor/core
|
|
292
|
+
harbor gen l2 --module harbor/core --write
|
|
293
|
+
# 例如会生成:
|
|
294
|
+
# harbor/core/README.md
|
|
295
|
+
```
|
|
296
|
+
- 更新策略
|
|
297
|
+
- 当 Docstring 或实现变更后:先 `harbor build-index`,再 `harbor gen l2`
|
|
298
|
+
- L2 文件包含头部标识:`AUTO-GENERATED ... DO NOT EDIT MANUALLY`,请保持只读
|
|
299
|
+
### 🧭 命令用法速查
|
|
300
|
+
- 预览(不写文件):`harbor gen l2 --module harbor/core`
|
|
301
|
+
- 写入 README:`harbor gen l2 --module harbor/core --write`
|
|
302
|
+
- 覆盖已有 README:在写入模式下追加 `--force`
|
|
303
|
+
|
|
304
|
+
## 🤝 Contribution
|
|
305
|
+
|
|
306
|
+
Harbor 遵循 **Strict L3** 开发规范。
|
|
307
|
+
|
|
308
|
+
- 所有 Public API 必须包含完整的 Google-style Docstring。
|
|
309
|
+
- 所有新增功能必须包含 DDT 测试绑定。
|
|
310
|
+
- 提交前请运行 `harbor audit --semantic` 自测。
|
|
311
|
+
|
|
312
|
+
## 📄 License
|
|
313
|
+
|
|
314
|
+
MIT © 2025 Harbor-spec Authors.
|