# openspec/changes/wf-cost-per-outcome/proposal.md

## Why

Agent spend arrives as tokens and dollars per vendor. Nobody can say what a recovered incident, a kept promise or a won deal cost. You can defend agent spend in a board meeting, and cut the spend that produces nothing.

## What Changes

- Add the `cost-per-outcome` workflow to the PromptReports workflow library, run by the workflow runner as an agent acting for a person.
- RepoOps: Read spend per outcome (spend_outcome) (today: Built).
- RepoOps: Send agent cost back to CommitOps (today: Switching on).
- Sales as Code: Read cost per forecast and per won deal (today: Built).
- PromptReports: Join spend to outcomes across products (today: Planned).
- PromptCompute: Hold spend above the budget for a person (cumulative limit) (today: Built).
- Gate `credits.top_up`, `subscription.plan.change` through PromptCompute approvals; each gated step runs only after `verifyResume` passes, with `final_args`.
- Write a signed record at the end of each run and count one workflow request.

## Impact

- Products: RepoOps, PromptCompute, CommitOps, Sales as Code.
- Outside the family: Your model provider billing export (optional).
- State: Partly built. Sales as Code shows cost per forecast and per won deal. Three parts to go.
- Plan row: LDG-2274.

# openspec/changes/wf-cost-per-outcome/tasks.md

## 1. Connect and authorize

- [ ] 1.1 Confirm the org has a connection for each of: RepoOps, PromptCompute, CommitOps, Sales as Code. Stop and tell the user which is missing.
- [ ] 1.2 Get a PromptReports ID agent token for each product the run calls (RFC 8693 exchange). Where a product does not accept ID tokens yet, use its connect key.
- [ ] 1.3 Register policy for `credits.top_up`, `subscription.plan.change` with `require_approval` as the default.

## 2. Build the steps

- [ ] 2.1 RepoOps: Read spend per outcome (spend_outcome).
- [ ] 2.2 RepoOps: Send agent cost back to CommitOps.
- [ ] 2.3 Sales as Code: Read cost per forecast and per won deal.
- [ ] 2.4 PromptReports: Join spend to outcomes across products. Blocked: this part is Planned; until it ships, record the step's evidence as `missing` and continue.
- [ ] 2.5 PromptCompute: Hold spend above the budget for a person (cumulative limit).

## 3. Verify and record

- [ ] 3.1 Run a dry run with the user's inputs: Period = This month; Budget = $4,000; Outcomes = Incidents, promises, deals.
- [ ] 3.2 Run it once for real; approve each gate as the user.
- [ ] 3.3 Measure the outcomes: Cost per recovered incident; Cost per kept promise; Share of agent spend tied to an outcome: target above 80%.
- [ ] 3.4 Confirm the run left a signed record with a task id and one workflow request counted.

# openspec/changes/wf-cost-per-outcome/specs/workflow-cost-per-outcome/spec.md

## ADDED Requirements

### Requirement: Spend over budget waits

The workflow SHALL route any agent spend that takes the period total past the budget to a person.

#### Scenario: Spend over budget waits

- **WHEN** a request would take the month past $4,000
- **THEN** PromptCompute holds it for a person instead of passing it

### Requirement: Evidence stays honest

Each step SHALL report what it saw as measured, inferred or missing, and SHALL never report missing as measured.

#### Scenario: A product part is not built

- **WHEN** a step depends on a part that is Planned or Proposed
- **THEN** the step records `missing`, names the part, and the approval card shows the gap

### Requirement: One run is one workflow request

The runner SHALL count one workflow request when the first step starts, and SHALL NOT count retries, resumes or dry runs.

#### Scenario: A gated step resumes after two days

- **WHEN** a person approves a step and the run resumes
- **THEN** the org's usage does not change
