plugin-agent-orchestrator 1.1.3 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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/253.59577b40d9875be0.js +10 -0
- package/dist/client-v2/264.7ed8bee71fa5935c.js +10 -0
- package/dist/client-v2/{41.ba6e080cc0488143.js → 41.3deb9e57a0f64fc4.js} +1 -1
- package/dist/client-v2/418.41ed200e7d6da332.js +10 -0
- package/dist/client-v2/53.349fff8db80b19d4.js +10 -0
- package/dist/client-v2/619.0a2144aaffefe018.js +10 -0
- package/dist/client-v2/{70.bda9518881c05360.js → 70.ffff2997c10fd4b0.js} +1 -1
- package/dist/client-v2/{794.9bbb8d3a61b2bdf2.js → 794.a67a2614405ba627.js} +1 -1
- package/dist/client-v2/981.e4a1f41cfc94a856.js +10 -0
- package/dist/client-v2/{993.82778b6fd6db3cb1.js → 993.b0688fdc5ccb000a.js} +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +8 -7
- package/dist/locale/en-US.json +199 -123
- package/dist/locale/vi-VN.json +199 -123
- package/dist/locale/zh-CN.json +199 -123
- package/dist/server/collections/agent-execution-spans.js +55 -0
- package/dist/server/collections/agent-harness-profile-versions.js +56 -0
- package/dist/server/collections/agent-harness-profiles.js +12 -0
- package/dist/server/collections/agent-loop-action-approvals.js +69 -0
- package/dist/server/collections/agent-loop-artifacts.js +55 -0
- package/dist/server/collections/agent-loop-circuit-states.js +53 -0
- package/dist/server/collections/agent-loop-control-settings.js +51 -0
- package/dist/server/collections/agent-loop-events.js +27 -0
- package/dist/server/collections/agent-loop-path-locks.js +57 -0
- package/dist/server/collections/agent-loop-patterns.js +72 -0
- package/dist/server/collections/agent-loop-runs.js +170 -3
- package/dist/server/collections/agent-loop-steps.js +76 -0
- package/dist/server/collections/agent-loop-usage-buckets.js +52 -0
- package/dist/server/collections/agent-loop-worktrees.js +55 -0
- package/dist/server/collections/skill-executions.js +8 -0
- package/dist/server/migrations/{20260427000000-change-packages-to-text.js → 20260427000002-change-packages-to-text.js} +2 -0
- package/dist/server/migrations/20260429000000-add-llm-fields.js +3 -2
- package/dist/server/migrations/{20260429000000-fix-inputargs-json-to-text.js → 20260429000001-fix-inputargs-json-to-text.js} +14 -6
- package/dist/server/migrations/20260503000000-add-orchestrator-trace-fields.js +2 -1
- package/dist/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.js +2 -1
- package/dist/server/migrations/20260803010000-backfill-loop-control-plane-data.js +262 -0
- package/dist/server/plugin.js +54 -150
- 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 +108 -0
- package/dist/server/resources/agent-loop-artifact.js +86 -0
- package/dist/server/resources/agent-loop-events-stream.js +130 -0
- package/dist/server/resources/agent-loop.js +183 -139
- package/dist/server/resources/resource-helpers.js +63 -0
- package/dist/server/resources/tracing.js +121 -107
- package/dist/server/services/AgentMemoryContextService.js +34 -0
- package/dist/server/services/DurableCircuitBreakerService.js +229 -0
- package/dist/server/services/HarnessCompiler.js +175 -0
- package/dist/server/services/HarnessProfileService.js +233 -0
- package/dist/server/services/HarnessSchema.js +194 -0
- package/dist/server/services/LoopBudgetService.js +269 -0
- package/dist/server/services/LoopControlService.js +139 -0
- package/dist/server/services/LoopPatternSchema.js +195 -0
- package/dist/server/services/LoopPatternService.js +132 -0
- package/dist/server/services/LoopRunRepository.js +285 -0
- package/dist/server/services/LoopRunStateMachine.js +478 -0
- package/dist/server/services/LoopSchedulerService.js +193 -0
- package/dist/server/services/LoopTriggerService.js +165 -0
- package/dist/server/services/LoopWorkerService.js +699 -0
- package/dist/server/services/NativeSubAgentObserver.js +165 -23
- package/dist/server/services/PathLockService.js +288 -0
- package/dist/server/services/PluginAiRuntimeAdapter.js +226 -0
- package/dist/server/services/ToolLoopDetectionService.js +210 -0
- package/dist/server/services/VerificationSchema.js +59 -0
- package/dist/server/services/VerificationService.js +235 -0
- package/dist/server/services/harness-runtime-policy.js +139 -0
- package/dist/server/skill-hub/actions/git-import.js +13 -7
- package/dist/server/skill-hub/plugin.js +36 -3
- package/dist/server/skill-hub/services/RegistrySkillSnapshotService.js +1 -5
- package/dist/server/skill-hub/tasks/SkillExecutionTask.js +29 -0
- package/dist/server/utils/ensure-indexes.js +111 -0
- package/dist/server/utils/loop-pattern-context.js +56 -0
- package/package.json +53 -53
- package/src/client/ApprovalsTab.tsx +2 -0
- package/src/client/OrchestratorSettings.tsx +55 -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 +100 -18
- package/src/client-v2/components/ApprovalsTab.tsx +337 -0
- package/src/client-v2/components/HarnessProfilesTab.tsx +385 -100
- package/src/client-v2/components/MemoryInspectorTab.tsx +141 -125
- package/src/client-v2/components/TracingTab.tsx +160 -77
- package/src/client-v2/hooks/useApiRequest.ts +28 -8
- 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 +11 -6
- package/src/locale/en-US.json +199 -123
- package/src/locale/vi-VN.json +199 -123
- package/src/locale/zh-CN.json +199 -123
- package/src/server/__tests__/circuit-breaker.test.ts +248 -169
- package/src/server/__tests__/harness-compiler.test.ts +214 -0
- package/src/server/__tests__/harness-profile-service.test.ts +167 -0
- package/src/server/__tests__/harness-runtime-policy.test.ts +293 -0
- package/src/server/__tests__/harness-tool-plan.test.ts +172 -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 -0
- package/src/server/__tests__/loop-control-plane-backfill.test.ts +266 -0
- package/src/server/__tests__/loop-pattern-schema.test.ts +73 -0
- package/src/server/__tests__/loop-pattern-service.test.ts +148 -0
- package/src/server/__tests__/loop-run-state-machine.test.ts +101 -0
- package/src/server/__tests__/loop-trigger-service.test.ts +148 -0
- package/src/server/__tests__/loop-worker-claim.test.ts +341 -0
- package/src/server/__tests__/loop-worker-execution.test.ts +317 -0
- package/src/server/__tests__/native-sub-agent-observer.test.ts +288 -4
- package/src/server/__tests__/path-lock-service.test.ts +254 -0
- package/src/server/__tests__/skill-execution-task-policy.test.ts +140 -0
- package/src/server/__tests__/smoke.test.ts +162 -121
- package/src/server/__tests__/tool-loop-detection.test.ts +275 -0
- package/src/server/collections/agent-execution-spans.ts +55 -0
- package/src/server/collections/agent-harness-profile-versions.ts +26 -0
- package/src/server/collections/agent-harness-profiles.ts +71 -59
- package/src/server/collections/agent-loop-action-approvals.ts +39 -0
- package/src/server/collections/agent-loop-artifacts.ts +25 -0
- package/src/server/collections/agent-loop-circuit-states.ts +23 -0
- package/src/server/collections/agent-loop-control-settings.ts +21 -0
- package/src/server/collections/agent-loop-events.ts +98 -71
- package/src/server/collections/agent-loop-path-locks.ts +27 -0
- package/src/server/collections/agent-loop-patterns.ts +42 -0
- package/src/server/collections/agent-loop-runs.ts +170 -3
- package/src/server/collections/agent-loop-steps.ts +76 -0
- package/src/server/collections/agent-loop-usage-buckets.ts +22 -0
- package/src/server/collections/agent-loop-worktrees.ts +25 -0
- package/src/server/collections/skill-executions.ts +8 -0
- package/src/server/migrations/{20260427000000-change-packages-to-text.ts → 20260427000002-change-packages-to-text.ts} +52 -49
- package/src/server/migrations/20260429000000-add-llm-fields.ts +47 -46
- package/src/server/migrations/{20260429000000-fix-inputargs-json-to-text.ts → 20260429000001-fix-inputargs-json-to-text.ts} +45 -38
- package/src/server/migrations/20260503000000-add-orchestrator-trace-fields.ts +33 -32
- package/src/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.ts +31 -30
- package/src/server/migrations/20260803010000-backfill-loop-control-plane-data.ts +293 -0
- package/src/server/plugin.ts +55 -167
- 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 +84 -0
- package/src/server/resources/agent-loop-artifact.ts +56 -0
- package/src/server/resources/agent-loop-events-stream.ts +105 -0
- package/src/server/resources/agent-loop.ts +204 -143
- package/src/server/resources/agent-monitor.ts +482 -482
- package/src/server/resources/resource-helpers.ts +33 -0
- package/src/server/resources/tracing.ts +266 -258
- package/src/server/services/AgentMemoryContextService.ts +44 -0
- package/src/server/services/DurableCircuitBreakerService.ts +239 -0
- package/src/server/services/ExecutionSpanService.ts +2 -0
- package/src/server/services/HarnessCompiler.ts +170 -0
- package/src/server/services/HarnessProfileService.ts +239 -0
- package/src/server/services/HarnessSchema.ts +194 -0
- package/src/server/services/LoopBudgetService.ts +275 -0
- package/src/server/services/LoopControlService.ts +124 -0
- package/src/server/services/LoopPatternSchema.ts +185 -0
- package/src/server/services/LoopPatternService.ts +143 -0
- package/src/server/services/LoopRunRepository.ts +336 -0
- package/src/server/services/LoopRunStateMachine.ts +529 -0
- package/src/server/services/LoopSchedulerService.ts +199 -0
- package/src/server/services/LoopTriggerService.ts +157 -0
- package/src/server/services/LoopWorkerService.ts +822 -0
- package/src/server/services/NativeSubAgentObserver.ts +186 -21
- package/src/server/services/PathLockService.ts +321 -0
- package/src/server/services/PluginAiRuntimeAdapter.ts +332 -0
- package/src/server/services/ToolLoopDetectionService.ts +213 -0
- package/src/server/services/VerificationSchema.ts +31 -0
- package/src/server/services/VerificationService.ts +264 -0
- package/src/server/services/harness-runtime-policy.ts +141 -0
- package/src/server/skill-hub/actions/__tests__/git-import.test.ts +15 -1
- package/src/server/skill-hub/actions/git-import.ts +9 -3
- package/src/server/skill-hub/plugin.ts +41 -3
- package/src/server/skill-hub/services/RegistrySkillSnapshotService.ts +235 -239
- package/src/server/skill-hub/services/__tests__/RegistrySkillSnapshotService.test.ts +61 -63
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +43 -2
- package/src/server/utils/ensure-indexes.ts +96 -0
- package/src/server/utils/logging.ts +62 -63
- package/src/server/utils/loop-pattern-context.ts +20 -0
- package/dist/client-v2/214.79650a549273f163.js +0 -10
- package/dist/client-v2/253.9f0537713cee9e6c.js +0 -10
- package/dist/client-v2/264.718a107e43fc163c.js +0 -10
- package/dist/client-v2/373.f5d5292e53c4e832.js +0 -10
- package/dist/client-v2/418.ce8f5b2b8a229d1d.js +0 -10
- package/dist/client-v2/53.7c29c28043e9112d.js +0 -10
- package/dist/client-v2/619.bd3c5698b40705c3.js +0 -10
- package/dist/client-v2/677.a991ce0250ff5c77.js +0 -10
- package/dist/client-v2/925.f5370de8f6632d65.js +0 -10
- package/dist/server/services/AgentHarness.js +0 -81
- package/dist/server/services/AgentLoopController.js +0 -1023
- package/dist/server/services/AgentLoopRepository.js +0 -192
- package/dist/server/services/AgentLoopService.js +0 -142
- package/dist/server/services/AgentPlanValidator.js +0 -99
- package/dist/server/services/AgentPlannerService.js +0 -99
- package/dist/server/services/CircuitBreaker.js +0 -125
- package/dist/server/services/ContextAggregator.js +0 -201
- package/dist/server/services/PlanningPromptService.js +0 -95
- package/dist/server/tools/agent-loop.js +0 -373
- package/dist/server/tools/orchestrator-plan.js +0 -254
- 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
- package/src/server/__tests__/agent-loop-controller.test.ts +0 -375
- package/src/server/__tests__/context-aggregator.test.ts +0 -222
- package/src/server/__tests__/parallel-execution.test.ts +0 -318
- package/src/server/__tests__/planning-prompt-service.test.ts +0 -23
- package/src/server/services/AgentHarness.ts +0 -67
- package/src/server/services/AgentLoopController.ts +0 -1263
- package/src/server/services/AgentLoopRepository.ts +0 -178
- package/src/server/services/AgentLoopService.ts +0 -177
- package/src/server/services/AgentPlanValidator.ts +0 -73
- package/src/server/services/AgentPlannerService.ts +0 -80
- package/src/server/services/CircuitBreaker.ts +0 -126
- package/src/server/services/ContextAggregator.ts +0 -239
- package/src/server/services/PlanningPromptService.ts +0 -72
- package/src/server/tools/agent-loop.ts +0 -360
- package/src/server/tools/orchestrator-plan.ts +0 -255
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var LoopWorkerService_exports = {};
|
|
28
|
+
__export(LoopWorkerService_exports, {
|
|
29
|
+
LoopWorkerService: () => LoopWorkerService,
|
|
30
|
+
loopWorkerAbortMessage: () => loopWorkerAbortMessage
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(LoopWorkerService_exports);
|
|
33
|
+
var import_node_crypto = require("node:crypto");
|
|
34
|
+
var import_DurableCircuitBreakerService = require("./DurableCircuitBreakerService");
|
|
35
|
+
var import_LoopBudgetService = require("./LoopBudgetService");
|
|
36
|
+
var import_LoopControlService = require("./LoopControlService");
|
|
37
|
+
var import_LoopPatternSchema = require("./LoopPatternSchema");
|
|
38
|
+
var import_LoopRunStateMachine = require("./LoopRunStateMachine");
|
|
39
|
+
var import_PathLockService = require("./PathLockService");
|
|
40
|
+
var import_PluginAiRuntimeAdapter = require("./PluginAiRuntimeAdapter");
|
|
41
|
+
var import_ToolLoopDetectionService = require("./ToolLoopDetectionService");
|
|
42
|
+
var import_VerificationService = require("./VerificationService");
|
|
43
|
+
const POLL_INTERVAL_MS = 3e3;
|
|
44
|
+
const LEASE_MS = 12e4;
|
|
45
|
+
const HEARTBEAT_MS = 3e4;
|
|
46
|
+
const PATH_LOCK_TTL_MS = 9e5;
|
|
47
|
+
const LEADER_SYSTEM_MESSAGE = "You lead an autonomous run. Break the goal into concrete work for your makers, then finish with a concise plan of what must be built and which evidence will prove it. You do not modify anything yourself.";
|
|
48
|
+
const MAKER_SYSTEM_MESSAGE = "You are a maker on an autonomous run. Do the work assigned by the leader using only the tools you were granted, and report exactly what you changed and the evidence for it.";
|
|
49
|
+
function loopWorkerAbortMessage(runId) {
|
|
50
|
+
return { type: "loop-run-abort-requested", runId };
|
|
51
|
+
}
|
|
52
|
+
function read(record, key) {
|
|
53
|
+
const model = record;
|
|
54
|
+
return typeof model.get === "function" ? model.get(key) : record[key];
|
|
55
|
+
}
|
|
56
|
+
function asObject(value) {
|
|
57
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
58
|
+
}
|
|
59
|
+
function asStringArray(value) {
|
|
60
|
+
return Array.isArray(value) ? value.map((item) => String(item)).filter(Boolean) : [];
|
|
61
|
+
}
|
|
62
|
+
function autonomyLevel(value) {
|
|
63
|
+
return value === "L2" || value === "L3" ? value : "L1";
|
|
64
|
+
}
|
|
65
|
+
function harnessFrom(value) {
|
|
66
|
+
const snapshot = asObject(value);
|
|
67
|
+
const effective = asObject(snapshot.effective);
|
|
68
|
+
if (!effective.tools) throw new Error("The run harness snapshot is missing its compiled settings.");
|
|
69
|
+
return effective;
|
|
70
|
+
}
|
|
71
|
+
function resumeContextFrom(value) {
|
|
72
|
+
const context = asObject(value);
|
|
73
|
+
if (!context) return null;
|
|
74
|
+
const role = context.role === "leader" || context.role === "maker" ? context.role : null;
|
|
75
|
+
const username = typeof context.username === "string" ? context.username : "";
|
|
76
|
+
const sessionId = typeof context.sessionId === "string" ? context.sessionId : "";
|
|
77
|
+
const messageId = typeof context.messageId === "string" ? context.messageId : "";
|
|
78
|
+
if (!role || !username || !sessionId || !messageId) return null;
|
|
79
|
+
return {
|
|
80
|
+
role,
|
|
81
|
+
username,
|
|
82
|
+
sessionId,
|
|
83
|
+
messageId,
|
|
84
|
+
makerReports: asStringArray(context.makerReports),
|
|
85
|
+
makerQueue: asStringArray(context.makerQueue)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function inputHash(value) {
|
|
89
|
+
return (0, import_node_crypto.createHash)("sha256").update(JSON.stringify(value ?? null)).digest("hex");
|
|
90
|
+
}
|
|
91
|
+
class LoopWorkerService {
|
|
92
|
+
constructor(app, database) {
|
|
93
|
+
this.app = app;
|
|
94
|
+
this.database = database;
|
|
95
|
+
const distributedLock = {
|
|
96
|
+
runExclusive: (key, operation, ttl) => this.app.lockManager.runExclusive(key, operation, ttl)
|
|
97
|
+
};
|
|
98
|
+
this.control = new import_LoopControlService.LoopControlService(database);
|
|
99
|
+
this.stateMachine = new import_LoopRunStateMachine.LoopRunStateMachine(database);
|
|
100
|
+
this.budgets = new import_LoopBudgetService.LoopBudgetService(database, distributedLock);
|
|
101
|
+
this.circuits = new import_DurableCircuitBreakerService.DurableCircuitBreakerService(database, distributedLock);
|
|
102
|
+
this.pathLocks = new import_PathLockService.PathLockService(database, distributedLock);
|
|
103
|
+
this.runtime = new import_PluginAiRuntimeAdapter.PluginAiRuntimeAdapter(app);
|
|
104
|
+
this.verification = new import_VerificationService.VerificationService(database, this.runtime, this.stateMachine);
|
|
105
|
+
this.loopGuard = new import_ToolLoopDetectionService.ToolLoopDetectionService(database);
|
|
106
|
+
this.workerId = `${app.name}:${process.pid}:${(0, import_node_crypto.randomUUID)()}`;
|
|
107
|
+
}
|
|
108
|
+
control;
|
|
109
|
+
stateMachine;
|
|
110
|
+
budgets;
|
|
111
|
+
circuits;
|
|
112
|
+
pathLocks;
|
|
113
|
+
runtime;
|
|
114
|
+
verification;
|
|
115
|
+
loopGuard;
|
|
116
|
+
workerId;
|
|
117
|
+
active = /* @__PURE__ */ new Map();
|
|
118
|
+
heldPathLocks = /* @__PURE__ */ new Map();
|
|
119
|
+
timer = null;
|
|
120
|
+
running = false;
|
|
121
|
+
draining = null;
|
|
122
|
+
start = () => {
|
|
123
|
+
if (this.running || !this.app.serving("agent-loop:worker")) return;
|
|
124
|
+
this.running = true;
|
|
125
|
+
this.scheduleNextPoll();
|
|
126
|
+
};
|
|
127
|
+
async stop() {
|
|
128
|
+
this.running = false;
|
|
129
|
+
if (this.timer) clearTimeout(this.timer);
|
|
130
|
+
this.timer = null;
|
|
131
|
+
for (const controller of this.active.values()) controller.abort();
|
|
132
|
+
if (this.draining) await this.draining.catch(() => void 0);
|
|
133
|
+
}
|
|
134
|
+
abortRun(runId) {
|
|
135
|
+
var _a;
|
|
136
|
+
(_a = this.active.get(runId)) == null ? void 0 : _a.abort();
|
|
137
|
+
}
|
|
138
|
+
// A run is only active on the node that claimed it, and `sendSyncMessage` publishes with
|
|
139
|
+
// `skipSelf`, so pause/cancel aborts locally and broadcasts for whichever other node holds it.
|
|
140
|
+
async handleSyncMessage(message) {
|
|
141
|
+
if (!message || typeof message !== "object") return;
|
|
142
|
+
const value = message;
|
|
143
|
+
if (value.type !== "loop-run-abort-requested") return;
|
|
144
|
+
const runId = Number(value.runId);
|
|
145
|
+
if (!Number.isSafeInteger(runId) || runId <= 0) return;
|
|
146
|
+
this.abortRun(runId);
|
|
147
|
+
}
|
|
148
|
+
scheduleNextPoll() {
|
|
149
|
+
if (!this.running) return;
|
|
150
|
+
this.timer = setTimeout(() => {
|
|
151
|
+
this.draining = this.poll().catch((error) => {
|
|
152
|
+
this.app.logger.error("[AgentOrchestrator] Loop worker poll failed.", { error });
|
|
153
|
+
}).finally(() => this.scheduleNextPoll());
|
|
154
|
+
}, POLL_INTERVAL_MS);
|
|
155
|
+
}
|
|
156
|
+
async poll() {
|
|
157
|
+
if (!this.running) return;
|
|
158
|
+
await this.stateMachine.expireOverdueApprovals(/* @__PURE__ */ new Date()).catch((error) => {
|
|
159
|
+
this.app.logger.error("[AgentOrchestrator] Approval expiry sweep failed.", { error });
|
|
160
|
+
});
|
|
161
|
+
const claimed = await this.stateMachine.claimNext(this.workerId, LEASE_MS);
|
|
162
|
+
if (!claimed) return;
|
|
163
|
+
await this.execute(claimed);
|
|
164
|
+
}
|
|
165
|
+
async execute(claimed) {
|
|
166
|
+
const runId = Number(claimed.run.id);
|
|
167
|
+
const controller = new AbortController();
|
|
168
|
+
this.active.set(runId, controller);
|
|
169
|
+
const heartbeat = setInterval(() => {
|
|
170
|
+
this.heartbeat(runId, claimed.leaseToken).catch((error) => {
|
|
171
|
+
this.app.logger.warn(`[AgentOrchestrator] Loop run ${runId} lost its worker lease.`, { error });
|
|
172
|
+
controller.abort();
|
|
173
|
+
});
|
|
174
|
+
}, HEARTBEAT_MS);
|
|
175
|
+
let snapshot = null;
|
|
176
|
+
try {
|
|
177
|
+
snapshot = this.snapshotFrom(claimed.run);
|
|
178
|
+
await this.runToCompletion(snapshot, claimed.leaseToken, controller.signal);
|
|
179
|
+
} catch (error) {
|
|
180
|
+
await this.failRun(runId, claimed.leaseToken, snapshot, error);
|
|
181
|
+
} finally {
|
|
182
|
+
clearInterval(heartbeat);
|
|
183
|
+
this.active.delete(runId);
|
|
184
|
+
const repositoryKey = this.heldPathLocks.get(runId);
|
|
185
|
+
this.heldPathLocks.delete(runId);
|
|
186
|
+
if (repositoryKey) {
|
|
187
|
+
await this.pathLocks.release(runId, repositoryKey, claimed.leaseToken).catch((error) => this.app.logger.warn(`[AgentOrchestrator] Path lock release failed.`, { error }));
|
|
188
|
+
}
|
|
189
|
+
await this.stateMachine.releaseLease(runId, claimed.leaseToken).catch(() => void 0);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async heartbeat(runId, leaseToken) {
|
|
193
|
+
await this.stateMachine.renewLease(runId, leaseToken, LEASE_MS);
|
|
194
|
+
const repositoryKey = this.heldPathLocks.get(runId);
|
|
195
|
+
if (repositoryKey) await this.pathLocks.renew(runId, repositoryKey, PATH_LOCK_TTL_MS, leaseToken);
|
|
196
|
+
}
|
|
197
|
+
async runToCompletion(snapshot, leaseToken, signal) {
|
|
198
|
+
var _a;
|
|
199
|
+
await this.control.assertCanExecute();
|
|
200
|
+
const scopeKey = `pattern:${snapshot.patternId}`;
|
|
201
|
+
await this.circuits.authorizeAttempt({
|
|
202
|
+
patternId: snapshot.patternId,
|
|
203
|
+
scopeKey,
|
|
204
|
+
policy: snapshot.policy.circuit
|
|
205
|
+
});
|
|
206
|
+
if (!await this.acquirePaths(snapshot, leaseToken)) return;
|
|
207
|
+
if (snapshot.resumeContext && snapshot.approvalStatus === "pending") {
|
|
208
|
+
await this.stateMachine.transition({
|
|
209
|
+
runId: snapshot.id,
|
|
210
|
+
to: "waiting_approval",
|
|
211
|
+
actorType: "worker",
|
|
212
|
+
actorIdentity: this.workerId,
|
|
213
|
+
leaseToken,
|
|
214
|
+
eventType: "run_waiting_approval",
|
|
215
|
+
title: "Waiting for action approval",
|
|
216
|
+
content: "Run requeued while its approval decisions are still pending.",
|
|
217
|
+
correlationKey: `approval-repark:${snapshot.id}`,
|
|
218
|
+
values: { approvalStatus: "pending" }
|
|
219
|
+
});
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
await this.stateMachine.transition({
|
|
223
|
+
runId: snapshot.id,
|
|
224
|
+
to: "running",
|
|
225
|
+
actorType: "worker",
|
|
226
|
+
actorIdentity: this.workerId,
|
|
227
|
+
leaseToken,
|
|
228
|
+
eventType: "run_started",
|
|
229
|
+
title: snapshot.resumeContext ? "Run resumed" : "Run started",
|
|
230
|
+
content: snapshot.resumeContext ? `${snapshot.resumeContext.username} resumed after approval decisions.` : `Leader ${snapshot.leaderUsername} began working on the goal.`,
|
|
231
|
+
correlationKey: `started:${snapshot.id}`,
|
|
232
|
+
values: { currentRole: snapshot.resumeContext ? snapshot.resumeContext.role : "leader" }
|
|
233
|
+
});
|
|
234
|
+
const loopNotices = await this.loopGuard.existingNotices(snapshot.id);
|
|
235
|
+
let makerReports;
|
|
236
|
+
let makerQueue;
|
|
237
|
+
if (snapshot.resumeContext) {
|
|
238
|
+
const resume = snapshot.resumeContext;
|
|
239
|
+
const decisions = await this.consumeDecisions(snapshot.id);
|
|
240
|
+
const resumed = await this.resumeRole(snapshot, resume, decisions, signal);
|
|
241
|
+
const resumeGuard = await this.enforceToolLoopGuard(
|
|
242
|
+
snapshot,
|
|
243
|
+
leaseToken,
|
|
244
|
+
{ role: resume.role, username: resume.username },
|
|
245
|
+
loopNotices
|
|
246
|
+
);
|
|
247
|
+
if (resumeGuard === "halt") return;
|
|
248
|
+
if (resumed.interrupted.length) {
|
|
249
|
+
await this.parkForApproval(snapshot, resumed, leaseToken, {
|
|
250
|
+
role: resume.role,
|
|
251
|
+
username: resume.username,
|
|
252
|
+
makerReports: resume.makerReports,
|
|
253
|
+
makerQueue: resume.makerQueue
|
|
254
|
+
});
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (resume.role === "leader") {
|
|
258
|
+
makerReports = [resumed.content];
|
|
259
|
+
makerQueue = [...snapshot.makerUsernames];
|
|
260
|
+
} else {
|
|
261
|
+
makerReports = [...resume.makerReports, `# ${resume.username}
|
|
262
|
+
${resumed.content}`];
|
|
263
|
+
makerQueue = [...resume.makerQueue];
|
|
264
|
+
}
|
|
265
|
+
} else {
|
|
266
|
+
const leader = await this.invokeRole(snapshot, {
|
|
267
|
+
role: "leader",
|
|
268
|
+
username: snapshot.leaderUsername,
|
|
269
|
+
harness: snapshot.leaderHarness,
|
|
270
|
+
systemMessage: LEADER_SYSTEM_MESSAGE,
|
|
271
|
+
prompt: snapshot.goal,
|
|
272
|
+
leaseToken,
|
|
273
|
+
signal
|
|
274
|
+
});
|
|
275
|
+
const leaderGuard = await this.enforceToolLoopGuard(
|
|
276
|
+
snapshot,
|
|
277
|
+
leaseToken,
|
|
278
|
+
{ role: "leader", username: snapshot.leaderUsername },
|
|
279
|
+
loopNotices
|
|
280
|
+
);
|
|
281
|
+
if (leaderGuard === "halt") return;
|
|
282
|
+
if (leader.interrupted.length) {
|
|
283
|
+
await this.parkForApproval(snapshot, leader, leaseToken, {
|
|
284
|
+
role: "leader",
|
|
285
|
+
username: snapshot.leaderUsername,
|
|
286
|
+
makerReports: [],
|
|
287
|
+
makerQueue: snapshot.makerUsernames
|
|
288
|
+
});
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
makerReports = [leader.content];
|
|
292
|
+
makerQueue = [...snapshot.makerUsernames];
|
|
293
|
+
}
|
|
294
|
+
while (makerQueue.length) {
|
|
295
|
+
const username = makerQueue[0];
|
|
296
|
+
const harness = (_a = snapshot.makerHarnesses[username]) == null ? void 0 : _a.effective;
|
|
297
|
+
if (!harness) {
|
|
298
|
+
throw new Error(`The claimed run has no harness snapshot for maker "${username}".`);
|
|
299
|
+
}
|
|
300
|
+
const maker = await this.invokeRole(snapshot, {
|
|
301
|
+
role: "maker",
|
|
302
|
+
username,
|
|
303
|
+
harness,
|
|
304
|
+
systemMessage: MAKER_SYSTEM_MESSAGE,
|
|
305
|
+
prompt: [
|
|
306
|
+
`Goal: ${snapshot.goal}`,
|
|
307
|
+
"",
|
|
308
|
+
"Leader's plan:",
|
|
309
|
+
makerReports[0] || "(the leader produced no textual plan)",
|
|
310
|
+
...loopNotices
|
|
311
|
+
].join("\n"),
|
|
312
|
+
leaseToken,
|
|
313
|
+
signal
|
|
314
|
+
});
|
|
315
|
+
const makerGuard = await this.enforceToolLoopGuard(
|
|
316
|
+
snapshot,
|
|
317
|
+
leaseToken,
|
|
318
|
+
{ role: "maker", username },
|
|
319
|
+
loopNotices
|
|
320
|
+
);
|
|
321
|
+
if (makerGuard === "halt") return;
|
|
322
|
+
if (maker.interrupted.length) {
|
|
323
|
+
await this.parkForApproval(snapshot, maker, leaseToken, {
|
|
324
|
+
role: "maker",
|
|
325
|
+
username,
|
|
326
|
+
makerReports,
|
|
327
|
+
makerQueue: makerQueue.slice(1)
|
|
328
|
+
});
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
makerReports.push(`# ${username}
|
|
332
|
+
${maker.content}`);
|
|
333
|
+
makerQueue = makerQueue.slice(1);
|
|
334
|
+
}
|
|
335
|
+
const makerSummary = makerReports.join("\n\n");
|
|
336
|
+
await this.stateMachine.transition({
|
|
337
|
+
runId: snapshot.id,
|
|
338
|
+
to: "verifying",
|
|
339
|
+
actorType: "worker",
|
|
340
|
+
actorIdentity: this.workerId,
|
|
341
|
+
leaseToken,
|
|
342
|
+
eventType: "run_verifying",
|
|
343
|
+
title: "Verification started",
|
|
344
|
+
content: `Verifier ${snapshot.verifierUsername} is reviewing the reported result.`,
|
|
345
|
+
correlationKey: `verifying:${snapshot.id}`,
|
|
346
|
+
values: { currentRole: "verifier", finalAnswer: makerSummary }
|
|
347
|
+
});
|
|
348
|
+
await this.budgets.reserve({
|
|
349
|
+
runId: snapshot.id,
|
|
350
|
+
patternId: snapshot.patternId,
|
|
351
|
+
policy: snapshot.policy,
|
|
352
|
+
delta: { verifications: 1 }
|
|
353
|
+
});
|
|
354
|
+
const outcome = await this.verification.verifyAndFinalize({
|
|
355
|
+
runId: snapshot.id,
|
|
356
|
+
goal: snapshot.goal,
|
|
357
|
+
autonomyLevel: snapshot.autonomyLevel,
|
|
358
|
+
verifierUsername: snapshot.verifierUsername,
|
|
359
|
+
leaderUsername: snapshot.leaderUsername,
|
|
360
|
+
makerUsernames: snapshot.makerUsernames,
|
|
361
|
+
verifierHarness: snapshot.verifierHarness,
|
|
362
|
+
policy: snapshot.policy,
|
|
363
|
+
makerSummary,
|
|
364
|
+
userId: snapshot.userId,
|
|
365
|
+
workerId: this.workerId,
|
|
366
|
+
leaseToken,
|
|
367
|
+
loopNotices,
|
|
368
|
+
signal
|
|
369
|
+
});
|
|
370
|
+
if (outcome.finalStatus === "halted") return;
|
|
371
|
+
if (outcome.finalStatus === "failed") {
|
|
372
|
+
await this.circuits.recordFailure({
|
|
373
|
+
patternId: snapshot.patternId,
|
|
374
|
+
scopeKey: `pattern:${snapshot.patternId}`,
|
|
375
|
+
policy: snapshot.policy.circuit,
|
|
376
|
+
error: new Error(`Verifier rejected run ${snapshot.id}.`),
|
|
377
|
+
lastRunId: snapshot.id
|
|
378
|
+
});
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
await this.circuits.recordSuccess(snapshot.patternId, `pattern:${snapshot.patternId}`, snapshot.id);
|
|
382
|
+
}
|
|
383
|
+
async acquirePaths(snapshot, leaseToken) {
|
|
384
|
+
if (!snapshot.repositoryKey || !snapshot.actingOn.length) return true;
|
|
385
|
+
const lock = await this.pathLocks.acquire({
|
|
386
|
+
runId: snapshot.id,
|
|
387
|
+
repositoryKey: snapshot.repositoryKey,
|
|
388
|
+
owner: leaseToken,
|
|
389
|
+
paths: snapshot.actingOn,
|
|
390
|
+
ttlMs: PATH_LOCK_TTL_MS
|
|
391
|
+
});
|
|
392
|
+
if (lock.acquired) {
|
|
393
|
+
this.heldPathLocks.set(snapshot.id, snapshot.repositoryKey);
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
const deadlocked = lock.deadlocked;
|
|
397
|
+
await this.stateMachine.transition({
|
|
398
|
+
runId: snapshot.id,
|
|
399
|
+
to: deadlocked ? "blocked" : "waiting_lock",
|
|
400
|
+
actorType: "worker",
|
|
401
|
+
actorIdentity: this.workerId,
|
|
402
|
+
leaseToken,
|
|
403
|
+
eventType: deadlocked ? "run_blocked" : "run_waiting_lock",
|
|
404
|
+
title: deadlocked ? "Run blocked by a lock cycle" : "Waiting for repository paths",
|
|
405
|
+
content: `Blocked by run(s): ${lock.blockers.join(", ")}.`,
|
|
406
|
+
correlationKey: `lock:${snapshot.id}:${lock.blockers.join("-")}`,
|
|
407
|
+
values: deadlocked ? { blockedReason: "Path lock wait graph contains a cycle." } : {}
|
|
408
|
+
});
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
async invokeRole(snapshot, input) {
|
|
412
|
+
await this.budgets.reserve({
|
|
413
|
+
runId: snapshot.id,
|
|
414
|
+
patternId: snapshot.patternId,
|
|
415
|
+
policy: snapshot.policy,
|
|
416
|
+
delta: { invocations: 1 }
|
|
417
|
+
});
|
|
418
|
+
const sessionId = await this.runtime.createConversation({
|
|
419
|
+
username: input.username,
|
|
420
|
+
userId: snapshot.userId,
|
|
421
|
+
runId: snapshot.id,
|
|
422
|
+
title: snapshot.goal.slice(0, 200)
|
|
423
|
+
});
|
|
424
|
+
const step = await this.database.getRepository("agentLoopSteps").create({
|
|
425
|
+
values: {
|
|
426
|
+
runId: snapshot.id,
|
|
427
|
+
runtimeVersion: "control-plane-v2",
|
|
428
|
+
sequence: await this.nextSequence(snapshot.id),
|
|
429
|
+
role: input.role,
|
|
430
|
+
kind: "invocation",
|
|
431
|
+
type: "reasoning",
|
|
432
|
+
employeeUsername: input.username,
|
|
433
|
+
sessionId,
|
|
434
|
+
title: `${input.role} invocation`,
|
|
435
|
+
inputHash: inputHash(input.prompt),
|
|
436
|
+
status: "running",
|
|
437
|
+
startedAt: /* @__PURE__ */ new Date(),
|
|
438
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
const outcome = await this.runtime.invoke({
|
|
442
|
+
username: input.username,
|
|
443
|
+
sessionId,
|
|
444
|
+
userId: snapshot.userId,
|
|
445
|
+
systemMessage: input.systemMessage,
|
|
446
|
+
harness: input.harness,
|
|
447
|
+
prompt: input.prompt,
|
|
448
|
+
signal: input.signal
|
|
449
|
+
});
|
|
450
|
+
await this.database.getRepository("agentLoopSteps").update({
|
|
451
|
+
filterByTk: read(step, "id"),
|
|
452
|
+
values: {
|
|
453
|
+
status: outcome.interrupted.length ? "waiting_user" : "succeeded",
|
|
454
|
+
output: { content: outcome.content },
|
|
455
|
+
endedAt: /* @__PURE__ */ new Date()
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
await this.database.getRepository("agentLoopRuns").update({
|
|
459
|
+
filterByTk: snapshot.id,
|
|
460
|
+
values: { sessionId: outcome.sessionId, messageId: outcome.messageId }
|
|
461
|
+
});
|
|
462
|
+
return outcome;
|
|
463
|
+
}
|
|
464
|
+
async resumeRole(snapshot, resume, decisions, signal) {
|
|
465
|
+
var _a;
|
|
466
|
+
await this.budgets.reserve({
|
|
467
|
+
runId: snapshot.id,
|
|
468
|
+
patternId: snapshot.patternId,
|
|
469
|
+
policy: snapshot.policy,
|
|
470
|
+
delta: { invocations: 1 }
|
|
471
|
+
});
|
|
472
|
+
const harness = resume.role === "leader" ? snapshot.leaderHarness : (_a = snapshot.makerHarnesses[resume.username]) == null ? void 0 : _a.effective;
|
|
473
|
+
if (!harness) {
|
|
474
|
+
throw new Error(`The claimed run has no harness snapshot for ${resume.role} "${resume.username}".`);
|
|
475
|
+
}
|
|
476
|
+
const step = await this.database.getRepository("agentLoopSteps").create({
|
|
477
|
+
values: {
|
|
478
|
+
runId: snapshot.id,
|
|
479
|
+
runtimeVersion: "control-plane-v2",
|
|
480
|
+
sequence: await this.nextSequence(snapshot.id),
|
|
481
|
+
role: resume.role,
|
|
482
|
+
kind: "invocation",
|
|
483
|
+
type: "resume",
|
|
484
|
+
employeeUsername: resume.username,
|
|
485
|
+
sessionId: resume.sessionId,
|
|
486
|
+
title: `${resume.role} resume after approval`,
|
|
487
|
+
inputHash: inputHash(decisions),
|
|
488
|
+
status: "running",
|
|
489
|
+
startedAt: /* @__PURE__ */ new Date(),
|
|
490
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
const outcome = await this.runtime.resume({
|
|
494
|
+
username: resume.username,
|
|
495
|
+
sessionId: resume.sessionId,
|
|
496
|
+
messageId: resume.messageId,
|
|
497
|
+
userId: snapshot.userId,
|
|
498
|
+
systemMessage: resume.role === "leader" ? LEADER_SYSTEM_MESSAGE : MAKER_SYSTEM_MESSAGE,
|
|
499
|
+
harness,
|
|
500
|
+
decisions,
|
|
501
|
+
signal
|
|
502
|
+
});
|
|
503
|
+
await this.database.getRepository("agentLoopSteps").update({
|
|
504
|
+
filterByTk: read(step, "id"),
|
|
505
|
+
values: {
|
|
506
|
+
status: outcome.interrupted.length ? "waiting_user" : "succeeded",
|
|
507
|
+
output: { content: outcome.content },
|
|
508
|
+
endedAt: /* @__PURE__ */ new Date()
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
await this.database.getRepository("agentLoopRuns").update({
|
|
512
|
+
filterByTk: snapshot.id,
|
|
513
|
+
values: { sessionId: outcome.sessionId, messageId: outcome.messageId }
|
|
514
|
+
});
|
|
515
|
+
return outcome;
|
|
516
|
+
}
|
|
517
|
+
// Run-level scan over every tool call issued so far, evaluated at each pass boundary. A warning
|
|
518
|
+
// is injected into the prompts composed later in the run; block and escalate halt it at once.
|
|
519
|
+
async enforceToolLoopGuard(snapshot, leaseToken, pass, loopNotices) {
|
|
520
|
+
const finding = await this.loopGuard.scanRun(snapshot.id, snapshot.policy);
|
|
521
|
+
if (!finding) return "continue";
|
|
522
|
+
await this.loopGuard.recordFinding(
|
|
523
|
+
{
|
|
524
|
+
runId: snapshot.id,
|
|
525
|
+
role: pass.role,
|
|
526
|
+
username: pass.username,
|
|
527
|
+
runStatus: "running",
|
|
528
|
+
actorType: "worker",
|
|
529
|
+
actorIdentity: this.workerId
|
|
530
|
+
},
|
|
531
|
+
finding
|
|
532
|
+
);
|
|
533
|
+
if (finding.level === "warn") {
|
|
534
|
+
const notice = (0, import_ToolLoopDetectionService.toolLoopNotice)(finding.toolName, finding.count);
|
|
535
|
+
if (!loopNotices.includes(notice)) loopNotices.push(notice);
|
|
536
|
+
return "continue";
|
|
537
|
+
}
|
|
538
|
+
const reason = (0, import_ToolLoopDetectionService.toolLoopReason)(finding);
|
|
539
|
+
await this.stateMachine.transition({
|
|
540
|
+
runId: snapshot.id,
|
|
541
|
+
to: finding.level === "block" ? "blocked" : "waiting_human",
|
|
542
|
+
actorType: "worker",
|
|
543
|
+
actorIdentity: this.workerId,
|
|
544
|
+
leaseToken,
|
|
545
|
+
eventType: finding.level === "block" ? "run_blocked" : "run_escalated",
|
|
546
|
+
title: finding.level === "block" ? "Run blocked by tool loop detection" : "Run escalated by tool loop detection",
|
|
547
|
+
content: reason,
|
|
548
|
+
correlationKey: `tool-loop-${finding.level}:${snapshot.id}:${finding.signature}`,
|
|
549
|
+
values: finding.level === "block" ? { blockedReason: reason } : { escalationReason: reason }
|
|
550
|
+
});
|
|
551
|
+
return "halt";
|
|
552
|
+
}
|
|
553
|
+
// Decisions are consumed exactly once: rows are marked consumed so a retry or a second resume
|
|
554
|
+
// can never replay a human decision against a fresh conversation.
|
|
555
|
+
async consumeDecisions(runId) {
|
|
556
|
+
const repository = this.database.getRepository("agentLoopActionApprovals");
|
|
557
|
+
const approvals = await repository.find({ filter: { runId } });
|
|
558
|
+
const decisions = [];
|
|
559
|
+
const now = /* @__PURE__ */ new Date();
|
|
560
|
+
for (const approval of approvals) {
|
|
561
|
+
const status = String(read(approval, "status"));
|
|
562
|
+
if (status !== "approved" && status !== "rejected") continue;
|
|
563
|
+
if (read(approval, "consumedAt")) continue;
|
|
564
|
+
const editedInput = read(approval, "editedInput");
|
|
565
|
+
const decision = status === "approved" ? editedInput ? { type: "edit", editedAction: { name: String(read(approval, "toolName")), args: editedInput } } : { type: "approve" } : { type: "reject", message: String(read(approval, "decisionNote") || "") || void 0 };
|
|
566
|
+
decisions.push({ toolCallId: String(read(approval, "toolCallId")), decision });
|
|
567
|
+
await repository.update({ filterByTk: Number(read(approval, "id")), values: { consumedAt: now } });
|
|
568
|
+
}
|
|
569
|
+
if (!decisions.length) throw new Error("The resumed run has no recorded approval decisions.");
|
|
570
|
+
return decisions;
|
|
571
|
+
}
|
|
572
|
+
async parkForApproval(snapshot, outcome, leaseToken, context) {
|
|
573
|
+
var _a;
|
|
574
|
+
const now = /* @__PURE__ */ new Date();
|
|
575
|
+
const expiresAt = new Date(now.getTime() + snapshot.policy.actions.approvalTimeoutMs);
|
|
576
|
+
const assignedToId = snapshot.policy.actions.approvalAssigneeIds[0] || snapshot.userId || null;
|
|
577
|
+
const harness = context.role === "leader" ? snapshot.leaderHarness : (_a = snapshot.makerHarnesses[context.username]) == null ? void 0 : _a.effective;
|
|
578
|
+
const escalatable = new Set((harness == null ? void 0 : harness.tools.escalate) ?? []);
|
|
579
|
+
for (const call of outcome.interrupted) {
|
|
580
|
+
const escalation = escalatable.has(call.toolName);
|
|
581
|
+
await this.database.getRepository("agentLoopActionApprovals").create({
|
|
582
|
+
values: {
|
|
583
|
+
runId: snapshot.id,
|
|
584
|
+
toolCallId: call.toolCallId,
|
|
585
|
+
toolName: call.toolName,
|
|
586
|
+
actionType: escalation ? "escalation" : "tool_call",
|
|
587
|
+
proposedInput: call.args,
|
|
588
|
+
inputHash: inputHash(call.args),
|
|
589
|
+
paths: snapshot.actingOn,
|
|
590
|
+
policyDecision: { decision: "ask", interruptId: call.interruptId },
|
|
591
|
+
reason: escalation ? `Tool "${call.toolName}" is not granted by this run's harness. Approving widens authority for this single call only.` : `Tool "${call.toolName}" requires human approval under this pattern's policy.`,
|
|
592
|
+
status: "pending",
|
|
593
|
+
assignedToId,
|
|
594
|
+
requestedById: snapshot.userId || null,
|
|
595
|
+
requestedAt: now,
|
|
596
|
+
expiresAt,
|
|
597
|
+
createdAt: now
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
await this.database.getRepository("agentLoopRuns").update({
|
|
602
|
+
filterByTk: snapshot.id,
|
|
603
|
+
values: {
|
|
604
|
+
// The resume point is durable: which role stopped, in which conversation, with which
|
|
605
|
+
// reports already collected and which makers still queued. Without it a resumed run
|
|
606
|
+
// would restart from the leader and redo every completed invocation.
|
|
607
|
+
resumeContext: {
|
|
608
|
+
role: context.role,
|
|
609
|
+
username: context.username,
|
|
610
|
+
sessionId: outcome.sessionId,
|
|
611
|
+
messageId: outcome.messageId,
|
|
612
|
+
makerReports: context.makerReports,
|
|
613
|
+
makerQueue: context.makerQueue
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
await this.stateMachine.transition({
|
|
618
|
+
runId: snapshot.id,
|
|
619
|
+
to: "waiting_approval",
|
|
620
|
+
actorType: "worker",
|
|
621
|
+
actorIdentity: this.workerId,
|
|
622
|
+
leaseToken,
|
|
623
|
+
eventType: "run_waiting_approval",
|
|
624
|
+
title: "Waiting for action approval",
|
|
625
|
+
content: `${outcome.interrupted.length} tool call(s) require approval.`,
|
|
626
|
+
correlationKey: `approval:${snapshot.id}:${outcome.messageId}`,
|
|
627
|
+
values: { approvalStatus: "pending", messageId: outcome.messageId }
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
async failRun(runId, leaseToken, snapshot, error) {
|
|
631
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
632
|
+
try {
|
|
633
|
+
if (snapshot) {
|
|
634
|
+
await this.circuits.recordFailure({
|
|
635
|
+
patternId: snapshot.patternId,
|
|
636
|
+
scopeKey: `pattern:${snapshot.patternId}`,
|
|
637
|
+
policy: snapshot.policy.circuit,
|
|
638
|
+
error,
|
|
639
|
+
lastRunId: runId
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
await this.stateMachine.transition({
|
|
643
|
+
runId,
|
|
644
|
+
to: "failed",
|
|
645
|
+
actorType: "worker",
|
|
646
|
+
actorIdentity: this.workerId,
|
|
647
|
+
leaseToken,
|
|
648
|
+
eventType: "run_failed",
|
|
649
|
+
title: "Run failed",
|
|
650
|
+
content: message,
|
|
651
|
+
correlationKey: `failed:${runId}`,
|
|
652
|
+
values: { summary: message }
|
|
653
|
+
});
|
|
654
|
+
} catch (transitionError) {
|
|
655
|
+
this.app.logger.error(`[AgentOrchestrator] Loop run ${runId} could not be marked failed.`, {
|
|
656
|
+
error: transitionError,
|
|
657
|
+
cause: message
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
async nextSequence(runId) {
|
|
662
|
+
const steps = await this.database.getRepository("agentLoopSteps").find({ filter: { runId } });
|
|
663
|
+
return steps.length;
|
|
664
|
+
}
|
|
665
|
+
snapshotFrom(run) {
|
|
666
|
+
const patternId = Number(run.patternId);
|
|
667
|
+
if (!Number.isSafeInteger(patternId) || patternId <= 0) {
|
|
668
|
+
throw new Error("The claimed run has no pattern snapshot.");
|
|
669
|
+
}
|
|
670
|
+
const roleBindings = asObject(run.roleBindingsSnapshot);
|
|
671
|
+
const makerSnapshots = asObject(run.makerHarnessSnapshot);
|
|
672
|
+
const approvalStatus = String(run.approvalStatus || "none");
|
|
673
|
+
return {
|
|
674
|
+
id: Number(run.id),
|
|
675
|
+
patternId,
|
|
676
|
+
goal: String(run.goal || ""),
|
|
677
|
+
autonomyLevel: autonomyLevel(run.autonomyLevel),
|
|
678
|
+
policy: import_LoopPatternSchema.loopPatternPolicySchema.parse(run.policySnapshot),
|
|
679
|
+
leaderUsername: String(roleBindings.leader || run.leaderUsername || ""),
|
|
680
|
+
makerUsernames: asStringArray(roleBindings.makers),
|
|
681
|
+
verifierUsername: String(roleBindings.verifier || run.verifierUsername || ""),
|
|
682
|
+
leaderHarness: harnessFrom(run.leaderHarnessSnapshot),
|
|
683
|
+
makerHarnesses: makerSnapshots,
|
|
684
|
+
verifierHarness: harnessFrom(run.verifierHarnessSnapshot),
|
|
685
|
+
repositoryKey: String(run.repositoryKey || ""),
|
|
686
|
+
actingOn: asStringArray(run.actingOn),
|
|
687
|
+
userId: Number(run.userId) || void 0,
|
|
688
|
+
approvalStatus,
|
|
689
|
+
// Only a live approval state may resume. Stale context left over from an expired window or a
|
|
690
|
+
// cleared retry would otherwise replay an old conversation against fresh approvals.
|
|
691
|
+
resumeContext: approvalStatus === "decided" || approvalStatus === "pending" ? resumeContextFrom(run.resumeContext) : null
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
696
|
+
0 && (module.exports = {
|
|
697
|
+
LoopWorkerService,
|
|
698
|
+
loopWorkerAbortMessage
|
|
699
|
+
});
|