@botlearn-course/daemon 0.0.29 → 0.0.30-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent-service-sandbox.js +5 -3
- package/dist/file-candidates.d.ts +3 -2
- package/dist/file-candidates.js +46 -2
- package/dist/run-dispatcher.js +1 -1
- package/dist/runtimes/deepseek-tui.js +14 -7
- package/dist/runtimes/pi-agent.js +9 -5
- package/dist/runtimes/pi-managed-subagent-extension.js +10 -2
- package/dist/runtimes/pi-managed.d.ts +4 -3
- package/dist/runtimes/pi-managed.js +6 -3
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
|
@@ -1732,10 +1732,12 @@ export class AgentServiceSandboxClient {
|
|
|
1732
1732
|
this.assertTurnScope(frame, pendingFile.scope, "file report ACK");
|
|
1733
1733
|
this.pendingFileReports.delete(frameId);
|
|
1734
1734
|
const files = frame.payload.files;
|
|
1735
|
-
|
|
1735
|
+
// 删除提交后已无文件记录;帧回执确认整批,只返回新增/修改的记录。
|
|
1736
|
+
const expectedFiles = pendingFile.files.filter(file => file.event !== "deleted");
|
|
1737
|
+
if (!Array.isArray(files) || files.length !== expectedFiles.length || files.some((file, i) => (!file || typeof file !== "object" || typeof file.id !== "string"
|
|
1736
1738
|
|| file.agent_run_id !== pendingFile.agentRunId
|
|
1737
|
-
|| file.path !==
|
|
1738
|
-
|| file.sha256 !==
|
|
1739
|
+
|| file.path !== expectedFiles[i]?.path
|
|
1740
|
+
|| file.sha256 !== expectedFiles[i]?.sha256))) {
|
|
1739
1741
|
pendingFile.reject(new Error("file report ACK does not match the batch"));
|
|
1740
1742
|
}
|
|
1741
1743
|
else {
|
|
@@ -35,13 +35,14 @@ interface WorkspaceFileFingerprint {
|
|
|
35
35
|
/**
|
|
36
36
|
* 普通 Agent turn 启动前捕获的 workspace 文件基线。
|
|
37
37
|
*
|
|
38
|
-
*
|
|
38
|
+
* 基线只用于判断本轮新增/修改/删除,不会整体上报给 Course Service,也不把迁移复制进来的
|
|
39
39
|
* 既有文件变成本轮 TaskRun 证据。truncated=true 时,缺席于 files 的路径不能安全地
|
|
40
|
-
* 判定为“本轮新增”,所以上报阶段会对这类路径 fail closed
|
|
40
|
+
* 判定为“本轮新增”,所以上报阶段会对这类路径 fail closed;截断时也不推断删除。
|
|
41
41
|
*/
|
|
42
42
|
export interface WorkspaceFileBaseline {
|
|
43
43
|
files: ReadonlyMap<string, WorkspaceFileFingerprint>;
|
|
44
44
|
truncated: boolean;
|
|
45
|
+
rootIdentity?: string;
|
|
45
46
|
}
|
|
46
47
|
/**
|
|
47
48
|
* 递归扫描 workspace 产出文件候选。
|
package/dist/file-candidates.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { constants } from "node:fs";
|
|
3
|
-
import { open, readdir } from "node:fs/promises";
|
|
3
|
+
import { lstat, open, readdir } from "node:fs/promises";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { performance } from "node:perf_hooks";
|
|
6
6
|
import { redactSecretString } from "./redaction.js";
|
|
@@ -17,6 +17,32 @@ const DEFAULT_MAX_DEPTH = 8;
|
|
|
17
17
|
const DEFAULT_MAX_REPORT_MS = 10_000;
|
|
18
18
|
// 文本启发式:前 4KiB 不含 NUL 字节即视为文本,可取 preview。
|
|
19
19
|
const TEXT_SNIFF_BYTES = 4096;
|
|
20
|
+
async function workspaceRootIdentity(workspaceDir) {
|
|
21
|
+
try {
|
|
22
|
+
const stat = await lstat(workspaceDir, { bigint: true });
|
|
23
|
+
return stat.isDirectory() ? `${stat.dev}:${stat.ino}` : undefined;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** 只确认不存在;不可访问、符号链接或路径类型变化均不能证明删除。 */
|
|
30
|
+
async function isMissingWorkspacePath(workspaceDir, relativePath) {
|
|
31
|
+
const segments = relativePath.split("/");
|
|
32
|
+
let current = workspaceDir;
|
|
33
|
+
for (const [index, segment] of segments.entries()) {
|
|
34
|
+
current = path.join(current, segment);
|
|
35
|
+
try {
|
|
36
|
+
const stat = await lstat(current);
|
|
37
|
+
if (stat.isSymbolicLink() || (index < segments.length - 1 && !stat.isDirectory()))
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
return error.code === "ENOENT";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
20
46
|
// 与后端 _is_safe_relative_path 同规:非空、非绝对、无反斜杠、无 .. 段、无空段。
|
|
21
47
|
function isSafeRelativePath(rel) {
|
|
22
48
|
if (!rel || rel.startsWith("/") || rel.includes("\\"))
|
|
@@ -163,13 +189,15 @@ export async function scanWorkspaceFiles(workspaceDir, limits = {}) {
|
|
|
163
189
|
}
|
|
164
190
|
/** 捕获普通 Agent turn 开始前的 workspace 基线。 */
|
|
165
191
|
export async function captureWorkspaceFileBaseline(workspaceDir, limits = {}) {
|
|
192
|
+
const rootIdentity = await workspaceRootIdentity(workspaceDir);
|
|
166
193
|
const { files, truncated } = await scanWorkspaceFiles(workspaceDir, limits);
|
|
167
194
|
return {
|
|
168
195
|
files: new Map(files.map((file) => [
|
|
169
196
|
file.path,
|
|
170
197
|
{ sizeBytes: file.size_bytes ?? 0, sha256: file.sha256 ?? "" },
|
|
171
198
|
])),
|
|
172
|
-
truncated,
|
|
199
|
+
truncated: truncated || !rootIdentity || rootIdentity !== await workspaceRootIdentity(workspaceDir),
|
|
200
|
+
rootIdentity,
|
|
173
201
|
};
|
|
174
202
|
}
|
|
175
203
|
export function fileReportBatches(files) {
|
|
@@ -222,6 +250,22 @@ export async function reportFileCandidates(client, agentRunId, workspaceDir, log
|
|
|
222
250
|
})
|
|
223
251
|
: scan.files;
|
|
224
252
|
const truncated = scan.truncated || Boolean(baseline?.truncated);
|
|
253
|
+
if (baseline?.rootIdentity && !truncated &&
|
|
254
|
+
baseline.rootIdentity === await workspaceRootIdentity(workspaceDir)) {
|
|
255
|
+
const currentPaths = new Set(scan.files.map((file) => file.path));
|
|
256
|
+
const deleted = [];
|
|
257
|
+
for (const [relativePath, previous] of baseline.files) {
|
|
258
|
+
if (!currentPaths.has(relativePath) && await isMissingWorkspacePath(workspaceDir, relativePath)) {
|
|
259
|
+
deleted.push({
|
|
260
|
+
absPath: path.join(workspaceDir, relativePath),
|
|
261
|
+
event: "deleted", path: relativePath,
|
|
262
|
+
size_bytes: previous.sizeBytes, sha256: previous.sha256,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (baseline.rootIdentity === await workspaceRootIdentity(workspaceDir))
|
|
267
|
+
files.push(...deleted);
|
|
268
|
+
}
|
|
225
269
|
if (truncated) {
|
|
226
270
|
log.warn("workspace file scan truncated", { agentRunId, reported: files.length });
|
|
227
271
|
}
|
package/dist/run-dispatcher.js
CHANGED
|
@@ -419,7 +419,7 @@ export class RunDispatcher {
|
|
|
419
419
|
throw new RuntimeExecutionError(`runtime is missing required capabilities: ${missingCapabilities.join(", ")}`, "runtime_unavailable");
|
|
420
420
|
}
|
|
421
421
|
// 迁移复制进来的文件属于 CourseRun workspace 基线,不属于本轮 TaskRun 产物。
|
|
422
|
-
//
|
|
422
|
+
// 模型执行后上报相对这份快照新增、修改或明确删除的文件。
|
|
423
423
|
const workspaceFileBaseline = await captureWorkspaceFileBaseline(workspaceDir, this.scanLimits);
|
|
424
424
|
const activeTranscript = new TranscriptWriter(persistentTurn?.transcriptFile ?? transcriptPath(runId));
|
|
425
425
|
transcript = activeTranscript;
|
|
@@ -440,8 +440,11 @@ export class DeepseekTuiAdapter {
|
|
|
440
440
|
// Newer runtimes must advertise native external tools; never silently fall
|
|
441
441
|
// back to asynchronous MCP discovery after a capability/protocol failure.
|
|
442
442
|
const legacySkillsMcp = runtimeVersion === "0.8.39";
|
|
443
|
-
|
|
444
|
-
|
|
443
|
+
// Managed turns always run in an isolated runtime profile; the Skill tools themselves
|
|
444
|
+
// are exposed only when this activation authorizes at least one course Skill.
|
|
445
|
+
const isolatesSkillProfile = Boolean(opts.skillProvider || managedActivationId);
|
|
446
|
+
const exposesCourseSkills = (opts.skillProvider?.catalog.length ?? 0) > 0;
|
|
447
|
+
const courseSkills = exposesCourseSkills && !legacySkillsMcp
|
|
445
448
|
? new DeepseekCourseSkills({
|
|
446
449
|
prepared: opts.skillProvider,
|
|
447
450
|
onEvent: opts.onSkillEvent ?? (async () => { }),
|
|
@@ -451,14 +454,14 @@ export class DeepseekTuiAdapter {
|
|
|
451
454
|
let progressMcpConfig;
|
|
452
455
|
let runtimeConfigPath;
|
|
453
456
|
try {
|
|
454
|
-
courseSkillsMcpServer =
|
|
457
|
+
courseSkillsMcpServer = exposesCourseSkills && legacySkillsMcp
|
|
455
458
|
? await startCourseSkillsMcpServer({
|
|
456
459
|
prepared: opts.skillProvider,
|
|
457
460
|
onEvent: opts.onSkillEvent ?? (async () => { }),
|
|
458
461
|
})
|
|
459
462
|
: undefined;
|
|
460
463
|
progressMcpConfig =
|
|
461
|
-
this.progressPromptInjectionEnabled ||
|
|
464
|
+
this.progressPromptInjectionEnabled || isolatesSkillProfile
|
|
462
465
|
|| (managedActivationId && opts.env?.DEEPSEEK_BASE_URL?.trim())
|
|
463
466
|
? createProgressMcpConfig({
|
|
464
467
|
...(courseSkillsMcpServer
|
|
@@ -466,7 +469,7 @@ export class DeepseekTuiAdapter {
|
|
|
466
469
|
: {}),
|
|
467
470
|
})
|
|
468
471
|
: undefined;
|
|
469
|
-
if (
|
|
472
|
+
if (isolatesSkillProfile && progressMcpConfig) {
|
|
470
473
|
prepareRuntimeSkillsHome(progressMcpConfig.dir, Boolean(this.managedActivationId(opts)));
|
|
471
474
|
}
|
|
472
475
|
runtimeConfigPath = createManagedRuntimeConfig(opts, progressMcpConfig);
|
|
@@ -490,7 +493,7 @@ export class DeepseekTuiAdapter {
|
|
|
490
493
|
try {
|
|
491
494
|
child = this.spawnFn(launch.binary, launch.args, {
|
|
492
495
|
cwd: opts.cwd,
|
|
493
|
-
env: this.spawnEnv(opts, progressMcpConfig?.path, runtimeConfigPath, progressMcpConfig &&
|
|
496
|
+
env: this.spawnEnv(opts, progressMcpConfig?.path, runtimeConfigPath, progressMcpConfig && isolatesSkillProfile
|
|
494
497
|
? path.join(progressMcpConfig.dir, RUNTIME_SKILLS_RELATIVE_PATH)
|
|
495
498
|
: undefined),
|
|
496
499
|
...launch.identity,
|
|
@@ -1494,7 +1497,11 @@ function extractDeepseekError(eventName, payload) {
|
|
|
1494
1497
|
return undefined;
|
|
1495
1498
|
}
|
|
1496
1499
|
function classifyDeepseekProviderError(error) {
|
|
1497
|
-
|
|
1500
|
+
// DeepSeek can flatten a provider 403 into this authorization-prefixed message,
|
|
1501
|
+
// dropping the structured code. Match the specific precharge signature only.
|
|
1502
|
+
const quotaCode = /"code"\s*:\s*"(?:insufficient_user_quota|insufficient_token_quota|insufficient_quota|trial_credit_insufficient|credit_insufficient)"/.test(error);
|
|
1503
|
+
const prechargeFailure = /^Authorization failed:\s*预扣费额度失败,\s*用户剩余额度:\s*[$$][0-9]+(?:\.[0-9]+)?,\s*需要预扣费额度:\s*[$$][0-9]+(?:\.[0-9]+)?(?:\s|$)/.test(error);
|
|
1504
|
+
return quotaCode || prechargeFailure
|
|
1498
1505
|
? "model_token_quota_exhausted"
|
|
1499
1506
|
: undefined;
|
|
1500
1507
|
}
|
|
@@ -77,11 +77,15 @@ export class PiAgentAdapter {
|
|
|
77
77
|
const prepared = await prepareManagedPi(opts);
|
|
78
78
|
let skills;
|
|
79
79
|
try {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
// Without an authorized Skill the turn gets no Skill tools: an empty catalog only
|
|
81
|
+
// invites the model to look it up and narrate the result to the learner.
|
|
82
|
+
if ((opts.skillProvider?.catalog.length ?? 0) > 0) {
|
|
83
|
+
skills = await startCourseSkillsMcpServer({ prepared: opts.skillProvider,
|
|
84
|
+
onEvent: opts.onSkillEvent ?? (async () => { }) });
|
|
85
|
+
await skills.markApplied();
|
|
86
|
+
}
|
|
87
|
+
const delegation = writeManagedPiDelegation(prepared.delegationDir, opts, skills?.socketPath);
|
|
88
|
+
return await this.execute({ ...opts, env: prepared.env }, skills?.socketPath, true, delegation);
|
|
85
89
|
}
|
|
86
90
|
finally {
|
|
87
91
|
try {
|
|
@@ -56,11 +56,17 @@ export function readDelegation(file) {
|
|
|
56
56
|
throw new Error("Invalid managed delegation path");
|
|
57
57
|
const config = object(JSON.parse(readFileSync(file, "utf8")));
|
|
58
58
|
const keys = ["binary", "model", "workspaceDir", "systemPrompt", "workerPromptPath", "webExtension",
|
|
59
|
-
"
|
|
59
|
+
"subagentExtension"];
|
|
60
60
|
for (const key of keys) {
|
|
61
61
|
if (typeof config[key] !== "string" || !config[key])
|
|
62
62
|
throw new Error("Invalid managed delegation configuration");
|
|
63
63
|
}
|
|
64
|
+
// Skill delegation is all-or-nothing: an activation without Skills omits both fields.
|
|
65
|
+
const skillKeys = ["skillsExtension", "skillsSocket"];
|
|
66
|
+
const presentSkillKeys = skillKeys.filter((key) => config[key] !== undefined);
|
|
67
|
+
if (presentSkillKeys.length === 1 || presentSkillKeys.some((key) => typeof config[key] !== "string" || !config[key])) {
|
|
68
|
+
throw new Error("Invalid managed delegation configuration");
|
|
69
|
+
}
|
|
64
70
|
if (config.thinking !== undefined && typeof config.thinking !== "string") {
|
|
65
71
|
throw new Error("Invalid managed delegation thinking level");
|
|
66
72
|
}
|
|
@@ -73,7 +79,9 @@ export function childArgs(config, manifestPath, task) {
|
|
|
73
79
|
"--model", config.model];
|
|
74
80
|
if (config.thinking)
|
|
75
81
|
args.push("--thinking", config.thinking);
|
|
76
|
-
args.push("--system-prompt", config.systemPrompt, "--append-system-prompt", config.workerPromptPath,
|
|
82
|
+
args.push("--system-prompt", config.systemPrompt, "--append-system-prompt", config.workerPromptPath, ...(config.skillsExtension && config.skillsSocket ? [
|
|
83
|
+
"--extension", config.skillsExtension, "--botlearn-course-skills-socket", config.skillsSocket,
|
|
84
|
+
] : []), "--extension", config.webExtension, "--extension", config.subagentExtension, "--botlearn-delegation-manifest", manifestPath, `Task: ${task}`);
|
|
77
85
|
return args;
|
|
78
86
|
}
|
|
79
87
|
const active = new Set();
|
|
@@ -10,8 +10,9 @@ export interface ManagedPiDelegation {
|
|
|
10
10
|
systemPrompt: string;
|
|
11
11
|
workerPromptPath: string;
|
|
12
12
|
webExtension: string;
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
/** Present only when the activation authorizes at least one course Skill. */
|
|
14
|
+
skillsExtension?: string;
|
|
15
|
+
skillsSocket?: string;
|
|
15
16
|
subagentExtension: string;
|
|
16
17
|
}
|
|
17
18
|
/** The image verifies the CLI once; per-turn probes read immutable installed metadata. */
|
|
@@ -22,4 +23,4 @@ export declare function prepareManagedPi(opts: EngineRunOptions): Promise<{
|
|
|
22
23
|
cleanup(): void;
|
|
23
24
|
}>;
|
|
24
25
|
/** Called after the course Skills socket exists, before the first Pi process starts. */
|
|
25
|
-
export declare function writeManagedPiDelegation(dir: string, opts: EngineRunOptions, skillsSocket
|
|
26
|
+
export declare function writeManagedPiDelegation(dir: string, opts: EngineRunOptions, skillsSocket?: string): string;
|
|
@@ -102,7 +102,8 @@ export function writeManagedPiDelegation(dir, opts, skillsSocket) {
|
|
|
102
102
|
const thinkingIndex = opts.extraArgs?.indexOf("--thinking") ?? -1;
|
|
103
103
|
const model = modelIndex >= 0 ? opts.extraArgs?.[modelIndex + 1] : undefined;
|
|
104
104
|
const thinking = thinkingIndex >= 0 ? opts.extraArgs?.[thinkingIndex + 1] : undefined;
|
|
105
|
-
if (!model || opts.systemPrompt === undefined
|
|
105
|
+
if (!model || opts.systemPrompt === undefined
|
|
106
|
+
|| (skillsSocket !== undefined && !skillsSocket.startsWith("/"))) {
|
|
106
107
|
throw new Error("pi-agent managed delegation configuration is incomplete");
|
|
107
108
|
}
|
|
108
109
|
const workerPromptPath = path.join(dir, "worker.md");
|
|
@@ -116,8 +117,10 @@ export function writeManagedPiDelegation(dir, opts, skillsSocket) {
|
|
|
116
117
|
systemPrompt: opts.systemPrompt,
|
|
117
118
|
workerPromptPath,
|
|
118
119
|
webExtension: MANAGED_PI_WEB_ACCESS_EXTENSION,
|
|
119
|
-
|
|
120
|
-
|
|
120
|
+
...(skillsSocket ? {
|
|
121
|
+
skillsExtension: path.resolve(fileURLToPath(new URL("./pi-course-skills-extension.js", import.meta.url))),
|
|
122
|
+
skillsSocket,
|
|
123
|
+
} : {}),
|
|
121
124
|
subagentExtension: MANAGED_PI_SUBAGENT_EXTENSION,
|
|
122
125
|
};
|
|
123
126
|
const file = path.join(dir, "launch.json");
|
package/dist/types.d.ts
CHANGED
|
@@ -83,6 +83,7 @@ export interface RunEventReceipt {
|
|
|
83
83
|
}
|
|
84
84
|
/** 经 Agent Service 上报的文件候选。 */
|
|
85
85
|
export interface RunFileCandidate {
|
|
86
|
+
/** deleted 使用删除前的 size_bytes/sha256;确认后不返回文件记录。 */
|
|
86
87
|
event?: "created" | "modified" | "deleted";
|
|
87
88
|
/** workspace 相对路径,正斜杠;不得包含 `..`、绝对路径或反斜杠(后端 400)。 */
|
|
88
89
|
path: string;
|