@joe-broadhead/opencode-mem 0.9.1 → 0.9.5

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/src/connect.js ADDED
@@ -0,0 +1,353 @@
1
+ /**
2
+ * Sign-in through OpenCode's `/connect` (docs/design/plugin-sign-in.md).
3
+ *
4
+ * The plugin adds integration `memory` ("Memory") with one OAuth method,
5
+ * `browser`. Its `authorize` runs the device authorization grant (RFC 8628)
6
+ * against the gateway: it asks for a code, gives OpenCode the URL to open, and
7
+ * its callback polls until the person approves in the browser. OpenCode
8
+ * stores the credential `{ type: "oauth", access, refresh, expires, metadata }`
9
+ * and calls `refresh` to renew it.
10
+ *
11
+ * **Only OpenCode rotates.** A refresh token is single use, and a second use
12
+ * revokes the sign-in. So after sign-in the plugin never calls the token
13
+ * endpoint itself: `createConnectSource` reads the current credential with
14
+ * `ctx.integration.connection.active` and `resolve`, and OpenCode refreshes it
15
+ * there. After a 401 the source reads once more; when OpenCode hands back the
16
+ * same refused token, the connection is marked **Sign in required**.
17
+ *
18
+ * Tokens are held in memory only and never logged.
19
+ */
20
+ import os from "node:os"
21
+ import { CredentialError } from "./credentials.js"
22
+
23
+ export const INTEGRATION_ID = "memory"
24
+ export const INTEGRATION_NAME = "Memory"
25
+ export const METHOD_ID = "browser"
26
+ export const CLIENT_ID = "opencode-mem"
27
+ export const DEVICE_GRANT = "urn:ietf:params:oauth:grant-type:device_code"
28
+ /** An access token is used until this long before it expires; then OpenCode is asked again. */
29
+ export const ACCESS_MARGIN_MS = 60_000
30
+ /** While not connected, OpenCode is asked again at most this often. */
31
+ export const PROBE_EVERY_MS = 5_000
32
+ const REQUEST_TIMEOUT_MS = 15_000
33
+ /** Consecutive network failures while polling before the sign-in gives up. */
34
+ const POLL_FAILURES_MAX = 5
35
+ export const SIGN_IN_AGAIN = "Memory's sign-in has ended. Run /connect, choose Memory, and sign in again."
36
+
37
+ /** The origin of a gateway address, or "". */
38
+ export function gatewayOrigin(value) {
39
+ if (typeof value !== "string" || !value) return ""
40
+ try {
41
+ const url = new URL(value)
42
+ return /^https?:$/.test(url.protocol) && !url.username && !url.password ? url.origin : ""
43
+ } catch {
44
+ return ""
45
+ }
46
+ }
47
+
48
+ function deviceLabel() {
49
+ let host = ""
50
+ try {
51
+ host = os.hostname()
52
+ } catch {
53
+ /* no host name */
54
+ }
55
+ return `OpenCode on ${host || "this computer"}`.slice(0, 80)
56
+ }
57
+
58
+ function defaultSleep(ms) {
59
+ return new Promise((resolve) => {
60
+ const timer = setTimeout(resolve, ms)
61
+ timer.unref?.()
62
+ })
63
+ }
64
+
65
+ /** Visible ASCII only: a bearer goes into an HTTP header. */
66
+ function usable(value) {
67
+ return typeof value === "string" && value.length > 0 && value.length <= 8192 && /^[!-~]+$/.test(value)
68
+ }
69
+
70
+ async function postForm(fetchImpl, url, form) {
71
+ const response = await fetchImpl(url, {
72
+ method: "POST",
73
+ headers: { "content-type": "application/x-www-form-urlencoded", accept: "application/json" },
74
+ body: new URLSearchParams(form).toString(),
75
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
76
+ })
77
+ let body = {}
78
+ try {
79
+ body = await response.json()
80
+ } catch {
81
+ body = {}
82
+ }
83
+ return { status: response.status, ok: response.ok, body: body && typeof body === "object" ? body : {} }
84
+ }
85
+
86
+ /** An OAuth error code fit to show: letters, digits and `_` only. */
87
+ function errorCode(body) {
88
+ const code = typeof body?.error === "string" ? body.error : ""
89
+ return /^[a-z_]{1,40}$/.test(code) ? code : "error"
90
+ }
91
+
92
+ /** The credential OpenCode stores, from a token response. */
93
+ export function credentialFrom(body, origin, now = Date.now(), previous = null) {
94
+ if (!usable(body?.access_token) || !usable(body?.refresh_token)) {
95
+ throw new Error("Memory's sign-in answered without a usable token. Run /connect and choose Memory again.")
96
+ }
97
+ const seconds = Number(body.expires_in)
98
+ const email = typeof body.email === "string" && body.email.length <= 254 ? body.email : previous?.metadata?.email
99
+ return {
100
+ type: "oauth",
101
+ methodID: METHOD_ID,
102
+ access: body.access_token,
103
+ refresh: body.refresh_token,
104
+ expires: Math.floor(now + (Number.isFinite(seconds) && seconds > 0 ? seconds : 60) * 1000),
105
+ metadata: { origin, ...(typeof email === "string" && email ? { email } : {}) },
106
+ }
107
+ }
108
+
109
+ /**
110
+ * Start a device sign-in at `origin`: the `authorize` answer OpenCode shows.
111
+ *
112
+ * @param {{ origin: string, fetch?: typeof fetch, now?: () => number, sleep?: (ms: number) => Promise<void>, label?: string }} input
113
+ */
114
+ export async function authorizeDevice(input) {
115
+ const origin = gatewayOrigin(input.origin)
116
+ if (!origin) {
117
+ throw new Error("No Memory address is set. Set the plugin's controlURL option (or MEMORY_CONTROL_URL), then run /connect again.")
118
+ }
119
+ const fetchImpl = input.fetch ?? globalThis.fetch
120
+ const now = input.now ?? Date.now
121
+ const sleep = input.sleep ?? defaultSleep
122
+ let started
123
+ try {
124
+ started = await postForm(fetchImpl, `${origin}/oauth/device/code`, { client_id: CLIENT_ID, label: input.label ?? deviceLabel() })
125
+ } catch {
126
+ throw new Error(`Memory at ${origin} could not be reached. Check the address and your network, then run /connect again.`)
127
+ }
128
+ if (started.status === 404) {
129
+ throw new Error(`Memory at ${origin} does not offer browser sign-in. Use a key from ${origin}/account in the token option instead.`)
130
+ }
131
+ const body = started.body
132
+ if (!started.ok || typeof body.device_code !== "string" || typeof body.user_code !== "string" || !/^[A-Z]{4}-[A-Z]{4}$/.test(body.user_code)) {
133
+ throw new Error(`Memory at ${origin} refused to start a sign-in (${errorCode(body)}). Try /connect again later.`)
134
+ }
135
+ // The plain page, never a link with the code in it: the person types the
136
+ // code, so a link someone else sends them cannot sign them in with one click.
137
+ const url = `${origin}/connect`
138
+ const expiresAt = now() + Math.max(1, Number(body.expires_in) || 600) * 1000
139
+ let interval = Math.max(0, Number(body.interval ?? 5)) * 1000
140
+
141
+ const callback = (async () => {
142
+ let failures = 0
143
+ while (now() < expiresAt) {
144
+ await sleep(interval)
145
+ let answer
146
+ try {
147
+ answer = await postForm(fetchImpl, `${origin}/oauth/token`, { grant_type: DEVICE_GRANT, device_code: body.device_code, client_id: CLIENT_ID })
148
+ } catch {
149
+ failures += 1
150
+ if (failures >= POLL_FAILURES_MAX) throw new Error(`Memory at ${origin} stopped answering during sign-in. Run /connect again.`)
151
+ continue
152
+ }
153
+ failures = 0
154
+ if (answer.ok) return credentialFrom(answer.body, origin, now())
155
+ const code = errorCode(answer.body)
156
+ if (code === "authorization_pending") continue
157
+ if (code === "slow_down") {
158
+ interval += 5000
159
+ continue
160
+ }
161
+ if (code === "access_denied") throw new Error("The Memory sign-in was denied in the browser.")
162
+ if (code === "expired_token") break
163
+ if (answer.status >= 500 || answer.status === 429) {
164
+ failures += 1
165
+ if (failures >= POLL_FAILURES_MAX) throw new Error(`Memory at ${origin} could not finish the sign-in (${code}). Run /connect again.`)
166
+ continue
167
+ }
168
+ throw new Error(`The Memory sign-in failed (${code}). Run /connect and choose Memory again.`)
169
+ }
170
+ throw new Error("The Memory sign-in timed out. Run /connect and choose Memory again.")
171
+ })()
172
+ // OpenCode may drop an attempt the person cancels; never an unhandled rejection.
173
+ callback.catch(() => {})
174
+
175
+ return {
176
+ url,
177
+ instructions: `Open the link, sign in with your work Google account if asked, and type the code ${body.user_code}. Approve only this sign-in that you just started.`,
178
+ expiresAt,
179
+ mode: "auto",
180
+ callback,
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Renew a stored credential: OpenCode calls this, and stores what it returns.
186
+ *
187
+ * @param {{ refresh?: string, metadata?: Record<string, unknown> }} credential
188
+ * @param {{ fetch?: typeof fetch, now?: () => number }} [options]
189
+ */
190
+ export async function refreshCredential(credential, options = {}) {
191
+ const origin = gatewayOrigin(String(credential?.metadata?.origin ?? ""))
192
+ if (!origin || !usable(credential?.refresh)) throw new Error(`AUTH_REQUIRED: ${SIGN_IN_AGAIN}`)
193
+ const fetchImpl = options.fetch ?? globalThis.fetch
194
+ const now = options.now ?? Date.now
195
+ let answer
196
+ try {
197
+ answer = await postForm(fetchImpl, `${origin}/oauth/token`, { grant_type: "refresh_token", refresh_token: credential.refresh, client_id: CLIENT_ID })
198
+ } catch {
199
+ throw new Error(`Memory at ${origin} could not be reached to renew the sign-in; it is tried again on the next request.`)
200
+ }
201
+ if (answer.ok) return credentialFrom(answer.body, origin, now(), credential)
202
+ const code = errorCode(answer.body)
203
+ if (code === "invalid_grant" || code === "invalid_client" || answer.status === 404) throw new Error(`AUTH_REQUIRED: ${SIGN_IN_AGAIN}`)
204
+ throw new Error(`Memory could not renew the sign-in just now (${code}); it is tried again on the next request.`)
205
+ }
206
+
207
+ /**
208
+ * Register Memory's `/connect` method. Nothing happens on a host without the
209
+ * integration API (OpenCode before 2.2), which keeps working with keys.
210
+ *
211
+ * @param {any} ctx the plugin context
212
+ * @param {{ origin: () => string, log?: any, fetch?: typeof fetch, now?: () => number, sleep?: (ms: number) => Promise<void>, label?: string }} options
213
+ */
214
+ export async function registerConnect(ctx, options) {
215
+ const integration = ctx?.integration
216
+ if (typeof integration?.transform !== "function") {
217
+ options.log?.info?.("this OpenCode has no integration API; sign in with a key instead of /connect")
218
+ return null
219
+ }
220
+ const registration = await integration.transform((editor) => {
221
+ editor.update(INTEGRATION_ID, (entry) => {
222
+ entry.name = INTEGRATION_NAME
223
+ })
224
+ editor.method.update({
225
+ integrationID: INTEGRATION_ID,
226
+ method: { id: METHOD_ID, type: "oauth", label: "Sign in with your browser" },
227
+ authorize: () => authorizeDevice({ origin: options.origin(), fetch: options.fetch, now: options.now, sleep: options.sleep, label: options.label }),
228
+ refresh: (credential) => refreshCredential(credential, { fetch: options.fetch, now: options.now }),
229
+ label: (credential) => {
230
+ const email = credential?.metadata?.email
231
+ return typeof email === "string" && email ? email : undefined
232
+ },
233
+ })
234
+ })
235
+ return registration ?? null
236
+ }
237
+
238
+ /**
239
+ * The bearer from OpenCode's stored Memory sign-in, for `src/credentials.js`.
240
+ *
241
+ * @param {{ integration?: any, origin: () => string, log?: any, now?: () => number }} input
242
+ */
243
+ export function createConnectSource(input) {
244
+ const integration = input.integration
245
+ const now = input.now ?? Date.now
246
+ const log = input.log ?? null
247
+ const supported = typeof integration?.connection?.active === "function" && typeof integration?.connection?.resolve === "function"
248
+ /** `{ access, expires, connection }` from the last read, or null. */
249
+ let cached = null
250
+ let connected = false
251
+ let probedAt = -Infinity
252
+ let reading = null
253
+ /** The access token reported as refused, while the connection shows Sign in required. */
254
+ let refused = ""
255
+ let mismatchLogged = false
256
+
257
+ async function status(connection, value) {
258
+ try {
259
+ await integration.connection.status?.({ integrationID: INTEGRATION_ID, connection, status: value })
260
+ } catch {
261
+ /* the next request reports again */
262
+ }
263
+ }
264
+
265
+ /** Read OpenCode's current credential (OpenCode renews an expired one here). One read in flight. */
266
+ function read() {
267
+ if (reading) return reading
268
+ reading = (async () => {
269
+ probedAt = now()
270
+ let connection
271
+ let credential
272
+ try {
273
+ connection = await integration.connection.active(INTEGRATION_ID)
274
+ credential = connection ? await integration.connection.resolve(connection) : undefined
275
+ } catch (error) {
276
+ cached = null
277
+ throw new CredentialError(`OpenCode could not read Memory's sign-in: ${error instanceof Error ? error.message.slice(0, 200) : "unknown error"}`, "unavailable")
278
+ }
279
+ if (!connection || credential?.type !== "oauth" || !usable(credential.access)) {
280
+ connected = false
281
+ cached = null
282
+ return null
283
+ }
284
+ const stored = gatewayOrigin(String(credential.metadata?.origin ?? ""))
285
+ if (stored !== input.origin()) {
286
+ if (!mismatchLogged) log?.warn?.("the Memory sign-in in /connect is for another Memory address; it is not used", { configured: input.origin() || "none" })
287
+ mismatchLogged = true
288
+ connected = false
289
+ cached = null
290
+ return null
291
+ }
292
+ connected = true
293
+ cached = { access: credential.access, expires: Number(credential.expires) || 0, connection }
294
+ if (refused && refused !== cached.access) {
295
+ refused = ""
296
+ await status(connection, undefined)
297
+ }
298
+ return cached
299
+ })().finally(() => {
300
+ reading = null
301
+ })
302
+ return reading
303
+ }
304
+
305
+ return {
306
+ /** The host can hold a Memory sign-in. */
307
+ supported,
308
+ /** OpenCode held a usable Memory sign-in at the last read. Synchronous. */
309
+ connected: () => supported && connected,
310
+ /** Learn whether a sign-in exists now; asks OpenCode at most every few seconds while there is none. */
311
+ async probe() {
312
+ if (!supported) return false
313
+ if (!connected && now() - probedAt < PROBE_EVERY_MS) return false
314
+ try {
315
+ await read()
316
+ } catch {
317
+ /* reported on use */
318
+ }
319
+ return connected
320
+ },
321
+ /** The access token for one request, or "" with no sign-in. */
322
+ async bearer() {
323
+ if (!supported) return ""
324
+ if (cached && cached.expires - ACCESS_MARGIN_MS > now()) return cached.access
325
+ const current = await read()
326
+ return current?.access ?? ""
327
+ },
328
+ /**
329
+ * After a 401: read OpenCode's credential once more. Returns a different
330
+ * access token, or "" when there is none; then the connection shows Sign
331
+ * in required.
332
+ */
333
+ async refresh(rejected) {
334
+ if (!supported || !rejected) return ""
335
+ if (cached?.access === rejected) cached = null
336
+ let current
337
+ try {
338
+ current = await read()
339
+ } catch {
340
+ return ""
341
+ }
342
+ if (current && current.access !== rejected) return current.access
343
+ if (current && refused !== rejected) {
344
+ refused = rejected
345
+ log?.warn?.("Memory refused the /connect sign-in; OpenCode shows Sign in required")
346
+ await status(current.connection, { status: "needs_auth", message: SIGN_IN_AGAIN })
347
+ }
348
+ return ""
349
+ },
350
+ /** True when `token` is the sign-in's current access token. */
351
+ owns: (token) => Boolean(token) && cached?.access === token,
352
+ }
353
+ }
package/src/copies.js CHANGED
@@ -49,6 +49,25 @@ const MAX_ANCHORS = 40
49
49
  const MAX_STEMS = 60
50
50
  /** Text kept per forgotten row; the specific tokens of a fact sit well inside it. */
51
51
  export const MAX_FORGOTTEN_CHARS = 2_000
52
+ /**
53
+ * Characters of a candidate row read by `matchCopy` (and by recall's
54
+ * fingerprint matcher, src/forgotten.js): a copy later in a longer row is not
55
+ * found. It bounds the work per row.
56
+ */
57
+ export const MAX_MATCH_CHARS = 8_000
58
+ /**
59
+ * A long fact (more features than this) is a copy only where a proportion of
60
+ * its features is shared: one number and a word in common do not make a row a
61
+ * copy of a paragraph. Measured in docs/design/adr-forget-copies.md.
62
+ */
63
+ export const PROPORTIONAL_ABOVE = 12
64
+ export const PROPORTION = 0.2
65
+ /**
66
+ * A row that shares this many of a long fact's strong anchors (numbers,
67
+ * identifiers, quoted text, mixed case) is a copy whatever the proportion:
68
+ * one quoted sentence of a long fact names its specifics.
69
+ */
70
+ export const MIN_STRONG_ANCHORS = 2
52
71
 
53
72
  const LETTERS_OR_DIGITS = /[\p{L}\p{N}]/u
54
73
  const QUOTED = /[`"“”]([^`"“”\n]{2,60})[`"“”]/gu
@@ -59,7 +78,7 @@ function stem(word) {
59
78
  return word
60
79
  }
61
80
 
62
- function trimToken(raw) {
81
+ export function trimToken(raw) {
63
82
  return raw.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, "")
64
83
  }
65
84
 
@@ -175,7 +194,7 @@ function containsToken(lower, anchor) {
175
194
  }
176
195
 
177
196
  /** The stems of every content word in a candidate row, anchors included. */
178
- function candidateStems(lower) {
197
+ export function candidateStems(lower) {
179
198
  const stems = new Set()
180
199
  for (const word of lower.match(/\p{L}{4,}/gu) ?? []) if (!STOPWORDS.has(word)) stems.add(stem(word))
181
200
  return stems
@@ -193,12 +212,16 @@ function candidateStems(lower) {
193
212
  * A weak anchor and topical words are not enough when the fact has a strong
194
213
  * one: "export runs at 04:00 UTC" is not a copy of "export runs at 02:30 UTC".
195
214
  *
215
+ * A fact with more than PROPORTIONAL_ABOVE features also needs a PROPORTION of
216
+ * them shared, unless the row shares MIN_STRONG_ANCHORS of its strong anchors.
217
+ * Only the first MAX_MATCH_CHARS characters of the row are read.
218
+ *
196
219
  * @param {Array<ReturnType<typeof copyFeatures>>} forgotten
197
220
  * @param {unknown} text
198
221
  * @returns {null | { anchors: string[], stems: string[] }}
199
222
  */
200
223
  export function matchCopy(forgotten, text) {
201
- const lower = String(text ?? "").toLowerCase()
224
+ const lower = String(text ?? "").slice(0, MAX_MATCH_CHARS).toLowerCase()
202
225
  if (!lower.trim()) return null
203
226
  const stems = candidateStems(lower)
204
227
  let best = null
@@ -212,6 +235,8 @@ export function matchCopy(forgotten, text) {
212
235
  if (features.strong.length > 0) copy = (strong >= 1 && signals >= 2) || weak >= 2
213
236
  else if (features.anchors.length > 0) copy = weak >= 1 && signals >= 2
214
237
  else copy = features.stems.length >= 2 && shared.length >= 2 && shared.length >= Math.ceil(0.6 * features.stems.length)
238
+ const size = features.anchors.length + features.stems.length
239
+ if (copy && size > PROPORTIONAL_ABOVE && signals < Math.ceil(PROPORTION * size) && strong < MIN_STRONG_ANCHORS) copy = false
215
240
  if (!copy) continue
216
241
  const strength = anchors.length * 2 + shared.length
217
242
  if (!best || strength > best.strength) best = { anchors, stems: shared, strength }
@@ -3,13 +3,18 @@
3
3
  *
4
4
  * Credentials, first match wins:
5
5
  *
6
- * 1. the `token` option or `MEMORY_TOKEN` (a memory key);
6
+ * 1. the `token` option or `MEMORY_TOKEN` (a memory key, for automation);
7
7
  * 2. Jelly's identity accessor, `globalThis[Symbol.for("jelly.identity")]`,
8
8
  * when it advertises the `memory` resource: a ten-minute resource token,
9
9
  * asked for **per OpenCode session and again at each user turn** (below),
10
10
  * cached until 60 s before it expires, one request in flight per session,
11
11
  * and asked for again once when the gateway answers 401;
12
- * 3. Jelly's plugin-secrets accessor,
12
+ * 3. OpenCode's own sign-in, `/connect` then Memory (`src/connect.js`,
13
+ * docs/design/plugin-sign-in.md): an access token OpenCode stores and
14
+ * renews, read for each request and kept until a minute before it expires;
15
+ * after a 401 OpenCode is asked once more, never the gateway (a refresh
16
+ * token is single use, and only OpenCode may spend it);
17
+ * 4. Jelly's plugin-secrets accessor,
13
18
  * `globalThis[Symbol.for("jelly.plugin-secrets")].get(PACKAGE_NAME, "token")`:
14
19
  * the key a person pasted into the plugin's settings.
15
20
  *
@@ -241,7 +246,7 @@ const MAX_TOKEN_CHARS = 8192
241
246
  const TOKEN_PATTERN = /^[!-~]+$/
242
247
 
243
248
  /** Credential sources, in the order they are tried. */
244
- export const SOURCES = Object.freeze({ option: "option", identity: "jelly-identity", secret: "plugin-secret" })
249
+ export const SOURCES = Object.freeze({ option: "option", identity: "jelly-identity", connect: "opencode-sign-in", secret: "plugin-secret" })
245
250
 
246
251
  /** What each `token()` rejection code from the identity accessor means. */
247
252
  const TOKEN_ERRORS = Object.freeze({
@@ -355,10 +360,13 @@ export function originOf(value) {
355
360
  * holder?: Record<symbol, unknown>,
356
361
  * now?: () => number,
357
362
  * log?: { info?: Function, warn?: Function },
363
+ * connect?: { connected: () => boolean, probe: () => Promise<boolean>, bearer: () => Promise<string>, refresh: (rejected: string) => Promise<string>, owns: (token: string) => boolean } | null,
358
364
  * }} [input]
359
365
  */
360
366
  export function createCredentials(input = {}) {
361
367
  const optionToken = typeof input.token === "string" ? input.token : ""
368
+ /** OpenCode's `/connect` sign-in (src/connect.js), or null. */
369
+ const connect = input.connect ?? null
362
370
  const holder = input.holder ?? globalThis
363
371
  const now = typeof input.now === "function" ? input.now : Date.now
364
372
  const log = input.log ?? null
@@ -571,6 +579,7 @@ export function createCredentials(input = {}) {
571
579
  }
572
580
  return SOURCES.identity
573
581
  }
582
+ if (connect?.connected()) return SOURCES.connect
574
583
  if (secretToken()) return SOURCES.secret
575
584
  return ""
576
585
  }
@@ -600,6 +609,8 @@ export function createCredentials(input = {}) {
600
609
  * is `unlinked()`. Without a Jelly sign-in in use this asks nothing.
601
610
  */
602
611
  async function check() {
612
+ // A /connect sign-in made after setup is found here, before a tool acts.
613
+ if (connect && !optionToken && !memoryResource()) await connect.probe()
603
614
  if (source() !== SOURCES.identity) return false
604
615
  try {
605
616
  await bearer()
@@ -609,6 +620,16 @@ export function createCredentials(input = {}) {
609
620
  return unlinked()
610
621
  }
611
622
 
623
+ /**
624
+ * Ask OpenCode whether a `/connect` sign-in exists now (at most every few
625
+ * seconds while there is none). True when one is in use.
626
+ */
627
+ async function probeSignIn() {
628
+ if (!connect || optionToken || memoryResource()) return false
629
+ await connect.probe()
630
+ return source() === SOURCES.connect
631
+ }
632
+
612
633
  /** Jelly's core plugin has published at least one accessor in this engine. */
613
634
  function inJelly() {
614
635
  return Boolean(identity() || secrets())
@@ -759,7 +780,10 @@ export function createCredentials(input = {}) {
759
780
  const strict = strictIdentity()
760
781
  if (strict && (optionToken || !memoryResource() || !sessionAsks)) throw new CredentialError("a caller-specific Jelly memory token is required", "not_enrolled")
761
782
  if (optionToken) return optionToken
762
- if (!memoryResource()) return secretToken()
783
+ if (!memoryResource()) {
784
+ const signedIn = connect ? await connect.bearer() : ""
785
+ return signedIn || secretToken()
786
+ }
763
787
  const asked = current()
764
788
  let refused = held(asked.state)
765
789
  if (!refused) {
@@ -793,7 +817,11 @@ export function createCredentials(input = {}) {
793
817
  * @returns {Promise<string>}
794
818
  */
795
819
  async function refresh(rejected) {
796
- if (optionToken || !memoryResource()) return ""
820
+ if (optionToken) return ""
821
+ if (!memoryResource()) {
822
+ // OpenCode is asked again for its sign-in; a pasted key is not retried.
823
+ return connect && rejected && connect.owns(rejected) ? connect.refresh(rejected) : ""
824
+ }
797
825
  if (!rejected || rejected === secretToken()) return ""
798
826
  const { state, session, view } = current()
799
827
  if (held(state)) return ""
@@ -814,7 +842,7 @@ export function createCredentials(input = {}) {
814
842
  return sessions.size
815
843
  }
816
844
 
817
- return { source, describe, unlinked, check, inJelly, origin, bearer, refresh, userTurn, forget, subject, sessionCount, memoryResource }
845
+ return { source, describe, unlinked, check, probeSignIn, inJelly, origin, bearer, refresh, userTurn, forget, subject, sessionCount, memoryResource }
818
846
  }
819
847
 
820
848
  /**
@@ -846,7 +874,7 @@ export function unavailableReason({ source, controlURL, inJelly }) {
846
874
  if (!source) {
847
875
  return inJelly
848
876
  ? "Memory is unavailable: this agent's Jelly sign-in can't be used for memory, and no memory key is saved in the plugin's settings."
849
- : "Memory is unavailable: no memory key is set; set the token option or MEMORY_TOKEN to the key from Memory's /account page."
877
+ : "Memory is unavailable: no memory key is set; run /connect and choose Memory, or set the token option or MEMORY_TOKEN to a key from Memory's /account page."
850
878
  }
851
879
  if (!controlURL) {
852
880
  return inJelly
package/src/curation.js CHANGED
@@ -262,16 +262,38 @@ export function partitionTurns(messages, options = {}) {
262
262
 
263
263
  /** Add two withheld tallies. */
264
264
  export function addWithheld(a, b) {
265
- return {
265
+ const sum = {
266
266
  status: (a?.status ?? 0) + (b?.status ?? 0),
267
267
  instruction: (a?.instruction ?? 0) + (b?.instruction ?? 0),
268
268
  task: (a?.task ?? 0) + (b?.task ?? 0),
269
269
  redacted: (a?.redacted ?? 0) + (b?.redacted ?? 0),
270
270
  }
271
+ // Rows that copy text the caller forgot (src/forgotten.js): their ids, so a
272
+ // row seen by two reads counts once. Only the count is ever shown.
273
+ const forgotten = [...new Set([...(a?.forgotten ?? []), ...(b?.forgotten ?? [])])]
274
+ if (forgotten.length > 0) sum.forgotten = forgotten
275
+ // Rows recall had no time to check against those fingerprints: withheld too.
276
+ const unchecked = [...new Set([...(a?.unchecked ?? []), ...(b?.unchecked ?? [])])].filter((id) => !forgotten.includes(id))
277
+ if (unchecked.length > 0) sum.unchecked = unchecked
278
+ return sum
271
279
  }
272
280
 
273
281
  export function withheldCount(withheld) {
274
- return (withheld?.status ?? 0) + (withheld?.instruction ?? 0) + (withheld?.task ?? 0)
282
+ return (withheld?.status ?? 0) + (withheld?.instruction ?? 0) + (withheld?.task ?? 0) + (withheld?.forgotten?.length ?? 0) + (withheld?.unchecked?.length ?? 0)
283
+ }
284
+
285
+ /**
286
+ * The line that says recall withheld copies of forgotten text: how many and
287
+ * why, nothing else. No ids, no content, and no way to bring them back: the
288
+ * person asks for that (docs/design/adr-forget-copies.md).
289
+ */
290
+ export function forgottenNote(rows, unchecked = []) {
291
+ const count = Array.isArray(rows) ? rows.length : 0
292
+ const late = Array.isArray(unchecked) ? unchecked.length : 0
293
+ const parts = []
294
+ if (count) parts.push(`(${count} stored row(s) withheld: they hold text you asked to forget. Do not answer from them or guess what they said.)`)
295
+ if (late) parts.push(`(${late} stored row(s) not shown: there was no time to check them against text you asked to forget.)`)
296
+ return parts.join(" ")
275
297
  }
276
298
 
277
299
  /** One line for a recall block, so a withheld record is not invisible. */
@@ -283,6 +305,7 @@ export function withheldNote(withheld) {
283
305
  const redacted = withheld?.redacted
284
306
  ? `${withheld.redacted} record(s) had an instruction removed and are shown with ${REDACTION_MARK}; the rest of each is data.`
285
307
  : ""
286
- if (parts.length === 0) return redacted ? `(${redacted})` : ""
287
- return `(${parts.join(", ")} record(s) withheld: things to do are commitments (mem_commitments), where work stands is a project page's Status section, and memory is a data channel.${redacted ? ` ${redacted}` : ""})`
308
+ const forgotten = forgottenNote(withheld?.forgotten, withheld?.unchecked)
309
+ if (parts.length === 0) return [redacted ? `(${redacted})` : "", forgotten].filter(Boolean).join(" ")
310
+ return [`(${parts.join(", ")} record(s) withheld: things to do are commitments (mem_commitments), where work stands is a project page's Status section, and memory is a data channel.${redacted ? ` ${redacted}` : ""})`, forgotten].filter(Boolean).join(" ")
288
311
  }