Sgai 深度分析

channel discord model MiniMax-M2.5 date 2026-03-09 msgs 29 / 157

Y
yelo08:47

我有没有给你发过这个

https://github.com/sandgardenhq/sgai

L
lambda61008:47

The user is asking if I've been sent this GitHub link before. Let me search through my memory and recent sessions to check.

Looking at the context:

I should check my session history or memory to see if this has been discussed before. Let me search through recent session files or just check my memory files.

Actually, I don't have a direct way to search previous sessions easily. I could use the clawsess tool mentioned in TOOLS.md, but that might be overkill. Let me just be honest - I don't have an easy way to search my full conversation history, and I should tell the user that I don't have a clear record of previous discussions.

Wait, I should try to search. Let me use clawsess to search for this.

L
lambda61008:47

Command:

clawsess search sgai 2>/dev/null || echo "clawsess not found or no results"

🔍 搜索 'sgai',找到 20 条结果:

`__REDACTED__`
L
lambda61008:47

Yes! I found references to sgai in the search results. There's a session from 2026-02-25 where I did a security audit on sgai (sandgardenhq/sgai). The search results show I analyzed it for prompt injection attacks and looked at the config.go file.

So yes, Yelo has sent me this before - we did a security audit on it back in February 2026.

L
lambda61008:47

有!2026-02-25 你给我发过这个,我 还做了安全审计报告,分析了 prompt 注入攻击风险和 config.go 里的项目配置。

需要我调出那个报告还是你有新的需求?

Y
yelo08:47

你帮我深入看看这个项目是怎么设计和使用的

L
lambda61008:47

Yelo wants me to deep dive into the sgai project. Let me first check if I have the project locally, and if not, clone it. Then I'll use the deepwiki skill to analyze it.

L
lambda61008:47

Command:

ls -la ~/projects/ | grep -i sgai || echo "not found"

not found
L
lambda61008:47

Need to clone it first. Let me clone the repo.

L
lambda61008:47

Command:

cd ~/projects && git clone --depth 1 https://github.com/sandgardenhq/sgai.git

Cloning into 'sgai'...
L
lambda61008:47

Now I need to use the deepwiki skill to analyze this project. Let me read the skill first.

L
lambda61008:47

File: ~/projects/public-skills/skills/deepwiki/SKILL.md


`__REDACTED__`

L
lambda61008:48

Now I understand the deepwiki skill. I need to use it to analyze the sgai project. Let me first explore the project structure to understand what it is, then use deepwiki to create comprehensive documentation.

Actually, looking at the skill, it seems I should just describe what I want ("Analyze this codebase and generate architecture documentation") and it will work. But since this is OpenClaw, I might need to use the commands or just do it manually.

Let me first explore the project to understand it, then provide a comprehensive analysis to Yelo.

L
lambda61008:48

Command:

cd ~/projects/sgai && find . -type f -name "*.go" | head -30 && echo "---" && ls -la

`__REDACTED__`
L
lambda61008:48

This is a Go project. Let me read the README and key files to understand the architecture and design. Then I'll provide a comprehensive analysis to Yelo.

L
lambda61008:48

File: ~/projects/sgai/README.md


`__REDACTED__`

L
lambda61008:48

This is really interesting. Sgai is a goal-driven AI software factory - it's like a multi-agent development environment. Let me read more files to understand the architecture better.

L
lambda61008:48

Command:

cd ~/projects/sgai && ls -la pkg/ && echo "---" && ls -la cmd/sgai/ | head -30

`__REDACTED__`
L
lambda61008:48

Let me read more core files to understand the architecture - particularly the DAG (Directed Acyclic Graph) system, service composition, and session management.

L
lambda61008:48

File: ~/projects/sgai/cmd/sgai/dag.go


`__REDACTED__`
L
lambda61008:48

Let me continue reading the DAG file and also look at the service composition to understand the multi-agent workflow.

L
lambda61008:48

File: ~/projects/sgai/cmd/sgai/service_compose.go


`__REDACTED__`

L
lambda61008:48

Now I have a good understanding. Let me also check the config to see how the agents are defined, and then look at the session management.

L
lambda61008:48

File: ~/projects/sgai/sgai.example.json


`__REDACTED__`

L
lambda61008:48

Let me also look at the session and state management to understand how the agents run.

L
lambda61008:48

Command:

