@joe-broadhead/opencode-mem 0.9.14 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/CHANGELOG.md +1805 -0
  2. package/README.md +14 -7
  3. package/SECURITY.md +3 -1
  4. package/docs/agent-instructions.md +1 -1
  5. package/docs/agent-protocol.md +7 -7
  6. package/docs/permissions.md +16 -0
  7. package/package.json +7688 -339
  8. package/shared/page-paths.js +44 -0
  9. package/skills/mem.pages/SKILL.md +3 -1
  10. package/skills/mem.usage/SKILL.md +15 -15
  11. package/src/action-grant-keys.js +159 -0
  12. package/src/action-grant.js +439 -0
  13. package/src/actions.js +1690 -0
  14. package/src/backend/records.js +25 -2
  15. package/src/backend/v3.js +76 -22
  16. package/src/call-shapes.js +17 -16
  17. package/src/collector-lock.js +533 -62
  18. package/src/collector-service.js +153 -26
  19. package/src/collector.js +623 -30
  20. package/src/config.js +79 -6
  21. package/src/connect.js +106 -8
  22. package/src/credentials.js +123 -25
  23. package/src/events.js +174 -3
  24. package/src/forget-turn.js +141 -0
  25. package/src/host-refusal.js +78 -0
  26. package/src/index.js +209 -22
  27. package/src/jobs/host.js +557 -0
  28. package/src/ops/codes.js +110 -0
  29. package/src/ops/copy-lineage.js +42 -0
  30. package/src/ops/index.js +28 -0
  31. package/src/ops/space-ops.js +1936 -0
  32. package/src/outlinks.js +103 -0
  33. package/src/p2-reservation.js +151 -0
  34. package/src/permissions.js +30 -0
  35. package/src/plugins/descriptor.js +62 -0
  36. package/src/plugins/discovery.js +318 -0
  37. package/src/plugins/grants.js +178 -0
  38. package/src/plugins/locate.js +319 -0
  39. package/src/plugins/manifest.js +1162 -0
  40. package/src/plugins/source-contract/index.js +34 -0
  41. package/src/plugins/source-contract/policy.js +102 -0
  42. package/src/plugins/source-contract/vendor/PROVENANCE.json +17 -0
  43. package/src/plugins/source-contract/vendor/RELEASE-SHA256SUMS +3 -0
  44. package/src/plugins/source-contract/vendor/RELEASE.json +17 -0
  45. package/src/plugins/source-contract/vendor/SHA256SUMS +41 -0
  46. package/src/plugins/source-contract/vendor/THIRD-PARTY-NOTICES.md +35 -0
  47. package/src/plugins/source-contract/vendor/broker-client.js +93 -0
  48. package/src/plugins/source-contract/vendor/chunks/chunk-3M5PAKSS.js +84 -0
  49. package/src/plugins/source-contract/vendor/chunks/chunk-4ERCA4TK.js +185 -0
  50. package/src/plugins/source-contract/vendor/chunks/chunk-6HDIB2SA.js +183 -0
  51. package/src/plugins/source-contract/vendor/chunks/chunk-ELPJD3ND.js +10 -0
  52. package/src/plugins/source-contract/vendor/chunks/chunk-HLVAKAYG.js +10 -0
  53. package/src/plugins/source-contract/vendor/chunks/chunk-I2RXJJJM.js +81 -0
  54. package/src/plugins/source-contract/vendor/chunks/chunk-KWEWSZ4V.js +505 -0
  55. package/src/plugins/source-contract/vendor/chunks/chunk-KYWBTAF4.js +182 -0
  56. package/src/plugins/source-contract/vendor/chunks/chunk-Q3RQA5J2.js +249 -0
  57. package/src/plugins/source-contract/vendor/chunks/chunk-RRUAIQ4I.js +156 -0
  58. package/src/plugins/source-contract/vendor/chunks/chunk-RYNKNCPP.js +165 -0
  59. package/src/plugins/source-contract/vendor/chunks/chunk-W5SHR7DQ.js +9477 -0
  60. package/src/plugins/source-contract/vendor/chunks/chunk-ZUKF7P6U.js +653 -0
  61. package/src/plugins/source-contract/vendor/cursor.js +26 -0
  62. package/src/plugins/source-contract/vendor/descriptor.js +34 -0
  63. package/src/plugins/source-contract/vendor/events.js +257 -0
  64. package/src/plugins/source-contract/vendor/index.js +10 -0
  65. package/src/plugins/source-contract/vendor/params.js +22 -0
  66. package/src/plugins/source-contract/vendor/permission.js +28 -0
  67. package/src/plugins/source-contract/vendor/pick.js +22 -0
  68. package/src/plugins/source-contract/vendor/read.js +29 -0
  69. package/src/plugins/source-contract/vendor/registry.js +23 -0
  70. package/src/plugins/source-contract/vendor/schema.js +17 -0
  71. package/src/plugins/source-contract/vendor/schemas/output.agenda.json +96 -0
  72. package/src/plugins/source-contract/vendor/schemas/output.countdown.json +28 -0
  73. package/src/plugins/source-contract/vendor/schemas/output.image.json +28 -0
  74. package/src/plugins/source-contract/vendor/schemas/output.inbox.json +84 -0
  75. package/src/plugins/source-contract/vendor/schemas/output.links.json +46 -0
  76. package/src/plugins/source-contract/vendor/schemas/output.list.json +57 -0
  77. package/src/plugins/source-contract/vendor/schemas/output.metric.json +61 -0
  78. package/src/plugins/source-contract/vendor/schemas/output.note.json +14 -0
  79. package/src/plugins/source-contract/vendor/schemas/output.progress.json +50 -0
  80. package/src/plugins/source-contract/vendor/schemas/output.record.json +38 -0
  81. package/src/plugins/source-contract/vendor/schemas/output.table.json +61 -0
  82. package/src/plugins/source-contract/vendor/schemas/output.thread.json +62 -0
  83. package/src/plugins/source-contract/vendor/schemas/sources.json +819 -0
  84. package/src/plugins/source-contract/vendor/schemas.js +19 -0
  85. package/src/plugins/source-contract/vendor/tools-rpc.js +42 -0
  86. package/src/plugins/verify.js +56 -0
  87. package/src/protocol.js +11 -6
  88. package/src/recall.js +92 -5
  89. package/src/records.js +61 -2
  90. package/src/requests.js +216 -0
  91. package/src/runtime.js +89 -7
  92. package/src/setup-time.js +68 -0
  93. package/src/skills.js +230 -8
  94. package/src/source-ids.js +25 -0
  95. package/src/sources/declaration.js +365 -0
  96. package/src/sources/reads.js +772 -0
  97. package/src/sources/rpc.js +151 -0
  98. package/src/sources.js +389 -92
  99. package/src/spaces.js +283 -11
  100. package/src/spool.js +61 -4
  101. package/src/stated.js +10 -5
  102. package/src/storable.js +139 -0
  103. package/src/tools/commitments.js +37 -4
  104. package/src/tools/held-write.js +31 -12
  105. package/src/tools/index.js +27 -0
  106. package/src/tools/ingest.js +27 -2
  107. package/src/tools/memory.js +30 -7
  108. package/src/tools/page.js +95 -9
  109. package/src/tools/recall.js +208 -4
  110. package/src/tools/share.js +42 -3
  111. package/src/tools/shared.js +44 -4
  112. package/src/tools/space-lifecycle.js +278 -0
  113. package/src/tools/spaces.js +373 -29
  114. package/src/tools/status.js +9 -2
  115. package/src/tools/wiki.js +34 -4
  116. package/src/util.js +110 -6
  117. package/src/vendor/source-contract/README.md +24 -0
  118. package/src/vendor/source-contract/THIRD-PARTY-NOTICES.md +35 -0
  119. package/src/vendor/source-contract/VENDOR.json +81 -0
  120. package/src/vendor/source-contract/chunks/chunk-3M5PAKSS.js +84 -0
  121. package/src/vendor/source-contract/chunks/chunk-6HDIB2SA.js +183 -0
  122. package/src/vendor/source-contract/chunks/chunk-HLVAKAYG.js +10 -0
  123. package/src/vendor/source-contract/chunks/chunk-I2RXJJJM.js +81 -0
  124. package/src/vendor/source-contract/chunks/chunk-KWEWSZ4V.js +505 -0
  125. package/src/vendor/source-contract/chunks/chunk-W5SHR7DQ.js +9477 -0
  126. package/src/vendor/source-contract/chunks/chunk-ZUKF7P6U.js +653 -0
  127. package/src/vendor/source-contract/cursor.js +26 -0
  128. package/src/vendor/source-contract/events.js +257 -0
  129. package/src/vendor/source-contract/registry.js +23 -0
  130. package/src/vendor/source-contract/tools-rpc.js +42 -0
  131. package/src/views/access-reads.js +256 -0
  132. package/src/views/communities.js +77 -0
  133. package/src/views/contracts.js +930 -21
  134. package/src/views/inbox-bytes.js +103 -0
  135. package/src/views/profile.js +31 -0
  136. package/src/views/records-reads.js +355 -38
  137. package/src/views/rpc.js +118 -8
  138. package/src/views/shape.js +8 -5
  139. package/views/pages.html +2 -2
