@nones/agentflow-core 0.7.1 → 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 +305 -59
- 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/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -121,6 +121,8 @@ class ClientImpl implements Client {
|
|
|
121
121
|
return this.call<RunSnapshot>("append", {
|
|
122
122
|
id,
|
|
123
123
|
waves: request.waves,
|
|
124
|
+
...(request.scripts === undefined ? {} : { scripts: request.scripts }),
|
|
125
|
+
...(request.gates === undefined ? {} : { gates: request.gates }),
|
|
124
126
|
});
|
|
125
127
|
}
|
|
126
128
|
|
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,6 +3014,7 @@ 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 (
|
|
@@ -4542,6 +4771,23 @@ export class EngineImpl implements Engine {
|
|
|
4542
4771
|
return profile;
|
|
4543
4772
|
}
|
|
4544
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
|
+
|
|
4545
4791
|
private requireRun(id: string): NonNullable<ReturnType<Store["getRunRow"]>> {
|
|
4546
4792
|
const run = this.store.getRunRow(id);
|
|
4547
4793
|
if (run === undefined) {
|
package/src/inspection.ts
CHANGED
|
@@ -221,6 +221,10 @@ export interface LogRecord {
|
|
|
221
221
|
type: string;
|
|
222
222
|
waveId?: string;
|
|
223
223
|
agentId?: string;
|
|
224
|
+
/** Non-agent step selected from the event payload, when recorded. */
|
|
225
|
+
stepId?: string;
|
|
226
|
+
/** Immutable step attempt selected from the event payload, when recorded. */
|
|
227
|
+
attemptId?: string;
|
|
224
228
|
data?: unknown;
|
|
225
229
|
/** Explicit data states so a missing body never reads as success. */
|
|
226
230
|
state: "included" | "redacted" | "truncated" | "omitted";
|
|
@@ -231,15 +235,34 @@ export interface LogRecord {
|
|
|
231
235
|
summary?: string;
|
|
232
236
|
}
|
|
233
237
|
|
|
238
|
+
export type LogsScope = "run" | "invocation" | "step";
|
|
239
|
+
|
|
240
|
+
/** The durable identity behind `logs <attempt-id>`. */
|
|
241
|
+
export interface StepAttemptScope {
|
|
242
|
+
attemptId: string;
|
|
243
|
+
waveId: string;
|
|
244
|
+
stepId: string;
|
|
245
|
+
kind: StepKind;
|
|
246
|
+
status: string;
|
|
247
|
+
}
|
|
248
|
+
|
|
234
249
|
export interface LogsPage {
|
|
235
250
|
snapshotVersion: number;
|
|
236
251
|
readOnly: true;
|
|
252
|
+
/** How the reference was resolved: a run, a script invocation or a step attempt. */
|
|
253
|
+
scope: LogsScope;
|
|
237
254
|
runId: string;
|
|
255
|
+
/** All runs covered by an invocation page; one run for a run or attempt page. */
|
|
256
|
+
runIds: string[];
|
|
257
|
+
/** Present when the page was resolved from a script invocation ID. */
|
|
258
|
+
invocationId?: string;
|
|
259
|
+
/** Present when the page was resolved from a non-agent step attempt ID. */
|
|
260
|
+
step?: StepAttemptScope;
|
|
238
261
|
limit: number;
|
|
239
262
|
truncated: boolean;
|
|
240
263
|
nextCursor: string | null;
|
|
241
264
|
highWaterMark: number;
|
|
242
|
-
/** True when the cursor is past everything persisted. */
|
|
265
|
+
/** True when the cursor is past everything persisted in this scope. */
|
|
243
266
|
atEnd: boolean;
|
|
244
267
|
records: LogRecord[];
|
|
245
268
|
/** Non-fatal projection problems (undecodable payloads, dropped bytes). */
|
|
@@ -270,6 +293,33 @@ interface CursorShape {
|
|
|
270
293
|
seq?: number;
|
|
271
294
|
}
|
|
272
295
|
|
|
296
|
+
/** The resolved reference behind one `logs` page. */
|
|
297
|
+
interface LogScope {
|
|
298
|
+
mode: LogsScope;
|
|
299
|
+
/** The exact reference the caller supplied, echoed when no run id exists. */
|
|
300
|
+
selector: string;
|
|
301
|
+
runIds: string[];
|
|
302
|
+
invocationId?: string;
|
|
303
|
+
step?: StepAttemptScope;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* A non-agent step event belongs to one attempt only when its payload carries
|
|
308
|
+
* that attempt identity. Older events without the field stay untagged instead
|
|
309
|
+
* of being attributed to an attempt that may not have produced them.
|
|
310
|
+
*/
|
|
311
|
+
function attemptEventMatches(
|
|
312
|
+
row: { wave_id: string | null; data_json: string | null },
|
|
313
|
+
step: StepAttemptScope,
|
|
314
|
+
): boolean {
|
|
315
|
+
if (row.wave_id !== null && row.wave_id !== step.waveId) return false;
|
|
316
|
+
const data = parseJson(row.data_json);
|
|
317
|
+
if (!isPlainObject(data)) return false;
|
|
318
|
+
if (typeof data.attemptId !== "string") return false;
|
|
319
|
+
if (data.attemptId !== step.attemptId) return false;
|
|
320
|
+
return typeof data.stepId !== "string" || data.stepId === step.stepId;
|
|
321
|
+
}
|
|
322
|
+
|
|
273
323
|
/**
|
|
274
324
|
* Encode a resumable cursor. Human-readable mode prints it too, so machines can
|
|
275
325
|
* always resume from the last delivered record.
|
|
@@ -573,6 +623,13 @@ export class InspectionReader {
|
|
|
573
623
|
);
|
|
574
624
|
}
|
|
575
625
|
|
|
626
|
+
/** Additive columns are probed so an older, un-migrated file stays readable. */
|
|
627
|
+
private hasColumn(table: string, column: string): boolean {
|
|
628
|
+
return this.all<{ name: string }>(`PRAGMA table_info("${table}")`).some(
|
|
629
|
+
(row) => row.name === column,
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
|
|
576
633
|
private all<T extends DbRow>(
|
|
577
634
|
sql: string,
|
|
578
635
|
...params: SQLQueryBindings[]
|
|
@@ -1348,99 +1405,248 @@ export class InspectionReader {
|
|
|
1348
1405
|
// ------------------------------------------------------------------ logs
|
|
1349
1406
|
|
|
1350
1407
|
/**
|
|
1351
|
-
* Bounded, redacted diagnostic records for one run
|
|
1352
|
-
*
|
|
1353
|
-
*
|
|
1408
|
+
* Bounded, redacted diagnostic records for one run, one script invocation or
|
|
1409
|
+
* one non-agent step attempt. `after` is an opaque cursor wrapping the stable
|
|
1410
|
+
* global event `seq`, so a truncated page always resumes from the last
|
|
1411
|
+
* delivered (or skipped) record without duplicates or gaps.
|
|
1412
|
+
*
|
|
1413
|
+
* Legacy resolution is unchanged: an exact run id wins, then a display name.
|
|
1414
|
+
* Only when neither matches does the reference resolve to a script invocation
|
|
1415
|
+
* (every run it started, ordered by the global event sequence) or to a
|
|
1416
|
+
* `plan_steps.attempt_id` (only events that carry that attempt identity).
|
|
1354
1417
|
*/
|
|
1355
1418
|
readRunLogs(runRef: string, options: ReadLogsOptions = {}): LogsPage {
|
|
1356
1419
|
const limit = boundedLimit(options.limit);
|
|
1357
1420
|
const cursor = decodeCursor(options.after, "logs");
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1421
|
+
const scope = this.resolveLogScope(runRef);
|
|
1422
|
+
return this.consistent(() =>
|
|
1423
|
+
this.readLogsPage(scope, options, limit, cursor),
|
|
1424
|
+
);
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
private resolveLogScope(selector: string): LogScope {
|
|
1428
|
+
if (typeof selector !== "string" || selector.trim() === "") {
|
|
1429
|
+
throw new AgentFlowError(
|
|
1430
|
+
"validation",
|
|
1431
|
+
"a run id, name, invocation id or step attempt id is required",
|
|
1432
|
+
);
|
|
1433
|
+
}
|
|
1434
|
+
const byId = this.one<{ id: string }>(
|
|
1435
|
+
"SELECT id FROM runs WHERE id = ?",
|
|
1436
|
+
selector,
|
|
1437
|
+
);
|
|
1438
|
+
if (byId !== undefined) {
|
|
1439
|
+
return { mode: "run", selector, runIds: [byId.id] };
|
|
1440
|
+
}
|
|
1441
|
+
if (this.hasColumn("runs", "name")) {
|
|
1442
|
+
const byName = this.one<{ id: string }>(
|
|
1443
|
+
"SELECT id FROM runs WHERE name = ? ORDER BY created_at DESC, id DESC LIMIT 1",
|
|
1444
|
+
selector,
|
|
1445
|
+
);
|
|
1446
|
+
if (byName !== undefined) {
|
|
1447
|
+
return { mode: "run", selector, runIds: [byName.id] };
|
|
1374
1448
|
}
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1449
|
+
}
|
|
1450
|
+
if (this.hasColumn("runs", "invocation_id")) {
|
|
1451
|
+
const runs = this.all<{ id: string }>(
|
|
1452
|
+
"SELECT id FROM runs WHERE invocation_id = ? ORDER BY created_at ASC, id ASC",
|
|
1453
|
+
selector,
|
|
1454
|
+
);
|
|
1455
|
+
if (runs.length > 0) {
|
|
1456
|
+
return {
|
|
1457
|
+
mode: "invocation",
|
|
1458
|
+
selector,
|
|
1459
|
+
runIds: runs.map((run) => run.id),
|
|
1460
|
+
invocationId: selector,
|
|
1461
|
+
};
|
|
1379
1462
|
}
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
wave_id: string
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1463
|
+
}
|
|
1464
|
+
if (this.hasTable("plan_steps")) {
|
|
1465
|
+
const step = this.one<{
|
|
1466
|
+
run_id: string;
|
|
1467
|
+
wave_id: string;
|
|
1468
|
+
step_id: string;
|
|
1469
|
+
kind: StepKind;
|
|
1470
|
+
status: string;
|
|
1471
|
+
attempt_id: string;
|
|
1388
1472
|
}>(
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
...params,
|
|
1392
|
-
limit + 1,
|
|
1473
|
+
"SELECT run_id, wave_id, step_id, kind, status, attempt_id FROM plan_steps WHERE attempt_id = ? LIMIT 1",
|
|
1474
|
+
selector,
|
|
1393
1475
|
);
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
!levelMatches(record.level, options.level)
|
|
1408
|
-
) {
|
|
1409
|
-
continue;
|
|
1410
|
-
}
|
|
1411
|
-
const size = byteLength(record);
|
|
1412
|
-
if (records.length > 0 && bytes + size > INSPECTION_PAGE_MAX_BYTES) {
|
|
1413
|
-
capped = row.seq;
|
|
1414
|
-
break;
|
|
1415
|
-
}
|
|
1416
|
-
records.push(record);
|
|
1417
|
-
bytes += size;
|
|
1476
|
+
if (step !== undefined) {
|
|
1477
|
+
return {
|
|
1478
|
+
mode: "step",
|
|
1479
|
+
selector,
|
|
1480
|
+
runIds: [step.run_id],
|
|
1481
|
+
step: {
|
|
1482
|
+
attemptId: step.attempt_id,
|
|
1483
|
+
waveId: step.wave_id,
|
|
1484
|
+
stepId: step.step_id,
|
|
1485
|
+
kind: step.kind,
|
|
1486
|
+
status: step.status,
|
|
1487
|
+
},
|
|
1488
|
+
};
|
|
1418
1489
|
}
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1490
|
+
}
|
|
1491
|
+
throw new AgentFlowError(
|
|
1492
|
+
"not_found",
|
|
1493
|
+
`no run, invocation or step attempt matches ${JSON.stringify(selector)}`,
|
|
1494
|
+
{ selector },
|
|
1495
|
+
);
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
/** One SQL predicate for every run covered by the resolved scope. */
|
|
1499
|
+
private scopeFilter(scope: LogScope): {
|
|
1500
|
+
sql: string;
|
|
1501
|
+
params: SQLQueryBindings[];
|
|
1502
|
+
} {
|
|
1503
|
+
if (scope.mode === "invocation") {
|
|
1424
1504
|
return {
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
runId,
|
|
1428
|
-
limit,
|
|
1429
|
-
truncated: delivered !== undefined && capped !== undefined,
|
|
1430
|
-
nextCursor:
|
|
1431
|
-
delivered === undefined
|
|
1432
|
-
? null
|
|
1433
|
-
: encodeCursor({ k: "logs", seq: nextSeq }),
|
|
1434
|
-
highWaterMark: highWater,
|
|
1435
|
-
atEnd: !more && nextSeq >= highWater,
|
|
1436
|
-
records,
|
|
1437
|
-
projectionErrors,
|
|
1505
|
+
sql: "run_id IN (SELECT id FROM runs WHERE invocation_id = ?)",
|
|
1506
|
+
params: [scope.invocationId as string],
|
|
1438
1507
|
};
|
|
1439
|
-
}
|
|
1508
|
+
}
|
|
1509
|
+
return { sql: "run_id = ?", params: [scope.runIds[0] as string] };
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
private readLogsPage(
|
|
1513
|
+
scope: LogScope,
|
|
1514
|
+
options: ReadLogsOptions,
|
|
1515
|
+
limit: number,
|
|
1516
|
+
cursor: CursorShape | undefined,
|
|
1517
|
+
): LogsPage {
|
|
1518
|
+
const filter = this.scopeFilter(scope);
|
|
1519
|
+
const highWater =
|
|
1520
|
+
this.one<{ seq: number | null }>(
|
|
1521
|
+
`SELECT MAX(seq) AS seq FROM events WHERE ${filter.sql}`,
|
|
1522
|
+
...filter.params,
|
|
1523
|
+
)?.seq ?? 0;
|
|
1524
|
+
|
|
1525
|
+
const stepMismatch =
|
|
1526
|
+
scope.mode === "step" &&
|
|
1527
|
+
scope.step !== undefined &&
|
|
1528
|
+
options.step !== undefined &&
|
|
1529
|
+
options.step !== scope.step.stepId;
|
|
1530
|
+
|
|
1531
|
+
const params: SQLQueryBindings[] = [...filter.params, cursor?.seq ?? 0];
|
|
1532
|
+
let where = `${filter.sql} AND seq > ?`;
|
|
1533
|
+
if (scope.mode === "step" && scope.step !== undefined) {
|
|
1534
|
+
// The attempt's own wave bounds the scan; the payload then decides the
|
|
1535
|
+
// exact attempt identity.
|
|
1536
|
+
where += " AND wave_id = ?";
|
|
1537
|
+
params.push(scope.step.waveId);
|
|
1538
|
+
}
|
|
1539
|
+
if (options.agent !== undefined) {
|
|
1540
|
+
where +=
|
|
1541
|
+
" AND (agent_id = ? OR agent_id IN (SELECT id FROM agents WHERE " +
|
|
1542
|
+
`${filter.sql} AND step_key = ?))`;
|
|
1543
|
+
params.push(options.agent, ...filter.params, options.agent);
|
|
1544
|
+
}
|
|
1545
|
+
if (options.step !== undefined && scope.mode !== "step") {
|
|
1546
|
+
where +=
|
|
1547
|
+
" AND agent_id IN (SELECT id FROM agents WHERE " +
|
|
1548
|
+
`${filter.sql} AND (id = ? OR step_key = ?))`;
|
|
1549
|
+
params.push(...filter.params, options.step, options.step);
|
|
1550
|
+
}
|
|
1551
|
+
if (options.errorsOnly === true) where += ` AND ${ERROR_EVENT_FILTER}`;
|
|
1552
|
+
|
|
1553
|
+
const rows = stepMismatch
|
|
1554
|
+
? []
|
|
1555
|
+
: this.all<{
|
|
1556
|
+
run_id: string;
|
|
1557
|
+
seq: number;
|
|
1558
|
+
type: string;
|
|
1559
|
+
wave_id: string | null;
|
|
1560
|
+
agent_id: string | null;
|
|
1561
|
+
data_json: string | null;
|
|
1562
|
+
at: number;
|
|
1563
|
+
}>(
|
|
1564
|
+
`SELECT run_id, seq, type, wave_id, agent_id, data_json, at FROM events
|
|
1565
|
+
WHERE ${where} ORDER BY seq ASC LIMIT ?`,
|
|
1566
|
+
...params,
|
|
1567
|
+
limit + 1,
|
|
1568
|
+
);
|
|
1569
|
+
|
|
1570
|
+
const projectionErrors: string[] = [];
|
|
1571
|
+
const records: LogRecord[] = [];
|
|
1572
|
+
let bytes = 0;
|
|
1573
|
+
let capped: number | undefined;
|
|
1574
|
+
let lastSeen: number | undefined;
|
|
1575
|
+
let consumed = 0;
|
|
1576
|
+
let moreRows = false;
|
|
1577
|
+
// Rows are read in ascending `seq`. A row is either delivered, explicitly
|
|
1578
|
+
// skipped (another attempt or a level below the filter) or ends the page.
|
|
1579
|
+
// The cursor always advances past every inspected row, so an attempt page
|
|
1580
|
+
// with sparse matching events cannot rescan the same prefix forever.
|
|
1581
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
1582
|
+
const row = rows[index] as (typeof rows)[number];
|
|
1583
|
+
lastSeen = row.seq;
|
|
1584
|
+
if (records.length >= limit) {
|
|
1585
|
+
moreRows = true;
|
|
1586
|
+
break;
|
|
1587
|
+
}
|
|
1588
|
+
if (
|
|
1589
|
+
scope.mode === "step" &&
|
|
1590
|
+
scope.step !== undefined &&
|
|
1591
|
+
!attemptEventMatches(row, scope.step)
|
|
1592
|
+
) {
|
|
1593
|
+
consumed = index + 1;
|
|
1594
|
+
continue;
|
|
1595
|
+
}
|
|
1596
|
+
const record = this.logRecord(row, projectionErrors);
|
|
1597
|
+
if (scope.mode === "step" && scope.step !== undefined) {
|
|
1598
|
+
record.waveId ??= scope.step.waveId;
|
|
1599
|
+
record.stepId ??= scope.step.stepId;
|
|
1600
|
+
record.attemptId ??= scope.step.attemptId;
|
|
1601
|
+
}
|
|
1602
|
+
if (
|
|
1603
|
+
options.level !== undefined &&
|
|
1604
|
+
!levelMatches(record.level, options.level)
|
|
1605
|
+
) {
|
|
1606
|
+
consumed = index + 1;
|
|
1607
|
+
continue;
|
|
1608
|
+
}
|
|
1609
|
+
const size = byteLength(record);
|
|
1610
|
+
if (records.length > 0 && bytes + size > INSPECTION_PAGE_MAX_BYTES) {
|
|
1611
|
+
capped = row.seq;
|
|
1612
|
+
moreRows = true;
|
|
1613
|
+
break;
|
|
1614
|
+
}
|
|
1615
|
+
records.push(record);
|
|
1616
|
+
bytes += size;
|
|
1617
|
+
consumed = index + 1;
|
|
1618
|
+
}
|
|
1619
|
+
const delivered = records[records.length - 1];
|
|
1620
|
+
const nextSeq = delivered?.seq ?? lastSeen ?? cursor?.seq ?? 0;
|
|
1621
|
+
const more = moreRows || rows.length > limit;
|
|
1622
|
+
return {
|
|
1623
|
+
snapshotVersion: INSPECTION_SNAPSHOT_VERSION,
|
|
1624
|
+
readOnly: true,
|
|
1625
|
+
scope: scope.mode,
|
|
1626
|
+
runId: scope.runIds[0] ?? scope.selector,
|
|
1627
|
+
runIds: [...scope.runIds],
|
|
1628
|
+
...(scope.invocationId === undefined
|
|
1629
|
+
? {}
|
|
1630
|
+
: { invocationId: scope.invocationId }),
|
|
1631
|
+
...(scope.step === undefined ? {} : { step: scope.step }),
|
|
1632
|
+
limit,
|
|
1633
|
+
// `truncated` keeps its meaning: the byte cap ended a page that had a
|
|
1634
|
+
// deliverable record. A page that only skipped rows is not truncated.
|
|
1635
|
+
truncated: delivered !== undefined && capped !== undefined,
|
|
1636
|
+
nextCursor:
|
|
1637
|
+
delivered !== undefined || more
|
|
1638
|
+
? encodeCursor({ k: "logs", seq: nextSeq })
|
|
1639
|
+
: null,
|
|
1640
|
+
highWaterMark: highWater,
|
|
1641
|
+
atEnd: !more,
|
|
1642
|
+
records,
|
|
1643
|
+
projectionErrors,
|
|
1644
|
+
};
|
|
1440
1645
|
}
|
|
1441
1646
|
|
|
1442
1647
|
private logRecord(
|
|
1443
1648
|
row: {
|
|
1649
|
+
run_id: string;
|
|
1444
1650
|
seq: number;
|
|
1445
1651
|
type: string;
|
|
1446
1652
|
wave_id: string | null;
|
|
@@ -1448,13 +1654,12 @@ export class InspectionReader {
|
|
|
1448
1654
|
data_json: string | null;
|
|
1449
1655
|
at: number;
|
|
1450
1656
|
},
|
|
1451
|
-
runId: string,
|
|
1452
1657
|
projectionErrors: string[],
|
|
1453
1658
|
): LogRecord {
|
|
1454
1659
|
const level = levelOf(row.type);
|
|
1455
1660
|
const record: LogRecord = {
|
|
1456
1661
|
seq: row.seq,
|
|
1457
|
-
runId,
|
|
1662
|
+
runId: row.run_id,
|
|
1458
1663
|
at: row.at,
|
|
1459
1664
|
level,
|
|
1460
1665
|
type: row.type,
|
|
@@ -1474,6 +1679,10 @@ export class InspectionReader {
|
|
|
1474
1679
|
record.omitted = "undecodable payload";
|
|
1475
1680
|
return record;
|
|
1476
1681
|
}
|
|
1682
|
+
if (isPlainObject(data)) {
|
|
1683
|
+
if (typeof data.stepId === "string") record.stepId = data.stepId;
|
|
1684
|
+
if (typeof data.attemptId === "string") record.attemptId = data.attemptId;
|
|
1685
|
+
}
|
|
1477
1686
|
const redacted = redactValue(data);
|
|
1478
1687
|
record.data = redacted.value;
|
|
1479
1688
|
record.state = redacted.state;
|
package/src/protocol.ts
CHANGED
|
@@ -64,7 +64,12 @@ export interface RpcActionArgs {
|
|
|
64
64
|
runs: Record<string, never>;
|
|
65
65
|
status: { id: string };
|
|
66
66
|
events: { id: string; after?: number; agentId?: string };
|
|
67
|
-
append: {
|
|
67
|
+
append: {
|
|
68
|
+
id: string;
|
|
69
|
+
waves: AppendRequest["waves"];
|
|
70
|
+
scripts?: AppendRequest["scripts"];
|
|
71
|
+
gates?: AppendRequest["gates"];
|
|
72
|
+
};
|
|
68
73
|
send: { id: string; agentId: string } & SendOptions;
|
|
69
74
|
seal: { id: string };
|
|
70
75
|
cancel: { id: string };
|
package/src/server.ts
CHANGED
|
@@ -563,6 +563,8 @@ async function dispatch(
|
|
|
563
563
|
const typed = args as RpcActionArgs["append"];
|
|
564
564
|
return engine.append(typed.id, {
|
|
565
565
|
waves: typed.waves,
|
|
566
|
+
...(typed.scripts === undefined ? {} : { scripts: typed.scripts }),
|
|
567
|
+
...(typed.gates === undefined ? {} : { gates: typed.gates }),
|
|
566
568
|
} as AppendRequest);
|
|
567
569
|
}
|
|
568
570
|
case "send": {
|
|
@@ -666,7 +668,20 @@ function validateActionArgs(action: string, args: unknown): unknown {
|
|
|
666
668
|
if (!Array.isArray(object.waves)) {
|
|
667
669
|
throw new AgentFlowError("ipc", "append args.waves must be an array");
|
|
668
670
|
}
|
|
669
|
-
|
|
671
|
+
for (const key of ["scripts", "gates"] as const) {
|
|
672
|
+
if (object[key] !== undefined && !Array.isArray(object[key])) {
|
|
673
|
+
throw new AgentFlowError(
|
|
674
|
+
"ipc",
|
|
675
|
+
`append args.${key} must be an array`,
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return {
|
|
680
|
+
id: requireString(object, "id"),
|
|
681
|
+
waves: object.waves,
|
|
682
|
+
...(object.scripts === undefined ? {} : { scripts: object.scripts }),
|
|
683
|
+
...(object.gates === undefined ? {} : { gates: object.gates }),
|
|
684
|
+
};
|
|
670
685
|
}
|
|
671
686
|
case "send": {
|
|
672
687
|
const delivery = object.delivery;
|
package/src/store.ts
CHANGED
|
@@ -72,6 +72,8 @@ interface WaveRow {
|
|
|
72
72
|
started_at: number | null;
|
|
73
73
|
finished_at: number | null;
|
|
74
74
|
phase_id: string | null;
|
|
75
|
+
/** 1 while the wave is a reserved hole (schema v6). */
|
|
76
|
+
hole: number;
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
interface AgentRow {
|
|
@@ -285,6 +287,7 @@ export interface WaveInsert {
|
|
|
285
287
|
status: WaveStatus;
|
|
286
288
|
createdAt: number;
|
|
287
289
|
phaseId?: string;
|
|
290
|
+
hole?: boolean;
|
|
288
291
|
}
|
|
289
292
|
|
|
290
293
|
export interface AgentInsert {
|
|
@@ -355,7 +358,7 @@ export interface QuotaRateLimitResult {
|
|
|
355
358
|
* Highest schema version this build understands. Bump it and append a step to
|
|
356
359
|
* `MIGRATIONS` for every schema change; never edit a released step in place.
|
|
357
360
|
*/
|
|
358
|
-
export const SCHEMA_VERSION =
|
|
361
|
+
export const SCHEMA_VERSION = 6;
|
|
359
362
|
|
|
360
363
|
/**
|
|
361
364
|
* Frozen version-0 schema: the exact shape released before `user_version`
|
|
@@ -507,6 +510,7 @@ const WAVE_PATCH_KEYS = [
|
|
|
507
510
|
"started_at",
|
|
508
511
|
"finished_at",
|
|
509
512
|
"phase_id",
|
|
513
|
+
"hole",
|
|
510
514
|
] as const;
|
|
511
515
|
const AGENT_PATCH_KEYS = [
|
|
512
516
|
"status",
|
|
@@ -754,6 +758,15 @@ const MIGRATIONS: readonly MigrationStep[] = [
|
|
|
754
758
|
}
|
|
755
759
|
},
|
|
756
760
|
},
|
|
761
|
+
{
|
|
762
|
+
version: 6,
|
|
763
|
+
// Reserved wave positions. A legacy wave has no hole, so every existing
|
|
764
|
+
// plan keeps its order and behaves exactly as before.
|
|
765
|
+
up(db) {
|
|
766
|
+
if (!tableExists(db, "waves")) return;
|
|
767
|
+
addColumnIfMissing(db, "waves", "hole", "INTEGER NOT NULL DEFAULT 0");
|
|
768
|
+
},
|
|
769
|
+
},
|
|
757
770
|
];
|
|
758
771
|
|
|
759
772
|
interface VersionRow {
|
|
@@ -967,8 +980,8 @@ export class Store {
|
|
|
967
980
|
insertWave(runId: string, wave: WaveInsert): void {
|
|
968
981
|
this.db
|
|
969
982
|
.query(
|
|
970
|
-
`INSERT INTO waves (run_id, id, idx, status, created_at, phase_id)
|
|
971
|
-
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
983
|
+
`INSERT INTO waves (run_id, id, idx, status, created_at, phase_id, hole)
|
|
984
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
972
985
|
)
|
|
973
986
|
.run(
|
|
974
987
|
runId,
|
|
@@ -977,6 +990,7 @@ export class Store {
|
|
|
977
990
|
wave.status,
|
|
978
991
|
wave.createdAt,
|
|
979
992
|
wave.phaseId ?? null,
|
|
993
|
+
wave.hole === true ? 1 : 0,
|
|
980
994
|
);
|
|
981
995
|
}
|
|
982
996
|
|
|
@@ -1876,12 +1890,18 @@ export class Store {
|
|
|
1876
1890
|
const agents = this.getAgentRows(id);
|
|
1877
1891
|
const artifacts = this.getArtifactRows(id);
|
|
1878
1892
|
const agentSnapshots = agents.map(agentRowToSnapshot);
|
|
1879
|
-
const
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1893
|
+
const stepResults = this.getPlanStepRows(id).map(planStepRowToResult);
|
|
1894
|
+
const waveSnapshots: WaveSnapshot[] = waves.map((wave) => {
|
|
1895
|
+
const steps = stepResults.filter((step) => step.waveId === wave.id);
|
|
1896
|
+
return {
|
|
1897
|
+
id: wave.id,
|
|
1898
|
+
index: wave.idx,
|
|
1899
|
+
status: wave.status,
|
|
1900
|
+
agents: agentSnapshots.filter((agent) => agent.waveId === wave.id),
|
|
1901
|
+
...(wave.hole === 1 ? { hole: true } : {}),
|
|
1902
|
+
...(steps.length === 0 ? {} : { steps }),
|
|
1903
|
+
};
|
|
1904
|
+
});
|
|
1885
1905
|
return {
|
|
1886
1906
|
id: run.id,
|
|
1887
1907
|
status: run.status,
|
package/src/types.ts
CHANGED
|
@@ -52,6 +52,12 @@ export interface AgentSpec {
|
|
|
52
52
|
export interface WaveSpec {
|
|
53
53
|
id: string;
|
|
54
54
|
agents: AgentSpec[];
|
|
55
|
+
/**
|
|
56
|
+
* Reserve this wave position without admitting work. A hole is persisted in
|
|
57
|
+
* order, holds every later wave, and is filled by a later `append` with the
|
|
58
|
+
* same wave id. A hole cannot declare agents.
|
|
59
|
+
*/
|
|
60
|
+
hole?: boolean;
|
|
55
61
|
}
|
|
56
62
|
|
|
57
63
|
export type ArtifactType = "json" | "text" | "file";
|
|
@@ -464,6 +470,10 @@ export interface WaveSnapshot {
|
|
|
464
470
|
index: number;
|
|
465
471
|
status: WaveStatus;
|
|
466
472
|
agents: AgentSnapshot[];
|
|
473
|
+
/** True while this wave is a reserved hole that has not been filled. */
|
|
474
|
+
hole?: boolean;
|
|
475
|
+
/** Persisted script and gate results belonging to this wave. */
|
|
476
|
+
steps?: PlanStepResult[];
|
|
467
477
|
}
|
|
468
478
|
|
|
469
479
|
export interface ArtifactSnapshot {
|
|
@@ -551,6 +561,10 @@ export interface EventOptions {
|
|
|
551
561
|
|
|
552
562
|
export interface AppendRequest {
|
|
553
563
|
waves: WaveSpec[];
|
|
564
|
+
/** Trusted script steps appended with these waves, keyed by their wave. */
|
|
565
|
+
scripts?: PlanHandlerStep[];
|
|
566
|
+
/** Trusted verification gates appended with these waves, keyed by their wave. */
|
|
567
|
+
gates?: PlanHandlerStep[];
|
|
554
568
|
}
|
|
555
569
|
|
|
556
570
|
export interface SendOptions {
|
package/src/validation.ts
CHANGED
|
@@ -106,6 +106,9 @@ export function validateRunPlan(input: unknown): ValidatedPlan {
|
|
|
106
106
|
|
|
107
107
|
const profile = optionalString(input.profile, "plan.profile");
|
|
108
108
|
const sealed = optionalBoolean(input.sealed, "plan.sealed") ?? false;
|
|
109
|
+
if (sealed && waves.some((wave) => wave.hole === true)) {
|
|
110
|
+
fail("plan.sealed", "a sealed plan cannot contain a reserved hole");
|
|
111
|
+
}
|
|
109
112
|
|
|
110
113
|
const scripts = validateHandlerSteps(
|
|
111
114
|
input.scripts,
|
|
@@ -253,7 +256,11 @@ function validateWave(
|
|
|
253
256
|
const agents = input.agents.map((rawAgent, agentIndex) =>
|
|
254
257
|
validateAgent(rawAgent, index, agentIndex, agentIds),
|
|
255
258
|
);
|
|
256
|
-
|
|
259
|
+
const hole = optionalBoolean(input.hole, `plan.waves[${index}].hole`);
|
|
260
|
+
if (hole === true && agents.length > 0) {
|
|
261
|
+
fail(`plan.waves[${index}].hole`, "a reserved hole cannot contain agents");
|
|
262
|
+
}
|
|
263
|
+
return { id, agents, ...(hole === true ? { hole: true } : {}) };
|
|
257
264
|
}
|
|
258
265
|
|
|
259
266
|
function validateAgent(
|