plugin-agent-orchestrator 1.1.6 → 1.1.7

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 (69) hide show
  1. package/dist/client/index.js +1 -1
  2. package/dist/client-v2/index.js +1 -1
  3. package/dist/externalVersion.js +7 -7
  4. package/dist/server/collections/agent-loop-runs.js +6 -0
  5. package/dist/server/collections/skill-metrics.js +66 -0
  6. package/dist/server/plugin.js +91 -2
  7. package/dist/server/resources/agent-loop-events-stream.js +1 -0
  8. package/dist/server/resources/agent-loop.js +1 -1
  9. package/dist/server/services/DurableCircuitBreakerService.js +9 -12
  10. package/dist/server/services/ExecutionSpanService.js +8 -3
  11. package/dist/server/services/HarnessProfileService.js +23 -26
  12. package/dist/server/services/LoopBudgetService.js +36 -39
  13. package/dist/server/services/LoopControlService.js +8 -11
  14. package/dist/server/services/LoopPatternSchema.js +51 -17
  15. package/dist/server/services/LoopPatternService.js +31 -11
  16. package/dist/server/services/LoopRunRepository.js +2 -5
  17. package/dist/server/services/LoopRunStateMachine.js +31 -34
  18. package/dist/server/services/LoopSchedulerService.js +4 -7
  19. package/dist/server/services/LoopTriggerService.js +11 -6
  20. package/dist/server/services/LoopWorkerService.js +88 -22
  21. package/dist/server/services/PathLockService.js +19 -22
  22. package/dist/server/services/PluginAiRuntimeAdapter.js +51 -8
  23. package/dist/server/services/RunEventBus.js +14 -2
  24. package/dist/server/services/SkillAccessService.js +50 -1
  25. package/dist/server/services/TokenTracker.js +17 -1
  26. package/dist/server/services/ToolLoopDetectionService.js +14 -17
  27. package/dist/server/services/VerificationService.js +47 -16
  28. package/dist/server/services/harness-runtime-policy.js +7 -12
  29. package/dist/server/skill-hub/plugin.js +34 -27
  30. package/dist/server/utils/ha-lock.js +73 -0
  31. package/dist/server/utils/loop-pattern-context.js +42 -5
  32. package/dist/server/utils/record-utils.js +44 -0
  33. package/package.json +1 -1
  34. package/src/client/plugin.tsx +1 -1
  35. package/src/client-v2/plugin.tsx +14 -13
  36. package/src/server/__tests__/tool-loop-detection.test.ts +1 -0
  37. package/src/server/collections/agent-loop-runs.ts +7 -0
  38. package/src/server/collections/skill-metrics.ts +36 -0
  39. package/src/server/plugin.ts +117 -1
  40. package/src/server/resources/agent-loop-events-stream.ts +108 -105
  41. package/src/server/resources/agent-loop.ts +1 -1
  42. package/src/server/resources/tracing.ts +267 -266
  43. package/src/server/services/DurableCircuitBreakerService.ts +235 -239
  44. package/src/server/services/ExecutionSpanService.ts +37 -12
  45. package/src/server/services/HarnessProfileService.ts +1 -5
  46. package/src/server/services/LoopBudgetService.ts +271 -275
  47. package/src/server/services/LoopControlService.ts +120 -124
  48. package/src/server/services/LoopPatternSchema.ts +60 -14
  49. package/src/server/services/LoopPatternService.ts +171 -143
  50. package/src/server/services/LoopRunRepository.ts +1 -5
  51. package/src/server/services/LoopRunStateMachine.ts +1 -5
  52. package/src/server/services/LoopSchedulerService.ts +195 -199
  53. package/src/server/services/LoopTriggerService.ts +7 -5
  54. package/src/server/services/LoopWorkerService.ts +102 -12
  55. package/src/server/services/PathLockService.ts +317 -321
  56. package/src/server/services/PluginAiRuntimeAdapter.ts +59 -4
  57. package/src/server/services/RunEventBus.ts +13 -0
  58. package/src/server/services/SkillAccessService.ts +80 -5
  59. package/src/server/services/TokenTracker.ts +59 -7
  60. package/src/server/services/ToolLoopDetectionService.ts +8 -11
  61. package/src/server/services/VerificationSchema.ts +31 -31
  62. package/src/server/services/VerificationService.ts +60 -11
  63. package/src/server/services/harness-runtime-policy.ts +2 -9
  64. package/src/server/skill-hub/plugin.ts +36 -28
  65. package/src/server/types.ts +5 -39
  66. package/src/server/utils/ensure-indexes.ts +96 -96
  67. package/src/server/utils/ha-lock.ts +80 -0
  68. package/src/server/utils/loop-pattern-context.ts +84 -20
  69. package/src/server/utils/record-utils.ts +19 -0