package/src/index.js CHANGED
@@ -1,17 +1,27 @@
1
1
  import { applySkipAsksSource, configWarnings, resolveConfig } from "./config.js"
2
- import { asSession, createCredentials, followOrigin } from "./credentials.js"
2
+ import { asSession, createCredentials, followOrigin, memoryResourceAvailable, memoryResourceOrigin, SOURCES } from "./credentials.js"
3
3
  import { createConnectSource, gatewayOrigin, registerConnect } from "./connect.js"
4
4
  import { createLogger } from "./log.js"
5
5
  import { createBackend } from "./backend/v3.js"
6
6
  import { createRuntime } from "./runtime.js"
7
7
  import { registerTools } from "./tools/index.js"
8
- import { syncSkills, registerBuiltinSkill } from "./skills.js"
8
+ import {
9
+ registerMirroredSkills,
10
+ registerBuiltinSkill,
11
+ syncSkills,
12
+ SKILL_REFRESH_BACKGROUND_MS,
13
+ SKILL_RETRY_DELAYS_MS,
14
+ SKILL_REFRESH_START_MS,
15
+ } from "./skills.js"
9
16
  import { registerCommands } from "./commands.js"
10
17
  import { attachMemoryHooks } from "./hooks.js"
11
18
  import { attachCapture } from "./events.js"
