plugin-agent-orchestrator 1.0.32 → 1.0.35

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 (45) hide show
  1. package/README.md +114 -114
  2. package/dist/externalVersion.js +6 -6
  3. package/dist/server/collections/skill-executions.js +21 -0
  4. package/dist/server/plugin.js +132 -3
  5. package/dist/server/resources/agent-loop.js +1 -2
  6. package/dist/server/services/AgentLoopController.js +39 -1
  7. package/dist/server/services/AgentLoopService.js +3 -0
  8. package/dist/server/services/AgentRuntimeLifecycle.js +70 -0
  9. package/dist/server/services/FileManager.js +12 -0
  10. package/dist/server/services/NativeSubAgentObserver.js +20 -0
  11. package/dist/server/services/PlanningPromptService.js +95 -0
  12. package/dist/server/services/SandboxRunner.js +251 -56
  13. package/dist/server/services/TokenTracker.js +20 -0
  14. package/dist/server/services/WorkerEnvManager.js +76 -10
  15. package/dist/server/services/pythonGuardTemplate.js +105 -0
  16. package/dist/server/services/sandboxCapabilities.js +286 -0
  17. package/dist/server/skill-hub/plugin.js +98 -24
  18. package/dist/server/skill-hub/tasks/SkillExecutionTask.js +53 -11
  19. package/dist/server/utils/skill-settings.js +1 -1
  20. package/package.json +47 -47
  21. package/src/client-v2/components/AgentRunsTab.tsx +494 -494
  22. package/src/client-v2/components/TracingTab.tsx +432 -432
  23. package/src/client-v2/hooks/useApiRequest.ts +121 -121
  24. package/src/server/__tests__/agent-runtime-lifecycle.test.ts +46 -0
  25. package/src/server/__tests__/planning-prompt-service.test.ts +23 -0
  26. package/src/server/__tests__/skill-sandbox-worker-mode.test.ts +15 -0
  27. package/src/server/collections/skill-executions.ts +21 -0
  28. package/src/server/plugin.ts +151 -0
  29. package/src/server/resources/agent-loop.ts +1 -2
  30. package/src/server/resources/agent-monitor.ts +482 -482
  31. package/src/server/services/AgentLoopController.ts +50 -1
  32. package/src/server/services/AgentLoopService.ts +4 -0
  33. package/src/server/services/AgentRuntimeLifecycle.ts +64 -0
  34. package/src/server/services/ExecutionSpanService.ts +110 -110
  35. package/src/server/services/FileManager.ts +16 -1
  36. package/src/server/services/NativeSubAgentObserver.ts +528 -507
  37. package/src/server/services/PlanningPromptService.ts +72 -0
  38. package/src/server/services/SandboxRunner.ts +341 -74
  39. package/src/server/services/TokenTracker.ts +26 -0
  40. package/src/server/services/WorkerEnvManager.ts +95 -9
  41. package/src/server/services/pythonGuardTemplate.ts +87 -0
  42. package/src/server/services/sandboxCapabilities.ts +348 -0
  43. package/src/server/skill-hub/plugin.ts +106 -23
  44. package/src/server/skill-hub/tasks/SkillExecutionTask.ts +51 -6
  45. package/src/server/utils/skill-settings.ts +1 -0
@@ -19,6 +19,11 @@ type ToolRuntimeInput = string | ToolsRuntime | undefined;
19
19
  const SKILL_TASK_POLL_INTERVAL_MS = Number.parseInt(process.env.SKILL_HUB_TASK_POLL_INTERVAL_MS || '5000', 10);
20
20
  const SKILL_TASK_POLL_LIMIT = Number.parseInt(process.env.SKILL_HUB_TASK_POLL_LIMIT || '3', 10);
21
21
 
