@mostra/runtime 0.12.0 → 0.14.0
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/bin.js +2912 -2318
- package/dist/bin.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/{instance-CBUlOmzG.js → instance-CFTw3g-c.js} +227 -25
- package/dist/instance-CFTw3g-c.js.map +1 -0
- package/dist/reference/diagnostics.json +2 -2
- package/dist/reference/errors/MOSTRA_REMOTE_QUEUE_FULL.md +2 -2
- package/dist/reference/self-hosting.md +2 -1
- package/dist/skill/SKILL.md +2 -2
- package/dist/skill/assets/examples/dashboard/styles.css +27 -9
- package/dist/skill/assets/examples/decision/styles.css +27 -9
- package/dist/skill/assets/examples/inbox/styles.css +27 -9
- package/dist/skill/assets/studio.css +27 -9
- package/dist/skill/references/mcp.md +2 -2
- package/package.json +7 -6
- package/src/actions/index.ts +5 -2
- package/src/gateway/actions.ts +6 -1
- package/src/gateway/capabilities.ts +5 -1
- package/src/gateway/http.ts +15 -19
- package/src/gateway/index.ts +70 -1
- package/src/gateway/listener.ts +10 -5
- package/src/gateway/routes.ts +6 -1
- package/src/gateway/theme.ts +238 -0
- package/src/ledger/migrations.ts +9 -0
- package/src/server/api.ts +79 -87
- package/src/server/auth.ts +83 -16
- package/src/server/authorize.ts +74 -58
- package/src/server/config.ts +98 -0
- package/src/server/directory.ts +51 -0
- package/src/server/hosted-pages.ts +19 -6
- package/src/server/hosted.ts +59 -27
- package/src/server/index.ts +144 -17
- package/src/server/metering.ts +117 -12
- package/src/server/pages.ts +242 -0
- package/src/server/providers.ts +61 -9
- package/src/server/relay.ts +258 -107
- package/dist/instance-CBUlOmzG.js.map +0 -1
package/src/server/index.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
-
import { readFileSync } from "node:fs";
|
|
15
|
+
import { readFileSync, statSync } from "node:fs";
|
|
16
16
|
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
|
|
17
17
|
import type { AddressInfo } from "node:net";
|
|
18
18
|
import { join } from "node:path";
|
|
@@ -29,13 +29,20 @@ import {
|
|
|
29
29
|
import { faultsFrom } from "../faults.ts";
|
|
30
30
|
import {
|
|
31
31
|
BASE_HEADERS,
|
|
32
|
+
defaultPages,
|
|
32
33
|
documentHeaders,
|
|
33
34
|
locateShell,
|
|
35
|
+
MADE_WITH_HREF,
|
|
36
|
+
refusalDocument,
|
|
34
37
|
resetOnClose,
|
|
38
|
+
type ServerPages,
|
|
39
|
+
serverPages,
|
|
35
40
|
shellAssetResponse,
|
|
41
|
+
THEME_BASE,
|
|
42
|
+
themePageCsp,
|
|
36
43
|
} from "../gateway/index.ts";
|
|
37
44
|
import { appendEvent } from "../interaction/index.ts";
|
|
38
|
-
import type { LedgerOwner } from "../ledger/index.ts";
|
|
45
|
+
import type { Ledger, LedgerOwner } from "../ledger/index.ts";
|
|
39
46
|
import type { Environment, Log } from "../log.ts";
|
|
40
47
|
import { type Api, createApi } from "./api.ts";
|
|
41
48
|
import { type Authorization, createAuthorization } from "./auth.ts";
|
|
@@ -47,9 +54,10 @@ import { createHandle } from "./handle.ts";
|
|
|
47
54
|
import { type HostedHome, type HostedServices, openHostedHome } from "./hosted.ts";
|
|
48
55
|
import { type HomeOwner, homeUnavailable } from "./hosted-pages.ts";
|
|
49
56
|
import { createRemoteMcp, serverDocs } from "./mcp.ts";
|
|
50
|
-
import { createMeter, type Meter } from "./metering.ts";
|
|
57
|
+
import { createMeter, heldLevels, type Meter } from "./metering.ts";
|
|
51
58
|
import { createClients } from "./oauth-clients.ts";
|
|
52
59
|
import { loadOperator, type Operator } from "./operator.ts";
|
|
60
|
+
import { APPEARANCE_PATH, createApiPages } from "./pages.ts";
|
|
53
61
|
import { type BuiltInProvider, builtInProviders } from "./providers.ts";
|
|
54
62
|
import { createRouting, type Destination, type Routing } from "./routing.ts";
|
|
55
63
|
|
|
@@ -93,8 +101,43 @@ function json(response: ServerResponse, status: number, body: unknown): void {
|
|
|
93
101
|
response.end(text);
|
|
94
102
|
}
|
|
95
103
|
|
|
96
|
-
|
|
97
|
-
|
|
104
|
+
/** Whether a request is a person's navigation, answered with a page rather than JSON. */
|
|
105
|
+
function wantsPage(request: IncomingMessage | undefined): boolean {
|
|
106
|
+
if (!request) return false;
|
|
107
|
+
const dest = request.headers["sec-fetch-dest"];
|
|
108
|
+
if (typeof dest === "string") return dest === "document";
|
|
109
|
+
return (request.headers.accept ?? "").includes("text/html");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A refusal: a page in the server's theme for a person's navigation (an unknown host, a request
|
|
114
|
+
* the server couldn't answer), the diagnostic as JSON for anything else.
|
|
115
|
+
*/
|
|
116
|
+
function refusal(
|
|
117
|
+
response: ServerResponse,
|
|
118
|
+
status: number,
|
|
119
|
+
diagnostic: Diagnostic,
|
|
120
|
+
pages: ServerPages = defaultPages(),
|
|
121
|
+
): void {
|
|
122
|
+
const request = response.req as IncomingMessage | undefined;
|
|
123
|
+
if (!wantsPage(request)) {
|
|
124
|
+
json(response, status, diagnostic);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const text = refusalDocument(pages.pages, {
|
|
128
|
+
title: diagnostic.message,
|
|
129
|
+
detail: diagnostic.hint,
|
|
130
|
+
code: diagnostic.code,
|
|
131
|
+
});
|
|
132
|
+
response.writeHead(status, {
|
|
133
|
+
...BASE_HEADERS,
|
|
134
|
+
"content-type": "text/html; charset=utf-8",
|
|
135
|
+
"content-length": String(Buffer.byteLength(text)),
|
|
136
|
+
"cache-control": "no-store",
|
|
137
|
+
"content-security-policy": themePageCsp(pages.pages),
|
|
138
|
+
"x-frame-options": "DENY",
|
|
139
|
+
});
|
|
140
|
+
response.end(request?.method === "HEAD" ? undefined : text);
|
|
98
141
|
}
|
|
99
142
|
|
|
100
143
|
/** Splits `host:port`, with `[::1]:port` for IPv6. */
|
|
@@ -116,6 +159,8 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
116
159
|
// The authorization server and the operator module are made once the directory is open; homes
|
|
117
160
|
// open only after both.
|
|
118
161
|
let auth: Authorization | undefined;
|
|
162
|
+
// The hosted hosts' own pages, once the configuration's are made (below); the defaults until then.
|
|
163
|
+
let pagesOfHosts: ServerPages = defaultPages();
|
|
119
164
|
let operator: Operator | undefined;
|
|
120
165
|
let meter: Meter | undefined;
|
|
121
166
|
let routing: Routing;
|
|
@@ -136,27 +181,28 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
136
181
|
* health check is never counted. Without the setting there is no limit.
|
|
137
182
|
*/
|
|
138
183
|
const perIpRequests = rateOf(config.rateLimits?.perIp?.requests);
|
|
139
|
-
/**
|
|
184
|
+
/**
|
|
185
|
+
* Who owns a home, as its pages' sender line names them (D161): the account's name, and the
|
|
186
|
+
* verifier its first identity recorded when its provider named the person (MT-03), never the
|
|
187
|
+
* configuration now, so a provider renamed or retired since changes no sender line. A name the
|
|
188
|
+
* development provider took on trust, or one no provider verified, carries none.
|
|
189
|
+
*/
|
|
140
190
|
const ownerOf = (homeId: string): HomeOwner => {
|
|
141
191
|
const row = data.directory.read((db) =>
|
|
142
|
-
db.get<{ account: string; name: string | null;
|
|
143
|
-
`SELECT a.id AS account, a.name,
|
|
144
|
-
|
|
145
|
-
|
|
192
|
+
db.get<{ account: string; name: string | null; verified_by: string | null }>(
|
|
193
|
+
`SELECT a.id AS account, a.name, i.verified_by
|
|
194
|
+
FROM home h JOIN account a ON a.id = h.owner_account_id
|
|
195
|
+
LEFT JOIN identity i ON i.rowid = (SELECT rowid FROM identity WHERE account_id = a.id
|
|
196
|
+
ORDER BY linked_at, rowid LIMIT 1)
|
|
197
|
+
WHERE h.id = ?`,
|
|
146
198
|
homeId,
|
|
147
199
|
),
|
|
148
200
|
);
|
|
149
201
|
if (!row) return { account: "" };
|
|
150
|
-
const provider = config.identityProviders?.find((candidate) => candidate.id === row.provider);
|
|
151
|
-
// A name the development provider took on trust is unverified (D161).
|
|
152
|
-
const verifiedBy =
|
|
153
|
-
provider && provider.kind !== "development"
|
|
154
|
-
? (provider.label ?? (provider.kind === "github" ? "GitHub" : provider.id))
|
|
155
|
-
: undefined;
|
|
156
202
|
return {
|
|
157
203
|
account: row.account,
|
|
158
204
|
...(row.name !== null ? { name: row.name } : {}),
|
|
159
|
-
...(
|
|
205
|
+
...(row.verified_by !== null ? { verifiedBy: row.verified_by } : {}),
|
|
160
206
|
};
|
|
161
207
|
};
|
|
162
208
|
const services = (): HostedServices => ({
|
|
@@ -176,6 +222,10 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
176
222
|
},
|
|
177
223
|
signInOffered: () =>
|
|
178
224
|
(config.identityProviders?.length ?? 0) > 0 || (operator?.has.identity ?? false),
|
|
225
|
+
// On by default for every page, self-hosted included; an operator's `policy.link` turns it
|
|
226
|
+
// off for the pages it promises unbranded (D171 item 5).
|
|
227
|
+
madeWith: { href: MADE_WITH_HREF },
|
|
228
|
+
pages: () => pagesOfHosts,
|
|
179
229
|
});
|
|
180
230
|
const assembler: HomeAssembler<HostedHome> = {
|
|
181
231
|
assemble: (home) =>
|
|
@@ -194,6 +244,9 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
194
244
|
domains: { active: config.domains.active, spares: config.domains.spares ?? [] },
|
|
195
245
|
shellDir,
|
|
196
246
|
services: services(),
|
|
247
|
+
...(Number(options.env["MOSTRA_TEST_SESSION_TOUCH_MS"]) > 0
|
|
248
|
+
? { sessionTouchMs: Number(options.env["MOSTRA_TEST_SESSION_TOUCH_MS"]) }
|
|
249
|
+
: {}),
|
|
197
250
|
relay: {
|
|
198
251
|
waitingRunsPerArtifact: config.relay?.waitingRunsPerArtifact,
|
|
199
252
|
drainPerSecond: config.relay?.drainPerSecond,
|
|
@@ -263,6 +316,8 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
263
316
|
await data.close();
|
|
264
317
|
throw error;
|
|
265
318
|
}
|
|
319
|
+
// A held file recorded before sizes were kept (directory migration 6) takes its stored size once.
|
|
320
|
+
fillHeldSizes(data.directory, join(options.data, "files"), log);
|
|
266
321
|
routing = createRouting({
|
|
267
322
|
apiHost: new URL(config.apiOrigin).host,
|
|
268
323
|
domains: () => [config.domains.active, ...(config.domains.spares ?? [])],
|
|
@@ -279,6 +334,11 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
279
334
|
? { windowMs: Number(options.env["MOSTRA_TEST_METER_WINDOW_MS"]) }
|
|
280
335
|
: {}),
|
|
281
336
|
activeHomes: () => data.open().map((home) => ({ home, account: ownerOf(home).account })),
|
|
337
|
+
// `bytes.held` (MT-03): each home's running total, sent when it changed, and all in a snapshot.
|
|
338
|
+
held: heldLevels(data.directory),
|
|
339
|
+
...(Number(options.env["MOSTRA_TEST_HELD_SNAPSHOT_MS"]) > 0
|
|
340
|
+
? { snapshotMs: Number(options.env["MOSTRA_TEST_HELD_SNAPSHOT_MS"]) }
|
|
341
|
+
: {}),
|
|
282
342
|
});
|
|
283
343
|
auth = createAuthorization({
|
|
284
344
|
directory: data.directory,
|
|
@@ -335,7 +395,26 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
335
395
|
await data.close();
|
|
336
396
|
throw error;
|
|
337
397
|
}
|
|
398
|
+
// The server's own pages (D171): the API origin's, with the appearance switch its own cookie
|
|
399
|
+
// keeps, and the hosted hosts', which keep no cookie (D165 item 8).
|
|
400
|
+
let apiPages: ServerPages;
|
|
401
|
+
let hostedPages: ServerPages;
|
|
402
|
+
try {
|
|
403
|
+
apiPages = serverPages(config.appearance, { appearanceAction: APPEARANCE_PATH });
|
|
404
|
+
hostedPages = serverPages(config.appearance);
|
|
405
|
+
} catch (error) {
|
|
406
|
+
operator?.stop();
|
|
407
|
+
await data.close();
|
|
408
|
+
throw new RpcError(
|
|
409
|
+
createDiagnostic("MOSTRA_REMOTE_CONFIG_INVALID", {
|
|
410
|
+
message: `The configuration key \`appearance.logo\` can't be used: ${(error as Error).message}`,
|
|
411
|
+
}),
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
pagesOfHosts = hostedPages;
|
|
338
415
|
const authorize = createAuthorize({
|
|
416
|
+
server: apiPages,
|
|
417
|
+
pages: createApiPages(apiPages, apiPages.name),
|
|
339
418
|
apiOrigin: config.apiOrigin,
|
|
340
419
|
directory: data.directory,
|
|
341
420
|
auth,
|
|
@@ -357,6 +436,7 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
357
436
|
ensureHome: (accountId) => (auth as Authorization).ensureHome(accountId),
|
|
358
437
|
});
|
|
359
438
|
api = createApi({
|
|
439
|
+
server: apiPages,
|
|
360
440
|
config,
|
|
361
441
|
auth,
|
|
362
442
|
mcp,
|
|
@@ -386,6 +466,16 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
386
466
|
// Cookies never reach anything on a hosted host (D165 item 8): dropped before routing. Only
|
|
387
467
|
// the API origin's own sign-in pages read the cookies it set itself.
|
|
388
468
|
if (host !== apiHost) delete request.headers.cookie;
|
|
469
|
+
// The files a server's own pages load (D171): the same public files on every host, so even
|
|
470
|
+
// the page for an unknown host is drawn in the server's theme.
|
|
471
|
+
if ((request.method === "GET" || request.method === "HEAD") && path.startsWith(THEME_BASE)) {
|
|
472
|
+
const file = pagesOfHosts.asset(path.slice(THEME_BASE.length));
|
|
473
|
+
if (file) {
|
|
474
|
+
response.writeHead(200, { ...BASE_HEADERS, ...file.headers });
|
|
475
|
+
response.end(request.method === "HEAD" ? undefined : file.body);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
389
479
|
if (host === apiHost && request.method === "GET" && path === "/health") {
|
|
390
480
|
json(response, 200, { status: "ok", homes: data.open().length });
|
|
391
481
|
return;
|
|
@@ -426,6 +516,7 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
426
516
|
createDiagnostic("MOSTRA_HYGIENE_HOST_REFUSED", {
|
|
427
517
|
message: "This server serves no pages at this host.",
|
|
428
518
|
}),
|
|
519
|
+
pagesOfHosts,
|
|
429
520
|
);
|
|
430
521
|
// An empty or malformed Host gets nothing, and is never asked of an operator.
|
|
431
522
|
if (!/^[a-z0-9.-]{1,253}(:\d{1,5})?$/.test(host)) {
|
|
@@ -658,3 +749,39 @@ export async function startServer(options: ServerOptions): Promise<RemoteServer>
|
|
|
658
749
|
},
|
|
659
750
|
};
|
|
660
751
|
}
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Gives each `home_file` row recorded before sizes were kept (directory migration 6) the size of
|
|
755
|
+
* its stored file, so `bytes.held` counts it (MT-03); the triggers add it to the home's total. A
|
|
756
|
+
* file that is gone holds nothing on disk, so its row takes 0 and is never read again.
|
|
757
|
+
*/
|
|
758
|
+
function fillHeldSizes(directory: Ledger, filesDir: string, log: Log): void {
|
|
759
|
+
const rows = directory.read((db) =>
|
|
760
|
+
db.all<{ home_id: string; sha256: string }>(
|
|
761
|
+
"SELECT home_id, sha256 FROM home_file WHERE size IS NULL",
|
|
762
|
+
),
|
|
763
|
+
);
|
|
764
|
+
if (rows.length === 0) return;
|
|
765
|
+
const sizes = new Map<string, number>();
|
|
766
|
+
let missing = 0;
|
|
767
|
+
for (const row of rows) {
|
|
768
|
+
if (sizes.has(row.sha256)) continue;
|
|
769
|
+
try {
|
|
770
|
+
sizes.set(row.sha256, statSync(join(filesDir, row.sha256)).size);
|
|
771
|
+
} catch {
|
|
772
|
+
sizes.set(row.sha256, 0);
|
|
773
|
+
missing += 1;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
directory.write((db) => {
|
|
777
|
+
for (const row of rows) {
|
|
778
|
+
db.run(
|
|
779
|
+
"UPDATE home_file SET size = ? WHERE home_id = ? AND sha256 = ? AND size IS NULL",
|
|
780
|
+
sizes.get(row.sha256) ?? 0,
|
|
781
|
+
row.home_id,
|
|
782
|
+
row.sha256,
|
|
783
|
+
);
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
log.info("server.held_sizes_filled", { files: sizes.size, missing });
|
|
787
|
+
}
|
package/src/server/metering.ts
CHANGED
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
* Metering for an operator (INTERFACES §18.6, D165 item 9): counters kept in memory for a window,
|
|
3
3
|
* then handed to the operator's `metering.record` as one batch with a stable `batchId`, through
|
|
4
4
|
* the durable outbound queue, so a batch is delivered at least once across restarts. With no
|
|
5
|
-
* metering port nothing is counted.
|
|
5
|
+
* metering port nothing is counted. Besides the sums, windows carry one level, `bytes.held`: what
|
|
6
|
+
* a home holds now, sent when it changes (and 0 once when it holds nothing any more), and for every
|
|
7
|
+
* home that holds anything at the first window after a start and once a day after (MT-03). A
|
|
8
|
+
* window's counters go out in batches of at most {@link MAX_COUNTERS}.
|
|
6
9
|
*
|
|
7
10
|
* Views carry `viewerKey`, a monthly pseudonymous key: an HMAC of the viewer's subject under a key
|
|
8
11
|
* the server makes for each month (`<data>/keys/metering-<YYYY-MM>`), so an operator can count
|
|
@@ -14,11 +17,23 @@ import { createHmac, randomBytes, randomUUID } from "node:crypto";
|
|
|
14
17
|
import { readFileSync, writeFileSync } from "node:fs";
|
|
15
18
|
import { join } from "node:path";
|
|
16
19
|
import type { MeterCounter, MeterName } from "@mostra/protocol/operator";
|
|
20
|
+
import type { Ledger } from "../ledger/index.ts";
|
|
17
21
|
import type { Log } from "../log.ts";
|
|
18
22
|
import type { Operator } from "./operator.ts";
|
|
19
23
|
|
|
20
24
|
/** How long one batch's window lasts. */
|
|
21
25
|
const WINDOW_MS = 60_000;
|
|
26
|
+
/** How often every home that holds anything reports its level again, changed or not. */
|
|
27
|
+
const SNAPSHOT_MS = 24 * 60 * 60 * 1000;
|
|
28
|
+
/** The most counters one batch carries; a window with more sends several batches. */
|
|
29
|
+
export const MAX_COUNTERS = 1000;
|
|
30
|
+
|
|
31
|
+
/** One home's held level (`bytes.held`, MT-03). */
|
|
32
|
+
export interface HeldLevel {
|
|
33
|
+
readonly home: string;
|
|
34
|
+
readonly account: string;
|
|
35
|
+
readonly bytes: number;
|
|
36
|
+
}
|
|
22
37
|
|
|
23
38
|
/** What one counter is counted under, besides its name. */
|
|
24
39
|
export interface MeterScope {
|
|
@@ -53,6 +68,21 @@ export interface MeterOptions {
|
|
|
53
68
|
readonly windowMs?: number;
|
|
54
69
|
/** Seconds each open home was active, added at every window. */
|
|
55
70
|
readonly activeHomes?: () => readonly { readonly home: string; readonly account: string }[];
|
|
71
|
+
/**
|
|
72
|
+
* The homes' held levels for `bytes.held` (MT-03), kept durably by the caller so a change made
|
|
73
|
+
* just before a stop, or while nothing was reported, is still sent after a restart.
|
|
74
|
+
*/
|
|
75
|
+
readonly held?: {
|
|
76
|
+
/**
|
|
77
|
+
* The levels to report: each home whose level changed since it was last reported, or with
|
|
78
|
+
* `full`, also every home that holds anything (or last reported more than nothing).
|
|
79
|
+
*/
|
|
80
|
+
pending(full: boolean): readonly HeldLevel[];
|
|
81
|
+
/** The levels were queued: each is reported unless its home's level changed meanwhile. */
|
|
82
|
+
reported(levels: readonly HeldLevel[]): void;
|
|
83
|
+
};
|
|
84
|
+
/** How often every held level is sent again; a day unless a test shortens it. */
|
|
85
|
+
readonly snapshotMs?: number;
|
|
56
86
|
}
|
|
57
87
|
|
|
58
88
|
/** Creates the meter. */
|
|
@@ -63,6 +93,9 @@ export function createMeter(options: MeterOptions): Meter {
|
|
|
63
93
|
const counters = new Map<string, { counter: MeterCounter; value: number }>();
|
|
64
94
|
let from = now();
|
|
65
95
|
const monthKeys = new Map<string, Buffer>();
|
|
96
|
+
const snapshotMs = options.snapshotMs ?? SNAPSHOT_MS;
|
|
97
|
+
/** When every level was last sent; never, so the first window after a start sends them all. */
|
|
98
|
+
let snapshotAt = Number.NEGATIVE_INFINITY;
|
|
66
99
|
|
|
67
100
|
const monthKey = (): Buffer => {
|
|
68
101
|
const month = new Date(now()).toISOString().slice(0, 7);
|
|
@@ -94,19 +127,39 @@ export function createMeter(options: MeterOptions): Meter {
|
|
|
94
127
|
add("home.activeSeconds", { account: active.account, home: active.home }, seconds);
|
|
95
128
|
}
|
|
96
129
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
130
|
+
// `bytes.held` is a level, not a sum: a home whose level changed reports it, and every home
|
|
131
|
+
// that holds anything reports it again at the first window after a start and once a day.
|
|
132
|
+
const full = to - snapshotAt >= snapshotMs;
|
|
133
|
+
const levels = options.held?.pending(full) ?? [];
|
|
134
|
+
if (full) snapshotAt = to;
|
|
135
|
+
const held: MeterCounter[] = levels
|
|
136
|
+
.filter((level) => level.account !== "" && level.bytes >= 0)
|
|
137
|
+
.map((level) => ({
|
|
138
|
+
name: "bytes.held",
|
|
139
|
+
account: level.account,
|
|
140
|
+
home: level.home,
|
|
141
|
+
value: level.bytes,
|
|
142
|
+
}));
|
|
143
|
+
const all = [
|
|
144
|
+
...[...counters.values()].map(({ counter, value }) => ({ ...counter, value })),
|
|
145
|
+
...held,
|
|
146
|
+
];
|
|
107
147
|
counters.clear();
|
|
148
|
+
if (all.length > 0) {
|
|
149
|
+
const window = { from: new Date(from).toISOString(), to: new Date(to).toISOString() };
|
|
150
|
+
for (let start = 0; start < all.length; start += MAX_COUNTERS) {
|
|
151
|
+
options.operator.enqueue({
|
|
152
|
+
port: "metering",
|
|
153
|
+
batch: {
|
|
154
|
+
batchId: randomUUID(),
|
|
155
|
+
...window,
|
|
156
|
+
counters: all.slice(start, start + MAX_COUNTERS),
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (levels.length > 0) options.held?.reported(levels);
|
|
108
162
|
from = to;
|
|
109
|
-
options.operator.enqueue({ port: "metering", batch });
|
|
110
163
|
};
|
|
111
164
|
|
|
112
165
|
const add = (name: MeterName, scope: MeterScope, value = 1): void => {
|
|
@@ -154,3 +207,55 @@ export function createMeter(options: MeterOptions): Meter {
|
|
|
154
207
|
},
|
|
155
208
|
};
|
|
156
209
|
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The held levels of a server's homes, from its directory (MT-03): each home's running total in
|
|
213
|
+
* `home.held_bytes`, which `home_file`'s triggers keep in the transaction of every change, and
|
|
214
|
+
* `held_dirty`, set by them until the level is queued, so a change made just before a stop is
|
|
215
|
+
* still reported after the restart. A level is marked reported only while it is still the home's.
|
|
216
|
+
*/
|
|
217
|
+
export function heldLevels(directory: Ledger): NonNullable<MeterOptions["held"]> {
|
|
218
|
+
return {
|
|
219
|
+
pending: (full) =>
|
|
220
|
+
directory.write((db) => {
|
|
221
|
+
const rows = db.all<{
|
|
222
|
+
home: string;
|
|
223
|
+
account: string;
|
|
224
|
+
bytes: number;
|
|
225
|
+
reported: number | null;
|
|
226
|
+
}>(
|
|
227
|
+
`SELECT id AS home, owner_account_id AS account, held_bytes AS bytes,
|
|
228
|
+
held_reported AS reported
|
|
229
|
+
FROM home
|
|
230
|
+
WHERE held_dirty = 1${full ? " OR held_bytes > 0 OR COALESCE(held_reported, 0) > 0" : ""}`,
|
|
231
|
+
);
|
|
232
|
+
const levels: HeldLevel[] = [];
|
|
233
|
+
for (const row of rows) {
|
|
234
|
+
// Outside a snapshot, a change that came back to the level last sent, or a home that
|
|
235
|
+
// never held anything, has nothing new to say.
|
|
236
|
+
if (!full && row.bytes === (row.reported ?? 0)) {
|
|
237
|
+
db.run(
|
|
238
|
+
"UPDATE home SET held_dirty = 0 WHERE id = ? AND held_bytes = ?",
|
|
239
|
+
row.home,
|
|
240
|
+
row.bytes,
|
|
241
|
+
);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (row.reported === null && row.bytes === 0) continue;
|
|
245
|
+
levels.push({ home: row.home, account: row.account, bytes: row.bytes });
|
|
246
|
+
}
|
|
247
|
+
return levels;
|
|
248
|
+
}),
|
|
249
|
+
reported: (levels) =>
|
|
250
|
+
directory.write((db) => {
|
|
251
|
+
for (const level of levels) {
|
|
252
|
+
db.run(
|
|
253
|
+
"UPDATE home SET held_reported = ?, held_dirty = 0 WHERE id = ? AND held_bytes = ?",
|
|
254
|
+
level.bytes,
|
|
255
|
+
level.home,
|
|
256
|
+
level.bytes,
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}),
|
|
260
|
+
};
|
|
261
|
+
}
|