rl-rock 1.3.9 → 1.10.0-beta.1

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/README.md CHANGED
@@ -52,6 +52,9 @@ const sandbox = new Sandbox({
52
52
  await sandbox.start();
53
53
 
54
54
  console.log(`Sandbox ID: ${sandbox.getSandboxId()}`);
55
+
56
+ // 重启沙箱
57
+ await sandbox.restart();
55
58
  ```
56
59
 
57
60
  ### 执行命令
@@ -237,6 +240,7 @@ interface SandboxConfig {
237
240
  cpus: number; // CPU 核心数
238
241
  autoClearSeconds: number; // 自动清理时间
239
242
  startupTimeout: number; // 启动超时
243
+ autoDeleteSeconds?: number | null; // 沙箱自动删除时间(秒),0 表示立即清理,null/undefined 表示不自动删除
240
244
  routeKey?: string; // 路由键
241
245
  extraHeaders?: Record<string, string>; // 额外请求头
242
246
  }
package/dist/index.d.mts CHANGED
@@ -983,6 +983,7 @@ declare const SandboxConfigSchema: z.ZodObject<{
983
983
  experimentId: z.ZodOptional<z.ZodString>;
984
984
  cluster: z.ZodDefault<z.ZodString>;
985
985
  namespace: z.ZodOptional<z.ZodString>;
986
+ autoDeleteSeconds: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
986
987
  }, "strip", z.ZodTypeAny, {
987
988
  image: string;
988
989
  cpus: number;
@@ -997,6 +998,7 @@ declare const SandboxConfigSchema: z.ZodObject<{
997
998
  namespace?: string | undefined;
998
999
  xrlAuthorization?: string | undefined;
999
1000
  routeKey?: string | undefined;
1001
+ autoDeleteSeconds?: number | null | undefined;
1000
1002
  }, {
1001
1003
  image?: string | undefined;
1002
1004
  userId?: string | undefined;
@@ -1011,6 +1013,7 @@ declare const SandboxConfigSchema: z.ZodObject<{
1011
1013
  autoClearSeconds?: number | undefined;
1012
1014
  routeKey?: string | undefined;
1013
1015
  startupTimeout?: number | undefined;
1016
+ autoDeleteSeconds?: number | null | undefined;
1014
1017
  }>;
1015
1018
  type SandboxConfig = z.infer<typeof SandboxConfigSchema>;
1016
1019
  /**
@@ -1031,6 +1034,7 @@ declare const SandboxGroupConfigSchema: z.ZodObject<{
1031
1034
  experimentId: z.ZodOptional<z.ZodString>;
1032
1035
  cluster: z.ZodDefault<z.ZodString>;
1033
1036
  namespace: z.ZodOptional<z.ZodString>;
1037
+ autoDeleteSeconds: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
1034
1038
  } & {
1035
1039
  size: z.ZodDefault<z.ZodNumber>;
1036
1040
  startConcurrency: z.ZodDefault<z.ZodNumber>;
@@ -1052,6 +1056,7 @@ declare const SandboxGroupConfigSchema: z.ZodObject<{
1052
1056
  namespace?: string | undefined;
1053
1057
  xrlAuthorization?: string | undefined;
1054
1058
  routeKey?: string | undefined;
1059
+ autoDeleteSeconds?: number | null | undefined;
1055
1060
  }, {
1056
1061
  image?: string | undefined;
1057
1062
  userId?: string | undefined;
@@ -1066,6 +1071,7 @@ declare const SandboxGroupConfigSchema: z.ZodObject<{
1066
1071
  autoClearSeconds?: number | undefined;
1067
1072
  routeKey?: string | undefined;
1068
1073
  startupTimeout?: number | undefined;
1074
+ autoDeleteSeconds?: number | null | undefined;
1069
1075
  size?: number | undefined;
1070
1076
  startConcurrency?: number | undefined;
1071
1077
  startRetryTimes?: number | undefined;
@@ -1312,7 +1318,9 @@ declare class Sandbox extends AbstractSandbox {
1312
1318
  start(): Promise<void>;
1313
1319
  stop(): Promise<void>;
1314
1320
  isAlive(): Promise<IsAliveResponse>;
1315
- getStatus(): Promise<SandboxStatusResponse>;
1321
+ getStatus(options?: {
1322
+ includeAllStates?: boolean;
1323
+ }): Promise<SandboxStatusResponse>;
1316
1324
  execute(command: Command): Promise<CommandResponse>;
1317
1325
  createSession(request: CreateBashSessionRequest): Promise<CreateSessionResponse>;
1318
1326
  closeSession(request: CloseSessionRequest): Promise<CloseSessionResponse>;
@@ -1367,6 +1375,9 @@ declare class Sandbox extends AbstractSandbox {
1367
1375
  * @param timeout - Optional timeout in milliseconds (defaults to ROCK_OSS_TIMEOUT env var or 300000ms)
1368
1376
  */
1369
1377
  private setupOss;
1378
+ restart(): Promise<void>;
1379
+ private waitForAlive;
1380
+ private parseErrorMessageFromStatus;
1370
1381
  close(): Promise<void>;
1371
1382
  toString(): string;
1372
1383
  }
package/dist/index.d.ts CHANGED
@@ -983,6 +983,7 @@ declare const SandboxConfigSchema: z.ZodObject<{
983
983
  experimentId: z.ZodOptional<z.ZodString>;
984
984
  cluster: z.ZodDefault<z.ZodString>;
985
985
  namespace: z.ZodOptional<z.ZodString>;
986
+ autoDeleteSeconds: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
986
987
  }, "strip", z.ZodTypeAny, {
987
988
  image: string;
988
989
  cpus: number;
@@ -997,6 +998,7 @@ declare const SandboxConfigSchema: z.ZodObject<{
997
998
  namespace?: string | undefined;
998
999
  xrlAuthorization?: string | undefined;
999
1000
  routeKey?: string | undefined;
1001
+ autoDeleteSeconds?: number | null | undefined;
1000
1002
  }, {
1001
1003
  image?: string | undefined;
1002
1004
  userId?: string | undefined;
@@ -1011,6 +1013,7 @@ declare const SandboxConfigSchema: z.ZodObject<{
1011
1013
  autoClearSeconds?: number | undefined;
1012
1014
  routeKey?: string | undefined;
1013
1015
  startupTimeout?: number | undefined;
1016
+ autoDeleteSeconds?: number | null | undefined;
1014
1017
  }>;
1015
1018
  type SandboxConfig = z.infer<typeof SandboxConfigSchema>;
1016
1019
  /**
@@ -1031,6 +1034,7 @@ declare const SandboxGroupConfigSchema: z.ZodObject<{
1031
1034
  experimentId: z.ZodOptional<z.ZodString>;
1032
1035
  cluster: z.ZodDefault<z.ZodString>;
1033
1036
  namespace: z.ZodOptional<z.ZodString>;
1037
+ autoDeleteSeconds: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
1034
1038
  } & {
1035
1039
  size: z.ZodDefault<z.ZodNumber>;
1036
1040
  startConcurrency: z.ZodDefault<z.ZodNumber>;
@@ -1052,6 +1056,7 @@ declare const SandboxGroupConfigSchema: z.ZodObject<{
1052
1056
  namespace?: string | undefined;
1053
1057
  xrlAuthorization?: string | undefined;
1054
1058
  routeKey?: string | undefined;
1059
+ autoDeleteSeconds?: number | null | undefined;
1055
1060
  }, {
1056
1061
  image?: string | undefined;
1057
1062
  userId?: string | undefined;
@@ -1066,6 +1071,7 @@ declare const SandboxGroupConfigSchema: z.ZodObject<{
1066
1071
  autoClearSeconds?: number | undefined;
1067
1072
  routeKey?: string | undefined;
1068
1073
  startupTimeout?: number | undefined;
1074
+ autoDeleteSeconds?: number | null | undefined;
1069
1075
  size?: number | undefined;
1070
1076
  startConcurrency?: number | undefined;
1071
1077
  startRetryTimes?: number | undefined;
@@ -1312,7 +1318,9 @@ declare class Sandbox extends AbstractSandbox {
1312
1318
  start(): Promise<void>;
1313
1319
  stop(): Promise<void>;
1314
1320
  isAlive(): Promise<IsAliveResponse>;
1315
- getStatus(): Promise<SandboxStatusResponse>;
1321
+ getStatus(options?: {
1322
+ includeAllStates?: boolean;
1323
+ }): Promise<SandboxStatusResponse>;
1316
1324
  execute(command: Command): Promise<CommandResponse>;
1317
1325
  createSession(request: CreateBashSessionRequest): Promise<CreateSessionResponse>;
1318
1326
  closeSession(request: CloseSessionRequest): Promise<CloseSessionResponse>;
@@ -1367,6 +1375,9 @@ declare class Sandbox extends AbstractSandbox {
1367
1375
  * @param timeout - Optional timeout in milliseconds (defaults to ROCK_OSS_TIMEOUT env var or 300000ms)
1368
1376
  */
1369
1377
  private setupOss;
1378
+ restart(): Promise<void>;
1379
+ private waitForAlive;
1380
+ private parseErrorMessageFromStatus;
1370
1381
  close(): Promise<void>;
1371
1382
  toString(): string;
1372
1383
  }
package/dist/index.js CHANGED
@@ -1114,7 +1114,8 @@ var SandboxConfigSchema = BaseConfigSchema.extend({
1114
1114
  userId: zod.z.string().optional(),
1115
1115
  experimentId: zod.z.string().optional(),
1116
1116
  cluster: zod.z.string().default(() => envVars.ROCK_DEFAULT_CLUSTER),
1117
- namespace: zod.z.string().optional()
1117
+ namespace: zod.z.string().optional(),
1118
+ autoDeleteSeconds: zod.z.number().min(0, "autoDeleteSeconds must be >= 0").nullish()
1118
1119
  });
1119
1120
  var SandboxGroupConfigSchema = SandboxConfigSchema.extend({
1120
1121
  size: zod.z.number().default(() => envVars.ROCK_DEFAULT_GROUP_SIZE),
@@ -1995,7 +1996,8 @@ var Sandbox = class extends AbstractSandbox {
1995
1996
  autoClearTimeMinutes: this.config.autoClearSeconds / 60,
1996
1997
  startupTimeout: this.config.startupTimeout,
1997
1998
  memory: this.config.memory,
1998
- cpus: this.config.cpus
1999
+ cpus: this.config.cpus,
2000
+ autoDeleteSeconds: this.config.autoDeleteSeconds
1999
2001
  };
2000
2002
  logger8.debug(`Calling start_async API: ${url}`);
2001
2003
  logger8.debug(`Request data: ${JSON.stringify(data)}`);
@@ -2014,30 +2016,7 @@ var Sandbox = class extends AbstractSandbox {
2014
2016
  this.hostName = response.result?.hostName ?? null;
2015
2017
  this.hostIp = response.result?.hostIp ?? null;
2016
2018
  logger8.info(`Sandbox ID: ${this.sandboxId}`);
2017
- await sleep(2e3);
2018
- const startTime = Date.now();
2019
- const checkTimeout = 1e4;
2020
- const checkInterval = 3e3;
2021
- while (Date.now() - startTime < this.config.startupTimeout * 1e3) {
2022
- try {
2023
- logger8.info(`Checking status... (elapsed: ${Math.round((Date.now() - startTime) / 1e3)}s)`);
2024
- const statusPromise = this.getStatus();
2025
- const timeoutPromise = new Promise(
2026
- (_, reject) => setTimeout(() => reject(new Error("Status check timeout")), checkTimeout)
2027
- );
2028
- const status = await Promise.race([statusPromise, timeoutPromise]);
2029
- if (status && status.isAlive) {
2030
- logger8.info("Sandbox is alive");
2031
- return;
2032
- }
2033
- } catch (e) {
2034
- logger8.debug(`Status check failed (will retry): ${e}`);
2035
- }
2036
- await sleep(checkInterval);
2037
- }
2038
- throw new InternalServerRockError(
2039
- `Failed to start sandbox within ${this.config.startupTimeout}s, sandbox: ${this.toString()}`
2040
- );
2019
+ await this.waitForAlive();
2041
2020
  }
2042
2021
  async stop() {
2043
2022
  if (!this.sandboxId) return;
@@ -2060,8 +2039,11 @@ var Sandbox = class extends AbstractSandbox {
2060
2039
  throw new Error(`Failed to get is alive: ${e}`);
2061
2040
  }
2062
2041
  }
2063
- async getStatus() {
2064
- const url = `${this.url}/get_status?sandbox_id=${this.sandboxId}`;
2042
+ async getStatus(options) {
2043
+ let url = `${this.url}/get_status?sandbox_id=${this.sandboxId}`;
2044
+ if (options?.includeAllStates) {
2045
+ url += "&include_all_states=true";
2046
+ }
2065
2047
  const headers = this.buildHeaders();
2066
2048
  const response = await HttpUtils.get(url, headers);
2067
2049
  if (response.status !== "Success") {
@@ -2555,6 +2537,75 @@ var Sandbox = class extends AbstractSandbox {
2555
2537
  // 5 minutes
2556
2538
  });
2557
2539
  }
2540
+ async restart() {
2541
+ if (!this.sandboxId) {
2542
+ throw new Error("sandbox_id is not set, cannot restart");
2543
+ }
2544
+ const url = `${this.url}/restart`;
2545
+ const headers = this.buildHeaders();
2546
+ const data = { sandboxId: this.sandboxId };
2547
+ const response = await HttpUtils.post(url, headers, data);
2548
+ logger8.debug(`Restart sandbox response: ${JSON.stringify(response)}`);
2549
+ if (response.status !== "Success") {
2550
+ const code = response.result?.code;
2551
+ raiseForCode(code, `Failed to restart sandbox: ${JSON.stringify(response)}`);
2552
+ throw new Error(`Failed to restart sandbox: ${JSON.stringify(response)}`);
2553
+ }
2554
+ await this.waitForAlive({ includeAllStates: true, operation: "restart" });
2555
+ }
2556
+ async waitForAlive(options) {
2557
+ await sleep(2e3);
2558
+ const startTime = Date.now();
2559
+ const checkTimeout = 1e4;
2560
+ const checkInterval = 3e3;
2561
+ while (Date.now() - startTime < this.config.startupTimeout * 1e3) {
2562
+ let timeoutId;
2563
+ try {
2564
+ logger8.info(`Checking status... (elapsed: ${Math.round((Date.now() - startTime) / 1e3)}s)`);
2565
+ const statusPromise = this.getStatus(options);
2566
+ const timeoutPromise = new Promise((_, reject) => {
2567
+ timeoutId = setTimeout(() => reject(new Error("Status check timeout")), checkTimeout);
2568
+ });
2569
+ const status = await Promise.race([statusPromise, timeoutPromise]);
2570
+ if (status && status.isAlive) {
2571
+ logger8.info("Sandbox is alive");
2572
+ return;
2573
+ }
2574
+ if (options?.includeAllStates && status) {
2575
+ const errorMsg = this.parseErrorMessageFromStatus(status.status);
2576
+ if (errorMsg) {
2577
+ throw new InternalServerRockError(
2578
+ `Failed to restart sandbox because ${errorMsg}, sandbox: ${this.toString()}`
2579
+ );
2580
+ }
2581
+ }
2582
+ } catch (e) {
2583
+ if (e instanceof InternalServerRockError) {
2584
+ throw e;
2585
+ }
2586
+ logger8.debug(`Status check failed (will retry): ${e}`);
2587
+ } finally {
2588
+ clearTimeout(timeoutId);
2589
+ }
2590
+ await sleep(checkInterval);
2591
+ }
2592
+ const operation = options?.operation ?? "start";
2593
+ throw new InternalServerRockError(
2594
+ `Failed to ${operation} sandbox within ${this.config.startupTimeout}s, sandbox: ${this.toString()}`
2595
+ );
2596
+ }
2597
+ parseErrorMessageFromStatus(status) {
2598
+ if (!status) return null;
2599
+ for (const [stage, details] of Object.entries(status)) {
2600
+ if (details && typeof details === "object") {
2601
+ const d = details;
2602
+ if (d.status === "failed" || d.status === "timeout") {
2603
+ return `${stage}: ${d.message ?? "No message provided"}`;
2604
+ }
2605
+ }
2606
+ }
2607
+ return null;
2608
+ }
2558
2609
  // Close
2559
2610
  async close() {
2560
2611
  await this.stop();