plugin-agent-orchestrator 1.1.4 → 1.1.6

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 (148) 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/418.41ed200e7d6da332.js +10 -0
  6. package/dist/client-v2/53.349fff8db80b19d4.js +10 -0
  7. package/dist/client-v2/981.e4a1f41cfc94a856.js +10 -0
  8. package/dist/client-v2/index.js +1 -1
  9. package/dist/externalVersion.js +7 -8
  10. package/dist/locale/en-US.json +226 -170
  11. package/dist/locale/vi-VN.json +226 -170
  12. package/dist/locale/zh-CN.json +226 -170
  13. package/dist/server/collections/agent-execution-spans.js +8 -0
  14. package/dist/server/collections/agent-loop-action-approvals.js +1 -0
  15. package/dist/server/collections/agent-loop-runs.js +7 -1
  16. package/dist/server/collections/agent-loop-steps.js +7 -0
  17. package/dist/server/collections/skill-executions.js +8 -0
  18. package/dist/server/plugin.js +3 -1
  19. package/dist/server/resources/agent-harness-profile.js +117 -0
  20. package/dist/server/resources/agent-knowledge-insights.js +1 -3
  21. package/dist/server/resources/agent-loop-approval.js +25 -0
  22. package/dist/server/resources/agent-loop.js +3 -1
  23. package/dist/server/services/AgentMemoryContextService.js +34 -0
  24. package/dist/server/services/HarnessCompiler.js +35 -2
  25. package/dist/server/services/HarnessProfileService.js +74 -9
  26. package/dist/server/services/HarnessSchema.js +30 -3
  27. package/dist/server/services/LoopPatternSchema.js +16 -1
  28. package/dist/server/services/LoopRunRepository.js +80 -0
  29. package/dist/server/services/LoopRunStateMachine.js +92 -3
  30. package/dist/server/services/LoopTriggerService.js +37 -0
  31. package/dist/server/services/LoopWorkerService.js +261 -23
  32. package/dist/server/services/NativeSubAgentObserver.js +165 -23
  33. package/dist/server/services/PluginAiRuntimeAdapter.js +16 -5
  34. package/dist/server/services/ToolLoopDetectionService.js +210 -0
  35. package/dist/server/services/VerificationService.js +66 -0
  36. package/dist/server/services/harness-runtime-policy.js +139 -0
  37. package/dist/server/skill-hub/plugin.js +36 -3
  38. package/dist/server/skill-hub/tasks/SkillExecutionTask.js +29 -0
  39. package/package.json +53 -53
  40. package/src/client/ApprovalsTab.tsx +2 -0
  41. package/src/client/OrchestratorSettings.tsx +11 -0
  42. package/src/client/skill-hub/components/GitSkillImport.tsx +3 -3
  43. package/src/client/skill-hub/components/SkillMetrics.tsx +15 -10
  44. package/src/client/skill-hub/index.tsx +0 -52
  45. package/src/client-v2/components/AgentRunsTab.tsx +576 -576
  46. package/src/client-v2/components/ApprovalsTab.tsx +337 -0
  47. package/src/client-v2/components/HarnessProfilesTab.tsx +385 -105
  48. package/src/client-v2/components/MemoryInspectorTab.tsx +141 -141
  49. package/src/client-v2/components/TracingTab.tsx +515 -515
  50. package/src/client-v2/hooks/useApiRequest.ts +141 -141
  51. package/src/client-v2/pages/ApprovalsPage.tsx +10 -0
  52. package/src/client-v2/plugin.tsx +6 -6
  53. package/src/client-v2/skill-hub/components/SkillMetrics.tsx +5 -2
  54. package/src/locale/en-US.json +226 -174
  55. package/src/locale/vi-VN.json +226 -174
  56. package/src/locale/zh-CN.json +226 -174
  57. package/src/server/__tests__/circuit-breaker.test.ts +248 -248
  58. package/src/server/__tests__/harness-compiler.test.ts +83 -0
  59. package/src/server/__tests__/harness-profile-service.test.ts +51 -0
  60. package/src/server/__tests__/harness-runtime-policy.test.ts +293 -0
  61. package/src/server/__tests__/harness-tool-plan.test.ts +24 -0
  62. package/src/server/__tests__/loop-approval-decision.test.ts +512 -0
  63. package/src/server/__tests__/loop-approval-expiry.test.ts +258 -0
  64. package/src/server/__tests__/loop-budget-service.test.ts +255 -255
  65. package/src/server/__tests__/loop-control-plane-backfill.test.ts +266 -266
  66. package/src/server/__tests__/loop-pattern-schema.test.ts +18 -1
  67. package/src/server/__tests__/loop-pattern-service.test.ts +148 -148
  68. package/src/server/__tests__/loop-run-state-machine.test.ts +11 -0
  69. package/src/server/__tests__/loop-trigger-service.test.ts +148 -0
  70. package/src/server/__tests__/loop-worker-claim.test.ts +341 -341
  71. package/src/server/__tests__/loop-worker-execution.test.ts +82 -1
  72. package/src/server/__tests__/native-sub-agent-observer.test.ts +288 -4
  73. package/src/server/__tests__/path-lock-service.test.ts +254 -254
  74. package/src/server/__tests__/skill-execution-task-policy.test.ts +140 -0
  75. package/src/server/__tests__/smoke.test.ts +162 -162
  76. package/src/server/__tests__/tool-loop-detection.test.ts +275 -0
  77. package/src/server/collections/agent-execution-spans.ts +8 -0
  78. package/src/server/collections/agent-harness-profile-versions.ts +26 -26
  79. package/src/server/collections/agent-harness-profiles.ts +71 -71
  80. package/src/server/collections/agent-loop-action-approvals.ts +1 -0
  81. package/src/server/collections/agent-loop-artifacts.ts +25 -25
  82. package/src/server/collections/agent-loop-circuit-states.ts +23 -23
  83. package/src/server/collections/agent-loop-control-settings.ts +21 -21
  84. package/src/server/collections/agent-loop-events.ts +98 -98
  85. package/src/server/collections/agent-loop-path-locks.ts +27 -27
  86. package/src/server/collections/agent-loop-patterns.ts +42 -42
  87. package/src/server/collections/agent-loop-runs.ts +7 -1
  88. package/src/server/collections/agent-loop-steps.ts +7 -0
  89. package/src/server/collections/agent-loop-usage-buckets.ts +22 -22
  90. package/src/server/collections/agent-loop-worktrees.ts +25 -25
  91. package/src/server/collections/skill-executions.ts +8 -0
  92. package/src/server/migrations/20260427000002-change-packages-to-text.ts +52 -52
  93. package/src/server/migrations/20260429000000-add-llm-fields.ts +47 -47
  94. package/src/server/migrations/20260429000001-fix-inputargs-json-to-text.ts +45 -45
  95. package/src/server/migrations/20260503000000-add-orchestrator-trace-fields.ts +33 -33
  96. package/src/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.ts +31 -31
  97. package/src/server/migrations/20260803010000-backfill-loop-control-plane-data.ts +293 -293
  98. package/src/server/plugin.ts +3 -1
  99. package/src/server/resources/agent-harness-profile.ts +94 -0
  100. package/src/server/resources/agent-knowledge-insights.ts +5 -3
  101. package/src/server/resources/agent-loop-approval.ts +32 -0
  102. package/src/server/resources/agent-loop-artifact.ts +56 -56
  103. package/src/server/resources/agent-loop-events-stream.ts +105 -105
  104. package/src/server/resources/agent-loop.ts +3 -1
  105. package/src/server/resources/resource-helpers.ts +33 -33
  106. package/src/server/resources/tracing.ts +266 -266
  107. package/src/server/services/AgentMemoryContextService.ts +44 -0
  108. package/src/server/services/DurableCircuitBreakerService.ts +239 -239
  109. package/src/server/services/ExecutionSpanService.ts +2 -0
  110. package/src/server/services/HarnessCompiler.ts +37 -0
  111. package/src/server/services/HarnessProfileService.ts +92 -10
  112. package/src/server/services/HarnessSchema.ts +31 -0
  113. package/src/server/services/LoopBudgetService.ts +275 -275
  114. package/src/server/services/LoopControlService.ts +124 -124
  115. package/src/server/services/LoopPatternSchema.ts +19 -1
  116. package/src/server/services/LoopPatternService.ts +143 -143
  117. package/src/server/services/LoopRunRepository.ts +99 -0
  118. package/src/server/services/LoopRunStateMachine.ts +100 -3
  119. package/src/server/services/LoopSchedulerService.ts +199 -199
  120. package/src/server/services/LoopTriggerService.ts +42 -0
  121. package/src/server/services/LoopWorkerService.ts +327 -27
  122. package/src/server/services/NativeSubAgentObserver.ts +186 -21
  123. package/src/server/services/PathLockService.ts +321 -321
  124. package/src/server/services/PluginAiRuntimeAdapter.ts +24 -7
  125. package/src/server/services/ToolLoopDetectionService.ts +213 -0
  126. package/src/server/services/VerificationSchema.ts +31 -31
  127. package/src/server/services/VerificationService.ts +81 -2
  128. package/src/server/services/harness-runtime-policy.ts +141 -0
  129. package/src/server/skill-hub/plugin.ts +41 -3
  130. package/src/server/skill-hub/services/RegistrySkillSnapshotService.ts +235 -235
  131. package/src/server/skill-hub/services/__tests__/RegistrySkillSnapshotService.test.ts +61 -61
  132. package/src/server/skill-hub/tasks/SkillExecutionTask.ts +43 -2
  133. package/src/server/utils/ensure-indexes.ts +96 -96
  134. package/src/server/utils/logging.ts +62 -62
  135. package/src/server/utils/loop-pattern-context.ts +20 -20
  136. package/dist/client-v2/214.b063f33ba044a808.js +0 -10
  137. package/dist/client-v2/373.f5d5292e53c4e832.js +0 -10
  138. package/dist/client-v2/418.58fa350f5496efd3.js +0 -10
  139. package/dist/client-v2/53.9a10455323390f14.js +0 -10
  140. package/dist/client-v2/677.a991ce0250ff5c77.js +0 -10
  141. package/dist/client-v2/925.e5925bf21ed68e7a.js +0 -10
  142. package/src/client/AIEmployeeSelect.tsx +0 -47
  143. package/src/client/AIEmployeesContext.tsx +0 -102
  144. package/src/client/RulesTab.tsx +0 -2
  145. package/src/client/TracingTab.tsx +0 -436
  146. package/src/client/skill-hub/components/ExecutionProgress.tsx +0 -51
  147. package/src/client-v2/components/RulesTab.tsx +0 -2
  148. package/src/client-v2/pages/AgentRunsPage.tsx +0 -13
