@botlearn-course/daemon 0.0.21-beta.3 → 0.0.21-beta.4

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.
@@ -53,6 +53,7 @@ export declare class AgentServiceSandboxClient implements RunReportingClient, Pe
53
53
  private socket;
54
54
  private sandboxGeneration;
55
55
  private connectionEpoch;
56
+ private currentLifecycleOperationId;
56
57
  private outboundSeq;
57
58
  private inboundSeq;
58
59
  private activeSessionId;
@@ -102,7 +103,7 @@ export declare class AgentServiceSandboxClient implements RunReportingClient, Pe
102
103
  private queueRuntimeLog;
103
104
  private assertServerFrame;
104
105
  private handleServerFrame;
105
- /** sandbox.sync 只做对账:重放 spool、关闭待关 session、应用 drain/shutdown。 */
106
+ /** sandbox.sync 只做对账:重放 spool、关闭待关 session、应用 shutdown。 */
106
107
  private applySync;
107
108
  private handleSessionOpen;
108
109
  private handleWorkspaceCheckpoint;
@@ -121,6 +122,7 @@ export declare class AgentServiceSandboxClient implements RunReportingClient, Pe
121
122
  private scheduleLifecycle;
122
123
  private currentLifecycleFence;
123
124
  private isCurrentLifecycleFence;
125
+ private isCurrentDrainFence;
124
126
  private runIdsForSession;
125
127
  private deactivateSession;
126
128
  private terminateSessionWriters;
@@ -1,4 +1,5 @@
1
- import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, writeFileSync, } from "node:fs";
1
+ import { randomUUID } from "node:crypto";
2
+ import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
2
3
  import path from "node:path";
3
4
  import { ensureDaemonHome } from "./auth-store.js";
4
5
  import { AGENT_SERVICE_WS_SCHEMA, AGENT_SERVICE_WS_SUBPROTOCOL, createSandboxFrame, createWorkspaceFileChunk, parseSandboxFrame, UnsupportedSandboxProtocolError, WORKSPACE_FILE_READ_BINARY_CAPABILITY, } from "./agent-service-ws-protocol.js";
