Files
2nd/10_Wiki/Topic_Programming/AI_and_ML/Refinement.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

6.6 KiB
Raw Blame History

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-refinement Refinement 10_Wiki/Topics verified self
Iterative-Refinement
Self-Refine
Type-Refinement
none A 0.9 applied
llm
iteration
types
design
2026-05-10 pending
language framework
python anthropic-sdk

Refinement

매 한 줄

"매 첫 시도는 draft, 매 refinement는 product". Refinement는 초안을 critique → revise loop로 다듬어 quality를 끌어올리는 patterns의 family — LLM self-refine, type narrowing, design iteration 모두 매 같은 핵심 idea를 공유.

매 핵심

매 LLM Self-Refine (2024 Madaan et al. → 2026 mainstream)

  • Generate → Critique → Refine loop. Single model이 매 세 role을 모두 수행.
  • 매 효과: math, code, dialog 매 +520% accuracy without extra training.
  • Reflexion (Shinn 2023)은 verbal RL — 매 episode 끝에 매 self-critique를 episodic memory로 저장.
  • 2026 standard: Claude Opus 4.7 / GPT-5 매 native "extended thinking" mode가 매 refine을 internal로 흡수 → external loop는 매 high-stakes (legal, medical) 에서만.

매 Type Refinement (TypeScript / Flow / Python typing)

  • Narrowing: union type 의 매 instance를 specific subtype 으로 좁힘 (typeof, instanceof, discriminated union).
  • Refinement type: predicate-attached type — {x: number | x > 0}. Liquid Haskell, F* 매 사용.
  • 2026 TS 5.x: satisfies operator + control-flow analysis 매 강력 — 매 manual cast 의 거의 elimination.

매 Design / Spec Refinement

  • Stepwise refinement (Wirth 1971) — abstract spec → concrete implementation을 매 단계적으로.
  • BDD (Given-When-Then) 매 modern incarnation.
  • AI-aided: spec → Claude → multiple impl candidates → human picks → refine.

매 응용

  1. RAG answer 의 self-refine으로 hallucination ↓.
  2. Code generation 매 compile error → refine loop.
  3. TS API 의 progressive type narrowing.
  4. Product spec 의 PM ↔ AI iterative tightening.

💻 패턴

Self-Refine loop (Anthropic SDK)

from anthropic import Anthropic

client = Anthropic()
MODEL = "claude-opus-4-7"

def self_refine(task: str, max_iter: int = 3) -> str:
    answer = client.messages.create(
        model=MODEL, max_tokens=2048,
        messages=[{"role": "user", "content": task}],
    ).content[0].text

    for i in range(max_iter):
        critique = client.messages.create(
            model=MODEL, max_tokens=1024,
            system="You are a strict critic. List concrete flaws or reply 'NO_ISSUES'.",
            messages=[{"role": "user", "content": f"Task: {task}\n\nDraft:\n{answer}"}],
        ).content[0].text

        if "NO_ISSUES" in critique:
            return answer

        answer = client.messages.create(
            model=MODEL, max_tokens=2048,
            messages=[{"role": "user",
                       "content": f"Task: {task}\nDraft: {answer}\nCritique: {critique}\nRevise."}],
        ).content[0].text
    return answer

Reflexion-style episodic memory

class Reflexion:
    def __init__(self):
        self.memory: list[str] = []  # accumulated lessons

    def step(self, task: str) -> str:
        ctx = "\n".join(f"- {m}" for m in self.memory[-5:])
        attempt = llm(f"Task: {task}\nPast lessons:\n{ctx}\nAct.")
        feedback = environment(attempt)
        if not feedback.success:
            lesson = llm(f"Why did this fail? Task: {task}\nAttempt: {attempt}\nFeedback: {feedback}")
            self.memory.append(lesson)
        return attempt

TypeScript discriminated union narrowing

type Result<T> =
  | { kind: 'ok'; value: T }
  | { kind: 'err'; error: Error };

function unwrap<T>(r: Result<T>): T {
  if (r.kind === 'err') throw r.error;  // narrow → 'ok' branch
  return r.value;  // typed as T, no cast
}

Python TypeGuard refinement

from typing import TypeGuard

def is_str_list(x: list[object]) -> TypeGuard[list[str]]:
    return all(isinstance(i, str) for i in x)

def join(items: list[object]) -> str:
    if is_str_list(items):
        return ", ".join(items)  # narrowed
    raise TypeError("not str list")

Compile-error refine loop (code generation)

def codegen_refine(spec: str, max_iter=5):
    code = llm(f"Write Python for: {spec}")
    for _ in range(max_iter):
        ok, err = run_pytest(code)
        if ok: return code
        code = llm(f"Spec: {spec}\nCode: {code}\nFailing: {err}\nFix.")
    raise RuntimeError("refinement budget exhausted")

Best-of-N + judge (ensemble refinement)

def best_of_n(prompt: str, n: int = 5) -> str:
    candidates = [llm(prompt, temperature=1.0) for _ in range(n)]
    ranking = llm(f"Pick best of these:\n{candidates}\nReturn index 0..{n-1}")
    return candidates[int(ranking.strip())]

매 결정 기준

상황 Approach
Single-shot good enough (chat) No refine — extra cost
High-stakes (legal/medical) External self-refine + human review
Code with tests Compile/test-driven refine
Long agentic task Reflexion (episodic memory)
Reasoning math extended thinking (native) — already refine internally
TS API design Narrowing + satisfies

기본값: native extended thinking 우선 → 부족하면 external self-refine 12 iter.

🔗 Graph

🤖 LLM 활용

언제: high-stakes output, agentic loops, code with verifiable feedback. 언제 X: 매 latency-sensitive UX, 매 simple chat — extra latency × cost는 매 안 맞음.

안티패턴

  • Infinite refine loop: max_iter 의 hard cap 의 X → cost explosion.
  • Same-model critique only: 매 critic = generator인 경우 같은 blind spot. Mix models (Opus critic, Sonnet generator).
  • Refine without termination signal: "NO_ISSUES" 같은 매 explicit stop 의 부재 → 매 endless tweaking.
  • Type assertion 으로 narrow: TS 매 as 사용은 매 refinement 의 X — 매 unsafe cast.

🧪 검증 / 중복

  • Verified (Madaan 2024 Self-Refine, Shinn 2023 Reflexion, TS handbook narrowing).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — full rewrite covering LLM self-refine, type narrowing, design iteration