@cursor/july 0.1.48 → 0.1.49
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/dist/docs/404.html +2 -2
- package/dist/docs/ab.html +5 -5
- package/dist/docs/assets/{ab.md.hdxsvX5M.js → ab.md.DYjwREAP.js} +2 -2
- package/dist/docs/assets/{ab.md.hdxsvX5M.lean.js → ab.md.DYjwREAP.lean.js} +1 -1
- package/dist/docs/assets/{app.BC1P20xI.js → app.BpWAgjaR.js} +1 -1
- package/dist/docs/assets/chunks/@localSearchIndexroot.D00d772K.js +1 -0
- package/dist/docs/assets/chunks/{VPLocalSearchBox.C_900xn2.js → VPLocalSearchBox.CnAktZtG.js} +1 -1
- package/dist/docs/assets/chunks/{theme.CT135Jhh.js → theme.B5IvUxOB.js} +2 -2
- package/dist/docs/assets/{evals.md.qeoGgPw0.js → evals.md.CVe_O75-.js} +3 -3
- package/dist/docs/assets/{evals.md.qeoGgPw0.lean.js → evals.md.CVe_O75-.lean.js} +1 -1
- package/dist/docs/assets/{reference_http-api.md.DGrw_wOu.js → reference_http-api.md.DMbdFGVQ.js} +1 -1
- package/dist/docs/assets/{storage.md.CJYd5YmN.js → storage.md.D8NQ389l.js} +3 -3
- package/dist/docs/assets/storage.md.D8NQ389l.lean.js +1 -0
- package/dist/docs/building-with-agents.html +3 -3
- package/dist/docs/concepts.html +3 -3
- package/dist/docs/deployment.html +3 -3
- package/dist/docs/evals.html +6 -6
- package/dist/docs/example-agents/approval-buddy.html +3 -3
- package/dist/docs/example-agents/benny.html +3 -3
- package/dist/docs/example-agents/bugbot.html +3 -3
- package/dist/docs/example-agents/codebase-wiki.html +3 -3
- package/dist/docs/example-agents/codeowners-review.html +3 -3
- package/dist/docs/example-agents/concierge.html +3 -3
- package/dist/docs/example-agents/fsd.html +3 -3
- package/dist/docs/example-agents/index.html +3 -3
- package/dist/docs/example-agents/knowledge-base.html +3 -3
- package/dist/docs/example-agents/oncall.html +3 -3
- package/dist/docs/example-agents/security-reviewer.html +3 -3
- package/dist/docs/example-agents/slack-agent.html +3 -3
- package/dist/docs/example-agents/weather-agent.html +3 -3
- package/dist/docs/guides/agent-to-agent.html +3 -3
- package/dist/docs/guides/cloud-runtime.html +3 -3
- package/dist/docs/guides/github.html +3 -3
- package/dist/docs/guides/human-in-the-loop.html +3 -3
- package/dist/docs/guides/mcp-oauth.html +3 -3
- package/dist/docs/guides/opentelemetry.html +3 -3
- package/dist/docs/guides/slack.html +3 -3
- package/dist/docs/guides/webhooks.html +3 -3
- package/dist/docs/hashmap.json +1 -1
- package/dist/docs/hillclimbing.html +3 -3
- package/dist/docs/index.html +3 -3
- package/dist/docs/quickstart.html +3 -3
- package/dist/docs/reference/agent-config.html +3 -3
- package/dist/docs/reference/artifacts.html +3 -3
- package/dist/docs/reference/channels.html +3 -3
- package/dist/docs/reference/cli.html +3 -3
- package/dist/docs/reference/connections.html +3 -3
- package/dist/docs/reference/hooks.html +3 -3
- package/dist/docs/reference/http-api.html +5 -5
- package/dist/docs/reference/instructions.html +3 -3
- package/dist/docs/reference/playground.html +3 -3
- package/dist/docs/reference/project-layout.html +3 -3
- package/dist/docs/reference/prompt.html +3 -3
- package/dist/docs/reference/schedules.html +3 -3
- package/dist/docs/reference/sessions.html +3 -3
- package/dist/docs/reference/skills.html +3 -3
- package/dist/docs/reference/subagents.html +3 -3
- package/dist/docs/reference/tools.html +3 -3
- package/dist/docs/scaffolding-agents.html +3 -3
- package/dist/docs/storage.html +5 -5
- package/dist/docs/templates/triage.html +3 -3
- package/dist/docs/troubleshooting.html +3 -3
- package/dist/internal/cli-slack.js +0 -3
- package/dist/internal/event-log.d.ts +17 -0
- package/dist/internal/event-log.d.ts.map +1 -1
- package/dist/internal/event-log.js +32 -2
- package/dist/internal/process-memory-telemetry.d.ts +90 -0
- package/dist/internal/process-memory-telemetry.d.ts.map +1 -0
- package/dist/internal/process-memory-telemetry.js +259 -0
- package/dist/internal/server.d.ts.map +1 -1
- package/dist/internal/server.js +9 -10
- package/dist/internal/session-engine.d.ts +4 -0
- package/dist/internal/session-engine.d.ts.map +1 -1
- package/dist/internal/session-engine.js +19 -5
- package/dist/internal/slack-provision-client.d.ts +4 -4
- package/dist/internal/slack-provision-client.js +7 -7
- package/dist/playground/assets/index-Dox74yWJ.css +1 -0
- package/dist/playground/assets/{index-DEVTyeOu.js → index-Dq_HqyU-.js} +41 -41
- package/dist/playground/index.html +2 -2
- package/dist/storage.d.ts +36 -10
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +82 -6
- package/docs/ab.md +4 -3
- package/docs/evals.md +10 -7
- package/docs/reference/http-api.md +4 -3
- package/docs/storage.md +38 -22
- package/package.json +1 -1
- package/skills/setup-slack/SKILL.md +1 -1
- package/src/internal/cli-slack.ts +0 -5
- package/src/internal/event-log.ts +45 -7
- package/src/internal/process-memory-telemetry.ts +373 -0
- package/src/internal/server.ts +13 -0
- package/src/internal/session-engine.ts +20 -1
- package/src/internal/slack-provision-client.ts +7 -7
- package/src/storage.ts +130 -11
- package/dist/docs/assets/chunks/@localSearchIndexroot.DV5SAHnQ.js +0 -1
- package/dist/docs/assets/storage.md.CJYd5YmN.lean.js +0 -1
- package/dist/playground/assets/index-CiCmH6R-.css +0 -1
- /package/dist/docs/assets/{reference_http-api.md.DGrw_wOu.lean.js → reference_http-api.md.DMbdFGVQ.lean.js} +0 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process heap + event-log residency for hosted engines.
|
|
3
|
+
*
|
|
4
|
+
* Pod RSS (`runner_daemon.pod.memory_usage_bytes`) cannot show a V8 heap-cap
|
|
5
|
+
* OOM: the cgroup limit is tens of GB while Node aborts near
|
|
6
|
+
* `--max-old-space-size`. The live path is `/v1/health`: the control plane
|
|
7
|
+
* already probes every engine and emits `cursor.agent_serve.engine.*` gauges.
|
|
8
|
+
* An optional in-process sampler (`AGENT_SDK_MEMORY_METRICS=1`) still logs
|
|
9
|
+
* and, if a Datadog key happens to be in the pod, posts directly.
|
|
10
|
+
*
|
|
11
|
+
* `event_log.events_retained` is the in-process cache size (the OOM
|
|
12
|
+
* suspect). `event_log.events_total` is the durable recorded count. They
|
|
13
|
+
* are equal today; a later consume-and-drop change should split them.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { getHeapStatistics } from "node:v8";
|
|
17
|
+
import type { Attributes, Meter } from "@opentelemetry/api";
|
|
18
|
+
import type { EventLogMemoryStats } from "./event-log.js";
|
|
19
|
+
|
|
20
|
+
const DEFAULT_INTERVAL_MS = 15_000;
|
|
21
|
+
const DEFAULT_DD_SITE = "us5.datadoghq.com";
|
|
22
|
+
const DEFAULT_TIMEOUT_MS = 5_000;
|
|
23
|
+
const MB = 1024 * 1024;
|
|
24
|
+
|
|
25
|
+
export const MEMORY_METRIC_PREFIX = "agent_sdk.memory";
|
|
26
|
+
export const EVENT_LOG_METRIC_PREFIX = "agent_sdk.event_log";
|
|
27
|
+
|
|
28
|
+
export type ProcessMemorySample = {
|
|
29
|
+
heapUsedBytes: number;
|
|
30
|
+
heapTotalBytes: number;
|
|
31
|
+
heapLimitBytes: number;
|
|
32
|
+
rssBytes: number;
|
|
33
|
+
externalBytes: number;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type DatadogMemoryMetricsConfig = {
|
|
37
|
+
apiKey: string;
|
|
38
|
+
seriesUrl: string;
|
|
39
|
+
timeoutMs: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type ProcessMemoryTelemetryOptions = {
|
|
43
|
+
agentName: string;
|
|
44
|
+
getEventLogStats: () => EventLogMemoryStats;
|
|
45
|
+
logger?: (line: string) => void;
|
|
46
|
+
meter?: Meter;
|
|
47
|
+
env?: NodeJS.ProcessEnv;
|
|
48
|
+
intervalMs?: number;
|
|
49
|
+
now?: () => number;
|
|
50
|
+
memoryUsage?: () => {
|
|
51
|
+
heapUsed: number;
|
|
52
|
+
heapTotal: number;
|
|
53
|
+
rss: number;
|
|
54
|
+
external: number;
|
|
55
|
+
};
|
|
56
|
+
heapSizeLimit?: () => number;
|
|
57
|
+
postSeries?: (
|
|
58
|
+
config: DatadogMemoryMetricsConfig,
|
|
59
|
+
body: string
|
|
60
|
+
) => Promise<void>;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export function shouldStartProcessMemoryTelemetry(
|
|
64
|
+
env: NodeJS.ProcessEnv = process.env
|
|
65
|
+
): boolean {
|
|
66
|
+
const flag = env.AGENT_SDK_MEMORY_METRICS?.trim().toLowerCase();
|
|
67
|
+
return flag === "1" || flag === "true";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function resolveDatadogMemoryMetricsConfig(
|
|
71
|
+
env: NodeJS.ProcessEnv = process.env
|
|
72
|
+
): DatadogMemoryMetricsConfig | undefined {
|
|
73
|
+
const apiKey = env.DATADOG_API_KEY?.trim() || env.DD_API_KEY?.trim();
|
|
74
|
+
if (apiKey === undefined || apiKey === "") {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
const site = env.DD_SITE?.trim() || DEFAULT_DD_SITE;
|
|
78
|
+
return {
|
|
79
|
+
apiKey,
|
|
80
|
+
seriesUrl: `https://api.${site}/api/v1/series`,
|
|
81
|
+
timeoutMs: DEFAULT_TIMEOUT_MS,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function sampleProcessMemory(
|
|
86
|
+
args: {
|
|
87
|
+
memoryUsage?: ProcessMemoryTelemetryOptions["memoryUsage"];
|
|
88
|
+
heapSizeLimit?: ProcessMemoryTelemetryOptions["heapSizeLimit"];
|
|
89
|
+
} = {}
|
|
90
|
+
): ProcessMemorySample {
|
|
91
|
+
const usage = args.memoryUsage?.() ?? process.memoryUsage();
|
|
92
|
+
const heapLimitBytes =
|
|
93
|
+
args.heapSizeLimit?.() ?? getHeapStatistics().heap_size_limit;
|
|
94
|
+
return {
|
|
95
|
+
heapUsedBytes: usage.heapUsed,
|
|
96
|
+
heapTotalBytes: usage.heapTotal,
|
|
97
|
+
heapLimitBytes,
|
|
98
|
+
rssBytes: usage.rss,
|
|
99
|
+
externalBytes: usage.external,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type MemoryHealthFields = {
|
|
104
|
+
memory: ProcessMemorySample & { heapUsedRatio: number };
|
|
105
|
+
eventLog: EventLogMemoryStats;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export function heapUsedRatio(memory: ProcessMemorySample): number {
|
|
109
|
+
return memory.heapLimitBytes > 0
|
|
110
|
+
? memory.heapUsedBytes / memory.heapLimitBytes
|
|
111
|
+
: 0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function sumEventLogStats(
|
|
115
|
+
stats: readonly EventLogMemoryStats[]
|
|
116
|
+
): EventLogMemoryStats {
|
|
117
|
+
let residentSessions = 0;
|
|
118
|
+
let eventsRetained = 0;
|
|
119
|
+
let eventsTotal = 0;
|
|
120
|
+
for (const item of stats) {
|
|
121
|
+
residentSessions += item.residentSessions;
|
|
122
|
+
eventsRetained += item.eventsRetained;
|
|
123
|
+
eventsTotal += item.eventsTotal;
|
|
124
|
+
}
|
|
125
|
+
return { residentSessions, eventsRetained, eventsTotal };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Fields attached to `/v1/health` so the control plane can scrape them. */
|
|
129
|
+
export function buildMemoryHealthFields(
|
|
130
|
+
eventLog: EventLogMemoryStats,
|
|
131
|
+
sample: ProcessMemorySample = sampleProcessMemory()
|
|
132
|
+
): MemoryHealthFields {
|
|
133
|
+
return {
|
|
134
|
+
memory: { ...sample, heapUsedRatio: heapUsedRatio(sample) },
|
|
135
|
+
eventLog,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Best-effort health payload. Sampling or event-log stats must never fail
|
|
141
|
+
* `/v1/health`; omit the fields instead.
|
|
142
|
+
*/
|
|
143
|
+
export function collectMemoryHealthFields(args: {
|
|
144
|
+
eventLog: () => EventLogMemoryStats;
|
|
145
|
+
sample?: () => ProcessMemorySample;
|
|
146
|
+
}): MemoryHealthFields | Record<string, never> {
|
|
147
|
+
try {
|
|
148
|
+
return buildMemoryHealthFields(
|
|
149
|
+
args.eventLog(),
|
|
150
|
+
args.sample === undefined ? sampleProcessMemory() : args.sample()
|
|
151
|
+
);
|
|
152
|
+
} catch {
|
|
153
|
+
return {};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function formatMemoryLogLine(args: {
|
|
158
|
+
agentName: string;
|
|
159
|
+
memory: ProcessMemorySample;
|
|
160
|
+
eventLog: EventLogMemoryStats;
|
|
161
|
+
}): string {
|
|
162
|
+
const ratio = heapUsedRatio(args.memory);
|
|
163
|
+
return (
|
|
164
|
+
`[agent-sdk:memory] agent=${args.agentName}` +
|
|
165
|
+
` heap_used_mb=${mb(args.memory.heapUsedBytes)}` +
|
|
166
|
+
` heap_total_mb=${mb(args.memory.heapTotalBytes)}` +
|
|
167
|
+
` heap_limit_mb=${mb(args.memory.heapLimitBytes)}` +
|
|
168
|
+
` heap_used_ratio=${ratio.toFixed(3)}` +
|
|
169
|
+
` rss_mb=${mb(args.memory.rssBytes)}` +
|
|
170
|
+
` event_log.sessions=${args.eventLog.residentSessions}` +
|
|
171
|
+
` event_log.retained=${args.eventLog.eventsRetained}` +
|
|
172
|
+
` event_log.total=${args.eventLog.eventsTotal}`
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function buildDatadogGaugeSeries(args: {
|
|
177
|
+
agentName: string;
|
|
178
|
+
memory: ProcessMemorySample;
|
|
179
|
+
eventLog: EventLogMemoryStats;
|
|
180
|
+
timestampSec: number;
|
|
181
|
+
}): Array<{
|
|
182
|
+
metric: string;
|
|
183
|
+
type: "gauge";
|
|
184
|
+
points: [[number, number]];
|
|
185
|
+
tags: string[];
|
|
186
|
+
}> {
|
|
187
|
+
const tags = [`agent_sdk.agent:${sanitizeTag(args.agentName)}`];
|
|
188
|
+
const heapRatio = heapUsedRatio(args.memory);
|
|
189
|
+
const points: Array<{ name: string; value: number }> = [
|
|
190
|
+
{
|
|
191
|
+
name: `${MEMORY_METRIC_PREFIX}.heap_used_bytes`,
|
|
192
|
+
value: args.memory.heapUsedBytes,
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
name: `${MEMORY_METRIC_PREFIX}.heap_total_bytes`,
|
|
196
|
+
value: args.memory.heapTotalBytes,
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
name: `${MEMORY_METRIC_PREFIX}.heap_limit_bytes`,
|
|
200
|
+
value: args.memory.heapLimitBytes,
|
|
201
|
+
},
|
|
202
|
+
{ name: `${MEMORY_METRIC_PREFIX}.rss_bytes`, value: args.memory.rssBytes },
|
|
203
|
+
{
|
|
204
|
+
name: `${MEMORY_METRIC_PREFIX}.external_bytes`,
|
|
205
|
+
value: args.memory.externalBytes,
|
|
206
|
+
},
|
|
207
|
+
{ name: `${MEMORY_METRIC_PREFIX}.heap_used_ratio`, value: heapRatio },
|
|
208
|
+
{
|
|
209
|
+
name: `${EVENT_LOG_METRIC_PREFIX}.resident_sessions`,
|
|
210
|
+
value: args.eventLog.residentSessions,
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: `${EVENT_LOG_METRIC_PREFIX}.events_retained`,
|
|
214
|
+
value: args.eventLog.eventsRetained,
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
name: `${EVENT_LOG_METRIC_PREFIX}.events_total`,
|
|
218
|
+
value: args.eventLog.eventsTotal,
|
|
219
|
+
},
|
|
220
|
+
];
|
|
221
|
+
return points.map((point) => ({
|
|
222
|
+
metric: point.name,
|
|
223
|
+
type: "gauge",
|
|
224
|
+
points: [[args.timestampSec, point.value]],
|
|
225
|
+
tags,
|
|
226
|
+
}));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Start the sampler. Returns a stop function. */
|
|
230
|
+
export function startProcessMemoryTelemetry(
|
|
231
|
+
options: ProcessMemoryTelemetryOptions
|
|
232
|
+
): () => void {
|
|
233
|
+
const env = options.env ?? process.env;
|
|
234
|
+
const logger = options.logger ?? (() => {});
|
|
235
|
+
const intervalMs = options.intervalMs ?? DEFAULT_INTERVAL_MS;
|
|
236
|
+
const datadog = resolveDatadogMemoryMetricsConfig(env);
|
|
237
|
+
const attributes: Attributes = { "agent_sdk.agent": options.agentName };
|
|
238
|
+
let stopped = false;
|
|
239
|
+
let inFlight: Promise<void> | undefined;
|
|
240
|
+
|
|
241
|
+
const sample = (): void => {
|
|
242
|
+
if (stopped) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const memory = sampleProcessMemory({
|
|
246
|
+
memoryUsage: options.memoryUsage,
|
|
247
|
+
heapSizeLimit: options.heapSizeLimit,
|
|
248
|
+
});
|
|
249
|
+
const eventLog = options.getEventLogStats();
|
|
250
|
+
logger(
|
|
251
|
+
formatMemoryLogLine({ agentName: options.agentName, memory, eventLog })
|
|
252
|
+
);
|
|
253
|
+
recordOtel(options.meter, memory, eventLog, attributes);
|
|
254
|
+
if (datadog === undefined) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const body = JSON.stringify({
|
|
258
|
+
series: buildDatadogGaugeSeries({
|
|
259
|
+
agentName: options.agentName,
|
|
260
|
+
memory,
|
|
261
|
+
eventLog,
|
|
262
|
+
timestampSec: Math.floor((options.now?.() ?? Date.now()) / 1000),
|
|
263
|
+
}),
|
|
264
|
+
});
|
|
265
|
+
const post = options.postSeries ?? defaultPostSeries;
|
|
266
|
+
inFlight = post(datadog, body).catch((error: unknown) => {
|
|
267
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
268
|
+
logger(`[agent-sdk:memory] datadog flush failed: ${message}`);
|
|
269
|
+
});
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
sample();
|
|
273
|
+
const timer = setInterval(sample, intervalMs);
|
|
274
|
+
timer.unref();
|
|
275
|
+
return () => {
|
|
276
|
+
stopped = true;
|
|
277
|
+
clearInterval(timer);
|
|
278
|
+
void inFlight;
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function recordOtel(
|
|
283
|
+
meter: Meter | undefined,
|
|
284
|
+
memory: ProcessMemorySample,
|
|
285
|
+
eventLog: EventLogMemoryStats,
|
|
286
|
+
attributes: Attributes
|
|
287
|
+
): void {
|
|
288
|
+
if (meter === undefined) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
histogram(meter, `${MEMORY_METRIC_PREFIX}.heap_used_bytes`).record(
|
|
292
|
+
memory.heapUsedBytes,
|
|
293
|
+
attributes
|
|
294
|
+
);
|
|
295
|
+
histogram(meter, `${MEMORY_METRIC_PREFIX}.heap_limit_bytes`).record(
|
|
296
|
+
memory.heapLimitBytes,
|
|
297
|
+
attributes
|
|
298
|
+
);
|
|
299
|
+
histogram(meter, `${MEMORY_METRIC_PREFIX}.rss_bytes`).record(
|
|
300
|
+
memory.rssBytes,
|
|
301
|
+
attributes
|
|
302
|
+
);
|
|
303
|
+
histogram(meter, `${EVENT_LOG_METRIC_PREFIX}.events_retained`).record(
|
|
304
|
+
eventLog.eventsRetained,
|
|
305
|
+
attributes
|
|
306
|
+
);
|
|
307
|
+
histogram(meter, `${EVENT_LOG_METRIC_PREFIX}.events_total`).record(
|
|
308
|
+
eventLog.eventsTotal,
|
|
309
|
+
attributes
|
|
310
|
+
);
|
|
311
|
+
histogram(meter, `${EVENT_LOG_METRIC_PREFIX}.resident_sessions`).record(
|
|
312
|
+
eventLog.residentSessions,
|
|
313
|
+
attributes
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
type HistogramInstrument = {
|
|
318
|
+
record: (value: number, attributes?: Attributes) => void;
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
const histograms = new WeakMap<Meter, Map<string, HistogramInstrument>>();
|
|
322
|
+
|
|
323
|
+
function histogram(meter: Meter, name: string): HistogramInstrument {
|
|
324
|
+
let byName = histograms.get(meter);
|
|
325
|
+
if (byName === undefined) {
|
|
326
|
+
byName = new Map();
|
|
327
|
+
histograms.set(meter, byName);
|
|
328
|
+
}
|
|
329
|
+
const existing = byName.get(name);
|
|
330
|
+
if (existing !== undefined) {
|
|
331
|
+
return existing;
|
|
332
|
+
}
|
|
333
|
+
const unit = name.endsWith("_bytes") ? "By" : "{count}";
|
|
334
|
+
const created = meter.createHistogram(name, { unit });
|
|
335
|
+
byName.set(name, created);
|
|
336
|
+
return created;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async function defaultPostSeries(
|
|
340
|
+
config: DatadogMemoryMetricsConfig,
|
|
341
|
+
body: string
|
|
342
|
+
): Promise<void> {
|
|
343
|
+
const controller = new AbortController();
|
|
344
|
+
const timer = setTimeout(() => controller.abort(), config.timeoutMs);
|
|
345
|
+
try {
|
|
346
|
+
const response = await fetch(config.seriesUrl, {
|
|
347
|
+
method: "POST",
|
|
348
|
+
headers: {
|
|
349
|
+
"Content-Type": "application/json",
|
|
350
|
+
"DD-API-KEY": config.apiKey,
|
|
351
|
+
},
|
|
352
|
+
body,
|
|
353
|
+
signal: controller.signal,
|
|
354
|
+
});
|
|
355
|
+
if (!response.ok) {
|
|
356
|
+
throw new Error(`HTTP ${response.status}`);
|
|
357
|
+
}
|
|
358
|
+
} finally {
|
|
359
|
+
clearTimeout(timer);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function mb(bytes: number): string {
|
|
364
|
+
return (bytes / MB).toFixed(1);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function sanitizeTag(value: string): string {
|
|
368
|
+
const normalized = value
|
|
369
|
+
.trim()
|
|
370
|
+
.toLowerCase()
|
|
371
|
+
.replace(/[^a-z0-9_\-./:]+/g, "_");
|
|
372
|
+
return normalized === "" ? "unknown" : normalized;
|
|
373
|
+
}
|
package/src/internal/server.ts
CHANGED
|
@@ -135,6 +135,10 @@ import {
|
|
|
135
135
|
playgroundDevBasePath,
|
|
136
136
|
startPlaygroundDevServer,
|
|
137
137
|
} from "./playground/toolchain.js";
|
|
138
|
+
import {
|
|
139
|
+
collectMemoryHealthFields,
|
|
140
|
+
sumEventLogStats,
|
|
141
|
+
} from "./process-memory-telemetry.js";
|
|
138
142
|
import {
|
|
139
143
|
ReminderRunner,
|
|
140
144
|
ReminderValidationError,
|
|
@@ -162,6 +166,13 @@ import {
|
|
|
162
166
|
} from "./session-engine.js";
|
|
163
167
|
import { sessionUrlsForRequest } from "./session-urls.js";
|
|
164
168
|
|
|
169
|
+
function memoryHealthFields(engines: readonly SessionEngine[]) {
|
|
170
|
+
return collectMemoryHealthFields({
|
|
171
|
+
eventLog: () =>
|
|
172
|
+
sumEventLogStats(engines.map((engine) => engine.eventLogMemoryStats())),
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
165
176
|
const MAX_BODY_BYTES = 32 * 1024 * 1024;
|
|
166
177
|
|
|
167
178
|
/** `GET /v1/artifacts` limit defaults / ceiling. */
|
|
@@ -636,6 +647,7 @@ export async function startServer(
|
|
|
636
647
|
slug,
|
|
637
648
|
name: project.name,
|
|
638
649
|
})),
|
|
650
|
+
...memoryHealthFields(runtimes.map((runtime) => runtime.engine)),
|
|
639
651
|
}),
|
|
640
652
|
});
|
|
641
653
|
}
|
|
@@ -1049,6 +1061,7 @@ function mountAgentRoutes(
|
|
|
1049
1061
|
ok: true,
|
|
1050
1062
|
name: project.name,
|
|
1051
1063
|
version: packageVersion(),
|
|
1064
|
+
...memoryHealthFields([engine]),
|
|
1052
1065
|
}),
|
|
1053
1066
|
});
|
|
1054
1067
|
|
|
@@ -91,7 +91,7 @@ import {
|
|
|
91
91
|
namespacedKv,
|
|
92
92
|
storageCasKv,
|
|
93
93
|
} from "./durable-session-store.js";
|
|
94
|
-
import { EventLogStore } from "./event-log.js";
|
|
94
|
+
import { type EventLogMemoryStats, EventLogStore } from "./event-log.js";
|
|
95
95
|
import { TurnEventMapper } from "./event-mapper.js";
|
|
96
96
|
import { selectFrameworkStorageDefinition } from "./framework-storage-selection.js";
|
|
97
97
|
import { createHostFilesApi } from "./host-files.js";
|
|
@@ -112,6 +112,10 @@ import { validateToolInput } from "./json-schema.js";
|
|
|
112
112
|
import { LedgerServing, newLedgerPodId } from "./ledger-serving.js";
|
|
113
113
|
import { McpHost } from "./mcp-host.js";
|
|
114
114
|
import type { OtelCollector } from "./otel/collector.js";
|
|
115
|
+
import {
|
|
116
|
+
shouldStartProcessMemoryTelemetry,
|
|
117
|
+
startProcessMemoryTelemetry,
|
|
118
|
+
} from "./process-memory-telemetry.js";
|
|
115
119
|
import {
|
|
116
120
|
buildSessionContextBlock,
|
|
117
121
|
SCHEDULE_CHANNEL_PREFIX,
|
|
@@ -364,6 +368,7 @@ export class SessionEngine {
|
|
|
364
368
|
private readonly abCollector: ABCollector;
|
|
365
369
|
private readonly otel: OtelCollector | undefined;
|
|
366
370
|
private readonly otelHost: HostOtelSessionApi;
|
|
371
|
+
private readonly stopMemoryTelemetry: (() => void) | undefined;
|
|
367
372
|
private reminderApi: HostContext["reminders"];
|
|
368
373
|
private evalsApi: HostContext["evals"];
|
|
369
374
|
private readonly kvApi: HostContext["kv"];
|
|
@@ -480,6 +485,14 @@ export class SessionEngine {
|
|
|
480
485
|
}
|
|
481
486
|
this.ledgerServing = this.buildLedgerServing(options);
|
|
482
487
|
this.ledgerServing?.start();
|
|
488
|
+
this.stopMemoryTelemetry = shouldStartProcessMemoryTelemetry()
|
|
489
|
+
? startProcessMemoryTelemetry({
|
|
490
|
+
agentName: options.project.name,
|
|
491
|
+
getEventLogStats: () => this.logs.memoryStats(),
|
|
492
|
+
logger: this.logger,
|
|
493
|
+
meter: options.otelMeter,
|
|
494
|
+
})
|
|
495
|
+
: undefined;
|
|
483
496
|
}
|
|
484
497
|
|
|
485
498
|
/**
|
|
@@ -3445,8 +3458,14 @@ export class SessionEngine {
|
|
|
3445
3458
|
});
|
|
3446
3459
|
}
|
|
3447
3460
|
|
|
3461
|
+
/** In-process event-log residency (OOM-suspect cache vs durable count). */
|
|
3462
|
+
eventLogMemoryStats(): EventLogMemoryStats {
|
|
3463
|
+
return this.logs.memoryStats();
|
|
3464
|
+
}
|
|
3465
|
+
|
|
3448
3466
|
/** Drain running turns, dispatches, and pending writes, then dispose. */
|
|
3449
3467
|
async close(): Promise<void> {
|
|
3468
|
+
this.stopMemoryTelemetry?.();
|
|
3450
3469
|
this.approvals.cancelAll("deny");
|
|
3451
3470
|
// Ledger deliveries spawn turns, so settle them before activeTurns.
|
|
3452
3471
|
await this.ledgerServing?.close();
|
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
*
|
|
7
7
|
* Auth is the user API key sent directly as the bearer (the routes are
|
|
8
8
|
* `wrapUserApiRoute` on the backend); there is no token exchange here.
|
|
9
|
-
* The endpoints
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* The endpoints ride `requireAgentServeAccess` and 404 `{ error: "Not found" }`
|
|
10
|
+
* when Agent Serve is off — same mapping as other agent-sdk deploy 404s.
|
|
11
|
+
* Expected provisioning failures carry a stable JSON `code`
|
|
12
12
|
* (`not_connected`, `insufficient_scopes`, `manager_reauth_required`,
|
|
13
|
-
* `app_conflict`, `
|
|
13
|
+
* `app_conflict`, `ratelimited`, `invalid_app_id`, …)
|
|
14
14
|
* which flows through on {@link SlackProvisionApiError.code} so the CLI
|
|
15
15
|
* can branch.
|
|
16
16
|
*/
|
|
@@ -398,8 +398,8 @@ function mapErrorResponse(
|
|
|
398
398
|
{ status: 404 }
|
|
399
399
|
);
|
|
400
400
|
}
|
|
401
|
-
//
|
|
402
|
-
//
|
|
401
|
+
// `{ error: "Not found" }` is Agent Serve off (`requireAgentServeAccess`),
|
|
402
|
+
// not a Slack-only enrollment bar.
|
|
403
403
|
if (serverError !== undefined && serverError !== "Not found") {
|
|
404
404
|
return new SlackProvisionApiError(serverError, {
|
|
405
405
|
status: 404,
|
|
@@ -407,7 +407,7 @@ function mapErrorResponse(
|
|
|
407
407
|
});
|
|
408
408
|
}
|
|
409
409
|
return new SlackProvisionApiError(
|
|
410
|
-
`Not found —
|
|
410
|
+
`Not found — the agent_serve_mvp feature gate may not be enabled for your team.`,
|
|
411
411
|
{ status: 404 }
|
|
412
412
|
);
|
|
413
413
|
}
|
package/src/storage.ts
CHANGED
|
@@ -9,9 +9,12 @@
|
|
|
9
9
|
* The sink is a plain key-value store — put/get/delete/list, no schema —
|
|
10
10
|
* plus a conditional-write group ({@link StorageConfig.cas}, the substrate
|
|
11
11
|
* for framework coordination; optional for plain storage, required by
|
|
12
|
-
* coordination features)
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* coordination features). Implementing that small KV core is enough for
|
|
13
|
+
* FULL functionality: eval-run and A/B history are derived over the core
|
|
14
|
+
* when the optional dedicated tables ({@link StorageConfig.evals} and
|
|
15
|
+
* {@link StorageConfig.abs}) are not authored — those groups exist as
|
|
16
|
+
* backend-native optimizations (a real table, an analytics pipeline),
|
|
17
|
+
* not as required-or-lose-history hooks:
|
|
15
18
|
*
|
|
16
19
|
* ```ts
|
|
17
20
|
* import { defineStorage } from "@anysphere/agent-serve/storage";
|
|
@@ -207,13 +210,20 @@ export interface StorageConfig {
|
|
|
207
210
|
/** Timing knobs; see {@link StoragePolicy}. */
|
|
208
211
|
policy?: StoragePolicy;
|
|
209
212
|
/**
|
|
210
|
-
* Dedicated eval-runs table
|
|
211
|
-
*
|
|
213
|
+
* Dedicated eval-runs table — a backend-native optimization, not a
|
|
214
|
+
* requirement. When omitted, `defineStorage` derives the table over the
|
|
215
|
+
* KV core (one key per run under `agentkit/v1/{agent}/eval-runs/`),
|
|
216
|
+
* which needs core `put`, `delete`, and `list`; only when the core
|
|
217
|
+
* lacks `delete` or `list` does playground eval history stay in process
|
|
218
|
+
* memory (lost on restart).
|
|
212
219
|
*/
|
|
213
220
|
evals?: StorageEvalsTable;
|
|
214
221
|
/**
|
|
215
|
-
* Dedicated A/B table
|
|
216
|
-
*
|
|
222
|
+
* Dedicated A/B table — a backend-native optimization, not a
|
|
223
|
+
* requirement. When omitted, `defineStorage` derives it over the KV
|
|
224
|
+
* core: `putSample` and `putSnapshot` need only core `put` (always
|
|
225
|
+
* present), `getSnapshot` additionally needs core `get`. Session event
|
|
226
|
+
* logs remain the assignment/fold source of truth either way.
|
|
217
227
|
*/
|
|
218
228
|
abs?: StorageABTable;
|
|
219
229
|
/**
|
|
@@ -249,7 +259,8 @@ export interface StorageConfig {
|
|
|
249
259
|
/**
|
|
250
260
|
* All entries under a key prefix, in ascending key order. Optional —
|
|
251
261
|
* required for the **startup bulk restore** (sessions + event streams)
|
|
252
|
-
* and
|
|
262
|
+
* and, together with `delete`, for the derived eval-runs table
|
|
263
|
+
* (playground eval history).
|
|
253
264
|
*/
|
|
254
265
|
list?(
|
|
255
266
|
prefix: string,
|
|
@@ -326,7 +337,91 @@ export function defineStorage(config: StorageConfig): StorageDefinition {
|
|
|
326
337
|
}
|
|
327
338
|
// Validate eagerly so a bad policy fails at discovery, not first flush.
|
|
328
339
|
resolveStoragePolicy(config.policy);
|
|
329
|
-
|
|
340
|
+
// A backend that implements only the KV core gets full functionality:
|
|
341
|
+
// the dedicated tables are backend-native optimizations, derived over
|
|
342
|
+
// the core when absent. Authored groups always win.
|
|
343
|
+
const derivedEvals =
|
|
344
|
+
config.evals === undefined ? deriveEvalsTable(config) : undefined;
|
|
345
|
+
const derivedAbs =
|
|
346
|
+
config.abs === undefined ? deriveAbsTable(config) : undefined;
|
|
347
|
+
return brandDefinition("storage", {
|
|
348
|
+
...config,
|
|
349
|
+
...(derivedEvals === undefined ? {} : { evals: derivedEvals }),
|
|
350
|
+
...(derivedAbs === undefined ? {} : { abs: derivedAbs }),
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Eval-runs table over the KV core: one key per run under
|
|
356
|
+
* {@link storageKeys.evalRun}, hydrate via prefix `list`, prune via
|
|
357
|
+
* `delete`. Requires core `put` + `delete` + `list`; without `delete` or
|
|
358
|
+
* `list` there is no derivation (pruning or hydration would silently not
|
|
359
|
+
* work) and eval history stays in process memory.
|
|
360
|
+
*/
|
|
361
|
+
function deriveEvalsTable(
|
|
362
|
+
config: StorageConfig
|
|
363
|
+
): StorageEvalsTable | undefined {
|
|
364
|
+
if (config.delete === undefined || config.list === undefined) {
|
|
365
|
+
return undefined;
|
|
366
|
+
}
|
|
367
|
+
return {
|
|
368
|
+
put: (run, ctx) =>
|
|
369
|
+
config.put(
|
|
370
|
+
storageKeys.evalRun(ctx.agentName, run.runId),
|
|
371
|
+
run as unknown as JsonValue,
|
|
372
|
+
ctx
|
|
373
|
+
),
|
|
374
|
+
delete: (runId, ctx) =>
|
|
375
|
+
config.delete?.(storageKeys.evalRun(ctx.agentName, runId), ctx),
|
|
376
|
+
list: async (ctx) => {
|
|
377
|
+
const entries =
|
|
378
|
+
(await config.list?.(storageKeys.evalRunsPrefix(ctx.agentName), ctx)) ??
|
|
379
|
+
[];
|
|
380
|
+
return entries.map((entry) => entry.value as unknown as EvalRunSnapshot);
|
|
381
|
+
},
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* A/B table over the KV core. Each sample lands as its own key (identity
|
|
387
|
+
* = experiment + sessionId + at, matching the dedicated table's row
|
|
388
|
+
* uniqueness): appending to one array value would need read-modify-write
|
|
389
|
+
* — lost updates under concurrent writers without CAS, and unbounded
|
|
390
|
+
* value growth — while a key per sample keeps `putSample` a blind put
|
|
391
|
+
* with real append-only semantics. The aggregate snapshot lives at one
|
|
392
|
+
* fixed key via plain `put` (last-write-wins is correct for "latest").
|
|
393
|
+
* Requires only core `put`; `getSnapshot` is included when core `get`
|
|
394
|
+
* exists.
|
|
395
|
+
*/
|
|
396
|
+
function deriveAbsTable(config: StorageConfig): StorageABTable {
|
|
397
|
+
const get = config.get;
|
|
398
|
+
return {
|
|
399
|
+
putSample: (sample, ctx) =>
|
|
400
|
+
config.put(
|
|
401
|
+
storageKeys.abSample(
|
|
402
|
+
ctx.agentName,
|
|
403
|
+
sample.experiment,
|
|
404
|
+
sample.sessionId,
|
|
405
|
+
sample.at
|
|
406
|
+
),
|
|
407
|
+
sample as unknown as JsonValue,
|
|
408
|
+
ctx
|
|
409
|
+
),
|
|
410
|
+
putSnapshot: (snapshot, ctx) =>
|
|
411
|
+
config.put(
|
|
412
|
+
storageKeys.abSnapshot(ctx.agentName),
|
|
413
|
+
snapshot as unknown as JsonValue,
|
|
414
|
+
ctx
|
|
415
|
+
),
|
|
416
|
+
...(get === undefined
|
|
417
|
+
? {}
|
|
418
|
+
: {
|
|
419
|
+
getSnapshot: async (ctx: StorageContext) => {
|
|
420
|
+
const value = await get(storageKeys.abSnapshot(ctx.agentName), ctx);
|
|
421
|
+
return (value ?? undefined) as ABSnapshot | undefined;
|
|
422
|
+
},
|
|
423
|
+
}),
|
|
424
|
+
};
|
|
330
425
|
}
|
|
331
426
|
|
|
332
427
|
// ============================================================================
|
|
@@ -357,9 +452,14 @@ export const STORAGE_KEY_ROOT = "agentkit/v1" as const;
|
|
|
357
452
|
* | `agentkit/v1/{agent}/session-events/{sessionId}/{index}` | `SessionEvent[]` chunk (index = first event's index, zero-padded) |
|
|
358
453
|
* | `agentkit/v1/{agent}/continuation/{channelId}/{token}` | `{ sessionId }` |
|
|
359
454
|
* | `agentkit/v1/{agent}/kv/{key}` | Author JSON via {@link HostContext.kv} |
|
|
455
|
+
* | `agentkit/v1/{agent}/eval-runs/{runId}` | `EvalRunSnapshot` (derived `evals` table) |
|
|
456
|
+
* | `agentkit/v1/{agent}/ab-samples/{experiment}/{sessionId}/{at}` | `ABMetricSample` (derived `abs` table, append-only) |
|
|
457
|
+
* | `agentkit/v1/{agent}/ab-snapshot` | `ABSnapshot` (derived `abs` table, LWW) |
|
|
360
458
|
*
|
|
361
|
-
*
|
|
362
|
-
*
|
|
459
|
+
* The `eval-runs` / `ab-samples` / `ab-snapshot` families are minted only
|
|
460
|
+
* by the tables `defineStorage` derives over the KV core; a backend that
|
|
461
|
+
* authors dedicated {@link StorageConfig.evals} / {@link StorageConfig.abs}
|
|
462
|
+
* groups never sees them.
|
|
363
463
|
*/
|
|
364
464
|
export const storageKeys = {
|
|
365
465
|
session: (agent: string, sessionId: string): string =>
|
|
@@ -387,6 +487,25 @@ export const storageKeys = {
|
|
|
387
487
|
kv: (agent: string, key: string): string =>
|
|
388
488
|
`${STORAGE_KEY_ROOT}/${agent}/kv/${keySegment(key)}`,
|
|
389
489
|
kvPrefix: (agent: string): string => `${STORAGE_KEY_ROOT}/${agent}/kv/`,
|
|
490
|
+
/** One playground eval-run snapshot (derived `evals` table). */
|
|
491
|
+
evalRun: (agent: string, runId: string): string =>
|
|
492
|
+
`${STORAGE_KEY_ROOT}/${agent}/eval-runs/${keySegment(runId)}`,
|
|
493
|
+
evalRunsPrefix: (agent: string): string =>
|
|
494
|
+
`${STORAGE_KEY_ROOT}/${agent}/eval-runs/`,
|
|
495
|
+
/**
|
|
496
|
+
* One A/B metric sample (derived `abs` table); the segments mirror the
|
|
497
|
+
* dedicated table's row identity (experiment + sessionId + at).
|
|
498
|
+
*/
|
|
499
|
+
abSample: (
|
|
500
|
+
agent: string,
|
|
501
|
+
experiment: string,
|
|
502
|
+
sessionId: string,
|
|
503
|
+
at: string
|
|
504
|
+
): string =>
|
|
505
|
+
`${STORAGE_KEY_ROOT}/${agent}/ab-samples/${keySegment(experiment)}/${keySegment(sessionId)}/${keySegment(at)}`,
|
|
506
|
+
/** Latest aggregate A/B snapshot (derived `abs` table, LWW). */
|
|
507
|
+
abSnapshot: (agent: string): string =>
|
|
508
|
+
`${STORAGE_KEY_ROOT}/${agent}/ab-snapshot`,
|
|
390
509
|
} as const;
|
|
391
510
|
|
|
392
511
|
/**
|