@@ -1,162 +1,162 @@
1
- import { createMockServer, MockServer } from '@nocobase/test';
2
- import { PluginAgentOrchestratorServer } from '../plugin';
3
- import { ensureAgentOrchestratorIndexes } from '../utils/ensure-indexes';
4
-
5
- type DatabaseIndex = {
6
- name?: string;
7
- unique?: boolean;
8
- fields?: Array<{ attribute?: string; name?: string }>;
9
- };
10
-
11
- describe('Agent Orchestrator plugin smoke', () => {
12
- let app: MockServer;
13
-
14
- beforeAll(async () => {
15
- const createdApp = await createMockServer({
16
- plugins: ['nocobase', PluginAgentOrchestratorServer],
17
- name: `agent-orchestrator-smoke-${process.pid}`,
18
- skipSupervisor: true,
19
- beforeInstall: async (mockApp) => {
20
- app = mockApp;
21
- },
22
- });
23
- app = createdApp;
24
- });
25
-
26
- afterAll(async () => {
27
- await app?.destroy();
28
- });
29
-
30
- it('loads without starting the full app', async () => {
31
- expect(app).toBeTruthy();
32
- });
33
-
34
- it('registers collection definitions', async () => {
35
- const collections = [
36
- 'agentLoopRuns',
37
- 'agentLoopSteps',
38
- 'agentLoopEvents',
39
- 'orchestratorConfig',
40
- 'orchestratorLogs',
41
- 'agentExecutionSpans',
42
- 'agentMemoryContexts',
43
- 'agentHarnessProfiles',
44
- 'agentHarnessProfileVersions',
45
- 'agentLoopPatterns',
46
- 'agentLoopActionApprovals',
47
- 'agentLoopArtifacts',
48
- 'agentLoopWorktrees',
49
- 'agentLoopPathLocks',
50
- 'agentLoopCircuitStates',
51
- 'agentLoopUsageBuckets',
52
- 'agentLoopControlSettings',
53
- 'skillDefinitions',
54
- 'skillExecutions',
55
- 'skillLoopConfigs',
56
- 'skillWorkerConfigs',
57
- ];
58
- for (const name of collections) {
59
- const collection = app.db.getCollection(name);
60
- expect(collection).toBeTruthy();
61
- }
62
- });
63
-
64
- it('has agentLoopRuns schema with required fields and indexes', async () => {
65
- const collection = app.db.getCollection('agentLoopRuns');
66
- expect(collection).toBeTruthy();
67
-
68
- const fields = {
69
- goal: 'text',
70
- status: 'string',
71
- runtimeVersion: 'string',
72
- recordMode: 'string',
73
- patternId: 'bigInt',
74
- pattern: 'belongsTo',
75
- userId: 'bigInt',
76
- policySnapshot: 'json',
77
- planVersion: 'integer',
78
- iterationCount: 'integer',
79
- totalTokens: 'integer',
80
- totalCost: 'float',
81
- };
82
-
83
- for (const [name, type] of Object.entries(fields)) {
84
- const field = collection.getField(name);
85
- expect(field).toBeTruthy();
86
- expect(field.type).toBe(type);
87
- }
88
-
89
- await ensureAgentOrchestratorIndexes(app.db);
90
- await ensureAgentOrchestratorIndexes(app.db);
91
-
92
- const indexes = (await app.db.sequelize
93
- .getQueryInterface()
94
- .showIndex(collection.getTableNameWithSchema())) as DatabaseIndex[];
95
- const fieldNames = (index: DatabaseIndex) => index.fields?.map((field) => field.attribute || field.name).join(',');
96
-
97
- expect(indexes.some((index) => fieldNames(index) === 'userId,status')).toBe(true);
98
- expect(
99
- indexes.some(
100
- (index) =>
101
- index.name === 'agent_loop_runs_pattern_id_trigger_key' &&
102
- index.unique === true &&
103
- fieldNames(index) === 'patternId,triggerKey',
104
- ),
105
- ).toBe(true);
106
-
107
- const indexNames = indexes.flatMap((index) => (index.name ? [index.name] : []));
108
- expect(new Set(indexNames).size).toBe(indexNames.length);
109
- });
110
-
111
- it('can create an agentLoopRun record', async () => {
112
- const repo = app.db.getRepository('agentLoopRuns');
113
- const run = await repo.create({
114
- values: {
115
- rootRunId: 'test-root',
116
- goal: 'Test goal',
117
- status: 'queued',
118
- },
119
- });
120
- expect(run.id).toBeTruthy();
121
- expect(run.goal).toBe('Test goal');
122
- expect(run.status).toBe('queued');
123
- expect(run.rootRunId).toBe('test-root');
124
- });
125
-
126
- it('can create agentLoopSteps with parent-child relationship', async () => {
127
- const runRepo = app.db.getRepository('agentLoopRuns');
128
- const stepRepo = app.db.getRepository('agentLoopSteps');
129
-
130
- const run = await runRepo.create({
131
- values: {
132
- rootRunId: 'test-root-2',
133
- goal: 'Multi-step test',
134
- },
135
- });
136
-
137
- const parentStep = await stepRepo.create({
138
- values: {
139
- runId: run.id,
140
- planKey: 'step_1',
141
- title: 'Parent step',
142
- type: 'reasoning',
143
- status: 'succeeded',
144
- },
145
- });
146
-
147
- const childStep = await stepRepo.create({
148
- values: {
149
- runId: run.id,
150
- planKey: 'step_2',
151
- title: 'Child step',
152
- type: 'skill',
153
- status: 'pending',
154
- dependsOn: ['step_1'],
155
- },
156
- });
157
-
158
- expect(parentStep.id).toBeTruthy();
159
- expect(childStep.id).toBeTruthy();
160
- expect(childStep.dependsOn).toEqual(['step_1']);
161
- });
162
- });
1
+ import { createMockServer, MockServer } from '@nocobase/test';
2
+ import { PluginAgentOrchestratorServer } from '../plugin';
3
+ import { ensureAgentOrchestratorIndexes } from '../utils/ensure-indexes';
4
+
5
+ type DatabaseIndex = {
6
+ name?: string;
7
+ unique?: boolean;
8
+ fields?: Array<{ attribute?: string; name?: string }>;
9
+ };
10
+
11
+ describe('Agent Orchestrator plugin smoke', () => {
12
+ let app: MockServer;
13
+
14
+ beforeAll(async () => {
15
+ const createdApp = await createMockServer({
16
+ plugins: ['nocobase', PluginAgentOrchestratorServer],
17
+ name: `agent-orchestrator-smoke-${process.pid}`,
18
+ skipSupervisor: true,
19
+ beforeInstall: async (mockApp) => {
20
+ app = mockApp;
21
+ },
22
+ });
23
+ app = createdApp;
24
+ });
25
+
26
+ afterAll(async () => {
27
+ await app?.destroy();
28
+ });
29
+
30
+ it('loads without starting the full app', async () => {
31
+ expect(app).toBeTruthy();
32
+ });
33
+
34
+ it('registers collection definitions', async () => {
35
+ const collections = [
36
+ 'agentLoopRuns',
37
+ 'agentLoopSteps',
38
+ 'agentLoopEvents',
39
+ 'orchestratorConfig',
40
+ 'orchestratorLogs',
41
+ 'agentExecutionSpans',
42
+ 'agentMemoryContexts',
43
+ 'agentHarnessProfiles',
44
+ 'agentHarnessProfileVersions',
45
+ 'agentLoopPatterns',
46
+ 'agentLoopActionApprovals',
47
+ 'agentLoopArtifacts',
48
+ 'agentLoopWorktrees',
49
+ 'agentLoopPathLocks',
50
+ 'agentLoopCircuitStates',
51
+ 'agentLoopUsageBuckets',
52
+ 'agentLoopControlSettings',
53
+ 'skillDefinitions',
54
+ 'skillExecutions',
55
+ 'skillLoopConfigs',
56
+ 'skillWorkerConfigs',
57
+ ];
58
+ for (const name of collections) {
59
+ const collection = app.db.getCollection(name);
60
+ expect(collection).toBeTruthy();
61
+ }
62
+ });
63
+
64
+ it('has agentLoopRuns schema with required fields and indexes', async () => {
65
+ const collection = app.db.getCollection('agentLoopRuns');
66
+ expect(collection).toBeTruthy();
67
+
68
+ const fields = {
69
+ goal: 'text',
70
+ status: 'string',
71
+ runtimeVersion: 'string',
72
+ recordMode: 'string',
73
+ patternId: 'bigInt',
74
+ pattern: 'belongsTo',
75
+ userId: 'bigInt',
76
+ policySnapshot: 'json',
77
+ planVersion: 'integer',
78
+ iterationCount: 'integer',
79
+ totalTokens: 'integer',
80
+ totalCost: 'float',
81
+ };
82
+
83
+ for (const [name, type] of Object.entries(fields)) {
84
+ const field = collection.getField(name);
85
+ expect(field).toBeTruthy();
86
+ expect(field.type).toBe(type);
87
+ }
88
+
89
+ await ensureAgentOrchestratorIndexes(app.db);
90
+ await ensureAgentOrchestratorIndexes(app.db);
91
+
92
+ const indexes = (await app.db.sequelize
93
+ .getQueryInterface()
94
+ .showIndex(collection.getTableNameWithSchema())) as DatabaseIndex[];
95
+ const fieldNames = (index: DatabaseIndex) => index.fields?.map((field) => field.attribute || field.name).join(',');
96
+
97
+ expect(indexes.some((index) => fieldNames(index) === 'userId,status')).toBe(true);
98
+ expect(
99
+ indexes.some(
100
+ (index) =>
101
+ index.name === 'agent_loop_runs_pattern_id_trigger_key' &&
102
+ index.unique === true &&
103
+ fieldNames(index) === 'patternId,triggerKey',
104
+ ),
105
+ ).toBe(true);
106
+
107
+ const indexNames = indexes.flatMap((index) => (index.name ? [index.name] : []));
108
+ expect(new Set(indexNames).size).toBe(indexNames.length);
109
+ });
110
+
111
+ it('can create an agentLoopRun record', async () => {
112
+ const repo = app.db.getRepository('agentLoopRuns');
113
+ const run = await repo.create({
114
+ values: {
115
+ rootRunId: 'test-root',
116
+ goal: 'Test goal',
117
+ status: 'queued',
118
+ },
119
+ });
120
+ expect(run.id).toBeTruthy();
121
+ expect(run.goal).toBe('Test goal');
122
+ expect(run.status).toBe('queued');
123
+ expect(run.rootRunId).toBe('test-root');
124
+ });
125
+
126
+ it('can create agentLoopSteps with parent-child relationship', async () => {
127
+ const runRepo = app.db.getRepository('agentLoopRuns');
128
+ const stepRepo = app.db.getRepository('agentLoopSteps');
129
+
130
+ const run = await runRepo.create({
131
+ values: {
132
+ rootRunId: 'test-root-2',
133
+ goal: 'Multi-step test',
134
+ },
135
+ });
136
+
137
+ const parentStep = await stepRepo.create({
138
+ values: {
139
+ runId: run.id,
140
+ planKey: 'step_1',
141
+ title: 'Parent step',
142
+ type: 'reasoning',
143
+ status: 'succeeded',
144
+ },
145
+ });
146
+
147
+ const childStep = await stepRepo.create({
148
+ values: {
149
+ runId: run.id,
150
+ planKey: 'step_2',
151
+ title: 'Child step',
152
+ type: 'skill',
153
+ status: 'pending',
154
+ dependsOn: ['step_1'],
155
+ },
156
+ });
157
+
158
+ expect(parentStep.id).toBeTruthy();
159
+ expect(childStep.id).toBeTruthy();
160
+ expect(childStep.dependsOn).toEqual(['step_1']);
161
+ });
162
+ });
@@ -0,0 +1,275 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { Database } from '@nocobase/database';
3
+ import { loopPatternPolicySchema } from '../services/LoopPatternSchema';
4
+ import { getRunEventBus } from '../services/RunEventBus';
5
+ import {
6
+ ToolLoopDetectionService,
7
+ countToolRepetitions,
8
+ toolCallSignature,
9
+ toolLoopLevel,
10
+ } from '../services/ToolLoopDetectionService';
11
+ import type { ToolLoopFinding } from '../services/ToolLoopDetectionService';
12
+
13
+ type Row = Record<string, unknown>;
14
+
15
+ const defaultPolicy = loopPatternPolicySchema.parse({});
16
+
17
+ function matches(row: Row, filter: Row): boolean {
18
+ return Object.entries(filter).every(([key, value]) => {
19
+ if (value && typeof value === 'object' && Array.isArray((value as Row).$in)) {
20
+ return ((value as Row).$in as unknown[]).includes(row[key]);
21
+ }
22
+ return row[key] === value;
23
+ });
24
+ }
25
+
26
+ function mockDatabase(options: { steps?: Row[]; messages?: Row[] } = {}) {
27
+ const steps: Row[] = [...(options.steps ?? [])];
28
+ const events: Row[] = [];
29
+ let nextId = 1;
30
+ const database = {
31
+ getRepository(name: string) {
32
+ if (name === 'agentLoopSteps') {
33
+ return {
34
+ find: async ({ filter }: { filter: Row }) => steps.filter((row) => matches(row, filter)),
35
+ findOne: async ({ filter }: { filter: Row }) => steps.find((row) => matches(row, filter)) ?? null,
36
+ create: async ({ values }: { values: Row }) => {
37
+ const row = { id: nextId++, ...values };
38
+ steps.push(row);
39
+ return row;
40
+ },
41
+ };
42
+ }
43
+ if (name === 'aiMessages') {
44
+ return {
45
+ find: async ({ filter }: { filter: Row }) => (options.messages ?? []).filter((row) => matches(row, filter)),
46
+ };
47
+ }
48
+ if (name === 'agentLoopEvents') {
49
+ return {
50
+ create: async ({ values }: { values: Row }) => {
51
+ const row = { id: nextId++, ...values };
52
+ events.push(row);
53
+ return row;
54
+ },
55
+ };
56
+ }
57
+ throw new Error(`Unexpected repository ${name}`);
58
+ },
59
+ } as unknown as Database;
60
+ return { database, steps, events };
61
+ }
62
+
63
+ function message(sessionId: string, calls: Array<{ id: string; name: string; args: unknown }>): Row {
64
+ return { sessionId, role: 'assistant', toolCalls: calls };
65
+ }
66
+
67
+ function finding(overrides: Partial<ToolLoopFinding> = {}): ToolLoopFinding {
68
+ return {
69
+ level: 'warn',
70
+ toolName: 'searchDocs',
71
+ signature: toolCallSignature('searchDocs', { query: 'loop' }),
72
+ count: 3,
73
+ sampleArgs: { query: 'loop' },
74
+ ...overrides,
75
+ };
76
+ }
77
+
78
+ describe('toolCallSignature', () => {
79
+ it('is stable across object key order', () => {
80
+ expect(toolCallSignature('search', { a: 1, b: 2 })).toBe(toolCallSignature('search', { b: 2, a: 1 }));
81
+ });
82
+
83
+ it('separates the tool name from its arguments', () => {
84
+ expect(toolCallSignature('a 1', null)).not.toBe(toolCallSignature('a', '1 null'));
85
+ expect(toolCallSignature('search', { a: 1 })).not.toBe(toolCallSignature('search', { a: 2 }));
86
+ });
87
+
88
+ it('treats missing args the same as null', () => {
89
+ expect(toolCallSignature('ping', undefined)).toBe(toolCallSignature('ping', null));
90
+ });
91
+ });
92
+
93
+ describe('countToolRepetitions', () => {
94
+ it('counts identical calls and separates differing args', () => {
95
+ const counts = countToolRepetitions([
96
+ { name: 'search', args: { q: 'x' } },
97
+ { name: 'search', args: { q: 'x' } },
98
+ { name: 'search', args: { q: 'y' } },
99
+ { name: 'write', args: { q: 'x' } },
100
+ { name: '', args: {} },
101
+ ]);
102
+
103
+ expect(counts.size).toBe(3);
104
+ const repeated = [...counts.values()].find((entry) => entry.count === 2);
105
+ expect(repeated?.toolName).toBe('search');
106
+ expect(repeated?.sampleArgs).toEqual({ q: 'x' });
107
+ });
108
+ });
109
+
110
+ describe('toolLoopLevel', () => {
111
+ it('maps counts onto the policy thresholds', () => {
112
+ const detection = defaultPolicy.loopDetection;
113
+ expect(toolLoopLevel(2, detection)).toBe('none');
114
+ expect(toolLoopLevel(3, detection)).toBe('warn');
115
+ expect(toolLoopLevel(5, detection)).toBe('block');
116
+ expect(toolLoopLevel(8, detection)).toBe('escalate');
117
+ expect(toolLoopLevel(100, detection)).toBe('escalate');
118
+ });
119
+
120
+ it('returns none when detection is disabled', () => {
121
+ expect(toolLoopLevel(100, { ...defaultPolicy.loopDetection, enabled: false })).toBe('none');
122
+ });
123
+ });
124
+
125
+ describe('ToolLoopDetectionService.scanRun', () => {
126
+ it('returns the most repeated signature across every pass session of the run', async () => {
127
+ const { database } = mockDatabase({
128
+ steps: [
129
+ { runId: 1, sessionId: 's1' },
130
+ { runId: 1, sessionId: 's2' },
131
+ { runId: 2, sessionId: 'other-run' },
132
+ ],
133
+ messages: [
134
+ message('s1', [
135
+ { id: 'c1', name: 'searchDocs', args: { query: 'loop' } },
136
+ { id: 'c2', name: 'searchDocs', args: { query: 'loop' } },
137
+ ]),
138
+ message('s2', [
139
+ { id: 'c3', name: 'searchDocs', args: { query: 'loop' } },
140
+ { id: 'c4', name: 'readFile', args: { path: '/tmp/a' } },
141
+ ]),
142
+ message('other-run', [
143
+ { id: 'c5', name: 'searchDocs', args: { query: 'loop' } },
144
+ { id: 'c6', name: 'searchDocs', args: { query: 'loop' } },
145
+ ]),
146
+ ],
147
+ });
148
+ const service = new ToolLoopDetectionService(database);
149
+
150
+ const result = await service.scanRun(1, defaultPolicy);
151
+
152
+ expect(result).toMatchObject({ level: 'warn', toolName: 'searchDocs', count: 3 });
153
+ });
154
+
155
+ it('returns null while the run stays under the warn threshold', async () => {
156
+ const { database } = mockDatabase({
157
+ steps: [{ runId: 1, sessionId: 's1' }],
158
+ messages: [
159
+ message('s1', [
160
+ { id: 'c1', name: 'searchDocs', args: { query: 'loop' } },
161
+ { id: 'c2', name: 'searchDocs', args: { query: 'loop' } },
162
+ ]),
163
+ ],
164
+ });
165
+ const service = new ToolLoopDetectionService(database);
166
+
167
+ expect(await service.scanRun(1, defaultPolicy)).toBeNull();
168
+ });
169
+
170
+ it('returns null when detection is disabled', async () => {
171
+ const { database } = mockDatabase({
172
+ steps: [{ runId: 1, sessionId: 's1' }],
173
+ messages: [
174
+ message('s1', [
175
+ { id: 'c1', name: 'searchDocs', args: { query: 'loop' } },
176
+ { id: 'c2', name: 'searchDocs', args: { query: 'loop' } },
177
+ { id: 'c3', name: 'searchDocs', args: { query: 'loop' } },
178
+ ]),
179
+ ],
180
+ });
181
+ const service = new ToolLoopDetectionService(database);
182
+ const policy = loopPatternPolicySchema.parse({ loopDetection: { enabled: false } });
183
+
184
+ expect(await service.scanRun(1, policy)).toBeNull();
185
+ });
186
+
187
+ it('returns null when no pass session recorded tool calls', async () => {
188
+ const { database } = mockDatabase({ steps: [{ runId: 1, sessionId: 's1' }], messages: [] });
189
+ const service = new ToolLoopDetectionService(database);
190
+
191
+ expect(await service.scanRun(1, defaultPolicy)).toBeNull();
192
+ expect(await service.scanRun(99, defaultPolicy)).toBeNull();
193
+ });
194
+
195
+ it('ignores malformed tool call entries', async () => {
196
+ const { database } = mockDatabase({
197
+ steps: [{ runId: 1, sessionId: 's1' }],
198
+ messages: [
199
+ {
200
+ sessionId: 's1',
201
+ role: 'assistant',
202
+ toolCalls: [
203
+ { id: 'c1', args: { query: 'loop' } },
204
+ { id: 'c2', name: 'searchDocs', args: { query: 'loop' } },
205
+ 'not-an-object',
206
+ ],
207
+ },
208
+ ],
209
+ });
210
+ const service = new ToolLoopDetectionService(database);
211
+
212
+ expect(await service.scanRun(1, defaultPolicy)).toBeNull();
213
+ });
214
+ });
215
+
216
+ describe('ToolLoopDetectionService.recordFinding', () => {
217
+ it('records one guard step and one event per signature and emits on the run bus', async () => {
218
+ const { database, steps, events } = mockDatabase({ steps: [{ runId: 42, sessionId: 's1' }] });
219
+ const service = new ToolLoopDetectionService(database);
220
+ const seen: unknown[] = [];
221
+ const unsubscribe = getRunEventBus().subscribe(42, (event) => seen.push(event));
222
+ const context = {
223
+ runId: 42,
224
+ role: 'leader',
225
+ username: 'lead',
226
+ runStatus: 'running',
227
+ actorType: 'worker',
228
+ actorIdentity: 'worker-a',
229
+ };
230
+
231
+ try {
232
+ expect(await service.recordFinding(context, finding())).toBe(true);
233
+ expect(await service.recordFinding(context, finding())).toBe(false);
234
+ } finally {
235
+ unsubscribe();
236
+ }
237
+
238
+ const guardSteps = steps.filter((step) => step.kind === 'guard');
239
+ expect(guardSteps).toHaveLength(1);
240
+ expect(guardSteps[0]).toMatchObject({
241
+ runId: 42,
242
+ type: 'tool_loop',
243
+ toolName: 'searchDocs',
244
+ status: 'succeeded',
245
+ });
246
+ expect(events).toHaveLength(1);
247
+ expect(events[0]).toMatchObject({ runId: 42, type: 'tool_loop_detected', status: 'running' });
248
+ expect(seen).toHaveLength(1);
249
+ });
250
+ });
251
+
252
+ describe('ToolLoopDetectionService.existingNotices', () => {
253
+ it('rebuilds notices from recorded guard steps', async () => {
254
+ const { database } = mockDatabase({
255
+ steps: [
256
+ {
257
+ runId: 7,
258
+ kind: 'guard',
259
+ toolName: 'searchDocs',
260
+ metadata: { level: 'warn', count: 3 },
261
+ },
262
+ { runId: 7, kind: 'guard', toolName: '', metadata: { count: 5 } },
263
+ { runId: 7, kind: 'invocation', toolName: 'other', metadata: {} },
264
+ ],
265
+ });
266
+ const service = new ToolLoopDetectionService(database);
267
+
268
+ const notices = await service.existingNotices(7);
269
+
270
+ expect(notices).toHaveLength(1);
271
+ expect(notices[0]).toContain('<tool_loop_warning>');
272
+ expect(notices[0]).toContain('searchDocs');
273
+ expect(notices[0]).toContain('3 times');
274
+ });
275
+ });
@@ -63,6 +63,14 @@ export default defineCollection({
63
63
  type: 'string',
64
64
  length: 100,
65
65
  },
66
+ {
67
+ // Delegation depth of a sub-agent span: 1 for a sub-agent dispatched from a top-level
68
+ // conversation, parent depth + 1 below that. Persisted so a resumed child can never reset
69
+ // its depth, and the harness delegation.maxDepth cap is enforceable across restarts.
70
+ name: 'depth',
71
+ type: 'integer',
72
+ defaultValue: 0,
73
+ },
66
74
  {
67
75
  name: 'source',
68
76
  type: 'string',
@@ -1,26 +1,26 @@
1
- import { defineCollection } from '@nocobase/database';
2
-
3
- export default defineCollection({
4
- name: 'agentHarnessProfileVersions',
5
- title: 'Agent Harness Profile Versions',
6
- fields: [
7
- { name: 'id', type: 'bigInt', autoIncrement: true, primaryKey: true },
8
- { name: 'profileId', type: 'bigInt', allowNull: false },
9
- {
10
- name: 'profile',
11
- type: 'belongsTo',
12
- target: 'agentHarnessProfiles',
13
- foreignKey: 'profileId',
14
- allowNull: false,
15
- },
16
- { name: 'version', type: 'integer', allowNull: false },
17
- { name: 'schemaVersion', type: 'integer', allowNull: false, defaultValue: 1 },
18
- { name: 'status', type: 'string', length: 20, allowNull: false, defaultValue: 'draft' },
19
- { name: 'settings', type: 'json', allowNull: false, defaultValue: {} },
20
- { name: 'publishedBy', type: 'belongsTo', target: 'users', foreignKey: 'publishedById' },
21
- { name: 'publishedAt', type: 'date' },
22
- { name: 'createdAt', type: 'date' },
23
- { name: 'updatedAt', type: 'date' },
24
- ],
25
- indexes: [{ unique: true, fields: ['profileId', 'version'] }, { fields: ['profileId', 'status'] }],
26
- });
1
+ import { defineCollection } from '@nocobase/database';
2
+
3
+ export default defineCollection({
4
+ name: 'agentHarnessProfileVersions',
5
+ title: 'Agent Harness Profile Versions',
6
+ fields: [
7
+ { name: 'id', type: 'bigInt', autoIncrement: true, primaryKey: true },
8
+ { name: 'profileId', type: 'bigInt', allowNull: false },
9
+ {
10
+ name: 'profile',
11
+ type: 'belongsTo',
12
+ target: 'agentHarnessProfiles',
13
+ foreignKey: 'profileId',
14
+ allowNull: false,
15
+ },
16
+ { name: 'version', type: 'integer', allowNull: false },
17
+ { name: 'schemaVersion', type: 'integer', allowNull: false, defaultValue: 1 },
18
+ { name: 'status', type: 'string', length: 20, allowNull: false, defaultValue: 'draft' },
19
+ { name: 'settings', type: 'json', allowNull: false, defaultValue: {} },
20
+ { name: 'publishedBy', type: 'belongsTo', target: 'users', foreignKey: 'publishedById' },
21
+ { name: 'publishedAt', type: 'date' },
22
+ { name: 'createdAt', type: 'date' },
23
+ { name: 'updatedAt', type: 'date' },
24
+ ],
25
+ indexes: [{ unique: true, fields: ['profileId', 'version'] }, { fields: ['profileId', 'status'] }],
26
+ });