<!-- task=m-iop-agent-cli-runtime/07+06_target_policy plan=0 tag=API -->

# Deterministic Target Policy Evaluator

## For the Implementing Agent

`06+05_config_registry/complete.log`를 확인한 뒤 진행한다. 구현 evidence만 `CODE_REVIEW-cloud-G07.md`에 채운다.

## Background

`agenttask.Selector`는 port뿐이며 (`ports.go`), ordered rule precedence·reason/history가 구현돼 있지 않다. runtime은 one target을 결정적으로 선택하고 damaged route를 silent reselect하지 않아야 한다.

## Roadmap Targets

- Milestone: `agent-roadmap/phase/automation-runtime-bridge/milestones/iop-agent-cli-runtime.md`
- Milestone link: [Milestone 문서](agent-roadmap/phase/automation-runtime-bridge/milestones/iop-agent-cli-runtime.md)
- Task ids:
  - `target-policy`: ordered evaluator와 durable route history
- Completion mode: check-on-pass

## Analysis

### Files Read

- `packages/go/agenttask/ports.go`
- `packages/go/agenttask/types.go`
- `packages/go/agenttask/dispatch.go`
- `packages/go/agentconfig/catalog.go`
- `agent-contract/inner/agent-runtime.md`
- `agent-roadmap/sdd/automation-runtime-bridge/iop-agent-cli-runtime/SDD.md`

### SDD Criteria

S06/Evidence Map S06: time/quota/stage/grade overlap에서 first-match 하나만 반환하고 selected rule, rejected candidates, persisted route를 immutable revision과 함께 보존한다.

### Test Environment Rules

local rules를 읽었고 `go test -count=1 ./packages/go/agenttask` baseline은 PASS다.

### Split Judgment

registry snapshot이 stable input이므로 `06`에 의존한다. quota observation/failover policy는 `08`에서 확장한다.

### Scope Rationale

Scheduler capacity와 provider 실행은 변경하지 않는다. selector는 policy evaluation과 durable decision만 소유한다.

### Final Routing

`first-pass`; local G07. temporal-state와 structured-interpretation risk 2개. review는 official cloud G07.

## Implementation Checklist

- [ ] Config snapshot에서 policy input을 받고 one-target `RouteDecision`을 산출하는 evaluator를 추가한다.
- [ ] ordered first-match, time/stage/grade/capability predicate와 no-match blocker를 구현한다.
- [ ] route decision/history encode-decode와 corruption rejection을 구현한다.
- [ ] overlap, first-match, persisted-history/tamper test를 추가한다.
- [ ] Fill implementation-owned sections in CODE_REVIEW-*-G??.md with actual implementation notes and verification output.

### [API-1] policy selection과 durable decision을 구현한다

**Problem:** `agenttask` dispatch는 `m.selector.Select` 결과를 신뢰하지만 source policy/reason history는 없다.

**Solution:** common runtime 아래 새 policy package를 만들고 `agentconfig.RuntimeSnapshot`만 입력으로 받는다. rule array 순서대로 하나의 profile을 선택하고 JSON decision envelope에 candidate/rule/reason/config revision을 기록한다; malformed/foreign revision은 typed error다.

**Test Strategy:** table test로 overlapping rules, boundary time, grade/stage, no eligible candidate, stable history replay와 tamper를 검증한다.

**Verification:** policy package와 `agenttask` fresh tests가 PASS한다.

## Modified Files Summary

| File | Item |
|---|---|
| `packages/go/agentpolicy/evaluator.go` | API-1 |
| `packages/go/agentpolicy/decision.go` | API-1 |
| `packages/go/agentpolicy/evaluator_test.go` | API-1 |
| `packages/go/agenttask/ports.go` | API-1 |

## Dependencies and Execution Order

`06+05_config_registry` PASS 후에만 구현한다.

## Final Verification

```bash
go test -count=1 ./packages/go/agentpolicy ./packages/go/agenttask
git diff --check
```

After completing all code changes, fill implementation-owned sections in `CODE_REVIEW-*-G??.md`.
