@shumkov/orchestra 0.10.14 → 0.10.16

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.
@@ -40,6 +40,39 @@ const DEFAULT_MAX_USED_REQUEST_IDS = 65_536;
40
40
  const DEFAULT_MAX_QUEUED_LINES = 256;
41
41
  const DEFAULT_MAX_QUEUED_BYTES = 4 * 1024 * 1024;
42
42
  const DEFAULT_SINK_TIMEOUT_MS = 5_000;
43
+ const CLIENT_ROOT_ERROR_CODES = Object.freeze([
44
+ 'CODEX_PROTOCOL_ERROR',
45
+ 'CODEX_TRANSPORT_ERROR',
46
+ 'CODEX_PROCESS_EXITED',
47
+ 'CODEX_PROCESS_ERROR',
48
+ 'CODEX_RPC_TIMEOUT',
49
+ 'CODEX_SINK_TIMEOUT',
50
+ 'CODEX_PROCESS_CLOSE_TIMEOUT',
51
+ 'CODEX_PROCESS_CLEANUP_UNVERIFIED',
52
+ 'unknown',
53
+ ]);
54
+ const CLIENT_FAULT_CLASSES = Object.freeze([
55
+ 'stderr-limit',
56
+ 'transport',
57
+ 'protocol',
58
+ 'process-exit',
59
+ 'rpc-timeout',
60
+ 'sink',
61
+ 'cleanup',
62
+ 'unknown',
63
+ ]);
64
+ const CLIENT_ROOT_ERROR_CODE_SET = new Set(CLIENT_ROOT_ERROR_CODES);
65
+ const CLIENT_FAULT_CLASS_SET = new Set(CLIENT_FAULT_CLASSES);
66
+ const CLIENT_FAULT_CLASS_BY_CODE = Object.freeze({
67
+ CODEX_PROTOCOL_ERROR: 'protocol',
68
+ CODEX_TRANSPORT_ERROR: 'transport',
69
+ CODEX_PROCESS_EXITED: 'process-exit',
70
+ CODEX_PROCESS_ERROR: 'process-exit',
71
+ CODEX_RPC_TIMEOUT: 'rpc-timeout',
72
+ CODEX_SINK_TIMEOUT: 'sink',
73
+ CODEX_PROCESS_CLOSE_TIMEOUT: 'cleanup',
74
+ CODEX_PROCESS_CLEANUP_UNVERIFIED: 'cleanup',
75
+ });
43
76
  const MAX_ID_BYTES = 512;
44
77
  const MAX_TEXT_BYTES = 64 * 1024;
45
78
  const MAX_COMPLETED_ITEMS = 512;
@@ -104,6 +137,115 @@ const RECOGNIZED_RPC_ERRORS = new Map(
104
137
  .map(([method, messages]) => [method, new Set(messages)]),
105
138
  );
106
139
 
140
+ function faultProvenanceFrom(error) {
141
+ const seen = new Set();
142
+ let current = error;
143
+ for (let depth = 0; current && depth < 8; depth += 1) {
144
+ if (
145
+ (typeof current !== 'object' && typeof current !== 'function')
146
+ || seen.has(current)
147
+ ) {
148
+ break;
149
+ }
150
+ seen.add(current);
151
+ const rootDescriptor = Object.getOwnPropertyDescriptor(
152
+ current,
153
+ 'clientRootErrorCode',
154
+ );
155
+ const classDescriptor = Object.getOwnPropertyDescriptor(
156
+ current,
157
+ 'clientFaultClass',
158
+ );
159
+ const codeDescriptor = Object.getOwnPropertyDescriptor(current, 'code');
160
+ const rootCode = rootDescriptor?.value;
161
+ const code = codeDescriptor?.value;
162
+ const faultClass = classDescriptor?.value;
163
+ if (
164
+ typeof rootCode === 'string'
165
+ && CLIENT_ROOT_ERROR_CODE_SET.has(rootCode)
166
+ && rootCode !== 'unknown'
167
+ ) {
168
+ return Object.freeze({
169
+ clientRootErrorCode: rootCode,
170
+ clientFaultClass: CLIENT_FAULT_CLASS_SET.has(faultClass)
171
+ ? faultClass
172
+ : (CLIENT_FAULT_CLASS_BY_CODE[rootCode] ?? 'unknown'),
173
+ });
174
+ }
175
+ if (
176
+ typeof code === 'string'
177
+ && CLIENT_ROOT_ERROR_CODE_SET.has(code)
178
+ && code !== 'unknown'
179
+ ) {
180
+ return Object.freeze({
181
+ clientRootErrorCode: code,
182
+ clientFaultClass: CLIENT_FAULT_CLASS_SET.has(faultClass)
183
+ ? faultClass
184
+ : (CLIENT_FAULT_CLASS_BY_CODE[code] ?? 'unknown'),
185
+ });
186
+ }
187
+ const causeDescriptor = Object.getOwnPropertyDescriptor(current, 'cause');
188
+ current = causeDescriptor?.value;
189
+ }
190
+ return Object.freeze({
191
+ clientRootErrorCode: 'unknown',
192
+ clientFaultClass: 'unknown',
193
+ });
194
+ }
195
+
196
+ function defineFaultProvenance(error, provenance) {
197
+ const properties = {
198
+ clientRootErrorCode: {
199
+ value: provenance.clientRootErrorCode,
200
+ enumerable: true,
201
+ writable: false,
202
+ configurable: false,
203
+ },
204
+ clientFaultClass: {
205
+ value: provenance.clientFaultClass,
206
+ enumerable: true,
207
+ writable: false,
208
+ configurable: false,
209
+ },
210
+ };
211
+ try {
212
+ Object.defineProperties(error, properties);
213
+ return error;
214
+ } catch {
215
+ const safeCode = provenance.clientRootErrorCode === 'unknown'
216
+ ? 'CODEX_PROTOCOL_ERROR'
217
+ : provenance.clientRootErrorCode;
218
+ const replacement = new CodexAppServerError(
219
+ 'app-server fault could not be annotated',
220
+ safeCode,
221
+ { cause: error },
222
+ );
223
+ Object.defineProperties(replacement, properties);
224
+ return replacement;
225
+ }
226
+ }
227
+
228
+ function faultWithClass(error, clientFaultClass) {
229
+ Object.defineProperty(error, 'clientFaultClass', {
230
+ value: clientFaultClass,
231
+ enumerable: true,
232
+ writable: false,
233
+ configurable: false,
234
+ });
235
+ return error;
236
+ }
237
+
238
+ function transportWriteError(cause) {
239
+ if (cause instanceof CodexAppServerError && cause.code === 'CODEX_TRANSPORT_ERROR') {
240
+ return cause;
241
+ }
242
+ return new CodexAppServerError(
243
+ 'app-server transport write failed',
244
+ 'CODEX_TRANSPORT_ERROR',
245
+ { cause },
246
+ );
247
+ }
248
+
107
249
  function assertProtocolSchema() {
108
250
  const experimental = [...CLIENT_REQUESTS]
109
251
  .filter(([, spec]) => spec.experimental)
@@ -1794,14 +1936,19 @@ function projectRequestParams(method, params, spec, ownedCwd) {
1794
1936
  );
1795
1937
  }