12
19
  import { registerViewRpc } from "./views/rpc.js"
20
+ import { registerSourceRpc } from "./sources/rpc.js"
13
21
  import { registerDefaultRules } from "./permissions.js"
14
- import { errorMessage, versionAtLeast } from "./util.js"
22
+ import { createSetupTimer } from "./setup-time.js"
23
+ import { hostRefusal, registerRefusal } from "./host-refusal.js"
24
+ import { errorMessage } from "./util.js"
15
25
 
16
26
  /**
17
27
  * OpenCode 2 plugin entrypoint.
@@ -29,18 +39,36 @@ export default {
29
39
  id: "mem",
30
40
 
31
41
  async setup(ctx) {
42
+ // One timer per engine start, on a monotonic clock: the phases are what
43
+ // `memory.setup` answers, and what the host's engine-start telemetry names
44
+ // (jelly #80, src/setup-time.js). Marks are cheap; nothing is read from it.
45
+ const setupTime = createSetupTimer()
32
46
  let config
33
47
  try {
34
- config = resolveConfig(ctx?.options ?? {})
48
+ // Jelly's sheet shows `ownService` as it will be applied, so the switch is
49
+ // resolved with whether this host is Jelly's at all: the accessors it
50
+ // publishes are the only signal there is.
51
+ config = resolveConfig({ ...(ctx?.options ?? {}), memoryResourceOffered: memoryResourceAvailable() })
35
52
  } catch (error) {
36
53
  console.error(`[mem] invalid configuration: ${errorMessage(error)}`)
37
54
  return
38
55
  }
56
+ setupTime.mark("config")
39
57
 
40
58
  const log = createLogger(config)
41
59
  const version = String(ctx?.app?.version ?? "")
42
- if (!versionAtLeast(version, 2, 1, 4)) {
43
- console.error(`[mem] OpenCode 2.1.4 or newer is required; this host reports ${version || "an unknown version"}`)
60
+ const refusal = hostRefusal(version)
61
+ if (refusal) {
62
+ // OpenCode discards a plugin's console output, so the reason has to be
63
+ // reachable without it: one tool that says it (the agent can tell the
64
+ // person, and anyone can call it), and Memory's own debug log. Nothing
65
+ // else is registered — no hooks, no protocol block, no request — and the
66
+ // console line stays for a host or a run that does show it.
67
+ console.error(`[mem] ${refusal.sentence}`)
68
+ log.error(refusal.sentence)
69
+ // Deliberately not caught: if the one tool cannot be registered, the
70
+ // failure belongs in the host's own log rather than in silence.
71
+ await registerRefusal(ctx, refusal.sentence)
44
72
  return
45
73
  }
46
74
  const missing = [
@@ -57,37 +85,91 @@ export default {
57
85
  console.error(`[mem] OpenCode 2.1.4 or newer is required; missing ${missing.join(", ")}`)
58
86
  return
59
87
  }
88
+ setupTime.mark("host")
60
89
 
61
90
  // Credentials and, inside Jelly, the gateway origin are read on use: the
62
91
  // accessors Jelly's core plugin publishes may appear after this setup.
63
92
  // With a gateway address, a person can also sign in with OpenCode's
64
93
  // /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 })
94
+ // Memory's origin, as it will be used: an address of one's own with the
95
+ // switch on, else Jelly's `memory` resource, read on every use. With either,
96
+ // Memory is registered in /connect, so the declared `account` sign-in row
97
+ // matches the engine integration even while the switch is off (jelly #72).
98
+ const memoryOrigin = () => gatewayOrigin(config.controlURL) || gatewayOrigin(memoryResourceOrigin())
99
+ /**
100
+ * Whether Memory's `/connect` sign-in can be used at all: with the switch on
101
+ * (an address of one's own, where the Jelly identity is never used), or on a
102
+ * host where Jelly's identity offers no `memory` resource. Where Jelly's
103
+ * identity signs the person in, a stored `/connect` credential is never
104
+ * read, so registering the integration would only offer a button that does
105
+ * nothing (R3-S2).
106
+ */
107
+ const signInUsable = config.ownService === true || !memoryResourceOrigin()
108
+ const signIn = memoryOrigin() && signInUsable ? createConnectSource({ integration: ctx?.integration, origin: memoryOrigin, log }) : null
109
+ const credentials = createCredentials({
110
+ token: config.token,
111
+ keyAllowed: config.keyAllowed !== false,
112
+ // R2-B1: the Jelly identity token is Jelly's own (no `cnf`), so with the
113
+ // switch on it is never used: only the key or `/connect` reaches the
114
+ // person's own address.
115
+ identityAllowed: !(config.memoryResourceOffered === true && config.ownService === true),
116
+ log,
117
+ connect: signIn?.supported ? signIn : null,
118
+ })
67
119
  // skipAsks: Jelly's setting inside Jelly, else the user-level config only.
