@joe-broadhead/opencode-mem 0.9.2 → 0.9.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1605 -0
- package/README.md +135 -43
- package/docs/agent-protocol.md +29 -28
- package/docs/permissions.md +148 -0
- package/package.json +277 -16
- package/skills/mem.pages/SKILL.md +35 -16
- package/skills/mem.sharing/SKILL.md +39 -6
- package/skills/mem.sources/SKILL.md +41 -10
- package/skills/mem.usage/SKILL.md +47 -16
- package/src/backend/records.js +413 -50
- package/src/backend/v3.js +15 -10
- package/src/call-shapes.js +18 -25
- package/src/capture-pause.js +136 -0
- package/src/capture.js +7 -4
- package/src/collector-service.js +212 -35
- package/src/collector.js +186 -47
- package/src/commands.js +37 -0
- package/src/compaction.js +4 -2
- package/src/config.js +162 -1
- package/src/connect.js +353 -0
- package/src/copies.js +28 -3
- package/src/credentials.js +35 -7
- package/src/curation.js +33 -14
- package/src/events.js +144 -36
- package/src/forget-purge.js +377 -0
- package/src/forgotten.js +356 -0
- package/src/hooks.js +9 -4
- package/src/index.js +25 -6
- package/src/permissions.js +116 -38
- package/src/protocol.js +21 -11
- package/src/records.js +4 -1
- package/src/runtime.js +207 -16
- package/src/shapes.js +63 -0
- package/src/sources.js +183 -12
- package/src/spaces.js +58 -1
- package/src/spool.js +442 -64
- package/src/stated.js +334 -0
- package/src/storable.js +477 -0
- package/src/tools/aliases.js +36 -147
- package/src/tools/commitments.js +69 -12
- package/src/tools/held-write.js +9 -1
- package/src/tools/index.js +29 -44
- package/src/tools/ingest.js +33 -128
- package/src/tools/memory.js +594 -901
- package/src/tools/page-sections.js +147 -0
- package/src/tools/page.js +314 -82
- package/src/tools/recall-browse.js +173 -0
- package/src/tools/recall.js +79 -19
- package/src/tools/share.js +494 -131
- package/src/tools/shared.js +97 -19
- package/src/tools/spaces.js +215 -104
- package/src/tools/status.js +31 -3
- package/src/tools/wiki.js +5 -13
- package/src/util.js +10 -20
- package/src/views/contracts.js +137 -6
- package/src/views/overdue.js +125 -0
- package/src/views/profile.js +154 -0
- package/src/views/records-reads.js +132 -33
- package/src/views/rpc.js +63 -12
- package/src/views/shape.js +49 -0
- package/views/commitments.html +31 -4
- package/views/home.html +22 -3
- package/views/memory.html +126 -24
- package/src/erase.js +0 -36
package/src/forgotten.js
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recall withholds copies of forgotten text (docs/design/adr-forget-copies.md,
|
|
3
|
+
* "Recall withholds copies after a forget").
|
|
4
|
+
*
|
|
5
|
+
* A forget deletes a record and keeps the turns and atoms that copy it until
|
|
6
|
+
* the person consents to deleting each one. So that no later session answers
|
|
7
|
+
* from those copies, the forget stores a fingerprint in the space's records
|
|
8
|
+
* (`forgotten.put`): the service extracts the text's copy features with the
|
|
9
|
+
* same `copyFeatures` (records/src/copies.js is this repository's
|
|
10
|
+
* src/copies.js, byte for byte), hashes them with the author's own key, and
|
|
11
|
+
* keeps no text. Recall reads the caller's own fingerprints and key
|
|
12
|
+
* (`forgotten.list`) and withholds each of the caller's rows `matchCopy`
|
|
13
|
+
* would call a copy, comparing keyed hashes instead of text.
|
|
14
|
+
*
|
|
15
|
+
* The match is exact, not an approximation: `containsToken` finds an anchor
|
|
16
|
+
* bounded by non-letters-or-digits, so a token anchor (no whitespace, a letter
|
|
17
|
+
* or digit at each end) is a run of `pieces` letter-or-digit runs of the row,
|
|
18
|
+
* and any other anchor (a quoted phrase) is a bounded span of its length.
|
|
19
|
+
* Those spans and the row's stems are hashed and looked up.
|
|
20
|
+
* test/forgotten-parity.test.js holds the two equal on a corpus.
|
|
21
|
+
*
|
|
22
|
+
* The plugin package does not ship the records service, so the keyed hash is
|
|
23
|
+
* computed here as there (HMAC-SHA256, the first 16 bytes, base64url).
|
|
24
|
+
*/
|
|
25
|
+
import { createHmac } from "node:crypto"
|
|
26
|
+
import { MAX_MATCH_CHARS, MIN_STRONG_ANCHORS, PROPORTION, PROPORTIONAL_ABOVE, candidateStems } from "./copies.js"
|
|
27
|
+
|
|
28
|
+
/** The fingerprint ids `forgotten.put` mints. */
|
|
29
|
+
export const FORGOTTEN_ID = /^fp_[0-9A-HJKMNP-TV-Z]{26}$/
|
|
30
|
+
/** The feature rules this plugin matches by (records/src/forgotten.js FEATURE_VERSION). */
|
|
31
|
+
export const FEATURE_VERSION = 1
|
|
32
|
+
const HASH_BYTES = 16
|
|
33
|
+
const PIECE = /[\p{L}\p{N}]+/gu
|
|
34
|
+
const ALNUM = /[\p{L}\p{N}]/u
|
|
35
|
+
const SPACE = /\s/
|
|
36
|
+
|
|
37
|
+
/** The keyed hash of one feature, as the records service computes it. */
|
|
38
|
+
export function hashTerm(key, term) {
|
|
39
|
+
return createHmac("sha256", Buffer.from(String(key), "base64url")).update(String(term), "utf8").digest().subarray(0, HASH_BYTES).toString("base64url")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The code point that ends just before `index`, or "". */
|
|
43
|
+
function before(text, index) {
|
|
44
|
+
if (index <= 0) return ""
|
|
45
|
+
const low = text.charCodeAt(index - 1)
|
|
46
|
+
if (index >= 2 && low >= 0xdc00 && low <= 0xdfff) {
|
|
47
|
+
const high = text.charCodeAt(index - 2)
|
|
48
|
+
if (high >= 0xd800 && high <= 0xdbff) return text.slice(index - 2, index)
|
|
49
|
+
}
|
|
50
|
+
return text[index - 1]
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The code point that starts at `index`, or "". */
|
|
54
|
+
function at(text, index) {
|
|
55
|
+
if (index >= text.length) return ""
|
|
56
|
+
const point = text.codePointAt(index)
|
|
57
|
+
return String.fromCodePoint(point)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A matcher over the caller's own fingerprints, as `forgotten.list` answers
|
|
62
|
+
* them. `usable` is false when there are none it can apply; `unknown` counts
|
|
63
|
+
* fingerprints of feature rules this plugin does not know (a newer service),
|
|
64
|
+
* which it cannot apply: recall says so.
|
|
65
|
+
*
|
|
66
|
+
* @param {{ version?: number, key?: string | null, fingerprints?: any[] } | null | undefined} list
|
|
67
|
+
*/
|
|
68
|
+
export function createForgottenMatcher(list) {
|
|
69
|
+
const key = typeof list?.key === "string" && list.key ? list.key : ""
|
|
70
|
+
const all = Array.isArray(list?.fingerprints) ? list.fingerprints : []
|
|
71
|
+
const known = all.filter((fp) => fp && Number(fp.v) === FEATURE_VERSION && typeof fp.id === "string" && Array.isArray(fp.anchors) && Array.isArray(fp.stems))
|
|
72
|
+
const unknown = all.length - known.length
|
|
73
|
+
const prints = known.map((fp) => ({
|
|
74
|
+
id: fp.id,
|
|
75
|
+
anchors: new Set(fp.anchors),
|
|
76
|
+
strong: new Set(Array.isArray(fp.strong) ? fp.strong : []),
|
|
77
|
+
stems: new Set(fp.stems),
|
|
78
|
+
}))
|
|
79
|
+
const numbers = (field) => [...new Set(known.flatMap((fp) => (Array.isArray(fp[field]) ? fp[field] : [])).map(Number).filter((n) => Number.isInteger(n) && n >= 1))]
|
|
80
|
+
const pieces = numbers("pieces")
|
|
81
|
+
const phrases = numbers("phrases")
|
|
82
|
+
const memo = new Map()
|
|
83
|
+
const hashed = (term) => {
|
|
84
|
+
let value = memo.get(term)
|
|
85
|
+
if (value === undefined) {
|
|
86
|
+
value = hashTerm(key, term)
|
|
87
|
+
if (memo.size < 100_000) memo.set(term, value)
|
|
88
|
+
}
|
|
89
|
+
return value
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The hashes of every span of the row that could equal an anchor. */
|
|
93
|
+
function spanHashes(lower) {
|
|
94
|
+
const spans = new Set()
|
|
95
|
+
if (pieces.length > 0) {
|
|
96
|
+
const runs = [...lower.matchAll(PIECE)].map((found) => ({ start: found.index, end: found.index + found[0].length }))
|
|
97
|
+
for (let i = 0; i < runs.length; i += 1) {
|
|
98
|
+
for (const n of pieces) {
|
|
99
|
+
const last = runs[i + n - 1]
|
|
100
|
+
if (!last) continue
|
|
101
|
+
const span = lower.slice(runs[i].start, last.end)
|
|
102
|
+
if (n > 1 && SPACE.test(span)) continue
|
|
103
|
+
spans.add(hashed(span))
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (phrases.length > 0) {
|
|
108
|
+
for (let p = 0; p < lower.length; p += 1) {
|
|
109
|
+
const code = lower.charCodeAt(p)
|
|
110
|
+
if (code >= 0xdc00 && code <= 0xdfff) continue
|
|
111
|
+
if (ALNUM.test(before(lower, p))) continue
|
|
112
|
+
for (const length of phrases) {
|
|
113
|
+
const end = p + length
|
|
114
|
+
if (end > lower.length || ALNUM.test(at(lower, end))) continue
|
|
115
|
+
spans.add(hashed(lower.slice(p, end)))
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return spans
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The ids of every fingerprint the text copies, strongest first. */
|
|
123
|
+
function matches(text) {
|
|
124
|
+
if (!key || prints.length === 0) return []
|
|
125
|
+
// As matchCopy reads it: the first MAX_MATCH_CHARS characters, lower case.
|
|
126
|
+
const lower = String(text ?? "").slice(0, MAX_MATCH_CHARS).toLowerCase()
|
|
127
|
+
if (!lower.trim()) return []
|
|
128
|
+
const spans = spanHashes(lower)
|
|
129
|
+
const stems = new Set([...candidateStems(lower)].map(hashed))
|
|
130
|
+
const found = []
|
|
131
|
+
for (const fp of prints) {
|
|
132
|
+
let anchors = 0
|
|
133
|
+
let strong = 0
|
|
134
|
+
for (const hash of fp.anchors) {
|
|
135
|
+
if (!spans.has(hash)) continue
|
|
136
|
+
anchors += 1
|
|
137
|
+
if (fp.strong.has(hash)) strong += 1
|
|
138
|
+
}
|
|
139
|
+
const weak = anchors - strong
|
|
140
|
+
let shared = 0
|
|
141
|
+
for (const hash of fp.stems) if (stems.has(hash)) shared += 1
|
|
142
|
+
const signals = anchors + shared
|
|
143
|
+
let copy
|
|
144
|
+
if (fp.strong.size > 0) copy = (strong >= 1 && signals >= 2) || weak >= 2
|
|
145
|
+
else if (fp.anchors.size > 0) copy = weak >= 1 && signals >= 2
|
|
146
|
+
else copy = fp.stems.size >= 2 && shared >= 2 && shared >= Math.ceil(0.6 * fp.stems.size)
|
|
147
|
+
const size = fp.anchors.size + fp.stems.size
|
|
148
|
+
if (copy && size > PROPORTIONAL_ABOVE && signals < Math.ceil(PROPORTION * size) && strong < MIN_STRONG_ANCHORS) copy = false
|
|
149
|
+
if (copy) found.push({ id: fp.id, strength: anchors * 2 + shared })
|
|
150
|
+
}
|
|
151
|
+
return found.sort((a, b) => b.strength - a.strength).map((entry) => entry.id)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
usable: Boolean(key) && prints.length > 0,
|
|
156
|
+
unknown,
|
|
157
|
+
ids: prints.map((fp) => fp.id),
|
|
158
|
+
matches,
|
|
159
|
+
match: (text) => matches(text)[0] ?? null,
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Split rows into those recall may serve and those that copy text the caller
|
|
165
|
+
* forgot. A fingerprint is its author's, and recall reads only the caller's,
|
|
166
|
+
* so a row another user wrote is never withheld by it.
|
|
167
|
+
*
|
|
168
|
+
* @template T
|
|
169
|
+
* @param {T[]} rows
|
|
170
|
+
* @param {ReturnType<typeof createForgottenMatcher> | null} matcher
|
|
171
|
+
* @param {string} [userID] the caller
|
|
172
|
+
* Matching is bounded in time: `budget.ms` is the matching time left for the
|
|
173
|
+
* whole recall, and only the time spent matching counts against it (never a
|
|
174
|
+
* slow search). A row not checked once it runs out is withheld too, as
|
|
175
|
+
* unchecked, never served.
|
|
176
|
+
*
|
|
177
|
+
* @param {string} [layer] the rows' layer, which keys the withheld ids
|
|
178
|
+
* @param {{ ms: number }} [budget] shared by every call of one recall; spent here
|
|
179
|
+
* @returns {{ served: T[], hidden: string[], unchecked: string[] }} `layer:id` of each withheld row, never shown
|
|
180
|
+
*/
|
|
181
|
+
export function hideForgotten(rows, matcher, userID, layer = "", budget = { ms: Infinity }) {
|
|
182
|
+
if (!matcher?.usable || !Array.isArray(rows) || rows.length === 0) return { served: Array.isArray(rows) ? rows : [], hidden: [], unchecked: [] }
|
|
183
|
+
const served = []
|
|
184
|
+
const hidden = []
|
|
185
|
+
const unchecked = []
|
|
186
|
+
for (const row of rows) {
|
|
187
|
+
const any = /** @type {any} */ (row)
|
|
188
|
+
if (!(budget.ms > 0)) {
|
|
189
|
+
unchecked.push(`${layer}:${any?.id ?? unchecked.length}`)
|
|
190
|
+
continue
|
|
191
|
+
}
|
|
192
|
+
const author = any?.user_id ?? any?.userId
|
|
193
|
+
const own = !userID || author === undefined || author === null || author === "" || String(author) === String(userID)
|
|
194
|
+
const started = performance.now()
|
|
195
|
+
const copy = own && matcher.match(any?.content ?? any?.text ?? "")
|
|
196
|
+
budget.ms -= performance.now() - started
|
|
197
|
+
if (copy) hidden.push(`${layer}:${any?.id ?? hidden.length}`)
|
|
198
|
+
else served.push(row)
|
|
199
|
+
}
|
|
200
|
+
return { served, hidden, unchecked }
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Recall's time for matching rows against fingerprints, in milliseconds: matching only, never the searches. */
|
|
204
|
+
export const MATCH_BUDGET_MS = 1_000
|
|
205
|
+
|
|
206
|
+
// ---------------------------------------------------------------------------
|
|
207
|
+
// The purge contract, client side (0.9.8 slice 6b, docs/design/forget-erase.md
|
|
208
|
+
// "The purge contract"). `forgotten.match` answers the caller's own purge
|
|
209
|
+
// entries: a salt, word counts and keyed hashes of each sentence a forget
|
|
210
|
+
// erased, never the text. A text matches an entry when a run of its
|
|
211
|
+
// normalised words hashes, under the entry's salt, to one of the entry's
|
|
212
|
+
// hashes. This is an equal copy of `purgeMatcher` in records/src/forgotten.js
|
|
213
|
+
// (the plugin package does not ship the records service);
|
|
214
|
+
// test/forget-purge.test.js holds the two equal.
|
|
215
|
+
|
|
216
|
+
/** Sentences shorter than this have no purge hash (records TEXT_MIN_WORDS). */
|
|
217
|
+
export const PURGE_MIN_WORDS = 3
|
|
218
|
+
/** A sentence is cut to this many words before it is hashed (records TEXT_MAX_WORDS). */
|
|
219
|
+
export const PURGE_MAX_WORDS = 40
|
|
220
|
+
/** Hashes one local entry keeps (records MAX_PURGE_HASHES). */
|
|
221
|
+
const MAX_LOCAL_HASHES = 2_000
|
|
222
|
+
/** The text one local entry hashes, at most. */
|
|
223
|
+
const MAX_LOCAL_CHARS = 20_000
|
|
224
|
+
const WORD = /[\p{L}\p{N}]+/gu
|
|
225
|
+
|
|
226
|
+
/** The words of a text as a purge entry compares them: NFKC, lower case, letter-or-digit runs. */
|
|
227
|
+
export function normalWords(text) {
|
|
228
|
+
return String(text ?? "")
|
|
229
|
+
.normalize("NFKC")
|
|
230
|
+
.toLowerCase()
|
|
231
|
+
.split(/[^\p{L}\p{N}]+/u)
|
|
232
|
+
.filter(Boolean)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* The normalised sentences of a text, as the records service splits a
|
|
237
|
+
* forgotten text (`tombstoneSentences`): at sentence ends, line breaks and
|
|
238
|
+
* quotation marks, each cut to PURGE_MAX_WORDS words, shorter than
|
|
239
|
+
* PURGE_MIN_WORDS left out.
|
|
240
|
+
*
|
|
241
|
+
* @returns {string[][]}
|
|
242
|
+
*/
|
|
243
|
+
export function purgeSentences(text) {
|
|
244
|
+
const out = []
|
|
245
|
+
for (const piece of String(text ?? "").slice(0, MAX_LOCAL_CHARS).split(/[.!?;\n]+(?=\s|$)|\n+|["“”«»]/u)) {
|
|
246
|
+
const words = normalWords(piece).slice(0, PURGE_MAX_WORDS)
|
|
247
|
+
if (words.length >= PURGE_MIN_WORDS) out.push(words)
|
|
248
|
+
}
|
|
249
|
+
return out
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** A purge entry's hash of a run of words. */
|
|
253
|
+
export function purgeHash(salt, words) {
|
|
254
|
+
return hashTerm(salt, words.join(" "))
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* A purge entry the plugin makes itself from text it holds when it makes a
|
|
259
|
+
* forget (the contract: "the client must also match the text it already holds
|
|
260
|
+
* when it makes a forget itself"). It covers the moments before the
|
|
261
|
+
* service's entry is fetched, and a service older than 0.9.8. Like the
|
|
262
|
+
* service's, it holds a salt of its own and hashes, never the text.
|
|
263
|
+
*
|
|
264
|
+
* @param {string[]} texts
|
|
265
|
+
* @param {{ id: string, salt: string, created: string, expires: string }} meta
|
|
266
|
+
*/
|
|
267
|
+
export function localPurgeEntry(texts, meta) {
|
|
268
|
+
const hashes = new Set()
|
|
269
|
+
const lengths = new Set()
|
|
270
|
+
for (const text of texts) {
|
|
271
|
+
for (const words of purgeSentences(text)) {
|
|
272
|
+
if (hashes.size >= MAX_LOCAL_HASHES) break
|
|
273
|
+
const before = hashes.size
|
|
274
|
+
hashes.add(purgeHash(meta.salt, words))
|
|
275
|
+
if (hashes.size > before) lengths.add(words.length)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (hashes.size === 0) return null
|
|
279
|
+
return { ...meta, lengths: [...lengths].sort((a, b) => a - b), hashes: [...hashes].sort() }
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* The client's matcher over purge entries, equal to the records service's
|
|
284
|
+
* `purgeMatcher`: `matches(text)` answers the ids of the entries a text
|
|
285
|
+
* matches. `mask(text)` replaces each matching run of words with
|
|
286
|
+
* `[forgotten]` and answers `{ text, hits }`; a text with no hit comes back
|
|
287
|
+
* unchanged.
|
|
288
|
+
*
|
|
289
|
+
* @param {Array<{id: string, salt: string, lengths: number[], hashes: string[]}>} entries
|
|
290
|
+
*/
|
|
291
|
+
export function purgeMatcher(entries) {
|
|
292
|
+
const list = (Array.isArray(entries) ? entries : []).filter((e) => e && typeof e.salt === "string" && Array.isArray(e.hashes) && Array.isArray(e.lengths))
|
|
293
|
+
const prepared = list.map((e) => ({ id: e.id, salt: e.salt, lengths: e.lengths.map(Number).filter((n) => Number.isInteger(n) && n >= 1), hashes: new Set(e.hashes) }))
|
|
294
|
+
|
|
295
|
+
/** Every [start, end) word range that hits, with the entry it hits; with `all` false, the first per entry. */
|
|
296
|
+
function hitsOf(words, all) {
|
|
297
|
+
const hits = []
|
|
298
|
+
for (const entry of prepared) {
|
|
299
|
+
let found = false
|
|
300
|
+
for (const n of entry.lengths) {
|
|
301
|
+
for (let i = 0; i + n <= words.length; i += 1) {
|
|
302
|
+
if (!entry.hashes.has(purgeHash(entry.salt, words.slice(i, i + n)))) continue
|
|
303
|
+
hits.push({ id: entry.id, start: i, end: i + n })
|
|
304
|
+
found = true
|
|
305
|
+
if (!all) break
|
|
306
|
+
}
|
|
307
|
+
if (found && !all) break
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return hits
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
size: prepared.length,
|
|
315
|
+
ids: prepared.map((entry) => entry.id),
|
|
316
|
+
matches(text) {
|
|
317
|
+
if (prepared.length === 0) return []
|
|
318
|
+
return [...new Set(hitsOf(normalWords(text), false).map((hit) => hit.id))]
|
|
319
|
+
},
|
|
320
|
+
mask(text) {
|
|
321
|
+
const source = String(text ?? "")
|
|
322
|
+
if (prepared.length === 0 || !source) return { text: source, hits: [] }
|
|
323
|
+
const words = normalWords(source)
|
|
324
|
+
const hits = hitsOf(words, true)
|
|
325
|
+
if (hits.length === 0) return { text: source, hits: [] }
|
|
326
|
+
const ids = [...new Set(hits.map((hit) => hit.id))]
|
|
327
|
+
// Word positions in the NFKC text. Each letter-or-digit run there is
|
|
328
|
+
// normalised on its own; when the runs do not give the same words as
|
|
329
|
+
// the whole text (a case change that splits a run), the whole text is
|
|
330
|
+
// masked rather than guessed at.
|
|
331
|
+
const nfkc = source.normalize("NFKC")
|
|
332
|
+
const spans = []
|
|
333
|
+
const runWords = []
|
|
334
|
+
for (const run of nfkc.matchAll(WORD)) {
|
|
335
|
+
for (const word of normalWords(run[0])) {
|
|
336
|
+
spans.push({ start: run.index, end: run.index + run[0].length })
|
|
337
|
+
runWords.push(word)
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (runWords.join(" ") !== words.join(" ")) return { text: "[forgotten]", hits: ids }
|
|
341
|
+
const covered = new Array(words.length).fill(false)
|
|
342
|
+
for (const hit of hits) for (let i = hit.start; i < hit.end; i += 1) covered[i] = true
|
|
343
|
+
let out = ""
|
|
344
|
+
let at = 0
|
|
345
|
+
for (let i = 0; i < words.length; i += 1) {
|
|
346
|
+
if (!covered[i]) continue
|
|
347
|
+
let j = i
|
|
348
|
+
while (j + 1 < words.length && covered[j + 1]) j += 1
|
|
349
|
+
out += nfkc.slice(at, spans[i].start) + "[forgotten]"
|
|
350
|
+
at = Math.max(at, spans[j].end)
|
|
351
|
+
i = j
|
|
352
|
+
}
|
|
353
|
+
return { text: out + nfkc.slice(at), hits: ids }
|
|
354
|
+
},
|
|
355
|
+
}
|
|
356
|
+
}
|
package/src/hooks.js
CHANGED
|
@@ -55,14 +55,14 @@ export async function attachMemoryHooks(ctx, runtime, options = {}) {
|
|
|
55
55
|
* `bootstrapChars` of prefix on every subagent model call. An unknown or
|
|
56
56
|
* slow answer counts as a root session.
|
|
57
57
|
*/
|
|
58
|
-
async function isSubagent(sessionID) {
|
|
58
|
+
async function isSubagent(sessionID, budgetMs = deadlineMs) {
|
|
59
59
|
if (config.captureSubagents || !sessionID) return false
|
|
60
60
|
let timer
|
|
61
61
|
try {
|
|
62
62
|
return await Promise.race([
|
|
63
63
|
isChildSession(ctx, runtime, sessionID),
|
|
64
64
|
new Promise((resolve) => {
|
|
65
|
-
timer = setTimeout(() => resolve(false),
|
|
65
|
+
timer = setTimeout(() => resolve(false), budgetMs)
|
|
66
66
|
}),
|
|
67
67
|
])
|
|
68
68
|
} catch {
|
|
@@ -78,6 +78,8 @@ export async function attachMemoryHooks(ctx, runtime, options = {}) {
|
|
|
78
78
|
const promptRegistration = await ctx.session.hook("prompt", (event) => asSession(event?.sessionID, () => {
|
|
79
79
|
try {
|
|
80
80
|
runtime.userTurn?.(event?.sessionID, event?.messageID)
|
|
81
|
+
// The person's words, for the writes that keep only what they said (src/stated.js).
|
|
82
|
+
runtime.notePrompt?.(event?.sessionID, event?.prompt?.text)
|
|
81
83
|
runtime.noteAgent?.(event?.sessionID, event?.agent)
|
|
82
84
|
if (!config.bootstrap) return
|
|
83
85
|
if (runtime.unavailable?.()) return
|
|
@@ -103,9 +105,12 @@ export async function attachMemoryHooks(ctx, runtime, options = {}) {
|
|
|
103
105
|
|
|
104
106
|
if (config.bootstrap) {
|
|
105
107
|
try {
|
|
106
|
-
// One deadline covers the subagent check and the bootstrap
|
|
108
|
+
// One deadline covers the subagent check and the bootstrap, but the
|
|
109
|
+
// check may use only a third of it. A host that answers `session.get`
|
|
110
|
+
// slowly would otherwise spend the whole deadline, leave the bootstrap
|
|
111
|
+
// 1 ms, and so guarantee a miss (and its 15 s negative cache).
|
|
107
112
|
const started = Date.now()
|
|
108
|
-
const child = await isSubagent(event.sessionID)
|
|
113
|
+
const child = await isSubagent(event.sessionID, Math.max(1, Math.floor(deadlineMs / 3)))
|
|
109
114
|
const timeoutMs = Math.max(1, deadlineMs - (Date.now() - started))
|
|
110
115
|
const block = child
|
|
111
116
|
? ""
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { configWarnings, resolveConfig } from "./config.js"
|
|
1
|
+
import { applySkipAsksSource, configWarnings, resolveConfig } from "./config.js"
|
|
2
2
|
import { asSession, createCredentials, followOrigin } from "./credentials.js"
|
|
3
|
+
import { createConnectSource, gatewayOrigin, registerConnect } from "./connect.js"
|
|
3
4
|
import { createLogger } from "./log.js"
|
|
4
5
|
import { createBackend } from "./backend/v3.js"
|
|
5
6
|
import { createRuntime } from "./runtime.js"
|
|
@@ -59,7 +60,12 @@ export default {
|
|
|
59
60
|
|
|
60
61
|
// Credentials and, inside Jelly, the gateway origin are read on use: the
|
|
61
62
|
// accessors Jelly's core plugin publishes may appear after this setup.
|
|
62
|
-
|
|
63
|
+
// With a gateway address, a person can also sign in with OpenCode's
|
|
64
|
+
// /connect (src/connect.js); OpenCode keeps and renews that credential.
|
|
65
|
+
const signIn = config.controlURL ? createConnectSource({ integration: ctx?.integration, origin: () => gatewayOrigin(config.controlURL), log }) : null
|
|
66
|
+
const credentials = createCredentials({ token: config.token, log, connect: signIn?.supported ? signIn : null })
|
|
67
|
+
// skipAsks: Jelly's setting inside Jelly, else the user-level config only.
|
|
68
|
+
applySkipAsksSource(config, { inJelly: () => credentials.inJelly() })
|
|
63
69
|
const jelly = !config.controlURL && credentials.inJelly()
|
|
64
70
|
if (jelly) followOrigin(config, () => credentials.origin())
|
|
65
71
|
const gateway = Boolean(config.controlURL) || jelly
|
|
@@ -67,12 +73,24 @@ export default {
|
|
|
67
73
|
const runtime = createRuntime({ config, log, backend, credentials, gateway })
|
|
68
74
|
runtime.pruneClaims()
|
|
69
75
|
|
|
76
|
+
// Register Memory in /connect, then learn whether a sign-in is stored.
|
|
77
|
+
let connectRegistration = null
|
|
78
|
+
if (signIn) {
|
|
79
|
+
try {
|
|
80
|
+
connectRegistration = await registerConnect(ctx, { origin: () => gatewayOrigin(config.controlURL), log })
|
|
81
|
+
} catch (error) {
|
|
82
|
+
log.warn("could not add Memory to /connect; keys still work", errorMessage(error))
|
|
83
|
+
}
|
|
84
|
+
if (signIn.supported && !config.token) await signIn.probe()
|
|
85
|
+
}
|
|
86
|
+
|
|
70
87
|
log.info("setup", { version, directory: ctx?.location?.directory, jelly, credential: credentials.source() || "none" })
|
|
71
88
|
for (const warning of configWarnings(config, { credentialSource: credentials.source(), gateway })) log.warn(warning)
|
|
72
89
|
const unavailable = runtime.unavailable()
|
|
73
90
|
if (unavailable) log.info("memory unavailable at setup; tools stay registered", { reason: unavailable })
|
|
74
91
|
|
|
75
92
|
const disposers = []
|
|
93
|
+
if (typeof connectRegistration?.dispose === "function") disposers.push(() => connectRegistration.dispose())
|
|
76
94
|
let skillState = { ok: true, count: 0, ids: [], spaceIds: [], sourceSpaces: 0, registration: null }
|
|
77
95
|
// Setup and a session bind can race. Coalesce them so control is asked once
|
|
78
96
|
// and the last requested refresh always runs after the current one.
|
|
@@ -90,8 +108,9 @@ export default {
|
|
|
90
108
|
// /mem-sync or bind asked for it.
|
|
91
109
|
skillRefreshPromise = asSession("", async () => {
|
|
92
110
|
try {
|
|
93
|
-
// Nothing to mirror from while memory is unavailable
|
|
94
|
-
|
|
111
|
+
// Nothing to mirror from while memory is unavailable (a /connect
|
|
112
|
+
// sign-in made since setup counts: OpenCode is asked first).
|
|
113
|
+
if (runtime.unavailable() && !(await credentials.probeSignIn?.())) return skillState
|
|
95
114
|
const next = await syncSkills(ctx, runtime, {
|
|
96
115
|
...options,
|
|
97
116
|
dispose: async () => {
|
|
@@ -134,9 +153,9 @@ export default {
|
|
|
134
153
|
log.error("tool registration failed", error)
|
|
135
154
|
}
|
|
136
155
|
|
|
137
|
-
//
|
|
156
|
+
// A forget asks before it runs, on every agent with a base allow (D17).
|
|
138
157
|
try {
|
|
139
|
-
const rules = await registerDefaultRules(ctx, log)
|
|
158
|
+
const rules = await registerDefaultRules(ctx, log, config)
|
|
140
159
|
if (typeof rules?.dispose === "function") disposers.push(() => rules.dispose())
|
|
141
160
|
} catch (error) {
|
|
142
161
|
log.error("default permission rules failed", error)
|