1796
1938
 
1797
- function mutationDeliveryError(pending, cause) {
1939
+ function mutationDeliveryError(
1940
+ pending,
1941
+ cause,
1942
+ provenance = faultProvenanceFrom(cause),
1943
+ ) {
1798
1944
  const code = pending.writeAttempted
1799
1945
  ? 'CODEX_RPC_OUTCOME_UNKNOWN'
1800
1946
  : 'CODEX_RPC_NOT_SENT';
1801
1947
  const message = pending.writeAttempted
1802
1948
  ? `app-server ${pending.method} outcome is unknown`
1803
1949
  : `app-server ${pending.method} was not sent`;
1804
- return new CodexAppServerError(message, code, { cause });
1950
+ const error = new CodexAppServerError(message, code, { cause });
1951
+ return defineFaultProvenance(error, provenance);
1805
1952
  }
1806
1953
 
1807
1954
  function sameFingerprint(left, right) {
@@ -1994,6 +2141,7 @@ class CodexAppServerClient extends EventEmitter {
1994
2141
  this.protocolError = null;
1995
2142
  this.faultPromise = null;
1996
2143
  this.faultOutcome = null;
2144
+ this.faultProvenance = null;
1997
2145
  this.faultWaitPromise = new Promise((resolve, reject) => {
1998
2146
  this.resolveFaultWait = resolve;
1999
2147
  this.rejectFaultWait = reject;
@@ -2463,7 +2611,10 @@ class CodexAppServerClient extends EventEmitter {
2463
2611
  }
2464
2612
  }),
2465
2613
  'transport write',
2466
- );
2614
+ ).catch((error) => {
2615
+ if (error?.code === 'CODEX_SINK_TIMEOUT') throw error;
2616
+ throw transportWriteError(error);
2617
+ });
2467
2618
  }
2468
2619
 
