@botlearn-course/daemon 0.0.21-beta.4 → 0.0.21-beta.5
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.d.ts +0 -7
- package/dist/agent-service-sandbox.js +8 -142
- package/dist/agent-service-ws-protocol.d.ts +1 -1
- package/dist/agent-service-ws-protocol.js +0 -3
- package/dist/cli.js +0 -50
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -5
- package/dist/runtime-quiescence.d.ts +0 -18
- package/dist/runtime-quiescence.js +0 -101
- package/dist/workspace-filesystem.d.ts +0 -1
- package/dist/workspace-filesystem.js +5 -11
- package/package.json +1 -1
- package/dist/agent-service-client.d.ts +0 -18
- package/dist/agent-service-client.js +0 -90
- package/dist/workspace-entry-set.d.ts +0 -31
- package/dist/workspace-entry-set.js +0 -164
- package/dist/workspace-snapshot-control.d.ts +0 -29
- package/dist/workspace-snapshot-control.js +0 -144
- package/dist/workspace-snapshot-policy.d.ts +0 -24
- package/dist/workspace-snapshot-policy.js +0 -47
- package/dist/workspace-snapshot-staging.d.ts +0 -27
- package/dist/workspace-snapshot-staging.js +0 -275
|
@@ -4,12 +4,6 @@ import { type RuntimeSkillProviderFactory } from "./runtime-skills.js";
|
|
|
4
4
|
import { type PersistentSessionExecution, type PreparedPersistentTurn, type RunReportingClient } from "./run-dispatcher.js";
|
|
5
5
|
import type { CourseRuntime, CourseRuntimeProfile, RunEvent, RunEventReceipt, RunFileCandidate, RunFileRecord, RunStartPayload } from "./types.js";
|
|
6
6
|
export declare function workspaceDurabilityCapabilities(): string[];
|
|
7
|
-
/**
|
|
8
|
-
* NAS 是运行时 workspace 的唯一持久事实来源,committed snapshot 只供前端查看。
|
|
9
|
-
* 任何 restore payload 都意味着对端仍在下发已删除的 S3 回写指令,必须 fail closed,
|
|
10
|
-
* 否则可能用落后的派生快照覆盖 NAS 中较新的内容。
|
|
11
|
-
*/
|
|
12
|
-
export declare function assertWorkspaceRestoreAllowed(requestedRestore: unknown): void;
|
|
13
7
|
export interface AgentServiceSandboxOptions {
|
|
14
8
|
wsUrl: string;
|
|
15
9
|
sandboxId: string;
|
|
@@ -106,7 +100,6 @@ export declare class AgentServiceSandboxClient implements RunReportingClient, Pe
|
|
|
106
100
|
/** sandbox.sync 只做对账:重放 spool、关闭待关 session、应用 shutdown。 */
|
|
107
101
|
private applySync;
|
|
108
102
|
private handleSessionOpen;
|
|
109
|
-
private handleWorkspaceCheckpoint;
|
|
110
103
|
private handleSessionActivate;
|
|
111
104
|
private handleTurnStart;
|
|
112
105
|
private handleTurnCancel;
|
|
@@ -11,11 +11,7 @@ import { parseRuntimeSkillProviderGrantSet, prepareRuntimeSkillProvider, Runtime
|
|
|
11
11
|
import { RunDispatcher, } from "./run-dispatcher.js";
|
|
12
12
|
import { ensureRuntimeSessionDirectories, ensureRuntimeSessionWorkspace, exposeRuntimeSessionWorkspace, removeRuntimeSessionWorkspace, revokeRuntimeSessionWorkspace, runtimeSessionWorkspaceDir, } from "./workspace.js";
|
|
13
13
|
import { readWorkspaceFile, WorkspaceFileReadError } from "./workspace-file-read.js";
|
|
14
|
-
import {
|
|
15
|
-
import { assertWorkspaceWriterFreezeSupported, freezeRuntimeWriters, issueWorkspaceWriterFreezeProof, quiesceRuntimeWriters, thawRuntimeWriters, } from "./runtime-quiescence.js";
|
|
16
|
-
import { WorkspaceSnapshotStagingError } from "./workspace-snapshot-staging.js";
|
|
17
|
-
import { WorkspaceEntrySetError, } from "./workspace-entry-set.js";
|
|
18
|
-
import { workspaceSnapshotPolicyCapability, } from "./workspace-snapshot-policy.js";
|
|
14
|
+
import { quiesceRuntimeWriters, } from "./runtime-quiescence.js";
|
|
19
15
|
import { assertWorkspaceFilesystem, flushWorkspaceFilesystem, } from "./workspace-filesystem.js";
|
|
20
16
|
import { WebSocketClient, } from "./websocket-client.js";
|
|
21
17
|
const RECONNECT_DELAYS_MS = [1_000, 2_000, 4_000, 8_000, 16_000, 30_000];
|
|
@@ -27,33 +23,10 @@ const INPUT_ATTACHMENT_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-
|
|
|
27
23
|
const ASSET_ACTIVATION_ID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
28
24
|
/** 出站 seq 基址:seq = connection_epoch * SEQ_EPOCH_BASE + n,跨重连单调(合同 §1.1)。 */
|
|
29
25
|
const SEQ_EPOCH_BASE = 1_000_000_000;
|
|
30
|
-
function agentServiceHttpBase(wsUrl) {
|
|
31
|
-
const url = new URL(wsUrl);
|
|
32
|
-
url.protocol = url.protocol === "wss:" ? "https:" : "http:";
|
|
33
|
-
url.pathname = "/internal/v1";
|
|
34
|
-
url.search = "";
|
|
35
|
-
url.hash = "";
|
|
36
|
-
return url.toString().replace(/\/$/, "");
|
|
37
|
-
}
|
|
38
26
|
const RUNTIME_LOG_WINDOW_MS = 60_000;
|
|
39
27
|
const WORKSPACE_FILE_TRANSFER_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
40
28
|
export function workspaceDurabilityCapabilities() {
|
|
41
|
-
return [
|
|
42
|
-
"workspace_writer_freeze_v1",
|
|
43
|
-
"workspace_snapshot_v2",
|
|
44
|
-
"workspace_nas_v1",
|
|
45
|
-
"workspace_identity_v1",
|
|
46
|
-
];
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* NAS 是运行时 workspace 的唯一持久事实来源,committed snapshot 只供前端查看。
|
|
50
|
-
* 任何 restore payload 都意味着对端仍在下发已删除的 S3 回写指令,必须 fail closed,
|
|
51
|
-
* 否则可能用落后的派生快照覆盖 NAS 中较新的内容。
|
|
52
|
-
*/
|
|
53
|
-
export function assertWorkspaceRestoreAllowed(requestedRestore) {
|
|
54
|
-
if (requestedRestore !== null && requestedRestore !== undefined) {
|
|
55
|
-
throw new Error("workspace_restore_forbidden_for_nas");
|
|
56
|
-
}
|
|
29
|
+
return ["workspace_nas_v1", "workspace_identity_v1"];
|
|
57
30
|
}
|
|
58
31
|
const DISABLED_RUNTIME_LOG_POLICY = {
|
|
59
32
|
enabled: false,
|
|
@@ -591,7 +564,7 @@ export class AgentServiceSandboxClient {
|
|
|
591
564
|
}
|
|
592
565
|
catch (error) {
|
|
593
566
|
const close = error instanceof SandboxClosedError ? error : null;
|
|
594
|
-
if (close?.code === 4403 || close?.code === 4410) {
|
|
567
|
+
if (close?.code === 4403 || close?.code === 4409 || close?.code === 4410) {
|
|
595
568
|
this.permanentFailure = true;
|
|
596
569
|
break;
|
|
597
570
|
}
|
|
@@ -620,7 +593,7 @@ export class AgentServiceSandboxClient {
|
|
|
620
593
|
}
|
|
621
594
|
this.removeOperationalLogSink();
|
|
622
595
|
if (this.permanentFailure) {
|
|
623
|
-
throw new Error("Agent Service sandbox stopped after a permanent
|
|
596
|
+
throw new Error("Agent Service sandbox stopped after a permanent connection failure");
|
|
624
597
|
}
|
|
625
598
|
}
|
|
626
599
|
stop() {
|
|
@@ -668,8 +641,7 @@ export class AgentServiceSandboxClient {
|
|
|
668
641
|
if (!this.sandboxGeneration || !this.connectionEpoch) {
|
|
669
642
|
throw new Error("Agent Service sandbox is not authenticated");
|
|
670
643
|
}
|
|
671
|
-
// Terminal truth
|
|
672
|
-
// is a separate lifecycle command issued only for pause/cleanup/rotation.
|
|
644
|
+
// Terminal truth remains an ordering barrier after earlier transient blocks.
|
|
673
645
|
if (event.type === "run.block") {
|
|
674
646
|
await this.waitForEventCapacity();
|
|
675
647
|
}
|
|
@@ -907,14 +879,12 @@ export class AgentServiceSandboxClient {
|
|
|
907
879
|
this.runtimeLogPolicy = runtimeLogPolicy(frame.payload.runtime_log_policy);
|
|
908
880
|
this.persist();
|
|
909
881
|
assertWorkspaceFilesystem(process.env.BOTLEARN_AGENT_SERVICE_WORKSPACE_ROOT ?? "");
|
|
910
|
-
assertWorkspaceWriterFreezeSupported();
|
|
911
882
|
await this.sendControlFrame("sandbox.ready", {
|
|
912
883
|
protocol_versions: [AGENT_SERVICE_WS_SCHEMA],
|
|
913
884
|
capabilities: [
|
|
914
885
|
WORKSPACE_FILE_READ_BINARY_CAPABILITY,
|
|
915
886
|
...workspaceDurabilityCapabilities(),
|
|
916
887
|
],
|
|
917
|
-
workspace_snapshot_policy: workspaceSnapshotPolicyCapability(),
|
|
918
888
|
daemon_version: this.options.daemonVersion,
|
|
919
889
|
runtime_versions: {
|
|
920
890
|
course_daemon: this.options.daemonVersion,
|
|
@@ -1096,13 +1066,6 @@ export class AgentServiceSandboxClient {
|
|
|
1096
1066
|
});
|
|
1097
1067
|
});
|
|
1098
1068
|
return;
|
|
1099
|
-
case "workspace.checkpoint":
|
|
1100
|
-
this.scheduleLifecycle("workspace.checkpoint", () => this.handleWorkspaceCheckpoint(frame));
|
|
1101
|
-
return;
|
|
1102
|
-
case "workspace.thaw":
|
|
1103
|
-
await thawRuntimeWriters();
|
|
1104
|
-
await this.sendCommandAck(frame, "ok");
|
|
1105
|
-
return;
|
|
1106
1069
|
case "sandbox.shutdown":
|
|
1107
1070
|
await this.stopGracefully();
|
|
1108
1071
|
return;
|
|
@@ -1135,9 +1098,6 @@ export class AgentServiceSandboxClient {
|
|
|
1135
1098
|
this.scheduleLifecycle("sandbox.sync.close", (fence) => this.closeSession(sessionId, "sync_close", frame, fence));
|
|
1136
1099
|
}
|
|
1137
1100
|
const state = frame.payload.state;
|
|
1138
|
-
if (frame.payload.thaw_runtime_writers === true) {
|
|
1139
|
-
await thawRuntimeWriters();
|
|
1140
|
-
}
|
|
1141
1101
|
if (state === "shutdown") {
|
|
1142
1102
|
await this.stopGracefully();
|
|
1143
1103
|
return;
|
|
@@ -1148,16 +1108,11 @@ export class AgentServiceSandboxClient {
|
|
|
1148
1108
|
const sessionId = frame.runtime_session_id;
|
|
1149
1109
|
const session = this.state.sessions[sessionId] ?? emptySessionState();
|
|
1150
1110
|
const courseRunId = frame.payload.course_run_id;
|
|
1151
|
-
|
|
1152
|
-
// Its physical directory was keyed by RuntimeSession id, which is the exact backfill
|
|
1153
|
-
// identity used by the database migration.
|
|
1154
|
-
const requestedWorkspaceId = typeof frame.payload.workspace_id === "string"
|
|
1155
|
-
? frame.payload.workspace_id
|
|
1156
|
-
: session.workspaceId ?? sessionId;
|
|
1111
|
+
const requestedWorkspaceId = frame.payload.workspace_id;
|
|
1157
1112
|
const requestedWorkspaceRef = frame.payload.workspace_ref;
|
|
1158
|
-
assertWorkspaceRestoreAllowed(frame.payload.workspace_restore);
|
|
1159
1113
|
if (typeof courseRunId !== "string" ||
|
|
1160
1114
|
courseRunId.length < 1 ||
|
|
1115
|
+
typeof requestedWorkspaceId !== "string" ||
|
|
1161
1116
|
requestedWorkspaceId.length < 1 ||
|
|
1162
1117
|
(requestedWorkspaceRef !== null &&
|
|
1163
1118
|
requestedWorkspaceRef !== undefined &&
|
|
@@ -1201,104 +1156,15 @@ export class AgentServiceSandboxClient {
|
|
|
1201
1156
|
}
|
|
1202
1157
|
this.state.sessions[sessionId] = session;
|
|
1203
1158
|
this.persist();
|
|
1204
|
-
// NAS 是唯一的 workspace
|
|
1205
|
-
// 挂载证明只说明当前 generation 已挂上 NAS,不要求运行目录等于某个 committed revision。
|
|
1159
|
+
// NAS 是唯一的 workspace 权威来源。挂载证明只说明当前 generation 已挂上 NAS。
|
|
1206
1160
|
await this.sendSessionFrame("session.opened", sessionId, {
|
|
1207
1161
|
workspace_id: session.workspaceId,
|
|
1208
1162
|
workspace_ref: session.workspaceRef,
|
|
1209
1163
|
native_session_id: session.nativeSessionId,
|
|
1210
|
-
workspace_authority: "nas",
|
|
1211
1164
|
workspace_readable: true,
|
|
1212
1165
|
workspace_writable: true,
|
|
1213
1166
|
});
|
|
1214
1167
|
}
|
|
1215
|
-
async handleWorkspaceCheckpoint(frame) {
|
|
1216
|
-
const sessionId = frame.runtime_session_id;
|
|
1217
|
-
const session = this.state.sessions[sessionId];
|
|
1218
|
-
const checkpointId = frame.payload.checkpoint_id;
|
|
1219
|
-
const controlToken = frame.payload.control_token;
|
|
1220
|
-
const baseRevision = Number(frame.payload.base_revision);
|
|
1221
|
-
const sourceKind = frame.payload.source_kind;
|
|
1222
|
-
if (!session ||
|
|
1223
|
-
this.currentTurnSessionId === sessionId ||
|
|
1224
|
-
typeof checkpointId !== "string" ||
|
|
1225
|
-
typeof controlToken !== "string" ||
|
|
1226
|
-
controlToken.length < 32 ||
|
|
1227
|
-
frame.payload.policy_id !== "WorkspaceSnapshotPolicyV1" ||
|
|
1228
|
-
![
|
|
1229
|
-
"sandbox_pause",
|
|
1230
|
-
"sandbox_cleanup",
|
|
1231
|
-
"generation_rotation",
|
|
1232
|
-
"maintenance_repair",
|
|
1233
|
-
].includes(String(sourceKind)) ||
|
|
1234
|
-
!Number.isSafeInteger(baseRevision) ||
|
|
1235
|
-
baseRevision < 0 ||
|
|
1236
|
-
frame.payload.agent_run_id != null ||
|
|
1237
|
-
frame.payload.worker_attempt != null ||
|
|
1238
|
-
frame.payload.activation_id != null) {
|
|
1239
|
-
await this.sendCommandAck(frame, "rejected", "invalid_workspace_checkpoint");
|
|
1240
|
-
return;
|
|
1241
|
-
}
|
|
1242
|
-
const lifecycleCheckpoint = [
|
|
1243
|
-
"sandbox_pause",
|
|
1244
|
-
"sandbox_cleanup",
|
|
1245
|
-
"generation_rotation",
|
|
1246
|
-
].includes(String(sourceKind));
|
|
1247
|
-
let checkpointCommitted = false;
|
|
1248
|
-
try {
|
|
1249
|
-
await freezeRuntimeWriters();
|
|
1250
|
-
const freezeProof = issueWorkspaceWriterFreezeProof({
|
|
1251
|
-
sandboxId: this.options.sandboxId,
|
|
1252
|
-
sandboxGeneration: this.sandboxGeneration,
|
|
1253
|
-
runtimeSessionId: sessionId,
|
|
1254
|
-
checkpointId,
|
|
1255
|
-
});
|
|
1256
|
-
await checkpointWorkspace({
|
|
1257
|
-
wsUrl: this.options.wsUrl,
|
|
1258
|
-
reconnectToken: controlToken,
|
|
1259
|
-
workspaceDirectory: runtimeSessionWorkspaceDir(session.workspaceId ?? sessionId, this.sandboxGeneration),
|
|
1260
|
-
scope: {
|
|
1261
|
-
checkpointId,
|
|
1262
|
-
baseRevision,
|
|
1263
|
-
sandboxId: this.options.sandboxId,
|
|
1264
|
-
sandboxGeneration: this.sandboxGeneration,
|
|
1265
|
-
connectionEpoch: this.connectionEpoch,
|
|
1266
|
-
runtimeSessionId: sessionId,
|
|
1267
|
-
},
|
|
1268
|
-
freezeProof,
|
|
1269
|
-
});
|
|
1270
|
-
checkpointCommitted = true;
|
|
1271
|
-
// committed revision 只是派生投影,不改变运行目录,所以这里不上报物化标记。
|
|
1272
|
-
await this.sendSessionFrame("session.opened", sessionId, {
|
|
1273
|
-
workspace_id: session.workspaceId,
|
|
1274
|
-
workspace_ref: session.workspaceRef,
|
|
1275
|
-
native_session_id: session.nativeSessionId,
|
|
1276
|
-
workspace_authority: "nas",
|
|
1277
|
-
workspace_readable: true,
|
|
1278
|
-
workspace_writable: true,
|
|
1279
|
-
});
|
|
1280
|
-
await this.sendCommandAck(frame, "ok");
|
|
1281
|
-
if (!lifecycleCheckpoint)
|
|
1282
|
-
await thawRuntimeWriters();
|
|
1283
|
-
}
|
|
1284
|
-
catch (error) {
|
|
1285
|
-
if (!lifecycleCheckpoint || !checkpointCommitted) {
|
|
1286
|
-
await thawRuntimeWriters().catch(() => undefined);
|
|
1287
|
-
}
|
|
1288
|
-
this.log.error("workspace baseline checkpoint failed", {
|
|
1289
|
-
sandboxId: this.options.sandboxId,
|
|
1290
|
-
runtimeSessionId: sessionId,
|
|
1291
|
-
error: error instanceof Error ? redactSecretString(error.message) : "unexpected",
|
|
1292
|
-
});
|
|
1293
|
-
const deterministic = (error instanceof WorkspaceSnapshotStagingError && !error.retryable) ||
|
|
1294
|
-
error instanceof WorkspaceEntrySetError ||
|
|
1295
|
-
(error instanceof WorkspaceSnapshotControlError && !error.retryable);
|
|
1296
|
-
const errorCode = deterministic && "code" in error
|
|
1297
|
-
? String(error.code)
|
|
1298
|
-
: "workspace_checkpoint_retryable";
|
|
1299
|
-
await this.sendCommandAck(frame, "rejected", errorCode, !deterministic);
|
|
1300
|
-
}
|
|
1301
|
-
}
|
|
1302
1168
|
async handleSessionActivate(frame, fence) {
|
|
1303
1169
|
const sessionId = frame.runtime_session_id;
|
|
1304
1170
|
const activationId = frame.activation_id;
|
|
@@ -3,7 +3,7 @@ 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
4
|
export declare const WORKSPACE_FILE_CHUNK_BYTES: number;
|
|
5
5
|
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" | "
|
|
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
7
|
export declare class UnsupportedSandboxProtocolError extends Error {
|
|
8
8
|
readonly schemaVersion: unknown;
|
|
9
9
|
constructor(schemaVersion: unknown);
|
|
@@ -18,8 +18,6 @@ const FRAME_TYPES = new Set([
|
|
|
18
18
|
"event.ack",
|
|
19
19
|
"auth.rotate",
|
|
20
20
|
"workspace.file.read",
|
|
21
|
-
"workspace.checkpoint",
|
|
22
|
-
"workspace.thaw",
|
|
23
21
|
"ping",
|
|
24
22
|
"sandbox.ready",
|
|
25
23
|
"sandbox.heartbeat",
|
|
@@ -45,7 +43,6 @@ const SESSION_TYPES = new Set([
|
|
|
45
43
|
"session.open_failed",
|
|
46
44
|
"session.closed",
|
|
47
45
|
"workspace.file.read",
|
|
48
|
-
"workspace.checkpoint",
|
|
49
46
|
"workspace.file.result",
|
|
50
47
|
]);
|
|
51
48
|
/** TURN 帧:必须带 runtime_session_id + agent_run_id + worker_attempt + activation_id 四元组。 */
|
package/dist/cli.js
CHANGED
|
@@ -4,7 +4,6 @@ import { createRequire } from "node:module";
|
|
|
4
4
|
import { hostname } from "node:os";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
import { authFilePath, clearAuth, machineFingerprint, readAuth, writeAuth } from "./auth-store.js";
|
|
7
|
-
import { AgentServiceRunClient } from "./agent-service-client.js";
|
|
8
7
|
import { AgentServiceSandboxClient } from "./agent-service-sandbox.js";
|
|
9
8
|
import { CourseClient, isAuthFailure, loginCourseDaemon } from "./course-client.js";
|
|
10
9
|
import { runCourseDoctor } from "./doctor.js";
|
|
@@ -23,7 +22,6 @@ Usage:
|
|
|
23
22
|
botlearn-course-daemon course start [--once] [--poll-interval-ms <ms>]
|
|
24
23
|
botlearn-course-daemon course logout
|
|
25
24
|
botlearn-course-daemon course doctor
|
|
26
|
-
botlearn-course-daemon agent-service run
|
|
27
25
|
botlearn-course-daemon agent-service session
|
|
28
26
|
botlearn-course-daemon --help | --version
|
|
29
27
|
|
|
@@ -43,9 +41,6 @@ Environment:
|
|
|
43
41
|
|
|
44
42
|
Agent Service sandbox environment:
|
|
45
43
|
BOTLEARN_COURSE_API_URL
|
|
46
|
-
BOTLEARN_AGENT_SERVICE_RUN_ID
|
|
47
|
-
BOTLEARN_AGENT_SERVICE_RUN_TOKEN
|
|
48
|
-
BOTLEARN_AGENT_SERVICE_WORKER_ID
|
|
49
44
|
BOTLEARN_AGENT_SERVICE_WS_URL
|
|
50
45
|
BOTLEARN_AGENT_SERVICE_SANDBOX_ID
|
|
51
46
|
BOTLEARN_AGENT_SERVICE_SANDBOX_TOKEN`;
|
|
@@ -252,48 +247,6 @@ async function cmdDoctor() {
|
|
|
252
247
|
// doctor 是诊断不是校验:即使 0 个 runtime 可用也 exit 0。
|
|
253
248
|
return 0;
|
|
254
249
|
}
|
|
255
|
-
async function cmdAgentServiceRun() {
|
|
256
|
-
const courseApiUrl = process.env.BOTLEARN_COURSE_API_URL;
|
|
257
|
-
const agentRunId = process.env.BOTLEARN_AGENT_SERVICE_RUN_ID;
|
|
258
|
-
const runToken = process.env.BOTLEARN_AGENT_SERVICE_RUN_TOKEN;
|
|
259
|
-
const workerId = process.env.BOTLEARN_AGENT_SERVICE_WORKER_ID;
|
|
260
|
-
if (!courseApiUrl || !agentRunId || !runToken || !workerId) {
|
|
261
|
-
console.error("Agent Service sandbox environment is incomplete");
|
|
262
|
-
return 1;
|
|
263
|
-
}
|
|
264
|
-
augmentProcessPath();
|
|
265
|
-
const client = new AgentServiceRunClient(courseApiUrl, agentRunId, runToken, workerId);
|
|
266
|
-
const payload = await client.getRun();
|
|
267
|
-
if (payload.agent_run_id !== agentRunId) {
|
|
268
|
-
throw new Error("Agent Service run payload does not match requested run");
|
|
269
|
-
}
|
|
270
|
-
clearAgentServiceControlEnv();
|
|
271
|
-
const runtimes = new Map();
|
|
272
|
-
for (const mod of RUNTIME_MODULES) {
|
|
273
|
-
if (mod.hidden && !fakeRuntimeEnabled())
|
|
274
|
-
continue;
|
|
275
|
-
runtimes.set(mod.id, mod.create());
|
|
276
|
-
}
|
|
277
|
-
const dispatcher = new RunDispatcher(client, runtimes);
|
|
278
|
-
const heartbeat = setInterval(() => {
|
|
279
|
-
void client.heartbeat().catch((err) => {
|
|
280
|
-
log.warn("Agent Service heartbeat failed", {
|
|
281
|
-
agentRunId,
|
|
282
|
-
error: err instanceof Error ? redactSecretString(err.message) : String(err),
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
}, 20_000);
|
|
286
|
-
if (typeof heartbeat.unref === "function")
|
|
287
|
-
heartbeat.unref();
|
|
288
|
-
try {
|
|
289
|
-
await client.heartbeat();
|
|
290
|
-
await dispatcher.dispatch(payload);
|
|
291
|
-
}
|
|
292
|
-
finally {
|
|
293
|
-
clearInterval(heartbeat);
|
|
294
|
-
}
|
|
295
|
-
return 0;
|
|
296
|
-
}
|
|
297
250
|
async function readSandboxBootstrapFromStdin() {
|
|
298
251
|
const chunks = [];
|
|
299
252
|
let size = 0;
|
|
@@ -422,9 +375,6 @@ export async function runCli(argv) {
|
|
|
422
375
|
return 1;
|
|
423
376
|
}
|
|
424
377
|
}
|
|
425
|
-
if (command === "agent-service" && sub === "run") {
|
|
426
|
-
return cmdAgentServiceRun();
|
|
427
|
-
}
|
|
428
378
|
if (command === "agent-service" && sub === "session") {
|
|
429
379
|
return cmdAgentServiceSession(args);
|
|
430
380
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,16 +5,11 @@
|
|
|
5
5
|
export * from "./types.js";
|
|
6
6
|
export * from "./auth-store.js";
|
|
7
7
|
export * from "./course-client.js";
|
|
8
|
-
export * from "./agent-service-client.js";
|
|
9
8
|
export * from "./agent-service-sandbox.js";
|
|
10
9
|
export * from "./agent-service-ws-protocol.js";
|
|
11
10
|
export * from "./run-dispatcher.js";
|
|
12
11
|
export * from "./run-queue.js";
|
|
13
12
|
export * from "./workspace.js";
|
|
14
|
-
export * from "./workspace-entry-set.js";
|
|
15
|
-
export * from "./workspace-snapshot-staging.js";
|
|
16
|
-
export * from "./workspace-snapshot-policy.js";
|
|
17
|
-
export * from "./workspace-snapshot-control.js";
|
|
18
13
|
export * from "./transcript.js";
|
|
19
14
|
export * from "./file-candidates.js";
|
|
20
15
|
export * from "./doctor.js";
|
package/dist/index.js
CHANGED
|
@@ -5,16 +5,11 @@
|
|
|
5
5
|
export * from "./types.js";
|
|
6
6
|
export * from "./auth-store.js";
|
|
7
7
|
export * from "./course-client.js";
|
|
8
|
-
export * from "./agent-service-client.js";
|
|
9
8
|
export * from "./agent-service-sandbox.js";
|
|
10
9
|
export * from "./agent-service-ws-protocol.js";
|
|
11
10
|
export * from "./run-dispatcher.js";
|
|
12
11
|
export * from "./run-queue.js";
|
|
13
12
|
export * from "./workspace.js";
|
|
14
|
-
export * from "./workspace-entry-set.js";
|
|
15
|
-
export * from "./workspace-snapshot-staging.js";
|
|
16
|
-
export * from "./workspace-snapshot-policy.js";
|
|
17
|
-
export * from "./workspace-snapshot-control.js";
|
|
18
13
|
export * from "./transcript.js";
|
|
19
14
|
export * from "./file-candidates.js";
|
|
20
15
|
export * from "./doctor.js";
|
|
@@ -1,19 +1 @@
|
|
|
1
|
-
export interface WorkspaceWriterFreezeProof {
|
|
2
|
-
readonly proofVersion: "workspace-writer-freeze-proof/1";
|
|
3
|
-
readonly sandboxId: string;
|
|
4
|
-
readonly sandboxGeneration: number;
|
|
5
|
-
readonly runtimeSessionId: string;
|
|
6
|
-
readonly checkpointId: string;
|
|
7
|
-
readonly allWritersFrozen: true;
|
|
8
|
-
}
|
|
9
|
-
export declare function freezeRuntimeWriters(): Promise<void>;
|
|
10
|
-
export declare function thawRuntimeWriters(): Promise<void>;
|
|
11
|
-
export declare function assertWorkspaceWriterFreezeSupported(): void;
|
|
12
1
|
export declare function quiesceRuntimeWriters(activationId: string | null): Promise<void>;
|
|
13
|
-
export declare function issueWorkspaceWriterFreezeProof(scope: {
|
|
14
|
-
sandboxId: string;
|
|
15
|
-
sandboxGeneration: number;
|
|
16
|
-
runtimeSessionId: string;
|
|
17
|
-
checkpointId: string;
|
|
18
|
-
}): WorkspaceWriterFreezeProof;
|
|
19
|
-
export declare function isWorkspaceWriterFreezeProof(value: object): value is WorkspaceWriterFreezeProof;
|
|
@@ -1,96 +1,7 @@
|
|
|
1
1
|
import { execFile } from "node:child_process";
|
|
2
|
-
import { accessSync, constants as fsConstants } from "node:fs";
|
|
3
|
-
import { readFile, readdir, writeFile } from "node:fs/promises";
|
|
4
|
-
import path from "node:path";
|
|
5
2
|
import { promisify } from "node:util";
|
|
6
3
|
const execFileAsync = promisify(execFile);
|
|
7
4
|
const RUNTIME_LAUNCHER = "/opt/botlearn/bin/botlearn-runtime-launcher";
|
|
8
|
-
const RUNTIME_CGROUP_ROOT = "/sys/fs/cgroup/botlearn-runtime";
|
|
9
|
-
const RUNTIME_CGROUP_UNAVAILABLE = "/run/botlearn-runtime-cgroup-unavailable";
|
|
10
|
-
const proofs = new WeakSet();
|
|
11
|
-
const sleep = (milliseconds) => new Promise((resolve) => {
|
|
12
|
-
setTimeout(resolve, milliseconds);
|
|
13
|
-
});
|
|
14
|
-
async function setDirectCgroupsFrozen(frozen) {
|
|
15
|
-
try {
|
|
16
|
-
accessSync(RUNTIME_CGROUP_UNAVAILABLE, fsConstants.F_OK);
|
|
17
|
-
await execFileAsync(RUNTIME_LAUNCHER, [frozen ? "--freeze-all" : "--thaw-all"], {
|
|
18
|
-
timeout: 10_000,
|
|
19
|
-
});
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
if (error.code !== "ENOENT")
|
|
24
|
-
throw error;
|
|
25
|
-
}
|
|
26
|
-
let entries;
|
|
27
|
-
try {
|
|
28
|
-
entries = await readdir(RUNTIME_CGROUP_ROOT, { withFileTypes: true });
|
|
29
|
-
}
|
|
30
|
-
catch (error) {
|
|
31
|
-
if (error.code === "ENOENT")
|
|
32
|
-
return;
|
|
33
|
-
throw error;
|
|
34
|
-
}
|
|
35
|
-
for (const entry of entries) {
|
|
36
|
-
if (!entry.isDirectory())
|
|
37
|
-
continue;
|
|
38
|
-
const directory = path.join(RUNTIME_CGROUP_ROOT, entry.name);
|
|
39
|
-
await writeFile(path.join(directory, "cgroup.freeze"), frozen ? "1" : "0");
|
|
40
|
-
for (let attempt = 0; attempt < 100; attempt += 1) {
|
|
41
|
-
const events = await readFile(path.join(directory, "cgroup.events"), "utf8");
|
|
42
|
-
if (events.includes(`frozen ${frozen ? 1 : 0}`))
|
|
43
|
-
break;
|
|
44
|
-
if (attempt === 99)
|
|
45
|
-
throw new Error("workspace_runtime_freeze_timeout");
|
|
46
|
-
await sleep(50);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
async function setRuntimeWritersFrozen(frozen) {
|
|
51
|
-
const mode = process.env.BOTLEARN_RUNTIME_LAUNCH_MODE;
|
|
52
|
-
if (mode === "direct-uid") {
|
|
53
|
-
await setDirectCgroupsFrozen(frozen);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
if (mode === "sudo") {
|
|
57
|
-
await execFileAsync("/usr/bin/sudo", ["-n", "--", RUNTIME_LAUNCHER, frozen ? "--freeze-all" : "--thaw-all"], { timeout: 10_000 });
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
if (process.env.BOTLEARN_DAEMON_ENABLE_FAKE_RUNTIME === "1" || process.env.NODE_ENV === "test") {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
throw new Error("workspace_runtime_cgroup_unavailable");
|
|
64
|
-
}
|
|
65
|
-
export async function freezeRuntimeWriters() {
|
|
66
|
-
await setRuntimeWritersFrozen(true);
|
|
67
|
-
}
|
|
68
|
-
export async function thawRuntimeWriters() {
|
|
69
|
-
await setRuntimeWritersFrozen(false);
|
|
70
|
-
}
|
|
71
|
-
export function assertWorkspaceWriterFreezeSupported() {
|
|
72
|
-
const mode = process.env.BOTLEARN_RUNTIME_LAUNCH_MODE;
|
|
73
|
-
if (mode === "direct-uid") {
|
|
74
|
-
try {
|
|
75
|
-
accessSync(RUNTIME_CGROUP_UNAVAILABLE, fsConstants.F_OK);
|
|
76
|
-
accessSync(RUNTIME_LAUNCHER, fsConstants.X_OK);
|
|
77
|
-
}
|
|
78
|
-
catch (error) {
|
|
79
|
-
if (error.code !== "ENOENT")
|
|
80
|
-
throw error;
|
|
81
|
-
accessSync(RUNTIME_CGROUP_ROOT, fsConstants.R_OK | fsConstants.W_OK);
|
|
82
|
-
}
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
if (mode === "sudo") {
|
|
86
|
-
accessSync(RUNTIME_LAUNCHER, fsConstants.X_OK);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
if (process.env.BOTLEARN_DAEMON_ENABLE_FAKE_RUNTIME === "1" || process.env.NODE_ENV === "test") {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
throw new Error("workspace_runtime_cgroup_unavailable");
|
|
93
|
-
}
|
|
94
5
|
function managedQuiescenceCommand(activationId) {
|
|
95
6
|
const mode = process.env.BOTLEARN_RUNTIME_LAUNCH_MODE;
|
|
96
7
|
if (mode === "direct-uid") {
|
|
@@ -116,15 +27,3 @@ export async function quiesceRuntimeWriters(activationId) {
|
|
|
116
27
|
}
|
|
117
28
|
await execFileAsync(command[0], command[1], { timeout: 10_000 });
|
|
118
29
|
}
|
|
119
|
-
export function issueWorkspaceWriterFreezeProof(scope) {
|
|
120
|
-
const proof = Object.freeze({
|
|
121
|
-
proofVersion: "workspace-writer-freeze-proof/1",
|
|
122
|
-
...scope,
|
|
123
|
-
allWritersFrozen: true,
|
|
124
|
-
});
|
|
125
|
-
proofs.add(proof);
|
|
126
|
-
return proof;
|
|
127
|
-
}
|
|
128
|
-
export function isWorkspaceWriterFreezeProof(value) {
|
|
129
|
-
return proofs.has(value);
|
|
130
|
-
}
|
|
@@ -8,7 +8,6 @@ interface MountInfoEntry {
|
|
|
8
8
|
superOptions: Set<string>;
|
|
9
9
|
}
|
|
10
10
|
export interface WorkspaceCapacityProbes {
|
|
11
|
-
statfsBytes(workspaceRoot: string): bigint;
|
|
12
11
|
backingFileBytes(imagePath: string): bigint;
|
|
13
12
|
}
|
|
14
13
|
export declare function parseWorkspaceMountInfo(text: string): MountInfoEntry[];
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import { execFile } from "node:child_process";
|
|
2
|
-
import { readFileSync,
|
|
2
|
+
import { readFileSync, statSync } from "node:fs";
|
|
3
3
|
import { promisify } from "node:util";
|
|
4
4
|
export const WORKSPACE_IMAGE_FORMAT = "botlearn-workspace-image/1";
|
|
5
5
|
export const WORKSPACE_IMAGE_LOGICAL_BYTES = 3 * 1024 * 1024 * 1024;
|
|
6
6
|
const RUNTIME_LAUNCHER = "/opt/botlearn/bin/botlearn-runtime-launcher";
|
|
7
7
|
const execFileAsync = promisify(execFile);
|
|
8
8
|
const defaultCapacityProbes = {
|
|
9
|
-
statfsBytes(workspaceRoot) {
|
|
10
|
-
const filesystem = statfsSync(workspaceRoot, { bigint: true });
|
|
11
|
-
return filesystem.blocks * filesystem.bsize;
|
|
12
|
-
},
|
|
13
9
|
backingFileBytes(imagePath) {
|
|
14
10
|
return BigInt(statSync(imagePath).size);
|
|
15
11
|
},
|
|
@@ -85,15 +81,15 @@ export function assertWorkspaceFilesystem(workspaceRoot, env = process.env, moun
|
|
|
85
81
|
return;
|
|
86
82
|
const mount = parseWorkspaceMountInfo(mountInfo ?? readFileSync("/proc/self/mountinfo", "utf8"))
|
|
87
83
|
.find((candidate) => candidate.target === workspaceRoot);
|
|
88
|
-
const validFilesystem = mount?.filesystemType === "
|
|
84
|
+
const validFilesystem = mount?.filesystemType === "fuse.ext4"
|
|
89
85
|
&& mount.source === "/mnt/botlearn-workspace-store/workspace.ext4"
|
|
90
|
-
&& mount.superOptions.has("allow_other")
|
|
86
|
+
&& mount.superOptions.has("allow_other");
|
|
91
87
|
if (!mount
|
|
92
88
|
|| !validFilesystem
|
|
93
89
|
|| !mount.mountOptions.has("rw")
|
|
94
90
|
|| !mount.mountOptions.has("nodev")
|
|
95
91
|
|| !mount.mountOptions.has("nosuid")) {
|
|
96
|
-
throw new Error("
|
|
92
|
+
throw new Error("workspace_nas_mount_failed");
|
|
97
93
|
}
|
|
98
94
|
if (env.NODE_ENV === "test" && capacityProbes === undefined)
|
|
99
95
|
return;
|
|
@@ -101,9 +97,7 @@ export function assertWorkspaceFilesystem(workspaceRoot, env = process.env, moun
|
|
|
101
97
|
// fuse2fs reports a zero-sized statfs view in veFaaS even though the exact backing
|
|
102
98
|
// image is mounted and enforces the filesystem boundary. The mount proof above pins
|
|
103
99
|
// that image, so its validated file size is the authoritative capacity for FUSE.
|
|
104
|
-
const boundedBytes = mount.
|
|
105
|
-
? probes.backingFileBytes(mount.source)
|
|
106
|
-
: probes.statfsBytes(workspaceRoot);
|
|
100
|
+
const boundedBytes = probes.backingFileBytes(mount.source);
|
|
107
101
|
if (boundedBytes !== BigInt(WORKSPACE_IMAGE_LOGICAL_BYTES)) {
|
|
108
102
|
throw new Error("workspace_quota_mismatch");
|
|
109
103
|
}
|
package/package.json
CHANGED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { CourseRuntimeProfile, RunEvent, RunEventReceipt, RunFileCandidate, RunFileRecord, RunStartPayload } from "./types.js";
|
|
2
|
-
/** Run-scoped client used only inside an Agent Service sandbox. */
|
|
3
|
-
export declare class AgentServiceRunClient {
|
|
4
|
-
private readonly baseUrl;
|
|
5
|
-
private readonly agentRunId;
|
|
6
|
-
private readonly runToken;
|
|
7
|
-
private readonly workerId;
|
|
8
|
-
private traceId;
|
|
9
|
-
constructor(baseUrl: string, agentRunId: string, runToken: string, workerId: string);
|
|
10
|
-
private url;
|
|
11
|
-
private request;
|
|
12
|
-
getRun(): Promise<RunStartPayload>;
|
|
13
|
-
postEvent(agentRunId: string, event: RunEvent): Promise<RunEventReceipt>;
|
|
14
|
-
postFile(agentRunId: string, file: RunFileCandidate): Promise<RunFileRecord>;
|
|
15
|
-
getRunRuntimeProfile(agentRunId: string): Promise<CourseRuntimeProfile>;
|
|
16
|
-
heartbeat(): Promise<void>;
|
|
17
|
-
private assertRunId;
|
|
18
|
-
}
|