operon-agents-core 0.1.0-alpha.22 → 0.1.0-alpha.24

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 CHANGED
@@ -5,3 +5,16 @@ Internal engine for `operon-agents`.
5
5
  Application code should import from `operon-agents`. This package intentionally
6
6
  contains lower-level runtime, loop, permission, storage, and capability modules
7
7
  that may change as the engine evolves.
8
+
9
+ ## Execution recovery
10
+
11
+ `Runner.run` and `runStream` reconcile abandoned lifecycle frames before a Session's
12
+ first fresh run, under its run lock and any configured `SessionLock`. Durable
13
+ approval/input pauses remain resumable through `Runner.resume`. `Session.open`
14
+ alone never closes unfinished turns: opening is not proof that another executor died.
15
+
16
+ Hosts can call `recoverOrphanedTurns` without opening a Session, or
17
+ `closeOrphanedTurns` to receive the committed repair events for broadcasting.
18
+ Both require exclusive execution ownership. For stores shared across processes,
19
+ provide a distributed lock to Runner or acquire a host-managed lease before takeover.
20
+ Recovery closes orphan frames; it does not retry tools with unknown side effects.
@@ -22,4 +22,5 @@ export type { ContextBreakdown, ContextSlice, ContextInjectionSlice, ComputeCont
22
22
  export { Runner } from "./runner.ts";
23
23
  export type { AgentInput, ResumeInput, RunInterruption, RunnerConfig, RunOptions, RunResult, RunStatus, } from "./runner.ts";
24
24
  export * from "./workflow/index.ts";
25
+ export { closeOrphanedTurns, recoverOrphanedTurns } from "./lifecycle-recovery.ts";
25
26
  //# sourceMappingURL=index.d.ts.map
@@ -16,4 +16,5 @@ export { Runner } from "./runner.js";
16
16
  // Workflow engine (the deterministic subagent-orchestration runtime) + its manager
17
17
  // and snapshot/journal layers, exposed so upper layers can integrate programmatically.
18
18
  export * from "./workflow/index.js";
19
+ export { closeOrphanedTurns, recoverOrphanedTurns } from "./lifecycle-recovery.js";
19
20
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,15 @@
1
+ import { type AgentEvent } from "../events/index.ts";
2
+ import type { AgentRecord, SessionStore } from "../store/index.ts";
3
+ /**
4
+ * Close lifecycle frames abandoned by a previous executor, preserving durable pauses.
5
+ * The host MUST establish exclusive execution ownership before calling this function.
6
+ * It does not acquire a lease: an open turn alone is never evidence of a dead executor.
7
+ * Repeated calls under that ownership are idempotent, including after a partial write.
8
+ * A supplied log must be read under the same ownership and include every shard's tail.
9
+ * Returns committed records; a raw store does not broadcast them to live observers.
10
+ */
11
+ export declare function recoverOrphanedTurns(sessionId: string, store: SessionStore, records?: readonly AgentRecord[]): Promise<AgentRecord[]>;
12
+ /** Host-facing recovery with projected events to broadcast after durable writes. Same
13
+ * exclusive-ownership requirement as recoverOrphanedTurns; no Session needs to be opened. */
14
+ export declare function closeOrphanedTurns(sessionId: string, store: SessionStore): Promise<readonly AgentEvent[]>;
15
+ //# sourceMappingURL=lifecycle-recovery.d.ts.map
@@ -0,0 +1,44 @@
1
+ import { SessionProjection, readSessionTails } from "../events/projection.js";
2
+ import { agentEventFromRecord, newAgentEventId } from "../events/index.js";
3
+ import { INTERRUPTION_STATE_KEY } from "../loop/interruption.js";
4
+ /**
5
+ * Close lifecycle frames abandoned by a previous executor, preserving durable pauses.
6
+ * The host MUST establish exclusive execution ownership before calling this function.
7
+ * It does not acquire a lease: an open turn alone is never evidence of a dead executor.
8
+ * Repeated calls under that ownership are idempotent, including after a partial write.
9
+ * A supplied log must be read under the same ownership and include every shard's tail.
10
+ * Returns committed records; a raw store does not broadcast them to live observers.
11
+ */
12
+ export async function recoverOrphanedTurns(sessionId, store, records) {
13
+ if (await store.getState(INTERRUPTION_STATE_KEY) !== null)
14
+ return [];
15
+ // Reuse the core checkpoint/lifecycle reducer, without history or live subscriptions.
16
+ const frames = new SessionProjection(sessionId, { history: false });
17
+ for (const record of records ?? await readSessionTails(store))
18
+ frames.applyRecord(record);
19
+ const recovered = [];
20
+ for (const agent of frames.snapshot({ maxMessages: 0 }).agents) {
21
+ if (agent.turn !== undefined) {
22
+ await append(agent.address, { type: "turn.ended", turnId: agent.turn.turnId, reason: "failed" });
23
+ }
24
+ if (agent.live && agent.agent !== undefined) {
25
+ await append(agent.address, { type: "agent.ended", agent: agent.agent });
26
+ }
27
+ }
28
+ return recovered;
29
+ async function append(address, event) {
30
+ const record = { type: "event.lifecycle", eventId: newAgentEventId(), time: Date.now(), address, event };
31
+ await store.appendRecord(record);
32
+ recovered.push(record);
33
+ }
34
+ }
35
+ /** Host-facing recovery with projected events to broadcast after durable writes. Same
36
+ * exclusive-ownership requirement as recoverOrphanedTurns; no Session needs to be opened. */
37
+ export async function closeOrphanedTurns(sessionId, store) {
38
+ const records = await recoverOrphanedTurns(sessionId, store);
39
+ return records.flatMap((record) => {
40
+ const event = agentEventFromRecord(record, sessionId);
41
+ return event === undefined ? [] : [event];
42
+ });
43
+ }
44
+ //# sourceMappingURL=lifecycle-recovery.js.map
@@ -293,6 +293,7 @@ export class Runner {
293
293
  if (session.store && (await session.store.getState(INTERRUPTION_STATE_KEY)) !== null) {
294
294
  throw new Error("This session has an interrupted run. Resume or resolve it before starting a new prompt.");
295
295
  }
296
+ await session.recoverLifecycle();
296
297
  const head = await this.resolveConversationHead(session, rootAgent);
297
298
  const agent = head.agent;
298
299
  const state = await this.beginRun(session, opts, undefined, head.address);
@@ -254,6 +254,13 @@ export declare class Session implements SessionPort {
254
254
  /** Each stateful capability's `durableState`, by name — registered when it is restored. */
255
255
  private readonly durableOwners;
256
256
  private constructor();
257
+ private lifecycleRecovered;
258
+ /**
259
+ * Reconcile a cold session before its first run. The caller MUST hold this session's
260
+ * run lock and exclusive execution ownership (a distributed lease for shared stores).
261
+ * Opening/observing a session alone never proves that the previous executor is dead.
262
+ */
263
+ recoverLifecycle(): Promise<void>;
257
264
  /**
258
265
  * Open a session. Everything it depends on arrives in `opts`; `open` fills in the defaults for
259
266
  * whatever is missing, builds the session's own objects (signal, event publisher, log reader,
@@ -1,3 +1,5 @@
1
+ import { recoverOrphanedTurns } from "./lifecycle-recovery.js";
2
+ import { readSessionTails } from "../events/projection.js";
1
3
  import { FileFreshnessLedger } from "../tool/file-freshness.js";
2
4
  import { PermissionManager } from "../permission/manager.js";
3
5
  import { STAGED_POLICY_SLOTS } from "../permission/policies.js";
@@ -175,6 +177,25 @@ export class Session {
175
177
  this.allCapabilities = opts.capabilities ?? [];
176
178
  this.preloadedLog = opts.preloadedLog;
177
179
  }
180
+ lifecycleRecovered = false;
181
+ /**
182
+ * Reconcile a cold session before its first run. The caller MUST hold this session's
183
+ * run lock and exclusive execution ownership (a distributed lease for shared stores).
184
+ * Opening/observing a session alone never proves that the previous executor is dead.
185
+ */
186
+ async recoverLifecycle() {
187
+ if (this.lifecycleRecovered)
188
+ return;
189
+ if (this.store !== undefined) {
190
+ const records = await readSessionTails(this.store);
191
+ for (const address of new Set(records.map((record) => record.address ?? DEFAULT_ADDRESS))) {
192
+ this.checkpointing?.restoreFrame(address, records);
193
+ }
194
+ await recoverOrphanedTurns(this.id, this.store, records);
195
+ await this.flushEvents();
196
+ }
197
+ this.lifecycleRecovered = true;
198
+ }
178
199
  /**
179
200
  * Open a session. Everything it depends on arrives in `opts`; `open` fills in the defaults for
180
201
  * whatever is missing, builds the session's own objects (signal, event publisher, log reader,
@@ -132,9 +132,9 @@ export declare const ThinkingConfigSchema: z.ZodObject<{
132
132
  on: "on";
133
133
  }>;
134
134
  effort: z.ZodOptional<z.ZodEnum<{
135
+ high: "high";
135
136
  low: "low";
136
137
  medium: "medium";
137
- high: "high";
138
138
  }>>;
139
139
  }, z.core.$strip>;
140
140
  export declare const McpOAuthClientConfigSchema: z.ZodObject<{
@@ -319,9 +319,9 @@ export declare const AgentFrameworkConfigSchema: z.ZodObject<{
319
319
  on: "on";
320
320
  }>;
321
321
  effort: z.ZodOptional<z.ZodEnum<{
322
+ high: "high";
322
323
  low: "low";
323
324
  medium: "medium";
324
- high: "high";
325
325
  }>>;
326
326
  }, z.core.$strip>>;
327
327
  mcpServers: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodPreprocess<z.ZodPipe<z.ZodObject<{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "operon-agents-core",
3
- "version": "0.1.0-alpha.22",
3
+ "version": "0.1.0-alpha.24",
4
4
  "description": "Internal engine for operon-agents. Prefer operon-agents for application code.",
5
5
  "type": "module",
6
6
  "engines": {
@@ -71,7 +71,7 @@
71
71
  "README.md"
72
72
  ],
73
73
  "dependencies": {
74
- "@earendil-works/pi-ai": "1.3.0",
74
+ "@earendil-works/pi-ai": "1.5.0",
75
75
  "@modelcontextprotocol/sdk": "^1.29.0",
76
76
  "@opentelemetry/api": "^1.9.0",
77
77
  "acorn": "^8.17.0",