opencode-growth 0.1.2 → 0.1.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -13
- package/package.json +1 -1
- package/src/adapter-opencode/curator-runner.mjs +257 -0
- package/src/adapter-opencode/index.mjs +309 -19
- package/src/adapter-opencode/install.mjs +329 -52
- package/src/adapter-opencode/journey-tool.mjs +57 -0
- package/src/adapter-opencode/memory-tool.mjs +57 -5
- package/src/adapter-opencode/provider-tools.mjs +237 -0
- package/src/adapter-opencode/review-runner.mjs +180 -37
- package/src/adapter-opencode/session-search-tool.mjs +66 -0
- package/src/adapter-opencode/skill-tool.mjs +221 -0
- package/src/cli/commands.mjs +98 -5
- package/src/cli/curator-cmd.mjs +149 -0
- package/src/cli/journey-cmd.mjs +61 -0
- package/src/cli/journey-view.mjs +174 -0
- package/src/cli/main.mjs +173 -26
- package/src/cli/memory-cmd.mjs +165 -0
- package/src/cli/session-index-cmd.mjs +87 -0
- package/src/cli/skill-cmd.mjs +194 -0
- package/src/core/approve.mjs +295 -0
- package/src/core/audit.mjs +51 -0
- package/src/core/background-review.mjs +114 -2
- package/src/core/home.mjs +56 -21
- package/src/core/journey.mjs +298 -0
- package/src/core/lock.mjs +6 -0
- package/src/core/memories.mjs +215 -10
- package/src/core/memory-actions.mjs +73 -18
- package/src/core/paths.mjs +0 -1
- package/src/core/session-freshness.mjs +201 -0
- package/src/core/session-index.mjs +508 -0
- package/src/core/session-query.mjs +161 -0
- package/src/core/session-source.mjs +209 -0
- package/src/core/session-tool.mjs +189 -0
- package/src/core/skill-actions.mjs +391 -0
- package/src/core/skill-backup.mjs +95 -0
- package/src/core/skill-lifecycle.mjs +277 -0
- package/src/core/skill-usage.mjs +119 -0
- package/src/core/skills.mjs +30 -3
- package/src/core/sqlite-runtime.mjs +35 -0
- package/src/core/write-approval.mjs +13 -9
- package/src/host/rest-client.mjs +70 -0
- package/src/index.mjs +0 -1
- package/src/provider/config.mjs +187 -0
- package/src/provider/openviking-rest.mjs +742 -0
- package/src/provider/runtime.mjs +417 -0
- package/src/provider/wiring.mjs +164 -0
- package/src/core/budget.mjs +0 -64
- package/src/core/retrieve.mjs +0 -127
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
// 技能写路径(S1 形成面):`skill` 工具面的动作层。
|
|
2
|
+
//
|
|
3
|
+
// 形制照两处抄(对齐优先=默认动作):
|
|
4
|
+
// · Hermes `tools/skill_manager_tool.py`:name/description/正文校验(`_validate_name` /
|
|
5
|
+
// `_validate_frontmatter` / `_validate_content_size`)、create 撞名报 already exists、
|
|
6
|
+
// patch=old_string→new_string 的定点编辑、写入前安全扫描(`_security_scan_skill`)、
|
|
7
|
+
// description 的 system-prompt 预算 `SKILL_PROMPT_DESC_LIMIT=60`(`agent/skill_utils.py:763`)。
|
|
8
|
+
// · 本仓记忆写路径 `memory-actions.mjs`:动作**封闭枚举**、身份不由模型自报、
|
|
9
|
+
// `decideWrite` 闸门分流(闸关直写 D73)、暂存进**同一**待批队列、每条结果都带可读回执。
|
|
10
|
+
//
|
|
11
|
+
// 与记忆面的两条差异(都是已定 D 决策,不是自造):
|
|
12
|
+
// 1. 盘上形态是**目录树** `skills/<name>/SKILL.md`——宿主发现契约是硬的(递归扫 `**/SKILL.md`、
|
|
13
|
+
// name=目录名、缺 description 静默消失;60- §7.1)。记忆是单文件 § 条目,技能不是。
|
|
14
|
+
// 2. **patch 不算破坏性**(W8 技能面裁定,60- §5.1.1):回合后通道直写 agent-created 的 patch;
|
|
15
|
+
// 归档=自动直写(2026-09-25 修正:归档≠删除,照 Hermes curator 自动归档、跑前快照兑底)。
|
|
16
|
+
// ⇒ decideWrite 的 destructive 恒 false(真删除动作不存在)。
|
|
17
|
+
//
|
|
18
|
+
// 认账回执(§6 规则①):成功与失败都返回 `receipt`,成功带宿主要核对的 name+location,
|
|
19
|
+
// 失败如实报「未生效 / 冲突 / 待批」——**绝不误报「已交付」**(§6 规则⑤ 三夹具的靶子)。
|
|
20
|
+
import fs from "node:fs"
|
|
21
|
+
import path from "node:path"
|
|
22
|
+
|
|
23
|
+
import { appendAudit } from "./audit.mjs"
|
|
24
|
+
import { sha256 } from "./ids.mjs"
|
|
25
|
+
import { layout } from "./paths.mjs"
|
|
26
|
+
import {
|
|
27
|
+
checkSkillLayout,
|
|
28
|
+
skillsDir,
|
|
29
|
+
SKILL_FILE,
|
|
30
|
+
NAME_RE,
|
|
31
|
+
NAME_MAX,
|
|
32
|
+
listSkillsIn,
|
|
33
|
+
readExternalDirs,
|
|
34
|
+
readCreatedBy,
|
|
35
|
+
} from "./skills.mjs"
|
|
36
|
+
import { createMemoryStore } from "./memories.mjs"
|
|
37
|
+
import { decideWrite } from "./write-approval.mjs"
|
|
38
|
+
import { scanForThreats } from "./threats.mjs"
|
|
39
|
+
import { readUsage, recordSkillCreated, bumpSkillPatch, bumpSkillView, updateRecord } from "./skill-usage.mjs"
|
|
40
|
+
import { archiveSkill } from "./skill-lifecycle.mjs"
|
|
41
|
+
|
|
42
|
+
/** 动作是封闭枚举:加动作=改工具契约(照 memory-actions 的 extra#4 纪律,无 approve/apply)。 */
|
|
43
|
+
export const SKILL_ACTIONS = ["create", "patch", "view", "list", "archive"]
|
|
44
|
+
|
|
45
|
+
/** description 预算:照 Hermes `SKILL_PROMPT_DESC_LIMIT`(新技能不许吃掉索引的路由信号)。 */
|
|
46
|
+
export const MAX_DESCRIPTION_LENGTH = 60
|
|
47
|
+
/** 正文上限:照 Hermes `MAX_SKILL_CONTENT_CHARS`。 */
|
|
48
|
+
export const MAX_CONTENT_CHARS = 100_000
|
|
49
|
+
|
|
50
|
+
const skillFileOf = (root, name) => path.join(skillsDir(root), name, SKILL_FILE)
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 从**宿主配置**(opencode.json 解析结果)推技能面的两条宿主闸——
|
|
54
|
+
* §6 规则⑤ 反例三夹具的前两夹具就在这儿判:
|
|
55
|
+
* · `permission.skill.<name>=deny`(嵌套 `permission.skill` 是 map,或扁平键 `permission["skill.<name>"]`)
|
|
56
|
+
* · 目标 agent `tools.skill:false`(`agent.<agent>.tools.skill`,回退全局 `tools.skill`)
|
|
57
|
+
* 读不到一律按「宿主闸全开」——写面只管写;宿主真禁了就在认账要素里如实判「未生效」。
|
|
58
|
+
*/
|
|
59
|
+
export function skillGateFromHostConfig({ hostConfig = {}, agent = null, name = "" } = {}) {
|
|
60
|
+
const perm = hostConfig?.permission ?? {}
|
|
61
|
+
let hostPermission = "allow"
|
|
62
|
+
const skillPerm = perm?.skill
|
|
63
|
+
if (typeof skillPerm === "string") hostPermission = skillPerm
|
|
64
|
+
else if (skillPerm && typeof skillPerm === "object" && name && typeof skillPerm[name] === "string") {
|
|
65
|
+
hostPermission = skillPerm[name]
|
|
66
|
+
}
|
|
67
|
+
const flat = name ? perm?.[`skill.${name}`] : undefined
|
|
68
|
+
if (typeof flat === "string") hostPermission = flat
|
|
69
|
+
|
|
70
|
+
let agentSkillEnabled = true
|
|
71
|
+
const agentCfg = agent ? hostConfig?.agent?.[agent] : null
|
|
72
|
+
const tools = agentCfg?.tools ?? hostConfig?.tools
|
|
73
|
+
if (tools && typeof tools === "object" && tools.skill === false) agentSkillEnabled = false
|
|
74
|
+
|
|
75
|
+
return { hostPermission: hostPermission === "deny" ? "deny" : "allow", agentSkillEnabled }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function receipt(status, { name = null, location = null, detail = "", createdBy = null } = {}) {
|
|
79
|
+
return { status, name, location, detail, createdBy }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function fail(action, error, rc = {}) {
|
|
83
|
+
return { ok: false, action, error, receipt: receipt("未生效", rc) }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function validateName(name) {
|
|
87
|
+
if (typeof name !== "string" || !name.trim()) return "技能名必填"
|
|
88
|
+
if (!NAME_RE.test(name)) return `技能名不合宿主契约(小写连字符、字母或数字开头):${name}`
|
|
89
|
+
if (name.length > NAME_MAX) return `技能名超过 ${NAME_MAX} 字符`
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** description 形制照 Hermes:触发句开头、一句行为、句号收尾(`skill_manager_tool.py:828`)。 */
|
|
94
|
+
function validateDescription(description) {
|
|
95
|
+
if (typeof description !== "string" || !description.trim()) return "缺 description:宿主会把没有 description 的技能**静默过滤**,模型永远看不到它"
|
|
96
|
+
if (description.length > MAX_DESCRIPTION_LENGTH) {
|
|
97
|
+
return `description ${description.length} 字符,超出 ${MAX_DESCRIPTION_LENGTH}:索引会截断并毁掉路由信号,细节请放进正文`
|
|
98
|
+
}
|
|
99
|
+
if (!description.startsWith("Use when ")) return "description 必须以 `Use when ` 开头(触发句先行)"
|
|
100
|
+
if (!description.endsWith(".")) return "description 要以句号收尾"
|
|
101
|
+
if (description.includes("\n")) return "description 只能一句(不许换行)"
|
|
102
|
+
return null
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function validateContent(content) {
|
|
106
|
+
if (typeof content !== "string" || !content.trim()) return "正文不能为空(instructions / procedures)"
|
|
107
|
+
if (content.length > MAX_CONTENT_CHARS) return `正文 ${content.length} 字符,超出 ${MAX_CONTENT_CHARS}:拆进 references/`
|
|
108
|
+
return null
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
/** 外部来源是否已占用这个名字(跨全局-项目同名 ⇒ 冲突;外部目录只读)。 */
|
|
113
|
+
function externalHit(name, externalDirs = []) {
|
|
114
|
+
for (const d of externalDirs) {
|
|
115
|
+
if (!d) continue
|
|
116
|
+
if (fs.existsSync(path.join(d, name, SKILL_FILE))) return path.join(d, name, SKILL_FILE)
|
|
117
|
+
}
|
|
118
|
+
return null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 跑一个技能动作。返回结构化结果(不抛):成功与失败都带认账 `receipt`。
|
|
123
|
+
*
|
|
124
|
+
* @param {object} o
|
|
125
|
+
* @param {string} o.root D 目录
|
|
126
|
+
* @param {object} [o.identity] {origin, agent}——宿主给的,不是模型报的
|
|
127
|
+
* @param {string} o.action SKILL_ACTIONS 之一
|
|
128
|
+
* @param {string} [o.name] 技能名
|
|
129
|
+
* @param {string} [o.description] create:`Use when …` 一句
|
|
130
|
+
* @param {string} [o.content] create:正文
|
|
131
|
+
* @param {string} [o.old_string] patch:要改的原文(唯一命中)
|
|
132
|
+
* @param {string} [o.new_string] patch:改成什么
|
|
133
|
+
* @param {Function} [o.ask] 交互通道(闸开 ask 档才用)
|
|
134
|
+
* @param {string} [o.hostPermission] 宿主权限闸("deny" ⇒ 未生效)
|
|
135
|
+
* @param {boolean} [o.agentSkillEnabled] 目标 agent 的 tools.skill(false ⇒ 未生效)
|
|
136
|
+
* @param {string[]} [o.externalDirs] 外部技能来源(只读;同名 ⇒ 冲突)
|
|
137
|
+
*/
|
|
138
|
+
export async function runSkillAction({
|
|
139
|
+
root,
|
|
140
|
+
identity = null,
|
|
141
|
+
action,
|
|
142
|
+
name = null,
|
|
143
|
+
description = null,
|
|
144
|
+
content = null,
|
|
145
|
+
old_string = null,
|
|
146
|
+
new_string = null,
|
|
147
|
+
ask = null,
|
|
148
|
+
hostPermission = null,
|
|
149
|
+
agentSkillEnabled = true,
|
|
150
|
+
externalDirs = undefined,
|
|
151
|
+
absorbedInto = null,
|
|
152
|
+
approved = null, // { by } ⇒ 人已经批准过这条提案,跳过写入门控(宿主 deny 仍然压过)
|
|
153
|
+
} = {}) {
|
|
154
|
+
if (!root) return fail(action, "缺少 root(D 目录)")
|
|
155
|
+
if (!SKILL_ACTIONS.includes(action)) {
|
|
156
|
+
return fail(action, `未知动作:${String(action)}。可用动作:${SKILL_ACTIONS.join(" / ")}`)
|
|
157
|
+
}
|
|
158
|
+
// 外部来源:调用方没给就按 store 配置(D/config.json 的 skills.external_dirs)认,没配就是没有。
|
|
159
|
+
const externals = Array.isArray(externalDirs) ? externalDirs : readExternalDirs(root)
|
|
160
|
+
|
|
161
|
+
// createdBy 忠实于文件 frontmatter(保护三分的 owner 口径,与门的 readCreatedBy 同源);
|
|
162
|
+
// create 时文件尚不存在 ⇒ 记动作方(background ⇒ agent,否则 user)。2026-09-25 修:
|
|
163
|
+
// 旧实现恒按身份推导,前台动 agent-created 技能会在回执/审计里误记 user(真机审计实证)。
|
|
164
|
+
const actorMark = identity?.origin === "background" ? "agent" : "user"
|
|
165
|
+
const namedFile = name ? skillFileOf(root, String(name)) : null
|
|
166
|
+
const createdBy = (namedFile && fs.existsSync(namedFile) ? readCreatedBy(namedFile) : null) || actorMark
|
|
167
|
+
const origin = identity?.origin ?? "unknown"
|
|
168
|
+
|
|
169
|
+
if (action === "list") {
|
|
170
|
+
const names = listSkillsIn(skillsDir(root)) ?? []
|
|
171
|
+
const skills = names.map((n) => {
|
|
172
|
+
const file = skillFileOf(root, n)
|
|
173
|
+
const raw = fs.readFileSync(file, "utf8")
|
|
174
|
+
const dm = /^description:\s*(.+)$/m.exec(raw)
|
|
175
|
+
return { name: n, description: dm ? dm[1].trim() : null, location: file, source: "local", createdBy: readCreatedBy(file) }
|
|
176
|
+
})
|
|
177
|
+
return { ok: true, action, skills }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (action === "view") {
|
|
181
|
+
// 查看=只读动作(照 Hermes `skill_view` + `_skill_view_with_bump`):回正文并记一笔 view。
|
|
182
|
+
if (!name) return fail(action, "view 需要 name", { name })
|
|
183
|
+
const nameErrForView = validateName(name)
|
|
184
|
+
if (nameErrForView) return fail(action, nameErrForView, { name })
|
|
185
|
+
const viewFile = skillFileOf(root, name)
|
|
186
|
+
if (!fs.existsSync(viewFile)) return fail(action, `技能不存在:${name}`, { name })
|
|
187
|
+
const content = fs.readFileSync(viewFile, "utf8")
|
|
188
|
+
bumpSkillView(root, name) // best-effort 记账(账本坏了绝不炸查看)
|
|
189
|
+
return {
|
|
190
|
+
ok: true,
|
|
191
|
+
action,
|
|
192
|
+
name,
|
|
193
|
+
location: viewFile,
|
|
194
|
+
content,
|
|
195
|
+
receipt: receipt("已交付", { name, location: viewFile, createdBy: readCreatedBy(viewFile), detail: "查看技能" }),
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ── 反例三夹具(§6 规则⑤):认账前提不成立 ⇒ 如实报未生效/冲突,绝不落盘 ──────
|
|
200
|
+
// 夹具一:宿主权限闸 deny。压过其余一切(照 decideWrite 的优先级链)。
|
|
201
|
+
if (hostPermission === "deny") {
|
|
202
|
+
return { ok: false, action, error: "宿主权限闸拒绝了这次写入", receipt: receipt("未生效", { name, detail: "permission deny" }) }
|
|
203
|
+
}
|
|
204
|
+
// 夹具二:目标 agent 禁 skill(tools.skill=false)⇒ 技能写了也没人能用。
|
|
205
|
+
if (agentSkillEnabled === false) {
|
|
206
|
+
return { ok: false, action, error: "目标 agent 禁用 skill 工具,技能不会生效", receipt: receipt("未生效", { name, detail: "tools.skill=false" }) }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const nameErr = validateName(name)
|
|
210
|
+
if (nameErr) return fail(action, nameErr, { name })
|
|
211
|
+
const location = skillFileOf(root, name)
|
|
212
|
+
|
|
213
|
+
// 夹具三:跨全局-项目同名(外部来源已有同名)⇒ 冲突(本地写入会把外部那份静默压掉)。
|
|
214
|
+
const ext = externalHit(name, externals)
|
|
215
|
+
if (ext && action === "create") {
|
|
216
|
+
return { ok: false, action, error: `外部来源已有同名技能(${ext}),本地再建会把它静默压掉`, receipt: receipt("冲突", { name, location: ext, detail: "跨来源同名" }) }
|
|
217
|
+
}
|
|
218
|
+
if (ext && !fs.existsSync(location)) {
|
|
219
|
+
// 写面碰外部技能=只读保护(片 4 判据「写面碰外部技能必须红」,防线从写路径第一天就立)
|
|
220
|
+
return { ok: false, action, error: `外部来源技能只读:${ext}`, receipt: receipt("冲突", { name, location: ext, detail: "external 只读" }) }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// ── S4 归档动词(照 Hermes curator 的 action=delete 实为 archive):absorbed_into 强制 ──
|
|
224
|
+
// 「MUST pass absorbed_into=<umbrella>…Deletes without a verified forwarding target are
|
|
225
|
+
// refused — pruning with no absorption target is the deterministic staleness pass's job,
|
|
226
|
+
// never this one's.」命名 D 决策:动作名取行为本义=archive(只搬不删)。
|
|
227
|
+
if (action === "archive") {
|
|
228
|
+
if (!absorbedInto) return fail(action, "archive 需要 absorbed_into=<伞技能名>(合并去向必须点名)", { name })
|
|
229
|
+
const umbrellaFile = skillFileOf(root, String(absorbedInto))
|
|
230
|
+
if (!fs.existsSync(umbrellaFile)) return fail(action, `absorbed_into 的伞不存在:${String(absorbedInto)}`, { name })
|
|
231
|
+
if (!fs.existsSync(location)) return fail(action, `技能不存在:${name}`, { name })
|
|
232
|
+
// pin 防归档(照 Hermes skill_manager_guards:pin 挡删除/归档,不挡 patch 与编辑)。
|
|
233
|
+
// 迁移点(archiveSkill)已有一道守卫,这里再判一次是为了给出**可见回执**——
|
|
234
|
+
// 工具面拒绝必须说明原因,不能只留下无声的 ok:false。
|
|
235
|
+
if (readUsage(root)[name]?.pinned) {
|
|
236
|
+
return { ok: false, action, error: `pinned 技能不归档:${name}(先取消 pin)`, receipt: receipt("未生效", { name, location, detail: "pinned" }) }
|
|
237
|
+
}
|
|
238
|
+
// 保护三分(照 A41)+归档直写(2026-09-25 修正,照 Hermes curator 自动归档):
|
|
239
|
+
// 无人值守归档**自动直写**(curator 叉以 plan 身份跑也一样),跑前快照兑底(restore/rollback 可回);
|
|
240
|
+
// user-owned 仍拒动(curator 不碰不拥有的技能,adopt 后才可管)、external 只读红。
|
|
241
|
+
const attended = origin === "user" || origin === "foreground"
|
|
242
|
+
if (!attended && readCreatedBy(location) !== "agent") {
|
|
243
|
+
return { ok: false, action, error: `回合后通道只动 agent-created 技能:${name} 是 ${readCreatedBy(location)}-owned`, receipt: receipt("未生效", { name, location, detail: "非 agent-created" }) }
|
|
244
|
+
}
|
|
245
|
+
const r = archiveSkill(root, name)
|
|
246
|
+
if (!r.ok) return fail(action, r.error ?? "归档失败", { name, location })
|
|
247
|
+
updateRecord(root, name, { absorbed_into: String(absorbedInto) })
|
|
248
|
+
appendAudit(layout(root), {
|
|
249
|
+
event: "skill-archive",
|
|
250
|
+
name,
|
|
251
|
+
location: path.relative(root, r.dest),
|
|
252
|
+
absorbedInto: String(absorbedInto),
|
|
253
|
+
createdBy,
|
|
254
|
+
origin,
|
|
255
|
+
agent: identity?.agent ?? null,
|
|
256
|
+
})
|
|
257
|
+
return {
|
|
258
|
+
ok: true,
|
|
259
|
+
action,
|
|
260
|
+
name,
|
|
261
|
+
location: r.dest,
|
|
262
|
+
receipt: receipt("已交付", { name, location: r.dest, createdBy, detail: `已归档(吸收进 ${String(absorbedInto)},只搬不删)` }),
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
let body = null
|
|
267
|
+
if (action === "create") {
|
|
268
|
+
const errs = [validateDescription(description), validateContent(content)].filter(Boolean)
|
|
269
|
+
if (errs.length) return fail(action, errs[0], { name })
|
|
270
|
+
if (fs.existsSync(location)) {
|
|
271
|
+
return fail(action, `A skill named '${name}' already exists at ${location}(增长走 patch)`, { name, location })
|
|
272
|
+
}
|
|
273
|
+
const hits = scanForThreats(String(content), { scope: "strict" })
|
|
274
|
+
if (hits.length) {
|
|
275
|
+
return fail(action, `写入被拒:内容命中威胁模式 ${hits.join(", ")}`, { name, location })
|
|
276
|
+
}
|
|
277
|
+
body = `---\nname: ${name}\ndescription: ${description}\ncreated_by: ${createdBy}\n---\n\n${String(content).trim()}\n`
|
|
278
|
+
} else {
|
|
279
|
+
// patch
|
|
280
|
+
if (typeof old_string !== "string" || !old_string.length) return fail(action, "patch 需要非空 old_string", { name })
|
|
281
|
+
if (typeof new_string !== "string") return fail(action, "patch 需要 new_string", { name })
|
|
282
|
+
if (!fs.existsSync(location)) {
|
|
283
|
+
return fail(action, `技能不存在:${name}(create 建新技能、patch 才是增长)`, { name })
|
|
284
|
+
}
|
|
285
|
+
// 规则④:回合后通道只动 agent-created(动 pinned/user-owned/external 必须红)
|
|
286
|
+
if (origin === "background" && readCreatedBy(location) !== "agent") {
|
|
287
|
+
return { ok: false, action, error: `回合后通道只动 agent-created 技能:${name} 是 ${readCreatedBy(location)}-owned`, receipt: receipt("未生效", { name, location, detail: "非 agent-created" }) }
|
|
288
|
+
}
|
|
289
|
+
const before = fs.readFileSync(location, "utf8")
|
|
290
|
+
const parts = before.split(old_string)
|
|
291
|
+
const hitsCount = parts.length - 1
|
|
292
|
+
if (hitsCount === 0) return fail(action, `old_string 零命中:${old_string.slice(0, 40)}`, { name, location })
|
|
293
|
+
if (hitsCount > 1) return fail(action, `old_string 歧义命中(${hitsCount} 处):换更长的原文`, { name, location })
|
|
294
|
+
const scanHits = scanForThreats(String(new_string), { scope: "strict" })
|
|
295
|
+
if (scanHits.length) {
|
|
296
|
+
return fail(action, `写入被拒:内容命中威胁模式 ${scanHits.join(", ")}`, { name, location })
|
|
297
|
+
}
|
|
298
|
+
body = before.replace(old_string, new_string)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// ── 闸门分流(照 memory-actions;技能面 destructive 恒 false,W8 裁定见文件头) ──
|
|
302
|
+
// 批准过的提案走受信任路径:人已经看过旧文与 diff,再问一次闸门只会把它送回待批队列。
|
|
303
|
+
// 宿主 deny 仍然压过一切(那不是我们的闸门,是宿主的权限面)。
|
|
304
|
+
const decision =
|
|
305
|
+
hostPermission === "deny"
|
|
306
|
+
? "deny"
|
|
307
|
+
: approved?.by
|
|
308
|
+
? "apply"
|
|
309
|
+
: decideWrite({
|
|
310
|
+
root,
|
|
311
|
+
subsystem: "skills",
|
|
312
|
+
origin,
|
|
313
|
+
destructive: false,
|
|
314
|
+
interactive: typeof ask === "function",
|
|
315
|
+
hostPermission,
|
|
316
|
+
})
|
|
317
|
+
if (decision === "deny") {
|
|
318
|
+
return { ok: false, action, error: "宿主权限闸拒绝了这次写入", receipt: receipt("未生效", { name, location, detail: "permission deny" }) }
|
|
319
|
+
}
|
|
320
|
+
if (decision === "stage") {
|
|
321
|
+
// 同一待批队列(pending/);记录带技能名与完整落盘正文,人才能批得了。
|
|
322
|
+
const store = createMemoryStore({ root, identity: { origin, agent: identity?.agent ?? null } })
|
|
323
|
+
const file = store.stageProposal({
|
|
324
|
+
op: action,
|
|
325
|
+
kind: "skills",
|
|
326
|
+
name,
|
|
327
|
+
text: action === "create" ? body : new_string,
|
|
328
|
+
match: action === "create" ? name : old_string,
|
|
329
|
+
// 批准时必须能回答两件事:批的是什么(description/正文)与「批的还是不是当初那份」(baseHash/createdBy)。
|
|
330
|
+
description: description ?? null,
|
|
331
|
+
baseHash: fs.existsSync(location) ? sha256(fs.readFileSync(location)) : null,
|
|
332
|
+
createdBy: fs.existsSync(location) ? readCreatedBy(location) : null,
|
|
333
|
+
})
|
|
334
|
+
return {
|
|
335
|
+
ok: true,
|
|
336
|
+
action,
|
|
337
|
+
staged: true,
|
|
338
|
+
file,
|
|
339
|
+
receipt: receipt("待批", { name, location, detail: "闸开:进待批队列", createdBy }),
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (decision === "ask") {
|
|
343
|
+
try {
|
|
344
|
+
await ask({
|
|
345
|
+
permission: "ocg_skill_write",
|
|
346
|
+
patterns: [name],
|
|
347
|
+
always: [],
|
|
348
|
+
metadata: { action, name, description: description ?? null },
|
|
349
|
+
})
|
|
350
|
+
} catch (err) {
|
|
351
|
+
const rejected = /reject|denied|not allowed|permission/i.test(String(err?.name ?? "") + String(err?.message ?? ""))
|
|
352
|
+
return {
|
|
353
|
+
ok: false,
|
|
354
|
+
action,
|
|
355
|
+
ask: { status: rejected ? "rejected" : "channel-error" },
|
|
356
|
+
retryable: !rejected,
|
|
357
|
+
error: rejected ? `人拒绝了这次 ${action}` : `调用通道出错(不是人拒绝):${err?.message ?? err}`,
|
|
358
|
+
receipt: receipt("未生效", { name, location, detail: rejected ? "人拒绝" : "通道出错" }),
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// ── 落盘(闸关=直写 / ask 已批准)────────────────────────────────────────
|
|
364
|
+
fs.mkdirSync(path.dirname(location), { recursive: true })
|
|
365
|
+
fs.writeFileSync(location, body)
|
|
366
|
+
const layoutCheck = checkSkillLayout(path.dirname(location))
|
|
367
|
+
if (!layoutCheck.ok) {
|
|
368
|
+
// 校验在写前已做过,走到这里是实现自相矛盾:回滚到零落盘,别留半成品
|
|
369
|
+
try { fs.rmSync(path.dirname(location), { recursive: true, force: true }) } catch { /* 回滚尽力 */ }
|
|
370
|
+
return fail(action, `生成物没过布局校验(已回滚):${layoutCheck.error}`, { name, location })
|
|
371
|
+
}
|
|
372
|
+
appendAudit(layout(root), {
|
|
373
|
+
event: action === "create" ? "skill-create" : "skill-patch",
|
|
374
|
+
name,
|
|
375
|
+
location: path.relative(root, location),
|
|
376
|
+
createdBy,
|
|
377
|
+
origin,
|
|
378
|
+
agent: identity?.agent ?? null,
|
|
379
|
+
})
|
|
380
|
+
// usage 账本(S2,best-effort):create 立户头(不计数,口径不掺水)、patch 记一笔 patch。
|
|
381
|
+
if (action === "create") recordSkillCreated(root, name, { createdBy })
|
|
382
|
+
else bumpSkillPatch(root, name)
|
|
383
|
+
return {
|
|
384
|
+
ok: true,
|
|
385
|
+
action,
|
|
386
|
+
name,
|
|
387
|
+
location,
|
|
388
|
+
createdBy,
|
|
389
|
+
receipt: receipt("已交付", { name, location, createdBy, detail: action === "create" ? "新建技能" : "同技能增长" }),
|
|
390
|
+
}
|
|
391
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// S3 跑前快照与回滚 —— 照 Hermes `agent/curator_backup.py`:
|
|
2
|
+
// · 快照=`skills/` 打 tar.gz 到 `skills/.curator_backups/<utc-iso>/{skills.tar.gz,manifest.json}`;
|
|
3
|
+
// 排除 `.curator_backups/` 自身与仓库/缓存垃圾(.git / node_modules / .venv / __pycache__)。
|
|
4
|
+
// · rollback **先快照当前树**(回滚本身也可撤销),再解档到原位。
|
|
5
|
+
// · 归档器用平台自带 `tar`(Hermes 用 Python 同款思路:平台自带归档器,不引第三方依赖;
|
|
6
|
+
// Windows 10+ 自带 bsdtar)。失败方向=报错不静默(快照失败则迁移中止、零落盘)。
|
|
7
|
+
import fs from "node:fs"
|
|
8
|
+
import path from "node:path"
|
|
9
|
+
import { spawnSync } from "node:child_process"
|
|
10
|
+
|
|
11
|
+
import { skillsDir } from "./skills.mjs"
|
|
12
|
+
|
|
13
|
+
export const BACKUPS_DIR = ".curator_backups"
|
|
14
|
+
export const ARCHIVE_NAME = "skills.tar.gz"
|
|
15
|
+
export const MANIFEST_NAME = "manifest.json"
|
|
16
|
+
/** 照 curator_backup 的排除口径:备份目录自身、git 元数据、缓存目录。 */
|
|
17
|
+
export const SNAPSHOT_EXCLUDES = [BACKUPS_DIR, ".git", "node_modules", ".venv", "__pycache__"]
|
|
18
|
+
|
|
19
|
+
const isoId = (d = new Date()) => d.toISOString().replace(/[:.]/g, "-")
|
|
20
|
+
|
|
21
|
+
function tarOf(root) {
|
|
22
|
+
return path.join(skillsDir(root), BACKUPS_DIR)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function runTar(args) {
|
|
26
|
+
const r = spawnSync("tar", args, { encoding: "utf8" })
|
|
27
|
+
if (r.error) return { ok: false, error: String(r.error.message ?? r.error) }
|
|
28
|
+
if (r.status !== 0) return { ok: false, error: (r.stderr || r.stdout || `tar exit ${r.status}`).trim() }
|
|
29
|
+
return { ok: true }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 跑前快照:skills/ → .curator_backups/<id>/skills.tar.gz + manifest.json。 */
|
|
33
|
+
export function snapshotSkills(root, reason = "manual") {
|
|
34
|
+
const base = tarOf(root)
|
|
35
|
+
let id = isoId()
|
|
36
|
+
if (fs.existsSync(path.join(base, id))) {
|
|
37
|
+
let n = 1
|
|
38
|
+
while (fs.existsSync(path.join(base, `${id}-${String(n).padStart(2, "0")}`))) n++
|
|
39
|
+
id = `${id}-${String(n).padStart(2, "0")}`
|
|
40
|
+
}
|
|
41
|
+
const dir = path.join(base, id)
|
|
42
|
+
try {
|
|
43
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
44
|
+
const args = ["-czf", path.join(dir, ARCHIVE_NAME)]
|
|
45
|
+
for (const x of SNAPSHOT_EXCLUDES) args.push(`--exclude=${x}`)
|
|
46
|
+
args.push("-C", skillsDir(root), ".")
|
|
47
|
+
const r = runTar(args)
|
|
48
|
+
if (!r.ok) {
|
|
49
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
50
|
+
return { ok: false, error: r.error }
|
|
51
|
+
}
|
|
52
|
+
fs.writeFileSync(
|
|
53
|
+
path.join(dir, MANIFEST_NAME),
|
|
54
|
+
JSON.stringify({ id, reason, created_at: new Date().toISOString(), excluded: SNAPSHOT_EXCLUDES }, null, 2) + "\n",
|
|
55
|
+
)
|
|
56
|
+
return { ok: true, id, dir }
|
|
57
|
+
} catch (err) {
|
|
58
|
+
return { ok: false, error: String(err?.message ?? err) }
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 快照清单(时间序)。 */
|
|
63
|
+
export function listSnapshots(root) {
|
|
64
|
+
try {
|
|
65
|
+
return fs
|
|
66
|
+
.readdirSync(tarOf(root))
|
|
67
|
+
.filter((d) => fs.existsSync(path.join(tarOf(root), d, ARCHIVE_NAME)))
|
|
68
|
+
.sort()
|
|
69
|
+
} catch {
|
|
70
|
+
return []
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 回滚到指定快照:先快照当前树(理由 `pre-rollback:<id>`,回滚自身可撤销),再清树解档。
|
|
76
|
+
* 清树保留 `.curator_backups/`(否则会把自己刚拍的快照删掉)。
|
|
77
|
+
*/
|
|
78
|
+
export function rollbackSkills(root, snapshotId) {
|
|
79
|
+
const dir = path.join(tarOf(root), snapshotId)
|
|
80
|
+
const tarPath = path.join(dir, ARCHIVE_NAME)
|
|
81
|
+
if (!fs.existsSync(tarPath)) return { ok: false, error: `快照不存在:${snapshotId}` }
|
|
82
|
+
const pre = snapshotSkills(root, `pre-rollback:${snapshotId}`)
|
|
83
|
+
if (!pre.ok) return { ok: false, error: `回滚前快照失败(中止,零落盘):${pre.error ?? ""}` }
|
|
84
|
+
try {
|
|
85
|
+
for (const entry of fs.readdirSync(skillsDir(root))) {
|
|
86
|
+
if (entry === BACKUPS_DIR) continue
|
|
87
|
+
fs.rmSync(path.join(skillsDir(root), entry), { recursive: true, force: true })
|
|
88
|
+
}
|
|
89
|
+
const r = runTar(["-xzf", tarPath, "-C", skillsDir(root)])
|
|
90
|
+
if (!r.ok) return { ok: false, error: `解档失败:${r.error}`, backupId: pre.id }
|
|
91
|
+
return { ok: true, id: snapshotId, backupId: pre.id }
|
|
92
|
+
} catch (err) {
|
|
93
|
+
return { ok: false, error: String(err?.message ?? err), backupId: pre.id }
|
|
94
|
+
}
|
|
95
|
+
}
|