plugin-agent-orchestrator 1.1.4 → 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.
- package/README.md +204 -114
- package/dist/client/index.js +1 -1
- package/dist/client-v2/117.7565cff57c39318a.js +10 -0
- package/dist/client-v2/214.106635011859dede.js +10 -0
- package/dist/client-v2/418.41ed200e7d6da332.js +10 -0
- package/dist/client-v2/53.349fff8db80b19d4.js +10 -0
- package/dist/client-v2/981.e4a1f41cfc94a856.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +7 -8
- package/dist/locale/en-US.json +226 -170
- package/dist/locale/vi-VN.json +226 -170
- package/dist/locale/zh-CN.json +226 -170
- package/dist/server/collections/agent-execution-spans.js +8 -0
- package/dist/server/collections/agent-loop-action-approvals.js +1 -0
- package/dist/server/collections/agent-loop-runs.js +7 -1
- package/dist/server/collections/agent-loop-steps.js +7 -0
- package/dist/server/collections/skill-executions.js +8 -0
- package/dist/server/plugin.js +3 -1
- package/dist/server/resources/agent-harness-profile.js +117 -0
- package/dist/server/resources/agent-knowledge-insights.js +1 -3
- package/dist/server/resources/agent-loop-approval.js +25 -0
- package/dist/server/resources/agent-loop.js +3 -1
- package/dist/server/services/AgentMemoryContextService.js +34 -0
- package/dist/server/services/HarnessCompiler.js +35 -2
- package/dist/server/services/HarnessProfileService.js +74 -9
- package/dist/server/services/HarnessSchema.js +30 -3
- package/dist/server/services/LoopPatternSchema.js +16 -1
- package/dist/server/services/LoopRunRepository.js +80 -0
- package/dist/server/services/LoopRunStateMachine.js +92 -3
- package/dist/server/services/LoopTriggerService.js +37 -0
- package/dist/server/services/LoopWorkerService.js +261 -23
- package/dist/server/services/NativeSubAgentObserver.js +165 -23
- package/dist/server/services/PluginAiRuntimeAdapter.js +16 -5
- package/dist/server/services/ToolLoopDetectionService.js +210 -0
- package/dist/server/services/VerificationService.js +66 -0
- package/dist/server/services/harness-runtime-policy.js +139 -0
- package/dist/server/skill-hub/plugin.js +36 -3
- package/dist/server/skill-hub/tasks/SkillExecutionTask.js +29 -0
- package/package.json +53 -53
- package/src/client/ApprovalsTab.tsx +2 -0
- package/src/client/OrchestratorSettings.tsx +11 -0
- package/src/client/skill-hub/components/GitSkillImport.tsx +3 -3
- package/src/client/skill-hub/components/SkillMetrics.tsx +15 -10
- package/src/client/skill-hub/index.tsx +0 -52
- package/src/client-v2/components/AgentRunsTab.tsx +576 -576
- package/src/client-v2/components/ApprovalsTab.tsx +337 -0
- package/src/client-v2/components/HarnessProfilesTab.tsx +385 -105
- package/src/client-v2/components/MemoryInspectorTab.tsx +141 -141
- package/src/client-v2/components/TracingTab.tsx +515 -515
- package/src/client-v2/hooks/useApiRequest.ts +141 -141
- package/src/client-v2/pages/ApprovalsPage.tsx +10 -0
- package/src/client-v2/plugin.tsx +6 -6
- package/src/client-v2/skill-hub/components/SkillMetrics.tsx +5 -2
- package/src/locale/en-US.json +226 -174
- package/src/locale/vi-VN.json +226 -174
- package/src/locale/zh-CN.json +226 -174
- package/src/server/__tests__/circuit-breaker.test.ts +248 -248
- package/src/server/__tests__/harness-compiler.test.ts +83 -0
- package/src/server/__tests__/harness-profile-service.test.ts +51 -0
- package/src/server/__tests__/harness-runtime-policy.test.ts +293 -0
- package/src/server/__tests__/harness-tool-plan.test.ts +24 -0
- package/src/server/__tests__/loop-approval-decision.test.ts +512 -0
- package/src/server/__tests__/loop-approval-expiry.test.ts +258 -0
- package/src/server/__tests__/loop-budget-service.test.ts +255 -255
- package/src/server/__tests__/loop-control-plane-backfill.test.ts +266 -266
- package/src/server/__tests__/loop-pattern-schema.test.ts +18 -1
- package/src/server/__tests__/loop-pattern-service.test.ts +148 -148
- package/src/server/__tests__/loop-run-state-machine.test.ts +11 -0
- package/src/server/__tests__/loop-trigger-service.test.ts +148 -0
- package/src/server/__tests__/loop-worker-claim.test.ts +341 -341
- package/src/server/__tests__/loop-worker-execution.test.ts +82 -1
- package/src/server/__tests__/native-sub-agent-observer.test.ts +288 -4
- package/src/server/__tests__/path-lock-service.test.ts +254 -254
- package/src/server/__tests__/skill-execution-task-policy.test.ts +140 -0
- package/src/server/__tests__/smoke.test.ts +162 -162
- package/src/server/__tests__/tool-loop-detection.test.ts +275 -0
- package/src/server/collections/agent-execution-spans.ts +8 -0
- package/src/server/collections/agent-harness-profile-versions.ts +26 -26
- package/src/server/collections/agent-harness-profiles.ts +71 -71
- package/src/server/collections/agent-loop-action-approvals.ts +1 -0
- package/src/server/collections/agent-loop-artifacts.ts +25 -25
- package/src/server/collections/agent-loop-circuit-states.ts +23 -23
- package/src/server/collections/agent-loop-control-settings.ts +21 -21
- package/src/server/collections/agent-loop-events.ts +98 -98
- package/src/server/collections/agent-loop-path-locks.ts +27 -27
- package/src/server/collections/agent-loop-patterns.ts +42 -42
- package/src/server/collections/agent-loop-runs.ts +7 -1
- package/src/server/collections/agent-loop-steps.ts +7 -0
- package/src/server/collections/agent-loop-usage-buckets.ts +22 -22
- package/src/server/collections/agent-loop-worktrees.ts +25 -25
- package/src/server/collections/skill-executions.ts +8 -0
- package/src/server/migrations/20260427000002-change-packages-to-text.ts +52 -52
- package/src/server/migrations/20260429000000-add-llm-fields.ts +47 -47
- package/src/server/migrations/20260429000001-fix-inputargs-json-to-text.ts +45 -45
- package/src/server/migrations/20260503000000-add-orchestrator-trace-fields.ts +33 -33
- package/src/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.ts +31 -31
- package/src/server/migrations/20260803010000-backfill-loop-control-plane-data.ts +293 -293
- package/src/server/plugin.ts +3 -1
- package/src/server/resources/agent-harness-profile.ts +94 -0
- package/src/server/resources/agent-knowledge-insights.ts +5 -3
- package/src/server/resources/agent-loop-approval.ts +32 -0
- package/src/server/resources/agent-loop-artifact.ts +56 -56
- package/src/server/resources/agent-loop-events-stream.ts +105 -105
- package/src/server/resources/agent-loop.ts +3 -1
- package/src/server/resources/resource-helpers.ts +33 -33
- package/src/server/resources/tracing.ts +266 -266
- package/src/server/services/AgentMemoryContextService.ts +44 -0
- package/src/server/services/DurableCircuitBreakerService.ts +239 -239
- package/src/server/services/ExecutionSpanService.ts +2 -0
- package/src/server/services/HarnessCompiler.ts +37 -0
- package/src/server/services/HarnessProfileService.ts +92 -10
- package/src/server/services/HarnessSchema.ts +31 -0
- package/src/server/services/LoopBudgetService.ts +275 -275
- package/src/server/services/LoopControlService.ts +124 -124
- package/src/server/services/LoopPatternSchema.ts +19 -1
- package/src/server/services/LoopPatternService.ts +143 -143
- package/src/server/services/LoopRunRepository.ts +99 -0
- package/src/server/services/LoopRunStateMachine.ts +100 -3
- package/src/server/services/LoopSchedulerService.ts +199 -199
- package/src/server/services/LoopTriggerService.ts +42 -0
- package/src/server/services/LoopWorkerService.ts +327 -27
- package/src/server/services/NativeSubAgentObserver.ts +186 -21
- package/src/server/services/PathLockService.ts +321 -321
- package/src/server/services/PluginAiRuntimeAdapter.ts +24 -7
- package/src/server/services/ToolLoopDetectionService.ts +213 -0
- package/src/server/services/VerificationSchema.ts +31 -31
- package/src/server/services/VerificationService.ts +81 -2
- package/src/server/services/harness-runtime-policy.ts +141 -0
- package/src/server/skill-hub/plugin.ts +41 -3
- package/src/server/skill-hub/services/RegistrySkillSnapshotService.ts +235 -235
- package/src/server/skill-hub/services/__tests__/RegistrySkillSnapshotService.test.ts +61 -61
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +43 -2
- package/src/server/utils/ensure-indexes.ts +96 -96
- package/src/server/utils/logging.ts +62 -62
- package/src/server/utils/loop-pattern-context.ts +20 -20
- package/dist/client-v2/214.b063f33ba044a808.js +0 -10
- package/dist/client-v2/373.f5d5292e53c4e832.js +0 -10
- package/dist/client-v2/418.58fa350f5496efd3.js +0 -10
- package/dist/client-v2/53.9a10455323390f14.js +0 -10
- package/dist/client-v2/677.a991ce0250ff5c77.js +0 -10
- package/dist/client-v2/925.e5925bf21ed68e7a.js +0 -10
- package/src/client/AIEmployeeSelect.tsx +0 -47
- package/src/client/AIEmployeesContext.tsx +0 -102
- package/src/client/RulesTab.tsx +0 -2
- package/src/client/TracingTab.tsx +0 -436
- package/src/client/skill-hub/components/ExecutionProgress.tsx +0 -51
- package/src/client-v2/components/RulesTab.tsx +0 -2
- package/src/client-v2/pages/AgentRunsPage.tsx +0 -13
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Database, Model } from '@nocobase/database';
|
|
2
|
+
import { getRunEventBus } from './RunEventBus';
|
|
2
3
|
|
|
3
4
|
export type LoopRunRecord = Record<string, unknown>;
|
|
4
5
|
|
|
@@ -184,6 +185,104 @@ export class LoopRunRepository {
|
|
|
184
185
|
return approval;
|
|
185
186
|
}
|
|
186
187
|
|
|
188
|
+
// Records a human decision on one approval and requeues the run once every pending approval of
|
|
189
|
+
// the run has been decided. Fail-closed throughout: an expired window, a decided row, or a run
|
|
190
|
+
// that moved on all reject the decision instead of guessing what the human meant.
|
|
191
|
+
async decideApproval(input: {
|
|
192
|
+
approvalId: string | number;
|
|
193
|
+
userId: number | undefined;
|
|
194
|
+
isAdmin: boolean;
|
|
195
|
+
decision: 'approved' | 'rejected';
|
|
196
|
+
note?: string;
|
|
197
|
+
editedInput?: unknown;
|
|
198
|
+
}) {
|
|
199
|
+
const decidedById = requireUserId(input.userId);
|
|
200
|
+
const id = positiveId(input.approvalId, 'Agent loop approval');
|
|
201
|
+
// Fast access check before taking row locks; the transaction re-reads the rows anyway.
|
|
202
|
+
await this.requireAccessibleApproval(id, input.userId, input.isAdmin);
|
|
203
|
+
|
|
204
|
+
const committed = await this.database.sequelize.transaction(async (transaction) => {
|
|
205
|
+
const approvals = this.database.getRepository('agentLoopActionApprovals');
|
|
206
|
+
const runs = this.database.getRepository('agentLoopRuns');
|
|
207
|
+
const record = await approvals.findOne({ filterByTk: id, transaction, lock: transaction.LOCK.UPDATE });
|
|
208
|
+
if (!record) throw new LoopRunAccessError(404, `Agent loop approval ${id} was not found.`);
|
|
209
|
+
const approval = plain(record);
|
|
210
|
+
|
|
211
|
+
if (approval.status !== 'pending') {
|
|
212
|
+
throw new LoopRunAccessError(409, `This approval has already been ${String(approval.status)}.`);
|
|
213
|
+
}
|
|
214
|
+
const expiresAt = approval.expiresAt ? new Date(String(approval.expiresAt)) : null;
|
|
215
|
+
if (expiresAt && !Number.isNaN(expiresAt.getTime()) && expiresAt.getTime() <= Date.now()) {
|
|
216
|
+
throw new LoopRunAccessError(409, 'The approval window has expired. Retry the run to request approval again.');
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const runId = positiveId(approval.runId, 'Agent loop run');
|
|
220
|
+
const runRecord = await runs.findOne({ filterByTk: runId, transaction, lock: transaction.LOCK.UPDATE });
|
|
221
|
+
if (!runRecord) throw new LoopRunAccessError(404, `Agent loop run ${runId} was not found.`);
|
|
222
|
+
const run = plain(runRecord);
|
|
223
|
+
if (run.runtimeVersion !== 'control-plane-v2') {
|
|
224
|
+
throw new LoopRunAccessError(409, 'Historical plan-era runs are read-only.');
|
|
225
|
+
}
|
|
226
|
+
// A paused run keeps its decisions; the human resume requeues it later. Any other status
|
|
227
|
+
// means the run moved on and the decision can no longer be applied.
|
|
228
|
+
if (run.status !== 'waiting_approval' && run.status !== 'paused') {
|
|
229
|
+
throw new LoopRunAccessError(409, 'The run is no longer waiting for approval.');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const now = new Date();
|
|
233
|
+
await approvals.update({
|
|
234
|
+
filterByTk: id,
|
|
235
|
+
values: {
|
|
236
|
+
status: input.decision,
|
|
237
|
+
decidedById,
|
|
238
|
+
decidedAt: now,
|
|
239
|
+
decisionNote: input.note || null,
|
|
240
|
+
...(input.decision === 'approved' && input.editedInput !== undefined
|
|
241
|
+
? { editedInput: input.editedInput }
|
|
242
|
+
: {}),
|
|
243
|
+
},
|
|
244
|
+
transaction,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const remaining = await approvals.find({ filter: { runId, status: 'pending' }, transaction });
|
|
248
|
+
let requeued = false;
|
|
249
|
+
let event: LoopRunRecord | null = null;
|
|
250
|
+
if (!remaining.length && run.status === 'waiting_approval') {
|
|
251
|
+
await runs.update({
|
|
252
|
+
filterByTk: runId,
|
|
253
|
+
values: { status: 'queued', approvalStatus: 'decided' },
|
|
254
|
+
transaction,
|
|
255
|
+
});
|
|
256
|
+
const created = await this.database.getRepository('agentLoopEvents').create({
|
|
257
|
+
values: {
|
|
258
|
+
runId,
|
|
259
|
+
type: 'run_approval_decided',
|
|
260
|
+
title: 'Approvals decided',
|
|
261
|
+
content: 'All pending action approvals were decided. Run requeued to resume.',
|
|
262
|
+
status: 'queued',
|
|
263
|
+
payload: { decision: input.decision },
|
|
264
|
+
actorType: 'human',
|
|
265
|
+
actorIdentity: String(decidedById),
|
|
266
|
+
correlationKey: `approval-decided:${runId}`,
|
|
267
|
+
createdAt: now,
|
|
268
|
+
},
|
|
269
|
+
transaction,
|
|
270
|
+
});
|
|
271
|
+
event = typeof created.toJSON === 'function' ? created.toJSON() : created;
|
|
272
|
+
requeued = true;
|
|
273
|
+
}
|
|
274
|
+
return { approval, runId, requeued, event };
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
if (committed.requeued && committed.event) getRunEventBus().emit(committed.runId, committed.event);
|
|
278
|
+
return {
|
|
279
|
+
...committed.approval,
|
|
280
|
+
status: input.decision,
|
|
281
|
+
decidedById,
|
|
282
|
+
requeued: committed.requeued,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
187
286
|
async listArtifacts(input: {
|
|
188
287
|
userId: number | undefined;
|
|
189
288
|
isAdmin: boolean;
|
|
@@ -24,10 +24,28 @@ export type LoopRunStatus = (typeof loopRunStatuses)[number];
|
|
|
24
24
|
|
|
25
25
|
const transitions: Record<LoopRunStatus, ReadonlySet<LoopRunStatus>> = {
|
|
26
26
|
queued: new Set(['preparing', 'paused', 'blocked', 'canceled']),
|
|
27
|
-
preparing: new Set([
|
|
27
|
+
preparing: new Set([
|
|
28
|
+
'queued',
|
|
29
|
+
'waiting_lock',
|
|
30
|
+
'waiting_approval',
|
|
31
|
+
'running',
|
|
32
|
+
'paused',
|
|
33
|
+
'blocked',
|
|
34
|
+
'failed',
|
|
35
|
+
'canceled',
|
|
36
|
+
]),
|
|
28
37
|
waiting_lock: new Set(['preparing', 'paused', 'blocked', 'canceled']),
|
|
29
|
-
running: new Set([
|
|
30
|
-
|
|
38
|
+
running: new Set([
|
|
39
|
+
'queued',
|
|
40
|
+
'waiting_approval',
|
|
41
|
+
'verifying',
|
|
42
|
+
'waiting_human',
|
|
43
|
+
'paused',
|
|
44
|
+
'blocked',
|
|
45
|
+
'failed',
|
|
46
|
+
'canceled',
|
|
47
|
+
]),
|
|
48
|
+
waiting_approval: new Set(['queued', 'running', 'waiting_human', 'paused', 'blocked', 'failed', 'canceled']),
|
|
31
49
|
verifying: new Set(['running', 'waiting_human', 'paused', 'blocked', 'succeeded', 'failed', 'canceled']),
|
|
32
50
|
waiting_human: new Set(['running', 'succeeded', 'failed', 'canceled']),
|
|
33
51
|
paused: new Set(['queued', 'preparing', 'running', 'waiting_approval', 'verifying', 'blocked', 'canceled']),
|
|
@@ -300,6 +318,85 @@ export class LoopRunStateMachine {
|
|
|
300
318
|
};
|
|
301
319
|
}
|
|
302
320
|
|
|
321
|
+
// Approval windows fail closed: an unanswered request blocks the run instead of silently
|
|
322
|
+
// turning into an approval. Runs only move while they are still `waiting_approval`, so a
|
|
323
|
+
// decision recorded through another path always wins the race against this sweep.
|
|
324
|
+
async expireOverdueApprovals(now: Date): Promise<number[]> {
|
|
325
|
+
const blockedRunIds: number[] = [];
|
|
326
|
+
const committedEvents: Array<{ runId: number; event: unknown }> = [];
|
|
327
|
+
|
|
328
|
+
await this.database.sequelize.transaction(async (transaction) => {
|
|
329
|
+
const approvals = this.database.getRepository('agentLoopActionApprovals');
|
|
330
|
+
const runs = this.database.getRepository('agentLoopRuns');
|
|
331
|
+
const pending = await approvals.find({ filter: { status: 'pending' }, transaction });
|
|
332
|
+
|
|
333
|
+
const overdueByRun = new Map<number, Array<Model | Record<string, unknown>>>();
|
|
334
|
+
for (const approval of pending) {
|
|
335
|
+
const expiresAt = asDate(read(approval, 'expiresAt'));
|
|
336
|
+
if (!expiresAt || expiresAt.getTime() > now.getTime()) continue;
|
|
337
|
+
const runId = Number(read(approval, 'runId'));
|
|
338
|
+
if (!Number.isSafeInteger(runId) || runId <= 0) continue;
|
|
339
|
+
const bucket = overdueByRun.get(runId) || [];
|
|
340
|
+
bucket.push(approval);
|
|
341
|
+
overdueByRun.set(runId, bucket);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
for (const [runId, overdue] of overdueByRun) {
|
|
345
|
+
const run = await runs.findOne({ filterByTk: runId, transaction, lock: transaction.LOCK.UPDATE });
|
|
346
|
+
if (!run) continue;
|
|
347
|
+
if (String(read(run, 'runtimeVersion')) !== 'control-plane-v2') continue;
|
|
348
|
+
if (asStatus(read(run, 'status')) !== 'waiting_approval') continue;
|
|
349
|
+
|
|
350
|
+
assertRunTransition({
|
|
351
|
+
from: 'waiting_approval',
|
|
352
|
+
to: 'blocked',
|
|
353
|
+
runtimeVersion: String(read(run, 'runtimeVersion') || ''),
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
for (const approval of overdue) {
|
|
357
|
+
await approvals.update({
|
|
358
|
+
filterByTk: Number(read(approval, 'id')),
|
|
359
|
+
values: { status: 'expired', decidedAt: now },
|
|
360
|
+
transaction,
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const reason = `${overdue.length} action approval(s) expired before a decision was made.`;
|
|
365
|
+
await runs.update({
|
|
366
|
+
filterByTk: runId,
|
|
367
|
+
values: {
|
|
368
|
+
status: 'blocked',
|
|
369
|
+
blockedReason: reason,
|
|
370
|
+
approvalStatus: 'expired',
|
|
371
|
+
lockedBy: null,
|
|
372
|
+
lockedUntil: null,
|
|
373
|
+
},
|
|
374
|
+
transaction,
|
|
375
|
+
});
|
|
376
|
+
const event = await this.createEvent(
|
|
377
|
+
{
|
|
378
|
+
runId,
|
|
379
|
+
type: 'run_approval_expired',
|
|
380
|
+
title: 'Approval window expired',
|
|
381
|
+
content: reason,
|
|
382
|
+
status: 'blocked',
|
|
383
|
+
payload: { reason, expiredApprovals: overdue.length },
|
|
384
|
+
actorType: 'system',
|
|
385
|
+
actorIdentity: 'approval-reaper',
|
|
386
|
+
correlationKey: `approval-expired:${runId}`,
|
|
387
|
+
createdAt: now,
|
|
388
|
+
},
|
|
389
|
+
transaction,
|
|
390
|
+
);
|
|
391
|
+
blockedRunIds.push(runId);
|
|
392
|
+
committedEvents.push({ runId, event });
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
for (const blocked of committedEvents) getRunEventBus().emit(blocked.runId, blocked.event);
|
|
397
|
+
return blockedRunIds;
|
|
398
|
+
}
|
|
399
|
+
|
|
303
400
|
async renewLease(runId: number, leaseToken: string, leaseMs: number) {
|
|
304
401
|
if (!leaseToken.trim()) throw new Error('Worker lease token is required.');
|
|
305
402
|
if (!Number.isSafeInteger(leaseMs) || leaseMs < 5_000) throw new Error('Worker lease must be at least 5000ms.');
|
|
@@ -1,199 +1,199 @@
|
|
|
1
|
-
import type { Database, Model, Transaction } from '@nocobase/database';
|
|
2
|
-
import type { Application, Plugin } from '@nocobase/server';
|
|
3
|
-
import type { LoopPattern } from './LoopPatternSchema';
|
|
4
|
-
import { LoopPatternService } from './LoopPatternService';
|
|
5
|
-
import { LoopTriggerService } from './LoopTriggerService';
|
|
6
|
-
|
|
7
|
-
type ManagedCronJob = ReturnType<Application['cronJobManager']['addJob']>;
|
|
8
|
-
|
|
9
|
-
type ScheduledPattern = {
|
|
10
|
-
job: ManagedCronJob;
|
|
11
|
-
signature: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
type TransactionOptions = {
|
|
15
|
-
transaction?: Transaction;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type LoopSchedulerSyncMessage = {
|
|
19
|
-
type: 'loop-pattern-schedule-changed';
|
|
20
|
-
patternId: number;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
function read(record: Model | Record<string, unknown>, key: string) {
|
|
24
|
-
const model = record as Model & { get?: (name: string) => unknown };
|
|
25
|
-
return typeof model.get === 'function' ? model.get(key) : (record as Record<string, unknown>)[key];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function positiveInteger(value: unknown) {
|
|
29
|
-
const parsed = Number(value);
|
|
30
|
-
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function isLockAcquireError(error: unknown) {
|
|
34
|
-
return error instanceof Error && error.constructor.name === 'LockAcquireError';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function tickDate(pattern: LoopPattern, now: Date) {
|
|
38
|
-
const fields = pattern.cronExpression?.split(/\s+/).filter(Boolean).length || 5;
|
|
39
|
-
const interval = fields === 6 ? 1_000 : 60_000;
|
|
40
|
-
return new Date(Math.floor(now.getTime() / interval) * interval);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export class LoopSchedulerService {
|
|
44
|
-
private readonly scheduled = new Map<number, ScheduledPattern>();
|
|
45
|
-
private started = false;
|
|
46
|
-
private installed = false;
|
|
47
|
-
|
|
48
|
-
constructor(
|
|
49
|
-
private readonly plugin: Plugin,
|
|
50
|
-
private readonly patterns: LoopPatternService,
|
|
51
|
-
private readonly triggers: LoopTriggerService,
|
|
52
|
-
) {}
|
|
53
|
-
|
|
54
|
-
install() {
|
|
55
|
-
if (this.installed) return;
|
|
56
|
-
this.installed = true;
|
|
57
|
-
this.plugin.db.on('agentLoopPatterns.afterCreate', this.onPatternChanged);
|
|
58
|
-
this.plugin.db.on('agentLoopPatterns.afterUpdate', this.onPatternChanged);
|
|
59
|
-
this.plugin.db.on('agentLoopPatterns.afterDestroy', this.onPatternChanged);
|
|
60
|
-
this.plugin.app.on('afterStart', this.start);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async dispose() {
|
|
64
|
-
if (!this.installed) return;
|
|
65
|
-
this.installed = false;
|
|
66
|
-
this.plugin.db.off('agentLoopPatterns.afterCreate', this.onPatternChanged);
|
|
67
|
-
this.plugin.db.off('agentLoopPatterns.afterUpdate', this.onPatternChanged);
|
|
68
|
-
this.plugin.db.off('agentLoopPatterns.afterDestroy', this.onPatternChanged);
|
|
69
|
-
this.plugin.app.off('afterStart', this.start);
|
|
70
|
-
this.stop();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
readonly start = async () => {
|
|
74
|
-
if (this.started) return;
|
|
75
|
-
this.started = true;
|
|
76
|
-
await this.resyncAll();
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
stop() {
|
|
80
|
-
this.started = false;
|
|
81
|
-
for (const { job } of this.scheduled.values()) this.plugin.app.cronJobManager.removeJob(job);
|
|
82
|
-
this.scheduled.clear();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async handleSyncMessage(message: unknown) {
|
|
86
|
-
if (!message || typeof message !== 'object') return;
|
|
87
|
-
const value = message as Partial<LoopSchedulerSyncMessage>;
|
|
88
|
-
if (value.type !== 'loop-pattern-schedule-changed') return;
|
|
89
|
-
const patternId = positiveInteger(value.patternId);
|
|
90
|
-
if (!patternId) return;
|
|
91
|
-
await this.resyncPattern(patternId);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async resyncAll() {
|
|
95
|
-
if (!this.started) return;
|
|
96
|
-
const records = await this.plugin.db.getRepository('agentLoopPatterns').find({ fields: ['id'] });
|
|
97
|
-
const patternIds = new Set<number>();
|
|
98
|
-
for (const record of records) {
|
|
99
|
-
const patternId = positiveInteger(read(record, 'id'));
|
|
100
|
-
if (!patternId) continue;
|
|
101
|
-
patternIds.add(patternId);
|
|
102
|
-
await this.resyncPattern(patternId);
|
|
103
|
-
}
|
|
104
|
-
for (const patternId of this.scheduled.keys()) {
|
|
105
|
-
if (!patternIds.has(patternId)) this.remove(patternId);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async resyncPattern(patternId: number) {
|
|
110
|
-
if (!this.started) return;
|
|
111
|
-
const record = await this.plugin.db.getRepository('agentLoopPatterns').findOne({ filterByTk: patternId });
|
|
112
|
-
if (!record || read(record, 'enabled') !== true || read(record, 'triggerType') !== 'cron') {
|
|
113
|
-
this.remove(patternId);
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
try {
|
|
118
|
-
const { pattern } = await this.patterns.get(patternId);
|
|
119
|
-
const signature = JSON.stringify([pattern.cronExpression, pattern.timezone]);
|
|
120
|
-
if (this.scheduled.get(patternId)?.signature === signature) return;
|
|
121
|
-
this.remove(patternId);
|
|
122
|
-
const job = this.plugin.app.cronJobManager.addJob({
|
|
123
|
-
cronTime: pattern.cronExpression as string,
|
|
124
|
-
timeZone: pattern.timezone as string,
|
|
125
|
-
onTick: () => this.onTick(patternId, pattern).catch((error) => this.logTickError(patternId, error)),
|
|
126
|
-
});
|
|
127
|
-
this.scheduled.set(patternId, { job, signature });
|
|
128
|
-
if (this.plugin.app.cronJobManager.started) job.start();
|
|
129
|
-
this.plugin.app.logger.info(`[AgentOrchestrator] Scheduled loop pattern ${pattern.key}.`, {
|
|
130
|
-
patternId,
|
|
131
|
-
cronExpression: pattern.cronExpression,
|
|
132
|
-
timezone: pattern.timezone,
|
|
133
|
-
});
|
|
134
|
-
} catch (error) {
|
|
135
|
-
this.remove(patternId);
|
|
136
|
-
this.plugin.app.logger.error(`[AgentOrchestrator] Loop pattern ${patternId} could not be scheduled.`, { error });
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private readonly onPatternChanged = async (record: Model, options: TransactionOptions = {}) => {
|
|
141
|
-
const patternId = positiveInteger(read(record, 'id'));
|
|
142
|
-
if (!patternId) return;
|
|
143
|
-
const apply = async () => {
|
|
144
|
-
await this.resyncPattern(patternId);
|
|
145
|
-
await this.plugin.sendSyncMessage({ type: 'loop-pattern-schedule-changed', patternId });
|
|
146
|
-
};
|
|
147
|
-
if (options.transaction) {
|
|
148
|
-
// afterCommit does not await its callback, so a rejection here would be
|
|
149
|
-
// an unhandled promise. Catch and log so a failed resync/broadcast does
|
|
150
|
-
// not silently drop the schedule change.
|
|
151
|
-
options.transaction.afterCommit(() =>
|
|
152
|
-
apply().catch((error) =>
|
|
153
|
-
this.plugin.app.logger.error(
|
|
154
|
-
`[AgentOrchestrator] Failed to apply schedule change for loop pattern ${patternId} after commit.`,
|
|
155
|
-
{ error },
|
|
156
|
-
),
|
|
157
|
-
),
|
|
158
|
-
);
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
await apply();
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
private async onTick(patternId: number, pattern: LoopPattern) {
|
|
165
|
-
const scheduledAt = tickDate(pattern, new Date());
|
|
166
|
-
const triggerKey = `cron:${pattern.key}:${scheduledAt.toISOString()}`;
|
|
167
|
-
let lock;
|
|
168
|
-
try {
|
|
169
|
-
lock = await this.plugin.app.lockManager.tryAcquire(
|
|
170
|
-
`agent-loop:schedule:${this.plugin.app.name}:${patternId}:${triggerKey}`,
|
|
171
|
-
);
|
|
172
|
-
} catch (error) {
|
|
173
|
-
if (isLockAcquireError(error)) return;
|
|
174
|
-
throw error;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
await lock.runExclusive(async () => {
|
|
178
|
-
await this.triggers.enqueue({
|
|
179
|
-
patternId,
|
|
180
|
-
triggerType: 'cron',
|
|
181
|
-
triggerKey,
|
|
182
|
-
triggerPayload: { scheduledAt: scheduledAt.toISOString(), timezone: pattern.timezone },
|
|
183
|
-
});
|
|
184
|
-
}, 60_000);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
private remove(patternId: number) {
|
|
188
|
-
const existing = this.scheduled.get(patternId);
|
|
189
|
-
if (!existing) return;
|
|
190
|
-
this.plugin.app.cronJobManager.removeJob(existing.job);
|
|
191
|
-
this.scheduled.delete(patternId);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
private logTickError(patternId: number, error: unknown) {
|
|
195
|
-
this.plugin.app.logger.error(`[AgentOrchestrator] Scheduled loop pattern ${patternId} failed to enqueue.`, {
|
|
196
|
-
error,
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
}
|
|
1
|
+
import type { Database, Model, Transaction } from '@nocobase/database';
|
|
2
|
+
import type { Application, Plugin } from '@nocobase/server';
|
|
3
|
+
import type { LoopPattern } from './LoopPatternSchema';
|
|
4
|
+
import { LoopPatternService } from './LoopPatternService';
|
|
5
|
+
import { LoopTriggerService } from './LoopTriggerService';
|
|
6
|
+
|
|
7
|
+
type ManagedCronJob = ReturnType<Application['cronJobManager']['addJob']>;
|
|
8
|
+
|
|
9
|
+
type ScheduledPattern = {
|
|
10
|
+
job: ManagedCronJob;
|
|
11
|
+
signature: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type TransactionOptions = {
|
|
15
|
+
transaction?: Transaction;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type LoopSchedulerSyncMessage = {
|
|
19
|
+
type: 'loop-pattern-schedule-changed';
|
|
20
|
+
patternId: number;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function read(record: Model | Record<string, unknown>, key: string) {
|
|
24
|
+
const model = record as Model & { get?: (name: string) => unknown };
|
|
25
|
+
return typeof model.get === 'function' ? model.get(key) : (record as Record<string, unknown>)[key];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function positiveInteger(value: unknown) {
|
|
29
|
+
const parsed = Number(value);
|
|
30
|
+
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isLockAcquireError(error: unknown) {
|
|
34
|
+
return error instanceof Error && error.constructor.name === 'LockAcquireError';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function tickDate(pattern: LoopPattern, now: Date) {
|
|
38
|
+
const fields = pattern.cronExpression?.split(/\s+/).filter(Boolean).length || 5;
|
|
39
|
+
const interval = fields === 6 ? 1_000 : 60_000;
|
|
40
|
+
return new Date(Math.floor(now.getTime() / interval) * interval);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class LoopSchedulerService {
|
|
44
|
+
private readonly scheduled = new Map<number, ScheduledPattern>();
|
|
45
|
+
private started = false;
|
|
46
|
+
private installed = false;
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
private readonly plugin: Plugin,
|
|
50
|
+
private readonly patterns: LoopPatternService,
|
|
51
|
+
private readonly triggers: LoopTriggerService,
|
|
52
|
+
) {}
|
|
53
|
+
|
|
54
|
+
install() {
|
|
55
|
+
if (this.installed) return;
|
|
56
|
+
this.installed = true;
|
|
57
|
+
this.plugin.db.on('agentLoopPatterns.afterCreate', this.onPatternChanged);
|
|
58
|
+
this.plugin.db.on('agentLoopPatterns.afterUpdate', this.onPatternChanged);
|
|
59
|
+
this.plugin.db.on('agentLoopPatterns.afterDestroy', this.onPatternChanged);
|
|
60
|
+
this.plugin.app.on('afterStart', this.start);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async dispose() {
|
|
64
|
+
if (!this.installed) return;
|
|
65
|
+
this.installed = false;
|
|
66
|
+
this.plugin.db.off('agentLoopPatterns.afterCreate', this.onPatternChanged);
|
|
67
|
+
this.plugin.db.off('agentLoopPatterns.afterUpdate', this.onPatternChanged);
|
|
68
|
+
this.plugin.db.off('agentLoopPatterns.afterDestroy', this.onPatternChanged);
|
|
69
|
+
this.plugin.app.off('afterStart', this.start);
|
|
70
|
+
this.stop();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
readonly start = async () => {
|
|
74
|
+
if (this.started) return;
|
|
75
|
+
this.started = true;
|
|
76
|
+
await this.resyncAll();
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
stop() {
|
|
80
|
+
this.started = false;
|
|
81
|
+
for (const { job } of this.scheduled.values()) this.plugin.app.cronJobManager.removeJob(job);
|
|
82
|
+
this.scheduled.clear();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async handleSyncMessage(message: unknown) {
|
|
86
|
+
if (!message || typeof message !== 'object') return;
|
|
87
|
+
const value = message as Partial<LoopSchedulerSyncMessage>;
|
|
88
|
+
if (value.type !== 'loop-pattern-schedule-changed') return;
|
|
89
|
+
const patternId = positiveInteger(value.patternId);
|
|
90
|
+
if (!patternId) return;
|
|
91
|
+
await this.resyncPattern(patternId);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async resyncAll() {
|
|
95
|
+
if (!this.started) return;
|
|
96
|
+
const records = await this.plugin.db.getRepository('agentLoopPatterns').find({ fields: ['id'] });
|
|
97
|
+
const patternIds = new Set<number>();
|
|
98
|
+
for (const record of records) {
|
|
99
|
+
const patternId = positiveInteger(read(record, 'id'));
|
|
100
|
+
if (!patternId) continue;
|
|
101
|
+
patternIds.add(patternId);
|
|
102
|
+
await this.resyncPattern(patternId);
|
|
103
|
+
}
|
|
104
|
+
for (const patternId of this.scheduled.keys()) {
|
|
105
|
+
if (!patternIds.has(patternId)) this.remove(patternId);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async resyncPattern(patternId: number) {
|
|
110
|
+
if (!this.started) return;
|
|
111
|
+
const record = await this.plugin.db.getRepository('agentLoopPatterns').findOne({ filterByTk: patternId });
|
|
112
|
+
if (!record || read(record, 'enabled') !== true || read(record, 'triggerType') !== 'cron') {
|
|
113
|
+
this.remove(patternId);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
const { pattern } = await this.patterns.get(patternId);
|
|
119
|
+
const signature = JSON.stringify([pattern.cronExpression, pattern.timezone]);
|
|
120
|
+
if (this.scheduled.get(patternId)?.signature === signature) return;
|
|
121
|
+
this.remove(patternId);
|
|
122
|
+
const job = this.plugin.app.cronJobManager.addJob({
|
|
123
|
+
cronTime: pattern.cronExpression as string,
|
|
124
|
+
timeZone: pattern.timezone as string,
|
|
125
|
+
onTick: () => this.onTick(patternId, pattern).catch((error) => this.logTickError(patternId, error)),
|
|
126
|
+
});
|
|
127
|
+
this.scheduled.set(patternId, { job, signature });
|
|
128
|
+
if (this.plugin.app.cronJobManager.started) job.start();
|
|
129
|
+
this.plugin.app.logger.info(`[AgentOrchestrator] Scheduled loop pattern ${pattern.key}.`, {
|
|
130
|
+
patternId,
|
|
131
|
+
cronExpression: pattern.cronExpression,
|
|
132
|
+
timezone: pattern.timezone,
|
|
133
|
+
});
|
|
134
|
+
} catch (error) {
|
|
135
|
+
this.remove(patternId);
|
|
136
|
+
this.plugin.app.logger.error(`[AgentOrchestrator] Loop pattern ${patternId} could not be scheduled.`, { error });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private readonly onPatternChanged = async (record: Model, options: TransactionOptions = {}) => {
|
|
141
|
+
const patternId = positiveInteger(read(record, 'id'));
|
|
142
|
+
if (!patternId) return;
|
|
143
|
+
const apply = async () => {
|
|
144
|
+
await this.resyncPattern(patternId);
|
|
145
|
+
await this.plugin.sendSyncMessage({ type: 'loop-pattern-schedule-changed', patternId });
|
|
146
|
+
};
|
|
147
|
+
if (options.transaction) {
|
|
148
|
+
// afterCommit does not await its callback, so a rejection here would be
|
|
149
|
+
// an unhandled promise. Catch and log so a failed resync/broadcast does
|
|
150
|
+
// not silently drop the schedule change.
|
|
151
|
+
options.transaction.afterCommit(() =>
|
|
152
|
+
apply().catch((error) =>
|
|
153
|
+
this.plugin.app.logger.error(
|
|
154
|
+
`[AgentOrchestrator] Failed to apply schedule change for loop pattern ${patternId} after commit.`,
|
|
155
|
+
{ error },
|
|
156
|
+
),
|
|
157
|
+
),
|
|
158
|
+
);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
await apply();
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
private async onTick(patternId: number, pattern: LoopPattern) {
|
|
165
|
+
const scheduledAt = tickDate(pattern, new Date());
|
|
166
|
+
const triggerKey = `cron:${pattern.key}:${scheduledAt.toISOString()}`;
|
|
167
|
+
let lock;
|
|
168
|
+
try {
|
|
169
|
+
lock = await this.plugin.app.lockManager.tryAcquire(
|
|
170
|
+
`agent-loop:schedule:${this.plugin.app.name}:${patternId}:${triggerKey}`,
|
|
171
|
+
);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
if (isLockAcquireError(error)) return;
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
await lock.runExclusive(async () => {
|
|
178
|
+
await this.triggers.enqueue({
|
|
179
|
+
patternId,
|
|
180
|
+
triggerType: 'cron',
|
|
181
|
+
triggerKey,
|
|
182
|
+
triggerPayload: { scheduledAt: scheduledAt.toISOString(), timezone: pattern.timezone },
|
|
183
|
+
});
|
|
184
|
+
}, 60_000);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private remove(patternId: number) {
|
|
188
|
+
const existing = this.scheduled.get(patternId);
|
|
189
|
+
if (!existing) return;
|
|
190
|
+
this.plugin.app.cronJobManager.removeJob(existing.job);
|
|
191
|
+
this.scheduled.delete(patternId);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private logTickError(patternId: number, error: unknown) {
|
|
195
|
+
this.plugin.app.logger.error(`[AgentOrchestrator] Scheduled loop pattern ${patternId} failed to enqueue.`, {
|
|
196
|
+
error,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|