Files
Bo 242da84ec2 feat(compile): type-check the context-compiler thesis as Go (ag-mfh #context-compiler-typed) (#553)
## Summary

Add `cli/internal/compile/` — the operational-flow axis sibling of
`cli/internal/posture/` (merged via PR #551). Together they model the
same six-layer agent stack from two angles: **posture** is the
structural-ownership axis (who owns which layer across projects),
**compile** is the operational-flow axis (how context flows through one
loop tick).

This PR types the AgentOps 3.0 core thesis — *"AgentOps is the
in-session agent operating loop and the context compiler that feeds it"*
— that previously lived only as doctrine prose.

One package, two files, 368 LOC total. 9 tests, all PASS.

## What changed

- **`cli/internal/compile/compile.go`** — types for the operational-flow
axis:
- `Phase` enum + `PhaseSet` bitfield — the five canonical beats from
`loop.md` (Research, Plan, Implement, Validate, Ratchet)
- `DensityPayload` enum — the six allowed payloads from
`context-density-rule.md` (Intent, Boundary, Evidence, Decision,
Constraint, NextAction)
- `ContextBundle` — the artifact at every loop edge; each field carries
one named payload kind (Density Rule typed at struct level)
  - `LoopTick` — `BeadID` + `Phase` + `In`/`Out` bundle
- `Learning` + `Constraint` + `EnforcedBy` + `PromoteToConstraint()` —
the ratchet rule "knowledge becomes constraints" expressed as a typed
function (anonymous learnings cannot ratchet; every Constraint must name
its enforcement mechanism)
- `LoopDeclaration` + `AgentOpsLoop` — the 3.0 thesis "AgentOps IS the
in-session operating loop" as a `go build` claim
- **`cli/internal/compile/compile_test.go`** — 9 tests covering the
invariants:
  1. `PhaseEnum_FiveCanonicalBeats`
  2. `NoSixthPhase` (parity with posture's `TestNoSeventhLayer`)
3. `ContextBundleHasFieldPerPayload` (Density Rule alignment, via
reflect)
  4. `LoopTickShape`
  5. `Ratchet_PromoteRequiresCitation`
  6. `Ratchet_PromoteRequiresEnforcedBy`
  7. `Ratchet_PromoteHappyPath`
  8. `AgentOpsLoopOwnsAllFiveBeats`
  9. `PhaseSetOps`

## Sibling pattern

Mirrors the posture.go shape from PR #551 (commit `733ebc5b`). Same
shape, different axis:

| posture (PR #551) | compile (this PR) | Models |
|---|---|---|
| `type Layer uint8` (6) | `type Phase uint8` (5) | layer / beat enum |
| `type LayerSet uint8` | `type PhaseSet uint8` | bit-set sibling |
| `var AllLayers` | `var AllPhases` | universe |
| `var ExternalLayers` | (none — phases are all in-session) |
external-dep layer |
| `type Kind` (Substrate/Distribution/Sovereign) | (none — phases
invariant across postures) | posture family |
| `type Stance struct{ Kind; OwnedLayers }` | `type LoopDeclaration
struct{ OwnedPhases }` | distribution claim |
| `var AgentOps = Stance{...}` | `var AgentOpsLoop =
LoopDeclaration{...}` | what this repo owns |

The package doc-comments cross-reference each other; readers landing in
either can find the other immediately.

## Why now

This PR closes the load-bearing follow-up the audit on PR #551 surfaced.
The posture/ rename addressed naming; the deeper concern was that the
**core** axis (context-as-artifact, fractal loop, ratchet) remained
un-typed while a secondary axis was about to ship. ag-mfh closes that
loop.

The core thesis was — and still is — well-expressed in `docs/3.0.md`,
`PRODUCT.md`, `README.md`, `docs/architecture/operating-loop.md`, and
`skills/domain/references/context-compiler.md`. As of this PR it is
*also* expressed in Go types that `go build` checks.

## Fitness delta

- **Typed expression of the AgentOps 3.0 core thesis: 0 → 1**
- Typed model axes in `cli/internal/`: structural-only → **structural +
operational**

## Honest scope (this time as a feature, not a hedge)

This PR types **shape**, not **persistence**. ContextBundle describes
what crosses a loop edge; mapping bundles to `.agents/*.jsonl` lives in
a separate adapter package per hexagonal discipline (domain knows
nothing about its IO). Methods like `Compact()` and `Densify()` are
deliberately not here — they're follow-ups that can ride on these types
being a stable contract.

## Deferred follow-ups

(Will file as `discovered-from:ag-mfh` after merge; not bundled.)

1. `.agents/` JSONL persistence adapter for ContextBundle
round-tripping.
2. ContextBundle `Compact()` / `Densify()` methods (operate on bundles
per Density Rule).
3. SKILL.md frontmatter `phase:` field + lint rule (each skill declares
its loop phase).
4. `docs/architecture/agent-stack-two-axes.md` — doctrine doc now that
BOTH typed axes exist (posture + compile). Mirrors the post-ag-1w7
deferral pattern.

## Validation

```
gofmt -l: clean
go vet ./internal/compile/...: clean
go build ./...: status 0
go test -count=1 ./internal/compile/... ./internal/posture/... ./internal/gascity/...: PASS (9/9 compile, no regression)
LOC: 368 total (199 compile.go + 169 compile_test.go)
```

## Trailers

Closes-scenario: ag-mfh#phase-enum-five-canonical-beats
Closes-scenario: ag-mfh#context-bundle-six-density-payloads
Closes-scenario: ag-mfh#loop-tick-shape
Closes-scenario: ag-mfh#ratchet-promote-to-constraint
Closes-scenario: ag-mfh#agentops-loop-owns-all-five-beats
Bounded-context: BC1-Corpus
Evidence: skills/domain/references/context-compiler.md
Evidence: skills/domain/references/loop.md
Evidence: skills/domain/references/context-density-rule.md
Evidence: cli/internal/compile/compile_test.go (9 tests, all PASS)

Sibling pattern: mirrors the posture.go shape from PR #551 (commit
733ebc5b).
2026-05-27 00:43:39 +00:00
..