@codelia/runtime 0.1.47 → 0.1.48

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.
Files changed (3) hide show
  1. package/dist/index.cjs +118 -10
  2. package/dist/index.js +118 -10
  3. package/package.json +9 -9
package/dist/index.cjs CHANGED
@@ -5984,13 +5984,15 @@ var shellStateSchema = import_zod10.z.enum([
5984
5984
  "cancelled"
5985
5985
  ]);
5986
5986
  var shellRunSchema = import_zod10.z.object({
5987
- command: import_zod10.z.string().describe("Shell command to execute in the current workspace."),
5987
+ command: import_zod10.z.string().describe(
5988
+ "Shell command to execute as a runtime-managed child process in the current workspace."
5989
+ ),
5988
5990
  label: import_zod10.z.string().max(SHELL_LABEL_MAX_CHARS).optional().describe(`Optional short task label used to build the returned task key. Max ${SHELL_LABEL_MAX_CHARS} chars.`),
5989
5991
  timeout: import_zod10.z.number().int().positive().optional().describe(
5990
- `Execution timeout in seconds. Foreground default: ${DEFAULT_TIMEOUT_SECONDS}, max ${MAX_TIMEOUT_SECONDS}. Background accepts larger values up to ${MAX_EXECUTION_TIMEOUT_SECONDS}; omit to keep running until completion, cancel, or runtime exit.`
5992
+ `Execution timeout in seconds. Foreground default: ${DEFAULT_TIMEOUT_SECONDS}, max ${MAX_TIMEOUT_SECONDS}. Background accepts larger values up to ${MAX_EXECUTION_TIMEOUT_SECONDS}; omit to keep the managed child job running until completion, cancel, or runtime exit.`
5991
5993
  ),
5992
5994
  background: import_zod10.z.boolean().optional().describe(
5993
- "Return immediately with task info instead of waiting. Manage the task with shell_status/logs/wait/result/cancel. Default: false."
5995
+ "Detach the wait and return task info immediately. The runtime still owns the child process, so this is not persistence/daemonization. Manage it with shell_status/logs/wait/result/cancel. Default: false."
5994
5996
  )
5995
5997
  }).superRefine((input, ctx) => {
5996
5998
  if (input.timeout === void 0) {
@@ -6017,6 +6019,11 @@ var shellRunSchema = import_zod10.z.object({
6017
6019
  var shellTaskKeySchema = import_zod10.z.object({
6018
6020
  key: import_zod10.z.string().describe("Canonical shell task key returned by shell or shell_list.")
6019
6021
  });
6022
+ var shellWaitSchema = shellTaskKeySchema.extend({
6023
+ wait_timeout: import_zod10.z.number().int().positive().max(MAX_TIMEOUT_SECONDS).optional().describe(
6024
+ `Attached wait window in seconds. Default: ${DEFAULT_TIMEOUT_SECONDS}. Max ${MAX_TIMEOUT_SECONDS}. Returns still_running=true if the task has not finished before this window expires.`
6025
+ )
6026
+ });
6020
6027
  var shellStreamSchema = import_zod10.z.enum(["stdout", "stderr"]);
6021
6028
  var shellListSchema = import_zod10.z.object({
6022
6029
  limit: import_zod10.z.number().int().positive().max(SHELL_LIST_MAX_LIMIT).optional().describe(
@@ -6317,11 +6324,54 @@ var waitForManagedTask = async (tasks, taskId, signal) => {
6317
6324
  throw error;
6318
6325
  }
6319
6326
  };
6327
+ var resolveShellWaitTimeoutSeconds = (value) => Math.max(1, Math.min(Math.trunc(value ?? DEFAULT_TIMEOUT_SECONDS), MAX_TIMEOUT_SECONDS));
6328
+ var waitForManagedTaskWindow = async (tasks, taskId, options) => {
6329
+ let timeoutHandle;
6330
+ const waitPromise = waitForManagedTask(tasks, taskId, options.signal).then(
6331
+ (result) => ({
6332
+ type: "task",
6333
+ result
6334
+ })
6335
+ );
6336
+ const timeoutPromise = new Promise((resolve) => {
6337
+ timeoutHandle = setTimeout(
6338
+ () => resolve({ type: "timeout" }),
6339
+ options.waitTimeoutSeconds * 1e3
6340
+ );
6341
+ });
6342
+ try {
6343
+ const outcome = await Promise.race([waitPromise, timeoutPromise]);
6344
+ if (outcome.type === "task") {
6345
+ return {
6346
+ task: outcome.result.task,
6347
+ aborted: outcome.result.aborted,
6348
+ stillRunning: false
6349
+ };
6350
+ }
6351
+ const task = await requireTask(tasks, taskId);
6352
+ if (isTerminalTaskState(task.state)) {
6353
+ return {
6354
+ task,
6355
+ aborted: false,
6356
+ stillRunning: false
6357
+ };
6358
+ }
6359
+ return {
6360
+ task,
6361
+ aborted: false,
6362
+ stillRunning: true
6363
+ };
6364
+ } finally {
6365
+ if (timeoutHandle) {
6366
+ clearTimeout(timeoutHandle);
6367
+ }
6368
+ }
6369
+ };
6320
6370
  var createShellTool = (sandboxKey, options = {}) => {
6321
6371
  const shared = getSharedDeps(options);
6322
6372
  return (0, import_core11.defineTool)({
6323
6373
  name: "shell",
6324
- description: "Run a shell command in the sandbox. By default wait for completion; with `background=true`, return a task you can inspect, wait on, or cancel later.",
6374
+ description: "Run a shell command as a runtime-managed child process in the sandbox. By default wait for completion; with `background=true`, detach the wait and return task info you can inspect, wait on, or cancel later.",
6325
6375
  input: shellRunSchema,
6326
6376
  execute: async (input, ctx) => {
6327
6377
  const sandbox = await getSandboxContext(ctx, sandboxKey);
@@ -6453,18 +6503,25 @@ var createShellWaitTool = (options = {}) => {
6453
6503
  const shared = getSharedDeps(options);
6454
6504
  return (0, import_core11.defineTool)({
6455
6505
  name: "shell_wait",
6456
- description: "Wait for a shell task to reach a terminal state and return its retained outcome.",
6457
- input: shellTaskKeySchema,
6506
+ description: "Wait for a shell task within a bounded window and return its retained outcome; if the window expires first, return still_running=true.",
6507
+ input: shellWaitSchema,
6458
6508
  execute: async (input, ctx) => {
6459
6509
  try {
6460
- const task = await resolveShellTask(shared.tasks, input);
6461
- const result = await waitForManagedTask(
6510
+ const waitInput = input;
6511
+ const task = await resolveShellTask(shared.tasks, waitInput);
6512
+ const result = await waitForManagedTaskWindow(
6462
6513
  shared.tasks,
6463
6514
  task.task_id,
6464
- ctx.signal
6515
+ {
6516
+ waitTimeoutSeconds: resolveShellWaitTimeoutSeconds(
6517
+ waitInput.wait_timeout
6518
+ ),
6519
+ signal: ctx.signal
6520
+ }
6465
6521
  );
6466
6522
  return {
6467
6523
  ...result.aborted ? { aborted: true } : {},
6524
+ ...result.stillRunning ? { still_running: true } : {},
6468
6525
  task: toShellTaskInfo(result.task)
6469
6526
  };
6470
6527
  } catch (error) {
@@ -11190,6 +11247,28 @@ var resolveShellTimeoutSeconds2 = (requestedTimeout, background) => {
11190
11247
  }
11191
11248
  return background ? void 0 : DEFAULT_TIMEOUT_SECONDS;
11192
11249
  };
11250
+ var resolveShellWaitTimeoutSeconds2 = (requestedTimeout) => {
11251
+ if (requestedTimeout !== void 0) {
11252
+ if (!Number.isFinite(requestedTimeout) || requestedTimeout <= 0) {
11253
+ return {
11254
+ error: {
11255
+ code: import_protocol6.RPC_ERROR_CODE.INVALID_PARAMS,
11256
+ message: "wait_timeout_seconds must be a positive number"
11257
+ }
11258
+ };
11259
+ }
11260
+ if (requestedTimeout > MAX_TIMEOUT_SECONDS) {
11261
+ return {
11262
+ error: {
11263
+ code: import_protocol6.RPC_ERROR_CODE.INVALID_PARAMS,
11264
+ message: `wait_timeout_seconds must be ${MAX_TIMEOUT_SECONDS} seconds or less`
11265
+ }
11266
+ };
11267
+ }
11268
+ return Math.max(1, Math.trunc(requestedTimeout));
11269
+ }
11270
+ return DEFAULT_TIMEOUT_SECONDS;
11271
+ };
11193
11272
  var formatShellTimeoutForLog2 = (value) => value === void 0 ? "none" : String(value);
11194
11273
  var parseShellStartRequest = (state, params, options) => {
11195
11274
  const command = params?.command?.trim() ?? "";
@@ -11395,6 +11474,13 @@ var createShellHandlers = ({
11395
11474
  });
11396
11475
  return;
11397
11476
  }
11477
+ const waitTimeoutSeconds = resolveShellWaitTimeoutSeconds2(
11478
+ params?.wait_timeout_seconds
11479
+ );
11480
+ if (typeof waitTimeoutSeconds === "object") {
11481
+ sendError(id, waitTimeoutSeconds.error);
11482
+ return;
11483
+ }
11398
11484
  try {
11399
11485
  const existing = await requireShellTask(shellTaskManager, taskId);
11400
11486
  if (isTerminalTaskState(existing.state)) {
@@ -11402,12 +11488,19 @@ var createShellHandlers = ({
11402
11488
  return;
11403
11489
  }
11404
11490
  const controller = new AbortController();
11491
+ let timeoutHandle;
11405
11492
  let resolveDetached;
11406
11493
  const detachPromise = new Promise(
11407
11494
  (resolve) => {
11408
11495
  resolveDetached = (result) => resolve({ type: "detached", result });
11409
11496
  }
11410
11497
  );
11498
+ const timeoutPromise = new Promise((resolve) => {
11499
+ timeoutHandle = setTimeout(() => {
11500
+ controller.abort();
11501
+ resolve({ type: "still_running" });
11502
+ }, waitTimeoutSeconds * 1e3);
11503
+ });
11411
11504
  const unregister = registerActiveShellWait(taskId, {
11412
11505
  detach: (task) => {
11413
11506
  resolveDetached({
@@ -11425,16 +11518,31 @@ var createShellHandlers = ({
11425
11518
  }
11426
11519
  throw error;
11427
11520
  });
11428
- const outcome = await Promise.race([waitPromise, detachPromise]);
11521
+ const outcome = await Promise.race([
11522
+ waitPromise,
11523
+ detachPromise,
11524
+ timeoutPromise
11525
+ ]);
11429
11526
  if (outcome.type === "detached") {
11430
11527
  sendResult(id, outcome.result);
11431
11528
  return;
11432
11529
  }
11530
+ if (outcome.type === "still_running") {
11531
+ const task = await requireShellTask(shellTaskManager, taskId);
11532
+ sendResult(id, {
11533
+ ...toShellTaskInfo2(task),
11534
+ ...isTerminalTaskState(task.state) ? {} : { still_running: true }
11535
+ });
11536
+ return;
11537
+ }
11433
11538
  if (outcome.type === "aborted") {
11434
11539
  return;
11435
11540
  }
11436
11541
  sendResult(id, toShellTaskInfo2(outcome.task));
11437
11542
  } finally {
11543
+ if (timeoutHandle) {
11544
+ clearTimeout(timeoutHandle);
11545
+ }
11438
11546
  unregister();
11439
11547
  }
11440
11548
  } catch (error) {
package/dist/index.js CHANGED
@@ -5992,13 +5992,15 @@ var shellStateSchema = z10.enum([
5992
5992
  "cancelled"
5993
5993
  ]);
5994
5994
  var shellRunSchema = z10.object({
5995
- command: z10.string().describe("Shell command to execute in the current workspace."),
5995
+ command: z10.string().describe(
5996
+ "Shell command to execute as a runtime-managed child process in the current workspace."
5997
+ ),
5996
5998
  label: z10.string().max(SHELL_LABEL_MAX_CHARS).optional().describe(`Optional short task label used to build the returned task key. Max ${SHELL_LABEL_MAX_CHARS} chars.`),
5997
5999
  timeout: z10.number().int().positive().optional().describe(
5998
- `Execution timeout in seconds. Foreground default: ${DEFAULT_TIMEOUT_SECONDS}, max ${MAX_TIMEOUT_SECONDS}. Background accepts larger values up to ${MAX_EXECUTION_TIMEOUT_SECONDS}; omit to keep running until completion, cancel, or runtime exit.`
6000
+ `Execution timeout in seconds. Foreground default: ${DEFAULT_TIMEOUT_SECONDS}, max ${MAX_TIMEOUT_SECONDS}. Background accepts larger values up to ${MAX_EXECUTION_TIMEOUT_SECONDS}; omit to keep the managed child job running until completion, cancel, or runtime exit.`
5999
6001
  ),
6000
6002
  background: z10.boolean().optional().describe(
6001
- "Return immediately with task info instead of waiting. Manage the task with shell_status/logs/wait/result/cancel. Default: false."
6003
+ "Detach the wait and return task info immediately. The runtime still owns the child process, so this is not persistence/daemonization. Manage it with shell_status/logs/wait/result/cancel. Default: false."
6002
6004
  )
6003
6005
  }).superRefine((input, ctx) => {
6004
6006
  if (input.timeout === void 0) {
@@ -6025,6 +6027,11 @@ var shellRunSchema = z10.object({
6025
6027
  var shellTaskKeySchema = z10.object({
6026
6028
  key: z10.string().describe("Canonical shell task key returned by shell or shell_list.")
6027
6029
  });
6030
+ var shellWaitSchema = shellTaskKeySchema.extend({
6031
+ wait_timeout: z10.number().int().positive().max(MAX_TIMEOUT_SECONDS).optional().describe(
6032
+ `Attached wait window in seconds. Default: ${DEFAULT_TIMEOUT_SECONDS}. Max ${MAX_TIMEOUT_SECONDS}. Returns still_running=true if the task has not finished before this window expires.`
6033
+ )
6034
+ });
6028
6035
  var shellStreamSchema = z10.enum(["stdout", "stderr"]);
6029
6036
  var shellListSchema = z10.object({
6030
6037
  limit: z10.number().int().positive().max(SHELL_LIST_MAX_LIMIT).optional().describe(
@@ -6325,11 +6332,54 @@ var waitForManagedTask = async (tasks, taskId, signal) => {
6325
6332
  throw error;
6326
6333
  }
6327
6334
  };
6335
+ var resolveShellWaitTimeoutSeconds = (value) => Math.max(1, Math.min(Math.trunc(value ?? DEFAULT_TIMEOUT_SECONDS), MAX_TIMEOUT_SECONDS));
6336
+ var waitForManagedTaskWindow = async (tasks, taskId, options) => {
6337
+ let timeoutHandle;
6338
+ const waitPromise = waitForManagedTask(tasks, taskId, options.signal).then(
6339
+ (result) => ({
6340
+ type: "task",
6341
+ result
6342
+ })
6343
+ );
6344
+ const timeoutPromise = new Promise((resolve) => {
6345
+ timeoutHandle = setTimeout(
6346
+ () => resolve({ type: "timeout" }),
6347
+ options.waitTimeoutSeconds * 1e3
6348
+ );
6349
+ });
6350
+ try {
6351
+ const outcome = await Promise.race([waitPromise, timeoutPromise]);
6352
+ if (outcome.type === "task") {
6353
+ return {
6354
+ task: outcome.result.task,
6355
+ aborted: outcome.result.aborted,
6356
+ stillRunning: false
6357
+ };
6358
+ }
6359
+ const task = await requireTask(tasks, taskId);
6360
+ if (isTerminalTaskState(task.state)) {
6361
+ return {
6362
+ task,
6363
+ aborted: false,
6364
+ stillRunning: false
6365
+ };
6366
+ }
6367
+ return {
6368
+ task,
6369
+ aborted: false,
6370
+ stillRunning: true
6371
+ };
6372
+ } finally {
6373
+ if (timeoutHandle) {
6374
+ clearTimeout(timeoutHandle);
6375
+ }
6376
+ }
6377
+ };
6328
6378
  var createShellTool = (sandboxKey, options = {}) => {
6329
6379
  const shared = getSharedDeps(options);
6330
6380
  return defineTool9({
6331
6381
  name: "shell",
6332
- description: "Run a shell command in the sandbox. By default wait for completion; with `background=true`, return a task you can inspect, wait on, or cancel later.",
6382
+ description: "Run a shell command as a runtime-managed child process in the sandbox. By default wait for completion; with `background=true`, detach the wait and return task info you can inspect, wait on, or cancel later.",
6333
6383
  input: shellRunSchema,
6334
6384
  execute: async (input, ctx) => {
6335
6385
  const sandbox = await getSandboxContext(ctx, sandboxKey);
@@ -6461,18 +6511,25 @@ var createShellWaitTool = (options = {}) => {
6461
6511
  const shared = getSharedDeps(options);
6462
6512
  return defineTool9({
6463
6513
  name: "shell_wait",
6464
- description: "Wait for a shell task to reach a terminal state and return its retained outcome.",
6465
- input: shellTaskKeySchema,
6514
+ description: "Wait for a shell task within a bounded window and return its retained outcome; if the window expires first, return still_running=true.",
6515
+ input: shellWaitSchema,
6466
6516
  execute: async (input, ctx) => {
6467
6517
  try {
6468
- const task = await resolveShellTask(shared.tasks, input);
6469
- const result = await waitForManagedTask(
6518
+ const waitInput = input;
6519
+ const task = await resolveShellTask(shared.tasks, waitInput);
6520
+ const result = await waitForManagedTaskWindow(
6470
6521
  shared.tasks,
6471
6522
  task.task_id,
6472
- ctx.signal
6523
+ {
6524
+ waitTimeoutSeconds: resolveShellWaitTimeoutSeconds(
6525
+ waitInput.wait_timeout
6526
+ ),
6527
+ signal: ctx.signal
6528
+ }
6473
6529
  );
6474
6530
  return {
6475
6531
  ...result.aborted ? { aborted: true } : {},
6532
+ ...result.stillRunning ? { still_running: true } : {},
6476
6533
  task: toShellTaskInfo(result.task)
6477
6534
  };
6478
6535
  } catch (error) {
@@ -11230,6 +11287,28 @@ var resolveShellTimeoutSeconds2 = (requestedTimeout, background) => {
11230
11287
  }
11231
11288
  return background ? void 0 : DEFAULT_TIMEOUT_SECONDS;
11232
11289
  };
11290
+ var resolveShellWaitTimeoutSeconds2 = (requestedTimeout) => {
11291
+ if (requestedTimeout !== void 0) {
11292
+ if (!Number.isFinite(requestedTimeout) || requestedTimeout <= 0) {
11293
+ return {
11294
+ error: {
11295
+ code: RPC_ERROR_CODE5.INVALID_PARAMS,
11296
+ message: "wait_timeout_seconds must be a positive number"
11297
+ }
11298
+ };
11299
+ }
11300
+ if (requestedTimeout > MAX_TIMEOUT_SECONDS) {
11301
+ return {
11302
+ error: {
11303
+ code: RPC_ERROR_CODE5.INVALID_PARAMS,
11304
+ message: `wait_timeout_seconds must be ${MAX_TIMEOUT_SECONDS} seconds or less`
11305
+ }
11306
+ };
11307
+ }
11308
+ return Math.max(1, Math.trunc(requestedTimeout));
11309
+ }
11310
+ return DEFAULT_TIMEOUT_SECONDS;
11311
+ };
11233
11312
  var formatShellTimeoutForLog2 = (value) => value === void 0 ? "none" : String(value);
11234
11313
  var parseShellStartRequest = (state, params, options) => {
11235
11314
  const command = params?.command?.trim() ?? "";
@@ -11435,6 +11514,13 @@ var createShellHandlers = ({
11435
11514
  });
11436
11515
  return;
11437
11516
  }
11517
+ const waitTimeoutSeconds = resolveShellWaitTimeoutSeconds2(
11518
+ params?.wait_timeout_seconds
11519
+ );
11520
+ if (typeof waitTimeoutSeconds === "object") {
11521
+ sendError(id, waitTimeoutSeconds.error);
11522
+ return;
11523
+ }
11438
11524
  try {
11439
11525
  const existing = await requireShellTask(shellTaskManager, taskId);
11440
11526
  if (isTerminalTaskState(existing.state)) {
@@ -11442,12 +11528,19 @@ var createShellHandlers = ({
11442
11528
  return;
11443
11529
  }
11444
11530
  const controller = new AbortController();
11531
+ let timeoutHandle;
11445
11532
  let resolveDetached;
11446
11533
  const detachPromise = new Promise(
11447
11534
  (resolve) => {
11448
11535
  resolveDetached = (result) => resolve({ type: "detached", result });
11449
11536
  }
11450
11537
  );
11538
+ const timeoutPromise = new Promise((resolve) => {
11539
+ timeoutHandle = setTimeout(() => {
11540
+ controller.abort();
11541
+ resolve({ type: "still_running" });
11542
+ }, waitTimeoutSeconds * 1e3);
11543
+ });
11451
11544
  const unregister = registerActiveShellWait(taskId, {
11452
11545
  detach: (task) => {
11453
11546
  resolveDetached({
@@ -11465,16 +11558,31 @@ var createShellHandlers = ({
11465
11558
  }
11466
11559
  throw error;
11467
11560
  });
11468
- const outcome = await Promise.race([waitPromise, detachPromise]);
11561
+ const outcome = await Promise.race([
11562
+ waitPromise,
11563
+ detachPromise,
11564
+ timeoutPromise
11565
+ ]);
11469
11566
  if (outcome.type === "detached") {
11470
11567
  sendResult(id, outcome.result);
11471
11568
  return;
11472
11569
  }
11570
+ if (outcome.type === "still_running") {
11571
+ const task = await requireShellTask(shellTaskManager, taskId);
11572
+ sendResult(id, {
11573
+ ...toShellTaskInfo2(task),
11574
+ ...isTerminalTaskState(task.state) ? {} : { still_running: true }
11575
+ });
11576
+ return;
11577
+ }
11473
11578
  if (outcome.type === "aborted") {
11474
11579
  return;
11475
11580
  }
11476
11581
  sendResult(id, toShellTaskInfo2(outcome.task));
11477
11582
  } finally {
11583
+ if (timeoutHandle) {
11584
+ clearTimeout(timeoutHandle);
11585
+ }
11478
11586
  unregister();
11479
11587
  }
11480
11588
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codelia/runtime",
3
- "version": "0.1.47",
3
+ "version": "0.1.48",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=20 <25"
@@ -23,14 +23,14 @@
23
23
  "typecheck": "tsc --noEmit"
24
24
  },
25
25
  "dependencies": {
26
- "@codelia/config": "0.1.47",
27
- "@codelia/config-loader": "0.1.47",
28
- "@codelia/core": "0.1.47",
29
- "@codelia/logger": "0.1.47",
30
- "@codelia/model-metadata": "0.1.47",
31
- "@codelia/protocol": "0.1.47",
32
- "@codelia/shared-types": "0.1.47",
33
- "@codelia/storage": "0.1.47",
26
+ "@codelia/config": "0.1.48",
27
+ "@codelia/config-loader": "0.1.48",
28
+ "@codelia/core": "0.1.48",
29
+ "@codelia/logger": "0.1.48",
30
+ "@codelia/model-metadata": "0.1.48",
31
+ "@codelia/protocol": "0.1.48",
32
+ "@codelia/shared-types": "0.1.48",
33
+ "@codelia/storage": "0.1.48",
34
34
  "eventsource-parser": "^3.0.6",
35
35
  "oauth4webapi": "^3.8.4",
36
36
  "zod": "^4.3.5"