Fix date framing in briefings and make Blogger posts idempotent

The 9/1 briefing called the US 8/31 session "9월 첫 거래일" - the LLM
was anchoring on the KRX date. Both prompts now pin every temporal
phrase (weekday, first/last trading day, month boundaries) to the US
session date, and the verifier treats a mismatch as a major issue.
BloggerDeliverer now updates the existing post for the same KRX date
instead of creating a duplicate on resends.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
jsahn
2026-09-01 14:13:01 +09:00
co-authored by Claude Fable 5
parent b75bf0b887
commit 830e4e0522
3 changed files with 131 additions and 29 deletions
+56 -13
View File
@@ -38,12 +38,16 @@ def briefing_html(markdown_text: str) -> str:
return _MD.convert("\n".join(lines).strip())
def post_title_prefix(result: BriefingResult) -> str:
"""Stable per-day prefix - resends update the existing post instead of creating a duplicate."""
return f"미국 마감 브리핑 {result.payload.krx_date.isoformat()}"
def post_title(result: BriefingResult) -> str:
"""'미국 마감 브리핑 YYYY-MM-DD — 헤드라인' (falls back to the session date without a report)."""
p = result.payload
base = f"미국 마감 브리핑 {p.krx_date.isoformat()}"
base = post_title_prefix(result)
headline = (result.report.headline or "").strip() if result.report else ""
return f"{base} — {headline}" if headline else f"{base} (미 {p.session_date.isoformat()} 세션)"
return f"{base} — {headline}" if headline else f"{base} (미 {result.payload.session_date.isoformat()} 세션)"
class BloggerDeliverer:
@@ -101,7 +105,7 @@ class BloggerDeliverer:
# ------------------------------------------------------------------ delivery
def deliver(self, result: BriefingResult) -> str:
"""Create the post and return its public URL."""
"""Create the day's post - or update it when one already exists - and return its public URL."""
body: dict[str, Any] = {
"kind": "blogger#post",
"title": post_title(result),
@@ -109,31 +113,70 @@ class BloggerDeliverer:
}
if self.labels:
body["labels"] = self.labels
url = f"{API_BASE}/blogs/{self.blog_id}/posts/"
existing_id = self._find_existing(post_title_prefix(result))
if existing_id:
data = self._request("PUT", f"{API_BASE}/blogs/{self.blog_id}/posts/{existing_id}", json_body=body)
action = "updated"
else:
data = self._request("POST", f"{API_BASE}/blogs/{self.blog_id}/posts/", json_body=body)
action = "published"
post_url = data.get("url") or f"post id {data.get('id', '?')}"
log.info("blogger post %s: %s", action, post_url)
return post_url
def _find_existing(self, title_prefix: str) -> str | None:
"""Id of the newest post whose title carries this day's prefix; None on miss or lookup failure."""
try:
data = self._request(
"GET",
f"{API_BASE}/blogs/{self.blog_id}/posts",
params={"fetchBodies": "false", "maxResults": "25"},
)
except DeliveryError as exc:
if "token refresh failed" in str(exc):
raise # a broken token will fail the publish too - surface it once, immediately
# any other lookup failure must not block a fresh publish
log.warning("blogger post lookup failed, publishing as a new post: %s", exc)
return None
for item in data.get("items", []):
if str(item.get("title", "")).startswith(title_prefix):
return str(item["id"])
return None
def _request(
self,
method: str,
url: str,
*,
params: dict[str, str] | None = None,
json_body: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""One authorized API call with retries (fresh token on 401, backoff on 429/5xx)."""
force_token = False
last_error = "unknown error"
for attempt in range(1, MAX_ATTEMPTS + 1):
try:
resp = self._client.post(
url, json=body, headers={"Authorization": f"Bearer {self._token(force=force_token)}"}
resp = self._client.request(
method,
url,
params=params,
json=json_body,
headers={"Authorization": f"Bearer {self._token(force=force_token)}"},
)
except httpx.HTTPError as exc:
last_error = f"{type(exc).__name__}: {exc}"
log.warning("blogger post attempt %d/%d failed: %s", attempt, MAX_ATTEMPTS, last_error)
log.warning("blogger %s attempt %d/%d failed: %s", method, attempt, MAX_ATTEMPTS, last_error)
time.sleep(BACKOFF_BASE_SECONDS * attempt)
continue
if resp.status_code == 200:
data = resp.json()
post_url = data.get("url") or f"post id {data.get('id', '?')}"
log.info("blogger post published: %s", post_url)
return post_url
return resp.json()
last_error = f"HTTP {resp.status_code} {resp.text[:200]}"
if resp.status_code == 401 and not force_token:
force_token = True # cached token stale/revoked - mint a fresh one and retry
continue
if resp.status_code in (429, 500, 502, 503, 504) and attempt < MAX_ATTEMPTS:
log.warning("blogger post attempt %d/%d failed: %s", attempt, MAX_ATTEMPTS, last_error)
log.warning("blogger %s attempt %d/%d failed: %s", method, attempt, MAX_ATTEMPTS, last_error)
time.sleep(BACKOFF_BASE_SECONDS * attempt)
continue
break # non-retryable (4xx)
+8 -1
View File
@@ -64,6 +64,11 @@ SYSTEM_PROMPT: str = f"""당신은 한국 투자자를 위한 미국 시장 마
8. 출력은 지정된 JSON 스키마만 반환합니다. 스키마 밖의 텍스트, 마크다운 코드 펜스, 설명을 추가하지 않습니다.
배열 필드는 반드시 JSON 배열(각 항목이 한 문장인 문자열 배열)로 출력하고, 여러 문장을 하나의 문자열로 이어 쓰지
않습니다.
9. 날짜·요일·"첫 거래일 / 마지막 거래일"·"월초 / 월말" 같은 시점 표현은 전부 **미국 거래일**(세션 정보의
'미국 거래일') 기준으로만 씁니다. 미국 장 마감이 한국 시간으로 다음 날 새벽이라 '한국 거래일'은 하루 뒤인
경우가 많은데, 한국 거래일 날짜를 미국 장 서술에 쓰면 안 됩니다.
예: 미국 거래일이 8월 31일(월)이면 이 세션은 "8월 마지막 거래일"입니다 — 한국 거래일이 9월 1일이어도
"9월 첫 거래일"이라고 쓰면 틀립니다.
## 입력 데이터 용어집
사용자 메시지의 시세는 아래 그룹으로 나뉘어 제공됩니다.
@@ -171,7 +176,9 @@ VERIFY_SYSTEM_PROMPT: str = """당신은 미국 시장 브리핑의 팩트체커
1. 판단 근거는 사용자 메시지에 제공된 데이터(시세·히스토리 노트·뉴스·경제지표·실적)뿐입니다.
당신의 배경지식이나 최신 시황과 달라 보여도, 제공된 데이터와 일치하면 문제가 아닙니다.
2. major: 브리핑의 숫자가 데이터와 다르거나(반올림 오차 ±0.05는 허용), 상승/하락 방향이 반대이거나,
데이터에 없는 숫자·사건을 사실처럼 서술한 경우.
데이터에 없는 숫자·사건을 사실처럼 서술한 경우. 시점 표현이 세션 정보의 '미국 거래일'과 어긋나는 경우도
major입니다 — 날짜·요일·"N월 첫/마지막 거래일"·"월초/월말"은 미국 거래일 기준이어야 하며, 한국 거래일
날짜로 미국 장을 서술하면 안 됩니다 (예: 미국 거래일이 8월 31일인 세션을 "9월 첫 거래일"로 서술).
3. minor: 뉴스 헤드라인만으로 확인되지 않는 인과관계를 단정, 제공된 표기와 다른 종목명, 모호하지만
틀렸다고 단정할 수 없는 서술.
4. 각 이슈의 claim에는 브리핑 원문을 그대로 인용하고, problem에는 데이터의 실제 값과 함께 한 문장으로