opencode-growth 0.1.1 → 0.1.59
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 +60 -30
- package/package.json +3 -3
- package/src/adapter-opencode/curator-runner.mjs +254 -0
- package/src/adapter-opencode/index.mjs +305 -19
- package/src/adapter-opencode/install.mjs +63 -12
- 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 +75 -21
- package/src/adapter-opencode/session-search-tool.mjs +66 -0
- package/src/adapter-opencode/skill-tool.mjs +221 -0
- package/src/cli/commands.mjs +74 -1
- package/src/cli/curator-cmd.mjs +147 -0
- package/src/cli/journey-cmd.mjs +61 -0
- package/src/cli/journey-view.mjs +174 -0
- package/src/cli/main.mjs +172 -25
- package/src/cli/memory-cmd.mjs +165 -0
- package/src/cli/session-index-cmd.mjs +74 -0
- package/src/cli/skill-cmd.mjs +192 -0
- package/src/core/approve.mjs +155 -0
- package/src/core/audit.mjs +51 -0
- package/src/core/background-review.mjs +88 -1
- 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 +186 -9
- package/src/core/memory-actions.mjs +73 -18
- package/src/core/session-freshness.mjs +201 -0
- package/src/core/session-index.mjs +467 -0
- package/src/core/session-query.mjs +161 -0
- package/src/core/session-source.mjs +202 -0
- package/src/core/session-tool.mjs +181 -0
- package/src/core/skill-actions.mjs +372 -0
- package/src/core/skill-backup.mjs +95 -0
- package/src/core/skill-lifecycle.mjs +265 -0
- package/src/core/skill-usage.mjs +119 -0
- package/src/core/skills.mjs +28 -2
- 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/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
|
@@ -7,7 +7,9 @@ import { WRITE_PERMISSION_NAME } from "./paths.mjs"
|
|
|
7
7
|
// · 前台 + **有**交互通道 → 改/删**内联问一次**,人同意才落盘
|
|
8
8
|
// · 前台 + **没有**交互通道 → 暂存(**不能因为"没人拦"就直写**)
|
|
9
9
|
// · 后台(agent 在名单里)→ add 直写(D16)、改/删暂存
|
|
10
|
-
// · 身份不明 →
|
|
10
|
+
// · 身份不明 → 暂存(fail-closed)
|
|
11
|
+
//
|
|
12
|
+
// 以上四条是**闸开档**的分流;**闸关(默认)不看来源、直接生效**(D73:照 Hermes `write_approval.py:175`)。
|
|
11
13
|
//
|
|
12
14
|
// 三条不许破的线:
|
|
13
15
|
// 1. **模型不能自报身份**:动作参数里没有 origin 这个字段;就算塞进来也不看(extra#4)。
|
|
@@ -67,7 +69,7 @@ export function identityFor({
|
|
|
67
69
|
// 无头 `opencode run` 里 `ctx.ask` 会**立刻返回、没有任何人看过一眼**,
|
|
68
70
|
// 于是"有人批过"的判断完全落空、改删直接落盘。
|
|
69
71
|
// 所以前台直写还要部署方**显式声明**这个会话面是可交互的(`OCG_INTERACTIVE=1`)。
|
|
70
|
-
// 默认不声明 ⇒
|
|
72
|
+
// 默认不声明 ⇒ 判不出前台(闸开档会因身份不明而暂存;闸关档不看来源、直接生效,D73)。
|
|
71
73
|
// A41(astra 实跑复现):**「声明可交互」≠「有人能拦」** —— 只凭 hasAsk+interactive 判定前台,
|
|
72
74
|
// 会让**未登记**的会话身份直接改删落盘(pending=0),违反 extra#4「身份不明时一律暂存」。
|
|
73
75
|
// 现在前台要**两侧都成立**:部署方声明可交互 **且** 这个身份是部署方**登记过**的(对称于 backgroundAgents)。
|
|
@@ -97,11 +99,16 @@ export async function runMemoryAction({
|
|
|
97
99
|
kind = "memory",
|
|
98
100
|
text,
|
|
99
101
|
match,
|
|
102
|
+
operations,
|
|
100
103
|
ask = null,
|
|
101
104
|
limits = {},
|
|
102
105
|
enabled = {},
|
|
103
106
|
} = {}) {
|
|
104
107
|
if (!root) return { ok: false, error: "缺少 root(D 目录)" }
|
|
108
|
+
// G3 批量:给了 `operations` 就走批量面(`action` 忽略——照 Hermes,一个 memory 工具两副参数)。
|
|
109
|
+
if (Array.isArray(operations) && operations.length) {
|
|
110
|
+
return runMemoryBatch({ root, identity, kind, operations, limits, enabled })
|
|
111
|
+
}
|
|
105
112
|
if (!MEMORY_ACTIONS.includes(action)) {
|
|
106
113
|
return { ok: false, error: `未知动作:${String(action)}。可用动作:${MEMORY_ACTIONS.join(" / ")}` }
|
|
107
114
|
}
|
|
@@ -109,8 +116,8 @@ export async function runMemoryAction({
|
|
|
109
116
|
|
|
110
117
|
const hasAsk = typeof ask === "function"
|
|
111
118
|
const declared = identity?.origin
|
|
112
|
-
// **fail-closed**:自称前台但没有交互通道 ⇒ 降级为"身份不明"
|
|
113
|
-
// 这条防的是"通道断了却还在直写"——那正是 extra#4 说的"没人拦"
|
|
119
|
+
// **fail-closed**:自称前台但没有交互通道 ⇒ 降级为"身份不明"(闸开档会因此暂存)。
|
|
120
|
+
// 这条防的是"通道断了却还在直写"——那正是 extra#4 说的"没人拦"的情形;闸关档不看来源(D73)。
|
|
114
121
|
// 需要人批的动作才受"通道断了"的影响:add 不需要(D16),改/删才需要。
|
|
115
122
|
const needsAsk = action === "update" || action === "remove"
|
|
116
123
|
// 【subgoal② · 照 Hermes】闸门判定决定这一笔怎么写:
|
|
@@ -163,19 +170,13 @@ export async function runMemoryAction({
|
|
|
163
170
|
return { ok: true, action, pending: items, ...status() }
|
|
164
171
|
}
|
|
165
172
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
kind,
|
|
174
|
-
entries: out.length,
|
|
175
|
-
used: store.status().kinds[kind].used,
|
|
176
|
-
limit: store.status().kinds[kind].limit,
|
|
177
|
-
...status(),
|
|
178
|
-
}
|
|
173
|
+
// 三类动作的执行都**按动作层自己的判定来**(照 Hermes:闸门只判一次,判完就执行)。
|
|
174
|
+
// stage ⇒ 走 store 的暂存原语落一条待批;
|
|
175
|
+
// apply / ask 已获批准 ⇒ 用**受信任原语**落地(`approve.mjs` 的人批准路径同此形状)。
|
|
176
|
+
// 若继续让 store 的受门控家族再判一次,就成了**二次门控**——实测红过:
|
|
177
|
+
// 闸开时刚被人批准的改/删会被 store 重新打进待批(`同意后应当真的删掉`)。
|
|
178
|
+
if (action === "add" && (typeof text !== "string" || !text.trim())) {
|
|
179
|
+
return { ok: false, error: "add 需要非空 text" }
|
|
179
180
|
}
|
|
180
181
|
|
|
181
182
|
// update / remove:**先校验提案,再问,最后才写**。
|
|
@@ -213,10 +214,64 @@ export async function runMemoryAction({
|
|
|
213
214
|
}
|
|
214
215
|
}
|
|
215
216
|
|
|
216
|
-
|
|
217
|
+
// 判定说暂存 ⇒ 落一条待批(不碰正文)
|
|
218
|
+
if (decision === "stage") {
|
|
219
|
+
const file = store.stageProposal({ op: action, kind, text, match })
|
|
220
|
+
return { ok: true, action, staged: true, file, ...status() }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// 否则(`apply`,或闸开时人批准了那个 `ask`)⇒ 用**受信任原语**落地
|
|
224
|
+
if (action === "add") {
|
|
225
|
+
const out = await store.writeTrusted({ kind, text })
|
|
226
|
+
return {
|
|
227
|
+
ok: true,
|
|
228
|
+
action,
|
|
229
|
+
kind,
|
|
230
|
+
entries: out.length,
|
|
231
|
+
used: store.status().kinds[kind].used,
|
|
232
|
+
limit: store.status().kinds[kind].limit,
|
|
233
|
+
...status(),
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const out = action === "update" ? await store.updateTrusted({ kind, match, text }) : await store.removeTrusted({ kind, match })
|
|
217
237
|
if (out && out.staged) return { ok: true, action, staged: true, file: out.staged, ...status() }
|
|
218
238
|
return { ok: true, action, kind, entries: out.length, ...status() }
|
|
219
239
|
} catch (err) {
|
|
220
240
|
return { ok: false, action, error: err?.message ?? String(err) }
|
|
221
241
|
}
|
|
222
242
|
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* 批量原子(G3):**一个库、一次调用、全成或全败**。
|
|
246
|
+
*
|
|
247
|
+
* 判定与单条面同源:`store.batch` 内部先做「整批预扫」(`scanBatchOps`——与 `memories.mjs`
|
|
248
|
+
* 同一个实现,扫描先于任何落盘),再走**同一条** `decideWrite` 闸门;闸开且这批含非 add 动作
|
|
249
|
+
* (或来源是后台/身份不明)⇒ 整批作为**一个**待批文件暂存,正文一字不动。
|
|
250
|
+
*
|
|
251
|
+
* 与单条面的已知差别(照 Hermes:批量的工具面不做内联问):批量不会 `ask`——store 面没有交互
|
|
252
|
+
* 通道(`interactive:false`),闸开时前台也会落到「暂存」而不是「内联问」。这不是降级,是同一个
|
|
253
|
+
* fail-closed 方向:问不到人就不写。
|
|
254
|
+
*/
|
|
255
|
+
async function runMemoryBatch({ root, identity, kind, operations, limits, enabled }) {
|
|
256
|
+
const action = "batch"
|
|
257
|
+
try {
|
|
258
|
+
const store = createMemoryStore({ root, identity, limits, enabled })
|
|
259
|
+
const out = await store.batch({ kind, operations })
|
|
260
|
+
const st = store.status()
|
|
261
|
+
if (out && out.staged) return { ok: true, action, staged: true, file: out.staged, ...st }
|
|
262
|
+
return {
|
|
263
|
+
ok: true,
|
|
264
|
+
action,
|
|
265
|
+
kind,
|
|
266
|
+
applied: out.applied,
|
|
267
|
+
matchedEntries: out.matchedEntries,
|
|
268
|
+
entries: out.entries,
|
|
269
|
+
used: out.used,
|
|
270
|
+
limit: out.limit,
|
|
271
|
+
...st,
|
|
272
|
+
}
|
|
273
|
+
} catch (err) {
|
|
274
|
+
// 整批拒:正文一字未写;带上点名(「第 N 项」)与威胁模式(若有),让调用方原样转给人看。
|
|
275
|
+
return { ok: false, action, error: err?.message ?? String(err), code: err?.code ?? null, patterns: err?.patterns ?? null }
|
|
276
|
+
}
|
|
277
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// S4(G4):自动新鲜度——三路径调**同一幂等 sync**(session-index.syncStore):
|
|
2
|
+
// ① onIdle 定向 session 快刷(scope_session_id:比对该 session 的 part 集合与指纹,撤回删除/编辑)
|
|
3
|
+
// ② fs.watch 盯 库+WAL+父目录 唤醒(消抖、单飞、失败退避、唤醒不丢 pending 位)
|
|
4
|
+
// ③ 低频 timer 完整对账(mark-and-sweep 全库——快刷只作加速,完整对账才是完备边界)
|
|
5
|
+
// watcher 是唤醒源不是持久日志;pending 位持久化进 store meta,status 可见。
|
|
6
|
+
// 搜索前有界快刷预算:超时带 stale 返回,不卡调用(boundedRefresh)。
|
|
7
|
+
import path from "node:path"
|
|
8
|
+
import fs from "node:fs"
|
|
9
|
+
|
|
10
|
+
import { metaSet, openStore, syncStore } from "./session-index.mjs"
|
|
11
|
+
|
|
12
|
+
/** 定向 session 快刷(①):只抽取/撤回该 session 的 part,其余行不动。 */
|
|
13
|
+
export async function fastSync({ root, sourcePath, session_id, includeToolOutput = false, now = null } = {}) {
|
|
14
|
+
return syncStore({ root, sourcePath, includeToolOutput, scope_session_id: session_id, now })
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 完整对账(③):全库 mark-and-sweep——完备边界。 */
|
|
18
|
+
export async function fullReconcile({ root, sourcePath, includeToolOutput = false, now = null } = {}) {
|
|
19
|
+
return syncStore({ root, sourcePath, includeToolOutput, now })
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 搜索前有界快刷预算:budgetMs 内没刷完就带 timedOut=true 返回(调用方标 stale),
|
|
24
|
+
* 被赛跑的刷新自身继续跑完(幂等,无害)。budgetMs<=0 ⇒ 不刷、直接如实报「没核对」。
|
|
25
|
+
*/
|
|
26
|
+
export async function boundedRefresh({ root, sourcePath, includeToolOutput = false, budgetMs = 500 } = {}) {
|
|
27
|
+
if (!(budgetMs > 0)) return { refreshed: false, timedOut: true, reason: "no-budget" }
|
|
28
|
+
let timer = null
|
|
29
|
+
const timeout = new Promise((resolve) => {
|
|
30
|
+
timer = setTimeout(() => resolve({ refreshed: false, timedOut: true, reason: "budget-exceeded" }), budgetMs)
|
|
31
|
+
timer.unref?.()
|
|
32
|
+
})
|
|
33
|
+
const run = fullReconcile({ root, sourcePath, includeToolOutput })
|
|
34
|
+
.then((r) => ({ refreshed: Boolean(r.ok), timedOut: false, result: r }))
|
|
35
|
+
.catch((e) => ({ refreshed: false, timedOut: false, error: String(e?.message ?? e) }))
|
|
36
|
+
try {
|
|
37
|
+
return await Promise.race([run, timeout])
|
|
38
|
+
} finally {
|
|
39
|
+
clearTimeout(timer)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function setPendingMeta(root, v) {
|
|
44
|
+
try {
|
|
45
|
+
const store = openStore(root)
|
|
46
|
+
metaSet(store.db, "pending", v ? "1" : "0")
|
|
47
|
+
store.close()
|
|
48
|
+
} catch { /* 簿记失败不弄挂调度 */ }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 新鲜度调度器。fsImpl/timersImpl/now 可注入(单测用假时钟/假 fs 全确定性驱动)。
|
|
53
|
+
* 串行链保单飞(同一时刻只跑一轮);失败退避 = backoffBaseMs * 2^(failures-1) 封顶 backoffMaxMs,
|
|
54
|
+
* 退避期内的唤醒只挂起 pending(位不丢),退避过后补跑;补跑成功清 pending。
|
|
55
|
+
*/
|
|
56
|
+
export function createFreshnessScheduler({
|
|
57
|
+
root,
|
|
58
|
+
sourcePath,
|
|
59
|
+
includeToolOutput = false,
|
|
60
|
+
debounceMs = 250,
|
|
61
|
+
backoffBaseMs = 1000,
|
|
62
|
+
backoffMaxMs = 60_000,
|
|
63
|
+
intervalMs = 10 * 60 * 1000,
|
|
64
|
+
fsImpl = fs,
|
|
65
|
+
timersImpl = globalThis,
|
|
66
|
+
now = () => Date.now(),
|
|
67
|
+
onEvent = null,
|
|
68
|
+
} = {}) {
|
|
69
|
+
const st = { pending: false, running: false, failures: 0, nextAllowedAt: 0, stopped: false, lastError: null }
|
|
70
|
+
let chain = Promise.resolve()
|
|
71
|
+
let debounceTimer = null
|
|
72
|
+
let retryTimer = null
|
|
73
|
+
let intervalHandle = null
|
|
74
|
+
const watchers = []
|
|
75
|
+
|
|
76
|
+
function setPending(v) {
|
|
77
|
+
st.pending = v
|
|
78
|
+
setPendingMeta(root, v)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function enqueue(task) {
|
|
82
|
+
chain = chain.then(() => task(), () => task())
|
|
83
|
+
return chain
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function runSync(kind, { session_id = null } = {}) {
|
|
87
|
+
if (st.stopped) return { skipped: "stopped" }
|
|
88
|
+
if (now() < st.nextAllowedAt) {
|
|
89
|
+
// 退避期内:只挂起,唤醒不丢 pending 位
|
|
90
|
+
setPending(true)
|
|
91
|
+
onEvent?.({ what: "backed-off", kind, until: st.nextAllowedAt })
|
|
92
|
+
return { backedOff: true, until: st.nextAllowedAt }
|
|
93
|
+
}
|
|
94
|
+
st.running = true
|
|
95
|
+
try {
|
|
96
|
+
const r = session_id
|
|
97
|
+
? await fastSync({ root, sourcePath, session_id, includeToolOutput })
|
|
98
|
+
: await fullReconcile({ root, sourcePath, includeToolOutput })
|
|
99
|
+
if (r.ok) {
|
|
100
|
+
st.failures = 0
|
|
101
|
+
st.nextAllowedAt = 0
|
|
102
|
+
st.lastError = null
|
|
103
|
+
setPending(false)
|
|
104
|
+
onEvent?.({ what: "sync-done", kind, ok: true })
|
|
105
|
+
} else {
|
|
106
|
+
st.failures += 1
|
|
107
|
+
st.lastError = r.error ?? r.error_code ?? "sync-failed"
|
|
108
|
+
st.nextAllowedAt = now() + Math.min(backoffBaseMs * 2 ** (st.failures - 1), backoffMaxMs)
|
|
109
|
+
setPending(true) // 唤醒不丢 pending 位
|
|
110
|
+
onEvent?.({ what: "sync-failed", kind, error: st.lastError, nextAllowedAt: st.nextAllowedAt })
|
|
111
|
+
scheduleRetry()
|
|
112
|
+
}
|
|
113
|
+
return r
|
|
114
|
+
} finally {
|
|
115
|
+
st.running = false
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function scheduleRetry() {
|
|
120
|
+
if (retryTimer != null || st.stopped) return
|
|
121
|
+
const delay = Math.max(0, st.nextAllowedAt - now())
|
|
122
|
+
retryTimer = timersImpl.setTimeout(() => {
|
|
123
|
+
retryTimer = null
|
|
124
|
+
void enqueue(() => runSync("retry"))
|
|
125
|
+
}, delay)
|
|
126
|
+
retryTimer?.unref?.()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** fs 事件/手动唤醒(②):置 pending + 消抖,窗口内多事件只跑一轮。 */
|
|
130
|
+
function notify() {
|
|
131
|
+
if (st.stopped) return
|
|
132
|
+
setPending(true) // 唤醒不丢 pending 位
|
|
133
|
+
if (debounceTimer != null) timersImpl.clearTimeout(debounceTimer)
|
|
134
|
+
debounceTimer = timersImpl.setTimeout(() => {
|
|
135
|
+
debounceTimer = null
|
|
136
|
+
void enqueue(() => runSync("wake"))
|
|
137
|
+
}, debounceMs)
|
|
138
|
+
debounceTimer?.unref?.()
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** onIdle 定向快刷(①)。 */
|
|
142
|
+
function onIdle(sessionId) {
|
|
143
|
+
return enqueue(() => runSync("idle", { session_id: sessionId }))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** timer 完整对账(③)。 */
|
|
147
|
+
function tick() {
|
|
148
|
+
return enqueue(() => runSync("timer"))
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function start() {
|
|
152
|
+
const targets = [sourcePath, `${sourcePath}-wal`, path.dirname(sourcePath)]
|
|
153
|
+
for (const t of targets) {
|
|
154
|
+
try {
|
|
155
|
+
const w = fsImpl.watch(t, () => notify())
|
|
156
|
+
w?.unref?.()
|
|
157
|
+
watchers.push({ target: t, w })
|
|
158
|
+
} catch (e) {
|
|
159
|
+
onEvent?.({ what: "watch-failed", target: t, error: String(e?.message ?? e) })
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (intervalMs > 0) {
|
|
163
|
+
intervalHandle = timersImpl.setInterval(() => { void enqueue(() => runSync("timer")) }, intervalMs)
|
|
164
|
+
intervalHandle?.unref?.()
|
|
165
|
+
}
|
|
166
|
+
return { watched: watchers.map((x) => x.target) }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function stop() {
|
|
170
|
+
st.stopped = true
|
|
171
|
+
for (const { w } of watchers) {
|
|
172
|
+
try { w?.close?.() } catch { /* 关不掉就算了 */ }
|
|
173
|
+
}
|
|
174
|
+
watchers.length = 0
|
|
175
|
+
if (debounceTimer != null) timersImpl.clearTimeout(debounceTimer)
|
|
176
|
+
if (retryTimer != null) timersImpl.clearTimeout(retryTimer)
|
|
177
|
+
if (intervalHandle != null) timersImpl.clearInterval(intervalHandle)
|
|
178
|
+
debounceTimer = retryTimer = intervalHandle = null
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function whenIdle() {
|
|
182
|
+
let prev = null
|
|
183
|
+
while (prev !== chain) {
|
|
184
|
+
prev = chain
|
|
185
|
+
await chain.catch(() => {})
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function status() {
|
|
190
|
+
return {
|
|
191
|
+
pending: st.pending,
|
|
192
|
+
running: st.running,
|
|
193
|
+
failures: st.failures,
|
|
194
|
+
nextAllowedAt: st.nextAllowedAt,
|
|
195
|
+
lastError: st.lastError,
|
|
196
|
+
watched: watchers.map((x) => x.target),
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return { start, stop, notify, onIdle, tick, whenIdle, status }
|
|
201
|
+
}
|