Skip to content

Commit f5cb4db

Browse files
committed
Add Permanent Fix v3.1 lensEnforcer middleware
1 parent 1f1fb69 commit f5cb4db

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// middleware: lensEnforcer.ts
2+
export function lensEnforcer(req, res, next) {
3+
res.on('finish', () => {}); // no-op
4+
res._send = res.send;
5+
res.send = (body) => {
6+
const out = typeof body === 'string' ? JSON.parse(body) : body;
7+
const lens = out?.meta?.lens;
8+
const tags = out?.sentences?.flatMap(s => s.tags || []) || out?.tags || [];
9+
const hasFacts = tags.some(t => t.type === 'fact');
10+
const unlabeledIntuition = out?.text && hasFacts && !tags.some(t => t.type !== 'fact');
11+
12+
if (!lens || !Array.isArray(lens.passed) || lens.error || unlabeledIntuition) {
13+
out.prefix = '[WARN]';
14+
out.meta = out.meta || {};
15+
out.meta.lens = lens || { error: 'lens-missing-or-unlabeled' };
16+
out.policy = { route: 'AureaReview' };
17+
return res._send(JSON.stringify(out));
18+
}
19+
return res._send(JSON.stringify(out));
20+
};
21+
next();
22+
}

0 commit comments

Comments
 (0)