@peopl-health/nexus 5.75.1 → 5.75.2

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.
@@ -35,6 +35,14 @@ const triageRecommendationEventSchema = new mongoose.Schema({
35
35
  gateLatencyMs: { type: Number, default: null },
36
36
  planLatencyMs: { type: Number, default: null },
37
37
  planMaxOutputTokens: { type: Number, default: null },
38
+ planRetryReasons: {
39
+ type: [new mongoose.Schema({
40
+ attempt: { type: Number, required: true },
41
+ reason: { type: String, required: true },
42
+ paths: { type: [String], default: undefined },
43
+ }, { _id: false })],
44
+ default: undefined,
45
+ },
38
46
  totalLatencyMs: { type: Number, default: null },
39
47
  triageStored: { type: Boolean, default: null },
40
48
  unmappedAnswerCount: { type: Number, default: null },
@@ -610,6 +610,7 @@ async function generateTriageRecommendations({ payload, provider, runId = null,
610
610
  settle.planLatencyMs = Date.now() - planStart;
611
611
  settle.attempts = planOutcome.attempts;
612
612
  settle.planMaxOutputTokens = planOutcome.planMaxOutputTokens;
613
+ if (planOutcome.retryReasons.length) settle.planRetryReasons = planOutcome.retryReasons;
613
614
  if (planOutcome.safetyUncertain) settle.supportSafetyCleared = false;
614
615
  if (planOutcome.failure) return finish({ ...planOutcome.failure, autoRepairs: [...gateRepairs, ...(planOutcome.repairs || [])] });
615
616
  const { plan, planRepairs, qualityWarnings } = planOutcome;
@@ -863,12 +864,21 @@ async function runGate({ provider, presetId, casoBase, routeFloor, rid, code, pa
863
864
  return { attempts, gate, gateRepairs, gateWarnings };
864
865
  }
865
866
 
867
+ // Validator issue paths only (never the problem text), indices collapsed so retries aggregate.
868
+ function issuePaths(issues) {
869
+ return [...new Set(issues.map((issue) => String(issue.path || '').replace(/\[[^\]]*\]/g, '[]')).filter(Boolean))];
870
+ }
871
+
866
872
  async function runPlan({ provider, presetId, casoBase, gate, boundary = null, rankContext = {}, rid, code, payload }) {
867
873
  const planInput = `${casoBase}\n\n### Gate de seguridad comprometido (PASO 1)\n${JSON.stringify(buildLegacyStep1(gate))}`;
868
874
  let lastIssues = [];
869
875
  let safetyUncertain = false;
870
876
  let maxOutputTokens = null;
871
- const outcome = (result) => ({ ...result, safetyUncertain, planMaxOutputTokens: maxOutputTokens });
877
+ const retryReasons = [];
878
+ const retried = (attempt, reason, issues = null) => {
879
+ retryReasons.push(issues ? { attempt, reason, paths: issuePaths(issues) } : { attempt, reason });
880
+ };
881
+ const outcome = (result) => ({ ...result, safetyUncertain, planMaxOutputTokens: maxOutputTokens, retryReasons });
872
882
  const buildPlan = (takeaways, elements) => new RecommendationPlan({
873
883
  runId: rid,
874
884
  patientId: code,
@@ -906,13 +916,16 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
906
916
  } catch {
907
917
  lastIssues = [];
908
918
  if (last) return outcome({ attempts: attempt, failure: { status: 'failed', stage: 'plan', reason: 'plan_provider_failed' } });
919
+ retried(attempt, 'plan_provider_failed');
909
920
  continue;
910
921
  }
911
922
  if (result?.status && result.status !== 'completed') {
912
923
  lastIssues = [{ path: 'plan', problem: `respuesta incompleta del proveedor (status=${result.status})` }];
913
924
  firstEvidence = firstEvidence ?? rawEvidence(result);
914
925
  if (last) return outcome({ attempts: attempt, failure: { status: 'failed', stage: 'plan', reason: 'plan_incomplete', hardRejectIssues: lastIssues, rawExcerpt: firstEvidence } });
915
- if (hitTokenLimit(result)) maxOutputTokens = truncationBudget(result, maxOutputTokens);
926
+ const isTruncated = hitTokenLimit(result);
927
+ if (isTruncated) maxOutputTokens = truncationBudget(result, maxOutputTokens);
928
+ retried(attempt, isTruncated ? 'plan_truncated' : 'plan_incomplete');
916
929
  continue;
917
930
  }
918
931
  const parsed = safeParse(readStructuredText(result), null);
@@ -920,6 +933,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
920
933
  if (!isPlainObject(parsed)) {
921
934
  lastIssues = [{ path: 'plan', problem: 'la respuesta no es un objeto JSON válido' }];
922
935
  if (last) return outcome({ attempts: attempt, failure: { status: 'failed', stage: 'plan', reason: 'plan_malformed', rawExcerpt: firstEvidence } });
936
+ retried(attempt, 'plan_malformed');
923
937
  continue;
924
938
  }
925
939
  const rawPlan = isPlainObject(parsed.input) ? parsed.input : parsed;
@@ -928,7 +942,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
928
942
  const rawElements = arr(arrayArg(rawPlan.elements));
929
943
  if (!takeaways || !rawElements.length) {
930
944
  lastIssues = [{ path: 'elements', problem: 'takeaways y elements[] no vacío son obligatorios' }];
931
- if (attempt < PLAN_MAX_ATTEMPTS) continue;
945
+ if (attempt < PLAN_MAX_ATTEMPTS) { retried(attempt, 'plan_empty'); continue; }
932
946
  return outcome({ attempts: attempt, failure: { status: 'failed', stage: 'plan', reason: 'plan_empty', rawExcerpt: firstEvidence } });
933
947
  }
934
948
 
@@ -940,7 +954,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
940
954
  deniedTargetsStripped = stripped;
941
955
  if (!elements.length) {
942
956
  lastIssues = [{ path: 'elements', problem: 'ningún elemento nombra un síntoma reportado en este triaje; construye el plan sobre los síntomas que el paciente sí reportó hoy' }];
943
- if (attempt < PLAN_MAX_ATTEMPTS) continue;
957
+ if (attempt < PLAN_MAX_ATTEMPTS) { retried(attempt, 'plan_all_denied'); continue; }
944
958
  return outcome({ attempts: attempt, repairs, failure: { status: 'failed', stage: 'validate', reason: 'plan_all_denied', hardRejectIssues: lastIssues } });
945
959
  }
946
960
  rankReordered = applyRankOrder(elements, gate, repairs, rankContext);
@@ -948,7 +962,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
948
962
  const blocking = blockingIssues(issues);
949
963
  if (blocking.length) {
950
964
  lastIssues = blocking;
951
- if (attempt < PLAN_MAX_ATTEMPTS) continue;
965
+ if (attempt < PLAN_MAX_ATTEMPTS) { retried(attempt, 'plan_invalid', blocking); continue; }
952
966
  return outcome({ attempts: attempt, repairs, failure: { status: 'failed', stage: 'validate', reason: 'plan_invalid', hardRejectIssues: blocking } });
953
967
  }
954
968
 
@@ -957,7 +971,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
957
971
  plan = buildPlan(takeaways, elements);
958
972
  } catch (err) {
959
973
  lastIssues = [{ path: 'plan', problem: err?.message || 'DTO inválido' }];
960
- if (attempt < PLAN_MAX_ATTEMPTS) continue;
974
+ if (attempt < PLAN_MAX_ATTEMPTS) { retried(attempt, 'plan_dto_invalid'); continue; }
961
975
  return outcome({ attempts: attempt, repairs, failure: { status: 'failed', stage: 'validate', reason: 'plan_invalid', hardRejectIssues: lastIssues } });
962
976
  }
963
977
  return outcome({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.75.1",
3
+ "version": "5.75.2",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",