@xera-ai/core 0.4.3 → 0.5.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 (60) hide show
  1. package/dist/artifact/status.d.ts +12 -0
  2. package/dist/artifact/status.d.ts.map +1 -1
  3. package/dist/bin/internal.js +1059 -567
  4. package/dist/bin-internal/auth-setup.d.ts +2 -0
  5. package/dist/bin-internal/auth-setup.d.ts.map +1 -0
  6. package/dist/bin-internal/disputes.d.ts +2 -0
  7. package/dist/bin-internal/disputes.d.ts.map +1 -0
  8. package/dist/bin-internal/doctor.d.ts +1 -1
  9. package/dist/bin-internal/doctor.d.ts.map +1 -1
  10. package/dist/bin-internal/exec.d.ts.map +1 -1
  11. package/dist/bin-internal/graph-record-script.d.ts.map +1 -1
  12. package/dist/bin-internal/graph-record.d.ts.map +1 -1
  13. package/dist/bin-internal/index.d.ts.map +1 -1
  14. package/dist/bin-internal/normalize.d.ts.map +1 -1
  15. package/dist/bin-internal/report.d.ts.map +1 -1
  16. package/dist/bin-internal/verify-prompts.d.ts.map +1 -1
  17. package/dist/classifier/aggregate.d.ts.map +1 -1
  18. package/dist/classifier/auth-expired.d.ts +12 -0
  19. package/dist/classifier/auth-expired.d.ts.map +1 -0
  20. package/dist/classifier/contract-drift.d.ts +35 -0
  21. package/dist/classifier/contract-drift.d.ts.map +1 -0
  22. package/dist/classifier/rate-limited.d.ts +15 -0
  23. package/dist/classifier/rate-limited.d.ts.map +1 -0
  24. package/dist/config/schema.d.ts +32 -3
  25. package/dist/config/schema.d.ts.map +1 -1
  26. package/dist/graph/schema.d.ts +9 -0
  27. package/dist/graph/schema.d.ts.map +1 -1
  28. package/dist/graph/store.d.ts.map +1 -1
  29. package/dist/graph/types.d.ts +2 -1
  30. package/dist/graph/types.d.ts.map +1 -1
  31. package/dist/index.d.ts +2 -0
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/scrub/index.d.ts +2 -0
  34. package/dist/scrub/index.d.ts.map +1 -0
  35. package/dist/scrub/rules.d.ts +12 -0
  36. package/dist/scrub/rules.d.ts.map +1 -0
  37. package/dist/src/index.js +110 -5
  38. package/package.json +4 -3
  39. package/src/artifact/status.ts +3 -0
  40. package/src/bin-internal/auth-setup.ts +116 -0
  41. package/src/bin-internal/disputes.ts +88 -0
  42. package/src/bin-internal/doctor.ts +13 -1
  43. package/src/bin-internal/exec.ts +45 -9
  44. package/src/bin-internal/graph-record-script.ts +37 -8
  45. package/src/bin-internal/graph-record.ts +3 -0
  46. package/src/bin-internal/index.ts +4 -0
  47. package/src/bin-internal/normalize.ts +13 -1
  48. package/src/bin-internal/report.ts +94 -2
  49. package/src/bin-internal/verify-prompts.ts +2 -1
  50. package/src/classifier/aggregate.ts +3 -0
  51. package/src/classifier/auth-expired.ts +44 -0
  52. package/src/classifier/contract-drift.ts +111 -0
  53. package/src/classifier/rate-limited.ts +25 -0
  54. package/src/config/schema.ts +52 -9
  55. package/src/graph/schema.ts +3 -0
  56. package/src/graph/store.ts +8 -1
  57. package/src/graph/types.ts +5 -1
  58. package/src/index.ts +2 -0
  59. package/src/scrub/index.ts +1 -0
  60. package/src/scrub/rules.ts +69 -0