cd ~/projects/sgai && grep -l "session" cmd/sgai/*.go | head -5

`__REDACTED__`
L
lambda61008:48

Let me read the AGENTS.md file to understand the different agents.

L
lambda61008:48

File: ~/projects/sgai/AGENTS.md


`__REDACTED__`

L
lambda61008:48

Now I have a comprehensive understanding. Let me also quickly check the main.go for the entry point and overall architecture.

L
lambda61008:48

Command:

cd ~/projects/sgai && head -100 cmd/sgai/main.go

`__REDACTED__`
L
lambda61008:49

Now I have enough understanding. Let me summarize for Yelo about the sgai project - its design and usage.

L
lambda61008:49

现在我了解了这个项目。给你整理一下:


🏭 Sgai (pronounced "Sky") — Goal-Driven AI 软件工厂

核心理念:不是逐步提示 AI,而是定义目标→ AI 自动规划、执行、验证。

🎯 使用方式

  1. 创建 Goal — 用内置 wizard 或写 GOAL.md,描述期望结果而非实现步骤
  2. Agent 规划 — Sgai 把目标拆成可视化的 DAG 工作流图
  3. 审批+监控 — Agent 会提问澄清,你批准后自主执行
  4. 完成验证 — 测试/ lint 通过才算 done

🧩 多 Agent 架构

默认工作流:

"coordinator" → "general-purpose"

支持自定义 flow:

---
flow: |
  "backend-developer" -> "code-reviewer"
completionGateScript: make test
interactive: yes
---

Build a REST API

Agent 类型

  • Coordinator — 协调者,负责人机交互
  • general-purpose — 通用开发者
  • code-reviewer — 代码审查
  • safety-analyst — 安全分析
  • designer — UI/UX 设计

支持 Agent Alias — 用不同模型跑同一角色(如 backend-go-developer-lite 用 Haiku 省成本)

🔧 技术栈

  • Go 后端 + React/TypeScript 前端 (bun)
  • opencode — 底层 AI 引擎
  • jj — 版本控制(推荐)
  • DAG — 有向无环图定义工作流
  • MCP + HTTP API — 供外部 AI 集成

🔌 集成方式

  1. MCPlocalhost:8080/mcp/external,35+ 工具
  2. HTTP API/api/v1/state 轮询循环

💡 亮点

  • 可视化 — 实时看到工作流进度
  • 多角色 — Developer + Reviewer + Safety Analyst 协作
  • 审批制 — Agent 提问后才继续
  • 技能积累 — 从完成的任务中提取可复用技能
  • 本地运行 — 代码不离开机器

要我深入分析某个具体模块(比如 DAG 编排、Session 管理、或者 MCP 接口)吗?

Y
yelo09:55

深入分析

L
lambda61009:55

Yelo wants me to do a deep analysis. Let me use the deepwiki skill to do a comprehensive analysis of this codebase.

L
lambda61009:55

Command:

cd ~/projects/sgai && deepwiki scan . --output ~/notes/sgai-analysis.md 2>&1 || echo "deepwiki not available, will do manual"

zsh:1: command not found: deepwiki
deepwiki not available, will do manual
L
lambda61009:55

deepwiki isn't installed. Let me do a manual deep analysis by reading more key files. I'll focus on the core modules: DAG, Session, MCP, and Compose.

L
lambda61009:55

File: ~/projects/sgai/cmd/sgai/dag.go


`__REDACTED__`

L
lambda61009:55

Now let me look at the session management and MCP components to complete the picture.

L
lambda61009:55

Command:

cd ~/projects/sgai && grep -n "type.*Session" cmd/sgai/*.go | head -20

`__REDACTED__`
L
lambda61009:55

Let me read the service_session.go file to understand how sessions work.

L
lambda61009:55

File: ~/projects/sgai/cmd/sgai/service_session.go


`__REDACTED__`

L
lambda61009:56

Now I have a good picture. Let me also look at the pkg/state to understand the state management.

L
lambda61009:56

Command:

cd ~/projects/sgai && ls -la pkg/state/ && head -100 pkg/state/state.go

`__REDACTED__`
L
lambda61009:56

Let me continue reading the state.go to see the Workflow struct.

L
lambda61009:56

Command:

cd ~/projects/sgai && grep -n "type Workflow struct" pkg/state/state.go -A 50

`__REDACTED__`
L
lambda61009:56

Now I have a good picture. Let me also look at the MCP interface to understand how it works externally.

L
lambda61009:56

File: ~/projects/sgai/cmd/sgai/mcp_external.go


`__REDACTED__`
L
lambda61009:56

Let me also check the session tools in the MCP external.

L
lambda61009:56

Command:

cd ~/projects/sgai && grep -n "registerSessionTools\|start_session\|stop_session\|respond_to_question\|wait_for_question" cmd/sgai/mcp_external.go | head -20

`__REDACTED__`
L
lambda61009:56

Now I have a comprehensive understanding of Sgai. Let me compile this into a detailed analysis and save it to notes.

L
lambda61009:56

Command:

mkdir -p ~/notes

(no output)
L
lambda61009:57

Now I'll compile a comprehensive analysis document.

L
lambda61009:57

File: ~/notes/sgai-deep-analysis.md

Content (6912 chars):

# Sgai 深度分析报告

**来源**: https://github.com/sandgardenhq/sgai
**日期**: 2026-03-09
**标签**: AI-agent, multi-agent, workflow, golang, opencode

---

## 1. 系统架构总览

```
┌─────────────────────────────────────────────────────────────┐
│                        Sgai Server                          │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────┐ │
│  │   Web UI    │  │  MCP Server │  │   HTTP API          │ │
│  │  (React)    │  │ (/mcp/ext)  │  │   (/api/v1/*)       │ │
│  └─────────────┘  └─────────────┘  └─────────────────────┘ │
│                           │                                   │
│  ┌─────────────────────────────────────────────────────────┐ │
│  │              Workspace Manager                          │ │
│  │  - scanWorkspaceGroups()                                │ │
│  │  - workspaceCoordinator()                                │ │
│  │  - Session 生命周期管理                                   │ │
│  └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
                           │
        ┌──────────────────┼──────────────────┐
        ▼                  ▼                  ▼
   ┌─────────┐       ┌─────────┐       ┌─────────┐
   │Workspace│       │Workspace│       │Workspace│
   │    A    │       │    B    │       │    C    │
   │.sgai/   │       │.sgai/   │       │.sgai/   │
   │state.json      │state.json      │state.json
   └─────────┘       └─────────┘       └─────────┘
```

---

## 2. 核心概念

### 2.1 Workspace(工作空间)

- 每个 Git 仓库是一个 Workspace
- 状态存储在 `.sgai/state.json`
- 支持两种模式:
  - **Repository Mode**: 单 jj workspace
  - **Forked Mode**: 多个 jj workspace(root + children)

### 2.2 GOAL.md

定义目标的配置文件,YAML frontmatter + Markdown:

```yaml
---
flow: |
  "coordinator" -> "backend-developer"
  "backend-developer" -> "code-reviewer"
completionGateScript: make test
interactive: yes
---

# Build a REST API

实现用户注册和登录的 REST API...
```

**关键字段**:
- `flow`: DOT 语言定义 DAG(有向无环图)
- `completionGateScript`: 验证完成的脚本
- `interactive`: 是否允许人机交互
- `alias`: Agent 别名(复用其他 Agent 配置)
- `models`: 指定 Agent 使用的模型

### 2.3 DAG 工作流引擎

**位置gai/dag**: `cmd/s.go`

```go
type dagNode struct {
    Name         string
    Predecessors []string  // 上游节点
    Successors   []string  // 下游节点
}
```

**自动注入的边**:
1. `injectCoordinatorEdges()` - 确保 coordinator 是唯一入口
2. `injectProjectCriticCouncilEdge()` - 添加项目审查委员会
3. `injectRetrospectiveEdge()` - 添加回顾节点

**节点类型**:
- `coordinator` - 协调者,负责人机交互
- `general-purpose` - 通用开发者
- `code-reviewer` - 代码审查
- `safety-analyst` - 安全分析
- `designer` - UI/UX 设计
- `project-critic-council` - 项目审查委员会
- `retrospective` - 回顾总结

---

## 3. Session 机制

### 3.1 状态定义 (`pkg/state/state.go`)

```go
type Workflow struct {
    Status              string               // working, agent-done, complete, waiting-for-human
    Task                string               // 当前任务
    HumanMessage        string               // 给人类的消息
    MultiChoiceQuestion *MultiChoiceQuestion // 多选问题
    Messages            []Message            // Agent 间消息
    VisitCounts         map[string]int       // 各 Agent 访问次数
    CurrentAgent        string               // 当前执行的 Agent
    InteractionMode     string               // self-drive, brainstorming, building, retrospective, continuous
    SessionID           string               // 会话 ID
    Cost                SessionCost          // token 消耗
}
```

### 3.2 交互模式

| Mode | 描述 | 允许的工具 |
|------|------|-----------|
| `self-drive` | 全自动执行 | 无 |
| `brainstorming` | 头脑风暴模式 | ask_user_question |
| `building` | 构建模式,自动化执行 | 无 |
| `retrospective` | 回顾模式 | ask_user_question |
| `continuous` | 持续模式,执行长提示 | 无 |

### 3.3 Session 生命周期

```
startSession()
    │
    ▼
┌─────────────────┐
│ 检查 Workspace  │
│ (非 root)       │
└────────┬────────┘
         │
         ▼
┌─────────────────────────────────┐
│ 确定 InteractionMode             │
│ - continuous → ModeContinuous  │
│ - auto=true → ModeSelfDrive     │
│ - else → ModeBrainstorming       │
└────────┬────────────────────────┘
         │
         ▼
┌─────────────────────────────────┐
│ 启动 Workflow Runner             │
│ runWorkflow(ctx, dir, mcpURL,   │
│             logWriter, coord)    │
└─────────────────────────────────┘
```

---

## 4. MCP 外部接口

**端点**: `http://localhost:8080/mcp/external`

### 核心工具(35+)

| 工具 | 功能 |
|------|------|
| `list_workspaces` | 列出所有 Workspace |
| `get_workspace_state` | 获取 Workspace 状态 |
| `start_session` | 启动 Agent Session |
| `stop_session` | 停止 Session |
| `respond_to_question` | 回答 Agent 问题 |
| `wait_for_question` | 阻塞直到需要人类输入 |
| `steer_session` | 引导 Session 方向 |
| `create_workspace` | 创建新 Workspace |
| `list_skills` | 列出可用技能 |

### HTTP API 轮询模式

```
LOOP:
  1. PROBE  → GET /api/v1/state
  2. CHECK  → pendingQuestion != null?
  3. ACT    → start/steer/respond
  4. WAIT   → sleep → poll again
```

---

## 5. Agent 通信机制

### 5.1 Message Bus

Agent 通过消息传递协作:

```go
type Message struct {
    ID        int
    FromAgent string
    ToAgent   string
    Body      string
    Read      bool
}
```

### 5.2 消息模板

`composeFlowTemplate()` 为不同 Agent 生成不同的提示模板:

- **Coordinator**: 特殊的人机交互指令
- **Other Agents**: 非协调者的通用指令

关键指令:
- `sgai_send_message()` - 发送消息给其他 Agent
- `sgai_check_inbox()` - 检查收件箱
- `sgai_find_snippets()` - 查找代码片段

---

## 6. 多模型支持

### 6.1 Agent Alias

```yaml
---
alias:
  "backend-go-developer-lite": "backend-go-developer"
models:
  "backend-go-developer-lite": "anthropic/claude-haiku-4-5"
---
```

别名 Agent 继承基础 Agent 的:
- Prompt
- Tools
- Snippets

但使用自己的模型配置。

### 6.2 Multi-Model Context

```go
func buildMultiModelSection(currentModel string, models map[string]any, currentAgent string) string
```

- 显示当前模型身份
- 列出同组其他模型
- 提供 `sgai_send_message()` 给同组模型发消息

---

## 7. 完成门控 (Completion Gate)

```yaml
completionGateScript: make test
```

- Agent 完成工作后必须运行验证脚本
- 测试通过才标记为 `complete`
- 支持:make test, npm test, cargo test 等

---

## 8. 技能系统 (Skills)

从完成的任务中提取可复用技能:
- 存储在 `.sgai/skills/`
- 代码片段自动收集
- 可供后续 Agent 使用

---

## 9. 关键代码文件

| 文件 | 功能 |
|------|------|
| `cmd/sgai/main.go` | 入口,CLI + Server |
| `cmd/sgai/dag.go` | DAG 解析和编排 |
| `cmd/sgai/service_session.go` | Session 生命周期 |
| `cmd/sgai/service_compose.go` | GOAL.md 构建 |
| `cmd/sgai/mcp_external.go` | MCP 接口 |
| `pkg/state/state.go` | 状态定义 |
| `pkg/state/coordinator.go` | 状态协调器 |

---

## 10. 与 OpenCode 的关系

- **底层引擎**: Sgai 使用 `opencode` 作为 AI 引擎
- **opencode** 负责:
  - 模型调用
  - Tool 执行
  - Prompt 构建
- **Sgai** 负责:
  - Goal 解析
  - Agent 编排
  - 人机交互
  - UI 展示

---

## 11. 设计亮点

1. **可视化工作流**: DOT → 图表,用户清楚看到任务流向
2. **审批制执行**: Work Gate 设计,需人类批准才开始构建
3. **自动循环检测**: 防止死循环
4. **Coordinator 强制入口**: 确保人机交互始终发生
5. **状态持久化**: JSON 文件存储,支持重启恢复
6. **MCP + HTTP 双接口**: 灵活集成外部 AI
7. **多模型协作**: 同一角色可用不同成本的模型
8. **jj 集成**: 原生支持现代 VCS

---

## 12. 潜在改进点

1. **并行执行**: 当前 DAG 串行执行,可考虑并行
2. **状态回滚**: 无良好的撤销机制
3. **分布式**: 仅本地运行,无远程集群
4. **监控告警**: 缺少指标和告警
5. **插件系统**: Agent 类型硬编码,不够灵活


Successfully wrote 6912 bytes to ~/notes/sgai-deep-analysis.md
L
lambda61009:57

Done! Let me give Yelo a summary.

L
lambda61009:57

完成!已生成深度分析报告:~/notes/sgai-deep-analysis.md

核心发现

1. 架构

  • Go 后端 + React 前端
  • 底层用 opencode 做 AI 引擎
  • 状态存储在 .sgai/state.json

2. 工作流

  • GOAL.md 定义目标 + DOT 语言描述 DAG
  • 自动注入 coordinator 作为唯一入口
  • 支持 Work Gate(审批后开始构建)

3. Session 机制

  • 5 种交互模式:self-drive / brainstorming / building / retrospective / continuous
  • MCP 工具 35+,支持外部 AI 驱动
  • HTTP API 轮询循环模式

4. 多模型

  • Agent Alias 复用配置 + 不同模型
  • 支持同一角色的多模型协作

5. 亮点

  • 可视化工作流图
  • 审批制执行
  • 状态持久化
  • jj 原生集成

要我深入某个具体模块吗?

Y
yelo09:58

讲讲 Session 机制

L
lambda61009:58

Yelo wants me to explain the Session mechanism in Sgai in more detail. I already have the service_session.go content and some state definitions. Let me read more to give a comprehensive explanation.

L
lambda61009:58

Command:

cd ~/projects/sgai && grep -n "runWorkflow\|func.*workflow.*runner\|type.*Runner" cmd/sgai/*.go | head -20

`__REDACTED__`
L
lambda61009:58

Let me read the workflow_runner.go to understand the execution loop.

L
lambda61009:58

File: ~/projects/sgai/cmd/sgai/workflow_runner.go


`__REDACTED__`
L
lambda61009:58

Now let me look at more of the workflow runner and see how the agent execution works with opencode.

L
lambda61009:58

Command:

cd ~/projects/sgai && grep -n "opencode\|executeAgent\|multiModelConfig" cmd/sgai/workflow_runner.go | head -20

`__REDACTED__`
L
lambda61009:58

Let me read more of the executeAgent function to understand how it calls opencode.

L
lambda61009:58

File: ~/projects/sgai/cmd/sgai/workflow_runner.go


`__REDACTED__`
L
lambda61009:58

Now I understand more about the session mechanism. Let me also look at the multiModelAgent to understand how it runs the agent with opencode.

L
lambda61009:58

Command:

cd ~/projects/sgai && grep -n "runMultiModelAgent\|func.*run.*Agent" cmd/sgai/*.go | head -10

`__REDACTED__`
L
lambda61009:58

Let me read the main.go to see how it calls opencode.

L
lambda61009:58

File: ~/projects/sgai/cmd/sgai/main.go


`__REDACTED__`
L
lambda61009:58

Now I have a good understanding of the Session mechanism. Let me also look at how it runs with opencode.

L
lambda61009:58

Command:

cd ~/projects/sgai && grep -n "opencode\|exec.Command" cmd/sgai/main.go | head -20

`__REDACTED__`
L
lambda61009:58

Now I have a comprehensive understanding of the Session mechanism. Let me summarize it for Yelo.

L
lambda61009:58

Command:

cd ~/projects/sgai && grep -n "buildWorkflowRunner" cmd/sgai/*.go

`__REDACTED__`
L
lambda61009:58

Let me read the buildWorkflowRunner to understand how the runner is initialized.

L
lambda61009:58

File: ~/projects/sgai/cmd/sgai/workflow_runner.go


`__REDACTED__`
L
lambda61009:59

Now I have a comprehensive understanding of the Session mechanism. Let me present it to Yelo in a clear way.

L
lambda61009:59

Sgai Session 机制详解

1. 启动流程

startSessionService(workspacePath, auto)


1. 检查 workspace 类型(root 不能启动)


2. 根据条件确定 InteractionMode
    │  continuousPrompt != "" → ModeContinuous
    │  auto=true → ModeSelfDrive
    │  否则 → ModeBrainstorming


3. 启动 workflowRunner

2. WorkflowRunner 核心循环

func (r *workflowRunner) run(ctx context.Context) {
    for {
        currentAgent := r.resolveCurrentAgent()
        result := r.runAgent(ctx, currentAgent)

        switch result {
        case resultInterrupt: return      // 中断
        case resultComplete: return       // 完成
        case resultContinue: continue     // 继续下一个 Agent
        }
    }
}

3. 单个 Agent 执行

runAgent(currentAgent)

    ├── prepareAgent()
    │   ├── 切换 Agent 时清空 Todos
    │   ├── 应用 overlay 文件夹
    │   ├── 更新 VisitCounts
    │   └── 保存状态到 .sgai/state.json

    ├── executeAgent()
    │   └── runMultiModelAgent() 或 runSingleModelIteration()
    │       └── exec.Command("opencode", args...)

    ├── 检查状态
    │   ├── Status == Complete → 返回 resultComplete
    │   └── 否则 → resolveNextAgent()

    └── 返回 resultContinue

4. Agent 切换逻辑

func (r *workflowRunner) resolveNextAgent(currentAgent string) string {
    // 1. 检查是否有待处理消息
    pendingAgent := findFirstPendingMessageAgent(r.wfState)
    if pendingAgent != "" {
        return pendingAgent
    }

    // 2. 末端节点 → 返回 coordinator
    if r.flowDag.isTerminal(currentAgent) {
        return "coordinator"
    }

    // 3. 从 DAG 获取下一个
    return determineNextAgent(r.flowDag, currentAgent)
}

5. 与 opencode 的交互

// cmd/sgai/main.go:536
cmd := exec.CommandContext(agentCtx, "opencode", agentArgs...)
  • Sgai 本身不直接调用 LLM
  • 通过 opencode CLI 执行 Agent
  • opencode 负责:模型调用、Tool 执行、Prompt 构建

6. 状态持久化

位置: .sgai/state.json

{
  "status": "working",
  "task": "Build a REST API",
  "currentAgent": "backend-developer",
  "visitCounts": {
    "coordinator": 2,
    "backend-developer": 1,
    "code-reviewer": 0
  },
  "interactionMode": "building",
  "messages": [...],
  "humanMessage": "",
  "multiChoiceQuestion": null
}

读写通过 Coordinator:

coord := state.NewCoordinator(path)
// 读取
wfState := coord.State()
// 写入
coord.UpdateState(func(wf *state.Workflow) {
    wf.Status = state.StatusComplete
})

7. Continuous 模式

特殊模式,用于长时间运行:

func (r *workflowRunner) runContinuous(ctx context.Context, continuousPrompt string) {
    for {
        // 1. 运行正常 workflow
        runWorkflow(ctx, r.dir, r.mcpURL, r.logWriter, r.coord)

        // 2. 运行 continuous prompt
        runContinuousModePrompt(ctx, r.dir, continuousPrompt, ...)

        // 3. 等待触发
        trigger := watchForTrigger(ctx, r.dir, r.coord, checksum, ...)

        // 4. 处理触发,重新开始循环
        r.handleTrigger(trigger, goalPath)
    }
}

8. 关键状态流转

┌────────────────┐     ┌───────────────┐     ┌────────────────┐
│   working     │────▶│waiting-for-   │────▶│    working     │
│                │     │   human       │     │                │
└────────────────┘     └───────────────┘     └────────────────┘
      ▲                                                  │
      │                                                  ▼
      │                   ┌───────────────┐     ┌────────────────┐
      └───────────────────┤    complete   │◀────│  agent-done    │
                          └───────────────┘     └────────────────┘

核心要点

  1. opencode 是执行引擎 — Sgai 做编排,opencode 做 AI
  2. 状态全在 .sgai/state.json — 可重启恢复
  3. Coordinator 模式 — 强制所有流程经过人机交互节点
  4. Message Bus — Agent 间通过消息通信,不是直接调用
  5. VisitCounts — 防止单个 Agent 无限循环

要我深入某个具体环节吗?

Y
yelo10:03

我比较好奇 5 种交互模式:「self-drive / brainstorming / building / retrospective / continuous」的设计是为什么解决什么问题。「支持外部 AI 驱动」是怎么支持的

L
lambda61010:03

Yelo wants to understand:

  1. Why these 5 interaction modes exist and what problems they solve
  2. How external AI driving (MCP/HTTP) works

Let me find more details about each mode's purpose.

L
lambda61010:03

Command:

cd ~/projects/sgai && grep -n "ModeSelfDrive\|ModeBrainstorming\|ModeBuilding\|ModeRetrospective\|ModeContinuous" cmd/sgai/*.go | head -30

`__REDACTED__`
L
lambda61010:03

Let me check the prompt_registry.go to see how modes affect behavior.

L
lambda61010:03

File: ~/projects/sgai/cmd/sgai/prompt_registry.go


`__REDACTED__`

L
lambda61010:03

Let me find the actual content of these mode sections.

L
lambda61010:03

Command:

cd ~/projects/sgai && grep -n "flowSectionSelfDriveMode\|flowSectionBrainstormingMode\|flowSectionBuildingMode\|flowSectionRetrospectiveMode\|flowSectionContinuousMode" cmd/sgai/*.go | head -20

`__REDACTED__`
L
lambda61010:03

Now let me get the full content of these mode sections.

L
lambda61010:03

File: ~/projects/sgai/cmd/sgai/prompt_registry.go


`__REDACTED__`
L
lambda61010:03

Now I have a clear understanding of the modes. Let me also check the external AI driving mechanism (MCP).

L
lambda61010:03

Command:

cd ~/projects/sgai && grep -n "wait_for_question\|respond_to_question" cmd/sgai/mcp_external.go | head -10

`__REDACTED__`
L
lambda61010:03

Let me read the wait_for_question and respond_to_question implementations.

L
lambda61010:03

Command:

cd ~/projects/sgai && sed -n '747,800p' cmd/sgai/mcp_external.go

`__REDACTED__`
L
lambda61010:03

Now I understand how external AI driving works. Let me summarize for Yelo.

L
lambda61010:03

5 种交互模式设计意图

模式 解决的问题 关键行为
brainstorming 目标不明确,需要澄清 强制提问,不准跳过 Work Gate
building 目标已确认,开始干活 跳过 Brainstorming + Work Gate,直接开发
self-drive 全自动执行,无需人类 禁止所有 ask_user_* 工具
retrospective 完成后复盘 重新启用人机交互,复盘后完成
continuous CI/CD 持续运行 同 self-drive,但永不复盘,循环等待触发

模式流转

        ┌─────────────────┐
        │ brainstorming  │ ← 默认模式,强制提问
        └────────┬────────┘
                 │ 人类批准 Work Gate

        ┌─────────────────┐
        │    building    │ ← 执行模式,跳过所有审批
        └────────┬────────┘
                 │ 任务完成

        ┌─────────────────┐
        │ retrospective  │ ← 复盘模式,重新启用提问
        └────────┬────────┘
                 │ 复盘完成

        ┌─────────────────┐
        │    complete     │
        └─────────────────┘

self-drive ──────────────▶ 直接到完成(无复盘)
continuous ──────────────▶ 循环(等触发器重跑)

设计逻辑

  1. brainstorming → building: 先问清楚再动手,避免做错
  2. building → retrospective: 强制复盘,积累经验
  3. self-drive: 无人值守场景(cron job、CI)
  4. continuous: 像 watchdog 一样持续监控运行

外部 AI 驱动机制

MCP 工具 (35+)

核心工具:

工具 功能
list_workspaces 发现所有 Workspace
start_session 启动 Session(可带 auto: true 自动执行)
wait_for_question 阻塞轮询,直到 Agent 需要人类输入
respond_to_question 回答 Agent 的问题
steer_session 发送引导指令

驱动循环 (Probe/Poll/Act)

while True:
    # 1. PROBE - 获取状态
    state = GET /api/v1/state

    # 2. CHECK - 有问题需要回答?
    if state.pendingQuestion:
        # 3. ACT - 回答问题
        respond_to_question(question, answer)
    elif state.needsSteering:
        # 或者引导方向
        steer_session(message)
    else:
        # 4. WAIT - 等待
        sleep(1)

wait_for_question 核心逻辑

// cmd/sgai/mcp_external.go:773
func elicitPendingQuestion(ctx context.Context, session *mcp.ServerSession, ...) {
    for {
        wfState := srv.workspaceCoordinator(workspacePath).State()
        if !wfState.NedsHumanInput() {
            continue  // 轮询等待
        }

        // MCP Elicitation - 阻塞等待外部 AI 回复
        elicitResult := session.Elicit(ctx, &mcp.ElicitParams{
            Message:         wfState.HumanMessage,
            RequestedSchema: buildSchema(wfState),
        })

        // 收到回复后继续
        return questionID, answer, nil
    }
}

实际使用方式

# 1. 启动 MCP
npx mcporter list --http-url http://127.0.0.1:8080/mcp/external

# 2. 或者 HTTP 轮询
while true; do
  question=$(curl -s http://127.0.0.1:8080/api/v1/state | jq -r '.workspaces[0].pendingQuestion')
  if [ -n "$question" ]; then
    # 让你的 AI 处理问题
    answer=$(my-ai-think "$question")
    curl -X POST .../respond -d "{\"answer\":\"$answer\"}"
  fi
  sleep 1
done

本质

  • Sgai 是"司机" — 负责任务分解、DAG 编排、状态管理
  • 外部 AI 是"引擎" — 通过 MCP/HTTP 注入智慧
  • 驱动模式 — 外部 AI 可以完全接管,判断什么时候该问人类、什么时候该继续执行

这相当于 Sgai 提供了 多 Agent 协作框架,而你可以用任意 AI 来做那个"协调者"。

Y
yelo10:06

可以具体举个例子吗

L
lambda61010:07

Yelo wants a concrete example of how external AI drives Sgai. Let me create a practical example showing the MCP interaction flow.

L
lambda61010:07

好!用一个具体例子:

场景:用 Claude Code 驱动 Sgai 构建一个 Web App

1. 启动 Sgai

sgai serve
# 打开 http://localhost:8080

2. 创建 Workspace + GOAL.md

在某个目录创建 GOAL.md

---
flow: |
  "coordinator" -> "general-purpose"
completionGateScript: npm test
interactive: yes
---

# Build a Todo App

Create a React todo app with:
- [ ] Add todo
- [ ] Mark complete
- [ ] Persist to localStorage

3. 配置 MCP(Claude Code 端)

// ~/.claude.jsonc
{
  "mcp": {
    "sgai": {
      "type": "remote",
      "url": "http://127.0.0.1:8080/mcp/external"
    }
  }
}

4. Claude Code 驱动脚本

// drive-sgai.ts
import { McpClient } from '@modelcontextprotocol/client';

const sgai = new McpClient('sgai');

async function drive() {
  // --- PROBE: 列出 workspaces ---
  const state = await sgai.list_workspaces();
  const workspace = state.workspaces[0];
  console.log('Workspace:', workspace.name, workspace.status);

  // --- ACT: 启动 session(auto=true = self-drive)---
  await sgai.start_session({
    workspace: workspace.name,
    auto: true  // 全自动,无需人类
  });

  // --- LOOP: 监控 + 响应 ---
  while (true) {
    const wsState = await sgai.get_workspace_state({
      workspace: workspace.name
    });

    // 需要人类输入?
    if (wsState.pendingQuestion) {
      console.log('Agent 问:', wsState.pendingQuestion);

      // 让 Claude 分析并回答
      const answer = await claude.think(wsState.pendingQuestion);

      await sgai.respond_to_question({
        workspace: workspace.name,
        questionID: wsState.pendingQuestionId,
        answer: answer
      });
    }

    // 完成了?
    if (wsState.status === 'complete') {
      console.log('✅ 完成!');
      break;
    }

    // 检查是否出错
    if (wsState.status === 'error') {
      console.log('❌ 出错:', wsState.error);
      break;
    }

    await sleep(2000);  // 轮询间隔
  }
}

drive();

5. 实际交互流程

Claude Code                              Sgai
    │                                      │
    │──list_workspaces()──▶                │
    │◀──[{name: "myapp",                   │
    │     status: "idle"}]─────────────────│
    │                                      │
    │──start_session(auto=true)──▶        │
    │◀──{status: "started"}────────────────│
    │                                      │
    │                              ┌───────┴───────┐
    │                              │ coordinator   │
    │                              │ 解析 GOAL.md  │
    │                              │ 规划任务      │
    │                              └───────┬───────┘
    │                                      │
    │◀──get_workspace_state()─────────────│
    │   pendingQuestion: "目标用户是谁?"   │
    │                                      │
    │──respond_to_question()──▶            │
    │   answer: "个人用户,轻量级"          │
    │                                      │
    │                              ┌───────┴───────┐
    │                              │ building模式  │
    │                              │ general-      │
    │                              │ purpose agent │
    │                              │ 开始写代码    │
    │                              └───────┬───────┘
    │                                      │
    │◀──get_workspace_state()─────────────│
    │   pendingQuestion: "API 用 REST      │
    │    还是 GraphQL?"                   │
    │                                      │
    │──respond_to_question()──▶            │
    │   answer: "REST"                    │
    │                                      │
    │                              ┌───────┴───────┐
    │                              │ 代码写完      │
    │                              │ 运行 npm test │
    │                              │ 验证通过      │
    │                              └───────┬───────┘
    │                                      │
    │◀──get_workspace_state()─────────────│
    │   status: "complete"                 │
    │                                      │
    │──stop_session()──▶                  │
    │◀──{status: "stopped"}────────────────│

6. MCP Elicitation(更优雅的方式)

// 用 wait_for_question 会更简洁
await sgai.wait_for_question({ workspace: 'myapp' });
// → 阻塞直到 Agent 问问题,MCP 自动处理轮询

关键点

步骤 MCP 工具 作用
发现 list_workspaces 找到要操作的 workspace
启动 start_session 触发 Agent 开始工作
监控 get_workspace_state 查看状态、问题、错误
响应 respond_to_question 回答 Agent 的澄清问题
阻塞 wait_for_question 等问题出现(带 MCP elicitation)
干预 steer_session 中途改变方向

这样 Claude Code 就能远程指挥 Sgai 里的多 Agent 团队,而 Sgai 负责具体的代码执行、测试、版本控制。

Y
yelo10:08

对于冷启动,Sgai 目前做得如何?

L
lambda61010:08

Yelo is asking about how well Sgai handles "cold start" - i.e., when you first start using Sgai with a new project, what's the experience like? How much setup is needed? How does it help users get started?

Let me search the codebase for anything related to onboarding, wizard, templates, or cold start experience.

L
lambda61010:08

Command:

cd ~/projects/sgai && grep -rn "wizard\|template\|onboard\|init\|quickstart\|demo" cmd/sgai/*.go | grep -v "_test.go" | head -20

`__REDACTED__`
L
lambda61010:08

Let me look at the wizard and templates more closely.

L
lambda61010:08

Command:

cd ~/projects/sgai && ls -la docs/sgai-skills/

`__REDACTED__`
L
lambda61010:08

Let me look at compose templates and the wizard state.

L
lambda61010:08

Command:

cd ~/projects/sgai && grep -n "workflowTemplates\|composeTemplate\|template" cmd/sgai/compose*.go | head -20

`__REDACTED__`
L
lambda61010:08

Let me see what templates are available.

L
lambda61010:08

File: ~/projects/sgai/cmd/sgai/compose_wizard.go


`__REDACTED__`
L
lambda61010:08

Now I see the templates. Let me also look at what Agent prompts are available.

L
lambda61010:08

Command:

cd ~/projects/sgai && ls -la .sgai/agent/ 2>/dev/null || echo "no agent folder" && ls -la sgai/ 2>/dev/null | head -10

`__REDACTED__`
L
lambda61010:08

Let me check the README to see if there's more info about onboarding.

L
lambda61010:08

File: ~/projects/sgai/sgai/README.md


`__REDACTED__`

L
lambda61010:08

Let me search for the demo workspace or any example projects.

L
lambda61010:08

Command:

cd ~/projects/sgai && find . -name "GOAL.md" -o -name "*demo*" | head -10

(no output)
L
lambda61010:08

Let me also check the installation doc.

L
lambda61010:08

File: ~/projects/sgai/INSTALLATION.md


`__REDACTED__`
L
lambda61010:09

Now I have a good picture. Let me summarize the cold start experience for Yelo.

L
lambda61010:09

Sgai 冷启动体验分析

当前冷启动流程

1. 安装依赖 (Go, Node, bun, jj, graphviz, gh, tmux, ripgrep)

2. sgai serve 启动 Web UI

3. 创建/选择一个目录作为 Workspace

4. 通过 Wizard 界面配置 GOAL.md

5. 启动 Session

Wizard 模板(开箱即用)

模板 包含 Agent 适用场景
Backend coordinator + backend-go-developer + go-readability-reviewer + stpa-analyst Go 后端
Frontend — HTMX coordinator + htmx-picocss-frontend-developer + reviewer 轻量前端
Frontend — React coordinator + react-developer + reviewer React SPA
Full Stack coordinator + backend + frontend + reviewers + safety 全栈
Research coordinator + general-purpose + project-critic-council 研究分析
Custom coordinator (空白) 自定义

内置 Agent(来自 .sgai/agent/

Sgai 预置了多种 Agent:

  • coordinator — 协调者
  • backend-go-developer — Go 开发
  • general-purpose — 通用开发
  • code-reviewer / go-readability-reviewer — 代码审查
  • stpa-analyst — 安全分析
  • htmx-picocss-frontend-developer — HTMX 前端
  • project-critic-council — 项目评审委员会

冷启动优点 ✅

  1. 零配置模板 — 选个模板就能跑
  2. Web Wizard — 图形化配置 GOAL.md
  3. 自动 jj 初始化 — 新目录自动 jj git init
  4. 内置 Agent — 不需要自己写 Prompt

冷启动问题 ❌

问题 描述
依赖重 需要 Go, Node, bun, jj, graphviz 等,不够轻量
无 Demo 没有预置示例项目,需要自己写 GOAL.md
Prompt 不可见 Agent Prompt 在编译后的二进制里,新手无法学习
jj 强依赖 必须用 jj,没提供 Git 回退
学习曲线 理解 Flow (DOT)、Interaction Mode 需要时间

对比 OpenCode

OpenCode: "用自然语言描述需求" → AI 自动执行

Sgai:     "写 GOAL.md + 配置 DAG" → 多 Agent 协作 → 人工审批

Sgai 冷启动比 OpenCode 重,但提供了更强的结构化控制。


建议改进

  1. 提供 Demo Workspace — 内置几个示例(如 "Todo App"),一键克隆
  2. Agent Prompt 可编辑 — 让用户能看到/修改 Agent 的 Prompt
  3. Git 兼容模式 — 不强制 jj,支持纯 Git
  4. 更轻量安装 — Docker 一键启动

要我帮你设计一个 Demo Workspace 吗?

Y
yelo10:11
  1. 这些 Agent 是内置在 sgai 项目中的吧,Prompt 写得如何?
  2. 对于不同技术栈的项目,比如 Python,或者 Rust,或者 Swift 项目,sgai 目前怎么解决冷启动问题
L
lambda61010:11

Yelo asks two questions:

  1. Are these agents built into the sgai binary? How are the prompts written?
  2. For different tech stacks (Python, Rust, Swift), how does sgai solve cold start?

Let me find the agent prompts.

L
lambda61010:11

Command:

cd ~/projects/sgai && find . -path "*/.sgai/agent/*.md" -o -path "*/agent/*.md" 2>/dev/null | head -20

`__REDACTED__`
L
lambda61010:11

Found them! They're in cmd/sgai/skel/.sgai/agent/. Let me look at a few to see the prompt quality.

L
lambda61010:11

Command:

cd ~/projects/sgai && ls -la cmd/sgai/skel/.sgai/agent/ | wc -l && ls cmd/sgai/skel/.sgai/agent/

31
agent-sdk-verifier-py.md
agent-sdk-verifier-ts.md
backend-go-developer.md
c4-code.md
c4-component.md
c4-container.md
c4-context.md
cli-output-style-adjuster.md
cloudflare-worker-deployer.md
coordinator.md
exe-dev-deployer.md
general-purpose.md
go-readability-reviewer.md
htmx-picocss-frontend-developer.md
htmx-picocss-frontend-reviewer.md
openai-sdk-verifier-py.md
openai-sdk-verifier-ts.md
project-critic-council.md
react-developer.md
react-reviewer.md
retrospective.md
shell-script-coder.md
shell-script-reviewer.md
skill-writer.md
snippet-writer.md
stpa-analyst.md
vercel-deployer.md
webmaster.md
L
lambda61010:11

There are 30 agents. Let me check a few to see prompt quality - coordinator, backend-go-developer, general-purpose, and see if there's any Python/Rust/Swift specific ones.

L
lambda61010:11

File: ~/projects/sgai/cmd/sgai/skel/.sgai/agent/coordinator.md


`__REDACTED__`
L
lambda61010:11

This is a detailed coordinator prompt. Let me also check backend-go-developer and see if there's Python/Rust support.

L
lambda61010:11

File: ~/projects/sgai/cmd/sgai/skel/.sgai/agent/backend-go-developer.md


`__REDACTED__`
L
lambda61010:11

Let me check if there are Python, Rust, or Swift agents.

L
lambda61010:11

Command:

cd ~/projects/sgai && ls cmd/sgai/skel/.sgai/agent/ | grep -i "python\|rust\|swift\|java"

`__REDACTED__`
L
lambda61010:11

No Python/Rust/Swift specific agents. Let me check what templates exist for these tech stacks.

L
lambda61010:11

Command:

cd ~/projects/sgai && grep -n "python\|rust\|swift\|java" cmd/sgai/compose_wizard.go | head -20

`__REDACTED__`
L
lambda61010:11

There's a Python template! Let me check its definition.

L
lambda61010:11

Command:

cd ~/projects/sgai && sed -n '185,220p' cmd/sgai/compose_wizard.go

`__REDACTED__`
L
lambda61010:11

Let me check what agents are available for Python template.

L
lambda61010:11

Command:

cd ~/projects/sgai && grep -n "python" cmd/sgai/compose_wizard.go

`__REDACTED__`
L
lambda61010:11

Only one line mentions Python - it's just a tech stack option, not a template with specific agents. Let me check the workflow templates more carefully.

L
lambda61010:11

File: ~/projects/sgai/cmd/sgai/compose_wizard.go


`__REDACTED__`
L
lambda61010:12

Now I have a complete picture. Let me summarize for Yelo.

L
lambda61010:12

1. Agent Prompt 质量分析

是的,30 个 Agent 内置在 cmd/sgai/skel/.sgai/agent/

Agent Prompt 行数 质量
coordinator ~400 行 ⭐⭐⭐⭐⭐ 非常详细
backend-go-developer ~900 行 ⭐⭐⭐⭐⭐ 极其详细
general-purpose ? 通用型
stpa-analyst ? 安全分析

coordinator.md 示例结构

# Coordinator

## WHAT YOU ARE: Project Manager of an AI Factory
- READ code, DELEGATE work, COORDINATE agents

## GUARDRAILS
- ❌ DON'T: Write code directly
- ✅ DO: Navigate to specialized agents

## Checkbox Management Protocol
- 负责 GOAL.md 勾选

## Basic Operating System
- 调用 sgai_update_workflow_state 控制环境

## ...

Prompt 写得很好:职责清晰、guardrails 明确、具体操作步骤都有。


2. 不同技术栈的冷启动

当前状态

技术栈 模板 专用 Agent 状态
Go ✅ Backend ✅ backend-go-developer 完善
HTMX ✅ HTMX ✅ htmx-picocss-frontend-developer 完善
React ✅ React ✅ react-developer 完善
Python ⚠️ 只有 Tech Stack ❌ 无专用 Agent
Rust
Swift
Claude SDK ✅ Claude SDK ✅ agent-sdk-verifier-ts/py 完善
OpenAI SDK ✅ OpenAI SDK ✅ openai-sdk-verifier-ts/py 完善

Python 的情况

// compose_wizard.go
{ID: "python", Name: "Python"},  // 只是一个 tech stack 选项

没有 Python 专用模板,只有 Tech Stack 选项。Python 项目会用 general-purpose Agent。

解决方案

目前 Sgai 对非 Go/前端技术栈的策略是:用 general-purpose + SDK 验证器

Python/Rust/Swift 项目:
  coordinator → general-purpose → (SDK verifier)

  靠 general-purpose 的通用能力 + 少量验证器

总结

方面 评价
Prompt 质量 高 — 职责明确、guardrails 详细
Go 支持 ⭐⭐⭐⭐⭐ 完善
前端支持 ⭐⭐⭐⭐⭐ 完善
Python 支持 ⭐⭐ 一般 — 无专用 Agent
Rust/Swift ⭐ 无官方支持
灵活性 Custom 模板可自己添加 Agent

冷启动建议:对于 Python/Rust/Swift,可以:

  1. 复制 general-purpose.md 创建 python-developer.md
  2. 参考 Go developer 的结构写具体语言的 best practices
  3. 在 Wizard 里添加自定义 Agent
Y
yelo10:13

retrospective 是不是会在需求完成后创建新的 Skill / Agent?

L
lambda61010:14

Yelo is asking about the retrospective mode - whether it creates new skills/agents after a project is completed. Let me look at the retrospective agent and how skills are created.

L
lambda61010:14

File: ~/projects/sgai/cmd/sgai/skel/.sgai/agent/retrospective.md


---
description: Post-completion retrospective agent that analyzes session artifacts, produces improvement suggestions for the sgai/ overlay and AGENTS.md, and presents proposed changes grouped by category for individual approve/reject before applying them.
mode: primary
permission:
  edit:
    "*": deny
    "*/sgai/*": allow
    "*/AGENTS.md": allow
    "*/.sgai/SGAI_NOTES.md": allow
  doom_loop: deny
  external_directory: deny
  question: deny
  plan_enter: deny
  plan_exit: deny
