'use strict';

// Provider CLIs sometimes persist operational context as `role=user` so it is
// available to the model. These rows are not human-authored prompts and should
// not be projected into CTM timelines, prompt history, or message search.
const PROVIDER_GENERATED_USER_CONTEXT_PATTERNS = [
  /^<environment_context>/i,
  /^<permissions instructions>/i,
  /^<collaboration_mode>/i,
  /^<skills_instructions>/i,
  /^<plugins_instructions>/i,
  /^<task-notification>/i,
  /^<skill>\s*<name>/i,
  /^# AGENTS\.md instructions for\b/i,
  /^Base directory for this skill:\s+/i,
  /^This session is being continued from a previous conversation\b/i,
  /^Continue from where you left off\b/i,
  /^Compacted\b/i,
  /^\[Image:\s*source:\s*(?:"[^"]+"|'[^']+'|[^\]]+)\]$/i,
  /^\[Image:\s*original\s+\d+(?:\.\d+)?x\d+(?:\.\d+)?,\s*displayed at\s+\d+(?:\.\d+)?x\d+(?:\.\d+)?\.\s*Multiply coordinates by\s+[0-9.]+\s+to map to original image\.?\]$/i,
  /^\[Request interrupted by user(?: for tool use)?\]$/i,
];

function isProviderGeneratedUserContextText(text) {
  const cleaned = String(text || '').trim();
  if (!cleaned) return false;
  if (/^<turn_aborted>\s*[\s\S]*?<\/turn_aborted>\s*$/i.test(cleaned)) return true;
  return PROVIDER_GENERATED_USER_CONTEXT_PATTERNS.some((pattern) => pattern.test(cleaned));
}

module.exports = {
  PROVIDER_GENERATED_USER_CONTEXT_PATTERNS,
  isProviderGeneratedUserContextText,
};