@@ -386,6 +387,7 @@ export class AgentServiceSandboxClient {
386
387
  socket = null;
387
388
  sandboxGeneration = 0;
388
389
  connectionEpoch = 0;
390
+ currentLifecycleOperationId = null;
389
391
  outboundSeq = 0;
390
392
  inboundSeq = 0;
391
393
  activeSessionId = null;
@@ -644,14 +646,16 @@ export class AgentServiceSandboxClient {
644
646
  this.stop();
645
647
  }
646
648
  async drainWorkspace(fence) {
647
- if (!(await this.dispatcher.drain(10_000)) || !this.isCurrentLifecycleFence(fence)) {
649
+ if (!(await this.dispatcher.drain(10_000)) || !this.isCurrentDrainFence(fence)) {
648
650
  return;
649
651
  }
650
652
  await quiesceRuntimeWriters(null);
651
653
  await flushWorkspaceFilesystem();
652
- if (!this.isCurrentLifecycleFence(fence))
654
+ if (!this.isCurrentDrainFence(fence))
653
655
  return;
654
- await this.sendControlFrame("sandbox.drained", {});
656
+ if (fence.operationId === null)
657
+ throw new Error("sandbox drain operation is missing");
658
+ await this.sendControlFrame("sandbox.drained", { operation_id: fence.operationId });
655
659
  this.stop();
656
660
  }
657
661
  async postEvent(agentRunId, event) {
@@ -1071,8 +1075,13 @@ export class AgentServiceSandboxClient {
1071
1075
  return;
1072
1076
  case "auth.rotate": {
1073
1077
  const token = frame.payload.reconnect_token;
1078
+ const operationId = frame.payload.operation_id;
1074
1079
  if (typeof token !== "string" || !token)
1075
1080
  throw new Error("invalid reconnect token");
1081
+ if (typeof operationId !== "string" || !operationId) {
1082
+ throw new Error("auth.rotate requires operation_id");
1083
+ }
1084
+ this.currentLifecycleOperationId = operationId;
1076
1085
  this.state.reconnectToken = token;
1077
1086
  this.persist();
1078
1087
  await this.sendCommandAck(frame, "ok");
@@ -1098,6 +1107,10 @@ export class AgentServiceSandboxClient {
1098
1107
  await this.stopGracefully();
1099
1108
  return;
1100
1109
  case "sandbox.drain":
1110
+ if (typeof frame.payload.operation_id !== "string" || !frame.payload.operation_id) {
1111
+ throw new Error("sandbox.drain requires operation_id");
1112
+ }
1113
+ this.currentLifecycleOperationId = frame.payload.operation_id;
1101
1114
  await this.sendCommandAck(frame, "ok");
1102
1115
  this.scheduleLifecycle("sandbox.drain", (fence) => this.drainWorkspace(fence));
1103
1116
  return;
@@ -1107,9 +1120,14 @@ export class AgentServiceSandboxClient {
1107
1120
  throw new Error(`unexpected server frame: ${frame.type}`);
1108
1121
  }
1109
1122
  }
1110
- /** sandbox.sync 只做对账:重放 spool、关闭待关 session、应用 drain/shutdown。 */
1123
+ /** sandbox.sync 只做对账:重放 spool、关闭待关 session、应用 shutdown。 */
1111
1124
  async applySync(frame) {
1112
1125
  await this.replaySpool();
1126
+ const operationId = frame.payload.operation_id;
1127
+ if (operationId !== null && operationId !== undefined && typeof operationId !== "string") {
1128
+ throw new Error("sandbox.sync operation_id is invalid");
1129
+ }
1130
+ this.currentLifecycleOperationId = typeof operationId === "string" ? operationId : null;
1113
1131
  const closeSessions = Array.isArray(frame.payload.close_sessions)
1114
1132
  ? frame.payload.close_sessions.filter((item) => typeof item === "string" && item.length > 0)
1115
1133
  : [];
@@ -1124,9 +1142,6 @@ export class AgentServiceSandboxClient {
1124
1142
  await this.stopGracefully();
1125
1143
  return;
1126
1144
  }
1127
- if (state === "drain") {
1128
- this.scheduleLifecycle("sandbox.sync.drain", (fence) => this.drainWorkspace(fence));
1129
- }
1130
1145
  // idle/run/cancel:实际工作全部由显式命令帧下发(合同 §1.3)。
1131
1146
  }
1132
1147
  async handleSessionOpen(frame) {
@@ -1172,6 +1187,18 @@ export class AgentServiceSandboxClient {
1172
1187
  if (isNewSession || session.activationId === null) {
1173
1188
  ensureRuntimeSessionDirectories(sessionId, this.sandboxGeneration, requestedWorkspaceId);
1174
1189
  }
1190
+ const workspaceDirectory = runtimeSessionWorkspaceDir(requestedWorkspaceId, this.sandboxGeneration);
1191
+ const probePath = path.join(workspaceDirectory, `.botlearn-health-${randomUUID()}`);
1192
+ const probeContent = `sandbox=${this.options.sandboxId}\ngeneration=${this.sandboxGeneration}\n`;
1193
+ try {
1194
+ writeFileSync(probePath, probeContent, { encoding: "utf8", mode: 0o600 });
1195
+ if (readFileSync(probePath, "utf8") !== probeContent) {
1196
+ throw new Error("workspace probe content mismatch");
1197
+ }
1198
+ }
1199
+ finally {
1200
+ rmSync(probePath, { force: true });
1201
+ }
1175
1202
  this.state.sessions[sessionId] = session;
1176
1203
  this.persist();
1177
1204
  // NAS 是唯一的 workspace 权威来源,所以 session.opened 不携带 snapshot 物化标记:
@@ -1181,6 +1208,8 @@ export class AgentServiceSandboxClient {
1181
1208
  workspace_ref: session.workspaceRef,
1182
1209
  native_session_id: session.nativeSessionId,
1183
1210
  workspace_authority: "nas",
1211
+ workspace_readable: true,
1212
+ workspace_writable: true,
1184
1213
  });
1185
1214
  }
1186
1215
  async handleWorkspaceCheckpoint(frame) {
@@ -1245,6 +1274,8 @@ export class AgentServiceSandboxClient {
1245
1274
  workspace_ref: session.workspaceRef,
1246
1275
  native_session_id: session.nativeSessionId,
1247
1276
  workspace_authority: "nas",
1277
+ workspace_readable: true,
1278
+ workspace_writable: true,
1248
1279
  });
1249
1280
  await this.sendCommandAck(frame, "ok");
1250
1281
  if (!lifecycleCheckpoint)
@@ -1750,6 +1781,7 @@ export class AgentServiceSandboxClient {
1750
1781
  return {
1751
1782
  sandboxGeneration: this.sandboxGeneration,
1752
1783
  connectionEpoch: this.connectionEpoch,
1784
+ operationId: this.currentLifecycleOperationId,
1753
1785
  socket: this.socket,
1754
1786
  };
1755
1787
  }
@@ -1759,6 +1791,10 @@ export class AgentServiceSandboxClient {
1759
1791
  this.socket === fence.socket &&
1760
1792
  fence.socket?.readyState === WebSocketClient.OPEN;
1761
1793
  }
1794
+ isCurrentDrainFence(fence) {
1795
+ return this.isCurrentLifecycleFence(fence) &&
1796
+ this.currentLifecycleOperationId === fence.operationId;
1797
+ }
1762
1798
  runIdsForSession(sessionId) {
1763
1799
  const runIds = new Set([...this.turnScopes]
1764
1800
  .filter(([, scope]) => scope.sessionId === sessionId)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botlearn-course/daemon",
3
- "version": "0.0.21-beta.3",
3
+ "version": "0.0.21-beta.4",
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": {