@nones/agentflow-core 0.7.0 → 0.8.0
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/package.json +1 -1
- package/src/client.ts +2 -0
- package/src/engine.ts +404 -67
- package/src/inspection.ts +290 -81
- package/src/protocol.ts +6 -1
- package/src/server.ts +16 -1
- package/src/store.ts +29 -9
- package/src/types.ts +14 -0
- package/src/validation.ts +8 -1
package/src/engine.ts
CHANGED
|
@@ -336,6 +336,12 @@ function stepKey(waveId: string, stepId: string): string {
|
|
|
336
336
|
/** Bounded, JSON-safe handler data. Handlers cannot smuggle unbounded values. */
|
|
337
337
|
const PLAN_STEP_DATA_MAX_BYTES = 64 * 1024;
|
|
338
338
|
|
|
339
|
+
/**
|
|
340
|
+
* Persisted adapter identity of a run whose plan has no agent steps and whose
|
|
341
|
+
* engine has no profiles. No provider adapter is looked up or created.
|
|
342
|
+
*/
|
|
343
|
+
const HANDLER_ONLY_ADAPTER = "none";
|
|
344
|
+
|
|
339
345
|
/** One declared non-agent step resolved against the trusted registry. */
|
|
340
346
|
interface BoundStep {
|
|
341
347
|
waveId: string;
|
|
@@ -345,6 +351,33 @@ interface BoundStep {
|
|
|
345
351
|
cwd: string;
|
|
346
352
|
}
|
|
347
353
|
|
|
354
|
+
/**
|
|
355
|
+
* Wave and step identities a plan binding may refer to. A start request supplies
|
|
356
|
+
* only its own plan; an append adds the persisted waves, agents and steps so a
|
|
357
|
+
* new step can depend on earlier work without rewriting it.
|
|
358
|
+
*/
|
|
359
|
+
interface PlanStepGraph {
|
|
360
|
+
/** Final wave order, including persisted waves for an append. */
|
|
361
|
+
waveOrder: ReadonlyMap<string, number>;
|
|
362
|
+
/** Persisted step keys (`waveId/stepId`) that already exist. */
|
|
363
|
+
persistedStepKeys: ReadonlySet<string>;
|
|
364
|
+
/** Agent IDs per wave, including appended agents. */
|
|
365
|
+
agentIdsByWave: ReadonlyMap<string, ReadonlySet<string>>;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function planStepGraph(plan: ValidatedPlan): PlanStepGraph {
|
|
369
|
+
return {
|
|
370
|
+
waveOrder: new Map(plan.waves.map((wave, index) => [wave.id, index])),
|
|
371
|
+
persistedStepKeys: new Set<string>(),
|
|
372
|
+
agentIdsByWave: new Map(
|
|
373
|
+
plan.waves.map((wave) => [
|
|
374
|
+
wave.id,
|
|
375
|
+
new Set(wave.agents.map((agent) => agent.id)),
|
|
376
|
+
]),
|
|
377
|
+
),
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
348
381
|
/**
|
|
349
382
|
* Registry handler name and registered version of one declared step. A plan
|
|
350
383
|
* without an explicit `handlerVersion` resolves the only registered revision of
|
|
@@ -391,14 +424,18 @@ function bindHandler(
|
|
|
391
424
|
*/
|
|
392
425
|
function bindPlanSteps(
|
|
393
426
|
plan: ValidatedPlan,
|
|
394
|
-
planMeta: RunPlanMetadata,
|
|
427
|
+
planMeta: RunPlanMetadata | undefined,
|
|
395
428
|
registry: ReadonlyMap<string, PlanHandler[]>,
|
|
429
|
+
graph: PlanStepGraph = planStepGraph(plan),
|
|
396
430
|
): { steps: BoundStep[]; blockers: string[] } {
|
|
397
|
-
const waveOrder =
|
|
431
|
+
const waveOrder = graph.waveOrder;
|
|
398
432
|
const declared = [...plan.scripts, ...plan.gates];
|
|
399
433
|
const declaredByKey = new Map<string, PlanHandlerStep>();
|
|
400
434
|
for (const step of declared) {
|
|
401
435
|
const key = stepKey(step.waveId, step.id);
|
|
436
|
+
if (graph.persistedStepKeys.has(key)) {
|
|
437
|
+
throw new AgentFlowError("validation", `plan step ${key} already exists`);
|
|
438
|
+
}
|
|
402
439
|
if (declaredByKey.has(key)) {
|
|
403
440
|
throw new AgentFlowError("validation", `duplicate plan step ${key}`);
|
|
404
441
|
}
|
|
@@ -438,7 +475,7 @@ function bindPlanSteps(
|
|
|
438
475
|
}
|
|
439
476
|
throw error;
|
|
440
477
|
}
|
|
441
|
-
const meta = planMeta
|
|
478
|
+
const meta = planMeta?.steps.find(
|
|
442
479
|
(descriptor) =>
|
|
443
480
|
descriptor.kind === step.kind &&
|
|
444
481
|
descriptor.waveId === waveId &&
|
|
@@ -455,23 +492,24 @@ function bindPlanSteps(
|
|
|
455
492
|
const parent = declaredByKey.get(key);
|
|
456
493
|
const parentIndex = waveOrder.get(ref.waveId)!;
|
|
457
494
|
const childIndex = waveOrder.get(waveId)!;
|
|
495
|
+
const persisted = graph.persistedStepKeys.has(key);
|
|
458
496
|
if (
|
|
459
497
|
parentIndex > childIndex ||
|
|
460
|
-
(parent === undefined && parentIndex === childIndex)
|
|
498
|
+
(parent === undefined && parentIndex === childIndex && !persisted)
|
|
461
499
|
) {
|
|
462
500
|
throw new AgentFlowError(
|
|
463
501
|
"validation",
|
|
464
502
|
`plan step ${step.id} cannot depend on ${key} before its admission`,
|
|
465
503
|
);
|
|
466
504
|
}
|
|
467
|
-
if (
|
|
468
|
-
|
|
469
|
-
!
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
505
|
+
if (parent === undefined && !persisted) {
|
|
506
|
+
const agentExists = graph.agentIdsByWave.get(ref.waveId)?.has(ref.id);
|
|
507
|
+
if (!agentExists) {
|
|
508
|
+
throw new AgentFlowError(
|
|
509
|
+
"validation",
|
|
510
|
+
`plan step ${step.id} has unknown dependency ${key}`,
|
|
511
|
+
);
|
|
512
|
+
}
|
|
475
513
|
}
|
|
476
514
|
return { waveId: ref.waveId, id: ref.id };
|
|
477
515
|
});
|
|
@@ -816,7 +854,7 @@ export class EngineImpl implements Engine {
|
|
|
816
854
|
);
|
|
817
855
|
}
|
|
818
856
|
const plan = validateRunPlan(rawPlan);
|
|
819
|
-
const runProfile = this.
|
|
857
|
+
const runProfile = this.selectRunProfile(request.profile, plan);
|
|
820
858
|
const planMeta = derivePlanMetadata(plan, strategy);
|
|
821
859
|
const binding = bindPlanSteps(plan, planMeta, this.handlers);
|
|
822
860
|
if (binding.blockers.length > 0) {
|
|
@@ -838,6 +876,12 @@ export class EngineImpl implements Engine {
|
|
|
838
876
|
const resolved: ResolvedAgent[] = [];
|
|
839
877
|
for (const wave of plan.waves) {
|
|
840
878
|
for (const spec of wave.agents) {
|
|
879
|
+
if (runProfile === undefined) {
|
|
880
|
+
throw new AgentFlowError(
|
|
881
|
+
"validation",
|
|
882
|
+
`agent ${spec.id} needs a configured profile`,
|
|
883
|
+
);
|
|
884
|
+
}
|
|
841
885
|
resolved.push(this.resolveAgent(spec, runProfile, strategy));
|
|
842
886
|
}
|
|
843
887
|
}
|
|
@@ -848,9 +892,11 @@ export class EngineImpl implements Engine {
|
|
|
848
892
|
id: runId,
|
|
849
893
|
strategy: strategy.id,
|
|
850
894
|
requestJson: toJson(request),
|
|
851
|
-
profileId: runProfile.id,
|
|
852
|
-
|
|
853
|
-
|
|
895
|
+
...(runProfile === undefined ? {} : { profileId: runProfile.id }),
|
|
896
|
+
...(runProfile === undefined
|
|
897
|
+
? {}
|
|
898
|
+
: { profileJson: toJson(runProfile) }),
|
|
899
|
+
adapter: runProfile?.adapter ?? HANDLER_ONLY_ADAPTER,
|
|
854
900
|
sealed: plan.sealed,
|
|
855
901
|
status: "idle",
|
|
856
902
|
createdAt: now,
|
|
@@ -871,6 +917,7 @@ export class EngineImpl implements Engine {
|
|
|
871
917
|
status: "pending",
|
|
872
918
|
createdAt: now,
|
|
873
919
|
phaseId: phaseByWave.get(wave.id),
|
|
920
|
+
hole: wave.hole === true,
|
|
874
921
|
});
|
|
875
922
|
index += 1;
|
|
876
923
|
for (const spec of wave.agents) {
|
|
@@ -929,8 +976,8 @@ export class EngineImpl implements Engine {
|
|
|
929
976
|
origin: { daemon: this.generation },
|
|
930
977
|
data: {
|
|
931
978
|
strategy: strategy.id,
|
|
932
|
-
profile: runProfile
|
|
933
|
-
adapter: runProfile
|
|
979
|
+
profile: runProfile?.id ?? null,
|
|
980
|
+
adapter: runProfile?.adapter ?? HANDLER_ONLY_ADAPTER,
|
|
934
981
|
sealed: plan.sealed,
|
|
935
982
|
waves: plan.waves.length,
|
|
936
983
|
agents: resolved.length,
|
|
@@ -999,11 +1046,11 @@ export class EngineImpl implements Engine {
|
|
|
999
1046
|
"append request.waves must be an array",
|
|
1000
1047
|
);
|
|
1001
1048
|
}
|
|
1002
|
-
const plan = validateRunPlan({
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1049
|
+
const plan = validateRunPlan({
|
|
1050
|
+
waves: request.waves,
|
|
1051
|
+
...(request.scripts === undefined ? {} : { scripts: request.scripts }),
|
|
1052
|
+
...(request.gates === undefined ? {} : { gates: request.gates }),
|
|
1053
|
+
});
|
|
1007
1054
|
const runStrategy = this.strategies.get(run.strategy);
|
|
1008
1055
|
if (runStrategy === undefined) {
|
|
1009
1056
|
throw new AgentFlowError(
|
|
@@ -1011,68 +1058,156 @@ export class EngineImpl implements Engine {
|
|
|
1011
1058
|
`run strategy ${run.strategy} is no longer registered`,
|
|
1012
1059
|
);
|
|
1013
1060
|
}
|
|
1061
|
+
const appendsAgents = plan.waves.some((wave) => wave.agents.length > 0);
|
|
1062
|
+
let runProfile: Profile | undefined;
|
|
1063
|
+
if (run.profile_json !== null) {
|
|
1064
|
+
runProfile = fromJson<Profile>(run.profile_json, this.profileFor());
|
|
1065
|
+
} else if (appendsAgents) {
|
|
1066
|
+
throw new AgentFlowError(
|
|
1067
|
+
"validation",
|
|
1068
|
+
`run ${id} has no profile; an agent step cannot be appended`,
|
|
1069
|
+
);
|
|
1070
|
+
}
|
|
1014
1071
|
|
|
1015
|
-
const
|
|
1016
|
-
|
|
1072
|
+
const waveRows = this.store.getWaveRows(id);
|
|
1073
|
+
const existingWaves = new Map(waveRows.map((wave) => [wave.id, wave]));
|
|
1074
|
+
const existingAgentRows = this.store.getAgentRows(id);
|
|
1075
|
+
const existingAgentIds = new Set(
|
|
1076
|
+
existingAgentRows.map((agent) => agent.id),
|
|
1017
1077
|
);
|
|
1018
|
-
const
|
|
1019
|
-
|
|
1078
|
+
const existingStepRows = this.store.getPlanStepRows(id);
|
|
1079
|
+
const persistedStepKeys = new Set(
|
|
1080
|
+
existingStepRows.map((row) => stepKey(row.wave_id, row.step_id)),
|
|
1020
1081
|
);
|
|
1021
|
-
let nextIndex =
|
|
1082
|
+
let nextIndex = waveRows.length;
|
|
1022
1083
|
const now = Date.now();
|
|
1023
1084
|
|
|
1024
1085
|
interface PendingWave {
|
|
1025
1086
|
wave: WaveSpec;
|
|
1026
|
-
existing:
|
|
1087
|
+
existing: (typeof waveRows)[number] | undefined;
|
|
1027
1088
|
index: number;
|
|
1028
1089
|
resolved: ResolvedAgent[];
|
|
1090
|
+
resolveHole: boolean;
|
|
1029
1091
|
}
|
|
1030
1092
|
const pending: PendingWave[] = [];
|
|
1031
1093
|
for (const wave of plan.waves) {
|
|
1032
1094
|
const existing = existingWaves.get(wave.id);
|
|
1033
1095
|
if (existing !== undefined) {
|
|
1034
|
-
if (
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1096
|
+
if (existing.hole === 1) {
|
|
1097
|
+
if (wave.hole === true) {
|
|
1098
|
+
throw new AgentFlowError(
|
|
1099
|
+
"conflict",
|
|
1100
|
+
`wave ${wave.id} is already a reserved hole`,
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
} else {
|
|
1104
|
+
if (wave.hole === true) {
|
|
1105
|
+
throw new AgentFlowError(
|
|
1106
|
+
"conflict",
|
|
1107
|
+
`wave ${wave.id} already exists and cannot become a reserved hole`,
|
|
1108
|
+
);
|
|
1109
|
+
}
|
|
1110
|
+
if (TERMINAL_WAVE.has(existing.status)) {
|
|
1111
|
+
throw new AgentFlowError(
|
|
1112
|
+
"conflict",
|
|
1113
|
+
`wave ${wave.id} is ${existing.status} and is immutable`,
|
|
1114
|
+
);
|
|
1115
|
+
}
|
|
1044
1116
|
}
|
|
1045
1117
|
}
|
|
1046
1118
|
for (const spec of wave.agents) {
|
|
1047
|
-
if (
|
|
1119
|
+
if (existingAgentIds.has(spec.id)) {
|
|
1048
1120
|
throw new AgentFlowError(
|
|
1049
1121
|
"validation",
|
|
1050
1122
|
`agent id ${spec.id} already exists in run ${id}`,
|
|
1051
1123
|
);
|
|
1052
1124
|
}
|
|
1053
|
-
|
|
1125
|
+
if (persistedStepKeys.has(stepKey(wave.id, spec.id))) {
|
|
1126
|
+
throw new AgentFlowError(
|
|
1127
|
+
"validation",
|
|
1128
|
+
`agent id ${spec.id} collides with a plan step in wave ${wave.id}`,
|
|
1129
|
+
);
|
|
1130
|
+
}
|
|
1131
|
+
existingAgentIds.add(spec.id);
|
|
1054
1132
|
}
|
|
1055
|
-
const resolved =
|
|
1056
|
-
|
|
1057
|
-
|
|
1133
|
+
const resolved =
|
|
1134
|
+
runProfile === undefined
|
|
1135
|
+
? []
|
|
1136
|
+
: wave.agents.map((spec) =>
|
|
1137
|
+
this.resolveAgent(spec, runProfile as Profile, runStrategy),
|
|
1138
|
+
);
|
|
1058
1139
|
pending.push({
|
|
1059
1140
|
wave,
|
|
1060
|
-
existing
|
|
1141
|
+
existing,
|
|
1061
1142
|
index: existing === undefined ? nextIndex : existing.idx,
|
|
1062
1143
|
resolved,
|
|
1144
|
+
resolveHole: existing?.hole === 1 && wave.hole !== true,
|
|
1063
1145
|
});
|
|
1064
1146
|
if (existing === undefined) nextIndex += 1;
|
|
1065
1147
|
}
|
|
1066
1148
|
|
|
1149
|
+
// A new handler step may only target a wave that has not started yet. A
|
|
1150
|
+
// running wave already executed its step phase, and a terminal wave is
|
|
1151
|
+
// immutable, so either target would strand the step without running it.
|
|
1152
|
+
for (const step of [...plan.scripts, ...plan.gates]) {
|
|
1153
|
+
const target = existingWaves.get(step.waveId);
|
|
1154
|
+
if (
|
|
1155
|
+
target !== undefined &&
|
|
1156
|
+
(target.status === "running" || TERMINAL_WAVE.has(target.status))
|
|
1157
|
+
) {
|
|
1158
|
+
throw new AgentFlowError(
|
|
1159
|
+
"conflict",
|
|
1160
|
+
`wave ${step.waveId} is ${target.status}; a handler step cannot be appended to it`,
|
|
1161
|
+
);
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
const waveOrder = new Map(waveRows.map((wave) => [wave.id, wave.idx]));
|
|
1166
|
+
const agentIdsByWave = new Map<string, Set<string>>();
|
|
1167
|
+
for (const wave of waveRows) {
|
|
1168
|
+
agentIdsByWave.set(wave.id, new Set<string>());
|
|
1169
|
+
}
|
|
1170
|
+
for (const agent of existingAgentRows) {
|
|
1171
|
+
agentIdsByWave.get(agent.wave_id)?.add(agent.id);
|
|
1172
|
+
}
|
|
1173
|
+
for (const entry of pending) {
|
|
1174
|
+
if (entry.existing === undefined) {
|
|
1175
|
+
waveOrder.set(entry.wave.id, entry.index);
|
|
1176
|
+
}
|
|
1177
|
+
const ids = agentIdsByWave.get(entry.wave.id) ?? new Set<string>();
|
|
1178
|
+
for (const spec of entry.wave.agents) ids.add(spec.id);
|
|
1179
|
+
agentIdsByWave.set(entry.wave.id, ids);
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
const binding = bindPlanSteps(plan, undefined, this.handlers, {
|
|
1183
|
+
waveOrder,
|
|
1184
|
+
persistedStepKeys,
|
|
1185
|
+
agentIdsByWave,
|
|
1186
|
+
});
|
|
1187
|
+
if (binding.blockers.length > 0) {
|
|
1188
|
+
throw new AgentFlowError("validation", binding.blockers.join(", "));
|
|
1189
|
+
}
|
|
1190
|
+
for (const entry of binding.steps) {
|
|
1191
|
+
if (agentIdsByWave.get(entry.waveId)?.has(entry.step.id)) {
|
|
1192
|
+
throw new AgentFlowError(
|
|
1193
|
+
"validation",
|
|
1194
|
+
`plan step ${stepKey(entry.waveId, entry.step.id)} collides with an agent id`,
|
|
1195
|
+
);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1067
1199
|
this.store.tx(() => {
|
|
1068
1200
|
for (const entry of pending) {
|
|
1069
|
-
if (
|
|
1201
|
+
if (entry.existing === undefined) {
|
|
1070
1202
|
this.store.insertWave(id, {
|
|
1071
1203
|
id: entry.wave.id,
|
|
1072
1204
|
index: entry.index,
|
|
1073
1205
|
status: "pending",
|
|
1074
1206
|
createdAt: now,
|
|
1207
|
+
hole: entry.wave.hole === true,
|
|
1075
1208
|
});
|
|
1209
|
+
} else if (entry.resolveHole) {
|
|
1210
|
+
this.store.updateWave(id, entry.wave.id, { hole: 0 });
|
|
1076
1211
|
}
|
|
1077
1212
|
for (const spec of entry.wave.agents) {
|
|
1078
1213
|
const match = entry.resolved.find(
|
|
@@ -1106,8 +1241,27 @@ export class EngineImpl implements Engine {
|
|
|
1106
1241
|
this.store.emit({
|
|
1107
1242
|
runId: id,
|
|
1108
1243
|
waveId: entry.wave.id,
|
|
1109
|
-
type: entry.existing ? "wave.
|
|
1110
|
-
data: {
|
|
1244
|
+
type: entry.existing === undefined ? "wave.added" : "wave.appended",
|
|
1245
|
+
data: {
|
|
1246
|
+
agents: entry.wave.agents.map((agent) => agent.id),
|
|
1247
|
+
...(entry.existing === undefined && entry.wave.hole === true
|
|
1248
|
+
? { hole: true }
|
|
1249
|
+
: {}),
|
|
1250
|
+
...(entry.resolveHole ? { resolved: true } : {}),
|
|
1251
|
+
},
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
for (const entry of binding.steps) {
|
|
1255
|
+
this.store.insertPlanStep(id, {
|
|
1256
|
+
waveId: entry.waveId,
|
|
1257
|
+
stepId: entry.step.id,
|
|
1258
|
+
kind: entry.step.kind,
|
|
1259
|
+
handler: entry.step.handler,
|
|
1260
|
+
handlerVersion: entry.step.handlerVersion as string,
|
|
1261
|
+
cwd: entry.cwd,
|
|
1262
|
+
dependsOn: entry.dependsOn,
|
|
1263
|
+
attemptId: newId("attempt"),
|
|
1264
|
+
startedAt: now,
|
|
1111
1265
|
});
|
|
1112
1266
|
}
|
|
1113
1267
|
this.store.emit({
|
|
@@ -1119,6 +1273,8 @@ export class EngineImpl implements Engine {
|
|
|
1119
1273
|
(total, wave) => total + wave.agents.length,
|
|
1120
1274
|
0,
|
|
1121
1275
|
),
|
|
1276
|
+
scripts: plan.scripts.length,
|
|
1277
|
+
gates: plan.gates.length,
|
|
1122
1278
|
},
|
|
1123
1279
|
});
|
|
1124
1280
|
});
|
|
@@ -1267,6 +1423,15 @@ export class EngineImpl implements Engine {
|
|
|
1267
1423
|
if (run.status === "cancelled") {
|
|
1268
1424
|
throw new AgentFlowError("conflict", `run ${id} is cancelled`);
|
|
1269
1425
|
}
|
|
1426
|
+
const openHoles = this.store
|
|
1427
|
+
.getWaveRows(id)
|
|
1428
|
+
.filter((wave) => wave.hole === 1);
|
|
1429
|
+
if (openHoles.length > 0) {
|
|
1430
|
+
throw new AgentFlowError(
|
|
1431
|
+
"conflict",
|
|
1432
|
+
`run ${id} has open reserved wave${openHoles.length === 1 ? "" : "s"} ${openHoles.map((wave) => wave.id).join(", ")}; append them before sealing`,
|
|
1433
|
+
);
|
|
1434
|
+
}
|
|
1270
1435
|
if (run.sealed === 0) {
|
|
1271
1436
|
this.store.tx(() => {
|
|
1272
1437
|
this.store.updateRun(id, {
|
|
@@ -1792,11 +1957,17 @@ export class EngineImpl implements Engine {
|
|
|
1792
1957
|
const planDigest = digest("plan", plan);
|
|
1793
1958
|
const runProfile =
|
|
1794
1959
|
run.profile_json === null
|
|
1795
|
-
? this.
|
|
1960
|
+
? this.selectRunProfile(undefined, plan)
|
|
1796
1961
|
: fromJson<Profile>(run.profile_json, this.profileFor());
|
|
1797
1962
|
const resolved: ResolvedAgent[] = [];
|
|
1798
1963
|
for (const wave of plan.waves) {
|
|
1799
1964
|
for (const spec of wave.agents) {
|
|
1965
|
+
if (runProfile === undefined) {
|
|
1966
|
+
throw new AgentFlowError(
|
|
1967
|
+
"validation",
|
|
1968
|
+
`agent ${spec.id} needs a configured profile`,
|
|
1969
|
+
);
|
|
1970
|
+
}
|
|
1800
1971
|
resolved.push(this.resolveAgent(spec, runProfile, strategy));
|
|
1801
1972
|
}
|
|
1802
1973
|
}
|
|
@@ -1872,9 +2043,11 @@ export class EngineImpl implements Engine {
|
|
|
1872
2043
|
id: successorId,
|
|
1873
2044
|
strategy: strategy.id,
|
|
1874
2045
|
requestJson: toJson({ ...request, input: plan, apply: true }),
|
|
1875
|
-
profileId: runProfile.id,
|
|
1876
|
-
|
|
1877
|
-
|
|
2046
|
+
...(runProfile === undefined ? {} : { profileId: runProfile.id }),
|
|
2047
|
+
...(runProfile === undefined
|
|
2048
|
+
? {}
|
|
2049
|
+
: { profileJson: toJson(runProfile) }),
|
|
2050
|
+
adapter: runProfile?.adapter ?? HANDLER_ONLY_ADAPTER,
|
|
1878
2051
|
sealed: plan.sealed === true,
|
|
1879
2052
|
status: "idle",
|
|
1880
2053
|
createdAt: now,
|
|
@@ -1896,6 +2069,7 @@ export class EngineImpl implements Engine {
|
|
|
1896
2069
|
status: "pending",
|
|
1897
2070
|
createdAt: now,
|
|
1898
2071
|
phaseId: meta.steps.find((step) => step.waveId === wave.id)?.phaseId,
|
|
2072
|
+
hole: wave.hole === true,
|
|
1899
2073
|
});
|
|
1900
2074
|
index += 1;
|
|
1901
2075
|
for (const spec of wave.agents) {
|
|
@@ -1977,8 +2151,8 @@ export class EngineImpl implements Engine {
|
|
|
1977
2151
|
origin: { daemon: this.generation, actionId: effectiveId, revision },
|
|
1978
2152
|
data: {
|
|
1979
2153
|
strategy: strategy.id,
|
|
1980
|
-
profile: runProfile
|
|
1981
|
-
adapter: runProfile
|
|
2154
|
+
profile: runProfile?.id ?? null,
|
|
2155
|
+
adapter: runProfile?.adapter ?? HANDLER_ONLY_ADAPTER,
|
|
1982
2156
|
sealed: plan.sealed === true,
|
|
1983
2157
|
waves: plan.waves.length,
|
|
1984
2158
|
agents: resolved.length,
|
|
@@ -2302,6 +2476,28 @@ export class EngineImpl implements Engine {
|
|
|
2302
2476
|
return;
|
|
2303
2477
|
}
|
|
2304
2478
|
|
|
2479
|
+
// A reserved hole holds itself and every later wave until `append`
|
|
2480
|
+
// resolves it with the same wave id. Waiting here is deliberate: nothing
|
|
2481
|
+
// is admitted and no loop reschedules, so an open hole cannot busy-spin.
|
|
2482
|
+
if (actionable.hole === 1) {
|
|
2483
|
+
const current = this.store.getRunRow(runId);
|
|
2484
|
+
if (current !== undefined && current.status === "running") {
|
|
2485
|
+
this.store.tx(() => {
|
|
2486
|
+
this.store.updateRun(runId, {
|
|
2487
|
+
status: "idle",
|
|
2488
|
+
error: null,
|
|
2489
|
+
updated_at: Date.now(),
|
|
2490
|
+
});
|
|
2491
|
+
this.store.emit({
|
|
2492
|
+
runId,
|
|
2493
|
+
type: "run.idle",
|
|
2494
|
+
data: { reason: "hole_waiting", waveId: actionable.id },
|
|
2495
|
+
});
|
|
2496
|
+
});
|
|
2497
|
+
}
|
|
2498
|
+
return;
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2305
2501
|
const earlier = waves.filter((wave) => wave.idx < actionable.idx);
|
|
2306
2502
|
if (
|
|
2307
2503
|
earlier.some(
|
|
@@ -2495,7 +2691,13 @@ export class EngineImpl implements Engine {
|
|
|
2495
2691
|
runId,
|
|
2496
2692
|
waveId,
|
|
2497
2693
|
type: `step.failed`,
|
|
2498
|
-
data: {
|
|
2694
|
+
data: {
|
|
2695
|
+
waveId,
|
|
2696
|
+
stepId,
|
|
2697
|
+
kind: row.kind,
|
|
2698
|
+
attemptId: row.attempt_id,
|
|
2699
|
+
error: failure.message,
|
|
2700
|
+
},
|
|
2499
2701
|
});
|
|
2500
2702
|
});
|
|
2501
2703
|
}
|
|
@@ -2538,6 +2740,7 @@ export class EngineImpl implements Engine {
|
|
|
2538
2740
|
waveId,
|
|
2539
2741
|
type: "step.started",
|
|
2540
2742
|
data: {
|
|
2743
|
+
waveId,
|
|
2541
2744
|
stepId,
|
|
2542
2745
|
kind: row.kind,
|
|
2543
2746
|
handler: row.handler,
|
|
@@ -2604,7 +2807,12 @@ export class EngineImpl implements Engine {
|
|
|
2604
2807
|
runId,
|
|
2605
2808
|
waveId,
|
|
2606
2809
|
type: "step.cancelled",
|
|
2607
|
-
data: {
|
|
2810
|
+
data: {
|
|
2811
|
+
waveId,
|
|
2812
|
+
stepId,
|
|
2813
|
+
kind: row.kind,
|
|
2814
|
+
attemptId: row.attempt_id,
|
|
2815
|
+
},
|
|
2608
2816
|
});
|
|
2609
2817
|
});
|
|
2610
2818
|
return;
|
|
@@ -2620,7 +2828,13 @@ export class EngineImpl implements Engine {
|
|
|
2620
2828
|
runId,
|
|
2621
2829
|
waveId,
|
|
2622
2830
|
type: "step.failed",
|
|
2623
|
-
data: {
|
|
2831
|
+
data: {
|
|
2832
|
+
waveId,
|
|
2833
|
+
stepId,
|
|
2834
|
+
kind: row.kind,
|
|
2835
|
+
attemptId: row.attempt_id,
|
|
2836
|
+
error: (error as Error).message,
|
|
2837
|
+
},
|
|
2624
2838
|
});
|
|
2625
2839
|
});
|
|
2626
2840
|
return;
|
|
@@ -2660,10 +2874,12 @@ export class EngineImpl implements Engine {
|
|
|
2660
2874
|
waveId,
|
|
2661
2875
|
type: `step.${normalized.status}`,
|
|
2662
2876
|
data: {
|
|
2877
|
+
waveId,
|
|
2663
2878
|
stepId,
|
|
2664
2879
|
kind: row.kind,
|
|
2665
2880
|
handler: row.handler,
|
|
2666
2881
|
handlerVersion: row.handler_version,
|
|
2882
|
+
attemptId,
|
|
2667
2883
|
...(normalized.passed === undefined
|
|
2668
2884
|
? {}
|
|
2669
2885
|
: { passed: normalized.passed }),
|
|
@@ -2692,7 +2908,13 @@ export class EngineImpl implements Engine {
|
|
|
2692
2908
|
runId,
|
|
2693
2909
|
waveId,
|
|
2694
2910
|
type: "step.blocked",
|
|
2695
|
-
data: {
|
|
2911
|
+
data: {
|
|
2912
|
+
waveId,
|
|
2913
|
+
stepId: row.step_id,
|
|
2914
|
+
kind: row.kind,
|
|
2915
|
+
attemptId: row.attempt_id,
|
|
2916
|
+
reason,
|
|
2917
|
+
},
|
|
2696
2918
|
});
|
|
2697
2919
|
});
|
|
2698
2920
|
}
|
|
@@ -2719,7 +2941,13 @@ export class EngineImpl implements Engine {
|
|
|
2719
2941
|
runId,
|
|
2720
2942
|
waveId,
|
|
2721
2943
|
type: "step.interrupted",
|
|
2722
|
-
data: {
|
|
2944
|
+
data: {
|
|
2945
|
+
waveId,
|
|
2946
|
+
stepId,
|
|
2947
|
+
kind: row.kind,
|
|
2948
|
+
attemptId: row.attempt_id,
|
|
2949
|
+
reason,
|
|
2950
|
+
},
|
|
2723
2951
|
});
|
|
2724
2952
|
this.store.appendRecovery(runId, {
|
|
2725
2953
|
daemon: this.generation,
|
|
@@ -2786,12 +3014,14 @@ export class EngineImpl implements Engine {
|
|
|
2786
3014
|
}
|
|
2787
3015
|
|
|
2788
3016
|
private async executeWave(runId: string, waveId: string): Promise<void> {
|
|
3017
|
+
if (this.store.getWaveRow(runId, waveId)?.hole === 1) return;
|
|
2789
3018
|
while (!this.closed && !this.draining()) {
|
|
2790
3019
|
const run = this.store.getRunRow(runId);
|
|
2791
3020
|
if (
|
|
2792
3021
|
run === undefined ||
|
|
2793
3022
|
TERMINAL_RUN.has(run.status) ||
|
|
2794
|
-
run.status === "paused"
|
|
3023
|
+
run.status === "paused" ||
|
|
3024
|
+
run.stopped === 1
|
|
2795
3025
|
) {
|
|
2796
3026
|
return;
|
|
2797
3027
|
}
|
|
@@ -3133,14 +3363,18 @@ export class EngineImpl implements Engine {
|
|
|
3133
3363
|
});
|
|
3134
3364
|
}
|
|
3135
3365
|
|
|
3366
|
+
// `next_prompt` is the durable strategy follow-up checkpoint. Read it as a
|
|
3367
|
+
// provisional prompt only; it is consumed inside the admission transaction
|
|
3368
|
+
// so a stop or crash before admission never loses the follow-up.
|
|
3136
3369
|
let prompt = agent.next_prompt ?? spec.prompt;
|
|
3137
|
-
if (agent.next_prompt !== null) {
|
|
3138
|
-
this.store.updateAgent(runId, agentId, { next_prompt: null });
|
|
3139
|
-
}
|
|
3140
3370
|
let rateLimitAttempts = 0;
|
|
3141
3371
|
|
|
3142
3372
|
while (!this.closed) {
|
|
3143
|
-
if (this.draining()
|
|
3373
|
+
if (this.draining()) return;
|
|
3374
|
+
if (this.isAdmissionBlocked(runId, agentId)) {
|
|
3375
|
+
this.requeueStrandedFollowUp(runId, agentId);
|
|
3376
|
+
return;
|
|
3377
|
+
}
|
|
3144
3378
|
const currentAgent = this.store.getAgentRow(runId, agentId);
|
|
3145
3379
|
if (currentAgent === undefined) return;
|
|
3146
3380
|
const currentRun = this.store.getRunRow(runId);
|
|
@@ -3167,6 +3401,7 @@ export class EngineImpl implements Engine {
|
|
|
3167
3401
|
reason: "quota group is missing",
|
|
3168
3402
|
});
|
|
3169
3403
|
} else if (ceiling.reason === "stopped") {
|
|
3404
|
+
this.requeueStrandedFollowUp(runId, agentId);
|
|
3170
3405
|
return;
|
|
3171
3406
|
}
|
|
3172
3407
|
return;
|
|
@@ -3174,8 +3409,8 @@ export class EngineImpl implements Engine {
|
|
|
3174
3409
|
|
|
3175
3410
|
const turnId = newId("turn");
|
|
3176
3411
|
const turnNumber = currentAgent.turns + 1;
|
|
3177
|
-
const
|
|
3178
|
-
|
|
3412
|
+
const admissionAgent = this.store.getAgentRow(runId, agentId);
|
|
3413
|
+
const session = admissionAgent?.session_id ?? sessionId;
|
|
3179
3414
|
if (session === null) {
|
|
3180
3415
|
this.store.releaseQuota(group);
|
|
3181
3416
|
this.markAgent(currentAgent, "interrupted", {
|
|
@@ -3183,6 +3418,9 @@ export class EngineImpl implements Engine {
|
|
|
3183
3418
|
});
|
|
3184
3419
|
return;
|
|
3185
3420
|
}
|
|
3421
|
+
// The durable follow-up wins over the in-memory prompt and is cleared in
|
|
3422
|
+
// this same transaction as the next admission.
|
|
3423
|
+
const turnPrompt = admissionAgent?.next_prompt ?? prompt;
|
|
3186
3424
|
|
|
3187
3425
|
try {
|
|
3188
3426
|
this.store.tx(() => {
|
|
@@ -3192,7 +3430,7 @@ export class EngineImpl implements Engine {
|
|
|
3192
3430
|
waveId: currentAgent.wave_id,
|
|
3193
3431
|
agentId,
|
|
3194
3432
|
number: turnNumber,
|
|
3195
|
-
prompt,
|
|
3433
|
+
prompt: turnPrompt,
|
|
3196
3434
|
sessionId: session,
|
|
3197
3435
|
createdAt: Date.now(),
|
|
3198
3436
|
});
|
|
@@ -3224,6 +3462,7 @@ export class EngineImpl implements Engine {
|
|
|
3224
3462
|
this.store.releaseQuota(group);
|
|
3225
3463
|
throw error;
|
|
3226
3464
|
}
|
|
3465
|
+
prompt = turnPrompt;
|
|
3227
3466
|
|
|
3228
3467
|
const controller = new AbortController();
|
|
3229
3468
|
this.turnHandles.set(turnId, { runId, controller });
|
|
@@ -4532,6 +4771,23 @@ export class EngineImpl implements Engine {
|
|
|
4532
4771
|
return profile;
|
|
4533
4772
|
}
|
|
4534
4773
|
|
|
4774
|
+
/**
|
|
4775
|
+
* Profile for a plan's run. A plan with no agent step can run without any
|
|
4776
|
+
* configured profile: no adapter is resolved, so a handler-only workflow is
|
|
4777
|
+
* executable on a daemon that carries no provider configuration. Any named
|
|
4778
|
+
* profile, or any agent step, still requires a configured profile.
|
|
4779
|
+
*/
|
|
4780
|
+
private selectRunProfile(
|
|
4781
|
+
explicit: string | undefined,
|
|
4782
|
+
plan: ValidatedPlan,
|
|
4783
|
+
): Profile | undefined {
|
|
4784
|
+
const named = explicit ?? plan.profile;
|
|
4785
|
+
if (named !== undefined) return this.profileFor(named);
|
|
4786
|
+
if (this.profiles.size > 0) return this.profileFor();
|
|
4787
|
+
if (plan.waves.every((wave) => wave.agents.length === 0)) return undefined;
|
|
4788
|
+
return this.profileFor();
|
|
4789
|
+
}
|
|
4790
|
+
|
|
4535
4791
|
private requireRun(id: string): NonNullable<ReturnType<Store["getRunRow"]>> {
|
|
4536
4792
|
const run = this.store.getRunRow(id);
|
|
4537
4793
|
if (run === undefined) {
|
|
@@ -4588,6 +4844,73 @@ export class EngineImpl implements Engine {
|
|
|
4588
4844
|
return agent.status === "cancelled";
|
|
4589
4845
|
}
|
|
4590
4846
|
|
|
4847
|
+
/**
|
|
4848
|
+
* Requeue an agent whose last durable turn succeeded while its strategy
|
|
4849
|
+
* follow-up is already persisted. The provider effect is known (the turn and
|
|
4850
|
+
* its receipt settled), so this is a durable checkpoint, not a replay. The
|
|
4851
|
+
* prompt and provider session stay persisted and `next_prompt` is consumed
|
|
4852
|
+
* only by the next admission transaction. A persisted operator stop is never
|
|
4853
|
+
* cleared here, so it still gates admission through the scheduler.
|
|
4854
|
+
*/
|
|
4855
|
+
private requeueDurableFollowUp(
|
|
4856
|
+
runId: string,
|
|
4857
|
+
agent: {
|
|
4858
|
+
id: string;
|
|
4859
|
+
wave_id: string;
|
|
4860
|
+
session_id: string | null;
|
|
4861
|
+
next_prompt: string | null;
|
|
4862
|
+
},
|
|
4863
|
+
reason: string,
|
|
4864
|
+
eventType: "agent.recovered" | "agent.requeued",
|
|
4865
|
+
): boolean {
|
|
4866
|
+
if (agent.session_id === null) return false;
|
|
4867
|
+
const nextPrompt = agent.next_prompt;
|
|
4868
|
+
if (nextPrompt === null || nextPrompt.trim() === "") return false;
|
|
4869
|
+
const last = this.store.getTurnRows(runId, agent.id).at(-1);
|
|
4870
|
+
if (last === undefined || last.state !== "succeeded") return false;
|
|
4871
|
+
// An admitted or running turn has an unknown effect: never requeue over it.
|
|
4872
|
+
if (
|
|
4873
|
+
this.store
|
|
4874
|
+
.listInflightTurnRows(runId)
|
|
4875
|
+
.some((turn) => turn.agent_id === agent.id)
|
|
4876
|
+
)
|
|
4877
|
+
return false;
|
|
4878
|
+
this.store.tx(() => {
|
|
4879
|
+
this.store.updateAgent(runId, agent.id, {
|
|
4880
|
+
status: "pending",
|
|
4881
|
+
error: null,
|
|
4882
|
+
started_at: null,
|
|
4883
|
+
finished_at: null,
|
|
4884
|
+
});
|
|
4885
|
+
this.store.emit({
|
|
4886
|
+
runId,
|
|
4887
|
+
waveId: agent.wave_id,
|
|
4888
|
+
agentId: agent.id,
|
|
4889
|
+
type: eventType,
|
|
4890
|
+
data: { action: "requeued_follow_up", reason },
|
|
4891
|
+
});
|
|
4892
|
+
});
|
|
4893
|
+
return true;
|
|
4894
|
+
}
|
|
4895
|
+
|
|
4896
|
+
/**
|
|
4897
|
+
* The executeAgent owner found a persisted follow-up while a durable
|
|
4898
|
+
* operator stop blocked the next admission. Requeue that known checkpoint so
|
|
4899
|
+
* the `running` row is not stranded, without touching the stop itself.
|
|
4900
|
+
*/
|
|
4901
|
+
private requeueStrandedFollowUp(runId: string, agentId: string): void {
|
|
4902
|
+
if (this.closed) return;
|
|
4903
|
+
if (this.store.getRunRow(runId)?.stopped !== 1) return;
|
|
4904
|
+
const agent = this.store.getAgentRow(runId, agentId);
|
|
4905
|
+
if (agent === undefined || agent.status !== "running") return;
|
|
4906
|
+
this.requeueDurableFollowUp(
|
|
4907
|
+
runId,
|
|
4908
|
+
agent,
|
|
4909
|
+
"operator stopped the run between turns",
|
|
4910
|
+
"agent.requeued",
|
|
4911
|
+
);
|
|
4912
|
+
}
|
|
4913
|
+
|
|
4591
4914
|
private getRunAbort(runId: string): AbortController {
|
|
4592
4915
|
let controller = this.runAborts.get(runId);
|
|
4593
4916
|
if (controller === undefined) {
|
|
@@ -4723,6 +5046,20 @@ export class EngineImpl implements Engine {
|
|
|
4723
5046
|
continue;
|
|
4724
5047
|
}
|
|
4725
5048
|
if (last.state === "succeeded") {
|
|
5049
|
+
// The strategy decision was made durable as a nonempty `next_prompt`
|
|
5050
|
+
// only after the native turn succeeded, so this is a known checkpoint
|
|
5051
|
+
// and the follow-up may be admitted. Without that checkpoint the
|
|
5052
|
+
// decision is unknown: keep the hold and never resubmit the turn.
|
|
5053
|
+
if (
|
|
5054
|
+
this.requeueDurableFollowUp(
|
|
5055
|
+
agent.run_id,
|
|
5056
|
+
agent,
|
|
5057
|
+
"turn succeeded with a persisted strategy follow-up",
|
|
5058
|
+
"agent.recovered",
|
|
5059
|
+
)
|
|
5060
|
+
) {
|
|
5061
|
+
continue;
|
|
5062
|
+
}
|
|
4726
5063
|
// The native turn succeeded but the strategy decision was not made
|
|
4727
5064
|
// durable before the restart. Do not submit another native turn.
|
|
4728
5065
|
this.pauseRecoveredAgent(
|