marmar-context / web / context / server / source-policy.ts
source-policy.ts
Raw
// Source content can describe a patient, but cannot direct extraction or approval.
// This conservative screen is also applied to structured fields and returned refs.
export const INSTRUCTION_REASON =
  "Instruction or template text was withheld. It cannot establish a patient fact or clinical approval.";

export function instructionLike(text: string): boolean {
  return /\b(?:ignore|disregard|override|forget)\b.{0,100}\b(?:instructions?|allerg\w*|previous|above|rules?|system)\b|\b(?:system|developer|assistant)\s*:|<\/?(?:system|instruction|prompt)>|system prompt|api.?key|\bpretend\b|template instruction|not patient data|\b(?:return|output|emit|generate|respond|print|set|replace)\b.{0,100}(?:\b(?:allerg\w*|patient|medication|lab|condition|facts|json|clinical approval)\b)|\breport\b.{0,60}\b(?:clinical approval|sign.?off)\b/i.test(
    text,
  );
}

export function policyLines(text: string) {
  let instructionBlock = false;
  return text.split("\n").map((raw, index) => {
    const text = raw.trim();
    if (!text) instructionBlock = false;
    if (instructionLike(text)) instructionBlock = true;
    return { text, line: index + 1, blocked: instructionBlock && !!text };
  });
}