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
|
@@ -38,11 +38,14 @@ var import_LoopPatternSchema = require("./LoopPatternSchema");
|
|
|
38
38
|
var import_LoopRunStateMachine = require("./LoopRunStateMachine");
|
|
39
39
|
var import_PathLockService = require("./PathLockService");
|
|
40
40
|
var import_PluginAiRuntimeAdapter = require("./PluginAiRuntimeAdapter");
|
|
41
|
+
var import_ToolLoopDetectionService = require("./ToolLoopDetectionService");
|
|
41
42
|
var import_VerificationService = require("./VerificationService");
|
|
42
43
|
const POLL_INTERVAL_MS = 3e3;
|
|
43
44
|
const LEASE_MS = 12e4;
|
|
44
45
|
const HEARTBEAT_MS = 3e4;
|
|
45
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.";
|
|
46
49
|
function loopWorkerAbortMessage(runId) {
|
|
47
50
|
return { type: "loop-run-abort-requested", runId };
|
|
48
51
|
}
|
|
@@ -65,6 +68,23 @@ function harnessFrom(value) {
|
|
|
65
68
|
if (!effective.tools) throw new Error("The run harness snapshot is missing its compiled settings.");
|
|
66
69
|
return effective;
|
|
67
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
|
+
}
|
|
68
88
|
function inputHash(value) {
|
|
69
89
|
return (0, import_node_crypto.createHash)("sha256").update(JSON.stringify(value ?? null)).digest("hex");
|
|
70
90
|
}
|
|
@@ -82,6 +102,7 @@ class LoopWorkerService {
|
|
|
82
102
|
this.pathLocks = new import_PathLockService.PathLockService(database, distributedLock);
|
|
83
103
|
this.runtime = new import_PluginAiRuntimeAdapter.PluginAiRuntimeAdapter(app);
|
|
84
104
|
this.verification = new import_VerificationService.VerificationService(database, this.runtime, this.stateMachine);
|
|
105
|
+
this.loopGuard = new import_ToolLoopDetectionService.ToolLoopDetectionService(database);
|
|
85
106
|
this.workerId = `${app.name}:${process.pid}:${(0, import_node_crypto.randomUUID)()}`;
|
|
86
107
|
}
|
|
87
108
|
control;
|
|
@@ -91,6 +112,7 @@ class LoopWorkerService {
|
|
|
91
112
|
pathLocks;
|
|
92
113
|
runtime;
|
|
93
114
|
verification;
|
|
115
|
+
loopGuard;
|
|
94
116
|
workerId;
|
|
95
117
|
active = /* @__PURE__ */ new Map();
|
|
96
118
|
heldPathLocks = /* @__PURE__ */ new Map();
|
|
@@ -133,6 +155,9 @@ class LoopWorkerService {
|
|
|
133
155
|
}
|
|
134
156
|
async poll() {
|
|
135
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
|
+
});
|
|
136
161
|
const claimed = await this.stateMachine.claimNext(this.workerId, LEASE_MS);
|
|
137
162
|
if (!claimed) return;
|
|
138
163
|
await this.execute(claimed);
|
|
@@ -179,6 +204,21 @@ class LoopWorkerService {
|
|
|
179
204
|
policy: snapshot.policy.circuit
|
|
180
205
|
});
|
|
181
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
|
+
}
|
|
182
222
|
await this.stateMachine.transition({
|
|
183
223
|
runId: snapshot.id,
|
|
184
224
|
to: "running",
|
|
@@ -186,26 +226,73 @@ class LoopWorkerService {
|
|
|
186
226
|
actorIdentity: this.workerId,
|
|
187
227
|
leaseToken,
|
|
188
228
|
eventType: "run_started",
|
|
189
|
-
title: "Run started",
|
|
190
|
-
content: `Leader ${snapshot.leaderUsername} began working on the goal.`,
|
|
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.`,
|
|
191
231
|
correlationKey: `started:${snapshot.id}`,
|
|
192
|
-
values: { currentRole: "leader" }
|
|
232
|
+
values: { currentRole: snapshot.resumeContext ? snapshot.resumeContext.role : "leader" }
|
|
193
233
|
});
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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];
|
|
206
293
|
}
|
|
207
|
-
|
|
208
|
-
|
|
294
|
+
while (makerQueue.length) {
|
|
295
|
+
const username = makerQueue[0];
|
|
209
296
|
const harness = (_a = snapshot.makerHarnesses[username]) == null ? void 0 : _a.effective;
|
|
210
297
|
if (!harness) {
|
|
211
298
|
throw new Error(`The claimed run has no harness snapshot for maker "${username}".`);
|
|
@@ -214,22 +301,36 @@ class LoopWorkerService {
|
|
|
214
301
|
role: "maker",
|
|
215
302
|
username,
|
|
216
303
|
harness,
|
|
217
|
-
systemMessage:
|
|
304
|
+
systemMessage: MAKER_SYSTEM_MESSAGE,
|
|
218
305
|
prompt: [
|
|
219
306
|
`Goal: ${snapshot.goal}`,
|
|
220
307
|
"",
|
|
221
308
|
"Leader's plan:",
|
|
222
|
-
|
|
309
|
+
makerReports[0] || "(the leader produced no textual plan)",
|
|
310
|
+
...loopNotices
|
|
223
311
|
].join("\n"),
|
|
224
312
|
leaseToken,
|
|
225
313
|
signal
|
|
226
314
|
});
|
|
315
|
+
const makerGuard = await this.enforceToolLoopGuard(
|
|
316
|
+
snapshot,
|
|
317
|
+
leaseToken,
|
|
318
|
+
{ role: "maker", username },
|
|
319
|
+
loopNotices
|
|
320
|
+
);
|
|
321
|
+
if (makerGuard === "halt") return;
|
|
227
322
|
if (maker.interrupted.length) {
|
|
228
|
-
await this.parkForApproval(snapshot, maker, leaseToken
|
|
323
|
+
await this.parkForApproval(snapshot, maker, leaseToken, {
|
|
324
|
+
role: "maker",
|
|
325
|
+
username,
|
|
326
|
+
makerReports,
|
|
327
|
+
makerQueue: makerQueue.slice(1)
|
|
328
|
+
});
|
|
229
329
|
return;
|
|
230
330
|
}
|
|
231
331
|
makerReports.push(`# ${username}
|
|
232
332
|
${maker.content}`);
|
|
333
|
+
makerQueue = makerQueue.slice(1);
|
|
233
334
|
}
|
|
234
335
|
const makerSummary = makerReports.join("\n\n");
|
|
235
336
|
await this.stateMachine.transition({
|
|
@@ -263,8 +364,10 @@ ${maker.content}`);
|
|
|
263
364
|
userId: snapshot.userId,
|
|
264
365
|
workerId: this.workerId,
|
|
265
366
|
leaseToken,
|
|
367
|
+
loopNotices,
|
|
266
368
|
signal
|
|
267
369
|
});
|
|
370
|
+
if (outcome.finalStatus === "halted") return;
|
|
268
371
|
if (outcome.finalStatus === "failed") {
|
|
269
372
|
await this.circuits.recordFailure({
|
|
270
373
|
patternId: snapshot.patternId,
|
|
@@ -327,6 +430,7 @@ ${maker.content}`);
|
|
|
327
430
|
kind: "invocation",
|
|
328
431
|
type: "reasoning",
|
|
329
432
|
employeeUsername: input.username,
|
|
433
|
+
sessionId,
|
|
330
434
|
title: `${input.role} invocation`,
|
|
331
435
|
inputHash: inputHash(input.prompt),
|
|
332
436
|
status: "running",
|
|
@@ -357,21 +461,134 @@ ${maker.content}`);
|
|
|
357
461
|
});
|
|
358
462
|
return outcome;
|
|
359
463
|
}
|
|
360
|
-
async
|
|
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;
|
|
361
574
|
const now = /* @__PURE__ */ new Date();
|
|
362
575
|
const expiresAt = new Date(now.getTime() + snapshot.policy.actions.approvalTimeoutMs);
|
|
363
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) ?? []);
|
|
364
579
|
for (const call of outcome.interrupted) {
|
|
580
|
+
const escalation = escalatable.has(call.toolName);
|
|
365
581
|
await this.database.getRepository("agentLoopActionApprovals").create({
|
|
366
582
|
values: {
|
|
367
583
|
runId: snapshot.id,
|
|
368
584
|
toolCallId: call.toolCallId,
|
|
369
585
|
toolName: call.toolName,
|
|
586
|
+
actionType: escalation ? "escalation" : "tool_call",
|
|
370
587
|
proposedInput: call.args,
|
|
371
588
|
inputHash: inputHash(call.args),
|
|
372
589
|
paths: snapshot.actingOn,
|
|
373
590
|
policyDecision: { decision: "ask", interruptId: call.interruptId },
|
|
374
|
-
reason: `Tool "${call.toolName}" requires human approval under this pattern's policy.`,
|
|
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.`,
|
|
375
592
|
status: "pending",
|
|
376
593
|
assignedToId,
|
|
377
594
|
requestedById: snapshot.userId || null,
|
|
@@ -381,6 +598,22 @@ ${maker.content}`);
|
|
|
381
598
|
}
|
|
382
599
|
});
|
|
383
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
|
+
});
|
|
384
617
|
await this.stateMachine.transition({
|
|
385
618
|
runId: snapshot.id,
|
|
386
619
|
to: "waiting_approval",
|
|
@@ -436,6 +669,7 @@ ${maker.content}`);
|
|
|
436
669
|
}
|
|
437
670
|
const roleBindings = asObject(run.roleBindingsSnapshot);
|
|
438
671
|
const makerSnapshots = asObject(run.makerHarnessSnapshot);
|
|
672
|
+
const approvalStatus = String(run.approvalStatus || "none");
|
|
439
673
|
return {
|
|
440
674
|
id: Number(run.id),
|
|
441
675
|
patternId,
|
|
@@ -450,7 +684,11 @@ ${maker.content}`);
|
|
|
450
684
|
verifierHarness: harnessFrom(run.verifierHarnessSnapshot),
|
|
451
685
|
repositoryKey: String(run.repositoryKey || ""),
|
|
452
686
|
actingOn: asStringArray(run.actingOn),
|
|
453
|
-
userId: Number(run.userId) || void 0
|
|
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
|
|
454
692
|
};
|
|
455
693
|
}
|
|
456
694
|
}
|