<!-- task=m-principal-provider-credential-slot-routing/01_principal_store plan=1 tag=REVIEW_API -->

# Code Review Reference - REVIEW_API

> **[IMPLEMENTING AGENT — READ FIRST] Filling in this file is the mandatory final step of implementation.**
> The task is NOT complete until every implementation-owned section below is filled in.
> Complete the `Implementation Checklist`; the final checklist item is mandatory before saving.
> Fill implementation-owned sections, then stop with active files in place and report ready for review.
> If implementation is blocked, record the exact blocker, attempted commands/output, and resume condition only in implementation-owned evidence fields.
> Do not ask the user directly, present choices, call user-input tools, create control-plane stop files, or classify the next state.
> Finalization (`Code Review Result`, log rename, `complete.log`, archive moves, `Review-Only Checklist`) is review-agent-only, even after compaction/resume.
> Follow the ownership table at the bottom of this file for which sections you own.

## Overview

date=2026-08-01
task=m-principal-provider-credential-slot-routing/01_principal_store, plan=1, tag=REVIEW_API

## Roadmap Targets

- Milestone: `agent-roadmap/phase/operational-observability-provider-management/milestones/principal-provider-credential-slot-routing.md`
- Milestone link: [Milestone document](agent-roadmap/phase/operational-observability-provider-management/milestones/principal-provider-credential-slot-routing.md)
- Task ids:
  - `principal-store`: principal and IOP token hash ledger
- Completion mode: check-on-pass

## Archive Evidence Snapshot

- Prior task: `agent-task/m-principal-provider-credential-slot-routing/01_principal_store/`
- Prior plan: `agent-task/m-principal-provider-credential-slot-routing/01_principal_store/plan_local_G06_0.log`
- Prior review: `agent-task/m-principal-provider-credential-slot-routing/01_principal_store/code_review_cloud_G06_0.log`
- Verdict: FAIL
- Required findings:
  - PostgreSQL DDL and query placeholders are not executable through `pgx`, and unknown schemes containing `.db` can be misclassified as SQLite.
  - Disabled tokens cannot be revoked, CAS ignores affected-row count, and stale/concurrent results are not reliably fenced.
  - `ListPrincipals` scans nullable joined token columns into non-null Go fields after disable/revoke.
  - Recorded `ip/apps/...` verification output conflicts with module `iop` and fresh reviewer output.
- Affected files: `apps/control-plane/internal/credentialstore/store.go`, `apps/control-plane/internal/credentialstore/schema.go`, `apps/control-plane/internal/credentialstore/principal.go`, `apps/control-plane/internal/credentialstore/principal_test.go`, and the active review evidence file.
- Fresh reviewer evidence: `go vet ./apps/control-plane/...`, `go test -count=1 -race ./apps/control-plane/internal/credentialstore`, `go test -count=1 ./apps/control-plane/...`, `go mod tidy -diff`, and `git diff --check` exited zero; Go printed `iop/apps/...` package paths. These passing SQLite tests do not cover the Required failures above.
- Roadmap carryover: keep `principal-store` / SDD S01 targeted; do not update the roadmap until a later PASS completion event.

## For the Review Agent

> **[REVIEW AGENT ONLY]** The finalization steps below are review-agent only. Implementing agents must not execute this section.

Compare implementation of each item against source files and verify that output in `Verification Results` matches code.
Review completion means the following steps are finished:

1. Append verdict and `review_rework_count` / `evidence_integrity_failure` routing signals.
2. Archive `CODE_REVIEW-cloud-G08.md` → `code_review_cloud_G08_1.log` and `PLAN-cloud-G08.md` → `plan_cloud_G08_1.log`.
3. If PASS, write `complete.log` and move active task directory to `agent-task/archive/YYYY/MM/m-principal-provider-credential-slot-routing/01_principal_store/`. If WARN/FAIL, fully write the next filesystem state required by the code-review skill.
4. If PASS and task group is `m-<milestone-slug>`, report completion event metadata. Roadmap state check and `update-roadmap` calls are runtime responsibilities.
5. Check applicable `Review-Only Checklist` items at the final `.log` location before reporting.