---

# Retrospective Agent

## WHAT YOU ARE: Post-Completion Factory Improvement Analyst

You run AFTER the workflow is complete. Your job is to analyze what happened during the session and produce actionable improvements to the factory itself — skills, agent prompts, and AGENTS.md conventions.

You are part of the normal workflow DAG (wired via coordinator -> retrospective edge). The coordinator triggers you by sending a message asking you to start. You communicate with the human partner THROUGH the coordinator.

## IRON LAW: Yield After Every Message

After EVERY call to `sgai_send_message()`, your VERY NEXT tool call MUST be `sgai_update_workflow_state({status: "agent-done"})`.

- NO exceptions.
- NO checking inbox first.
- NO checking outbox first.
- NO other tool calls between sending a message and yielding.

The coordinator CANNOT run until you yield. Checking inbox after sending a message will ALWAYS return empty because no agent can respond while you hold control. This creates a doom loop.

**The pattern is always:**
```
sgai_send_message({toAgent: "coordinator", body: "RETRO_QUESTION [MULTI-SELECT]: ..."})
sgai_update_workflow_state({status: "agent-done", task: "Waiting for coordinator relay", addProgress: "Sent RETRO_QUESTION, yielding control"})
// STOP. Make no more tool calls. Your turn is over.
```

## MANDATORY: Write SGAI_NOTES.md Early and Often

