stitchkit 0.76.2 → 0.78.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 (74) hide show
  1. package/dist/agent-runtime-coding-tools.js +1 -1
  2. package/dist/agent-runtime-harness.js +6 -6
  3. package/dist/agent-runtime.js +11 -11
  4. package/dist/application/decisions.d.ts +89 -0
  5. package/dist/application/decisions.d.ts.map +1 -0
  6. package/dist/application/kernel.d.ts +35 -1
  7. package/dist/application/kernel.d.ts.map +1 -1
  8. package/dist/application/keyspace.d.ts +11 -1
  9. package/dist/application/keyspace.d.ts.map +1 -1
  10. package/dist/application/schedule.d.ts.map +1 -1
  11. package/dist/application/server-resource.d.ts.map +1 -1
  12. package/dist/application-diagnostic-journal.d.ts +21 -0
  13. package/dist/application-diagnostic-journal.d.ts.map +1 -0
  14. package/dist/application-diagnostic-journal.js +714 -0
  15. package/dist/application-opentelemetry.js +92 -5
  16. package/dist/application-schemas.d.ts +15 -0
  17. package/dist/application-schemas.d.ts.map +1 -0
  18. package/dist/application-schemas.js +26 -0
  19. package/dist/application.d.ts +3 -3
  20. package/dist/application.d.ts.map +1 -1
  21. package/dist/application.js +1832 -844
  22. package/dist/cli.js +7 -7
  23. package/dist/contract/index.js +9 -7
  24. package/dist/files/boundary.d.ts.map +1 -1
  25. package/dist/files.js +1 -1
  26. package/dist/{index-3z73fh2c.js → index-1ckavx4h.js} +49 -1
  27. package/dist/{index-s2rchahr.js → index-31k1ns1s.js} +4 -4
  28. package/dist/{index-m668wzyc.js → index-4ntp63ps.js} +4 -4
  29. package/dist/{index-1rxswfbv.js → index-56fm73re.js} +1 -1
  30. package/dist/{index-ezmn6ac6.js → index-5r0ak8fr.js} +3 -3
  31. package/dist/{index-f6n5n7nz.js → index-7z69kyrh.js} +10 -87
  32. package/dist/{index-k2zczx1g.js → index-93xp5tgg.js} +1 -1
  33. package/dist/{index-bfcpjw20.js → index-actkayzn.js} +7 -3
  34. package/dist/index-anyqnqcv.js +79 -0
  35. package/dist/{index-7qy2ex0m.js → index-cwp38wxn.js} +1 -1
  36. package/dist/{index-35z5h2ty.js → index-dmfn9m4x.js} +125 -4
  37. package/dist/index-h4pj6fta.js +87 -0
  38. package/dist/{index-jqtsc9mj.js → index-j6mfaw26.js} +2 -2
  39. package/dist/{index-58jzmnn4.js → index-k45qqrdh.js} +513 -39
  40. package/dist/{index-8eywc9zv.js → index-kw12fsbc.js} +0 -1
  41. package/dist/index-n1g2xm6a.js +203 -0
  42. package/dist/{index-t8qyvrvg.js → index-n7apa6sy.js} +12 -12
  43. package/dist/{index-s4c8wy8m.js → index-qaajjfp6.js} +8 -8
  44. package/dist/{index-nemjkxjp.js → index-s5tn4kdv.js} +14 -10
  45. package/dist/index-wfq8zerm.js +92 -0
  46. package/dist/index.js +53 -1215
  47. package/dist/internal/deadline.d.ts +17 -0
  48. package/dist/internal/deadline.d.ts.map +1 -0
  49. package/dist/internal/decision.d.ts +31 -0
  50. package/dist/internal/decision.d.ts.map +1 -0
  51. package/dist/live/events.d.ts +8 -13
  52. package/dist/live/events.d.ts.map +1 -1
  53. package/dist/live.d.ts +1 -1
  54. package/dist/live.d.ts.map +1 -1
  55. package/dist/live.js +12 -89
  56. package/dist/node.js +5 -6
  57. package/dist/observability/context.d.ts.map +1 -1
  58. package/dist/observability/index.js +3 -3
  59. package/dist/primitives.js +2 -1
  60. package/dist/remote.js +3 -6
  61. package/dist/server/event-bus.d.ts +3 -2
  62. package/dist/server/event-bus.d.ts.map +1 -1
  63. package/dist/server/index.js +31 -30
  64. package/dist/testing.js +1562 -87
  65. package/dist/tool-invoker.js +6 -6
  66. package/dist/tools.js +66 -23
  67. package/llms-full.txt +276 -6
  68. package/package.json +12 -4
  69. package/dist/index-3cwck0rm.js +0 -897
  70. package/dist/index-7b188kmz.js +0 -49
  71. package/dist/index-a916km3r.js +0 -45
  72. package/dist/index-hvftzz91.js +0 -721
  73. package/dist/{index-da1aqnhb.js → index-aewy3x5j.js} +3 -3
  74. package/dist/{index-nt1mp8km.js → index-efm026pw.js} +3 -3
package/dist/testing.js CHANGED
@@ -1,18 +1,27 @@
1
+ import {
2
+ ApplicationIdSchema,
3
+ ApplicationShutdownResultSchema,
4
+ ApplicationSnapshotSchema
5
+ } from "./index-anyqnqcv.js";
1
6
  import {
2
7
  AgentMessageSchema,
3
8
  AgentRunSchema
4
9
  } from "./index-x1th9s8c.js";