22
+ function positiveInteger(value: string | undefined, fallback: number): number {
23
+ const parsed = Number.parseInt(value || '', 10);
24
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
25
+ }
26
+
22
27
  function normalizeToolRuntime(runtime: ToolRuntimeInput): ToolsRuntime | undefined {
23
28
  if (!runtime) return undefined;
24
29
  if (typeof runtime === 'string') {
@@ -113,6 +118,16 @@ export class SkillHubSubFeature {
113
118
  parseInt(process.env.SKILL_HUB_RATE_LIMIT_WINDOW_MS || '60000', 10),
114
119
  );
115
120
  private skillTemplates = new Map<string, any>();
121
+ private readonly afterStart = async () => {
122
+ this.registerAITools();
123
+ this.startCleanupInterval();
124
+ this.startSkillTaskPoller();
125
+ await this.subscribeInitEnvDone();
126
+ await this.skillManager.seedDefaults().catch((error: unknown) => {
127
+ const message = error instanceof Error ? error.message : String(error);
128
+ (this as any).app.logger.error(`[skill-hub] Failed to seed default skills: ${message}`);
129
+ });
130
+ };
116
131
 
117
132
  constructor(private plugin: any) {}
118
133
 
@@ -261,30 +276,23 @@ export class SkillHubSubFeature {
261
276
  });
262
277
 
263
278
  // 5. Subscribe PubSub — worker processes skill execution tasks
264
- this.skillTaskCallback = async (payload: any) => {
265
- if (process.env.SKILL_HUB_SANDBOX === 'false') return;
266
- await this.onQueueTask(payload);
267
- };
268
- (this as any).app.pubSubManager.subscribe('skill-hub.task', this.skillTaskCallback);
279
+ if (shouldRunSkillSandbox()) {
280
+ this.skillTaskCallback = async (payload: any) => {
281
+ await this.onQueueTask(payload);
282
+ };
283
+ (this as any).app.pubSubManager.subscribe('skill-hub.task', this.skillTaskCallback);
284
+ }
269
285
 
270
286
  // 5b. Subscribe PubSub — worker processes init-env tasks
271
- this.initEnvTaskCallback = async (payload: any) => {
272
- if (process.env.SKILL_HUB_SANDBOX === 'false') return;
273
- await this.workerEnvManager.executeInit(payload);
274
- };
275
- (this as any).app.pubSubManager.subscribe('skill-hub.init-env', this.initEnvTaskCallback);
287
+ if (shouldRunSkillSandbox()) {
288
+ this.initEnvTaskCallback = async (payload: any) => {
289
+ await this.workerEnvManager.executeInit(payload);
290
+ };
291
+ (this as any).app.pubSubManager.subscribe('skill-hub.init-env', this.initEnvTaskCallback);
292
+ }
276
293
 
277
294
  // 6. Register AI tools + subscriptions (deferred — after all plugins loaded)
278
- (this as any).app.on('afterStart', async () => {
279
- this.registerAITools();
280
- this.startCleanupInterval();
281
- this.startSkillTaskPoller();
282
- await this.subscribeInitEnvDone();
283
- // Ensure any newly added built-in skills are seeded automatically on upgrade/restart
284
- await this.skillManager.seedDefaults().catch((e) => {
285
- (this as any).app.logger.error(`[skill-hub] Failed to seed default skills: ${e.message}`);
286
- });
287
- });
295
+ (this as any).app.on('afterStart', this.afterStart);
288
296
  }
289
297
 
