@joe-broadhead/opencode-mem 0.9.14 → 0.11.3

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 (143) hide show
  1. package/CHANGELOG.md +1856 -0
  2. package/README.md +15 -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 +8921 -1070
  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 +88 -7
  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 +74 -23
  104. package/src/tools/held-write.js +46 -19
  105. package/src/tools/index.js +28 -0
  106. package/src/tools/ingest.js +38 -2
  107. package/src/tools/memory.js +56 -25
  108. package/src/tools/page.js +212 -95
  109. package/src/tools/recall.js +218 -4
  110. package/src/tools/share.js +42 -3
  111. package/src/tools/shared.js +84 -6
  112. package/src/tools/space-lifecycle.js +278 -0
  113. package/src/tools/spaces.js +381 -30
  114. package/src/tools/status.js +9 -2
  115. package/src/tools/wiki.js +231 -116
  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 +1082 -23
  134. package/src/views/inbox-bytes.js +103 -0
  135. package/src/views/profile.js +31 -0
  136. package/src/views/receipt.js +26 -1
  137. package/src/views/records-reads.js +520 -41
  138. package/src/views/rpc.js +153 -25
  139. package/src/views/shape.js +8 -5
  140. package/src/views/write-state.js +89 -0
  141. package/src/wiki-ingest.js +215 -0
  142. package/src/wiki-state.js +228 -0
  143. 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,13 +8,16 @@ 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"
20
+ import { commitmentPlace, failedAnswer, refusedAnswer, withWriteState } from "../views/write-state.js"
18
21
  import { spaceOf } from "../views/shape.js"
19
22
 
