<!-- task=m-iop-agent-cli-runtime/20+13,17_project_log_journal plan=0 tag=API -->

# CAS Project Log Journal and Archive Reconciliation

## For the Implementing Agent

Fill every implementation-owned section in `CODE_REVIEW-cloud-G08.md`, including actual command output. Keep active files in place and report ready for review; only the review agent finalizes. Record blockers and resume conditions in evidence fields without asking the user or creating control-plane artifacts.

## Background

Stable records need one CAS-governed project journal that survives append conflicts and archive crash windows. This packet owns durable append, replay, retention, terminal archive, and restart reconciliation without owning event projection or the final S12 scenario.

## Analysis

### Files Read

- `packages/go/agentstate/store.go`
- `packages/go/agenttask/types.go`
- `agent-task/m-iop-agent-cli-runtime/17+13_project_log_records/plan_local_G08_0.log`
- `agent-task/m-iop-agent-cli-runtime/17+13_project_log_records/code_review_cloud_G08_0.log`

### SDD Criteria

S12 requires restart-safe evidence and exactly-once terminal archives. This strict subset supplies the durable journal and crash-window matrix but leaves EventSink mapping, WORK_LOG projection, and the 11-retry closure scenario to the next child.

### Verification Context

Use fresh focused tests, real temp filesystem state, race, vet, formatting, and `git diff --check` from the original plan.

### Test Coverage Gaps

- No project-log adapter uses `agentstate.Store` CAS records.
- No test covers archive intent, rename, manifest commit, cleanup, or restart conflicts.

### Symbol References

None. The adapter consumes the record schema from predecessor 17.

### Split Judgment

API-2 is independently implementable and verifiable after the record producer. It owns the persistence invariant while the sink/closure child remains a consumer.

### Scope Rationale

Do not map manager events, render WORK_LOG, add the 11-retry parallel scenario, update the contract, or persist raw provider streams.

### Final Routing

- evaluation_mode: `first-pass`
- finalizer: `finalize-task-policy.sh pair`
- build: `local-G08`, basis `local-fit`, filename `PLAN-local-G08.md`
- review: `cloud-G08`, basis `official-review`, filename `CODE_REVIEW-cloud-G08.md`
- large_indivisible_context: `false`
- positive loop risks: `CAS/archive crash windows`, `retry/parallel identity matrix` (count `2`)
- recovery signals: rework `0`, evidence-integrity failure `false`
- capability-gap evidence: none

## Implementation Checklist

- [x] Implement CAS-backed append, retention, replay, terminal archive, and restart reconciliation over device-local roots.
- [x] Fill implementation-owned sections in CODE_REVIEW-*-G??.md with actual implementation notes and verification output.

### [API-2] Implement CAS Journal and Archive Reconciliation

#### Problem

`agentstate.Store` supports opaque CAS integration records, but no project-log adapter coordinates append, retention, or archive state.

#### Solution

Use one namespaced integration record per project as the logical journal. Append by CAS with bounded retry, assign monotonic sequence and independent archive ordinal, prune only after terminal evidence, and materialize redacted JSONL/timeline files with temp-file sync/rename. Persist archive intent/checksum before cleanup; reconcile matching artifacts idempotently and fail closed on conflicting content.

#### Modified Files and Checklist

- [x] `apps/agent/internal/projectlog/store.go` — implement append, replay, archive, and reconcile.
- [x] `apps/agent/internal/projectlog/store_test.go` — cover CAS conflict, retention, crash phases, restart, and exactly-once output.

#### Test Strategy

Use temp roots and real `agentstate.Store`. Inject failures before write, after artifact rename, before manifest commit, and before cleanup. Assert one logical archive and unchanged evidence on replay.

#### Verification

```bash
go test -count=1 ./apps/agent/internal/projectlog -run 'TestStore|TestArchive|TestReconcile'
```

Expected: crash/restart cases converge or fail closed without duplicates.

## Dependencies and Execution Order

Predecessors `13_agent_domain` and `17+13_project_log_records` must each produce a same-group active or archived `complete.log`.

## Modified Files Summary

| File | Item |
|------|------|
| `apps/agent/internal/projectlog/store.go` | API-2 |
| `apps/agent/internal/projectlog/store_test.go` | API-2 |

## Final Verification

```bash
gofmt -w apps/agent/internal/projectlog/store.go apps/agent/internal/projectlog/store_test.go
go test -count=1 ./apps/agent/internal/projectlog -run 'TestStore|TestArchive|TestReconcile'
go test -count=1 -race ./apps/agent/internal/projectlog ./packages/go/agentstate
go vet ./apps/agent/internal/projectlog
git diff --check
```

Expected: fresh, race, vet, formatting, and diff checks pass with exactly-once archive behavior. After completing all changes, fill implementation-owned sections in `CODE_REVIEW-*-G??.md`.
