scheduler_prompt.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. """Cron job prompt assembly: context_from injection, skill loading, the assembled-prompt
  2. injection scan, and the credential-exfil / config-block guards.
  3. Split out of ``cron.scheduler``. Import names from this module directly (``cron.scheduler`` only
  4. imports the few it calls itself). Origin-resident helpers and sibling split modules are reached
  5. late-bound (``_sched`` / module refs at the bottom) so monkeypatching the defining module works.
  6. """
  7. from __future__ import annotations
  8. import json
  9. import logging
  10. import re
  11. from hermes_time import now as _hermes_now
  12. from typing import Optional
  13. # Log-record parity with the origin module.
  14. logger = logging.getLogger("cron.scheduler")
  15. def _parse_wake_gate(script_output: str) -> bool:
  16. """Wake gate: False only if the last non-empty stdout line is JSON ``{"wakeAgent": false}``
  17. (agent skipped entirely — no LLM run, no delivery); anything else wakes normally.
  18. Any other output (non-JSON, missing flag, gate absent, or ``wakeAgent: true``) means wake the agent
  19. normally. See #1232.
  20. """
  21. stripped_lines = [line for line in (script_output or "").splitlines() if line.strip()]
  22. if not stripped_lines:
  23. return True
  24. try:
  25. gate = json.loads(stripped_lines[-1].strip())
  26. except (json.JSONDecodeError, ValueError):
  27. return True
  28. return not isinstance(gate, dict) or gate.get("wakeAgent", True) is not False
  29. def _prepend_context_block(prompt: str, heading: str, intro: str, body: str) -> str:
  30. """Prefix ``prompt`` with a fenced ``## heading`` data block."""
  31. return f"## {heading}\n{intro}\n\n```\n{body}\n```\n\n{prompt}"
  32. def _job_skill_names(job: dict) -> list[str]:
  33. """Normalized skill names from ``skills`` (list or str) or the legacy singular ``skill``."""
  34. skills = job.get("skills")
  35. if skills is None:
  36. legacy = job.get("skill")
  37. skills = [legacy] if legacy else []
  38. elif isinstance(skills, str):
  39. skills = [skills]
  40. return [str(name).strip() for name in skills if str(name).strip()]
  41. _MAX_CONTEXT_CHARS = 8000
  42. _SELF_CONTEXT_INTRO = (
  43. "The following is this job's most recent non-silent output from a previous run. Use it "
  44. "for continuity: avoid repeating what was already reported, and continue where the last "
  45. "run left off."
  46. )
  47. _UPSTREAM_CONTEXT_INTRO = (
  48. "The following is the most recent output from a preceding cron job. Use it as context for "
  49. "your analysis."
  50. )
  51. # Run-document length frames. The writer (``cron.scheduler.run_job``) stamps
  52. # these labels; the reader below parses them. Defined here (not in scheduler.py)
  53. # because this module binds ``_sched`` only at import tail, so module-level
  54. # patterns cannot be built from scheduler attributes without an import cycle.
  55. _PROMPT_FRAME = "**Prompt Characters:** "
  56. _RESPONSE_FRAME = "**Response Characters:** "
  57. _PROMPT_HEADING = "## Prompt\n\n"
  58. _RESPONSE_HEADING = "## Response\n\n"
  59. _PROMPT_SEPARATOR = "\n\n" # writer's blank line after the prompt body
  60. _RESPONSE_TERMINATOR = "\n" # writer's trailing newline after the response body
  61. _PROMPT_FRAME_RE = re.compile(
  62. rf"(?m)^{re.escape(_PROMPT_FRAME)}(\d+)\n{re.escape(_PROMPT_HEADING)}")
  63. _RESPONSE_FRAME_RE = re.compile(
  64. rf"(?m)^{re.escape(_RESPONSE_FRAME)}(\d+)\n{re.escape(_RESPONSE_HEADING)}")
  65. def _job_doc_header(job_name: str, job_id: str, now_iso: str, mode: str) -> str:
  66. """Common markdown header for the short-circuit run docs (no_agent / monitor)."""
  67. return (
  68. f"# Cron Job: {job_name}\n\n"
  69. f"**Job ID:** {job_id}\n"
  70. f"**Run Time:** {now_iso}\n"
  71. f"**Mode:** {mode}\n"
  72. )
  73. def _archive_answer(archive: str) -> str | None:
  74. """The reusable answer of a stored run, using its length frame when available.
  75. Framed runs (``_PROMPT_FRAME``/``_RESPONSE_FRAME`` stamps) validate the
  76. response length before whitespace normalization, so quoted frames inside the
  77. prompt or answer can never become boundaries and a truncated write is
  78. rejected. Archives without the heading (script-mode runs) stay whole-document.
  79. For legacy unframed archives the LAST ``## Response`` occurrence is the
  80. writer's boundary — the assembled prompt half can itself carry the literal
  81. heading (a skill documenting its response format, an injected previous answer
  82. quoting it), so an early split would re-inject the prompt noise this
  83. extraction exists to drop.
  84. ``None`` marks "no usable answer" — a blank or silent response (any form the
  85. delivery lane itself suppresses) — so the caller falls through to an older
  86. archive instead of injecting prompt noise the job already has.
  87. """
  88. # New writers stamp the prompt length outside user-owned text. Jump past
  89. # that prompt instead of searching its quoted markers for a response frame.
  90. prompt_frame = _PROMPT_FRAME_RE.search(archive)
  91. if (prompt_frame is not None
  92. and archive.find(_PROMPT_HEADING) == prompt_frame.end() - len(_PROMPT_HEADING)):
  93. response_start = prompt_frame.end() + int(prompt_frame.group(1)) + len(_PROMPT_SEPARATOR)
  94. frame = _RESPONSE_FRAME_RE.match(archive, response_start)
  95. tail = archive[frame.end():] if frame is not None else ""
  96. # A missing or truncated writer-owned boundary is unusable.
  97. if (frame is None or len(tail) != int(frame.group(1)) + len(_RESPONSE_TERMINATOR)
  98. or not tail.endswith(_RESPONSE_TERMINATOR)):
  99. return None
  100. answer = tail[:-len(_RESPONSE_TERMINATOR)].strip()
  101. elif "## Response" not in archive:
  102. return archive.strip()
  103. else:
  104. answer = archive.rpartition("## Response")[2].strip()
  105. if not answer or _sched._is_cron_silence_response(answer):
  106. return None
  107. return answer
  108. def _clip_to_context_budget(text: str) -> str:
  109. """Clip oversized context head+tail; conclusions and summaries sit at the end."""
  110. if len(text) <= _MAX_CONTEXT_CHARS:
  111. return text
  112. keep = _MAX_CONTEXT_CHARS // 2
  113. omitted = len(text) - 2 * keep
  114. return f"{text[:keep]}\n\n[... {omitted} chars omitted ...]\n\n{text[-keep:]}"
  115. def _inject_context_from(job: dict, prompt: str) -> tuple[str, bool]:
  116. """Prepend the latest output of each ``context_from`` job; returns ``(prompt, injected)``."""
  117. context_from = job.get("context_from")
  118. if not context_from:
  119. return prompt, False
  120. from cron.jobs import get_cron_output_dir
  121. output_dir = get_cron_output_dir()
  122. if isinstance(context_from, str):
  123. context_from = [context_from]
  124. injected = False
  125. for source_job_id in context_from:
  126. # "self" = the job's own id: continuity across runs without touching session history.
  127. if isinstance(source_job_id, str) and source_job_id.strip().lower() == "self":
  128. source_job_id = str(job.get("id") or "")
  129. is_self = source_job_id == job.get("id")
  130. # Traversal guard — valid job IDs are hex strings.
  131. if not source_job_id or not all(c in "0123456789abcdef" for c in source_job_id):
  132. logger.warning(
  133. "context_from: skipping invalid job_id %r for job_id=%r name=%r%s",
  134. source_job_id, job.get("id"), job.get("name"),
  135. _delivery._cron_job_origin_log_suffix(job),
  136. )
  137. continue
  138. try:
  139. output_files = sorted(
  140. (output_dir / source_job_id).glob("*.md"), key=lambda f: f.stat().st_mtime,
  141. reverse=True,
  142. )
  143. latest_output = ""
  144. for output_file in output_files:
  145. candidate = output_file.read_text(encoding="utf-8-sig")
  146. # Only the run header describes suppression; script/agent payloads can
  147. # quote these markers. Keep error documents useful for recovery context.
  148. header = candidate.split("\n---\n", 1)[0].split("\n## Prompt", 1)[0]
  149. silent_audit = candidate.startswith("# Cron Job:") and any(
  150. line.startswith(("**Status:** no_change", "**Status:** silent",
  151. "Script gate returned `wakeAgent=false`"))
  152. for line in header.splitlines()
  153. )
  154. if not candidate.strip() or silent_audit:
  155. continue
  156. answer = _archive_answer(candidate)
  157. if answer is None:
  158. continue # [SILENT]/blank response — try an older archive
  159. latest_output = answer
  160. break
  161. if not latest_output:
  162. continue # silent skip — no archive with a usable answer
  163. latest_output = _clip_to_context_budget(latest_output)
  164. if is_self:
  165. prompt = _prepend_context_block(
  166. prompt, "Your previous run's output", _SELF_CONTEXT_INTRO, latest_output)
  167. else:
  168. prompt = _prepend_context_block(
  169. prompt, f"Output from job '{source_job_id}'", _UPSTREAM_CONTEXT_INTRO,
  170. latest_output,
  171. )
  172. injected = True
  173. except (OSError, PermissionError) as e:
  174. # silent skip — never put error text into the prompt
  175. logger.warning("context_from: failed to read output for job %r: %s", source_job_id, e)
  176. return prompt, injected
  177. def _load_cron_skill_parts(job: dict, skill_names: list[str]) -> list[str]:
  178. """Load each named skill/bundle into prompt parts; unknown ones are skipped with a notice."""
  179. from tools.skills_tool import skill_view
  180. from tools.skill_usage import bump_use
  181. from agent.skill_bundles import build_bundle_invocation_message, resolve_bundle_command_key
  182. from agent.skill_commands import _inject_skill_config, ambiguous_skill_label
  183. from agent.skill_utils import normalize_skill_lookup_name
  184. job_label = job.get("name", job.get("id"))
  185. task_id = str(job.get("id") or "") or None
  186. parts: list[str] = []
  187. skipped: list[str] = []
  188. def _skip(msg: str, *args, label: str | None = None) -> None:
  189. logger.warning("Cron job '%s': " + msg, job_label, *args)
  190. skipped.append(label or skill_name)
  191. for skill_name in skill_names:
  192. # Bundles shadow same-slug skills, mirroring the CLI/gateway slash-command path.
  193. bundle_key = resolve_bundle_command_key(skill_name.lstrip("/"))
  194. if bundle_key:
  195. bundle_payload = build_bundle_invocation_message(
  196. bundle_key, user_instruction="", task_id=task_id)
  197. if bundle_payload:
  198. if parts:
  199. parts.append("")
  200. parts.append(bundle_payload[0])
  201. else:
  202. _skip("bundle '%s' could not load any skills, skipping", skill_name)
  203. continue
  204. try:
  205. loaded = json.loads(skill_view(normalize_skill_lookup_name(skill_name)))
  206. except (json.JSONDecodeError, TypeError):
  207. _skip("skill '%s' returned invalid JSON, skipping", skill_name)
  208. continue
  209. if ambiguous := ambiguous_skill_label(skill_name, loaded):
  210. _skip("%s — skipping", ambiguous, label=ambiguous)
  211. continue
  212. if not loaded.get("success"):
  213. _skip(
  214. "skill not found, skipping — %s",
  215. loaded.get("error") or f"Failed to load skill '{skill_name}'")
  216. continue
  217. try:
  218. bump_use(skill_name, task_id=task_id)
  219. except Exception:
  220. logger.debug("Cron job: failed to bump skill usage for '%s'", skill_name, exc_info=True)
  221. if parts:
  222. parts.append("")
  223. parts.extend([
  224. f'[IMPORTANT: The user has invoked the "{skill_name}" skill, indicating they want you to follow its instructions. The full skill content is loaded below.]',
  225. "",
  226. str(loaded.get("content") or "").strip()])
  227. _inject_skill_config(loaded, parts)
  228. if skipped:
  229. parts.insert(0, (
  230. f"[IMPORTANT: The following skill(s) were listed for this job but could not be found "
  231. f"and were skipped: {', '.join(skipped)}. "
  232. f"Start your response with a brief notice so the user is aware, e.g.: "
  233. f"'⚠️ Skill(s) not found and skipped: {', '.join(skipped)}']"
  234. ))
  235. return parts
  236. _CRON_HINT = (
  237. "[IMPORTANT: You are running as a scheduled cron job. "
  238. "DELIVERY: Your final response will be automatically delivered "
  239. "to the user — do NOT use send_message or try to deliver "
  240. "the output yourself. Just produce your report/output as your "
  241. "final response and the system handles the rest. "
  242. "SILENT: If there is genuinely nothing new to report, respond "
  243. "with exactly \"[SILENT]\" (nothing else) to suppress delivery. "
  244. "[SILENT] is a literal ASCII control token — never translate or "
  245. "rephrase it, whatever language the rest of your answer uses. "
  246. "Never combine [SILENT] with content — either report your "
  247. "findings normally, or say [SILENT] and nothing more. "
  248. "FAILURE: If a delegated child fails and this cron run must be "
  249. "recorded as failed, put [CRON_FAILURE] on the first line by itself, "
  250. "then explain the child failure on following lines. "
  251. "RECURSION: This is a run of an EXISTING scheduled job — execute "
  252. "the task now. NEVER create or update a cron job because of "
  253. "recurring or future-schedule language in the task prompt below; "
  254. "treat phrasing like \"each Monday\" or \"every day at 9\" as "
  255. "context for this run, not as a request to schedule another job.]\n\n"
  256. )
  257. def _run_time_line() -> str:
  258. """This fire's wall-clock time in the configured zone. The system prompt is date-only (byte-stable
  259. for caching), so without it a 07:00 brief can't tell itself from a 19:00 retry. Kept outside
  260. ``_CRON_HINT`` and after it: the hint stays byte-stable, and cron sessions are fresh per fire."""
  261. from hermes_time import safe_strftime
  262. now = _hermes_now()
  263. offset = now.strftime("%z")
  264. zone = f" (UTC{offset[:3]}:{offset[3:]})" if offset else ""
  265. return f"[CURRENT RUN TIME: {safe_strftime(now, '%A, %Y-%m-%d %H:%M %Z').strip()}{zone}]\n\n"
  266. def _build_job_prompt(
  267. job: dict, prerun_script: Optional[tuple] = None, extra_prompt: Optional[str] = None,
  268. runtime_data_prompt: Optional[str] = None,
  269. ) -> str:
  270. """Build the effective prompt for a cron job, optionally loading skills first.
  271. ``prerun_script``: cached ``(success, stdout)`` from a script the caller already ran (wake-gate
  272. check) — skips re-execution. ``extra_prompt``: user-authored per-run ``## Run Context`` for this
  273. fire only, never persisted to the job. ``runtime_data_prompt`` is operator-configured runtime
  274. data (such as monitor output) and is scanned as injected data rather than user input.
  275. When provided, the script is not re-executed and the cached result is used for prompt injection. When
  276. omitted, the script (if any) runs inline as before. extra_prompt: Optional per-run context (from
  277. ``cronjob(action='run')``, 57331 — salvaged from #57342 by @liuhao1024).
  278. """
  279. user_prompt = str(job.get("prompt") or "")
  280. if extra_prompt:
  281. user_prompt = f"{user_prompt}\n\n## Run Context\n{extra_prompt}"
  282. prompt = user_prompt
  283. # Runtime DATA (script stdout, upstream output) legitimately quotes command-shape strings, so it
  284. # must not be scanned with the strict user-prompt set — see _scan_assembled_cron_prompt.
  285. has_injected_data = False
  286. if runtime_data_prompt:
  287. prompt = f"{prompt}\n\n## Run Context\n{runtime_data_prompt}"
  288. has_injected_data = True
  289. script_path = job.get("script")
  290. if script_path:
  291. success, script_output = (
  292. prerun_script if prerun_script is not None
  293. else _script._run_job_script(
  294. script_path, workdir=_sched._resolve_job_workdir(job, str(job.get("id") or "")),
  295. interpreter=job.get("interpreter")))
  296. if success and not script_output:
  297. return None # no output → nothing to report, skip the AI call
  298. heading, intro = (
  299. ("Script Output", "The following data was collected by a pre-run script. "
  300. "Use it as context for your analysis.")
  301. if success
  302. else ("Script Error", "The data-collection script failed. Report this to the user.")
  303. )
  304. prompt = _prepend_context_block(prompt, heading, intro, script_output)
  305. has_injected_data = True
  306. prompt, _ctx_injected = _inject_context_from(job, prompt)
  307. has_injected_data = has_injected_data or _ctx_injected
  308. # Durable per-job notepad; empty renders as "" so unused → byte-identical prompt.
  309. from cron import notepad as cron_notepad
  310. notepad_section = cron_notepad.render_notepad_section(str(job.get("id") or ""))
  311. if notepad_section:
  312. prompt = f"{notepad_section}{prompt}"
  313. has_injected_data = True
  314. prompt = _CRON_HINT + _run_time_line() + prompt
  315. skill_names = _job_skill_names(job)
  316. if not skill_names:
  317. return _scan_assembled_cron_prompt(
  318. prompt, job, has_skills=False, has_injected_data=has_injected_data,
  319. user_prompt=user_prompt,
  320. )
  321. parts = _load_cron_skill_parts(job, skill_names)
  322. stable_prefix = None
  323. if prompt:
  324. from agent.skill_commands import append_user_instruction
  325. parts.append("")
  326. # Skill blocks are stable per job config; the appended instruction is volatile per-run.
  327. # Declare that boundary for the Anthropic cache planner.
  328. # The skill blocks (and any skipped-skill notice) above are stable per job config; the appended
  329. # instruction carries the volatile per-run data (cron hint + prompt + script output + run context).
  330. # See #81867.
  331. stable_prefix = append_user_instruction(parts, prompt)
  332. assembled = _scan_assembled_cron_prompt("\n".join(parts), job, has_skills=True)
  333. if (
  334. stable_prefix
  335. and len(assembled) > len(stable_prefix)
  336. and assembled.startswith(stable_prefix)
  337. ):
  338. # Guarded: the scanner may mutate the bytes; mismatch → whole-message caching.
  339. from agent.prompt_cache_boundary import register_stable_prefix
  340. register_stable_prefix(stable_prefix)
  341. return assembled
  342. def _scan_assembled_cron_prompt(
  343. assembled: str, job: dict, *, has_skills: bool = False, has_injected_data: bool = False,
  344. user_prompt: Optional[str] = None,
  345. ) -> str:
  346. """Scan the assembled cron prompt for injection; raise ``CronPromptInjectionBlocked`` on a hit.
  347. Needed because skill content is loaded from disk at runtime (never scanned at create/update)
  348. and cron auto-approves tool calls. Tier by what the prompt CONTAINS: user prompt + hint only →
  349. STRICT ``_scan_cron_prompt``; skills or injected data → LOOSER ``_scan_cron_skill_assembled``
  350. (command-shape patterns dropped, invisible unicode sanitized not blocked, so a false positive
  351. cannot permanently kill a job); injected data without skills also scans ``user_prompt`` STRICT.
  352. Since cron runs non-interactively (auto-approves tool calls), a malicious skill carrying an injection
  353. payload bypassed every gate. See #3968.
  354. """
  355. from tools.cronjob_tools import _scan_cron_prompt
  356. from tools.cronjob_prompt_scan import _scan_cron_skill_assembled
  357. if has_skills or has_injected_data:
  358. # The cleaned (sanitized) prompt is what actually runs.
  359. assembled, scan_error = _scan_cron_skill_assembled(assembled)
  360. if not scan_error and not has_skills and user_prompt:
  361. scan_error = _scan_cron_prompt(user_prompt)
  362. else:
  363. scan_error = _scan_cron_prompt(assembled)
  364. if scan_error:
  365. logger.warning(
  366. "Cron job '%s': assembled prompt blocked by injection scanner — %s",
  367. job.get("name") or job.get("id") or "<unknown>", scan_error)
  368. raise _sched.CronPromptInjectionBlocked(scan_error)
  369. return assembled
  370. def _guard_job_credential_exfil(job: dict) -> None:
  371. """Fail closed (RuntimeError) if the stored provider/base_url pair could exfiltrate a key.
  372. Runtime backstop: jobs persisted before the create/update guard, or written directly to the
  373. store, reach provider resolution unchecked. Fallback providers come from operator config and
  374. are validated by the caller, not here."""
  375. try:
  376. from tools.cronjob_tools import _validate_cron_base_url
  377. err = _validate_cron_base_url(job.get("provider"), job.get("base_url"))
  378. except Exception as exc:
  379. # Fail CLOSED on validator/import errors — but only for jobs WITH a base_url override;
  380. # a job without one cannot exfiltrate via this path, so it still runs.
  381. err = (
  382. f"could not validate provider/base_url pair "
  383. f"({exc.__class__.__name__}: {exc}); refusing to run a job with "
  384. "an unverified base_url override"
  385. ) if job.get("base_url") else None
  386. if err:
  387. job_id = job.get("id")
  388. logger.error(
  389. "Job '%s': refusing to run — unsafe provider/base_url pair could "
  390. "exfiltrate a stored credential: %s",
  391. job_id, err)
  392. raise RuntimeError(f"Cron job '{job_id}' blocked for safety: {err}")
  393. def _block_and_pause_job(
  394. job_id: str, job_name: str, reason: str) -> tuple[bool, str, str, Optional[str]]:
  395. """Fail a run closed and pause the job: an unrunnable job left enabled re-fires every tick
  396. forever; ``paused_at``/``paused_reason`` give an auditable record."""
  397. from cron.jobs import pause_job
  398. logger.error("Job '%s': %s", job_id, reason)
  399. try:
  400. pause_job(job_id, f"Auto-paused by scheduler: {reason}")
  401. except Exception:
  402. logger.exception("Job '%s': failed to auto-pause unrunnable job", job_id)
  403. now_iso = _hermes_now().strftime("%Y-%m-%d %H:%M:%S")
  404. doc = (
  405. f"# Cron Job: {job_name}\n\n"
  406. f"**Job ID:** {job_id}\n"
  407. f"**Run Time:** {now_iso}\n"
  408. f"**Status:** blocked (unrunnable job) — auto-paused\n\n"
  409. f"{reason}\n"
  410. )
  411. alert = f"⚠ Cron job '{job_name}' was auto-paused\n\n{reason}"
  412. return False, doc, alert, reason
  413. # Late-bound origin namespace (see module docstring). Imported LAST so this module is fully
  414. # populated before ``scheduler`` re-exports from it.
  415. from cron import scheduler as _sched
  416. from cron import scheduler_delivery as _delivery
  417. from cron import scheduler_script as _script