@joe-broadhead/opencode-mem 0.9.14 → 0.11.2

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.
Files changed (139) hide show
  1. package/CHANGELOG.md +1805 -0
  2. package/README.md +14 -7
  3. package/SECURITY.md +3 -1
  4. package/docs/agent-instructions.md +1 -1
  5. package/docs/agent-protocol.md +7 -7
  6. package/docs/permissions.md +16 -0
  7. package/package.json +7688 -339
  8. package/shared/page-paths.js +44 -0
  9. package/skills/mem.pages/SKILL.md +3 -1
  10. package/skills/mem.usage/SKILL.md +15 -15
  11. package/src/action-grant-keys.js +159 -0
  12. package/src/action-grant.js +439 -0
  13. package/src/actions.js +1690 -0
  14. package/src/backend/records.js +25 -2
  15. package/src/backend/v3.js +76 -22
  16. package/src/call-shapes.js +17 -16
  17. package/src/collector-lock.js +533 -62
  18. package/src/collector-service.js +153 -26
  19. package/src/collector.js +623 -30
  20. package/src/config.js +79 -6
  21. package/src/connect.js +106 -8
  22. package/src/credentials.js +123 -25
  23. package/src/events.js +174 -3
  24. package/src/forget-turn.js +141 -0
  25. package/src/host-refusal.js +78 -0
  26. package/src/index.js +209 -22
  27. package/src/jobs/host.js +557 -0
  28. package/src/ops/codes.js +110 -0
  29. package/src/ops/copy-lineage.js +42 -0
  30. package/src/ops/index.js +28 -0
  31. package/src/ops/space-ops.js +1936 -0
  32. package/src/outlinks.js +103 -0
  33. package/src/p2-reservation.js +151 -0
  34. package/src/permissions.js +30 -0
  35. package/src/plugins/descriptor.js +62 -0
  36. package/src/plugins/discovery.js +318 -0
  37. package/src/plugins/grants.js +178 -0
  38. package/src/plugins/locate.js +319 -0
  39. package/src/plugins/manifest.js +1162 -0
  40. package/src/plugins/source-contract/index.js +34 -0
  41. package/src/plugins/source-contract/policy.js +102 -0
  42. package/src/plugins/source-contract/vendor/PROVENANCE.json +17 -0
  43. package/src/plugins/source-contract/vendor/RELEASE-SHA256SUMS +3 -0
  44. package/src/plugins/source-contract/vendor/RELEASE.json +17 -0
  45. package/src/plugins/source-contract/vendor/SHA256SUMS +41 -0
  46. package/src/plugins/source-contract/vendor/THIRD-PARTY-NOTICES.md +35 -0
  47. package/src/plugins/source-contract/vendor/broker-client.js +93 -0
  48. package/src/plugins/source-contract/vendor/chunks/chunk-3M5PAKSS.js +84 -0
  49. package/src/plugins/source-contract/vendor/chunks/chunk-4ERCA4TK.js +185 -0
  50. package/src/plugins/source-contract/vendor/chunks/chunk-6HDIB2SA.js +183 -0
  51. package/src/plugins/source-contract/vendor/chunks/chunk-ELPJD3ND.js +10 -0
  52. package/src/plugins/source-contract/vendor/chunks/chunk-HLVAKAYG.js +10 -0
  53. package/src/plugins/source-contract/vendor/chunks/chunk-I2RXJJJM.js +81 -0
  54. package/src/plugins/source-contract/vendor/chunks/chunk-KWEWSZ4V.js +505 -0
  55. package/src/plugins/source-contract/vendor/chunks/chunk-KYWBTAF4.js +182 -0
  56. package/src/plugins/source-contract/vendor/chunks/chunk-Q3RQA5J2.js +249 -0
  57. package/src/plugins/source-contract/vendor/chunks/chunk-RRUAIQ4I.js +156 -0
  58. package/src/plugins/source-contract/vendor/chunks/chunk-RYNKNCPP.js +165 -0
  59. package/src/plugins/source-contract/vendor/chunks/chunk-W5SHR7DQ.js +9477 -0
  60. package/src/plugins/source-contract/vendor/chunks/chunk-ZUKF7P6U.js +653 -0
  61. package/src/plugins/source-contract/vendor/cursor.js +26 -0
  62. package/src/plugins/source-contract/vendor/descriptor.js +34 -0
  63. package/src/plugins/source-contract/vendor/events.js +257 -0
  64. package/src/plugins/source-contract/vendor/index.js +10 -0
  65. package/src/plugins/source-contract/vendor/params.js +22 -0
  66. package/src/plugins/source-contract/vendor/permission.js +28 -0
  67. package/src/plugins/source-contract/vendor/pick.js +22 -0
  68. package/src/plugins/source-contract/vendor/read.js +29 -0
  69. package/src/plugins/source-contract/vendor/registry.js +23 -0
  70. package/src/plugins/source-contract/vendor/schema.js +17 -0
  71. package/src/plugins/source-contract/vendor/schemas/output.agenda.json +96 -0
  72. package/src/plugins/source-contract/vendor/schemas/output.countdown.json +28 -0
  73. package/src/plugins/source-contract/vendor/schemas/output.image.json +28 -0
  74. package/src/plugins/source-contract/vendor/schemas/output.inbox.json +84 -0
  75. package/src/plugins/source-contract/vendor/schemas/output.links.json +46 -0
  76. package/src/plugins/source-contract/vendor/schemas/output.list.json +57 -0
  77. package/src/plugins/source-contract/vendor/schemas/output.metric.json +61 -0
  78. package/src/plugins/source-contract/vendor/schemas/output.note.json +14 -0
  79. package/src/plugins/source-contract/vendor/schemas/output.progress.json +50 -0
  80. package/src/plugins/source-contract/vendor/schemas/output.record.json +38 -0
  81. package/src/plugins/source-contract/vendor/schemas/output.table.json +61 -0
  82. package/src/plugins/source-contract/vendor/schemas/output.thread.json +62 -0
  83. package/src/plugins/source-contract/vendor/schemas/sources.json +819 -0
  84. package/src/plugins/source-contract/vendor/schemas.js +19 -0
  85. package/src/plugins/source-contract/vendor/tools-rpc.js +42 -0
  86. package/src/plugins/verify.js +56 -0
  87. package/src/protocol.js +11 -6
  88. package/src/recall.js +92 -5
  89. package/src/records.js +61 -2
  90. package/src/requests.js +216 -0
  91. package/src/runtime.js +89 -7
  92. package/src/setup-time.js +68 -0
  93. package/src/skills.js +230 -8
  94. package/src/source-ids.js +25 -0
  95. package/src/sources/declaration.js +365 -0
  96. package/src/sources/reads.js +772 -0
  97. package/src/sources/rpc.js +151 -0
  98. package/src/sources.js +389 -92
  99. package/src/spaces.js +283 -11
  100. package/src/spool.js +61 -4
  101. package/src/stated.js +10 -5
  102. package/src/storable.js +139 -0
  103. package/src/tools/commitments.js +37 -4
  104. package/src/tools/held-write.js +31 -12
  105. package/src/tools/index.js +27 -0
  106. package/src/tools/ingest.js +27 -2
  107. package/src/tools/memory.js +30 -7
  108. package/src/tools/page.js +95 -9
  109. package/src/tools/recall.js +208 -4
  110. package/src/tools/share.js +42 -3
  111. package/src/tools/shared.js +44 -4
  112. package/src/tools/space-lifecycle.js +278 -0
  113. package/src/tools/spaces.js +373 -29
  114. package/src/tools/status.js +9 -2
  115. package/src/tools/wiki.js +34 -4
  116. package/src/util.js +110 -6
  117. package/src/vendor/source-contract/README.md +24 -0
  118. package/src/vendor/source-contract/THIRD-PARTY-NOTICES.md +35 -0
  119. package/src/vendor/source-contract/VENDOR.json +81 -0
  120. package/src/vendor/source-contract/chunks/chunk-3M5PAKSS.js +84 -0
  121. package/src/vendor/source-contract/chunks/chunk-6HDIB2SA.js +183 -0
  122. package/src/vendor/source-contract/chunks/chunk-HLVAKAYG.js +10 -0
  123. package/src/vendor/source-contract/chunks/chunk-I2RXJJJM.js +81 -0
  124. package/src/vendor/source-contract/chunks/chunk-KWEWSZ4V.js +505 -0
  125. package/src/vendor/source-contract/chunks/chunk-W5SHR7DQ.js +9477 -0
  126. package/src/vendor/source-contract/chunks/chunk-ZUKF7P6U.js +653 -0
  127. package/src/vendor/source-contract/cursor.js +26 -0
  128. package/src/vendor/source-contract/events.js +257 -0
  129. package/src/vendor/source-contract/registry.js +23 -0
  130. package/src/vendor/source-contract/tools-rpc.js +42 -0
  131. package/src/views/access-reads.js +256 -0
  132. package/src/views/communities.js +77 -0
  133. package/src/views/contracts.js +930 -21
  134. package/src/views/inbox-bytes.js +103 -0
  135. package/src/views/profile.js +31 -0
  136. package/src/views/records-reads.js +355 -38
  137. package/src/views/rpc.js +118 -8
  138. package/src/views/shape.js +8 -5
  139. package/views/pages.html +2 -2
