plugin-agent-orchestrator 1.1.3 → 1.1.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.
Files changed (218) hide show
  1. package/README.md +204 -114
  2. package/dist/client/index.js +1 -1
  3. package/dist/client-v2/117.7565cff57c39318a.js +10 -0
  4. package/dist/client-v2/214.106635011859dede.js +10 -0
  5. package/dist/client-v2/253.59577b40d9875be0.js +10 -0
  6. package/dist/client-v2/264.7ed8bee71fa5935c.js +10 -0
  7. package/dist/client-v2/{41.ba6e080cc0488143.js → 41.3deb9e57a0f64fc4.js} +1 -1
  8. package/dist/client-v2/418.41ed200e7d6da332.js +10 -0
  9. package/dist/client-v2/53.349fff8db80b19d4.js +10 -0
  10. package/dist/client-v2/619.0a2144aaffefe018.js +10 -0
  11. package/dist/client-v2/{70.bda9518881c05360.js → 70.ffff2997c10fd4b0.js} +1 -1
  12. package/dist/client-v2/{794.9bbb8d3a61b2bdf2.js → 794.a67a2614405ba627.js} +1 -1
  13. package/dist/client-v2/981.e4a1f41cfc94a856.js +10 -0
  14. package/dist/client-v2/{993.82778b6fd6db3cb1.js → 993.b0688fdc5ccb000a.js} +1 -1
  15. package/dist/client-v2/index.js +1 -1
  16. package/dist/externalVersion.js +8 -7
  17. package/dist/locale/en-US.json +199 -123
  18. package/dist/locale/vi-VN.json +199 -123
  19. package/dist/locale/zh-CN.json +199 -123
  20. package/dist/server/collections/agent-execution-spans.js +55 -0
  21. package/dist/server/collections/agent-harness-profile-versions.js +56 -0
  22. package/dist/server/collections/agent-harness-profiles.js +12 -0
  23. package/dist/server/collections/agent-loop-action-approvals.js +69 -0
  24. package/dist/server/collections/agent-loop-artifacts.js +55 -0
  25. package/dist/server/collections/agent-loop-circuit-states.js +53 -0
  26. package/dist/server/collections/agent-loop-control-settings.js +51 -0
  27. package/dist/server/collections/agent-loop-events.js +27 -0
  28. package/dist/server/collections/agent-loop-path-locks.js +57 -0
  29. package/dist/server/collections/agent-loop-patterns.js +72 -0
  30. package/dist/server/collections/agent-loop-runs.js +170 -3
  31. package/dist/server/collections/agent-loop-steps.js +76 -0
  32. package/dist/server/collections/agent-loop-usage-buckets.js +52 -0
  33. package/dist/server/collections/agent-loop-worktrees.js +55 -0
  34. package/dist/server/collections/skill-executions.js +8 -0
  35. package/dist/server/migrations/{20260427000000-change-packages-to-text.js → 20260427000002-change-packages-to-text.js} +2 -0
  36. package/dist/server/migrations/20260429000000-add-llm-fields.js +3 -2
  37. package/dist/server/migrations/{20260429000000-fix-inputargs-json-to-text.js → 20260429000001-fix-inputargs-json-to-text.js} +14 -6
  38. package/dist/server/migrations/20260503000000-add-orchestrator-trace-fields.js +2 -1
  39. package/dist/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.js +2 -1
  40. package/dist/server/migrations/20260803010000-backfill-loop-control-plane-data.js +262 -0
  41. package/dist/server/plugin.js +54 -150
  42. package/dist/server/resources/agent-harness-profile.js +117 -0
  43. package/dist/server/resources/agent-knowledge-insights.js +1 -3
  44. package/dist/server/resources/agent-loop-approval.js +108 -0
  45. package/dist/server/resources/agent-loop-artifact.js +86 -0
  46. package/dist/server/resources/agent-loop-events-stream.js +130 -0
  47. package/dist/server/resources/agent-loop.js +183 -139
  48. package/dist/server/resources/resource-helpers.js +63 -0
  49. package/dist/server/resources/tracing.js +121 -107
  50. package/dist/server/services/AgentMemoryContextService.js +34 -0
  51. package/dist/server/services/DurableCircuitBreakerService.js +229 -0
  52. package/dist/server/services/HarnessCompiler.js +175 -0
  53. package/dist/server/services/HarnessProfileService.js +233 -0
  54. package/dist/server/services/HarnessSchema.js +194 -0
  55. package/dist/server/services/LoopBudgetService.js +269 -0
  56. package/dist/server/services/LoopControlService.js +139 -0
  57. package/dist/server/services/LoopPatternSchema.js +195 -0
  58. package/dist/server/services/LoopPatternService.js +132 -0
  59. package/dist/server/services/LoopRunRepository.js +285 -0
  60. package/dist/server/services/LoopRunStateMachine.js +478 -0
  61. package/dist/server/services/LoopSchedulerService.js +193 -0
  62. package/dist/server/services/LoopTriggerService.js +165 -0
  63. package/dist/server/services/LoopWorkerService.js +699 -0
  64. package/dist/server/services/NativeSubAgentObserver.js +165 -23
  65. package/dist/server/services/PathLockService.js +288 -0
  66. package/dist/server/services/PluginAiRuntimeAdapter.js +226 -0
  67. package/dist/server/services/ToolLoopDetectionService.js +210 -0
  68. package/dist/server/services/VerificationSchema.js +59 -0
  69. package/dist/server/services/VerificationService.js +235 -0
  70. package/dist/server/services/harness-runtime-policy.js +139 -0
  71. package/dist/server/skill-hub/actions/git-import.js +13 -7
  72. package/dist/server/skill-hub/plugin.js +36 -3
  73. package/dist/server/skill-hub/services/RegistrySkillSnapshotService.js +1 -5
  74. package/dist/server/skill-hub/tasks/SkillExecutionTask.js +29 -0
  75. package/dist/server/utils/ensure-indexes.js +111 -0
  76. package/dist/server/utils/loop-pattern-context.js +56 -0
  77. package/package.json +53 -53
  78. package/src/client/ApprovalsTab.tsx +2 -0
  79. package/src/client/OrchestratorSettings.tsx +55 -0
  80. package/src/client/skill-hub/components/GitSkillImport.tsx +3 -3
  81. package/src/client/skill-hub/components/SkillMetrics.tsx +15 -10
  82. package/src/client/skill-hub/index.tsx +0 -52
  83. package/src/client-v2/components/AgentRunsTab.tsx +100 -18
  84. package/src/client-v2/components/ApprovalsTab.tsx +337 -0
  85. package/src/client-v2/components/HarnessProfilesTab.tsx +385 -100
  86. package/src/client-v2/components/MemoryInspectorTab.tsx +141 -125
  87. package/src/client-v2/components/TracingTab.tsx +160 -77
  88. package/src/client-v2/hooks/useApiRequest.ts +28 -8
  89. package/src/client-v2/pages/ApprovalsPage.tsx +10 -0
  90. package/src/client-v2/plugin.tsx +6 -6
  91. package/src/client-v2/skill-hub/components/SkillMetrics.tsx +11 -6
  92. package/src/locale/en-US.json +199 -123
  93. package/src/locale/vi-VN.json +199 -123
  94. package/src/locale/zh-CN.json +199 -123
  95. package/src/server/__tests__/circuit-breaker.test.ts +248 -169
  96. package/src/server/__tests__/harness-compiler.test.ts +214 -0
  97. package/src/server/__tests__/harness-profile-service.test.ts +167 -0
  98. package/src/server/__tests__/harness-runtime-policy.test.ts +293 -0
  99. package/src/server/__tests__/harness-tool-plan.test.ts +172 -0
  100. package/src/server/__tests__/loop-approval-decision.test.ts +512 -0
  101. package/src/server/__tests__/loop-approval-expiry.test.ts +258 -0
  102. package/src/server/__tests__/loop-budget-service.test.ts +255 -0
  103. package/src/server/__tests__/loop-control-plane-backfill.test.ts +266 -0
  104. package/src/server/__tests__/loop-pattern-schema.test.ts +73 -0
  105. package/src/server/__tests__/loop-pattern-service.test.ts +148 -0
  106. package/src/server/__tests__/loop-run-state-machine.test.ts +101 -0
  107. package/src/server/__tests__/loop-trigger-service.test.ts +148 -0
  108. package/src/server/__tests__/loop-worker-claim.test.ts +341 -0
  109. package/src/server/__tests__/loop-worker-execution.test.ts +317 -0
  110. package/src/server/__tests__/native-sub-agent-observer.test.ts +288 -4
  111. package/src/server/__tests__/path-lock-service.test.ts +254 -0
  112. package/src/server/__tests__/skill-execution-task-policy.test.ts +140 -0
  113. package/src/server/__tests__/smoke.test.ts +162 -121
  114. package/src/server/__tests__/tool-loop-detection.test.ts +275 -0
  115. package/src/server/collections/agent-execution-spans.ts +55 -0
  116. package/src/server/collections/agent-harness-profile-versions.ts +26 -0
  117. package/src/server/collections/agent-harness-profiles.ts +71 -59
  118. package/src/server/collections/agent-loop-action-approvals.ts +39 -0
  119. package/src/server/collections/agent-loop-artifacts.ts +25 -0
  120. package/src/server/collections/agent-loop-circuit-states.ts +23 -0
  121. package/src/server/collections/agent-loop-control-settings.ts +21 -0
  122. package/src/server/collections/agent-loop-events.ts +98 -71
  123. package/src/server/collections/agent-loop-path-locks.ts +27 -0
  124. package/src/server/collections/agent-loop-patterns.ts +42 -0
  125. package/src/server/collections/agent-loop-runs.ts +170 -3
  126. package/src/server/collections/agent-loop-steps.ts +76 -0
  127. package/src/server/collections/agent-loop-usage-buckets.ts +22 -0
  128. package/src/server/collections/agent-loop-worktrees.ts +25 -0
  129. package/src/server/collections/skill-executions.ts +8 -0
  130. package/src/server/migrations/{20260427000000-change-packages-to-text.ts → 20260427000002-change-packages-to-text.ts} +52 -49
  131. package/src/server/migrations/20260429000000-add-llm-fields.ts +47 -46
  132. package/src/server/migrations/{20260429000000-fix-inputargs-json-to-text.ts → 20260429000001-fix-inputargs-json-to-text.ts} +45 -38
  133. package/src/server/migrations/20260503000000-add-orchestrator-trace-fields.ts +33 -32
  134. package/src/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.ts +31 -30
  135. package/src/server/migrations/20260803010000-backfill-loop-control-plane-data.ts +293 -0
  136. package/src/server/plugin.ts +55 -167
  137. package/src/server/resources/agent-harness-profile.ts +94 -0
  138. package/src/server/resources/agent-knowledge-insights.ts +5 -3
  139. package/src/server/resources/agent-loop-approval.ts +84 -0
  140. package/src/server/resources/agent-loop-artifact.ts +56 -0
  141. package/src/server/resources/agent-loop-events-stream.ts +105 -0
  142. package/src/server/resources/agent-loop.ts +204 -143
  143. package/src/server/resources/agent-monitor.ts +482 -482
  144. package/src/server/resources/resource-helpers.ts +33 -0
  145. package/src/server/resources/tracing.ts +266 -258
  146. package/src/server/services/AgentMemoryContextService.ts +44 -0
  147. package/src/server/services/DurableCircuitBreakerService.ts +239 -0
  148. package/src/server/services/ExecutionSpanService.ts +2 -0
  149. package/src/server/services/HarnessCompiler.ts +170 -0
  150. package/src/server/services/HarnessProfileService.ts +239 -0
  151. package/src/server/services/HarnessSchema.ts +194 -0
  152. package/src/server/services/LoopBudgetService.ts +275 -0
  153. package/src/server/services/LoopControlService.ts +124 -0
  154. package/src/server/services/LoopPatternSchema.ts +185 -0
  155. package/src/server/services/LoopPatternService.ts +143 -0
  156. package/src/server/services/LoopRunRepository.ts +336 -0
  157. package/src/server/services/LoopRunStateMachine.ts +529 -0
  158. package/src/server/services/LoopSchedulerService.ts +199 -0
  159. package/src/server/services/LoopTriggerService.ts +157 -0
  160. package/src/server/services/LoopWorkerService.ts +822 -0
  161. package/src/server/services/NativeSubAgentObserver.ts +186 -21
  162. package/src/server/services/PathLockService.ts +321 -0
  163. package/src/server/services/PluginAiRuntimeAdapter.ts +332 -0
  164. package/src/server/services/ToolLoopDetectionService.ts +213 -0
  165. package/src/server/services/VerificationSchema.ts +31 -0
  166. package/src/server/services/VerificationService.ts +264 -0
  167. package/src/server/services/harness-runtime-policy.ts +141 -0
  168. package/src/server/skill-hub/actions/__tests__/git-import.test.ts +15 -1
  169. package/src/server/skill-hub/actions/git-import.ts +9 -3
  170. package/src/server/skill-hub/plugin.ts +41 -3
  171. package/src/server/skill-hub/services/RegistrySkillSnapshotService.ts +235 -239
  172. package/src/server/skill-hub/services/__tests__/RegistrySkillSnapshotService.test.ts +61 -63
  173. package/src/server/skill-hub/tasks/SkillExecutionTask.ts +43 -2
  174. package/src/server/utils/ensure-indexes.ts +96 -0
  175. package/src/server/utils/logging.ts +62 -63
  176. package/src/server/utils/loop-pattern-context.ts +20 -0
  177. package/dist/client-v2/214.79650a549273f163.js +0 -10
  178. package/dist/client-v2/253.9f0537713cee9e6c.js +0 -10
  179. package/dist/client-v2/264.718a107e43fc163c.js +0 -10
  180. package/dist/client-v2/373.f5d5292e53c4e832.js +0 -10
  181. package/dist/client-v2/418.ce8f5b2b8a229d1d.js +0 -10
  182. package/dist/client-v2/53.7c29c28043e9112d.js +0 -10
  183. package/dist/client-v2/619.bd3c5698b40705c3.js +0 -10
  184. package/dist/client-v2/677.a991ce0250ff5c77.js +0 -10
  185. package/dist/client-v2/925.f5370de8f6632d65.js +0 -10
  186. package/dist/server/services/AgentHarness.js +0 -81
  187. package/dist/server/services/AgentLoopController.js +0 -1023
  188. package/dist/server/services/AgentLoopRepository.js +0 -192
  189. package/dist/server/services/AgentLoopService.js +0 -142
  190. package/dist/server/services/AgentPlanValidator.js +0 -99
  191. package/dist/server/services/AgentPlannerService.js +0 -99
  192. package/dist/server/services/CircuitBreaker.js +0 -125
  193. package/dist/server/services/ContextAggregator.js +0 -201
  194. package/dist/server/services/PlanningPromptService.js +0 -95
  195. package/dist/server/tools/agent-loop.js +0 -373
  196. package/dist/server/tools/orchestrator-plan.js +0 -254
  197. package/src/client/AIEmployeeSelect.tsx +0 -47
  198. package/src/client/AIEmployeesContext.tsx +0 -102
  199. package/src/client/RulesTab.tsx +0 -2
  200. package/src/client/TracingTab.tsx +0 -436
  201. package/src/client/skill-hub/components/ExecutionProgress.tsx +0 -51
  202. package/src/client-v2/components/RulesTab.tsx +0 -2
  203. package/src/client-v2/pages/AgentRunsPage.tsx +0 -13
  204. package/src/server/__tests__/agent-loop-controller.test.ts +0 -375
  205. package/src/server/__tests__/context-aggregator.test.ts +0 -222
  206. package/src/server/__tests__/parallel-execution.test.ts +0 -318
  207. package/src/server/__tests__/planning-prompt-service.test.ts +0 -23
  208. package/src/server/services/AgentHarness.ts +0 -67
  209. package/src/server/services/AgentLoopController.ts +0 -1263
  210. package/src/server/services/AgentLoopRepository.ts +0 -178
  211. package/src/server/services/AgentLoopService.ts +0 -177
  212. package/src/server/services/AgentPlanValidator.ts +0 -73
  213. package/src/server/services/AgentPlannerService.ts +0 -80
  214. package/src/server/services/CircuitBreaker.ts +0 -126
  215. package/src/server/services/ContextAggregator.ts +0 -239
  216. package/src/server/services/PlanningPromptService.ts +0 -72
  217. package/src/server/tools/agent-loop.ts +0 -360
  218. package/src/server/tools/orchestrator-plan.ts +0 -255
