|
1 | 1 | /** |
2 | | - * LensEnforcer v3.1 – CI-safe response guard. |
3 | | - * - If client requests SSE, ensure `text/event-stream` header. |
4 | | - * - If client requests JSON, enforce: |
5 | | - * { meta: { lens: ... }, sentences: [ { tags: [...] }, ... ] } |
6 | | - * Allowed tags: 'fact' | 'intuition' | 'metaphor' |
7 | | - * On invalid/missing, return a tiny safe stub so CI can proceed. |
| 2 | + * lensEnforcer (Permanent fix v3.1) |
| 3 | + * - Wraps res.send() and validates the output contract. |
| 4 | + * - If lens metadata is missing/failed, or content is “intuition-only” with no facts, |
| 5 | + * it quarantines the response (adds policy + meta.lens error) and logs a warning. |
8 | 6 | */ |
| 7 | +function isJsonLike(v) { |
| 8 | + if (v == null) return false; |
| 9 | + if (typeof v === 'object') return true; |
| 10 | + if (typeof v === 'string') { |
| 11 | + try { JSON.parse(v); return true; } catch { return false; } |
| 12 | + } |
| 13 | + return false; |
| 14 | +} |
9 | 15 |
|
10 | | -const ALLOWED = new Set(['fact', 'intuition', 'metaphor']); |
11 | | - |
12 | | -function hasLensMeta(payload) { |
13 | | - return payload && typeof payload === 'object' && payload.meta && payload.meta.lens; |
| 16 | +function coerceBody(body) { |
| 17 | + if (!isJsonLike(body)) return { raw: body }; |
| 18 | + return typeof body === 'string' ? JSON.parse(body) : body; |
14 | 19 | } |
15 | 20 |
|
16 | | -function sentencesAreValid(payload) { |
17 | | - const arr = Array.isArray(payload?.sentences) ? payload.sentences : []; |
18 | | - for (const s of arr) { |
19 | | - if (!Array.isArray(s?.tags)) return false; |
20 | | - for (const t of s.tags) if (!ALLOWED.has(t)) return false; |
| 21 | +function collectTags(out) { |
| 22 | + // sentences[].tags or top-level tags |
| 23 | + if (Array.isArray(out?.sentences)) { |
| 24 | + return out.sentences.flatMap(s => Array.isArray(s?.tags) ? s.tags : []); |
21 | 25 | } |
| 26 | + return Array.isArray(out?.tags) ? out.tags : []; |
| 27 | +} |
| 28 | + |
| 29 | +function hasFacts(tags) { |
| 30 | + return tags.some(t => (t?.type || t) === 'fact'); |
| 31 | +} |
| 32 | + |
| 33 | +function onlyIntuition(tags) { |
| 34 | + const tset = tags.map(t => t?.type || t); |
| 35 | + // "intuition" present, but no *non*-intuition tag |
| 36 | + return tset.includes('intuition') && tset.every(t => t === 'intuition'); |
| 37 | +} |
| 38 | + |
| 39 | +function ensureLensShape(lens) { |
| 40 | + if (!lens || typeof lens !== 'object') return false; |
| 41 | + if (!Array.isArray(lens.passed)) return false; |
22 | 42 | return true; |
23 | 43 | } |
24 | 44 |
|
25 | | -function safeStub() { |
26 | | - return { |
27 | | - meta: { lens: { status: 'missing/invalid' } }, |
28 | | - sentences: [], |
29 | | - }; |
| 45 | +function markQuarantine(out, reason) { |
| 46 | + out.prefix = '[WARN]'; |
| 47 | + out.policy = Object.assign({ route: 'AureaReview' }, out.policy); |
| 48 | + out.meta = out.meta || {}; |
| 49 | + out.meta.lens = out.meta.lens || {}; |
| 50 | + out.meta.lens.error = out.meta.lens.error || reason || 'lens-missing-or-invalid'; |
| 51 | + return out; |
30 | 52 | } |
31 | 53 |
|
32 | | -module.exports = function lensEnforcer() { |
| 54 | +function lensEnforcer() { |
33 | 55 | return function (req, res, next) { |
34 | | - const accept = String(req.headers?.accept || ''); |
35 | | - |
36 | | - // Canary: if client wants SSE, advertise the right type. |
37 | | - if (accept.includes('text/event-stream')) { |
38 | | - res.setHeader('Content-Type', 'text/event-stream'); |
39 | | - } |
40 | | - |
41 | | - // Wrap res.json to validate JSON responses when client asked for JSON. |
42 | | - const wantsJson = accept.includes('application/json'); |
43 | | - const originalJson = res.json.bind(res); |
44 | | - |
45 | | - res.json = (body) => { |
46 | | - if (wantsJson) { |
47 | | - if (!hasLensMeta(body) || !sentencesAreValid(body)) { |
48 | | - // return safe stub; using originalJson to avoid recursion. |
49 | | - return originalJson(safeStub()); |
50 | | - } |
| 56 | + const _send = res.send.bind(res); |
| 57 | + res.send = (body) => { |
| 58 | + let out = coerceBody(body); |
| 59 | + |
| 60 | + // If body isn't JSON‑ish, just forward |
| 61 | + if (!isJsonLike(body)) return _send(body); |
| 62 | + |
| 63 | + const lensOk = ensureLensShape(out?.meta?.lens) && !out?.meta?.lens?.error; |
| 64 | + const tags = collectTags(out); |
| 65 | + const intuitionOnly = onlyIntuition(tags); |
| 66 | + const missingFactsWithIntuition = !hasFacts(tags) && tags.length > 0 && tags.includes('intuition'); |
| 67 | + |
| 68 | + let quarantined = false; |
| 69 | + if (!lensOk) { |
| 70 | + out = markQuarantine(out, 'lens-missing-or-invalid'); |
| 71 | + quarantined = true; |
| 72 | + } else if (intuitionOnly || missingFactsWithIntuition) { |
| 73 | + out = markQuarantine(out, 'intuition-unlabeled-or-no-facts'); |
| 74 | + quarantined = true; |
51 | 75 | } |
52 | | - return originalJson(body); |
53 | | - }; |
54 | 76 |
|
| 77 | + if (quarantined) { |
| 78 | + // terse log line, safe for journald/CI |
| 79 | + try { |
| 80 | + const id = out?.id || req.id || '-'; |
| 81 | + const route = req?.originalUrl || req?.url || '-'; |
| 82 | + console.warn('[LensEnforcer] quarantine', JSON.stringify({ |
| 83 | + id, route, reason: out?.meta?.lens?.error || 'unknown' |
| 84 | + })); |
| 85 | + } catch {} |
| 86 | + } |
| 87 | + return _send(typeof body === 'string' ? JSON.stringify(out) : out); |
| 88 | + }; |
55 | 89 | next(); |
56 | 90 | }; |
57 | | -}; |
| 91 | +} |
| 92 | + |
| 93 | +module.exports = { lensEnforcer }; |
0 commit comments