@resolveio/server-lib 22.3.158 → 22.3.160

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.
@@ -47,10 +47,12 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
47
47
  return to.concat(ar || Array.prototype.slice.call(from));
48
48
  };
49
49
  Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.evaluateAssistantAnswerQuality = evaluateAssistantAnswerQuality;
50
51
  exports.buildSupportAIRunFromEvidence = buildSupportAIRunFromEvidence;
51
52
  exports.buildAICoderAIRunFromEvidence = buildAICoderAIRunFromEvidence;
52
53
  exports.buildAssistantAIRunFromEvidence = buildAssistantAIRunFromEvidence;
53
54
  var ai_run_evidence_1 = require("./ai-run-evidence");
55
+ var aicoder_runner_v6_1 = require("./aicoder-runner-v6");
54
56
  var ai_runner_manager_policy_1 = require("./ai-runner-manager-policy");
55
57
  function cleanText(value, max) {
56
58
  if (max === void 0) { max = 1000; }
@@ -64,6 +66,17 @@ function idText(value, max) {
64
66
  function asArray(value) {
65
67
  return Array.isArray(value) ? value : [];
66
68
  }
69
+ function cleanStringList(value, limit, max) {
70
+ if (limit === void 0) { limit = 40; }
71
+ if (max === void 0) { max = 500; }
72
+ return asArray(value)
73
+ .map(function (entry) { return cleanText(entry, max); })
74
+ .filter(Boolean)
75
+ .slice(0, limit);
76
+ }
77
+ function plainObject(value) {
78
+ return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
79
+ }
67
80
  function firstText(source, fields, max) {
68
81
  var e_1, _a;
69
82
  if (max === void 0) { max = 1000; }
@@ -377,11 +390,11 @@ function statusFromBoolean(value, fallback) {
377
390
  return fallback;
378
391
  }
379
392
  function collectArtifacts(evidence) {
380
- var e_9, _a, e_10, _b;
393
+ var e_9, _a, e_10, _b, e_11, _c;
381
394
  var artifacts = [];
382
395
  try {
383
- for (var _c = __values(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.artifacts)), _d = _c.next(); !_d.done; _d = _c.next()) {
384
- var entry = _d.value;
396
+ for (var _d = __values(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.artifacts)), _e = _d.next(); !_e.done; _e = _d.next()) {
397
+ var entry = _e.value;
385
398
  artifacts.push({
386
399
  type: entry.type || 'other',
387
400
  path: cleanText(entry.path || entry.file || entry.artifactPath, 500),
@@ -395,14 +408,14 @@ function collectArtifacts(evidence) {
395
408
  catch (e_9_1) { e_9 = { error: e_9_1 }; }
396
409
  finally {
397
410
  try {
398
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
411
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
399
412
  }
400
413
  finally { if (e_9) throw e_9.error; }
401
414
  }
402
415
  var screenshots = asArray((evidence === null || evidence === void 0 ? void 0 : evidence.screenshots) || (evidence === null || evidence === void 0 ? void 0 : evidence.qaScreenshots) || (evidence === null || evidence === void 0 ? void 0 : evidence.browserScreenshots));
403
416
  try {
404
- for (var _e = __values(screenshots.slice(0, 40)), _f = _e.next(); !_f.done; _f = _e.next()) {
405
- var screenshot = _f.value;
417
+ for (var _f = __values(screenshots.slice(0, 40)), _g = _f.next(); !_g.done; _g = _f.next()) {
418
+ var screenshot = _g.value;
406
419
  if (typeof screenshot === 'string') {
407
420
  artifacts.push({ type: 'screenshot', path: screenshot });
408
421
  }
@@ -421,14 +434,40 @@ function collectArtifacts(evidence) {
421
434
  catch (e_10_1) { e_10 = { error: e_10_1 }; }
422
435
  finally {
423
436
  try {
424
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
437
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
425
438
  }
426
439
  finally { if (e_10) throw e_10.error; }
427
440
  }
441
+ var artifactRefs = __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.artifactPaths)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.artifact_paths)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.runnerEvidenceArtifacts)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.runner_evidence_artifacts)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.supportQaArtifacts)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.support_qa_artifacts)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.qa_artifacts)), false);
442
+ try {
443
+ for (var _h = __values(artifactRefs.slice(0, 60)), _j = _h.next(); !_j.done; _j = _h.next()) {
444
+ var entry = _j.value;
445
+ if (typeof entry === 'string') {
446
+ artifacts.push({ type: 'other', path: cleanText(entry, 500) });
447
+ }
448
+ else if (entry && typeof entry === 'object') {
449
+ artifacts.push({
450
+ type: entry.type || 'other',
451
+ path: cleanText(entry.path || entry.file || entry.artifactPath || entry.artifact_path, 500),
452
+ url: cleanText(entry.url, 500),
453
+ title: cleanText(entry.title || entry.name || entry.label, 300),
454
+ caption: cleanText(entry.caption || entry.message || entry.summary, 800),
455
+ metadata: entry.metadata
456
+ });
457
+ }
458
+ }
459
+ }
460
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
461
+ finally {
462
+ try {
463
+ if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
464
+ }
465
+ finally { if (e_11) throw e_11.error; }
466
+ }
428
467
  return artifacts;
429
468
  }
430
469
  function collectInfraChecks(evidence) {
431
- var e_11, _a;
470
+ var e_12, _a;
432
471
  var _b;
433
472
  var checks = [];
434
473
  try {
@@ -445,12 +484,12 @@ function collectInfraChecks(evidence) {
445
484
  });
446
485
  }
447
486
  }
448
- catch (e_11_1) { e_11 = { error: e_11_1 }; }
487
+ catch (e_12_1) { e_12 = { error: e_12_1 }; }
449
488
  finally {
450
489
  try {
451
490
  if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
452
491
  }
453
- finally { if (e_11) throw e_11.error; }
492
+ finally { if (e_12) throw e_12.error; }
454
493
  }
455
494
  if (evidence === null || evidence === void 0 ? void 0 : evidence.preflightStatus) {
456
495
  checks.push({
@@ -483,11 +522,16 @@ function collectCompileResult(evidence) {
483
522
  };
484
523
  }
485
524
  function collectRouteProbes(evidence) {
486
- var e_12, _a, e_13, _b;
525
+ var e_13, _a, e_14, _b;
487
526
  var probes = [];
527
+ var rows = __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.routeProbes)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.route_probes)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.routes)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.browserRoutes)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.browser_routes)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.workflowProbes)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.workflow_probes)), false);
528
+ var directProbe = plainObject((evidence === null || evidence === void 0 ? void 0 : evidence.routeProbe) || (evidence === null || evidence === void 0 ? void 0 : evidence.route_probe) || (evidence === null || evidence === void 0 ? void 0 : evidence.workflowProbe) || (evidence === null || evidence === void 0 ? void 0 : evidence.workflow_probe));
529
+ if (Object.keys(directProbe).length) {
530
+ rows.push(directProbe);
531
+ }
488
532
  try {
489
- for (var _c = __values(asArray((evidence === null || evidence === void 0 ? void 0 : evidence.routeProbes) || (evidence === null || evidence === void 0 ? void 0 : evidence.routes) || (evidence === null || evidence === void 0 ? void 0 : evidence.browserRoutes))), _d = _c.next(); !_d.done; _d = _c.next()) {
490
- var entry = _d.value;
533
+ for (var rows_1 = __values(rows), rows_1_1 = rows_1.next(); !rows_1_1.done; rows_1_1 = rows_1.next()) {
534
+ var entry = rows_1_1.value;
491
535
  probes.push({
492
536
  route: cleanText(entry.route || entry.path || entry.url, 500) || '/',
493
537
  status: entry.status || statusFromBoolean(entry.passed),
@@ -502,17 +546,17 @@ function collectRouteProbes(evidence) {
502
546
  });
503
547
  }
504
548
  }
505
- catch (e_12_1) { e_12 = { error: e_12_1 }; }
549
+ catch (e_13_1) { e_13 = { error: e_13_1 }; }
506
550
  finally {
507
551
  try {
508
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
552
+ if (rows_1_1 && !rows_1_1.done && (_a = rows_1.return)) _a.call(rows_1);
509
553
  }
510
- finally { if (e_12) throw e_12.error; }
554
+ finally { if (e_13) throw e_13.error; }
511
555
  }
512
556
  var screenshots = asArray((evidence === null || evidence === void 0 ? void 0 : evidence.screenshots) || (evidence === null || evidence === void 0 ? void 0 : evidence.qaScreenshots) || (evidence === null || evidence === void 0 ? void 0 : evidence.browserScreenshots));
513
557
  try {
514
- for (var _e = __values(screenshots.slice(0, 20)), _f = _e.next(); !_f.done; _f = _e.next()) {
515
- var screenshot = _f.value;
558
+ for (var _c = __values(screenshots.slice(0, 20)), _d = _c.next(); !_d.done; _d = _c.next()) {
559
+ var screenshot = _d.value;
516
560
  if (typeof screenshot === 'string') {
517
561
  probes.push({ route: '/', status: 'pass', screenshot: screenshot, caption: 'Historical browser screenshot recorded.' });
518
562
  }
@@ -529,43 +573,45 @@ function collectRouteProbes(evidence) {
529
573
  }
530
574
  }
531
575
  }
532
- catch (e_13_1) { e_13 = { error: e_13_1 }; }
576
+ catch (e_14_1) { e_14 = { error: e_14_1 }; }
533
577
  finally {
534
578
  try {
535
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
579
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
536
580
  }
537
- finally { if (e_13) throw e_13.error; }
581
+ finally { if (e_14) throw e_14.error; }
538
582
  }
539
583
  return probes;
540
584
  }
541
585
  function collectBusinessAssertions(evidence) {
542
- var e_14, _a;
586
+ var e_15, _a;
543
587
  var assertions = [];
588
+ var rows = __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.businessAssertions)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.business_assertions)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.workflowAssertions)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.workflow_assertions)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.assertions)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.qaRows)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.qa_rows)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.rows)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.supportQaAssertions)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.support_qa_assertions)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.aiQaBusinessAssertions)), false), __read(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.ai_qa_business_assertions)), false);
544
589
  try {
545
- for (var _b = __values(asArray((evidence === null || evidence === void 0 ? void 0 : evidence.businessAssertions) || (evidence === null || evidence === void 0 ? void 0 : evidence.workflowAssertions) || (evidence === null || evidence === void 0 ? void 0 : evidence.assertions))), _c = _b.next(); !_c.done; _c = _b.next()) {
546
- var entry = _c.value;
590
+ for (var rows_2 = __values(rows), rows_2_1 = rows_2.next(); !rows_2_1.done; rows_2_1 = rows_2.next()) {
591
+ var entry = rows_2_1.value;
547
592
  assertions.push({
548
- assertion: cleanText(entry.assertion || entry.name || entry.workflow || entry.expected, 1000) || 'business assertion',
549
- status: entry.status || statusFromBoolean(entry.passed),
550
- workflow: cleanText(entry.workflow, 400),
551
- route: cleanText(entry.route, 500),
552
- action: cleanText(entry.action, 500),
553
- expected: cleanText(entry.expected, 1000),
554
- observed: cleanText(entry.observed || entry.actual, 1000),
555
- dataProof: cleanText(entry.dataProof || entry.proof, 1400),
556
- artifactPaths: asArray(entry.artifactPaths || entry.artifacts),
593
+ assertion: cleanText(entry.assertion || entry.name || entry.label || entry.workflow || entry.expected, 1000) || 'business assertion',
594
+ status: entry.status || entry.outcome || entry.result || statusFromBoolean(entry.passed),
595
+ workflow: cleanText(entry.workflow || entry.workflowName || entry.workflow_name, 400),
596
+ route: cleanText(entry.route || entry.url, 500),
597
+ action: cleanText(entry.action || entry.action_under_test || entry.actionUnderTest, 500),
598
+ expected: cleanText(entry.expected || entry.expected_business_state_change || entry.expectedBusinessStateChange, 1000),
599
+ observed: cleanText(entry.observed || entry.actual || entry.after || entry.after_state || entry.afterState, 1000),
600
+ dataProof: cleanText(entry.dataProof || entry.data_proof || entry.domProof || entry.dom_proof || entry.proof || entry.summary, 1400),
601
+ mongoDelta: plainObject(entry.mongoDelta || entry.mongo_delta),
602
+ artifactPaths: cleanStringList(entry.artifactPaths || entry.artifact_paths || entry.artifacts || entry.screenshots || (entry.screenshot ? [entry.screenshot] : []), 30, 500),
557
603
  message: cleanText(entry.message || entry.error || entry.reason, 1000),
558
604
  recordedAt: eventDate(entry),
559
- metadata: entry.metadata
605
+ metadata: __assign(__assign(__assign({}, plainObject(entry.metadata)), (entry.acceptanceBlocked === true || entry.acceptance_blocked === true ? { acceptanceBlocked: true } : {})), (entry.routeOnly === true || entry.route_only === true ? { routeOnly: true } : {}))
560
606
  });
561
607
  }
562
608
  }
563
- catch (e_14_1) { e_14 = { error: e_14_1 }; }
609
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
564
610
  finally {
565
611
  try {
566
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
612
+ if (rows_2_1 && !rows_2_1.done && (_a = rows_2.return)) _a.call(rows_2);
567
613
  }
568
- finally { if (e_14) throw e_14.error; }
614
+ finally { if (e_15) throw e_15.error; }
569
615
  }
570
616
  if (!assertions.length && (evidence === null || evidence === void 0 ? void 0 : evidence.qaRunOutcome) === 'business_assertion_passed') {
571
617
  assertions.push({
@@ -624,7 +670,7 @@ function businessAssertionPassed(assertion) {
624
670
  return /^(pass|passed|success|ok|done)$/i.test(cleanText(assertion.status, 40));
625
671
  }
626
672
  function supportAssertionMatchesDiagnosisProof(assertion, proofPlan) {
627
- var e_15, _a;
673
+ var e_16, _a;
628
674
  var metadata = assertion.metadata || {};
629
675
  if (metadata.diagnosisProofPlanMatched === true || metadata.proofPlanMatched === true || metadata.supportDiagnosisProof === true) {
630
676
  return true;
@@ -669,12 +715,12 @@ function supportAssertionMatchesDiagnosisProof(assertion, proofPlan) {
669
715
  }
670
716
  }
671
717
  }
672
- catch (e_15_1) { e_15 = { error: e_15_1 }; }
718
+ catch (e_16_1) { e_16 = { error: e_16_1 }; }
673
719
  finally {
674
720
  try {
675
721
  if (proofTokens_1_1 && !proofTokens_1_1.done && (_a = proofTokens_1.return)) _a.call(proofTokens_1);
676
722
  }
677
- finally { if (e_15) throw e_15.error; }
723
+ finally { if (e_16) throw e_16.error; }
678
724
  }
679
725
  return overlap >= Math.min(4, Math.max(3, Math.ceil(proofTokens.size * 0.45)));
680
726
  }
@@ -717,7 +763,7 @@ function applySupportDiagnosisProofGate(qa, diagnosisGate, now) {
717
763
  ], false) });
