@joe-broadhead/opencode-mem 0.8.5 → 0.9.1
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/CHANGELOG.md +512 -0
- package/README.md +127 -61
- package/docs/agent-protocol.md +67 -177
- package/package.json +1453 -100
- package/skills/mem.pages/SKILL.md +146 -0
- package/skills/mem.sharing/SKILL.md +91 -0
- package/skills/mem.sources/SKILL.md +94 -0
- package/skills/mem.usage/SKILL.md +129 -492
- package/src/backend/records.js +285 -0
- package/src/backend/v3.js +3 -0
- package/src/bootstrap.js +49 -45
- package/src/call-shapes.js +41 -10
- package/src/collector-lock.js +162 -0
- package/src/collector-service.js +441 -0
- package/src/collector.js +381 -0
- package/src/commands.js +3 -3
- package/src/config.js +15 -1
- package/src/credentials.js +26 -1
- package/src/curation.js +1 -1
- package/src/permissions.js +87 -10
- package/src/protocol.js +29 -20
- package/src/recall.js +104 -27
- package/src/records-store.js +267 -121
- package/src/records.js +63 -11
- package/src/runtime.js +23 -14
- package/src/skills.js +44 -18
- package/src/sources.js +58 -0
- package/src/spaces.js +93 -10
- package/src/tools/aliases.js +164 -0
- package/src/tools/commitments.js +12 -9
- package/src/tools/held-write.js +82 -0
- package/src/tools/index.js +100 -23
- package/src/tools/ingest.js +277 -0
- package/src/tools/knowledge.js +6 -1
- package/src/tools/memory.js +722 -229
- package/src/tools/page.js +423 -0
- package/src/tools/recall.js +371 -113
- package/src/tools/records-kit.js +142 -0
- package/src/tools/share.js +1042 -0
- package/src/tools/shared.js +16 -2
- package/src/tools/spaces.js +431 -143
- package/src/tools/status.js +87 -58
- package/src/tools/wiki.js +8 -4
- package/src/views/contracts.js +293 -3
- package/src/views/inbox-decision.js +59 -0
- package/src/views/recall-result.js +12 -0
- package/src/views/records-reads.js +675 -0
- package/src/views/rpc.js +58 -99
- package/src/views/shape.js +61 -0
- package/views/commitments.html +4 -0
- package/views/graph.html +753 -0
- package/views/home.html +11 -5
- package/views/journal.html +621 -0
- package/views/memory.html +7 -1
- package/views/pages.html +636 -0
- package/views/recall.html +123 -0
- package/views/review.html +406 -0
- package/views/today.html +309 -0
package/src/collector.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { errorMessage, isPlainObject, oneLine } from "./util.js"
|
|
2
|
+
import { contractRefusal, rpcDefinition, sourceEntry } from "./sources.js"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The memory collector (docs/design/memory-v1-contracts.md §4.4, §4.5).
|
|
6
|
+
*
|
|
7
|
+
* For each enabled, confirmed binding of a registered source (src/sources.js)
|
|
8
|
+
* it reads the cursor, lists the source's events, and sends them to Memory,
|
|
9
|
+
* at most five pages per source per run. The records service saves the cursor
|
|
10
|
+
* with the events, so a failed run retries from the old cursor, and event ids
|
|
11
|
+
* are idempotent, so a repeat is harmless.
|
|
12
|
+
*
|
|
13
|
+
* Two ways run it:
|
|
14
|
+
*
|
|
15
|
+
* - **In the plugin** (src/collector-service.js): a timer, one process per
|
|
16
|
+
* machine, through each source's tools RPC (`runOnce`, `hostSourceLister`).
|
|
17
|
+
* OpenCode 2.2.5 gives plugins no schedule API (a plugin's `Context` has no
|
|
18
|
+
* schedule domain; `experimental.schedule.*` is on the host's SDK client
|
|
19
|
+
* only), so this is how plain OpenCode collects, and Jelly while awake.
|
|
20
|
+
* - **A host schedule** (`collectCheckCode`): the Code Mode check a host that
|
|
21
|
+
* offers a schedule writes, calling `tools.<namespace>.events_list`. Jelly
|
|
22
|
+
* instead declares a routine that calls `tools.mem.collect` (`jelly.routines`).
|
|
23
|
+
*
|
|
24
|
+
* Every `events_list` answer must carry `source` and `account` (§4.5); one
|
|
25
|
+
* without them is refused with the release its plugin needs, and the binding
|
|
26
|
+
* and the ingest use the answer's own `source` and `account`.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export const COLLECT_SCHEDULE = "memory-collect"
|
|
30
|
+
/** Every 30 minutes, in the engine's zone. */
|
|
31
|
+
export const COLLECT_CRON = "*/30 * * * *"
|
|
32
|
+
/** At most this many pages per source in one run (contract §4.4). */
|
|
33
|
+
export const PAGES_PER_SOURCE = 5
|
|
34
|
+
/** Events asked of a source per page (contract §4.4). */
|
|
35
|
+
export const PAGE_LIMIT = 200
|
|
36
|
+
/** The account a binding without one uses. */
|
|
37
|
+
export const DEFAULT_ACCOUNT = "default"
|
|
38
|
+
/** The reserved session the collector's RPC calls and a host schedule run in. */
|
|
39
|
+
export const COLLECT_SESSION = "ses_mem_collect"
|
|
40
|
+
/**
|
|
41
|
+
* The engine sends a schedule's prompt only after its check fails three times
|
|
42
|
+
* (or once, invalidly). The check catches everything, so this is a last resort.
|
|
43
|
+
*/
|
|
44
|
+
export const COLLECT_FAILURE_PROMPT =
|
|
45
|
+
"The memory-collect schedule's check failed repeatedly, so the engine paused it. Reply with the single word: paused."
|
|
46
|
+
/**
|
|
47
|
+
* The collector session's own rules: only Code Mode and the sources'
|
|
48
|
+
* `events_list`, so neither an RPC call nor a failure prompt's turn can do
|
|
49
|
+
* anything else there.
|
|
50
|
+
*/
|
|
51
|
+
export const COLLECT_SESSION_RULES = Object.freeze([
|
|
52
|
+
Object.freeze({ action: "*", resource: "*", effect: "deny" }),
|
|
53
|
+
Object.freeze({ action: "execute", resource: "*", effect: "allow" }),
|
|
54
|
+
Object.freeze({ action: "mem_cursor", resource: "*", effect: "allow" }),
|
|
55
|
+
Object.freeze({ action: "mem_ingest", resource: "*", effect: "allow" }),
|
|
56
|
+
Object.freeze({ action: "*_events_list", resource: "*", effect: "allow" }),
|
|
57
|
+
])
|
|
58
|
+
|
|
59
|
+
/** A source name is a lowercase identifier; only registered sources are collected. */
|
|
60
|
+
const SOURCE_NAME = /^[a-z][a-z0-9_-]{0,63}$/
|
|
61
|
+
const ACCOUNT_NAME = /^[A-Za-z0-9][A-Za-z0-9_.@+-]{0,127}$/
|
|
62
|
+
const IDENTIFIER = /^[a-zA-Z][a-zA-Z0-9_]*$/
|
|
63
|
+
|
|
64
|
+
export function isSourceName(value) {
|
|
65
|
+
return typeof value === "string" && SOURCE_NAME.test(value)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function isAccountName(value) {
|
|
69
|
+
return typeof value === "string" && ACCOUNT_NAME.test(value)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function bindingKey(binding) {
|
|
73
|
+
return `${binding.source}\u0000${binding.account || DEFAULT_ACCOUNT}`
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The bindings a run collects: enabled, confirmed, of a registered source, with
|
|
78
|
+
* an account name safe to write into code. Sorted, so the same set always gives
|
|
79
|
+
* the same code; `only` narrows to one source.
|
|
80
|
+
*
|
|
81
|
+
* @param {unknown} sources the `sources` array `sources.list` answers
|
|
82
|
+
* @param {{ only?: string }} [options]
|
|
83
|
+
* @returns {{ source: string, account: string }[]}
|
|
84
|
+
*/
|
|
85
|
+
export function collectableSources(sources, { only } = {}) {
|
|
86
|
+
const seen = new Map()
|
|
87
|
+
for (const entry of Array.isArray(sources) ? sources : []) {
|
|
88
|
+
if (!isPlainObject(entry)) continue
|
|
89
|
+
if (entry.enabled !== true && entry.enabled !== 1) continue
|
|
90
|
+
if (entry.status !== "confirmed") continue
|
|
91
|
+
const source = String(entry.source ?? "")
|
|
92
|
+
const account = String(entry.account ?? "") || DEFAULT_ACCOUNT
|
|
93
|
+
if (!sourceEntry(source) || !isAccountName(account)) continue
|
|
94
|
+
if (only && source !== only) continue
|
|
95
|
+
const binding = { source, account }
|
|
96
|
+
seen.set(bindingKey(binding), binding)
|
|
97
|
+
}
|
|
98
|
+
return [...seen.values()].sort((a, b) => bindingKey(a).localeCompare(bindingKey(b)))
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The input every `events_list` call to a binding sends: the page, its account, and the registry's input. */
|
|
102
|
+
export function listInput(binding, cursor) {
|
|
103
|
+
const entry = sourceEntry(binding.source)
|
|
104
|
+
return {
|
|
105
|
+
...(cursor ? { cursor } : {}),
|
|
106
|
+
limit: PAGE_LIMIT,
|
|
107
|
+
...(binding.account && binding.account !== DEFAULT_ACCOUNT ? { account: binding.account } : {}),
|
|
108
|
+
...(entry?.input ?? {}),
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The Code Mode check for a host's `memory-collect` schedule, or null when no
|
|
114
|
+
* source is collectable (then the schedule is removed).
|
|
115
|
+
*
|
|
116
|
+
* The code runs as the body of an async function with `tools` in scope. It
|
|
117
|
+
* calls each source by the registry's namespace (`tools.gws.events_list` for
|
|
118
|
+
* gmail, with `source: "gmail"`), never throws (a throwing check is a strike
|
|
119
|
+
* toward the engine pausing the schedule and prompting a turn), never calls a
|
|
120
|
+
* model, and returns false. It stops a source at an answer without `source`
|
|
121
|
+
* and `account`, an answer for another source, or a cursor Memory did not save.
|
|
122
|
+
*
|
|
123
|
+
* @param {unknown} sources
|
|
124
|
+
* @returns {string | null}
|
|
125
|
+
*/
|
|
126
|
+
export function collectCheckCode(sources) {
|
|
127
|
+
const bindings = collectableSources(sources)
|
|
128
|
+
if (bindings.length === 0) return null
|
|
129
|
+
const calls = bindings.map((binding) => {
|
|
130
|
+
const entry = sourceEntry(binding.source)
|
|
131
|
+
const name = JSON.stringify(binding.source)
|
|
132
|
+
const account = binding.account === DEFAULT_ACCOUNT ? "null" : JSON.stringify(binding.account)
|
|
133
|
+
const extra = Object.keys(entry.input).length ? `, ...${JSON.stringify(entry.input)}` : ""
|
|
134
|
+
// Dotted, as the engine's scan of a check reads tool names: both parts are
|
|
135
|
+
// identifiers from the registry, never from a binding.
|
|
136
|
+
if (!IDENTIFIER.test(entry.namespace) || !IDENTIFIER.test(entry.tool)) throw new Error(`registry entry ${binding.source} names no Code Mode identifier`)
|
|
137
|
+
return `await collect(${name}, ${account}, (input) => tools.${entry.namespace}.${entry.tool}({ ...input${extra} }));`
|
|
138
|
+
})
|
|
139
|
+
return [
|
|
140
|
+
`// ${COLLECT_SCHEDULE} v2, written by opencode-mem from your enabled, confirmed sources; rewritten when they change.`,
|
|
141
|
+
"// No model turn: it reads each source's events and sends them to Memory, then returns false.",
|
|
142
|
+
"async function collect(source, account, list) {",
|
|
143
|
+
" try {",
|
|
144
|
+
" const c = await tools.mem.cursor(account ? { source, account } : { source });",
|
|
145
|
+
' if (!c || typeof c !== "object" || c.enabled === false || (c.status && c.status !== "confirmed")) return;',
|
|
146
|
+
' let cursor = typeof c.cursor === "string" && c.cursor ? c.cursor : undefined;',
|
|
147
|
+
` for (let page = 0; page < ${PAGES_PER_SOURCE}; page += 1) {`,
|
|
148
|
+
` const r = await list({ ...(cursor ? { cursor } : {}), limit: ${PAGE_LIMIT}, ...(account ? { account } : {}) });`,
|
|
149
|
+
' if (!r || typeof r !== "object" || r.source !== source || typeof r.account !== "string" || !r.account) return;',
|
|
150
|
+
' if (!Array.isArray(r.events) || typeof r.cursor !== "string") return;',
|
|
151
|
+
" const saved = await tools.mem.ingest({ source: r.source, account: r.account, owner: r.owner, events: r.events, cursor: r.cursor });",
|
|
152
|
+
' if (!saved || typeof saved !== "object" || saved.cursor_saved !== true) return;',
|
|
153
|
+
" cursor = r.cursor;",
|
|
154
|
+
" if (!r.more) return;",
|
|
155
|
+
" }",
|
|
156
|
+
" } catch {}",
|
|
157
|
+
"}",
|
|
158
|
+
...calls,
|
|
159
|
+
"return false;",
|
|
160
|
+
].join("\n")
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The host's schedule API as the collector uses it, or null when a plugin
|
|
165
|
+
* cannot reach one. OpenCode 2.2.5 gives plugins none, so this returns null
|
|
166
|
+
* there. A host that hands plugins the SDK client's shape
|
|
167
|
+
* (`experimental.schedule.{list,create,delete}`) is used as is, in the
|
|
168
|
+
* reserved collector session.
|
|
169
|
+
*/
|
|
170
|
+
export function hostScheduler(ctx, ensureSession) {
|
|
171
|
+
const api = ctx?.experimental?.schedule
|
|
172
|
+
if (!api || typeof api.create !== "function" || typeof api.list !== "function" || typeof api.delete !== "function") return null
|
|
173
|
+
if (typeof ensureSession !== "function") return null
|
|
174
|
+
return {
|
|
175
|
+
async list() {
|
|
176
|
+
const sessionID = await ensureSession()
|
|
177
|
+
const rows = await api.list({ sessionID })
|
|
178
|
+
return Array.isArray(rows) ? rows : []
|
|
179
|
+
},
|
|
180
|
+
async create({ name, cron, code, prompt }) {
|
|
181
|
+
const sessionID = await ensureSession()
|
|
182
|
+
return api.create({ sessionID, name, when: { cron }, if: { code }, prompt })
|
|
183
|
+
},
|
|
184
|
+
async remove(id) {
|
|
185
|
+
await api.delete({ scheduleID: id })
|
|
186
|
+
},
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function codeOf(row) {
|
|
191
|
+
return typeof row?.if?.code === "string" ? row.if.code : typeof row?.check?.code === "string" ? row.check.code : ""
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function idOf(row) {
|
|
195
|
+
return String(row?.id ?? row?.scheduleID ?? "")
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** A source plugin's refusal to keep the source contract: no `source` and `account` in its answer. */
|
|
199
|
+
export class SourceContractError extends Error {
|
|
200
|
+
constructor(message) {
|
|
201
|
+
super(message)
|
|
202
|
+
this.name = "SourceContractError"
|
|
203
|
+
this.code = "source_contract"
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* An `events_list` answer, bare, as an RPC result's `output`, or as JSON text,
|
|
209
|
+
* checked against the source contract for `entry`: it must name `source` (this
|
|
210
|
+
* entry's) and `account`. Throws a SourceContractError otherwise; null when it
|
|
211
|
+
* is not an answer at all.
|
|
212
|
+
*/
|
|
213
|
+
export function readEventsAnswer(value, entry) {
|
|
214
|
+
let answer = value
|
|
215
|
+
if (isPlainObject(answer) && !Array.isArray(answer.events) && isPlainObject(answer.output)) answer = answer.output
|
|
216
|
+
if (isPlainObject(answer) && !Array.isArray(answer.events) && typeof (answer.content ?? answer.text) === "string") answer = answer.content ?? answer.text
|
|
217
|
+
if (typeof answer === "string") {
|
|
218
|
+
try {
|
|
219
|
+
answer = JSON.parse(answer)
|
|
220
|
+
} catch {
|
|
221
|
+
return null
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (!isPlainObject(answer) || !Array.isArray(answer.events)) return null
|
|
225
|
+
const source = typeof answer.source === "string" ? answer.source.trim() : ""
|
|
226
|
+
const account = typeof answer.account === "string" ? answer.account.trim() : ""
|
|
227
|
+
if (!source || !account) throw new SourceContractError(contractRefusal(entry))
|
|
228
|
+
if (source !== entry.source) throw new SourceContractError(`${entry.package}'s events_list answered for source "${oneLine(source, 40)}" when asked for ${entry.source}, so Memory refused it.`)
|
|
229
|
+
if (!isAccountName(account)) throw new SourceContractError(`${entry.package}'s events_list answered an account name Memory cannot use ("${oneLine(account, 60)}").`)
|
|
230
|
+
return {
|
|
231
|
+
source,
|
|
232
|
+
account,
|
|
233
|
+
owner: isPlainObject(answer.owner) ? answer.owner : null,
|
|
234
|
+
events: answer.events,
|
|
235
|
+
cursor: typeof answer.cursor === "string" ? answer.cursor : "",
|
|
236
|
+
more: answer.more === true,
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* A source's `events_list`, called through the host's tools RPC (`ctx.rpc`)
|
|
242
|
+
* with the registry's RPC id, in the session `sessionFor()` gives; null when
|
|
243
|
+
* the host has no RPC client.
|
|
244
|
+
*
|
|
245
|
+
* @returns {null | ((source: string, input: object, sessionID?: string) => Promise<ReturnType<typeof readEventsAnswer>>)}
|
|
246
|
+
*/
|
|
247
|
+
export function hostSourceLister(ctx) {
|
|
248
|
+
if (typeof ctx?.rpc !== "function") return null
|
|
249
|
+
return async (source, input, sessionID) => {
|
|
250
|
+
const entry = sourceEntry(source)
|
|
251
|
+
if (!entry) throw new Error(`${oneLine(source, 40)} is not a source Memory knows`)
|
|
252
|
+
const client = ctx.rpc(rpcDefinition(entry))
|
|
253
|
+
if (typeof client?.[entry.tool] !== "function") throw new Error(`the host has no RPC method ${entry.rpc}/${entry.tool}`)
|
|
254
|
+
return readEventsAnswer(await client[entry.tool]({ sessionID, input: { ...input, ...entry.input } }), entry)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The collector's core: schedule upkeep, one collection, and the owners a
|
|
260
|
+
* refused ingest reported.
|
|
261
|
+
*
|
|
262
|
+
* @param {{ scheduler?: ReturnType<typeof hostScheduler>, lister?: ReturnType<typeof hostSourceLister>, log?: any }} [input]
|
|
263
|
+
*/
|
|
264
|
+
export function createCollector({ scheduler = null, lister = null, log } = {}) {
|
|
265
|
+
const reported = new Map()
|
|
266
|
+
|
|
267
|
+
function noteOwner(source, account, owner) {
|
|
268
|
+
if (!isSourceName(source) || !isPlainObject(owner) || !owner.id) return
|
|
269
|
+
const key = bindingKey({ source, account: account || DEFAULT_ACCOUNT })
|
|
270
|
+
reported.delete(key)
|
|
271
|
+
reported.set(key, { id: oneLine(owner.id, 300), label: oneLine(owner.label ?? owner.email ?? owner.id, 300), account: account || DEFAULT_ACCOUNT })
|
|
272
|
+
if (reported.size > 100) reported.delete(reported.keys().next().value)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** The owner last reported for a source and account; with no account, the latest for the source, whatever its account. */
|
|
276
|
+
function ownerFor(source, account) {
|
|
277
|
+
if (account) return reported.get(bindingKey({ source, account })) ?? null
|
|
278
|
+
const prefix = `${source}\u0000`
|
|
279
|
+
const keys = [...reported.keys()].filter((key) => key.startsWith(prefix))
|
|
280
|
+
return keys.length ? reported.get(keys.at(-1)) : null
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Bring a host schedule in step with the sources: create it, rewrite it when
|
|
285
|
+
* the check changes (the engine has no edit), or remove it when nothing is
|
|
286
|
+
* collectable. Without a scheduler it changes nothing.
|
|
287
|
+
*/
|
|
288
|
+
async function sync(sources) {
|
|
289
|
+
const code = collectCheckCode(sources)
|
|
290
|
+
const count = collectableSources(sources).length
|
|
291
|
+
if (!scheduler) return { mode: "manual", action: "none", sources: count }
|
|
292
|
+
try {
|
|
293
|
+
const mine = (await scheduler.list()).filter((row) => row?.name === COLLECT_SCHEDULE)
|
|
294
|
+
let action
|
|
295
|
+
if (!code) {
|
|
296
|
+
for (const row of mine) await scheduler.remove(idOf(row))
|
|
297
|
+
action = mine.length > 0 ? "removed" : "absent"
|
|
298
|
+
} else if (mine.length === 1 && codeOf(mine[0]) === code) {
|
|
299
|
+
action = "unchanged"
|
|
300
|
+
} else {
|
|
301
|
+
for (const row of mine) await scheduler.remove(idOf(row))
|
|
302
|
+
await scheduler.create({ name: COLLECT_SCHEDULE, cron: COLLECT_CRON, code, prompt: COLLECT_FAILURE_PROMPT })
|
|
303
|
+
action = mine.length > 0 ? "rewritten" : "created"
|
|
304
|
+
}
|
|
305
|
+
return { mode: "schedule", action, sources: count }
|
|
306
|
+
} catch (error) {
|
|
307
|
+
const message = errorMessage(error)
|
|
308
|
+
log?.warn?.("memory-collect schedule upkeep failed", { error: message })
|
|
309
|
+
return { mode: "schedule", action: "failed", sources: count, error: message }
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* One collection. Each binding's result is pushed to `results` as soon as it
|
|
315
|
+
* starts and updated page by page, so a caller that stops waiting can report
|
|
316
|
+
* progress.
|
|
317
|
+
*
|
|
318
|
+
* @param {{
|
|
319
|
+
* sources: unknown,
|
|
320
|
+
* only?: string,
|
|
321
|
+
* cursor: (binding: { source: string, account: string }) => Promise<{ cursor?: string, enabled?: boolean, status?: string }>,
|
|
322
|
+
* ingest: (input: { source: string, account: string, owner: any, events: unknown[], cursor: string }) => Promise<{ accepted?: number, duplicates?: number, refused?: number, cursor_saved?: boolean }>,
|
|
323
|
+
* sessionID?: string,
|
|
324
|
+
* results?: object[],
|
|
325
|
+
* }} input
|
|
326
|
+
*/
|
|
327
|
+
async function runOnce({ sources, only, cursor, ingest, sessionID, results = [] }) {
|
|
328
|
+
for (const binding of collectableSources(sources, { only })) {
|
|
329
|
+
const result = { ...binding, pages: 0, accepted: 0, duplicates: 0, refused: 0, more: false, done: false }
|
|
330
|
+
results.push(result)
|
|
331
|
+
try {
|
|
332
|
+
if (!lister) {
|
|
333
|
+
result.error = "this host gives plugins no way to call another plugin's events_list"
|
|
334
|
+
continue
|
|
335
|
+
}
|
|
336
|
+
const state = await cursor(binding)
|
|
337
|
+
if (!state || state.enabled === false || (state.status && state.status !== "confirmed")) {
|
|
338
|
+
result.error = `the binding is ${state?.enabled === false ? "disabled" : state?.status || "unknown"}`
|
|
339
|
+
continue
|
|
340
|
+
}
|
|
341
|
+
let at = typeof state.cursor === "string" && state.cursor ? state.cursor : undefined
|
|
342
|
+
for (let page = 0; page < PAGES_PER_SOURCE; page += 1) {
|
|
343
|
+
const answer = await lister(binding.source, listInput(binding, at), sessionID)
|
|
344
|
+
if (!answer || !answer.cursor) {
|
|
345
|
+
result.error = "the source's events_list gave no events and cursor"
|
|
346
|
+
break
|
|
347
|
+
}
|
|
348
|
+
// The answer's own source and account (§4.5): the binding they name decides.
|
|
349
|
+
const saved = await ingest({ source: answer.source, account: answer.account, owner: answer.owner, events: answer.events, cursor: answer.cursor })
|
|
350
|
+
result.pages += 1
|
|
351
|
+
result.accepted += Number(saved?.accepted) || 0
|
|
352
|
+
result.duplicates += Number(saved?.duplicates) || 0
|
|
353
|
+
result.refused += Number(saved?.refused) || 0
|
|
354
|
+
if (saved?.cursor_saved !== true) {
|
|
355
|
+
result.error = saved?.error || "Memory did not save the cursor, so collection stopped here"
|
|
356
|
+
break
|
|
357
|
+
}
|
|
358
|
+
at = answer.cursor
|
|
359
|
+
result.more = answer.more
|
|
360
|
+
if (!answer.more) break
|
|
361
|
+
}
|
|
362
|
+
} catch (error) {
|
|
363
|
+
result.error = errorMessage(error)
|
|
364
|
+
} finally {
|
|
365
|
+
result.done = true
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return results
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return {
|
|
372
|
+
get scheduled() {
|
|
373
|
+
return Boolean(scheduler)
|
|
374
|
+
},
|
|
375
|
+
lister,
|
|
376
|
+
noteOwner,
|
|
377
|
+
ownerFor,
|
|
378
|
+
sync,
|
|
379
|
+
runOnce,
|
|
380
|
+
}
|
|
381
|
+
}
|
package/src/commands.js
CHANGED
|
@@ -83,7 +83,7 @@ export async function registerCommands(ctx, runtime) {
|
|
|
83
83
|
}
|
|
84
84
|
editor.add({
|
|
85
85
|
name: "mem-status",
|
|
86
|
-
description: "Show memory
|
|
86
|
+
description: "Show memory health, scope, credential, per-layer counts and spool depth, without a model turn",
|
|
87
87
|
execute: async ({ sessionID }) => {
|
|
88
88
|
try {
|
|
89
89
|
const context = runtime.control ? await runtime.resolveContext({ sessionID }) : null
|
|
@@ -116,7 +116,7 @@ export async function registerCommands(ctx, runtime) {
|
|
|
116
116
|
|
|
117
117
|
editor.add({
|
|
118
118
|
name: "mem-sync",
|
|
119
|
-
description: "
|
|
119
|
+
description: "Rebuild the pinned block, flush spooled captures, and re-mirror skills from every accessible space",
|
|
120
120
|
execute: async ({ sessionID }) => {
|
|
121
121
|
runtime.invalidate(sessionID)
|
|
122
122
|
runtime.resetSkillContexts?.()
|
|
@@ -146,7 +146,7 @@ export async function registerCommands(ctx, runtime) {
|
|
|
146
146
|
|
|
147
147
|
editor.add({
|
|
148
148
|
name: "mem-why",
|
|
149
|
-
description: "Show the exact memory block pinned into this session",
|
|
149
|
+
description: "Show the exact memory block pinned into this session (dashboard, Due list, persona)",
|
|
150
150
|
execute: async ({ sessionID, agent } = {}) => {
|
|
151
151
|
await reply(sessionID, await explainPin(sessionID, agent))
|
|
152
152
|
},
|
package/src/config.js
CHANGED
|
@@ -43,9 +43,19 @@ const DEFAULTS = Object.freeze({
|
|
|
43
43
|
*/
|
|
44
44
|
compactionCheckpoint: undefined,
|
|
45
45
|
tools: true,
|
|
46
|
+
/** Opt-in only for hosts that expose the held-write Inbox decisions. */
|
|
47
|
+
heldTeamWrites: false,
|
|
46
48
|
skills: true,
|
|
47
49
|
commands: true,
|
|
48
50
|
spool: true,
|
|
51
|
+
/**
|
|
52
|
+
* Collect from the person's bound sources on a timer while OpenCode runs
|
|
53
|
+
* (one process per machine; docs/design/memory-v1-contracts.md §4.4). Off:
|
|
54
|
+
* mem_collect and /mem-collect still collect on request.
|
|
55
|
+
*/
|
|
56
|
+
collect: true,
|
|
57
|
+
/** Minutes between collections, 5 to 1440. */
|
|
58
|
+
collectEveryMinutes: 30,
|
|
49
59
|
|
|
50
60
|
bootstrapChars: 4000,
|
|
51
61
|
recallLimit: 8,
|
|
@@ -82,6 +92,7 @@ const ENV_MAP = {
|
|
|
82
92
|
captureSubagents: "MEMORY_CAPTURE_SUBAGENTS",
|
|
83
93
|
stateDir: "MEMORY_STATE_DIR",
|
|
84
94
|
timeZone: "MEMORY_TIME_ZONE",
|
|
95
|
+
collect: "MEMORY_COLLECT",
|
|
85
96
|
}
|
|
86
97
|
|
|
87
98
|
const BOOLEAN_KEYS = [
|
|
@@ -91,13 +102,15 @@ const BOOLEAN_KEYS = [
|
|
|
91
102
|
"bootstrap",
|
|
92
103
|
"compactionCheckpoint",
|
|
93
104
|
"tools",
|
|
105
|
+
"heldTeamWrites",
|
|
94
106
|
"skills",
|
|
95
107
|
"commands",
|
|
96
108
|
"spool",
|
|
109
|
+
"collect",
|
|
97
110
|
"debug",
|
|
98
111
|
]
|
|
99
112
|
|
|
100
|
-
const NUMBER_KEYS = ["bootstrapChars", "recallLimit", "recallChars", "timeoutMs", "retries"]
|
|
113
|
+
const NUMBER_KEYS = ["bootstrapChars", "recallLimit", "recallChars", "timeoutMs", "retries", "collectEveryMinutes"]
|
|
101
114
|
|
|
102
115
|
/** An IANA zone this runtime knows, or "". */
|
|
103
116
|
function validTimeZone(value) {
|
|
@@ -164,6 +177,7 @@ export function resolveConfig(options = {}, env = process.env) {
|
|
|
164
177
|
merged[key] = toPositiveInt(merged[key], DEFAULTS[key])
|
|
165
178
|
}
|
|
166
179
|
merged.retries = Math.min(merged.retries, 5)
|
|
180
|
+
merged.collectEveryMinutes = Math.min(Math.max(merged.collectEveryMinutes, 5), 1440)
|
|
167
181
|
|
|
168
182
|
for (const key of ["baseURL", "controlURL", "knowledgeURL"]) {
|
|
169
183
|
if (!merged[key]) continue
|
package/src/credentials.js
CHANGED
|
@@ -162,6 +162,13 @@ export function asSession(sessionID, fn, turn) {
|
|
|
162
162
|
return sessionScope.run({ session: sessionKey(sessionID), turn: typeof turn === "string" ? turn : "" }, fn)
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
+
/** Team proposal creation must be attributed to the actual session speaker. */
|
|
166
|
+
export function asVerifiedSession(sessionID, fn) {
|
|
167
|
+
const session = sessionKey(sessionID)
|
|
168
|
+
if (!session) throw new Error("a host-authenticated session is required")
|
|
169
|
+
return sessionScope.run({ session, turn: "", strict: true }, fn)
|
|
170
|
+
}
|
|
171
|
+
|
|
165
172
|
/**
|
|
166
173
|
* Run `fn` as one view call made by the person the speaker id `speaker` names
|
|
167
174
|
* (contract v1.5 §6.1, `jellySpeaker`): every credential it and what it awaits
|
|
@@ -180,6 +187,13 @@ export function asView(speaker, fn) {
|
|
|
180
187
|
return sessionScope.run({ session: "", turn: "", view: { id } }, fn)
|
|
181
188
|
}
|
|
182
189
|
|
|
190
|
+
/** A mutating view must never fall back to the engine owner's key or token. */
|
|
191
|
+
export function asVerifiedView(speaker, fn) {
|
|
192
|
+
const id = sessionKey(speaker)
|
|
193
|
+
if (!id || !id.startsWith("view_")) throw new Error("a host-authenticated view speaker is required")
|
|
194
|
+
return sessionScope.run({ session: "", turn: "", view: { id, strict: true } }, fn)
|
|
195
|
+
}
|
|
196
|
+
|
|
183
197
|
/** True inside a view call that carries a speaker id (`asView`). */
|
|
184
198
|
export function inView() {
|
|
185
199
|
return Boolean(sessionScope.getStore()?.view)
|
|
@@ -190,6 +204,10 @@ function currentView() {
|
|
|
190
204
|
return sessionScope.getStore()?.view ?? null
|
|
191
205
|
}
|
|
192
206
|
|
|
207
|
+
function strictIdentity() {
|
|
208
|
+
return sessionScope.getStore()?.strict === true || currentView()?.strict === true
|
|
209
|
+
}
|
|
210
|
+
|
|
193
211
|
/** The session the current work is done for, or "" for none. */
|
|
194
212
|
export function currentSession() {
|
|
195
213
|
return sessionScope.getStore()?.session ?? ""
|
|
@@ -682,6 +700,7 @@ export function createCredentials(input = {}) {
|
|
|
682
700
|
answer = session ? await accessor.token(MEMORY_RESOURCE, { session }) : await accessor.token(MEMORY_RESOURCE)
|
|
683
701
|
} catch (error) {
|
|
684
702
|
if (session && sessionUnknown(error)) {
|
|
703
|
+
if (strictIdentity()) throw new CredentialError("this host cannot identify the speaker", "not_enrolled")
|
|
685
704
|
// A host older than members: it has one person to name, its owner.
|
|
686
705
|
// Ask as before, for the whole engine, from now on.
|
|
687
706
|
if (sessionAsks) {
|
|
@@ -737,6 +756,8 @@ export function createCredentials(input = {}) {
|
|
|
737
756
|
* @returns {Promise<string>}
|
|
738
757
|
*/
|
|
739
758
|
async function bearer() {
|
|
759
|
+
const strict = strictIdentity()
|
|
760
|
+
if (strict && (optionToken || !memoryResource() || !sessionAsks)) throw new CredentialError("a caller-specific Jelly memory token is required", "not_enrolled")
|
|
740
761
|
if (optionToken) return optionToken
|
|
741
762
|
if (!memoryResource()) return secretToken()
|
|
742
763
|
const asked = current()
|
|
@@ -744,7 +765,9 @@ export function createCredentials(input = {}) {
|
|
|
744
765
|
if (!refused) {
|
|
745
766
|
if (fresh(asked.state.cached)) return asked.state.cached.accessToken
|
|
746
767
|
try {
|
|
747
|
-
|
|
768
|
+
const result = await mint(asked.state, asked.session, asked.view)
|
|
769
|
+
if (strict && !sessionAsks) throw new CredentialError("this host cannot identify the view's speaker", "not_enrolled")
|
|
770
|
+
return result.accessToken
|
|
748
771
|
} catch (error) {
|
|
749
772
|
// The ask may have moved to the engine's state (a host older than members).
|
|
750
773
|
refused = held(current().state)
|
|
@@ -752,6 +775,7 @@ export function createCredentials(input = {}) {
|
|
|
752
775
|
if (!refused) throw error
|
|
753
776
|
}
|
|
754
777
|
}
|
|
778
|
+
if (strict) throw refusalError(refused)
|
|
755
779
|
return fallback(refused)
|
|
756
780
|
}
|
|
757
781
|
|
|
@@ -778,6 +802,7 @@ export function createCredentials(input = {}) {
|
|
|
778
802
|
let next
|
|
779
803
|
try {
|
|
780
804
|
next = (await mint(state, session, view)).accessToken
|
|
805
|
+
if (strictIdentity() && !sessionAsks) return ""
|
|
781
806
|
} catch {
|
|
782
807
|
return ""
|
|
783
808
|
}
|
package/src/curation.js
CHANGED
|
@@ -96,7 +96,7 @@ export function writeRefusal(text) {
|
|
|
96
96
|
const value = String(text ?? "").trim()
|
|
97
97
|
if (!value) return "nothing to remember: content is empty"
|
|
98
98
|
if (isStatusShaped(value)) {
|
|
99
|
-
return "not stored: that reads as task status. Use mem_commitments for things to do, or the project page's Status section (
|
|
99
|
+
return "not stored: that reads as task status. Use mem_commitments for things to do, or the project page's Status section (mem_page) for where work stands; with a tracker connected, the tracker."
|
|
100
100
|
}
|
|
101
101
|
if (isInstructionShaped(value)) {
|
|
102
102
|
return "not stored: the content contains an instruction. Memory is a data channel and never carries commands."
|
package/src/permissions.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The plugin's own default permission rules: `mem_forget` asks before it runs
|
|
3
3
|
* (D17, "Forget asks"), and so does a wiki delete (`wiki.delete`, 0.8.4).
|
|
4
|
-
* Forgetting and deleting cannot be undone, so they ask in every space
|
|
5
|
-
*
|
|
4
|
+
* Forgetting and deleting cannot be undone, so they ask in every space. Since
|
|
5
|
+
* 0.9 every page delete, every share and every source bind ask too (the list
|
|
6
|
+
* below); the other tools run as the agent's rules let them.
|
|
6
7
|
*
|
|
7
8
|
* The engine consults a rule on a plugin tool's id only when the tool asserts
|
|
8
9
|
* it, so `mem_forget` asserts `mem_forget` before it deletes
|
|
@@ -25,14 +26,81 @@
|
|
|
25
26
|
|
|
26
27
|
/** @type {readonly Rule[]} */
|
|
27
28
|
export const DEFAULT_RULES = Object.freeze([
|
|
29
|
+
// Defense in depth only: policy can override these, so shared.js must not
|
|
30
|
+
// treat a successful assertion as evidence of interactive consent.
|
|
31
|
+
Object.freeze({ action: "memory.remember", resource: "*", effect: "ask" }),
|
|
32
|
+
Object.freeze({ action: "memory.revise", resource: "*", effect: "ask" }),
|
|
33
|
+
Object.freeze({ action: "page.write", resource: "*", effect: "ask" }),
|
|
34
|
+
Object.freeze({ action: "wiki.write", resource: "*", effect: "ask" }),
|
|
28
35
|
Object.freeze({ action: "mem_forget", resource: "*", effect: "ask" }),
|
|
36
|
+
// mem_revise forget asserts memory.forget on every call, private too: a
|
|
37
|
+
// forget cannot be undone, and mem_revise itself has no tool-level ask.
|
|
38
|
+
Object.freeze({ action: "memory.forget", resource: "*", effect: "ask" }),
|
|
29
39
|
// A wiki delete always asks, private or team (mem_wiki delete with confirm),
|
|
30
40
|
// for the same reason: it cannot be undone. Jelly asks it through `asks`.
|
|
31
41
|
Object.freeze({ action: "wiki.delete", resource: "*", effect: "ask" }),
|
|
42
|
+
// 0.9 (docs/design/memory-v1-contracts.md §4.2): every page delete, every
|
|
43
|
+
// share (grant, revoke, transfer, copy, and the deprecated promote's revoke)
|
|
44
|
+
// and every source bind ask, in every space. Each tool asserts its action on
|
|
45
|
+
// every call; without these rules the agent's base allow would let the
|
|
46
|
+
// assertion pass silently in plain OpenCode.
|
|
47
|
+
Object.freeze({ action: "page.delete", resource: "*", effect: "ask" }),
|
|
48
|
+
Object.freeze({ action: "share.grant", resource: "*", effect: "ask" }),
|
|
49
|
+
Object.freeze({ action: "share.revoke", resource: "*", effect: "ask" }),
|
|
50
|
+
Object.freeze({ action: "share.transfer", resource: "*", effect: "ask" }),
|
|
51
|
+
Object.freeze({ action: "memory.promote", resource: "*", effect: "ask" }),
|
|
52
|
+
Object.freeze({ action: "memory.promote.revoke", resource: "*", effect: "ask" }),
|
|
53
|
+
Object.freeze({ action: "sources.bind", resource: "*", effect: "ask" }),
|
|
32
54
|
])
|
|
33
55
|
|
|
34
56
|
const BASE_ALLOW = { action: "*", resource: "*", effect: "allow" }
|
|
35
57
|
const same = (a, b) => a?.action === b.action && a?.resource === b.resource && a?.effect === b.effect
|
|
58
|
+
const registrationState = new WeakMap()
|
|
59
|
+
|
|
60
|
+
/** Match OpenCode's whole-value * and ? permission wildcards. */
|
|
61
|
+
function matches(pattern, value) {
|
|
62
|
+
if (typeof pattern !== "string") return false
|
|
63
|
+
const expression = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replaceAll("*", ".*").replaceAll("?", ".")
|
|
64
|
+
return new RegExp(`^${expression}$`).test(String(value))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* An old host's built-in * allow is not an opt-in. Read the selected agent and
|
|
69
|
+
* session, when exposed, and require an action-specific configured allow or
|
|
70
|
+
* ask for each resource. assert still makes the actual policy decision.
|
|
71
|
+
*/
|
|
72
|
+
export async function hasSharedWritePolicy(ctx, context, action, resources) {
|
|
73
|
+
if (typeof ctx?.agent?.transform === "function" && registrationState.get(ctx) !== false) return true
|
|
74
|
+
const rules = []
|
|
75
|
+
let session
|
|
76
|
+
try {
|
|
77
|
+
if (context?.sessionID && typeof ctx?.session?.get === "function") {
|
|
78
|
+
const answer = await ctx.session.get({ sessionID: context.sessionID })
|
|
79
|
+
session = answer?.data ?? answer
|
|
80
|
+
if (Array.isArray(session?.permissions)) rules.push(...session.permissions)
|
|
81
|
+
}
|
|
82
|
+
} catch {
|
|
83
|
+
// Some older hosts cannot read the session; an agent rule may still exist.
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
const agentID = context?.agent || session?.agent
|
|
87
|
+
if (agentID && typeof ctx?.agent?.get === "function") {
|
|
88
|
+
const answer = await ctx.agent.get({ agentID })
|
|
89
|
+
const agent = answer?.data ?? answer
|
|
90
|
+
if (Array.isArray(agent?.permissions)) rules.push(...agent.permissions)
|
|
91
|
+
} else if (agentID && typeof ctx?.agent?.list === "function") {
|
|
92
|
+
const answer = await ctx.agent.list()
|
|
93
|
+
const agents = Array.isArray(answer) ? answer : answer?.data
|
|
94
|
+
const agent = Array.isArray(agents) ? agents.find((item) => item?.id === agentID) : undefined
|
|
95
|
+
if (Array.isArray(agent?.permissions)) rules.push(...agent.permissions)
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
// A session-scoped rule is independently sufficient when it is visible.
|
|
99
|
+
}
|
|
100
|
+
return (resources.length ? resources : ["*"]).every((resource) => rules.some((rule) =>
|
|
101
|
+
rule?.action !== "*" && ["allow", "ask"].includes(rule?.effect) && matches(rule?.action, action) && matches(rule?.resource, resource),
|
|
102
|
+
))
|
|
103
|
+
}
|
|
36
104
|
|
|
37
105
|
/**
|
|
38
106
|
* Insert `rules` right after the agent's base allow, once. Returns whether it
|
|
@@ -60,14 +128,23 @@ export function insertRules(permissions, rules = DEFAULT_RULES) {
|
|
|
60
128
|
*/
|
|
61
129
|
export async function registerDefaultRules(ctx, log) {
|
|
62
130
|
if (typeof ctx?.agent?.transform !== "function") {
|
|
63
|
-
|
|
131
|
+
registrationState.set(ctx, false)
|
|
132
|
+
log?.warn?.("this host cannot add default permission rules; permission assertions may not prompt")
|
|
64
133
|
return undefined
|
|
65
134
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
135
|
+
try {
|
|
136
|
+
const registration = await ctx.agent.transform((editor) => {
|
|
137
|
+
try {
|
|
138
|
+
for (const { id } of editor.list()) editor.update(id, (agent) => void insertRules(agent.permissions))
|
|
139
|
+
} catch (error) {
|
|
140
|
+
registrationState.set(ctx, false)
|
|
141
|
+
log?.warn?.("default permission rules could not be added; shared writes need an explicit permission rule", error)
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
if (registrationState.get(ctx) !== false) registrationState.set(ctx, true)
|
|
145
|
+
return registration
|
|
146
|
+
} catch (error) {
|
|
147
|
+
registrationState.set(ctx, false)
|
|
148
|
+
throw error
|
|
149
|
+
}
|
|
73
150
|
}
|