@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/tools/spaces.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { contextScopeLabel } from "../scope.js"
|
|
2
2
|
import { DEFAULT_ACCOUNT, isAccountName, isSourceName } from "../collector.js"
|
|
3
3
|
import { knownSources, sourceEntry } from "../sources.js"
|
|
4
|
+
import { createProfileReads, profileText } from "../views/profile.js"
|
|
4
5
|
import { errorMessage, isPlainObject, oneLine } from "../util.js"
|
|
5
6
|
import { RECORDS_MISSING, recordsFailure, recordsOf } from "./share.js"
|
|
6
7
|
import { renderHealth } from "./status.js"
|
|
7
8
|
|
|
8
|
-
export const SPACES_ACTIONS = Object.freeze(["current", "list", "members", "access", "health", "sources", "review"])
|
|
9
|
+
export const SPACES_ACTIONS = Object.freeze(["current", "list", "members", "access", "health", "sources", "review", "profile"])
|
|
9
10
|
const DECISIONS = Object.freeze(["accept", "reject", "edit"])
|
|
10
11
|
const REVIEW_LIMIT = 20
|
|
11
12
|
|
|
@@ -18,9 +19,10 @@ const REVIEW_LIMIT = 20
|
|
|
18
19
|
* - `health`: the backend, the layers, the records service's counts and the
|
|
19
20
|
* collector (it absorbs `mem_status`).
|
|
20
21
|
* - `sources`: the funnel's bindings (memory-funnel-v1.md §10). Listing reads
|
|
21
|
-
* them; `bind`
|
|
22
|
-
*
|
|
23
|
-
*
|
|
22
|
+
* them; `bind` asks the person's collector for the signed-in account
|
|
23
|
+
* (`sources.request`) and answers with the console link where the person
|
|
24
|
+
* confirms it (0.9.7, owner decision D1: no agent binds); `unbind` previews
|
|
25
|
+
* unless `confirm`, then stops collection and deletes the source's events.
|
|
24
26
|
* - `review`: the routines' proposals; `decide` accepts, edits or rejects one.
|
|
25
27
|
*
|
|
26
28
|
* @param {any} editor
|
|
@@ -31,7 +33,7 @@ export function registerSpaces(editor, toolkit) {
|
|
|
31
33
|
editor.add({
|
|
32
34
|
name: "spaces",
|
|
33
35
|
description:
|
|
34
|
-
"
|
|
36
|
+
"Where memory lives and what flows in: spaces, members, access, health, sources, review. current: the active space and whether this session is bound to it; with bind {space} it binds this session to that space (\"personal\" for your personal space), only when the person asks; a team space asks the person first: the binding is where recall without a space, the pinned block and team capture go. Passing space to any tool never binds. list: every space you can use. members: who is in a space, with user ids. access: your role and the grants that name you. health: whether memory is reachable, what each layer holds, and whether the collector runs; use it when memory itself may be down, not to explain an ordinary recall miss. Do not inspect spaces or health to verify a successful read or write; use the answer already returned. sources: the accounts Memory collects from (mail, chat, meetings); bind one only when the person asks: it asks the person's collector which account the source is signed in to and answers with the console link where the person confirms it (no agent binds; nothing is collected until they do); unbind previews unless confirm is true, then stops collection and deletes that source's events. review: proposals the daily routine made; decide accepts, edits or rejects one, only when the person says (in a shared space it asks). current, list, members and access need a control plane; health, sources and review work without one. It says who can see what, never what the rules are: \"am I allowed\" is a mem_recall question. Never bind an account or decide a proposal the person did not ask for. Membership and roles are the control plane's: this tool never changes them.",
|
|
35
37
|
input: {
|
|
36
38
|
type: "object",
|
|
37
39
|
properties: {
|
|
@@ -39,17 +41,18 @@ export function registerSpaces(editor, toolkit) {
|
|
|
39
41
|
type: "string",
|
|
40
42
|
enum: [...SPACES_ACTIONS],
|
|
41
43
|
description:
|
|
42
|
-
'Required. Read-only: current, list, members, access, health. sources and review read unless given bind, unbind or decide. Example: {"action": "health"}',
|
|
44
|
+
'Required. Read-only: current, list, members, access, health, profile (what Memory holds about the person: persona and scenarios, as data). sources and review read unless given bind, unbind or decide. Example: {"action": "health"}',
|
|
43
45
|
},
|
|
44
46
|
space: { type: "string", description: "Space id (control plane) for members, access, health, sources and review; defaults to the session's space (sources: your personal space). Never rebinds the session" },
|
|
45
47
|
bind: {
|
|
46
48
|
type: "object",
|
|
47
|
-
description:
|
|
49
|
+
description:
|
|
50
|
+
'current: bind this session to a space, only when the person asks. Example: {"action": "current", "bind": {"space": "personal"}}. sources: ask to collect from one source; the person confirms the account in the Memory console, from the link the answer gives. Example: {"action": "sources", "bind": {"source": "gmail", "account": "work"}}',
|
|
48
51
|
properties: {
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
space: { type: "string", description: 'current: "personal" or a space id from list' },
|
|
53
|
+
source: { type: "string", description: "sources: the source plugin's Code Mode namespace, such as gmail or slack" },
|
|
54
|
+
account: { type: "string", description: 'sources: the account within it; default "default"' },
|
|
51
55
|
},
|
|
52
|
-
required: ["source"],
|
|
53
56
|
additionalProperties: false,
|
|
54
57
|
},
|
|
55
58
|
unbind: {
|
|
@@ -68,8 +71,8 @@ export function registerSpaces(editor, toolkit) {
|
|
|
68
71
|
description: 'review: answer one proposal. Example: {"action": "review", "decide": {"id": "rec_...", "decision": "reject", "reason": "vendor newsletters are noise"}}',
|
|
69
72
|
properties: {
|
|
70
73
|
id: { type: "string", description: "The proposal id review listed" },
|
|
71
|
-
decision: { type: "string", enum: [...DECISIONS], description: "accept applies it; edit applies payload instead; reject drops it
|
|
72
|
-
reason: { type: "string", description: "Why;
|
|
74
|
+
decision: { type: "string", enum: [...DECISIONS], description: "accept applies it; edit applies payload instead; reject drops it" },
|
|
75
|
+
reason: { type: "string", description: "Why; kept on the proposal, never a memory" },
|
|
73
76
|
payload: { type: "string", description: "edit: the text to apply instead of the proposal's" },
|
|
74
77
|
},
|
|
75
78
|
required: ["id", "decision"],
|
|
@@ -87,8 +90,7 @@ export function registerSpaces(editor, toolkit) {
|
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
/**
|
|
90
|
-
* The spaces actions
|
|
91
|
-
* `mem_grants` and `mem_members`.
|
|
93
|
+
* The spaces actions behind `mem_spaces`.
|
|
92
94
|
*
|
|
93
95
|
* @param {ReturnType<import("./shared.js").createToolkit>} toolkit
|
|
94
96
|
*/
|
|
@@ -97,12 +99,69 @@ export function createSpacesActions(toolkit) {
|
|
|
97
99
|
const records = () => recordsOf(toolkit)
|
|
98
100
|
const noControl = (what) => text(`control plane not configured; set MEMORY_CONTROL_URL to ${what}`)
|
|
99
101
|
|
|
100
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Bind this session to a space: the one way a session's binding changes.
|
|
104
|
+
* The target is resolved first without the session, so a session bound to
|
|
105
|
+
* an archived space can still be moved, and the ask can name the space.
|
|
106
|
+
* Binding to the personal space only narrows and does not ask; any other
|
|
107
|
+
* space asks `session.bind` with what the binding changes. The answer must
|
|
108
|
+
* report the session bound to the target: a gateway before 0.9.7 drops
|
|
109
|
+
* `bind`, and then nothing was bound.
|
|
110
|
+
*/
|
|
111
|
+
async function bindSession(bind, context) {
|
|
112
|
+
const target = String(bind?.space ?? "").trim()
|
|
113
|
+
if (!target) return { message: 'bind needs a space: {"action": "current", "bind": {"space": "personal"}} or a space id from {"action": "list"}.' }
|
|
114
|
+
if (!context?.sessionID) return { message: "there is no session to bind." }
|
|
115
|
+
const own = await toolkit.resolveContext({ agent: context?.agent })
|
|
116
|
+
const personalId = own?.personal_space_id || (own?.space_kind === "personal" ? own.space_id : "")
|
|
117
|
+
const spaceId = target === "personal" ? personalId : target
|
|
118
|
+
if (!spaceId) return { error: "the control plane returned no personal space" }
|
|
119
|
+
const targetContext = spaceId === own?.space_id ? own : await toolkit.resolveContext({ space: spaceId, agent: context?.agent })
|
|
120
|
+
if (!targetContext?.space_id) return { error: "the control plane returned no space context" }
|
|
121
|
+
const narrows = targetContext.space_kind === "personal" && targetContext.space_id === personalId
|
|
122
|
+
if (!narrows) {
|
|
123
|
+
try {
|
|
124
|
+
await toolkit.assert({
|
|
125
|
+
sessionID: context.sessionID,
|
|
126
|
+
agent: context?.agent,
|
|
127
|
+
source: { type: "tool", messageID: context?.messageID, id: context?.id },
|
|
128
|
+
action: "session.bind",
|
|
129
|
+
resources: [targetContext.space_id],
|
|
130
|
+
save: [],
|
|
131
|
+
metadata: {
|
|
132
|
+
space: targetContext.space_id,
|
|
133
|
+
space_name: oneLine(targetContext.space_name || targetContext.space_id, 200),
|
|
134
|
+
space_kind: targetContext.space_kind || "unknown",
|
|
135
|
+
changes: `This session's recall without a space, its pinned block, team capture and team writes without a space would use ${oneLine(targetContext.space_name || targetContext.space_id, 120)} until it is bound again; passing space to a call is unaffected.`,
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
} catch (error) {
|
|
139
|
+
return { message: `not bound (approval): ${errorMessage(error)}. The session keeps its binding.` }
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const bound = await toolkit.resolveContext({ space: spaceId, sessionID: context.sessionID, agent: context?.agent, bind: true })
|
|
143
|
+
if (!bound?.space_id) return { error: "the control plane returned no space context" }
|
|
144
|
+
if (bound.space_id !== spaceId || bound.session_space_id !== spaceId) {
|
|
145
|
+
return {
|
|
146
|
+
message: `not bound: the server answered without binding this session to ${oneLine(spaceId, 80)}. An older Memory gateway (before 0.9.7) drops the bind; ask the operator to upgrade it. Meanwhile pass space on each call.`,
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return { bound }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function current(input, context) {
|
|
101
153
|
if (!control) return noControl("use spaces")
|
|
154
|
+
let boundLine = ""
|
|
155
|
+
if (input?.bind !== undefined) {
|
|
156
|
+
const result = await bindSession(input.bind, context)
|
|
157
|
+
if (result.message) return text(result.message)
|
|
158
|
+
if (result.error) return fail(result.error)
|
|
159
|
+
boundLine = `bound: this session now uses ${describeSpace(result.bound)}`
|
|
160
|
+
}
|
|
102
161
|
const spaceContext = await toolkit.resolveContext({ sessionID: context?.sessionID, agent: context?.agent })
|
|
103
162
|
if (!spaceContext) return fail("the control plane returned no space context")
|
|
104
163
|
const scoped = toolkit.scopeFor(spaceContext, { sessionID: context?.sessionID, agent: context?.agent })
|
|
105
|
-
const lines = ["<space_context>", `space: ${describeSpace(spaceContext)}`]
|
|
164
|
+
const lines = ["<space_context>", ...(boundLine ? [boundLine] : []), `space: ${describeSpace(spaceContext)}`]
|
|
106
165
|
const collection = describeCollection(spaceContext)
|
|
107
166
|
if (collection) lines.push(`collection: ${collection}`)
|
|
108
167
|
if (spaceContext.role) lines.push(`role: ${spaceContext.role}`)
|
|
@@ -202,6 +261,7 @@ export function createSpacesActions(toolkit) {
|
|
|
202
261
|
} else spaceContext = await toolkit.runtime.sessionContext(context?.sessionID, context?.agent)
|
|
203
262
|
if (!spaceContext?.space_id) return { error: "the control plane returned no space context" }
|
|
204
263
|
return {
|
|
264
|
+
context: spaceContext,
|
|
205
265
|
where: requested || personalDefault ? { space: spaceContext.space_id } : { sessionID: context?.sessionID, space: spaceContext.space_id },
|
|
206
266
|
label: describeSpace(spaceContext),
|
|
207
267
|
shared: spaceContext.space_kind === "shared",
|
|
@@ -231,8 +291,27 @@ export function createSpacesActions(toolkit) {
|
|
|
231
291
|
}
|
|
232
292
|
|
|
233
293
|
async function listSources(where) {
|
|
294
|
+
return (await listAll(where)).sources
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Bindings and the person's open bind requests (a request made in the console, or here). */
|
|
298
|
+
async function listAll(where) {
|
|
234
299
|
const answer = await records().call("sources.list", {}, where)
|
|
235
|
-
return
|
|
300
|
+
return {
|
|
301
|
+
sources: Array.isArray(answer?.sources) ? answer.sources.filter(isPlainObject) : [],
|
|
302
|
+
requests: Array.isArray(answer?.requests) ? answer.requests.filter(isPlainObject) : [],
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const describeRequest = (row) => {
|
|
307
|
+
const name = `${oneLine(row.source, 64)}:${oneLine(row.account || DEFAULT_ACCOUNT, 128)}`
|
|
308
|
+
const asked = row.requested_at ? ` (asked ${oneLine(row.requested_at, 40)})` : ""
|
|
309
|
+
if (row.state === "reported" && row.owner?.id) {
|
|
310
|
+
return `- ${name} bind requested${asked}: the collector reports it is signed in as ${oneLine(row.owner.label || row.owner.id, 120)}; the person confirms it in ${consoleAt("sources")}${row.note ? `. The source says: ${oneLine(row.note, 240)}` : ""}`
|
|
311
|
+
}
|
|
312
|
+
if (row.state === "attention") return `- ${name} bind requested${asked}: needs attention: the collector could not read the account ${Number(row.failures) || 3} times${row.note ? ` (${oneLine(row.note, 240)})` : ""}; it stopped trying. Bind… again in the console once the source is signed in`
|
|
313
|
+
if (row.state === "failed") return `- ${name} bind requested${asked}: the collector could not learn the account${row.note ? ` (${oneLine(row.note, 240)})` : ""}; it tries again${row.retry_at ? ` after ${oneLine(row.retry_at, 40)}` : " on its next run"}`
|
|
314
|
+
return `- ${name} bind requested${asked}: waiting for the collector's next run to report the signed-in account`
|
|
236
315
|
}
|
|
237
316
|
|
|
238
317
|
/** Keep a host schedule (where there is one) in step with the sources; one line on how collection runs. */
|
|
@@ -252,32 +331,51 @@ export function createSpacesActions(toolkit) {
|
|
|
252
331
|
: "Collection: the collection timer is off (collect: false); /mem-collect collects now."
|
|
253
332
|
}
|
|
254
333
|
|
|
334
|
+
/** Memory's console page for an area, or how to find it when no gateway is configured. */
|
|
335
|
+
function consoleAt(area) {
|
|
336
|
+
const url = config?.controlURL ?? toolkit.runtime?.config?.controlURL
|
|
337
|
+
try {
|
|
338
|
+
return `${new URL(String(url ?? "")).origin}/console/${area}`
|
|
339
|
+
} catch {
|
|
340
|
+
return `Memory's console (${area[0].toUpperCase()}${area.slice(1)})`
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
255
344
|
/**
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
345
|
+
* `sources` with `bind` (0.9.7, owner decision D1): the person confirms
|
|
346
|
+
* whose account Memory collects in a browser, in the console; no agent
|
|
347
|
+
* confirms it, and the records service refuses `sources.bind` from any key
|
|
348
|
+
* or token. Here the plugin only asks: it records a bind request when there
|
|
349
|
+
* is none (`sources.request`, which reads and collects nothing; the
|
|
350
|
+
* person's collector then reports the signed-in account), and answers with
|
|
351
|
+
* the console link where the person confirms it.
|
|
261
352
|
*/
|
|
262
|
-
async function
|
|
263
|
-
const
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
}
|
|
353
|
+
async function bindSource(value, space, rows, requests) {
|
|
354
|
+
const asked = bindingOf(value, { known: true })
|
|
355
|
+
if (asked.error) return `not bound: ${asked.error}`
|
|
356
|
+
const name = `${asked.source}${asked.asked ? `:${asked.account}` : ""}`
|
|
357
|
+
const label = asked.source
|
|
358
|
+
const link = consoleAt("sources")
|
|
359
|
+
const matches = (row, ...accounts) => row.source === asked.source && (!asked.asked || accounts.some((account) => (account || DEFAULT_ACCOUNT) === asked.account))
|
|
360
|
+
const bound = rows.find((row) => matches(row, row.account) && row.status === "confirmed" && (row.enabled === true || row.enabled === 1))
|
|
361
|
+
if (bound) return `${asked.source}:${oneLine(bound.account || DEFAULT_ACCOUNT, 128)} is already collecting from ${oneLine(bound.owner_label || bound.owner_id || "its owner", 120)}. Nothing changed.`
|
|
362
|
+
const confirm = `Confirming whose account Memory collects is the person's own act, in a browser: open ${link} and choose Confirm for ${label}. Nothing is collected until they do; no agent and no key can confirm it.`
|
|
363
|
+
const open = requests.find((row) => matches(row, row.account, row.reported_account))
|
|
364
|
+
if (open?.state === "reported" && open.owner?.id) {
|
|
365
|
+
return `not bound here: the person's collector reports ${name} is signed in as ${oneLine(open.owner.label || open.owner.id, 120)} (${oneLine(open.owner.id, 120)}). ${confirm}`
|
|
366
|
+
}
|
|
367
|
+
if (open) {
|
|
368
|
+
const why = open.state === "attention" || open.state === "failed" ? ` The collector could not read the account yet${open.note ? ` (${oneLine(open.note, 240)})` : ""}.` : ""
|
|
369
|
+
return `not bound here: a bind request for ${name} is open; the person's collector reports the signed-in account on its next run.${why} ${confirm}`
|
|
370
|
+
}
|
|
371
|
+
try {
|
|
372
|
+
await records().call("sources.request", { source: asked.source, account: asked.account }, space.where)
|
|
373
|
+
} catch (error) {
|
|
374
|
+
log.warn("sources request failed", error)
|
|
375
|
+
return `not asked: ${recordsFailure(error)}`
|
|
275
376
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
const pending = rows.find((row) => row.source === binding.source && (!binding.asked || (row.account || DEFAULT_ACCOUNT) === binding.account) && row.owner_id)
|
|
279
|
-
if (pending) return { owner: { id: pending.owner_id, label: pending.owner_label || pending.owner_id }, source: binding.source, account: pending.account || DEFAULT_ACCOUNT, from: "the binding the records service holds" }
|
|
280
|
-
return { notes }
|
|
377
|
+
log.info("sources request", { source: asked.source })
|
|
378
|
+
return `not bound here: asked. On its next run (every 30 minutes while OpenCode is open, or now with mem_collect when the person asks), the person's collector reads only which account ${label} is signed in to. ${confirm}`
|
|
281
379
|
}
|
|
282
380
|
|
|
283
381
|
async function sources(input, context) {
|
|
@@ -287,68 +385,56 @@ export function createSpacesActions(toolkit) {
|
|
|
287
385
|
if (space.message) return text(space.message)
|
|
288
386
|
if (space.error) return fail(space.error)
|
|
289
387
|
let rows
|
|
388
|
+
let requests
|
|
290
389
|
try {
|
|
291
|
-
rows = await
|
|
390
|
+
;({ sources: rows, requests } = await listAll(space.where))
|
|
292
391
|
} catch (error) {
|
|
293
392
|
return fail(recordsFailure(error))
|
|
294
393
|
}
|
|
295
394
|
|
|
395
|
+
// A source belongs to a personal space: a bind request, or its cancel, in
|
|
396
|
+
// a shared space is refused here (the records service refuses it too). An
|
|
397
|
+
// unbind there deletes the team's events, so it goes through the one gate
|
|
398
|
+
// and asks sources.unbind.
|
|
399
|
+
const personalOnly = (verb) => `not ${verb}: sources belong to your personal space, where they collect the person's own mail and chat; ${space.label} is shared. Nothing changed.`
|
|
400
|
+
const gateUnbind = async (binding, deletes) => (await toolkit.gateWrite(context, {
|
|
401
|
+
action: "sources.unbind",
|
|
402
|
+
spaceContext: space.context,
|
|
403
|
+
shared: space.shared,
|
|
404
|
+
resources: [`${binding.source}:${binding.account}`],
|
|
405
|
+
metadata: { space: space.spaceId, source: binding.source, account: binding.account, deletes },
|
|
406
|
+
})).denied
|
|
407
|
+
|
|
296
408
|
if (input?.bind) {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const known = await ownerOf(asked, rows, context)
|
|
300
|
-
if (known.refused) return text(`not bound: ${known.refused}`)
|
|
301
|
-
if (!known.owner) {
|
|
302
|
-
const why = known.notes?.length ? ` (${known.notes.join("; ")})` : ""
|
|
303
|
-
return text(
|
|
304
|
-
`not bound: Memory could not learn which account ${asked.source}${asked.asked ? `:${asked.account}` : ""} is signed in to${why}. Sign in to that source in its own plugin, then try again; Memory shows the account and asks the person before anything is collected.`,
|
|
305
|
-
)
|
|
306
|
-
}
|
|
307
|
-
const owner = { id: oneLine(known.owner.id, 300), label: oneLine(known.owner.label ?? known.owner.email ?? known.owner.id, 300) }
|
|
308
|
-
// The binding is the one the source's own answer names (§4.5).
|
|
309
|
-
const binding = { source: known.source, account: known.account }
|
|
310
|
-
try {
|
|
311
|
-
await toolkit.assert({
|
|
312
|
-
sessionID: context?.sessionID,
|
|
313
|
-
agent: context?.agent,
|
|
314
|
-
source: { type: "tool", messageID: context?.messageID, id: context?.id },
|
|
315
|
-
action: "sources.bind",
|
|
316
|
-
resources: [`${binding.source}:${binding.account}`],
|
|
317
|
-
save: [],
|
|
318
|
-
metadata: { space: space.spaceId ?? "local", source: binding.source, account: binding.account, owner: `${owner.label} (${owner.id})`, from: known.from },
|
|
319
|
-
})
|
|
320
|
-
} catch (error) {
|
|
321
|
-
return text(`not bound (approval): ${errorMessage(error)}`)
|
|
322
|
-
}
|
|
323
|
-
try {
|
|
324
|
-
await records().call("sources.bind", { source: binding.source, account: binding.account, owner, enabled: true }, space.where)
|
|
325
|
-
} catch (error) {
|
|
326
|
-
log.warn("sources bind failed", error)
|
|
327
|
-
return fail(`${error?.outcomeUnknown ? "bind outcome unknown" : "not bound"}: ${recordsFailure(error)}`)
|
|
328
|
-
}
|
|
329
|
-
let after = rows
|
|
330
|
-
try {
|
|
331
|
-
after = await listSources(space.where)
|
|
332
|
-
} catch {
|
|
333
|
-
/* the schedule is brought in step at the next list */
|
|
334
|
-
}
|
|
335
|
-
const sync = await syncCollector(after)
|
|
336
|
-
log.info("sources bind", { source: binding.source })
|
|
337
|
-
return text(
|
|
338
|
-
`bound ${binding.source}:${binding.account} in ${space.label} to ${owner.label} (${owner.id}), as the person confirmed. Memory accepts its events only while that account is the one signed in. Raw events stay private to this space for 30 days; the daily routine turns them into journal pages and proposals for review.${sync ? ` ${sync}` : ""}`,
|
|
339
|
-
)
|
|
409
|
+
if (space.shared) return text(personalOnly("bound"))
|
|
410
|
+
return text(await bindSource(input.bind, space, rows, requests))
|
|
340
411
|
}
|
|
341
412
|
|
|
342
413
|
if (input?.unbind) {
|
|
343
414
|
const binding = bindingOf(input.unbind)
|
|
344
415
|
if (binding.error) return text(`not unbound: ${binding.error}`)
|
|
345
416
|
const row = rows.find((entry) => entry.source === binding.source && (entry.account || DEFAULT_ACCOUNT) === binding.account)
|
|
417
|
+
const request = requests.find((entry) => entry.source === binding.source && (entry.account || DEFAULT_ACCOUNT) === binding.account)
|
|
418
|
+
if (!row && request) {
|
|
419
|
+
if (space.shared) return text(personalOnly("cancelled"))
|
|
420
|
+
// Only a bind request: nothing was collected, so cancelling it deletes nothing.
|
|
421
|
+
try {
|
|
422
|
+
await records().call("sources.cancel", { source: binding.source, account: binding.account }, space.where)
|
|
423
|
+
} catch (error) {
|
|
424
|
+
return fail(`not cancelled: ${recordsFailure(error)}`)
|
|
425
|
+
}
|
|
426
|
+
// No session keeps this binding's allow rules once nobody asks for it.
|
|
427
|
+
await toolkit.collector?.forgetSession?.({ source: binding.source, account: binding.account })
|
|
428
|
+
return text(`cancelled the bind request for ${binding.source}:${binding.account} in ${space.label}; nothing had been collected from it.`)
|
|
429
|
+
}
|
|
346
430
|
if (!row) return text(`not unbound: ${binding.source}:${binding.account} is not a source in ${space.label}.`)
|
|
347
431
|
if (input?.confirm !== true) {
|
|
348
432
|
return text(
|
|
349
433
|
`preview, nothing changed: unbinding ${binding.source}:${binding.account} (${oneLine(row.owner_label || row.owner_id || "owner unknown", 120)}) stops its collection and deletes every event collected from it in ${space.label}. Journal pages and memories already made from them stay. Call again with "confirm": true to unbind.`,
|
|
350
434
|
)
|
|
351
435
|
}
|
|
436
|
+
const denied = await gateUnbind(binding, `stops collecting ${binding.source}:${binding.account} and deletes every event collected from it in ${oneLine(space.context?.space_name || space.spaceId || "this space", 120)}; pages and memories already made from them stay.`)
|
|
437
|
+
if (denied) return text(`not unbound (approval): ${denied}`)
|
|
352
438
|
let answer
|
|
353
439
|
try {
|
|
354
440
|
answer = await records().call("sources.unbind", { source: binding.source, account: binding.account }, space.where)
|
|
@@ -356,6 +442,7 @@ export function createSpacesActions(toolkit) {
|
|
|
356
442
|
log.warn("sources unbind failed", error)
|
|
357
443
|
return fail(`${error?.outcomeUnknown ? "unbind outcome unknown" : "not unbound"}: ${recordsFailure(error)}`)
|
|
358
444
|
}
|
|
445
|
+
await toolkit.collector?.forgetSession?.({ source: binding.source, account: binding.account })
|
|
359
446
|
let after = rows.filter((entry) => entry !== row)
|
|
360
447
|
try {
|
|
361
448
|
after = await listSources(space.where)
|
|
@@ -370,8 +457,9 @@ export function createSpacesActions(toolkit) {
|
|
|
370
457
|
|
|
371
458
|
const sync = await syncCollector(rows)
|
|
372
459
|
const lines = ["<sources>", `space: ${space.label}`]
|
|
373
|
-
if (rows.length === 0) lines.push('(no sources: Memory collects nothing. When the person asks, bind one with {"action": "sources", "bind": {"source": "<namespace>"}}.)')
|
|
460
|
+
if (rows.length === 0 && requests.length === 0) lines.push('(no sources: Memory collects nothing. When the person asks, bind one with {"action": "sources", "bind": {"source": "<namespace>"}}.)')
|
|
374
461
|
for (const row of rows) lines.push(describeBinding(row))
|
|
462
|
+
for (const row of requests) lines.push(describeRequest(row))
|
|
375
463
|
lines.push("</sources>")
|
|
376
464
|
if (toolkit.collector) lines.push(toolkit.collector.describe())
|
|
377
465
|
else if (sync) lines.push(sync)
|
|
@@ -401,32 +489,38 @@ export function createSpacesActions(toolkit) {
|
|
|
401
489
|
if (decision === "edit" && !payload) return text("not decided: an edit needs payload, the text to apply instead.")
|
|
402
490
|
const reason = String(input.decide.reason ?? "").trim()
|
|
403
491
|
// A decision in a shared space changes what every member reads, so it
|
|
404
|
-
// asks, as
|
|
405
|
-
|
|
492
|
+
// goes through the one gate and asks, as a revise does.
|
|
493
|
+
const metadata = { space: space.spaceId, proposal: id, decision, ...(reason ? { reason: oneLine(reason, 300) } : {}), ...(payload ? { payload: oneLine(payload, 300) } : {}) }
|
|
494
|
+
const gate = await toolkit.gateWrite(context, { action: "memory.revise", spaceContext: space.context, shared: space.shared, resources: [id], metadata })
|
|
495
|
+
if (gate.denied) return text(`not decided (approval): ${gate.denied}`)
|
|
496
|
+
const decide = (extra = {}) => records().call("review.decide", { id, decision, ...(payload ? { payload } : {}), ...(reason ? { reason } : {}), ...extra }, space.where)
|
|
497
|
+
let answer
|
|
498
|
+
try {
|
|
406
499
|
try {
|
|
407
|
-
await
|
|
408
|
-
sessionID: context?.sessionID,
|
|
409
|
-
agent: context?.agent,
|
|
410
|
-
source: { type: "tool", messageID: context?.messageID, id: context?.id },
|
|
411
|
-
action: "memory.revise",
|
|
412
|
-
resources: [id],
|
|
413
|
-
save: [],
|
|
414
|
-
metadata: { space: space.spaceId, proposal: id, decision, ...(reason ? { reason: oneLine(reason, 300) } : {}), ...(payload ? { payload: oneLine(payload, 300) } : {}) },
|
|
415
|
-
})
|
|
500
|
+
answer = await decide()
|
|
416
501
|
} catch (error) {
|
|
417
|
-
|
|
502
|
+
// Whether the decision writes the pinned `index` is the store's to
|
|
503
|
+
// say, from what it would write (slice 7 review P2). It refuses a
|
|
504
|
+
// bearer without confirm_pinned and rolls back; ask, then repeat.
|
|
505
|
+
if (error?.code !== "pinned_confirmation_required") throw error
|
|
506
|
+
const pinned = await toolkit.gateWrite(context, {
|
|
507
|
+
action: "page.write",
|
|
508
|
+
spaceContext: space.context,
|
|
509
|
+
shared: space.shared,
|
|
510
|
+
pinned: true,
|
|
511
|
+
resources: ["index", id],
|
|
512
|
+
metadata: { ...metadata, page: "index" },
|
|
513
|
+
})
|
|
514
|
+
if (pinned.denied) return text(`not decided (approval): ${pinned.denied}`)
|
|
515
|
+
answer = await decide({ confirm_pinned: true })
|
|
418
516
|
}
|
|
419
|
-
}
|
|
420
|
-
let answer
|
|
421
|
-
try {
|
|
422
|
-
answer = await records().call("review.decide", { id, decision, ...(payload ? { payload } : {}), ...(reason ? { reason } : {}) }, space.where)
|
|
423
517
|
} catch (error) {
|
|
424
518
|
log.warn("review decide failed", error)
|
|
425
519
|
return fail(`${error?.outcomeUnknown ? "decision outcome unknown" : "not decided"}: ${recordsFailure(error)}`)
|
|
426
520
|
}
|
|
427
521
|
const applied = answer?.applied
|
|
428
522
|
const appliedText = isPlainObject(applied) && applied.id ? ` Applied as ${oneLine(applied.id, 40)}${applied.path ? ` (${oneLine(applied.path, 120)})` : ""}.` : ""
|
|
429
|
-
const learned = decision === "reject" && reason ? " The reason
|
|
523
|
+
const learned = decision === "reject" && reason ? " The reason is kept on the proposal only, not as a memory." : ""
|
|
430
524
|
return text(`${decision === "reject" ? "rejected" : decision === "edit" ? "edited and applied" : "accepted"} proposal ${oneLine(id, 40)} in ${space.label}.${appliedText}${learned}`)
|
|
431
525
|
}
|
|
432
526
|
|
|
@@ -445,17 +539,34 @@ export function createSpacesActions(toolkit) {
|
|
|
445
539
|
return text(lines.join("\n"))
|
|
446
540
|
}
|
|
447
541
|
|
|
542
|
+
/** What Memory holds about the person: the views' `profile.read`, as text. Personal space only; read-only. */
|
|
543
|
+
async function profile(context) {
|
|
544
|
+
const resolved = await toolkit.resolvePrivateSpace({}, context)
|
|
545
|
+
if (resolved.invalid || resolved.error) return fail(resolved.invalid || resolved.error)
|
|
546
|
+
const personal = resolved.context
|
|
547
|
+
const scope = personal ? toolkit.scopeFor(personal, { agent: context?.agent }) : toolkit.scope({ agent: context?.agent })
|
|
548
|
+
delete scope.session_id
|
|
549
|
+
const read = createProfileReads({
|
|
550
|
+
runtime: { backend: toolkit.backend },
|
|
551
|
+
personalScopes: async () => [{ context: personal, scope }],
|
|
552
|
+
available: async () => {},
|
|
553
|
+
describe: errorMessage,
|
|
554
|
+
})["profile.read"]
|
|
555
|
+
return text(profileText(await read()))
|
|
556
|
+
}
|
|
557
|
+
|
|
448
558
|
async function run(input, context) {
|
|
449
559
|
// No action lists, as the tool did before 0.9 (old calls passed none).
|
|
450
560
|
const action = String(input?.action ?? "").trim() || "list"
|
|
451
561
|
if (!SPACES_ACTIONS.includes(action)) return text(`unknown action ${oneLine(action || "(none)", 40)}: use ${SPACES_ACTIONS.join(", ")}.`)
|
|
452
562
|
try {
|
|
453
|
-
if (action === "current") return await current(context)
|
|
563
|
+
if (action === "current") return await current(input, context)
|
|
454
564
|
if (action === "list") return await list()
|
|
455
565
|
if (action === "members") return await members(input, context)
|
|
456
566
|
if (action === "access") return await access(input, context)
|
|
457
567
|
if (action === "health") return await health(input, context)
|
|
458
568
|
if (action === "sources") return await sources(input, context)
|
|
569
|
+
if (action === "profile") return await profile(context)
|
|
459
570
|
return await review(input, context)
|
|
460
571
|
} catch (error) {
|
|
461
572
|
log.warn(`spaces ${action} failed`, error)
|
|
@@ -463,5 +574,5 @@ export function createSpacesActions(toolkit) {
|
|
|
463
574
|
}
|
|
464
575
|
}
|
|
465
576
|
|
|
466
|
-
return { run, current, list, members, access, health, sources, review }
|
|
577
|
+
return { run, profile, current, list, members, access, health, sources, review }
|
|
467
578
|
}
|
package/src/tools/status.js
CHANGED
|
@@ -2,8 +2,7 @@ import { describeBootstrap, describeSpool, errorMessage, isPlainObject, oneLine
|
|
|
2
2
|
import { recordsFailure, recordsOf } from "./share.js"
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* The health report `mem_spaces` (`"action": "health"`) gives,
|
|
6
|
-
* deprecated `mem_status` alias with it: backend health, active scope,
|
|
5
|
+
* The health report `mem_spaces` (`"action": "health"`) gives: backend health, active scope,
|
|
7
6
|
* per-layer counts (L0-L3), skills, knowledge assets, queued captures, then the
|
|
8
7
|
* records service's counts for the space (`space.stats`) and the collector.
|
|
9
8
|
*
|
|
@@ -18,6 +17,8 @@ export async function renderHealth(toolkit, input, context) {
|
|
|
18
17
|
try {
|
|
19
18
|
status = await toolkit.runtime.status({
|
|
20
19
|
sessionID: input?.space ? undefined : context?.sessionID,
|
|
20
|
+
// The pause belongs to this session, whatever space the counts are for.
|
|
21
|
+
captureSession: context?.sessionID,
|
|
21
22
|
agent: context?.agent,
|
|
22
23
|
context: spaceContext,
|
|
23
24
|
})
|
|
@@ -84,11 +85,38 @@ async function recordsLines(toolkit, input, context, spaceContext) {
|
|
|
84
85
|
if (isPlainObject(stats?.events)) lines.push(`events: ${Number(stats.events.total) || 0}`)
|
|
85
86
|
if (isPlainObject(stats?.sources)) {
|
|
86
87
|
const sources = stats.sources
|
|
87
|
-
|
|
88
|
+
const requested = Number(sources.requested) || 0
|
|
89
|
+
const attention = Number(sources.attention) || 0
|
|
90
|
+
const asks = requested ? `; ${requested} bind request(s)${attention ? `, ${attention} needing attention: press Bind… again in the console` : ""}` : ""
|
|
91
|
+
lines.push(`sources: ${Number(sources.total) || 0} (${Number(sources.confirmed) || 0} confirmed, ${Number(sources.pending) || 0} pending)${asks}`)
|
|
88
92
|
}
|
|
93
|
+
lines.push(await routinesLine(records, where, stats))
|
|
89
94
|
const runs = Array.isArray(stats?.last_runs) ? stats.last_runs : []
|
|
90
95
|
for (const run of runs.slice(0, 5)) {
|
|
91
96
|
lines.push(`routine ${oneLine(run?.routine ?? "?", 20)}: ${oneLine(run?.status ?? "?", 20)} ${oneLine(run?.date ?? run?.ended ?? run?.started ?? "", 40)}${run?.error ? ` (${oneLine(run.error, 120)})` : ""}`)
|
|
92
97
|
}
|
|
93
98
|
return lines
|
|
94
99
|
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Whether the records service runs the routines, and with which model
|
|
103
|
+
* (`routines.status` `scheduled`, `model`). Off, collected events only wait
|
|
104
|
+
* out their 30 days: nothing turns them into journal pages or proposals.
|
|
105
|
+
*/
|
|
106
|
+
async function routinesLine(records, where, stats) {
|
|
107
|
+
let status
|
|
108
|
+
try {
|
|
109
|
+
status = await records.call("routines.status", {}, where)
|
|
110
|
+
} catch (error) {
|
|
111
|
+
const code = String(error?.code ?? error?.error?.code ?? "")
|
|
112
|
+
if (code === "routines_unavailable") return "routines: off (this records build has none); collected events only wait out their 30 days"
|
|
113
|
+
return `routines: unknown (${oneLine(recordsFailure(error), 160)})`
|
|
114
|
+
}
|
|
115
|
+
if (typeof status?.scheduled !== "boolean") return "routines: unknown (the records service does not say; it predates 0.9.7)"
|
|
116
|
+
const sources = Number(stats?.sources?.enabled) || 0
|
|
117
|
+
if (!status.scheduled) {
|
|
118
|
+
return `routines: off${status.model ? ` (model ${oneLine(status.model, 80)} set, but the scheduler is off)` : ""}; ${sources ? `the ${sources} collecting source(s) only buffer events for 30 days: nothing turns them into journal pages or proposals` : "collection would only buffer events for 30 days"}. The operator turns routines on.`
|
|
119
|
+
}
|
|
120
|
+
const hour = Number.isInteger(status.hour) ? `, daily after ${String(status.hour).padStart(2, "0")}:00 in the space's zone` : ""
|
|
121
|
+
return `routines: on (model ${oneLine(status.model || "unknown", 80)}${hour})`
|
|
122
|
+
}
|
package/src/tools/wiki.js
CHANGED
|
@@ -75,7 +75,6 @@ export function registerWiki(editor, toolkit) {
|
|
|
75
75
|
resolveReadSpace,
|
|
76
76
|
resolvePrivateSpace,
|
|
77
77
|
resolveWriteSpace,
|
|
78
|
-
assertTeamWrite,
|
|
79
78
|
runtime,
|
|
80
79
|
} = toolkit
|
|
81
80
|
|
|
@@ -160,21 +159,14 @@ export function registerWiki(editor, toolkit) {
|
|
|
160
159
|
/**
|
|
161
160
|
* Ask for a team write, once everything that could refuse it has passed.
|
|
162
161
|
* Every team-visible write asks, in local mode too: mem_remember and
|
|
163
|
-
*
|
|
162
|
+
* revise forget already do, and the wiki is the more durable of the three.
|
|
164
163
|
* Returns the denial's words, or "".
|
|
165
164
|
*/
|
|
166
165
|
async function approveWrite(context, visibility, spaceContext, approval) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
...(spaceContext?.space_id ? { space_id: spaceContext.space_id } : {}),
|
|
172
|
-
...(spaceContext?.space_name ? { space_name: oneLine(spaceContext.space_name, 200) } : {}),
|
|
173
|
-
})
|
|
174
|
-
return ""
|
|
175
|
-
} catch (error) {
|
|
176
|
-
return errorMessage(error)
|
|
177
|
-
}
|
|
166
|
+
// The one gate (toolkit.gateWrite): a wiki file is not a record, so a
|
|
167
|
+
// team write is asked, never held.
|
|
168
|
+
const gate = await toolkit.gateWrite(context, { action: "wiki.write", spaceContext, shared: visibility === "team", metadata: approval })
|
|
169
|
+
return gate.denied ?? ""
|
|
178
170
|
}
|
|
179
171
|
|
|
180
172
|
/**
|
package/src/util.js
CHANGED
|
@@ -78,37 +78,27 @@ export function rememberBounded(store, key, value, max = 500) {
|
|
|
78
78
|
return store
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
[
|
|
86
|
-
/\b(api[_-]?key|access[_-]?token|refresh[_-]?token|token|password|passwd|secret)\b["']?(\s*[:=]\s*)(?:"[^"\n]*"|'[^'\n]*'|[^\s,;]+)/gi,
|
|
87
|
-
"$1$2[redacted]",
|
|
88
|
-
],
|
|
89
|
-
[/\bBearer\s+[A-Za-z0-9._~+/=-]{3,}/gi, "Bearer [redacted]"],
|
|
90
|
-
[/\bsk-[A-Za-z0-9_-]{6,}/g, "[redacted-key]"],
|
|
91
|
-
[/\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{8,}\b/g, "[redacted-token]"],
|
|
92
|
-
])
|
|
93
|
-
|
|
94
|
-
/** Redact values that look like credentials. Used on every text this plugin stores or logs. */
|
|
95
|
-
export function redactSecrets(value) {
|
|
96
|
-
let text = String(value ?? "")
|
|
97
|
-
for (const [pattern, replacement] of SECRET_PATTERNS) text = text.replace(pattern, replacement)
|
|
98
|
-
return text
|
|
99
|
-
}
|
|
81
|
+
// The redaction rules live in storable.js, with the decision of what may be
|
|
82
|
+
// stored (owner decision D3); the logger, ask excerpts and the redaction
|
|
83
|
+
// vectors use them from here.
|
|
84
|
+
export { redactLocalPaths, redactSecrets } from "./storable.js"
|
|
100
85
|
|
|
101
86
|
/**
|
|
102
87
|
* One line describing the capture spool: pending depth, quarantined entries,
|
|
103
88
|
* claims in flight, and the age of the oldest pending entry.
|
|
104
89
|
*
|
|
105
|
-
* @param {{ pending?: number, quarantined?: number, inflight?: number, oldest?: string | null } | number} stats
|
|
90
|
+
* @param {{ pending?: number, quarantined?: number, inflight?: number, oldest?: string | null, reasons?: { reason: string, expires: string }[], quarantineDays?: number } | number} stats
|
|
106
91
|
*/
|
|
107
92
|
export function describeSpool(stats) {
|
|
108
93
|
if (typeof stats === "number") return `${stats} pending`
|
|
109
94
|
const parts = [`${stats?.pending ?? 0} pending`, `${stats?.quarantined ?? 0} quarantined`]
|
|
110
95
|
if (stats?.inflight) parts.push(`${stats.inflight} in flight`)
|
|
111
96
|
if (stats?.pending && stats?.oldest) parts.push(`oldest ${stats.oldest}`)
|
|
97
|
+
// A quarantined capture was refused or gave up; say why, and when it goes.
|
|
98
|
+
const newest = Array.isArray(stats?.reasons) ? stats.reasons[0] : null
|
|
99
|
+
if (stats?.quarantined && newest) {
|
|
100
|
+
parts.push(`latest quarantine: ${newest.reason} (deleted ${String(newest.expires).slice(0, 10)}; quarantined captures are kept ${stats.quarantineDays ?? 14} days)`)
|
|
101
|
+
}
|
|
112
102
|
return parts.join(", ")
|
|
113
103
|
}
|
|
114
104
|
|