290
298
  private async onQueueTask(message: { id: string }) {
@@ -319,8 +327,14 @@ export class SkillHubSubFeature {
319
327
 
320
328
  private async claimSkillExecution(id: string): Promise<boolean> {
321
329
  const model = (this as any).db.getModel('skillExecutions');
330
+ const now = new Date();
322
331
  const [affected] = await model.update(
323
- { status: 'running' },
332
+ {
333
+ status: 'running',
334
+ startedAt: now,
335
+ heartbeatAt: now,
336
+ workerId: `${process.env.HOSTNAME || process.env.COMPUTERNAME || 'worker'}:${process.pid}`,
337
+ },
324
338
  {
325
339
  where: {
326
340
  id,
@@ -332,12 +346,12 @@ export class SkillHubSubFeature {
332
346
  }
333
347
 
334
348
  private startSkillTaskPoller() {
335
- if (process.env.SKILL_HUB_SANDBOX === 'false' || this.skillTaskPoller) {
349
+ if (!shouldRunSkillSandbox() || this.skillTaskPoller) {
336
350
  return;
337
351
  }
338
352
 
339
353
  const tick = () => {
340
- this.processPendingSkillExecutions().catch((error) => {
354
+ this.processSkillExecutionQueue().catch((error) => {
341
355
  (this as any).app.logger.warn(`[skill-hub] Pending task poll failed: ${error?.message || error}`);
342
356
  });
343
357
  };
@@ -347,6 +361,54 @@ export class SkillHubSubFeature {
347
361
  setTimeout(tick, 1000);
348
362
  }
349
363
 
364
+ private async processSkillExecutionQueue() {
365
+ await this.recoverStaleSkillExecutions();
366
+ await this.processPendingSkillExecutions();
367
+ }
368
+
369
+ private async recoverStaleSkillExecutions() {
370
+ const staleAfterMs = positiveInteger(process.env.SKILL_HUB_STALE_EXECUTION_MS, 120000);
371
+ const maxRetries = positiveInteger(process.env.SKILL_HUB_MAX_EXECUTION_RETRIES, 2);
372
+ const cutoff = new Date(Date.now() - staleAfterMs);
373
+ const repo = (this as any).db.getRepository('skillExecutions');
374
+ const model = (this as any).db.getModel('skillExecutions');
375
+ const staleExecutions = await repo.find({
376
+ filter: {
377
+ status: 'running',
378
+ $or: [{ heartbeatAt: null }, { heartbeatAt: { $lt: cutoff } }],
379
+ },
380
+ limit: SKILL_TASK_POLL_LIMIT,
381
+ });
382
+
383
+ for (const execution of staleExecutions) {
384
+ const id = execution.get('id');
385
+ const retryCount = Number(execution.get('retryCount') || 0);
386
+ const failed = retryCount >= maxRetries;
387
+ const Op = model.sequelize.Sequelize.Op;
388
+ const [affected] = await model.update(
389
+ {
390
+ status: failed ? 'failed' : 'pending',
391
+ retryCount: retryCount + 1,
392
+ workerId: null,
393
+ heartbeatAt: null,
394
+ stderr: failed ? `Execution abandoned after ${retryCount + 1} worker attempts.` : execution.get('stderr'),
395
+ },
396
+ {
397
+ where: {
398
+ id,
399
+ status: 'running',
400
+ [Op.or]: [{ heartbeatAt: null }, { heartbeatAt: { [Op.lt]: cutoff } }],
401
+ },
402
+ },
403
+ );
404
+ if (affected > 0) {
405
+ (this as any).app.logger.warn(
406
+ `[skill-hub] Recovered stale execution ${id}: ${failed ? 'failed' : 'returned to pending'}.`,
407
+ );
408
+ }
409
+ }
410
+ }
411
+
350
412
  private async processPendingSkillExecutions() {
351
413
  if (this.skillTaskPolling) return;
352
414
  this.skillTaskPolling = true;
@@ -788,6 +850,7 @@ export class SkillHubSubFeature {
788
850
  }
789
851
 
790
852
  private startCleanupInterval() {
853
+ if (this.cleanupInterval) return;
791
854
  // Check old execution files every hour, rate limiter every 5 minutes
792
855
  const CLEANUP_INTERVAL = 60 * 60 * 1000; // 1 hour
793
856
 
@@ -823,6 +886,8 @@ export class SkillHubSubFeature {
823
886
  }
824
887
 
825
888
  async beforeStop() {
889
+ (this as any).app.off?.('afterStart', this.afterStart);
890
+ (this as any).app.removeListener?.('afterStart', this.afterStart);
826
891
  // Unsubscribe PubSub
827
892
  if (this.skillTaskCallback) {
828
893
  try {
@@ -1014,3 +1079,21 @@ export class SkillHubSubFeature {
1014
1079
  }
1015
1080
 
1016
1081
  export default SkillHubSubFeature;
1082
+ export function shouldRunSkillSandbox(env: NodeJS.ProcessEnv = process.env): boolean {
1083
+ if (env.SKILL_HUB_SANDBOX === 'false') return false;
1084
+ const workerMode = String(env.WORKER_MODE || '').trim();
1085
+ if (!workerMode) return true;
1086
+ if (
1087
+ workerMode === '*' ||
1088
+ workerMode
1089
+ .split(',')
1090
+ .map((value) => value.trim())
1091
+ .includes('skill-hub:sandbox')
1092
+ ) {
1093
+ return true;
1094
+ }
1095
+ return workerMode
1096
+ .split(',')
1097
+ .map((value) => value.trim())
1098
+ .includes('skill-hub.task');
1099
+ }
@@ -57,9 +57,10 @@ export class SkillExecutionTask {
57
57
  async run() {
58
58
  const skill = this.execution.get('skill') || this.execution.skill;
59
59
  const execId = String(this.execution.get('id'));
60
-
61
- if (this.execution.get('status') !== 'running') {
62
- await this.execution.update({ status: 'running' });
60
+ const workerId = String(this.execution.get('workerId') || '');
61
+ if (this.execution.get('status') !== 'running' || !workerId) {
62
+ (this as any).app.logger.warn(`[skill-hub] Task ${execId}: refused execution without an active worker claim.`);
63
+ return;
63
64
  }
64
65
 
65
66
  // Set up abort controller — listens for cancel from main server via PubSub
@@ -69,6 +70,24 @@ export class SkillExecutionTask {
69
70
  (this as any).app.logger.info(`[skill-hub] Task ${execId}: received abort signal`);
70
71
  abortController.abort();
71
72
  };
73
+ const configuredHeartbeat = Number.parseInt(process.env.SKILL_HUB_HEARTBEAT_INTERVAL_MS || '', 10);
74
+ const heartbeatIntervalMs =
75
+ Number.isFinite(configuredHeartbeat) && configuredHeartbeat > 0 ? configuredHeartbeat : 10000;
76
+ const heartbeat = setInterval(() => {
77
+ this.updateIfOwned(execId, workerId, { heartbeatAt: new Date() })
78
+ .then((updated) => {
79
+ if (!updated) {
80
+ (this as any).app.logger.warn(`[skill-hub] Task ${execId}: worker claim was lost; aborting stale worker.`);
81
+ abortController.abort();
82
+ }
83
+ })
84
+ .catch((error: unknown) => {
85
+ (this as any).app.logger.warn(`[skill-hub] Task ${execId}: heartbeat update failed`, {
86
+ error: error instanceof Error ? error : new Error(String(error)),
87
+ });
88
+ });
89
+ }, heartbeatIntervalMs);
90
+ heartbeat.unref?.();
72
91
 
73
92
  try {
74
93
  // Subscribe to abort channel before starting execution
@@ -149,7 +168,10 @@ export class SkillExecutionTask {
149
168
  }
150
169
 
151
170
  const code = this.renderTemplate(rawCodeTemplate, inputArgs, execId, skillDir);
152
- await this.execution.update({ executedCode: code });
171
+ if (!(await this.updateIfOwned(execId, workerId, { executedCode: code }))) {
172
+ abortController.abort();
173
+ return;
174
+ }
153
175
 
154
176
  // Load package whitelist for import validation
155
177
  let packageWhitelist: string[] = [];
@@ -256,13 +278,19 @@ export class SkillExecutionTask {
256
278
  status = result.success ? 'succeeded' : 'failed';
257
279
  }
258
280
 
259
- await this.execution.update({
281
+ const completed = await this.updateIfOwned(execId, workerId, {
260
282
  status,
261
283
  stdout: result.stdout,
262
284
  stderr: result.stderr,
263
285
  outputFiles: stringifyJsonText(result.files, []),
264
286
  durationMs: result.durationMs,
265
287
  });
288
+ if (!completed) {
289
+ (this as any).app.logger.warn(
290
+ `[skill-hub] Task ${execId}: discarded result from a worker that lost its claim.`,
291
+ );
292
+ return;
293
+ }
266
294
 
267
295
  // Notify main server: task completed
268
296
  await (this as any).app.pubSubManager.publish(`skill-hub.done.${execId}`, {
@@ -285,10 +313,14 @@ export class SkillExecutionTask {
285
313
  } catch (error) {
286
314
  const errorMessage = error instanceof Error ? error.message : String(error);
287
315
 
288
- await this.execution.update({
316
+ const failed = await this.updateIfOwned(execId, workerId, {
289
317
  status: 'failed',
290
318
  stderr: errorMessage,
291
319
  });
320
+ if (!failed) {
321
+ (this as any).app.logger.warn(`[skill-hub] Task ${execId}: discarded error from a worker that lost its claim.`);
322
+ return;
323
+ }
292
324
 
293
325
  await (this as any).app.pubSubManager.publish(`skill-hub.done.${execId}`, {
294
326
  status: 'failed',
@@ -299,6 +331,7 @@ export class SkillExecutionTask {
299
331
 
300
332
  (this as any).app.logger.error(`[skill-hub] Execution ${execId} error: ${errorMessage}`);
301
333
  } finally {
334
+ clearInterval(heartbeat);
302
335
  // Always cleanup abort subscription
303
336
  try {
304
337
  await (this as any).app.pubSubManager.unsubscribe(abortChannel, abortCallback);
@@ -308,6 +341,18 @@ export class SkillExecutionTask {
308
341
  }
309
342
  }
310
343
 
344
+ private async updateIfOwned(execId: string, workerId: string, values: Record<string, unknown>): Promise<boolean> {
345
+ const model = (this as any).app.db.getModel('skillExecutions');
346
+ const [affected] = await model.update(values, {
347
+ where: {
348
+ id: execId,
349
+ status: 'running',
350
+ workerId,
351
+ },
352
+ });
353
+ return affected > 0;
354
+ }
355
+
311
356
  private renderTemplate(template: string, args: Record<string, any>, execId: string, skillDir?: string): string {
312
357
  let code = template;
313
358
  for (const [key, value] of Object.entries(args)) {
@@ -15,6 +15,7 @@ export function isOrchestratorToolName(name: string) {
15
15
  name === 'external_rag_search' ||
16
16
  name === 'skill_hub_execute' ||
17
17
  name.startsWith('skill_hub_') ||
18
+ name.startsWith('agent_loop_') ||
18
19
  name.startsWith('browser_') ||
19
20
  name.startsWith('drawio-')
20
21
  );