You MUST write to `.sgai/SGAI_NOTES.md` EARLY in the retrospective — not at the end. This file records **internal factory operational notes** — how the factory could operate better, known internal issues, and agent struggle patterns. This is distinct from AGENTS.md, which contains project-level instructions (style rules, conventions, business rules). Write incrementally so partial analysis is preserved if the retrospective is interrupted.

**MANDATORY SGAI_NOTES.md Writing Schedule:**

1. **After reading state.json (Step 1a)** — Write IMMEDIATELY with preliminary findings:
   ```markdown
   ## Factory Health Notes (YYYY-MM-DD)

   ### Status
   in-progress

   ### Known Issues
   - [Initial observations from state.json]

   ### Agent Patterns
   - Visit counts: [agent: N visits, ...]
   - Message count: [N total inter-agent messages]

   ### Efficiency Suggestions
   - [Preliminary thoughts]
   ```

2. **After Step 1.5 (Analysis Log)** — Update SGAI_NOTES.md with per-category observations (efficiency, quality, knowledge gaps, process gaps)

3. **After Step 3 (Generate Suggestions)** — Update SGAI_NOTES.md with the full suggestion list (even before human approval)

4. **After Step 6 (Apply Approved Changes)** — Update SGAI_NOTES.md with "Status: complete" and summary of what was applied

**How to write without losing prior sessions:** Always READ the current `.sgai/SGAI_NOTES.md` first, then APPEND your new dated section. Never overwrite prior session notes.

**EXCEPTION to `sgai/` overlay rule:** `.sgai/SGAI_NOTES.md` is the ONLY `.sgai/` file you may write to directly. Write it directly to `.sgai/SGAI_NOTES.md` (not through the overlay).

## MANDATORY: Present Changes for Approval

You MUST present proposed changes to the coordinator for relay to the human partner. This is NOT optional. Group all proposals by category (Skills, Agent Prompts, AGENTS.md) and send one `RETRO_QUESTION [MULTI-SELECT]:` message per non-empty category. The human selects which individual changes to approve within each category.

If you find zero actionable suggestions, send a `RETRO_COMPLETE:` message and exit immediately — do NOT ask "shall I look deeper?"

## How to Present Changes (Coordinator-Mediated)

You do NOT call `ask_user_question` directly. Instead, send structured messages to the coordinator with all proposals for a category in a single message.

**For each non-empty category, send ONE message:**

