| 1 | --- |
| 2 | type: idea |
| 3 | id: ida-594df79 |
| 4 | status: exploring |
| 5 | title: klaus is the requirements oracle for S3 |
| 6 | created: 2026-07-13 |
| 7 | source: directed review, 2026-07-13 |
| 8 | --- |
| 9 | |
| 10 | # klaus is the requirements oracle for S3 |
| 11 | |
| 12 | **Thesis.** [klaus](https://github.com/patflynn/klaus) — the operator's daily agent orchestrator, |
| 13 | S3's anchor in the [ladder](../../design/user-scenarios.md#the-ladder) — is a _working_ system whose |
| 14 | observed behaviors are S3's requirements. It is GitHub-shaped end to end: PR-indexed pipeline FSM, |
| 15 | `gh`-mediated state, webhook-relay feed. The migration story is therefore not "rebuild klaus" but |
| 16 | **swap the GitHub feed for valley events** — klaus already anticipates this (its |
| 17 | `pr:approval-changed` event is documented as "deliberately backend-agnostic … for non-GitHub |
| 18 | merge-readiness sources", `internal/event/event.go`). |
| 19 | |
| 20 | ## Behavior → valley concept |
| 21 | |
| 22 | | klaus behavior (today) | valley concept | phase | |
| 23 | | ------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ----- | |
| 24 | | github-relay webhook feed + poll reconcile — events are _invalidation signals_; GitHub API is the state store | event log as the consumer feed (`ref-updated`, …) | 1 | |
| 25 | | `klaus approve` / `auto_merge_on_approval` / trusted-reviewer config | integrator policy | 3 | |
| 26 | | agent opens a PR (hard-coded in the default agent prompt, `internal/config/config.go`) | integration request | 3 | |
| 27 | | `klaus launch --issue` / prompt-carried dispatch | dispatch against an outcome node ([[ida-eac723e]], [ida-eac723e-outcome-dag.md](./ida-eac723e-outcome-dag.md)) | 4 | |
| 28 | | run state files + `refs/klaus/data` artifacts + dashboard + `klaus watch` JSONL | one queryable history | 5 | |
| 29 | | trusted-reviewer PR comment → fix-agent dispatch | threads / review-as-feedback | 7 | |
| 30 | | budget-pause: draft PR + `klaus:budget-paused` label + comment _is_ the persisted state; `klaus launch --pr` is the only resume | **no valley equivalent yet** — needs paused-work persistence, likely a node + branch | gap | |
| 31 | |
| 32 | ## Evidence: one week of S1 direct-push (the-valley, qinling) |
| 33 | |
| 34 | | # | observed | precise cause in klaus | |
| 35 | | --- | ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 36 | | 1 | agent pushed a branch, ended as bare `exited`; the operator finds work via `git ls-remote` | no event kind carries a pushed branch; `determineStatus` knows only PR/merged states (`internal/cmd/status.go`); default `klaus watch` filter omits `agent:completed` (`internal/cmd/watch.go`) | |
| 37 | | 2 | budget-capped direct-push agent has **no** persistence/resume story | `draft.HandleBudgetPause` pushes WIP fine (pure git) but then needs `gh pr create --draft` + label; on a sovereign origin that fails and `_finalize` falls through to a normal completion — no `agent:paused`, nothing to resume against | |
| 38 | | 3 | no "branch awaiting integration" state anywhere | pipeline FSM keyed by PR number (`internal/pipeline/pipeline.go`); `track`/`approve`/`merge` all take PR refs | |
| 39 | | 4 | a run got labeled with an upstream repo's PR #130 it merely _mentioned_ | `_finalize` regex-scrapes any `github.com/.../pull/N` URL from the transcript, last match wins, never checked against the run's target repo (`internal/cmd/hidden.go`) | |
| 40 | | 5 | agent killed by a usage limit died silently; only the log tail said why | crash detection reads only the final `result` JSONL line; a killed stream sets no `FailureReason` → false `agent:completed`; `agent:needs-attention` is also absent from the default watch filter | |
| 41 | |
| 42 | Gap 5 is [[ida-3145b7a]]'s stall row "agent run died silently" |
| 43 | ([ida-3145b7a-demand-pressure.md](./ida-3145b7a-demand-pressure.md)), observed in production — klaus |
| 44 | is the spawn-delivery mechanism that idea's demand pressure schedules, and it needs the |
| 45 | lease/re-dispatch machinery sketched there. |
| 46 | |
| 47 | ## What Phases 1/3/4 must minimally satisfy for klaus to swap feeds |
| 48 | |
| 49 | - **Phase 1 (events).** `ref-updated` carrying repo, ref, old/new sha, and pusher identity — that |
| 50 | alone kills gaps 1 and 5's silence (a branch push and a _missing_ expected push are both |
| 51 | observable). Remotely subscribable (klaus runs on the workstation, not classic-laddie) and |
| 52 | deterministically replayable, since klaus treats events as invalidation + reconcile, never as the |
| 53 | store. |
| 54 | - **Phase 3 (integrator).** `integration-requested` addressable by branch/request id, not PR number |
| 55 | — klaus's FSM re-keys from PR to request id; `integration-succeeded` / `request-stale` replace |
| 56 | what `gh pr view` answers today; plus a _queryable_ per-request status, because klaus's controller |
| 57 | is level-triggered and reconciles state, it doesn't just react. "Awaiting integration" (gap 3) |
| 58 | becomes a first-class stage. `klaus approve` becomes integrator policy input. |
| 59 | - **Phase 4 (dispatch + attribution).** Dispatch targets an outcome node — but klaus doctrine says |
| 60 | the prompt is the agent's _entire briefing_ (`docs/PIPELINE.md`), so the node body must be |
| 61 | brief-quality or dispatch stays prompt+node. Per-agent keys sign commits and attestations: |
| 62 | attribution then lives in git objects, which retires gap 4's transcript-scraping for good. Paused |
| 63 | work (gap 2) persists as a node + WIP branch — klaus already syncs the resume trajectory over |
| 64 | plain git (`refs/klaus/data`), so only the pause _marker_ and resume index need valley-native |
| 65 | homes. |
| 66 | |
| 67 | ## Related |
| 68 | |
| 69 | - Dispatch semantics and the outcome DAG this feeds: [[ida-eac723e]] |
| 70 | ([ida-eac723e-outcome-dag.md](./ida-eac723e-outcome-dag.md)) |
| 71 | - Demand pressure / anti-stall, whose failure taxonomy gap 5 instantiates: [[ida-3145b7a]] |
| 72 | ([ida-3145b7a-demand-pressure.md](./ida-3145b7a-demand-pressure.md)) |
| 73 | - Near-term operational fixes filed upstream: patflynn/klaus issues (branch-push visibility, |
| 74 | sovereign budget-pause, PR-URL misattribution) |