@@ -0,0 +1,278 @@
1
+ import { errorMessage, oneLine } from "../util.js"
2
+
3
+ /**
4
+ * `mem_spaces` create, rename, archive, leave and handover (DESIGN-CRUD slice
5
+ * S1): a person's own space lifecycle, only when the person asks.
6
+ *
7
+ * - create, rename and unarchive (`archive` with `restore: true`) are G1: one
8
+ * ask (`space.create`, `space.rename`, `space.unarchive`) naming what
9
+ * changes; the person's skipAsks may lift it.
10
+ * - archive, leave and handover are G2. Without `confirm: true` they ask the
11
+ * server for its preview and show it, and change nothing. With it they
12
+ * take a fresh server preview, ask (`space.archive`, `space.leave`,
13
+ * `space.handover`) with the server's own words, and commit with the
14
+ * server's `expected_digest`: the digest of the state the prompt showed, so
15
+ * a state that moved is refused (`409 state_changed`). skipAsks never lifts
16
+ * these asks (src/permissions.js NEVER_SKIPPED). The digest proves the
17
+ * change is the one previewed; it cannot prove a person saw the ask: that
18
+ * is this code's rule.
19
+ * - Handover changes ownership and cannot be taken back, so a tool call (a
20
+ * bearer) only files the request; the space's owner performs it in Memory's
21
+ * console, at a browser (P1, DESIGN-CRUD §3.3). The answer says so.
22
+ * - Deleting a shared space is a console request; no tool deletes one.
23
+ *
24
+ * Every answer names the next call. Membership and roles stay control's.
25
+ *
26
+ * @param {ReturnType<import("./shared.js").createToolkit>} toolkit
27
+ */
28
+ export function createSpaceLifecycle(toolkit) {
29
+ const { config, control, text, fail } = toolkit
30
+
31
+ const consoleLink = () => {
32
+ const base = String(control?.controlURL ?? "").replace(/\/+$/, "")
33
+ return base ? `${base}/console/spaces` : "Memory's console › Spaces"
34
+ }
35
+
36
+ /** The host's ask. Each call names its action as a literal (scripts/approval-actions.mjs reads them). */
37
+ async function ask(context, { action, space, metadata }) {
38
+ try {
39
+ await toolkit.assert({
40
+ sessionID: context?.sessionID,
41
+ agent: context?.agent,
42
+ source: { type: "tool", messageID: context?.messageID, id: context?.id },
43
+ action,
44
+ resources: [space],
45
+ save: [],
46
+ metadata,
47
+ })
48
+ return null
49
+ } catch (error) {
50
+ return errorMessage(error)
51
+ }
52
+ }
53
+
54
+ /** A space the person names: an id, or the exact name of one they can list. */
55
+ async function spaceNamed(value) {
56
+ const given = String(value ?? "").trim()
57
+ if (!given) return { error: 'name the space: "space": "<id>" from {"action": "list"}.' }
58
+ const spaces = await control.listSpaces({ userId: config.user })
59
+ const found = spaces.find((space) => space.space_id === given) ?? spaces.filter((space) => String(space.name ?? "").toLowerCase() === given.toLowerCase()).at(0)
60
+ // An archived space is not listed: its id still works for unarchive.
61
+ return found ? { id: found.space_id, name: found.name || found.space_id, kind: found.kind, role: found.role } : { id: given, name: given, kind: "", role: "" }
62
+ }
63
+
64
+ /**
65
+ * The handover target: a member of the space by user id, email or exact
66
+ * name, else a colleague by exact email. Membership is checked again by the
67
+ * server, which refuses anyone who is not already a member.
68
+ */
69
+ async function personNamed(value, spaceId) {
70
+ const given = String(value ?? "").trim()
71
+ if (!given) return { error: 'name the new owner: "user": an email or a user id from {"action": "members"}.' }
72
+ const wanted = given.toLowerCase()
73
+ let roster = []
74
+ try {
75
+ roster = spaceId ? await control.listMembers({ userId: config.user, spaceId }) : []
76
+ } catch {
77
+ roster = []
78
+ }
79
+ const member = roster.find((entry) => [entry.user_id, entry.email, entry.name].some((field) => String(field ?? "").toLowerCase() === wanted))
80
+ if (member) return { id: member.user_id, name: member.name || member.email || member.user_id }
81
+ if (!given.includes("@")) return { id: given }
82
+ const found = await control.lookupUser({ userId: config.user, email: given })
83
+ return found ? { id: found.user_id, name: found.name } : { error: `no one in your organisation has the email ${oneLine(given, 120)}.` }
84
+ }
85
+
86
+ /**
87
+ * A refusal in the person's terms. The server's codes are `state_changed`
88
+ * (the space moved after the preview) and `digest_required` (a bearer's G2
89
+ * call without the preview's digest); `ControlError` keeps a code in
90
+ * `errorCode` when it has one and in the message text otherwise, so both
91
+ * are read.
92
+ */
93
+ const refused = (verb, error) => {
94
+ const message = errorMessage(error)
95
+ const code = String(error?.errorCode ?? "")
96
+ if (code === "state_changed" || /\bstate_changed\b|changed since the preview/i.test(message)) {
97
+ return text(`not ${verb}: the space changed since the preview, so nothing changed. Preview it again and show the person the new preview.`)
98
+ }
99
+ if (code === "digest_required" || /\bdigest_required\b|expected_digest is required/i.test(message)) {
100
+ return text(`not ${verb}: expected_digest is required of a bearer, and this call had none; nothing changed. Preview the change again and send its digest.`)
101
+ }
102
+ // A refusal control decided (403: a role or membership it does not grant)
103
+ // is an answer, not an outage: its own words, never "memory unavailable".
104
+ if (Number(error?.status) === 403) {
105
+ const what = String(error?.what ?? "").replace(/^space (?:preview|update|handover|leave|create): /, "")
106
+ const next = String(error?.next ?? "")
107
+ return text(`not ${verb}: ${what || message}${next ? ` Next step: ${next}` : ""}`)
108
+ }
109
+ return fail(`not ${verb}: ${message}`)
110
+ }
111
+
112
+ async function create(input, context) {
113
+ const name = oneLine(input?.name ?? "", 200).trim()
114
+ if (!name) return text('not created: give the new space a name: {"action": "create", "name": "Harbour team"}.')
115
+ const denied = await ask(context, {
116
+ action: "space.create",
117
+ space: name,
118
+ metadata: { name, owner: "you", changes: `Creates the shared space "${name}". You own it. Nothing is shared and no one else is in it until someone is added in Memory's console or memories are copied in.` },
119
+ })
120
+ if (denied) return text(`not created (approval): ${denied}`)
121
+ try {
122
+ const made = await control.createSpace({ userId: config.user, name })
123
+ const id = made.space?.space_id ?? "?"
124
+ return text(
125
+ `created shared space ${oneLine(name, 120)} (${id}); you own it${made.owned && made.cap ? ` (${made.owned} of ${made.cap} you may own)` : ""}. No one else is in it yet. Next, only when the person asks: {"action": "current", "bind": {"space": "${id}"}} binds this session to it; mem_share copy puts memories in; an admin adds members in ${consoleLink()}.`,
126
+ )
127
+ } catch (error) {
128
+ return refused("created", error)
129
+ }
130
+ }
131
+
132
+ async function rename(input, context) {
133
+ const name = oneLine(input?.name ?? "", 200).trim()
134
+ if (!name) return text('not renamed: give the new name: {"action": "rename", "space": "<id>", "name": "<new name>"}.')
135
+ const space = await spaceNamed(input?.space)
136
+ if (space.error) return text(`not renamed: ${space.error}`)
137
+ if (space.kind === "personal") return text("not renamed: a personal space keeps its name.")
138
+ const denied = await ask(context, { action: "space.rename", space: space.id, metadata: { space: space.id, from: oneLine(space.name, 200), to: name, changes: `Every member sees "${oneLine(space.name, 120)}" renamed "${name}".` } })
139
+ if (denied) return text(`not renamed (approval): ${denied}`)
140
+ try {
141
+ const done = await control.updateSpace({ userId: config.user, spaceId: space.id, name })
142
+ return text(done.unchanged ? `unchanged: the space is already called ${oneLine(name, 120)}.` : `renamed ${oneLine(done.from || space.name, 120)} to ${oneLine(name, 120)} (${space.id}).`)
143
+ } catch (error) {
144
+ return refused("renamed", error)
145
+ }
146
+ }
147
+
148
+ async function unarchive(space, context) {
149
+ const denied = await ask(context, { action: "space.unarchive", space: space.id, metadata: { space: space.id, changes: `Brings ${oneLine(space.name, 120)} back online for every member.` } })
150
+ if (denied) return text(`not unarchived (approval): ${denied}`)
151
+ try {
152
+ const done = await control.updateSpace({ userId: config.user, spaceId: space.id, status: "active" })
153
+ return text(done.unchanged ? "unchanged: the space is not archived." : `unarchived ${oneLine(done.space?.name || space.name, 120)} (${space.id}): it is back online for its members.`)
154
+ } catch (error) {
155
+ return refused("unarchived", error)
156
+ }
157
+ }
158
+
159
+ /**
160
+ * A G2 change: the server's preview first; with confirm, a fresh preview,
161
+ * the ask with the server's own words, and the commit with its token.
162
+ * `words` lets one action say the change in the caller's own terms, because
163
+ * the server's preview speaks as the person who may perform it: handover
164
+ * uses it for anyone who is not the space's owner.
165
+ */
166
+ async function g2(action, input, context, { asks, verb, commit, user, words }) {
167
+ const space = await spaceNamed(input?.space)
168
+ if (space.error) return text(`not ${verb}: ${space.error}`)
169
+ let preview
170
+ try {
171
+ preview = await control.previewSpace({ userId: config.user, spaceId: space.id, action, ...(user ? { user: user.id } : {}) })
172
+ } catch (error) {
173
+ return refused(verb, error)
174
+ }
175
+ const name = oneLine(preview.space?.name || space.name, 120)
176
+ const changes = words?.changes ? words.changes(preview, name) : oneLine(preview.summary, 600)
177
+ if (input?.confirm !== true) {
178
+ const shown = words?.preview ? words.preview(preview, name) : `${oneLine(preview.summary, 600)} ${preview.reversible ? "It can be undone." : "It cannot be undone by you."}`
179
+ return text(`preview, nothing changed: ${shown} Show the person this; only when they agree, call again with "confirm": true (they are asked once more).`)
180
+ }
181
+ const denied = await ask(context, {
182
+ ...asks,
183
+ space: space.id,
184
+ metadata: {
185
+ space: space.id,
186
+ space_name: name,
187
+ changes,
188
+ reversible: preview.reversible,
189
+ ...(preview.newOwner ? { new_owner: preview.newOwner.name || preview.newOwner.user_id } : {}),
190
+ expected_digest: preview.digest,
191
+ },
192
+ })
193
+ if (denied) return text(`not ${verb} (approval): ${denied}`)
194
+ try {
195
+ return text(await commit(space, preview.digest, name))
196
+ } catch (error) {
197
+ return refused(verb, error)
198
+ }
199
+ }
200
+
201
+ async function archive(input, context) {
202
+ if (input?.restore === true) {
203
+ const space = await spaceNamed(input?.space)
204
+ if (space.error) return text(`not unarchived: ${space.error}`)
205
+ return unarchive(space, context)
206
+ }
207
+ return g2("archive", input, context, {
208
+ asks: { action: "space.archive" },
209
+ verb: "archived",
210
+ commit: async (space, digest, name) => {
211
+ const done = await control.updateSpace({ userId: config.user, spaceId: space.id, status: "archived", expectedDigest: digest })
212
+ return done.unchanged
213
+ ? `unchanged: ${name} is already archived.`
214
+ : `archived ${name} (${space.id}): it is offline for every member, and nothing in it was deleted. {"action": "archive", "space": "${space.id}", "restore": true} brings it back; deletion is requested in ${consoleLink()}.`
215
+ },
216
+ })
217
+ }
218
+
219
+ async function leave(input, context) {
220
+ return g2("leave", input, context, {
221
+ asks: { action: "space.leave" },
222
+ verb: "left",
223
+ commit: async (space, digest, name) => {
224
+ await control.leaveSpace({ userId: config.user, spaceId: space.id, expectedDigest: digest })
225
+ return `left ${name} (${space.id}): you no longer have access; only an admin of it can add you back.`
226
+ },
227
+ })
228
+ }
229
+
230
+ /**
231
+ * The space's owner, as this caller can see them: the roster row whose role
232
+ * is owner, else none. Control's spaces list names only the caller's own
233
+ * role, so the members read is the one place an owner's name comes from. A
234
+ * caller who cannot see one is told that, and never given a name to guess.
235
+ */
236
+ async function ownerSeen(spaceId) {
237
+ const roster = await control.listMembers({ userId: config.user, spaceId }).catch(() => [])
238
+ return (Array.isArray(roster) ? roster : []).find((member) => String(member?.role ?? "").toLowerCase() === "owner") ?? null
239
+ }
240
+
241
+ async function handover(input, context) {
242
+ const target = await spaceNamed(input?.space)
243
+ if (target.error) return text(`not handed over: ${target.error}`)
244
+ const user = await personNamed(input?.user, target.id)
245
+ if (user.error) return text(`not handed over: ${user.error}`)
246
+ // 0.10.5 gate B10: the preview and the result follow the caller's ROLE. A
247
+ // tool's handover only ever files a request (above), and the server's
248
+ // preview is written as the person who may perform it, so for anyone who
249
+ // is not the owner the answer says what this call really does: sends a
250
+ // request the owner confirms, naming that owner when the caller can see
251
+ // them, and never claiming the caller becomes an admin.
252
+ const mine = String(target.role ?? "").toLowerCase() === "owner"
253
+ const owner = await ownerSeen(target.id)
254
+ const confirmer = owner
255
+ ? `${oneLine(owner.name || owner.email || owner.user_id, 120)} (${mine ? "you, the space's owner" : "the space's owner"})`
256
+ : mine
257
+ ? "you (the space's owner)"
258
+ : "the space's owner (not visible to you)"
259
+ const would = oneLine(user.name || user.id, 120)
260
+ const request = (name, id) =>
261
+ `this sends a handover request: ${would} would own ${name} (${id}). Ownership cannot be taken back, so ${confirmer} confirms the request in Memory's console › Spaces › Manage this space, in a browser (${consoleLink()}). Nothing changes hands until they do.`
262
+ return g2("handover", input, context, {
263
+ asks: { action: "space.handover" },
264
+ verb: "handed over",
265
+ user,
266
+ ...(mine ? {} : { words: { preview: (_preview, name) => request(name, target.id), changes: (_preview, name) => request(name, target.id) } }),
267
+ commit: async (space, digest, name) => {
268
+ const done = await control.handoverSpace({ userId: config.user, spaceId: space.id, user: user.id, expectedDigest: digest })
269
+ if (done.requested) {
270
+ return `requested: ${would} would own ${name} (${space.id}). Ownership cannot be taken back, so ${confirmer} confirms it in Memory's console › Spaces › Manage this space, in a browser (${consoleLink()}). Until then nothing has changed hands.`
271
+ }
272
+ return `handed ${name} (${space.id}) over to ${oneLine(user.name || done.owner || user.id, 120)}: they own it now${mine ? ", and you are an admin" : ""}.`
273
+ },
274
+ })
275
+ }
276
+
277
+ return { create, rename, archive, leave, handover }
278
+ }