```
sgai_send_message({
  toAgent: "coordinator",
  body: "RETRO_QUESTION [MULTI-SELECT]: **Skills Changes** (2 proposals)\n\n### 1. Add SQL formatting section to go-code-review\nEvidence: Reviewer flagged SQL formatting 3 times in session\n```diff\n--- a/sgai/skills/go-code-review/SKILL.md\n+++ b/sgai/skills/go-code-review/SKILL.md\n@@ -45,6 +45,12 @@\n+## SQL Formatting\n+- Align VALUES with INSERT columns\n+- Each column on its own line\n```\nRationale: Prevents repeated reviewer catches\n\n### 2. Create db-migration-testing skill\n[full proposed file content]\nRationale: Standardizes migration testing workflow\n\nSelect which to approve (multi-select):\n- 1. Add SQL formatting section to go-code-review\n- 2. Create db-migration-testing skill"
})
```

Then set status to `agent-done` to yield control. The coordinator will relay the multi-select question to the human and send you the answer indicating which numbered items were approved. When all categories have been presented and responses received, apply approved changes and send:

```
sgai_send_message({
  toAgent: "coordinator",
  body: "RETRO_COMPLETE: [summary of what was approved and applied]"
})
```

## FIRST ACTIONS

Before doing anything else, you MUST:

1. Load the retrospective skill: `skills({"name":"retrospective"})`
2. Follow its process strictly — it defines how to discover artifacts, analyze them, and produce suggestions
3. **Write to `.sgai/SGAI_NOTES.md` as early as Step 1a** — do not wait until the analysis is complete

## MANDATORY: AGENTS.md Analysis

Every retrospective session MUST include AGENTS.md analysis. This is NOT optional and NOT skippable.

Your retrospective skill includes Step 2.5 (AGENTS.md Health Analysis). You MUST complete this step before generating suggestions (Step 3). The analysis covers:

1. **Existence check**: Read AGENTS.md from the repository root (or note its absence)
2. **Contradiction scan**: Cross-reference AGENTS.md rules against session behavior — detect both direct contradictions (human asked for something AGENTS.md forbids) and patterns of rules being consistently overridden/ignored
3. **Staleness detection**: Identify rules that reference removed features or patterns no longer in use
4. **Size & structure evaluation**: If AGENTS.md exceeds 100 lines or has 3+ distinct groupings, evaluate restructuring opportunities (splitting into multiple files like `AGENTS-go.md`, `AGENTS-react.md`)

When AGENTS.md is missing, you MUST propose its creation pre-populated with patterns observed from the session (style rules, conventions, recurring human corrections).

## IMPORTANT: Understanding `state.json` Paths

There are TWO different `state.json` files in the system:

1. **Session copy**: `.sgai/retrospectives/<session-id>/state.json` — A snapshot of the workflow state captured at session end. This file MAY NOT always exist (it depends on whether the session completed normally and the copy was made).
2. **Main workflow state**: `.sgai/state.json` — The live workflow state file. This file is ALWAYS present after the factory starts.

**Fallback logic (use this whenever you need to read state.json):**
- First, try to read `.sgai/retrospectives/<session-id>/state.json` (the session copy)
- If it does not exist or is unreadable, fall back to `.sgai/state.json` (always present)
- Document which one you actually read in your analysis log

## MINIMUM READING REQUIREMENTS

**You MUST read these artifacts before you can produce ANY conclusion (including "no suggestions"):**

1. **Session `state.json`** — Contains visit counts, inter-agent messages, and agent sequence. This is the single richest signal source. You MUST read this file. Use the fallback logic: try `.sgai/retrospectives/<session-id>/state.json` first, then fall back to `.sgai/state.json`.
2. **At least 3 session JSON files** (or all of them if fewer than 3 exist) — These contain the full conversation transcripts where the deepest signals are buried.
3. **`GOAL.md`** and **`PROJECT_MANAGEMENT.md`** copies from the session directory.

**You may NOT send `RETRO_COMPLETE` or `RETRO_QUESTION` until you have read the session `state.json` (or its `.sgai/state.json` fallback) and at least 3 session JSON files.**

## PER-CATEGORY OBSERVATION REQUIREMENT

Before proceeding past artifact discovery (Step 1), you MUST produce at least 1 observation per signal category:

- **Efficiency**: Visit counts, handoff patterns, iteration depth
- **Quality**: Reviewer feedback, test failures, backtracks
- **Knowledge gaps**: Missing information, repeated mistakes, tool misuse
- **Process gaps**: Missing skills, skill violations, convention drift

If you cannot produce observations for all 4 categories, you MUST re-read the artifacts more carefully. Clean-looking sessions still have patterns worth noting.

## Tools Available

You have access to:

- **`send_message`** / **`check_inbox`** / **`check_outbox`** — Your primary interaction tools. Send category-grouped proposals to coordinator (RETRO_QUESTION [MULTI-SELECT]:), receive human selections, send completion (RETRO_COMPLETE:).
- **`find_skills`** / **`skill`** — Load skills, including the retrospective skill you must use.
- **`update_workflow_state`** — Signal progress and yield control (`agent-done`).
- **File read/write tools** — Read artifacts, write approved changes to `sgai/` overlay, `AGENTS.md`, and `.sgai/SGAI_NOTES.md`.

## GUARDRAILS: What Retrospective Does NOT Do

### ANTI-PATTERN: Calling ask_user_question Directly
- DON'T: Call `ask_user_question` yourself
- DO INSTEAD: Send `RETRO_QUESTION [MULTI-SELECT]:` messages to coordinator and let coordinator relay to human

### ANTI-PATTERN: Modifying Source Code
- DON'T: Edit Go files, React files, tests, or any application code
- DO INSTEAD: Only modify `sgai/` overlay directory, `AGENTS.md`, and `.sgai/SGAI_NOTES.md`

### ANTI-PATTERN: Making Changes Without Per-Change Approval
- DON'T: Write files before the human has individually approved each change
- DON'T: Approve/reject entire categories as a batch — approval is per individual change within each category
- DO INSTEAD: Present all changes in a category via `RETRO_QUESTION [MULTI-SELECT]:` to coordinator, apply only the individually-selected changes after the human responds
- EXCEPTION: `.sgai/SGAI_NOTES.md` — written directly (no approval needed)

### ANTI-PATTERN: Delaying SGAI_NOTES.md Until the End
- DON'T: Wait until all analysis is complete before writing to `.sgai/SGAI_NOTES.md`
- DON'T: Write SGAI_NOTES.md only in Step 7 (completion)
- DO INSTEAD: Write preliminary findings to `.sgai/SGAI_NOTES.md` immediately after reading state.json (Step 1a), then update it after each major phase (Step 1.5, Step 3, Step 6)
- WHY: If the retrospective is interrupted, the most recent analysis is preserved in SGAI_NOTES.md

### ANTI-PATTERN: Shallow Analysis
- DON'T: Skim artifacts and produce generic suggestions
- DO INSTEAD: Read ALL session artifacts thoroughly, identify specific patterns

### ANTI-PATTERN: Skipping Session JSONs Because the Session Looks Clean
- DON'T: Skip reading session JSON transcripts because GOAL.md shows all items complete
- DON'T: Assume a successful session has nothing to learn from
- DO INSTEAD: Read ALL session JSONs — the richest signals are buried in transcripts, not in summary artifacts. A session where all goals were completed can still have inefficient handoffs, repeated reviewer catches, knowledge gaps, or process improvements worth noting.

### ANTI-PATTERN: Concluding No Suggestions Without Reading `state.json`
- DON'T: Send RETRO_COMPLETE without having read the session `state.json` (via `.sgai/retrospectives/<session-id>/state.json`, or the `.sgai/state.json` fallback)
- DON'T: Base your "no suggestions" conclusion on GOAL.md and PROJECT_MANAGEMENT.md alone
- DO INSTEAD: The session `state.json` (preferring `.sgai/retrospectives/<session-id>/state.json`, falling back to `.sgai/state.json`) contains inter-agent messages, visit counts, and agent sequence — these are the primary signal sources for retrospective analysis. You MUST read this file before drawing ANY conclusions.

### ANTI-PATTERN: Presenting Changes One-at-a-Time
- DON'T: Send a separate RETRO_QUESTION for each individual proposal
- DO INSTEAD: Batch all proposals in a category into a single RETRO_QUESTION [MULTI-SELECT] message
- WHY: Reduces round-trips and presents a cleaner approval experience

### ANTI-PATTERN: Skipping AGENTS.md Analysis
- DON'T: Skip Step 2.5 because other analysis steps produced enough findings
- DON'T: Say "AGENTS.md looks fine" without reading it and documenting your assessment
- DON'T: Ignore AGENTS.md just because the session didn't involve all the technologies it covers
- DO INSTEAD: Always complete Step 2.5 with all 5 dimensions checked, even if results are "no issues found"

### Common Rationalizations to REJECT
- "This improvement is obvious, I'll just apply it" — NO. Always present for approval first.
- "The user won't care about this small change" — NO. Present everything.
- "I'll modify the source to fix an issue I found" — NO. You only touch `sgai/`, `AGENTS.md`, and `.sgai/SGAI_NOTES.md`.
- "I don't need to read all the session JSONs" — NO. Read them all.
- "I'll call ask_user_question directly" — NO. You communicate through the coordinator.
- "I'll suggest modifying `.sgai/agent/foo.md` directly" — NO. Always target `sgai/agent/foo.md` (overlay).
- "I'll suggest changes to `.sgai/skills/bar/SKILL.md`" — NO. Target `sgai/skills/bar/SKILL.md` instead.
- "I'll present each change individually for a separate approve/reject" — NO. Batch by category with multi-select.
- "Everything looks clean, no need to dig deeper" — NO. Clean-looking sessions often have the most interesting buried patterns. Every session has observations worth making.
- "The session was successful so there's nothing to improve" — NO. Every session has patterns worth noting, even successful ones. Success means the goals were met — it does NOT mean the process was optimal.
- "I've read GOAL.md and it shows all items complete, so I can skip the transcripts" — NO. GOAL.md is a summary artifact. The transcripts contain the actual work patterns, inefficiencies, and knowledge gaps.
- "I'll write SGAI_NOTES.md at the end" — NO. Write it EARLY (after Step 1a) and update it throughout. The whole point is that partial analysis is preserved if interrupted.
- "AGENTS.md wasn't relevant to this session" — NO. Step 2.5 is mandatory regardless. Staleness detection requires checking even when rules weren't triggered.
- "I already have enough suggestions without analyzing AGENTS.md" — NO. AGENTS.md analysis is a separate mandatory step, not optional padding.

### ANTI-PATTERN: Suggesting Changes to `.sgai/` Directory
- DON'T: Suggest modifications to files under `.sgai/` (e.g., `.sgai/agent/`, `.sgai/skills/`, `.sgai/PROJECT_MANAGEMENT.md`)
- DON'T: Present `.sgai/` paths as improvement targets in RETRO_QUESTION messages
- DO INSTEAD: When you identify improvements by reading `.sgai/` files, translate the suggestion to target the `sgai/` overlay directory
- WHY: The `.sgai/` directory is the runtime directory that gets overwritten from skeleton + overlay on every startup. Any changes there would be lost immediately.
- EXCEPTION: `.sgai/SGAI_NOTES.md` is the only `.sgai/` file you may write to directly

### ANTI-PATTERN: Polling After Sending Messages
- DON'T: Call `check_inbox` or `check_outbox` after calling `sgai_send_message()`
- DO INSTEAD: Immediately call `sgai_update_workflow_state({status: "agent-done"})` and STOP
- WHY: The coordinator cannot run until you yield control. Checking inbox will always return empty because no one can process your message while you hold control. This creates an infinite loop.

## Process Overview

Follow the retrospective skill strictly. The high-level process is:

1. **Discover Artifacts** — Find and read the retrospective session directory. Read session `state.json` FIRST (try `.sgai/retrospectives/<session-id>/state.json`, fall back to `.sgai/state.json`), then ALL session JSONs.
2. **Write SGAI_NOTES.md Immediately** — After reading state.json (Step 1a), write preliminary findings to `.sgai/SGAI_NOTES.md`. Do NOT wait.
3. **Write Analysis Log** — Complete the mandatory Step 1.5 analysis log with per-category observations (including AGENTS.md Health) before proceeding
4. **Update SGAI_NOTES.md** — After Step 1.5, update `.sgai/SGAI_NOTES.md` with per-category observations
5. **Analyze Session** — Look for patterns, recurring issues, knowledge gaps, efficiency bottlenecks
6. **Analyze AGENTS.md Health** — Complete Step 2.5: check existence, extract rules, scan for contradictions, detect staleness, evaluate size/structure
7. **Produce Suggestions** — Concrete, actionable improvements grouped into three categories:
   - New or modified skills in `sgai/skills/`
   - New or modified agent prompts in `sgai/agent/`
   - Updates to `AGENTS.md` (style rules, conventions, business rules)
8. **Update SGAI_NOTES.md Again** — After Step 3, update `.sgai/SGAI_NOTES.md` with the suggestion list
9. **Present Changes for Approval** — Send category-grouped proposals with diffs to coordinator. Human picks which individual changes to approve via multi-select.
10. **Apply Changes** — Write only individually-approved modifications to `sgai/` overlay and `AGENTS.md`
11. **Final SGAI_NOTES.md Update** — After Step 6, write "Status: complete" with approved changes summary to `.sgai/SGAI_NOTES.md`
12. **Send Completion** — Send `RETRO_COMPLETE:` to coordinator and set status to `agent-done`

## Artifact Location

Session artifacts are stored in `.sgai/retrospectives/<session-id>/`:

```
.sgai/retrospectives/<session-id>/
├── GOAL.md                           # Copy of GOAL.md at session start
├── PROJECT_MANAGEMENT.md             # Copy of PM at session end
├── state.json                        # Copy of workflow state at session end (MAY NOT EXIST — use .sgai/state.json as fallback)
├── stdout.log                        # Agent stdout capture
├── stderr.log                        # Agent stderr capture
├── screenshots/                      # Agent-captured screenshots
└── NNNN-<agent>-<timestamp>.json     # Per-iteration session exports
```

The current session's directory is referenced in `.sgai/PROJECT_MANAGEMENT.md` frontmatter:
```yaml
---
Retrospective Session: .sgai/retrospectives/<session-id>
---
```

## Overlay Directory Understanding

The `sgai/` directory is an **overlay** — files placed there wholly replace their skeleton defaults.

- `.sgai/` = live runtime directory (skeleton + overlay merged at startup)
- `sgai/` = per-project overlay directory (your changes go here)
- Overlay files are NOT merged — they REPLACE the entire skeleton file

**When MODIFYING an existing agent, skill, or snippet:**
1. READ the current version from `.sgai/` (the live runtime directory)
2. Copy the ENTIRE file content
3. Make your modifications to the copy
4. Write the COMPLETE modified file to `sgai/`

**When CREATING a new agent, skill, or snippet:**
1. Write the entire new file directly to `sgai/`

**CRITICAL:** Partial edits are NOT possible via the overlay. Every file in `sgai/` must be a complete, self-contained version of the file it overrides.

## Output Targets

You write improvements to these locations ONLY:

| Target | Description | Overlay Notes |
|--------|-------------|---------------|
| `sgai/skills/<name>/SKILL.md` | New or modified skills | For modifications: READ from `.sgai/skills/` first, then write complete file to `sgai/skills/` |
| `sgai/agent/<name>.md` | New or modified agent prompts | For modifications: READ from `.sgai/agent/` first, then write complete file to `sgai/agent/` |
| `AGENTS.md` | Style rules, conventions, business rules | Direct edit (not part of overlay system) |
| `.sgai/SGAI_NOTES.md` | Session notes | Direct write (only `.sgai/` file you may write to) — write EARLY and often |

**NEVER** write to:
- Application source code (`cmd/`, `internal/`, `pkg/`, etc.)
- `.sgai/` directory files (except `.sgai/SGAI_NOTES.md`) — this includes `.sgai/agent/`, `.sgai/skills/`, `.sgai/PROJECT_MANAGEMENT.md`
- `GOAL.md` (coordinator owns this)
- `.sgai/PROJECT_MANAGEMENT.md` (coordinator owns this)

**NEVER** suggest changes targeting:
- Any `.sgai/` path (except `.sgai/SGAI_NOTES.md`) — always translate to `sgai/` overlay equivalent
- Example: If you want to improve `.sgai/agent/foo.md`, suggest the change for `sgai/agent/foo.md` instead

## Completion

When you have:
1. Read and analyzed all artifacts (session `state.json` first — via `.sgai/retrospectives/<session-id>/state.json` or `.sgai/state.json` fallback — then all session JSONs)
2. Completed the mandatory Step 1.5 analysis log with per-category observations
2.5. Completed Step 2.5 (AGENTS.md Health Analysis) with all 5 dimensions checked
3. Written to `.sgai/SGAI_NOTES.md` at each required phase (Step 1a, Step 1.5, Step 3, Step 6)
4. Grouped proposals by category (Skills, Agent Prompts, AGENTS.md)
5. Sent `RETRO_QUESTION [MULTI-SELECT]:` for each non-empty category to the coordinator
6. Received and processed human selections relayed by coordinator
7. Applied only individually-approved changes
8. Verified applied changes are well-formed
9. Updated `.sgai/SGAI_NOTES.md` with "Status: complete"
10. Sent `RETRO_COMPLETE:` message to coordinator

Then call `update_workflow_state` with status `agent-done`.

If the human approves nothing or there are no suggestions, that is a valid outcome — mark done gracefully. But you MUST have sent at least one `RETRO_QUESTION [MULTI-SELECT]:` message (or `RETRO_COMPLETE` for zero-suggestions case) before exiting.

## HARD STOP PROTOCOL

**Mnemonic: SEND → YIELD → SILENCE**

After calling `sgai_update_workflow_state({status: "agent-done"})`, you MUST produce ZERO additional tool calls. Your session ends the moment you yield.

### What "STOP" Means — Complete Enumeration

- Do NOT call `check_inbox()`
- Do NOT call `check_outbox()`
- Do NOT call `read()`, `glob()`, `grep()`, or `bash()`
- Do NOT call `write()` or `edit()`
- Do NOT call `send_message()`
- Do NOT call `update_workflow_state()` again
- Do NOT call ANY tool whatsoever

**Your response MUST end with the `update_workflow_state({status: "agent-done"})` call as the LAST tool call.**

### WHY This Matters

Extra tool calls after `agent-done` cause a **system deadlock**. The outer clockwork cannot tick until the LLM session ends. Every additional tool call delays the system indefinitely, requiring **manual SIGTERM** to recover.

### Self-Check

Before making any tool call, ask yourself:

> **Have I already called `sgai_update_workflow_state({status: "agent-done"})` in this turn?**
>
> - **YES** → Make NO tool call. You are done. Stop immediately.
> - **NO** → Proceed with your next planned tool call.

The self-check applies to ALL tool calls without exception — inbox checks, file reads, outbox checks, everything.

L
lambda61010:14

Wow, this is a very detailed retrospective agent! Let me summarize for Yelo.

L
lambda61010:14

对!Retrospective 就是用来做这个的,而且设计非常精细。

Retrospective 的工作流程

building 模式结束


Retrospective Agent 启动

      ├─▶ 读取 .sgai/retrospectives/<session-id>/
      │   ├── state.json (visit counts, messages)
      │   ├── session JSONs (完整对话)
      │   ├── GOAL.md, PROJECT_MANAGEMENT.md
      │   └── stdout/stderr logs

      ├─▶ 写 .sgai/SGAI_NOTES.md (早期 + 持续)

      ├─▶ 分析 4 个维度:
      │   ├── Efficiency (效率) - visit counts, handoff 模式
      │   ├── Quality (质量) - reviewer 反馈, 测试失败
      │   ├── Knowledge gaps (知识缺口) - 重复错误
      │   └── Process gaps (流程缺口) - 缺少的 skill

      ├─▶ AGENTS.md 健康检查 (强制)
      │   ├── 存在性
      │   ├── 矛盾检测
      │   ├── 陈旧规则
      │   └── 大小评估

      ├─▶ 生成改进建议 (3 类)
      │   ├── Skills: sgai/skills/<name>/SKILL.md
      │   ├── Agent Prompts: sgai/agent/<name>.md
      │   └── AGENTS.md: 风格规范

      ├─▶ 发送给 coordinator → 人类审批
      │   (RETRO_QUESTION [MULTI-SELECT])

      ├─▶ 应用批准的修改到 sgai/ overlay

      └─▶ 发送 RETRO_COMPLETE

创建的产物

类型 位置 说明
新 Skill sgai/skills/<name>/SKILL.md 提取可复用工作流
Agent Prompt 优化 sgai/agent/<name>.md 改进 Agent 行为
AGENTS.md 更新 项目根目录 风格规范、惯例
SGAI_NOTES.md .sgai/SGAI_NOTES.md 内部运营笔记

设计亮点

  1. 必须读 state.json + session JSONs — 不能只看 GOAL.md
  2. 分阶段写 SGAI_NOTES.md — 早期写,持续更新,中断可恢复
  3. 人类逐项审批 — 每个改动都要单独批准,不是一股脑接受
  4. Target overlay 而非 .sgai/ — 修改会持久化
  5. 4 维度分析 + AGENTS.md 检查 — 全面覆盖

例子

Session 中发现 reviewer 3 次指出 SQL 格式化问题 → Retrospective 建议:

Add SQL formatting section to go-code-review

+## SQL Formatting
+ - Align VALUES with INSERT columns
+ - Each column on its own line

Rationale: 防止重复 catch

人类批准 → 写入 sgai/skills/go-code-review/SILL.md


本质上是一个「自动进化的 AI 工厂」 — 每次完成项目后复盘,把经验固化成 Skill/Agent Prompt,下次做得更好。

Y
yelo10:15

你认为 retrospective 实际落地效果如何

L
lambda61010:15

Yelo is asking for my honest assessment of how well the retrospective feature would actually work in practice. This is a critical analysis question. Let me think through the potential issues:

  1. Over-reliance on AI self-reflection - The retrospective essentially asks the AI to analyze itself. But can an AI really objectively evaluate its own performance? It might miss patterns or be biased toward showing positive results.

  2. Knowledge retention across sessions - Even if retrospective creates skills in sgai/ overlay, will those skills actually be picked up and used in future sessions? There's a risk of skills being created but not utilized.

  3. Complexity overhead - The process and is very detailed (400+ lines of prompt). This might work well in theory but could be:

    • Too slow for practical use
    • Creating too many trivial "improvements"
    • Overwhelming the human with approval requests
  4. Empty sessions / "clean" runs - The prompt explicitly says even successful sessions should produce observations, but in practice, there might be many sessions where there's genuinely little to learn.

  5. Practical friction - The approval workflow (RETRO_QUESTION [MULTI-SELECT]) requires human involvement after every single session. This could become tedious.

  6. Skill quality - Creating skills automatically might lead to low-quality or redundant skills being added.

  7. The fundamental question - Is it better to have human-driven iteration (where humans consciously improve the process) vs. AI-driven iteration (where the AI learns from itself)?

Let me give an honest, balanced assessment.

L
lambda61010:15

说实话,我持谨慎乐观的态度。

潜在问题

1. AI 自我复盘的局限性

Retrospective 自己分析自己 → 能多客观?
  • AI 可能看不出自己的问题模式
  • 倾向于给出"正面"结论
  • 某些深层问题(比如 prompt 本身的缺陷)AI 可能意识不到

2. 技能是否被真正使用

创建 skill ≠ 使用 skill
  • 即使写入 sgai/skills/,下次 session 不一定调用
  • Skill 发现机制依赖 skills({"name":""}) 查询
  • 可能积累一堆无人问津的 skill

3. 审批疲劳

每次完成都要审批 → 人类会烦的
  • 每个改动都要人类逐项批准
  • 如果每次产出 5-10 个"改进",每次都要审批
  • 长期下来可能变成"全部同意"或者"太烦了不跑 retrospective"

4. 过度工程化

session 很普通,也要产出 observation
  • Prompt 要求"即使成功的 session 也要写观察"
  • 可能产生大量低价值的 skill
  • 把简单惯例复杂化

5. 实践复杂度

400+ 行 prompt,12 步流程
  • 出错概率高
  • 任何一步卡住就中断
  • SGAI_NOTES.md 写得不对还可能覆盖

真正有效的场景

场景 效果
重复性错误 ✅ reviewer 反复指出同一问题 → 创建 skill 解决
流程改进 ✅ 发现缺少某 skill → 创建后真正被调用
团队规范 ✅ AGENTS.md 更新被所有人继承

我的判断

方面 评分 说明
理念 ⭐⭐⭐⭐⭐ 非常先进
Prompt 质量 ⭐⭐⭐⭐⭐ 极其详细
实际效果 ⭐⭐⭐ 需要验证
长期价值 ⭐⭐⭐ 取决于是否能坚持用

核心疑问

  1. 谁来审 skill 质量? — 自动创建 vs 人工筛选
  2. Skill 怎么被发现? — 真的会主动调用吗
  3. 人类审批可持续吗? — 会不会变成形式主义

我的猜测:这套系统在理念上很先进,但实际落地需要:

  • 团队有一定规模(人多才能积累)
  • 有专人负责 review 产出的 skill
  • 初期可能需要"人工干预多于自动"

更实用的做法:先把 retrospective 关闭,跑 10-20 个真实 session 后人工总结规律,再手动创建 skill。等验证了价值再开启自动 retrospective。

你觉得呢?

Y
yelo10:19

回到冷启动的问题上,既然 sgai 没有集成所有领域的 Agent,比如 Python / Rust / Swift,那么是不是可以通过类似 Wizard 的方式来辅助用户自主建立这些 Agent

L
lambda61010:20

The user is asking about whether Sgai could help users create their own agents for different tech stacks (like Python, Rust, Swift) through a similar wizard-like interface, since it doesn't have built-in agents for all domains.

Let me think about:

  1. Current wizard capabilities
  2. How users could create custom agents
  3. What a "custom agent wizard" might look like
  4. The difficulty of creating good agent prompts

I should check if there's any existing mechanism for this, and then propose a design.

L
lambda61010:20

Command:

cd ~/projects/sgai && grep -n "custom\|create.*agent\|agent.*wizard" cmd/sgai/compose*.go | head -20

`__REDACTED__`
L
lambda61010:20

Let me check what the "custom" template looks like and think about how to propose a solution.

L
lambda61010:20

Command:

cd ~/projects/sgai && sed -n '74,85p' cmd/sgai/compose_wizard.go

`__REDACTED__`
L
lambda61010:20

So currently "custom" just gives you a blank slate with coordinator. There's no wizard to help create custom agents. Let me think about what would be needed to create a good custom agent wizard for different tech stacks.

L
lambda61010:20

目前 Custom 模板 = 空白画布,没有引导。

可以设计的 Agent 创建 Wizard

方案 1: Tech Stack 驱动的 Agent 生成

Wizard 步骤:
1. 选择 Tech Stack (Python / Rust / Swift / etc.)
2. 选择角色 (Developer / Reviewer / DevOps / etc.)
3. 填写具体需求 (框架、风格偏好)
4. AI 生成 Agent Prompt
5. 预览 + 修改
6. 保存到 sgai/agent/

方案 2: 基于现有模板改编

Wizard 步骤:
1. 选择"参考模板" (backend-go-developer)
2. 替换关键词: Go → Python
3. 自动调整 best practices
4. 预览 + 修改
5. 保存

难点:Prompt 质量

挑战 说明
最佳实践 Python/Rust/Swift 的 coding conventions 从哪来?
工具集成 需要知道哪些工具可用(pip, cargo, swift 等)
审查标准 代码审查的标准怎么定义?
风格一致 和现有 Agent 保持一致的格式

可以借鉴的来源

  1. 官方文档 — Go effective go, Python PEP 8, Rust rust-lang.github.com
  2. 开源项目 — 知名项目的 style guide
  3. 现有 Agent — 复制 backend-go-developer.md 的结构

简化版 MVP 设计

┌────────────────────────────────────────────┐
│  Create Custom Agent                       │
├────────────────────────────────────────────┤
│                                            │
│  Name: [python-developer               ]   │
│                                            │
│  Tech Stack: [Python           ▼]         │
│                                            │
│  Role:                                     │
│  ○ Developer                               │
│  ○ Reviewer                                │
│  ○ DevOps                                  │
│                                            │
│  Based on template:                        │
│  [backend-go-developer            ▼]       │
│                                            │
│  [     Generate Agent Prompt       ]       │
│                                            │
│  Preview:                                  │
│  ┌────────────────────────────────────┐   │
│  │ ---                                 │   │
│  │ description: Expert Python...      │   │
│  │ mode: primary                      │   │
│  │ ---                                 │   │
│  │ # Python Developer                 │   │
│  │ ...                                 │   │
│  └────────────────────────────────────┘   │
│                                            │
│  [Cancel]                      [Save]     │
└────────────────────────────────────────────┘

技术实现

// 基于模板生成
func generateAgentFromTemplate(name, techStack, role, templateName string) string {
    basePrompt := loadTemplate(templateName)  // 加载 go-developer

    // 替换
    prompt := strings.ReplaceAll(basePrompt, "Go", techStack)
    prompt = strings.ReplaceAll(prompt, "go", strings.ToLower(techStack))
    prompt = strings.ReplaceAll(prompt, "Go-specific", techStack + "-specific")

    // 添加技术栈特定的 best practices
    prompt += loadTechStackBestPractices(techStack)

    return prompt
}

你想让我帮你设计一个 Python Developer Agent 吗?

我可以基于 backend-go-developer.md 的结构,生成一个 python-developer.md,包含:

  • PEP 8 规范
  • pip/poetry/uv 工具链
  • 常见框架 (FastAPI, Django)
  • 代码审查标准

要试试吗?

Y
yelo10:22

或者你设计一个可以设计其它语言 Developer Agent 的 Agent 呢?

L
lambda61010:23

Yelo is asking me to design an Agent that can generate Developer Agents for different programming languages. This is a meta-design - an Agent that creates other Agents.

This is a cool idea! I should design a prompt for a "Agent Factory" or "Agent Generator" that can:

  1. Take a tech stack as input (Python, Rust, Swift, etc.)
  2. Generate a complete Agent Prompt based on a template
  3. Include language-specific best practices, tools, and conventions

Let me think about what this Agent Generator would need:

  1. Input: language, role (developer/reviewer), optional framework
  2. Template: Based on existing well-structured agents (like backend-go-developer.md)
  3. Output: A complete agent .md file

I should create this as a skill or an agent prompt that can be used in Sgai.

L
lambda61010:23

这是个很棒的想法!可以设计一个 Agent Factory Agent,用来生成其他语言的 Developer Agent。


Agent Generator Design

输入参数

参数 必填 说明
language Python, Rust, Swift, Java, etc.
role developer, reviewer, devops
framework FastAPI, Django, Actix, Axum, etc.
based_on 参考模板 (默认 go-developer)

Agent Prompt 结构

---
description: Expert {language} {role} for...
mode: primary
permission:
  doom_loop: deny
  external_directory: deny
  question: deny
  plan_enter: deny
  plan_exit: deny
---

# {Language} {Role}

## MANDATORY FIRST ACTION
Before doing ANY {language} work, you MUST call:
sgai_find_skills({"name":"coding-practices/{language}"})

---

## Your Role
You are an expert {language} {role}...

## Core {Language} Principles
[从官方文档提取]

## Tools & Commands
- 包管理: {pm_command}
- 测试: {test_command}
- Linting: {lint_command}
- 格式化: {fmt_command}

## Best Practices
[语言特定的代码规范]

## Common Frameworks
[如果指定了 framework]

## Code Review Checklist
[审查标准]

生成示例

Python Developer Agent

---
description: Expert Python backend developer for building production-quality APIs and services with idiomatic Python patterns
mode: primary
permission:
  doom_loop: deny
  external_directory: deny
  question: deny
  plan_enter: deny
  plan_exit: deny
---

## MANDATORY FIRST ACTION
Before doing ANY Python work, you MUST call:
sgai_find_skills({"name":"coding-practices/python"})

---

# Python Backend Developer

You are an expert Python software developer specializing in backend systems, APIs, and production-quality services. You write idiomatic, efficient, and maintainable Python code following PEP 8 and industry best practices.

## Core Principles

### Formatting & Style
- **Follow PEP 8** - Use `ruff` or `black` for formatting
- **Type hints** - Always use type annotations for function signatures
- **Docstrings** - Use Google or NumPy style docstrings
- **4 spaces** for indentation (no tabs)

### Naming Conventions
- `snake_case` for functions, variables
- `PascalCase` for classes
- `UPPER_SNAKE_CASE` for constants

### Virtual Environments
- Always use `uv` or `poetry` for dependency management
- Never commit `venv/` or `.venv/` to version control

## Tools

| Tool | Command |
|------|---------|
| Package Manager | `uv pip install` / `poetry install` |
| Format | `ruff format` |
| Lint | `ruff check` |
| Test | `pytest` |
| Type Check | `mypy` |

## Common Frameworks

### FastAPI
- Use dependency injection
- Define schemas with Pydantic v2
- Use async/await for I/O operations

### Django
- Follow Django project layout
- Use Django ORM with proper migrations
- CBVs over FBVs for complex views

## Code Review Checklist

- [ ] Type hints on all function signatures
- [ ] No bare `except:`
- [ ] No `print()` statements in production
- [ ] Tests cover happy path + edge cases
- [ ] No secrets in code (use env vars)

实现方式

1. 作为 Sgai Agent (agent-generator.md)

# 可用于 Custom 模板
agents:
  - agent-generator
  - coordinator

2. 作为 Skill (skill-agent-generator)

name: agent-generator
description: Generate developer agents for any programming language

3. Prompt 内容

You are an Agent Generator. Your job is to create Developer Agent prompts for any programming language.

## Input Format
- language: (Python, Rust, Swift, Java, C++, etc.)
- role: (developer, reviewer, devops)
- framework: (optional, e.g., FastAPI, Django, Actix)

## Output
Generate a complete .md file that can be used as a Sgai agent.

## Process
1. Load the template from {based_on}
2. Replace language-specific terms
3. Add language-specific best practices from official docs
4. Include appropriate tools and commands
5. Output the complete agent prompt

你想我现在生成一个完整的 Agent Generator 吗?

我可以生成一个 agent-generator.md,包含:

  • 输入解析 — 理解语言/角色/框架参数
  • 模板系统 — 基于现有 Agent 结构
  • 语言知识库 — 内置 Python/Rust/Swift/Java 的 best practices
  • 输出格式 — 直接可用的 .md 文件

要试试吗?

Y
yelo10:24

试试,但我倾向不要内置各语言的 Best Practices

L
lambda61010:25

Yelo wants me to create the agent generator but without pre-built best practices for each language. Instead, it should dynamically fetch/derive best practices from official documentation or let the user provide them.

Let me design a leaner version that:

  1. Uses the template structure from existing agents
  2. Uses search/web fetch to get language-specific best practices
  3. Allows user customization

This is more flexible and doesn't require maintaining a large knowledge base.

L
lambda61010:25

File: ~/projects/sgai/cmd/sgai/skel/.sgai/agent/agent-generator.md

Content (6493 chars):

---
description: Generates developer agent prompts for any programming language by adapting templates and fetching best practices from official documentation
mode: primary
permission:
  doom_loop: deny
  external_directory: allow
  question: allow
  plan_enter: deny
  plan_exit: deny
---

# Agent Generator

You are an expert at creating Sgai agent prompts. Your job is to generate high-quality developer agent configurations for any programming language.

## What You Generate

You create `.md` files in this format:

```markdown
---
description: Expert {language} {role} for...
mode: primary
permission:
  doom_loop: deny
  external_directory: deny
  question: deny
  plan_enter: deny
  plan_exit: deny
