@gajae-code/agent-core 0.10.2 → 0.11.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.
package/src/types.ts CHANGED
@@ -25,11 +25,82 @@ export type StreamFn = (
25
25
  ...args: Parameters<typeof streamSimple>
26
26
  ) => AssistantMessageEventStream | Promise<AssistantMessageEventStream>;
27
27
 
28
+ /** Stable identifier for a managed logical run, shared by all of its retry attempts. */
29
+ export type ManagedLogicalRunId = number;
30
+
31
+ /** Terminal completion requested for a logical run. */
32
+ export interface RunTerminalRequest {
33
+ stopReason: "cancelled" | "error" | "exhausted";
34
+ messages?: AgentMessage[];
35
+ }
36
+
37
+ /**
38
+ * Ownership token supplied when Agent invokes a retry continuation.
39
+ *
40
+ * A continuation MUST verify `isCurrent()` immediately before starting a
41
+ * follow-up invocation and abandon the retry when it returns false. The token
42
+ * becomes invalid when its originating run is force-aborted or superseded.
43
+ * Coding-agent retry continuations must accept this argument and must not call
44
+ * `agent.continue()` after ownership has been lost.
45
+ */
46
+ export interface ManagedAttemptContinuationOwnership {
47
+ /** Per-attempt run-loop id; use only for attempt-local ownership checks. */
48
+ readonly runId: number;
49
+ /** Stable managed logical-run id; use for all terminal completion requests. */
50
+ readonly logicalRunId: ManagedLogicalRunId;
51
+ readonly generation: number;
52
+ isCurrent(): boolean;
53
+ }
54
+
55
+ /** Runs after a discarded attempt is idle, only while its ownership token remains current. */
56
+ export type ManagedAttemptContinuation = (ownership: ManagedAttemptContinuationOwnership) => void | Promise<void>;
57
+
58
+ /** Decision returned by managed fallback policy for one provisional attempt. */
59
+ export type ManagedAttemptDecision =
60
+ | { type: "retry"; continuation: ManagedAttemptContinuation }
61
+ | { type: "terminal"; terminal: RunTerminalRequest };
62
+
63
+ /** Structured result for one managed upstream invocation. */
64
+ export type ManagedAttemptOutcome =
65
+ | {
66
+ type: "retryable_discarded";
67
+ failure: {
68
+ message: AssistantMessage;
69
+ /** Exact provider transport facts, including retry headers, for fallback policy. */
70
+ transportFailure?: import("@gajae-code/ai").TransportFailureFacts;
71
+ };
72
+ }
73
+ | { type: "run_terminal"; reason: "cancelled" | "error" | "exhausted" };
74
+
75
+ export type ManagedAttemptOutcomeHandler = (
76
+ outcome: ManagedAttemptOutcome,
77
+ ) => ManagedAttemptDecision | Promise<ManagedAttemptDecision>;
78
+
79
+ /**
80
+ * Outcome of a cooperative mid-run context-maintenance checkpoint (see
81
+ * {@link AgentLoopConfig.maintainContext}). Any value other than "not-needed"
82
+ * means the checkpoint mutated (or attempted to mutate) durable context, so the
83
+ * loop ends the current run without the lossy `agent_end` finalization and the
84
+ * maintenance owner resumes the run on the rewritten context.
85
+ */
86
+ export type MidRunMaintenanceOutcome = "not-needed" | "pruned" | "compacted" | "promoted" | "failed" | "aborted";
87
+
28
88
  /**
29
89
  * Configuration for the agent loop.
30
90
  */
31
91
  export interface AgentLoopConfig extends SimpleStreamOptions {
32
92
  model: Model;
93
+ /**
94
+ * Supplies a fresh opaque token at each concrete managed transport invocation.
95
+ * The callback runs at the stream boundary so controller accounting matches
96
+ * upstream request count, including multi-step tool turns.
97
+ */
98
+ nextFallbackAttempt?: (model: Model) => SimpleStreamOptions["fallbackAttempt"];
99
+ /** Called after a managed upstream request is accepted and committed. */
100
+ onManagedAttemptAccepted?: () => void | Promise<void>;
101
+
102
+ /** Receives a managed invocation outcome without publishing provisional lifecycle events. */
103
+ onManagedAttemptOutcome?: ManagedAttemptOutcomeHandler;
33
104
 
34
105
  /**
35
106
  * When to interrupt tool execution for steering messages.
@@ -161,6 +232,33 @@ export interface AgentLoopConfig extends SimpleStreamOptions {
161
232
  */
162
233
  syncContextBeforeModelCall?: (context: AgentContext) => void | Promise<void>;
163
234
 
235
+ /**
236
+ * Cooperative mid-run context-maintenance checkpoint.
237
+ *
238
+ * Invoked at the top of every loop iteration AFTER pending tool-result /
239
+ * steering messages have been materialized into durable context and BEFORE
240
+ * {@link syncContextBeforeModelCall} and the model call. This is the only
241
+ * boundary where the full unsent context (tool results + dequeued steering)
242
+ * is already durable, so a long uninterrupted tool loop can be bounded here
243
+ * before it grows past the provider window.
244
+ *
245
+ * The callback owns the maintenance decision (prune / compact / promote) and
246
+ * receives the minimal cancellation-aware lifecycle: `signal` is the
247
+ * non-optional loop signal, and `awaitEventDrain(invocationSignal)` waits for
248
+ * prior event consumer bodies with loop and invocation cancellation composed.
249
+ * Any outcome other than "not-needed" ends the current run with
250
+ * `agent_end.stopReason === "maintenance"` (NOT the lossy pause / completed
251
+ * finalization); the callback's continuation owner resumes the run on the
252
+ * rewritten context.
253
+ */
254
+ maintainContext?: (
255
+ context: AgentContext,
256
+ lifecycle: {
257
+ signal: AbortSignal;
258
+ awaitEventDrain: (invocationSignal: AbortSignal) => Promise<void>;
259
+ },
260
+ ) => Promise<MidRunMaintenanceOutcome> | MidRunMaintenanceOutcome;
261
+
164
262
  /**
165
263
  * Optional transform applied to tool call arguments before execution.
166
264
  * Use for deobfuscating secrets or rewriting arguments.
@@ -470,8 +568,10 @@ export type AgentEvent =
470
568
  | {
471
569
  type: "agent_end";
472
570
  messages: AgentMessage[];
473
- /** Indicates whether the loop ended normally or suspended at a pause checkpoint. */
474
- stopReason?: "completed" | "paused";
571
+ /** Indicates whether the loop ended normally, suspended, cancelled, or entered maintenance. */
572
+ stopReason?: "completed" | "paused" | "cancelled" | "maintenance";
573
+ /** Present iff `stopReason === "maintenance"`; the maintenance outcome. */
574
+ maintenanceOutcome?: MidRunMaintenanceOutcome;
475
575
  /** Present iff `AgentTelemetryConfig` was supplied on this run. */
476
576
  telemetry?: AgentRunSummary;
477
577
  coverage?: AgentRunCoverage;