68
- applySkipAsksSource(config, { inJelly: () => credentials.inJelly() })
69
- const jelly = !config.controlURL && credentials.inJelly()
70
- if (jelly) followOrigin(config, () => credentials.origin())
71
- const gateway = Boolean(config.controlURL) || jelly
120
+ applySkipAsksSource(config, { memoryResourceOffered: () => Boolean(credentials.memoryResource()) })
121
+ const memoryOffered = !config.controlURL && Boolean(credentials.memoryResource())
122
+ // R2-S1: with the switch on, the origin is the person's own address (or
123
+ // none yet), never the resource's: following it would answer with that
124
+ // address.
125
+ if (memoryOffered && config.ownService !== true) followOrigin(config, () => credentials.origin())
126
+ // The switch's "on with no address" is a configuration answer, not local
127
+ // direct mode: it needs the control client's one sentence.
128
+ const gateway = Boolean(config.controlURL) || memoryOffered || config.ownServiceNote === "incomplete"
129
+ setupTime.mark("credentials")
130
+
72
131
  const backend = createBackend(config, { log, credentials })
73
132
  const runtime = createRuntime({ config, log, backend, credentials, gateway })
74
133
  runtime.pruneClaims()
134
+ setupTime.mark("backend")
75
135
 
76
136
  // Register Memory in /connect, then learn whether a sign-in is stored.
