@botlearn-course/daemon 0.0.26 → 0.0.27

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.
@@ -124,6 +124,7 @@ export declare class AgentServiceSandboxClient implements RunReportingClient, Pe
124
124
  private replaySpool;
125
125
  private sendHeartbeat;
126
126
  private sendCommandAck;
127
+ private handleWorkspaceFilesList;
127
128
  private handleWorkspaceFileRead;
128
129
  private sendWorkspaceFileResult;
129
130
  private sendControlFrame;
@@ -1,9 +1,10 @@
1
+ import { scanWorkspaceFiles } from "./file-candidates.js";
1
2
  import { RUNTIME_PROMPT_CAPABILITY, validateRuntimePrompt } from "./runtime-prompt.js";
2
3
  import { randomUUID } from "node:crypto";
3
4
  import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
4
5
  import path from "node:path";
5
6
  import { ensureDaemonHome } from "./auth-store.js";
6
- import { AGENT_SERVICE_WS_SCHEMA, AGENT_SERVICE_WS_SUBPROTOCOL, createSandboxFrame, createWorkspaceFileChunk, parseSandboxFrame, UnsupportedSandboxProtocolError, WORKSPACE_FILE_READ_BINARY_CAPABILITY, } from "./agent-service-ws-protocol.js";
7
+ import { AGENT_SERVICE_WS_SCHEMA, AGENT_SERVICE_WS_SUBPROTOCOL, createSandboxFrame, createWorkspaceFileChunk, parseSandboxFrame, UnsupportedSandboxProtocolError, WORKSPACE_FILE_READ_BINARY_CAPABILITY, WORKSPACE_FILES_LIST_CAPABILITY, } from "./agent-service-ws-protocol.js";
7
8
  import { log as defaultLog, setOperationalLogSink, } from "./log.js";
8
9
  import { availableRunCapabilities, runtimeSupportsCourseSkills, } from "./runtime-capabilities.js";
9
10
  import { activationRuntimeEnv, runtimeChildEnv } from "./runtime-env.js";
