@vib-rato/agent-core 0.16.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/CHANGELOG.md +852 -0
- package/README.md +493 -0
- package/dist/types/agent-loop.d.ts +229 -0
- package/dist/types/agent.d.ts +533 -0
- package/dist/types/append-only-context.d.ts +141 -0
- package/dist/types/attempt-scope.d.ts +84 -0
- package/dist/types/compaction/adaptive.d.ts +31 -0
- package/dist/types/compaction/branch-summarization.d.ts +103 -0
- package/dist/types/compaction/compaction.d.ts +330 -0
- package/dist/types/compaction/entries.d.ts +124 -0
- package/dist/types/compaction/errors.d.ts +26 -0
- package/dist/types/compaction/index.d.ts +12 -0
- package/dist/types/compaction/messages.d.ts +61 -0
- package/dist/types/compaction/openai.d.ts +65 -0
- package/dist/types/compaction/pruning.d.ts +130 -0
- package/dist/types/compaction/utils.d.ts +32 -0
- package/dist/types/compaction.d.ts +1 -0
- package/dist/types/harmony-leak.d.ts +100 -0
- package/dist/types/heap-eviction-retainers.test.d.ts +1 -0
- package/dist/types/image-placeholder-guard.d.ts +4 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/proxy.d.ts +95 -0
- package/dist/types/run-collector.d.ts +223 -0
- package/dist/types/run-resource-ledger.d.ts +2 -0
- package/dist/types/telemetry.d.ts +605 -0
- package/dist/types/thinking.d.ts +18 -0
- package/dist/types/tool-dispatch-identity.d.ts +27 -0
- package/dist/types/types.d.ts +790 -0
- package/package.json +72 -0
- package/src/agent-loop.ts +5632 -0
- package/src/agent.ts +2437 -0
- package/src/append-only-context.ts +496 -0
- package/src/attempt-scope.ts +195 -0
- package/src/compaction/adaptive.ts +92 -0
- package/src/compaction/branch-summarization.ts +358 -0
- package/src/compaction/compaction.ts +1569 -0
- package/src/compaction/entries.ts +158 -0
- package/src/compaction/errors.ts +31 -0
- package/src/compaction/index.ts +13 -0
- package/src/compaction/messages.ts +212 -0
- package/src/compaction/openai.ts +580 -0
- package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
- package/src/compaction/prompts/branch-summary-context.md +5 -0
- package/src/compaction/prompts/branch-summary-preamble.md +2 -0
- package/src/compaction/prompts/branch-summary.md +30 -0
- package/src/compaction/prompts/compaction-short-summary.md +9 -0
- package/src/compaction/prompts/compaction-summary-context.md +5 -0
- package/src/compaction/prompts/compaction-summary.md +38 -0
- package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
- package/src/compaction/prompts/compaction-update-summary.md +45 -0
- package/src/compaction/prompts/file-operations.md +10 -0
- package/src/compaction/prompts/handoff-document.md +56 -0
- package/src/compaction/prompts/summarization-system.md +3 -0
- package/src/compaction/pruning.ts +1026 -0
- package/src/compaction/utils.ts +189 -0
- package/src/compaction.ts +1 -0
- package/src/harmony-leak.ts +457 -0
- package/src/heap-eviction-retainers.test.ts +293 -0
- package/src/image-placeholder-guard.ts +20 -0
- package/src/index.ts +23 -0
- package/src/prompts/escaped-nonascii-recovery.md +3 -0
- package/src/prompts/repeated-tool-failure-recovery.md +1 -0
- package/src/proxy.ts +408 -0
- package/src/run-collector.ts +728 -0
- package/src/run-resource-ledger.ts +345 -0
- package/src/telemetry.ts +2161 -0
- package/src/thinking.ts +20 -0
- package/src/tool-dispatch-identity.ts +87 -0
- package/src/types.ts +882 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent loop that works with AgentMessage throughout.
|
|
3
|
+
* Transforms to Message[] only at the LLM call boundary.
|
|
4
|
+
*/
|
|
5
|
+
import { type AssistantMessage, type AssistantMessageEvent, type Context, EventStream } from "@vib-rato/ai";
|
|
6
|
+
import type { AttemptScope } from "./attempt-scope";
|
|
7
|
+
import { type AgentRunCoverage, type AgentRunSummary } from "./run-collector";
|
|
8
|
+
import { type AgentContext, type AgentEvent, type AgentLoopConfig, type AgentMessage, type StreamFn } from "./types";
|
|
9
|
+
/** Sentinel returned by the abort race in `streamAssistantResponse`. */
|
|
10
|
+
/**
|
|
11
|
+
* Defensive caps for a provisional managed attempt. These are intentionally
|
|
12
|
+
* well above ordinary streamed responses; they only bound memory when an
|
|
13
|
+
* upstream emits an unbounded event stream before the attempt can commit.
|
|
14
|
+
*/
|
|
15
|
+
export declare const MANAGED_ATTEMPT_MAX_STAGED_EVENTS = 10000;
|
|
16
|
+
export declare const MANAGED_ATTEMPT_MAX_STAGED_BYTES: number;
|
|
17
|
+
/**
|
|
18
|
+
* Hard ceilings for the operator overrides. The caps exist to bound memory, so
|
|
19
|
+
* an override may raise them only within a range that still leaves the guard
|
|
20
|
+
* meaningful — near-`MAX_SAFE_INTEGER` values would trade a typed, bounded
|
|
21
|
+
* `local_buffer_overflow` for a process OOM, which is strictly harder to
|
|
22
|
+
* diagnose. Above-ceiling overrides clamp to the ceiling with a warning
|
|
23
|
+
* instead of being honored.
|
|
24
|
+
*
|
|
25
|
+
* The ceilings are derived from a survivable PEAK-RSS budget, not from the
|
|
26
|
+
* counted-bytes number: peak resident memory holds the live payload, its
|
|
27
|
+
* detached snapshot, and the retained batch simultaneously, so it is a
|
|
28
|
+
* multiple of the counted bytes. Sizing itself is walk-based (no JSON string
|
|
29
|
+
* or UTF-8 copy is materialized to measure), which is why the factor below
|
|
30
|
+
* covers the live value plus one detached copy plus batch retention with
|
|
31
|
+
* headroom. The bytes ceiling is the peak budget divided by that multiplier,
|
|
32
|
+
* so an override at the ceiling still fits an ordinary host. The events
|
|
33
|
+
* ceiling is the object-count equivalent for the same budget at a
|
|
34
|
+
* conservative per-item floor.
|
|
35
|
+
*/
|
|
36
|
+
export declare const MANAGED_STAGED_PEAK_RSS_BUDGET_BYTES: number;
|
|
37
|
+
export declare const MANAGED_STAGED_PEAK_RSS_FACTOR = 4;
|
|
38
|
+
export declare const MANAGED_ATTEMPT_STAGED_EVENTS_CEILING = 2000000;
|
|
39
|
+
export declare const MANAGED_ATTEMPT_STAGED_BYTES_CEILING: number;
|
|
40
|
+
/**
|
|
41
|
+
* Max events staged by a provisional managed-attempt transaction before it is
|
|
42
|
+
* rejected. Configurable via `VIB_FALLBACK_MAX_STAGED_EVENTS` (default
|
|
43
|
+
* `MANAGED_ATTEMPT_MAX_STAGED_EVENTS`, ceiling
|
|
44
|
+
* `MANAGED_ATTEMPT_STAGED_EVENTS_CEILING`). Read once per transaction so
|
|
45
|
+
* operators can raise the cap without a rebuild and tests can exercise the
|
|
46
|
+
* knob in-process. Values must be positive integers after the trusted
|
|
47
|
+
* resolver ignores surrounding whitespace; invalid or
|
|
48
|
+
* non-positive values fall back to the default, and values above the ceiling
|
|
49
|
+
* clamp to it with a warning.
|
|
50
|
+
*
|
|
51
|
+
* @internal
|
|
52
|
+
*/
|
|
53
|
+
export declare function managedAttemptMaxStagedEvents(): number;
|
|
54
|
+
/**
|
|
55
|
+
* Max bytes staged by a provisional managed-attempt transaction before it is
|
|
56
|
+
* rejected. Configurable via `VIB_FALLBACK_MAX_STAGED_BYTES` (default
|
|
57
|
+
* `MANAGED_ATTEMPT_MAX_STAGED_BYTES`, ceiling
|
|
58
|
+
* `MANAGED_ATTEMPT_STAGED_BYTES_CEILING`). Read once per transaction; values
|
|
59
|
+
* must be positive integers after the trusted resolver ignores surrounding
|
|
60
|
+
* whitespace, anything else falls back to the
|
|
61
|
+
* default, and values above the ceiling clamp to it with a warning.
|
|
62
|
+
*
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
export declare function managedAttemptMaxStagedBytes(): number;
|
|
66
|
+
/**
|
|
67
|
+
* Closed set of local-failure sites. A bounded diagnostic may name only these
|
|
68
|
+
* literals: the log is shape-only, so no caller-supplied or provider-derived
|
|
69
|
+
* string may ever reach it.
|
|
70
|
+
*/
|
|
71
|
+
declare const MANAGED_LOCAL_FAILURE_STAGES: readonly ["shell.role", "shell.content", "event.snapshot", "event.contentIndex", "event.delta", "event.content", "event.toolcall", "event.done.reason", "event.error.reason", "event.unknownType", "staging.losslessSnapshot", "staging.measure", "staging.sanitize", "staging.preMeasure", "staging.overflow", "overflow.preMeasure", "overflow.staged"];
|
|
72
|
+
type ManagedLocalFailureStage = (typeof MANAGED_LOCAL_FAILURE_STAGES)[number];
|
|
73
|
+
/**
|
|
74
|
+
* How many times a single turn may be re-requested because its tool arguments
|
|
75
|
+
* arrived as `\uXXXX` escapes instead of literal UTF-8.
|
|
76
|
+
*
|
|
77
|
+
* The defect is a wire-format accident that resampling clears, so a small
|
|
78
|
+
* budget recovers the overwhelming majority of turns; past it the terminal
|
|
79
|
+
* per-call rejection takes over rather than spending the run on retries.
|
|
80
|
+
*/
|
|
81
|
+
export declare const ESCAPED_NONASCII_RECOVERY_PROMPT: string;
|
|
82
|
+
/**
|
|
83
|
+
* Start an agent loop with a new prompt message.
|
|
84
|
+
* The prompt is added to the context and events are emitted for it.
|
|
85
|
+
*/
|
|
86
|
+
export declare function agentLoop(prompts: AgentMessage[], context: AgentContext, config: AgentLoopConfig, signal?: AbortSignal, streamFn?: StreamFn, emitAgentStart?: boolean, initialScope?: AttemptScope): EventStream<AgentEvent, AgentMessage[]>;
|
|
87
|
+
/**
|
|
88
|
+
* Continue an agent loop from the current context without adding a new message.
|
|
89
|
+
* Used for retries - context already has user message or tool results.
|
|
90
|
+
*
|
|
91
|
+
* **Important:** The last message in context must convert to a `user` or `toolResult` message
|
|
92
|
+
* via `convertToLlm`. If it doesn't, the LLM provider will reject the request.
|
|
93
|
+
* This cannot be validated here since `convertToLlm` is only called once per turn.
|
|
94
|
+
*/
|
|
95
|
+
export declare function agentLoopContinue(context: AgentContext, config: AgentLoopConfig, signal?: AbortSignal, streamFn?: StreamFn, emitAgentStart?: boolean, initialScope?: AttemptScope): EventStream<AgentEvent, AgentMessage[]>;
|
|
96
|
+
/**
|
|
97
|
+
* Structured, shape-only overflow diagnostic carried on the terminal
|
|
98
|
+
* `AssistantMessage` of a managed run that died of a staging-buffer overflow.
|
|
99
|
+
* Every field is closed-vocabulary or numeric, so parent surfaces can render a
|
|
100
|
+
* trustworthy summary WITHOUT trusting the free-form `errorMessage` string
|
|
101
|
+
* (which a foreign, self-labeled error can still fill with arbitrary text).
|
|
102
|
+
*/
|
|
103
|
+
export interface ManagedBufferOverflowDiagnostic {
|
|
104
|
+
stage: ManagedLocalFailureStage | "unknown";
|
|
105
|
+
exceeded: "events" | "bytes" | "both";
|
|
106
|
+
stagedEventCount: number;
|
|
107
|
+
stagedBytes: number;
|
|
108
|
+
incomingEventBytes: number;
|
|
109
|
+
maxStagedEvents: number;
|
|
110
|
+
maxStagedBytes: number;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The complete set of local-diagnostic authority fields a terminal
|
|
114
|
+
* `AssistantMessage` may carry. Produced only by
|
|
115
|
+
* {@link managedLocalErrorDiagnostic}, so `errorKind` and `bufferOverflow`
|
|
116
|
+
* always travel together from one identity check.
|
|
117
|
+
*/
|
|
118
|
+
export interface ManagedLocalErrorDiagnostic {
|
|
119
|
+
errorKind: "local_snapshot_failure" | "local_buffer_overflow";
|
|
120
|
+
bufferOverflow?: ManagedBufferOverflowDiagnostic;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Single identity-checked source of local-failure authority. Returns
|
|
124
|
+
* `undefined` unless the error is genuinely `instanceof` one of this module's
|
|
125
|
+
* private local-failure classes — a foreign error that merely sets
|
|
126
|
+
* `errorKind: "local_buffer_overflow"` fails the identity check and receives
|
|
127
|
+
* NEITHER the kind nor the structured shape, so a provider or custom-stream
|
|
128
|
+
* failure can never be reported to the parent as a local staging-buffer
|
|
129
|
+
* overflow (#4618).
|
|
130
|
+
*
|
|
131
|
+
* Every producer of a terminal assistant message (`managedFailureMessage` and
|
|
132
|
+
* the `Agent` run catch) MUST derive both fields from this function instead of
|
|
133
|
+
* reading `errorKind`/`errorMessage` off the thrown value.
|
|
134
|
+
*/
|
|
135
|
+
export declare function managedLocalErrorDiagnostic(error: unknown): ManagedLocalErrorDiagnostic | undefined;
|
|
136
|
+
/**
|
|
137
|
+
* Hard work budget for one degraded snapshot: every visited node AND every
|
|
138
|
+
* enumerated own key is debited against this budget before it is processed
|
|
139
|
+
* (accessor keys and re-visits of shared objects included), and any remainder
|
|
140
|
+
* collapses to the deterministic `"[truncated]"` placeholder. Well above
|
|
141
|
+
* ordinary streamed events; it only bounds hostile graphs.
|
|
142
|
+
*/
|
|
143
|
+
export declare const MANAGED_SNAPSHOT_MAX_NODES = 100000;
|
|
144
|
+
/**
|
|
145
|
+
* Cycle-aware deep clone that always returns a detached, JSON-serializable
|
|
146
|
+
* value. Used whenever a detached snapshot cannot be safely obtained or
|
|
147
|
+
* measured: after `structuredClone` fails, and again when a (successfully
|
|
148
|
+
* cloned) snapshot cannot be serialized for byte accounting.
|
|
149
|
+
*
|
|
150
|
+
* Totality rules — the walk must never dispatch through payload-controlled
|
|
151
|
+
* code, throw, or do unbounded work:
|
|
152
|
+
* - proxies (revoked or live) are collapsed to `"[unserializable]"` BEFORE
|
|
153
|
+
* any reflective operation, so `ownKeys`/descriptor traps are never
|
|
154
|
+
* dispatched (`util.types.isProxy` identifies proxies without touching
|
|
155
|
+
* their handlers);
|
|
156
|
+
* - only intrinsics are used on the remaining ordinary objects (no
|
|
157
|
+
* `input.map`, no `input.getTime()`, no `input.length` reads);
|
|
158
|
+
* - arrays are enumerated through their own present keys, never their
|
|
159
|
+
* declared length, so a sparse array cannot force a dense allocation
|
|
160
|
+
* proportional to `length`; sparse/exotic arrays degrade to a null-proto
|
|
161
|
+
* record of their present indices, and the dense-shape decision verifies
|
|
162
|
+
* every index against its ordinal;
|
|
163
|
+
* - the walk debits `maxNodes` budget per visited node and per enumerated
|
|
164
|
+
* key before processing it; anything beyond the budget becomes
|
|
165
|
+
* `"[truncated]"` (the one linear primitive per visited node is a single
|
|
166
|
+
* `Object.keys` call on a non-proxy object the process already holds);
|
|
167
|
+
* - property values are read via own-property descriptors, so accessors are
|
|
168
|
+
* never invoked (a snapshot must not cause observable side effects) and are
|
|
169
|
+
* replaced with `"[accessor]"`;
|
|
170
|
+
* - functions/symbols and any property that cannot be read safely become
|
|
171
|
+
* short placeholders, `bigint` becomes its decimal string, and references
|
|
172
|
+
* back into the current path collapse to `"[Circular]"`;
|
|
173
|
+
* - records are built on a null prototype so a `__proto__` key cannot mutate
|
|
174
|
+
* the clone's prototype chain.
|
|
175
|
+
*
|
|
176
|
+
* Exported for direct regression coverage of the budget accounting; runtime
|
|
177
|
+
* callers use the default budget via {@link managedAttemptSnapshot}.
|
|
178
|
+
*/
|
|
179
|
+
export declare function sanitizedDetachedClone<T>(value: T, maxNodes?: number): T;
|
|
180
|
+
/**
|
|
181
|
+
* Exact serialized size of a staged snapshot, computed by walking the JSON
|
|
182
|
+
* surface. Replaces the previous `JSON.stringify` + `TextEncoder.encode`
|
|
183
|
+
* measurement, which materialized a full copy of the serialized value — and
|
|
184
|
+
* a second copy of its UTF-8 encoding — BEFORE the cap check could reject
|
|
185
|
+
* it: the budget-sized transient allocation the memory guard exists to
|
|
186
|
+
* prevent (exact-head 078e22c0 finding 1). Returns `undefined` when the
|
|
187
|
+
* value cannot be serialized, matching the previous measurement's failure
|
|
188
|
+
* mode so callers keep their sanitize fallbacks.
|
|
189
|
+
*
|
|
190
|
+
* @internal
|
|
191
|
+
*/
|
|
192
|
+
export declare function managedSnapshotJsonByteLength(value: unknown): number | undefined;
|
|
193
|
+
export declare function managedAssistantEventSnapshot(event: AssistantMessageEvent, message: AssistantMessage, degradedFieldDiagnostics?: Set<string>): AssistantMessageEvent;
|
|
194
|
+
/**
|
|
195
|
+
* Detailed-result handle returned by {@link agentLoopDetailed}. Adds the
|
|
196
|
+
* run-level telemetry/coverage rollup to the existing `AgentMessage[]`
|
|
197
|
+
* payload without changing the resolved type of `stream.result()`.
|
|
198
|
+
*/
|
|
199
|
+
export interface AgentLoopDetailedResult {
|
|
200
|
+
readonly messages: AgentMessage[];
|
|
201
|
+
readonly telemetry: AgentRunSummary | undefined;
|
|
202
|
+
readonly coverage: AgentRunCoverage | undefined;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Convenience wrapper over {@link agentLoop} that exposes the run-level
|
|
206
|
+
* summary + coverage alongside the messages. The returned `stream` is the
|
|
207
|
+
* same `EventStream` callers already consume; `detailed()` awaits the
|
|
208
|
+
* stream's `agent_end` event and returns the additive fields.
|
|
209
|
+
*
|
|
210
|
+
* Existing `stream.result()` semantics are preserved — it still resolves to
|
|
211
|
+
* `AgentMessage[]`. Use {@link agentLoopDetailed} when you need the rollup;
|
|
212
|
+
* use {@link agentLoop} when you do not.
|
|
213
|
+
*/
|
|
214
|
+
export declare function agentLoopDetailed(prompts: AgentMessage[], context: AgentContext, config: AgentLoopConfig, signal?: AbortSignal, streamFn?: StreamFn): {
|
|
215
|
+
readonly stream: EventStream<AgentEvent, AgentMessage[]>;
|
|
216
|
+
readonly detailed: () => Promise<AgentLoopDetailedResult>;
|
|
217
|
+
};
|
|
218
|
+
/**
|
|
219
|
+
* Like {@link agentLoopDetailed} but built on top of
|
|
220
|
+
* {@link agentLoopContinue}.
|
|
221
|
+
*/
|
|
222
|
+
export declare function agentLoopContinueDetailed(context: AgentContext, config: AgentLoopConfig, signal?: AbortSignal, streamFn?: StreamFn): {
|
|
223
|
+
readonly stream: EventStream<AgentEvent, AgentMessage[]>;
|
|
224
|
+
readonly detailed: () => Promise<AgentLoopDetailedResult>;
|
|
225
|
+
};
|
|
226
|
+
export declare function normalizeMessagesForProvider(messages: Context["messages"], model: AgentLoopConfig["model"]): Context["messages"];
|
|
227
|
+
export declare const INTENT_FIELD = "_i";
|
|
228
|
+
export declare function normalizeTools(tools: AgentContext["tools"], injectIntent: boolean): Context["tools"];
|
|
229
|
+
export {};
|