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,264 @@
1
+ import type { Database, Model } from '@nocobase/database';
2
+ import type { CompiledHarness } from './HarnessCompiler';
3
+ import type { LoopPatternPolicy } from './LoopPatternSchema';
4
+ import type { LoopRunStateMachine } from './LoopRunStateMachine';
5
+ import type { PluginAiRuntimeAdapter } from './PluginAiRuntimeAdapter';
6
+ import { ToolLoopDetectionService, toolLoopReason } from './ToolLoopDetectionService';
7
+ import { parseVerificationVerdict } from './VerificationSchema';
8
+ import type { VerificationVerdict } from './VerificationSchema';
9
+
10
+ export type VerificationRequest = {
11
+ runId: number;
12
+ goal: string;
13
+ autonomyLevel: 'L1' | 'L2' | 'L3';
14
+ verifierUsername: string;
15
+ leaderUsername: string;
16
+ makerUsernames: string[];
17
+ verifierHarness: CompiledHarness;
18
+ policy: LoopPatternPolicy;
19
+ makerSummary: string;
20
+ userId?: number;
21
+ workerId: string;
22
+ leaseToken: string;
23
+ loopNotices?: string[];
24
+ signal: AbortSignal;
25
+ };
26
+
27
+ export type VerificationOutcome = {
28
+ verdict: VerificationVerdict;
29
+ finalStatus: 'succeeded' | 'waiting_human' | 'failed' | 'halted';
30
+ };
31
+
32
+ function read(record: Model | Record<string, unknown>, key: string) {
33
+ const model = record as Model & { get?: (name: string) => unknown };
34
+ return typeof model.get === 'function' ? model.get(key) : (record as Record<string, unknown>)[key];
35
+ }
36
+
37
+ // The verifier answers in prose around its JSON verdict, so the object is located rather than
38
+ // assumed to be the whole response.
39
+ function extractVerdictObject(content: string): unknown {
40
+ const fenced = content.match(/```(?:json)?\s*([\s\S]*?)```/i);
41
+ const candidate = fenced ? fenced[1] : content;
42
+ const start = candidate.indexOf('{');
43
+ const end = candidate.lastIndexOf('}');
44
+ if (start < 0 || end <= start) {
45
+ throw new Error('The verifier did not return a structured verdict object.');
46
+ }
47
+ return JSON.parse(candidate.slice(start, end + 1));
48
+ }
49
+
50
+ function verifierPrompt(input: VerificationRequest, artifacts: Array<Record<string, unknown>>) {
51
+ const catalogue = artifacts.length
52
+ ? artifacts.map((artifact) => `- id=${artifact.id} kind=${artifact.kind} title=${artifact.title || ''}`).join('\n')
53
+ : '- (none)';
54
+ return [
55
+ 'You are the independent verifier for an autonomous run. You did not produce this work.',
56
+ `Goal: ${input.goal}`,
57
+ '',
58
+ 'Reported result:',
59
+ input.makerSummary || '(the maker produced no textual summary)',
60
+ '',
61
+ 'Evidence artifacts recorded for this run:',
62
+ catalogue,
63
+ '',
64
+ `Required checks: ${input.policy.verification.requiredChecks.join(', ')}`,
65
+ '',
66
+ ...(input.loopNotices ?? []),
67
+ 'Reply with a single JSON object and nothing else:',
68
+ '{"verdict":"pass|reject|escalate","summary":"...","checks":[{"name":"...","status":"pass|fail|skipped",',
69
+ '"evidenceArtifactIds":[<artifact ids from the list above>]}],"residualRisks":["..."]}',
70
+ 'Every check you mark "pass" must cite at least one artifact id from the list above.',
71
+ 'If the evidence does not support the goal, answer "reject". Never invent artifact ids.',
72
+ ].join('\n');
73
+ }
74
+
75
+ export class VerificationService {
76
+ private readonly loopGuard: ToolLoopDetectionService;
77
+
78
+ constructor(
79
+ private readonly database: Database,
80
+ private readonly runtime: PluginAiRuntimeAdapter,
81
+ private readonly stateMachine: LoopRunStateMachine,
82
+ ) {
83
+ this.loopGuard = new ToolLoopDetectionService(database);
84
+ }
85
+
86
+ async verifyAndFinalize(input: VerificationRequest): Promise<VerificationOutcome> {
87
+ this.assertIndependentVerifier(input);
88
+ const artifacts = await this.runArtifacts(input.runId);
89
+ const verdict = await this.collectVerdict(input, artifacts);
90
+
91
+ // The guard also sees the verifier's own tool calls: a verifier retrying the same evidence
92
+ // lookup over and over is the same behavioral failure as a maker looping on its tools.
93
+ const guard = await this.enforceToolLoopGuard(input);
94
+ if (guard === 'halt') {
95
+ return { verdict, finalStatus: 'halted' };
96
+ }
97
+
98
+ await this.assertEvidenceBelongsToRun(verdict, artifacts, input.verifierUsername);
99
+
100
+ const finalStatus = this.finalStatusFor(verdict, input.autonomyLevel);
101
+ await this.stateMachine.transition({
102
+ runId: input.runId,
103
+ to: finalStatus,
104
+ actorType: 'worker',
105
+ actorIdentity: input.workerId,
106
+ leaseToken: input.leaseToken,
107
+ eventType: 'run_verified',
108
+ title: `Verifier returned ${verdict.verdict}`,
109
+ content: verdict.summary,
110
+ correlationKey: `verified:${input.runId}:${verdict.verdict}`,
111
+ values: {
112
+ verifierVerdict: verdict.verdict,
113
+ verifierEvidence: verdict,
114
+ summary: verdict.summary,
115
+ },
116
+ });
117
+ return { verdict, finalStatus };
118
+ }
119
+
120
+ private assertIndependentVerifier(input: VerificationRequest) {
121
+ const verifier = input.verifierUsername.trim();
122
+ if (!verifier) throw new Error('The run snapshot does not name a verifier.');
123
+ if (verifier === input.leaderUsername || input.makerUsernames.includes(verifier)) {
124
+ throw new Error('The verifier must be a different employee from the leader and every maker.');
125
+ }
126
+ }
127
+
128
+ private async enforceToolLoopGuard(input: VerificationRequest): Promise<'continue' | 'halt'> {
129
+ const finding = await this.loopGuard.scanRun(input.runId, input.policy);
130
+ if (!finding) return 'continue';
131
+
132
+ await this.loopGuard.recordFinding(
133
+ {
134
+ runId: input.runId,
135
+ role: 'verifier',
136
+ username: input.verifierUsername,
137
+ runStatus: 'verifying',
138
+ actorType: 'worker',
139
+ actorIdentity: input.workerId,
140
+ },
141
+ finding,
142
+ );
143
+ if (finding.level === 'warn') return 'continue';
144
+
145
+ const reason = toolLoopReason(finding);
146
+ await this.stateMachine.transition({
147
+ runId: input.runId,
148
+ to: finding.level === 'block' ? 'blocked' : 'waiting_human',
149
+ actorType: 'worker',
150
+ actorIdentity: input.workerId,
151
+ leaseToken: input.leaseToken,
152
+ eventType: finding.level === 'block' ? 'run_blocked' : 'run_escalated',
153
+ title: finding.level === 'block' ? 'Run blocked by tool loop detection' : 'Run escalated by tool loop detection',
154
+ content: reason,
155
+ correlationKey: `tool-loop-${finding.level}:${input.runId}:${finding.signature}`,
156
+ values: finding.level === 'block' ? { blockedReason: reason } : { escalationReason: reason },
157
+ });
158
+ return 'halt';
159
+ }
160
+
161
+ private async nextSequence(runId: number) {
162
+ const steps = await this.database.getRepository('agentLoopSteps').find({ filter: { runId } });
163
+ return steps.length;
164
+ }
165
+
166
+ private async collectVerdict(input: VerificationRequest, artifacts: Array<Record<string, unknown>>) {
167
+ const sessionId = await this.runtime.createConversation({
168
+ username: input.verifierUsername,
169
+ userId: input.userId,
170
+ runId: input.runId,
171
+ title: `Verification of run ${input.runId}`,
172
+ });
173
+ // Recording the session on a step row is what lets the tool loop guard include the verifier's
174
+ // own tool calls in its run-level scan.
175
+ const step = await this.database.getRepository('agentLoopSteps').create({
176
+ values: {
177
+ runId: input.runId,
178
+ runtimeVersion: 'control-plane-v2',
179
+ sequence: await this.nextSequence(input.runId),
180
+ role: 'verifier',
181
+ kind: 'invocation',
182
+ type: 'verification',
183
+ employeeUsername: input.verifierUsername,
184
+ sessionId,
185
+ title: 'verifier invocation',
186
+ status: 'running',
187
+ startedAt: new Date(),
188
+ createdAt: new Date(),
189
+ },
190
+ });
191
+ const outcome = await this.runtime.invoke({
192
+ username: input.verifierUsername,
193
+ sessionId,
194
+ userId: input.userId,
195
+ systemMessage:
196
+ 'You verify completed work against its stated goal using only recorded evidence. You never modify anything.',
197
+ harness: input.verifierHarness,
198
+ prompt: verifierPrompt(input, artifacts),
199
+ signal: input.signal,
200
+ });
201
+ await this.database.getRepository('agentLoopSteps').update({
202
+ filterByTk: read(step, 'id'),
203
+ values: {
204
+ status: 'succeeded',
205
+ output: { content: outcome.content },
206
+ endedAt: new Date(),
207
+ },
208
+ });
209
+ // A verifier that stops to ask for approval cannot produce a trustworthy verdict, and its
210
+ // interrupted tool call would otherwise be silently dropped.
211
+ if (outcome.interrupted.length) {
212
+ throw new Error('The verifier requested an approval-gated tool; verification cannot complete.');
213
+ }
214
+ const verdict = parseVerificationVerdict(extractVerdictObject(outcome.content));
215
+ const required = new Set(input.policy.verification.requiredChecks);
216
+ for (const check of verdict.checks) required.delete(check.name);
217
+ if (required.size) {
218
+ throw new Error(`The verifier omitted required checks: ${Array.from(required).join(', ')}.`);
219
+ }
220
+ return verdict;
221
+ }
222
+
223
+ private async assertEvidenceBelongsToRun(
224
+ verdict: VerificationVerdict,
225
+ artifacts: Array<Record<string, unknown>>,
226
+ verifierUsername: string,
227
+ ) {
228
+ const owned = new Map(artifacts.map((artifact) => [Number(artifact.id), artifact]));
229
+ for (const check of verdict.checks) {
230
+ for (const artifactId of check.evidenceArtifactIds) {
231
+ const artifact = owned.get(artifactId);
232
+ if (!artifact) {
233
+ throw new Error(`Evidence artifact ${artifactId} does not belong to this run.`);
234
+ }
235
+ if (String(artifact.producerUsername || '') === verifierUsername) {
236
+ throw new Error(`Evidence artifact ${artifactId} was produced by the verifier itself.`);
237
+ }
238
+ }
239
+ }
240
+ }
241
+
242
+ private finalStatusFor(verdict: VerificationVerdict, autonomyLevel: 'L1' | 'L2' | 'L3') {
243
+ if (verdict.verdict !== 'pass') {
244
+ return verdict.verdict === 'escalate' ? ('waiting_human' as const) : ('failed' as const);
245
+ }
246
+ const passesEveryCheck = verdict.checks.every(
247
+ (check) => check.status === 'pass' && check.evidenceArtifactIds.length > 0,
248
+ );
249
+ if (!passesEveryCheck) return 'failed' as const;
250
+ // L1 only ever reports, so a passing verdict is final. Anything that touched a repository
251
+ // still needs a human to accept the change.
252
+ return autonomyLevel === 'L1' ? ('succeeded' as const) : ('waiting_human' as const);
253
+ }
254
+
255
+ private async runArtifacts(runId: number) {
256
+ const rows = await this.database.getRepository('agentLoopArtifacts').find({ filter: { runId } });
257
+ return rows.map((row) => ({
258
+ id: Number(read(row, 'id')),
259
+ kind: String(read(row, 'kind') || ''),
260
+ title: String(read(row, 'title') || ''),
261
+ producerUsername: String(read(row, 'producerUsername') || ''),
262
+ }));
263
+ }
264
+ }
@@ -0,0 +1,141 @@
1
+ import type { Database } from '@nocobase/database';
2
+ import { compileHarness } from './HarnessCompiler';
3
+ import type { CompiledHarness } from './HarnessCompiler';
4
+ import { HarnessProfileService } from './HarnessProfileService';
5
+ import { parseHarnessSettings } from './HarnessSchema';
6
+
7
+ export type TraceLike = {
8
+ agentLoopRunId?: string | number;
9
+ employeeUsername?: string;
10
+ };
11
+
12
+ function read(record: unknown, key: string) {
13
+ const model = record as { get?: (name: string) => unknown } | null;
14
+ return typeof model?.get === 'function' ? model.get(key) : (model as Record<string, unknown> | null)?.[key];
15
+ }
16
+
17
+ function asObject(value: unknown): Record<string, unknown> {
18
+ return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
19
+ }
20
+
21
+ // Run snapshots were compiled at enqueue time, possibly by an older schema revision. Re-parsing
22
+ // through the current schema backfills new policy fields with their defaults. `sources` is a
23
+ // compiler artifact the strict schema rejects, so it is stripped first.
24
+ function compiledFromSnapshot(snapshot: unknown): CompiledHarness | null {
25
+ const effective = asObject(asObject(snapshot).effective);
26
+ if (!effective.tools) return null;
27
+ const { sources: _sources, ...settings } = effective;
28
+ return compileHarness([{ source: 'run-snapshot', settings: parseHarnessSettings(settings) }]);
29
+ }
30
+
31
+ function snapshotForEmployee(run: Record<string, unknown>, username: string | undefined) {
32
+ const bindings = asObject(run.roleBindingsSnapshot);
33
+ if (!username) return null;
34
+ if (bindings.leader === username) return run.leaderHarnessSnapshot;
35
+ if (bindings.verifier === username) return run.verifierHarnessSnapshot;
36
+ const makers = asObject(run.makerHarnessSnapshot);
37
+ return makers[username] ?? null;
38
+ }
39
+
40
+ // Resolves the compiled harness of one role inside a loop run, or null when the run or the role's
41
+ // snapshot is unavailable. Loop snapshots are immutable and authoritative for everything the run
42
+ // delegates, so callers outside the run (sub-agent inheritance, skill enforcement) resolve through
43
+ // this instead of re-deriving policy.
44
+ export async function resolveRunRoleHarness(
45
+ database: Database,
46
+ runId: number,
47
+ username: string | undefined,
48
+ ): Promise<CompiledHarness | null> {
49
+ if (!Number.isSafeInteger(runId) || runId <= 0 || !username) return null;
50
+ const run = await database.getRepository('agentLoopRuns').findOne({ filterByTk: runId });
51
+ if (!run) return null;
52
+ const snapshot = snapshotForEmployee(asObject((run as { toJSON?: () => unknown }).toJSON?.() ?? run), username);
53
+ return compiledFromSnapshot(snapshot);
54
+ }
55
+
56
+ // Resolves the compiled harness governing a tool call. Loop runs carry an immutable snapshot per
57
+ // role, which is authoritative; anything outside a loop run falls back to the employee's
58
+ // harness-tag profile. Returns null when no policy applies (enforcement points then keep their
59
+ // existing behavior).
60
+ export async function resolveTraceHarness(database: Database, trace: TraceLike): Promise<CompiledHarness | null> {
61
+ const runHarness = await resolveRunRoleHarness(database, Number(trace.agentLoopRunId), trace.employeeUsername);
62
+ if (runHarness) return runHarness;
63
+ if (!trace.employeeUsername) return null;
64
+
65
+ const employee = await database.getRepository('aiEmployees').findOne({
66
+ filter: { username: trace.employeeUsername },
67
+ });
68
+ if (!employee) return null;
69
+ const settings = asObject(read(employee, 'skillSettings'));
70
+ const tag =
71
+ typeof settings.harnessTag === 'string' && settings.harnessTag.trim() ? settings.harnessTag.trim() : 'default';
72
+
73
+ const profiles = new HarnessProfileService(database);
74
+ const version =
75
+ (await profiles.getPublishedByTag(tag)) || (tag !== 'default' ? await profiles.getPublishedByTag('default') : null);
76
+ if (!version) return null;
77
+ return compileHarness([{ source: `profile:${tag}`, settings: version.settings }]);
78
+ }
79
+
80
+ export function harnessTimeoutMs(harness: CompiledHarness, toolName: string): number | null {
81
+ const timeouts = harness.tools.timeouts;
82
+ const direct = timeouts[toolName];
83
+ if (typeof direct === 'number' && direct > 0) return direct;
84
+ const generic = timeouts['skill_hub_execute'];
85
+ return typeof generic === 'number' && generic > 0 ? generic : null;
86
+ }
87
+
88
+ const SECRET_KEY_PATTERN = /(?:KEY|SECRET|TOKEN|PASSWORD|PASSWD|CREDENTIAL)/i;
89
+ export const REDACTED_PLACEHOLDER = '[REDACTED]';
90
+
91
+ // Key-name scrubbing for persisted telemetry: any value whose key looks secret-bearing is
92
+ // replaced wholesale. Free text is deliberately left alone — pattern-matching arbitrary output
93
+ // produces false negatives that only look like safety.
94
+ export function redactSecretsIn<T>(value: T): T {
95
+ const scrub = (input: unknown, depth: number): unknown => {
96
+ if (depth > 8 || input === null || input === undefined) return input;
97
+ if (Array.isArray(input)) return input.map((item) => scrub(item, depth + 1));
98
+ if (typeof input === 'object') {
99
+ const source = input as Record<string, unknown>;
100
+ const output: Record<string, unknown> = {};
101
+ for (const [key, entry] of Object.entries(source)) {
102
+ output[key] = SECRET_KEY_PATTERN.test(key) ? REDACTED_PLACEHOLDER : scrub(entry, depth + 1);
103
+ }
104
+ return output;
105
+ }
106
+ return input;
107
+ };
108
+ return scrub(value, 0) as T;
109
+ }
110
+
111
+ // Byte-safe slice that never splits a multi-byte character: the cut point advances past any
112
+ // continuation bytes (10xxxxxx) left dangling at the boundary.
113
+ function sliceBytes(buffer: Buffer, start: number, end: number) {
114
+ let from = Math.max(0, start);
115
+ while (from < end && (buffer[from] & 0xc0) === 0x80) from += 1;
116
+ return buffer.subarray(from, Math.min(buffer.length, end));
117
+ }
118
+
119
+ // Builds the model-facing replacement for an oversized output: head/tail preview plus a locator
120
+ // supplied by the caller (download URL for skill output, file path for sub-agent results).
121
+ // Returns null when the text already fits the inline budget. Mirrors the dsh spill policy.
122
+ export function spilledText(text: string, maxInlineBytes: number, locator: string): string | null {
123
+ const buffer = Buffer.from(text, 'utf8');
124
+ if (buffer.byteLength <= maxInlineBytes) return null;
125
+
126
+ const headTarget = Math.floor(maxInlineBytes * 0.6);
127
+ const tailTarget = Math.floor(maxInlineBytes * 0.2);
128
+ let head = sliceBytes(buffer, 0, headTarget).toString('utf8');
129
+ let tail = sliceBytes(buffer, buffer.byteLength - tailTarget, buffer.byteLength).toString('utf8');
130
+ const notice = () =>
131
+ `\n\n... (${
132
+ buffer.byteLength - Buffer.byteLength(head, 'utf8') - Buffer.byteLength(tail, 'utf8')
133
+ } bytes omitted. Full output stored at: ${locator}) ...`;
134
+ while (Buffer.byteLength(head + notice() + tail, 'utf8') > maxInlineBytes && head.length + tail.length > 0) {
135
+ head = head.slice(0, Math.floor(head.length * 0.9));
136
+ // Keep the true end of the output: shrinking from the front preserves the final lines.
137
+ const keepTail = Math.floor(tail.length * 0.9);
138
+ tail = keepTail > 0 ? tail.slice(-keepTail) : '';
139
+ }
140
+ return `${head}${notice()}${tail}`;
141
+ }
@@ -177,6 +177,20 @@ describe('Skill Hub Git import', () => {
177
177
  expect(service.readFile).not.toHaveBeenCalled();
178
178
  });