@@ -0,0 +1,111 @@
1
+ import type { ClassifyResult } from './rate-limited';
2
+
3
+ export interface OpenAPISchema {
4
+ type?: 'object' | 'array' | 'string' | 'integer' | 'number' | 'boolean' | 'null';
5
+ properties?: Record<string, OpenAPISchema>;
6
+ required?: readonly string[];
7
+ items?: OpenAPISchema;
8
+ }
9
+
10
+ interface OpenAPIOperation {
11
+ responses?: Record<string, { content?: Record<string, { schema?: OpenAPISchema }> }>;
12
+ requestBody?: { content?: Record<string, { schema?: OpenAPISchema }> };
13
+ }
14
+
15
+ export interface OpenAPIDocument {
16
+ paths: Record<
17
+ string,
18
+ Partial<Record<'get' | 'post' | 'put' | 'patch' | 'delete', OpenAPIOperation>>
19
+ >;
20
+ }
21
+
22
+ export interface ContractDriftCall {
23
+ method: string;
24
+ url: string;
25
+ status: number;
26
+ respBody: unknown;
27
+ }
28
+
29
+ export interface ClassifyContractDriftInput {
30
+ calls: readonly ContractDriftCall[];
31
+ openapi: OpenAPIDocument | null;
32
+ }
33
+
34
+ function matchPath(specPaths: readonly string[], actualUrl: string): string | null {
35
+ const path = actualUrl.split('?')[0] ?? actualUrl;
36
+ for (const tmpl of specPaths) {
37
+ const re = new RegExp(`^${tmpl.replace(/\{[^}]+\}/g, '[^/]+')}$`);
38
+ if (re.test(path)) return tmpl;
39
+ }
40
+ return null;
41
+ }
42
+
43
+ function matchesSchema(body: unknown, schema: OpenAPISchema | undefined): boolean {
44
+ if (!schema) return true;
45
+ if (schema.type === 'object') {
46
+ if (typeof body !== 'object' || body === null || Array.isArray(body)) return false;
47
+ const obj = body as Record<string, unknown>;
48
+ for (const req of schema.required ?? []) {
49
+ if (!(req in obj)) return false;
50
+ }
51
+ return true;
52
+ }
53
+ if (schema.type === 'array') return Array.isArray(body);
54
+ if (schema.type === 'string') return typeof body === 'string';
55
+ if (schema.type === 'integer' || schema.type === 'number') return typeof body === 'number';
56
+ if (schema.type === 'boolean') return typeof body === 'boolean';
57
+ if (schema.type === 'null') return body === null;
58
+ return true;
59
+ }
60
+
61
+ const VERBS = ['get', 'post', 'put', 'patch', 'delete'] as const;
62
+ type Verb = (typeof VERBS)[number];
63
+
64
+ function isVerb(s: string): s is Verb {
65
+ return (VERBS as readonly string[]).includes(s);
66
+ }
67
+
68
+ export function classifyContractDrift(input: ClassifyContractDriftInput): ClassifyResult | null {
69
+ if (input.openapi === null) return null;
70
+ const specPaths = Object.keys(input.openapi.paths);
71
+
72
+ for (const call of input.calls) {
73
+ const tmpl = matchPath(specPaths, call.url);
74
+ if (!tmpl) {
75
+ return {
76
+ class: 'CONTRACT_DRIFT',
77
+ rationale: `Endpoint ${call.method} ${call.url} not found in OpenAPI`,
78
+ };
79
+ }
80
+ const methodLower = call.method.toLowerCase();
81
+ if (!isVerb(methodLower)) {
82
+ return {
83
+ class: 'CONTRACT_DRIFT',
84
+ rationale: `Method ${call.method} not supported by classifier for ${tmpl}`,
85
+ };
86
+ }
87
+ const pathItem = input.openapi.paths[tmpl];
88
+ const op = pathItem?.[methodLower];
89
+ if (!op) {
90
+ return {
91
+ class: 'CONTRACT_DRIFT',
92
+ rationale: `${call.method} not defined for ${tmpl} in OpenAPI`,
93
+ };
94
+ }
95
+ const respDef = op.responses?.[String(call.status)];
96
+ if (!respDef) {
97
+ return {
98
+ class: 'CONTRACT_DRIFT',
99
+ rationale: `Status ${call.status} not enumerated for ${call.method} ${tmpl} in OpenAPI`,
100
+ };
101
+ }
102
+ const schema = respDef.content?.['application/json']?.schema;
103
+ if (!matchesSchema(call.respBody, schema)) {
104
+ return {
105
+ class: 'CONTRACT_DRIFT',
106
+ rationale: `Response body for ${call.method} ${tmpl} (${call.status}) does not match OpenAPI schema`,
107
+ };
108
+ }
109
+ }
110
+ return null;
111
+ }
@@ -0,0 +1,25 @@
1
+ import type { Classification } from '../artifact/status';
2
+
3
+ export interface HttpCallSummary {
4
+ method: string;
5
+ url: string;
6
+ status: number;
7
+ }
8
+
9
+ export interface ClassifyResult {
10
+ class: Classification;
11
+ rationale: string;
12
+ }
13
+
14
+ export interface ClassifyRateLimitedInput {
15
+ calls: readonly HttpCallSummary[];
16
+ }
17
+
18
+ export function classifyRateLimited(input: ClassifyRateLimitedInput): ClassifyResult | null {
19
+ const hit = input.calls.find((c) => c.status === 429);
20
+ if (!hit) return null;
21
+ return {
22
+ class: 'RATE_LIMITED',
23
+ rationale: `Captured HTTP 429 on ${hit.method} ${hit.url}`,
24
+ };
25
+ }
@@ -31,6 +31,37 @@ const WebSchema = z
31
31
  path: ['defaultEnv'],
