@av-pi-studio/server 0.0.115 → 0.0.117
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 +8 -8
- package/dist/agent/index.d.ts +0 -1
- package/dist/agent/index.js +0 -1
- package/dist/agent/mcp-server.d.ts +3 -13
- package/dist/agent/mcp-server.js +2 -10
- package/dist/agent/provider-contract.d.ts +0 -7
- package/dist/agent/providers/mock/mock-provider.d.ts +12 -3
- package/dist/agent/providers/mock/mock-provider.js +46 -7
- package/dist/agent/providers/mock/stream-script.d.ts +28 -0
- package/dist/agent/providers/mock/stream-script.js +57 -0
- package/dist/agent/providers/pi/agent.js +0 -7
- package/dist/daemon/bootstrap.d.ts +18 -2
- package/dist/daemon/bootstrap.js +63 -13
- package/dist/daemon/dev-bootstrap.d.ts +7 -0
- package/dist/daemon/dev-bootstrap.js +34 -5
- package/dist/daemon/dev-main.js +5 -2
- package/dist/daemon/loopback.d.ts +4 -0
- package/dist/daemon/loopback.js +12 -0
- package/dist/daemon/main.js +4 -10
- package/dist/extensions/curated-packs.d.ts +0 -1
- package/dist/extensions/curated-packs.js +1 -6
- package/dist/extensions/sync-executor.js +1 -1
- package/dist/logging/index.d.ts +1 -0
- package/dist/logging/index.js +1 -0
- package/dist/logging/log-tap.d.ts +33 -0
- package/dist/logging/log-tap.js +233 -0
- package/dist/logging/logger.d.ts +6 -0
- package/dist/logging/logger.js +4 -0
- package/dist/observability/daemon-logs-rpc.d.ts +12 -0
- package/dist/observability/daemon-logs-rpc.js +17 -0
- package/dist/observability/daemon-logs-service.d.ts +67 -0
- package/dist/observability/daemon-logs-service.js +165 -0
- package/dist/observability/daemon-status-rpc.d.ts +12 -0
- package/dist/observability/daemon-status-rpc.js +21 -0
- package/dist/observability/daemon-status-service.d.ts +85 -0
- package/dist/observability/daemon-status-service.js +154 -0
- package/dist/test-support/auth-gated-rpc.d.ts +18 -0
- package/dist/test-support/auth-gated-rpc.js +80 -0
- package/dist/test-support/daemon-test-client.d.ts +43 -0
- package/dist/test-support/daemon-test-client.js +91 -0
- package/dist/test-support/external-fixtures-scenario.d.ts +30 -0
- package/dist/test-support/external-fixtures-scenario.js +223 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +7 -0
- package/dist/ws/session.d.ts +11 -0
- package/dist/ws/session.js +11 -0
- package/dist/ws/ws-server.js +2 -0
- package/package.json +3 -3
- package/dist/agent/permissions.d.ts +0 -71
- package/dist/agent/permissions.js +0 -132
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { Writable } from "node:stream";
|
|
2
|
+
/**
|
|
3
|
+
* `LogTap` — a pino multistream destination that mirrors every line the daemon logger writes into
|
|
4
|
+
* a bounded, sanitised, size-capped ring and fans it out to live listeners
|
|
5
|
+
* (swe/features/daemon-log-streaming.md). It only ever receives a copy: stdout and the rotating
|
|
6
|
+
* file are separate multistream entries and stay byte-for-byte untouched.
|
|
7
|
+
*
|
|
8
|
+
* Invariants: the tap never throws into pino; the ring never holds a line over
|
|
9
|
+
* `LOG_LINE_MAX_BYTES`; redaction/truncation happen once, here, at tap time.
|
|
10
|
+
*/
|
|
11
|
+
export const LOG_RING_CAPACITY = 1000;
|
|
12
|
+
export const LOG_LINE_MAX_BYTES = 8192;
|
|
13
|
+
const MSG_MAX_CHARS = 2048;
|
|
14
|
+
const FIELD_STRING_MAX_CHARS = 1024;
|
|
15
|
+
const STACK_MAX_CHARS = 2048;
|
|
16
|
+
const MAX_DEPTH = 4;
|
|
17
|
+
const REDACTED = "[redacted]";
|
|
18
|
+
const TRUNCATED_NODE = "[truncated]";
|
|
19
|
+
/** Pino's numeric levels. */
|
|
20
|
+
export const LOG_LEVEL_NUMBERS = {
|
|
21
|
+
trace: 10,
|
|
22
|
+
debug: 20,
|
|
23
|
+
info: 30,
|
|
24
|
+
warn: 40,
|
|
25
|
+
error: 50,
|
|
26
|
+
fatal: 60,
|
|
27
|
+
};
|
|
28
|
+
const LEVEL_NAMES = new Map(Object.entries(LOG_LEVEL_NUMBERS).map(([name, num]) => [
|
|
29
|
+
num,
|
|
30
|
+
name,
|
|
31
|
+
]));
|
|
32
|
+
/** Keys stripped from `LogLine.fields`. */
|
|
33
|
+
const CORE_KEYS = new Set(["level", "time", "msg", "pid", "hostname", "v"]);
|
|
34
|
+
/**
|
|
35
|
+
* Whole key names (lower-cased) whose values are redacted. Whole-name matching deliberately
|
|
36
|
+
* spares usage counters such as `tokens` / `inputTokens`.
|
|
37
|
+
*/
|
|
38
|
+
const SENSITIVE_KEYS = new Set([
|
|
39
|
+
"password",
|
|
40
|
+
"passwd",
|
|
41
|
+
"secret",
|
|
42
|
+
"token",
|
|
43
|
+
"authorization",
|
|
44
|
+
"cookie",
|
|
45
|
+
"apikey",
|
|
46
|
+
"api_key",
|
|
47
|
+
"bearer",
|
|
48
|
+
"privatekey",
|
|
49
|
+
"secretkey",
|
|
50
|
+
]);
|
|
51
|
+
const BEARER_PATTERN = /pi-studio\.bearer\.[^\s"']+/g;
|
|
52
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
53
|
+
const maskBearer = (s) => s.replace(BEARER_PATTERN, "pi-studio.bearer.[redacted]");
|
|
54
|
+
const cut = (s, max) => (s.length > max ? s.slice(0, max) : s);
|
|
55
|
+
/** Redact sensitive keys and mask bearer tokens in strings, recursively. Mutates `node`. */
|
|
56
|
+
function sanitize(node) {
|
|
57
|
+
if (typeof node === "string")
|
|
58
|
+
return maskBearer(node);
|
|
59
|
+
if (Array.isArray(node)) {
|
|
60
|
+
for (let i = 0; i < node.length; i++)
|
|
61
|
+
node[i] = sanitize(node[i]);
|
|
62
|
+
return node;
|
|
63
|
+
}
|
|
64
|
+
if (isObject(node)) {
|
|
65
|
+
for (const key of Object.keys(node)) {
|
|
66
|
+
node[key] = SENSITIVE_KEYS.has(key.toLowerCase()) ? REDACTED : sanitize(node[key]);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return node;
|
|
70
|
+
}
|
|
71
|
+
/** Shorten strings and cap nesting depth. Returns a new value. */
|
|
72
|
+
function shrink(node, depth, inErr) {
|
|
73
|
+
if (typeof node === "string")
|
|
74
|
+
return cut(node, FIELD_STRING_MAX_CHARS);
|
|
75
|
+
if (Array.isArray(node)) {
|
|
76
|
+
if (depth > MAX_DEPTH)
|
|
77
|
+
return TRUNCATED_NODE;
|
|
78
|
+
return node.map((v) => shrink(v, depth + 1, false));
|
|
79
|
+
}
|
|
80
|
+
if (isObject(node)) {
|
|
81
|
+
if (depth > MAX_DEPTH)
|
|
82
|
+
return TRUNCATED_NODE;
|
|
83
|
+
const out = {};
|
|
84
|
+
for (const [k, v] of Object.entries(node)) {
|
|
85
|
+
out[k] =
|
|
86
|
+
inErr && k === "stack" && typeof v === "string"
|
|
87
|
+
? cut(v, STACK_MAX_CHARS)
|
|
88
|
+
: shrink(v, depth + 1, k === "err" && depth === 0);
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
return node;
|
|
93
|
+
}
|
|
94
|
+
function truncateLine(obj) {
|
|
95
|
+
const out = {};
|
|
96
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
97
|
+
if (k === "msg" && typeof v === "string")
|
|
98
|
+
out[k] = cut(v, MSG_MAX_CHARS);
|
|
99
|
+
else if (CORE_KEYS.has(k))
|
|
100
|
+
out[k] = v;
|
|
101
|
+
else
|
|
102
|
+
out[k] = shrink(v, 1, k === "err");
|
|
103
|
+
}
|
|
104
|
+
out.truncated = true;
|
|
105
|
+
if (Buffer.byteLength(JSON.stringify(out)) <= LOG_LINE_MAX_BYTES)
|
|
106
|
+
return out;
|
|
107
|
+
// Pathologically wide line (thousands of small fields): keep only the core keys.
|
|
108
|
+
const core = {};
|
|
109
|
+
for (const k of CORE_KEYS)
|
|
110
|
+
if (k in out)
|
|
111
|
+
core[k] = out[k];
|
|
112
|
+
core.truncated = true;
|
|
113
|
+
return core;
|
|
114
|
+
}
|
|
115
|
+
export class LogTap {
|
|
116
|
+
stream;
|
|
117
|
+
#ring = Array.from({
|
|
118
|
+
length: LOG_RING_CAPACITY,
|
|
119
|
+
});
|
|
120
|
+
#next = 0;
|
|
121
|
+
#size = 0;
|
|
122
|
+
#listeners = new Set();
|
|
123
|
+
#effectiveLevel = "silent";
|
|
124
|
+
constructor() {
|
|
125
|
+
this.stream = new Writable({
|
|
126
|
+
decodeStrings: false,
|
|
127
|
+
write: (chunk, _enc, cb) => {
|
|
128
|
+
try {
|
|
129
|
+
this.#ingest(typeof chunk === "string" ? chunk : chunk.toString("utf8"));
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
// The tap must never push an error back into pino.
|
|
133
|
+
}
|
|
134
|
+
cb();
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/** The logger level the tap was attached at, or `"silent"` while unattached. */
|
|
139
|
+
get effectiveLevel() {
|
|
140
|
+
return this.#effectiveLevel;
|
|
141
|
+
}
|
|
142
|
+
/** Called by `createLogger` when it pushes the tap's stream. */
|
|
143
|
+
attach(level) {
|
|
144
|
+
this.#effectiveLevel = level;
|
|
145
|
+
}
|
|
146
|
+
/** Register a live listener; returns its remover. Listener errors are swallowed. */
|
|
147
|
+
onLine(listener) {
|
|
148
|
+
this.#listeners.add(listener);
|
|
149
|
+
return () => {
|
|
150
|
+
this.#listeners.delete(listener);
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** The newest `n` lines at or above `minLevelNum`, oldest first. */
|
|
154
|
+
backlog(minLevelNum, n) {
|
|
155
|
+
const out = [];
|
|
156
|
+
for (let i = 1; i <= this.#size && out.length < n; i++) {
|
|
157
|
+
const entry = this.#ring[(this.#next - i + LOG_RING_CAPACITY) % LOG_RING_CAPACITY];
|
|
158
|
+
if (entry && entry.level >= minLevelNum)
|
|
159
|
+
out.push(entry.raw);
|
|
160
|
+
}
|
|
161
|
+
return out.toReversed();
|
|
162
|
+
}
|
|
163
|
+
/** Project a ring/listener line to the wire shape. Never throws. */
|
|
164
|
+
toLogLine(raw) {
|
|
165
|
+
let parsed;
|
|
166
|
+
try {
|
|
167
|
+
parsed = JSON.parse(raw);
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return { time: Date.now(), level: "info", msg: cut(raw, MSG_MAX_CHARS) };
|
|
171
|
+
}
|
|
172
|
+
if (!isObject(parsed))
|
|
173
|
+
return { time: Date.now(), level: "info", msg: cut(raw, MSG_MAX_CHARS) };
|
|
174
|
+
const fields = {};
|
|
175
|
+
for (const [k, v] of Object.entries(parsed))
|
|
176
|
+
if (!CORE_KEYS.has(k))
|
|
177
|
+
fields[k] = v;
|
|
178
|
+
const level = typeof parsed.level === "number" ? (LEVEL_NAMES.get(parsed.level) ?? "info") : "info";
|
|
179
|
+
return {
|
|
180
|
+
time: typeof parsed.time === "number" ? parsed.time : Date.now(),
|
|
181
|
+
level,
|
|
182
|
+
msg: typeof parsed.msg === "string" ? parsed.msg : "",
|
|
183
|
+
...(Object.keys(fields).length > 0 ? { fields } : {}),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
#ingest(chunk) {
|
|
187
|
+
for (const part of chunk.split("\n")) {
|
|
188
|
+
if (part.trim().length === 0)
|
|
189
|
+
continue;
|
|
190
|
+
const entry = this.#normalise(part);
|
|
191
|
+
this.#ring[this.#next] = entry;
|
|
192
|
+
this.#next = (this.#next + 1) % LOG_RING_CAPACITY;
|
|
193
|
+
if (this.#size < LOG_RING_CAPACITY)
|
|
194
|
+
this.#size++;
|
|
195
|
+
for (const listener of this.#listeners) {
|
|
196
|
+
try {
|
|
197
|
+
listener(entry.raw, entry.level);
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
// A faulty listener must not affect pino or other listeners.
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
#normalise(part) {
|
|
206
|
+
let parsed;
|
|
207
|
+
try {
|
|
208
|
+
parsed = JSON.parse(part);
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
parsed = undefined;
|
|
212
|
+
}
|
|
213
|
+
if (!isObject(parsed)) {
|
|
214
|
+
return {
|
|
215
|
+
raw: JSON.stringify({
|
|
216
|
+
level: LOG_LEVEL_NUMBERS.info,
|
|
217
|
+
time: Date.now(),
|
|
218
|
+
msg: maskBearer(cut(part, MSG_MAX_CHARS)),
|
|
219
|
+
}),
|
|
220
|
+
level: LOG_LEVEL_NUMBERS.info,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
const level = typeof parsed.level === "number" ? parsed.level : LOG_LEVEL_NUMBERS.info;
|
|
224
|
+
let obj = sanitize(parsed);
|
|
225
|
+
let raw = JSON.stringify(obj);
|
|
226
|
+
if (Buffer.byteLength(raw) > LOG_LINE_MAX_BYTES) {
|
|
227
|
+
obj = truncateLine(obj);
|
|
228
|
+
raw = JSON.stringify(obj);
|
|
229
|
+
}
|
|
230
|
+
return { raw, level };
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
//# sourceMappingURL=log-tap.js.map
|
package/dist/logging/logger.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { LogTap } from "./log-tap.js";
|
|
1
2
|
/**
|
|
2
3
|
* Daemon logging (operational concern; adopted from the reference daemon's `pino` stack). The rest
|
|
3
4
|
* of the codebase injects a `Pick<Console, "info" | "warn" | "error">`-shaped logger; a pino logger
|
|
@@ -41,6 +42,11 @@ export interface CreateLoggerOptions {
|
|
|
41
42
|
bindings?: Record<string, unknown>;
|
|
42
43
|
/** Override the stdout stream (tests). Defaults to `process.stdout`. */
|
|
43
44
|
stdoutStream?: NodeJS.WritableStream;
|
|
45
|
+
/**
|
|
46
|
+
* Mirror every written line into a `LogTap` (live log streaming). Adds ONE extra multistream
|
|
47
|
+
* entry at the resolved level; stdout/file entries are untouched. Ignored when silent.
|
|
48
|
+
*/
|
|
49
|
+
tap?: LogTap;
|
|
44
50
|
}
|
|
45
51
|
/** Build a pino logger per the options. Pure/synchronous; safe to call in tests. */
|
|
46
52
|
export declare function createLogger(options?: CreateLoggerOptions): Logger;
|
package/dist/logging/logger.js
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { HandlerRegistry } from "../ws/router.js";
|
|
2
|
+
import type { DaemonLogsService } from "./daemon-logs-service.js";
|
|
3
|
+
/**
|
|
4
|
+
* Wires the `daemon_logs_*` request family (swe/features/daemon-log-streaming.md) onto
|
|
5
|
+
* `DaemonLogsService`. Invalid `minLevel`/`tail` are domain failures in `payload`, never
|
|
6
|
+
* `rpc_error`. `requestId` is stamped by the router. Registered in both bootstraps; advertised by the
|
|
7
|
+
* `daemonObservability` server feature.
|
|
8
|
+
*/
|
|
9
|
+
export declare function registerDaemonLogsHandlers(registry: HandlerRegistry, deps: {
|
|
10
|
+
service: DaemonLogsService;
|
|
11
|
+
}): void;
|
|
12
|
+
//# sourceMappingURL=daemon-logs-rpc.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wires the `daemon_logs_*` request family (swe/features/daemon-log-streaming.md) onto
|
|
3
|
+
* `DaemonLogsService`. Invalid `minLevel`/`tail` are domain failures in `payload`, never
|
|
4
|
+
* `rpc_error`. `requestId` is stamped by the router. Registered in both bootstraps; advertised by the
|
|
5
|
+
* `daemonObservability` server feature.
|
|
6
|
+
*/
|
|
7
|
+
export function registerDaemonLogsHandlers(registry, deps) {
|
|
8
|
+
registry.register("daemon_logs_subscribe_request", (ctx) => ({
|
|
9
|
+
type: "daemon_logs_subscribe_response",
|
|
10
|
+
payload: deps.service.subscribe(ctx.session, ctx.message.minLevel, ctx.message.tail),
|
|
11
|
+
}));
|
|
12
|
+
registry.register("daemon_logs_unsubscribe_request", (ctx) => {
|
|
13
|
+
deps.service.unsubscribe(ctx.session);
|
|
14
|
+
return { type: "daemon_logs_unsubscribe_response", payload: { ok: true } };
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=daemon-logs-rpc.js.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { LogLine } from "@av-pi-studio/protocol";
|
|
2
|
+
import { type LogTap } from "../logging/log-tap.js";
|
|
3
|
+
import type { LogLevel } from "../logging/logger.js";
|
|
4
|
+
import type { Session } from "../ws/session.js";
|
|
5
|
+
/**
|
|
6
|
+
* Per-session daemon log delivery (swe/features/daemon-log-streaming.md § Subscribe / deliver /
|
|
7
|
+
* cap). Each subscriber owns a level filter, a 200 lines/s token bucket (burst 200), a queue and a
|
|
8
|
+
* 100 ms flush timer that exists only while that subscriber has pending data. Overflow lines are
|
|
9
|
+
* counted in `dropped` (incremental, per subscriber) and never re-delivered. The ring and the log
|
|
10
|
+
* files are never subject to the cap.
|
|
11
|
+
*/
|
|
12
|
+
export declare const LOG_RATE_PER_SECOND = 200;
|
|
13
|
+
export declare const LOG_FLUSH_INTERVAL_MS = 100;
|
|
14
|
+
export declare const LOG_TAIL_MAX = 1000;
|
|
15
|
+
export type DaemonLogLevelName = Exclude<LogLevel, "silent">;
|
|
16
|
+
export interface DaemonLogsTimers {
|
|
17
|
+
setTimeout(fn: () => void, ms: number): unknown;
|
|
18
|
+
clearTimeout(handle: unknown): void;
|
|
19
|
+
}
|
|
20
|
+
export interface DaemonLogsServiceDeps {
|
|
21
|
+
tap: LogTap;
|
|
22
|
+
/** Delivers one `{type:"session", message}` envelope to a session. May throw. */
|
|
23
|
+
send(session: Session, envelope: unknown): void;
|
|
24
|
+
/** Monotonic milliseconds. Defaults to `performance.now`. */
|
|
25
|
+
now?: () => number;
|
|
26
|
+
timers?: DaemonLogsTimers;
|
|
27
|
+
}
|
|
28
|
+
export type DaemonLogsSubscribeResult = {
|
|
29
|
+
ok: true;
|
|
30
|
+
minLevel: DaemonLogLevelName;
|
|
31
|
+
effectiveLevel: LogLevel;
|
|
32
|
+
lines: LogLine[];
|
|
33
|
+
dropped: 0;
|
|
34
|
+
} | {
|
|
35
|
+
ok: false;
|
|
36
|
+
error: "invalid_level" | "invalid_tail";
|
|
37
|
+
};
|
|
38
|
+
export declare class DaemonLogsService {
|
|
39
|
+
private readonly tap;
|
|
40
|
+
private readonly send;
|
|
41
|
+
private readonly now;
|
|
42
|
+
private readonly timers;
|
|
43
|
+
private readonly subs;
|
|
44
|
+
private removeListener;
|
|
45
|
+
constructor(deps: DaemonLogsServiceDeps);
|
|
46
|
+
/**
|
|
47
|
+
* Validates, then REPLACES any existing subscription for the session (fresh limiter). An invalid
|
|
48
|
+
* request leaves the previous subscription untouched.
|
|
49
|
+
*/
|
|
50
|
+
subscribe(session: Session, minLevel: unknown, tail?: unknown): DaemonLogsSubscribeResult;
|
|
51
|
+
/** Idempotent. */
|
|
52
|
+
unsubscribe(session: Session): void;
|
|
53
|
+
/** `SessionSubscriptions` disposer; safe when not subscribed. */
|
|
54
|
+
disposeSession(session: Session): void;
|
|
55
|
+
/** Release every subscription, timer and tap listener (daemon shutdown). */
|
|
56
|
+
dispose(): void;
|
|
57
|
+
/** The stricter of the daemon's level and `minLevel`; `"silent"` when the tap is unattached. */
|
|
58
|
+
private effectiveLevel;
|
|
59
|
+
private ensureListening;
|
|
60
|
+
private stopListeningIfIdle;
|
|
61
|
+
private clearSubscription;
|
|
62
|
+
private onLine;
|
|
63
|
+
private enqueue;
|
|
64
|
+
private scheduleFlush;
|
|
65
|
+
private flush;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=daemon-logs-service.d.ts.map
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { performance } from "node:perf_hooks";
|
|
2
|
+
import { LOG_LEVEL_NUMBERS } from "../logging/log-tap.js";
|
|
3
|
+
/**
|
|
4
|
+
* Per-session daemon log delivery (swe/features/daemon-log-streaming.md § Subscribe / deliver /
|
|
5
|
+
* cap). Each subscriber owns a level filter, a 200 lines/s token bucket (burst 200), a queue and a
|
|
6
|
+
* 100 ms flush timer that exists only while that subscriber has pending data. Overflow lines are
|
|
7
|
+
* counted in `dropped` (incremental, per subscriber) and never re-delivered. The ring and the log
|
|
8
|
+
* files are never subject to the cap.
|
|
9
|
+
*/
|
|
10
|
+
export const LOG_RATE_PER_SECOND = 200;
|
|
11
|
+
export const LOG_FLUSH_INTERVAL_MS = 100;
|
|
12
|
+
export const LOG_TAIL_MAX = 1000;
|
|
13
|
+
const systemTimers = {
|
|
14
|
+
setTimeout: (fn, ms) => {
|
|
15
|
+
const handle = setTimeout(fn, ms);
|
|
16
|
+
handle.unref();
|
|
17
|
+
return handle;
|
|
18
|
+
},
|
|
19
|
+
clearTimeout: (handle) => clearTimeout(handle),
|
|
20
|
+
};
|
|
21
|
+
const isLevelName = (v) => typeof v === "string" && Object.hasOwn(LOG_LEVEL_NUMBERS, v);
|
|
22
|
+
export class DaemonLogsService {
|
|
23
|
+
tap;
|
|
24
|
+
send;
|
|
25
|
+
now;
|
|
26
|
+
timers;
|
|
27
|
+
subs = new Map();
|
|
28
|
+
removeListener = null;
|
|
29
|
+
constructor(deps) {
|
|
30
|
+
this.tap = deps.tap;
|
|
31
|
+
this.send = deps.send;
|
|
32
|
+
this.now = deps.now ?? (() => performance.now());
|
|
33
|
+
this.timers = deps.timers ?? systemTimers;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Validates, then REPLACES any existing subscription for the session (fresh limiter). An invalid
|
|
37
|
+
* request leaves the previous subscription untouched.
|
|
38
|
+
*/
|
|
39
|
+
subscribe(session, minLevel, tail) {
|
|
40
|
+
if (!isLevelName(minLevel))
|
|
41
|
+
return { ok: false, error: "invalid_level" };
|
|
42
|
+
const tailCount = tail === undefined ? 0 : tail;
|
|
43
|
+
if (typeof tailCount !== "number" ||
|
|
44
|
+
!Number.isInteger(tailCount) ||
|
|
45
|
+
tailCount < 0 ||
|
|
46
|
+
tailCount > LOG_TAIL_MAX) {
|
|
47
|
+
return { ok: false, error: "invalid_tail" };
|
|
48
|
+
}
|
|
49
|
+
this.clearSubscription(session);
|
|
50
|
+
const minLevelNum = LOG_LEVEL_NUMBERS[minLevel];
|
|
51
|
+
this.subs.set(session, {
|
|
52
|
+
session,
|
|
53
|
+
minLevelNum,
|
|
54
|
+
queue: [],
|
|
55
|
+
tokens: LOG_RATE_PER_SECOND,
|
|
56
|
+
refilledAt: this.now(),
|
|
57
|
+
droppedSinceLastPush: 0,
|
|
58
|
+
timer: null,
|
|
59
|
+
});
|
|
60
|
+
this.ensureListening();
|
|
61
|
+
const lines = tailCount > 0
|
|
62
|
+
? this.tap.backlog(minLevelNum, tailCount).map((raw) => this.tap.toLogLine(raw))
|
|
63
|
+
: [];
|
|
64
|
+
return {
|
|
65
|
+
ok: true,
|
|
66
|
+
minLevel,
|
|
67
|
+
effectiveLevel: this.effectiveLevel(minLevel),
|
|
68
|
+
lines,
|
|
69
|
+
dropped: 0,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/** Idempotent. */
|
|
73
|
+
unsubscribe(session) {
|
|
74
|
+
this.clearSubscription(session);
|
|
75
|
+
this.stopListeningIfIdle();
|
|
76
|
+
}
|
|
77
|
+
/** `SessionSubscriptions` disposer; safe when not subscribed. */
|
|
78
|
+
disposeSession(session) {
|
|
79
|
+
this.unsubscribe(session);
|
|
80
|
+
}
|
|
81
|
+
/** Release every subscription, timer and tap listener (daemon shutdown). */
|
|
82
|
+
dispose() {
|
|
83
|
+
for (const session of this.subs.keys())
|
|
84
|
+
this.clearSubscription(session);
|
|
85
|
+
this.stopListeningIfIdle();
|
|
86
|
+
}
|
|
87
|
+
/** The stricter of the daemon's level and `minLevel`; `"silent"` when the tap is unattached. */
|
|
88
|
+
effectiveLevel(minLevel) {
|
|
89
|
+
const daemonLevel = this.tap.effectiveLevel;
|
|
90
|
+
if (daemonLevel === "silent")
|
|
91
|
+
return "silent";
|
|
92
|
+
return LOG_LEVEL_NUMBERS[daemonLevel] > LOG_LEVEL_NUMBERS[minLevel] ? daemonLevel : minLevel;
|
|
93
|
+
}
|
|
94
|
+
ensureListening() {
|
|
95
|
+
this.removeListener ??= this.tap.onLine((raw, levelNum) => this.onLine(raw, levelNum));
|
|
96
|
+
}
|
|
97
|
+
stopListeningIfIdle() {
|
|
98
|
+
if (this.subs.size > 0 || this.removeListener === null)
|
|
99
|
+
return;
|
|
100
|
+
this.removeListener();
|
|
101
|
+
this.removeListener = null;
|
|
102
|
+
}
|
|
103
|
+
clearSubscription(session) {
|
|
104
|
+
const sub = this.subs.get(session);
|
|
105
|
+
if (!sub)
|
|
106
|
+
return;
|
|
107
|
+
if (sub.timer !== null)
|
|
108
|
+
this.timers.clearTimeout(sub.timer);
|
|
109
|
+
sub.timer = null;
|
|
110
|
+
this.subs.delete(session);
|
|
111
|
+
}
|
|
112
|
+
onLine(raw, levelNum) {
|
|
113
|
+
for (const sub of this.subs.values()) {
|
|
114
|
+
if (levelNum >= sub.minLevelNum)
|
|
115
|
+
this.enqueue(sub, raw);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
enqueue(sub, raw) {
|
|
119
|
+
const now = this.now();
|
|
120
|
+
const elapsed = Math.max(0, now - sub.refilledAt);
|
|
121
|
+
sub.refilledAt = now;
|
|
122
|
+
sub.tokens = Math.min(LOG_RATE_PER_SECOND, sub.tokens + (elapsed * LOG_RATE_PER_SECOND) / 1000);
|
|
123
|
+
if (sub.tokens >= 1) {
|
|
124
|
+
sub.tokens -= 1;
|
|
125
|
+
sub.queue.push(raw);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
sub.droppedSinceLastPush += 1;
|
|
129
|
+
}
|
|
130
|
+
this.scheduleFlush(sub);
|
|
131
|
+
}
|
|
132
|
+
scheduleFlush(sub) {
|
|
133
|
+
if (sub.timer !== null)
|
|
134
|
+
return;
|
|
135
|
+
sub.timer = this.timers.setTimeout(() => this.flush(sub), LOG_FLUSH_INTERVAL_MS);
|
|
136
|
+
}
|
|
137
|
+
flush(sub) {
|
|
138
|
+
sub.timer = null;
|
|
139
|
+
if (this.subs.get(sub.session) !== sub)
|
|
140
|
+
return;
|
|
141
|
+
if (sub.queue.length === 0 && sub.droppedSinceLastPush === 0)
|
|
142
|
+
return;
|
|
143
|
+
const batch = sub.queue.splice(0, LOG_RATE_PER_SECOND);
|
|
144
|
+
const dropped = sub.droppedSinceLastPush;
|
|
145
|
+
sub.droppedSinceLastPush = 0;
|
|
146
|
+
try {
|
|
147
|
+
this.send(sub.session, {
|
|
148
|
+
type: "session",
|
|
149
|
+
message: {
|
|
150
|
+
type: "daemon_log_lines",
|
|
151
|
+
lines: batch.map((raw) => this.tap.toLogLine(raw)),
|
|
152
|
+
dropped,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// Dead socket: remove only this subscriber; its close path also disposes.
|
|
158
|
+
this.unsubscribe(sub.session);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (sub.queue.length > 0)
|
|
162
|
+
this.scheduleFlush(sub);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=daemon-logs-service.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { HandlerRegistry } from "../ws/router.js";
|
|
2
|
+
import type { DaemonStatusService } from "./daemon-status-service.js";
|
|
3
|
+
/**
|
|
4
|
+
* Wires the `daemon_status_*` request family (swe/features/daemon-status-rpc.md) onto
|
|
5
|
+
* `DaemonStatusService`: a thin adapter with no policy of its own. Domain results travel in
|
|
6
|
+
* `payload`; `requestId` is stamped by the router. Registered in both bootstraps; advertised by the
|
|
7
|
+
* `daemonObservability` server feature.
|
|
8
|
+
*/
|
|
9
|
+
export declare function registerDaemonStatusHandlers(registry: HandlerRegistry, deps: {
|
|
10
|
+
service: DaemonStatusService;
|
|
11
|
+
}): void;
|
|
12
|
+
//# sourceMappingURL=daemon-status-rpc.d.ts.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wires the `daemon_status_*` request family (swe/features/daemon-status-rpc.md) onto
|
|
3
|
+
* `DaemonStatusService`: a thin adapter with no policy of its own. Domain results travel in
|
|
4
|
+
* `payload`; `requestId` is stamped by the router. Registered in both bootstraps; advertised by the
|
|
5
|
+
* `daemonObservability` server feature.
|
|
6
|
+
*/
|
|
7
|
+
export function registerDaemonStatusHandlers(registry, deps) {
|
|
8
|
+
registry.register("daemon_status_request", () => ({
|
|
9
|
+
type: "daemon_status_response",
|
|
10
|
+
payload: deps.service.oneShot(),
|
|
11
|
+
}));
|
|
12
|
+
registry.register("daemon_status_subscribe_request", (ctx) => ({
|
|
13
|
+
type: "daemon_status_subscribe_response",
|
|
14
|
+
payload: deps.service.subscribe(ctx.session),
|
|
15
|
+
}));
|
|
16
|
+
registry.register("daemon_status_unsubscribe_request", (ctx) => {
|
|
17
|
+
deps.service.unsubscribe(ctx.session);
|
|
18
|
+
return { type: "daemon_status_unsubscribe_response", payload: { ok: true } };
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=daemon-status-rpc.js.map
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { DaemonStatus } from "@av-pi-studio/protocol";
|
|
2
|
+
import type { Logger } from "../logging/logger.js";
|
|
3
|
+
import type { Session } from "../ws/session.js";
|
|
4
|
+
/**
|
|
5
|
+
* Daemon status snapshots + per-session push (swe/features/daemon-status-rpc.md § Behavior &
|
|
6
|
+
* algorithms). One shared 5 s timer and one event-loop histogram exist only while at least one
|
|
7
|
+
* session is subscribed; with no subscribers the service holds no timer, no histogram and does
|
|
8
|
+
* no work. Each tick builds ONE snapshot shared by every subscriber.
|
|
9
|
+
*/
|
|
10
|
+
export declare const DAEMON_STATUS_INTERVAL_MS = 5000;
|
|
11
|
+
/** The slice of `perf_hooks.IntervalHistogram` the service uses. */
|
|
12
|
+
export interface EventLoopHistogram {
|
|
13
|
+
enable(): unknown;
|
|
14
|
+
disable(): unknown;
|
|
15
|
+
reset(): void;
|
|
16
|
+
/** Mean delay in nanoseconds; `NaN` before any sample. */
|
|
17
|
+
readonly mean: number;
|
|
18
|
+
}
|
|
19
|
+
/** Process-level reads, injectable so tests can count calls and feed known values. */
|
|
20
|
+
export interface DaemonStatusSampler {
|
|
21
|
+
cpuUsage(): {
|
|
22
|
+
user: number;
|
|
23
|
+
system: number;
|
|
24
|
+
};
|
|
25
|
+
memoryUsage(): {
|
|
26
|
+
rss: number;
|
|
27
|
+
heapUsed: number;
|
|
28
|
+
heapTotal: number;
|
|
29
|
+
};
|
|
30
|
+
createEventLoopHistogram(): EventLoopHistogram;
|
|
31
|
+
/** Monotonic milliseconds. */
|
|
32
|
+
now(): number;
|
|
33
|
+
/** Wall-clock epoch milliseconds. */
|
|
34
|
+
wallNow(): number;
|
|
35
|
+
}
|
|
36
|
+
export declare const systemSampler: DaemonStatusSampler;
|
|
37
|
+
/** The minimal agent shape the status counts need (`AgentManager.list()` satisfies it). */
|
|
38
|
+
export interface StatusAgentView {
|
|
39
|
+
record: {
|
|
40
|
+
lastStatus: string;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface DaemonStatusServiceDeps {
|
|
44
|
+
/** Non-archived agents only (`AgentManager.list()`). */
|
|
45
|
+
listAgents(): readonly StatusAgentView[];
|
|
46
|
+
getActiveSessions(): readonly Session[];
|
|
47
|
+
/** Delivers one shared `{type:"session", message}` envelope to a session. May throw. */
|
|
48
|
+
send(session: Session, envelope: unknown): void;
|
|
49
|
+
sampler?: DaemonStatusSampler;
|
|
50
|
+
logger?: Logger;
|
|
51
|
+
}
|
|
52
|
+
export type DaemonStatusSubscribeResult = {
|
|
53
|
+
ok: true;
|
|
54
|
+
intervalMs: number;
|
|
55
|
+
status: DaemonStatus;
|
|
56
|
+
};
|
|
57
|
+
export declare class DaemonStatusService {
|
|
58
|
+
private readonly deps;
|
|
59
|
+
private readonly sampler;
|
|
60
|
+
private readonly startedAt;
|
|
61
|
+
private readonly startMonotonic;
|
|
62
|
+
private readonly subscribers;
|
|
63
|
+
private timer;
|
|
64
|
+
private histogram;
|
|
65
|
+
private lastCpu;
|
|
66
|
+
constructor(deps: DaemonStatusServiceDeps);
|
|
67
|
+
/** Build a snapshot. Advances the CPU baseline and resets the lag window. */
|
|
68
|
+
snapshot(): DaemonStatus;
|
|
69
|
+
/**
|
|
70
|
+
* One-shot status. Without subscribers no sampling runs, so `cpuPercent` and `eventLoopLagMs`
|
|
71
|
+
* are 0 (no window to measure): use `subscribe` for live gauges.
|
|
72
|
+
*/
|
|
73
|
+
oneShot(): DaemonStatus;
|
|
74
|
+
/** Idempotent per session; always returns a fresh snapshot. */
|
|
75
|
+
subscribe(session: Session): DaemonStatusSubscribeResult;
|
|
76
|
+
unsubscribe(session: Session): void;
|
|
77
|
+
/** `SessionSubscriptions` disposer: same as unsubscribe, safe when not subscribed. */
|
|
78
|
+
disposeSession(session: Session): void;
|
|
79
|
+
/** Release the timer and histogram (daemon shutdown). */
|
|
80
|
+
dispose(): void;
|
|
81
|
+
private startSampling;
|
|
82
|
+
private stopSampling;
|
|
83
|
+
private tick;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=daemon-status-service.d.ts.map
|