@@ -861,6 +862,7 @@ export class AgentServiceSandboxClient {
861
862
  protocol_versions: [AGENT_SERVICE_WS_SCHEMA],
862
863
  capabilities: [
863
864
  WORKSPACE_FILE_READ_BINARY_CAPABILITY,
865
+ WORKSPACE_FILES_LIST_CAPABILITY,
864
866
  RUNTIME_PROMPT_CAPABILITY,
865
867
  ...workspaceDurabilityCapabilities(),
866
868
  ],
@@ -1036,6 +1038,11 @@ export class AgentServiceSandboxClient {
1036
1038
  await this.sendCommandAck(frame, "ok");
1037
1039
  return;
1038
1040
  }
1041
+ case "workspace.files.list":
1042
+ void this.handleWorkspaceFilesList(frame).catch(() => {
1043
+ this.log.warn("Workspace file list response failed");
1044
+ });
1045
+ return;
1039
1046
  case "workspace.file.read":
1040
1047
  void this.handleWorkspaceFileRead(frame).catch((error) => {
1041
1048
  this.log.warn("Agent Service workspace file read response failed", {
@@ -1790,6 +1797,41 @@ export class AgentServiceSandboxClient {
1790
1797
  ...(retryable !== undefined ? { retryable } : {}),
1791
1798
  });
1792
1799
  }
1800
+ async handleWorkspaceFilesList(frame) {
1801
+ const session = this.state.sessions[frame.runtime_session_id];
1802
+ const requestId = frame.payload.request_id;
1803
+ let payload = {
1804
+ request_id: requestId, status: "rejected", error_code: "workspace_file_unavailable",
1805
+ };
1806
+ if (typeof requestId !== "string" || !WORKSPACE_FILE_TRANSFER_ID.test(requestId))
1807
+ return;
1808
+ if (session?.workspaceId && !this.workspaceFileReadInFlight) {
1809
+ this.workspaceFileReadInFlight = true;
1810
+ try {
1811
+ const scan = await scanWorkspaceFiles(runtimeSessionWorkspaceDir(session.workspaceId, this.sandboxGeneration), { maxPreviewChars: 0 });
1812
+ payload = {
1813
+ request_id: requestId, status: "ok", truncated: scan.truncated,
1814
+ files: scan.files.map(({ path, name, size_bytes, sha256 }) => ({
1815
+ path, name, size_bytes, sha256,
1816
+ })),
1817
+ };
1818
+ }
1819
+ catch {
1820
+ // A scan failure is never an empty successful directory.
1821
+ }
1822
+ finally {
1823
+ this.workspaceFileReadInFlight = false;
1824
+ }
1825
+ }
1826
+ if (frame.sandbox_generation !== this.sandboxGeneration ||
1827
+ frame.connection_epoch !== this.connectionEpoch)
1828
+ return;
1829
+ await this.sendFrame(createSandboxFrame({
1830
+ type: "workspace.files.result", sandboxId: this.options.sandboxId,
1831
+ sandboxGeneration: this.sandboxGeneration, connectionEpoch: this.connectionEpoch,
1832
+ seq: this.nextOutboundSeq(), runtimeSessionId: frame.runtime_session_id, payload,
1833
+ }));
1834
+ }
1793
1835
  async handleWorkspaceFileRead(frame) {
1794
1836
  const transferId = frame.payload.transfer_id;
1795
1837
  const relativePath = frame.payload.path;
@@ -1,9 +1,10 @@
1
1
  export declare const AGENT_SERVICE_WS_SCHEMA: "botlearn-agent-sandbox-ws/0.4";
2
2
  export declare const AGENT_SERVICE_WS_SUBPROTOCOL: "botlearn-agent-sandbox.v4";
3
3
  export declare const WORKSPACE_FILE_READ_BINARY_CAPABILITY: "workspace_file_read_binary_v1";
4
+ export declare const WORKSPACE_FILES_LIST_CAPABILITY: "workspace_files_list_v1";
4
5
  export declare const WORKSPACE_FILE_CHUNK_BYTES: number;
5
6
  export declare const WORKSPACE_FILE_CHUNK_HEADER_BYTES = 29;
6
- export type SandboxFrameType = "sandbox.hello" | "sandbox.sync" | "session.open" | "session.activate" | "turn.start" | "turn.cancel" | "session.close" | "sandbox.drain" | "sandbox.shutdown" | "event.ack" | "auth.rotate" | "workspace.file.read" | "ping" | "sandbox.ready" | "sandbox.heartbeat" | "command.ack" | "session.opened" | "session.open_failed" | "session.closed" | "activation.cleaned" | "turn.event" | "turn.file.report" | "workspace.file.result" | "sandbox.drained" | "sandbox.log" | "pong" | "protocol.error";
7
+ export type SandboxFrameType = "sandbox.hello" | "sandbox.sync" | "session.open" | "session.activate" | "turn.start" | "turn.cancel" | "session.close" | "sandbox.drain" | "sandbox.shutdown" | "event.ack" | "auth.rotate" | "workspace.file.read" | "workspace.files.list" | "ping" | "sandbox.ready" | "sandbox.heartbeat" | "command.ack" | "session.opened" | "session.open_failed" | "session.closed" | "activation.cleaned" | "turn.event" | "turn.file.report" | "workspace.file.result" | "workspace.files.result" | "sandbox.drained" | "sandbox.log" | "pong" | "protocol.error";
7
8
  export declare class UnsupportedSandboxProtocolError extends Error {
8
9
  readonly schemaVersion: unknown;
9
10
  constructor(schemaVersion: unknown);
@@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto";
2
2
  export const AGENT_SERVICE_WS_SCHEMA = "botlearn-agent-sandbox-ws/0.4";
3
3
  export const AGENT_SERVICE_WS_SUBPROTOCOL = "botlearn-agent-sandbox.v4";
4
4
  export const WORKSPACE_FILE_READ_BINARY_CAPABILITY = "workspace_file_read_binary_v1";
5
+ export const WORKSPACE_FILES_LIST_CAPABILITY = "workspace_files_list_v1";
5
6
  export const WORKSPACE_FILE_CHUNK_BYTES = 64 * 1024;
6
7
  export const WORKSPACE_FILE_CHUNK_HEADER_BYTES = 29;
7
8
  const WORKSPACE_FILE_CHUNK_MAGIC = Buffer.from("BLWF", "ascii");
@@ -18,6 +19,7 @@ const FRAME_TYPES = new Set([
18
19
  "event.ack",
19
20
  "auth.rotate",
20
21
  "workspace.file.read",
22
+ "workspace.files.list",
21
23
  "ping",
22
24
  "sandbox.ready",
23
25
  "sandbox.heartbeat",
@@ -29,6 +31,7 @@ const FRAME_TYPES = new Set([
29
31
  "turn.event",
30
32
  "turn.file.report",
31
33
  "workspace.file.result",
34
+ "workspace.files.result",
32
35
  "sandbox.drained",
33
36
  "sandbox.log",
34
37
  "pong",
@@ -43,7 +46,9 @@ const SESSION_TYPES = new Set([
43
46
  "session.open_failed",
44
47
  "session.closed",
45
48
  "workspace.file.read",
49
+ "workspace.files.list",
46
50
  "workspace.file.result",
51
+ "workspace.files.result",
47
52
  ]);
48
53
  /** TURN 帧:必须带 runtime_session_id + agent_run_id + worker_attempt + activation_id 四元组。 */
49
54
  const TURN_TYPES = new Set([
@@ -94,6 +94,7 @@ export async function scanWorkspaceFiles(workspaceDir, limits = {}) {
94
94
  entries = await readdir(dir, { withFileTypes: true });
95
95
  }
96
96
  catch {
97
+ truncated = true;
97
98
  return;
98
99
  }
99
100
  entries.sort((a, b) => a.name.localeCompare(b.name));
@@ -408,19 +408,23 @@ export class DeepseekTuiAdapter {
408
408
  };
409
409
  }
410
410
  finally {
411
- opts.signal.removeEventListener("abort", onAbort);
412
- if (handle && countedInFlight) {
413
- handle.inFlight = Math.max(0, handle.inFlight - 1);
414
- if (managedActivationId && !this.explicitServerUrl && handle.inFlight === 0) {
415
- if (PROCESS_POOL.get(POOL_KEY) === handle)
416
- PROCESS_POOL.delete(POOL_KEY);
417
- shutdownHandle(handle, "managed-activation-finished");
418
- }
419
- else if (!this.explicitServerUrl) {
420
- resetIdle(handle, POOL_KEY);
411
+ try {
412
+ opts.signal.removeEventListener("abort", onAbort);
413
+ if (handle && countedInFlight) {
414
+ handle.inFlight = Math.max(0, handle.inFlight - 1);
415
+ if (managedActivationId && !this.explicitServerUrl && handle.inFlight === 0) {
416
+ if (PROCESS_POOL.get(POOL_KEY) === handle)
417
+ PROCESS_POOL.delete(POOL_KEY);
418
+ shutdownHandle(handle, "managed-activation-finished");
419
+ }
420
+ else if (!this.explicitServerUrl) {
421
+ resetIdle(handle, POOL_KEY);
422
+ }
421
423
  }
422
424
  }
423
- releaseTurn?.();
425
+ finally {
426
+ releaseTurn?.();
427
+ }
424
428
  }
425
429
  }
426
430
  resolveBinary() {
@@ -492,8 +496,7 @@ export class DeepseekTuiAdapter {
492
496
  visionConfigPath = createManagedVisionConfig(opts, progressMcpConfig);
493
497
  }
494
498
  catch (error) {
495
- cleanupProgressMcpConfig(progressMcpConfig);
496
- await courseSkillsMcpServer?.close();
499
+ cleanupDeepseekResources({ progressMcpConfig, courseSkillsMcpServer });
497
500
  throw error;
498
501
  }
499
502
  const binary = this.resolveBinary();
@@ -523,8 +526,7 @@ export class DeepseekTuiAdapter {
523
526
  });
524
527
  }
525
528
  catch (error) {
526
- cleanupProgressMcpConfig(progressMcpConfig);
527
- await courseSkillsMcpServer?.close();
529
+ cleanupDeepseekResources({ progressMcpConfig, courseSkillsMcpServer });
528
530
  throw error;
529
531
  }
530
532
  installExitCleanupHook();
@@ -547,19 +549,14 @@ export class DeepseekTuiAdapter {
547
549
  handle.closed = true;
548
550
  if (PROCESS_POOL.get(POOL_KEY) === handle)
549
551
  PROCESS_POOL.delete(POOL_KEY);
550
- cleanupProgressMcpConfig(handle.progressMcpConfig);
551
- handle.progressMcpConfig = undefined;
552
- void handle.courseSkillsMcpServer?.close();
553
- handle.courseSkillsMcpServer = undefined;
552
+ cleanupDeepseekResources(handle);
554
553
  });
555
554
  child.on("error", () => {
556
555
  handle.closed = true;
557
556
  if (PROCESS_POOL.get(POOL_KEY) === handle)
558
557
  PROCESS_POOL.delete(POOL_KEY);
559
- cleanupProgressMcpConfig(handle.progressMcpConfig);
560
- handle.progressMcpConfig = undefined;
561
- void handle.courseSkillsMcpServer?.close();
562
- handle.courseSkillsMcpServer = undefined;
558
+ // Node emits close after a spawn error too. An error alone (for example a
559
+ // failed kill) does not prove the process has stopped writing its profile.
563
560
  });
564
561
  try {
565
562
  await waitForHealth(baseUrl, this.fetchFn, handle, STARTUP_TIMEOUT_MS, signal);
@@ -1588,16 +1585,37 @@ function resetIdle(handle, key) {
1588
1585
  }, DEEPSEEK_IDLE_TIMEOUT_MS);
1589
1586
  handle.idleTimer.unref?.();
1590
1587
  }
1588
+ function cleanupDeepseekResources(handle) {
1589
+ const config = handle.progressMcpConfig;
1590
+ const server = handle.courseSkillsMcpServer;
1591
+ handle.progressMcpConfig = undefined;
1592
+ handle.courseSkillsMcpServer = undefined;
1593
+ try {
1594
+ cleanupProgressMcpConfig(config);
1595
+ }
1596
+ catch (error) {
1597
+ log.warn("deepseek-tui.profile-cleanup-failed", {
1598
+ error: sanitizeRuntimeFailureText(error instanceof Error ? error.message : String(error), 1024),
1599
+ });
1600
+ }
1601
+ finally {
1602
+ if (server) {
1603
+ void Promise.resolve().then(() => server.close()).catch((error) => {
1604
+ log.warn("deepseek-tui.skills-server-close-failed", {
1605
+ error: sanitizeRuntimeFailureText(error instanceof Error ? error.message : String(error), 1024),
1606
+ });
1607
+ });
1608
+ }
1609
+ }
1610
+ }
1591
1611
  function shutdownHandle(handle, reason) {
1592
1612
  if (handle.closed)
1593
1613
  return;
1594
1614
  handle.closed = true;
1595
1615
  if (handle.idleTimer)
1596
1616
  clearTimeout(handle.idleTimer);
1597
- cleanupProgressMcpConfig(handle.progressMcpConfig);
1598
- handle.progressMcpConfig = undefined;
1599
- void handle.courseSkillsMcpServer?.close();
1600
- handle.courseSkillsMcpServer = undefined;
1617
+ // The close listener owns cleanup. The process (or its dispatcher child) may
1618
+ // still be writing HOME/.deepseek/snapshots after SIGTERM is sent.
1601
1619
  try {
1602
1620
  const pid = handle.child.pid;
1603
1621
  if (typeof pid === "number" && pid > 0) {
@@ -1618,9 +1636,8 @@ function shutdownHandle(handle, reason) {
1618
1636
  }
1619
1637
  }
1620
1638
  try {
1621
- handle.child.stdout?.destroy();
1622
- handle.child.stderr?.destroy();
1623
- handle.child.stdin?.destroy();
1639
+ // Keep the pipes open until close confirms both process exit and closed
1640
+ // stdio; destroying them here would discard that ordering guarantee.
1624
1641
  handle.child.unref();
1625
1642
  }
1626
1643
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botlearn-course/daemon",
3
- "version": "0.0.26",
3
+ "version": "0.0.27",
4
4
  "description": "Lightweight BotLearn Course daemon: run course tasks on your own machine with your own agent runtime (BYOA).",
5
5
  "type": "module",
6
6
  "bin": {