@joe-broadhead/opencode-mem 0.9.12 → 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.
- package/CHANGELOG.md +1934 -2
- package/README.md +29 -17
- package/SECURITY.md +3 -1
- package/docs/agent-instructions.md +17 -0
- package/docs/agent-protocol.md +8 -8
- package/docs/permissions.md +22 -4
- package/package.json +7923 -217
- package/shared/page-paths.js +44 -0
- package/skills/mem.pages/SKILL.md +8 -1
- package/skills/mem.usage/SKILL.md +15 -15
- package/src/action-grant-keys.js +159 -0
- package/src/action-grant.js +439 -0
- package/src/actions.js +1690 -0
- package/src/backend/records.js +25 -2
- package/src/backend/v3.js +76 -22
- package/src/call-shapes.js +17 -16
- package/src/collector-lock.js +533 -62
- package/src/collector-service.js +153 -26
- package/src/collector.js +627 -33
- package/src/config.js +79 -6
- package/src/connect.js +106 -8
- package/src/credentials.js +123 -25
- package/src/events.js +174 -3
- package/src/forget-turn.js +141 -0
- package/src/host-refusal.js +78 -0
- package/src/index.js +209 -22
- package/src/jobs/host.js +557 -0
- package/src/ops/codes.js +110 -0
- package/src/ops/copy-lineage.js +42 -0
- package/src/ops/index.js +28 -0
- package/src/ops/space-ops.js +1936 -0
- package/src/outlinks.js +103 -0
- package/src/p2-reservation.js +151 -0
- package/src/permissions.js +30 -0
- package/src/plugins/descriptor.js +62 -0
- package/src/plugins/discovery.js +318 -0
- package/src/plugins/grants.js +178 -0
- package/src/plugins/locate.js +319 -0
- package/src/plugins/manifest.js +1162 -0
- package/src/plugins/source-contract/index.js +34 -0
- package/src/plugins/source-contract/policy.js +102 -0
- package/src/plugins/source-contract/vendor/PROVENANCE.json +17 -0
- package/src/plugins/source-contract/vendor/RELEASE-SHA256SUMS +3 -0
- package/src/plugins/source-contract/vendor/RELEASE.json +17 -0
- package/src/plugins/source-contract/vendor/SHA256SUMS +41 -0
- package/src/plugins/source-contract/vendor/THIRD-PARTY-NOTICES.md +35 -0
- package/src/plugins/source-contract/vendor/broker-client.js +93 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-3M5PAKSS.js +84 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-4ERCA4TK.js +185 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-6HDIB2SA.js +183 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-ELPJD3ND.js +10 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-HLVAKAYG.js +10 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-I2RXJJJM.js +81 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-KWEWSZ4V.js +505 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-KYWBTAF4.js +182 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-Q3RQA5J2.js +249 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-RRUAIQ4I.js +156 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-RYNKNCPP.js +165 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-W5SHR7DQ.js +9477 -0
- package/src/plugins/source-contract/vendor/chunks/chunk-ZUKF7P6U.js +653 -0
- package/src/plugins/source-contract/vendor/cursor.js +26 -0
- package/src/plugins/source-contract/vendor/descriptor.js +34 -0
- package/src/plugins/source-contract/vendor/events.js +257 -0
- package/src/plugins/source-contract/vendor/index.js +10 -0
- package/src/plugins/source-contract/vendor/params.js +22 -0
- package/src/plugins/source-contract/vendor/permission.js +28 -0
- package/src/plugins/source-contract/vendor/pick.js +22 -0
- package/src/plugins/source-contract/vendor/read.js +29 -0
- package/src/plugins/source-contract/vendor/registry.js +23 -0
- package/src/plugins/source-contract/vendor/schema.js +17 -0
- package/src/plugins/source-contract/vendor/schemas/output.agenda.json +96 -0
- package/src/plugins/source-contract/vendor/schemas/output.countdown.json +28 -0
- package/src/plugins/source-contract/vendor/schemas/output.image.json +28 -0
- package/src/plugins/source-contract/vendor/schemas/output.inbox.json +84 -0
- package/src/plugins/source-contract/vendor/schemas/output.links.json +46 -0
- package/src/plugins/source-contract/vendor/schemas/output.list.json +57 -0
- package/src/plugins/source-contract/vendor/schemas/output.metric.json +61 -0
- package/src/plugins/source-contract/vendor/schemas/output.note.json +14 -0
- package/src/plugins/source-contract/vendor/schemas/output.progress.json +50 -0
- package/src/plugins/source-contract/vendor/schemas/output.record.json +38 -0
- package/src/plugins/source-contract/vendor/schemas/output.table.json +61 -0
- package/src/plugins/source-contract/vendor/schemas/output.thread.json +62 -0
- package/src/plugins/source-contract/vendor/schemas/sources.json +819 -0
- package/src/plugins/source-contract/vendor/schemas.js +19 -0
- package/src/plugins/source-contract/vendor/tools-rpc.js +42 -0
- package/src/plugins/verify.js +56 -0
- package/src/protocol.js +21 -5
- package/src/recall.js +104 -5
- package/src/records.js +61 -2
- package/src/requests.js +216 -0
- package/src/runtime.js +89 -7
- package/src/setup-time.js +68 -0
- package/src/skills.js +230 -8
- package/src/source-ids.js +25 -0
- package/src/sources/declaration.js +365 -0
- package/src/sources/reads.js +772 -0
- package/src/sources/rpc.js +151 -0
- package/src/sources.js +409 -90
- package/src/spaces.js +283 -11
- package/src/spool.js +61 -4
- package/src/stated.js +10 -5
- package/src/storable.js +139 -0
- package/src/tools/commitments.js +47 -5
- package/src/tools/held-write.js +36 -13
- package/src/tools/index.js +27 -0
- package/src/tools/ingest.js +27 -2
- package/src/tools/memory.js +52 -14
- package/src/tools/page.js +95 -9
- package/src/tools/recall.js +208 -4
- package/src/tools/records-kit.js +2 -1
- package/src/tools/share.js +42 -3
- package/src/tools/shared.js +44 -4
- package/src/tools/space-lifecycle.js +278 -0
- package/src/tools/spaces.js +373 -29
- package/src/tools/status.js +9 -2
- package/src/tools/wiki.js +34 -4
- package/src/util.js +110 -6
- package/src/vendor/source-contract/README.md +24 -0
- package/src/vendor/source-contract/THIRD-PARTY-NOTICES.md +35 -0
- package/src/vendor/source-contract/VENDOR.json +81 -0
- package/src/vendor/source-contract/chunks/chunk-3M5PAKSS.js +84 -0
- package/src/vendor/source-contract/chunks/chunk-6HDIB2SA.js +183 -0
- package/src/vendor/source-contract/chunks/chunk-HLVAKAYG.js +10 -0
- package/src/vendor/source-contract/chunks/chunk-I2RXJJJM.js +81 -0
- package/src/vendor/source-contract/chunks/chunk-KWEWSZ4V.js +505 -0
- package/src/vendor/source-contract/chunks/chunk-W5SHR7DQ.js +9477 -0
- package/src/vendor/source-contract/chunks/chunk-ZUKF7P6U.js +653 -0
- package/src/vendor/source-contract/cursor.js +26 -0
- package/src/vendor/source-contract/events.js +257 -0
- package/src/vendor/source-contract/registry.js +23 -0
- package/src/vendor/source-contract/tools-rpc.js +42 -0
- package/src/views/access-reads.js +256 -0
- package/src/views/communities.js +77 -0
- package/src/views/contracts.js +1183 -36
- package/src/views/inbox-bytes.js +103 -0
- package/src/views/overdue.js +1 -1
- package/src/views/profile.js +31 -0
- package/src/views/receipt.js +81 -0
- package/src/views/records-reads.js +869 -37
- package/src/views/rpc.js +159 -11
- package/src/views/shape.js +8 -5
- package/views/memory.html +16 -3
- package/views/pages.html +2 -2
- package/views/remembered.html +130 -0
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 {
|
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
43
|
-
|
|
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
|
-
|
|
66
|
-
|
|
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, {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
88
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
291
|
+
const sources = await registerSourceRpc(ctx, runtime)
|
|
292
|
+
if (sources) disposers.push(() => sources.dispose())
|
|
175
293
|
} catch (error) {
|
|
176
|
-
log.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
|
-
|
|
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
|
-
|
|
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()
|