@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.
Files changed (69) hide show
  1. package/CHANGELOG.md +852 -0
  2. package/README.md +493 -0
  3. package/dist/types/agent-loop.d.ts +229 -0
  4. package/dist/types/agent.d.ts +533 -0
  5. package/dist/types/append-only-context.d.ts +141 -0
  6. package/dist/types/attempt-scope.d.ts +84 -0
  7. package/dist/types/compaction/adaptive.d.ts +31 -0
  8. package/dist/types/compaction/branch-summarization.d.ts +103 -0
  9. package/dist/types/compaction/compaction.d.ts +330 -0
  10. package/dist/types/compaction/entries.d.ts +124 -0
  11. package/dist/types/compaction/errors.d.ts +26 -0
  12. package/dist/types/compaction/index.d.ts +12 -0
  13. package/dist/types/compaction/messages.d.ts +61 -0
  14. package/dist/types/compaction/openai.d.ts +65 -0
  15. package/dist/types/compaction/pruning.d.ts +130 -0
  16. package/dist/types/compaction/utils.d.ts +32 -0
  17. package/dist/types/compaction.d.ts +1 -0
  18. package/dist/types/harmony-leak.d.ts +100 -0
  19. package/dist/types/heap-eviction-retainers.test.d.ts +1 -0
  20. package/dist/types/image-placeholder-guard.d.ts +4 -0
  21. package/dist/types/index.d.ts +13 -0
  22. package/dist/types/proxy.d.ts +95 -0
  23. package/dist/types/run-collector.d.ts +223 -0
  24. package/dist/types/run-resource-ledger.d.ts +2 -0
  25. package/dist/types/telemetry.d.ts +605 -0
  26. package/dist/types/thinking.d.ts +18 -0
  27. package/dist/types/tool-dispatch-identity.d.ts +27 -0
  28. package/dist/types/types.d.ts +790 -0
  29. package/package.json +72 -0
  30. package/src/agent-loop.ts +5632 -0
  31. package/src/agent.ts +2437 -0
  32. package/src/append-only-context.ts +496 -0
  33. package/src/attempt-scope.ts +195 -0
  34. package/src/compaction/adaptive.ts +92 -0
  35. package/src/compaction/branch-summarization.ts +358 -0
  36. package/src/compaction/compaction.ts +1569 -0
  37. package/src/compaction/entries.ts +158 -0
  38. package/src/compaction/errors.ts +31 -0
  39. package/src/compaction/index.ts +13 -0
  40. package/src/compaction/messages.ts +212 -0
  41. package/src/compaction/openai.ts +580 -0
  42. package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
  43. package/src/compaction/prompts/branch-summary-context.md +5 -0
  44. package/src/compaction/prompts/branch-summary-preamble.md +2 -0
  45. package/src/compaction/prompts/branch-summary.md +30 -0
  46. package/src/compaction/prompts/compaction-short-summary.md +9 -0
  47. package/src/compaction/prompts/compaction-summary-context.md +5 -0
  48. package/src/compaction/prompts/compaction-summary.md +38 -0
  49. package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
  50. package/src/compaction/prompts/compaction-update-summary.md +45 -0
  51. package/src/compaction/prompts/file-operations.md +10 -0
  52. package/src/compaction/prompts/handoff-document.md +56 -0
  53. package/src/compaction/prompts/summarization-system.md +3 -0
  54. package/src/compaction/pruning.ts +1026 -0
  55. package/src/compaction/utils.ts +189 -0
  56. package/src/compaction.ts +1 -0
  57. package/src/harmony-leak.ts +457 -0
  58. package/src/heap-eviction-retainers.test.ts +293 -0
  59. package/src/image-placeholder-guard.ts +20 -0
  60. package/src/index.ts +23 -0
  61. package/src/prompts/escaped-nonascii-recovery.md +3 -0
  62. package/src/prompts/repeated-tool-failure-recovery.md +1 -0
  63. package/src/proxy.ts +408 -0
  64. package/src/run-collector.ts +728 -0
  65. package/src/run-resource-ledger.ts +345 -0
  66. package/src/telemetry.ts +2161 -0
  67. package/src/thinking.ts +20 -0
  68. package/src/tool-dispatch-identity.ts +87 -0
  69. package/src/types.ts +882 -0
