返回 last30days-skill
report_checklist.py
根目录 / skills / last30days / scripts / lib / report_checklist.py
1 """The short format checklist printed after ``--emit=compact`` stdout.
2
3 The report's format rules live in the skill's instruction files, which a host
4 model reads long before the engine's output arrives. This block restates the
5 few that decide whether the report renders correctly, as facts about the
6 output it follows, at the point the model starts writing.
7
8 It is command-line presentation, not evidence and not part of the canonical
9 output: the renderers never produce it, so the saved file, ``--emit=md``,
10 JSON, HTML and the MCP ``research`` tool do not carry it. It describes the
11 default report shape and claims no precedence over user or host instructions.
12 """
13
14 from __future__ import annotations
15
16 import os
17 import re
18 from typing import Any, Mapping
19
20 from . import env
21
22 HEADING = "## Before you write the report"
23
24 _QUOTE_POOL_HEADING = "## Top Community Comments"
25 _SUBJECT_POSTS_HEADING = re.compile(r"^## Posts by (@\S+)$", re.MULTILINE)
26 _JOBS_DIGEST_HEADING = "## Jobs Digest"
27 _PATENT_DIGEST_HEADING = "## Patent Digest"
28 _POLYMARKET_HEADING = "## Polymarket Markets"
29 _PERSON_PROFILE_HEADING = "## Person Profile"
30 # Registers and shapes whose template has no story paragraphs or quote lines.
31 _REGISTERS_WITHOUT_QUOTE_LINES = frozenset({"exec"})
32
33
34 # The topics the research runbook classifies as QUERY_TYPE = RECOMMENDATIONS:
35 # "best X", "top X", "what X should I use", "recommended X". The engine is
36 # handed no query type, only the topic and the plan's intent, so the topic's
37 # own words are the signal.
38 _RECOMMENDATION_TOPIC = re.compile(
39 r"^\s*(?:what(?:'s| is| are)?\s+(?:the\s+)?)?(?:best|top|recommended)\b"
40 r"|\bshould i (?:use|buy|get|pick|choose)\b|\brecommendations?\b",
41 re.IGNORECASE,
42 )
43 # Plan intents under which a topic that opens "Top ..." or "Best ..." is a
44 # name or a news story, not a request for picks.
45 _NOT_RECOMMENDATION_INTENTS = frozenset(
46 {"breaking_news", "prediction", "comparison", "factual"}
47 )
48
49
50 def is_recommendation_run(intent: str | None, *topics: str | None) -> bool:
51 """Whether a run's topic asks for recommended picks."""
52 if (intent or "").strip().lower() in _NOT_RECOMMENDATION_INTENTS:
53 return False
54 return any(_RECOMMENDATION_TOPIC.search(topic or "") for topic in topics)
55
56
57 def _has_heading(rendered: str, heading: str) -> bool:
58 return f"\n{heading}\n" in rendered
59
60
61 # A detailed patent entry: a linked title whose line carries its abstract or first claim.
62 _PATENT_ENTRY_WITH_TEXT = re.compile(r"^- \[.*\| (?:abstract|first claim): ", re.MULTILINE)
63
64
65 def _patent_digest_has_text(rendered: str) -> bool:
66 """True when the ``## Patent Digest`` block lists at least one filing with its text."""
67 marker = f"\n{_PATENT_DIGEST_HEADING}\n"
68 if marker not in rendered:
69 return False
70 block = rendered.split(marker, 1)[1].split("\n## ", 1)[0]
71 return bool(_PATENT_ENTRY_WITH_TEXT.search(block))
72
73
74 def on_claude_code(environ: Mapping[str, Any] | None = None) -> bool:
75 """True when the run's environment carries Claude Code's ``CLAUDECODE``."""
76 source = os.environ if environ is None else environ
77 return bool(str(source.get("CLAUDECODE") or "").strip())
78
79
80 def claude_code_in_terminal(environ: Mapping[str, Any] | None = None) -> bool:
81 """True when Claude Code runs as the terminal app (``CLAUDE_CODE_ENTRYPOINT``
82 is ``cli``, or unset on older builds). Only the terminal redraws links as
83 ``name (url)`` while a markdown list streams; T3 Code (``sdk-ts``), the
84 desktop app and IDEs render the markdown themselves."""
85 source = os.environ if environ is None else environ
86 entrypoint = str(source.get("CLAUDE_CODE_ENTRYPOINT") or "").strip()
87 return entrypoint in ("", "cli")
88
89
90 def build(
91 rendered: str,
92 *,
93 comparison: bool = False,
94 recommendation: bool = False,
95 register: str = "default",
96 environ: Mapping[str, Any] | None = None,
97 ) -> str:
98 """The checklist block for ``rendered`` compact output, or "" when the
99 caller asked for none (``LAST30DAYS_NO_REPORT_CHECKLIST``).
100
101 Comparison, recommendation and ``exec`` reports have no story paragraphs
102 with quote lines under them (a recommendation puts quoted evidence on each
103 pick's ``Evidence:`` line), so they get neither of those two lines."""
104 if env.report_checklist_disabled(environ):
105 return ""
106 story_paragraphs = (
107 not comparison
108 and not recommendation
109 and register not in _REGISTERS_WITHOUT_QUOTE_LINES
110 )
111 lines = [
112 "Format facts for a report written from this output. User and host instructions come first.",
113 "• Line 1 is the badge line, copied as printed (`🌐 last30days · ...`), then one blank line.",
114 ]
115 if story_paragraphs:
116 lines.append(
117 "• Each story paragraph opens with a bold headline phrase: `**Headline** - text`."
118 )
119 pools = []
120 if _has_heading(rendered, _QUOTE_POOL_HEADING):
121 pools.append(f"`{_QUOTE_POOL_HEADING}`")
122 subject_posts = _SUBJECT_POSTS_HEADING.search(rendered)
123 if subject_posts:
124 pools.append(f"`## Posts by {subject_posts.group(1)}`")
125 source = (
126 " Copy the entry through its count (through the platform name when it "
127 f"has no count) from {' or '.join(pools)}."
128 if pools
129 else ""
130 )
131 lines.append(
132 "• The report's best six quotes each get their own line under their "
133 'paragraph: `> "quote" 🟠 u/author on r/sub · 1,315 upvotes` (six in the '
134 "whole report, the subject's own posts included; a ceiling, not a target). "
135 "Other good quotes may stay inside a paragraph, attributed and linked, with "
136 "no count; no quote is used both ways, and weak or off-topic comments are "
137 "not quoted. Use each thread's or video's top comment first, then its #2; "
138 "an entry ending `said` is a line said in the video itself and is a quote "
139 "like any comment; like counts do not rank a comment from one place "
140 "against another."
141 + source
142 )
143 if _has_heading(rendered, _POLYMARKET_HEADING):
144 lines.append(
145 "• Polymarket odds for a story go on their own line under it: copy the "
146 f"whole odds line from `{_POLYMARKET_HEADING}`, up to the ` — ` before its "
147 "URL (at most three in the report); the paragraph links the market and "
148 "does not repeat the numbers. "
149 "Shape: `> 📊 Polymarket · Huang divorce: 9.5% by Dec 31 (down 1 pt today) · "
150 "18% by Mar 31, 2027`. They do not count toward the six quote lines."
151 )
152 if on_claude_code(environ) and claude_code_in_terminal(environ):
153 lines.append(
154 "• `CLAUDECODE` is set in the terminal: no markdown lists anywhere in the reply "
155 "(KEY PATTERNS as `1\\.`, invitation lines as `•`). Keep each block tight: "
156 "end every line of the block except its last with `\\` (the label line too) and "
157 "leave no blank line between them; a markdown viewer joins plain line breaks. "
158 "In a reply that holds a list, "
159 "Claude Code prints every link as raw `name (url)` text. This holds for the whole "
160 "message, not only the report: notes you add before or after it carry no bullets or "
161 "numbered lines either, and it holds again whenever you show the report in a later "
162 "message."
163 )
164 elif on_claude_code(environ):
165 lines.append(
166 "• `CLAUDECODE` is set in an app that renders markdown: write KEY PATTERNS as a "
167 "real numbered list and the invitation as a real bulleted list, with no blank line "
168 "between items."
169 )
170 if not comparison:
171 # One line for both digests: the checklist stays a few lines long.
172 if _has_heading(rendered, _JOBS_DIGEST_HEADING):
173 jobs = (
174 f"This output has a `{_JOBS_DIGEST_HEADING}` block: `hiring-signals.md` "
175 "decides whether a Jobs Page Secret Signals section is written."
176 )
177 else:
178 jobs = (
179 f"This output has no `{_JOBS_DIGEST_HEADING}` block, so the report has "
180 "no Jobs Page Secret Signals section."
181 )
182 if _has_heading(rendered, _PATENT_DIGEST_HEADING):
183 patents = (
184 f"This output has a `{_PATENT_DIGEST_HEADING}` block: `patent-signals.md` "
185 "decides whether a Patent Secret Signals section is written."
186 )
187 if story_paragraphs and _patent_digest_has_text(rendered):
188 # The check is owed before the section is written or left out, so it is
189 # stated here, outside the evidence block, not only in the reference.
190 patents += (
191 " Before deciding, judge every detailed entry for something concrete a "
192 "reader can picture, from its abstract and first claim, then web-check the "
193 "two strongest concrete entries for coverage and a matching product before "
194 "dropping either; send these patent checks in the Step 2 dispatch in "
195 "addition to the three supplements. With no concrete entry, run no patent "
196 "search."
197 )
198 else:
199 patents = (
200 f"This output has no `{_PATENT_DIGEST_HEADING}` block, so the report has "
201 "no Patent Secret Signals section."
202 )
203 digests = f"• {jobs} {patents}"
204 if _has_heading(rendered, _PERSON_PROFILE_HEADING):
205 # Shares the digest line: the checklist stays a few lines long.
206 digests += (
207 f" This output has a `{_PERSON_PROFILE_HEADING}` block: call the person quiet "
208 "only when it says `LinkedIn: checked` and gives a count of zero LinkedIn posts in "
209 "the window (not `unknown`); otherwise "
210 "name the sources that were not checked. Use no fact from an item that does not "
211 "match the person's identity, and say so when identity is uncertain."
212 )
213 lines.append(digests)
214 lines.append(
215 "• The fenced footer (```text ... ```) is copied whole, fence included, "
216 "after the body and before the invitation."
217 )
218 lines.append("• The report ends on the `I'm now an expert on ...` block.")
219 return "\n".join(["", HEADING, "", *lines]) + "\n"
220
220 lines PYTHON