@botlearn-course/daemon 0.0.30-beta.1 → 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.
@@ -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
- if (!Array.isArray(files) || files.length !== pendingFile.files.length || files.some((file, i) => (!file || typeof file !== "object" || typeof file.id !== "string"
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 !== pendingFile.files[i]?.path
1738
- || file.sha256 !== pendingFile.files[i]?.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
- * 基线只用于判断本轮新增/修改,不会上报给 Course Service,也不把迁移复制进来的
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 产出文件候选。
@@ -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
  }
@@ -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;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botlearn-course/daemon",
3
- "version": "0.0.30-beta.1",
3
+ "version": "0.0.30-beta.2",
4
4
  "description": "BotLearn Course daemon for Agent Service managed sandboxes (internal).",
5
5
  "type": "module",
6
6
  "bin": {