2469
2620
  _awaitBounded(value, label) {
@@ -2713,10 +2864,10 @@ class CodexAppServerClient extends EventEmitter {
2713
2864
  ? chunk.length
2714
2865
  : Buffer.byteLength(String(chunk));
2715
2866
  if (this.stderrBytes > this.maxStderrBytes) {
2716
- this._fault(new CodexAppServerError(
2867
+ this._fault(faultWithClass(new CodexAppServerError(
2717
2868
  'app-server stderr exceeded the size limit',
2718
2869
  'CODEX_PROTOCOL_ERROR',
2719
- ));
2870
+ ), 'stderr-limit'));
2720
2871
  }
2721
2872
  }
2722
2873
 
@@ -2922,13 +3073,16 @@ class CodexAppServerClient extends EventEmitter {
2922
3073
 
2923
3074
  _fault(error) {
2924
3075
  if (this.protocolError || this.state === 'closed') return;
2925
- const fault = error instanceof Error
3076
+ let fault = error instanceof Error
2926
3077
  ? error
2927
3078
  : new CodexAppServerError(
2928
3079
  'app-server protocol failed',
2929
3080
  'CODEX_PROTOCOL_ERROR',
2930
3081
  );
2931
- const ambiguousMutation = this._rejectAll(fault);
3082
+ const provenance = faultProvenanceFrom(fault);
3083
+ this.faultProvenance = provenance;
3084
+ fault = defineFaultProvenance(fault, provenance);
3085
+ const ambiguousMutation = this._rejectAll(fault, provenance);
2932
3086
  this.protocolError = ambiguousMutation ?? fault;
2933
3087
  this._invalidateSinks();
2934
3088
  try { this.emit('protocol-error', this.protocolError); } catch {}
@@ -2953,6 +3107,8 @@ class CodexAppServerClient extends EventEmitter {
2953
3107
  cleanup: cleanupError ? 'failed' : 'completed',
2954
3108
  errorCode: this.protocolError?.code ?? 'CODEX_PROTOCOL_ERROR',
2955
3109
  cleanupErrorCode: cleanupError?.code ?? null,
3110
+ clientRootErrorCode: this.faultProvenance?.clientRootErrorCode ?? 'unknown',
3111
+ clientFaultClass: this.faultProvenance?.clientFaultClass ?? 'unknown',
2956
3112
  mutationOutcomeUnknown:
2957
3113
  this.protocolError?.code === 'CODEX_RPC_OUTCOME_UNKNOWN',
2958
3114
  });
@@ -2976,13 +3132,13 @@ class CodexAppServerClient extends EventEmitter {
2976
3132
  return this.faultWaitPromise;
2977
3133
  }
2978
3134
 
2979
- _rejectAll(cause) {
3135
+ _rejectAll(cause, provenance = faultProvenanceFrom(cause)) {
2980
3136
  let ambiguousMutation = null;
2981
3137
  for (const pending of this.pending.values()) {
2982
3138
  this.clearTimer(pending.timer);
2983
3139
  pending.sinkController?.abort(cause);
2984
3140
  const requestError = pending.stateChanging
2985
- ? mutationDeliveryError(pending, cause)
3141
+ ? mutationDeliveryError(pending, cause, provenance)
2986
3142
  : cause;
2987
3143
  if (
2988
3144
  requestError.code === 'CODEX_RPC_OUTCOME_UNKNOWN'
@@ -20,6 +20,27 @@ const MAX_NODES = 16_384;
20
20
  const MAX_DEPTH = 16;
21
21
  const POST_READ_SETTLE_MS = 25;
22
22
  const SHA256_RE = /^[a-f0-9]{64}$/;
23
+ const CLIENT_ROOT_ERROR_CODES = new Set([
24
+ 'CODEX_PROTOCOL_ERROR',
25
+ 'CODEX_TRANSPORT_ERROR',
26
+ 'CODEX_PROCESS_EXITED',
27
+ 'CODEX_PROCESS_ERROR',
28
+ 'CODEX_RPC_TIMEOUT',
29
+ 'CODEX_SINK_TIMEOUT',
30
+ 'CODEX_PROCESS_CLOSE_TIMEOUT',
31
+ 'CODEX_PROCESS_CLEANUP_UNVERIFIED',
32
+ 'unknown',
33
+ ]);
34
+ const CLIENT_FAULT_CLASSES = new Set([
35
+ 'stderr-limit',
36
+ 'transport',
37
+ 'protocol',
38
+ 'process-exit',
39
+ 'rpc-timeout',
40
+ 'sink',
41
+ 'cleanup',
42
+ 'unknown',
43
+ ]);
23
44
  const EXPECTED_PROFILE_KEYS = new Set([
24
45
  'runtime',
25
46
  'binary',
@@ -467,6 +488,8 @@ function projectClientFault(value) {
467
488
  cleanup: outcome.cleanup,
468
489
  errorCode: outcome.errorCode,
469
490
  cleanupErrorCode: outcome.cleanupErrorCode,
491
+ clientRootErrorCode: outcome.clientRootErrorCode ?? 'unknown',
492
+ clientFaultClass: outcome.clientFaultClass ?? 'unknown',
470
493
  mutationOutcomeUnknown: outcome.mutationOutcomeUnknown,
471
494
  };
472
495
  if (
@@ -484,6 +507,8 @@ function projectClientFault(value) {
484
507
  )
485
508
  )
486
509
  || typeof projected.mutationOutcomeUnknown !== 'boolean'
510
+ || !CLIENT_ROOT_ERROR_CODES.has(projected.clientRootErrorCode)
511
+ || !CLIENT_FAULT_CLASSES.has(projected.clientFaultClass)
487
512
  ) {
488
513
  throw new CodexPreflightError(
489
514
  'Codex preflight client fault handoff was malformed',
@@ -503,6 +528,8 @@ function clientFaultError(outcome) {
503
528
  error.clientFaultContainment = outcome.containment;
504
529
  error.clientFaultCleanup = outcome.cleanup;
505
530
  error.clientFaultCleanupErrorCode = outcome.cleanupErrorCode;
531
+ error.clientRootErrorCode = outcome.clientRootErrorCode;
532
+ error.clientFaultClass = outcome.clientFaultClass;
506
533
  return error;
507
534
  }
508
535