179
179
 
180
+ it('localizes Git Manager bridge setup errors', async () => {
181
+ const { context } = createContext({ params: { repositoryId: 42, rootFolder: 'skills' } });
182
+ const translate = vi.fn((key: string) => `localized:${key}`);
183
+ (context as unknown as { t: typeof translate }).t = translate;
184
+
185
+ await expect(gitListSkills(context, vi.fn())).rejects.toMatchObject({
186
+ status: 424,
187
+ message: 'localized:Git Manager is not enabled for Skill Hub imports.',
188
+ });
189
+ expect(translate).toHaveBeenCalledWith('Git Manager is not enabled for Skill Hub imports.', {
190
+ ns: 'plugin-agent-orchestrator',
191
+ });
192
+ });
193
+
180
194
  it('uses a virtual manifest without writing a Git checkout when skills.json is missing', async () => {
181
195
  const service = createContentService({
182
196
  listTree: vi.fn().mockImplementation(async ({ rootPath }: { rootPath: string }) => skillsRootEntries(rootPath)),
@@ -518,7 +532,7 @@ describe('Skill Hub Git import', () => {
518
532
  actor,
519
533
  );
520
534
  expect(created).toEqual([
521
- expect.objectContaining({ name: 'alpha', storageUrl: `git://42/agent-assets/skills@${commitSha}#alpha` }),
535
+ expect.objectContaining({ name: 'alpha', storageUrl: `git://42/agent-assets/skills/alpha@${commitSha}` }),
522
536
  ]);
523
537
  });
524
538
 
@@ -152,7 +152,7 @@ export async function gitListSkills(ctx: Context, next: () => Promise<void>) {
152
152
  const rootFolder = normalizeRootFolder(ctx.action.params.rootFolder);
153
153
 
154
154
  if (!repositoryId) {
155
- ctx.throw(400, 'repositoryId and rootFolder are required');
155
+ ctx.throw(400, translateGitImportMessage(ctx, 'repositoryId and rootFolder are required'));
156
156
  }
157
157
 
158
158
  try {
@@ -216,7 +216,7 @@ export async function gitSyncSkills(ctx: Context, next: () => Promise<void>) {
216
216
  const { skills: selectedKeys, overwrite = false, prefix = '' } = ctx.action.params.values || ctx.action.params;
217
217
 
218
218
  if (!repositoryId || !Array.isArray(selectedKeys) || selectedKeys.length === 0) {
219
- ctx.throw(400, 'repositoryId, rootFolder and skills[] are required');
219
+ ctx.throw(400, translateGitImportMessage(ctx, 'repositoryId, rootFolder and skills[] are required'));
220
220
  }
221
221
  const selectedSkillKeys = selectedKeys.filter(
222
222
  (key): key is string => typeof key === 'string' && key.trim().length > 0,
@@ -414,6 +414,7 @@ function getGitManagerContentService(ctx: Context): GitManagerContentService {
414
414
  'GIT_MANAGER_CONTENT_UNAVAILABLE',
415
415
  424,
416
416
  'Git Manager is not enabled for Skill Hub imports.',
417
+ { key: 'Git Manager is not enabled for Skill Hub imports.' },
417
418
  );
418
419
  }
419
420
 
@@ -423,6 +424,7 @@ function getGitManagerContentService(ctx: Context): GitManagerContentService {
423
424
  'GIT_MANAGER_CONTENT_UNAVAILABLE',
424
425
  424,
425
426
  'Git Manager must support actor-aware content reads for Skill Hub imports.',
427
+ { key: 'Git Manager must support actor-aware content reads for Skill Hub imports.' },
426
428
  );
427
429
  }
428
430
 
@@ -445,6 +447,7 @@ function getGitManagerContentService(ctx: Context): GitManagerContentService {
445
447
  'GIT_MANAGER_CONTENT_UNAVAILABLE',
446
448
  424,
447
449
  'Git Manager must support actor-aware content reads for Skill Hub imports.',
450
+ { key: 'Git Manager must support actor-aware content reads for Skill Hub imports.' },
448
451
  );
449
452
  }
450
453
  return service as GitManagerContentService;
@@ -457,6 +460,7 @@ function accessFromContext(ctx: Context): GitManagerAccessContext {
457
460
  'SKILL_HUB_REPOSITORY_ACCESS_DENIED',
458
461
  403,
459
462
  'A valid user role context is required to import skills from Git Manager.',
463
+ { key: 'A valid user role context is required to import skills from Git Manager.' },
460
464
  );
461
465
  }
462
466
  return { kind: 'user', roles: currentRoles };
@@ -495,7 +499,9 @@ function throwGitImportError(ctx: Context, error: unknown): never {
495
499
  case MISSING_GIT_FILE_CODE:
496
500
  return throwHttpError(ctx, 404, errorMessage(error));
497
501
  default:
498
- return throwHttpError(ctx, 502, 'Git Manager could not read the requested repository content.');
502
+ return throwHttpError(ctx, 502, 'Git Manager could not read the requested repository content.', {
503
+ key: 'Git Manager could not read the requested repository content.',
504
+ });
499
505
  }
500
506
  }
501
507
 
@@ -13,6 +13,7 @@ import { gitListSkills, gitSyncSkills } from './actions/git-import';
13
13
  import { createGitImportRequestMethodPolicy } from './middlewares/git-import-request-method-policy';
14
14
  import { parseJsonText, stringifyJsonText, parseJsonLike } from './utils/json-fields';
15
15
  import { getOrchestratorTraceContext } from '../services/ExecutionSpanService';
16
+ import { harnessTimeoutMs, resolveTraceHarness } from '../services/harness-runtime-policy';
16
17
  import { tryGetAIToolsManager } from '../utils/ai-manager';
17
18
  import type { ToolsRuntime } from '@nocobase/ai';
18
19
  import { SkillAccessService } from '../services/SkillAccessService';
@@ -442,6 +443,40 @@ export class SkillHubSubFeature {
442
443
  }
443
444
  }
444
445
 
446
+ /**
447
+ * Resolve harness-derived enforcement overrides for one skill execution. The worker node owns
448
+ * the sandbox but not the harness, so the decision is made here (run snapshot for loop runs,
449
+ * employee harness-tag profile otherwise) and persisted on the execution row. A resolution
450
+ * failure must never block execution: the skill falls back to its own defaults.
451
+ */
452
+ private async resolveRuntimePolicy(
453
+ traceContext: ReturnType<typeof getOrchestratorTraceContext>,
454
+ skill: any,
455
+ aiEmployeeUsername?: string,
456
+ ): Promise<{ timeoutSeconds?: number; spillMaxInlineBytes?: number } | null> {
457
+ try {
458
+ const harness = await resolveTraceHarness((this as any).db, {
459
+ agentLoopRunId: traceContext?.agentLoopRunId,
460
+ employeeUsername: traceContext?.employeeUsername || aiEmployeeUsername,
461
+ });
462
+ if (!harness) return null;
463
+
464
+ const policy: { timeoutSeconds?: number; spillMaxInlineBytes?: number } = {};
465
+ const timeoutMs = harnessTimeoutMs(harness, getSkillToolName(skill));
466
+ if (timeoutMs) {
467
+ const skillTimeoutSeconds = Number(skill.timeoutSeconds || skill.get?.('timeoutSeconds') || 60);
468
+ policy.timeoutSeconds = Math.min(skillTimeoutSeconds, Math.ceil(timeoutMs / 1000));
469
+ }
470
+ if (harness.context.spill.maxInlineBytes) {
471
+ policy.spillMaxInlineBytes = harness.context.spill.maxInlineBytes;
472
+ }
473
+ return Object.keys(policy).length ? policy : null;
474
+ } catch (error) {
475
+ (this as any).app.logger?.warn?.('[skill-hub] Failed to resolve harness runtime policy.', { error });
476
+ return null;
477
+ }
478
+ }
479
+
445
480
  /**
446
481
  * Execute skill — called by both AI tool and REST test endpoint.
447
482
  * Dispatches to sandbox workers via PubSub, waits for result via PubSub.
@@ -456,7 +491,6 @@ export class SkillHubSubFeature {
456
491
  ): Promise<any> {
457
492
  validateSkillInput(readRecordValue(skill, 'inputSchema'), inputArgs);
458
493
  const aiEmployeeUsername = await this.skillAccessService.assertCanExecute(ctx, skill, options);
459
-
460
494
  // ── Rate limiting ──
461
495
  const userId = currentUserId(ctx);
462
496
  if (userId) {
@@ -470,6 +504,7 @@ export class SkillHubSubFeature {
470
504
  }
471
505
 
472
506
  const traceContext = getOrchestratorTraceContext(ctx);
507
+ const runtimePolicy = await this.resolveRuntimePolicy(traceContext, skill, aiEmployeeUsername);
473
508
  const execution = await (this as any).db.getRepository('skillExecutions').create({
474
509
  values: {
475
510
  skillId: skill.id,
@@ -485,6 +520,7 @@ export class SkillHubSubFeature {
485
520
  agentLoopRunId: traceContext?.agentLoopRunId,
486
521
  agentLoopStepId: traceContext?.agentLoopStepId,
487
522
  triggeredById: ctx?.state?.currentUser?.id,
523
+ runtimePolicy,
488
524
  },
489
525
  });
490
526
 
@@ -532,10 +568,12 @@ export class SkillHubSubFeature {
532
568
  rejectPromise = reject;
533
569
  });
534
570
 
535
- const timeoutMs = ((skill.timeoutSeconds || skill.get?.('timeoutSeconds') || 60) + 15) * 1000;
571
+ const effectiveTimeoutSeconds =
572
+ runtimePolicy?.timeoutSeconds || skill.timeoutSeconds || skill.get?.('timeoutSeconds') || 60;
573
+ const timeoutMs = (effectiveTimeoutSeconds + 15) * 1000;
536
574
  const timeout = setTimeout(() => {
537
575
  clearInterval(poll);
538
- rejectPromise(new Error(`Skill execution timeout after ${skill.timeoutSeconds || 60}s`));
576
+ rejectPromise(new Error(`Skill execution timeout after ${effectiveTimeoutSeconds}s`));
539
577
  }, timeoutMs);
540
578
  const poll = setInterval(async () => {
541
579
  try {