@reefclaw/openclaw-plugin 0.1.44 → 0.1.47
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 +1 -1
- package/bridge/bridge.js +3 -3
- package/bridge/config.js +5 -2
- package/bridge/gateway/gateway-config.d.ts +5 -0
- package/bridge/gateway/gateway-config.js +5 -2
- package/bridge/gateway/heartbeat-cron.js +2 -2
- package/bridge/heartbeat-runs.js +2 -3
- package/bridge/heartbeat-transcript.d.ts +2 -0
- package/bridge/heartbeat-transcript.js +45 -13
- package/bridge/index.js +28 -0
- package/bridge/providers/connector-update.js +2 -3
- package/bridge/providers/emergency-commands.js +2 -3
- package/bridge/providers/gateway.d.ts +6 -0
- package/bridge/providers/gateway.js +40 -25
- package/bridge/providers/onboarding-commands.d.ts +8 -7
- package/bridge/setup.js +12 -0
- package/bridge/shock-wake.d.ts +7 -0
- package/bridge/shock-wake.js +60 -7
- package/bridge/utils/base-url.d.ts +26 -0
- package/bridge/utils/base-url.js +108 -0
- package/bridge/utils/operator-secret.d.ts +7 -0
- package/bridge/utils/operator-secret.js +59 -0
- package/bridge/utils/relay-host.d.ts +23 -0
- package/bridge/utils/relay-host.js +66 -0
- package/bridge/utils/untrusted-text.d.ts +9 -0
- package/bridge/utils/untrusted-text.js +51 -0
- package/config/base-url.d.ts +16 -0
- package/config/base-url.js +87 -0
- package/config/operator-provenance.d.ts +19 -0
- package/config/operator-provenance.js +102 -25
- package/config/user-data-stream-config.d.ts +5 -1
- package/config/user-data-stream-config.js +7 -5
- package/connector-supervisor.d.ts +15 -0
- package/connector-supervisor.js +22 -1
- package/index.js +75 -25
- package/ingest/position-decisions-client.js +23 -4
- package/ingest/retry-after.d.ts +5 -0
- package/ingest/retry-after.js +21 -0
- package/ingest/trade-store-client.js +18 -3
- package/lifecycle/process-role.d.ts +4 -0
- package/lifecycle/process-role.js +1 -1
- package/openclaw.plugin.json +1 -1
- package/package.json +2 -2
- package/release-integrity.json +10 -0
- package/release-notes.json +3 -1
- package/risk/pre-trade-check.js +1 -1
- package/risk/stop-widening-guard.d.ts +35 -0
- package/risk/stop-widening-guard.js +98 -0
- package/skills/reefclaw/SKILL.md +2 -2
- package/tools/create-order.js +17 -12
- package/tools/get-bracket-config.d.ts +3 -1
- package/tools/get-bracket-config.js +3 -1
- package/tools/get-market-intel.d.ts +1 -0
- package/tools/get-market-intel.js +14 -3
- package/tools/modify-stop.d.ts +6 -0
- package/tools/modify-stop.js +110 -0
- package/tools/set-bracket-requirement.d.ts +7 -0
- package/tools/set-bracket-requirement.js +12 -0
- package/trading-params-bounds.d.ts +32 -0
- package/trading-params-bounds.js +113 -0
- package/trading-params-cache.d.ts +9 -1
- package/trading-params-cache.js +33 -2
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { TradingParameters } from './trading-params-cache.js';
|
|
2
|
+
export type TradingParamKey = keyof TradingParameters;
|
|
3
|
+
/** [min, max] per key — a copy of intel's PARAM_META ranges, kept HERE so the
|
|
4
|
+
* floor cannot be moved from the network it is meant to bound. */
|
|
5
|
+
export declare const TRADING_PARAM_BOUNDS: Readonly<Record<TradingParamKey, readonly [number, number]>>;
|
|
6
|
+
export interface ParamAdjustment {
|
|
7
|
+
key: TradingParamKey;
|
|
8
|
+
/** What the network supplied (stringified for the log line). */
|
|
9
|
+
received: string;
|
|
10
|
+
/** What the gate will actually use. */
|
|
11
|
+
applied: number;
|
|
12
|
+
reason: 'not_a_number' | 'below_min' | 'below_min_kept' | 'above_max' | 'drawdown_order';
|
|
13
|
+
}
|
|
14
|
+
export interface SanitizedTradingParams {
|
|
15
|
+
params: TradingParameters;
|
|
16
|
+
adjustments: ParamAdjustment[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Build a TradingParameters object from an untrusted network payload.
|
|
20
|
+
*
|
|
21
|
+
* Per key: a finite number inside its hardcoded range is taken as-is; a finite
|
|
22
|
+
* number outside it is CLAMPED to the nearest bound (the operator's intent is
|
|
23
|
+
* "as far as allowed", not "ignore me") — except a risk LIMIT below its
|
|
24
|
+
* minimum, which is kept (tighter than the UI allows is never a risk to
|
|
25
|
+
* clamp away; see RISK_LIMIT_KEYS); anything else (missing, NaN, string,
|
|
26
|
+
* null, object) falls back to the hardcoded default. Unknown keys are dropped.
|
|
27
|
+
*
|
|
28
|
+
* Drawdown zones must additionally satisfy RED ≤ ORANGE ≤ YELLOW (deeper
|
|
29
|
+
* drawdown = more negative). An inverted triple would make the zone ladder
|
|
30
|
+
* skip rungs, so it is replaced wholesale by the defaults.
|
|
31
|
+
*/
|
|
32
|
+
export declare function sanitizeTradingParams(raw: unknown, defaults: TradingParameters): SanitizedTradingParams;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Hardcoded bounds for the network-sourced trading parameters.
|
|
2
|
+
//
|
|
3
|
+
// The intel service is the POLICY channel for these knobs (the operator moves
|
|
4
|
+
// them from the Parameters tab); the plugin is the MECHANISM that enforces
|
|
5
|
+
// them in the pre-trade gate. CLAUDE.md's load-bearing rule for that split:
|
|
6
|
+
// "config can only move knobs WITHIN hardcoded bounds, never disable a stop".
|
|
7
|
+
// Before this module the fetched object was spread over the defaults with no
|
|
8
|
+
// type or range check, so a compromised / spoofed intel response (or a
|
|
9
|
+
// plain-http MITM of it) could hand the gate `maxOpenPositions: NaN` or
|
|
10
|
+
// `drawdownRed: "x"` — every `>`/`<=` comparison against it is then `false`
|
|
11
|
+
// and that gate is silently OFF. Audit 2026-09-27 H4.
|
|
12
|
+
//
|
|
13
|
+
// The ranges mirror intelligence/src/trading-params/types.ts PARAM_META (the
|
|
14
|
+
// operator UI's own min/max), so a legitimate value is never altered — the
|
|
15
|
+
// clamp only bites on values the UI could not have produced.
|
|
16
|
+
/** [min, max] per key — a copy of intel's PARAM_META ranges, kept HERE so the
|
|
17
|
+
* floor cannot be moved from the network it is meant to bound. */
|
|
18
|
+
export const TRADING_PARAM_BOUNDS = {
|
|
19
|
+
regimeConfidenceFloor: [0.15, 0.8],
|
|
20
|
+
signalConfluenceThreshold: [0.5, 1.0],
|
|
21
|
+
scorecardStrongGoThreshold: [6.0, 9.0],
|
|
22
|
+
scorecardGoThreshold: [4.0, 7.5],
|
|
23
|
+
scorecardMarginalThreshold: [2.0, 5.5],
|
|
24
|
+
confidencePenaltyThreshold: [0.15, 0.6],
|
|
25
|
+
unprovenSetupScore: [3, 7],
|
|
26
|
+
rrWarningThreshold: [1.0, 2.5],
|
|
27
|
+
lossStreakQuarterSize: [3, 10],
|
|
28
|
+
lossStreakHalfSize: [2, 8],
|
|
29
|
+
maxPositionSize: [1_000, 100_000],
|
|
30
|
+
maxOpenPositions: [1, 20],
|
|
31
|
+
maxGrossExposure: [0.5, 3.0],
|
|
32
|
+
maxPerTradeLoss: [50, 5_000],
|
|
33
|
+
drawdownYellow: [-0.05, -0.005],
|
|
34
|
+
drawdownOrange: [-0.1, -0.01],
|
|
35
|
+
drawdownRed: [-0.15, -0.015],
|
|
36
|
+
};
|
|
37
|
+
/** Risk LIMITS: a value below the UI minimum is TIGHTER than the UI allows,
|
|
38
|
+
* and a floor must never loosen risk — so it is kept as-is (and reported),
|
|
39
|
+
* never raised to the minimum. Only the upper bound is clamped for these.
|
|
40
|
+
* (Review 2026-09-27 N1: intel's own validation stores e.g. maxPerTradeLoss
|
|
41
|
+
* below 50; clamping it UP to 50 would have raised a live tenant's loss cap.) */
|
|
42
|
+
const RISK_LIMIT_KEYS = new Set([
|
|
43
|
+
'maxPositionSize',
|
|
44
|
+
'maxOpenPositions',
|
|
45
|
+
'maxGrossExposure',
|
|
46
|
+
'maxPerTradeLoss',
|
|
47
|
+
]);
|
|
48
|
+
function describe(v) {
|
|
49
|
+
if (typeof v === 'string')
|
|
50
|
+
return JSON.stringify(v.length > 40 ? `${v.slice(0, 40)}…` : v);
|
|
51
|
+
if (typeof v === 'number' || typeof v === 'boolean' || v == null)
|
|
52
|
+
return String(v);
|
|
53
|
+
return Array.isArray(v) ? 'array' : typeof v;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Build a TradingParameters object from an untrusted network payload.
|
|
57
|
+
*
|
|
58
|
+
* Per key: a finite number inside its hardcoded range is taken as-is; a finite
|
|
59
|
+
* number outside it is CLAMPED to the nearest bound (the operator's intent is
|
|
60
|
+
* "as far as allowed", not "ignore me") — except a risk LIMIT below its
|
|
61
|
+
* minimum, which is kept (tighter than the UI allows is never a risk to
|
|
62
|
+
* clamp away; see RISK_LIMIT_KEYS); anything else (missing, NaN, string,
|
|
63
|
+
* null, object) falls back to the hardcoded default. Unknown keys are dropped.
|
|
64
|
+
*
|
|
65
|
+
* Drawdown zones must additionally satisfy RED ≤ ORANGE ≤ YELLOW (deeper
|
|
66
|
+
* drawdown = more negative). An inverted triple would make the zone ladder
|
|
67
|
+
* skip rungs, so it is replaced wholesale by the defaults.
|
|
68
|
+
*/
|
|
69
|
+
export function sanitizeTradingParams(raw, defaults) {
|
|
70
|
+
const src = raw && typeof raw === 'object' && !Array.isArray(raw) ? raw : {};
|
|
71
|
+
const adjustments = [];
|
|
72
|
+
const out = { ...defaults };
|
|
73
|
+
for (const key of Object.keys(TRADING_PARAM_BOUNDS)) {
|
|
74
|
+
const [min, max] = TRADING_PARAM_BOUNDS[key];
|
|
75
|
+
const v = src[key];
|
|
76
|
+
if (typeof v !== 'number' || !Number.isFinite(v)) {
|
|
77
|
+
// Absent is the normal case for a partial payload — only report when
|
|
78
|
+
// something was actually supplied and unusable.
|
|
79
|
+
if (v !== undefined) {
|
|
80
|
+
adjustments.push({ key, received: describe(v), applied: defaults[key], reason: 'not_a_number' });
|
|
81
|
+
}
|
|
82
|
+
out[key] = defaults[key];
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (v < min && RISK_LIMIT_KEYS.has(key)) {
|
|
86
|
+
adjustments.push({ key, received: String(v), applied: v, reason: 'below_min_kept' });
|
|
87
|
+
out[key] = v;
|
|
88
|
+
}
|
|
89
|
+
else if (v < min) {
|
|
90
|
+
adjustments.push({ key, received: String(v), applied: min, reason: 'below_min' });
|
|
91
|
+
out[key] = min;
|
|
92
|
+
}
|
|
93
|
+
else if (v > max) {
|
|
94
|
+
adjustments.push({ key, received: String(v), applied: max, reason: 'above_max' });
|
|
95
|
+
out[key] = max;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
out[key] = v;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!(out.drawdownRed <= out.drawdownOrange && out.drawdownOrange <= out.drawdownYellow)) {
|
|
102
|
+
for (const key of ['drawdownYellow', 'drawdownOrange', 'drawdownRed']) {
|
|
103
|
+
adjustments.push({
|
|
104
|
+
key,
|
|
105
|
+
received: String(out[key]),
|
|
106
|
+
applied: defaults[key],
|
|
107
|
+
reason: 'drawdown_order',
|
|
108
|
+
});
|
|
109
|
+
out[key] = defaults[key];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return { params: out, adjustments };
|
|
113
|
+
}
|
|
@@ -18,8 +18,16 @@ export interface TradingParameters {
|
|
|
18
18
|
drawdownOrange: number;
|
|
19
19
|
drawdownRed: number;
|
|
20
20
|
}
|
|
21
|
-
/** Get trading parameters (from cache or fetch). Returns defaults on failure.
|
|
21
|
+
/** Get trading parameters (from cache or fetch). Returns defaults on failure.
|
|
22
|
+
*
|
|
23
|
+
* The fetched payload is UNTRUSTED policy from the network: every field is
|
|
24
|
+
* type-checked and clamped into the hardcoded range (trading-params-bounds.ts)
|
|
25
|
+
* before it can reach the pre-trade gate. Audit 2026-09-27 H4 — a NaN /
|
|
26
|
+
* string / null used to make the gate's comparison `false` and switch that
|
|
27
|
+
* limit off silently. */
|
|
22
28
|
export declare function getTradingParams(deps: IntelApiDeps): Promise<TradingParameters>;
|
|
29
|
+
/** Test-only: drop the cache back to the hardcoded defaults. */
|
|
30
|
+
export declare function resetTradingParamsCacheForTest(): void;
|
|
23
31
|
/** Get cached params synchronously (for pure functions like preTradeRiskCheck). */
|
|
24
32
|
export declare function getCachedTradingParams(): TradingParameters;
|
|
25
33
|
/** Force-refresh the cache (call on plugin init). */
|
package/trading-params-cache.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// Trading parameters cache — fetches from intelligence API and caches locally.
|
|
2
2
|
// Plugin tools (score-setup, pre-trade-check) read from this cache.
|
|
3
3
|
import { fetchIntelApi } from './tools/intel-api.js';
|
|
4
|
+
import { logger } from './logger.js';
|
|
5
|
+
import { sanitizeTradingParams } from './trading-params-bounds.js';
|
|
6
|
+
const TAG = 'trading-params';
|
|
4
7
|
// Hardcoded defaults (same as intelligence/src/trading-params/types.ts)
|
|
5
8
|
const DEFAULTS = {
|
|
6
9
|
regimeConfidenceFloor: 0.35,
|
|
@@ -24,7 +27,27 @@ const DEFAULTS = {
|
|
|
24
27
|
let cached = { ...DEFAULTS };
|
|
25
28
|
let cacheExpiry = 0;
|
|
26
29
|
const CACHE_TTL = 60_000; // 60s
|
|
27
|
-
/**
|
|
30
|
+
/** Signature of the last reported adjustment set — the fetch repeats every
|
|
31
|
+
* 60s, the warning should not. */
|
|
32
|
+
let lastAdjustmentSig = '';
|
|
33
|
+
function reportAdjustments(adjustments) {
|
|
34
|
+
const sig = adjustments.map((a) => `${a.key}=${a.received}→${a.applied}(${a.reason})`).join(', ');
|
|
35
|
+
if (sig === lastAdjustmentSig)
|
|
36
|
+
return;
|
|
37
|
+
lastAdjustmentSig = sig;
|
|
38
|
+
if (sig === '') {
|
|
39
|
+
logger.info(TAG, 'intel trading params back within the hardcoded bounds — clamp released');
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
logger.warn(TAG, `intel trading params outside the hardcoded bounds — clamped (the gate uses the applied value): ${sig}`);
|
|
43
|
+
}
|
|
44
|
+
/** Get trading parameters (from cache or fetch). Returns defaults on failure.
|
|
45
|
+
*
|
|
46
|
+
* The fetched payload is UNTRUSTED policy from the network: every field is
|
|
47
|
+
* type-checked and clamped into the hardcoded range (trading-params-bounds.ts)
|
|
48
|
+
* before it can reach the pre-trade gate. Audit 2026-09-27 H4 — a NaN /
|
|
49
|
+
* string / null used to make the gate's comparison `false` and switch that
|
|
50
|
+
* limit off silently. */
|
|
28
51
|
export async function getTradingParams(deps) {
|
|
29
52
|
const now = Date.now();
|
|
30
53
|
if (now < cacheExpiry)
|
|
@@ -32,8 +55,10 @@ export async function getTradingParams(deps) {
|
|
|
32
55
|
try {
|
|
33
56
|
const res = await fetchIntelApi('/api/trading-params', deps);
|
|
34
57
|
if (!('error' in res) && res.params) {
|
|
35
|
-
|
|
58
|
+
const { params, adjustments } = sanitizeTradingParams(res.params, DEFAULTS);
|
|
59
|
+
cached = params;
|
|
36
60
|
cacheExpiry = now + CACHE_TTL;
|
|
61
|
+
reportAdjustments(adjustments);
|
|
37
62
|
}
|
|
38
63
|
}
|
|
39
64
|
catch {
|
|
@@ -41,6 +66,12 @@ export async function getTradingParams(deps) {
|
|
|
41
66
|
}
|
|
42
67
|
return cached;
|
|
43
68
|
}
|
|
69
|
+
/** Test-only: drop the cache back to the hardcoded defaults. */
|
|
70
|
+
export function resetTradingParamsCacheForTest() {
|
|
71
|
+
cached = { ...DEFAULTS };
|
|
72
|
+
cacheExpiry = 0;
|
|
73
|
+
lastAdjustmentSig = '';
|
|
74
|
+
}
|
|
44
75
|
/** Get cached params synchronously (for pure functions like preTradeRiskCheck). */
|
|
45
76
|
export function getCachedTradingParams() {
|
|
46
77
|
return cached;
|