@reefclaw/openclaw-plugin 0.1.47 → 0.1.49
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/bridge/bridge.d.ts +39 -0
- package/bridge/bridge.js +229 -20
- package/bridge/gateway/heartbeat-cron.d.ts +64 -0
- package/bridge/gateway/heartbeat-cron.js +153 -3
- package/bridge/heartbeat-runs.d.ts +6 -0
- package/bridge/heartbeat-runs.js +43 -1
- package/bridge/index.js +3 -0
- package/bridge/provider.d.ts +12 -0
- package/bridge/providers/connector-update.js +20 -2
- package/bridge/providers/gateway.d.ts +21 -0
- package/bridge/providers/gateway.js +121 -9
- package/bridge/shock-wake.js +12 -2
- package/bridge/types.d.ts +17 -0
- package/bridge/utils/skill-version.d.ts +6 -0
- package/bridge/utils/skill-version.js +10 -0
- package/bridge/utils/webapp-edge.d.ts +145 -0
- package/bridge/utils/webapp-edge.js +366 -0
- package/config/agent-config-client.js +18 -2
- package/config/agent-config-poller.d.ts +4 -0
- package/config/agent-config-poller.js +46 -6
- package/http/webapp-edge.d.ts +116 -0
- package/http/webapp-edge.js +313 -0
- package/index.js +7 -0
- package/ingest/position-decisions-client.js +28 -7
- package/ingest/readiness-reporter.d.ts +13 -0
- package/ingest/readiness-reporter.js +78 -8
- package/ingest/trade-store-client.js +25 -4
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/release-integrity.json +10 -0
- package/release-notes.json +3 -1
- package/signals/stop-rules.d.ts +13 -0
- package/signals/stop-rules.js +35 -6
- package/signals/strategy-adapter.js +25 -6
- package/skills/reefclaw/SKILL.md +1 -1
- package/tools/assessment-validation.d.ts +10 -2
- package/tools/assessment-validation.js +19 -5
- package/tools/create-order.d.ts +4 -0
- package/tools/create-order.js +15 -0
- package/tools/get-market-intel.d.ts +3 -0
- package/tools/get-market-intel.js +24 -1
- package/tools/intel-cache.d.ts +14 -0
- package/tools/intel-cache.js +59 -0
package/bridge/bridge.d.ts
CHANGED
|
@@ -55,6 +55,9 @@ export declare class Bridge {
|
|
|
55
55
|
* timer) double-applying / double-notifying during the one-time bootstrap→
|
|
56
56
|
* full transition. The chain that starts (attempt 1) owns the flag. */
|
|
57
57
|
private skillPullInFlight;
|
|
58
|
+
/** Drains webapp edge-deny transitions into one-shot agent_state events. */
|
|
59
|
+
private webappEdgeTimer;
|
|
60
|
+
private edgeProbeInFlight;
|
|
58
61
|
constructor(provider: OpenClawProvider, connectorConfig: ConnectorConfig,
|
|
59
62
|
/** Where the served agent's SKILL.md and session index live. The legacy
|
|
60
63
|
* single-agent paths by default; index.ts passes the resolved agent's
|
|
@@ -142,6 +145,26 @@ export declare class Bridge {
|
|
|
142
145
|
private handleClearExchangeCredentials;
|
|
143
146
|
/** Apply a SKILL.md update: verify signature (C1), write to workspace +
|
|
144
147
|
* installed skill dir, clear session cache, notify agent. */
|
|
148
|
+
/**
|
|
149
|
+
* HEARTBEAT.md — the heartbeat checklist the agent reads at the start of
|
|
150
|
+
* every beat. Until 2026-09-27 the file never reached npx boxes, so every
|
|
151
|
+
* beat opened with a failed `read` and ran on the abbreviated inline list.
|
|
152
|
+
* It now rides the SKILL.md pull, signed offline like SKILL.md (same C1
|
|
153
|
+
* gate: enforced → a valid signature is required; a PRESENT signature is
|
|
154
|
+
* always verified), and is written beside every agent-visible reefclaw
|
|
155
|
+
* SKILL.md copy — NEVER at the workspace root, where OpenClaw's own
|
|
156
|
+
* heartbeat monitor (`openclaw doctor`) would adopt it and double-run beats.
|
|
157
|
+
*/
|
|
158
|
+
private applyHeartbeatChecklist;
|
|
159
|
+
/** Every `<skills dir>/<*reefclaw*>/` that currently holds a SKILL.md, across
|
|
160
|
+
* the layouts applySkillUpdate maintains (~/.openclaw/skills, the workspace
|
|
161
|
+
* skills dir, plugin-extension bundled skills). Fail-soft. */
|
|
162
|
+
private reefclawSkillDirs;
|
|
163
|
+
/** Write `HEARTBEAT.md` into each dir whose copy differs; returns the count
|
|
164
|
+
* written. Pure with respect to the bridge (exported for tests via the
|
|
165
|
+
* instance); never creates a directory, so it can never plant a file where
|
|
166
|
+
* no reefclaw skill lives. */
|
|
167
|
+
private writeHeartbeatChecklistFiles;
|
|
145
168
|
private applySkillUpdate;
|
|
146
169
|
/**
|
|
147
170
|
* Fetch the stored SKILL.md envelope from the relay's Durable-Object store
|
|
@@ -165,6 +188,22 @@ export declare class Bridge {
|
|
|
165
188
|
* gate when enforced) as the relay pull path.
|
|
166
189
|
*/
|
|
167
190
|
private pullSkillContentFromWebapp;
|
|
191
|
+
/** Every minute: surface an edge-deny episode (the bridge's own webapp calls
|
|
192
|
+
* ∪ the plugin's gate file) as one-shot agent_state events. The relay audits
|
|
193
|
+
* them as skill.webapp_edge_denied / skill.webapp_edge_recovered — what the
|
|
194
|
+
* fleet alarm keys on — while buildAgentState carries the live verdict
|
|
195
|
+
* (health.webappApi) to the dashboard banner. Relay-borne on purpose: every
|
|
196
|
+
* HTTP path to the webapp is the thing being denied. */
|
|
197
|
+
private startWebappEdgeEmitter;
|
|
198
|
+
/** Re-send agent_state from a fresh snapshot (carries health.webappApi). */
|
|
199
|
+
private reemitAgentState;
|
|
200
|
+
/** While the bridge's own episode is open and its gate has reopened, send
|
|
201
|
+
* ONE authenticated probe (GET /api/internal/config — the same class of
|
|
202
|
+
* call the plugin polls) and feed the verdict to the tracker: a success
|
|
203
|
+
* ends the episode, a deny re-closes the gate at the next rung. Exported
|
|
204
|
+
* on the instance for tests. */
|
|
205
|
+
private maybeProbeEdge;
|
|
206
|
+
private stopWebappEdgeEmitter;
|
|
168
207
|
/** Start a bounded periodic re-pull of the full SKILL.md while the box is
|
|
169
208
|
* still on the bootstrap (0.x). pullSkillContentFromWebapp() (fired once at
|
|
170
209
|
* startup) is a one-shot with a finite retry burst; if every attempt fails —
|
package/bridge/bridge.js
CHANGED
|
@@ -8,13 +8,14 @@
|
|
|
8
8
|
// out of this file: it owns the relay connection, and a local read sitting
|
|
9
9
|
// beside a network send is what a scanner classifies as exfiltration, and what
|
|
10
10
|
// a human reviewer then has to disprove.
|
|
11
|
-
import { writeFileSync, renameSync, existsSync, mkdirSync, unlinkSync, readdirSync } from 'fs';
|
|
11
|
+
import { writeFileSync, renameSync, existsSync, mkdirSync, unlinkSync, readdirSync, readFileSync } from 'fs';
|
|
12
12
|
import { join } from 'path';
|
|
13
13
|
import { homedir } from 'os';
|
|
14
14
|
import { logger, formatError } from './logger.js';
|
|
15
|
+
import { noteWebappResponse, webappEdgeTracker, webappHeaders } from './utils/webapp-edge.js';
|
|
15
16
|
import { resolveWebappBaseUrl } from './utils/base-url.js';
|
|
16
17
|
import { Connector } from './connector.js';
|
|
17
|
-
import { readLocalSkillVersion, readAgentVisibleSkillVersions, validateSkillContent, compareSemver } from './utils/skill-version.js';
|
|
18
|
+
import { readLocalSkillVersion, readAgentVisibleSkillVersions, validateSkillContent, compareSemver, isBootstrapSkillVersion } from './utils/skill-version.js';
|
|
18
19
|
import { invalidateSkillsSnapshot } from './utils/skills-snapshot-invalidation.js';
|
|
19
20
|
import { verifySkillSignature, signatureRequired, readLastAppliedSignedAtMs, recordAppliedSignedAt, } from './utils/skill-signing.js';
|
|
20
21
|
import { isTradingMode, validateModeTransition, redactTokens } from '@reefclaw/shared';
|
|
@@ -62,15 +63,9 @@ const CRITICAL_EVENTS = new Set([
|
|
|
62
63
|
* (0.x) version. Long enough to be negligible load, short enough to un-strand
|
|
63
64
|
* a new user whose one-shot startup pull failed. */
|
|
64
65
|
const BOOTSTRAP_SKILL_HEAL_INTERVAL_MS = 10 * 60_000; // 10 min
|
|
65
|
-
/**
|
|
66
|
-
*
|
|
67
|
-
|
|
68
|
-
* as bootstrap so we keep pulling until the real skill lands. */
|
|
69
|
-
function isBootstrapSkillVersion(version) {
|
|
70
|
-
if (!version)
|
|
71
|
-
return true;
|
|
72
|
-
return compareSemver('1.0.0', version) > 0; // '1.0.0' > version ⟺ version < 1.0.0
|
|
73
|
-
}
|
|
66
|
+
/** How often the bridge drains edge-deny transitions into agent_state events
|
|
67
|
+
* (skill.webapp_edge_denied / _recovered audit rows via the relay). */
|
|
68
|
+
const WEBAPP_EDGE_EMIT_INTERVAL_MS = 60_000;
|
|
74
69
|
/** The single-agent layout every box used before multi-agent attach. */
|
|
75
70
|
export function defaultBridgePaths() {
|
|
76
71
|
return {
|
|
@@ -132,6 +127,9 @@ export class Bridge {
|
|
|
132
127
|
* timer) double-applying / double-notifying during the one-time bootstrap→
|
|
133
128
|
* full transition. The chain that starts (attempt 1) owns the flag. */
|
|
134
129
|
skillPullInFlight = false;
|
|
130
|
+
/** Drains webapp edge-deny transitions into one-shot agent_state events. */
|
|
131
|
+
webappEdgeTimer = null;
|
|
132
|
+
edgeProbeInFlight = false;
|
|
135
133
|
constructor(provider, connectorConfig,
|
|
136
134
|
/** Where the served agent's SKILL.md and session index live. The legacy
|
|
137
135
|
* single-agent paths by default; index.ts passes the resolved agent's
|
|
@@ -166,12 +164,14 @@ export class Bridge {
|
|
|
166
164
|
// without reconnecting can't be stranded on the bootstrap. No-op once
|
|
167
165
|
// upgraded (guarded on version).
|
|
168
166
|
this.startBootstrapHeal();
|
|
167
|
+
this.startWebappEdgeEmitter();
|
|
169
168
|
}
|
|
170
169
|
/** Stop the bridge: disconnect + stop provider */
|
|
171
170
|
stop() {
|
|
172
171
|
logger.info(TAG, 'Stopping bridge');
|
|
173
172
|
this.stopThrottleTimer();
|
|
174
173
|
this.stopBootstrapHeal();
|
|
174
|
+
this.stopWebappEdgeEmitter();
|
|
175
175
|
this.unwireProviderEvents();
|
|
176
176
|
this.connector.destroy();
|
|
177
177
|
this.provider.stop();
|
|
@@ -1075,6 +1075,86 @@ export class Bridge {
|
|
|
1075
1075
|
// ---- OTA SKILL.md update ----
|
|
1076
1076
|
/** Apply a SKILL.md update: verify signature (C1), write to workspace +
|
|
1077
1077
|
* installed skill dir, clear session cache, notify agent. */
|
|
1078
|
+
/**
|
|
1079
|
+
* HEARTBEAT.md — the heartbeat checklist the agent reads at the start of
|
|
1080
|
+
* every beat. Until 2026-09-27 the file never reached npx boxes, so every
|
|
1081
|
+
* beat opened with a failed `read` and ran on the abbreviated inline list.
|
|
1082
|
+
* It now rides the SKILL.md pull, signed offline like SKILL.md (same C1
|
|
1083
|
+
* gate: enforced → a valid signature is required; a PRESENT signature is
|
|
1084
|
+
* always verified), and is written beside every agent-visible reefclaw
|
|
1085
|
+
* SKILL.md copy — NEVER at the workspace root, where OpenClaw's own
|
|
1086
|
+
* heartbeat monitor (`openclaw doctor`) would adopt it and double-run beats.
|
|
1087
|
+
*/
|
|
1088
|
+
applyHeartbeatChecklist(env) {
|
|
1089
|
+
if (!env || typeof env.content !== 'string' || !env.content || typeof env.version !== 'string')
|
|
1090
|
+
return;
|
|
1091
|
+
if (signatureRequired() || env.signature) {
|
|
1092
|
+
const verdict = verifySkillSignature(env);
|
|
1093
|
+
if (!verdict.ok) {
|
|
1094
|
+
logger.warn(TAG, `HEARTBEAT.md: rejected — ${verdict.reason}`);
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
const dirs = this.reefclawSkillDirs();
|
|
1099
|
+
const written = this.writeHeartbeatChecklistFiles(env.content, dirs);
|
|
1100
|
+
if (written > 0)
|
|
1101
|
+
logger.info(TAG, `HEARTBEAT.md v${env.version} written beside ${written} reefclaw SKILL.md cop${written === 1 ? 'y' : 'ies'}`);
|
|
1102
|
+
}
|
|
1103
|
+
/** Every `<skills dir>/<*reefclaw*>/` that currently holds a SKILL.md, across
|
|
1104
|
+
* the layouts applySkillUpdate maintains (~/.openclaw/skills, the workspace
|
|
1105
|
+
* skills dir, plugin-extension bundled skills). Fail-soft. */
|
|
1106
|
+
reefclawSkillDirs() {
|
|
1107
|
+
const out = [];
|
|
1108
|
+
const scan = (skillsDir) => {
|
|
1109
|
+
let entries;
|
|
1110
|
+
try {
|
|
1111
|
+
entries = readdirSync(skillsDir, { withFileTypes: true });
|
|
1112
|
+
}
|
|
1113
|
+
catch {
|
|
1114
|
+
return;
|
|
1115
|
+
}
|
|
1116
|
+
for (const e of entries) {
|
|
1117
|
+
if (!e.isDirectory() || !e.name.includes('reefclaw'))
|
|
1118
|
+
continue;
|
|
1119
|
+
const dir = join(skillsDir, e.name);
|
|
1120
|
+
if (existsSync(join(dir, 'SKILL.md')))
|
|
1121
|
+
out.push(dir);
|
|
1122
|
+
}
|
|
1123
|
+
};
|
|
1124
|
+
scan(join(homedir(), '.openclaw', 'skills'));
|
|
1125
|
+
scan(join(this.paths.workspaceDir, 'skills'));
|
|
1126
|
+
const extensionsDir = join(homedir(), '.openclaw', 'extensions');
|
|
1127
|
+
try {
|
|
1128
|
+
for (const ext of readdirSync(extensionsDir, { withFileTypes: true })) {
|
|
1129
|
+
if (ext.isDirectory() && ext.name.includes('reefclaw'))
|
|
1130
|
+
scan(join(extensionsDir, ext.name, 'skills'));
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
catch {
|
|
1134
|
+
// no extensions dir
|
|
1135
|
+
}
|
|
1136
|
+
return out;
|
|
1137
|
+
}
|
|
1138
|
+
/** Write `HEARTBEAT.md` into each dir whose copy differs; returns the count
|
|
1139
|
+
* written. Pure with respect to the bridge (exported for tests via the
|
|
1140
|
+
* instance); never creates a directory, so it can never plant a file where
|
|
1141
|
+
* no reefclaw skill lives. */
|
|
1142
|
+
writeHeartbeatChecklistFiles(content, dirs) {
|
|
1143
|
+
let written = 0;
|
|
1144
|
+
for (const dir of dirs) {
|
|
1145
|
+
const target = join(dir, 'HEARTBEAT.md');
|
|
1146
|
+
try {
|
|
1147
|
+
if (existsSync(target) && readFileSync(target, 'utf-8') === content)
|
|
1148
|
+
continue;
|
|
1149
|
+
writeFileSync(target, content, 'utf-8');
|
|
1150
|
+
written++;
|
|
1151
|
+
}
|
|
1152
|
+
catch (err) {
|
|
1153
|
+
logger.warn(TAG, `HEARTBEAT.md: could not write ${target}: ${err instanceof Error ? err.message : String(err)}`);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
return written;
|
|
1157
|
+
}
|
|
1078
1158
|
async applySkillUpdate(env) {
|
|
1079
1159
|
const { version, content } = env;
|
|
1080
1160
|
const workspacePath = this.paths.workspaceDir;
|
|
@@ -1209,13 +1289,28 @@ export class Bridge {
|
|
|
1209
1289
|
else if (invalidation.outcome !== 'no-sessions-file') {
|
|
1210
1290
|
logger.info(TAG, invalidation.message);
|
|
1211
1291
|
}
|
|
1212
|
-
// 4. Send chat message to agent to trigger immediate re-read
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1292
|
+
// 4. Send chat message to agent to trigger immediate re-read — except
|
|
1293
|
+
// while this install's FIRST market check is pending (the heartbeat
|
|
1294
|
+
// job was just created and runFirstHeartbeat is waiting for these
|
|
1295
|
+
// instructions): that check reads them itself, and the notice would
|
|
1296
|
+
// be a second agent turn at the same moment on a new box. The
|
|
1297
|
+
// provider holds the notice and sends it only if the first check
|
|
1298
|
+
// ends without starting, so every other path (existing job, a slow
|
|
1299
|
+
// or blocked pull past the wait, a restart) still gets it.
|
|
1300
|
+
const sendNotice = async () => {
|
|
1301
|
+
try {
|
|
1302
|
+
await this.provider.handleChat(`Your trading instructions (SKILL.md) have been updated to v${version}. ` +
|
|
1303
|
+
`Check for any open positions with fetch_positions() and fetch_open_orders(), then re-read SKILL.md to confirm you've processed the changes.`);
|
|
1304
|
+
}
|
|
1305
|
+
catch (chatErr) {
|
|
1306
|
+
logger.warn(TAG, `Chat notification failed (non-fatal): ${chatErr instanceof Error ? chatErr.message : String(chatErr)}`);
|
|
1307
|
+
}
|
|
1308
|
+
};
|
|
1309
|
+
if (this.provider.deferToFirstMarketCheck?.(() => { void sendNotice(); }) === true) {
|
|
1310
|
+
logger.info(TAG, `SKILL.md v${version} applied while the first market check is pending — chat notice held (sent only if that check does not start)`);
|
|
1216
1311
|
}
|
|
1217
|
-
|
|
1218
|
-
|
|
1312
|
+
else {
|
|
1313
|
+
await sendNotice();
|
|
1219
1314
|
}
|
|
1220
1315
|
// 5. Update local version tracker + advance the replay clock. The clock
|
|
1221
1316
|
// advances ONLY on a VERIFIED signature: recording an unverified
|
|
@@ -1303,23 +1398,48 @@ export class Bridge {
|
|
|
1303
1398
|
// Scheme-checked (https, or http only to a private host) — see utils/base-url.ts.
|
|
1304
1399
|
const base = resolveWebappBaseUrl();
|
|
1305
1400
|
try {
|
|
1401
|
+
const pullStartedAt = Date.now();
|
|
1306
1402
|
const res = await fetch(`${base}/api/internal/skill-content`, {
|
|
1307
|
-
headers:
|
|
1403
|
+
headers: webappHeaders(this.connectionToken),
|
|
1308
1404
|
signal: AbortSignal.timeout(15_000),
|
|
1309
1405
|
});
|
|
1310
1406
|
if (res.status === 401 || res.status === 403) {
|
|
1311
|
-
|
|
1407
|
+
// A 403 can be the EDGE (Vercel mitigation denying this host's IP —
|
|
1408
|
+
// plain-text Forbidden + x-vercel-mitigated) rather than our auth.
|
|
1409
|
+
// Naming it matters: for a day this read as "connect the account
|
|
1410
|
+
// first" on a box whose account was fine (2026-09-28).
|
|
1411
|
+
let bodyText = null;
|
|
1412
|
+
if (res.status === 403) {
|
|
1413
|
+
try {
|
|
1414
|
+
bodyText = (await res.text()).slice(0, 200);
|
|
1415
|
+
}
|
|
1416
|
+
catch {
|
|
1417
|
+
/* classify on headers alone */
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
const edge = noteWebappResponse(res, '/api/internal/skill-content', bodyText);
|
|
1421
|
+
if (edge) {
|
|
1422
|
+
logger.warn(TAG, `SKILL.md webapp pull: denied by ReefClaw's edge firewall (${edge.action ?? 'deny'}${edge.requestId ? `, request ${edge.requestId}` : ''}) — ` +
|
|
1423
|
+
`an IP-level block on this host, not the account; the heal timer retries every ${BOOTSTRAP_SKILL_HEAL_INTERVAL_MS / 60_000} min`);
|
|
1424
|
+
}
|
|
1425
|
+
else {
|
|
1426
|
+
logger.warn(TAG, `SKILL.md webapp pull: not authorized (${res.status}) — connect the account first`);
|
|
1427
|
+
}
|
|
1312
1428
|
this.skillPullInFlight = false;
|
|
1313
|
-
return; //
|
|
1429
|
+
return; // Neither a bad token nor an edge block gets better within this chain.
|
|
1314
1430
|
}
|
|
1315
1431
|
if (!res.ok)
|
|
1316
1432
|
throw new Error(`HTTP ${res.status}`);
|
|
1433
|
+
noteWebappResponse(res, '/api/internal/skill-content', undefined, pullStartedAt);
|
|
1317
1434
|
const body = (await res.json());
|
|
1318
1435
|
if (!body?.version || !body?.content) {
|
|
1319
1436
|
logger.warn(TAG, 'SKILL.md webapp pull: response missing version/content');
|
|
1320
1437
|
this.skillPullInFlight = false;
|
|
1321
1438
|
return;
|
|
1322
1439
|
}
|
|
1440
|
+
// The heartbeat checklist is independent of the SKILL.md version gate
|
|
1441
|
+
// below: an up-to-date box still gets (or refreshes) HEARTBEAT.md.
|
|
1442
|
+
this.applyHeartbeatChecklist(body.heartbeat);
|
|
1323
1443
|
this.currentSkillVersion = readLocalSkillVersion(this.paths.workspaceDir);
|
|
1324
1444
|
// The workspace tracker alone is NOT proof the agent has the full skill:
|
|
1325
1445
|
// a plugin (re)install resets the agent-visible extension copy to the
|
|
@@ -1376,6 +1496,91 @@ export class Bridge {
|
|
|
1376
1496
|
setTimeout(() => void this.pullSkillContentFromWebapp(attempt + 1), delayMs).unref?.();
|
|
1377
1497
|
}
|
|
1378
1498
|
}
|
|
1499
|
+
/** Every minute: surface an edge-deny episode (the bridge's own webapp calls
|
|
1500
|
+
* ∪ the plugin's gate file) as one-shot agent_state events. The relay audits
|
|
1501
|
+
* them as skill.webapp_edge_denied / skill.webapp_edge_recovered — what the
|
|
1502
|
+
* fleet alarm keys on — while buildAgentState carries the live verdict
|
|
1503
|
+
* (health.webappApi) to the dashboard banner. Relay-borne on purpose: every
|
|
1504
|
+
* HTTP path to the webapp is the thing being denied. */
|
|
1505
|
+
startWebappEdgeEmitter() {
|
|
1506
|
+
if (this.webappEdgeTimer)
|
|
1507
|
+
return;
|
|
1508
|
+
const tick = () => {
|
|
1509
|
+
try {
|
|
1510
|
+
const events = webappEdgeTracker().takeEvents();
|
|
1511
|
+
for (const ev of events) {
|
|
1512
|
+
this.emit('agent_state', ev.event, ev);
|
|
1513
|
+
}
|
|
1514
|
+
// The live verdict rides health.webappApi on agent_state — re-emit the
|
|
1515
|
+
// state right away so the banner does not wait for the next natural
|
|
1516
|
+
// emission (review M2).
|
|
1517
|
+
if (events.length > 0)
|
|
1518
|
+
void this.reemitAgentState();
|
|
1519
|
+
}
|
|
1520
|
+
catch (err) {
|
|
1521
|
+
logger.debug(TAG, `webapp-edge emitter: ${formatError(err)}`);
|
|
1522
|
+
}
|
|
1523
|
+
// A deny observed on a one-shot call (the startup pull on an upgraded
|
|
1524
|
+
// box, the release check) has nothing periodic to clear it: re-probe
|
|
1525
|
+
// once per gate window so the bridge's own episode can never stick.
|
|
1526
|
+
void this.maybeProbeEdge();
|
|
1527
|
+
};
|
|
1528
|
+
this.webappEdgeTimer = setInterval(tick, WEBAPP_EDGE_EMIT_INTERVAL_MS);
|
|
1529
|
+
this.webappEdgeTimer.unref?.();
|
|
1530
|
+
}
|
|
1531
|
+
/** Re-send agent_state from a fresh snapshot (carries health.webappApi). */
|
|
1532
|
+
async reemitAgentState() {
|
|
1533
|
+
try {
|
|
1534
|
+
const snap = await this.provider.getSnapshot();
|
|
1535
|
+
if (snap?.agentState) {
|
|
1536
|
+
// Same payload shape the provider's own emission uses (AgentStateData).
|
|
1537
|
+
this.emit('agent_state', 'state_update', { state: snap.agentState, timestamp: new Date().toISOString() });
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
catch (err) {
|
|
1541
|
+
logger.debug(TAG, `webapp-edge: state re-emit skipped: ${formatError(err)}`);
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
/** While the bridge's own episode is open and its gate has reopened, send
|
|
1545
|
+
* ONE authenticated probe (GET /api/internal/config — the same class of
|
|
1546
|
+
* call the plugin polls) and feed the verdict to the tracker: a success
|
|
1547
|
+
* ends the episode, a deny re-closes the gate at the next rung. Exported
|
|
1548
|
+
* on the instance for tests. */
|
|
1549
|
+
async maybeProbeEdge() {
|
|
1550
|
+
const tr = webappEdgeTracker();
|
|
1551
|
+
if (!tr.hasOwnEpisode() || tr.isBlocked() || this.edgeProbeInFlight)
|
|
1552
|
+
return;
|
|
1553
|
+
this.edgeProbeInFlight = true;
|
|
1554
|
+
try {
|
|
1555
|
+
const startedAt = Date.now();
|
|
1556
|
+
const res = await fetch(`${resolveWebappBaseUrl()}/api/internal/config`, {
|
|
1557
|
+
headers: webappHeaders(this.connectionToken),
|
|
1558
|
+
signal: AbortSignal.timeout(10_000),
|
|
1559
|
+
});
|
|
1560
|
+
let body = null;
|
|
1561
|
+
if (res.status === 403) {
|
|
1562
|
+
try {
|
|
1563
|
+
body = (await res.text()).slice(0, 200);
|
|
1564
|
+
}
|
|
1565
|
+
catch {
|
|
1566
|
+
/* classify on headers alone */
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
noteWebappResponse(res, '/api/internal/config', body, startedAt);
|
|
1570
|
+
}
|
|
1571
|
+
catch (err) {
|
|
1572
|
+
logger.debug(TAG, `webapp-edge re-probe failed: ${formatError(err)}`);
|
|
1573
|
+
}
|
|
1574
|
+
finally {
|
|
1575
|
+
this.edgeProbeInFlight = false;
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
stopWebappEdgeEmitter() {
|
|
1579
|
+
if (this.webappEdgeTimer) {
|
|
1580
|
+
clearInterval(this.webappEdgeTimer);
|
|
1581
|
+
this.webappEdgeTimer = null;
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1379
1584
|
/** Start a bounded periodic re-pull of the full SKILL.md while the box is
|
|
1380
1585
|
* still on the bootstrap (0.x). pullSkillContentFromWebapp() (fired once at
|
|
1381
1586
|
* startup) is a one-shot with a finite retry burst; if every attempt fails —
|
|
@@ -1398,6 +1603,10 @@ export class Bridge {
|
|
|
1398
1603
|
this.stopBootstrapHeal();
|
|
1399
1604
|
return;
|
|
1400
1605
|
}
|
|
1606
|
+
if (webappEdgeTracker().isBlocked()) {
|
|
1607
|
+
logger.debug(TAG, `bootstrap heal: webapp edge-denied (${Math.ceil(webappEdgeTracker().msUntilOpen() / 60_000)} min left) — skipping this re-pull`);
|
|
1608
|
+
return;
|
|
1609
|
+
}
|
|
1401
1610
|
logger.info(TAG, `Still on bootstrap SKILL.md (v${this.currentSkillVersion ?? 'unknown'}) — re-attempting webapp pull`);
|
|
1402
1611
|
void this.pullSkillContentFromWebapp();
|
|
1403
1612
|
}, BOOTSTRAP_SKILL_HEAL_INTERVAL_MS);
|
|
@@ -24,6 +24,17 @@ export declare function hasConfiguredChannels(cfg: unknown): boolean | undefined
|
|
|
24
24
|
* delivery `announce` can only fail at run time, so switch it to `none`.
|
|
25
25
|
* Runs on every bridge start (cheap: one cron.list), independent of the
|
|
26
26
|
* once-per-install marker, because the job predates this fix on every box.
|
|
27
|
+
*
|
|
28
|
+
* ★ `cron.update` needs operator.admin, which the long-lived session holds
|
|
29
|
+
* only when the process started with the heartbeat ensure owed, so on a box
|
|
30
|
+
* with a marker this patch is refused and nothing changes. That is
|
|
31
|
+
* DELIBERATE (2026-09-29 review): the "no channel" signal
|
|
32
|
+
* (hasConfiguredChannels) reads only the top-level openclaw.json and
|
|
33
|
+
* misreads an env-only Telegram token, `$include`d channels and a moved
|
|
34
|
+
* config dir as "none". Widening the repair (an admin side-session was
|
|
35
|
+
* tried) would silently switch off heartbeat summaries that work today. A
|
|
36
|
+
* wider repair must key off the job's own recorded delivery failure, never
|
|
37
|
+
* this guess.
|
|
27
38
|
*/
|
|
28
39
|
export declare function reconcileHeartbeatDelivery(opts: {
|
|
29
40
|
rpc: CronRpcClient;
|
|
@@ -65,6 +76,16 @@ export declare function pickHeartbeatJob<T extends {
|
|
|
65
76
|
name?: unknown;
|
|
66
77
|
payload?: unknown;
|
|
67
78
|
}>(jobs: readonly T[]): T | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* Is the once-per-install ensure still owed on this box? True with no marker,
|
|
81
|
+
* and with a marker from the old rule that adopted a job that is not
|
|
82
|
+
* ReefClaw's (re-checked once). ensureHeartbeatCron runs exactly when this is
|
|
83
|
+
* true, and the bridge requests operator.admin exactly when this is true:
|
|
84
|
+
* both read this one function so they cannot drift. (They did: the re-check
|
|
85
|
+
* ran on a read/write-only session, so its cron.add failed on the scope and
|
|
86
|
+
* the adopted box kept getting no ReefClaw beat.)
|
|
87
|
+
*/
|
|
88
|
+
export declare function heartbeatEnsureOwed(markerPath: string): boolean;
|
|
68
89
|
export type EnsureHeartbeatOutcome = 'already_ensured' | 'found_existing' | 'created' | 'failed';
|
|
69
90
|
/**
|
|
70
91
|
* Ensure a heartbeat cron exists. Non-fatal by contract: every failure path
|
|
@@ -96,3 +117,46 @@ export declare function ensureHeartbeatCron(opts: {
|
|
|
96
117
|
* `none` (announce can only fail); true/undefined → announce as before. */
|
|
97
118
|
channelsConfigured?: boolean;
|
|
98
119
|
}): Promise<EnsureHeartbeatOutcome>;
|
|
120
|
+
/** How long a fresh install waits for the full trading instructions before
|
|
121
|
+
* starting its first market check. Past this, the first check is left to the
|
|
122
|
+
* regular schedule (one interval later), exactly as before. */
|
|
123
|
+
export declare const FIRST_BEAT_MAX_WAIT_MS = 120000;
|
|
124
|
+
export type FirstBeatOutcome = 'started' | 'not_ready' | 'not_found' | 'not_run' | 'inactive' | 'unknown' | 'failed';
|
|
125
|
+
/**
|
|
126
|
+
* Start the FIRST market check right away on a fresh install instead of one
|
|
127
|
+
* full interval (15 min) after connecting.
|
|
128
|
+
*
|
|
129
|
+
* Why (2026-09-29): a new user connected, saw "You're connected", and then
|
|
130
|
+
* nothing happened for 15 minutes — the job's first run is scheduled one
|
|
131
|
+
* interval after creation. Call this ONLY when ensureHeartbeatCron returned
|
|
132
|
+
* 'created' (a brand-new job, once per install): it must never add beats to
|
|
133
|
+
* an existing agent.
|
|
134
|
+
*
|
|
135
|
+
* Rules:
|
|
136
|
+
* • Waits (≤ FIRST_BEAT_MAX_WAIT_MS) until the full trading SKILL.md is what
|
|
137
|
+
* the agent reads. A beat on the bootstrap instructions would be a wasted
|
|
138
|
+
* run; if they never arrive, the first check stays on the schedule (old
|
|
139
|
+
* behaviour).
|
|
140
|
+
* • `cron.run` needs operator.admin, which the bridge's session holds only
|
|
141
|
+
* when the process started with the ensure owed; call this with the client
|
|
142
|
+
* that created the job.
|
|
143
|
+
* • ONE cron.run call, never retried: a timeout or a dropped socket may
|
|
144
|
+
* still have started the run, and a second call would start a second beat.
|
|
145
|
+
* • Never throws; every failure leaves the regular schedule untouched.
|
|
146
|
+
*/
|
|
147
|
+
export declare function runFirstHeartbeat(opts: {
|
|
148
|
+
rpc: CronRpcClient;
|
|
149
|
+
log: (msg: string) => void;
|
|
150
|
+
agentId?: string;
|
|
151
|
+
agentIsDefault?: boolean;
|
|
152
|
+
/** True once the full trading SKILL.md (not the bootstrap) is what the agent reads. */
|
|
153
|
+
instructionsReady: () => boolean;
|
|
154
|
+
/** False once the connection this was started on is gone (provider stopped
|
|
155
|
+
* or client replaced). Checked while waiting and before each call.
|
|
156
|
+
* Absent = always active. */
|
|
157
|
+
isActive?: () => boolean;
|
|
158
|
+
maxWaitMs?: number;
|
|
159
|
+
pollMs?: number;
|
|
160
|
+
/** Injectable for tests. */
|
|
161
|
+
sleep?: (ms: number) => Promise<void>;
|
|
162
|
+
}): Promise<FirstBeatOutcome>;
|