@@ -14,6 +14,7 @@ import { createGitImportRequestMethodPolicy } from './middlewares/git-import-req
14
14
  import { parseJsonText, stringifyJsonText, parseJsonLike } from './utils/json-fields';
15
15
  import { getOrchestratorTraceContext } from '../services/ExecutionSpanService';
16
16
  import { harnessTimeoutMs, resolveTraceHarness } from '../services/harness-runtime-policy';
17
+ import { runWithDistributedLock } from '../utils/ha-lock';
17
18
  import { tryGetAIToolsManager } from '../utils/ai-manager';
18
19
  import type { ToolsRuntime } from '@nocobase/ai';
19
20
  import { SkillAccessService } from '../services/SkillAccessService';
@@ -367,14 +368,16 @@ export class SkillHubSubFeature {
367
368
  return;
368
369
  }
369
370
 
370
- const tick = () => {
371
- this.processSkillExecutionQueue().catch((error) => {
372
- (this as any).app.logger.warn(`[skill-hub] Pending task poll failed: ${error?.message || error}`);
373
- });
371
+ const tick = async () => {
372
+ await this.processSkillExecutionQueue();
374
373
  };
375
374
 
376
- this.skillTaskPoller = setInterval(tick, SKILL_TASK_POLL_INTERVAL_MS);
377
- (this.skillTaskPoller as any).unref?.();
375
+ this.skillTaskPoller = runWithDistributedLock(
376
+ (this as any).app,
377
+ 'skill-hub:task-poller',
378
+ tick,
379
+ SKILL_TASK_POLL_INTERVAL_MS,
380
+ );
378
381
  setTimeout(tick, 1000);
379
382
  }
380
383
 
