@senpi-ai/runtime 3.0.151 → 3.0.155
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/dist/actions/action-events.d.ts +4 -4
- package/dist/actions/action-events.d.ts.map +1 -1
- package/dist/actions/action-events.js +17 -7
- package/dist/actions/action-events.js.map +1 -1
- package/dist/actions/event-derivation.d.ts.map +1 -1
- package/dist/actions/event-derivation.js +17 -2
- package/dist/actions/event-derivation.js.map +1 -1
- package/dist/actions/open-position/open-position.action.d.ts.map +1 -1
- package/dist/actions/open-position/open-position.action.js +8 -3
- package/dist/actions/open-position/open-position.action.js.map +1 -1
- package/dist/cli/guide-commands.d.ts.map +1 -1
- package/dist/cli/guide-commands.js +2 -1
- package/dist/cli/guide-commands.js.map +1 -1
- package/dist/constants/telemetry.d.ts +19 -0
- package/dist/constants/telemetry.d.ts.map +1 -0
- package/dist/constants/telemetry.js +32 -0
- package/dist/constants/telemetry.js.map +1 -0
- package/dist/dsl/monitor/process-one-position.d.ts.map +1 -1
- package/dist/dsl/monitor/process-one-position.js +6 -2
- package/dist/dsl/monitor/process-one-position.js.map +1 -1
- package/dist/dsl/plugin/index.d.ts +0 -1
- package/dist/dsl/plugin/index.d.ts.map +1 -1
- package/dist/dsl/plugin/index.js +4 -17
- package/dist/dsl/plugin/index.js.map +1 -1
- package/dist/health/types.d.ts +10 -0
- package/dist/health/types.d.ts.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +77 -1
- package/dist/index.js.map +1 -1
- package/dist/runtime/domain-events.d.ts.map +1 -1
- package/dist/runtime/domain-events.js +2 -0
- package/dist/runtime/domain-events.js.map +1 -1
- package/dist/runtime/events-telemetry.d.ts +120 -0
- package/dist/runtime/events-telemetry.d.ts.map +1 -0
- package/dist/runtime/events-telemetry.js +314 -0
- package/dist/runtime/events-telemetry.js.map +1 -0
- package/dist/runtime/risk-guard.d.ts +20 -5
- package/dist/runtime/risk-guard.d.ts.map +1 -1
- package/dist/runtime/risk-guard.js +111 -23
- package/dist/runtime/risk-guard.js.map +1 -1
- package/dist/runtime/run.d.ts +7 -0
- package/dist/runtime/run.d.ts.map +1 -1
- package/dist/runtime/run.js +7 -0
- package/dist/runtime/run.js.map +1 -1
- package/dist/runtime/runtime.d.ts +21 -0
- package/dist/runtime/runtime.d.ts.map +1 -1
- package/dist/runtime/runtime.js +51 -3
- package/dist/runtime/runtime.js.map +1 -1
- package/dist/runtime/strategies-store.d.ts +26 -0
- package/dist/runtime/strategies-store.d.ts.map +1 -0
- package/dist/runtime/strategies-store.js +47 -0
- package/dist/runtime/strategies-store.js.map +1 -0
- package/dist/runtime/strategies-telemetry.d.ts +124 -0
- package/dist/runtime/strategies-telemetry.d.ts.map +1 -0
- package/dist/runtime/strategies-telemetry.js +143 -0
- package/dist/runtime/strategies-telemetry.js.map +1 -0
- package/dist/runtime/strategy-lifecycle.d.ts +36 -13
- package/dist/runtime/strategy-lifecycle.d.ts.map +1 -1
- package/dist/runtime/strategy-lifecycle.js +120 -71
- package/dist/runtime/strategy-lifecycle.js.map +1 -1
- package/dist/runtime-api/intake/intake-mount.d.ts +2 -0
- package/dist/runtime-api/intake/intake-mount.d.ts.map +1 -1
- package/dist/runtime-api/intake/intake-mount.js +6 -3
- package/dist/runtime-api/intake/intake-mount.js.map +1 -1
- package/dist/runtime-api/routes/signals.d.ts.map +1 -1
- package/dist/runtime-api/routes/signals.js +10 -0
- package/dist/runtime-api/routes/signals.js.map +1 -1
- package/dist/state/state-manager.d.ts +2 -0
- package/dist/state/state-manager.d.ts.map +1 -1
- package/dist/state/state-manager.js +8 -0
- package/dist/state/state-manager.js.map +1 -1
- package/dist/telemetry/event-builders.d.ts +8 -2
- package/dist/telemetry/event-builders.d.ts.map +1 -1
- package/dist/telemetry/event-builders.js +6 -0
- package/dist/telemetry/event-builders.js.map +1 -1
- package/dist/telemetry/event-store.d.ts +30 -7
- package/dist/telemetry/event-store.d.ts.map +1 -1
- package/dist/telemetry/event-store.js +114 -20
- package/dist/telemetry/event-store.js.map +1 -1
- package/dist/telemetry/spans/redact-pii.d.ts.map +1 -1
- package/dist/telemetry/spans/redact-pii.js +19 -2
- package/dist/telemetry/spans/redact-pii.js.map +1 -1
- package/dist/types/action.d.ts +27 -7
- package/dist/types/action.d.ts.map +1 -1
- package/dist/utils/event-catalog.d.ts +9 -1
- package/dist/utils/event-catalog.d.ts.map +1 -1
- package/dist/utils/event-catalog.js.map +1 -1
- package/dist/utils/logger.d.ts +5 -3
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +39 -28
- package/dist/utils/logger.js.map +1 -1
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Events telemetry: the in-memory buffer of runtime event records, the tap that fills it from the
|
|
3
|
+
* per-wallet event sink, and the loop that broadcasts new records as `plugin.senpi.events`.
|
|
4
|
+
*/
|
|
5
|
+
import { type EventSink, type StoredEvent } from "../telemetry/event-store.js";
|
|
6
|
+
import { type PiiRedactor } from "../telemetry/spans/redact-pii.js";
|
|
7
|
+
import { type LogLevel } from "../utils/logger.js";
|
|
8
|
+
import { type StrategiesBroadcastFn } from "./strategies-telemetry.js";
|
|
9
|
+
/** One event as broadcast. `address` is the wallet whose sink received the event. */
|
|
10
|
+
export interface EventRecord {
|
|
11
|
+
seq: number;
|
|
12
|
+
ts: number;
|
|
13
|
+
name: string;
|
|
14
|
+
level: LogLevel;
|
|
15
|
+
address: string;
|
|
16
|
+
body: string;
|
|
17
|
+
attrs: Record<string, string | number | boolean>;
|
|
18
|
+
}
|
|
19
|
+
export interface EventsMessage {
|
|
20
|
+
v: 1;
|
|
21
|
+
epoch: string;
|
|
22
|
+
batchSeq: number;
|
|
23
|
+
events: EventRecord[];
|
|
24
|
+
}
|
|
25
|
+
export interface EventsReplay {
|
|
26
|
+
epoch: string;
|
|
27
|
+
events: EventRecord[];
|
|
28
|
+
truncated: boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface EventsCursor {
|
|
31
|
+
afterEpoch: string;
|
|
32
|
+
afterSeq: number;
|
|
33
|
+
}
|
|
34
|
+
export interface EventsBuffer {
|
|
35
|
+
/** Gives the event the next `seq` and appends it to the ring of `address`. */
|
|
36
|
+
add(address: string, event: StoredEvent): void;
|
|
37
|
+
/** Held records with `seq` greater than `seq`, in `seq` order. */
|
|
38
|
+
after(seq: number): EventRecord[];
|
|
39
|
+
/** The highest `seq` held in any ring, `0` when nothing is held. */
|
|
40
|
+
newestSeq(): number;
|
|
41
|
+
/**
|
|
42
|
+
* The highest `seq` evicted from any ring, `0` when nothing was evicted. Deleting a closed ring
|
|
43
|
+
* evicts its records.
|
|
44
|
+
*/
|
|
45
|
+
maxEvictedSeq(): number;
|
|
46
|
+
/**
|
|
47
|
+
* Closes the ring of `address`. A closed ring takes no new records and stays in `after` until
|
|
48
|
+
* `dropSent` deletes it. The next record for `address` starts a new, empty ring. Closing does not
|
|
49
|
+
* change `maxEvictedSeq`.
|
|
50
|
+
*/
|
|
51
|
+
remove(address: string): void;
|
|
52
|
+
/**
|
|
53
|
+
* Deletes each closed ring whose newest `seq` is at most `sentSeq`, and each closed ring closed at
|
|
54
|
+
* least `EVENTS_CLOSED_RING_TTL_MS` ago. `maxEvictedSeq` rises to the newest `seq` of each deleted ring.
|
|
55
|
+
*/
|
|
56
|
+
dropSent(sentSeq: number): void;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* One ring of `capacity` records per address. `seq` starts at 1 and counts across every address.
|
|
60
|
+
* `now` is the clock, in epoch ms, that times a closed ring.
|
|
61
|
+
*/
|
|
62
|
+
export declare function createEventsBuffer(capacity?: number, now?: () => number): EventsBuffer;
|
|
63
|
+
/** The process buffer. Its `seq` run belongs to `PROCESS_EPOCH`. */
|
|
64
|
+
export declare const eventsBuffer: EventsBuffer;
|
|
65
|
+
/**
|
|
66
|
+
* Adds a record to `buffer` under `address` for a case that is not a runtime event. It does not go
|
|
67
|
+
* through `logger.event`, so no event sink, disk ring or OTel record receives it. A blank `address`
|
|
68
|
+
* adds nothing. Never throws.
|
|
69
|
+
*/
|
|
70
|
+
export declare function addEventRecord(address: string, event: StoredEvent, buffer?: EventsBuffer): void;
|
|
71
|
+
/**
|
|
72
|
+
* Wraps a wallet's event sink. Every event goes to `sink`. An event whose name is in
|
|
73
|
+
* `EVENTS_BROADCAST_NAMES` is also added to `buffer` under `address`.
|
|
74
|
+
*/
|
|
75
|
+
export declare function withEventsTap(address: string, sink: EventSink, buffer?: EventsBuffer): EventSink;
|
|
76
|
+
/**
|
|
77
|
+
* A copy of the record with every string in `body` and `attrs` run through the redactor, except the
|
|
78
|
+
* join-id attrs in `UNSCRUBBED_ATTRS`. A string attr the redactor cannot produce is left out; such a
|
|
79
|
+
* `body` becomes `""`.
|
|
80
|
+
*/
|
|
81
|
+
export declare function scrubEventRecord(record: EventRecord, redactor: PiiRedactor): Promise<EventRecord>;
|
|
82
|
+
/**
|
|
83
|
+
* The events part of the subscribe reply. A cursor whose `afterEpoch` is `epoch` selects the held
|
|
84
|
+
* records after `afterSeq`; any other cursor, or none, selects every held record. The newest
|
|
85
|
+
* selected records that fit `maxBytes` are served, scrubbed, in `seq` order. `truncated` is `true`
|
|
86
|
+
* when a record owed to the cursor was evicted, or a selected record was not served. A cursor of
|
|
87
|
+
* the current epoch is owed the records after `afterSeq`; a cursor of another epoch is owed every
|
|
88
|
+
* record of the current epoch; no cursor is owed nothing. Without a redactor no record is served.
|
|
89
|
+
*/
|
|
90
|
+
export declare function buildEventsReplay(input: {
|
|
91
|
+
buffer?: EventsBuffer;
|
|
92
|
+
epoch?: string;
|
|
93
|
+
cursor?: EventsCursor;
|
|
94
|
+
redactor: PiiRedactor | null;
|
|
95
|
+
maxBytes?: number;
|
|
96
|
+
}): Promise<EventsReplay>;
|
|
97
|
+
export interface EventsPublisher {
|
|
98
|
+
/** Sends every interval. A second call while running does nothing. */
|
|
99
|
+
start(): void;
|
|
100
|
+
/** Stops the sends. */
|
|
101
|
+
stop(): void;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Each tick takes the records added since the previous tick. With a broadcast function, it scrubs
|
|
105
|
+
* them and broadcasts them as one or more messages of at most `maxFrameBytes` serialized bytes. A
|
|
106
|
+
* record that does not fit an empty message is dropped and passed to `warn`. Without a broadcast
|
|
107
|
+
* function, the records are not sent. While there is no redactor, the tick sends nothing and keeps
|
|
108
|
+
* the records for the first tick that has one. Each tick ends with `dropSent`: it deletes the closed
|
|
109
|
+
* rings whose records it has passed, and the closed rings held past `EVENTS_CLOSED_RING_TTL_MS`.
|
|
110
|
+
*/
|
|
111
|
+
export declare function createEventsPublisher(deps: {
|
|
112
|
+
getBroadcast: () => StrategiesBroadcastFn | undefined;
|
|
113
|
+
buffer?: EventsBuffer;
|
|
114
|
+
getRedactor?: () => PiiRedactor | null;
|
|
115
|
+
epoch?: string;
|
|
116
|
+
maxFrameBytes?: number;
|
|
117
|
+
warn?: (message: string, meta: Record<string, unknown>) => void;
|
|
118
|
+
onError?: (err: unknown) => void;
|
|
119
|
+
}): EventsPublisher;
|
|
120
|
+
//# sourceMappingURL=events-telemetry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events-telemetry.d.ts","sourceRoot":"","sources":["../../src/runtime/events-telemetry.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAYH,OAAO,EAA4B,KAAK,SAAS,EAAE,KAAK,WAAW,EAAE,MAAM,6BAA6B,CAAC;AACzG,OAAO,EAAyB,KAAK,WAAW,EAAE,MAAM,kCAAkC,CAAC;AAE3F,OAAO,EAAgB,KAAK,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAiB,KAAK,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAItF,qFAAqF;AACrF,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,EAAE,EA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}
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Events telemetry: the in-memory buffer of runtime event records, the tap that fills it from the
|
|
3
|
+
* per-wallet event sink, and the loop that broadcasts new records as `plugin.senpi.events`.
|
|
4
|
+
*/
|
|
5
|
+
import { EVENTS_BROADCAST_NAMES, EVENTS_CLOSED_RING_TTL_MS, EVENTS_EVENT_NAME, EVENTS_MAX_BODY_CHARS, EVENTS_MAX_FRAME_BYTES, EVENTS_PER_STRATEGY, EVENTS_SEND_INTERVAL_MS, } from "../constants/telemetry.js";
|
|
6
|
+
import { capValue } from "../telemetry/attributes.js";
|
|
7
|
+
import { normalizeStrategyAddress } from "../telemetry/event-store.js";
|
|
8
|
+
import { getDefaultPiiRedactor } from "../telemetry/spans/redact-pii.js";
|
|
9
|
+
import { JOIN_ATTR } from "../utils/event-catalog.js";
|
|
10
|
+
import { createLogger } from "../utils/logger.js";
|
|
11
|
+
import { PROCESS_EPOCH } from "./strategies-telemetry.js";
|
|
12
|
+
const logger = createLogger("[runtime][events-telemetry]");
|
|
13
|
+
const BROADCAST_NAMES = new Set(EVENTS_BROADCAST_NAMES);
|
|
14
|
+
/**
|
|
15
|
+
* One ring of `capacity` records per address. `seq` starts at 1 and counts across every address.
|
|
16
|
+
* `now` is the clock, in epoch ms, that times a closed ring.
|
|
17
|
+
*/
|
|
18
|
+
export function createEventsBuffer(capacity = EVENTS_PER_STRATEGY, now = () => Date.now()) {
|
|
19
|
+
const rings = new Map();
|
|
20
|
+
// Rings closed by `remove`, each with the time it was closed. Kept apart from `rings` so a new
|
|
21
|
+
// ring for the same address never mixes with them.
|
|
22
|
+
const closed = new Map();
|
|
23
|
+
let seq = 0;
|
|
24
|
+
let maxEvicted = 0;
|
|
25
|
+
return {
|
|
26
|
+
add(address, event) {
|
|
27
|
+
const key = normalizeStrategyAddress(address);
|
|
28
|
+
const attrs = {};
|
|
29
|
+
for (const [k, v] of Object.entries(event.attrs)) {
|
|
30
|
+
if (typeof v === "string" || typeof v === "boolean" || (typeof v === "number" && Number.isFinite(v)))
|
|
31
|
+
attrs[k] = v;
|
|
32
|
+
}
|
|
33
|
+
let body = capValue(event.body, EVENTS_MAX_BODY_CHARS);
|
|
34
|
+
// A truncated body is a slice of the original string, and V8 keeps the whole original alive
|
|
35
|
+
// behind a slice. A JSON round trip stores an exact, flat copy of the capped length only.
|
|
36
|
+
if (body !== event.body)
|
|
37
|
+
body = JSON.parse(JSON.stringify(body));
|
|
38
|
+
let ring = rings.get(key);
|
|
39
|
+
if (ring === undefined) {
|
|
40
|
+
ring = [];
|
|
41
|
+
rings.set(key, ring);
|
|
42
|
+
}
|
|
43
|
+
ring.push({ seq: ++seq, ts: event.ts, name: event.name, level: event.level, address: key, body, attrs });
|
|
44
|
+
if (ring.length > capacity)
|
|
45
|
+
maxEvicted = Math.max(maxEvicted, ring.shift().seq);
|
|
46
|
+
},
|
|
47
|
+
after(after) {
|
|
48
|
+
const out = [];
|
|
49
|
+
const collect = (ring) => {
|
|
50
|
+
let i = ring.length;
|
|
51
|
+
while (i > 0 && ring[i - 1].seq > after)
|
|
52
|
+
i--;
|
|
53
|
+
for (; i < ring.length; i++)
|
|
54
|
+
out.push(ring[i]);
|
|
55
|
+
};
|
|
56
|
+
for (const ring of rings.values())
|
|
57
|
+
collect(ring);
|
|
58
|
+
for (const ring of closed.keys())
|
|
59
|
+
collect(ring);
|
|
60
|
+
return out.sort((a, b) => a.seq - b.seq);
|
|
61
|
+
},
|
|
62
|
+
newestSeq() {
|
|
63
|
+
let newest = 0;
|
|
64
|
+
for (const ring of rings.values()) {
|
|
65
|
+
if (ring.length > 0)
|
|
66
|
+
newest = Math.max(newest, ring[ring.length - 1].seq);
|
|
67
|
+
}
|
|
68
|
+
// A closed ring is never empty.
|
|
69
|
+
for (const ring of closed.keys())
|
|
70
|
+
newest = Math.max(newest, ring[ring.length - 1].seq);
|
|
71
|
+
return newest;
|
|
72
|
+
},
|
|
73
|
+
maxEvictedSeq: () => maxEvicted,
|
|
74
|
+
remove(address) {
|
|
75
|
+
const key = normalizeStrategyAddress(address);
|
|
76
|
+
const ring = rings.get(key);
|
|
77
|
+
if (ring === undefined)
|
|
78
|
+
return;
|
|
79
|
+
rings.delete(key);
|
|
80
|
+
if (ring.length > 0)
|
|
81
|
+
closed.set(ring, now());
|
|
82
|
+
},
|
|
83
|
+
dropSent(sentSeq) {
|
|
84
|
+
const expiredAt = now() - EVENTS_CLOSED_RING_TTL_MS;
|
|
85
|
+
for (const [ring, closedAt] of closed) {
|
|
86
|
+
const newest = ring[ring.length - 1].seq;
|
|
87
|
+
if (newest > sentSeq && closedAt > expiredAt)
|
|
88
|
+
continue;
|
|
89
|
+
closed.delete(ring);
|
|
90
|
+
maxEvicted = Math.max(maxEvicted, newest);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** The process buffer. Its `seq` run belongs to `PROCESS_EPOCH`. */
|
|
96
|
+
export const eventsBuffer = createEventsBuffer();
|
|
97
|
+
/**
|
|
98
|
+
* Adds a record to `buffer` under `address` for a case that is not a runtime event. It does not go
|
|
99
|
+
* through `logger.event`, so no event sink, disk ring or OTel record receives it. A blank `address`
|
|
100
|
+
* adds nothing. Never throws.
|
|
101
|
+
*/
|
|
102
|
+
export function addEventRecord(address, event, buffer = eventsBuffer) {
|
|
103
|
+
try {
|
|
104
|
+
if (normalizeStrategyAddress(address) !== "")
|
|
105
|
+
buffer.add(address, event);
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
// Callers are request handlers; a buffer failure must not fail the request.
|
|
109
|
+
logger.debug("addEventRecord failed", { address, name: event.name, err });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Wraps a wallet's event sink. Every event goes to `sink`. An event whose name is in
|
|
114
|
+
* `EVENTS_BROADCAST_NAMES` is also added to `buffer` under `address`.
|
|
115
|
+
*/
|
|
116
|
+
export function withEventsTap(address, sink, buffer = eventsBuffer) {
|
|
117
|
+
return {
|
|
118
|
+
append(event) {
|
|
119
|
+
sink.append(event);
|
|
120
|
+
try {
|
|
121
|
+
if (BROADCAST_NAMES.has(event.name))
|
|
122
|
+
buffer.add(address, event);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// The tap runs inside `logger.event` and must not throw into it.
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
flush: () => sink.flush(),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/** The redacted value, or `undefined` when the redactor throws. `""` is kept as is. */
|
|
132
|
+
async function scrubString(value, redactor) {
|
|
133
|
+
if (value === "")
|
|
134
|
+
return value;
|
|
135
|
+
try {
|
|
136
|
+
const out = await redactor.redact(value);
|
|
137
|
+
return typeof out === "string" ? out : undefined;
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/** Join-id attrs. They hold ids, never free text, and are sent as they are. */
|
|
144
|
+
const UNSCRUBBED_ATTRS = new Set([
|
|
145
|
+
...Object.values(JOIN_ATTR),
|
|
146
|
+
"senpi.strategy.address",
|
|
147
|
+
"senpi.scanner.id",
|
|
148
|
+
]);
|
|
149
|
+
/**
|
|
150
|
+
* A copy of the record with every string in `body` and `attrs` run through the redactor, except the
|
|
151
|
+
* join-id attrs in `UNSCRUBBED_ATTRS`. A string attr the redactor cannot produce is left out; such a
|
|
152
|
+
* `body` becomes `""`.
|
|
153
|
+
*/
|
|
154
|
+
export async function scrubEventRecord(record, redactor) {
|
|
155
|
+
const attrs = {};
|
|
156
|
+
for (const [key, value] of Object.entries(record.attrs)) {
|
|
157
|
+
if (typeof value !== "string" || UNSCRUBBED_ATTRS.has(key)) {
|
|
158
|
+
attrs[key] = value;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const scrubbed = await scrubString(value, redactor);
|
|
162
|
+
if (scrubbed !== undefined)
|
|
163
|
+
attrs[key] = scrubbed;
|
|
164
|
+
}
|
|
165
|
+
return { ...record, body: (await scrubString(record.body, redactor)) ?? "", attrs };
|
|
166
|
+
}
|
|
167
|
+
const bytes = (value) => Buffer.byteLength(JSON.stringify(value), "utf8");
|
|
168
|
+
/**
|
|
169
|
+
* Fills one frame of records: a JSON envelope of `envelopeBytes` whose `events` array holds the
|
|
170
|
+
* records, at most `maxBytes` serialized in total. `offer` answers `"added"`, `"full"` (the record
|
|
171
|
+
* does not fit what is already in the frame; it is not added) or `"oversize"` (the record does not
|
|
172
|
+
* fit an empty frame; it is not added). `take` returns the frame and starts an empty one.
|
|
173
|
+
*/
|
|
174
|
+
function createFramePacker(envelopeBytes, maxBytes) {
|
|
175
|
+
let frame = [];
|
|
176
|
+
let used = envelopeBytes;
|
|
177
|
+
return {
|
|
178
|
+
offer(record) {
|
|
179
|
+
const size = bytes(record);
|
|
180
|
+
if (envelopeBytes + size > maxBytes)
|
|
181
|
+
return { result: "oversize", bytes: size };
|
|
182
|
+
// One comma between two records.
|
|
183
|
+
const added = size + (frame.length > 0 ? 1 : 0);
|
|
184
|
+
if (used + added > maxBytes)
|
|
185
|
+
return { result: "full", bytes: size };
|
|
186
|
+
used += added;
|
|
187
|
+
frame.push(record);
|
|
188
|
+
return { result: "added", bytes: size };
|
|
189
|
+
},
|
|
190
|
+
take() {
|
|
191
|
+
const out = frame;
|
|
192
|
+
frame = [];
|
|
193
|
+
used = envelopeBytes;
|
|
194
|
+
return out;
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* The events part of the subscribe reply. A cursor whose `afterEpoch` is `epoch` selects the held
|
|
200
|
+
* records after `afterSeq`; any other cursor, or none, selects every held record. The newest
|
|
201
|
+
* selected records that fit `maxBytes` are served, scrubbed, in `seq` order. `truncated` is `true`
|
|
202
|
+
* when a record owed to the cursor was evicted, or a selected record was not served. A cursor of
|
|
203
|
+
* the current epoch is owed the records after `afterSeq`; a cursor of another epoch is owed every
|
|
204
|
+
* record of the current epoch; no cursor is owed nothing. Without a redactor no record is served.
|
|
205
|
+
*/
|
|
206
|
+
export async function buildEventsReplay(input) {
|
|
207
|
+
const buffer = input.buffer ?? eventsBuffer;
|
|
208
|
+
const epoch = input.epoch ?? PROCESS_EPOCH;
|
|
209
|
+
const maxBytes = input.maxBytes ?? EVENTS_MAX_FRAME_BYTES;
|
|
210
|
+
const redactor = input.redactor;
|
|
211
|
+
const afterSeq = input.cursor?.afterEpoch === epoch ? input.cursor.afterSeq : undefined;
|
|
212
|
+
let truncated = input.cursor !== undefined && buffer.maxEvictedSeq() > (afterSeq ?? 0);
|
|
213
|
+
if (redactor === null)
|
|
214
|
+
return { epoch, events: [], truncated: truncated || buffer.newestSeq() > (afterSeq ?? 0) };
|
|
215
|
+
const selected = buffer.after(afterSeq ?? 0);
|
|
216
|
+
const packer = createFramePacker(bytes({ epoch, events: [], truncated: false }), maxBytes);
|
|
217
|
+
// Newest first, one record at a time, until the frame is full.
|
|
218
|
+
for (let i = selected.length - 1; i >= 0; i--) {
|
|
219
|
+
const { result } = packer.offer(await scrubEventRecord(selected[i], redactor));
|
|
220
|
+
if (result === "added")
|
|
221
|
+
continue;
|
|
222
|
+
truncated = true;
|
|
223
|
+
// An oversize record is skipped and the older records are still served. A full reply ends here.
|
|
224
|
+
if (result === "full")
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
return { epoch, events: packer.take().reverse(), truncated };
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Each tick takes the records added since the previous tick. With a broadcast function, it scrubs
|
|
231
|
+
* them and broadcasts them as one or more messages of at most `maxFrameBytes` serialized bytes. A
|
|
232
|
+
* record that does not fit an empty message is dropped and passed to `warn`. Without a broadcast
|
|
233
|
+
* function, the records are not sent. While there is no redactor, the tick sends nothing and keeps
|
|
234
|
+
* the records for the first tick that has one. Each tick ends with `dropSent`: it deletes the closed
|
|
235
|
+
* rings whose records it has passed, and the closed rings held past `EVENTS_CLOSED_RING_TTL_MS`.
|
|
236
|
+
*/
|
|
237
|
+
export function createEventsPublisher(deps) {
|
|
238
|
+
const buffer = deps.buffer ?? eventsBuffer;
|
|
239
|
+
const getRedactor = deps.getRedactor ?? getDefaultPiiRedactor;
|
|
240
|
+
const epoch = deps.epoch ?? PROCESS_EPOCH;
|
|
241
|
+
const maxFrameBytes = deps.maxFrameBytes ?? EVENTS_MAX_FRAME_BYTES;
|
|
242
|
+
// Upper bound of the message without its records.
|
|
243
|
+
const envelopeBytes = bytes({ v: 1, epoch, batchSeq: Number.MAX_SAFE_INTEGER, events: [] });
|
|
244
|
+
let lastSeq = 0;
|
|
245
|
+
let batchSeq = 0;
|
|
246
|
+
let timer;
|
|
247
|
+
let inFlight = false;
|
|
248
|
+
const send = (broadcast, events) => {
|
|
249
|
+
const message = { v: 1, epoch, batchSeq: ++batchSeq, events };
|
|
250
|
+
broadcast(EVENTS_EVENT_NAME, message, { dropIfSlow: true });
|
|
251
|
+
};
|
|
252
|
+
const tick = async () => {
|
|
253
|
+
if (inFlight)
|
|
254
|
+
return;
|
|
255
|
+
inFlight = true;
|
|
256
|
+
try {
|
|
257
|
+
if (deps.getBroadcast() === undefined) {
|
|
258
|
+
lastSeq = Math.max(lastSeq, buffer.newestSeq());
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const redactor = getRedactor();
|
|
262
|
+
if (redactor === null)
|
|
263
|
+
return;
|
|
264
|
+
const records = buffer.after(lastSeq);
|
|
265
|
+
if (records.length === 0)
|
|
266
|
+
return;
|
|
267
|
+
lastSeq = records[records.length - 1].seq;
|
|
268
|
+
const scrubbed = await Promise.all(records.map((record) => scrubEventRecord(record, redactor)));
|
|
269
|
+
const broadcast = deps.getBroadcast();
|
|
270
|
+
if (broadcast === undefined || timer === undefined)
|
|
271
|
+
return;
|
|
272
|
+
const packer = createFramePacker(envelopeBytes, maxFrameBytes);
|
|
273
|
+
for (const record of scrubbed) {
|
|
274
|
+
let offered = packer.offer(record);
|
|
275
|
+
if (offered.result === "full") {
|
|
276
|
+
send(broadcast, packer.take());
|
|
277
|
+
offered = packer.offer(record);
|
|
278
|
+
}
|
|
279
|
+
if (offered.result === "oversize") {
|
|
280
|
+
deps.warn?.("[senpi-runtime] event record exceeds the events frame size; dropped", {
|
|
281
|
+
seq: record.seq,
|
|
282
|
+
name: record.name,
|
|
283
|
+
address: record.address,
|
|
284
|
+
bytes: offered.bytes,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const last = packer.take();
|
|
289
|
+
if (last.length > 0)
|
|
290
|
+
send(broadcast, last);
|
|
291
|
+
}
|
|
292
|
+
catch (err) {
|
|
293
|
+
deps.onError?.(err);
|
|
294
|
+
}
|
|
295
|
+
finally {
|
|
296
|
+
buffer.dropSent(lastSeq);
|
|
297
|
+
inFlight = false;
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
return {
|
|
301
|
+
start() {
|
|
302
|
+
if (timer !== undefined)
|
|
303
|
+
return;
|
|
304
|
+
timer = setInterval(() => void tick(), EVENTS_SEND_INTERVAL_MS);
|
|
305
|
+
timer.unref?.();
|
|
306
|
+
},
|
|
307
|
+
stop() {
|
|
308
|
+
if (timer !== undefined)
|
|
309
|
+
clearInterval(timer);
|
|
310
|
+
timer = undefined;
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
//# sourceMappingURL=events-telemetry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events-telemetry.js","sourceRoot":"","sources":["../../src/runtime/events-telemetry.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,sBAAsB,EACtB,yBAAyB,EACzB,iBAAiB,EACjB,qBAAqB,EACrB,sBAAsB,EACtB,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAE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oBAC/B,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;gBACjC,CAAC;gBACD,IAAI,OAAO,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;oBAClC,IAAI,CAAC,IAAI,EAAE,CAAC,qEAAqE,EAAE;wBACjF,GAAG,EAAE,MAAM,CAAC,GAAG;wBACf,IAAI,EAAE,MAAM,CAAC,IAAI;wBACjB,OAAO,EAAE,MAAM,CAAC,OAAO;wBACvB,KAAK,EAAE,OAAO,CAAC,KAAK;qBACrB,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;YACD,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;YAC3B,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;gBAAE,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QAC7C,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC;QACtB,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;YACzB,QAAQ,GAAG,KAAK,CAAC;QACnB,CAAC;IACH,CAAC,CAAC;IAEF,OAAO;QACL,KAAK;YACH,IAAI,KAAK,KAAK,SAAS;gBAAE,OAAO;YAChC,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,EAAE,EAAE,uBAAuB,CAAC,CAAC;YAChE,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;QAClB,CAAC;QACD,IAAI;YACF,IAAI,KAAK,KAAK,SAAS;gBAAE,aAAa,CAAC,KAAK,CAAC,CAAC;YAC9C,KAAK,GAAG,SAAS,CAAC;QACpB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -10,9 +10,16 @@
|
|
|
10
10
|
* If any risk API call fails, the gate is CLOSED (fail-closed).
|
|
11
11
|
*
|
|
12
12
|
* Evaluation is stateless, but enforcement of the halt-class gates (daily loss, drawdown) is
|
|
13
|
-
* not: each of
|
|
13
|
+
* not: each live evaluation of them passes through the lifecycle tracker's `applyLatch`, which
|
|
14
14
|
* keeps a genuinely-breached halt CLOSED until its release rule (UTC day rollover) fires, so a
|
|
15
|
-
* mark-to-market bounce back under the limit cannot reopen entries mid-day.
|
|
15
|
+
* mark-to-market bounce back under the limit cannot reopen entries mid-day. Setting or releasing
|
|
16
|
+
* a latch there is what pauses or resumes the strategy for these two gates.
|
|
17
|
+
*
|
|
18
|
+
* A per-signal check skips the PnL snapshot fetch while the first snapshot-using halt gate (daily
|
|
19
|
+
* loss when configured, else drawdown) holds a latch that is active by the box clock. It returns
|
|
20
|
+
* that gate's latched block from `activeLatchBlock`, which never sets or releases a latch. When
|
|
21
|
+
* daily loss is configured and not latched, the snapshot is fetched as usual. `evaluateAll`
|
|
22
|
+
* (status and health) always fetches.
|
|
16
23
|
*
|
|
17
24
|
* Supports two modes:
|
|
18
25
|
* - Default (short-circuit): returns on first non-OPEN gate.
|
|
@@ -40,9 +47,9 @@ export interface RiskGuardConfig {
|
|
|
40
47
|
auditLog?: RiskAuditLog;
|
|
41
48
|
/**
|
|
42
49
|
* Optional strategy-lifecycle tracker. RiskGuard is stateless and only reports; the
|
|
43
|
-
* lifecycle component owns the self-pause state
|
|
44
|
-
*
|
|
45
|
-
* call sites/tests don't break.
|
|
50
|
+
* lifecycle component owns the self-pause state and fires the hook event + user notification
|
|
51
|
+
* on a pause: from `observe` for the non-latched gates, from `applyLatch` for the latched ones.
|
|
52
|
+
* Optional so existing call sites/tests don't break.
|
|
46
53
|
*/
|
|
47
54
|
lifecycleTracker?: {
|
|
48
55
|
observe(address: string, label: string, evaluations: RiskGateEvaluation[], source: RiskSource): void;
|
|
@@ -52,7 +59,15 @@ export interface RiskGuardConfig {
|
|
|
52
59
|
*/
|
|
53
60
|
applyLatch?(address: string, evaluation: RiskGateEvaluation, opts: {
|
|
54
61
|
releaseOnDayRollover: boolean;
|
|
62
|
+
label?: string;
|
|
55
63
|
}): Promise<RiskGateEvaluation>;
|
|
64
|
+
/**
|
|
65
|
+
* Active-latch lookup (see StrategyLifecycle.activeLatchBlock). Optional for the same reason;
|
|
66
|
+
* without it the per-signal check always runs the halt gates live.
|
|
67
|
+
*/
|
|
68
|
+
activeLatchBlock?(address: string, baseline: RiskGateEvaluation, opts: {
|
|
69
|
+
releaseOnDayRollover: boolean;
|
|
70
|
+
}): Promise<RiskGateEvaluation | null>;
|
|
56
71
|
};
|
|
57
72
|
}
|
|
58
73
|
export declare function createRiskGuard(config: RiskGuardConfig): IRiskGuard;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"risk-guard.d.ts","sourceRoot":"","sources":["../../src/runtime/risk-guard.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"risk-guard.d.ts","sourceRoot":"","sources":["../../src/runtime/risk-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,KAAK,EAAmE,SAAS,IAAI,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACnI,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAiB,MAAM,4BAA4B,CAAC;AACjF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,KAAK,EAAE,kBAAkB,EAAE,UAAU,EAAsB,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAIzG,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAe9D;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,eAAe,GAAG,SAAS,GAAG,GAAG,CAAC,UAAU,CAAC,CAM5F;AAED,wBAAgB,mBAAmB,IAAI,UAAU,CAiBhD;AAED,MAAM,WAAW,eAAe;I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