@@ -0,0 +1,332 @@
1
+ import type { Model } from '@nocobase/database';
2
+ import type { Application } from '@nocobase/server';
3
+ import { AIEmployee } from '@nocobase/plugin-ai/server';
4
+ import { listSystemTools } from '@nocobase/ai';
5
+ import { getAIToolsManager } from '../utils/ai-manager';
6
+ import { decideTool } from './HarnessCompiler';
7
+ import type { CompiledHarness } from './HarnessCompiler';
8
+
9
+ // plugin-ai keeps every tool whose name is in `listSystemTools()` regardless of the constructor
10
+ // tool filter (ai-employee.ts getAIEmployeeTools). The harness `filter` cannot remove them, so a
11
+ // denied system tool would still reach the model. The adapter neutralizes them at their source
12
+ // instead: web search via the `webSearch` constructor flag, knowledge-base retrieval via the
13
+ // in-memory `enableKnowledgeBase` employee flag. This maps each system tool to the switch that
14
+ // stops plugin-ai from ever adding it.
15
+ const SYSTEM_TOOL_WEB_SEARCH = 'subAgentWebSearch';
16
+ const SYSTEM_TOOL_KNOWLEDGE_BASE = 'knowledge-base-retrieve';
17
+
18
+ export type LoopRole = 'leader' | 'maker' | 'verifier';
19
+
20
+ export type ToolCallDecision = {
21
+ type: 'approve' | 'edit' | 'reject';
22
+ message?: string;
23
+ editedAction?: { name: string; args: unknown };
24
+ };
25
+
26
+ export type InterruptedToolCall = {
27
+ toolCallId: string;
28
+ toolName: string;
29
+ args: unknown;
30
+ interruptId: string;
31
+ };
32
+
33
+ export type InvocationOutcome = {
34
+ sessionId: string;
35
+ messageId: string;
36
+ interrupted: InterruptedToolCall[];
37
+ content: string;
38
+ };
39
+
40
+ type ConversationsManager = {
41
+ create(input: {
42
+ userId?: number;
43
+ aiEmployee: { username: string };
44
+ title?: string;
45
+ options?: Record<string, unknown>;
46
+ category?: string;
47
+ }): Promise<Model>;
48
+ getUserDecisions(messageId: string): Promise<{ interruptId?: string; decisions: unknown[] } | undefined>;
49
+ };
50
+
51
+ type EmployeesManager = {
52
+ getEmployee(username: string): Promise<Model | null>;
53
+ resolveModel(employee: Model, model?: unknown): Promise<{ llmService: string; model: string }>;
54
+ };
55
+
56
+ type PluginAi = {
57
+ aiConversationsManager: ConversationsManager;
58
+ aiEmployeesManager: EmployeesManager;
59
+ };
60
+
61
+ function read(record: Model | Record<string, unknown>, key: string) {
62
+ const model = record as Model & { get?: (name: string) => unknown };
63
+ return typeof model.get === 'function' ? model.get(key) : (record as Record<string, unknown>)[key];
64
+ }
65
+
66
+ function textContent(messages: unknown): string {
67
+ if (!Array.isArray(messages)) return '';
68
+ const last = messages[messages.length - 1] as { content?: unknown } | undefined;
69
+ if (typeof last?.content === 'string') return last.content;
70
+ if (Array.isArray(last?.content)) {
71
+ return last.content
72
+ .map((part) =>
73
+ typeof part === 'object' && part && 'text' in part ? String((part as { text: unknown }).text) : '',
74
+ )
75
+ .join('');
76
+ }
77
+ return '';
78
+ }
79
+
80
+ function interruptedFrom(result: Record<string, unknown>): InterruptedToolCall[] {
81
+ const interrupts = result.__interrupt__;
82
+ if (!Array.isArray(interrupts)) return [];
83
+ const calls: InterruptedToolCall[] = [];
84
+ for (const interrupt of interrupts) {
85
+ const value = (interrupt as { value?: unknown }).value ?? interrupt;
86
+ const record = value as Record<string, unknown>;
87
+ const toolCallId = String(record.toolCallId || record.tool_call_id || '');
88
+ if (!toolCallId) continue;
89
+ calls.push({
90
+ toolCallId,
91
+ toolName: String(record.name || record.toolName || ''),
92
+ args: record.args ?? record.input ?? {},
93
+ interruptId: String((interrupt as { id?: unknown }).id || record.interruptId || ''),
94
+ });
95
+ }
96
+ return calls;
97
+ }
98
+
99
+ // plugin-ai reads the compiled harness through two different channels, and they disagree on shape:
100
+ //
101
+ // 1. `skillSettings.tools` on the AIEmployee constructor is a `string[]` name filter once
102
+ // `toolsVersion` is set (ai-employee.ts `getAIEmployeeTools`).
103
+ // 2. `employee.skillSettings.tools` on the persisted record is a `{ name, autoCall }[]` that both
104
+ // widens the candidate tool set and decides whether a call interrupts (`isAutoCall`).
105
+ //
106
+ // Passing objects to (1) silently filtered out every tool, because `string[].includes(object)` is
107
+ // always false. Both channels are populated here, from the same decision, so a tool is exposed only
108
+ // when the harness allows it and interrupts whenever the harness says `ask`.
109
+ export type HarnessToolPlan = {
110
+ filter: string[];
111
+ employeeTools: Array<{ name: string; autoCall: boolean }>;
112
+ withheld: Array<{ name: string; reason: string }>;
113
+ // System tools the harness does not grant. They ignore `filter`, so the caller has to disable
114
+ // the feature that injects them rather than relying on the filter to drop them.
115
+ blockedSystemTools: string[];
116
+ };
117
+
118
+ type ToolEntry = { scope?: string; defaultPermission?: string } | undefined;
119
+
120
+ // `isAutoCall` only consults the employee override for CUSTOM-scope tools; every other scope is
121
+ // governed by the tool's own `defaultPermission`. So an `ask` decision is only enforceable when one
122
+ // of those two is true. When it is not, the tool is withheld rather than exposed un-gated.
123
+ function askIsEnforceable(entry: ToolEntry) {
124
+ return entry?.scope === 'CUSTOM' || entry?.defaultPermission !== 'ALLOW';
125
+ }
126
+
127
+ // Tool discovery must run with the same context plugin-ai uses. `DefaultToolsManager.getTools`
128
+ // only consults dynamic providers when a filter is supplied, and the MCP provider registers a
129
+ // user's context-scoped tools solely when `filter.ctx` is present. Discovering without it reports
130
+ // every MCP tool as unregistered even though `AIEmployee.getToolsMap()` would find it at call time.
131
+ export async function planHarnessTools(
132
+ app: Application,
133
+ harness: CompiledHarness,
134
+ discovery?: { ctx?: unknown; sessionId?: string },
135
+ ): Promise<HarnessToolPlan> {
136
+ const toolsManager = getAIToolsManager(app);
137
+ // Escalatable tools join the plan as ask-gated candidates: the model can invoke them, but every
138
+ // call interrupts and parks the run until a human approves that single call.
139
+ const candidates = Array.from(new Set([...harness.tools.allow, ...harness.tools.ask, ...harness.tools.escalate]));
140
+ const plan: HarnessToolPlan = { filter: [], employeeTools: [], withheld: [], blockedSystemTools: [] };
141
+ const filter = discovery ? { ctx: discovery.ctx, sessionId: discovery.sessionId } : undefined;
142
+
143
+ for (const name of candidates.sort()) {
144
+ const decision = decideTool(harness, name);
145
+ if (decision === 'deny') {
146
+ plan.withheld.push({ name, reason: 'denied by harness' });
147
+ continue;
148
+ }
149
+ const entry = (await toolsManager.getTools(name, filter)) as ToolEntry;
150
+ if (!entry) {
151
+ plan.withheld.push({ name, reason: 'not registered with plugin-ai' });
152
+ continue;
153
+ }
154
+ if (decision === 'ask' && !askIsEnforceable(entry)) {
155
+ plan.withheld.push({ name, reason: 'approval cannot be enforced for this tool' });
156
+ continue;
157
+ }
158
+ plan.filter.push(name);
159
+ plan.employeeTools.push({ name, autoCall: decision === 'allow' });
160
+ }
161
+
162
+ const granted = new Set(plan.filter);
163
+ plan.blockedSystemTools = listSystemTools()
164
+ .filter((name) => !granted.has(name))
165
+ .sort();
166
+ return plan;
167
+ }
168
+
169
+ export class PluginAiRuntimeAdapter {
170
+ constructor(private readonly app: Application) {}
171
+
172
+ private plugin(): PluginAi {
173
+ const plugin = this.app.pm.get('ai') as unknown as PluginAi | undefined;
174
+ if (!plugin?.aiEmployeesManager || !plugin?.aiConversationsManager) {
175
+ throw new Error('The official @nocobase/plugin-ai plugin is required to execute Loop Control Plane runs.');
176
+ }
177
+ return plugin;
178
+ }
179
+
180
+ async createConversation(input: { username: string; userId?: number; runId: number; title: string }) {
181
+ const conversation = await this.plugin().aiConversationsManager.create({
182
+ userId: input.userId,
183
+ aiEmployee: { username: input.username },
184
+ title: input.title,
185
+ category: 'task',
186
+ options: { controlPlaneRunId: input.runId },
187
+ });
188
+ const sessionId = String(read(conversation, 'sessionId') || '');
189
+ if (!sessionId) throw new Error('The AI conversation was created without a session identifier.');
190
+ return sessionId;
191
+ }
192
+
193
+ async invoke(input: {
194
+ username: string;
195
+ sessionId: string;
196
+ userId?: number;
197
+ systemMessage: string;
198
+ harness: CompiledHarness;
199
+ prompt: string;
200
+ signal: AbortSignal;
201
+ }): Promise<InvocationOutcome> {
202
+ const { aiEmployee } = await this.buildEmployee(input);
203
+
204
+ const result = (await aiEmployee.invoke({
205
+ userMessages: [{ role: 'user', content: input.prompt }],
206
+ signal: input.signal,
207
+ })) as Record<string, unknown>;
208
+
209
+ return {
210
+ sessionId: input.sessionId,
211
+ messageId: String(result.messageId || ''),
212
+ interrupted: interruptedFrom(result),
213
+ content: textContent(result.messages),
214
+ };
215
+ }
216
+
217
+ // Resume mirrors the official workflow employee node: flip the persisted tool call to
218
+ // `waiting` with a decision, then re-invoke so plugin-ai resumes its own checkpoint.
219
+ // Decisions are per tool call: plugin-ai stores one `userDecision` per interrupted row and
220
+ // `getUserDecisions` only unblocks once every interrupted call of the message has one.
221
+ async resume(input: {
222
+ username: string;
223
+ sessionId: string;
224
+ messageId: string;
225
+ userId?: number;
226
+ systemMessage: string;
227
+ harness: CompiledHarness;
228
+ decisions: Array<{ toolCallId: string; decision: ToolCallDecision }>;
229
+ signal: AbortSignal;
230
+ }): Promise<InvocationOutcome> {
231
+ const plugin = this.plugin();
232
+
233
+ for (const entry of input.decisions) {
234
+ await this.app.db.getModel('aiToolMessages').update(
235
+ { userDecision: entry.decision, invokeStatus: 'waiting' },
236
+ {
237
+ where: {
238
+ sessionId: input.sessionId,
239
+ messageId: input.messageId,
240
+ toolCallId: entry.toolCallId,
241
+ invokeStatus: 'interrupted',
242
+ },
243
+ },
244
+ );
245
+ }
246
+ const userDecisions = await plugin.aiConversationsManager.getUserDecisions(input.messageId);
247
+ if (!userDecisions) throw new Error('The interrupted tool calls are not all ready to resume.');
248
+
249
+ const { aiEmployee } = await this.buildEmployee(input);
250
+
251
+ const result = (await aiEmployee.invoke({
252
+ messageId: input.messageId,
253
+ userDecisions: userDecisions as { interruptId?: string; decisions: never[] },
254
+ signal: input.signal,
255
+ })) as Record<string, unknown>;
256
+
257
+ return {
258
+ sessionId: input.sessionId,
259
+ messageId: String(result.messageId || input.messageId),
260
+ interrupted: interruptedFrom(result),
261
+ content: textContent(result.messages),
262
+ };
263
+ }
264
+
265
+ // The employee model is re-read per invocation and mutated only in memory. `isAutoCall` reads
266
+ // `employee.skillSettings` off the Sequelize dataValues, so `set()` without `save()` gates this
267
+ // run's tool calls without touching the shared employee configuration.
268
+ private async buildEmployee(input: {
269
+ username: string;
270
+ sessionId: string;
271
+ userId?: number;
272
+ systemMessage: string;
273
+ harness: CompiledHarness;
274
+ }) {
275
+ const plugin = this.plugin();
276
+ const employee = await plugin.aiEmployeesManager.getEmployee(input.username);
277
+ if (!employee) throw new Error(`AI employee "${input.username}" was not found.`);
278
+ const model = await plugin.aiEmployeesManager.resolveModel(employee);
279
+ const currentRoles = await this.rolesFor(input.userId);
280
+
281
+ const ctx = {
282
+ app: this.app,
283
+ db: this.app.db,
284
+ log: this.app.log,
285
+ logger: this.app.log,
286
+ state: { currentRoles },
287
+ auth: { user: { id: input.userId } },
288
+ action: { params: { values: { sessionId: input.sessionId, model } } },
289
+ };
290
+
291
+ // Discovery runs with the same ctx the invocation will use, so context-scoped MCP tools
292
+ // resolve here exactly as they will when plugin-ai builds its tool map.
293
+ const plan = await planHarnessTools(this.app, input.harness, { ctx, sessionId: input.sessionId });
294
+
295
+ if (plan.withheld.length) {
296
+ this.app.logger.info(
297
+ `[AgentOrchestrator] Harness withheld ${plan.withheld.length} tool(s) from ${input.username}.`,
298
+ { withheld: plan.withheld },
299
+ );
300
+ }
301
+
302
+ const persisted = (employee.get('skillSettings') || {}) as Record<string, unknown>;
303
+ employee.set('skillSettings', { ...persisted, tools: plan.employeeTools });
304
+
305
+ // plugin-ai adds `knowledge-base-retrieve` whenever the employee has knowledge base enabled,
306
+ // and keeps it through the tool filter because it is a system tool. Clearing the in-memory flag
307
+ // is the only way a harness that does not grant it actually keeps it out of the model's hands.
308
+ const blockedSystemTools = new Set(plan.blockedSystemTools);
309
+ if (blockedSystemTools.has(SYSTEM_TOOL_KNOWLEDGE_BASE)) {
310
+ employee.set('enableKnowledgeBase', false);
311
+ }
312
+
313
+ const aiEmployee = new AIEmployee({
314
+ ctx: ctx as never,
315
+ employee,
316
+ sessionId: input.sessionId,
317
+ systemMessage: input.systemMessage,
318
+ skillSettings: { skillsVersion: 2, toolsVersion: 2, skills: [], tools: plan.filter },
319
+ // Same reasoning for web search: the flag is what injects the system tool, so it is only
320
+ // ever enabled when the harness explicitly granted it.
321
+ webSearch: !blockedSystemTools.has(SYSTEM_TOOL_WEB_SEARCH),
322
+ model,
323
+ });
324
+ return { aiEmployee, plan };
325
+ }
326
+
327
+ private async rolesFor(userId?: number) {
328
+ if (!userId) return [];
329
+ const rows = await this.app.db.getRepository('rolesUsers').find({ filter: { userId } });
330
+ return rows.map((row) => String(read(row, 'roleName') || '')).filter(Boolean);
331
+ }
332
+ }
@@ -0,0 +1,213 @@
1
+ import { createHash } from 'node:crypto';
2
+ import type { Database, Model } from '@nocobase/database';
3
+ import { getRunEventBus } from './RunEventBus';
4
+ import type { LoopPatternPolicy } from './LoopPatternSchema';
5
+
6
+ export type ToolLoopLevel = 'none' | 'warn' | 'block' | 'escalate';
7
+
8
+ export type ToolLoopFinding = {
9
+ level: Exclude<ToolLoopLevel, 'none'>;
10
+ toolName: string;
11
+ signature: string;
12
+ count: number;
13
+ sampleArgs: Record<string, unknown>;
14
+ };
15
+
16
+ export type ToolCallEntry = {
17
+ name: string;
18
+ args: unknown;
19
+ };
20
+
21
+ export type ToolLoopRecordContext = {
22
+ runId: number;
23
+ role: string;
24
+ username: string;
25
+ runStatus: string;
26
+ actorType: string;
27
+ actorIdentity: string;
28
+ };
29
+
30
+ function read(record: Model | Record<string, unknown>, key: string) {
31
+ const model = record as Model & { get?: (name: string) => unknown };
32
+ return typeof model.get === 'function' ? model.get(key) : (record as Record<string, unknown>)[key];
33
+ }
34
+
35
+ function asObject(value: unknown): Record<string, unknown> {
36
+ return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
37
+ }
38
+
39
+ // Key order alone must not change the signature, so object keys are sorted before hashing.
40
+ function canonicalize(value: unknown): unknown {
41
+ if (Array.isArray(value)) return value.map((entry) => canonicalize(entry));
42
+ if (value && typeof value === 'object') {
43
+ const source = value as Record<string, unknown>;
44
+ const ordered: Record<string, unknown> = {};
45
+ for (const key of Object.keys(source).sort()) ordered[key] = canonicalize(source[key]);
46
+ return ordered;
47
+ }
48
+ return value;
49
+ }
50
+
51
+ // A NUL byte cannot appear unescaped in JSON string output, so the name/args boundary of the
52
+ // hashed payload is unambiguous.
53
+ const NAME_ARGS_SEPARATOR = String.fromCharCode(0);
54
+
55
+ export function toolCallSignature(name: string, args: unknown): string {
56
+ return createHash('sha256')
57
+ .update(name + NAME_ARGS_SEPARATOR + JSON.stringify(canonicalize(args ?? null)))
58
+ .digest('hex');
59
+ }
60
+
61
+ type RepetitionEntry = { toolName: string; signature: string; count: number; sampleArgs: Record<string, unknown> };
62
+
63
+ export function countToolRepetitions(calls: ToolCallEntry[]): Map<string, RepetitionEntry> {
64
+ const counts = new Map<string, RepetitionEntry>();
65
+ for (const call of calls) {
66
+ if (!call.name) continue;
67
+ const signature = toolCallSignature(call.name, call.args);
68
+ const entry = counts.get(signature) ?? {
69
+ toolName: call.name,
70
+ signature,
71
+ count: 0,
72
+ sampleArgs: asObject(call.args),
73
+ };
74
+ entry.count += 1;
75
+ counts.set(signature, entry);
76
+ }
77
+ return counts;
78
+ }
79
+
80
+ export function toolLoopLevel(count: number, detection: LoopPatternPolicy['loopDetection']): ToolLoopLevel {
81
+ if (!detection.enabled) return 'none';
82
+ if (count >= detection.escalateAt) return 'escalate';
83
+ if (count >= detection.blockAt) return 'block';
84
+ if (count >= detection.warnAt) return 'warn';
85
+ return 'none';
86
+ }
87
+
88
+ export function toolLoopReason(finding: ToolLoopFinding): string {
89
+ return `Tool loop detected: "${finding.toolName}" was invoked ${finding.count} times with identical arguments.`;
90
+ }
91
+
92
+ export function toolLoopNotice(toolName: string, count: number): string {
93
+ return [
94
+ '<tool_loop_warning>',
95
+ `An earlier pass of this run invoked the tool "${toolName}" with identical arguments ${count} times.`,
96
+ 'Do not repeat this pattern. If the tool did not return what you expected, change your approach or report the blocker explicitly instead of retrying the same call.',
97
+ '</tool_loop_warning>',
98
+ ].join('\n');
99
+ }
100
+
101
+ export class ToolLoopDetectionService {
102
+ constructor(private readonly database: Database) {}
103
+
104
+ // Scans every tool call the run has issued so far (across all pass sessions, which are linked
105
+ // through agentLoopSteps.sessionId) and returns the most repeated signature once it crosses the
106
+ // policy's warn threshold. Returns null while the run stays under every threshold.
107
+ async scanRun(runId: number, policy: LoopPatternPolicy): Promise<ToolLoopFinding | null> {
108
+ const detection = policy.loopDetection;
109
+ if (!detection.enabled) return null;
110
+
111
+ const steps = await this.database.getRepository('agentLoopSteps').find({ filter: { runId } });
112
+ const sessionIds = new Set<string>();
113
+ for (const step of steps) {
114
+ const sessionId = read(step, 'sessionId');
115
+ if (typeof sessionId === 'string' && sessionId) sessionIds.add(sessionId);
116
+ }
117
+ if (!sessionIds.size) return null;
118
+
119
+ const messages = await this.database.getRepository('aiMessages').find({
120
+ filter: { sessionId: { $in: [...sessionIds] } },
121
+ });
122
+ const calls: ToolCallEntry[] = [];
123
+ for (const message of messages) {
124
+ const toolCalls = read(message, 'toolCalls');
125
+ if (!Array.isArray(toolCalls)) continue;
126
+ for (const toolCall of toolCalls) {
127
+ const entry = asObject(toolCall);
128
+ if (typeof entry.name === 'string' && entry.name) calls.push({ name: entry.name, args: entry.args });
129
+ }
130
+ }
131
+ if (!calls.length) return null;
132
+
133
+ let worst: ToolLoopFinding | null = null;
134
+ for (const entry of countToolRepetitions(calls).values()) {
135
+ const level = toolLoopLevel(entry.count, detection);
136
+ if (level === 'none') continue;
137
+ if (!worst || entry.count > worst.count) worst = { level, ...entry };
138
+ }
139
+ return worst;
140
+ }
141
+
142
+ // Persists the guard step and event for a finding, deduplicated by signature so a run records
143
+ // each loop once no matter how many subsequent passes observe it. Returns true when a new
144
+ // record was created.
145
+ async recordFinding(context: ToolLoopRecordContext, finding: ToolLoopFinding): Promise<boolean> {
146
+ const steps = this.database.getRepository('agentLoopSteps');
147
+ const existing = await steps.findOne({
148
+ filter: { runId: context.runId, kind: 'guard', inputHash: finding.signature },
149
+ });
150
+ if (existing) return false;
151
+
152
+ const now = new Date();
153
+ const runSteps = await steps.find({ filter: { runId: context.runId } });
154
+ const step = await steps.create({
155
+ values: {
156
+ runId: context.runId,
157
+ runtimeVersion: 'control-plane-v2',
158
+ sequence: runSteps.length,
159
+ role: context.role,
160
+ kind: 'guard',
161
+ type: 'tool_loop',
162
+ employeeUsername: context.username,
163
+ toolName: finding.toolName,
164
+ inputHash: finding.signature,
165
+ title: `Tool loop ${finding.level}: ${finding.toolName} x${finding.count}`,
166
+ description: toolLoopReason(finding),
167
+ status: 'succeeded',
168
+ metadata: { level: finding.level, count: finding.count, sampleArgs: finding.sampleArgs },
169
+ startedAt: now,
170
+ endedAt: now,
171
+ createdAt: now,
172
+ },
173
+ });
174
+ const event = await this.database.getRepository('agentLoopEvents').create({
175
+ values: {
176
+ runId: context.runId,
177
+ stepId: read(step, 'id'),
178
+ type: 'tool_loop_detected',
179
+ title: `Tool loop ${finding.level}: ${finding.toolName}`,
180
+ content: toolLoopReason(finding),
181
+ status: context.runStatus,
182
+ payload: {
183
+ level: finding.level,
184
+ toolName: finding.toolName,
185
+ count: finding.count,
186
+ signature: finding.signature,
187
+ },
188
+ actorType: context.actorType,
189
+ actorIdentity: context.actorIdentity,
190
+ correlationKey: `tool-loop:${finding.signature}`,
191
+ createdAt: now,
192
+ },
193
+ });
194
+ getRunEventBus().emit(context.runId, typeof event.toJSON === 'function' ? event.toJSON() : event);
195
+ return true;
196
+ }
197
+
198
+ // Rebuilds the model-facing notices from guard steps recorded before the current worker claim,
199
+ // so a run that parked and resumed keeps warning later roles about an earlier loop.
200
+ async existingNotices(runId: number): Promise<string[]> {
201
+ const steps = await this.database.getRepository('agentLoopSteps').find({
202
+ filter: { runId, kind: 'guard' },
203
+ });
204
+ const notices: string[] = [];
205
+ for (const step of steps) {
206
+ const toolName = String(read(step, 'toolName') || '');
207
+ const count = Number(asObject(read(step, 'metadata')).count) || 0;
208
+ if (!toolName || !count) continue;
209
+ notices.push(toolLoopNotice(toolName, count));
210
+ }
211
+ return notices;
212
+ }
213
+ }
@@ -0,0 +1,31 @@
1
+ import { z } from 'zod';
2
+
3
+ export const verificationVerdictSchema = z.object({
4
+ verdict: z.enum(['pass', 'reject', 'escalate']),
5
+ summary: z.string().min(1),
6
+ checks: z
7
+ .array(
8
+ z.object({
9
+ name: z.string().min(1),
10
+ status: z.enum(['pass', 'fail', 'skipped']),
11
+ evidenceArtifactIds: z.array(z.number().int().positive()).default([]),
12
+ }),
13
+ )
14
+ .min(1),
15
+ residualRisks: z.array(z.string()).default([]),
16
+ });
17
+
18
+ export type VerificationVerdict = z.infer<typeof verificationVerdictSchema>;
19
+
20
+ export function parseVerificationVerdict(value: unknown) {
21
+ return verificationVerdictSchema.parse(value);
22
+ }
23
+
24
+ export function isPassingVerification(value: unknown) {
25
+ const parsed = verificationVerdictSchema.safeParse(value);
26
+ return (
27
+ parsed.success &&
28
+ parsed.data.verdict === 'pass' &&
29
+ parsed.data.checks.every((check) => check.status === 'pass' && check.evidenceArtifactIds.length > 0)
30
+ );
31
+ }