114 lines
3.3 KiB
Markdown
Executable File
114 lines
3.3 KiB
Markdown
Executable File
# Go 代码修改技术要点
|
||
|
||
> 来源:2026-06-09 修复 consolidation cluster_only bug 实战
|
||
|
||
---
|
||
|
||
## 1. Go 缩进规范(tabs)
|
||
|
||
```
|
||
switch expr {
|
||
case "a": // 4 tabs
|
||
foo() // 5 tabs
|
||
case "b", "c": // 4 tabs
|
||
bar() // 5 tabs
|
||
baz() // 5 tabs
|
||
if cond { // 6 tabs
|
||
qux() // 7 tabs
|
||
}
|
||
default:
|
||
quux() // 5 tabs
|
||
}
|
||
```
|
||
|
||
- **switch 语句本身**:T5(比包裹它的 func/block 多 4 层)
|
||
- **case 标签**:T4(比 switch 多 1 层)
|
||
- **case 第一条语句**:T5(比 case 多 1 层)
|
||
- **case 嵌套 block**:每层 +1 tab
|
||
|
||
验证实际缩进:`python3 -c "with open('file.go','rb') as f: [print(i+1, len(l)-len(l.lstrip()), l.rstrip()) for i,l in enumerate(f) if b'case' in l or b'switch' in l]"`
|
||
|
||
---
|
||
|
||
## 2. patch 工具失效时的字节级修复
|
||
|
||
当 patch 工具因 tab/space 缩进不匹配而失败时,用 Python 字节级读写:
|
||
|
||
```python
|
||
python3 -c "
|
||
with open('/path/to/server.go','rb') as f:
|
||
data = f.read()
|
||
|
||
# 替换:6 tabs -> 5 tabs
|
||
old = b'\x09\x09\x09\x09\x09\x09graphStore.Prune(0.15)' # 6 tabs
|
||
new = b'\x09\x09\x09\x09\x09graphStore.Prune(0.15)' # 5 tabs
|
||
|
||
count = data.count(old)
|
||
print(f'Found: {count}')
|
||
if count:
|
||
data = data.replace(old, new, 1)
|
||
with open('/path/to/server.go','wb') as f:
|
||
f.write(data)
|
||
print('Fixed!')
|
||
"
|
||
```
|
||
|
||
**关键点:**
|
||
- 用 `\x09` 表示 tab,不要用文字 `\t`(可能有转义问题)
|
||
- 用 `rb` 模式读取,保留原始字节
|
||
- 替换后立即 `go build ./...` 验证
|
||
|
||
---
|
||
|
||
## 3. Binary 部署验证
|
||
|
||
```bash
|
||
# 查看真实 mtime(stat 比 find 更可靠)
|
||
stat --format='%y' /home/muc/.local/bin/zhiyid
|
||
|
||
# 对比 git commit 时间
|
||
git -C /home/muc/projects/memoryweave log -1 --format="%ci" HEAD
|
||
|
||
# restart 并验证
|
||
systemctl --user restart zhiyid && sleep 2 && systemctl --user status zhiyid
|
||
```
|
||
|
||
⚠️ `find -printf "%T+"` 对普通文件返回正确时间,但对某些文件(如 device node)可能返回 Unix epoch 0。
|
||
|
||
---
|
||
|
||
## 4. 织忆 Consolidation Pipeline 两种模式
|
||
|
||
位于 `go/internal/api/routes/consolidation_pipe.go`:
|
||
|
||
| 模式 | 调用 | 包含步骤 |
|
||
|------|------|---------|
|
||
| `cluster_only` | `consolPipe.Run()` | Rust sidecar(聚类)+ runGraphMaintenance(PageRank) |
|
||
| `full` | `consolPipe.RunWithMode("full")` | Rust sidecar(聚类+decay+quality)+ runGraphMaintenance(Prune+PageRank) |
|
||
|
||
**调度规则(server.go):**
|
||
- `t_distill` → `Run()`(cluster_only,高频 60s)
|
||
- `t_consolidation` / `t_backtrack` → `RunWithMode("full")`(full,cooldown=48h)
|
||
- `t_prune` → 单独 `graphStore.Prune(0.15)` 调用(Go 层,T5 缩进)
|
||
- `t_decay` → 单独 decay 逻辑(从 ldb.GetCandidatesForForgetting 读取)
|
||
|
||
**常见 bug:** `consolPipe.Run()` 默认 cluster_only,consolidation 触发器实际跑了快速聚类,跳过了 decay/quality 步骤。修复:ticker switch 中 `case "consolidation"` 改为 `RunWithMode("full")`。
|
||
|
||
---
|
||
|
||
## 5. journalctl 调试技巧
|
||
|
||
```bash
|
||
# 只看某 PID 的日志
|
||
journalctl --user -u zhiyid -n 100 | grep "$(pgrep -n zhiyid)"
|
||
|
||
# 按 trigger 过滤
|
||
journalctl --user -u zhiyid --no-pager | grep -E "consolidation|Rust sidecar|prune|Decay|Quality"
|
||
|
||
# 看服务启动日志
|
||
journalctl --user -u zhiyid -n 20 --no-pager | grep -v "971305"
|
||
```
|
||
|
||
---
|
||
|
||
*记录日期:2026-06-09* |