@nones/agentflow-core 0.7.0 → 0.7.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/engine.ts +99 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nones/agentflow-core",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "AgentFlow contracts, durable store, scheduler, quotas and local JSON RPC",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
package/src/engine.ts CHANGED
@@ -2791,7 +2791,8 @@ export class EngineImpl implements Engine {
2791
2791
  if (
2792
2792
  run === undefined ||
2793
2793
  TERMINAL_RUN.has(run.status) ||
2794
- run.status === "paused"
2794
+ run.status === "paused" ||
2795
+ run.stopped === 1
2795
2796
  ) {
2796
2797
  return;
2797
2798
  }
@@ -3133,14 +3134,18 @@ export class EngineImpl implements Engine {
3133
3134
  });
3134
3135
  }
3135
3136
 
3137
+ // `next_prompt` is the durable strategy follow-up checkpoint. Read it as a
3138
+ // provisional prompt only; it is consumed inside the admission transaction
3139
+ // so a stop or crash before admission never loses the follow-up.
3136
3140
  let prompt = agent.next_prompt ?? spec.prompt;
3137
- if (agent.next_prompt !== null) {
3138
- this.store.updateAgent(runId, agentId, { next_prompt: null });
3139
- }
3140
3141
  let rateLimitAttempts = 0;
3141
3142
 
3142
3143
  while (!this.closed) {
3143
- if (this.draining() || this.isAdmissionBlocked(runId, agentId)) return;
3144
+ if (this.draining()) return;
3145
+ if (this.isAdmissionBlocked(runId, agentId)) {
3146
+ this.requeueStrandedFollowUp(runId, agentId);
3147
+ return;
3148
+ }
3144
3149
  const currentAgent = this.store.getAgentRow(runId, agentId);
3145
3150
  if (currentAgent === undefined) return;
3146
3151
  const currentRun = this.store.getRunRow(runId);
@@ -3167,6 +3172,7 @@ export class EngineImpl implements Engine {
3167
3172
  reason: "quota group is missing",
3168
3173
  });
3169
3174
  } else if (ceiling.reason === "stopped") {
3175
+ this.requeueStrandedFollowUp(runId, agentId);
3170
3176
  return;
3171
3177
  }
3172
3178
  return;