---

## Implementation Item Completion

| Item | Status |
|------|---------|
| REVIEW_API-1 Make SQL dialect handling executable | [x] |
| REVIEW_API-2 Repair lifecycle CAS and inactive listing | [x] |
| REVIEW_API-3 Rebuild trusted verification evidence | [x] |

## Implementation Checklist

- [x] Make PostgreSQL/SQLite scheme selection, DDL, migrations, and query binding executable without fallback, with deterministic dialect regressions.
- [x] Enforce exact one-row revision/status CAS, disabled-to-revoked lifecycle, and inactive-principal listing, with sequential, concurrent, and reopen tests.
- [x] Run fresh package, race, vet, module, diff, and compiled Control Plane startup verification and record exact `iop/...` stdout/stderr.
- [x] Fill implementation-owned sections in CODE_REVIEW-*-G??.md with actual implementation notes and verification output.

## Review-Only Checklist

> **[REVIEW AGENT ONLY]** This checklist is used only by the review agent.
> Implementing agents must not modify or check this section.

- [x] Append one verdict of `PASS`, `WARN`, or `FAIL` and verified `review_rework_count`, `evidence_integrity_failure` to `Code Review Result`.
- [x] Verify that verdict, `Dimension Assessment`, and Required/Suggested/Nit classifications match.
- [x] Archive active `CODE_REVIEW-*-G??.md` to `code_review_cloud_G08_1.log`.
- [x] Archive active `PLAN-*-G??.md` to `plan_cloud_G08_1.log`.
- [x] Verify that the Agent-Ops managed block in `.gitignore` unignores `agent-task/**/*.md` and `agent-task/**/*.log` and ignores `agent-roadmap/current.md`.
- [x] If PASS, write `complete.log` based on `agent-ops/skills/common/code-review/templates/complete-log-template.md` and leave no active `.md` files.
- [x] If PASS, move active task directory `agent-task/m-principal-provider-credential-slot-routing/01_principal_store/` to `agent-task/archive/YYYY/MM/m-principal-provider-credential-slot-routing/01_principal_store/` and update this checklist at the final archive path.
- [x] If PASS and task group is `m-principal-provider-credential-slot-routing`, report completion event metadata for runtime, without modifying roadmap or directly calling `update-roadmap`.
- [x] If PASS for split work, remove empty active parent `agent-task/m-principal-provider-credential-slot-routing/` or verify it was kept due to remaining siblings/files.
- [ ] If WARN/FAIL, write the next filesystem state matching code-review verdict and do not write `complete.log`.

## Deviations from Plan

The compiled-entrypoint command initially used a system temporary directory, but this environment mounts `/tmp` with `noexec`. The same prescribed cycle was rerun with an executable temporary directory under the ignored workspace `.cache/` directory. Listener overrides, temporary SQLite URL, readiness check, SIGINT shutdown, and output capture were otherwise unchanged.

## Key Design Decisions

- `dialectFromURL` accepts only PostgreSQL URLs, `file:` URLs, and scheme-free SQLite paths; configured unknown schemes never fall back to SQLite.
- Schema statements are selected per dialect and executed one at a time. PostgreSQL uses `TIMESTAMPTZ` and ordinal placeholders; SQLite retains question-mark binding and text timestamps.
- Token transitions use one revision-and-status-fenced update. A zero-row update is classified from durable state, with stale revisions taking precedence and revoked tokens remaining terminal.

## Reviewer Checkpoints

- Confirm every PostgreSQL DDL statement uses supported types, every PostgreSQL parameterized query receives `$1...$n`, SQLite still opens/reopens, and no configured URL falls back to another dialect.
- Confirm revoke accepts active or disabled at the expected revision, revoked stays terminal, and every stale/concurrent loser is a typed non-success backed by an exact affected-row check.
- Confirm listing a principal with no active token succeeds with zero token metadata and does not expose raw token material.
- Re-run the targeted, race, full Control Plane, module/diff, and compiled-entrypoint checks; compare literal output to this file and require canonical `iop/...` package paths.

## Verification Results

