@senpi/runtime 3.0.266 → 3.0.267
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/daemon/errors.d.ts.map +1 -1
- package/dist/daemon/errors.js +9 -0
- package/dist/daemon/errors.js.map +1 -1
- package/dist/daemon/mcp-endpoint.d.ts +5 -2
- package/dist/daemon/mcp-endpoint.d.ts.map +1 -1
- package/dist/daemon/mcp-endpoint.js +62 -5
- package/dist/daemon/mcp-endpoint.js.map +1 -1
- package/dist/daemon/route-table.d.ts.map +1 -1
- package/dist/daemon/route-table.js +10 -0
- package/dist/daemon/route-table.js.map +1 -1
- package/dist/deploy/batch/batch.d.ts +7 -0
- package/dist/deploy/batch/batch.d.ts.map +1 -1
- package/dist/deploy/batch/batch.js +32 -6
- package/dist/deploy/batch/batch.js.map +1 -1
- package/dist/deploy/packages/files.d.ts +27 -0
- package/dist/deploy/packages/files.d.ts.map +1 -0
- package/dist/deploy/packages/files.js +81 -0
- package/dist/deploy/packages/files.js.map +1 -0
- package/dist/deploy/packages/store.d.ts +121 -0
- package/dist/deploy/packages/store.d.ts.map +1 -0
- package/dist/deploy/packages/store.js +417 -0
- package/dist/deploy/packages/store.js.map +1 -0
- package/dist/deploy/register.d.ts +9 -0
- package/dist/deploy/register.d.ts.map +1 -1
- package/dist/deploy/register.js +82 -7
- package/dist/deploy/register.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/integrity-manifest.json +33 -25
- package/dist/runtime/process-state.d.ts +3 -0
- package/dist/runtime/process-state.d.ts.map +1 -1
- package/dist/runtime/process-state.js.map +1 -1
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The files of a strategy package sent over MCP by an outside agent: the limits they must meet and
|
|
3
|
+
* the id they get. Pure: nothing here touches the disk.
|
|
4
|
+
*/
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
export const PACKAGE_MAX_FILES = 40;
|
|
7
|
+
export const PACKAGE_MAX_TOTAL_BYTES = 512 * 1024;
|
|
8
|
+
export const PACKAGE_MAX_FILE_BYTES = 128 * 1024;
|
|
9
|
+
export const PACKAGE_EXTENSIONS = [".py", ".yaml", ".yml", ".json", ".md", ".txt"];
|
|
10
|
+
const PATH_RE = /^[A-Za-z0-9_][A-Za-z0-9_./-]{0,127}$/;
|
|
11
|
+
export const PACKAGE_ID_RE = /^pkg-[0-9a-f]{16}$/;
|
|
12
|
+
/** A refusal answered before anything is written. `code` is one of the `E_PACKAGE_*` codes. */
|
|
13
|
+
export class PackageRefusal extends Error {
|
|
14
|
+
code;
|
|
15
|
+
constructor(code, message) {
|
|
16
|
+
super(`[${code}] ${message}`);
|
|
17
|
+
this.code = code;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function extensionOf(path) {
|
|
21
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
22
|
+
const dot = name.lastIndexOf(".");
|
|
23
|
+
return dot <= 0 ? "" : name.slice(dot).toLowerCase();
|
|
24
|
+
}
|
|
25
|
+
/** Check `raw` against every limit and return it as files, or throw the first refusal. */
|
|
26
|
+
export function checkPackageFiles(raw) {
|
|
27
|
+
if (!Array.isArray(raw) || raw.length === 0) {
|
|
28
|
+
throw new PackageRefusal("INVALID_REQUEST", "files must be a non-empty list of {path, content}.");
|
|
29
|
+
}
|
|
30
|
+
if (raw.length > PACKAGE_MAX_FILES) {
|
|
31
|
+
throw new PackageRefusal("E_PACKAGE_TOO_LARGE", `a package holds at most ${PACKAGE_MAX_FILES} files; this one has ${raw.length}.`);
|
|
32
|
+
}
|
|
33
|
+
const seen = new Set();
|
|
34
|
+
const files = [];
|
|
35
|
+
let total = 0;
|
|
36
|
+
for (const [i, entry] of raw.entries()) {
|
|
37
|
+
const e = entry;
|
|
38
|
+
if (!e || typeof e.path !== "string" || typeof e.content !== "string") {
|
|
39
|
+
throw new PackageRefusal("INVALID_REQUEST", `files[${i}] must be {path: string, content: string}.`);
|
|
40
|
+
}
|
|
41
|
+
const path = e.path;
|
|
42
|
+
const parts = path.split("/");
|
|
43
|
+
if (!PATH_RE.test(path) || parts.some((p) => p === "" || p === ".." || p.startsWith("."))) {
|
|
44
|
+
throw new PackageRefusal("E_PACKAGE_PATH", `"${path.slice(0, 140)}" is not a package path: use a relative path of letters, digits, "_", "-", "." and "/", with no part that is empty, "..", or starts with ".".`);
|
|
45
|
+
}
|
|
46
|
+
if (seen.has(path))
|
|
47
|
+
throw new PackageRefusal("E_PACKAGE_PATH", `"${path}" is sent twice.`);
|
|
48
|
+
seen.add(path);
|
|
49
|
+
if (!PACKAGE_EXTENSIONS.includes(extensionOf(path))) {
|
|
50
|
+
throw new PackageRefusal("E_PACKAGE_FILE", `"${path}" has a file type a package cannot hold; use ${PACKAGE_EXTENSIONS.join(" ")}.`);
|
|
51
|
+
}
|
|
52
|
+
if (e.content.includes("\0"))
|
|
53
|
+
throw new PackageRefusal("E_PACKAGE_FILE", `"${path}" holds a NUL byte; a package holds text files only.`);
|
|
54
|
+
const bytes = Buffer.byteLength(e.content, "utf8");
|
|
55
|
+
if (bytes > PACKAGE_MAX_FILE_BYTES) {
|
|
56
|
+
throw new PackageRefusal("E_PACKAGE_FILE", `"${path}" is ${bytes} bytes; one file holds at most ${PACKAGE_MAX_FILE_BYTES}.`);
|
|
57
|
+
}
|
|
58
|
+
total += bytes;
|
|
59
|
+
files.push({ path, content: e.content });
|
|
60
|
+
}
|
|
61
|
+
if (total > PACKAGE_MAX_TOTAL_BYTES) {
|
|
62
|
+
throw new PackageRefusal("E_PACKAGE_TOO_LARGE", `the package is ${total} bytes; it holds at most ${PACKAGE_MAX_TOTAL_BYTES}.`);
|
|
63
|
+
}
|
|
64
|
+
if (!seen.has("strategy.yaml")) {
|
|
65
|
+
throw new PackageRefusal("E_PACKAGE_LAYOUT", "a package needs strategy.yaml at its root, beside runtime.yaml and scanners/.");
|
|
66
|
+
}
|
|
67
|
+
return files.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
68
|
+
}
|
|
69
|
+
export function sha256(text) {
|
|
70
|
+
return createHash("sha256").update(text).digest("hex");
|
|
71
|
+
}
|
|
72
|
+
/** The package's id: `pkg-` and 16 hex of SHA-256 over the files sorted by path, each `path \0 content \0`. */
|
|
73
|
+
export function packageDigest(files) {
|
|
74
|
+
const h = createHash("sha256");
|
|
75
|
+
for (const f of [...files].sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0))) {
|
|
76
|
+
h.update(f.path, "utf8").update("\0").update(f.content, "utf8").update("\0");
|
|
77
|
+
}
|
|
78
|
+
const hex = h.digest("hex");
|
|
79
|
+
return { packageId: `pkg-${hex.slice(0, 16)}`, sha256: hex };
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=files.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.js","sourceRoot":"","sources":["../../../src/deploy/packages/files.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AACpC,MAAM,CAAC,MAAM,uBAAuB,GAAG,GAAG,GAAG,IAAI,CAAC;AAClD,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAG,GAAG,IAAI,CAAC;AACjD,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,CAAU,CAAC;AAC5F,MAAM,OAAO,GAAG,sCAAsC,CAAC;AACvD,MAAM,CAAC,MAAM,aAAa,GAAG,oBAAoB,CAAC;AAOlD,+FAA+F;AAC/F,MAAM,OAAO,cAAe,SAAQ,KAAK;IAE5B;IADX,YACW,IAAY,EACrB,OAAe;QAEf,KAAK,CA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CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5F,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC/E,CAAC;IACD,MAAM,GAAG,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5B,OAAO,EAAE,SAAS,EAAE,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;AAC/D,CAAC"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strategy packages an outside agent uploads to this box (`senpi.packages.*`), for simulations only.
|
|
3
|
+
*
|
|
4
|
+
* One package per strategy id under `<baseStateDir>/uploaded-packages/<strategyId>/`, with a
|
|
5
|
+
* `.package.json` beside the files. An upload is checked (load, one instance, `senpi validate` to
|
|
6
|
+
* the import depth) in a staging dir and kept only when clean; a clean upload of the same strategy
|
|
7
|
+
* id replaces the one before it. A live check (`validate`) runs one tick on a copy, one at a time
|
|
8
|
+
* per box, and its verdict is kept with the package. `.package.json` is copied into every variant a
|
|
9
|
+
* batch makes from the package, which is how a live deploy of uploaded code is recognised and refused.
|
|
10
|
+
*/
|
|
11
|
+
import { type StrategySettings } from "../../strategy/settings.js";
|
|
12
|
+
import type { ValidateResult } from "../../validate/run.js";
|
|
13
|
+
export declare const PACKAGE_META_FILE = ".package.json";
|
|
14
|
+
export declare const PACKAGE_STORE_MAX_STRATEGIES = 20;
|
|
15
|
+
export declare const PACKAGE_STORE_MAX_BYTES: number;
|
|
16
|
+
export declare const PACKAGE_PAGE_CHARS = 32000;
|
|
17
|
+
export type ImportVerdict = "PASS" | "FAIL";
|
|
18
|
+
export type LiveVerdict = "PASS" | "UNPROVEN" | "FAIL";
|
|
19
|
+
export interface PackageFinding {
|
|
20
|
+
code: string;
|
|
21
|
+
severity: string;
|
|
22
|
+
stage: string;
|
|
23
|
+
file?: string;
|
|
24
|
+
line?: number;
|
|
25
|
+
yamlPath?: string;
|
|
26
|
+
what: string;
|
|
27
|
+
why: string;
|
|
28
|
+
fix: string;
|
|
29
|
+
}
|
|
30
|
+
export interface LiveRecord {
|
|
31
|
+
status: "running" | "done" | "interrupted";
|
|
32
|
+
startedAt: string;
|
|
33
|
+
finishedAt?: string;
|
|
34
|
+
verdict?: LiveVerdict;
|
|
35
|
+
stagesRun?: string[];
|
|
36
|
+
findings?: PackageFinding[];
|
|
37
|
+
measurements?: Array<{
|
|
38
|
+
scanner: string;
|
|
39
|
+
callsPerTick: number;
|
|
40
|
+
cpuSecondsPerTick: number;
|
|
41
|
+
peakRssMb: number;
|
|
42
|
+
}>;
|
|
43
|
+
}
|
|
44
|
+
export interface PackageMeta {
|
|
45
|
+
packageId: string;
|
|
46
|
+
sha256: string;
|
|
47
|
+
strategyId: string;
|
|
48
|
+
uploadedAt: string;
|
|
49
|
+
bytes: number;
|
|
50
|
+
files: Array<{
|
|
51
|
+
path: string;
|
|
52
|
+
bytes: number;
|
|
53
|
+
sha256: string;
|
|
54
|
+
}>;
|
|
55
|
+
settings: StrategySettings;
|
|
56
|
+
importVerdict: ImportVerdict;
|
|
57
|
+
importFindings: PackageFinding[];
|
|
58
|
+
live?: LiveRecord;
|
|
59
|
+
}
|
|
60
|
+
export interface PackageStoreDeps {
|
|
61
|
+
root: string;
|
|
62
|
+
runtimeVersion: string;
|
|
63
|
+
/** `senpi validate` to the import depth on an instance dir (no credentials, no wallet). */
|
|
64
|
+
importCheck(instanceDir: string): Promise<ValidateResult>;
|
|
65
|
+
/** One live tick on an instance dir, as a batch validates each variant. */
|
|
66
|
+
liveCheck(instanceDir: string): Promise<ValidateResult>;
|
|
67
|
+
/** The package dir a simulation on this box runs from, or undefined. */
|
|
68
|
+
simulationPackageDir(simulationId: string): Promise<string | undefined>;
|
|
69
|
+
now(): Date;
|
|
70
|
+
log(line: string): void;
|
|
71
|
+
}
|
|
72
|
+
export interface PutAnswer {
|
|
73
|
+
stored: boolean;
|
|
74
|
+
packageId: string | null;
|
|
75
|
+
strategyId: string | null;
|
|
76
|
+
runtimeVersion: string;
|
|
77
|
+
files: number;
|
|
78
|
+
bytes: number;
|
|
79
|
+
replaced?: string;
|
|
80
|
+
settings: StrategySettings;
|
|
81
|
+
validation: {
|
|
82
|
+
stage: "import";
|
|
83
|
+
verdict: ImportVerdict;
|
|
84
|
+
findings: PackageFinding[];
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
export declare class PackageStore {
|
|
88
|
+
private readonly deps;
|
|
89
|
+
private running;
|
|
90
|
+
private ready;
|
|
91
|
+
/** Touches nothing on disk: the store is made at plugin registration, and the dir on first use. */
|
|
92
|
+
constructor(deps: PackageStoreDeps);
|
|
93
|
+
/** First use: make the dir, drop half-written uploads and validation copies, mark live checks cut short. */
|
|
94
|
+
private ensure;
|
|
95
|
+
private sweep;
|
|
96
|
+
private metaPath;
|
|
97
|
+
private readMeta;
|
|
98
|
+
private writeMeta;
|
|
99
|
+
private metas;
|
|
100
|
+
private byPackageId;
|
|
101
|
+
/** The stored package with this id, for a batch: its dir and its last live verdict. */
|
|
102
|
+
uploaded(packageId: string): {
|
|
103
|
+
dir: string;
|
|
104
|
+
strategyId: string;
|
|
105
|
+
liveVerdict: LiveVerdict | undefined;
|
|
106
|
+
} | undefined;
|
|
107
|
+
private answerFor;
|
|
108
|
+
put(params: Record<string, unknown> | undefined): Promise<PutAnswer>;
|
|
109
|
+
/** Over the caps: drop the least recently uploaded strategies, never `keep`. */
|
|
110
|
+
private evict;
|
|
111
|
+
list(): {
|
|
112
|
+
packages: Array<Record<string, unknown>>;
|
|
113
|
+
};
|
|
114
|
+
get(params: Record<string, unknown> | undefined): Promise<Record<string, unknown>>;
|
|
115
|
+
private liveAnswer;
|
|
116
|
+
/** Start a live check of a stored package, or read the one already run. */
|
|
117
|
+
validate(params: Record<string, unknown> | undefined): Promise<Record<string, unknown>>;
|
|
118
|
+
/** Test hook: wait for the running live check, if any. */
|
|
119
|
+
idle(): Promise<void>;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/deploy/packages/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAMH,OAAO,EAAoB,KAAK,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAErF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAI5D,eAAO,MAAM,iBAAiB,kBAAkB,CAAC;AACjD,eAAO,MAAM,4BAA4B,KAAK,CAAC;AAC/C,eAAO,MAAM,uBAAuB,QAAmB,CAAC;AACxD,eAAO,MAAM,kBAAkB,QAAS,CAAC;AAIzC,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,CAAC;AAC5C,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,UAAU,GAAG,MAAM,CAAC;AAEvD,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,SAAS,GAAG,MAAM,GA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QAAE,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;KAAE;IAc9C,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IA+ExF,OAAO,CAAC,UAAU;IAKlB,2EAA2E;IACrE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IA4E7F,0DAA0D;IACpD,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAG5B"}
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strategy packages an outside agent uploads to this box (`senpi.packages.*`), for simulations only.
|
|
3
|
+
*
|
|
4
|
+
* One package per strategy id under `<baseStateDir>/uploaded-packages/<strategyId>/`, with a
|
|
5
|
+
* `.package.json` beside the files. An upload is checked (load, one instance, `senpi validate` to
|
|
6
|
+
* the import depth) in a staging dir and kept only when clean; a clean upload of the same strategy
|
|
7
|
+
* id replaces the one before it. A live check (`validate`) runs one tick on a copy, one at a time
|
|
8
|
+
* per box, and its verdict is kept with the package. `.package.json` is copied into every variant a
|
|
9
|
+
* batch makes from the package, which is how a live deploy of uploaded code is recognised and refused.
|
|
10
|
+
*/
|
|
11
|
+
import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { join, relative, sep } from "node:path";
|
|
13
|
+
import { parse as parseYaml } from "yaml";
|
|
14
|
+
import { strategySettings } from "../../strategy/settings.js";
|
|
15
|
+
import { loadDeployPackage, readRuntimeYaml } from "../package.js";
|
|
16
|
+
import { checkPackageFiles, packageDigest, PACKAGE_ID_RE, PackageRefusal, sha256 } from "./files.js";
|
|
17
|
+
export const PACKAGE_META_FILE = ".package.json";
|
|
18
|
+
export const PACKAGE_STORE_MAX_STRATEGIES = 20;
|
|
19
|
+
export const PACKAGE_STORE_MAX_BYTES = 10 * 1024 * 1024;
|
|
20
|
+
export const PACKAGE_PAGE_CHARS = 32_000;
|
|
21
|
+
const STRATEGY_ID_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
22
|
+
const SIMULATION_ID_RE = /^simulation-[0-9a-f-]{36}$/i;
|
|
23
|
+
function toFinding(f) {
|
|
24
|
+
return {
|
|
25
|
+
code: f.code,
|
|
26
|
+
severity: f.severity,
|
|
27
|
+
stage: f.stage,
|
|
28
|
+
...(f.where?.file !== undefined ? { file: f.where.file } : {}),
|
|
29
|
+
...(f.where?.line !== undefined ? { line: f.where.line } : {}),
|
|
30
|
+
...(f.where?.yaml_path !== undefined ? { yamlPath: f.where.yaml_path } : {}),
|
|
31
|
+
what: f.what,
|
|
32
|
+
why: f.why,
|
|
33
|
+
fix: f.fix,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function layout(message) {
|
|
37
|
+
return new PackageRefusal("E_PACKAGE_LAYOUT", message);
|
|
38
|
+
}
|
|
39
|
+
function settingsOf(runtimeYaml) {
|
|
40
|
+
try {
|
|
41
|
+
return strategySettings((parseYaml(runtimeYaml) ?? {}));
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return {};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** Every file under `dir` a package holds: no dot names at any depth, sorted by path. */
|
|
48
|
+
function walk(dir) {
|
|
49
|
+
const out = [];
|
|
50
|
+
const visit = (d) => {
|
|
51
|
+
for (const name of readdirSync(d)) {
|
|
52
|
+
if (name.startsWith("."))
|
|
53
|
+
continue;
|
|
54
|
+
const full = join(d, name);
|
|
55
|
+
const st = statSync(full);
|
|
56
|
+
if (st.isDirectory())
|
|
57
|
+
visit(full);
|
|
58
|
+
else if (st.isFile())
|
|
59
|
+
out.push(relative(dir, full).split(sep).join("/"));
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
visit(dir);
|
|
63
|
+
return out.sort();
|
|
64
|
+
}
|
|
65
|
+
export class PackageStore {
|
|
66
|
+
deps;
|
|
67
|
+
running;
|
|
68
|
+
ready = false;
|
|
69
|
+
/** Touches nothing on disk: the store is made at plugin registration, and the dir on first use. */
|
|
70
|
+
constructor(deps) {
|
|
71
|
+
this.deps = deps;
|
|
72
|
+
}
|
|
73
|
+
/** First use: make the dir, drop half-written uploads and validation copies, mark live checks cut short. */
|
|
74
|
+
ensure() {
|
|
75
|
+
if (this.ready)
|
|
76
|
+
return;
|
|
77
|
+
mkdirSync(this.deps.root, { recursive: true });
|
|
78
|
+
this.sweep();
|
|
79
|
+
this.ready = true;
|
|
80
|
+
}
|
|
81
|
+
sweep() {
|
|
82
|
+
for (const name of readdirSync(this.deps.root)) {
|
|
83
|
+
if (name.startsWith(".")) {
|
|
84
|
+
rmSync(join(this.deps.root, name), { recursive: true, force: true });
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const meta = this.readMeta(name);
|
|
88
|
+
if (meta?.live?.status === "running") {
|
|
89
|
+
meta.live = { ...meta.live, status: "interrupted" };
|
|
90
|
+
this.writeMeta(name, meta);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
metaPath(strategyId) {
|
|
95
|
+
return join(this.deps.root, strategyId, PACKAGE_META_FILE);
|
|
96
|
+
}
|
|
97
|
+
readMeta(strategyId) {
|
|
98
|
+
try {
|
|
99
|
+
return JSON.parse(readFileSync(this.metaPath(strategyId), "utf8"));
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
writeMeta(strategyId, meta) {
|
|
106
|
+
const path = this.metaPath(strategyId);
|
|
107
|
+
writeFileSync(`${path}.tmp`, JSON.stringify(meta, null, 2));
|
|
108
|
+
renameSync(`${path}.tmp`, path);
|
|
109
|
+
}
|
|
110
|
+
metas() {
|
|
111
|
+
this.ensure();
|
|
112
|
+
return readdirSync(this.deps.root)
|
|
113
|
+
.filter((name) => !name.startsWith("."))
|
|
114
|
+
.map((name) => this.readMeta(name))
|
|
115
|
+
.filter((m) => m !== undefined);
|
|
116
|
+
}
|
|
117
|
+
byPackageId(packageId) {
|
|
118
|
+
return this.metas().find((m) => m.packageId === packageId);
|
|
119
|
+
}
|
|
120
|
+
/** The stored package with this id, for a batch: its dir and its last live verdict. */
|
|
121
|
+
uploaded(packageId) {
|
|
122
|
+
const meta = this.byPackageId(packageId);
|
|
123
|
+
if (!meta)
|
|
124
|
+
return undefined;
|
|
125
|
+
return { dir: join(this.deps.root, meta.strategyId), strategyId: meta.strategyId, liveVerdict: meta.live?.status === "done" ? meta.live.verdict : undefined };
|
|
126
|
+
}
|
|
127
|
+
answerFor(meta, replaced) {
|
|
128
|
+
return {
|
|
129
|
+
stored: true,
|
|
130
|
+
packageId: meta.packageId,
|
|
131
|
+
strategyId: meta.strategyId,
|
|
132
|
+
runtimeVersion: this.deps.runtimeVersion,
|
|
133
|
+
files: meta.files.length,
|
|
134
|
+
bytes: meta.bytes,
|
|
135
|
+
...(replaced ? { replaced } : {}),
|
|
136
|
+
settings: meta.settings,
|
|
137
|
+
validation: { stage: "import", verdict: meta.importVerdict, findings: meta.importFindings },
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
async put(params) {
|
|
141
|
+
const files = checkPackageFiles(params?.files);
|
|
142
|
+
this.ensure();
|
|
143
|
+
const { packageId, sha256: digest } = packageDigest(files);
|
|
144
|
+
const bytes = files.reduce((n, f) => n + Buffer.byteLength(f.content, "utf8"), 0);
|
|
145
|
+
const same = this.byPackageId(packageId);
|
|
146
|
+
if (same)
|
|
147
|
+
return this.answerFor(same);
|
|
148
|
+
const staging = mkdtempSync(join(this.deps.root, `.put-${packageId}-`));
|
|
149
|
+
try {
|
|
150
|
+
for (const f of files) {
|
|
151
|
+
const out = join(staging, ...f.path.split("/"));
|
|
152
|
+
mkdirSync(join(out, ".."), { recursive: true });
|
|
153
|
+
writeFileSync(out, f.content, "utf8");
|
|
154
|
+
}
|
|
155
|
+
let pkg;
|
|
156
|
+
try {
|
|
157
|
+
pkg = loadDeployPackage(staging);
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
throw layout(`the package does not load: ${err instanceof Error ? err.message.replaceAll(staging, "<package>") : String(err)}`);
|
|
161
|
+
}
|
|
162
|
+
if (pkg.instances.length !== 1) {
|
|
163
|
+
throw layout(`a package for simulations holds one instance; this one has ${pkg.instances.length} (${pkg.instances.map((i) => i.name).join(", ")}).`);
|
|
164
|
+
}
|
|
165
|
+
if (!STRATEGY_ID_RE.test(pkg.id)) {
|
|
166
|
+
throw layout(`strategy.yaml id "${pkg.id}" must be 1-64 lowercase letters, digits, "_" or "-", starting with a letter or digit.`);
|
|
167
|
+
}
|
|
168
|
+
const instance = pkg.instances[0];
|
|
169
|
+
const result = await this.deps.importCheck(instance.runtimeYamlDir);
|
|
170
|
+
const findings = result.findings.map(toFinding);
|
|
171
|
+
const clean = result.verdict !== "FAIL" && !result.findings.some((f) => f.severity === "error");
|
|
172
|
+
if (!clean) {
|
|
173
|
+
this.deps.log(`package upload ${packageId} (${pkg.id}) not stored: import check ${result.verdict}`);
|
|
174
|
+
return {
|
|
175
|
+
stored: false,
|
|
176
|
+
packageId: null,
|
|
177
|
+
strategyId: pkg.id,
|
|
178
|
+
runtimeVersion: this.deps.runtimeVersion,
|
|
179
|
+
files: files.length,
|
|
180
|
+
bytes,
|
|
181
|
+
settings: {},
|
|
182
|
+
validation: { stage: "import", verdict: "FAIL", findings },
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
const meta = {
|
|
186
|
+
packageId,
|
|
187
|
+
sha256: digest,
|
|
188
|
+
strategyId: pkg.id,
|
|
189
|
+
uploadedAt: this.deps.now().toISOString(),
|
|
190
|
+
bytes,
|
|
191
|
+
files: files.map((f) => ({ path: f.path, bytes: Buffer.byteLength(f.content, "utf8"), sha256: sha256(f.content) })),
|
|
192
|
+
settings: settingsOf(readRuntimeYaml(instance)),
|
|
193
|
+
importVerdict: "PASS",
|
|
194
|
+
importFindings: findings,
|
|
195
|
+
};
|
|
196
|
+
writeFileSync(join(staging, PACKAGE_META_FILE), JSON.stringify(meta, null, 2));
|
|
197
|
+
// From here to the end of the swap nothing awaits: a batch copying this strategy's package
|
|
198
|
+
// reads either the old files or the new ones, never a mix.
|
|
199
|
+
const dest = join(this.deps.root, pkg.id);
|
|
200
|
+
const previous = this.readMeta(pkg.id);
|
|
201
|
+
let old;
|
|
202
|
+
if (existsSync(dest)) {
|
|
203
|
+
old = join(this.deps.root, `.old-${pkg.id}-${packageId}`);
|
|
204
|
+
rmSync(old, { recursive: true, force: true });
|
|
205
|
+
renameSync(dest, old);
|
|
206
|
+
}
|
|
207
|
+
renameSync(staging, dest);
|
|
208
|
+
if (old)
|
|
209
|
+
rmSync(old, { recursive: true, force: true });
|
|
210
|
+
this.evict(pkg.id);
|
|
211
|
+
this.deps.log(`package upload ${packageId} stored as ${pkg.id} (${files.length} files, ${bytes} bytes)${previous ? `, replaced ${previous.packageId}` : ""}`);
|
|
212
|
+
return this.answerFor(meta, previous && previous.packageId !== packageId ? previous.packageId : undefined);
|
|
213
|
+
}
|
|
214
|
+
finally {
|
|
215
|
+
rmSync(staging, { recursive: true, force: true });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/** Over the caps: drop the least recently uploaded strategies, never `keep`. */
|
|
219
|
+
evict(keep) {
|
|
220
|
+
const metas = this.metas().sort((a, b) => (a.uploadedAt < b.uploadedAt ? -1 : 1));
|
|
221
|
+
let count = metas.length;
|
|
222
|
+
let total = metas.reduce((n, m) => n + m.bytes, 0);
|
|
223
|
+
for (const m of metas) {
|
|
224
|
+
if (count <= PACKAGE_STORE_MAX_STRATEGIES && total <= PACKAGE_STORE_MAX_BYTES)
|
|
225
|
+
break;
|
|
226
|
+
if (m.strategyId === keep)
|
|
227
|
+
continue;
|
|
228
|
+
rmSync(join(this.deps.root, m.strategyId), { recursive: true, force: true });
|
|
229
|
+
this.deps.log(`package ${m.packageId} (${m.strategyId}) evicted: over ${PACKAGE_STORE_MAX_STRATEGIES} strategies or ${PACKAGE_STORE_MAX_BYTES} bytes`);
|
|
230
|
+
count -= 1;
|
|
231
|
+
total -= m.bytes;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
list() {
|
|
235
|
+
const packages = this.metas()
|
|
236
|
+
.sort((a, b) => (a.uploadedAt < b.uploadedAt ? 1 : -1))
|
|
237
|
+
.map((m) => ({
|
|
238
|
+
strategyId: m.strategyId,
|
|
239
|
+
packageId: m.packageId,
|
|
240
|
+
uploadedAt: m.uploadedAt,
|
|
241
|
+
files: m.files.length,
|
|
242
|
+
bytes: m.bytes,
|
|
243
|
+
lastVerdict: m.live?.status === "done" ? m.live.verdict : null,
|
|
244
|
+
}));
|
|
245
|
+
return { packages };
|
|
246
|
+
}
|
|
247
|
+
async get(params) {
|
|
248
|
+
const p = params ?? {};
|
|
249
|
+
const keys = ["packageId", "strategyId", "simulationId"].filter((k) => p[k] !== undefined);
|
|
250
|
+
if (keys.length !== 1)
|
|
251
|
+
throw new PackageRefusal("INVALID_REQUEST", "send exactly one of packageId, strategyId or simulationId.");
|
|
252
|
+
const key = keys[0];
|
|
253
|
+
const value = p[key];
|
|
254
|
+
if (typeof value !== "string")
|
|
255
|
+
throw new PackageRefusal("INVALID_REQUEST", `${key} must be a string.`);
|
|
256
|
+
let dir;
|
|
257
|
+
let head;
|
|
258
|
+
if (key === "simulationId") {
|
|
259
|
+
if (!SIMULATION_ID_RE.test(value))
|
|
260
|
+
throw new PackageRefusal("INVALID_REQUEST", "simulationId is \"simulation-\" and a UUID.");
|
|
261
|
+
const found = await this.deps.simulationPackageDir(value);
|
|
262
|
+
if (!found)
|
|
263
|
+
throw new PackageRefusal("E_PACKAGE_NOT_FOUND", `simulation ${value} does not run on this box.`);
|
|
264
|
+
dir = found;
|
|
265
|
+
const meta = (() => {
|
|
266
|
+
try {
|
|
267
|
+
return JSON.parse(readFileSync(join(dir, PACKAGE_META_FILE), "utf8"));
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
272
|
+
})();
|
|
273
|
+
const pkg = loadDeployPackage(dir);
|
|
274
|
+
head = {
|
|
275
|
+
packageId: meta?.packageId ?? null,
|
|
276
|
+
strategyId: pkg.id,
|
|
277
|
+
source: "simulation",
|
|
278
|
+
uploadedAt: meta?.uploadedAt ?? null,
|
|
279
|
+
settings: pkg.instances.length === 1 ? settingsOf(readRuntimeYaml(pkg.instances[0])) : {},
|
|
280
|
+
lastVerdict: null,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
const meta = key === "packageId"
|
|
285
|
+
? PACKAGE_ID_RE.test(value)
|
|
286
|
+
? this.byPackageId(value)
|
|
287
|
+
: undefined
|
|
288
|
+
: STRATEGY_ID_RE.test(value)
|
|
289
|
+
? this.readMeta(value)
|
|
290
|
+
: undefined;
|
|
291
|
+
if (!meta)
|
|
292
|
+
throw new PackageRefusal("E_PACKAGE_NOT_FOUND", `no uploaded package ${value} on this box; upload it again.`);
|
|
293
|
+
dir = join(this.deps.root, meta.strategyId);
|
|
294
|
+
head = {
|
|
295
|
+
packageId: meta.packageId,
|
|
296
|
+
strategyId: meta.strategyId,
|
|
297
|
+
source: "upload",
|
|
298
|
+
uploadedAt: meta.uploadedAt,
|
|
299
|
+
settings: meta.settings,
|
|
300
|
+
lastVerdict: meta.live?.status === "done" ? meta.live.verdict : null,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
const paths = walk(dir);
|
|
304
|
+
if (p.path === undefined) {
|
|
305
|
+
return {
|
|
306
|
+
...head,
|
|
307
|
+
files: paths.map((path) => {
|
|
308
|
+
const buf = readFileSync(join(dir, ...path.split("/")));
|
|
309
|
+
return { path, bytes: buf.length, sha256: sha256(buf) };
|
|
310
|
+
}),
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
if (typeof p.path !== "string" || !paths.includes(p.path)) {
|
|
314
|
+
throw new PackageRefusal("E_PACKAGE_NOT_FOUND", `the package has no file "${String(p.path).slice(0, 140)}"; read the file list first.`);
|
|
315
|
+
}
|
|
316
|
+
const offset = p.offset === undefined ? 0 : p.offset;
|
|
317
|
+
if (typeof offset !== "number" || !Number.isInteger(offset) || offset < 0)
|
|
318
|
+
throw new PackageRefusal("INVALID_REQUEST", "offset must be a whole number of characters, 0 or more.");
|
|
319
|
+
const text = readFileSync(join(dir, ...p.path.split("/")), "utf8");
|
|
320
|
+
const end = Math.min(text.length, offset + PACKAGE_PAGE_CHARS);
|
|
321
|
+
return {
|
|
322
|
+
packageId: head.packageId,
|
|
323
|
+
path: p.path,
|
|
324
|
+
offset,
|
|
325
|
+
content: text.slice(offset, end),
|
|
326
|
+
nextOffset: end < text.length ? end : null,
|
|
327
|
+
bytes: Buffer.byteLength(text, "utf8"),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
liveAnswer(meta) {
|
|
331
|
+
const live = meta.live;
|
|
332
|
+
return { packageId: meta.packageId, ...live };
|
|
333
|
+
}
|
|
334
|
+
/** Start a live check of a stored package, or read the one already run. */
|
|
335
|
+
async validate(params) {
|
|
336
|
+
const packageId = params?.packageId;
|
|
337
|
+
if (typeof packageId !== "string" || !PACKAGE_ID_RE.test(packageId)) {
|
|
338
|
+
throw new PackageRefusal("INVALID_REQUEST", "packageId must be \"pkg-\" and 16 hex characters, as the upload answered it.");
|
|
339
|
+
}
|
|
340
|
+
const meta = this.byPackageId(packageId);
|
|
341
|
+
if (!meta)
|
|
342
|
+
throw new PackageRefusal("E_PACKAGE_NOT_FOUND", `no uploaded package ${packageId} on this box; upload it again.`);
|
|
343
|
+
if (meta.live && (meta.live.status === "done" || (meta.live.status === "running" && this.running?.packageId === packageId))) {
|
|
344
|
+
return this.liveAnswer(meta);
|
|
345
|
+
}
|
|
346
|
+
if (this.running) {
|
|
347
|
+
throw new PackageRefusal("E_PACKAGE_BUSY", `another package (${this.running.packageId}) is being validated on this box; try again in a minute.`);
|
|
348
|
+
}
|
|
349
|
+
const copy = mkdtempSync(join(this.deps.root, `.validate-${packageId}-`));
|
|
350
|
+
cpSync(join(this.deps.root, meta.strategyId), copy, { recursive: true });
|
|
351
|
+
meta.live = { status: "running", startedAt: this.deps.now().toISOString() };
|
|
352
|
+
this.writeMeta(meta.strategyId, meta);
|
|
353
|
+
const strategyId = meta.strategyId;
|
|
354
|
+
const done = (async () => {
|
|
355
|
+
let record;
|
|
356
|
+
try {
|
|
357
|
+
const pkg = loadDeployPackage(copy);
|
|
358
|
+
const result = await this.deps.liveCheck(pkg.instances[0].runtimeYamlDir);
|
|
359
|
+
record = {
|
|
360
|
+
status: "done",
|
|
361
|
+
startedAt: meta.live.startedAt,
|
|
362
|
+
finishedAt: this.deps.now().toISOString(),
|
|
363
|
+
verdict: result.verdict,
|
|
364
|
+
stagesRun: [...result.stages_run],
|
|
365
|
+
findings: result.findings.map(toFinding),
|
|
366
|
+
...(result.measurements
|
|
367
|
+
? {
|
|
368
|
+
measurements: result.measurements.map((m) => ({
|
|
369
|
+
scanner: m.scanner,
|
|
370
|
+
callsPerTick: m.calls_per_tick,
|
|
371
|
+
cpuSecondsPerTick: m.cpu_seconds_per_tick,
|
|
372
|
+
peakRssMb: m.peak_rss_mb,
|
|
373
|
+
})),
|
|
374
|
+
}
|
|
375
|
+
: {}),
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
catch (err) {
|
|
379
|
+
record = {
|
|
380
|
+
status: "done",
|
|
381
|
+
startedAt: meta.live.startedAt,
|
|
382
|
+
finishedAt: this.deps.now().toISOString(),
|
|
383
|
+
verdict: "FAIL",
|
|
384
|
+
stagesRun: [],
|
|
385
|
+
findings: [
|
|
386
|
+
{
|
|
387
|
+
code: "E_PACKAGE_VALIDATE_ERROR",
|
|
388
|
+
severity: "error",
|
|
389
|
+
stage: "live",
|
|
390
|
+
what: `the live check could not run: ${err instanceof Error ? err.message.replaceAll(copy, "<package>") : String(err)}`,
|
|
391
|
+
why: "nothing about the package was proven",
|
|
392
|
+
fix: "upload the package again; if it repeats, report it",
|
|
393
|
+
},
|
|
394
|
+
],
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
finally {
|
|
398
|
+
rmSync(copy, { recursive: true, force: true });
|
|
399
|
+
}
|
|
400
|
+
// The package may have been replaced while it ran: the verdict belongs to this id only.
|
|
401
|
+
const now = this.readMeta(strategyId);
|
|
402
|
+
if (now?.packageId === packageId) {
|
|
403
|
+
now.live = record;
|
|
404
|
+
this.writeMeta(strategyId, now);
|
|
405
|
+
}
|
|
406
|
+
this.deps.log(`package ${packageId} live check ${record.verdict}`);
|
|
407
|
+
this.running = undefined;
|
|
408
|
+
})();
|
|
409
|
+
this.running = { packageId, done };
|
|
410
|
+
return this.liveAnswer(meta);
|
|
411
|
+
}
|
|
412
|
+
/** Test hook: wait for the running live check, if any. */
|
|
413
|
+
async idle() {
|
|
414
|
+
await this.running?.done;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
//# sourceMappingURL=store.js.map
|