@joe-broadhead/opencode-mem 0.9.2 → 0.9.12
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 +1605 -0
- package/README.md +135 -43
- package/docs/agent-protocol.md +29 -28
- package/docs/permissions.md +148 -0
- package/package.json +277 -16
- package/skills/mem.pages/SKILL.md +35 -16
- package/skills/mem.sharing/SKILL.md +39 -6
- package/skills/mem.sources/SKILL.md +41 -10
- package/skills/mem.usage/SKILL.md +47 -16
- package/src/backend/records.js +413 -50
- package/src/backend/v3.js +15 -10
- package/src/call-shapes.js +18 -25
- package/src/capture-pause.js +136 -0
- package/src/capture.js +7 -4
- package/src/collector-service.js +212 -35
- package/src/collector.js +186 -47
- package/src/commands.js +37 -0
- package/src/compaction.js +4 -2
- package/src/config.js +162 -1
- package/src/connect.js +353 -0
- package/src/copies.js +28 -3
- package/src/credentials.js +35 -7
- package/src/curation.js +33 -14
- package/src/events.js +144 -36
- package/src/forget-purge.js +377 -0
- package/src/forgotten.js +356 -0
- package/src/hooks.js +9 -4
- package/src/index.js +25 -6
- package/src/permissions.js +116 -38
- package/src/protocol.js +21 -11
- package/src/records.js +4 -1
- package/src/runtime.js +207 -16
- package/src/shapes.js +63 -0
- package/src/sources.js +183 -12
- package/src/spaces.js +58 -1
- package/src/spool.js +442 -64
- package/src/stated.js +334 -0
- package/src/storable.js +477 -0
- package/src/tools/aliases.js +36 -147
- package/src/tools/commitments.js +69 -12
- package/src/tools/held-write.js +9 -1
- package/src/tools/index.js +29 -44
- package/src/tools/ingest.js +33 -128
- package/src/tools/memory.js +594 -901
- package/src/tools/page-sections.js +147 -0
- package/src/tools/page.js +314 -82
- package/src/tools/recall-browse.js +173 -0
- package/src/tools/recall.js +79 -19
- package/src/tools/share.js +494 -131
- package/src/tools/shared.js +97 -19
- package/src/tools/spaces.js +215 -104
- package/src/tools/status.js +31 -3
- package/src/tools/wiki.js +5 -13
- package/src/util.js +10 -20
- package/src/views/contracts.js +137 -6
- package/src/views/overdue.js +125 -0
- package/src/views/profile.js +154 -0
- package/src/views/records-reads.js +132 -33
- package/src/views/rpc.js +63 -12
- package/src/views/shape.js +49 -0
- package/views/commitments.html +31 -4
- package/views/home.html +22 -3
- package/views/memory.html +126 -24
- package/src/erase.js +0 -36
package/src/collector.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { errorMessage, isPlainObject, oneLine } from "./util.js"
|
|
2
|
-
import {
|
|
2
|
+
import { createHash } from "node:crypto"
|
|
3
|
+
import { ACCOUNT_NAME, DEFAULT_EVERY_MINUTES, SOURCE_REGISTRY, contractRefusal, rpcDefinition, sourceAllowRules, sourceEntry } from "./sources.js"
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* The memory collector (docs/design/memory-v1-contracts.md §4.4, §4.5).
|
|
@@ -17,9 +18,16 @@ import { contractRefusal, rpcDefinition, sourceEntry } from "./sources.js"
|
|
|
17
18
|
* OpenCode 2.2.5 gives plugins no schedule API (a plugin's `Context` has no
|
|
18
19
|
* schedule domain; `experimental.schedule.*` is on the host's SDK client
|
|
19
20
|
* only), so this is how plain OpenCode collects, and Jelly while awake.
|
|
20
|
-
* - **
|
|
21
|
-
*
|
|
22
|
-
*
|
|
21
|
+
* - **Jelly** declares a routine that calls `tools.mem.collect`
|
|
22
|
+
* (`jelly.routines`), which runs the same in-plugin collection.
|
|
23
|
+
*
|
|
24
|
+
* 0.9.7 (audit runtime #8): the host-schedule path is gone. It ran every
|
|
25
|
+
* source, on any account, in one broad session (`ses_mem_collect`), which
|
|
26
|
+
* would undo the per-binding sessions. No OpenCode release up to 2.2.6 gives a
|
|
27
|
+
* plugin `ctx.experimental.schedule` (its `experimental` holds `terminal`,
|
|
28
|
+
* `generate` and `artifact`), so nothing reached it; it is removed so a host
|
|
29
|
+
* that adds the API does not reach it either. `collectCheckCode` and
|
|
30
|
+
* `createCollector({scheduler})` stay as pure units; nothing wires a scheduler.
|
|
23
31
|
*
|
|
24
32
|
* Every `events_list` answer must carry `source` and `account` (§4.5); one
|
|
25
33
|
* without them is refused with the release its plugin needs, and the binding
|
|
@@ -35,7 +43,7 @@ export const PAGES_PER_SOURCE = 5
|
|
|
35
43
|
export const PAGE_LIMIT = 200
|
|
36
44
|
/** The account a binding without one uses. */
|
|
37
45
|
export const DEFAULT_ACCOUNT = "default"
|
|
38
|
-
/** The
|
|
46
|
+
/** The prefix of the collector's reserved sessions: each binding's is `ses_mem_collect_<source>_<hash>`. */
|
|
39
47
|
export const COLLECT_SESSION = "ses_mem_collect"
|
|
40
48
|
/**
|
|
41
49
|
* The engine sends a schedule's prompt only after its check fails three times
|
|
@@ -43,22 +51,64 @@ export const COLLECT_SESSION = "ses_mem_collect"
|
|
|
43
51
|
*/
|
|
44
52
|
export const COLLECT_FAILURE_PROMPT =
|
|
45
53
|
"The memory-collect schedule's check failed repeatedly, so the engine paused it. Reply with the single word: paused."
|
|
54
|
+
const BASE_RULES = [
|
|
55
|
+
{ action: "*", resource: "*", effect: "deny" },
|
|
56
|
+
{ action: "execute", resource: "*", effect: "allow" },
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
const freezeRules = (rules) => Object.freeze(rules.map((rule) => Object.freeze({ ...rule })))
|
|
60
|
+
|
|
46
61
|
/**
|
|
47
|
-
* The collector
|
|
48
|
-
*
|
|
49
|
-
*
|
|
62
|
+
* The collector sessions' rules: deny everything, then allow Code Mode and
|
|
63
|
+
* exactly what the registry says each source's `events_list` asserts
|
|
64
|
+
* (`sourceAllowRules`), never a generic `<ns>.read`.
|
|
65
|
+
*
|
|
66
|
+
* `collectSessionRules({source, account})`: one binding's session, which the
|
|
67
|
+
* in-plugin collector and a bind's probe both use. It allows the source's
|
|
68
|
+
* events action, the exact inner reads its events_list asserts (OpenCode
|
|
69
|
+
* checks each against this session: S0, 0.9.11), and for gws the Gmail
|
|
70
|
+
* operations of its EVENT_READS on the binding's own account. There is no
|
|
71
|
+
* session for every source at once (0.9.7).
|
|
50
72
|
*/
|
|
51
|
-
export
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
73
|
+
export function collectSessionRules({ source, account }) {
|
|
74
|
+
const entry = sourceEntry(source)
|
|
75
|
+
return freezeRules([...BASE_RULES, ...(entry ? sourceAllowRules(entry, account || DEFAULT_ACCOUNT) : [])])
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The reserved session of one binding. Its id comes from the binding (source
|
|
80
|
+
* and account), not from its rules: when the registry changes the rules, the
|
|
81
|
+
* same session is updated to them (`rulesHash` in its metadata).
|
|
82
|
+
*/
|
|
83
|
+
export function collectSessionId({ source, account }) {
|
|
84
|
+
const hash = createHash("sha256").update(`${source}\u0000${account || DEFAULT_ACCOUNT}`).digest("hex").slice(0, 12)
|
|
85
|
+
return `${COLLECT_SESSION}_${source}_${hash}`
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A short hash of a session's rules, kept in its metadata so a stale session is noticed and updated. */
|
|
89
|
+
export function rulesHash(rules) {
|
|
90
|
+
const canonical = JSON.stringify(rules.map(({ action, resource, effect }) => [action, resource, effect]))
|
|
91
|
+
return createHash("sha256").update(canonical).digest("hex").slice(0, 16)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Match OpenCode's whole-value * and ? permission wildcards. */
|
|
95
|
+
function wildcard(pattern, value) {
|
|
96
|
+
const expression = String(pattern).replace(/[.+^${}()|[\]\\]/g, "\\$&").replaceAll("*", ".*").replaceAll("?", ".")
|
|
97
|
+
return new RegExp(`^${expression}$`).test(String(value))
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The effect a session's rules give an action on a resource: the last
|
|
102
|
+
* matching rule wins, as in OpenCode's permission evaluation.
|
|
103
|
+
*/
|
|
104
|
+
export function sessionEffect(action, resource = "*", rules = []) {
|
|
105
|
+
if (Array.isArray(resource)) return sessionEffect(action, "*", resource)
|
|
106
|
+
if (!Array.isArray(rules) || rules.length === 0) return "deny"
|
|
107
|
+
return rules.findLast((rule) => wildcard(rule.action, action) && wildcard(rule.resource, resource))?.effect ?? "ask"
|
|
108
|
+
}
|
|
58
109
|
|
|
59
110
|
/** A source name is a lowercase identifier; only registered sources are collected. */
|
|
60
111
|
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
112
|
const IDENTIFIER = /^[a-zA-Z][a-zA-Z0-9_]*$/
|
|
63
113
|
|
|
64
114
|
export function isSourceName(value) {
|
|
@@ -99,12 +149,22 @@ export function collectableSources(sources, { only } = {}) {
|
|
|
99
149
|
}
|
|
100
150
|
|
|
101
151
|
/** The input every `events_list` call to a binding sends: the page, its account, and the registry's input. */
|
|
152
|
+
/**
|
|
153
|
+
* The account an events_list call names: a named binding's, and for a source
|
|
154
|
+
* whose reads are scoped by account (`namesAccount`, gws) "default" too, so
|
|
155
|
+
* the plugin reads exactly the account the session's rules allow.
|
|
156
|
+
*/
|
|
157
|
+
function accountInput(entry, account) {
|
|
158
|
+
const named = account || DEFAULT_ACCOUNT
|
|
159
|
+
return named !== DEFAULT_ACCOUNT || entry?.namesAccount ? { account: named } : {}
|
|
160
|
+
}
|
|
161
|
+
|
|
102
162
|
export function listInput(binding, cursor) {
|
|
103
163
|
const entry = sourceEntry(binding.source)
|
|
104
164
|
return {
|
|
105
165
|
...(cursor ? { cursor } : {}),
|
|
106
166
|
limit: PAGE_LIMIT,
|
|
107
|
-
...(
|
|
167
|
+
...accountInput(entry, binding.account),
|
|
108
168
|
...(entry?.input ?? {}),
|
|
109
169
|
}
|
|
110
170
|
}
|
|
@@ -160,33 +220,6 @@ export function collectCheckCode(sources) {
|
|
|
160
220
|
].join("\n")
|
|
161
221
|
}
|
|
162
222
|
|
|
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
223
|
function codeOf(row) {
|
|
191
224
|
return typeof row?.if?.code === "string" ? row.if.code : typeof row?.check?.code === "string" ? row.check.code : ""
|
|
192
225
|
}
|
|
@@ -212,6 +245,8 @@ export class SourceContractError extends Error {
|
|
|
212
245
|
*/
|
|
213
246
|
export function readEventsAnswer(value, entry) {
|
|
214
247
|
let answer = value
|
|
248
|
+
// A wrapper's text beside its output: a source's own words, such as Slack's "No Slack channel is enabled".
|
|
249
|
+
const said = isPlainObject(answer) && isPlainObject(answer.output) && typeof (answer.content ?? answer.text) === "string" ? String(answer.content ?? answer.text) : ""
|
|
215
250
|
if (isPlainObject(answer) && !Array.isArray(answer.events) && isPlainObject(answer.output)) answer = answer.output
|
|
216
251
|
if (isPlainObject(answer) && !Array.isArray(answer.events) && typeof (answer.content ?? answer.text) === "string") answer = answer.content ?? answer.text
|
|
217
252
|
if (typeof answer === "string") {
|
|
@@ -234,7 +269,55 @@ export function readEventsAnswer(value, entry) {
|
|
|
234
269
|
events: answer.events,
|
|
235
270
|
cursor: typeof answer.cursor === "string" ? answer.cursor : "",
|
|
236
271
|
more: answer.more === true,
|
|
272
|
+
setup: setupNote(answer, said),
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const SETUP_WORDS = /\b(?:is|are) (?:not )?(?:enabled|configured|set)\b|\bset the plugin option\b|\bsign in\b|\bnot configured\b/i
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* The source's own sentence for setup it still needs, or null: the contract's
|
|
280
|
+
* `setup` field (§4.5), else a wrapper's text when nothing came back and it
|
|
281
|
+
* reads as a setup step (Slack 0.0.8 says "No Slack channel is enabled for
|
|
282
|
+
* events_list: set the plugin option events.channels …" only in its text).
|
|
283
|
+
*/
|
|
284
|
+
export function setupNote(answer, said = "") {
|
|
285
|
+
const own = typeof answer?.setup === "string" ? answer.setup.trim() : ""
|
|
286
|
+
if (own) return oneLine(own, 400)
|
|
287
|
+
if (said && Array.isArray(answer?.events) && answer.events.length === 0 && SETUP_WORDS.test(said)) return oneLine(said.split(/(?<=\.)\s/)[0], 400)
|
|
288
|
+
return null
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** The input of the owner-only call a bind request makes (§4.5): `limit: 0` where the plugin keeps it, else `limit: 1`. */
|
|
292
|
+
export function probeInput(entry, account) {
|
|
293
|
+
return { limit: entry.ownerProbe === "limit0" ? 0 : 1, ...accountInput(entry, account) }
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** How often the timer collects a source, in milliseconds. */
|
|
297
|
+
export function cadenceMs(source) {
|
|
298
|
+
return Math.max(5, Number(sourceEntry(source)?.everyMinutes) || DEFAULT_EVERY_MINUTES) * 60_000
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Bind requests the collector answers this run: asked for by the person
|
|
303
|
+
* (state `requested`, or `failed` once its `retry_at` has passed), of a
|
|
304
|
+
* registered source. A request that needs attention (3 failures) is not
|
|
305
|
+
* asked again until the person presses Bind… again. Nothing else is ever probed.
|
|
306
|
+
*/
|
|
307
|
+
export function requestedSources(requests, { only, now = Date.now() } = {}) {
|
|
308
|
+
const out = []
|
|
309
|
+
for (const entry of Array.isArray(requests) ? requests : []) {
|
|
310
|
+
if (!isPlainObject(entry)) continue
|
|
311
|
+
if (entry.state !== "requested" && entry.state !== "failed") continue
|
|
312
|
+
// A failed request waits out its back-off (1 h, then 6 h); "attention" (3 failures) waits for Bind… again.
|
|
313
|
+
if (entry.state === "failed" && entry.retry_at && Date.parse(entry.retry_at) > now) continue
|
|
314
|
+
const source = String(entry.source ?? "")
|
|
315
|
+
const account = String(entry.account ?? "") || DEFAULT_ACCOUNT
|
|
316
|
+
if (!sourceEntry(source) || !isAccountName(account)) continue
|
|
317
|
+
if (only && source !== only) continue
|
|
318
|
+
out.push({ source, account })
|
|
237
319
|
}
|
|
320
|
+
return out
|
|
238
321
|
}
|
|
239
322
|
|
|
240
323
|
/**
|
|
@@ -259,7 +342,7 @@ export function hostSourceLister(ctx) {
|
|
|
259
342
|
* The collector's core: schedule upkeep, one collection, and the owners a
|
|
260
343
|
* refused ingest reported.
|
|
261
344
|
*
|
|
262
|
-
* @param {{ scheduler?:
|
|
345
|
+
* @param {{ scheduler?: { list(): Promise<any[]>, create(input: any): Promise<any>, remove(id: string): Promise<void> } | null, lister?: ReturnType<typeof hostSourceLister>, log?: any }} [input]
|
|
263
346
|
*/
|
|
264
347
|
export function createCollector({ scheduler = null, lister = null, log } = {}) {
|
|
265
348
|
const reported = new Map()
|
|
@@ -322,10 +405,15 @@ export function createCollector({ scheduler = null, lister = null, log } = {}) {
|
|
|
322
405
|
* ingest: (input: { source: string, account: string, owner: any, events: unknown[], cursor: string }) => Promise<{ accepted?: number, duplicates?: number, refused?: number, cursor_saved?: boolean }>,
|
|
323
406
|
* sessionID?: string,
|
|
324
407
|
* results?: object[],
|
|
408
|
+
* due?: (binding: { source: string, account: string }) => boolean,
|
|
325
409
|
* }} input
|
|
326
410
|
*/
|
|
327
|
-
async function runOnce({ sources, only, cursor, ingest, sessionID, results = [] }) {
|
|
411
|
+
async function runOnce({ sources, only, cursor, ingest, sessionID, results = [], due = () => true }) {
|
|
328
412
|
for (const binding of collectableSources(sources, { only })) {
|
|
413
|
+
if (!due(binding)) {
|
|
414
|
+
results.push({ ...binding, pages: 0, accepted: 0, duplicates: 0, refused: 0, more: false, notDue: true, everyMinutes: cadenceMs(binding.source) / 60_000, done: true })
|
|
415
|
+
continue
|
|
416
|
+
}
|
|
329
417
|
const result = { ...binding, pages: 0, accepted: 0, duplicates: 0, refused: 0, more: false, done: false }
|
|
330
418
|
results.push(result)
|
|
331
419
|
try {
|
|
@@ -333,6 +421,7 @@ export function createCollector({ scheduler = null, lister = null, log } = {}) {
|
|
|
333
421
|
result.error = "this host gives plugins no way to call another plugin's events_list"
|
|
334
422
|
continue
|
|
335
423
|
}
|
|
424
|
+
const session = typeof sessionID === "function" ? await sessionID(binding) : sessionID
|
|
336
425
|
const state = await cursor(binding)
|
|
337
426
|
if (!state || state.enabled === false || (state.status && state.status !== "confirmed")) {
|
|
338
427
|
result.error = `the binding is ${state?.enabled === false ? "disabled" : state?.status || "unknown"}`
|
|
@@ -340,12 +429,13 @@ export function createCollector({ scheduler = null, lister = null, log } = {}) {
|
|
|
340
429
|
}
|
|
341
430
|
let at = typeof state.cursor === "string" && state.cursor ? state.cursor : undefined
|
|
342
431
|
for (let page = 0; page < PAGES_PER_SOURCE; page += 1) {
|
|
343
|
-
const answer = await lister(binding.source, listInput(binding, at),
|
|
432
|
+
const answer = await lister(binding.source, listInput(binding, at), session)
|
|
344
433
|
if (!answer || !answer.cursor) {
|
|
345
434
|
result.error = "the source's events_list gave no events and cursor"
|
|
346
435
|
break
|
|
347
436
|
}
|
|
348
437
|
// The answer's own source and account (§4.5): the binding they name decides.
|
|
438
|
+
if (answer.setup) result.setup = answer.setup
|
|
349
439
|
const saved = await ingest({ source: answer.source, account: answer.account, owner: answer.owner, events: answer.events, cursor: answer.cursor })
|
|
350
440
|
result.pages += 1
|
|
351
441
|
result.accepted += Number(saved?.accepted) || 0
|
|
@@ -368,6 +458,54 @@ export function createCollector({ scheduler = null, lister = null, log } = {}) {
|
|
|
368
458
|
return results
|
|
369
459
|
}
|
|
370
460
|
|
|
461
|
+
/**
|
|
462
|
+
* Answer the person's bind requests: for each requested source only, ask
|
|
463
|
+
* its `events_list` for the signed-in account (`probeInput`; any event it
|
|
464
|
+
* returns is dropped, never stored) and report it, or the source's own
|
|
465
|
+
* reason it could not, with `report`. Nothing is bound here: the person
|
|
466
|
+
* confirms the reported account.
|
|
467
|
+
*
|
|
468
|
+
* @param {{ requests: unknown, only?: string, sessionID?: string, report: (input: object) => Promise<unknown>, probes?: object[] }} input
|
|
469
|
+
*/
|
|
470
|
+
async function probeRequests({ requests, only, now = Date.now(), sessionID, report, probes = [] }) {
|
|
471
|
+
for (const asked of requestedSources(requests, { only, now })) {
|
|
472
|
+
const probe = { ...asked, reported: false }
|
|
473
|
+
probes.push(probe)
|
|
474
|
+
const entry = sourceEntry(asked.source)
|
|
475
|
+
// The binding's own session first: when it cannot be made, nothing is read and
|
|
476
|
+
// nothing is reported (it is not the source's failure, so no back-off either).
|
|
477
|
+
let session
|
|
478
|
+
try {
|
|
479
|
+
session = typeof sessionID === "function" ? await sessionID(asked) : sessionID
|
|
480
|
+
} catch (error) {
|
|
481
|
+
probe.error = oneLine(errorMessage(error), 300)
|
|
482
|
+
continue
|
|
483
|
+
}
|
|
484
|
+
let body
|
|
485
|
+
try {
|
|
486
|
+
if (!lister) throw new Error("this host gives plugins no way to call another plugin's events_list")
|
|
487
|
+
const answer = await lister(asked.source, probeInput(entry, asked.account), session)
|
|
488
|
+
if (answer?.owner?.id) {
|
|
489
|
+
const owner = { id: oneLine(answer.owner.id, 300), label: oneLine(answer.owner.label ?? answer.owner.email ?? answer.owner.id, 300) }
|
|
490
|
+
body = { source: asked.source, account: asked.account, owner, reported_account: answer.account, ...(answer.setup || entry.setupOptions ? { note: answer.setup || entry.setupOptions } : {}) }
|
|
491
|
+
probe.owner = owner
|
|
492
|
+
} else {
|
|
493
|
+
body = { source: asked.source, account: asked.account, note: answer?.setup || `${entry.package}'s events_list named no signed-in account; sign in to it in its own plugin.` }
|
|
494
|
+
}
|
|
495
|
+
} catch (error) {
|
|
496
|
+
body = { source: asked.source, account: asked.account, note: oneLine(errorMessage(error), 400) }
|
|
497
|
+
}
|
|
498
|
+
probe.note = body.note ?? null
|
|
499
|
+
try {
|
|
500
|
+
await report(body)
|
|
501
|
+
probe.reported = true
|
|
502
|
+
} catch (error) {
|
|
503
|
+
probe.error = oneLine(errorMessage(error), 300)
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return probes
|
|
507
|
+
}
|
|
508
|
+
|
|
371
509
|
return {
|
|
372
510
|
get scheduled() {
|
|
373
511
|
return Boolean(scheduler)
|
|
@@ -377,5 +515,6 @@ export function createCollector({ scheduler = null, lister = null, log } = {}) {
|
|
|
377
515
|
ownerFor,
|
|
378
516
|
sync,
|
|
379
517
|
runOnce,
|
|
518
|
+
probeRequests,
|
|
380
519
|
}
|
|
381
520
|
}
|
package/src/commands.js
CHANGED
|
@@ -95,6 +95,7 @@ export async function registerCommands(ctx, runtime) {
|
|
|
95
95
|
}
|
|
96
96
|
if (status.collection) lines.push(`- collection: ${oneLine(status.collection, 120)}`)
|
|
97
97
|
lines.push(
|
|
98
|
+
`- capture: ${status.capture ?? "?"}`,
|
|
98
99
|
`- credential: ${status.credential}`,
|
|
99
100
|
`- L0 conversations: ${status.layers?.l0 ?? "?"}`,
|
|
100
101
|
`- L1 atoms: ${status.layers?.l1 ?? "?"}`,
|
|
@@ -144,6 +145,42 @@ export async function registerCommands(ctx, runtime) {
|
|
|
144
145
|
},
|
|
145
146
|
})
|
|
146
147
|
|
|
148
|
+
// Pause and resume touch only this machine's state, so they work even
|
|
149
|
+
// while memory is unavailable: nothing is sent.
|
|
150
|
+
target.add({
|
|
151
|
+
name: "mem-pause",
|
|
152
|
+
description: "Pause memory capture for this session: no turn, compaction summary or checkpoint is kept until /mem-resume",
|
|
153
|
+
execute: async ({ sessionID } = {}) => {
|
|
154
|
+
if (!sessionID) return
|
|
155
|
+
if (!runtime.config.capture) {
|
|
156
|
+
await reply(sessionID, "capture is already off for this install (capture: false); nothing from this session is kept automatically.")
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
const since = runtime.pauseCapture(sessionID)
|
|
160
|
+
await reply(
|
|
161
|
+
sessionID,
|
|
162
|
+
`memory capture paused for this session since ${since}: no turn, compaction summary or checkpoint is kept. mem_remember and mem_page still store what you ask for. /mem-resume captures new turns again; this session gets no checkpoint or compaction summary, because they cover the whole transcript.`,
|
|
163
|
+
)
|
|
164
|
+
},
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
target.add({
|
|
168
|
+
name: "mem-resume",
|
|
169
|
+
description: "Resume memory capture for this session after /mem-pause",
|
|
170
|
+
execute: async ({ sessionID } = {}) => {
|
|
171
|
+
if (!sessionID) return
|
|
172
|
+
const resumed = runtime.resumeCapture(sessionID)
|
|
173
|
+
await reply(
|
|
174
|
+
sessionID,
|
|
175
|
+
resumed === "unreadable"
|
|
176
|
+
? "memory capture stays paused: the pause state file (capture-paused.json in the state directory) is unreadable, so every session counts as paused. Fix or remove it, then /mem-resume."
|
|
177
|
+
: resumed
|
|
178
|
+
? "memory capture resumed for this session: new turns are captured again. Turns from the pause are never captured, and this session gets no checkpoint or compaction summary."
|
|
179
|
+
: "memory capture was not paused for this session; nothing changed.",
|
|
180
|
+
)
|
|
181
|
+
},
|
|
182
|
+
})
|
|
183
|
+
|
|
147
184
|
editor.add({
|
|
148
185
|
name: "mem-why",
|
|
149
186
|
description: "Show the exact memory block pinned into this session (dashboard, Due list, persona)",
|
package/src/compaction.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import os from "node:os"
|
|
2
|
+
import { storable } from "./storable.js"
|
|
2
3
|
import { FENCE_TAGS, clampText, neutralizeFenceClosers, nowIso, oneLine, redactSecrets, shortHash } from "./util.js"
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -32,12 +33,13 @@ const DECISION_PATTERN =
|
|
|
32
33
|
const OPEN_PATTERN =
|
|
33
34
|
/\b(next|todos?|open questions?|pending|blocked|follow[- ]?ups?|remaining|still need|unresolved|awaiting)\b/i
|
|
34
35
|
|
|
35
|
-
//
|
|
36
|
+
// Kept for callers that import it from here; the checkpoint itself uses storable.
|
|
36
37
|
export { redactSecrets }
|
|
37
38
|
|
|
39
|
+
/** As every automatic write (src/storable.js): secret lines withheld, emails and paths masked. */
|
|
38
40
|
function sanitize(value, max) {
|
|
39
41
|
if (typeof value !== "string" && typeof value !== "number") return ""
|
|
40
|
-
return clampText(neutralizeFenceClosers(
|
|
42
|
+
return clampText(neutralizeFenceClosers(storable(String(value), "checkpoint").text, FENCE_TAGS), max).trim()
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
function field(value, max) {
|
package/src/config.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import fs from "node:fs"
|
|
1
2
|
import os from "node:os"
|
|
2
3
|
import path from "node:path"
|
|
3
4
|
import { isPlainObject, toPositiveInt } from "./util.js"
|
|
5
|
+
import { ASK_ACTIONS } from "./permissions.js"
|
|
4
6
|
|
|
5
7
|
/** The bundled simulator: the core URL of local direct mode. */
|
|
6
8
|
export const LOCAL_BASE_URL = "http://127.0.0.1:8420"
|
|
@@ -45,6 +47,16 @@ const DEFAULTS = Object.freeze({
|
|
|
45
47
|
tools: true,
|
|
46
48
|
/** Opt-in only for hosts that expose the held-write Inbox decisions. */
|
|
47
49
|
heldTeamWrites: false,
|
|
50
|
+
/**
|
|
51
|
+
* Default asks the person turned off: approval action names (ASK_ACTIONS in
|
|
52
|
+
* src/permissions.js), as a list or one comma-separated string, such as
|
|
53
|
+
* "memory.forget, page.delete". Each one then runs without an assertion. In
|
|
54
|
+
* plain OpenCode a `permission` rule does the same; in Jelly a person's
|
|
55
|
+
* Allow cannot lift a declared ask (D77), so this is the way to loosen one.
|
|
56
|
+
* A held team write still waits in the Inbox. Unknown names are ignored,
|
|
57
|
+
* with a warning.
|
|
58
|
+
*/
|
|
59
|
+
skipAsks: [],
|
|
48
60
|
skills: true,
|
|
49
61
|
commands: true,
|
|
50
62
|
spool: true,
|
|
@@ -62,6 +74,18 @@ const DEFAULTS = Object.freeze({
|
|
|
62
74
|
recallChars: 6000,
|
|
63
75
|
timeoutMs: 2000,
|
|
64
76
|
recordsWriteTimeoutMs: 10000,
|
|
77
|
+
/**
|
|
78
|
+
* Timeout of one turn capture on the gateway's `/v1/capture`. Longer than a
|
|
79
|
+
* read's: a capture that times out is sent again, and only the gateway's
|
|
80
|
+
* receipt keeps the retry from storing it twice (docs/capture-delivery.md).
|
|
81
|
+
*/
|
|
82
|
+
captureTimeoutMs: 10000,
|
|
83
|
+
/**
|
|
84
|
+
* Retries of one keyed records write after an unknown outcome, 0 to 5
|
|
85
|
+
* (docs/design/records-idempotency.md). Sent only to a records service that
|
|
86
|
+
* advertises replay; 0 sends the key but never retries.
|
|
87
|
+
*/
|
|
88
|
+
recordsWriteRetries: 2,
|
|
65
89
|
retries: 1,
|
|
66
90
|
|
|
67
91
|
/**
|
|
@@ -95,6 +119,8 @@ const ENV_MAP = {
|
|
|
95
119
|
timeZone: "MEMORY_TIME_ZONE",
|
|
96
120
|
collect: "MEMORY_COLLECT",
|
|
97
121
|
recordsWriteTimeoutMs: "MEMORY_RECORDS_WRITE_TIMEOUT_MS",
|
|
122
|
+
captureTimeoutMs: "MEMORY_CAPTURE_TIMEOUT_MS",
|
|
123
|
+
recordsWriteRetries: "MEMORY_RECORDS_WRITE_RETRIES",
|
|
98
124
|
}
|
|
99
125
|
|
|
100
126
|
const BOOLEAN_KEYS = [
|
|
@@ -112,7 +138,7 @@ const BOOLEAN_KEYS = [
|
|
|
112
138
|
"debug",
|
|
113
139
|
]
|
|
114
140
|
|
|
115
|
-
const NUMBER_KEYS = ["bootstrapChars", "recallLimit", "recallChars", "timeoutMs", "recordsWriteTimeoutMs", "retries", "collectEveryMinutes"]
|
|
141
|
+
const NUMBER_KEYS = ["bootstrapChars", "recallLimit", "recallChars", "timeoutMs", "recordsWriteTimeoutMs", "captureTimeoutMs", "retries", "collectEveryMinutes"]
|
|
116
142
|
|
|
117
143
|
/** An IANA zone this runtime knows, or "". */
|
|
118
144
|
function validTimeZone(value) {
|
|
@@ -148,6 +174,125 @@ export function createClock(config, started = Date.now()) {
|
|
|
148
174
|
* @param {Record<string, unknown>} [options]
|
|
149
175
|
* @param {Record<string, string | undefined>} [env]
|
|
150
176
|
*/
|
|
177
|
+
/** `skipAsks` as given: a list, or one string separated by commas or spaces. */
|
|
178
|
+
function parseAskList(value) {
|
|
179
|
+
const items = Array.isArray(value) ? value : typeof value === "string" ? value.split(/[\s,]+/) : []
|
|
180
|
+
return [...new Set(items.map((item) => String(item ?? "").trim()).filter(Boolean))]
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The known action names of a `skipAsks` value, and the others. */
|
|
184
|
+
function splitAsks(value) {
|
|
185
|
+
const named = parseAskList(value)
|
|
186
|
+
return { names: Object.freeze(named.filter((name) => ASK_ACTIONS.includes(name))), unknown: Object.freeze(named.filter((name) => !ASK_ACTIONS.includes(name))) }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** JSON with comments and trailing commas (opencode.jsonc), parsed; strings keep their slashes. */
|
|
190
|
+
function parseJsonc(text) {
|
|
191
|
+
let out = ""
|
|
192
|
+
let inString = false
|
|
193
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
194
|
+
const char = text[i]
|
|
195
|
+
if (inString) {
|
|
196
|
+
out += char
|
|
197
|
+
if (char === "\\") {
|
|
198
|
+
out += text[i + 1] ?? ""
|
|
199
|
+
i += 1
|
|
200
|
+
} else if (char === '"') inString = false
|
|
201
|
+
continue
|
|
202
|
+
}
|
|
203
|
+
if (char === '"') {
|
|
204
|
+
inString = true
|
|
205
|
+
out += char
|
|
206
|
+
} else if (char === "/" && text[i + 1] === "/") {
|
|
207
|
+
while (i < text.length && text[i] !== "\n") i += 1
|
|
208
|
+
out += "\n"
|
|
209
|
+
} else if (char === "/" && text[i + 1] === "*") {
|
|
210
|
+
i += 2
|
|
211
|
+
while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) i += 1
|
|
212
|
+
i += 1
|
|
213
|
+
} else out += char
|
|
214
|
+
}
|
|
215
|
+
return JSON.parse(out.replace(/,(\s*[}\]])/g, "$1"))
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** True when a config's plugin spec names this package: its name, a version or tag of it, or a directory holding it. */
|
|
219
|
+
function namesThisPlugin(spec, configDir, packageName) {
|
|
220
|
+
const value = String(spec ?? "").trim()
|
|
221
|
+
if (!value) return false
|
|
222
|
+
if (value === packageName || value.startsWith(`${packageName}@`)) return true
|
|
223
|
+
if (/^(?:github:)?joe-broadhead\/opencode-mem(?:#.*)?$/.test(value)) return true
|
|
224
|
+
if (!/^(?:\/|\.|~)/.test(value)) return false
|
|
225
|
+
try {
|
|
226
|
+
const dir = path.resolve(configDir, value.replace(/^~(?=\/)/, os.homedir()))
|
|
227
|
+
return JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8"))?.name === packageName
|
|
228
|
+
} catch {
|
|
229
|
+
return false
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* `skipAsks` from the user-level OpenCode config only:
|
|
235
|
+
* `$XDG_CONFIG_HOME/opencode/opencode.json(c)`, else `~/.config/opencode/`.
|
|
236
|
+
* OpenCode merges a repository's own opencode.json over it and adds its
|
|
237
|
+
* plugin entries, so a project file could otherwise turn asks off for anyone
|
|
238
|
+
* who opens the repository. Neither the environment nor OPENCODE_CONFIG is read.
|
|
239
|
+
*/
|
|
240
|
+
export function userLevelSkipAsks(where = {}) {
|
|
241
|
+
const names = []
|
|
242
|
+
for (const options of userLevelOptions(where)) if (options.skipAsks !== undefined) names.push(...parseAskList(options.skipAsks))
|
|
243
|
+
return splitAsks(names)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** The options of every entry naming this plugin in the user-level OpenCode config. */
|
|
247
|
+
function userLevelOptions({ env = process.env, home = os.homedir(), packageName = "@joe-broadhead/opencode-mem" } = {}) {
|
|
248
|
+
const dir = path.join(env.XDG_CONFIG_HOME || path.join(home, ".config"), "opencode")
|
|
249
|
+
const found = []
|
|
250
|
+
for (const file of ["opencode.json", "opencode.jsonc"]) {
|
|
251
|
+
let document
|
|
252
|
+
try {
|
|
253
|
+
document = parseJsonc(fs.readFileSync(path.join(dir, file), "utf8"))
|
|
254
|
+
} catch {
|
|
255
|
+
continue
|
|
256
|
+
}
|
|
257
|
+
const entries = [...(Array.isArray(document?.plugins) ? document.plugins : []), ...(Array.isArray(document?.plugin) ? document.plugin : [])]
|
|
258
|
+
for (const entry of entries) {
|
|
259
|
+
const spec = Array.isArray(entry) ? entry[0] : isPlainObject(entry) ? entry.package ?? entry.name : entry
|
|
260
|
+
const options = Array.isArray(entry) ? entry[1] : isPlainObject(entry) ? entry.options : undefined
|
|
261
|
+
if (isPlainObject(options) && namesThisPlugin(spec, dir, packageName)) found.push(options)
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return found
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Where `skipAsks` comes from (REVIEW-SMOOTH): inside Jelly, the person's
|
|
269
|
+
* "Run without asking" setting, which reaches the plugin as an option; else
|
|
270
|
+
* the user-level OpenCode config only. Read on every use, because Jelly's
|
|
271
|
+
* accessors may appear after setup.
|
|
272
|
+
*
|
|
273
|
+
* @param {any} config resolveConfig's answer
|
|
274
|
+
* @param {{ inJelly: () => boolean, user?: { names: readonly string[], unknown: readonly string[] } }} source
|
|
275
|
+
*/
|
|
276
|
+
export function applySkipAsksSource(config, { inJelly, user = userLevelSkipAsks() }) {
|
|
277
|
+
const fromOptions = config.optionSkipAsks ?? Object.freeze([])
|
|
278
|
+
const optionUnknown = config.unknownSkipAsks ?? Object.freeze([])
|
|
279
|
+
const jelly = () => {
|
|
280
|
+
try {
|
|
281
|
+
return Boolean(inJelly?.())
|
|
282
|
+
} catch {
|
|
283
|
+
return false
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
Object.defineProperty(config, "skipAsks", { get: () => (jelly() ? fromOptions : user.names), enumerable: true, configurable: true })
|
|
287
|
+
Object.defineProperty(config, "unknownSkipAsks", { get: () => (jelly() ? optionUnknown : user.unknown), enumerable: false, configurable: true })
|
|
288
|
+
Object.defineProperty(config, "ignoredSkipAsks", {
|
|
289
|
+
get: () => (jelly() ? Object.freeze([]) : Object.freeze(fromOptions.filter((name) => !user.names.includes(name)))),
|
|
290
|
+
enumerable: false,
|
|
291
|
+
configurable: true,
|
|
292
|
+
})
|
|
293
|
+
return config
|
|
294
|
+
}
|
|
295
|
+
|
|
151
296
|
export function resolveConfig(options = {}, env = process.env) {
|
|
152
297
|
const input = isPlainObject(options) ? options : {}
|
|
153
298
|
const backend = isPlainObject(input.backend) ? input.backend : {}
|
|
@@ -179,7 +324,20 @@ export function resolveConfig(options = {}, env = process.env) {
|
|
|
179
324
|
merged[key] = toPositiveInt(merged[key], DEFAULTS[key])
|
|
180
325
|
}
|
|
181
326
|
merged.retries = Math.min(merged.retries, 5)
|
|
327
|
+
{
|
|
328
|
+
const value = Number(merged.recordsWriteRetries)
|
|
329
|
+
merged.recordsWriteRetries = Number.isInteger(value) && value >= 0 ? Math.min(value, 5) : DEFAULTS.recordsWriteRetries
|
|
330
|
+
}
|
|
182
331
|
merged.collectEveryMinutes = Math.min(Math.max(merged.collectEveryMinutes, 5), 1440)
|
|
332
|
+
{
|
|
333
|
+
// What the plugin options say. Outside Jelly the plugin uses the
|
|
334
|
+
// user-level config's value instead (applySkipAsksSource), because a
|
|
335
|
+
// repository's own opencode.json can set plugin options too.
|
|
336
|
+
const split = splitAsks(merged.skipAsks)
|
|
337
|
+
merged.skipAsks = split.names
|
|
338
|
+
Object.defineProperty(merged, "optionSkipAsks", { value: split.names, enumerable: false, writable: true, configurable: true })
|
|
339
|
+
Object.defineProperty(merged, "unknownSkipAsks", { value: split.unknown, enumerable: false, writable: true, configurable: true })
|
|
340
|
+
}
|
|
183
341
|
|
|
184
342
|
for (const key of ["baseURL", "controlURL", "knowledgeURL"]) {
|
|
185
343
|
if (!merged[key]) continue
|
|
@@ -244,5 +402,8 @@ export function configWarnings(config, { credentialSource = config.token ? "opti
|
|
|
244
402
|
warnings.push("compactionCheckpoint=false; no pre-compaction checkpoint is written to memory")
|
|
245
403
|
if (config.captureSubagents)
|
|
246
404
|
warnings.push("captureSubagents=true; subagent sessions are captured and pinned like primary sessions")
|
|
405
|
+
if (config.skipAsks?.length) warnings.push(`skipAsks=${config.skipAsks.join(",")}; these run without asking (a held team write still waits in the Inbox)`)
|
|
406
|
+
if (config.ignoredSkipAsks?.length) warnings.push(`skipAsks ${config.ignoredSkipAsks.join(", ")} ignored: outside Jelly it is read only from the user-level config (~/.config/opencode/opencode.json), never from a project's`)
|
|
407
|
+
if (config.unknownSkipAsks?.length) warnings.push(`skipAsks ignores ${config.unknownSkipAsks.join(", ")}: not one of ${ASK_ACTIONS.join(", ")}`)
|
|
247
408
|
return warnings
|
|
248
409
|
}
|