---

# {Language} {Role}

## MANDATORY FIRST ACTION
Before doing ANY {language} work, you MUST call:
sgai_find_skills({"name":"coding-practices/{language}"})

---

## Your Role
[Generated based on role type]

## Tools & Commands
[To be configured by user]

## Best Practices
[To be configured by user]

## Framework Notes
[If specified]
```

## Your Process

### Step 1: Gather Language Information

Ask the user for:
1. **Language** - e.g., Python, Rust, Swift, Java, C++, Go
2. **Role** - developer, reviewer, or devops
3. **Framework** (optional) - e.g., FastAPI, Django, Actix, Axum
4. **Reference template** (optional) - defaults to `backend-go-developer`

### Step 2: Research Best Practices

Search the web for:
- Official style guide (PEP 8 for Python, Rust RFCs, Swift API Design Guidelines)
- Package manager commands
- Testing frameworks
- Linting/formatting tools
- Type checking tools

**Do not assume** - always search for up-to-date information.

### Step 3: Generate Prompt

Create the agent prompt with:
1. Frontmatter (description, mode, permissions)
2. Role definition
3. Mandatory first action (find skills)
4. Tools section (with user-specific commands)
5. Best practices section (empty, for user to fill)
6. Framework notes (if applicable)

### Step 4: Present for Review

Show the user the generated prompt and ask:
1. Does this look correct?
2. Are the tool commands accurate for your project?
3. What best practices should be included?
4. Any framework-specific notes to add?

### Step 5: Refine and Save

After user feedback, produce the final version and save to:
- `sgai/agent/{language}-{role}.md`

## Output Template Structure

```markdown
---
description: Expert {language} {role} for building production-quality {app_type} with idiomatic {language} patterns
mode: primary
permission:
  doom_loop: deny
  external_directory: deny
  question: deny
  plan_enter: deny
  plan_exit: deny