@@ -3174,8 +3180,8 @@ export class EngineImpl implements Engine {
3174
3180
 
3175
3181
  const turnId = newId("turn");
3176
3182
  const turnNumber = currentAgent.turns + 1;
3177
- const session =
3178
- this.store.getAgentRow(runId, agentId)?.session_id ?? sessionId;
3183
+ const admissionAgent = this.store.getAgentRow(runId, agentId);
3184
+ const session = admissionAgent?.session_id ?? sessionId;
3179
3185
  if (session === null) {
3180
3186
  this.store.releaseQuota(group);
3181
3187
  this.markAgent(currentAgent, "interrupted", {
@@ -3183,6 +3189,9 @@ export class EngineImpl implements Engine {
3183
3189
  });
3184
3190
  return;
3185
3191
  }
3192
+ // The durable follow-up wins over the in-memory prompt and is cleared in
3193
+ // this same transaction as the next admission.
3194
+ const turnPrompt = admissionAgent?.next_prompt ?? prompt;
3186
3195
 
3187
3196
  try {
3188
3197
  this.store.tx(() => {
@@ -3192,7 +3201,7 @@ export class EngineImpl implements Engine {
3192
3201
  waveId: currentAgent.wave_id,
3193
3202
  agentId,
3194
3203
  number: turnNumber,
3195
- prompt,
3204
+ prompt: turnPrompt,
3196
3205
  sessionId: session,
3197
3206
  createdAt: Date.now(),
3198
3207
  });
@@ -3224,6 +3233,7 @@ export class EngineImpl implements Engine {
3224
3233
  this.store.releaseQuota(group);
3225
3234
  throw error;
3226
3235
  }
3236
+ prompt = turnPrompt;
3227
3237
 
3228
3238
  const controller = new AbortController();
3229
3239
  this.turnHandles.set(turnId, { runId, controller });
@@ -4588,6 +4598,73 @@ export class EngineImpl implements Engine {
4588
4598
  return agent.status === "cancelled";
4589
4599
  }
4590
4600
 
4601
+ /**
4602
+ * Requeue an agent whose last durable turn succeeded while its strategy
4603
+ * follow-up is already persisted. The provider effect is known (the turn and
4604
+ * its receipt settled), so this is a durable checkpoint, not a replay. The
4605
+ * prompt and provider session stay persisted and `next_prompt` is consumed
4606
+ * only by the next admission transaction. A persisted operator stop is never
4607
+ * cleared here, so it still gates admission through the scheduler.
4608
+ */
4609
+ private requeueDurableFollowUp(
4610
+ runId: string,
4611
+ agent: {
4612
+ id: string;
4613
+ wave_id: string;
4614
+ session_id: string | null;
4615
+ next_prompt: string | null;
4616
+ },
4617
+ reason: string,
4618
+ eventType: "agent.recovered" | "agent.requeued",
4619
+ ): boolean {
4620
+ if (agent.session_id === null) return false;
4621
+ const nextPrompt = agent.next_prompt;
4622
+ if (nextPrompt === null || nextPrompt.trim() === "") return false;
4623
+ const last = this.store.getTurnRows(runId, agent.id).at(-1);
4624
+ if (last === undefined || last.state !== "succeeded") return false;
4625
+ // An admitted or running turn has an unknown effect: never requeue over it.
4626
+ if (
4627
+ this.store
4628
+ .listInflightTurnRows(runId)
4629
+ .some((turn) => turn.agent_id === agent.id)
4630
+ )
4631
+ return false;
4632
+ this.store.tx(() => {
4633
+ this.store.updateAgent(runId, agent.id, {
4634
+ status: "pending",
4635
+ error: null,
4636
+ started_at: null,
4637
+ finished_at: null,
4638
+ });
4639
+ this.store.emit({
4640
+ runId,
4641
+ waveId: agent.wave_id,
4642
+ agentId: agent.id,
4643
+ type: eventType,
4644
+ data: { action: "requeued_follow_up", reason },
4645
+ });
4646
+ });
4647
+ return true;
4648
+ }
4649
+
4650
+ /**
4651
+ * The executeAgent owner found a persisted follow-up while a durable
4652
+ * operator stop blocked the next admission. Requeue that known checkpoint so
4653
+ * the `running` row is not stranded, without touching the stop itself.
4654
+ */
4655
+ private requeueStrandedFollowUp(runId: string, agentId: string): void {
4656
+ if (this.closed) return;
4657
+ if (this.store.getRunRow(runId)?.stopped !== 1) return;
4658
+ const agent = this.store.getAgentRow(runId, agentId);
4659
+ if (agent === undefined || agent.status !== "running") return;
4660
+ this.requeueDurableFollowUp(
4661
+ runId,
4662
+ agent,
4663
+ "operator stopped the run between turns",
4664
+ "agent.requeued",
4665
+ );
4666
+ }
4667
+
4591
4668
  private getRunAbort(runId: string): AbortController {
4592
4669
  let controller = this.runAborts.get(runId);
4593
4670
  if (controller === undefined) {
@@ -4723,6 +4800,20 @@ export class EngineImpl implements Engine {
4723
4800
  continue;
4724
4801
  }
4725
4802
  if (last.state === "succeeded") {
4803
+ // The strategy decision was made durable as a nonempty `next_prompt`
4804
+ // only after the native turn succeeded, so this is a known checkpoint
4805
+ // and the follow-up may be admitted. Without that checkpoint the
4806
+ // decision is unknown: keep the hold and never resubmit the turn.
4807
+ if (
4808
+ this.requeueDurableFollowUp(
4809
+ agent.run_id,
4810
+ agent,
4811
+ "turn succeeded with a persisted strategy follow-up",
4812
+ "agent.recovered",
4813
+ )
4814
+ ) {
4815
+ continue;
4816
+ }
4726
4817
  // The native turn succeeded but the strategy decision was not made
4727
4818
  // durable before the restart. Do not submit another native turn.
4728
4819
  this.pauseRecoveredAgent(