77
137
  let connectRegistration = null
138
+ // A sign-in OpenCode holds on disk, read locally: the start's own answer.
139
+ let storedSignIn = false
140
+ // The background probe, when OpenCode's `/connect` can hold a sign-in.
141
+ let signInWarm = null
78
142
  if (signIn) {
79
143
  try {
80
- connectRegistration = await registerConnect(ctx, { origin: () => gatewayOrigin(config.controlURL), log })
144
+ connectRegistration = await registerConnect(ctx, { origin: memoryOrigin, log })
81
145
  } catch (error) {
82
146
  log.warn("could not add Memory to /connect; keys still work", errorMessage(error))
83
147
  }
84
- if (signIn.supported && !config.token) await signIn.probe()
148
+ // What OpenCode holds is local; confirming it is not. A stored sign-in due
149
+ // for renewal is renewed by OpenCode inside `resolve`, which is a request
150
+ // (opencode-mem#58), so setup starts the probe and awaits none of it:
151
+ // whatever needs the answer joins it at first use (`probeSignIn`,
152
+ // src/credentials.js), and the spool waits for it before replaying.
153
+ if (signIn.supported && !config.token) {
154
+ storedSignIn = await signIn.stored()
155
+ signInWarm = signIn.warm()
156
+ }
85
157
  }
158
+ setupTime.mark("connect")
86
159
 
87
- log.info("setup", { version, directory: ctx?.location?.directory, jelly, credential: credentials.source() || "none" })
88
- for (const warning of configWarnings(config, { credentialSource: credentials.source(), gateway })) log.warn(warning)
160
+ // The start's own line and warnings say what the disk holds, not what the
161
+ // probe will find: it has not answered yet, and the tools ask at first use.
162
+ // `source()`'s own order, with `/connect`'s stored sign-in where the probe
163
+ // would put it (after Jelly's identity, before a saved key).
164
+ const diskSource = credentials.source()
165
+ const credentialSource = storedSignIn && (diskSource === "" || diskSource === SOURCES.secret) ? SOURCES.connect : diskSource
166
+ log.info("setup", { version, directory: ctx?.location?.directory, memoryResourceOffered: memoryOffered, credential: credentialSource || "none" })
167
+ for (const warning of configWarnings(config, { credentialSource, gateway })) log.warn(warning)
89
168
  const unavailable = runtime.unavailable()
90
- if (unavailable) log.info("memory unavailable at setup; tools stay registered", { reason: unavailable })
169
+ // Not "unavailable" when OpenCode holds a sign-in it has not confirmed yet:
170
+ // the probe decides, and its answer is in the log either way.
171
+ if (unavailable && !storedSignIn) log.info("memory unavailable at setup; tools stay registered", { reason: unavailable })
172
+ setupTime.mark("status")
91
173
 
92
174
  const disposers = []
93
175
  if (typeof connectRegistration?.dispose === "function") disposers.push(() => connectRegistration.dispose())
@@ -96,6 +178,22 @@ export default {
96
178
  // and the last requested refresh always runs after the current one.
97
179
  let skillRefreshPromise = null
98
180
  let skillRefreshQueued = null
181
+ // Nothing this plugin started may run after it is disposed (nits).
182
+ let disposed = false
183
+ const skillTimers = new Set()
184
+ const scheduleSkillWork = (ms, work) => {
185
+ const timer = setTimeout(() => {
186
+ skillTimers.delete(timer)
187
+ if (!disposed) void work()
188
+ }, ms)
189
+ timer.unref?.()
190
+ skillTimers.add(timer)
191
+ return timer
192
+ }
193
+ const clearSkillWork = () => {
194
+ for (const timer of skillTimers) clearTimeout(timer)
195
+ skillTimers.clear()
196
+ }
99
197
 
100
198
  runtime.skills = skillState
101
199
  runtime.refreshSkills = (options = {}) => {
@@ -126,6 +224,18 @@ export default {
126
224
  reason: next.reason,
127
225
  spaces: skillState.sourceSpaces,
128
226
  })
227
+ // The result says it failed, so a caller with nothing cached can try
228
+ // again later (`refreshSkillsLater`, below).
229
+ return next
230
+ }
231
+ if (disposed) {
232
+ // The engine went away while this ran: its registration would outlive
233
+ // everything that could dispose it (jelly #80 review, nits).
234
+ try {
235
+ await next?.registration?.dispose?.()
236
+ } catch {
237
+ /* ignore */
238
+ }
129
239
  return skillState
130
240
  }