@@ -0,0 +1,223 @@
1
+ /**
2
+ * Per-invocation run aggregator. Buffers per-chat and per-tool records as the
3
+ * loop executes and folds them into a single {@link AgentRunSummary} +
4
+ * {@link AgentRunCoverage} value at the end.
5
+ *
6
+ * One collector lives on each {@link AgentTelemetry} handle, which is
7
+ * constructed once per `agentLoop` invocation in {@link resolveTelemetry}.
8
+ * Collector lookups use the live `Span` as a `WeakMap` key — bounded memory,
9
+ * no cross-invoke leakage.
10
+ *
11
+ * The collector is fed exclusively by helpers in `./telemetry.ts`. Loop
12
+ * authors do not interact with it directly except via the public
13
+ * `recordSkippedTool` helper used for the two skip paths that bypass spans
14
+ * entirely (pre-run interrupt and the tail-sweep for tool calls that never
15
+ * produced a result message).
16
+ */
17
+ import type { Span } from "@opentelemetry/api";
18
+ import type { AssistantMessage, Model, StopReason } from "@vib-rato/ai";
19
+ /** Terminal status reported by an `execute_tool` span. */
20
+ export type ToolStatus = "ok" | "error" | "skipped" | "blocked" | "timeout" | "aborted";
21
+ /** Raw record for a single `chat` step, finalized by `finishChatSpan`. */
22
+ export interface ChatRecord {
23
+ readonly stepNumber: number;
24
+ readonly model: string;
25
+ readonly provider: string;
26
+ readonly stopReason: StopReason | undefined;
27
+ readonly latencyMs: number;
28
+ readonly inputTokens: number;
29
+ readonly outputTokens: number;
30
+ readonly cachedInputTokens: number;
31
+ readonly cacheWriteTokens: number;
32
+ readonly reasoningOutputTokens: number;
33
+ readonly totalTokens: number;
34
+ readonly costUsd: number | undefined;
35
+ readonly costUnavailableReason: string | undefined;
36
+ readonly errorType: string | undefined;
37
+ }
38
+ /** Raw record for a single `execute_tool` invocation. */
39
+ export interface ToolRecord {
40
+ readonly toolCallId: string;
41
+ readonly toolName: string;
42
+ readonly status: ToolStatus;
43
+ readonly latencyMs: number;
44
+ readonly errorType: string | undefined;
45
+ }
46
+ /** Per-tool counters surfaced under {@link AgentRunSummary.tools.byName}. */
47
+ export interface ToolCounters {
48
+ readonly total: number;
49
+ readonly ok: number;
50
+ readonly error: number;
51
+ readonly skipped: number;
52
+ readonly blocked: number;
53
+ readonly timeout: number;
54
+ readonly aborted: number;
55
+ readonly totalLatencyMs: number;
56
+ }
57
+ /**
58
+ * Run-level rollup returned in the `agent_end` event and passed to
59
+ * {@link AgentTelemetryConfig.onRunEnd}. Pure aggregation — no references to
60
+ * spans, no callbacks, no live state. Safe to persist / diff / assert.
61
+ */
62
+ export interface AgentRunSummary {
63
+ readonly chats: {
64
+ readonly total: number;
65
+ /** Bucketed by raw {@link StopReason}; absent reasons omitted. */
66
+ readonly byStopReason: Readonly<Record<string, number>>;
67
+ readonly totalLatencyMs: number;
68
+ };
69
+ readonly tools: {
70
+ readonly total: number;
71
+ readonly ok: number;
72
+ readonly error: number;
73
+ readonly skipped: number;
74
+ readonly blocked: number;
75
+ readonly timeout: number;
76
+ readonly aborted: number;
77
+ readonly totalLatencyMs: number;
78
+ /** Per-tool-name counters; keys sorted by name on snapshot. */
79
+ readonly byName: Readonly<Record<string, ToolCounters>>;
80
+ };
81
+ readonly usage: {
82
+ readonly inputTokens: number;
83
+ readonly outputTokens: number;
84
+ readonly cachedInputTokens: number;
85
+ readonly cacheWriteTokens: number;
86
+ readonly reasoningOutputTokens: number;
87
+ readonly totalTokens: number;
88
+ };
89
+ readonly cost: {
90
+ readonly estimatedUsd: number;
91
+ /** Sorted, deduped. */
92
+ readonly unavailableReasons: readonly string[];
93
+ };
94
+ readonly errors: {
95
+ readonly total: number;
96
+ readonly byType: Readonly<Record<string, number>>;
97
+ };
98
+ readonly stepCount: number;
99
+ }
100
+ /**
101
+ * Coverage rollup: registered-vs-invoked across the run. All arrays are
102
+ * sorted ascending and deduped so the value is stable for diffing.
103
+ */
104
+ export interface AgentRunCoverage {
105
+ readonly toolsAvailable: readonly string[];
106
+ readonly toolsInvoked: readonly string[];
107
+ readonly toolsUnused: readonly string[];
108
+ readonly modelsUsed: readonly string[];
109
+ readonly providersUsed: readonly string[];
110
+ }
111
+ export declare class AgentRunCollector {
112
+ #private;
113
+ /** True once `markRunEnded()` has been called for this invocation. */
114
+ get runEnded(): boolean;
115
+ /**
116
+ * Mark this run as logically ended. Callers use this to coordinate the
117
+ * `onRunEnd` hook between the success path (fires inside
118
+ * `buildAgentEndEvent`, before `stream.end()`) and the error path (fires
119
+ * inside `finishInvokeAgentSpan`'s finally). Idempotent — returns `true`
120
+ * the first time, `false` on subsequent calls.
121
+ */
122
+ markRunEnded(): boolean;
123
+ /** Record the tool names exposed on a single chat step. */
124
+ noteAvailableTools(tools: readonly {
125
+ readonly name: string;
126
+ }[] | undefined): void;
127
+ beginChat(span: Span, init: {
128
+ readonly stepNumber: number;
129
+ readonly model: Model;
130
+ readonly provider?: string;
131
+ }): void;
132
+ /** Begin a chat record without allocating or mutating an OTEL span. */
133
+ beginChatWithoutSpan(init: {
134
+ readonly stepNumber: number;
135
+ readonly model: Model;
136
+ readonly provider?: string;
137
+ }): void;
138
+ endChat(span: Span, message: AssistantMessage, fields: {
139
+ readonly costUsd: number | undefined;
140
+ readonly costUnavailableReason: string | undefined;
141
+ }): void;
142
+ /** Finish a chat record without allocating or mutating an OTEL span. */
143
+ endChatWithoutSpan(stepNumber: number | undefined, message: AssistantMessage, fields: {
144
+ readonly costUsd: number | undefined;
145
+ readonly costUnavailableReason: string | undefined;
146
+ }): void;
147
+ /**
148
+ * Stamp the chat span as failed without a finalized AssistantMessage. Used
149
+ * by the `catch` arm of `streamAssistantResponse` so error chats still
150
+ * appear in the run summary.
151
+ */
152
+ failChat(span: Span, fields: {
153
+ readonly errorType: string;
154
+ }): void;
155
+ /** Record a failed chat without allocating or mutating an OTEL span. */
156
+ failChatWithoutSpan(stepNumber: number | undefined, fields: {
157
+ readonly errorType: string;
158
+ }): void;
159
+ beginTool(span: Span, init: {
160
+ readonly toolCallId: string;
161
+ readonly toolName: string;
162
+ }): void;
163
+ /** Begin a tool record without allocating or mutating an OTEL span. */
164
+ beginToolWithoutSpan(init: {
165
+ readonly toolCallId: string;
166
+ readonly toolName: string;
167
+ }): void;
168
+ endTool(span: Span, fields: {
169
+ readonly status: ToolStatus;
170
+ readonly errorType: string | undefined;
171
+ }): void;
172
+ /** Finish a tool record without allocating or mutating an OTEL span. */
173
+ endToolWithoutSpan(record: {
174
+ readonly toolCallId: string;
175
+ readonly toolName: string;
176
+ readonly status: ToolStatus;
177
+ readonly errorType: string | undefined;
178
+ }): void;
179
+ /**
180
+ * Record a tool that never produced a span — pre-run interrupt or tail
181
+ * sweep. The LLM still asked for it, so it counts toward
182
+ * {@link AgentRunCoverage.toolsInvoked}.
183
+ */
184
+ recordOrphanTool(record: {
185
+ readonly toolCallId: string;
186
+ readonly toolName: string;
187
+ readonly status: ToolStatus;
188
+ }): void;
189
+ /** Build the immutable summary value from buffered records. */
190
+ snapshot(opts: {
191
+ readonly stepCount: number;
192
+ }): {
193
+ readonly summary: AgentRunSummary;
194
+ readonly coverage: AgentRunCoverage;
195
+ };
196
+ }
197
+ /**
198
+ * Fold multiple per-run summaries into one. Pure aggregation — useful when a
199
+ * caller (verify pass, benchmark harness) drives the agent loop N times and
200
+ * needs a single rollup across all invocations.
201
+ *
202
+ * Counters sum element-wise. Sets (cost reasons, error types, per-tool
203
+ * counters) merge by key. Numeric totals sum. The output is in the same
204
+ * shape as a single `AgentRunSummary`, so all dashboards and persistence
205
+ * layers handle it uniformly.
206
+ */
207
+ export declare function aggregateAgentRunSummaries(summaries: readonly AgentRunSummary[]): AgentRunSummary;
208
+ /** Union-merge multiple coverage values, preserving the sorted+deduped invariant. */
209
+ export declare function aggregateAgentRunCoverage(coverages: readonly AgentRunCoverage[]): AgentRunCoverage;
210
+ /** Empty `AgentRunSummary` constant. Exported for tests and default-initializers. */
211
+ export declare function emptyAgentRunSummary(): AgentRunSummary;
212
+ /** Empty `AgentRunCoverage` constant. Exported for tests and default-initializers. */
213
+ export declare function emptyAgentRunCoverage(): AgentRunCoverage;
214
+ /**
215
+ * Distinguishable error class thrown when `beforeToolCall` returns
216
+ * `{ block: true }`. Lets the catch arm of `runTool` set the terminal status
217
+ * on the execute_tool span to `"blocked"` instead of conflating with a real
218
+ * tool exception.
219
+ */
220
+ export declare class ToolCallBlockedError extends Error {
221
+ readonly name = "ToolCallBlockedError";
222
+ constructor(reason?: string);
223
+ }
@@ -0,0 +1,2 @@
1
+ import type { RunResourceLedger } from "./types";
2
+ export declare function createRunResourceLedger(): RunResourceLedger;