@koed-labs/server 0.0.0-stage → 0.11.1
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/LICENSE +201 -0
- package/README.md +64 -2
- package/bin/koed.js +7 -0
- package/package.json +26 -4
- package/third-party-notices.json +92 -0
- package/vendor/chunk-2XKSVKQA.js +49 -0
- package/vendor/chunk-4OO6VAU3.js +259 -0
- package/vendor/chunk-53ZFCDHP.js +332 -0
- package/vendor/chunk-6JRWGOQG.js +236 -0
- package/vendor/chunk-6KS67EQE.js +75 -0
- package/vendor/chunk-7EGSY3E4.js +8 -0
- package/vendor/chunk-7FSHVVTZ.js +128 -0
- package/vendor/chunk-7NOQSX4T.js +14 -0
- package/vendor/chunk-7NXKPDF3.js +1586 -0
- package/vendor/chunk-ALJUSOF7.js +175 -0
- package/vendor/chunk-AVQCUFYQ.js +16 -0
- package/vendor/chunk-BGENSONI.js +18 -0
- package/vendor/chunk-BTS4ZTF3.js +43 -0
- package/vendor/chunk-BUIM2M4R.js +22 -0
- package/vendor/chunk-C4V72GXC.js +194 -0
- package/vendor/chunk-C5SNDPAP.js +2472 -0
- package/vendor/chunk-CHMH4LH5.js +2497 -0
- package/vendor/chunk-EA4QDDQA.js +58 -0
- package/vendor/chunk-EDPNYIX5.js +5448 -0
- package/vendor/chunk-FN3HUUO2.js +1974 -0
- package/vendor/chunk-FSNLU46W.js +14548 -0
- package/vendor/chunk-FYYRFX4U.js +4423 -0
- package/vendor/chunk-GFASBPV2.js +42 -0
- package/vendor/chunk-HC76426R.js +48 -0
- package/vendor/chunk-IY2HRXOZ.js +99 -0
- package/vendor/chunk-JBGD4JLR.js +3779 -0
- package/vendor/chunk-JYK6UL6D.js +261 -0
- package/vendor/chunk-LRP66XEY.js +13907 -0
- package/vendor/chunk-NOU7RHYZ.js +26 -0
- package/vendor/chunk-NVTMIAAF.js +113 -0
- package/vendor/chunk-O7DW2VAV.js +14 -0
- package/vendor/chunk-OCILJ737.js +299 -0
- package/vendor/chunk-PL3LWC5C.js +754 -0
- package/vendor/chunk-QRKHHWJJ.js +104 -0
- package/vendor/chunk-QT24SAW6.js +110 -0
- package/vendor/chunk-RCR4XPE3.js +26020 -0
- package/vendor/chunk-SHLRINKO.js +73 -0
- package/vendor/chunk-SNKWETM5.js +35 -0
- package/vendor/chunk-SRKIBFOE.js +242 -0
- package/vendor/chunk-T6UB5PNX.js +124 -0
- package/vendor/chunk-TG26CZGQ.js +23 -0
- package/vendor/chunk-TISYUUEJ.js +188 -0
- package/vendor/chunk-TSAYTRX3.js +89 -0
- package/vendor/chunk-TVYEB2GN.js +343 -0
- package/vendor/chunk-UB6JCVQ3.js +461 -0
- package/vendor/chunk-V67IQB7H.js +153 -0
- package/vendor/chunk-VGIYSDNU.js +522 -0
- package/vendor/chunk-W2HNI6AA.js +75 -0
- package/vendor/chunk-W2OJZZ6X.js +20 -0
- package/vendor/chunk-WDFERJG3.js +2208 -0
- package/vendor/chunk-WPIEF4LY.js +1647 -0
- package/vendor/chunk-XIZVLPHZ.js +18 -0
- package/vendor/chunk-YZLQZU5E.js +46 -0
- package/vendor/chunk-Z5WCAUA6.js +148 -0
- package/vendor/chunk-ZIOKV2X6.js +1589 -0
- package/vendor/chunk-ZOHNXUDY.js +12501 -0
- package/vendor/chunk-ZUDSZGVO.js +11 -0
- package/vendor/cli.js +7 -0
- package/vendor/component-manifest-catalog.js +9 -0
- package/vendor/control-plane.js +28 -0
- package/vendor/package.json +1 -0
- package/vendor/privacy-service-bootstrap.js +6 -0
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
import { createRequire as createKoedRequire } from "node:module"; const require = createKoedRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
checkCudaHost,
|
|
4
|
+
nativeCommand,
|
|
5
|
+
validateStagedCuda
|
|
6
|
+
} from "./chunk-TVYEB2GN.js";
|
|
7
|
+
import "./chunk-JYK6UL6D.js";
|
|
8
|
+
import {
|
|
9
|
+
resolveKoedServerConfig
|
|
10
|
+
} from "./chunk-ALJUSOF7.js";
|
|
11
|
+
import {
|
|
12
|
+
resolveKoedRuntimeOwner
|
|
13
|
+
} from "./chunk-V67IQB7H.js";
|
|
14
|
+
import {
|
|
15
|
+
assertDirectoryChain,
|
|
16
|
+
fetchBounded,
|
|
17
|
+
renameAtomically,
|
|
18
|
+
withStoppedRuntime
|
|
19
|
+
} from "./chunk-FN3HUUO2.js";
|
|
20
|
+
import "./chunk-T6UB5PNX.js";
|
|
21
|
+
import "./chunk-7NXKPDF3.js";
|
|
22
|
+
import "./chunk-ZUDSZGVO.js";
|
|
23
|
+
import {
|
|
24
|
+
canonicalArtifactJson,
|
|
25
|
+
extractNativeArchive,
|
|
26
|
+
nativeDigest,
|
|
27
|
+
nativeFileDigest,
|
|
28
|
+
nativeJsonObject,
|
|
29
|
+
readNativeMetadata,
|
|
30
|
+
syncNativeDirectory,
|
|
31
|
+
verifyNativePayload
|
|
32
|
+
} from "./chunk-ZIOKV2X6.js";
|
|
33
|
+
import {
|
|
34
|
+
compareNativeVersions,
|
|
35
|
+
compatibleCuda,
|
|
36
|
+
getNativeRuntimeCatalog,
|
|
37
|
+
nativeKind,
|
|
38
|
+
validateNativeRuntimeCatalog
|
|
39
|
+
} from "./chunk-QRKHHWJJ.js";
|
|
40
|
+
import "./chunk-7EGSY3E4.js";
|
|
41
|
+
import "./chunk-IY2HRXOZ.js";
|
|
42
|
+
import "./chunk-FSNLU46W.js";
|
|
43
|
+
import "./chunk-BTS4ZTF3.js";
|
|
44
|
+
|
|
45
|
+
// packages/koed-server/dist/runtime-release.js
|
|
46
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
47
|
+
import { mkdirSync as mkdirSync2, mkdtempSync, renameSync as renameSync2, rmSync as rmSync2, statfsSync, writeFileSync as writeFileSync2, lstatSync as lstatSync2, readlinkSync as readlinkSync2 } from "node:fs";
|
|
48
|
+
import { dirname as dirname2, resolve as resolve3 } from "node:path";
|
|
49
|
+
|
|
50
|
+
// packages/koed-server/dist/native-selection.js
|
|
51
|
+
import { lstatSync, mkdirSync, readlinkSync, readdirSync, rmSync, renameSync, symlinkSync, writeFileSync } from "node:fs";
|
|
52
|
+
import { dirname, relative, resolve } from "node:path";
|
|
53
|
+
import { randomUUID } from "node:crypto";
|
|
54
|
+
var releaseInstallCommand = "koed runtime install --provider release --dependency-mode bundled-local";
|
|
55
|
+
var nativeStore = (paths) => resolve(paths.koedHome, "runtime/native");
|
|
56
|
+
var nativePathExists = (path) => lstatSync(path, { throwIfNoEntry: false }) !== void 0;
|
|
57
|
+
var payloadRoot = (paths, pin) => resolve(nativeStore(paths), nativeKind(pin), pin.recipeId);
|
|
58
|
+
var coreLinks = [
|
|
59
|
+
"postgres",
|
|
60
|
+
"llama.cpp/cpu",
|
|
61
|
+
"llama.cpp/llama-server"
|
|
62
|
+
];
|
|
63
|
+
var entryNames = ["postgres", "llama.cpp"];
|
|
64
|
+
var document = (pin, cuda) => ({
|
|
65
|
+
schemaVersion: 1,
|
|
66
|
+
core: pin,
|
|
67
|
+
cuda
|
|
68
|
+
});
|
|
69
|
+
function conflictsWithPin(installed, current) {
|
|
70
|
+
return current?.recipeId === installed.recipeId && canonicalArtifactJson({ ...installed, url: current.url }) !== canonicalArtifactJson(current);
|
|
71
|
+
}
|
|
72
|
+
function checkLink(path, target) {
|
|
73
|
+
if (!lstatSync(path).isSymbolicLink() || readlinkSync(path) !== target)
|
|
74
|
+
throw new Error(`Unexpected native runtime link: ${path}`);
|
|
75
|
+
}
|
|
76
|
+
function checkEntryPaths(paths, requireLinks) {
|
|
77
|
+
const runtime = dirname(nativeStore(paths));
|
|
78
|
+
assertDirectoryChain(paths.koedHome, runtime);
|
|
79
|
+
assertDirectoryChain(paths.koedHome, paths.cacheDir);
|
|
80
|
+
const store = nativeStore(paths);
|
|
81
|
+
if (nativePathExists(store) && readdirSync(store).some((name) => !["core", "selections", "cuda", "current"].includes(name) && !/^\.stage-[A-Za-z0-9]{6}$/.test(name) && !/^\.current-[a-f0-9-]{36}$/.test(name)))
|
|
82
|
+
throw new Error("Unrecognized native store contents. Inspect the existing runtime or use a fresh KOED_HOME.");
|
|
83
|
+
for (const name of ["runtime-homebrew.json", "runtime-packaged.json"]) {
|
|
84
|
+
if (nativePathExists(resolve(paths.cacheDir, name)))
|
|
85
|
+
throw new Error("An existing native provider owns this installation. Keep using that provider or use a fresh KOED_HOME.");
|
|
86
|
+
}
|
|
87
|
+
for (const name of entryNames) {
|
|
88
|
+
const path = resolve(runtime, name);
|
|
89
|
+
if (!requireLinks && !nativePathExists(path))
|
|
90
|
+
continue;
|
|
91
|
+
if (!lstatSync(path).isSymbolicLink() || readlinkSync(path) !== `native/current/${name}`)
|
|
92
|
+
throw new Error(`Unrecognized native entry ${path}. Keep using its current provider or choose a fresh KOED_HOME.`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function currentNativeId(paths) {
|
|
96
|
+
const store = nativeStore(paths), pointer = resolve(store, "current");
|
|
97
|
+
assertDirectoryChain(paths.koedHome, store);
|
|
98
|
+
if (!nativePathExists(pointer))
|
|
99
|
+
return null;
|
|
100
|
+
const match = /^selections\/([a-f0-9]{64})$/.exec(readlinkSync(pointer));
|
|
101
|
+
if (!match)
|
|
102
|
+
throw new Error("Invalid native selection pointer.");
|
|
103
|
+
return match[1];
|
|
104
|
+
}
|
|
105
|
+
function loadSelection(paths, id, catalog) {
|
|
106
|
+
const root = resolve(nativeStore(paths), "selections", id);
|
|
107
|
+
assertDirectoryChain(paths.koedHome, resolve(root, "llama.cpp"));
|
|
108
|
+
for (const path of [
|
|
109
|
+
nativeStore(paths),
|
|
110
|
+
dirname(root),
|
|
111
|
+
root,
|
|
112
|
+
resolve(root, "selection.json")
|
|
113
|
+
]) {
|
|
114
|
+
const stat = lstatSync(path);
|
|
115
|
+
if (stat.uid !== process.getuid?.() || (stat.mode & 18) !== 0)
|
|
116
|
+
throw new Error("Native selection ownership or permissions changed.");
|
|
117
|
+
}
|
|
118
|
+
const raw = readNativeMetadata(resolve(root, "selection.json"), 16 * 1024);
|
|
119
|
+
if (nativeDigest(raw) !== id)
|
|
120
|
+
throw new Error("Native selection metadata changed.");
|
|
121
|
+
const record = nativeJsonObject(raw);
|
|
122
|
+
const { core: pin, cuda } = validateNativeRuntimeCatalog({
|
|
123
|
+
schemaVersion: 1,
|
|
124
|
+
minimumCoreVersion: "0.0.0",
|
|
125
|
+
core: record.core,
|
|
126
|
+
cuda: record.cuda
|
|
127
|
+
});
|
|
128
|
+
if (canonicalArtifactJson(document(pin, cuda)) !== raw.toString("utf8") || cuda && conflictsWithPin(cuda, catalog.cuda) || conflictsWithPin(pin, catalog.core))
|
|
129
|
+
throw new Error("Native selection conflicts with the reviewed identity.");
|
|
130
|
+
if (canonicalArtifactJson(readdirSync(root).sort()) !== canonicalArtifactJson(["llama.cpp", "postgres", "selection.json"]) || canonicalArtifactJson(readdirSync(resolve(root, "llama.cpp")).sort()) !== canonicalArtifactJson(cuda ? ["cpu", "cuda", "llama-server"] : ["cpu", "llama-server"]))
|
|
131
|
+
throw new Error("Unexpected native selection contents.");
|
|
132
|
+
const payload = payloadRoot(paths, pin);
|
|
133
|
+
assertDirectoryChain(paths.koedHome, payload);
|
|
134
|
+
for (const name of coreLinks) {
|
|
135
|
+
const path = resolve(root, name);
|
|
136
|
+
checkLink(path, relative(dirname(path), resolve(payload, name)));
|
|
137
|
+
}
|
|
138
|
+
verifyNativePayload(payload, pin);
|
|
139
|
+
if (cuda) {
|
|
140
|
+
const cudaRoot = payloadRoot(paths, cuda), name = "llama.cpp/cuda";
|
|
141
|
+
assertDirectoryChain(paths.koedHome, cudaRoot);
|
|
142
|
+
checkLink(resolve(root, name), relative(resolve(root, "llama.cpp"), resolve(cudaRoot, name)));
|
|
143
|
+
verifyNativePayload(cudaRoot, cuda);
|
|
144
|
+
}
|
|
145
|
+
return { id, root, pin, cuda };
|
|
146
|
+
}
|
|
147
|
+
function readNativeSelection(paths, catalog) {
|
|
148
|
+
const id = currentNativeId(paths);
|
|
149
|
+
checkEntryPaths(paths, id !== null);
|
|
150
|
+
if (!id)
|
|
151
|
+
return null;
|
|
152
|
+
try {
|
|
153
|
+
return loadSelection(paths, id, catalog);
|
|
154
|
+
} catch (cause) {
|
|
155
|
+
const detail = (cause instanceof Error ? cause.message : String(cause)).replace(/\.$/, "");
|
|
156
|
+
throw new Error(`Cannot admit the installed native selection: ${detail}. Stop Koed, preserve the damaged native store for recovery, then run ${releaseInstallCommand}.`, { cause });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function coreStartBlocker(pin, catalog) {
|
|
160
|
+
if (compareNativeVersions(pin.runtimeVersion, catalog.minimumCoreVersion) < 0)
|
|
161
|
+
return `Native core ${pin.runtimeVersion} is below the required ${catalog.minimumCoreVersion}`;
|
|
162
|
+
if (canonicalArtifactJson(pin.contracts) !== canonicalArtifactJson(catalog.core.contracts))
|
|
163
|
+
return `Native core ${pin.runtimeVersion} implements contracts ${canonicalArtifactJson(pin.contracts)}, but this Control Plane requires ${canonicalArtifactJson(catalog.core.contracts)}`;
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
function assertStartableCore(pin, catalog) {
|
|
167
|
+
const blocker = coreStartBlocker(pin, catalog);
|
|
168
|
+
if (blocker)
|
|
169
|
+
throw new Error(`${blocker}. Stop Koed and run ${releaseInstallCommand}.`);
|
|
170
|
+
}
|
|
171
|
+
function composeNativeSelection(paths, pin, staging, catalog, cuda = null) {
|
|
172
|
+
const bytes = canonicalArtifactJson(document(pin, cuda)), id = nativeDigest(bytes);
|
|
173
|
+
const root = resolve(nativeStore(paths), "selections", id);
|
|
174
|
+
assertDirectoryChain(paths.koedHome, root);
|
|
175
|
+
if (!nativePathExists(root)) {
|
|
176
|
+
const fresh = resolve(staging, "selection");
|
|
177
|
+
mkdirSync(resolve(fresh, "llama.cpp"), { recursive: true, mode: 448 });
|
|
178
|
+
writeFileSync(resolve(fresh, "selection.json"), bytes, {
|
|
179
|
+
flag: "wx",
|
|
180
|
+
mode: 384,
|
|
181
|
+
flush: true
|
|
182
|
+
});
|
|
183
|
+
for (const name of coreLinks) {
|
|
184
|
+
const target = relative(dirname(resolve(root, name)), resolve(payloadRoot(paths, pin), name));
|
|
185
|
+
symlinkSync(target, resolve(fresh, name));
|
|
186
|
+
}
|
|
187
|
+
if (cuda) {
|
|
188
|
+
const name = "llama.cpp/cuda";
|
|
189
|
+
symlinkSync(relative(resolve(root, "llama.cpp"), resolve(payloadRoot(paths, cuda), name)), resolve(fresh, name));
|
|
190
|
+
}
|
|
191
|
+
syncNativeDirectory(resolve(fresh, "llama.cpp"));
|
|
192
|
+
syncNativeDirectory(fresh);
|
|
193
|
+
renameSync(fresh, root);
|
|
194
|
+
syncNativeDirectory(dirname(root));
|
|
195
|
+
}
|
|
196
|
+
return loadSelection(paths, id, catalog);
|
|
197
|
+
}
|
|
198
|
+
function activateNativeSelection(paths, id) {
|
|
199
|
+
const store = nativeStore(paths), runtime = dirname(store);
|
|
200
|
+
for (const name of entryNames) {
|
|
201
|
+
const path = resolve(runtime, name), target = `native/current/${name}`;
|
|
202
|
+
if (nativePathExists(path))
|
|
203
|
+
checkLink(path, target);
|
|
204
|
+
else
|
|
205
|
+
symlinkSync(target, path);
|
|
206
|
+
}
|
|
207
|
+
syncNativeDirectory(runtime);
|
|
208
|
+
syncNativeDirectory(paths.koedHome);
|
|
209
|
+
const tmp = resolve(store, `.current-${randomUUID()}`);
|
|
210
|
+
symlinkSync(`selections/${id}`, tmp);
|
|
211
|
+
try {
|
|
212
|
+
renameAtomically(tmp, resolve(store, "current"));
|
|
213
|
+
syncNativeDirectory(store);
|
|
214
|
+
} finally {
|
|
215
|
+
rmSync(tmp, { force: true });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// packages/koed-server/dist/runtime-release-assets.js
|
|
220
|
+
import { resolve as resolve2 } from "node:path";
|
|
221
|
+
function embeddingPolicy(paths, environment) {
|
|
222
|
+
const policy = environment.KOED_EMBEDDING_ACCELERATION?.trim().toLowerCase() || resolveKoedServerConfig(paths, environment).hardwareAcceleration;
|
|
223
|
+
if (!["auto", "cpu", "cuda", "metal"].includes(policy))
|
|
224
|
+
throw new Error("KOED_EMBEDDING_ACCELERATION must be auto, cpu, metal, or cuda.");
|
|
225
|
+
return policy;
|
|
226
|
+
}
|
|
227
|
+
function chooseCuda(catalog, installed, choice, policy) {
|
|
228
|
+
if (choice === "with") {
|
|
229
|
+
if (!catalog.cuda)
|
|
230
|
+
throw new Error("This release has no reviewed CUDA add-on pin. Use a trusted release with a qualified CUDA add-on.");
|
|
231
|
+
return catalog.cuda;
|
|
232
|
+
}
|
|
233
|
+
if (choice === "without") {
|
|
234
|
+
if (policy !== "auto" && policy !== "cpu")
|
|
235
|
+
throw new Error("Select auto or cpu embedding acceleration before removing the CUDA add-on.");
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
if (!installed || compatibleCuda(catalog.core, installed))
|
|
239
|
+
return installed;
|
|
240
|
+
if (policy !== "cpu" && policy !== "auto")
|
|
241
|
+
throw new Error(`This core update would remove the selected CUDA add-on under forced ${policy} policy. Stop Koed and run ${releaseInstallCommand} --with-cuda.`);
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
function preservedCudaFailure(error, choice, policy) {
|
|
245
|
+
if (choice === "preserve" && (policy === "auto" || policy === "cpu") && error instanceof Error)
|
|
246
|
+
error.message += ` The installed CUDA add-on is kept by default. To update the CPU core without it, run ${releaseInstallCommand} --without-cuda.`;
|
|
247
|
+
return error;
|
|
248
|
+
}
|
|
249
|
+
async function stageNativeAsset(pin, staging, signal, progress) {
|
|
250
|
+
const kind = nativeKind(pin), archive = resolve2(staging, kind + ".tar.gz"), unpack = resolve2(staging, kind);
|
|
251
|
+
try {
|
|
252
|
+
await fetchBounded(pin.url, archive, pin.archiveBytes, signal, progress, false);
|
|
253
|
+
if (nativeFileDigest(archive, pin.archiveBytes) !== pin.sha256)
|
|
254
|
+
throw new Error("Native archive digest mismatch.");
|
|
255
|
+
} catch (cause) {
|
|
256
|
+
throw new Error(`Could not acquire the pinned native ${kind}: ${cause instanceof Error ? cause.message : String(cause)}. Check connectivity and free space, then retry ${releaseInstallCommand}${kind === "cuda" ? " --with-cuda" : ""}.`, { cause });
|
|
257
|
+
}
|
|
258
|
+
await extractNativeArchive(kind, archive, unpack, pin.expandedBytes, signal);
|
|
259
|
+
const root = resolve2(unpack, "koed-runtime");
|
|
260
|
+
verifyNativePayload(root, pin, true);
|
|
261
|
+
return root;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// packages/koed-server/dist/runtime-release.js
|
|
265
|
+
var reserve = 128 * 1024 * 1024;
|
|
266
|
+
function assertReleaseHost() {
|
|
267
|
+
const report = process.report.getReport();
|
|
268
|
+
const libc = report.header?.glibcVersionRuntime ?? "";
|
|
269
|
+
const [major, minor] = libc.split(".").map(Number);
|
|
270
|
+
if (process.platform !== "linux" || process.arch !== "x64" || !/^\d+\.\d+(\.\d+)?$/.test(libc) || !(major > 2 || major === 2 && minor >= 35))
|
|
271
|
+
throw new Error("The release provider requires Linux x64 and glibc 2.35 or later. On a host supported by Homebrew, install Homebrew and its host prerequisites, then run koed runtime install --provider homebrew --dependency-mode bundled-local.");
|
|
272
|
+
}
|
|
273
|
+
function collectReleaseRuntimeStatus(paths, environment = process.env) {
|
|
274
|
+
assertReleaseHost();
|
|
275
|
+
const policy = embeddingPolicy(paths, environment);
|
|
276
|
+
const catalog = getNativeRuntimeCatalog();
|
|
277
|
+
const selected = readNativeSelection(paths, catalog);
|
|
278
|
+
if (!selected)
|
|
279
|
+
return {
|
|
280
|
+
ok: false,
|
|
281
|
+
provider: "release",
|
|
282
|
+
state: "missing",
|
|
283
|
+
pinnedCoreId: catalog.core.recipeId,
|
|
284
|
+
installedCudaId: null,
|
|
285
|
+
pinnedCudaId: catalog.cuda?.recipeId ?? null,
|
|
286
|
+
executionPolicy: policy,
|
|
287
|
+
message: `Install the CPU runtime with ${releaseInstallCommand}.`
|
|
288
|
+
};
|
|
289
|
+
const blocker = coreStartBlocker(selected.pin, catalog);
|
|
290
|
+
if (blocker)
|
|
291
|
+
return {
|
|
292
|
+
ok: false,
|
|
293
|
+
provider: "release",
|
|
294
|
+
state: "incompatible",
|
|
295
|
+
selectionId: selected.id,
|
|
296
|
+
installedCoreId: selected.pin.recipeId,
|
|
297
|
+
pinnedCoreId: catalog.core.recipeId,
|
|
298
|
+
installedCudaId: selected.cuda?.recipeId ?? null,
|
|
299
|
+
pinnedCudaId: catalog.cuda?.recipeId ?? null,
|
|
300
|
+
executionPolicy: policy,
|
|
301
|
+
runtimeVersion: selected.pin.runtimeVersion,
|
|
302
|
+
message: `${blocker}. Stop Koed and run ${releaseInstallCommand}.`
|
|
303
|
+
};
|
|
304
|
+
const coreOutdated = selected.pin.recipeId !== catalog.core.recipeId;
|
|
305
|
+
const cudaOutdated = !!(selected.cuda && catalog.cuda && selected.cuda.recipeId !== catalog.cuda.recipeId);
|
|
306
|
+
const outdated = coreOutdated || cudaOutdated;
|
|
307
|
+
return {
|
|
308
|
+
ok: true,
|
|
309
|
+
provider: "release",
|
|
310
|
+
state: outdated ? "outdated" : "installed",
|
|
311
|
+
selectionId: selected.id,
|
|
312
|
+
installedCoreId: selected.pin.recipeId,
|
|
313
|
+
pinnedCoreId: catalog.core.recipeId,
|
|
314
|
+
installedCudaId: selected.cuda?.recipeId ?? null,
|
|
315
|
+
pinnedCudaId: catalog.cuda?.recipeId ?? null,
|
|
316
|
+
executionPolicy: policy,
|
|
317
|
+
runtimeVersion: selected.pin.runtimeVersion,
|
|
318
|
+
message: cudaOutdated ? `A CUDA add-on update is available. Stop Koed and run ${releaseInstallCommand} --with-cuda.` : coreOutdated ? `A native core update is available. Stop Koed and run ${releaseInstallCommand}.` : selected.cuda ? "The selected CPU core and CUDA add-on are installed." : "The selected CPU runtime is installed."
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
async function probeCore(root, signal) {
|
|
322
|
+
for (const binary of [
|
|
323
|
+
"postgres/bin/postgres",
|
|
324
|
+
"postgres/bin/psql",
|
|
325
|
+
"llama.cpp/llama-server"
|
|
326
|
+
]) {
|
|
327
|
+
try {
|
|
328
|
+
await nativeCommand(resolve3(root, binary), ["--version"], {
|
|
329
|
+
cwd: root,
|
|
330
|
+
signal,
|
|
331
|
+
env: {
|
|
332
|
+
PATH: "/usr/bin:/bin",
|
|
333
|
+
LANG: "C",
|
|
334
|
+
LC_ALL: "C",
|
|
335
|
+
KOED_LLAMA_SERVER_BACKEND: "cpu"
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
} catch (error) {
|
|
339
|
+
throw new Error(`Staged native probe failed: ${binary}. ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
function recordStatus(paths, id) {
|
|
344
|
+
const cache = resolve3(paths.cacheDir, "runtime-release.json");
|
|
345
|
+
const temp = `${cache}.${randomUUID2()}.tmp`;
|
|
346
|
+
try {
|
|
347
|
+
assertDirectoryChain(paths.koedHome, paths.cacheDir);
|
|
348
|
+
mkdirSync2(paths.cacheDir, { recursive: true, mode: 448 });
|
|
349
|
+
const bytes = JSON.stringify({ schemaVersion: 1, selectionId: id });
|
|
350
|
+
if (lstatSync2(cache, { throwIfNoEntry: false })?.isFile() && readNativeMetadata(cache, 16 * 1024).toString("utf8") === bytes)
|
|
351
|
+
return;
|
|
352
|
+
writeFileSync2(temp, bytes, {
|
|
353
|
+
flag: "wx",
|
|
354
|
+
mode: 384,
|
|
355
|
+
flush: true
|
|
356
|
+
});
|
|
357
|
+
renameSync2(temp, cache);
|
|
358
|
+
syncNativeDirectory(paths.cacheDir);
|
|
359
|
+
} catch (error) {
|
|
360
|
+
return `The selection is active. Could not refresh derived status: ${error instanceof Error ? error.message : String(error)}`;
|
|
361
|
+
} finally {
|
|
362
|
+
try {
|
|
363
|
+
rmSync2(temp, { force: true });
|
|
364
|
+
} catch {
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return void 0;
|
|
368
|
+
}
|
|
369
|
+
async function installReleaseRuntime(paths, options = {}) {
|
|
370
|
+
assertReleaseHost();
|
|
371
|
+
const catalog = getNativeRuntimeCatalog(), pin = catalog.core, store = nativeStore(paths), owner = resolveKoedRuntimeOwner();
|
|
372
|
+
const choice = options.cuda ?? "preserve", environment = options.environment ?? process.env;
|
|
373
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
374
|
+
const plan = () => {
|
|
375
|
+
signal.throwIfAborted();
|
|
376
|
+
const selected = readNativeSelection(paths, catalog);
|
|
377
|
+
const policy = embeddingPolicy(paths, environment);
|
|
378
|
+
const cuda = chooseCuda(catalog, selected?.cuda ?? null, choice, policy);
|
|
379
|
+
if (selected && selected.pin.recipeId !== pin.recipeId && compareNativeVersions(pin.runtimeVersion, selected.pin.runtimeVersion) <= 0)
|
|
380
|
+
throw new Error("Refusing a native core downgrade or replacement at the same runtime version.");
|
|
381
|
+
assertPostgresDataVersion(paths, pin.components.postgres.major);
|
|
382
|
+
return { selected, cuda, policy };
|
|
383
|
+
};
|
|
384
|
+
const before = await withStoppedRuntime(paths, owner, async () => {
|
|
385
|
+
const { selected, cuda, policy } = plan();
|
|
386
|
+
for (const name of ["core", "selections", ...cuda ? ["cuda"] : []]) {
|
|
387
|
+
const dir = resolve3(store, name);
|
|
388
|
+
assertDirectoryChain(paths.koedHome, dir);
|
|
389
|
+
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
390
|
+
}
|
|
391
|
+
if (selected?.pin.recipeId === pin.recipeId && selected.cuda?.recipeId === cuda?.recipeId) {
|
|
392
|
+
const validation = choice === "with" ? await validateStagedCuda(paths, selected.root, environment, {
|
|
393
|
+
signal
|
|
394
|
+
}) : void 0;
|
|
395
|
+
const warning = recordStatus(paths, selected.id);
|
|
396
|
+
return {
|
|
397
|
+
unchanged: true,
|
|
398
|
+
result: {
|
|
399
|
+
ok: true,
|
|
400
|
+
provider: "release",
|
|
401
|
+
state: "installed",
|
|
402
|
+
changed: false,
|
|
403
|
+
selectionId: selected.id,
|
|
404
|
+
coreId: pin.recipeId,
|
|
405
|
+
cudaId: cuda?.recipeId ?? null,
|
|
406
|
+
executionPolicy: policy,
|
|
407
|
+
validation,
|
|
408
|
+
downloadedBytes: 0,
|
|
409
|
+
warning,
|
|
410
|
+
message: warning ?? "The selected native runtime is intact. Run koed start when ready."
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
if (cuda)
|
|
415
|
+
await checkCudaHost(signal).catch((error) => {
|
|
416
|
+
if (signal.aborted)
|
|
417
|
+
throw error;
|
|
418
|
+
throw preservedCudaFailure(error, choice, policy);
|
|
419
|
+
});
|
|
420
|
+
return {
|
|
421
|
+
unchanged: false,
|
|
422
|
+
previousId: selected?.id ?? null,
|
|
423
|
+
cuda
|
|
424
|
+
};
|
|
425
|
+
});
|
|
426
|
+
if (before.unchanged)
|
|
427
|
+
return before.result;
|
|
428
|
+
const assets = before.cuda ? [pin, before.cuda] : [pin];
|
|
429
|
+
const missing = assets.filter((asset) => !nativePathExists(payloadRoot(paths, asset)));
|
|
430
|
+
if (missing.some((asset) => nativeKind(asset) === "cuda" && (choice !== "with" || asset !== catalog.cuda)))
|
|
431
|
+
throw new Error("The retained CUDA add-on is missing. Inspect the native store before retrying.");
|
|
432
|
+
const downloadBytes = missing.reduce((n, a) => n + a.archiveBytes, 0);
|
|
433
|
+
const required = missing.reduce((n, a) => n + a.expandedBytes, reserve + downloadBytes);
|
|
434
|
+
const disk = statfsSync(store, { bigint: true });
|
|
435
|
+
if (disk.bavail * disk.bsize < BigInt(required))
|
|
436
|
+
throw new Error(`Insufficient free space. The install requires ${required} available bytes, including a ${reserve}-byte reserve. Retained payloads are already counted as used space.`);
|
|
437
|
+
const staging = mkdtempSync(resolve3(store, ".stage-"));
|
|
438
|
+
try {
|
|
439
|
+
const staged = /* @__PURE__ */ new Map();
|
|
440
|
+
for (const asset of missing)
|
|
441
|
+
staged.set(asset, await stageNativeAsset(asset, staging, signal, options.progress));
|
|
442
|
+
return await withStoppedRuntime(paths, owner, async () => {
|
|
443
|
+
const { selected, cuda, policy } = plan();
|
|
444
|
+
if ((selected?.id ?? null) !== before.previousId || before.cuda?.recipeId !== cuda?.recipeId)
|
|
445
|
+
throw new Error("Another installer changed the selection. Inspect runtime status and retry.");
|
|
446
|
+
for (const [asset, source] of staged) {
|
|
447
|
+
const dest = payloadRoot(paths, asset);
|
|
448
|
+
assertDirectoryChain(paths.koedHome, dest);
|
|
449
|
+
if (!nativePathExists(dest)) {
|
|
450
|
+
renameSync2(source, dest);
|
|
451
|
+
syncNativeDirectory(dirname2(dest));
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
const next = composeNativeSelection(paths, pin, staging, catalog, cuda);
|
|
455
|
+
await probeCore(next.root, signal);
|
|
456
|
+
const validation = cuda ? await validateStagedCuda(paths, next.root, environment, {
|
|
457
|
+
signal
|
|
458
|
+
}).catch((error) => {
|
|
459
|
+
if (signal.aborted)
|
|
460
|
+
throw error;
|
|
461
|
+
throw preservedCudaFailure(error, choice, policy);
|
|
462
|
+
}) : void 0;
|
|
463
|
+
signal.throwIfAborted();
|
|
464
|
+
activateNativeSelection(paths, next.id);
|
|
465
|
+
const warning = recordStatus(paths, next.id);
|
|
466
|
+
const dropped = choice === "preserve" && selected?.cuda && !cuda;
|
|
467
|
+
const message = dropped ? `CPU runtime selected. The prior CUDA add-on is incompatible; run ${releaseInstallCommand} --with-cuda for a compatible pair. Run koed start when ready.` : `${cuda ? "CPU core and CUDA add-on" : "CPU runtime"} installed. Run koed start when ready.`;
|
|
468
|
+
return {
|
|
469
|
+
ok: true,
|
|
470
|
+
provider: "release",
|
|
471
|
+
state: "installed",
|
|
472
|
+
changed: true,
|
|
473
|
+
selectionId: next.id,
|
|
474
|
+
coreId: pin.recipeId,
|
|
475
|
+
cudaId: cuda?.recipeId ?? null,
|
|
476
|
+
executionPolicy: policy,
|
|
477
|
+
validation,
|
|
478
|
+
downloadedBytes: downloadBytes,
|
|
479
|
+
expandedBytes: assets.reduce((n, a) => n + a.expandedBytes, 0),
|
|
480
|
+
requiredFreeBytes: required,
|
|
481
|
+
warning,
|
|
482
|
+
message: warning ?? message
|
|
483
|
+
};
|
|
484
|
+
});
|
|
485
|
+
} finally {
|
|
486
|
+
rmSync2(staging, { recursive: true, force: true });
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
function assertPostgresDataVersion(paths, major) {
|
|
490
|
+
const file = resolve3(paths.postgresDataDir, "PG_VERSION");
|
|
491
|
+
if (nativePathExists(file) && readNativeMetadata(file, 32).toString("utf8").trim() !== String(major)) {
|
|
492
|
+
throw new Error("Postgres data requires another major version. Database upgrades are outside runtime installation.");
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function resolveSelectedCoreForStart(paths, environment = process.env, useSelectedLlama = true) {
|
|
496
|
+
try {
|
|
497
|
+
const id = currentNativeId(paths);
|
|
498
|
+
if (!id) {
|
|
499
|
+
for (const name of ["postgres", "llama.cpp"]) {
|
|
500
|
+
const entry = resolve3(paths.koedHome, "runtime", name);
|
|
501
|
+
if (lstatSync2(entry, { throwIfNoEntry: false })?.isSymbolicLink() && readlinkSync2(entry) === `native/current/${name}`)
|
|
502
|
+
throw new Error("Incomplete native selection.");
|
|
503
|
+
}
|
|
504
|
+
return void 0;
|
|
505
|
+
}
|
|
506
|
+
assertReleaseHost();
|
|
507
|
+
const catalog = getNativeRuntimeCatalog(), selected = readNativeSelection(paths, catalog);
|
|
508
|
+
assertStartableCore(selected.pin, catalog);
|
|
509
|
+
if (useSelectedLlama && embeddingPolicy(paths, environment) === "cuda" && !selected.cuda)
|
|
510
|
+
throw new Error("The forced CUDA policy requires a selected CUDA add-on.");
|
|
511
|
+
return selected.root;
|
|
512
|
+
} catch (cause) {
|
|
513
|
+
const command = environment.KOED_EMBEDDING_ACCELERATION?.trim().toLowerCase() === "cuda" ? releaseInstallCommand + " --with-cuda" : releaseInstallCommand;
|
|
514
|
+
throw new Error(`Cannot start the release-managed native runtime: ${cause instanceof Error ? cause.message : String(cause)}. Stop Koed, inspect runtime status, and run ${command}.`, { cause });
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
export {
|
|
518
|
+
assertReleaseHost,
|
|
519
|
+
collectReleaseRuntimeStatus,
|
|
520
|
+
installReleaseRuntime,
|
|
521
|
+
resolveSelectedCoreForStart
|
|
522
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createRequire as createKoedRequire } from "node:module"; const require = createKoedRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// packages/koed-server/dist/env-file.js
|
|
4
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
5
|
+
import { resolve } from "node:path";
|
|
6
|
+
var parseEnvFile = (content, options = {}) => {
|
|
7
|
+
const values = {};
|
|
8
|
+
const fail = (lineNumber, reason) => {
|
|
9
|
+
throw new Error(`${options.source ?? "Environment file"}:${lineNumber}: ${reason}`);
|
|
10
|
+
};
|
|
11
|
+
for (const [index, line] of content.split(/\r?\n/).entries()) {
|
|
12
|
+
const trimmed = line.trim();
|
|
13
|
+
if (!trimmed || trimmed.startsWith("#")) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const equals = trimmed.indexOf("=");
|
|
17
|
+
if (equals <= 0) {
|
|
18
|
+
if (options.strict)
|
|
19
|
+
fail(index + 1, "expected KEY=VALUE");
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
const key = trimmed.slice(0, equals).trim();
|
|
23
|
+
let value = trimmed.slice(equals + 1).trim();
|
|
24
|
+
const quoted = value.startsWith('"') || value.startsWith("'");
|
|
25
|
+
if (quoted && options.strict) {
|
|
26
|
+
const quote = value[0];
|
|
27
|
+
let closingQuote = -1;
|
|
28
|
+
for (let position = 1; position < value.length; position += 1) {
|
|
29
|
+
if (quote === '"' && value[position] === "\\") {
|
|
30
|
+
position += 1;
|
|
31
|
+
} else if (value[position] === quote) {
|
|
32
|
+
closingQuote = position;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (closingQuote < 0) {
|
|
37
|
+
fail(index + 1, `unterminated quoted value for ${key}`);
|
|
38
|
+
}
|
|
39
|
+
const trailing = value.slice(closingQuote + 1).trim();
|
|
40
|
+
if (trailing && !trailing.startsWith("#")) {
|
|
41
|
+
fail(index + 1, `unexpected text after quoted value for ${key}`);
|
|
42
|
+
}
|
|
43
|
+
value = value.slice(1, closingQuote);
|
|
44
|
+
} else if (quoted) {
|
|
45
|
+
if (value.length < 2 || value.at(-1) !== value[0]) {
|
|
46
|
+
} else {
|
|
47
|
+
value = value.slice(1, -1);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (options.strict && !/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) {
|
|
51
|
+
fail(index + 1, `invalid environment variable name ${key}`);
|
|
52
|
+
}
|
|
53
|
+
values[key] = value;
|
|
54
|
+
}
|
|
55
|
+
return values;
|
|
56
|
+
};
|
|
57
|
+
var loadRepoEnv = (repoRoot, environment = process.env) => {
|
|
58
|
+
const envPath = environment.KOED_ENV_PATH?.trim() ? resolve(environment.KOED_ENV_PATH) : resolve(repoRoot, ".env");
|
|
59
|
+
if (!existsSync(envPath)) {
|
|
60
|
+
return {};
|
|
61
|
+
}
|
|
62
|
+
return parseEnvFile(readFileSync(envPath, "utf8"));
|
|
63
|
+
};
|
|
64
|
+
var environmentWithRepoEnv = (repoRoot, environment = process.env) => ({
|
|
65
|
+
...loadRepoEnv(repoRoot, environment),
|
|
66
|
+
...environment
|
|
67
|
+
});
|
|
68
|
+
var resolveApiUrl = (environment, repoEnv) => ((environment.KOED_AUTO_PORTS === "1" && environment.API_HOST_PORT ? `http://localhost:${environment.API_HOST_PORT}` : null) ?? environment.MEMORY_API_URL ?? (environment.API_HOST_PORT ? `http://localhost:${environment.API_HOST_PORT}` : null) ?? repoEnv.MEMORY_API_URL ?? (repoEnv.API_HOST_PORT ? `http://localhost:${repoEnv.API_HOST_PORT}` : null) ?? environment.CODEX_MEMORY_BASE_URL ?? repoEnv.CODEX_MEMORY_BASE_URL ?? "http://localhost:3300").trim();
|
|
69
|
+
|
|
70
|
+
export {
|
|
71
|
+
parseEnvFile,
|
|
72
|
+
loadRepoEnv,
|
|
73
|
+
environmentWithRepoEnv,
|
|
74
|
+
resolveApiUrl
|
|
75
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire as createKoedRequire } from "node:module"; const require = createKoedRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
isKoedServerCliEntrypoint,
|
|
5
|
+
runKoedServerCli,
|
|
6
|
+
shouldExitPackagedSupervisor
|
|
7
|
+
} from "./chunk-FYYRFX4U.js";
|
|
8
|
+
import {
|
|
9
|
+
startKoedServerDaemon
|
|
10
|
+
} from "./chunk-7FSHVVTZ.js";
|
|
11
|
+
import "./chunk-TSAYTRX3.js";
|
|
12
|
+
import "./chunk-YZLQZU5E.js";
|
|
13
|
+
import "./chunk-V67IQB7H.js";
|
|
14
|
+
import "./chunk-BTS4ZTF3.js";
|
|
15
|
+
export {
|
|
16
|
+
isKoedServerCliEntrypoint,
|
|
17
|
+
runKoedServerCli,
|
|
18
|
+
shouldExitPackagedSupervisor,
|
|
19
|
+
startKoedServerDaemon
|
|
20
|
+
};
|