Paste actual stdout/stderr beneath every command. Do not summarize reconstructed output; if output is too long, record the saved output path and exact capture command.

### REVIEW_API-1

`GOTMPDIR=/config/workspace/iop-s0/.cache/go-build GOCACHE=/config/workspace/iop-s0/.cache/go-cache go test -count=1 ./apps/control-plane/internal/credentialstore -run 'Dialect|Bind|Schema|Open|Reopen'`

```text
ok  	iop/apps/control-plane/internal/credentialstore	0.200s
```

### REVIEW_API-2

`GOTMPDIR=/config/workspace/iop-s0/.cache/go-build GOCACHE=/config/workspace/iop-s0/.cache/go-cache go test -count=1 -race ./apps/control-plane/internal/credentialstore`

```text
ok  	iop/apps/control-plane/internal/credentialstore	1.815s
```

### REVIEW_API-3 / Final Verification

1. `command -v go && go version && go env GOROOT`

```text
/config/.local/bin/go
go version go1.26.2 linux/arm64
/config/opt/go
```

2. `mkdir -p .cache/go-build .cache/go-cache`

```text
No stdout/stderr.
```

3. `gofmt -w apps/control-plane/internal/credentialstore/store.go apps/control-plane/internal/credentialstore/schema.go apps/control-plane/internal/credentialstore/principal.go apps/control-plane/internal/credentialstore/principal_test.go`

```text
No stdout/stderr.
```

4. `GOTMPDIR=/config/workspace/iop-s0/.cache/go-build GOCACHE=/config/workspace/iop-s0/.cache/go-cache go test -count=1 ./apps/control-plane/internal/credentialstore -run 'Dialect|Bind|Schema|Open|Reopen|RevokeDisabled|LosingCAS|ListPrincipalsWithoutActiveToken'`

```text
ok  	iop/apps/control-plane/internal/credentialstore	0.400s
```

5. `GOTMPDIR=/config/workspace/iop-s0/.cache/go-build GOCACHE=/config/workspace/iop-s0/.cache/go-cache go test -count=1 -race ./apps/control-plane/internal/credentialstore`

```text
ok  	iop/apps/control-plane/internal/credentialstore	1.815s
```

6. `GOTMPDIR=/config/workspace/iop-s0/.cache/go-build GOCACHE=/config/workspace/iop-s0/.cache/go-cache go vet ./apps/control-plane/...`

```text
No stdout/stderr.
```

7. `GOTMPDIR=/config/workspace/iop-s0/.cache/go-build GOCACHE=/config/workspace/iop-s0/.cache/go-cache go test -count=1 ./apps/control-plane/...`

```text
ok  	iop/apps/control-plane/cmd/control-plane	2.304s
ok  	iop/apps/control-plane/internal/credentialstore	0.315s
ok  	iop/apps/control-plane/internal/wire	1.594s
```

8. `GOTMPDIR=/config/workspace/iop-s0/.cache/go-build GOCACHE=/config/workspace/iop-s0/.cache/go-cache go mod tidy -diff`

```text
No stdout/stderr.
```

9. Local compiled-entrypoint cycle:

```bash
runtime_dir="$(mktemp -d)"
runtime_pid=""
cleanup_runtime() {
  if [ -n "$runtime_pid" ] && kill -0 "$runtime_pid" 2>/dev/null; then
    kill -INT "$runtime_pid"
    wait "$runtime_pid"
  fi
  rm -rf "$runtime_dir"
}
trap cleanup_runtime EXIT
GOTMPDIR=/config/workspace/iop-s0/.cache/go-build GOCACHE=/config/workspace/iop-s0/.cache/go-cache go build -o "$runtime_dir/control-plane" ./apps/control-plane/cmd/control-plane
IOP_LISTEN=127.0.0.1:0 IOP_WIRE_LISTEN=127.0.0.1:0 IOP_EDGE_WIRE_LISTEN=127.0.0.1:0 IOP_DATABASE_URL="$runtime_dir/credentials.db" "$runtime_dir/control-plane" serve --config "" >"$runtime_dir/stdout.log" 2>"$runtime_dir/stderr.log" &
runtime_pid=$!
for attempt in $(seq 1 50); do
  if [ -f "$runtime_dir/credentials.db" ]; then
    break
  fi
  if ! kill -0 "$runtime_pid" 2>/dev/null; then
    sed -n '1,160p' "$runtime_dir/stdout.log" "$runtime_dir/stderr.log"
    exit 1
  fi
  sleep 0.1
done
test -f "$runtime_dir/credentials.db"
rg --fixed-strings 'control-plane credential store ready' "$runtime_dir/stdout.log" "$runtime_dir/stderr.log"
kill -INT "$runtime_pid"
wait "$runtime_pid"
runtime_pid=""
```

