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
@@ -272,6 +272,45 @@ export class AgentLoopController {
272
272
  return this.getRunDetail(run.id);
273
273
  }
274
274
 
275
+ async approvePlan(
276
+ runId: string | number,
277
+ options: { userId?: string | number; reason?: string } = {},
278
+ ) {
279
+ const run = await this.repository.requireRun(runId);
280
+ if (TERMINAL_RUN_STATUSES.has(run.status)) {
281
+ throw new Error(`Agent loop run ${run.id} is already ${run.status}.`);
282
+ }
283
+ if (!['waiting_plan_approval', 'approved', 'needs_replan'].includes(run.status)) {
284
+ throw new Error(`Run ${run.id} is not waiting for plan approval.`);
285
+ }
286
+
287
+ await this.repository.updateRun(run.id, {
288
+ status: 'approved',
289
+ approvalStatus: 'approved',
290
+ approvedById: options.userId,
291
+ approvedAt: now(),
292
+ rejectionReason: '',
293
+ changeRequest: '',
294
+ updatedAt: now(),
295
+ });
296
+
297
+ await this.repository.createEvent({
298
+ runId: run.id,
299
+ type: 'plan_approved',
300
+ title: 'Plan approved',
301
+ content: options.reason || '',
302
+ status: 'approved',
303
+ userId: options.userId,
304
+ payload: { planVersion: run.planVersion || 1 },
305
+ });
306
+
307
+ return this.getRunDetail(run.id);
308
+ }
309
+
310
+ /**
311
+ * @deprecated Use approvePlan() instead. Auto-execution via AgentHarness is retired;
312
+ * step execution is now driven by the AI via agent_loop_update_step tools.
313
+ */
275
314
  async approvePlanAndExecute(
276
315
  runId: string | number,
277
316
  options: { userId?: string | number; ctx?: any; reason?: string } = {},
@@ -602,6 +641,11 @@ export class AgentLoopController {
602
641
  userId: options.userId,
603
642
  });
604
643
 
644
+ // Circuit breaker: record success for sub_agent steps
645
+ if (step.type === 'sub_agent' && step.target && run.leaderUsername) {
646
+ getCircuitBreaker().recordSuccess(subAgentCircuitKey(run.leaderUsername, step.target));
647
+ }
648
+
605
649
  return this.getRunSnapshot(run.id);
606
650
  }
607
651
 
@@ -644,6 +688,11 @@ export class AgentLoopController {
644
688
  },
645
689
  });
646
690
 
691
+ // Circuit breaker: record failure for sub_agent steps
692
+ if (step.type === 'sub_agent' && step.target && run.leaderUsername) {
693
+ getCircuitBreaker().recordFailure(subAgentCircuitKey(run.leaderUsername, step.target));
694
+ }
695
+
647
696
  return this.getRunSnapshot(run.id);
648
697
  }
649
698
 
@@ -786,7 +835,7 @@ export class AgentLoopController {
786
835
  });
787
836
 
788
837
  if (options.approved) {
789
- return this.executeApprovedPlan(run.id, { userId: options.userId, ctx: options.ctx });
838
+ return this.getRunSnapshot(run.id);
790
839
  }
791
840
  return this.getRunSnapshot(run.id);
792
841
  }
@@ -95,6 +95,10 @@ export class AgentLoopService {
95
95
  return this.controller.revisePlanGoal(runId, plan, options);
96
96
  }
97
97
 
98
+ async approvePlan(runId: any, options: any = {}) {
99
+ return this.controller.approvePlan(runId, options);
100
+ }
101
+
98
102
  async approvePlanAndExecute(runId: any, options: any = {}) {
99
103
  return this.controller.approvePlanAndExecute(runId, options);
100
104
  }
