@tuned-tensor/cli 0.28.1 → 0.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -180
- package/dist/chunk-6CETSU2H.js +260 -0
- package/dist/chunk-6CETSU2H.js.map +1 -0
- package/dist/chunk-B2O6P7EX.js +1623 -0
- package/dist/chunk-B2O6P7EX.js.map +1 -0
- package/dist/{chunk-7CO5DBCY.js → chunk-GMBZ2FE6.js} +5046 -5014
- package/dist/chunk-GMBZ2FE6.js.map +1 -0
- package/dist/chunk-MM75IAY3.js +138 -0
- package/dist/chunk-MM75IAY3.js.map +1 -0
- package/dist/chunk-ST6ZSEUH.js +1272 -0
- package/dist/chunk-ST6ZSEUH.js.map +1 -0
- package/dist/contracts-SHBUXEOQ.js +72 -0
- package/dist/dataset-Y5NEUKI5.js +22 -0
- package/dist/dataset-Y5NEUKI5.js.map +1 -0
- package/dist/index.js +4530 -9349
- package/dist/index.js.map +1 -1
- package/dist/local-runtime.js +84 -467
- package/dist/local-runtime.js.map +1 -1
- package/dist/model-server-IWCRFAOO.js +18 -0
- package/dist/model-server-IWCRFAOO.js.map +1 -0
- package/dist/{prefetch-LBIOO7ZO.js → prefetch-Q2ZA3SJ7.js} +3 -2
- package/dist/prefetch-Q2ZA3SJ7.js.map +1 -0
- package/docs/local-runtime/architecture.md +4 -4
- package/docs/local-runtime/aws-gpu.md +4 -5
- package/docs/local-runtime/decision.md +2 -2
- package/docs/local-runtime/hosting.md +103 -92
- package/docs/local-runtime/model-quality.md +103 -0
- package/docs/local-runtime/serving.md +1 -6
- package/docs/local-runtime/spark.md +2 -3
- package/docs/spec-architecture.html +2 -26
- package/docs/spec-workflow.md +9 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/LICENSE.md +55 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/README.md +364 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/index.d.ts +123 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/index.d.ts.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/index.js +778 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/index.js.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/index.min.js +3 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/index.min.js.map +7 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/numeric-range.d.ts +15 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/numeric-range.d.ts.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/numeric-range.js +76 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/numeric-range.js.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/package.json +3 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/parse-class.d.ts +4 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/parse-class.d.ts.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/parse-class.js +142 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/commonjs/parse-class.js.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/index.d.ts +123 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/index.d.ts.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/index.js +774 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/index.js.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/index.min.js +3 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/index.min.js.map +7 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/numeric-range.d.ts +15 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/numeric-range.d.ts.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/numeric-range.js +72 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/numeric-range.js.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/package.json +3 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/parse-class.d.ts +4 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/parse-class.d.ts.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/parse-class.js +138 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/dist/esm/parse-class.js.map +1 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/package.json +56 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/src/index.ts +932 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/src/numeric-range.ts +79 -0
- package/node_modules/@isaacs/nonrecursive-glob-match/src/parse-class.ts +158 -0
- package/node_modules/minimatch/README.md +125 -107
- package/node_modules/minimatch/dist/commonjs/index.d.ts +49 -38
- package/node_modules/minimatch/dist/commonjs/index.d.ts.map +1 -1
- package/node_modules/minimatch/dist/commonjs/index.js +782 -359
- package/node_modules/minimatch/dist/commonjs/index.js.map +1 -1
- package/node_modules/minimatch/dist/esm/index.d.ts +49 -38
- package/node_modules/minimatch/dist/esm/index.d.ts.map +1 -1
- package/node_modules/minimatch/dist/esm/index.js +781 -356
- package/node_modules/minimatch/dist/esm/index.js.map +1 -1
- package/node_modules/minimatch/package.json +5 -4
- package/package.json +4 -5
- package/training/adapter/src/train.py +16 -1
- package/dist/chunk-7CO5DBCY.js.map +0 -1
- package/dist/chunk-FSVTMX4L.js +0 -2820
- package/dist/chunk-FSVTMX4L.js.map +0 -1
- package/node_modules/minimatch/dist/commonjs/ast.d.ts +0 -22
- package/node_modules/minimatch/dist/commonjs/ast.d.ts.map +0 -1
- package/node_modules/minimatch/dist/commonjs/ast.js +0 -845
- package/node_modules/minimatch/dist/commonjs/ast.js.map +0 -1
- package/node_modules/minimatch/dist/commonjs/brace-expressions.d.ts +0 -8
- package/node_modules/minimatch/dist/commonjs/brace-expressions.d.ts.map +0 -1
- package/node_modules/minimatch/dist/commonjs/brace-expressions.js +0 -150
- package/node_modules/minimatch/dist/commonjs/brace-expressions.js.map +0 -1
- package/node_modules/minimatch/dist/esm/ast.d.ts +0 -22
- package/node_modules/minimatch/dist/esm/ast.d.ts.map +0 -1
- package/node_modules/minimatch/dist/esm/ast.js +0 -841
- package/node_modules/minimatch/dist/esm/ast.js.map +0 -1
- package/node_modules/minimatch/dist/esm/brace-expressions.d.ts +0 -8
- package/node_modules/minimatch/dist/esm/brace-expressions.d.ts.map +0 -1
- package/node_modules/minimatch/dist/esm/brace-expressions.js +0 -146
- package/node_modules/minimatch/dist/esm/brace-expressions.js.map +0 -1
- /package/dist/{prefetch-LBIOO7ZO.js.map → contracts-SHBUXEOQ.js.map} +0 -0
|
@@ -0,0 +1,1623 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/local-runtime/task-quality.ts
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
var taskQualityPolicySchema = z.object({
|
|
6
|
+
minScore: z.number().min(0).max(1),
|
|
7
|
+
minPassRate: z.number().min(0).max(1),
|
|
8
|
+
minExamples: z.number().int().min(1),
|
|
9
|
+
minScoreDelta: z.number().min(0).max(1).default(0)
|
|
10
|
+
}).strict();
|
|
11
|
+
var taskQualityGateSchema = z.object({
|
|
12
|
+
policy: taskQualityPolicySchema,
|
|
13
|
+
passed: z.boolean(),
|
|
14
|
+
failures: z.array(z.string())
|
|
15
|
+
}).strict();
|
|
16
|
+
function evaluateTaskQuality(baseline, candidate, policy) {
|
|
17
|
+
const failures = [];
|
|
18
|
+
const tolerance = 1e-12;
|
|
19
|
+
const minimum = policy?.minExamples ?? 1;
|
|
20
|
+
if (candidate.total < minimum || candidate.eval_examples_used < minimum) {
|
|
21
|
+
failures.push(`Task evaluation needs at least ${minimum} held-out examples; evaluated ${candidate.eval_examples_used}.`);
|
|
22
|
+
}
|
|
23
|
+
if (baseline.results.length !== candidate.results.length || baseline.results.length !== baseline.total || candidate.results.length !== candidate.total || baseline.results.some((row, i) => row.prompt !== candidate.results[i]?.prompt || row.expected !== candidate.results[i]?.expected)) {
|
|
24
|
+
failures.push("Baseline and candidate must evaluate the same held-out prompts and references.");
|
|
25
|
+
}
|
|
26
|
+
if (policy && (!["spec_holdout", "prebuilt_test", "prebuilt_validation"].includes(candidate.eval_split ?? "") || baseline.inference_provider !== "transformers" || candidate.inference_provider !== "transformers")) {
|
|
27
|
+
failures.push("Task acceptance requires recorded held-out inference evidence.");
|
|
28
|
+
}
|
|
29
|
+
if (candidate.eval_split === "spec_examples") failures.push("Task quality requires held-out evaluation, not the training examples.");
|
|
30
|
+
if (baseline.inference_provider === "none" || candidate.inference_provider === "none") {
|
|
31
|
+
failures.push("A dry-run has no measured model quality.");
|
|
32
|
+
}
|
|
33
|
+
if (candidate.avg_score + tolerance < (policy?.minScore ?? 0)) {
|
|
34
|
+
failures.push(`Task score ${candidate.avg_score.toFixed(4)} is below ${policy.minScore.toFixed(4)}.`);
|
|
35
|
+
}
|
|
36
|
+
if (candidate.pass_rate + tolerance < (policy?.minPassRate ?? 0)) {
|
|
37
|
+
failures.push(`Task pass rate ${candidate.pass_rate.toFixed(4)} is below ${policy.minPassRate.toFixed(4)}.`);
|
|
38
|
+
}
|
|
39
|
+
if (candidate.avg_score - baseline.avg_score + tolerance < (policy?.minScoreDelta ?? 0)) {
|
|
40
|
+
failures.push(`Task score change ${(candidate.avg_score - baseline.avg_score).toFixed(4)} is below the required ${(policy?.minScoreDelta ?? 0).toFixed(4)}.`);
|
|
41
|
+
}
|
|
42
|
+
if (candidate.pass_rate + tolerance < baseline.pass_rate) {
|
|
43
|
+
failures.push("Task pass rate regressed from the baseline.");
|
|
44
|
+
}
|
|
45
|
+
return { passed: failures.length === 0, failures };
|
|
46
|
+
}
|
|
47
|
+
function taskQualityForReport(report) {
|
|
48
|
+
return evaluateTaskQuality(report.baseline, report.candidate, report.task_quality?.policy);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/local-runtime/contracts.ts
|
|
52
|
+
import { z as z2 } from "zod";
|
|
53
|
+
import { pipelineDocumentSchema, isFoundationPipeline, parsePipeline } from "@tuned-tensor/pipeline-contract";
|
|
54
|
+
|
|
55
|
+
// src/paths.ts
|
|
56
|
+
import { existsSync } from "fs";
|
|
57
|
+
import { homedir } from "os";
|
|
58
|
+
import { isAbsolute, join, resolve } from "path";
|
|
59
|
+
var TUNED_TENSOR_DIR = ".tuned-tensor";
|
|
60
|
+
var DEFAULT_ARTIFACT_ROOT = `${TUNED_TENSOR_DIR}/artifacts`;
|
|
61
|
+
var DEFAULT_PROJECT_STORE_ROOT = `${TUNED_TENSOR_DIR}/store`;
|
|
62
|
+
var DEFAULT_FOUNDATION_RUNS_DIR = `${TUNED_TENSOR_DIR}/foundation-runs`;
|
|
63
|
+
var DEFAULT_DECISION_RUNS_DIR = `${TUNED_TENSOR_DIR}/decision-runs`;
|
|
64
|
+
function envHome(env = process.env) {
|
|
65
|
+
const home = env.HOME?.trim();
|
|
66
|
+
return home ? resolve(home) : homedir();
|
|
67
|
+
}
|
|
68
|
+
function expandUserPath(value, env = process.env, baseDirectory = process.cwd()) {
|
|
69
|
+
const trimmed = value.trim();
|
|
70
|
+
if (trimmed === "~") return envHome(env);
|
|
71
|
+
if (trimmed.startsWith("~/")) return resolve(envHome(env), trimmed.slice(2));
|
|
72
|
+
return isAbsolute(trimmed) ? resolve(trimmed) : resolve(baseDirectory, trimmed);
|
|
73
|
+
}
|
|
74
|
+
function getTunedTensorHome(env = process.env) {
|
|
75
|
+
const override = env.TUNED_TENSOR_HOME?.trim();
|
|
76
|
+
if (override) return expandUserPath(override, env);
|
|
77
|
+
return join(envHome(env), TUNED_TENSOR_DIR);
|
|
78
|
+
}
|
|
79
|
+
function legacyConfigDir(env) {
|
|
80
|
+
const configHome = env.XDG_CONFIG_HOME?.trim() ? resolve(env.XDG_CONFIG_HOME) : join(envHome(env), ".config");
|
|
81
|
+
return join(configHome, "tuned-tensor");
|
|
82
|
+
}
|
|
83
|
+
function getConfigDir(env = process.env) {
|
|
84
|
+
const next = getTunedTensorHome(env);
|
|
85
|
+
if (env.TUNED_TENSOR_HOME?.trim()) return next;
|
|
86
|
+
const legacy = legacyConfigDir(env);
|
|
87
|
+
if (!existsSync(join(next, "config.json")) && existsSync(join(legacy, "config.json"))) {
|
|
88
|
+
return legacy;
|
|
89
|
+
}
|
|
90
|
+
return next;
|
|
91
|
+
}
|
|
92
|
+
function getAgentConfigDir(env = process.env) {
|
|
93
|
+
const next = join(getTunedTensorHome(env), "agent");
|
|
94
|
+
if (env.TUNED_TENSOR_HOME?.trim()) return next;
|
|
95
|
+
const legacy = join(legacyConfigDir(env), "agent");
|
|
96
|
+
if (!existsSync(next) && existsSync(legacy)) return legacy;
|
|
97
|
+
return next;
|
|
98
|
+
}
|
|
99
|
+
function defaultStoreRoot(env = process.env) {
|
|
100
|
+
const override = env.TT_LOCAL_HOME?.trim();
|
|
101
|
+
if (override) return expandUserPath(override, env);
|
|
102
|
+
const next = join(getTunedTensorHome(env), "store");
|
|
103
|
+
if (env.TUNED_TENSOR_HOME?.trim()) return next;
|
|
104
|
+
const legacy = join(envHome(env), ".tuned-tensor-local");
|
|
105
|
+
if (!existsSync(next) && existsSync(legacy)) return legacy;
|
|
106
|
+
return next;
|
|
107
|
+
}
|
|
108
|
+
function defaultCacheRoot(env = process.env) {
|
|
109
|
+
return join(getTunedTensorHome(env), "cache");
|
|
110
|
+
}
|
|
111
|
+
function pythonEnvironmentPath(kind, hash, env = process.env) {
|
|
112
|
+
const next = join(defaultCacheRoot(env), kind, hash);
|
|
113
|
+
if (env.TUNED_TENSOR_HOME?.trim()) return next;
|
|
114
|
+
const cacheHome = env.XDG_CACHE_HOME?.trim() ? resolve(env.XDG_CACHE_HOME) : join(envHome(env), ".cache");
|
|
115
|
+
const legacy = join(cacheHome, "tuned-tensor-local", kind, hash);
|
|
116
|
+
if (!existsSync(next) && existsSync(legacy)) return legacy;
|
|
117
|
+
return next;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/local-runtime/model-registry.ts
|
|
121
|
+
import { lstat as lstat2, readdir as readdir2 } from "fs/promises";
|
|
122
|
+
import { isAbsolute as isAbsolute3, resolve as resolve3 } from "path";
|
|
123
|
+
import { fileURLToPath } from "url";
|
|
124
|
+
|
|
125
|
+
// src/local-runtime/artifacts.ts
|
|
126
|
+
import { createHash, randomUUID } from "crypto";
|
|
127
|
+
import { createReadStream } from "fs";
|
|
128
|
+
import { copyFile, lstat, mkdir, open, readFile, readdir, realpath, rename, stat, writeFile } from "fs/promises";
|
|
129
|
+
import { dirname, isAbsolute as isAbsolute2, join as join2, relative, resolve as resolve2, sep } from "path";
|
|
130
|
+
import { createGunzip } from "zlib";
|
|
131
|
+
import { pathToFileURL } from "url";
|
|
132
|
+
var RUN_OWNER_FILE = ".tt-local-run-owner.json";
|
|
133
|
+
var ARTIFACT_WORKFLOW_LOCK_FILE = ".tt-local-workflow.lock";
|
|
134
|
+
function fileUri(path) {
|
|
135
|
+
return pathToFileURL(resolve2(path)).href;
|
|
136
|
+
}
|
|
137
|
+
function defaultArtifactPrefix(input) {
|
|
138
|
+
return join2("users", input.userId, "specs", input.behaviorSpecId, "runs", input.runId);
|
|
139
|
+
}
|
|
140
|
+
function resolveRunArtifacts(args) {
|
|
141
|
+
const root = resolve2(args.artifactRoot);
|
|
142
|
+
const safePrefix = args.prefix.replace(/^[/\\]+/, "");
|
|
143
|
+
const runDir = resolve2(root, safePrefix);
|
|
144
|
+
const containment = relative(root, runDir);
|
|
145
|
+
if (containment === ".." || containment.startsWith(`..${sep}`) || isAbsolute2(containment)) {
|
|
146
|
+
throw new Error(`Artifact prefix escapes artifactRoot: ${args.prefix}`);
|
|
147
|
+
}
|
|
148
|
+
const trainingDir = join2(runDir, "training");
|
|
149
|
+
return {
|
|
150
|
+
root,
|
|
151
|
+
prefix: safePrefix,
|
|
152
|
+
runDir,
|
|
153
|
+
trainingJsonl: join2(runDir, "training.jsonl"),
|
|
154
|
+
stageMetadataJson: join2(runDir, "stage-metadata.json"),
|
|
155
|
+
trainingReportJson: join2(runDir, "training-report.json"),
|
|
156
|
+
baselineEvalJson: join2(runDir, "baseline-eval.json"),
|
|
157
|
+
candidateEvalJson: join2(runDir, "candidate-eval.json"),
|
|
158
|
+
generalBaselineEvalJson: join2(runDir, "general-baseline-eval.json"),
|
|
159
|
+
generalCandidateEvalJson: join2(runDir, "general-candidate-eval.json"),
|
|
160
|
+
runReportJson: join2(runDir, "run-report.json"),
|
|
161
|
+
artifactManifestJson: join2(runDir, "artifact-manifest.json"),
|
|
162
|
+
progressJsonl: join2(runDir, "progress.jsonl"),
|
|
163
|
+
trainingDir,
|
|
164
|
+
trainingInputDir: join2(trainingDir, "input", "data", "training"),
|
|
165
|
+
trainingConfigDir: join2(trainingDir, "input", "config"),
|
|
166
|
+
trainingOutputDir: join2(trainingDir, "output"),
|
|
167
|
+
trainingModelDir: join2(trainingDir, "model"),
|
|
168
|
+
trainingLog: join2(trainingDir, "training.log")
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
async function ensureSafeRunDirectory(artifacts) {
|
|
172
|
+
await mkdir(artifacts.root, { recursive: true });
|
|
173
|
+
const canonicalRoot = await realpath(artifacts.root);
|
|
174
|
+
const pathFromRoot = relative(artifacts.root, artifacts.runDir);
|
|
175
|
+
const segments = pathFromRoot ? pathFromRoot.split(sep).filter(Boolean) : [];
|
|
176
|
+
let current = artifacts.root;
|
|
177
|
+
for (const segment of segments) {
|
|
178
|
+
current = join2(current, segment);
|
|
179
|
+
let metadata = await lstat(current).catch((error) => {
|
|
180
|
+
if (error.code === "ENOENT") return null;
|
|
181
|
+
throw error;
|
|
182
|
+
});
|
|
183
|
+
if (!metadata) {
|
|
184
|
+
await mkdir(current).catch((error) => {
|
|
185
|
+
if (error.code !== "EEXIST") throw error;
|
|
186
|
+
});
|
|
187
|
+
metadata = await lstat(current);
|
|
188
|
+
}
|
|
189
|
+
if (metadata.isSymbolicLink()) {
|
|
190
|
+
throw new Error(`Artifact path must not contain symbolic links: ${current}`);
|
|
191
|
+
}
|
|
192
|
+
if (!metadata.isDirectory()) {
|
|
193
|
+
throw new Error(`Artifact path component is not a directory: ${current}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
const canonicalRunDir = await realpath(artifacts.runDir);
|
|
197
|
+
const containment = relative(canonicalRoot, canonicalRunDir);
|
|
198
|
+
if (containment === ".." || containment.startsWith(`..${sep}`) || isAbsolute2(containment)) {
|
|
199
|
+
throw new Error(`Artifact run directory escapes artifactRoot: ${artifacts.runDir}`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
async function claimRunArtifactDirectory(args) {
|
|
203
|
+
await ensureSafeRunDirectory(args.artifacts);
|
|
204
|
+
const ownerPath = join2(args.artifacts.runDir, RUN_OWNER_FILE);
|
|
205
|
+
const proposed = {
|
|
206
|
+
schema_version: 1,
|
|
207
|
+
run_id: args.runId,
|
|
208
|
+
user_id: args.userId,
|
|
209
|
+
behavior_spec_id: args.behaviorSpecId,
|
|
210
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
211
|
+
};
|
|
212
|
+
try {
|
|
213
|
+
const handle = await open(ownerPath, "wx", 384);
|
|
214
|
+
try {
|
|
215
|
+
await handle.writeFile(`${JSON.stringify(proposed, null, 2)}
|
|
216
|
+
`);
|
|
217
|
+
await handle.sync();
|
|
218
|
+
} finally {
|
|
219
|
+
await handle.close();
|
|
220
|
+
}
|
|
221
|
+
return proposed;
|
|
222
|
+
} catch (error) {
|
|
223
|
+
if (error.code !== "EEXIST") throw error;
|
|
224
|
+
}
|
|
225
|
+
let existing;
|
|
226
|
+
try {
|
|
227
|
+
existing = JSON.parse(await readFile(ownerPath, "utf8"));
|
|
228
|
+
} catch (error) {
|
|
229
|
+
throw new Error(`Artifact run owner claim is unreadable: ${ownerPath}`, { cause: error });
|
|
230
|
+
}
|
|
231
|
+
if (existing.schema_version !== 1 || existing.run_id !== args.runId || existing.user_id !== args.userId || existing.behavior_spec_id !== args.behaviorSpecId) {
|
|
232
|
+
throw new Error(
|
|
233
|
+
`Artifact directory ${args.artifacts.runDir} is already owned by run ${existing.run_id ?? "unknown"}; run ${args.runId} must use a different artifacts.prefix.`
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
return existing;
|
|
237
|
+
}
|
|
238
|
+
async function prepareRunDirectories(artifacts) {
|
|
239
|
+
await ensureSafeRunDirectory(artifacts);
|
|
240
|
+
await Promise.all([
|
|
241
|
+
mkdir(artifacts.runDir, { recursive: true }),
|
|
242
|
+
mkdir(artifacts.trainingInputDir, { recursive: true }),
|
|
243
|
+
mkdir(artifacts.trainingConfigDir, { recursive: true }),
|
|
244
|
+
mkdir(artifacts.trainingOutputDir, { recursive: true }),
|
|
245
|
+
mkdir(artifacts.trainingModelDir, { recursive: true })
|
|
246
|
+
]);
|
|
247
|
+
}
|
|
248
|
+
async function writeJson(path, value) {
|
|
249
|
+
await mkdir(dirname(path), { recursive: true });
|
|
250
|
+
await writeFile(path, `${JSON.stringify(value, null, 2)}
|
|
251
|
+
`, "utf8");
|
|
252
|
+
}
|
|
253
|
+
async function writeJsonAtomic(path, value) {
|
|
254
|
+
await writeFileAtomic(path, `${JSON.stringify(value, null, 2)}
|
|
255
|
+
`);
|
|
256
|
+
}
|
|
257
|
+
async function writeFileAtomic(path, value) {
|
|
258
|
+
await mkdir(dirname(path), { recursive: true });
|
|
259
|
+
const temporary = `${path}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
|
|
260
|
+
await writeFile(temporary, value);
|
|
261
|
+
await rename(temporary, path);
|
|
262
|
+
}
|
|
263
|
+
async function readJson(path) {
|
|
264
|
+
return JSON.parse(await readFile(path, "utf8"));
|
|
265
|
+
}
|
|
266
|
+
async function copyDatasetToTrainingChannel(artifacts) {
|
|
267
|
+
const destination = join2(artifacts.trainingInputDir, "training.jsonl");
|
|
268
|
+
await copyFile(artifacts.trainingJsonl, destination);
|
|
269
|
+
return destination;
|
|
270
|
+
}
|
|
271
|
+
var MANIFEST_IGNORED_PATHS = /* @__PURE__ */ new Set([
|
|
272
|
+
"artifact-manifest.json",
|
|
273
|
+
// These are append-only/control-plane files rather than immutable stage
|
|
274
|
+
// outputs. Including them would invalidate a manifest whenever the store
|
|
275
|
+
// records the next stage event or refreshes the canonical request copy.
|
|
276
|
+
"progress.jsonl",
|
|
277
|
+
"request.json",
|
|
278
|
+
RUN_OWNER_FILE,
|
|
279
|
+
ARTIFACT_WORKFLOW_LOCK_FILE
|
|
280
|
+
]);
|
|
281
|
+
function portableRelative(root, path) {
|
|
282
|
+
return relative(root, path).split(sep).join("/");
|
|
283
|
+
}
|
|
284
|
+
function shouldManifest(path) {
|
|
285
|
+
const atomicTemporary = /\.\d+\.\d+\.[0-9a-f-]{36}\.tmp$/i.test(path);
|
|
286
|
+
return !MANIFEST_IGNORED_PATHS.has(path) && !atomicTemporary;
|
|
287
|
+
}
|
|
288
|
+
async function listArtifactFiles(root, current = root) {
|
|
289
|
+
const files = [];
|
|
290
|
+
for (const entry of await readdir(current, { withFileTypes: true }).catch(() => [])) {
|
|
291
|
+
const absolute = join2(current, entry.name);
|
|
292
|
+
if (entry.isSymbolicLink()) {
|
|
293
|
+
throw new Error(`Artifact tree must not contain symbolic links: ${absolute}`);
|
|
294
|
+
}
|
|
295
|
+
if (entry.isDirectory()) {
|
|
296
|
+
if (portableRelative(root, absolute) === "inference-checkpoints") continue;
|
|
297
|
+
files.push(...await listArtifactFiles(root, absolute));
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (!entry.isFile()) continue;
|
|
301
|
+
const relativePath = portableRelative(root, absolute);
|
|
302
|
+
if (!shouldManifest(relativePath)) continue;
|
|
303
|
+
const metadata = await stat(absolute).catch(() => null);
|
|
304
|
+
if (metadata?.isFile()) files.push(relativePath);
|
|
305
|
+
}
|
|
306
|
+
return files.sort();
|
|
307
|
+
}
|
|
308
|
+
async function sha256File(path) {
|
|
309
|
+
const hash = createHash("sha256");
|
|
310
|
+
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
|
311
|
+
return hash.digest("hex");
|
|
312
|
+
}
|
|
313
|
+
async function describeArtifactFile(root, path) {
|
|
314
|
+
const absolute = join2(root, path);
|
|
315
|
+
const metadata = await stat(absolute);
|
|
316
|
+
return {
|
|
317
|
+
path,
|
|
318
|
+
size_bytes: metadata.size,
|
|
319
|
+
sha256: await sha256File(absolute)
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
function assertManifestFiles(value, label) {
|
|
323
|
+
if (!Array.isArray(value)) throw new Error(`Artifact manifest ${label} must be an array.`);
|
|
324
|
+
const paths = /* @__PURE__ */ new Set();
|
|
325
|
+
for (const file of value) {
|
|
326
|
+
if (!file || typeof file.path !== "string" || !Number.isSafeInteger(file.size_bytes) || file.size_bytes < 0 || typeof file.sha256 !== "string" || !/^[a-f0-9]{64}$/.test(file.sha256)) {
|
|
327
|
+
throw new Error(`Artifact manifest contains an invalid ${label} entry.`);
|
|
328
|
+
}
|
|
329
|
+
const manifestPath = file.path;
|
|
330
|
+
const segments = manifestPath.split("/");
|
|
331
|
+
if (!manifestPath || manifestPath.includes("\\") || manifestPath.includes("\0") || manifestPath.startsWith("/") || /^[a-z]:/i.test(manifestPath) || segments.some((segment) => !segment || segment === "." || segment === "..")) {
|
|
332
|
+
throw new Error(`Artifact manifest contains an unsafe ${label} path: ${manifestPath}`);
|
|
333
|
+
}
|
|
334
|
+
if (paths.has(manifestPath)) throw new Error(`Artifact manifest contains a duplicate ${label} path: ${manifestPath}`);
|
|
335
|
+
paths.add(manifestPath);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function hasPeftAdapterFiles(files) {
|
|
339
|
+
const names = new Set(
|
|
340
|
+
files.filter((file) => file.size_bytes > 0).map((file) => file.path.split("/").at(-1)?.toLowerCase())
|
|
341
|
+
);
|
|
342
|
+
return names.has("adapter_config.json") && (names.has("adapter_model.safetensors") || names.has("adapter_model.bin"));
|
|
343
|
+
}
|
|
344
|
+
function parseArtifactManifest(value) {
|
|
345
|
+
if (!value || typeof value !== "object") throw new Error("Artifact manifest must be an object.");
|
|
346
|
+
const candidate = value;
|
|
347
|
+
if (candidate.schema_version !== 1 || !Array.isArray(candidate.files)) {
|
|
348
|
+
throw new Error("Unsupported or invalid artifact manifest.");
|
|
349
|
+
}
|
|
350
|
+
assertManifestFiles(candidate.files, "file");
|
|
351
|
+
if (candidate.model) {
|
|
352
|
+
const model = candidate.model;
|
|
353
|
+
const allowedModelKeys = /* @__PURE__ */ new Set([
|
|
354
|
+
"artifact_kind",
|
|
355
|
+
"format",
|
|
356
|
+
"framework",
|
|
357
|
+
"base_model",
|
|
358
|
+
"base_model_revision",
|
|
359
|
+
"base_model_artifact_uri",
|
|
360
|
+
"base_model_fingerprint",
|
|
361
|
+
"artifact_uri",
|
|
362
|
+
"artifact_root",
|
|
363
|
+
"servable",
|
|
364
|
+
"files"
|
|
365
|
+
]);
|
|
366
|
+
const hasBaseArtifact = model.base_model_artifact_uri !== void 0;
|
|
367
|
+
const hasBaseFingerprint = model.base_model_fingerprint !== void 0;
|
|
368
|
+
if (Object.keys(model).some((key) => !allowedModelKeys.has(key)) || model.artifact_kind !== "file" && model.artifact_kind !== "directory" || model.format !== "tar.gz" && model.format !== "huggingface-directory" || model.framework !== "transformers-peft" || typeof model.base_model !== "string" || model.base_model_revision !== void 0 && typeof model.base_model_revision !== "string" || model.base_model_artifact_uri !== void 0 && typeof model.base_model_artifact_uri !== "string" || model.base_model_fingerprint !== void 0 && typeof model.base_model_fingerprint !== "string" || hasBaseArtifact !== hasBaseFingerprint || typeof model.artifact_uri !== "string" || typeof model.artifact_root !== "string" || model.servable !== true || !Array.isArray(model.files) || model.files.length === 0 || model.artifact_kind === "file" && model.format !== "tar.gz" || model.artifact_kind === "directory" && model.format !== "huggingface-directory") {
|
|
369
|
+
throw new Error("Artifact manifest contains invalid model contract metadata.");
|
|
370
|
+
}
|
|
371
|
+
assertManifestFiles(model.files, "model file");
|
|
372
|
+
if (model.artifact_kind === "directory" && !hasPeftAdapterFiles(model.files)) {
|
|
373
|
+
throw new Error("PEFT model manifest requires adapter weights and adapter_config.json.");
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return candidate;
|
|
377
|
+
}
|
|
378
|
+
async function writeArtifactManifest(artifacts, options = {}) {
|
|
379
|
+
const paths = await listArtifactFiles(artifacts.runDir);
|
|
380
|
+
let model;
|
|
381
|
+
if (options.model) {
|
|
382
|
+
if (options.model.framework !== "transformers-peft" || options.model.servable !== true || options.model.artifact_kind === "file" && options.model.format !== "tar.gz" || options.model.artifact_kind === "directory" && options.model.format !== "huggingface-directory") {
|
|
383
|
+
throw new Error("TT local model artifacts must be verified, servable Transformers PEFT adapters.");
|
|
384
|
+
}
|
|
385
|
+
if (options.model.base_model_artifact_uri === void 0 !== (options.model.base_model_fingerprint === void 0)) {
|
|
386
|
+
throw new Error("Model contracts must record a local base-model URI and fingerprint together.");
|
|
387
|
+
}
|
|
388
|
+
const modelRoot = resolve2(options.model.artifact_root);
|
|
389
|
+
const modelPaths = options.model.artifact_kind === "file" ? [portableRelative(dirname(modelRoot), modelRoot)] : await listArtifactFiles(modelRoot);
|
|
390
|
+
const checksumRoot = options.model.artifact_kind === "file" ? dirname(modelRoot) : modelRoot;
|
|
391
|
+
if (options.model.artifact_kind === "file") {
|
|
392
|
+
if (!modelRoot.toLowerCase().endsWith(".tar.gz")) {
|
|
393
|
+
throw new Error(`PEFT file artifacts must be .tar.gz archives: ${modelRoot}`);
|
|
394
|
+
}
|
|
395
|
+
const archive = await verifyTarGzipArchive(modelRoot);
|
|
396
|
+
if (archive.adapter_weight_entries === 0 || archive.adapter_weight_bytes === 0) {
|
|
397
|
+
throw new Error(`PEFT model archive contains no adapter_model.safetensors or adapter_model.bin: ${modelRoot}`);
|
|
398
|
+
}
|
|
399
|
+
if (archive.adapter_config_entries === 0) {
|
|
400
|
+
throw new Error(`PEFT model archive contains no adapter_config.json: ${modelRoot}`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
const modelFiles = await Promise.all(modelPaths.map((path) => describeArtifactFile(checksumRoot, path)));
|
|
404
|
+
if (options.model.artifact_kind === "directory" && !hasPeftAdapterFiles(modelFiles)) {
|
|
405
|
+
throw new Error(`PEFT model directory requires adapter weights and adapter_config.json: ${modelRoot}`);
|
|
406
|
+
}
|
|
407
|
+
model = {
|
|
408
|
+
...options.model,
|
|
409
|
+
artifact_root: modelRoot,
|
|
410
|
+
files: modelFiles
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
const manifest = {
|
|
414
|
+
schema_version: 1,
|
|
415
|
+
generated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
416
|
+
files: await Promise.all(paths.map((path) => describeArtifactFile(artifacts.runDir, path))),
|
|
417
|
+
...model ? { model } : {}
|
|
418
|
+
};
|
|
419
|
+
await writeJsonAtomic(artifacts.artifactManifestJson, manifest);
|
|
420
|
+
return manifest;
|
|
421
|
+
}
|
|
422
|
+
async function verifyArtifactManifest(manifestPath, options = {}) {
|
|
423
|
+
const root = dirname(resolve2(manifestPath));
|
|
424
|
+
const manifest = parseArtifactManifest(JSON.parse(await readFile(manifestPath, "utf8")));
|
|
425
|
+
const expected = new Map(manifest.files.map((file) => [file.path, file]));
|
|
426
|
+
const actualPaths = await listArtifactFiles(root);
|
|
427
|
+
const actual = new Set(actualPaths);
|
|
428
|
+
const missing = [];
|
|
429
|
+
const changed = [];
|
|
430
|
+
const normalizedRequired = (options.requiredPaths ?? []).map((path) => path.split(sep).join("/").replace(/^\.\//, ""));
|
|
431
|
+
const checkedEntries = options.scopeToRequired ? normalizedRequired.flatMap((path) => {
|
|
432
|
+
const entry = expected.get(path);
|
|
433
|
+
return entry ? [[path, entry]] : [];
|
|
434
|
+
}) : [...expected.entries()];
|
|
435
|
+
for (const [path, expectedFile] of checkedEntries) {
|
|
436
|
+
if (!actual.has(path)) {
|
|
437
|
+
missing.push(path);
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
const actualFile = await describeArtifactFile(root, path);
|
|
441
|
+
if (actualFile.size_bytes !== expectedFile.size_bytes || actualFile.sha256 !== expectedFile.sha256) {
|
|
442
|
+
changed.push(path);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
for (const normalized of normalizedRequired) {
|
|
446
|
+
if (!expected.has(normalized) && !missing.includes(normalized)) missing.push(normalized);
|
|
447
|
+
}
|
|
448
|
+
const unexpected = options.allowUnexpected || options.scopeToRequired ? [] : actualPaths.filter((path) => !expected.has(path));
|
|
449
|
+
let modelChecked = 0;
|
|
450
|
+
if (manifest.model && (options.verifyModel ?? !options.scopeToRequired)) {
|
|
451
|
+
const modelRoot = resolve2(manifest.model.artifact_root);
|
|
452
|
+
const checksumRoot = manifest.model.artifact_kind === "file" ? dirname(modelRoot) : modelRoot;
|
|
453
|
+
const modelActualPaths = manifest.model.artifact_kind === "file" ? [portableRelative(checksumRoot, modelRoot)] : await listArtifactFiles(modelRoot);
|
|
454
|
+
const modelActual = new Set(modelActualPaths);
|
|
455
|
+
for (const expectedFile of manifest.model.files) {
|
|
456
|
+
const label = `model:${expectedFile.path}`;
|
|
457
|
+
if (!modelActual.has(expectedFile.path)) {
|
|
458
|
+
missing.push(label);
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
const actualFile = await describeArtifactFile(checksumRoot, expectedFile.path);
|
|
462
|
+
modelChecked += 1;
|
|
463
|
+
if (actualFile.size_bytes !== expectedFile.size_bytes || actualFile.sha256 !== expectedFile.sha256) {
|
|
464
|
+
changed.push(label);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (!options.allowUnexpected) {
|
|
468
|
+
const expectedModelPaths = new Set(manifest.model.files.map((file) => file.path));
|
|
469
|
+
unexpected.push(...modelActualPaths.filter((path) => !expectedModelPaths.has(path)).map((path) => `model:${path}`));
|
|
470
|
+
}
|
|
471
|
+
if (manifest.model.artifact_kind === "file") {
|
|
472
|
+
try {
|
|
473
|
+
const archive = await verifyTarGzipArchive(modelRoot);
|
|
474
|
+
if (archive.adapter_weight_entries === 0 || archive.adapter_weight_bytes === 0) {
|
|
475
|
+
changed.push(`model:${portableRelative(checksumRoot, modelRoot)}:missing_adapter_weights`);
|
|
476
|
+
}
|
|
477
|
+
if (archive.adapter_config_entries === 0) {
|
|
478
|
+
changed.push(`model:${portableRelative(checksumRoot, modelRoot)}:missing_adapter_config`);
|
|
479
|
+
}
|
|
480
|
+
} catch {
|
|
481
|
+
changed.push(`model:${portableRelative(checksumRoot, modelRoot)}:invalid_archive`);
|
|
482
|
+
}
|
|
483
|
+
} else if (!hasPeftAdapterFiles(
|
|
484
|
+
await Promise.all(modelActualPaths.map((path) => describeArtifactFile(checksumRoot, path)))
|
|
485
|
+
)) {
|
|
486
|
+
changed.push("model:missing_peft_adapter");
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return {
|
|
490
|
+
valid: missing.length === 0 && changed.length === 0 && unexpected.length === 0,
|
|
491
|
+
checked: checkedEntries.length + modelChecked,
|
|
492
|
+
missing: [...new Set(missing)].sort(),
|
|
493
|
+
changed: changed.sort(),
|
|
494
|
+
unexpected: unexpected.sort()
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
async function verifyTarGzipArchive(path) {
|
|
498
|
+
const maxEntries = 1e5;
|
|
499
|
+
const maxExpandedBytes = 20 * 1024 * 1024 * 1024;
|
|
500
|
+
const maxPaxBytes = 1024 * 1024;
|
|
501
|
+
const safeMemberPath = (value) => {
|
|
502
|
+
const normalized = value.replaceAll("\\", "/");
|
|
503
|
+
const segments = normalized.split("/").filter(Boolean);
|
|
504
|
+
if (!normalized || normalized.startsWith("/") || /^[a-z]:\//i.test(normalized) || segments.includes("..")) {
|
|
505
|
+
throw new Error(`Unsafe tar member path ${JSON.stringify(normalized)} in ${path}.`);
|
|
506
|
+
}
|
|
507
|
+
return normalized;
|
|
508
|
+
};
|
|
509
|
+
const parsePax = (payload, global) => {
|
|
510
|
+
let offset = 0;
|
|
511
|
+
let memberPath;
|
|
512
|
+
const allowedMetadata = /* @__PURE__ */ new Set(["mtime", "atime", "ctime", "uid", "gid", "uname", "gname"]);
|
|
513
|
+
while (offset < payload.length) {
|
|
514
|
+
const separator = payload.indexOf(32, offset);
|
|
515
|
+
if (separator < 0) throw new Error(`Malformed PAX record in ${path}.`);
|
|
516
|
+
const lengthText = payload.subarray(offset, separator).toString("ascii");
|
|
517
|
+
if (!/^[1-9][0-9]*$/.test(lengthText)) throw new Error(`Malformed PAX record length in ${path}.`);
|
|
518
|
+
const length = Number.parseInt(lengthText, 10);
|
|
519
|
+
const end = offset + length;
|
|
520
|
+
if (!Number.isSafeInteger(length) || end > payload.length || payload[end - 1] !== 10) {
|
|
521
|
+
throw new Error(`Truncated PAX record in ${path}.`);
|
|
522
|
+
}
|
|
523
|
+
const record = payload.subarray(separator + 1, end - 1).toString("utf8");
|
|
524
|
+
const equals = record.indexOf("=");
|
|
525
|
+
if (equals <= 0) throw new Error(`Malformed PAX key/value in ${path}.`);
|
|
526
|
+
const key = record.slice(0, equals);
|
|
527
|
+
const value = record.slice(equals + 1);
|
|
528
|
+
if (key === "path") {
|
|
529
|
+
if (global) throw new Error(`Global PAX path overrides are unsupported in ${path}.`);
|
|
530
|
+
memberPath = safeMemberPath(value);
|
|
531
|
+
} else if (!allowedMetadata.has(key)) {
|
|
532
|
+
throw new Error(`Unsupported PAX key ${JSON.stringify(key)} in ${path}.`);
|
|
533
|
+
}
|
|
534
|
+
offset = end;
|
|
535
|
+
}
|
|
536
|
+
return memberPath;
|
|
537
|
+
};
|
|
538
|
+
const gunzip = createReadStream(path).pipe(createGunzip());
|
|
539
|
+
let buffered = Buffer.alloc(0);
|
|
540
|
+
let payloadBytes = 0;
|
|
541
|
+
let paddingBytes = 0;
|
|
542
|
+
let paxType = null;
|
|
543
|
+
let paxChunks = [];
|
|
544
|
+
let pendingPaxPath;
|
|
545
|
+
let entries = 0;
|
|
546
|
+
let payloadEntries = 0;
|
|
547
|
+
let recognizedPayloadEntries = 0;
|
|
548
|
+
let recognizedPayloadBytes = 0;
|
|
549
|
+
let adapterWeightEntries = 0;
|
|
550
|
+
let adapterWeightBytes = 0;
|
|
551
|
+
let fullModelWeightEntries = 0;
|
|
552
|
+
let fullModelWeightBytes = 0;
|
|
553
|
+
let adapterConfigEntries = 0;
|
|
554
|
+
let expandedBytes = 0;
|
|
555
|
+
let zeroBlocks = 0;
|
|
556
|
+
let ended = false;
|
|
557
|
+
for await (const chunk of gunzip) {
|
|
558
|
+
buffered = Buffer.concat([buffered, Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)]);
|
|
559
|
+
while (true) {
|
|
560
|
+
if (payloadBytes > 0) {
|
|
561
|
+
if (buffered.length === 0) break;
|
|
562
|
+
const consumed = Math.min(payloadBytes, buffered.length);
|
|
563
|
+
if (paxType) paxChunks.push(buffered.subarray(0, consumed));
|
|
564
|
+
buffered = buffered.subarray(consumed);
|
|
565
|
+
payloadBytes -= consumed;
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
if (paddingBytes > 0) {
|
|
569
|
+
if (buffered.length === 0) break;
|
|
570
|
+
const consumed = Math.min(paddingBytes, buffered.length);
|
|
571
|
+
buffered = buffered.subarray(consumed);
|
|
572
|
+
paddingBytes -= consumed;
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
if (paxType) {
|
|
576
|
+
const paxPath = parsePax(Buffer.concat(paxChunks), paxType === "g");
|
|
577
|
+
if (paxType === "x") pendingPaxPath = paxPath;
|
|
578
|
+
paxType = null;
|
|
579
|
+
paxChunks = [];
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
if (buffered.length < 512) break;
|
|
583
|
+
const header = buffered.subarray(0, 512);
|
|
584
|
+
buffered = buffered.subarray(512);
|
|
585
|
+
if (header.every((byte) => byte === 0)) {
|
|
586
|
+
zeroBlocks += 1;
|
|
587
|
+
if (zeroBlocks >= 2) ended = true;
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
if (ended) throw new Error(`Tar archive contains data after its end marker: ${path}.`);
|
|
591
|
+
zeroBlocks = 0;
|
|
592
|
+
const checksumText = header.subarray(148, 156).toString("ascii").replace(/\0/g, "").trim();
|
|
593
|
+
if (!/^[0-7]+$/.test(checksumText)) throw new Error(`Invalid tar checksum field in ${path}.`);
|
|
594
|
+
const expectedChecksum = Number.parseInt(checksumText, 8);
|
|
595
|
+
let actualChecksum = 0;
|
|
596
|
+
for (let index = 0; index < header.length; index += 1) {
|
|
597
|
+
actualChecksum += index >= 148 && index < 156 ? 32 : header[index];
|
|
598
|
+
}
|
|
599
|
+
if (actualChecksum !== expectedChecksum) throw new Error(`Tar member checksum mismatch in ${path}.`);
|
|
600
|
+
const sizeText = header.subarray(124, 136).toString("ascii").replace(/\0/g, "").trim();
|
|
601
|
+
if (sizeText && !/^[0-7]+$/.test(sizeText)) throw new Error(`Invalid tar member size in ${path}.`);
|
|
602
|
+
const size = sizeText ? Number.parseInt(sizeText, 8) : 0;
|
|
603
|
+
if (!Number.isSafeInteger(size) || size < 0) throw new Error(`Invalid tar member size in ${path}.`);
|
|
604
|
+
const type = String.fromCharCode(header[156] ?? 0);
|
|
605
|
+
if (type !== "\0" && type !== "0" && type !== "5" && type !== "x" && type !== "g") {
|
|
606
|
+
throw new Error(`Unsafe or unsupported tar member type ${JSON.stringify(type)} in ${path}.`);
|
|
607
|
+
}
|
|
608
|
+
const shortName = header.subarray(0, 100).toString("utf8").replace(/\0.*$/, "");
|
|
609
|
+
const prefix = header.subarray(345, 500).toString("utf8").replace(/\0.*$/, "");
|
|
610
|
+
const headerPath = safeMemberPath(prefix ? `${prefix}/${shortName}` : shortName);
|
|
611
|
+
const memberPath = type === "x" || type === "g" ? headerPath : safeMemberPath(pendingPaxPath ?? headerPath);
|
|
612
|
+
if (type !== "x" && type !== "g") pendingPaxPath = void 0;
|
|
613
|
+
const segments = memberPath.split("/").filter(Boolean);
|
|
614
|
+
const name = segments.at(-1)?.toLowerCase();
|
|
615
|
+
const metadataOnly = name === "training-metrics.json" || name === "trainer_state.json" || name === "training_args.bin" || name === "config.json" || name === "generation_config.json" || name === "adapter_config.json" || name === "readme.md" || name?.startsWith("tokenizer") || name?.startsWith("special_tokens") || name?.startsWith("added_tokens") || name === "vocab.json" || name === "merges.txt";
|
|
616
|
+
const recognizedPayload = Boolean(name) && name !== "training_args.bin" && (name.endsWith(".safetensors") || name.endsWith(".bin"));
|
|
617
|
+
const adapterWeight = name === "adapter_model.safetensors" || name === "adapter_model.bin";
|
|
618
|
+
const fullModelWeight = Boolean(name) && (name.endsWith(".safetensors") && !name.startsWith("adapter_") || /^pytorch_model.*\.bin$/.test(name));
|
|
619
|
+
if (size > 0 && name === "adapter_config.json" && (type === "\0" || type === "0")) {
|
|
620
|
+
adapterConfigEntries += 1;
|
|
621
|
+
}
|
|
622
|
+
if ((type === "x" || type === "g") && size > maxPaxBytes) {
|
|
623
|
+
throw new Error(`PAX metadata exceeds 1 MiB in ${path}.`);
|
|
624
|
+
}
|
|
625
|
+
if (size > 0 && !metadataOnly && (type === "\0" || type === "0")) payloadEntries += 1;
|
|
626
|
+
if (size > 0 && recognizedPayload && (type === "\0" || type === "0")) {
|
|
627
|
+
recognizedPayloadEntries += 1;
|
|
628
|
+
recognizedPayloadBytes += size;
|
|
629
|
+
}
|
|
630
|
+
if (size > 0 && adapterWeight && (type === "\0" || type === "0")) {
|
|
631
|
+
adapterWeightEntries += 1;
|
|
632
|
+
adapterWeightBytes += size;
|
|
633
|
+
}
|
|
634
|
+
if (size > 0 && fullModelWeight && (type === "\0" || type === "0")) {
|
|
635
|
+
fullModelWeightEntries += 1;
|
|
636
|
+
fullModelWeightBytes += size;
|
|
637
|
+
}
|
|
638
|
+
expandedBytes += size;
|
|
639
|
+
if (expandedBytes > maxExpandedBytes) throw new Error(`Tar archive exceeds 20 GiB expanded size: ${path}.`);
|
|
640
|
+
payloadBytes = size;
|
|
641
|
+
paddingBytes = (512 - size % 512) % 512;
|
|
642
|
+
paxType = type === "x" || type === "g" ? type : null;
|
|
643
|
+
paxChunks = [];
|
|
644
|
+
entries += 1;
|
|
645
|
+
if (entries > maxEntries) throw new Error(`Tar archive exceeds ${maxEntries} members: ${path}.`);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
if (payloadBytes !== 0 || paddingBytes !== 0 || paxType || buffered.some((byte) => byte !== 0)) {
|
|
649
|
+
throw new Error(`Truncated tar archive: ${path}.`);
|
|
650
|
+
}
|
|
651
|
+
if (pendingPaxPath) throw new Error(`PAX path override has no following member in ${path}.`);
|
|
652
|
+
if (!ended) throw new Error(`Tar archive is missing its end marker: ${path}.`);
|
|
653
|
+
if (entries === 0) throw new Error(`Empty tar archive: ${path}.`);
|
|
654
|
+
if (payloadEntries === 0) throw new Error(`Tar archive contains no model payload files: ${path}.`);
|
|
655
|
+
return {
|
|
656
|
+
entries,
|
|
657
|
+
payload_entries: payloadEntries,
|
|
658
|
+
recognized_payload_entries: recognizedPayloadEntries,
|
|
659
|
+
recognized_payload_bytes: recognizedPayloadBytes,
|
|
660
|
+
adapter_weight_entries: adapterWeightEntries,
|
|
661
|
+
adapter_weight_bytes: adapterWeightBytes,
|
|
662
|
+
full_model_weight_entries: fullModelWeightEntries,
|
|
663
|
+
full_model_weight_bytes: fullModelWeightBytes,
|
|
664
|
+
adapter_config_entries: adapterConfigEntries,
|
|
665
|
+
expanded_bytes: expandedBytes
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
async function assertArtifactManifest(manifestPath, options = {}) {
|
|
669
|
+
const result = await verifyArtifactManifest(manifestPath, options);
|
|
670
|
+
if (!result.valid) {
|
|
671
|
+
const details = [
|
|
672
|
+
result.missing.length ? `missing=${result.missing.join(",")}` : "",
|
|
673
|
+
result.changed.length ? `changed=${result.changed.join(",")}` : "",
|
|
674
|
+
result.unexpected.length ? `unexpected=${result.unexpected.join(",")}` : ""
|
|
675
|
+
].filter(Boolean).join(" ");
|
|
676
|
+
throw new Error(`Artifact integrity verification failed${details ? `: ${details}` : "."}`);
|
|
677
|
+
}
|
|
678
|
+
return result;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// src/local-runtime/model-registry.ts
|
|
682
|
+
var QWEN_3_5_2B_REVISION = "15852e8c16360a2fea060d615a32b45270f8a8fc";
|
|
683
|
+
var NEMOTRON_BF16_REVISION = "ce38b6ab8b252b4b8ee7165b4605e93191cafd73";
|
|
684
|
+
var MUSE_GLIMMER_BF16_REVISION = "a4e59da52a7bc87ae7251dd5545c0dd437c44b68";
|
|
685
|
+
var TRAINING_MODELS = [
|
|
686
|
+
{
|
|
687
|
+
id: "Qwen/Qwen3.5-2B",
|
|
688
|
+
family: "qwen3_5",
|
|
689
|
+
defaultRevision: QWEN_3_5_2B_REVISION,
|
|
690
|
+
modelLoader: "causal_lm",
|
|
691
|
+
defaultLearningRate: 1e-5,
|
|
692
|
+
defaultPerDeviceBatchSize: 1,
|
|
693
|
+
defaultGradientAccumulationSteps: 8,
|
|
694
|
+
defaultLoraRank: 16,
|
|
695
|
+
defaultLoraAlpha: 32,
|
|
696
|
+
defaultLoraDropout: 0.05,
|
|
697
|
+
defaultMaxSeqLength: 2048,
|
|
698
|
+
loraTargetModules: "all-linear",
|
|
699
|
+
gradientCheckpointing: false
|
|
700
|
+
},
|
|
701
|
+
{
|
|
702
|
+
id: "nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-BF16",
|
|
703
|
+
family: "nemotron_h",
|
|
704
|
+
defaultRevision: NEMOTRON_BF16_REVISION,
|
|
705
|
+
modelLoader: "causal_lm",
|
|
706
|
+
defaultLearningRate: 1e-5,
|
|
707
|
+
defaultPerDeviceBatchSize: 1,
|
|
708
|
+
defaultGradientAccumulationSteps: 8,
|
|
709
|
+
defaultLoraRank: 16,
|
|
710
|
+
defaultLoraAlpha: 32,
|
|
711
|
+
defaultLoraDropout: 0.05,
|
|
712
|
+
defaultMaxSeqLength: 1024,
|
|
713
|
+
// Avoid one LoRA pair for every routed expert matrix. Adapt the shared
|
|
714
|
+
// attention (q/k/v/o) and Mamba input projections instead, keeping the
|
|
715
|
+
// adapter bounded. out_proj/conv1d are rejected by PEFT for Mamba models.
|
|
716
|
+
loraTargetModules: ["q_proj", "k_proj", "v_proj", "o_proj", "in_proj"],
|
|
717
|
+
gradientCheckpointing: true
|
|
718
|
+
},
|
|
719
|
+
{
|
|
720
|
+
id: "meta-models/Muse-Glimmer-30B",
|
|
721
|
+
family: "muse_glimmer",
|
|
722
|
+
defaultRevision: MUSE_GLIMMER_BF16_REVISION,
|
|
723
|
+
// Muse Glimmer is a vision-language model whose text tower is not exposed
|
|
724
|
+
// through the causal-LM auto mapping, so the bundled runtime loads the full
|
|
725
|
+
// conditional-generation checkpoint (vision tower frozen and unused) via
|
|
726
|
+
// AutoModelForImageTextToText for text-only SFT.
|
|
727
|
+
modelLoader: "image_text_to_text",
|
|
728
|
+
defaultLearningRate: 1e-5,
|
|
729
|
+
defaultPerDeviceBatchSize: 1,
|
|
730
|
+
defaultGradientAccumulationSteps: 8,
|
|
731
|
+
defaultLoraRank: 16,
|
|
732
|
+
defaultLoraAlpha: 32,
|
|
733
|
+
defaultLoraDropout: 0.05,
|
|
734
|
+
defaultMaxSeqLength: 2048,
|
|
735
|
+
loraTargetModules: "all-linear",
|
|
736
|
+
gradientCheckpointing: true
|
|
737
|
+
}
|
|
738
|
+
];
|
|
739
|
+
var HUGGING_FACE_REPO_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,95}\/[A-Za-z0-9][A-Za-z0-9._-]{0,95}$/;
|
|
740
|
+
function uncertifiedTrainingModel(id) {
|
|
741
|
+
return {
|
|
742
|
+
id,
|
|
743
|
+
family: "uncertified",
|
|
744
|
+
modelLoader: "causal_lm",
|
|
745
|
+
defaultLearningRate: 1e-4,
|
|
746
|
+
defaultPerDeviceBatchSize: 1,
|
|
747
|
+
defaultGradientAccumulationSteps: 8,
|
|
748
|
+
defaultLoraRank: 16,
|
|
749
|
+
defaultLoraAlpha: 32,
|
|
750
|
+
defaultLoraDropout: 0.05,
|
|
751
|
+
defaultMaxSeqLength: 2048,
|
|
752
|
+
loraTargetModules: "all-linear",
|
|
753
|
+
gradientCheckpointing: true
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
function findCertifiedTrainingModel(modelId) {
|
|
757
|
+
const normalized = modelId.trim().toLowerCase();
|
|
758
|
+
return TRAINING_MODELS.find((candidate) => candidate.id.toLowerCase() === normalized);
|
|
759
|
+
}
|
|
760
|
+
function isCertifiedTrainingModel(modelId) {
|
|
761
|
+
return findCertifiedTrainingModel(modelId) !== void 0;
|
|
762
|
+
}
|
|
763
|
+
function resolveTrainingModel(modelId) {
|
|
764
|
+
const model = findCertifiedTrainingModel(modelId);
|
|
765
|
+
if (model) return model;
|
|
766
|
+
const id = modelId.trim();
|
|
767
|
+
if (HUGGING_FACE_REPO_ID.test(id)) return uncertifiedTrainingModel(id);
|
|
768
|
+
throw new Error(
|
|
769
|
+
`Unsupported base model "${modelId}". Use a certified model (${TRAINING_MODELS.map((item) => item.id).join(", ")}) or a Hugging Face repo id such as owner/name.`
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
function uncertifiedRevisionError(modelId) {
|
|
773
|
+
return new Error(
|
|
774
|
+
`${modelId} is not a TT-certified base model. To fine-tune it anyway, set hyperparameters.base_model_revision to its 40-character commit SHA (see https://huggingface.co/${modelId}/commits/main). Uncertified models load as text causal LMs with safetensors weights and no remote code.`
|
|
775
|
+
);
|
|
776
|
+
}
|
|
777
|
+
function canonicalizeTrainingModel(modelId) {
|
|
778
|
+
return resolveTrainingModel(modelId).id;
|
|
779
|
+
}
|
|
780
|
+
function resolveRequestedBaseModelRevision(modelId, requestedRevision) {
|
|
781
|
+
const model = resolveTrainingModel(modelId);
|
|
782
|
+
if (!isCertifiedTrainingModel(model.id) && !requestedRevision) {
|
|
783
|
+
throw uncertifiedRevisionError(model.id);
|
|
784
|
+
}
|
|
785
|
+
if (model.defaultRevision && requestedRevision && requestedRevision !== model.defaultRevision) {
|
|
786
|
+
throw new Error(
|
|
787
|
+
`${model.id} must use certified revision ${model.defaultRevision}; got ${requestedRevision}.`
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
return requestedRevision ?? model.defaultRevision;
|
|
791
|
+
}
|
|
792
|
+
function defaultBaseModelRevision(modelId) {
|
|
793
|
+
return resolveTrainingModel(modelId).defaultRevision;
|
|
794
|
+
}
|
|
795
|
+
function resolveModelLoader(modelId) {
|
|
796
|
+
return resolveTrainingModel(modelId).modelLoader;
|
|
797
|
+
}
|
|
798
|
+
var CERTIFIED_QWEN_TEXT_CONFIG = {
|
|
799
|
+
model_type: "qwen3_5_text",
|
|
800
|
+
hidden_size: 2048,
|
|
801
|
+
num_hidden_layers: 24,
|
|
802
|
+
num_attention_heads: 8,
|
|
803
|
+
num_key_value_heads: 2,
|
|
804
|
+
intermediate_size: 6144,
|
|
805
|
+
vocab_size: 248320
|
|
806
|
+
};
|
|
807
|
+
var CERTIFIED_MUSE_GLIMMER_TEXT_CONFIG = {
|
|
808
|
+
model_type: "muse_glimmer_text",
|
|
809
|
+
hidden_size: 6656,
|
|
810
|
+
num_hidden_layers: 52,
|
|
811
|
+
num_attention_heads: 32,
|
|
812
|
+
num_key_value_heads: 2,
|
|
813
|
+
intermediate_size: 19968,
|
|
814
|
+
vocab_size: 202048
|
|
815
|
+
};
|
|
816
|
+
var CERTIFIED_NEMOTRON_CONFIG = {
|
|
817
|
+
hidden_size: 2688,
|
|
818
|
+
num_hidden_layers: 52,
|
|
819
|
+
num_attention_heads: 32,
|
|
820
|
+
num_key_value_heads: 2,
|
|
821
|
+
intermediate_size: 1856,
|
|
822
|
+
vocab_size: 131072,
|
|
823
|
+
n_routed_experts: 128,
|
|
824
|
+
num_experts_per_tok: 6,
|
|
825
|
+
num_nextn_predict_layers: 1,
|
|
826
|
+
max_position_embeddings: 262144
|
|
827
|
+
};
|
|
828
|
+
function isRecord(value) {
|
|
829
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
830
|
+
}
|
|
831
|
+
function hasArchitecture(config, architecture) {
|
|
832
|
+
const architectures = config.architectures;
|
|
833
|
+
return Array.isArray(architectures) && architectures.includes(architecture);
|
|
834
|
+
}
|
|
835
|
+
function assertCertifiedTextTowerConfig(config, label, certifiedModelId, architecture, certifiedTextConfig) {
|
|
836
|
+
if (!hasArchitecture(config, architecture)) {
|
|
837
|
+
throw new Error(`${label} is not the certified ${certifiedModelId} architecture.`);
|
|
838
|
+
}
|
|
839
|
+
const textConfig = config.text_config;
|
|
840
|
+
if (!isRecord(textConfig)) {
|
|
841
|
+
throw new Error(`${label} is not the certified ${certifiedModelId} architecture.`);
|
|
842
|
+
}
|
|
843
|
+
for (const [key, expected] of Object.entries(certifiedTextConfig)) {
|
|
844
|
+
if (textConfig[key] !== expected) {
|
|
845
|
+
throw new Error(
|
|
846
|
+
`${label} is not the certified ${certifiedModelId} architecture: text_config.${key} must be ${JSON.stringify(expected)}.`
|
|
847
|
+
);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
function assertCertifiedBaseModelConfig(value, label = "base-model config.json", expectedModelId) {
|
|
852
|
+
if (!isRecord(value)) {
|
|
853
|
+
throw new Error(`${label} must contain a JSON object.`);
|
|
854
|
+
}
|
|
855
|
+
const config = value;
|
|
856
|
+
if (expectedModelId && !isCertifiedTrainingModel(expectedModelId)) return;
|
|
857
|
+
const expectedFamily = expectedModelId ? resolveTrainingModel(expectedModelId).family : void 0;
|
|
858
|
+
if (config.model_type === "qwen3_5") {
|
|
859
|
+
if (expectedFamily && expectedFamily !== "qwen3_5") {
|
|
860
|
+
throw new Error(`${label} does not match requested base model ${expectedModelId}.`);
|
|
861
|
+
}
|
|
862
|
+
assertCertifiedTextTowerConfig(
|
|
863
|
+
config,
|
|
864
|
+
label,
|
|
865
|
+
"Qwen/Qwen3.5-2B",
|
|
866
|
+
"Qwen3_5ForConditionalGeneration",
|
|
867
|
+
CERTIFIED_QWEN_TEXT_CONFIG
|
|
868
|
+
);
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
if (config.model_type === "muse_glimmer") {
|
|
872
|
+
if (expectedFamily && expectedFamily !== "muse_glimmer") {
|
|
873
|
+
throw new Error(`${label} does not match requested base model ${expectedModelId}.`);
|
|
874
|
+
}
|
|
875
|
+
assertCertifiedTextTowerConfig(
|
|
876
|
+
config,
|
|
877
|
+
label,
|
|
878
|
+
"meta-models/Muse-Glimmer-30B",
|
|
879
|
+
"MuseGlimmerForConditionalGeneration",
|
|
880
|
+
CERTIFIED_MUSE_GLIMMER_TEXT_CONFIG
|
|
881
|
+
);
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
if (config.model_type === "nemotron_h" && hasArchitecture(config, "NemotronHForCausalLM")) {
|
|
885
|
+
if (expectedFamily && expectedFamily !== "nemotron_h") {
|
|
886
|
+
throw new Error(`${label} does not match requested base model ${expectedModelId}.`);
|
|
887
|
+
}
|
|
888
|
+
for (const [key, expected] of Object.entries(CERTIFIED_NEMOTRON_CONFIG)) {
|
|
889
|
+
if (config[key] !== expected) {
|
|
890
|
+
throw new Error(
|
|
891
|
+
`${label} is not the certified nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-BF16 architecture: ${key} must be ${JSON.stringify(expected)}.`
|
|
892
|
+
);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
throw new Error(
|
|
898
|
+
`${label} is not a certified TT base-model architecture.`
|
|
899
|
+
);
|
|
900
|
+
}
|
|
901
|
+
function localModelArtifactPath(uri) {
|
|
902
|
+
if (uri.startsWith("file://")) {
|
|
903
|
+
const legacy = uri.slice("file://".length);
|
|
904
|
+
if (process.platform === "win32" && /^[a-z]:[\\/]/i.test(legacy)) return resolve3(legacy);
|
|
905
|
+
return fileURLToPath(new URL(uri));
|
|
906
|
+
}
|
|
907
|
+
if (isAbsolute3(uri)) return resolve3(uri);
|
|
908
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(uri)) {
|
|
909
|
+
throw new Error(`Model artifact must be a local path or file URI: ${uri}`);
|
|
910
|
+
}
|
|
911
|
+
return resolve3(uri);
|
|
912
|
+
}
|
|
913
|
+
function adapterWeightName(name) {
|
|
914
|
+
const lower = name.toLowerCase();
|
|
915
|
+
return lower === "adapter_model.safetensors" || lower === "adapter_model.bin";
|
|
916
|
+
}
|
|
917
|
+
async function inspectAdapterDirectory(path) {
|
|
918
|
+
let count = 0;
|
|
919
|
+
let bytes = 0;
|
|
920
|
+
let hasConfig = false;
|
|
921
|
+
for (const entry of await readdir2(path, { withFileTypes: true })) {
|
|
922
|
+
const child = resolve3(path, entry.name);
|
|
923
|
+
if (entry.isSymbolicLink()) {
|
|
924
|
+
throw new Error(`Model artifact must not contain symbolic links: ${child}`);
|
|
925
|
+
}
|
|
926
|
+
if (entry.isDirectory()) {
|
|
927
|
+
const nested = await inspectAdapterDirectory(child);
|
|
928
|
+
count += nested.count;
|
|
929
|
+
bytes += nested.bytes;
|
|
930
|
+
hasConfig ||= nested.hasConfig;
|
|
931
|
+
continue;
|
|
932
|
+
}
|
|
933
|
+
if (!entry.isFile()) continue;
|
|
934
|
+
const metadata = await lstat2(child);
|
|
935
|
+
if (adapterWeightName(entry.name) && metadata.size > 0) {
|
|
936
|
+
count += 1;
|
|
937
|
+
bytes += metadata.size;
|
|
938
|
+
}
|
|
939
|
+
if (entry.name.toLowerCase() === "adapter_config.json" && metadata.size > 0) {
|
|
940
|
+
hasConfig = true;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
return { count, bytes, hasConfig };
|
|
944
|
+
}
|
|
945
|
+
async function inspectModelArtifact(uri) {
|
|
946
|
+
const path = localModelArtifactPath(uri);
|
|
947
|
+
const metadata = await lstat2(path).catch(() => null);
|
|
948
|
+
if (!metadata) throw new Error(`Model artifact does not exist: ${path}`);
|
|
949
|
+
if (metadata.isSymbolicLink()) {
|
|
950
|
+
throw new Error(`Model artifact must not be a symbolic link: ${path}`);
|
|
951
|
+
}
|
|
952
|
+
if (metadata.isDirectory()) {
|
|
953
|
+
const adapter = await inspectAdapterDirectory(path);
|
|
954
|
+
return {
|
|
955
|
+
uri,
|
|
956
|
+
path,
|
|
957
|
+
kind: "directory",
|
|
958
|
+
adapter_weight_file_count: adapter.count,
|
|
959
|
+
adapter_weight_bytes: adapter.bytes,
|
|
960
|
+
has_adapter_config: adapter.hasConfig
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
if (!metadata.isFile() || !path.toLowerCase().endsWith(".tar.gz")) {
|
|
964
|
+
throw new Error(
|
|
965
|
+
`Model artifact must be a PEFT adapter directory or .tar.gz archive: ${path}`
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
const archive = await verifyTarGzipArchive(path);
|
|
969
|
+
return {
|
|
970
|
+
uri,
|
|
971
|
+
path,
|
|
972
|
+
kind: "file",
|
|
973
|
+
adapter_weight_file_count: archive.adapter_weight_entries,
|
|
974
|
+
adapter_weight_bytes: archive.adapter_weight_bytes,
|
|
975
|
+
has_adapter_config: archive.adapter_config_entries > 0
|
|
976
|
+
};
|
|
977
|
+
}
|
|
978
|
+
async function assertUsableModelArtifact(uri) {
|
|
979
|
+
const inspection = await inspectModelArtifact(uri);
|
|
980
|
+
if (inspection.adapter_weight_file_count === 0 || inspection.adapter_weight_bytes === 0) {
|
|
981
|
+
throw new Error(
|
|
982
|
+
`PEFT model artifact ${inspection.path} contains no non-empty adapter_model weights.`
|
|
983
|
+
);
|
|
984
|
+
}
|
|
985
|
+
if (!inspection.has_adapter_config) {
|
|
986
|
+
throw new Error(
|
|
987
|
+
`PEFT model artifact ${inspection.path} contains adapter weights but no non-empty adapter_config.json.`
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
return inspection;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
// src/local-runtime/decision-models.ts
|
|
994
|
+
var LAYA_REVISION = "55cf4c4ebb4ebe31b2550e8bdf3bd21b99753851";
|
|
995
|
+
var DECISION_MODELS = [
|
|
996
|
+
{
|
|
997
|
+
id: "convaiinnovations/laya",
|
|
998
|
+
family: "laya",
|
|
999
|
+
revision: LAYA_REVISION,
|
|
1000
|
+
parameters: "421M (ModernBERT-large encoder + typed decision head)",
|
|
1001
|
+
license: "Apache-2.0",
|
|
1002
|
+
defaultLearningRate: 2e-5,
|
|
1003
|
+
defaultBatchSize: 8,
|
|
1004
|
+
defaultEpochs: 3
|
|
1005
|
+
}
|
|
1006
|
+
];
|
|
1007
|
+
var DEFAULT_DECISION_MODEL = DECISION_MODELS[0].id;
|
|
1008
|
+
function resolveDecisionModel(modelId) {
|
|
1009
|
+
const normalized = modelId.trim().toLowerCase();
|
|
1010
|
+
const model = DECISION_MODELS.find((candidate) => candidate.id.toLowerCase() === normalized);
|
|
1011
|
+
if (!model) {
|
|
1012
|
+
throw new Error(
|
|
1013
|
+
`Unsupported decision model "${modelId}". Supported decision models: ${DECISION_MODELS.map((item) => item.id).join(", ")}`
|
|
1014
|
+
);
|
|
1015
|
+
}
|
|
1016
|
+
return model;
|
|
1017
|
+
}
|
|
1018
|
+
function isDecisionModel(modelId) {
|
|
1019
|
+
return DECISION_MODELS.some((candidate) => candidate.id.toLowerCase() === modelId.trim().toLowerCase());
|
|
1020
|
+
}
|
|
1021
|
+
function canonicalizeDecisionModel(modelId) {
|
|
1022
|
+
return resolveDecisionModel(modelId).id;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// src/local-runtime/contracts.ts
|
|
1026
|
+
var datasetFormatSchema = z2.literal("chat_jsonl");
|
|
1027
|
+
var baseModelRevisionSchema = z2.string().regex(/^[0-9a-f]{40}$/i, "base_model_revision must be a 40-character Hugging Face commit SHA").transform((value) => value.toLowerCase());
|
|
1028
|
+
var behaviorSpecExampleSchema = z2.object({
|
|
1029
|
+
input: z2.string().min(1),
|
|
1030
|
+
output: z2.string().min(1)
|
|
1031
|
+
}).strict();
|
|
1032
|
+
var behaviorSpecSchema = z2.object({
|
|
1033
|
+
name: z2.string().min(1),
|
|
1034
|
+
description: z2.string().default(""),
|
|
1035
|
+
system_prompt: z2.string().default(""),
|
|
1036
|
+
guidelines: z2.array(z2.string()).default([]),
|
|
1037
|
+
examples: z2.array(behaviorSpecExampleSchema).default([]),
|
|
1038
|
+
constraints: z2.array(z2.string()).default([])
|
|
1039
|
+
}).strict();
|
|
1040
|
+
var specSnapshotSchema = behaviorSpecSchema.extend({
|
|
1041
|
+
base_model: z2.string().transform((value) => canonicalizeTrainingModel(value))
|
|
1042
|
+
});
|
|
1043
|
+
var foundationHyperparametersSchema = z2.object({
|
|
1044
|
+
vocab_size: z2.number().int().min(64).max(65536).default(256),
|
|
1045
|
+
max_chars: z2.number().int().min(1e3).max(1e12).default(2e4),
|
|
1046
|
+
depth: z2.number().int().min(1).max(64).default(2),
|
|
1047
|
+
pretrain_steps: z2.number().int().min(1).max(1e7).default(2),
|
|
1048
|
+
finetune_steps: z2.number().int().min(1).max(1e7).default(2),
|
|
1049
|
+
rl_steps: z2.number().int().min(0).max(1e7).default(0),
|
|
1050
|
+
batch_size: z2.number().int().min(1).max(1024).default(2),
|
|
1051
|
+
sequence_length: z2.number().int().min(16).max(8192).default(64),
|
|
1052
|
+
nproc_per_node: z2.number().int().min(1).max(16).default(1),
|
|
1053
|
+
corpus_path: z2.string().min(1).optional(),
|
|
1054
|
+
tokenizer_max_chars: z2.number().int().min(1e3).max(1e12).optional(),
|
|
1055
|
+
validation_path: z2.string().min(1).optional(),
|
|
1056
|
+
checkpoint_backup_dir: z2.string().min(1).optional(),
|
|
1057
|
+
seed: z2.number().int().min(0).max(2147483647).optional(),
|
|
1058
|
+
learning_rate: z2.number().positive().max(1).optional(),
|
|
1059
|
+
weight_decay: z2.number().min(0).max(10).optional(),
|
|
1060
|
+
warmup_steps: z2.number().int().min(0).max(1e7).optional(),
|
|
1061
|
+
min_lr_ratio: z2.number().min(0).max(1).optional(),
|
|
1062
|
+
gradient_accumulation_steps: z2.number().int().min(1).max(1024).optional(),
|
|
1063
|
+
gradient_clip: z2.number().positive().max(1e3).optional(),
|
|
1064
|
+
bf16: z2.boolean().optional(),
|
|
1065
|
+
checkpoint_interval_steps: z2.number().int().min(1).max(1e7).optional(),
|
|
1066
|
+
checkpoint_interval_seconds: z2.number().int().min(1).max(86400).optional(),
|
|
1067
|
+
keep_checkpoints: z2.number().int().min(1).max(100).optional(),
|
|
1068
|
+
log_interval_steps: z2.number().int().min(1).max(1e6).optional()
|
|
1069
|
+
}).strict();
|
|
1070
|
+
var huggingFaceRepoIdSchema = z2.string().regex(
|
|
1071
|
+
/^[A-Za-z0-9][A-Za-z0-9._-]{0,95}\/[A-Za-z0-9][A-Za-z0-9._-]{0,95}$/,
|
|
1072
|
+
"huggingface.repo must be a Hugging Face dataset id such as org/name"
|
|
1073
|
+
);
|
|
1074
|
+
var datasetColumnsSchema = z2.union([
|
|
1075
|
+
z2.object({
|
|
1076
|
+
input: z2.string().min(1),
|
|
1077
|
+
output: z2.string().min(1)
|
|
1078
|
+
}).strict(),
|
|
1079
|
+
/** A chat column of `{role, content}` turns; its first user turn and reply become one example. */
|
|
1080
|
+
z2.object({ messages: z2.string().min(1) }).strict()
|
|
1081
|
+
]);
|
|
1082
|
+
var huggingFaceDatasetSourceSchema = z2.object({
|
|
1083
|
+
repo: huggingFaceRepoIdSchema,
|
|
1084
|
+
revision: baseModelRevisionSchema.optional(),
|
|
1085
|
+
/**
|
|
1086
|
+
* Field names for plain JSONL or Parquet records such as `{"text": ..., "label": ...}`,
|
|
1087
|
+
* or `{messages}` for a chat column. Each record becomes a chat row with the
|
|
1088
|
+
* spec's compiled system message. Omit for files that are already TT chat JSONL.
|
|
1089
|
+
*/
|
|
1090
|
+
columns: datasetColumnsSchema.optional()
|
|
1091
|
+
}).strict();
|
|
1092
|
+
function isRepoRelativePath(value) {
|
|
1093
|
+
if (/^(?:[A-Za-z]:|[/\\]|file:)/.test(value)) return false;
|
|
1094
|
+
return value.split(/[/\\]/).every((part) => part !== "" && part !== "." && part !== "..");
|
|
1095
|
+
}
|
|
1096
|
+
var datasetPrebuiltSchema = z2.object({
|
|
1097
|
+
training: z2.string().min(1),
|
|
1098
|
+
validation: z2.string().min(1).optional(),
|
|
1099
|
+
test: z2.string().min(1).optional(),
|
|
1100
|
+
format: datasetFormatSchema.default("chat_jsonl"),
|
|
1101
|
+
huggingface: huggingFaceDatasetSourceSchema.optional()
|
|
1102
|
+
}).strict().superRefine((dataset, context) => {
|
|
1103
|
+
if (!dataset.validation && !dataset.test) {
|
|
1104
|
+
context.addIssue({
|
|
1105
|
+
code: z2.ZodIssueCode.custom,
|
|
1106
|
+
path: ["validation"],
|
|
1107
|
+
message: "Provide a distinct validation or test split for held-out evaluation"
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1110
|
+
if (!dataset.huggingface) return;
|
|
1111
|
+
for (const key of ["training", "validation", "test"]) {
|
|
1112
|
+
const value = dataset[key];
|
|
1113
|
+
if (value === void 0) continue;
|
|
1114
|
+
if (!isRepoRelativePath(value)) {
|
|
1115
|
+
context.addIssue({
|
|
1116
|
+
code: z2.ZodIssueCode.custom,
|
|
1117
|
+
path: [key],
|
|
1118
|
+
message: `With dataset_prebuilt.huggingface, ${key} must be a file path inside the dataset repo, such as data/${key}.jsonl`
|
|
1119
|
+
});
|
|
1120
|
+
} else if (value.toLowerCase().endsWith(".parquet")) {
|
|
1121
|
+
if (!dataset.huggingface.columns) {
|
|
1122
|
+
context.addIssue({
|
|
1123
|
+
code: z2.ZodIssueCode.custom,
|
|
1124
|
+
path: [key],
|
|
1125
|
+
message: `${key} is a Parquet file; set dataset_prebuilt.huggingface.columns to map its records`
|
|
1126
|
+
});
|
|
1127
|
+
}
|
|
1128
|
+
} else if (!value.toLowerCase().endsWith(".jsonl")) {
|
|
1129
|
+
context.addIssue({
|
|
1130
|
+
code: z2.ZodIssueCode.custom,
|
|
1131
|
+
path: [key],
|
|
1132
|
+
message: `${key} must be a .jsonl or .parquet file; CSV and other formats are not supported yet`
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
});
|
|
1137
|
+
var datasetSourceFileSchema = z2.object({
|
|
1138
|
+
path: z2.string().min(1),
|
|
1139
|
+
size_bytes: z2.number().int().nonnegative(),
|
|
1140
|
+
sha256: z2.string().regex(/^[a-f0-9]{64}$/)
|
|
1141
|
+
}).strict();
|
|
1142
|
+
var datasetConversionStatsSchema = z2.object({
|
|
1143
|
+
records: z2.number().int().nonnegative(),
|
|
1144
|
+
kept: z2.number().int().nonnegative(),
|
|
1145
|
+
skipped_missing_fields: z2.number().int().nonnegative(),
|
|
1146
|
+
dropped_duplicate_inputs: z2.number().int().nonnegative(),
|
|
1147
|
+
dropped_eval_overlap: z2.number().int().nonnegative()
|
|
1148
|
+
}).strict();
|
|
1149
|
+
var datasetSourceSchema = z2.object({
|
|
1150
|
+
provider: z2.literal("huggingface"),
|
|
1151
|
+
repo: huggingFaceRepoIdSchema,
|
|
1152
|
+
requested_revision: baseModelRevisionSchema.nullable(),
|
|
1153
|
+
revision: baseModelRevisionSchema,
|
|
1154
|
+
pinned: z2.boolean(),
|
|
1155
|
+
columns: datasetColumnsSchema.nullable(),
|
|
1156
|
+
/** Per-split record accounting when `columns` converted plain JSONL records. */
|
|
1157
|
+
conversion: z2.object({
|
|
1158
|
+
training: datasetConversionStatsSchema,
|
|
1159
|
+
validation: datasetConversionStatsSchema.optional(),
|
|
1160
|
+
test: datasetConversionStatsSchema.optional()
|
|
1161
|
+
}).strict().nullable().optional(),
|
|
1162
|
+
snapshot_path: z2.string().min(1),
|
|
1163
|
+
files: z2.object({
|
|
1164
|
+
training: datasetSourceFileSchema,
|
|
1165
|
+
validation: datasetSourceFileSchema.optional(),
|
|
1166
|
+
test: datasetSourceFileSchema.optional()
|
|
1167
|
+
}).strict()
|
|
1168
|
+
}).strict();
|
|
1169
|
+
var fineTuneHyperparametersSchema = z2.object({
|
|
1170
|
+
n_epochs: z2.number().int().min(1).max(20).default(1),
|
|
1171
|
+
seed: z2.number().int().min(0).max(4294967295).optional(),
|
|
1172
|
+
learning_rate: z2.number().positive().optional(),
|
|
1173
|
+
batch_size: z2.number().int().min(1).optional(),
|
|
1174
|
+
lora_rank: z2.number().int().min(1).optional(),
|
|
1175
|
+
lora_alpha: z2.number().int().min(1).optional(),
|
|
1176
|
+
lora_dropout: z2.number().min(0).max(1).optional(),
|
|
1177
|
+
max_seq_length: z2.number().int().min(128).max(32768).optional(),
|
|
1178
|
+
gradient_accumulation_steps: z2.number().int().min(1).optional(),
|
|
1179
|
+
max_eval_examples: z2.number().int().min(1).optional(),
|
|
1180
|
+
/** Immutable Hugging Face commit used by every stage of the run. */
|
|
1181
|
+
base_model_revision: baseModelRevisionSchema.optional()
|
|
1182
|
+
}).strict();
|
|
1183
|
+
function requirePinnedUncertifiedModel(spec, context) {
|
|
1184
|
+
const baseModel = "spec_snapshot" in spec ? spec.spec_snapshot.base_model : spec.base_model;
|
|
1185
|
+
if (isCertifiedTrainingModel(baseModel) || spec.hyperparameters?.base_model_revision) return;
|
|
1186
|
+
context.addIssue({
|
|
1187
|
+
code: z2.ZodIssueCode.custom,
|
|
1188
|
+
path: ["hyperparameters", "base_model_revision"],
|
|
1189
|
+
message: uncertifiedRevisionError(baseModel).message
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
var fineTuneRunRequestSchema = z2.object({
|
|
1193
|
+
run_id: z2.string().uuid(),
|
|
1194
|
+
user_id: z2.string().min(1),
|
|
1195
|
+
behavior_spec_id: z2.string().uuid(),
|
|
1196
|
+
run_number: z2.number().int().min(1),
|
|
1197
|
+
spec_snapshot: specSnapshotSchema,
|
|
1198
|
+
hyperparameters: fineTuneHyperparametersSchema.default({ n_epochs: 1 }),
|
|
1199
|
+
dataset_prebuilt: datasetPrebuiltSchema.optional(),
|
|
1200
|
+
dataset_source: datasetSourceSchema.optional()
|
|
1201
|
+
}).strict().superRefine((request, context) => {
|
|
1202
|
+
requirePinnedUncertifiedModel(request, context);
|
|
1203
|
+
if (request.spec_snapshot.examples.length === 0 && !request.dataset_prebuilt) {
|
|
1204
|
+
context.addIssue({
|
|
1205
|
+
code: z2.ZodIssueCode.custom,
|
|
1206
|
+
path: ["spec_snapshot", "examples"],
|
|
1207
|
+
message: "Add examples or provide dataset_prebuilt training and validation files"
|
|
1208
|
+
});
|
|
1209
|
+
}
|
|
1210
|
+
});
|
|
1211
|
+
var evalExampleResultSchema = z2.object({
|
|
1212
|
+
prompt: z2.string(),
|
|
1213
|
+
expected: z2.string(),
|
|
1214
|
+
actual: z2.string(),
|
|
1215
|
+
passed: z2.boolean(),
|
|
1216
|
+
score: z2.number().min(0).max(1),
|
|
1217
|
+
reasoning: z2.string().nullable(),
|
|
1218
|
+
latency_ms: z2.number().int().nonnegative(),
|
|
1219
|
+
scored_by: z2.enum(["exact_match", "json_fields", "heuristic"]).optional()
|
|
1220
|
+
}).strict();
|
|
1221
|
+
var jsonFieldMetricsSchema = z2.object({
|
|
1222
|
+
fields: z2.array(z2.string()),
|
|
1223
|
+
valid_json_count: z2.number().int().nonnegative(),
|
|
1224
|
+
valid_json_rate: z2.number().min(0).max(1),
|
|
1225
|
+
schema_match_count: z2.number().int().nonnegative(),
|
|
1226
|
+
schema_match_rate: z2.number().min(0).max(1),
|
|
1227
|
+
all_fields_match_count: z2.number().int().nonnegative(),
|
|
1228
|
+
all_fields_match_rate: z2.number().min(0).max(1),
|
|
1229
|
+
field_accuracy: z2.record(z2.string(), z2.object({
|
|
1230
|
+
correct: z2.number().int().nonnegative(),
|
|
1231
|
+
total: z2.number().int().nonnegative(),
|
|
1232
|
+
accuracy: z2.number().min(0).max(1)
|
|
1233
|
+
}).strict())
|
|
1234
|
+
}).strict();
|
|
1235
|
+
var evalSplitSchema = z2.enum([
|
|
1236
|
+
"spec_examples",
|
|
1237
|
+
"spec_holdout",
|
|
1238
|
+
"prebuilt_test",
|
|
1239
|
+
"prebuilt_validation",
|
|
1240
|
+
"general_regression"
|
|
1241
|
+
]);
|
|
1242
|
+
var evalReportSchema = z2.object({
|
|
1243
|
+
kind: z2.enum(["baseline", "candidate"]),
|
|
1244
|
+
model_id: z2.string(),
|
|
1245
|
+
total: z2.number().int().nonnegative(),
|
|
1246
|
+
eval_examples_total: z2.number().int().nonnegative(),
|
|
1247
|
+
eval_examples_used: z2.number().int().nonnegative(),
|
|
1248
|
+
eval_truncated: z2.boolean(),
|
|
1249
|
+
eval_split: evalSplitSchema.optional(),
|
|
1250
|
+
eval_sample_seed: z2.number().int().nullable().optional(),
|
|
1251
|
+
avg_score: z2.number().min(0).max(1),
|
|
1252
|
+
pass_rate: z2.number().min(0).max(1),
|
|
1253
|
+
exact_match_rate: z2.number().min(0).max(1),
|
|
1254
|
+
avg_token_f1: z2.number().min(0).max(1).optional(),
|
|
1255
|
+
avg_latency_ms: z2.number().int().nonnegative(),
|
|
1256
|
+
cached: z2.boolean().optional(),
|
|
1257
|
+
cache_key: z2.string().optional(),
|
|
1258
|
+
results: z2.array(evalExampleResultSchema),
|
|
1259
|
+
artifact_uri: z2.string(),
|
|
1260
|
+
scoring_method: z2.enum(["heuristic", "exact_match", "json_fields"]),
|
|
1261
|
+
inference_provider: z2.enum(["none", "transformers"]).optional(),
|
|
1262
|
+
scoring_mode: z2.enum(["exact_match", "json_fields"]).optional(),
|
|
1263
|
+
json_field_metrics: jsonFieldMetricsSchema.optional(),
|
|
1264
|
+
generation_config: z2.record(z2.string(), z2.unknown()).optional(),
|
|
1265
|
+
log_uri: z2.string().optional()
|
|
1266
|
+
}).strict();
|
|
1267
|
+
var comparisonReportSchema = z2.object({
|
|
1268
|
+
avg_score_delta: z2.number(),
|
|
1269
|
+
pass_rate_delta: z2.number(),
|
|
1270
|
+
exact_match_rate_delta: z2.number(),
|
|
1271
|
+
token_f1_delta: z2.number().optional(),
|
|
1272
|
+
regressions: z2.number().int().nonnegative(),
|
|
1273
|
+
improvements: z2.number().int().nonnegative(),
|
|
1274
|
+
regressed_examples: z2.array(z2.object({
|
|
1275
|
+
prompt: z2.string(),
|
|
1276
|
+
old_score: z2.number(),
|
|
1277
|
+
new_score: z2.number()
|
|
1278
|
+
}).strict())
|
|
1279
|
+
}).strict();
|
|
1280
|
+
var trainingReportSchema = z2.object({
|
|
1281
|
+
provider: z2.enum(["local-uv", "aws-ssh"]),
|
|
1282
|
+
training_job_name: z2.string(),
|
|
1283
|
+
model_artifact_uri: z2.string().optional(),
|
|
1284
|
+
base_model_artifact_uri: z2.string().optional(),
|
|
1285
|
+
metrics: z2.record(z2.string(), z2.unknown()).nullable(),
|
|
1286
|
+
exit_code: z2.number().int().nullable(),
|
|
1287
|
+
log_uri: z2.string(),
|
|
1288
|
+
command: z2.array(z2.string()).optional()
|
|
1289
|
+
}).strict();
|
|
1290
|
+
var runProvenanceSchema = z2.object({
|
|
1291
|
+
schema_version: z2.literal(1),
|
|
1292
|
+
recorded_at: z2.string(),
|
|
1293
|
+
software: z2.object({
|
|
1294
|
+
tt_version: z2.string(),
|
|
1295
|
+
node_version: z2.string(),
|
|
1296
|
+
platform: z2.string(),
|
|
1297
|
+
arch: z2.string(),
|
|
1298
|
+
runtime_fingerprint: z2.string(),
|
|
1299
|
+
python_lock_sha256: z2.string().nullable()
|
|
1300
|
+
}).strict(),
|
|
1301
|
+
execution: z2.object({
|
|
1302
|
+
target: z2.enum(["local", "aws"]),
|
|
1303
|
+
training_backend: z2.string(),
|
|
1304
|
+
instance_id: z2.string().optional(),
|
|
1305
|
+
region: z2.string().optional()
|
|
1306
|
+
}).strict(),
|
|
1307
|
+
spec: z2.object({
|
|
1308
|
+
behavior_spec_id: z2.string(),
|
|
1309
|
+
run_number: z2.number().int(),
|
|
1310
|
+
snapshot_sha256: z2.string(),
|
|
1311
|
+
system_prompt_sha256: z2.string(),
|
|
1312
|
+
file_path: z2.string().optional(),
|
|
1313
|
+
file_sha256: z2.string().optional()
|
|
1314
|
+
}).strict(),
|
|
1315
|
+
base_model: z2.object({
|
|
1316
|
+
id: z2.string(),
|
|
1317
|
+
revision: z2.string().nullable(),
|
|
1318
|
+
fingerprint: z2.string().nullable()
|
|
1319
|
+
}).strict(),
|
|
1320
|
+
data: z2.object({
|
|
1321
|
+
prebuilt: z2.boolean(),
|
|
1322
|
+
format: z2.string().nullable(),
|
|
1323
|
+
file_sha256: z2.record(z2.string(), z2.string()),
|
|
1324
|
+
compiled_training_sha256: z2.string().nullable(),
|
|
1325
|
+
training_examples: z2.number().int().nonnegative().nullable(),
|
|
1326
|
+
eval_split: z2.string(),
|
|
1327
|
+
eval_sample_seed: z2.number().int(),
|
|
1328
|
+
eval_examples_total: z2.number().int().nonnegative(),
|
|
1329
|
+
eval_examples_used: z2.number().int().nonnegative()
|
|
1330
|
+
}).strict(),
|
|
1331
|
+
training: z2.object({
|
|
1332
|
+
hyperparameters: z2.record(z2.string(), z2.unknown()),
|
|
1333
|
+
exit_code: z2.number().int().nullable(),
|
|
1334
|
+
log_uri: z2.string()
|
|
1335
|
+
}).strict(),
|
|
1336
|
+
/** Hash over request, runtime, preparation config and input digests. */
|
|
1337
|
+
source_fingerprint: z2.string()
|
|
1338
|
+
}).strict();
|
|
1339
|
+
var runReportSchema = z2.object({
|
|
1340
|
+
run_id: z2.string().uuid(),
|
|
1341
|
+
behavior_spec_id: z2.string().uuid(),
|
|
1342
|
+
user_id: z2.string(),
|
|
1343
|
+
run_number: z2.number().int().min(1),
|
|
1344
|
+
base_model: z2.string(),
|
|
1345
|
+
fine_tuned_model_id: z2.string(),
|
|
1346
|
+
status: z2.enum(["completed", "failed"]),
|
|
1347
|
+
baseline: evalReportSchema,
|
|
1348
|
+
candidate: evalReportSchema,
|
|
1349
|
+
comparison: comparisonReportSchema,
|
|
1350
|
+
task_quality: taskQualityGateSchema.optional(),
|
|
1351
|
+
general_regression: z2.object({
|
|
1352
|
+
dataset_uri: z2.string(),
|
|
1353
|
+
dataset_sha256: z2.string().regex(/^[a-f0-9]{64}$/),
|
|
1354
|
+
baseline: evalReportSchema,
|
|
1355
|
+
candidate: evalReportSchema,
|
|
1356
|
+
comparison: comparisonReportSchema,
|
|
1357
|
+
policy: z2.object({
|
|
1358
|
+
max_score_drop: z2.number().min(0).max(1),
|
|
1359
|
+
max_pass_rate_drop: z2.number().min(0).max(1)
|
|
1360
|
+
}).strict(),
|
|
1361
|
+
passed: z2.boolean(),
|
|
1362
|
+
failures: z2.array(z2.string())
|
|
1363
|
+
}).strict().optional(),
|
|
1364
|
+
training: trainingReportSchema,
|
|
1365
|
+
provenance: runProvenanceSchema.optional(),
|
|
1366
|
+
artifact_uris: z2.object({
|
|
1367
|
+
dataset: z2.string(),
|
|
1368
|
+
baseline_eval: z2.string(),
|
|
1369
|
+
candidate_eval: z2.string(),
|
|
1370
|
+
general_baseline_eval: z2.string().optional(),
|
|
1371
|
+
general_candidate_eval: z2.string().optional(),
|
|
1372
|
+
report: z2.string()
|
|
1373
|
+
}).strict(),
|
|
1374
|
+
run_metadata: z2.object({
|
|
1375
|
+
base_model: z2.string(),
|
|
1376
|
+
fine_tuned_model_id: z2.string(),
|
|
1377
|
+
dataset_prebuilt: z2.boolean(),
|
|
1378
|
+
dataset_format: datasetFormatSchema.nullable().optional(),
|
|
1379
|
+
dataset_uri: z2.string(),
|
|
1380
|
+
dataset_source: datasetSourceSchema.optional(),
|
|
1381
|
+
spec_example_count: z2.number().int().nonnegative(),
|
|
1382
|
+
training_example_count: z2.number().int().nonnegative().nullable(),
|
|
1383
|
+
eval_examples_total: z2.number().int().nonnegative(),
|
|
1384
|
+
eval_examples_used: z2.number().int().nonnegative(),
|
|
1385
|
+
eval_split: evalSplitSchema.optional(),
|
|
1386
|
+
eval_sample_seed: z2.number().int().nullable().optional(),
|
|
1387
|
+
started_at: z2.string(),
|
|
1388
|
+
completed_at: z2.string(),
|
|
1389
|
+
elapsed_ms: z2.number().int().nonnegative(),
|
|
1390
|
+
elapsed_seconds: z2.number().nonnegative()
|
|
1391
|
+
}).strict(),
|
|
1392
|
+
created_at: z2.string()
|
|
1393
|
+
}).strict();
|
|
1394
|
+
var inferenceConfigSchema = z2.object({
|
|
1395
|
+
batchSize: z2.number().int().min(1).max(128).default(4),
|
|
1396
|
+
maxNewTokens: z2.number().int().min(1).default(256),
|
|
1397
|
+
temperature: z2.number().min(0).default(0),
|
|
1398
|
+
topP: z2.number().min(0).max(1).default(1),
|
|
1399
|
+
device: z2.enum(["cuda", "cpu"]).default("cuda")
|
|
1400
|
+
}).strict();
|
|
1401
|
+
var scoringConfigSchema = z2.object({
|
|
1402
|
+
mode: z2.enum(["exact_match", "json_fields"]).default("exact_match"),
|
|
1403
|
+
fields: z2.array(z2.string().min(1)).optional()
|
|
1404
|
+
}).strict();
|
|
1405
|
+
var evaluationConfigSchema = z2.object({
|
|
1406
|
+
inference: inferenceConfigSchema.default({
|
|
1407
|
+
batchSize: 4,
|
|
1408
|
+
maxNewTokens: 256,
|
|
1409
|
+
temperature: 0,
|
|
1410
|
+
topP: 1,
|
|
1411
|
+
device: "cuda"
|
|
1412
|
+
}),
|
|
1413
|
+
scoring: scoringConfigSchema.default({ mode: "exact_match" }),
|
|
1414
|
+
timeoutMs: z2.number().int().min(100).default(18e5),
|
|
1415
|
+
maxExamples: z2.number().int().min(1).optional(),
|
|
1416
|
+
sampleSeed: z2.number().int().optional(),
|
|
1417
|
+
baselineCache: z2.boolean().default(true),
|
|
1418
|
+
taskQuality: taskQualityPolicySchema.optional(),
|
|
1419
|
+
generalRegression: z2.object({
|
|
1420
|
+
dataset: z2.string().min(1),
|
|
1421
|
+
systemPrompt: z2.string().optional(),
|
|
1422
|
+
maxScoreDrop: z2.number().min(0).max(1).default(0.03),
|
|
1423
|
+
maxPassRateDrop: z2.number().min(0).max(1).default(0.05)
|
|
1424
|
+
}).strict().optional()
|
|
1425
|
+
}).strict();
|
|
1426
|
+
var localRunnerConfigSchema = z2.object({
|
|
1427
|
+
gpu: z2.object({
|
|
1428
|
+
provider: z2.literal("aws"),
|
|
1429
|
+
instanceId: z2.string().regex(/^i-[a-f0-9]{8,17}$/),
|
|
1430
|
+
profile: z2.string().min(1).optional(),
|
|
1431
|
+
region: z2.string().min(1).optional(),
|
|
1432
|
+
user: z2.string().regex(/^[a-z_][a-z0-9_-]*$/i),
|
|
1433
|
+
identityFile: z2.string().min(1).optional(),
|
|
1434
|
+
privateIp: z2.boolean().default(false),
|
|
1435
|
+
maxSeconds: z2.number().int().min(60).max(604800).default(86400)
|
|
1436
|
+
}).strict().optional(),
|
|
1437
|
+
storeRoot: z2.string().optional(),
|
|
1438
|
+
artifactRoot: z2.string().default(DEFAULT_ARTIFACT_ROOT),
|
|
1439
|
+
dryRun: z2.boolean().default(false),
|
|
1440
|
+
paths: z2.object({
|
|
1441
|
+
baseModel: z2.string().optional(),
|
|
1442
|
+
modelCache: z2.string().optional()
|
|
1443
|
+
}).strict().default({}),
|
|
1444
|
+
evaluation: evaluationConfigSchema.default({
|
|
1445
|
+
inference: {
|
|
1446
|
+
batchSize: 4,
|
|
1447
|
+
maxNewTokens: 256,
|
|
1448
|
+
temperature: 0,
|
|
1449
|
+
topP: 1,
|
|
1450
|
+
device: "cuda"
|
|
1451
|
+
},
|
|
1452
|
+
scoring: { mode: "exact_match" },
|
|
1453
|
+
timeoutMs: 18e5,
|
|
1454
|
+
baselineCache: true
|
|
1455
|
+
})
|
|
1456
|
+
}).strict();
|
|
1457
|
+
var projectRuntimeSchema = z2.object({
|
|
1458
|
+
gpu: localRunnerConfigSchema.shape.gpu,
|
|
1459
|
+
artifactRoot: z2.string().min(1).optional(),
|
|
1460
|
+
storeRoot: z2.string().min(1).optional(),
|
|
1461
|
+
paths: z2.object({ baseModel: z2.string().min(1).optional(), modelCache: z2.string().min(1).optional() }).strict().optional()
|
|
1462
|
+
}).strict();
|
|
1463
|
+
var projectWorkflowFields = {
|
|
1464
|
+
evaluation: evaluationConfigSchema.optional(),
|
|
1465
|
+
runtime: projectRuntimeSchema.optional(),
|
|
1466
|
+
pipeline: pipelineDocumentSchema.optional()
|
|
1467
|
+
};
|
|
1468
|
+
var localFoundationSpecFileSchema = behaviorSpecSchema.extend({
|
|
1469
|
+
...projectWorkflowFields,
|
|
1470
|
+
engine: z2.literal("foundation"),
|
|
1471
|
+
id: z2.string().uuid().optional(),
|
|
1472
|
+
examples: z2.array(behaviorSpecExampleSchema).min(2),
|
|
1473
|
+
foundation: foundationHyperparametersSchema
|
|
1474
|
+
});
|
|
1475
|
+
var localAdapterSpecFileSchema = specSnapshotSchema.extend({
|
|
1476
|
+
...projectWorkflowFields,
|
|
1477
|
+
engine: z2.literal("adapter").optional(),
|
|
1478
|
+
id: z2.string().uuid().optional(),
|
|
1479
|
+
hyperparameters: fineTuneHyperparametersSchema.optional(),
|
|
1480
|
+
dataset_prebuilt: datasetPrebuiltSchema.optional()
|
|
1481
|
+
}).strict().superRefine(requirePinnedUncertifiedModel);
|
|
1482
|
+
var decisionQuestionSchema = z2.discriminatedUnion("type", [
|
|
1483
|
+
z2.object({
|
|
1484
|
+
type: z2.literal("choice"),
|
|
1485
|
+
/** Answer label -> description the model reads. Key order is option order. */
|
|
1486
|
+
criteria: z2.record(z2.string().min(1), z2.string()).refine(
|
|
1487
|
+
(criteria) => Object.keys(criteria).length >= 2 && Object.keys(criteria).length <= 32,
|
|
1488
|
+
"A choice decision needs 2 to 32 labels."
|
|
1489
|
+
)
|
|
1490
|
+
}).strict(),
|
|
1491
|
+
z2.object({
|
|
1492
|
+
type: z2.literal("noul"),
|
|
1493
|
+
criteria: z2.object({ true: z2.string().optional(), false: z2.string().optional() }).strict().optional()
|
|
1494
|
+
}).strict(),
|
|
1495
|
+
z2.object({
|
|
1496
|
+
type: z2.literal("score"),
|
|
1497
|
+
/** Level descriptions, index 0 first; example outputs are level indices. */
|
|
1498
|
+
criteria: z2.array(z2.string().min(1)).min(2).max(10)
|
|
1499
|
+
}).strict()
|
|
1500
|
+
]);
|
|
1501
|
+
var decisionHyperparametersSchema = z2.object({
|
|
1502
|
+
n_epochs: z2.number().int().min(1).max(50).optional(),
|
|
1503
|
+
learning_rate: z2.number().positive().max(1).optional(),
|
|
1504
|
+
batch_size: z2.number().int().min(1).max(256).optional(),
|
|
1505
|
+
/** Train only the decision head; faster on CPU, usually less accurate. */
|
|
1506
|
+
freeze_encoder: z2.boolean().optional(),
|
|
1507
|
+
/** Shuffle option order per training row so the head learns meaning, not position. */
|
|
1508
|
+
shuffle_options: z2.boolean().optional(),
|
|
1509
|
+
seed: z2.number().int().min(0).max(2147483647).optional(),
|
|
1510
|
+
device: z2.enum(["auto", "cpu", "cuda", "mps"]).optional()
|
|
1511
|
+
}).strict();
|
|
1512
|
+
var localDecisionSpecFileSchema = behaviorSpecSchema.extend({
|
|
1513
|
+
...projectWorkflowFields,
|
|
1514
|
+
engine: z2.literal("decision"),
|
|
1515
|
+
id: z2.string().uuid().optional(),
|
|
1516
|
+
// Refine first: a union member must report an issue, not throw, for other engines' specs.
|
|
1517
|
+
base_model: z2.string().refine(isDecisionModel, `Unsupported decision model. Supported decision models: ${DECISION_MODELS.map((model) => model.id).join(", ")}`).transform((value) => canonicalizeDecisionModel(value)),
|
|
1518
|
+
decision: decisionQuestionSchema,
|
|
1519
|
+
hyperparameters: decisionHyperparametersSchema.optional()
|
|
1520
|
+
});
|
|
1521
|
+
function decisionLabels(question) {
|
|
1522
|
+
if (question.type === "choice") return Object.keys(question.criteria);
|
|
1523
|
+
if (question.type === "noul") return ["false", "true"];
|
|
1524
|
+
return question.criteria.map((_, index) => String(index));
|
|
1525
|
+
}
|
|
1526
|
+
var localBehaviorSpecFileSchema = z2.union([
|
|
1527
|
+
localFoundationSpecFileSchema,
|
|
1528
|
+
localDecisionSpecFileSchema,
|
|
1529
|
+
localAdapterSpecFileSchema
|
|
1530
|
+
]).superRefine((spec, ctx) => {
|
|
1531
|
+
if (spec.engine === "decision" && spec.evaluation?.taskQuality) {
|
|
1532
|
+
ctx.addIssue({ code: "custom", path: ["evaluation", "taskQuality"], message: "Task quality acceptance currently supports adapter models only." });
|
|
1533
|
+
}
|
|
1534
|
+
if (spec.pipeline && isFoundationPipeline(parsePipeline(spec.pipeline)) !== (spec.engine === "foundation")) {
|
|
1535
|
+
ctx.addIssue({ code: "custom", path: ["pipeline"], message: "Pipeline engine must match the spec engine." });
|
|
1536
|
+
}
|
|
1537
|
+
if (spec.engine === "foundation" && (spec.evaluation || spec.runtime?.storeRoot || spec.runtime?.paths)) {
|
|
1538
|
+
ctx.addIssue({ code: "custom", path: ["runtime"], message: "Foundation uses foundation settings for evaluation and checkpoints; runtime.artifactRoot and runtime.gpu are supported." });
|
|
1539
|
+
}
|
|
1540
|
+
if (spec.engine === "decision" && (spec.evaluation || spec.runtime?.gpu || spec.runtime?.storeRoot || spec.runtime?.paths)) {
|
|
1541
|
+
ctx.addIssue({ code: "custom", path: ["runtime"], message: "Decision specs support runtime.artifactRoot only; the device is set in hyperparameters.device." });
|
|
1542
|
+
}
|
|
1543
|
+
});
|
|
1544
|
+
function isFoundationSpecFile(spec) {
|
|
1545
|
+
return spec.engine === "foundation";
|
|
1546
|
+
}
|
|
1547
|
+
function isDecisionSpecFile(spec) {
|
|
1548
|
+
return spec.engine === "decision";
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
export {
|
|
1552
|
+
ARTIFACT_WORKFLOW_LOCK_FILE,
|
|
1553
|
+
fileUri,
|
|
1554
|
+
defaultArtifactPrefix,
|
|
1555
|
+
resolveRunArtifacts,
|
|
1556
|
+
claimRunArtifactDirectory,
|
|
1557
|
+
prepareRunDirectories,
|
|
1558
|
+
writeJson,
|
|
1559
|
+
writeJsonAtomic,
|
|
1560
|
+
writeFileAtomic,
|
|
1561
|
+
readJson,
|
|
1562
|
+
copyDatasetToTrainingChannel,
|
|
1563
|
+
writeArtifactManifest,
|
|
1564
|
+
assertArtifactManifest,
|
|
1565
|
+
TRAINING_MODELS,
|
|
1566
|
+
isCertifiedTrainingModel,
|
|
1567
|
+
resolveTrainingModel,
|
|
1568
|
+
resolveRequestedBaseModelRevision,
|
|
1569
|
+
defaultBaseModelRevision,
|
|
1570
|
+
resolveModelLoader,
|
|
1571
|
+
assertCertifiedBaseModelConfig,
|
|
1572
|
+
localModelArtifactPath,
|
|
1573
|
+
assertUsableModelArtifact,
|
|
1574
|
+
DEFAULT_ARTIFACT_ROOT,
|
|
1575
|
+
DEFAULT_PROJECT_STORE_ROOT,
|
|
1576
|
+
DEFAULT_FOUNDATION_RUNS_DIR,
|
|
1577
|
+
DEFAULT_DECISION_RUNS_DIR,
|
|
1578
|
+
expandUserPath,
|
|
1579
|
+
getTunedTensorHome,
|
|
1580
|
+
getConfigDir,
|
|
1581
|
+
getAgentConfigDir,
|
|
1582
|
+
defaultStoreRoot,
|
|
1583
|
+
pythonEnvironmentPath,
|
|
1584
|
+
evaluateTaskQuality,
|
|
1585
|
+
taskQualityForReport,
|
|
1586
|
+
DECISION_MODELS,
|
|
1587
|
+
DEFAULT_DECISION_MODEL,
|
|
1588
|
+
resolveDecisionModel,
|
|
1589
|
+
datasetFormatSchema,
|
|
1590
|
+
baseModelRevisionSchema,
|
|
1591
|
+
behaviorSpecExampleSchema,
|
|
1592
|
+
behaviorSpecSchema,
|
|
1593
|
+
specSnapshotSchema,
|
|
1594
|
+
foundationHyperparametersSchema,
|
|
1595
|
+
huggingFaceRepoIdSchema,
|
|
1596
|
+
datasetColumnsSchema,
|
|
1597
|
+
huggingFaceDatasetSourceSchema,
|
|
1598
|
+
isRepoRelativePath,
|
|
1599
|
+
datasetPrebuiltSchema,
|
|
1600
|
+
datasetSourceSchema,
|
|
1601
|
+
fineTuneHyperparametersSchema,
|
|
1602
|
+
fineTuneRunRequestSchema,
|
|
1603
|
+
evalExampleResultSchema,
|
|
1604
|
+
jsonFieldMetricsSchema,
|
|
1605
|
+
evalSplitSchema,
|
|
1606
|
+
evalReportSchema,
|
|
1607
|
+
comparisonReportSchema,
|
|
1608
|
+
trainingReportSchema,
|
|
1609
|
+
runProvenanceSchema,
|
|
1610
|
+
runReportSchema,
|
|
1611
|
+
localRunnerConfigSchema,
|
|
1612
|
+
projectRuntimeSchema,
|
|
1613
|
+
localFoundationSpecFileSchema,
|
|
1614
|
+
localAdapterSpecFileSchema,
|
|
1615
|
+
decisionQuestionSchema,
|
|
1616
|
+
decisionHyperparametersSchema,
|
|
1617
|
+
localDecisionSpecFileSchema,
|
|
1618
|
+
decisionLabels,
|
|
1619
|
+
localBehaviorSpecFileSchema,
|
|
1620
|
+
isFoundationSpecFile,
|
|
1621
|
+
isDecisionSpecFile
|
|
1622
|
+
};
|
|
1623
|
+
//# sourceMappingURL=chunk-B2O6P7EX.js.map
|