@peopl-health/nexus 5.68.10 → 5.68.11

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.
@@ -1,3 +1,5 @@
1
+ const crypto = require('crypto');
2
+
1
3
  const { getEscalationPatternFloor } = require('../config/subAgentsConfig');
2
4
  const { requireGatewayProvider } = require('../config/llmConfig');
3
5
  const { readClinicalProfile } = require('../helpers/clinicalProfileHelper');
@@ -9,13 +11,17 @@ const { isPlainObject, safeParse } = require('../../utils/jsonUtils');
9
11
  const { logger } = require('../../utils/logger');
10
12
  const { redactDirectIdentifiers } = require('../../utils/sanitizerUtils');
11
13
  const { withTimeout } = require('../../utils/timeoutUtils');
14
+ const { composePrompt } = require('./promptComposerService');
12
15
 
13
16
  const FLOOR_LEVELS = ['asap', 'stat'];
14
17
  const PATTERN_NAME = /^[a-z0-9_]{1,64}$/;
15
18
  const UNLABELLED_PATTERN = 'sin_etiqueta';
16
19
  const MAX_TEXT_LENGTH = 500;
20
+ const MAX_LEVEL_LENGTH = 32;
21
+ const MAX_PATTERN_LENGTH = 64;
22
+ const PROVENANCE_GRACE_MS = 500;
17
23
 
18
- const degraded = (reason) => ({ floor: null, degraded: reason });
24
+ const degraded = (reason, check = null) => ({ floor: null, degraded: reason, check });
19
25
 
20
26
  function normalizePattern(raw) {
21
27
  const name = typeof raw === 'string' ? raw.trim().toLowerCase() : '';
@@ -24,6 +30,32 @@ function normalizePattern(raw) {
24
30
 
25
31
  const clippedText = (value) => cleanText(value)?.slice(0, MAX_TEXT_LENGTH) || null;
26
32
 
33
+ // Which prompt judged this escalation. Prompt text is edited in Airtable without bumping the preset
34
+ // version, so the version alone cannot tell two prompts apart; the hash of the composed prompt can.
35
+ async function promptProvenance(presetId, turnId) {
36
+ try {
37
+ // Composed alongside runStructured's own compose, with the same inputs and caches: it names the prompt
38
+ // the model ran with, though an Airtable edit landing between the two reads could make them differ.
39
+ const composed = await composePrompt({ presetId });
40
+ // A missing preset composes to an empty prompt; a hash of nothing would read as real provenance.
41
+ const promptHash = composed.resolvedPrompt
42
+ ? crypto.createHash('sha256').update(`${composed.resolvedPrompt}\n${JSON.stringify(composed.modelConfig || null)}`).digest('hex').slice(0, 12)
43
+ : null;
44
+ return { presetId, presetVersion: composed.presetVersion || null, promptHash };
45
+ } catch (error) {
46
+ logger.warn('[clinicalPatternFloor] The prompt provenance could not be read', { turnId, error: error.message });
47
+ return { presetId, presetVersion: null, promptHash: null };
48
+ }
49
+ }
50
+
51
+ // What the model answered, before validation, so a rejected or empty answer can be audited too.
52
+ const rawOutput = (parsed) => ({
53
+ minLevel: typeof parsed.min_level === 'string' ? parsed.min_level.slice(0, MAX_LEVEL_LENGTH) : null,
54
+ pattern: typeof parsed.pattern === 'string' ? parsed.pattern.slice(0, MAX_PATTERN_LENGTH) : null,
55
+ evidence: clippedText(parsed.evidence),
56
+ reason: clippedText(parsed.reason),
57
+ });
58
+
27
59
  function buildPatternInput({ profile, details, justification, today }) {
28
60
  const indicators = Array.isArray(details.indicators) ? details.indicators.filter((item) => typeof item === 'string' && item.trim()) : [];
29
61
  const exposure = buildExposureBlock(profile);
@@ -55,32 +87,42 @@ async function evaluatePatternFloor({ code, turnId, details, justification, prov
55
87
  logger.warn('[clinicalPatternFloor] The clinical profile is missing or unreadable; the check is skipped', { turnId, profile: reason });
56
88
  return degraded(reason);
57
89
  }
90
+ // Read alongside the model call so the audit never spends the floor's deadline.
91
+ const pendingProvenance = promptProvenance(presetId, turnId);
58
92
  const result = await (provider || requireGatewayProvider()).runStructured({
59
93
  presetId,
60
94
  input: [{ role: 'user', content: buildPatternInput({ profile, details, justification, today: now.toISOString().slice(0, 10) }) }],
61
95
  metadata: { subAgent: 'escalation-pattern-floor', turnId },
62
96
  });
97
+ // Once the model has answered, provenance gets a short grace and is otherwise dropped: a slow
98
+ // Airtable read must never turn an answer the floor already has into a timeout.
99
+ const provenance = await withTimeout(pendingProvenance, PROVENANCE_GRACE_MS, 'pattern floor provenance').catch((error) => {
100
+ logger.warn('[clinicalPatternFloor] The prompt provenance was not ready; the answer is kept without it', { turnId, error: error.message });
101
+ return { presetId, presetVersion: null, promptHash: null };
102
+ });
63
103
  if (result?.status && result.status !== 'completed') {
64
104
  logger.warn('[clinicalPatternFloor] The pattern check did not complete', { turnId, status: result.status });
65
- return degraded('incomplete');
105
+ return degraded('incomplete', { ...provenance, output: null });
66
106
  }
67
107
  const parsed = safeParse(readStructuredText(result), null);
68
108
  if (!isPlainObject(parsed)) {
69
109
  logger.warn('[clinicalPatternFloor] The pattern check did not return a JSON object', { turnId });
70
- return degraded('malformed_output');
110
+ return degraded('malformed_output', { ...provenance, output: null });
71
111
  }
112
+ const check = { ...provenance, output: rawOutput(parsed) };
72
113
  if (parsed.min_level == null) {
73
- if (parsed.pattern == null) return { floor: null, degraded: null };
114
+ if (parsed.pattern == null) return { floor: null, degraded: null, check };
74
115
  logger.warn('[clinicalPatternFloor] The pattern check named a pattern without a level', { turnId, pattern: parsed.pattern });
75
- return degraded('missing_level');
116
+ return degraded('missing_level', check);
76
117
  }
77
118
  if (!FLOOR_LEVELS.includes(parsed.min_level)) {
78
119
  logger.warn('[clinicalPatternFloor] The pattern check returned an unsupported level', { turnId, minLevel: parsed.min_level, pattern: parsed.pattern });
79
- return degraded('unsupported_level');
120
+ return degraded('unsupported_level', check);
80
121
  }
81
122
  return {
82
123
  floor: { level: parsed.min_level, pattern: normalizePattern(parsed.pattern), evidence: clippedText(parsed.evidence), reason: clippedText(parsed.reason) },
83
124
  degraded: null,
125
+ check,
84
126
  };
85
127
  }
86
128
 
@@ -349,6 +349,7 @@ async function handler(args, context = {}) {
349
349
  ...(patternFloor ? { clinicalPatternFloor: { ...patternFloor, raised: raisedByPattern } } : {}),
350
350
  ...(gateCheck?.degraded ? { triageGateFloorDegraded: gateCheck.degraded } : {}),
351
351
  ...(patternCheck?.degraded ? { clinicalPatternFloorDegraded: patternCheck.degraded } : {}),
352
+ ...(patternCheck?.check ? { clinicalPatternFloorCheck: patternCheck.check } : {}),
352
353
  },
353
354
  });
354
355
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.68.10",
3
+ "version": "5.68.11",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",