32
32
  });
33
33
 
34
+ const HttpAuthRoleSchema = z.object({
35
+ tokenEnv: z.string().optional(),
36
+ userEnv: z.string().optional(),
37
+ passEnv: z.string().optional(),
38
+ tokenUrl: z.string().url().optional(),
39
+ clientIdEnv: z.string().optional(),
40
+ clientSecretEnv: z.string().optional(),
41
+ scope: z.string().optional(),
42
+ });
43
+
44
+ const HttpAuthSchema = z.object({
45
+ strategy: z.enum(['bearer', 'apiKey', 'basic', 'oauth-cc', 'custom', 'none']).default('none'),
46
+ ttl: z.string().default('8h'),
47
+ refreshBuffer: z.string().default('30m'),
48
+ roles: z.record(z.string(), HttpAuthRoleSchema).default({}),
49
+ });
50
+
51
+ const HttpSchema = z
52
+ .object({
53
+ baseUrl: z.record(z.string(), z.string().url()).refine((m) => Object.keys(m).length > 0, {
54
+ message: 'baseUrl must have at least one environment',
55
+ }),
56
+ defaultEnv: z.string(),
57
+ spec: z.string().optional(),
58
+ auth: HttpAuthSchema.prefault({}),
59
+ })
60
+ .refine((h) => h.baseUrl[h.defaultEnv] !== undefined, {
61
+ message: 'defaultEnv must exist in baseUrl map',
62
+ path: ['defaultEnv'],
63
+ });
64
+
34
65
  const JiraSchema = z.object({
35
66
  baseUrl: z.string().url(),
36
67
  projectKeys: z.array(z.string().min(1)).min(1),
@@ -74,19 +105,31 @@ const RunSchema = z
74
105
  autoImpact: z
75
106
  .object({
76
107
  enabled: z.boolean().default(true),
77
- threshold: z.number().nonnegative().default(6.0),
108
+ threshold: z.number().nonnegative().default(8.0),
78
109
  })
79
110
  .prefault({}),
80
111
  })
81
112
  .prefault({});
82
113
 
83
- export const XeraConfigSchema = z.object({
84
- jira: JiraSchema,
85
- web: WebSchema,
86
- ai: AISchema,
87
- reporting: ReportingSchema,
88
- run: RunSchema.prefault({}),
89
- adapters: z.array(z.string().min(1)).min(1).default(['web']),
90
- });
114
+ export const XeraConfigSchema = z
115
+ .object({
116
+ jira: JiraSchema,
117
+ web: WebSchema.optional(),
118
+ http: HttpSchema.optional(),
119
+ ai: AISchema,
120
+ reporting: ReportingSchema,
121
+ run: RunSchema.prefault({}),
122
+ adapters: z
123
+ .array(z.enum(['web', 'http']))
124
+ .min(1)
125
+ .default(['web']),
126
+ })
127
+ .refine((c) => c.web !== undefined || c.http !== undefined, {
128
+ message: 'At least one of `web` or `http` must be configured',
129
+ })
130
+ .refine((c) => c.adapters.every((a) => (a === 'web' ? c.web : c.http) !== undefined), {
131
+ message: 'Every adapter in `adapters` must have a corresponding config block',
132
+ path: ['adapters'],
133
+ });
91
134
 
92
135
  export type XeraConfig = z.infer<typeof XeraConfigSchema>;
@@ -78,6 +78,9 @@ const classification = z.enum([
78
78
  'FLAKY',
79
79
  'PASS',
80
80
  'TEST_OUTDATED',
81
+ 'CONTRACT_DRIFT',
82
+ 'RATE_LIMITED',
83
+ 'AUTH_EXPIRED',
81
84
  ]);
82
85
 
83
86
  const runClassified = z
@@ -185,7 +185,14 @@ export function deriveSnapshot(events: Event[]): Snapshot {
185
185
  edges.push(ed);
186
186
  break;
187
187
  }
188
- // run.classified and classification.disputed: not materialized in v0.6.0 snapshot
188
+ case 'classification.disputed': {
189
+ const existing = latestFailures[e.payload.scenarioId];
190
+ if (existing && existing.runId === e.payload.runId) {
191
+ existing.disputed = true;
192
+ }
193
+ break;
194
+ }
195
+ // run.classified: not materialized in snapshot
189
196
  default:
190
197
  break;
191
198
  }