5
10
  import {
6
- createApplication
7
- } from "./index-hvftzz91.js";
8
- import"./index-2k4yrqkc.js";
9
- import"./index-8eywc9zv.js";
11
+ managedResourceDependencyId
12
+ } from "./index-2k4yrqkc.js";
13
+ import {
14
+ DEFAULT_STREAM_LINE_BYTES,
15
+ inputIsQuery,
16
+ parseSSE,
17
+ readBoundedUtf8Lines
18
+ } from "./index-56fm73re.js";
10
19
  import {
11
20
  RealtimeRequestDisconnectedError,
12
21
  RealtimeRequestInvalidAcknowledgementError,
13
- RealtimeRequestTimeoutError
14
- } from "./index-7b188kmz.js";
15
- import"./index-3z73fh2c.js";
22
+ RealtimeRequestTimeoutError,
23
+ ShutdownOptionsSchema
24
+ } from "./index-1ckavx4h.js";
16
25
  import {
17
26
  mcpProjectionCandidate,
18
27
  prepareProjectedMcpTools,
@@ -21,21 +30,642 @@ import {
21
30
  import {
22
31
  toJsonSchema
23
32
  } from "./index-cby4ar3v.js";
33
+ import"./index-efm026pw.js";
24
34
  import {
25
- createClient,
26
- createClients
27
- } from "./index-58jzmnn4.js";
28
- import"./index-1rxswfbv.js";
29
- import"./index-nt1mp8km.js";
30
- import {
31
- joinRoutePath
35
+ ContractStreamFrameSchema,
36
+ DEFAULT_CONTRACT_STREAM_FRAME_BYTES,
37
+ joinRoutePath,
38
+ parseTrailingWildcard
32
39
  } from "./index-vj3vvpaa.js";
33
- import"./index-0w9abg87.js";
34
40
  import {
35
- isRecord
41
+ isRecord,
42
+ mapObject,
43
+ typedEntries
36
44
  } from "./index-qyrqwr4c.js";
45
+ import {
46
+ AppError
47
+ } from "./index-0w9abg87.js";
37
48
  import"./index-6k1937bx.js";
38
49
 
50
+ // src/browser/cancellation.ts
51
+ class RequestCancellationError extends Error {
52
+ cause;
53
+ constructor(cause) {
54
+ super(cause === "caller" ? "Request was aborted" : "Request timed out");
55
+ this.cause = cause;
56
+ this.name = "RequestCancellationError";
57
+ }
58
+ }
59
+ function createRequestCancellation(caller, timeoutMs) {
60
+ if (!caller && timeoutMs === undefined) {
61
+ return { signal: undefined, run: (operation) => operation() };
62
+ }
63
+ const timeoutController = timeoutMs === undefined ? undefined : new AbortController;
64
+ const signal = caller && timeoutController ? AbortSignal.any([caller, timeoutController.signal]) : caller ?? timeoutController?.signal;
65
+ let cause;
66
+ let timer;
67
+ const abortFromCaller = () => {
68
+ if (cause)
69
+ return;
70
+ cause = "caller";
71
+ };
72
+ if (caller?.aborted)
73
+ abortFromCaller();
74
+ else
75
+ caller?.addEventListener("abort", abortFromCaller, { once: true });
76
+ if (timeoutMs !== undefined && timeoutController) {
77
+ timer = setTimeout(() => {
78
+ if (cause)
79
+ return;
80
+ cause = "timeout";
81
+ timeoutController.abort(new DOMException("Request timed out", "TimeoutError"));
82
+ }, timeoutMs);
83
+ }
84
+ return {
85
+ signal,
86
+ async run(operation) {
87
+ try {
88
+ if (cause === "caller")
89
+ throw cancellationError(cause);
90
+ return await operation(signal);
91
+ } catch (error) {
92
+ if (cause)
93
+ throw cancellationError(cause);
94
+ throw error;
95
+ } finally {
96
+ if (timer !== undefined)
97
+ clearTimeout(timer);
98
+ caller?.removeEventListener("abort", abortFromCaller);
99
+ }
100
+ }
101
+ };
102
+ }
103
+ function cancellationError(cause) {
104
+ return new RequestCancellationError(cause);
105
+ }
106
+
107
+ // src/browser/http.ts
108
+ import ky, {
109
+ isHTTPError,
110
+ isNetworkError,
111
+ isTimeoutError
112
+ } from "ky";
113
+
114
+ // src/browser/request-id.ts
115
+ var REQUEST_ID_HEADER = "x-request-id";
116
+ function responseTraceId(response) {
117
+ return response?.headers.get(REQUEST_ID_HEADER) ?? undefined;
118
+ }
119
+
120
+ // src/browser/http.ts
121
+ var API_ERROR_BRAND = Symbol.for("stitchkit.ApiError");
122
+ function messageForCode(code, message) {
123
+ return message !== undefined && message.length > 0 ? message : `${code} (no message supplied)`;
124
+ }
125
+
126
+ class ApiError extends Error {
127
+ code;
128
+ status;
129
+ details;
130
+ hint;
131
+ traceId;
132
+ constructor(code, status = 0, details, message, hint, traceId, options) {
133
+ super(messageForCode(code, message), options);
134
+ this.code = code;
135
+ this.status = status;
136
+ this.details = details;
137
+ this.hint = hint;
138
+ this.traceId = traceId;
139
+ this.name = "ApiError";
140
+ Object.defineProperty(this, API_ERROR_BRAND, { value: true });
141
+ }
142
+ static is(error) {
143
+ return typeof error === "object" && error !== null && API_ERROR_BRAND in error;
144
+ }
145
+ }
146
+ function refuseLocally(path, message) {
147
+ return new ApiError("VALIDATION_ERROR", 0, { issues: [{ path, code: "invalid_type", message }] }, message);
148
+ }
149
+ function parseApiErrorBody(body) {
150
+ if (!isRecord(body) || !isRecord(body.error))
151
+ return null;
152
+ const error = body.error;
153
+ if (typeof error.code !== "string")
154
+ return null;
155
+ return {
156
+ code: error.code,
157
+ message: typeof error.message === "string" ? error.message : undefined,
158
+ details: error.details,
159
+ hint: typeof error.hint === "string" ? error.hint : undefined
160
+ };
161
+ }
162
+
163
+ // src/browser/client-multipart.ts
164
+ function isFileDescriptor(value) {
165
+ return typeof value === "object" && value !== null && !(value instanceof Blob) && "uri" in value && typeof value.uri === "string" && "name" in value && typeof value.name === "string" && "type" in value && typeof value.type === "string";
166
+ }
167
+ function isMultipartFile(value) {
168
+ return value instanceof Blob || isFileDescriptor(value);
169
+ }
170
+ function appendMultipartFile(form, field, file) {
171
+ const sink = form;
172
+ sink.append(field, file);
173
+ }
174
+ function appendFormFields(formData, values, skipKeys) {
175
+ for (const [key, value] of Object.entries(values)) {
176
+ if (skipKeys.has(key) || value === undefined || value === null)
177
+ continue;
178
+ formData.append(key, typeof value === "string" ? value : JSON.stringify(value));
179
+ }
180
+ }
181
+ function buildMultipartForm(descriptor, values) {
182
+ const formData = new FormData;
183
+ const fileFields = new Set(Object.keys(descriptor.files));
184
+ for (const [field, policy] of Object.entries(descriptor.files)) {
185
+ const value = values[field];
186
+ if (value === undefined) {
187
+ if (policy.required !== false) {
188
+ throw refuseLocally(field, `Missing multipart file field: ${field}`);
189
+ }
190
+ continue;
191
+ }
192
+ if (policy.multiple === true) {
193
+ if (!Array.isArray(value) || value.length === 0) {
194
+ throw refuseLocally(field, `Multipart file field "${field}" must be a non-empty array`);
195
+ }
196
+ for (const file of value) {
197
+ if (!isMultipartFile(file)) {
198
+ throw refuseLocally(field, `Invalid multipart file field: ${field}`);
199
+ }
200
+ appendMultipartFile(formData, field, file);
201
+ }
202
+ continue;
203
+ }
204
+ if (!isMultipartFile(value)) {
205
+ throw refuseLocally(field, `Invalid multipart file field: ${field}`);
206
+ }
207
+ appendMultipartFile(formData, field, value);
208
+ }
209
+ appendFormFields(formData, values, fileFields);
210
+ return formData;
211
+ }
212
+
213
+ // src/browser/client-url.ts
214
+ function isParamArray(value) {
215
+ return Array.isArray(value) && value.every((item) => typeof item === "string" || typeof item === "number");
216
+ }
217
+ function collectQueryParams(args, endpoint) {
218
+ const params = {};
219
+ let hasParams = false;
220
+ for (const [key, value] of Object.entries(args)) {
221
+ if (value === undefined || value === null)
222
+ continue;
223
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || isParamArray(value)) {
224
+ params[key] = value;
225
+ hasParams = true;
226
+ continue;
227
+ }
228
+ const what = Array.isArray(value) ? "an array with non-primitive items" : typeof value === "object" ? "a nested object" : `a ${typeof value}`;
229
+ throw refuseLocally(key, `${endpoint.method} ${endpoint.path}: input field "${key}" is ${what} — it cannot ` + "travel as a query parameter. GET / DELETE input must be flat (string / number / " + "boolean, or an array of string / number); flatten the field or move the " + "operation to a body verb (POST).");
230
+ }
231
+ return hasParams ? params : undefined;
232
+ }
233
+ function hasStringKeys(args, keys) {
234
+ for (const key of keys) {
235
+ if (typeof args[key] !== "string")
236
+ return false;
237
+ }
238
+ return true;
239
+ }
240
+ function resolvePathPrefix(config, args) {
241
+ if (!config?.pathPrefix)
242
+ return "";
243
+ if (typeof config.pathPrefix === "string")
244
+ return config.pathPrefix;
245
+ const keys = config.stripPrefixKeys ?? [];
246
+ if (!hasStringKeys(args, keys)) {
247
+ const missing = keys.find((key) => typeof args[key] !== "string");
248
+ throw refuseLocally(String(missing), `Missing path prefix key: ${missing}`);
249
+ }
250
+ return config.pathPrefix(args);
251
+ }
252
+ function extractParamNames(path) {
253
+ const matches = path.match(/:(\w+)/g);
254
+ const names = matches ? matches.map((match) => match.slice(1)) : [];
255
+ const wildcard = parseTrailingWildcard(path);
256
+ if (wildcard)
257
+ names.push(wildcard.name);
258
+ return names;
259
+ }
260
+ function fillPathParams(path, args) {
261
+ const wildcard = parseTrailingWildcard(path);
262
+ let filled = path.replace(/:(\w+)/g, (_, key) => {
263
+ const value = args[key];
264
+ if (value === undefined || value === null) {
265
+ throw refuseLocally(String(key), `Missing path param: ${key}`);
266
+ }
267
+ return encodeURIComponent(String(value));
268
+ });
269
+ if (!wildcard)
270
+ return filled;
271
+ const wildcardValue = args[wildcard.name];
272
+ if (wildcardValue === undefined || wildcardValue === null) {
273
+ throw refuseLocally(wildcard.name, `Missing path param: ${wildcard.name}`);
274
+ }
275
+ const remainder = String(wildcardValue).split("/").map((segment) => encodeURIComponent(segment)).join("/");
276
+ filled = `${filled.slice(0, -(wildcard.name.length + 1))}${remainder}`;
277
+ return filled;
278
+ }
279
+ function stripConsumedArgs(args, path, scopeKeys) {
280
+ const consumed = new Set(scopeKeys);
281
+ for (const name of extractParamNames(path))
282
+ consumed.add(name);
283
+ const remaining = {};
284
+ for (const [key, value] of Object.entries(args)) {
285
+ if (!consumed.has(key) && value !== undefined)
286
+ remaining[key] = value;
287
+ }
288
+ return remaining;
289
+ }
290
+ function appendQuery(relativeUrl, params) {
291
+ if (!params)
292
+ return relativeUrl;
293
+ const search = new URLSearchParams;
294
+ for (const [key, value] of Object.entries(params)) {
295
+ if (Array.isArray(value)) {
296
+ for (const item of value)
297
+ search.append(key, String(item));
298
+ } else {
299
+ search.set(key, String(value));
300
+ }
301
+ }
302
+ return search.size > 0 ? `${relativeUrl}?${search}` : relativeUrl;
303
+ }
304
+ function planClientRequest(endpoint, contractPrefix, args, config) {
305
+ let pathPrefix = resolvePathPrefix(config, args);
306
+ if (pathPrefix && !pathPrefix.endsWith("/"))
307
+ pathPrefix += "/";
308
+ if (pathPrefix.startsWith("/"))
309
+ pathPrefix = pathPrefix.slice(1);
310
+ const endpointPath = endpoint.path === "/" ? "" : endpoint.path;
311
+ let relativeUrl = fillPathParams(`${pathPrefix}${contractPrefix}${endpointPath}`, args);
312
+ if (relativeUrl.endsWith("/"))
313
+ relativeUrl = relativeUrl.slice(0, -1);
314
+ const remainingArgs = stripConsumedArgs(args, endpoint.path, config?.stripPrefixKeys ?? []);
315
+ if (inputIsQuery(endpoint.method)) {
316
+ relativeUrl = appendQuery(relativeUrl, collectQueryParams(remainingArgs, endpoint));
317
+ }
318
+ return { relativeUrl, remainingArgs };
319
+ }
320
+ function joinClientBaseUrl(baseUrl, relativeUrl) {
321
+ const base = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
322
+ const path = relativeUrl.startsWith("/") ? relativeUrl : `/${relativeUrl}`;
323
+ return `${base}${path}`;
324
+ }
325
+ // src/browser/stream.ts
326
+ function parseFailure(raw, error, onParseError) {
327
+ const failure = error instanceof Error ? error : new Error(String(error));
328
+ if (!onParseError)
329
+ throw failure;
330
+ onParseError(raw, failure);
331
+ }
332
+ async function* parseNDJSON(response, options) {
333
+ try {
334
+ for await (const rawLine of readBoundedUtf8Lines(response, options?.maxLineBytes ?? DEFAULT_STREAM_LINE_BYTES, options?.finalLine === "require-newline")) {
335
+ const line = (rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine).trim();
336
+ if (line === "")
337
+ continue;
338
+ try {
339
+ yield JSON.parse(line);
340
+ } catch (error) {
341
+ parseFailure(line, error, options?.onParseError);
342
+ }
343
+ }
344
+ } catch (error) {
345
+ parseFailure("", error, options?.onParseError);
346
+ }
347
+ }
348
+
349
+ // src/browser/contract-stream.ts
350
+ function sourceFor(response, descriptor) {
351
+ const options = {
352
+ maxLineBytes: descriptor.maxFrameBytes ?? DEFAULT_CONTRACT_STREAM_FRAME_BYTES,
353
+ finalLine: descriptor.finalLine
354
+ };
355
+ return descriptor.format === "sse" ? parseSSE(response, options) : parseNDJSON(response, options);
356
+ }
357
+ async function* readContractStream(response, descriptor, abort) {
358
+ let ended = false;
359
+ let terminalSeen = descriptor.terminal === undefined;
360
+ const source = sourceFor(response, descriptor);
361
+ try {
362
+ for (;; ) {
363
+ const next = await source.next();
364
+ if (next.done)
365
+ break;
366
+ let candidate = next.value;
367
+ if (descriptor.framing !== "item") {
368
+ const parsedFrame = ContractStreamFrameSchema.safeParse(candidate);
369
+ if (!parsedFrame.success) {
370
+ throw new ApiError("STREAM_PROTOCOL_ERROR", 0, undefined, "Stream frame did not match the protocol envelope");
371
+ }
372
+ const frame = parsedFrame.data;
373
+ if (frame.type === "error") {
374
+ throw new ApiError(frame.error.code, 0, frame.error.details, frame.error.message, frame.error.hint);
375
+ }
376
+ if (frame.type === "end") {
377
+ if (!terminalSeen) {
378
+ throw new ApiError("STREAM_TERMINAL_MISSING", 0, undefined, "Stream completed before its declared terminal item");
379
+ }
380
+ ended = true;
381
+ return;
382
+ }
383
+ candidate = frame.data;
384
+ }
385
+ const parsedItem = descriptor.item.safeParse(candidate);
386
+ if (!parsedItem.success) {
387
+ throw new ApiError("STREAM_ITEM_INVALID", 0, undefined, "Stream item did not match its contract");
388
+ }
389
+ const item = parsedItem.data;
390
+ const terminal = descriptor.terminal?.safeParse(item).success ?? false;
391
+ if (terminal)
392
+ terminalSeen = true;
393
+ if (terminal && descriptor.completion === "terminal") {
394
+ abort();
395
+ await source.return(undefined);
396
+ ended = true;
397
+ }
398
+ yield item;
399
+ if (ended)
400
+ return;
401
+ }
402
+ if (descriptor.framing === "item") {
403
+ if (terminalSeen)
404
+ return;
405
+ throw new ApiError("STREAM_TERMINAL_MISSING", 0, undefined, "Stream completed before its declared terminal item");
406
+ }
407
+ if (!ended) {
408
+ throw new ApiError("STREAM_TRUNCATED", 0, undefined, "Stream ended without its protocol terminal frame");
409
+ }
410
+ } finally {
411
+ abort();
412
+ await source.return(undefined);
413
+ }
414
+ }
415
+
416
+ class OwnedContractStream {
417
+ #inner;
418
+ #abort;
419
+ constructor(inner, abort) {
420
+ this.#inner = inner;
421
+ this.#abort = abort;
422
+ }
423
+ [Symbol.asyncIterator]() {
424
+ return this;
425
+ }
426
+ next() {
427
+ return this.#inner.next();
428
+ }
429
+ return() {
430
+ this.#abort();
431
+ return this.#inner.return(undefined);
432
+ }
433
+ throw(error) {
434
+ this.#abort();
435
+ return this.#inner.throw(error);
436
+ }
437
+ }
438
+ function parseContractStream(response, descriptor, abort) {
439
+ return new OwnedContractStream(readContractStream(response, descriptor, abort), abort);
440
+ }
441
+
442
+ // src/browser/client.ts
443
+ function withTimeout(options, endpoint) {
444
+ const responseType = endpoint.rawResponse || "stream" in endpoint ? "response" : endpoint.output ? undefined : "void";
445
+ if (endpoint.timeout === undefined && responseType === undefined)
446
+ return options;
447
+ return {
448
+ ...options,
449
+ ...endpoint.timeout !== undefined && { timeout: endpoint.timeout },
450
+ ...responseType && { responseType }
451
+ };
452
+ }
453
+ function withOutput(endpoint, result, abortStream) {
454
+ if (endpoint.rawResponse)
455
+ return result;
456
+ if ("stream" in endpoint && endpoint.stream) {
457
+ return result.then((value) => {
458
+ if (!(value instanceof Response)) {
459
+ abortStream?.();
460
+ throw new Error("Streaming endpoint did not return a Response");
461
+ }
462
+ return parseContractStream(value, endpoint.stream, abortStream ?? (() => {
463
+ return;
464
+ }));
465
+ }, (error) => {
466
+ abortStream?.();
467
+ throw error;
468
+ });
469
+ }
470
+ const schema = endpoint.output;
471
+ return result.then((value) => {
472
+ if (!schema) {
473
+ if (value === undefined || value === null)
474
+ return;
475
+ throw new Error("Server returned data for an endpoint with no output contract");
476
+ }
477
+ if (value === undefined) {
478
+ throw new Error("Server returned no body for an endpoint with an output contract");
479
+ }
480
+ return schema.parse(value);
481
+ });
482
+ }
483
+ function createClient(contract, configOrClient, contractConfig) {
484
+ const client = {};
485
+ const makeExecutor = isHttpAdapter(configOrClient) ? (endpoint) => createHttpExecutor(endpoint, contract.meta.prefix, configOrClient, contractConfig) : (endpoint) => createFetchExecutor(endpoint, contract.meta.prefix, configOrClient, contractConfig);
486
+ for (const [key, endpoint] of typedEntries(contract.endpoints)) {
487
+ if (endpoint.expose && !endpoint.expose.includes("HTTP"))
488
+ continue;
489
+ setClientMethod(client, key, createEndpointMethod(endpoint, makeExecutor(endpoint), contractConfig));
490
+ }
491
+ return client;
492
+ }
493
+ function createClients(contracts, configOrClient, contractConfig) {
494
+ return mapObject(contracts, (_key, contract) => createClient(contract, configOrClient, contractConfig));
495
+ }
496
+ function isHttpAdapter(value) {
497
+ return typeof value === "object" && "get" in value && typeof value.get === "function";
498
+ }
499
+ function setClientMethod(target, key, method) {
500
+ target[key] = method;
501
+ }
502
+ function createEndpointMethod(endpoint, execute, contractConfig) {
503
+ const hasScopedArguments = (contractConfig?.stripPrefixKeys?.length ?? 0) > 0;
504
+ if (endpointHasArguments(endpoint) || hasScopedArguments) {
505
+ const method2 = (requestArgs) => settle(execute, readClientRequestArgs(requestArgs), undefined);
506
+ return Object.assign(method2, {
507
+ withOptions: (...args) => {
508
+ refuseExtraWithOptionsArguments(endpoint, args.length, 2);
509
+ return settle(execute, readClientRequestArgs(args[0]), readClientRequestOptions(args[1]));
510
+ }
511
+ });
512
+ }
513
+ const method = () => settle(execute, {}, undefined);
514
+ return Object.assign(method, {
515
+ withOptions: (...args) => {
516
+ refuseExtraWithOptionsArguments(endpoint, args.length, 1);
517
+ return settle(execute, {}, readClientRequestOptions(args[0]));
518
+ }
519
+ });
520
+ }
521
+ function settle(execute, requestArgs, options) {
522
+ try {
523
+ return execute(requestArgs, options);
524
+ } catch (error) {
525
+ return Promise.reject(error);
526
+ }
527
+ }
528
+ function refuseExtraWithOptionsArguments(endpoint, received, expected) {
529
+ if (received <= expected)
530
+ return;
531
+ const shape = expected === 1 ? "withOptions(options)" : "withOptions(args, options)";
532
+ throw new TypeError(`${endpoint.method} ${endpoint.path}: this endpoint declares ${expected === 1 ? "no input" : "an input"}, so its method is ${shape} — it received ${received} arguments. ` + "An extra argument here is dropped, and a request options object in the dropped position " + "sends the request without them: an abort signal placed there never reaches the server.");
533
+ }
534
+ function createHttpExecutor(endpoint, prefix, client, config) {
535
+ const httpMethod = endpoint.method.toLowerCase();
536
+ return (requestArgs, options) => {
537
+ const plan = planClientRequest(endpoint, prefix, requestArgs, config);
538
+ const streamAbort = "stream" in endpoint ? new AbortController : undefined;
539
+ const requestOptions = streamAbort ? {
540
+ ...options,
541
+ signal: options?.signal ? AbortSignal.any([options.signal, streamAbort.signal]) : streamAbort.signal
542
+ } : options;
543
+ const finishStream = streamAbort ? () => streamAbort.abort() : undefined;
544
+ if (endpoint.multipart) {
545
+ if (httpMethod === "get" || httpMethod === "head" || httpMethod === "delete") {
546
+ throw new Error(`Multipart endpoint ${endpoint.method} ${endpoint.path} must be POST / PUT / PATCH`);
547
+ }
548
+ const formData = buildMultipartForm(endpoint.multipart, plan.remainingArgs);
549
+ return withOutput(endpoint, client[httpMethod](plan.relativeUrl, formData, withTimeout(requestOptions, endpoint)), finishStream);
550
+ }
551
+ if (httpMethod === "get" || httpMethod === "head") {
552
+ return withOutput(endpoint, client[httpMethod](plan.relativeUrl, withTimeout(requestOptions, endpoint)), finishStream);
553
+ }
554
+ if (httpMethod === "delete") {
555
+ return withOutput(endpoint, client.delete(plan.relativeUrl, withTimeout(requestOptions, endpoint)), finishStream);
556
+ }
557
+ return withOutput(endpoint, client[httpMethod](plan.relativeUrl, Object.keys(plan.remainingArgs).length > 0 ? plan.remainingArgs : undefined, withTimeout(requestOptions, endpoint)), finishStream);
558
+ };
559
+ }
560
+ function createFetchExecutor(endpoint, prefix, config, contractConfig) {
561
+ const executeFetch = config.fetch ?? globalThis.fetch;
562
+ return async (requestArgs, options) => {
563
+ const plan = planClientRequest(endpoint, prefix, requestArgs, contractConfig);
564
+ const url = joinClientBaseUrl(config.baseUrl, plan.relativeUrl);
565
+ const headers = {
566
+ Accept: "application/json",
567
+ ...typeof config.headers === "function" ? config.headers() : config.headers
568
+ };
569
+ const streamAbort = "stream" in endpoint ? new AbortController : undefined;
570
+ const requestSignal = streamAbort ? options?.signal ? AbortSignal.any([options.signal, streamAbort.signal]) : streamAbort.signal : options?.signal;
571
+ const openTimeoutMs = endpoint.timeout ?? config.timeout ?? 30000;
572
+ const cancellation = streamAbort ? {
573
+ signal: requestSignal,
574
+ async run(operation) {
575
+ let timedOut = false;
576
+ const timer = setTimeout(() => {
577
+ timedOut = true;
578
+ streamAbort.abort(new DOMException("Request timed out", "TimeoutError"));
579
+ }, openTimeoutMs);
580
+ try {
581
+ if (options?.signal?.aborted)
582
+ throw new RequestCancellationError("caller");
583
+ return await operation(requestSignal);
584
+ } catch (error) {
585
+ if (timedOut)
586
+ throw new RequestCancellationError("timeout");
587
+ if (options?.signal?.aborted)
588
+ throw new RequestCancellationError("caller");
589
+ throw error;
590
+ } finally {
591
+ clearTimeout(timer);
592
+ }
593
+ }
594
+ } : createRequestCancellation(requestSignal, openTimeoutMs);
595
+ const hasBody = endpoint.method !== "GET" && endpoint.method !== "HEAD" && endpoint.method !== "DELETE" && !endpoint.multipart && endpoint.input && Object.keys(plan.remainingArgs).length > 0;
596
+ if (hasBody)
597
+ headers["Content-Type"] = "application/json";
598
+ try {
599
+ return await cancellation.run(async (signal) => {
600
+ const body = endpoint.multipart ? buildMultipartForm(endpoint.multipart, plan.remainingArgs) : hasBody ? JSON.stringify(plan.remainingArgs) : undefined;
601
+ const res = await executeFetch(url, {
602
+ method: endpoint.method,
603
+ headers,
604
+ credentials: config.credentials,
605
+ signal,
606
+ ...body !== undefined && { body }
607
+ });
608
+ if (!res.ok) {
609
+ await throwForErrorResponse(res, config, { error: res.statusText });
610
+ }
611
+ if (endpoint.rawResponse)
612
+ return res;
613
+ if ("stream" in endpoint && endpoint.stream) {
614
+ return parseContractStream(res, endpoint.stream, () => streamAbort?.abort());
615
+ }
616
+ if (!endpoint.output) {
617
+ const text = await res.text();
618
+ if (text.length > 0) {
619
+ throw new Error("Server returned data for an endpoint with no output contract");
620
+ }
621
+ return;
622
+ }
623
+ return endpoint.output.parse(await res.json());
624
+ });
625
+ } catch (error) {
626
+ if (error instanceof RequestCancellationError) {
627
+ throw new ApiError(error.cause === "caller" ? "REQUEST_ABORTED" : "REQUEST_TIMEOUT", 0, undefined, error.message);
628
+ }
629
+ if (ApiError.is(error))
630
+ throw error;
631
+ const message = error instanceof Error ? error.message : undefined;
632
+ throw new ApiError("UNKNOWN_ERROR", 0, message ? { message } : undefined, message, undefined, undefined, { cause: error });
633
+ }
634
+ };
635
+ }
636
+ function endpointHasArguments(endpoint) {
637
+ return Boolean(endpoint.params || endpoint.input || endpoint.multipart);
638
+ }
639
+ function readClientRequestArgs(requestArgs) {
640
+ if (requestArgs !== undefined && !isRecord(requestArgs)) {
641
+ throw new TypeError("Endpoint arguments must be an object");
642
+ }
643
+ return requestArgs ?? {};
644
+ }
645
+ function readClientRequestOptions(value) {
646
+ if (!isRecord(value))
647
+ throw new TypeError("Client request options must be an object");
648
+ const signal = value.signal;
649
+ if (signal === undefined)
650
+ return {};
651
+ if (!isAbortSignal(signal)) {
652
+ throw new TypeError("Client request signal must be an AbortSignal");
653
+ }
654
+ return { signal };
655
+ }
656
+ function isAbortSignal(value) {
657
+ return typeof value === "object" && value !== null && "aborted" in value && typeof value.aborted === "boolean" && "addEventListener" in value && typeof value.addEventListener === "function" && "removeEventListener" in value && typeof value.removeEventListener === "function";
658
+ }
659
+ async function throwForErrorResponse(res, config, fallbackBody) {
660
+ const body = await res.json().catch(() => fallbackBody);
661
+ config.onError?.(res.status, body);
662
+ const parsed = parseApiErrorBody(body);
663
+ if (parsed) {
664
+ throw new ApiError(parsed.code, res.status, parsed.details, parsed.message, parsed.hint, responseTraceId(res));
665
+ }
666
+ throw new ApiError("HTTP_ERROR", res.status, { body }, undefined, undefined, responseTraceId(res));
667
+ }
668
+
39
669
  // src/agent-runtime/testing.ts
40
670
  function createAgentRaceBarrier(timeoutMs = 5000) {
41
671
  if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1) {
@@ -1022,6 +1652,851 @@ function observeManagedResource(resource, required, trace) {
1022
1652
  };
1023
1653
  }
1024
1654
 
1655
+ // src/application/kernel.ts
1656
+ import { z as z2 } from "zod";
1657
+
1658
+ // src/application/graph.ts
1659
+ function resolveResourceGraph(resources) {
1660
+ const entries = resources.map((resource, declarationIndex) => ({
1661
+ resource,
1662
+ id: ApplicationIdSchema.parse(resource.id),
1663
+ dependsOn: [...resource.dependsOn ?? []].map((dependency) => ApplicationIdSchema.parse(managedResourceDependencyId(dependency))),
1664
+ required: resource.required ?? true,
1665
+ declarationIndex
1666
+ }));
1667
+ const byId = new Map;
1668
+ for (const entry of entries) {
1669
+ if (byId.has(entry.id)) {
1670
+ throw new Error(`[stitchkit] createApplication: duplicate resource id "${entry.id}"`);
1671
+ }
1672
+ byId.set(entry.id, entry);
1673
+ }
1674
+ for (const entry of entries) {
1675
+ for (const dependencyId of entry.dependsOn) {
1676
+ const dependency = byId.get(dependencyId);
1677
+ if (!dependency) {
1678
+ throw new Error(`[stitchkit] createApplication: resource "${entry.id}" depends on missing resource "${dependencyId}"`);
1679
+ }
1680
+ if (entry.required && !dependency.required) {
1681
+ throw new Error(`[stitchkit] createApplication: required resource "${entry.id}" cannot depend on optional resource "${dependencyId}"`);
1682
+ }
1683
+ }
1684
+ }
1685
+ const pending = new Map(entries.map((entry) => [entry.id, entry]));
1686
+ const resolved = new Set;
1687
+ const ordered = [];
1688
+ while (pending.size > 0) {
1689
+ const ready = [...pending.values()].filter((entry) => entry.dependsOn.every((id) => resolved.has(id))).sort((left, right) => left.declarationIndex - right.declarationIndex);
1690
+ if (ready.length === 0) {
1691
+ throw new Error(`[stitchkit] createApplication: resource dependency cycle: ${[...pending.keys()].join(", ")}`);
1692
+ }
1693
+ for (const entry of ready) {
1694
+ pending.delete(entry.id);
1695
+ resolved.add(entry.id);
1696
+ ordered.push(entry);
1697
+ }
1698
+ }
1699
+ return ordered;
1700
+ }
1701
+
1702
+ // src/application/kernel.ts
1703
+ class ResourceCompletionBeforeReadyError extends Error {
1704
+ constructor(resourceId, cause) {
1705
+ super(`[stitchkit] resource "${resourceId}" completed before reaching readiness`, {
1706
+ ...cause !== undefined && { cause }
1707
+ });
1708
+ this.name = "ResourceCompletionBeforeReadyError";
1709
+ }
1710
+ }
1711
+
1712
+ class ApplicationStartupInterruptedError extends Error {
1713
+ constructor() {
1714
+ super("[stitchkit] application startup interrupted by shutdown");
1715
+ this.name = "ApplicationStartupInterruptedError";
1716
+ }
1717
+ }
1718
+ var ApplicationShutdownOptionsSchema = ShutdownOptionsSchema.pick({
1719
+ gracePeriodMs: true,
1720
+ forceTimeoutMs: true,
1721
+ signal: true
1722
+ });
1723
+ var ApplicationShutdownBudgetSchema = ShutdownOptionsSchema.pick({
1724
+ gracePeriodMs: true,
1725
+ forceTimeoutMs: true
1726
+ });
1727
+ var ApplicationRestartInputSchema = z2.object({
1728
+ resourceId: z2.string().min(1),
1729
+ gracePeriodMs: z2.number().int().nonnegative().optional(),
1730
+ forceTimeoutMs: z2.number().int().nonnegative().optional()
1731
+ }).strict().readonly();
1732
+ var ApplicationRestartOutcomeSchema = z2.enum(["restarted", "failed", "refused"]);
1733
+ var ApplicationRestartResultSchema = z2.object({
1734
+ resourceId: z2.string(),
1735
+ affected: z2.array(z2.string()).readonly(),
1736
+ outcome: ApplicationRestartOutcomeSchema,
1737
+ reason: z2.string().optional(),
1738
+ durationMs: z2.number().int().nonnegative()
1739
+ }).strict().readonly();
1740
+
1741
+ class ApplicationAdmissionError extends AppError {
1742
+ constructor() {
1743
+ super("APPLICATION_NOT_ACCEPTING", "Application is not accepting new operations", 503);
1744
+ this.name = "ApplicationAdmissionError";
1745
+ }
1746
+ }
1747
+ function isStartResult(value) {
1748
+ return typeof value === "object" && value !== null;
1749
+ }
1750
+ function waitForAbort(signal) {
1751
+ if (signal.aborted)
1752
+ return Promise.resolve();
1753
+ return new Promise((resolve) => signal.addEventListener("abort", () => resolve(), { once: true }));
1754
+ }
1755
+ async function untilDeadline(work, signal) {
1756
+ const result = await Promise.race([
1757
+ work.then((value) => ({ settled: true, value }), (error) => ({ settled: true, error })),
1758
+ waitForAbort(signal).then(() => ({ settled: false }))
1759
+ ]);
1760
+ return result;
1761
+ }
1762
+ function createApplication(config) {
1763
+ const id = ApplicationIdSchema.parse(config.id);
1764
+ const ordered = resolveResourceGraph(config.resources ?? []);
1765
+ const shutdownBudget = ApplicationShutdownBudgetSchema.parse(config.shutdown ?? {});
1766
+ const reverse = [...ordered].reverse();
1767
+ const reportFailure = (resourceId, phase, error) => {
1768
+ if (!config.onResourceFailure)
1769
+ return;
1770
+ try {
1771
+ Promise.resolve(config.onResourceFailure({ resourceId, phase, error })).catch(() => {
1772
+ return;
1773
+ });
1774
+ } catch {}
1775
+ };
1776
+ const published = new Map;
1777
+ const records = new Map;
1778
+ for (const entry of ordered) {
1779
+ records.set(entry.id, {
1780
+ entry,
1781
+ state: "registered",
1782
+ health: "unknown",
1783
+ healthReported: false,
1784
+ everHealthy: false,
1785
+ attempted: false,
1786
+ activated: false,
1787
+ closeInvoked: false,
1788
+ closed: false,
1789
+ failures: []
1790
+ });
1791
+ }
1792
+ const epoch = crypto.randomUUID();
1793
+ const listeners = new Set;
1794
+ const lifetimeAbort = new AbortController;
1795
+ const startupAbort = new AbortController;
1796
+ let lifecycle = "created";
1797
+ let revision = 0;
1798
+ let changedAt = new Date().toISOString();
1799
+ let accepting = false;
1800
+ let accepted = 0;
1801
+ let completed = 0;
1802
+ let pending = 0;
1803
+ let startPromise;
1804
+ let shutdownPromise;
1805
+ let shutdownRequested = false;
1806
+ let activationComplete = false;
1807
+ const pendingWaiters = new Set;
1808
+ const aggregateHealth = () => {
1809
+ if (lifecycle === "created" || lifecycle === "starting")
1810
+ return "unknown";
1811
+ let optionalUnhealthy = false;
1812
+ for (const record of records.values()) {
1813
+ if (record.entry.required && (record.state !== "ready" || record.health !== "healthy")) {
1814
+ return "unhealthy";
1815
+ }
1816
+ if (!record.entry.required && (record.state !== "ready" || record.health !== "healthy")) {
1817
+ optionalUnhealthy = true;
1818
+ }
1819
+ }
1820
+ return optionalUnhealthy ? "degraded" : "healthy";
1821
+ };
1822
+ const isReady = () => lifecycle === "ready" && [...records.values()].every((record) => !record.entry.required || record.state === "ready" && record.health === "healthy");
1823
+ const snapshot = () => ApplicationSnapshotSchema.parse({
1824
+ id,
1825
+ epoch,
1826
+ revision,
1827
+ lifecycle,
1828
+ health: aggregateHealth(),
1829
+ ready: isReady(),
1830
+ capturedAt: new Date().toISOString(),
1831
+ changedAt,
1832
+ admission: { accepting, accepted, completed, pending },
1833
+ resources: ordered.map((entry) => {
1834
+ const record = records.get(entry.id);
1835
+ if (!record)
1836
+ throw new Error("Managed resource record disappeared");
1837
+ return {
1838
+ id: entry.id,
1839
+ required: entry.required,
1840
+ dependsOn: entry.dependsOn,
1841
+ state: record.state,
1842
+ health: record.health,
1843
+ ready: record.state === "ready" && record.health === "healthy"
1844
+ };
1845
+ })
1846
+ });
1847
+ const publish = () => {
1848
+ revision += 1;
1849
+ changedAt = new Date().toISOString();
1850
+ const value = snapshot();
1851
+ for (const listener of listeners) {
1852
+ try {
1853
+ listener(value);
1854
+ } catch {}
1855
+ }
1856
+ if (config.onSnapshot) {
1857
+ Promise.resolve().then(() => config.onSnapshot?.(value)).catch(() => {});
1858
+ }
1859
+ };
1860
+ const contextFor = (record, options = {}) => ({
1861
+ applicationId: id,
1862
+ signal: options.signal ?? lifetimeAbort.signal,
1863
+ ...options.deadlineAt !== undefined && { deadlineAt: options.deadlineAt },
1864
+ ...options.forceDeadlineAt !== undefined && {
1865
+ forceDeadlineAt: options.forceDeadlineAt
1866
+ },
1867
+ now: () => performance.now(),
1868
+ use(resource) {
1869
+ const dependencyId = resource?.id;
1870
+ if (typeof dependencyId !== "string" || dependencyId.length === 0) {
1871
+ throw new Error(`[stitchkit] resource "${record.entry.id}": use() takes a managed resource, and this one has no id`);
1872
+ }
1873
+ if (!record.entry.dependsOn.includes(dependencyId)) {
1874
+ throw new Error(`[stitchkit] resource "${record.entry.id}" used "${dependencyId}" without declaring it in dependsOn`);
1875
+ }
1876
+ if (!published.has(dependencyId)) {
1877
+ throw new Error(`[stitchkit] resource "${record.entry.id}" used "${dependencyId}", which published no value from start()`);
1878
+ }
1879
+ return published.get(dependencyId);
1880
+ },
1881
+ reportHealth(health) {
1882
+ if (record.state === "stopped")
1883
+ return;
1884
+ record.health = health;
1885
+ record.healthReported = true;
1886
+ if (health === "healthy")
1887
+ record.everHealthy = true;
1888
+ accepting = activationComplete && !shutdownRequested && isReady();
1889
+ publish();
1890
+ }
1891
+ });
1892
+ const markLateCompletion = (record, failure) => {
1893
+ if (shutdownRequested || record.state === "stopping" || record.state === "stopped")
1894
+ return;
1895
+ if (failure) {
1896
+ record.failures.push("completion");
1897
+ reportFailure(record.entry.id, "completion", failure.error);
1898
+ }
1899
+ record.state = "failed";
1900
+ record.health = "unhealthy";
1901
+ accepting = activationComplete && isReady();
1902
+ publish();
1903
+ };
1904
+ const closeAttempted = async () => {
1905
+ const errors = [];
1906
+ const rollbackStartedAt = performance.now();
1907
+ const rollbackDeadlineAt = rollbackStartedAt + shutdownBudget.gracePeriodMs;
1908
+ const rollbackForceDeadlineAt = rollbackDeadlineAt + shutdownBudget.forceTimeoutMs;
1909
+ const rollbackAbort = new AbortController;
1910
+ const rollbackTimer = setTimeout(() => rollbackAbort.abort(), Math.max(0, rollbackForceDeadlineAt - performance.now()));
1911
+ try {
1912
+ await closeEachAttempted(rollbackAbort.signal, errors, {
1913
+ deadlineAt: rollbackDeadlineAt,
1914
+ forceDeadlineAt: rollbackForceDeadlineAt
1915
+ });
1916
+ } finally {
1917
+ clearTimeout(rollbackTimer);
1918
+ }
1919
+ return errors;
1920
+ };
1921
+ const closeEachAttempted = async (bound, errors, deadlines) => {
1922
+ for (const entry of reverse) {
1923
+ const record = records.get(entry.id);
1924
+ if (!record?.attempted || record.closed)
1925
+ continue;
1926
+ record.state = "stopping";
1927
+ try {
1928
+ record.closeInvoked = true;
1929
+ const settled = await untilDeadline(Promise.resolve(entry.resource.close?.(contextFor(record, { signal: startupAbort.signal, ...deadlines }))), bound);
1930
+ if (!settled.settled) {
1931
+ record.failures.push("close");
1932
+ record.state = "failed";
1933
+ const timedOut = new Error(`[stitchkit] resource "${entry.id}" did not finish closing during rollback`);
1934
+ reportFailure(entry.id, "close", timedOut);
1935
+ errors.push(timedOut);
1936
+ publish();
1937
+ break;
1938
+ }
1939
+ if (settled.error !== undefined)
1940
+ throw settled.error;
1941
+ record.closed = true;
1942
+ record.state = "stopped";
1943
+ } catch (error) {
1944
+ record.failures.push("close");
1945
+ record.state = "failed";
1946
+ reportFailure(entry.id, "close", error);
1947
+ errors.push(error);
1948
+ }
1949
+ publish();
1950
+ }
1951
+ };
1952
+ const startEach = async (entries, signal) => {
1953
+ for (const entry of entries) {
1954
+ if (shutdownRequested || signal.aborted) {
1955
+ throw new ApplicationStartupInterruptedError;
1956
+ }
1957
+ const record = records.get(entry.id);
1958
+ if (!record)
1959
+ throw new Error("Managed resource record disappeared");
1960
+ const dependencyFailed = entry.dependsOn.some((dependencyId) => records.get(dependencyId)?.state !== "ready");
1961
+ if (dependencyFailed) {
1962
+ record.state = "failed";
1963
+ record.health = "unhealthy";
1964
+ record.failures.push("start");
1965
+ publish();
1966
+ if (entry.required) {
1967
+ throw new Error(`[stitchkit] required resource "${entry.id}" has an unavailable dependency`);
1968
+ }
1969
+ continue;
1970
+ }
1971
+ record.attempted = true;
1972
+ record.state = "starting";
1973
+ publish();
1974
+ try {
1975
+ const started = await entry.resource.start(contextFor(record, { signal }));
1976
+ if (shutdownRequested || signal.aborted) {
1977
+ throw new ApplicationStartupInterruptedError;
1978
+ }
1979
+ if (isStartResult(started)) {
1980
+ record.runtime = started;
1981
+ if (started.value !== undefined)
1982
+ published.set(entry.id, started.value);
1983
+ let resourceReady = started.ready === undefined;
1984
+ let completionSettled = false;
1985
+ let completionFailure;
1986
+ const completion = started.completion?.then(() => {
1987
+ completionSettled = true;
1988
+ if (resourceReady)
1989
+ markLateCompletion(record);
1990
+ }, (error) => {
1991
+ completionSettled = true;
1992
+ completionFailure = error;
1993
+ if (resourceReady)
1994
+ markLateCompletion(record, { error });
1995
+ });
1996
+ if (started.ready && completion) {
1997
+ const readiness = started.ready.then(() => "ready");
1998
+ const completionBeforeReady = completion.then(() => "completion");
1999
+ const first = await Promise.race([readiness, completionBeforeReady]);
2000
+ if (first === "completion") {
2001
+ throw new ResourceCompletionBeforeReadyError(entry.id, completionFailure);
2002
+ }
2003
+ resourceReady = true;
2004
+ if (completionSettled) {
2005
+ throw new ResourceCompletionBeforeReadyError(entry.id, completionFailure);
2006
+ }
2007
+ } else if (started.ready) {
2008
+ await started.ready;
2009
+ resourceReady = true;
2010
+ } else if (completion) {}
2011
+ }
2012
+ if (shutdownRequested || signal.aborted) {
2013
+ throw new ApplicationStartupInterruptedError;
2014
+ }
2015
+ record.state = "ready";
2016
+ if (!record.healthReported) {
2017
+ record.health = "healthy";
2018
+ record.everHealthy = true;
2019
+ }
2020
+ publish();
2021
+ } catch (error) {
2022
+ const interrupted = shutdownRequested || signal.aborted;
2023
+ if (!(error instanceof ApplicationStartupInterruptedError)) {
2024
+ record.failures.push(error instanceof ResourceCompletionBeforeReadyError ? "completion" : record.runtime?.ready ? "ready" : "start");
2025
+ record.state = "failed";
2026
+ record.health = "unhealthy";
2027
+ reportFailure(entry.id, record.failures[record.failures.length - 1] ?? "start", error);
2028
+ publish();
2029
+ }
2030
+ if (interrupted || entry.required)
2031
+ throw error;
2032
+ }
2033
+ }
2034
+ };
2035
+ const activateEach = async (entries, signal) => {
2036
+ for (const entry of entries) {
2037
+ if (shutdownRequested || signal.aborted) {
2038
+ throw new ApplicationStartupInterruptedError;
2039
+ }
2040
+ const record = records.get(entry.id);
2041
+ if (record?.state !== "ready")
2042
+ continue;
2043
+ const dependencyUnavailable = entry.dependsOn.some((dependencyId) => {
2044
+ const dependency = records.get(dependencyId);
2045
+ return dependency?.state !== "ready" || !dependency.activated;
2046
+ });
2047
+ if (dependencyUnavailable) {
2048
+ record.failures.push("start");
2049
+ record.state = "failed";
2050
+ record.health = "unhealthy";
2051
+ publish();
2052
+ if (entry.required) {
2053
+ throw new Error(`[stitchkit] required resource "${entry.id}" has an unavailable activation dependency`);
2054
+ }
2055
+ continue;
2056
+ }
2057
+ try {
2058
+ await entry.resource.activate?.(contextFor(record));
2059
+ if (shutdownRequested || signal.aborted) {
2060
+ throw new ApplicationStartupInterruptedError;
2061
+ }
2062
+ record.activated = true;
2063
+ if (entry.required && (record.state !== "ready" || record.health !== "healthy")) {
2064
+ const observed = `${record.state}/${record.health}`;
2065
+ throw new Error(record.everHealthy ? `[stitchkit] required resource "${entry.id}" lost readiness during activation (${observed})` : `[stitchkit] required resource "${entry.id}" is not healthy (${observed}). A required resource must be healthy for the application to be ready; a resource that is expected to start degraded belongs behind \`required: false\`.`);
2066
+ }
2067
+ } catch (error) {
2068
+ const interrupted = shutdownRequested || signal.aborted;
2069
+ if (!(error instanceof ApplicationStartupInterruptedError)) {
2070
+ record.failures.push("start");
2071
+ record.state = "failed";
2072
+ record.health = "unhealthy";
2073
+ reportFailure(entry.id, "start", error);
2074
+ publish();
2075
+ }
2076
+ if (interrupted || entry.required)
2077
+ throw error;
2078
+ }
2079
+ }
2080
+ };
2081
+ const subtreeOf = (resourceId) => {
2082
+ const affected = new Set([resourceId]);
2083
+ for (const entry of ordered) {
2084
+ if (entry.dependsOn.some((dependencyId) => affected.has(dependencyId))) {
2085
+ affected.add(entry.id);
2086
+ }
2087
+ }
2088
+ return ordered.filter((entry) => affected.has(entry.id));
2089
+ };
2090
+ const closeOne = async (entry, record, signal, deadlines) => {
2091
+ const context = () => contextFor(record, { ...deadlines, signal });
2092
+ if (record.attempted && !record.closed) {
2093
+ const phase = async (work, what) => {
2094
+ const settled = await untilDeadline(Promise.resolve(work), signal);
2095
+ if (!settled.settled) {
2096
+ throw new Error(`[stitchkit] resource "${entry.id}" did not ${what} within the restart budget`);
2097
+ }
2098
+ if (settled.error !== undefined)
2099
+ throw settled.error;
2100
+ };
2101
+ await phase(entry.resource.stopAdmission?.(context()), "stop admitting");
2102
+ await phase(entry.resource.drain?.(context()), "drain");
2103
+ record.closeInvoked = true;
2104
+ await phase(entry.resource.close?.(context()), "close");
2105
+ }
2106
+ record.closed = false;
2107
+ record.closeInvoked = false;
2108
+ record.attempted = false;
2109
+ record.activated = false;
2110
+ record.state = "registered";
2111
+ record.health = "unknown";
2112
+ record.healthReported = false;
2113
+ record.runtime = undefined;
2114
+ published.delete(entry.id);
2115
+ };
2116
+ let restarting = Promise.resolve();
2117
+ const runRestart = async (input) => {
2118
+ const startedAt = Date.now();
2119
+ const parsed = ApplicationRestartInputSchema.parse(input);
2120
+ const affected = subtreeOf(parsed.resourceId);
2121
+ const affectedIds = affected.map((entry) => entry.id);
2122
+ const refuse = (reason) => ({
2123
+ resourceId: parsed.resourceId,
2124
+ affected: affectedIds,
2125
+ outcome: "refused",
2126
+ reason,
2127
+ durationMs: Date.now() - startedAt
2128
+ });
2129
+ if (!records.has(parsed.resourceId)) {
2130
+ return refuse(`no resource is registered as "${parsed.resourceId}"`);
2131
+ }
2132
+ if (shutdownRequested) {
2133
+ return refuse("the application is shutting down");
2134
+ }
2135
+ if (lifecycle !== "ready") {
2136
+ return refuse(`the application is ${lifecycle}, not ready`);
2137
+ }
2138
+ const budget = {
2139
+ gracePeriodMs: parsed.gracePeriodMs ?? shutdownBudget.gracePeriodMs,
2140
+ forceTimeoutMs: parsed.forceTimeoutMs ?? shutdownBudget.forceTimeoutMs
2141
+ };
2142
+ const closeDeadlineAt = performance.now() + budget.gracePeriodMs;
2143
+ const deadlines = {
2144
+ deadlineAt: closeDeadlineAt,
2145
+ forceDeadlineAt: closeDeadlineAt + budget.forceTimeoutMs
2146
+ };
2147
+ const restartAbort = new AbortController;
2148
+ const closeTimer = setTimeout(() => restartAbort.abort(), Math.max(0, deadlines.forceDeadlineAt - performance.now()));
2149
+ const stillFailed = () => affectedIds.filter((id2) => records.get(id2)?.state === "failed");
2150
+ try {
2151
+ for (const entry of [...affected].reverse()) {
2152
+ const record = records.get(entry.id);
2153
+ if (record)
2154
+ await closeOne(entry, record, restartAbort.signal, deadlines);
2155
+ }
2156
+ publish();
2157
+ await startEach(affected, restartAbort.signal);
2158
+ await activateEach(affected, restartAbort.signal);
2159
+ publish();
2160
+ const failed = stillFailed();
2161
+ if (failed.length > 0) {
2162
+ return {
2163
+ resourceId: parsed.resourceId,
2164
+ affected: affectedIds,
2165
+ outcome: "failed",
2166
+ reason: `did not come back: ${failed.join(", ")}`,
2167
+ durationMs: Date.now() - startedAt
2168
+ };
2169
+ }
2170
+ return {
2171
+ resourceId: parsed.resourceId,
2172
+ affected: affectedIds,
2173
+ outcome: "restarted",
2174
+ durationMs: Date.now() - startedAt
2175
+ };
2176
+ } catch (error) {
2177
+ restartAbort.abort();
2178
+ publish();
2179
+ return {
2180
+ resourceId: parsed.resourceId,
2181
+ affected: affectedIds,
2182
+ outcome: "failed",
2183
+ reason: error instanceof Error ? error.message : String(error),
2184
+ durationMs: Date.now() - startedAt
2185
+ };
2186
+ } finally {
2187
+ clearTimeout(closeTimer);
2188
+ }
2189
+ };
2190
+ const runStart = async () => {
2191
+ lifecycle = "starting";
2192
+ publish();
2193
+ let startFailure;
2194
+ try {
2195
+ await startEach(ordered, startupAbort.signal);
2196
+ lifecycle = "ready";
2197
+ publish();
2198
+ await activateEach(ordered, startupAbort.signal);
2199
+ if (shutdownRequested) {
2200
+ throw new ApplicationStartupInterruptedError;
2201
+ }
2202
+ if (!isReady()) {
2203
+ const blocking = [...records.values()].filter((record) => record.entry.required && (record.state !== "ready" || record.health !== "healthy")).map((record) => `${record.entry.id} (${record.state}/${record.health})`);
2204
+ if (blocking.length === 0)
2205
+ blocking.push("(none identified)");
2206
+ const chosen = [...records.values()].some((record) => record.entry.required && record.health !== "healthy" && !record.everHealthy);
2207
+ throw new Error(`[stitchkit] the application is not ready after startup — required ${blocking.length === 1 ? "resource" : "resources"} ${blocking.join(", ")}. A required resource must be healthy for the application to be ready; ${chosen ? "a resource that is expected to start degraded belongs behind `required: false`." : "this one was healthy and stopped being so — read `onResourceFailure` for the cause."}`);
2208
+ }
2209
+ activationComplete = true;
2210
+ accepting = isReady();
2211
+ publish();
2212
+ return snapshot();
2213
+ } catch (error) {
2214
+ startFailure = error;
2215
+ }
2216
+ if (!shutdownRequested) {
2217
+ const rollbackErrors = await closeAttempted();
2218
+ lifecycle = "failed";
2219
+ accepting = false;
2220
+ publish();
2221
+ if (rollbackErrors.length > 0) {
2222
+ throw new AggregateError([startFailure, ...rollbackErrors], "[stitchkit] application startup and rollback failed", { cause: startFailure });
2223
+ }
2224
+ }
2225
+ throw startFailure;
2226
+ };
2227
+ const start = () => {
2228
+ if (startPromise)
2229
+ return startPromise;
2230
+ if (lifecycle !== "created") {
2231
+ return Promise.reject(new Error(`[stitchkit] application cannot start from lifecycle "${lifecycle}"`));
2232
+ }
2233
+ startPromise = runStart();
2234
+ startPromise.catch(() => {
2235
+ return;
2236
+ });
2237
+ return startPromise;
2238
+ };
2239
+ const acquire = () => {
2240
+ if (!accepting || !isReady())
2241
+ return null;
2242
+ accepted += 1;
2243
+ pending += 1;
2244
+ publish();
2245
+ let released = false;
2246
+ return {
2247
+ get released() {
2248
+ return released;
2249
+ },
2250
+ release() {
2251
+ if (released)
2252
+ return;
2253
+ released = true;
2254
+ pending -= 1;
2255
+ completed += 1;
2256
+ if (pending === 0) {
2257
+ for (const waiter of pendingWaiters)
2258
+ waiter();
2259
+ pendingWaiters.clear();
2260
+ }
2261
+ publish();
2262
+ }
2263
+ };
2264
+ };
2265
+ const run = async (work) => {
2266
+ const lease = acquire();
2267
+ if (!lease)
2268
+ throw new ApplicationAdmissionError;
2269
+ try {
2270
+ return await work();
2271
+ } finally {
2272
+ lease.release();
2273
+ }
2274
+ };
2275
+ const waitForPending = () => {
2276
+ if (pending === 0)
2277
+ return Promise.resolve();
2278
+ return new Promise((resolve) => pendingWaiters.add(resolve));
2279
+ };
2280
+ const shutdown = (options) => {
2281
+ if (shutdownPromise)
2282
+ return shutdownPromise;
2283
+ const requested = options ?? {};
2284
+ const parsed = ApplicationShutdownOptionsSchema.parse({
2285
+ gracePeriodMs: requested.gracePeriodMs ?? shutdownBudget.gracePeriodMs,
2286
+ forceTimeoutMs: requested.forceTimeoutMs ?? shutdownBudget.forceTimeoutMs,
2287
+ ...requested.signal !== undefined && { signal: requested.signal }
2288
+ });
2289
+ const startedAt = performance.now();
2290
+ const graceDeadlineAt = startedAt + parsed.gracePeriodMs;
2291
+ const forceDeadlineAt = graceDeadlineAt + parsed.forceTimeoutMs;
2292
+ shutdownRequested = true;
2293
+ accepting = false;
2294
+ startupAbort.abort();
2295
+ lifecycle = lifecycle === "created" ? "stopping" : "draining";
2296
+ publish();
2297
+ shutdownPromise = (async () => {
2298
+ const gracefulAbort = new AbortController;
2299
+ let forcedReason;
2300
+ let gracefulFailed = [...records.values()].some((record) => record.attempted && record.closeInvoked && !record.closed);
2301
+ const force = (reason) => {
2302
+ if (forcedReason)
2303
+ return;
2304
+ forcedReason = reason;
2305
+ gracefulAbort.abort();
2306
+ lifetimeAbort.abort();
2307
+ };
2308
+ const graceTimer = setTimeout(() => force("deadline"), Math.max(0, graceDeadlineAt - performance.now()));
2309
+ const onExternalAbort = () => force("signal");
2310
+ parsed.signal?.addEventListener("abort", onExternalAbort, { once: true });
2311
+ if (parsed.signal?.aborted)
2312
+ force("signal");
2313
+ if (startPromise) {
2314
+ await untilDeadline(startPromise.catch(() => {
2315
+ return;
2316
+ }), gracefulAbort.signal);
2317
+ }
2318
+ const gracefulContext = (record) => contextFor(record, {
2319
+ signal: gracefulAbort.signal,
2320
+ deadlineAt: graceDeadlineAt,
2321
+ forceDeadlineAt
2322
+ });
2323
+ for (const entry of reverse) {
2324
+ if (gracefulAbort.signal.aborted)
2325
+ break;
2326
+ const record = records.get(entry.id);
2327
+ if (!record?.attempted || record.closed)
2328
+ continue;
2329
+ try {
2330
+ const result = await untilDeadline(Promise.resolve(entry.resource.stopAdmission?.(gracefulContext(record))), gracefulAbort.signal);
2331
+ if (!result.settled)
2332
+ break;
2333
+ if (result.error !== undefined)
2334
+ throw result.error;
2335
+ } catch (error) {
2336
+ record.failures.push("admission");
2337
+ reportFailure(entry.id, "admission", error);
2338
+ gracefulFailed = true;
2339
+ lifetimeAbort.abort();
2340
+ }
2341
+ }
2342
+ if (!gracefulAbort.signal.aborted && !gracefulFailed) {
2343
+ const result = await untilDeadline(waitForPending(), gracefulAbort.signal);
2344
+ if (!result.settled)
2345
+ force("deadline");
2346
+ }
2347
+ for (const entry of reverse) {
2348
+ if (gracefulAbort.signal.aborted || gracefulFailed)
2349
+ break;
2350
+ const record = records.get(entry.id);
2351
+ if (!record?.attempted || record.closed)
2352
+ continue;
2353
+ record.state = "stopping";
2354
+ publish();
2355
+ try {
2356
+ const drained = await untilDeadline(Promise.resolve(entry.resource.drain?.(gracefulContext(record))), gracefulAbort.signal);
2357
+ if (!drained.settled) {
2358
+ force("deadline");
2359
+ break;
2360
+ }
2361
+ if (drained.error !== undefined)
2362
+ throw drained.error;
2363
+ } catch (error) {
2364
+ record.failures.push("drain");
2365
+ reportFailure(entry.id, "drain", error);
2366
+ gracefulFailed = true;
2367
+ lifetimeAbort.abort();
2368
+ break;
2369
+ }
2370
+ }
2371
+ lifecycle = "stopping";
2372
+ publish();
2373
+ for (const entry of reverse) {
2374
+ if (gracefulAbort.signal.aborted || gracefulFailed)
2375
+ break;
2376
+ const record = records.get(entry.id);
2377
+ if (!record?.attempted || record.closed || record.closeInvoked)
2378
+ continue;
2379
+ try {
2380
+ record.closeInvoked = true;
2381
+ const closed = await untilDeadline(Promise.resolve(entry.resource.close?.(gracefulContext(record))), gracefulAbort.signal);
2382
+ if (!closed.settled) {
2383
+ force("deadline");
2384
+ break;
2385
+ }
2386
+ if (closed.error !== undefined)
2387
+ throw closed.error;
2388
+ record.closed = true;
2389
+ record.state = "stopped";
2390
+ publish();
2391
+ } catch (error) {
2392
+ record.failures.push("close");
2393
+ reportFailure(entry.id, "close", error);
2394
+ record.state = "failed";
2395
+ gracefulFailed = true;
2396
+ lifetimeAbort.abort();
2397
+ publish();
2398
+ break;
2399
+ }
2400
+ }
2401
+ clearTimeout(graceTimer);
2402
+ parsed.signal?.removeEventListener("abort", onExternalAbort);
2403
+ const mustForce = forcedReason !== undefined || gracefulFailed;
2404
+ const pendingOperationsAtForce = mustForce ? pending : 0;
2405
+ if (mustForce) {
2406
+ lifetimeAbort.abort();
2407
+ const forceAbort = new AbortController;
2408
+ const forceTimer = setTimeout(() => forceAbort.abort(), Math.max(0, forceDeadlineAt - performance.now()));
2409
+ await Promise.all(reverse.map(async (entry) => {
2410
+ const record = records.get(entry.id);
2411
+ if (!record?.attempted || record.closed)
2412
+ return;
2413
+ try {
2414
+ let cleanup;
2415
+ if (entry.resource.force) {
2416
+ cleanup = Promise.resolve(entry.resource.force(contextFor(record, {
2417
+ signal: forceAbort.signal,
2418
+ deadlineAt: graceDeadlineAt,
2419
+ forceDeadlineAt
2420
+ })));
2421
+ } else if (entry.resource.close && !record.closeInvoked) {
2422
+ record.closeInvoked = true;
2423
+ cleanup = Promise.resolve(entry.resource.close(contextFor(record, {
2424
+ signal: forceAbort.signal,
2425
+ deadlineAt: graceDeadlineAt,
2426
+ forceDeadlineAt
2427
+ })));
2428
+ } else if (entry.resource.close) {
2429
+ record.failures.push("force");
2430
+ reportFailure(entry.id, "force", new Error(`[stitchkit] resource "${entry.id}" was already closing and did not settle before the force deadline`));
2431
+ return;
2432
+ }
2433
+ const forced = await untilDeadline(cleanup ?? Promise.resolve(), forceAbort.signal);
2434
+ if (!forced.settled || forced.error !== undefined) {
2435
+ record.failures.push("force");
2436
+ reportFailure(entry.id, "force", forced.settled ? forced.error : new Error("[stitchkit] forced cleanup did not settle in time"));
2437
+ return;
2438
+ }
2439
+ record.closed = true;
2440
+ record.state = "stopped";
2441
+ } catch (error) {
2442
+ record.failures.push("force");
2443
+ reportFailure(entry.id, "force", error);
2444
+ }
2445
+ }));
2446
+ clearTimeout(forceTimer);
2447
+ }
2448
+ const cleanupComplete = [...records.values()].every((record) => !record.attempted || record.closed);
2449
+ lifecycle = cleanupComplete ? "stopped" : "failed";
2450
+ publish();
2451
+ return ApplicationShutdownResultSchema.parse({
2452
+ outcome: mustForce ? "forced" : "clean",
2453
+ ...forcedReason && { reason: forcedReason },
2454
+ cleanupComplete,
2455
+ acceptedOperations: accepted,
2456
+ completedOperations: completed,
2457
+ pendingOperations: pending,
2458
+ pendingOperationsAtForce,
2459
+ resources: ordered.map((entry) => {
2460
+ const record = records.get(entry.id);
2461
+ if (!record)
2462
+ throw new Error("Managed resource record disappeared");
2463
+ return {
2464
+ id: entry.id,
2465
+ state: !record.attempted ? "not-started" : record.closed ? "closed" : "force-failed",
2466
+ failures: record.failures
2467
+ };
2468
+ }),
2469
+ durationMs: performance.now() - startedAt
2470
+ });
2471
+ })();
2472
+ shutdownPromise.catch(() => {
2473
+ return;
2474
+ });
2475
+ return shutdownPromise;
2476
+ };
2477
+ return {
2478
+ id,
2479
+ admission: { acquire, run },
2480
+ start,
2481
+ getSnapshot: snapshot,
2482
+ subscribe(listener) {
2483
+ listeners.add(listener);
2484
+ listener(snapshot());
2485
+ return () => listeners.delete(listener);
2486
+ },
2487
+ shutdown,
2488
+ restart(input) {
2489
+ const queued = restarting.then(() => runRestart(input));
2490
+ restarting = queued.then(() => {
2491
+ return;
2492
+ }, () => {
2493
+ return;
2494
+ });
2495
+ return queued;
2496
+ }
2497
+ };
2498
+ }
2499
+
1025
2500
  // src/testing/managed-resource-conformance-scenarios.ts
1026
2501
  var MANAGED_RESOURCE_CONFORMANCE_SCENARIOS = ManagedResourceConformanceScenarioSchema.array().parse([
1027
2502
  { id: "clean-shutdown", required: true },
@@ -1389,90 +2864,90 @@ async function runManagedResourceConformance(config) {
1389
2864
  }
1390
2865
  }
1391
2866
  // src/testing/surface-conformance.ts
1392
- import { z as z4 } from "zod";
2867
+ import { z as z5 } from "zod";
1393
2868
 
1394
2869
  // src/realtime/contract.ts
1395
- import { z as z2 } from "zod";
1396
- var RealtimeRejectDirectionSchema = z2.enum([
2870
+ import { z as z3 } from "zod";
2871
+ var RealtimeRejectDirectionSchema = z3.enum([
1397
2872
  "client-inbound",
1398
2873
  "client-outbound",
1399
2874
  "server-inbound",
1400
2875
  "server-outbound"
1401
2876
  ]);
1402
- var RealtimeRejectPhaseSchema = z2.enum(["arguments", "acknowledgement"]);
1403
- var RealtimeRejectReasonSchema = z2.enum([
2877
+ var RealtimeRejectPhaseSchema = z3.enum(["arguments", "acknowledgement"]);
2878
+ var RealtimeRejectReasonSchema = z3.enum([
1404
2879
  "unknown-event",
1405
2880
  "invalid-arguments",
1406
2881
  "invalid-acknowledgement-value",
1407
2882
  "missing-acknowledgement",
1408
2883
  "rejected-by-peer"
1409
2884
  ]);
1410
- var RealtimeRejectFaultSchema = z2.enum(["peer", "local"]);
2885
+ var RealtimeRejectFaultSchema = z3.enum(["peer", "local"]);
1411
2886
 
1412
2887
  // src/testing/surface-manifest.ts
1413
2888
  import { createHash } from "node:crypto";
1414
- import { z as z3 } from "zod";
1415
- var HttpMethodSchema = z3.enum(["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"]);
1416
- var ToolTransportSchema = z3.enum(["MCP", "AGENT", "CLI"]);
1417
- var SurfaceTransportSchema = z3.enum(["HTTP", "MCP", "AGENT", "CLI", "REALTIME"]);
1418
- var SurfaceSchemaDigestsSchema = z3.object({
1419
- params: z3.string().nullable(),
1420
- input: z3.string().nullable(),
1421
- output: z3.string().nullable(),
1422
- multipart: z3.string().nullable()
2889
+ import { z as z4 } from "zod";
2890
+ var HttpMethodSchema = z4.enum(["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"]);
2891
+ var ToolTransportSchema = z4.enum(["MCP", "AGENT", "CLI"]);
2892
+ var SurfaceTransportSchema = z4.enum(["HTTP", "MCP", "AGENT", "CLI", "REALTIME"]);
2893
+ var SurfaceSchemaDigestsSchema = z4.object({
2894
+ params: z4.string().nullable(),
2895
+ input: z4.string().nullable(),
2896
+ output: z4.string().nullable(),
2897
+ multipart: z4.string().nullable()
1423
2898
  });
1424
- var SurfaceManifestOperationSchema = z3.object({
1425
- kind: z3.enum(["contract", "runtime"]),
1426
- service: z3.string(),
1427
- action: z3.string(),
2899
+ var SurfaceManifestOperationSchema = z4.object({
2900
+ kind: z4.enum(["contract", "runtime"]),
2901
+ service: z4.string(),
2902
+ action: z4.string(),
1428
2903
  method: HttpMethodSchema,
1429
- scope: z3.string().nullable(),
1430
- description: z3.string(),
2904
+ scope: z4.string().nullable(),
2905
+ description: z4.string(),
1431
2906
  schemas: SurfaceSchemaDigestsSchema,
1432
- http: z3.array(z3.object({ method: HttpMethodSchema, path: z3.string() }))
2907
+ http: z4.array(z4.object({ method: HttpMethodSchema, path: z4.string() }))
1433
2908
  });
1434
- var SurfaceManifestToolSchema = z3.object({
1435
- kind: z3.enum(["contract", "runtime"]),
1436
- service: z3.string(),
1437
- action: z3.string(),
1438
- name: z3.string(),
1439
- input: z3.string()
2909
+ var SurfaceManifestToolSchema = z4.object({
2910
+ kind: z4.enum(["contract", "runtime"]),
2911
+ service: z4.string(),
2912
+ action: z4.string(),
2913
+ name: z4.string(),
2914
+ input: z4.string()
1440
2915
  });
1441
- var SurfaceManifestToolSurfaceSchema = z3.object({
2916
+ var SurfaceManifestToolSurfaceSchema = z4.object({
1442
2917
  transport: ToolTransportSchema,
1443
- surface: z3.string().nullable(),
1444
- tools: z3.array(SurfaceManifestToolSchema)
2918
+ surface: z4.string().nullable(),
2919
+ tools: z4.array(SurfaceManifestToolSchema)
1445
2920
  });
1446
- var SurfaceManifestCliCommandSchema = z3.object({
1447
- name: z3.string(),
1448
- description: z3.string(),
1449
- input: z3.string(),
1450
- output: z3.string().nullable()
2921
+ var SurfaceManifestCliCommandSchema = z4.object({
2922
+ name: z4.string(),
2923
+ description: z4.string(),
2924
+ input: z4.string(),
2925
+ output: z4.string().nullable()
1451
2926
  });
1452
- var SurfaceManifestExtensionSchema = z3.object({
1453
- name: z3.string(),
2927
+ var SurfaceManifestExtensionSchema = z4.object({
2928
+ name: z4.string(),
1454
2929
  transport: SurfaceTransportSchema
1455
2930
  });
1456
- var SurfaceRealtimeSchemaPairSchema = z3.object({
1457
- input: z3.string(),
1458
- output: z3.string()
2931
+ var SurfaceRealtimeSchemaPairSchema = z4.object({
2932
+ input: z4.string(),
2933
+ output: z4.string()
1459
2934
  });
1460
- var SurfaceManifestRealtimeEventSchema = z3.object({
1461
- contract: z3.string(),
1462
- direction: z3.enum(["serverToClient", "clientToServer"]),
1463
- event: z3.string(),
2935
+ var SurfaceManifestRealtimeEventSchema = z4.object({
2936
+ contract: z4.string(),
2937
+ direction: z4.enum(["serverToClient", "clientToServer"]),
2938
+ event: z4.string(),
1464
2939
  args: SurfaceRealtimeSchemaPairSchema,
1465
2940
  acknowledgement: SurfaceRealtimeSchemaPairSchema.nullable()
1466
2941
  });
1467
- var SurfaceManifestSchema = z3.object({
1468
- manifestVersion: z3.literal(2),
1469
- digestVersion: z3.literal(1),
1470
- operations: z3.array(SurfaceManifestOperationSchema),
1471
- toolSurfaces: z3.array(SurfaceManifestToolSurfaceSchema),
1472
- realtimeContracts: z3.array(z3.string()),
1473
- realtime: z3.array(SurfaceManifestRealtimeEventSchema),
1474
- cliOnly: z3.array(SurfaceManifestCliCommandSchema),
1475
- extensions: z3.array(SurfaceManifestExtensionSchema)
2942
+ var SurfaceManifestSchema = z4.object({
2943
+ manifestVersion: z4.literal(2),
2944
+ digestVersion: z4.literal(1),
2945
+ operations: z4.array(SurfaceManifestOperationSchema),
2946
+ toolSurfaces: z4.array(SurfaceManifestToolSurfaceSchema),
2947
+ realtimeContracts: z4.array(z4.string()),
2948
+ realtime: z4.array(SurfaceManifestRealtimeEventSchema),
2949
+ cliOnly: z4.array(SurfaceManifestCliCommandSchema),
2950
+ extensions: z4.array(SurfaceManifestExtensionSchema)
1476
2951
  });
1477
2952
  function compareCodepoints(left, right) {
1478
2953
  if (left < right)
@@ -1753,37 +3228,37 @@ actual: ${actual}`);
1753
3228
  }
1754
3229
 
1755
3230
  // src/testing/surface-conformance.ts
1756
- var RealtimeRejectionObservationSchema = z4.object({
3231
+ var RealtimeRejectionObservationSchema = z5.object({
1757
3232
  direction: RealtimeRejectDirectionSchema,
1758
3233
  phase: RealtimeRejectPhaseSchema,
1759
3234
  reason: RealtimeRejectReasonSchema,
1760
3235
  fault: RealtimeRejectFaultSchema
1761
3236
  });
1762
3237
  var ObservationFields = {
1763
- code: z4.string().optional(),
1764
- data: z4.unknown().optional(),
1765
- diagnostic: z4.string().optional(),
1766
- handlerCalls: z4.number().int().nonnegative().optional()
3238
+ code: z5.string().optional(),
3239
+ data: z5.unknown().optional(),
3240
+ diagnostic: z5.string().optional(),
3241
+ handlerCalls: z5.number().int().nonnegative().optional()
1767
3242
  };
1768
- var RealtimeDisconnectObservationSchema = z4.object({
1769
- phase: z4.enum(["before-invoke", "in-flight"])
3243
+ var RealtimeDisconnectObservationSchema = z5.object({
3244
+ phase: z5.enum(["before-invoke", "in-flight"])
1770
3245
  });
1771
- var TransportObservationSchema = z4.discriminatedUnion("outcome", [
1772
- z4.object({ outcome: z4.literal("success"), ...ObservationFields }),
1773
- z4.object({ outcome: z4.literal("validation_error"), ...ObservationFields }),
1774
- z4.object({ outcome: z4.literal("domain_error"), ...ObservationFields }),
1775
- z4.object({ outcome: z4.literal("aborted"), ...ObservationFields }),
1776
- z4.object({
1777
- outcome: z4.literal("realtime_rejected"),
3246
+ var TransportObservationSchema = z5.discriminatedUnion("outcome", [
3247
+ z5.object({ outcome: z5.literal("success"), ...ObservationFields }),
3248
+ z5.object({ outcome: z5.literal("validation_error"), ...ObservationFields }),
3249
+ z5.object({ outcome: z5.literal("domain_error"), ...ObservationFields }),
3250
+ z5.object({ outcome: z5.literal("aborted"), ...ObservationFields }),
3251
+ z5.object({
3252
+ outcome: z5.literal("realtime_rejected"),
1778
3253
  ...ObservationFields,
1779
3254
  rejection: RealtimeRejectionObservationSchema
1780
3255
  }),
1781
- z4.object({
1782
- outcome: z4.literal("disconnected"),
3256
+ z5.object({
3257
+ outcome: z5.literal("disconnected"),
1783
3258
  ...ObservationFields,
1784
3259
  disconnect: RealtimeDisconnectObservationSchema
1785
3260
  }),
1786
- z4.object({ outcome: z4.literal("timeout"), ...ObservationFields })
3261
+ z5.object({ outcome: z5.literal("timeout"), ...ObservationFields })
1787
3262
  ]);
1788
3263
  function scenarioOutcome(scenario) {
1789
3264
  if (scenario === "event" || scenario === "acknowledgement")