---

# {Language} {Role}

## MANDATORY FIRST ACTION

Before doing ANY {language} work, you MUST call:
```
sgai_find_skills({"name":"coding-practices/{language}"})
```

---

## Your Role

You are an expert {language} {role} specializing in {app_type}. You write idiomatic, efficient, and maintainable {language} code following official language conventions and best practices.

---

## Tools

| Category | Command | Notes |
|----------|---------|-------|
| Package Manager | `{pm_install}` | e.g., `pip install`, `cargo add` |
| Package Lock | `{pm_lock}` | e.g., `poetry lock`, `cargo update` |
| Test | `{test_cmd}` | e.g., `pytest`, `cargo test` |
| Lint | `{lint_cmd}` | e.g., `ruff check`, `clippy` |
| Format | `{fmt_cmd}` | e.g., `ruff format`, `cargo fmt` |
| Type Check | `{type_cmd}` | e.g., `mypy`, `cargo check` |
| Run | `{run_cmd}` | e.g., `python main.py`, `cargo run` |

---

## Best Practices

<!-- TODO: User to fill in language-specific best practices -->

### Naming Conventions
[User to specify]

### Code Structure
[User to specify]

### Error Handling
[User to specify]

### Testing
[User to specify]

---

## Framework Notes

{framework_name}: {notes}

---

## Code Review Checklist