@@ -12,7 +12,10 @@ export type Classification =
12
12
  | 'SELECTOR_DRIFT'
13
13
  | 'FLAKY'
14
14
  | 'PASS'
15
- | 'TEST_OUTDATED';
15
+ | 'TEST_OUTDATED'
16
+ | 'CONTRACT_DRIFT'
17
+ | 'RATE_LIMITED'
18
+ | 'AUTH_EXPIRED';
16
19
 
17
20
  export interface TicketFetchedPayload {
18
21
  ticketId: string;
@@ -154,6 +157,7 @@ export interface FailureNode {
154
157
  runId: string;
155
158
  traceId?: string;
156
159
  ts: string;
160
+ disputed?: boolean;
157
161
  }
158
162
 
159
163
  export interface EdgeRecord {
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@ export * from './auth/encrypt';
8
8
  export * from './auth/key';
9
9
  export * from './auth/refresh';
10
10
  export * from './auth/state';
11
+ export type { OpenAPIDocument, OpenAPISchema } from './classifier/contract-drift';
11
12
  export * from './config/define';
12
13
  export * from './config/load';
13
14
  export * from './config/schema';
@@ -17,3 +18,4 @@ export * from './jira/retry';
17
18
  export * from './jira/types';
18
19
  export * from './lock/file-lock';
19
20
  export * from './logging/ndjson-logger';
21
+ export * from './scrub';
@@ -0,0 +1 @@
1
+ export * from './rules';
@@ -0,0 +1,69 @@
1
+ export const SENSITIVE_HEADERS: readonly string[] = [
2
+ 'authorization',
3
+ 'cookie',
4
+ 'set-cookie',
5
+ 'x-api-key',
6
+ 'x-auth-token',
7
+ 'x-csrf-token',
8
+ 'proxy-authorization',
9
+ ];
10
+
11
+ export const SENSITIVE_BODY_KEYS: readonly RegExp[] = [
12
+ /password/i,
13
+ /passwd/i,
14
+ /token/i,
15
+ /secret/i,
16
+ /api[-_]?key/i,
17
+ /access[-_]?key/i,
18
+ /private[-_]?key/i,
19
+ /authorization/i,
20
+ /credit[-_]?card/i,
21
+ /card[-_]?number/i,
22
+ /cvv/i,
23
+ ];
24
+
25
+ export const JWT_RE = /\beyJ[A-Za-z0-9_-]{7,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{5,}\b/;
26
+ export const CREDIT_CARD_RE = /\b(?:\d{4}[-\s]?){3}\d{4}\b/;
27
+ export const EMAIL_RE = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/;
28
+ // E.164-ish phone with optional + and separators. Conservative: require at least 7 digits.
29
+ export const PHONE_RE = /(?:\+?\d[\d\s().-]{6,}\d)/;
30
+
31
+ const JWT_RE_G = new RegExp(JWT_RE.source, 'g');
32
+ const CREDIT_CARD_RE_G = new RegExp(CREDIT_CARD_RE.source, 'g');
33
+ export const EMAIL_RE_G = new RegExp(EMAIL_RE.source, 'g');
34
+ export const PHONE_RE_G = new RegExp(PHONE_RE.source, 'g');
35
+
36
+ const REDACTED = '[REDACTED]';
37
+
38
+ export function scrubHeaders(headers: Record<string, string>): Record<string, string> {
39
+ const out: Record<string, string> = {};
40
+ for (const [k, v] of Object.entries(headers)) {
41
+ out[k] = SENSITIVE_HEADERS.includes(k.toLowerCase()) ? REDACTED : v;
42
+ }
43
+ return out;
44
+ }
45
+
46
+ export function scrubBodyJson(body: unknown): unknown {
47
+ if (Array.isArray(body)) return body.map(scrubBodyJson);
48
+ if (body && typeof body === 'object') {
49
+ const out: Record<string, unknown> = {};
50
+ for (const [k, v] of Object.entries(body)) {
51
+ if (SENSITIVE_BODY_KEYS.some((re) => re.test(k))) {
52
+ out[k] = REDACTED;
53
+ } else {
54
+ out[k] = scrubBodyJson(v);
55
+ }
56
+ }
57
+ return out;
58
+ }
59
+ if (typeof body === 'string') return scrubFreeText(body);
60
+ return body;
61
+ }
62
+
63
+ export function scrubFreeText(s: string): string {
64
+ return s
65
+ .replace(JWT_RE_G, REDACTED)
66
+ .replace(CREDIT_CARD_RE_G, REDACTED)
67
+ .replace(EMAIL_RE_G, REDACTED)
68
+ .replace(PHONE_RE_G, REDACTED);
69
+ }