@reefclaw/openclaw-plugin 0.1.49 → 0.1.50
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/README.md +10 -6
- package/bridge/gateway/gateway-ws-client.d.ts +79 -1
- package/bridge/gateway/gateway-ws-client.js +99 -5
- package/bridge/providers/connector-update-direct.d.ts +3 -0
- package/bridge/providers/connector-update-direct.js +11 -1
- package/bridge/providers/connector-update-openclaw.d.ts +268 -0
- package/bridge/providers/connector-update-openclaw.js +772 -0
- package/bridge/providers/connector-update.d.ts +115 -1
- package/bridge/providers/connector-update.js +228 -13
- package/bridge/providers/gateway.d.ts +31 -10
- package/bridge/providers/gateway.js +139 -27
- package/config/first-start-venue.d.ts +147 -0
- package/config/first-start-venue.js +400 -0
- package/config/plugin-config-io.d.ts +9 -2
- package/config/plugin-config-io.js +29 -5
- package/connector-supervisor.d.ts +20 -0
- package/connector-supervisor.js +27 -1
- package/index.js +32 -1
- package/openclaw.plugin.json +333 -1
- package/package.json +1 -1
- package/release-integrity.json +5 -0
- package/release-notes.json +2 -1
- package/skills/reefclaw/SKILL.md +21 -8
- package/tools/venue-quote-hint.d.ts +49 -0
- package/tools/venue-quote-hint.js +200 -0
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
// Operator-triggered update of a ReefClaw plugin that OPENCLAW installed from
|
|
2
|
+
// ClawHub, through OpenClaw's own `plugins.install` RPC in update mode, over
|
|
3
|
+
// the same short-lived operator.admin gateway session the installer paths use.
|
|
4
|
+
//
|
|
5
|
+
// WHY THIS PATH EXISTS: the no-terminal onboarding route (2026-09-30) has the
|
|
6
|
+
// owner's agent install ReefClaw from ClawHub with OpenClaw's `plugins` tool.
|
|
7
|
+
// That copy lives under OpenClaw's own install roots, recorded as
|
|
8
|
+
// `clawhub:@reefclaw/openclaw-plugin@<v>`, and the connect installer refuses to
|
|
9
|
+
// touch it (installer/src/existing-install.ts, ALREADY_INSTALLED_MARKER). So
|
|
10
|
+
// without this path exactly the users who never opened a terminal lost the
|
|
11
|
+
// dashboard's Update button and were sent back to one.
|
|
12
|
+
//
|
|
13
|
+
// VERIFIED LIVE (canary box, fresh OpenClaw 2026.9.6, 2026-09-30) — every step
|
|
14
|
+
// below follows from these:
|
|
15
|
+
// - `plugins.install {source:'clawhub', packageName, version, mode:'update',
|
|
16
|
+
// expectedPluginId}` over a RUNNING copy of this plugin answered after
|
|
17
|
+
// 34 s with ok:false UNAVAILABLE "Plugin reefclaw-paper-trading still has
|
|
18
|
+
// active retained work; retry after the work finishes. Gateway generation
|
|
19
|
+
// 3: replacement not applied." — no retryable flag — YET the files on disk
|
|
20
|
+
// AND the install record were updated (`plugins.inspect` right after:
|
|
21
|
+
// plugin.version = the new release, source spec re-pinned). This plugin
|
|
22
|
+
// always holds retained work (intervals, sockets, the connector child), so
|
|
23
|
+
// that is the NORMAL outcome on every real box. The install's reply
|
|
24
|
+
// therefore decides nothing on its own: the re-inspect after it does.
|
|
25
|
+
// - The gateway kept running the OLD code until a restart — and a plain
|
|
26
|
+
// ok:true would too: the plugin keeps process-wide state across register()
|
|
27
|
+
// calls (plugin/src/index.ts singleton), so no hot reload ever loads new
|
|
28
|
+
// code. Every confirmed install therefore ends in `gateway.restart.request`
|
|
29
|
+
// with reason `update.run`, the one reason that replaces the process
|
|
30
|
+
// (connector-update.ts RESTART_REASON_AFTER_UPDATE), whatever
|
|
31
|
+
// `restartRequired` says.
|
|
32
|
+
// - `plugins.inspect` reports the version ON DISK (it said the new release
|
|
33
|
+
// while the old one still ran). "Disk == pinned" alone is therefore not
|
|
34
|
+
// "up to date": only a running release handed down by the plugin process
|
|
35
|
+
// itself can say that (getRunningPluginRelease).
|
|
36
|
+
// - The files can reach the disk before the install has finished (progress
|
|
37
|
+
// stages resolve → download → extract → files → dependencies → runtime),
|
|
38
|
+
// so "the pinned release is on disk" is corroborated by the install
|
|
39
|
+
// RECORD, which the live run re-pinned to the new release
|
|
40
|
+
// (`source.spec` clawhub:<pkg>@<v>) — never a restart onto an install
|
|
41
|
+
// that may still be running.
|
|
42
|
+
//
|
|
43
|
+
// From OpenClaw's source (2026.9.5–9.7): `mode` is accepted only from 2026.9.5
|
|
44
|
+
// — 9.4 and older have closed param schemas and answer INVALID_REQUEST — so
|
|
45
|
+
// the install is gated on hello-ok's `server.version` and `features.methods`.
|
|
46
|
+
// There is no `plugins.update` RPC, and the agent's own `plugins` tool sends no
|
|
47
|
+
// `mode` (it can never replace an installed copy), so a chat-driven reinstall
|
|
48
|
+
// is not an update path. A lost reply can follow a committed mutation (the
|
|
49
|
+
// install is not cancelled when its caller disconnects): never resend after a
|
|
50
|
+
// transport loss, always re-inspect. 2026.9.7 re-checks the caller's admin
|
|
51
|
+
// authority before persisting, so the admin session stays open for the whole
|
|
52
|
+
// call (the provider tears it down only after this module returns).
|
|
53
|
+
//
|
|
54
|
+
// ★★ SECURITY — the same rule as connector-update.ts ★★
|
|
55
|
+
// The request is a FIXED CONSTANT: source 'clawhub', package
|
|
56
|
+
// @reefclaw/openclaw-plugin, plugin id reefclaw-paper-trading, mode 'update'
|
|
57
|
+
// (compile-time constants), and ONE variable: the exact plugin release
|
|
58
|
+
// ReefClaw's public release endpoint vouches for (`pluginVersion` on
|
|
59
|
+
// /api/release, fetched by this process, validated MAJOR.MINOR.PATCH). Nothing
|
|
60
|
+
// from the browser, the relay, the agent or any config reaches it — the relay
|
|
61
|
+
// RPC still carries only `acknowledgeOpenPositions`. A missing or malformed
|
|
62
|
+
// version refuses; there is never an unpinned install, never a downgrade, and
|
|
63
|
+
// never an acknowledgement given on the owner's behalf: no `enable`, no
|
|
64
|
+
// `acknowledgeCapabilities`, no `acknowledgeInstallPolicyWarning`.
|
|
65
|
+
import { readFileSync } from 'node:fs';
|
|
66
|
+
import { dirname, join } from 'node:path';
|
|
67
|
+
import { logger } from '../logger.js';
|
|
68
|
+
import { OPENCLAW_PLUGIN_ID, OPENCLAW_PLUGIN_PACKAGE, RESTART_REASON_AFTER_UPDATE, describeUnconfirmedPluginVersion, isExactConnectVersion, openClawManualUpdateHint, openClawNpmManualUpdateHint, requestGatewayRestart, } from './connector-update.js';
|
|
69
|
+
import { appendTail, compareExactVersions } from './connector-update-direct.js';
|
|
70
|
+
const TAG = 'connector-update';
|
|
71
|
+
// ---- Fixed constants ------------------------------------------------------------
|
|
72
|
+
/** The only package this path installs. */
|
|
73
|
+
export const PACKAGE = OPENCLAW_PLUGIN_PACKAGE;
|
|
74
|
+
/** The only plugin id this path updates (sent as `expectedPluginId`). */
|
|
75
|
+
export const PLUGIN_ID = OPENCLAW_PLUGIN_ID;
|
|
76
|
+
/** The only install source this path uses or updates. */
|
|
77
|
+
export const SOURCE = 'clawhub';
|
|
78
|
+
/** First OpenClaw whose `plugins.install` takes `mode: 'update'`. */
|
|
79
|
+
export const MIN_OPENCLAW_FOR_PLUGIN_UPDATE = '2026.9.5';
|
|
80
|
+
/** OpenClaw's own CLI allows plugins.install this long. */
|
|
81
|
+
export const INSTALL_TIMEOUT_MS = 600_000;
|
|
82
|
+
/** Retries after an explicitly transient refusal — never after a transport loss. */
|
|
83
|
+
export const MAX_INSTALL_RETRIES = 3;
|
|
84
|
+
/** Cap on any single wait between retries, whatever retryAfterMs asks. */
|
|
85
|
+
export const RETRY_WAIT_CAP_MS = 10_000;
|
|
86
|
+
const DEFAULT_RETRY_WAIT_MS = 3_000;
|
|
87
|
+
// Structured error fields, named exactly as scripts/canary-plugins-install.mjs
|
|
88
|
+
// reads them (never decided on message text where a code exists).
|
|
89
|
+
export const CAPABILITY_CONSENT_CODE = 'PLUGIN_CAPABILITY_CONSENT_REQUIRED';
|
|
90
|
+
export const INSTALL_POLICY_CODE = 'install_policy_warning_acknowledgement_required';
|
|
91
|
+
/** ClawHub refuses the package for everyone; no acknowledgement exists. */
|
|
92
|
+
const DOWNLOAD_BLOCKED_CODES = new Set(['clawhub_download_blocked']);
|
|
93
|
+
/** ClawHub-side outages: nothing was staged, the same request can be resent. */
|
|
94
|
+
const TRANSIENT_INSTALL_CODES = new Set([
|
|
95
|
+
'clawhub_security_unavailable',
|
|
96
|
+
'artifact_unavailable',
|
|
97
|
+
'artifact_download_unavailable',
|
|
98
|
+
]);
|
|
99
|
+
/**
|
|
100
|
+
* The ONE request this path can ever send. The only variable is an exact
|
|
101
|
+
* release, validated here; anything else throws. Exactly these keys — never
|
|
102
|
+
* `enable` (9.7 only; 9.5/9.6 reject it), never an acknowledgement flag.
|
|
103
|
+
*/
|
|
104
|
+
export function buildOpenClawInstallRequest(version) {
|
|
105
|
+
if (!isExactConnectVersion(version)) {
|
|
106
|
+
throw new Error(`refusing to build a plugin install request for a non-exact version: ${JSON.stringify(version)}`);
|
|
107
|
+
}
|
|
108
|
+
return { source: SOURCE, packageName: PACKAGE, version, mode: 'update', expectedPluginId: PLUGIN_ID };
|
|
109
|
+
}
|
|
110
|
+
// ---- Which release is RUNNING ---------------------------------------------------------
|
|
111
|
+
/**
|
|
112
|
+
* Env var through which the supervising plugin hands the connector it spawns
|
|
113
|
+
* the release the GATEWAY PROCESS LOADED (processRuntime().release, stamped at
|
|
114
|
+
* register time) — the only first-hand answer to "is the pinned release
|
|
115
|
+
* running?". Non-secret. The supervisor's launch()
|
|
116
|
+
* (plugin/src/connector-supervisor.ts) is where it gets set; absent (a plugin
|
|
117
|
+
* that does not pass it yet, a connector run some other way) → the release is
|
|
118
|
+
* not confirmed, and a box whose disk already holds the pinned release is
|
|
119
|
+
* restarted rather than reported up to date — never a false "nothing to
|
|
120
|
+
* update" while the old code runs.
|
|
121
|
+
*/
|
|
122
|
+
export const RUNNING_RELEASE_ENV = 'REEFCLAW_PLUGIN_RUNNING_RELEASE';
|
|
123
|
+
function stampedRelease(v) {
|
|
124
|
+
const s = typeof v === 'string' ? v.trim() : '';
|
|
125
|
+
// 0.1.0 = the repo's unstamped manifest (a source / dist deploy), not a release.
|
|
126
|
+
return isExactConnectVersion(s) && s !== '0.1.0' ? s : null;
|
|
127
|
+
}
|
|
128
|
+
/** The release the supervising plugin says the gateway loaded, or null. */
|
|
129
|
+
export function resolveLoadedPluginRelease(env = process.env) {
|
|
130
|
+
return stampedRelease(env[RUNNING_RELEASE_ENV]);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The plugin release whose files this connector was STARTED from. The ClawHub
|
|
134
|
+
* package ships the connector at <pluginRoot>/bridge/index.js with the stamped
|
|
135
|
+
* release manifest one directory up (plugin-package/scripts/assemble.mjs), and
|
|
136
|
+
* the plugin's supervisor spawns it from there — so the manifest beside the
|
|
137
|
+
* bridge's parent names the files it started from. Any other layout (the
|
|
138
|
+
* connect installer's ~/.reefclaw/bridge, a source tree, the repo's unstamped
|
|
139
|
+
* 0.1.0) → null = unknown, never a guess.
|
|
140
|
+
*
|
|
141
|
+
* Evidence, not proof, of what the gateway runs: the supervisor respawns an
|
|
142
|
+
* exited connector from the SAME directory, so where OpenClaw replaced the
|
|
143
|
+
* files in place after an install whose restart never happened, a respawned
|
|
144
|
+
* connector reads the NEW manifest here while the gateway still runs the old
|
|
145
|
+
* plugin. It can therefore show that an OLDER release is running (R < X → a
|
|
146
|
+
* restart), or a newer one (R > X → never restarted onto an older copy), but
|
|
147
|
+
* never that the pinned one is — that takes resolveLoadedPluginRelease.
|
|
148
|
+
*/
|
|
149
|
+
export function resolveRunningPluginRelease(entryPath = process.argv[1], readText = (p) => readFileSync(p, 'utf-8')) {
|
|
150
|
+
if (!entryPath)
|
|
151
|
+
return null;
|
|
152
|
+
try {
|
|
153
|
+
const manifest = JSON.parse(readText(join(dirname(entryPath), '..', 'openclaw.plugin.json')));
|
|
154
|
+
if (manifest.id !== PLUGIN_ID)
|
|
155
|
+
return null;
|
|
156
|
+
return stampedRelease(manifest.version);
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// Read ONCE, at module load (= bridge boot: gateway.ts imports this module
|
|
163
|
+
// statically). Read at click time the manifest would be the disk version —
|
|
164
|
+
// exactly the "installed but not running" state this path must detect.
|
|
165
|
+
const RUNNING_RELEASE_AT_BOOT = (() => {
|
|
166
|
+
const loaded = resolveLoadedPluginRelease();
|
|
167
|
+
return loaded ? { version: loaded, loadedByGateway: true } : { version: resolveRunningPluginRelease(), loadedByGateway: false };
|
|
168
|
+
})();
|
|
169
|
+
export function getRunningPluginRelease() {
|
|
170
|
+
return RUNNING_RELEASE_AT_BOOT;
|
|
171
|
+
}
|
|
172
|
+
// ---- plugins.inspect ---------------------------------------------------------------
|
|
173
|
+
/** plugins.inspect could not be used (unknown method, scope, transport, …). */
|
|
174
|
+
export const INSPECT_UNAVAILABLE = 'unavailable';
|
|
175
|
+
function isRecord(v) {
|
|
176
|
+
return !!v && typeof v === 'object' && !Array.isArray(v);
|
|
177
|
+
}
|
|
178
|
+
function str(v) {
|
|
179
|
+
return typeof v === 'string' && v.trim().length > 0 ? v.trim() : null;
|
|
180
|
+
}
|
|
181
|
+
/** `clawhub:@reefclaw/openclaw-plugin@0.1.49` → `@reefclaw/openclaw-plugin`. */
|
|
182
|
+
export function packageFromSpec(spec) {
|
|
183
|
+
if (typeof spec !== 'string')
|
|
184
|
+
return null;
|
|
185
|
+
const body = spec.trim().replace(/^[a-z][a-z0-9+-]*:/i, '');
|
|
186
|
+
if (!body)
|
|
187
|
+
return null;
|
|
188
|
+
const at = body.lastIndexOf('@');
|
|
189
|
+
const name = at > 0 ? body.slice(0, at) : body;
|
|
190
|
+
return name.length > 0 ? name : null;
|
|
191
|
+
}
|
|
192
|
+
/** `clawhub:@reefclaw/openclaw-plugin@0.1.49` → `0.1.49`; an unpinned spec, a
|
|
193
|
+
* tag or a range → null. */
|
|
194
|
+
export function versionFromSpec(spec) {
|
|
195
|
+
if (typeof spec !== 'string')
|
|
196
|
+
return null;
|
|
197
|
+
const body = spec.trim().replace(/^[a-z][a-z0-9+-]*:/i, '');
|
|
198
|
+
const at = body.lastIndexOf('@');
|
|
199
|
+
if (at <= 0)
|
|
200
|
+
return null;
|
|
201
|
+
const v = body.slice(at + 1);
|
|
202
|
+
return isExactConnectVersion(v) ? v : null;
|
|
203
|
+
}
|
|
204
|
+
/** Defensive read of a plugins.inspect reply (L6 keys: ok, plugin{id,version,…},
|
|
205
|
+
* source{kind,spec,packageName,…}, …). Null when it is not a usable answer. */
|
|
206
|
+
export function readInspectResult(result) {
|
|
207
|
+
if (!isRecord(result) || result.ok === false)
|
|
208
|
+
return null;
|
|
209
|
+
const plugin = isRecord(result.plugin) ? result.plugin : {};
|
|
210
|
+
const source = isRecord(result.source) ? result.source : null;
|
|
211
|
+
const recordVersion = source
|
|
212
|
+
? (isExactConnectVersion(source.version) ? source.version : null) ?? versionFromSpec(source.spec)
|
|
213
|
+
: null;
|
|
214
|
+
return {
|
|
215
|
+
pluginId: str(plugin.id),
|
|
216
|
+
diskVersion: str(plugin.version),
|
|
217
|
+
sourceKind: source ? str(source.kind) : null,
|
|
218
|
+
packageName: source ? (str(source.packageName) ?? packageFromSpec(source.spec)) : null,
|
|
219
|
+
recordVersion,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Pure: may a restart be put onto what plugins.inspect shows now? Only when
|
|
224
|
+
* the pinned release X is on disk AND the install record agrees it is
|
|
225
|
+
* ReefClaw's ClawHub package pinned to X. Files reach the disk before an
|
|
226
|
+
* install's dependency / runtime stages are done, and a restart replaces the
|
|
227
|
+
* process — killing an install still in flight and booting a half-installed
|
|
228
|
+
* copy — so the record is the corroboration. A record that pins no exact
|
|
229
|
+
* release is accepted only when the install call itself answered ok (our
|
|
230
|
+
* operation demonstrably ended); after a refusal or a lost reply it is not.
|
|
231
|
+
*/
|
|
232
|
+
export function confirmInstalledAt(view, X, opts) {
|
|
233
|
+
if (!view || (view.pluginId !== null && view.pluginId !== PLUGIN_ID)) {
|
|
234
|
+
return { confirmed: false, why: 'OpenClaw did not report this plugin' };
|
|
235
|
+
}
|
|
236
|
+
if (view.diskVersion !== X) {
|
|
237
|
+
return { confirmed: false, why: `OpenClaw reports ${view.diskVersion ? oneLine(view.diskVersion, 40) : 'no version'} on disk` };
|
|
238
|
+
}
|
|
239
|
+
if (view.sourceKind !== SOURCE || view.packageName !== PACKAGE) {
|
|
240
|
+
return { confirmed: false, why: 'its install record no longer names ReefClaw\'s ClawHub package' };
|
|
241
|
+
}
|
|
242
|
+
if (view.recordVersion !== null) {
|
|
243
|
+
return view.recordVersion === X
|
|
244
|
+
? { confirmed: true }
|
|
245
|
+
: { confirmed: false, why: `its install record still names ${view.recordVersion}` };
|
|
246
|
+
}
|
|
247
|
+
return opts.installReplied
|
|
248
|
+
? { confirmed: true }
|
|
249
|
+
: { confirmed: false, why: 'its install record names no release' };
|
|
250
|
+
}
|
|
251
|
+
/** Ask OpenClaw how this plugin was installed. Any failure → INSPECT_UNAVAILABLE
|
|
252
|
+
* (the installer path, today's behaviour, decides). Never throws. */
|
|
253
|
+
export async function inspectInstalledPlugin(rpc) {
|
|
254
|
+
try {
|
|
255
|
+
return await rpc('plugins.inspect', { pluginId: PLUGIN_ID });
|
|
256
|
+
}
|
|
257
|
+
catch (err) {
|
|
258
|
+
logger.info(TAG, `plugins.inspect unavailable (${oneLine(errorMessage(err))}) — the installer path decides`);
|
|
259
|
+
return INSPECT_UNAVAILABLE;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Pure: which update path a box takes, from its plugins.inspect answer (or
|
|
264
|
+
* INSPECT_UNAVAILABLE).
|
|
265
|
+
* - 'openclaw' — a ClawHub install of exactly our package: this module.
|
|
266
|
+
* - 'blocked' — only on a POSITIVE finding that nothing here may update
|
|
267
|
+
* the copy: an npm install (it updates through OpenClaw and
|
|
268
|
+
* stays on npm — the owner gets the npm: line), or a
|
|
269
|
+
* ClawHub install of a package that is not ours.
|
|
270
|
+
* - 'installer' — everything else: no usable answer (older OpenClaw,
|
|
271
|
+
* unknown method, error), no install source, a `path`
|
|
272
|
+
* install (the connect installer's linked copy) and ANY
|
|
273
|
+
* other or unrecognised source kind. Today's PTY / direct
|
|
274
|
+
* path, unchanged. This is the safe direction: which kind
|
|
275
|
+
* OpenClaw reports for the installer's own `--link` copy is
|
|
276
|
+
* not pinned by a live capture, and a wrong guess must not
|
|
277
|
+
* strand that box or send its owner to lay a second ClawHub
|
|
278
|
+
* copy beside a load-path one (the duplicate-install / 4011
|
|
279
|
+
* case). The installer's own duplicate guard is the
|
|
280
|
+
* authority on "is this copy mine": it refuses a foreign
|
|
281
|
+
* copy with ALREADY_INSTALLED_MARKER, which ends `blocked`
|
|
282
|
+
* with no restart (describeInstalledElsewhere).
|
|
283
|
+
*/
|
|
284
|
+
export function planConnectorUpdate(inspect, opts = {}) {
|
|
285
|
+
if (inspect === INSPECT_UNAVAILABLE) {
|
|
286
|
+
return { path: 'installer', reason: 'plugins.inspect unavailable (older OpenClaw, or no answer)' };
|
|
287
|
+
}
|
|
288
|
+
const view = readInspectResult(inspect);
|
|
289
|
+
if (!view)
|
|
290
|
+
return { path: 'installer', reason: 'plugins.inspect gave no usable answer' };
|
|
291
|
+
if (view.pluginId !== null && view.pluginId !== PLUGIN_ID) {
|
|
292
|
+
return { path: 'installer', reason: `plugins.inspect answered for another plugin (${view.pluginId})` };
|
|
293
|
+
}
|
|
294
|
+
if (view.sourceKind === null)
|
|
295
|
+
return { path: 'installer', reason: 'no install source recorded' };
|
|
296
|
+
if (view.sourceKind === SOURCE && view.packageName === PACKAGE) {
|
|
297
|
+
return { path: 'openclaw', reason: `ClawHub install of ${PACKAGE}`, installed: view };
|
|
298
|
+
}
|
|
299
|
+
if (view.sourceKind === SOURCE) {
|
|
300
|
+
const pkg = view.packageName ?? 'an unnamed package';
|
|
301
|
+
return {
|
|
302
|
+
path: 'blocked',
|
|
303
|
+
reason: `ClawHub install of a foreign package (${pkg})`,
|
|
304
|
+
outcome: {
|
|
305
|
+
ok: false,
|
|
306
|
+
status: 'blocked',
|
|
307
|
+
message: `OpenClaw reports this box's ReefClaw plugin as installed from ClawHub package ${pkg}, which is not ReefClaw's `
|
|
308
|
+
+ `package (${PACKAGE}), so the dashboard will not update it — nothing changed. To replace it with ReefClaw's own, `
|
|
309
|
+
+ `${openClawManualUpdateHint(opts.pluginVersion)}.`,
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
if (view.sourceKind === 'npm') {
|
|
314
|
+
return {
|
|
315
|
+
path: 'blocked',
|
|
316
|
+
reason: 'npm install',
|
|
317
|
+
outcome: {
|
|
318
|
+
ok: false,
|
|
319
|
+
status: 'blocked',
|
|
320
|
+
message: 'ReefClaw on this box was installed through OpenClaw from npm, not from ClawHub, so the dashboard cannot update '
|
|
321
|
+
+ `it for you — nothing changed. It stays on npm: to update it, ${openClawNpmManualUpdateHint(opts.pluginVersion)}.`,
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
return {
|
|
326
|
+
path: 'installer',
|
|
327
|
+
reason: `${oneLine(view.sourceKind, 40)} install (not ClawHub / npm) — the installer's duplicate guard decides`,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
// ---- OpenClaw capability gate ------------------------------------------------------
|
|
331
|
+
/** OpenClaw's calendar version (YYYY.M.D, optionally followed by a suffix
|
|
332
|
+
* such as `-2` or `-beta.1`) → numbers; null when it is not one. The suffix
|
|
333
|
+
* is ignored: OpenClaw ships post-release builds as `2026.7.1-2`. */
|
|
334
|
+
export function parseOpenClawVersion(v) {
|
|
335
|
+
if (typeof v !== 'string')
|
|
336
|
+
return null;
|
|
337
|
+
const m = /^v?(\d{4})\.(\d{1,2})\.(\d{1,3})(?![\d.])/.exec(v.trim());
|
|
338
|
+
return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null;
|
|
339
|
+
}
|
|
340
|
+
function compareTriples(a, b) {
|
|
341
|
+
for (let i = 0; i < 3; i += 1) {
|
|
342
|
+
if (a[i] !== b[i])
|
|
343
|
+
return a[i] < b[i] ? -1 : 1;
|
|
344
|
+
}
|
|
345
|
+
return 0;
|
|
346
|
+
}
|
|
347
|
+
/** Can this gateway take `plugins.install` with `mode: 'update'`? From the
|
|
348
|
+
* admin session's hello-ok: `server.version` ≥ 2026.9.5 (unknown or
|
|
349
|
+
* unreadable → no), and — when the gateway lists its methods — both
|
|
350
|
+
* plugins.install and plugins.inspect among them. */
|
|
351
|
+
export function checkPluginUpdateCapability(hello) {
|
|
352
|
+
const reported = hello?.serverVersion ?? null;
|
|
353
|
+
const parsed = parseOpenClawVersion(reported);
|
|
354
|
+
const min = parseOpenClawVersion(MIN_OPENCLAW_FOR_PLUGIN_UPDATE);
|
|
355
|
+
if (!parsed) {
|
|
356
|
+
return {
|
|
357
|
+
ok: false,
|
|
358
|
+
reported,
|
|
359
|
+
why: reported ? `it reports a version ReefClaw cannot read (${reported})` : 'it did not report its version',
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
if (compareTriples(parsed, min) < 0)
|
|
363
|
+
return { ok: false, reported, why: `it reports ${reported}` };
|
|
364
|
+
const methods = hello?.methods;
|
|
365
|
+
if (Array.isArray(methods)) {
|
|
366
|
+
const missing = ['plugins.install', 'plugins.inspect'].filter((m) => !methods.includes(m));
|
|
367
|
+
if (missing.length > 0)
|
|
368
|
+
return { ok: false, reported, why: `it does not offer ${missing.join(' or ')}` };
|
|
369
|
+
}
|
|
370
|
+
return { ok: true };
|
|
371
|
+
}
|
|
372
|
+
function describeOpenClawTooOld(capability, version) {
|
|
373
|
+
const parsed = parseOpenClawVersion(capability.reported);
|
|
374
|
+
const min = parseOpenClawVersion(MIN_OPENCLAW_FOR_PLUGIN_UPDATE);
|
|
375
|
+
const lead = parsed && compareTriples(parsed, min) < 0
|
|
376
|
+
? `Your OpenClaw is older than ${MIN_OPENCLAW_FOR_PLUGIN_UPDATE} (${capability.why})`
|
|
377
|
+
: `Your OpenClaw is older than ${MIN_OPENCLAW_FOR_PLUGIN_UPDATE}, or does not show that it can update a plugin in place (${capability.why})`;
|
|
378
|
+
return {
|
|
379
|
+
ok: false,
|
|
380
|
+
status: 'blocked',
|
|
381
|
+
message: `${lead}, so ReefClaw did not start the update — nothing changed. Upgrade OpenClaw for one-click updates, `
|
|
382
|
+
+ `or ${openClawManualUpdateHint(version)}.`,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
/** The structured fields of a GatewayRpcError (gateway-ws-client.ts), read by
|
|
386
|
+
* shape so this module needs no runtime import of the client. Null = not a
|
|
387
|
+
* gateway answer at all: a TRANSPORT failure (timeout, closed, not connected). */
|
|
388
|
+
export function readGatewayError(err) {
|
|
389
|
+
if (!(err instanceof Error) || err.name !== 'GatewayRpcError')
|
|
390
|
+
return null;
|
|
391
|
+
const e = err;
|
|
392
|
+
return {
|
|
393
|
+
code: typeof e.code === 'string' ? e.code : null,
|
|
394
|
+
message: e.message,
|
|
395
|
+
details: isRecord(e.details) ? e.details : {},
|
|
396
|
+
retryable: e.retryable === true,
|
|
397
|
+
retryAfterMs: typeof e.retryAfterMs === 'number' && Number.isFinite(e.retryAfterMs) && e.retryAfterMs >= 0 ? e.retryAfterMs : null,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
function errorMessage(err) {
|
|
401
|
+
return err instanceof Error ? err.message : String(err);
|
|
402
|
+
}
|
|
403
|
+
/** Gateway-supplied text on one bounded line (it reaches the dashboard). */
|
|
404
|
+
function oneLine(text, max = 400) {
|
|
405
|
+
const flat = text.replace(/\s*\r?\n\s*/g, ' | ').trim();
|
|
406
|
+
return flat.length > max ? `${flat.slice(0, max - 1)}…` : flat;
|
|
407
|
+
}
|
|
408
|
+
/** The surface a capability-consent error says widened (`details.widened`),
|
|
409
|
+
* rendered short: `tools: a, b (+3 more)`. Shape-tolerant. */
|
|
410
|
+
export function describeWidenedSurface(widened) {
|
|
411
|
+
const list = (items) => {
|
|
412
|
+
const names = items.filter((i) => typeof i === 'string');
|
|
413
|
+
const head = names.slice(0, 5).join(', ');
|
|
414
|
+
return names.length > 5 ? `${head} (+${names.length - 5} more)` : head;
|
|
415
|
+
};
|
|
416
|
+
const parts = [];
|
|
417
|
+
if (typeof widened === 'string')
|
|
418
|
+
parts.push(widened);
|
|
419
|
+
else if (Array.isArray(widened))
|
|
420
|
+
parts.push(list(widened));
|
|
421
|
+
else if (isRecord(widened)) {
|
|
422
|
+
for (const [key, value] of Object.entries(widened)) {
|
|
423
|
+
if (Array.isArray(value) && value.length > 0)
|
|
424
|
+
parts.push(`${key}: ${list(value)}`);
|
|
425
|
+
else if (typeof value === 'string')
|
|
426
|
+
parts.push(`${key}: ${value}`);
|
|
427
|
+
else if (value === true)
|
|
428
|
+
parts.push(key);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
const text = parts.filter((p) => p.length > 0).join('; ') || 'new capabilities';
|
|
432
|
+
return oneLine(text, 300);
|
|
433
|
+
}
|
|
434
|
+
/** INVALID_REQUEST about `mode` / extra properties: a gateway older than
|
|
435
|
+
* 2026.9.5 slipped past the hello-ok gate (closed params schema). */
|
|
436
|
+
export function isModeRejection(error) {
|
|
437
|
+
return error.code === 'INVALID_REQUEST' && /\bmode\b|additional propert/i.test(error.message);
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Pure: what a failed install attempt means. Consulted twice by the run:
|
|
441
|
+
* - BEFORE the re-inspect, only for its `retry` verdict — a refusal before
|
|
442
|
+
* anything ran (the canary's "same request again" class). The disk says
|
|
443
|
+
* nothing about our request then, so it is resent and never read as ours.
|
|
444
|
+
* - AFTER a re-inspect that does NOT confirm the pinned release (confirmed
|
|
445
|
+
* on disk and in the record it is a success, whatever the reply said).
|
|
446
|
+
* Mirrors the canary's ladder (scripts/canary-plugins-install.mjs decide) with
|
|
447
|
+
* the operator's-box differences: no acknowledgement is ever given, and a
|
|
448
|
+
* transport loss is never resent.
|
|
449
|
+
*/
|
|
450
|
+
export function classifyInstallFailure(attempt, ctx) {
|
|
451
|
+
const X = ctx.pinned;
|
|
452
|
+
const D = ctx.diskVersion ? oneLine(ctx.diskVersion, 40) : 'no version';
|
|
453
|
+
const hint = openClawManualUpdateHint(X);
|
|
454
|
+
const final = (status, message) => ({
|
|
455
|
+
kind: 'final',
|
|
456
|
+
outcome: { ok: false, status, message },
|
|
457
|
+
});
|
|
458
|
+
if (attempt.ok) {
|
|
459
|
+
return final('failed', `OpenClaw reported ReefClaw ${X} as installed, but it still reports ${D} on disk, so the update is not confirmed and `
|
|
460
|
+
+ `OpenClaw was not restarted. Check the plugin version on the dashboard in a few minutes; if it did not change, ${hint}.`);
|
|
461
|
+
}
|
|
462
|
+
const gw = readGatewayError(attempt.error);
|
|
463
|
+
if (!gw) {
|
|
464
|
+
// Transport: we never heard back. The install is not cancelled when its
|
|
465
|
+
// caller goes away, so it may still be running — resending would collide.
|
|
466
|
+
return final('failed', `OpenClaw did not answer the install request (${oneLine(errorMessage(attempt.error))}) and still reports ReefClaw ${D} `
|
|
467
|
+
+ 'on disk. The install may still be running on the box, so it was not retried and OpenClaw was not restarted. '
|
|
468
|
+
+ 'Check the plugin version on the dashboard in a few minutes before trying again.');
|
|
469
|
+
}
|
|
470
|
+
const msg = oneLine(gw.message);
|
|
471
|
+
if (/unknown method/i.test(gw.message) || isModeRejection(gw)) {
|
|
472
|
+
return {
|
|
473
|
+
kind: 'final',
|
|
474
|
+
outcome: describeOpenClawTooOld({ reported: null, why: `it rejected the update request: ${msg}` }, X),
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
const d = gw.details;
|
|
478
|
+
if (gw.code === CAPABILITY_CONSENT_CODE || d.capabilityConsentCode === CAPABILITY_CONSENT_CODE) {
|
|
479
|
+
return final('blocked', `OpenClaw needs your approval before it installs ReefClaw ${X}: this release asks for capabilities the installed copy `
|
|
480
|
+
+ `did not have (${describeWidenedSurface(d.widened)}). The dashboard never approves capabilities for you, so nothing `
|
|
481
|
+
+ `was installed. To review and accept them, ${hint} (OpenClaw lists the new capabilities; run the line again with `
|
|
482
|
+
+ '--accept-capabilities added once you have read them).');
|
|
483
|
+
}
|
|
484
|
+
if (d.installPolicyCode === INSTALL_POLICY_CODE) {
|
|
485
|
+
return final('blocked', `ClawHub shows an install-policy warning for ReefClaw ${X} that only you can acknowledge — the dashboard never does it `
|
|
486
|
+
+ `for you, so nothing was installed. To review it, ${hint}.`);
|
|
487
|
+
}
|
|
488
|
+
const installCode = str(d.clawhubTrustCode) ?? str(d.pluginInstallCode);
|
|
489
|
+
if (installCode !== null && DOWNLOAD_BLOCKED_CODES.has(installCode)) {
|
|
490
|
+
return final('blocked', `ClawHub refused the download of ReefClaw ${X} (${installCode}), so nothing was installed and nothing on the box `
|
|
491
|
+
+ "changed. A retry from here cannot fix that; watch the dashboard's update notice for a newer release.");
|
|
492
|
+
}
|
|
493
|
+
if (isRecord(d.persistence) || isRecord(d.runtime) || isRecord(d.runtimeAttempt)) {
|
|
494
|
+
// Persisted or partly applied, yet the pinned release is not on disk: a
|
|
495
|
+
// resend is not safe (the canary's rule). The gateway's words, verbatim.
|
|
496
|
+
return final('failed', `OpenClaw could not finish installing ReefClaw ${X} (${msg}) and still reports ${D} on disk, so OpenClaw was not `
|
|
497
|
+
+ `restarted. Try again in a few minutes, or ${hint}.`);
|
|
498
|
+
}
|
|
499
|
+
if (gw.retryable
|
|
500
|
+
|| (installCode !== null && TRANSIENT_INSTALL_CODES.has(installCode))
|
|
501
|
+
|| /config changed|already running/i.test(gw.message)) {
|
|
502
|
+
const cap = ctx.retryWaitCapMs ?? RETRY_WAIT_CAP_MS;
|
|
503
|
+
return { kind: 'retry', waitMs: Math.min(cap, gw.retryAfterMs ?? DEFAULT_RETRY_WAIT_MS), why: msg };
|
|
504
|
+
}
|
|
505
|
+
return final('failed', `OpenClaw refused the update to ReefClaw ${X}: ${msg}. It still reports ${D} on disk; nothing was restarted.`);
|
|
506
|
+
}
|
|
507
|
+
/** Only short identifier-like progress fields reach the dashboard pane. */
|
|
508
|
+
function progressWord(v) {
|
|
509
|
+
return typeof v === 'string' && /^[a-z][a-z0-9_.-]{0,39}$/i.test(v) ? v : null;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Confirmed on disk AND in the install record → have the gateway restart
|
|
513
|
+
* ITSELF (`update.run`), then report what it answered — the same order as the
|
|
514
|
+
* installer paths (the `restarting` frame only once the gateway accepted; on
|
|
515
|
+
* 2026.9.6 its reply arrives before the process is replaced, L4). The caller
|
|
516
|
+
* has already answered the RPC with a `started` frame, so a verdict frame lost
|
|
517
|
+
* with the dying connector still reads as the restart on the dashboard (it
|
|
518
|
+
* latches "restart expected" from the connection drop while a run is in
|
|
519
|
+
* flight). A refused restart maps exactly like the installer paths:
|
|
520
|
+
* completed + restartRequired + the host-side advice.
|
|
521
|
+
*/
|
|
522
|
+
async function restartOntoInstalled(rpc, emit, lead, output) {
|
|
523
|
+
const kicked = await requestGatewayRestart(rpc, { reason: RESTART_REASON_AFTER_UPDATE });
|
|
524
|
+
const final = kicked.status === 'restarting'
|
|
525
|
+
? { ok: true, status: 'restarting', output, message: `${lead} ${kicked.message}` }
|
|
526
|
+
: {
|
|
527
|
+
ok: true,
|
|
528
|
+
status: 'completed',
|
|
529
|
+
restartRequired: true,
|
|
530
|
+
output,
|
|
531
|
+
message: `${lead} But the gateway was NOT restarted, so the previous version is still running. `
|
|
532
|
+
+ 'If OpenClaw runs in Docker, restart the container from the host machine (docker restart <container>); '
|
|
533
|
+
+ `otherwise restart OpenClaw. The new version loads then. (Self-restart refused: ${kicked.message})`,
|
|
534
|
+
};
|
|
535
|
+
emit(final);
|
|
536
|
+
return final;
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Update a ClawHub-installed copy of the plugin to the pinned release and
|
|
540
|
+
* restart the gateway onto it. `emit` receives the intermediate frames
|
|
541
|
+
* (`started` + progress) and the final one; refusals decided before anything
|
|
542
|
+
* ran are returned, not emitted (as in the direct path). The caller has
|
|
543
|
+
* applied the per-host opt-out, the position guard and the one-update-at-a-
|
|
544
|
+
* time lock, holds the operator.admin session `rpc` runs on, and tears it down
|
|
545
|
+
* after this returns. Never throws.
|
|
546
|
+
*
|
|
547
|
+
* With X = pinned release, D = on disk (plugins.inspect), R = running:
|
|
548
|
+
* D > X → blocked: never a downgrade.
|
|
549
|
+
* R > X → blocked: never restarted onto an older copy.
|
|
550
|
+
* D == X, R == X from the
|
|
551
|
+
* plugin process itself → completed: already up to date, no restart.
|
|
552
|
+
* D == X otherwise → restart only (recovers "installed, not
|
|
553
|
+
* restarted"), once the install record agrees.
|
|
554
|
+
* D < X → capability gate → install → (a refusal before
|
|
555
|
+
* anything ran is retried, never taken for
|
|
556
|
+
* success) → RE-INSPECT → restart iff disk AND
|
|
557
|
+
* record show X, whatever the install replied.
|
|
558
|
+
*/
|
|
559
|
+
export async function runOpenClawPluginUpdate(rpc, ctx, emit, deps = {}) {
|
|
560
|
+
const X = typeof ctx.pinnedPluginVersion === 'string' && isExactConnectVersion(ctx.pinnedPluginVersion)
|
|
561
|
+
? ctx.pinnedPluginVersion
|
|
562
|
+
: null;
|
|
563
|
+
const R = typeof ctx.runningRelease === 'string' && isExactConnectVersion(ctx.runningRelease) ? ctx.runningRelease : null;
|
|
564
|
+
const initial = readInspectResult(ctx.inspect);
|
|
565
|
+
let D = initial?.diskVersion ?? null;
|
|
566
|
+
const finish = (outcome) => {
|
|
567
|
+
logger.info(TAG, `connector.update path=openclaw D=${D ? oneLine(D, 40) : 'unknown'} X=${X ?? 'unknown'} `
|
|
568
|
+
+ `R=${R ?? 'unknown'}${R && ctx.runningReleaseLoadedByGateway === true ? '(loaded)' : ''} `
|
|
569
|
+
+ `outcome=${outcome.status}${outcome.restartRequired ? '+restartRequired' : ''}`);
|
|
570
|
+
return outcome;
|
|
571
|
+
};
|
|
572
|
+
try {
|
|
573
|
+
if (X === null)
|
|
574
|
+
return finish(describeUnconfirmedPluginVersion());
|
|
575
|
+
if (D === null || !isExactConnectVersion(D)) {
|
|
576
|
+
return finish({
|
|
577
|
+
ok: false,
|
|
578
|
+
status: 'blocked',
|
|
579
|
+
message: `OpenClaw did not report which ReefClaw version is installed (${D ? oneLine(D, 40) : 'no version'}), so the update was not `
|
|
580
|
+
+ `started — it only replaces a copy whose version it can compare. To update it, ${openClawManualUpdateHint(X)}.`,
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
const cmp = compareExactVersions(D, X);
|
|
584
|
+
if (cmp > 0) {
|
|
585
|
+
// Mirrors the SKILL.md OTA downgrade guard: a box ahead of what ReefClaw
|
|
586
|
+
// currently vouches for is left alone.
|
|
587
|
+
return finish({
|
|
588
|
+
ok: false,
|
|
589
|
+
status: 'blocked',
|
|
590
|
+
message: `This box has ReefClaw ${D} installed, newer than the ${X} ReefClaw currently vouches for. The dashboard never `
|
|
591
|
+
+ 'downgrades, so nothing was changed.',
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
if (R !== null && compareExactVersions(R, X) > 0) {
|
|
595
|
+
// The disk went back under a gateway running something newer than X: a
|
|
596
|
+
// restart (with or without an install) would load an older copy — a
|
|
597
|
+
// downgrade of the running code.
|
|
598
|
+
return finish({
|
|
599
|
+
ok: false,
|
|
600
|
+
status: 'blocked',
|
|
601
|
+
message: `ReefClaw ${R} is running on this box (as far as the connector can tell), newer than the ${X} ReefClaw currently `
|
|
602
|
+
+ 'vouches for. The dashboard never downgrades, so nothing was installed or restarted.',
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
if (cmp === 0) {
|
|
606
|
+
if (R === X && ctx.runningReleaseLoadedByGateway === true) {
|
|
607
|
+
return finish({ ok: true, status: 'completed', message: `ReefClaw ${X} is already installed and running on this box — nothing to update.` });
|
|
608
|
+
}
|
|
609
|
+
// The record must not contradict the disk before anything is restarted:
|
|
610
|
+
// X on disk with a record still pinning another release is an install
|
|
611
|
+
// that may be running right now (the owner's own shell line, say) — a
|
|
612
|
+
// restart would kill it. A record pinning no release cannot tell either
|
|
613
|
+
// way; with no install of ours in flight (the provider's lock) the
|
|
614
|
+
// restart goes ahead.
|
|
615
|
+
const recorded = initial?.recordVersion ?? null;
|
|
616
|
+
if (recorded !== null && recorded !== X) {
|
|
617
|
+
return finish({
|
|
618
|
+
ok: false,
|
|
619
|
+
status: 'failed',
|
|
620
|
+
message: `OpenClaw reports ReefClaw ${X} on disk, but its install record still names ${recorded}, so an install may still `
|
|
621
|
+
+ 'be in progress — nothing was restarted. Try again in a few minutes; if this persists, '
|
|
622
|
+
+ `${openClawManualUpdateHint(X)}.`,
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
const lead = `ReefClaw ${X} is already installed on this box, but `
|
|
626
|
+
+ (R === X
|
|
627
|
+
? 'the connector cannot confirm that the gateway has loaded it.'
|
|
628
|
+
: R
|
|
629
|
+
? `this gateway is still running ${R}.`
|
|
630
|
+
: 'the running version could not be confirmed.');
|
|
631
|
+
const output = `${lead}\n`;
|
|
632
|
+
// This frame is the RPC's reply (the dashboard shows the run as in
|
|
633
|
+
// flight from here); the verdict follows once the gateway has answered.
|
|
634
|
+
emit({
|
|
635
|
+
ok: true,
|
|
636
|
+
status: 'started',
|
|
637
|
+
output,
|
|
638
|
+
message: 'Restarting OpenClaw to load the installed version — the dashboard reconnects on its own.',
|
|
639
|
+
});
|
|
640
|
+
return finish(await restartOntoInstalled(rpc, emit, lead, output));
|
|
641
|
+
}
|
|
642
|
+
const capability = checkPluginUpdateCapability(ctx.hello);
|
|
643
|
+
if (!capability.ok)
|
|
644
|
+
return finish(describeOpenClawTooOld(capability, X));
|
|
645
|
+
const request = buildOpenClawInstallRequest(X);
|
|
646
|
+
const sleep = deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
647
|
+
const maxRetries = deps.maxRetries ?? MAX_INSTALL_RETRIES;
|
|
648
|
+
const timeoutMs = deps.installTimeoutMs ?? INSTALL_TIMEOUT_MS;
|
|
649
|
+
let output = '';
|
|
650
|
+
const note = (line) => {
|
|
651
|
+
output = appendTail(output, `${line}\n`);
|
|
652
|
+
};
|
|
653
|
+
note(`Updating ReefClaw ${D} → ${X} from ClawHub through OpenClaw…`);
|
|
654
|
+
emit({
|
|
655
|
+
ok: true,
|
|
656
|
+
status: 'started',
|
|
657
|
+
output,
|
|
658
|
+
message: 'Update started. OpenClaw restarts when it finishes, so the dashboard will reconnect on its own.',
|
|
659
|
+
});
|
|
660
|
+
const onEvent = (event, payload) => {
|
|
661
|
+
if (event !== 'plugins.install.progress' || !isRecord(payload))
|
|
662
|
+
return;
|
|
663
|
+
const stage = progressWord(payload.stage);
|
|
664
|
+
const status = progressWord(payload.status);
|
|
665
|
+
if (!stage || !status)
|
|
666
|
+
return;
|
|
667
|
+
note(` ${stage}: ${status}`);
|
|
668
|
+
emit({ ok: true, status: 'started', output, message: 'Updating…' });
|
|
669
|
+
};
|
|
670
|
+
const fail = (outcome) => {
|
|
671
|
+
const withOutput = { ...outcome, output };
|
|
672
|
+
emit(withOutput);
|
|
673
|
+
return finish(withOutput);
|
|
674
|
+
};
|
|
675
|
+
for (let attempt = 0;; attempt += 1) {
|
|
676
|
+
const startedAt = Date.now();
|
|
677
|
+
let result;
|
|
678
|
+
try {
|
|
679
|
+
// Keys pinned by test; the admin session stays open for the whole call.
|
|
680
|
+
result = { ok: true, payload: await rpc('plugins.install', request, { timeoutMs, onEvent }) };
|
|
681
|
+
}
|
|
682
|
+
catch (error) {
|
|
683
|
+
result = { ok: false, error };
|
|
684
|
+
}
|
|
685
|
+
const secs = Math.round((Date.now() - startedAt) / 1000);
|
|
686
|
+
const gw = result.ok ? null : readGatewayError(result.error);
|
|
687
|
+
const replied = result.ok
|
|
688
|
+
? 'ok'
|
|
689
|
+
: gw
|
|
690
|
+
? `${gw.code ?? 'error'}: ${oneLine(gw.message)}${Object.keys(gw.details).length > 0 ? ` details=${oneLine(JSON.stringify(gw.details), 600)}` : ''}`
|
|
691
|
+
: `no answer (${oneLine(errorMessage(result.error))})`;
|
|
692
|
+
logger.info(TAG, `plugins.install ${X} (attempt ${attempt + 1}) after ${secs}s → ${replied}`);
|
|
693
|
+
// A refusal BEFORE anything ran (another plugin / config operation holds
|
|
694
|
+
// the gateway's lock, a rate limit, a ClawHub outage — the canary's
|
|
695
|
+
// "same request again" class) says nothing about what is on disk: X
|
|
696
|
+
// there would be SOMEONE ELSE's install, possibly still in flight, and a
|
|
697
|
+
// restart would kill it. Wait and resend; never read the disk as ours.
|
|
698
|
+
const early = result.ok ? null : classifyInstallFailure(result, { diskVersion: D, pinned: X, retryWaitCapMs: deps.retryWaitCapMs });
|
|
699
|
+
if (early?.kind === 'retry') {
|
|
700
|
+
if (attempt < maxRetries) {
|
|
701
|
+
note(`OpenClaw is busy (${early.why}); trying again in ${Math.max(1, Math.round(early.waitMs / 1000))} s…`);
|
|
702
|
+
emit({ ok: true, status: 'started', output, message: 'Updating…' });
|
|
703
|
+
await sleep(early.waitMs);
|
|
704
|
+
continue;
|
|
705
|
+
}
|
|
706
|
+
return fail({
|
|
707
|
+
ok: false,
|
|
708
|
+
status: 'failed',
|
|
709
|
+
message: `OpenClaw could not install ReefClaw ${X} right now (${early.why}). Nothing was restarted — try again shortly.`,
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
// ★ THE DECISIVE STEP: what is on disk now, whatever the install said.
|
|
713
|
+
let after;
|
|
714
|
+
try {
|
|
715
|
+
after = await rpc('plugins.inspect', { pluginId: PLUGIN_ID });
|
|
716
|
+
}
|
|
717
|
+
catch (err) {
|
|
718
|
+
note(`Could not read the installed version back (${oneLine(errorMessage(err))}).`);
|
|
719
|
+
return fail({
|
|
720
|
+
ok: false,
|
|
721
|
+
status: 'failed',
|
|
722
|
+
message: `ReefClaw could not confirm whether the update was installed (plugins.inspect: ${oneLine(errorMessage(err))}), `
|
|
723
|
+
+ 'so it did not restart OpenClaw. Check the plugin version on the dashboard in a few minutes before retrying.',
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
const view = readInspectResult(after);
|
|
727
|
+
const ours = view && (view.pluginId === null || view.pluginId === PLUGIN_ID) ? view : null;
|
|
728
|
+
const disk = ours ? ours.diskVersion : null;
|
|
729
|
+
D = disk;
|
|
730
|
+
if (disk === X) {
|
|
731
|
+
const confirmation = confirmInstalledAt(ours, X, { installReplied: result.ok });
|
|
732
|
+
if (confirmation.confirmed) {
|
|
733
|
+
note(result.ok
|
|
734
|
+
? `OpenClaw installed ${X}.`
|
|
735
|
+
: `OpenClaw answered "${gw ? oneLine(gw.message, 200) : 'no answer'}", and reports ${X} on disk and in its install record — the files are in place (the usual answer while ReefClaw is running).`);
|
|
736
|
+
logger.info(TAG, `plugins.inspect after the install: ${X} on disk and recorded — restarting the gateway to load it (install reply: ${replied})`);
|
|
737
|
+
return finish(await restartOntoInstalled(rpc, emit, `ReefClaw ${X} is installed.`, output));
|
|
738
|
+
}
|
|
739
|
+
// X on disk, but the record does not (yet) say the install finished:
|
|
740
|
+
// it may still be running (a lost reply is not a cancelled install).
|
|
741
|
+
// Neither a restart nor a resend is safe.
|
|
742
|
+
note(`OpenClaw reports ${X} on disk, but ${confirmation.why}.`);
|
|
743
|
+
logger.info(TAG, `plugins.inspect after the install: ${X} on disk but not confirmed (${confirmation.why}) — no restart`);
|
|
744
|
+
return fail({
|
|
745
|
+
ok: false,
|
|
746
|
+
status: 'failed',
|
|
747
|
+
message: `OpenClaw reports ReefClaw ${X} on disk, but ${confirmation.why}, so the install may still be running — OpenClaw `
|
|
748
|
+
+ `was not restarted and the install was not retried. Check the plugin version on the dashboard in a few minutes; `
|
|
749
|
+
+ 'running the update again then restarts OpenClaw onto it once the install has finished.',
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
const verdict = classifyInstallFailure(result, { diskVersion: disk, pinned: X, retryWaitCapMs: deps.retryWaitCapMs });
|
|
753
|
+
if (verdict.kind === 'retry') {
|
|
754
|
+
// Unreachable (retry verdicts are handled before the re-inspect); kept
|
|
755
|
+
// total so a future verdict can never fall through to a restart.
|
|
756
|
+
return fail({
|
|
757
|
+
ok: false,
|
|
758
|
+
status: 'failed',
|
|
759
|
+
message: `OpenClaw could not install ReefClaw ${X} right now (${verdict.why}). Nothing was restarted — try again shortly.`,
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
return fail(verdict.outcome);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
catch (err) {
|
|
766
|
+
return finish({
|
|
767
|
+
ok: false,
|
|
768
|
+
status: 'failed',
|
|
769
|
+
message: `The update stopped unexpectedly (${oneLine(errorMessage(err))}). Check the plugin version on the dashboard before retrying.`,
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
}
|