@malloy-publisher/server 0.0.197 → 0.0.198
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.docker.md +47 -0
- package/dist/app/api-doc.yaml +3 -20
- package/dist/app/assets/{EnvironmentPage-BVkQH_xQ.js → EnvironmentPage-C7rtH4mC.js} +1 -1
- package/dist/app/assets/{HomePage-BgH9UkjK.js → HomePage-DwkH7OrS.js} +1 -1
- package/dist/app/assets/{MainPage-DiBxABem.js → MainPage-D38LtZDV.js} +1 -1
- package/dist/app/assets/{ModelPage-oS70fj83.js → ModelPage-DOol8Mz7.js} +1 -1
- package/dist/app/assets/{PackagePage-F_qLDAdv.js → PackagePage-0tgzA_kO.js} +1 -1
- package/dist/app/assets/{RouteError-WqpffppN.js → RouteError-BaMsOSly.js} +1 -1
- package/dist/app/assets/{WorkbookPage-_YmC-ebR.js → WorkbookPage-Cx4SePkx.js} +1 -1
- package/dist/app/assets/{core-B8L9xCYT.es-BcRLJTnC.js → core-CbsC6R_Y.es-Cwf6asf3.js} +1 -1
- package/dist/app/assets/{index-rg8Ok8nl.js → index-DL6BZTuw.js} +1 -1
- package/dist/app/assets/{index-C3XPaTaS.js → index-DNofXMxi.js} +1 -1
- package/dist/app/assets/{index-BMViiwtJ.js → index-U38AyjJL.js} +3 -3
- package/dist/app/assets/{index.umd-CCAfKkxY.js → index.umd-B68wGGkM.js} +1 -1
- package/dist/app/index.html +1 -1
- package/dist/server.mjs +812 -450
- package/package.json +1 -1
- package/src/config.spec.ts +81 -0
- package/src/config.ts +126 -0
- package/src/controller/package.controller.ts +70 -29
- package/src/errors.ts +13 -0
- package/src/health.ts +0 -26
- package/src/mcp/tools/discovery_tools.ts +6 -2
- package/src/path_safety.spec.ts +158 -0
- package/src/path_safety.ts +140 -0
- package/src/server.ts +13 -0
- package/src/service/environment.ts +614 -198
- package/src/service/environment_admission.spec.ts +180 -0
- package/src/service/environment_store.spec.ts +0 -19
- package/src/service/environment_store.ts +24 -21
- package/src/service/manifest_service.spec.ts +7 -2
- package/src/service/manifest_service.ts +8 -2
- package/src/service/materialization_service.ts +14 -3
- package/src/service/package_memory_governor.spec.ts +173 -0
- package/src/service/package_memory_governor.ts +233 -0
- package/src/service/package_race.spec.ts +208 -0
package/package.json
CHANGED
package/src/config.spec.ts
CHANGED
|
@@ -1081,3 +1081,84 @@ describe("Config path resolution (--config and bundled default)", () => {
|
|
|
1081
1081
|
expect(result.environments).toEqual([]);
|
|
1082
1082
|
});
|
|
1083
1083
|
});
|
|
1084
|
+
|
|
1085
|
+
describe("getMemoryGovernorConfig", () => {
|
|
1086
|
+
const GOVERNOR_ENV_VARS = [
|
|
1087
|
+
"PUBLISHER_MAX_MEMORY_BYTES",
|
|
1088
|
+
"PUBLISHER_MEMORY_HIGH_WATER_FRACTION",
|
|
1089
|
+
"PUBLISHER_MEMORY_LOW_WATER_FRACTION",
|
|
1090
|
+
"PUBLISHER_MEMORY_CHECK_INTERVAL_MS",
|
|
1091
|
+
"PUBLISHER_MEMORY_BACKPRESSURE",
|
|
1092
|
+
];
|
|
1093
|
+
|
|
1094
|
+
beforeEach(() => {
|
|
1095
|
+
for (const v of GOVERNOR_ENV_VARS) delete process.env[v];
|
|
1096
|
+
});
|
|
1097
|
+
afterEach(() => {
|
|
1098
|
+
for (const v of GOVERNOR_ENV_VARS) delete process.env[v];
|
|
1099
|
+
});
|
|
1100
|
+
|
|
1101
|
+
it("returns null when PUBLISHER_MAX_MEMORY_BYTES is unset", async () => {
|
|
1102
|
+
const { getMemoryGovernorConfig } = await import("./config");
|
|
1103
|
+
expect(getMemoryGovernorConfig()).toBeNull();
|
|
1104
|
+
});
|
|
1105
|
+
|
|
1106
|
+
it("parses defaults when only PUBLISHER_MAX_MEMORY_BYTES is set", async () => {
|
|
1107
|
+
process.env.PUBLISHER_MAX_MEMORY_BYTES = String(2 * 1024 * 1024 * 1024);
|
|
1108
|
+
const { getMemoryGovernorConfig } = await import("./config");
|
|
1109
|
+
const cfg = getMemoryGovernorConfig();
|
|
1110
|
+
expect(cfg).not.toBeNull();
|
|
1111
|
+
expect(cfg!.maxMemoryBytes).toBe(2 * 1024 * 1024 * 1024);
|
|
1112
|
+
expect(cfg!.backpressureEnabled).toBe(true);
|
|
1113
|
+
expect(cfg!.highWaterFraction).toBeGreaterThan(cfg!.lowWaterFraction);
|
|
1114
|
+
});
|
|
1115
|
+
|
|
1116
|
+
it("honours fraction and interval overrides", async () => {
|
|
1117
|
+
process.env.PUBLISHER_MAX_MEMORY_BYTES = "1000000000";
|
|
1118
|
+
process.env.PUBLISHER_MEMORY_HIGH_WATER_FRACTION = "0.85";
|
|
1119
|
+
process.env.PUBLISHER_MEMORY_LOW_WATER_FRACTION = "0.7";
|
|
1120
|
+
process.env.PUBLISHER_MEMORY_CHECK_INTERVAL_MS = "10000";
|
|
1121
|
+
process.env.PUBLISHER_MEMORY_BACKPRESSURE = "false";
|
|
1122
|
+
const { getMemoryGovernorConfig } = await import("./config");
|
|
1123
|
+
const cfg = getMemoryGovernorConfig();
|
|
1124
|
+
expect(cfg).not.toBeNull();
|
|
1125
|
+
expect(cfg!.highWaterFraction).toBe(0.85);
|
|
1126
|
+
expect(cfg!.lowWaterFraction).toBe(0.7);
|
|
1127
|
+
expect(cfg!.checkIntervalMs).toBe(10000);
|
|
1128
|
+
expect(cfg!.backpressureEnabled).toBe(false);
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
it("treats PUBLISHER_MAX_MEMORY_BYTES=0 as disabled (returns null)", async () => {
|
|
1132
|
+
process.env.PUBLISHER_MAX_MEMORY_BYTES = "0";
|
|
1133
|
+
const { getMemoryGovernorConfig } = await import("./config");
|
|
1134
|
+
expect(getMemoryGovernorConfig()).toBeNull();
|
|
1135
|
+
});
|
|
1136
|
+
|
|
1137
|
+
it("rejects a negative PUBLISHER_MAX_MEMORY_BYTES", async () => {
|
|
1138
|
+
process.env.PUBLISHER_MAX_MEMORY_BYTES = "-1";
|
|
1139
|
+
const { getMemoryGovernorConfig } = await import("./config");
|
|
1140
|
+
expect(() => getMemoryGovernorConfig()).toThrow();
|
|
1141
|
+
});
|
|
1142
|
+
|
|
1143
|
+
it("rejects low >= high", async () => {
|
|
1144
|
+
process.env.PUBLISHER_MAX_MEMORY_BYTES = "1000000000";
|
|
1145
|
+
process.env.PUBLISHER_MEMORY_HIGH_WATER_FRACTION = "0.7";
|
|
1146
|
+
process.env.PUBLISHER_MEMORY_LOW_WATER_FRACTION = "0.8";
|
|
1147
|
+
const { getMemoryGovernorConfig } = await import("./config");
|
|
1148
|
+
expect(() => getMemoryGovernorConfig()).toThrow();
|
|
1149
|
+
});
|
|
1150
|
+
|
|
1151
|
+
it("rejects an out-of-range fraction", async () => {
|
|
1152
|
+
process.env.PUBLISHER_MAX_MEMORY_BYTES = "1000000000";
|
|
1153
|
+
process.env.PUBLISHER_MEMORY_HIGH_WATER_FRACTION = "1.5";
|
|
1154
|
+
const { getMemoryGovernorConfig } = await import("./config");
|
|
1155
|
+
expect(() => getMemoryGovernorConfig()).toThrow();
|
|
1156
|
+
});
|
|
1157
|
+
|
|
1158
|
+
it("rejects a check interval below the safety floor", async () => {
|
|
1159
|
+
process.env.PUBLISHER_MAX_MEMORY_BYTES = "1000000000";
|
|
1160
|
+
process.env.PUBLISHER_MEMORY_CHECK_INTERVAL_MS = "10";
|
|
1161
|
+
const { getMemoryGovernorConfig } = await import("./config");
|
|
1162
|
+
expect(() => getMemoryGovernorConfig()).toThrow();
|
|
1163
|
+
});
|
|
1164
|
+
});
|
package/src/config.ts
CHANGED
|
@@ -99,6 +99,132 @@ export type ProcessedPublisherConfig = {
|
|
|
99
99
|
environments: ProcessedEnvironment[];
|
|
100
100
|
};
|
|
101
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Tunables for {@link PackageMemoryGovernor}. All values are sourced
|
|
104
|
+
* from environment variables at startup; see {@link getMemoryGovernorConfig}
|
|
105
|
+
* for parsing and defaults.
|
|
106
|
+
*
|
|
107
|
+
* The governor is admission control only: it polls process RSS on
|
|
108
|
+
* `checkIntervalMs` and toggles a single `isBackpressured` flag using
|
|
109
|
+
* a low/high-water hysteresis band. It does NOT evict, unload, or
|
|
110
|
+
* interrupt already-loaded packages — recovery is left to the kernel
|
|
111
|
+
* reclaiming pages as in-flight traffic completes.
|
|
112
|
+
*/
|
|
113
|
+
export interface MemoryGovernorConfig {
|
|
114
|
+
/** Hard ceiling for process RSS in bytes (the OOM-relevant figure). */
|
|
115
|
+
maxMemoryBytes: number;
|
|
116
|
+
/** Fraction of `maxMemoryBytes` at which the governor activates back-pressure (new package loads start returning HTTP 503). Must be in (0, 1) and strictly greater than `lowWaterFraction`. */
|
|
117
|
+
highWaterFraction: number;
|
|
118
|
+
/** Fraction of `maxMemoryBytes` at which the governor clears back-pressure (new package loads admitted again). Must be in (0, 1) and strictly less than `highWaterFraction`; the gap is the hysteresis band that prevents flap. */
|
|
119
|
+
lowWaterFraction: number;
|
|
120
|
+
/** Polling cadence for the RSS sampler, in milliseconds. */
|
|
121
|
+
checkIntervalMs: number;
|
|
122
|
+
/** When true, RSS crossings flip the back-pressure flag. When false, the governor still samples and emits metrics but never rejects requests — useful for a monitoring-only rollout before enabling the 503 behaviour. */
|
|
123
|
+
backpressureEnabled: boolean;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const DEFAULT_HIGH_WATER_FRACTION = 0.8;
|
|
127
|
+
const DEFAULT_LOW_WATER_FRACTION = 0.7;
|
|
128
|
+
const DEFAULT_CHECK_INTERVAL_MS = 5_000;
|
|
129
|
+
const MIN_CHECK_INTERVAL_MS = 100;
|
|
130
|
+
|
|
131
|
+
function parseIntEnv(name: string): number | undefined {
|
|
132
|
+
const raw = process.env[name];
|
|
133
|
+
if (raw === undefined || raw.trim() === "") return undefined;
|
|
134
|
+
const value = Number.parseInt(raw, 10);
|
|
135
|
+
if (!Number.isFinite(value) || String(value) !== raw.trim()) {
|
|
136
|
+
throw new Error(
|
|
137
|
+
`Invalid value for ${name}: expected a base-10 integer, got "${raw}"`,
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function parseFloatEnv(name: string): number | undefined {
|
|
144
|
+
const raw = process.env[name];
|
|
145
|
+
if (raw === undefined || raw.trim() === "") return undefined;
|
|
146
|
+
const value = Number.parseFloat(raw);
|
|
147
|
+
if (!Number.isFinite(value)) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`Invalid value for ${name}: expected a finite number, got "${raw}"`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function parseBoolEnv(name: string): boolean | undefined {
|
|
156
|
+
const raw = process.env[name];
|
|
157
|
+
if (raw === undefined || raw.trim() === "") return undefined;
|
|
158
|
+
const normalised = raw.trim().toLowerCase();
|
|
159
|
+
if (["1", "true", "yes", "on"].includes(normalised)) return true;
|
|
160
|
+
if (["0", "false", "no", "off"].includes(normalised)) return false;
|
|
161
|
+
throw new Error(
|
|
162
|
+
`Invalid value for ${name}: expected a boolean (true/false), got "${raw}"`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Parse memory-governor settings from environment variables and return
|
|
168
|
+
* either a fully-validated config or `null` when the feature is
|
|
169
|
+
* disabled. The feature is disabled iff `PUBLISHER_MAX_MEMORY_BYTES`
|
|
170
|
+
* is unset or set to `0`.
|
|
171
|
+
*
|
|
172
|
+
* Throws at startup on malformed input so a typo in a k8s manifest
|
|
173
|
+
* surfaces as a loud failure rather than silently disabling the cap.
|
|
174
|
+
*/
|
|
175
|
+
export const getMemoryGovernorConfig = (): MemoryGovernorConfig | null => {
|
|
176
|
+
const maxMemoryBytes = parseIntEnv("PUBLISHER_MAX_MEMORY_BYTES");
|
|
177
|
+
if (maxMemoryBytes === undefined || maxMemoryBytes === 0) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
if (maxMemoryBytes < 0) {
|
|
181
|
+
throw new Error(
|
|
182
|
+
`PUBLISHER_MAX_MEMORY_BYTES must be a positive integer (got ${maxMemoryBytes})`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const highWaterFraction =
|
|
187
|
+
parseFloatEnv("PUBLISHER_MEMORY_HIGH_WATER_FRACTION") ??
|
|
188
|
+
DEFAULT_HIGH_WATER_FRACTION;
|
|
189
|
+
const lowWaterFraction =
|
|
190
|
+
parseFloatEnv("PUBLISHER_MEMORY_LOW_WATER_FRACTION") ??
|
|
191
|
+
DEFAULT_LOW_WATER_FRACTION;
|
|
192
|
+
const checkIntervalMs =
|
|
193
|
+
parseIntEnv("PUBLISHER_MEMORY_CHECK_INTERVAL_MS") ??
|
|
194
|
+
DEFAULT_CHECK_INTERVAL_MS;
|
|
195
|
+
const backpressureEnabled =
|
|
196
|
+
parseBoolEnv("PUBLISHER_MEMORY_BACKPRESSURE") ?? true;
|
|
197
|
+
|
|
198
|
+
if (highWaterFraction <= 0 || highWaterFraction >= 1) {
|
|
199
|
+
throw new Error(
|
|
200
|
+
`PUBLISHER_MEMORY_HIGH_WATER_FRACTION must be in (0, 1) (got ${highWaterFraction})`,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
if (lowWaterFraction <= 0 || lowWaterFraction >= 1) {
|
|
204
|
+
throw new Error(
|
|
205
|
+
`PUBLISHER_MEMORY_LOW_WATER_FRACTION must be in (0, 1) (got ${lowWaterFraction})`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
if (lowWaterFraction >= highWaterFraction) {
|
|
209
|
+
throw new Error(
|
|
210
|
+
`PUBLISHER_MEMORY_LOW_WATER_FRACTION (${lowWaterFraction}) must be strictly less than PUBLISHER_MEMORY_HIGH_WATER_FRACTION (${highWaterFraction})`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
if (checkIntervalMs < MIN_CHECK_INTERVAL_MS) {
|
|
214
|
+
throw new Error(
|
|
215
|
+
`PUBLISHER_MEMORY_CHECK_INTERVAL_MS must be >= ${MIN_CHECK_INTERVAL_MS} (got ${checkIntervalMs})`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
maxMemoryBytes,
|
|
221
|
+
highWaterFraction,
|
|
222
|
+
lowWaterFraction,
|
|
223
|
+
checkIntervalMs,
|
|
224
|
+
backpressureEnabled,
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
|
|
102
228
|
function substituteEnvVars(value: string): string {
|
|
103
229
|
const envVarPattern = /\$\{([A-Z_][A-Z0-9_]*)\}/g;
|
|
104
230
|
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import * as path from "path";
|
|
2
1
|
import { components } from "../api";
|
|
3
|
-
import { PUBLISHER_DATA_DIR } from "../constants";
|
|
4
2
|
import { BadRequestError, FrozenConfigError } from "../errors";
|
|
5
3
|
import { logger } from "../logger";
|
|
6
4
|
import { EnvironmentStore } from "../service/environment_store";
|
|
@@ -37,15 +35,38 @@ export class PackageController {
|
|
|
37
35
|
environmentName,
|
|
38
36
|
false,
|
|
39
37
|
);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
38
|
+
|
|
39
|
+
if (reload) {
|
|
40
|
+
// Resolve the package's source location from the currently-cached
|
|
41
|
+
// metadata WITHOUT triggering a stale-state reload. If a `location`
|
|
42
|
+
// is set, route the reload through `installPackage` so that
|
|
43
|
+
// download-then-load happens atomically; otherwise fall back to an
|
|
44
|
+
// in-place reload of the existing on-disk content.
|
|
45
|
+
let location: string | undefined;
|
|
46
|
+
try {
|
|
47
|
+
const cached = await environment.getPackage(packageName, false);
|
|
48
|
+
location = cached.getPackageMetadata().location;
|
|
49
|
+
} catch {
|
|
50
|
+
// Not previously loaded — nothing to reinstall from.
|
|
51
|
+
}
|
|
52
|
+
if (location) {
|
|
53
|
+
const reinstalled = await environment.installPackage(
|
|
54
|
+
packageName,
|
|
55
|
+
(stagingPath) =>
|
|
56
|
+
this.downloadInto(
|
|
57
|
+
environmentName,
|
|
58
|
+
packageName,
|
|
59
|
+
location,
|
|
60
|
+
stagingPath,
|
|
61
|
+
),
|
|
62
|
+
);
|
|
63
|
+
return reinstalled.getPackageMetadata();
|
|
64
|
+
}
|
|
65
|
+
const _package = await environment.getPackage(packageName, true);
|
|
66
|
+
return _package.getPackageMetadata();
|
|
48
67
|
}
|
|
68
|
+
|
|
69
|
+
const _package = await environment.getPackage(packageName, false);
|
|
49
70
|
return _package.getPackageMetadata();
|
|
50
71
|
}
|
|
51
72
|
|
|
@@ -60,21 +81,32 @@ export class PackageController {
|
|
|
60
81
|
if (!body.name) {
|
|
61
82
|
throw new BadRequestError("Package name is required");
|
|
62
83
|
}
|
|
84
|
+
const packageName = body.name;
|
|
63
85
|
const environment = await this.environmentStore.getEnvironment(
|
|
64
86
|
environmentName,
|
|
65
87
|
false,
|
|
66
88
|
);
|
|
89
|
+
let result;
|
|
67
90
|
if (body.location) {
|
|
68
|
-
|
|
91
|
+
const bodyLocation = body.location;
|
|
92
|
+
result = await environment.installPackage(packageName, (stagingPath) =>
|
|
93
|
+
this.downloadInto(
|
|
94
|
+
environmentName,
|
|
95
|
+
packageName,
|
|
96
|
+
bodyLocation,
|
|
97
|
+
stagingPath,
|
|
98
|
+
),
|
|
99
|
+
);
|
|
100
|
+
} else {
|
|
101
|
+
result = await environment.addPackage(packageName);
|
|
69
102
|
}
|
|
70
|
-
const result = await environment.addPackage(body.name);
|
|
71
103
|
await this.environmentStore.addPackageToDatabase(
|
|
72
104
|
environmentName,
|
|
73
|
-
|
|
105
|
+
packageName,
|
|
74
106
|
);
|
|
75
107
|
|
|
76
108
|
if (options?.autoLoadManifest === true) {
|
|
77
|
-
await this.tryLoadExistingManifest(environmentName,
|
|
109
|
+
await this.tryLoadExistingManifest(environmentName, packageName);
|
|
78
110
|
}
|
|
79
111
|
|
|
80
112
|
return result;
|
|
@@ -151,12 +183,20 @@ export class PackageController {
|
|
|
151
183
|
false,
|
|
152
184
|
);
|
|
153
185
|
if (body.location) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
186
|
+
// Re-install: stream the new content into a staging dir (no lock)
|
|
187
|
+
// and atomically swap it in (under the lock).
|
|
188
|
+
const bodyLocation = body.location;
|
|
189
|
+
await environment.installPackage(packageName, (stagingPath) =>
|
|
190
|
+
this.downloadInto(
|
|
191
|
+
environmentName,
|
|
192
|
+
packageName,
|
|
193
|
+
bodyLocation,
|
|
194
|
+
stagingPath,
|
|
195
|
+
),
|
|
158
196
|
);
|
|
159
197
|
}
|
|
198
|
+
// Apply metadata changes (publisher.json) under the same per-package
|
|
199
|
+
// mutex via `Environment.updatePackage`.
|
|
160
200
|
const result = await environment.updatePackage(packageName, body);
|
|
161
201
|
await this.environmentStore.addPackageToDatabase(
|
|
162
202
|
environmentName,
|
|
@@ -166,17 +206,18 @@ export class PackageController {
|
|
|
166
206
|
return result;
|
|
167
207
|
}
|
|
168
208
|
|
|
169
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Run the right downloader for the given location into `targetPath`.
|
|
211
|
+
* This used to point at the canonical package directory, but the
|
|
212
|
+
* install pipeline now passes a sibling staging dir so the long-running
|
|
213
|
+
* download doesn't hold the per-package mutex.
|
|
214
|
+
*/
|
|
215
|
+
private async downloadInto(
|
|
170
216
|
environmentName: string,
|
|
171
217
|
packageName: string,
|
|
172
218
|
packageLocation: string,
|
|
219
|
+
targetPath: string,
|
|
173
220
|
) {
|
|
174
|
-
const absoluteTargetPath = path.join(
|
|
175
|
-
this.environmentStore.serverRootPath,
|
|
176
|
-
PUBLISHER_DATA_DIR,
|
|
177
|
-
environmentName,
|
|
178
|
-
packageName,
|
|
179
|
-
);
|
|
180
221
|
const isCompressedFile = packageLocation.endsWith(".zip");
|
|
181
222
|
if (
|
|
182
223
|
packageLocation.startsWith("https://") ||
|
|
@@ -184,20 +225,20 @@ export class PackageController {
|
|
|
184
225
|
) {
|
|
185
226
|
await this.environmentStore.downloadGitHubDirectory(
|
|
186
227
|
packageLocation,
|
|
187
|
-
|
|
228
|
+
targetPath,
|
|
188
229
|
);
|
|
189
230
|
} else if (packageLocation.startsWith("gs://")) {
|
|
190
231
|
await this.environmentStore.downloadGcsDirectory(
|
|
191
232
|
packageLocation,
|
|
192
233
|
environmentName,
|
|
193
|
-
|
|
234
|
+
targetPath,
|
|
194
235
|
isCompressedFile,
|
|
195
236
|
);
|
|
196
237
|
} else if (packageLocation.startsWith("s3://")) {
|
|
197
238
|
await this.environmentStore.downloadS3Directory(
|
|
198
239
|
packageLocation,
|
|
199
240
|
environmentName,
|
|
200
|
-
|
|
241
|
+
targetPath,
|
|
201
242
|
isCompressedFile,
|
|
202
243
|
);
|
|
203
244
|
}
|
|
@@ -207,7 +248,7 @@ export class PackageController {
|
|
|
207
248
|
// so we need to mount them on the right place.
|
|
208
249
|
await this.environmentStore.mountLocalDirectory(
|
|
209
250
|
packageLocation,
|
|
210
|
-
|
|
251
|
+
targetPath,
|
|
211
252
|
environmentName,
|
|
212
253
|
packageName,
|
|
213
254
|
);
|
package/src/errors.ts
CHANGED
|
@@ -28,6 +28,8 @@ export function internalErrorToHttpError(error: Error) {
|
|
|
28
28
|
return httpError(409, error.message);
|
|
29
29
|
} else if (error instanceof InvalidStateTransitionError) {
|
|
30
30
|
return httpError(409, error.message);
|
|
31
|
+
} else if (error instanceof ServiceUnavailableError) {
|
|
32
|
+
return httpError(503, error.message);
|
|
31
33
|
} else {
|
|
32
34
|
return httpError(500, error.message);
|
|
33
35
|
}
|
|
@@ -122,3 +124,14 @@ export class InvalidStateTransitionError extends Error {
|
|
|
122
124
|
super(message);
|
|
123
125
|
}
|
|
124
126
|
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Thrown when the publisher is temporarily refusing a request to keep
|
|
130
|
+
* RSS under the configured `PUBLISHER_MAX_MEMORY_BYTES` cap. Mapped to
|
|
131
|
+
* HTTP 503 so an upstream proxy / client can retry with back-off.
|
|
132
|
+
*/
|
|
133
|
+
export class ServiceUnavailableError extends Error {
|
|
134
|
+
constructor(message: string) {
|
|
135
|
+
super(message);
|
|
136
|
+
}
|
|
137
|
+
}
|
package/src/health.ts
CHANGED
|
@@ -41,32 +41,6 @@ export function markReady(): void {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
/**
|
|
45
|
-
* Marks the service as degraded: one or more environments failed to
|
|
46
|
-
* initialize. The surviving environments are still queryable, and
|
|
47
|
-
* callers polling /api/v0/status see operationalState="degraded" plus
|
|
48
|
-
* a failedEnvironments list.
|
|
49
|
-
*
|
|
50
|
-
* Readiness probe (/health/readiness) returns 503 — degraded pods are
|
|
51
|
-
* pulled out of K8s load-balancer rotation so traffic does not get
|
|
52
|
-
* routed to a replica that can only serve a fraction of the configured
|
|
53
|
-
* environments. Operators should fix the failing config and restart
|
|
54
|
-
* the pod; if you want degraded traffic to be served anyway (e.g. for
|
|
55
|
-
* a single-replica local dev instance), poll /api/v0/status directly
|
|
56
|
-
* instead of /health/readiness.
|
|
57
|
-
*/
|
|
58
|
-
export function markDegraded(): void {
|
|
59
|
-
if (operationalState !== "draining") {
|
|
60
|
-
operationalState = "degraded";
|
|
61
|
-
ready = false;
|
|
62
|
-
logger.warn(
|
|
63
|
-
"Service marked as degraded; one or more environments failed to initialize. Readiness probe will fail until the config is fixed and the process restarts.",
|
|
64
|
-
);
|
|
65
|
-
} else {
|
|
66
|
-
logger.error("Service is already draining - cannot mark as degraded");
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
44
|
/**
|
|
71
45
|
* Marks the service as not ready (readiness probe will return 503).
|
|
72
46
|
*/
|
|
@@ -222,8 +222,12 @@ export function registerTools(
|
|
|
222
222
|
throw new Error(`Model not found: ${modelPath}`);
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
-
//
|
|
226
|
-
|
|
225
|
+
// Route through the Environment so the disk read is serialized
|
|
226
|
+
// against installPackage / deletePackage.
|
|
227
|
+
const fileText = await environment.getModelFileText(
|
|
228
|
+
packageName,
|
|
229
|
+
modelPath,
|
|
230
|
+
);
|
|
227
231
|
|
|
228
232
|
console.log(
|
|
229
233
|
`[MCP LOG] Successfully retrieved model text for ${modelPath}`,
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
|
|
4
|
+
import { BadRequestError } from "./errors";
|
|
5
|
+
import {
|
|
6
|
+
assertSafeEnvironmentPath,
|
|
7
|
+
assertSafePackageName,
|
|
8
|
+
assertSafeRelativeModelPath,
|
|
9
|
+
safeJoinUnderRoot,
|
|
10
|
+
} from "./path_safety";
|
|
11
|
+
|
|
12
|
+
describe("assertSafePackageName", () => {
|
|
13
|
+
it.each([
|
|
14
|
+
"pkg",
|
|
15
|
+
"test_package",
|
|
16
|
+
"test-package",
|
|
17
|
+
"TestPackage1",
|
|
18
|
+
"test.package.name",
|
|
19
|
+
"a",
|
|
20
|
+
"x".repeat(255),
|
|
21
|
+
])("accepts %p", (name) => {
|
|
22
|
+
expect(() => assertSafePackageName(name)).not.toThrow();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it.each([
|
|
26
|
+
["empty", ""],
|
|
27
|
+
["dot", "."],
|
|
28
|
+
["dot-dot", ".."],
|
|
29
|
+
["leading dot", ".staging"],
|
|
30
|
+
["forward slash", "foo/bar"],
|
|
31
|
+
["backslash", "foo\\bar"],
|
|
32
|
+
["null byte", "foo\0bar"],
|
|
33
|
+
["traversal", "../etc/passwd"],
|
|
34
|
+
["abs", "/etc/passwd"],
|
|
35
|
+
["space", "my pkg"],
|
|
36
|
+
["unicode", "pkg\u202E"],
|
|
37
|
+
["too long", "x".repeat(256)],
|
|
38
|
+
])("rejects %s (%p)", (_label, name) => {
|
|
39
|
+
expect(() => assertSafePackageName(name)).toThrow(BadRequestError);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it.each([
|
|
43
|
+
["number", 42],
|
|
44
|
+
["null", null],
|
|
45
|
+
["undefined", undefined],
|
|
46
|
+
["object", { name: "pkg" }],
|
|
47
|
+
])("rejects non-string %s (%p)", (_label, value) => {
|
|
48
|
+
expect(() => assertSafePackageName(value)).toThrow(BadRequestError);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("assertSafeRelativeModelPath", () => {
|
|
53
|
+
it.each([
|
|
54
|
+
"model.malloy",
|
|
55
|
+
"models/foo.malloy",
|
|
56
|
+
"a/b/c/d.malloynb",
|
|
57
|
+
"deep/nested/file_name-1.malloy",
|
|
58
|
+
])("accepts %p", (modelPath) => {
|
|
59
|
+
expect(() => assertSafeRelativeModelPath(modelPath)).not.toThrow();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it.each([
|
|
63
|
+
["empty", ""],
|
|
64
|
+
["leading slash (absolute)", "/etc/passwd"],
|
|
65
|
+
["traversal", "../etc/passwd"],
|
|
66
|
+
["embedded traversal", "models/../../../etc/passwd"],
|
|
67
|
+
["embedded dot segment", "models/./foo.malloy"],
|
|
68
|
+
["double slash", "models//foo.malloy"],
|
|
69
|
+
["trailing slash", "models/foo/"],
|
|
70
|
+
["backslash", "models\\foo.malloy"],
|
|
71
|
+
["null byte", "models/foo\0.malloy"],
|
|
72
|
+
["dotfile segment", ".staging/foo.malloy"],
|
|
73
|
+
["dotfile leaf", "models/.hidden.malloy"],
|
|
74
|
+
])("rejects %s (%p)", (_label, modelPath) => {
|
|
75
|
+
expect(() => assertSafeRelativeModelPath(modelPath)).toThrow(
|
|
76
|
+
BadRequestError,
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("rejects non-string inputs", () => {
|
|
81
|
+
expect(() => assertSafeRelativeModelPath(undefined)).toThrow(
|
|
82
|
+
BadRequestError,
|
|
83
|
+
);
|
|
84
|
+
expect(() => assertSafeRelativeModelPath(123)).toThrow(BadRequestError);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("assertSafeEnvironmentPath", () => {
|
|
89
|
+
it.each([
|
|
90
|
+
"/etc/publisher",
|
|
91
|
+
"/var/lib/publisher/env1",
|
|
92
|
+
"/Users/me/data",
|
|
93
|
+
"/a",
|
|
94
|
+
"C:\\Users\\me\\publisher",
|
|
95
|
+
"C:/Users/me/publisher",
|
|
96
|
+
])("accepts %p", (p) => {
|
|
97
|
+
expect(() => assertSafeEnvironmentPath(p)).not.toThrow();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it.each([
|
|
101
|
+
["empty", ""],
|
|
102
|
+
["relative", "publisher/data"],
|
|
103
|
+
["traversal in middle", "/var/lib/../../etc/passwd"],
|
|
104
|
+
["traversal at end", "/var/lib/publisher/.."],
|
|
105
|
+
["null byte", "/var/lib/publisher\0"],
|
|
106
|
+
["bare dot-dot", ".."],
|
|
107
|
+
["bare dot", "."],
|
|
108
|
+
["too long", "/" + "a".repeat(5000)],
|
|
109
|
+
])("rejects %s (%p)", (_label, p) => {
|
|
110
|
+
expect(() => assertSafeEnvironmentPath(p)).toThrow(BadRequestError);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("rejects non-string inputs", () => {
|
|
114
|
+
expect(() => assertSafeEnvironmentPath(undefined)).toThrow(
|
|
115
|
+
BadRequestError,
|
|
116
|
+
);
|
|
117
|
+
expect(() => assertSafeEnvironmentPath(null)).toThrow(BadRequestError);
|
|
118
|
+
expect(() => assertSafeEnvironmentPath(42)).toThrow(BadRequestError);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("safeJoinUnderRoot", () => {
|
|
123
|
+
const root = "/tmp/test-root";
|
|
124
|
+
|
|
125
|
+
it("returns the resolved root when joined with no segments", () => {
|
|
126
|
+
expect(safeJoinUnderRoot(root)).toBe(path.resolve(root));
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("joins safe segments into a path under root", () => {
|
|
130
|
+
expect(safeJoinUnderRoot(root, "pkg", "model.malloy")).toBe(
|
|
131
|
+
path.resolve(root, "pkg", "model.malloy"),
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("throws when traversal escapes the root", () => {
|
|
136
|
+
expect(() => safeJoinUnderRoot(root, "..")).toThrow(BadRequestError);
|
|
137
|
+
expect(() => safeJoinUnderRoot(root, "..", "etc", "passwd")).toThrow(
|
|
138
|
+
BadRequestError,
|
|
139
|
+
);
|
|
140
|
+
expect(() => safeJoinUnderRoot(root, "pkg", "..", "..", "etc")).toThrow(
|
|
141
|
+
BadRequestError,
|
|
142
|
+
);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("throws when an absolute segment overrides the root", () => {
|
|
146
|
+
expect(() => safeJoinUnderRoot(root, "/etc/passwd")).toThrow(
|
|
147
|
+
BadRequestError,
|
|
148
|
+
);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("does NOT match a sibling directory with the same prefix", () => {
|
|
152
|
+
// path.resolve("/tmp/test-root", "../test-root-bad") -> "/tmp/test-root-bad"
|
|
153
|
+
// which starts with "/tmp/test-root" textually but is NOT a child.
|
|
154
|
+
expect(() => safeJoinUnderRoot(root, "..", "test-root-bad")).toThrow(
|
|
155
|
+
BadRequestError,
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
});
|