# task-705.1: 텍스트 포스트 자수 하드 리밋 강제

## 배경
프롬프트에 text_data: 80~120자 규칙이 있지만 LLM이 무시하고 500자+ 생성한다.
review 단계의 G 섹션 검사도 실질적으로 자수 초과를 걸러내지 못한다.
제이회장님 지시: "현행규칙대로" → 코드 레벨에서 하드 리밋 강제.

## 현행 자수 규칙 (03_writing.md, 05_review.md)
```
text_empathy: 50~80자
text_data: 80~120자
text_story: 120~200자
text_insight: 90~140자
text_cta_soft: 70~100자
text_cta_hard: 80~120자
```
- 이 자수는 **줄바꿈(\n) 제외** 순수 텍스트 기준으로 해석한다.
- 줄바꿈 포함 시 실제 len()은 더 길어지므로, 검증 시 `text.replace('\n', '')` 기준으로 카운트.

## 수정 대상

### `/home/jay/projects/ThreadAuto/content/five_stage_pipeline.py`

#### 1. 자수 상수 정의 (파일 상단, VALID_CONTENT_TYPES 근처)
```python
TEXT_CHAR_LIMITS: dict[str, tuple[int, int]] = {
    "text_empathy": (50, 80),
    "text_data": (80, 120),
    "text_story": (120, 200),
    "text_insight": (90, 140),
    "text_cta_soft": (70, 100),
    "text_cta_hard": (80, 120),
}
```

#### 2. `_build_result()` 메서드 — text_* 분기 안에 자수 강제 로직 추가
텍스트 추출 후, 자수 초과 시 문장 단위로 잘라서 max 이내로 맞춘다.

```python
# 자수 강제 (줄바꿈 제외 기준)
if content_type in TEXT_CHAR_LIMITS:
    min_chars, max_chars = TEXT_CHAR_LIMITS[content_type]
    pure_len = len(text.replace('\n', ''))
    if pure_len > max_chars:
        # 문장 단위 트리밍: 끝에서부터 문장을 하나씩 제거
        lines = text.split('\n')
        trimmed_lines = []
        current_len = 0
        for line in lines:
            line_len = len(line)
            if current_len + line_len > max_chars:
                break
            trimmed_lines.append(line)
            current_len += line_len
        text = '\n'.join(trimmed_lines).rstrip('\n')
        logger.warning(
            "text_* 자수 초과로 트리밍: %d자 → %d자 (max=%d, content_type=%s)",
            pure_len, len(text.replace('\n', '')), max_chars, content_type,
        )
```

**주의**: 위 트리밍 로직은 참고용이다. 문장 경계(마침표)를 더 정밀하게 잡아도 된다.
핵심은 `text.replace('\n', '')` 기준으로 max_chars를 넘지 않도록 강제하는 것.

#### 3. Threads API 500자 제한 대응
- 면책 문구(`AUTO_POST_DISCLAIMER`)와 `\n\n`을 합산한 최종 길이가 500자를 넘지 않도록 확인
- 면책 문구 ~31자 + `\n\n` 2자 = 약 33자 예약
- text_* 자수 상한이 200자(text_story)이므로, 줄바꿈 포함해도 500자 제한에는 여유 있음
- 별도 처리 불필요 (현행 자수 규칙이면 500자 제한에 걸릴 일 없음)

## 검증 방법
수정 후 테스트:
```bash
cd /home/jay/projects/ThreadAuto && python3 -c "
from content.five_stage_pipeline import FiveStagePipeline, TEXT_CHAR_LIMITS
from content.topic_selector import select_single_topic

topic = select_single_topic(category='업계동향')
pipeline = FiveStagePipeline()
content = pipeline.generate(topic, 'text_data')
text = content.get('text', '')
pure_len = len(text.replace('\n', ''))
min_c, max_c = TEXT_CHAR_LIMITS['text_data']
print(f'순수 텍스트: {pure_len}자 (허용: {min_c}~{max_c})')
print(f'줄바꿈 포함: {len(text)}자')
print(text)
assert pure_len <= max_c, f'자수 초과: {pure_len} > {max_c}'
print('PASS')
"
```

## 작업 완료 시
- `python3 /home/jay/workspace/memory/task-timer.py end task-705.1`