1---2name: optimizing-a-pipeline3description: Optimize an existing named Superpipelines workflow for topology, model tiers, cost, latency, and reliability.4---56# Optimizing a Pipeline — On-Demand Optimization Workflow78<overview>9Top-level orchestrator for optimizing an existing pipeline. A read-only `pipeline-optimizer` analyst surveys the selected bundle across four axes (topology structure, model-tier cost, past-run signals, protocol/prompt quality); a discovery session (4D → brainstorm → grill) converges the findings into an `optimization_plan` with the user; the approved plan is batch-applied atomically through the existing mutation and `change-models` engines, gated by a `pipeline-auditor` DELTA pass and proven by a mandatory full audit. Orchestration lives only here (`SUB_AGENT_SPAWNING: FALSE`); the optimizer never mutates (render-inline, #33).10</overview>1112<glossary>13 <term name="Opportunity">A single proposed improvement rendered by the optimizer, carrying an axis, impact, affected steps, and a suggested engine.</term>14 <term name="optimization_plan">The reconciled set of accepted/rejected/modified opportunities returned by `sk-pipeline-grilling MODE=optimization`.</term>15 <term name="Batch apply">Staging ALL approved changes in one `edit-{ts}/` and promoting them all-or-nothing — a multi-change optimization is one semantic change.</term>16 <term name="Snapshot">A pre-mutation copy of the bundle in `edit-{ts}/backup/` plus a git checkpoint, used for rollback.</term>17</glossary>1819## Workflow Phases2021<protocol>22### PHASE 0 — SELECTION23- Reuse the `running-a-pipeline` Phase 0 multi-scope discovery pattern: call `sk-pipeline-paths.ENUMERATE_ALL_SCOPE_ROOTS(workspace)`, merge every `<root>/superpipelines/registry.json`, annotate each entry with `source_tier` and `scope`.24- Present the pipelines; capture the selection `{ROOT, P, pattern, source_tier}`.25- Load `sk-platform-dispatch` → `DETECT()` → `platform_profile` (cache once; same probe/fallback rules as `running-a-pipeline` Phase 0.25). Emit every `platform_profile.degradation_warnings` entry.26- IF `$ARGUMENTS` named a pipeline, pre-select it; still confirm before proceeding.2728### PHASE 0.5 — NO-ACTIVE-RUN SOFT GATE29- Scan `<ROOT>/superpipelines/temp/{P}/*` for run directories; read each `pipeline-state.json` top-level `status`.30- IF any run is `running` or `escalated` (non-terminal):31 - `AskUserQuestion`: **(a) discard those run states** and proceed (delete the non-terminal run dirs), or **(b) abort** and let the user finish/handle them manually.32- <HARD-GATE>NEVER stage or mutate the bundle while a `running`/`escalated` run exists unless the user explicitly chose discard. `escalated`/`failed` run dirs are never deleted silently.</HARD-GATE>3334### PHASE 1 — SURVEY35- Dispatch the read-only `pipeline-optimizer` via profile-driven dispatch — the SAME `platform_profile.capabilities.dispatch_mechanism` branching used by `creating-a-pipeline` Phase 4 (`native_task` → `Task()`; `native_subagent` / `model_driven` → platform-native; `inline` → Tier 2 inline loop). Hand it absolute paths (resolved via `sk-pipeline-paths`) to `topology.json`, the bundle `agents/` dir, the `temp/{P}/*/pipeline-state.json` history, any `run-telemetry.jsonl`, and the `platform_profile`.36- The optimizer renders an opportunity report as terminal output and NEVER writes a file.37- <HARD-GATE>Persistence is the orchestrator's job (#33): write the rendered report to `<ROOT>/superpipelines/temp/{P}/optimize-{ts}/findings.md` (ensure the dir exists first).</HARD-GATE>38- IF the optimizer returns `DONE_WITH_CONCERNS` (telemetry-blind axes), surface the advisory on enabling the opt-in telemetry hook (`CLAUDE_CODE_ENHANCED_TELEMETRY_BETA=1` + register `subagent-telemetry`). NEVER auto-edit settings.39- IF no opportunities were found: report that and exit cleanly — nothing to optimize.4041### PHASE 2 — DISCOVERY42- `sk-4d-method` — frame what "better" means for this pipeline (cost? latency? reliability? clarity?). Produce the `hardened` constraints.43- `superpipelines:brainstorming` — divergent exploration of alternative redesigns and their trade-offs against the findings.44- `sk-pipeline-grilling GRILL(MODE=optimization, findings, hardened)` — convergent: walk each opportunity one at a time, capturing accept/reject/modify + rationale. Returns the `optimization_plan`.45- <HARD-GATE>The grilling reconciliation gate must close with ZERO unresolved opportunities before Phase 3.</HARD-GATE>4647### PHASE 3 — PLAN GATE (single human approval)48- Present the `optimization_plan` concretely: the chosen opportunities, the resulting topology diff (steps merged/split/parallelized/removed), the model-tier diff, and the predicted effect against the `hardened` success criteria.49- ONE `AskUserQuestion` approval for the whole plan (plan-gate + batch-apply: the changes interact and must be approved/audited/promoted together).50- <HARD-GATE>No staging, snapshot, or mutation before this approval returns yes. Rejection ends the workflow with the findings preserved.</HARD-GATE>5152### PHASE 4 — BATCH APPLY (atomic)53- **Snapshot:** copy the bundle to `<ROOT>/superpipelines/temp/{P}/edit-{ts}/backup/` AND create a git checkpoint commit. This is the rollback source.54- **Stage ALL changes in one `edit-{ts}/`** (never promote partials):55 - Topology changes (merge/split/parallelize/reorder/remove) route through the existing mutation engines — `updating-a-pipeline-step` / `adding-a-pipeline-step` / `deleting-a-pipeline-step` (architect STEP-* modes), staging into the shared `edit-{ts}/`.56 - Model-tier changes route through `change-models` **Mode C** (per-agent `model_tier:` override) — recommend a tier direction only; concrete model IDs stay in profile JSON (`DEPENDENCY_INVERSION`).57 - Advisory-only (Axis-4) opportunities are NOT auto-applied; surface them for manual follow-up.58- **DELTA audit:** run ONE combined `pipeline-auditor` DELTA pass over the whole staged delta.59- <HARD-GATE>SEV-0/1 == 0 is required to promote. Any SEV-0/1 → roll back from the snapshot, restore the git checkpoint, and surface the findings. Do NOT promote a partial set.</HARD-GATE>60- **Promote all-or-nothing:** on a clean DELTA audit, promote the entire `edit-{ts}/` atomically.61- **Stamp:** bump `plugin_version` on `topology.json`, the `registry.json` entry, and every touched agent; set `topology.metadata.optimization = { ts, opportunities_applied: [...], baseline_ref }` (`baseline_ref` = the git checkpoint).6263### PHASE 5 — POST-APPLY PROOF64- <HARD-GATE>MANDATORY full `pipeline-auditor` pass over the promoted bundle. Any SEV-0/1 → auto-rollback from the snapshot + git checkpoint.</HARD-GATE>65- **Graph-integrity check:** every `depends_on` resolves to an existing step; no orphan edges; no unreachable non-entry step; I/O contracts chain (each consumed input is produced upstream). Any failure → auto-rollback.66- Persist the auditor report per the `commands/audit-steps.md` REPORTING contract (orchestrator owns persistence; ensure `audit/` exists; write `audit/latest.md`; update `registry.json last_audit`).67- **Offer** an optional live smoke-run via `running-a-pipeline` (not mandatory — `PARITY_TESTING: MANUAL_PHASE1`).68- Write the durable provenance report to `<ROOT>/superpipelines/pipelines/{P}/optimization-report-{ts}.md` (opportunities applied/rejected, diffs, audit verdict, baseline_ref).69</protocol>7071<invariants>72- No mutation under a live run — Phase 0.5 soft gate with explicit discard-or-abort; `escalated`/`failed` runs are never deleted silently.73- Snapshot (`edit-{ts}/backup/`) + git checkpoint precede any production write.74- All-or-nothing promotion; roll back from the snapshot on any DELTA-audit, full-audit, graph-integrity, or promotion failure.75- SEV-0/1 == 0 gates BOTH the DELTA (pre-promote) and full (post-promote) audits.76- `plugin_version` is re-stamped on `topology.json`, the registry entry, and every touched agent on promotion.77- The optimizer is read-only and renders inline; the orchestrator persists (#33). Orchestration is top-level only (`SUB_AGENT_SPAWNING: FALSE`).78- Isolation-correctness and frontmatter-compliance are delegated to `pipeline-auditor` — never re-checked here (`DEPENDENCY_INVERSION`).79- No concrete model IDs in this body; model-tier changes name a tier direction only and route through `change-models` Mode C.80- One plan gate (Phase 3); batch-apply is one semantic change.81</invariants>8283## Red Flags — STOP84- "The optimizer can write `findings.md` itself to save a step." → **STOP**. Read-only render-inline; the orchestrator persists (#33).85- "Promote the topology changes now; apply the tier changes after." → **STOP**. Batch-apply is all-or-nothing; partial promotion leaves the bundle in an unaudited interleaved state.86- "The DELTA audit found a SEV-1, but it's minor — promote anyway." → **STOP**. SEV-0/1 == 0 gates promotion. Roll back.87- "A run is escalated, but optimizing won't touch it." → **STOP**. Phase 0.5 gate: mutating the definition under a live run corrupts resume. Discard explicitly or abort.88- "Skip the snapshot — the git checkpoint is enough." → **STOP**. Both are required; the snapshot is the staging-local rollback source, the checkpoint the version baseline.89- "Re-check the isolation defect while surveying." → **STOP**. That is `pipeline-auditor`'s job (`DEPENDENCY_INVERSION`); the optimizer delegates.90- "Apply the advisory (Axis-4) quality fixes automatically." → **STOP**. Advisory-only opportunities are surfaced for manual decision, never auto-applied.9192## Rationalization Table9394<rationalization_table>95| Excuse | Reality |96| :--- | :--- |97| "One plan gate is too slow — approve each change inline." | The changes interact; piecemeal approval can promote a half-coherent topology. One plan, one gate, one atomic promote. |98| "The optimizer already audited the bundle." | It did not — it surveys opportunities. Compliance/isolation is the auditor's DELTA + full passes (`DEPENDENCY_INVERSION`). |99| "Skip the post-apply full audit; the DELTA passed." | The DELTA only saw the changed delta. The full pass + graph-integrity prove the whole bundle still chains. |100| "No telemetry, so skip past-run analysis silently." | Degrade and SAY SO — surface the opt-in hook advisory so the next run can ground cost/latency signals. |101| "Down-tier this step to `fast` and name the model." | Name a tier direction only; route through `change-models` Mode C. Concrete IDs live in profile JSON. |102| "Rolling back is wasteful after staging so much." | A failed audit/graph check means the staged set is unsafe. Rollback is the contract, not a failure of effort. |103</rationalization_table>104105## Reference Files106- `agents/pipeline-optimizer.md` + `skills/pipeline-optimizer-protocol/SKILL.md` — the read-only survey worker.107- `skills/pipeline-optimizer-references/references/opportunity-taxonomy.md` — opportunity classes + heuristics.108- `sk-pipeline-grilling/SKILL.md` — `MODE=optimization` reconciliation (returns `optimization_plan`).109- `sk-4d-method/SKILL.md` · `superpipelines:brainstorming` — the discovery session.110- `updating-a-pipeline-step` · `adding-a-pipeline-step` · `deleting-a-pipeline-step` — topology mutation engines (`edit-{ts}/` staging).111- `change-models/SKILL.md` — Mode C per-agent `model_tier:` override.112- `pipeline-auditor` + `commands/audit-steps.md` — DELTA + full audit and report persistence.113- `running-a-pipeline/SKILL.md` — Phase 0 discovery pattern reused here; optional Phase 5 smoke-run.114- `sk-pipeline-paths/SKILL.md` — scope-root and path resolution.