@botlearn-course/daemon 0.0.21 → 0.0.23-beta.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.
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Materialize the Skills authorized for one turn into the runtime's skills directory.
|
|
3
|
+
*
|
|
4
|
+
* The DeepSeek/Codewhale CLI discovers Skills from `<skills-dir>/<name>/SKILL.md` and
|
|
5
|
+
* exposes them through its native `load_skill` tool. That path does not involve MCP, which
|
|
6
|
+
* matters because `deepseek serve --http` — the mode the daemon runs — never loads an MCP
|
|
7
|
+
* config at all, so the previous relay could not reach the model.
|
|
8
|
+
*
|
|
9
|
+
* The directory is rewritten per turn rather than created fresh each time: its path is
|
|
10
|
+
* baked into the runtime process environment at spawn, and that process is pooled and
|
|
11
|
+
* reused. A per-turn path would leave a reused process pointing at a stale directory,
|
|
12
|
+
* and the authorized set genuinely differs between a task turn (global set plus the
|
|
13
|
+
* task's set) and ordinary conversation (global set only).
|
|
14
|
+
*/
|
|
15
|
+
import type { PreparedRuntimeSkillProvider, RuntimeSkillCatalogEntry } from "./runtime-skills.js";
|
|
16
|
+
/** Directory name created under the runtime profile root. */
|
|
17
|
+
export declare const RUNTIME_SKILLS_DIR_NAME = "skills";
|
|
18
|
+
export declare class RuntimeSkillMaterializationError extends Error {
|
|
19
|
+
readonly code: string;
|
|
20
|
+
constructor(code: string, message: string);
|
|
21
|
+
}
|
|
22
|
+
export interface MaterializedSkill {
|
|
23
|
+
ref: string;
|
|
24
|
+
/** Directory name under the skills root; also the name the model passes to load_skill. */
|
|
25
|
+
name: string;
|
|
26
|
+
files: number;
|
|
27
|
+
bytes: number;
|
|
28
|
+
}
|
|
29
|
+
export interface MaterializeCourseSkillsResult {
|
|
30
|
+
skillsDir: string;
|
|
31
|
+
skills: MaterializedSkill[];
|
|
32
|
+
/** Stable fingerprint of the materialized set, for logging and reuse assertions. */
|
|
33
|
+
fingerprint: string;
|
|
34
|
+
}
|
|
35
|
+
export interface MaterializeCourseSkillsOptions {
|
|
36
|
+
prepared: PreparedRuntimeSkillProvider;
|
|
37
|
+
/** Runtime profile root; the skills directory is created directly below it. */
|
|
38
|
+
profileRoot: string;
|
|
39
|
+
/** Managed sessions hand the directory to a separate runtime UID via the group bit. */
|
|
40
|
+
managed: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The directory name the model will use. Derived from the grant's logical id, never from
|
|
44
|
+
* manifest text, so a package cannot rename itself into another Skill's directory.
|
|
45
|
+
*/
|
|
46
|
+
export declare function skillDirectoryName(entry: RuntimeSkillCatalogEntry): string;
|
|
47
|
+
export declare function skillSetFingerprint(refs: readonly string[]): string;
|
|
48
|
+
/**
|
|
49
|
+
* Rewrite `<profileRoot>/skills` so it contains exactly the authorized Skills.
|
|
50
|
+
*
|
|
51
|
+
* Throws on any failure. A partial or stale directory must never be handed to the model:
|
|
52
|
+
* silently continuing without a Skill is the failure mode this whole change exists to
|
|
53
|
+
* remove.
|
|
54
|
+
*/
|
|
55
|
+
export declare function materializeCourseSkills(options: MaterializeCourseSkillsOptions): Promise<MaterializeCourseSkillsResult>;
|
|
56
|
+
export declare function cleanupCourseSkills(profileRoot: string | undefined): void;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Materialize the Skills authorized for one turn into the runtime's skills directory.
|
|
3
|
+
*
|
|
4
|
+
* The DeepSeek/Codewhale CLI discovers Skills from `<skills-dir>/<name>/SKILL.md` and
|
|
5
|
+
* exposes them through its native `load_skill` tool. That path does not involve MCP, which
|
|
6
|
+
* matters because `deepseek serve --http` — the mode the daemon runs — never loads an MCP
|
|
7
|
+
* config at all, so the previous relay could not reach the model.
|
|
8
|
+
*
|
|
9
|
+
* The directory is rewritten per turn rather than created fresh each time: its path is
|
|
10
|
+
* baked into the runtime process environment at spawn, and that process is pooled and
|
|
11
|
+
* reused. A per-turn path would leave a reused process pointing at a stale directory,
|
|
12
|
+
* and the authorized set genuinely differs between a task turn (global set plus the
|
|
13
|
+
* task's set) and ordinary conversation (global set only).
|
|
14
|
+
*/
|
|
15
|
+
import { chmodSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
16
|
+
import path from "node:path";
|
|
17
|
+
/** Directory name created under the runtime profile root. */
|
|
18
|
+
export const RUNTIME_SKILLS_DIR_NAME = "skills";
|
|
19
|
+
const DIR_MODE_MANAGED = 0o750;
|
|
20
|
+
const FILE_MODE_MANAGED = 0o640;
|
|
21
|
+
const DIR_MODE_PRIVATE = 0o700;
|
|
22
|
+
const FILE_MODE_PRIVATE = 0o600;
|
|
23
|
+
export class RuntimeSkillMaterializationError extends Error {
|
|
24
|
+
code;
|
|
25
|
+
constructor(code, message) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.code = code;
|
|
28
|
+
this.name = "RuntimeSkillMaterializationError";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The directory name the model will use. Derived from the grant's logical id, never from
|
|
33
|
+
* manifest text, so a package cannot rename itself into another Skill's directory.
|
|
34
|
+
*/
|
|
35
|
+
export function skillDirectoryName(entry) {
|
|
36
|
+
const separator = entry.ref.lastIndexOf("@");
|
|
37
|
+
const logicalId = separator > 0 ? entry.ref.slice(0, separator) : entry.ref;
|
|
38
|
+
const name = logicalId.split(".").pop() ?? logicalId;
|
|
39
|
+
if (!/^[a-z0-9]+(?:[._-][a-z0-9]+)*$/i.test(name)) {
|
|
40
|
+
throw new RuntimeSkillMaterializationError("skill_directory_name_invalid", `Skill logical id does not yield a usable directory name: ${entry.ref}`);
|
|
41
|
+
}
|
|
42
|
+
return name;
|
|
43
|
+
}
|
|
44
|
+
export function skillSetFingerprint(refs) {
|
|
45
|
+
return [...refs].sort().join(",");
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Rewrite `<profileRoot>/skills` so it contains exactly the authorized Skills.
|
|
49
|
+
*
|
|
50
|
+
* Throws on any failure. A partial or stale directory must never be handed to the model:
|
|
51
|
+
* silently continuing without a Skill is the failure mode this whole change exists to
|
|
52
|
+
* remove.
|
|
53
|
+
*/
|
|
54
|
+
export async function materializeCourseSkills(options) {
|
|
55
|
+
const { prepared, profileRoot, managed } = options;
|
|
56
|
+
const skillsDir = path.join(profileRoot, RUNTIME_SKILLS_DIR_NAME);
|
|
57
|
+
const dirMode = managed ? DIR_MODE_MANAGED : DIR_MODE_PRIVATE;
|
|
58
|
+
const fileMode = managed ? FILE_MODE_MANAGED : FILE_MODE_PRIVATE;
|
|
59
|
+
// Rebuild rather than merge: a Skill dropped from the authorized set must disappear.
|
|
60
|
+
rmSync(skillsDir, { recursive: true, force: true });
|
|
61
|
+
if (prepared.catalog.length === 0) {
|
|
62
|
+
return { skillsDir, skills: [], fingerprint: "" };
|
|
63
|
+
}
|
|
64
|
+
mkdirSync(skillsDir, { recursive: true, mode: dirMode });
|
|
65
|
+
chmodSync(skillsDir, dirMode);
|
|
66
|
+
const skills = [];
|
|
67
|
+
try {
|
|
68
|
+
for (const entry of prepared.catalog) {
|
|
69
|
+
const name = skillDirectoryName(entry);
|
|
70
|
+
const pkg = await fetchPackage(prepared, entry);
|
|
71
|
+
const target = path.join(skillsDir, name);
|
|
72
|
+
mkdirSync(target, { recursive: true, mode: dirMode });
|
|
73
|
+
chmodSync(target, dirMode);
|
|
74
|
+
writePackage(pkg, target, dirMode, fileMode);
|
|
75
|
+
skills.push({ ref: entry.ref, name, files: pkg.files.length, bytes: pkg.bytes });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
// Never leave a half-written directory behind; the next turn would inherit it.
|
|
80
|
+
rmSync(skillsDir, { recursive: true, force: true });
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
skillsDir,
|
|
85
|
+
skills,
|
|
86
|
+
fingerprint: skillSetFingerprint(skills.map((skill) => skill.ref)),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async function fetchPackage(prepared, entry) {
|
|
90
|
+
try {
|
|
91
|
+
return await prepared.provider.fetchPackage(entry.ref);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
const code = error instanceof Error && "code" in error
|
|
95
|
+
? String(error.code)
|
|
96
|
+
: "skill_package_fetch_failed";
|
|
97
|
+
throw new RuntimeSkillMaterializationError(code, `Failed to fetch Skill package ${entry.ref}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function writePackage(pkg, target, dirMode, fileMode) {
|
|
101
|
+
const entry = pkg.files.find((file) => file.path === pkg.entrypoint);
|
|
102
|
+
if (!entry) {
|
|
103
|
+
throw new RuntimeSkillMaterializationError("skill_package_entrypoint_missing", `Skill package ${pkg.ref} is missing its entrypoint ${pkg.entrypoint}`);
|
|
104
|
+
}
|
|
105
|
+
for (const file of pkg.files) {
|
|
106
|
+
writeFile(path.join(target, file.path), file.content, target, dirMode, fileMode);
|
|
107
|
+
}
|
|
108
|
+
// The CLI only discovers `SKILL.md`. When the package entrypoint has another name we
|
|
109
|
+
// write both: the canonical name for discovery, and the original so that any in-package
|
|
110
|
+
// reference to it keeps resolving.
|
|
111
|
+
if (pkg.entrypoint !== "SKILL.md") {
|
|
112
|
+
writeFile(path.join(target, "SKILL.md"), entry.content, target, dirMode, fileMode);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function writeFile(destination, content, root, dirMode, fileMode) {
|
|
116
|
+
const resolvedRoot = path.resolve(root);
|
|
117
|
+
const resolved = path.resolve(destination);
|
|
118
|
+
// Defence in depth: the provider already rejects absolute paths and `..` segments, but
|
|
119
|
+
// a corrupted or hostile package must not be able to write outside its own directory.
|
|
120
|
+
if (resolved !== resolvedRoot && !resolved.startsWith(`${resolvedRoot}${path.sep}`)) {
|
|
121
|
+
throw new RuntimeSkillMaterializationError("skill_package_path_escape", "Skill package file escapes its materialization directory");
|
|
122
|
+
}
|
|
123
|
+
mkdirSync(path.dirname(resolved), { recursive: true, mode: dirMode });
|
|
124
|
+
writeFileSync(resolved, content, { mode: fileMode });
|
|
125
|
+
chmodSync(resolved, fileMode);
|
|
126
|
+
}
|
|
127
|
+
export function cleanupCourseSkills(profileRoot) {
|
|
128
|
+
if (!profileRoot)
|
|
129
|
+
return;
|
|
130
|
+
rmSync(path.join(profileRoot, RUNTIME_SKILLS_DIR_NAME), {
|
|
131
|
+
recursive: true,
|
|
132
|
+
force: true,
|
|
133
|
+
});
|
|
134
|
+
}
|
package/dist/runtime-skills.d.ts
CHANGED
|
@@ -30,10 +30,30 @@ export interface RuntimeLoadedSkill {
|
|
|
30
30
|
export interface RuntimeSkillReference extends RuntimeLoadedSkill {
|
|
31
31
|
path: string;
|
|
32
32
|
}
|
|
33
|
+
export interface RuntimeSkillPackageFile {
|
|
34
|
+
path: string;
|
|
35
|
+
content: Uint8Array;
|
|
36
|
+
}
|
|
37
|
+
export interface RuntimeSkillPackage {
|
|
38
|
+
ref: string;
|
|
39
|
+
digest: string;
|
|
40
|
+
envelopeDigest: string;
|
|
41
|
+
entrypoint: string;
|
|
42
|
+
files: RuntimeSkillPackageFile[];
|
|
43
|
+
bytes: number;
|
|
44
|
+
}
|
|
33
45
|
export interface RuntimeSkillProvider {
|
|
34
46
|
listEligibleSkills(): Promise<RuntimeSkillCatalogEntry[]>;
|
|
35
47
|
loadSkill(ref: string): Promise<RuntimeLoadedSkill>;
|
|
36
48
|
loadReference(ref: string, referencePath: string): Promise<RuntimeSkillReference>;
|
|
49
|
+
/**
|
|
50
|
+
* Fetch every file of one authorized Skill in a single call.
|
|
51
|
+
*
|
|
52
|
+
* Materializing a Skill into the runtime's skills directory needs the whole package:
|
|
53
|
+
* `loadSkill`/`loadReference` only expose one caller-named path at a time and cannot
|
|
54
|
+
* enumerate the package, so they can never produce a complete directory.
|
|
55
|
+
*/
|
|
56
|
+
fetchPackage(ref: string): Promise<RuntimeSkillPackage>;
|
|
37
57
|
}
|
|
38
58
|
export interface PreparedRuntimeSkillProvider {
|
|
39
59
|
binding: RuntimeSkillProviderBinding;
|
|
@@ -85,6 +105,7 @@ export declare class HttpRuntimeSkillProvider implements RuntimeSkillProvider {
|
|
|
85
105
|
});
|
|
86
106
|
listEligibleSkills(): Promise<RuntimeSkillCatalogEntry[]>;
|
|
87
107
|
loadSkill(ref: string): Promise<RuntimeLoadedSkill>;
|
|
108
|
+
fetchPackage(ref: string): Promise<RuntimeSkillPackage>;
|
|
88
109
|
loadReference(ref: string, referencePath: string): Promise<RuntimeSkillReference>;
|
|
89
110
|
private requireEligible;
|
|
90
111
|
private call;
|
package/dist/runtime-skills.js
CHANGED
|
@@ -8,6 +8,14 @@ const MAX_SKILLS = 32;
|
|
|
8
8
|
const MAX_CATALOG_DESCRIPTION_CODE_POINTS = 500;
|
|
9
9
|
const MAX_PROVIDER_RESPONSE_BYTES = 512 * 1024;
|
|
10
10
|
const MAX_LOADED_CONTENT_BYTES = 256 * 1024;
|
|
11
|
+
// 物化一次取整包,响应必然大于单文件调用,因此单独给出预算。
|
|
12
|
+
// 文件数、单文件和整包上限必须与 Course Registry、agent-svc 的包合同一致;
|
|
13
|
+
// 整包 base64 后约 2.7 MiB,响应上限留到 4 MiB 覆盖 JSON 结构开销。
|
|
14
|
+
const MAX_PACKAGE_BYTES = 2 * 1024 * 1024;
|
|
15
|
+
const MAX_PACKAGE_FILE_BYTES = 1024 * 1024;
|
|
16
|
+
const MAX_PACKAGE_BASE64 = Math.ceil(MAX_PACKAGE_FILE_BYTES / 3) * 4;
|
|
17
|
+
const MAX_PACKAGE_FILES = 128;
|
|
18
|
+
const MAX_PACKAGE_RESPONSE_BYTES = 4 * 1024 * 1024;
|
|
11
19
|
const MAX_BINDING_LIFETIME_MS = 24 * 60 * 60 * 1000;
|
|
12
20
|
export const COURSE_SKILLS_BINDING_SCHEMA = "course-skills-binding/0.1";
|
|
13
21
|
export const COURSE_SKILLS_PROTOCOL = "course-skills/0.1";
|
|
@@ -107,6 +115,12 @@ export class HttpRuntimeSkillProvider {
|
|
|
107
115
|
const data = resultData(envelope, "skill_loaded");
|
|
108
116
|
return parseLoadedSkill(data, grant);
|
|
109
117
|
}
|
|
118
|
+
async fetchPackage(ref) {
|
|
119
|
+
const grant = this.requireEligible(ref);
|
|
120
|
+
const envelope = await this.call("fetch_package", { ref }, MAX_PACKAGE_RESPONSE_BYTES);
|
|
121
|
+
const data = resultData(envelope, "skill_package_fetched");
|
|
122
|
+
return parseSkillPackage(data, grant);
|
|
123
|
+
}
|
|
110
124
|
async loadReference(ref, referencePath) {
|
|
111
125
|
const grant = this.requireEligible(ref);
|
|
112
126
|
const safePath = safeReferencePath(referencePath);
|
|
@@ -125,7 +139,7 @@ export class HttpRuntimeSkillProvider {
|
|
|
125
139
|
}
|
|
126
140
|
return grant;
|
|
127
141
|
}
|
|
128
|
-
async call(operation, input) {
|
|
142
|
+
async call(operation, input, maxResponseBytes = MAX_PROVIDER_RESPONSE_BYTES) {
|
|
129
143
|
if (Date.parse(this.binding.expiresAt) <= this.now()) {
|
|
130
144
|
throw new RuntimeSkillProviderError("agent_run_activation_expired", "Skill Provider binding has expired");
|
|
131
145
|
}
|
|
@@ -149,7 +163,7 @@ export class HttpRuntimeSkillProvider {
|
|
|
149
163
|
catch {
|
|
150
164
|
throw new RuntimeSkillProviderError("skill_provider_unavailable", "Skill Provider request failed");
|
|
151
165
|
}
|
|
152
|
-
const envelope = await readBoundedJson(response);
|
|
166
|
+
const envelope = await readBoundedJson(response, maxResponseBytes);
|
|
153
167
|
if (!response.ok) {
|
|
154
168
|
const code = safeErrorCode(envelope.code) ?? "skill_provider_unavailable";
|
|
155
169
|
throw new RuntimeSkillProviderError(code, "Skill Provider rejected the request");
|
|
@@ -320,9 +334,9 @@ function parseEffectiveSkillGrants(raw) {
|
|
|
320
334
|
}
|
|
321
335
|
return grants;
|
|
322
336
|
}
|
|
323
|
-
async function readBoundedJson(response) {
|
|
337
|
+
async function readBoundedJson(response, maxBytes = MAX_PROVIDER_RESPONSE_BYTES) {
|
|
324
338
|
const declared = Number(response.headers.get("content-length") ?? 0);
|
|
325
|
-
if (Number.isFinite(declared) && declared >
|
|
339
|
+
if (Number.isFinite(declared) && declared > maxBytes) {
|
|
326
340
|
throw providerResponseError("Skill Provider response is too large");
|
|
327
341
|
}
|
|
328
342
|
if (!response.body)
|
|
@@ -337,7 +351,7 @@ async function readBoundedJson(response) {
|
|
|
337
351
|
if (!value)
|
|
338
352
|
continue;
|
|
339
353
|
size += value.byteLength;
|
|
340
|
-
if (size >
|
|
354
|
+
if (size > maxBytes) {
|
|
341
355
|
await reader.cancel().catch(() => undefined);
|
|
342
356
|
throw providerResponseError("Skill Provider response is too large");
|
|
343
357
|
}
|
|
@@ -355,6 +369,53 @@ async function readBoundedJson(response) {
|
|
|
355
369
|
throw providerResponseError("Skill Provider response is not valid JSON");
|
|
356
370
|
}
|
|
357
371
|
}
|
|
372
|
+
function parseSkillPackage(data, grant) {
|
|
373
|
+
const ref = requiredString(data.ref, "ref", 5, 240);
|
|
374
|
+
const digest = requiredString(data.digest, "digest", 71, 71);
|
|
375
|
+
if (ref !== grant.ref || digest !== grant.digest) {
|
|
376
|
+
throw new RuntimeSkillProviderError("skill_provider_identity_mismatch", "Skill package identity does not match the activation grant");
|
|
377
|
+
}
|
|
378
|
+
const entrypoint = safeReferencePath(requiredString(data.entrypoint, "entrypoint", 1, 512));
|
|
379
|
+
if (!Array.isArray(data.files) || data.files.length === 0) {
|
|
380
|
+
throw providerResponseError("Skill package contains no files");
|
|
381
|
+
}
|
|
382
|
+
if (data.files.length > MAX_PACKAGE_FILES) {
|
|
383
|
+
throw providerResponseError("Skill package contains too many files");
|
|
384
|
+
}
|
|
385
|
+
const seen = new Set();
|
|
386
|
+
let bytes = 0;
|
|
387
|
+
const files = data.files.map((entry) => {
|
|
388
|
+
if (!entry || typeof entry !== "object") {
|
|
389
|
+
throw providerResponseError("Skill package file entry is invalid");
|
|
390
|
+
}
|
|
391
|
+
const record = entry;
|
|
392
|
+
// safeReferencePath rejects absolute paths and any `..` segment, so a hostile or
|
|
393
|
+
// corrupted package cannot write outside the materialization directory.
|
|
394
|
+
const path = safeReferencePath(requiredString(record.path, "path", 1, 512));
|
|
395
|
+
if (seen.has(path)) {
|
|
396
|
+
throw providerResponseError("Skill package repeats a file path");
|
|
397
|
+
}
|
|
398
|
+
seen.add(path);
|
|
399
|
+
const encoded = requiredString(record.contentBase64, "contentBase64", 0, MAX_PACKAGE_BASE64);
|
|
400
|
+
const content = Buffer.from(encoded, "base64");
|
|
401
|
+
bytes += content.byteLength;
|
|
402
|
+
if (bytes > MAX_PACKAGE_BYTES) {
|
|
403
|
+
throw providerResponseError("Skill package exceeds the materialization budget");
|
|
404
|
+
}
|
|
405
|
+
return { path, content: new Uint8Array(content) };
|
|
406
|
+
});
|
|
407
|
+
if (!files.some((file) => file.path === entrypoint)) {
|
|
408
|
+
throw providerResponseError("Skill package is missing its entrypoint");
|
|
409
|
+
}
|
|
410
|
+
return {
|
|
411
|
+
ref,
|
|
412
|
+
digest,
|
|
413
|
+
envelopeDigest: requiredString(data.envelopeDigest, "envelopeDigest", 71, 71),
|
|
414
|
+
entrypoint,
|
|
415
|
+
files,
|
|
416
|
+
bytes,
|
|
417
|
+
};
|
|
418
|
+
}
|
|
358
419
|
function resultData(envelope, expectedCode) {
|
|
359
420
|
if (envelope.schemaVersion !== "tool-result/0.1"
|
|
360
421
|
|| envelope.ok !== true
|
|
@@ -3,6 +3,7 @@ import { chmodSync, existsSync, lstatSync, mkdirSync, realpathSync, rmSync, writ
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import net from "node:net";
|
|
5
5
|
import { MAX_PROGRESS_EVENTS_PER_ATTEMPT } from "../mcp/report-progress.js";
|
|
6
|
+
import { materializeCourseSkills, RUNTIME_SKILLS_DIR_NAME, skillDirectoryName, } from "../runtime-skill-materialization.js";
|
|
6
7
|
import { startCourseSkillsMcpServer, } from "../mcp/course-skills-server.js";
|
|
7
8
|
import { sanitizeRuntimeFailureText } from "../redaction.js";
|
|
8
9
|
import { runtimeChildEnv, runtimeChildLaunch } from "../runtime-env.js";
|
|
@@ -58,6 +59,9 @@ function courseSkillsSystemContext(systemContext, catalog) {
|
|
|
58
59
|
source: "course_service_activation",
|
|
59
60
|
authority: "read_only_catalog_metadata",
|
|
60
61
|
skills: catalog.map((entry) => ({
|
|
62
|
+
// `name` 是 skills 目录名,也是模型传给 load_skill 的参数。0.8.39 的
|
|
63
|
+
// load_skill 不支持列举,名称必须由这里给出。
|
|
64
|
+
name: skillDirectoryName(entry),
|
|
61
65
|
ref: entry.ref,
|
|
62
66
|
description: entry.description.replace(/[\u0000-\u001f\u007f]+/g, " ").trim(),
|
|
63
67
|
})),
|
|
@@ -68,8 +72,8 @@ function courseSkillsSystemContext(systemContext, catalog) {
|
|
|
68
72
|
COURSE_SKILL_CATALOG_BEGIN,
|
|
69
73
|
JSON.stringify(contextEnvelope),
|
|
70
74
|
COURSE_SKILL_CATALOG_END,
|
|
71
|
-
"When one entry is useful, call
|
|
72
|
-
"
|
|
75
|
+
"When one entry is useful, call load_skill with that entry's exact `name` before following that Skill.",
|
|
76
|
+
"These Skills are already installed for this activation; read their files directly and do not fetch them from anywhere else.",
|
|
73
77
|
"Never search for, install, or substitute a Skill outside this activation catalog.",
|
|
74
78
|
].join("\n");
|
|
75
79
|
const context = systemContext?.trim();
|
|
@@ -223,6 +227,40 @@ export class DeepseekTuiAdapter {
|
|
|
223
227
|
throw abortReason(turnAbort.signal);
|
|
224
228
|
handle.inFlight += 1;
|
|
225
229
|
countedInFlight = true;
|
|
230
|
+
// 每个 turn 都要重写 skills 目录,且必须在此处而不是 acquireHandle 内:
|
|
231
|
+
// acquireHandle 只在需要新建进程时执行,复用进程时不会走到,那样第二个 turn
|
|
232
|
+
// 会沿用上一轮的授权集。目录路径固定,所以复用进程的环境变量依然指对。
|
|
233
|
+
if (opts.skillProvider && handle.progressMcpConfig) {
|
|
234
|
+
const startedAt = Date.now();
|
|
235
|
+
try {
|
|
236
|
+
const materialized = await materializeCourseSkills({
|
|
237
|
+
prepared: opts.skillProvider,
|
|
238
|
+
profileRoot: handle.progressMcpConfig.dir,
|
|
239
|
+
managed: Boolean(this.managedActivationId(opts)),
|
|
240
|
+
});
|
|
241
|
+
// info 而非 debug:这是「Skill 是否真的到达模型」的唯一事实。上一次同类故障
|
|
242
|
+
// 之所以要跨四个进程反复排查,就是因为这一段完全没有留痕。
|
|
243
|
+
log.info("deepseek-tui.course-skills-materialized", {
|
|
244
|
+
skillsDir: materialized.skillsDir,
|
|
245
|
+
count: materialized.skills.length,
|
|
246
|
+
fingerprint: materialized.fingerprint,
|
|
247
|
+
refs: materialized.skills.map((skill) => skill.ref),
|
|
248
|
+
names: materialized.skills.map((skill) => skill.name),
|
|
249
|
+
bytes: materialized.skills.reduce((total, skill) => total + skill.bytes, 0),
|
|
250
|
+
durationMs: Date.now() - startedAt,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
log.error("deepseek-tui.course-skills-materialization-failed", {
|
|
255
|
+
code: error instanceof Error && "code" in error
|
|
256
|
+
? String(error.code)
|
|
257
|
+
: "unknown",
|
|
258
|
+
refs: opts.skillProvider.catalog.map((entry) => entry.ref),
|
|
259
|
+
durationMs: Date.now() - startedAt,
|
|
260
|
+
});
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
226
264
|
if (handle.idleTimer)
|
|
227
265
|
clearTimeout(handle.idleTimer);
|
|
228
266
|
const headers = authHeaders(handle.token);
|
|
@@ -443,7 +481,9 @@ export class DeepseekTuiAdapter {
|
|
|
443
481
|
try {
|
|
444
482
|
child = this.spawnFn(launch.binary, launch.args, {
|
|
445
483
|
cwd: opts.cwd,
|
|
446
|
-
env: this.spawnEnv(opts, progressMcpConfig?.path, visionConfigPath
|
|
484
|
+
env: this.spawnEnv(opts, progressMcpConfig?.path, visionConfigPath, progressMcpConfig
|
|
485
|
+
? path.join(progressMcpConfig.dir, RUNTIME_SKILLS_DIR_NAME)
|
|
486
|
+
: undefined),
|
|
447
487
|
...launch.identity,
|
|
448
488
|
stdio: ["ignore", "pipe", "pipe"],
|
|
449
489
|
// 自成进程组:解析到的二进制可能是会再 spawn 真实 deepseek-tui
|
|
@@ -507,7 +547,7 @@ export class DeepseekTuiAdapter {
|
|
|
507
547
|
* pins the durable RuntimeThreadStore to the RuntimeSession workspace even though
|
|
508
548
|
* the credential-bearing server process is still reclaimed after every activation.
|
|
509
549
|
*/
|
|
510
|
-
spawnEnv(opts, progressMcpConfigPath, visionConfigPath) {
|
|
550
|
+
spawnEnv(opts, progressMcpConfigPath, visionConfigPath, skillsDir) {
|
|
511
551
|
const env = {
|
|
512
552
|
...runtimeChildEnv(opts.env ?? process.env),
|
|
513
553
|
FORCE_COLOR: "0",
|
|
@@ -515,6 +555,12 @@ export class DeepseekTuiAdapter {
|
|
|
515
555
|
};
|
|
516
556
|
if (progressMcpConfigPath)
|
|
517
557
|
env.DEEPSEEK_MCP_CONFIG = progressMcpConfigPath;
|
|
558
|
+
if (skillsDir) {
|
|
559
|
+
// 该路径在一个 profile root 内固定,进程池复用时依然有效;每个 turn 只重写目录
|
|
560
|
+
// 内容。0.9.x 起 CLI 改名为 Codewhale,两个变量都设以覆盖升级窗口。
|
|
561
|
+
env.DEEPSEEK_SKILLS_DIR = skillsDir;
|
|
562
|
+
env.CODEWHALE_SKILLS_DIR = skillsDir;
|
|
563
|
+
}
|
|
518
564
|
if (visionConfigPath)
|
|
519
565
|
env.DEEPSEEK_CONFIG_PATH = visionConfigPath;
|
|
520
566
|
if (this.managedActivationId(opts)) {
|
package/package.json
CHANGED