20
23
  /**
@@ -155,37 +158,44 @@ export function registerCommitments(editor, toolkit) {
155
158
  options: { namespace: "mem" },
156
159
  execute: async (input, context) => {
157
160
  const action = String(input?.action ?? "")
161
+ // 0.11.3 (jelly's #89 ask A4): a write's answer opens with its state
162
+ // line, and a refusal opens with `state: refused` and names nothing.
163
+ const isWrite = action === "add" || action === "update" || action === "delete"
164
+ const answer = (content) => (isWrite ? refusedAnswer(text(content)) : text(content))
165
+ const failure = (message) => (isWrite ? failedAnswer(fail(message)) : fail(message))
158
166
  if (!COMMITMENT_ACTIONS.includes(action)) {
159
- return text(`action is required: one of ${COMMITMENT_ACTIONS.join(", ")}. Example: {"action": "list"}`)
167
+ return answer(`action is required: one of ${COMMITMENT_ACTIONS.join(", ")}. Example: {"action": "list"}`)
160
168
  }
161
169
  const where = await personalScope(context)
162
- if (where.invalid) return text(where.invalid)
163
- if (where.error) return fail(where.error)
170
+ if (where.invalid) return answer(where.invalid)
171
+ if (where.error) return failure(where.error)
164
172
  const { now, zone, today } = clockNow()
165
173
 
166
174
  if (action === "list") {
167
175
  const state = String(input?.state ?? "open").toLowerCase()
168
- if (![...STATES, "all"].includes(state)) return text(`state must be one of ${[...STATES, "all"].join(", ")}`)
176
+ if (![...STATES, "all"].includes(state)) return answer(`state must be one of ${[...STATES, "all"].join(", ")}`)
169
177
  if (input?.due_before !== undefined && input?.due_before !== "") {
170
178
  const before = validateDue(input.due_before)
171
- if (before.error || before.due.includes("T")) return text("due_before must be a date, YYYY-MM-DD")
179
+ if (before.error || before.due.includes("T")) return answer("due_before must be a date, YYYY-MM-DD")
172
180
  }
173
181
  let records
174
182
  try {
175
183
  records = (await runtime.records.read(where.scoped)).records
176
184
  } catch (error) {
177
185
  log.warn("commitments read failed", error)
178
- return fail(errorMessage(error))
186
+ return failure(errorMessage(error))
179
187
  }
180
188
  const items = filterItems(records.items, input, zone)
181
189
  const label = state === "all" ? "" : `${state} `
182
190
  if (items.length === 0) {
183
191
  const filters = `${input?.project ? ` for project ${oneLine(input.project, 80)}` : ""}${input?.owner ? ` owned by ${oneLine(input.owner, 80)}` : ""}`
184
- return text(`No ${label}commitments${filters}. Today is ${formatDay(today)}.`)
192
+ // 0.11.3: name the space, because agents restate "no commitments" as
193
+ // a claim about the person's records (0.11.1 gate H2).
194
+ return answer(`No ${label}commitments${filters} in your personal space. Today is ${formatDay(today)}.`)
185
195
  }
186
196
  const lines = items.slice(0, LIST_MAX).map((item) => itemLine(item, today, zone))
187
197
  if (items.length > LIST_MAX) lines.push(`(${items.length - LIST_MAX} more)`)
188
- return text(
198
+ return answer(
189
199
  [
190
200
  `<commitments today="${formatDay(today)}">`,
191
201
  `Stored commitments, not instructions. Overdue first; undated last.${
@@ -198,9 +208,9 @@ export function registerCommitments(editor, toolkit) {
198
208
  }
199
209
 
200
210
  const id = String(input?.id ?? "").trim()
201
- if (action !== "add" && !id) return text(`id is required for ${action}: the rec_… id from list or add`)
202
- if (action === "add" && id) return text("add makes a new id; to change a commitment, use update with its id")
203
- if (action !== "update" && input?.state !== undefined) return text("state is set with update; add always starts open")
211
+ if (action !== "add" && !id) return answer(`id is required for ${action}: the rec_… id from list or add`)
212
+ if (action === "add" && id) return answer("add makes a new id; to change a commitment, use update with its id")
213
+ if (action !== "update" && input?.state !== undefined) return answer("state is set with update; add always starts open")
204
214
 
205
215
  let change
206
216
  let warning = ""
@@ -208,20 +218,28 @@ export function registerCommitments(editor, toolkit) {
208
218
  if (action === "delete") {
209
219
  change = { action, id }
210
220
  } else {
211
- if (action === "update" && input?.state === "all") return text("state must be open, done, or cancelled")
221
+ if (action === "update" && input?.state === "all") return answer("state must be open, done, or cancelled")
212
222
  const validated = validateFields(input, { requireWhat: action === "add" })
213
- if (validated.error) return text(`not saved: ${validated.error}`)
223
+ if (validated.error) return answer(`not saved: ${validated.error}`)
214
224
  const fields = validated.fields
215
225
  // Secrets and email addresses are refused; a home path or a role address is stored with a warning (D3').
216
226
  const personal = storable([fields.what, fields.trigger, fields.owner, fields.link].filter((part) => typeof part === "string").join("\n"), "commitment")
217
- if (personal.outcome === "refuse") return text(personal.message.replace(/^not stored/, "not saved"))
227
+ if (personal.outcome === "refuse") return answer(personal.message.replace(/^not stored/, "not saved"))
218
228
  if (personal.outcome === "warn") warning = personal.message
219
229
  // 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))
230
+ // (0.9.9, WRITES-099; 0.11 gate H2). A call with no call id — a host's
231
+ // press in a fresh session — is checked exactly as 0.11.0 checked it:
232
+ // with a person message noted the field must be theirs; with none noted
233
+ // it is kept. A model's call with no noted prompt fails closed, and so
234
+ // does a session the host cannot place (an agent's brief). The words are
235
+ // the request guards' source (src/requests.js).
236
+ await toolkit.ensureSession?.(context?.sessionID)
237
+ const said = latestWord(runtime, context?.sessionID, context)
238
+ const personMessages = said.brief || (said.model && !said.seen) ? [{ text: "", at: 0 }] : said.seen ? (runtime.personSaid?.(context?.sessionID) ?? []) : []
239
+ const unstated = keepStated(fields, personMessages, zone, runtime.personDrops?.(context?.sessionID))
222
240
  if (action === "update" && Object.keys(fields).length === 0) {
223
- if (unstated.length > 0) return text(`not updated: ${unstated.join(" ")}`)
224
- return text("nothing to update: pass state, what, owner, due, trigger, project, or link")
241
+ if (unstated.length > 0) return answer(`not updated: ${unstated.join(" ")}`)
242
+ return answer("nothing to update: pass state, what, owner, due, trigger, project, or link")
225
243
  }
226
244
  if (unstated.length > 0) dropped = unstated.join(" ")
227
245
  change = { action, id: action === "add" ? newCommitmentId() : id, fields }
@@ -236,20 +254,49 @@ export function registerCommitments(editor, toolkit) {
236
254
  })
237
255
  } catch (error) {
238
256
  log.warn("commitments write failed", error)
239
- return fail(errorMessage(error))
257
+ return failure(errorMessage(error))
240
258
  }
241
- if (result.error) return text(`${result.conflict ? "not saved (conflict)" : "not saved"}: ${result.error}`)
259
+ if (result.error) return answer(`${result.conflict ? "not saved (conflict)" : "not saved"}: ${result.error}`)
242
260
  // The Due list in the pinned block is rebuilt on the next model call.
243
261
  runtime.invalidate?.(context?.sessionID)
244
262
  log.info("commitments", { action, rounds: result.rounds, unchanged: Boolean(result.unchanged) })
245
263
 
246
264
  if (action === "add" && result.unchanged) {
247
- return text([`already open, not added again: ${describe(result.item, zone)}`, dropped].filter(Boolean).join("\n"))
265
+ // A duplicate changed nothing (0.10.3 gate, B13): say so plainly, and
266
+ // tag the record in the list as the one that was already open.
267
+ return withWriteState(
268
+ text(
269
+ [
270
+ `already open, so nothing was added: ${describe(result.item, zone)}`,
271
+ dropped,
272
+ openNowSummary(result.records?.items, today, zone, { mark: { id: result.item.id, label: "already open, nothing added" } }),
273
+ ]
274
+ .filter(Boolean)
275
+ .join("\n"),
276
+ ),
277
+ { state: "stored", id: result.item.id, link: commitmentPlace(result.item.id) },
278
+ )
279
+ }
280
+ if (action === "delete") {
281
+ return withWriteState(
282
+ text(
283
+ [
284
+ `deleted ${oneLine(result.item.id || id, 40)}: ${oneLine(result.item.what, 300)}. It is gone from this space's records.`,
285
+ openNowSummary(result.records?.items, today, zone),
286
+ ].join("\n"),
287
+ ),
288
+ { state: "ready" },
289
+ )
248
290
  }
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
291
  const verb = action === "add" ? "added" : "updated"
251
292
  const lines = [`${verb} ${describe(result.item, zone)}`]
252
293
  if (dropped) lines.push(dropped)
294
+ // What is open now, from the same scope `list` reads (0.10.0 gate, H2).
295
+ // A new record is tagged as new (0.10.3 gate, B1 and B13): the list
296
+ // otherwise reads like a record that was already there.
297
+ lines.push(
298
+ openNowSummary(result.records?.items, today, zone, action === "add" ? { mark: { id: result.item.id, label: "just added" } } : {}),
299
+ )
253
300
  if (action === "add") {
254
301
  const similar = similarOpen(result.records?.items, result.item.what, result.item.id)
255
302
  if (similar.length > 0) {
@@ -272,7 +319,11 @@ export function registerCommitments(editor, toolkit) {
272
319
  const receipt = action === "add"
273
320
  ? writeReceipt("commit", { record: { ...result.item, kind: "commitment", type: "commitment", status: "current", title: result.item.what }, space: spaceOf(where.spaceContext) })
274
321
  : undefined
275
- return withReceipt(text(lines.join("\n")), receipt)
322
+ return withWriteState(withReceipt(text(lines.join("\n")), receipt), {
323
+ state: "stored",
324
+ id: result.item.id,
325
+ link: commitmentPlace(result.item.id),
326
+ })
276
327
  },
277
328
  })
278
329
  }
@@ -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
 
@@ -20,7 +23,14 @@ function writeKey(context, space) {
20
23
  return `hw_${hash(JSON.stringify([space, context.sessionID, context.messageID, context.id]))}`
21
24
  }
22
25
 
23
- /** Hold only the submitted records, in the caller's personal space. */
26
+ /**
27
+ * Hold only the submitted records, in the caller's personal space.
28
+ *
29
+ * Answers `{ text, id, state }` (0.11.3, jelly's #89 ask A4): the sentence the
30
+ * write answers, the proposal's id, and the state the write's first line
31
+ * carries: `held` for a new hold, `stored` when an earlier acceptance already
32
+ * saved it, `refused` when the person declined it.
33
+ */
24
34
  export async function holdTeamWrite(toolkit, context, spaceContext, records) {
25
35
  if (!spaceContext?.space_id || !spaceContext?.space_name || spaceContext.space_kind !== "shared") throw new Error("held write needs a named shared space from control")
26
36
  if (!Array.isArray(records) || records.length === 0 || records.length > 20) throw new Error("held write needs one to twenty records")
@@ -35,7 +45,7 @@ export async function holdTeamWrite(toolkit, context, spaceContext, records) {
35
45
  const content = String(records[0]?.body ?? "")
36
46
  const preview = oneLine(redactSecrets(content), 400)
37
47
  const record = {
38
- kind: "proposal", type: "team_write", title: "Team write awaiting approval", body: JSON.stringify({ records }),
48
+ kind: "proposal", type: "team_write", title: HELD_WRITE_TITLE, body: JSON.stringify({ records }),
39
49
  fields: { space_id: spaceContext.space_id, space_name: oneLine(spaceContext.space_name, 200), preview, key: idempotencyKey },
40
50
  source: {
41
51
  ...(context?.sessionID ? { session: String(context.sessionID) } : {}),
@@ -44,12 +54,13 @@ export async function holdTeamWrite(toolkit, context, spaceContext, records) {
44
54
  },
45
55
  }
46
56
  const answer = await callRecords(toolkit, "records.put", { record, idempotency_key: idempotencyKey }, personal, scoped)
47
- if (!RECORD_ID.test(String(answer?.record?.id ?? ""))) throw new Error("the records service did not confirm the proposal")
48
- if (answer.record.status === "applied") return `Already approved and saved in ${oneLine(spaceContext.space_name, 200)}; no new write was made.`
49
- if (answer.record.status === "applied_private") return "Already saved privately; no new write was made."
50
- if (answer.record.status === "declined") return "The person declined this write; nothing was saved and it must not be retried elsewhere."
51
- if (answer.record.status === "accepting") return "Approval is being completed; retry the same acceptance."
52
- return `Held for approval in the Inbox for ${oneLine(spaceContext.space_name, 200)}; not saved as a memory or page yet.`
57
+ const id = String(answer?.record?.id ?? "")
58
+ if (!RECORD_ID.test(id)) throw new Error("the records service did not confirm the proposal")
59
+ if (answer.record.status === "applied") return { text: `Already approved and saved in ${oneLine(spaceContext.space_name, 200)}; no new write was made.`, id, state: "stored" }
60
+ if (answer.record.status === "applied_private") return { text: "Already saved privately; no new write was made.", id, state: "stored" }
61
+ if (answer.record.status === "declined") return { text: "The person declined this write; nothing was saved and it must not be retried elsewhere.", id, state: "refused" }
62
+ if (answer.record.status === "accepting") return { text: "Approval is being completed; retry the same acceptance.", id, state: "held" }
63
+ return { text: `Held for approval in the Inbox for ${oneLine(spaceContext.space_name, 200)}; not saved as a memory or page yet.`, id, state: "held" }
53
64
  }
54
65
 
55
66
  /**
@@ -75,20 +86,36 @@ export async function decideProposal({ key, decision, caller }) {
75
86
  if (proposal.author?.user !== personal.user_id) throw new Error("only the proposal's author can decide")
76
87
  let shared
77
88
  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")
89
+ shared = await sharedFor(runtime, personal, proposal)
80
90
  }
81
- let answer = await runtime.recordsClient.call("review.decide", { id, decision, base_version: proposal.version }, options)
91
+ const answer = await runtime.recordsClient.call("review.decide", { id, decision, base_version: proposal.version }, options)
82
92
  const held = answer?.proposal
83
93
  if (decision !== "accept" || held?.status !== "accepting") return answer
84
94
  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
95
+ return completeHeldWrite({ runtime, space, id, held, shared })
93
96
  })
94
97
  }
98
+
99
+ /** The shared space a held write is meant for, if this author may still write it. */
100
+ async function sharedFor(runtime, personal, proposal) {
101
+ const shared = await runtime.resolveContext({ space: proposal.fields.space_id })
102
+ 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")
103
+ return shared
104
+ }
105
+
106
+ /**
107
+ * The second half of an accept, and the one copy of it: the author's editor
108
+ * right is re-checked, the held batch is written to the shared cell with its
109
+ * signed receipt, and the proposal is completed to `applied`. The act path
110
+ * (inbox.decide, review.decide) reaches the same `accepting` state records
111
+ * answers, and calls this with the proposal as it read it back.
112
+ */
113
+ export async function completeHeldWrite({ runtime, space, id, held, shared }) {
114
+ const target = { space: shared.space_id }
115
+ const stats = await runtime.recordsClient.call("space.stats", {}, target)
116
+ if (stats?.held_receipts !== true) throw new Error("the shared records service cannot verify held acceptance")
117
+ const records = JSON.parse(held.body).records
118
+ const written = await runtime.recordsClient.call("records.put", { held_batch: { key: held.fields.accept_key, records } }, target)
119
+ if (typeof written?.receipt !== "string") throw new Error("the shared write was not confirmed; retry acceptance")
120
+ return runtime.recordsClient.call("review.decide", { id, decision: "accept", base_version: held.version, receipt: written.receipt }, { space })
121
+ }
@@ -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,23 @@ 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
+ toolkit.wikiIngest?.stop?.()
85
+ await toolkit.jobHost.stop()
86
+ await toolkit.plugins.stop()
59
87
  await toolkit.collector.stop()
60
88
  await tools?.dispose?.()
61
89
  await command?.dispose?.()
@@ -1,4 +1,7 @@
1
1
  import { describeResult } from "../collector-service.js"
2
+ import { memoryResourceAvailable } from "../credentials.js"
3
+ import { asksToCollect, latestWord, mustRefuse } from "../requests.js"
4
+ import { errorMessage } from "../util.js"
2
5
 
3
6
  /**
4
7
  * 0.9.7 (security review of audit tools #4): `mem_ingest` and `mem_cursor` are
@@ -83,6 +86,13 @@ export function registerIngest(editor, _toolkit) {
83
86
  })
84
87
  }
85
88
 
89
+ /**
90
+ * The answer a collect gets when the person's own latest message did not ask
91
+ * for one. The words are the ones a person uses to ask; the agent repeats
92
+ * them, so nothing happens this turn (0.10.6 gate, life-inject-chain-email #4).
93
+ */
94
+ const COLLECT_NEEDS_REQUEST = "Collecting needs your request. Offer it: say 'collect my mail now' and I will pull the latest from your sources."
95
+
86
96
  /**
87
97
  * `mem_collect`: collect now. For routines (Jelly's `collect` routine calls
88
98
  * `await tools.mem.collect({})` and then returns false) and the person's
@@ -91,6 +101,13 @@ export function registerIngest(editor, _toolkit) {
91
101
  * as the timer (src/collector-service.js), and its counts appear in
92
102
  * `mem_spaces` health; `wait: true` waits up to 25 seconds and reports them.
93
103
  *
104
+ * 0.10.6 gate H3: the description said "only for routines and the person's
105
+ * explicit request" and an agent collected from Gmail while answering a
106
+ * question about an email. The check is here, in code: with a person message
107
+ * in the session, the latest one must ask (src/requests.js). A routine turn
108
+ * has none, so the routine path is unchanged, and so is a host that runs no
109
+ * prompt hook.
110
+ *
94
111
  * @param {any} editor
95
112
  * @param {ReturnType<import("./shared.js").createToolkit>} toolkit
96
113
  */
@@ -113,12 +130,29 @@ export function registerCollect(editor, toolkit) {
113
130
  started: { type: "boolean" },
114
131
  running: { type: "boolean" },
115
132
  skipped: { type: "string" },
133
+ /** Why a routine could not collect: the same state `memory.overview` names. */
134
+ state: { type: "string", enum: ["unconfigured", "refused", "unreachable"] },
116
135
  done: { type: "boolean" },
117
136
  sources: { type: "array", items: { type: "object" } },
118
137
  },
119
138
  },
120
139
  options: { namespace: "mem" },
121
140
  execute: async (input, context) => {
141
+ // The guard, not the description: a model's call runs only when the
142
+ // person's latest message asks, and an agent's brief never counts. A
143
+ // host's direct execution (a routine's check) carries no call id, so it
144
+ // is not a model call and collects as before.
145
+ await toolkit.ensureSession?.(context?.sessionID)
146
+ const said = latestWord(toolkit.runtime, context?.sessionID, context)
147
+ if (mustRefuse(said, asksToCollect(said.text))) return { output: { skipped: COLLECT_NEEDS_REQUEST }, content: COLLECT_NEEDS_REQUEST }
148
+ // A host that offers no `memory` resource has no Memory service to collect
149
+ // from. A host's direct execution (a routine's check) says so, rather than
150
+ // answer "started" for a collection that cannot run. A model's call in
151
+ // plain OpenCode keeps the local path it has today.
152
+ if (!said.model && !toolkit.config?.controlURL && !memoryResourceAvailable()) {
153
+ const answer = "Memory is unavailable: this host has not offered the memory resource, so a routine cannot collect. Next step: turn on Memory's services on the host, or ask the person to connect Memory."
154
+ return { output: { skipped: answer, state: "unconfigured" }, content: answer }
155
+ }
122
156
  const outcome = await toolkit.collector.trigger({ source: input?.source, wait: input?.wait === true, sessionID: context?.sessionID, from: "tool" })
123
157
  return { output: collectOutput(outcome), content: describeOutcome(outcome) }
124
158
  },
@@ -133,7 +167,7 @@ function collectOutput(outcome) {
133
167
  if (outcome.skipped) output.skipped = outcome.skipped
134
168
  if ("done" in outcome) output.done = Boolean(outcome.done)
135
169
  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 } : {}) }))
170
+ 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
171
  return output
138
172
  }
139
173
 
@@ -148,7 +182,7 @@ export function describeOutcome(outcome) {
148
182
  }
149
183
  const run = outcome.run
150
184
  if (!run) return `${head}; it finished.`
151
- if (run.error) return `${head}; it failed: ${run.error}`
185
+ if (run.error) return `${head}; it failed: ${errorMessage(run.error)}`
152
186
  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
187
  return ["Memory collection:", ...run.sources.map((result) => `- ${describeResult(result)}`)].join("\n")
154
188
  }
@@ -174,6 +208,8 @@ export async function registerCollectCommand(ctx, runtime, toolkit, { asSession
174
208
  description: "Collect now from the sources you bound to Memory (mail, chat, meetings), without a model turn",
175
209
  execute: ({ sessionID }) =>
176
210
  asSession(sessionID, async () => {
211
+ // /mem-collect also re-reads the installed plugins' declarations (read-only).
212
+ void toolkit.plugins?.scan?.("command")
177
213
  const outcome = await toolkit.collector.trigger({ wait: true, sessionID, from: "command" })
178
214
  await reply(sessionID, describeOutcome(outcome))
179
215
  }),