kwcode-local-coding-agent
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseKWCode Local Coding Agent
KWCode 本地编码Agent
Skill by ara.so — Daily 2026 Skills collection.
KWCode is a CLI coding agent designed specifically for local open-source models (8B–30B parameters). Unlike cloud-first agents (Claude Code, Cursor), KWCode uses a deterministic expert pipeline where the LLM only classifies and generates — all routing, validation, and decision-making is handled by deterministic code. This lets small models succeed where they would otherwise hallucinate or loop.
由ara.so开发的技能 — 属于Daily 2026技能合集。
KWCode是一款专为本地开源模型(8B–30B参数)设计的CLI编码Agent。与优先适配云端的Agent(如Claude Code、Cursor)不同,KWCode采用确定性专家流程,LLM仅负责分类和生成任务,所有路由、验证和决策环节均由确定性代码处理。这让小模型能够完成原本会出现幻觉或循环的任务。
Installation
安装
bash
undefinedbash
undefinedStandard install
标准安装
pip install kwcode
pip install kwcode
China mirror (faster in mainland)
国内镜像(大陆地区更快)
pip install kwcode -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install kwcode -i https://pypi.tuna.tsinghua.edu.cn/simple
Optional: Cross-Encoder search reranking
可选:Cross-Encoder搜索重排序
pip install "kwcode[rerank]"
**Requirements:** Python 3.10+, any OpenAI-compatible API (local or cloud).
---pip install "kwcode[rerank]"
**系统要求:** Python 3.10+,任何兼容OpenAI的API(本地或云端)。
---Quick Start
快速开始
bash
undefinedbash
undefinedLaunch interactive REPL
启动交互式REPL
kwcode
On first launch, KWCode runs a setup wizard to configure your model connection.
---kwcode
首次启动时,KWCode会运行设置向导来配置模型连接。
---Configuration
配置
Connect to a local inference engine (Ollama example)
连接到本地推理引擎(Ollama示例)
bash
undefinedbash
undefinedInside kwcode REPL or as a slash command:
在kwcode REPL内或作为斜杠命令执行:
/api default http://localhost:11434/v1 ollama qwen3:8b
undefined/api default http://localhost:11434/v1 ollama qwen3:8b
undefinedConnect to a cloud API (no local GPU needed)
连接到云端API(无需本地GPU)
bash
/api default https://api.deepseek.com $DEEPSEEK_API_KEY deepseek-coder
/api default https://api.siliconflow.cn/v1 $SILICONFLOW_API_KEY Qwen/Qwen3-8Bbash
/api default https://api.deepseek.com $DEEPSEEK_API_KEY deepseek-coder
/api default https://api.siliconflow.cn/v1 $SILICONFLOW_API_KEY Qwen/Qwen3-8BEnvironment variable approach
环境变量配置方式
bash
export KWCODE_API_BASE=http://localhost:11434/v1
export KWCODE_API_KEY=ollama
export KWCODE_MODEL=qwen3:8b
kwcodebash
export KWCODE_API_BASE=http://localhost:11434/v1
export KWCODE_API_KEY=ollama
export KWCODE_MODEL=qwen3:8b
kwcodeRecommended models by VRAM
按显存推荐的模型
| VRAM | Model |
|---|---|
| 4 GB | |
| 8 GB | |
| 16 GB | |
| 24 GB+ | |
| 显存 | 模型 |
|---|---|
| 4 GB | |
| 8 GB | |
| 16 GB | |
| 24 GB+ | |
Key Commands
核心命令
All commands are entered inside the REPL ( prompt).
kwcode>所有命令需在 REPL( 提示符)内输入。
kwcode>Core task commands
核心任务命令
> <natural language task description>> 修复登录验证失败的问题
> write a FastAPI login endpoint with JWT auth
> refactor calculate_price into smaller functions> <自然语言任务描述>> 修复登录验证失败的问题
> write a FastAPI login endpoint with JWT auth
> refactor calculate_price into smaller functionsPlanning & safety
规划与安全
> /plan <task description>Shows execution steps and risk level (High / Medium / Low) before touching any file. Requires confirmation.
> /checkpointManually snapshot current project state. KWCode also auto-snapshots before each task.
> /rollbackRestore to the last checkpoint if a task goes wrong.
> /plan <任务描述>在修改任何文件前,展示执行步骤和风险等级(高/中/低),需确认后执行。
> /checkpoint手动快照当前项目状态。KWCode也会在每个任务前自动创建快照。
> /rollback若任务执行出错,恢复到上一个检查点状态。
Multi-task orchestration (DAG)
多任务编排(DAG)
> /multi task1 ; task2 ; task3 # all parallel
> /multi task1 -> task2 -> task3 # serial chain
> /multi # interactive builderInteractive multi-task example:
> /multi
+ add docstring to function `add` (parallel)
+ add docstring to function `sub` (parallel)
+ >write tests for the modified code (serial, depends on above two)> /multi task1 ; task2 ; task3 # 全部并行执行
> /multi task1 -> task2 -> task3 # 串行链式执行
> /multi # 交互式构建器交互式多任务示例:
> /multi
+ 为函数`add`添加文档字符串 (并行)
+ 为函数`sub`添加文档字符串 (并行)
+ >为修改后的代码编写测试用例 (串行,依赖前两项)Search enhancement
搜索增强
bash
undefinedbash
undefinedInstall SearXNG (requires Docker Desktop running)
安装SearXNG(需Docker Desktop运行)
kwcode setup-search
undefined/search <query> # explicit search inside REPL
Without Docker, KWCode falls back to DuckDuckGo automatically.kwcode setup-search
undefined/search <查询内容> # 在REPL内执行显式搜索
若未安装Docker,KWCode会自动回退到DuckDuckGo搜索。Statistics & flywheel
统计与飞轮机制
bash
kwcode stats # CLI command (outside REPL)Shows tasks completed, estimated time saved, and flywheel expert promotions.
bash
kwcode stats # CLI命令(在REPL外执行)展示已完成任务、预估节省时间以及飞轮机制的专家晋升情况。
API management
API管理
> /api list # show configured endpoints
> /api default <base_url> <key> [model] # set default endpoint
> /api add <name> <base_url> <key> # add named endpoint
> /api use <name> # switch active endpoint> /api list # 展示已配置的端点
> /api default <base_url> <key> [model] # 设置默认端点
> /api add <name> <base_url> <key> # 添加命名端点
> /api use <name> # 切换到指定端点Project Configuration Files
项目配置文件
KWCode looks for these files in your project root and injects them as context:
| File | Purpose |
|---|---|
| Project-level rules, conventions, coding standards. Injected per task type. |
| Auto-maintained project summary (Layer 1 memory). |
| Domain expert knowledge accumulated by the flywheel (Layer 2). |
| Recurring code patterns learned from your project (Layer 3). |
| Structured log of past failures and lessons (auto-updated). |
KWCode会在项目根目录查找以下文件,并将其作为上下文注入:
| 文件 | 用途 |
|---|---|
| 项目级规则、约定、编码规范,按任务类型注入上下文。 |
| 自动维护的项目摘要(第一层记忆)。 |
| 飞轮机制积累的领域专家知识(第二层记忆)。 |
| 从项目中学习到的重复代码模式(第三层记忆)。 |
| 结构化的过往失败记录与经验总结(自动更新)。 |
Example KWCODE.md
KWCODE.mdKWCODE.md
示例
KWCODE.mdmarkdown
undefinedmarkdown
undefinedProject Rules
项目规则
bugfix
bug修复
- Always run after any fix
pytest tests/ - Never modify migration files directly
- 修复后必须运行
pytest tests/ - 禁止直接修改迁移文件
codegen
代码生成
- Use for all new route handlers
async def - Import order: stdlib → third-party → local
- 所有新路由处理函数使用
async def - 导入顺序:标准库 → 第三方库 → 本地模块
general
通用规则
- Line length: 88 (black default)
- All new functions must have type hints
---- 行长度:88(black默认值)
- 所有新函数必须包含类型提示
---How the Expert Pipeline Works
专家流程工作原理
Every user input flows through five deterministic stages:
Input
└─► Gate — classifies task, routes to skill, matches domain knowledge
└─► Locator — BM25 keyword recall + AST call-graph expansion (no LLM)
└─► Generator — generates only the changed diff, injects SKILL.md
└─► Verifier — syntax check + pytest (deterministic)
└─► Debugger — sys.settrace captures live variable values
└─► Reviewer — LLM checks intent vs actual changeThe LLM is only called at Generator and Reviewer stages. Everything else is deterministic Python.
用户输入会经过五个确定性阶段:
输入
└─► 网关 — 分类任务、路由到对应技能、匹配领域知识
└─► 定位器 — BM25关键词召回 + AST调用图扩展(无LLM参与)
└─► 生成器 — 仅生成变更的diff,注入SKILL.md
└─► 验证器 — 语法检查 + pytest(确定性)
└─► 调试器 — 通过sys.settrace捕获实时变量值
└─► 审核器 — LLM检查意图与实际变更是否匹配LLM仅在生成器和审核器阶段被调用,其余环节均由确定性Python代码处理。
Code Examples
代码示例
Trigger a bugfix task
触发bug修复任务
python
undefinedpython
undefinedKWCode detects "bug", "fix", "error", "失败" → routes to BugFix expert
KWCode检测到"bug"、"fix"、"error"、"失败"等关键词 → 路由到BugFix专家
Inside REPL:
在REPL内执行:
> fix the KeyError in user_service.py when email is missing
> fix the KeyError in user_service.py when email is missing
KWCode will:
1. BM25-locate `user_service.py` + trace call graph for hidden dependencies
2. Generate a minimal patch (only changed lines)
3. Run `pytest` automatically
4. If failing: inject runtime variable values via `sys.settrace` and retry
KWCode会执行以下操作:
1. 通过BM25定位`user_service.py` + 追踪调用图查找隐藏依赖
2. 生成最小化补丁(仅包含变更行)
3. 自动运行`pytest`
4. 若测试失败:通过`sys.settrace`注入运行时变量值并重试Trigger a test generation task
触发测试生成任务
> generate pytest tests for the PaymentProcessor classKWCode injects the SKILL.md, locates via AST, generates tests, and verifies they pass.
TestGenPaymentProcessor> generate pytest tests for the PaymentProcessor classKWCode会注入 SKILL.md,通过AST定位,生成测试用例并验证其通过。
TestGenPaymentProcessorUse the Python API (programmatic access)
使用Python API(程序化调用)
python
from kwcode import KWCodeAgent
agent = KWCodeAgent(
api_base="http://localhost:11434/v1",
api_key="ollama",
model="qwen3:8b",
project_dir="/path/to/your/project",
)
result = agent.run("fix the login validation bug")
print(result.status) # "success" | "failed" | "rolled_back"
print(result.files_changed) # list of modified file paths
print(result.patch) # unified diff stringpython
from kwcode import KWCodeAgent
agent = KWCodeAgent(
api_base="http://localhost:11434/v1",
api_key="ollama",
model="qwen3:8b",
project_dir="/path/to/your/project",
)
result = agent.run("fix the login validation bug")
print(result.status) # "success" | "failed" | "rolled_back"
print(result.files_changed) # 修改文件路径列表
print(result.patch) # 统一diff字符串Multi-task via Python API
通过Python API执行多任务
python
from kwcode import KWCodeAgent, TaskGraph
agent = KWCodeAgent(model="qwen3:14b", project_dir=".")
graph = TaskGraph()
t1 = graph.add("add type hints to utils.py")
t2 = graph.add("add type hints to models.py")
t3 = graph.add("write tests for typed functions", depends_on=[t1, t2])
results = agent.run_graph(graph)
for task_id, result in results.items():
print(f"{task_id}: {result.status}")python
from kwcode import KWCodeAgent, TaskGraph
agent = KWCodeAgent(model="qwen3:14b", project_dir=".")
graph = TaskGraph()
t1 = graph.add("为utils.py添加类型提示")
t2 = graph.add("为models.py添加类型提示")
t3 = graph.add("为带类型提示的函数编写测试用例", depends_on=[t1, t2])
results = agent.run_graph(graph)
for task_id, result in results.items():
print(f"{task_id}: {result.status}")Three-Stage Retry Logic
三级重试逻辑
When fails, KWCode does not repeat the same attempt:
Verifier| Attempt | Strategy |
|---|---|
| 1st | Normal task description sent to Generator |
| 2nd | Error message + |
| 3rd | Minimal-change constraint enforced; Debug Subagent provides full |
After 3 failures, task is marked failed and rolled back to checkpoint.
当失败时,KWCode不会重复相同尝试:
验证器| 尝试次数 | 策略 |
|---|---|
| 第1次 | 将常规任务描述发送给生成器 |
| 第2次 | 注入错误信息 + |
| 第3次 | 强制执行最小变更约束;调试子Agent提供完整的 |
3次尝试失败后,任务标记为失败并回退到检查点。
Troubleshooting
故障排查
Model returns garbled output or loops
模型返回乱码或循环输出
/api default <your_base_url> <key> <model> # re-confirm model name matches serverCheck that your local inference engine is running:
bash
curl http://localhost:11434/v1/models # Ollama
curl http://localhost:8080/v1/models # llama.cpp / LM Studio/api default <your_base_url> <key> <model> # 确认模型名称与服务器匹配检查本地推理引擎是否运行:
bash
curl http://localhost:11434/v1/models # Ollama
curl http://localhost:8080/v1/models # llama.cpp / LM StudioKWCode can't find the right file
KWCode无法找到正确文件
BM25 needs indexed content. If your project is new:
> /index # force re-index project filesIf the file uses an unusual extension, add it to :
KWCODE.mdmarkdown
undefinedBM25需要索引内容。若项目是新建的:
> /index # 强制重新索引项目文件若文件使用非常规扩展名,可添加到:
KWCODE.mdmarkdown
undefinedindex
index
- include: [".pyx", ".pxd", "*.proto"]
undefined- include: [".pyx", ".pxd", "*.proto"]
undefinedpytest not found / tests not running
pytest未找到/测试未运行
> /config verifier.test_cmd "python -m pytest tests/ -x"Or set in :
KWCODE.mdmarkdown
undefined> /config verifier.test_cmd "python -m pytest tests/ -x"或在中设置:
KWCODE.mdmarkdown
undefinedverifier
verifier
- test_cmd: python -m pytest tests/ -x --tb=short
undefined- test_cmd: python -m pytest tests/ -x --tb=short
undefinedSearch not working (DuckDuckGo blocked)
搜索无法工作(DuckDuckGo被屏蔽)
bash
undefinedbash
undefinedInstall local SearXNG (requires Docker)
安装本地SearXNG(需Docker)
kwcode setup-search
kwcode setup-search
Verify it's running
验证是否运行
undefinedundefinedContext window overflow with large projects
大型项目上下文窗口溢出
KWCode auto-compresses context when it approaches the limit, but you can tune aggressiveness:
> /config context.compression_ratio 0.6 # keep 60% of mid-conversation history
> /config locator.max_files 3 # limit files sent to Generator当接近上下文限制时,KWCode会自动压缩上下文,你也可以调整压缩程度:
> /config context.compression_ratio 0.6 # 保留60%的对话历史
> /config locator.max_files 3 # 限制发送给生成器的文件数量Flywheel expert not promoting
飞轮机制专家未晋升
Promotion requires: ≥5 successes of the same task type → backtest pass rate ≥ baseline → 10-run A/B test with >10% improvement. Check flywheel status:
kwcode stats --flywheel晋升需要满足:≥5次相同任务类型的成功 → 回溯通过率≥基准线 → 10次A/B测试提升率>10%。查看飞轮状态:
kwcode stats --flywheelSkill Domains (SKILL.md Library)
技能领域(SKILL.md 库)
KWCode ships with 15 built-in domain skills injected at Generator stage:
| Skill | Triggers |
|---|---|
| fix, bug, error, crash, 修复 |
| fastapi, route, endpoint, async |
| test, pytest, unittest, 测试 |
| api, rest, http, request |
| deepseek, r1, v3 |
| docstring, document, 注释 |
| mybatis, mapper, xml, sql |
| excel, word, ppt, spreadsheet |
| refactor, clean, extract, 重构 |
| spring, springboot, java |
| sql, query, optimize, index |
| type hint, annotation, mypy |
| uniapp, vue, miniprogram |
Custom skills can be added by placing a file in your project root or .
SKILL_<name>.md~/.kwcode/skills/KWCode内置15个领域技能,会在生成器阶段注入:
| 技能 | 触发词 |
|---|---|
| fix, bug, error, crash, 修复 |
| fastapi, route, endpoint, async |
| test, pytest, unittest, 测试 |
| api, rest, http, request |
| deepseek, r1, v3 |
| docstring, document, 注释 |
| mybatis, mapper, xml, sql |
| excel, word, ppt, spreadsheet |
| refactor, clean, extract, 重构 |
| spring, springboot, java |
| sql, query, optimize, index |
| type hint, annotation, mypy |
| uniapp, vue, miniprogram |
自定义技能可通过在项目根目录或放置文件添加。
~/.kwcode/skills/SKILL_<name>.mdData & Privacy
数据与隐私
- All processing is local. No code, file contents, or task descriptions leave your machine.
- Model inference: your local engine or your chosen cloud API endpoint only.
- Search: SearXNG self-hosted (recommended) or DuckDuckGo (queries only, no code).
- Statistics: stored in (SQLite, local only).
~/.kwcode/stats.db - Reflection/memory files: written to your project directory, fully under your control.
- 所有处理均在本地完成,代码、文件内容或任务描述不会离开你的设备。
- 模型推理:仅使用你的本地引擎或你选择的云端API端点。
- 搜索:推荐使用自托管SearXNG,或使用DuckDuckGo(仅发送查询内容,无代码)。
- 统计数据:存储在(SQLite,仅本地存储)。
~/.kwcode/stats.db - 反思/记忆文件:写入你的项目目录,完全由你掌控。