131
241
  skillState = next
@@ -133,7 +243,7 @@ export default {
133
243
  return skillState
134
244
  } catch (error) {
135
245
  log.warn("skill refresh failed; keeping the last good registry", error)
136
- return skillState
246
+ return { ok: false, reason: "error", error: errorMessage(error) }
137
247
  } finally {
138
248
  skillRefreshPromise = null
139
249
  const queued = skillRefreshQueued
@@ -143,6 +253,7 @@ export default {
143
253
  })
144
254
  return skillRefreshPromise
145
255
  }
256
+ setupTime.mark("wiring")
146
257
 
147
258
  // Every registration is disposed with the plugin, in reverse order, so a
148
259
  // reload does not leave a second set of tools, commands, or skills behind.
@@ -152,6 +263,7 @@ export default {
152
263
  } catch (error) {
153
264
  log.error("tool registration failed", error)
154
265
  }
266
+ setupTime.mark("tools")
155
267
 
156
268
  // A forget asks before it runs, on every agent with a base allow (D17).
157
269
  try {
@@ -160,6 +272,7 @@ export default {
160
272
  } catch (error) {
161
273
  log.error("default permission rules failed", error)
162
274
  }
275
+ setupTime.mark("rules")
163
276
 
164
277
  // Memory's views in Jelly call this RPC. It registers even while memory is
165
278
  // unavailable, so the views can say why and what to do.
@@ -169,15 +282,63 @@ export default {
169
282
  } catch (error) {
170
283
  log.error("view RPC registration failed", error)
171
284
  }
285
+ setupTime.mark("views")
172
286
 
287
+ // Memory as a source provider (D202, app#2346): the tools RPC that serves
288
+ // `package.json#sources`. Not an agent tool: another project reads it under
289
+ // a grant, and it adds no text to a model step.
173
290
  try {
174
- await runtime.refreshSkills()
291
+ const sources = await registerSourceRpc(ctx, runtime)
292
+ if (sources) disposers.push(() => sources.dispose())
175
293
  } catch (error) {
176
- log.error("skill sync failed", error)
294
+ log.error("source RPC registration failed", error)
177
295
  }
296
+ setupTime.mark("sources")
297
+
298
+ // The mirror on disk is registered first, from local files alone: the first
299
+ // session of this start has the skills the last successful refresh left,
300
+ // even with the server down, and setup never waits for the network for a
301
+ // skill (jelly #80, F1). A skill published since the last run arrives at the
302
+ // session bind, which refreshes the space it binds (`refreshSkillsForSpace`,
303
+ // src/runtime.js); a fresh host with no mirror registers nothing here and
304
+ // learns its skills exactly as it did.
305
+ try {
306
+ const cached = await registerMirroredSkills(ctx, runtime)
307
+ if (cached) {
308
+ skillState = cached
309
+ runtime.skills = skillState
310
+ }
311
+ } catch (error) {
312
+ log.warn("mirrored skills could not be registered at setup", error)
313
+ }
314
+ // Not awaited, and started after this tick: the refresh swaps the
315
+ // registration when it completes, and a slow or missing server costs this
316
+ // start nothing — not even the event-loop work of asking it.
317
+ //
318
+ // A host whose cache was usable has its skills already; one with nothing
319
+ // cached (a fresh host, a signed-out cache) needs this to succeed, so a
320
+ // failure is tried again with backoff until it does (jelly #80 review,
321
+ // R2-S1): at 600 ms a request a mirror needs 1.8 s, and a start waits for
322
+ // none of it. The whole thing is bounded by SKILL_RETRY_DELAYS_MS' last
323
+ // entry, so a dead server costs occasional requests and nothing else.
324
+ let skillAttempt = 0
325
+ const refreshSkillsLater = async () => {
326
+ if (disposed) return
327
+ const result = await runtime.refreshSkills({ reason: "setup", deadlineMs: SKILL_REFRESH_BACKGROUND_MS }).catch(() => ({ ok: false }))
328
+ if (disposed) return
329
+ if (result?.ok === false) {
330
+ const delay = SKILL_RETRY_DELAYS_MS[Math.min(skillAttempt, SKILL_RETRY_DELAYS_MS.length - 1)]
331
+ skillAttempt += 1
332
+ scheduleSkillWork(delay, refreshSkillsLater)
333
+ return
334
+ }
335
+ skillAttempt = 0
336
+ }
337
+ scheduleSkillWork(SKILL_REFRESH_START_MS, refreshSkillsLater)
178
338
  // The mirrored registration is replaced on every refresh; dispose the
179
339
  // current one, whichever it is at shutdown.
180
340
  disposers.push(() => skillState.registration?.dispose?.())
341
+ setupTime.mark("skills")
181
342
 
182
343
  try {
183
344
  const builtin = await registerBuiltinSkill(ctx, runtime)
@@ -185,6 +346,7 @@ export default {
185
346
  } catch (error) {
186
347
  log.error("builtin skill registration failed", error)
187
348
  }
349
+ setupTime.mark("builtinSkill")
188
350
 
189
351
  try {
190
352
  const commands = await registerCommands(ctx, runtime)
@@ -192,6 +354,7 @@ export default {
192
354
  } catch (error) {
193
355
  log.error("command registration failed", error)
194
356
  }
357
+ setupTime.mark("commands")
195
358
 
196
359
  try {
197
360
  const hooks = await attachMemoryHooks(ctx, runtime)
@@ -199,23 +362,47 @@ export default {
199
362
  } catch (error) {
200
363
  log.error("memory hook registration failed", error)
201
364
  }
365
+ setupTime.mark("hooks")
202
366
 
203
367
  try {
204
368
  disposers.push(attachCapture(ctx, runtime))
205
369
  } catch (error) {
206
370
  log.error("capture registration failed", error)
207
371
  }
372
+ setupTime.mark("capture")
208
373
 
209
- if (!runtime.unavailable()) {
374
+ // OpenCode holds a sign-in that is not confirmed yet: the spool and the
375
+ // backend probe wait for the probe's answer, and setup waits for neither
376
+ // (review B1). A stored credential the probe refuses — another address, or
377
+ // an unusable token — answers `bearer()` with "", and replaying the spool
378
+ // then would send captured turns under no identity at all.
379
+ const startOffSetupWork = () => {
380
+ if (disposed) return
210
381
  void runtime.flushSpool().catch(() => {})
211
382
  void backend.probe().then((result) => {
212
383
  if (!result.ok) log.warn("memory backend unreachable at setup; plugin stays loaded", result.error)
213
384
  })
214
385
  }
386
+ if (signInWarm) {
387
+ // `warm()` never rejects: its answer is whether the stored sign-in is this
388
+ // address's and usable. A renewal can take seconds, and none of it is the
389
+ // start's.
390
+ void signInWarm.then((confirmed) => {
391
+ if (confirmed || !runtime.unavailable()) startOffSetupWork()
392
+ })
393
+ } else if (!runtime.unavailable()) {
394
+ startOffSetupWork()
395
+ }
215
396
 
216
- log.info("ready", { skills: skillState.count, debug: config.debug ? config.debugFile : null })
397
+ setupTime.mark("flush")
398
+ // The start's own numbers, answered by the declared read `memory.setup`
399
+ // (jelly #80): frozen here, once, so reading it measures nothing.
400
+ runtime.setupTime = setupTime.finish()
401
+ log.info("ready", { skills: skillState.count, debug: config.debug ? config.debugFile : null, setupMs: runtime.setupTime.ms })
217
402
 
218
403
  return async () => {
404
+ disposed = true
405
+ clearSkillWork()
219
406
  for (const dispose of disposers.reverse()) {
220
407
  try {
221
408
  await dispose()