```text
.cache/credentialstore-runtime.cXkWXB/stderr.log:{"level":"info","ts":1785560498.4306984,"caller":"control-plane/server.go:39","msg":"control-plane credential store ready","dialect":"sqlite"}
compiled-entrypoint cycle completed cleanly
```

10. `git diff --check`

```text
No stdout/stderr.
```

---

> **[IMPLEMENTING AGENT — BEFORE SAVING] Have you filled in every implementation-owned section?**
> If anything is blank, go back and fill it in before saving this file.
> Leave review-agent-only sections unchanged.

## Section Ownership

| Section | Owner | Note |
|---------|-------|------|
| Header comment, Overview, Review Agent Instructions | Fixed at stub creation | Implementing agent must not modify or execute these (archive, complete.log, and task-directory archive move are review-agent only) |
| Roadmap Targets | Fixed at stub creation from plan when present | Implementing agent must not modify; code-review copies it into `complete.log` as `Roadmap Completion` only on PASS |
| Archive Evidence Snapshot | Fixed at stub creation from plan when present | Implementing agent uses it as default prior-loop context; read only the specific archive files cited there when more detail is required |
| Implementation Item Completion (item names) | Fixed at stub creation | Implementing agent checks `[ ]` → `[x]` only |
| Implementation Checklist (item text/order) | Fixed at stub creation from plan | Implementing agent checks `[ ]` → `[x]` only |
| Review-Only Checklist | Review agent only | Implementing agent must not modify or check this section |
| Deviations from Plan, Key Design Decisions | Implementing agent | Replace placeholder text with actual content |
| Reviewer Checkpoints | Fixed at stub creation | Pre-filled from plan |
| Verification Results (section headings + commands) | Fixed at stub creation | Implementing agent fills in command output only; command changes require a `Deviations from Plan` entry |
| Code Review Result | Review agent appends | Not included in stub |

## Code Review Result

- Overall Verdict: PASS
- Dimension Assessment:
  - Correctness: Pass — PostgreSQL and SQLite now use executable dialect-specific DDL and bindings, lifecycle updates are revision/status fenced with exact affected-row checks, and inactive-principal listing handles nullable token columns.
  - Completeness: Pass — all follow-up checklist items and the targeted SDD S01 principal-store evidence are present.
  - Test Coverage: Pass — deterministic dialect, reopen, revoke-after-disable, concurrent CAS, inactive-list, full package, and compiled-entrypoint coverage passed fresh reviewer execution.
  - API Contract: Pass — configured database URLs fail closed, disabled tokens can be revoked, revoked tokens remain terminal, and stale revisions return the documented typed error.
  - Code Quality: Pass — the store remains cohesive; stale token-reference comments and an unused internal error declaration were repaired during review without behavioral changes.
  - Implementation Deviation: Pass — the executable workspace-local temporary directory is a justified environment-only adjustment to the prescribed compiled-entrypoint cycle.
  - Verification Trust: Pass — fresh reviewer commands reproduced canonical `iop/...` package paths and all claimed success conditions.
  - Spec Conformance: Pass — SDD S01 evidence establishes one-time raw-token return, digest-only durable storage, monotonic revision, irreversible revocation, and restart persistence.
- Findings: None
- Routing Signals:
  - review_rework_count=1
  - evidence_integrity_failure=false
- Next Step: PASS — write `complete.log`, archive this pair and task directory, and report milestone completion metadata for runtime processing.
