@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
package/src/storable.js CHANGED
@@ -124,6 +124,145 @@ export function redactLocalPaths(value) {
124
124
  return text
125
125
  }
126
126
 
127
+ // ---------------------------------------------------------------------------
128
+ // Error text: what a failure may say. The plugin (src/util.js) and the records
129
+ // service (records/src/collector text, jobs.checkpoint) both redact a source's
130
+ // own words with this, so a secret never reaches an answer, a log or a store.
131
+ // It lives here, beside `redactSecrets`, because both sides carry this file.
132
+
133
+ /**
134
+ * `scheme://userinfo@host`: a URL's userinfo is a credential, and Memory's own
135
+ * patterns (storable.js) do not cover it.
136
+ */
137
+ const URL_USERINFO = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@]*@/gi
138
+ /** A whole URL: scheme, authority and path kept; the query string and fragment dropped. */
139
+ const ERROR_URL = /\b([a-z][a-z0-9+.-]*:\/\/)([^\s/?#]*)([^\s?#]*)(?:[?#][^\s]*)?/gi
140
+ /** A `Cookie:` or `Set-Cookie:` header: header name kept, value dropped. */
141
+ const ERROR_COOKIE = /\b((?:set-)?cookie\s*:\s*)[^\r\n]+/gi
142
+ /** A JWT: the shape Memory's own detector knows, masked whole rather than by part. */
143
+ const ERROR_JWT = /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g
144
+ /** A PEM private key block, whole. */
145
+ const ERROR_PEM = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----|$)/g
146
+ /**
147
+ * The backstop's candidate: one run of 24 or more word characters (letters,
148
+ * digits, `_`, `-`, `+`, `/`, `=`) — the alphabet base64 secrets use, so an
149
+ * AWS secret key with `/` and `+` stays one run. Paths and URLs are reduced
150
+ * before this runs, so the `/` here is a secret's, not a directory's.
151
+ */
152
+ const ERROR_LONG_TOKEN = /(?<![A-Za-z0-9_+/=-])[A-Za-z0-9_+/=-]{24,}(?![A-Za-z0-9_+/=-])/g
153
+ /** Characters of error text shown at most: enough for a cause, never a dump. */
154
+ export const ERROR_TEXT_MAX = 400
155
+
156
+ /** What an error says when showing its own text would risk a secret. */
157
+ export const ERROR_WITHHELD = "an error (details withheld: they may contain a secret)"
158
+
159
+ /**
160
+ * A hex run of 32 or more characters: an SHA-256 digest or a hex-encoded key,
161
+ * never something an error needs in full (owner decision, review F2 fourth
162
+ * pass). Shorter hex — a 7 to 12 character commit hash or an id — stays
163
+ * readable.
164
+ */
165
+ const ERROR_HEX = /(?<![0-9A-Za-z])[0-9a-fA-F]{32,}(?![0-9A-Za-z])/g
166
+
167
+ /**
168
+ * When a run of characters is a secret rather than a name (review F2, third
169
+ * pass). Length alone cut hostnames and paths, so the rule is randomness:
170
+ *
171
+ * - **at least 24 characters** — a secret is long, and below that the entropy
172
+ * of a short random string is too close to prose's to judge;
173
+ * - **mixed classes**: at least one lowercase, one uppercase and one digit, and
174
+ * **at least 20% lowercase**. Base64 secrets are lowercase-dominant or
175
+ * balanced; a record id (`rec_` + a ULID of uppercase and digits) is 13%
176
+ * lowercase, a hex hash and a UUID carry no uppercase at all, so each of
177
+ * those stays readable;
178
+ * - **Shannon entropy at a length-aware cutoff: `min(4.5, log2(len) - 0.7)`
179
+ * bits per character** (the reviewer's rule, fourth pass). A string's
180
+ * entropy is capped at `log2(len)`, so a flat 4.5 missed 4% of 24-character
181
+ * tokens, 30% at 28 and 70% at 32; the length-aware line misses at most 5%
182
+ * and flags no new benign text. Measured: English words, file paths and
183
+ * sentences sit at 3.3–4.0 (a path with a digit, 3.9), a ULID-based record
184
+ * id at 4.3; random base62/base64 tokens sit at 4.2 (24 characters) to 5.0
185
+ * (32) and AWS secret keys at 4.7.
186
+ *
187
+ * Names can never cover every credential format; this rule covers the shapes
188
+ * no name knows. Memory's own detector and patterns still run first, so a
189
+ * named secret is caught even if this misses it.
190
+ */
191
+ const MIXED_MIN_ENTROPY = 4.5
192
+ function randomToken(run) {
193
+ const value = String(run ?? "")
194
+ if (value.length < 24) return false
195
+ // A short string cannot reach 4.5 bits per character: the ceiling is log2(len).
196
+ const cutoff = Math.min(MIXED_MIN_ENTROPY, Math.log2(value.length) - 0.7)
197
+ const lower = (value.match(/[a-z]/g) ?? []).length
198
+ const upper = (value.match(/[A-Z]/g) ?? []).length
199
+ if (lower === 0 || upper === 0 || !/\d/.test(value)) return false
200
+ if (lower / value.length < 0.2) return false
201
+ return entropyOf(value) >= cutoff
202
+ }
203
+
204
+ /** A URL's path, kept segment by segment: a part masked only when it alone is a secret by `randomToken`. */
205
+ function maskPath(path) {
206
+ if (!path) return path
207
+ const segments = path.split("/")
208
+ // A secret may hold `/` itself (an AWS secret key), so the path's characters
209
+ // are judged together first; only then is each segment judged alone.
210
+ const joined = segments.join("")
211
+ if (randomToken(joined)) return "/[redacted]"
212
+ return segments.map((segment) => (randomToken(segment) ? "[redacted]" : segment)).join("/")
213
+ }
214
+
215
+ /** Shannon entropy, in bits per character. */
216
+ function entropyOf(text) {
217
+ const counts = new Map()
218
+ for (const char of text) counts.set(char, (counts.get(char) ?? 0) + 1)
219
+ let bits = 0
220
+ for (const count of counts.values()) {
221
+ const p = count / text.length
222
+ bits -= p * Math.log2(p)
223
+ }
224
+ return bits
225
+ }
226
+
227
+ /** A URL's authority with any userinfo masked, so `user:pass@host` keeps only the host. */
228
+ function maskAuthority(authority) {
229
+ const at = authority.lastIndexOf("@")
230
+ return at < 0 ? authority : `[redacted]@${authority.slice(at + 1)}`
231
+ }
232
+
233
+ /**
234
+ * Text from a failure, safe to show in a tool answer or a console report
235
+ * (0.10.3 gate A15; review F2). A plugin source's thrown value is third-party
236
+ * data, so this is a sanitiser, not a formatter:
237
+ *
238
+ * 1. a PEM private key block becomes `[redacted-private-key]`, and a hex run
239
+ * of 32+ characters becomes `[redacted]` (a digest is no help in an error);
240
+ * 2. every URL keeps its scheme, host and path — userinfo is masked and the
241
+ * query string and fragment are dropped, which covers a signed URL's
242
+ * signature and any `..._token=`, `key=` or `code=` parameter;
243
+ * 3. a `Cookie:` or `Set-Cookie:` header's value becomes `[redacted]`;
244
+ * 4. Memory's own secret patterns run (`redactSecrets`, src/storable.js);
245
+ * 5. the backstop: a run of 24+ word characters that mixes letters and digits
246
+ * at random-token entropy becomes `[redacted]` — names cover only the
247
+ * formats someone thought of, and this is the rule for the rest.
248
+ *
249
+ * The result is capped at `ERROR_TEXT_MAX`. `errorMessage` then asks Memory's
250
+ * own store detector (`secretKinds`) whether anything is still secret, and
251
+ * withholds the whole message when it is.
252
+ */
253
+ export function redactErrorText(value) {
254
+ let text = String(value ?? "")
255
+ .replace(ERROR_PEM, "[redacted-private-key]")
256
+ .replace(ERROR_HEX, "[redacted]")
257
+ .replace(ERROR_URL, (_match, scheme, authority, path) => `${scheme}${maskAuthority(authority)}${maskPath(path)}`)
258
+ text = text.replace(ERROR_COOKIE, "$1[redacted]").replace(ERROR_JWT, "[redacted-jwt]")
259
+ // Memory's patterns before the backstop: a JWT's three dot-separated parts
260
+ // must match whole, which the backstop's runs would break apart.
261
+ text = redactSecrets(text).replace(URL_USERINFO, "$1[redacted]@")
262
+ text = text.replace(ERROR_LONG_TOKEN, (run) => (randomToken(run) ? "[redacted]" : run))
263
+ return text.length > ERROR_TEXT_MAX ? `${text.slice(0, ERROR_TEXT_MAX - 1)}…` : text
264
+ }
265
+
127
266
  // ---------------------------------------------------------------------------
128
267
  // Detection for storage.
129
268
 
@@ -8,11 +8,13 @@ import {
8
8
  formatDue,
9
9
  itemLine,
10
10
  newCommitmentId,
11
+ openNowSummary,
11
12
  similarOpen,
12
13
  validateDue,
13
14
  validateFields,
14
15
  } from "../records.js"
15
16
  import { dueStated, holdsWord, tiedWords, triggerStated } from "../stated.js"
17
+ import { latestWord } from "../requests.js"
16
18
  import { errorMessage, oneLine } from "../util.js"
17
19
  import { withReceipt, writeReceipt } from "../views/receipt.js"
18
20
  import { spaceOf } from "../views/shape.js"
@@ -217,8 +219,16 @@ export function registerCommitments(editor, toolkit) {
217
219
  if (personal.outcome === "refuse") return text(personal.message.replace(/^not stored/, "not saved"))
218
220
  if (personal.outcome === "warn") warning = personal.message
219
221
  // A due date or trigger is kept only when the person's messages name it
220
- // (0.9.9, WRITES-099): one the agent worked out is dropped, and said.
221
- const unstated = keepStated(fields, runtime.personSaid?.(context?.sessionID) ?? [], zone, runtime.personDrops?.(context?.sessionID))
222
+ // (0.9.9, WRITES-099; 0.11 gate H2). A call with no call id — a host's
223
+ // press in a fresh session — is checked exactly as 0.11.0 checked it:
224
+ // with a person message noted the field must be theirs; with none noted
225
+ // it is kept. A model's call with no noted prompt fails closed, and so
226
+ // does a session the host cannot place (an agent's brief). The words are
227
+ // the request guards' source (src/requests.js).
228
+ await toolkit.ensureSession?.(context?.sessionID)
229
+ const said = latestWord(runtime, context?.sessionID, context)
230
+ const personMessages = said.brief || (said.model && !said.seen) ? [{ text: "", at: 0 }] : said.seen ? (runtime.personSaid?.(context?.sessionID) ?? []) : []
231
+ const unstated = keepStated(fields, personMessages, zone, runtime.personDrops?.(context?.sessionID))
222
232
  if (action === "update" && Object.keys(fields).length === 0) {
223
233
  if (unstated.length > 0) return text(`not updated: ${unstated.join(" ")}`)
224
234
  return text("nothing to update: pass state, what, owner, due, trigger, project, or link")
@@ -244,12 +254,35 @@ export function registerCommitments(editor, toolkit) {
244
254
  log.info("commitments", { action, rounds: result.rounds, unchanged: Boolean(result.unchanged) })
245
255
 
246
256
  if (action === "add" && result.unchanged) {
247
- return text([`already open, not added again: ${describe(result.item, zone)}`, dropped].filter(Boolean).join("\n"))
257
+ // A duplicate changed nothing (0.10.3 gate, B13): say so plainly, and
258
+ // tag the record in the list as the one that was already open.
259
+ return text(
260
+ [
261
+ `already open, so nothing was added: ${describe(result.item, zone)}`,
262
+ dropped,
263
+ openNowSummary(result.records?.items, today, zone, { mark: { id: result.item.id, label: "already open, nothing added" } }),
264
+ ]
265
+ .filter(Boolean)
266
+ .join("\n"),
267
+ )
268
+ }
269
+ if (action === "delete") {
270
+ return text(
271
+ [
272
+ `deleted ${oneLine(result.item.id || id, 40)}: ${oneLine(result.item.what, 300)}. It is gone from this space's records.`,
273
+ openNowSummary(result.records?.items, today, zone),
274
+ ].join("\n"),
275
+ )
248
276
  }
249
- if (action === "delete") return text(`deleted ${oneLine(result.item.id || id, 40)}: ${oneLine(result.item.what, 300)}. It is gone from this space's records.`)
250
277
  const verb = action === "add" ? "added" : "updated"
251
278
  const lines = [`${verb} ${describe(result.item, zone)}`]
252
279
  if (dropped) lines.push(dropped)
280
+ // What is open now, from the same scope `list` reads (0.10.0 gate, H2).
281
+ // A new record is tagged as new (0.10.3 gate, B1 and B13): the list
282
+ // otherwise reads like a record that was already there.
283
+ lines.push(
284
+ openNowSummary(result.records?.items, today, zone, action === "add" ? { mark: { id: result.item.id, label: "just added" } } : {}),
285
+ )
253
286
  if (action === "add") {
254
287
  const similar = similarOpen(result.records?.items, result.item.what, result.item.id)
255
288
  if (similar.length > 0) {
@@ -4,6 +4,9 @@ import { oneLine, redactSecrets } from "../util.js"
4
4
  import { callRecords, RECORD_ID } from "./records-kit.js"
5
5
  import { writeCallRoot } from "../backend/records.js"
6
6
 
7
+ /** The title a held write is stored under, which the preview replaces with its own words. */
8
+ export const HELD_WRITE_TITLE = "Team write awaiting approval"
9
+
7
10
  const hash = (value) => createHash("sha256").update(value).digest("hex")
8
11
  const SPACE = /^[A-Za-z0-9_.-]{1,128}$/
9
12
 
@@ -35,7 +38,7 @@ export async function holdTeamWrite(toolkit, context, spaceContext, records) {
35
38
  const content = String(records[0]?.body ?? "")
36
39
  const preview = oneLine(redactSecrets(content), 400)
37
40
  const record = {
38
- kind: "proposal", type: "team_write", title: "Team write awaiting approval", body: JSON.stringify({ records }),
41
+ kind: "proposal", type: "team_write", title: HELD_WRITE_TITLE, body: JSON.stringify({ records }),
39
42
  fields: { space_id: spaceContext.space_id, space_name: oneLine(spaceContext.space_name, 200), preview, key: idempotencyKey },
40
43
  source: {
41
44
  ...(context?.sessionID ? { session: String(context.sessionID) } : {}),
@@ -75,20 +78,36 @@ export async function decideProposal({ key, decision, caller }) {
75
78
  if (proposal.author?.user !== personal.user_id) throw new Error("only the proposal's author can decide")
76
79
  let shared
77
80
  if (decision === "accept" && ["proposed", "accepting"].includes(proposal.status)) {
78
- shared = await runtime.resolveContext({ space: proposal.fields.space_id })
79
- if (shared?.space_id !== proposal.fields.space_id || shared?.space_kind !== "shared" || shared?.user_id !== personal.user_id || !["editor", "admin", "owner"].includes(String(shared.role ?? "").toLowerCase())) throw new Error("the author no longer has write access to the target shared space")
81
+ shared = await sharedFor(runtime, personal, proposal)
80
82
  }
81
- let answer = await runtime.recordsClient.call("review.decide", { id, decision, base_version: proposal.version }, options)
83
+ const answer = await runtime.recordsClient.call("review.decide", { id, decision, base_version: proposal.version }, options)
82
84
  const held = answer?.proposal
83
85
  if (decision !== "accept" || held?.status !== "accepting") return answer
84
86
  if (!shared) throw new Error("the shared space was not verified")
85
- const target = { space: shared.space_id }
86
- const stats = await runtime.recordsClient.call("space.stats", {}, target)
87
- if (stats?.held_receipts !== true) throw new Error("the shared records service cannot verify held acceptance")
88
- const records = JSON.parse(held.body).records
89
- const written = await runtime.recordsClient.call("records.put", { held_batch: { key: held.fields.accept_key, records } }, target)
90
- if (typeof written?.receipt !== "string") throw new Error("the shared write was not confirmed; retry acceptance")
91
- answer = await runtime.recordsClient.call("review.decide", { id, decision: "accept", base_version: held.version, receipt: written.receipt }, options)
92
- return answer
87
+ return completeHeldWrite({ runtime, space, id, held, shared })
93
88
  })
94
89
  }
90
+
91
+ /** The shared space a held write is meant for, if this author may still write it. */
92
+ async function sharedFor(runtime, personal, proposal) {
93
+ const shared = await runtime.resolveContext({ space: proposal.fields.space_id })
94
+ if (shared?.space_id !== proposal.fields.space_id || shared?.space_kind !== "shared" || shared?.user_id !== personal.user_id || !["editor", "admin", "owner"].includes(String(shared.role ?? "").toLowerCase())) throw new Error("the author no longer has write access to the target shared space")
95
+ return shared
96
+ }
97
+
98
+ /**
99
+ * The second half of an accept, and the one copy of it: the author's editor
100
+ * right is re-checked, the held batch is written to the shared cell with its
101
+ * signed receipt, and the proposal is completed to `applied`. The act path
102
+ * (inbox.decide, review.decide) reaches the same `accepting` state records
103
+ * answers, and calls this with the proposal as it read it back.
104
+ */
105
+ export async function completeHeldWrite({ runtime, space, id, held, shared }) {
106
+ const target = { space: shared.space_id }
107
+ const stats = await runtime.recordsClient.call("space.stats", {}, target)
108
+ if (stats?.held_receipts !== true) throw new Error("the shared records service cannot verify held acceptance")
109
+ const records = JSON.parse(held.body).records
110
+ const written = await runtime.recordsClient.call("records.put", { held_batch: { key: held.fields.accept_key, records } }, target)
111
+ if (typeof written?.receipt !== "string") throw new Error("the shared write was not confirmed; retry acceptance")
112
+ return runtime.recordsClient.call("review.decide", { id, decision: "accept", base_version: held.version, receipt: written.receipt }, { space })
113
+ }
@@ -2,6 +2,8 @@ import { MEM_NAMESPACE_DESCRIPTION, PINNED_TOOLS, WIKI_DOCUMENTS_NOTE, WRITE_ACT
2
2
  import { asSession, asVerifiedSession } from "../credentials.js"
3
3
  import { withWriteCall } from "../backend/records.js"
4
4
  import { createCollectorService } from "../collector-service.js"
5
+ import { createPluginDiscovery } from "../plugins/discovery.js"
6
+ import { createJobHost } from "../jobs/host.js"
5
7
  import { createToolkit } from "./shared.js"
6
8
  import { registerWiki } from "./wiki.js"
7
9
  import { registerShare } from "./share.js"
@@ -37,6 +39,17 @@ export async function registerTools(ctx, runtime, { core = CORE_TOOLS } = {}) {
37
39
  if (!runtime.config.tools) return null
38
40
  const toolkit = createToolkit(ctx, runtime)
39
41
  toolkit.collector = createCollectorService({ ctx, runtime, toolkit })
42
+ // Plugin declarations (DESIGN-PLUGIN-MEMORY S1): read-only discovery and a
43
+ // report to the records service. It changes nothing the collector does.
44
+ toolkit.plugins = createPluginDiscovery({ ctx, runtime, toolkit })
45
+ runtime.onPluginsChanged = (type) => toolkit.plugins.changed(type)
46
+ // Memory Jobs' host (DESIGN-MEMORY-JOBS §3.2): the extract stage, in this
47
+ // process, beside the connector plugins and their sign-ins. It starts below
48
+ // with the collector's timer, behind the same switch: `collect: false` is a
49
+ // host that claims nothing.
50
+ toolkit.jobHost = createJobHost({ ctx, runtime, toolkit })
51
+ // Health and the tests read it here, as they read the collector.
52
+ runtime.jobHost = toolkit.jobHost
40
53
  const tools = await ctx.tool.transform((editor) => {
41
54
  editor.namespace({ name: "mem", description: MEM_NAMESPACE_DESCRIPTION })
42
55
  buildToolset(gateEditor(editor, runtime), toolkit, core)
@@ -54,8 +67,22 @@ export async function registerTools(ctx, runtime, { core = CORE_TOOLS } = {}) {
54
67
  } catch (error) {
55
68
  runtime.log?.warn?.("the collector could not start", error)
56
69
  }
70
+ try {
71
+ toolkit.plugins.start()
72
+ } catch (error) {
73
+ runtime.log?.warn?.("plugin discovery could not start", error)
74
+ }
75
+ // The job host's heartbeat is behind the collector's own switch: one switch
76
+ // turns off everything Memory does in the background in this process.
77
+ try {
78
+ if (runtime.config.collect) toolkit.jobHost.start()
79
+ } catch (error) {
80
+ runtime.log?.warn?.("the job host could not start", error)
81
+ }
57
82
  return {
58
83
  dispose: async () => {
84
+ await toolkit.jobHost.stop()
85
+ await toolkit.plugins.stop()
59
86
  await toolkit.collector.stop()
60
87
  await tools?.dispose?.()
61
88
  await command?.dispose?.()
@@ -1,4 +1,6 @@
1
1
  import { describeResult } from "../collector-service.js"
2
+ import { asksToCollect, latestWord, mustRefuse } from "../requests.js"
3
+ import { errorMessage } from "../util.js"
2
4
 
3
5
  /**
4
6
  * 0.9.7 (security review of audit tools #4): `mem_ingest` and `mem_cursor` are
@@ -83,6 +85,13 @@ export function registerIngest(editor, _toolkit) {
83
85
  })
84
86
  }
85
87
 
88
+ /**
89
+ * The answer a collect gets when the person's own latest message did not ask
90
+ * for one. The words are the ones a person uses to ask; the agent repeats
91
+ * them, so nothing happens this turn (0.10.6 gate, life-inject-chain-email #4).
92
+ */
93
+ const COLLECT_NEEDS_REQUEST = "Collecting needs your request. Offer it: say 'collect my mail now' and I will pull the latest from your sources."
94
+
86
95
  /**
87
96
  * `mem_collect`: collect now. For routines (Jelly's `collect` routine calls
88
97
  * `await tools.mem.collect({})` and then returns false) and the person's
@@ -91,6 +100,13 @@ export function registerIngest(editor, _toolkit) {
91
100
  * as the timer (src/collector-service.js), and its counts appear in
92
101
  * `mem_spaces` health; `wait: true` waits up to 25 seconds and reports them.
93
102
  *
103
+ * 0.10.6 gate H3: the description said "only for routines and the person's
104
+ * explicit request" and an agent collected from Gmail while answering a
105
+ * question about an email. The check is here, in code: with a person message
106
+ * in the session, the latest one must ask (src/requests.js). A routine turn
107
+ * has none, so the routine path is unchanged, and so is a host that runs no
108
+ * prompt hook.
109
+ *
94
110
  * @param {any} editor
95
111
  * @param {ReturnType<import("./shared.js").createToolkit>} toolkit
96
112
  */
@@ -119,6 +135,13 @@ export function registerCollect(editor, toolkit) {
119
135
  },
120
136
  options: { namespace: "mem" },
121
137
  execute: async (input, context) => {
138
+ // The guard, not the description: a model's call runs only when the
139
+ // person's latest message asks, and an agent's brief never counts. A
140
+ // host's direct execution (a routine's check) carries no call id, so it
141
+ // is not a model call and collects as before.
142
+ await toolkit.ensureSession?.(context?.sessionID)
143
+ const said = latestWord(toolkit.runtime, context?.sessionID, context)
144
+ if (mustRefuse(said, asksToCollect(said.text))) return { output: { skipped: COLLECT_NEEDS_REQUEST }, content: COLLECT_NEEDS_REQUEST }
122
145
  const outcome = await toolkit.collector.trigger({ source: input?.source, wait: input?.wait === true, sessionID: context?.sessionID, from: "tool" })
123
146
  return { output: collectOutput(outcome), content: describeOutcome(outcome) }
124
147
  },
@@ -133,7 +156,7 @@ function collectOutput(outcome) {
133
156
  if (outcome.skipped) output.skipped = outcome.skipped
134
157
  if ("done" in outcome) output.done = Boolean(outcome.done)
135
158
  const sources = outcome.run?.sources ?? outcome.progress
136
- if (Array.isArray(sources)) output.sources = sources.map(({ source, account, pages, accepted, duplicates, refused, more, error }) => ({ source, account, pages, accepted, duplicates, refused, more, ...(error ? { error } : {}) }))
159
+ if (Array.isArray(sources)) output.sources = sources.map(({ source, account, pages, accepted, duplicates, refused, more, error }) => ({ source, account, pages, accepted, duplicates, refused, more, ...(error ? { error: errorMessage(error) } : {}) }))
137
160
  return output
138
161
  }
139
162
 
@@ -148,7 +171,7 @@ export function describeOutcome(outcome) {
148
171
  }
149
172
  const run = outcome.run
150
173
  if (!run) return `${head}; it finished.`
151
- if (run.error) return `${head}; it failed: ${run.error}`
174
+ if (run.error) return `${head}; it failed: ${errorMessage(run.error)}`
152
175
  if (run.none || !run.sources?.length) return `${head}; nothing to collect: no source is enabled and confirmed. Bind one with mem_spaces ("action": "sources") when the person asks.`
153
176
  return ["Memory collection:", ...run.sources.map((result) => `- ${describeResult(result)}`)].join("\n")
154
177
  }
@@ -174,6 +197,8 @@ export async function registerCollectCommand(ctx, runtime, toolkit, { asSession
174
197
  description: "Collect now from the sources you bound to Memory (mail, chat, meetings), without a model turn",
175
198
  execute: ({ sessionID }) =>
176
199
  asSession(sessionID, async () => {
200
+ // /mem-collect also re-reads the installed plugins' declarations (read-only).
201
+ void toolkit.plugins?.scan?.("command")
177
202
  const outcome = await toolkit.collector.trigger({ wait: true, sessionID, from: "command" })
178
203
  await reply(sessionID, describeOutcome(outcome))
179
204
  }),
@@ -233,7 +233,7 @@ export function createCoreForget(toolkit) {
233
233
  if (notRemoved.length > 0) lines.push(`Kept the text of ${notRemoved.join(", ")}, but the atom was not removed from the core, so recall may still show it.`)
234
234
  if (keep.left.length > 0) {
235
235
  const list = keep.left.map((id) => oneLine(id, 80)).join(", ")
236
- lines.push(`Not replaced: ${list}. ${keep.left.length === 1 ? "Its" : "Their"} text could not be kept as history, so ${keep.left.length === 1 ? "it stays" : "they stay"} and recall may still show ${keep.left.length === 1 ? "it" : "them"}. Removing ${keep.left.length === 1 ? "it" : "them"} is a forget: ask the person first.`)
236
+ lines.push(`Not replaced: ${list}. ${keep.left.length === 1 ? "Its" : "Their"} text could not be kept as history, so ${keep.left.length === 1 ? "it stays" : "they stay"} and recall may still show ${keep.left.length === 1 ? "it" : "them"}. Removing ${keep.left.length === 1 ? "it" : "them"} is a forget: only on the person's request, never as a tidy-up.`)
237
237
  }
238
238
  lines.push("Captured turns that mention the old value stay as history.")
239
239
  return lines.join(" ")
@@ -1049,7 +1049,7 @@ export function registerRemember(editor, toolkit) {
1049
1049
  * @param {ReturnType<import("./shared.js").createToolkit>} toolkit
1050
1050
  */
1051
1051
  export function registerRevise(editor, toolkit) {
1052
- const { text, fail, log, scope, scopeFor, resolveReadSpace, assertTeamWrite, gateWrite, displayScope, taskFor, forgetLocations } = toolkit
1052
+ const { text, fail, log, runtime, scope, scopeFor, resolveReadSpace, assertTeamWrite, gateWrite, displayScope, taskFor, forgetLocations } = toolkit
1053
1053
  /**
1054
1054
  * The space a revise acts in: where this process last saw the id (recall,
1055
1055
  * remember, page), else the session's (personal, or a bound shared one).
@@ -1086,7 +1086,7 @@ export function registerRevise(editor, toolkit) {
1086
1086
  editor.add({
1087
1087
  name: "revise",
1088
1088
  description:
1089
- "Change memories by their ids (rec_… from mem_remember or mem_recall). Call it when the person says a memory changed or is wrong, or you find it so. correct writes a successor; confirm, retire, restore and history manage its lifecycle. To clean up many, list them with mem_recall's filter, retire them (restore undoes it), and forget only what the person asks to forget. forget deletes the record and its history and checks a bounded subset for copies; it cannot establish complete erasure. When the person asked you to forget it, the request is their consent. When you propose a deletion yourself (including a whole turn), ask the person first, and obtain separate consent before deleting it. Shared revisions follow the host permission policy, which may prompt or allow silently. Never put status, instructions, email addresses or local paths in content.",
1089
+ "Change memories by their ids (rec_… from mem_remember or mem_recall). Call it when the person says a memory changed or is wrong, or you find it so. correct writes a successor; confirm, retire, restore and history manage its lifecycle. To clean up many, list them with mem_recall's filter, retire them (restore undoes it), and forget only what the person asks to forget. forget deletes the record and its history and checks a bounded subset for copies; it cannot establish complete erasure. When the person asked you to forget it, the request is their consent. When you propose a deletion yourself (including a whole turn), report it, never tidy it up, and delete only on their request. Shared revisions follow the host permission policy, which may prompt or allow silently. Never put status, instructions, email addresses or local paths in content.",
1090
1090
  input: {
1091
1091
  type: "object",
1092
1092
  properties: {
@@ -1228,7 +1228,7 @@ export function registerRevise(editor, toolkit) {
1228
1228
  const scan = await createCoreForget(toolkit).scanCopies({ texts: [factOf(old)], spaceContext, base })
1229
1229
  const atoms = scan.copies.filter((row) => row.layer === "l1")
1230
1230
  correctionWarning = atoms.length
1231
- ? `Possible stale/conflicting atoms remain and may appear in recall (heuristic candidates, not verified lineage): ${atoms.map((row) => `${oneLine(row.id, 80)}: ${JSON.stringify(oneLine(row.content, 300))}`).join("; ")}. Leave them unless the person asks: removing one is a forget, so ask first.`
1231
+ ? `Possible stale/conflicting atoms remain and may appear in recall (heuristic candidates, not verified lineage): ${atoms.map((row) => `${oneLine(row.id, 80)}: ${JSON.stringify(oneLine(row.content, 300))}`).join("; ")}. Leave them unless the person asks: removing one is a forget, so only on their request, never as a tidy-up.`
1232
1232
  : "Old atoms and turns may still appear in recall; provenance and the bounded scan cannot rule out stale conflicts."
1233
1233
  if (scan.windows.some((window) => !window.listing.complete)) correctionWarning += " The core scan was incomplete."
1234
1234
  } catch (error) {
@@ -1305,7 +1305,7 @@ export function registerRevise(editor, toolkit) {
1305
1305
  function outcomeLines(results, done, undone = "not changed") {
1306
1306
  return results.map((item) => item.ok
1307
1307
  ? `- ${oneLine(item.id, 40)}: ${done}`
1308
- : `- ${oneLine(item.id, 40)}: ${undone} (${oneLine(item.error?.code ?? "error", 40)}): ${oneLine(item.error?.message ?? "", 200)}`)
1308
+ : `- ${oneLine(item.id, 40)}: ${undone} (${oneLine(item.error?.code ?? "error", 40)}): ${oneLine(errorMessage(item.error), 200)}`)
1309
1309
  }
1310
1310
 
1311
1311
  /**
@@ -1457,13 +1457,15 @@ export function registerRevise(editor, toolkit) {
1457
1457
  }
1458
1458
 
1459
1459
  let gone = []
1460
+ // A delete whose outcome the service did not report: the text may be gone,
1461
+ // so for capture the forget counts as done (fail closed).
1462
+ let deleteUnknown = false
1460
1463
  // Records the service refused to delete: still there by its answer, so the
1461
1464
  // read back does not report them as copies left behind.
1462
1465
  const refusedIDs = new Set()
1463
1466
  let refusedBlock = ""
1464
1467
  if (records.length > 0) {
1465
1468
  let data
1466
- let deleteUnknown = false
1467
1469
  const recordList = records.map((record) => record.id)
1468
1470
  try {
1469
1471
  // A batch forget authorises each id on its own (0.9.7); an older
@@ -1562,7 +1564,7 @@ export function registerRevise(editor, toolkit) {
1562
1564
  }
1563
1565
  // Forget, end to end on this machine: turns waiting in the spool or in
1564
1566
  // quarantine that hold the text are deleted, and later captures of this
1565
- // session (the asking turn, the next checkpoint) are masked.
1567
+ // session (the next checkpoint) are masked.
1566
1568
  for (const record of records) {
1567
1569
  if (!gone.includes(record.id)) continue
1568
1570
  purgeTexts.push(String(record.title ?? ""), factOf(record), String(record.body ?? "").replace(/^\s*why\s*:\s*/gim, ""))
@@ -1576,6 +1578,27 @@ export function registerRevise(editor, toolkit) {
1576
1578
  log.warn("forget purge failed", errorMessage(error))
1577
1579
  }
1578
1580
  }
1581
+ // The turn that asked restates the text: the person's request quotes it,
1582
+ // the answer often does, and this result can too. Masking misses a
1583
+ // paraphrase, so the turn it ran in (a subagent's runs in its parent's) is
1584
+ // not kept at all, and the exchange's captures are removed
1585
+ // (src/forget-turn.js). A delete the service did not confirm counts as
1586
+ // done here: the text may already be gone. A declined forget, or one that
1587
+ // deleted nothing, marks no turn.
1588
+ const removal = gone.length > 0 || coreGone > 0 || deleteUnknown ? await runtime?.noteForgetTurn?.(context?.sessionID) : null
1589
+ // The one instruction about the answer, whenever the text may be gone.
1590
+ if (gone.length > 0 || coreGone > 0 || deleteUnknown) parts.push("Do not repeat what was forgotten in your answer.")
1591
+ // Whether any turn was dropped, so the sentence says what happened rather
1592
+ // than what was expected (a background subagent's forget drops none).
1593
+ if (removal?.marked > 0) parts.push("This turn isn't kept where the text was restated.")
1594
+ // A copy of the turn that could not be confirmed erased: the person can
1595
+ // still ask for that turn to be removed, and only they can decide.
1596
+ if (removal?.failed > 0) parts.push("A copy of this turn could not be confirmed erased; tell the person you can remove that turn if they ask, and do it only with their consent.")
1597
+ // A turn the person steered away from, or that the host evicted, may hold
1598
+ // the text and is already kept: it is never deleted for them. The person
1599
+ // decides, through the consent path a retained turn always needs.
1600
+ const partial = context?.sessionID ? runtime?.partialTurnOf?.(context.sessionID) : null
1601
+ if (partial) parts.push("An earlier turn of this exchange may hold the text. Offer to remove it if the person wants; a kept turn is removed only with their consent.")
1579
1602
  log.info("revise forget", { records: gone.length, core: coreIDs.length, copies: copyIDs.length })
1580
1603
  parts.push("Coverage is incomplete: other agents (including a fifth or unknown agent), rows beyond 300 per layer/agent, older record versions, other users and spaces, L2/L3, wiki, backups and derived copies were not comprehensively searched. Searches use at most 20 record and 25 core hits per query and can miss paraphrases. The persona and scenarios (L2/L3) are not changed by an id forget; the person erases a phrase there on the Profile page of Memory's console.")
1581
1604
  return [parts.join(" "), refusedBlock, report].filter(Boolean).join("\n")