# Factory Execution Workflow

**Status:** Approved target-state baseline  
**First realization:** I1.

## Target State

```text
PlanRevision + selected Task graph + ExecutionPolicy
  -> preflight dependencies/connections/provider/account/workspace capability
  -> create FactoryRun
  -> FactoryProvider materializes/starts graph
  -> for ready Task:
       create AgentRun
       select provider/account/model
       create Attempt + Workspace
       execute/checkpoint/observe
       collect ChangeSet/output
       update Task/graph
  -> review/correction as required
  -> advance newly-unblocked Tasks
  -> complete FactoryRun when selected graph outcomes accepted
```

## Preflight

Check immutable source revision, project bindings, execution profile, eligible accounts/models, cluster capability/capacity, Task dependencies and required policy/Approvals. Missing capacity/provider may queue; missing permission/connection creates attention.

## Attempt Start

Provider/account/model are resolved once and persisted. Workspace is prepared from immutable base/profile. Agent receives only attempt-scoped capabilities/credentials.

## Runtime Observation

Persist authoritative lifecycle transitions and business events; provider/tool messages are observations/projections. Realtime can show progress but lost stream does not lose state.

## WIP / Checkpoint

Periodic or milestone checkpoint before destructive retry/cancel. Process/pod/node interruption either resumes durable checkpoint or creates new Attempt from it.

## Account/Model Fallback

```text
Attempt N -> rate/capacity exhausted
  -> record reason + preserve checkpoint
  -> choose next eligible account/model
  -> Attempt N+1 with immutable new provenance
```

Dogfood preference: Spark low effort -> OpenCode Zen DeepSeek free -> OpenCode/OpenRouter North free, by test-purpose alias and live availability.

## Completion

Agent process exit alone does not complete Task. Required ChangeSet/output/evidence/review semantics decide Task completion. FactoryRun completes only when selected graph reaches accepted terminal state.

## Cancellation

Persist request, checkpoint/collect WIP, cancel provider/workload, reconcile resources, then mark cancelled. Cleanup failure is separate attention/Incident.

## Increment Realization

I1 implements small graph/full recovery path. I4 adds rich graph/chat/files/cost UX; I5 CI integrates; I8 resolver can initiate normal Factory changes.

## Acceptance Journeys

- happy path real code change;
- pod/process interruption with WIP survival;
- model/account exhaustion with new Attempt;
- review rejection -> correction -> re-review.

## Invariants

No workstation execution, no silent account switch, no WIP loss, no provider status as sole product truth, no agent publication authority.