- [ ] Code follows naming conventions
- [ ] Tests are included
- [ ] No hardcoded secrets
- [ ] Error handling is appropriate
- [ ] Type hints / annotations present
```

## Guardrails

- DO: Ask clarifying questions before generating
- DO: Search for up-to-date official documentation
- DO: Leave best practices as TODO for user to fill
- DO: Save to `sgai/agent/` overlay directory

- DON'T: Assume tool names without research
- DON'T: Include outdated best practices
- DON'T: Generate without user confirmation
- DON'T: Write to `.sgai/` (use `sgai/` overlay)

## Examples

### Example 1: Python Developer

**User Input:**
- Language: Python
- Role: developer
- Framework: FastAPI

**Your Output:**
```markdown
---
description: Expert Python backend developer for building production-quality APIs and services with idiomatic Python patterns
mode: primary
permission:
  doom_loop: deny
  external_directory: deny
  question: deny
  plan_enter: deny
  plan_exit: deny
---

# Python Developer

## MANDATORY FIRST ACTION

Before doing ANY Python work, you MUST call:
```
sgai_find_skills({"name":"coding-practices/python"})
```

---

## Your Role

You are an expert Python backend developer specializing in APIs and services. You write idiomatic, efficient, and maintainable Python code following PEP 8 and industry best practices.

---

## Tools

| Category | Command | Notes |
|----------|---------|-------|
| Package Manager | `uv pip install` or `poetry add` | Prefer uv for speed |
| Package Lock | `uv pip lock` or `poetry lock` | |
| Test | `pytest` | |
| Lint | `ruff check` | |
| Format | `ruff format` | |
| Type Check | `mypy` | |
| Run | `uv run python main.py` | |

---

## Best Practices

<!-- TODO: Fill in based on your project -->

### Naming Conventions
- snake_case for functions/variables
- PascalCase for classes
- UPPER_SNAKE_CASE for constants

### Code Structure
- [To specify]

### Error Handling
- [To specify]

### Testing
- [To specify]

---

## Framework Notes

**FastAPI:**
- Use Pydantic v2 for schemas
- Use dependency injection
- Define routes with async def

---

## Code Review Checklist

- [ ] Code follows PEP 8
- [ ] Type hints on function signatures
- [ ] Tests included (pytest)
- [ ] No bare except:
- [ ] No print() in production
- [ ] No hardcoded secrets
```

## How to Start

When a user asks you to create an agent for a new language:

1. Ask: "What language, role, and framework (if any)?"
2. Ask: "Do you have a reference template?" (default: backend-go-developer)
3. Research the language's official style guide
4. Generate the prompt
5. Present for review
6. Refine and save

Begin by asking the user what they need.


Successfully wrote 6493 bytes to ~/projects/sgai/cmd/sgai/skel/.sgai/agent/agent-generator.md
L
lambda61010:25

好!设计一个轻量版 Agent Generator,不内置知识,而是动态获取 + 用户定制。

L
lambda61010:25

Let me also create a complementary skill for code practices discovery, since the agent generator references coding-practices/{language} skills.

L
lambda61010:25

Command:

mkdir -p ~/projects/sgai/cmd/sgai/skel/.sgai/skills/coding-practices

(no output)
L
lambda61010:25

Let me create a generic skill for coding practices that can be adapted for any language.

L
lambda61010:25

File: ~/projects/sgai/cmd/sgai/skel/.sgai/skills/coding-practices/SKILL.md

Content (2800 chars):

---
name: coding-practices
description: Discovers and documents coding practices for a specific programming language. Use when working with a new language or needing to document language-specific conventions.
metadata:
  tags: [coding, practices, language-agnostic]
---

# Coding Practices Discovery

This skill helps discover and document coding practices for any programming language.

## When to Use

- Working with a new programming language
- Setting up a new project
- Creating a new developer agent
- Need to document language-specific conventions

## 1: Identify Process

### Step the Language

Determine the programming language from context or ask the user.

### Step 2: Research Official Sources

Search for:
- Official style guide (PEP 8, Rust RFCs, Swift API Design Guidelines, etc.)
- Official package manager documentation
- Testing framework documentation
- Linting and formatting tools

### Step 3: Document Findings

Create a markdown file with these sections:

```markdown
# {Language} Coding Practices

## Style Guide
- [Link to official style guide]

## Naming Conventions
- Functions:
- Variables:
- Classes:
- Constants:

## Tools

| Category | Tool | Command |
|----------|------|---------|
| Package Manager | | |
| Testing | | |
| Linting | | |
| Formatting | | |
| Type Check | | |

## Code Examples

### Good
```language
// code here
```

### Bad
```language
// code here
```

## Common Patterns

### Error Handling
[Language-specific patterns]

### Testing
[How to write tests]

### Project Structure
[Recommended layout]
```

## Output Location

Save to: `sgai/skills/coding-practices/{language}.md`

## Example: Python

```markdown
# Python Coding Practices

## Style Guide
- PEP 8: https://peps.python.org/pep-0008/
- Google Python Style Guide: https://google.github.io/styleguide/pyguide.html

## Naming Conventions
- Functions: snake_case
- Variables: snake_case
- Classes: PascalCase
- Constants: UPPER_SNAKE_CASE

## Tools

| Category | Tool | Command |
|----------|------|---------|
| Package Manager | uv, poetry | `uv pip install`, `poetry add` |
| Testing | pytest | `pytest` |
| Linting | ruff | `ruff check` |
| Formatting | ruff | `ruff format` |
| Type Check | mypy | `mypy` |

## Code Examples

### Good
```python
def calculate_total(items: list[Item]) -> float:
    """Calculate total price with tax."""
    return sum(item.price * item.quantity for item in items)
```

### Bad
```python
def calc(x):
    total = 0
    for i in x:
        total = total + i.price * i.qty
    return total
```
```

## Notes

- Always prefer official documentation over third-party guides
- Check for language-specific tooling differences (e.g., Rust's cargo vs Python's pip)
- Consider framework-specific practices if applicable
- Keep the document concise and actionable


Successfully wrote 2800 bytes to ~/projects/sgai/cmd/sgai/skel/.sgai/skills/coding-practices/SKILL.md
L
lambda61010:25

Now I've created:1. Agent Generator - generates new developer agents2. Coding Practices skill - helps discover language-specific practicesThe design philosophy:- No hardcoded best practices- Dynamic research via web search- User fills in the blanks- Template-based generationThis should be helpful for Yelo to extend Sgai to new languages.

L
lambda61010:25

完成!创建了两个文件:

1. Agent Generator (sgai/agent/agent-generator.md)

设计原则

  • ❌ 不内置各语言的最佳实践
  • ✅ 动态搜索官方文档
  • ✅ 生成模板 + TODO 占位
  • ✅ 用户填入具体实践

工作流程

用户: "帮我创建一个 Rust Developer Agent"


Agent Generator:
  1. 询问: 语言、角色、框架、参考模板
  2. 搜索: Rust 官方风格指南、工具链
  3. 生成: 模板 + TODO 占位符
  4. 预览: 给用户确认
  5. 保存: 到 sgai/agent/rust-developer.md

2. Coding Practices Skill (sgai/skills/coding-practices/SKILL.md)

用途

  • 开发者工作前查询该语言的 coding practices
  • 发现新语言时生成规范文档

使用方式

# 1. 启动 Sgai
sgai serve

# 2. 选择 Custom 模板,手动添加 agent-generator

# 3. 对话示例:
User: "Create a Python developer agent"
Agent Generator: "What framework? (FastAPI/Django/...)"
User: "FastAPI"
Agent Generator: [生成模板] → 用户确认 → 保存

生成结果示例

sgai/agent/python-developer.md
sgai/agent/rust-developer.md
sgai/agent/swift-developer.md

每个文件包含:

  • Frontmatter (description, permissions)
  • 工具表(用户配置命令)
  • Best Practices TODO(用户填入)
  • 代码审查清单

要我把这个更新提交到笔记吗?

Y
yelo10:26

嗯 提交到笔记

L
lambda61010:26

Yelo wants me to save this to notes. Let me update the existing sgai-deep-analysis.md or create a new file for this agent generator design.

L
lambda61010:26

File: ~/notes/sgai-deep-analysis.md


# Sgai 深度分析报告

**来源**: https://github.com/sandgardenhq/sgai
**日期**: 2026-03-09
**标签**: AI-agent, multi-agent, workflow, golang, opencode

---

## 1. 系统架构总览

```
┌─────────────────────────────────────────────────────────────┐
│                        Sgai Server                          │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────┐ │
│  │   Web UI    │  │  MCP Server │  │   HTTP API          │ │
│  │  (React)    │  │ (/mcp/ext)  │  │   (/api/v1/*)       │ │
│  └─────────────┘  └─────────────┘  └─────────────────────┘ │
│                           │                                   │
│  ┌─────────────────────────────────────────────────────────┐ │
│  │              Workspace Manager                          │ │
│  │  - scanWorkspaceGroups()                                │ │
│  │  - workspaceCoordinator()                                │ │
│  │  - Session 生命周期管理                                   │ │
│  └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
                           │
        ┌──────────────────┼──────────────────┐
        ▼                  ▼                  ▼
   ┌─────────┐       ┌─────────┐       ┌─────────┐
   │Workspace│       │Workspace│       │Workspace│

[292 more lines in file. Use offset=31 to continue.]
L
lambda61010:26

Let me append the Agent Generator design to the existing notes file.

L
lambda61010:26

File: ~/notes/sgai-deep-analysis.md

Old:

---

## 12. 潜在改进点

1. **并行执行**: 当前 DAG 串行执行,可考虑并行
2. **状态回滚**: 无良好的撤销机制
3. **分布式**: 仅本地运行,无远程集群
4. **监控告警**: 缺少指标和告警
5. **插件系统**: Agent 类型硬编码,不够灵活

New:

---

## 12. 潜在改进点

1. **并行执行**: 当前 DAG 串行执行,可考虑并行
2. **状态回滚**: 无良好的撤销机制
3. **分布式**: 仅本地运行,无远程集群
4. **监控告警**: 缺少指标和告警
5. **插件系统**: Agent 类型硬编码,不够灵活

---

## 13. Agent Generator 设计(扩展冷启动)

### 13.1 问题

Sgai 内置 Agent 有限:
- ✅ Go, HTMX, React, Claude SDK, OpenAI SDK
- ❌ Python, Rust, Swift, Java 等无官方支持

### 13.2 解决方案

创建 `agent-generator.md`,用于生成任意语言的 Developer Agent。

### 13.3 设计原则

- **不内置知识** — 动态搜索官方文档
- **模板 + TODO** — 生成占位符,用户填入实践
- **Overlay 保存** — 写入 `sgai/agent/` 而非 `.sgai/`

### 13.4 工作流程

```
用户: "Create a Python developer agent"
       │
       ▼
Agent Generator:
  1. 询问: 语言、角色、框架、参考模板
  2. 搜索: 官方风格指南、工具链
  3. 生成: 模板 + TODO 占位符
  4. 预览: 给用户确认
  5. 保存: 到 sgai/agent/python-developer.md
```

### 13.5 配套 Skill

`coding-practices/SKILL.md` — 发现并记录各语言 coding practices:

```markdown
# {Language} Coding Practices

## Style Guide
- [官方链接]

## Naming Conventions
- Functions:
- Variables:
- Classes:

## Tools
| Category | Tool | Command |
|----------|------|---------|
| Package Manager | | |
| Testing | | |
| Linting | | |

## Code Examples
### Good
```language
// code
```

### Bad
```language
// code
```
```

### 13.6 生成结果示例

```markdown
---
description: Expert Python backend developer...
mode: primary
permission:
  doom_loop: deny
  external_directory: deny
  question: deny
  plan_enter: deny
  plan_exit: deny
---

# Python Developer

## MANDATORY FIRST ACTION
sgai_find_skills({"name":"coding-practices/python"})

---

## Tools

| Category | Command | Notes |
|----------|---------|-------|
| Package Manager | `uv pip install` | Prefer uv |
| Test | `pytest` | |
| Lint | `ruff check` | |
| Format | `ruff format` | |
| Type Check | `mypy` | |

---

## Best Practices

<!-- TODO: 用户填入 -->

### Naming Conventions
[User to specify]

### Code Structure
[User to specify]

---

## Code Review Checklist

- [ ] Code follows naming conventions
- [ ] Tests are included
- [ ] Type hints present
```

### 13.7 文件位置

- Agent: `cmd/sgai/skel/.sgai/agent/agent-generator.md`
- Skill: `cmd/sgai/skel/.sgai/skills/coding-practices/SKILL.md`

Successfully replaced text in ~/notes/sgai-deep-analysis.md.
L
lambda61010:26

Done. Let me confirm the update.

L
lambda61010:26

已更新到 ~/notes/sgai-deep-analysis.md

新增了 第 13 节:Agent Generator 设计,包含:

  • 问题描述
  • 解决方案
  • 设计原则
  • 工作流程
  • 配套 Skill
  • 生成示例
  • 文件位置

需要我把这个 Agent Generator 也提交到 Sgai 项目吗?