@@ -979,35 +982,40 @@ export class SkillHubSubFeature {
979
982
  // Check old execution files every hour, rate limiter every 5 minutes
980
983
  const CLEANUP_INTERVAL = 60 * 60 * 1000; // 1 hour
981
984
 
982
- this.cleanupInterval = setInterval(async () => {
983
- // 1. Storage Retention Cleanup
984
- try {
985
- const config = await (this as any).db.getRepository('skillWorkerConfigs').findOne();
986
- const hours = config ? config.get('retentionHours') : 24;
985
+ this.cleanupInterval = runWithDistributedLock(
986
+ (this as any).app,
987
+ 'skill-hub:cleanup',
988
+ async () => {
989
+ // 1. Storage Retention Cleanup
990
+ try {
991
+ const config = await (this as any).db.getRepository('skillWorkerConfigs').findOne();
992
+ const hours = config ? config.get('retentionHours') : 24;
987
993
 
988
- if (hours && hours > 0) {
989
- const MAX_AGE_MS = hours * 60 * 60 * 1000;
990
- const cutoff = new Date(Date.now() - MAX_AGE_MS);
991
- const repo = (this as any).db.getRepository('skillExecutions');
994
+ if (hours && hours > 0) {
995
+ const MAX_AGE_MS = hours * 60 * 60 * 1000;
996
+ const cutoff = new Date(Date.now() - MAX_AGE_MS);
997
+ const repo = (this as any).db.getRepository('skillExecutions');
992
998
 
993
- const outdated = await repo.find({
994
- filter: { createdAt: { $lt: cutoff } },
995
- });
999
+ const outdated = await repo.find({
1000
+ filter: { createdAt: { $lt: cutoff } },
1001
+ });
996
1002
 
997
- if (outdated.length > 0) {
998
- for (const record of outdated) {
999
- await record.destroy(); // Fires afterDestroy hook which removes physical folder
1003
+ if (outdated.length > 0) {
1004
+ for (const record of outdated) {
1005
+ await record.destroy(); // Fires afterDestroy hook which removes physical folder
1006
+ }
1007
+ (this as any).app.logger.info(`[skill-hub] Auto-cleaned up ${outdated.length} expired execution records`);
1000
1008
  }
1001
- (this as any).app.logger.info(`[skill-hub] Auto-cleaned up ${outdated.length} expired execution records`);
1002
1009
  }
1010
+ } catch (err) {
1011
+ (this as any).app.logger.warn('[skill-hub] Auto Cleanup error:', err);
1003
1012
  }
1004
- } catch (err) {
1005
- (this as any).app.logger.warn('[skill-hub] Auto Cleanup error:', err);
1006
- }
1007
1013
 
1008
- // 2. Cleanup rate limiter stale entries
1009
- this.rateLimiter.cleanup();
1010
- }, CLEANUP_INTERVAL);
1014
+ // 2. Cleanup rate limiter stale entries
1015
+ this.rateLimiter.cleanup();
1016
+ },
1017
+ CLEANUP_INTERVAL,
1018
+ );
1011
1019
  }
1012
1020
 
1013
1021
  async beforeStop() {
@@ -1,37 +1,7 @@
1
1
  // ── Shared types for Agent Orchestrator ──────────────────────────────────
2
-
3
- export interface TokenUsage {
4
- inputTokens: number;
5
- outputTokens: number;
6
- totalTokens: number;
7
- cost: number;
8
- }
9
-
10
- export interface BudgetConfig {
11
- budgetMaxTokens?: number;
12
- budgetMaxCost?: number;
13
- }
14
-
15
- export interface BudgetCheckResult {
16
- allowed: boolean;
17
- reason?: string;
18
- }
19
-
20
- export interface CircuitState {
21
- failures: number;
22
- lastFailureTime: number;
23
- state: 'closed' | 'open' | 'half-open';
24
- }
25
-
26
- export interface TraceEvent {
27
- type: string;
28
- at: string;
29
- title: string;
30
- content?: string;
31
- toolName?: string;
32
- args?: any;
33
- status?: string;
34
- }
2
+ // Re-export canonical types from their implementation modules.
3
+ // This file exists for backward compatibility; prefer direct imports.
4
+ export type { TokenUsage, BudgetConfig, BudgetCheckResult } from './services/TokenTracker';
35
5
 
36
6
  export interface DelegationLogData {
37
7
  id?: number | string;
@@ -45,11 +15,7 @@ export interface DelegationLogData {
45
15
  depth: number;
46
16
  durationMs: number;
47
17
  error?: string;
48
- trace?: TraceEvent[];
49
- messages?: any[];
18
+ trace?: unknown[];
19
+ messages?: unknown[];
50
20
  userId?: number | string;
51
21
  }
52
-
53
- export type CtxSnapshot = {
54
- userId?: number;
55
- };
@@ -1,96 +1,96 @@
1
- import type { Database } from '@nocobase/database';
2
-
3
- const INDEXED_COLLECTIONS = [
4
- 'agentExecutionSpans',
5
- 'agentHarnessProfiles',
6
- 'agentHarnessProfileVersions',
7
- 'agentLoopActionApprovals',
8
- 'agentLoopArtifacts',
9
- 'agentLoopCircuitStates',
10
- 'agentLoopControlSettings',
11
- 'agentLoopEvents',
12
- 'agentLoopPathLocks',
13
- 'agentLoopPatterns',
14
- 'agentLoopRuns',
15
- 'agentLoopSteps',
16
- 'agentLoopUsageBuckets',
17
- 'agentLoopWorktrees',
18
- 'agentMemoryContexts',
19
- 'orchestratorConfig',
20
- 'skillRegistryInstallations',
21
- ] as const;
22
-
23
- type PhysicalIndex = {
24
- name?: string;
25
- fields?: Array<{ attribute?: string } | string>;
26
- };
27
-
28
- function resolvePhysicalField(collection: ReturnType<Database['getCollection']>, field: unknown): string | null {
29
- if (typeof field === 'string') {
30
- return collection.model.rawAttributes[field]?.field || field;
31
- }
32
- if (field && typeof field === 'object' && 'name' in field && typeof (field as { name: unknown }).name === 'string') {
33
- const name = (field as { name: string }).name;
34
- return collection.model.rawAttributes[name]?.field || name;
35
- }
36
- return null;
37
- }
38
-
39
- function deriveIndexName(tableName: unknown, fieldNames: string[]) {
40
- const table =
41
- typeof tableName === 'string' ? tableName : String((tableName as { tableName?: string })?.tableName ?? '');
42
- return `${table}_${fieldNames.join('_')}`.replace(/[^a-zA-Z0-9_]/g, '_');
43
- }
44
-
45
- function physicalIndexFieldKey(index: PhysicalIndex): string {
46
- return (index.fields || []).map((field) => (typeof field === 'string' ? field : field?.attribute || '')).join(',');
47
- }
48
-
49
- export async function ensureAgentOrchestratorIndexes(database: Database) {
50
- const queryInterface = database.sequelize.getQueryInterface();
51
-
52
- for (const collectionName of INDEXED_COLLECTIONS) {
53
- const collection = database.getCollection(collectionName);
54
- if (!collection) continue;
55
-
56
- const tableName = collection.getTableNameWithSchema();
57
- const physicalIndexes = (await queryInterface.showIndex(tableName)) as PhysicalIndex[];
58
- const physicalIndexNames = new Set(physicalIndexes.flatMap((index) => (index.name ? [index.name] : [])));
59
- // Track existing indexes by their ordered column composition so we skip an
60
- // index the framework's own db.sync() already created under a different name.
61
- const physicalFieldKeys = new Set(physicalIndexes.map(physicalIndexFieldKey));
62
-
63
- for (const index of collection.model.options.indexes || []) {
64
- if (!index.fields?.length) continue;
65
-
66
- const physicalFieldNames = index.fields
67
- .map((field) => resolvePhysicalField(collection, field))
68
- .filter((name): name is string => !!name);
69
- if (physicalFieldKeys.has(physicalFieldNames.join(','))) continue;
70
-
71
- const resolvedFields = index.fields.map((field) => {
72
- if (typeof field === 'string') {
73
- return collection.model.rawAttributes[field]?.field || field;
74
- }
75
- if (field && typeof field === 'object' && 'name' in field && typeof field.name === 'string') {
76
- return {
77
- ...field,
78
- name: collection.model.rawAttributes[field.name]?.field || field.name,
79
- };
80
- }
81
- return field;
82
- });
83
-
84
- const indexName = index.name || deriveIndexName(tableName, physicalFieldNames);
85
- if (physicalIndexNames.has(indexName)) continue;
86
-
87
- await queryInterface.addIndex(tableName, {
88
- name: indexName,
89
- fields: resolvedFields,
90
- unique: index.unique,
91
- });
92
- physicalIndexNames.add(indexName);
93
- physicalFieldKeys.add(physicalFieldNames.join(','));
94
- }
95
- }
96
- }
1
+ import type { Database } from '@nocobase/database';
2
+
3
+ const INDEXED_COLLECTIONS = [
4
+ 'agentExecutionSpans',
5
+ 'agentHarnessProfiles',
6
+ 'agentHarnessProfileVersions',
7
+ 'agentLoopActionApprovals',
8
+ 'agentLoopArtifacts',
9
+ 'agentLoopCircuitStates',
10
+ 'agentLoopControlSettings',
11
+ 'agentLoopEvents',
12
+ 'agentLoopPathLocks',
13
+ 'agentLoopPatterns',
14
+ 'agentLoopRuns',
15
+ 'agentLoopSteps',
16
+ 'agentLoopUsageBuckets',
17
+ 'agentLoopWorktrees',
18
+ 'agentMemoryContexts',
19
+ 'orchestratorConfig',
20
+ 'skillRegistryInstallations',
21
+ ] as const;
22
+
23
+ type PhysicalIndex = {
24
+ name?: string;
25
+ fields?: Array<{ attribute?: string } | string>;
26
+ };
27
+
28
+ function resolvePhysicalField(collection: ReturnType<Database['getCollection']>, field: unknown): string | null {
29
+ if (typeof field === 'string') {
30
+ return collection.model.rawAttributes[field]?.field || field;
31
+ }
32
+ if (field && typeof field === 'object' && 'name' in field && typeof (field as { name: unknown }).name === 'string') {
33
+ const name = (field as { name: string }).name;
34
+ return collection.model.rawAttributes[name]?.field || name;
35
+ }
36
+ return null;
37
+ }
38
+
39
+ function deriveIndexName(tableName: unknown, fieldNames: string[]) {
40
+ const table =
41
+ typeof tableName === 'string' ? tableName : String((tableName as { tableName?: string })?.tableName ?? '');
42
+ return `${table}_${fieldNames.join('_')}`.replace(/[^a-zA-Z0-9_]/g, '_');
43
+ }
44
+
45
+ function physicalIndexFieldKey(index: PhysicalIndex): string {
46
+ return (index.fields || []).map((field) => (typeof field === 'string' ? field : field?.attribute || '')).join(',');
47
+ }
48
+
49
+ export async function ensureAgentOrchestratorIndexes(database: Database) {
50
+ const queryInterface = database.sequelize.getQueryInterface();
51
+
52
+ for (const collectionName of INDEXED_COLLECTIONS) {
53
+ const collection = database.getCollection(collectionName);
54
+ if (!collection) continue;
55
+
56
+ const tableName = collection.getTableNameWithSchema();
57
+ const physicalIndexes = (await queryInterface.showIndex(tableName)) as PhysicalIndex[];
58
+ const physicalIndexNames = new Set(physicalIndexes.flatMap((index) => (index.name ? [index.name] : [])));
59
+ // Track existing indexes by their ordered column composition so we skip an
60
+ // index the framework's own db.sync() already created under a different name.
61
+ const physicalFieldKeys = new Set(physicalIndexes.map(physicalIndexFieldKey));
62
+
63
+ for (const index of collection.model.options.indexes || []) {
64
+ if (!index.fields?.length) continue;
65
+
66
+ const physicalFieldNames = index.fields
67
+ .map((field) => resolvePhysicalField(collection, field))
68
+ .filter((name): name is string => !!name);
69
+ if (physicalFieldKeys.has(physicalFieldNames.join(','))) continue;
70
+
71
+ const resolvedFields = index.fields.map((field) => {
72
+ if (typeof field === 'string') {
73
+ return collection.model.rawAttributes[field]?.field || field;
74
+ }
75
+ if (field && typeof field === 'object' && 'name' in field && typeof field.name === 'string') {
76
+ return {
77
+ ...field,
78
+ name: collection.model.rawAttributes[field.name]?.field || field.name,
79
+ };
80
+ }
81
+ return field;
82
+ });
83
+
84
+ const indexName = index.name || deriveIndexName(tableName, physicalFieldNames);
85
+ if (physicalIndexNames.has(indexName)) continue;
86
+
87
+ await queryInterface.addIndex(tableName, {
88
+ name: indexName,
89
+ fields: resolvedFields,
90
+ unique: index.unique,
91
+ });
92
+ physicalIndexNames.add(indexName);
93
+ physicalFieldKeys.add(physicalFieldNames.join(','));
94
+ }
95
+ }
96
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * HA-lock helper for active-active deployments.
3
+ *
4
+ * Wraps a repeating interval so that only one app node executes the task per
5
+ * interval. Uses app.lockManager which falls back to the in-process
6
+ * LocalLockAdapter when LOCK_ADAPTER_DEFAULT is not set, or uses
7
+ * RedisLockAdapter when LOCK_ADAPTER_DEFAULT=redis (as in the HA compose).
8
+ *
9
+ * Other nodes skip their tick when the lock is held. If the owner crashes,
10
+ * the lock expires and another node picks up the next tick.
11
+ */
12
+
13
+ type LockManagerLike = {
14
+ tryAcquire?: (
15
+ key: string,
16
+ timeout?: number,
17
+ ) => Promise<{
18
+ runExclusive: <T>(task: () => Promise<T>, ttl: number) => Promise<T>;
19
+ }>;
20
+ };
21
+
22
+ type AppLike = {
23
+ lockManager?: LockManagerLike;
24
+ logger?: {
25
+ debug?: (...args: unknown[]) => void;
26
+ warn?: (...args: unknown[]) => void;
27
+ info?: (...args: unknown[]) => void;
28
+ };
29
+ };
30
+
31
+ function isLockAcquireError(error: unknown): boolean {
32
+ if (!error) return false;
33
+ if (typeof error === 'object' && typeof (error as { constructor?: unknown }).constructor === 'function') {
34
+ const name = (error as { constructor?: { name?: string } }).constructor?.name;
35
+ if (name === 'LockAcquireError' || name === 'LockAbortError') return true;
36
+ }
37
+ const message = error instanceof Error ? error.message : String(error);
38
+ return /lock (is locked|acquire timed out|acquire timeout)/i.test(message);
39
+ }
40
+
41
+ /**
42
+ * Schedule a repeating interval where only one node acquires the lock.
43
+ * Returns NodeJS.Timeout so callers can keep the same variable type and
44
+ * clearInterval() on cleanup.
45
+ */
46
+ export function runWithDistributedLock(
47
+ app: AppLike,
48
+ key: string,
49
+ task: () => Promise<void>,
50
+ intervalMs: number,
51
+ ttlMs = Math.max(1_000, Math.min(intervalMs - 1, 300_000)),
52
+ ): NodeJS.Timeout {
53
+ const timer = setInterval(async () => {
54
+ const lockKey = `ha:${key}`;
55
+ const lockManager = app.lockManager;
56
+
57
+ try {
58
+ if (!lockManager?.tryAcquire) {
59
+ await task();
60
+ return;
61
+ }
62
+ const lock = await lockManager.tryAcquire(lockKey, 0);
63
+ await lock.runExclusive(async () => {
64
+ await task();
65
+ }, ttlMs);
66
+ } catch (error: unknown) {
67
+ if (isLockAcquireError(error)) {
68
+ // Another node holds the lock — skip this tick
69
+ const message = error instanceof Error ? error.message : String(error);
70
+ app.logger?.debug?.(`[ha-lock] skipped "${key}" (${message})`);
71
+ return;
72
+ }
73
+ const message = error instanceof Error ? error.message : String(error);
74
+ app.logger?.warn?.(`[ha-lock] error on "${key}": ${message}`);
75
+ }
76
+ }, intervalMs);
77
+
78
+ timer.unref?.();
79
+ return timer;
80
+ }
@@ -1,20 +1,84 @@
1
- import type { Plugin } from '@nocobase/server';
2
- import { asObject } from './ctx-utils';
3
-
4
- export function worktreeCapability(plugin: Plugin) {
5
- const pluginManager = plugin.app.pm as unknown as { get?: (name: string) => unknown };
6
- const gitManager = pluginManager.get?.('plugin-git-manager') as { loopWorktreeService?: unknown } | undefined;
7
- return {
8
- available: Boolean(gitManager?.loopWorktreeService),
9
- provider: gitManager?.loopWorktreeService ? 'plugin-git-manager' : undefined,
10
- };
11
- }
12
-
13
- export function employeeHarnessResolver(plugin: Plugin) {
14
- return async (username: string) => {
15
- const employee = await plugin.db.getRepository('aiEmployees')?.findOne({ filter: { username } });
16
- if (!employee) return undefined;
17
- const skillSettings = asObject(employee.get?.('skillSettings'));
18
- return skillSettings.orchestratorHarness;
19
- };
20
- }
1
+ import type { Plugin } from '@nocobase/server';
2
+ import type { Database } from '@nocobase/database';
3
+ import { asObject } from './ctx-utils';
4
+
5
+ export function worktreeCapability(plugin: Plugin) {
6
+ const pluginManager = plugin.app.pm as unknown as { get?: (name: string) => unknown };
7
+ const gitManager = pluginManager.get?.('plugin-git-manager') as { loopWorktreeService?: unknown } | undefined;
8
+ return {
9
+ available: Boolean(gitManager?.loopWorktreeService),
10
+ provider: gitManager?.loopWorktreeService ? 'plugin-git-manager' : undefined,
11
+ };
12
+ }
13
+
14
+ /**
15
+ * Build an employee harness resolution chain with layered fallback.
16
+ *
17
+ * Resolution order (first non-null wins):
18
+ * 1. Employee-specific `skillSettings.orchestratorHarness` — direct override set by the
19
+ * employee's own configuration. This is the highest-priority layer.
20
+ * 2. Role-based harness mapping — when the employee is bound to a loop pattern role via
21
+ * `orchestratorConfig`, its `harnessTag` settings can supply a fallback. This covers
22
+ * cases where the employee has no personal override but the pattern has expectations.
23
+ * 3. `undefined` — signals to `LoopPatternService.compileRole()` that no employee layer is
24
+ * needed; the profile + pattern layers already govern the employee.
25
+ *
26
+ * Each layer's settings are returned individually so `compileHarness()` can stack them
27
+ * independently with most-restrictive-wins semantics.
28
+ */
29
+ export function employeeHarnessResolver(plugin: Plugin) {
30
+ return async (username: string) => {
31
+ const db: Database = plugin.db;
32
+
33
+ // ── Layer 1: Employee-specific orchestratorHarness override ──
34
+ try {
35
+ const employee = await db.getRepository('aiEmployees')?.findOne({ filter: { username } });
36
+ if (employee) {
37
+ const skillSettings = asObject(employee.get?.('skillSettings'));
38
+ const directOverride = skillSettings.orchestratorHarness;
39
+ if (directOverride && typeof directOverride === 'object' && Object.keys(directOverride).length > 0) {
40
+ return directOverride;
41
+ }
42
+ }
43
+ } catch {
44
+ // Employee lookup failed; continue to fallback layers.
45
+ }
46
+
47
+ // ── Layer 2: Role-based harness from orchestratorConfig ──
48
+ // orchestratorConfig stores leader<->sub-agent bindings with harnessTag overrides.
49
+ // When this username appears as a sub-agent and the config specifies a harnessTag,
50
+ // resolve the tag's published harness settings as a fallback.
51
+ try {
52
+ const config = await db.getRepository('orchestratorConfig')?.findOne({
53
+ filter: { subAgentUsername: username, enabled: true },
54
+ });
55
+ if (config) {
56
+ const harnessTag = config.get?.('harnessTag') || (config as Record<string, unknown>).harnessTag;
57
+ if (typeof harnessTag === 'string' && harnessTag.trim()) {
58
+ const profile = await db.getRepository('agentHarnessProfiles')?.findOne({
59
+ filter: { tag: harnessTag.trim(), enabled: true },
60
+ });
61
+ if (profile) {
62
+ const currentVersionId = Number(profile.get?.('currentVersionId') || 0);
63
+ if (currentVersionId > 0) {
64
+ const version = await db.getRepository('agentHarnessProfileVersions')?.findOne({
65
+ filter: { id: currentVersionId, status: 'published' },
66
+ });
67
+ if (version) {
68
+ const settings = version.get?.('settings');
69
+ if (settings && typeof settings === 'object' && Object.keys(settings).length > 0) {
70
+ return settings;
71
+ }
72
+ }
73
+ }
74
+ }
75
+ }
76
+ }
77
+ } catch {
78
+ // Config lookup failed; fall through to no override.
79
+ }
80
+
81
+ // ── Layer 3: No employee-level override — profile + pattern layers suffice ──
82
+ return undefined;
83
+ };
84
+ }
@@ -0,0 +1,19 @@
1
+ import type { Model } from '@nocobase/database';
2
+
3
+ /**
4
+ * Read a value from a NocoBase model or plain record.
5
+ *
6
+ * NocoBase models use a getter (record.get('fieldName')) to access attributes.
7
+ * Plain records use direct property access. This helper transparently handles both.
8
+ */
9
+ export function read(record: Model | Record<string, unknown> | unknown, key: string): unknown {
10
+ const model = record as { get?: (name: string) => unknown } | null;
11
+ return typeof model?.get === 'function' ? model.get(key) : (model as Record<string, unknown> | null)?.[key];
12
+ }
13
+
14
+ /**
15
+ * Coerce a value to a plain object. Returns {} for non-object inputs.
16
+ */
17
+ export function asRecord(value: unknown): Record<string, unknown> {
18
+ return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
19
+ }