@@ -0,0 +1,64 @@
1
+ export type AgentRuntimeSource = 'chat-ui' | 'api' | 'workflow' | 'sub-agent';
2
+
3
+ export interface AgentRuntimeMessage {
4
+ role: string;
5
+ content: unknown;
6
+ }
7
+
8
+ export interface AgentRuntimeContext {
9
+ ctx: unknown;
10
+ source: AgentRuntimeSource;
11
+ employee: unknown;
12
+ sessionId: string;
13
+ userId?: number;
14
+ messages: AgentRuntimeMessage[];
15
+ metadata: Record<string, unknown>;
16
+ }
17
+
18
+ export interface AgentRuntimeResult {
19
+ succeeded: boolean;
20
+ value?: unknown;
21
+ error?: Error;
22
+ }
23
+
24
+ type BeforeRunHook = (context: AgentRuntimeContext) => Promise<void> | void;
25
+ type AfterRunHook = (context: AgentRuntimeContext, result: AgentRuntimeResult) => Promise<void> | void;
26
+
27
+ export class AgentRuntimeLifecycle {
28
+ private readonly beforeRunHooks = new Map<string, BeforeRunHook>();
29
+ private readonly afterRunHooks = new Map<string, AfterRunHook>();
30
+
31
+ constructor(
32
+ private readonly onHookError?: (hookType: 'beforeRun' | 'afterRun', name: string, error: Error) => void,
33
+ ) {}
34
+
35
+ registerBeforeRunHook(name: string, hook: BeforeRunHook): () => void {
36
+ this.beforeRunHooks.set(name, hook);
37
+ return () => this.beforeRunHooks.delete(name);
38
+ }
39
+
40
+ registerAfterRunHook(name: string, hook: AfterRunHook): () => void {
41
+ this.afterRunHooks.set(name, hook);
42
+ return () => this.afterRunHooks.delete(name);
43
+ }
44
+
45
+ async runBeforeHooks(context: AgentRuntimeContext): Promise<void> {
46
+ for (const [name, hook] of this.beforeRunHooks) {
47
+ try {
48
+ await hook(context);
49
+ } catch (error) {
50
+ this.onHookError?.('beforeRun', name, error instanceof Error ? error : new Error(String(error)));
51
+ }
52
+ }
53
+ }
54
+
55
+ async runAfterHooks(context: AgentRuntimeContext, result: AgentRuntimeResult): Promise<void> {
56
+ for (const [name, hook] of this.afterRunHooks) {
57
+ try {
58
+ await hook(context, result);
59
+ } catch (error) {
60
+ this.onHookError?.('afterRun', name, error instanceof Error ? error : new Error(String(error)));
61
+ }
62
+ }
63
+ }
64
+ }
@@ -1,110 +1,110 @@
1
- import { toPlain } from '../utils/ctx-utils';
2
-
3
- export const ORCHESTRATOR_TRACE_CONTEXT_KEY = '__orchestratorTraceContext';
4
-
5
- export type OrchestratorTraceContext = {
6
- rootRunId: string;
7
- spanId?: string;
8
- parentSpanId?: string;
9
- toolCallId?: string;
10
- leaderUsername?: string;
11
- employeeUsername?: string;
12
- toolName?: string;
13
- agentLoopRunId?: string;
14
- agentLoopStepId?: string;
15
- };
16
-
17
- type SpanValues = {
18
- rootRunId: string;
19
- parentSpanId?: string;
20
- source?: string;
21
- parentSessionId?: string;
22
- subSessionId?: string;
23
- toolCallId?: string;
24
- type: string;
25
- status: string;
26
- leaderUsername?: string;
27
- employeeUsername?: string;
28
- toolName?: string;
29
- title?: string;
30
- input?: any;
31
- output?: string;
32
- error?: string;
33
- durationMs?: number;
34
- startedAt?: Date;
35
- endedAt?: Date;
36
- orchestratorLogId?: string | number;
37
- skillExecutionId?: string | number;
38
- metadata?: any;
39
- userId?: string | number;
40
- };
41
-
42
- export class ExecutionSpanService {
43
- constructor(private readonly plugin: any) {}
44
-
45
- async create(values: SpanValues) {
46
- try {
47
- const repo = this.plugin.db.getRepository('agentExecutionSpans');
48
- if (!repo) return null;
49
-
50
- const record = await repo.create({
51
- values: {
52
- ...values,
53
- startedAt: values.startedAt || new Date(),
54
- createdAt: new Date(),
55
- },
56
- });
57
- return toPlain(record);
58
- } catch (error) {
59
- this.plugin.app.logger?.warn?.('[AgentOrchestrator] Failed to create execution span', error);
60
- return null;
61
- }
62
- }
63
-
64
- async update(spanId: string | number | undefined, values: Record<string, any>) {
65
- if (!spanId) return null;
66
- try {
67
- const repo = this.plugin.db.getRepository('agentExecutionSpans');
68
- if (!repo) return null;
69
- await repo.update({
70
- filterByTk: spanId,
71
- values: {
72
- ...values,
73
- updatedAt: new Date(),
74
- },
75
- });
76
- return { id: spanId };
77
- } catch (error) {
78
- this.plugin.app.logger?.warn?.(`[AgentOrchestrator] Failed to update execution span ${spanId}`, error);
79
- return null;
80
- }
81
- }
82
-
83
- async finish(
84
- spanId: string | number | undefined,
85
- status: 'success' | 'error' | 'canceled' | 'timeout',
86
- startedAt: number,
87
- values: Record<string, any> = {},
88
- ) {
89
- return this.update(spanId, {
90
- ...values,
91
- status,
92
- endedAt: new Date(),
93
- durationMs: Date.now() - startedAt,
94
- });
95
- }
96
- }
97
-
98
- export function getOrchestratorTraceContext(ctx: any): OrchestratorTraceContext | null {
99
- return (
100
- ctx?.[ORCHESTRATOR_TRACE_CONTEXT_KEY] ||
101
- ctx?.state?.orchestratorTraceContext ||
102
- ctx?.runtime?.context?.orchestratorTraceContext ||
103
- null
104
- );
105
- }
106
-
107
- export function setOrchestratorTraceContext(ctx: any, traceContext: OrchestratorTraceContext) {
108
- ctx[ORCHESTRATOR_TRACE_CONTEXT_KEY] = traceContext;
109
- return ctx;
110
- }
1
+ import { toPlain } from '../utils/ctx-utils';
2
+
3
+ export const ORCHESTRATOR_TRACE_CONTEXT_KEY = '__orchestratorTraceContext';
4
+
5
+ export type OrchestratorTraceContext = {
6
+ rootRunId: string;
7
+ spanId?: string;
8
+ parentSpanId?: string;
9
+ toolCallId?: string;
10
+ leaderUsername?: string;
11
+ employeeUsername?: string;
12
+ toolName?: string;
13
+ agentLoopRunId?: string;
14
+ agentLoopStepId?: string;
15
+ };
16
+
17
+ type SpanValues = {
18
+ rootRunId: string;
19
+ parentSpanId?: string;
20
+ source?: string;
21
+ parentSessionId?: string;
22
+ subSessionId?: string;
23
+ toolCallId?: string;
24
+ type: string;
25
+ status: string;
26
+ leaderUsername?: string;
27
+ employeeUsername?: string;
28
+ toolName?: string;
29
+ title?: string;
30
+ input?: any;
31
+ output?: string;
32
+ error?: string;
33
+ durationMs?: number;
34
+ startedAt?: Date;
35
+ endedAt?: Date;
36
+ orchestratorLogId?: string | number;
37
+ skillExecutionId?: string | number;
38
+ metadata?: any;
39
+ userId?: string | number;
40
+ };
41
+
42
+ export class ExecutionSpanService {
43
+ constructor(private readonly plugin: any) {}
44
+
45
+ async create(values: SpanValues) {
46
+ try {
47
+ const repo = this.plugin.db.getRepository('agentExecutionSpans');
48
+ if (!repo) return null;
49
+
50
+ const record = await repo.create({
51
+ values: {
52
+ ...values,
53
+ startedAt: values.startedAt || new Date(),
54
+ createdAt: new Date(),
55
+ },
56
+ });
57
+ return toPlain(record);
58
+ } catch (error) {
59
+ this.plugin.app.logger?.warn?.('[AgentOrchestrator] Failed to create execution span', error);
60
+ return null;
61
+ }
62
+ }
63
+
64
+ async update(spanId: string | number | undefined, values: Record<string, any>) {
65
+ if (!spanId) return null;
66
+ try {
67
+ const repo = this.plugin.db.getRepository('agentExecutionSpans');
68
+ if (!repo) return null;
69
+ await repo.update({
70
+ filterByTk: spanId,
71
+ values: {
72
+ ...values,
73
+ updatedAt: new Date(),
74
+ },
75
+ });
76
+ return { id: spanId };
77
+ } catch (error) {
78
+ this.plugin.app.logger?.warn?.(`[AgentOrchestrator] Failed to update execution span ${spanId}`, error);
79
+ return null;
80
+ }
81
+ }
82
+
83
+ async finish(
84
+ spanId: string | number | undefined,
85
+ status: 'success' | 'error' | 'canceled' | 'timeout',
86
+ startedAt: number,
87
+ values: Record<string, any> = {},
88
+ ) {
89
+ return this.update(spanId, {
90
+ ...values,
91
+ status,
92
+ endedAt: new Date(),
93
+ durationMs: Date.now() - startedAt,
94
+ });
95
+ }
96
+ }
97
+
98
+ export function getOrchestratorTraceContext(ctx: any): OrchestratorTraceContext | null {
99
+ return (
100
+ ctx?.[ORCHESTRATOR_TRACE_CONTEXT_KEY] ||
101
+ ctx?.state?.orchestratorTraceContext ||
102
+ ctx?.runtime?.context?.orchestratorTraceContext ||
103
+ null
104
+ );
105
+ }
106
+
107
+ export function setOrchestratorTraceContext(ctx: any, traceContext: OrchestratorTraceContext) {
108
+ ctx[ORCHESTRATOR_TRACE_CONTEXT_KEY] = traceContext;
109
+ return ctx;
110
+ }
@@ -1,4 +1,4 @@
1
- import { mkdirSync, readdirSync, statSync, existsSync, rmSync } from 'fs';
1
+ import { mkdirSync, readdirSync, statSync, existsSync, rmSync, chownSync } from 'fs';
2
2
  import { resolve, join } from 'path';
3
3
 
4
4
  export interface OutputFileInfo {
@@ -52,6 +52,21 @@ export class FileManager {
52
52
  return resolve(this.executionsDir, execId, 'output');
53
53
  }
54
54
 
55
+ /**
56
+ * Hand the execution directory to the dropped-privilege sandbox user so the
57
+ * child can read its script and write output. Requires root (chown), which
58
+ * matches the only case privilege drop is active.
59
+ */
60
+ chownExecDir(execId: string, uid: number, gid: number): void {
61
+ const dir = resolve(this.executionsDir, execId);
62
+ chownSync(dir, uid, gid);
63
+ // Direct children too (script + output dir): under a restrictive umask
64
+ // (e.g. 077) the root-written script would stay unreadable for the sandbox uid.
65
+ for (const entry of readdirSync(dir)) {
66
+ chownSync(resolve(dir, entry), uid, gid);
67
+ }
68
+ }
69
+
55
70
  /**
56
71
  * Returns absolute path to an output file, or null if invalid/not found.
57
72
  * Includes path traversal protection.