718
764
  }
719
765
  function evidenceObject() {
720
- var e_16, _a;
766
+ var e_17, _a;
721
767
  var values = [];
722
768
  for (var _i = 0; _i < arguments.length; _i++) {
723
769
  values[_i] = arguments[_i];
@@ -730,21 +776,688 @@ function evidenceObject() {
730
776
  }
731
777
  }
732
778
  }
733
- catch (e_16_1) { e_16 = { error: e_16_1 }; }
779
+ catch (e_17_1) { e_17 = { error: e_17_1 }; }
734
780
  finally {
735
781
  try {
736
782
  if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
737
783
  }
738
- finally { if (e_16) throw e_16.error; }
784
+ finally { if (e_17) throw e_17.error; }
739
785
  }
740
786
  return {};
741
787
  }
788
+ function mergeEvidenceRecords() {
789
+ var e_18, _a, e_19, _b;
790
+ var values = [];
791
+ for (var _i = 0; _i < arguments.length; _i++) {
792
+ values[_i] = arguments[_i];
793
+ }
794
+ var merged = {};
795
+ try {
796
+ for (var values_2 = __values(values), values_2_1 = values_2.next(); !values_2_1.done; values_2_1 = values_2.next()) {
797
+ var value = values_2_1.value;
798
+ var source = plainObject(value);
799
+ try {
800
+ for (var _c = (e_19 = void 0, __values(Object.entries(source))), _d = _c.next(); !_d.done; _d = _c.next()) {
801
+ var _e = __read(_d.value, 2), key = _e[0], entry = _e[1];
802
+ if (entry === undefined || entry === null || entry === '') {
803
+ continue;
804
+ }
805
+ if (Array.isArray(entry)) {
806
+ merged[key] = __spreadArray(__spreadArray([], __read(asArray(merged[key])), false), __read(entry), false);
807
+ continue;
808
+ }
809
+ if (plainObject(entry) === entry && plainObject(merged[key]) === merged[key]) {
810
+ merged[key] = __assign(__assign({}, merged[key]), entry);
811
+ continue;
812
+ }
813
+ merged[key] = entry;
814
+ }
815
+ }
816
+ catch (e_19_1) { e_19 = { error: e_19_1 }; }
817
+ finally {
818
+ try {
819
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
820
+ }
821
+ finally { if (e_19) throw e_19.error; }
822
+ }
823
+ }
824
+ }
825
+ catch (e_18_1) { e_18 = { error: e_18_1 }; }
826
+ finally {
827
+ try {
828
+ if (values_2_1 && !values_2_1.done && (_a = values_2.return)) _a.call(values_2);
829
+ }
830
+ finally { if (e_18) throw e_18.error; }
831
+ }
832
+ return merged;
833
+ }
834
+ function buildSupportQaEvidence(input) {
835
+ var job = input.job || {};
836
+ var ticket = input.ticket || {};
837
+ var qaCursor = plainObject(job.supportQaValidationCursor || job.support_qa_validation_cursor);
838
+ var autonomousDecision = plainObject(job.supportV5AutonomousDecision || job.support_v5_autonomous_decision);
839
+ var businessProofReadiness = plainObject(autonomousDecision.businessProofReadiness
840
+ || autonomousDecision.business_proof_readiness
841
+ || job.businessProofReadiness
842
+ || job.business_proof_readiness);
843
+ var customerReplyPolicy = plainObject(autonomousDecision.customerReplyPolicy
844
+ || autonomousDecision.customer_reply_policy
845
+ || job.customerReplyPolicy
846
+ || job.customer_reply_policy);
847
+ var humanReviewPacket = plainObject(autonomousDecision.humanReviewPacket
848
+ || autonomousDecision.human_review_packet
849
+ || customerReplyPolicy.humanReviewPacket
850
+ || customerReplyPolicy.human_review_packet
851
+ || job.humanReviewPacket
852
+ || job.human_review_packet);
853
+ var evidence = mergeEvidenceRecords(qaCursor, job.qaEvidence, job.qa_evidence, ticket.qaEvidence, ticket.qa_evidence, input.qaEvidence);
854
+ evidence.supportQaAssertions = __spreadArray(__spreadArray(__spreadArray([], __read(asArray(job.supportQaAssertions)), false), __read(asArray(job.support_qa_assertions)), false), __read(asArray(evidence.supportQaAssertions)), false);
855
+ evidence.aiQaBusinessAssertions = __spreadArray(__spreadArray(__spreadArray([], __read(asArray(job.aiQaBusinessAssertions)), false), __read(asArray(job.ai_qa_business_assertions)), false), __read(asArray(evidence.aiQaBusinessAssertions)), false);
856
+ evidence.runnerEvidenceArtifacts = __spreadArray(__spreadArray(__spreadArray([], __read(asArray(job.runnerEvidenceArtifacts)), false), __read(asArray(job.runner_evidence_artifacts)), false), __read(asArray(evidence.runnerEvidenceArtifacts)), false);
857
+ evidence.supportQaArtifacts = __spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(job.supportQaArtifacts)), false), __read(asArray(job.support_qa_artifacts)), false), __read(asArray(job.qa_artifacts)), false), __read(asArray(evidence.supportQaArtifacts)), false);
858
+ if (Object.keys(businessProofReadiness).length) {
859
+ evidence.businessProofReadiness = businessProofReadiness;
860
+ }
861
+ if (Object.keys(customerReplyPolicy).length) {
862
+ evidence.customerReplyPolicy = __assign(__assign({}, customerReplyPolicy), (Object.keys(humanReviewPacket).length ? { humanReviewPacket: humanReviewPacket } : {}));
863
+ }
864
+ else if (Object.keys(humanReviewPacket).length) {
865
+ evidence.customerReplyPolicy = { humanReviewPacket: humanReviewPacket };
866
+ }
867
+ return evidence;
868
+ }
869
+ function supportBusinessProofReadinessGate(readiness, now) {
870
+ if (!readiness || !Object.keys(readiness).length) {
871
+ return undefined;
872
+ }
873
+ var ready = readiness.ready === true;
874
+ var readinessStatus = cleanText(readiness.status, 120).toLowerCase();
875
+ var blockers = cleanStringList(readiness.blockers, 20, 500);
876
+ var artifactPaths = cleanStringList(readiness.artifactPaths || readiness.artifact_paths, 40, 500);
877
+ var proofFingerprint = cleanText(readiness.proofFingerprint || readiness.proof_fingerprint, 160);
878
+ var artifactFingerprint = cleanText(readiness.artifactFingerprint || readiness.artifact_fingerprint, 160);
879
+ var proofFreshness = cleanText(readiness.proofFreshness || readiness.proof_freshness, 120);
880
+ var status = ready
881
+ ? 'pass'
882
+ : /fail|failed/.test(readinessStatus)
883
+ ? 'fail'
884
+ : /missing|route_only|blocked|weak|stale|incomplete/.test(readinessStatus)
885
+ ? 'blocked'
886
+ : 'warn';
887
+ return {
888
+ key: 'support_business_proof_readiness',
889
+ label: 'Support business proof readiness',
890
+ status: status,
891
+ reason: cleanText(readiness.reason || blockers.join('; ') || readinessStatus || 'Support business proof readiness recorded.', 1200),
892
+ evidenceRefs: artifactPaths,
893
+ recordedAt: isoNow(now || readiness.recordedAt || readiness.recorded_at),
894
+ metadata: {
895
+ ready: ready,
896
+ status: readinessStatus,
897
+ blockers: blockers,
898
+ requiredEvidence: cleanStringList(readiness.requiredEvidence || readiness.required_evidence, 20, 500),
899
+ proofFingerprint: proofFingerprint,
900
+ artifactFingerprint: artifactFingerprint,
901
+ proofFreshness: proofFreshness
902
+ }
903
+ };
904
+ }
905
+ function supportCustomerReplyPolicyGate(policy, now) {
906
+ if (!policy || !Object.keys(policy).length) {
907
+ return undefined;
908
+ }
909
+ var action = cleanText(policy.action, 120);
910
+ var safety = cleanText(policy.safety, 120);
911
+ var reason = cleanText(policy.reason, 1200);
912
+ var canDraft = policy.canDraftCustomerReply === true || policy.can_draft_customer_reply === true;
913
+ var canSend = policy.canSendCustomerReply === true || policy.can_send_customer_reply === true;
914
+ var reviewPacket = plainObject(policy.humanReviewPacket || policy.human_review_packet);
915
+ var reviewType = cleanText(reviewPacket.reviewType || reviewPacket.review_type, 160);
916
+ var evidenceRefs = cleanStringList(reviewPacket.evidenceRefs || reviewPacket.evidence_refs, 40, 500);
917
+ var blockers = cleanStringList(policy.blockers || reviewPacket.blockers, 20, 500);
918
+ var gateStatus = canSend
919
+ ? 'blocked'
920
+ : action === 'draft_resolution_reply' && canDraft
921
+ ? 'warn'
922
+ : action === 'ask_clarification' && canDraft
923
+ ? 'warn'
924
+ : 'blocked';
925
+ return {
926
+ key: 'support_customer_reply_policy',
927
+ label: 'Support customer reply policy',
928
+ status: gateStatus,
929
+ reason: reason || action || 'Support customer reply policy recorded.',
930
+ evidenceRefs: evidenceRefs,
931
+ recordedAt: isoNow(now || policy.recordedAt || policy.recorded_at || reviewPacket.createdAt || reviewPacket.created_at),
932
+ metadata: {
933
+ action: action,
934
+ safety: safety,
935
+ canDraftCustomerReply: canDraft,
936
+ canSendCustomerReply: canSend,
937
+ confidenceLevel: cleanText(policy.confidenceLevel || policy.confidence_level, 120),
938
+ reviewType: reviewType,
939
+ primaryAction: cleanText(reviewPacket.primaryAction || reviewPacket.primary_action, 160),
940
+ requiresHumanApproval: reviewPacket.requiresHumanApproval !== false && reviewPacket.requires_human_approval !== false,
941
+ questionPresent: !!cleanText(policy.clarificationQuestion || policy.clarification_question || reviewPacket.question, 1000),
942
+ blockers: blockers
943
+ }
944
+ };
945
+ }
946
+ function firstNonEmptyText(values, max) {
947
+ var e_20, _a;
948
+ if (max === void 0) { max = 1000; }
949
+ try {
950
+ for (var values_3 = __values(values), values_3_1 = values_3.next(); !values_3_1.done; values_3_1 = values_3.next()) {
951
+ var value = values_3_1.value;
952
+ var normalized = idText(value, max);
953
+ if (normalized) {
954
+ return normalized;
955
+ }
956
+ }
957
+ }
958
+ catch (e_20_1) { e_20 = { error: e_20_1 }; }
959
+ finally {
960
+ try {
961
+ if (values_3_1 && !values_3_1.done && (_a = values_3.return)) _a.call(values_3);
962
+ }
963
+ finally { if (e_20) throw e_20.error; }
964
+ }
965
+ return undefined;
966
+ }
967
+ function buildAICoderQaEvidence(input) {
968
+ var _a, _b, _c;
969
+ var app = input.app || {};
970
+ var job = input.job || {};
971
+ var workflowMemory = plainObject(job.aiCoderV6WorkflowMemory
972
+ || job.ai_coder_v6_workflow_memory
973
+ || app.aiCoderV6WorkflowMemory
974
+ || app.ai_coder_v6_workflow_memory);
975
+ var evidence = mergeEvidenceRecords(job.qaEvidence, job.qa_evidence, app.qaEvidence, app.qa_evidence, input.qaEvidence);
976
+ evidence.workflowQaRows = __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(workflowMemory.workflowQaRows)), false), __read(asArray(workflowMemory.workflow_qa_rows)), false), __read(asArray(job.workflowQaRows)), false), __read(asArray(job.workflow_qa_rows)), false), __read(asArray(app.workflowQaRows)), false), __read(asArray(app.workflow_qa_rows)), false), __read(asArray(evidence.workflowQaRows)), false), __read(asArray(evidence.workflow_qa_rows)), false);
977
+ evidence.businessAssertions = __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(job.aiQaBusinessAssertions)), false), __read(asArray(job.ai_qa_business_assertions)), false), __read(asArray(job.businessAssertions)), false), __read(asArray(job.business_assertions)), false), __read(asArray(app.businessAssertions)), false), __read(asArray(app.business_assertions)), false), __read(asArray(evidence.businessAssertions)), false), __read(asArray(evidence.business_assertions)), false);
978
+ evidence.routeProbes = __spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(job.workflowProbes)), false), __read(asArray(job.workflow_probes)), false), __read(asArray(evidence.routeProbes)), false), __read(asArray(evidence.route_probes)), false);
979
+ evidence.artifactPaths = __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(workflowMemory.businessProofArtifacts)), false), __read(asArray(workflowMemory.business_proof_artifacts)), false), __read(asArray(job.artifactPaths)), false), __read(asArray(job.artifact_paths)), false), __read(asArray(app.artifactPaths)), false), __read(asArray(app.artifact_paths)), false), __read(asArray(evidence.artifactPaths)), false), __read(asArray(evidence.artifact_paths)), false);
980
+ evidence.journeyContract = evidence.journeyContract
981
+ || evidence.journey_contract
982
+ || job.journeyContract
983
+ || job.journey_contract
984
+ || app.journeyContract
985
+ || app.journey_contract
986
+ || workflowMemory.journeyContract
987
+ || workflowMemory.journey_contract;
988
+ evidence.journeyContractMarkdown = evidence.journeyContractMarkdown
989
+ || evidence.journey_contract_markdown
990
+ || job.journeyContractMarkdown
991
+ || job.journey_contract_markdown
992
+ || app.journeyContractMarkdown
993
+ || app.journey_contract_markdown;
994
+ evidence.deployStatus = firstNonEmptyText([
995
+ evidence.deployStatus,
996
+ evidence.deploy_status,
997
+ job.deployStatus,
998
+ job.deploy_status,
999
+ job.testDeployStatus,
1000
+ job.test_deploy_status,
1001
+ app.deployStatus,
1002
+ app.deploy_status
1003
+ ], 160);
1004
+ evidence.publishStatus = firstNonEmptyText([
1005
+ evidence.publishStatus,
1006
+ evidence.publish_status,
1007
+ evidence.pullRequestStatus,
1008
+ evidence.pull_request_status,
1009
+ job.publishStatus,
1010
+ job.publish_status,
1011
+ job.pullRequestStatus,
1012
+ job.pull_request_status,
1013
+ app.publishStatus,
1014
+ app.publish_status
1015
+ ], 160);
1016
+ evidence.sampleDataStatus = firstNonEmptyText([
1017
+ evidence.sampleDataStatus,
1018
+ evidence.sample_data_status,
1019
+ job.sampleDataStatus,
1020
+ job.sample_data_status,
1021
+ app.sampleDataStatus,
1022
+ app.sample_data_status
1023
+ ], 160) || statusFromBoolean((_c = (_b = (_a = evidence.sampleDataReady) !== null && _a !== void 0 ? _a : evidence.hasSampleData) !== null && _b !== void 0 ? _b : job.sampleDataReady) !== null && _c !== void 0 ? _c : app.sampleDataReady);
1024
+ return evidence;
1025
+ }
1026
+ function normalizeAICoderWorkflowProofReadiness(value, now) {
1027
+ if (!value || !Object.keys(value).length) {
1028
+ return undefined;
1029
+ }
1030
+ var ready = value.ready === true;
1031
+ var status = cleanText(value.status, 120) || (ready ? 'ready' : 'blocked');
1032
+ return {
1033
+ ready: ready,
1034
+ status: status,
1035
+ reason: cleanText(value.reason || value.summary || status, 1200),
1036
+ blockers: cleanStringList(value.blockers, 40, 500),
1037
+ journeyContractValid: value.journeyContractValid === true || value.journey_contract_valid === true,
1038
+ primaryWorkflowId: cleanText(value.primaryWorkflowId || value.primary_workflow_id, 200),
1039
+ passedWorkflowRows: cleanStringList(value.passedWorkflowRows || value.passed_workflow_rows, 80, 300),
1040
+ missingWorkflowRows: cleanStringList(value.missingWorkflowRows || value.missing_workflow_rows, 80, 300),
1041
+ failedWorkflowRows: cleanStringList(value.failedWorkflowRows || value.failed_workflow_rows, 80, 300),
1042
+ passedBusinessAssertions: cleanStringList(value.passedBusinessAssertions || value.passed_business_assertions, 80, 500),
1043
+ routeOnly: value.routeOnly === true || value.route_only === true,
1044
+ scorecardOnly: value.scorecardOnly === true || value.scorecard_only === true,
1045
+ sampleDataReady: value.sampleDataReady === true || value.sample_data_ready === true,
1046
+ releaseBlockers: cleanStringList(value.releaseBlockers || value.release_blockers, 40, 500),
1047
+ artifactPaths: cleanStringList(value.artifactPaths || value.artifact_paths, 80, 500),
1048
+ workflowProofFingerprint: cleanText(value.workflowProofFingerprint || value.workflow_proof_fingerprint, 200),
1049
+ artifactFingerprint: cleanText(value.artifactFingerprint || value.artifact_fingerprint, 200),
1050
+ proofFreshness: cleanText(value.proofFreshness || value.proof_freshness, 80),
1051
+ nextAction: cleanText(value.nextAction || value.next_action, 1000),
1052
+ evaluatedAt: isoNow(now || value.evaluatedAt || value.evaluated_at || value.recordedAt || value.recorded_at)
1053
+ };
1054
+ }
1055
+ function aicoderWorkflowProofReadinessGate(readiness, now) {
1056
+ var status = readiness.ready
1057
+ ? 'pass'
1058
+ : readiness.status === 'release_blocked' || readiness.status === 'journey_invalid' || readiness.status === 'sample_data_missing'
1059
+ ? 'fail'
1060
+ : 'blocked';
1061
+ return {
1062
+ key: 'aicoder_workflow_proof_readiness',
1063
+ label: 'AICoder workflow proof readiness',
1064
+ status: status,
1065
+ reason: readiness.reason || readiness.blockers.join('; ') || readiness.status,
1066
+ evidenceRefs: readiness.artifactPaths,
1067
+ recordedAt: isoNow(now || readiness.evaluatedAt),
1068
+ metadata: {
1069
+ ready: readiness.ready,
1070
+ status: readiness.status,
1071
+ primaryWorkflowId: readiness.primaryWorkflowId,
1072
+ journeyContractValid: readiness.journeyContractValid,
1073
+ blockers: readiness.blockers,
1074
+ passedWorkflowRows: readiness.passedWorkflowRows,
1075
+ missingWorkflowRows: readiness.missingWorkflowRows,
1076
+ failedWorkflowRows: readiness.failedWorkflowRows,
1077
+ passedBusinessAssertions: readiness.passedBusinessAssertions,
1078
+ routeOnly: readiness.routeOnly,
1079
+ scorecardOnly: readiness.scorecardOnly,
1080
+ sampleDataReady: readiness.sampleDataReady,
1081
+ releaseBlockers: readiness.releaseBlockers,
1082
+ workflowProofFingerprint: readiness.workflowProofFingerprint,
1083
+ artifactFingerprint: readiness.artifactFingerprint,
1084
+ proofFreshness: readiness.proofFreshness,
1085
+ nextAction: readiness.nextAction
1086
+ }
1087
+ };
1088
+ }
1089
+ function adapterStatusPassed(value) {
1090
+ var normalized = cleanText(value, 120).toLowerCase();
1091
+ return !!normalized && /(pass|passed|success|succeeded|ok|done|complete|completed|ready|saved|published|deployed)/i.test(normalized);
1092
+ }
1093
+ function applyAICoderWorkflowProofGate(qa, readiness, now) {
1094
+ if (readiness.ready || readiness.status === 'release_blocked') {
1095
+ return qa;
1096
+ }
1097
+ var gate = aicoderWorkflowProofReadinessGate(readiness, now);
1098
+ return __assign(__assign({}, qa), { outcome: qa.routeProbes.some(function (probe) { return adapterStatusPassed(probe.status); })
1099
+ ? 'route_only_pass'
1100
+ : 'incomplete', gateResults: __spreadArray(__spreadArray([], __read(qa.gateResults.filter(function (existing) { return !(existing.key === 'qa_business_assertion' && existing.status === 'pass'); })), false), [
1101
+ gate
1102
+ ], false) });
1103
+ }
1104
+ function assistantQualityObject(input) {
1105
+ var _a, _b;
1106
+ var conversation = input.conversation || {};
1107
+ var latestAssistantMessage = __spreadArray([], __read(asArray(input.messages)), false).reverse()
1108
+ .find(function (message) { return /assistant/i.test(cleanText(message.role || message.authorRole || message.type, 80)); });
1109
+ return plainObject(input.answerQuality
1110
+ || conversation.answerQuality
1111
+ || conversation.answer_quality
1112
+ || conversation.assistantAnswerQuality
1113
+ || conversation.assistant_answer_quality
1114
+ || (latestAssistantMessage === null || latestAssistantMessage === void 0 ? void 0 : latestAssistantMessage.answerQuality)
1115
+ || (latestAssistantMessage === null || latestAssistantMessage === void 0 ? void 0 : latestAssistantMessage.answer_quality)
1116
+ || ((_a = latestAssistantMessage === null || latestAssistantMessage === void 0 ? void 0 : latestAssistantMessage.metadata) === null || _a === void 0 ? void 0 : _a.answerQuality)
1117
+ || ((_b = latestAssistantMessage === null || latestAssistantMessage === void 0 ? void 0 : latestAssistantMessage.metadata) === null || _b === void 0 ? void 0 : _b.answer_quality));
1118
+ }
1119
+ function assistantConfidenceLevel(value) {
1120
+ var normalized = cleanText((value === null || value === void 0 ? void 0 : value.level) || (value === null || value === void 0 ? void 0 : value.confidenceLevel) || (value === null || value === void 0 ? void 0 : value.confidence_level) || value, 80).toLowerCase();
1121
+ if (/^high$/.test(normalized)) {
1122
+ return 'high';
1123
+ }
1124
+ if (/^medium$/.test(normalized)) {
1125
+ return 'medium';
1126
+ }
1127
+ if (/^low$/.test(normalized)) {
1128
+ return 'low';
1129
+ }
1130
+ return 'unknown';
1131
+ }
1132
+ function assistantQueryStatus(value) {
1133
+ var normalized = cleanText((value === null || value === void 0 ? void 0 : value.status) || (value === null || value === void 0 ? void 0 : value.queryStatus) || (value === null || value === void 0 ? void 0 : value.query_status) || value, 120).toLowerCase().replace(/[\s-]+/g, '_');
1134
+ if (/^(ok|success|answered|data_found|data)$/.test(normalized)) {
1135
+ return 'ok';
1136
+ }
1137
+ if (/^(no_data|no_results|empty|not_found)$/.test(normalized)) {
1138
+ return 'no_data';
1139
+ }
1140
+ if (/^(query_error|mongo_error|error|failed)$/.test(normalized)) {
1141
+ return 'query_error';
1142
+ }
1143
+ if (/^(permission_error|permission_denied|forbidden|unauthorized)$/.test(normalized)) {
1144
+ return 'permission_error';
1145
+ }
1146
+ return 'unknown';
1147
+ }
1148
+ function asObjectList(value) {
1149
+ return asArray(value).filter(function (entry) { return entry && typeof entry === 'object' && !Array.isArray(entry); });
1150
+ }
1151
+ function assistantEvidenceRefs(quality) {
1152
+ return cleanStringList(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(quality.evidenceRefs)), false), __read(asArray(quality.evidence_refs)), false), __read(asArray(quality.citationRefs)), false), __read(asArray(quality.citation_refs)), false), __read(asArray(quality.artifactPaths)), false), __read(asArray(quality.artifact_paths)), false), __read(asObjectList(quality.citations).map(function (citation) { return citation.path || citation.url || citation.id || citation.ref; })), false), __read(asObjectList(quality.evidence).map(function (entry) { return entry.path || entry.url || entry.id || entry.ref; })), false), 80, 500);
1153
+ }
1154
+ function assistantCitationRefs(quality) {
1155
+ return cleanStringList(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(quality.citationRefs)), false), __read(asArray(quality.citation_refs)), false), __read(asObjectList(quality.citations).map(function (citation) { return citation.path || citation.url || citation.id || citation.ref; })), false), __read(asObjectList(quality.evidence).map(function (entry) { return entry.citation || entry.ref || entry.path || entry.url; })), false), 80, 500);
1156
+ }
1157
+ function assistantQueryExecutions(quality) {
1158
+ var queryExecution = plainObject(quality.queryExecution || quality.query_execution);
1159
+ var toolResult = plainObject(quality.toolResult || quality.tool_result);
1160
+ return __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asObjectList(quality.queryExecutions)), false), __read(asObjectList(quality.query_executions)), false), __read(asObjectList(quality.dataQueries)), false), __read(asObjectList(quality.data_queries)), false), __read(asObjectList(quality.toolResults)), false), __read(asObjectList(quality.tool_results)), false), __read(asObjectList(quality.mongoReads)), false), __read(asObjectList(quality.mongo_reads)), false), __read(asObjectList(quality.mongoAggregations)), false), __read(asObjectList(quality.mongo_aggregations)), false), __read((Object.keys(queryExecution).length ? [queryExecution] : [])), false), __read((Object.keys(toolResult).length ? [toolResult] : [])), false).filter(function (entry) { return Object.keys(entry).length; });
1161
+ }
1162
+ function assistantQueryEvidenceRefs(quality, executions) {
1163
+ if (executions === void 0) { executions = assistantQueryExecutions(quality); }
1164
+ return cleanStringList(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(quality.queryEvidenceRefs)), false), __read(asArray(quality.query_evidence_refs)), false), __read(asArray(quality.toolResultRefs)), false), __read(asArray(quality.tool_result_refs)), false), __read(asArray(quality.queryResultRefs)), false), __read(asArray(quality.query_result_refs)), false), __read(executions.map(function (execution) {
1165
+ return execution.id
1166
+ || execution._id
1167
+ || execution.ref
1168
+ || execution.toolResultId
1169
+ || execution.tool_result_id
1170
+ || execution.toolCallId
1171
+ || execution.tool_call_id
1172
+ || execution.artifactPath
1173
+ || execution.artifact_path
1174
+ || execution.path
1175
+ || execution.url;
1176
+ })), false), 80, 500);
1177
+ }
1178
+ function assistantNextActions(quality) {
1179
+ return cleanStringList(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(quality.nextActions)), false), __read(asArray(quality.next_actions)), false), __read(asArray(quality.recommendedActions)), false), __read(asArray(quality.recommended_actions)), false), 20, 500);
1180
+ }
1181
+ function normalizedIsoDay(value) {
1182
+ if (!value) {
1183
+ return '';
1184
+ }
1185
+ var date = value instanceof Date ? value : new Date(value);
1186
+ if (!Number.isFinite(date.getTime())) {
1187
+ return cleanText(value, 40).slice(0, 10);
1188
+ }
1189
+ return date.toISOString().slice(0, 10);
1190
+ }
1191
+ function assistantDateWindowObject(quality) {
1192
+ var _a, _b, _c;
1193
+ var dateWindow = plainObject(quality.dateWindow || quality.date_window);
1194
+ var nestedWindow = plainObject(((_b = (_a = quality.verification) === null || _a === void 0 ? void 0 : _a.metrics) === null || _b === void 0 ? void 0 : _b.window) || ((_c = quality.metrics) === null || _c === void 0 ? void 0 : _c.window));
1195
+ return Object.keys(dateWindow).length ? dateWindow : nestedWindow;
1196
+ }
1197
+ function collectMongoProjectionObjects(value, result) {
1198
+ var e_21, _a;
1199
+ if (result === void 0) { result = []; }
1200
+ if (!value || typeof value !== 'object') {
1201
+ return result;
1202
+ }
1203
+ if (Array.isArray(value)) {
1204
+ try {
1205
+ for (var value_2 = __values(value), value_2_1 = value_2.next(); !value_2_1.done; value_2_1 = value_2.next()) {
1206
+ var entry = value_2_1.value;
1207
+ collectMongoProjectionObjects(entry, result);
1208
+ }
1209
+ }
1210
+ catch (e_21_1) { e_21 = { error: e_21_1 }; }
1211
+ finally {
1212
+ try {
1213
+ if (value_2_1 && !value_2_1.done && (_a = value_2.return)) _a.call(value_2);
1214
+ }
1215
+ finally { if (e_21) throw e_21.error; }
1216
+ }
1217
+ return result;
1218
+ }
1219
+ if (value.projection && typeof value.projection === 'object' && !Array.isArray(value.projection)) {
1220
+ result.push(value.projection);
1221
+ }
1222
+ if (value.options && typeof value.options === 'object' && !Array.isArray(value.options) && value.options.projection) {
1223
+ collectMongoProjectionObjects({ projection: value.options.projection }, result);
1224
+ }
1225
+ if (value.query && typeof value.query === 'object') {
1226
+ collectMongoProjectionObjects(value.query, result);
1227
+ }
1228
+ if (value.mongoQuery && typeof value.mongoQuery === 'object') {
1229
+ collectMongoProjectionObjects(value.mongoQuery, result);
1230
+ }
1231
+ if (value.mongo_query && typeof value.mongo_query === 'object') {
1232
+ collectMongoProjectionObjects(value.mongo_query, result);
1233
+ }
1234
+ return result;
1235
+ }
1236
+ function projectionHasPositionalOperator(projection) {
1237
+ return Object.keys(projection || {}).some(function (key) { return /\.(?:\$|\$\[)|\.\$$/.test(key); });
1238
+ }
1239
+ function assistantQueryShapeLegal(quality) {
1240
+ if (quality.legalQueryShape === true || quality.legal_query_shape === true) {
1241
+ return true;
1242
+ }
1243
+ if (quality.legalQueryShape === false || quality.legal_query_shape === false || quality.illegalQueryShape === true || quality.illegal_query_shape === true) {
1244
+ return false;
1245
+ }
1246
+ var queryShape = plainObject(quality.queryShape || quality.query_shape);
1247
+ if (queryShape.legal === true || queryShape.status === 'pass') {
1248
+ return true;
1249
+ }
1250
+ if (queryShape.legal === false || queryShape.status === 'fail') {
1251
+ return false;
1252
+ }
1253
+ var projections = collectMongoProjectionObjects(quality);
1254
+ if (projections.some(projectionHasPositionalOperator)) {
1255
+ return false;
1256
+ }
1257
+ return true;
1258
+ }
1259
+ function assistantCorrectnessFailed(checks) {
1260
+ return checks
1261
+ .filter(function (check) {
1262
+ var status = cleanText(check.status || (check.passed === true ? 'pass' : check.passed === false ? 'fail' : ''), 80).toLowerCase();
1263
+ return /^(fail|failed|blocked|incorrect|wrong|error)$/.test(status);
1264
+ })
1265
+ .map(function (check) { return cleanText(check.assertion || check.name || check.case || check.reason || 'assistant correctness check failed', 300); })
1266
+ .slice(0, 20);
1267
+ }
1268
+ function evaluateAssistantAnswerQuality(input) {
1269
+ var _a, _b;
1270
+ if (input === void 0) { input = {}; }
1271
+ var quality = plainObject(input.answerQuality);
1272
+ var now = isoNow(input.now);
1273
+ var correctnessChecks = asArray(input.correctnessChecks);
1274
+ var failedChecks = assistantCorrectnessFailed(correctnessChecks);
1275
+ var queryStatus = assistantQueryStatus(quality.queryStatus || quality.query_status || quality.queryResult || quality.query_result);
1276
+ var confidenceLevel = assistantConfidenceLevel(quality.confidence || quality.confidenceLevel || quality.confidence_level);
1277
+ var evidenceRefs = assistantEvidenceRefs(quality);
1278
+ var citationRefs = assistantCitationRefs(quality);
1279
+ var queryExecutions = assistantQueryExecutions(quality);
1280
+ var queryEvidenceRefs = assistantQueryEvidenceRefs(quality, queryExecutions);
1281
+ var nextActions = assistantNextActions(quality);
1282
+ var requiresCurrentDate = quality.requiresCurrentDate === true || quality.requires_current_date === true;
1283
+ var dateWindowRequired = quality.requiresDateWindow === true
1284
+ || quality.requires_date_window === true
1285
+ || quality.dateWindowRequired === true
1286
+ || quality.date_window_required === true;
1287
+ var requiresCitations = quality.requiresCitations !== false && quality.requires_citations !== false;
1288
+ var requiresNextAction = quality.requiresNextAction !== false && quality.requires_next_action !== false;
1289
+ var dateWindow = assistantDateWindowObject(quality);
1290
+ var dateWindowStart = cleanText(dateWindow.startDate || dateWindow.start_date || dateWindow.start, 80);
1291
+ var dateWindowEnd = cleanText(dateWindow.endDate || dateWindow.end_date || dateWindow.end, 80);
1292
+ var dateWindowPresent = !!dateWindowStart && !!dateWindowEnd;
1293
+ var dateBasis = cleanText(quality.currentDateUsed
1294
+ || quality.current_date_used
1295
+ || quality.dateBasis
1296
+ || quality.date_basis
1297
+ || ((_a = quality.dateWindow) === null || _a === void 0 ? void 0 : _a.basisDate)
1298
+ || ((_b = quality.date_window) === null || _b === void 0 ? void 0 : _b.basis_date), 80);
1299
+ var expectedCurrentDate = cleanText(quality.expectedCurrentDate || quality.expected_current_date, 80)
1300
+ || normalizedIsoDay(input.now);
1301
+ var noDataConfirmed = quality.noDataConfirmed === true
1302
+ || quality.no_data_confirmed === true
1303
+ || quality.confirmedNoData === true
1304
+ || quality.confirmed_no_data === true;
1305
+ var legalQueryShape = assistantQueryShapeLegal(quality);
1306
+ var queryEvidenceRequired = quality.requiresQueryEvidence !== false
1307
+ && quality.requires_query_evidence !== false
1308
+ && (queryStatus === 'ok' || queryStatus === 'no_data');
1309
+ var queryEvidencePresent = queryExecutions.length > 0 || queryEvidenceRefs.length > 0;
1310
+ var blockers = [];
1311
+ var status = 'ready';
1312
+ var reason = 'Assistant answer has structured data evidence, legal query shape, citations, confidence, and next action.';
1313
+ if (!Object.keys(quality).length) {
1314
+ status = 'missing_data_source';
1315
+ blockers.push('Assistant answerQuality evidence is missing.');
1316
+ reason = 'Assistant answer cannot be accepted without structured answerQuality evidence.';
1317
+ }
1318
+ else if (failedChecks.length) {
1319
+ status = 'incorrect';
1320
+ blockers.push.apply(blockers, __spreadArray([], __read(failedChecks), false));
1321
+ reason = 'Assistant correctness checks failed.';
1322
+ }
1323
+ else if (queryStatus === 'permission_error') {
1324
+ status = 'permission_error';
1325
+ blockers.push('Assistant data query was blocked by permission or authorization.');
1326
+ reason = 'Assistant must report permission-error state and avoid claiming no data or success.';
1327
+ }
1328
+ else if (queryStatus === 'query_error') {
1329
+ status = 'query_error';
1330
+ blockers.push('Assistant data query failed.');
1331
+ reason = 'Assistant must report query-error state and avoid claiming no data or success.';
1332
+ }
1333
+ else if (!legalQueryShape) {
1334
+ status = 'illegal_query_shape';
1335
+ blockers.push('Assistant query shape is illegal or uses an unsafe Mongo projection shape.');
1336
+ reason = 'Assistant must use legal Mongo query/projection shapes before answering.';
1337
+ }
1338
+ else if (requiresCurrentDate && (!dateBasis || normalizedIsoDay(dateBasis) !== normalizedIsoDay(expectedCurrentDate))) {
1339
+ status = 'date_incorrect';
1340
+ blockers.push("Expected current date ".concat(normalizedIsoDay(expectedCurrentDate) || expectedCurrentDate, ", got ").concat(dateBasis || 'missing date basis', "."));
1341
+ reason = 'Assistant answer did not prove it used the actual current date.';
1342
+ }
1343
+ else if (dateWindowRequired && !dateWindowPresent) {
1344
+ status = 'missing_date_window';
1345
+ blockers.push('Assistant dated answer did not record concrete date-window start and end evidence.');
1346
+ reason = 'Assistant dated answers must record the exact query date window used.';
1347
+ }
1348
+ else if (queryStatus === 'unknown') {
1349
+ status = 'missing_data_source';
1350
+ blockers.push('Assistant answer did not record queryStatus ok/no_data/query_error/permission_error.');
1351
+ reason = 'Assistant answer must classify data access result before acceptance.';
1352
+ }
1353
+ else if (queryStatus === 'no_data' && !noDataConfirmed) {
1354
+ status = 'no_data_unverified';
1355
+ blockers.push('No-data answer was not confirmed by a probe or query evidence.');
1356
+ reason = 'Assistant no-data answers require explicit noDataConfirmed evidence.';
1357
+ }
1358
+ else if (queryEvidenceRequired && !queryEvidencePresent) {
1359
+ status = 'missing_query_proof';
1360
+ blockers.push('Assistant data answer did not record a structured query execution, tool result, or query evidence reference.');
1361
+ reason = 'Assistant answers from system data require structured query/tool execution proof before acceptance.';
1362
+ }
1363
+ else if (requiresCitations && !citationRefs.length && !evidenceRefs.length) {
1364
+ status = 'missing_citations';
1365
+ blockers.push('Assistant answer has no cited evidence or artifact reference.');
1366
+ reason = 'Assistant answer must cite system data evidence.';
1367
+ }
1368
+ else if (confidenceLevel === 'low' || confidenceLevel === 'unknown') {
1369
+ status = 'low_confidence';
1370
+ blockers.push('Assistant answer confidence is low or unknown.');
1371
+ reason = 'Assistant answer requires recorded medium/high confidence.';
1372
+ }
1373
+ else if (requiresNextAction && !nextActions.length) {
1374
+ status = 'missing_next_action';
1375
+ blockers.push('Assistant answer did not record next actions.');
1376
+ reason = 'Assistant answer must provide a next action or explicit no-action state.';
1377
+ }
1378
+ return {
1379
+ ready: status === 'ready',
1380
+ status: status,
1381
+ reason: reason,
1382
+ blockers: blockers,
1383
+ queryStatus: queryStatus,
1384
+ confidenceLevel: confidenceLevel,
1385
+ dateBasis: dateBasis || undefined,
1386
+ expectedCurrentDate: expectedCurrentDate || undefined,
1387
+ dateWindowRequired: dateWindowRequired,
1388
+ dateWindowPresent: dateWindowPresent,
1389
+ dateWindow: dateWindowPresent ? {
1390
+ startDate: dateWindowStart,
1391
+ endDate: dateWindowEnd,
1392
+ mode: cleanText(dateWindow.mode || dateWindow.type, 80) || undefined,
1393
+ months: typeof dateWindow.months === 'number' ? dateWindow.months : undefined
1394
+ } : undefined,
1395
+ noDataConfirmed: noDataConfirmed,
1396
+ legalQueryShape: legalQueryShape,
1397
+ citationRefs: citationRefs,
1398
+ queryEvidenceRequired: queryEvidenceRequired,
1399
+ queryEvidencePresent: queryEvidencePresent,
1400
+ queryEvidenceRefs: queryEvidenceRefs,
1401
+ queryExecutionCount: queryExecutions.length,
1402
+ nextActions: nextActions,
1403
+ evidenceRefs: evidenceRefs,
1404
+ failedChecks: failedChecks,
1405
+ recordedAt: now
1406
+ };
1407
+ }
1408
+ function assistantAnswerQualityGate(decision, now) {
1409
+ var status = decision.ready
1410
+ ? 'pass'
1411
+ : /incorrect|date_incorrect|missing_date_window|illegal_query_shape/.test(decision.status)
1412
+ ? 'fail'
1413
+ : 'blocked';
1414
+ return {
1415
+ key: 'assistant_answer_quality',
1416
+ label: 'Assistant answer quality',
1417
+ status: status,
1418
+ reason: decision.reason,
1419
+ evidenceRefs: decision.evidenceRefs.length ? decision.evidenceRefs : decision.citationRefs,
1420
+ recordedAt: isoNow(now || decision.recordedAt),
1421
+ metadata: {
1422
+ ready: decision.ready,
1423
+ status: decision.status,
1424
+ queryStatus: decision.queryStatus,
1425
+ confidenceLevel: decision.confidenceLevel,
1426
+ dateBasis: decision.dateBasis,
1427
+ expectedCurrentDate: decision.expectedCurrentDate,
1428
+ dateWindowRequired: decision.dateWindowRequired,
1429
+ dateWindowPresent: decision.dateWindowPresent,
1430
+ dateWindow: decision.dateWindow,
1431
+ noDataConfirmed: decision.noDataConfirmed,
1432
+ legalQueryShape: decision.legalQueryShape,
1433
+ citationRefs: decision.citationRefs,
1434
+ queryEvidenceRequired: decision.queryEvidenceRequired,
1435
+ queryEvidencePresent: decision.queryEvidencePresent,
1436
+ queryEvidenceRefs: decision.queryEvidenceRefs,
1437
+ queryExecutionCount: decision.queryExecutionCount,
1438
+ nextActions: decision.nextActions,
1439
+ blockers: decision.blockers,
1440
+ failedChecks: decision.failedChecks
1441
+ }
1442
+ };
1443
+ }
1444
+ function applyAssistantAnswerQualityGate(qa, decision, now) {
1445
+ var gate = assistantAnswerQualityGate(decision, now);
1446
+ if (decision.ready) {
1447
+ return __assign(__assign({}, qa), { gateResults: __spreadArray(__spreadArray([], __read(qa.gateResults), false), [gate], false) });
1448
+ }
1449
+ return __assign(__assign({}, qa), { outcome: /incorrect|date_incorrect|missing_date_window|illegal_query_shape/.test(decision.status)
1450
+ ? 'business_assertion_failed'
1451
+ : 'incomplete', gateResults: __spreadArray(__spreadArray([], __read(qa.gateResults.filter(function (existing) { return !(existing.key === 'qa_business_assertion' && existing.status === 'pass'); })), false), [
1452
+ gate
1453
+ ], false) });
1454
+ }
742
1455
  function buildSupportAIRunFromEvidence(input) {
743
- var e_17, _a, e_18, _b;
744
- var _c, _d, _e, _f;
1456
+ var e_22, _a, e_23, _b;
1457
+ var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
745
1458
  var ticket = input.ticket || {};
746
1459
  var job = input.job || {};
747
- var evidence = evidenceObject(input.qaEvidence, job.qaEvidence, job.qa_evidence, ticket.qaEvidence, ticket.qa_evidence);
1460
+ var evidence = buildSupportQaEvidence(input);
748
1461
  var events = [];
749
1462
  var gates = [];
750
1463
  var sourceIds = {};
@@ -753,8 +1466,8 @@ function buildSupportAIRunFromEvidence(input) {
753
1466
  addSourceId(sourceIds, 'jobId', job._id || job.id || job.jobId);
754
1467
  addSourceId(sourceIds, 'buildPlanId', job.buildPlanId || ticket.buildPlanId);
755
1468
  try {
756
- for (var _g = __values(asArray(input.taskEvents)), _h = _g.next(); !_h.done; _h = _g.next()) {
757
- var event_1 = _h.value;
1469
+ for (var _o = __values(asArray(input.taskEvents)), _p = _o.next(); !_p.done; _p = _o.next()) {
1470
+ var event_1 = _p.value;
758
1471
  pushEvent(events, {
759
1472
  type: event_1.type === 'human_intervention' ? 'human_intervention' : 'log',
760
1473
  category: cleanText(event_1.category || event_1.phase || event_1.type, 160),
@@ -768,16 +1481,16 @@ function buildSupportAIRunFromEvidence(input) {
768
1481
  });
769
1482
  }
770
1483
  }
771
- catch (e_17_1) { e_17 = { error: e_17_1 }; }
1484
+ catch (e_22_1) { e_22 = { error: e_22_1 }; }
772
1485
  finally {
773
1486
  try {
774
- if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
1487
+ if (_p && !_p.done && (_a = _o.return)) _a.call(_o);
775
1488
  }
776
- finally { if (e_17) throw e_17.error; }
1489
+ finally { if (e_22) throw e_22.error; }
777
1490
  }
778
1491
  try {
779
- for (var _j = __values(asArray(input.aiJobs)), _k = _j.next(); !_k.done; _k = _j.next()) {
780
- var aiJob = _k.value;
1492
+ for (var _q = __values(asArray(input.aiJobs)), _r = _q.next(); !_r.done; _r = _q.next()) {
1493
+ var aiJob = _r.value;
781
1494
  pushEvent(events, {
782
1495
  type: aiJob.model ? 'model_call' : 'log',
783
1496
  category: cleanText(aiJob.category || aiJob.phase || aiJob.runner, 160),
@@ -792,15 +1505,41 @@ function buildSupportAIRunFromEvidence(input) {
792
1505
  });
793
1506
  }
794
1507
  }
795
- catch (e_18_1) { e_18 = { error: e_18_1 }; }
1508
+ catch (e_23_1) { e_23 = { error: e_23_1 }; }
796
1509
  finally {
797
1510
  try {
798
- if (_k && !_k.done && (_b = _j.return)) _b.call(_j);
1511
+ if (_r && !_r.done && (_b = _q.return)) _b.call(_q);
799
1512
  }
800
- finally { if (e_18) throw e_18.error; }
1513
+ finally { if (e_23) throw e_23.error; }
801
1514
  }
802
1515
  collectCommitEvents(asArray(input.commits), events);
803
1516
  collectHotfixEvidenceEvents([job, ticket, evidence, input.qaEvidence], events, gates, input.now);
1517
+ var businessProofReadiness = plainObject(evidence.businessProofReadiness || evidence.business_proof_readiness);
1518
+ var businessProofReadinessGate = supportBusinessProofReadinessGate(businessProofReadiness, input.now);
1519
+ if (businessProofReadinessGate) {
1520
+ gates.push(businessProofReadinessGate);
1521
+ pushEvent(events, {
1522
+ type: 'qa_business_assertion',
1523
+ category: 'support_business_proof_readiness',
1524
+ message: businessProofReadinessGate.reason,
1525
+ artifactPaths: businessProofReadinessGate.evidenceRefs,
1526
+ recordedAt: businessProofReadinessGate.recordedAt,
1527
+ metadata: businessProofReadinessGate.metadata
1528
+ });
1529
+ }
1530
+ var customerReplyPolicy = plainObject(evidence.customerReplyPolicy || evidence.customer_reply_policy);
1531
+ var customerReplyPolicyGate = supportCustomerReplyPolicyGate(customerReplyPolicy, input.now);
1532
+ if (customerReplyPolicyGate) {
1533
+ gates.push(customerReplyPolicyGate);
1534
+ pushEvent(events, {
1535
+ type: 'human_intervention',
1536
+ category: 'support_customer_reply_policy',
1537
+ message: customerReplyPolicyGate.reason,
1538
+ artifactPaths: customerReplyPolicyGate.evidenceRefs,
1539
+ recordedAt: customerReplyPolicyGate.recordedAt,
1540
+ metadata: customerReplyPolicyGate.metadata
1541
+ });
1542
+ }
804
1543
  var cost = collectUsageEvents(asArray(input.usageLedger), events);
805
1544
  var diagnosisGate = evidenceObject(job.supportV5DiagnosisGate, job.support_v5_diagnosis_gate, job.diagnosisGate, ticket.supportV5DiagnosisGate, ticket.diagnosisGate, evidence.diagnosisGate, evidence.supportV5DiagnosisGate);
806
1545
  var qa = applySupportDiagnosisProofGate(buildQaFromEvidence(evidence, input.now), diagnosisGate, input.now);
@@ -849,16 +1588,35 @@ function buildSupportAIRunFromEvidence(input) {
849
1588
  ownerFiles: diagnosisOwnerFiles,
850
1589
  acceptedHypothesis: cleanText(((_e = diagnosisGate.accepted_hypothesis) === null || _e === void 0 ? void 0 : _e.statement) || ((_f = diagnosisGate.acceptedHypothesis) === null || _f === void 0 ? void 0 : _f.statement), 1000),
851
1590
  proofPlan: diagnosisGate.proof_plan || diagnosisGate.proofPlan
1591
+ } : undefined,
1592
+ businessProofReadiness: Object.keys(businessProofReadiness).length ? {
1593
+ ready: businessProofReadiness.ready === true,
1594
+ status: cleanText(businessProofReadiness.status, 120),
1595
+ reason: cleanText(businessProofReadiness.reason, 1000),
1596
+ blockers: cleanStringList(businessProofReadiness.blockers, 20, 500)
1597
+ } : undefined,
1598
+ customerReplyPolicy: Object.keys(customerReplyPolicy).length ? {
1599
+ action: cleanText(customerReplyPolicy.action, 120),
1600
+ safety: cleanText(customerReplyPolicy.safety, 120),
1601
+ canDraftCustomerReply: customerReplyPolicy.canDraftCustomerReply === true || customerReplyPolicy.can_draft_customer_reply === true,
1602
+ canSendCustomerReply: customerReplyPolicy.canSendCustomerReply === true || customerReplyPolicy.can_send_customer_reply === true,
1603
+ reason: cleanText(customerReplyPolicy.reason, 1000),
1604
+ reviewType: cleanText(((_g = customerReplyPolicy.humanReviewPacket) === null || _g === void 0 ? void 0 : _g.reviewType)
1605
+ || ((_h = customerReplyPolicy.human_review_packet) === null || _h === void 0 ? void 0 : _h.review_type)
1606
+ || ((_j = customerReplyPolicy.human_review_packet) === null || _j === void 0 ? void 0 : _j.reviewType), 160),
1607
+ primaryAction: cleanText(((_k = customerReplyPolicy.humanReviewPacket) === null || _k === void 0 ? void 0 : _k.primaryAction)
1608
+ || ((_l = customerReplyPolicy.human_review_packet) === null || _l === void 0 ? void 0 : _l.primary_action)
1609
+ || ((_m = customerReplyPolicy.human_review_packet) === null || _m === void 0 ? void 0 : _m.primaryAction), 160)
852
1610
  } : undefined
853
1611
  }
854
1612
  });
855
1613
  }
856
1614
  function buildAICoderAIRunFromEvidence(input) {
857
- var e_19, _a;
1615
+ var e_24, _a;
858
1616
  var _b, _c, _d, _e, _f, _g, _h;
859
1617
  var app = input.app || {};
860
1618
  var job = input.job || {};
861
- var evidence = evidenceObject(input.qaEvidence, job.qaEvidence, job.qa_evidence, app.qaEvidence, app.qa_evidence);
1619
+ var evidence = buildAICoderQaEvidence(input);
862
1620
  var events = [];
863
1621
  var gates = [];
864
1622
  var sourceIds = {};
@@ -882,20 +1640,69 @@ function buildAICoderAIRunFromEvidence(input) {
882
1640
  });
883
1641
  }
884
1642
  }
885
- catch (e_19_1) { e_19 = { error: e_19_1 }; }
1643
+ catch (e_24_1) { e_24 = { error: e_24_1 }; }
886
1644
  finally {
887
1645
  try {
888
1646
  if (_k && !_k.done && (_a = _j.return)) _a.call(_j);
889
1647
  }
890
- finally { if (e_19) throw e_19.error; }
1648
+ finally { if (e_24) throw e_24.error; }
891
1649
  }
892
1650
  collectCommitEvents(asArray(input.commits), events);
893
1651
  collectHotfixEvidenceEvents([job, app, evidence, input.qaEvidence], events, gates, input.now);
894
1652
  var cost = collectUsageEvents(asArray(input.usageLedger), events);
895
- var qa = buildQaFromEvidence(evidence, input.now);
896
1653
  var scorecardPassed = evidence.scorecardPassed === true
897
1654
  || evidence.scorecardStatus === 'pass'
898
1655
  || (Number((_c = (_b = evidence.score) !== null && _b !== void 0 ? _b : evidence.qualityScore) !== null && _c !== void 0 ? _c : 0) >= Number((_e = (_d = evidence.scoreThreshold) !== null && _d !== void 0 ? _d : job.scoreThreshold) !== null && _e !== void 0 ? _e : 90));
1656
+ var explicitWorkflowReadiness = normalizeAICoderWorkflowProofReadiness(plainObject(evidence.workflowProofReadiness
1657
+ || evidence.workflow_proof_readiness
1658
+ || evidence.aicoderWorkflowProofReadiness
1659
+ || evidence.aicoder_workflow_proof_readiness
1660
+ || job.workflowProofReadiness
1661
+ || job.workflow_proof_readiness
1662
+ || job.aicoderWorkflowProofReadiness
1663
+ || job.aicoder_workflow_proof_readiness), input.now);
1664
+ var workflowReadiness = explicitWorkflowReadiness || (0, aicoder_runner_v6_1.evaluateResolveIOAICoderWorkflowProofReadiness)({
1665
+ journeyContract: evidence.journeyContract,
1666
+ journeyContractMarkdown: evidence.journeyContractMarkdown,
1667
+ workflowQaRows: asArray(evidence.workflowQaRows || evidence.workflow_qa_rows),
1668
+ businessAssertions: __spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(evidence.businessAssertions)), false), __read(asArray(evidence.business_assertions)), false), __read(asArray(evidence.workflowAssertions)), false), __read(asArray(evidence.workflow_assertions)), false),
1669
+ routeProbes: __spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(evidence.routeProbes)), false), __read(asArray(evidence.route_probes)), false), __read(asArray(evidence.workflowProbes)), false), __read(asArray(evidence.workflow_probes)), false),
1670
+ artifactPaths: __spreadArray(__spreadArray([], __read(asArray(evidence.artifactPaths)), false), __read(asArray(evidence.artifact_paths)), false),
1671
+ scorecardPassed: scorecardPassed,
1672
+ deployStatus: evidence.deployStatus,
1673
+ publishStatus: evidence.publishStatus,
1674
+ sampleDataStatus: evidence.sampleDataStatus,
1675
+ previousWorkflowProofFingerprint: firstNonEmptyText([
1676
+ evidence.previousWorkflowProofFingerprint,
1677
+ evidence.previous_workflow_proof_fingerprint,
1678
+ job.previousWorkflowProofFingerprint,
1679
+ job.previous_workflow_proof_fingerprint,
1680
+ app.previousWorkflowProofFingerprint,
1681
+ app.previous_workflow_proof_fingerprint
1682
+ ], 200),
1683
+ previousArtifactFingerprint: firstNonEmptyText([
1684
+ evidence.previousArtifactFingerprint,
1685
+ evidence.previous_artifact_fingerprint,
1686
+ job.previousArtifactFingerprint,
1687
+ job.previous_artifact_fingerprint,
1688
+ app.previousArtifactFingerprint,
1689
+ app.previous_artifact_fingerprint
1690
+ ], 200),
1691
+ previousWorkflowProofFingerprints: __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(evidence.previousWorkflowProofFingerprints)), false), __read(asArray(evidence.previous_workflow_proof_fingerprints)), false), __read(asArray(job.previousWorkflowProofFingerprints)), false), __read(asArray(job.previous_workflow_proof_fingerprints)), false), __read(asArray(app.previousWorkflowProofFingerprints)), false), __read(asArray(app.previous_workflow_proof_fingerprints)), false),
1692
+ previousArtifactFingerprints: __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(asArray(evidence.previousArtifactFingerprints)), false), __read(asArray(evidence.previous_artifact_fingerprints)), false), __read(asArray(job.previousArtifactFingerprints)), false), __read(asArray(job.previous_artifact_fingerprints)), false), __read(asArray(app.previousArtifactFingerprints)), false), __read(asArray(app.previous_artifact_fingerprints)), false),
1693
+ now: input.now
1694
+ });
1695
+ var workflowReadinessGate = aicoderWorkflowProofReadinessGate(workflowReadiness, input.now);
1696
+ gates.push(workflowReadinessGate);
1697
+ pushEvent(events, {
1698
+ type: 'qa_business_assertion',
1699
+ category: 'aicoder_workflow_proof_readiness',
1700
+ message: workflowReadinessGate.reason,
1701
+ artifactPaths: workflowReadinessGate.evidenceRefs,
1702
+ recordedAt: workflowReadinessGate.recordedAt,
1703
+ metadata: workflowReadinessGate.metadata
1704
+ });
1705
+ var qa = applyAICoderWorkflowProofGate(buildQaFromEvidence(evidence, input.now), workflowReadiness, input.now);
899
1706
  return (0, ai_run_evidence_1.buildAIRun)({
900
1707
  source: 'aicoder_app',
901
1708
  sourceIds: sourceIds,
@@ -921,13 +1728,28 @@ function buildAICoderAIRunFromEvidence(input) {
921
1728
  metadata: {
922
1729
  domain: app.domain || app.customDomain || job.domain,
923
1730
  wowScore: (_g = evidence.wowScore) !== null && _g !== void 0 ? _g : job.wowScore,
924
- score: (_h = evidence.score) !== null && _h !== void 0 ? _h : evidence.qualityScore
1731
+ score: (_h = evidence.score) !== null && _h !== void 0 ? _h : evidence.qualityScore,
1732
+ workflowProofReadiness: {
1733
+ ready: workflowReadiness.ready,
1734
+ status: workflowReadiness.status,
1735
+ reason: workflowReadiness.reason,
1736
+ blockers: workflowReadiness.blockers,
1737
+ primaryWorkflowId: workflowReadiness.primaryWorkflowId,
1738
+ passedWorkflowRows: workflowReadiness.passedWorkflowRows,
1739
+ missingWorkflowRows: workflowReadiness.missingWorkflowRows,
1740
+ failedWorkflowRows: workflowReadiness.failedWorkflowRows,
1741
+ sampleDataReady: workflowReadiness.sampleDataReady,
1742
+ releaseBlockers: workflowReadiness.releaseBlockers,
1743
+ workflowProofFingerprint: workflowReadiness.workflowProofFingerprint,
1744
+ artifactFingerprint: workflowReadiness.artifactFingerprint,
1745
+ proofFreshness: workflowReadiness.proofFreshness
1746
+ }
925
1747
  }
926
1748
  });
927
1749
  }
928
1750
  function buildAssistantAIRunFromEvidence(input) {
929
- var e_20, _a, e_21, _b;
930
- var _c;
1751
+ var e_25, _a, e_26, _b, e_27, _c;
1752
+ var _d, _e, _f, _g;
931
1753
  var conversation = input.conversation || {};
932
1754
  var events = [];
933
1755
  var sourceIds = {};
@@ -937,8 +1759,8 @@ function buildAssistantAIRunFromEvidence(input) {
937
1759
  addSourceId(sourceIds, 'conversationId', conversation._id || conversation.id || conversation.conversationId);
938
1760
  addSourceId(sourceIds, 'userId', conversation.userId || conversation.ownerId);
939
1761
  try {
940
- for (var _d = __values(asArray(input.messages)), _e = _d.next(); !_e.done; _e = _d.next()) {
941
- var message = _e.value;
1762
+ for (var _h = __values(asArray(input.messages)), _j = _h.next(); !_j.done; _j = _h.next()) {
1763
+ var message = _j.value;
942
1764
  pushEvent(events, {
943
1765
  type: 'assistant_message',
944
1766
  category: cleanText(message.role || message.authorRole || message.type, 160),
@@ -947,21 +1769,21 @@ function buildAssistantAIRunFromEvidence(input) {
947
1769
  metadata: {
948
1770
  id: message._id || message.id,
949
1771
  role: message.role,
950
- model: message.model || ((_c = message === null || message === void 0 ? void 0 : message.usage) === null || _c === void 0 ? void 0 : _c.model)
1772
+ model: message.model || ((_d = message === null || message === void 0 ? void 0 : message.usage) === null || _d === void 0 ? void 0 : _d.model)
951
1773
  }
952
1774
  });
953
1775
  }
954
1776
  }
955
- catch (e_20_1) { e_20 = { error: e_20_1 }; }
1777
+ catch (e_25_1) { e_25 = { error: e_25_1 }; }
956
1778
  finally {
957
1779
  try {
958
- if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1780
+ if (_j && !_j.done && (_a = _h.return)) _a.call(_h);
959
1781
  }
960
- finally { if (e_20) throw e_20.error; }
1782
+ finally { if (e_25) throw e_25.error; }
961
1783
  }
962
1784
  try {
963
- for (var _f = __values(asArray(input.issueReports)), _g = _f.next(); !_g.done; _g = _f.next()) {
964
- var report = _g.value;
1785
+ for (var _k = __values(asArray(input.issueReports)), _l = _k.next(); !_l.done; _l = _k.next()) {
1786
+ var report = _l.value;
965
1787
  pushEvent(events, {
966
1788
  type: 'human_intervention',
967
1789
  category: cleanText(report.category || report.status || 'issue_report', 160),
@@ -975,16 +1797,68 @@ function buildAssistantAIRunFromEvidence(input) {
975
1797
  });
976
1798
  }
977
1799
  }
978
- catch (e_21_1) { e_21 = { error: e_21_1 }; }
1800
+ catch (e_26_1) { e_26 = { error: e_26_1 }; }
979
1801
  finally {
980
1802
  try {
981
- if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
1803
+ if (_l && !_l.done && (_b = _k.return)) _b.call(_k);
982
1804
  }
983
- finally { if (e_21) throw e_21.error; }
1805
+ finally { if (e_26) throw e_26.error; }
984
1806
  }
985
1807
  var cost = collectUsageEvents(asArray(input.usageLedger), events);
1808
+ var answerQuality = assistantQualityObject(input);
1809
+ try {
1810
+ for (var _m = __values(assistantQueryExecutions(answerQuality).slice(0, 40)), _o = _m.next(); !_o.done; _o = _m.next()) {
1811
+ var execution = _o.value;
1812
+ var status_1 = cleanText(execution.status || execution.queryStatus || execution.query_status || execution.resultStatus || execution.result_status, 120);
1813
+ var toolName = cleanText(execution.tool || execution.toolName || execution.tool_name || execution.type || 'query', 160);
1814
+ var collection = cleanText(execution.collection || execution.collectionName || execution.collection_name, 240);
1815
+ pushEvent(events, {
1816
+ type: 'assistant_query',
1817
+ category: status_1 || toolName,
1818
+ message: cleanText(execution.summary || execution.message || "".concat(toolName).concat(collection ? " ".concat(collection) : ''), 1200),
1819
+ artifactPaths: cleanStringList(__spreadArray(__spreadArray([
1820
+ execution.artifactPath,
1821
+ execution.artifact_path,
1822
+ execution.path
1823
+ ], __read(asArray(execution.artifactPaths)), false), __read(asArray(execution.artifact_paths)), false), 20, 500),
1824
+ recordedAt: eventDate(execution),
1825
+ metadata: {
1826
+ id: execution.id || execution._id || execution.ref,
1827
+ tool: toolName,
1828
+ status: status_1,
1829
+ collection: collection,
1830
+ resultCount: (_f = (_e = execution.resultCount) !== null && _e !== void 0 ? _e : execution.result_count) !== null && _f !== void 0 ? _f : execution.count,
1831
+ queryStatus: execution.queryStatus || execution.query_status,
1832
+ legalQueryShape: (_g = execution.legalQueryShape) !== null && _g !== void 0 ? _g : execution.legal_query_shape
1833
+ }
1834
+ });
1835
+ }
1836
+ }
1837
+ catch (e_27_1) { e_27 = { error: e_27_1 }; }
1838
+ finally {
1839
+ try {
1840
+ if (_o && !_o.done && (_c = _m.return)) _c.call(_m);
1841
+ }
1842
+ finally { if (e_27) throw e_27.error; }
1843
+ }
1844
+ var answerQualityDecision = evaluateAssistantAnswerQuality({
1845
+ answerQuality: answerQuality,
1846
+ correctnessChecks: input.correctnessChecks,
1847
+ now: input.now
1848
+ });
1849
+ var answerQualityGate = assistantAnswerQualityGate(answerQualityDecision, input.now);
1850
+ pushEvent(events, {
1851
+ type: 'assistant_message',
1852
+ category: 'assistant_answer_quality',
1853
+ message: answerQualityGate.reason,
1854
+ artifactPaths: answerQualityGate.evidenceRefs,
1855
+ recordedAt: answerQualityGate.recordedAt,
1856
+ metadata: answerQualityGate.metadata
1857
+ });
1858
+ var correctnessChecks = asArray(input.correctnessChecks);
1859
+ var hasPassedCorrectnessCheck = correctnessChecks.some(function (check) { return /^(pass|passed|success|ok)$/i.test(cleanText(check.status || (check.passed === true ? 'pass' : ''), 80)); });
986
1860
  var correctnessEvidence = {
987
- businessAssertions: asArray(input.correctnessChecks).map(function (check) { return ({
1861
+ businessAssertions: __spreadArray(__spreadArray([], __read(correctnessChecks.map(function (check) { return ({
988
1862
  assertion: check.assertion || check.name || check.case || 'assistant correctness check',
989
1863
  status: check.status || statusFromBoolean(check.passed),
990
1864
  expected: check.expected,
@@ -994,9 +1868,24 @@ function buildAssistantAIRunFromEvidence(input) {
994
1868
  message: check.message || check.reason,
995
1869
  metadata: check.metadata,
996
1870
  recordedAt: eventDate(check)
997
- }); })
1871
+ }); })), false), __read((answerQualityDecision.ready && !hasPassedCorrectnessCheck ? [{
1872
+ assertion: 'Assistant answer quality gate passed.',
1873
+ status: 'pass',
1874
+ expected: 'Correct dates, legal query shape, query result classification, cited evidence, confidence, and next action.',
1875
+ observed: answerQualityDecision.reason,
1876
+ dataProof: answerQualityDecision.evidenceRefs.concat(answerQualityDecision.citationRefs).join(', '),
1877
+ artifactPaths: answerQualityDecision.evidenceRefs,
1878
+ message: answerQualityDecision.reason,
1879
+ metadata: {
1880
+ answerQualityStatus: answerQualityDecision.status,
1881
+ queryStatus: answerQualityDecision.queryStatus,
1882
+ dateWindowRequired: answerQualityDecision.dateWindowRequired,
1883
+ dateWindowPresent: answerQualityDecision.dateWindowPresent,
1884
+ dateWindow: answerQualityDecision.dateWindow
1885
+ }
1886
+ }] : [])), false)
998
1887
  };
999
- var qa = buildQaFromEvidence(correctnessEvidence, input.now);
1888
+ var qa = applyAssistantAnswerQualityGate(buildQaFromEvidence(correctnessEvidence, input.now), answerQualityDecision, input.now);
1000
1889
  var failedReports = asArray(input.issueReports)
1001
1890
  .filter(function (report) { return /open|new|fail|bug|wrong|incorrect|error/i.test("".concat(report.status || '', " ").concat(report.category || '', " ").concat(report.summary || '')); });
1002
1891
  return (0, ai_run_evidence_1.buildAIRun)({
@@ -1019,7 +1908,27 @@ function buildAssistantAIRunFromEvidence(input) {
1019
1908
  now: input.now,
1020
1909
  metadata: {
1021
1910
  issueReportCount: asArray(input.issueReports).length,
1022
- correctnessCheckCount: asArray(input.correctnessChecks).length
1911
+ correctnessCheckCount: correctnessChecks.length,
1912
+ answerQuality: {
1913
+ ready: answerQualityDecision.ready,
1914
+ status: answerQualityDecision.status,
1915
+ queryStatus: answerQualityDecision.queryStatus,
1916
+ confidenceLevel: answerQualityDecision.confidenceLevel,
1917
+ dateBasis: answerQualityDecision.dateBasis,
1918
+ expectedCurrentDate: answerQualityDecision.expectedCurrentDate,
1919
+ dateWindowRequired: answerQualityDecision.dateWindowRequired,
1920
+ dateWindowPresent: answerQualityDecision.dateWindowPresent,
1921
+ dateWindow: answerQualityDecision.dateWindow,
1922
+ noDataConfirmed: answerQualityDecision.noDataConfirmed,
1923
+ legalQueryShape: answerQualityDecision.legalQueryShape,
1924
+ citationRefs: answerQualityDecision.citationRefs,
1925
+ queryEvidenceRequired: answerQualityDecision.queryEvidenceRequired,
1926
+ queryEvidencePresent: answerQualityDecision.queryEvidencePresent,
1927
+ queryEvidenceRefs: answerQualityDecision.queryEvidenceRefs,
1928
+ queryExecutionCount: answerQualityDecision.queryExecutionCount,
1929
+ nextActions: answerQualityDecision.nextActions,
1930
+ blockers: answerQualityDecision.blockers
1931
+ }
1023
1932
  }
1024
1933
  });
1025
1934
  }